Commit | Line | Data |
---|---|---|
2c8d7ca0 NT |
1 | /* |
2 | * Copyright (C)2003-2006 Helsinki University of Technology | |
3 | * Copyright (C)2003-2006 USAGI/WIDE Project | |
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 as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | */ | |
19 | /* | |
20 | * Authors: | |
21 | * Noriaki TAKAMIYA @USAGI | |
22 | * Masahide NAKAMURA @USAGI | |
23 | */ | |
24 | ||
2c8d7ca0 NT |
25 | #include <linux/module.h> |
26 | #include <linux/skbuff.h> | |
70182ed2 | 27 | #include <linux/time.h> |
2c8d7ca0 | 28 | #include <linux/ipv6.h> |
7be96f76 MN |
29 | #include <linux/icmpv6.h> |
30 | #include <net/sock.h> | |
2c8d7ca0 | 31 | #include <net/ipv6.h> |
7be96f76 | 32 | #include <net/ip6_checksum.h> |
2c8d7ca0 NT |
33 | #include <net/xfrm.h> |
34 | #include <net/mip6.h> | |
35 | ||
36 | static xfrm_address_t *mip6_xfrm_addr(struct xfrm_state *x, xfrm_address_t *addr) | |
37 | { | |
38 | return x->coaddr; | |
39 | } | |
40 | ||
3d126890 NT |
41 | static inline unsigned int calc_padlen(unsigned int len, unsigned int n) |
42 | { | |
43 | return (n - len + 16) & 0x7; | |
44 | } | |
45 | ||
46 | static inline void *mip6_padn(__u8 *data, __u8 padlen) | |
47 | { | |
48 | if (!data) | |
49 | return NULL; | |
50 | if (padlen == 1) { | |
51 | data[0] = MIP6_OPT_PAD_1; | |
52 | } else if (padlen > 1) { | |
53 | data[0] = MIP6_OPT_PAD_N; | |
54 | data[1] = padlen - 2; | |
55 | if (padlen > 2) | |
56 | memset(data+2, 0, data[1]); | |
57 | } | |
58 | return data + padlen; | |
59 | } | |
60 | ||
7be96f76 MN |
61 | static inline void mip6_param_prob(struct sk_buff *skb, int code, int pos) |
62 | { | |
63 | icmpv6_send(skb, ICMPV6_PARAMPROB, code, pos, skb->dev); | |
64 | } | |
65 | ||
66 | static int mip6_mh_len(int type) | |
67 | { | |
68 | int len = 0; | |
69 | ||
70 | switch (type) { | |
71 | case IP6_MH_TYPE_BRR: | |
72 | len = 0; | |
73 | break; | |
74 | case IP6_MH_TYPE_HOTI: | |
75 | case IP6_MH_TYPE_COTI: | |
76 | case IP6_MH_TYPE_BU: | |
77 | case IP6_MH_TYPE_BACK: | |
78 | len = 1; | |
79 | break; | |
80 | case IP6_MH_TYPE_HOT: | |
81 | case IP6_MH_TYPE_COT: | |
82 | case IP6_MH_TYPE_BERROR: | |
83 | len = 2; | |
84 | break; | |
85 | } | |
86 | return len; | |
87 | } | |
88 | ||
89 | int mip6_mh_filter(struct sock *sk, struct sk_buff *skb) | |
90 | { | |
91 | struct ip6_mh *mh; | |
7be96f76 MN |
92 | |
93 | if (!pskb_may_pull(skb, (skb->h.raw - skb->data) + 8) || | |
94 | !pskb_may_pull(skb, (skb->h.raw - skb->data) + ((skb->h.raw[1] + 1) << 3))) | |
95 | return -1; | |
96 | ||
97 | mh = (struct ip6_mh *)skb->h.raw; | |
98 | ||
99 | if (mh->ip6mh_hdrlen < mip6_mh_len(mh->ip6mh_type)) { | |
100 | LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH message too short: %d vs >=%d\n", | |
101 | mh->ip6mh_hdrlen, mip6_mh_len(mh->ip6mh_type)); | |
d56f90a7 ACM |
102 | mip6_param_prob(skb, 0, ((&mh->ip6mh_hdrlen) - |
103 | skb_network_header(skb))); | |
7be96f76 MN |
104 | return -1; |
105 | } | |
7be96f76 MN |
106 | |
107 | if (mh->ip6mh_proto != IPPROTO_NONE) { | |
108 | LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH invalid payload proto = %d\n", | |
109 | mh->ip6mh_proto); | |
d56f90a7 ACM |
110 | mip6_param_prob(skb, 0, ((&mh->ip6mh_proto) - |
111 | skb_network_header(skb))); | |
7be96f76 MN |
112 | return -1; |
113 | } | |
114 | ||
115 | return 0; | |
116 | } | |
117 | ||
70182ed2 MN |
118 | struct mip6_report_rate_limiter { |
119 | spinlock_t lock; | |
120 | struct timeval stamp; | |
121 | int iif; | |
122 | struct in6_addr src; | |
123 | struct in6_addr dst; | |
124 | }; | |
125 | ||
126 | static struct mip6_report_rate_limiter mip6_report_rl = { | |
127 | .lock = SPIN_LOCK_UNLOCKED | |
128 | }; | |
129 | ||
3d126890 NT |
130 | static int mip6_destopt_input(struct xfrm_state *x, struct sk_buff *skb) |
131 | { | |
0660e03f | 132 | struct ipv6hdr *iph = ipv6_hdr(skb); |
3d126890 NT |
133 | struct ipv6_destopt_hdr *destopt = (struct ipv6_destopt_hdr *)skb->data; |
134 | ||
135 | if (!ipv6_addr_equal(&iph->saddr, (struct in6_addr *)x->coaddr) && | |
136 | !ipv6_addr_any((struct in6_addr *)x->coaddr)) | |
137 | return -ENOENT; | |
138 | ||
139 | return destopt->nexthdr; | |
140 | } | |
141 | ||
142 | /* Destination Option Header is inserted. | |
143 | * IP Header's src address is replaced with Home Address Option in | |
144 | * Destination Option Header. | |
145 | */ | |
146 | static int mip6_destopt_output(struct xfrm_state *x, struct sk_buff *skb) | |
147 | { | |
148 | struct ipv6hdr *iph; | |
149 | struct ipv6_destopt_hdr *dstopt; | |
150 | struct ipv6_destopt_hao *hao; | |
151 | u8 nexthdr; | |
152 | int len; | |
153 | ||
154 | iph = (struct ipv6hdr *)skb->data; | |
155 | iph->payload_len = htons(skb->len - sizeof(*iph)); | |
156 | ||
d56f90a7 ACM |
157 | nexthdr = *skb_network_header(skb); |
158 | *skb_network_header(skb) = IPPROTO_DSTOPTS; | |
3d126890 NT |
159 | |
160 | dstopt = (struct ipv6_destopt_hdr *)skb->h.raw; | |
161 | dstopt->nexthdr = nexthdr; | |
162 | ||
163 | hao = mip6_padn((char *)(dstopt + 1), | |
164 | calc_padlen(sizeof(*dstopt), 6)); | |
165 | ||
166 | hao->type = IPV6_TLV_HAO; | |
167 | hao->length = sizeof(*hao) - 2; | |
168 | BUG_TRAP(hao->length == 16); | |
169 | ||
170 | len = ((char *)hao - (char *)dstopt) + sizeof(*hao); | |
171 | ||
172 | memcpy(&hao->addr, &iph->saddr, sizeof(hao->addr)); | |
173 | memcpy(&iph->saddr, x->coaddr, sizeof(iph->saddr)); | |
174 | ||
175 | BUG_TRAP(len == x->props.header_len); | |
176 | dstopt->hdrlen = (x->props.header_len >> 3) - 1; | |
177 | ||
178 | return 0; | |
179 | } | |
180 | ||
70182ed2 MN |
181 | static inline int mip6_report_rl_allow(struct timeval *stamp, |
182 | struct in6_addr *dst, | |
183 | struct in6_addr *src, int iif) | |
184 | { | |
185 | int allow = 0; | |
186 | ||
187 | spin_lock_bh(&mip6_report_rl.lock); | |
188 | if (mip6_report_rl.stamp.tv_sec != stamp->tv_sec || | |
189 | mip6_report_rl.stamp.tv_usec != stamp->tv_usec || | |
190 | mip6_report_rl.iif != iif || | |
191 | !ipv6_addr_equal(&mip6_report_rl.src, src) || | |
192 | !ipv6_addr_equal(&mip6_report_rl.dst, dst)) { | |
193 | mip6_report_rl.stamp.tv_sec = stamp->tv_sec; | |
194 | mip6_report_rl.stamp.tv_usec = stamp->tv_usec; | |
195 | mip6_report_rl.iif = iif; | |
196 | ipv6_addr_copy(&mip6_report_rl.src, src); | |
197 | ipv6_addr_copy(&mip6_report_rl.dst, dst); | |
198 | allow = 1; | |
199 | } | |
200 | spin_unlock_bh(&mip6_report_rl.lock); | |
201 | return allow; | |
202 | } | |
203 | ||
204 | static int mip6_destopt_reject(struct xfrm_state *x, struct sk_buff *skb, struct flowi *fl) | |
205 | { | |
206 | struct inet6_skb_parm *opt = (struct inet6_skb_parm *)skb->cb; | |
207 | struct ipv6_destopt_hao *hao = NULL; | |
208 | struct xfrm_selector sel; | |
209 | int offset; | |
210 | struct timeval stamp; | |
211 | int err = 0; | |
212 | ||
e731c248 YH |
213 | if (unlikely(fl->proto == IPPROTO_MH && |
214 | fl->fl_mh_type <= IP6_MH_TYPE_MAX)) | |
01be8e5d MN |
215 | goto out; |
216 | ||
70182ed2 MN |
217 | if (likely(opt->dsthao)) { |
218 | offset = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO); | |
219 | if (likely(offset >= 0)) | |
d56f90a7 ACM |
220 | hao = (struct ipv6_destopt_hao *) |
221 | (skb_network_header(skb) + offset); | |
70182ed2 MN |
222 | } |
223 | ||
224 | skb_get_timestamp(skb, &stamp); | |
225 | ||
0660e03f ACM |
226 | if (!mip6_report_rl_allow(&stamp, &ipv6_hdr(skb)->daddr, |
227 | hao ? &hao->addr : &ipv6_hdr(skb)->saddr, | |
70182ed2 MN |
228 | opt->iif)) |
229 | goto out; | |
230 | ||
231 | memset(&sel, 0, sizeof(sel)); | |
0660e03f | 232 | memcpy(&sel.daddr, (xfrm_address_t *)&ipv6_hdr(skb)->daddr, |
70182ed2 MN |
233 | sizeof(sel.daddr)); |
234 | sel.prefixlen_d = 128; | |
0660e03f | 235 | memcpy(&sel.saddr, (xfrm_address_t *)&ipv6_hdr(skb)->saddr, |
70182ed2 MN |
236 | sizeof(sel.saddr)); |
237 | sel.prefixlen_s = 128; | |
238 | sel.family = AF_INET6; | |
239 | sel.proto = fl->proto; | |
240 | sel.dport = xfrm_flowi_dport(fl); | |
241 | if (sel.dport) | |
e69a4adc | 242 | sel.dport_mask = htons(~0); |
70182ed2 MN |
243 | sel.sport = xfrm_flowi_sport(fl); |
244 | if (sel.sport) | |
e69a4adc | 245 | sel.sport_mask = htons(~0); |
70182ed2 MN |
246 | sel.ifindex = fl->oif; |
247 | ||
248 | err = km_report(IPPROTO_DSTOPTS, &sel, | |
249 | (hao ? (xfrm_address_t *)&hao->addr : NULL)); | |
250 | ||
251 | out: | |
252 | return err; | |
253 | } | |
254 | ||
3d126890 NT |
255 | static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb, |
256 | u8 **nexthdr) | |
257 | { | |
258 | u16 offset = sizeof(struct ipv6hdr); | |
0660e03f ACM |
259 | struct ipv6_opt_hdr *exthdr = |
260 | (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1); | |
d56f90a7 ACM |
261 | const unsigned char *nh = skb_network_header(skb); |
262 | unsigned int packet_len = skb->tail - nh; | |
3d126890 NT |
263 | int found_rhdr = 0; |
264 | ||
0660e03f | 265 | *nexthdr = &ipv6_hdr(skb)->nexthdr; |
3d126890 NT |
266 | |
267 | while (offset + 1 <= packet_len) { | |
268 | ||
269 | switch (**nexthdr) { | |
270 | case NEXTHDR_HOP: | |
271 | break; | |
272 | case NEXTHDR_ROUTING: | |
273 | found_rhdr = 1; | |
274 | break; | |
275 | case NEXTHDR_DEST: | |
276 | /* | |
277 | * HAO MUST NOT appear more than once. | |
278 | * XXX: It is better to try to find by the end of | |
279 | * XXX: packet if HAO exists. | |
280 | */ | |
281 | if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0) { | |
282 | LIMIT_NETDEBUG(KERN_WARNING "mip6: hao exists already, override\n"); | |
283 | return offset; | |
284 | } | |
285 | ||
286 | if (found_rhdr) | |
287 | return offset; | |
288 | ||
289 | break; | |
290 | default: | |
291 | return offset; | |
292 | } | |
293 | ||
294 | offset += ipv6_optlen(exthdr); | |
295 | *nexthdr = &exthdr->nexthdr; | |
d56f90a7 | 296 | exthdr = (struct ipv6_opt_hdr *)(nh + offset); |
3d126890 NT |
297 | } |
298 | ||
299 | return offset; | |
300 | } | |
301 | ||
302 | static int mip6_destopt_init_state(struct xfrm_state *x) | |
303 | { | |
304 | if (x->id.spi) { | |
305 | printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__, | |
306 | x->id.spi); | |
307 | return -EINVAL; | |
308 | } | |
309 | if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) { | |
310 | printk(KERN_INFO "%s: state's mode is not %u: %u\n", | |
311 | __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode); | |
312 | return -EINVAL; | |
313 | } | |
314 | ||
315 | x->props.header_len = sizeof(struct ipv6_destopt_hdr) + | |
316 | calc_padlen(sizeof(struct ipv6_destopt_hdr), 6) + | |
317 | sizeof(struct ipv6_destopt_hao); | |
318 | BUG_TRAP(x->props.header_len == 24); | |
319 | ||
320 | return 0; | |
321 | } | |
322 | ||
323 | /* | |
324 | * Do nothing about destroying since it has no specific operation for | |
325 | * destination options header unlike IPsec protocols. | |
326 | */ | |
327 | static void mip6_destopt_destroy(struct xfrm_state *x) | |
328 | { | |
329 | } | |
330 | ||
331 | static struct xfrm_type mip6_destopt_type = | |
332 | { | |
333 | .description = "MIP6DESTOPT", | |
334 | .owner = THIS_MODULE, | |
335 | .proto = IPPROTO_DSTOPTS, | |
336 | .flags = XFRM_TYPE_NON_FRAGMENT, | |
337 | .init_state = mip6_destopt_init_state, | |
338 | .destructor = mip6_destopt_destroy, | |
339 | .input = mip6_destopt_input, | |
340 | .output = mip6_destopt_output, | |
1ab1457c | 341 | .reject = mip6_destopt_reject, |
3d126890 NT |
342 | .hdr_offset = mip6_destopt_offset, |
343 | .local_addr = mip6_xfrm_addr, | |
344 | }; | |
345 | ||
2c8d7ca0 NT |
346 | static int mip6_rthdr_input(struct xfrm_state *x, struct sk_buff *skb) |
347 | { | |
348 | struct rt2_hdr *rt2 = (struct rt2_hdr *)skb->data; | |
349 | ||
350 | if (!ipv6_addr_equal(&rt2->addr, (struct in6_addr *)x->coaddr) && | |
351 | !ipv6_addr_any((struct in6_addr *)x->coaddr)) | |
352 | return -ENOENT; | |
353 | ||
354 | return rt2->rt_hdr.nexthdr; | |
355 | } | |
356 | ||
357 | /* Routing Header type 2 is inserted. | |
358 | * IP Header's dst address is replaced with Routing Header's Home Address. | |
359 | */ | |
360 | static int mip6_rthdr_output(struct xfrm_state *x, struct sk_buff *skb) | |
361 | { | |
362 | struct ipv6hdr *iph; | |
363 | struct rt2_hdr *rt2; | |
364 | u8 nexthdr; | |
365 | ||
366 | iph = (struct ipv6hdr *)skb->data; | |
367 | iph->payload_len = htons(skb->len - sizeof(*iph)); | |
368 | ||
d56f90a7 ACM |
369 | nexthdr = *skb_network_header(skb); |
370 | *skb_network_header(skb) = IPPROTO_ROUTING; | |
2c8d7ca0 NT |
371 | |
372 | rt2 = (struct rt2_hdr *)skb->h.raw; | |
373 | rt2->rt_hdr.nexthdr = nexthdr; | |
374 | rt2->rt_hdr.hdrlen = (x->props.header_len >> 3) - 1; | |
375 | rt2->rt_hdr.type = IPV6_SRCRT_TYPE_2; | |
376 | rt2->rt_hdr.segments_left = 1; | |
377 | memset(&rt2->reserved, 0, sizeof(rt2->reserved)); | |
378 | ||
379 | BUG_TRAP(rt2->rt_hdr.hdrlen == 2); | |
380 | ||
381 | memcpy(&rt2->addr, &iph->daddr, sizeof(rt2->addr)); | |
382 | memcpy(&iph->daddr, x->coaddr, sizeof(iph->daddr)); | |
383 | ||
384 | return 0; | |
385 | } | |
386 | ||
387 | static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb, | |
388 | u8 **nexthdr) | |
389 | { | |
390 | u16 offset = sizeof(struct ipv6hdr); | |
0660e03f ACM |
391 | struct ipv6_opt_hdr *exthdr = |
392 | (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1); | |
d56f90a7 ACM |
393 | const unsigned char *nh = skb_network_header(skb); |
394 | unsigned int packet_len = skb->tail - nh; | |
2c8d7ca0 NT |
395 | int found_rhdr = 0; |
396 | ||
0660e03f | 397 | *nexthdr = &ipv6_hdr(skb)->nexthdr; |
2c8d7ca0 NT |
398 | |
399 | while (offset + 1 <= packet_len) { | |
400 | ||
401 | switch (**nexthdr) { | |
402 | case NEXTHDR_HOP: | |
403 | break; | |
404 | case NEXTHDR_ROUTING: | |
405 | if (offset + 3 <= packet_len) { | |
406 | struct ipv6_rt_hdr *rt; | |
d56f90a7 | 407 | rt = (struct ipv6_rt_hdr *)(nh + offset); |
2c8d7ca0 NT |
408 | if (rt->type != 0) |
409 | return offset; | |
410 | } | |
411 | found_rhdr = 1; | |
412 | break; | |
413 | case NEXTHDR_DEST: | |
414 | if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0) | |
415 | return offset; | |
416 | ||
417 | if (found_rhdr) | |
418 | return offset; | |
419 | ||
420 | break; | |
421 | default: | |
422 | return offset; | |
423 | } | |
424 | ||
425 | offset += ipv6_optlen(exthdr); | |
426 | *nexthdr = &exthdr->nexthdr; | |
d56f90a7 | 427 | exthdr = (struct ipv6_opt_hdr *)(nh + offset); |
2c8d7ca0 NT |
428 | } |
429 | ||
430 | return offset; | |
431 | } | |
432 | ||
433 | static int mip6_rthdr_init_state(struct xfrm_state *x) | |
434 | { | |
435 | if (x->id.spi) { | |
436 | printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__, | |
437 | x->id.spi); | |
438 | return -EINVAL; | |
439 | } | |
440 | if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) { | |
441 | printk(KERN_INFO "%s: state's mode is not %u: %u\n", | |
442 | __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode); | |
443 | return -EINVAL; | |
444 | } | |
445 | ||
446 | x->props.header_len = sizeof(struct rt2_hdr); | |
447 | ||
448 | return 0; | |
449 | } | |
450 | ||
451 | /* | |
452 | * Do nothing about destroying since it has no specific operation for routing | |
453 | * header type 2 unlike IPsec protocols. | |
454 | */ | |
455 | static void mip6_rthdr_destroy(struct xfrm_state *x) | |
456 | { | |
457 | } | |
458 | ||
459 | static struct xfrm_type mip6_rthdr_type = | |
460 | { | |
461 | .description = "MIP6RT", | |
462 | .owner = THIS_MODULE, | |
463 | .proto = IPPROTO_ROUTING, | |
464 | .flags = XFRM_TYPE_NON_FRAGMENT, | |
465 | .init_state = mip6_rthdr_init_state, | |
466 | .destructor = mip6_rthdr_destroy, | |
467 | .input = mip6_rthdr_input, | |
468 | .output = mip6_rthdr_output, | |
469 | .hdr_offset = mip6_rthdr_offset, | |
470 | .remote_addr = mip6_xfrm_addr, | |
471 | }; | |
472 | ||
473 | int __init mip6_init(void) | |
474 | { | |
475 | printk(KERN_INFO "Mobile IPv6\n"); | |
476 | ||
3d126890 NT |
477 | if (xfrm_register_type(&mip6_destopt_type, AF_INET6) < 0) { |
478 | printk(KERN_INFO "%s: can't add xfrm type(destopt)\n", __FUNCTION__); | |
479 | goto mip6_destopt_xfrm_fail; | |
480 | } | |
2c8d7ca0 NT |
481 | if (xfrm_register_type(&mip6_rthdr_type, AF_INET6) < 0) { |
482 | printk(KERN_INFO "%s: can't add xfrm type(rthdr)\n", __FUNCTION__); | |
483 | goto mip6_rthdr_xfrm_fail; | |
484 | } | |
485 | return 0; | |
486 | ||
487 | mip6_rthdr_xfrm_fail: | |
3d126890 NT |
488 | xfrm_unregister_type(&mip6_destopt_type, AF_INET6); |
489 | mip6_destopt_xfrm_fail: | |
2c8d7ca0 NT |
490 | return -EAGAIN; |
491 | } | |
492 | ||
493 | void __exit mip6_fini(void) | |
494 | { | |
495 | if (xfrm_unregister_type(&mip6_rthdr_type, AF_INET6) < 0) | |
496 | printk(KERN_INFO "%s: can't remove xfrm type(rthdr)\n", __FUNCTION__); | |
3d126890 NT |
497 | if (xfrm_unregister_type(&mip6_destopt_type, AF_INET6) < 0) |
498 | printk(KERN_INFO "%s: can't remove xfrm type(destopt)\n", __FUNCTION__); | |
2c8d7ca0 | 499 | } |