* add p cc
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / net / ethertap.c
blob0b5aa3db807be112eb2291bae13fbb61fcd15554
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>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/malloc.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 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
46 static int ethertap_debug = 0;
48 static struct net_device *tap_map[32]; /* Returns the tap device for a given netlink */
51 * Board-specific info in dev->priv.
54 struct net_local
56 struct sock *nl;
57 #ifdef CONFIG_ETHERTAP_MC
58 __u32 groups;
59 #endif
60 struct net_device_stats stats;
64 * To call this a probe is a bit misleading, however for real
65 * hardware it would have to check what was present.
68 int __init ethertap_probe(struct net_device *dev)
70 memcpy(dev->dev_addr, "\xFE\xFD\x00\x00\x00\x00", 6);
71 if (dev->mem_start & 0xf)
72 ethertap_debug = dev->mem_start & 0x7;
75 * Initialize the device structure.
78 dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
79 if (dev->priv == NULL)
80 return -ENOMEM;
81 memset(dev->priv, 0, sizeof(struct net_local));
84 * The tap specific entries in the device structure.
87 dev->open = ethertap_open;
88 dev->hard_start_xmit = ethertap_start_xmit;
89 dev->stop = ethertap_close;
90 dev->get_stats = ethertap_get_stats;
91 #ifdef CONFIG_ETHERTAP_MC
92 dev->set_multicast_list = set_multicast_list;
93 #endif
96 * Setup the generic properties
99 ether_setup(dev);
101 dev->tx_queue_len = 0;
102 dev->flags|=IFF_NOARP;
103 tap_map[dev->base_addr]=dev;
105 return 0;
109 * Open/initialize the board.
112 static int ethertap_open(struct net_device *dev)
114 struct net_local *lp = (struct net_local*)dev->priv;
116 if (ethertap_debug > 2)
117 printk("%s: Doing ethertap_open()...", dev->name);
119 MOD_INC_USE_COUNT;
121 lp->nl = netlink_kernel_create(dev->base_addr, ethertap_rx);
122 if (lp->nl == NULL) {
123 MOD_DEC_USE_COUNT;
124 return -ENOBUFS;
127 dev->start = 1;
128 dev->tbusy = 0;
129 return 0;
132 #ifdef CONFIG_ETHERTAP_MC
133 static unsigned ethertap_mc_hash(__u8 *dest)
135 unsigned idx = 0;
136 idx ^= dest[0];
137 idx ^= dest[1];
138 idx ^= dest[2];
139 idx ^= dest[3];
140 idx ^= dest[4];
141 idx ^= dest[5];
142 return 1U << (idx&0x1F);
145 static void set_multicast_list(struct net_device *dev)
147 unsigned groups = ~0;
148 struct net_local *lp = (struct net_local *)dev->priv;
150 if (!(dev->flags&(IFF_NOARP|IFF_PROMISC|IFF_ALLMULTI))) {
151 struct dev_mc_list *dmi;
153 groups = ethertap_mc_hash(dev->broadcast);
155 for (dmi=dev->mc_list; dmi; dmi=dmi->next) {
156 if (dmi->dmi_addrlen != 6)
157 continue;
158 groups |= ethertap_mc_hash(dmi->dmi_addr);
161 lp->groups = groups;
162 if (lp->nl)
163 lp->nl->protinfo.af_netlink.groups = groups;
165 #endif
168 * We transmit by throwing the packet at netlink. We have to clone
169 * it for 2.0 so that we dev_kfree_skb() the locked original.
172 static int ethertap_start_xmit(struct sk_buff *skb, struct net_device *dev)
174 struct net_local *lp = (struct net_local *)dev->priv;
175 #ifdef CONFIG_ETHERTAP_MC
176 struct ethhdr *eth = (struct ethhdr*)skb->data;
177 #endif
179 if (skb_headroom(skb) < 2) {
180 static int once;
181 struct sk_buff *skb2;
183 if (!once) {
184 once = 1;
185 printk(KERN_DEBUG "%s: not aligned xmit by protocol %04x\n", dev->name, skb->protocol);
188 skb2 = skb_realloc_headroom(skb, 2);
189 dev_kfree_skb(skb);
190 if (skb2 == NULL)
191 return 0;
192 skb = skb2;
194 __skb_push(skb, 2);
196 /* Make the same thing, which loopback does. */
197 if (skb_shared(skb)) {
198 struct sk_buff *skb2 = skb;
199 skb = skb_clone(skb, GFP_ATOMIC); /* Clone the buffer */
200 if (skb==NULL) {
201 dev_kfree_skb(skb2);
202 return 0;
204 dev_kfree_skb(skb2);
206 /* ... but do not orphan it here, netlink does it in any case. */
208 lp->stats.tx_bytes+=skb->len;
209 lp->stats.tx_packets++;
211 #ifndef CONFIG_ETHERTAP_MC
212 netlink_broadcast(lp->nl, skb, 0, ~0, GFP_ATOMIC);
213 #else
214 if (dev->flags&IFF_NOARP) {
215 netlink_broadcast(lp->nl, skb, 0, ~0, GFP_ATOMIC);
216 return 0;
219 if (!(eth->h_dest[0]&1)) {
220 /* Unicast packet */
221 __u32 pid;
222 memcpy(&pid, eth->h_dest+2, 4);
223 netlink_unicast(lp->nl, skb, ntohl(pid), MSG_DONTWAIT);
224 } else
225 netlink_broadcast(lp->nl, skb, 0, ethertap_mc_hash(eth->h_dest), GFP_ATOMIC);
226 #endif
227 return 0;
230 static __inline__ int ethertap_rx_skb(struct sk_buff *skb, struct net_device *dev)
232 struct net_local *lp = (struct net_local *)dev->priv;
233 #ifdef CONFIG_ETHERTAP_MC
234 struct ethhdr *eth = (struct ethhdr*)(skb->data + 2);
235 #endif
236 int len = skb->len;
238 if (len < 16) {
239 printk(KERN_DEBUG "%s : rx len = %d\n", dev->name, len);
240 kfree_skb(skb);
241 return -EINVAL;
243 if (NETLINK_CREDS(skb)->uid) {
244 printk(KERN_INFO "%s : user %d\n", dev->name, NETLINK_CREDS(skb)->uid);
245 kfree_skb(skb);
246 return -EPERM;
249 #ifdef CONFIG_ETHERTAP_MC
250 if (!(dev->flags&(IFF_NOARP|IFF_PROMISC))) {
251 int drop = 0;
253 if (eth->h_dest[0]&1) {
254 if (!(ethertap_mc_hash(eth->h_dest)&lp->groups))
255 drop = 1;
256 } else if (memcmp(eth->h_dest, dev->dev_addr, 6) != 0)
257 drop = 1;
259 if (drop) {
260 if (ethertap_debug > 3)
261 printk(KERN_DEBUG "%s : not for us\n", dev->name);
262 kfree_skb(skb);
263 return -EINVAL;
266 #endif
268 if (skb_shared(skb)) {
269 struct sk_buff *skb2 = skb;
270 skb = skb_clone(skb, GFP_KERNEL); /* Clone the buffer */
271 if (skb==NULL) {
272 kfree_skb(skb2);
273 return -ENOBUFS;
275 kfree_skb(skb2);
276 } else
277 skb_orphan(skb);
279 skb_pull(skb, 2);
280 skb->dev = dev;
281 skb->protocol=eth_type_trans(skb,dev);
282 memset(skb->cb, 0, sizeof(skb->cb));
283 lp->stats.rx_packets++;
284 lp->stats.rx_bytes+=len;
285 netif_rx(skb);
286 return len;
290 * The typical workload of the driver:
291 * Handle the ether interface interrupts.
293 * (In this case handle the packets posted from user space..)
296 static void ethertap_rx(struct sock *sk, int len)
298 struct net_device *dev = tap_map[sk->protocol];
299 struct sk_buff *skb;
301 if (dev==NULL) {
302 printk(KERN_CRIT "ethertap: bad unit!\n");
303 skb_queue_purge(&sk->receive_queue);
304 return;
307 if (ethertap_debug > 3)
308 printk("%s: ethertap_rx()\n", dev->name);
310 while ((skb = skb_dequeue(&sk->receive_queue)) != NULL)
311 ethertap_rx_skb(skb, dev);
314 static int ethertap_close(struct net_device *dev)
316 struct net_local *lp = (struct net_local *)dev->priv;
317 struct sock *sk = lp->nl;
319 if (ethertap_debug > 2)
320 printk("%s: Shutting down.\n", dev->name);
322 dev->tbusy = 1;
323 dev->start = 0;
325 if (sk) {
326 lp->nl = NULL;
327 sock_release(sk->socket);
330 MOD_DEC_USE_COUNT;
331 return 0;
334 static struct net_device_stats *ethertap_get_stats(struct net_device *dev)
336 struct net_local *lp = (struct net_local *)dev->priv;
337 return &lp->stats;
340 #ifdef MODULE
342 static int unit;
343 MODULE_PARM(unit,"i");
345 static char devicename[9] = { 0, };
347 static struct net_device dev_ethertap =
349 devicename,
350 0, 0, 0, 0,
351 1, 5,
352 0, 0, 0, NULL, ethertap_probe
355 int init_module(void)
357 dev_ethertap.base_addr=unit+NETLINK_TAPBASE;
358 sprintf(devicename,"tap%d",unit);
359 if (dev_get(devicename))
361 printk(KERN_INFO "%s already loaded.\n", devicename);
362 return -EBUSY;
364 if (register_netdev(&dev_ethertap) != 0)
365 return -EIO;
366 return 0;
369 void cleanup_module(void)
371 tap_map[dev_ethertap.base_addr]=NULL;
372 unregister_netdev(&dev_ethertap);
375 * Free up the private structure.
378 kfree(dev_ethertap.priv);
379 dev_ethertap.priv = NULL; /* gets re-allocated by ethertap_probe */
382 #endif /* MODULE */