| Harald Welte | 46281d4 | 2001-12-18 07:55:41 +0000 | [diff] [blame] | 1 | /* ipv6header match - matches IPv6 packets based |
| 2 | on whether they contain certain headers */ |
| 3 | |
| 4 | /* Original idea: Brad Chapman |
| 5 | * Rewritten by: Andras Kis-Szabo <kisza@sch.bme.hu> */ |
| 6 | |
| 7 | #include <getopt.h> |
| 8 | #include <ip6tables.h> |
| 9 | #include <stddef.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <netdb.h> |
| 14 | #include <sys/types.h> |
| 15 | |
| 16 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 17 | #include <linux/netfilter_ipv6/ip6t_ipv6header.h> |
| 18 | |
| 19 | /* This maybe required |
| 20 | #include <linux/in.h> |
| 21 | #include <linux/in6.h> |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | /* A few hardcoded protocols for 'all' and in case the user has no |
| 26 | * /etc/protocols */ |
| 27 | struct pprot { |
| 28 | char *name; |
| 29 | u_int8_t num; |
| 30 | }; |
| 31 | |
| 32 | struct numflag { |
| 33 | u_int8_t proto; |
| 34 | u_int8_t flag; |
| 35 | }; |
| 36 | |
| 37 | static const struct pprot chain_protos[] = { |
| 38 | { "hop-by-hop", IPPROTO_HOPOPTS }, |
| 39 | { "protocol", IPPROTO_RAW }, |
| 40 | { "hop", IPPROTO_HOPOPTS }, |
| 41 | { "dst", IPPROTO_DSTOPTS }, |
| 42 | { "route", IPPROTO_ROUTING }, |
| 43 | { "frag", IPPROTO_FRAGMENT }, |
| 44 | { "auth", IPPROTO_AH }, |
| 45 | { "esp", IPPROTO_ESP }, |
| 46 | { "none", IPPROTO_NONE }, |
| 47 | { "prot", IPPROTO_RAW }, |
| 48 | { "0", IPPROTO_HOPOPTS }, |
| 49 | { "60", IPPROTO_DSTOPTS }, |
| 50 | { "43", IPPROTO_ROUTING }, |
| 51 | { "44", IPPROTO_FRAGMENT }, |
| 52 | { "51", IPPROTO_AH }, |
| 53 | { "50", IPPROTO_ESP }, |
| 54 | { "59", IPPROTO_NONE }, |
| 55 | { "255", IPPROTO_RAW }, |
| 56 | /* { "all", 0 }, */ |
| 57 | }; |
| 58 | |
| 59 | static const struct numflag chain_flags[] = { |
| 60 | { IPPROTO_HOPOPTS, MASK_HOPOPTS }, |
| 61 | { IPPROTO_DSTOPTS, MASK_DSTOPTS }, |
| 62 | { IPPROTO_ROUTING, MASK_ROUTING }, |
| 63 | { IPPROTO_FRAGMENT, MASK_FRAGMENT }, |
| 64 | { IPPROTO_AH, MASK_AH }, |
| 65 | { IPPROTO_ESP, MASK_ESP }, |
| 66 | { IPPROTO_NONE, MASK_NONE }, |
| 67 | { IPPROTO_RAW, MASK_PROTO }, |
| 68 | }; |
| 69 | |
| 70 | static char * |
| 71 | proto_to_name(u_int8_t proto, int nolookup) |
| 72 | { |
| 73 | unsigned int i; |
| 74 | |
| 75 | if (proto && !nolookup) { |
| 76 | struct protoent *pent = getprotobynumber(proto); |
| 77 | if (pent) |
| 78 | return pent->p_name; |
| 79 | } |
| 80 | |
| 81 | for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++) |
| 82 | if (chain_protos[i].num == proto) |
| 83 | return chain_protos[i].name; |
| 84 | |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | static u_int16_t |
| 89 | name_to_proto(const char *s) |
| 90 | { |
| 91 | unsigned int proto=0; |
| 92 | struct protoent *pent; |
| 93 | |
| 94 | if ((pent = getprotobyname(s))) |
| 95 | proto = pent->p_proto; |
| 96 | else { |
| 97 | unsigned int i; |
| 98 | for (i = 0; |
| 99 | i < sizeof(chain_protos)/sizeof(struct pprot); |
| 100 | i++) { |
| 101 | if (strcmp(s, chain_protos[i].name) == 0) { |
| 102 | proto = chain_protos[i].num; |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (i == sizeof(chain_protos)/sizeof(struct pprot)) |
| 108 | exit_error(PARAMETER_PROBLEM, |
| 109 | "unknown header `%s' specified", |
| 110 | s); |
| 111 | } |
| 112 | |
| 113 | return (u_int16_t)proto; |
| 114 | } |
| 115 | |
| 116 | static unsigned int |
| 117 | add_proto_to_mask(int proto){ |
| 118 | unsigned int i=0, flag=0; |
| 119 | |
| 120 | for (i = 0; |
| 121 | i < sizeof(chain_flags)/sizeof(struct numflag); |
| 122 | i++) { |
| 123 | if (proto == chain_flags[i].proto){ |
| 124 | flag = chain_flags[i].flag; |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if (i == sizeof(chain_flags)/sizeof(struct numflag)) |
| 130 | exit_error(PARAMETER_PROBLEM, |
| 131 | "unknown header `%d' specified", |
| 132 | proto); |
| 133 | |
| 134 | return flag; |
| 135 | } |
| 136 | |
| Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 137 | static void ipv6header_help(void) |
| Harald Welte | 46281d4 | 2001-12-18 07:55:41 +0000 | [diff] [blame] | 138 | { |
| 139 | printf( |
| 140 | "ipv6header v%s match options:\n" |
| 141 | "--header [!] headers Type of header to match, by name\n" |
| 142 | " names: hop,dst,route,frag,auth,esp,none,proto\n" |
| 143 | " long names: hop-by-hop,ipv6-opts,ipv6-route,\n" |
| 144 | " ipv6-frag,ah,esp,ipv6-nonxt,protocol\n" |
| Harald Welte | 426d901 | 2001-12-25 09:27:03 +0000 | [diff] [blame] | 145 | " numbers: 0,60,43,44,51,50,59\n" |
| 146 | "--soft The header CONTAINS the specified extensions\n", |
| Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 147 | IPTABLES_VERSION); |
| Harald Welte | 46281d4 | 2001-12-18 07:55:41 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 150 | static const struct option ipv6header_opts[] = { |
| Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 151 | { "header", 1, NULL, '1' }, |
| 152 | { "soft", 0, NULL, '2' }, |
| Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame^] | 153 | { .name = NULL } |
| Harald Welte | 46281d4 | 2001-12-18 07:55:41 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
| Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 156 | static void ipv6header_init(struct xt_entry_match *m) |
| Harald Welte | 46281d4 | 2001-12-18 07:55:41 +0000 | [diff] [blame] | 157 | { |
| 158 | struct ip6t_ipv6header_info *info = (struct ip6t_ipv6header_info *)m->data; |
| 159 | |
| 160 | info->matchflags = 0x00; |
| 161 | info->invflags = 0x00; |
| Harald Welte | 426d901 | 2001-12-25 09:27:03 +0000 | [diff] [blame] | 162 | info->modeflag = 0x00; |
| Harald Welte | 46281d4 | 2001-12-18 07:55:41 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static unsigned int |
| 166 | parse_header(const char *flags) { |
| 167 | unsigned int ret = 0; |
| 168 | char *ptr; |
| 169 | char *buffer; |
| 170 | |
| 171 | buffer = strdup(flags); |
| 172 | |
| 173 | for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) |
| 174 | ret |= add_proto_to_mask(name_to_proto(ptr)); |
| 175 | |
| 176 | free(buffer); |
| 177 | return ret; |
| 178 | } |
| 179 | |
| Harald Welte | 426d901 | 2001-12-25 09:27:03 +0000 | [diff] [blame] | 180 | #define IPV6_HDR_HEADER 0x01 |
| 181 | #define IPV6_HDR_SOFT 0x02 |
| Harald Welte | 46281d4 | 2001-12-18 07:55:41 +0000 | [diff] [blame] | 182 | |
| 183 | /* Parses command options; returns 0 if it ate an option */ |
| 184 | static int |
| Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 185 | ipv6header_parse(int c, char **argv, int invert, unsigned int *flags, |
| 186 | const void *entry, struct xt_entry_match **match) |
| Harald Welte | 46281d4 | 2001-12-18 07:55:41 +0000 | [diff] [blame] | 187 | { |
| 188 | struct ip6t_ipv6header_info *info = (struct ip6t_ipv6header_info *)(*match)->data; |
| 189 | |
| 190 | switch (c) { |
| 191 | case '1' : |
| 192 | /* Parse the provided header names */ |
| 193 | if (*flags & IPV6_HDR_HEADER) |
| 194 | exit_error(PARAMETER_PROBLEM, |
| 195 | "Only one `--header' allowed"); |
| 196 | |
| Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 197 | check_inverse(optarg, &invert, &optind, 0); |
| Harald Welte | 46281d4 | 2001-12-18 07:55:41 +0000 | [diff] [blame] | 198 | |
| 199 | if (! (info->matchflags = parse_header(argv[optind-1])) ) |
| 200 | exit_error(PARAMETER_PROBLEM, "ip6t_ipv6header: cannot parse header names"); |
| 201 | |
| 202 | if (invert) |
| Harald Welte | 426d901 | 2001-12-25 09:27:03 +0000 | [diff] [blame] | 203 | info->invflags |= 0xFF; |
| Harald Welte | 46281d4 | 2001-12-18 07:55:41 +0000 | [diff] [blame] | 204 | *flags |= IPV6_HDR_HEADER; |
| Harald Welte | 426d901 | 2001-12-25 09:27:03 +0000 | [diff] [blame] | 205 | break; |
| 206 | case '2' : |
| 207 | /* Soft-mode requested? */ |
| 208 | if (*flags & IPV6_HDR_SOFT) |
| 209 | exit_error(PARAMETER_PROBLEM, |
| 210 | "Only one `--soft' allowed"); |
| Harald Welte | 46281d4 | 2001-12-18 07:55:41 +0000 | [diff] [blame] | 211 | |
| Harald Welte | 426d901 | 2001-12-25 09:27:03 +0000 | [diff] [blame] | 212 | info->modeflag |= 0xFF; |
| 213 | *flags |= IPV6_HDR_SOFT; |
| Harald Welte | 46281d4 | 2001-12-18 07:55:41 +0000 | [diff] [blame] | 214 | break; |
| 215 | default: |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | return 1; |
| 220 | } |
| 221 | |
| 222 | /* Checks the flags variable */ |
| Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 223 | static void ipv6header_check(unsigned int flags) |
| Harald Welte | 46281d4 | 2001-12-18 07:55:41 +0000 | [diff] [blame] | 224 | { |
| 225 | if (!flags) exit_error(PARAMETER_PROBLEM, "ip6t_ipv6header: no options specified"); |
| 226 | } |
| 227 | |
| 228 | static void |
| 229 | print_header(u_int8_t flags){ |
| 230 | int have_flag = 0; |
| 231 | |
| 232 | while (flags) { |
| 233 | unsigned int i; |
| 234 | |
| 235 | for (i = 0; (flags & chain_flags[i].flag) == 0; i++); |
| 236 | |
| 237 | if (have_flag) |
| 238 | printf(","); |
| 239 | |
| 240 | printf("%s", proto_to_name(chain_flags[i].proto,0)); |
| 241 | have_flag = 1; |
| 242 | |
| 243 | flags &= ~chain_flags[i].flag; |
| 244 | } |
| 245 | |
| 246 | if (!have_flag) |
| 247 | printf("NONE"); |
| 248 | } |
| 249 | |
| 250 | /* Prints out the match */ |
| Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 251 | static void ipv6header_print(const void *ip, |
| 252 | const struct xt_entry_match *match, int numeric) |
| Harald Welte | 46281d4 | 2001-12-18 07:55:41 +0000 | [diff] [blame] | 253 | { |
| 254 | const struct ip6t_ipv6header_info *info = (const struct ip6t_ipv6header_info *)match->data; |
| 255 | printf("ipv6header "); |
| 256 | |
| 257 | if (info->matchflags || info->invflags) { |
| 258 | printf("flags:%s", info->invflags ? "!" : ""); |
| 259 | if (numeric) |
| 260 | printf("0x%02X ", info->matchflags); |
| 261 | else { |
| 262 | print_header(info->matchflags); |
| 263 | printf(" "); |
| 264 | } |
| 265 | } |
| 266 | |
| Harald Welte | 426d901 | 2001-12-25 09:27:03 +0000 | [diff] [blame] | 267 | if (info->modeflag) |
| 268 | printf("soft "); |
| 269 | |
| Harald Welte | 46281d4 | 2001-12-18 07:55:41 +0000 | [diff] [blame] | 270 | return; |
| 271 | } |
| 272 | |
| 273 | /* Saves the match */ |
| Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 274 | static void ipv6header_save(const void *ip, const struct xt_entry_match *match) |
| Harald Welte | 46281d4 | 2001-12-18 07:55:41 +0000 | [diff] [blame] | 275 | { |
| 276 | |
| 277 | const struct ip6t_ipv6header_info *info = (const struct ip6t_ipv6header_info *)match->data; |
| 278 | |
| 279 | printf("--header "); |
| 280 | printf("%s", info->invflags ? "!" : ""); |
| 281 | print_header(info->matchflags); |
| 282 | printf(" "); |
| Harald Welte | 426d901 | 2001-12-25 09:27:03 +0000 | [diff] [blame] | 283 | if (info->modeflag) |
| 284 | printf("--soft "); |
| Harald Welte | 46281d4 | 2001-12-18 07:55:41 +0000 | [diff] [blame] | 285 | |
| 286 | return; |
| 287 | } |
| 288 | |
| Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 289 | static struct ip6tables_match ipv6header_match6 = { |
| Harald Welte | 02aa733 | 2005-02-01 15:38:20 +0000 | [diff] [blame] | 290 | .name = "ipv6header", |
| 291 | .version = IPTABLES_VERSION, |
| 292 | .size = IP6T_ALIGN(sizeof(struct ip6t_ipv6header_info)), |
| 293 | .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_ipv6header_info)), |
| Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 294 | .help = ipv6header_help, |
| 295 | .init = ipv6header_init, |
| 296 | .parse = ipv6header_parse, |
| 297 | .final_check = ipv6header_check, |
| 298 | .print = ipv6header_print, |
| 299 | .save = ipv6header_save, |
| 300 | .extra_opts = ipv6header_opts, |
| Harald Welte | 46281d4 | 2001-12-18 07:55:41 +0000 | [diff] [blame] | 301 | }; |
| 302 | |
| 303 | void _init(void) |
| 304 | { |
| Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 305 | register_match6(&ipv6header_match6); |
| Harald Welte | 46281d4 | 2001-12-18 07:55:41 +0000 | [diff] [blame] | 306 | } |