blob: c89e58090598f7326410a149149cab3df4e5cd51 [file] [log] [blame]
Patrick McHardy8532c702010-05-21 12:57:23 +02001/*
2 * "TEE" target extension for iptables
3 * Copyright © Sebastian Claßen <sebastian.classen [at] freenet.ag>, 2007
4 * Jan Engelhardt <jengelh [at] medozas de>, 2007 - 2010
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License; either
8 * version 2 of the License, or any later version, as published by the
9 * Free Software Foundation.
10 */
11#include <sys/socket.h>
12#include <getopt.h>
13#include <stdbool.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17
18#include <arpa/inet.h>
19#include <net/if.h>
20#include <netinet/in.h>
21
22#include <xtables.h>
23#include <linux/netfilter.h>
24#include <linux/netfilter/x_tables.h>
25#include <linux/netfilter/xt_TEE.h>
26
27enum {
Jan Engelhardtd44c31a2011-04-14 13:42:43 +020028 O_GATEWAY = 0,
29 O_OIF,
Patrick McHardy8532c702010-05-21 12:57:23 +020030};
31
Jan Engelhardtd44c31a2011-04-14 13:42:43 +020032#define s struct xt_tee_tginfo
33static const struct xt_option_entry tee_tg_opts[] = {
Jan Engelhardtd7282412011-05-04 16:41:13 +020034 {.name = "gateway", .id = O_GATEWAY, .type = XTTYPE_HOST,
Jan Engelhardtd44c31a2011-04-14 13:42:43 +020035 .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, gw)},
36 {.name = "oif", .id = O_OIF, .type = XTTYPE_STRING,
37 .flags = XTOPT_PUT, XTOPT_POINTER(s, oif)},
38 XTOPT_TABLEEND,
Patrick McHardy8532c702010-05-21 12:57:23 +020039};
Jan Engelhardtd44c31a2011-04-14 13:42:43 +020040#undef s
Patrick McHardy8532c702010-05-21 12:57:23 +020041
42static void tee_tg_help(void)
43{
44 printf(
45"TEE target options:\n"
46" --gateway IPADDR Route packet via the gateway given by address\n"
47" --oif NAME Include oif in route calculation\n"
48"\n");
49}
50
Patrick McHardy8532c702010-05-21 12:57:23 +020051static void tee_tg_print(const void *ip, const struct xt_entry_target *target,
52 int numeric)
53{
54 const struct xt_tee_tginfo *info = (const void *)target->data;
55
56 if (numeric)
Jan Engelhardt73866352010-12-18 02:04:59 +010057 printf(" TEE gw:%s", xtables_ipaddr_to_numeric(&info->gw.in));
Patrick McHardy8532c702010-05-21 12:57:23 +020058 else
Jan Engelhardt73866352010-12-18 02:04:59 +010059 printf(" TEE gw:%s", xtables_ipaddr_to_anyname(&info->gw.in));
Patrick McHardy8532c702010-05-21 12:57:23 +020060 if (*info->oif != '\0')
Jan Engelhardt73866352010-12-18 02:04:59 +010061 printf(" oif=%s", info->oif);
Patrick McHardy8532c702010-05-21 12:57:23 +020062}
63
64static void tee_tg6_print(const void *ip, const struct xt_entry_target *target,
65 int numeric)
66{
67 const struct xt_tee_tginfo *info = (const void *)target->data;
68
69 if (numeric)
Jan Engelhardt73866352010-12-18 02:04:59 +010070 printf(" TEE gw:%s", xtables_ip6addr_to_numeric(&info->gw.in6));
Patrick McHardy8532c702010-05-21 12:57:23 +020071 else
Jan Engelhardt73866352010-12-18 02:04:59 +010072 printf(" TEE gw:%s", xtables_ip6addr_to_anyname(&info->gw.in6));
Patrick McHardy8532c702010-05-21 12:57:23 +020073 if (*info->oif != '\0')
Jan Engelhardt73866352010-12-18 02:04:59 +010074 printf(" oif=%s", info->oif);
Patrick McHardy8532c702010-05-21 12:57:23 +020075}
76
77static void tee_tg_save(const void *ip, const struct xt_entry_target *target)
78{
79 const struct xt_tee_tginfo *info = (const void *)target->data;
80
Jan Engelhardt73866352010-12-18 02:04:59 +010081 printf(" --gateway %s", xtables_ipaddr_to_numeric(&info->gw.in));
Patrick McHardy8532c702010-05-21 12:57:23 +020082 if (*info->oif != '\0')
Jan Engelhardt73866352010-12-18 02:04:59 +010083 printf(" --oif %s", info->oif);
Patrick McHardy8532c702010-05-21 12:57:23 +020084}
85
86static void tee_tg6_save(const void *ip, const struct xt_entry_target *target)
87{
88 const struct xt_tee_tginfo *info = (const void *)target->data;
89
Jan Engelhardt73866352010-12-18 02:04:59 +010090 printf(" --gateway %s", xtables_ip6addr_to_numeric(&info->gw.in6));
Patrick McHardy8532c702010-05-21 12:57:23 +020091 if (*info->oif != '\0')
Jan Engelhardt73866352010-12-18 02:04:59 +010092 printf(" --oif %s", info->oif);
Patrick McHardy8532c702010-05-21 12:57:23 +020093}
94
95static struct xtables_target tee_tg_reg = {
96 .name = "TEE",
97 .version = XTABLES_VERSION,
98 .revision = 1,
99 .family = NFPROTO_IPV4,
100 .size = XT_ALIGN(sizeof(struct xt_tee_tginfo)),
101 .userspacesize = XT_ALIGN(sizeof(struct xt_tee_tginfo)),
102 .help = tee_tg_help,
Patrick McHardy8532c702010-05-21 12:57:23 +0200103 .print = tee_tg_print,
104 .save = tee_tg_save,
Jan Engelhardtd44c31a2011-04-14 13:42:43 +0200105 .x6_parse = xtables_option_parse,
106 .x6_options = tee_tg_opts,
Patrick McHardy8532c702010-05-21 12:57:23 +0200107};
108
109static struct xtables_target tee_tg6_reg = {
110 .name = "TEE",
111 .version = XTABLES_VERSION,
112 .revision = 1,
113 .family = NFPROTO_IPV6,
114 .size = XT_ALIGN(sizeof(struct xt_tee_tginfo)),
115 .userspacesize = XT_ALIGN(sizeof(struct xt_tee_tginfo)),
116 .help = tee_tg_help,
Patrick McHardy8532c702010-05-21 12:57:23 +0200117 .print = tee_tg6_print,
118 .save = tee_tg6_save,
Jan Engelhardtd44c31a2011-04-14 13:42:43 +0200119 .x6_parse = xtables_option_parse,
120 .x6_options = tee_tg_opts,
Patrick McHardy8532c702010-05-21 12:57:23 +0200121};
122
Jan Engelhardt0428e5a2010-08-03 19:58:38 +0200123void _init(void)
Patrick McHardy8532c702010-05-21 12:57:23 +0200124{
125 xtables_register_target(&tee_tg_reg);
126 xtables_register_target(&tee_tg6_reg);
127}