libip6t_ipv6header.c revision 458d84de2412b43604a8efe2b82a2084a2859a46
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 uint8_t num; 30}; 31 32struct numflag { 33 uint8_t proto; 34 uint8_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(uint8_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 uint16_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 unsigned int 150parse_header(const char *flags) { 151 unsigned int ret = 0; 152 char *ptr; 153 char *buffer; 154 155 buffer = strdup(flags); 156 157 for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) 158 ret |= add_proto_to_mask(name_to_proto(ptr)); 159 160 free(buffer); 161 return ret; 162} 163 164#define IPV6_HDR_HEADER 0x01 165#define IPV6_HDR_SOFT 0x02 166 167static int 168ipv6header_parse(int c, char **argv, int invert, unsigned int *flags, 169 const void *entry, struct xt_entry_match **match) 170{ 171 struct ip6t_ipv6header_info *info = (struct ip6t_ipv6header_info *)(*match)->data; 172 173 switch (c) { 174 case '1' : 175 /* Parse the provided header names */ 176 if (*flags & IPV6_HDR_HEADER) 177 xtables_error(PARAMETER_PROBLEM, 178 "Only one `--header' allowed"); 179 180 xtables_check_inverse(optarg, &invert, &optind, 0, argv); 181 182 if (! (info->matchflags = parse_header(optarg)) ) 183 xtables_error(PARAMETER_PROBLEM, "ip6t_ipv6header: cannot parse header names"); 184 185 if (invert) 186 info->invflags |= 0xFF; 187 *flags |= IPV6_HDR_HEADER; 188 break; 189 case '2' : 190 /* Soft-mode requested? */ 191 if (*flags & IPV6_HDR_SOFT) 192 xtables_error(PARAMETER_PROBLEM, 193 "Only one `--soft' allowed"); 194 195 info->modeflag |= 0xFF; 196 *flags |= IPV6_HDR_SOFT; 197 break; 198 } 199 200 return 1; 201} 202 203static void ipv6header_check(unsigned int flags) 204{ 205 if (!(flags & IPV6_HDR_HEADER)) 206 xtables_error(PARAMETER_PROBLEM, 207 "ip6t_ipv6header: no options specified"); 208} 209 210static void 211print_header(uint8_t flags){ 212 int have_flag = 0; 213 214 while (flags) { 215 unsigned int i; 216 217 for (i = 0; (flags & chain_flags[i].flag) == 0; i++); 218 219 if (have_flag) 220 printf(","); 221 222 printf("%s", proto_to_name(chain_flags[i].proto,0)); 223 have_flag = 1; 224 225 flags &= ~chain_flags[i].flag; 226 } 227 228 if (!have_flag) 229 printf("NONE"); 230} 231 232static void ipv6header_print(const void *ip, 233 const struct xt_entry_match *match, int numeric) 234{ 235 const struct ip6t_ipv6header_info *info = (const struct ip6t_ipv6header_info *)match->data; 236 printf(" ipv6header"); 237 238 if (info->matchflags || info->invflags) { 239 printf(" flags:%s", info->invflags ? "!" : ""); 240 if (numeric) 241 printf("0x%02X", info->matchflags); 242 else { 243 print_header(info->matchflags); 244 } 245 } 246 247 if (info->modeflag) 248 printf(" soft"); 249} 250 251static void ipv6header_save(const void *ip, const struct xt_entry_match *match) 252{ 253 254 const struct ip6t_ipv6header_info *info = (const struct ip6t_ipv6header_info *)match->data; 255 256 printf("%s --header ", info->invflags ? " !" : ""); 257 print_header(info->matchflags); 258 if (info->modeflag) 259 printf(" --soft"); 260} 261 262static struct xtables_match ipv6header_mt6_reg = { 263 .name = "ipv6header", 264 .version = XTABLES_VERSION, 265 .family = NFPROTO_IPV6, 266 .size = XT_ALIGN(sizeof(struct ip6t_ipv6header_info)), 267 .userspacesize = XT_ALIGN(sizeof(struct ip6t_ipv6header_info)), 268 .help = ipv6header_help, 269 .parse = ipv6header_parse, 270 .final_check = ipv6header_check, 271 .print = ipv6header_print, 272 .save = ipv6header_save, 273 .extra_opts = ipv6header_opts, 274}; 275 276void _init(void) 277{ 278 xtables_register_match(&ipv6header_mt6_reg); 279} 280