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 "user_util.h"
23 #include "kern_util.h"
26 #include "mconsole_kern.h"
30 static spinlock_t opened_lock = SPIN_LOCK_UNLOCKED;
33 static int uml_net_rx(struct net_device *dev)
35 struct uml_net_private *lp = dev->priv;
39 /* If we can't allocate memory, try again next round. */
40 if ((skb = dev_alloc_skb(dev->mtu)) == NULL) {
41 lp->stats.rx_dropped++;
46 skb_put(skb, dev->mtu);
47 skb->mac.raw = skb->data;
48 pkt_len = (*lp->read)(lp->fd, &skb, lp);
51 skb_trim(skb, pkt_len);
52 skb->protocol = (*lp->protocol)(skb);
55 lp->stats.rx_bytes += skb->len;
56 lp->stats.rx_packets++;
64 void uml_net_interrupt(int irq, void *dev_id, struct pt_regs *regs)
66 struct net_device *dev = dev_id;
67 struct uml_net_private *lp = dev->priv;
70 if(!netif_running(dev))
74 while((err = uml_net_rx(dev)) > 0) ;
77 "Device '%s' read returned %d, shutting it down\n",
82 reactivate_fd(lp->fd, UM_ETH_IRQ);
85 spin_unlock(&lp->lock);
88 static int uml_net_open(struct net_device *dev)
90 struct uml_net_private *lp = dev->priv;
91 char addr[sizeof("255.255.255.255\0")];
102 dev_ip_addr(dev, addr, &lp->mac[2]);
103 set_ether_mac(dev, lp->mac);
106 lp->fd = (*lp->open)(&lp->user);
112 err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
113 SA_INTERRUPT | SA_SHIRQ, dev->name, dev);
115 printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
116 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
121 lp->tl.data = (unsigned long) &lp->user;
122 netif_start_queue(dev);
124 spin_lock(&opened_lock);
125 list_add(&lp->list, &opened);
126 spin_unlock(&opened_lock);
129 spin_unlock(&lp->lock);
133 static int uml_net_close(struct net_device *dev)
135 struct uml_net_private *lp = dev->priv;
137 netif_stop_queue(dev);
138 spin_lock(&lp->lock);
140 free_irq(dev->irq, dev);
141 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
143 spin_lock(&opened_lock);
145 spin_unlock(&opened_lock);
148 spin_unlock(&lp->lock);
152 static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
154 struct uml_net_private *lp = dev->priv;
158 netif_stop_queue(dev);
160 spin_lock_irqsave(&lp->lock, flags);
162 len = (*lp->write)(lp->fd, &skb, lp);
164 if(len == skb->len) {
165 lp->stats.tx_packets++;
166 lp->stats.tx_bytes += skb->len;
167 dev->trans_start = jiffies;
168 netif_start_queue(dev);
170 /* this is normally done in the interrupt when tx finishes */
171 netif_wake_queue(dev);
174 netif_start_queue(dev);
175 lp->stats.tx_dropped++;
178 netif_start_queue(dev);
179 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
182 spin_unlock_irqrestore(&lp->lock, flags);
189 static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
191 struct uml_net_private *lp = dev->priv;
195 static void uml_net_set_multicast_list(struct net_device *dev)
197 if (dev->flags & IFF_PROMISC) return;
198 else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
199 else dev->flags &= ~IFF_ALLMULTI;
202 static void uml_net_tx_timeout(struct net_device *dev)
204 dev->trans_start = jiffies;
205 netif_wake_queue(dev);
208 static int uml_net_set_mac(struct net_device *dev, void *addr)
210 struct uml_net_private *lp = dev->priv;
211 struct sockaddr *hwaddr = addr;
213 spin_lock(&lp->lock);
214 memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
215 spin_unlock(&lp->lock);
220 static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
222 struct uml_net_private *lp = dev->priv;
225 spin_lock(&lp->lock);
227 new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
236 spin_unlock(&lp->lock);
240 static int uml_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
245 void uml_net_user_timer_expire(unsigned long _conn)
248 struct connection *conn = (struct connection *)_conn;
250 dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
256 * default do nothing hard header packet routines for struct net_device init.
257 * real ethernet transports will overwrite with real routines.
259 static int uml_net_hard_header(struct sk_buff *skb, struct net_device *dev,
260 unsigned short type, void *daddr, void *saddr, unsigned len)
262 return(0); /* no change */
265 static int uml_net_rebuild_header(struct sk_buff *skb)
267 return(0); /* ignore */
270 static int uml_net_header_cache(struct neighbour *neigh, struct hh_cache *hh)
272 return(-1); /* fail */
275 static void uml_net_header_cache_update(struct hh_cache *hh,
276 struct net_device *dev, unsigned char * haddr)
281 static int uml_net_header_parse(struct sk_buff *skb, unsigned char *haddr)
283 return(0); /* nothing */
286 static spinlock_t devices_lock = SPIN_LOCK_UNLOCKED;
287 static struct list_head devices = LIST_HEAD_INIT(devices);
289 static int eth_configure(int n, void *init, char *mac,
290 struct transport *transport)
292 struct uml_net *device;
293 struct net_device *dev;
294 struct uml_net_private *lp;
297 size = transport->private_size + sizeof(struct uml_net_private) +
298 sizeof(((struct uml_net_private *) 0)->user);
300 device = kmalloc(sizeof(*device), GFP_KERNEL);
301 if (device == NULL) {
302 printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
306 memset(device, 0, sizeof(*device));
307 INIT_LIST_HEAD(&device->list);
310 spin_lock(&devices_lock);
311 list_add(&device->list, &devices);
312 spin_unlock(&devices_lock);
314 if (setup_etheraddr(mac, device->mac))
315 device->have_mac = 1;
317 printk(KERN_INFO "Netdevice %d ", n);
318 if (device->have_mac)
319 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
320 device->mac[0], device->mac[1],
321 device->mac[2], device->mac[3],
322 device->mac[4], device->mac[5]);
324 dev = alloc_etherdev(size);
326 printk(KERN_ERR "eth_configure: failed to allocate device\n");
330 /* If this name ends up conflicting with an existing registered
331 * netdevice, that is OK, register_netdev{,ice}() will notice this
334 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
337 dev->hard_header = uml_net_hard_header;
338 dev->rebuild_header = uml_net_rebuild_header;
339 dev->hard_header_cache = uml_net_header_cache;
340 dev->header_cache_update= uml_net_header_cache_update;
341 dev->hard_header_parse = uml_net_header_parse;
343 (*transport->kern->init)(dev, init);
345 dev->mtu = transport->user->max_packet;
346 dev->open = uml_net_open;
347 dev->hard_start_xmit = uml_net_start_xmit;
348 dev->stop = uml_net_close;
349 dev->get_stats = uml_net_get_stats;
350 dev->set_multicast_list = uml_net_set_multicast_list;
351 dev->tx_timeout = uml_net_tx_timeout;
352 dev->set_mac_address = uml_net_set_mac;
353 dev->change_mtu = uml_net_change_mtu;
354 dev->do_ioctl = uml_net_ioctl;
355 dev->watchdog_timeo = (HZ >> 1);
356 dev->irq = UM_ETH_IRQ;
359 err = register_netdevice(dev);
363 /* XXX: should we call ->remove() here? */
369 INIT_LIST_HEAD(&lp->list);
370 spin_lock_init(&lp->lock);
373 lp->mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0 };
374 lp->have_mac = device->have_mac;
375 lp->protocol = transport->kern->protocol;
376 lp->open = transport->user->open;
377 lp->close = transport->user->close;
378 lp->remove = transport->user->remove;
379 lp->read = transport->kern->read;
380 lp->write = transport->kern->write;
381 lp->add_address = transport->user->add_address;
382 lp->delete_address = transport->user->delete_address;
383 lp->set_mtu = transport->user->set_mtu;
386 lp->tl.function = uml_net_user_timer_expire;
388 memcpy(lp->mac, device->mac, sizeof(lp->mac));
390 if (transport->user->init)
391 (*transport->user->init)(&lp->user, dev);
393 if (device->have_mac)
394 set_ether_mac(dev, device->mac);
398 static struct uml_net *find_device(int n)
400 struct uml_net *device;
401 struct list_head *ele;
403 spin_lock(&devices_lock);
404 list_for_each(ele, &devices){
405 device = list_entry(ele, struct uml_net, list);
406 if(device->index == n)
411 spin_unlock(&devices_lock);
415 static int eth_parse(char *str, int *index_out, char **str_out)
420 n = simple_strtoul(str, &end, 0);
422 printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
426 printk(KERN_ERR "eth_setup: device %d is negative\n", n);
432 "eth_setup: expected '=' after device number\n");
437 printk(KERN_ERR "eth_setup: Device %d already configured\n",
441 if(index_out) *index_out = n;
447 struct list_head list;
452 /* Filled in at boot time. Will need locking if the transports become
455 struct list_head transports = LIST_HEAD_INIT(transports);
457 /* Filled in during early boot */
458 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
460 static int check_transport(struct transport *transport, char *eth, int n,
461 void **init_out, char **mac_out)
465 len = strlen(transport->name);
466 if(strncmp(eth, transport->name, len))
472 else if(*eth != '\0')
475 *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
476 if(*init_out == NULL)
479 if(!transport->setup(eth, mac_out, *init_out)){
486 void register_transport(struct transport *new)
488 struct list_head *ele, *next;
489 struct eth_init *eth;
494 list_add(&new->list, &transports);
496 list_for_each_safe(ele, next, ð_cmd_line){
497 eth = list_entry(ele, struct eth_init, list);
498 match = check_transport(new, eth->init, eth->index, &init,
502 else if(init != NULL){
503 eth_configure(eth->index, init, mac, new);
506 list_del(ð->list);
510 static int eth_setup_common(char *str, int index)
512 struct list_head *ele;
513 struct transport *transport;
517 list_for_each(ele, &transports){
518 transport = list_entry(ele, struct transport, list);
519 if(!check_transport(transport, str, index, &init, &mac))
522 eth_configure(index, init, mac, transport);
530 static int eth_setup(char *str)
532 struct eth_init *new;
535 err = eth_parse(str, &n, &str);
538 new = alloc_bootmem(sizeof(new));
540 printk("eth_init : alloc_bootmem failed\n");
544 INIT_LIST_HEAD(&new->list);
548 list_add_tail(&new->list, ð_cmd_line);
552 __setup("eth", eth_setup);
553 __uml_help(eth_setup,
554 "eth[0-9]+=<transport>,<options>\n"
555 " Configure a network device.\n\n"
558 static int eth_init(void)
560 struct list_head *ele, *next;
561 struct eth_init *eth;
563 list_for_each_safe(ele, next, ð_cmd_line){
564 eth = list_entry(ele, struct eth_init, list);
566 if(eth_setup_common(eth->init, eth->index))
567 list_del(ð->list);
573 __initcall(eth_init);
575 static int net_config(char *str)
579 err = eth_parse(str, &n, &str);
582 str = uml_strdup(str);
584 printk(KERN_ERR "net_config failed to strdup string\n");
587 err = !eth_setup_common(str, n);
593 static int net_remove(char *str)
595 struct uml_net *device;
596 struct net_device *dev;
597 struct uml_net_private *lp;
601 n = simple_strtoul(str, &end, 0);
602 if((*end != '\0') || (end == str))
605 device = find_device(n);
611 if(lp->fd > 0) return(-1);
612 if(lp->remove != NULL) (*lp->remove)(&lp->user);
613 unregister_netdev(dev);
615 list_del(&device->list);
620 static struct mc_device net_mc = {
622 .config = net_config,
624 .remove = net_remove,
627 static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
630 struct in_ifaddr *ifa = ptr;
631 u32 addr = ifa->ifa_address;
632 u32 netmask = ifa->ifa_mask;
633 struct net_device *dev = ifa->ifa_dev->dev;
634 struct uml_net_private *lp;
635 void (*proc)(unsigned char *, unsigned char *, void *);
636 unsigned char addr_buf[4], netmask_buf[4];
638 if(dev->open != uml_net_open) return(NOTIFY_DONE);
645 proc = lp->add_address;
648 proc = lp->delete_address;
652 addr_buf[0] = addr & 0xff;
653 addr_buf[1] = (addr >> 8) & 0xff;
654 addr_buf[2] = (addr >> 16) & 0xff;
655 addr_buf[3] = addr >> 24;
656 netmask_buf[0] = netmask & 0xff;
657 netmask_buf[1] = (netmask >> 8) & 0xff;
658 netmask_buf[2] = (netmask >> 16) & 0xff;
659 netmask_buf[3] = netmask >> 24;
660 (*proc)(addr_buf, netmask_buf, &lp->user);
665 struct notifier_block uml_inetaddr_notifier = {
666 .notifier_call = uml_inetaddr_event,
669 static int uml_net_init(void)
671 struct list_head *ele;
672 struct uml_net_private *lp;
673 struct in_device *ip;
674 struct in_ifaddr *in;
676 mconsole_register_dev(&net_mc);
677 register_inetaddr_notifier(¨_inetaddr_notifier);
679 /* Devices may have been opened already, so the uml_inetaddr_notifier
680 * didn't get a chance to run for them. This fakes it so that
681 * addresses which have already been set up get handled properly.
683 list_for_each(ele, &opened){
684 lp = list_entry(ele, struct uml_net_private, list);
685 ip = lp->dev->ip_ptr;
686 if(ip == NULL) continue;
689 uml_inetaddr_event(NULL, NETDEV_UP, in);
697 __initcall(uml_net_init);
699 static void close_devices(void)
701 struct list_head *ele;
702 struct uml_net_private *lp;
704 list_for_each(ele, &opened){
705 lp = list_entry(ele, struct uml_net_private, list);
706 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
707 if(lp->remove != NULL) (*lp->remove)(&lp->user);
711 __uml_exitcall(close_devices);
713 int setup_etheraddr(char *str, unsigned char *addr)
721 addr[i] = simple_strtoul(str, &end, 16);
723 ((*end != ':') && (*end != ',') && (*end != '\0'))){
725 "setup_etheraddr: failed to parse '%s' "
726 "as an ethernet address\n", str);
733 "Attempt to assign a broadcast ethernet address to a "
734 "device disallowed\n");
740 void dev_ip_addr(void *d, char *buf, char *bin_buf)
742 struct net_device *dev = d;
743 struct in_device *ip = dev->ip_ptr;
744 struct in_ifaddr *in;
747 if((ip == NULL) || ((in = ip->ifa_list) == NULL)){
748 printk(KERN_WARNING "dev_ip_addr - device not assigned an "
752 addr = in->ifa_address;
753 sprintf(buf, "%d.%d.%d.%d", addr & 0xff, (addr >> 8) & 0xff,
754 (addr >> 16) & 0xff, addr >> 24);
756 bin_buf[0] = addr & 0xff;
757 bin_buf[1] = (addr >> 8) & 0xff;
758 bin_buf[2] = (addr >> 16) & 0xff;
759 bin_buf[3] = addr >> 24;
763 void set_ether_mac(void *d, unsigned char *addr)
765 struct net_device *dev = d;
767 memcpy(dev->dev_addr, addr, ETH_ALEN);
770 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
772 if((skb != NULL) && (skb_tailroom(skb) < extra)){
773 struct sk_buff *skb2;
775 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
779 if(skb != NULL) skb_put(skb, extra);
783 void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
787 struct net_device *dev = d;
788 struct in_device *ip = dev->ip_ptr;
789 struct in_ifaddr *in;
790 unsigned char address[4], netmask[4];
792 if(ip == NULL) return;
795 address[0] = in->ifa_address & 0xff;
796 address[1] = (in->ifa_address >> 8) & 0xff;
797 address[2] = (in->ifa_address >> 16) & 0xff;
798 address[3] = in->ifa_address >> 24;
799 netmask[0] = in->ifa_mask & 0xff;
800 netmask[1] = (in->ifa_mask >> 8) & 0xff;
801 netmask[2] = (in->ifa_mask >> 16) & 0xff;
802 netmask[3] = in->ifa_mask >> 24;
803 (*cb)(address, netmask, arg);
808 int dev_netmask(void *d, void *m)
810 struct net_device *dev = d;
811 struct in_device *ip = dev->ip_ptr;
812 struct in_ifaddr *in;
822 *mask_out = in->ifa_mask;
826 void *get_output_buffer(int *len_out)
830 ret = (void *) __get_free_pages(GFP_KERNEL, 0);
831 if(ret) *len_out = PAGE_SIZE;
836 void free_output_buffer(void *buffer)
838 free_pages((unsigned long) buffer, 0);
841 int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
846 remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
848 printk("tap_setup_common - Extra garbage on specification : "
856 unsigned short eth_protocol(struct sk_buff *skb)
858 return(eth_type_trans(skb, skb->dev));
862 * Overrides for Emacs so that we follow Linus's tabbing style.
863 * Emacs will notice this stuff at the end of the file and automatically
864 * adjust the settings for this buffer only. This must remain at the end
866 * ---------------------------------------------------------------------------
868 * c-file-style: "linux"