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 #include <linux/config.h> /* for CONFIG_IP_FORWARD */ 66
67 /* Only two headers!! :-) */ 68 #include <net/ip.h>
69 #include <linux/if_arp.h>
70
71
72 /*#define TUNNEL_DEBUG*/ 73
74 /* 75 * Our header is a simple IP packet with no options 76 */ 77
78 #definetunnel_hlensizeof(structiphdr)
79
80 /* 81 * Okay, this needs to be high enough that we can fit a "standard" 82 * ethernet header and an IP tunnel header into the outgoing packet. 83 * [36 bytes] 84 */ 85
86 #defineTUNL_HLEN (((ETH_HLEN+15)&~15)+tunnel_hlen)
87
88
89 #ifdefMODULE 90 staticinttunnel_open(structdevice *dev)
/* */ 91 { 92 MOD_INC_USE_COUNT;
93 return 0;
94 } 95
96 staticinttunnel_close(structdevice *dev)
/* */ 97 { 98 MOD_DEC_USE_COUNT;
99 return 0;
100 } 101
102 #endif 103
104 #ifdefTUNNEL_DEBUG 105 voidprint_ip(structiphdr *ip)
/* */ 106 { 107 unsignedchar *ipaddr;
108
109 printk("IP packet:\n");
110 printk("--- header len = %d\n", ip->ihl*4);
111 printk("--- ip version: %d\n", ip->version);
112 printk("--- ip protocol: %d\n", ip->protocol);
113 ipaddr=(unsignedchar *)&ip->saddr;
114 printk("--- source address: %u.%u.%u.%u\n",
115 *ipaddr, *(ipaddr+1), *(ipaddr+2), *(ipaddr+3));
116 ipaddr=(unsignedchar *)&ip->daddr;
117 printk("--- destination address: %u.%u.%u.%u\n",
118 *ipaddr, *(ipaddr+1), *(ipaddr+2), *(ipaddr+3));
119 printk("--- total packet len: %d\n", ntohs(ip->tot_len));
120 } 121 #endif 122
123 /* 124 * This function assumes it is being called from dev_queue_xmit() 125 * and that skb is filled properly by that function. 126 */ 127
128 staticinttunnel_xmit(structsk_buff *skb, structdevice *dev)
/* */ 129 { 130 structenet_statistics *stats; /* This device's statistics */ 131 structrtable *rt; /* Route to the other host */ 132 structdevice *tdev; /* Device to other host */ 133 structiphdr *iph; /* Our new IP header */ 134 __u32target; /* The other host's IP address */ 135 intmax_headroom; /* The extra header space needed */ 136
137 /* 138 * Return if there is nothing to do. (Does this ever happen?) 139 */ 140 if (skb == NULL || dev == NULL) { 141 #ifdefTUNNEL_DEBUG 142 printk ( KERN_INFO "tunnel: Nothing to do!\n" );
143 #endif 144 return 0;
145 } 146
147 /* 148 * Make sure we are not busy (check lock variable) 149 */ 150
151 stats = (structenet_statistics *)dev->priv;
152 cli();
153 if (dev->tbusy != 0)
154 { 155 sti();
156 stats->tx_errors++;
157 return(1);
158 } 159 dev->tbusy = 1;
160 sti();
161
162 /*printk("-");*/ 163 /* 164 * First things first. Look up the destination address in the 165 * routing tables 166 */ 167 iph = (structiphdr *) skb->data;
168 if ((rt = ip_rt_route(iph->daddr, 0)) == NULL)
169 { 170 /* No route to host */ 171 /* Where did the packet come from? */ 172 /*icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_UNREACH, 0, dev);*/ 173 printk ( KERN_INFO "%s: Packet with no route!\n", dev->name);
174 dev->tbusy=0;
175 stats->tx_errors++;
176 dev_kfree_skb(skb, FREE_WRITE);
177 return 0;
178 } 179
180 /* 181 * Get the target address (other end of IP tunnel) 182 */ 183 if (rt->rt_flags & RTF_GATEWAY)
184 target = rt->rt_gateway;
185 else 186 target = dev->pa_dstaddr;
187
188 if ( ! target )
189 {/* No gateway to tunnel through? */ 190 /* Where did the packet come from? */ 191 /*icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_UNREACH, 0, dev);*/ 192 printk ( KERN_INFO "%s: Packet with no target gateway!\n", dev->name);
193 ip_rt_put(rt);
194 dev->tbusy=0;
195 stats->tx_errors++;
196 dev_kfree_skb(skb, FREE_WRITE);
197 return 0;
198 } 199 ip_rt_put(rt);
200
201 if ((rt = ip_rt_route(target, 0)) == NULL)
202 { 203 /* No route to host */ 204 /* Where did the packet come from? */ 205 /*icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_UNREACH, 0, dev);*/ 206 printk ( KERN_INFO "%s: Can't reach target gateway!\n", dev->name);
207 dev->tbusy=0;
208 stats->tx_errors++;
209 dev_kfree_skb(skb, FREE_WRITE);
210 return 0;
211 } 212 tdev = rt->rt_dev;
213
214 if (tdev == dev)
215 { 216 /* Tunnel to ourselves? -- I don't think so. */ 217 printk ( KERN_INFO "%s: Packet targetted at myself!\n" , dev->name);
218 ip_rt_put(rt);
219 dev->tbusy=0;
220 stats->tx_errors++;
221 dev_kfree_skb(skb, FREE_WRITE);
222 return 0;
223 } 224
225 #ifdefTUNNEL_DEBUG 226 printk("Old IP Header....\n");
227 print_ip(iph);
228 #endif 229
230 /* 231 * Okay, now see if we can stuff it in the buffer as-is. 232 */ 233 max_headroom = (((tdev->hard_header_len+15)&~15)+tunnel_hlen);
234 #ifdefTUNNEL_DEBUG 235 printk("Room left at head: %d\n", skb_headroom(skb));
236 printk("Room left at tail: %d\n", skb_tailroom(skb));
237 printk("Required room: %d, Tunnel hlen: %d\n", max_headroom, TUNL_HLEN);
238 #endif 239 if (skb_headroom(skb) >= max_headroom) { 240 skb->h.iph = (structiphdr *) skb_push(skb, tunnel_hlen);
241 }else{ 242 structsk_buff *new_skb;
243
244 if ( !(new_skb = dev_alloc_skb(skb->len+max_headroom)) )
245 { 246 printk( KERN_INFO "%s: Out of memory, dropped packet\n",
247 dev->name);
248 ip_rt_put(rt);
249 dev->tbusy = 0;
250 stats->tx_dropped++;
251 dev_kfree_skb(skb, FREE_WRITE);
252 return 0;
253 } 254 new_skb->free = 1;
255
256 /* 257 * Reserve space for our header and the lower device header 258 */ 259 skb_reserve(new_skb, max_headroom);
260
261 /* 262 * Copy the old packet to the new buffer. 263 * Note that new_skb->h.iph will be our (tunnel driver's) header 264 * and new_skb->ip_hdr is the IP header of the old packet. 265 */ 266 new_skb->ip_hdr = (structiphdr *) skb_put(new_skb, skb->len);
267 memcpy(new_skb->ip_hdr, skb->data, skb->len);
268 memset(new_skb->proto_priv, 0, sizeof(skb->proto_priv));
269
270 /* Tack on our header */ 271 new_skb->h.iph = (structiphdr *) skb_push(new_skb, tunnel_hlen);
272
273 /* Free the old packet, we no longer need it */ 274 dev_kfree_skb(skb, FREE_WRITE);
275 skb = new_skb;
276 } 277
278 /* 279 * Push down and install the IPIP header. 280 */ 281
282 iph = skb->h.iph;
283 iph->version = 4;
284 iph->tos = skb->ip_hdr->tos;
285 iph->ttl = skb->ip_hdr->ttl;
286 iph->frag_off = 0;
287 iph->daddr = target;
288 iph->saddr = tdev->pa_addr;
289 iph->protocol = IPPROTO_IPIP;
290 iph->ihl = 5;
291 iph->tot_len = htons(skb->len);
292 iph->id = htons(ip_id_count++); /* Race condition here? */ 293 ip_send_check(iph);
294 skb->ip_hdr = skb->h.iph;
295 skb->protocol = htons(ETH_P_IP);
296 #ifdefTUNNEL_DEBUG 297 printk("New IP Header....\n");
298 print_ip(iph);
299 #endif 300
301 /* 302 * Send the packet on its way! 303 * Note that dev_queue_xmit() will eventually free the skb. 304 * If ip_forward() made a copy, it will return 1 so we can free. 305 */ 306
307 #ifdefCONFIG_IP_FORWARD 308 if (ip_forward(skb, dev, 0, target))
309 #endif 310 kfree_skb(skb, FREE_WRITE);
311
312 /* 313 * Clean up: We're done with the route and the packet 314 */ 315
316 ip_rt_put(rt);
317
318 #ifdefTUNNEL_DEBUG 319 printk("Packet sent through tunnel interface!\n");
320 #endif 321 /*printk(">");*/ 322 /* Record statistics and return */ 323 stats->tx_packets++;
324 dev->tbusy=0;
325 return 0;
326 } 327
328 staticstructenet_statistics *tunnel_get_stats(structdevice *dev)
/* */ 329 { 330 return((structenet_statistics*) dev->priv);
331 } 332
333 /* 334 * Called when a new tunnel device is initialized. 335 * The new tunnel device structure is passed to us. 336 */ 337
338 inttunnel_init(structdevice *dev)
/* */ 339 { 340 inti;
341
342 /* Oh, just say we're here, in case anyone cares */ 343 staticinttun_msg=0;
344 if(!tun_msg)
345 { 346 printk ( KERN_INFO "tunnel: version v0.2b2\n" );
347 tun_msg=1;
348 } 349
350 /* Add our tunnel functions to the device */ 351 #ifdefMODULE 352 dev->open = tunnel_open;
353 dev->stop = tunnel_close;
354 #endif 355 dev->hard_start_xmit = tunnel_xmit;
356 dev->get_stats = tunnel_get_stats;
357 dev->priv = kmalloc(sizeof(structenet_statistics), GFP_KERNEL);
358 if (dev->priv == NULL)
359 return -ENOMEM;
360 memset(dev->priv, 0, sizeof(structenet_statistics));
361
362 /* Initialize the tunnel device structure */ 363 for (i = 0; i < DEV_NUMBUFFS; i++)
364 skb_queue_head_init(&dev->buffs[i]);
365
366 dev->hard_header = NULL;
367 dev->rebuild_header = NULL;
368 dev->set_mac_address = NULL;
369 dev->header_cache_bind = NULL;
370 dev->header_cache_update= NULL;
371
372 dev->type = ARPHRD_TUNNEL;
373 dev->hard_header_len = TUNL_HLEN;
374 dev->mtu = 1500-tunnel_hlen; /* eth_mtu */ 375 dev->addr_len = 0; /* Is this only for ARP? */ 376 dev->tx_queue_len = 2; /* Small queue */ 377 memset(dev->broadcast,0xFF, ETH_ALEN);
378
379 /* New-style flags. */ 380 dev->flags = IFF_NOARP; /* Don't use ARP on this device */ 381 /* No broadcasting through a tunnel */ 382 dev->family = AF_INET;
383 dev->pa_addr = 0;
384 dev->pa_brdaddr = 0;
385 dev->pa_mask = 0;
386 dev->pa_alen = 4;
387
388 /* We're done. Have I forgotten anything? */ 389 return 0;
390 } 391
392 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 393 /* Module specific interface */ 394 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 395 #ifdefMODULE 396
397 staticinttunnel_probe(structdevice *dev)
/* */ 398 { 399 tunnel_init(dev);
400 return 0;
401 } 402
403 staticstructdevicedev_tunnel =
404 { 405 "tunl0\0 ",
406 0, 0, 0, 0,
407 0x0, 0,
408 0, 0, 0, NULL, tunnel_probe 409 };
410
411 intinit_module(void)
/* */ 412 { 413 /* Find a name for this unit */ 414 intct= 1;
415
416 while(dev_get(dev_tunnel.name)!=NULL && ct<100)
417 { 418 sprintf(dev_tunnel.name,"tunl%d",ct);
419 ct++;
420 } 421
422 #ifdefTUNNEL_DEBUG 423 printk("tunnel: registering device %s\n", dev_tunnel.name);
424 #endif 425 if (register_netdev(&dev_tunnel) != 0)
426 return -EIO;
427 return 0;
428 } 429
430 voidcleanup_module(void)
/* */ 431 { 432 unregister_netdev(&dev_tunnel);
433 kfree_s(dev_tunnel.priv,sizeof(structenet_statistics));
434 dev_tunnel.priv=NULL;
435 } 436 #endif/* MODULE */ 437