blob: e2ae09f4eb22028355a328905c3d56550efc935c [file] [log] [blame]
András Kis-Szabóa4204162002-04-24 09:35:01 +00001/* 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>
András Kis-Szabófce86992002-04-29 13:48:46 +00009/*#include <linux/in6.h>*/
András Kis-Szabóa4204162002-04-24 09:35:01 +000010#include <linux/netfilter_ipv6/ip6t_rt.h>
András Kis-Szabófce86992002-04-29 13:48:46 +000011#include <sys/types.h>
12#include <sys/socket.h>
13#include <arpa/inet.h>
András Kis-Szabóa4204162002-04-24 09:35:01 +000014
András Kis-Szabófce86992002-04-29 13:48:46 +000015/*#define DEBUG 1*/
16
András Kis-Szabóa4204162002-04-24 09:35:01 +000017/* Function which prints out usage message. */
Jan Engelhardt997045f2007-10-04 16:29:21 +000018static void rt_help(void)
András Kis-Szabóa4204162002-04-24 09:35:01 +000019{
20 printf(
21"RT v%s 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"
András Kis-Szabófce86992002-04-29 13:48:46 +000026" --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",
Harald Welte80fe35d2002-05-29 13:08:15 +000028IPTABLES_VERSION, IP6T_RT_HOPS);
András Kis-Szabóa4204162002-04-24 09:35:01 +000029}
30
Jan Engelhardt997045f2007-10-04 16:29:21 +000031static const struct option rt_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000032 { "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' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000038 { .name = NULL }
András Kis-Szabóa4204162002-04-24 09:35:01 +000039};
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
András Kis-Szabófce86992002-04-29 13:48:46 +000084static 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
András Kis-Szabóa4204162002-04-24 09:35:01 +0000139/* Initialize the match. */
Jan Engelhardt997045f2007-10-04 16:29:21 +0000140static void rt_init(struct xt_entry_match *m)
András Kis-Szabóa4204162002-04-24 09:35:01 +0000141{
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;
András Kis-Szabófce86992002-04-29 13:48:46 +0000150 rtinfo->addrnr = 0;
András Kis-Szabóa4204162002-04-24 09:35:01 +0000151}
152
153/* Function which parses command options; returns true if it
154 ate an option */
Jan Engelhardt997045f2007-10-04 16:29:21 +0000155static int rt_parse(int c, char **argv, int invert, unsigned int *flags,
156 const void *entry, struct xt_entry_match **match)
András Kis-Szabóa4204162002-04-24 09:35:01 +0000157{
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,
András Kis-Szabófce86992002-04-29 13:48:46 +0000210 "`--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);
András Kis-Szabóa4204162002-04-24 09:35:01 +0000216 rtinfo->flags |= IP6T_RT_FST;
217 *flags |= IP6T_RT_FST;
218 break;
András Kis-Szabófce86992002-04-29 13:48:46 +0000219 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;
András Kis-Szabóa4204162002-04-24 09:35:01 +0000229 default:
230 return 0;
231 }
232
233 return 1;
234}
235
András Kis-Szabóa4204162002-04-24 09:35:01 +0000236static 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
András Kis-Szabófce86992002-04-29 13:48:46 +0000257static void
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100258print_addresses(unsigned int addrnr, struct in6_addr *addrp)
András Kis-Szabófce86992002-04-29 13:48:46 +0000259{
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
András Kis-Szabóa4204162002-04-24 09:35:01 +0000267/* Prints out the union ip6t_matchinfo. */
Jan Engelhardt997045f2007-10-04 16:29:21 +0000268static void rt_print(const void *ip, const struct xt_entry_match *match,
269 int numeric)
András Kis-Szabóa4204162002-04-24 09:35:01 +0000270{
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 ");
András Kis-Szabófce86992002-04-29 13:48:46 +0000286 if (rtinfo->flags & IP6T_RT_FST) printf("0-addrs ");
Fabrice MARIEae31bb62002-06-14 07:38:16 +0000287 print_addresses(rtinfo->addrnr, (struct in6_addr *)rtinfo->addrs);
András Kis-Szabófce86992002-04-29 13:48:46 +0000288 if (rtinfo->flags & IP6T_RT_FST_NSTRICT) printf("0-not-strict ");
András Kis-Szabóa4204162002-04-24 09:35:01 +0000289 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. */
Jan Engelhardt997045f2007-10-04 16:29:21 +0000295static void rt_save(const void *ip, const struct xt_entry_match *match)
András Kis-Szabóa4204162002-04-24 09:35:01 +0000296{
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 ");
Fabrice MARIEae31bb62002-06-14 07:38:16 +0000327 print_addresses(rtinfo->addrnr, (struct in6_addr *)rtinfo->addrs);
András Kis-Szabófce86992002-04-29 13:48:46 +0000328 if (rtinfo->flags & IP6T_RT_FST_NSTRICT) printf("--rt-0-not-strict ");
András Kis-Szabóa4204162002-04-24 09:35:01 +0000329
330}
331
Jan Engelhardt997045f2007-10-04 16:29:21 +0000332static struct ip6tables_match rt_match6 = {
Harald Welte02aa7332005-02-01 15:38:20 +0000333 .name = "rt",
334 .version = IPTABLES_VERSION,
335 .size = IP6T_ALIGN(sizeof(struct ip6t_rt)),
336 .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_rt)),
Jan Engelhardt997045f2007-10-04 16:29:21 +0000337 .help = rt_help,
338 .init = rt_init,
339 .parse = rt_parse,
340 .print = rt_print,
341 .save = rt_save,
342 .extra_opts = rt_opts,
András Kis-Szabóa4204162002-04-24 09:35:01 +0000343};
344
345void
346_init(void)
347{
Jan Engelhardt997045f2007-10-04 16:29:21 +0000348 register_match6(&rt_match6);
András Kis-Szabóa4204162002-04-24 09:35:01 +0000349}