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/config.h>
26 #include <linux/module.h>
27 #include <linux/skbuff.h>
28 #include <linux/ipv6.h>
33 static xfrm_address_t
*mip6_xfrm_addr(struct xfrm_state
*x
, xfrm_address_t
*addr
)
38 static int mip6_rthdr_input(struct xfrm_state
*x
, struct sk_buff
*skb
)
40 struct rt2_hdr
*rt2
= (struct rt2_hdr
*)skb
->data
;
42 if (!ipv6_addr_equal(&rt2
->addr
, (struct in6_addr
*)x
->coaddr
) &&
43 !ipv6_addr_any((struct in6_addr
*)x
->coaddr
))
46 return rt2
->rt_hdr
.nexthdr
;
49 /* Routing Header type 2 is inserted.
50 * IP Header's dst address is replaced with Routing Header's Home Address.
52 static int mip6_rthdr_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
58 iph
= (struct ipv6hdr
*)skb
->data
;
59 iph
->payload_len
= htons(skb
->len
- sizeof(*iph
));
61 nexthdr
= *skb
->nh
.raw
;
62 *skb
->nh
.raw
= IPPROTO_ROUTING
;
64 rt2
= (struct rt2_hdr
*)skb
->h
.raw
;
65 rt2
->rt_hdr
.nexthdr
= nexthdr
;
66 rt2
->rt_hdr
.hdrlen
= (x
->props
.header_len
>> 3) - 1;
67 rt2
->rt_hdr
.type
= IPV6_SRCRT_TYPE_2
;
68 rt2
->rt_hdr
.segments_left
= 1;
69 memset(&rt2
->reserved
, 0, sizeof(rt2
->reserved
));
71 BUG_TRAP(rt2
->rt_hdr
.hdrlen
== 2);
73 memcpy(&rt2
->addr
, &iph
->daddr
, sizeof(rt2
->addr
));
74 memcpy(&iph
->daddr
, x
->coaddr
, sizeof(iph
->daddr
));
79 static int mip6_rthdr_offset(struct xfrm_state
*x
, struct sk_buff
*skb
,
82 u16 offset
= sizeof(struct ipv6hdr
);
83 struct ipv6_opt_hdr
*exthdr
= (struct ipv6_opt_hdr
*)(skb
->nh
.ipv6h
+ 1);
84 unsigned int packet_len
= skb
->tail
- skb
->nh
.raw
;
87 *nexthdr
= &skb
->nh
.ipv6h
->nexthdr
;
89 while (offset
+ 1 <= packet_len
) {
95 if (offset
+ 3 <= packet_len
) {
96 struct ipv6_rt_hdr
*rt
;
97 rt
= (struct ipv6_rt_hdr
*)(skb
->nh
.raw
+ offset
);
104 if (ipv6_find_tlv(skb
, offset
, IPV6_TLV_HAO
) >= 0)
115 offset
+= ipv6_optlen(exthdr
);
116 *nexthdr
= &exthdr
->nexthdr
;
117 exthdr
= (struct ipv6_opt_hdr
*)(skb
->nh
.raw
+ offset
);
123 static int mip6_rthdr_init_state(struct xfrm_state
*x
)
126 printk(KERN_INFO
"%s: spi is not 0: %u\n", __FUNCTION__
,
130 if (x
->props
.mode
!= XFRM_MODE_ROUTEOPTIMIZATION
) {
131 printk(KERN_INFO
"%s: state's mode is not %u: %u\n",
132 __FUNCTION__
, XFRM_MODE_ROUTEOPTIMIZATION
, x
->props
.mode
);
136 x
->props
.header_len
= sizeof(struct rt2_hdr
);
142 * Do nothing about destroying since it has no specific operation for routing
143 * header type 2 unlike IPsec protocols.
145 static void mip6_rthdr_destroy(struct xfrm_state
*x
)
149 static struct xfrm_type mip6_rthdr_type
=
151 .description
= "MIP6RT",
152 .owner
= THIS_MODULE
,
153 .proto
= IPPROTO_ROUTING
,
154 .flags
= XFRM_TYPE_NON_FRAGMENT
,
155 .init_state
= mip6_rthdr_init_state
,
156 .destructor
= mip6_rthdr_destroy
,
157 .input
= mip6_rthdr_input
,
158 .output
= mip6_rthdr_output
,
159 .hdr_offset
= mip6_rthdr_offset
,
160 .remote_addr
= mip6_xfrm_addr
,
163 int __init
mip6_init(void)
165 printk(KERN_INFO
"Mobile IPv6\n");
167 if (xfrm_register_type(&mip6_rthdr_type
, AF_INET6
) < 0) {
168 printk(KERN_INFO
"%s: can't add xfrm type(rthdr)\n", __FUNCTION__
);
169 goto mip6_rthdr_xfrm_fail
;
173 mip6_rthdr_xfrm_fail
:
177 void __exit
mip6_fini(void)
179 if (xfrm_unregister_type(&mip6_rthdr_type
, AF_INET6
) < 0)
180 printk(KERN_INFO
"%s: can't remove xfrm type(rthdr)\n", __FUNCTION__
);