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