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