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/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"
23 #include "linux/platform_device.h"
24 #include "asm/uaccess.h"
25 #include "user_util.h"
26 #include "kern_util.h"
29 #include "mconsole_kern.h"
34 static inline void set_ether_mac(struct net_device *dev, unsigned char *addr)
36 memcpy(dev->dev_addr, addr, ETH_ALEN);
39 #define DRIVER_NAME "uml-netdev"
41 static DEFINE_SPINLOCK(opened_lock);
42 static LIST_HEAD(opened);
44 static int uml_net_rx(struct net_device *dev)
46 struct uml_net_private *lp = dev->priv;
50 /* If we can't allocate memory, try again next round. */
51 skb = dev_alloc_skb(dev->mtu);
53 lp->stats.rx_dropped++;
58 skb_put(skb, dev->mtu);
59 skb->mac.raw = skb->data;
60 pkt_len = (*lp->read)(lp->fd, &skb, lp);
63 skb_trim(skb, pkt_len);
64 skb->protocol = (*lp->protocol)(skb);
67 lp->stats.rx_bytes += skb->len;
68 lp->stats.rx_packets++;
76 static void uml_dev_close(void* dev)
78 dev_close( (struct net_device *) dev);
81 irqreturn_t uml_net_interrupt(int irq, void *dev_id, struct pt_regs *regs)
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) ;
93 DECLARE_WORK(close_work, uml_dev_close, dev);
95 "Device '%s' read returned %d, shutting it down\n",
97 /* dev_close can't be called in interrupt context, and takes
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);
105 reactivate_fd(lp->fd, UM_ETH_IRQ);
108 spin_unlock(&lp->lock);
112 static int uml_net_open(struct net_device *dev)
114 struct uml_net_private *lp = dev->priv;
123 dev_ip_addr(dev, &lp->mac[2]);
124 set_ether_mac(dev, lp->mac);
127 lp->fd = (*lp->open)(&lp->user);
133 err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
134 IRQF_DISABLED | IRQF_SHARED, dev->name, dev);
136 printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
141 lp->tl.data = (unsigned long) &lp->user;
142 netif_start_queue(dev);
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.
148 while((err = uml_net_rx(dev)) > 0) ;
150 spin_lock(&opened_lock);
151 list_add(&lp->list, &opened);
152 spin_unlock(&opened_lock);
156 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
162 static int uml_net_close(struct net_device *dev)
164 struct uml_net_private *lp = dev->priv;
166 netif_stop_queue(dev);
168 free_irq(dev->irq, dev);
169 if(lp->close != NULL)
170 (*lp->close)(lp->fd, &lp->user);
173 spin_lock(&opened_lock);
175 spin_unlock(&opened_lock);
180 static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
182 struct uml_net_private *lp = dev->priv;
186 netif_stop_queue(dev);
188 spin_lock_irqsave(&lp->lock, flags);
190 len = (*lp->write)(lp->fd, &skb, lp);
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);
198 /* this is normally done in the interrupt when tx finishes */
199 netif_wake_queue(dev);
202 netif_start_queue(dev);
203 lp->stats.tx_dropped++;
206 netif_start_queue(dev);
207 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
210 spin_unlock_irqrestore(&lp->lock, flags);
217 static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
219 struct uml_net_private *lp = dev->priv;
223 static void uml_net_set_multicast_list(struct net_device *dev)
225 if (dev->flags & IFF_PROMISC) return;
226 else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
227 else dev->flags &= ~IFF_ALLMULTI;
230 static void uml_net_tx_timeout(struct net_device *dev)
232 dev->trans_start = jiffies;
233 netif_wake_queue(dev);
236 static int uml_net_set_mac(struct net_device *dev, void *addr)
238 struct uml_net_private *lp = dev->priv;
239 struct sockaddr *hwaddr = addr;
241 spin_lock_irq(&lp->lock);
242 set_ether_mac(dev, hwaddr->sa_data);
243 spin_unlock_irq(&lp->lock);
248 static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
250 struct uml_net_private *lp = dev->priv;
253 spin_lock_irq(&lp->lock);
255 new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
264 spin_unlock_irq(&lp->lock);
268 static void uml_net_get_drvinfo(struct net_device *dev,
269 struct ethtool_drvinfo *info)
271 strcpy(info->driver, DRIVER_NAME);
272 strcpy(info->version, "42");
275 static struct ethtool_ops uml_net_ethtool_ops = {
276 .get_drvinfo = uml_net_get_drvinfo,
277 .get_link = ethtool_op_get_link,
280 void uml_net_user_timer_expire(unsigned long _conn)
283 struct connection *conn = (struct connection *)_conn;
285 dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
290 static DEFINE_SPINLOCK(devices_lock);
291 static LIST_HEAD(devices);
293 static struct platform_driver uml_net_driver = {
298 static int driver_registered;
300 static int eth_configure(int n, void *init, char *mac,
301 struct transport *transport)
303 struct uml_net *device;
304 struct net_device *dev;
305 struct uml_net_private *lp;
308 size = transport->private_size + sizeof(struct uml_net_private) +
309 sizeof(((struct uml_net_private *) 0)->user);
311 device = kmalloc(sizeof(*device), GFP_KERNEL);
312 if (device == NULL) {
313 printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
317 memset(device, 0, sizeof(*device));
318 INIT_LIST_HEAD(&device->list);
321 spin_lock(&devices_lock);
322 list_add(&device->list, &devices);
323 spin_unlock(&devices_lock);
325 if (setup_etheraddr(mac, device->mac))
326 device->have_mac = 1;
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]);
335 dev = alloc_etherdev(size);
337 printk(KERN_ERR "eth_configure: failed to allocate device\n");
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. */
347 if (!driver_registered) {
348 platform_driver_register(¨_net_driver);
349 driver_registered = 1;
352 device->pdev.name = DRIVER_NAME;
353 platform_device_register(&device->pdev);
354 SET_NETDEV_DEV(dev,&device->pdev.dev);
356 /* If this name ends up conflicting with an existing registered
357 * netdevice, that is OK, register_netdev{,ice}() will notice this
360 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
363 (*transport->kern->init)(dev, init);
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;
374 dev->ethtool_ops = ¨_net_ethtool_ops;
375 dev->watchdog_timeo = (HZ >> 1);
376 dev->irq = UM_ETH_IRQ;
379 err = register_netdevice(dev);
383 /* XXX: should we call ->remove() here? */
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.
394 *lp = ((struct uml_net_private)
395 { .list = LIST_HEAD_INIT(lp->list),
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,
412 spin_lock_init(&lp->lock);
413 lp->tl.function = uml_net_user_timer_expire;
415 memcpy(lp->mac, device->mac, sizeof(lp->mac));
417 if (transport->user->init)
418 (*transport->user->init)(&lp->user, dev);
420 if (device->have_mac)
421 set_ether_mac(dev, device->mac);
426 static struct uml_net *find_device(int n)
428 struct uml_net *device;
429 struct list_head *ele;
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)
439 spin_unlock(&devices_lock);
443 static int eth_parse(char *str, int *index_out, char **str_out)
448 n = simple_strtoul(str, &end, 0);
450 printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
454 printk(KERN_ERR "eth_setup: device %d is negative\n", n);
460 "eth_setup: expected '=' after device number\n");
465 printk(KERN_ERR "eth_setup: Device %d already configured\n",
469 if(index_out) *index_out = n;
475 struct list_head list;
480 /* Filled in at boot time. Will need locking if the transports become
483 struct list_head transports = LIST_HEAD_INIT(transports);
485 /* Filled in during early boot */
486 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
488 static int check_transport(struct transport *transport, char *eth, int n,
489 void **init_out, char **mac_out)
493 len = strlen(transport->name);
494 if(strncmp(eth, transport->name, len))
500 else if(*eth != '\0')
503 *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
504 if(*init_out == NULL)
507 if(!transport->setup(eth, mac_out, *init_out)){
514 void register_transport(struct transport *new)
516 struct list_head *ele, *next;
517 struct eth_init *eth;
522 list_add(&new->list, &transports);
524 list_for_each_safe(ele, next, ð_cmd_line){
525 eth = list_entry(ele, struct eth_init, list);
526 match = check_transport(new, eth->init, eth->index, &init,
530 else if(init != NULL){
531 eth_configure(eth->index, init, mac, new);
534 list_del(ð->list);
538 static int eth_setup_common(char *str, int index)
540 struct list_head *ele;
541 struct transport *transport;
545 list_for_each(ele, &transports){
546 transport = list_entry(ele, struct transport, list);
547 if(!check_transport(transport, str, index, &init, &mac))
550 eth_configure(index, init, mac, transport);
558 static int eth_setup(char *str)
560 struct eth_init *new;
563 err = eth_parse(str, &n, &str);
566 new = alloc_bootmem(sizeof(new));
568 printk("eth_init : alloc_bootmem failed\n");
572 INIT_LIST_HEAD(&new->list);
576 list_add_tail(&new->list, ð_cmd_line);
580 __setup("eth", eth_setup);
581 __uml_help(eth_setup,
582 "eth[0-9]+=<transport>,<options>\n"
583 " Configure a network device.\n\n"
587 static int eth_init(void)
589 struct list_head *ele, *next;
590 struct eth_init *eth;
592 list_for_each_safe(ele, next, ð_cmd_line){
593 eth = list_entry(ele, struct eth_init, list);
595 if(eth_setup_common(eth->init, eth->index))
596 list_del(ð->list);
601 __initcall(eth_init);
604 static int net_config(char *str)
608 err = eth_parse(str, &n, &str);
611 str = kstrdup(str, GFP_KERNEL);
613 printk(KERN_ERR "net_config failed to strdup string\n");
616 err = !eth_setup_common(str, n);
622 static int net_id(char **str, int *start_out, int *end_out)
627 n = simple_strtoul(*str, &end, 0);
628 if((*end != '\0') || (end == *str))
637 static int net_remove(int n)
639 struct uml_net *device;
640 struct net_device *dev;
641 struct uml_net_private *lp;
643 device = find_device(n);
651 if(lp->remove != NULL) (*lp->remove)(&lp->user);
652 unregister_netdev(dev);
653 platform_device_unregister(&device->pdev);
655 list_del(&device->list);
661 static struct mc_device net_mc = {
663 .config = net_config,
666 .remove = net_remove,
669 static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
672 struct in_ifaddr *ifa = ptr;
673 struct net_device *dev = ifa->ifa_dev->dev;
674 struct uml_net_private *lp;
675 void (*proc)(unsigned char *, unsigned char *, void *);
676 unsigned char addr_buf[4], netmask_buf[4];
678 if(dev->open != uml_net_open) return(NOTIFY_DONE);
685 proc = lp->add_address;
688 proc = lp->delete_address;
692 memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
693 memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
694 (*proc)(addr_buf, netmask_buf, &lp->user);
699 struct notifier_block uml_inetaddr_notifier = {
700 .notifier_call = uml_inetaddr_event,
703 static int uml_net_init(void)
705 struct list_head *ele;
706 struct uml_net_private *lp;
707 struct in_device *ip;
708 struct in_ifaddr *in;
710 mconsole_register_dev(&net_mc);
711 register_inetaddr_notifier(¨_inetaddr_notifier);
713 /* Devices may have been opened already, so the uml_inetaddr_notifier
714 * didn't get a chance to run for them. This fakes it so that
715 * addresses which have already been set up get handled properly.
717 list_for_each(ele, &opened){
718 lp = list_entry(ele, struct uml_net_private, list);
719 ip = lp->dev->ip_ptr;
720 if(ip == NULL) continue;
723 uml_inetaddr_event(NULL, NETDEV_UP, in);
731 __initcall(uml_net_init);
733 static void close_devices(void)
735 struct list_head *ele;
736 struct uml_net_private *lp;
738 list_for_each(ele, &opened){
739 lp = list_entry(ele, struct uml_net_private, list);
740 free_irq(lp->dev->irq, lp->dev);
741 if((lp->close != NULL) && (lp->fd >= 0))
742 (*lp->close)(lp->fd, &lp->user);
743 if(lp->remove != NULL) (*lp->remove)(&lp->user);
747 __uml_exitcall(close_devices);
749 int setup_etheraddr(char *str, unsigned char *addr)
757 addr[i] = simple_strtoul(str, &end, 16);
759 ((*end != ':') && (*end != ',') && (*end != '\0'))){
761 "setup_etheraddr: failed to parse '%s' "
762 "as an ethernet address\n", str);
769 "Attempt to assign a broadcast ethernet address to a "
770 "device disallowed\n");
776 void dev_ip_addr(void *d, unsigned char *bin_buf)
778 struct net_device *dev = d;
779 struct in_device *ip = dev->ip_ptr;
780 struct in_ifaddr *in;
782 if((ip == NULL) || ((in = ip->ifa_list) == NULL)){
783 printk(KERN_WARNING "dev_ip_addr - device not assigned an "
787 memcpy(bin_buf, &in->ifa_address, sizeof(in->ifa_address));
790 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
792 if((skb != NULL) && (skb_tailroom(skb) < extra)){
793 struct sk_buff *skb2;
795 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
799 if(skb != NULL) skb_put(skb, extra);
803 void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
807 struct net_device *dev = d;
808 struct in_device *ip = dev->ip_ptr;
809 struct in_ifaddr *in;
810 unsigned char address[4], netmask[4];
812 if(ip == NULL) return;
815 memcpy(address, &in->ifa_address, sizeof(address));
816 memcpy(netmask, &in->ifa_mask, sizeof(netmask));
817 (*cb)(address, netmask, arg);
822 int dev_netmask(void *d, void *m)
824 struct net_device *dev = d;
825 struct in_device *ip = dev->ip_ptr;
826 struct in_ifaddr *in;
836 *mask_out = in->ifa_mask;
840 void *get_output_buffer(int *len_out)
844 ret = (void *) __get_free_pages(GFP_KERNEL, 0);
845 if(ret) *len_out = PAGE_SIZE;
850 void free_output_buffer(void *buffer)
852 free_pages((unsigned long) buffer, 0);
855 int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
860 remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
862 printk("tap_setup_common - Extra garbage on specification : "
870 unsigned short eth_protocol(struct sk_buff *skb)
872 return(eth_type_trans(skb, skb->dev));
876 * Overrides for Emacs so that we follow Linus's tabbing style.
877 * Emacs will notice this stuff at the end of the file and automatically
878 * adjust the settings for this buffer only. This must remain at the end
880 * ---------------------------------------------------------------------------
882 * c-file-style: "linux"