1 /* tunnel.c: an IP tunnel driver 3 The purpose of this driver is to provide an IP tunnel through 4 which you can tunnel network traffic transparently across subnets. 6 This was written by looking at Nick Holloway's dummy driver 7 Thanks for the great code! 9 -Sam Lantinga (slouken@cs.ucdavis.edu) 02/01/95 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. 18 -Alan Cox (Alan.Cox@linux.org) 21 March 95 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. 26 -Sam Lantinga (slouken@cs.ucdavis.edu) 02/13/96 30 /* Things I wish I had known when writing the tunnel driver: 32 When the tunnel_xmit() function is called, the skb contains the 33 packet to be sent (plus a great deal of extra info), and dev 34 contains the tunnel device that _we_ are. 36 When we are passed a packet, we are expected to fill in the 37 source address with our source IP address. 39 What is the proper way to allocate, copy and free a buffer? 40 After you allocate it, it is a "0 length" chunk of memory 41 starting at zero. If you want to add headers to the buffer 42 later, you'll have to call "skb_reserve(skb, amount)" with 43 the amount of memory you want reserved. Then, you call 44 "skb_put(skb, amount)" with the amount of space you want in 45 the buffer. skb_put() returns a pointer to the top (#0) of 46 that buffer. skb->len is set to the amount of space you have 47 "allocated" with skb_put(). You can then write up to skb->len 48 bytes to that buffer. If you need more, you can call skb_put() 49 again with the additional amount of space you need. You can 50 find out how much more space you can allocate by calling 52 Now, to add header space, call "skb_push(skb, header_len)". 53 This creates space at the beginning of the buffer and returns 54 a pointer to this new space. If later you need to strip a 55 header from a buffer, call "skb_pull(skb, header_len)". 56 skb_headroom() will return how much space is left at the top 57 of the buffer (before the main data). Remember, this headroom 58 space must be reserved before the skb_put() function is called. 61 #include <linux/module.h> 62 #include <linux/types.h> 63 #include <linux/socket.h> 66 #include <linux/if_arp.h> 68 /*#define TUNNEL_DEBUG*/ 71 * Our header is a simple IP packet with no options 74 #define tunnel_hlen sizeof(struct iphdr) 77 * Okay, this needs to be high enough that we can fit a "standard" 78 * ethernet header and an IP tunnel header into the outgoing packet. 82 #define TUNL_HLEN (((ETH_HLEN+15)&~15)+tunnel_hlen) 85 static inttunnel_open(struct device
*dev
) 91 static inttunnel_close(struct device
*dev
) 98 voidprint_ip(struct iphdr
*ip
) 100 unsigned char*ipaddr
; 102 printk("IP packet:\n"); 103 printk("--- header len = %d\n", ip
->ihl
*4); 104 printk("--- ip version: %d\n", ip
->version
); 105 printk("--- ip protocol: %d\n", ip
->protocol
); 106 ipaddr
=(unsigned char*)&ip
->saddr
; 107 printk("--- source address: %u.%u.%u.%u\n", 108 *ipaddr
, *(ipaddr
+1), *(ipaddr
+2), *(ipaddr
+3)); 109 ipaddr
=(unsigned char*)&ip
->daddr
; 110 printk("--- destination address: %u.%u.%u.%u\n", 111 *ipaddr
, *(ipaddr
+1), *(ipaddr
+2), *(ipaddr
+3)); 112 printk("--- total packet len: %d\n",ntohs(ip
->tot_len
)); 117 * This function assumes it is being called from dev_queue_xmit() 118 * and that skb is filled properly by that function. 121 static inttunnel_xmit(struct sk_buff
*skb
,struct device
*dev
) 123 struct enet_statistics
*stats
;/* This device's statistics */ 124 struct rtable
*rt
;/* Route to the other host */ 126 struct device
*tdev
;/* Device to other host */ 127 struct iphdr
*iph
;/* Our new IP header */ 128 int max_headroom
;/* The extra header space needed */ 130 stats
= (struct enet_statistics
*)dev
->priv
; 133 * First things first. Look up the destination address in the 138 if(ip_route_output(&rt
, dev
->pa_dstaddr
, dev
->pa_addr
,RT_TOS(iph
->tos
), NULL
)) { 139 /* No route to host */ 140 printk( KERN_INFO
"%s: Can't reach target gateway!\n", dev
->name
); 142 dev_kfree_skb(skb
, FREE_WRITE
); 145 tdev
= rt
->u
.dst
.dev
; 148 if(tdev
->type
== ARPHRD_TUNNEL
) { 149 /* Tunnel to tunnel? -- I don't think so. */ 150 printk( KERN_INFO
"%s: Packet targetted at myself!\n", dev
->name
); 153 dev_kfree_skb(skb
, FREE_WRITE
); 157 skb
->h
.ipiph
= skb
->nh
.iph
; 160 * Okay, now see if we can stuff it in the buffer as-is. 162 max_headroom
= (((tdev
->hard_header_len
+15)&~15)+tunnel_hlen
); 164 if(skb_headroom(skb
) < max_headroom
|| skb
->users
!=1) { 165 struct sk_buff
*new_skb
=skb_realloc_headroom(skb
, max_headroom
); 169 dev_kfree_skb(skb
, FREE_WRITE
); 172 dev_kfree_skb(skb
, FREE_WRITE
); 176 skb
->nh
.iph
= (struct iphdr
*)skb_push(skb
, tunnel_hlen
); 177 dst_release(skb
->dst
); 178 memset(&(IPCB(skb
)->opt
),0,sizeof(IPCB(skb
)->opt
)); 179 dst_release(skb
->dst
); 180 skb
->dst
= &rt
->u
.dst
; 183 * Push down and install the IPIP header. 188 iph
->tos
= skb
->h
.ipiph
->tos
; 189 iph
->ttl
= skb
->h
.ipiph
->ttl
; 191 iph
->daddr
= dev
->pa_dstaddr
; 192 iph
->saddr
= dev
->pa_addr
; 193 iph
->protocol
= IPPROTO_IPIP
; 195 iph
->tot_len
=htons(skb
->len
); 196 iph
->id
=htons(ip_id_count
++);/* Race condition here? */ 201 /* Record statistics and return */ 206 static struct enet_statistics
*tunnel_get_stats(struct device
*dev
) 208 return((struct enet_statistics
*) dev
->priv
); 212 * Called when a new tunnel device is initialized. 213 * The new tunnel device structure is passed to us. 216 inttunnel_init(struct device
*dev
) 220 /* Oh, just say we're here, in case anyone cares */ 221 static int tun_msg
=0; 224 printk( KERN_INFO
"tunnel: version v0.2b2\n"); 228 /* Add our tunnel functions to the device */ 229 dev
->open
= tunnel_open
; 230 dev
->stop
= tunnel_close
; 231 dev
->hard_start_xmit
= tunnel_xmit
; 232 dev
->get_stats
= tunnel_get_stats
; 233 dev
->priv
=kmalloc(sizeof(struct enet_statistics
), GFP_KERNEL
); 234 if(dev
->priv
== NULL
) 236 memset(dev
->priv
,0,sizeof(struct enet_statistics
)); 238 /* Initialize the tunnel device structure */ 239 for(i
=0; i
< DEV_NUMBUFFS
; i
++) 240 skb_queue_head_init(&dev
->buffs
[i
]); 242 dev
->hard_header
= NULL
; 243 dev
->rebuild_header
= NULL
; 244 dev
->set_mac_address
= NULL
; 245 dev
->hard_header_cache
= NULL
; 246 dev
->header_cache_update
= NULL
; 248 dev
->type
= ARPHRD_TUNNEL
; 249 dev
->hard_header_len
= TUNL_HLEN
; 250 dev
->mtu
=1500-tunnel_hlen
;/* eth_mtu */ 251 dev
->addr_len
=0;/* Is this only for ARP? */ 252 dev
->tx_queue_len
=2;/* Small queue */ 253 memset(dev
->broadcast
,0xFF, ETH_ALEN
); 255 /* New-style flags. */ 256 dev
->flags
= IFF_NOARP
;/* Don't use ARP on this device */ 257 /* No broadcasting through a tunnel */ 258 dev
->family
= AF_INET
; 264 /* We're done. Have I forgotten anything? */ 268 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 269 /* Module specific interface */ 270 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 273 static inttunnel_probe(struct device
*dev
) 279 static struct device dev_tunnel
= 284 0,0,0, NULL
, tunnel_probe
289 /* Find a name for this unit */ 292 while(dev_get(dev_tunnel
.name
)!=NULL
&& ct
<100) 294 sprintf(dev_tunnel
.name
,"tunl%d",ct
); 299 printk("tunnel: registering device %s\n", dev_tunnel
.name
); 301 if(register_netdev(&dev_tunnel
) !=0) 306 voidcleanup_module(void) 308 unregister_netdev(&dev_tunnel
); 309 kfree_s(dev_tunnel
.priv
,sizeof(struct enet_statistics
)); 310 dev_tunnel
.priv
=NULL
;