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
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>
30 #include <linux/netlink.h>
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
);
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.
60 #ifdef CONFIG_ETHERTAP_MC
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
;
75 dev
= alloc_etherdev(sizeof(struct net_local
));
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
;
103 dev
->tx_queue_len
= 0;
104 dev
->flags
|=IFF_NOARP
;
106 err
= register_netdev(dev
);
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
);
133 netif_start_queue(dev
);
137 #ifdef CONFIG_ETHERTAP_MC
138 static unsigned ethertap_mc_hash(__u8
*dest
)
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)
163 groups
|= ethertap_mc_hash(dmi
->dmi_addr
);
168 lp
->nl
->protinfo
.af_netlink
.groups
= groups
;
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
;
184 if (skb_headroom(skb
) < 2) {
186 struct sk_buff
*skb2
;
190 printk(KERN_DEBUG
"%s: not aligned xmit by protocol %04x\n", dev
->name
, skb
->protocol
);
193 skb2
= skb_realloc_headroom(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 */
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
);
219 if (dev
->flags
&IFF_NOARP
) {
220 netlink_broadcast(lp
->nl
, skb
, 0, ~0, GFP_ATOMIC
);
224 if (!(eth
->h_dest
[0]&1)) {
227 memcpy(&pid
, eth
->h_dest
+2, 4);
228 netlink_unicast(lp
->nl
, skb
, ntohl(pid
), MSG_DONTWAIT
);
230 netlink_broadcast(lp
->nl
, skb
, 0, ethertap_mc_hash(eth
->h_dest
), GFP_ATOMIC
);
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);
244 printk(KERN_DEBUG
"%s : rx len = %d\n", dev
->name
, len
);
248 if (NETLINK_CREDS(skb
)->uid
) {
249 printk(KERN_INFO
"%s : user %d\n", dev
->name
, NETLINK_CREDS(skb
)->uid
);
254 #ifdef CONFIG_ETHERTAP_MC
255 if (!(dev
->flags
&(IFF_NOARP
|IFF_PROMISC
))) {
258 if (eth
->h_dest
[0]&1) {
259 if (!(ethertap_mc_hash(eth
->h_dest
)&lp
->groups
))
261 } else if (memcmp(eth
->h_dest
, dev
->dev_addr
, 6) != 0)
265 if (ethertap_debug
> 3)
266 printk(KERN_DEBUG
"%s : not for us\n", dev
->name
);
273 if (skb_shared(skb
)) {
274 struct sk_buff
*skb2
= skb
;
275 skb
= skb_clone(skb
, GFP_KERNEL
); /* Clone the buffer */
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
;
291 dev
->last_rx
= jiffies
;
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
;
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
);
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
);
333 sock_release(sk
->sk_socket
);
339 static struct net_device_stats
*ethertap_get_stats(struct net_device
*dev
)
341 struct net_local
*lp
= netdev_priv(dev
);
346 int __init
ethertap_init(void)
350 /* netlink can only hande 16 entries unless modified */
351 if (max_taps
> MAX_LINKS
- NETLINK_TAPBASE
)
354 tap_map
= kmalloc(sizeof(struct net_device
*)*max_taps
, GFP_KERNEL
);
358 for (i
= 0; i
< max_taps
; i
++) {
359 err
= ethertap_probe(i
);
362 unregister_netdev(tap_map
[i
]);
363 free_netdev(tap_map
[i
]);
372 module_init(ethertap_init
);
374 void __exit
ethertap_cleanup(void)
378 for (i
= 0; i
< max_taps
; i
++) {
379 struct net_device
*dev
= tap_map
[i
];
382 unregister_netdev(dev
);
388 module_exit(ethertap_cleanup
);
390 MODULE_LICENSE("GPL");