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 irqreturn_t uml_net_interrupt(int irq, void *dev_id, struct pt_regs *regs)
73 struct net_device *dev = dev_id;
74 struct uml_net_private *lp = dev->priv;
77 if(!netif_running(dev))
81 while((err = uml_net_rx(dev)) > 0) ;
84 "Device '%s' read returned %d, shutting it down\n",
89 reactivate_fd(lp->fd, UM_ETH_IRQ);
92 spin_unlock(&lp->lock);
96 static int uml_net_open(struct net_device *dev)
98 struct uml_net_private *lp = dev->priv;
101 spin_lock(&lp->lock);
109 dev_ip_addr(dev, &lp->mac[2]);
110 set_ether_mac(dev, lp->mac);
113 lp->fd = (*lp->open)(&lp->user);
119 err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
120 SA_INTERRUPT | SA_SHIRQ, dev->name, dev);
122 printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
123 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
128 lp->tl.data = (unsigned long) &lp->user;
129 netif_start_queue(dev);
131 /* clear buffer - it can happen that the host side of the interface
132 * is full when we get here. In this case, new data is never queued,
133 * SIGIOs never arrive, and the net never works.
135 while((err = uml_net_rx(dev)) > 0) ;
138 spin_unlock(&lp->lock);
142 static int uml_net_close(struct net_device *dev)
144 struct uml_net_private *lp = dev->priv;
146 netif_stop_queue(dev);
147 spin_lock(&lp->lock);
149 free_irq(dev->irq, dev);
150 if(lp->close != NULL)
151 (*lp->close)(lp->fd, &lp->user);
155 spin_unlock(&lp->lock);
159 static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
161 struct uml_net_private *lp = dev->priv;
165 netif_stop_queue(dev);
167 spin_lock_irqsave(&lp->lock, flags);
169 len = (*lp->write)(lp->fd, &skb, lp);
171 if(len == skb->len) {
172 lp->stats.tx_packets++;
173 lp->stats.tx_bytes += skb->len;
174 dev->trans_start = jiffies;
175 netif_start_queue(dev);
177 /* this is normally done in the interrupt when tx finishes */
178 netif_wake_queue(dev);
181 netif_start_queue(dev);
182 lp->stats.tx_dropped++;
185 netif_start_queue(dev);
186 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
189 spin_unlock_irqrestore(&lp->lock, flags);
196 static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
198 struct uml_net_private *lp = dev->priv;
202 static void uml_net_set_multicast_list(struct net_device *dev)
204 if (dev->flags & IFF_PROMISC) return;
205 else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
206 else dev->flags &= ~IFF_ALLMULTI;
209 static void uml_net_tx_timeout(struct net_device *dev)
211 dev->trans_start = jiffies;
212 netif_wake_queue(dev);
215 static int uml_net_set_mac(struct net_device *dev, void *addr)
217 struct uml_net_private *lp = dev->priv;
218 struct sockaddr *hwaddr = addr;
220 spin_lock(&lp->lock);
221 memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
222 spin_unlock(&lp->lock);
227 static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
229 struct uml_net_private *lp = dev->priv;
232 spin_lock(&lp->lock);
234 new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
243 spin_unlock(&lp->lock);
247 static void uml_net_get_drvinfo(struct net_device *dev,
248 struct ethtool_drvinfo *info)
250 strcpy(info->driver, DRIVER_NAME);
251 strcpy(info->version, "42");
254 static struct ethtool_ops uml_net_ethtool_ops = {
255 .get_drvinfo = uml_net_get_drvinfo,
256 .get_link = ethtool_op_get_link,
259 void uml_net_user_timer_expire(unsigned long _conn)
262 struct connection *conn = (struct connection *)_conn;
264 dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
269 static DEFINE_SPINLOCK(devices_lock);
270 static LIST_HEAD(devices);
272 static struct platform_driver uml_net_driver = {
277 static int driver_registered;
279 static int eth_configure(int n, void *init, char *mac,
280 struct transport *transport)
282 struct uml_net *device;
283 struct net_device *dev;
284 struct uml_net_private *lp;
287 size = transport->private_size + sizeof(struct uml_net_private) +
288 sizeof(((struct uml_net_private *) 0)->user);
290 device = kmalloc(sizeof(*device), GFP_KERNEL);
291 if (device == NULL) {
292 printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
296 memset(device, 0, sizeof(*device));
297 INIT_LIST_HEAD(&device->list);
300 spin_lock(&devices_lock);
301 list_add(&device->list, &devices);
302 spin_unlock(&devices_lock);
304 if (setup_etheraddr(mac, device->mac))
305 device->have_mac = 1;
307 printk(KERN_INFO "Netdevice %d ", n);
308 if (device->have_mac)
309 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
310 device->mac[0], device->mac[1],
311 device->mac[2], device->mac[3],
312 device->mac[4], device->mac[5]);
314 dev = alloc_etherdev(size);
316 printk(KERN_ERR "eth_configure: failed to allocate device\n");
321 /* This points to the transport private data. It's still clear, but we
322 * must memset it to 0 *now*. Let's help the drivers. */
326 if (!driver_registered) {
327 platform_driver_register(¨_net_driver);
328 driver_registered = 1;
331 device->pdev.name = DRIVER_NAME;
332 platform_device_register(&device->pdev);
333 SET_NETDEV_DEV(dev,&device->pdev.dev);
335 /* If this name ends up conflicting with an existing registered
336 * netdevice, that is OK, register_netdev{,ice}() will notice this
339 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
342 (*transport->kern->init)(dev, init);
344 dev->mtu = transport->user->max_packet;
345 dev->open = uml_net_open;
346 dev->hard_start_xmit = uml_net_start_xmit;
347 dev->stop = uml_net_close;
348 dev->get_stats = uml_net_get_stats;
349 dev->set_multicast_list = uml_net_set_multicast_list;
350 dev->tx_timeout = uml_net_tx_timeout;
351 dev->set_mac_address = uml_net_set_mac;
352 dev->change_mtu = uml_net_change_mtu;
353 dev->ethtool_ops = ¨_net_ethtool_ops;
354 dev->watchdog_timeo = (HZ >> 1);
355 dev->irq = UM_ETH_IRQ;
358 err = register_netdevice(dev);
362 /* XXX: should we call ->remove() here? */
367 /* lp.user is the first four bytes of the transport data, which
368 * has already been initialized. This structure assignment will
369 * overwrite that, so we make sure that .user gets overwritten with
370 * what it already has.
373 *lp = ((struct uml_net_private)
374 { .list = LIST_HEAD_INIT(lp->list),
377 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
378 .have_mac = device->have_mac,
379 .protocol = transport->kern->protocol,
380 .open = transport->user->open,
381 .close = transport->user->close,
382 .remove = transport->user->remove,
383 .read = transport->kern->read,
384 .write = transport->kern->write,
385 .add_address = transport->user->add_address,
386 .delete_address = transport->user->delete_address,
387 .set_mtu = transport->user->set_mtu,
391 spin_lock_init(&lp->lock);
392 lp->tl.function = uml_net_user_timer_expire;
394 memcpy(lp->mac, device->mac, sizeof(lp->mac));
396 if (transport->user->init)
397 (*transport->user->init)(&lp->user, dev);
399 if (device->have_mac)
400 set_ether_mac(dev, device->mac);
402 spin_lock(&opened_lock);
403 list_add(&lp->list, &opened);
404 spin_unlock(&opened_lock);
409 static struct uml_net *find_device(int n)
411 struct uml_net *device;
412 struct list_head *ele;
414 spin_lock(&devices_lock);
415 list_for_each(ele, &devices){
416 device = list_entry(ele, struct uml_net, list);
417 if(device->index == n)
422 spin_unlock(&devices_lock);
426 static int eth_parse(char *str, int *index_out, char **str_out)
431 n = simple_strtoul(str, &end, 0);
433 printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
437 printk(KERN_ERR "eth_setup: device %d is negative\n", n);
443 "eth_setup: expected '=' after device number\n");
448 printk(KERN_ERR "eth_setup: Device %d already configured\n",
452 if(index_out) *index_out = n;
458 struct list_head list;
463 /* Filled in at boot time. Will need locking if the transports become
466 struct list_head transports = LIST_HEAD_INIT(transports);
468 /* Filled in during early boot */
469 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
471 static int check_transport(struct transport *transport, char *eth, int n,
472 void **init_out, char **mac_out)
476 len = strlen(transport->name);
477 if(strncmp(eth, transport->name, len))
483 else if(*eth != '\0')
486 *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
487 if(*init_out == NULL)
490 if(!transport->setup(eth, mac_out, *init_out)){
497 void register_transport(struct transport *new)
499 struct list_head *ele, *next;
500 struct eth_init *eth;
505 list_add(&new->list, &transports);
507 list_for_each_safe(ele, next, ð_cmd_line){
508 eth = list_entry(ele, struct eth_init, list);
509 match = check_transport(new, eth->init, eth->index, &init,
513 else if(init != NULL){
514 eth_configure(eth->index, init, mac, new);
517 list_del(ð->list);
521 static int eth_setup_common(char *str, int index)
523 struct list_head *ele;
524 struct transport *transport;
528 list_for_each(ele, &transports){
529 transport = list_entry(ele, struct transport, list);
530 if(!check_transport(transport, str, index, &init, &mac))
533 eth_configure(index, init, mac, transport);
541 static int eth_setup(char *str)
543 struct eth_init *new;
546 err = eth_parse(str, &n, &str);
549 new = alloc_bootmem(sizeof(new));
551 printk("eth_init : alloc_bootmem failed\n");
555 INIT_LIST_HEAD(&new->list);
559 list_add_tail(&new->list, ð_cmd_line);
563 __setup("eth", eth_setup);
564 __uml_help(eth_setup,
565 "eth[0-9]+=<transport>,<options>\n"
566 " Configure a network device.\n\n"
570 static int eth_init(void)
572 struct list_head *ele, *next;
573 struct eth_init *eth;
575 list_for_each_safe(ele, next, ð_cmd_line){
576 eth = list_entry(ele, struct eth_init, list);
578 if(eth_setup_common(eth->init, eth->index))
579 list_del(ð->list);
584 __initcall(eth_init);
587 static int net_config(char *str)
591 err = eth_parse(str, &n, &str);
594 str = kstrdup(str, GFP_KERNEL);
596 printk(KERN_ERR "net_config failed to strdup string\n");
599 err = !eth_setup_common(str, n);
605 static int net_id(char **str, int *start_out, int *end_out)
610 n = simple_strtoul(*str, &end, 0);
611 if((*end != '\0') || (end == *str))
620 static int net_remove(int n)
622 struct uml_net *device;
623 struct net_device *dev;
624 struct uml_net_private *lp;
626 device = find_device(n);
634 if(lp->remove != NULL) (*lp->remove)(&lp->user);
635 unregister_netdev(dev);
636 platform_device_unregister(&device->pdev);
638 list_del(&device->list);
644 static struct mc_device net_mc = {
646 .config = net_config,
649 .remove = net_remove,
652 static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
655 struct in_ifaddr *ifa = ptr;
656 struct net_device *dev = ifa->ifa_dev->dev;
657 struct uml_net_private *lp;
658 void (*proc)(unsigned char *, unsigned char *, void *);
659 unsigned char addr_buf[4], netmask_buf[4];
661 if(dev->open != uml_net_open) return(NOTIFY_DONE);
668 proc = lp->add_address;
671 proc = lp->delete_address;
675 memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
676 memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
677 (*proc)(addr_buf, netmask_buf, &lp->user);
682 struct notifier_block uml_inetaddr_notifier = {
683 .notifier_call = uml_inetaddr_event,
686 static int uml_net_init(void)
688 struct list_head *ele;
689 struct uml_net_private *lp;
690 struct in_device *ip;
691 struct in_ifaddr *in;
693 mconsole_register_dev(&net_mc);
694 register_inetaddr_notifier(¨_inetaddr_notifier);
696 /* Devices may have been opened already, so the uml_inetaddr_notifier
697 * didn't get a chance to run for them. This fakes it so that
698 * addresses which have already been set up get handled properly.
700 list_for_each(ele, &opened){
701 lp = list_entry(ele, struct uml_net_private, list);
702 ip = lp->dev->ip_ptr;
703 if(ip == NULL) continue;
706 uml_inetaddr_event(NULL, NETDEV_UP, in);
714 __initcall(uml_net_init);
716 static void close_devices(void)
718 struct list_head *ele;
719 struct uml_net_private *lp;
721 list_for_each(ele, &opened){
722 lp = list_entry(ele, struct uml_net_private, list);
723 free_irq(lp->dev->irq, lp->dev);
724 if((lp->close != NULL) && (lp->fd >= 0))
725 (*lp->close)(lp->fd, &lp->user);
726 if(lp->remove != NULL) (*lp->remove)(&lp->user);
730 __uml_exitcall(close_devices);
732 int setup_etheraddr(char *str, unsigned char *addr)
740 addr[i] = simple_strtoul(str, &end, 16);
742 ((*end != ':') && (*end != ',') && (*end != '\0'))){
744 "setup_etheraddr: failed to parse '%s' "
745 "as an ethernet address\n", str);
752 "Attempt to assign a broadcast ethernet address to a "
753 "device disallowed\n");
759 void dev_ip_addr(void *d, unsigned char *bin_buf)
761 struct net_device *dev = d;
762 struct in_device *ip = dev->ip_ptr;
763 struct in_ifaddr *in;
765 if((ip == NULL) || ((in = ip->ifa_list) == NULL)){
766 printk(KERN_WARNING "dev_ip_addr - device not assigned an "
770 memcpy(bin_buf, &in->ifa_address, sizeof(in->ifa_address));
773 void set_ether_mac(void *d, unsigned char *addr)
775 struct net_device *dev = d;
777 memcpy(dev->dev_addr, addr, ETH_ALEN);
780 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
782 if((skb != NULL) && (skb_tailroom(skb) < extra)){
783 struct sk_buff *skb2;
785 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
789 if(skb != NULL) skb_put(skb, extra);
793 void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
797 struct net_device *dev = d;
798 struct in_device *ip = dev->ip_ptr;
799 struct in_ifaddr *in;
800 unsigned char address[4], netmask[4];
802 if(ip == NULL) return;
805 memcpy(address, &in->ifa_address, sizeof(address));
806 memcpy(netmask, &in->ifa_mask, sizeof(netmask));
807 (*cb)(address, netmask, arg);
812 int dev_netmask(void *d, void *m)
814 struct net_device *dev = d;
815 struct in_device *ip = dev->ip_ptr;
816 struct in_ifaddr *in;
826 *mask_out = in->ifa_mask;
830 void *get_output_buffer(int *len_out)
834 ret = (void *) __get_free_pages(GFP_KERNEL, 0);
835 if(ret) *len_out = PAGE_SIZE;
840 void free_output_buffer(void *buffer)
842 free_pages((unsigned long) buffer, 0);
845 int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
850 remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
852 printk("tap_setup_common - Extra garbage on specification : "
860 unsigned short eth_protocol(struct sk_buff *skb)
862 return(eth_type_trans(skb, skb->dev));
866 * Overrides for Emacs so that we follow Linus's tabbing style.
867 * Emacs will notice this stuff at the end of the file and automatically
868 * adjust the settings for this buffer only. This must remain at the end
870 * ---------------------------------------------------------------------------
872 * c-file-style: "linux"