blob: c067abbbfd97fda603c833307a55950b8a9c276b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
3 * 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
8#include "linux/config.h"
9#include "linux/kernel.h"
10#include "linux/netdevice.h"
11#include "linux/rtnetlink.h"
12#include "linux/skbuff.h"
13#include "linux/socket.h"
14#include "linux/spinlock.h"
15#include "linux/module.h"
16#include "linux/init.h"
17#include "linux/etherdevice.h"
18#include "linux/list.h"
19#include "linux/inetdevice.h"
20#include "linux/ctype.h"
21#include "linux/bootmem.h"
22#include "linux/ethtool.h"
Russell Kingd052d1b2005-10-29 19:07:23 +010023#include "linux/platform_device.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "asm/uaccess.h"
25#include "user_util.h"
26#include "kern_util.h"
27#include "net_kern.h"
28#include "net_user.h"
29#include "mconsole_kern.h"
30#include "init.h"
31#include "irq_user.h"
32#include "irq_kern.h"
33
Paolo 'Blaisorblade' Giarrussobf61f502006-09-25 23:33:07 -070034static inline void set_ether_mac(struct net_device *dev, unsigned char *addr)
35{
36 memcpy(dev->dev_addr, addr, ETH_ALEN);
37}
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define DRIVER_NAME "uml-netdev"
40
41static DEFINE_SPINLOCK(opened_lock);
Jeff Dike90107722006-01-06 00:18:54 -080042static LIST_HEAD(opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44static int uml_net_rx(struct net_device *dev)
45{
46 struct uml_net_private *lp = dev->priv;
47 int pkt_len;
48 struct sk_buff *skb;
49
50 /* If we can't allocate memory, try again next round. */
51 skb = dev_alloc_skb(dev->mtu);
52 if (skb == NULL) {
53 lp->stats.rx_dropped++;
54 return 0;
55 }
56
57 skb->dev = dev;
58 skb_put(skb, dev->mtu);
59 skb->mac.raw = skb->data;
60 pkt_len = (*lp->read)(lp->fd, &skb, lp);
61
62 if (pkt_len > 0) {
63 skb_trim(skb, pkt_len);
64 skb->protocol = (*lp->protocol)(skb);
65 netif_rx(skb);
66
67 lp->stats.rx_bytes += skb->len;
68 lp->stats.rx_packets++;
69 return pkt_len;
70 }
71
72 kfree_skb(skb);
73 return pkt_len;
74}
75
Paolo 'Blaisorblade' Giarrusso71c8d4c2006-01-18 17:42:56 -080076static void uml_dev_close(void* dev)
77{
78 dev_close( (struct net_device *) dev);
79}
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081irqreturn_t uml_net_interrupt(int irq, void *dev_id, struct pt_regs *regs)
82{
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) {
Paolo 'Blaisorblade' Giarrusso71c8d4c2006-01-18 17:42:56 -080093 DECLARE_WORK(close_work, uml_dev_close, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 printk(KERN_ERR
95 "Device '%s' read returned %d, shutting it down\n",
96 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
101 * there's no harm in delaying the device shutdown. */
102 schedule_work(&close_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 goto out;
104 }
105 reactivate_fd(lp->fd, UM_ETH_IRQ);
106
Paolo 'Blaisorblade' Giarrusso71c8d4c2006-01-18 17:42:56 -0800107out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 spin_unlock(&lp->lock);
109 return(IRQ_HANDLED);
110}
111
112static int uml_net_open(struct net_device *dev)
113{
114 struct uml_net_private *lp = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 int err;
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if(lp->fd >= 0){
118 err = -ENXIO;
119 goto out;
120 }
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 lp->fd = (*lp->open)(&lp->user);
123 if(lp->fd < 0){
124 err = lp->fd;
125 goto out;
126 }
127
128 err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
Thomas Gleixnerbd6aa652006-07-01 19:29:27 -0700129 IRQF_DISABLED | IRQF_SHARED, dev->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if(err != 0){
131 printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 err = -ENETUNREACH;
Jeff Dike14d9ead2006-02-07 12:58:42 -0800133 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135
136 lp->tl.data = (unsigned long) &lp->user;
137 netif_start_queue(dev);
138
139 /* clear buffer - it can happen that the host side of the interface
140 * is full when we get here. In this case, new data is never queued,
141 * SIGIOs never arrive, and the net never works.
142 */
143 while((err = uml_net_rx(dev)) > 0) ;
144
Jeff Dike14d9ead2006-02-07 12:58:42 -0800145 spin_lock(&opened_lock);
146 list_add(&lp->list, &opened);
147 spin_unlock(&opened_lock);
148
149 return 0;
150out_close:
151 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
152 lp->fd = -1;
153out:
Jeff Dike14d9ead2006-02-07 12:58:42 -0800154 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157static int uml_net_close(struct net_device *dev)
158{
159 struct uml_net_private *lp = dev->priv;
160
161 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 free_irq(dev->irq, dev);
164 if(lp->close != NULL)
165 (*lp->close)(lp->fd, &lp->user);
166 lp->fd = -1;
167
Jeff Dike14d9ead2006-02-07 12:58:42 -0800168 spin_lock(&opened_lock);
169 list_del(&lp->list);
170 spin_unlock(&opened_lock);
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return 0;
173}
174
175static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
176{
177 struct uml_net_private *lp = dev->priv;
178 unsigned long flags;
179 int len;
180
181 netif_stop_queue(dev);
182
183 spin_lock_irqsave(&lp->lock, flags);
184
185 len = (*lp->write)(lp->fd, &skb, lp);
186
187 if(len == skb->len) {
188 lp->stats.tx_packets++;
189 lp->stats.tx_bytes += skb->len;
190 dev->trans_start = jiffies;
191 netif_start_queue(dev);
192
193 /* this is normally done in the interrupt when tx finishes */
194 netif_wake_queue(dev);
195 }
196 else if(len == 0){
197 netif_start_queue(dev);
198 lp->stats.tx_dropped++;
199 }
200 else {
201 netif_start_queue(dev);
202 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
203 }
204
205 spin_unlock_irqrestore(&lp->lock, flags);
206
207 dev_kfree_skb(skb);
208
209 return 0;
210}
211
212static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
213{
214 struct uml_net_private *lp = dev->priv;
215 return &lp->stats;
216}
217
218static void uml_net_set_multicast_list(struct net_device *dev)
219{
220 if (dev->flags & IFF_PROMISC) return;
221 else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
222 else dev->flags &= ~IFF_ALLMULTI;
223}
224
225static void uml_net_tx_timeout(struct net_device *dev)
226{
227 dev->trans_start = jiffies;
228 netif_wake_queue(dev);
229}
230
231static int uml_net_set_mac(struct net_device *dev, void *addr)
232{
233 struct uml_net_private *lp = dev->priv;
234 struct sockaddr *hwaddr = addr;
235
Paolo 'Blaisorblade' Giarrusso48af05e2006-09-27 01:50:31 -0700236 spin_lock_irq(&lp->lock);
Paolo 'Blaisorblade' Giarrussobf61f502006-09-25 23:33:07 -0700237 set_ether_mac(dev, hwaddr->sa_data);
Paolo 'Blaisorblade' Giarrusso48af05e2006-09-27 01:50:31 -0700238 spin_unlock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 return(0);
241}
242
243static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
244{
245 struct uml_net_private *lp = dev->priv;
246 int err = 0;
247
Paolo 'Blaisorblade' Giarrusso48af05e2006-09-27 01:50:31 -0700248 spin_lock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
251 if(new_mtu < 0){
252 err = new_mtu;
253 goto out;
254 }
255
256 dev->mtu = new_mtu;
257
258 out:
Paolo 'Blaisorblade' Giarrusso48af05e2006-09-27 01:50:31 -0700259 spin_unlock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return err;
261}
262
Christoph Hellwig6d387482005-11-07 06:21:21 +0100263static void uml_net_get_drvinfo(struct net_device *dev,
264 struct ethtool_drvinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Christoph Hellwig6d387482005-11-07 06:21:21 +0100266 strcpy(info->driver, DRIVER_NAME);
267 strcpy(info->version, "42");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Christoph Hellwig6d387482005-11-07 06:21:21 +0100270static struct ethtool_ops uml_net_ethtool_ops = {
271 .get_drvinfo = uml_net_get_drvinfo,
272 .get_link = ethtool_op_get_link,
273};
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275void uml_net_user_timer_expire(unsigned long _conn)
276{
277#ifdef undef
278 struct connection *conn = (struct connection *)_conn;
279
280 dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
281 do_connect(conn);
282#endif
283}
284
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700285static void setup_etheraddr(char *str, unsigned char *addr)
286{
287 char *end;
288 int i;
289
290 if(str == NULL)
291 goto random;
292
293 for(i=0;i<6;i++){
294 addr[i] = simple_strtoul(str, &end, 16);
295 if((end == str) ||
296 ((*end != ':') && (*end != ',') && (*end != '\0'))){
297 printk(KERN_ERR
298 "setup_etheraddr: failed to parse '%s' "
299 "as an ethernet address\n", str);
300 goto random;
301 }
302 str = end + 1;
303 }
304 if(addr[0] & 1){
305 printk(KERN_ERR
306 "Attempt to assign a broadcast ethernet address to a "
307 "device disallowed\n");
308 goto random;
309 }
310 return;
311
312random:
313 addr[0] = 0xfe;
314 addr[1] = 0xfd;
315 random_mac(addr);
316}
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
328static int eth_configure(int n, void *init, char *mac,
329 struct transport *transport)
330{
331 struct uml_net *device;
332 struct net_device *dev;
333 struct uml_net_private *lp;
334 int save, err, size;
335
336 size = transport->private_size + sizeof(struct uml_net_private) +
337 sizeof(((struct uml_net_private *) 0)->user);
338
339 device = kmalloc(sizeof(*device), GFP_KERNEL);
340 if (device == NULL) {
341 printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
342 return(1);
343 }
344
345 memset(device, 0, sizeof(*device));
346 INIT_LIST_HEAD(&device->list);
347 device->index = n;
348
349 spin_lock(&devices_lock);
350 list_add(&device->list, &devices);
351 spin_unlock(&devices_lock);
352
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700353 setup_etheraddr(mac, device->mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 printk(KERN_INFO "Netdevice %d ", n);
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700356 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
357 device->mac[0], device->mac[1],
358 device->mac[2], device->mac[3],
359 device->mac[4], device->mac[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 printk(": ");
361 dev = alloc_etherdev(size);
362 if (dev == NULL) {
363 printk(KERN_ERR "eth_configure: failed to allocate device\n");
364 return 1;
365 }
366
Paolo 'Blaisorblade' Giarrussoe56a7882006-01-18 17:42:55 -0800367 lp = dev->priv;
368 /* This points to the transport private data. It's still clear, but we
369 * must memset it to 0 *now*. Let's help the drivers. */
370 memset(lp, 0, size);
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /* sysfs register */
373 if (!driver_registered) {
Russell King3ae5eae2005-11-09 22:32:44 +0000374 platform_driver_register(&uml_net_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 driver_registered = 1;
376 }
377 device->pdev.id = n;
378 device->pdev.name = DRIVER_NAME;
379 platform_device_register(&device->pdev);
380 SET_NETDEV_DEV(dev,&device->pdev.dev);
381
382 /* If this name ends up conflicting with an existing registered
383 * netdevice, that is OK, register_netdev{,ice}() will notice this
384 * and fail.
385 */
386 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
387 device->dev = dev;
388
389 (*transport->kern->init)(dev, init);
390
391 dev->mtu = transport->user->max_packet;
392 dev->open = uml_net_open;
393 dev->hard_start_xmit = uml_net_start_xmit;
394 dev->stop = uml_net_close;
395 dev->get_stats = uml_net_get_stats;
396 dev->set_multicast_list = uml_net_set_multicast_list;
397 dev->tx_timeout = uml_net_tx_timeout;
398 dev->set_mac_address = uml_net_set_mac;
399 dev->change_mtu = uml_net_change_mtu;
Christoph Hellwig6d387482005-11-07 06:21:21 +0100400 dev->ethtool_ops = &uml_net_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 dev->watchdog_timeo = (HZ >> 1);
402 dev->irq = UM_ETH_IRQ;
403
404 rtnl_lock();
405 err = register_netdevice(dev);
406 rtnl_unlock();
407 if (err) {
408 device->dev = NULL;
409 /* XXX: should we call ->remove() here? */
410 free_netdev(dev);
411 return 1;
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 /* lp.user is the first four bytes of the transport data, which
415 * has already been initialized. This structure assignment will
416 * overwrite that, so we make sure that .user gets overwritten with
417 * what it already has.
418 */
419 save = lp->user[0];
420 *lp = ((struct uml_net_private)
421 { .list = LIST_HEAD_INIT(lp->list),
422 .dev = dev,
423 .fd = -1,
424 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 .protocol = transport->kern->protocol,
426 .open = transport->user->open,
427 .close = transport->user->close,
428 .remove = transport->user->remove,
429 .read = transport->kern->read,
430 .write = transport->kern->write,
431 .add_address = transport->user->add_address,
432 .delete_address = transport->user->delete_address,
433 .set_mtu = transport->user->set_mtu,
434 .user = { save } });
435
436 init_timer(&lp->tl);
437 spin_lock_init(&lp->lock);
438 lp->tl.function = uml_net_user_timer_expire;
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700439 memcpy(lp->mac, device->mac, sizeof(lp->mac));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (transport->user->init)
442 (*transport->user->init)(&lp->user, dev);
443
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700444 set_ether_mac(dev, device->mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Jeff Dike14d9ead2006-02-07 12:58:42 -0800446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449static struct uml_net *find_device(int n)
450{
451 struct uml_net *device;
452 struct list_head *ele;
453
454 spin_lock(&devices_lock);
455 list_for_each(ele, &devices){
456 device = list_entry(ele, struct uml_net, list);
457 if(device->index == n)
458 goto out;
459 }
460 device = NULL;
461 out:
462 spin_unlock(&devices_lock);
463 return(device);
464}
465
466static int eth_parse(char *str, int *index_out, char **str_out)
467{
468 char *end;
469 int n;
470
471 n = simple_strtoul(str, &end, 0);
472 if(end == str){
473 printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
474 return(1);
475 }
476 if(n < 0){
477 printk(KERN_ERR "eth_setup: device %d is negative\n", n);
478 return(1);
479 }
480 str = end;
481 if(*str != '='){
482 printk(KERN_ERR
483 "eth_setup: expected '=' after device number\n");
484 return(1);
485 }
486 str++;
487 if(find_device(n)){
488 printk(KERN_ERR "eth_setup: Device %d already configured\n",
489 n);
490 return(1);
491 }
492 if(index_out) *index_out = n;
493 *str_out = str;
494 return(0);
495}
496
497struct eth_init {
498 struct list_head list;
499 char *init;
500 int index;
501};
502
503/* Filled in at boot time. Will need locking if the transports become
504 * modular.
505 */
506struct list_head transports = LIST_HEAD_INIT(transports);
507
508/* Filled in during early boot */
509struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
510
511static int check_transport(struct transport *transport, char *eth, int n,
512 void **init_out, char **mac_out)
513{
514 int len;
515
516 len = strlen(transport->name);
517 if(strncmp(eth, transport->name, len))
518 return(0);
519
520 eth += len;
521 if(*eth == ',')
522 eth++;
523 else if(*eth != '\0')
524 return(0);
525
526 *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
527 if(*init_out == NULL)
528 return(1);
529
530 if(!transport->setup(eth, mac_out, *init_out)){
531 kfree(*init_out);
532 *init_out = NULL;
533 }
534 return(1);
535}
536
537void register_transport(struct transport *new)
538{
539 struct list_head *ele, *next;
540 struct eth_init *eth;
541 void *init;
542 char *mac = NULL;
543 int match;
544
545 list_add(&new->list, &transports);
546
547 list_for_each_safe(ele, next, &eth_cmd_line){
548 eth = list_entry(ele, struct eth_init, list);
549 match = check_transport(new, eth->init, eth->index, &init,
550 &mac);
551 if(!match)
552 continue;
553 else if(init != NULL){
554 eth_configure(eth->index, init, mac, new);
555 kfree(init);
556 }
557 list_del(&eth->list);
558 }
559}
560
561static int eth_setup_common(char *str, int index)
562{
563 struct list_head *ele;
564 struct transport *transport;
565 void *init;
566 char *mac = NULL;
567
568 list_for_each(ele, &transports){
569 transport = list_entry(ele, struct transport, list);
570 if(!check_transport(transport, str, index, &init, &mac))
571 continue;
572 if(init != NULL){
573 eth_configure(index, init, mac, transport);
574 kfree(init);
575 }
576 return(1);
577 }
578 return(0);
579}
580
581static int eth_setup(char *str)
582{
583 struct eth_init *new;
584 int n, err;
585
586 err = eth_parse(str, &n, &str);
Jeff Dike1183dc92006-09-27 01:50:43 -0700587 if(err)
588 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Jeff Dike1183dc92006-09-27 01:50:43 -0700590 new = alloc_bootmem(sizeof(*new));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (new == NULL){
592 printk("eth_init : alloc_bootmem failed\n");
Jeff Dike1183dc92006-09-27 01:50:43 -0700593 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595
596 INIT_LIST_HEAD(&new->list);
597 new->index = n;
598 new->init = str;
599
600 list_add_tail(&new->list, &eth_cmd_line);
Jeff Dike1183dc92006-09-27 01:50:43 -0700601 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604__setup("eth", eth_setup);
605__uml_help(eth_setup,
606"eth[0-9]+=<transport>,<options>\n"
607" Configure a network device.\n\n"
608);
609
610#if 0
611static int eth_init(void)
612{
613 struct list_head *ele, *next;
614 struct eth_init *eth;
615
616 list_for_each_safe(ele, next, &eth_cmd_line){
617 eth = list_entry(ele, struct eth_init, list);
618
619 if(eth_setup_common(eth->init, eth->index))
620 list_del(&eth->list);
621 }
622
623 return(1);
624}
625__initcall(eth_init);
626#endif
627
628static int net_config(char *str)
629{
630 int n, err;
631
632 err = eth_parse(str, &n, &str);
633 if(err) return(err);
634
Jeff Dike970d6e32006-01-06 00:18:48 -0800635 str = kstrdup(str, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if(str == NULL){
637 printk(KERN_ERR "net_config failed to strdup string\n");
638 return(-1);
639 }
640 err = !eth_setup_common(str, n);
641 if(err)
642 kfree(str);
643 return(err);
644}
645
Jeff Dike29d56cf2005-06-25 14:55:25 -0700646static int net_id(char **str, int *start_out, int *end_out)
647{
648 char *end;
649 int n;
650
651 n = simple_strtoul(*str, &end, 0);
652 if((*end != '\0') || (end == *str))
653 return -1;
654
655 *start_out = n;
656 *end_out = n;
657 *str = end;
658 return n;
659}
660
661static int net_remove(int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 struct uml_net *device;
664 struct net_device *dev;
665 struct uml_net_private *lp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 device = find_device(n);
668 if(device == NULL)
Jeff Dike29d56cf2005-06-25 14:55:25 -0700669 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 dev = device->dev;
672 lp = dev->priv;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700673 if(lp->fd > 0)
674 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if(lp->remove != NULL) (*lp->remove)(&lp->user);
676 unregister_netdev(dev);
677 platform_device_unregister(&device->pdev);
678
679 list_del(&device->list);
680 kfree(device);
681 free_netdev(dev);
Jeff Dike29d56cf2005-06-25 14:55:25 -0700682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685static struct mc_device net_mc = {
686 .name = "eth",
687 .config = net_config,
688 .get_config = NULL,
Jeff Dike29d56cf2005-06-25 14:55:25 -0700689 .id = net_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 .remove = net_remove,
691};
692
693static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
694 void *ptr)
695{
696 struct in_ifaddr *ifa = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 struct net_device *dev = ifa->ifa_dev->dev;
698 struct uml_net_private *lp;
699 void (*proc)(unsigned char *, unsigned char *, void *);
700 unsigned char addr_buf[4], netmask_buf[4];
701
702 if(dev->open != uml_net_open) return(NOTIFY_DONE);
703
704 lp = dev->priv;
705
706 proc = NULL;
707 switch (event){
708 case NETDEV_UP:
709 proc = lp->add_address;
710 break;
711 case NETDEV_DOWN:
712 proc = lp->delete_address;
713 break;
714 }
715 if(proc != NULL){
Bodo Stroesser0e764222005-11-07 00:58:47 -0800716 memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
717 memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 (*proc)(addr_buf, netmask_buf, &lp->user);
719 }
720 return(NOTIFY_DONE);
721}
722
723struct notifier_block uml_inetaddr_notifier = {
724 .notifier_call = uml_inetaddr_event,
725};
726
727static int uml_net_init(void)
728{
729 struct list_head *ele;
730 struct uml_net_private *lp;
731 struct in_device *ip;
732 struct in_ifaddr *in;
733
734 mconsole_register_dev(&net_mc);
735 register_inetaddr_notifier(&uml_inetaddr_notifier);
736
737 /* Devices may have been opened already, so the uml_inetaddr_notifier
738 * didn't get a chance to run for them. This fakes it so that
739 * addresses which have already been set up get handled properly.
740 */
741 list_for_each(ele, &opened){
742 lp = list_entry(ele, struct uml_net_private, list);
743 ip = lp->dev->ip_ptr;
744 if(ip == NULL) continue;
745 in = ip->ifa_list;
746 while(in != NULL){
747 uml_inetaddr_event(NULL, NETDEV_UP, in);
748 in = in->ifa_next;
749 }
750 }
751
752 return(0);
753}
754
755__initcall(uml_net_init);
756
757static void close_devices(void)
758{
759 struct list_head *ele;
760 struct uml_net_private *lp;
761
762 list_for_each(ele, &opened){
763 lp = list_entry(ele, struct uml_net_private, list);
Jeff Dike8d93c702006-01-06 00:19:06 -0800764 free_irq(lp->dev->irq, lp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if((lp->close != NULL) && (lp->fd >= 0))
766 (*lp->close)(lp->fd, &lp->user);
767 if(lp->remove != NULL) (*lp->remove)(&lp->user);
768 }
769}
770
771__uml_exitcall(close_devices);
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
774{
775 if((skb != NULL) && (skb_tailroom(skb) < extra)){
776 struct sk_buff *skb2;
777
778 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
779 dev_kfree_skb(skb);
780 skb = skb2;
781 }
782 if(skb != NULL) skb_put(skb, extra);
783 return(skb);
784}
785
786void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
787 void *),
788 void *arg)
789{
790 struct net_device *dev = d;
791 struct in_device *ip = dev->ip_ptr;
792 struct in_ifaddr *in;
793 unsigned char address[4], netmask[4];
794
795 if(ip == NULL) return;
796 in = ip->ifa_list;
797 while(in != NULL){
Bodo Stroesser0e764222005-11-07 00:58:47 -0800798 memcpy(address, &in->ifa_address, sizeof(address));
799 memcpy(netmask, &in->ifa_mask, sizeof(netmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 (*cb)(address, netmask, arg);
801 in = in->ifa_next;
802 }
803}
804
805int dev_netmask(void *d, void *m)
806{
807 struct net_device *dev = d;
808 struct in_device *ip = dev->ip_ptr;
809 struct in_ifaddr *in;
Al Viroa144ea42006-09-28 18:00:55 -0700810 __be32 *mask_out = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 if(ip == NULL)
813 return(1);
814
815 in = ip->ifa_list;
816 if(in == NULL)
817 return(1);
818
819 *mask_out = in->ifa_mask;
820 return(0);
821}
822
823void *get_output_buffer(int *len_out)
824{
825 void *ret;
826
827 ret = (void *) __get_free_pages(GFP_KERNEL, 0);
828 if(ret) *len_out = PAGE_SIZE;
829 else *len_out = 0;
830 return(ret);
831}
832
833void free_output_buffer(void *buffer)
834{
835 free_pages((unsigned long) buffer, 0);
836}
837
838int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
839 char **gate_addr)
840{
841 char *remain;
842
843 remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
844 if(remain != NULL){
845 printk("tap_setup_common - Extra garbage on specification : "
846 "'%s'\n", remain);
847 return(1);
848 }
849
850 return(0);
851}
852
853unsigned short eth_protocol(struct sk_buff *skb)
854{
855 return(eth_type_trans(skb, skb->dev));
856}
857
858/*
859 * Overrides for Emacs so that we follow Linus's tabbing style.
860 * Emacs will notice this stuff at the end of the file and automatically
861 * adjust the settings for this buffer only. This must remain at the end
862 * of the file.
863 * ---------------------------------------------------------------------------
864 * Local variables:
865 * c-file-style: "linux"
866 * End:
867 */