2 * IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
3 * Linux INET6 implementation
6 * Pedro Roque <pedro_m@yahoo.com>
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
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/errno.h>
24 #include <linux/types.h>
25 #include <linux/socket.h>
26 #include <linux/sockios.h>
27 #include <linux/sched.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>
41 #include <net/protocol.h>
42 #include <net/transp_v6.h>
43 #include <net/ip6_fib.h>
44 #include <net/ip6_route.h>
45 #include <net/ndisc.h>
46 #include <net/addrconf.h>
51 #include <net/inet_ecn.h>
54 This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
56 For comments look at net/ipv4/ip_gre.c --ANK
60 #define HASH(addr) ((addr^(addr>>4))&0xF)
62 static int ipip6_fb_tunnel_init(struct net_device *dev);
63 static int ipip6_tunnel_init(struct net_device *dev);
65 static struct net_device ipip6_fb_tunnel_dev = {
67 init: ipip6_fb_tunnel_init,
70 static struct ip_tunnel ipip6_fb_tunnel = {
71 NULL, &ipip6_fb_tunnel_dev, {0, }, 0, 0, 0, 0, 0, 0, 0, {"sit0", }
74 static struct ip_tunnel *tunnels_r_l[HASH_SIZE];
75 static struct ip_tunnel *tunnels_r[HASH_SIZE];
76 static struct ip_tunnel *tunnels_l[HASH_SIZE];
77 static struct ip_tunnel *tunnels_wc[1];
78 static struct ip_tunnel **tunnels[4] = { tunnels_wc, tunnels_l, tunnels_r, tunnels_r_l };
80 static rwlock_t ipip6_lock = RW_LOCK_UNLOCKED;
82 static struct ip_tunnel * ipip6_tunnel_lookup(u32 remote, u32 local)
84 unsigned h0 = HASH(remote);
85 unsigned h1 = HASH(local);
88 for (t = tunnels_r_l[h0^h1]; t; t = t->next) {
89 if (local == t->parms.iph.saddr &&
90 remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
93 for (t = tunnels_r[h0]; t; t = t->next) {
94 if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
97 for (t = tunnels_l[h1]; t; t = t->next) {
98 if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP))
101 if ((t = tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP))
106 static struct ip_tunnel ** ipip6_bucket(struct ip_tunnel *t)
108 u32 remote = t->parms.iph.daddr;
109 u32 local = t->parms.iph.saddr;
121 return &tunnels[prio][h];
124 static void ipip6_tunnel_unlink(struct ip_tunnel *t)
126 struct ip_tunnel **tp;
128 for (tp = ipip6_bucket(t); *tp; tp = &(*tp)->next) {
130 write_lock_bh(&ipip6_lock);
132 write_unlock_bh(&ipip6_lock);
138 static void ipip6_tunnel_link(struct ip_tunnel *t)
140 struct ip_tunnel **tp = ipip6_bucket(t);
143 write_lock_bh(&ipip6_lock);
145 write_unlock_bh(&ipip6_lock);
148 struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int create)
150 u32 remote = parms->iph.daddr;
151 u32 local = parms->iph.saddr;
152 struct ip_tunnel *t, **tp, *nt;
153 struct net_device *dev;
165 for (tp = &tunnels[prio][h]; (t = *tp) != NULL; tp = &t->next) {
166 if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
173 dev = kmalloc(sizeof(*dev) + sizeof(*t), GFP_KERNEL);
178 memset(dev, 0, sizeof(*dev) + sizeof(*t));
179 dev->priv = (void*)(dev+1);
180 nt = (struct ip_tunnel*)dev->priv;
182 dev->init = ipip6_tunnel_init;
183 dev->features |= NETIF_F_DYNALLOC;
184 memcpy(&nt->parms, parms, sizeof(*parms));
185 nt->parms.name[IFNAMSIZ-1] = '\0';
186 strcpy(dev->name, nt->parms.name);
187 if (dev->name[0] == 0) {
189 for (i=1; i<100; i++) {
190 sprintf(dev->name, "sit%d", i);
191 if (__dev_get_by_name(dev->name) == NULL)
196 memcpy(nt->parms.name, dev->name, IFNAMSIZ);
198 if (register_netdevice(dev) < 0)
202 ipip6_tunnel_link(nt);
203 /* Do not decrement MOD_USE_COUNT here. */
212 static void ipip6_tunnel_destructor(struct net_device *dev)
214 if (dev != &ipip6_fb_tunnel_dev) {
219 static void ipip6_tunnel_uninit(struct net_device *dev)
221 if (dev == &ipip6_fb_tunnel_dev) {
222 write_lock_bh(&ipip6_lock);
223 tunnels_wc[0] = NULL;
224 write_unlock_bh(&ipip6_lock);
227 ipip6_tunnel_unlink((struct ip_tunnel*)dev->priv);
233 void ipip6_err(struct sk_buff *skb, u32 info)
235 #ifndef I_WISH_WORLD_WERE_PERFECT
237 /* It is not :-( All the routers (except for Linux) return only
238 8 bytes of packet payload. It means, that precise relaying of
239 ICMP in the real Internet is absolutely infeasible.
241 struct iphdr *iph = (struct iphdr*)skb->data;
242 int type = skb->h.icmph->type;
243 int code = skb->h.icmph->code;
248 case ICMP_PARAMETERPROB:
251 case ICMP_DEST_UNREACH:
254 case ICMP_PORT_UNREACH:
255 /* Impossible event. */
257 case ICMP_FRAG_NEEDED:
258 /* Soft state for pmtu is maintained by IP core. */
261 /* All others are translated to HOST_UNREACH.
262 rfc2003 contains "deep thoughts" about NET_UNREACH,
263 I believe they are just ether pollution. --ANK
268 case ICMP_TIME_EXCEEDED:
269 if (code != ICMP_EXC_TTL)
274 read_lock(&ipip6_lock);
275 t = ipip6_tunnel_lookup(iph->daddr, iph->saddr);
276 if (t == NULL || t->parms.iph.daddr == 0)
278 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
281 if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
285 t->err_time = jiffies;
287 read_unlock(&ipip6_lock);
290 struct iphdr *iph = (struct iphdr*)dp;
291 int hlen = iph->ihl<<2;
292 struct ipv6hdr *iph6;
293 int type = skb->h.icmph->type;
294 int code = skb->h.icmph->code;
298 struct sk_buff *skb2;
299 struct rt6_info *rt6i;
301 if (len < hlen + sizeof(struct ipv6hdr))
303 iph6 = (struct ipv6hdr*)(dp + hlen);
308 case ICMP_PARAMETERPROB:
309 if (skb->h.icmph->un.gateway < hlen)
312 /* So... This guy found something strange INSIDE encapsulated
313 packet. Well, he is fool, but what can we do ?
315 rel_type = ICMPV6_PARAMPROB;
316 rel_info = skb->h.icmph->un.gateway - hlen;
319 case ICMP_DEST_UNREACH:
322 case ICMP_PORT_UNREACH:
323 /* Impossible event. */
325 case ICMP_FRAG_NEEDED:
326 /* Too complicated case ... */
329 /* All others are translated to HOST_UNREACH.
330 rfc2003 contains "deep thoughts" about NET_UNREACH,
331 I believe, it is just ether pollution. --ANK
333 rel_type = ICMPV6_DEST_UNREACH;
334 rel_code = ICMPV6_ADDR_UNREACH;
338 case ICMP_TIME_EXCEEDED:
339 if (code != ICMP_EXC_TTL)
341 rel_type = ICMPV6_TIME_EXCEED;
342 rel_code = ICMPV6_EXC_HOPLIMIT;
346 /* Prepare fake skb to feed it to icmpv6_send */
347 skb2 = skb_clone(skb, GFP_ATOMIC);
350 dst_release(skb2->dst);
352 skb_pull(skb2, skb->data - (u8*)iph6);
353 skb2->nh.raw = skb2->data;
355 /* Try to guess incoming interface */
356 rt6i = rt6_lookup(&iph6->saddr, NULL, NULL, 0);
357 if (rt6i && rt6i->rt6i_dev) {
358 skb2->dev = rt6i->rt6i_dev;
360 rt6i = rt6_lookup(&iph6->daddr, &iph6->saddr, NULL, 0);
362 if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
363 struct ip_tunnel * t = (struct ip_tunnel*)rt6i->rt6i_dev->priv;
364 if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
365 rel_type = ICMPV6_DEST_UNREACH;
366 rel_code = ICMPV6_ADDR_UNREACH;
368 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
376 static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
378 if (INET_ECN_is_ce(iph->tos) &&
379 INET_ECN_is_not_ce(ip6_get_dsfield(skb->nh.ipv6h)))
380 IP6_ECN_set_ce(skb->nh.ipv6h);
383 int ipip6_rcv(struct sk_buff *skb)
386 struct ip_tunnel *tunnel;
388 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
393 read_lock(&ipip6_lock);
394 if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
395 skb->mac.raw = skb->nh.raw;
396 skb->nh.raw = skb->data;
397 memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
398 skb->protocol = htons(ETH_P_IPV6);
399 skb->pkt_type = PACKET_HOST;
400 tunnel->stat.rx_packets++;
401 tunnel->stat.rx_bytes += skb->len;
402 skb->dev = tunnel->dev;
403 dst_release(skb->dst);
406 ipip6_ecn_decapsulate(iph, skb);
408 read_unlock(&ipip6_lock);
412 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, 0);
413 read_unlock(&ipip6_lock);
419 /* Need this wrapper because NF_HOOK takes the function address */
420 static inline int do_ip_send(struct sk_buff *skb)
426 /* Returns the embedded IPv4 address if the IPv6 address
427 comes from 6to4 (draft-ietf-ngtrans-6to4-04) addr space */
429 static inline u32 try_6to4(struct in6_addr *v6dst)
433 if (v6dst->s6_addr16[0] == htons(0x2002)) {
434 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
435 memcpy(&dst, &v6dst->s6_addr16[1], 4);
441 * This function assumes it is being called from dev_queue_xmit()
442 * and that skb is filled properly by that function.
445 static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
447 struct ip_tunnel *tunnel = (struct ip_tunnel*)dev->priv;
448 struct net_device_stats *stats = &tunnel->stat;
449 struct iphdr *tiph = &tunnel->parms.iph;
450 struct ipv6hdr *iph6 = skb->nh.ipv6h;
451 u8 tos = tunnel->parms.iph.tos;
452 struct rtable *rt; /* Route to the other host */
453 struct net_device *tdev; /* Device to other host */
454 struct iphdr *iph; /* Our new IP header */
455 int max_headroom; /* The extra header space needed */
456 u32 dst = tiph->daddr;
458 struct in6_addr *addr6;
461 if (tunnel->recursion++) {
462 tunnel->stat.collisions++;
466 if (skb->protocol != htons(ETH_P_IPV6))
470 dst = try_6to4(&iph6->daddr);
473 struct neighbour *neigh = NULL;
476 neigh = skb->dst->neighbour;
480 printk(KERN_DEBUG "sit: nexthop == NULL\n");
484 addr6 = (struct in6_addr*)&neigh->primary_key;
485 addr_type = ipv6_addr_type(addr6);
487 if (addr_type == IPV6_ADDR_ANY) {
488 addr6 = &skb->nh.ipv6h->daddr;
489 addr_type = ipv6_addr_type(addr6);
492 if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
495 dst = addr6->s6_addr32[3];
498 if (ip_route_output(&rt, dst, tiph->saddr, RT_TOS(tos), tunnel->parms.link)) {
499 tunnel->stat.tx_carrier_errors++;
502 if (rt->rt_type != RTN_UNICAST) {
504 tunnel->stat.tx_carrier_errors++;
507 tdev = rt->u.dst.dev;
511 tunnel->stat.collisions++;
516 mtu = rt->u.dst.pmtu - sizeof(struct iphdr);
518 mtu = skb->dst ? skb->dst->pmtu : dev->mtu;
521 tunnel->stat.collisions++;
525 if (mtu < IPV6_MIN_MTU)
527 if (skb->dst && mtu < skb->dst->pmtu) {
528 struct rt6_info *rt6 = (struct rt6_info*)skb->dst;
529 if (mtu < rt6->u.dst.pmtu) {
530 if (tunnel->parms.iph.daddr || rt6->rt6i_dst.plen == 128) {
531 rt6->rt6i_flags |= RTF_MODIFIED;
532 rt6->u.dst.pmtu = mtu;
536 if (skb->len > mtu) {
537 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
542 if (tunnel->err_count > 0) {
543 if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
545 dst_link_failure(skb);
547 tunnel->err_count = 0;
551 * Okay, now see if we can stuff it in the buffer as-is.
553 max_headroom = (((tdev->hard_header_len+15)&~15)+sizeof(struct iphdr));
555 if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
556 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
565 skb_set_owner_w(new_skb, skb->sk);
568 iph6 = skb->nh.ipv6h;
571 skb->h.raw = skb->nh.raw;
572 skb->nh.raw = skb_push(skb, sizeof(struct iphdr));
573 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
574 dst_release(skb->dst);
575 skb->dst = &rt->u.dst;
578 * Push down and install the IPIP header.
583 iph->ihl = sizeof(struct iphdr)>>2;
584 if (mtu > IPV6_MIN_MTU)
585 iph->frag_off = htons(IP_DF);
589 iph->protocol = IPPROTO_IPV6;
590 iph->tos = INET_ECN_encapsulate(tos, ip6_get_dsfield(iph6));
591 iph->daddr = rt->rt_dst;
592 iph->saddr = rt->rt_src;
594 if ((iph->ttl = tiph->ttl) == 0)
595 iph->ttl = iph6->hop_limit;
604 dst_link_failure(skb);
613 ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
616 struct ip_tunnel_parm p;
624 if (dev == &ipip6_fb_tunnel_dev) {
625 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
629 t = ipip6_tunnel_locate(&p, 0);
632 t = (struct ip_tunnel*)dev->priv;
633 memcpy(&p, &t->parms, sizeof(p));
634 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
641 if (!capable(CAP_NET_ADMIN))
645 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
649 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
650 p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
653 p.iph.frag_off |= htons(IP_DF);
655 t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
657 if (dev != &ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL &&
658 t != &ipip6_fb_tunnel) {
665 if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
666 (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
670 t = (struct ip_tunnel*)dev->priv;
671 ipip6_tunnel_unlink(t);
672 t->parms.iph.saddr = p.iph.saddr;
673 t->parms.iph.daddr = p.iph.daddr;
674 memcpy(dev->dev_addr, &p.iph.saddr, 4);
675 memcpy(dev->broadcast, &p.iph.daddr, 4);
676 ipip6_tunnel_link(t);
677 netdev_state_change(dev);
683 if (cmd == SIOCCHGTUNNEL) {
684 t->parms.iph.ttl = p.iph.ttl;
685 t->parms.iph.tos = p.iph.tos;
687 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
690 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
695 if (!capable(CAP_NET_ADMIN))
698 if (dev == &ipip6_fb_tunnel_dev) {
700 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
703 if ((t = ipip6_tunnel_locate(&p, 0)) == NULL)
706 if (t == &ipip6_fb_tunnel)
710 err = unregister_netdevice(dev);
722 static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
724 return &(((struct ip_tunnel*)dev->priv)->stat);
727 static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
729 if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
735 static void ipip6_tunnel_init_gen(struct net_device *dev)
737 struct ip_tunnel *t = (struct ip_tunnel*)dev->priv;
739 dev->destructor = ipip6_tunnel_destructor;
740 dev->uninit = ipip6_tunnel_uninit;
741 dev->hard_start_xmit = ipip6_tunnel_xmit;
742 dev->get_stats = ipip6_tunnel_get_stats;
743 dev->do_ioctl = ipip6_tunnel_ioctl;
744 dev->change_mtu = ipip6_tunnel_change_mtu;
746 dev->type = ARPHRD_SIT;
747 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
748 dev->mtu = 1500 - sizeof(struct iphdr);
749 dev->flags = IFF_NOARP;
752 memcpy(dev->dev_addr, &t->parms.iph.saddr, 4);
753 memcpy(dev->broadcast, &t->parms.iph.daddr, 4);
756 static int ipip6_tunnel_init(struct net_device *dev)
758 struct net_device *tdev = NULL;
759 struct ip_tunnel *tunnel;
762 tunnel = (struct ip_tunnel*)dev->priv;
763 iph = &tunnel->parms.iph;
765 ipip6_tunnel_init_gen(dev);
769 if (!ip_route_output(&rt, iph->daddr, iph->saddr, RT_TOS(iph->tos), tunnel->parms.link)) {
770 tdev = rt->u.dst.dev;
773 dev->flags |= IFF_POINTOPOINT;
776 if (!tdev && tunnel->parms.link)
777 tdev = __dev_get_by_index(tunnel->parms.link);
780 dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
781 dev->mtu = tdev->mtu - sizeof(struct iphdr);
782 if (dev->mtu < IPV6_MIN_MTU)
783 dev->mtu = IPV6_MIN_MTU;
785 dev->iflink = tunnel->parms.link;
791 static int ipip6_fb_tunnel_open(struct net_device *dev)
797 static int ipip6_fb_tunnel_close(struct net_device *dev)
804 int __init ipip6_fb_tunnel_init(struct net_device *dev)
808 ipip6_tunnel_init_gen(dev);
810 dev->open = ipip6_fb_tunnel_open;
811 dev->stop = ipip6_fb_tunnel_close;
814 iph = &ipip6_fb_tunnel.parms.iph;
816 iph->protocol = IPPROTO_IPV6;
821 tunnels_wc[0] = &ipip6_fb_tunnel;
825 static struct inet_protocol sit_protocol = {
836 void sit_cleanup(void)
838 inet_del_protocol(&sit_protocol);
839 unregister_netdev(&ipip6_fb_tunnel_dev);
843 int __init sit_init(void)
845 printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
847 ipip6_fb_tunnel_dev.priv = (void*)&ipip6_fb_tunnel;
848 strcpy(ipip6_fb_tunnel_dev.name, ipip6_fb_tunnel.parms.name);
849 register_netdev(&ipip6_fb_tunnel_dev);
850 inet_add_protocol(&sit_protocol);