m_ipt.c revision 4c7abb271b8fc44caa40388d02bdd0de196d0dac
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 13#include <syslog.h> 14#include <sys/socket.h> 15#include <netinet/in.h> 16#include <arpa/inet.h> 17#include <linux/if.h> 18#include <iptables.h> 19#include <linux/netfilter.h> 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> 33#include <limits.h> 34#include <unistd.h> 35#include <fcntl.h> 36#include <sys/wait.h> 37 38static const char *pname = "tc-ipt"; 39static const char *tname = "mangle"; 40static const char *pversion = "0.1"; 41 42static const char *ipthooks[] = { 43 "NF_IP_PRE_ROUTING", 44 "NF_IP_LOCAL_IN", 45 "NF_IP_FORWARD", 46 "NF_IP_LOCAL_OUT", 47 "NF_IP_POST_ROUTING", 48}; 49 50static struct option original_opts[] = { 51 {"jump", 1, 0, 'j'}, 52 {0, 0, 0, 0} 53}; 54 55static struct iptables_target *t_list = NULL; 56static struct option *opts = original_opts; 57static unsigned int global_option_offset = 0; 58#define OPTION_OFFSET 256 59 60char *lib_dir; 61 62void 63register_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 72void 73xtables_register_target(struct iptables_target *me) 74{ 75 me->next = t_list; 76 t_list = me; 77} 78 79void 80exit_tryhelp(int status) 81{ 82 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n", 83 pname, pname); 84 exit(status); 85} 86 87void 88exit_error(enum exittype status, char *msg, ...) 89{ 90 va_list args; 91 92 va_start(args, msg); 93 fprintf(stderr, "%s v%s: ", pname, pversion); 94 vfprintf(stderr, msg, args); 95 va_end(args); 96 fprintf(stderr, "\n"); 97 if (status == PARAMETER_PROBLEM) 98 exit_tryhelp(status); 99 if (status == VERSION_PROBLEM) 100 fprintf(stderr, 101 "Perhaps iptables or your kernel needs to be upgraded.\n"); 102 exit(status); 103} 104 105/* stolen from iptables 1.2.11 106They should really have them as a library so i can link to them 107Email them next time i remember 108*/ 109 110char * 111addr_to_dotted(const struct in_addr *addrp) 112{ 113 static char buf[20]; 114 const unsigned char *bytep; 115 116 bytep = (const unsigned char *) &(addrp->s_addr); 117 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]); 118 return buf; 119} 120 121int string_to_number_ll(const char *s, unsigned long long min, 122 unsigned long long max, 123 unsigned long long *ret) 124{ 125 unsigned long long number; 126 char *end; 127 128 /* Handle hex, octal, etc. */ 129 errno = 0; 130 number = strtoull(s, &end, 0); 131 if (*end == '\0' && end != s) { 132 /* we parsed a number, let's see if we want this */ 133 if (errno != ERANGE && min <= number && (!max || number <= max)) { 134 *ret = number; 135 return 0; 136 } 137 } 138 return -1; 139} 140 141int string_to_number_l(const char *s, unsigned long min, unsigned long max, 142 unsigned long *ret) 143{ 144 int result; 145 unsigned long long number; 146 147 result = string_to_number_ll(s, min, max, &number); 148 *ret = (unsigned long)number; 149 150 return result; 151} 152 153int string_to_number(const char *s, unsigned int min, unsigned int max, 154 unsigned int *ret) 155{ 156 int result; 157 unsigned long number; 158 159 result = string_to_number_l(s, min, max, &number); 160 *ret = (unsigned int)number; 161 162 return result; 163} 164 165static void free_opts(struct option *opts) 166{ 167 if (opts != original_opts) { 168 free(opts); 169 opts = original_opts; 170 global_option_offset = 0; 171 } 172} 173 174static struct option * 175merge_options(struct option *oldopts, const struct option *newopts, 176 unsigned int *option_offset) 177{ 178 struct option *merge; 179 unsigned int num_old, num_new, i; 180 181 for (num_old = 0; oldopts[num_old].name; num_old++) ; 182 for (num_new = 0; newopts[num_new].name; num_new++) ; 183 184 *option_offset = global_option_offset + OPTION_OFFSET; 185 186 merge = malloc(sizeof (struct option) * (num_new + num_old + 1)); 187 memcpy(merge, oldopts, num_old * sizeof (struct option)); 188 for (i = 0; i < num_new; i++) { 189 merge[num_old + i] = newopts[i]; 190 merge[num_old + i].val += *option_offset; 191 } 192 memset(merge + num_old + num_new, 0, sizeof (struct option)); 193 194 return merge; 195} 196 197static void * 198fw_calloc(size_t count, size_t size) 199{ 200 void *p; 201 202 if ((p = (void *) calloc(count, size)) == NULL) { 203 perror("iptables: calloc failed"); 204 exit(1); 205 } 206 return p; 207} 208 209static struct iptables_target * 210find_t(char *name) 211{ 212 struct iptables_target *m; 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 * 222get_target_name(const char *name) 223{ 224 void *handle; 225 char *error; 226 char *new_name, *lname; 227 struct iptables_target *m; 228 char path[strlen(lib_dir) + sizeof ("/libipt_.so") + strlen(name)]; 229 230 new_name = malloc(strlen(name) + 1); 231 lname = malloc(strlen(name) + 1); 232 if (new_name) 233 memset(new_name, '\0', strlen(name) + 1); 234 else 235 exit_error(PARAMETER_PROBLEM, "get_target_name"); 236 237 if (lname) 238 memset(lname, '\0', strlen(name) + 1); 239 else 240 exit_error(PARAMETER_PROBLEM, "get_target_name"); 241 242 strcpy(new_name, name); 243 strcpy(lname, name); 244 245 if (isupper(lname[0])) { 246 int i; 247 for (i = 0; i < strlen(name); i++) { 248 lname[i] = tolower(lname[i]); 249 } 250 } 251 252 if (islower(new_name[0])) { 253 int i; 254 for (i = 0; i < strlen(new_name); i++) { 255 new_name[i] = toupper(new_name[i]); 256 } 257 } 258 259 /* try libxt_xx first */ 260 sprintf(path, "%s/libxt_%s.so", lib_dir, new_name); 261 handle = dlopen(path, RTLD_LAZY); 262 if (!handle) { 263 /* try libipt_xx next */ 264 sprintf(path, "%s/libipt_%s.so", lib_dir, new_name); 265 handle = dlopen(path, RTLD_LAZY); 266 267 if (!handle) { 268 sprintf(path, "%s/libxt_%s.so", lib_dir , lname); 269 handle = dlopen(path, RTLD_LAZY); 270 } 271 272 if (!handle) { 273 sprintf(path, "%s/libipt_%s.so", lib_dir , lname); 274 handle = dlopen(path, RTLD_LAZY); 275 } 276 /* ok, lets give up .. */ 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 305struct 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 340static 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*/ 351int 352build_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 379static 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 int argc = 0, iargc = 0; 389 char k[16]; 390 int res = -1; 391 int size = 0; 392 int iok = 0, ok = 0; 393 __u32 hook = 0, index = 0; 394 res = 0; 395 396 lib_dir = getenv("IPTABLES_LIB_DIR"); 397 if (!lib_dir) 398 lib_dir = IPT_LIB_DIR; 399 400 { 401 int i; 402 for (i = 0; i < rargc; i++) { 403 if (NULL == argv[i] || 0 == strcmp(argv[i], "action")) { 404 break; 405 } 406 } 407 iargc = argc = i; 408 } 409 410 if (argc <= 2) { 411 fprintf(stderr,"bad arguements to ipt %d vs %d \n", argc, rargc); 412 return -1; 413 } 414 415 while (1) { 416 c = getopt_long(argc, argv, "j:", opts, NULL); 417 if (c == -1) 418 break; 419 switch (c) { 420 case 'j': 421 m = get_target_name(optarg); 422 if (NULL != m) { 423 424 if (0 > build_st(m, NULL)) { 425 printf(" %s error \n", m->name); 426 return -1; 427 } 428 opts = 429 merge_options(opts, m->extra_opts, 430 &m->option_offset); 431 } else { 432 fprintf(stderr," failed to find target %s\n\n", optarg); 433 return -1; 434 } 435 ok++; 436 break; 437 438 default: 439 memset(&fw, 0, sizeof (fw)); 440 if (m) { 441 m->parse(c - m->option_offset, argv, 0, 442 &m->tflags, NULL, &m->t); 443 } else { 444 fprintf(stderr," failed to find target %s\n\n", optarg); 445 return -1; 446 447 } 448 ok++; 449 break; 450 451 } 452 } 453 454 if (iargc > optind) { 455 if (matches(argv[optind], "index") == 0) { 456 if (get_u32(&index, argv[optind + 1], 10)) { 457 fprintf(stderr, "Illegal \"index\"\n"); 458 free_opts(opts); 459 return -1; 460 } 461 iok++; 462 463 optind += 2; 464 } 465 } 466 467 if (!ok && !iok) { 468 fprintf(stderr," ipt Parser BAD!! (%s)\n", *argv); 469 return -1; 470 } 471 472 /* check that we passed the correct parameters to the target */ 473 if (m) 474 m->final_check(m->tflags); 475 476 { 477 struct tcmsg *t = NLMSG_DATA(n); 478 if (t->tcm_parent != TC_H_ROOT 479 && t->tcm_parent == TC_H_MAJ(TC_H_INGRESS)) { 480 hook = NF_IP_PRE_ROUTING; 481 } else { 482 hook = NF_IP_POST_ROUTING; 483 } 484 } 485 486 tail = NLMSG_TAIL(n); 487 addattr_l(n, MAX_MSG, tca_id, NULL, 0); 488 fprintf(stdout, "tablename: %s hook: %s\n ", tname, ipthooks[hook]); 489 fprintf(stdout, "\ttarget: "); 490 491 if (m) 492 m->print(NULL, m->t, 0); 493 fprintf(stdout, " index %d\n", index); 494 495 if (strlen(tname) > 16) { 496 size = 16; 497 k[15] = 0; 498 } else { 499 size = 1 + strlen(tname); 500 } 501 strncpy(k, tname, size); 502 503 addattr_l(n, MAX_MSG, TCA_IPT_TABLE, k, size); 504 addattr_l(n, MAX_MSG, TCA_IPT_HOOK, &hook, 4); 505 addattr_l(n, MAX_MSG, TCA_IPT_INDEX, &index, 4); 506 if (m) 507 addattr_l(n, MAX_MSG, TCA_IPT_TARG, m->t, m->t->u.target_size); 508 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; 509 510 argc -= optind; 511 argv += optind; 512 *argc_p = rargc - iargc; 513 *argv_p = argv; 514 515 optind = 1; 516 free_opts(opts); 517 518 return 0; 519 520} 521 522static int 523print_ipt(struct action_util *au,FILE * f, struct rtattr *arg) 524{ 525 struct rtattr *tb[TCA_IPT_MAX + 1]; 526 struct ipt_entry_target *t = NULL; 527 528 if (arg == NULL) 529 return -1; 530 531 lib_dir = getenv("IPTABLES_LIB_DIR"); 532 if (!lib_dir) 533 lib_dir = IPT_LIB_DIR; 534 535 parse_rtattr_nested(tb, TCA_IPT_MAX, arg); 536 537 if (tb[TCA_IPT_TABLE] == NULL) { 538 fprintf(f, "[NULL ipt table name ] assuming mangle "); 539 } else { 540 fprintf(f, "tablename: %s ", 541 (char *) RTA_DATA(tb[TCA_IPT_TABLE])); 542 } 543 544 if (tb[TCA_IPT_HOOK] == NULL) { 545 fprintf(f, "[NULL ipt hook name ]\n "); 546 return -1; 547 } else { 548 __u32 hook; 549 hook = *(__u32 *) RTA_DATA(tb[TCA_IPT_HOOK]); 550 fprintf(f, " hook: %s \n", ipthooks[hook]); 551 } 552 553 if (tb[TCA_IPT_TARG] == NULL) { 554 fprintf(f, "\t[NULL ipt target parameters ] \n"); 555 return -1; 556 } else { 557 struct iptables_target *m = NULL; 558 t = RTA_DATA(tb[TCA_IPT_TARG]); 559 m = get_target_name(t->u.user.name); 560 if (NULL != m) { 561 if (0 > build_st(m, t)) { 562 fprintf(stderr, " %s error \n", m->name); 563 return -1; 564 } 565 566 opts = 567 merge_options(opts, m->extra_opts, 568 &m->option_offset); 569 } else { 570 fprintf(stderr, " failed to find target %s\n\n", 571 t->u.user.name); 572 return -1; 573 } 574 fprintf(f, "\ttarget "); 575 m->print(NULL, m->t, 0); 576 if (tb[TCA_IPT_INDEX] == NULL) { 577 fprintf(f, " [NULL ipt target index ]\n"); 578 } else { 579 __u32 index; 580 index = *(__u32 *) RTA_DATA(tb[TCA_IPT_INDEX]); 581 fprintf(f, " \n\tindex %d", index); 582 } 583 584 if (tb[TCA_IPT_CNT]) { 585 struct tc_cnt *c = RTA_DATA(tb[TCA_IPT_CNT]);; 586 fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt); 587 } 588 if (show_stats) { 589 if (tb[TCA_IPT_TM]) { 590 struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]); 591 print_tm(f,tm); 592 } 593 } 594 fprintf(f, " \n"); 595 596 } 597 free_opts(opts); 598 599 return 0; 600} 601 602struct action_util ipt_action_util = { 603 .id = "ipt", 604 .parse_aopt = parse_ipt, 605 .print_aopt = print_ipt, 606}; 607 608