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.1.1.1 2005/04/11 02:51:13 jack 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 #define __NO_VERSION__
22 #include <linux/config.h>
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/socket.h>
27 #include <linux/sockios.h>
28 #include <linux/sched.h>
29 #include <linux/net.h>
30 #include <linux/in6.h>
31 #include <linux/netdevice.h>
32 #include <linux/if_arp.h>
33 #include <linux/icmp.h>
34 #include <asm/uaccess.h>
35 #include <linux/init.h>
36 #include <linux/netfilter_ipv4.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>
55 This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
57 For comments look at net/ipv4/ip_gre.c --ANK
61 #define HASH(addr) ((addr^(addr>>4))&0xF)
63 static int ipip6_fb_tunnel_init(struct net_device *dev);
64 static int ipip6_tunnel_init(struct net_device *dev);
66 static struct net_device ipip6_fb_tunnel_dev = {
68 init: ipip6_fb_tunnel_init,
71 static struct ip_tunnel ipip6_fb_tunnel = {
72 NULL, &ipip6_fb_tunnel_dev, {0, }, 0, 0, 0, 0, 0, 0, 0, {"sit0", }
75 static struct ip_tunnel *tunnels_r_l[HASH_SIZE];
76 static struct ip_tunnel *tunnels_r[HASH_SIZE];
77 static struct ip_tunnel *tunnels_l[HASH_SIZE];
78 static struct ip_tunnel *tunnels_wc[1];
79 static struct ip_tunnel **tunnels[4] = { tunnels_wc, tunnels_l, tunnels_r, tunnels_r_l };
81 static rwlock_t ipip6_lock = RW_LOCK_UNLOCKED;
83 static struct ip_tunnel * ipip6_tunnel_lookup(u32 remote, u32 local)
85 unsigned h0 = HASH(remote);
86 unsigned h1 = HASH(local);
89 for (t = tunnels_r_l[h0^h1]; t; t = t->next) {
90 if (local == t->parms.iph.saddr &&
91 remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
94 for (t = tunnels_r[h0]; t; t = t->next) {
95 if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
98 for (t = tunnels_l[h1]; t; t = t->next) {
99 if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP))
102 if ((t = tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP))
107 static struct ip_tunnel ** ipip6_bucket(struct ip_tunnel *t)
109 u32 remote = t->parms.iph.daddr;
110 u32 local = t->parms.iph.saddr;
122 return &tunnels[prio][h];
125 static void ipip6_tunnel_unlink(struct ip_tunnel *t)
127 struct ip_tunnel **tp;
129 for (tp = ipip6_bucket(t); *tp; tp = &(*tp)->next) {
131 write_lock_bh(&ipip6_lock);
133 write_unlock_bh(&ipip6_lock);
139 static void ipip6_tunnel_link(struct ip_tunnel *t)
141 struct ip_tunnel **tp = ipip6_bucket(t);
143 write_lock_bh(&ipip6_lock);
145 write_unlock_bh(&ipip6_lock);
149 struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int create)
151 u32 remote = parms->iph.daddr;
152 u32 local = parms->iph.saddr;
153 struct ip_tunnel *t, **tp, *nt;
154 struct net_device *dev;
166 for (tp = &tunnels[prio][h]; (t = *tp) != NULL; tp = &t->next) {
167 if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
174 dev = kmalloc(sizeof(*dev) + sizeof(*t), GFP_KERNEL);
179 memset(dev, 0, sizeof(*dev) + sizeof(*t));
180 dev->priv = (void*)(dev+1);
181 nt = (struct ip_tunnel*)dev->priv;
183 dev->init = ipip6_tunnel_init;
184 dev->features |= NETIF_F_DYNALLOC;
185 memcpy(&nt->parms, parms, sizeof(*parms));
186 nt->parms.name[IFNAMSIZ-1] = '\0';
187 strcpy(dev->name, nt->parms.name);
188 if (dev->name[0] == 0) {
190 for (i=1; i<100; i++) {
191 sprintf(dev->name, "sit%d", i);
192 if (__dev_get_by_name(dev->name) == NULL)
197 memcpy(nt->parms.name, dev->name, IFNAMSIZ);
199 if (register_netdevice(dev) < 0)
203 ipip6_tunnel_link(nt);
204 /* Do not decrement MOD_USE_COUNT here. */
213 static void ipip6_tunnel_destructor(struct net_device *dev)
215 if (dev != &ipip6_fb_tunnel_dev) {
220 static void ipip6_tunnel_uninit(struct net_device *dev)
222 if (dev == &ipip6_fb_tunnel_dev) {
223 write_lock_bh(&ipip6_lock);
224 tunnels_wc[0] = NULL;
225 write_unlock_bh(&ipip6_lock);
228 ipip6_tunnel_unlink((struct ip_tunnel*)dev->priv);
234 void ipip6_err(struct sk_buff *skb, u32 info)
236 #ifndef I_WISH_WORLD_WERE_PERFECT
238 /* It is not :-( All the routers (except for Linux) return only
239 8 bytes of packet payload. It means, that precise relaying of
240 ICMP in the real Internet is absolutely infeasible.
242 struct iphdr *iph = (struct iphdr*)skb->data;
243 int type = skb->h.icmph->type;
244 int code = skb->h.icmph->code;
249 case ICMP_PARAMETERPROB:
252 case ICMP_DEST_UNREACH:
255 case ICMP_PORT_UNREACH:
256 /* Impossible event. */
258 case ICMP_FRAG_NEEDED:
259 /* Soft state for pmtu is maintained by IP core. */
262 /* All others are translated to HOST_UNREACH.
263 rfc2003 contains "deep thoughts" about NET_UNREACH,
264 I believe they are just ether pollution. --ANK
269 case ICMP_TIME_EXCEEDED:
270 if (code != ICMP_EXC_TTL)
275 read_lock(&ipip6_lock);
276 t = ipip6_tunnel_lookup(iph->daddr, iph->saddr);
277 if (t == NULL || t->parms.iph.daddr == 0)
279 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
282 if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
286 t->err_time = jiffies;
288 read_unlock(&ipip6_lock);
291 struct iphdr *iph = (struct iphdr*)dp;
292 int hlen = iph->ihl<<2;
293 struct ipv6hdr *iph6;
294 int type = skb->h.icmph->type;
295 int code = skb->h.icmph->code;
299 struct sk_buff *skb2;
300 struct rt6_info *rt6i;
302 if (len < hlen + sizeof(struct ipv6hdr))
304 iph6 = (struct ipv6hdr*)(dp + hlen);
309 case ICMP_PARAMETERPROB:
310 if (skb->h.icmph->un.gateway < hlen)
313 /* So... This guy found something strange INSIDE encapsulated
314 packet. Well, he is fool, but what can we do ?
316 rel_type = ICMPV6_PARAMPROB;
317 rel_info = skb->h.icmph->un.gateway - hlen;
320 case ICMP_DEST_UNREACH:
323 case ICMP_PORT_UNREACH:
324 /* Impossible event. */
326 case ICMP_FRAG_NEEDED:
327 /* Too complicated case ... */
330 /* All others are translated to HOST_UNREACH.
331 rfc2003 contains "deep thoughts" about NET_UNREACH,
332 I believe, it is just ether pollution. --ANK
334 rel_type = ICMPV6_DEST_UNREACH;
335 rel_code = ICMPV6_ADDR_UNREACH;
339 case ICMP_TIME_EXCEEDED:
340 if (code != ICMP_EXC_TTL)
342 rel_type = ICMPV6_TIME_EXCEED;
343 rel_code = ICMPV6_EXC_HOPLIMIT;
347 /* Prepare fake skb to feed it to icmpv6_send */
348 skb2 = skb_clone(skb, GFP_ATOMIC);
351 dst_release(skb2->dst);
353 skb_pull(skb2, skb->data - (u8*)iph6);
354 skb2->nh.raw = skb2->data;
356 /* Try to guess incoming interface */
357 rt6i = rt6_lookup(&iph6->saddr, NULL, NULL, 0);
358 if (rt6i && rt6i->rt6i_dev) {
359 skb2->dev = rt6i->rt6i_dev;
361 rt6i = rt6_lookup(&iph6->daddr, &iph6->saddr, NULL, 0);
363 if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
364 struct ip_tunnel * t = (struct ip_tunnel*)rt6i->rt6i_dev->priv;
365 if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
366 rel_type = ICMPV6_DEST_UNREACH;
367 rel_code = ICMPV6_ADDR_UNREACH;
369 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
377 static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
379 if (INET_ECN_is_ce(iph->tos) &&
380 INET_ECN_is_not_ce(ip6_get_dsfield(skb->nh.ipv6h)))
381 IP6_ECN_set_ce(skb->nh.ipv6h);
384 int ipip6_rcv(struct sk_buff *skb)
387 struct ip_tunnel *tunnel;
389 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
394 read_lock(&ipip6_lock);
395 if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
396 skb->mac.raw = skb->nh.raw;
397 skb->nh.raw = skb->data;
398 memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
399 skb->protocol = htons(ETH_P_IPV6);
400 skb->pkt_type = PACKET_HOST;
401 tunnel->stat.rx_packets++;
402 tunnel->stat.rx_bytes += skb->len;
403 skb->dev = tunnel->dev;
404 dst_release(skb->dst);
406 #ifdef CONFIG_NETFILTER
407 nf_conntrack_put(skb->nfct);
409 #ifdef CONFIG_NETFILTER_DEBUG
413 ipip6_ecn_decapsulate(iph, skb);
415 read_unlock(&ipip6_lock);
419 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, 0);
421 read_unlock(&ipip6_lock);
426 /* Need this wrapper because NF_HOOK takes the function address */
427 static inline int do_ip_send(struct sk_buff *skb)
433 /* Returns the embedded IPv4 address if the IPv6 address
434 comes from 6to4 (draft-ietf-ngtrans-6to4-04) addr space */
436 static inline u32 try_6to4(struct in6_addr *v6dst)
440 if (v6dst->s6_addr16[0] == htons(0x2002)) {
441 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
442 memcpy(&dst, &v6dst->s6_addr16[1], 4);
448 * This function assumes it is being called from dev_queue_xmit()
449 * and that skb is filled properly by that function.
452 static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
454 struct ip_tunnel *tunnel = (struct ip_tunnel*)dev->priv;
455 struct net_device_stats *stats = &tunnel->stat;
456 struct iphdr *tiph = &tunnel->parms.iph;
457 struct ipv6hdr *iph6 = skb->nh.ipv6h;
458 u8 tos = tunnel->parms.iph.tos;
459 struct rtable *rt; /* Route to the other host */
460 struct net_device *tdev; /* Device to other host */
461 struct iphdr *iph; /* Our new IP header */
462 int max_headroom; /* The extra header space needed */
463 u32 dst = tiph->daddr;
465 struct in6_addr *addr6;
468 if (tunnel->recursion++) {
469 tunnel->stat.collisions++;
473 if (skb->protocol != htons(ETH_P_IPV6))
477 dst = try_6to4(&iph6->daddr);
480 struct neighbour *neigh = NULL;
483 neigh = skb->dst->neighbour;
487 printk(KERN_DEBUG "sit: nexthop == NULL\n");
491 addr6 = (struct in6_addr*)&neigh->primary_key;
492 addr_type = ipv6_addr_type(addr6);
494 if (addr_type == IPV6_ADDR_ANY) {
495 addr6 = &skb->nh.ipv6h->daddr;
496 addr_type = ipv6_addr_type(addr6);
499 if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
502 dst = addr6->s6_addr32[3];
505 if (ip_route_output(&rt, dst, tiph->saddr, RT_TOS(tos), tunnel->parms.link)) {
506 tunnel->stat.tx_carrier_errors++;
509 if (rt->rt_type != RTN_UNICAST) {
510 tunnel->stat.tx_carrier_errors++;
513 tdev = rt->u.dst.dev;
517 tunnel->stat.collisions++;
522 mtu = rt->u.dst.pmtu - sizeof(struct iphdr);
524 mtu = skb->dst ? skb->dst->pmtu : dev->mtu;
527 tunnel->stat.collisions++;
531 if (mtu < IPV6_MIN_MTU)
533 if (skb->dst && mtu < skb->dst->pmtu) {
534 struct rt6_info *rt6 = (struct rt6_info*)skb->dst;
535 if (mtu < rt6->u.dst.pmtu) {
536 if (tunnel->parms.iph.daddr || rt6->rt6i_dst.plen == 128) {
537 rt6->rt6i_flags |= RTF_MODIFIED;
538 rt6->u.dst.pmtu = mtu;
542 if (skb->len > mtu) {
543 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
548 if (tunnel->err_count > 0) {
549 if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
551 dst_link_failure(skb);
553 tunnel->err_count = 0;
556 skb->h.raw = skb->nh.raw;
559 * Okay, now see if we can stuff it in the buffer as-is.
561 max_headroom = (((tdev->hard_header_len+15)&~15)+sizeof(struct iphdr));
563 if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
564 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
573 skb_set_owner_w(new_skb, skb->sk);
578 skb->nh.raw = skb_push(skb, sizeof(struct iphdr));
579 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
580 dst_release(skb->dst);
581 skb->dst = &rt->u.dst;
584 * Push down and install the IPIP header.
589 iph->ihl = sizeof(struct iphdr)>>2;
590 if (mtu > IPV6_MIN_MTU)
591 iph->frag_off = htons(IP_DF);
595 iph->protocol = IPPROTO_IPV6;
596 iph->tos = INET_ECN_encapsulate(tos, ip6_get_dsfield(iph6));
597 iph->daddr = rt->rt_dst;
598 iph->saddr = rt->rt_src;
600 if ((iph->ttl = tiph->ttl) == 0)
601 iph->ttl = iph6->hop_limit;
603 #ifdef CONFIG_NETFILTER
604 nf_conntrack_put(skb->nfct);
606 #ifdef CONFIG_NETFILTER_DEBUG
616 dst_link_failure(skb);
625 ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
628 struct ip_tunnel_parm p;
636 if (dev == &ipip6_fb_tunnel_dev) {
637 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
641 t = ipip6_tunnel_locate(&p, 0);
644 t = (struct ip_tunnel*)dev->priv;
645 memcpy(&p, &t->parms, sizeof(p));
646 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
653 if (!capable(CAP_NET_ADMIN))
657 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
661 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
662 p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
665 p.iph.frag_off |= htons(IP_DF);
667 t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
669 if (dev != &ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL &&
670 t != &ipip6_fb_tunnel) {
677 if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
678 (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
682 t = (struct ip_tunnel*)dev->priv;
683 ipip6_tunnel_unlink(t);
684 t->parms.iph.saddr = p.iph.saddr;
685 t->parms.iph.daddr = p.iph.daddr;
686 memcpy(dev->dev_addr, &p.iph.saddr, 4);
687 memcpy(dev->broadcast, &p.iph.daddr, 4);
688 ipip6_tunnel_link(t);
689 netdev_state_change(dev);
695 if (cmd == SIOCCHGTUNNEL) {
696 t->parms.iph.ttl = p.iph.ttl;
697 t->parms.iph.tos = p.iph.tos;
699 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
702 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
707 if (!capable(CAP_NET_ADMIN))
710 if (dev == &ipip6_fb_tunnel_dev) {
712 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
715 if ((t = ipip6_tunnel_locate(&p, 0)) == NULL)
718 if (t == &ipip6_fb_tunnel)
722 err = unregister_netdevice(dev);
734 static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
736 return &(((struct ip_tunnel*)dev->priv)->stat);
739 static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
741 if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
747 static void ipip6_tunnel_init_gen(struct net_device *dev)
749 struct ip_tunnel *t = (struct ip_tunnel*)dev->priv;
751 dev->destructor = ipip6_tunnel_destructor;
752 dev->uninit = ipip6_tunnel_uninit;
753 dev->hard_start_xmit = ipip6_tunnel_xmit;
754 dev->get_stats = ipip6_tunnel_get_stats;
755 dev->do_ioctl = ipip6_tunnel_ioctl;
756 dev->change_mtu = ipip6_tunnel_change_mtu;
758 dev->type = ARPHRD_SIT;
759 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
760 dev->mtu = 1500 - sizeof(struct iphdr);
761 dev->flags = IFF_NOARP;
764 memcpy(dev->dev_addr, &t->parms.iph.saddr, 4);
765 memcpy(dev->broadcast, &t->parms.iph.daddr, 4);
768 static int ipip6_tunnel_init(struct net_device *dev)
770 struct net_device *tdev = NULL;
771 struct ip_tunnel *tunnel;
774 tunnel = (struct ip_tunnel*)dev->priv;
775 iph = &tunnel->parms.iph;
777 ipip6_tunnel_init_gen(dev);
781 if (!ip_route_output(&rt, iph->daddr, iph->saddr, RT_TOS(iph->tos), tunnel->parms.link)) {
782 tdev = rt->u.dst.dev;
785 dev->flags |= IFF_POINTOPOINT;
788 if (!tdev && tunnel->parms.link)
789 tdev = __dev_get_by_index(tunnel->parms.link);
792 dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
793 dev->mtu = tdev->mtu - sizeof(struct iphdr);
794 if (dev->mtu < IPV6_MIN_MTU)
795 dev->mtu = IPV6_MIN_MTU;
797 dev->iflink = tunnel->parms.link;
803 static int ipip6_fb_tunnel_open(struct net_device *dev)
809 static int ipip6_fb_tunnel_close(struct net_device *dev)
816 int __init ipip6_fb_tunnel_init(struct net_device *dev)
820 ipip6_tunnel_init_gen(dev);
822 dev->open = ipip6_fb_tunnel_open;
823 dev->stop = ipip6_fb_tunnel_close;
826 iph = &ipip6_fb_tunnel.parms.iph;
828 iph->protocol = IPPROTO_IPV6;
833 tunnels_wc[0] = &ipip6_fb_tunnel;
837 static struct inet_protocol sit_protocol = {
848 void sit_cleanup(void)
850 inet_del_protocol(&sit_protocol);
851 unregister_netdev(&ipip6_fb_tunnel_dev);
855 int __init sit_init(void)
857 printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
859 ipip6_fb_tunnel_dev.priv = (void*)&ipip6_fb_tunnel;
860 strcpy(ipip6_fb_tunnel_dev.name, ipip6_fb_tunnel.parms.name);
861 register_netdev(&ipip6_fb_tunnel_dev);
862 inet_add_protocol(&sit_protocol);