libip6t_rt.c revision 32b8e61e4e5bd405d9ad07bf9468498dfbb19f9e
1/* Shared library add-on to ip6tables to add Routing header support. */
2#include <stdbool.h>
3#include <stdio.h>
4#include <netdb.h>
5#include <string.h>
6#include <stdlib.h>
7#include <getopt.h>
8#include <errno.h>
9#include <xtables.h>
10/*#include <linux/in6.h>*/
11#include <linux/netfilter_ipv6/ip6t_rt.h>
12#include <sys/types.h>
13#include <sys/socket.h>
14#include <arpa/inet.h>
15
16/*#define DEBUG	1*/
17
18static void rt_help(void)
19{
20	printf(
21"rt match options:\n"
22"[!] --rt-type type             match the type\n"
23"[!] --rt-segsleft num[:num]    match the Segments Left field (range)\n"
24"[!] --rt-len length            total length of this header\n"
25" --rt-0-res                    check the reserved filed, too (type 0)\n"
26" --rt-0-addrs ADDR[,ADDR...]   Type=0 addresses (list, max: %d)\n"
27" --rt-0-not-strict             List of Type=0 addresses not a strict list\n",
28IP6T_RT_HOPS);
29}
30
31static const struct option rt_opts[] = {
32	{.name = "rt-type",         .has_arg = true,  .val = '1'},
33	{.name = "rt-segsleft",     .has_arg = true,  .val = '2'},
34	{.name = "rt-len",          .has_arg = true,  .val = '3'},
35	{.name = "rt-0-res",        .has_arg = false, .val = '4'},
36	{.name = "rt-0-addrs",      .has_arg = true,  .val = '5'},
37	{.name = "rt-0-not-strict", .has_arg = false, .val = '6'},
38	XT_GETOPT_TABLEEND,
39};
40
41static u_int32_t
42parse_rt_num(const char *idstr, const char *typestr)
43{
44	unsigned long int id;
45	char* ep;
46
47	id =  strtoul(idstr,&ep,0) ;
48
49	if ( idstr == ep ) {
50		xtables_error(PARAMETER_PROBLEM,
51			   "RT no valid digits in %s `%s'", typestr, idstr);
52	}
53	if ( id == ULONG_MAX  && errno == ERANGE ) {
54		xtables_error(PARAMETER_PROBLEM,
55			   "%s `%s' specified too big: would overflow",
56			   typestr, idstr);
57	}
58	if ( *idstr != '\0'  && *ep != '\0' ) {
59		xtables_error(PARAMETER_PROBLEM,
60			   "RT error parsing %s `%s'", typestr, idstr);
61	}
62	return id;
63}
64
65static void
66parse_rt_segsleft(const char *idstring, u_int32_t *ids)
67{
68	char *buffer;
69	char *cp;
70
71	buffer = strdup(idstring);
72	if ((cp = strchr(buffer, ':')) == NULL)
73		ids[0] = ids[1] = parse_rt_num(buffer,"segsleft");
74	else {
75		*cp = '\0';
76		cp++;
77
78		ids[0] = buffer[0] ? parse_rt_num(buffer,"segsleft") : 0;
79		ids[1] = cp[0] ? parse_rt_num(cp,"segsleft") : 0xFFFFFFFF;
80	}
81	free(buffer);
82}
83
84static char *
85addr_to_numeric(const struct in6_addr *addrp)
86{
87	static char buf[50+1];
88	return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
89}
90
91static struct in6_addr *
92numeric_to_addr(const char *num)
93{
94	static struct in6_addr ap;
95	int err;
96
97	if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
98		return &ap;
99#ifdef DEBUG
100	fprintf(stderr, "\nnumeric2addr: %d\n", err);
101#endif
102	xtables_error(PARAMETER_PROBLEM, "bad address: %s", num);
103
104	return (struct in6_addr *)NULL;
105}
106
107
108static int
109parse_addresses(const char *addrstr, struct in6_addr *addrp)
110{
111        char *buffer, *cp, *next;
112        unsigned int i;
113
114	buffer = strdup(addrstr);
115	if (!buffer) xtables_error(OTHER_PROBLEM, "strdup failed");
116
117        for (cp=buffer, i=0; cp && i<IP6T_RT_HOPS; cp=next,i++)
118        {
119                next=strchr(cp, ',');
120                if (next) *next++='\0';
121		memcpy(&(addrp[i]), numeric_to_addr(cp), sizeof(struct in6_addr));
122#if DEBUG
123		printf("addr str: %s\n", cp);
124		printf("addr ip6: %s\n", addr_to_numeric((numeric_to_addr(cp))));
125		printf("addr [%d]: %s\n", i, addr_to_numeric(&(addrp[i])));
126#endif
127	}
128	if (cp) xtables_error(PARAMETER_PROBLEM, "too many addresses specified");
129
130	free(buffer);
131
132#if DEBUG
133	printf("addr nr: %d\n", i);
134#endif
135
136	return i;
137}
138
139static void rt_init(struct xt_entry_match *m)
140{
141	struct ip6t_rt *rtinfo = (struct ip6t_rt *)m->data;
142
143	rtinfo->rt_type = 0x0L;
144	rtinfo->segsleft[0] = 0x0L;
145	rtinfo->segsleft[1] = 0xFFFFFFFF;
146	rtinfo->hdrlen = 0;
147	rtinfo->flags = 0;
148	rtinfo->invflags = 0;
149	rtinfo->addrnr = 0;
150}
151
152static int rt_parse(int c, char **argv, int invert, unsigned int *flags,
153                    const void *entry, struct xt_entry_match **match)
154{
155	struct ip6t_rt *rtinfo = (struct ip6t_rt *)(*match)->data;
156
157	switch (c) {
158	case '1':
159		if (*flags & IP6T_RT_TYP)
160			xtables_error(PARAMETER_PROBLEM,
161				   "Only one `--rt-type' allowed");
162		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
163		rtinfo->rt_type = parse_rt_num(optarg, "type");
164		if (invert)
165			rtinfo->invflags |= IP6T_RT_INV_TYP;
166		rtinfo->flags |= IP6T_RT_TYP;
167		*flags |= IP6T_RT_TYP;
168		break;
169	case '2':
170		if (*flags & IP6T_RT_SGS)
171			xtables_error(PARAMETER_PROBLEM,
172				   "Only one `--rt-segsleft' allowed");
173		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
174		parse_rt_segsleft(optarg, rtinfo->segsleft);
175		if (invert)
176			rtinfo->invflags |= IP6T_RT_INV_SGS;
177		rtinfo->flags |= IP6T_RT_SGS;
178		*flags |= IP6T_RT_SGS;
179		break;
180	case '3':
181		if (*flags & IP6T_RT_LEN)
182			xtables_error(PARAMETER_PROBLEM,
183				   "Only one `--rt-len' allowed");
184		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
185		rtinfo->hdrlen = parse_rt_num(optarg, "length");
186		if (invert)
187			rtinfo->invflags |= IP6T_RT_INV_LEN;
188		rtinfo->flags |= IP6T_RT_LEN;
189		*flags |= IP6T_RT_LEN;
190		break;
191	case '4':
192		if (*flags & IP6T_RT_RES)
193			xtables_error(PARAMETER_PROBLEM,
194				   "Only one `--rt-0-res' allowed");
195		if ( !(*flags & IP6T_RT_TYP) || (rtinfo->rt_type != 0) || (rtinfo->invflags & IP6T_RT_INV_TYP) )
196			xtables_error(PARAMETER_PROBLEM,
197				   "`--rt-type 0' required before `--rt-0-res'");
198		rtinfo->flags |= IP6T_RT_RES;
199		*flags |= IP6T_RT_RES;
200		break;
201	case '5':
202		if (*flags & IP6T_RT_FST)
203			xtables_error(PARAMETER_PROBLEM,
204				   "Only one `--rt-0-addrs' allowed");
205		if ( !(*flags & IP6T_RT_TYP) || (rtinfo->rt_type != 0) || (rtinfo->invflags & IP6T_RT_INV_TYP) )
206			xtables_error(PARAMETER_PROBLEM,
207				   "`--rt-type 0' required before `--rt-0-addrs'");
208		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
209		if (invert)
210			xtables_error(PARAMETER_PROBLEM,
211				   " '!' not allowed with `--rt-0-addrs'");
212		rtinfo->addrnr = parse_addresses(optarg, rtinfo->addrs);
213		rtinfo->flags |= IP6T_RT_FST;
214		*flags |= IP6T_RT_FST;
215		break;
216	case '6':
217		if (*flags & IP6T_RT_FST_NSTRICT)
218			xtables_error(PARAMETER_PROBLEM,
219				   "Only one `--rt-0-not-strict' allowed");
220		if ( !(*flags & IP6T_RT_FST) )
221			xtables_error(PARAMETER_PROBLEM,
222				   "`--rt-0-addr ...' required before `--rt-0-not-strict'");
223		rtinfo->flags |= IP6T_RT_FST_NSTRICT;
224		*flags |= IP6T_RT_FST_NSTRICT;
225		break;
226	default:
227		return 0;
228	}
229
230	return 1;
231}
232
233static void
234print_nums(const char *name, u_int32_t min, u_int32_t max,
235	    int invert)
236{
237	const char *inv = invert ? "!" : "";
238
239	if (min != 0 || max != 0xFFFFFFFF || invert) {
240		printf("%s", name);
241		if (min == max) {
242			printf(":%s", inv);
243			printf("%u", min);
244		} else {
245			printf("s:%s", inv);
246			printf("%u",min);
247			printf(":");
248			printf("%u",max);
249		}
250		printf(" ");
251	}
252}
253
254static void
255print_addresses(unsigned int addrnr, struct in6_addr *addrp)
256{
257	unsigned int i;
258
259	for(i=0; i<addrnr; i++){
260		printf("%s%c", addr_to_numeric(&(addrp[i])), (i!=addrnr-1)?',':' ');
261	}
262}
263
264static void rt_print(const void *ip, const struct xt_entry_match *match,
265                     int numeric)
266{
267	const struct ip6t_rt *rtinfo = (struct ip6t_rt *)match->data;
268
269	printf("rt ");
270	if (rtinfo->flags & IP6T_RT_TYP)
271	    printf("type:%s%d ", rtinfo->invflags & IP6T_RT_INV_TYP ? "!" : "",
272		    rtinfo->rt_type);
273	print_nums("segsleft", rtinfo->segsleft[0], rtinfo->segsleft[1],
274		    rtinfo->invflags & IP6T_RT_INV_SGS);
275	if (rtinfo->flags & IP6T_RT_LEN) {
276		printf("length");
277		printf(":%s", rtinfo->invflags & IP6T_RT_INV_LEN ? "!" : "");
278		printf("%u", rtinfo->hdrlen);
279		printf(" ");
280	}
281	if (rtinfo->flags & IP6T_RT_RES) printf("reserved ");
282	if (rtinfo->flags & IP6T_RT_FST) printf("0-addrs ");
283	print_addresses(rtinfo->addrnr, (struct in6_addr *)rtinfo->addrs);
284	if (rtinfo->flags & IP6T_RT_FST_NSTRICT) printf("0-not-strict ");
285	if (rtinfo->invflags & ~IP6T_RT_INV_MASK)
286		printf("Unknown invflags: 0x%X ",
287		       rtinfo->invflags & ~IP6T_RT_INV_MASK);
288}
289
290static void rt_save(const void *ip, const struct xt_entry_match *match)
291{
292	const struct ip6t_rt *rtinfo = (struct ip6t_rt *)match->data;
293
294	if (rtinfo->flags & IP6T_RT_TYP) {
295		printf("%s--rt-type %u ",
296			(rtinfo->invflags & IP6T_RT_INV_TYP) ? "! " : "",
297			rtinfo->rt_type);
298	}
299
300	if (!(rtinfo->segsleft[0] == 0
301	    && rtinfo->segsleft[1] == 0xFFFFFFFF)) {
302		printf("%s--rt-segsleft ",
303			(rtinfo->invflags & IP6T_RT_INV_SGS) ? "! " : "");
304		if (rtinfo->segsleft[0]
305		    != rtinfo->segsleft[1])
306			printf("%u:%u ",
307			       rtinfo->segsleft[0],
308			       rtinfo->segsleft[1]);
309		else
310			printf("%u ",
311			       rtinfo->segsleft[0]);
312	}
313
314	if (rtinfo->flags & IP6T_RT_LEN) {
315		printf("%s--rt-len %u ",
316			(rtinfo->invflags & IP6T_RT_INV_LEN) ? "! " : "",
317			rtinfo->hdrlen);
318	}
319
320	if (rtinfo->flags & IP6T_RT_RES) printf("--rt-0-res ");
321	if (rtinfo->flags & IP6T_RT_FST) printf("--rt-0-addrs ");
322	print_addresses(rtinfo->addrnr, (struct in6_addr *)rtinfo->addrs);
323	if (rtinfo->flags & IP6T_RT_FST_NSTRICT) printf("--rt-0-not-strict ");
324
325}
326
327static struct xtables_match rt_mt6_reg = {
328	.name		= "rt",
329	.version	= XTABLES_VERSION,
330	.family		= NFPROTO_IPV6,
331	.size		= XT_ALIGN(sizeof(struct ip6t_rt)),
332	.userspacesize	= XT_ALIGN(sizeof(struct ip6t_rt)),
333	.help		= rt_help,
334	.init		= rt_init,
335	.parse		= rt_parse,
336	.print		= rt_print,
337	.save		= rt_save,
338	.extra_opts	= rt_opts,
339};
340
341void
342_init(void)
343{
344	xtables_register_match(&rt_mt6_reg);
345}
346