1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
5 * James Leu (jleu@mindspring.net).
6 * Copyright (C) 2001 by various other people who didn't put their name here.
9 #include <linux/memblock.h>
10 #include <linux/etherdevice.h>
11 #include <linux/ethtool.h>
12 #include <linux/inetdevice.h>
13 #include <linux/init.h>
14 #include <linux/list.h>
15 #include <linux/netdevice.h>
16 #include <linux/platform_device.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/skbuff.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
24 #include "mconsole_kern.h"
28 #define DRIVER_NAME "uml-netdev"
30 static DEFINE_SPINLOCK(opened_lock);
31 static LIST_HEAD(opened);
34 * The drop_skb is used when we can't allocate an skb. The
35 * packet is read into drop_skb in order to get the data off the
36 * connection to the host.
37 * It is reallocated whenever a maximum packet size is seen which is
38 * larger than any seen before. update_drop_skb is called from
39 * eth_configure when a new interface is added.
41 static DEFINE_SPINLOCK(drop_lock);
42 static struct sk_buff *drop_skb;
45 static int update_drop_skb(int max)
51 spin_lock_irqsave(&drop_lock, flags);
57 new = dev_alloc_skb(max);
68 spin_unlock_irqrestore(&drop_lock, flags);
73 static int uml_net_rx(struct net_device *dev)
75 struct uml_net_private *lp = netdev_priv(dev);
79 /* If we can't allocate memory, try again next round. */
80 skb = dev_alloc_skb(lp->max_packet);
83 /* Read a packet into drop_skb and don't do anything with it. */
84 (*lp->read)(lp->fd, drop_skb, lp);
85 dev->stats.rx_dropped++;
90 skb_put(skb, lp->max_packet);
91 skb_reset_mac_header(skb);
92 pkt_len = (*lp->read)(lp->fd, skb, lp);
95 skb_trim(skb, pkt_len);
96 skb->protocol = (*lp->protocol)(skb);
98 dev->stats.rx_bytes += skb->len;
99 dev->stats.rx_packets++;
108 static void uml_dev_close(struct work_struct *work)
110 struct uml_net_private *lp =
111 container_of(work, struct uml_net_private, work);
115 static irqreturn_t uml_net_interrupt(int irq, void *dev_id)
117 struct net_device *dev = dev_id;
118 struct uml_net_private *lp = netdev_priv(dev);
121 if (!netif_running(dev))
124 spin_lock(&lp->lock);
125 while ((err = uml_net_rx(dev)) > 0) ;
128 "Device '%s' read returned %d, shutting it down\n",
130 /* dev_close can't be called in interrupt context, and takes
132 * And dev_close() can be safely called multiple times on the
133 * same device, since it tests for (dev->flags & IFF_UP). So
134 * there's no harm in delaying the device shutdown.
135 * Furthermore, the workqueue will not re-enqueue an already
136 * enqueued work item. */
137 schedule_work(&lp->work);
141 spin_unlock(&lp->lock);
145 static int uml_net_open(struct net_device *dev)
147 struct uml_net_private *lp = netdev_priv(dev);
155 lp->fd = (*lp->open)(&lp->user);
161 err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
162 IRQF_SHARED, dev->name, dev);
164 printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
169 netif_start_queue(dev);
171 /* clear buffer - it can happen that the host side of the interface
172 * is full when we get here. In this case, new data is never queued,
173 * SIGIOs never arrive, and the net never works.
175 while ((err = uml_net_rx(dev)) > 0) ;
177 spin_lock(&opened_lock);
178 list_add(&lp->list, &opened);
179 spin_unlock(&opened_lock);
183 if (lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
189 static int uml_net_close(struct net_device *dev)
191 struct uml_net_private *lp = netdev_priv(dev);
193 netif_stop_queue(dev);
195 um_free_irq(dev->irq, dev);
196 if (lp->close != NULL)
197 (*lp->close)(lp->fd, &lp->user);
200 spin_lock(&opened_lock);
202 spin_unlock(&opened_lock);
207 static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
209 struct uml_net_private *lp = netdev_priv(dev);
213 netif_stop_queue(dev);
215 spin_lock_irqsave(&lp->lock, flags);
217 len = (*lp->write)(lp->fd, skb, lp);
218 skb_tx_timestamp(skb);
220 if (len == skb->len) {
221 dev->stats.tx_packets++;
222 dev->stats.tx_bytes += skb->len;
223 netif_trans_update(dev);
224 netif_start_queue(dev);
226 /* this is normally done in the interrupt when tx finishes */
227 netif_wake_queue(dev);
230 netif_start_queue(dev);
231 dev->stats.tx_dropped++;
234 netif_start_queue(dev);
235 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
238 spin_unlock_irqrestore(&lp->lock, flags);
240 dev_consume_skb_any(skb);
245 static void uml_net_set_multicast_list(struct net_device *dev)
250 static void uml_net_tx_timeout(struct net_device *dev, unsigned int txqueue)
252 netif_trans_update(dev);
253 netif_wake_queue(dev);
256 #ifdef CONFIG_NET_POLL_CONTROLLER
257 static void uml_net_poll_controller(struct net_device *dev)
259 disable_irq(dev->irq);
260 uml_net_interrupt(dev->irq, dev);
261 enable_irq(dev->irq);
265 static void uml_net_get_drvinfo(struct net_device *dev,
266 struct ethtool_drvinfo *info)
268 strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
271 static const struct ethtool_ops uml_net_ethtool_ops = {
272 .get_drvinfo = uml_net_get_drvinfo,
273 .get_link = ethtool_op_get_link,
274 .get_ts_info = ethtool_op_get_ts_info,
277 void uml_net_setup_etheraddr(struct net_device *dev, char *str)
286 for (i = 0; i < 6; i++) {
287 addr[i] = simple_strtoul(str, &end, 16);
289 ((*end != ':') && (*end != ',') && (*end != '\0'))) {
291 "setup_etheraddr: failed to parse '%s' "
292 "as an ethernet address\n", str);
297 if (is_multicast_ether_addr(addr)) {
299 "Attempt to assign a multicast ethernet address to a "
300 "device disallowed\n");
303 if (!is_valid_ether_addr(addr)) {
305 "Attempt to assign an invalid ethernet address to a "
306 "device disallowed\n");
309 if (!is_local_ether_addr(addr)) {
311 "Warning: Assigning a globally valid ethernet "
312 "address to a device\n");
313 printk(KERN_WARNING "You should set the 2nd rightmost bit in "
314 "the first byte of the MAC,\n");
315 printk(KERN_WARNING "i.e. %02x:%02x:%02x:%02x:%02x:%02x\n",
316 addr[0] | 0x02, addr[1], addr[2], addr[3], addr[4],
319 eth_hw_addr_set(dev, addr);
324 "Choosing a random ethernet address for device %s\n", dev->name);
325 eth_hw_addr_random(dev);
328 static DEFINE_SPINLOCK(devices_lock);
329 static LIST_HEAD(devices);
331 static struct platform_driver uml_net_driver = {
337 static void net_device_release(struct device *dev)
339 struct uml_net *device = dev_get_drvdata(dev);
340 struct net_device *netdev = device->dev;
341 struct uml_net_private *lp = netdev_priv(netdev);
343 if (lp->remove != NULL)
344 (*lp->remove)(&lp->user);
345 list_del(&device->list);
350 static const struct net_device_ops uml_netdev_ops = {
351 .ndo_open = uml_net_open,
352 .ndo_stop = uml_net_close,
353 .ndo_start_xmit = uml_net_start_xmit,
354 .ndo_set_rx_mode = uml_net_set_multicast_list,
355 .ndo_tx_timeout = uml_net_tx_timeout,
356 .ndo_set_mac_address = eth_mac_addr,
357 .ndo_validate_addr = eth_validate_addr,
358 #ifdef CONFIG_NET_POLL_CONTROLLER
359 .ndo_poll_controller = uml_net_poll_controller,
364 * Ensures that platform_driver_register is called only once by
365 * eth_configure. Will be set in an initcall.
367 static int driver_registered;
369 static void eth_configure(int n, void *init, char *mac,
370 struct transport *transport, gfp_t gfp_mask)
372 struct uml_net *device;
373 struct net_device *dev;
374 struct uml_net_private *lp;
377 size = transport->private_size + sizeof(struct uml_net_private);
379 device = kzalloc(sizeof(*device), gfp_mask);
380 if (device == NULL) {
381 printk(KERN_ERR "eth_configure failed to allocate struct "
386 dev = alloc_etherdev(size);
388 printk(KERN_ERR "eth_configure: failed to allocate struct "
389 "net_device for eth%d\n", n);
390 goto out_free_device;
393 INIT_LIST_HEAD(&device->list);
396 /* If this name ends up conflicting with an existing registered
397 * netdevice, that is OK, register_netdev{,ice}() will notice this
400 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
402 uml_net_setup_etheraddr(dev, mac);
404 printk(KERN_INFO "Netdevice %d (%pM) : ", n, dev->dev_addr);
406 lp = netdev_priv(dev);
407 /* This points to the transport private data. It's still clear, but we
408 * must memset it to 0 *now*. Let's help the drivers. */
410 INIT_WORK(&lp->work, uml_dev_close);
413 if (!driver_registered) {
414 platform_driver_register(¨_net_driver);
415 driver_registered = 1;
418 device->pdev.name = DRIVER_NAME;
419 device->pdev.dev.release = net_device_release;
420 dev_set_drvdata(&device->pdev.dev, device);
421 if (platform_device_register(&device->pdev))
422 goto out_free_netdev;
423 SET_NETDEV_DEV(dev,&device->pdev.dev);
428 * These just fill in a data structure, so there's no failure
429 * to be worried about.
431 (*transport->kern->init)(dev, init);
433 *lp = ((struct uml_net_private)
434 { .list = LIST_HEAD_INIT(lp->list),
437 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
438 .max_packet = transport->user->max_packet,
439 .protocol = transport->kern->protocol,
440 .open = transport->user->open,
441 .close = transport->user->close,
442 .remove = transport->user->remove,
443 .read = transport->kern->read,
444 .write = transport->kern->write,
445 .add_address = transport->user->add_address,
446 .delete_address = transport->user->delete_address });
448 spin_lock_init(&lp->lock);
449 memcpy(lp->mac, dev->dev_addr, sizeof(lp->mac));
451 if ((transport->user->init != NULL) &&
452 ((*transport->user->init)(&lp->user, dev) != 0))
455 dev->mtu = transport->user->mtu;
456 dev->netdev_ops = ¨_netdev_ops;
457 dev->ethtool_ops = ¨_net_ethtool_ops;
458 dev->watchdog_timeo = (HZ >> 1);
459 dev->irq = UM_ETH_IRQ;
461 err = update_drop_skb(lp->max_packet);
463 goto out_undo_user_init;
466 err = register_netdevice(dev);
469 goto out_undo_user_init;
471 spin_lock(&devices_lock);
472 list_add(&device->list, &devices);
473 spin_unlock(&devices_lock);
478 if (transport->user->remove != NULL)
479 (*transport->user->remove)(&lp->user);
481 platform_device_unregister(&device->pdev);
482 return; /* platform_device_unregister frees dev and device */
489 static struct uml_net *find_device(int n)
491 struct uml_net *device;
492 struct list_head *ele;
494 spin_lock(&devices_lock);
495 list_for_each(ele, &devices) {
496 device = list_entry(ele, struct uml_net, list);
497 if (device->index == n)
502 spin_unlock(&devices_lock);
506 static int eth_parse(char *str, int *index_out, char **str_out,
510 int n, err = -EINVAL;
512 n = simple_strtoul(str, &end, 0);
514 *error_out = "Bad device number";
520 *error_out = "Expected '=' after device number";
525 if (find_device(n)) {
526 *error_out = "Device already configured";
536 struct list_head list;
541 static DEFINE_SPINLOCK(transports_lock);
542 static LIST_HEAD(transports);
544 /* Filled in during early boot */
545 static LIST_HEAD(eth_cmd_line);
547 static int check_transport(struct transport *transport, char *eth, int n,
548 void **init_out, char **mac_out, gfp_t gfp_mask)
552 len = strlen(transport->name);
553 if (strncmp(eth, transport->name, len))
559 else if (*eth != '\0')
562 *init_out = kmalloc(transport->setup_size, gfp_mask);
563 if (*init_out == NULL)
566 if (!transport->setup(eth, mac_out, *init_out)) {
573 void register_transport(struct transport *new)
575 struct list_head *ele, *next;
576 struct eth_init *eth;
581 spin_lock(&transports_lock);
582 BUG_ON(!list_empty(&new->list));
583 list_add(&new->list, &transports);
584 spin_unlock(&transports_lock);
586 list_for_each_safe(ele, next, ð_cmd_line) {
587 eth = list_entry(ele, struct eth_init, list);
588 match = check_transport(new, eth->init, eth->index, &init,
592 else if (init != NULL) {
593 eth_configure(eth->index, init, mac, new, GFP_KERNEL);
596 list_del(ð->list);
600 static int eth_setup_common(char *str, int index)
602 struct list_head *ele;
603 struct transport *transport;
608 spin_lock(&transports_lock);
609 list_for_each(ele, &transports) {
610 transport = list_entry(ele, struct transport, list);
611 if (!check_transport(transport, str, index, &init,
615 eth_configure(index, init, mac, transport, GFP_ATOMIC);
622 spin_unlock(&transports_lock);
626 static int __init 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 = memblock_alloc(sizeof(*new), SMP_CACHE_BYTES);
641 panic("%s: Failed to allocate %zu bytes\n", __func__,
644 INIT_LIST_HEAD(&new->list);
648 list_add_tail(&new->list, ð_cmd_line);
652 __setup("eth", eth_setup);
653 __uml_help(eth_setup,
654 "eth[0-9]+=<transport>,<options>\n"
655 " Configure a network device.\n\n"
658 static int net_config(char *str, char **error_out)
662 err = eth_parse(str, &n, &str, error_out);
666 /* This string is broken up and the pieces used by the underlying
667 * driver. So, it is freed only if eth_setup_common fails.
669 str = kstrdup(str, GFP_KERNEL);
671 *error_out = "net_config failed to strdup string";
674 err = !eth_setup_common(str, n);
680 static int net_id(char **str, int *start_out, int *end_out)
685 n = simple_strtoul(*str, &end, 0);
686 if ((*end != '\0') || (end == *str))
695 static int net_remove(int n, char **error_out)
697 struct uml_net *device;
698 struct net_device *dev;
699 struct uml_net_private *lp;
701 device = find_device(n);
706 lp = netdev_priv(dev);
709 unregister_netdev(dev);
710 platform_device_unregister(&device->pdev);
715 static struct mc_device net_mc = {
716 .list = LIST_HEAD_INIT(net_mc.list),
718 .config = net_config,
721 .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->netdev_ops->ndo_open != uml_net_open)
737 lp = netdev_priv(dev);
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 static struct notifier_block uml_inetaddr_notifier = {
758 .notifier_call = uml_inetaddr_event,
761 static void inet_register(void)
763 struct list_head *ele;
764 struct uml_net_private *lp;
765 struct in_device *ip;
766 struct in_ifaddr *in;
768 register_inetaddr_notifier(¨_inetaddr_notifier);
770 /* Devices may have been opened already, so the uml_inetaddr_notifier
771 * didn't get a chance to run for them. This fakes it so that
772 * addresses which have already been set up get handled properly.
774 spin_lock(&opened_lock);
775 list_for_each(ele, &opened) {
776 lp = list_entry(ele, struct uml_net_private, list);
777 ip = lp->dev->ip_ptr;
782 uml_inetaddr_event(NULL, NETDEV_UP, in);
786 spin_unlock(&opened_lock);
789 static inline void inet_register(void)
794 static int uml_net_init(void)
796 mconsole_register_dev(&net_mc);
801 __initcall(uml_net_init);
803 static void close_devices(void)
805 struct list_head *ele;
806 struct uml_net_private *lp;
808 spin_lock(&opened_lock);
809 list_for_each(ele, &opened) {
810 lp = list_entry(ele, struct uml_net_private, list);
811 um_free_irq(lp->dev->irq, lp->dev);
812 if ((lp->close != NULL) && (lp->fd >= 0))
813 (*lp->close)(lp->fd, &lp->user);
814 if (lp->remove != NULL)
815 (*lp->remove)(&lp->user);
817 spin_unlock(&opened_lock);
820 __uml_exitcall(close_devices);
822 void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
826 struct net_device *dev = d;
827 struct in_device *ip = dev->ip_ptr;
828 struct in_ifaddr *in;
829 unsigned char address[4], netmask[4];
831 if (ip == NULL) return;
834 memcpy(address, &in->ifa_address, sizeof(address));
835 memcpy(netmask, &in->ifa_mask, sizeof(netmask));
836 (*cb)(address, netmask, arg);
841 int dev_netmask(void *d, void *m)
843 struct net_device *dev = d;
844 struct in_device *ip = dev->ip_ptr;
845 struct in_ifaddr *in;
846 __be32 *mask_out = m;
855 *mask_out = in->ifa_mask;
859 void *get_output_buffer(int *len_out)
863 ret = (void *) __get_free_pages(GFP_KERNEL, 0);
864 if (ret) *len_out = PAGE_SIZE;
869 void free_output_buffer(void *buffer)
871 free_pages((unsigned long) buffer, 0);
874 int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
879 remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
880 if (remain != NULL) {
881 printk(KERN_ERR "tap_setup_common - Extra garbage on "
882 "specification : '%s'\n", remain);
889 unsigned short eth_protocol(struct sk_buff *skb)
891 return eth_type_trans(skb, skb->dev);