Import 2.1.15
[davej-history.git] / drivers / net / tunnel.c
blob43487fc96c9d498aaf14e9b0090e0e0ac74fccb9
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
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
18 -Alan Cox (Alan.Cox@linux.org) 21 March 95
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.
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
51 "skb_tailroom(skb)".
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>
64 #include <linux/in.h>
65 #include <net/ip.h>
66 #include <linux/if_arp.h>
68 /*#define TUNNEL_DEBUG*/
70 /*
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.
79 * [36 bytes]
82 #define TUNL_HLEN (((ETH_HLEN+15)&~15)+tunnel_hlen)
85 static inttunnel_open(struct device *dev)
87 MOD_INC_USE_COUNT;
88 return0;
91 static inttunnel_close(struct device *dev)
93 MOD_DEC_USE_COUNT;
94 return0;
97 #ifdef TUNNEL_DEBUG
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));
114 #endif
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 */
125 struct hh_cache *hh;
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
134 * routing tables
136 iph = skb->nh.iph;
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);
141 stats->tx_errors++;
142 dev_kfree_skb(skb, FREE_WRITE);
143 return0;
145 tdev = rt->u.dst.dev;
146 hh = rt->u.dst.hh;
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);
151 ip_rt_put(rt);
152 stats->tx_errors++;
153 dev_kfree_skb(skb, FREE_WRITE);
154 return0;
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);
166 if(!new_skb) {
167 ip_rt_put(rt);
168 stats->tx_dropped++;
169 dev_kfree_skb(skb, FREE_WRITE);
170 return0;
172 dev_kfree_skb(skb, FREE_WRITE);
173 skb = new_skb;
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.
186 iph = skb->nh.iph;
187 iph->version =4;
188 iph->tos = skb->h.ipiph->tos;
189 iph->ttl = skb->h.ipiph->ttl;
190 iph->frag_off =0;
191 iph->daddr = dev->pa_dstaddr;
192 iph->saddr = dev->pa_addr;
193 iph->protocol = IPPROTO_IPIP;
194 iph->ihl =5;
195 iph->tot_len =htons(skb->len);
196 iph->id =htons(ip_id_count++);/* Race condition here? */
197 ip_send_check(iph);
199 ip_send(skb);
201 /* Record statistics and return */
202 stats->tx_packets++;
203 return0;
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)
218 int i;
220 /* Oh, just say we're here, in case anyone cares */
221 static int tun_msg=0;
222 if(!tun_msg)
224 printk( KERN_INFO "tunnel: version v0.2b2\n");
225 tun_msg=1;
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)
235 return-ENOMEM;
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;
259 dev->pa_addr =0;
260 dev->pa_brdaddr =0;
261 dev->pa_mask =0;
262 dev->pa_alen =4;
264 /* We're done. Have I forgotten anything? */
265 return0;
268 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
269 /* Module specific interface */
270 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
271 #ifdef MODULE
273 static inttunnel_probe(struct device *dev)
275 tunnel_init(dev);
276 return0;
279 static struct device dev_tunnel =
281 "tunl0\0 ",
282 0,0,0,0,
283 0x0,0,
284 0,0,0, NULL, tunnel_probe
287 intinit_module(void)
289 /* Find a name for this unit */
290 int ct=1;
292 while(dev_get(dev_tunnel.name)!=NULL && ct<100)
294 sprintf(dev_tunnel.name,"tunl%d",ct);
295 ct++;
298 #ifdef TUNNEL_DEBUG
299 printk("tunnel: registering device %s\n", dev_tunnel.name);
300 #endif
301 if(register_netdev(&dev_tunnel) !=0)
302 return-EIO;
303 return0;
306 voidcleanup_module(void)
308 unregister_netdev(&dev_tunnel);
309 kfree_s(dev_tunnel.priv,sizeof(struct enet_statistics));
310 dev_tunnel.priv=NULL;
312 #endif/* MODULE */
close