blob: ac746fb5d10f897f751b8249f48020ac3e2ce943 [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"
24#include "user_util.h"
25#include "kern_util.h"
26#include "net_kern.h"
27#include "net_user.h"
28#include "mconsole_kern.h"
29#include "init.h"
30#include "irq_user.h"
31#include "irq_kern.h"
32
Paolo 'Blaisorblade' Giarrussobf61f502006-09-25 23:33:07 -070033static inline void set_ether_mac(struct net_device *dev, unsigned char *addr)
34{
35 memcpy(dev->dev_addr, addr, ETH_ALEN);
36}
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define DRIVER_NAME "uml-netdev"
39
40static DEFINE_SPINLOCK(opened_lock);
Jeff Dike90107722006-01-06 00:18:54 -080041static LIST_HEAD(opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43static int uml_net_rx(struct net_device *dev)
44{
45 struct uml_net_private *lp = dev->priv;
46 int pkt_len;
47 struct sk_buff *skb;
48
49 /* If we can't allocate memory, try again next round. */
50 skb = dev_alloc_skb(dev->mtu);
51 if (skb == NULL) {
52 lp->stats.rx_dropped++;
53 return 0;
54 }
55
56 skb->dev = dev;
57 skb_put(skb, dev->mtu);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -070058 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 pkt_len = (*lp->read)(lp->fd, &skb, lp);
60
61 if (pkt_len > 0) {
62 skb_trim(skb, pkt_len);
63 skb->protocol = (*lp->protocol)(skb);
64 netif_rx(skb);
65
66 lp->stats.rx_bytes += skb->len;
67 lp->stats.rx_packets++;
68 return pkt_len;
69 }
70
71 kfree_skb(skb);
72 return pkt_len;
73}
74
Peter Zijlstraeff3b632006-12-13 00:33:50 -080075static void uml_dev_close(struct work_struct *work)
Paolo 'Blaisorblade' Giarrusso71c8d4c2006-01-18 17:42:56 -080076{
Peter Zijlstraeff3b632006-12-13 00:33:50 -080077 struct uml_net_private *lp =
78 container_of(work, struct uml_net_private, work);
79 dev_close(lp->dev);
Paolo 'Blaisorblade' Giarrusso71c8d4c2006-01-18 17:42:56 -080080}
81
Al Viro7bea96f2006-10-08 22:49:34 +010082irqreturn_t uml_net_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 struct net_device *dev = dev_id;
85 struct uml_net_private *lp = dev->priv;
86 int err;
87
88 if(!netif_running(dev))
89 return(IRQ_NONE);
90
91 spin_lock(&lp->lock);
92 while((err = uml_net_rx(dev)) > 0) ;
93 if(err < 0) {
Jeff Dikef28169d2007-02-10 01:43:53 -080094 printk(KERN_ERR
95 "Device '%s' read returned %d, shutting it down\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 dev->name, err);
Paolo 'Blaisorblade' Giarrusso71c8d4c2006-01-18 17:42:56 -080097 /* dev_close can't be called in interrupt context, and takes
98 * again lp->lock.
99 * And dev_close() can be safely called multiple times on the
100 * same device, since it tests for (dev->flags & IFF_UP). So
Peter Zijlstraeff3b632006-12-13 00:33:50 -0800101 * there's no harm in delaying the device shutdown.
102 * Furthermore, the workqueue will not re-enqueue an already
103 * enqueued work item. */
104 schedule_work(&lp->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 goto out;
106 }
107 reactivate_fd(lp->fd, UM_ETH_IRQ);
108
Paolo 'Blaisorblade' Giarrusso71c8d4c2006-01-18 17:42:56 -0800109out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 spin_unlock(&lp->lock);
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800111 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114static int uml_net_open(struct net_device *dev)
115{
116 struct uml_net_private *lp = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 int err;
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if(lp->fd >= 0){
120 err = -ENXIO;
121 goto out;
122 }
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 lp->fd = (*lp->open)(&lp->user);
125 if(lp->fd < 0){
126 err = lp->fd;
127 goto out;
128 }
129
130 err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
Thomas Gleixnerbd6aa652006-07-01 19:29:27 -0700131 IRQF_DISABLED | IRQF_SHARED, dev->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if(err != 0){
133 printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 err = -ENETUNREACH;
Jeff Dike14d9ead2006-02-07 12:58:42 -0800135 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137
138 lp->tl.data = (unsigned long) &lp->user;
139 netif_start_queue(dev);
140
141 /* clear buffer - it can happen that the host side of the interface
142 * is full when we get here. In this case, new data is never queued,
143 * SIGIOs never arrive, and the net never works.
144 */
145 while((err = uml_net_rx(dev)) > 0) ;
146
Jeff Dike14d9ead2006-02-07 12:58:42 -0800147 spin_lock(&opened_lock);
148 list_add(&lp->list, &opened);
149 spin_unlock(&opened_lock);
150
151 return 0;
152out_close:
153 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
154 lp->fd = -1;
155out:
Jeff Dike14d9ead2006-02-07 12:58:42 -0800156 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159static int uml_net_close(struct net_device *dev)
160{
161 struct uml_net_private *lp = dev->priv;
Jeff Dikef28169d2007-02-10 01:43:53 -0800162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 free_irq(dev->irq, dev);
166 if(lp->close != NULL)
167 (*lp->close)(lp->fd, &lp->user);
168 lp->fd = -1;
169
Jeff Dike14d9ead2006-02-07 12:58:42 -0800170 spin_lock(&opened_lock);
171 list_del(&lp->list);
172 spin_unlock(&opened_lock);
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return 0;
175}
176
177static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
178{
179 struct uml_net_private *lp = dev->priv;
180 unsigned long flags;
181 int len;
182
183 netif_stop_queue(dev);
184
185 spin_lock_irqsave(&lp->lock, flags);
186
187 len = (*lp->write)(lp->fd, &skb, lp);
188
189 if(len == skb->len) {
190 lp->stats.tx_packets++;
191 lp->stats.tx_bytes += skb->len;
192 dev->trans_start = jiffies;
193 netif_start_queue(dev);
194
195 /* this is normally done in the interrupt when tx finishes */
196 netif_wake_queue(dev);
Jeff Dikef28169d2007-02-10 01:43:53 -0800197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 else if(len == 0){
199 netif_start_queue(dev);
200 lp->stats.tx_dropped++;
201 }
202 else {
203 netif_start_queue(dev);
204 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
205 }
206
207 spin_unlock_irqrestore(&lp->lock, flags);
208
209 dev_kfree_skb(skb);
210
211 return 0;
212}
213
214static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
215{
216 struct uml_net_private *lp = dev->priv;
217 return &lp->stats;
218}
219
220static void uml_net_set_multicast_list(struct net_device *dev)
221{
222 if (dev->flags & IFF_PROMISC) return;
223 else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
224 else dev->flags &= ~IFF_ALLMULTI;
225}
226
227static void uml_net_tx_timeout(struct net_device *dev)
228{
229 dev->trans_start = jiffies;
230 netif_wake_queue(dev);
231}
232
233static int uml_net_set_mac(struct net_device *dev, void *addr)
234{
235 struct uml_net_private *lp = dev->priv;
236 struct sockaddr *hwaddr = addr;
237
Paolo 'Blaisorblade' Giarrusso48af05e2006-09-27 01:50:31 -0700238 spin_lock_irq(&lp->lock);
Paolo 'Blaisorblade' Giarrussobf61f502006-09-25 23:33:07 -0700239 set_ether_mac(dev, hwaddr->sa_data);
Paolo 'Blaisorblade' Giarrusso48af05e2006-09-27 01:50:31 -0700240 spin_unlock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
246{
247 struct uml_net_private *lp = dev->priv;
248 int err = 0;
249
Paolo 'Blaisorblade' Giarrusso48af05e2006-09-27 01:50:31 -0700250 spin_lock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
253 if(new_mtu < 0){
254 err = new_mtu;
255 goto out;
256 }
257
258 dev->mtu = new_mtu;
259
260 out:
Paolo 'Blaisorblade' Giarrusso48af05e2006-09-27 01:50:31 -0700261 spin_unlock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return err;
263}
264
Christoph Hellwig6d387482005-11-07 06:21:21 +0100265static void uml_net_get_drvinfo(struct net_device *dev,
266 struct ethtool_drvinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Christoph Hellwig6d387482005-11-07 06:21:21 +0100268 strcpy(info->driver, DRIVER_NAME);
269 strcpy(info->version, "42");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Christoph Hellwig6d387482005-11-07 06:21:21 +0100272static struct ethtool_ops uml_net_ethtool_ops = {
273 .get_drvinfo = uml_net_get_drvinfo,
274 .get_link = ethtool_op_get_link,
275};
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277void uml_net_user_timer_expire(unsigned long _conn)
278{
279#ifdef undef
280 struct connection *conn = (struct connection *)_conn;
281
282 dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
283 do_connect(conn);
284#endif
285}
286
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700287static void setup_etheraddr(char *str, unsigned char *addr)
288{
289 char *end;
290 int i;
291
292 if(str == NULL)
293 goto random;
294
295 for(i=0;i<6;i++){
296 addr[i] = simple_strtoul(str, &end, 16);
297 if((end == str) ||
298 ((*end != ':') && (*end != ',') && (*end != '\0'))){
299 printk(KERN_ERR
300 "setup_etheraddr: failed to parse '%s' "
301 "as an ethernet address\n", str);
302 goto random;
303 }
304 str = end + 1;
305 }
306 if(addr[0] & 1){
307 printk(KERN_ERR
308 "Attempt to assign a broadcast ethernet address to a "
309 "device disallowed\n");
310 goto random;
311 }
312 return;
313
314random:
Ollie Wildd6c64102006-09-29 15:50:28 -0700315 random_ether_addr(addr);
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318static DEFINE_SPINLOCK(devices_lock);
Jeff Dike90107722006-01-06 00:18:54 -0800319static LIST_HEAD(devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Russell King3ae5eae2005-11-09 22:32:44 +0000321static struct platform_driver uml_net_driver = {
322 .driver = {
323 .name = DRIVER_NAME,
324 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325};
326static int driver_registered;
327
Jeff Dikef34d9d22007-05-06 14:51:04 -0700328static void eth_configure(int n, void *init, char *mac,
329 struct transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 struct uml_net *device;
332 struct net_device *dev;
333 struct uml_net_private *lp;
334 int save, err, size;
335
Jeff Dikef28169d2007-02-10 01:43:53 -0800336 size = transport->private_size + sizeof(struct uml_net_private) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 sizeof(((struct uml_net_private *) 0)->user);
338
Yan Burman0268bd02006-12-12 19:54:52 +0100339 device = kzalloc(sizeof(*device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (device == NULL) {
341 printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
Jeff Dikef34d9d22007-05-06 14:51:04 -0700342 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 INIT_LIST_HEAD(&device->list);
346 device->index = n;
347
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700348 setup_etheraddr(mac, device->mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 printk(KERN_INFO "Netdevice %d ", n);
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700351 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
352 device->mac[0], device->mac[1],
353 device->mac[2], device->mac[3],
354 device->mac[4], device->mac[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 printk(": ");
356 dev = alloc_etherdev(size);
357 if (dev == NULL) {
358 printk(KERN_ERR "eth_configure: failed to allocate device\n");
Jeff Dikef34d9d22007-05-06 14:51:04 -0700359 goto out_free_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361
Paolo 'Blaisorblade' Giarrussoe56a7882006-01-18 17:42:55 -0800362 lp = dev->priv;
363 /* This points to the transport private data. It's still clear, but we
364 * must memset it to 0 *now*. Let's help the drivers. */
365 memset(lp, 0, size);
Peter Zijlstraeff3b632006-12-13 00:33:50 -0800366 INIT_WORK(&lp->work, uml_dev_close);
Paolo 'Blaisorblade' Giarrussoe56a7882006-01-18 17:42:55 -0800367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 /* sysfs register */
369 if (!driver_registered) {
Russell King3ae5eae2005-11-09 22:32:44 +0000370 platform_driver_register(&uml_net_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 driver_registered = 1;
372 }
373 device->pdev.id = n;
374 device->pdev.name = DRIVER_NAME;
Jeff Dikef34d9d22007-05-06 14:51:04 -0700375 if(platform_device_register(&device->pdev))
376 goto out_free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 SET_NETDEV_DEV(dev,&device->pdev.dev);
378
379 /* If this name ends up conflicting with an existing registered
380 * netdevice, that is OK, register_netdev{,ice}() will notice this
381 * and fail.
382 */
383 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
384 device->dev = dev;
385
Jeff Dikef34d9d22007-05-06 14:51:04 -0700386 /*
387 * These just fill in a data structure, so there's no failure
388 * to be worried about.
389 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 (*transport->kern->init)(dev, init);
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /* lp.user is the first four bytes of the transport data, which
393 * has already been initialized. This structure assignment will
394 * overwrite that, so we make sure that .user gets overwritten with
395 * what it already has.
396 */
397 save = lp->user[0];
398 *lp = ((struct uml_net_private)
399 { .list = LIST_HEAD_INIT(lp->list),
400 .dev = dev,
401 .fd = -1,
402 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 .protocol = transport->kern->protocol,
404 .open = transport->user->open,
405 .close = transport->user->close,
406 .remove = transport->user->remove,
407 .read = transport->kern->read,
408 .write = transport->kern->write,
409 .add_address = transport->user->add_address,
410 .delete_address = transport->user->delete_address,
411 .set_mtu = transport->user->set_mtu,
412 .user = { save } });
413
414 init_timer(&lp->tl);
415 spin_lock_init(&lp->lock);
416 lp->tl.function = uml_net_user_timer_expire;
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700417 memcpy(lp->mac, device->mac, sizeof(lp->mac));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Jeff Dikef34d9d22007-05-06 14:51:04 -0700419 if ((transport->user->init != NULL) &&
420 ((*transport->user->init)(&lp->user, dev) != 0))
421 goto out_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700423 set_ether_mac(dev, device->mac);
Jeff Dikef34d9d22007-05-06 14:51:04 -0700424 dev->mtu = transport->user->max_packet;
425 dev->open = uml_net_open;
426 dev->hard_start_xmit = uml_net_start_xmit;
427 dev->stop = uml_net_close;
428 dev->get_stats = uml_net_get_stats;
429 dev->set_multicast_list = uml_net_set_multicast_list;
430 dev->tx_timeout = uml_net_tx_timeout;
431 dev->set_mac_address = uml_net_set_mac;
432 dev->change_mtu = uml_net_change_mtu;
433 dev->ethtool_ops = &uml_net_ethtool_ops;
434 dev->watchdog_timeo = (HZ >> 1);
435 dev->irq = UM_ETH_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Jeff Dikef34d9d22007-05-06 14:51:04 -0700437 rtnl_lock();
438 err = register_netdevice(dev);
439 rtnl_unlock();
440 if (err)
441 goto out_undo_user_init;
442
443 spin_lock(&devices_lock);
444 list_add(&device->list, &devices);
445 spin_unlock(&devices_lock);
446
447 return;
448
449out_undo_user_init:
450 if (transport->user->init != NULL)
451 (*transport->user->remove)(&lp->user);
452out_unregister:
453 platform_device_unregister(&device->pdev);
454out_free_netdev:
455 free_netdev(dev);
456out_free_device: ;
457 kfree(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460static struct uml_net *find_device(int n)
461{
462 struct uml_net *device;
463 struct list_head *ele;
464
465 spin_lock(&devices_lock);
466 list_for_each(ele, &devices){
467 device = list_entry(ele, struct uml_net, list);
468 if(device->index == n)
469 goto out;
470 }
471 device = NULL;
472 out:
473 spin_unlock(&devices_lock);
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800474 return device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Jeff Dikef28169d2007-02-10 01:43:53 -0800477static int eth_parse(char *str, int *index_out, char **str_out,
478 char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 char *end;
Jeff Dikef28169d2007-02-10 01:43:53 -0800481 int n, err = -EINVAL;;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 n = simple_strtoul(str, &end, 0);
484 if(end == str){
Jeff Dikef28169d2007-02-10 01:43:53 -0800485 *error_out = "Bad device number";
486 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 str = end;
490 if(*str != '='){
Jeff Dikef28169d2007-02-10 01:43:53 -0800491 *error_out = "Expected '=' after device number";
492 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 str++;
496 if(find_device(n)){
Jeff Dikef28169d2007-02-10 01:43:53 -0800497 *error_out = "Device already configured";
498 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800500
501 *index_out = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 *str_out = str;
Jeff Dikef28169d2007-02-10 01:43:53 -0800503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
506struct eth_init {
507 struct list_head list;
508 char *init;
509 int index;
510};
511
Jeff Diked3b7f692007-02-10 01:43:56 -0800512static DEFINE_SPINLOCK(transports_lock);
513static LIST_HEAD(transports);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515/* Filled in during early boot */
Jeff Dikec862fc32007-02-10 01:44:04 -0800516static LIST_HEAD(eth_cmd_line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518static int check_transport(struct transport *transport, char *eth, int n,
519 void **init_out, char **mac_out)
520{
521 int len;
522
523 len = strlen(transport->name);
524 if(strncmp(eth, transport->name, len))
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 eth += len;
528 if(*eth == ',')
529 eth++;
530 else if(*eth != '\0')
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
534 if(*init_out == NULL)
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800535 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 if(!transport->setup(eth, mac_out, *init_out)){
538 kfree(*init_out);
539 *init_out = NULL;
540 }
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800541 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544void register_transport(struct transport *new)
545{
546 struct list_head *ele, *next;
547 struct eth_init *eth;
548 void *init;
549 char *mac = NULL;
550 int match;
551
Jeff Diked3b7f692007-02-10 01:43:56 -0800552 spin_lock(&transports_lock);
553 BUG_ON(!list_empty(&new->list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 list_add(&new->list, &transports);
Jeff Diked3b7f692007-02-10 01:43:56 -0800555 spin_unlock(&transports_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 list_for_each_safe(ele, next, &eth_cmd_line){
558 eth = list_entry(ele, struct eth_init, list);
559 match = check_transport(new, eth->init, eth->index, &init,
560 &mac);
561 if(!match)
562 continue;
563 else if(init != NULL){
564 eth_configure(eth->index, init, mac, new);
565 kfree(init);
566 }
567 list_del(&eth->list);
568 }
569}
570
571static int eth_setup_common(char *str, int index)
572{
573 struct list_head *ele;
574 struct transport *transport;
575 void *init;
576 char *mac = NULL;
Jeff Dikec862fc32007-02-10 01:44:04 -0800577 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Jeff Dikec862fc32007-02-10 01:44:04 -0800579 spin_lock(&transports_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 list_for_each(ele, &transports){
581 transport = list_entry(ele, struct transport, list);
582 if(!check_transport(transport, str, index, &init, &mac))
583 continue;
584 if(init != NULL){
585 eth_configure(index, init, mac, transport);
586 kfree(init);
587 }
Jeff Dikec862fc32007-02-10 01:44:04 -0800588 found = 1;
589 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Jeff Dikec862fc32007-02-10 01:44:04 -0800591
592 spin_unlock(&transports_lock);
593 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
596static int eth_setup(char *str)
597{
598 struct eth_init *new;
Jeff Dikef28169d2007-02-10 01:43:53 -0800599 char *error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 int n, err;
601
Jeff Dikef28169d2007-02-10 01:43:53 -0800602 err = eth_parse(str, &n, &str, &error);
603 if(err){
604 printk(KERN_ERR "eth_setup - Couldn't parse '%s' : %s\n",
605 str, error);
Jeff Dike1183dc92006-09-27 01:50:43 -0700606 return 1;
Jeff Dikef28169d2007-02-10 01:43:53 -0800607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Jeff Dike1183dc92006-09-27 01:50:43 -0700609 new = alloc_bootmem(sizeof(*new));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (new == NULL){
611 printk("eth_init : alloc_bootmem failed\n");
Jeff Dike1183dc92006-09-27 01:50:43 -0700612 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
615 INIT_LIST_HEAD(&new->list);
616 new->index = n;
617 new->init = str;
618
619 list_add_tail(&new->list, &eth_cmd_line);
Jeff Dike1183dc92006-09-27 01:50:43 -0700620 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623__setup("eth", eth_setup);
624__uml_help(eth_setup,
625"eth[0-9]+=<transport>,<options>\n"
626" Configure a network device.\n\n"
627);
628
Jeff Dikef28169d2007-02-10 01:43:53 -0800629static int net_config(char *str, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 int n, err;
632
Jeff Dikef28169d2007-02-10 01:43:53 -0800633 err = eth_parse(str, &n, &str, error_out);
634 if(err)
635 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Jeff Dikef28169d2007-02-10 01:43:53 -0800637 /* This string is broken up and the pieces used by the underlying
638 * driver. So, it is freed only if eth_setup_common fails.
639 */
Jeff Dike970d6e32006-01-06 00:18:48 -0800640 str = kstrdup(str, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if(str == NULL){
Jeff Dikef28169d2007-02-10 01:43:53 -0800642 *error_out = "net_config failed to strdup string";
643 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645 err = !eth_setup_common(str, n);
Jeff Dikef28169d2007-02-10 01:43:53 -0800646 if(err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 kfree(str);
648 return(err);
649}
650
Jeff Dike29d56cf2005-06-25 14:55:25 -0700651static int net_id(char **str, int *start_out, int *end_out)
652{
653 char *end;
654 int n;
655
656 n = simple_strtoul(*str, &end, 0);
657 if((*end != '\0') || (end == *str))
658 return -1;
659
660 *start_out = n;
661 *end_out = n;
662 *str = end;
663 return n;
664}
665
Jeff Dikef28169d2007-02-10 01:43:53 -0800666static int net_remove(int n, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct uml_net *device;
669 struct net_device *dev;
670 struct uml_net_private *lp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 device = find_device(n);
673 if(device == NULL)
Jeff Dike29d56cf2005-06-25 14:55:25 -0700674 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 dev = device->dev;
677 lp = dev->priv;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700678 if(lp->fd > 0)
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800679 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if(lp->remove != NULL) (*lp->remove)(&lp->user);
681 unregister_netdev(dev);
682 platform_device_unregister(&device->pdev);
683
684 list_del(&device->list);
685 kfree(device);
686 free_netdev(dev);
Jeff Dike29d56cf2005-06-25 14:55:25 -0700687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690static struct mc_device net_mc = {
Jeff Dike84f48d42007-02-10 01:44:01 -0800691 .list = LIST_HEAD_INIT(net_mc.list),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 .name = "eth",
693 .config = net_config,
694 .get_config = NULL,
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800695 .id = net_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 .remove = net_remove,
697};
698
699static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
700 void *ptr)
701{
702 struct in_ifaddr *ifa = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct net_device *dev = ifa->ifa_dev->dev;
704 struct uml_net_private *lp;
705 void (*proc)(unsigned char *, unsigned char *, void *);
706 unsigned char addr_buf[4], netmask_buf[4];
707
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800708 if(dev->open != uml_net_open)
709 return NOTIFY_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 lp = dev->priv;
712
713 proc = NULL;
714 switch (event){
715 case NETDEV_UP:
716 proc = lp->add_address;
717 break;
718 case NETDEV_DOWN:
719 proc = lp->delete_address;
720 break;
721 }
722 if(proc != NULL){
Bodo Stroesser0e764222005-11-07 00:58:47 -0800723 memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
724 memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 (*proc)(addr_buf, netmask_buf, &lp->user);
726 }
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800727 return NOTIFY_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Jeff Dikec862fc32007-02-10 01:44:04 -0800730/* uml_net_init shouldn't be called twice on two CPUs at the same time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731struct notifier_block uml_inetaddr_notifier = {
732 .notifier_call = uml_inetaddr_event,
733};
734
735static int uml_net_init(void)
736{
737 struct list_head *ele;
Jeff Dikef28169d2007-02-10 01:43:53 -0800738 struct uml_net_private *lp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 struct in_device *ip;
740 struct in_ifaddr *in;
741
742 mconsole_register_dev(&net_mc);
743 register_inetaddr_notifier(&uml_inetaddr_notifier);
744
745 /* Devices may have been opened already, so the uml_inetaddr_notifier
746 * didn't get a chance to run for them. This fakes it so that
747 * addresses which have already been set up get handled properly.
748 */
Jeff Dikec862fc32007-02-10 01:44:04 -0800749 spin_lock(&opened_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 list_for_each(ele, &opened){
751 lp = list_entry(ele, struct uml_net_private, list);
752 ip = lp->dev->ip_ptr;
Jeff Dikec862fc32007-02-10 01:44:04 -0800753 if(ip == NULL)
754 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 in = ip->ifa_list;
756 while(in != NULL){
757 uml_inetaddr_event(NULL, NETDEV_UP, in);
758 in = in->ifa_next;
759 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800760 }
Jeff Dikec862fc32007-02-10 01:44:04 -0800761 spin_unlock(&opened_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Jeff Dikec862fc32007-02-10 01:44:04 -0800763 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766__initcall(uml_net_init);
767
768static void close_devices(void)
769{
770 struct list_head *ele;
771 struct uml_net_private *lp;
772
Jeff Dikec862fc32007-02-10 01:44:04 -0800773 spin_lock(&opened_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 list_for_each(ele, &opened){
775 lp = list_entry(ele, struct uml_net_private, list);
Jeff Dike8d93c702006-01-06 00:19:06 -0800776 free_irq(lp->dev->irq, lp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if((lp->close != NULL) && (lp->fd >= 0))
778 (*lp->close)(lp->fd, &lp->user);
Jeff Dikec862fc32007-02-10 01:44:04 -0800779 if(lp->remove != NULL)
780 (*lp->remove)(&lp->user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
Jeff Dikec862fc32007-02-10 01:44:04 -0800782 spin_unlock(&opened_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
785__uml_exitcall(close_devices);
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
788{
789 if((skb != NULL) && (skb_tailroom(skb) < extra)){
790 struct sk_buff *skb2;
791
792 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
793 dev_kfree_skb(skb);
794 skb = skb2;
795 }
796 if(skb != NULL) skb_put(skb, extra);
797 return(skb);
798}
799
Jeff Dikef28169d2007-02-10 01:43:53 -0800800void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
801 void *),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 void *arg)
803{
804 struct net_device *dev = d;
805 struct in_device *ip = dev->ip_ptr;
806 struct in_ifaddr *in;
807 unsigned char address[4], netmask[4];
808
809 if(ip == NULL) return;
810 in = ip->ifa_list;
811 while(in != NULL){
Bodo Stroesser0e764222005-11-07 00:58:47 -0800812 memcpy(address, &in->ifa_address, sizeof(address));
813 memcpy(netmask, &in->ifa_mask, sizeof(netmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 (*cb)(address, netmask, arg);
815 in = in->ifa_next;
816 }
817}
818
819int dev_netmask(void *d, void *m)
820{
821 struct net_device *dev = d;
822 struct in_device *ip = dev->ip_ptr;
823 struct in_ifaddr *in;
Al Viroa144ea42006-09-28 18:00:55 -0700824 __be32 *mask_out = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Jeff Dikef28169d2007-02-10 01:43:53 -0800826 if(ip == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return(1);
828
829 in = ip->ifa_list;
Jeff Dikef28169d2007-02-10 01:43:53 -0800830 if(in == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return(1);
832
833 *mask_out = in->ifa_mask;
834 return(0);
835}
836
837void *get_output_buffer(int *len_out)
838{
839 void *ret;
840
841 ret = (void *) __get_free_pages(GFP_KERNEL, 0);
842 if(ret) *len_out = PAGE_SIZE;
843 else *len_out = 0;
Jeff Dike4ea21cd2007-02-10 01:43:56 -0800844 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
847void free_output_buffer(void *buffer)
848{
849 free_pages((unsigned long) buffer, 0);
850}
851
Jeff Dikef28169d2007-02-10 01:43:53 -0800852int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 char **gate_addr)
854{
855 char *remain;
856
857 remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
858 if(remain != NULL){
859 printk("tap_setup_common - Extra garbage on specification : "
860 "'%s'\n", remain);
861 return(1);
862 }
863
864 return(0);
865}
866
867unsigned short eth_protocol(struct sk_buff *skb)
868{
869 return(eth_type_trans(skb, skb->dev));
870}