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>
67 #include <linux/init.h>
69 /*#define TUNNEL_DEBUG*/
72 * Our header is a simple IP packet with no options
75 #define tunnel_hlen sizeof(struct iphdr)
78 * Okay, this needs to be high enough that we can fit a "standard"
79 * ethernet header and an IP tunnel header into the outgoing packet.
83 #define TUNL_HLEN (((ETH_HLEN+15)&~15)+tunnel_hlen)
86 static int tunnel_open(struct device
*dev
)
92 static int tunnel_close(struct device
*dev
)
99 void print_ip(struct iphdr
*ip
)
101 unsigned char *ipaddr
;
103 printk("IP packet:\n");
104 printk("--- header len = %d\n", ip
->ihl
*4);
105 printk("--- ip version: %d\n", ip
->version
);
106 printk("--- ip protocol: %d\n", ip
->protocol
);
107 ipaddr
=(unsigned char *)&ip
->saddr
;
108 printk("--- source address: %u.%u.%u.%u\n",
109 *ipaddr
, *(ipaddr
+1), *(ipaddr
+2), *(ipaddr
+3));
110 ipaddr
=(unsigned char *)&ip
->daddr
;
111 printk("--- destination address: %u.%u.%u.%u\n",
112 *ipaddr
, *(ipaddr
+1), *(ipaddr
+2), *(ipaddr
+3));
113 printk("--- total packet len: %d\n", ntohs(ip
->tot_len
));
118 * This function assumes it is being called from dev_queue_xmit()
119 * and that skb is filled properly by that function.
122 static int tunnel_xmit(struct sk_buff
*skb
, struct device
*dev
)
124 struct net_device_stats
*stats
; /* This device's statistics */
125 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 net_device_stats
*)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
;
147 if (tdev
->type
== ARPHRD_TUNNEL
) {
148 /* Tunnel to tunnel? -- I don't think so. */
149 printk ( KERN_INFO
"%s: Packet targetted at myself!\n" , dev
->name
);
152 dev_kfree_skb(skb
, FREE_WRITE
);
156 skb
->h
.ipiph
= skb
->nh
.iph
;
159 * Okay, now see if we can stuff it in the buffer as-is.
161 max_headroom
= (((tdev
->hard_header_len
+15)&~15)+tunnel_hlen
);
163 if (skb_headroom(skb
) < max_headroom
|| skb_shared(skb
)) {
164 struct sk_buff
*new_skb
= skb_realloc_headroom(skb
, max_headroom
);
168 dev_kfree_skb(skb
, FREE_WRITE
);
171 dev_kfree_skb(skb
, FREE_WRITE
);
175 skb
->nh
.iph
= (struct iphdr
*) skb_push(skb
, tunnel_hlen
);
176 dst_release(skb
->dst
);
177 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
178 dst_release(skb
->dst
);
179 skb
->dst
= &rt
->u
.dst
;
182 * Push down and install the IPIP header.
187 iph
->tos
= skb
->h
.ipiph
->tos
;
188 iph
->ttl
= skb
->h
.ipiph
->ttl
;
190 iph
->daddr
= dev
->pa_dstaddr
;
191 iph
->saddr
= dev
->pa_addr
;
192 iph
->protocol
= IPPROTO_IPIP
;
194 iph
->tot_len
= htons(skb
->len
);
195 iph
->id
= htons(ip_id_count
++); /* Race condition here? */
198 stats
->tx_bytes
+=skb
->len
;
202 /* Record statistics and return */
207 static struct net_device_stats
*tunnel_get_stats(struct device
*dev
)
209 return((struct net_device_stats
*) dev
->priv
);
213 * Called when a new tunnel device is initialized.
214 * The new tunnel device structure is passed to us.
217 __initfunc(int tunnel_init(struct device
*dev
))
219 /* Oh, just say we're here, in case anyone cares */
220 static int tun_msg
=0;
223 printk ( KERN_INFO
"tunnel: version v0.2b2\n" );
227 /* Add our tunnel functions to the device */
228 dev
->open
= tunnel_open
;
229 dev
->stop
= tunnel_close
;
230 dev
->hard_start_xmit
= tunnel_xmit
;
231 dev
->get_stats
= tunnel_get_stats
;
232 dev
->priv
= kmalloc(sizeof(struct net_device_stats
), GFP_KERNEL
);
233 if (dev
->priv
== NULL
)
235 memset(dev
->priv
, 0, sizeof(struct net_device_stats
));
237 /* Initialize the tunnel device structure */
239 dev_init_buffers(dev
);
241 dev
->hard_header
= NULL
;
242 dev
->rebuild_header
= NULL
;
243 dev
->set_mac_address
= NULL
;
244 dev
->hard_header_cache
= NULL
;
245 dev
->header_cache_update
= NULL
;
247 dev
->type
= ARPHRD_TUNNEL
;
248 dev
->hard_header_len
= TUNL_HLEN
;
249 dev
->mtu
= 1500-tunnel_hlen
; /* eth_mtu */
250 dev
->addr_len
= 0; /* Is this only for ARP? */
251 dev
->tx_queue_len
= 2; /* Small queue */
252 memset(dev
->broadcast
,0xFF, ETH_ALEN
);
254 /* New-style flags. */
255 dev
->flags
= IFF_NOARP
; /* Don't use ARP on this device */
256 /* No broadcasting through a tunnel */
257 dev
->family
= AF_INET
;
263 /* We're done. Have I forgotten anything? */
267 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
268 /* Module specific interface */
269 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
273 static char tunnel_name
[16];
275 static struct device dev_tunnel
=
280 0, 0, 0, NULL
, tunnel_init
283 int init_module(void)
285 /* Find a name for this unit */
286 int err
=dev_alloc_name(&dev_tunnel
, "tunl%d");
291 printk("tunnel: registering device %s\n", dev_tunnel
.name
);
293 if (register_netdev(&dev_tunnel
) != 0)
298 void cleanup_module(void)
300 unregister_netdev(&dev_tunnel
);
301 kfree_s(dev_tunnel
.priv
,sizeof(struct net_device_stats
));
302 dev_tunnel
.priv
=NULL
;