]>
Commit | Line | Data |
---|---|---|
1ffd7fd2 | 1 | /* |
ae665a52 | 2 | * m_ipt.c iptables based targets |
3d0b7439 | 3 | * utilities mostly ripped from iptables <duh, its the linux way> |
1ffd7fd2 | 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 | * | |
ae665a52 | 10 | * Authors: J Hadi Salim (hadi@cyberus.ca) |
de539ecf | 11 | */ |
1ffd7fd2 | 12 | |
1ffd7fd2 | 13 | #include <sys/socket.h> |
14 | #include <netinet/in.h> | |
15 | #include <arpa/inet.h> | |
16 | #include <iptables.h> | |
ece02ea0 | 17 | #include <linux/netfilter.h> |
1ffd7fd2 | 18 | #include <linux/netfilter_ipv4/ip_tables.h> |
19 | #include "utils.h" | |
20 | #include "tc_util.h" | |
21 | #include <linux/tc_act/tc_ipt.h> | |
22 | #include <stdio.h> | |
23 | #include <dlfcn.h> | |
24 | #include <getopt.h> | |
25 | #include <errno.h> | |
26 | #include <string.h> | |
27 | #include <netdb.h> | |
28 | #include <stdlib.h> | |
29 | #include <ctype.h> | |
30 | #include <stdarg.h> | |
1ffd7fd2 | 31 | #include <unistd.h> |
32 | #include <fcntl.h> | |
33 | #include <sys/wait.h> | |
34 | ||
95dd5950 MF |
35 | static const char *pname = "tc-ipt"; |
36 | static const char *tname = "mangle"; | |
37 | static const char *pversion = "0.1"; | |
1ffd7fd2 | 38 | |
39 | static const char *ipthooks[] = { | |
40 | "NF_IP_PRE_ROUTING", | |
41 | "NF_IP_LOCAL_IN", | |
42 | "NF_IP_FORWARD", | |
43 | "NF_IP_LOCAL_OUT", | |
44 | "NF_IP_POST_ROUTING", | |
45 | }; | |
46 | ||
47 | static struct option original_opts[] = { | |
48 | {"jump", 1, 0, 'j'}, | |
49 | {0, 0, 0, 0} | |
50 | }; | |
51 | ||
9b32f896 | 52 | static struct xtables_target *t_list; |
6d4662d4 | 53 | static struct option *opts = original_opts; |
32a121cb | 54 | static unsigned int global_option_offset; |
1ffd7fd2 | 55 | #define OPTION_OFFSET 256 |
56 | ||
de539ecf | 57 | char *lib_dir; |
1ffd7fd2 | 58 | |
59 | void | |
9b32f896 | 60 | xtables_register_target(struct xtables_target *me) |
1ffd7fd2 | 61 | { |
1ffd7fd2 | 62 | me->next = t_list; |
63 | t_list = me; | |
64 | ||
65 | } | |
1ffd7fd2 | 66 | |
9b32f896 | 67 | static void exit_tryhelp(int status) |
1ffd7fd2 | 68 | { |
69 | fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n", | |
70 | pname, pname); | |
71 | exit(status); | |
72 | } | |
73 | ||
9b32f896 | 74 | static void exit_error(enum xtables_exittype status, char *msg, ...) |
1ffd7fd2 | 75 | { |
76 | va_list args; | |
77 | ||
78 | va_start(args, msg); | |
79 | fprintf(stderr, "%s v%s: ", pname, pversion); | |
80 | vfprintf(stderr, msg, args); | |
81 | va_end(args); | |
82 | fprintf(stderr, "\n"); | |
83 | if (status == PARAMETER_PROBLEM) | |
84 | exit_tryhelp(status); | |
85 | if (status == VERSION_PROBLEM) | |
86 | fprintf(stderr, | |
87 | "Perhaps iptables or your kernel needs to be upgraded.\n"); | |
88 | exit(status); | |
89 | } | |
90 | ||
91 | /* stolen from iptables 1.2.11 | |
92 | They should really have them as a library so i can link to them | |
93 | Email them next time i remember | |
94 | */ | |
95 | ||
a589dcda | 96 | static void free_opts(struct option *local_opts) |
1ffd7fd2 | 97 | { |
a589dcda DF |
98 | if (local_opts != original_opts) { |
99 | free(local_opts); | |
6d4662d4 SH |
100 | opts = original_opts; |
101 | global_option_offset = 0; | |
102 | } | |
1ffd7fd2 | 103 | } |
104 | ||
105 | static struct option * | |
106 | merge_options(struct option *oldopts, const struct option *newopts, | |
107 | unsigned int *option_offset) | |
108 | { | |
109 | struct option *merge; | |
110 | unsigned int num_old, num_new, i; | |
111 | ||
32a121cb SH |
112 | for (num_old = 0; oldopts[num_old].name; num_old++); |
113 | for (num_new = 0; newopts[num_new].name; num_new++); | |
1ffd7fd2 | 114 | |
115 | *option_offset = global_option_offset + OPTION_OFFSET; | |
116 | ||
32a121cb SH |
117 | merge = malloc(sizeof(struct option) * (num_new + num_old + 1)); |
118 | memcpy(merge, oldopts, num_old * sizeof(struct option)); | |
1ffd7fd2 | 119 | for (i = 0; i < num_new; i++) { |
120 | merge[num_old + i] = newopts[i]; | |
121 | merge[num_old + i].val += *option_offset; | |
122 | } | |
32a121cb | 123 | memset(merge + num_old + num_new, 0, sizeof(struct option)); |
1ffd7fd2 | 124 | |
125 | return merge; | |
126 | } | |
127 | ||
128 | static void * | |
129 | fw_calloc(size_t count, size_t size) | |
130 | { | |
131 | void *p; | |
132 | ||
133 | if ((p = (void *) calloc(count, size)) == NULL) { | |
134 | perror("iptables: calloc failed"); | |
135 | exit(1); | |
136 | } | |
137 | return p; | |
138 | } | |
139 | ||
9b32f896 | 140 | static struct xtables_target * |
1ffd7fd2 | 141 | find_t(char *name) |
142 | { | |
9b32f896 | 143 | struct xtables_target *m; |
32a121cb | 144 | |
1ffd7fd2 | 145 | for (m = t_list; m; m = m->next) { |
146 | if (strcmp(m->name, name) == 0) | |
147 | return m; | |
148 | } | |
149 | ||
150 | return NULL; | |
151 | } | |
152 | ||
9b32f896 | 153 | static struct xtables_target * |
de539ecf | 154 | get_target_name(const char *name) |
1ffd7fd2 | 155 | { |
156 | void *handle; | |
157 | char *error; | |
158 | char *new_name, *lname; | |
9b32f896 | 159 | struct xtables_target *m; |
32a121cb | 160 | char path[strlen(lib_dir) + sizeof("/libipt_.so") + strlen(name)]; |
1ffd7fd2 | 161 | |
f2e27cfb MF |
162 | #ifdef NO_SHARED_LIBS |
163 | return NULL; | |
164 | #endif | |
165 | ||
f89bb021 PS |
166 | new_name = calloc(1, strlen(name) + 1); |
167 | lname = calloc(1, strlen(name) + 1); | |
168 | if (!new_name) | |
1ffd7fd2 | 169 | exit_error(PARAMETER_PROBLEM, "get_target_name"); |
f89bb021 | 170 | if (!lname) |
1ffd7fd2 | 171 | exit_error(PARAMETER_PROBLEM, "get_target_name"); |
172 | ||
173 | strcpy(new_name, name); | |
174 | strcpy(lname, name); | |
175 | ||
176 | if (isupper(lname[0])) { | |
177 | int i; | |
32a121cb | 178 | |
1ffd7fd2 | 179 | for (i = 0; i < strlen(name); i++) { |
180 | lname[i] = tolower(lname[i]); | |
181 | } | |
182 | } | |
183 | ||
184 | if (islower(new_name[0])) { | |
185 | int i; | |
32a121cb | 186 | |
1ffd7fd2 | 187 | for (i = 0; i < strlen(new_name); i++) { |
188 | new_name[i] = toupper(new_name[i]); | |
189 | } | |
190 | } | |
191 | ||
53c01788 DF |
192 | /* try libxt_xx first */ |
193 | sprintf(path, "%s/libxt_%s.so", lib_dir, new_name); | |
1ffd7fd2 | 194 | handle = dlopen(path, RTLD_LAZY); |
195 | if (!handle) { | |
53c01788 DF |
196 | /* try libipt_xx next */ |
197 | sprintf(path, "%s/libipt_%s.so", lib_dir, new_name); | |
1ffd7fd2 | 198 | handle = dlopen(path, RTLD_LAZY); |
53c01788 DF |
199 | |
200 | if (!handle) { | |
32a121cb | 201 | sprintf(path, "%s/libxt_%s.so", lib_dir, lname); |
53c01788 DF |
202 | handle = dlopen(path, RTLD_LAZY); |
203 | } | |
204 | ||
205 | if (!handle) { | |
32a121cb | 206 | sprintf(path, "%s/libipt_%s.so", lib_dir, lname); |
53c01788 DF |
207 | handle = dlopen(path, RTLD_LAZY); |
208 | } | |
209 | /* ok, lets give up .. */ | |
1ffd7fd2 | 210 | if (!handle) { |
211 | fputs(dlerror(), stderr); | |
212 | printf("\n"); | |
6e34e7dc | 213 | free(new_name); |
1a6543c5 | 214 | free(lname); |
1ffd7fd2 | 215 | return NULL; |
216 | } | |
217 | } | |
218 | ||
219 | m = dlsym(handle, new_name); | |
220 | if ((error = dlerror()) != NULL) { | |
9b32f896 | 221 | m = (struct xtables_target *) dlsym(handle, lname); |
1ffd7fd2 | 222 | if ((error = dlerror()) != NULL) { |
223 | m = find_t(new_name); | |
32a121cb | 224 | if (m == NULL) { |
1ffd7fd2 | 225 | m = find_t(lname); |
32a121cb | 226 | if (m == NULL) { |
1ffd7fd2 | 227 | fputs(error, stderr); |
228 | fprintf(stderr, "\n"); | |
229 | dlclose(handle); | |
6e34e7dc | 230 | free(new_name); |
1a6543c5 | 231 | free(lname); |
1ffd7fd2 | 232 | return NULL; |
233 | } | |
234 | } | |
235 | } | |
236 | } | |
237 | ||
6e34e7dc | 238 | free(new_name); |
1a6543c5 | 239 | free(lname); |
1ffd7fd2 | 240 | return m; |
241 | } | |
242 | ||
894b1c66 | 243 | static void set_revision(char *name, u_int8_t revision) |
244 | { | |
245 | /* Old kernel sources don't have ".revision" field, | |
246 | * but we stole a byte from name. */ | |
247 | name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0'; | |
248 | name[IPT_FUNCTION_MAXNAMELEN - 1] = revision; | |
249 | } | |
250 | ||
ae665a52 | 251 | /* |
894b1c66 | 252 | * we may need to check for version mismatch |
253 | */ | |
9b32f896 | 254 | static int build_st(struct xtables_target *target, struct ipt_entry_target *t) |
1ffd7fd2 | 255 | { |
1ffd7fd2 | 256 | if (target) { |
257 | size_t size; | |
258 | ||
259 | size = | |
9b32f896 | 260 | XT_ALIGN(sizeof(struct ipt_entry_target)) + target->size; |
1ffd7fd2 | 261 | |
32a121cb | 262 | if (t == NULL) { |
1ffd7fd2 | 263 | target->t = fw_calloc(1, size); |
1ffd7fd2 | 264 | target->t->u.target_size = size; |
894b1c66 | 265 | |
266 | if (target->init != NULL) | |
9b32f896 | 267 | target->init(target->t); |
894b1c66 | 268 | set_revision(target->t->u.user.name, target->revision); |
1ffd7fd2 | 269 | } else { |
270 | target->t = t; | |
271 | } | |
272 | strcpy(target->t->u.user.name, target->name); | |
273 | return 0; | |
274 | } | |
275 | ||
276 | return -1; | |
277 | } | |
278 | ||
32a121cb | 279 | static int parse_ipt(struct action_util *a, int *argc_p, |
1ffd7fd2 | 280 | char ***argv_p, int tca_id, struct nlmsghdr *n) |
281 | { | |
9b32f896 | 282 | struct xtables_target *m = NULL; |
1ffd7fd2 | 283 | struct ipt_entry fw; |
284 | struct rtattr *tail; | |
285 | int c; | |
286 | int rargc = *argc_p; | |
287 | char **argv = *argv_p; | |
1ffd7fd2 | 288 | int argc = 0, iargc = 0; |
b317557f | 289 | char k[FILTER_NAMESZ]; |
1ffd7fd2 | 290 | int size = 0; |
291 | int iok = 0, ok = 0; | |
292 | __u32 hook = 0, index = 0; | |
1ffd7fd2 | 293 | |
de539ecf SH |
294 | lib_dir = getenv("IPTABLES_LIB_DIR"); |
295 | if (!lib_dir) | |
296 | lib_dir = IPT_LIB_DIR; | |
297 | ||
1ffd7fd2 | 298 | { |
299 | int i; | |
32a121cb | 300 | |
1ffd7fd2 | 301 | for (i = 0; i < rargc; i++) { |
302 | if (NULL == argv[i] || 0 == strcmp(argv[i], "action")) { | |
303 | break; | |
304 | } | |
305 | } | |
306 | iargc = argc = i; | |
307 | } | |
308 | ||
309 | if (argc <= 2) { | |
32a121cb | 310 | fprintf(stderr, "bad arguments to ipt %d vs %d\n", argc, rargc); |
1ffd7fd2 | 311 | return -1; |
312 | } | |
313 | ||
1ffd7fd2 | 314 | while (1) { |
315 | c = getopt_long(argc, argv, "j:", opts, NULL); | |
316 | if (c == -1) | |
317 | break; | |
318 | switch (c) { | |
319 | case 'j': | |
320 | m = get_target_name(optarg); | |
32a121cb | 321 | if (m != NULL) { |
1ffd7fd2 | 322 | |
32a121cb SH |
323 | if (build_st(m, NULL) < 0) { |
324 | printf(" %s error\n", m->name); | |
1ffd7fd2 | 325 | return -1; |
326 | } | |
327 | opts = | |
328 | merge_options(opts, m->extra_opts, | |
329 | &m->option_offset); | |
330 | } else { | |
32a121cb | 331 | fprintf(stderr, " failed to find target %s\n\n", optarg); |
1ffd7fd2 | 332 | return -1; |
333 | } | |
334 | ok++; | |
335 | break; | |
336 | ||
337 | default: | |
32a121cb | 338 | memset(&fw, 0, sizeof(fw)); |
1ffd7fd2 | 339 | if (m) { |
1ffd7fd2 | 340 | m->parse(c - m->option_offset, argv, 0, |
6d4662d4 | 341 | &m->tflags, NULL, &m->t); |
1ffd7fd2 | 342 | } else { |
32a121cb | 343 | fprintf(stderr, " failed to find target %s\n\n", optarg); |
1ffd7fd2 | 344 | return -1; |
345 | ||
346 | } | |
347 | ok++; | |
1ffd7fd2 | 348 | break; |
349 | ||
350 | } | |
351 | } | |
352 | ||
353 | if (iargc > optind) { | |
354 | if (matches(argv[optind], "index") == 0) { | |
355 | if (get_u32(&index, argv[optind + 1], 10)) { | |
356 | fprintf(stderr, "Illegal \"index\"\n"); | |
6d4662d4 | 357 | free_opts(opts); |
1ffd7fd2 | 358 | return -1; |
359 | } | |
360 | iok++; | |
361 | ||
362 | optind += 2; | |
363 | } | |
364 | } | |
365 | ||
366 | if (!ok && !iok) { | |
32a121cb | 367 | fprintf(stderr, " ipt Parser BAD!! (%s)\n", *argv); |
1ffd7fd2 | 368 | return -1; |
369 | } | |
370 | ||
6d4662d4 SH |
371 | /* check that we passed the correct parameters to the target */ |
372 | if (m) | |
373 | m->final_check(m->tflags); | |
374 | ||
1ffd7fd2 | 375 | { |
376 | struct tcmsg *t = NLMSG_DATA(n); | |
32a121cb | 377 | |
1ffd7fd2 | 378 | if (t->tcm_parent != TC_H_ROOT |
379 | && t->tcm_parent == TC_H_MAJ(TC_H_INGRESS)) { | |
380 | hook = NF_IP_PRE_ROUTING; | |
381 | } else { | |
382 | hook = NF_IP_POST_ROUTING; | |
383 | } | |
384 | } | |
385 | ||
fc78a8e9 | 386 | tail = NLMSG_TAIL(n); |
1ffd7fd2 | 387 | addattr_l(n, MAX_MSG, tca_id, NULL, 0); |
388 | fprintf(stdout, "tablename: %s hook: %s\n ", tname, ipthooks[hook]); | |
389 | fprintf(stdout, "\ttarget: "); | |
390 | ||
391 | if (m) | |
392 | m->print(NULL, m->t, 0); | |
393 | fprintf(stdout, " index %d\n", index); | |
394 | ||
395 | if (strlen(tname) > 16) { | |
396 | size = 16; | |
397 | k[15] = 0; | |
398 | } else { | |
399 | size = 1 + strlen(tname); | |
400 | } | |
401 | strncpy(k, tname, size); | |
402 | ||
403 | addattr_l(n, MAX_MSG, TCA_IPT_TABLE, k, size); | |
404 | addattr_l(n, MAX_MSG, TCA_IPT_HOOK, &hook, 4); | |
405 | addattr_l(n, MAX_MSG, TCA_IPT_INDEX, &index, 4); | |
406 | if (m) | |
407 | addattr_l(n, MAX_MSG, TCA_IPT_TARG, m->t, m->t->u.target_size); | |
fc78a8e9 | 408 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
1ffd7fd2 | 409 | |
410 | argc -= optind; | |
411 | argv += optind; | |
412 | *argc_p = rargc - iargc; | |
413 | *argv_p = argv; | |
ae665a52 | 414 | |
6e34e7dc | 415 | optind = 0; |
6d4662d4 | 416 | free_opts(opts); |
6e34e7dc | 417 | /* Clear flags if target will be used again */ |
32a121cb SH |
418 | m->tflags = 0; |
419 | m->used = 0; | |
6e34e7dc | 420 | /* Free allocated memory */ |
32a121cb SH |
421 | if (m->t) |
422 | free(m->t); | |
6e34e7dc | 423 | |
1ffd7fd2 | 424 | |
425 | return 0; | |
426 | ||
427 | } | |
428 | ||
429 | static int | |
32a121cb | 430 | print_ipt(struct action_util *au, FILE * f, struct rtattr *arg) |
1ffd7fd2 | 431 | { |
432 | struct rtattr *tb[TCA_IPT_MAX + 1]; | |
433 | struct ipt_entry_target *t = NULL; | |
1ffd7fd2 | 434 | |
435 | if (arg == NULL) | |
436 | return -1; | |
437 | ||
c6ab5b82 PM |
438 | lib_dir = getenv("IPTABLES_LIB_DIR"); |
439 | if (!lib_dir) | |
440 | lib_dir = IPT_LIB_DIR; | |
441 | ||
78934000 | 442 | parse_rtattr_nested(tb, TCA_IPT_MAX, arg); |
1ffd7fd2 | 443 | |
444 | if (tb[TCA_IPT_TABLE] == NULL) { | |
445 | fprintf(f, "[NULL ipt table name ] assuming mangle "); | |
446 | } else { | |
447 | fprintf(f, "tablename: %s ", | |
ff24746c | 448 | rta_getattr_str(tb[TCA_IPT_TABLE])); |
1ffd7fd2 | 449 | } |
450 | ||
451 | if (tb[TCA_IPT_HOOK] == NULL) { | |
452 | fprintf(f, "[NULL ipt hook name ]\n "); | |
453 | return -1; | |
454 | } else { | |
455 | __u32 hook; | |
32a121cb | 456 | |
ff24746c | 457 | hook = rta_getattr_u32(tb[TCA_IPT_HOOK]); |
32a121cb | 458 | fprintf(f, " hook: %s\n", ipthooks[hook]); |
1ffd7fd2 | 459 | } |
460 | ||
461 | if (tb[TCA_IPT_TARG] == NULL) { | |
32a121cb | 462 | fprintf(f, "\t[NULL ipt target parameters ]\n"); |
1ffd7fd2 | 463 | return -1; |
464 | } else { | |
9b32f896 | 465 | struct xtables_target *m = NULL; |
32a121cb | 466 | |
1ffd7fd2 | 467 | t = RTA_DATA(tb[TCA_IPT_TARG]); |
468 | m = get_target_name(t->u.user.name); | |
32a121cb SH |
469 | if (m != NULL) { |
470 | if (build_st(m, t) < 0) { | |
471 | fprintf(stderr, " %s error\n", m->name); | |
1ffd7fd2 | 472 | return -1; |
473 | } | |
474 | ||
475 | opts = | |
476 | merge_options(opts, m->extra_opts, | |
477 | &m->option_offset); | |
478 | } else { | |
479 | fprintf(stderr, " failed to find target %s\n\n", | |
480 | t->u.user.name); | |
481 | return -1; | |
482 | } | |
483 | fprintf(f, "\ttarget "); | |
484 | m->print(NULL, m->t, 0); | |
485 | if (tb[TCA_IPT_INDEX] == NULL) { | |
486 | fprintf(f, " [NULL ipt target index ]\n"); | |
487 | } else { | |
488 | __u32 index; | |
32a121cb | 489 | |
ff24746c | 490 | index = rta_getattr_u32(tb[TCA_IPT_INDEX]); |
53075318 | 491 | fprintf(f, "\n\tindex %u", index); |
1ffd7fd2 | 492 | } |
493 | ||
494 | if (tb[TCA_IPT_CNT]) { | |
32a121cb SH |
495 | struct tc_cnt *c = RTA_DATA(tb[TCA_IPT_CNT]); |
496 | ||
1ffd7fd2 | 497 | fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt); |
498 | } | |
499 | if (show_stats) { | |
500 | if (tb[TCA_IPT_TM]) { | |
501 | struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]); | |
32a121cb SH |
502 | |
503 | print_tm(f, tm); | |
1ffd7fd2 | 504 | } |
ae665a52 | 505 | } |
32a121cb | 506 | fprintf(f, "\n"); |
1ffd7fd2 | 507 | |
508 | } | |
6d4662d4 | 509 | free_opts(opts); |
1ffd7fd2 | 510 | |
511 | return 0; | |
512 | } | |
513 | ||
514 | struct action_util ipt_action_util = { | |
32a121cb SH |
515 | .id = "ipt", |
516 | .parse_aopt = parse_ipt, | |
517 | .print_aopt = print_ipt, | |
1ffd7fd2 | 518 | }; |