Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Kernel module to match ROUTING parameters. */ |
2 | ||
3 | /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | */ | |
9 | ||
10 | #include <linux/module.h> | |
11 | #include <linux/skbuff.h> | |
12 | #include <linux/ipv6.h> | |
13 | #include <linux/types.h> | |
14 | #include <net/checksum.h> | |
15 | #include <net/ipv6.h> | |
16 | ||
17 | #include <asm/byteorder.h> | |
18 | ||
19 | #include <linux/netfilter_ipv6/ip6_tables.h> | |
20 | #include <linux/netfilter_ipv6/ip6t_rt.h> | |
21 | ||
22 | MODULE_LICENSE("GPL"); | |
23 | MODULE_DESCRIPTION("IPv6 RT match"); | |
24 | MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>"); | |
25 | ||
26 | #if 0 | |
27 | #define DEBUGP printk | |
28 | #else | |
29 | #define DEBUGP(format, args...) | |
30 | #endif | |
31 | ||
32 | /* Returns 1 if the id is matched by the range, 0 otherwise */ | |
33 | static inline int | |
34 | segsleft_match(u_int32_t min, u_int32_t max, u_int32_t id, int invert) | |
35 | { | |
36 | int r=0; | |
37 | DEBUGP("rt segsleft_match:%c 0x%x <= 0x%x <= 0x%x",invert? '!':' ', | |
38 | min,id,max); | |
39 | r=(id >= min && id <= max) ^ invert; | |
40 | DEBUGP(" result %s\n",r? "PASS" : "FAILED"); | |
41 | return r; | |
42 | } | |
43 | ||
44 | static int | |
45 | match(const struct sk_buff *skb, | |
46 | const struct net_device *in, | |
47 | const struct net_device *out, | |
48 | const void *matchinfo, | |
49 | int offset, | |
50 | unsigned int protoff, | |
51 | int *hotdrop) | |
52 | { | |
53 | struct ipv6_rt_hdr _route, *rh = NULL; | |
54 | const struct ip6t_rt *rtinfo = matchinfo; | |
55 | unsigned int temp; | |
56 | unsigned int len; | |
57 | u8 nexthdr; | |
58 | unsigned int ptr; | |
59 | unsigned int hdrlen = 0; | |
60 | unsigned int ret = 0; | |
61 | struct in6_addr *ap, _addr; | |
62 | ||
63 | /* type of the 1st exthdr */ | |
64 | nexthdr = skb->nh.ipv6h->nexthdr; | |
65 | /* pointer to the 1st exthdr */ | |
66 | ptr = sizeof(struct ipv6hdr); | |
67 | /* available length */ | |
68 | len = skb->len - ptr; | |
69 | temp = 0; | |
70 | ||
71 | while (ip6t_ext_hdr(nexthdr)) { | |
72 | struct ipv6_opt_hdr _hdr, *hp; | |
73 | ||
74 | DEBUGP("ipv6_rt header iteration \n"); | |
75 | ||
76 | /* Is there enough space for the next ext header? */ | |
77 | if (len < (int)sizeof(struct ipv6_opt_hdr)) | |
78 | return 0; | |
79 | /* No more exthdr -> evaluate */ | |
80 | if (nexthdr == NEXTHDR_NONE) { | |
81 | break; | |
82 | } | |
83 | /* ESP -> evaluate */ | |
84 | if (nexthdr == NEXTHDR_ESP) { | |
85 | break; | |
86 | } | |
87 | ||
88 | hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr); | |
89 | BUG_ON(hp == NULL); | |
90 | ||
91 | /* Calculate the header length */ | |
92 | if (nexthdr == NEXTHDR_FRAGMENT) { | |
93 | hdrlen = 8; | |
94 | } else if (nexthdr == NEXTHDR_AUTH) | |
95 | hdrlen = (hp->hdrlen+2)<<2; | |
96 | else | |
97 | hdrlen = ipv6_optlen(hp); | |
98 | ||
99 | /* ROUTING -> evaluate */ | |
100 | if (nexthdr == NEXTHDR_ROUTING) { | |
101 | temp |= MASK_ROUTING; | |
102 | break; | |
103 | } | |
104 | ||
105 | ||
106 | /* set the flag */ | |
107 | switch (nexthdr){ | |
108 | case NEXTHDR_HOP: | |
109 | case NEXTHDR_ROUTING: | |
110 | case NEXTHDR_FRAGMENT: | |
111 | case NEXTHDR_AUTH: | |
112 | case NEXTHDR_DEST: | |
113 | break; | |
114 | default: | |
115 | DEBUGP("ipv6_rt match: unknown nextheader %u\n",nexthdr); | |
116 | return 0; | |
117 | break; | |
118 | } | |
119 | ||
120 | nexthdr = hp->nexthdr; | |
121 | len -= hdrlen; | |
122 | ptr += hdrlen; | |
123 | if ( ptr > skb->len ) { | |
124 | DEBUGP("ipv6_rt: new pointer is too large! \n"); | |
125 | break; | |
126 | } | |
127 | } | |
128 | ||
129 | /* ROUTING header not found */ | |
130 | if ( temp != MASK_ROUTING ) return 0; | |
131 | ||
132 | if (len < (int)sizeof(struct ipv6_rt_hdr)){ | |
133 | *hotdrop = 1; | |
134 | return 0; | |
135 | } | |
136 | ||
137 | if (len < hdrlen){ | |
138 | /* Pcket smaller than its length field */ | |
139 | return 0; | |
140 | } | |
141 | ||
142 | rh = skb_header_pointer(skb, ptr, sizeof(_route), &_route); | |
143 | BUG_ON(rh == NULL); | |
144 | ||
145 | DEBUGP("IPv6 RT LEN %u %u ", hdrlen, rh->hdrlen); | |
146 | DEBUGP("TYPE %04X ", rh->type); | |
147 | DEBUGP("SGS_LEFT %u %02X\n", rh->segments_left, rh->segments_left); | |
148 | ||
149 | DEBUGP("IPv6 RT segsleft %02X ", | |
150 | (segsleft_match(rtinfo->segsleft[0], rtinfo->segsleft[1], | |
151 | rh->segments_left, | |
152 | !!(rtinfo->invflags & IP6T_RT_INV_SGS)))); | |
153 | DEBUGP("type %02X %02X %02X ", | |
154 | rtinfo->rt_type, rh->type, | |
155 | (!(rtinfo->flags & IP6T_RT_TYP) || | |
156 | ((rtinfo->rt_type == rh->type) ^ | |
157 | !!(rtinfo->invflags & IP6T_RT_INV_TYP)))); | |
158 | DEBUGP("len %02X %04X %02X ", | |
159 | rtinfo->hdrlen, hdrlen, | |
160 | (!(rtinfo->flags & IP6T_RT_LEN) || | |
161 | ((rtinfo->hdrlen == hdrlen) ^ | |
162 | !!(rtinfo->invflags & IP6T_RT_INV_LEN)))); | |
163 | DEBUGP("res %02X %02X %02X ", | |
e6df439b BH |
164 | (rtinfo->flags & IP6T_RT_RES), ((struct rt0_hdr *)rh)->reserved, |
165 | !((rtinfo->flags & IP6T_RT_RES) && (((struct rt0_hdr *)rh)->reserved))); | |
1da177e4 LT |
166 | |
167 | ret = (rh != NULL) | |
168 | && | |
169 | (segsleft_match(rtinfo->segsleft[0], rtinfo->segsleft[1], | |
170 | rh->segments_left, | |
171 | !!(rtinfo->invflags & IP6T_RT_INV_SGS))) | |
172 | && | |
173 | (!(rtinfo->flags & IP6T_RT_LEN) || | |
174 | ((rtinfo->hdrlen == hdrlen) ^ | |
175 | !!(rtinfo->invflags & IP6T_RT_INV_LEN))) | |
176 | && | |
177 | (!(rtinfo->flags & IP6T_RT_TYP) || | |
178 | ((rtinfo->rt_type == rh->type) ^ | |
179 | !!(rtinfo->invflags & IP6T_RT_INV_TYP))); | |
180 | ||
181 | if (ret && (rtinfo->flags & IP6T_RT_RES)) { | |
e6df439b BH |
182 | u_int32_t *rp, _reserved; |
183 | rp = skb_header_pointer(skb, | |
184 | ptr + offsetof(struct rt0_hdr, reserved), | |
185 | sizeof(_reserved), &_reserved); | |
1da177e4 | 186 | |
e6df439b | 187 | ret = (*rp == 0); |
1da177e4 LT |
188 | } |
189 | ||
190 | DEBUGP("#%d ",rtinfo->addrnr); | |
191 | if ( !(rtinfo->flags & IP6T_RT_FST) ){ | |
192 | return ret; | |
193 | } else if (rtinfo->flags & IP6T_RT_FST_NSTRICT) { | |
194 | DEBUGP("Not strict "); | |
195 | if ( rtinfo->addrnr > (unsigned int)((hdrlen-8)/16) ){ | |
196 | DEBUGP("There isn't enough space\n"); | |
197 | return 0; | |
198 | } else { | |
199 | unsigned int i = 0; | |
200 | ||
201 | DEBUGP("#%d ",rtinfo->addrnr); | |
202 | for(temp=0; temp<(unsigned int)((hdrlen-8)/16); temp++){ | |
203 | ap = skb_header_pointer(skb, | |
204 | ptr | |
205 | + sizeof(struct rt0_hdr) | |
206 | + temp * sizeof(_addr), | |
207 | sizeof(_addr), | |
208 | &_addr); | |
209 | ||
210 | BUG_ON(ap == NULL); | |
211 | ||
212 | if (ipv6_addr_equal(ap, &rtinfo->addrs[i])) { | |
213 | DEBUGP("i=%d temp=%d;\n",i,temp); | |
214 | i++; | |
215 | } | |
216 | if (i==rtinfo->addrnr) break; | |
217 | } | |
218 | DEBUGP("i=%d #%d\n", i, rtinfo->addrnr); | |
219 | if (i == rtinfo->addrnr) | |
220 | return ret; | |
221 | else return 0; | |
222 | } | |
223 | } else { | |
224 | DEBUGP("Strict "); | |
225 | if ( rtinfo->addrnr > (unsigned int)((hdrlen-8)/16) ){ | |
226 | DEBUGP("There isn't enough space\n"); | |
227 | return 0; | |
228 | } else { | |
229 | DEBUGP("#%d ",rtinfo->addrnr); | |
230 | for(temp=0; temp<rtinfo->addrnr; temp++){ | |
231 | ap = skb_header_pointer(skb, | |
232 | ptr | |
233 | + sizeof(struct rt0_hdr) | |
234 | + temp * sizeof(_addr), | |
235 | sizeof(_addr), | |
236 | &_addr); | |
237 | BUG_ON(ap == NULL); | |
238 | ||
239 | if (!ipv6_addr_equal(ap, &rtinfo->addrs[temp])) | |
240 | break; | |
241 | } | |
242 | DEBUGP("temp=%d #%d\n", temp, rtinfo->addrnr); | |
243 | if ((temp == rtinfo->addrnr) && (temp == (unsigned int)((hdrlen-8)/16))) | |
244 | return ret; | |
245 | else return 0; | |
246 | } | |
247 | } | |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
252 | /* Called when user tries to insert an entry of this type. */ | |
253 | static int | |
254 | checkentry(const char *tablename, | |
255 | const struct ip6t_ip6 *ip, | |
256 | void *matchinfo, | |
257 | unsigned int matchinfosize, | |
258 | unsigned int hook_mask) | |
259 | { | |
260 | const struct ip6t_rt *rtinfo = matchinfo; | |
261 | ||
262 | if (matchinfosize != IP6T_ALIGN(sizeof(struct ip6t_rt))) { | |
263 | DEBUGP("ip6t_rt: matchsize %u != %u\n", | |
264 | matchinfosize, IP6T_ALIGN(sizeof(struct ip6t_rt))); | |
265 | return 0; | |
266 | } | |
267 | if (rtinfo->invflags & ~IP6T_RT_INV_MASK) { | |
268 | DEBUGP("ip6t_rt: unknown flags %X\n", | |
269 | rtinfo->invflags); | |
270 | return 0; | |
271 | } | |
272 | if ( (rtinfo->flags & (IP6T_RT_RES|IP6T_RT_FST_MASK)) && | |
273 | (!(rtinfo->flags & IP6T_RT_TYP) || | |
274 | (rtinfo->rt_type != 0) || | |
275 | (rtinfo->invflags & IP6T_RT_INV_TYP)) ) { | |
276 | DEBUGP("`--rt-type 0' required before `--rt-0-*'"); | |
277 | return 0; | |
278 | } | |
279 | ||
280 | return 1; | |
281 | } | |
282 | ||
283 | static struct ip6t_match rt_match = { | |
284 | .name = "rt", | |
285 | .match = &match, | |
286 | .checkentry = &checkentry, | |
287 | .me = THIS_MODULE, | |
288 | }; | |
289 | ||
290 | static int __init init(void) | |
291 | { | |
292 | return ip6t_register_match(&rt_match); | |
293 | } | |
294 | ||
295 | static void __exit cleanup(void) | |
296 | { | |
297 | ip6t_unregister_match(&rt_match); | |
298 | } | |
299 | ||
300 | module_init(init); | |
301 | module_exit(cleanup); |