blob: 04749e6d6b9ea4c0f2074eb7eac66cdf6ae2cdba [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>
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 */
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
113 return (u_int16_t)proto;
114}
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
137static void
138help(void)
139{
140 printf(
141"ipv6header v%s match options:\n"
142"--header [!] headers Type of header to match, by name\n"
143" names: hop,dst,route,frag,auth,esp,none,proto\n"
144" long names: hop-by-hop,ipv6-opts,ipv6-route,\n"
145" ipv6-frag,ah,esp,ipv6-nonxt,protocol\n"
Harald Welte426d9012001-12-25 09:27:03 +0000146" numbers: 0,60,43,44,51,50,59\n"
147"--soft The header CONTAINS the specified extensions\n",
Harald Welte80fe35d2002-05-29 13:08:15 +0000148 IPTABLES_VERSION);
Harald Welte46281d42001-12-18 07:55:41 +0000149}
150
Jan Engelhardt661f1122007-07-30 14:46:51 +0000151static const struct option opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +0000152 { "header", 1, NULL, '1' },
153 { "soft", 0, NULL, '2' },
154 { }
Harald Welte46281d42001-12-18 07:55:41 +0000155};
156
157static void
Peter Rileyea146a92007-09-02 13:09:07 +0000158init(struct xt_entry_match *m)
Harald Welte46281d42001-12-18 07:55:41 +0000159{
160 struct ip6t_ipv6header_info *info = (struct ip6t_ipv6header_info *)m->data;
161
162 info->matchflags = 0x00;
163 info->invflags = 0x00;
Harald Welte426d9012001-12-25 09:27:03 +0000164 info->modeflag = 0x00;
Harald Welte46281d42001-12-18 07:55:41 +0000165}
166
167static unsigned int
168parse_header(const char *flags) {
169 unsigned int ret = 0;
170 char *ptr;
171 char *buffer;
172
173 buffer = strdup(flags);
174
175 for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ","))
176 ret |= add_proto_to_mask(name_to_proto(ptr));
177
178 free(buffer);
179 return ret;
180}
181
Harald Welte426d9012001-12-25 09:27:03 +0000182#define IPV6_HDR_HEADER 0x01
183#define IPV6_HDR_SOFT 0x02
Harald Welte46281d42001-12-18 07:55:41 +0000184
185/* Parses command options; returns 0 if it ate an option */
186static int
187parse(int c, char **argv, int invert, unsigned int *flags,
Yasuyuki KOZAKAIa620c612007-07-24 06:03:45 +0000188 const void *entry,
Yasuyuki KOZAKAIb85256b2007-07-24 05:58:56 +0000189 struct xt_entry_match **match)
Harald Welte46281d42001-12-18 07:55:41 +0000190{
191 struct ip6t_ipv6header_info *info = (struct ip6t_ipv6header_info *)(*match)->data;
192
193 switch (c) {
194 case '1' :
195 /* Parse the provided header names */
196 if (*flags & IPV6_HDR_HEADER)
197 exit_error(PARAMETER_PROBLEM,
198 "Only one `--header' allowed");
199
Harald Welteb77f1da2002-03-14 11:35:58 +0000200 check_inverse(optarg, &invert, &optind, 0);
Harald Welte46281d42001-12-18 07:55:41 +0000201
202 if (! (info->matchflags = parse_header(argv[optind-1])) )
203 exit_error(PARAMETER_PROBLEM, "ip6t_ipv6header: cannot parse header names");
204
205 if (invert)
Harald Welte426d9012001-12-25 09:27:03 +0000206 info->invflags |= 0xFF;
Harald Welte46281d42001-12-18 07:55:41 +0000207 *flags |= IPV6_HDR_HEADER;
Harald Welte426d9012001-12-25 09:27:03 +0000208 break;
209 case '2' :
210 /* Soft-mode requested? */
211 if (*flags & IPV6_HDR_SOFT)
212 exit_error(PARAMETER_PROBLEM,
213 "Only one `--soft' allowed");
Harald Welte46281d42001-12-18 07:55:41 +0000214
Harald Welte426d9012001-12-25 09:27:03 +0000215 info->modeflag |= 0xFF;
216 *flags |= IPV6_HDR_SOFT;
Harald Welte46281d42001-12-18 07:55:41 +0000217 break;
218 default:
219 return 0;
220 }
221
222 return 1;
223}
224
225/* Checks the flags variable */
226static void
227final_check(unsigned int flags)
228{
229 if (!flags) exit_error(PARAMETER_PROBLEM, "ip6t_ipv6header: no options specified");
230}
231
232static void
233print_header(u_int8_t flags){
234 int have_flag = 0;
235
236 while (flags) {
237 unsigned int i;
238
239 for (i = 0; (flags & chain_flags[i].flag) == 0; i++);
240
241 if (have_flag)
242 printf(",");
243
244 printf("%s", proto_to_name(chain_flags[i].proto,0));
245 have_flag = 1;
246
247 flags &= ~chain_flags[i].flag;
248 }
249
250 if (!have_flag)
251 printf("NONE");
252}
253
254/* Prints out the match */
255static void
Yasuyuki KOZAKAIa620c612007-07-24 06:03:45 +0000256print(const void *ip,
Yasuyuki KOZAKAIb85256b2007-07-24 05:58:56 +0000257 const struct xt_entry_match *match,
Harald Welte46281d42001-12-18 07:55:41 +0000258 int numeric)
259{
260 const struct ip6t_ipv6header_info *info = (const struct ip6t_ipv6header_info *)match->data;
261 printf("ipv6header ");
262
263 if (info->matchflags || info->invflags) {
264 printf("flags:%s", info->invflags ? "!" : "");
265 if (numeric)
266 printf("0x%02X ", info->matchflags);
267 else {
268 print_header(info->matchflags);
269 printf(" ");
270 }
271 }
272
Harald Welte426d9012001-12-25 09:27:03 +0000273 if (info->modeflag)
274 printf("soft ");
275
Harald Welte46281d42001-12-18 07:55:41 +0000276 return;
277}
278
279/* Saves the match */
280static void
Yasuyuki KOZAKAIa620c612007-07-24 06:03:45 +0000281save(const void *ip,
Yasuyuki KOZAKAIb85256b2007-07-24 05:58:56 +0000282 const struct xt_entry_match *match)
Harald Welte46281d42001-12-18 07:55:41 +0000283{
284
285 const struct ip6t_ipv6header_info *info = (const struct ip6t_ipv6header_info *)match->data;
286
287 printf("--header ");
288 printf("%s", info->invflags ? "!" : "");
289 print_header(info->matchflags);
290 printf(" ");
Harald Welte426d9012001-12-25 09:27:03 +0000291 if (info->modeflag)
292 printf("--soft ");
Harald Welte46281d42001-12-18 07:55:41 +0000293
294 return;
295}
296
297static
Harald Welte02aa7332005-02-01 15:38:20 +0000298struct ip6tables_match ipv6header = {
299 .name = "ipv6header",
300 .version = IPTABLES_VERSION,
301 .size = IP6T_ALIGN(sizeof(struct ip6t_ipv6header_info)),
302 .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_ipv6header_info)),
303 .help = &help,
304 .init = &init,
305 .parse = &parse,
306 .final_check = &final_check,
307 .print = &print,
308 .save = &save,
309 .extra_opts = opts,
Harald Welte46281d42001-12-18 07:55:41 +0000310};
311
312void _init(void)
313{
314 register_match6(&ipv6header);
315}