[UDP]: Fix AF-specific references in AF-agnostic code.
[linux-2.6/openmoko-kernel/knife-kernel.git] / net / ipv6 / mip6.c
blob13b7160fb8927b77b30bd6826f295169c71154e8
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/module.h>
26 #include <linux/skbuff.h>
27 #include <linux/time.h>
28 #include <linux/ipv6.h>
29 #include <linux/icmpv6.h>
30 #include <net/sock.h>
31 #include <net/ipv6.h>
32 #include <net/ip6_checksum.h>
33 #include <net/xfrm.h>
34 #include <net/mip6.h>
36 static xfrm_address_t *mip6_xfrm_addr(struct xfrm_state *x, xfrm_address_t *addr)
38 return x->coaddr;
41 static inline unsigned int calc_padlen(unsigned int len, unsigned int n)
43 return (n - len + 16) & 0x7;
46 static inline void *mip6_padn(__u8 *data, __u8 padlen)
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]);
58 return data + padlen;
61 static inline void mip6_param_prob(struct sk_buff *skb, int code, int pos)
63 icmpv6_send(skb, ICMPV6_PARAMPROB, code, pos, skb->dev);
66 static int mip6_mh_len(int type)
68 int len = 0;
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;
86 return len;
89 int mip6_mh_filter(struct sock *sk, struct sk_buff *skb)
91 struct ip6_mh *mh;
93 if (!pskb_may_pull(skb, (skb_transport_offset(skb)) + 8) ||
94 !pskb_may_pull(skb, (skb_transport_offset(skb) +
95 ((skb_transport_header(skb)[1] + 1) << 3))))
96 return -1;
98 mh = (struct ip6_mh *)skb_transport_header(skb);
100 if (mh->ip6mh_hdrlen < mip6_mh_len(mh->ip6mh_type)) {
101 LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH message too short: %d vs >=%d\n",
102 mh->ip6mh_hdrlen, mip6_mh_len(mh->ip6mh_type));
103 mip6_param_prob(skb, 0, ((&mh->ip6mh_hdrlen) -
104 skb_network_header(skb)));
105 return -1;
108 if (mh->ip6mh_proto != IPPROTO_NONE) {
109 LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH invalid payload proto = %d\n",
110 mh->ip6mh_proto);
111 mip6_param_prob(skb, 0, ((&mh->ip6mh_proto) -
112 skb_network_header(skb)));
113 return -1;
116 return 0;
119 struct mip6_report_rate_limiter {
120 spinlock_t lock;
121 struct timeval stamp;
122 int iif;
123 struct in6_addr src;
124 struct in6_addr dst;
127 static struct mip6_report_rate_limiter mip6_report_rl = {
128 .lock = __SPIN_LOCK_UNLOCKED(mip6_report_rl.lock)
131 static int mip6_destopt_input(struct xfrm_state *x, struct sk_buff *skb)
133 struct ipv6hdr *iph = ipv6_hdr(skb);
134 struct ipv6_destopt_hdr *destopt = (struct ipv6_destopt_hdr *)skb->data;
136 if (!ipv6_addr_equal(&iph->saddr, (struct in6_addr *)x->coaddr) &&
137 !ipv6_addr_any((struct in6_addr *)x->coaddr))
138 return -ENOENT;
140 return destopt->nexthdr;
143 /* Destination Option Header is inserted.
144 * IP Header's src address is replaced with Home Address Option in
145 * Destination Option Header.
147 static int mip6_destopt_output(struct xfrm_state *x, struct sk_buff *skb)
149 struct ipv6hdr *iph;
150 struct ipv6_destopt_hdr *dstopt;
151 struct ipv6_destopt_hao *hao;
152 u8 nexthdr;
153 int len;
155 iph = (struct ipv6hdr *)skb->data;
156 iph->payload_len = htons(skb->len - sizeof(*iph));
158 nexthdr = *skb_network_header(skb);
159 *skb_network_header(skb) = IPPROTO_DSTOPTS;
161 dstopt = (struct ipv6_destopt_hdr *)skb_transport_header(skb);
162 dstopt->nexthdr = nexthdr;
164 hao = mip6_padn((char *)(dstopt + 1),
165 calc_padlen(sizeof(*dstopt), 6));
167 hao->type = IPV6_TLV_HAO;
168 hao->length = sizeof(*hao) - 2;
169 BUG_TRAP(hao->length == 16);
171 len = ((char *)hao - (char *)dstopt) + sizeof(*hao);
173 memcpy(&hao->addr, &iph->saddr, sizeof(hao->addr));
174 memcpy(&iph->saddr, x->coaddr, sizeof(iph->saddr));
176 BUG_TRAP(len == x->props.header_len);
177 dstopt->hdrlen = (x->props.header_len >> 3) - 1;
179 return 0;
182 static inline int mip6_report_rl_allow(struct timeval *stamp,
183 struct in6_addr *dst,
184 struct in6_addr *src, int iif)
186 int allow = 0;
188 spin_lock_bh(&mip6_report_rl.lock);
189 if (mip6_report_rl.stamp.tv_sec != stamp->tv_sec ||
190 mip6_report_rl.stamp.tv_usec != stamp->tv_usec ||
191 mip6_report_rl.iif != iif ||
192 !ipv6_addr_equal(&mip6_report_rl.src, src) ||
193 !ipv6_addr_equal(&mip6_report_rl.dst, dst)) {
194 mip6_report_rl.stamp.tv_sec = stamp->tv_sec;
195 mip6_report_rl.stamp.tv_usec = stamp->tv_usec;
196 mip6_report_rl.iif = iif;
197 ipv6_addr_copy(&mip6_report_rl.src, src);
198 ipv6_addr_copy(&mip6_report_rl.dst, dst);
199 allow = 1;
201 spin_unlock_bh(&mip6_report_rl.lock);
202 return allow;
205 static int mip6_destopt_reject(struct xfrm_state *x, struct sk_buff *skb, struct flowi *fl)
207 struct inet6_skb_parm *opt = (struct inet6_skb_parm *)skb->cb;
208 struct ipv6_destopt_hao *hao = NULL;
209 struct xfrm_selector sel;
210 int offset;
211 struct timeval stamp;
212 int err = 0;
214 if (unlikely(fl->proto == IPPROTO_MH &&
215 fl->fl_mh_type <= IP6_MH_TYPE_MAX))
216 goto out;
218 if (likely(opt->dsthao)) {
219 offset = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
220 if (likely(offset >= 0))
221 hao = (struct ipv6_destopt_hao *)
222 (skb_network_header(skb) + offset);
225 skb_get_timestamp(skb, &stamp);
227 if (!mip6_report_rl_allow(&stamp, &ipv6_hdr(skb)->daddr,
228 hao ? &hao->addr : &ipv6_hdr(skb)->saddr,
229 opt->iif))
230 goto out;
232 memset(&sel, 0, sizeof(sel));
233 memcpy(&sel.daddr, (xfrm_address_t *)&ipv6_hdr(skb)->daddr,
234 sizeof(sel.daddr));
235 sel.prefixlen_d = 128;
236 memcpy(&sel.saddr, (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
237 sizeof(sel.saddr));
238 sel.prefixlen_s = 128;
239 sel.family = AF_INET6;
240 sel.proto = fl->proto;
241 sel.dport = xfrm_flowi_dport(fl);
242 if (sel.dport)
243 sel.dport_mask = htons(~0);
244 sel.sport = xfrm_flowi_sport(fl);
245 if (sel.sport)
246 sel.sport_mask = htons(~0);
247 sel.ifindex = fl->oif;
249 err = km_report(IPPROTO_DSTOPTS, &sel,
250 (hao ? (xfrm_address_t *)&hao->addr : NULL));
252 out:
253 return err;
256 static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
257 u8 **nexthdr)
259 u16 offset = sizeof(struct ipv6hdr);
260 struct ipv6_opt_hdr *exthdr =
261 (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
262 const unsigned char *nh = skb_network_header(skb);
263 unsigned int packet_len = skb->tail - skb->network_header;
264 int found_rhdr = 0;
266 *nexthdr = &ipv6_hdr(skb)->nexthdr;
268 while (offset + 1 <= packet_len) {
270 switch (**nexthdr) {
271 case NEXTHDR_HOP:
272 break;
273 case NEXTHDR_ROUTING:
274 found_rhdr = 1;
275 break;
276 case NEXTHDR_DEST:
278 * HAO MUST NOT appear more than once.
279 * XXX: It is better to try to find by the end of
280 * XXX: packet if HAO exists.
282 if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0) {
283 LIMIT_NETDEBUG(KERN_WARNING "mip6: hao exists already, override\n");
284 return offset;
287 if (found_rhdr)
288 return offset;
290 break;
291 default:
292 return offset;
295 offset += ipv6_optlen(exthdr);
296 *nexthdr = &exthdr->nexthdr;
297 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
300 return offset;
303 static int mip6_destopt_init_state(struct xfrm_state *x)
305 if (x->id.spi) {
306 printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
307 x->id.spi);
308 return -EINVAL;
310 if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
311 printk(KERN_INFO "%s: state's mode is not %u: %u\n",
312 __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
313 return -EINVAL;
316 x->props.header_len = sizeof(struct ipv6_destopt_hdr) +
317 calc_padlen(sizeof(struct ipv6_destopt_hdr), 6) +
318 sizeof(struct ipv6_destopt_hao);
319 BUG_TRAP(x->props.header_len == 24);
321 return 0;
325 * Do nothing about destroying since it has no specific operation for
326 * destination options header unlike IPsec protocols.
328 static void mip6_destopt_destroy(struct xfrm_state *x)
332 static struct xfrm_type mip6_destopt_type =
334 .description = "MIP6DESTOPT",
335 .owner = THIS_MODULE,
336 .proto = IPPROTO_DSTOPTS,
337 .flags = XFRM_TYPE_NON_FRAGMENT,
338 .init_state = mip6_destopt_init_state,
339 .destructor = mip6_destopt_destroy,
340 .input = mip6_destopt_input,
341 .output = mip6_destopt_output,
342 .reject = mip6_destopt_reject,
343 .hdr_offset = mip6_destopt_offset,
344 .local_addr = mip6_xfrm_addr,
347 static int mip6_rthdr_input(struct xfrm_state *x, struct sk_buff *skb)
349 struct rt2_hdr *rt2 = (struct rt2_hdr *)skb->data;
351 if (!ipv6_addr_equal(&rt2->addr, (struct in6_addr *)x->coaddr) &&
352 !ipv6_addr_any((struct in6_addr *)x->coaddr))
353 return -ENOENT;
355 return rt2->rt_hdr.nexthdr;
358 /* Routing Header type 2 is inserted.
359 * IP Header's dst address is replaced with Routing Header's Home Address.
361 static int mip6_rthdr_output(struct xfrm_state *x, struct sk_buff *skb)
363 struct ipv6hdr *iph;
364 struct rt2_hdr *rt2;
365 u8 nexthdr;
367 iph = (struct ipv6hdr *)skb->data;
368 iph->payload_len = htons(skb->len - sizeof(*iph));
370 nexthdr = *skb_network_header(skb);
371 *skb_network_header(skb) = IPPROTO_ROUTING;
373 rt2 = (struct rt2_hdr *)skb_transport_header(skb);
374 rt2->rt_hdr.nexthdr = nexthdr;
375 rt2->rt_hdr.hdrlen = (x->props.header_len >> 3) - 1;
376 rt2->rt_hdr.type = IPV6_SRCRT_TYPE_2;
377 rt2->rt_hdr.segments_left = 1;
378 memset(&rt2->reserved, 0, sizeof(rt2->reserved));
380 BUG_TRAP(rt2->rt_hdr.hdrlen == 2);
382 memcpy(&rt2->addr, &iph->daddr, sizeof(rt2->addr));
383 memcpy(&iph->daddr, x->coaddr, sizeof(iph->daddr));
385 return 0;
388 static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb,
389 u8 **nexthdr)
391 u16 offset = sizeof(struct ipv6hdr);
392 struct ipv6_opt_hdr *exthdr =
393 (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
394 const unsigned char *nh = skb_network_header(skb);
395 unsigned int packet_len = skb->tail - skb->network_header;
396 int found_rhdr = 0;
398 *nexthdr = &ipv6_hdr(skb)->nexthdr;
400 while (offset + 1 <= packet_len) {
402 switch (**nexthdr) {
403 case NEXTHDR_HOP:
404 break;
405 case NEXTHDR_ROUTING:
406 if (offset + 3 <= packet_len) {
407 struct ipv6_rt_hdr *rt;
408 rt = (struct ipv6_rt_hdr *)(nh + offset);
409 if (rt->type != 0)
410 return offset;
412 found_rhdr = 1;
413 break;
414 case NEXTHDR_DEST:
415 if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
416 return offset;
418 if (found_rhdr)
419 return offset;
421 break;
422 default:
423 return offset;
426 offset += ipv6_optlen(exthdr);
427 *nexthdr = &exthdr->nexthdr;
428 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
431 return offset;
434 static int mip6_rthdr_init_state(struct xfrm_state *x)
436 if (x->id.spi) {
437 printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
438 x->id.spi);
439 return -EINVAL;
441 if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
442 printk(KERN_INFO "%s: state's mode is not %u: %u\n",
443 __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
444 return -EINVAL;
447 x->props.header_len = sizeof(struct rt2_hdr);
449 return 0;
453 * Do nothing about destroying since it has no specific operation for routing
454 * header type 2 unlike IPsec protocols.
456 static void mip6_rthdr_destroy(struct xfrm_state *x)
460 static struct xfrm_type mip6_rthdr_type =
462 .description = "MIP6RT",
463 .owner = THIS_MODULE,
464 .proto = IPPROTO_ROUTING,
465 .flags = XFRM_TYPE_NON_FRAGMENT,
466 .init_state = mip6_rthdr_init_state,
467 .destructor = mip6_rthdr_destroy,
468 .input = mip6_rthdr_input,
469 .output = mip6_rthdr_output,
470 .hdr_offset = mip6_rthdr_offset,
471 .remote_addr = mip6_xfrm_addr,
474 int __init mip6_init(void)
476 printk(KERN_INFO "Mobile IPv6\n");
478 if (xfrm_register_type(&mip6_destopt_type, AF_INET6) < 0) {
479 printk(KERN_INFO "%s: can't add xfrm type(destopt)\n", __FUNCTION__);
480 goto mip6_destopt_xfrm_fail;
482 if (xfrm_register_type(&mip6_rthdr_type, AF_INET6) < 0) {
483 printk(KERN_INFO "%s: can't add xfrm type(rthdr)\n", __FUNCTION__);
484 goto mip6_rthdr_xfrm_fail;
486 return 0;
488 mip6_rthdr_xfrm_fail:
489 xfrm_unregister_type(&mip6_destopt_type, AF_INET6);
490 mip6_destopt_xfrm_fail:
491 return -EAGAIN;
494 void __exit mip6_fini(void)
496 if (xfrm_unregister_type(&mip6_rthdr_type, AF_INET6) < 0)
497 printk(KERN_INFO "%s: can't remove xfrm type(rthdr)\n", __FUNCTION__);
498 if (xfrm_unregister_type(&mip6_destopt_type, AF_INET6) < 0)
499 printk(KERN_INFO "%s: can't remove xfrm type(destopt)\n", __FUNCTION__);