blob: ea8870a579ef5f41757b62d3b64bbd3c407b9a71 [file] [log] [blame]
Harald Welte46281d42001-12-18 07:55:41 +00001/* 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>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +01008#include <xtables.h>
Harald Welte46281d42001-12-18 07:55:41 +00009#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 */
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 < 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
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;
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
Jan Engelhardt213e1852009-01-27 17:24:34 +0100113 return proto;
Harald Welte46281d42001-12-18 07:55:41 +0000114}
115
116static unsigned int
117add_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 Engelhardt997045f2007-10-04 16:29:21 +0000137static void ipv6header_help(void)
Harald Welte46281d42001-12-18 07:55:41 +0000138{
139 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200140"ipv6header match options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200141"[!] --header headers Type of header to match, by name\n"
Harald Welte46281d42001-12-18 07:55:41 +0000142" 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 Welte426d9012001-12-25 09:27:03 +0000145" numbers: 0,60,43,44,51,50,59\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200146"--soft The header CONTAINS the specified extensions\n");
Harald Welte46281d42001-12-18 07:55:41 +0000147}
148
Jan Engelhardt997045f2007-10-04 16:29:21 +0000149static const struct option ipv6header_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +0000150 { "header", 1, NULL, '1' },
151 { "soft", 0, NULL, '2' },
Max Kellermann9ee386a2008-01-29 13:48:05 +0000152 { .name = NULL }
Harald Welte46281d42001-12-18 07:55:41 +0000153};
154
Jan Engelhardt997045f2007-10-04 16:29:21 +0000155static void ipv6header_init(struct xt_entry_match *m)
Harald Welte46281d42001-12-18 07:55:41 +0000156{
157 struct ip6t_ipv6header_info *info = (struct ip6t_ipv6header_info *)m->data;
158
159 info->matchflags = 0x00;
160 info->invflags = 0x00;
Harald Welte426d9012001-12-25 09:27:03 +0000161 info->modeflag = 0x00;
Harald Welte46281d42001-12-18 07:55:41 +0000162}
163
164static unsigned int
165parse_header(const char *flags) {
166 unsigned int ret = 0;
167 char *ptr;
168 char *buffer;
169
170 buffer = strdup(flags);
171
172 for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ","))
173 ret |= add_proto_to_mask(name_to_proto(ptr));
174
175 free(buffer);
176 return ret;
177}
178
Harald Welte426d9012001-12-25 09:27:03 +0000179#define IPV6_HDR_HEADER 0x01
180#define IPV6_HDR_SOFT 0x02
Harald Welte46281d42001-12-18 07:55:41 +0000181
Harald Welte46281d42001-12-18 07:55:41 +0000182static int
Jan Engelhardt997045f2007-10-04 16:29:21 +0000183ipv6header_parse(int c, char **argv, int invert, unsigned int *flags,
184 const void *entry, struct xt_entry_match **match)
Harald Welte46281d42001-12-18 07:55:41 +0000185{
186 struct ip6t_ipv6header_info *info = (struct ip6t_ipv6header_info *)(*match)->data;
187
188 switch (c) {
189 case '1' :
190 /* Parse the provided header names */
191 if (*flags & IPV6_HDR_HEADER)
192 exit_error(PARAMETER_PROBLEM,
193 "Only one `--header' allowed");
194
Harald Welteb77f1da2002-03-14 11:35:58 +0000195 check_inverse(optarg, &invert, &optind, 0);
Harald Welte46281d42001-12-18 07:55:41 +0000196
197 if (! (info->matchflags = parse_header(argv[optind-1])) )
198 exit_error(PARAMETER_PROBLEM, "ip6t_ipv6header: cannot parse header names");
199
200 if (invert)
Harald Welte426d9012001-12-25 09:27:03 +0000201 info->invflags |= 0xFF;
Harald Welte46281d42001-12-18 07:55:41 +0000202 *flags |= IPV6_HDR_HEADER;
Harald Welte426d9012001-12-25 09:27:03 +0000203 break;
204 case '2' :
205 /* Soft-mode requested? */
206 if (*flags & IPV6_HDR_SOFT)
207 exit_error(PARAMETER_PROBLEM,
208 "Only one `--soft' allowed");
Harald Welte46281d42001-12-18 07:55:41 +0000209
Harald Welte426d9012001-12-25 09:27:03 +0000210 info->modeflag |= 0xFF;
211 *flags |= IPV6_HDR_SOFT;
Harald Welte46281d42001-12-18 07:55:41 +0000212 break;
213 default:
214 return 0;
215 }
216
217 return 1;
218}
219
Jan Engelhardt997045f2007-10-04 16:29:21 +0000220static void ipv6header_check(unsigned int flags)
Harald Welte46281d42001-12-18 07:55:41 +0000221{
222 if (!flags) exit_error(PARAMETER_PROBLEM, "ip6t_ipv6header: no options specified");
223}
224
225static void
226print_header(u_int8_t flags){
227 int have_flag = 0;
228
229 while (flags) {
230 unsigned int i;
231
232 for (i = 0; (flags & chain_flags[i].flag) == 0; i++);
233
234 if (have_flag)
235 printf(",");
236
237 printf("%s", proto_to_name(chain_flags[i].proto,0));
238 have_flag = 1;
239
240 flags &= ~chain_flags[i].flag;
241 }
242
243 if (!have_flag)
244 printf("NONE");
245}
246
Jan Engelhardt997045f2007-10-04 16:29:21 +0000247static void ipv6header_print(const void *ip,
248 const struct xt_entry_match *match, int numeric)
Harald Welte46281d42001-12-18 07:55:41 +0000249{
250 const struct ip6t_ipv6header_info *info = (const struct ip6t_ipv6header_info *)match->data;
251 printf("ipv6header ");
252
253 if (info->matchflags || info->invflags) {
254 printf("flags:%s", info->invflags ? "!" : "");
255 if (numeric)
256 printf("0x%02X ", info->matchflags);
257 else {
258 print_header(info->matchflags);
259 printf(" ");
260 }
261 }
262
Harald Welte426d9012001-12-25 09:27:03 +0000263 if (info->modeflag)
264 printf("soft ");
Harald Welte46281d42001-12-18 07:55:41 +0000265}
266
Jan Engelhardt997045f2007-10-04 16:29:21 +0000267static void ipv6header_save(const void *ip, const struct xt_entry_match *match)
Harald Welte46281d42001-12-18 07:55:41 +0000268{
269
270 const struct ip6t_ipv6header_info *info = (const struct ip6t_ipv6header_info *)match->data;
271
Jan Engelhardtcea9f712008-12-09 15:06:20 +0100272 printf("%s--header ", info->invflags ? "! " : "");
Harald Welte46281d42001-12-18 07:55:41 +0000273 print_header(info->matchflags);
274 printf(" ");
Harald Welte426d9012001-12-25 09:27:03 +0000275 if (info->modeflag)
276 printf("--soft ");
Harald Welte46281d42001-12-18 07:55:41 +0000277}
278
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200279static struct xtables_match ipv6header_mt6_reg = {
Harald Welte02aa7332005-02-01 15:38:20 +0000280 .name = "ipv6header",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200281 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100282 .family = NFPROTO_IPV6,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200283 .size = XT_ALIGN(sizeof(struct ip6t_ipv6header_info)),
284 .userspacesize = XT_ALIGN(sizeof(struct ip6t_ipv6header_info)),
Jan Engelhardt997045f2007-10-04 16:29:21 +0000285 .help = ipv6header_help,
286 .init = ipv6header_init,
287 .parse = ipv6header_parse,
288 .final_check = ipv6header_check,
289 .print = ipv6header_print,
290 .save = ipv6header_save,
291 .extra_opts = ipv6header_opts,
Harald Welte46281d42001-12-18 07:55:41 +0000292};
293
294void _init(void)
295{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200296 xtables_register_match(&ipv6header_mt6_reg);
Harald Welte46281d42001-12-18 07:55:41 +0000297}