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