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
) - skb
->nh
.raw
);
106 if (mh
->ip6mh_proto
!= IPPROTO_NONE
) {
107 LIMIT_NETDEBUG(KERN_DEBUG
"mip6: MH invalid payload proto = %d\n",
109 mip6_param_prob(skb
, 0, (&mh
->ip6mh_proto
) - skb
->nh
.raw
);
116 struct mip6_report_rate_limiter
{
118 struct timeval stamp
;
124 static struct mip6_report_rate_limiter mip6_report_rl
= {
125 .lock
= SPIN_LOCK_UNLOCKED
128 static int mip6_destopt_input(struct xfrm_state
*x
, struct sk_buff
*skb
)
130 struct ipv6hdr
*iph
= skb
->nh
.ipv6h
;
131 struct ipv6_destopt_hdr
*destopt
= (struct ipv6_destopt_hdr
*)skb
->data
;
133 if (!ipv6_addr_equal(&iph
->saddr
, (struct in6_addr
*)x
->coaddr
) &&
134 !ipv6_addr_any((struct in6_addr
*)x
->coaddr
))
137 return destopt
->nexthdr
;
140 /* Destination Option Header is inserted.
141 * IP Header's src address is replaced with Home Address Option in
142 * Destination Option Header.
144 static int mip6_destopt_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
147 struct ipv6_destopt_hdr
*dstopt
;
148 struct ipv6_destopt_hao
*hao
;
152 iph
= (struct ipv6hdr
*)skb
->data
;
153 iph
->payload_len
= htons(skb
->len
- sizeof(*iph
));
155 nexthdr
= *skb
->nh
.raw
;
156 *skb
->nh
.raw
= IPPROTO_DSTOPTS
;
158 dstopt
= (struct ipv6_destopt_hdr
*)skb
->h
.raw
;
159 dstopt
->nexthdr
= nexthdr
;
161 hao
= mip6_padn((char *)(dstopt
+ 1),
162 calc_padlen(sizeof(*dstopt
), 6));
164 hao
->type
= IPV6_TLV_HAO
;
165 hao
->length
= sizeof(*hao
) - 2;
166 BUG_TRAP(hao
->length
== 16);
168 len
= ((char *)hao
- (char *)dstopt
) + sizeof(*hao
);
170 memcpy(&hao
->addr
, &iph
->saddr
, sizeof(hao
->addr
));
171 memcpy(&iph
->saddr
, x
->coaddr
, sizeof(iph
->saddr
));
173 BUG_TRAP(len
== x
->props
.header_len
);
174 dstopt
->hdrlen
= (x
->props
.header_len
>> 3) - 1;
179 static inline int mip6_report_rl_allow(struct timeval
*stamp
,
180 struct in6_addr
*dst
,
181 struct in6_addr
*src
, int iif
)
185 spin_lock_bh(&mip6_report_rl
.lock
);
186 if (mip6_report_rl
.stamp
.tv_sec
!= stamp
->tv_sec
||
187 mip6_report_rl
.stamp
.tv_usec
!= stamp
->tv_usec
||
188 mip6_report_rl
.iif
!= iif
||
189 !ipv6_addr_equal(&mip6_report_rl
.src
, src
) ||
190 !ipv6_addr_equal(&mip6_report_rl
.dst
, dst
)) {
191 mip6_report_rl
.stamp
.tv_sec
= stamp
->tv_sec
;
192 mip6_report_rl
.stamp
.tv_usec
= stamp
->tv_usec
;
193 mip6_report_rl
.iif
= iif
;
194 ipv6_addr_copy(&mip6_report_rl
.src
, src
);
195 ipv6_addr_copy(&mip6_report_rl
.dst
, dst
);
198 spin_unlock_bh(&mip6_report_rl
.lock
);
202 static int mip6_destopt_reject(struct xfrm_state
*x
, struct sk_buff
*skb
, struct flowi
*fl
)
204 struct inet6_skb_parm
*opt
= (struct inet6_skb_parm
*)skb
->cb
;
205 struct ipv6_destopt_hao
*hao
= NULL
;
206 struct xfrm_selector sel
;
208 struct timeval stamp
;
211 if (unlikely(fl
->proto
== IPPROTO_MH
&&
212 fl
->fl_mh_type
<= IP6_MH_TYPE_MAX
))
215 if (likely(opt
->dsthao
)) {
216 offset
= ipv6_find_tlv(skb
, opt
->dsthao
, IPV6_TLV_HAO
);
217 if (likely(offset
>= 0))
218 hao
= (struct ipv6_destopt_hao
*)(skb
->nh
.raw
+ offset
);
221 skb_get_timestamp(skb
, &stamp
);
223 if (!mip6_report_rl_allow(&stamp
, &skb
->nh
.ipv6h
->daddr
,
224 hao
? &hao
->addr
: &skb
->nh
.ipv6h
->saddr
,
228 memset(&sel
, 0, sizeof(sel
));
229 memcpy(&sel
.daddr
, (xfrm_address_t
*)&skb
->nh
.ipv6h
->daddr
,
231 sel
.prefixlen_d
= 128;
232 memcpy(&sel
.saddr
, (xfrm_address_t
*)&skb
->nh
.ipv6h
->saddr
,
234 sel
.prefixlen_s
= 128;
235 sel
.family
= AF_INET6
;
236 sel
.proto
= fl
->proto
;
237 sel
.dport
= xfrm_flowi_dport(fl
);
239 sel
.dport_mask
= htons(~0);
240 sel
.sport
= xfrm_flowi_sport(fl
);
242 sel
.sport_mask
= htons(~0);
243 sel
.ifindex
= fl
->oif
;
245 err
= km_report(IPPROTO_DSTOPTS
, &sel
,
246 (hao
? (xfrm_address_t
*)&hao
->addr
: NULL
));
252 static int mip6_destopt_offset(struct xfrm_state
*x
, struct sk_buff
*skb
,
255 u16 offset
= sizeof(struct ipv6hdr
);
256 struct ipv6_opt_hdr
*exthdr
= (struct ipv6_opt_hdr
*)(skb
->nh
.ipv6h
+ 1);
257 unsigned int packet_len
= skb
->tail
- skb
->nh
.raw
;
260 *nexthdr
= &skb
->nh
.ipv6h
->nexthdr
;
262 while (offset
+ 1 <= packet_len
) {
267 case NEXTHDR_ROUTING
:
272 * HAO MUST NOT appear more than once.
273 * XXX: It is better to try to find by the end of
274 * XXX: packet if HAO exists.
276 if (ipv6_find_tlv(skb
, offset
, IPV6_TLV_HAO
) >= 0) {
277 LIMIT_NETDEBUG(KERN_WARNING
"mip6: hao exists already, override\n");
289 offset
+= ipv6_optlen(exthdr
);
290 *nexthdr
= &exthdr
->nexthdr
;
291 exthdr
= (struct ipv6_opt_hdr
*)(skb
->nh
.raw
+ offset
);
297 static int mip6_destopt_init_state(struct xfrm_state
*x
)
300 printk(KERN_INFO
"%s: spi is not 0: %u\n", __FUNCTION__
,
304 if (x
->props
.mode
!= XFRM_MODE_ROUTEOPTIMIZATION
) {
305 printk(KERN_INFO
"%s: state's mode is not %u: %u\n",
306 __FUNCTION__
, XFRM_MODE_ROUTEOPTIMIZATION
, x
->props
.mode
);
310 x
->props
.header_len
= sizeof(struct ipv6_destopt_hdr
) +
311 calc_padlen(sizeof(struct ipv6_destopt_hdr
), 6) +
312 sizeof(struct ipv6_destopt_hao
);
313 BUG_TRAP(x
->props
.header_len
== 24);
319 * Do nothing about destroying since it has no specific operation for
320 * destination options header unlike IPsec protocols.
322 static void mip6_destopt_destroy(struct xfrm_state
*x
)
326 static struct xfrm_type mip6_destopt_type
=
328 .description
= "MIP6DESTOPT",
329 .owner
= THIS_MODULE
,
330 .proto
= IPPROTO_DSTOPTS
,
331 .flags
= XFRM_TYPE_NON_FRAGMENT
,
332 .init_state
= mip6_destopt_init_state
,
333 .destructor
= mip6_destopt_destroy
,
334 .input
= mip6_destopt_input
,
335 .output
= mip6_destopt_output
,
336 .reject
= mip6_destopt_reject
,
337 .hdr_offset
= mip6_destopt_offset
,
338 .local_addr
= mip6_xfrm_addr
,
341 static int mip6_rthdr_input(struct xfrm_state
*x
, struct sk_buff
*skb
)
343 struct rt2_hdr
*rt2
= (struct rt2_hdr
*)skb
->data
;
345 if (!ipv6_addr_equal(&rt2
->addr
, (struct in6_addr
*)x
->coaddr
) &&
346 !ipv6_addr_any((struct in6_addr
*)x
->coaddr
))
349 return rt2
->rt_hdr
.nexthdr
;
352 /* Routing Header type 2 is inserted.
353 * IP Header's dst address is replaced with Routing Header's Home Address.
355 static int mip6_rthdr_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
361 iph
= (struct ipv6hdr
*)skb
->data
;
362 iph
->payload_len
= htons(skb
->len
- sizeof(*iph
));
364 nexthdr
= *skb
->nh
.raw
;
365 *skb
->nh
.raw
= IPPROTO_ROUTING
;
367 rt2
= (struct rt2_hdr
*)skb
->h
.raw
;
368 rt2
->rt_hdr
.nexthdr
= nexthdr
;
369 rt2
->rt_hdr
.hdrlen
= (x
->props
.header_len
>> 3) - 1;
370 rt2
->rt_hdr
.type
= IPV6_SRCRT_TYPE_2
;
371 rt2
->rt_hdr
.segments_left
= 1;
372 memset(&rt2
->reserved
, 0, sizeof(rt2
->reserved
));
374 BUG_TRAP(rt2
->rt_hdr
.hdrlen
== 2);
376 memcpy(&rt2
->addr
, &iph
->daddr
, sizeof(rt2
->addr
));
377 memcpy(&iph
->daddr
, x
->coaddr
, sizeof(iph
->daddr
));
382 static int mip6_rthdr_offset(struct xfrm_state
*x
, struct sk_buff
*skb
,
385 u16 offset
= sizeof(struct ipv6hdr
);
386 struct ipv6_opt_hdr
*exthdr
= (struct ipv6_opt_hdr
*)(skb
->nh
.ipv6h
+ 1);
387 unsigned int packet_len
= skb
->tail
- skb
->nh
.raw
;
390 *nexthdr
= &skb
->nh
.ipv6h
->nexthdr
;
392 while (offset
+ 1 <= packet_len
) {
397 case NEXTHDR_ROUTING
:
398 if (offset
+ 3 <= packet_len
) {
399 struct ipv6_rt_hdr
*rt
;
400 rt
= (struct ipv6_rt_hdr
*)(skb
->nh
.raw
+ offset
);
407 if (ipv6_find_tlv(skb
, offset
, IPV6_TLV_HAO
) >= 0)
418 offset
+= ipv6_optlen(exthdr
);
419 *nexthdr
= &exthdr
->nexthdr
;
420 exthdr
= (struct ipv6_opt_hdr
*)(skb
->nh
.raw
+ offset
);
426 static int mip6_rthdr_init_state(struct xfrm_state
*x
)
429 printk(KERN_INFO
"%s: spi is not 0: %u\n", __FUNCTION__
,
433 if (x
->props
.mode
!= XFRM_MODE_ROUTEOPTIMIZATION
) {
434 printk(KERN_INFO
"%s: state's mode is not %u: %u\n",
435 __FUNCTION__
, XFRM_MODE_ROUTEOPTIMIZATION
, x
->props
.mode
);
439 x
->props
.header_len
= sizeof(struct rt2_hdr
);
445 * Do nothing about destroying since it has no specific operation for routing
446 * header type 2 unlike IPsec protocols.
448 static void mip6_rthdr_destroy(struct xfrm_state
*x
)
452 static struct xfrm_type mip6_rthdr_type
=
454 .description
= "MIP6RT",
455 .owner
= THIS_MODULE
,
456 .proto
= IPPROTO_ROUTING
,
457 .flags
= XFRM_TYPE_NON_FRAGMENT
,
458 .init_state
= mip6_rthdr_init_state
,
459 .destructor
= mip6_rthdr_destroy
,
460 .input
= mip6_rthdr_input
,
461 .output
= mip6_rthdr_output
,
462 .hdr_offset
= mip6_rthdr_offset
,
463 .remote_addr
= mip6_xfrm_addr
,
466 int __init
mip6_init(void)
468 printk(KERN_INFO
"Mobile IPv6\n");
470 if (xfrm_register_type(&mip6_destopt_type
, AF_INET6
) < 0) {
471 printk(KERN_INFO
"%s: can't add xfrm type(destopt)\n", __FUNCTION__
);
472 goto mip6_destopt_xfrm_fail
;
474 if (xfrm_register_type(&mip6_rthdr_type
, AF_INET6
) < 0) {
475 printk(KERN_INFO
"%s: can't add xfrm type(rthdr)\n", __FUNCTION__
);
476 goto mip6_rthdr_xfrm_fail
;
480 mip6_rthdr_xfrm_fail
:
481 xfrm_unregister_type(&mip6_destopt_type
, AF_INET6
);
482 mip6_destopt_xfrm_fail
:
486 void __exit
mip6_fini(void)
488 if (xfrm_unregister_type(&mip6_rthdr_type
, AF_INET6
) < 0)
489 printk(KERN_INFO
"%s: can't remove xfrm type(rthdr)\n", __FUNCTION__
);
490 if (xfrm_unregister_type(&mip6_destopt_type
, AF_INET6
) < 0)
491 printk(KERN_INFO
"%s: can't remove xfrm type(destopt)\n", __FUNCTION__
);