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 inline unsigned int calc_padlen(unsigned int len, unsigned int n)
40 return (n - len + 16) & 0x7;
43 static inline void *mip6_padn(__u8 *data, __u8 padlen)
48 data[0] = MIP6_OPT_PAD_1;
49 } else if (padlen > 1) {
50 data[0] = MIP6_OPT_PAD_N;
53 memset(data+2, 0, data[1]);
58 static int mip6_destopt_input(struct xfrm_state *x, struct sk_buff *skb)
60 struct ipv6hdr *iph = skb->nh.ipv6h;
61 struct ipv6_destopt_hdr *destopt = (struct ipv6_destopt_hdr *)skb->data;
63 if (!ipv6_addr_equal(&iph->saddr, (struct in6_addr *)x->coaddr) &&
64 !ipv6_addr_any((struct in6_addr *)x->coaddr))
67 return destopt->nexthdr;
70 /* Destination Option Header is inserted.
71 * IP Header's src address is replaced with Home Address Option in
72 * Destination Option Header.
74 static int mip6_destopt_output(struct xfrm_state *x, struct sk_buff *skb)
77 struct ipv6_destopt_hdr *dstopt;
78 struct ipv6_destopt_hao *hao;
82 iph = (struct ipv6hdr *)skb->data;
83 iph->payload_len = htons(skb->len - sizeof(*iph));
85 nexthdr = *skb->nh.raw;
86 *skb->nh.raw = IPPROTO_DSTOPTS;
88 dstopt = (struct ipv6_destopt_hdr *)skb->h.raw;
89 dstopt->nexthdr = nexthdr;
91 hao = mip6_padn((char *)(dstopt + 1),
92 calc_padlen(sizeof(*dstopt), 6));
94 hao->type = IPV6_TLV_HAO;
95 hao->length = sizeof(*hao) - 2;
96 BUG_TRAP(hao->length == 16);
98 len = ((char *)hao - (char *)dstopt) + sizeof(*hao);
100 memcpy(&hao->addr, &iph->saddr, sizeof(hao->addr));
101 memcpy(&iph->saddr, x->coaddr, sizeof(iph->saddr));
103 BUG_TRAP(len == x->props.header_len);
104 dstopt->hdrlen = (x->props.header_len >> 3) - 1;
109 static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
112 u16 offset = sizeof(struct ipv6hdr);
113 struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.ipv6h + 1);
114 unsigned int packet_len = skb->tail - skb->nh.raw;
117 *nexthdr = &skb->nh.ipv6h->nexthdr;
119 while (offset + 1 <= packet_len) {
124 case NEXTHDR_ROUTING:
129 * HAO MUST NOT appear more than once.
130 * XXX: It is better to try to find by the end of
131 * XXX: packet if HAO exists.
133 if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0) {
134 LIMIT_NETDEBUG(KERN_WARNING "mip6: hao exists already, override\n");
146 offset += ipv6_optlen(exthdr);
147 *nexthdr = &exthdr->nexthdr;
148 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
154 static int mip6_destopt_init_state(struct xfrm_state *x)
157 printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
161 if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
162 printk(KERN_INFO "%s: state's mode is not %u: %u\n",
163 __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
167 x->props.header_len = sizeof(struct ipv6_destopt_hdr) +
168 calc_padlen(sizeof(struct ipv6_destopt_hdr), 6) +
169 sizeof(struct ipv6_destopt_hao);
170 BUG_TRAP(x->props.header_len == 24);
176 * Do nothing about destroying since it has no specific operation for
177 * destination options header unlike IPsec protocols.
179 static void mip6_destopt_destroy(struct xfrm_state *x)
183 static struct xfrm_type mip6_destopt_type =
185 .description = "MIP6DESTOPT",
186 .owner = THIS_MODULE,
187 .proto = IPPROTO_DSTOPTS,
188 .flags = XFRM_TYPE_NON_FRAGMENT,
189 .init_state = mip6_destopt_init_state,
190 .destructor = mip6_destopt_destroy,
191 .input = mip6_destopt_input,
192 .output = mip6_destopt_output,
193 .hdr_offset = mip6_destopt_offset,
194 .local_addr = mip6_xfrm_addr,
197 static int mip6_rthdr_input(struct xfrm_state *x, struct sk_buff *skb)
199 struct rt2_hdr *rt2 = (struct rt2_hdr *)skb->data;
201 if (!ipv6_addr_equal(&rt2->addr, (struct in6_addr *)x->coaddr) &&
202 !ipv6_addr_any((struct in6_addr *)x->coaddr))
205 return rt2->rt_hdr.nexthdr;
208 /* Routing Header type 2 is inserted.
209 * IP Header's dst address is replaced with Routing Header's Home Address.
211 static int mip6_rthdr_output(struct xfrm_state *x, struct sk_buff *skb)
217 iph = (struct ipv6hdr *)skb->data;
218 iph->payload_len = htons(skb->len - sizeof(*iph));
220 nexthdr = *skb->nh.raw;
221 *skb->nh.raw = IPPROTO_ROUTING;
223 rt2 = (struct rt2_hdr *)skb->h.raw;
224 rt2->rt_hdr.nexthdr = nexthdr;
225 rt2->rt_hdr.hdrlen = (x->props.header_len >> 3) - 1;
226 rt2->rt_hdr.type = IPV6_SRCRT_TYPE_2;
227 rt2->rt_hdr.segments_left = 1;
228 memset(&rt2->reserved, 0, sizeof(rt2->reserved));
230 BUG_TRAP(rt2->rt_hdr.hdrlen == 2);
232 memcpy(&rt2->addr, &iph->daddr, sizeof(rt2->addr));
233 memcpy(&iph->daddr, x->coaddr, sizeof(iph->daddr));
238 static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb,
241 u16 offset = sizeof(struct ipv6hdr);
242 struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.ipv6h + 1);
243 unsigned int packet_len = skb->tail - skb->nh.raw;
246 *nexthdr = &skb->nh.ipv6h->nexthdr;
248 while (offset + 1 <= packet_len) {
253 case NEXTHDR_ROUTING:
254 if (offset + 3 <= packet_len) {
255 struct ipv6_rt_hdr *rt;
256 rt = (struct ipv6_rt_hdr *)(skb->nh.raw + offset);
263 if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
274 offset += ipv6_optlen(exthdr);
275 *nexthdr = &exthdr->nexthdr;
276 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
282 static int mip6_rthdr_init_state(struct xfrm_state *x)
285 printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
289 if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
290 printk(KERN_INFO "%s: state's mode is not %u: %u\n",
291 __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
295 x->props.header_len = sizeof(struct rt2_hdr);
301 * Do nothing about destroying since it has no specific operation for routing
302 * header type 2 unlike IPsec protocols.
304 static void mip6_rthdr_destroy(struct xfrm_state *x)
308 static struct xfrm_type mip6_rthdr_type =
310 .description = "MIP6RT",
311 .owner = THIS_MODULE,
312 .proto = IPPROTO_ROUTING,
313 .flags = XFRM_TYPE_NON_FRAGMENT,
314 .init_state = mip6_rthdr_init_state,
315 .destructor = mip6_rthdr_destroy,
316 .input = mip6_rthdr_input,
317 .output = mip6_rthdr_output,
318 .hdr_offset = mip6_rthdr_offset,
319 .remote_addr = mip6_xfrm_addr,
322 int __init mip6_init(void)
324 printk(KERN_INFO "Mobile IPv6\n");
326 if (xfrm_register_type(&mip6_destopt_type, AF_INET6) < 0) {
327 printk(KERN_INFO "%s: can't add xfrm type(destopt)\n", __FUNCTION__);
328 goto mip6_destopt_xfrm_fail;
330 if (xfrm_register_type(&mip6_rthdr_type, AF_INET6) < 0) {
331 printk(KERN_INFO "%s: can't add xfrm type(rthdr)\n", __FUNCTION__);
332 goto mip6_rthdr_xfrm_fail;
336 mip6_rthdr_xfrm_fail:
337 xfrm_unregister_type(&mip6_destopt_type, AF_INET6);
338 mip6_destopt_xfrm_fail:
342 void __exit mip6_fini(void)
344 if (xfrm_unregister_type(&mip6_rthdr_type, AF_INET6) < 0)
345 printk(KERN_INFO "%s: can't remove xfrm type(rthdr)\n", __FUNCTION__);
346 if (xfrm_unregister_type(&mip6_destopt_type, AF_INET6) < 0)
347 printk(KERN_INFO "%s: can't remove xfrm type(destopt)\n", __FUNCTION__);