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);
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);
154 spin_unlock(&lp->lock);
158 static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
160 struct uml_net_private *lp = dev->priv;
164 netif_stop_queue(dev);
166 spin_lock_irqsave(&lp->lock, flags);
168 len = (*lp->write)(lp->fd, &skb, lp);
170 if(len == skb->len) {
171 lp->stats.tx_packets++;
172 lp->stats.tx_bytes += skb->len;
173 dev->trans_start = jiffies;
174 netif_start_queue(dev);
176 /* this is normally done in the interrupt when tx finishes */
177 netif_wake_queue(dev);
180 netif_start_queue(dev);
181 lp->stats.tx_dropped++;
184 netif_start_queue(dev);
185 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
188 spin_unlock_irqrestore(&lp->lock, flags);
195 static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
197 struct uml_net_private *lp = dev->priv;
201 static void uml_net_set_multicast_list(struct net_device *dev)
203 if (dev->flags & IFF_PROMISC) return;
204 else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
205 else dev->flags &= ~IFF_ALLMULTI;
208 static void uml_net_tx_timeout(struct net_device *dev)
210 dev->trans_start = jiffies;
211 netif_wake_queue(dev);
214 static int uml_net_set_mac(struct net_device *dev, void *addr)
216 struct uml_net_private *lp = dev->priv;
217 struct sockaddr *hwaddr = addr;
219 spin_lock(&lp->lock);
220 memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
221 spin_unlock(&lp->lock);
226 static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
228 struct uml_net_private *lp = dev->priv;
231 spin_lock(&lp->lock);
233 new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
242 spin_unlock(&lp->lock);
246 static void uml_net_get_drvinfo(struct net_device *dev,
247 struct ethtool_drvinfo *info)
249 strcpy(info->driver, DRIVER_NAME);
250 strcpy(info->version, "42");
253 static struct ethtool_ops uml_net_ethtool_ops = {
254 .get_drvinfo = uml_net_get_drvinfo,
255 .get_link = ethtool_op_get_link,
258 void uml_net_user_timer_expire(unsigned long _conn)
261 struct connection *conn = (struct connection *)_conn;
263 dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
268 static DEFINE_SPINLOCK(devices_lock);
269 static struct list_head devices = LIST_HEAD_INIT(devices);
271 static struct platform_driver uml_net_driver = {
276 static int driver_registered;
278 static int eth_configure(int n, void *init, char *mac,
279 struct transport *transport)
281 struct uml_net *device;
282 struct net_device *dev;
283 struct uml_net_private *lp;
286 size = transport->private_size + sizeof(struct uml_net_private) +
287 sizeof(((struct uml_net_private *) 0)->user);
289 device = kmalloc(sizeof(*device), GFP_KERNEL);
290 if (device == NULL) {
291 printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
295 memset(device, 0, sizeof(*device));
296 INIT_LIST_HEAD(&device->list);
299 spin_lock(&devices_lock);
300 list_add(&device->list, &devices);
301 spin_unlock(&devices_lock);
303 if (setup_etheraddr(mac, device->mac))
304 device->have_mac = 1;
306 printk(KERN_INFO "Netdevice %d ", n);
307 if (device->have_mac)
308 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
309 device->mac[0], device->mac[1],
310 device->mac[2], device->mac[3],
311 device->mac[4], device->mac[5]);
313 dev = alloc_etherdev(size);
315 printk(KERN_ERR "eth_configure: failed to allocate device\n");
320 if (!driver_registered) {
321 platform_driver_register(¨_net_driver);
322 driver_registered = 1;
325 device->pdev.name = DRIVER_NAME;
326 platform_device_register(&device->pdev);
327 SET_NETDEV_DEV(dev,&device->pdev.dev);
329 /* If this name ends up conflicting with an existing registered
330 * netdevice, that is OK, register_netdev{,ice}() will notice this
333 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
336 (*transport->kern->init)(dev, init);
338 dev->mtu = transport->user->max_packet;
339 dev->open = uml_net_open;
340 dev->hard_start_xmit = uml_net_start_xmit;
341 dev->stop = uml_net_close;
342 dev->get_stats = uml_net_get_stats;
343 dev->set_multicast_list = uml_net_set_multicast_list;
344 dev->tx_timeout = uml_net_tx_timeout;
345 dev->set_mac_address = uml_net_set_mac;
346 dev->change_mtu = uml_net_change_mtu;
347 dev->ethtool_ops = ¨_net_ethtool_ops;
348 dev->watchdog_timeo = (HZ >> 1);
349 dev->irq = UM_ETH_IRQ;
352 err = register_netdevice(dev);
356 /* XXX: should we call ->remove() here? */
362 /* lp.user is the first four bytes of the transport data, which
363 * has already been initialized. This structure assignment will
364 * overwrite that, so we make sure that .user gets overwritten with
365 * what it already has.
368 *lp = ((struct uml_net_private)
369 { .list = LIST_HEAD_INIT(lp->list),
372 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
373 .have_mac = device->have_mac,
374 .protocol = transport->kern->protocol,
375 .open = transport->user->open,
376 .close = transport->user->close,
377 .remove = transport->user->remove,
378 .read = transport->kern->read,
379 .write = transport->kern->write,
380 .add_address = transport->user->add_address,
381 .delete_address = transport->user->delete_address,
382 .set_mtu = transport->user->set_mtu,
386 spin_lock_init(&lp->lock);
387 lp->tl.function = uml_net_user_timer_expire;
389 memcpy(lp->mac, device->mac, sizeof(lp->mac));
391 if (transport->user->init)
392 (*transport->user->init)(&lp->user, dev);
394 if (device->have_mac)
395 set_ether_mac(dev, device->mac);
397 spin_lock(&opened_lock);
398 list_add(&lp->list, &opened);
399 spin_unlock(&opened_lock);
404 static struct uml_net *find_device(int n)
406 struct uml_net *device;
407 struct list_head *ele;
409 spin_lock(&devices_lock);
410 list_for_each(ele, &devices){
411 device = list_entry(ele, struct uml_net, list);
412 if(device->index == n)
417 spin_unlock(&devices_lock);
421 static int eth_parse(char *str, int *index_out, char **str_out)
426 n = simple_strtoul(str, &end, 0);
428 printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
432 printk(KERN_ERR "eth_setup: device %d is negative\n", n);
438 "eth_setup: expected '=' after device number\n");
443 printk(KERN_ERR "eth_setup: Device %d already configured\n",
447 if(index_out) *index_out = n;
453 struct list_head list;
458 /* Filled in at boot time. Will need locking if the transports become
461 struct list_head transports = LIST_HEAD_INIT(transports);
463 /* Filled in during early boot */
464 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
466 static int check_transport(struct transport *transport, char *eth, int n,
467 void **init_out, char **mac_out)
471 len = strlen(transport->name);
472 if(strncmp(eth, transport->name, len))
478 else if(*eth != '\0')
481 *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
482 if(*init_out == NULL)
485 if(!transport->setup(eth, mac_out, *init_out)){
492 void register_transport(struct transport *new)
494 struct list_head *ele, *next;
495 struct eth_init *eth;
500 list_add(&new->list, &transports);
502 list_for_each_safe(ele, next, ð_cmd_line){
503 eth = list_entry(ele, struct eth_init, list);
504 match = check_transport(new, eth->init, eth->index, &init,
508 else if(init != NULL){
509 eth_configure(eth->index, init, mac, new);
512 list_del(ð->list);
516 static int eth_setup_common(char *str, int index)
518 struct list_head *ele;
519 struct transport *transport;
523 list_for_each(ele, &transports){
524 transport = list_entry(ele, struct transport, list);
525 if(!check_transport(transport, str, index, &init, &mac))
528 eth_configure(index, init, mac, transport);
536 static int eth_setup(char *str)
538 struct eth_init *new;
541 err = eth_parse(str, &n, &str);
544 new = alloc_bootmem(sizeof(new));
546 printk("eth_init : alloc_bootmem failed\n");
550 INIT_LIST_HEAD(&new->list);
554 list_add_tail(&new->list, ð_cmd_line);
558 __setup("eth", eth_setup);
559 __uml_help(eth_setup,
560 "eth[0-9]+=<transport>,<options>\n"
561 " Configure a network device.\n\n"
565 static int eth_init(void)
567 struct list_head *ele, *next;
568 struct eth_init *eth;
570 list_for_each_safe(ele, next, ð_cmd_line){
571 eth = list_entry(ele, struct eth_init, list);
573 if(eth_setup_common(eth->init, eth->index))
574 list_del(ð->list);
579 __initcall(eth_init);
582 static int net_config(char *str)
586 err = eth_parse(str, &n, &str);
589 str = uml_strdup(str);
591 printk(KERN_ERR "net_config failed to strdup string\n");
594 err = !eth_setup_common(str, n);
600 static int net_id(char **str, int *start_out, int *end_out)
605 n = simple_strtoul(*str, &end, 0);
606 if((*end != '\0') || (end == *str))
615 static int net_remove(int n)
617 struct uml_net *device;
618 struct net_device *dev;
619 struct uml_net_private *lp;
621 device = find_device(n);
629 if(lp->remove != NULL) (*lp->remove)(&lp->user);
630 unregister_netdev(dev);
631 platform_device_unregister(&device->pdev);
633 list_del(&device->list);
639 static struct mc_device net_mc = {
641 .config = net_config,
644 .remove = net_remove,
647 static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
650 struct in_ifaddr *ifa = ptr;
651 struct net_device *dev = ifa->ifa_dev->dev;
652 struct uml_net_private *lp;
653 void (*proc)(unsigned char *, unsigned char *, void *);
654 unsigned char addr_buf[4], netmask_buf[4];
656 if(dev->open != uml_net_open) return(NOTIFY_DONE);
663 proc = lp->add_address;
666 proc = lp->delete_address;
670 memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
671 memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
672 (*proc)(addr_buf, netmask_buf, &lp->user);
677 struct notifier_block uml_inetaddr_notifier = {
678 .notifier_call = uml_inetaddr_event,
681 static int uml_net_init(void)
683 struct list_head *ele;
684 struct uml_net_private *lp;
685 struct in_device *ip;
686 struct in_ifaddr *in;
688 mconsole_register_dev(&net_mc);
689 register_inetaddr_notifier(¨_inetaddr_notifier);
691 /* Devices may have been opened already, so the uml_inetaddr_notifier
692 * didn't get a chance to run for them. This fakes it so that
693 * addresses which have already been set up get handled properly.
695 list_for_each(ele, &opened){
696 lp = list_entry(ele, struct uml_net_private, list);
697 ip = lp->dev->ip_ptr;
698 if(ip == NULL) continue;
701 uml_inetaddr_event(NULL, NETDEV_UP, in);
709 __initcall(uml_net_init);
711 static void close_devices(void)
713 struct list_head *ele;
714 struct uml_net_private *lp;
716 list_for_each(ele, &opened){
717 lp = list_entry(ele, struct uml_net_private, list);
718 if((lp->close != NULL) && (lp->fd >= 0))
719 (*lp->close)(lp->fd, &lp->user);
720 if(lp->remove != NULL) (*lp->remove)(&lp->user);
724 __uml_exitcall(close_devices);
726 int setup_etheraddr(char *str, unsigned char *addr)
734 addr[i] = simple_strtoul(str, &end, 16);
736 ((*end != ':') && (*end != ',') && (*end != '\0'))){
738 "setup_etheraddr: failed to parse '%s' "
739 "as an ethernet address\n", str);
746 "Attempt to assign a broadcast ethernet address to a "
747 "device disallowed\n");
753 void dev_ip_addr(void *d, unsigned char *bin_buf)
755 struct net_device *dev = d;
756 struct in_device *ip = dev->ip_ptr;
757 struct in_ifaddr *in;
759 if((ip == NULL) || ((in = ip->ifa_list) == NULL)){
760 printk(KERN_WARNING "dev_ip_addr - device not assigned an "
764 memcpy(bin_buf, &in->ifa_address, sizeof(in->ifa_address));
767 void set_ether_mac(void *d, unsigned char *addr)
769 struct net_device *dev = d;
771 memcpy(dev->dev_addr, addr, ETH_ALEN);
774 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
776 if((skb != NULL) && (skb_tailroom(skb) < extra)){
777 struct sk_buff *skb2;
779 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
783 if(skb != NULL) skb_put(skb, extra);
787 void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
791 struct net_device *dev = d;
792 struct in_device *ip = dev->ip_ptr;
793 struct in_ifaddr *in;
794 unsigned char address[4], netmask[4];
796 if(ip == NULL) return;
799 memcpy(address, &in->ifa_address, sizeof(address));
800 memcpy(netmask, &in->ifa_mask, sizeof(netmask));
801 (*cb)(address, netmask, arg);
806 int dev_netmask(void *d, void *m)
808 struct net_device *dev = d;
809 struct in_device *ip = dev->ip_ptr;
810 struct in_ifaddr *in;
820 *mask_out = in->ifa_mask;
824 void *get_output_buffer(int *len_out)
828 ret = (void *) __get_free_pages(GFP_KERNEL, 0);
829 if(ret) *len_out = PAGE_SIZE;
834 void free_output_buffer(void *buffer)
836 free_pages((unsigned long) buffer, 0);
839 int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
844 remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
846 printk("tap_setup_common - Extra garbage on specification : "
854 unsigned short eth_protocol(struct sk_buff *skb)
856 return(eth_type_trans(skb, skb->dev));
860 * Overrides for Emacs so that we follow Linus's tabbing style.
861 * Emacs will notice this stuff at the end of the file and automatically
862 * adjust the settings for this buffer only. This must remain at the end
864 * ---------------------------------------------------------------------------
866 * c-file-style: "linux"