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

ipip.c

Go to the documentation of this file.
00001 /*
00002  *      Linux NET3:     IP/IP protocol decoder. 
00003  *
00004  *      Version: $Id: ipip.c,v 1.26 1999/03/25 10:04:32 davem Exp $
00005  *
00006  *      Authors:
00007  *              Sam Lantinga (slouken@cs.ucdavis.edu)  02/01/95
00008  *
00009  *      Fixes:
00010  *              Alan Cox        :       Merged and made usable non modular (its so tiny its silly as
00011  *                                      a module taking up 2 pages).
00012  *              Alan Cox        :       Fixed bug with 1.3.18 and IPIP not working (now needs to set skb->h.iph)
00013  *                                      to keep ip_forward happy.
00014  *              Alan Cox        :       More fixes for 1.3.21, and firewall fix. Maybe this will work soon 8).
00015  *              Kai Schulte     :       Fixed #defines for IP_FIREWALL->FIREWALL
00016  *              David Woodhouse :       Perform some basic ICMP handling.
00017  *                                      IPIP Routing without decapsulation.
00018  *              Carlos Picoto   :       GRE over IP support
00019  *              Alexey Kuznetsov:       Reworked. Really, now it is truncated version of ipv4/ip_gre.c.
00020  *                                      I do not want to merge them together.
00021  *
00022  *      This program is free software; you can redistribute it and/or
00023  *      modify it under the terms of the GNU General Public License
00024  *      as published by the Free Software Foundation; either version
00025  *      2 of the License, or (at your option) any later version.
00026  *
00027  */
00028 
00029 /* tunnel.c: an IP tunnel driver
00030 
00031         The purpose of this driver is to provide an IP tunnel through
00032         which you can tunnel network traffic transparently across subnets.
00033 
00034         This was written by looking at Nick Holloway's dummy driver
00035         Thanks for the great code!
00036 
00037                 -Sam Lantinga   (slouken@cs.ucdavis.edu)  02/01/95
00038                 
00039         Minor tweaks:
00040                 Cleaned up the code a little and added some pre-1.3.0 tweaks.
00041                 dev->hard_header/hard_header_len changed to use no headers.
00042                 Comments/bracketing tweaked.
00043                 Made the tunnels use dev->name not tunnel: when error reporting.
00044                 Added tx_dropped stat
00045                 
00046                 -Alan Cox       (Alan.Cox@linux.org) 21 March 95
00047 
00048         Reworked:
00049                 Changed to tunnel to destination gateway in addition to the
00050                         tunnel's pointopoint address
00051                 Almost completely rewritten
00052                 Note:  There is currently no firewall or ICMP handling done.
00053 
00054                 -Sam Lantinga   (slouken@cs.ucdavis.edu) 02/13/96
00055                 
00056 */
00057 
00058 /* Things I wish I had known when writing the tunnel driver:
00059 
00060         When the tunnel_xmit() function is called, the skb contains the
00061         packet to be sent (plus a great deal of extra info), and dev
00062         contains the tunnel device that _we_ are.
00063 
00064         When we are passed a packet, we are expected to fill in the
00065         source address with our source IP address.
00066 
00067         What is the proper way to allocate, copy and free a buffer?
00068         After you allocate it, it is a "0 length" chunk of memory
00069         starting at zero.  If you want to add headers to the buffer
00070         later, you'll have to call "skb_reserve(skb, amount)" with
00071         the amount of memory you want reserved.  Then, you call
00072         "skb_put(skb, amount)" with the amount of space you want in
00073         the buffer.  skb_put() returns a pointer to the top (#0) of
00074         that buffer.  skb->len is set to the amount of space you have
00075         "allocated" with skb_put().  You can then write up to skb->len
00076         bytes to that buffer.  If you need more, you can call skb_put()
00077         again with the additional amount of space you need.  You can
00078         find out how much more space you can allocate by calling 
00079         "skb_tailroom(skb)".
00080         Now, to add header space, call "skb_push(skb, header_len)".
00081         This creates space at the beginning of the buffer and returns
00082         a pointer to this new space.  If later you need to strip a
00083         header from a buffer, call "skb_pull(skb, header_len)".
00084         skb_headroom() will return how much space is left at the top
00085         of the buffer (before the main data).  Remember, this headroom
00086         space must be reserved before the skb_put() function is called.
00087         */
00088 
00089 /*
00090    This version of net/ipv4/ipip.c is cloned of net/ipv4/ip_gre.c
00091 
00092    For comments look at net/ipv4/ip_gre.c --ANK
00093  */
00094 
00095  
00096 #include <linux/module.h>
00097 #include <linux/types.h>
00098 #include <linux/sched.h>
00099 #include <linux/kernel.h>
00100 #include <asm/uaccess.h>
00101 #include <linux/skbuff.h>
00102 #include <linux/netdevice.h>
00103 #include <linux/in.h>
00104 #include <linux/tcp.h>
00105 #include <linux/udp.h>
00106 #include <linux/if_arp.h>
00107 #include <linux/mroute.h>
00108 #include <linux/init.h>
00109 
00110 #include <net/sock.h>
00111 #include <net/ip.h>
00112 #include <net/icmp.h>
00113 #include <net/protocol.h>
00114 #include <net/ipip.h>
00115 
00116 #define HASH_SIZE  16
00117 #define HASH(addr) ((addr^(addr>>4))&0xF)
00118 
00119 static int ipip_fb_tunnel_init(struct device *dev);
00120 static int ipip_tunnel_init(struct device *dev);
00121 
00122 static struct device ipip_fb_tunnel_dev = {
00123         NULL, 0x0, 0x0, 0x0, 0x0, 0, 0, 0, 0, 0, NULL, ipip_fb_tunnel_init,
00124 };
00125 
00126 static struct ip_tunnel ipip_fb_tunnel = {
00127         NULL, &ipip_fb_tunnel_dev, {0, }, 0, 0, 0, 0, 0, 0, 0, {"tunl0", }
00128 };
00129 
00130 static struct ip_tunnel *tunnels_r_l[HASH_SIZE];
00131 static struct ip_tunnel *tunnels_r[HASH_SIZE];
00132 static struct ip_tunnel *tunnels_l[HASH_SIZE];
00133 static struct ip_tunnel *tunnels_wc[1];
00134 static struct ip_tunnel **tunnels[4] = { tunnels_wc, tunnels_l, tunnels_r, tunnels_r_l };
00135 
00136 static struct ip_tunnel * ipip_tunnel_lookup(u32 remote, u32 local)
00137 {
00138         unsigned h0 = HASH(remote);
00139         unsigned h1 = HASH(local);
00140         struct ip_tunnel *t;
00141 
00142         for (t = tunnels_r_l[h0^h1]; t; t = t->next) {
00143                 if (local == t->parms.iph.saddr &&
00144                     remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
00145                         return t;
00146         }
00147         for (t = tunnels_r[h0]; t; t = t->next) {
00148                 if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
00149                         return t;
00150         }
00151         for (t = tunnels_l[h1]; t; t = t->next) {
00152                 if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP))
00153                         return t;
00154         }
00155         if ((t = tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP))
00156                 return t;
00157         return NULL;
00158 }
00159 
00160 static struct ip_tunnel **ipip_bucket(struct ip_tunnel *t)
00161 {
00162         u32 remote = t->parms.iph.daddr;
00163         u32 local = t->parms.iph.saddr;
00164         unsigned h = 0;
00165         int prio = 0;
00166 
00167         if (remote) {
00168                 prio |= 2;
00169                 h ^= HASH(remote);
00170         }
00171         if (local) {
00172                 prio |= 1;
00173                 h ^= HASH(local);
00174         }
00175         return &tunnels[prio][h];
00176 }
00177 
00178 
00179 static void ipip_tunnel_unlink(struct ip_tunnel *t)
00180 {
00181         struct ip_tunnel **tp;
00182 
00183         for (tp = ipip_bucket(t); *tp; tp = &(*tp)->next) {
00184                 if (t == *tp) {
00185                         *tp = t->next;
00186                         synchronize_bh();
00187                         break;
00188                 }
00189         }
00190 }
00191 
00192 static void ipip_tunnel_link(struct ip_tunnel *t)
00193 {
00194         struct ip_tunnel **tp = ipip_bucket(t);
00195 
00196         t->next = *tp;
00197         wmb();
00198         *tp = t;
00199 }
00200 
00201 struct ip_tunnel * ipip_tunnel_locate(struct ip_tunnel_parm *parms, int create)
00202 {
00203         u32 remote = parms->iph.daddr;
00204         u32 local = parms->iph.saddr;
00205         struct ip_tunnel *t, **tp, *nt;
00206         struct device *dev;
00207         unsigned h = 0;
00208         int prio = 0;
00209 
00210         if (remote) {
00211                 prio |= 2;
00212                 h ^= HASH(remote);
00213         }
00214         if (local) {
00215                 prio |= 1;
00216                 h ^= HASH(local);
00217         }
00218         for (tp = &tunnels[prio][h]; (t = *tp) != NULL; tp = &t->next) {
00219                 if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
00220                         return t;
00221         }
00222         if (!create)
00223                 return NULL;
00224 
00225         MOD_INC_USE_COUNT;
00226         dev = kmalloc(sizeof(*dev) + sizeof(*t), GFP_KERNEL);
00227         if (dev == NULL) {
00228                 MOD_DEC_USE_COUNT;
00229                 return NULL;
00230         }
00231         memset(dev, 0, sizeof(*dev) + sizeof(*t));
00232         dev->priv = (void*)(dev+1);
00233         nt = (struct ip_tunnel*)dev->priv;
00234         nt->dev = dev;
00235         dev->name = nt->parms.name;
00236         dev->init = ipip_tunnel_init;
00237         memcpy(&nt->parms, parms, sizeof(*parms));
00238         if (dev->name[0] == 0) {
00239                 int i;
00240                 for (i=1; i<100; i++) {
00241                         sprintf(dev->name, "tunl%d", i);
00242                         if (dev_get(dev->name) == NULL)
00243                                 break;
00244                 }
00245                 if (i==100)
00246                         goto failed;
00247                 memcpy(parms->name, dev->name, IFNAMSIZ);
00248         }
00249         if (register_netdevice(dev) < 0)
00250                 goto failed;
00251 
00252         ipip_tunnel_link(nt);
00253         /* Do not decrement MOD_USE_COUNT here. */
00254         return nt;
00255 
00256 failed:
00257         kfree(dev);
00258         MOD_DEC_USE_COUNT;
00259         return NULL;
00260 }
00261 
00262 
00263 static void ipip_tunnel_destroy(struct device *dev)
00264 {
00265         if (dev == &ipip_fb_tunnel_dev) {
00266                 tunnels_wc[0] = NULL;
00267                 synchronize_bh();
00268         } else {
00269                 ipip_tunnel_unlink((struct ip_tunnel*)dev->priv);
00270                 kfree(dev);
00271                 MOD_DEC_USE_COUNT;
00272         }
00273 }
00274 
00275 void ipip_err(struct sk_buff *skb, unsigned char *dp, int len)
00276 {
00277 #ifndef I_WISH_WORLD_WERE_PERFECT
00278 
00279 /* It is not :-( All the routers (except for Linux) return only
00280    8 bytes of packet payload. It means, that precise relaying of
00281    ICMP in the real Internet is absolutely infeasible.
00282  */
00283         struct iphdr *iph = (struct iphdr*)dp;
00284         int type = skb->h.icmph->type;
00285         int code = skb->h.icmph->code;
00286         struct ip_tunnel *t;
00287 
00288         if (len < sizeof(struct iphdr))
00289                 return;
00290 
00291         switch (type) {
00292         default:
00293         case ICMP_PARAMETERPROB:
00294                 return;
00295 
00296         case ICMP_DEST_UNREACH:
00297                 switch (code) {
00298                 case ICMP_SR_FAILED:
00299                 case ICMP_PORT_UNREACH:
00300                         /* Impossible event. */
00301                         return;
00302                 case ICMP_FRAG_NEEDED:
00303                         /* Soft state for pmtu is maintained by IP core. */
00304                         return;
00305                 default:
00306                         /* All others are translated to HOST_UNREACH.
00307                            rfc2003 contains "deep thoughts" about NET_UNREACH,
00308                            I believe they are just ether pollution. --ANK
00309                          */
00310                         break;
00311                 }
00312                 break;
00313         case ICMP_TIME_EXCEEDED:
00314                 if (code != ICMP_EXC_TTL)
00315                         return;
00316                 break;
00317         }
00318 
00319         t = ipip_tunnel_lookup(iph->daddr, iph->saddr);
00320         if (t == NULL || t->parms.iph.daddr == 0)
00321                 return;
00322         if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
00323                 return;
00324 
00325         if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
00326                 t->err_count++;
00327         else
00328                 t->err_count = 1;
00329         t->err_time = jiffies;
00330         return;
00331 #else
00332         struct iphdr *iph = (struct iphdr*)dp;
00333         int hlen = iph->ihl<<2;
00334         struct iphdr *eiph;
00335         int type = skb->h.icmph->type;
00336         int code = skb->h.icmph->code;
00337         int rel_type = 0;
00338         int rel_code = 0;
00339         int rel_info = 0;
00340         struct sk_buff *skb2;
00341         struct rtable *rt;
00342 
00343         if (len < hlen + sizeof(struct iphdr))
00344                 return;
00345         eiph = (struct iphdr*)(dp + hlen);
00346 
00347         switch (type) {
00348         default:
00349                 return;
00350         case ICMP_PARAMETERPROB:
00351                 if (skb->h.icmph->un.gateway < hlen)
00352                         return;
00353 
00354                 /* So... This guy found something strange INSIDE encapsulated
00355                    packet. Well, he is fool, but what can we do ?
00356                  */
00357                 rel_type = ICMP_PARAMETERPROB;
00358                 rel_info = skb->h.icmph->un.gateway - hlen;
00359                 break;
00360 
00361         case ICMP_DEST_UNREACH:
00362                 switch (code) {
00363                 case ICMP_SR_FAILED:
00364                 case ICMP_PORT_UNREACH:
00365                         /* Impossible event. */
00366                         return;
00367                 case ICMP_FRAG_NEEDED:
00368                         /* And it is the only really necesary thing :-) */
00369                         rel_info = ntohs(skb->h.icmph->un.frag.mtu);
00370                         if (rel_info < hlen+68)
00371                                 return;
00372                         rel_info -= hlen;
00373                         /* BSD 4.2 MORE DOES NOT EXIST IN NATURE. */
00374                         if (rel_info > ntohs(eiph->tot_len))
00375                                 return;
00376                         break;
00377                 default:
00378                         /* All others are translated to HOST_UNREACH.
00379                            rfc2003 contains "deep thoughts" about NET_UNREACH,
00380                            I believe, it is just ether pollution. --ANK
00381                          */
00382                         rel_type = ICMP_DEST_UNREACH;
00383                         rel_code = ICMP_HOST_UNREACH;
00384                         break;
00385                 }
00386                 break;
00387         case ICMP_TIME_EXCEEDED:
00388                 if (code != ICMP_EXC_TTL)
00389                         return;
00390                 break;
00391         }
00392 
00393         /* Prepare fake skb to feed it to icmp_send */
00394         skb2 = skb_clone(skb, GFP_ATOMIC);
00395         if (skb2 == NULL)
00396                 return;
00397         dst_release(skb2->dst);
00398         skb2->dst = NULL;
00399         skb_pull(skb2, skb->data - (u8*)eiph);
00400         skb2->nh.raw = skb2->data;
00401 
00402         /* Try to guess incoming interface */
00403         if (ip_route_output(&rt, eiph->saddr, 0, RT_TOS(eiph->tos), 0)) {
00404                 kfree_skb(skb2);
00405                 return;
00406         }
00407         skb2->dev = rt->u.dst.dev;
00408 
00409         /* route "incoming" packet */
00410         if (rt->rt_flags&RTCF_LOCAL) {
00411                 ip_rt_put(rt);
00412                 rt = NULL;
00413                 if (ip_route_output(&rt, eiph->daddr, eiph->saddr, eiph->tos, 0) ||
00414                     rt->u.dst.dev->type != ARPHRD_IPGRE) {
00415                         ip_rt_put(rt);
00416                         kfree_skb(skb2);
00417                         return;
00418                 }
00419         } else {
00420                 ip_rt_put(rt);
00421                 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos, skb2->dev) ||
00422                     skb2->dst->dev->type != ARPHRD_IPGRE) {
00423                         kfree_skb(skb2);
00424                         return;
00425                 }
00426         }
00427 
00428         /* change mtu on this route */
00429         if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
00430                 if (rel_info > skb2->dst->pmtu) {
00431                         kfree_skb(skb2);
00432                         return;
00433                 }
00434                 skb2->dst->pmtu = rel_info;
00435                 rel_info = htonl(rel_info);
00436         } else if (type == ICMP_TIME_EXCEEDED) {
00437                 struct ip_tunnel *t = (struct ip_tunnel*)skb2->dev->priv;
00438                 if (t->parms.iph.ttl) {
00439                         rel_type = ICMP_DEST_UNREACH;
00440                         rel_code = ICMP_HOST_UNREACH;
00441                 }
00442         }
00443 
00444         icmp_send(skb2, rel_type, rel_code, rel_info);
00445         kfree_skb(skb2);
00446         return;
00447 #endif
00448 }
00449 
00450 int ipip_rcv(struct sk_buff *skb, unsigned short len)
00451 {
00452         struct iphdr *iph;
00453         struct ip_tunnel *tunnel;
00454 
00455         iph = skb->nh.iph;
00456         skb->mac.raw = skb->nh.raw;
00457         skb->nh.raw = skb_pull(skb, skb->h.raw - skb->data);
00458         memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
00459         skb->protocol = __constant_htons(ETH_P_IP);
00460         skb->ip_summed = 0;
00461         skb->pkt_type = PACKET_HOST;
00462 
00463         if ((tunnel = ipip_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
00464                 tunnel->stat.rx_packets++;
00465                 tunnel->stat.rx_bytes += skb->len;
00466                 skb->dev = tunnel->dev;
00467                 dst_release(skb->dst);
00468                 skb->dst = NULL;
00469                 netif_rx(skb);
00470                 return 0;
00471         }
00472 
00473         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, 0);
00474         kfree_skb(skb);
00475         return 0;
00476 }
00477 
00478 /*
00479  *      This function assumes it is being called from dev_queue_xmit()
00480  *      and that skb is filled properly by that function.
00481  */
00482 
00483 static int ipip_tunnel_xmit(struct sk_buff *skb, struct device *dev)
00484 {
00485         struct ip_tunnel *tunnel = (struct ip_tunnel*)dev->priv;
00486         struct net_device_stats *stats = &tunnel->stat;
00487         struct iphdr  *tiph = &tunnel->parms.iph;
00488         u8     tos = tunnel->parms.iph.tos;
00489         u16    df = tiph->frag_off;
00490         struct rtable *rt;                      /* Route to the other host */
00491         struct device *tdev;                    /* Device to other host */
00492         struct iphdr  *old_iph = skb->nh.iph;
00493         struct iphdr  *iph;                     /* Our new IP header */
00494         int    max_headroom;                    /* The extra header space needed */
00495         u32    dst = tiph->daddr;
00496         int    mtu;
00497 
00498         if (tunnel->recursion++) {
00499                 tunnel->stat.collisions++;
00500                 goto tx_error;
00501         }
00502 
00503         if (skb->protocol != __constant_htons(ETH_P_IP))
00504                 goto tx_error;
00505 
00506         if (tos&1)
00507                 tos = old_iph->tos;
00508 
00509         if (!dst) {
00510                 /* NBMA tunnel */
00511                 if ((rt = (struct rtable*)skb->dst) == NULL) {
00512                         tunnel->stat.tx_fifo_errors++;
00513                         goto tx_error;
00514                 }
00515                 if ((dst = rt->rt_gateway) == 0)
00516                         goto tx_error_icmp;
00517         }
00518 
00519         if (ip_route_output(&rt, dst, tiph->saddr, RT_TOS(tos), tunnel->parms.link)) {
00520                 tunnel->stat.tx_carrier_errors++;
00521                 goto tx_error_icmp;
00522         }
00523         tdev = rt->u.dst.dev;
00524 
00525         if (tdev == dev) {
00526                 ip_rt_put(rt);
00527                 tunnel->stat.collisions++;
00528                 goto tx_error;
00529         }
00530 
00531         mtu = rt->u.dst.pmtu - sizeof(struct iphdr);
00532         if (mtu < 68) {
00533                 tunnel->stat.collisions++;
00534                 ip_rt_put(rt);
00535                 goto tx_error;
00536         }
00537         if (skb->dst && mtu < skb->dst->pmtu)
00538                 skb->dst->pmtu = mtu;
00539 
00540         df |= (old_iph->frag_off&__constant_htons(IP_DF));
00541 
00542         if ((old_iph->frag_off&__constant_htons(IP_DF)) && mtu < ntohs(old_iph->tot_len)) {
00543                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
00544                 ip_rt_put(rt);
00545                 goto tx_error;
00546         }
00547 
00548         if (tunnel->err_count > 0) {
00549                 if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
00550                         tunnel->err_count--;
00551                         dst_link_failure(skb);
00552                 } else
00553                         tunnel->err_count = 0;
00554         }
00555 
00556         skb->h.raw = skb->nh.raw;
00557 
00558         /*
00559          * Okay, now see if we can stuff it in the buffer as-is.
00560          */
00561         max_headroom = (((tdev->hard_header_len+15)&~15)+sizeof(struct iphdr));
00562 
00563         if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
00564                 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
00565                 if (!new_skb) {
00566                         ip_rt_put(rt);
00567                         stats->tx_dropped++;
00568                         dev_kfree_skb(skb);
00569                         tunnel->recursion--;
00570                         return 0;
00571                 }
00572                 if (skb->sk)
00573                         skb_set_owner_w(new_skb, skb->sk);
00574                 dev_kfree_skb(skb);
00575                 skb = new_skb;
00576         }
00577 
00578         skb->nh.raw = skb_push(skb, sizeof(struct iphdr));
00579         memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
00580         dst_release(skb->dst);
00581         skb->dst = &rt->u.dst;
00582 
00583         /*
00584          *      Push down and install the IPIP header.
00585          */
00586 
00587         iph                     =       skb->nh.iph;
00588         iph->version            =       4;
00589         iph->ihl                =       sizeof(struct iphdr)>>2;
00590         iph->frag_off           =       df;
00591         iph->protocol           =       IPPROTO_IPIP;
00592         iph->tos                =       tos;
00593         iph->daddr              =       rt->rt_dst;
00594         iph->saddr              =       rt->rt_src;
00595 
00596         if ((iph->ttl = tiph->ttl) == 0)
00597                 iph->ttl        =       old_iph->ttl;
00598 
00599         iph->tot_len            =       htons(skb->len);
00600         iph->id                 =       htons(ip_id_count++);
00601         ip_send_check(iph);
00602 
00603         stats->tx_bytes += skb->len;
00604         stats->tx_packets++;
00605         ip_send(skb);
00606         tunnel->recursion--;
00607         return 0;
00608 
00609 tx_error_icmp:
00610         dst_link_failure(skb);
00611 tx_error:
00612         stats->tx_errors++;
00613         dev_kfree_skb(skb);
00614         tunnel->recursion--;
00615         return 0;
00616 }
00617 
00618 static int
00619 ipip_tunnel_ioctl (struct device *dev, struct ifreq *ifr, int cmd)
00620 {
00621         int err = 0;
00622         struct ip_tunnel_parm p;
00623         struct ip_tunnel *t;
00624 
00625         MOD_INC_USE_COUNT;
00626 
00627         switch (cmd) {
00628         case SIOCGETTUNNEL:
00629                 t = NULL;
00630                 if (dev == &ipip_fb_tunnel_dev) {
00631                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
00632                                 err = -EFAULT;
00633                                 break;
00634                         }
00635                         t = ipip_tunnel_locate(&p, 0);
00636                 }
00637                 if (t == NULL)
00638                         t = (struct ip_tunnel*)dev->priv;
00639                 memcpy(&p, &t->parms, sizeof(p));
00640                 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
00641                         err = -EFAULT;
00642                 break;
00643 
00644         case SIOCADDTUNNEL:
00645         case SIOCCHGTUNNEL:
00646                 err = -EPERM;
00647                 if (!capable(CAP_NET_ADMIN))
00648                         goto done;
00649 
00650                 err = -EFAULT;
00651                 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
00652                         goto done;
00653 
00654                 err = -EINVAL;
00655                 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPIP ||
00656                     p.iph.ihl != 5 || (p.iph.frag_off&__constant_htons(~IP_DF)))
00657                         goto done;
00658                 if (p.iph.ttl)
00659                         p.iph.frag_off |= __constant_htons(IP_DF);
00660 
00661                 t = ipip_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
00662 
00663                 if (dev != &ipip_fb_tunnel_dev && cmd == SIOCCHGTUNNEL &&
00664                     t != &ipip_fb_tunnel) {
00665                         if (t != NULL) {
00666                                 if (t->dev != dev) {
00667                                         err = -EEXIST;
00668                                         break;
00669                                 }
00670                         } else {
00671                                 if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
00672                                     (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
00673                                         err = -EINVAL;
00674                                         break;
00675                                 }
00676                                 t = (struct ip_tunnel*)dev->priv;
00677                                 start_bh_atomic();
00678                                 ipip_tunnel_unlink(t);
00679                                 t->parms.iph.saddr = p.iph.saddr;
00680                                 t->parms.iph.daddr = p.iph.daddr;
00681                                 memcpy(dev->dev_addr, &p.iph.saddr, 4);
00682                                 memcpy(dev->broadcast, &p.iph.daddr, 4);
00683                                 ipip_tunnel_link(t);
00684                                 end_bh_atomic();
00685                                 netdev_state_change(dev);
00686                         }
00687                 }
00688 
00689                 if (t) {
00690                         err = 0;
00691                         if (cmd == SIOCCHGTUNNEL) {
00692                                 t->parms.iph.ttl = p.iph.ttl;
00693                                 t->parms.iph.tos = p.iph.tos;
00694                                 t->parms.iph.frag_off = p.iph.frag_off;
00695                         }
00696                         if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
00697                                 err = -EFAULT;
00698                 } else
00699                         err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
00700                 break;
00701 
00702         case SIOCDELTUNNEL:
00703                 err = -EPERM;
00704                 if (!capable(CAP_NET_ADMIN))
00705                         goto done;
00706 
00707                 if (dev == &ipip_fb_tunnel_dev) {
00708                         err = -EFAULT;
00709                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
00710                                 goto done;
00711                         err = -ENOENT;
00712                         if ((t = ipip_tunnel_locate(&p, 0)) == NULL)
00713                                 goto done;
00714                         err = -EPERM;
00715                         if (t == &ipip_fb_tunnel)
00716                                 goto done;
00717                 }
00718                 err = unregister_netdevice(dev);
00719                 break;
00720 
00721         default:
00722                 err = -EINVAL;
00723         }
00724 
00725 done:
00726         MOD_DEC_USE_COUNT;
00727         return err;
00728 }
00729 
00730 static struct net_device_stats *ipip_tunnel_get_stats(struct device *dev)
00731 {
00732         return &(((struct ip_tunnel*)dev->priv)->stat);
00733 }
00734 
00735 static int ipip_tunnel_change_mtu(struct device *dev, int new_mtu)
00736 {
00737         if (new_mtu < 68 || new_mtu > 0xFFF8 - sizeof(struct iphdr))
00738                 return -EINVAL;
00739         dev->mtu = new_mtu;
00740         return 0;
00741 }
00742 
00743 static void ipip_tunnel_init_gen(struct device *dev)
00744 {
00745         struct ip_tunnel *t = (struct ip_tunnel*)dev->priv;
00746 
00747         dev->destructor         = ipip_tunnel_destroy;
00748         dev->hard_start_xmit    = ipip_tunnel_xmit;
00749         dev->get_stats          = ipip_tunnel_get_stats;
00750         dev->do_ioctl           = ipip_tunnel_ioctl;
00751         dev->change_mtu         = ipip_tunnel_change_mtu;
00752 
00753         dev_init_buffers(dev);
00754 
00755         dev->type               = ARPHRD_TUNNEL;
00756         dev->hard_header_len    = LL_MAX_HEADER + sizeof(struct iphdr);
00757         dev->mtu                = 1500 - sizeof(struct iphdr);
00758         dev->flags              = IFF_NOARP;
00759         dev->iflink             = 0;
00760         dev->addr_len           = 4;
00761         memcpy(dev->dev_addr, &t->parms.iph.saddr, 4);
00762         memcpy(dev->broadcast, &t->parms.iph.daddr, 4);
00763 }
00764 
00765 static int ipip_tunnel_init(struct device *dev)
00766 {
00767         struct device *tdev = NULL;
00768         struct ip_tunnel *tunnel;
00769         struct iphdr *iph;
00770 
00771         tunnel = (struct ip_tunnel*)dev->priv;
00772         iph = &tunnel->parms.iph;
00773 
00774         ipip_tunnel_init_gen(dev);
00775 
00776         if (iph->daddr) {
00777                 struct rtable *rt;
00778                 if (!ip_route_output(&rt, iph->daddr, iph->saddr, RT_TOS(iph->tos), tunnel->parms.link)) {
00779                         tdev = rt->u.dst.dev;
00780                         ip_rt_put(rt);
00781                 }
00782                 dev->flags |= IFF_POINTOPOINT;
00783         }
00784 
00785         if (!tdev && tunnel->parms.link)
00786                 tdev = dev_get_by_index(tunnel->parms.link);
00787 
00788         if (tdev) {
00789                 dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
00790                 dev->mtu = tdev->mtu - sizeof(struct iphdr);
00791         }
00792         dev->iflink = tunnel->parms.link;
00793 
00794         return 0;
00795 }
00796 
00797 #ifdef MODULE
00798 static int ipip_fb_tunnel_open(struct device *dev)
00799 {
00800         MOD_INC_USE_COUNT;
00801         return 0;
00802 }
00803 
00804 static int ipip_fb_tunnel_close(struct device *dev)
00805 {
00806         MOD_DEC_USE_COUNT;
00807         return 0;
00808 }
00809 #endif
00810 
00811 __initfunc(int ipip_fb_tunnel_init(struct device *dev))
00812 {
00813         struct iphdr *iph;
00814 
00815         ipip_tunnel_init_gen(dev);
00816 #ifdef MODULE
00817         dev->open               = ipip_fb_tunnel_open;
00818         dev->stop               = ipip_fb_tunnel_close;
00819 #endif
00820 
00821         iph = &ipip_fb_tunnel.parms.iph;
00822         iph->version            = 4;
00823         iph->protocol           = IPPROTO_IPIP;
00824         iph->ihl                = 5;
00825 
00826         tunnels_wc[0]           = &ipip_fb_tunnel;
00827         return 0;
00828 }
00829 
00830 static struct inet_protocol ipip_protocol = {
00831   ipip_rcv,             /* IPIP handler          */
00832   ipip_err,             /* TUNNEL error control */
00833   0,                    /* next                 */
00834   IPPROTO_IPIP,         /* protocol ID          */
00835   0,                    /* copy                 */
00836   NULL,                 /* data                 */
00837   "IPIP"                /* name                 */
00838 };
00839 
00840 #ifdef MODULE
00841 int init_module(void) 
00842 #else
00843 __initfunc(int ipip_init(void))
00844 #endif
00845 {
00846         printk(KERN_INFO "IPv4 over IPv4 tunneling driver\n");
00847 
00848         ipip_fb_tunnel_dev.priv = (void*)&ipip_fb_tunnel;
00849         ipip_fb_tunnel_dev.name = ipip_fb_tunnel.parms.name;
00850 #ifdef MODULE
00851         register_netdev(&ipip_fb_tunnel_dev);
00852 #else
00853         register_netdevice(&ipip_fb_tunnel_dev);
00854 #endif
00855 
00856         inet_add_protocol(&ipip_protocol);
00857         return 0;
00858 }
00859 
00860 #ifdef MODULE
00861 
00862 void cleanup_module(void)
00863 {
00864         if ( inet_del_protocol(&ipip_protocol) < 0 )
00865                 printk(KERN_INFO "ipip close: can't remove protocol\n");
00866 
00867         unregister_netdevice(&ipip_fb_tunnel_dev);
00868 }
00869 
00870 #endif