[IPV6] MIP6: Report to user-space when home address option is rejected.
[linux-2.6.22.y-op.git] / net / ipv6 / mip6.c
blob31445d09261e402272444f30d58b914da55b303b
1 /*
2 * Copyright (C)2003-2006 Helsinki University of Technology
3 * Copyright (C)2003-2006 USAGI/WIDE Project
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.
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.
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
20 * Authors:
21 * Noriaki TAKAMIYA @USAGI
22 * Masahide NAKAMURA @USAGI
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/skbuff.h>
28 #include <linux/time.h>
29 #include <linux/ipv6.h>
30 #include <linux/icmpv6.h>
31 #include <net/sock.h>
32 #include <net/ipv6.h>
33 #include <net/ip6_checksum.h>
34 #include <net/xfrm.h>
35 #include <net/mip6.h>
37 static xfrm_address_t *mip6_xfrm_addr(struct xfrm_state *x, xfrm_address_t *addr)
39 return x->coaddr;
42 static inline unsigned int calc_padlen(unsigned int len, unsigned int n)
44 return (n - len + 16) & 0x7;
47 static inline void *mip6_padn(__u8 *data, __u8 padlen)
49 if (!data)
50 return NULL;
51 if (padlen == 1) {
52 data[0] = MIP6_OPT_PAD_1;
53 } else if (padlen > 1) {
54 data[0] = MIP6_OPT_PAD_N;
55 data[1] = padlen - 2;
56 if (padlen > 2)
57 memset(data+2, 0, data[1]);
59 return data + padlen;
62 static inline void mip6_param_prob(struct sk_buff *skb, int code, int pos)
64 icmpv6_send(skb, ICMPV6_PARAMPROB, code, pos, skb->dev);
67 static int mip6_mh_len(int type)
69 int len = 0;
71 switch (type) {
72 case IP6_MH_TYPE_BRR:
73 len = 0;
74 break;
75 case IP6_MH_TYPE_HOTI:
76 case IP6_MH_TYPE_COTI:
77 case IP6_MH_TYPE_BU:
78 case IP6_MH_TYPE_BACK:
79 len = 1;
80 break;
81 case IP6_MH_TYPE_HOT:
82 case IP6_MH_TYPE_COT:
83 case IP6_MH_TYPE_BERROR:
84 len = 2;
85 break;
87 return len;
90 int mip6_mh_filter(struct sock *sk, struct sk_buff *skb)
92 struct ip6_mh *mh;
93 int mhlen;
95 if (!pskb_may_pull(skb, (skb->h.raw - skb->data) + 8) ||
96 !pskb_may_pull(skb, (skb->h.raw - skb->data) + ((skb->h.raw[1] + 1) << 3)))
97 return -1;
99 mh = (struct ip6_mh *)skb->h.raw;
101 if (mh->ip6mh_hdrlen < mip6_mh_len(mh->ip6mh_type)) {
102 LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH message too short: %d vs >=%d\n",
103 mh->ip6mh_hdrlen, mip6_mh_len(mh->ip6mh_type));
104 mip6_param_prob(skb, 0, (&mh->ip6mh_hdrlen) - skb->nh.raw);
105 return -1;
107 mhlen = (mh->ip6mh_hdrlen + 1) << 3;
109 if (skb->ip_summed == CHECKSUM_COMPLETE) {
110 skb->ip_summed = CHECKSUM_UNNECESSARY;
111 if (csum_ipv6_magic(&skb->nh.ipv6h->saddr,
112 &skb->nh.ipv6h->daddr,
113 mhlen, IPPROTO_MH,
114 skb->csum)) {
115 LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH hw checksum failed\n");
116 skb->ip_summed = CHECKSUM_NONE;
119 if (skb->ip_summed == CHECKSUM_NONE) {
120 if (csum_ipv6_magic(&skb->nh.ipv6h->saddr,
121 &skb->nh.ipv6h->daddr,
122 mhlen, IPPROTO_MH,
123 skb_checksum(skb, 0, mhlen, 0))) {
124 LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH checksum failed [%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x > %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x]\n",
125 NIP6(skb->nh.ipv6h->saddr),
126 NIP6(skb->nh.ipv6h->daddr));
127 return -1;
129 skb->ip_summed = CHECKSUM_UNNECESSARY;
132 if (mh->ip6mh_proto != IPPROTO_NONE) {
133 LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH invalid payload proto = %d\n",
134 mh->ip6mh_proto);
135 mip6_param_prob(skb, 0, (&mh->ip6mh_proto) - skb->nh.raw);
136 return -1;
139 return 0;
142 struct mip6_report_rate_limiter {
143 spinlock_t lock;
144 struct timeval stamp;
145 int iif;
146 struct in6_addr src;
147 struct in6_addr dst;
150 static struct mip6_report_rate_limiter mip6_report_rl = {
151 .lock = SPIN_LOCK_UNLOCKED
154 static int mip6_destopt_input(struct xfrm_state *x, struct sk_buff *skb)
156 struct ipv6hdr *iph = skb->nh.ipv6h;
157 struct ipv6_destopt_hdr *destopt = (struct ipv6_destopt_hdr *)skb->data;
159 if (!ipv6_addr_equal(&iph->saddr, (struct in6_addr *)x->coaddr) &&
160 !ipv6_addr_any((struct in6_addr *)x->coaddr))
161 return -ENOENT;
163 return destopt->nexthdr;
166 /* Destination Option Header is inserted.
167 * IP Header's src address is replaced with Home Address Option in
168 * Destination Option Header.
170 static int mip6_destopt_output(struct xfrm_state *x, struct sk_buff *skb)
172 struct ipv6hdr *iph;
173 struct ipv6_destopt_hdr *dstopt;
174 struct ipv6_destopt_hao *hao;
175 u8 nexthdr;
176 int len;
178 iph = (struct ipv6hdr *)skb->data;
179 iph->payload_len = htons(skb->len - sizeof(*iph));
181 nexthdr = *skb->nh.raw;
182 *skb->nh.raw = IPPROTO_DSTOPTS;
184 dstopt = (struct ipv6_destopt_hdr *)skb->h.raw;
185 dstopt->nexthdr = nexthdr;
187 hao = mip6_padn((char *)(dstopt + 1),
188 calc_padlen(sizeof(*dstopt), 6));
190 hao->type = IPV6_TLV_HAO;
191 hao->length = sizeof(*hao) - 2;
192 BUG_TRAP(hao->length == 16);
194 len = ((char *)hao - (char *)dstopt) + sizeof(*hao);
196 memcpy(&hao->addr, &iph->saddr, sizeof(hao->addr));
197 memcpy(&iph->saddr, x->coaddr, sizeof(iph->saddr));
199 BUG_TRAP(len == x->props.header_len);
200 dstopt->hdrlen = (x->props.header_len >> 3) - 1;
202 return 0;
205 static inline int mip6_report_rl_allow(struct timeval *stamp,
206 struct in6_addr *dst,
207 struct in6_addr *src, int iif)
209 int allow = 0;
211 spin_lock_bh(&mip6_report_rl.lock);
212 if (mip6_report_rl.stamp.tv_sec != stamp->tv_sec ||
213 mip6_report_rl.stamp.tv_usec != stamp->tv_usec ||
214 mip6_report_rl.iif != iif ||
215 !ipv6_addr_equal(&mip6_report_rl.src, src) ||
216 !ipv6_addr_equal(&mip6_report_rl.dst, dst)) {
217 mip6_report_rl.stamp.tv_sec = stamp->tv_sec;
218 mip6_report_rl.stamp.tv_usec = stamp->tv_usec;
219 mip6_report_rl.iif = iif;
220 ipv6_addr_copy(&mip6_report_rl.src, src);
221 ipv6_addr_copy(&mip6_report_rl.dst, dst);
222 allow = 1;
224 spin_unlock_bh(&mip6_report_rl.lock);
225 return allow;
228 static int mip6_destopt_reject(struct xfrm_state *x, struct sk_buff *skb, struct flowi *fl)
230 struct inet6_skb_parm *opt = (struct inet6_skb_parm *)skb->cb;
231 struct ipv6_destopt_hao *hao = NULL;
232 struct xfrm_selector sel;
233 int offset;
234 struct timeval stamp;
235 int err = 0;
237 if (likely(opt->dsthao)) {
238 offset = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
239 if (likely(offset >= 0))
240 hao = (struct ipv6_destopt_hao *)(skb->nh.raw + offset);
243 skb_get_timestamp(skb, &stamp);
245 if (!mip6_report_rl_allow(&stamp, &skb->nh.ipv6h->daddr,
246 hao ? &hao->addr : &skb->nh.ipv6h->saddr,
247 opt->iif))
248 goto out;
250 memset(&sel, 0, sizeof(sel));
251 memcpy(&sel.daddr, (xfrm_address_t *)&skb->nh.ipv6h->daddr,
252 sizeof(sel.daddr));
253 sel.prefixlen_d = 128;
254 memcpy(&sel.saddr, (xfrm_address_t *)&skb->nh.ipv6h->saddr,
255 sizeof(sel.saddr));
256 sel.prefixlen_s = 128;
257 sel.family = AF_INET6;
258 sel.proto = fl->proto;
259 sel.dport = xfrm_flowi_dport(fl);
260 if (sel.dport)
261 sel.dport_mask = ~((__u16)0);
262 sel.sport = xfrm_flowi_sport(fl);
263 if (sel.sport)
264 sel.sport_mask = ~((__u16)0);
265 sel.ifindex = fl->oif;
267 err = km_report(IPPROTO_DSTOPTS, &sel,
268 (hao ? (xfrm_address_t *)&hao->addr : NULL));
270 out:
271 return err;
274 static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
275 u8 **nexthdr)
277 u16 offset = sizeof(struct ipv6hdr);
278 struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.ipv6h + 1);
279 unsigned int packet_len = skb->tail - skb->nh.raw;
280 int found_rhdr = 0;
282 *nexthdr = &skb->nh.ipv6h->nexthdr;
284 while (offset + 1 <= packet_len) {
286 switch (**nexthdr) {
287 case NEXTHDR_HOP:
288 break;
289 case NEXTHDR_ROUTING:
290 found_rhdr = 1;
291 break;
292 case NEXTHDR_DEST:
294 * HAO MUST NOT appear more than once.
295 * XXX: It is better to try to find by the end of
296 * XXX: packet if HAO exists.
298 if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0) {
299 LIMIT_NETDEBUG(KERN_WARNING "mip6: hao exists already, override\n");
300 return offset;
303 if (found_rhdr)
304 return offset;
306 break;
307 default:
308 return offset;
311 offset += ipv6_optlen(exthdr);
312 *nexthdr = &exthdr->nexthdr;
313 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
316 return offset;
319 static int mip6_destopt_init_state(struct xfrm_state *x)
321 if (x->id.spi) {
322 printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
323 x->id.spi);
324 return -EINVAL;
326 if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
327 printk(KERN_INFO "%s: state's mode is not %u: %u\n",
328 __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
329 return -EINVAL;
332 x->props.header_len = sizeof(struct ipv6_destopt_hdr) +
333 calc_padlen(sizeof(struct ipv6_destopt_hdr), 6) +
334 sizeof(struct ipv6_destopt_hao);
335 BUG_TRAP(x->props.header_len == 24);
337 return 0;
341 * Do nothing about destroying since it has no specific operation for
342 * destination options header unlike IPsec protocols.
344 static void mip6_destopt_destroy(struct xfrm_state *x)
348 static struct xfrm_type mip6_destopt_type =
350 .description = "MIP6DESTOPT",
351 .owner = THIS_MODULE,
352 .proto = IPPROTO_DSTOPTS,
353 .flags = XFRM_TYPE_NON_FRAGMENT,
354 .init_state = mip6_destopt_init_state,
355 .destructor = mip6_destopt_destroy,
356 .input = mip6_destopt_input,
357 .output = mip6_destopt_output,
358 .reject = mip6_destopt_reject,
359 .hdr_offset = mip6_destopt_offset,
360 .local_addr = mip6_xfrm_addr,
363 static int mip6_rthdr_input(struct xfrm_state *x, struct sk_buff *skb)
365 struct rt2_hdr *rt2 = (struct rt2_hdr *)skb->data;
367 if (!ipv6_addr_equal(&rt2->addr, (struct in6_addr *)x->coaddr) &&
368 !ipv6_addr_any((struct in6_addr *)x->coaddr))
369 return -ENOENT;
371 return rt2->rt_hdr.nexthdr;
374 /* Routing Header type 2 is inserted.
375 * IP Header's dst address is replaced with Routing Header's Home Address.
377 static int mip6_rthdr_output(struct xfrm_state *x, struct sk_buff *skb)
379 struct ipv6hdr *iph;
380 struct rt2_hdr *rt2;
381 u8 nexthdr;
383 iph = (struct ipv6hdr *)skb->data;
384 iph->payload_len = htons(skb->len - sizeof(*iph));
386 nexthdr = *skb->nh.raw;
387 *skb->nh.raw = IPPROTO_ROUTING;
389 rt2 = (struct rt2_hdr *)skb->h.raw;
390 rt2->rt_hdr.nexthdr = nexthdr;
391 rt2->rt_hdr.hdrlen = (x->props.header_len >> 3) - 1;
392 rt2->rt_hdr.type = IPV6_SRCRT_TYPE_2;
393 rt2->rt_hdr.segments_left = 1;
394 memset(&rt2->reserved, 0, sizeof(rt2->reserved));
396 BUG_TRAP(rt2->rt_hdr.hdrlen == 2);
398 memcpy(&rt2->addr, &iph->daddr, sizeof(rt2->addr));
399 memcpy(&iph->daddr, x->coaddr, sizeof(iph->daddr));
401 return 0;
404 static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb,
405 u8 **nexthdr)
407 u16 offset = sizeof(struct ipv6hdr);
408 struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.ipv6h + 1);
409 unsigned int packet_len = skb->tail - skb->nh.raw;
410 int found_rhdr = 0;
412 *nexthdr = &skb->nh.ipv6h->nexthdr;
414 while (offset + 1 <= packet_len) {
416 switch (**nexthdr) {
417 case NEXTHDR_HOP:
418 break;
419 case NEXTHDR_ROUTING:
420 if (offset + 3 <= packet_len) {
421 struct ipv6_rt_hdr *rt;
422 rt = (struct ipv6_rt_hdr *)(skb->nh.raw + offset);
423 if (rt->type != 0)
424 return offset;
426 found_rhdr = 1;
427 break;
428 case NEXTHDR_DEST:
429 if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
430 return offset;
432 if (found_rhdr)
433 return offset;
435 break;
436 default:
437 return offset;
440 offset += ipv6_optlen(exthdr);
441 *nexthdr = &exthdr->nexthdr;
442 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
445 return offset;
448 static int mip6_rthdr_init_state(struct xfrm_state *x)
450 if (x->id.spi) {
451 printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
452 x->id.spi);
453 return -EINVAL;
455 if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
456 printk(KERN_INFO "%s: state's mode is not %u: %u\n",
457 __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
458 return -EINVAL;
461 x->props.header_len = sizeof(struct rt2_hdr);
463 return 0;
467 * Do nothing about destroying since it has no specific operation for routing
468 * header type 2 unlike IPsec protocols.
470 static void mip6_rthdr_destroy(struct xfrm_state *x)
474 static struct xfrm_type mip6_rthdr_type =
476 .description = "MIP6RT",
477 .owner = THIS_MODULE,
478 .proto = IPPROTO_ROUTING,
479 .flags = XFRM_TYPE_NON_FRAGMENT,
480 .init_state = mip6_rthdr_init_state,
481 .destructor = mip6_rthdr_destroy,
482 .input = mip6_rthdr_input,
483 .output = mip6_rthdr_output,
484 .hdr_offset = mip6_rthdr_offset,
485 .remote_addr = mip6_xfrm_addr,
488 int __init mip6_init(void)
490 printk(KERN_INFO "Mobile IPv6\n");
492 if (xfrm_register_type(&mip6_destopt_type, AF_INET6) < 0) {
493 printk(KERN_INFO "%s: can't add xfrm type(destopt)\n", __FUNCTION__);
494 goto mip6_destopt_xfrm_fail;
496 if (xfrm_register_type(&mip6_rthdr_type, AF_INET6) < 0) {
497 printk(KERN_INFO "%s: can't add xfrm type(rthdr)\n", __FUNCTION__);
498 goto mip6_rthdr_xfrm_fail;
500 return 0;
502 mip6_rthdr_xfrm_fail:
503 xfrm_unregister_type(&mip6_destopt_type, AF_INET6);
504 mip6_destopt_xfrm_fail:
505 return -EAGAIN;
508 void __exit mip6_fini(void)
510 if (xfrm_unregister_type(&mip6_rthdr_type, AF_INET6) < 0)
511 printk(KERN_INFO "%s: can't remove xfrm type(rthdr)\n", __FUNCTION__);
512 if (xfrm_unregister_type(&mip6_destopt_type, AF_INET6) < 0)
513 printk(KERN_INFO "%s: can't remove xfrm type(destopt)\n", __FUNCTION__);