root/drivers/net/new_tunnel.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. tunnel_open
  2. tunnel_close
  3. print_ip
  4. tunnel_xmit
  5. tunnel_get_stats
  6. tunnel_init
  7. tunnel_probe
  8. init_module
  9. cleanup_module

   1 /* tunnel.c: an IP tunnel driver
   2 
   3         The purpose of this driver is to provide an IP tunnel through
   4         which you can tunnel network traffic transparently across subnets.
   5 
   6         This was written by looking at Nick Holloway's dummy driver
   7         Thanks for the great code!
   8 
   9                 -Sam Lantinga   (slouken@cs.ucdavis.edu)  02/01/95
  10                 
  11         Minor tweaks:
  12                 Cleaned up the code a little and added some pre-1.3.0 tweaks.
  13                 dev->hard_header/hard_header_len changed to use no headers.
  14                 Comments/bracketing tweaked.
  15                 Made the tunnels use dev->name not tunnel: when error reporting.
  16                 Added tx_dropped stat
  17                 
  18                 -Alan Cox       (Alan.Cox@linux.org) 21 March 95
  19 
  20         Reworked:
  21                 Changed to tunnel to destination gateway in addition to the
  22                         tunnel's pointopoint address
  23                 Almost completely rewritten
  24                 Note:  There is currently no firewall or ICMP handling done.
  25 
  26                 -Sam Lantinga   (slouken@cs.ucdavis.edu) 02/13/96
  27                 
  28         Note:
  29                 The old driver is in tunnel.c if you have funnies with the
  30                 new one.
  31 */
  32 
  33 /* Things I wish I had known when writing the tunnel driver:
  34 
  35         When the tunnel_xmit() function is called, the skb contains the
  36         packet to be sent (plus a great deal of extra info), and dev
  37         contains the tunnel device that _we_ are.
  38 
  39         When we are passed a packet, we are expected to fill in the
  40         source address with our source IP address.
  41 
  42         What is the proper way to allocate, copy and free a buffer?
  43         After you allocate it, it is a "0 length" chunk of memory
  44         starting at zero.  If you want to add headers to the buffer
  45         later, you'll have to call "skb_reserve(skb, amount)" with
  46         the amount of memory you want reserved.  Then, you call
  47         "skb_put(skb, amount)" with the amount of space you want in
  48         the buffer.  skb_put() returns a pointer to the top (#0) of
  49         that buffer.  skb->len is set to the amount of space you have
  50         "allocated" with skb_put().  You can then write up to skb->len
  51         bytes to that buffer.  If you need more, you can call skb_put()
  52         again with the additional amount of space you need.  You can
  53         find out how much more space you can allocate by calling 
  54         "skb_tailroom(skb)".
  55         Now, to add header space, call "skb_push(skb, header_len)".
  56         This creates space at the beginning of the buffer and returns
  57         a pointer to this new space.  If later you need to strip a
  58         header from a buffer, call "skb_pull(skb, header_len)".
  59         skb_headroom() will return how much space is left at the top
  60         of the buffer (before the main data).  Remember, this headroom
  61         space must be reserved before the skb_put() function is called.
  62 */
  63 
  64 #include <linux/module.h>
  65 
  66 /* Only two headers!! :-) */
  67 #include <net/ip.h>
  68 #include <linux/if_arp.h>
  69 
  70 
  71 /*#define TUNNEL_DEBUG*/
  72 
  73 /* 
  74  *      Our header is a simple IP packet with no options
  75  */
  76  
  77 #define tunnel_hlen     sizeof(struct iphdr)
  78 
  79 /*
  80  *      Okay, this needs to be high enough that we can fit a "standard"
  81  *      ethernet header and an IP tunnel header into the outgoing packet.
  82  *      [36 bytes]
  83  */
  84  
  85 #define TUNL_HLEN       (((ETH_HLEN+15)&~15)+tunnel_hlen)
  86 
  87 
  88 #ifdef MODULE
  89 static int tunnel_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
  90 {
  91         MOD_INC_USE_COUNT;
  92         return 0;
  93 }
  94 
  95 static int tunnel_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
  96 {
  97         MOD_DEC_USE_COUNT;
  98         return 0;
  99 }
 100 
 101 #endif
 102 
 103 #ifdef TUNNEL_DEBUG
 104 void print_ip(struct iphdr *ip)
     /* [previous][next][first][last][top][bottom][index][help] */
 105 {
 106         unsigned char *ipaddr;
 107 
 108         printk("IP packet:\n");
 109         printk("--- header len = %d\n", ip->ihl*4);
 110         printk("--- ip version: %d\n", ip->version);
 111         printk("--- ip protocol: %d\n", ip->protocol);
 112         ipaddr=(unsigned char *)&ip->saddr;
 113         printk("--- source address: %u.%u.%u.%u\n", 
 114                         *ipaddr, *(ipaddr+1), *(ipaddr+2), *(ipaddr+3));
 115         ipaddr=(unsigned char *)&ip->daddr;
 116         printk("--- destination address: %u.%u.%u.%u\n", 
 117                         *ipaddr, *(ipaddr+1), *(ipaddr+2), *(ipaddr+3));
 118         printk("--- total packet len: %d\n", ntohs(ip->tot_len));
 119 }
 120 #endif
 121 
 122 /*
 123  *      This function assumes it is being called from dev_queue_xmit()
 124  *      and that skb is filled properly by that function.
 125  */
 126 
 127 static int tunnel_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 128 {
 129         struct enet_statistics *stats;          /* This device's statistics */
 130         struct rtable *rt;                      /* Route to the other host */
 131         struct device *tdev;                    /* Device to other host */
 132         struct iphdr  *iph;                     /* Our new IP header */
 133         __u32          target;                  /* The other host's IP address */
 134         int      max_headroom;                  /* The extra header space needed */
 135 
 136         /*
 137          *      Return if there is nothing to do.  (Does this ever happen?)
 138          */
 139         if (skb == NULL || dev == NULL) {
 140 #ifdef TUNNEL_DEBUG
 141                 printk ( KERN_INFO "tunnel: Nothing to do!\n" );
 142 #endif
 143                 return 0;
 144         }
 145 
 146         /* 
 147          *      Make sure we are not busy (check lock variable) 
 148          */
 149          
 150         stats = (struct enet_statistics *)dev->priv;
 151         cli();
 152         if (dev->tbusy != 0) 
 153         {
 154                 sti();
 155                 stats->tx_errors++;
 156                 return(1);
 157         }
 158         dev->tbusy = 1;
 159         sti();
 160   
 161         /*printk("-");*/
 162         /*
 163          *  First things first.  Look up the destination address in the 
 164          *  routing tables
 165          */
 166         iph = (struct iphdr *) skb->data;
 167         if ((rt = ip_rt_route(iph->daddr, 0)) == NULL)
 168         { 
 169                 /* No route to host */
 170                 /* Where did the packet come from? */
 171                 /*icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_UNREACH, 0, dev);*/
 172                 printk ( KERN_INFO "%s: Packet with no route!\n", dev->name);
 173                 dev->tbusy=0;
 174                 stats->tx_errors++;
 175                 return(1);
 176         }
 177 
 178         /*
 179          * Get the target address (other end of IP tunnel)
 180          */
 181         if (rt->rt_flags & RTF_GATEWAY)
 182                 target = rt->rt_gateway;
 183         else
 184                 target = dev->pa_dstaddr;
 185 
 186         if ( ! target )
 187         {       /* No gateway to tunnel through? */
 188                 /* Where did the packet come from? */
 189                 /*icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_UNREACH, 0, dev);*/
 190                 printk ( KERN_INFO "%s: Packet with no target gateway!\n", dev->name);
 191                 ip_rt_put(rt);
 192                 dev->tbusy=0;
 193                 stats->tx_errors++;
 194                 return(1);
 195         }
 196         ip_rt_put(rt);
 197 
 198         if ((rt = ip_rt_route(target, 0)) == NULL)
 199         { 
 200                 /* No route to host */
 201                 /* Where did the packet come from? */
 202                 /*icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_UNREACH, 0, dev);*/
 203                 printk ( KERN_INFO "%s: Can't reach target gateway!\n", dev->name);
 204                 dev->tbusy=0;
 205                 stats->tx_errors++;
 206                 return(1);
 207         }
 208         tdev = rt->rt_dev;
 209 
 210         if (tdev == dev)
 211         { 
 212                 /* Tunnel to ourselves?  -- I don't think so. */
 213                 printk ( KERN_INFO "%s: Packet targetted at myself!\n" , dev->name);
 214                 ip_rt_put(rt);
 215                 dev->tbusy=0;
 216                 stats->tx_errors++;
 217                 return(1);
 218         }
 219 
 220 #ifdef TUNNEL_DEBUG
 221         printk("Old IP Header....\n");
 222         print_ip(iph);
 223 #endif
 224 
 225         /*
 226          * Okay, now see if we can stuff it in the buffer as-is.
 227          */
 228         max_headroom = (((tdev->hard_header_len+15)&~15)+tunnel_hlen);
 229 #ifdef TUNNEL_DEBUG
 230 printk("Room left at head: %d\n", skb_headroom(skb));
 231 printk("Room left at tail: %d\n", skb_tailroom(skb));
 232 printk("Required room: %d, Tunnel hlen: %d\n", max_headroom, TUNL_HLEN);
 233 #endif
 234         if (skb_headroom(skb) >= max_headroom) {
 235                 skb->h.iph = (struct iphdr *) skb_push(skb, tunnel_hlen);
 236         } else {
 237                 struct sk_buff *new_skb;
 238 
 239                 if ( !(new_skb = dev_alloc_skb(skb->len+max_headroom)) ) 
 240                 {
 241                         printk( KERN_INFO "%s: Out of memory, dropped packet\n",
 242                                 dev->name);
 243                         ip_rt_put(rt);
 244                         dev->tbusy = 0;
 245                         stats->tx_dropped++;
 246                         return(1);
 247                 }
 248                 new_skb->free = 1;
 249 
 250                 /*
 251                  * Reserve space for our header and the lower device header
 252                  */
 253                 skb_reserve(new_skb, max_headroom);
 254 
 255                 /*
 256                  * Copy the old packet to the new buffer.
 257                  * Note that new_skb->h.iph will be our (tunnel driver's) header
 258                  * and new_skb->ip_hdr is the IP header of the old packet.
 259                  */
 260                 new_skb->ip_hdr = (struct iphdr *) skb_put(new_skb, skb->len);
 261                 memcpy(new_skb->ip_hdr, skb->data, skb->len);
 262                 /* Is this necessary? */
 263                 memcpy(new_skb->proto_priv, skb->proto_priv, sizeof(skb->proto_priv));
 264 
 265                 /* Tack on our header */
 266                 new_skb->h.iph = (struct iphdr *) skb_push(new_skb, tunnel_hlen);
 267 
 268                 /* Free the old packet, we no longer need it */
 269                 kfree_skb(skb, FREE_WRITE);
 270                 skb = new_skb;
 271         }
 272 
 273         /*
 274          *      Push down and install the IPIP header.
 275          */
 276          
 277         iph                     =       skb->h.iph;
 278         iph->version            =       4;
 279         iph->tos                =       skb->ip_hdr->tos;
 280         iph->ttl                =       skb->ip_hdr->ttl;
 281         iph->frag_off           =       0;
 282         iph->daddr              =       target;
 283         iph->saddr              =       tdev->pa_addr;
 284         iph->protocol           =       IPPROTO_IPIP;
 285         iph->ihl                =       5;
 286         iph->tot_len            =       htons(skb->len);
 287         iph->id                 =       htons(ip_id_count++);   /* Race condition here? */
 288         ip_send_check(iph);
 289         skb->ip_hdr             = skb->h.iph;
 290         skb->protocol           =       htons(ETH_P_IP);
 291 #ifdef TUNNEL_DEBUG
 292         printk("New IP Header....\n");
 293         print_ip(iph);
 294 #endif
 295 
 296         /*
 297          *      Send the packet on its way!
 298          *      Note that dev_queue_xmit() will eventually free the skb.
 299          *      If ip_forward() made a copy, it will return 1 so we can free.
 300          */
 301 
 302         if (ip_forward(skb, dev, 0, target) == 1)
 303                 kfree_skb(skb, FREE_WRITE);
 304 
 305         /*
 306          *      Clean up:  We're done with the route and the packet
 307          */
 308          
 309         ip_rt_put(rt);
 310  
 311 #ifdef TUNNEL_DEBUG
 312         printk("Packet sent through tunnel interface!\n");
 313 #endif
 314 /*printk(">");*/
 315         /* Record statistics and return */
 316         stats->tx_packets++;
 317         dev->tbusy=0;
 318         return 0;
 319 }
 320 
 321 static struct enet_statistics *tunnel_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 322 {
 323         return((struct enet_statistics*) dev->priv);
 324 }
 325 
 326 /*
 327  *      Called when a new tunnel device is initialized.
 328  *      The new tunnel device structure is passed to us.
 329  */
 330  
 331 int tunnel_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 332 {
 333         int i;
 334 
 335         /* Oh, just say we're here, in case anyone cares */
 336         static int tun_msg=0;
 337         if(!tun_msg)
 338         {
 339                 printk ( KERN_INFO "tunnel: version v0.2b2\n" );
 340                 tun_msg=1;
 341         }
 342 
 343         /* Add our tunnel functions to the device */
 344 #ifdef MODULE
 345         dev->open               = tunnel_open;
 346         dev->stop               = tunnel_close;
 347 #endif
 348         dev->hard_start_xmit    = tunnel_xmit;
 349         dev->get_stats          = tunnel_get_stats;
 350         dev->priv = kmalloc(sizeof(struct enet_statistics), GFP_KERNEL);
 351         if (dev->priv == NULL)
 352                 return -ENOMEM;
 353         memset(dev->priv, 0, sizeof(struct enet_statistics));
 354 
 355         /* Initialize the tunnel device structure */
 356         for (i = 0; i < DEV_NUMBUFFS; i++)
 357                 skb_queue_head_init(&dev->buffs[i]);
 358 
 359         dev->hard_header        = NULL;
 360         dev->rebuild_header     = NULL;
 361         dev->set_mac_address    = NULL;
 362         dev->header_cache_bind  = NULL;
 363         dev->header_cache_update= NULL;
 364 
 365         dev->type                               = ARPHRD_TUNNEL;
 366         dev->hard_header_len    = TUNL_HLEN;
 367         dev->mtu                = 1500-tunnel_hlen;     /* eth_mtu */
 368         dev->addr_len           = 0;            /* Is this only for ARP? */
 369         dev->tx_queue_len       = 2;            /* Small queue */
 370         memset(dev->broadcast,0xFF, ETH_ALEN);
 371 
 372         /* New-style flags. */
 373         dev->flags              = IFF_NOARP;    /* Don't use ARP on this device */
 374                                                 /* No broadcasting through a tunnel */
 375         dev->family             = AF_INET;
 376         dev->pa_addr            = 0;
 377         dev->pa_brdaddr         = 0;
 378         dev->pa_mask            = 0;
 379         dev->pa_alen            = 4;
 380 
 381         /* We're done.  Have I forgotten anything? */
 382         return 0;
 383 }
 384 
 385 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 386 /*  Module specific interface                                      */
 387 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 388 #ifdef MODULE
 389 
 390 static int tunnel_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 391 {
 392         tunnel_init(dev);
 393         return 0;
 394 }
 395 
 396 static struct device dev_tunnel = 
 397 {
 398         "tunl0\0   ", 
 399         0, 0, 0, 0,
 400         0x0, 0,
 401         0, 0, 0, NULL, tunnel_probe 
 402  };
 403 
 404 int init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 405 {
 406         /* Find a name for this unit */
 407         int ct= 1;
 408         
 409         while(dev_get(dev_tunnel.name)!=NULL && ct<100)
 410         {
 411                 sprintf(dev_tunnel.name,"tunl%d",ct);
 412                 ct++;
 413         }
 414         
 415 #ifdef TUNNEL_DEBUG
 416         printk("tunnel: registering device %s\n", dev_tunnel.name);
 417 #endif
 418         if (register_netdev(&dev_tunnel) != 0)
 419                 return -EIO;
 420         return 0;
 421 }
 422 
 423 void cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 424 {
 425         unregister_netdev(&dev_tunnel);
 426         kfree_s(dev_tunnel.priv,sizeof(struct enet_statistics));
 427         dev_tunnel.priv=NULL;
 428 }
 429 #endif /* MODULE */
 430 

/* [previous][next][first][last][top][bottom][index][help] */