libip6t_rt.c revision 967279231a9ecfa99f26694a954afc535c63db1d
1/* Shared library add-on to ip6tables to add Routing header support. */
2#include <stdio.h>
3#include <netdb.h>
4#include <string.h>
5#include <stdlib.h>
6#include <getopt.h>
7#include <errno.h>
8#include <ip6tables.h>
9/*#include <linux/in6.h>*/
10#include <linux/netfilter_ipv6/ip6t_rt.h>
11#include <sys/types.h>
12#include <sys/socket.h>
13#include <arpa/inet.h>
14
15/*#define DEBUG	1*/
16
17/* Function which prints out usage message. */
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	{ "rt-type", 1, NULL, '1' },
33	{ "rt-segsleft", 1, NULL, '2' },
34	{ "rt-len", 1, NULL, '3' },
35	{ "rt-0-res", 0, NULL, '4' },
36	{ "rt-0-addrs", 1, NULL, '5' },
37	{ "rt-0-not-strict", 0, NULL, '6' },
38	{ .name = NULL }
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		exit_error(PARAMETER_PROBLEM,
51			   "RT no valid digits in %s `%s'", typestr, idstr);
52	}
53	if ( id == ULONG_MAX  && errno == ERANGE ) {
54		exit_error(PARAMETER_PROBLEM,
55			   "%s `%s' specified too big: would overflow",
56			   typestr, idstr);
57	}
58	if ( *idstr != '\0'  && *ep != '\0' ) {
59		exit_error(PARAMETER_PROBLEM,
60			   "RT error parsing %s `%s'", typestr, idstr);
61	}
62	return (u_int32_t) 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        exit_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) exit_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) exit_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
139/* Initialize the match. */
140static void rt_init(struct xt_entry_match *m)
141{
142	struct ip6t_rt *rtinfo = (struct ip6t_rt *)m->data;
143
144	rtinfo->rt_type = 0x0L;
145	rtinfo->segsleft[0] = 0x0L;
146	rtinfo->segsleft[1] = 0xFFFFFFFF;
147	rtinfo->hdrlen = 0;
148	rtinfo->flags = 0;
149	rtinfo->invflags = 0;
150	rtinfo->addrnr = 0;
151}
152
153/* Function which parses command options; returns true if it
154   ate an option */
155static int rt_parse(int c, char **argv, int invert, unsigned int *flags,
156                    const void *entry, struct xt_entry_match **match)
157{
158	struct ip6t_rt *rtinfo = (struct ip6t_rt *)(*match)->data;
159
160	switch (c) {
161	case '1':
162		if (*flags & IP6T_RT_TYP)
163			exit_error(PARAMETER_PROBLEM,
164				   "Only one `--rt-type' allowed");
165		check_inverse(optarg, &invert, &optind, 0);
166		rtinfo->rt_type = parse_rt_num(argv[optind-1], "type");
167		if (invert)
168			rtinfo->invflags |= IP6T_RT_INV_TYP;
169		rtinfo->flags |= IP6T_RT_TYP;
170		*flags |= IP6T_RT_TYP;
171		break;
172	case '2':
173		if (*flags & IP6T_RT_SGS)
174			exit_error(PARAMETER_PROBLEM,
175				   "Only one `--rt-segsleft' allowed");
176		check_inverse(optarg, &invert, &optind, 0);
177		parse_rt_segsleft(argv[optind-1], rtinfo->segsleft);
178		if (invert)
179			rtinfo->invflags |= IP6T_RT_INV_SGS;
180		rtinfo->flags |= IP6T_RT_SGS;
181		*flags |= IP6T_RT_SGS;
182		break;
183	case '3':
184		if (*flags & IP6T_RT_LEN)
185			exit_error(PARAMETER_PROBLEM,
186				   "Only one `--rt-len' allowed");
187		check_inverse(optarg, &invert, &optind, 0);
188		rtinfo->hdrlen = parse_rt_num(argv[optind-1], "length");
189		if (invert)
190			rtinfo->invflags |= IP6T_RT_INV_LEN;
191		rtinfo->flags |= IP6T_RT_LEN;
192		*flags |= IP6T_RT_LEN;
193		break;
194	case '4':
195		if (*flags & IP6T_RT_RES)
196			exit_error(PARAMETER_PROBLEM,
197				   "Only one `--rt-0-res' allowed");
198		if ( !(*flags & IP6T_RT_TYP) || (rtinfo->rt_type != 0) || (rtinfo->invflags & IP6T_RT_INV_TYP) )
199			exit_error(PARAMETER_PROBLEM,
200				   "`--rt-type 0' required before `--rt-0-res'");
201		rtinfo->flags |= IP6T_RT_RES;
202		*flags |= IP6T_RT_RES;
203		break;
204	case '5':
205		if (*flags & IP6T_RT_FST)
206			exit_error(PARAMETER_PROBLEM,
207				   "Only one `--rt-0-addrs' allowed");
208		if ( !(*flags & IP6T_RT_TYP) || (rtinfo->rt_type != 0) || (rtinfo->invflags & IP6T_RT_INV_TYP) )
209			exit_error(PARAMETER_PROBLEM,
210				   "`--rt-type 0' required before `--rt-0-addrs'");
211		check_inverse(optarg, &invert, &optind, 0);
212		if (invert)
213			exit_error(PARAMETER_PROBLEM,
214				   " '!' not allowed with `--rt-0-addrs'");
215		rtinfo->addrnr = parse_addresses(argv[optind-1], rtinfo->addrs);
216		rtinfo->flags |= IP6T_RT_FST;
217		*flags |= IP6T_RT_FST;
218		break;
219	case '6':
220		if (*flags & IP6T_RT_FST_NSTRICT)
221			exit_error(PARAMETER_PROBLEM,
222				   "Only one `--rt-0-not-strict' allowed");
223		if ( !(*flags & IP6T_RT_FST) )
224			exit_error(PARAMETER_PROBLEM,
225				   "`--rt-0-addr ...' required before `--rt-0-not-strict'");
226		rtinfo->flags |= IP6T_RT_FST_NSTRICT;
227		*flags |= IP6T_RT_FST_NSTRICT;
228		break;
229	default:
230		return 0;
231	}
232
233	return 1;
234}
235
236static void
237print_nums(const char *name, u_int32_t min, u_int32_t max,
238	    int invert)
239{
240	const char *inv = invert ? "!" : "";
241
242	if (min != 0 || max != 0xFFFFFFFF || invert) {
243		printf("%s", name);
244		if (min == max) {
245			printf(":%s", inv);
246			printf("%u", min);
247		} else {
248			printf("s:%s", inv);
249			printf("%u",min);
250			printf(":");
251			printf("%u",max);
252		}
253		printf(" ");
254	}
255}
256
257static void
258print_addresses(unsigned int addrnr, struct in6_addr *addrp)
259{
260	unsigned int i;
261
262	for(i=0; i<addrnr; i++){
263		printf("%s%c", addr_to_numeric(&(addrp[i])), (i!=addrnr-1)?',':' ');
264	}
265}
266
267/* Prints out the union ip6t_matchinfo. */
268static void rt_print(const void *ip, const struct xt_entry_match *match,
269                     int numeric)
270{
271	const struct ip6t_rt *rtinfo = (struct ip6t_rt *)match->data;
272
273	printf("rt ");
274	if (rtinfo->flags & IP6T_RT_TYP)
275	    printf("type:%s%d ", rtinfo->invflags & IP6T_RT_INV_TYP ? "!" : "",
276		    rtinfo->rt_type);
277	print_nums("segsleft", rtinfo->segsleft[0], rtinfo->segsleft[1],
278		    rtinfo->invflags & IP6T_RT_INV_SGS);
279	if (rtinfo->flags & IP6T_RT_LEN) {
280		printf("length");
281		printf(":%s", rtinfo->invflags & IP6T_RT_INV_LEN ? "!" : "");
282		printf("%u", rtinfo->hdrlen);
283		printf(" ");
284	}
285	if (rtinfo->flags & IP6T_RT_RES) printf("reserved ");
286	if (rtinfo->flags & IP6T_RT_FST) printf("0-addrs ");
287	print_addresses(rtinfo->addrnr, (struct in6_addr *)rtinfo->addrs);
288	if (rtinfo->flags & IP6T_RT_FST_NSTRICT) printf("0-not-strict ");
289	if (rtinfo->invflags & ~IP6T_RT_INV_MASK)
290		printf("Unknown invflags: 0x%X ",
291		       rtinfo->invflags & ~IP6T_RT_INV_MASK);
292}
293
294/* Saves the union ip6t_matchinfo in parsable form to stdout. */
295static void rt_save(const void *ip, const struct xt_entry_match *match)
296{
297	const struct ip6t_rt *rtinfo = (struct ip6t_rt *)match->data;
298
299	if (rtinfo->flags & IP6T_RT_TYP) {
300		printf("--rt-type %s%u ",
301			(rtinfo->invflags & IP6T_RT_INV_TYP) ? "! " : "",
302			rtinfo->rt_type);
303	}
304
305	if (!(rtinfo->segsleft[0] == 0
306	    && rtinfo->segsleft[1] == 0xFFFFFFFF)) {
307		printf("--rt-segsleft %s",
308			(rtinfo->invflags & IP6T_RT_INV_SGS) ? "! " : "");
309		if (rtinfo->segsleft[0]
310		    != rtinfo->segsleft[1])
311			printf("%u:%u ",
312			       rtinfo->segsleft[0],
313			       rtinfo->segsleft[1]);
314		else
315			printf("%u ",
316			       rtinfo->segsleft[0]);
317	}
318
319	if (rtinfo->flags & IP6T_RT_LEN) {
320		printf("--rt-len %s%u ",
321			(rtinfo->invflags & IP6T_RT_INV_LEN) ? "! " : "",
322			rtinfo->hdrlen);
323	}
324
325	if (rtinfo->flags & IP6T_RT_RES) printf("--rt-0-res ");
326	if (rtinfo->flags & IP6T_RT_FST) printf("--rt-0-addrs ");
327	print_addresses(rtinfo->addrnr, (struct in6_addr *)rtinfo->addrs);
328	if (rtinfo->flags & IP6T_RT_FST_NSTRICT) printf("--rt-0-not-strict ");
329
330}
331
332static struct xtables_match rt_mt6_reg = {
333	.name		= "rt",
334	.version	= XTABLES_VERSION,
335	.family		= PF_INET6,
336	.size		= XT_ALIGN(sizeof(struct ip6t_rt)),
337	.userspacesize	= XT_ALIGN(sizeof(struct ip6t_rt)),
338	.help		= rt_help,
339	.init		= rt_init,
340	.parse		= rt_parse,
341	.print		= rt_print,
342	.save		= rt_save,
343	.extra_opts	= rt_opts,
344};
345
346void
347_init(void)
348{
349	xtables_register_match(&rt_mt6_reg);
350}
351