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 spin_unlock(&lp->lock);
137 static int uml_net_close(struct net_device *dev)
139 struct uml_net_private *lp = dev->priv;
141 netif_stop_queue(dev);
142 spin_lock(&lp->lock);
144 free_irq(dev->irq, dev);
145 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
147 spin_lock(&opened_lock);
149 spin_unlock(&opened_lock);
151 spin_unlock(&lp->lock);
155 static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
157 struct uml_net_private *lp = dev->priv;
161 netif_stop_queue(dev);
163 spin_lock_irqsave(&lp->lock, flags);
165 len = (*lp->write)(lp->fd, &skb, lp);
167 if(len == skb->len) {
168 lp->stats.tx_packets++;
169 lp->stats.tx_bytes += skb->len;
170 dev->trans_start = jiffies;
171 netif_start_queue(dev);
173 /* this is normally done in the interrupt when tx finishes */
174 netif_wake_queue(dev);
177 netif_start_queue(dev);
178 lp->stats.tx_dropped++;
181 netif_start_queue(dev);
182 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
185 spin_unlock_irqrestore(&lp->lock, flags);
192 static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
194 struct uml_net_private *lp = dev->priv;
198 static void uml_net_set_multicast_list(struct net_device *dev)
200 if (dev->flags & IFF_PROMISC) return;
201 else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
202 else dev->flags &= ~IFF_ALLMULTI;
205 static void uml_net_tx_timeout(struct net_device *dev)
207 dev->trans_start = jiffies;
208 netif_wake_queue(dev);
211 static int uml_net_set_mac(struct net_device *dev, void *addr)
213 struct uml_net_private *lp = dev->priv;
214 struct sockaddr *hwaddr = addr;
216 spin_lock(&lp->lock);
217 memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
218 spin_unlock(&lp->lock);
223 static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
225 struct uml_net_private *lp = dev->priv;
228 spin_lock(&lp->lock);
230 new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
239 spin_unlock(&lp->lock);
243 static int uml_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
245 static const struct ethtool_drvinfo info = {
246 .cmd = ETHTOOL_GDRVINFO,
247 .driver = "uml virtual ethernet",
255 useraddr = ifr->ifr_data;
256 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
259 case ETHTOOL_GDRVINFO:
260 if (copy_to_user(useraddr, &info, sizeof(info)))
271 void uml_net_user_timer_expire(unsigned long _conn)
274 struct connection *conn = (struct connection *)_conn;
276 dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
281 static spinlock_t devices_lock = SPIN_LOCK_UNLOCKED;
282 static struct list_head devices = LIST_HEAD_INIT(devices);
284 static int eth_configure(int n, void *init, char *mac,
285 struct transport *transport)
287 struct uml_net *device;
288 struct net_device *dev;
289 struct uml_net_private *lp;
292 size = transport->private_size + sizeof(struct uml_net_private) +
293 sizeof(((struct uml_net_private *) 0)->user);
295 device = kmalloc(sizeof(*device), GFP_KERNEL);
296 if (device == NULL) {
297 printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
301 memset(device, 0, sizeof(*device));
302 INIT_LIST_HEAD(&device->list);
305 spin_lock(&devices_lock);
306 list_add(&device->list, &devices);
307 spin_unlock(&devices_lock);
309 if (setup_etheraddr(mac, device->mac))
310 device->have_mac = 1;
312 printk(KERN_INFO "Netdevice %d ", n);
313 if (device->have_mac)
314 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
315 device->mac[0], device->mac[1],
316 device->mac[2], device->mac[3],
317 device->mac[4], device->mac[5]);
319 dev = alloc_etherdev(size);
321 printk(KERN_ERR "eth_configure: failed to allocate device\n");
325 /* If this name ends up conflicting with an existing registered
326 * netdevice, that is OK, register_netdev{,ice}() will notice this
329 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
332 (*transport->kern->init)(dev, init);
334 dev->mtu = transport->user->max_packet;
335 dev->open = uml_net_open;
336 dev->hard_start_xmit = uml_net_start_xmit;
337 dev->stop = uml_net_close;
338 dev->get_stats = uml_net_get_stats;
339 dev->set_multicast_list = uml_net_set_multicast_list;
340 dev->tx_timeout = uml_net_tx_timeout;
341 dev->set_mac_address = uml_net_set_mac;
342 dev->change_mtu = uml_net_change_mtu;
343 dev->do_ioctl = uml_net_ioctl;
344 dev->watchdog_timeo = (HZ >> 1);
345 dev->irq = UM_ETH_IRQ;
348 err = register_netdevice(dev);
352 /* XXX: should we call ->remove() here? */
358 /* lp.user is the first four bytes of the transport data, which
359 * has already been initialized. This structure assignment will
360 * overwrite that, so we make sure that .user gets overwritten with
361 * what it already has.
364 *lp = ((struct uml_net_private)
365 { .list = LIST_HEAD_INIT(lp->list),
366 .lock = SPIN_LOCK_UNLOCKED,
369 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
370 .have_mac = device->have_mac,
371 .protocol = transport->kern->protocol,
372 .open = transport->user->open,
373 .close = transport->user->close,
374 .remove = transport->user->remove,
375 .read = transport->kern->read,
376 .write = transport->kern->write,
377 .add_address = transport->user->add_address,
378 .delete_address = transport->user->delete_address,
379 .set_mtu = transport->user->set_mtu,
383 lp->tl.function = uml_net_user_timer_expire;
385 memcpy(lp->mac, device->mac, sizeof(lp->mac));
387 if (transport->user->init)
388 (*transport->user->init)(&lp->user, dev);
390 if (device->have_mac)
391 set_ether_mac(dev, device->mac);
395 static struct uml_net *find_device(int n)
397 struct uml_net *device;
398 struct list_head *ele;
400 spin_lock(&devices_lock);
401 list_for_each(ele, &devices){
402 device = list_entry(ele, struct uml_net, list);
403 if(device->index == n)
408 spin_unlock(&devices_lock);
412 static int eth_parse(char *str, int *index_out, char **str_out)
417 n = simple_strtoul(str, &end, 0);
419 printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
423 printk(KERN_ERR "eth_setup: device %d is negative\n", n);
429 "eth_setup: expected '=' after device number\n");
434 printk(KERN_ERR "eth_setup: Device %d already configured\n",
438 if(index_out) *index_out = n;
444 struct list_head list;
449 /* Filled in at boot time. Will need locking if the transports become
452 struct list_head transports = LIST_HEAD_INIT(transports);
454 /* Filled in during early boot */
455 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
457 static int check_transport(struct transport *transport, char *eth, int n,
458 void **init_out, char **mac_out)
462 len = strlen(transport->name);
463 if(strncmp(eth, transport->name, len))
469 else if(*eth != '\0')
472 *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
473 if(*init_out == NULL)
476 if(!transport->setup(eth, mac_out, *init_out)){
483 void register_transport(struct transport *new)
485 struct list_head *ele, *next;
486 struct eth_init *eth;
491 list_add(&new->list, &transports);
493 list_for_each_safe(ele, next, ð_cmd_line){
494 eth = list_entry(ele, struct eth_init, list);
495 match = check_transport(new, eth->init, eth->index, &init,
499 else if(init != NULL){
500 eth_configure(eth->index, init, mac, new);
503 list_del(ð->list);
507 static int eth_setup_common(char *str, int index)
509 struct list_head *ele;
510 struct transport *transport;
514 list_for_each(ele, &transports){
515 transport = list_entry(ele, struct transport, list);
516 if(!check_transport(transport, str, index, &init, &mac))
519 eth_configure(index, init, mac, transport);
527 static int eth_setup(char *str)
529 struct eth_init *new;
532 err = eth_parse(str, &n, &str);
535 new = alloc_bootmem(sizeof(new));
537 printk("eth_init : alloc_bootmem failed\n");
541 INIT_LIST_HEAD(&new->list);
545 list_add_tail(&new->list, ð_cmd_line);
549 __setup("eth", eth_setup);
550 __uml_help(eth_setup,
551 "eth[0-9]+=<transport>,<options>\n"
552 " Configure a network device.\n\n"
555 static int eth_init(void)
557 struct list_head *ele, *next;
558 struct eth_init *eth;
560 list_for_each_safe(ele, next, ð_cmd_line){
561 eth = list_entry(ele, struct eth_init, list);
563 if(eth_setup_common(eth->init, eth->index))
564 list_del(ð->list);
570 __initcall(eth_init);
572 static int net_config(char *str)
576 err = eth_parse(str, &n, &str);
579 str = uml_strdup(str);
581 printk(KERN_ERR "net_config failed to strdup string\n");
584 err = !eth_setup_common(str, n);
590 static int net_remove(char *str)
592 struct uml_net *device;
593 struct net_device *dev;
594 struct uml_net_private *lp;
598 n = simple_strtoul(str, &end, 0);
599 if((*end != '\0') || (end == str))
602 device = find_device(n);
608 if(lp->fd > 0) return(-1);
609 if(lp->remove != NULL) (*lp->remove)(&lp->user);
610 unregister_netdev(dev);
612 list_del(&device->list);
618 static struct mc_device net_mc = {
620 .config = net_config,
622 .remove = net_remove,
625 static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
628 struct in_ifaddr *ifa = ptr;
629 u32 addr = ifa->ifa_address;
630 u32 netmask = ifa->ifa_mask;
631 struct net_device *dev = ifa->ifa_dev->dev;
632 struct uml_net_private *lp;
633 void (*proc)(unsigned char *, unsigned char *, void *);
634 unsigned char addr_buf[4], netmask_buf[4];
636 if(dev->open != uml_net_open) return(NOTIFY_DONE);
643 proc = lp->add_address;
646 proc = lp->delete_address;
650 addr_buf[0] = addr & 0xff;
651 addr_buf[1] = (addr >> 8) & 0xff;
652 addr_buf[2] = (addr >> 16) & 0xff;
653 addr_buf[3] = addr >> 24;
654 netmask_buf[0] = netmask & 0xff;
655 netmask_buf[1] = (netmask >> 8) & 0xff;
656 netmask_buf[2] = (netmask >> 16) & 0xff;
657 netmask_buf[3] = netmask >> 24;
658 (*proc)(addr_buf, netmask_buf, &lp->user);
663 struct notifier_block uml_inetaddr_notifier = {
664 .notifier_call = uml_inetaddr_event,
667 static int uml_net_init(void)
669 struct list_head *ele;
670 struct uml_net_private *lp;
671 struct in_device *ip;
672 struct in_ifaddr *in;
674 mconsole_register_dev(&net_mc);
675 register_inetaddr_notifier(¨_inetaddr_notifier);
677 /* Devices may have been opened already, so the uml_inetaddr_notifier
678 * didn't get a chance to run for them. This fakes it so that
679 * addresses which have already been set up get handled properly.
681 list_for_each(ele, &opened){
682 lp = list_entry(ele, struct uml_net_private, list);
683 ip = lp->dev->ip_ptr;
684 if(ip == NULL) continue;
687 uml_inetaddr_event(NULL, NETDEV_UP, in);
695 __initcall(uml_net_init);
697 static void close_devices(void)
699 struct list_head *ele;
700 struct uml_net_private *lp;
702 list_for_each(ele, &opened){
703 lp = list_entry(ele, struct uml_net_private, list);
704 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
705 if(lp->remove != NULL) (*lp->remove)(&lp->user);
709 __uml_exitcall(close_devices);
711 int setup_etheraddr(char *str, unsigned char *addr)
719 addr[i] = simple_strtoul(str, &end, 16);
721 ((*end != ':') && (*end != ',') && (*end != '\0'))){
723 "setup_etheraddr: failed to parse '%s' "
724 "as an ethernet address\n", str);
731 "Attempt to assign a broadcast ethernet address to a "
732 "device disallowed\n");
738 void dev_ip_addr(void *d, char *buf, char *bin_buf)
740 struct net_device *dev = d;
741 struct in_device *ip = dev->ip_ptr;
742 struct in_ifaddr *in;
745 if((ip == NULL) || ((in = ip->ifa_list) == NULL)){
746 printk(KERN_WARNING "dev_ip_addr - device not assigned an "
750 addr = in->ifa_address;
751 sprintf(buf, "%d.%d.%d.%d", addr & 0xff, (addr >> 8) & 0xff,
752 (addr >> 16) & 0xff, addr >> 24);
754 bin_buf[0] = addr & 0xff;
755 bin_buf[1] = (addr >> 8) & 0xff;
756 bin_buf[2] = (addr >> 16) & 0xff;
757 bin_buf[3] = addr >> 24;
761 void set_ether_mac(void *d, unsigned char *addr)
763 struct net_device *dev = d;
765 memcpy(dev->dev_addr, addr, ETH_ALEN);
768 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
770 if((skb != NULL) && (skb_tailroom(skb) < extra)){
771 struct sk_buff *skb2;
773 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
777 if(skb != NULL) skb_put(skb, extra);
781 void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
785 struct net_device *dev = d;
786 struct in_device *ip = dev->ip_ptr;
787 struct in_ifaddr *in;
788 unsigned char address[4], netmask[4];
790 if(ip == NULL) return;
793 address[0] = in->ifa_address & 0xff;
794 address[1] = (in->ifa_address >> 8) & 0xff;
795 address[2] = (in->ifa_address >> 16) & 0xff;
796 address[3] = in->ifa_address >> 24;
797 netmask[0] = in->ifa_mask & 0xff;
798 netmask[1] = (in->ifa_mask >> 8) & 0xff;
799 netmask[2] = (in->ifa_mask >> 16) & 0xff;
800 netmask[3] = in->ifa_mask >> 24;
801 (*cb)(address, netmask, arg);
806 int dev_netmask(void *d, void *m)
808 struct net_device *dev = d;
809 struct in_device *ip = dev->ip_ptr;
810 struct in_ifaddr *in;
820 *mask_out = in->ifa_mask;
824 void *get_output_buffer(int *len_out)
828 ret = (void *) __get_free_pages(GFP_KERNEL, 0);
829 if(ret) *len_out = PAGE_SIZE;
834 void free_output_buffer(void *buffer)
836 free_pages((unsigned long) buffer, 0);
839 int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
844 remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
846 printk("tap_setup_common - Extra garbage on specification : "
854 unsigned short eth_protocol(struct sk_buff *skb)
856 return(eth_type_trans(skb, skb->dev));
860 * Overrides for Emacs so that we follow Linus's tabbing style.
861 * Emacs will notice this stuff at the end of the file and automatically
862 * adjust the settings for this buffer only. This must remain at the end
864 * ---------------------------------------------------------------------------
866 * c-file-style: "linux"