1 /* Shared library add-on to ip6tables to add Routing header support. */
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>
17 /* Function which prints out usage message. */
18 static void rt_help(void)
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",
31 static 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' },
42 parse_rt_num(const char *idstr, const char *typestr)
47 id = strtoul(idstr,&ep,0) ;
50 exit_error(PARAMETER_PROBLEM,
51 "RT no valid digits in %s `%s'", typestr, idstr);
53 if ( id == ULONG_MAX && errno == ERANGE ) {
54 exit_error(PARAMETER_PROBLEM,
55 "%s `%s' specified too big: would overflow",
58 if ( *idstr != '\0' && *ep != '\0' ) {
59 exit_error(PARAMETER_PROBLEM,
60 "RT error parsing %s `%s'", typestr, idstr);
62 return (u_int32_t) id;
66 parse_rt_segsleft(const char *idstring, u_int32_t *ids)
71 buffer = strdup(idstring);
72 if ((cp = strchr(buffer, ':')) == NULL)
73 ids[0] = ids[1] = parse_rt_num(buffer,"segsleft");
78 ids[0] = buffer[0] ? parse_rt_num(buffer,"segsleft") : 0;
79 ids[1] = cp[0] ? parse_rt_num(cp,"segsleft") : 0xFFFFFFFF;
85 addr_to_numeric(const struct in6_addr *addrp)
87 static char buf[50+1];
88 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
91 static struct in6_addr *
92 numeric_to_addr(const char *num)
94 static struct in6_addr ap;
97 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
100 fprintf(stderr, "\nnumeric2addr: %d\n", err);
102 exit_error(PARAMETER_PROBLEM, "bad address: %s", num);
104 return (struct in6_addr *)NULL;
109 parse_addresses(const char *addrstr, struct in6_addr *addrp)
111 char *buffer, *cp, *next;
114 buffer = strdup(addrstr);
115 if (!buffer) exit_error(OTHER_PROBLEM, "strdup failed");
117 for (cp=buffer, i=0; cp && i<IP6T_RT_HOPS; cp=next,i++)
119 next=strchr(cp, ',');
120 if (next) *next++='\0';
121 memcpy(&(addrp[i]), numeric_to_addr(cp), sizeof(struct in6_addr));
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])));
128 if (cp) exit_error(PARAMETER_PROBLEM, "too many addresses specified");
133 printf("addr nr: %d\n", i);
139 /* Initialize the match. */
140 static void rt_init(struct xt_entry_match *m)
142 struct ip6t_rt *rtinfo = (struct ip6t_rt *)m->data;
144 rtinfo->rt_type = 0x0L;
145 rtinfo->segsleft[0] = 0x0L;
146 rtinfo->segsleft[1] = 0xFFFFFFFF;
149 rtinfo->invflags = 0;
153 /* Function which parses command options; returns true if it
155 static int rt_parse(int c, char **argv, int invert, unsigned int *flags,
156 const void *entry, struct xt_entry_match **match)
158 struct ip6t_rt *rtinfo = (struct ip6t_rt *)(*match)->data;
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");
168 rtinfo->invflags |= IP6T_RT_INV_TYP;
169 rtinfo->flags |= IP6T_RT_TYP;
170 *flags |= IP6T_RT_TYP;
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);
179 rtinfo->invflags |= IP6T_RT_INV_SGS;
180 rtinfo->flags |= IP6T_RT_SGS;
181 *flags |= IP6T_RT_SGS;
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");
190 rtinfo->invflags |= IP6T_RT_INV_LEN;
191 rtinfo->flags |= IP6T_RT_LEN;
192 *flags |= IP6T_RT_LEN;
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;
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);
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;
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;
237 print_nums(const char *name, u_int32_t min, u_int32_t max,
240 const char *inv = invert ? "!" : "";
242 if (min != 0 || max != 0xFFFFFFFF || invert) {
258 print_addresses(unsigned int addrnr, struct in6_addr *addrp)
262 for(i=0; i<addrnr; i++){
263 printf("%s%c", addr_to_numeric(&(addrp[i])), (i!=addrnr-1)?',':' ');
267 /* Prints out the union ip6t_matchinfo. */
268 static void rt_print(const void *ip, const struct xt_entry_match *match,
271 const struct ip6t_rt *rtinfo = (struct ip6t_rt *)match->data;
274 if (rtinfo->flags & IP6T_RT_TYP)
275 printf("type:%s%d ", rtinfo->invflags & IP6T_RT_INV_TYP ? "!" : "",
277 print_nums("segsleft", rtinfo->segsleft[0], rtinfo->segsleft[1],
278 rtinfo->invflags & IP6T_RT_INV_SGS);
279 if (rtinfo->flags & IP6T_RT_LEN) {
281 printf(":%s", rtinfo->invflags & IP6T_RT_INV_LEN ? "!" : "");
282 printf("%u", rtinfo->hdrlen);
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);
294 /* Saves the union ip6t_matchinfo in parsable form to stdout. */
295 static void rt_save(const void *ip, const struct xt_entry_match *match)
297 const struct ip6t_rt *rtinfo = (struct ip6t_rt *)match->data;
299 if (rtinfo->flags & IP6T_RT_TYP) {
300 printf("--rt-type %s%u ",
301 (rtinfo->invflags & IP6T_RT_INV_TYP) ? "! " : "",
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])
313 rtinfo->segsleft[1]);
316 rtinfo->segsleft[0]);
319 if (rtinfo->flags & IP6T_RT_LEN) {
320 printf("--rt-len %s%u ",
321 (rtinfo->invflags & IP6T_RT_INV_LEN) ? "! " : "",
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 ");
332 static struct xtables_match rt_mt6_reg = {
334 .version = XTABLES_VERSION,
336 .size = XT_ALIGN(sizeof(struct ip6t_rt)),
337 .userspacesize = XT_ALIGN(sizeof(struct ip6t_rt)),
343 .extra_opts = rt_opts,
349 xtables_register_match(&rt_mt6_reg);