blob: 4a7966b219317440b991de2961130add5d94299d [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
117 spin_lock(&lp->lock);
118
119 if(lp->fd >= 0){
120 err = -ENXIO;
121 goto out;
122 }
123
124 if(!lp->have_mac){
Bodo Stroesser0e764222005-11-07 00:58:47 -0800125 dev_ip_addr(dev, &lp->mac[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 set_ether_mac(dev, lp->mac);
127 }
128
129 lp->fd = (*lp->open)(&lp->user);
130 if(lp->fd < 0){
131 err = lp->fd;
132 goto out;
133 }
134
135 err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
Thomas Gleixnerbd6aa652006-07-01 19:29:27 -0700136 IRQF_DISABLED | IRQF_SHARED, dev->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if(err != 0){
138 printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 err = -ENETUNREACH;
Jeff Dike14d9ead2006-02-07 12:58:42 -0800140 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142
143 lp->tl.data = (unsigned long) &lp->user;
144 netif_start_queue(dev);
145
146 /* clear buffer - it can happen that the host side of the interface
147 * is full when we get here. In this case, new data is never queued,
148 * SIGIOs never arrive, and the net never works.
149 */
150 while((err = uml_net_rx(dev)) > 0) ;
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 spin_unlock(&lp->lock);
Jeff Dike14d9ead2006-02-07 12:58:42 -0800153
154 spin_lock(&opened_lock);
155 list_add(&lp->list, &opened);
156 spin_unlock(&opened_lock);
157
158 return 0;
159out_close:
160 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
161 lp->fd = -1;
162out:
163 spin_unlock(&lp->lock);
164 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167static int uml_net_close(struct net_device *dev)
168{
169 struct uml_net_private *lp = dev->priv;
170
171 netif_stop_queue(dev);
172 spin_lock(&lp->lock);
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 free_irq(dev->irq, dev);
175 if(lp->close != NULL)
176 (*lp->close)(lp->fd, &lp->user);
177 lp->fd = -1;
178
179 spin_unlock(&lp->lock);
Jeff Dike14d9ead2006-02-07 12:58:42 -0800180
181 spin_lock(&opened_lock);
182 list_del(&lp->list);
183 spin_unlock(&opened_lock);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return 0;
186}
187
188static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
189{
190 struct uml_net_private *lp = dev->priv;
191 unsigned long flags;
192 int len;
193
194 netif_stop_queue(dev);
195
196 spin_lock_irqsave(&lp->lock, flags);
197
198 len = (*lp->write)(lp->fd, &skb, lp);
199
200 if(len == skb->len) {
201 lp->stats.tx_packets++;
202 lp->stats.tx_bytes += skb->len;
203 dev->trans_start = jiffies;
204 netif_start_queue(dev);
205
206 /* this is normally done in the interrupt when tx finishes */
207 netif_wake_queue(dev);
208 }
209 else if(len == 0){
210 netif_start_queue(dev);
211 lp->stats.tx_dropped++;
212 }
213 else {
214 netif_start_queue(dev);
215 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
216 }
217
218 spin_unlock_irqrestore(&lp->lock, flags);
219
220 dev_kfree_skb(skb);
221
222 return 0;
223}
224
225static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
226{
227 struct uml_net_private *lp = dev->priv;
228 return &lp->stats;
229}
230
231static void uml_net_set_multicast_list(struct net_device *dev)
232{
233 if (dev->flags & IFF_PROMISC) return;
234 else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
235 else dev->flags &= ~IFF_ALLMULTI;
236}
237
238static void uml_net_tx_timeout(struct net_device *dev)
239{
240 dev->trans_start = jiffies;
241 netif_wake_queue(dev);
242}
243
244static int uml_net_set_mac(struct net_device *dev, void *addr)
245{
246 struct uml_net_private *lp = dev->priv;
247 struct sockaddr *hwaddr = addr;
248
249 spin_lock(&lp->lock);
Paolo 'Blaisorblade' Giarrussobf61f502006-09-25 23:33:07 -0700250 set_ether_mac(dev, hwaddr->sa_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 spin_unlock(&lp->lock);
252
253 return(0);
254}
255
256static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
257{
258 struct uml_net_private *lp = dev->priv;
259 int err = 0;
260
261 spin_lock(&lp->lock);
262
263 new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
264 if(new_mtu < 0){
265 err = new_mtu;
266 goto out;
267 }
268
269 dev->mtu = new_mtu;
270
271 out:
272 spin_unlock(&lp->lock);
273 return err;
274}
275
Christoph Hellwig6d387482005-11-07 06:21:21 +0100276static void uml_net_get_drvinfo(struct net_device *dev,
277 struct ethtool_drvinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Christoph Hellwig6d387482005-11-07 06:21:21 +0100279 strcpy(info->driver, DRIVER_NAME);
280 strcpy(info->version, "42");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
Christoph Hellwig6d387482005-11-07 06:21:21 +0100283static struct ethtool_ops uml_net_ethtool_ops = {
284 .get_drvinfo = uml_net_get_drvinfo,
285 .get_link = ethtool_op_get_link,
286};
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288void uml_net_user_timer_expire(unsigned long _conn)
289{
290#ifdef undef
291 struct connection *conn = (struct connection *)_conn;
292
293 dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
294 do_connect(conn);
295#endif
296}
297
298static DEFINE_SPINLOCK(devices_lock);
Jeff Dike90107722006-01-06 00:18:54 -0800299static LIST_HEAD(devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Russell King3ae5eae2005-11-09 22:32:44 +0000301static struct platform_driver uml_net_driver = {
302 .driver = {
303 .name = DRIVER_NAME,
304 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305};
306static int driver_registered;
307
308static int eth_configure(int n, void *init, char *mac,
309 struct transport *transport)
310{
311 struct uml_net *device;
312 struct net_device *dev;
313 struct uml_net_private *lp;
314 int save, err, size;
315
316 size = transport->private_size + sizeof(struct uml_net_private) +
317 sizeof(((struct uml_net_private *) 0)->user);
318
319 device = kmalloc(sizeof(*device), GFP_KERNEL);
320 if (device == NULL) {
321 printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
322 return(1);
323 }
324
325 memset(device, 0, sizeof(*device));
326 INIT_LIST_HEAD(&device->list);
327 device->index = n;
328
329 spin_lock(&devices_lock);
330 list_add(&device->list, &devices);
331 spin_unlock(&devices_lock);
332
333 if (setup_etheraddr(mac, device->mac))
334 device->have_mac = 1;
335
336 printk(KERN_INFO "Netdevice %d ", n);
337 if (device->have_mac)
338 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
339 device->mac[0], device->mac[1],
340 device->mac[2], device->mac[3],
341 device->mac[4], device->mac[5]);
342 printk(": ");
343 dev = alloc_etherdev(size);
344 if (dev == NULL) {
345 printk(KERN_ERR "eth_configure: failed to allocate device\n");
346 return 1;
347 }
348
Paolo 'Blaisorblade' Giarrussoe56a7882006-01-18 17:42:55 -0800349 lp = dev->priv;
350 /* This points to the transport private data. It's still clear, but we
351 * must memset it to 0 *now*. Let's help the drivers. */
352 memset(lp, 0, size);
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 /* sysfs register */
355 if (!driver_registered) {
Russell King3ae5eae2005-11-09 22:32:44 +0000356 platform_driver_register(&uml_net_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 driver_registered = 1;
358 }
359 device->pdev.id = n;
360 device->pdev.name = DRIVER_NAME;
361 platform_device_register(&device->pdev);
362 SET_NETDEV_DEV(dev,&device->pdev.dev);
363
364 /* If this name ends up conflicting with an existing registered
365 * netdevice, that is OK, register_netdev{,ice}() will notice this
366 * and fail.
367 */
368 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
369 device->dev = dev;
370
371 (*transport->kern->init)(dev, init);
372
373 dev->mtu = transport->user->max_packet;
374 dev->open = uml_net_open;
375 dev->hard_start_xmit = uml_net_start_xmit;
376 dev->stop = uml_net_close;
377 dev->get_stats = uml_net_get_stats;
378 dev->set_multicast_list = uml_net_set_multicast_list;
379 dev->tx_timeout = uml_net_tx_timeout;
380 dev->set_mac_address = uml_net_set_mac;
381 dev->change_mtu = uml_net_change_mtu;
Christoph Hellwig6d387482005-11-07 06:21:21 +0100382 dev->ethtool_ops = &uml_net_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 dev->watchdog_timeo = (HZ >> 1);
384 dev->irq = UM_ETH_IRQ;
385
386 rtnl_lock();
387 err = register_netdevice(dev);
388 rtnl_unlock();
389 if (err) {
390 device->dev = NULL;
391 /* XXX: should we call ->remove() here? */
392 free_netdev(dev);
393 return 1;
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /* lp.user is the first four bytes of the transport data, which
397 * has already been initialized. This structure assignment will
398 * overwrite that, so we make sure that .user gets overwritten with
399 * what it already has.
400 */
401 save = lp->user[0];
402 *lp = ((struct uml_net_private)
403 { .list = LIST_HEAD_INIT(lp->list),
404 .dev = dev,
405 .fd = -1,
406 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
407 .have_mac = device->have_mac,
408 .protocol = transport->kern->protocol,
409 .open = transport->user->open,
410 .close = transport->user->close,
411 .remove = transport->user->remove,
412 .read = transport->kern->read,
413 .write = transport->kern->write,
414 .add_address = transport->user->add_address,
415 .delete_address = transport->user->delete_address,
416 .set_mtu = transport->user->set_mtu,
417 .user = { save } });
418
419 init_timer(&lp->tl);
420 spin_lock_init(&lp->lock);
421 lp->tl.function = uml_net_user_timer_expire;
422 if (lp->have_mac)
423 memcpy(lp->mac, device->mac, sizeof(lp->mac));
424
425 if (transport->user->init)
426 (*transport->user->init)(&lp->user, dev);
427
428 if (device->have_mac)
429 set_ether_mac(dev, device->mac);
430
Jeff Dike14d9ead2006-02-07 12:58:42 -0800431 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434static struct uml_net *find_device(int n)
435{
436 struct uml_net *device;
437 struct list_head *ele;
438
439 spin_lock(&devices_lock);
440 list_for_each(ele, &devices){
441 device = list_entry(ele, struct uml_net, list);
442 if(device->index == n)
443 goto out;
444 }
445 device = NULL;
446 out:
447 spin_unlock(&devices_lock);
448 return(device);
449}
450
451static int eth_parse(char *str, int *index_out, char **str_out)
452{
453 char *end;
454 int n;
455
456 n = simple_strtoul(str, &end, 0);
457 if(end == str){
458 printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
459 return(1);
460 }
461 if(n < 0){
462 printk(KERN_ERR "eth_setup: device %d is negative\n", n);
463 return(1);
464 }
465 str = end;
466 if(*str != '='){
467 printk(KERN_ERR
468 "eth_setup: expected '=' after device number\n");
469 return(1);
470 }
471 str++;
472 if(find_device(n)){
473 printk(KERN_ERR "eth_setup: Device %d already configured\n",
474 n);
475 return(1);
476 }
477 if(index_out) *index_out = n;
478 *str_out = str;
479 return(0);
480}
481
482struct eth_init {
483 struct list_head list;
484 char *init;
485 int index;
486};
487
488/* Filled in at boot time. Will need locking if the transports become
489 * modular.
490 */
491struct list_head transports = LIST_HEAD_INIT(transports);
492
493/* Filled in during early boot */
494struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
495
496static int check_transport(struct transport *transport, char *eth, int n,
497 void **init_out, char **mac_out)
498{
499 int len;
500
501 len = strlen(transport->name);
502 if(strncmp(eth, transport->name, len))
503 return(0);
504
505 eth += len;
506 if(*eth == ',')
507 eth++;
508 else if(*eth != '\0')
509 return(0);
510
511 *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
512 if(*init_out == NULL)
513 return(1);
514
515 if(!transport->setup(eth, mac_out, *init_out)){
516 kfree(*init_out);
517 *init_out = NULL;
518 }
519 return(1);
520}
521
522void register_transport(struct transport *new)
523{
524 struct list_head *ele, *next;
525 struct eth_init *eth;
526 void *init;
527 char *mac = NULL;
528 int match;
529
530 list_add(&new->list, &transports);
531
532 list_for_each_safe(ele, next, &eth_cmd_line){
533 eth = list_entry(ele, struct eth_init, list);
534 match = check_transport(new, eth->init, eth->index, &init,
535 &mac);
536 if(!match)
537 continue;
538 else if(init != NULL){
539 eth_configure(eth->index, init, mac, new);
540 kfree(init);
541 }
542 list_del(&eth->list);
543 }
544}
545
546static int eth_setup_common(char *str, int index)
547{
548 struct list_head *ele;
549 struct transport *transport;
550 void *init;
551 char *mac = NULL;
552
553 list_for_each(ele, &transports){
554 transport = list_entry(ele, struct transport, list);
555 if(!check_transport(transport, str, index, &init, &mac))
556 continue;
557 if(init != NULL){
558 eth_configure(index, init, mac, transport);
559 kfree(init);
560 }
561 return(1);
562 }
563 return(0);
564}
565
566static int eth_setup(char *str)
567{
568 struct eth_init *new;
569 int n, err;
570
571 err = eth_parse(str, &n, &str);
572 if(err) return(1);
573
574 new = alloc_bootmem(sizeof(new));
575 if (new == NULL){
576 printk("eth_init : alloc_bootmem failed\n");
577 return(1);
578 }
579
580 INIT_LIST_HEAD(&new->list);
581 new->index = n;
582 new->init = str;
583
584 list_add_tail(&new->list, &eth_cmd_line);
585 return(1);
586}
587
588__setup("eth", eth_setup);
589__uml_help(eth_setup,
590"eth[0-9]+=<transport>,<options>\n"
591" Configure a network device.\n\n"
592);
593
594#if 0
595static int eth_init(void)
596{
597 struct list_head *ele, *next;
598 struct eth_init *eth;
599
600 list_for_each_safe(ele, next, &eth_cmd_line){
601 eth = list_entry(ele, struct eth_init, list);
602
603 if(eth_setup_common(eth->init, eth->index))
604 list_del(&eth->list);
605 }
606
607 return(1);
608}
609__initcall(eth_init);
610#endif
611
612static int net_config(char *str)
613{
614 int n, err;
615
616 err = eth_parse(str, &n, &str);
617 if(err) return(err);
618
Jeff Dike970d6e32006-01-06 00:18:48 -0800619 str = kstrdup(str, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if(str == NULL){
621 printk(KERN_ERR "net_config failed to strdup string\n");
622 return(-1);
623 }
624 err = !eth_setup_common(str, n);
625 if(err)
626 kfree(str);
627 return(err);
628}
629
Jeff Dike29d56cf2005-06-25 14:55:25 -0700630static int net_id(char **str, int *start_out, int *end_out)
631{
632 char *end;
633 int n;
634
635 n = simple_strtoul(*str, &end, 0);
636 if((*end != '\0') || (end == *str))
637 return -1;
638
639 *start_out = n;
640 *end_out = n;
641 *str = end;
642 return n;
643}
644
645static int net_remove(int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 struct uml_net *device;
648 struct net_device *dev;
649 struct uml_net_private *lp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 device = find_device(n);
652 if(device == NULL)
Jeff Dike29d56cf2005-06-25 14:55:25 -0700653 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 dev = device->dev;
656 lp = dev->priv;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700657 if(lp->fd > 0)
658 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if(lp->remove != NULL) (*lp->remove)(&lp->user);
660 unregister_netdev(dev);
661 platform_device_unregister(&device->pdev);
662
663 list_del(&device->list);
664 kfree(device);
665 free_netdev(dev);
Jeff Dike29d56cf2005-06-25 14:55:25 -0700666 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669static struct mc_device net_mc = {
670 .name = "eth",
671 .config = net_config,
672 .get_config = NULL,
Jeff Dike29d56cf2005-06-25 14:55:25 -0700673 .id = net_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 .remove = net_remove,
675};
676
677static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
678 void *ptr)
679{
680 struct in_ifaddr *ifa = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 struct net_device *dev = ifa->ifa_dev->dev;
682 struct uml_net_private *lp;
683 void (*proc)(unsigned char *, unsigned char *, void *);
684 unsigned char addr_buf[4], netmask_buf[4];
685
686 if(dev->open != uml_net_open) return(NOTIFY_DONE);
687
688 lp = dev->priv;
689
690 proc = NULL;
691 switch (event){
692 case NETDEV_UP:
693 proc = lp->add_address;
694 break;
695 case NETDEV_DOWN:
696 proc = lp->delete_address;
697 break;
698 }
699 if(proc != NULL){
Bodo Stroesser0e764222005-11-07 00:58:47 -0800700 memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
701 memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 (*proc)(addr_buf, netmask_buf, &lp->user);
703 }
704 return(NOTIFY_DONE);
705}
706
707struct notifier_block uml_inetaddr_notifier = {
708 .notifier_call = uml_inetaddr_event,
709};
710
711static int uml_net_init(void)
712{
713 struct list_head *ele;
714 struct uml_net_private *lp;
715 struct in_device *ip;
716 struct in_ifaddr *in;
717
718 mconsole_register_dev(&net_mc);
719 register_inetaddr_notifier(&uml_inetaddr_notifier);
720
721 /* Devices may have been opened already, so the uml_inetaddr_notifier
722 * didn't get a chance to run for them. This fakes it so that
723 * addresses which have already been set up get handled properly.
724 */
725 list_for_each(ele, &opened){
726 lp = list_entry(ele, struct uml_net_private, list);
727 ip = lp->dev->ip_ptr;
728 if(ip == NULL) continue;
729 in = ip->ifa_list;
730 while(in != NULL){
731 uml_inetaddr_event(NULL, NETDEV_UP, in);
732 in = in->ifa_next;
733 }
734 }
735
736 return(0);
737}
738
739__initcall(uml_net_init);
740
741static void close_devices(void)
742{
743 struct list_head *ele;
744 struct uml_net_private *lp;
745
746 list_for_each(ele, &opened){
747 lp = list_entry(ele, struct uml_net_private, list);
Jeff Dike8d93c702006-01-06 00:19:06 -0800748 free_irq(lp->dev->irq, lp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if((lp->close != NULL) && (lp->fd >= 0))
750 (*lp->close)(lp->fd, &lp->user);
751 if(lp->remove != NULL) (*lp->remove)(&lp->user);
752 }
753}
754
755__uml_exitcall(close_devices);
756
757int setup_etheraddr(char *str, unsigned char *addr)
758{
759 char *end;
760 int i;
761
762 if(str == NULL)
763 return(0);
764 for(i=0;i<6;i++){
765 addr[i] = simple_strtoul(str, &end, 16);
766 if((end == str) ||
767 ((*end != ':') && (*end != ',') && (*end != '\0'))){
768 printk(KERN_ERR
769 "setup_etheraddr: failed to parse '%s' "
770 "as an ethernet address\n", str);
771 return(0);
772 }
773 str = end + 1;
774 }
775 if(addr[0] & 1){
776 printk(KERN_ERR
777 "Attempt to assign a broadcast ethernet address to a "
778 "device disallowed\n");
779 return(0);
780 }
781 return(1);
782}
783
Bodo Stroesser0e764222005-11-07 00:58:47 -0800784void dev_ip_addr(void *d, unsigned char *bin_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 struct net_device *dev = d;
787 struct in_device *ip = dev->ip_ptr;
788 struct in_ifaddr *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 if((ip == NULL) || ((in = ip->ifa_list) == NULL)){
791 printk(KERN_WARNING "dev_ip_addr - device not assigned an "
792 "IP address\n");
793 return;
794 }
Bodo Stroesser0e764222005-11-07 00:58:47 -0800795 memcpy(bin_buf, &in->ifa_address, sizeof(in->ifa_address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
799{
800 if((skb != NULL) && (skb_tailroom(skb) < extra)){
801 struct sk_buff *skb2;
802
803 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
804 dev_kfree_skb(skb);
805 skb = skb2;
806 }
807 if(skb != NULL) skb_put(skb, extra);
808 return(skb);
809}
810
811void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
812 void *),
813 void *arg)
814{
815 struct net_device *dev = d;
816 struct in_device *ip = dev->ip_ptr;
817 struct in_ifaddr *in;
818 unsigned char address[4], netmask[4];
819
820 if(ip == NULL) return;
821 in = ip->ifa_list;
822 while(in != NULL){
Bodo Stroesser0e764222005-11-07 00:58:47 -0800823 memcpy(address, &in->ifa_address, sizeof(address));
824 memcpy(netmask, &in->ifa_mask, sizeof(netmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 (*cb)(address, netmask, arg);
826 in = in->ifa_next;
827 }
828}
829
830int dev_netmask(void *d, void *m)
831{
832 struct net_device *dev = d;
833 struct in_device *ip = dev->ip_ptr;
834 struct in_ifaddr *in;
835 __u32 *mask_out = m;
836
837 if(ip == NULL)
838 return(1);
839
840 in = ip->ifa_list;
841 if(in == NULL)
842 return(1);
843
844 *mask_out = in->ifa_mask;
845 return(0);
846}
847
848void *get_output_buffer(int *len_out)
849{
850 void *ret;
851
852 ret = (void *) __get_free_pages(GFP_KERNEL, 0);
853 if(ret) *len_out = PAGE_SIZE;
854 else *len_out = 0;
855 return(ret);
856}
857
858void free_output_buffer(void *buffer)
859{
860 free_pages((unsigned long) buffer, 0);
861}
862
863int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
864 char **gate_addr)
865{
866 char *remain;
867
868 remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
869 if(remain != NULL){
870 printk("tap_setup_common - Extra garbage on specification : "
871 "'%s'\n", remain);
872 return(1);
873 }
874
875 return(0);
876}
877
878unsigned short eth_protocol(struct sk_buff *skb)
879{
880 return(eth_type_trans(skb, skb->dev));
881}
882
883/*
884 * Overrides for Emacs so that we follow Linus's tabbing style.
885 * Emacs will notice this stuff at the end of the file and automatically
886 * adjust the settings for this buffer only. This must remain at the end
887 * of the file.
888 * ---------------------------------------------------------------------------
889 * Local variables:
890 * c-file-style: "linux"
891 * End:
892 */