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 "asm/uaccess.h"
24 #include "user_util.h"
25 #include "kern_util.h"
28 #include "mconsole_kern.h"
33 static spinlock_t opened_lock = SPIN_LOCK_UNLOCKED;
36 static int uml_net_rx(struct net_device *dev)
38 struct uml_net_private *lp = dev->priv;
42 /* If we can't allocate memory, try again next round. */
43 skb = dev_alloc_skb(dev->mtu);
45 lp->stats.rx_dropped++;
50 skb_put(skb, dev->mtu);
51 skb->mac.raw = skb->data;
52 pkt_len = (*lp->read)(lp->fd, &skb, lp);
55 skb_trim(skb, pkt_len);
56 skb->protocol = (*lp->protocol)(skb);
59 lp->stats.rx_bytes += skb->len;
60 lp->stats.rx_packets++;
68 irqreturn_t uml_net_interrupt(int irq, void *dev_id, struct pt_regs *regs)
70 struct net_device *dev = dev_id;
71 struct uml_net_private *lp = dev->priv;
74 if(!netif_running(dev))
78 while((err = uml_net_rx(dev)) > 0) ;
81 "Device '%s' read returned %d, shutting it down\n",
86 reactivate_fd(lp->fd, UM_ETH_IRQ);
89 spin_unlock(&lp->lock);
93 static int uml_net_open(struct net_device *dev)
95 struct uml_net_private *lp = dev->priv;
96 char addr[sizeof("255.255.255.255\0")];
107 dev_ip_addr(dev, addr, &lp->mac[2]);
108 set_ether_mac(dev, lp->mac);
111 lp->fd = (*lp->open)(&lp->user);
117 err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
118 SA_INTERRUPT | SA_SHIRQ, dev->name, dev);
120 printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
121 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
126 lp->tl.data = (unsigned long) &lp->user;
127 netif_start_queue(dev);
129 spin_lock(&opened_lock);
130 list_add(&lp->list, &opened);
131 spin_unlock(&opened_lock);
133 /* clear buffer - it can happen that the host side of the interface
134 * is full when we get here. In this case, new data is never queued,
135 * SIGIOs never arrive, and the net never works.
137 while((err = uml_net_rx(dev)) > 0) ;
140 spin_unlock(&lp->lock);
144 static int uml_net_close(struct net_device *dev)
146 struct uml_net_private *lp = dev->priv;
148 netif_stop_queue(dev);
149 spin_lock(&lp->lock);
151 free_irq_by_irq_and_dev(dev->irq, dev);
152 free_irq(dev->irq, dev);
153 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
155 spin_lock(&opened_lock);
157 spin_unlock(&opened_lock);
159 spin_unlock(&lp->lock);
163 static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
165 struct uml_net_private *lp = dev->priv;
169 netif_stop_queue(dev);
171 spin_lock_irqsave(&lp->lock, flags);
173 len = (*lp->write)(lp->fd, &skb, lp);
175 if(len == skb->len) {
176 lp->stats.tx_packets++;
177 lp->stats.tx_bytes += skb->len;
178 dev->trans_start = jiffies;
179 netif_start_queue(dev);
181 /* this is normally done in the interrupt when tx finishes */
182 netif_wake_queue(dev);
185 netif_start_queue(dev);
186 lp->stats.tx_dropped++;
189 netif_start_queue(dev);
190 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
193 spin_unlock_irqrestore(&lp->lock, flags);
200 static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
202 struct uml_net_private *lp = dev->priv;
206 static void uml_net_set_multicast_list(struct net_device *dev)
208 if (dev->flags & IFF_PROMISC) return;
209 else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
210 else dev->flags &= ~IFF_ALLMULTI;
213 static void uml_net_tx_timeout(struct net_device *dev)
215 dev->trans_start = jiffies;
216 netif_wake_queue(dev);
219 static int uml_net_set_mac(struct net_device *dev, void *addr)
221 struct uml_net_private *lp = dev->priv;
222 struct sockaddr *hwaddr = addr;
224 spin_lock(&lp->lock);
225 memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
226 spin_unlock(&lp->lock);
231 static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
233 struct uml_net_private *lp = dev->priv;
236 spin_lock(&lp->lock);
238 new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
247 spin_unlock(&lp->lock);
251 static int uml_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
253 static const struct ethtool_drvinfo info = {
254 .cmd = ETHTOOL_GDRVINFO,
255 .driver = "uml virtual ethernet",
263 useraddr = ifr->ifr_data;
264 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
267 case ETHTOOL_GDRVINFO:
268 if (copy_to_user(useraddr, &info, sizeof(info)))
279 void uml_net_user_timer_expire(unsigned long _conn)
282 struct connection *conn = (struct connection *)_conn;
284 dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
289 static spinlock_t devices_lock = SPIN_LOCK_UNLOCKED;
290 static struct list_head devices = LIST_HEAD_INIT(devices);
292 static int eth_configure(int n, void *init, char *mac,
293 struct transport *transport)
295 struct uml_net *device;
296 struct net_device *dev;
297 struct uml_net_private *lp;
300 size = transport->private_size + sizeof(struct uml_net_private) +
301 sizeof(((struct uml_net_private *) 0)->user);
303 device = kmalloc(sizeof(*device), GFP_KERNEL);
304 if (device == NULL) {
305 printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
309 memset(device, 0, sizeof(*device));
310 INIT_LIST_HEAD(&device->list);
313 spin_lock(&devices_lock);
314 list_add(&device->list, &devices);
315 spin_unlock(&devices_lock);
317 if (setup_etheraddr(mac, device->mac))
318 device->have_mac = 1;
320 printk(KERN_INFO "Netdevice %d ", n);
321 if (device->have_mac)
322 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
323 device->mac[0], device->mac[1],
324 device->mac[2], device->mac[3],
325 device->mac[4], device->mac[5]);
327 dev = alloc_etherdev(size);
329 printk(KERN_ERR "eth_configure: failed to allocate device\n");
333 /* If this name ends up conflicting with an existing registered
334 * netdevice, that is OK, register_netdev{,ice}() will notice this
337 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
340 (*transport->kern->init)(dev, init);
342 dev->mtu = transport->user->max_packet;
343 dev->open = uml_net_open;
344 dev->hard_start_xmit = uml_net_start_xmit;
345 dev->stop = uml_net_close;
346 dev->get_stats = uml_net_get_stats;
347 dev->set_multicast_list = uml_net_set_multicast_list;
348 dev->tx_timeout = uml_net_tx_timeout;
349 dev->set_mac_address = uml_net_set_mac;
350 dev->change_mtu = uml_net_change_mtu;
351 dev->do_ioctl = uml_net_ioctl;
352 dev->watchdog_timeo = (HZ >> 1);
353 dev->irq = UM_ETH_IRQ;
356 err = register_netdevice(dev);
360 /* XXX: should we call ->remove() here? */
366 /* lp.user is the first four bytes of the transport data, which
367 * has already been initialized. This structure assignment will
368 * overwrite that, so we make sure that .user gets overwritten with
369 * what it already has.
372 *lp = ((struct uml_net_private)
373 { .list = LIST_HEAD_INIT(lp->list),
374 .lock = SPIN_LOCK_UNLOCKED,
377 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
378 .have_mac = device->have_mac,
379 .protocol = transport->kern->protocol,
380 .open = transport->user->open,
381 .close = transport->user->close,
382 .remove = transport->user->remove,
383 .read = transport->kern->read,
384 .write = transport->kern->write,
385 .add_address = transport->user->add_address,
386 .delete_address = transport->user->delete_address,
387 .set_mtu = transport->user->set_mtu,
391 lp->tl.function = uml_net_user_timer_expire;
393 memcpy(lp->mac, device->mac, sizeof(lp->mac));
395 if (transport->user->init)
396 (*transport->user->init)(&lp->user, dev);
398 if (device->have_mac)
399 set_ether_mac(dev, device->mac);
403 static struct uml_net *find_device(int n)
405 struct uml_net *device;
406 struct list_head *ele;
408 spin_lock(&devices_lock);
409 list_for_each(ele, &devices){
410 device = list_entry(ele, struct uml_net, list);
411 if(device->index == n)
416 spin_unlock(&devices_lock);
420 static int eth_parse(char *str, int *index_out, char **str_out)
425 n = simple_strtoul(str, &end, 0);
427 printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
431 printk(KERN_ERR "eth_setup: device %d is negative\n", n);
437 "eth_setup: expected '=' after device number\n");
442 printk(KERN_ERR "eth_setup: Device %d already configured\n",
446 if(index_out) *index_out = n;
452 struct list_head list;
457 /* Filled in at boot time. Will need locking if the transports become
460 struct list_head transports = LIST_HEAD_INIT(transports);
462 /* Filled in during early boot */
463 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
465 static int check_transport(struct transport *transport, char *eth, int n,
466 void **init_out, char **mac_out)
470 len = strlen(transport->name);
471 if(strncmp(eth, transport->name, len))
477 else if(*eth != '\0')
480 *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
481 if(*init_out == NULL)
484 if(!transport->setup(eth, mac_out, *init_out)){
491 void register_transport(struct transport *new)
493 struct list_head *ele, *next;
494 struct eth_init *eth;
499 list_add(&new->list, &transports);
501 list_for_each_safe(ele, next, ð_cmd_line){
502 eth = list_entry(ele, struct eth_init, list);
503 match = check_transport(new, eth->init, eth->index, &init,
507 else if(init != NULL){
508 eth_configure(eth->index, init, mac, new);
511 list_del(ð->list);
515 static int eth_setup_common(char *str, int index)
517 struct list_head *ele;
518 struct transport *transport;
522 list_for_each(ele, &transports){
523 transport = list_entry(ele, struct transport, list);
524 if(!check_transport(transport, str, index, &init, &mac))
527 eth_configure(index, init, mac, transport);
535 static int eth_setup(char *str)
537 struct eth_init *new;
540 err = eth_parse(str, &n, &str);
543 new = alloc_bootmem(sizeof(new));
545 printk("eth_init : alloc_bootmem failed\n");
549 INIT_LIST_HEAD(&new->list);
553 list_add_tail(&new->list, ð_cmd_line);
557 __setup("eth", eth_setup);
558 __uml_help(eth_setup,
559 "eth[0-9]+=<transport>,<options>\n"
560 " Configure a network device.\n\n"
563 static int eth_init(void)
565 struct list_head *ele, *next;
566 struct eth_init *eth;
568 list_for_each_safe(ele, next, ð_cmd_line){
569 eth = list_entry(ele, struct eth_init, list);
571 if(eth_setup_common(eth->init, eth->index))
572 list_del(ð->list);
578 __initcall(eth_init);
580 static int net_config(char *str)
584 err = eth_parse(str, &n, &str);
587 str = uml_strdup(str);
589 printk(KERN_ERR "net_config failed to strdup string\n");
592 err = !eth_setup_common(str, n);
598 static int net_remove(char *str)
600 struct uml_net *device;
601 struct net_device *dev;
602 struct uml_net_private *lp;
606 n = simple_strtoul(str, &end, 0);
607 if((*end != '\0') || (end == str))
610 device = find_device(n);
616 if(lp->fd > 0) return(-1);
617 if(lp->remove != NULL) (*lp->remove)(&lp->user);
618 unregister_netdev(dev);
620 list_del(&device->list);
626 static struct mc_device net_mc = {
628 .config = net_config,
630 .remove = net_remove,
633 static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
636 struct in_ifaddr *ifa = ptr;
637 u32 addr = ifa->ifa_address;
638 u32 netmask = ifa->ifa_mask;
639 struct net_device *dev = ifa->ifa_dev->dev;
640 struct uml_net_private *lp;
641 void (*proc)(unsigned char *, unsigned char *, void *);
642 unsigned char addr_buf[4], netmask_buf[4];
644 if(dev->open != uml_net_open) return(NOTIFY_DONE);
651 proc = lp->add_address;
654 proc = lp->delete_address;
658 addr_buf[0] = addr & 0xff;
659 addr_buf[1] = (addr >> 8) & 0xff;
660 addr_buf[2] = (addr >> 16) & 0xff;
661 addr_buf[3] = addr >> 24;
662 netmask_buf[0] = netmask & 0xff;
663 netmask_buf[1] = (netmask >> 8) & 0xff;
664 netmask_buf[2] = (netmask >> 16) & 0xff;
665 netmask_buf[3] = netmask >> 24;
666 (*proc)(addr_buf, netmask_buf, &lp->user);
671 struct notifier_block uml_inetaddr_notifier = {
672 .notifier_call = uml_inetaddr_event,
675 static int uml_net_init(void)
677 struct list_head *ele;
678 struct uml_net_private *lp;
679 struct in_device *ip;
680 struct in_ifaddr *in;
682 mconsole_register_dev(&net_mc);
683 register_inetaddr_notifier(¨_inetaddr_notifier);
685 /* Devices may have been opened already, so the uml_inetaddr_notifier
686 * didn't get a chance to run for them. This fakes it so that
687 * addresses which have already been set up get handled properly.
689 list_for_each(ele, &opened){
690 lp = list_entry(ele, struct uml_net_private, list);
691 ip = lp->dev->ip_ptr;
692 if(ip == NULL) continue;
695 uml_inetaddr_event(NULL, NETDEV_UP, in);
703 __initcall(uml_net_init);
705 static void close_devices(void)
707 struct list_head *ele;
708 struct uml_net_private *lp;
710 list_for_each(ele, &opened){
711 lp = list_entry(ele, struct uml_net_private, list);
712 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
713 if(lp->remove != NULL) (*lp->remove)(&lp->user);
717 __uml_exitcall(close_devices);
719 int setup_etheraddr(char *str, unsigned char *addr)
727 addr[i] = simple_strtoul(str, &end, 16);
729 ((*end != ':') && (*end != ',') && (*end != '\0'))){
731 "setup_etheraddr: failed to parse '%s' "
732 "as an ethernet address\n", str);
739 "Attempt to assign a broadcast ethernet address to a "
740 "device disallowed\n");
746 void dev_ip_addr(void *d, char *buf, char *bin_buf)
748 struct net_device *dev = d;
749 struct in_device *ip = dev->ip_ptr;
750 struct in_ifaddr *in;
753 if((ip == NULL) || ((in = ip->ifa_list) == NULL)){
754 printk(KERN_WARNING "dev_ip_addr - device not assigned an "
758 addr = in->ifa_address;
759 sprintf(buf, "%d.%d.%d.%d", addr & 0xff, (addr >> 8) & 0xff,
760 (addr >> 16) & 0xff, addr >> 24);
762 bin_buf[0] = addr & 0xff;
763 bin_buf[1] = (addr >> 8) & 0xff;
764 bin_buf[2] = (addr >> 16) & 0xff;
765 bin_buf[3] = addr >> 24;
769 void set_ether_mac(void *d, unsigned char *addr)
771 struct net_device *dev = d;
773 memcpy(dev->dev_addr, addr, ETH_ALEN);
776 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
778 if((skb != NULL) && (skb_tailroom(skb) < extra)){
779 struct sk_buff *skb2;
781 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
785 if(skb != NULL) skb_put(skb, extra);
789 void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
793 struct net_device *dev = d;
794 struct in_device *ip = dev->ip_ptr;
795 struct in_ifaddr *in;
796 unsigned char address[4], netmask[4];
798 if(ip == NULL) return;
801 address[0] = in->ifa_address & 0xff;
802 address[1] = (in->ifa_address >> 8) & 0xff;
803 address[2] = (in->ifa_address >> 16) & 0xff;
804 address[3] = in->ifa_address >> 24;
805 netmask[0] = in->ifa_mask & 0xff;
806 netmask[1] = (in->ifa_mask >> 8) & 0xff;
807 netmask[2] = (in->ifa_mask >> 16) & 0xff;
808 netmask[3] = in->ifa_mask >> 24;
809 (*cb)(address, netmask, arg);
814 int dev_netmask(void *d, void *m)
816 struct net_device *dev = d;
817 struct in_device *ip = dev->ip_ptr;
818 struct in_ifaddr *in;
828 *mask_out = in->ifa_mask;
832 void *get_output_buffer(int *len_out)
836 ret = (void *) __get_free_pages(GFP_KERNEL, 0);
837 if(ret) *len_out = PAGE_SIZE;
842 void free_output_buffer(void *buffer)
844 free_pages((unsigned long) buffer, 0);
847 int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
852 remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
854 printk("tap_setup_common - Extra garbage on specification : "
862 unsigned short eth_protocol(struct sk_buff *skb)
864 return(eth_type_trans(skb, skb->dev));
868 * Overrides for Emacs so that we follow Linus's tabbing style.
869 * Emacs will notice this stuff at the end of the file and automatically
870 * adjust the settings for this buffer only. This must remain at the end
872 * ---------------------------------------------------------------------------
874 * c-file-style: "linux"