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.
8 #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"
22 #include "linux/platform_device.h"
23 #include "asm/uaccess.h"
24 #include "kern_util.h"
27 #include "mconsole_kern.h"
32 static inline void set_ether_mac(struct net_device *dev, unsigned char *addr)
34 memcpy(dev->dev_addr, addr, ETH_ALEN);
37 #define DRIVER_NAME "uml-netdev"
39 static DEFINE_SPINLOCK(opened_lock);
40 static LIST_HEAD(opened);
42 static int uml_net_rx(struct net_device *dev)
44 struct uml_net_private *lp = dev->priv;
48 /* If we can't allocate memory, try again next round. */
49 skb = dev_alloc_skb(dev->mtu);
51 lp->stats.rx_dropped++;
56 skb_put(skb, dev->mtu);
57 skb_reset_mac_header(skb);
58 pkt_len = (*lp->read)(lp->fd, &skb, lp);
61 skb_trim(skb, pkt_len);
62 skb->protocol = (*lp->protocol)(skb);
65 lp->stats.rx_bytes += skb->len;
66 lp->stats.rx_packets++;
74 static void uml_dev_close(struct work_struct *work)
76 struct uml_net_private *lp =
77 container_of(work, struct uml_net_private, work);
81 irqreturn_t uml_net_interrupt(int irq, void *dev_id)
83 struct net_device *dev = dev_id;
84 struct uml_net_private *lp = dev->priv;
87 if(!netif_running(dev))
91 while((err = uml_net_rx(dev)) > 0) ;
94 "Device '%s' read returned %d, shutting it down\n",
96 /* dev_close can't be called in interrupt context, and takes
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 * Furthermore, the workqueue will not re-enqueue an already
102 * enqueued work item. */
103 schedule_work(&lp->work);
106 reactivate_fd(lp->fd, UM_ETH_IRQ);
109 spin_unlock(&lp->lock);
113 static int uml_net_open(struct net_device *dev)
115 struct uml_net_private *lp = dev->priv;
123 lp->fd = (*lp->open)(&lp->user);
129 err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
130 IRQF_DISABLED | IRQF_SHARED, dev->name, dev);
132 printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
137 lp->tl.data = (unsigned long) &lp->user;
138 netif_start_queue(dev);
140 /* clear buffer - it can happen that the host side of the interface
141 * is full when we get here. In this case, new data is never queued,
142 * SIGIOs never arrive, and the net never works.
144 while((err = uml_net_rx(dev)) > 0) ;
146 spin_lock(&opened_lock);
147 list_add(&lp->list, &opened);
148 spin_unlock(&opened_lock);
152 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
158 static int uml_net_close(struct net_device *dev)
160 struct uml_net_private *lp = dev->priv;
162 netif_stop_queue(dev);
164 free_irq(dev->irq, dev);
165 if(lp->close != NULL)
166 (*lp->close)(lp->fd, &lp->user);
169 spin_lock(&opened_lock);
171 spin_unlock(&opened_lock);
176 static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
178 struct uml_net_private *lp = dev->priv;
182 netif_stop_queue(dev);
184 spin_lock_irqsave(&lp->lock, flags);
186 len = (*lp->write)(lp->fd, &skb, lp);
188 if(len == skb->len) {
189 lp->stats.tx_packets++;
190 lp->stats.tx_bytes += skb->len;
191 dev->trans_start = jiffies;
192 netif_start_queue(dev);
194 /* this is normally done in the interrupt when tx finishes */
195 netif_wake_queue(dev);
198 netif_start_queue(dev);
199 lp->stats.tx_dropped++;
202 netif_start_queue(dev);
203 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
206 spin_unlock_irqrestore(&lp->lock, flags);
213 static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
215 struct uml_net_private *lp = dev->priv;
219 static void uml_net_set_multicast_list(struct net_device *dev)
221 if (dev->flags & IFF_PROMISC) return;
222 else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
223 else dev->flags &= ~IFF_ALLMULTI;
226 static void uml_net_tx_timeout(struct net_device *dev)
228 dev->trans_start = jiffies;
229 netif_wake_queue(dev);
232 static int uml_net_set_mac(struct net_device *dev, void *addr)
234 struct uml_net_private *lp = dev->priv;
235 struct sockaddr *hwaddr = addr;
237 spin_lock_irq(&lp->lock);
238 set_ether_mac(dev, hwaddr->sa_data);
239 spin_unlock_irq(&lp->lock);
244 static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
246 struct uml_net_private *lp = dev->priv;
249 spin_lock_irq(&lp->lock);
251 new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
260 spin_unlock_irq(&lp->lock);
264 static void uml_net_get_drvinfo(struct net_device *dev,
265 struct ethtool_drvinfo *info)
267 strcpy(info->driver, DRIVER_NAME);
268 strcpy(info->version, "42");
271 static struct ethtool_ops uml_net_ethtool_ops = {
272 .get_drvinfo = uml_net_get_drvinfo,
273 .get_link = ethtool_op_get_link,
276 void uml_net_user_timer_expire(unsigned long _conn)
279 struct connection *conn = (struct connection *)_conn;
281 dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
286 static void setup_etheraddr(char *str, unsigned char *addr, char *name)
295 addr[i] = simple_strtoul(str, &end, 16);
297 ((*end != ':') && (*end != ',') && (*end != '\0'))){
299 "setup_etheraddr: failed to parse '%s' "
300 "as an ethernet address\n", str);
305 if (is_multicast_ether_addr(addr)) {
307 "Attempt to assign a multicast ethernet address to a "
308 "device disallowed\n");
311 if (!is_valid_ether_addr(addr)) {
313 "Attempt to assign an invalid ethernet address to a "
314 "device disallowed\n");
317 if (!is_local_ether_addr(addr)) {
319 "Warning: attempt to assign a globally valid ethernet "
320 "address to a device\n");
321 printk(KERN_WARNING "You should better enable the 2nd "
322 "rightmost bit in the first byte of the MAC,\n");
323 printk(KERN_WARNING "i.e. %02x:%02x:%02x:%02x:%02x:%02x\n",
324 addr[0] | 0x02, addr[1], addr[2], addr[3], addr[4],
332 "Choosing a random ethernet address for device %s\n", name);
333 random_ether_addr(addr);
336 static DEFINE_SPINLOCK(devices_lock);
337 static LIST_HEAD(devices);
339 static struct platform_driver uml_net_driver = {
344 static int driver_registered;
346 static void net_device_release(struct device *dev)
348 struct uml_net *device = dev->driver_data;
349 struct net_device *netdev = device->dev;
350 struct uml_net_private *lp = netdev->priv;
352 if(lp->remove != NULL)
353 (*lp->remove)(&lp->user);
354 list_del(&device->list);
359 static void eth_configure(int n, void *init, char *mac,
360 struct transport *transport)
362 struct uml_net *device;
363 struct net_device *dev;
364 struct uml_net_private *lp;
367 size = transport->private_size + sizeof(struct uml_net_private);
369 device = kzalloc(sizeof(*device), GFP_KERNEL);
370 if (device == NULL) {
371 printk(KERN_ERR "eth_configure failed to allocate struct "
376 dev = alloc_etherdev(size);
378 printk(KERN_ERR "eth_configure: failed to allocate struct "
379 "net_device for eth%d\n", n);
380 goto out_free_device;
383 INIT_LIST_HEAD(&device->list);
386 /* If this name ends up conflicting with an existing registered
387 * netdevice, that is OK, register_netdev{,ice}() will notice this
390 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
392 setup_etheraddr(mac, device->mac, dev->name);
394 printk(KERN_INFO "Netdevice %d ", n);
395 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
396 device->mac[0], device->mac[1],
397 device->mac[2], device->mac[3],
398 device->mac[4], device->mac[5]);
402 /* This points to the transport private data. It's still clear, but we
403 * must memset it to 0 *now*. Let's help the drivers. */
405 INIT_WORK(&lp->work, uml_dev_close);
408 if (!driver_registered) {
409 platform_driver_register(¨_net_driver);
410 driver_registered = 1;
413 device->pdev.name = DRIVER_NAME;
414 device->pdev.dev.release = net_device_release;
415 device->pdev.dev.driver_data = device;
416 if(platform_device_register(&device->pdev))
417 goto out_free_netdev;
418 SET_NETDEV_DEV(dev,&device->pdev.dev);
423 * These just fill in a data structure, so there's no failure
424 * to be worried about.
426 (*transport->kern->init)(dev, init);
428 *lp = ((struct uml_net_private)
429 { .list = LIST_HEAD_INIT(lp->list),
432 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
433 .protocol = transport->kern->protocol,
434 .open = transport->user->open,
435 .close = transport->user->close,
436 .remove = transport->user->remove,
437 .read = transport->kern->read,
438 .write = transport->kern->write,
439 .add_address = transport->user->add_address,
440 .delete_address = transport->user->delete_address,
441 .set_mtu = transport->user->set_mtu });
444 spin_lock_init(&lp->lock);
445 lp->tl.function = uml_net_user_timer_expire;
446 memcpy(lp->mac, device->mac, sizeof(lp->mac));
448 if ((transport->user->init != NULL) &&
449 ((*transport->user->init)(&lp->user, dev) != 0))
452 set_ether_mac(dev, device->mac);
453 dev->mtu = transport->user->max_packet;
454 dev->open = uml_net_open;
455 dev->hard_start_xmit = uml_net_start_xmit;
456 dev->stop = uml_net_close;
457 dev->get_stats = uml_net_get_stats;
458 dev->set_multicast_list = uml_net_set_multicast_list;
459 dev->tx_timeout = uml_net_tx_timeout;
460 dev->set_mac_address = uml_net_set_mac;
461 dev->change_mtu = uml_net_change_mtu;
462 dev->ethtool_ops = ¨_net_ethtool_ops;
463 dev->watchdog_timeo = (HZ >> 1);
464 dev->irq = UM_ETH_IRQ;
467 err = register_netdevice(dev);
470 goto out_undo_user_init;
472 spin_lock(&devices_lock);
473 list_add(&device->list, &devices);
474 spin_unlock(&devices_lock);
479 if (transport->user->remove != NULL)
480 (*transport->user->remove)(&lp->user);
482 platform_device_unregister(&device->pdev);
483 return; /* platform_device_unregister frees dev and device */
490 static struct uml_net *find_device(int n)
492 struct uml_net *device;
493 struct list_head *ele;
495 spin_lock(&devices_lock);
496 list_for_each(ele, &devices){
497 device = list_entry(ele, struct uml_net, list);
498 if(device->index == n)
503 spin_unlock(&devices_lock);
507 static int eth_parse(char *str, int *index_out, char **str_out,
511 int n, err = -EINVAL;;
513 n = simple_strtoul(str, &end, 0);
515 *error_out = "Bad device number";
521 *error_out = "Expected '=' after device number";
527 *error_out = "Device already configured";
537 struct list_head list;
542 static DEFINE_SPINLOCK(transports_lock);
543 static LIST_HEAD(transports);
545 /* Filled in during early boot */
546 static LIST_HEAD(eth_cmd_line);
548 static int check_transport(struct transport *transport, char *eth, int n,
549 void **init_out, char **mac_out)
553 len = strlen(transport->name);
554 if(strncmp(eth, transport->name, len))
560 else if(*eth != '\0')
563 *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
564 if(*init_out == NULL)
567 if(!transport->setup(eth, mac_out, *init_out)){
574 void register_transport(struct transport *new)
576 struct list_head *ele, *next;
577 struct eth_init *eth;
582 spin_lock(&transports_lock);
583 BUG_ON(!list_empty(&new->list));
584 list_add(&new->list, &transports);
585 spin_unlock(&transports_lock);
587 list_for_each_safe(ele, next, ð_cmd_line){
588 eth = list_entry(ele, struct eth_init, list);
589 match = check_transport(new, eth->init, eth->index, &init,
593 else if(init != NULL){
594 eth_configure(eth->index, init, mac, new);
597 list_del(ð->list);
601 static int eth_setup_common(char *str, int index)
603 struct list_head *ele;
604 struct transport *transport;
609 spin_lock(&transports_lock);
610 list_for_each(ele, &transports){
611 transport = list_entry(ele, struct transport, list);
612 if(!check_transport(transport, str, index, &init, &mac))
615 eth_configure(index, init, mac, transport);
622 spin_unlock(&transports_lock);
626 static int eth_setup(char *str)
628 struct eth_init *new;
632 err = eth_parse(str, &n, &str, &error);
634 printk(KERN_ERR "eth_setup - Couldn't parse '%s' : %s\n",
639 new = alloc_bootmem(sizeof(*new));
641 printk("eth_init : alloc_bootmem failed\n");
645 INIT_LIST_HEAD(&new->list);
649 list_add_tail(&new->list, ð_cmd_line);
653 __setup("eth", eth_setup);
654 __uml_help(eth_setup,
655 "eth[0-9]+=<transport>,<options>\n"
656 " Configure a network device.\n\n"
659 static int net_config(char *str, char **error_out)
663 err = eth_parse(str, &n, &str, error_out);
667 /* This string is broken up and the pieces used by the underlying
668 * driver. So, it is freed only if eth_setup_common fails.
670 str = kstrdup(str, GFP_KERNEL);
672 *error_out = "net_config failed to strdup string";
675 err = !eth_setup_common(str, n);
681 static int net_id(char **str, int *start_out, int *end_out)
686 n = simple_strtoul(*str, &end, 0);
687 if((*end != '\0') || (end == *str))
696 static int net_remove(int n, char **error_out)
698 struct uml_net *device;
699 struct net_device *dev;
700 struct uml_net_private *lp;
702 device = find_device(n);
710 unregister_netdev(dev);
711 platform_device_unregister(&device->pdev);
716 static struct mc_device net_mc = {
717 .list = LIST_HEAD_INIT(net_mc.list),
719 .config = net_config,
722 .remove = net_remove,
725 static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
728 struct in_ifaddr *ifa = ptr;
729 struct net_device *dev = ifa->ifa_dev->dev;
730 struct uml_net_private *lp;
731 void (*proc)(unsigned char *, unsigned char *, void *);
732 unsigned char addr_buf[4], netmask_buf[4];
734 if(dev->open != uml_net_open)
742 proc = lp->add_address;
745 proc = lp->delete_address;
749 memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
750 memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
751 (*proc)(addr_buf, netmask_buf, &lp->user);
756 /* uml_net_init shouldn't be called twice on two CPUs at the same time */
757 struct notifier_block uml_inetaddr_notifier = {
758 .notifier_call = uml_inetaddr_event,
761 static int uml_net_init(void)
763 struct list_head *ele;
764 struct uml_net_private *lp;
765 struct in_device *ip;
766 struct in_ifaddr *in;
768 mconsole_register_dev(&net_mc);
769 register_inetaddr_notifier(¨_inetaddr_notifier);
771 /* Devices may have been opened already, so the uml_inetaddr_notifier
772 * didn't get a chance to run for them. This fakes it so that
773 * addresses which have already been set up get handled properly.
775 spin_lock(&opened_lock);
776 list_for_each(ele, &opened){
777 lp = list_entry(ele, struct uml_net_private, list);
778 ip = lp->dev->ip_ptr;
783 uml_inetaddr_event(NULL, NETDEV_UP, in);
787 spin_unlock(&opened_lock);
792 __initcall(uml_net_init);
794 static void close_devices(void)
796 struct list_head *ele;
797 struct uml_net_private *lp;
799 spin_lock(&opened_lock);
800 list_for_each(ele, &opened){
801 lp = list_entry(ele, struct uml_net_private, list);
802 free_irq(lp->dev->irq, lp->dev);
803 if((lp->close != NULL) && (lp->fd >= 0))
804 (*lp->close)(lp->fd, &lp->user);
805 if(lp->remove != NULL)
806 (*lp->remove)(&lp->user);
808 spin_unlock(&opened_lock);
811 __uml_exitcall(close_devices);
813 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
815 if((skb != NULL) && (skb_tailroom(skb) < extra)){
816 struct sk_buff *skb2;
818 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
822 if(skb != NULL) skb_put(skb, extra);
826 void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
830 struct net_device *dev = d;
831 struct in_device *ip = dev->ip_ptr;
832 struct in_ifaddr *in;
833 unsigned char address[4], netmask[4];
835 if(ip == NULL) return;
838 memcpy(address, &in->ifa_address, sizeof(address));
839 memcpy(netmask, &in->ifa_mask, sizeof(netmask));
840 (*cb)(address, netmask, arg);
845 int dev_netmask(void *d, void *m)
847 struct net_device *dev = d;
848 struct in_device *ip = dev->ip_ptr;
849 struct in_ifaddr *in;
850 __be32 *mask_out = m;
859 *mask_out = in->ifa_mask;
863 void *get_output_buffer(int *len_out)
867 ret = (void *) __get_free_pages(GFP_KERNEL, 0);
868 if(ret) *len_out = PAGE_SIZE;
873 void free_output_buffer(void *buffer)
875 free_pages((unsigned long) buffer, 0);
878 int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
883 remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
885 printk("tap_setup_common - Extra garbage on specification : "
893 unsigned short eth_protocol(struct sk_buff *skb)
895 return(eth_type_trans(skb, skb->dev));