blob: 286bc0b3207fd7b13b861088473959933f8aa99b [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
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);
58 skb->mac.raw = skb->data;
59 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
Paolo 'Blaisorblade' Giarrusso71c8d4c2006-01-18 17:42:56 -080075static void uml_dev_close(void* dev)
76{
77 dev_close( (struct net_device *) dev);
78}
79
Al Viro7bea96f2006-10-08 22:49:34 +010080irqreturn_t uml_net_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 struct net_device *dev = dev_id;
83 struct uml_net_private *lp = dev->priv;
84 int err;
85
86 if(!netif_running(dev))
87 return(IRQ_NONE);
88
89 spin_lock(&lp->lock);
90 while((err = uml_net_rx(dev)) > 0) ;
91 if(err < 0) {
Paolo 'Blaisorblade' Giarrusso71c8d4c2006-01-18 17:42:56 -080092 DECLARE_WORK(close_work, uml_dev_close, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 printk(KERN_ERR
94 "Device '%s' read returned %d, shutting it down\n",
95 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
100 * there's no harm in delaying the device shutdown. */
101 schedule_work(&close_work);
David Howells6d5aefb2006-12-05 19:36:26 +0000102#error this is not permitted - close_work will go out of scope
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:
Ollie Wildd6c64102006-09-29 15:50:28 -0700313 random_ether_addr(addr);
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700314}
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316static DEFINE_SPINLOCK(devices_lock);
Jeff Dike90107722006-01-06 00:18:54 -0800317static LIST_HEAD(devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Russell King3ae5eae2005-11-09 22:32:44 +0000319static struct platform_driver uml_net_driver = {
320 .driver = {
321 .name = DRIVER_NAME,
322 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323};
324static int driver_registered;
325
326static int eth_configure(int n, void *init, char *mac,
327 struct transport *transport)
328{
329 struct uml_net *device;
330 struct net_device *dev;
331 struct uml_net_private *lp;
332 int save, err, size;
333
334 size = transport->private_size + sizeof(struct uml_net_private) +
335 sizeof(((struct uml_net_private *) 0)->user);
336
337 device = kmalloc(sizeof(*device), GFP_KERNEL);
338 if (device == NULL) {
339 printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
340 return(1);
341 }
342
343 memset(device, 0, sizeof(*device));
344 INIT_LIST_HEAD(&device->list);
345 device->index = n;
346
347 spin_lock(&devices_lock);
348 list_add(&device->list, &devices);
349 spin_unlock(&devices_lock);
350
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700351 setup_etheraddr(mac, device->mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 printk(KERN_INFO "Netdevice %d ", n);
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700354 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
355 device->mac[0], device->mac[1],
356 device->mac[2], device->mac[3],
357 device->mac[4], device->mac[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 printk(": ");
359 dev = alloc_etherdev(size);
360 if (dev == NULL) {
361 printk(KERN_ERR "eth_configure: failed to allocate device\n");
362 return 1;
363 }
364
Paolo 'Blaisorblade' Giarrussoe56a7882006-01-18 17:42:55 -0800365 lp = dev->priv;
366 /* This points to the transport private data. It's still clear, but we
367 * must memset it to 0 *now*. Let's help the drivers. */
368 memset(lp, 0, size);
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* sysfs register */
371 if (!driver_registered) {
Russell King3ae5eae2005-11-09 22:32:44 +0000372 platform_driver_register(&uml_net_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 driver_registered = 1;
374 }
375 device->pdev.id = n;
376 device->pdev.name = DRIVER_NAME;
377 platform_device_register(&device->pdev);
378 SET_NETDEV_DEV(dev,&device->pdev.dev);
379
380 /* If this name ends up conflicting with an existing registered
381 * netdevice, that is OK, register_netdev{,ice}() will notice this
382 * and fail.
383 */
384 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
385 device->dev = dev;
386
387 (*transport->kern->init)(dev, init);
388
389 dev->mtu = transport->user->max_packet;
390 dev->open = uml_net_open;
391 dev->hard_start_xmit = uml_net_start_xmit;
392 dev->stop = uml_net_close;
393 dev->get_stats = uml_net_get_stats;
394 dev->set_multicast_list = uml_net_set_multicast_list;
395 dev->tx_timeout = uml_net_tx_timeout;
396 dev->set_mac_address = uml_net_set_mac;
397 dev->change_mtu = uml_net_change_mtu;
Christoph Hellwig6d387482005-11-07 06:21:21 +0100398 dev->ethtool_ops = &uml_net_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 dev->watchdog_timeo = (HZ >> 1);
400 dev->irq = UM_ETH_IRQ;
401
402 rtnl_lock();
403 err = register_netdevice(dev);
404 rtnl_unlock();
405 if (err) {
406 device->dev = NULL;
407 /* XXX: should we call ->remove() here? */
408 free_netdev(dev);
409 return 1;
410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 /* lp.user is the first four bytes of the transport data, which
413 * has already been initialized. This structure assignment will
414 * overwrite that, so we make sure that .user gets overwritten with
415 * what it already has.
416 */
417 save = lp->user[0];
418 *lp = ((struct uml_net_private)
419 { .list = LIST_HEAD_INIT(lp->list),
420 .dev = dev,
421 .fd = -1,
422 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 .protocol = transport->kern->protocol,
424 .open = transport->user->open,
425 .close = transport->user->close,
426 .remove = transport->user->remove,
427 .read = transport->kern->read,
428 .write = transport->kern->write,
429 .add_address = transport->user->add_address,
430 .delete_address = transport->user->delete_address,
431 .set_mtu = transport->user->set_mtu,
432 .user = { save } });
433
434 init_timer(&lp->tl);
435 spin_lock_init(&lp->lock);
436 lp->tl.function = uml_net_user_timer_expire;
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700437 memcpy(lp->mac, device->mac, sizeof(lp->mac));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 if (transport->user->init)
440 (*transport->user->init)(&lp->user, dev);
441
Jeff Dikeb10aeee2006-09-29 01:58:50 -0700442 set_ether_mac(dev, device->mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Jeff Dike14d9ead2006-02-07 12:58:42 -0800444 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447static struct uml_net *find_device(int n)
448{
449 struct uml_net *device;
450 struct list_head *ele;
451
452 spin_lock(&devices_lock);
453 list_for_each(ele, &devices){
454 device = list_entry(ele, struct uml_net, list);
455 if(device->index == n)
456 goto out;
457 }
458 device = NULL;
459 out:
460 spin_unlock(&devices_lock);
461 return(device);
462}
463
464static int eth_parse(char *str, int *index_out, char **str_out)
465{
466 char *end;
467 int n;
468
469 n = simple_strtoul(str, &end, 0);
470 if(end == str){
471 printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
472 return(1);
473 }
474 if(n < 0){
475 printk(KERN_ERR "eth_setup: device %d is negative\n", n);
476 return(1);
477 }
478 str = end;
479 if(*str != '='){
480 printk(KERN_ERR
481 "eth_setup: expected '=' after device number\n");
482 return(1);
483 }
484 str++;
485 if(find_device(n)){
486 printk(KERN_ERR "eth_setup: Device %d already configured\n",
487 n);
488 return(1);
489 }
490 if(index_out) *index_out = n;
491 *str_out = str;
492 return(0);
493}
494
495struct eth_init {
496 struct list_head list;
497 char *init;
498 int index;
499};
500
501/* Filled in at boot time. Will need locking if the transports become
502 * modular.
503 */
504struct list_head transports = LIST_HEAD_INIT(transports);
505
506/* Filled in during early boot */
507struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
508
509static int check_transport(struct transport *transport, char *eth, int n,
510 void **init_out, char **mac_out)
511{
512 int len;
513
514 len = strlen(transport->name);
515 if(strncmp(eth, transport->name, len))
516 return(0);
517
518 eth += len;
519 if(*eth == ',')
520 eth++;
521 else if(*eth != '\0')
522 return(0);
523
524 *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
525 if(*init_out == NULL)
526 return(1);
527
528 if(!transport->setup(eth, mac_out, *init_out)){
529 kfree(*init_out);
530 *init_out = NULL;
531 }
532 return(1);
533}
534
535void register_transport(struct transport *new)
536{
537 struct list_head *ele, *next;
538 struct eth_init *eth;
539 void *init;
540 char *mac = NULL;
541 int match;
542
543 list_add(&new->list, &transports);
544
545 list_for_each_safe(ele, next, &eth_cmd_line){
546 eth = list_entry(ele, struct eth_init, list);
547 match = check_transport(new, eth->init, eth->index, &init,
548 &mac);
549 if(!match)
550 continue;
551 else if(init != NULL){
552 eth_configure(eth->index, init, mac, new);
553 kfree(init);
554 }
555 list_del(&eth->list);
556 }
557}
558
559static int eth_setup_common(char *str, int index)
560{
561 struct list_head *ele;
562 struct transport *transport;
563 void *init;
564 char *mac = NULL;
565
566 list_for_each(ele, &transports){
567 transport = list_entry(ele, struct transport, list);
568 if(!check_transport(transport, str, index, &init, &mac))
569 continue;
570 if(init != NULL){
571 eth_configure(index, init, mac, transport);
572 kfree(init);
573 }
574 return(1);
575 }
576 return(0);
577}
578
579static int eth_setup(char *str)
580{
581 struct eth_init *new;
582 int n, err;
583
584 err = eth_parse(str, &n, &str);
Jeff Dike1183dc92006-09-27 01:50:43 -0700585 if(err)
586 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Jeff Dike1183dc92006-09-27 01:50:43 -0700588 new = alloc_bootmem(sizeof(*new));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (new == NULL){
590 printk("eth_init : alloc_bootmem failed\n");
Jeff Dike1183dc92006-09-27 01:50:43 -0700591 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
594 INIT_LIST_HEAD(&new->list);
595 new->index = n;
596 new->init = str;
597
598 list_add_tail(&new->list, &eth_cmd_line);
Jeff Dike1183dc92006-09-27 01:50:43 -0700599 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602__setup("eth", eth_setup);
603__uml_help(eth_setup,
604"eth[0-9]+=<transport>,<options>\n"
605" Configure a network device.\n\n"
606);
607
608#if 0
609static int eth_init(void)
610{
611 struct list_head *ele, *next;
612 struct eth_init *eth;
613
614 list_for_each_safe(ele, next, &eth_cmd_line){
615 eth = list_entry(ele, struct eth_init, list);
616
617 if(eth_setup_common(eth->init, eth->index))
618 list_del(&eth->list);
619 }
620
621 return(1);
622}
623__initcall(eth_init);
624#endif
625
626static int net_config(char *str)
627{
628 int n, err;
629
630 err = eth_parse(str, &n, &str);
631 if(err) return(err);
632
Jeff Dike970d6e32006-01-06 00:18:48 -0800633 str = kstrdup(str, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if(str == NULL){
635 printk(KERN_ERR "net_config failed to strdup string\n");
636 return(-1);
637 }
638 err = !eth_setup_common(str, n);
639 if(err)
640 kfree(str);
641 return(err);
642}
643
Jeff Dike29d56cf2005-06-25 14:55:25 -0700644static int net_id(char **str, int *start_out, int *end_out)
645{
646 char *end;
647 int n;
648
649 n = simple_strtoul(*str, &end, 0);
650 if((*end != '\0') || (end == *str))
651 return -1;
652
653 *start_out = n;
654 *end_out = n;
655 *str = end;
656 return n;
657}
658
659static int net_remove(int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 struct uml_net *device;
662 struct net_device *dev;
663 struct uml_net_private *lp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 device = find_device(n);
666 if(device == NULL)
Jeff Dike29d56cf2005-06-25 14:55:25 -0700667 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 dev = device->dev;
670 lp = dev->priv;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700671 if(lp->fd > 0)
672 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if(lp->remove != NULL) (*lp->remove)(&lp->user);
674 unregister_netdev(dev);
675 platform_device_unregister(&device->pdev);
676
677 list_del(&device->list);
678 kfree(device);
679 free_netdev(dev);
Jeff Dike29d56cf2005-06-25 14:55:25 -0700680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
683static struct mc_device net_mc = {
684 .name = "eth",
685 .config = net_config,
686 .get_config = NULL,
Jeff Dike29d56cf2005-06-25 14:55:25 -0700687 .id = net_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 .remove = net_remove,
689};
690
691static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
692 void *ptr)
693{
694 struct in_ifaddr *ifa = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct net_device *dev = ifa->ifa_dev->dev;
696 struct uml_net_private *lp;
697 void (*proc)(unsigned char *, unsigned char *, void *);
698 unsigned char addr_buf[4], netmask_buf[4];
699
700 if(dev->open != uml_net_open) return(NOTIFY_DONE);
701
702 lp = dev->priv;
703
704 proc = NULL;
705 switch (event){
706 case NETDEV_UP:
707 proc = lp->add_address;
708 break;
709 case NETDEV_DOWN:
710 proc = lp->delete_address;
711 break;
712 }
713 if(proc != NULL){
Bodo Stroesser0e764222005-11-07 00:58:47 -0800714 memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
715 memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 (*proc)(addr_buf, netmask_buf, &lp->user);
717 }
718 return(NOTIFY_DONE);
719}
720
721struct notifier_block uml_inetaddr_notifier = {
722 .notifier_call = uml_inetaddr_event,
723};
724
725static int uml_net_init(void)
726{
727 struct list_head *ele;
728 struct uml_net_private *lp;
729 struct in_device *ip;
730 struct in_ifaddr *in;
731
732 mconsole_register_dev(&net_mc);
733 register_inetaddr_notifier(&uml_inetaddr_notifier);
734
735 /* Devices may have been opened already, so the uml_inetaddr_notifier
736 * didn't get a chance to run for them. This fakes it so that
737 * addresses which have already been set up get handled properly.
738 */
739 list_for_each(ele, &opened){
740 lp = list_entry(ele, struct uml_net_private, list);
741 ip = lp->dev->ip_ptr;
742 if(ip == NULL) continue;
743 in = ip->ifa_list;
744 while(in != NULL){
745 uml_inetaddr_event(NULL, NETDEV_UP, in);
746 in = in->ifa_next;
747 }
748 }
749
750 return(0);
751}
752
753__initcall(uml_net_init);
754
755static void close_devices(void)
756{
757 struct list_head *ele;
758 struct uml_net_private *lp;
759
760 list_for_each(ele, &opened){
761 lp = list_entry(ele, struct uml_net_private, list);
Jeff Dike8d93c702006-01-06 00:19:06 -0800762 free_irq(lp->dev->irq, lp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if((lp->close != NULL) && (lp->fd >= 0))
764 (*lp->close)(lp->fd, &lp->user);
765 if(lp->remove != NULL) (*lp->remove)(&lp->user);
766 }
767}
768
769__uml_exitcall(close_devices);
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
772{
773 if((skb != NULL) && (skb_tailroom(skb) < extra)){
774 struct sk_buff *skb2;
775
776 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
777 dev_kfree_skb(skb);
778 skb = skb2;
779 }
780 if(skb != NULL) skb_put(skb, extra);
781 return(skb);
782}
783
784void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
785 void *),
786 void *arg)
787{
788 struct net_device *dev = d;
789 struct in_device *ip = dev->ip_ptr;
790 struct in_ifaddr *in;
791 unsigned char address[4], netmask[4];
792
793 if(ip == NULL) return;
794 in = ip->ifa_list;
795 while(in != NULL){
Bodo Stroesser0e764222005-11-07 00:58:47 -0800796 memcpy(address, &in->ifa_address, sizeof(address));
797 memcpy(netmask, &in->ifa_mask, sizeof(netmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 (*cb)(address, netmask, arg);
799 in = in->ifa_next;
800 }
801}
802
803int dev_netmask(void *d, void *m)
804{
805 struct net_device *dev = d;
806 struct in_device *ip = dev->ip_ptr;
807 struct in_ifaddr *in;
Al Viroa144ea42006-09-28 18:00:55 -0700808 __be32 *mask_out = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 if(ip == NULL)
811 return(1);
812
813 in = ip->ifa_list;
814 if(in == NULL)
815 return(1);
816
817 *mask_out = in->ifa_mask;
818 return(0);
819}
820
821void *get_output_buffer(int *len_out)
822{
823 void *ret;
824
825 ret = (void *) __get_free_pages(GFP_KERNEL, 0);
826 if(ret) *len_out = PAGE_SIZE;
827 else *len_out = 0;
828 return(ret);
829}
830
831void free_output_buffer(void *buffer)
832{
833 free_pages((unsigned long) buffer, 0);
834}
835
836int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
837 char **gate_addr)
838{
839 char *remain;
840
841 remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
842 if(remain != NULL){
843 printk("tap_setup_common - Extra garbage on specification : "
844 "'%s'\n", remain);
845 return(1);
846 }
847
848 return(0);
849}
850
851unsigned short eth_protocol(struct sk_buff *skb)
852{
853 return(eth_type_trans(skb, skb->dev));
854}
855
856/*
857 * Overrides for Emacs so that we follow Linus's tabbing style.
858 * Emacs will notice this stuff at the end of the file and automatically
859 * adjust the settings for this buffer only. This must remain at the end
860 * of the file.
861 * ---------------------------------------------------------------------------
862 * Local variables:
863 * c-file-style: "linux"
864 * End:
865 */