libip6t_ipv6header.c revision d09b6d591ca7d7d7575cb6aa20384c9830f777ab
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 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	}
208
209	return 1;
210}
211
212static void ipv6header_check(unsigned int flags)
213{
214	if (!flags) xtables_error(PARAMETER_PROBLEM, "ip6t_ipv6header: no options specified");
215}
216
217static void
218print_header(uint8_t flags){
219        int have_flag = 0;
220
221        while (flags) {
222                unsigned int i;
223
224                for (i = 0; (flags & chain_flags[i].flag) == 0; i++);
225
226                if (have_flag)
227                        printf(",");
228
229                printf("%s", proto_to_name(chain_flags[i].proto,0));
230                have_flag = 1;
231
232                flags &= ~chain_flags[i].flag;
233        }
234
235        if (!have_flag)
236                printf("NONE");
237}
238
239static void ipv6header_print(const void *ip,
240                             const struct xt_entry_match *match, int numeric)
241{
242	const struct ip6t_ipv6header_info *info = (const struct ip6t_ipv6header_info *)match->data;
243	printf("ipv6header ");
244
245        if (info->matchflags || info->invflags) {
246                printf("flags:%s", info->invflags ? "!" : "");
247                if (numeric)
248                        printf("0x%02X ", info->matchflags);
249                else {
250                        print_header(info->matchflags);
251                        printf(" ");
252                }
253        }
254
255	if (info->modeflag)
256		printf("soft ");
257}
258
259static void ipv6header_save(const void *ip, const struct xt_entry_match *match)
260{
261
262	const struct ip6t_ipv6header_info *info = (const struct ip6t_ipv6header_info *)match->data;
263
264	printf("%s--header ", info->invflags ? "! " : "");
265	print_header(info->matchflags);
266	printf(" ");
267	if (info->modeflag)
268		printf("--soft ");
269}
270
271static struct xtables_match ipv6header_mt6_reg = {
272	.name		= "ipv6header",
273	.version	= XTABLES_VERSION,
274	.family		= NFPROTO_IPV6,
275	.size		= XT_ALIGN(sizeof(struct ip6t_ipv6header_info)),
276	.userspacesize	= XT_ALIGN(sizeof(struct ip6t_ipv6header_info)),
277	.help		= ipv6header_help,
278	.init		= ipv6header_init,
279	.parse		= ipv6header_parse,
280	.final_check	= ipv6header_check,
281	.print		= ipv6header_print,
282	.save		= ipv6header_save,
283	.extra_opts	= ipv6header_opts,
284};
285
286void _init(void)
287{
288	xtables_register_match(&ipv6header_mt6_reg);
289}
290