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 static inline void set_ether_mac(struct net_device *dev, unsigned char *addr)
36 memcpy(dev->dev_addr, addr, ETH_ALEN);
39 #define DRIVER_NAME "uml-netdev"
41 static DEFINE_SPINLOCK(opened_lock);
42 static LIST_HEAD(opened);
44 static int uml_net_rx(struct net_device *dev)
46 struct uml_net_private *lp = dev->priv;
50 /* If we can't allocate memory, try again next round. */
51 skb = dev_alloc_skb(dev->mtu);
53 lp->stats.rx_dropped++;
58 skb_put(skb, dev->mtu);
59 skb->mac.raw = skb->data;
60 pkt_len = (*lp->read)(lp->fd, &skb, lp);
63 skb_trim(skb, pkt_len);
64 skb->protocol = (*lp->protocol)(skb);
67 lp->stats.rx_bytes += skb->len;
68 lp->stats.rx_packets++;
76 static void uml_dev_close(void* dev)
78 dev_close( (struct net_device *) dev);
81 irqreturn_t uml_net_interrupt(int irq, void *dev_id, struct pt_regs *regs)
83 struct net_device *dev = dev_id;
84 struct uml_net_private *lp = dev->priv;
87 if(!netif_running(dev))
91 while((err = uml_net_rx(dev)) > 0) ;
93 DECLARE_WORK(close_work, uml_dev_close, dev);
95 "Device '%s' read returned %d, shutting it down\n",
97 /* dev_close can't be called in interrupt context, and takes
99 * And dev_close() can be safely called multiple times on the
100 * same device, since it tests for (dev->flags & IFF_UP). So
101 * there's no harm in delaying the device shutdown. */
102 schedule_work(&close_work);
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;
117 spin_lock(&lp->lock);
125 dev_ip_addr(dev, &lp->mac[2]);
126 set_ether_mac(dev, lp->mac);
129 lp->fd = (*lp->open)(&lp->user);
135 err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
136 IRQF_DISABLED | IRQF_SHARED, dev->name, dev);
138 printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
143 lp->tl.data = (unsigned long) &lp->user;
144 netif_start_queue(dev);
146 /* clear buffer - it can happen that the host side of the interface
147 * is full when we get here. In this case, new data is never queued,
148 * SIGIOs never arrive, and the net never works.
150 while((err = uml_net_rx(dev)) > 0) ;
152 spin_unlock(&lp->lock);
154 spin_lock(&opened_lock);
155 list_add(&lp->list, &opened);
156 spin_unlock(&opened_lock);
160 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
163 spin_unlock(&lp->lock);
167 static int uml_net_close(struct net_device *dev)
169 struct uml_net_private *lp = dev->priv;
171 netif_stop_queue(dev);
172 spin_lock(&lp->lock);
174 free_irq(dev->irq, dev);
175 if(lp->close != NULL)
176 (*lp->close)(lp->fd, &lp->user);
179 spin_unlock(&lp->lock);
181 spin_lock(&opened_lock);
183 spin_unlock(&opened_lock);
188 static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
190 struct uml_net_private *lp = dev->priv;
194 netif_stop_queue(dev);
196 spin_lock_irqsave(&lp->lock, flags);
198 len = (*lp->write)(lp->fd, &skb, lp);
200 if(len == skb->len) {
201 lp->stats.tx_packets++;
202 lp->stats.tx_bytes += skb->len;
203 dev->trans_start = jiffies;
204 netif_start_queue(dev);
206 /* this is normally done in the interrupt when tx finishes */
207 netif_wake_queue(dev);
210 netif_start_queue(dev);
211 lp->stats.tx_dropped++;
214 netif_start_queue(dev);
215 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
218 spin_unlock_irqrestore(&lp->lock, flags);
225 static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
227 struct uml_net_private *lp = dev->priv;
231 static void uml_net_set_multicast_list(struct net_device *dev)
233 if (dev->flags & IFF_PROMISC) return;
234 else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
235 else dev->flags &= ~IFF_ALLMULTI;
238 static void uml_net_tx_timeout(struct net_device *dev)
240 dev->trans_start = jiffies;
241 netif_wake_queue(dev);
244 static int uml_net_set_mac(struct net_device *dev, void *addr)
246 struct uml_net_private *lp = dev->priv;
247 struct sockaddr *hwaddr = addr;
249 spin_lock(&lp->lock);
250 set_ether_mac(dev, hwaddr->sa_data);
251 spin_unlock(&lp->lock);
256 static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
258 struct uml_net_private *lp = dev->priv;
261 spin_lock(&lp->lock);
263 new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
272 spin_unlock(&lp->lock);
276 static void uml_net_get_drvinfo(struct net_device *dev,
277 struct ethtool_drvinfo *info)
279 strcpy(info->driver, DRIVER_NAME);
280 strcpy(info->version, "42");
283 static struct ethtool_ops uml_net_ethtool_ops = {
284 .get_drvinfo = uml_net_get_drvinfo,
285 .get_link = ethtool_op_get_link,
288 void uml_net_user_timer_expire(unsigned long _conn)
291 struct connection *conn = (struct connection *)_conn;
293 dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
298 static DEFINE_SPINLOCK(devices_lock);
299 static LIST_HEAD(devices);
301 static struct platform_driver uml_net_driver = {
306 static int driver_registered;
308 static int eth_configure(int n, void *init, char *mac,
309 struct transport *transport)
311 struct uml_net *device;
312 struct net_device *dev;
313 struct uml_net_private *lp;
316 size = transport->private_size + sizeof(struct uml_net_private) +
317 sizeof(((struct uml_net_private *) 0)->user);
319 device = kmalloc(sizeof(*device), GFP_KERNEL);
320 if (device == NULL) {
321 printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
325 memset(device, 0, sizeof(*device));
326 INIT_LIST_HEAD(&device->list);
329 spin_lock(&devices_lock);
330 list_add(&device->list, &devices);
331 spin_unlock(&devices_lock);
333 if (setup_etheraddr(mac, device->mac))
334 device->have_mac = 1;
336 printk(KERN_INFO "Netdevice %d ", n);
337 if (device->have_mac)
338 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
339 device->mac[0], device->mac[1],
340 device->mac[2], device->mac[3],
341 device->mac[4], device->mac[5]);
343 dev = alloc_etherdev(size);
345 printk(KERN_ERR "eth_configure: failed to allocate device\n");
350 /* This points to the transport private data. It's still clear, but we
351 * must memset it to 0 *now*. Let's help the drivers. */
355 if (!driver_registered) {
356 platform_driver_register(¨_net_driver);
357 driver_registered = 1;
360 device->pdev.name = DRIVER_NAME;
361 platform_device_register(&device->pdev);
362 SET_NETDEV_DEV(dev,&device->pdev.dev);
364 /* If this name ends up conflicting with an existing registered
365 * netdevice, that is OK, register_netdev{,ice}() will notice this
368 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
371 (*transport->kern->init)(dev, init);
373 dev->mtu = transport->user->max_packet;
374 dev->open = uml_net_open;
375 dev->hard_start_xmit = uml_net_start_xmit;
376 dev->stop = uml_net_close;
377 dev->get_stats = uml_net_get_stats;
378 dev->set_multicast_list = uml_net_set_multicast_list;
379 dev->tx_timeout = uml_net_tx_timeout;
380 dev->set_mac_address = uml_net_set_mac;
381 dev->change_mtu = uml_net_change_mtu;
382 dev->ethtool_ops = ¨_net_ethtool_ops;
383 dev->watchdog_timeo = (HZ >> 1);
384 dev->irq = UM_ETH_IRQ;
387 err = register_netdevice(dev);
391 /* XXX: should we call ->remove() here? */
396 /* lp.user is the first four bytes of the transport data, which
397 * has already been initialized. This structure assignment will
398 * overwrite that, so we make sure that .user gets overwritten with
399 * what it already has.
402 *lp = ((struct uml_net_private)
403 { .list = LIST_HEAD_INIT(lp->list),
406 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
407 .have_mac = device->have_mac,
408 .protocol = transport->kern->protocol,
409 .open = transport->user->open,
410 .close = transport->user->close,
411 .remove = transport->user->remove,
412 .read = transport->kern->read,
413 .write = transport->kern->write,
414 .add_address = transport->user->add_address,
415 .delete_address = transport->user->delete_address,
416 .set_mtu = transport->user->set_mtu,
420 spin_lock_init(&lp->lock);
421 lp->tl.function = uml_net_user_timer_expire;
423 memcpy(lp->mac, device->mac, sizeof(lp->mac));
425 if (transport->user->init)
426 (*transport->user->init)(&lp->user, dev);
428 if (device->have_mac)
429 set_ether_mac(dev, device->mac);
434 static struct uml_net *find_device(int n)
436 struct uml_net *device;
437 struct list_head *ele;
439 spin_lock(&devices_lock);
440 list_for_each(ele, &devices){
441 device = list_entry(ele, struct uml_net, list);
442 if(device->index == n)
447 spin_unlock(&devices_lock);
451 static int eth_parse(char *str, int *index_out, char **str_out)
456 n = simple_strtoul(str, &end, 0);
458 printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
462 printk(KERN_ERR "eth_setup: device %d is negative\n", n);
468 "eth_setup: expected '=' after device number\n");
473 printk(KERN_ERR "eth_setup: Device %d already configured\n",
477 if(index_out) *index_out = n;
483 struct list_head list;
488 /* Filled in at boot time. Will need locking if the transports become
491 struct list_head transports = LIST_HEAD_INIT(transports);
493 /* Filled in during early boot */
494 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
496 static int check_transport(struct transport *transport, char *eth, int n,
497 void **init_out, char **mac_out)
501 len = strlen(transport->name);
502 if(strncmp(eth, transport->name, len))
508 else if(*eth != '\0')
511 *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
512 if(*init_out == NULL)
515 if(!transport->setup(eth, mac_out, *init_out)){
522 void register_transport(struct transport *new)
524 struct list_head *ele, *next;
525 struct eth_init *eth;
530 list_add(&new->list, &transports);
532 list_for_each_safe(ele, next, ð_cmd_line){
533 eth = list_entry(ele, struct eth_init, list);
534 match = check_transport(new, eth->init, eth->index, &init,
538 else if(init != NULL){
539 eth_configure(eth->index, init, mac, new);
542 list_del(ð->list);
546 static int eth_setup_common(char *str, int index)
548 struct list_head *ele;
549 struct transport *transport;
553 list_for_each(ele, &transports){
554 transport = list_entry(ele, struct transport, list);
555 if(!check_transport(transport, str, index, &init, &mac))
558 eth_configure(index, init, mac, transport);
566 static int eth_setup(char *str)
568 struct eth_init *new;
571 err = eth_parse(str, &n, &str);
574 new = alloc_bootmem(sizeof(new));
576 printk("eth_init : alloc_bootmem failed\n");
580 INIT_LIST_HEAD(&new->list);
584 list_add_tail(&new->list, ð_cmd_line);
588 __setup("eth", eth_setup);
589 __uml_help(eth_setup,
590 "eth[0-9]+=<transport>,<options>\n"
591 " Configure a network device.\n\n"
595 static int eth_init(void)
597 struct list_head *ele, *next;
598 struct eth_init *eth;
600 list_for_each_safe(ele, next, ð_cmd_line){
601 eth = list_entry(ele, struct eth_init, list);
603 if(eth_setup_common(eth->init, eth->index))
604 list_del(ð->list);
609 __initcall(eth_init);
612 static int net_config(char *str)
616 err = eth_parse(str, &n, &str);
619 str = kstrdup(str, GFP_KERNEL);
621 printk(KERN_ERR "net_config failed to strdup string\n");
624 err = !eth_setup_common(str, n);
630 static int net_id(char **str, int *start_out, int *end_out)
635 n = simple_strtoul(*str, &end, 0);
636 if((*end != '\0') || (end == *str))
645 static int net_remove(int n)
647 struct uml_net *device;
648 struct net_device *dev;
649 struct uml_net_private *lp;
651 device = find_device(n);
659 if(lp->remove != NULL) (*lp->remove)(&lp->user);
660 unregister_netdev(dev);
661 platform_device_unregister(&device->pdev);
663 list_del(&device->list);
669 static struct mc_device net_mc = {
671 .config = net_config,
674 .remove = net_remove,
677 static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
680 struct in_ifaddr *ifa = ptr;
681 struct net_device *dev = ifa->ifa_dev->dev;
682 struct uml_net_private *lp;
683 void (*proc)(unsigned char *, unsigned char *, void *);
684 unsigned char addr_buf[4], netmask_buf[4];
686 if(dev->open != uml_net_open) return(NOTIFY_DONE);
693 proc = lp->add_address;
696 proc = lp->delete_address;
700 memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
701 memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
702 (*proc)(addr_buf, netmask_buf, &lp->user);
707 struct notifier_block uml_inetaddr_notifier = {
708 .notifier_call = uml_inetaddr_event,
711 static int uml_net_init(void)
713 struct list_head *ele;
714 struct uml_net_private *lp;
715 struct in_device *ip;
716 struct in_ifaddr *in;
718 mconsole_register_dev(&net_mc);
719 register_inetaddr_notifier(¨_inetaddr_notifier);
721 /* Devices may have been opened already, so the uml_inetaddr_notifier
722 * didn't get a chance to run for them. This fakes it so that
723 * addresses which have already been set up get handled properly.
725 list_for_each(ele, &opened){
726 lp = list_entry(ele, struct uml_net_private, list);
727 ip = lp->dev->ip_ptr;
728 if(ip == NULL) continue;
731 uml_inetaddr_event(NULL, NETDEV_UP, in);
739 __initcall(uml_net_init);
741 static void close_devices(void)
743 struct list_head *ele;
744 struct uml_net_private *lp;
746 list_for_each(ele, &opened){
747 lp = list_entry(ele, struct uml_net_private, list);
748 free_irq(lp->dev->irq, lp->dev);
749 if((lp->close != NULL) && (lp->fd >= 0))
750 (*lp->close)(lp->fd, &lp->user);
751 if(lp->remove != NULL) (*lp->remove)(&lp->user);
755 __uml_exitcall(close_devices);
757 int setup_etheraddr(char *str, unsigned char *addr)
765 addr[i] = simple_strtoul(str, &end, 16);
767 ((*end != ':') && (*end != ',') && (*end != '\0'))){
769 "setup_etheraddr: failed to parse '%s' "
770 "as an ethernet address\n", str);
777 "Attempt to assign a broadcast ethernet address to a "
778 "device disallowed\n");
784 void dev_ip_addr(void *d, unsigned char *bin_buf)
786 struct net_device *dev = d;
787 struct in_device *ip = dev->ip_ptr;
788 struct in_ifaddr *in;
790 if((ip == NULL) || ((in = ip->ifa_list) == NULL)){
791 printk(KERN_WARNING "dev_ip_addr - device not assigned an "
795 memcpy(bin_buf, &in->ifa_address, sizeof(in->ifa_address));
798 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
800 if((skb != NULL) && (skb_tailroom(skb) < extra)){
801 struct sk_buff *skb2;
803 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
807 if(skb != NULL) skb_put(skb, extra);
811 void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
815 struct net_device *dev = d;
816 struct in_device *ip = dev->ip_ptr;
817 struct in_ifaddr *in;
818 unsigned char address[4], netmask[4];
820 if(ip == NULL) return;
823 memcpy(address, &in->ifa_address, sizeof(address));
824 memcpy(netmask, &in->ifa_mask, sizeof(netmask));
825 (*cb)(address, netmask, arg);
830 int dev_netmask(void *d, void *m)
832 struct net_device *dev = d;
833 struct in_device *ip = dev->ip_ptr;
834 struct in_ifaddr *in;
844 *mask_out = in->ifa_mask;
848 void *get_output_buffer(int *len_out)
852 ret = (void *) __get_free_pages(GFP_KERNEL, 0);
853 if(ret) *len_out = PAGE_SIZE;
858 void free_output_buffer(void *buffer)
860 free_pages((unsigned long) buffer, 0);
863 int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
868 remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
870 printk("tap_setup_common - Extra garbage on specification : "
878 unsigned short eth_protocol(struct sk_buff *skb)
880 return(eth_type_trans(skb, skb->dev));
884 * Overrides for Emacs so that we follow Linus's tabbing style.
885 * Emacs will notice this stuff at the end of the file and automatically
886 * adjust the settings for this buffer only. This must remain at the end
888 * ---------------------------------------------------------------------------
890 * c-file-style: "linux"