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 #define DRIVER_NAME "uml-netdev"
36 static DEFINE_SPINLOCK(opened_lock);
37 static LIST_HEAD(opened);
39 static int uml_net_rx(struct net_device *dev)
41 struct uml_net_private *lp = dev->priv;
45 /* If we can't allocate memory, try again next round. */
46 skb = dev_alloc_skb(dev->mtu);
48 lp->stats.rx_dropped++;
53 skb_put(skb, dev->mtu);
54 skb->mac.raw = skb->data;
55 pkt_len = (*lp->read)(lp->fd, &skb, lp);
58 skb_trim(skb, pkt_len);
59 skb->protocol = (*lp->protocol)(skb);
62 lp->stats.rx_bytes += skb->len;
63 lp->stats.rx_packets++;
71 static void uml_dev_close(void* dev)
73 dev_close( (struct net_device *) dev);
76 irqreturn_t uml_net_interrupt(int irq, void *dev_id, struct pt_regs *regs)
78 struct net_device *dev = dev_id;
79 struct uml_net_private *lp = dev->priv;
82 if(!netif_running(dev))
86 while((err = uml_net_rx(dev)) > 0) ;
88 DECLARE_WORK(close_work, uml_dev_close, dev);
90 "Device '%s' read returned %d, shutting it down\n",
92 /* dev_close can't be called in interrupt context, and takes
94 * And dev_close() can be safely called multiple times on the
95 * same device, since it tests for (dev->flags & IFF_UP). So
96 * there's no harm in delaying the device shutdown. */
97 schedule_work(&close_work);
100 reactivate_fd(lp->fd, UM_ETH_IRQ);
103 spin_unlock(&lp->lock);
107 static int uml_net_open(struct net_device *dev)
109 struct uml_net_private *lp = dev->priv;
112 spin_lock(&lp->lock);
120 dev_ip_addr(dev, &lp->mac[2]);
121 set_ether_mac(dev, lp->mac);
124 lp->fd = (*lp->open)(&lp->user);
130 err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
131 IRQF_DISABLED | IRQF_SHARED, dev->name, dev);
133 printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
138 lp->tl.data = (unsigned long) &lp->user;
139 netif_start_queue(dev);
141 /* clear buffer - it can happen that the host side of the interface
142 * is full when we get here. In this case, new data is never queued,
143 * SIGIOs never arrive, and the net never works.
145 while((err = uml_net_rx(dev)) > 0) ;
147 spin_unlock(&lp->lock);
149 spin_lock(&opened_lock);
150 list_add(&lp->list, &opened);
151 spin_unlock(&opened_lock);
155 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
158 spin_unlock(&lp->lock);
162 static int uml_net_close(struct net_device *dev)
164 struct uml_net_private *lp = dev->priv;
166 netif_stop_queue(dev);
167 spin_lock(&lp->lock);
169 free_irq(dev->irq, dev);
170 if(lp->close != NULL)
171 (*lp->close)(lp->fd, &lp->user);
174 spin_unlock(&lp->lock);
176 spin_lock(&opened_lock);
178 spin_unlock(&opened_lock);
183 static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
185 struct uml_net_private *lp = dev->priv;
189 netif_stop_queue(dev);
191 spin_lock_irqsave(&lp->lock, flags);
193 len = (*lp->write)(lp->fd, &skb, lp);
195 if(len == skb->len) {
196 lp->stats.tx_packets++;
197 lp->stats.tx_bytes += skb->len;
198 dev->trans_start = jiffies;
199 netif_start_queue(dev);
201 /* this is normally done in the interrupt when tx finishes */
202 netif_wake_queue(dev);
205 netif_start_queue(dev);
206 lp->stats.tx_dropped++;
209 netif_start_queue(dev);
210 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
213 spin_unlock_irqrestore(&lp->lock, flags);
220 static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
222 struct uml_net_private *lp = dev->priv;
226 static void uml_net_set_multicast_list(struct net_device *dev)
228 if (dev->flags & IFF_PROMISC) return;
229 else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
230 else dev->flags &= ~IFF_ALLMULTI;
233 static void uml_net_tx_timeout(struct net_device *dev)
235 dev->trans_start = jiffies;
236 netif_wake_queue(dev);
239 static int uml_net_set_mac(struct net_device *dev, void *addr)
241 struct uml_net_private *lp = dev->priv;
242 struct sockaddr *hwaddr = addr;
244 spin_lock(&lp->lock);
245 memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
246 spin_unlock(&lp->lock);
251 static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
253 struct uml_net_private *lp = dev->priv;
256 spin_lock(&lp->lock);
258 new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
267 spin_unlock(&lp->lock);
271 static void uml_net_get_drvinfo(struct net_device *dev,
272 struct ethtool_drvinfo *info)
274 strcpy(info->driver, DRIVER_NAME);
275 strcpy(info->version, "42");
278 static struct ethtool_ops uml_net_ethtool_ops = {
279 .get_drvinfo = uml_net_get_drvinfo,
280 .get_link = ethtool_op_get_link,
283 void uml_net_user_timer_expire(unsigned long _conn)
286 struct connection *conn = (struct connection *)_conn;
288 dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
293 static DEFINE_SPINLOCK(devices_lock);
294 static LIST_HEAD(devices);
296 static struct platform_driver uml_net_driver = {
301 static int driver_registered;
303 static int eth_configure(int n, void *init, char *mac,
304 struct transport *transport)
306 struct uml_net *device;
307 struct net_device *dev;
308 struct uml_net_private *lp;
311 size = transport->private_size + sizeof(struct uml_net_private) +
312 sizeof(((struct uml_net_private *) 0)->user);
314 device = kmalloc(sizeof(*device), GFP_KERNEL);
315 if (device == NULL) {
316 printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
320 memset(device, 0, sizeof(*device));
321 INIT_LIST_HEAD(&device->list);
324 spin_lock(&devices_lock);
325 list_add(&device->list, &devices);
326 spin_unlock(&devices_lock);
328 if (setup_etheraddr(mac, device->mac))
329 device->have_mac = 1;
331 printk(KERN_INFO "Netdevice %d ", n);
332 if (device->have_mac)
333 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
334 device->mac[0], device->mac[1],
335 device->mac[2], device->mac[3],
336 device->mac[4], device->mac[5]);
338 dev = alloc_etherdev(size);
340 printk(KERN_ERR "eth_configure: failed to allocate device\n");
345 /* This points to the transport private data. It's still clear, but we
346 * must memset it to 0 *now*. Let's help the drivers. */
350 if (!driver_registered) {
351 platform_driver_register(¨_net_driver);
352 driver_registered = 1;
355 device->pdev.name = DRIVER_NAME;
356 platform_device_register(&device->pdev);
357 SET_NETDEV_DEV(dev,&device->pdev.dev);
359 /* If this name ends up conflicting with an existing registered
360 * netdevice, that is OK, register_netdev{,ice}() will notice this
363 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
366 (*transport->kern->init)(dev, init);
368 dev->mtu = transport->user->max_packet;
369 dev->open = uml_net_open;
370 dev->hard_start_xmit = uml_net_start_xmit;
371 dev->stop = uml_net_close;
372 dev->get_stats = uml_net_get_stats;
373 dev->set_multicast_list = uml_net_set_multicast_list;
374 dev->tx_timeout = uml_net_tx_timeout;
375 dev->set_mac_address = uml_net_set_mac;
376 dev->change_mtu = uml_net_change_mtu;
377 dev->ethtool_ops = ¨_net_ethtool_ops;
378 dev->watchdog_timeo = (HZ >> 1);
379 dev->irq = UM_ETH_IRQ;
382 err = register_netdevice(dev);
386 /* XXX: should we call ->remove() here? */
391 /* lp.user is the first four bytes of the transport data, which
392 * has already been initialized. This structure assignment will
393 * overwrite that, so we make sure that .user gets overwritten with
394 * what it already has.
397 *lp = ((struct uml_net_private)
398 { .list = LIST_HEAD_INIT(lp->list),
401 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
402 .have_mac = device->have_mac,
403 .protocol = transport->kern->protocol,
404 .open = transport->user->open,
405 .close = transport->user->close,
406 .remove = transport->user->remove,
407 .read = transport->kern->read,
408 .write = transport->kern->write,
409 .add_address = transport->user->add_address,
410 .delete_address = transport->user->delete_address,
411 .set_mtu = transport->user->set_mtu,
415 spin_lock_init(&lp->lock);
416 lp->tl.function = uml_net_user_timer_expire;
418 memcpy(lp->mac, device->mac, sizeof(lp->mac));
420 if (transport->user->init)
421 (*transport->user->init)(&lp->user, dev);
423 if (device->have_mac)
424 set_ether_mac(dev, device->mac);
429 static struct uml_net *find_device(int n)
431 struct uml_net *device;
432 struct list_head *ele;
434 spin_lock(&devices_lock);
435 list_for_each(ele, &devices){
436 device = list_entry(ele, struct uml_net, list);
437 if(device->index == n)
442 spin_unlock(&devices_lock);
446 static int eth_parse(char *str, int *index_out, char **str_out)
451 n = simple_strtoul(str, &end, 0);
453 printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
457 printk(KERN_ERR "eth_setup: device %d is negative\n", n);
463 "eth_setup: expected '=' after device number\n");
468 printk(KERN_ERR "eth_setup: Device %d already configured\n",
472 if(index_out) *index_out = n;
478 struct list_head list;
483 /* Filled in at boot time. Will need locking if the transports become
486 struct list_head transports = LIST_HEAD_INIT(transports);
488 /* Filled in during early boot */
489 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
491 static int check_transport(struct transport *transport, char *eth, int n,
492 void **init_out, char **mac_out)
496 len = strlen(transport->name);
497 if(strncmp(eth, transport->name, len))
503 else if(*eth != '\0')
506 *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
507 if(*init_out == NULL)
510 if(!transport->setup(eth, mac_out, *init_out)){
517 void register_transport(struct transport *new)
519 struct list_head *ele, *next;
520 struct eth_init *eth;
525 list_add(&new->list, &transports);
527 list_for_each_safe(ele, next, ð_cmd_line){
528 eth = list_entry(ele, struct eth_init, list);
529 match = check_transport(new, eth->init, eth->index, &init,
533 else if(init != NULL){
534 eth_configure(eth->index, init, mac, new);
537 list_del(ð->list);
541 static int eth_setup_common(char *str, int index)
543 struct list_head *ele;
544 struct transport *transport;
548 list_for_each(ele, &transports){
549 transport = list_entry(ele, struct transport, list);
550 if(!check_transport(transport, str, index, &init, &mac))
553 eth_configure(index, init, mac, transport);
561 static int eth_setup(char *str)
563 struct eth_init *new;
566 err = eth_parse(str, &n, &str);
569 new = alloc_bootmem(sizeof(new));
571 printk("eth_init : alloc_bootmem failed\n");
575 INIT_LIST_HEAD(&new->list);
579 list_add_tail(&new->list, ð_cmd_line);
583 __setup("eth", eth_setup);
584 __uml_help(eth_setup,
585 "eth[0-9]+=<transport>,<options>\n"
586 " Configure a network device.\n\n"
590 static int eth_init(void)
592 struct list_head *ele, *next;
593 struct eth_init *eth;
595 list_for_each_safe(ele, next, ð_cmd_line){
596 eth = list_entry(ele, struct eth_init, list);
598 if(eth_setup_common(eth->init, eth->index))
599 list_del(ð->list);
604 __initcall(eth_init);
607 static int net_config(char *str)
611 err = eth_parse(str, &n, &str);
614 str = kstrdup(str, GFP_KERNEL);
616 printk(KERN_ERR "net_config failed to strdup string\n");
619 err = !eth_setup_common(str, n);
625 static int net_id(char **str, int *start_out, int *end_out)
630 n = simple_strtoul(*str, &end, 0);
631 if((*end != '\0') || (end == *str))
640 static int net_remove(int n)
642 struct uml_net *device;
643 struct net_device *dev;
644 struct uml_net_private *lp;
646 device = find_device(n);
654 if(lp->remove != NULL) (*lp->remove)(&lp->user);
655 unregister_netdev(dev);
656 platform_device_unregister(&device->pdev);
658 list_del(&device->list);
664 static struct mc_device net_mc = {
666 .config = net_config,
669 .remove = net_remove,
672 static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
675 struct in_ifaddr *ifa = ptr;
676 struct net_device *dev = ifa->ifa_dev->dev;
677 struct uml_net_private *lp;
678 void (*proc)(unsigned char *, unsigned char *, void *);
679 unsigned char addr_buf[4], netmask_buf[4];
681 if(dev->open != uml_net_open) return(NOTIFY_DONE);
688 proc = lp->add_address;
691 proc = lp->delete_address;
695 memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
696 memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
697 (*proc)(addr_buf, netmask_buf, &lp->user);
702 struct notifier_block uml_inetaddr_notifier = {
703 .notifier_call = uml_inetaddr_event,
706 static int uml_net_init(void)
708 struct list_head *ele;
709 struct uml_net_private *lp;
710 struct in_device *ip;
711 struct in_ifaddr *in;
713 mconsole_register_dev(&net_mc);
714 register_inetaddr_notifier(¨_inetaddr_notifier);
716 /* Devices may have been opened already, so the uml_inetaddr_notifier
717 * didn't get a chance to run for them. This fakes it so that
718 * addresses which have already been set up get handled properly.
720 list_for_each(ele, &opened){
721 lp = list_entry(ele, struct uml_net_private, list);
722 ip = lp->dev->ip_ptr;
723 if(ip == NULL) continue;
726 uml_inetaddr_event(NULL, NETDEV_UP, in);
734 __initcall(uml_net_init);
736 static void close_devices(void)
738 struct list_head *ele;
739 struct uml_net_private *lp;
741 list_for_each(ele, &opened){
742 lp = list_entry(ele, struct uml_net_private, list);
743 free_irq(lp->dev->irq, lp->dev);
744 if((lp->close != NULL) && (lp->fd >= 0))
745 (*lp->close)(lp->fd, &lp->user);
746 if(lp->remove != NULL) (*lp->remove)(&lp->user);
750 __uml_exitcall(close_devices);
752 int setup_etheraddr(char *str, unsigned char *addr)
760 addr[i] = simple_strtoul(str, &end, 16);
762 ((*end != ':') && (*end != ',') && (*end != '\0'))){
764 "setup_etheraddr: failed to parse '%s' "
765 "as an ethernet address\n", str);
772 "Attempt to assign a broadcast ethernet address to a "
773 "device disallowed\n");
779 void dev_ip_addr(void *d, unsigned char *bin_buf)
781 struct net_device *dev = d;
782 struct in_device *ip = dev->ip_ptr;
783 struct in_ifaddr *in;
785 if((ip == NULL) || ((in = ip->ifa_list) == NULL)){
786 printk(KERN_WARNING "dev_ip_addr - device not assigned an "
790 memcpy(bin_buf, &in->ifa_address, sizeof(in->ifa_address));
793 void set_ether_mac(void *d, unsigned char *addr)
795 struct net_device *dev = d;
797 memcpy(dev->dev_addr, addr, ETH_ALEN);
800 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
802 if((skb != NULL) && (skb_tailroom(skb) < extra)){
803 struct sk_buff *skb2;
805 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
809 if(skb != NULL) skb_put(skb, extra);
813 void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
817 struct net_device *dev = d;
818 struct in_device *ip = dev->ip_ptr;
819 struct in_ifaddr *in;
820 unsigned char address[4], netmask[4];
822 if(ip == NULL) return;
825 memcpy(address, &in->ifa_address, sizeof(address));
826 memcpy(netmask, &in->ifa_mask, sizeof(netmask));
827 (*cb)(address, netmask, arg);
832 int dev_netmask(void *d, void *m)
834 struct net_device *dev = d;
835 struct in_device *ip = dev->ip_ptr;
836 struct in_ifaddr *in;
846 *mask_out = in->ifa_mask;
850 void *get_output_buffer(int *len_out)
854 ret = (void *) __get_free_pages(GFP_KERNEL, 0);
855 if(ret) *len_out = PAGE_SIZE;
860 void free_output_buffer(void *buffer)
862 free_pages((unsigned long) buffer, 0);
865 int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
870 remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
872 printk("tap_setup_common - Extra garbage on specification : "
880 unsigned short eth_protocol(struct sk_buff *skb)
882 return(eth_type_trans(skb, skb->dev));
886 * Overrides for Emacs so that we follow Linus's tabbing style.
887 * Emacs will notice this stuff at the end of the file and automatically
888 * adjust the settings for this buffer only. This must remain at the end
890 * ---------------------------------------------------------------------------
892 * c-file-style: "linux"