1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <xtables.h>
5 #include <linux/netfilter_ipv6/ip6t_rt.h>
6 #include <arpa/inet.h>
7
8 enum {
9 O_RT_TYPE = 0,
10 O_RT_SEGSLEFT,
11 O_RT_LEN,
12 O_RT0RES,
13 O_RT0ADDRS,
14 O_RT0NSTRICT,
15 F_RT_TYPE = 1 << O_RT_TYPE,
16 F_RT0ADDRS = 1 << O_RT0ADDRS,
17 };
18
rt_help(void)19 static void rt_help(void)
20 {
21 printf(
22 "rt match options:\n"
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",
29 IP6T_RT_HOPS);
30 }
31
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},
43 XTOPT_TABLEEND,
44 };
45 #undef s
46
47 static const char *
addr_to_numeric(const struct in6_addr * addrp)48 addr_to_numeric(const struct in6_addr *addrp)
49 {
50 static char buf[50+1];
51 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
52 }
53
54 static struct in6_addr *
numeric_to_addr(const char * num)55 numeric_to_addr(const char *num)
56 {
57 static struct in6_addr ap;
58 int err;
59
60 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
61 return ≈
62 #ifdef DEBUG
63 fprintf(stderr, "\nnumeric2addr: %d\n", err);
64 #endif
65 xtables_error(PARAMETER_PROBLEM, "bad address: %s", num);
66
67 return (struct in6_addr *)NULL;
68 }
69
70
71 static int
parse_addresses(const char * addrstr,struct in6_addr * addrp)72 parse_addresses(const char *addrstr, struct in6_addr *addrp)
73 {
74 char *buffer, *cp, *next;
75 unsigned int i;
76
77 buffer = xtables_strdup(addrstr);
78
79 for (cp=buffer, i=0; cp && i<IP6T_RT_HOPS; cp=next,i++)
80 {
81 next=strchr(cp, ',');
82 if (next) *next++='\0';
83 memcpy(&(addrp[i]), numeric_to_addr(cp), sizeof(struct in6_addr));
84 #if DEBUG
85 printf("addr str: %s\n", cp);
86 printf("addr ip6: %s\n", addr_to_numeric((numeric_to_addr(cp))));
87 printf("addr [%d]: %s\n", i, addr_to_numeric(&(addrp[i])));
88 #endif
89 }
90 if (cp) xtables_error(PARAMETER_PROBLEM, "too many addresses specified");
91
92 free(buffer);
93
94 #if DEBUG
95 printf("addr nr: %d\n", i);
96 #endif
97
98 return i;
99 }
100
rt_init(struct xt_entry_match * m)101 static void rt_init(struct xt_entry_match *m)
102 {
103 struct ip6t_rt *rtinfo = (void *)m->data;
104
105 rtinfo->segsleft[1] = ~0U;
106 }
107
rt_parse(struct xt_option_call * cb)108 static void rt_parse(struct xt_option_call *cb)
109 {
110 struct ip6t_rt *rtinfo = cb->data;
111
112 xtables_option_parse(cb);
113 switch (cb->entry->id) {
114 case O_RT_TYPE:
115 if (cb->invert)
116 rtinfo->invflags |= IP6T_RT_INV_TYP;
117 rtinfo->flags |= IP6T_RT_TYP;
118 break;
119 case O_RT_SEGSLEFT:
120 if (cb->nvals == 1)
121 rtinfo->segsleft[1] = rtinfo->segsleft[0];
122 if (cb->invert)
123 rtinfo->invflags |= IP6T_RT_INV_SGS;
124 rtinfo->flags |= IP6T_RT_SGS;
125 break;
126 case O_RT_LEN:
127 if (cb->invert)
128 rtinfo->invflags |= IP6T_RT_INV_LEN;
129 rtinfo->flags |= IP6T_RT_LEN;
130 break;
131 case O_RT0RES:
132 if (!(cb->xflags & F_RT_TYPE) || rtinfo->rt_type != 0 ||
133 rtinfo->invflags & IP6T_RT_INV_TYP)
134 xtables_error(PARAMETER_PROBLEM,
135 "`--rt-type 0' required before `--rt-0-res'");
136 rtinfo->flags |= IP6T_RT_RES;
137 break;
138 case O_RT0ADDRS:
139 if (!(cb->xflags & F_RT_TYPE) || rtinfo->rt_type != 0 ||
140 rtinfo->invflags & IP6T_RT_INV_TYP)
141 xtables_error(PARAMETER_PROBLEM,
142 "`--rt-type 0' required before `--rt-0-addrs'");
143 rtinfo->addrnr = parse_addresses(cb->arg, rtinfo->addrs);
144 rtinfo->flags |= IP6T_RT_FST;
145 break;
146 case O_RT0NSTRICT:
147 if (!(cb->xflags & F_RT0ADDRS))
148 xtables_error(PARAMETER_PROBLEM,
149 "`--rt-0-addr ...' required before `--rt-0-not-strict'");
150 rtinfo->flags |= IP6T_RT_FST_NSTRICT;
151 break;
152 }
153 }
154
155 static void
print_nums(const char * name,uint32_t min,uint32_t max,int invert)156 print_nums(const char *name, uint32_t min, uint32_t max,
157 int invert)
158 {
159 const char *inv = invert ? "!" : "";
160
161 if (min != 0 || max != 0xFFFFFFFF || invert) {
162 printf(" %s", name);
163 if (min == max) {
164 printf(":%s", inv);
165 printf("%u", min);
166 } else {
167 printf("s:%s", inv);
168 printf("%u",min);
169 printf(":");
170 printf("%u",max);
171 }
172 }
173 }
174
175 static void
print_addresses(unsigned int addrnr,struct in6_addr * addrp)176 print_addresses(unsigned int addrnr, struct in6_addr *addrp)
177 {
178 unsigned int i;
179
180 for(i=0; i<addrnr; i++){
181 printf("%c%s", (i==0)?' ':',', addr_to_numeric(&(addrp[i])));
182 }
183 }
184
rt_print(const void * ip,const struct xt_entry_match * match,int numeric)185 static void rt_print(const void *ip, const struct xt_entry_match *match,
186 int numeric)
187 {
188 const struct ip6t_rt *rtinfo = (struct ip6t_rt *)match->data;
189
190 printf(" rt");
191 if (rtinfo->flags & IP6T_RT_TYP)
192 printf(" type:%s%d", rtinfo->invflags & IP6T_RT_INV_TYP ? "!" : "",
193 rtinfo->rt_type);
194 print_nums("segsleft", rtinfo->segsleft[0], rtinfo->segsleft[1],
195 rtinfo->invflags & IP6T_RT_INV_SGS);
196 if (rtinfo->flags & IP6T_RT_LEN) {
197 printf(" length");
198 printf(":%s", rtinfo->invflags & IP6T_RT_INV_LEN ? "!" : "");
199 printf("%u", rtinfo->hdrlen);
200 }
201 if (rtinfo->flags & IP6T_RT_RES) printf(" reserved");
202 if (rtinfo->flags & IP6T_RT_FST) printf(" 0-addrs");
203 print_addresses(rtinfo->addrnr, (struct in6_addr *)rtinfo->addrs);
204 if (rtinfo->flags & IP6T_RT_FST_NSTRICT) printf(" 0-not-strict");
205 if (rtinfo->invflags & ~IP6T_RT_INV_MASK)
206 printf(" Unknown invflags: 0x%X",
207 rtinfo->invflags & ~IP6T_RT_INV_MASK);
208 }
209
rt_save(const void * ip,const struct xt_entry_match * match)210 static void rt_save(const void *ip, const struct xt_entry_match *match)
211 {
212 const struct ip6t_rt *rtinfo = (struct ip6t_rt *)match->data;
213
214 if (rtinfo->flags & IP6T_RT_TYP) {
215 printf("%s --rt-type %u",
216 (rtinfo->invflags & IP6T_RT_INV_TYP) ? " !" : "",
217 rtinfo->rt_type);
218 }
219
220 if (!(rtinfo->segsleft[0] == 0
221 && rtinfo->segsleft[1] == 0xFFFFFFFF)) {
222 printf("%s --rt-segsleft ",
223 (rtinfo->invflags & IP6T_RT_INV_SGS) ? " !" : "");
224 if (rtinfo->segsleft[0]
225 != rtinfo->segsleft[1])
226 printf("%u:%u",
227 rtinfo->segsleft[0],
228 rtinfo->segsleft[1]);
229 else
230 printf("%u",
231 rtinfo->segsleft[0]);
232 }
233
234 if (rtinfo->flags & IP6T_RT_LEN) {
235 printf("%s --rt-len %u",
236 (rtinfo->invflags & IP6T_RT_INV_LEN) ? " !" : "",
237 rtinfo->hdrlen);
238 }
239
240 if (rtinfo->flags & IP6T_RT_RES) printf(" --rt-0-res");
241 if (rtinfo->flags & IP6T_RT_FST) printf(" --rt-0-addrs");
242 print_addresses(rtinfo->addrnr, (struct in6_addr *)rtinfo->addrs);
243 if (rtinfo->flags & IP6T_RT_FST_NSTRICT) printf(" --rt-0-not-strict");
244
245 }
246
rt_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)247 static int rt_xlate(struct xt_xlate *xl,
248 const struct xt_xlate_mt_params *params)
249 {
250 const struct ip6t_rt *rtinfo = (struct ip6t_rt *)params->match->data;
251
252 if (rtinfo->flags & IP6T_RT_TYP) {
253 xt_xlate_add(xl, "rt type%s %u",
254 (rtinfo->invflags & IP6T_RT_INV_TYP) ? " !=" : "",
255 rtinfo->rt_type);
256 }
257
258 if (!(rtinfo->segsleft[0] == 0 && rtinfo->segsleft[1] == 0xFFFFFFFF)) {
259 xt_xlate_add(xl, "rt seg-left%s ",
260 (rtinfo->invflags & IP6T_RT_INV_SGS) ? " !=" : "");
261
262 if (rtinfo->segsleft[0] != rtinfo->segsleft[1])
263 xt_xlate_add(xl, "%u-%u", rtinfo->segsleft[0],
264 rtinfo->segsleft[1]);
265 else
266 xt_xlate_add(xl, "%u", rtinfo->segsleft[0]);
267 }
268
269 if (rtinfo->flags & IP6T_RT_LEN) {
270 xt_xlate_add(xl, "rt hdrlength%s %u",
271 (rtinfo->invflags & IP6T_RT_INV_LEN) ? " !=" : "",
272 rtinfo->hdrlen);
273 }
274
275 if (rtinfo->flags & (IP6T_RT_RES | IP6T_RT_FST | IP6T_RT_FST_NSTRICT))
276 return 0;
277
278 return 1;
279 }
280
281 static struct xtables_match rt_mt6_reg = {
282 .name = "rt",
283 .version = XTABLES_VERSION,
284 .family = NFPROTO_IPV6,
285 .size = XT_ALIGN(sizeof(struct ip6t_rt)),
286 .userspacesize = XT_ALIGN(sizeof(struct ip6t_rt)),
287 .help = rt_help,
288 .init = rt_init,
289 .x6_parse = rt_parse,
290 .print = rt_print,
291 .save = rt_save,
292 .x6_options = rt_opts,
293 .xlate = rt_xlate,
294 };
295
296 void
_init(void)297 _init(void)
298 {
299 xtables_register_match(&rt_mt6_reg);
300 }
301