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