2 * IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * $Id: sit.c,v 1.53 2001/09/25 05:09:53 davem Exp $
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 * Roger Venning <r.venning@telstra.com>: 6to4 support
18 * Nate Thompson <nate@thebog.net>: 6to4 support
19 * Fred L. Templin <fltemplin@acm.org>: isatap support
22 #include <linux/module.h>
23 #include <linux/capability.h>
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/socket.h>
27 #include <linux/sockios.h>
28 #include <linux/net.h>
29 #include <linux/in6.h>
30 #include <linux/netdevice.h>
31 #include <linux/if_arp.h>
32 #include <linux/icmp.h>
33 #include <asm/uaccess.h>
34 #include <linux/init.h>
35 #include <linux/netfilter_ipv4.h>
36 #include <linux/if_ether.h>
42 #include <net/protocol.h>
43 #include <net/transp_v6.h>
44 #include <net/ip6_fib.h>
45 #include <net/ip6_route.h>
46 #include <net/ndisc.h>
47 #include <net/addrconf.h>
52 #include <net/inet_ecn.h>
54 #include <net/dsfield.h>
57 This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
59 For comments look at net/ipv4/ip_gre.c --ANK
63 #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
65 static int ipip6_fb_tunnel_init(struct net_device *dev);
66 static int ipip6_tunnel_init(struct net_device *dev);
67 static void ipip6_tunnel_setup(struct net_device *dev);
69 static struct net_device *ipip6_fb_tunnel_dev;
71 static struct ip_tunnel *tunnels_r_l[HASH_SIZE];
72 static struct ip_tunnel *tunnels_r[HASH_SIZE];
73 static struct ip_tunnel *tunnels_l[HASH_SIZE];
74 static struct ip_tunnel *tunnels_wc[1];
75 static struct ip_tunnel **tunnels[4] = { tunnels_wc, tunnels_l, tunnels_r, tunnels_r_l };
77 static DEFINE_RWLOCK(ipip6_lock);
79 static struct ip_tunnel * ipip6_tunnel_lookup(__be32 remote, __be32 local)
81 unsigned h0 = HASH(remote);
82 unsigned h1 = HASH(local);
85 for (t = tunnels_r_l[h0^h1]; t; t = t->next) {
86 if (local == t->parms.iph.saddr &&
87 remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
90 for (t = tunnels_r[h0]; t; t = t->next) {
91 if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
94 for (t = tunnels_l[h1]; t; t = t->next) {
95 if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP))
98 if ((t = tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP))
103 static struct ip_tunnel **__ipip6_bucket(struct ip_tunnel_parm *parms)
105 __be32 remote = parms->iph.daddr;
106 __be32 local = parms->iph.saddr;
118 return &tunnels[prio][h];
121 static inline struct ip_tunnel **ipip6_bucket(struct ip_tunnel *t)
123 return __ipip6_bucket(&t->parms);
126 static void ipip6_tunnel_unlink(struct ip_tunnel *t)
128 struct ip_tunnel **tp;
130 for (tp = ipip6_bucket(t); *tp; tp = &(*tp)->next) {
132 write_lock_bh(&ipip6_lock);
134 write_unlock_bh(&ipip6_lock);
140 static void ipip6_tunnel_link(struct ip_tunnel *t)
142 struct ip_tunnel **tp = ipip6_bucket(t);
145 write_lock_bh(&ipip6_lock);
147 write_unlock_bh(&ipip6_lock);
150 static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int create)
152 __be32 remote = parms->iph.daddr;
153 __be32 local = parms->iph.saddr;
154 struct ip_tunnel *t, **tp, *nt;
155 struct net_device *dev;
158 for (tp = __ipip6_bucket(parms); (t = *tp) != NULL; tp = &t->next) {
159 if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
166 strlcpy(name, parms->name, IFNAMSIZ);
169 for (i=1; i<100; i++) {
170 sprintf(name, "sit%d", i);
171 if (__dev_get_by_name(&init_net, name) == NULL)
178 dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
182 nt = netdev_priv(dev);
183 dev->init = ipip6_tunnel_init;
186 if (parms->i_flags & SIT_ISATAP)
187 dev->priv_flags |= IFF_ISATAP;
189 if (register_netdevice(dev) < 0) {
196 ipip6_tunnel_link(nt);
203 static void ipip6_tunnel_uninit(struct net_device *dev)
205 if (dev == ipip6_fb_tunnel_dev) {
206 write_lock_bh(&ipip6_lock);
207 tunnels_wc[0] = NULL;
208 write_unlock_bh(&ipip6_lock);
211 ipip6_tunnel_unlink(netdev_priv(dev));
217 static int ipip6_err(struct sk_buff *skb, u32 info)
219 #ifndef I_WISH_WORLD_WERE_PERFECT
221 /* It is not :-( All the routers (except for Linux) return only
222 8 bytes of packet payload. It means, that precise relaying of
223 ICMP in the real Internet is absolutely infeasible.
225 struct iphdr *iph = (struct iphdr*)skb->data;
226 const int type = icmp_hdr(skb)->type;
227 const int code = icmp_hdr(skb)->code;
233 case ICMP_PARAMETERPROB:
236 case ICMP_DEST_UNREACH:
239 case ICMP_PORT_UNREACH:
240 /* Impossible event. */
242 case ICMP_FRAG_NEEDED:
243 /* Soft state for pmtu is maintained by IP core. */
246 /* All others are translated to HOST_UNREACH.
247 rfc2003 contains "deep thoughts" about NET_UNREACH,
248 I believe they are just ether pollution. --ANK
253 case ICMP_TIME_EXCEEDED:
254 if (code != ICMP_EXC_TTL)
261 read_lock(&ipip6_lock);
262 t = ipip6_tunnel_lookup(iph->daddr, iph->saddr);
263 if (t == NULL || t->parms.iph.daddr == 0)
267 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
270 if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
274 t->err_time = jiffies;
276 read_unlock(&ipip6_lock);
279 struct iphdr *iph = (struct iphdr*)dp;
280 int hlen = iph->ihl<<2;
281 struct ipv6hdr *iph6;
282 const int type = icmp_hdr(skb)->type;
283 const int code = icmp_hdr(skb)->code;
287 struct sk_buff *skb2;
288 struct rt6_info *rt6i;
290 if (len < hlen + sizeof(struct ipv6hdr))
292 iph6 = (struct ipv6hdr*)(dp + hlen);
297 case ICMP_PARAMETERPROB:
298 if (icmp_hdr(skb)->un.gateway < hlen)
301 /* So... This guy found something strange INSIDE encapsulated
302 packet. Well, he is fool, but what can we do ?
304 rel_type = ICMPV6_PARAMPROB;
305 rel_info = icmp_hdr(skb)->un.gateway - hlen;
308 case ICMP_DEST_UNREACH:
311 case ICMP_PORT_UNREACH:
312 /* Impossible event. */
314 case ICMP_FRAG_NEEDED:
315 /* Too complicated case ... */
318 /* All others are translated to HOST_UNREACH.
319 rfc2003 contains "deep thoughts" about NET_UNREACH,
320 I believe, it is just ether pollution. --ANK
322 rel_type = ICMPV6_DEST_UNREACH;
323 rel_code = ICMPV6_ADDR_UNREACH;
327 case ICMP_TIME_EXCEEDED:
328 if (code != ICMP_EXC_TTL)
330 rel_type = ICMPV6_TIME_EXCEED;
331 rel_code = ICMPV6_EXC_HOPLIMIT;
335 /* Prepare fake skb to feed it to icmpv6_send */
336 skb2 = skb_clone(skb, GFP_ATOMIC);
339 dst_release(skb2->dst);
341 skb_pull(skb2, skb->data - (u8*)iph6);
342 skb_reset_network_header(skb2);
344 /* Try to guess incoming interface */
345 rt6i = rt6_lookup(&iph6->saddr, NULL, NULL, 0);
346 if (rt6i && rt6i->rt6i_dev) {
347 skb2->dev = rt6i->rt6i_dev;
349 rt6i = rt6_lookup(&iph6->daddr, &iph6->saddr, NULL, 0);
351 if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
352 struct ip_tunnel *t = netdev_priv(rt6i->rt6i_dev);
353 if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
354 rel_type = ICMPV6_DEST_UNREACH;
355 rel_code = ICMPV6_ADDR_UNREACH;
357 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
365 static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
367 if (INET_ECN_is_ce(iph->tos))
368 IP6_ECN_set_ce(ipv6_hdr(skb));
371 /* ISATAP (RFC4214) - check source address */
373 isatap_srcok(struct sk_buff *skb, struct iphdr *iph, struct net_device *dev)
375 struct neighbour *neigh;
376 struct dst_entry *dst;
379 struct in6_addr *addr6;
381 struct ipv6hdr *iph6;
384 /* from onlink default router */
385 ipv6_addr_set(&rtr, htonl(0xFE800000), 0, 0, 0);
386 ipv6_isatap_eui64(rtr.s6_addr + 8, iph->saddr);
387 if ((rt = rt6_get_dflt_router(&rtr, dev))) {
388 dst_release(&rt->u.dst);
392 iph6 = ipv6_hdr(skb);
393 memset(&fl, 0, sizeof(fl));
394 fl.proto = iph6->nexthdr;
395 ipv6_addr_copy(&fl.fl6_dst, &iph6->saddr);
396 fl.oif = dev->ifindex;
397 security_skb_classify_flow(skb, &fl);
399 dst = ip6_route_output(NULL, &fl);
400 if (!dst->error && (dst->dev == dev) && (neigh = dst->neighbour)) {
402 addr6 = (struct in6_addr*)&neigh->primary_key;
404 /* from correct previous hop */
405 if (ipv6_addr_is_isatap(addr6) &&
406 (addr6->s6_addr32[3] == iph->saddr))
413 static int ipip6_rcv(struct sk_buff *skb)
416 struct ip_tunnel *tunnel;
418 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
423 read_lock(&ipip6_lock);
424 if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
426 skb->mac_header = skb->network_header;
427 skb_reset_network_header(skb);
428 IPCB(skb)->flags = 0;
429 skb->protocol = htons(ETH_P_IPV6);
430 skb->pkt_type = PACKET_HOST;
432 if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
433 !isatap_srcok(skb, iph, tunnel->dev)) {
434 tunnel->stat.rx_errors++;
435 read_unlock(&ipip6_lock);
439 tunnel->stat.rx_packets++;
440 tunnel->stat.rx_bytes += skb->len;
441 skb->dev = tunnel->dev;
442 dst_release(skb->dst);
445 ipip6_ecn_decapsulate(iph, skb);
447 read_unlock(&ipip6_lock);
451 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
453 read_unlock(&ipip6_lock);
458 /* Returns the embedded IPv4 address if the IPv6 address
459 comes from 6to4 (RFC 3056) addr space */
461 static inline __be32 try_6to4(struct in6_addr *v6dst)
465 if (v6dst->s6_addr16[0] == htons(0x2002)) {
466 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
467 memcpy(&dst, &v6dst->s6_addr16[1], 4);
473 * This function assumes it is being called from dev_queue_xmit()
474 * and that skb is filled properly by that function.
477 static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
479 struct ip_tunnel *tunnel = netdev_priv(dev);
480 struct net_device_stats *stats = &tunnel->stat;
481 struct iphdr *tiph = &tunnel->parms.iph;
482 struct ipv6hdr *iph6 = ipv6_hdr(skb);
483 u8 tos = tunnel->parms.iph.tos;
484 struct rtable *rt; /* Route to the other host */
485 struct net_device *tdev; /* Device to other host */
486 struct iphdr *iph; /* Our new IP header */
487 unsigned int max_headroom; /* The extra header space needed */
488 __be32 dst = tiph->daddr;
490 struct in6_addr *addr6;
493 if (tunnel->recursion++) {
494 tunnel->stat.collisions++;
498 if (skb->protocol != htons(ETH_P_IPV6))
501 /* ISATAP (RFC4214) - must come before 6to4 */
502 if (dev->priv_flags & IFF_ISATAP) {
503 struct neighbour *neigh = NULL;
506 neigh = skb->dst->neighbour;
510 printk(KERN_DEBUG "sit: nexthop == NULL\n");
514 addr6 = (struct in6_addr*)&neigh->primary_key;
515 addr_type = ipv6_addr_type(addr6);
517 if ((addr_type & IPV6_ADDR_UNICAST) &&
518 ipv6_addr_is_isatap(addr6))
519 dst = addr6->s6_addr32[3];
525 dst = try_6to4(&iph6->daddr);
528 struct neighbour *neigh = NULL;
531 neigh = skb->dst->neighbour;
535 printk(KERN_DEBUG "sit: nexthop == NULL\n");
539 addr6 = (struct in6_addr*)&neigh->primary_key;
540 addr_type = ipv6_addr_type(addr6);
542 if (addr_type == IPV6_ADDR_ANY) {
543 addr6 = &ipv6_hdr(skb)->daddr;
544 addr_type = ipv6_addr_type(addr6);
547 if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
550 dst = addr6->s6_addr32[3];
554 struct flowi fl = { .nl_u = { .ip4_u =
556 .saddr = tiph->saddr,
557 .tos = RT_TOS(tos) } },
558 .oif = tunnel->parms.link,
559 .proto = IPPROTO_IPV6 };
560 if (ip_route_output_key(&rt, &fl)) {
561 tunnel->stat.tx_carrier_errors++;
565 if (rt->rt_type != RTN_UNICAST) {
567 tunnel->stat.tx_carrier_errors++;
570 tdev = rt->u.dst.dev;
574 tunnel->stat.collisions++;
579 mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
581 mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
584 tunnel->stat.collisions++;
588 if (mtu < IPV6_MIN_MTU)
590 if (tunnel->parms.iph.daddr && skb->dst)
591 skb->dst->ops->update_pmtu(skb->dst, mtu);
593 if (skb->len > mtu) {
594 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
599 if (tunnel->err_count > 0) {
600 if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
602 dst_link_failure(skb);
604 tunnel->err_count = 0;
608 * Okay, now see if we can stuff it in the buffer as-is.
610 max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
612 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
613 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
614 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
623 skb_set_owner_w(new_skb, skb->sk);
626 iph6 = ipv6_hdr(skb);
629 skb->transport_header = skb->network_header;
630 skb_push(skb, sizeof(struct iphdr));
631 skb_reset_network_header(skb);
632 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
633 IPCB(skb)->flags = 0;
634 dst_release(skb->dst);
635 skb->dst = &rt->u.dst;
638 * Push down and install the IPIP header.
643 iph->ihl = sizeof(struct iphdr)>>2;
644 if (mtu > IPV6_MIN_MTU)
645 iph->frag_off = htons(IP_DF);
649 iph->protocol = IPPROTO_IPV6;
650 iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
651 iph->daddr = rt->rt_dst;
652 iph->saddr = rt->rt_src;
654 if ((iph->ttl = tiph->ttl) == 0)
655 iph->ttl = iph6->hop_limit;
664 dst_link_failure(skb);
673 ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
676 struct ip_tunnel_parm p;
682 if (dev == ipip6_fb_tunnel_dev) {
683 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
687 t = ipip6_tunnel_locate(&p, 0);
690 t = netdev_priv(dev);
691 memcpy(&p, &t->parms, sizeof(p));
692 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
699 if (!capable(CAP_NET_ADMIN))
703 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
707 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
708 p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
711 p.iph.frag_off |= htons(IP_DF);
713 t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
715 if (dev != ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
722 if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
723 (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
727 t = netdev_priv(dev);
728 ipip6_tunnel_unlink(t);
729 t->parms.iph.saddr = p.iph.saddr;
730 t->parms.iph.daddr = p.iph.daddr;
731 memcpy(dev->dev_addr, &p.iph.saddr, 4);
732 memcpy(dev->broadcast, &p.iph.daddr, 4);
733 ipip6_tunnel_link(t);
734 netdev_state_change(dev);
740 if (cmd == SIOCCHGTUNNEL) {
741 t->parms.iph.ttl = p.iph.ttl;
742 t->parms.iph.tos = p.iph.tos;
744 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
747 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
752 if (!capable(CAP_NET_ADMIN))
755 if (dev == ipip6_fb_tunnel_dev) {
757 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
760 if ((t = ipip6_tunnel_locate(&p, 0)) == NULL)
763 if (t == netdev_priv(ipip6_fb_tunnel_dev))
767 unregister_netdevice(dev);
779 static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
781 return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
784 static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
786 if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
792 static void ipip6_tunnel_setup(struct net_device *dev)
794 dev->uninit = ipip6_tunnel_uninit;
795 dev->destructor = free_netdev;
796 dev->hard_start_xmit = ipip6_tunnel_xmit;
797 dev->get_stats = ipip6_tunnel_get_stats;
798 dev->do_ioctl = ipip6_tunnel_ioctl;
799 dev->change_mtu = ipip6_tunnel_change_mtu;
801 dev->type = ARPHRD_SIT;
802 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
803 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
804 dev->flags = IFF_NOARP;
809 static int ipip6_tunnel_init(struct net_device *dev)
811 struct net_device *tdev = NULL;
812 struct ip_tunnel *tunnel;
815 tunnel = netdev_priv(dev);
816 iph = &tunnel->parms.iph;
819 strcpy(tunnel->parms.name, dev->name);
821 memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
822 memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
825 struct flowi fl = { .nl_u = { .ip4_u =
826 { .daddr = iph->daddr,
828 .tos = RT_TOS(iph->tos) } },
829 .oif = tunnel->parms.link,
830 .proto = IPPROTO_IPV6 };
832 if (!ip_route_output_key(&rt, &fl)) {
833 tdev = rt->u.dst.dev;
836 dev->flags |= IFF_POINTOPOINT;
839 if (!tdev && tunnel->parms.link)
840 tdev = __dev_get_by_index(&init_net, tunnel->parms.link);
843 dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
844 dev->mtu = tdev->mtu - sizeof(struct iphdr);
845 if (dev->mtu < IPV6_MIN_MTU)
846 dev->mtu = IPV6_MIN_MTU;
848 dev->iflink = tunnel->parms.link;
853 static int __init ipip6_fb_tunnel_init(struct net_device *dev)
855 struct ip_tunnel *tunnel = netdev_priv(dev);
856 struct iphdr *iph = &tunnel->parms.iph;
859 strcpy(tunnel->parms.name, dev->name);
862 iph->protocol = IPPROTO_IPV6;
867 tunnels_wc[0] = tunnel;
871 static struct xfrm_tunnel sit_handler = {
872 .handler = ipip6_rcv,
873 .err_handler = ipip6_err,
877 static void __exit sit_destroy_tunnels(void)
881 for (prio = 1; prio < 4; prio++) {
883 for (h = 0; h < HASH_SIZE; h++) {
885 while ((t = tunnels[prio][h]) != NULL)
886 unregister_netdevice(t->dev);
891 static void __exit sit_cleanup(void)
893 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
896 sit_destroy_tunnels();
897 unregister_netdevice(ipip6_fb_tunnel_dev);
901 static int __init sit_init(void)
905 printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
907 if (xfrm4_tunnel_register(&sit_handler, AF_INET6) < 0) {
908 printk(KERN_INFO "sit init: Can't add protocol\n");
912 ipip6_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
914 if (!ipip6_fb_tunnel_dev) {
919 ipip6_fb_tunnel_dev->init = ipip6_fb_tunnel_init;
921 if ((err = register_netdev(ipip6_fb_tunnel_dev)))
927 free_netdev(ipip6_fb_tunnel_dev);
929 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
933 module_init(sit_init);
934 module_exit(sit_cleanup);
935 MODULE_LICENSE("GPL");
936 MODULE_ALIAS("sit0");