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/kernel.h"
9 #include "linux/netdevice.h"
10 #include "linux/rtnetlink.h"
11 #include "linux/skbuff.h"
12 #include "linux/socket.h"
13 #include "linux/spinlock.h"
14 #include "linux/module.h"
15 #include "linux/init.h"
16 #include "linux/etherdevice.h"
17 #include "linux/list.h"
18 #include "linux/inetdevice.h"
19 #include "linux/ctype.h"
20 #include "linux/bootmem.h"
21 #include "linux/ethtool.h"
22 #include "linux/platform_device.h"
23 #include "asm/uaccess.h"
24 #include "user_util.h"
25 #include "kern_util.h"
28 #include "mconsole_kern.h"
33 static inline void set_ether_mac(struct net_device *dev, unsigned char *addr)
35 memcpy(dev->dev_addr, addr, ETH_ALEN);
38 #define DRIVER_NAME "uml-netdev"
40 static DEFINE_SPINLOCK(opened_lock);
41 static LIST_HEAD(opened);
43 static int uml_net_rx(struct net_device *dev)
45 struct uml_net_private *lp = dev->priv;
49 /* If we can't allocate memory, try again next round. */
50 skb = dev_alloc_skb(dev->mtu);
52 lp->stats.rx_dropped++;
57 skb_put(skb, dev->mtu);
58 skb->mac.raw = skb->data;
59 pkt_len = (*lp->read)(lp->fd, &skb, lp);
62 skb_trim(skb, pkt_len);
63 skb->protocol = (*lp->protocol)(skb);
66 lp->stats.rx_bytes += skb->len;
67 lp->stats.rx_packets++;
75 static void uml_dev_close(void* dev)
77 dev_close( (struct net_device *) dev);
80 irqreturn_t uml_net_interrupt(int irq, void *dev_id)
82 struct net_device *dev = dev_id;
83 struct uml_net_private *lp = dev->priv;
86 if(!netif_running(dev))
90 while((err = uml_net_rx(dev)) > 0) ;
92 DECLARE_WORK(close_work, uml_dev_close, dev);
94 "Device '%s' read returned %d, shutting it down\n",
96 /* dev_close can't be called in interrupt context, and takes
98 * And dev_close() can be safely called multiple times on the
99 * same device, since it tests for (dev->flags & IFF_UP). So
100 * there's no harm in delaying the device shutdown. */
101 schedule_work(&close_work);
102 #error this is not permitted - close_work will go out of scope
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;
122 lp->fd = (*lp->open)(&lp->user);
128 err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
129 IRQF_DISABLED | IRQF_SHARED, dev->name, dev);
131 printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
136 lp->tl.data = (unsigned long) &lp->user;
137 netif_start_queue(dev);
139 /* clear buffer - it can happen that the host side of the interface
140 * is full when we get here. In this case, new data is never queued,
141 * SIGIOs never arrive, and the net never works.
143 while((err = uml_net_rx(dev)) > 0) ;
145 spin_lock(&opened_lock);
146 list_add(&lp->list, &opened);
147 spin_unlock(&opened_lock);
151 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
157 static int uml_net_close(struct net_device *dev)
159 struct uml_net_private *lp = dev->priv;
161 netif_stop_queue(dev);
163 free_irq(dev->irq, dev);
164 if(lp->close != NULL)
165 (*lp->close)(lp->fd, &lp->user);
168 spin_lock(&opened_lock);
170 spin_unlock(&opened_lock);
175 static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
177 struct uml_net_private *lp = dev->priv;
181 netif_stop_queue(dev);
183 spin_lock_irqsave(&lp->lock, flags);
185 len = (*lp->write)(lp->fd, &skb, lp);
187 if(len == skb->len) {
188 lp->stats.tx_packets++;
189 lp->stats.tx_bytes += skb->len;
190 dev->trans_start = jiffies;
191 netif_start_queue(dev);
193 /* this is normally done in the interrupt when tx finishes */
194 netif_wake_queue(dev);
197 netif_start_queue(dev);
198 lp->stats.tx_dropped++;
201 netif_start_queue(dev);
202 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
205 spin_unlock_irqrestore(&lp->lock, flags);
212 static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
214 struct uml_net_private *lp = dev->priv;
218 static void uml_net_set_multicast_list(struct net_device *dev)
220 if (dev->flags & IFF_PROMISC) return;
221 else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
222 else dev->flags &= ~IFF_ALLMULTI;
225 static void uml_net_tx_timeout(struct net_device *dev)
227 dev->trans_start = jiffies;
228 netif_wake_queue(dev);
231 static int uml_net_set_mac(struct net_device *dev, void *addr)
233 struct uml_net_private *lp = dev->priv;
234 struct sockaddr *hwaddr = addr;
236 spin_lock_irq(&lp->lock);
237 set_ether_mac(dev, hwaddr->sa_data);
238 spin_unlock_irq(&lp->lock);
243 static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
245 struct uml_net_private *lp = dev->priv;
248 spin_lock_irq(&lp->lock);
250 new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
259 spin_unlock_irq(&lp->lock);
263 static void uml_net_get_drvinfo(struct net_device *dev,
264 struct ethtool_drvinfo *info)
266 strcpy(info->driver, DRIVER_NAME);
267 strcpy(info->version, "42");
270 static struct ethtool_ops uml_net_ethtool_ops = {
271 .get_drvinfo = uml_net_get_drvinfo,
272 .get_link = ethtool_op_get_link,
275 void uml_net_user_timer_expire(unsigned long _conn)
278 struct connection *conn = (struct connection *)_conn;
280 dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
285 static void setup_etheraddr(char *str, unsigned char *addr)
294 addr[i] = simple_strtoul(str, &end, 16);
296 ((*end != ':') && (*end != ',') && (*end != '\0'))){
298 "setup_etheraddr: failed to parse '%s' "
299 "as an ethernet address\n", str);
306 "Attempt to assign a broadcast ethernet address to a "
307 "device disallowed\n");
313 random_ether_addr(addr);
316 static DEFINE_SPINLOCK(devices_lock);
317 static LIST_HEAD(devices);
319 static struct platform_driver uml_net_driver = {
324 static int driver_registered;
326 static int eth_configure(int n, void *init, char *mac,
327 struct transport *transport)
329 struct uml_net *device;
330 struct net_device *dev;
331 struct uml_net_private *lp;
334 size = transport->private_size + sizeof(struct uml_net_private) +
335 sizeof(((struct uml_net_private *) 0)->user);
337 device = kmalloc(sizeof(*device), GFP_KERNEL);
338 if (device == NULL) {
339 printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
343 memset(device, 0, sizeof(*device));
344 INIT_LIST_HEAD(&device->list);
347 spin_lock(&devices_lock);
348 list_add(&device->list, &devices);
349 spin_unlock(&devices_lock);
351 setup_etheraddr(mac, device->mac);
353 printk(KERN_INFO "Netdevice %d ", n);
354 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
355 device->mac[0], device->mac[1],
356 device->mac[2], device->mac[3],
357 device->mac[4], device->mac[5]);
359 dev = alloc_etherdev(size);
361 printk(KERN_ERR "eth_configure: failed to allocate device\n");
366 /* This points to the transport private data. It's still clear, but we
367 * must memset it to 0 *now*. Let's help the drivers. */
371 if (!driver_registered) {
372 platform_driver_register(¨_net_driver);
373 driver_registered = 1;
376 device->pdev.name = DRIVER_NAME;
377 platform_device_register(&device->pdev);
378 SET_NETDEV_DEV(dev,&device->pdev.dev);
380 /* If this name ends up conflicting with an existing registered
381 * netdevice, that is OK, register_netdev{,ice}() will notice this
384 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
387 (*transport->kern->init)(dev, init);
389 dev->mtu = transport->user->max_packet;
390 dev->open = uml_net_open;
391 dev->hard_start_xmit = uml_net_start_xmit;
392 dev->stop = uml_net_close;
393 dev->get_stats = uml_net_get_stats;
394 dev->set_multicast_list = uml_net_set_multicast_list;
395 dev->tx_timeout = uml_net_tx_timeout;
396 dev->set_mac_address = uml_net_set_mac;
397 dev->change_mtu = uml_net_change_mtu;
398 dev->ethtool_ops = ¨_net_ethtool_ops;
399 dev->watchdog_timeo = (HZ >> 1);
400 dev->irq = UM_ETH_IRQ;
403 err = register_netdevice(dev);
407 /* XXX: should we call ->remove() here? */
412 /* lp.user is the first four bytes of the transport data, which
413 * has already been initialized. This structure assignment will
414 * overwrite that, so we make sure that .user gets overwritten with
415 * what it already has.
418 *lp = ((struct uml_net_private)
419 { .list = LIST_HEAD_INIT(lp->list),
422 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
423 .protocol = transport->kern->protocol,
424 .open = transport->user->open,
425 .close = transport->user->close,
426 .remove = transport->user->remove,
427 .read = transport->kern->read,
428 .write = transport->kern->write,
429 .add_address = transport->user->add_address,
430 .delete_address = transport->user->delete_address,
431 .set_mtu = transport->user->set_mtu,
435 spin_lock_init(&lp->lock);
436 lp->tl.function = uml_net_user_timer_expire;
437 memcpy(lp->mac, device->mac, sizeof(lp->mac));
439 if (transport->user->init)
440 (*transport->user->init)(&lp->user, dev);
442 set_ether_mac(dev, device->mac);
447 static struct uml_net *find_device(int n)
449 struct uml_net *device;
450 struct list_head *ele;
452 spin_lock(&devices_lock);
453 list_for_each(ele, &devices){
454 device = list_entry(ele, struct uml_net, list);
455 if(device->index == n)
460 spin_unlock(&devices_lock);
464 static int eth_parse(char *str, int *index_out, char **str_out)
469 n = simple_strtoul(str, &end, 0);
471 printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
475 printk(KERN_ERR "eth_setup: device %d is negative\n", n);
481 "eth_setup: expected '=' after device number\n");
486 printk(KERN_ERR "eth_setup: Device %d already configured\n",
490 if(index_out) *index_out = n;
496 struct list_head list;
501 /* Filled in at boot time. Will need locking if the transports become
504 struct list_head transports = LIST_HEAD_INIT(transports);
506 /* Filled in during early boot */
507 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
509 static int check_transport(struct transport *transport, char *eth, int n,
510 void **init_out, char **mac_out)
514 len = strlen(transport->name);
515 if(strncmp(eth, transport->name, len))
521 else if(*eth != '\0')
524 *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
525 if(*init_out == NULL)
528 if(!transport->setup(eth, mac_out, *init_out)){
535 void register_transport(struct transport *new)
537 struct list_head *ele, *next;
538 struct eth_init *eth;
543 list_add(&new->list, &transports);
545 list_for_each_safe(ele, next, ð_cmd_line){
546 eth = list_entry(ele, struct eth_init, list);
547 match = check_transport(new, eth->init, eth->index, &init,
551 else if(init != NULL){
552 eth_configure(eth->index, init, mac, new);
555 list_del(ð->list);
559 static int eth_setup_common(char *str, int index)
561 struct list_head *ele;
562 struct transport *transport;
566 list_for_each(ele, &transports){
567 transport = list_entry(ele, struct transport, list);
568 if(!check_transport(transport, str, index, &init, &mac))
571 eth_configure(index, init, mac, transport);
579 static int eth_setup(char *str)
581 struct eth_init *new;
584 err = eth_parse(str, &n, &str);
588 new = alloc_bootmem(sizeof(*new));
590 printk("eth_init : alloc_bootmem failed\n");
594 INIT_LIST_HEAD(&new->list);
598 list_add_tail(&new->list, ð_cmd_line);
602 __setup("eth", eth_setup);
603 __uml_help(eth_setup,
604 "eth[0-9]+=<transport>,<options>\n"
605 " Configure a network device.\n\n"
609 static int eth_init(void)
611 struct list_head *ele, *next;
612 struct eth_init *eth;
614 list_for_each_safe(ele, next, ð_cmd_line){
615 eth = list_entry(ele, struct eth_init, list);
617 if(eth_setup_common(eth->init, eth->index))
618 list_del(ð->list);
623 __initcall(eth_init);
626 static int net_config(char *str)
630 err = eth_parse(str, &n, &str);
633 str = kstrdup(str, GFP_KERNEL);
635 printk(KERN_ERR "net_config failed to strdup string\n");
638 err = !eth_setup_common(str, n);
644 static int net_id(char **str, int *start_out, int *end_out)
649 n = simple_strtoul(*str, &end, 0);
650 if((*end != '\0') || (end == *str))
659 static int net_remove(int n)
661 struct uml_net *device;
662 struct net_device *dev;
663 struct uml_net_private *lp;
665 device = find_device(n);
673 if(lp->remove != NULL) (*lp->remove)(&lp->user);
674 unregister_netdev(dev);
675 platform_device_unregister(&device->pdev);
677 list_del(&device->list);
683 static struct mc_device net_mc = {
685 .config = net_config,
688 .remove = net_remove,
691 static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
694 struct in_ifaddr *ifa = ptr;
695 struct net_device *dev = ifa->ifa_dev->dev;
696 struct uml_net_private *lp;
697 void (*proc)(unsigned char *, unsigned char *, void *);
698 unsigned char addr_buf[4], netmask_buf[4];
700 if(dev->open != uml_net_open) return(NOTIFY_DONE);
707 proc = lp->add_address;
710 proc = lp->delete_address;
714 memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
715 memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
716 (*proc)(addr_buf, netmask_buf, &lp->user);
721 struct notifier_block uml_inetaddr_notifier = {
722 .notifier_call = uml_inetaddr_event,
725 static int uml_net_init(void)
727 struct list_head *ele;
728 struct uml_net_private *lp;
729 struct in_device *ip;
730 struct in_ifaddr *in;
732 mconsole_register_dev(&net_mc);
733 register_inetaddr_notifier(¨_inetaddr_notifier);
735 /* Devices may have been opened already, so the uml_inetaddr_notifier
736 * didn't get a chance to run for them. This fakes it so that
737 * addresses which have already been set up get handled properly.
739 list_for_each(ele, &opened){
740 lp = list_entry(ele, struct uml_net_private, list);
741 ip = lp->dev->ip_ptr;
742 if(ip == NULL) continue;
745 uml_inetaddr_event(NULL, NETDEV_UP, in);
753 __initcall(uml_net_init);
755 static void close_devices(void)
757 struct list_head *ele;
758 struct uml_net_private *lp;
760 list_for_each(ele, &opened){
761 lp = list_entry(ele, struct uml_net_private, list);
762 free_irq(lp->dev->irq, lp->dev);
763 if((lp->close != NULL) && (lp->fd >= 0))
764 (*lp->close)(lp->fd, &lp->user);
765 if(lp->remove != NULL) (*lp->remove)(&lp->user);
769 __uml_exitcall(close_devices);
771 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
773 if((skb != NULL) && (skb_tailroom(skb) < extra)){
774 struct sk_buff *skb2;
776 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
780 if(skb != NULL) skb_put(skb, extra);
784 void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
788 struct net_device *dev = d;
789 struct in_device *ip = dev->ip_ptr;
790 struct in_ifaddr *in;
791 unsigned char address[4], netmask[4];
793 if(ip == NULL) return;
796 memcpy(address, &in->ifa_address, sizeof(address));
797 memcpy(netmask, &in->ifa_mask, sizeof(netmask));
798 (*cb)(address, netmask, arg);
803 int dev_netmask(void *d, void *m)
805 struct net_device *dev = d;
806 struct in_device *ip = dev->ip_ptr;
807 struct in_ifaddr *in;
808 __be32 *mask_out = m;
817 *mask_out = in->ifa_mask;
821 void *get_output_buffer(int *len_out)
825 ret = (void *) __get_free_pages(GFP_KERNEL, 0);
826 if(ret) *len_out = PAGE_SIZE;
831 void free_output_buffer(void *buffer)
833 free_pages((unsigned long) buffer, 0);
836 int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
841 remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
843 printk("tap_setup_common - Extra garbage on specification : "
851 unsigned short eth_protocol(struct sk_buff *skb)
853 return(eth_type_trans(skb, skb->dev));
857 * Overrides for Emacs so that we follow Linus's tabbing style.
858 * Emacs will notice this stuff at the end of the file and automatically
859 * adjust the settings for this buffer only. This must remain at the end
861 * ---------------------------------------------------------------------------
863 * c-file-style: "linux"