5 #include <linux/netfilter_ipv6/ip6t_rt.h>
15 F_RT_TYPE = 1 << O_RT_TYPE,
16 F_RT0ADDRS = 1 << O_RT0ADDRS,
19 static void rt_help(void)
23 "[!] --rt-type type match the type\n"
24 "[!] --rt-segsleft num[:num] match the Segments Left field (range)\n"
25 "[!] --rt-len length total length of this header\n"
26 " --rt-0-res check the reserved field too (type 0)\n"
27 " --rt-0-addrs ADDR[,ADDR...] Type=0 addresses (list, max: %d)\n"
28 " --rt-0-not-strict List of Type=0 addresses not a strict list\n",
32 #define s struct ip6t_rt
33 static const struct xt_option_entry rt_opts[] = {
34 {.name = "rt-type", .id = O_RT_TYPE, .type = XTTYPE_UINT32,
35 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, rt_type)},
36 {.name = "rt-segsleft", .id = O_RT_SEGSLEFT, .type = XTTYPE_UINT32RC,
37 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, segsleft)},
38 {.name = "rt-len", .id = O_RT_LEN, .type = XTTYPE_UINT32,
39 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, hdrlen)},
40 {.name = "rt-0-res", .id = O_RT0RES, .type = XTTYPE_NONE},
41 {.name = "rt-0-addrs", .id = O_RT0ADDRS, .type = XTTYPE_STRING},
42 {.name = "rt-0-not-strict", .id = O_RT0NSTRICT, .type = XTTYPE_NONE},
48 addr_to_numeric(const struct in6_addr *addrp)
50 static char buf[50+1];
51 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
54 static struct in6_addr *
55 numeric_to_addr(const char *num)
57 static struct in6_addr ap;
60 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
63 fprintf(stderr, "\nnumeric2addr: %d\n", err);
65 xtables_error(PARAMETER_PROBLEM, "bad address: %s", num);
67 return (struct in6_addr *)NULL;
72 parse_addresses(const char *addrstr, struct in6_addr *addrp)
74 char *buffer, *cp, *next;
77 buffer = strdup(addrstr);
78 if (!buffer) xtables_error(OTHER_PROBLEM, "strdup failed");
80 for (cp=buffer, i=0; cp && i<IP6T_RT_HOPS; cp=next,i++)
83 if (next) *next++='\0';
84 memcpy(&(addrp[i]), numeric_to_addr(cp), sizeof(struct in6_addr));
86 printf("addr str: %s\n", cp);
87 printf("addr ip6: %s\n", addr_to_numeric((numeric_to_addr(cp))));
88 printf("addr [%d]: %s\n", i, addr_to_numeric(&(addrp[i])));
91 if (cp) xtables_error(PARAMETER_PROBLEM, "too many addresses specified");
96 printf("addr nr: %d\n", i);
102 static void rt_parse(struct xt_option_call *cb)
104 struct ip6t_rt *rtinfo = cb->data;
106 xtables_option_parse(cb);
107 switch (cb->entry->id) {
110 rtinfo->invflags |= IP6T_RT_INV_TYP;
111 rtinfo->flags |= IP6T_RT_TYP;
115 rtinfo->segsleft[1] = rtinfo->segsleft[0];
117 rtinfo->invflags |= IP6T_RT_INV_SGS;
118 rtinfo->flags |= IP6T_RT_SGS;
122 rtinfo->invflags |= IP6T_RT_INV_LEN;
123 rtinfo->flags |= IP6T_RT_LEN;
126 if (!(cb->xflags & F_RT_TYPE) || rtinfo->rt_type != 0 ||
127 rtinfo->invflags & IP6T_RT_INV_TYP)
128 xtables_error(PARAMETER_PROBLEM,
129 "`--rt-type 0' required before `--rt-0-res'");
130 rtinfo->flags |= IP6T_RT_RES;
133 if (!(cb->xflags & F_RT_TYPE) || rtinfo->rt_type != 0 ||
134 rtinfo->invflags & IP6T_RT_INV_TYP)
135 xtables_error(PARAMETER_PROBLEM,
136 "`--rt-type 0' required before `--rt-0-addrs'");
137 rtinfo->addrnr = parse_addresses(cb->arg, rtinfo->addrs);
138 rtinfo->flags |= IP6T_RT_FST;
141 if (!(cb->xflags & F_RT0ADDRS))
142 xtables_error(PARAMETER_PROBLEM,
143 "`--rt-0-addr ...' required before `--rt-0-not-strict'");
144 rtinfo->flags |= IP6T_RT_FST_NSTRICT;
150 print_nums(const char *name, uint32_t min, uint32_t max,
153 const char *inv = invert ? "!" : "";
155 if (min != 0 || max != 0xFFFFFFFF || invert) {
170 print_addresses(unsigned int addrnr, struct in6_addr *addrp)
174 for(i=0; i<addrnr; i++){
175 printf("%c%s", (i==0)?' ':',', addr_to_numeric(&(addrp[i])));
179 static void rt_print(const void *ip, const struct xt_entry_match *match,
182 const struct ip6t_rt *rtinfo = (struct ip6t_rt *)match->data;
185 if (rtinfo->flags & IP6T_RT_TYP)
186 printf(" type:%s%d", rtinfo->invflags & IP6T_RT_INV_TYP ? "!" : "",
188 print_nums("segsleft", rtinfo->segsleft[0], rtinfo->segsleft[1],
189 rtinfo->invflags & IP6T_RT_INV_SGS);
190 if (rtinfo->flags & IP6T_RT_LEN) {
192 printf(":%s", rtinfo->invflags & IP6T_RT_INV_LEN ? "!" : "");
193 printf("%u", rtinfo->hdrlen);
195 if (rtinfo->flags & IP6T_RT_RES) printf(" reserved");
196 if (rtinfo->flags & IP6T_RT_FST) printf(" 0-addrs");
197 print_addresses(rtinfo->addrnr, (struct in6_addr *)rtinfo->addrs);
198 if (rtinfo->flags & IP6T_RT_FST_NSTRICT) printf(" 0-not-strict");
199 if (rtinfo->invflags & ~IP6T_RT_INV_MASK)
200 printf(" Unknown invflags: 0x%X",
201 rtinfo->invflags & ~IP6T_RT_INV_MASK);
204 static void rt_save(const void *ip, const struct xt_entry_match *match)
206 const struct ip6t_rt *rtinfo = (struct ip6t_rt *)match->data;
208 if (rtinfo->flags & IP6T_RT_TYP) {
209 printf("%s --rt-type %u",
210 (rtinfo->invflags & IP6T_RT_INV_TYP) ? " !" : "",
214 if (!(rtinfo->segsleft[0] == 0
215 && rtinfo->segsleft[1] == 0xFFFFFFFF)) {
216 printf("%s --rt-segsleft ",
217 (rtinfo->invflags & IP6T_RT_INV_SGS) ? " !" : "");
218 if (rtinfo->segsleft[0]
219 != rtinfo->segsleft[1])
222 rtinfo->segsleft[1]);
225 rtinfo->segsleft[0]);
228 if (rtinfo->flags & IP6T_RT_LEN) {
229 printf("%s --rt-len %u",
230 (rtinfo->invflags & IP6T_RT_INV_LEN) ? " !" : "",
234 if (rtinfo->flags & IP6T_RT_RES) printf(" --rt-0-res");
235 if (rtinfo->flags & IP6T_RT_FST) printf(" --rt-0-addrs");
236 print_addresses(rtinfo->addrnr, (struct in6_addr *)rtinfo->addrs);
237 if (rtinfo->flags & IP6T_RT_FST_NSTRICT) printf(" --rt-0-not-strict");
241 static struct xtables_match rt_mt6_reg = {
243 .version = XTABLES_VERSION,
244 .family = NFPROTO_IPV6,
245 .size = XT_ALIGN(sizeof(struct ip6t_rt)),
246 .userspacesize = XT_ALIGN(sizeof(struct ip6t_rt)),
248 .x6_parse = rt_parse,
251 .x6_options = rt_opts,
257 xtables_register_match(&rt_mt6_reg);