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_transport_offset(skb
)) + 8) ||
94 !pskb_may_pull(skb
, (skb_transport_offset(skb
) +
95 ((skb_transport_header(skb
)[1] + 1) << 3))))
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
)));
108 if (mh
->ip6mh_proto
!= IPPROTO_NONE
) {
109 LIMIT_NETDEBUG(KERN_DEBUG
"mip6: MH invalid payload proto = %d\n",
111 mip6_param_prob(skb
, 0, ((&mh
->ip6mh_proto
) -
112 skb_network_header(skb
)));
119 struct mip6_report_rate_limiter
{
121 struct timeval stamp
;
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
))
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
)
150 struct ipv6_destopt_hdr
*dstopt
;
151 struct ipv6_destopt_hao
*hao
;
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;
182 static inline int mip6_report_rl_allow(struct timeval
*stamp
,
183 struct in6_addr
*dst
,
184 struct in6_addr
*src
, int iif
)
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
);
201 spin_unlock_bh(&mip6_report_rl
.lock
);
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
;
211 struct timeval stamp
;
214 if (unlikely(fl
->proto
== IPPROTO_MH
&&
215 fl
->fl_mh_type
<= IP6_MH_TYPE_MAX
))
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
,
232 memset(&sel
, 0, sizeof(sel
));
233 memcpy(&sel
.daddr
, (xfrm_address_t
*)&ipv6_hdr(skb
)->daddr
,
235 sel
.prefixlen_d
= 128;
236 memcpy(&sel
.saddr
, (xfrm_address_t
*)&ipv6_hdr(skb
)->saddr
,
238 sel
.prefixlen_s
= 128;
239 sel
.family
= AF_INET6
;
240 sel
.proto
= fl
->proto
;
241 sel
.dport
= xfrm_flowi_dport(fl
);
243 sel
.dport_mask
= htons(~0);
244 sel
.sport
= xfrm_flowi_sport(fl
);
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
));
256 static int mip6_destopt_offset(struct xfrm_state
*x
, struct sk_buff
*skb
,
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
;
266 *nexthdr
= &ipv6_hdr(skb
)->nexthdr
;
268 while (offset
+ 1 <= packet_len
) {
273 case NEXTHDR_ROUTING
:
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");
295 offset
+= ipv6_optlen(exthdr
);
296 *nexthdr
= &exthdr
->nexthdr
;
297 exthdr
= (struct ipv6_opt_hdr
*)(nh
+ offset
);
303 static int mip6_destopt_init_state(struct xfrm_state
*x
)
306 printk(KERN_INFO
"%s: spi is not 0: %u\n", __FUNCTION__
,
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
);
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);
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
))
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
)
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
));
388 static int mip6_rthdr_offset(struct xfrm_state
*x
, struct sk_buff
*skb
,
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
;
398 *nexthdr
= &ipv6_hdr(skb
)->nexthdr
;
400 while (offset
+ 1 <= packet_len
) {
405 case NEXTHDR_ROUTING
:
406 if (offset
+ 3 <= packet_len
) {
407 struct ipv6_rt_hdr
*rt
;
408 rt
= (struct ipv6_rt_hdr
*)(nh
+ offset
);
415 if (ipv6_find_tlv(skb
, offset
, IPV6_TLV_HAO
) >= 0)
426 offset
+= ipv6_optlen(exthdr
);
427 *nexthdr
= &exthdr
->nexthdr
;
428 exthdr
= (struct ipv6_opt_hdr
*)(nh
+ offset
);
434 static int mip6_rthdr_init_state(struct xfrm_state
*x
)
437 printk(KERN_INFO
"%s: spi is not 0: %u\n", __FUNCTION__
,
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
);
447 x
->props
.header_len
= sizeof(struct rt2_hdr
);
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
;
488 mip6_rthdr_xfrm_fail
:
489 xfrm_unregister_type(&mip6_destopt_type
, AF_INET6
);
490 mip6_destopt_xfrm_fail
:
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__
);