]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv6/mip6.c
[VLAN] vlan_dev: Use skb_reset_network_header().
[net-next-2.6.git] / net / ipv6 / mip6.c
CommitLineData
2c8d7ca0
NT
1/*
2 * Copyright (C)2003-2006 Helsinki University of Technology
3 * Copyright (C)2003-2006 USAGI/WIDE Project
4 *
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.
9 *
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.
14 *
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
18 */
19/*
20 * Authors:
21 * Noriaki TAKAMIYA @USAGI
22 * Masahide NAKAMURA @USAGI
23 */
24
2c8d7ca0
NT
25#include <linux/module.h>
26#include <linux/skbuff.h>
70182ed2 27#include <linux/time.h>
2c8d7ca0 28#include <linux/ipv6.h>
7be96f76
MN
29#include <linux/icmpv6.h>
30#include <net/sock.h>
2c8d7ca0 31#include <net/ipv6.h>
7be96f76 32#include <net/ip6_checksum.h>
2c8d7ca0
NT
33#include <net/xfrm.h>
34#include <net/mip6.h>
35
36static xfrm_address_t *mip6_xfrm_addr(struct xfrm_state *x, xfrm_address_t *addr)
37{
38 return x->coaddr;
39}
40
3d126890
NT
41static inline unsigned int calc_padlen(unsigned int len, unsigned int n)
42{
43 return (n - len + 16) & 0x7;
44}
45
46static inline void *mip6_padn(__u8 *data, __u8 padlen)
47{
48 if (!data)
49 return NULL;
50 if (padlen == 1) {
51 data[0] = MIP6_OPT_PAD_1;
52 } else if (padlen > 1) {
53 data[0] = MIP6_OPT_PAD_N;
54 data[1] = padlen - 2;
55 if (padlen > 2)
56 memset(data+2, 0, data[1]);
57 }
58 return data + padlen;
59}
60
7be96f76
MN
61static inline void mip6_param_prob(struct sk_buff *skb, int code, int pos)
62{
63 icmpv6_send(skb, ICMPV6_PARAMPROB, code, pos, skb->dev);
64}
65
66static int mip6_mh_len(int type)
67{
68 int len = 0;
69
70 switch (type) {
71 case IP6_MH_TYPE_BRR:
72 len = 0;
73 break;
74 case IP6_MH_TYPE_HOTI:
75 case IP6_MH_TYPE_COTI:
76 case IP6_MH_TYPE_BU:
77 case IP6_MH_TYPE_BACK:
78 len = 1;
79 break;
80 case IP6_MH_TYPE_HOT:
81 case IP6_MH_TYPE_COT:
82 case IP6_MH_TYPE_BERROR:
83 len = 2;
84 break;
85 }
86 return len;
87}
88
89int mip6_mh_filter(struct sock *sk, struct sk_buff *skb)
90{
91 struct ip6_mh *mh;
7be96f76 92
ea2ae17d
ACM
93 if (!pskb_may_pull(skb, (skb_transport_offset(skb)) + 8) ||
94 !pskb_may_pull(skb, (skb_transport_offset(skb) +
9c70220b 95 ((skb_transport_header(skb)[1] + 1) << 3))))
7be96f76
MN
96 return -1;
97
9c70220b 98 mh = (struct ip6_mh *)skb_transport_header(skb);
7be96f76
MN
99
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));
d56f90a7
ACM
103 mip6_param_prob(skb, 0, ((&mh->ip6mh_hdrlen) -
104 skb_network_header(skb)));
7be96f76
MN
105 return -1;
106 }
7be96f76
MN
107
108 if (mh->ip6mh_proto != IPPROTO_NONE) {
109 LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH invalid payload proto = %d\n",
110 mh->ip6mh_proto);
d56f90a7
ACM
111 mip6_param_prob(skb, 0, ((&mh->ip6mh_proto) -
112 skb_network_header(skb)));
7be96f76
MN
113 return -1;
114 }
115
116 return 0;
117}
118
70182ed2
MN
119struct mip6_report_rate_limiter {
120 spinlock_t lock;
121 struct timeval stamp;
122 int iif;
123 struct in6_addr src;
124 struct in6_addr dst;
125};
126
127static struct mip6_report_rate_limiter mip6_report_rl = {
128 .lock = SPIN_LOCK_UNLOCKED
129};
130
3d126890
NT
131static int mip6_destopt_input(struct xfrm_state *x, struct sk_buff *skb)
132{
0660e03f 133 struct ipv6hdr *iph = ipv6_hdr(skb);
3d126890
NT
134 struct ipv6_destopt_hdr *destopt = (struct ipv6_destopt_hdr *)skb->data;
135
136 if (!ipv6_addr_equal(&iph->saddr, (struct in6_addr *)x->coaddr) &&
137 !ipv6_addr_any((struct in6_addr *)x->coaddr))
138 return -ENOENT;
139
140 return destopt->nexthdr;
141}
142
143/* Destination Option Header is inserted.
144 * IP Header's src address is replaced with Home Address Option in
145 * Destination Option Header.
146 */
147static int mip6_destopt_output(struct xfrm_state *x, struct sk_buff *skb)
148{
149 struct ipv6hdr *iph;
150 struct ipv6_destopt_hdr *dstopt;
151 struct ipv6_destopt_hao *hao;
152 u8 nexthdr;
153 int len;
154
155 iph = (struct ipv6hdr *)skb->data;
156 iph->payload_len = htons(skb->len - sizeof(*iph));
157
d56f90a7
ACM
158 nexthdr = *skb_network_header(skb);
159 *skb_network_header(skb) = IPPROTO_DSTOPTS;
3d126890 160
9c70220b 161 dstopt = (struct ipv6_destopt_hdr *)skb_transport_header(skb);
3d126890
NT
162 dstopt->nexthdr = nexthdr;
163
164 hao = mip6_padn((char *)(dstopt + 1),
165 calc_padlen(sizeof(*dstopt), 6));
166
167 hao->type = IPV6_TLV_HAO;
168 hao->length = sizeof(*hao) - 2;
169 BUG_TRAP(hao->length == 16);
170
171 len = ((char *)hao - (char *)dstopt) + sizeof(*hao);
172
173 memcpy(&hao->addr, &iph->saddr, sizeof(hao->addr));
174 memcpy(&iph->saddr, x->coaddr, sizeof(iph->saddr));
175
176 BUG_TRAP(len == x->props.header_len);
177 dstopt->hdrlen = (x->props.header_len >> 3) - 1;
178
179 return 0;
180}
181
70182ed2
MN
182static inline int mip6_report_rl_allow(struct timeval *stamp,
183 struct in6_addr *dst,
184 struct in6_addr *src, int iif)
185{
186 int allow = 0;
187
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);
199 allow = 1;
200 }
201 spin_unlock_bh(&mip6_report_rl.lock);
202 return allow;
203}
204
205static int mip6_destopt_reject(struct xfrm_state *x, struct sk_buff *skb, struct flowi *fl)
206{
207 struct inet6_skb_parm *opt = (struct inet6_skb_parm *)skb->cb;
208 struct ipv6_destopt_hao *hao = NULL;
209 struct xfrm_selector sel;
210 int offset;
211 struct timeval stamp;
212 int err = 0;
213
e731c248
YH
214 if (unlikely(fl->proto == IPPROTO_MH &&
215 fl->fl_mh_type <= IP6_MH_TYPE_MAX))
01be8e5d
MN
216 goto out;
217
70182ed2
MN
218 if (likely(opt->dsthao)) {
219 offset = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
220 if (likely(offset >= 0))
d56f90a7
ACM
221 hao = (struct ipv6_destopt_hao *)
222 (skb_network_header(skb) + offset);
70182ed2
MN
223 }
224
225 skb_get_timestamp(skb, &stamp);
226
0660e03f
ACM
227 if (!mip6_report_rl_allow(&stamp, &ipv6_hdr(skb)->daddr,
228 hao ? &hao->addr : &ipv6_hdr(skb)->saddr,
70182ed2
MN
229 opt->iif))
230 goto out;
231
232 memset(&sel, 0, sizeof(sel));
0660e03f 233 memcpy(&sel.daddr, (xfrm_address_t *)&ipv6_hdr(skb)->daddr,
70182ed2
MN
234 sizeof(sel.daddr));
235 sel.prefixlen_d = 128;
0660e03f 236 memcpy(&sel.saddr, (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
70182ed2
MN
237 sizeof(sel.saddr));
238 sel.prefixlen_s = 128;
239 sel.family = AF_INET6;
240 sel.proto = fl->proto;
241 sel.dport = xfrm_flowi_dport(fl);
242 if (sel.dport)
e69a4adc 243 sel.dport_mask = htons(~0);
70182ed2
MN
244 sel.sport = xfrm_flowi_sport(fl);
245 if (sel.sport)
e69a4adc 246 sel.sport_mask = htons(~0);
70182ed2
MN
247 sel.ifindex = fl->oif;
248
249 err = km_report(IPPROTO_DSTOPTS, &sel,
250 (hao ? (xfrm_address_t *)&hao->addr : NULL));
251
252 out:
253 return err;
254}
255
3d126890
NT
256static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
257 u8 **nexthdr)
258{
259 u16 offset = sizeof(struct ipv6hdr);
0660e03f
ACM
260 struct ipv6_opt_hdr *exthdr =
261 (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
d56f90a7
ACM
262 const unsigned char *nh = skb_network_header(skb);
263 unsigned int packet_len = skb->tail - nh;
3d126890
NT
264 int found_rhdr = 0;
265
0660e03f 266 *nexthdr = &ipv6_hdr(skb)->nexthdr;
3d126890
NT
267
268 while (offset + 1 <= packet_len) {
269
270 switch (**nexthdr) {
271 case NEXTHDR_HOP:
272 break;
273 case NEXTHDR_ROUTING:
274 found_rhdr = 1;
275 break;
276 case NEXTHDR_DEST:
277 /*
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.
281 */
282 if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0) {
283 LIMIT_NETDEBUG(KERN_WARNING "mip6: hao exists already, override\n");
284 return offset;
285 }
286
287 if (found_rhdr)
288 return offset;
289
290 break;
291 default:
292 return offset;
293 }
294
295 offset += ipv6_optlen(exthdr);
296 *nexthdr = &exthdr->nexthdr;
d56f90a7 297 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
3d126890
NT
298 }
299
300 return offset;
301}
302
303static int mip6_destopt_init_state(struct xfrm_state *x)
304{
305 if (x->id.spi) {
306 printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
307 x->id.spi);
308 return -EINVAL;
309 }
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);
313 return -EINVAL;
314 }
315
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);
320
321 return 0;
322}
323
324/*
325 * Do nothing about destroying since it has no specific operation for
326 * destination options header unlike IPsec protocols.
327 */
328static void mip6_destopt_destroy(struct xfrm_state *x)
329{
330}
331
332static struct xfrm_type mip6_destopt_type =
333{
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,
1ab1457c 342 .reject = mip6_destopt_reject,
3d126890
NT
343 .hdr_offset = mip6_destopt_offset,
344 .local_addr = mip6_xfrm_addr,
345};
346
2c8d7ca0
NT
347static int mip6_rthdr_input(struct xfrm_state *x, struct sk_buff *skb)
348{
349 struct rt2_hdr *rt2 = (struct rt2_hdr *)skb->data;
350
351 if (!ipv6_addr_equal(&rt2->addr, (struct in6_addr *)x->coaddr) &&
352 !ipv6_addr_any((struct in6_addr *)x->coaddr))
353 return -ENOENT;
354
355 return rt2->rt_hdr.nexthdr;
356}
357
358/* Routing Header type 2 is inserted.
359 * IP Header's dst address is replaced with Routing Header's Home Address.
360 */
361static int mip6_rthdr_output(struct xfrm_state *x, struct sk_buff *skb)
362{
363 struct ipv6hdr *iph;
364 struct rt2_hdr *rt2;
365 u8 nexthdr;
366
367 iph = (struct ipv6hdr *)skb->data;
368 iph->payload_len = htons(skb->len - sizeof(*iph));
369
d56f90a7
ACM
370 nexthdr = *skb_network_header(skb);
371 *skb_network_header(skb) = IPPROTO_ROUTING;
2c8d7ca0 372
9c70220b 373 rt2 = (struct rt2_hdr *)skb_transport_header(skb);
2c8d7ca0
NT
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));
379
380 BUG_TRAP(rt2->rt_hdr.hdrlen == 2);
381
382 memcpy(&rt2->addr, &iph->daddr, sizeof(rt2->addr));
383 memcpy(&iph->daddr, x->coaddr, sizeof(iph->daddr));
384
385 return 0;
386}
387
388static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb,
389 u8 **nexthdr)
390{
391 u16 offset = sizeof(struct ipv6hdr);
0660e03f
ACM
392 struct ipv6_opt_hdr *exthdr =
393 (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
d56f90a7
ACM
394 const unsigned char *nh = skb_network_header(skb);
395 unsigned int packet_len = skb->tail - nh;
2c8d7ca0
NT
396 int found_rhdr = 0;
397
0660e03f 398 *nexthdr = &ipv6_hdr(skb)->nexthdr;
2c8d7ca0
NT
399
400 while (offset + 1 <= packet_len) {
401
402 switch (**nexthdr) {
403 case NEXTHDR_HOP:
404 break;
405 case NEXTHDR_ROUTING:
406 if (offset + 3 <= packet_len) {
407 struct ipv6_rt_hdr *rt;
d56f90a7 408 rt = (struct ipv6_rt_hdr *)(nh + offset);
2c8d7ca0
NT
409 if (rt->type != 0)
410 return offset;
411 }
412 found_rhdr = 1;
413 break;
414 case NEXTHDR_DEST:
415 if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
416 return offset;
417
418 if (found_rhdr)
419 return offset;
420
421 break;
422 default:
423 return offset;
424 }
425
426 offset += ipv6_optlen(exthdr);
427 *nexthdr = &exthdr->nexthdr;
d56f90a7 428 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
2c8d7ca0
NT
429 }
430
431 return offset;
432}
433
434static int mip6_rthdr_init_state(struct xfrm_state *x)
435{
436 if (x->id.spi) {
437 printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
438 x->id.spi);
439 return -EINVAL;
440 }
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);
444 return -EINVAL;
445 }
446
447 x->props.header_len = sizeof(struct rt2_hdr);
448
449 return 0;
450}
451
452/*
453 * Do nothing about destroying since it has no specific operation for routing
454 * header type 2 unlike IPsec protocols.
455 */
456static void mip6_rthdr_destroy(struct xfrm_state *x)
457{
458}
459
460static struct xfrm_type mip6_rthdr_type =
461{
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,
472};
473
474int __init mip6_init(void)
475{
476 printk(KERN_INFO "Mobile IPv6\n");
477
3d126890
NT
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;
481 }
2c8d7ca0
NT
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;
485 }
486 return 0;
487
488 mip6_rthdr_xfrm_fail:
3d126890
NT
489 xfrm_unregister_type(&mip6_destopt_type, AF_INET6);
490 mip6_destopt_xfrm_fail:
2c8d7ca0
NT
491 return -EAGAIN;
492}
493
494void __exit mip6_fini(void)
495{
496 if (xfrm_unregister_type(&mip6_rthdr_type, AF_INET6) < 0)
497 printk(KERN_INFO "%s: can't remove xfrm type(rthdr)\n", __FUNCTION__);
3d126890
NT
498 if (xfrm_unregister_type(&mip6_destopt_type, AF_INET6) < 0)
499 printk(KERN_INFO "%s: can't remove xfrm type(destopt)\n", __FUNCTION__);
2c8d7ca0 500}