libip6t_rt.c revision e88a7c2c7175742b58b6aa03f2b5aba2d80330a1
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 field 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 uint32_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, uint32_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->segsleft[1] = 0xFFFFFFFF;
144}
145
146static int rt_parse(int c, char **argv, int invert, unsigned int *flags,
147                    const void *entry, struct xt_entry_match **match)
148{
149	struct ip6t_rt *rtinfo = (struct ip6t_rt *)(*match)->data;
150
151	switch (c) {
152	case '1':
153		if (*flags & IP6T_RT_TYP)
154			xtables_error(PARAMETER_PROBLEM,
155				   "Only one `--rt-type' allowed");
156		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
157		rtinfo->rt_type = parse_rt_num(optarg, "type");
158		if (invert)
159			rtinfo->invflags |= IP6T_RT_INV_TYP;
160		rtinfo->flags |= IP6T_RT_TYP;
161		*flags |= IP6T_RT_TYP;
162		break;
163	case '2':
164		if (*flags & IP6T_RT_SGS)
165			xtables_error(PARAMETER_PROBLEM,
166				   "Only one `--rt-segsleft' allowed");
167		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
168		parse_rt_segsleft(optarg, rtinfo->segsleft);
169		if (invert)
170			rtinfo->invflags |= IP6T_RT_INV_SGS;
171		rtinfo->flags |= IP6T_RT_SGS;
172		*flags |= IP6T_RT_SGS;
173		break;
174	case '3':
175		if (*flags & IP6T_RT_LEN)
176			xtables_error(PARAMETER_PROBLEM,
177				   "Only one `--rt-len' allowed");
178		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
179		rtinfo->hdrlen = parse_rt_num(optarg, "length");
180		if (invert)
181			rtinfo->invflags |= IP6T_RT_INV_LEN;
182		rtinfo->flags |= IP6T_RT_LEN;
183		*flags |= IP6T_RT_LEN;
184		break;
185	case '4':
186		if (*flags & IP6T_RT_RES)
187			xtables_error(PARAMETER_PROBLEM,
188				   "Only one `--rt-0-res' allowed");
189		if ( !(*flags & IP6T_RT_TYP) || (rtinfo->rt_type != 0) || (rtinfo->invflags & IP6T_RT_INV_TYP) )
190			xtables_error(PARAMETER_PROBLEM,
191				   "`--rt-type 0' required before `--rt-0-res'");
192		rtinfo->flags |= IP6T_RT_RES;
193		*flags |= IP6T_RT_RES;
194		break;
195	case '5':
196		if (*flags & IP6T_RT_FST)
197			xtables_error(PARAMETER_PROBLEM,
198				   "Only one `--rt-0-addrs' allowed");
199		if ( !(*flags & IP6T_RT_TYP) || (rtinfo->rt_type != 0) || (rtinfo->invflags & IP6T_RT_INV_TYP) )
200			xtables_error(PARAMETER_PROBLEM,
201				   "`--rt-type 0' required before `--rt-0-addrs'");
202		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
203		if (invert)
204			xtables_error(PARAMETER_PROBLEM,
205				   " '!' not allowed with `--rt-0-addrs'");
206		rtinfo->addrnr = parse_addresses(optarg, rtinfo->addrs);
207		rtinfo->flags |= IP6T_RT_FST;
208		*flags |= IP6T_RT_FST;
209		break;
210	case '6':
211		if (*flags & IP6T_RT_FST_NSTRICT)
212			xtables_error(PARAMETER_PROBLEM,
213				   "Only one `--rt-0-not-strict' allowed");
214		if ( !(*flags & IP6T_RT_FST) )
215			xtables_error(PARAMETER_PROBLEM,
216				   "`--rt-0-addr ...' required before `--rt-0-not-strict'");
217		rtinfo->flags |= IP6T_RT_FST_NSTRICT;
218		*flags |= IP6T_RT_FST_NSTRICT;
219		break;
220	}
221
222	return 1;
223}
224
225static void
226print_nums(const char *name, uint32_t min, uint32_t max,
227	    int invert)
228{
229	const char *inv = invert ? "!" : "";
230
231	if (min != 0 || max != 0xFFFFFFFF || invert) {
232		printf(" %s", name);
233		if (min == max) {
234			printf(":%s", inv);
235			printf("%u", min);
236		} else {
237			printf("s:%s", inv);
238			printf("%u",min);
239			printf(":");
240			printf("%u",max);
241		}
242	}
243}
244
245static void
246print_addresses(unsigned int addrnr, struct in6_addr *addrp)
247{
248	unsigned int i;
249
250	for(i=0; i<addrnr; i++){
251		printf("%c%s", (i==0)?' ':',', addr_to_numeric(&(addrp[i])));
252	}
253}
254
255static void rt_print(const void *ip, const struct xt_entry_match *match,
256                     int numeric)
257{
258	const struct ip6t_rt *rtinfo = (struct ip6t_rt *)match->data;
259
260	printf(" rt");
261	if (rtinfo->flags & IP6T_RT_TYP)
262	    printf(" type:%s%d", rtinfo->invflags & IP6T_RT_INV_TYP ? "!" : "",
263		    rtinfo->rt_type);
264	print_nums("segsleft", rtinfo->segsleft[0], rtinfo->segsleft[1],
265		    rtinfo->invflags & IP6T_RT_INV_SGS);
266	if (rtinfo->flags & IP6T_RT_LEN) {
267		printf(" length");
268		printf(":%s", rtinfo->invflags & IP6T_RT_INV_LEN ? "!" : "");
269		printf("%u", rtinfo->hdrlen);
270	}
271	if (rtinfo->flags & IP6T_RT_RES) printf(" reserved");
272	if (rtinfo->flags & IP6T_RT_FST) printf(" 0-addrs");
273	print_addresses(rtinfo->addrnr, (struct in6_addr *)rtinfo->addrs);
274	if (rtinfo->flags & IP6T_RT_FST_NSTRICT) printf(" 0-not-strict");
275	if (rtinfo->invflags & ~IP6T_RT_INV_MASK)
276		printf(" Unknown invflags: 0x%X",
277		       rtinfo->invflags & ~IP6T_RT_INV_MASK);
278}
279
280static void rt_save(const void *ip, const struct xt_entry_match *match)
281{
282	const struct ip6t_rt *rtinfo = (struct ip6t_rt *)match->data;
283
284	if (rtinfo->flags & IP6T_RT_TYP) {
285		printf("%s --rt-type %u",
286			(rtinfo->invflags & IP6T_RT_INV_TYP) ? " !" : "",
287			rtinfo->rt_type);
288	}
289
290	if (!(rtinfo->segsleft[0] == 0
291	    && rtinfo->segsleft[1] == 0xFFFFFFFF)) {
292		printf("%s --rt-segsleft ",
293			(rtinfo->invflags & IP6T_RT_INV_SGS) ? " !" : "");
294		if (rtinfo->segsleft[0]
295		    != rtinfo->segsleft[1])
296			printf("%u:%u",
297			       rtinfo->segsleft[0],
298			       rtinfo->segsleft[1]);
299		else
300			printf("%u",
301			       rtinfo->segsleft[0]);
302	}
303
304	if (rtinfo->flags & IP6T_RT_LEN) {
305		printf("%s --rt-len %u",
306			(rtinfo->invflags & IP6T_RT_INV_LEN) ? " !" : "",
307			rtinfo->hdrlen);
308	}
309
310	if (rtinfo->flags & IP6T_RT_RES) printf(" --rt-0-res");
311	if (rtinfo->flags & IP6T_RT_FST) printf(" --rt-0-addrs");
312	print_addresses(rtinfo->addrnr, (struct in6_addr *)rtinfo->addrs);
313	if (rtinfo->flags & IP6T_RT_FST_NSTRICT) printf(" --rt-0-not-strict");
314
315}
316
317static struct xtables_match rt_mt6_reg = {
318	.name		= "rt",
319	.version	= XTABLES_VERSION,
320	.family		= NFPROTO_IPV6,
321	.size		= XT_ALIGN(sizeof(struct ip6t_rt)),
322	.userspacesize	= XT_ALIGN(sizeof(struct ip6t_rt)),
323	.help		= rt_help,
324	.init		= rt_init,
325	.parse		= rt_parse,
326	.print		= rt_print,
327	.save		= rt_save,
328	.extra_opts	= rt_opts,
329};
330
331void
332_init(void)
333{
334	xtables_register_match(&rt_mt6_reg);
335}
336