MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / net / ethertap.c
blobef47c5c9aff86d8b1f8332e9cd124590d4ab5c85
1 /*
2 * Ethertap: A network device for bouncing packets via user space
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.
10 * As this is an ethernet device you can use it for appletalk, IPX etc
11 * even for building bridging tunnels.
14 #include <linux/config.h>
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
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>
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>
29 #include <net/sock.h>
30 #include <linux/netlink.h>
33 * Index to functions.
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
45 static int ethertap_debug;
47 static int max_taps = 1;
48 module_param(max_taps, int, 0);
49 MODULE_PARM_DESC(max_taps,"Max number of ethernet tap devices");
51 static struct net_device **tap_map; /* Returns the tap device for a given netlink */
54 * Board-specific info in dev->priv.
57 struct net_local
59 struct sock *nl;
60 #ifdef CONFIG_ETHERTAP_MC
61 __u32 groups;
62 #endif
63 struct net_device_stats stats;
67 * To call this a probe is a bit misleading, however for real
68 * hardware it would have to check what was present.
70 static int __init ethertap_probe(int unit)
72 struct net_device *dev;
73 int err = -ENOMEM;
75 dev = alloc_etherdev(sizeof(struct net_local));
77 if (!dev)
78 goto out;
80 SET_MODULE_OWNER(dev);
82 sprintf(dev->name, "tap%d", unit);
83 dev->base_addr = unit + NETLINK_TAPBASE;
85 netdev_boot_setup_check(dev);
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;
92 * The tap specific entries in the device structure.
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
103 dev->tx_queue_len = 0;
104 dev->flags|=IFF_NOARP;
106 err = register_netdev(dev);
107 if (err)
108 goto out_free;
110 tap_map[unit]=dev;
111 return 0;
112 out_free:
113 free_netdev(dev);
114 out:
115 return err;
119 * Open/initialize the board.
122 static int ethertap_open(struct net_device *dev)
124 struct net_local *lp = netdev_priv(dev);
126 if (ethertap_debug > 2)
127 printk(KERN_DEBUG "%s: Doing ethertap_open()...", dev->name);
129 lp->nl = netlink_kernel_create(dev->base_addr, ethertap_rx);
130 if (lp->nl == NULL)
131 return -ENOBUFS;
133 netif_start_queue(dev);
134 return 0;
137 #ifdef CONFIG_ETHERTAP_MC
138 static unsigned ethertap_mc_hash(__u8 *dest)
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);
150 static void set_multicast_list(struct net_device *dev)
152 unsigned groups = ~0;
153 struct net_local *lp = netdev_priv(dev);
155 if (!(dev->flags&(IFF_NOARP|IFF_PROMISC|IFF_ALLMULTI))) {
156 struct dev_mc_list *dmi;
158 groups = ethertap_mc_hash(dev->broadcast);
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);
166 lp->groups = groups;
167 if (lp->nl)
168 lp->nl->protinfo.af_netlink.groups = groups;
170 #endif
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.
177 static int ethertap_start_xmit(struct sk_buff *skb, struct net_device *dev)
179 struct net_local *lp = netdev_priv(dev);
180 #ifdef CONFIG_ETHERTAP_MC
181 struct ethhdr *eth = (struct ethhdr*)skb->data;
182 #endif
184 if (skb_headroom(skb) < 2) {
185 static int once;
186 struct sk_buff *skb2;
188 if (!once) {
189 once = 1;
190 printk(KERN_DEBUG "%s: not aligned xmit by protocol %04x\n", dev->name, skb->protocol);
193 skb2 = skb_realloc_headroom(skb, 2);
194 dev_kfree_skb(skb);
195 if (skb2 == NULL)
196 return 0;
197 skb = skb2;
199 __skb_push(skb, 2);
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;
209 dev_kfree_skb(skb2);
211 /* ... but do not orphan it here, netlink does it in any case. */
213 lp->stats.tx_bytes+=skb->len;
214 lp->stats.tx_packets++;
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;
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;
235 static __inline__ int ethertap_rx_skb(struct sk_buff *skb, struct net_device *dev)
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;
243 if (len < 16) {
244 printk(KERN_DEBUG "%s : rx len = %d\n", dev->name, len);
245 kfree_skb(skb);
246 return -EINVAL;
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;
254 #ifdef CONFIG_ETHERTAP_MC
255 if (!(dev->flags&(IFF_NOARP|IFF_PROMISC))) {
256 int drop = 0;
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;
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;
271 #endif
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;
280 kfree_skb(skb2);
281 } else
282 skb_orphan(skb);
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;
296 * The typical workload of the driver:
297 * Handle the ether interface interrupts.
299 * (In this case handle the packets posted from user space..)
302 static void ethertap_rx(struct sock *sk, int len)
304 unsigned unit = sk->sk_protocol - NETLINK_TAPBASE;
305 struct net_device *dev;
306 struct sk_buff *skb;
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;
314 if (ethertap_debug > 3)
315 printk(KERN_DEBUG "%s: ethertap_rx()\n", dev->name);
317 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL)
318 ethertap_rx_skb(skb, dev);
321 static int ethertap_close(struct net_device *dev)
323 struct net_local *lp = netdev_priv(dev);
324 struct sock *sk = lp->nl;
326 if (ethertap_debug > 2)
327 printk(KERN_DEBUG "%s: Shutting down.\n", dev->name);
329 netif_stop_queue(dev);
331 if (sk) {
332 lp->nl = NULL;
333 sock_release(sk->sk_socket);
336 return 0;
339 static struct net_device_stats *ethertap_get_stats(struct net_device *dev)
341 struct net_local *lp = netdev_priv(dev);
342 return &lp->stats;
346 int __init ethertap_init(void)
348 int i, err = 0;
350 /* netlink can only hande 16 entries unless modified */
351 if (max_taps > MAX_LINKS - NETLINK_TAPBASE)
352 return -E2BIG;
354 tap_map = kmalloc(sizeof(struct net_device *)*max_taps, GFP_KERNEL);
355 if (!tap_map)
356 return -ENOMEM;
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]);
365 break;
368 if (err)
369 kfree(tap_map);
370 return err;
372 module_init(ethertap_init);
374 void __exit ethertap_cleanup(void)
376 int i;
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);
386 kfree(tap_map);
388 module_exit(ethertap_cleanup);
390 MODULE_LICENSE("GPL");