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(dev->irq, dev);
152 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
154 spin_lock(&opened_lock);
156 spin_unlock(&opened_lock);
158 spin_unlock(&lp->lock);
162 static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
164 struct uml_net_private *lp = dev->priv;
168 netif_stop_queue(dev);
170 spin_lock_irqsave(&lp->lock, flags);
172 len = (*lp->write)(lp->fd, &skb, lp);
174 if(len == skb->len) {
175 lp->stats.tx_packets++;
176 lp->stats.tx_bytes += skb->len;
177 dev->trans_start = jiffies;
178 netif_start_queue(dev);
180 /* this is normally done in the interrupt when tx finishes */
181 netif_wake_queue(dev);
184 netif_start_queue(dev);
185 lp->stats.tx_dropped++;
188 netif_start_queue(dev);
189 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
192 spin_unlock_irqrestore(&lp->lock, flags);
199 static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
201 struct uml_net_private *lp = dev->priv;
205 static void uml_net_set_multicast_list(struct net_device *dev)
207 if (dev->flags & IFF_PROMISC) return;
208 else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
209 else dev->flags &= ~IFF_ALLMULTI;
212 static void uml_net_tx_timeout(struct net_device *dev)
214 dev->trans_start = jiffies;
215 netif_wake_queue(dev);
218 static int uml_net_set_mac(struct net_device *dev, void *addr)
220 struct uml_net_private *lp = dev->priv;
221 struct sockaddr *hwaddr = addr;
223 spin_lock(&lp->lock);
224 memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
225 spin_unlock(&lp->lock);
230 static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
232 struct uml_net_private *lp = dev->priv;
235 spin_lock(&lp->lock);
237 new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
246 spin_unlock(&lp->lock);
250 static int uml_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
252 static const struct ethtool_drvinfo info = {
253 .cmd = ETHTOOL_GDRVINFO,
254 .driver = "uml virtual ethernet",
262 useraddr = ifr->ifr_data;
263 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
266 case ETHTOOL_GDRVINFO:
267 if (copy_to_user(useraddr, &info, sizeof(info)))
278 void uml_net_user_timer_expire(unsigned long _conn)
281 struct connection *conn = (struct connection *)_conn;
283 dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
288 static spinlock_t devices_lock = SPIN_LOCK_UNLOCKED;
289 static struct list_head devices = LIST_HEAD_INIT(devices);
291 static int eth_configure(int n, void *init, char *mac,
292 struct transport *transport)
294 struct uml_net *device;
295 struct net_device *dev;
296 struct uml_net_private *lp;
299 size = transport->private_size + sizeof(struct uml_net_private) +
300 sizeof(((struct uml_net_private *) 0)->user);
302 device = kmalloc(sizeof(*device), GFP_KERNEL);
303 if (device == NULL) {
304 printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
308 memset(device, 0, sizeof(*device));
309 INIT_LIST_HEAD(&device->list);
312 spin_lock(&devices_lock);
313 list_add(&device->list, &devices);
314 spin_unlock(&devices_lock);
316 if (setup_etheraddr(mac, device->mac))
317 device->have_mac = 1;
319 printk(KERN_INFO "Netdevice %d ", n);
320 if (device->have_mac)
321 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
322 device->mac[0], device->mac[1],
323 device->mac[2], device->mac[3],
324 device->mac[4], device->mac[5]);
326 dev = alloc_etherdev(size);
328 printk(KERN_ERR "eth_configure: failed to allocate device\n");
332 /* If this name ends up conflicting with an existing registered
333 * netdevice, that is OK, register_netdev{,ice}() will notice this
336 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
339 (*transport->kern->init)(dev, init);
341 dev->mtu = transport->user->max_packet;
342 dev->open = uml_net_open;
343 dev->hard_start_xmit = uml_net_start_xmit;
344 dev->stop = uml_net_close;
345 dev->get_stats = uml_net_get_stats;
346 dev->set_multicast_list = uml_net_set_multicast_list;
347 dev->tx_timeout = uml_net_tx_timeout;
348 dev->set_mac_address = uml_net_set_mac;
349 dev->change_mtu = uml_net_change_mtu;
350 dev->do_ioctl = uml_net_ioctl;
351 dev->watchdog_timeo = (HZ >> 1);
352 dev->irq = UM_ETH_IRQ;
355 err = register_netdevice(dev);
359 /* XXX: should we call ->remove() here? */
365 /* lp.user is the first four bytes of the transport data, which
366 * has already been initialized. This structure assignment will
367 * overwrite that, so we make sure that .user gets overwritten with
368 * what it already has.
371 *lp = ((struct uml_net_private)
372 { .list = LIST_HEAD_INIT(lp->list),
373 .lock = SPIN_LOCK_UNLOCKED,
376 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
377 .have_mac = device->have_mac,
378 .protocol = transport->kern->protocol,
379 .open = transport->user->open,
380 .close = transport->user->close,
381 .remove = transport->user->remove,
382 .read = transport->kern->read,
383 .write = transport->kern->write,
384 .add_address = transport->user->add_address,
385 .delete_address = transport->user->delete_address,
386 .set_mtu = transport->user->set_mtu,
390 lp->tl.function = uml_net_user_timer_expire;
392 memcpy(lp->mac, device->mac, sizeof(lp->mac));
394 if (transport->user->init)
395 (*transport->user->init)(&lp->user, dev);
397 if (device->have_mac)
398 set_ether_mac(dev, device->mac);
402 static struct uml_net *find_device(int n)
404 struct uml_net *device;
405 struct list_head *ele;
407 spin_lock(&devices_lock);
408 list_for_each(ele, &devices){
409 device = list_entry(ele, struct uml_net, list);
410 if(device->index == n)
415 spin_unlock(&devices_lock);
419 static int eth_parse(char *str, int *index_out, char **str_out)
424 n = simple_strtoul(str, &end, 0);
426 printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
430 printk(KERN_ERR "eth_setup: device %d is negative\n", n);
436 "eth_setup: expected '=' after device number\n");
441 printk(KERN_ERR "eth_setup: Device %d already configured\n",
445 if(index_out) *index_out = n;
451 struct list_head list;
456 /* Filled in at boot time. Will need locking if the transports become
459 struct list_head transports = LIST_HEAD_INIT(transports);
461 /* Filled in during early boot */
462 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
464 static int check_transport(struct transport *transport, char *eth, int n,
465 void **init_out, char **mac_out)
469 len = strlen(transport->name);
470 if(strncmp(eth, transport->name, len))
476 else if(*eth != '\0')
479 *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
480 if(*init_out == NULL)
483 if(!transport->setup(eth, mac_out, *init_out)){
490 void register_transport(struct transport *new)
492 struct list_head *ele, *next;
493 struct eth_init *eth;
498 list_add(&new->list, &transports);
500 list_for_each_safe(ele, next, ð_cmd_line){
501 eth = list_entry(ele, struct eth_init, list);
502 match = check_transport(new, eth->init, eth->index, &init,
506 else if(init != NULL){
507 eth_configure(eth->index, init, mac, new);
510 list_del(ð->list);
514 static int eth_setup_common(char *str, int index)
516 struct list_head *ele;
517 struct transport *transport;
521 list_for_each(ele, &transports){
522 transport = list_entry(ele, struct transport, list);
523 if(!check_transport(transport, str, index, &init, &mac))
526 eth_configure(index, init, mac, transport);
534 static int eth_setup(char *str)
536 struct eth_init *new;
539 err = eth_parse(str, &n, &str);
542 new = alloc_bootmem(sizeof(new));
544 printk("eth_init : alloc_bootmem failed\n");
548 INIT_LIST_HEAD(&new->list);
552 list_add_tail(&new->list, ð_cmd_line);
556 __setup("eth", eth_setup);
557 __uml_help(eth_setup,
558 "eth[0-9]+=<transport>,<options>\n"
559 " Configure a network device.\n\n"
562 static int eth_init(void)
564 struct list_head *ele, *next;
565 struct eth_init *eth;
567 list_for_each_safe(ele, next, ð_cmd_line){
568 eth = list_entry(ele, struct eth_init, list);
570 if(eth_setup_common(eth->init, eth->index))
571 list_del(ð->list);
577 __initcall(eth_init);
579 static int net_config(char *str)
583 err = eth_parse(str, &n, &str);
586 str = uml_strdup(str);
588 printk(KERN_ERR "net_config failed to strdup string\n");
591 err = !eth_setup_common(str, n);
597 static int net_remove(char *str)
599 struct uml_net *device;
600 struct net_device *dev;
601 struct uml_net_private *lp;
605 n = simple_strtoul(str, &end, 0);
606 if((*end != '\0') || (end == str))
609 device = find_device(n);
615 if(lp->fd > 0) return(-1);
616 if(lp->remove != NULL) (*lp->remove)(&lp->user);
617 unregister_netdev(dev);
619 list_del(&device->list);
625 static struct mc_device net_mc = {
627 .config = net_config,
629 .remove = net_remove,
632 static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
635 struct in_ifaddr *ifa = ptr;
636 u32 addr = ifa->ifa_address;
637 u32 netmask = ifa->ifa_mask;
638 struct net_device *dev = ifa->ifa_dev->dev;
639 struct uml_net_private *lp;
640 void (*proc)(unsigned char *, unsigned char *, void *);
641 unsigned char addr_buf[4], netmask_buf[4];
643 if(dev->open != uml_net_open) return(NOTIFY_DONE);
650 proc = lp->add_address;
653 proc = lp->delete_address;
657 addr_buf[0] = addr & 0xff;
658 addr_buf[1] = (addr >> 8) & 0xff;
659 addr_buf[2] = (addr >> 16) & 0xff;
660 addr_buf[3] = addr >> 24;
661 netmask_buf[0] = netmask & 0xff;
662 netmask_buf[1] = (netmask >> 8) & 0xff;
663 netmask_buf[2] = (netmask >> 16) & 0xff;
664 netmask_buf[3] = netmask >> 24;
665 (*proc)(addr_buf, netmask_buf, &lp->user);
670 struct notifier_block uml_inetaddr_notifier = {
671 .notifier_call = uml_inetaddr_event,
674 static int uml_net_init(void)
676 struct list_head *ele;
677 struct uml_net_private *lp;
678 struct in_device *ip;
679 struct in_ifaddr *in;
681 mconsole_register_dev(&net_mc);
682 register_inetaddr_notifier(¨_inetaddr_notifier);
684 /* Devices may have been opened already, so the uml_inetaddr_notifier
685 * didn't get a chance to run for them. This fakes it so that
686 * addresses which have already been set up get handled properly.
688 list_for_each(ele, &opened){
689 lp = list_entry(ele, struct uml_net_private, list);
690 ip = lp->dev->ip_ptr;
691 if(ip == NULL) continue;
694 uml_inetaddr_event(NULL, NETDEV_UP, in);
702 __initcall(uml_net_init);
704 static void close_devices(void)
706 struct list_head *ele;
707 struct uml_net_private *lp;
709 list_for_each(ele, &opened){
710 lp = list_entry(ele, struct uml_net_private, list);
711 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
712 if(lp->remove != NULL) (*lp->remove)(&lp->user);
716 __uml_exitcall(close_devices);
718 int setup_etheraddr(char *str, unsigned char *addr)
726 addr[i] = simple_strtoul(str, &end, 16);
728 ((*end != ':') && (*end != ',') && (*end != '\0'))){
730 "setup_etheraddr: failed to parse '%s' "
731 "as an ethernet address\n", str);
738 "Attempt to assign a broadcast ethernet address to a "
739 "device disallowed\n");
745 void dev_ip_addr(void *d, char *buf, char *bin_buf)
747 struct net_device *dev = d;
748 struct in_device *ip = dev->ip_ptr;
749 struct in_ifaddr *in;
752 if((ip == NULL) || ((in = ip->ifa_list) == NULL)){
753 printk(KERN_WARNING "dev_ip_addr - device not assigned an "
757 addr = in->ifa_address;
758 sprintf(buf, "%d.%d.%d.%d", addr & 0xff, (addr >> 8) & 0xff,
759 (addr >> 16) & 0xff, addr >> 24);
761 bin_buf[0] = addr & 0xff;
762 bin_buf[1] = (addr >> 8) & 0xff;
763 bin_buf[2] = (addr >> 16) & 0xff;
764 bin_buf[3] = addr >> 24;
768 void set_ether_mac(void *d, unsigned char *addr)
770 struct net_device *dev = d;
772 memcpy(dev->dev_addr, addr, ETH_ALEN);
775 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
777 if((skb != NULL) && (skb_tailroom(skb) < extra)){
778 struct sk_buff *skb2;
780 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
784 if(skb != NULL) skb_put(skb, extra);
788 void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
792 struct net_device *dev = d;
793 struct in_device *ip = dev->ip_ptr;
794 struct in_ifaddr *in;
795 unsigned char address[4], netmask[4];
797 if(ip == NULL) return;
800 address[0] = in->ifa_address & 0xff;
801 address[1] = (in->ifa_address >> 8) & 0xff;
802 address[2] = (in->ifa_address >> 16) & 0xff;
803 address[3] = in->ifa_address >> 24;
804 netmask[0] = in->ifa_mask & 0xff;
805 netmask[1] = (in->ifa_mask >> 8) & 0xff;
806 netmask[2] = (in->ifa_mask >> 16) & 0xff;
807 netmask[3] = in->ifa_mask >> 24;
808 (*cb)(address, netmask, arg);
813 int dev_netmask(void *d, void *m)
815 struct net_device *dev = d;
816 struct in_device *ip = dev->ip_ptr;
817 struct in_ifaddr *in;
827 *mask_out = in->ifa_mask;
831 void *get_output_buffer(int *len_out)
835 ret = (void *) __get_free_pages(GFP_KERNEL, 0);
836 if(ret) *len_out = PAGE_SIZE;
841 void free_output_buffer(void *buffer)
843 free_pages((unsigned long) buffer, 0);
846 int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
851 remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
853 printk("tap_setup_common - Extra garbage on specification : "
861 unsigned short eth_protocol(struct sk_buff *skb)
863 return(eth_type_trans(skb, skb->dev));
867 * Overrides for Emacs so that we follow Linus's tabbing style.
868 * Emacs will notice this stuff at the end of the file and automatically
869 * adjust the settings for this buffer only. This must remain at the end
871 * ---------------------------------------------------------------------------
873 * c-file-style: "linux"