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
skip_segsleft_match(uint32_t min,uint32_t max,bool inv)155 static bool skip_segsleft_match(uint32_t min, uint32_t max, bool inv)
156 {
157 return min == 0 && max == UINT32_MAX && !inv;
158 }
159
160 static void
print_nums(const char * name,uint32_t min,uint32_t max,int invert)161 print_nums(const char *name, uint32_t min, uint32_t max,
162 int invert)
163 {
164 const char *inv = invert ? "!" : "";
165
166 if (!skip_segsleft_match(min, max, invert)) {
167 printf(" %s", name);
168 if (min == max) {
169 printf(":%s", inv);
170 printf("%u", min);
171 } else {
172 printf("s:%s", inv);
173 printf("%u",min);
174 printf(":");
175 printf("%u",max);
176 }
177 }
178 }
179
180 static void
print_addresses(unsigned int addrnr,struct in6_addr * addrp)181 print_addresses(unsigned int addrnr, struct in6_addr *addrp)
182 {
183 unsigned int i;
184
185 for(i=0; i<addrnr; i++){
186 printf("%c%s", (i==0)?' ':',', addr_to_numeric(&(addrp[i])));
187 }
188 }
189
rt_print(const void * ip,const struct xt_entry_match * match,int numeric)190 static void rt_print(const void *ip, const struct xt_entry_match *match,
191 int numeric)
192 {
193 const struct ip6t_rt *rtinfo = (struct ip6t_rt *)match->data;
194
195 printf(" rt");
196 if (rtinfo->flags & IP6T_RT_TYP)
197 printf(" type:%s%d", rtinfo->invflags & IP6T_RT_INV_TYP ? "!" : "",
198 rtinfo->rt_type);
199 print_nums("segsleft", rtinfo->segsleft[0], rtinfo->segsleft[1],
200 rtinfo->invflags & IP6T_RT_INV_SGS);
201 if (rtinfo->flags & IP6T_RT_LEN) {
202 printf(" length");
203 printf(":%s", rtinfo->invflags & IP6T_RT_INV_LEN ? "!" : "");
204 printf("%u", rtinfo->hdrlen);
205 }
206 if (rtinfo->flags & IP6T_RT_RES) printf(" reserved");
207 if (rtinfo->flags & IP6T_RT_FST) printf(" 0-addrs");
208 print_addresses(rtinfo->addrnr, (struct in6_addr *)rtinfo->addrs);
209 if (rtinfo->flags & IP6T_RT_FST_NSTRICT) printf(" 0-not-strict");
210 if (rtinfo->invflags & ~IP6T_RT_INV_MASK)
211 printf(" Unknown invflags: 0x%X",
212 rtinfo->invflags & ~IP6T_RT_INV_MASK);
213 }
214
rt_save(const void * ip,const struct xt_entry_match * match)215 static void rt_save(const void *ip, const struct xt_entry_match *match)
216 {
217 const struct ip6t_rt *rtinfo = (struct ip6t_rt *)match->data;
218 bool inv_sgs = rtinfo->invflags & IP6T_RT_INV_SGS;
219
220 if (rtinfo->flags & IP6T_RT_TYP) {
221 printf("%s --rt-type %u",
222 (rtinfo->invflags & IP6T_RT_INV_TYP) ? " !" : "",
223 rtinfo->rt_type);
224 }
225
226 if (!skip_segsleft_match(rtinfo->segsleft[0],
227 rtinfo->segsleft[1], inv_sgs)) {
228 printf("%s --rt-segsleft ", inv_sgs ? " !" : "");
229 if (rtinfo->segsleft[0]
230 != rtinfo->segsleft[1])
231 printf("%u:%u",
232 rtinfo->segsleft[0],
233 rtinfo->segsleft[1]);
234 else
235 printf("%u",
236 rtinfo->segsleft[0]);
237 }
238
239 if (rtinfo->flags & IP6T_RT_LEN) {
240 printf("%s --rt-len %u",
241 (rtinfo->invflags & IP6T_RT_INV_LEN) ? " !" : "",
242 rtinfo->hdrlen);
243 }
244
245 if (rtinfo->flags & IP6T_RT_RES) printf(" --rt-0-res");
246 if (rtinfo->flags & IP6T_RT_FST) printf(" --rt-0-addrs");
247 print_addresses(rtinfo->addrnr, (struct in6_addr *)rtinfo->addrs);
248 if (rtinfo->flags & IP6T_RT_FST_NSTRICT) printf(" --rt-0-not-strict");
249
250 }
251
252 #define XLATE_FLAGS (IP6T_RT_TYP | IP6T_RT_LEN | \
253 IP6T_RT_RES | IP6T_RT_FST | IP6T_RT_FST_NSTRICT)
254
rt_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)255 static int rt_xlate(struct xt_xlate *xl,
256 const struct xt_xlate_mt_params *params)
257 {
258 const struct ip6t_rt *rtinfo = (struct ip6t_rt *)params->match->data;
259 bool inv_sgs = rtinfo->invflags & IP6T_RT_INV_SGS;
260
261 if (rtinfo->flags & IP6T_RT_TYP) {
262 xt_xlate_add(xl, "rt type%s %u",
263 (rtinfo->invflags & IP6T_RT_INV_TYP) ? " !=" : "",
264 rtinfo->rt_type);
265 }
266
267 if (!skip_segsleft_match(rtinfo->segsleft[0],
268 rtinfo->segsleft[1], inv_sgs)) {
269 xt_xlate_add(xl, "rt seg-left%s ", inv_sgs ? " !=" : "");
270
271 if (rtinfo->segsleft[0] != rtinfo->segsleft[1])
272 xt_xlate_add(xl, "%u-%u", rtinfo->segsleft[0],
273 rtinfo->segsleft[1]);
274 else
275 xt_xlate_add(xl, "%u", rtinfo->segsleft[0]);
276 } else if (!(rtinfo->flags & XLATE_FLAGS)) {
277 xt_xlate_add(xl, "exthdr rt exists");
278 return 1;
279 }
280
281 if (rtinfo->flags & IP6T_RT_LEN) {
282 xt_xlate_add(xl, "rt hdrlength%s %u",
283 (rtinfo->invflags & IP6T_RT_INV_LEN) ? " !=" : "",
284 rtinfo->hdrlen);
285 }
286
287 if (rtinfo->flags & (IP6T_RT_RES | IP6T_RT_FST | IP6T_RT_FST_NSTRICT))
288 return 0;
289
290 return 1;
291 }
292
293 static struct xtables_match rt_mt6_reg = {
294 .name = "rt",
295 .version = XTABLES_VERSION,
296 .family = NFPROTO_IPV6,
297 .size = XT_ALIGN(sizeof(struct ip6t_rt)),
298 .userspacesize = XT_ALIGN(sizeof(struct ip6t_rt)),
299 .help = rt_help,
300 .init = rt_init,
301 .x6_parse = rt_parse,
302 .print = rt_print,
303 .save = rt_save,
304 .x6_options = rt_opts,
305 .xlate = rt_xlate,
306 };
307
308 void
_init(void)309 _init(void)
310 {
311 xtables_register_match(&rt_mt6_reg);
312 }
313