iptables-1.3.2-20050720
[iptables.git] / extensions / libipt_ROUTE.c
1 /* Shared library add-on to iptables to add ROUTE target support.
2  * Author : Cedric de Launois, <delaunois@info.ucl.ac.be>
3  * v 1.11 2004/11/23
4  */
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <getopt.h>
10 #include <iptables.h>
11 #include <net/if.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <linux/netfilter_ipv4/ip_tables.h>
16 #include <linux/netfilter_ipv4/ipt_ROUTE.h>
17
18 /* compile IPT_ROUTE_TEE support even if kernel headers are unpatched */
19 #ifndef IPT_ROUTE_TEE
20 #define IPT_ROUTE_TEE           0x02
21 #endif
22
23 /* Function which prints out usage message. */
24 static void
25 help(void)
26 {
27         printf(
28 "ROUTE target v%s options:\n"
29 "    --oif   \tifname \t\tRoute packet through `ifname' network interface\n"
30 "    --iif   \tifname \t\tChange packet's incoming interface to `ifname'\n"
31 "    --gw    \tip     \t\tRoute packet via this gateway `ip'\n"
32 "    --continue\t     \t\tRoute packet and continue traversing the\n"
33 "            \t       \t\trules. Not valid with --iif or --tee.\n"
34 "    --tee\t  \t\tDuplicate packet, route the duplicate,\n"
35 "            \t       \t\tcontinue traversing with original packet.\n"
36 "            \t       \t\tNot valid with --iif or --continue.\n"
37 "\n",
38 "1.11");
39 }
40
41 static struct option opts[] = {
42         { "oif", 1, 0, '1' },
43         { "iif", 1, 0, '2' },
44         { "gw", 1, 0, '3' },
45         { "continue", 0, 0, '4' },
46         { "tee", 0, 0, '5' },
47         { 0 }
48 };
49
50 /* Initialize the target. */
51 static void
52 init(struct ipt_entry_target *t, unsigned int *nfcache)
53 {
54         struct ipt_route_target_info *route_info = 
55                 (struct ipt_route_target_info*)t->data;
56
57         route_info->oif[0] = '\0';
58         route_info->iif[0] = '\0';
59         route_info->gw = 0;
60         route_info->flags = 0;
61 }
62
63
64 #define IPT_ROUTE_OPT_OIF      0x01
65 #define IPT_ROUTE_OPT_IIF      0x02
66 #define IPT_ROUTE_OPT_GW       0x04
67 #define IPT_ROUTE_OPT_CONTINUE 0x08
68 #define IPT_ROUTE_OPT_TEE      0x10
69
70 /* Function which parses command options; returns true if it
71    ate an option */
72 static int
73 parse(int c, char **argv, int invert, unsigned int *flags,
74       const struct ipt_entry *entry,
75       struct ipt_entry_target **target)
76 {
77         struct ipt_route_target_info *route_info = 
78                 (struct ipt_route_target_info*)(*target)->data;
79
80         switch (c) {
81         case '1':
82                 if (*flags & IPT_ROUTE_OPT_OIF)
83                         exit_error(PARAMETER_PROBLEM,
84                                    "Can't specify --oif twice");
85
86                 if (*flags & IPT_ROUTE_OPT_IIF)
87                         exit_error(PARAMETER_PROBLEM,
88                                    "Can't use --oif and --iif together");
89
90                 if (check_inverse(optarg, &invert, NULL, 0))
91                         exit_error(PARAMETER_PROBLEM,
92                                    "Unexpected `!' after --oif");
93
94                 if (strlen(optarg) > sizeof(route_info->oif) - 1)
95                         exit_error(PARAMETER_PROBLEM,
96                                    "Maximum interface name length %u",
97                                    sizeof(route_info->oif) - 1);
98
99                 strcpy(route_info->oif, optarg);
100                 *flags |= IPT_ROUTE_OPT_OIF;
101                 break;
102
103         case '2':
104                 if (*flags & IPT_ROUTE_OPT_IIF)
105                         exit_error(PARAMETER_PROBLEM,
106                                    "Can't specify --iif twice");
107
108                 if (*flags & IPT_ROUTE_OPT_OIF)
109                         exit_error(PARAMETER_PROBLEM,
110                                    "Can't use --iif and --oif together");
111
112                 if (check_inverse(optarg, &invert, NULL, 0))
113                         exit_error(PARAMETER_PROBLEM,
114                                    "Unexpected `!' after --iif");
115
116                 if (strlen(optarg) > sizeof(route_info->iif) - 1)
117                         exit_error(PARAMETER_PROBLEM,
118                                    "Maximum interface name length %u",
119                                    sizeof(route_info->iif) - 1);
120
121                 strcpy(route_info->iif, optarg);
122                 *flags |= IPT_ROUTE_OPT_IIF;
123                 break;
124
125         case '3':
126                 if (*flags & IPT_ROUTE_OPT_GW)
127                         exit_error(PARAMETER_PROBLEM,
128                                    "Can't specify --gw twice");
129
130                 if (check_inverse(optarg, &invert, NULL, 0))
131                         exit_error(PARAMETER_PROBLEM,
132                                    "Unexpected `!' after --gw");
133
134                 if (!inet_aton(optarg, (struct in_addr*)&route_info->gw)) {
135                         exit_error(PARAMETER_PROBLEM,
136                                    "Invalid IP address %s",
137                                    optarg);
138                 }
139
140                 *flags |= IPT_ROUTE_OPT_GW;
141                 break;
142
143         case '4':
144                 if (*flags & IPT_ROUTE_OPT_CONTINUE)
145                         exit_error(PARAMETER_PROBLEM,
146                                    "Can't specify --continue twice");
147                 if (*flags & IPT_ROUTE_OPT_TEE)
148                         exit_error(PARAMETER_PROBLEM,
149                                    "Can't specify --continue AND --tee");
150
151                 route_info->flags |= IPT_ROUTE_CONTINUE;
152                 *flags |= IPT_ROUTE_OPT_CONTINUE;
153
154                 break;
155
156         case '5':
157                 if (*flags & IPT_ROUTE_OPT_TEE)
158                         exit_error(PARAMETER_PROBLEM,
159                                    "Can't specify --tee twice");
160                 if (*flags & IPT_ROUTE_OPT_CONTINUE)
161                         exit_error(PARAMETER_PROBLEM,
162                                    "Can't specify --tee AND --continue");
163
164                 route_info->flags |= IPT_ROUTE_TEE;
165                 *flags |= IPT_ROUTE_OPT_TEE;
166
167                 break;
168
169         default:
170                 return 0;
171         }
172
173         return 1;
174 }
175
176
177 static void
178 final_check(unsigned int flags)
179 {
180         if (!flags)
181                 exit_error(PARAMETER_PROBLEM,
182                            "ROUTE target: oif, iif or gw option required");
183
184         if ((flags & (IPT_ROUTE_OPT_CONTINUE|IPT_ROUTE_OPT_TEE)) && (flags & IPT_ROUTE_OPT_IIF))
185                 exit_error(PARAMETER_PROBLEM,
186                            "ROUTE target: can't continue traversing the rules with iif option");
187 }
188
189
190 /* Prints out the targinfo. */
191 static void
192 print(const struct ipt_ip *ip,
193       const struct ipt_entry_target *target,
194       int numeric)
195 {
196         const struct ipt_route_target_info *route_info
197                 = (const struct ipt_route_target_info *)target->data;
198
199         printf("ROUTE ");
200
201         if (route_info->oif[0])
202                 printf("oif:%s ", route_info->oif);
203
204         if (route_info->iif[0])
205                 printf("iif:%s ", route_info->iif);
206
207         if (route_info->gw) {
208                 struct in_addr ip = { route_info->gw };
209                 printf("gw:%s ", inet_ntoa(ip));
210         }
211
212         if (route_info->flags & IPT_ROUTE_CONTINUE)
213                 printf("continue");
214
215         if (route_info->flags & IPT_ROUTE_TEE)
216                 printf("tee");
217
218 }
219
220
221 static void save(const struct ipt_ip *ip, 
222                  const struct ipt_entry_target *target)
223 {
224         const struct ipt_route_target_info *route_info
225                 = (const struct ipt_route_target_info *)target->data;
226
227         if (route_info->oif[0])
228                 printf("--oif %s ", route_info->oif);
229
230         if (route_info->iif[0])
231                 printf("--iif %s ", route_info->iif);
232
233         if (route_info->gw) {
234                 struct in_addr ip = { route_info->gw };
235                 printf("--gw %s ", inet_ntoa(ip));
236         }
237
238         if (route_info->flags & IPT_ROUTE_CONTINUE)
239                 printf("--continue ");
240
241         if (route_info->flags & IPT_ROUTE_TEE)
242                 printf("--tee ");
243 }
244
245
246 static struct iptables_target route = { 
247         .next           = NULL,
248         .name           = "ROUTE",
249         .version        = IPTABLES_VERSION,
250         .size           = IPT_ALIGN(sizeof(struct ipt_route_target_info)),
251         .userspacesize  = IPT_ALIGN(sizeof(struct ipt_route_target_info)),
252         .help           = &help,
253         .init           = &init,
254         .parse          = &parse,
255         .final_check    = &final_check,
256         .print          = &print,
257         .save           = &save,
258         .extra_opts     = opts
259 };
260
261 void _init(void)
262 {
263         register_target(&route);
264 }