blob: d35d0c1ee7f4d700ac17fd2fda5a653e28d1ae37 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Dikef28169d2007-02-10 01:43:53 -08002 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * James Leu (jleu@mindspring.net).
4 * Copyright (C) 2001 by various other people who didn't put their name here.
5 * Licensed under the GPL.
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include "linux/kernel.h"
9#include "linux/netdevice.h"
10#include "linux/rtnetlink.h"
11#include "linux/skbuff.h"
12#include "linux/socket.h"
13#include "linux/spinlock.h"
14#include "linux/module.h"
15#include "linux/init.h"
16#include "linux/etherdevice.h"
17#include "linux/list.h"
18#include "linux/inetdevice.h"
19#include "linux/ctype.h"
20#include "linux/bootmem.h"
21#include "linux/ethtool.h"
Russell Kingd052d1b2005-10-29 19:07:23 +010022#include "linux/platform_device.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "asm/uaccess.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "kern_util.h"
25#include "net_kern.h"
26#include "net_user.h"
27#include "mconsole_kern.h"
28#include "init.h"
29#include "irq_user.h"
30#include "irq_kern.h"
31
Paolo 'Blaisorblade' Giarrussobf61f502006-09-25 23:33:07 -070032static inline void set_ether_mac(struct net_device *dev, unsigned char *addr)
33{
34 memcpy(dev->dev_addr, addr, ETH_ALEN);
35}
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define DRIVER_NAME "uml-netdev"
38
39static DEFINE_SPINLOCK(opened_lock);
Jeff Dike90107722006-01-06 00:18:54 -080040static LIST_HEAD(opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42static int uml_net_rx(struct net_device *dev)
43{
44 struct uml_net_private *lp = dev->priv;
45 int pkt_len;
46 struct sk_buff *skb;
47
48 /* If we can't allocate memory, try again next round. */
49 skb = dev_alloc_skb(dev->mtu);
50 if (skb == NULL) {
51 lp->stats.rx_dropped++;
52 return 0;
53 }
54
55 skb->dev = dev;
56 skb_put(skb, dev->mtu);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -070057 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 pkt_len = (*lp->read)(lp->fd, &skb, lp);
59
60 if (pkt_len > 0) {
61 skb_trim(skb, pkt_len);
62 skb->protocol = (*lp->protocol)(skb);
63 netif_rx(skb);
64
65 lp->stats.rx_bytes += skb->len;
66 lp->stats.rx_packets++;
67 return pkt_len;
68 }
69
70 kfree_skb(skb);
71 return pkt_len;
72}
73
Peter Zijlstraeff3b632006-12-13 00:33:50 -080074static void uml_dev_close(struct work_struct *work)
Paolo 'Blaisorblade' Giarrusso71c8d4c2006-01-18 17:42:56 -080075{
Peter Zijlstraeff3b632006-12-13 00:33:50 -080076 struct uml_net_private *lp =
77 container_of(work, struct uml_net_private, work);
78 dev_close(lp->dev);
Paolo 'Blaisorblade' Giarrusso71c8d4c2006-01-18 17:42:56 -080079}
80
Al Viro7bea96f2006-10-08 22:49:34 +010081irqreturn_t uml_net_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 struct net_device *dev = dev_id;
84 struct uml_net_private *lp = dev->priv;
85 int err;
86
87 if(!netif_running(dev))
88 return(IRQ_NONE);
89
90 spin_lock(&lp->lock);
91 while((err = uml_net_rx(dev)) > 0) ;
92 if(err < 0) {
Jeff Dikef28169d2007-02-10 01:43:53 -080093 printk(KERN_ERR
94 "Device '%s' read returned %d, shutting it down\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 dev->name, err);
Paolo 'Blaisorblade' Giarrusso71c8d4c2006-01-18 17:42:56 -080096 /* dev_close can't be called in interrupt context, and takes
97 * again lp->lock.
98 * And dev_close() can be safely called multiple times on the
99 * same device, since it tests for (dev->flags & IFF_UP). So
Peter Zijlstraeff3b632006-12-13 00:33:50 -0800100 * there's no harm in delaying the device shutdown.
101 * Furthermore, the workqueue will not re-enqueue an already
102 * enqueued work item. */
103 schedule_work(&lp->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 goto out;
105 }
106 reactivate_fd(lp->fd, UM_ETH_IRQ);
107
Paolo 'Blaisorblade' Giarrusso71c8d4c2006-01-18 17:42:56 -0800108out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 spin_unlock(&lp->lock);
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800110 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113static int uml_net_open(struct net_device *dev)
114{
115 struct uml_net_private *lp = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 int err;
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if(lp->fd >= 0){
119 err = -ENXIO;
120 goto out;
121 }
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 lp->fd = (*lp->open)(&lp->user);
124 if(lp->fd < 0){
125 err = lp->fd;
126 goto out;
127 }
128
129 err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
Thomas Gleixnerbd6aa652006-07-01 19:29:27 -0700130 IRQF_DISABLED | IRQF_SHARED, dev->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if(err != 0){
132 printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 err = -ENETUNREACH;
Jeff Dike14d9ead2006-02-07 12:58:42 -0800134 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136
137 lp->tl.data = (unsigned long) &lp->user;
138 netif_start_queue(dev);
139
140 /* clear buffer - it can happen that the host side of the interface
141 * is full when we get here. In this case, new data is never queued,
142 * SIGIOs never arrive, and the net never works.
143 */
144 while((err = uml_net_rx(dev)) > 0) ;
145
Jeff Dike14d9ead2006-02-07 12:58:42 -0800146 spin_lock(&opened_lock);
147 list_add(&lp->list, &opened);
148 spin_unlock(&opened_lock);
149
150 return 0;
151out_close:
152 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
153 lp->fd = -1;
154out:
Jeff Dike14d9ead2006-02-07 12:58:42 -0800155 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158static int uml_net_close(struct net_device *dev)
159{
160 struct uml_net_private *lp = dev->priv;
Jeff Dikef28169d2007-02-10 01:43:53 -0800161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 free_irq(dev->irq, dev);
165 if(lp->close != NULL)
166 (*lp->close)(lp->fd, &lp->user);
167 lp->fd = -1;
168
Jeff Dike14d9ead2006-02-07 12:58:42 -0800169 spin_lock(&opened_lock);
170 list_del(&lp->list);
171 spin_unlock(&opened_lock);
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return 0;
174}
175
176static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
177{
178 struct uml_net_private *lp = dev->priv;
179 unsigned long flags;
180 int len;
181
182 netif_stop_queue(dev);
183
184 spin_lock_irqsave(&lp->lock, flags);
185
186 len = (*lp->write)(lp->fd, &skb, lp);
187
188 if(len == skb->len) {
189 lp->stats.tx_packets++;
190 lp->stats.tx_bytes += skb->len;
191 dev->trans_start = jiffies;
192 netif_start_queue(dev);
193
194 /* this is normally done in the interrupt when tx finishes */
195 netif_wake_queue(dev);
Jeff Dikef28169d2007-02-10 01:43:53 -0800196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 else if(len == 0){
198 netif_start_queue(dev);
199 lp->stats.tx_dropped++;
200 }
201 else {
202 netif_start_queue(dev);
203 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
204 }
205
206 spin_unlock_irqrestore(&lp->lock, flags);
207
208 dev_kfree_skb(skb);
209
210 return 0;
211}
212
213static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
214{
215 struct uml_net_private *lp = dev->priv;
216 return &lp->stats;
217}
218
219static void uml_net_set_multicast_list(struct net_device *dev)
220{
221 if (dev->flags & IFF_PROMISC) return;
222 else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
223 else dev->flags &= ~IFF_ALLMULTI;
224}
225
226static void uml_net_tx_timeout(struct net_device *dev)
227{
228 dev->trans_start = jiffies;
229 netif_wake_queue(dev);
230}
231
232static int uml_net_set_mac(struct net_device *dev, void *addr)
233{
234 struct uml_net_private *lp = dev->priv;
235 struct sockaddr *hwaddr = addr;
236
Paolo 'Blaisorblade' Giarrusso48af05e2006-09-27 01:50:31 -0700237 spin_lock_irq(&lp->lock);
Paolo 'Blaisorblade' Giarrussobf61f502006-09-25 23:33:07 -0700238 set_ether_mac(dev, hwaddr->sa_data);
Paolo 'Blaisorblade' Giarrusso48af05e2006-09-27 01:50:31 -0700239 spin_unlock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
245{
246 struct uml_net_private *lp = dev->priv;
247 int err = 0;
248
Paolo 'Blaisorblade' Giarrusso48af05e2006-09-27 01:50:31 -0700249 spin_lock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
252 if(new_mtu < 0){
253 err = new_mtu;
254 goto out;
255 }
256
257 dev->mtu = new_mtu;
258
259 out:
Paolo 'Blaisorblade' Giarrusso48af05e2006-09-27 01:50:31 -0700260 spin_unlock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return err;
262}
263
Christoph Hellwig6d387482005-11-07 06:21:21 +0100264static void uml_net_get_drvinfo(struct net_device *dev,
265 struct ethtool_drvinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Christoph Hellwig6d387482005-11-07 06:21:21 +0100267 strcpy(info->driver, DRIVER_NAME);
268 strcpy(info->version, "42");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
Christoph Hellwig6d387482005-11-07 06:21:21 +0100271static struct ethtool_ops uml_net_ethtool_ops = {
272 .get_drvinfo = uml_net_get_drvinfo,
273 .get_link = ethtool_op_get_link,
274};
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276void uml_net_user_timer_expire(unsigned long _conn)
277{
278#ifdef undef
279 struct connection *conn = (struct connection *)_conn;
280
281 dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
282 do_connect(conn);
283#endif
284}
285
Paolo 'Blaisorblade' Giarrussoe0247152007-05-06 14:51:13 -0700286static void setup_etheraddr(char *str, unsigned char *addr, char *name)
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700287{
288 char *end;
289 int i;
290
291 if(str == NULL)
292 goto random;
293
294 for(i=0;i<6;i++){
295 addr[i] = simple_strtoul(str, &end, 16);
296 if((end == str) ||
297 ((*end != ':') && (*end != ',') && (*end != '\0'))){
298 printk(KERN_ERR
299 "setup_etheraddr: failed to parse '%s' "
300 "as an ethernet address\n", str);
301 goto random;
302 }
303 str = end + 1;
304 }
Paolo 'Blaisorblade' Giarrussoe0247152007-05-06 14:51:13 -0700305 if (is_multicast_ether_addr(addr)) {
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700306 printk(KERN_ERR
Paolo 'Blaisorblade' Giarrussoe0247152007-05-06 14:51:13 -0700307 "Attempt to assign a multicast ethernet address to a "
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700308 "device disallowed\n");
309 goto random;
310 }
Paolo 'Blaisorblade' Giarrussoe0247152007-05-06 14:51:13 -0700311 if (!is_valid_ether_addr(addr)) {
312 printk(KERN_ERR
313 "Attempt to assign an invalid ethernet address to a "
314 "device disallowed\n");
315 goto random;
316 }
317 if (!is_local_ether_addr(addr)) {
318 printk(KERN_WARNING
Jeff Dike7d982302007-05-08 00:35:04 -0700319 "Warning: attempt to assign a globally valid ethernet "
320 "address to a device\n");
321 printk(KERN_WARNING "You should better enable the 2nd "
322 "rightmost bit in the first byte of the MAC,\n");
323 printk(KERN_WARNING "i.e. %02x:%02x:%02x:%02x:%02x:%02x\n",
324 addr[0] | 0x02, addr[1], addr[2], addr[3], addr[4],
325 addr[5]);
326 goto random;
Paolo 'Blaisorblade' Giarrussoe0247152007-05-06 14:51:13 -0700327 }
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700328 return;
329
330random:
Paolo 'Blaisorblade' Giarrussoe0247152007-05-06 14:51:13 -0700331 printk(KERN_INFO
332 "Choosing a random ethernet address for device %s\n", name);
Ollie Wildd6c64102006-09-29 15:50:28 -0700333 random_ether_addr(addr);
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700334}
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336static DEFINE_SPINLOCK(devices_lock);
Jeff Dike90107722006-01-06 00:18:54 -0800337static LIST_HEAD(devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Russell King3ae5eae2005-11-09 22:32:44 +0000339static struct platform_driver uml_net_driver = {
340 .driver = {
341 .name = DRIVER_NAME,
342 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343};
344static int driver_registered;
345
Jeff Dike2e3f5252007-05-06 14:51:29 -0700346static void net_device_release(struct device *dev)
347{
348 struct uml_net *device = dev->driver_data;
349 struct net_device *netdev = device->dev;
350 struct uml_net_private *lp = netdev->priv;
351
352 if(lp->remove != NULL)
353 (*lp->remove)(&lp->user);
354 list_del(&device->list);
355 kfree(device);
356 free_netdev(netdev);
357}
358
Jeff Dikef34d9d22007-05-06 14:51:04 -0700359static void eth_configure(int n, void *init, char *mac,
360 struct transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 struct uml_net *device;
363 struct net_device *dev;
364 struct uml_net_private *lp;
Paolo 'Blaisorblade' Giarrussoc74c69b2007-05-06 14:51:15 -0700365 int err, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Paolo 'Blaisorblade' Giarrussoc74c69b2007-05-06 14:51:15 -0700367 size = transport->private_size + sizeof(struct uml_net_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Yan Burman0268bd02006-12-12 19:54:52 +0100369 device = kzalloc(sizeof(*device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (device == NULL) {
Paolo 'Blaisorblade' Giarrusso8c840832007-05-06 14:51:14 -0700371 printk(KERN_ERR "eth_configure failed to allocate struct "
372 "uml_net\n");
Jeff Dikef34d9d22007-05-06 14:51:04 -0700373 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
Paolo 'Blaisorblade' Giarrusso8c840832007-05-06 14:51:14 -0700376 dev = alloc_etherdev(size);
377 if (dev == NULL) {
378 printk(KERN_ERR "eth_configure: failed to allocate struct "
379 "net_device for eth%d\n", n);
380 goto out_free_device;
381 }
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 INIT_LIST_HEAD(&device->list);
384 device->index = n;
385
Paolo 'Blaisorblade' Giarrussoe0247152007-05-06 14:51:13 -0700386 /* If this name ends up conflicting with an existing registered
387 * netdevice, that is OK, register_netdev{,ice}() will notice this
388 * and fail.
389 */
Paolo 'Blaisorblade' Giarrusso8c840832007-05-06 14:51:14 -0700390 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
Paolo 'Blaisorblade' Giarrussoe0247152007-05-06 14:51:13 -0700391
Paolo 'Blaisorblade' Giarrusso8c840832007-05-06 14:51:14 -0700392 setup_etheraddr(mac, device->mac, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 printk(KERN_INFO "Netdevice %d ", n);
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700395 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
396 device->mac[0], device->mac[1],
397 device->mac[2], device->mac[3],
398 device->mac[4], device->mac[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 printk(": ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Paolo 'Blaisorblade' Giarrussoe56a7882006-01-18 17:42:55 -0800401 lp = dev->priv;
402 /* This points to the transport private data. It's still clear, but we
403 * must memset it to 0 *now*. Let's help the drivers. */
404 memset(lp, 0, size);
Peter Zijlstraeff3b632006-12-13 00:33:50 -0800405 INIT_WORK(&lp->work, uml_dev_close);
Paolo 'Blaisorblade' Giarrussoe56a7882006-01-18 17:42:55 -0800406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /* sysfs register */
408 if (!driver_registered) {
Russell King3ae5eae2005-11-09 22:32:44 +0000409 platform_driver_register(&uml_net_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 driver_registered = 1;
411 }
412 device->pdev.id = n;
413 device->pdev.name = DRIVER_NAME;
Jeff Dike2e3f5252007-05-06 14:51:29 -0700414 device->pdev.dev.release = net_device_release;
415 device->pdev.dev.driver_data = device;
Jeff Dikef34d9d22007-05-06 14:51:04 -0700416 if(platform_device_register(&device->pdev))
417 goto out_free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 SET_NETDEV_DEV(dev,&device->pdev.dev);
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 device->dev = dev;
421
Jeff Dikef34d9d22007-05-06 14:51:04 -0700422 /*
423 * These just fill in a data structure, so there's no failure
424 * to be worried about.
425 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 (*transport->kern->init)(dev, init);
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 *lp = ((struct uml_net_private)
429 { .list = LIST_HEAD_INIT(lp->list),
430 .dev = dev,
431 .fd = -1,
432 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 .protocol = transport->kern->protocol,
434 .open = transport->user->open,
435 .close = transport->user->close,
436 .remove = transport->user->remove,
437 .read = transport->kern->read,
438 .write = transport->kern->write,
439 .add_address = transport->user->add_address,
440 .delete_address = transport->user->delete_address,
Paolo 'Blaisorblade' Giarrussoc74c69b2007-05-06 14:51:15 -0700441 .set_mtu = transport->user->set_mtu });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 init_timer(&lp->tl);
444 spin_lock_init(&lp->lock);
445 lp->tl.function = uml_net_user_timer_expire;
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700446 memcpy(lp->mac, device->mac, sizeof(lp->mac));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Jeff Dikef34d9d22007-05-06 14:51:04 -0700448 if ((transport->user->init != NULL) &&
449 ((*transport->user->init)(&lp->user, dev) != 0))
450 goto out_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700452 set_ether_mac(dev, device->mac);
Jeff Dikef34d9d22007-05-06 14:51:04 -0700453 dev->mtu = transport->user->max_packet;
454 dev->open = uml_net_open;
455 dev->hard_start_xmit = uml_net_start_xmit;
456 dev->stop = uml_net_close;
457 dev->get_stats = uml_net_get_stats;
458 dev->set_multicast_list = uml_net_set_multicast_list;
459 dev->tx_timeout = uml_net_tx_timeout;
460 dev->set_mac_address = uml_net_set_mac;
461 dev->change_mtu = uml_net_change_mtu;
462 dev->ethtool_ops = &uml_net_ethtool_ops;
463 dev->watchdog_timeo = (HZ >> 1);
464 dev->irq = UM_ETH_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Jeff Dikef34d9d22007-05-06 14:51:04 -0700466 rtnl_lock();
467 err = register_netdevice(dev);
468 rtnl_unlock();
469 if (err)
470 goto out_undo_user_init;
471
472 spin_lock(&devices_lock);
473 list_add(&device->list, &devices);
474 spin_unlock(&devices_lock);
475
476 return;
477
478out_undo_user_init:
Paolo 'Blaisorblade' Giarrusso8c840832007-05-06 14:51:14 -0700479 if (transport->user->remove != NULL)
Jeff Dikef34d9d22007-05-06 14:51:04 -0700480 (*transport->user->remove)(&lp->user);
481out_unregister:
482 platform_device_unregister(&device->pdev);
Jeff Dike7d982302007-05-08 00:35:04 -0700483 return; /* platform_device_unregister frees dev and device */
Jeff Dikef34d9d22007-05-06 14:51:04 -0700484out_free_netdev:
485 free_netdev(dev);
Paolo 'Blaisorblade' Giarrusso8c840832007-05-06 14:51:14 -0700486out_free_device:
Jeff Dikef34d9d22007-05-06 14:51:04 -0700487 kfree(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
490static struct uml_net *find_device(int n)
491{
492 struct uml_net *device;
493 struct list_head *ele;
494
495 spin_lock(&devices_lock);
496 list_for_each(ele, &devices){
497 device = list_entry(ele, struct uml_net, list);
498 if(device->index == n)
499 goto out;
500 }
501 device = NULL;
502 out:
503 spin_unlock(&devices_lock);
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800504 return device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Jeff Dikef28169d2007-02-10 01:43:53 -0800507static int eth_parse(char *str, int *index_out, char **str_out,
508 char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 char *end;
Jeff Dikef28169d2007-02-10 01:43:53 -0800511 int n, err = -EINVAL;;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 n = simple_strtoul(str, &end, 0);
514 if(end == str){
Jeff Dikef28169d2007-02-10 01:43:53 -0800515 *error_out = "Bad device number";
516 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 str = end;
520 if(*str != '='){
Jeff Dikef28169d2007-02-10 01:43:53 -0800521 *error_out = "Expected '=' after device number";
522 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 str++;
526 if(find_device(n)){
Jeff Dikef28169d2007-02-10 01:43:53 -0800527 *error_out = "Device already configured";
528 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800530
531 *index_out = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 *str_out = str;
Jeff Dikef28169d2007-02-10 01:43:53 -0800533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536struct eth_init {
537 struct list_head list;
538 char *init;
539 int index;
540};
541
Jeff Diked3b7f692007-02-10 01:43:56 -0800542static DEFINE_SPINLOCK(transports_lock);
543static LIST_HEAD(transports);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545/* Filled in during early boot */
Jeff Dikec862fc32007-02-10 01:44:04 -0800546static LIST_HEAD(eth_cmd_line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548static int check_transport(struct transport *transport, char *eth, int n,
549 void **init_out, char **mac_out)
550{
551 int len;
552
553 len = strlen(transport->name);
554 if(strncmp(eth, transport->name, len))
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800555 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 eth += len;
558 if(*eth == ',')
559 eth++;
560 else if(*eth != '\0')
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
564 if(*init_out == NULL)
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800565 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 if(!transport->setup(eth, mac_out, *init_out)){
568 kfree(*init_out);
569 *init_out = NULL;
570 }
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800571 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574void register_transport(struct transport *new)
575{
576 struct list_head *ele, *next;
577 struct eth_init *eth;
578 void *init;
579 char *mac = NULL;
580 int match;
581
Jeff Diked3b7f692007-02-10 01:43:56 -0800582 spin_lock(&transports_lock);
583 BUG_ON(!list_empty(&new->list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 list_add(&new->list, &transports);
Jeff Diked3b7f692007-02-10 01:43:56 -0800585 spin_unlock(&transports_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 list_for_each_safe(ele, next, &eth_cmd_line){
588 eth = list_entry(ele, struct eth_init, list);
589 match = check_transport(new, eth->init, eth->index, &init,
590 &mac);
591 if(!match)
592 continue;
593 else if(init != NULL){
594 eth_configure(eth->index, init, mac, new);
595 kfree(init);
596 }
597 list_del(&eth->list);
598 }
599}
600
601static int eth_setup_common(char *str, int index)
602{
603 struct list_head *ele;
604 struct transport *transport;
605 void *init;
606 char *mac = NULL;
Jeff Dikec862fc32007-02-10 01:44:04 -0800607 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Jeff Dikec862fc32007-02-10 01:44:04 -0800609 spin_lock(&transports_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 list_for_each(ele, &transports){
611 transport = list_entry(ele, struct transport, list);
612 if(!check_transport(transport, str, index, &init, &mac))
613 continue;
614 if(init != NULL){
615 eth_configure(index, init, mac, transport);
616 kfree(init);
617 }
Jeff Dikec862fc32007-02-10 01:44:04 -0800618 found = 1;
619 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
Jeff Dikec862fc32007-02-10 01:44:04 -0800621
622 spin_unlock(&transports_lock);
623 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Jeff Dike97a1fcb2007-07-23 18:43:48 -0700626static int __init eth_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
628 struct eth_init *new;
Jeff Dikef28169d2007-02-10 01:43:53 -0800629 char *error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 int n, err;
631
Jeff Dikef28169d2007-02-10 01:43:53 -0800632 err = eth_parse(str, &n, &str, &error);
633 if(err){
634 printk(KERN_ERR "eth_setup - Couldn't parse '%s' : %s\n",
635 str, error);
Jeff Dike1183dc92006-09-27 01:50:43 -0700636 return 1;
Jeff Dikef28169d2007-02-10 01:43:53 -0800637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Jeff Dike1183dc92006-09-27 01:50:43 -0700639 new = alloc_bootmem(sizeof(*new));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (new == NULL){
641 printk("eth_init : alloc_bootmem failed\n");
Jeff Dike1183dc92006-09-27 01:50:43 -0700642 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
645 INIT_LIST_HEAD(&new->list);
646 new->index = n;
647 new->init = str;
648
649 list_add_tail(&new->list, &eth_cmd_line);
Jeff Dike1183dc92006-09-27 01:50:43 -0700650 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
653__setup("eth", eth_setup);
654__uml_help(eth_setup,
655"eth[0-9]+=<transport>,<options>\n"
656" Configure a network device.\n\n"
657);
658
Jeff Dikef28169d2007-02-10 01:43:53 -0800659static int net_config(char *str, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 int n, err;
662
Jeff Dikef28169d2007-02-10 01:43:53 -0800663 err = eth_parse(str, &n, &str, error_out);
664 if(err)
665 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Jeff Dikef28169d2007-02-10 01:43:53 -0800667 /* This string is broken up and the pieces used by the underlying
668 * driver. So, it is freed only if eth_setup_common fails.
669 */
Jeff Dike970d6e32006-01-06 00:18:48 -0800670 str = kstrdup(str, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if(str == NULL){
Jeff Dikef28169d2007-02-10 01:43:53 -0800672 *error_out = "net_config failed to strdup string";
673 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675 err = !eth_setup_common(str, n);
Jeff Dikef28169d2007-02-10 01:43:53 -0800676 if(err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 kfree(str);
678 return(err);
679}
680
Jeff Dike29d56cf2005-06-25 14:55:25 -0700681static int net_id(char **str, int *start_out, int *end_out)
682{
683 char *end;
684 int n;
685
686 n = simple_strtoul(*str, &end, 0);
687 if((*end != '\0') || (end == *str))
688 return -1;
689
690 *start_out = n;
691 *end_out = n;
692 *str = end;
693 return n;
694}
695
Jeff Dikef28169d2007-02-10 01:43:53 -0800696static int net_remove(int n, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 struct uml_net *device;
699 struct net_device *dev;
700 struct uml_net_private *lp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 device = find_device(n);
703 if(device == NULL)
Jeff Dike29d56cf2005-06-25 14:55:25 -0700704 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 dev = device->dev;
707 lp = dev->priv;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700708 if(lp->fd > 0)
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800709 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 unregister_netdev(dev);
711 platform_device_unregister(&device->pdev);
712
Jeff Dike29d56cf2005-06-25 14:55:25 -0700713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716static struct mc_device net_mc = {
Jeff Dike84f48d42007-02-10 01:44:01 -0800717 .list = LIST_HEAD_INIT(net_mc.list),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 .name = "eth",
719 .config = net_config,
720 .get_config = NULL,
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800721 .id = net_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 .remove = net_remove,
723};
724
725static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
726 void *ptr)
727{
728 struct in_ifaddr *ifa = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 struct net_device *dev = ifa->ifa_dev->dev;
730 struct uml_net_private *lp;
731 void (*proc)(unsigned char *, unsigned char *, void *);
732 unsigned char addr_buf[4], netmask_buf[4];
733
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800734 if(dev->open != uml_net_open)
735 return NOTIFY_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 lp = dev->priv;
738
739 proc = NULL;
740 switch (event){
741 case NETDEV_UP:
742 proc = lp->add_address;
743 break;
744 case NETDEV_DOWN:
745 proc = lp->delete_address;
746 break;
747 }
748 if(proc != NULL){
Bodo Stroesser0e764222005-11-07 00:58:47 -0800749 memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
750 memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 (*proc)(addr_buf, netmask_buf, &lp->user);
752 }
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800753 return NOTIFY_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Jeff Dikec862fc32007-02-10 01:44:04 -0800756/* uml_net_init shouldn't be called twice on two CPUs at the same time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757struct notifier_block uml_inetaddr_notifier = {
758 .notifier_call = uml_inetaddr_event,
759};
760
761static int uml_net_init(void)
762{
763 struct list_head *ele;
Jeff Dikef28169d2007-02-10 01:43:53 -0800764 struct uml_net_private *lp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 struct in_device *ip;
766 struct in_ifaddr *in;
767
768 mconsole_register_dev(&net_mc);
769 register_inetaddr_notifier(&uml_inetaddr_notifier);
770
771 /* Devices may have been opened already, so the uml_inetaddr_notifier
772 * didn't get a chance to run for them. This fakes it so that
773 * addresses which have already been set up get handled properly.
774 */
Jeff Dikec862fc32007-02-10 01:44:04 -0800775 spin_lock(&opened_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 list_for_each(ele, &opened){
777 lp = list_entry(ele, struct uml_net_private, list);
778 ip = lp->dev->ip_ptr;
Jeff Dikec862fc32007-02-10 01:44:04 -0800779 if(ip == NULL)
780 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 in = ip->ifa_list;
782 while(in != NULL){
783 uml_inetaddr_event(NULL, NETDEV_UP, in);
784 in = in->ifa_next;
785 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800786 }
Jeff Dikec862fc32007-02-10 01:44:04 -0800787 spin_unlock(&opened_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Jeff Dikec862fc32007-02-10 01:44:04 -0800789 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
792__initcall(uml_net_init);
793
794static void close_devices(void)
795{
796 struct list_head *ele;
797 struct uml_net_private *lp;
798
Jeff Dikec862fc32007-02-10 01:44:04 -0800799 spin_lock(&opened_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 list_for_each(ele, &opened){
801 lp = list_entry(ele, struct uml_net_private, list);
Jeff Dike8d93c702006-01-06 00:19:06 -0800802 free_irq(lp->dev->irq, lp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if((lp->close != NULL) && (lp->fd >= 0))
804 (*lp->close)(lp->fd, &lp->user);
Jeff Dikec862fc32007-02-10 01:44:04 -0800805 if(lp->remove != NULL)
806 (*lp->remove)(&lp->user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Jeff Dikec862fc32007-02-10 01:44:04 -0800808 spin_unlock(&opened_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
811__uml_exitcall(close_devices);
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
814{
815 if((skb != NULL) && (skb_tailroom(skb) < extra)){
816 struct sk_buff *skb2;
817
818 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
819 dev_kfree_skb(skb);
820 skb = skb2;
821 }
822 if(skb != NULL) skb_put(skb, extra);
823 return(skb);
824}
825
Jeff Dikef28169d2007-02-10 01:43:53 -0800826void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
827 void *),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 void *arg)
829{
830 struct net_device *dev = d;
831 struct in_device *ip = dev->ip_ptr;
832 struct in_ifaddr *in;
833 unsigned char address[4], netmask[4];
834
835 if(ip == NULL) return;
836 in = ip->ifa_list;
837 while(in != NULL){
Bodo Stroesser0e764222005-11-07 00:58:47 -0800838 memcpy(address, &in->ifa_address, sizeof(address));
839 memcpy(netmask, &in->ifa_mask, sizeof(netmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 (*cb)(address, netmask, arg);
841 in = in->ifa_next;
842 }
843}
844
845int dev_netmask(void *d, void *m)
846{
847 struct net_device *dev = d;
848 struct in_device *ip = dev->ip_ptr;
849 struct in_ifaddr *in;
Al Viroa144ea42006-09-28 18:00:55 -0700850 __be32 *mask_out = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Jeff Dikef28169d2007-02-10 01:43:53 -0800852 if(ip == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return(1);
854
855 in = ip->ifa_list;
Jeff Dikef28169d2007-02-10 01:43:53 -0800856 if(in == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return(1);
858
859 *mask_out = in->ifa_mask;
860 return(0);
861}
862
863void *get_output_buffer(int *len_out)
864{
865 void *ret;
866
867 ret = (void *) __get_free_pages(GFP_KERNEL, 0);
868 if(ret) *len_out = PAGE_SIZE;
869 else *len_out = 0;
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800870 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
873void free_output_buffer(void *buffer)
874{
875 free_pages((unsigned long) buffer, 0);
876}
877
Jeff Dikef28169d2007-02-10 01:43:53 -0800878int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 char **gate_addr)
880{
881 char *remain;
882
883 remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
884 if(remain != NULL){
885 printk("tap_setup_common - Extra garbage on specification : "
886 "'%s'\n", remain);
887 return(1);
888 }
889
890 return(0);
891}
892
893unsigned short eth_protocol(struct sk_buff *skb)
894{
895 return(eth_type_trans(skb, skb->dev));
896}