2 * m_ipt.c iptables based targets
3 * utilities mostly ripped from iptables <duh, its the linux way>
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.
10 * Authors: J Hadi Salim (hadi@cyberus.ca)
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
19 #include <linux/netfilter.h>
20 #include <linux/netfilter_ipv4/ip_tables.h>
23 #include <linux/tc_act/tc_ipt.h>
37 static const char *pname = "tc-ipt";
38 static const char *tname = "mangle";
39 static const char *pversion = "0.1";
41 static const char *ipthooks[] = {
49 static struct option original_opts[] = {
54 static struct iptables_target *t_list = NULL;
55 static struct option *opts = original_opts;
56 static unsigned int global_option_offset = 0;
57 #define OPTION_OFFSET 256
62 register_target(struct iptables_target *me)
64 /* fprintf(stderr, "\nDummy register_target %s \n", me->name);
72 xtables_register_target(struct iptables_target *me)
79 exit_tryhelp(int status)
81 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
87 exit_error(enum exittype status, char *msg, ...)
92 fprintf(stderr, "%s v%s: ", pname, pversion);
93 vfprintf(stderr, msg, args);
95 fprintf(stderr, "\n");
96 if (status == PARAMETER_PROBLEM)
98 if (status == VERSION_PROBLEM)
100 "Perhaps iptables or your kernel needs to be upgraded.\n");
104 /* stolen from iptables 1.2.11
105 They should really have them as a library so i can link to them
106 Email them next time i remember
110 addr_to_dotted(const struct in_addr *addrp)
113 const unsigned char *bytep;
115 bytep = (const unsigned char *) &(addrp->s_addr);
116 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
120 int string_to_number_ll(const char *s, unsigned long long min,
121 unsigned long long max,
122 unsigned long long *ret)
124 unsigned long long number;
127 /* Handle hex, octal, etc. */
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)) {
140 int string_to_number_l(const char *s, unsigned long min, unsigned long max,
144 unsigned long long number;
146 result = string_to_number_ll(s, min, max, &number);
147 *ret = (unsigned long)number;
152 int string_to_number(const char *s, unsigned int min, unsigned int max,
156 unsigned long number;
158 result = string_to_number_l(s, min, max, &number);
159 *ret = (unsigned int)number;
164 static void free_opts(struct option *local_opts)
166 if (local_opts != original_opts) {
168 opts = original_opts;
169 global_option_offset = 0;
173 static struct option *
174 merge_options(struct option *oldopts, const struct option *newopts,
175 unsigned int *option_offset)
177 struct option *merge;
178 unsigned int num_old, num_new, i;
180 for (num_old = 0; oldopts[num_old].name; num_old++) ;
181 for (num_new = 0; newopts[num_new].name; num_new++) ;
183 *option_offset = global_option_offset + OPTION_OFFSET;
185 merge = malloc(sizeof (struct option) * (num_new + num_old + 1));
186 memcpy(merge, oldopts, num_old * sizeof (struct option));
187 for (i = 0; i < num_new; i++) {
188 merge[num_old + i] = newopts[i];
189 merge[num_old + i].val += *option_offset;
191 memset(merge + num_old + num_new, 0, sizeof (struct option));
197 fw_calloc(size_t count, size_t size)
201 if ((p = (void *) calloc(count, size)) == NULL) {
202 perror("iptables: calloc failed");
208 static struct iptables_target *
211 struct iptables_target *m;
212 for (m = t_list; m; m = m->next) {
213 if (strcmp(m->name, name) == 0)
220 static struct iptables_target *
221 get_target_name(const char *name)
225 char *new_name, *lname;
226 struct iptables_target *m;
227 char path[strlen(lib_dir) + sizeof ("/libipt_.so") + strlen(name)];
229 #ifdef NO_SHARED_LIBS
233 new_name = malloc(strlen(name) + 1);
234 lname = malloc(strlen(name) + 1);
236 memset(new_name, '\0', strlen(name) + 1);
238 exit_error(PARAMETER_PROBLEM, "get_target_name");
241 memset(lname, '\0', strlen(name) + 1);
243 exit_error(PARAMETER_PROBLEM, "get_target_name");
245 strcpy(new_name, name);
248 if (isupper(lname[0])) {
250 for (i = 0; i < strlen(name); i++) {
251 lname[i] = tolower(lname[i]);
255 if (islower(new_name[0])) {
257 for (i = 0; i < strlen(new_name); i++) {
258 new_name[i] = toupper(new_name[i]);
262 /* try libxt_xx first */
263 sprintf(path, "%s/libxt_%s.so", lib_dir, new_name);
264 handle = dlopen(path, RTLD_LAZY);
266 /* try libipt_xx next */
267 sprintf(path, "%s/libipt_%s.so", lib_dir, new_name);
268 handle = dlopen(path, RTLD_LAZY);
271 sprintf(path, "%s/libxt_%s.so", lib_dir , lname);
272 handle = dlopen(path, RTLD_LAZY);
276 sprintf(path, "%s/libipt_%s.so", lib_dir , lname);
277 handle = dlopen(path, RTLD_LAZY);
279 /* ok, lets give up .. */
281 fputs(dlerror(), stderr);
289 m = dlsym(handle, new_name);
290 if ((error = dlerror()) != NULL) {
291 m = (struct iptables_target *) dlsym(handle, lname);
292 if ((error = dlerror()) != NULL) {
293 m = find_t(new_name);
297 fputs(error, stderr);
298 fprintf(stderr, "\n");
314 struct in_addr *dotted_to_addr(const char *dotted)
316 static struct in_addr addr;
317 unsigned char *addrp;
319 unsigned int onebyte;
323 /* copy dotted string, because we need to modify it */
324 strncpy(buf, dotted, sizeof (buf) - 1);
325 addrp = (unsigned char *) &(addr.s_addr);
328 for (i = 0; i < 3; i++) {
329 if ((q = strchr(p, '.')) == NULL)
330 return (struct in_addr *) NULL;
333 if (string_to_number(p, 0, 255, &onebyte) == -1)
334 return (struct in_addr *) NULL;
336 addrp[i] = (unsigned char) onebyte;
340 /* we've checked 3 bytes, now we check the last one */
341 if (string_to_number(p, 0, 255, &onebyte) == -1)
342 return (struct in_addr *) NULL;
344 addrp[3] = (unsigned char) onebyte;
349 static void set_revision(char *name, u_int8_t revision)
351 /* Old kernel sources don't have ".revision" field,
352 * but we stole a byte from name. */
353 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
354 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
358 * we may need to check for version mismatch
361 build_st(struct iptables_target *target, struct ipt_entry_target *t)
363 unsigned int nfcache = 0;
369 IPT_ALIGN(sizeof (struct ipt_entry_target)) + target->size;
372 target->t = fw_calloc(1, size);
373 target->t->u.target_size = size;
375 if (target->init != NULL)
376 target->init(target->t, &nfcache);
377 set_revision(target->t->u.user.name, target->revision);
381 strcpy(target->t->u.user.name, target->name);
388 static int parse_ipt(struct action_util *a,int *argc_p,
389 char ***argv_p, int tca_id, struct nlmsghdr *n)
391 struct iptables_target *m = NULL;
396 char **argv = *argv_p;
397 int argc = 0, iargc = 0;
401 __u32 hook = 0, index = 0;
403 lib_dir = getenv("IPTABLES_LIB_DIR");
405 lib_dir = IPT_LIB_DIR;
409 for (i = 0; i < rargc; i++) {
410 if (NULL == argv[i] || 0 == strcmp(argv[i], "action")) {
418 fprintf(stderr,"bad arguements to ipt %d vs %d \n", argc, rargc);
423 c = getopt_long(argc, argv, "j:", opts, NULL);
428 m = get_target_name(optarg);
431 if (0 > build_st(m, NULL)) {
432 printf(" %s error \n", m->name);
436 merge_options(opts, m->extra_opts,
439 fprintf(stderr," failed to find target %s\n\n", optarg);
446 memset(&fw, 0, sizeof (fw));
448 m->parse(c - m->option_offset, argv, 0,
449 &m->tflags, NULL, &m->t);
451 fprintf(stderr," failed to find target %s\n\n", optarg);
461 if (iargc > optind) {
462 if (matches(argv[optind], "index") == 0) {
463 if (get_u32(&index, argv[optind + 1], 10)) {
464 fprintf(stderr, "Illegal \"index\"\n");
475 fprintf(stderr," ipt Parser BAD!! (%s)\n", *argv);
479 /* check that we passed the correct parameters to the target */
481 m->final_check(m->tflags);
484 struct tcmsg *t = NLMSG_DATA(n);
485 if (t->tcm_parent != TC_H_ROOT
486 && t->tcm_parent == TC_H_MAJ(TC_H_INGRESS)) {
487 hook = NF_IP_PRE_ROUTING;
489 hook = NF_IP_POST_ROUTING;
493 tail = NLMSG_TAIL(n);
494 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
495 fprintf(stdout, "tablename: %s hook: %s\n ", tname, ipthooks[hook]);
496 fprintf(stdout, "\ttarget: ");
499 m->print(NULL, m->t, 0);
500 fprintf(stdout, " index %d\n", index);
502 if (strlen(tname) > 16) {
506 size = 1 + strlen(tname);
508 strncpy(k, tname, size);
510 addattr_l(n, MAX_MSG, TCA_IPT_TABLE, k, size);
511 addattr_l(n, MAX_MSG, TCA_IPT_HOOK, &hook, 4);
512 addattr_l(n, MAX_MSG, TCA_IPT_INDEX, &index, 4);
514 addattr_l(n, MAX_MSG, TCA_IPT_TARG, m->t, m->t->u.target_size);
515 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
519 *argc_p = rargc - iargc;
524 /* Clear flags if target will be used again */
527 /* Free allocated memory */
537 print_ipt(struct action_util *au,FILE * f, struct rtattr *arg)
539 struct rtattr *tb[TCA_IPT_MAX + 1];
540 struct ipt_entry_target *t = NULL;
545 lib_dir = getenv("IPTABLES_LIB_DIR");
547 lib_dir = IPT_LIB_DIR;
549 parse_rtattr_nested(tb, TCA_IPT_MAX, arg);
551 if (tb[TCA_IPT_TABLE] == NULL) {
552 fprintf(f, "[NULL ipt table name ] assuming mangle ");
554 fprintf(f, "tablename: %s ",
555 (char *) RTA_DATA(tb[TCA_IPT_TABLE]));
558 if (tb[TCA_IPT_HOOK] == NULL) {
559 fprintf(f, "[NULL ipt hook name ]\n ");
563 hook = *(__u32 *) RTA_DATA(tb[TCA_IPT_HOOK]);
564 fprintf(f, " hook: %s \n", ipthooks[hook]);
567 if (tb[TCA_IPT_TARG] == NULL) {
568 fprintf(f, "\t[NULL ipt target parameters ] \n");
571 struct iptables_target *m = NULL;
572 t = RTA_DATA(tb[TCA_IPT_TARG]);
573 m = get_target_name(t->u.user.name);
575 if (0 > build_st(m, t)) {
576 fprintf(stderr, " %s error \n", m->name);
581 merge_options(opts, m->extra_opts,
584 fprintf(stderr, " failed to find target %s\n\n",
588 fprintf(f, "\ttarget ");
589 m->print(NULL, m->t, 0);
590 if (tb[TCA_IPT_INDEX] == NULL) {
591 fprintf(f, " [NULL ipt target index ]\n");
594 index = *(__u32 *) RTA_DATA(tb[TCA_IPT_INDEX]);
595 fprintf(f, " \n\tindex %d", index);
598 if (tb[TCA_IPT_CNT]) {
599 struct tc_cnt *c = RTA_DATA(tb[TCA_IPT_CNT]);;
600 fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt);
603 if (tb[TCA_IPT_TM]) {
604 struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]);
616 struct action_util ipt_action_util = {
618 .parse_aopt = parse_ipt,
619 .print_aopt = print_ipt,