libip6t_ipv6header.c revision 32b8e61e4e5bd405d9ad07bf9468498dfbb19f9e
1/* ipv6header match - matches IPv6 packets based 2on 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 <xtables.h> 9#include <stdbool.h> 10#include <stddef.h> 11#include <stdio.h> 12#include <stdlib.h> 13#include <string.h> 14#include <netdb.h> 15#include <sys/types.h> 16 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 */ 27struct pprot { 28 char *name; 29 u_int8_t num; 30}; 31 32struct numflag { 33 u_int8_t proto; 34 u_int8_t flag; 35}; 36 37static 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 59static 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 70static char * 71proto_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 < ARRAY_SIZE(chain_protos); ++i) 82 if (chain_protos[i].num == proto) 83 return chain_protos[i].name; 84 85 return NULL; 86} 87 88static u_int16_t 89name_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; i < ARRAY_SIZE(chain_protos); ++i) 99 if (strcmp(s, chain_protos[i].name) == 0) { 100 proto = chain_protos[i].num; 101 break; 102 } 103 104 if (i == ARRAY_SIZE(chain_protos)) 105 xtables_error(PARAMETER_PROBLEM, 106 "unknown header `%s' specified", 107 s); 108 } 109 110 return proto; 111} 112 113static unsigned int 114add_proto_to_mask(int proto){ 115 unsigned int i=0, flag=0; 116 117 for (i = 0; i < ARRAY_SIZE(chain_flags); ++i) 118 if (proto == chain_flags[i].proto){ 119 flag = chain_flags[i].flag; 120 break; 121 } 122 123 if (i == ARRAY_SIZE(chain_flags)) 124 xtables_error(PARAMETER_PROBLEM, 125 "unknown header `%d' specified", 126 proto); 127 128 return flag; 129} 130 131static void ipv6header_help(void) 132{ 133 printf( 134"ipv6header match options:\n" 135"[!] --header headers Type of header to match, by name\n" 136" names: hop,dst,route,frag,auth,esp,none,proto\n" 137" long names: hop-by-hop,ipv6-opts,ipv6-route,\n" 138" ipv6-frag,ah,esp,ipv6-nonxt,protocol\n" 139" numbers: 0,60,43,44,51,50,59\n" 140"--soft The header CONTAINS the specified extensions\n"); 141} 142 143static const struct option ipv6header_opts[] = { 144 {.name = "header", .has_arg = true, .val = '1'}, 145 {.name = "soft", .has_arg = false, .val = '2'}, 146 XT_GETOPT_TABLEEND, 147}; 148 149static void ipv6header_init(struct xt_entry_match *m) 150{ 151 struct ip6t_ipv6header_info *info = (struct ip6t_ipv6header_info *)m->data; 152 153 info->matchflags = 0x00; 154 info->invflags = 0x00; 155 info->modeflag = 0x00; 156} 157 158static unsigned int 159parse_header(const char *flags) { 160 unsigned int ret = 0; 161 char *ptr; 162 char *buffer; 163 164 buffer = strdup(flags); 165 166 for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) 167 ret |= add_proto_to_mask(name_to_proto(ptr)); 168 169 free(buffer); 170 return ret; 171} 172 173#define IPV6_HDR_HEADER 0x01 174#define IPV6_HDR_SOFT 0x02 175 176static int 177ipv6header_parse(int c, char **argv, int invert, unsigned int *flags, 178 const void *entry, struct xt_entry_match **match) 179{ 180 struct ip6t_ipv6header_info *info = (struct ip6t_ipv6header_info *)(*match)->data; 181 182 switch (c) { 183 case '1' : 184 /* Parse the provided header names */ 185 if (*flags & IPV6_HDR_HEADER) 186 xtables_error(PARAMETER_PROBLEM, 187 "Only one `--header' allowed"); 188 189 xtables_check_inverse(optarg, &invert, &optind, 0, argv); 190 191 if (! (info->matchflags = parse_header(optarg)) ) 192 xtables_error(PARAMETER_PROBLEM, "ip6t_ipv6header: cannot parse header names"); 193 194 if (invert) 195 info->invflags |= 0xFF; 196 *flags |= IPV6_HDR_HEADER; 197 break; 198 case '2' : 199 /* Soft-mode requested? */ 200 if (*flags & IPV6_HDR_SOFT) 201 xtables_error(PARAMETER_PROBLEM, 202 "Only one `--soft' allowed"); 203 204 info->modeflag |= 0xFF; 205 *flags |= IPV6_HDR_SOFT; 206 break; 207 default: 208 return 0; 209 } 210 211 return 1; 212} 213 214static void ipv6header_check(unsigned int flags) 215{ 216 if (!flags) xtables_error(PARAMETER_PROBLEM, "ip6t_ipv6header: no options specified"); 217} 218 219static void 220print_header(u_int8_t flags){ 221 int have_flag = 0; 222 223 while (flags) { 224 unsigned int i; 225 226 for (i = 0; (flags & chain_flags[i].flag) == 0; i++); 227 228 if (have_flag) 229 printf(","); 230 231 printf("%s", proto_to_name(chain_flags[i].proto,0)); 232 have_flag = 1; 233 234 flags &= ~chain_flags[i].flag; 235 } 236 237 if (!have_flag) 238 printf("NONE"); 239} 240 241static void ipv6header_print(const void *ip, 242 const struct xt_entry_match *match, int numeric) 243{ 244 const struct ip6t_ipv6header_info *info = (const struct ip6t_ipv6header_info *)match->data; 245 printf("ipv6header "); 246 247 if (info->matchflags || info->invflags) { 248 printf("flags:%s", info->invflags ? "!" : ""); 249 if (numeric) 250 printf("0x%02X ", info->matchflags); 251 else { 252 print_header(info->matchflags); 253 printf(" "); 254 } 255 } 256 257 if (info->modeflag) 258 printf("soft "); 259} 260 261static void ipv6header_save(const void *ip, const struct xt_entry_match *match) 262{ 263 264 const struct ip6t_ipv6header_info *info = (const struct ip6t_ipv6header_info *)match->data; 265 266 printf("%s--header ", info->invflags ? "! " : ""); 267 print_header(info->matchflags); 268 printf(" "); 269 if (info->modeflag) 270 printf("--soft "); 271} 272 273static struct xtables_match ipv6header_mt6_reg = { 274 .name = "ipv6header", 275 .version = XTABLES_VERSION, 276 .family = NFPROTO_IPV6, 277 .size = XT_ALIGN(sizeof(struct ip6t_ipv6header_info)), 278 .userspacesize = XT_ALIGN(sizeof(struct ip6t_ipv6header_info)), 279 .help = ipv6header_help, 280 .init = ipv6header_init, 281 .parse = ipv6header_parse, 282 .final_check = ipv6header_check, 283 .print = ipv6header_print, 284 .save = ipv6header_save, 285 .extra_opts = ipv6header_opts, 286}; 287 288void _init(void) 289{ 290 xtables_register_match(&ipv6header_mt6_reg); 291} 292