1 /* ipv6header match - matches IPv6 packets based
2 on whether they contain certain headers */
4 /* Original idea: Brad Chapman
5 * Rewritten by: Andras Kis-Szabo <kisza@sch.bme.hu> */
14 #include <sys/types.h>
16 #include <linux/netfilter_ipv6/ip6_tables.h>
17 #include <linux/netfilter_ipv6/ip6t_ipv6header.h>
19 /* This maybe required
21 #include <linux/in6.h>
25 /* A few hardcoded protocols for 'all' and in case the user has no
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 },
53 { "50", IPPROTO_ESP },
54 { "59", IPPROTO_NONE },
55 { "255", IPPROTO_RAW },
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 },
71 proto_to_name(u_int8_t proto, int nolookup)
75 if (proto && !nolookup) {
76 struct protoent *pent = getprotobynumber(proto);
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;
89 name_to_proto(const char *s)
92 struct protoent *pent;
94 if ((pent = getprotobyname(s)))
95 proto = pent->p_proto;
99 i < sizeof(chain_protos)/sizeof(struct pprot);
101 if (strcmp(s, chain_protos[i].name) == 0) {
102 proto = chain_protos[i].num;
107 if (i == sizeof(chain_protos)/sizeof(struct pprot))
108 exit_error(PARAMETER_PROBLEM,
109 "unknown header `%s' specified",
113 return (u_int16_t)proto;
117 add_proto_to_mask(int proto){
118 unsigned int i=0, flag=0;
121 i < sizeof(chain_flags)/sizeof(struct numflag);
123 if (proto == chain_flags[i].proto){
124 flag = chain_flags[i].flag;
129 if (i == sizeof(chain_flags)/sizeof(struct numflag))
130 exit_error(PARAMETER_PROBLEM,
131 "unknown header `%d' specified",
137 static void ipv6header_help(void)
140 "ipv6header 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"
145 " numbers: 0,60,43,44,51,50,59\n"
146 "--soft The header CONTAINS the specified extensions\n");
149 static const struct option ipv6header_opts[] = {
150 { "header", 1, NULL, '1' },
151 { "soft", 0, NULL, '2' },
155 static void ipv6header_init(struct xt_entry_match *m)
157 struct ip6t_ipv6header_info *info = (struct ip6t_ipv6header_info *)m->data;
159 info->matchflags = 0x00;
160 info->invflags = 0x00;
161 info->modeflag = 0x00;
165 parse_header(const char *flags) {
166 unsigned int ret = 0;
170 buffer = strdup(flags);
172 for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ","))
173 ret |= add_proto_to_mask(name_to_proto(ptr));
179 #define IPV6_HDR_HEADER 0x01
180 #define IPV6_HDR_SOFT 0x02
182 /* Parses command options; returns 0 if it ate an option */
184 ipv6header_parse(int c, char **argv, int invert, unsigned int *flags,
185 const void *entry, struct xt_entry_match **match)
187 struct ip6t_ipv6header_info *info = (struct ip6t_ipv6header_info *)(*match)->data;
191 /* Parse the provided header names */
192 if (*flags & IPV6_HDR_HEADER)
193 exit_error(PARAMETER_PROBLEM,
194 "Only one `--header' allowed");
196 check_inverse(optarg, &invert, &optind, 0);
198 if (! (info->matchflags = parse_header(argv[optind-1])) )
199 exit_error(PARAMETER_PROBLEM, "ip6t_ipv6header: cannot parse header names");
202 info->invflags |= 0xFF;
203 *flags |= IPV6_HDR_HEADER;
206 /* Soft-mode requested? */
207 if (*flags & IPV6_HDR_SOFT)
208 exit_error(PARAMETER_PROBLEM,
209 "Only one `--soft' allowed");
211 info->modeflag |= 0xFF;
212 *flags |= IPV6_HDR_SOFT;
221 /* Checks the flags variable */
222 static void ipv6header_check(unsigned int flags)
224 if (!flags) exit_error(PARAMETER_PROBLEM, "ip6t_ipv6header: no options specified");
228 print_header(u_int8_t flags){
234 for (i = 0; (flags & chain_flags[i].flag) == 0; i++);
239 printf("%s", proto_to_name(chain_flags[i].proto,0));
242 flags &= ~chain_flags[i].flag;
249 /* Prints out the match */
250 static void ipv6header_print(const void *ip,
251 const struct xt_entry_match *match, int numeric)
253 const struct ip6t_ipv6header_info *info = (const struct ip6t_ipv6header_info *)match->data;
254 printf("ipv6header ");
256 if (info->matchflags || info->invflags) {
257 printf("flags:%s", info->invflags ? "!" : "");
259 printf("0x%02X ", info->matchflags);
261 print_header(info->matchflags);
272 /* Saves the match */
273 static void ipv6header_save(const void *ip, const struct xt_entry_match *match)
276 const struct ip6t_ipv6header_info *info = (const struct ip6t_ipv6header_info *)match->data;
279 printf("%s", info->invflags ? "!" : "");
280 print_header(info->matchflags);
288 static struct xtables_match ipv6header_mt6_reg = {
289 .name = "ipv6header",
290 .version = XTABLES_VERSION,
292 .size = XT_ALIGN(sizeof(struct ip6t_ipv6header_info)),
293 .userspacesize = XT_ALIGN(sizeof(struct ip6t_ipv6header_info)),
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,
305 xtables_register_match(&ipv6header_mt6_reg);