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