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
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>
32 #include <net/ip6_checksum.h>
36 static xfrm_address_t *mip6_xfrm_addr(struct xfrm_state *x, xfrm_address_t *addr)
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)
51 data[0] = MIP6_OPT_PAD_1;
52 } else if (padlen > 1) {
53 data[0] = MIP6_OPT_PAD_N;
56 memset(data+2, 0, data[1]);
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)
74 case IP6_MH_TYPE_HOTI:
75 case IP6_MH_TYPE_COTI:
77 case IP6_MH_TYPE_BACK:
82 case IP6_MH_TYPE_BERROR:
89 int mip6_mh_filter(struct sock *sk, struct sk_buff *skb)
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)))
97 mh = (struct ip6_mh *)skb->h.raw;
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));
102 mip6_param_prob(skb, 0, ((&mh->ip6mh_hdrlen) -
103 skb_network_header(skb)));
107 if (mh->ip6mh_proto != IPPROTO_NONE) {
108 LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH invalid payload proto = %d\n",
110 mip6_param_prob(skb, 0, ((&mh->ip6mh_proto) -
111 skb_network_header(skb)));
118 struct mip6_report_rate_limiter {
120 struct timeval stamp;
126 static struct mip6_report_rate_limiter mip6_report_rl = {
127 .lock = SPIN_LOCK_UNLOCKED
130 static int mip6_destopt_input(struct xfrm_state *x, struct sk_buff *skb)
132 struct ipv6hdr *iph = skb->nh.ipv6h;
133 struct ipv6_destopt_hdr *destopt = (struct ipv6_destopt_hdr *)skb->data;
135 if (!ipv6_addr_equal(&iph->saddr, (struct in6_addr *)x->coaddr) &&
136 !ipv6_addr_any((struct in6_addr *)x->coaddr))
139 return destopt->nexthdr;
142 /* Destination Option Header is inserted.
143 * IP Header's src address is replaced with Home Address Option in
144 * Destination Option Header.
146 static int mip6_destopt_output(struct xfrm_state *x, struct sk_buff *skb)
149 struct ipv6_destopt_hdr *dstopt;
150 struct ipv6_destopt_hao *hao;
154 iph = (struct ipv6hdr *)skb->data;
155 iph->payload_len = htons(skb->len - sizeof(*iph));
157 nexthdr = *skb_network_header(skb);
158 *skb_network_header(skb) = IPPROTO_DSTOPTS;
160 dstopt = (struct ipv6_destopt_hdr *)skb->h.raw;
161 dstopt->nexthdr = nexthdr;
163 hao = mip6_padn((char *)(dstopt + 1),
164 calc_padlen(sizeof(*dstopt), 6));
166 hao->type = IPV6_TLV_HAO;
167 hao->length = sizeof(*hao) - 2;
168 BUG_TRAP(hao->length == 16);
170 len = ((char *)hao - (char *)dstopt) + sizeof(*hao);
172 memcpy(&hao->addr, &iph->saddr, sizeof(hao->addr));
173 memcpy(&iph->saddr, x->coaddr, sizeof(iph->saddr));
175 BUG_TRAP(len == x->props.header_len);
176 dstopt->hdrlen = (x->props.header_len >> 3) - 1;
181 static inline int mip6_report_rl_allow(struct timeval *stamp,
182 struct in6_addr *dst,
183 struct in6_addr *src, int iif)
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);
200 spin_unlock_bh(&mip6_report_rl.lock);
204 static int mip6_destopt_reject(struct xfrm_state *x, struct sk_buff *skb, struct flowi *fl)
206 struct inet6_skb_parm *opt = (struct inet6_skb_parm *)skb->cb;
207 struct ipv6_destopt_hao *hao = NULL;
208 struct xfrm_selector sel;
210 struct timeval stamp;
213 if (unlikely(fl->proto == IPPROTO_MH &&
214 fl->fl_mh_type <= IP6_MH_TYPE_MAX))
217 if (likely(opt->dsthao)) {
218 offset = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
219 if (likely(offset >= 0))
220 hao = (struct ipv6_destopt_hao *)
221 (skb_network_header(skb) + offset);
224 skb_get_timestamp(skb, &stamp);
226 if (!mip6_report_rl_allow(&stamp, &skb->nh.ipv6h->daddr,
227 hao ? &hao->addr : &skb->nh.ipv6h->saddr,
231 memset(&sel, 0, sizeof(sel));
232 memcpy(&sel.daddr, (xfrm_address_t *)&skb->nh.ipv6h->daddr,
234 sel.prefixlen_d = 128;
235 memcpy(&sel.saddr, (xfrm_address_t *)&skb->nh.ipv6h->saddr,
237 sel.prefixlen_s = 128;
238 sel.family = AF_INET6;
239 sel.proto = fl->proto;
240 sel.dport = xfrm_flowi_dport(fl);
242 sel.dport_mask = htons(~0);
243 sel.sport = xfrm_flowi_sport(fl);
245 sel.sport_mask = htons(~0);
246 sel.ifindex = fl->oif;
248 err = km_report(IPPROTO_DSTOPTS, &sel,
249 (hao ? (xfrm_address_t *)&hao->addr : NULL));
255 static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
258 u16 offset = sizeof(struct ipv6hdr);
259 struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.ipv6h + 1);
260 const unsigned char *nh = skb_network_header(skb);
261 unsigned int packet_len = skb->tail - nh;
264 *nexthdr = &skb->nh.ipv6h->nexthdr;
266 while (offset + 1 <= packet_len) {
271 case NEXTHDR_ROUTING:
276 * HAO MUST NOT appear more than once.
277 * XXX: It is better to try to find by the end of
278 * XXX: packet if HAO exists.
280 if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0) {
281 LIMIT_NETDEBUG(KERN_WARNING "mip6: hao exists already, override\n");
293 offset += ipv6_optlen(exthdr);
294 *nexthdr = &exthdr->nexthdr;
295 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
301 static int mip6_destopt_init_state(struct xfrm_state *x)
304 printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
308 if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
309 printk(KERN_INFO "%s: state's mode is not %u: %u\n",
310 __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
314 x->props.header_len = sizeof(struct ipv6_destopt_hdr) +
315 calc_padlen(sizeof(struct ipv6_destopt_hdr), 6) +
316 sizeof(struct ipv6_destopt_hao);
317 BUG_TRAP(x->props.header_len == 24);
323 * Do nothing about destroying since it has no specific operation for
324 * destination options header unlike IPsec protocols.
326 static void mip6_destopt_destroy(struct xfrm_state *x)
330 static struct xfrm_type mip6_destopt_type =
332 .description = "MIP6DESTOPT",
333 .owner = THIS_MODULE,
334 .proto = IPPROTO_DSTOPTS,
335 .flags = XFRM_TYPE_NON_FRAGMENT,
336 .init_state = mip6_destopt_init_state,
337 .destructor = mip6_destopt_destroy,
338 .input = mip6_destopt_input,
339 .output = mip6_destopt_output,
340 .reject = mip6_destopt_reject,
341 .hdr_offset = mip6_destopt_offset,
342 .local_addr = mip6_xfrm_addr,
345 static int mip6_rthdr_input(struct xfrm_state *x, struct sk_buff *skb)
347 struct rt2_hdr *rt2 = (struct rt2_hdr *)skb->data;
349 if (!ipv6_addr_equal(&rt2->addr, (struct in6_addr *)x->coaddr) &&
350 !ipv6_addr_any((struct in6_addr *)x->coaddr))
353 return rt2->rt_hdr.nexthdr;
356 /* Routing Header type 2 is inserted.
357 * IP Header's dst address is replaced with Routing Header's Home Address.
359 static int mip6_rthdr_output(struct xfrm_state *x, struct sk_buff *skb)
365 iph = (struct ipv6hdr *)skb->data;
366 iph->payload_len = htons(skb->len - sizeof(*iph));
368 nexthdr = *skb_network_header(skb);
369 *skb_network_header(skb) = IPPROTO_ROUTING;
371 rt2 = (struct rt2_hdr *)skb->h.raw;
372 rt2->rt_hdr.nexthdr = nexthdr;
373 rt2->rt_hdr.hdrlen = (x->props.header_len >> 3) - 1;
374 rt2->rt_hdr.type = IPV6_SRCRT_TYPE_2;
375 rt2->rt_hdr.segments_left = 1;
376 memset(&rt2->reserved, 0, sizeof(rt2->reserved));
378 BUG_TRAP(rt2->rt_hdr.hdrlen == 2);
380 memcpy(&rt2->addr, &iph->daddr, sizeof(rt2->addr));
381 memcpy(&iph->daddr, x->coaddr, sizeof(iph->daddr));
386 static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb,
389 u16 offset = sizeof(struct ipv6hdr);
390 struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.ipv6h + 1);
391 const unsigned char *nh = skb_network_header(skb);
392 unsigned int packet_len = skb->tail - nh;
395 *nexthdr = &skb->nh.ipv6h->nexthdr;
397 while (offset + 1 <= packet_len) {
402 case NEXTHDR_ROUTING:
403 if (offset + 3 <= packet_len) {
404 struct ipv6_rt_hdr *rt;
405 rt = (struct ipv6_rt_hdr *)(nh + offset);
412 if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
423 offset += ipv6_optlen(exthdr);
424 *nexthdr = &exthdr->nexthdr;
425 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
431 static int mip6_rthdr_init_state(struct xfrm_state *x)
434 printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
438 if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
439 printk(KERN_INFO "%s: state's mode is not %u: %u\n",
440 __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
444 x->props.header_len = sizeof(struct rt2_hdr);
450 * Do nothing about destroying since it has no specific operation for routing
451 * header type 2 unlike IPsec protocols.
453 static void mip6_rthdr_destroy(struct xfrm_state *x)
457 static struct xfrm_type mip6_rthdr_type =
459 .description = "MIP6RT",
460 .owner = THIS_MODULE,
461 .proto = IPPROTO_ROUTING,
462 .flags = XFRM_TYPE_NON_FRAGMENT,
463 .init_state = mip6_rthdr_init_state,
464 .destructor = mip6_rthdr_destroy,
465 .input = mip6_rthdr_input,
466 .output = mip6_rthdr_output,
467 .hdr_offset = mip6_rthdr_offset,
468 .remote_addr = mip6_xfrm_addr,
471 int __init mip6_init(void)
473 printk(KERN_INFO "Mobile IPv6\n");
475 if (xfrm_register_type(&mip6_destopt_type, AF_INET6) < 0) {
476 printk(KERN_INFO "%s: can't add xfrm type(destopt)\n", __FUNCTION__);
477 goto mip6_destopt_xfrm_fail;
479 if (xfrm_register_type(&mip6_rthdr_type, AF_INET6) < 0) {
480 printk(KERN_INFO "%s: can't add xfrm type(rthdr)\n", __FUNCTION__);
481 goto mip6_rthdr_xfrm_fail;
485 mip6_rthdr_xfrm_fail:
486 xfrm_unregister_type(&mip6_destopt_type, AF_INET6);
487 mip6_destopt_xfrm_fail:
491 void __exit mip6_fini(void)
493 if (xfrm_unregister_type(&mip6_rthdr_type, AF_INET6) < 0)
494 printk(KERN_INFO "%s: can't remove xfrm type(rthdr)\n", __FUNCTION__);
495 if (xfrm_unregister_type(&mip6_destopt_type, AF_INET6) < 0)
496 printk(KERN_INFO "%s: can't remove xfrm type(destopt)\n", __FUNCTION__);