[IPV6]: Improve IPv6 tunnel error reporting
[linux-2.6/linux-mips.git] / net / ipv6 / ip6_tunnel.c
blob8d918348f5bb62b4b61d8dbb82bc7670c1c98542
1 /*
2 * IPv6 over IPv6 tunnel device
3 * Linux INET6 implementation
5 * Authors:
6 * Ville Nuorvala <vnuorval@tcs.hut.fi>
8 * $Id$
10 * Based on:
11 * linux/net/ipv6/sit.c
13 * RFC 2473
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
22 #include <linux/module.h>
23 #include <linux/capability.h>
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/sockios.h>
27 #include <linux/if.h>
28 #include <linux/in.h>
29 #include <linux/ip.h>
30 #include <linux/if_tunnel.h>
31 #include <linux/net.h>
32 #include <linux/in6.h>
33 #include <linux/netdevice.h>
34 #include <linux/if_arp.h>
35 #include <linux/icmpv6.h>
36 #include <linux/init.h>
37 #include <linux/route.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/netfilter_ipv6.h>
41 #include <asm/uaccess.h>
42 #include <asm/atomic.h>
44 #include <net/ip.h>
45 #include <net/ipv6.h>
46 #include <net/ip6_route.h>
47 #include <net/addrconf.h>
48 #include <net/ip6_tunnel.h>
49 #include <net/xfrm.h>
50 #include <net/dsfield.h>
51 #include <net/inet_ecn.h>
53 MODULE_AUTHOR("Ville Nuorvala");
54 MODULE_DESCRIPTION("IPv6-in-IPv6 tunnel");
55 MODULE_LICENSE("GPL");
57 #define IPV6_TLV_TEL_DST_SIZE 8
59 #ifdef IP6_TNL_DEBUG
60 #define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __FUNCTION__)
61 #else
62 #define IP6_TNL_TRACE(x...) do {;} while(0)
63 #endif
65 #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
67 #define HASH_SIZE 32
69 #define HASH(addr) ((__force u32)((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
70 (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
71 (HASH_SIZE - 1))
73 static int ip6ip6_fb_tnl_dev_init(struct net_device *dev);
74 static int ip6ip6_tnl_dev_init(struct net_device *dev);
75 static void ip6ip6_tnl_dev_setup(struct net_device *dev);
77 /* the IPv6 tunnel fallback device */
78 static struct net_device *ip6ip6_fb_tnl_dev;
81 /* lists for storing tunnels in use */
82 static struct ip6_tnl *tnls_r_l[HASH_SIZE];
83 static struct ip6_tnl *tnls_wc[1];
84 static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l };
86 /* lock for the tunnel lists */
87 static DEFINE_RWLOCK(ip6ip6_lock);
89 static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
91 struct dst_entry *dst = t->dst_cache;
93 if (dst && dst->obsolete &&
94 dst->ops->check(dst, t->dst_cookie) == NULL) {
95 t->dst_cache = NULL;
96 dst_release(dst);
97 return NULL;
100 return dst;
103 static inline void ip6_tnl_dst_reset(struct ip6_tnl *t)
105 dst_release(t->dst_cache);
106 t->dst_cache = NULL;
109 static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
111 struct rt6_info *rt = (struct rt6_info *) dst;
112 t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
113 dst_release(t->dst_cache);
114 t->dst_cache = dst;
118 * ip6ip6_tnl_lookup - fetch tunnel matching the end-point addresses
119 * @remote: the address of the tunnel exit-point
120 * @local: the address of the tunnel entry-point
122 * Return:
123 * tunnel matching given end-points if found,
124 * else fallback tunnel if its device is up,
125 * else %NULL
128 static struct ip6_tnl *
129 ip6ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local)
131 unsigned h0 = HASH(remote);
132 unsigned h1 = HASH(local);
133 struct ip6_tnl *t;
135 for (t = tnls_r_l[h0 ^ h1]; t; t = t->next) {
136 if (ipv6_addr_equal(local, &t->parms.laddr) &&
137 ipv6_addr_equal(remote, &t->parms.raddr) &&
138 (t->dev->flags & IFF_UP))
139 return t;
141 if ((t = tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP))
142 return t;
144 return NULL;
148 * ip6ip6_bucket - get head of list matching given tunnel parameters
149 * @p: parameters containing tunnel end-points
151 * Description:
152 * ip6ip6_bucket() returns the head of the list matching the
153 * &struct in6_addr entries laddr and raddr in @p.
155 * Return: head of IPv6 tunnel list
158 static struct ip6_tnl **
159 ip6ip6_bucket(struct ip6_tnl_parm *p)
161 struct in6_addr *remote = &p->raddr;
162 struct in6_addr *local = &p->laddr;
163 unsigned h = 0;
164 int prio = 0;
166 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
167 prio = 1;
168 h = HASH(remote) ^ HASH(local);
170 return &tnls[prio][h];
174 * ip6ip6_tnl_link - add tunnel to hash table
175 * @t: tunnel to be added
178 static void
179 ip6ip6_tnl_link(struct ip6_tnl *t)
181 struct ip6_tnl **tp = ip6ip6_bucket(&t->parms);
183 t->next = *tp;
184 write_lock_bh(&ip6ip6_lock);
185 *tp = t;
186 write_unlock_bh(&ip6ip6_lock);
190 * ip6ip6_tnl_unlink - remove tunnel from hash table
191 * @t: tunnel to be removed
194 static void
195 ip6ip6_tnl_unlink(struct ip6_tnl *t)
197 struct ip6_tnl **tp;
199 for (tp = ip6ip6_bucket(&t->parms); *tp; tp = &(*tp)->next) {
200 if (t == *tp) {
201 write_lock_bh(&ip6ip6_lock);
202 *tp = t->next;
203 write_unlock_bh(&ip6ip6_lock);
204 break;
210 * ip6_tnl_create() - create a new tunnel
211 * @p: tunnel parameters
212 * @pt: pointer to new tunnel
214 * Description:
215 * Create tunnel matching given parameters.
217 * Return:
218 * created tunnel or NULL
221 static struct ip6_tnl *ip6_tnl_create(struct ip6_tnl_parm *p)
223 struct net_device *dev;
224 struct ip6_tnl *t;
225 char name[IFNAMSIZ];
226 int err;
228 if (p->name[0]) {
229 strlcpy(name, p->name, IFNAMSIZ);
230 } else {
231 int i;
232 for (i = 1; i < IP6_TNL_MAX; i++) {
233 sprintf(name, "ip6tnl%d", i);
234 if (__dev_get_by_name(name) == NULL)
235 break;
237 if (i == IP6_TNL_MAX)
238 goto failed;
240 dev = alloc_netdev(sizeof (*t), name, ip6ip6_tnl_dev_setup);
241 if (dev == NULL)
242 goto failed;
244 t = netdev_priv(dev);
245 dev->init = ip6ip6_tnl_dev_init;
246 t->parms = *p;
248 if ((err = register_netdevice(dev)) < 0) {
249 free_netdev(dev);
250 goto failed;
252 dev_hold(dev);
253 ip6ip6_tnl_link(t);
254 return t;
255 failed:
256 return NULL;
260 * ip6ip6_tnl_locate - find or create tunnel matching given parameters
261 * @p: tunnel parameters
262 * @create: != 0 if allowed to create new tunnel if no match found
264 * Description:
265 * ip6ip6_tnl_locate() first tries to locate an existing tunnel
266 * based on @parms. If this is unsuccessful, but @create is set a new
267 * tunnel device is created and registered for use.
269 * Return:
270 * matching tunnel or NULL
273 static struct ip6_tnl *ip6ip6_tnl_locate(struct ip6_tnl_parm *p, int create)
275 struct in6_addr *remote = &p->raddr;
276 struct in6_addr *local = &p->laddr;
277 struct ip6_tnl *t;
279 for (t = *ip6ip6_bucket(p); t; t = t->next) {
280 if (ipv6_addr_equal(local, &t->parms.laddr) &&
281 ipv6_addr_equal(remote, &t->parms.raddr))
282 return t;
284 if (!create)
285 return NULL;
286 return ip6_tnl_create(p);
290 * ip6ip6_tnl_dev_uninit - tunnel device uninitializer
291 * @dev: the device to be destroyed
293 * Description:
294 * ip6ip6_tnl_dev_uninit() removes tunnel from its list
297 static void
298 ip6ip6_tnl_dev_uninit(struct net_device *dev)
300 struct ip6_tnl *t = netdev_priv(dev);
302 if (dev == ip6ip6_fb_tnl_dev) {
303 write_lock_bh(&ip6ip6_lock);
304 tnls_wc[0] = NULL;
305 write_unlock_bh(&ip6ip6_lock);
306 } else {
307 ip6ip6_tnl_unlink(t);
309 ip6_tnl_dst_reset(t);
310 dev_put(dev);
314 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
315 * @skb: received socket buffer
317 * Return:
318 * 0 if none was found,
319 * else index to encapsulation limit
322 static __u16
323 parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
325 struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw;
326 __u8 nexthdr = ipv6h->nexthdr;
327 __u16 off = sizeof (*ipv6h);
329 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
330 __u16 optlen = 0;
331 struct ipv6_opt_hdr *hdr;
332 if (raw + off + sizeof (*hdr) > skb->data &&
333 !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
334 break;
336 hdr = (struct ipv6_opt_hdr *) (raw + off);
337 if (nexthdr == NEXTHDR_FRAGMENT) {
338 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
339 if (frag_hdr->frag_off)
340 break;
341 optlen = 8;
342 } else if (nexthdr == NEXTHDR_AUTH) {
343 optlen = (hdr->hdrlen + 2) << 2;
344 } else {
345 optlen = ipv6_optlen(hdr);
347 if (nexthdr == NEXTHDR_DEST) {
348 __u16 i = off + 2;
349 while (1) {
350 struct ipv6_tlv_tnl_enc_lim *tel;
352 /* No more room for encapsulation limit */
353 if (i + sizeof (*tel) > off + optlen)
354 break;
356 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
357 /* return index of option if found and valid */
358 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
359 tel->length == 1)
360 return i;
361 /* else jump to next option */
362 if (tel->type)
363 i += tel->length + 2;
364 else
365 i++;
368 nexthdr = hdr->nexthdr;
369 off += optlen;
371 return 0;
375 * ip6ip6_err - tunnel error handler
377 * Description:
378 * ip6ip6_err() should handle errors in the tunnel according
379 * to the specifications in RFC 2473.
382 static int
383 ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
384 int type, int code, int offset, __be32 info)
386 struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
387 struct ip6_tnl *t;
388 int rel_msg = 0;
389 int rel_type = ICMPV6_DEST_UNREACH;
390 int rel_code = ICMPV6_ADDR_UNREACH;
391 __u32 rel_info = 0;
392 __u16 len;
393 int err = -ENOENT;
395 /* If the packet doesn't contain the original IPv6 header we are
396 in trouble since we might need the source address for further
397 processing of the error. */
399 read_lock(&ip6ip6_lock);
400 if ((t = ip6ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
401 goto out;
403 err = 0;
405 switch (type) {
406 __u32 teli;
407 struct ipv6_tlv_tnl_enc_lim *tel;
408 __u32 mtu;
409 case ICMPV6_DEST_UNREACH:
410 if (net_ratelimit())
411 printk(KERN_WARNING
412 "%s: Path to destination invalid "
413 "or inactive!\n", t->parms.name);
414 rel_msg = 1;
415 break;
416 case ICMPV6_TIME_EXCEED:
417 if (code == ICMPV6_EXC_HOPLIMIT) {
418 if (net_ratelimit())
419 printk(KERN_WARNING
420 "%s: Too small hop limit or "
421 "routing loop in tunnel!\n",
422 t->parms.name);
423 rel_msg = 1;
425 break;
426 case ICMPV6_PARAMPROB:
427 teli = 0;
428 if (code == ICMPV6_HDR_FIELD)
429 teli = parse_tlv_tnl_enc_lim(skb, skb->data);
431 if (teli && teli == ntohl(info) - 2) {
432 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
433 if (tel->encap_limit == 0) {
434 if (net_ratelimit())
435 printk(KERN_WARNING
436 "%s: Too small encapsulation "
437 "limit or routing loop in "
438 "tunnel!\n", t->parms.name);
439 rel_msg = 1;
441 } else if (net_ratelimit()) {
442 printk(KERN_WARNING
443 "%s: Recipient unable to parse tunneled "
444 "packet!\n ", t->parms.name);
446 break;
447 case ICMPV6_PKT_TOOBIG:
448 mtu = ntohl(info) - offset;
449 if (mtu < IPV6_MIN_MTU)
450 mtu = IPV6_MIN_MTU;
451 t->dev->mtu = mtu;
453 if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
454 rel_type = ICMPV6_PKT_TOOBIG;
455 rel_code = 0;
456 rel_info = mtu;
457 rel_msg = 1;
459 break;
461 if (rel_msg && pskb_may_pull(skb, offset + sizeof (*ipv6h))) {
462 struct rt6_info *rt;
463 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
465 if (!skb2)
466 goto out;
468 dst_release(skb2->dst);
469 skb2->dst = NULL;
470 skb_pull(skb2, offset);
471 skb2->nh.raw = skb2->data;
473 /* Try to guess incoming interface */
474 rt = rt6_lookup(&skb2->nh.ipv6h->saddr, NULL, 0, 0);
476 if (rt && rt->rt6i_dev)
477 skb2->dev = rt->rt6i_dev;
479 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
481 if (rt)
482 dst_release(&rt->u.dst);
484 kfree_skb(skb2);
486 out:
487 read_unlock(&ip6ip6_lock);
488 return err;
491 static inline void ip6ip6_ecn_decapsulate(struct ipv6hdr *outer_iph,
492 struct sk_buff *skb)
494 struct ipv6hdr *inner_iph = skb->nh.ipv6h;
496 if (INET_ECN_is_ce(ipv6_get_dsfield(outer_iph)))
497 IP6_ECN_set_ce(inner_iph);
499 static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
501 struct ip6_tnl_parm *p = &t->parms;
502 int ret = 0;
504 if (p->flags & IP6_TNL_F_CAP_RCV) {
505 struct net_device *ldev = NULL;
507 if (p->link)
508 ldev = dev_get_by_index(p->link);
510 if ((ipv6_addr_is_multicast(&p->laddr) ||
511 likely(ipv6_chk_addr(&p->laddr, ldev, 0))) &&
512 likely(!ipv6_chk_addr(&p->raddr, NULL, 0)))
513 ret = 1;
515 if (ldev)
516 dev_put(ldev);
518 return ret;
522 * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally
523 * @skb: received socket buffer
525 * Return: 0
528 static int
529 ip6ip6_rcv(struct sk_buff *skb)
531 struct ipv6hdr *ipv6h;
532 struct ip6_tnl *t;
534 ipv6h = skb->nh.ipv6h;
536 read_lock(&ip6ip6_lock);
538 if ((t = ip6ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
539 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
540 read_unlock(&ip6ip6_lock);
541 goto discard;
544 if (!ip6_tnl_rcv_ctl(t)) {
545 t->stat.rx_dropped++;
546 read_unlock(&ip6ip6_lock);
547 goto discard;
549 secpath_reset(skb);
550 skb->mac.raw = skb->nh.raw;
551 skb->nh.raw = skb->data;
552 skb->protocol = htons(ETH_P_IPV6);
553 skb->pkt_type = PACKET_HOST;
554 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
555 skb->dev = t->dev;
556 dst_release(skb->dst);
557 skb->dst = NULL;
558 nf_reset(skb);
559 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
560 ipv6_copy_dscp(ipv6h, skb->nh.ipv6h);
561 ip6ip6_ecn_decapsulate(ipv6h, skb);
562 t->stat.rx_packets++;
563 t->stat.rx_bytes += skb->len;
564 netif_rx(skb);
565 read_unlock(&ip6ip6_lock);
566 return 0;
568 read_unlock(&ip6ip6_lock);
569 return 1;
571 discard:
572 kfree_skb(skb);
573 return 0;
576 struct ipv6_tel_txoption {
577 struct ipv6_txoptions ops;
578 __u8 dst_opt[8];
581 static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
583 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
585 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
586 opt->dst_opt[3] = 1;
587 opt->dst_opt[4] = encap_limit;
588 opt->dst_opt[5] = IPV6_TLV_PADN;
589 opt->dst_opt[6] = 1;
591 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
592 opt->ops.opt_nflen = 8;
596 * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
597 * @t: the outgoing tunnel device
598 * @hdr: IPv6 header from the incoming packet
600 * Description:
601 * Avoid trivial tunneling loop by checking that tunnel exit-point
602 * doesn't match source of incoming packet.
604 * Return:
605 * 1 if conflict,
606 * 0 else
609 static inline int
610 ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
612 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
615 static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
617 struct ip6_tnl_parm *p = &t->parms;
618 int ret = 0;
620 if (p->flags & IP6_TNL_F_CAP_XMIT) {
621 struct net_device *ldev = NULL;
623 if (p->link)
624 ldev = dev_get_by_index(p->link);
626 if (unlikely(!ipv6_chk_addr(&p->laddr, ldev, 0)))
627 printk(KERN_WARNING
628 "%s xmit: Local address not yet configured!\n",
629 p->name);
630 else if (!ipv6_addr_is_multicast(&p->raddr) &&
631 unlikely(ipv6_chk_addr(&p->raddr, NULL, 0)))
632 printk(KERN_WARNING
633 "%s xmit: Routing loop! "
634 "Remote address found on this node!\n",
635 p->name);
636 else
637 ret = 1;
638 if (ldev)
639 dev_put(ldev);
641 return ret;
644 * ip6ip6_tnl_xmit - encapsulate packet and send
645 * @skb: the outgoing socket buffer
646 * @dev: the outgoing tunnel device
648 * Description:
649 * Build new header and do some sanity checks on the packet before sending
650 * it.
652 * Return:
656 static int
657 ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
659 struct ip6_tnl *t = netdev_priv(dev);
660 struct net_device_stats *stats = &t->stat;
661 struct ipv6hdr *ipv6h = skb->nh.ipv6h;
662 int encap_limit = -1;
663 struct ipv6_tel_txoption opt;
664 __u16 offset;
665 struct flowi fl;
666 struct dst_entry *dst;
667 struct net_device *tdev;
668 int mtu;
669 int max_headroom = sizeof(struct ipv6hdr);
670 u8 proto;
671 int err;
672 int pkt_len;
673 int dsfield;
675 if (t->recursion++) {
676 stats->collisions++;
677 goto tx_err;
679 if (skb->protocol != htons(ETH_P_IPV6) ||
680 !ip6_tnl_xmit_ctl(t) || ip6ip6_tnl_addr_conflict(t, ipv6h))
681 goto tx_err;
683 if ((offset = parse_tlv_tnl_enc_lim(skb, skb->nh.raw)) > 0) {
684 struct ipv6_tlv_tnl_enc_lim *tel;
685 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->nh.raw[offset];
686 if (tel->encap_limit == 0) {
687 icmpv6_send(skb, ICMPV6_PARAMPROB,
688 ICMPV6_HDR_FIELD, offset + 2, skb->dev);
689 goto tx_err;
691 encap_limit = tel->encap_limit - 1;
692 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
693 encap_limit = t->parms.encap_limit;
695 memcpy(&fl, &t->fl, sizeof (fl));
696 proto = fl.proto;
698 dsfield = ipv6_get_dsfield(ipv6h);
699 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
700 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
701 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
702 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
704 if ((dst = ip6_tnl_dst_check(t)) != NULL)
705 dst_hold(dst);
706 else {
707 dst = ip6_route_output(NULL, &fl);
709 if (dst->error || xfrm_lookup(&dst, &fl, NULL, 0) < 0)
710 goto tx_err_link_failure;
713 tdev = dst->dev;
715 if (tdev == dev) {
716 stats->collisions++;
717 if (net_ratelimit())
718 printk(KERN_WARNING
719 "%s: Local routing loop detected!\n",
720 t->parms.name);
721 goto tx_err_dst_release;
723 mtu = dst_mtu(dst) - sizeof (*ipv6h);
724 if (encap_limit >= 0) {
725 max_headroom += 8;
726 mtu -= 8;
728 if (mtu < IPV6_MIN_MTU)
729 mtu = IPV6_MIN_MTU;
730 if (skb->dst && mtu < dst_mtu(skb->dst)) {
731 struct rt6_info *rt = (struct rt6_info *) skb->dst;
732 rt->rt6i_flags |= RTF_MODIFIED;
733 rt->u.dst.metrics[RTAX_MTU-1] = mtu;
735 if (skb->len > mtu) {
736 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
737 goto tx_err_dst_release;
741 * Okay, now see if we can stuff it in the buffer as-is.
743 max_headroom += LL_RESERVED_SPACE(tdev);
745 if (skb_headroom(skb) < max_headroom ||
746 skb_cloned(skb) || skb_shared(skb)) {
747 struct sk_buff *new_skb;
749 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
750 goto tx_err_dst_release;
752 if (skb->sk)
753 skb_set_owner_w(new_skb, skb->sk);
754 kfree_skb(skb);
755 skb = new_skb;
757 dst_release(skb->dst);
758 skb->dst = dst_clone(dst);
760 skb->h.raw = skb->nh.raw;
762 if (encap_limit >= 0) {
763 init_tel_txopt(&opt, encap_limit);
764 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
766 skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr));
767 ipv6h = skb->nh.ipv6h;
768 *(__be32*)ipv6h = fl.fl6_flowlabel | htonl(0x60000000);
769 dsfield = INET_ECN_encapsulate(0, dsfield);
770 ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
771 ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
772 ipv6h->hop_limit = t->parms.hop_limit;
773 ipv6h->nexthdr = proto;
774 ipv6_addr_copy(&ipv6h->saddr, &fl.fl6_src);
775 ipv6_addr_copy(&ipv6h->daddr, &fl.fl6_dst);
776 nf_reset(skb);
777 pkt_len = skb->len;
778 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL,
779 skb->dst->dev, dst_output);
781 if (net_xmit_eval(err) == 0) {
782 stats->tx_bytes += pkt_len;
783 stats->tx_packets++;
784 } else {
785 stats->tx_errors++;
786 stats->tx_aborted_errors++;
788 ip6_tnl_dst_store(t, dst);
789 t->recursion--;
790 return 0;
791 tx_err_link_failure:
792 stats->tx_carrier_errors++;
793 dst_link_failure(skb);
794 tx_err_dst_release:
795 dst_release(dst);
796 tx_err:
797 stats->tx_errors++;
798 stats->tx_dropped++;
799 kfree_skb(skb);
800 t->recursion--;
801 return 0;
804 static void ip6_tnl_set_cap(struct ip6_tnl *t)
806 struct ip6_tnl_parm *p = &t->parms;
807 int ltype = ipv6_addr_type(&p->laddr);
808 int rtype = ipv6_addr_type(&p->raddr);
810 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
812 if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
813 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
814 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
815 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
816 if (ltype&IPV6_ADDR_UNICAST)
817 p->flags |= IP6_TNL_F_CAP_XMIT;
818 if (rtype&IPV6_ADDR_UNICAST)
819 p->flags |= IP6_TNL_F_CAP_RCV;
823 static void ip6ip6_tnl_link_config(struct ip6_tnl *t)
825 struct net_device *dev = t->dev;
826 struct ip6_tnl_parm *p = &t->parms;
827 struct flowi *fl = &t->fl;
829 memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
830 memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
832 /* Set up flowi template */
833 ipv6_addr_copy(&fl->fl6_src, &p->laddr);
834 ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
835 fl->oif = p->link;
836 fl->fl6_flowlabel = 0;
838 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
839 fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
840 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
841 fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
843 ip6_tnl_set_cap(t);
845 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
846 dev->flags |= IFF_POINTOPOINT;
847 else
848 dev->flags &= ~IFF_POINTOPOINT;
850 dev->iflink = p->link;
852 if (p->flags & IP6_TNL_F_CAP_XMIT) {
853 int strict = (ipv6_addr_type(&p->raddr) &
854 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
856 struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
857 p->link, strict);
859 if (rt == NULL)
860 return;
862 if (rt->rt6i_dev) {
863 dev->hard_header_len = rt->rt6i_dev->hard_header_len +
864 sizeof (struct ipv6hdr);
866 dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
868 if (dev->mtu < IPV6_MIN_MTU)
869 dev->mtu = IPV6_MIN_MTU;
871 dst_release(&rt->u.dst);
876 * ip6ip6_tnl_change - update the tunnel parameters
877 * @t: tunnel to be changed
878 * @p: tunnel configuration parameters
879 * @active: != 0 if tunnel is ready for use
881 * Description:
882 * ip6ip6_tnl_change() updates the tunnel parameters
885 static int
886 ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
888 ipv6_addr_copy(&t->parms.laddr, &p->laddr);
889 ipv6_addr_copy(&t->parms.raddr, &p->raddr);
890 t->parms.flags = p->flags;
891 t->parms.hop_limit = p->hop_limit;
892 t->parms.encap_limit = p->encap_limit;
893 t->parms.flowinfo = p->flowinfo;
894 t->parms.link = p->link;
895 ip6_tnl_dst_reset(t);
896 ip6ip6_tnl_link_config(t);
897 return 0;
901 * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace
902 * @dev: virtual device associated with tunnel
903 * @ifr: parameters passed from userspace
904 * @cmd: command to be performed
906 * Description:
907 * ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels
908 * from userspace.
910 * The possible commands are the following:
911 * %SIOCGETTUNNEL: get tunnel parameters for device
912 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
913 * %SIOCCHGTUNNEL: change tunnel parameters to those given
914 * %SIOCDELTUNNEL: delete tunnel
916 * The fallback device "ip6tnl0", created during module
917 * initialization, can be used for creating other tunnel devices.
919 * Return:
920 * 0 on success,
921 * %-EFAULT if unable to copy data to or from userspace,
922 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
923 * %-EINVAL if passed tunnel parameters are invalid,
924 * %-EEXIST if changing a tunnel's parameters would cause a conflict
925 * %-ENODEV if attempting to change or delete a nonexisting device
928 static int
929 ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
931 int err = 0;
932 struct ip6_tnl_parm p;
933 struct ip6_tnl *t = NULL;
935 switch (cmd) {
936 case SIOCGETTUNNEL:
937 if (dev == ip6ip6_fb_tnl_dev) {
938 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
939 err = -EFAULT;
940 break;
942 t = ip6ip6_tnl_locate(&p, 0);
944 if (t == NULL)
945 t = netdev_priv(dev);
946 memcpy(&p, &t->parms, sizeof (p));
947 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
948 err = -EFAULT;
950 break;
951 case SIOCADDTUNNEL:
952 case SIOCCHGTUNNEL:
953 err = -EPERM;
954 if (!capable(CAP_NET_ADMIN))
955 break;
956 err = -EFAULT;
957 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
958 break;
959 err = -EINVAL;
960 if (p.proto != IPPROTO_IPV6)
961 break;
962 t = ip6ip6_tnl_locate(&p, cmd == SIOCADDTUNNEL);
963 if (dev != ip6ip6_fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
964 if (t != NULL) {
965 if (t->dev != dev) {
966 err = -EEXIST;
967 break;
969 } else
970 t = netdev_priv(dev);
972 ip6ip6_tnl_unlink(t);
973 err = ip6ip6_tnl_change(t, &p);
974 ip6ip6_tnl_link(t);
975 netdev_state_change(dev);
977 if (t) {
978 err = 0;
979 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))
980 err = -EFAULT;
982 } else
983 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
984 break;
985 case SIOCDELTUNNEL:
986 err = -EPERM;
987 if (!capable(CAP_NET_ADMIN))
988 break;
990 if (dev == ip6ip6_fb_tnl_dev) {
991 err = -EFAULT;
992 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
993 break;
994 err = -ENOENT;
995 if ((t = ip6ip6_tnl_locate(&p, 0)) == NULL)
996 break;
997 err = -EPERM;
998 if (t->dev == ip6ip6_fb_tnl_dev)
999 break;
1000 dev = t->dev;
1002 err = unregister_netdevice(dev);
1003 break;
1004 default:
1005 err = -EINVAL;
1007 return err;
1011 * ip6ip6_tnl_get_stats - return the stats for tunnel device
1012 * @dev: virtual device associated with tunnel
1014 * Return: stats for device
1017 static struct net_device_stats *
1018 ip6ip6_tnl_get_stats(struct net_device *dev)
1020 return &(((struct ip6_tnl *)netdev_priv(dev))->stat);
1024 * ip6ip6_tnl_change_mtu - change mtu manually for tunnel device
1025 * @dev: virtual device associated with tunnel
1026 * @new_mtu: the new mtu
1028 * Return:
1029 * 0 on success,
1030 * %-EINVAL if mtu too small
1033 static int
1034 ip6ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1036 if (new_mtu < IPV6_MIN_MTU) {
1037 return -EINVAL;
1039 dev->mtu = new_mtu;
1040 return 0;
1044 * ip6ip6_tnl_dev_setup - setup virtual tunnel device
1045 * @dev: virtual device associated with tunnel
1047 * Description:
1048 * Initialize function pointers and device parameters
1051 static void ip6ip6_tnl_dev_setup(struct net_device *dev)
1053 SET_MODULE_OWNER(dev);
1054 dev->uninit = ip6ip6_tnl_dev_uninit;
1055 dev->destructor = free_netdev;
1056 dev->hard_start_xmit = ip6ip6_tnl_xmit;
1057 dev->get_stats = ip6ip6_tnl_get_stats;
1058 dev->do_ioctl = ip6ip6_tnl_ioctl;
1059 dev->change_mtu = ip6ip6_tnl_change_mtu;
1061 dev->type = ARPHRD_TUNNEL6;
1062 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1063 dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
1064 dev->flags |= IFF_NOARP;
1065 dev->addr_len = sizeof(struct in6_addr);
1070 * ip6ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1071 * @dev: virtual device associated with tunnel
1074 static inline void
1075 ip6ip6_tnl_dev_init_gen(struct net_device *dev)
1077 struct ip6_tnl *t = netdev_priv(dev);
1078 t->fl.proto = IPPROTO_IPV6;
1079 t->dev = dev;
1080 strcpy(t->parms.name, dev->name);
1084 * ip6ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1085 * @dev: virtual device associated with tunnel
1088 static int
1089 ip6ip6_tnl_dev_init(struct net_device *dev)
1091 struct ip6_tnl *t = netdev_priv(dev);
1092 ip6ip6_tnl_dev_init_gen(dev);
1093 ip6ip6_tnl_link_config(t);
1094 return 0;
1098 * ip6ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1099 * @dev: fallback device
1101 * Return: 0
1104 static int
1105 ip6ip6_fb_tnl_dev_init(struct net_device *dev)
1107 struct ip6_tnl *t = netdev_priv(dev);
1108 ip6ip6_tnl_dev_init_gen(dev);
1109 dev_hold(dev);
1110 tnls_wc[0] = t;
1111 return 0;
1114 static struct xfrm6_tunnel ip6ip6_handler = {
1115 .handler = ip6ip6_rcv,
1116 .err_handler = ip6ip6_err,
1117 .priority = 1,
1121 * ip6_tunnel_init - register protocol and reserve needed resources
1123 * Return: 0 on success
1126 static int __init ip6_tunnel_init(void)
1128 int err;
1130 if (xfrm6_tunnel_register(&ip6ip6_handler)) {
1131 printk(KERN_ERR "ip6ip6 init: can't register tunnel\n");
1132 return -EAGAIN;
1134 ip6ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1135 ip6ip6_tnl_dev_setup);
1137 if (!ip6ip6_fb_tnl_dev) {
1138 err = -ENOMEM;
1139 goto fail;
1141 ip6ip6_fb_tnl_dev->init = ip6ip6_fb_tnl_dev_init;
1143 if ((err = register_netdev(ip6ip6_fb_tnl_dev))) {
1144 free_netdev(ip6ip6_fb_tnl_dev);
1145 goto fail;
1147 return 0;
1148 fail:
1149 xfrm6_tunnel_deregister(&ip6ip6_handler);
1150 return err;
1153 static void __exit ip6ip6_destroy_tunnels(void)
1155 int h;
1156 struct ip6_tnl *t;
1158 for (h = 0; h < HASH_SIZE; h++) {
1159 while ((t = tnls_r_l[h]) != NULL)
1160 unregister_netdevice(t->dev);
1163 t = tnls_wc[0];
1164 unregister_netdevice(t->dev);
1168 * ip6_tunnel_cleanup - free resources and unregister protocol
1171 static void __exit ip6_tunnel_cleanup(void)
1173 if (xfrm6_tunnel_deregister(&ip6ip6_handler))
1174 printk(KERN_INFO "ip6ip6 close: can't deregister tunnel\n");
1176 rtnl_lock();
1177 ip6ip6_destroy_tunnels();
1178 rtnl_unlock();
1181 module_init(ip6_tunnel_init);
1182 module_exit(ip6_tunnel_cleanup);