2 * IPv6 tunneling device
3 * Linux INET6 implementation
6 * Ville Nuorvala <vnuorval@tcs.hut.fi>
7 * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
12 * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
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>
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>
49 #include <net/ip6_route.h>
50 #include <net/addrconf.h>
51 #include <net/ip6_tunnel.h>
53 #include <net/dsfield.h>
54 #include <net/inet_ecn.h>
55 #include <net/net_namespace.h>
56 #include <net/netns/generic.h>
58 MODULE_AUTHOR("Ville Nuorvala");
59 MODULE_DESCRIPTION("IPv6 tunneling device");
60 MODULE_LICENSE("GPL");
62 #define IPV6_TLV_TEL_DST_SIZE 8
65 #define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __func__)
67 #define IP6_TNL_TRACE(x...) do {;} while(0)
70 #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
71 #define IPV6_TCLASS_SHIFT 20
75 #define HASH(addr) ((__force u32)((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
76 (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
79 static int ip6_fb_tnl_dev_init(struct net_device *dev);
80 static int ip6_tnl_dev_init(struct net_device *dev);
81 static void ip6_tnl_dev_setup(struct net_device *dev);
83 static int ip6_tnl_net_id;
85 /* the IPv6 tunnel fallback device */
86 struct net_device *fb_tnl_dev;
89 /* lists for storing tunnels in use */
90 static struct ip6_tnl *tnls_r_l[HASH_SIZE];
91 static struct ip6_tnl *tnls_wc[1];
92 static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l };
94 /* lock for the tunnel lists */
95 static DEFINE_RWLOCK(ip6_tnl_lock);
97 static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
99 struct dst_entry *dst = t->dst_cache;
101 if (dst && dst->obsolete &&
102 dst->ops->check(dst, t->dst_cookie) == NULL) {
111 static inline void ip6_tnl_dst_reset(struct ip6_tnl *t)
113 dst_release(t->dst_cache);
117 static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
119 struct rt6_info *rt = (struct rt6_info *) dst;
120 t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
121 dst_release(t->dst_cache);
126 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
127 * @remote: the address of the tunnel exit-point
128 * @local: the address of the tunnel entry-point
131 * tunnel matching given end-points if found,
132 * else fallback tunnel if its device is up,
136 static struct ip6_tnl *
137 ip6_tnl_lookup(struct net *net, struct in6_addr *remote, struct in6_addr *local)
139 unsigned h0 = HASH(remote);
140 unsigned h1 = HASH(local);
143 for (t = tnls_r_l[h0 ^ h1]; t; t = t->next) {
144 if (ipv6_addr_equal(local, &t->parms.laddr) &&
145 ipv6_addr_equal(remote, &t->parms.raddr) &&
146 (t->dev->flags & IFF_UP))
149 if ((t = tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP))
156 * ip6_tnl_bucket - get head of list matching given tunnel parameters
157 * @p: parameters containing tunnel end-points
160 * ip6_tnl_bucket() returns the head of the list matching the
161 * &struct in6_addr entries laddr and raddr in @p.
163 * Return: head of IPv6 tunnel list
166 static struct ip6_tnl **
167 ip6_tnl_bucket(struct ip6_tnl_net *ip6n, struct ip6_tnl_parm *p)
169 struct in6_addr *remote = &p->raddr;
170 struct in6_addr *local = &p->laddr;
174 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
176 h = HASH(remote) ^ HASH(local);
178 return &tnls[prio][h];
182 * ip6_tnl_link - add tunnel to hash table
183 * @t: tunnel to be added
187 ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
189 struct ip6_tnl **tp = ip6_tnl_bucket(ip6n, &t->parms);
192 write_lock_bh(&ip6_tnl_lock);
194 write_unlock_bh(&ip6_tnl_lock);
198 * ip6_tnl_unlink - remove tunnel from hash table
199 * @t: tunnel to be removed
203 ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
207 for (tp = ip6_tnl_bucket(ip6n, &t->parms); *tp; tp = &(*tp)->next) {
209 write_lock_bh(&ip6_tnl_lock);
211 write_unlock_bh(&ip6_tnl_lock);
218 * ip6_tnl_create() - create a new tunnel
219 * @p: tunnel parameters
220 * @pt: pointer to new tunnel
223 * Create tunnel matching given parameters.
226 * created tunnel or NULL
229 static struct ip6_tnl *ip6_tnl_create(struct net *net, struct ip6_tnl_parm *p)
231 struct net_device *dev;
235 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
238 strlcpy(name, p->name, IFNAMSIZ);
240 sprintf(name, "ip6tnl%%d");
242 dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup);
246 if (strchr(name, '%')) {
247 if (dev_alloc_name(dev, name) < 0)
251 t = netdev_priv(dev);
252 dev->init = ip6_tnl_dev_init;
255 if ((err = register_netdevice(dev)) < 0)
259 ip6_tnl_link(ip6n, t);
269 * ip6_tnl_locate - find or create tunnel matching given parameters
270 * @p: tunnel parameters
271 * @create: != 0 if allowed to create new tunnel if no match found
274 * ip6_tnl_locate() first tries to locate an existing tunnel
275 * based on @parms. If this is unsuccessful, but @create is set a new
276 * tunnel device is created and registered for use.
279 * matching tunnel or NULL
282 static struct ip6_tnl *ip6_tnl_locate(struct net *net,
283 struct ip6_tnl_parm *p, int create)
285 struct in6_addr *remote = &p->raddr;
286 struct in6_addr *local = &p->laddr;
288 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
290 for (t = *ip6_tnl_bucket(ip6n, p); t; t = t->next) {
291 if (ipv6_addr_equal(local, &t->parms.laddr) &&
292 ipv6_addr_equal(remote, &t->parms.raddr))
297 return ip6_tnl_create(net, p);
301 * ip6_tnl_dev_uninit - tunnel device uninitializer
302 * @dev: the device to be destroyed
305 * ip6_tnl_dev_uninit() removes tunnel from its list
309 ip6_tnl_dev_uninit(struct net_device *dev)
311 struct ip6_tnl *t = netdev_priv(dev);
312 struct net *net = dev_net(dev);
313 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
315 if (dev == ip6n->fb_tnl_dev) {
316 write_lock_bh(&ip6_tnl_lock);
318 write_unlock_bh(&ip6_tnl_lock);
320 ip6_tnl_unlink(ip6n, t);
322 ip6_tnl_dst_reset(t);
327 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
328 * @skb: received socket buffer
331 * 0 if none was found,
332 * else index to encapsulation limit
336 parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
338 struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw;
339 __u8 nexthdr = ipv6h->nexthdr;
340 __u16 off = sizeof (*ipv6h);
342 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
344 struct ipv6_opt_hdr *hdr;
345 if (raw + off + sizeof (*hdr) > skb->data &&
346 !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
349 hdr = (struct ipv6_opt_hdr *) (raw + off);
350 if (nexthdr == NEXTHDR_FRAGMENT) {
351 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
352 if (frag_hdr->frag_off)
355 } else if (nexthdr == NEXTHDR_AUTH) {
356 optlen = (hdr->hdrlen + 2) << 2;
358 optlen = ipv6_optlen(hdr);
360 if (nexthdr == NEXTHDR_DEST) {
363 struct ipv6_tlv_tnl_enc_lim *tel;
365 /* No more room for encapsulation limit */
366 if (i + sizeof (*tel) > off + optlen)
369 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
370 /* return index of option if found and valid */
371 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
374 /* else jump to next option */
376 i += tel->length + 2;
381 nexthdr = hdr->nexthdr;
388 * ip6_tnl_err - tunnel error handler
391 * ip6_tnl_err() should handle errors in the tunnel according
392 * to the specifications in RFC 2473.
396 ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
397 int *type, int *code, int *msg, __u32 *info, int offset)
399 struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
402 int rel_type = ICMPV6_DEST_UNREACH;
403 int rel_code = ICMPV6_ADDR_UNREACH;
408 /* If the packet doesn't contain the original IPv6 header we are
409 in trouble since we might need the source address for further
410 processing of the error. */
412 read_lock(&ip6_tnl_lock);
413 if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr,
414 &ipv6h->saddr)) == NULL)
417 if (t->parms.proto != ipproto && t->parms.proto != 0)
424 struct ipv6_tlv_tnl_enc_lim *tel;
426 case ICMPV6_DEST_UNREACH:
429 "%s: Path to destination invalid "
430 "or inactive!\n", t->parms.name);
433 case ICMPV6_TIME_EXCEED:
434 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
437 "%s: Too small hop limit or "
438 "routing loop in tunnel!\n",
443 case ICMPV6_PARAMPROB:
445 if ((*code) == ICMPV6_HDR_FIELD)
446 teli = parse_tlv_tnl_enc_lim(skb, skb->data);
448 if (teli && teli == *info - 2) {
449 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
450 if (tel->encap_limit == 0) {
453 "%s: Too small encapsulation "
454 "limit or routing loop in "
455 "tunnel!\n", t->parms.name);
458 } else if (net_ratelimit()) {
460 "%s: Recipient unable to parse tunneled "
461 "packet!\n ", t->parms.name);
464 case ICMPV6_PKT_TOOBIG:
465 mtu = *info - offset;
466 if (mtu < IPV6_MIN_MTU)
470 if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
471 rel_type = ICMPV6_PKT_TOOBIG;
485 read_unlock(&ip6_tnl_lock);
490 ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
491 int type, int code, int offset, __be32 info)
496 __u32 rel_info = ntohl(info);
498 struct sk_buff *skb2;
503 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
504 &rel_msg, &rel_info, offset);
512 case ICMPV6_DEST_UNREACH:
513 if (rel_code != ICMPV6_ADDR_UNREACH)
515 rel_type = ICMP_DEST_UNREACH;
516 rel_code = ICMP_HOST_UNREACH;
518 case ICMPV6_PKT_TOOBIG:
521 rel_type = ICMP_DEST_UNREACH;
522 rel_code = ICMP_FRAG_NEEDED;
528 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
531 skb2 = skb_clone(skb, GFP_ATOMIC);
535 dst_release(skb2->dst);
537 skb_pull(skb2, offset);
538 skb_reset_network_header(skb2);
541 /* Try to guess incoming interface */
542 memset(&fl, 0, sizeof(fl));
543 fl.fl4_dst = eiph->saddr;
544 fl.fl4_tos = RT_TOS(eiph->tos);
545 fl.proto = IPPROTO_IPIP;
546 if (ip_route_output_key(&init_net, &rt, &fl))
549 skb2->dev = rt->u.dst.dev;
551 /* route "incoming" packet */
552 if (rt->rt_flags & RTCF_LOCAL) {
555 fl.fl4_dst = eiph->daddr;
556 fl.fl4_src = eiph->saddr;
557 fl.fl4_tos = eiph->tos;
558 if (ip_route_output_key(&init_net, &rt, &fl) ||
559 rt->u.dst.dev->type != ARPHRD_TUNNEL) {
563 skb2->dst = (struct dst_entry *)rt;
566 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
568 skb2->dst->dev->type != ARPHRD_TUNNEL)
572 /* change mtu on this route */
573 if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
574 if (rel_info > dst_mtu(skb2->dst))
577 skb2->dst->ops->update_pmtu(skb2->dst, rel_info);
580 icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
588 ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
589 int type, int code, int offset, __be32 info)
594 __u32 rel_info = ntohl(info);
597 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
598 &rel_msg, &rel_info, offset);
602 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
604 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
609 dst_release(skb2->dst);
611 skb_pull(skb2, offset);
612 skb_reset_network_header(skb2);
614 /* Try to guess incoming interface */
615 rt = rt6_lookup(&init_net, &ipv6_hdr(skb2)->saddr, NULL, 0, 0);
617 if (rt && rt->rt6i_dev)
618 skb2->dev = rt->rt6i_dev;
620 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
623 dst_release(&rt->u.dst);
631 static void ip4ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
632 struct ipv6hdr *ipv6h,
635 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
637 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
638 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
640 if (INET_ECN_is_ce(dsfield))
641 IP_ECN_set_ce(ip_hdr(skb));
644 static void ip6ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
645 struct ipv6hdr *ipv6h,
648 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
649 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
651 if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
652 IP6_ECN_set_ce(ipv6_hdr(skb));
655 static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
657 struct ip6_tnl_parm *p = &t->parms;
660 if (p->flags & IP6_TNL_F_CAP_RCV) {
661 struct net_device *ldev = NULL;
664 ldev = dev_get_by_index(&init_net, p->link);
666 if ((ipv6_addr_is_multicast(&p->laddr) ||
667 likely(ipv6_chk_addr(&init_net, &p->laddr, ldev, 0))) &&
668 likely(!ipv6_chk_addr(&init_net, &p->raddr, NULL, 0)))
678 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
679 * @skb: received socket buffer
680 * @protocol: ethernet protocol ID
681 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
686 static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
688 void (*dscp_ecn_decapsulate)(struct ip6_tnl *t,
689 struct ipv6hdr *ipv6h,
690 struct sk_buff *skb))
693 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
695 read_lock(&ip6_tnl_lock);
697 if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
698 &ipv6h->daddr)) != NULL) {
699 if (t->parms.proto != ipproto && t->parms.proto != 0) {
700 read_unlock(&ip6_tnl_lock);
704 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
705 read_unlock(&ip6_tnl_lock);
709 if (!ip6_tnl_rcv_ctl(t)) {
710 t->stat.rx_dropped++;
711 read_unlock(&ip6_tnl_lock);
715 skb->mac_header = skb->network_header;
716 skb_reset_network_header(skb);
717 skb->protocol = htons(protocol);
718 skb->pkt_type = PACKET_HOST;
719 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
721 dst_release(skb->dst);
725 dscp_ecn_decapsulate(t, ipv6h, skb);
727 t->stat.rx_packets++;
728 t->stat.rx_bytes += skb->len;
730 read_unlock(&ip6_tnl_lock);
733 read_unlock(&ip6_tnl_lock);
741 static int ip4ip6_rcv(struct sk_buff *skb)
743 return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
744 ip4ip6_dscp_ecn_decapsulate);
747 static int ip6ip6_rcv(struct sk_buff *skb)
749 return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
750 ip6ip6_dscp_ecn_decapsulate);
753 struct ipv6_tel_txoption {
754 struct ipv6_txoptions ops;
758 static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
760 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
762 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
764 opt->dst_opt[4] = encap_limit;
765 opt->dst_opt[5] = IPV6_TLV_PADN;
768 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
769 opt->ops.opt_nflen = 8;
773 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
774 * @t: the outgoing tunnel device
775 * @hdr: IPv6 header from the incoming packet
778 * Avoid trivial tunneling loop by checking that tunnel exit-point
779 * doesn't match source of incoming packet.
787 ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
789 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
792 static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
794 struct ip6_tnl_parm *p = &t->parms;
797 if (p->flags & IP6_TNL_F_CAP_XMIT) {
798 struct net_device *ldev = NULL;
801 ldev = dev_get_by_index(&init_net, p->link);
803 if (unlikely(!ipv6_chk_addr(&init_net, &p->laddr, ldev, 0)))
805 "%s xmit: Local address not yet configured!\n",
807 else if (!ipv6_addr_is_multicast(&p->raddr) &&
808 unlikely(ipv6_chk_addr(&init_net, &p->raddr, NULL, 0)))
810 "%s xmit: Routing loop! "
811 "Remote address found on this node!\n",
821 * ip6_tnl_xmit2 - encapsulate packet and send
822 * @skb: the outgoing socket buffer
823 * @dev: the outgoing tunnel device
824 * @dsfield: dscp code for outer header
825 * @fl: flow of tunneled packet
826 * @encap_limit: encapsulation limit
827 * @pmtu: Path MTU is stored if packet is too big
830 * Build new header and do some sanity checks on the packet before sending
836 * %-EMSGSIZE message too big. return mtu in this case.
839 static int ip6_tnl_xmit2(struct sk_buff *skb,
840 struct net_device *dev,
846 struct ip6_tnl *t = netdev_priv(dev);
847 struct net_device_stats *stats = &t->stat;
848 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
849 struct ipv6_tel_txoption opt;
850 struct dst_entry *dst;
851 struct net_device *tdev;
853 unsigned int max_headroom = sizeof(struct ipv6hdr);
858 if ((dst = ip6_tnl_dst_check(t)) != NULL)
861 dst = ip6_route_output(&init_net, NULL, fl);
863 if (dst->error || xfrm_lookup(&dst, fl, NULL, 0) < 0)
864 goto tx_err_link_failure;
873 "%s: Local routing loop detected!\n",
875 goto tx_err_dst_release;
877 mtu = dst_mtu(dst) - sizeof (*ipv6h);
878 if (encap_limit >= 0) {
882 if (mtu < IPV6_MIN_MTU)
885 skb->dst->ops->update_pmtu(skb->dst, mtu);
886 if (skb->len > mtu) {
889 goto tx_err_dst_release;
893 * Okay, now see if we can stuff it in the buffer as-is.
895 max_headroom += LL_RESERVED_SPACE(tdev);
897 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
898 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
899 struct sk_buff *new_skb;
901 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
902 goto tx_err_dst_release;
905 skb_set_owner_w(new_skb, skb->sk);
909 dst_release(skb->dst);
910 skb->dst = dst_clone(dst);
912 skb->transport_header = skb->network_header;
915 if (encap_limit >= 0) {
916 init_tel_txopt(&opt, encap_limit);
917 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
919 skb_push(skb, sizeof(struct ipv6hdr));
920 skb_reset_network_header(skb);
921 ipv6h = ipv6_hdr(skb);
922 *(__be32*)ipv6h = fl->fl6_flowlabel | htonl(0x60000000);
923 dsfield = INET_ECN_encapsulate(0, dsfield);
924 ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
925 ipv6h->hop_limit = t->parms.hop_limit;
926 ipv6h->nexthdr = proto;
927 ipv6_addr_copy(&ipv6h->saddr, &fl->fl6_src);
928 ipv6_addr_copy(&ipv6h->daddr, &fl->fl6_dst);
931 err = ip6_local_out(skb);
933 if (net_xmit_eval(err) == 0) {
934 stats->tx_bytes += pkt_len;
938 stats->tx_aborted_errors++;
940 ip6_tnl_dst_store(t, dst);
943 stats->tx_carrier_errors++;
944 dst_link_failure(skb);
951 ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
953 struct ip6_tnl *t = netdev_priv(dev);
954 struct iphdr *iph = ip_hdr(skb);
955 int encap_limit = -1;
961 if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
962 !ip6_tnl_xmit_ctl(t))
965 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
966 encap_limit = t->parms.encap_limit;
968 memcpy(&fl, &t->fl, sizeof (fl));
969 fl.proto = IPPROTO_IPIP;
971 dsfield = ipv4_get_dsfield(iph);
973 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
974 fl.fl6_flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
977 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
979 /* XXX: send ICMP error even if DF is not set. */
980 if (err == -EMSGSIZE)
981 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
990 ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
992 struct ip6_tnl *t = netdev_priv(dev);
993 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
994 int encap_limit = -1;
1001 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
1002 !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))
1005 offset = parse_tlv_tnl_enc_lim(skb, skb_network_header(skb));
1007 struct ipv6_tlv_tnl_enc_lim *tel;
1008 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
1009 if (tel->encap_limit == 0) {
1010 icmpv6_send(skb, ICMPV6_PARAMPROB,
1011 ICMPV6_HDR_FIELD, offset + 2, skb->dev);
1014 encap_limit = tel->encap_limit - 1;
1015 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1016 encap_limit = t->parms.encap_limit;
1018 memcpy(&fl, &t->fl, sizeof (fl));
1019 fl.proto = IPPROTO_IPV6;
1021 dsfield = ipv6_get_dsfield(ipv6h);
1022 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
1023 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
1024 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
1025 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
1027 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
1029 if (err == -EMSGSIZE)
1030 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
1038 ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1040 struct ip6_tnl *t = netdev_priv(dev);
1041 struct net_device_stats *stats = &t->stat;
1044 if (t->recursion++) {
1045 t->stat.collisions++;
1049 switch (skb->protocol) {
1050 case __constant_htons(ETH_P_IP):
1051 ret = ip4ip6_tnl_xmit(skb, dev);
1053 case __constant_htons(ETH_P_IPV6):
1054 ret = ip6ip6_tnl_xmit(skb, dev);
1068 stats->tx_dropped++;
1074 static void ip6_tnl_set_cap(struct ip6_tnl *t)
1076 struct ip6_tnl_parm *p = &t->parms;
1077 int ltype = ipv6_addr_type(&p->laddr);
1078 int rtype = ipv6_addr_type(&p->raddr);
1080 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
1082 if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
1083 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
1084 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
1085 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
1086 if (ltype&IPV6_ADDR_UNICAST)
1087 p->flags |= IP6_TNL_F_CAP_XMIT;
1088 if (rtype&IPV6_ADDR_UNICAST)
1089 p->flags |= IP6_TNL_F_CAP_RCV;
1093 static void ip6_tnl_link_config(struct ip6_tnl *t)
1095 struct net_device *dev = t->dev;
1096 struct ip6_tnl_parm *p = &t->parms;
1097 struct flowi *fl = &t->fl;
1099 memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1100 memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1102 /* Set up flowi template */
1103 ipv6_addr_copy(&fl->fl6_src, &p->laddr);
1104 ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
1106 fl->fl6_flowlabel = 0;
1108 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1109 fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1110 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1111 fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1115 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1116 dev->flags |= IFF_POINTOPOINT;
1118 dev->flags &= ~IFF_POINTOPOINT;
1120 dev->iflink = p->link;
1122 if (p->flags & IP6_TNL_F_CAP_XMIT) {
1123 int strict = (ipv6_addr_type(&p->raddr) &
1124 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1126 struct rt6_info *rt = rt6_lookup(&init_net, &p->raddr, &p->laddr,
1133 dev->hard_header_len = rt->rt6i_dev->hard_header_len +
1134 sizeof (struct ipv6hdr);
1136 dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
1138 if (dev->mtu < IPV6_MIN_MTU)
1139 dev->mtu = IPV6_MIN_MTU;
1141 dst_release(&rt->u.dst);
1146 * ip6_tnl_change - update the tunnel parameters
1147 * @t: tunnel to be changed
1148 * @p: tunnel configuration parameters
1149 * @active: != 0 if tunnel is ready for use
1152 * ip6_tnl_change() updates the tunnel parameters
1156 ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
1158 ipv6_addr_copy(&t->parms.laddr, &p->laddr);
1159 ipv6_addr_copy(&t->parms.raddr, &p->raddr);
1160 t->parms.flags = p->flags;
1161 t->parms.hop_limit = p->hop_limit;
1162 t->parms.encap_limit = p->encap_limit;
1163 t->parms.flowinfo = p->flowinfo;
1164 t->parms.link = p->link;
1165 t->parms.proto = p->proto;
1166 ip6_tnl_dst_reset(t);
1167 ip6_tnl_link_config(t);
1172 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
1173 * @dev: virtual device associated with tunnel
1174 * @ifr: parameters passed from userspace
1175 * @cmd: command to be performed
1178 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
1181 * The possible commands are the following:
1182 * %SIOCGETTUNNEL: get tunnel parameters for device
1183 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1184 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1185 * %SIOCDELTUNNEL: delete tunnel
1187 * The fallback device "ip6tnl0", created during module
1188 * initialization, can be used for creating other tunnel devices.
1192 * %-EFAULT if unable to copy data to or from userspace,
1193 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1194 * %-EINVAL if passed tunnel parameters are invalid,
1195 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1196 * %-ENODEV if attempting to change or delete a nonexisting device
1200 ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1203 struct ip6_tnl_parm p;
1204 struct ip6_tnl *t = NULL;
1205 struct net *net = dev_net(dev);
1206 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1210 if (dev == ip6n->fb_tnl_dev) {
1211 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
1215 t = ip6_tnl_locate(net, &p, 0);
1218 t = netdev_priv(dev);
1219 memcpy(&p, &t->parms, sizeof (p));
1220 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
1227 if (!capable(CAP_NET_ADMIN))
1230 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
1233 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1236 t = ip6_tnl_locate(net, &p, cmd == SIOCADDTUNNEL);
1237 if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
1239 if (t->dev != dev) {
1244 t = netdev_priv(dev);
1246 ip6_tnl_unlink(ip6n, t);
1247 err = ip6_tnl_change(t, &p);
1248 ip6_tnl_link(ip6n, t);
1249 netdev_state_change(dev);
1253 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))
1257 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1261 if (!capable(CAP_NET_ADMIN))
1264 if (dev == ip6n->fb_tnl_dev) {
1266 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
1269 if ((t = ip6_tnl_locate(net, &p, 0)) == NULL)
1272 if (t->dev == ip6n->fb_tnl_dev)
1277 unregister_netdevice(dev);
1286 * ip6_tnl_get_stats - return the stats for tunnel device
1287 * @dev: virtual device associated with tunnel
1289 * Return: stats for device
1292 static struct net_device_stats *
1293 ip6_tnl_get_stats(struct net_device *dev)
1295 return &(((struct ip6_tnl *)netdev_priv(dev))->stat);
1299 * ip6_tnl_change_mtu - change mtu manually for tunnel device
1300 * @dev: virtual device associated with tunnel
1301 * @new_mtu: the new mtu
1305 * %-EINVAL if mtu too small
1309 ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1311 if (new_mtu < IPV6_MIN_MTU) {
1319 * ip6_tnl_dev_setup - setup virtual tunnel device
1320 * @dev: virtual device associated with tunnel
1323 * Initialize function pointers and device parameters
1326 static void ip6_tnl_dev_setup(struct net_device *dev)
1328 dev->uninit = ip6_tnl_dev_uninit;
1329 dev->destructor = free_netdev;
1330 dev->hard_start_xmit = ip6_tnl_xmit;
1331 dev->get_stats = ip6_tnl_get_stats;
1332 dev->do_ioctl = ip6_tnl_ioctl;
1333 dev->change_mtu = ip6_tnl_change_mtu;
1335 dev->type = ARPHRD_TUNNEL6;
1336 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1337 dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
1338 dev->flags |= IFF_NOARP;
1339 dev->addr_len = sizeof(struct in6_addr);
1344 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1345 * @dev: virtual device associated with tunnel
1349 ip6_tnl_dev_init_gen(struct net_device *dev)
1351 struct ip6_tnl *t = netdev_priv(dev);
1353 strcpy(t->parms.name, dev->name);
1357 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1358 * @dev: virtual device associated with tunnel
1362 ip6_tnl_dev_init(struct net_device *dev)
1364 struct ip6_tnl *t = netdev_priv(dev);
1365 ip6_tnl_dev_init_gen(dev);
1366 ip6_tnl_link_config(t);
1371 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1372 * @dev: fallback device
1378 ip6_fb_tnl_dev_init(struct net_device *dev)
1380 struct ip6_tnl *t = netdev_priv(dev);
1381 ip6_tnl_dev_init_gen(dev);
1382 t->parms.proto = IPPROTO_IPV6;
1388 static struct xfrm6_tunnel ip4ip6_handler = {
1389 .handler = ip4ip6_rcv,
1390 .err_handler = ip4ip6_err,
1394 static struct xfrm6_tunnel ip6ip6_handler = {
1395 .handler = ip6ip6_rcv,
1396 .err_handler = ip6ip6_err,
1400 static int ip6_tnl_init_net(struct net *net)
1403 struct ip6_tnl_net *ip6n;
1406 ip6n = kmalloc(sizeof(struct ip6_tnl_net), GFP_KERNEL);
1410 err = net_assign_generic(net, ip6_tnl_net_id, ip6n);
1415 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1418 if (!ip6n->fb_tnl_dev)
1421 ip6n->fb_tnl_dev->init = ip6_fb_tnl_dev_init;
1422 dev_net_set(ip6n->fb_tnl_dev, net);
1424 err = register_netdev(ip6n->fb_tnl_dev);
1430 free_netdev(ip6n->fb_tnl_dev);
1439 static void ip6_tnl_exit_net(struct net *net)
1441 struct ip6_tnl_net *ip6n;
1443 ip6n = net_generic(net, ip6_tnl_net_id);
1447 static struct pernet_operations ip6_tnl_net_ops = {
1448 .init = ip6_tnl_init_net,
1449 .exit = ip6_tnl_exit_net,
1453 * ip6_tunnel_init - register protocol and reserve needed resources
1455 * Return: 0 on success
1458 static int __init ip6_tunnel_init(void)
1462 if (xfrm6_tunnel_register(&ip4ip6_handler, AF_INET)) {
1463 printk(KERN_ERR "ip6_tunnel init: can't register ip4ip6\n");
1468 if (xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6)) {
1469 printk(KERN_ERR "ip6_tunnel init: can't register ip6ip6\n");
1474 err = register_pernet_gen_device(&ip6_tnl_net_id, &ip6_tnl_net_ops);
1479 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
1481 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
1486 static void __exit ip6_tnl_destroy_tunnels(void)
1491 for (h = 0; h < HASH_SIZE; h++) {
1492 while ((t = tnls_r_l[h]) != NULL)
1493 unregister_netdevice(t->dev);
1497 unregister_netdevice(t->dev);
1501 * ip6_tunnel_cleanup - free resources and unregister protocol
1504 static void __exit ip6_tunnel_cleanup(void)
1506 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
1507 printk(KERN_INFO "ip6_tunnel close: can't deregister ip4ip6\n");
1509 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
1510 printk(KERN_INFO "ip6_tunnel close: can't deregister ip6ip6\n");
1513 ip6_tnl_destroy_tunnels();
1516 unregister_pernet_gen_device(ip6_tnl_net_id, &ip6_tnl_net_ops);
1519 module_init(ip6_tunnel_init);
1520 module_exit(ip6_tunnel_cleanup);