/* Shared library add-on to ip6tables to add Hop-by-Hop and Dst headers support. */ #include #include #include #include #include #include #include /*#include */ #include #include #include #include #define DEBUG 0 /* Function which prints out usage message. */ static void hbh_help(void) { printf( "hbh v%s options:\n" " --hbh-len [!] length total length of this header\n" " --hbh-opts TYPE[:LEN][,TYPE[:LEN]...] \n" " Options and its length (list, max: %d)\n", IPTABLES_VERSION, IP6T_OPTS_OPTSNR); } static const struct option hbh_opts[] = { { "hbh-len", 1, NULL, '1' }, { "hbh-opts", 1, NULL, '2' }, { "hbh-not-strict", 1, NULL, '3' }, { } }; static u_int32_t parse_opts_num(const char *idstr, const char *typestr) { unsigned long int id; char* ep; id = strtoul(idstr,&ep,0) ; if ( idstr == ep ) { exit_error(PARAMETER_PROBLEM, "hbh: no valid digits in %s `%s'", typestr, idstr); } if ( id == ULONG_MAX && errno == ERANGE ) { exit_error(PARAMETER_PROBLEM, "%s `%s' specified too big: would overflow", typestr, idstr); } if ( *idstr != '\0' && *ep != '\0' ) { exit_error(PARAMETER_PROBLEM, "hbh: error parsing %s `%s'", typestr, idstr); } return (u_int32_t) id; } static int parse_options(const char *optsstr, u_int16_t *opts) { char *buffer, *cp, *next, *range; unsigned int i; buffer = strdup(optsstr); if (!buffer) exit_error(OTHER_PROBLEM, "strdup failed"); for (cp=buffer, i=0; cp && idata; optinfo->hdrlen = 0; optinfo->flags = 0; optinfo->invflags = 0; optinfo->optsnr = 0; } /* Function which parses command options; returns true if it ate an option */ static int hbh_parse(int c, char **argv, int invert, unsigned int *flags, const void *entry, struct xt_entry_match **match) { struct ip6t_opts *optinfo = (struct ip6t_opts *)(*match)->data; switch (c) { case '1': if (*flags & IP6T_OPTS_LEN) exit_error(PARAMETER_PROBLEM, "Only one `--hbh-len' allowed"); check_inverse(optarg, &invert, &optind, 0); optinfo->hdrlen = parse_opts_num(argv[optind-1], "length"); if (invert) optinfo->invflags |= IP6T_OPTS_INV_LEN; optinfo->flags |= IP6T_OPTS_LEN; *flags |= IP6T_OPTS_LEN; break; case '2': if (*flags & IP6T_OPTS_OPTS) exit_error(PARAMETER_PROBLEM, "Only one `--hbh-opts' allowed"); check_inverse(optarg, &invert, &optind, 0); if (invert) exit_error(PARAMETER_PROBLEM, " '!' not allowed with `--hbh-opts'"); optinfo->optsnr = parse_options(argv[optind-1], optinfo->opts); optinfo->flags |= IP6T_OPTS_OPTS; *flags |= IP6T_OPTS_OPTS; break; case '3': if (*flags & IP6T_OPTS_NSTRICT) exit_error(PARAMETER_PROBLEM, "Only one `--hbh-not-strict' allowed"); if ( !(*flags & IP6T_OPTS_OPTS) ) exit_error(PARAMETER_PROBLEM, "`--hbh-opts ...' required before `--hbh-not-strict'"); optinfo->flags |= IP6T_OPTS_NSTRICT; *flags |= IP6T_OPTS_NSTRICT; break; default: return 0; } return 1; } static void print_options(int optsnr, u_int16_t *optsp) { unsigned int i; for(i=0; i>8); if ((optsp[i] & 0x00FF) != 0x00FF){ printf(":%d", (optsp[i] & 0x00FF)); } printf("%c", (i!=optsnr-1)?',':' '); } } /* Prints out the union ip6t_matchinfo. */ static void hbh_print(const void *ip, const struct xt_entry_match *match, int numeric) { const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data; printf("hbh "); if (optinfo->flags & IP6T_OPTS_LEN) { printf("length"); printf(":%s", optinfo->invflags & IP6T_OPTS_INV_LEN ? "!" : ""); printf("%u", optinfo->hdrlen); printf(" "); } if (optinfo->flags & IP6T_OPTS_OPTS) printf("opts "); print_options(optinfo->optsnr, (u_int16_t *)optinfo->opts); if (optinfo->flags & IP6T_OPTS_NSTRICT) printf("not-strict "); if (optinfo->invflags & ~IP6T_OPTS_INV_MASK) printf("Unknown invflags: 0x%X ", optinfo->invflags & ~IP6T_OPTS_INV_MASK); } /* Saves the union ip6t_matchinfo in parsable form to stdout. */ static void hbh_save(const void *ip, const struct xt_entry_match *match) { const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data; if (optinfo->flags & IP6T_OPTS_LEN) { printf("--hbh-len %s%u ", (optinfo->invflags & IP6T_OPTS_INV_LEN) ? "! " : "", optinfo->hdrlen); } if (optinfo->flags & IP6T_OPTS_OPTS) printf("--hbh-opts "); print_options(optinfo->optsnr, (u_int16_t *)optinfo->opts); if (optinfo->flags & IP6T_OPTS_NSTRICT) printf("--hbh-not-strict "); } static struct ip6tables_match hbh_match6 = { .name = "hbh", .version = IPTABLES_VERSION, .size = IP6T_ALIGN(sizeof(struct ip6t_opts)), .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_opts)), .help = hbh_help, .init = hbh_init, .parse = hbh_parse, .print = hbh_print, .save = hbh_save, .extra_opts = hbh_opts, }; void _init(void) { register_match6(&hbh_match6); }