cleanup
[linux-2.4.21-pre4.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/sched.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 int         ethertap_probe(struct net_device *dev);
37 static int  ethertap_open(struct net_device *dev);
38 static int  ethertap_start_xmit(struct sk_buff *skb, struct net_device *dev);
39 static int  ethertap_close(struct net_device *dev);
40 static struct net_device_stats *ethertap_get_stats(struct net_device *dev);
41 static void ethertap_rx(struct sock *sk, int len);
42 #ifdef CONFIG_ETHERTAP_MC
43 static void set_multicast_list(struct net_device *dev);
44 #endif
45
46 static int ethertap_debug;
47
48 static struct net_device *tap_map[32];  /* Returns the tap device for a given netlink */
49
50 /*
51  *      Board-specific info in dev->priv.
52  */
53
54 struct net_local
55 {
56         struct sock     *nl;
57 #ifdef CONFIG_ETHERTAP_MC
58         __u32           groups;
59 #endif
60         struct net_device_stats stats;
61 };
62
63 /*
64  *      To call this a probe is a bit misleading, however for real
65  *      hardware it would have to check what was present.
66  */
67  
68 int __init ethertap_probe(struct net_device *dev)
69 {
70         SET_MODULE_OWNER(dev);
71
72         memcpy(dev->dev_addr, "\xFE\xFD\x00\x00\x00\x00", 6);
73         if (dev->mem_start & 0xf)
74                 ethertap_debug = dev->mem_start & 0x7;
75
76         /*
77          *      Initialize the device structure.
78          */
79
80         dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
81         if (dev->priv == NULL)
82                 return -ENOMEM;
83         memset(dev->priv, 0, sizeof(struct net_local));
84
85         /*
86          *      The tap specific entries in the device structure.
87          */
88
89         dev->open = ethertap_open;
90         dev->hard_start_xmit = ethertap_start_xmit;
91         dev->stop = ethertap_close;
92         dev->get_stats = ethertap_get_stats;
93 #ifdef CONFIG_ETHERTAP_MC
94         dev->set_multicast_list = set_multicast_list;
95 #endif
96
97         /*
98          *      Setup the generic properties
99          */
100
101         ether_setup(dev);
102
103         dev->tx_queue_len = 0;
104         dev->flags|=IFF_NOARP;
105         tap_map[dev->base_addr]=dev;
106
107         return 0;
108 }
109
110 /*
111  *      Open/initialize the board.
112  */
113
114 static int ethertap_open(struct net_device *dev)
115 {
116         struct net_local *lp = (struct net_local*)dev->priv;
117
118         if (ethertap_debug > 2)
119                 printk("%s: Doing ethertap_open()...", dev->name);
120
121         lp->nl = netlink_kernel_create(dev->base_addr, ethertap_rx);
122         if (lp->nl == NULL)
123                 return -ENOBUFS;
124         netif_start_queue(dev);
125         return 0;
126 }
127
128 #ifdef CONFIG_ETHERTAP_MC
129 static unsigned ethertap_mc_hash(__u8 *dest)
130 {
131         unsigned idx = 0;
132         idx ^= dest[0];
133         idx ^= dest[1];
134         idx ^= dest[2];
135         idx ^= dest[3];
136         idx ^= dest[4];
137         idx ^= dest[5];
138         return 1U << (idx&0x1F);
139 }
140
141 static void set_multicast_list(struct net_device *dev)
142 {
143         unsigned groups = ~0;
144         struct net_local *lp = (struct net_local *)dev->priv;
145
146         if (!(dev->flags&(IFF_NOARP|IFF_PROMISC|IFF_ALLMULTI))) {
147                 struct dev_mc_list *dmi;
148
149                 groups = ethertap_mc_hash(dev->broadcast);
150
151                 for (dmi=dev->mc_list; dmi; dmi=dmi->next) {
152                         if (dmi->dmi_addrlen != 6)
153                                 continue;
154                         groups |= ethertap_mc_hash(dmi->dmi_addr);
155                 }
156         }
157         lp->groups = groups;
158         if (lp->nl)
159                 lp->nl->protinfo.af_netlink.groups = groups;
160 }
161 #endif
162
163 /*
164  *      We transmit by throwing the packet at netlink. We have to clone
165  *      it for 2.0 so that we dev_kfree_skb() the locked original.
166  */
167  
168 static int ethertap_start_xmit(struct sk_buff *skb, struct net_device *dev)
169 {
170         struct net_local *lp = (struct net_local *)dev->priv;
171 #ifdef CONFIG_ETHERTAP_MC
172         struct ethhdr *eth = (struct ethhdr*)skb->data;
173 #endif
174
175         if (skb_headroom(skb) < 2) {
176                 static int once;
177                 struct sk_buff *skb2;
178
179                 if (!once) {
180                         once = 1;
181                         printk(KERN_DEBUG "%s: not aligned xmit by protocol %04x\n", dev->name, skb->protocol);
182                 }
183
184                 skb2 = skb_realloc_headroom(skb, 2);
185                 dev_kfree_skb(skb);
186                 if (skb2 == NULL)
187                         return 0;
188                 skb = skb2;
189         }
190         __skb_push(skb, 2);
191
192         /* Make the same thing, which loopback does. */
193         if (skb_shared(skb)) {
194                 struct sk_buff *skb2 = skb;
195                 skb = skb_clone(skb, GFP_ATOMIC);       /* Clone the buffer */
196                 if (skb==NULL) {
197                         dev_kfree_skb(skb2);
198                         return 0;
199                 }
200                 dev_kfree_skb(skb2);
201         }
202         /* ... but do not orphan it here, netlink does it in any case. */
203
204         lp->stats.tx_bytes+=skb->len;
205         lp->stats.tx_packets++;
206
207 #ifndef CONFIG_ETHERTAP_MC
208         netlink_broadcast(lp->nl, skb, 0, ~0, GFP_ATOMIC);
209 #else
210         if (dev->flags&IFF_NOARP) {
211                 netlink_broadcast(lp->nl, skb, 0, ~0, GFP_ATOMIC);
212                 return 0;
213         }
214
215         if (!(eth->h_dest[0]&1)) {
216                 /* Unicast packet */
217                 __u32 pid;
218                 memcpy(&pid, eth->h_dest+2, 4);
219                 netlink_unicast(lp->nl, skb, ntohl(pid), MSG_DONTWAIT);
220         } else
221                 netlink_broadcast(lp->nl, skb, 0, ethertap_mc_hash(eth->h_dest), GFP_ATOMIC);
222 #endif
223         return 0;
224 }
225
226 static __inline__ int ethertap_rx_skb(struct sk_buff *skb, struct net_device *dev)
227 {
228         struct net_local *lp = (struct net_local *)dev->priv;
229 #ifdef CONFIG_ETHERTAP_MC
230         struct ethhdr *eth = (struct ethhdr*)(skb->data + 2);
231 #endif
232         int len = skb->len;
233
234         if (len < 16) {
235                 printk(KERN_DEBUG "%s : rx len = %d\n", dev->name, len);
236                 kfree_skb(skb);
237                 return -EINVAL;
238         }
239         if (NETLINK_CREDS(skb)->uid) {
240                 printk(KERN_INFO "%s : user %d\n", dev->name, NETLINK_CREDS(skb)->uid);
241                 kfree_skb(skb);
242                 return -EPERM;
243         }
244
245 #ifdef CONFIG_ETHERTAP_MC
246         if (!(dev->flags&(IFF_NOARP|IFF_PROMISC))) {
247                 int drop = 0;
248
249                 if (eth->h_dest[0]&1) {
250                         if (!(ethertap_mc_hash(eth->h_dest)&lp->groups))
251                                 drop = 1;
252                 } else if (memcmp(eth->h_dest, dev->dev_addr, 6) != 0)
253                         drop = 1;
254
255                 if (drop) {
256                         if (ethertap_debug > 3)
257                                 printk(KERN_DEBUG "%s : not for us\n", dev->name);
258                         kfree_skb(skb);
259                         return -EINVAL;
260                 }
261         }
262 #endif
263
264         if (skb_shared(skb)) {
265                 struct sk_buff *skb2 = skb;
266                 skb = skb_clone(skb, GFP_KERNEL);       /* Clone the buffer */
267                 if (skb==NULL) {
268                         kfree_skb(skb2);
269                         return -ENOBUFS;
270                 }
271                 kfree_skb(skb2);
272         } else
273                 skb_orphan(skb);
274
275         skb_pull(skb, 2);
276         skb->dev = dev;
277         skb->protocol=eth_type_trans(skb,dev);
278         memset(skb->cb, 0, sizeof(skb->cb));
279         lp->stats.rx_packets++;
280         lp->stats.rx_bytes+=len;
281         netif_rx(skb);
282         dev->last_rx = jiffies;
283         return len;
284 }
285
286 /*
287  *      The typical workload of the driver:
288  *      Handle the ether interface interrupts.
289  *
290  *      (In this case handle the packets posted from user space..)
291  */
292
293 static void ethertap_rx(struct sock *sk, int len)
294 {
295         struct net_device *dev = tap_map[sk->protocol];
296         struct sk_buff *skb;
297
298         if (dev==NULL) {
299                 printk(KERN_CRIT "ethertap: bad unit!\n");
300                 skb_queue_purge(&sk->receive_queue);
301                 return;
302         }
303
304         if (ethertap_debug > 3)
305                 printk("%s: ethertap_rx()\n", dev->name);
306
307         while ((skb = skb_dequeue(&sk->receive_queue)) != NULL)
308                 ethertap_rx_skb(skb, dev);
309 }
310
311 static int ethertap_close(struct net_device *dev)
312 {
313         struct net_local *lp = (struct net_local *)dev->priv;
314         struct sock *sk = lp->nl;
315
316         if (ethertap_debug > 2)
317                 printk("%s: Shutting down.\n", dev->name);
318
319         netif_stop_queue(dev);
320
321         if (sk) {
322                 lp->nl = NULL;
323                 sock_release(sk->socket);
324         }
325
326         return 0;
327 }
328
329 static struct net_device_stats *ethertap_get_stats(struct net_device *dev)
330 {
331         struct net_local *lp = (struct net_local *)dev->priv;
332         return &lp->stats;
333 }
334
335 #ifdef MODULE
336
337 static int unit;
338 MODULE_PARM(unit,"i");
339 MODULE_PARM_DESC(unit,"Ethertap device number");
340
341 static struct net_device dev_ethertap =
342 {
343         " ",
344         0, 0, 0, 0,
345         1, 5,
346         0, 0, 0, NULL, ethertap_probe
347 };
348
349 int init_module(void)
350 {
351         dev_ethertap.base_addr=unit+NETLINK_TAPBASE;
352         sprintf(dev_ethertap.name,"tap%d",unit);
353         if (dev_get(dev_ethertap.name))
354         {
355                 printk(KERN_INFO "%s already loaded.\n", dev_ethertap.name);
356                 return -EBUSY;
357         }
358         if (register_netdev(&dev_ethertap) != 0)
359                 return -EIO;
360         return 0;
361 }
362
363 void cleanup_module(void)
364 {
365         tap_map[dev_ethertap.base_addr]=NULL;
366         unregister_netdev(&dev_ethertap);
367
368         /*
369          *      Free up the private structure.
370          */
371
372         kfree(dev_ethertap.priv);
373         dev_ethertap.priv = NULL;       /* gets re-allocated by ethertap_probe */
374 }
375
376 #endif /* MODULE */
377 MODULE_LICENSE("GPL");
378