ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / ethertap.c
1 /*
2  *      Ethertap: A network device for bouncing packets via user space
3  *
4  *      This is a very simple ethernet driver. It bounces ethernet frames
5  *      to user space on /dev/tap0->/dev/tap15 and expects ethernet frames
6  *      to be written back to it. By default it does not ARP. If you turn ARP
7  *      on it will attempt to ARP the user space and reply to ARPS from the
8  *      user space.
9  *
10  *      As this is an ethernet device you can use it for appletalk, IPX etc
11  *      even for building bridging tunnels.
12  */
13  
14 #include <linux/config.h>
15 #include <linux/module.h>
16
17 #include <linux/kernel.h>
18 #include <linux/jiffies.h>
19 #include <linux/slab.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22
23 #include <linux/netdevice.h>
24 #include <linux/inetdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/skbuff.h>
27 #include <linux/init.h>
28
29 #include <net/sock.h>
30 #include <linux/netlink.h>
31
32 /*
33  *      Index to functions.
34  */
35
36 static int  ethertap_open(struct net_device *dev);
37 static int  ethertap_start_xmit(struct sk_buff *skb, struct net_device *dev);
38 static int  ethertap_close(struct net_device *dev);
39 static struct net_device_stats *ethertap_get_stats(struct net_device *dev);
40 static void ethertap_rx(struct sock *sk, int len);
41 #ifdef CONFIG_ETHERTAP_MC
42 static void set_multicast_list(struct net_device *dev);
43 #endif
44
45 static int ethertap_debug;
46
47 static int max_taps = 1;
48 MODULE_PARM(max_taps, "i");
49 MODULE_PARM_DESC(max_taps,"Max number of ethernet tap devices");
50
51 static struct net_device **tap_map;     /* Returns the tap device for a given netlink */
52
53 /*
54  *      Board-specific info in dev->priv.
55  */
56
57 struct net_local
58 {
59         struct sock     *nl;
60 #ifdef CONFIG_ETHERTAP_MC
61         __u32           groups;
62 #endif
63         struct net_device_stats stats;
64 };
65
66 /*
67  *      To call this a probe is a bit misleading, however for real
68  *      hardware it would have to check what was present.
69  */
70 static int  __init ethertap_probe(int unit)
71 {
72         struct net_device *dev;
73         int err = -ENOMEM;
74
75         dev = alloc_etherdev(sizeof(struct net_local));
76
77         if (!dev)
78                 goto out;
79
80         SET_MODULE_OWNER(dev);
81
82         sprintf(dev->name, "tap%d", unit);
83         dev->base_addr = unit + NETLINK_TAPBASE;
84
85         netdev_boot_setup_check(dev);
86
87         memcpy(dev->dev_addr, "\xFE\xFD\x00\x00\x00\x00", 6);
88         if (dev->mem_start & 0xf)
89                 ethertap_debug = dev->mem_start & 0x7;
90
91         /*
92          *      The tap specific entries in the device structure.
93          */
94
95         dev->open = ethertap_open;
96         dev->hard_start_xmit = ethertap_start_xmit;
97         dev->stop = ethertap_close;
98         dev->get_stats = ethertap_get_stats;
99 #ifdef CONFIG_ETHERTAP_MC
100         dev->set_multicast_list = set_multicast_list;
101 #endif
102
103         dev->tx_queue_len = 0;
104         dev->flags|=IFF_NOARP;
105
106         err = register_netdev(dev);
107         if (err)
108                 goto out_free;
109
110         tap_map[unit]=dev;
111         return 0;
112 out_free:
113         free_netdev(dev);
114 out:
115         return err;
116 }
117
118 /*
119  *      Open/initialize the board.
120  */
121
122 static int ethertap_open(struct net_device *dev)
123 {
124         struct net_local *lp = netdev_priv(dev);
125
126         if (ethertap_debug > 2)
127                 printk(KERN_DEBUG "%s: Doing ethertap_open()...", dev->name);
128
129         lp->nl = netlink_kernel_create(dev->base_addr, ethertap_rx);
130         if (lp->nl == NULL)
131                 return -ENOBUFS;
132
133         netif_start_queue(dev);
134         return 0;
135 }
136
137 #ifdef CONFIG_ETHERTAP_MC
138 static unsigned ethertap_mc_hash(__u8 *dest)
139 {
140         unsigned idx = 0;
141         idx ^= dest[0];
142         idx ^= dest[1];
143         idx ^= dest[2];
144         idx ^= dest[3];
145         idx ^= dest[4];
146         idx ^= dest[5];
147         return 1U << (idx&0x1F);
148 }
149
150 static void set_multicast_list(struct net_device *dev)
151 {
152         unsigned groups = ~0;
153         struct net_local *lp = netdev_priv(dev);
154
155         if (!(dev->flags&(IFF_NOARP|IFF_PROMISC|IFF_ALLMULTI))) {
156                 struct dev_mc_list *dmi;
157
158                 groups = ethertap_mc_hash(dev->broadcast);
159
160                 for (dmi=dev->mc_list; dmi; dmi=dmi->next) {
161                         if (dmi->dmi_addrlen != 6)
162                                 continue;
163                         groups |= ethertap_mc_hash(dmi->dmi_addr);
164                 }
165         }
166         lp->groups = groups;
167         if (lp->nl)
168                 lp->nl->protinfo.af_netlink.groups = groups;
169 }
170 #endif
171
172 /*
173  *      We transmit by throwing the packet at netlink. We have to clone
174  *      it for 2.0 so that we dev_kfree_skb() the locked original.
175  */
176  
177 static int ethertap_start_xmit(struct sk_buff *skb, struct net_device *dev)
178 {
179         struct net_local *lp = netdev_priv(dev);
180 #ifdef CONFIG_ETHERTAP_MC
181         struct ethhdr *eth = (struct ethhdr*)skb->data;
182 #endif
183
184         if (skb_headroom(skb) < 2) {
185                 static int once;
186                 struct sk_buff *skb2;
187
188                 if (!once) {
189                         once = 1;
190                         printk(KERN_DEBUG "%s: not aligned xmit by protocol %04x\n", dev->name, skb->protocol);
191                 }
192
193                 skb2 = skb_realloc_headroom(skb, 2);
194                 dev_kfree_skb(skb);
195                 if (skb2 == NULL)
196                         return 0;
197                 skb = skb2;
198         }
199         __skb_push(skb, 2);
200
201         /* Make the same thing, which loopback does. */
202         if (skb_shared(skb)) {
203                 struct sk_buff *skb2 = skb;
204                 skb = skb_clone(skb, GFP_ATOMIC);       /* Clone the buffer */
205                 if (skb==NULL) {
206                         dev_kfree_skb(skb2);
207                         return 0;
208                 }
209                 dev_kfree_skb(skb2);
210         }
211         /* ... but do not orphan it here, netlink does it in any case. */
212
213         lp->stats.tx_bytes+=skb->len;
214         lp->stats.tx_packets++;
215
216 #ifndef CONFIG_ETHERTAP_MC
217         netlink_broadcast(lp->nl, skb, 0, ~0, GFP_ATOMIC);
218 #else
219         if (dev->flags&IFF_NOARP) {
220                 netlink_broadcast(lp->nl, skb, 0, ~0, GFP_ATOMIC);
221                 return 0;
222         }
223
224         if (!(eth->h_dest[0]&1)) {
225                 /* Unicast packet */
226                 __u32 pid;
227                 memcpy(&pid, eth->h_dest+2, 4);
228                 netlink_unicast(lp->nl, skb, ntohl(pid), MSG_DONTWAIT);
229         } else
230                 netlink_broadcast(lp->nl, skb, 0, ethertap_mc_hash(eth->h_dest), GFP_ATOMIC);
231 #endif
232         return 0;
233 }
234
235 static __inline__ int ethertap_rx_skb(struct sk_buff *skb, struct net_device *dev)
236 {
237         struct net_local *lp = netdev_priv(dev);
238 #ifdef CONFIG_ETHERTAP_MC
239         struct ethhdr *eth = (struct ethhdr*)(skb->data + 2);
240 #endif
241         int len = skb->len;
242
243         if (len < 16) {
244                 printk(KERN_DEBUG "%s : rx len = %d\n", dev->name, len);
245                 kfree_skb(skb);
246                 return -EINVAL;
247         }
248         if (NETLINK_CREDS(skb)->uid) {
249                 printk(KERN_INFO "%s : user %d\n", dev->name, NETLINK_CREDS(skb)->uid);
250                 kfree_skb(skb);
251                 return -EPERM;
252         }
253
254 #ifdef CONFIG_ETHERTAP_MC
255         if (!(dev->flags&(IFF_NOARP|IFF_PROMISC))) {
256                 int drop = 0;
257
258                 if (eth->h_dest[0]&1) {
259                         if (!(ethertap_mc_hash(eth->h_dest)&lp->groups))
260                                 drop = 1;
261                 } else if (memcmp(eth->h_dest, dev->dev_addr, 6) != 0)
262                         drop = 1;
263
264                 if (drop) {
265                         if (ethertap_debug > 3)
266                                 printk(KERN_DEBUG "%s : not for us\n", dev->name);
267                         kfree_skb(skb);
268                         return -EINVAL;
269                 }
270         }
271 #endif
272
273         if (skb_shared(skb)) {
274                 struct sk_buff *skb2 = skb;
275                 skb = skb_clone(skb, GFP_KERNEL);       /* Clone the buffer */
276                 if (skb==NULL) {
277                         kfree_skb(skb2);
278                         return -ENOBUFS;
279                 }
280                 kfree_skb(skb2);
281         } else
282                 skb_orphan(skb);
283
284         skb_pull(skb, 2);
285         skb->dev = dev;
286         skb->protocol=eth_type_trans(skb,dev);
287         memset(skb->cb, 0, sizeof(skb->cb));
288         lp->stats.rx_packets++;
289         lp->stats.rx_bytes+=len;
290         netif_rx(skb);
291         dev->last_rx = jiffies;
292         return len;
293 }
294
295 /*
296  *      The typical workload of the driver:
297  *      Handle the ether interface interrupts.
298  *
299  *      (In this case handle the packets posted from user space..)
300  */
301
302 static void ethertap_rx(struct sock *sk, int len)
303 {
304         unsigned unit = sk->sk_protocol - NETLINK_TAPBASE; 
305         struct net_device *dev;
306         struct sk_buff *skb;
307
308         if (unit >= max_taps || (dev = tap_map[unit]) == NULL) { 
309                 printk(KERN_CRIT "ethertap: bad unit %u!\n", unit);
310                 skb_queue_purge(&sk->sk_receive_queue);
311                 return;
312         }
313
314         if (ethertap_debug > 3)
315                 printk(KERN_DEBUG "%s: ethertap_rx()\n", dev->name);
316
317         while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL)
318                 ethertap_rx_skb(skb, dev);
319 }
320
321 static int ethertap_close(struct net_device *dev)
322 {
323         struct net_local *lp = netdev_priv(dev);
324         struct sock *sk = lp->nl;
325
326         if (ethertap_debug > 2)
327                 printk(KERN_DEBUG "%s: Shutting down.\n", dev->name);
328
329         netif_stop_queue(dev);
330
331         if (sk) {
332                 lp->nl = NULL;
333                 sock_release(sk->sk_socket);
334         }
335
336         return 0;
337 }
338
339 static struct net_device_stats *ethertap_get_stats(struct net_device *dev)
340 {
341         struct net_local *lp = netdev_priv(dev);
342         return &lp->stats;
343 }
344
345
346 int __init ethertap_init(void)
347 {
348         int i, err = 0;
349
350         /* netlink can only hande 16 entries unless modified */
351         if (max_taps > MAX_LINKS - NETLINK_TAPBASE)
352                 return -E2BIG;
353
354         tap_map = kmalloc(sizeof(struct net_device *)*max_taps, GFP_KERNEL);
355         if (!tap_map)
356                 return -ENOMEM;
357
358         for (i = 0; i < max_taps; i++) {
359                 err = ethertap_probe(i);
360                 if (err) {
361                         while (--i > 0) {
362                                 unregister_netdev(tap_map[i]);
363                                 free_netdev(tap_map[i]);
364                         }
365                         break;
366                 }
367         }
368         if (err)
369                 kfree(tap_map);
370         return err;
371 }
372 module_init(ethertap_init);
373
374 void __exit ethertap_cleanup(void)
375 {
376         int i;
377
378         for (i = 0; i < max_taps; i++) {
379                 struct net_device *dev = tap_map[i];
380                 if (dev) {
381                         tap_map[i] = NULL;
382                         unregister_netdev(dev);
383                         free_netdev(dev);
384                 }
385         }
386         kfree(tap_map);
387 }
388 module_exit(ethertap_cleanup);
389
390 MODULE_LICENSE("GPL");