András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 2 | #include <string.h> |
| 3 | #include <stdlib.h> |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 4 | #include <errno.h> |
Jan Engelhardt | 5d9678a | 2008-11-20 10:15:35 +0100 | [diff] [blame] | 5 | #include <xtables.h> |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 6 | #include <linux/netfilter_ipv6/ip6t_opts.h> |
Jan Engelhardt | ddac6c5 | 2008-09-01 14:22:19 +0200 | [diff] [blame] | 7 | |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 8 | #define DEBUG 0 |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 9 | |
Jan Engelhardt | 7a969bb | 2011-03-03 00:40:43 +0100 | [diff] [blame] | 10 | enum { |
| 11 | O_HBH_LEN = 0, |
| 12 | O_HBH_OPTS, |
| 13 | }; |
| 14 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 15 | static void hbh_help(void) |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 16 | { |
| 17 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 18 | "hbh match options:\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 19 | "[!] --hbh-len length total length of this header\n" |
Jan Engelhardt | e2f588a | 2007-10-04 16:30:40 +0000 | [diff] [blame] | 20 | " --hbh-opts TYPE[:LEN][,TYPE[:LEN]...] \n" |
| 21 | " Options and its length (list, max: %d)\n", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 22 | IP6T_OPTS_OPTSNR); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Jan Engelhardt | 7a969bb | 2011-03-03 00:40:43 +0100 | [diff] [blame] | 25 | static const struct xt_option_entry hbh_opts[] = { |
| 26 | {.name = "hbh-len", .id = O_HBH_LEN, .type = XTTYPE_UINT32, |
| 27 | .flags = XTOPT_INVERT | XTOPT_PUT, |
| 28 | XTOPT_POINTER(struct ip6t_opts, hdrlen)}, |
| 29 | {.name = "hbh-opts", .id = O_HBH_OPTS, .type = XTTYPE_STRING}, |
| 30 | XTOPT_TABLEEND, |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 31 | }; |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 32 | |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 33 | static uint32_t |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 34 | parse_opts_num(const char *idstr, const char *typestr) |
| 35 | { |
| 36 | unsigned long int id; |
| 37 | char* ep; |
| 38 | |
| 39 | id = strtoul(idstr,&ep,0) ; |
| 40 | |
| 41 | if ( idstr == ep ) { |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 42 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | e2f588a | 2007-10-04 16:30:40 +0000 | [diff] [blame] | 43 | "hbh: no valid digits in %s `%s'", typestr, idstr); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 44 | } |
| 45 | if ( id == ULONG_MAX && errno == ERANGE ) { |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 46 | xtables_error(PARAMETER_PROBLEM, |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 47 | "%s `%s' specified too big: would overflow", |
| 48 | typestr, idstr); |
| 49 | } |
| 50 | if ( *idstr != '\0' && *ep != '\0' ) { |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 51 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | e2f588a | 2007-10-04 16:30:40 +0000 | [diff] [blame] | 52 | "hbh: error parsing %s `%s'", typestr, idstr); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 53 | } |
Jan Engelhardt | 213e185 | 2009-01-27 17:24:34 +0100 | [diff] [blame] | 54 | return id; |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static int |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 58 | parse_options(const char *optsstr, uint16_t *opts) |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 59 | { |
| 60 | char *buffer, *cp, *next, *range; |
| 61 | unsigned int i; |
| 62 | |
| 63 | buffer = strdup(optsstr); |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 64 | if (!buffer) xtables_error(OTHER_PROBLEM, "strdup failed"); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 65 | |
| 66 | for (cp=buffer, i=0; cp && i<IP6T_OPTS_OPTSNR; cp=next,i++) |
| 67 | { |
| 68 | next=strchr(cp, ','); |
| 69 | if (next) *next++='\0'; |
| 70 | range = strchr(cp, ':'); |
| 71 | if (range) { |
| 72 | if (i == IP6T_OPTS_OPTSNR-1) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 73 | xtables_error(PARAMETER_PROBLEM, |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 74 | "too many ports specified"); |
| 75 | *range++ = '\0'; |
| 76 | } |
Jan Engelhardt | 213e185 | 2009-01-27 17:24:34 +0100 | [diff] [blame] | 77 | opts[i] = (parse_opts_num(cp, "opt") & 0xFF) << 8; |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 78 | if (range) { |
| 79 | if (opts[i] == 0) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 80 | xtables_error(PARAMETER_PROBLEM, "PAD0 has not got length"); |
Jan Engelhardt | 213e185 | 2009-01-27 17:24:34 +0100 | [diff] [blame] | 81 | opts[i] |= parse_opts_num(range, "length") & 0xFF; |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 82 | } else { |
| 83 | opts[i] |= (0x00FF); |
| 84 | } |
| 85 | |
| 86 | #if DEBUG |
| 87 | printf("opts str: %s %s\n", cp, range); |
| 88 | printf("opts opt: %04X\n", opts[i]); |
| 89 | #endif |
| 90 | } |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 91 | if (cp) xtables_error(PARAMETER_PROBLEM, "too many addresses specified"); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 92 | |
| 93 | free(buffer); |
| 94 | |
| 95 | #if DEBUG |
| 96 | printf("addr nr: %d\n", i); |
| 97 | #endif |
| 98 | |
| 99 | return i; |
| 100 | } |
| 101 | |
Jan Engelhardt | 7a969bb | 2011-03-03 00:40:43 +0100 | [diff] [blame] | 102 | static void hbh_parse(struct xt_option_call *cb) |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 103 | { |
Jan Engelhardt | 7a969bb | 2011-03-03 00:40:43 +0100 | [diff] [blame] | 104 | struct ip6t_opts *optinfo = cb->data; |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 105 | |
Jan Engelhardt | 7a969bb | 2011-03-03 00:40:43 +0100 | [diff] [blame] | 106 | xtables_option_parse(cb); |
| 107 | switch (cb->entry->id) { |
| 108 | case O_HBH_LEN: |
| 109 | if (cb->invert) |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 110 | optinfo->invflags |= IP6T_OPTS_INV_LEN; |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 111 | break; |
Jan Engelhardt | 7a969bb | 2011-03-03 00:40:43 +0100 | [diff] [blame] | 112 | case O_HBH_OPTS: |
| 113 | optinfo->optsnr = parse_options(cb->arg, optinfo->opts); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 114 | optinfo->flags |= IP6T_OPTS_OPTS; |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 115 | break; |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 116 | } |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 117 | } |
| 118 | |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 119 | static void |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 120 | print_options(unsigned int optsnr, uint16_t *optsp) |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 121 | { |
| 122 | unsigned int i; |
| 123 | |
| 124 | for(i=0; i<optsnr; i++){ |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 125 | printf("%c", (i==0)?' ':','); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 126 | printf("%d", (optsp[i] & 0xFF00)>>8); |
| 127 | if ((optsp[i] & 0x00FF) != 0x00FF){ |
| 128 | printf(":%d", (optsp[i] & 0x00FF)); |
| 129 | } |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 133 | static void hbh_print(const void *ip, const struct xt_entry_match *match, |
| 134 | int numeric) |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 135 | { |
| 136 | const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data; |
| 137 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 138 | printf(" hbh"); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 139 | if (optinfo->flags & IP6T_OPTS_LEN) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 140 | printf(" length"); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 141 | printf(":%s", optinfo->invflags & IP6T_OPTS_INV_LEN ? "!" : ""); |
| 142 | printf("%u", optinfo->hdrlen); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 143 | } |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 144 | if (optinfo->flags & IP6T_OPTS_OPTS) printf(" opts"); |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 145 | print_options(optinfo->optsnr, (uint16_t *)optinfo->opts); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 146 | if (optinfo->invflags & ~IP6T_OPTS_INV_MASK) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 147 | printf(" Unknown invflags: 0x%X", |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 148 | optinfo->invflags & ~IP6T_OPTS_INV_MASK); |
| 149 | } |
| 150 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 151 | static void hbh_save(const void *ip, const struct xt_entry_match *match) |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 152 | { |
| 153 | const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data; |
| 154 | |
| 155 | if (optinfo->flags & IP6T_OPTS_LEN) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 156 | printf("%s --hbh-len %u", |
| 157 | (optinfo->invflags & IP6T_OPTS_INV_LEN) ? " !" : "", |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 158 | optinfo->hdrlen); |
| 159 | } |
| 160 | |
Jan Engelhardt | e2f588a | 2007-10-04 16:30:40 +0000 | [diff] [blame] | 161 | if (optinfo->flags & IP6T_OPTS_OPTS) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 162 | printf(" --hbh-opts"); |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 163 | print_options(optinfo->optsnr, (uint16_t *)optinfo->opts); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 166 | static struct xtables_match hbh_mt6_reg = { |
Harald Welte | 02aa733 | 2005-02-01 15:38:20 +0000 | [diff] [blame] | 167 | .name = "hbh", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 168 | .version = XTABLES_VERSION, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 169 | .family = NFPROTO_IPV6, |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 170 | .size = XT_ALIGN(sizeof(struct ip6t_opts)), |
| 171 | .userspacesize = XT_ALIGN(sizeof(struct ip6t_opts)), |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 172 | .help = hbh_help, |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 173 | .print = hbh_print, |
| 174 | .save = hbh_save, |
Jan Engelhardt | 7a969bb | 2011-03-03 00:40:43 +0100 | [diff] [blame] | 175 | .x6_parse = hbh_parse, |
| 176 | .x6_options = hbh_opts, |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | void |
| 180 | _init(void) |
| 181 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 182 | xtables_register_match(&hbh_mt6_reg); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 183 | } |