]>
Commit | Line | Data |
---|---|---|
63c7d26f JHS |
1 | /* |
2 | * m_xt.c xtables based targets | |
3d0b7439 | 3 | * utilities mostly ripped from iptables <duh, its the linux way> |
63c7d26f JHS |
4 | * |
5 | * This program is free software; you can distribute it and/or | |
6 | * modify it under the terms of the GNU General Public License | |
7 | * as published by the Free Software Foundation; either version | |
8 | * 2 of the License, or (at your option) any later version. | |
9 | * | |
10 | * Authors: J Hadi Salim (hadi@cyberus.ca) | |
11 | */ | |
12 | ||
13 | /*XXX: in the future (xtables 1.4.3?) get rid of everything tagged | |
14 | * as TC_CONFIG_XT_H */ | |
15 | ||
63c7d26f JHS |
16 | #include <sys/socket.h> |
17 | #include <netinet/in.h> | |
18 | #include <arpa/inet.h> | |
19 | #include <net/if.h> | |
20 | #include <linux/netfilter.h> | |
21 | #include <linux/netfilter_ipv4/ip_tables.h> | |
22 | #include <xtables.h> | |
23 | #include "utils.h" | |
24 | #include "tc_util.h" | |
25 | #include <linux/tc_act/tc_ipt.h> | |
26 | #include <stdio.h> | |
63c7d26f JHS |
27 | #include <getopt.h> |
28 | #include <errno.h> | |
29 | #include <string.h> | |
30 | #include <netdb.h> | |
31 | #include <stdlib.h> | |
32 | #include <ctype.h> | |
33 | #include <stdarg.h> | |
34 | #include <limits.h> | |
35 | #include <unistd.h> | |
36 | #include <fcntl.h> | |
37 | #include <sys/wait.h> | |
38 | #ifdef TC_CONFIG_XT_H | |
39 | #include "xt-internal.h" | |
40 | #endif | |
41 | ||
edaaa11e | 42 | #ifndef ALIGN |
32a121cb SH |
43 | #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1) |
44 | #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask)) | |
edaaa11e SH |
45 | #endif |
46 | ||
63c7d26f JHS |
47 | static const char *pname = "tc-ipt"; |
48 | static const char *tname = "mangle"; | |
49 | static const char *pversion = "0.2"; | |
50 | ||
51 | static const char *ipthooks[] = { | |
52 | "NF_IP_PRE_ROUTING", | |
53 | "NF_IP_LOCAL_IN", | |
54 | "NF_IP_FORWARD", | |
55 | "NF_IP_LOCAL_OUT", | |
56 | "NF_IP_POST_ROUTING", | |
57 | }; | |
58 | ||
59 | static struct option original_opts[] = { | |
60 | {"jump", 1, 0, 'j'}, | |
61 | {0, 0, 0, 0} | |
62 | }; | |
63 | ||
64 | static struct option *opts = original_opts; | |
32a121cb | 65 | static unsigned int global_option_offset; |
63c7d26f JHS |
66 | char *lib_dir; |
67 | const char *program_version = XTABLES_VERSION; | |
68 | const char *program_name = "tc-ipt"; | |
69 | struct afinfo afinfo = { | |
70 | .family = AF_INET, | |
71 | .libprefix = "libxt_", | |
72 | .ipproto = IPPROTO_IP, | |
73 | .kmod = "ip_tables", | |
74 | .so_rev_target = IPT_SO_GET_REVISION_TARGET, | |
75 | }; | |
76 | ||
77 | ||
78 | #define OPTION_OFFSET 256 | |
79 | ||
80 | /*XXX: TC_CONFIG_XT_H */ | |
81 | static void free_opts(struct option *local_opts) | |
82 | { | |
83 | if (local_opts != original_opts) { | |
84 | free(local_opts); | |
85 | opts = original_opts; | |
86 | global_option_offset = 0; | |
87 | } | |
88 | } | |
89 | ||
90 | /*XXX: TC_CONFIG_XT_H */ | |
91 | static struct option * | |
92 | merge_options(struct option *oldopts, const struct option *newopts, | |
93 | unsigned int *option_offset) | |
94 | { | |
95 | struct option *merge; | |
96 | unsigned int num_old, num_new, i; | |
97 | ||
32a121cb SH |
98 | for (num_old = 0; oldopts[num_old].name; num_old++); |
99 | for (num_new = 0; newopts[num_new].name; num_new++); | |
63c7d26f JHS |
100 | |
101 | *option_offset = global_option_offset + OPTION_OFFSET; | |
102 | ||
32a121cb SH |
103 | merge = malloc(sizeof(struct option) * (num_new + num_old + 1)); |
104 | memcpy(merge, oldopts, num_old * sizeof(struct option)); | |
63c7d26f JHS |
105 | for (i = 0; i < num_new; i++) { |
106 | merge[num_old + i] = newopts[i]; | |
107 | merge[num_old + i].val += *option_offset; | |
108 | } | |
32a121cb | 109 | memset(merge + num_old + num_new, 0, sizeof(struct option)); |
63c7d26f JHS |
110 | |
111 | return merge; | |
112 | } | |
113 | ||
114 | ||
115 | /*XXX: TC_CONFIG_XT_H */ | |
116 | #ifndef TRUE | |
117 | #define TRUE 1 | |
118 | #endif | |
119 | #ifndef FALSE | |
120 | #define FALSE 0 | |
121 | #endif | |
122 | ||
123 | /*XXX: TC_CONFIG_XT_H */ | |
124 | int | |
125 | check_inverse(const char option[], int *invert, int *my_optind, int argc) | |
126 | { | |
32a121cb SH |
127 | if (option && strcmp(option, "!") == 0) { |
128 | if (*invert) | |
129 | exit_error(PARAMETER_PROBLEM, | |
130 | "Multiple `!' flags not allowed"); | |
131 | *invert = TRUE; | |
132 | if (my_optind != NULL) { | |
133 | ++*my_optind; | |
134 | if (argc && *my_optind > argc) | |
135 | exit_error(PARAMETER_PROBLEM, | |
136 | "no argument following `!'"); | |
137 | } | |
138 | ||
139 | return TRUE; | |
140 | } | |
141 | return FALSE; | |
63c7d26f JHS |
142 | } |
143 | ||
144 | /*XXX: TC_CONFIG_XT_H */ | |
145 | void exit_error(enum exittype status, const char *msg, ...) | |
146 | { | |
32a121cb SH |
147 | va_list args; |
148 | ||
149 | va_start(args, msg); | |
150 | fprintf(stderr, "%s v%s: ", pname, pversion); | |
151 | vfprintf(stderr, msg, args); | |
152 | va_end(args); | |
153 | fprintf(stderr, "\n"); | |
154 | /* On error paths, make sure that we don't leak memory */ | |
155 | exit(status); | |
63c7d26f JHS |
156 | } |
157 | ||
158 | /*XXX: TC_CONFIG_XT_H */ | |
159 | static void set_revision(char *name, u_int8_t revision) | |
160 | { | |
161 | /* Old kernel sources don't have ".revision" field, | |
162 | * but we stole a byte from name. */ | |
163 | name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0'; | |
164 | name[IPT_FUNCTION_MAXNAMELEN - 1] = revision; | |
165 | } | |
166 | ||
167 | /* | |
168 | * we may need to check for version mismatch | |
169 | */ | |
170 | int | |
171 | build_st(struct xtables_target *target, struct xt_entry_target *t) | |
172 | { | |
173 | ||
174 | size_t size = | |
32a121cb | 175 | XT_ALIGN(sizeof(struct xt_entry_target)) + target->size; |
63c7d26f | 176 | |
32a121cb | 177 | if (t == NULL) { |
63c7d26f JHS |
178 | target->t = fw_calloc(1, size); |
179 | target->t->u.target_size = size; | |
180 | strcpy(target->t->u.user.name, target->name); | |
181 | set_revision(target->t->u.user.name, target->revision); | |
182 | ||
183 | if (target->init != NULL) | |
184 | target->init(target->t); | |
185 | } else { | |
186 | target->t = t; | |
187 | } | |
188 | return 0; | |
189 | ||
190 | } | |
191 | ||
192 | inline void set_lib_dir(void) | |
193 | { | |
194 | ||
195 | lib_dir = getenv("XTABLES_LIBDIR"); | |
196 | if (!lib_dir) { | |
197 | lib_dir = getenv("IPTABLES_LIB_DIR"); | |
198 | if (lib_dir) | |
32a121cb | 199 | fprintf(stderr, "using deprecated IPTABLES_LIB_DIR\n"); |
63c7d26f JHS |
200 | } |
201 | if (lib_dir == NULL) | |
202 | lib_dir = XT_LIB_DIR; | |
203 | ||
204 | } | |
205 | ||
32a121cb | 206 | static int parse_ipt(struct action_util *a, int *argc_p, |
63c7d26f JHS |
207 | char ***argv_p, int tca_id, struct nlmsghdr *n) |
208 | { | |
209 | struct xtables_target *m = NULL; | |
210 | struct ipt_entry fw; | |
211 | struct rtattr *tail; | |
212 | int c; | |
213 | int rargc = *argc_p; | |
214 | char **argv = *argv_p; | |
215 | int argc = 0, iargc = 0; | |
b317557f | 216 | char k[FILTER_NAMESZ]; |
63c7d26f JHS |
217 | int size = 0; |
218 | int iok = 0, ok = 0; | |
219 | __u32 hook = 0, index = 0; | |
63c7d26f JHS |
220 | |
221 | set_lib_dir(); | |
222 | ||
223 | { | |
224 | int i; | |
32a121cb | 225 | |
63c7d26f JHS |
226 | for (i = 0; i < rargc; i++) { |
227 | if (NULL == argv[i] || 0 == strcmp(argv[i], "action")) { | |
228 | break; | |
229 | } | |
230 | } | |
231 | iargc = argc = i; | |
232 | } | |
233 | ||
234 | if (argc <= 2) { | |
32a121cb | 235 | fprintf(stderr, "bad arguments to ipt %d vs %d\n", argc, rargc); |
63c7d26f JHS |
236 | return -1; |
237 | } | |
238 | ||
239 | while (1) { | |
240 | c = getopt_long(argc, argv, "j:", opts, NULL); | |
241 | if (c == -1) | |
242 | break; | |
243 | switch (c) { | |
244 | case 'j': | |
245 | m = find_target(optarg, TRY_LOAD); | |
32a121cb | 246 | if (m != NULL) { |
63c7d26f | 247 | |
32a121cb SH |
248 | if (build_st(m, NULL) < 0) { |
249 | printf(" %s error\n", m->name); | |
63c7d26f JHS |
250 | return -1; |
251 | } | |
252 | opts = | |
253 | merge_options(opts, m->extra_opts, | |
254 | &m->option_offset); | |
255 | } else { | |
32a121cb | 256 | fprintf(stderr, " failed to find target %s\n\n", optarg); |
63c7d26f JHS |
257 | return -1; |
258 | } | |
259 | ok++; | |
260 | break; | |
261 | ||
262 | default: | |
32a121cb | 263 | memset(&fw, 0, sizeof(fw)); |
63c7d26f JHS |
264 | if (m) { |
265 | m->parse(c - m->option_offset, argv, 0, | |
266 | &m->tflags, NULL, &m->t); | |
267 | } else { | |
32a121cb | 268 | fprintf(stderr, " failed to find target %s\n\n", optarg); |
63c7d26f JHS |
269 | return -1; |
270 | ||
271 | } | |
272 | ok++; | |
273 | break; | |
274 | ||
275 | } | |
276 | } | |
277 | ||
278 | if (iargc > optind) { | |
279 | if (matches(argv[optind], "index") == 0) { | |
280 | if (get_u32(&index, argv[optind + 1], 10)) { | |
281 | fprintf(stderr, "Illegal \"index\"\n"); | |
282 | free_opts(opts); | |
283 | return -1; | |
284 | } | |
285 | iok++; | |
286 | ||
287 | optind += 2; | |
288 | } | |
289 | } | |
290 | ||
291 | if (!ok && !iok) { | |
32a121cb | 292 | fprintf(stderr, " ipt Parser BAD!! (%s)\n", *argv); |
63c7d26f JHS |
293 | return -1; |
294 | } | |
295 | ||
296 | /* check that we passed the correct parameters to the target */ | |
297 | if (m) | |
298 | m->final_check(m->tflags); | |
299 | ||
300 | { | |
301 | struct tcmsg *t = NLMSG_DATA(n); | |
32a121cb | 302 | |
63c7d26f JHS |
303 | if (t->tcm_parent != TC_H_ROOT |
304 | && t->tcm_parent == TC_H_MAJ(TC_H_INGRESS)) { | |
305 | hook = NF_IP_PRE_ROUTING; | |
306 | } else { | |
307 | hook = NF_IP_POST_ROUTING; | |
308 | } | |
309 | } | |
310 | ||
311 | tail = NLMSG_TAIL(n); | |
312 | addattr_l(n, MAX_MSG, tca_id, NULL, 0); | |
313 | fprintf(stdout, "tablename: %s hook: %s\n ", tname, ipthooks[hook]); | |
314 | fprintf(stdout, "\ttarget: "); | |
315 | ||
316 | if (m) | |
317 | m->print(NULL, m->t, 0); | |
318 | fprintf(stdout, " index %d\n", index); | |
319 | ||
320 | if (strlen(tname) > 16) { | |
321 | size = 16; | |
322 | k[15] = 0; | |
323 | } else { | |
324 | size = 1 + strlen(tname); | |
325 | } | |
326 | strncpy(k, tname, size); | |
327 | ||
328 | addattr_l(n, MAX_MSG, TCA_IPT_TABLE, k, size); | |
329 | addattr_l(n, MAX_MSG, TCA_IPT_HOOK, &hook, 4); | |
330 | addattr_l(n, MAX_MSG, TCA_IPT_INDEX, &index, 4); | |
331 | if (m) | |
332 | addattr_l(n, MAX_MSG, TCA_IPT_TARG, m->t, m->t->u.target_size); | |
333 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; | |
334 | ||
335 | argc -= optind; | |
336 | argv += optind; | |
337 | *argc_p = rargc - iargc; | |
338 | *argv_p = argv; | |
339 | ||
340 | optind = 0; | |
341 | free_opts(opts); | |
342 | /* Clear flags if target will be used again */ | |
32a121cb SH |
343 | m->tflags = 0; |
344 | m->used = 0; | |
63c7d26f | 345 | /* Free allocated memory */ |
32a121cb SH |
346 | if (m->t) |
347 | free(m->t); | |
63c7d26f JHS |
348 | |
349 | ||
350 | return 0; | |
351 | ||
352 | } | |
353 | ||
354 | static int | |
32a121cb | 355 | print_ipt(struct action_util *au, FILE * f, struct rtattr *arg) |
63c7d26f JHS |
356 | { |
357 | struct rtattr *tb[TCA_IPT_MAX + 1]; | |
358 | struct xt_entry_target *t = NULL; | |
359 | ||
360 | if (arg == NULL) | |
361 | return -1; | |
362 | ||
363 | set_lib_dir(); | |
364 | ||
365 | parse_rtattr_nested(tb, TCA_IPT_MAX, arg); | |
366 | ||
367 | if (tb[TCA_IPT_TABLE] == NULL) { | |
368 | fprintf(f, "[NULL ipt table name ] assuming mangle "); | |
369 | } else { | |
370 | fprintf(f, "tablename: %s ", | |
ff24746c | 371 | rta_getattr_str(tb[TCA_IPT_TABLE])); |
63c7d26f JHS |
372 | } |
373 | ||
374 | if (tb[TCA_IPT_HOOK] == NULL) { | |
375 | fprintf(f, "[NULL ipt hook name ]\n "); | |
376 | return -1; | |
377 | } else { | |
378 | __u32 hook; | |
32a121cb | 379 | |
ff24746c | 380 | hook = rta_getattr_u32(tb[TCA_IPT_HOOK]); |
32a121cb | 381 | fprintf(f, " hook: %s\n", ipthooks[hook]); |
63c7d26f JHS |
382 | } |
383 | ||
384 | if (tb[TCA_IPT_TARG] == NULL) { | |
32a121cb | 385 | fprintf(f, "\t[NULL ipt target parameters ]\n"); |
63c7d26f JHS |
386 | return -1; |
387 | } else { | |
388 | struct xtables_target *m = NULL; | |
32a121cb | 389 | |
63c7d26f JHS |
390 | t = RTA_DATA(tb[TCA_IPT_TARG]); |
391 | m = find_target(t->u.user.name, TRY_LOAD); | |
32a121cb SH |
392 | if (m != NULL) { |
393 | if (build_st(m, t) < 0) { | |
394 | fprintf(stderr, " %s error\n", m->name); | |
63c7d26f JHS |
395 | return -1; |
396 | } | |
397 | ||
398 | opts = | |
399 | merge_options(opts, m->extra_opts, | |
400 | &m->option_offset); | |
401 | } else { | |
402 | fprintf(stderr, " failed to find target %s\n\n", | |
403 | t->u.user.name); | |
404 | return -1; | |
405 | } | |
406 | fprintf(f, "\ttarget "); | |
407 | m->print(NULL, m->t, 0); | |
408 | if (tb[TCA_IPT_INDEX] == NULL) { | |
409 | fprintf(f, " [NULL ipt target index ]\n"); | |
410 | } else { | |
411 | __u32 index; | |
32a121cb | 412 | |
ff24746c | 413 | index = rta_getattr_u32(tb[TCA_IPT_INDEX]); |
53075318 | 414 | fprintf(f, "\n\tindex %u", index); |
63c7d26f JHS |
415 | } |
416 | ||
417 | if (tb[TCA_IPT_CNT]) { | |
32a121cb SH |
418 | struct tc_cnt *c = RTA_DATA(tb[TCA_IPT_CNT]); |
419 | ||
63c7d26f JHS |
420 | fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt); |
421 | } | |
422 | if (show_stats) { | |
423 | if (tb[TCA_IPT_TM]) { | |
424 | struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]); | |
32a121cb SH |
425 | |
426 | print_tm(f, tm); | |
63c7d26f JHS |
427 | } |
428 | } | |
32a121cb | 429 | fprintf(f, "\n"); |
63c7d26f JHS |
430 | |
431 | } | |
432 | free_opts(opts); | |
433 | ||
434 | return 0; | |
435 | } | |
436 | ||
437 | struct action_util ipt_action_util = { | |
32a121cb SH |
438 | .id = "ipt", |
439 | .parse_aopt = parse_ipt, | |
440 | .print_aopt = print_ipt, | |
63c7d26f | 441 | }; |