[IPV6] SIT: Disallow 0.0.0.0 in PRL and Flush PRL if given for DEL.
[safe/jmp/linux-2.6] / net / ipv6 / sit.c
index 85ff3dc..84c1ed2 100644 (file)
@@ -3,7 +3,7 @@
  *     Linux INET6 implementation
  *
  *     Authors:
- *     Pedro Roque             <roque@di.fc.ul.pt>     
+ *     Pedro Roque             <roque@di.fc.ul.pt>
  *     Alexey Kuznetsov        <kuznet@ms2.inr.ac.ru>
  *
  *     $Id: sit.c,v 1.53 2001/09/25 05:09:53 davem Exp $
@@ -16,6 +16,7 @@
  *     Changes:
  * Roger Venning <r.venning@telstra.com>:      6to4 support
  * Nate Thompson <nate@thebog.net>:            6to4 support
+ * Fred Templin <fred.l.templin@boeing.com>:   isatap support
  */
 
 #include <linux/module.h>
@@ -24,7 +25,6 @@
 #include <linux/types.h>
 #include <linux/socket.h>
 #include <linux/sockios.h>
-#include <linux/sched.h>
 #include <linux/net.h>
 #include <linux/in6.h>
 #include <linux/netdevice.h>
@@ -100,10 +100,10 @@ static struct ip_tunnel * ipip6_tunnel_lookup(__be32 remote, __be32 local)
        return NULL;
 }
 
-static struct ip_tunnel ** ipip6_bucket(struct ip_tunnel *t)
+static struct ip_tunnel **__ipip6_bucket(struct ip_tunnel_parm *parms)
 {
-       __be32 remote = t->parms.iph.daddr;
-       __be32 local = t->parms.iph.saddr;
+       __be32 remote = parms->iph.daddr;
+       __be32 local = parms->iph.saddr;
        unsigned h = 0;
        int prio = 0;
 
@@ -118,6 +118,11 @@ static struct ip_tunnel ** ipip6_bucket(struct ip_tunnel *t)
        return &tunnels[prio][h];
 }
 
+static inline struct ip_tunnel **ipip6_bucket(struct ip_tunnel *t)
+{
+       return __ipip6_bucket(&t->parms);
+}
+
 static void ipip6_tunnel_unlink(struct ip_tunnel *t)
 {
        struct ip_tunnel **tp;
@@ -148,19 +153,9 @@ static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int
        __be32 local = parms->iph.saddr;
        struct ip_tunnel *t, **tp, *nt;
        struct net_device *dev;
-       unsigned h = 0;
-       int prio = 0;
        char name[IFNAMSIZ];
 
-       if (remote) {
-               prio |= 2;
-               h ^= HASH(remote);
-       }
-       if (local) {
-               prio |= 1;
-               h ^= HASH(local);
-       }
-       for (tp = &tunnels[prio][h]; (t = *tp) != NULL; tp = &t->next) {
+       for (tp = __ipip6_bucket(parms); (t = *tp) != NULL; tp = &t->next) {
                if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
                        return t;
        }
@@ -169,39 +164,172 @@ static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int
 
        if (parms->name[0])
                strlcpy(name, parms->name, IFNAMSIZ);
-       else {
-               int i;
-               for (i=1; i<100; i++) {
-                       sprintf(name, "sit%d", i);
-                       if (__dev_get_by_name(name) == NULL)
-                               break;
-               }
-               if (i==100)
-                       goto failed;
-       }
+       else
+               sprintf(name, "sit%%d");
 
        dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
        if (dev == NULL)
                return NULL;
 
+       if (strchr(name, '%')) {
+               if (dev_alloc_name(dev, name) < 0)
+                       goto failed_free;
+       }
+
        nt = netdev_priv(dev);
        dev->init = ipip6_tunnel_init;
        nt->parms = *parms;
 
-       if (register_netdevice(dev) < 0) {
-               free_netdev(dev);
-               goto failed;
-       }
+       if (parms->i_flags & SIT_ISATAP)
+               dev->priv_flags |= IFF_ISATAP;
+
+       if (register_netdevice(dev) < 0)
+               goto failed_free;
 
        dev_hold(dev);
 
        ipip6_tunnel_link(nt);
        return nt;
 
+failed_free:
+       free_netdev(dev);
 failed:
        return NULL;
 }
 
+static struct ip_tunnel_prl_entry *
+__ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
+{
+       struct ip_tunnel_prl_entry *p = (struct ip_tunnel_prl_entry *)NULL;
+
+       for (p = t->prl; p; p = p->next)
+               if (p->entry.addr == addr)
+                       break;
+       return p;
+
+}
+
+static int
+ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
+{
+       struct ip_tunnel_prl_entry *p;
+       int err = 0;
+
+       if (a->addr == htonl(INADDR_ANY))
+               return -EINVAL;
+
+       write_lock(&ipip6_lock);
+
+       for (p = t->prl; p; p = p->next) {
+               if (p->entry.addr == a->addr) {
+                       if (chg)
+                               goto update;
+                       err = -EEXIST;
+                       goto out;
+               }
+       }
+
+       if (chg) {
+               err = -ENXIO;
+               goto out;
+       }
+
+       p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
+       if (!p) {
+               err = -ENOBUFS;
+               goto out;
+       }
+
+       p->next = t->prl;
+       t->prl = p;
+update:
+       p->entry = *a;
+out:
+       write_unlock(&ipip6_lock);
+       return err;
+}
+
+static int
+ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
+{
+       struct ip_tunnel_prl_entry *x, **p;
+       int err = 0;
+
+       write_lock(&ipip6_lock);
+
+       if (a && a->addr != htonl(INADDR_ANY)) {
+               for (p = &t->prl; *p; p = &(*p)->next) {
+                       if ((*p)->entry.addr == a->addr) {
+                               x = *p;
+                               *p = x->next;
+                               kfree(x);
+                               goto out;
+                       }
+               }
+               err = -ENXIO;
+       } else {
+               while (t->prl) {
+                       x = t->prl;
+                       t->prl = t->prl->next;
+                       kfree(x);
+               }
+       }
+out:
+       write_unlock(&ipip6_lock);
+       return 0;
+}
+
+/* copied directly from anycast.c */
+static int
+ipip6_onlink(struct in6_addr *addr, struct net_device *dev)
+{
+       struct inet6_dev        *idev;
+       struct inet6_ifaddr     *ifa;
+       int     onlink;
+
+       onlink = 0;
+       rcu_read_lock();
+       idev = __in6_dev_get(dev);
+       if (idev) {
+               read_lock_bh(&idev->lock);
+               for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
+                       onlink = ipv6_prefix_equal(addr, &ifa->addr,
+                                                  ifa->prefix_len);
+                       if (onlink)
+                               break;
+               }
+               read_unlock_bh(&idev->lock);
+       }
+       rcu_read_unlock();
+       return onlink;
+}
+
+static int
+isatap_chksrc(struct sk_buff *skb, struct iphdr *iph, struct ip_tunnel *t)
+{
+       struct ip_tunnel_prl_entry *p;
+       int ok = 1;
+
+       read_lock(&ipip6_lock);
+       p = __ipip6_tunnel_locate_prl(t, iph->saddr);
+       if (p) {
+               if (p->entry.flags & PRL_DEFAULT)
+                       skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
+               else
+                       skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
+       } else {
+               struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
+               if (ipv6_addr_is_isatap(addr6) &&
+                   (addr6->s6_addr32[3] == iph->saddr) &&
+                   ipip6_onlink(addr6, t->dev))
+                       skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
+               else
+                       ok = 0;
+       }
+       read_unlock(&ipip6_lock);
+       return ok;
+}
+
 static void ipip6_tunnel_uninit(struct net_device *dev)
 {
        if (dev == ipip6_fb_tunnel_dev) {
@@ -211,12 +339,13 @@ static void ipip6_tunnel_uninit(struct net_device *dev)
                dev_put(dev);
        } else {
                ipip6_tunnel_unlink(netdev_priv(dev));
+               ipip6_tunnel_del_prl(netdev_priv(dev), 0);
                dev_put(dev);
        }
 }
 
 
-static void ipip6_err(struct sk_buff *skb, u32 info)
+static int ipip6_err(struct sk_buff *skb, u32 info)
 {
 #ifndef I_WISH_WORLD_WERE_PERFECT
 
@@ -225,24 +354,25 @@ static void ipip6_err(struct sk_buff *skb, u32 info)
    ICMP in the real Internet is absolutely infeasible.
  */
        struct iphdr *iph = (struct iphdr*)skb->data;
-       int type = skb->h.icmph->type;
-       int code = skb->h.icmph->code;
+       const int type = icmp_hdr(skb)->type;
+       const int code = icmp_hdr(skb)->code;
        struct ip_tunnel *t;
+       int err;
 
        switch (type) {
        default:
        case ICMP_PARAMETERPROB:
-               return;
+               return 0;
 
        case ICMP_DEST_UNREACH:
                switch (code) {
                case ICMP_SR_FAILED:
                case ICMP_PORT_UNREACH:
                        /* Impossible event. */
-                       return;
+                       return 0;
                case ICMP_FRAG_NEEDED:
                        /* Soft state for pmtu is maintained by IP core. */
-                       return;
+                       return 0;
                default:
                        /* All others are translated to HOST_UNREACH.
                           rfc2003 contains "deep thoughts" about NET_UNREACH,
@@ -253,14 +383,18 @@ static void ipip6_err(struct sk_buff *skb, u32 info)
                break;
        case ICMP_TIME_EXCEEDED:
                if (code != ICMP_EXC_TTL)
-                       return;
+                       return 0;
                break;
        }
 
+       err = -ENOENT;
+
        read_lock(&ipip6_lock);
        t = ipip6_tunnel_lookup(iph->daddr, iph->saddr);
        if (t == NULL || t->parms.iph.daddr == 0)
                goto out;
+
+       err = 0;
        if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
                goto out;
 
@@ -271,13 +405,13 @@ static void ipip6_err(struct sk_buff *skb, u32 info)
        t->err_time = jiffies;
 out:
        read_unlock(&ipip6_lock);
-       return;
+       return err;
 #else
        struct iphdr *iph = (struct iphdr*)dp;
        int hlen = iph->ihl<<2;
        struct ipv6hdr *iph6;
-       int type = skb->h.icmph->type;
-       int code = skb->h.icmph->code;
+       const int type = icmp_hdr(skb)->type;
+       const int code = icmp_hdr(skb)->code;
        int rel_type = 0;
        int rel_code = 0;
        int rel_info = 0;
@@ -292,14 +426,14 @@ out:
        default:
                return;
        case ICMP_PARAMETERPROB:
-               if (skb->h.icmph->un.gateway < hlen)
+               if (icmp_hdr(skb)->un.gateway < hlen)
                        return;
 
                /* So... This guy found something strange INSIDE encapsulated
                   packet. Well, he is fool, but what can we do ?
                 */
                rel_type = ICMPV6_PARAMPROB;
-               rel_info = skb->h.icmph->un.gateway - hlen;
+               rel_info = icmp_hdr(skb)->un.gateway - hlen;
                break;
 
        case ICMP_DEST_UNREACH:
@@ -332,18 +466,18 @@ out:
        /* Prepare fake skb to feed it to icmpv6_send */
        skb2 = skb_clone(skb, GFP_ATOMIC);
        if (skb2 == NULL)
-               return;
+               return 0;
        dst_release(skb2->dst);
        skb2->dst = NULL;
        skb_pull(skb2, skb->data - (u8*)iph6);
-       skb2->nh.raw = skb2->data;
+       skb_reset_network_header(skb2);
 
        /* Try to guess incoming interface */
-       rt6i = rt6_lookup(&iph6->saddr, NULL, NULL, 0);
+       rt6i = rt6_lookup(&init_net, &iph6->saddr, NULL, NULL, 0);
        if (rt6i && rt6i->rt6i_dev) {
                skb2->dev = rt6i->rt6i_dev;
 
-               rt6i = rt6_lookup(&iph6->daddr, &iph6->saddr, NULL, 0);
+               rt6i = rt6_lookup(&init_net, &iph6->daddr, &iph6->saddr, NULL, 0);
 
                if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
                        struct ip_tunnel *t = netdev_priv(rt6i->rt6i_dev);
@@ -355,14 +489,14 @@ out:
                }
        }
        kfree_skb(skb2);
-       return;
+       return 0;
 #endif
 }
 
 static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
 {
        if (INET_ECN_is_ce(iph->tos))
-               IP6_ECN_set_ce(skb->nh.ipv6h);
+               IP6_ECN_set_ce(ipv6_hdr(skb));
 }
 
 static int ipip6_rcv(struct sk_buff *skb)
@@ -373,16 +507,24 @@ static int ipip6_rcv(struct sk_buff *skb)
        if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
                goto out;
 
-       iph = skb->nh.iph;
+       iph = ip_hdr(skb);
 
        read_lock(&ipip6_lock);
        if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
                secpath_reset(skb);
-               skb->mac.raw = skb->nh.raw;
-               skb->nh.raw = skb->data;
+               skb->mac_header = skb->network_header;
+               skb_reset_network_header(skb);
                IPCB(skb)->flags = 0;
                skb->protocol = htons(ETH_P_IPV6);
                skb->pkt_type = PACKET_HOST;
+
+               if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
+                   !isatap_chksrc(skb, iph, tunnel)) {
+                       tunnel->stat.rx_errors++;
+                       read_unlock(&ipip6_lock);
+                       kfree_skb(skb);
+                       return 0;
+               }
                tunnel->stat.rx_packets++;
                tunnel->stat.rx_bytes += skb->len;
                skb->dev = tunnel->dev;
@@ -410,7 +552,7 @@ static inline __be32 try_6to4(struct in6_addr *v6dst)
        __be32 dst = 0;
 
        if (v6dst->s6_addr16[0] == htons(0x2002)) {
-               /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
+               /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
                memcpy(&dst, &v6dst->s6_addr16[1], 4);
        }
        return dst;
@@ -426,15 +568,15 @@ static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
        struct ip_tunnel *tunnel = netdev_priv(dev);
        struct net_device_stats *stats = &tunnel->stat;
        struct iphdr  *tiph = &tunnel->parms.iph;
-       struct ipv6hdr *iph6 = skb->nh.ipv6h;
+       struct ipv6hdr *iph6 = ipv6_hdr(skb);
        u8     tos = tunnel->parms.iph.tos;
        struct rtable *rt;                      /* Route to the other host */
        struct net_device *tdev;                        /* Device to other host */
        struct iphdr  *iph;                     /* Our new IP header */
-       int    max_headroom;                    /* The extra header space needed */
+       unsigned int max_headroom;              /* The extra header space needed */
        __be32 dst = tiph->daddr;
        int    mtu;
-       struct in6_addr *addr6; 
+       struct in6_addr *addr6;
        int addr_type;
 
        if (tunnel->recursion++) {
@@ -445,6 +587,29 @@ static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
        if (skb->protocol != htons(ETH_P_IPV6))
                goto tx_error;
 
+       /* ISATAP (RFC4214) - must come before 6to4 */
+       if (dev->priv_flags & IFF_ISATAP) {
+               struct neighbour *neigh = NULL;
+
+               if (skb->dst)
+                       neigh = skb->dst->neighbour;
+
+               if (neigh == NULL) {
+                       if (net_ratelimit())
+                               printk(KERN_DEBUG "sit: nexthop == NULL\n");
+                       goto tx_error;
+               }
+
+               addr6 = (struct in6_addr*)&neigh->primary_key;
+               addr_type = ipv6_addr_type(addr6);
+
+               if ((addr_type & IPV6_ADDR_UNICAST) &&
+                    ipv6_addr_is_isatap(addr6))
+                       dst = addr6->s6_addr32[3];
+               else
+                       goto tx_error;
+       }
+
        if (!dst)
                dst = try_6to4(&iph6->daddr);
 
@@ -464,7 +629,7 @@ static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
                addr_type = ipv6_addr_type(addr6);
 
                if (addr_type == IPV6_ADDR_ANY) {
-                       addr6 = &skb->nh.ipv6h->daddr;
+                       addr6 = &ipv6_hdr(skb)->daddr;
                        addr_type = ipv6_addr_type(addr6);
                }
 
@@ -481,7 +646,7 @@ static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
                                                .tos = RT_TOS(tos) } },
                                    .oif = tunnel->parms.link,
                                    .proto = IPPROTO_IPV6 };
-               if (ip_route_output_key(&rt, &fl)) {
+               if (ip_route_output_key(&init_net, &rt, &fl)) {
                        tunnel->stat.tx_carrier_errors++;
                        goto tx_error_icmp;
                }
@@ -533,11 +698,12 @@ static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
         */
        max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
 
-       if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
+       if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
+           (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
                struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
                if (!new_skb) {
                        ip_rt_put(rt);
-                       stats->tx_dropped++;
+                       stats->tx_dropped++;
                        dev_kfree_skb(skb);
                        tunnel->recursion--;
                        return 0;
@@ -546,11 +712,12 @@ static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
                        skb_set_owner_w(new_skb, skb->sk);
                dev_kfree_skb(skb);
                skb = new_skb;
-               iph6 = skb->nh.ipv6h;
+               iph6 = ipv6_hdr(skb);
        }
 
-       skb->h.raw = skb->nh.raw;
-       skb->nh.raw = skb_push(skb, sizeof(struct iphdr));
+       skb->transport_header = skb->network_header;
+       skb_push(skb, sizeof(struct iphdr));
+       skb_reset_network_header(skb);
        memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
        IPCB(skb)->flags = 0;
        dst_release(skb->dst);
@@ -560,7 +727,7 @@ static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
         *      Push down and install the IPIP header.
         */
 
-       iph                     =       skb->nh.iph;
+       iph                     =       ip_hdr(skb);
        iph->version            =       4;
        iph->ihl                =       sizeof(struct iphdr)>>2;
        if (mtu > IPV6_MIN_MTU)
@@ -591,11 +758,48 @@ tx_error:
        return 0;
 }
 
+static void ipip6_tunnel_bind_dev(struct net_device *dev)
+{
+       struct net_device *tdev = NULL;
+       struct ip_tunnel *tunnel;
+       struct iphdr *iph;
+
+       tunnel = netdev_priv(dev);
+       iph = &tunnel->parms.iph;
+
+       if (iph->daddr) {
+               struct flowi fl = { .nl_u = { .ip4_u =
+                                             { .daddr = iph->daddr,
+                                               .saddr = iph->saddr,
+                                               .tos = RT_TOS(iph->tos) } },
+                                   .oif = tunnel->parms.link,
+                                   .proto = IPPROTO_IPV6 };
+               struct rtable *rt;
+               if (!ip_route_output_key(&init_net, &rt, &fl)) {
+                       tdev = rt->u.dst.dev;
+                       ip_rt_put(rt);
+               }
+               dev->flags |= IFF_POINTOPOINT;
+       }
+
+       if (!tdev && tunnel->parms.link)
+               tdev = __dev_get_by_index(&init_net, tunnel->parms.link);
+
+       if (tdev) {
+               dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
+               dev->mtu = tdev->mtu - sizeof(struct iphdr);
+               if (dev->mtu < IPV6_MIN_MTU)
+                       dev->mtu = IPV6_MIN_MTU;
+       }
+       dev->iflink = tunnel->parms.link;
+}
+
 static int
 ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
 {
        int err = 0;
        struct ip_tunnel_parm p;
+       struct ip_tunnel_prl prl;
        struct ip_tunnel *t;
 
        switch (cmd) {
@@ -662,6 +866,11 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
                        if (cmd == SIOCCHGTUNNEL) {
                                t->parms.iph.ttl = p.iph.ttl;
                                t->parms.iph.tos = p.iph.tos;
+                               if (t->parms.link != p.link) {
+                                       t->parms.link = p.link;
+                                       ipip6_tunnel_bind_dev(dev);
+                                       netdev_state_change(dev);
+                               }
                        }
                        if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
                                err = -EFAULT;
@@ -686,7 +895,31 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
                                goto done;
                        dev = t->dev;
                }
-               err = unregister_netdevice(dev);
+               unregister_netdevice(dev);
+               err = 0;
+               break;
+
+       case SIOCADDPRL:
+       case SIOCDELPRL:
+       case SIOCCHGPRL:
+               err = -EPERM;
+               if (!capable(CAP_NET_ADMIN))
+                       goto done;
+               err = -EINVAL;
+               if (dev == ipip6_fb_tunnel_dev)
+                       goto done;
+               err = -EFAULT;
+               if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
+                       goto done;
+               err = -ENOENT;
+               if (!(t = netdev_priv(dev)))
+                       goto done;
+
+               if (cmd == SIOCDELPRL)
+                       err = ipip6_tunnel_del_prl(t, &prl);
+               else
+                       err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
+               netdev_state_change(dev);
                break;
 
        default:
@@ -712,7 +945,6 @@ static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
 
 static void ipip6_tunnel_setup(struct net_device *dev)
 {
-       SET_MODULE_OWNER(dev);
        dev->uninit             = ipip6_tunnel_uninit;
        dev->destructor         = free_netdev;
        dev->hard_start_xmit    = ipip6_tunnel_xmit;
@@ -730,12 +962,9 @@ static void ipip6_tunnel_setup(struct net_device *dev)
 
 static int ipip6_tunnel_init(struct net_device *dev)
 {
-       struct net_device *tdev = NULL;
        struct ip_tunnel *tunnel;
-       struct iphdr *iph;
 
        tunnel = netdev_priv(dev);
-       iph = &tunnel->parms.iph;
 
        tunnel->dev = dev;
        strcpy(tunnel->parms.name, dev->name);
@@ -743,31 +972,7 @@ static int ipip6_tunnel_init(struct net_device *dev)
        memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
        memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
 
-       if (iph->daddr) {
-               struct flowi fl = { .nl_u = { .ip4_u =
-                                             { .daddr = iph->daddr,
-                                               .saddr = iph->saddr,
-                                               .tos = RT_TOS(iph->tos) } },
-                                   .oif = tunnel->parms.link,
-                                   .proto = IPPROTO_IPV6 };
-               struct rtable *rt;
-               if (!ip_route_output_key(&rt, &fl)) {
-                       tdev = rt->u.dst.dev;
-                       ip_rt_put(rt);
-               }
-               dev->flags |= IFF_POINTOPOINT;
-       }
-
-       if (!tdev && tunnel->parms.link)
-               tdev = __dev_get_by_index(tunnel->parms.link);
-
-       if (tdev) {
-               dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
-               dev->mtu = tdev->mtu - sizeof(struct iphdr);
-               if (dev->mtu < IPV6_MIN_MTU)
-                       dev->mtu = IPV6_MIN_MTU;
-       }
-       dev->iflink = tunnel->parms.link;
+       ipip6_tunnel_bind_dev(dev);
 
        return 0;
 }
@@ -790,9 +995,10 @@ static int __init ipip6_fb_tunnel_init(struct net_device *dev)
        return 0;
 }
 
-static struct net_protocol sit_protocol = {
+static struct xfrm_tunnel sit_handler = {
        .handler        =       ipip6_rcv,
        .err_handler    =       ipip6_err,
+       .priority       =       1,
 };
 
 static void __exit sit_destroy_tunnels(void)
@@ -809,9 +1015,9 @@ static void __exit sit_destroy_tunnels(void)
        }
 }
 
-void __exit sit_cleanup(void)
+static void __exit sit_cleanup(void)
 {
-       inet_del_protocol(&sit_protocol, IPPROTO_IPV6);
+       xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
 
        rtnl_lock();
        sit_destroy_tunnels();
@@ -819,18 +1025,18 @@ void __exit sit_cleanup(void)
        rtnl_unlock();
 }
 
-int __init sit_init(void)
+static int __init sit_init(void)
 {
        int err;
 
        printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
 
-       if (inet_add_protocol(&sit_protocol, IPPROTO_IPV6) < 0) {
+       if (xfrm4_tunnel_register(&sit_handler, AF_INET6) < 0) {
                printk(KERN_INFO "sit init: Can't add protocol\n");
                return -EAGAIN;
        }
 
-       ipip6_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0", 
+       ipip6_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
                                           ipip6_tunnel_setup);
        if (!ipip6_fb_tunnel_dev) {
                err = -ENOMEM;
@@ -847,7 +1053,7 @@ int __init sit_init(void)
  err2:
        free_netdev(ipip6_fb_tunnel_dev);
  err1:
-       inet_del_protocol(&sit_protocol, IPPROTO_IPV6);
+       xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
        goto out;
 }