Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

sit.c

Go to the documentation of this file.
00001 /*
00002  *      IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
00003  *      Linux INET6 implementation
00004  *
00005  *      Authors:
00006  *      Pedro Roque             <roque@di.fc.ul.pt>     
00007  *      Alexey Kuznetsov        <kuznet@ms2.inr.ac.ru>
00008  *
00009  *      $Id: sit.c,v 1.31 1999/03/25 10:04:55 davem Exp $
00010  *
00011  *      This program is free software; you can redistribute it and/or
00012  *      modify it under the terms of the GNU General Public License
00013  *      as published by the Free Software Foundation; either version
00014  *      2 of the License, or (at your option) any later version.
00015  */
00016 
00017 #define __NO_VERSION__
00018 #include <linux/module.h>
00019 #include <linux/errno.h>
00020 #include <linux/types.h>
00021 #include <linux/socket.h>
00022 #include <linux/sockios.h>
00023 #include <linux/sched.h>
00024 #include <linux/net.h>
00025 #include <linux/in6.h>
00026 #include <linux/netdevice.h>
00027 #include <linux/if_arp.h>
00028 #include <linux/icmp.h>
00029 #include <asm/uaccess.h>
00030 #include <linux/init.h>
00031 
00032 #include <net/sock.h>
00033 #include <net/snmp.h>
00034 
00035 #include <net/ipv6.h>
00036 #include <net/protocol.h>
00037 #include <net/transp_v6.h>
00038 #include <net/ip6_fib.h>
00039 #include <net/ip6_route.h>
00040 #include <net/ndisc.h>
00041 #include <net/addrconf.h>
00042 #include <net/ip.h>
00043 #include <net/udp.h>
00044 #include <net/icmp.h>
00045 #include <net/ipip.h>
00046 
00047 /*
00048    This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
00049 
00050    For comments look at net/ipv4/ip_gre.c --ANK
00051  */
00052 
00053 #define HASH_SIZE  16
00054 #define HASH(addr) ((addr^(addr>>4))&0xF)
00055 
00056 static int ipip6_fb_tunnel_init(struct device *dev);
00057 static int ipip6_tunnel_init(struct device *dev);
00058 
00059 static struct device ipip6_fb_tunnel_dev = {
00060         NULL, 0x0, 0x0, 0x0, 0x0, 0, 0, 0, 0, 0, NULL, ipip6_fb_tunnel_init,
00061 };
00062 
00063 static struct ip_tunnel ipip6_fb_tunnel = {
00064         NULL, &ipip6_fb_tunnel_dev, {0, }, 0, 0, 0, 0, 0, 0, 0, {"sit0", }
00065 };
00066 
00067 static struct ip_tunnel *tunnels_r_l[HASH_SIZE];
00068 static struct ip_tunnel *tunnels_r[HASH_SIZE];
00069 static struct ip_tunnel *tunnels_l[HASH_SIZE];
00070 static struct ip_tunnel *tunnels_wc[1];
00071 static struct ip_tunnel **tunnels[4] = { tunnels_wc, tunnels_l, tunnels_r, tunnels_r_l };
00072 
00073 static struct ip_tunnel * ipip6_tunnel_lookup(u32 remote, u32 local)
00074 {
00075         unsigned h0 = HASH(remote);
00076         unsigned h1 = HASH(local);
00077         struct ip_tunnel *t;
00078 
00079         for (t = tunnels_r_l[h0^h1]; t; t = t->next) {
00080                 if (local == t->parms.iph.saddr &&
00081                     remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
00082                         return t;
00083         }
00084         for (t = tunnels_r[h0]; t; t = t->next) {
00085                 if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
00086                         return t;
00087         }
00088         for (t = tunnels_l[h1]; t; t = t->next) {
00089                 if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP))
00090                         return t;
00091         }
00092         if ((t = tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP))
00093                 return t;
00094         return NULL;
00095 }
00096 
00097 static struct ip_tunnel ** ipip6_bucket(struct ip_tunnel *t)
00098 {
00099         u32 remote = t->parms.iph.daddr;
00100         u32 local = t->parms.iph.saddr;
00101         unsigned h = 0;
00102         int prio = 0;
00103 
00104         if (remote) {
00105                 prio |= 2;
00106                 h ^= HASH(remote);
00107         }
00108         if (local) {
00109                 prio |= 1;
00110                 h ^= HASH(local);
00111         }
00112         return &tunnels[prio][h];
00113 }
00114 
00115 static void ipip6_tunnel_unlink(struct ip_tunnel *t)
00116 {
00117         struct ip_tunnel **tp;
00118 
00119         for (tp = ipip6_bucket(t); *tp; tp = &(*tp)->next) {
00120                 if (t == *tp) {
00121                         *tp = t->next;
00122                         synchronize_bh();
00123                         break;
00124                 }
00125         }
00126 }
00127 
00128 static void ipip6_tunnel_link(struct ip_tunnel *t)
00129 {
00130         struct ip_tunnel **tp = ipip6_bucket(t);
00131 
00132         t->next = *tp;
00133         wmb();
00134         *tp = t;
00135 }
00136 
00137 struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int create)
00138 {
00139         u32 remote = parms->iph.daddr;
00140         u32 local = parms->iph.saddr;
00141         struct ip_tunnel *t, **tp, *nt;
00142         struct device *dev;
00143         unsigned h = 0;
00144         int prio = 0;
00145 
00146         if (remote) {
00147                 prio |= 2;
00148                 h ^= HASH(remote);
00149         }
00150         if (local) {
00151                 prio |= 1;
00152                 h ^= HASH(local);
00153         }
00154         for (tp = &tunnels[prio][h]; (t = *tp) != NULL; tp = &t->next) {
00155                 if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
00156                         return t;
00157         }
00158         if (!create)
00159                 return NULL;
00160 
00161         MOD_INC_USE_COUNT;
00162         dev = kmalloc(sizeof(*dev) + sizeof(*t), GFP_KERNEL);
00163         if (dev == NULL) {
00164                 MOD_DEC_USE_COUNT;
00165                 return NULL;
00166         }
00167         memset(dev, 0, sizeof(*dev) + sizeof(*t));
00168         dev->priv = (void*)(dev+1);
00169         nt = (struct ip_tunnel*)dev->priv;
00170         nt->dev = dev;
00171         dev->name = nt->parms.name;
00172         dev->init = ipip6_tunnel_init;
00173         memcpy(&nt->parms, parms, sizeof(*parms));
00174         if (dev->name[0] == 0) {
00175                 int i;
00176                 for (i=1; i<100; i++) {
00177                         sprintf(dev->name, "sit%d", i);
00178                         if (dev_get(dev->name) == NULL)
00179                                 break;
00180                 }
00181                 if (i==100)
00182                         goto failed;
00183                 memcpy(parms->name, dev->name, IFNAMSIZ);
00184         }
00185         if (register_netdevice(dev) < 0)
00186                 goto failed;
00187 
00188         ipip6_tunnel_link(nt);
00189         /* Do not decrement MOD_USE_COUNT here. */
00190         return nt;
00191 
00192 failed:
00193         kfree(dev);
00194         MOD_DEC_USE_COUNT;
00195         return NULL;
00196 }
00197 
00198 static void ipip6_tunnel_destroy(struct device *dev)
00199 {
00200         if (dev == &ipip6_fb_tunnel_dev) {
00201                 tunnels_wc[0] = NULL;
00202                 synchronize_bh();
00203                 return;
00204         } else {
00205                 ipip6_tunnel_unlink((struct ip_tunnel*)dev->priv);
00206                 kfree(dev);
00207                 MOD_DEC_USE_COUNT;
00208         }
00209 }
00210 
00211 void ipip6_err(struct sk_buff *skb, unsigned char *dp, int len)
00212 {
00213 #ifndef I_WISH_WORLD_WERE_PERFECT
00214 
00215 /* It is not :-( All the routers (except for Linux) return only
00216    8 bytes of packet payload. It means, that precise relaying of
00217    ICMP in the real Internet is absolutely infeasible.
00218  */
00219         struct iphdr *iph = (struct iphdr*)dp;
00220         int type = skb->h.icmph->type;
00221         int code = skb->h.icmph->code;
00222         struct ip_tunnel *t;
00223 
00224         if (len < sizeof(struct iphdr))
00225                 return;
00226 
00227         switch (type) {
00228         default:
00229         case ICMP_PARAMETERPROB:
00230                 return;
00231 
00232         case ICMP_DEST_UNREACH:
00233                 switch (code) {
00234                 case ICMP_SR_FAILED:
00235                 case ICMP_PORT_UNREACH:
00236                         /* Impossible event. */
00237                         return;
00238                 case ICMP_FRAG_NEEDED:
00239                         /* Soft state for pmtu is maintained by IP core. */
00240                         return;
00241                 default:
00242                         /* All others are translated to HOST_UNREACH.
00243                            rfc2003 contains "deep thoughts" about NET_UNREACH,
00244                            I believe they are just ether pollution. --ANK
00245                          */
00246                         break;
00247                 }
00248                 break;
00249         case ICMP_TIME_EXCEEDED:
00250                 if (code != ICMP_EXC_TTL)
00251                         return;
00252                 break;
00253         }
00254 
00255         t = ipip6_tunnel_lookup(iph->daddr, iph->saddr);
00256         if (t == NULL || t->parms.iph.daddr == 0)
00257                 return;
00258         if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
00259                 return;
00260 
00261         if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
00262                 t->err_count++;
00263         else
00264                 t->err_count = 1;
00265         t->err_time = jiffies;
00266         return;
00267 #else
00268         struct iphdr *iph = (struct iphdr*)dp;
00269         int hlen = iph->ihl<<2;
00270         struct ipv6hdr *iph6;
00271         int type = skb->h.icmph->type;
00272         int code = skb->h.icmph->code;
00273         int rel_type = 0;
00274         int rel_code = 0;
00275         int rel_info = 0;
00276         struct sk_buff *skb2;
00277         struct rt6_info *rt6i;
00278 
00279         if (len < hlen + sizeof(struct ipv6hdr))
00280                 return;
00281         iph6 = (struct ipv6hdr*)(dp + hlen);
00282 
00283         switch (type) {
00284         default:
00285                 return;
00286         case ICMP_PARAMETERPROB:
00287                 if (skb->h.icmph->un.gateway < hlen)
00288                         return;
00289 
00290                 /* So... This guy found something strange INSIDE encapsulated
00291                    packet. Well, he is fool, but what can we do ?
00292                  */
00293                 rel_type = ICMPV6_PARAMPROB;
00294                 rel_info = skb->h.icmph->un.gateway - hlen;
00295                 break;
00296 
00297         case ICMP_DEST_UNREACH:
00298                 switch (code) {
00299                 case ICMP_SR_FAILED:
00300                 case ICMP_PORT_UNREACH:
00301                         /* Impossible event. */
00302                         return;
00303                 case ICMP_FRAG_NEEDED:
00304                         /* Too complicated case ... */
00305                         return;
00306                 default:
00307                         /* All others are translated to HOST_UNREACH.
00308                            rfc2003 contains "deep thoughts" about NET_UNREACH,
00309                            I believe, it is just ether pollution. --ANK
00310                          */
00311                         rel_type = ICMPV6_DEST_UNREACH;
00312                         rel_code = ICMPV6_ADDR_UNREACH;
00313                         break;
00314                 }
00315                 break;
00316         case ICMP_TIME_EXCEEDED:
00317                 if (code != ICMP_EXC_TTL)
00318                         return;
00319                 rel_type = ICMPV6_TIME_EXCEED;
00320                 rel_code = ICMPV6_EXC_HOPLIMIT;
00321                 break;
00322         }
00323 
00324         /* Prepare fake skb to feed it to icmpv6_send */
00325         skb2 = skb_clone(skb, GFP_ATOMIC);
00326         if (skb2 == NULL)
00327                 return;
00328         dst_release(skb2->dst);
00329         skb2->dst = NULL;
00330         skb_pull(skb2, skb->data - (u8*)iph6);
00331         skb2->nh.raw = skb2->data;
00332 
00333         /* Try to guess incoming interface */
00334         rt6i = rt6_lookup(&iph6->saddr, NULL, NULL, 0);
00335         if (rt6i && rt6i->rt6i_dev) {
00336                 skb2->dev = rt6i->rt6i_dev;
00337 
00338                 rt6i = rt6_lookup(&iph6->daddr, &iph6->saddr, NULL, 0);
00339 
00340                 if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
00341                         struct ip_tunnel * t = (struct ip_tunnel*)rt6i->rt6i_dev->priv;
00342                         if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
00343                                 rel_type = ICMPV6_DEST_UNREACH;
00344                                 rel_code = ICMPV6_ADDR_UNREACH;
00345                         }
00346                         icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
00347                 }
00348         }
00349         kfree_skb(skb2);
00350         return;
00351 #endif
00352 }
00353 
00354 int ipip6_rcv(struct sk_buff *skb, unsigned short len)
00355 {
00356         struct iphdr *iph;
00357         struct ip_tunnel *tunnel;
00358 
00359         iph = skb->nh.iph;
00360 
00361         if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
00362                 skb->mac.raw = skb->nh.raw;
00363                 skb->nh.raw = skb_pull(skb, skb->h.raw - skb->data);
00364                 memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
00365                 skb->protocol = __constant_htons(ETH_P_IPV6);
00366                 skb->ip_summed = 0;
00367                 skb->pkt_type = PACKET_HOST;
00368                 tunnel->stat.rx_packets++;
00369                 tunnel->stat.rx_bytes += skb->len;
00370                 skb->dev = tunnel->dev;
00371                 dst_release(skb->dst);
00372                 skb->dst = NULL;
00373                 netif_rx(skb);
00374                 return 0;
00375         }
00376 
00377         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, 0);
00378         kfree_skb(skb);
00379         return 0;
00380 }
00381 
00382 /*
00383  *      This function assumes it is being called from dev_queue_xmit()
00384  *      and that skb is filled properly by that function.
00385  */
00386 
00387 static int ipip6_tunnel_xmit(struct sk_buff *skb, struct device *dev)
00388 {
00389         struct ip_tunnel *tunnel = (struct ip_tunnel*)dev->priv;
00390         struct net_device_stats *stats = &tunnel->stat;
00391         struct iphdr  *tiph = &tunnel->parms.iph;
00392         struct ipv6hdr *iph6 = skb->nh.ipv6h;
00393         u8     tos = tunnel->parms.iph.tos;
00394         struct rtable *rt;                      /* Route to the other host */
00395         struct device *tdev;                    /* Device to other host */
00396         struct iphdr  *iph;                     /* Our new IP header */
00397         int    max_headroom;                    /* The extra header space needed */
00398         u32    dst = tiph->daddr;
00399         int    mtu;
00400         struct in6_addr *addr6; 
00401         int addr_type;
00402 
00403         if (tunnel->recursion++) {
00404                 tunnel->stat.collisions++;
00405                 goto tx_error;
00406         }
00407 
00408         if (skb->protocol != __constant_htons(ETH_P_IPV6))
00409                 goto tx_error;
00410 
00411         if (!dst) {
00412                 struct neighbour *neigh = NULL;
00413 
00414                 if (skb->dst)
00415                         neigh = skb->dst->neighbour;
00416 
00417                 if (neigh == NULL) {
00418                         printk(KERN_DEBUG "sit: nexthop == NULL\n");
00419                         goto tx_error;
00420                 }
00421 
00422                 addr6 = (struct in6_addr*)&neigh->primary_key;
00423                 addr_type = ipv6_addr_type(addr6);
00424 
00425                 if (addr_type == IPV6_ADDR_ANY) {
00426                         addr6 = &skb->nh.ipv6h->daddr;
00427                         addr_type = ipv6_addr_type(addr6);
00428                 }
00429 
00430                 if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
00431                         goto tx_error_icmp;
00432 
00433                 dst = addr6->s6_addr32[3];
00434         }
00435 
00436         if (ip_route_output(&rt, dst, tiph->saddr, RT_TOS(tos), tunnel->parms.link)) {
00437                 tunnel->stat.tx_carrier_errors++;
00438                 goto tx_error_icmp;
00439         }
00440         tdev = rt->u.dst.dev;
00441 
00442         if (tdev == dev) {
00443                 ip_rt_put(rt);
00444                 tunnel->stat.collisions++;
00445                 goto tx_error;
00446         }
00447 
00448         mtu = rt->u.dst.pmtu - sizeof(struct iphdr);
00449         if (mtu < 68) {
00450                 tunnel->stat.collisions++;
00451                 ip_rt_put(rt);
00452                 goto tx_error;
00453         }
00454         if (mtu < IPV6_MIN_MTU)
00455                 mtu = IPV6_MIN_MTU;
00456         if (skb->dst && mtu < skb->dst->pmtu) {
00457                 struct rt6_info *rt6 = (struct rt6_info*)skb->dst;
00458                 if (mtu < rt6->u.dst.pmtu) {
00459                         if (tunnel->parms.iph.daddr || rt6->rt6i_dst.plen == 128) {
00460                                 rt6->rt6i_flags |= RTF_MODIFIED;
00461                                 rt6->u.dst.pmtu = mtu;
00462                         }
00463                 }
00464         }
00465         if (skb->len > mtu) {
00466                 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
00467                 ip_rt_put(rt);
00468                 goto tx_error;
00469         }
00470 
00471         if (tunnel->err_count > 0) {
00472                 if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
00473                         tunnel->err_count--;
00474                         dst_link_failure(skb);
00475                 } else
00476                         tunnel->err_count = 0;
00477         }
00478 
00479         skb->h.raw = skb->nh.raw;
00480 
00481         /*
00482          * Okay, now see if we can stuff it in the buffer as-is.
00483          */
00484         max_headroom = (((tdev->hard_header_len+15)&~15)+sizeof(struct iphdr));
00485 
00486         if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
00487                 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
00488                 if (!new_skb) {
00489                         ip_rt_put(rt);
00490                         stats->tx_dropped++;
00491                         dev_kfree_skb(skb);
00492                         tunnel->recursion--;
00493                         return 0;
00494                 }
00495                 if (skb->sk)
00496                         skb_set_owner_w(new_skb, skb->sk);
00497                 dev_kfree_skb(skb);
00498                 skb = new_skb;
00499         }
00500 
00501         skb->nh.raw = skb_push(skb, sizeof(struct iphdr));
00502         memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
00503         dst_release(skb->dst);
00504         skb->dst = &rt->u.dst;
00505 
00506         /*
00507          *      Push down and install the IPIP header.
00508          */
00509 
00510         iph                     =       skb->nh.iph;
00511         iph->version            =       4;
00512         iph->ihl                =       sizeof(struct iphdr)>>2;
00513         if (mtu > IPV6_MIN_MTU)
00514                 iph->frag_off   =       __constant_htons(IP_DF);
00515         else
00516                 iph->frag_off   =       0;
00517 
00518         iph->protocol           =       IPPROTO_IPV6;
00519         iph->tos                =       tos;
00520         iph->daddr              =       rt->rt_dst;
00521         iph->saddr              =       rt->rt_src;
00522 
00523         if ((iph->ttl = tiph->ttl) == 0)
00524                 iph->ttl        =       iph6->hop_limit;
00525 
00526         iph->tot_len            =       htons(skb->len);
00527         iph->id                 =       htons(ip_id_count++);
00528         ip_send_check(iph);
00529 
00530         stats->tx_bytes += skb->len;
00531         stats->tx_packets++;
00532         ip_send(skb);
00533 
00534         tunnel->recursion--;
00535         return 0;
00536 
00537 tx_error_icmp:
00538         dst_link_failure(skb);
00539 tx_error:
00540         stats->tx_errors++;
00541         dev_kfree_skb(skb);
00542         tunnel->recursion--;
00543         return 0;
00544 }
00545 
00546 static int
00547 ipip6_tunnel_ioctl (struct device *dev, struct ifreq *ifr, int cmd)
00548 {
00549         int err = 0;
00550         struct ip_tunnel_parm p;
00551         struct ip_tunnel *t;
00552 
00553         MOD_INC_USE_COUNT;
00554 
00555         switch (cmd) {
00556         case SIOCGETTUNNEL:
00557                 t = NULL;
00558                 if (dev == &ipip6_fb_tunnel_dev) {
00559                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
00560                                 err = -EFAULT;
00561                                 break;
00562                         }
00563                         t = ipip6_tunnel_locate(&p, 0);
00564                 }
00565                 if (t == NULL)
00566                         t = (struct ip_tunnel*)dev->priv;
00567                 memcpy(&p, &t->parms, sizeof(p));
00568                 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
00569                         err = -EFAULT;
00570                 break;
00571 
00572         case SIOCADDTUNNEL:
00573         case SIOCCHGTUNNEL:
00574                 err = -EPERM;
00575                 if (!capable(CAP_NET_ADMIN))
00576                         goto done;
00577 
00578                 err = -EFAULT;
00579                 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
00580                         goto done;
00581 
00582                 err = -EINVAL;
00583                 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
00584                     p.iph.ihl != 5 || (p.iph.frag_off&__constant_htons(~IP_DF)))
00585                         goto done;
00586                 if (p.iph.ttl)
00587                         p.iph.frag_off |= __constant_htons(IP_DF);
00588 
00589                 t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
00590 
00591                 if (dev != &ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL &&
00592                     t != &ipip6_fb_tunnel) {
00593                         if (t != NULL) {
00594                                 if (t->dev != dev) {
00595                                         err = -EEXIST;
00596                                         break;
00597                                 }
00598                         } else {
00599                                 if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
00600                                     (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
00601                                         err = -EINVAL;
00602                                         break;
00603                                 }
00604                                 t = (struct ip_tunnel*)dev->priv;
00605                                 start_bh_atomic();
00606                                 ipip6_tunnel_unlink(t);
00607                                 t->parms.iph.saddr = p.iph.saddr;
00608                                 t->parms.iph.daddr = p.iph.daddr;
00609                                 memcpy(dev->dev_addr, &p.iph.saddr, 4);
00610                                 memcpy(dev->broadcast, &p.iph.daddr, 4);
00611                                 ipip6_tunnel_link(t);
00612                                 end_bh_atomic();
00613                                 netdev_state_change(dev);
00614                         }
00615                 }
00616 
00617                 if (t) {
00618                         err = 0;
00619                         if (cmd == SIOCCHGTUNNEL) {
00620                                 t->parms.iph.ttl = p.iph.ttl;
00621                                 t->parms.iph.tos = p.iph.tos;
00622                         }
00623                         if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
00624                                 err = -EFAULT;
00625                 } else
00626                         err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
00627                 break;
00628 
00629         case SIOCDELTUNNEL:
00630                 err = -EPERM;
00631                 if (!capable(CAP_NET_ADMIN))
00632                         goto done;
00633 
00634                 if (dev == &ipip6_fb_tunnel_dev) {
00635                         err = -EFAULT;
00636                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
00637                                 goto done;
00638                         err = -ENOENT;
00639                         if ((t = ipip6_tunnel_locate(&p, 0)) == NULL)
00640                                 goto done;
00641                         err = -EPERM;
00642                         if (t == &ipip6_fb_tunnel)
00643                                 goto done;
00644                 }
00645                 err = unregister_netdevice(dev);
00646                 break;
00647 
00648         default:
00649                 err = -EINVAL;
00650         }
00651 
00652 done:
00653         MOD_DEC_USE_COUNT;
00654         return err;
00655 }
00656 
00657 static struct net_device_stats *ipip6_tunnel_get_stats(struct device *dev)
00658 {
00659         return &(((struct ip_tunnel*)dev->priv)->stat);
00660 }
00661 
00662 static int ipip6_tunnel_change_mtu(struct device *dev, int new_mtu)
00663 {
00664         if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
00665                 return -EINVAL;
00666         dev->mtu = new_mtu;
00667         return 0;
00668 }
00669 
00670 static void ipip6_tunnel_init_gen(struct device *dev)
00671 {
00672         struct ip_tunnel *t = (struct ip_tunnel*)dev->priv;
00673 
00674         dev->destructor         = ipip6_tunnel_destroy;
00675         dev->hard_start_xmit    = ipip6_tunnel_xmit;
00676         dev->get_stats          = ipip6_tunnel_get_stats;
00677         dev->do_ioctl           = ipip6_tunnel_ioctl;
00678         dev->change_mtu         = ipip6_tunnel_change_mtu;
00679 
00680         dev_init_buffers(dev);
00681 
00682         dev->type               = ARPHRD_SIT;
00683         dev->hard_header_len    = LL_MAX_HEADER + sizeof(struct iphdr);
00684         dev->mtu                = 1500 - sizeof(struct iphdr);
00685         dev->flags              = IFF_NOARP;
00686         dev->iflink             = 0;
00687         dev->addr_len           = 4;
00688         memcpy(dev->dev_addr, &t->parms.iph.saddr, 4);
00689         memcpy(dev->broadcast, &t->parms.iph.daddr, 4);
00690 }
00691 
00692 static int ipip6_tunnel_init(struct device *dev)
00693 {
00694         struct device *tdev = NULL;
00695         struct ip_tunnel *tunnel;
00696         struct iphdr *iph;
00697 
00698         tunnel = (struct ip_tunnel*)dev->priv;
00699         iph = &tunnel->parms.iph;
00700 
00701         ipip6_tunnel_init_gen(dev);
00702 
00703         if (iph->daddr) {
00704                 struct rtable *rt;
00705                 if (!ip_route_output(&rt, iph->daddr, iph->saddr, RT_TOS(iph->tos), tunnel->parms.link)) {
00706                         tdev = rt->u.dst.dev;
00707                         ip_rt_put(rt);
00708                 }
00709                 dev->flags |= IFF_POINTOPOINT;
00710         }
00711 
00712         if (!tdev && tunnel->parms.link)
00713                 tdev = dev_get_by_index(tunnel->parms.link);
00714 
00715         if (tdev) {
00716                 dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
00717                 dev->mtu = tdev->mtu - sizeof(struct iphdr);
00718                 if (dev->mtu < IPV6_MIN_MTU)
00719                         dev->mtu = IPV6_MIN_MTU;
00720         }
00721         dev->iflink = tunnel->parms.link;
00722 
00723         return 0;
00724 }
00725 
00726 #ifdef MODULE
00727 static int ipip6_fb_tunnel_open(struct device *dev)
00728 {
00729         MOD_INC_USE_COUNT;
00730         return 0;
00731 }
00732 
00733 static int ipip6_fb_tunnel_close(struct device *dev)
00734 {
00735         MOD_DEC_USE_COUNT;
00736         return 0;
00737 }
00738 #endif
00739 
00740 __initfunc(int ipip6_fb_tunnel_init(struct device *dev))
00741 {
00742         struct iphdr *iph;
00743 
00744         ipip6_tunnel_init_gen(dev);
00745 #ifdef MODULE
00746         dev->open               = ipip6_fb_tunnel_open;
00747         dev->stop               = ipip6_fb_tunnel_close;
00748 #endif
00749 
00750         iph = &ipip6_fb_tunnel.parms.iph;
00751         iph->version            = 4;
00752         iph->protocol           = IPPROTO_IPV6;
00753         iph->ihl                = 5;
00754         iph->ttl                = 64;
00755 
00756         tunnels_wc[0]           = &ipip6_fb_tunnel;
00757         return 0;
00758 }
00759 
00760 static struct inet_protocol sit_protocol = {
00761         ipip6_rcv,
00762         ipip6_err,
00763         0,
00764         IPPROTO_IPV6,
00765         0,
00766         NULL,
00767         "IPv6"
00768 };
00769 
00770 #ifdef MODULE
00771 void sit_cleanup(void)
00772 {
00773         inet_del_protocol(&sit_protocol);
00774         unregister_netdevice(&ipip6_fb_tunnel_dev);
00775 }
00776 #endif
00777 
00778 __initfunc(int sit_init(void))
00779 {
00780         printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
00781 
00782         ipip6_fb_tunnel_dev.priv = (void*)&ipip6_fb_tunnel;
00783         ipip6_fb_tunnel_dev.name = ipip6_fb_tunnel.parms.name;
00784 #ifdef MODULE
00785         register_netdev(&ipip6_fb_tunnel_dev);
00786 #else
00787         register_netdevice(&ipip6_fb_tunnel_dev);
00788 #endif
00789         inet_add_protocol(&sit_protocol);
00790         return 0;
00791 }