Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / net / ipv6 / ip6_tunnel.c
blob24f0729877968613685c64c48d70243a3de9b35a
1 /*
2 * IPv6 tunneling device
3 * Linux INET6 implementation
5 * Authors:
6 * Ville Nuorvala <vnuorval@tcs.hut.fi>
7 * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
9 * $Id$
11 * Based on:
12 * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
14 * RFC 2473
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
23 #include <linux/module.h>
24 #include <linux/capability.h>
25 #include <linux/errno.h>
26 #include <linux/types.h>
27 #include <linux/sockios.h>
28 #include <linux/icmp.h>
29 #include <linux/if.h>
30 #include <linux/in.h>
31 #include <linux/ip.h>
32 #include <linux/if_tunnel.h>
33 #include <linux/net.h>
34 #include <linux/in6.h>
35 #include <linux/netdevice.h>
36 #include <linux/if_arp.h>
37 #include <linux/icmpv6.h>
38 #include <linux/init.h>
39 #include <linux/route.h>
40 #include <linux/rtnetlink.h>
41 #include <linux/netfilter_ipv6.h>
43 #include <asm/uaccess.h>
44 #include <asm/atomic.h>
46 #include <net/icmp.h>
47 #include <net/ip.h>
48 #include <net/ipv6.h>
49 #include <net/ip6_route.h>
50 #include <net/addrconf.h>
51 #include <net/ip6_tunnel.h>
52 #include <net/xfrm.h>
53 #include <net/dsfield.h>
54 #include <net/inet_ecn.h>
56 MODULE_AUTHOR("Ville Nuorvala");
57 MODULE_DESCRIPTION("IPv6 tunneling device");
58 MODULE_LICENSE("GPL");
60 #define IPV6_TLV_TEL_DST_SIZE 8
62 #ifdef IP6_TNL_DEBUG
63 #define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __FUNCTION__)
64 #else
65 #define IP6_TNL_TRACE(x...) do {;} while(0)
66 #endif
68 #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
69 #define IPV6_TCLASS_SHIFT 20
71 #define HASH_SIZE 32
73 #define HASH(addr) ((__force u32)((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
74 (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
75 (HASH_SIZE - 1))
77 static int ip6_fb_tnl_dev_init(struct net_device *dev);
78 static int ip6_tnl_dev_init(struct net_device *dev);
79 static void ip6_tnl_dev_setup(struct net_device *dev);
81 /* the IPv6 tunnel fallback device */
82 static struct net_device *ip6_fb_tnl_dev;
85 /* lists for storing tunnels in use */
86 static struct ip6_tnl *tnls_r_l[HASH_SIZE];
87 static struct ip6_tnl *tnls_wc[1];
88 static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l };
90 /* lock for the tunnel lists */
91 static DEFINE_RWLOCK(ip6_tnl_lock);
93 static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
95 struct dst_entry *dst = t->dst_cache;
97 if (dst && dst->obsolete &&
98 dst->ops->check(dst, t->dst_cookie) == NULL) {
99 t->dst_cache = NULL;
100 dst_release(dst);
101 return NULL;
104 return dst;
107 static inline void ip6_tnl_dst_reset(struct ip6_tnl *t)
109 dst_release(t->dst_cache);
110 t->dst_cache = NULL;
113 static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
115 struct rt6_info *rt = (struct rt6_info *) dst;
116 t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
117 dst_release(t->dst_cache);
118 t->dst_cache = dst;
122 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
123 * @remote: the address of the tunnel exit-point
124 * @local: the address of the tunnel entry-point
126 * Return:
127 * tunnel matching given end-points if found,
128 * else fallback tunnel if its device is up,
129 * else %NULL
132 static struct ip6_tnl *
133 ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local)
135 unsigned h0 = HASH(remote);
136 unsigned h1 = HASH(local);
137 struct ip6_tnl *t;
139 for (t = tnls_r_l[h0 ^ h1]; t; t = t->next) {
140 if (ipv6_addr_equal(local, &t->parms.laddr) &&
141 ipv6_addr_equal(remote, &t->parms.raddr) &&
142 (t->dev->flags & IFF_UP))
143 return t;
145 if ((t = tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP))
146 return t;
148 return NULL;
152 * ip6_tnl_bucket - get head of list matching given tunnel parameters
153 * @p: parameters containing tunnel end-points
155 * Description:
156 * ip6_tnl_bucket() returns the head of the list matching the
157 * &struct in6_addr entries laddr and raddr in @p.
159 * Return: head of IPv6 tunnel list
162 static struct ip6_tnl **
163 ip6_tnl_bucket(struct ip6_tnl_parm *p)
165 struct in6_addr *remote = &p->raddr;
166 struct in6_addr *local = &p->laddr;
167 unsigned h = 0;
168 int prio = 0;
170 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
171 prio = 1;
172 h = HASH(remote) ^ HASH(local);
174 return &tnls[prio][h];
178 * ip6_tnl_link - add tunnel to hash table
179 * @t: tunnel to be added
182 static void
183 ip6_tnl_link(struct ip6_tnl *t)
185 struct ip6_tnl **tp = ip6_tnl_bucket(&t->parms);
187 t->next = *tp;
188 write_lock_bh(&ip6_tnl_lock);
189 *tp = t;
190 write_unlock_bh(&ip6_tnl_lock);
194 * ip6_tnl_unlink - remove tunnel from hash table
195 * @t: tunnel to be removed
198 static void
199 ip6_tnl_unlink(struct ip6_tnl *t)
201 struct ip6_tnl **tp;
203 for (tp = ip6_tnl_bucket(&t->parms); *tp; tp = &(*tp)->next) {
204 if (t == *tp) {
205 write_lock_bh(&ip6_tnl_lock);
206 *tp = t->next;
207 write_unlock_bh(&ip6_tnl_lock);
208 break;
214 * ip6_tnl_create() - create a new tunnel
215 * @p: tunnel parameters
216 * @pt: pointer to new tunnel
218 * Description:
219 * Create tunnel matching given parameters.
221 * Return:
222 * created tunnel or NULL
225 static struct ip6_tnl *ip6_tnl_create(struct ip6_tnl_parm *p)
227 struct net_device *dev;
228 struct ip6_tnl *t;
229 char name[IFNAMSIZ];
230 int err;
232 <<<<<<< HEAD:net/ipv6/ip6_tunnel.c
233 if (p->name[0]) {
234 =======
235 if (p->name[0])
236 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/ipv6/ip6_tunnel.c
237 strlcpy(name, p->name, IFNAMSIZ);
238 <<<<<<< HEAD:net/ipv6/ip6_tunnel.c
239 } else {
240 int i;
241 for (i = 1; i < IP6_TNL_MAX; i++) {
242 sprintf(name, "ip6tnl%d", i);
243 if (__dev_get_by_name(&init_net, name) == NULL)
244 break;
246 if (i == IP6_TNL_MAX)
247 goto failed;
249 =======
250 else
251 sprintf(name, "ip6tnl%%d");
253 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/ipv6/ip6_tunnel.c
254 dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup);
255 if (dev == NULL)
256 goto failed;
258 <<<<<<< HEAD:net/ipv6/ip6_tunnel.c
259 =======
260 if (strchr(name, '%')) {
261 if (dev_alloc_name(dev, name) < 0)
262 goto failed_free;
265 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/ipv6/ip6_tunnel.c
266 t = netdev_priv(dev);
267 dev->init = ip6_tnl_dev_init;
268 t->parms = *p;
270 <<<<<<< HEAD:net/ipv6/ip6_tunnel.c
271 if ((err = register_netdevice(dev)) < 0) {
272 free_netdev(dev);
273 goto failed;
275 =======
276 if ((err = register_netdevice(dev)) < 0)
277 goto failed_free;
279 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/ipv6/ip6_tunnel.c
280 dev_hold(dev);
281 ip6_tnl_link(t);
282 return t;
283 <<<<<<< HEAD:net/ipv6/ip6_tunnel.c
284 =======
286 failed_free:
287 free_netdev(dev);
288 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/ipv6/ip6_tunnel.c
289 failed:
290 return NULL;
294 * ip6_tnl_locate - find or create tunnel matching given parameters
295 * @p: tunnel parameters
296 * @create: != 0 if allowed to create new tunnel if no match found
298 * Description:
299 * ip6_tnl_locate() first tries to locate an existing tunnel
300 * based on @parms. If this is unsuccessful, but @create is set a new
301 * tunnel device is created and registered for use.
303 * Return:
304 * matching tunnel or NULL
307 static struct ip6_tnl *ip6_tnl_locate(struct ip6_tnl_parm *p, int create)
309 struct in6_addr *remote = &p->raddr;
310 struct in6_addr *local = &p->laddr;
311 struct ip6_tnl *t;
313 for (t = *ip6_tnl_bucket(p); t; t = t->next) {
314 if (ipv6_addr_equal(local, &t->parms.laddr) &&
315 ipv6_addr_equal(remote, &t->parms.raddr))
316 return t;
318 if (!create)
319 return NULL;
320 return ip6_tnl_create(p);
324 * ip6_tnl_dev_uninit - tunnel device uninitializer
325 * @dev: the device to be destroyed
327 * Description:
328 * ip6_tnl_dev_uninit() removes tunnel from its list
331 static void
332 ip6_tnl_dev_uninit(struct net_device *dev)
334 struct ip6_tnl *t = netdev_priv(dev);
336 if (dev == ip6_fb_tnl_dev) {
337 write_lock_bh(&ip6_tnl_lock);
338 tnls_wc[0] = NULL;
339 write_unlock_bh(&ip6_tnl_lock);
340 } else {
341 ip6_tnl_unlink(t);
343 ip6_tnl_dst_reset(t);
344 dev_put(dev);
348 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
349 * @skb: received socket buffer
351 * Return:
352 * 0 if none was found,
353 * else index to encapsulation limit
356 static __u16
357 parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
359 struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw;
360 __u8 nexthdr = ipv6h->nexthdr;
361 __u16 off = sizeof (*ipv6h);
363 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
364 __u16 optlen = 0;
365 struct ipv6_opt_hdr *hdr;
366 if (raw + off + sizeof (*hdr) > skb->data &&
367 !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
368 break;
370 hdr = (struct ipv6_opt_hdr *) (raw + off);
371 if (nexthdr == NEXTHDR_FRAGMENT) {
372 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
373 if (frag_hdr->frag_off)
374 break;
375 optlen = 8;
376 } else if (nexthdr == NEXTHDR_AUTH) {
377 optlen = (hdr->hdrlen + 2) << 2;
378 } else {
379 optlen = ipv6_optlen(hdr);
381 if (nexthdr == NEXTHDR_DEST) {
382 __u16 i = off + 2;
383 while (1) {
384 struct ipv6_tlv_tnl_enc_lim *tel;
386 /* No more room for encapsulation limit */
387 if (i + sizeof (*tel) > off + optlen)
388 break;
390 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
391 /* return index of option if found and valid */
392 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
393 tel->length == 1)
394 return i;
395 /* else jump to next option */
396 if (tel->type)
397 i += tel->length + 2;
398 else
399 i++;
402 nexthdr = hdr->nexthdr;
403 off += optlen;
405 return 0;
409 * ip6_tnl_err - tunnel error handler
411 * Description:
412 * ip6_tnl_err() should handle errors in the tunnel according
413 * to the specifications in RFC 2473.
416 static int
417 ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
418 int *type, int *code, int *msg, __u32 *info, int offset)
420 struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
421 struct ip6_tnl *t;
422 int rel_msg = 0;
423 int rel_type = ICMPV6_DEST_UNREACH;
424 int rel_code = ICMPV6_ADDR_UNREACH;
425 __u32 rel_info = 0;
426 __u16 len;
427 int err = -ENOENT;
429 /* If the packet doesn't contain the original IPv6 header we are
430 in trouble since we might need the source address for further
431 processing of the error. */
433 read_lock(&ip6_tnl_lock);
434 if ((t = ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
435 goto out;
437 if (t->parms.proto != ipproto && t->parms.proto != 0)
438 goto out;
440 err = 0;
442 switch (*type) {
443 __u32 teli;
444 struct ipv6_tlv_tnl_enc_lim *tel;
445 __u32 mtu;
446 case ICMPV6_DEST_UNREACH:
447 if (net_ratelimit())
448 printk(KERN_WARNING
449 "%s: Path to destination invalid "
450 "or inactive!\n", t->parms.name);
451 rel_msg = 1;
452 break;
453 case ICMPV6_TIME_EXCEED:
454 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
455 if (net_ratelimit())
456 printk(KERN_WARNING
457 "%s: Too small hop limit or "
458 "routing loop in tunnel!\n",
459 t->parms.name);
460 rel_msg = 1;
462 break;
463 case ICMPV6_PARAMPROB:
464 teli = 0;
465 if ((*code) == ICMPV6_HDR_FIELD)
466 teli = parse_tlv_tnl_enc_lim(skb, skb->data);
468 if (teli && teli == *info - 2) {
469 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
470 if (tel->encap_limit == 0) {
471 if (net_ratelimit())
472 printk(KERN_WARNING
473 "%s: Too small encapsulation "
474 "limit or routing loop in "
475 "tunnel!\n", t->parms.name);
476 rel_msg = 1;
478 } else if (net_ratelimit()) {
479 printk(KERN_WARNING
480 "%s: Recipient unable to parse tunneled "
481 "packet!\n ", t->parms.name);
483 break;
484 case ICMPV6_PKT_TOOBIG:
485 mtu = *info - offset;
486 if (mtu < IPV6_MIN_MTU)
487 mtu = IPV6_MIN_MTU;
488 t->dev->mtu = mtu;
490 if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
491 rel_type = ICMPV6_PKT_TOOBIG;
492 rel_code = 0;
493 rel_info = mtu;
494 rel_msg = 1;
496 break;
499 *type = rel_type;
500 *code = rel_code;
501 *info = rel_info;
502 *msg = rel_msg;
504 out:
505 read_unlock(&ip6_tnl_lock);
506 return err;
509 static int
510 ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
511 int type, int code, int offset, __be32 info)
513 int rel_msg = 0;
514 int rel_type = type;
515 int rel_code = code;
516 __u32 rel_info = ntohl(info);
517 int err;
518 struct sk_buff *skb2;
519 struct iphdr *eiph;
520 struct flowi fl;
521 struct rtable *rt;
523 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
524 &rel_msg, &rel_info, offset);
525 if (err < 0)
526 return err;
528 if (rel_msg == 0)
529 return 0;
531 switch (rel_type) {
532 case ICMPV6_DEST_UNREACH:
533 if (rel_code != ICMPV6_ADDR_UNREACH)
534 return 0;
535 rel_type = ICMP_DEST_UNREACH;
536 rel_code = ICMP_HOST_UNREACH;
537 break;
538 case ICMPV6_PKT_TOOBIG:
539 if (rel_code != 0)
540 return 0;
541 rel_type = ICMP_DEST_UNREACH;
542 rel_code = ICMP_FRAG_NEEDED;
543 break;
544 default:
545 return 0;
548 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
549 return 0;
551 skb2 = skb_clone(skb, GFP_ATOMIC);
552 if (!skb2)
553 return 0;
555 dst_release(skb2->dst);
556 skb2->dst = NULL;
557 skb_pull(skb2, offset);
558 skb_reset_network_header(skb2);
559 eiph = ip_hdr(skb2);
561 /* Try to guess incoming interface */
562 memset(&fl, 0, sizeof(fl));
563 fl.fl4_dst = eiph->saddr;
564 fl.fl4_tos = RT_TOS(eiph->tos);
565 fl.proto = IPPROTO_IPIP;
566 if (ip_route_output_key(&init_net, &rt, &fl))
567 goto out;
569 skb2->dev = rt->u.dst.dev;
571 /* route "incoming" packet */
572 if (rt->rt_flags & RTCF_LOCAL) {
573 ip_rt_put(rt);
574 rt = NULL;
575 fl.fl4_dst = eiph->daddr;
576 fl.fl4_src = eiph->saddr;
577 fl.fl4_tos = eiph->tos;
578 if (ip_route_output_key(&init_net, &rt, &fl) ||
579 rt->u.dst.dev->type != ARPHRD_TUNNEL) {
580 ip_rt_put(rt);
581 goto out;
583 <<<<<<< HEAD:net/ipv6/ip6_tunnel.c
584 =======
585 skb2->dst = (struct dst_entry *)rt;
586 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/ipv6/ip6_tunnel.c
587 } else {
588 ip_rt_put(rt);
589 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
590 skb2->dev) ||
591 skb2->dst->dev->type != ARPHRD_TUNNEL)
592 goto out;
595 /* change mtu on this route */
596 if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
597 if (rel_info > dst_mtu(skb2->dst))
598 goto out;
600 skb2->dst->ops->update_pmtu(skb2->dst, rel_info);
603 icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
605 out:
606 kfree_skb(skb2);
607 return 0;
610 static int
611 ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
612 int type, int code, int offset, __be32 info)
614 int rel_msg = 0;
615 int rel_type = type;
616 int rel_code = code;
617 __u32 rel_info = ntohl(info);
618 int err;
620 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
621 &rel_msg, &rel_info, offset);
622 if (err < 0)
623 return err;
625 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
626 struct rt6_info *rt;
627 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
629 if (!skb2)
630 return 0;
632 dst_release(skb2->dst);
633 skb2->dst = NULL;
634 skb_pull(skb2, offset);
635 skb_reset_network_header(skb2);
637 /* Try to guess incoming interface */
638 rt = rt6_lookup(&ipv6_hdr(skb2)->saddr, NULL, 0, 0);
640 if (rt && rt->rt6i_dev)
641 skb2->dev = rt->rt6i_dev;
643 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
645 if (rt)
646 dst_release(&rt->u.dst);
648 kfree_skb(skb2);
651 return 0;
654 static void ip4ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
655 struct ipv6hdr *ipv6h,
656 struct sk_buff *skb)
658 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
660 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
661 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
663 if (INET_ECN_is_ce(dsfield))
664 IP_ECN_set_ce(ip_hdr(skb));
667 static void ip6ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
668 struct ipv6hdr *ipv6h,
669 struct sk_buff *skb)
671 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
672 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
674 if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
675 IP6_ECN_set_ce(ipv6_hdr(skb));
678 static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
680 struct ip6_tnl_parm *p = &t->parms;
681 int ret = 0;
683 if (p->flags & IP6_TNL_F_CAP_RCV) {
684 struct net_device *ldev = NULL;
686 if (p->link)
687 ldev = dev_get_by_index(&init_net, p->link);
689 if ((ipv6_addr_is_multicast(&p->laddr) ||
690 likely(ipv6_chk_addr(&init_net, &p->laddr, ldev, 0))) &&
691 likely(!ipv6_chk_addr(&init_net, &p->raddr, NULL, 0)))
692 ret = 1;
694 if (ldev)
695 dev_put(ldev);
697 return ret;
701 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
702 * @skb: received socket buffer
703 * @protocol: ethernet protocol ID
704 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
706 * Return: 0
709 static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
710 __u8 ipproto,
711 void (*dscp_ecn_decapsulate)(struct ip6_tnl *t,
712 struct ipv6hdr *ipv6h,
713 struct sk_buff *skb))
715 struct ip6_tnl *t;
716 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
718 read_lock(&ip6_tnl_lock);
720 if ((t = ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
721 if (t->parms.proto != ipproto && t->parms.proto != 0) {
722 read_unlock(&ip6_tnl_lock);
723 goto discard;
726 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
727 read_unlock(&ip6_tnl_lock);
728 goto discard;
731 if (!ip6_tnl_rcv_ctl(t)) {
732 t->stat.rx_dropped++;
733 read_unlock(&ip6_tnl_lock);
734 goto discard;
736 secpath_reset(skb);
737 skb->mac_header = skb->network_header;
738 skb_reset_network_header(skb);
739 skb->protocol = htons(protocol);
740 skb->pkt_type = PACKET_HOST;
741 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
742 skb->dev = t->dev;
743 dst_release(skb->dst);
744 skb->dst = NULL;
745 nf_reset(skb);
747 dscp_ecn_decapsulate(t, ipv6h, skb);
749 t->stat.rx_packets++;
750 t->stat.rx_bytes += skb->len;
751 netif_rx(skb);
752 read_unlock(&ip6_tnl_lock);
753 return 0;
755 read_unlock(&ip6_tnl_lock);
756 return 1;
758 discard:
759 kfree_skb(skb);
760 return 0;
763 static int ip4ip6_rcv(struct sk_buff *skb)
765 return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
766 ip4ip6_dscp_ecn_decapsulate);
769 static int ip6ip6_rcv(struct sk_buff *skb)
771 return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
772 ip6ip6_dscp_ecn_decapsulate);
775 struct ipv6_tel_txoption {
776 struct ipv6_txoptions ops;
777 __u8 dst_opt[8];
780 static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
782 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
784 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
785 opt->dst_opt[3] = 1;
786 opt->dst_opt[4] = encap_limit;
787 opt->dst_opt[5] = IPV6_TLV_PADN;
788 opt->dst_opt[6] = 1;
790 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
791 opt->ops.opt_nflen = 8;
795 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
796 * @t: the outgoing tunnel device
797 * @hdr: IPv6 header from the incoming packet
799 * Description:
800 * Avoid trivial tunneling loop by checking that tunnel exit-point
801 * doesn't match source of incoming packet.
803 * Return:
804 * 1 if conflict,
805 * 0 else
808 static inline int
809 ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
811 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
814 static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
816 struct ip6_tnl_parm *p = &t->parms;
817 int ret = 0;
819 if (p->flags & IP6_TNL_F_CAP_XMIT) {
820 struct net_device *ldev = NULL;
822 if (p->link)
823 ldev = dev_get_by_index(&init_net, p->link);
825 if (unlikely(!ipv6_chk_addr(&init_net, &p->laddr, ldev, 0)))
826 printk(KERN_WARNING
827 "%s xmit: Local address not yet configured!\n",
828 p->name);
829 else if (!ipv6_addr_is_multicast(&p->raddr) &&
830 unlikely(ipv6_chk_addr(&init_net, &p->raddr, NULL, 0)))
831 printk(KERN_WARNING
832 "%s xmit: Routing loop! "
833 "Remote address found on this node!\n",
834 p->name);
835 else
836 ret = 1;
837 if (ldev)
838 dev_put(ldev);
840 return ret;
843 * ip6_tnl_xmit2 - encapsulate packet and send
844 * @skb: the outgoing socket buffer
845 * @dev: the outgoing tunnel device
846 * @dsfield: dscp code for outer header
847 * @fl: flow of tunneled packet
848 * @encap_limit: encapsulation limit
849 * @pmtu: Path MTU is stored if packet is too big
851 * Description:
852 * Build new header and do some sanity checks on the packet before sending
853 * it.
855 * Return:
856 * 0 on success
857 * -1 fail
858 * %-EMSGSIZE message too big. return mtu in this case.
861 static int ip6_tnl_xmit2(struct sk_buff *skb,
862 struct net_device *dev,
863 __u8 dsfield,
864 struct flowi *fl,
865 int encap_limit,
866 __u32 *pmtu)
868 struct ip6_tnl *t = netdev_priv(dev);
869 struct net_device_stats *stats = &t->stat;
870 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
871 struct ipv6_tel_txoption opt;
872 struct dst_entry *dst;
873 struct net_device *tdev;
874 int mtu;
875 unsigned int max_headroom = sizeof(struct ipv6hdr);
876 u8 proto;
877 int err = -1;
878 int pkt_len;
880 if ((dst = ip6_tnl_dst_check(t)) != NULL)
881 dst_hold(dst);
882 else {
883 dst = ip6_route_output(NULL, fl);
885 if (dst->error || xfrm_lookup(&dst, fl, NULL, 0) < 0)
886 goto tx_err_link_failure;
889 tdev = dst->dev;
891 if (tdev == dev) {
892 stats->collisions++;
893 if (net_ratelimit())
894 printk(KERN_WARNING
895 "%s: Local routing loop detected!\n",
896 t->parms.name);
897 goto tx_err_dst_release;
899 mtu = dst_mtu(dst) - sizeof (*ipv6h);
900 if (encap_limit >= 0) {
901 max_headroom += 8;
902 mtu -= 8;
904 if (mtu < IPV6_MIN_MTU)
905 mtu = IPV6_MIN_MTU;
906 if (skb->dst)
907 skb->dst->ops->update_pmtu(skb->dst, mtu);
908 if (skb->len > mtu) {
909 *pmtu = mtu;
910 err = -EMSGSIZE;
911 goto tx_err_dst_release;
915 * Okay, now see if we can stuff it in the buffer as-is.
917 max_headroom += LL_RESERVED_SPACE(tdev);
919 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
920 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
921 struct sk_buff *new_skb;
923 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
924 goto tx_err_dst_release;
926 if (skb->sk)
927 skb_set_owner_w(new_skb, skb->sk);
928 kfree_skb(skb);
929 skb = new_skb;
931 dst_release(skb->dst);
932 skb->dst = dst_clone(dst);
934 skb->transport_header = skb->network_header;
936 proto = fl->proto;
937 if (encap_limit >= 0) {
938 init_tel_txopt(&opt, encap_limit);
939 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
941 skb_push(skb, sizeof(struct ipv6hdr));
942 skb_reset_network_header(skb);
943 ipv6h = ipv6_hdr(skb);
944 *(__be32*)ipv6h = fl->fl6_flowlabel | htonl(0x60000000);
945 dsfield = INET_ECN_encapsulate(0, dsfield);
946 ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
947 ipv6h->hop_limit = t->parms.hop_limit;
948 ipv6h->nexthdr = proto;
949 ipv6_addr_copy(&ipv6h->saddr, &fl->fl6_src);
950 ipv6_addr_copy(&ipv6h->daddr, &fl->fl6_dst);
951 nf_reset(skb);
952 pkt_len = skb->len;
953 err = ip6_local_out(skb);
955 if (net_xmit_eval(err) == 0) {
956 stats->tx_bytes += pkt_len;
957 stats->tx_packets++;
958 } else {
959 stats->tx_errors++;
960 stats->tx_aborted_errors++;
962 ip6_tnl_dst_store(t, dst);
963 return 0;
964 tx_err_link_failure:
965 stats->tx_carrier_errors++;
966 dst_link_failure(skb);
967 tx_err_dst_release:
968 dst_release(dst);
969 return err;
972 static inline int
973 ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
975 struct ip6_tnl *t = netdev_priv(dev);
976 struct iphdr *iph = ip_hdr(skb);
977 int encap_limit = -1;
978 struct flowi fl;
979 __u8 dsfield;
980 __u32 mtu;
981 int err;
983 if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
984 !ip6_tnl_xmit_ctl(t))
985 return -1;
987 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
988 encap_limit = t->parms.encap_limit;
990 memcpy(&fl, &t->fl, sizeof (fl));
991 fl.proto = IPPROTO_IPIP;
993 dsfield = ipv4_get_dsfield(iph);
995 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
996 fl.fl6_flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
997 & IPV6_TCLASS_MASK;
999 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
1000 if (err != 0) {
1001 /* XXX: send ICMP error even if DF is not set. */
1002 if (err == -EMSGSIZE)
1003 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
1004 htonl(mtu));
1005 return -1;
1008 return 0;
1011 static inline int
1012 ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1014 struct ip6_tnl *t = netdev_priv(dev);
1015 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
1016 int encap_limit = -1;
1017 __u16 offset;
1018 struct flowi fl;
1019 __u8 dsfield;
1020 __u32 mtu;
1021 int err;
1023 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
1024 !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))
1025 return -1;
1027 offset = parse_tlv_tnl_enc_lim(skb, skb_network_header(skb));
1028 if (offset > 0) {
1029 struct ipv6_tlv_tnl_enc_lim *tel;
1030 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
1031 if (tel->encap_limit == 0) {
1032 icmpv6_send(skb, ICMPV6_PARAMPROB,
1033 ICMPV6_HDR_FIELD, offset + 2, skb->dev);
1034 return -1;
1036 encap_limit = tel->encap_limit - 1;
1037 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1038 encap_limit = t->parms.encap_limit;
1040 memcpy(&fl, &t->fl, sizeof (fl));
1041 fl.proto = IPPROTO_IPV6;
1043 dsfield = ipv6_get_dsfield(ipv6h);
1044 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
1045 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
1046 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
1047 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
1049 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
1050 if (err != 0) {
1051 if (err == -EMSGSIZE)
1052 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
1053 return -1;
1056 return 0;
1059 static int
1060 ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1062 struct ip6_tnl *t = netdev_priv(dev);
1063 struct net_device_stats *stats = &t->stat;
1064 int ret;
1066 if (t->recursion++) {
1067 t->stat.collisions++;
1068 goto tx_err;
1071 switch (skb->protocol) {
1072 case __constant_htons(ETH_P_IP):
1073 ret = ip4ip6_tnl_xmit(skb, dev);
1074 break;
1075 case __constant_htons(ETH_P_IPV6):
1076 ret = ip6ip6_tnl_xmit(skb, dev);
1077 break;
1078 default:
1079 goto tx_err;
1082 if (ret < 0)
1083 goto tx_err;
1085 t->recursion--;
1086 return 0;
1088 tx_err:
1089 stats->tx_errors++;
1090 stats->tx_dropped++;
1091 kfree_skb(skb);
1092 t->recursion--;
1093 return 0;
1096 static void ip6_tnl_set_cap(struct ip6_tnl *t)
1098 struct ip6_tnl_parm *p = &t->parms;
1099 int ltype = ipv6_addr_type(&p->laddr);
1100 int rtype = ipv6_addr_type(&p->raddr);
1102 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
1104 if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
1105 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
1106 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
1107 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
1108 if (ltype&IPV6_ADDR_UNICAST)
1109 p->flags |= IP6_TNL_F_CAP_XMIT;
1110 if (rtype&IPV6_ADDR_UNICAST)
1111 p->flags |= IP6_TNL_F_CAP_RCV;
1115 static void ip6_tnl_link_config(struct ip6_tnl *t)
1117 struct net_device *dev = t->dev;
1118 struct ip6_tnl_parm *p = &t->parms;
1119 struct flowi *fl = &t->fl;
1121 memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1122 memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1124 /* Set up flowi template */
1125 ipv6_addr_copy(&fl->fl6_src, &p->laddr);
1126 ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
1127 fl->oif = p->link;
1128 fl->fl6_flowlabel = 0;
1130 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1131 fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1132 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1133 fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1135 ip6_tnl_set_cap(t);
1137 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1138 dev->flags |= IFF_POINTOPOINT;
1139 else
1140 dev->flags &= ~IFF_POINTOPOINT;
1142 dev->iflink = p->link;
1144 if (p->flags & IP6_TNL_F_CAP_XMIT) {
1145 int strict = (ipv6_addr_type(&p->raddr) &
1146 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1148 struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
1149 p->link, strict);
1151 if (rt == NULL)
1152 return;
1154 if (rt->rt6i_dev) {
1155 dev->hard_header_len = rt->rt6i_dev->hard_header_len +
1156 sizeof (struct ipv6hdr);
1158 dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
1160 if (dev->mtu < IPV6_MIN_MTU)
1161 dev->mtu = IPV6_MIN_MTU;
1163 dst_release(&rt->u.dst);
1168 * ip6_tnl_change - update the tunnel parameters
1169 * @t: tunnel to be changed
1170 * @p: tunnel configuration parameters
1171 * @active: != 0 if tunnel is ready for use
1173 * Description:
1174 * ip6_tnl_change() updates the tunnel parameters
1177 static int
1178 ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
1180 ipv6_addr_copy(&t->parms.laddr, &p->laddr);
1181 ipv6_addr_copy(&t->parms.raddr, &p->raddr);
1182 t->parms.flags = p->flags;
1183 t->parms.hop_limit = p->hop_limit;
1184 t->parms.encap_limit = p->encap_limit;
1185 t->parms.flowinfo = p->flowinfo;
1186 t->parms.link = p->link;
1187 t->parms.proto = p->proto;
1188 ip6_tnl_dst_reset(t);
1189 ip6_tnl_link_config(t);
1190 return 0;
1194 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
1195 * @dev: virtual device associated with tunnel
1196 * @ifr: parameters passed from userspace
1197 * @cmd: command to be performed
1199 * Description:
1200 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
1201 * from userspace.
1203 * The possible commands are the following:
1204 * %SIOCGETTUNNEL: get tunnel parameters for device
1205 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1206 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1207 * %SIOCDELTUNNEL: delete tunnel
1209 * The fallback device "ip6tnl0", created during module
1210 * initialization, can be used for creating other tunnel devices.
1212 * Return:
1213 * 0 on success,
1214 * %-EFAULT if unable to copy data to or from userspace,
1215 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1216 * %-EINVAL if passed tunnel parameters are invalid,
1217 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1218 * %-ENODEV if attempting to change or delete a nonexisting device
1221 static int
1222 ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1224 int err = 0;
1225 struct ip6_tnl_parm p;
1226 struct ip6_tnl *t = NULL;
1228 switch (cmd) {
1229 case SIOCGETTUNNEL:
1230 if (dev == ip6_fb_tnl_dev) {
1231 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
1232 err = -EFAULT;
1233 break;
1235 t = ip6_tnl_locate(&p, 0);
1237 if (t == NULL)
1238 t = netdev_priv(dev);
1239 memcpy(&p, &t->parms, sizeof (p));
1240 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
1241 err = -EFAULT;
1243 break;
1244 case SIOCADDTUNNEL:
1245 case SIOCCHGTUNNEL:
1246 err = -EPERM;
1247 if (!capable(CAP_NET_ADMIN))
1248 break;
1249 err = -EFAULT;
1250 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
1251 break;
1252 err = -EINVAL;
1253 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1254 p.proto != 0)
1255 break;
1256 t = ip6_tnl_locate(&p, cmd == SIOCADDTUNNEL);
1257 if (dev != ip6_fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
1258 if (t != NULL) {
1259 if (t->dev != dev) {
1260 err = -EEXIST;
1261 break;
1263 } else
1264 t = netdev_priv(dev);
1266 ip6_tnl_unlink(t);
1267 err = ip6_tnl_change(t, &p);
1268 ip6_tnl_link(t);
1269 netdev_state_change(dev);
1271 if (t) {
1272 err = 0;
1273 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))
1274 err = -EFAULT;
1276 } else
1277 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1278 break;
1279 case SIOCDELTUNNEL:
1280 err = -EPERM;
1281 if (!capable(CAP_NET_ADMIN))
1282 break;
1284 if (dev == ip6_fb_tnl_dev) {
1285 err = -EFAULT;
1286 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
1287 break;
1288 err = -ENOENT;
1289 if ((t = ip6_tnl_locate(&p, 0)) == NULL)
1290 break;
1291 err = -EPERM;
1292 if (t->dev == ip6_fb_tnl_dev)
1293 break;
1294 dev = t->dev;
1296 err = 0;
1297 unregister_netdevice(dev);
1298 break;
1299 default:
1300 err = -EINVAL;
1302 return err;
1306 * ip6_tnl_get_stats - return the stats for tunnel device
1307 * @dev: virtual device associated with tunnel
1309 * Return: stats for device
1312 static struct net_device_stats *
1313 ip6_tnl_get_stats(struct net_device *dev)
1315 return &(((struct ip6_tnl *)netdev_priv(dev))->stat);
1319 * ip6_tnl_change_mtu - change mtu manually for tunnel device
1320 * @dev: virtual device associated with tunnel
1321 * @new_mtu: the new mtu
1323 * Return:
1324 * 0 on success,
1325 * %-EINVAL if mtu too small
1328 static int
1329 ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1331 if (new_mtu < IPV6_MIN_MTU) {
1332 return -EINVAL;
1334 dev->mtu = new_mtu;
1335 return 0;
1339 * ip6_tnl_dev_setup - setup virtual tunnel device
1340 * @dev: virtual device associated with tunnel
1342 * Description:
1343 * Initialize function pointers and device parameters
1346 static void ip6_tnl_dev_setup(struct net_device *dev)
1348 dev->uninit = ip6_tnl_dev_uninit;
1349 dev->destructor = free_netdev;
1350 dev->hard_start_xmit = ip6_tnl_xmit;
1351 dev->get_stats = ip6_tnl_get_stats;
1352 dev->do_ioctl = ip6_tnl_ioctl;
1353 dev->change_mtu = ip6_tnl_change_mtu;
1355 dev->type = ARPHRD_TUNNEL6;
1356 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1357 dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
1358 dev->flags |= IFF_NOARP;
1359 dev->addr_len = sizeof(struct in6_addr);
1364 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1365 * @dev: virtual device associated with tunnel
1368 static inline void
1369 ip6_tnl_dev_init_gen(struct net_device *dev)
1371 struct ip6_tnl *t = netdev_priv(dev);
1372 t->dev = dev;
1373 strcpy(t->parms.name, dev->name);
1377 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1378 * @dev: virtual device associated with tunnel
1381 static int
1382 ip6_tnl_dev_init(struct net_device *dev)
1384 struct ip6_tnl *t = netdev_priv(dev);
1385 ip6_tnl_dev_init_gen(dev);
1386 ip6_tnl_link_config(t);
1387 return 0;
1391 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1392 * @dev: fallback device
1394 * Return: 0
1397 static int
1398 ip6_fb_tnl_dev_init(struct net_device *dev)
1400 struct ip6_tnl *t = netdev_priv(dev);
1401 ip6_tnl_dev_init_gen(dev);
1402 t->parms.proto = IPPROTO_IPV6;
1403 dev_hold(dev);
1404 tnls_wc[0] = t;
1405 return 0;
1408 static struct xfrm6_tunnel ip4ip6_handler = {
1409 .handler = ip4ip6_rcv,
1410 .err_handler = ip4ip6_err,
1411 .priority = 1,
1414 static struct xfrm6_tunnel ip6ip6_handler = {
1415 .handler = ip6ip6_rcv,
1416 .err_handler = ip6ip6_err,
1417 .priority = 1,
1421 * ip6_tunnel_init - register protocol and reserve needed resources
1423 * Return: 0 on success
1426 static int __init ip6_tunnel_init(void)
1428 int err;
1430 if (xfrm6_tunnel_register(&ip4ip6_handler, AF_INET)) {
1431 printk(KERN_ERR "ip6_tunnel init: can't register ip4ip6\n");
1432 err = -EAGAIN;
1433 goto out;
1436 if (xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6)) {
1437 printk(KERN_ERR "ip6_tunnel init: can't register ip6ip6\n");
1438 err = -EAGAIN;
1439 goto unreg_ip4ip6;
1441 ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1442 ip6_tnl_dev_setup);
1444 if (!ip6_fb_tnl_dev) {
1445 err = -ENOMEM;
1446 goto fail;
1448 ip6_fb_tnl_dev->init = ip6_fb_tnl_dev_init;
1450 if ((err = register_netdev(ip6_fb_tnl_dev))) {
1451 free_netdev(ip6_fb_tnl_dev);
1452 goto fail;
1454 return 0;
1455 fail:
1456 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
1457 unreg_ip4ip6:
1458 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
1459 out:
1460 return err;
1463 static void __exit ip6_tnl_destroy_tunnels(void)
1465 int h;
1466 struct ip6_tnl *t;
1468 for (h = 0; h < HASH_SIZE; h++) {
1469 while ((t = tnls_r_l[h]) != NULL)
1470 unregister_netdevice(t->dev);
1473 t = tnls_wc[0];
1474 unregister_netdevice(t->dev);
1478 * ip6_tunnel_cleanup - free resources and unregister protocol
1481 static void __exit ip6_tunnel_cleanup(void)
1483 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
1484 printk(KERN_INFO "ip6_tunnel close: can't deregister ip4ip6\n");
1486 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
1487 printk(KERN_INFO "ip6_tunnel close: can't deregister ip6ip6\n");
1489 rtnl_lock();
1490 ip6_tnl_destroy_tunnels();
1491 rtnl_unlock();
1494 module_init(ip6_tunnel_init);
1495 module_exit(ip6_tunnel_cleanup);