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"
31 static spinlock_t opened_lock = SPIN_LOCK_UNLOCKED;
34 static int uml_net_rx(struct net_device *dev)
36 struct uml_net_private *lp = dev->priv;
40 /* If we can't allocate memory, try again next round. */
41 skb = dev_alloc_skb(dev->mtu);
43 lp->stats.rx_dropped++;
48 skb_put(skb, dev->mtu);
49 skb->mac.raw = skb->data;
50 pkt_len = (*lp->read)(lp->fd, &skb, lp);
53 skb_trim(skb, pkt_len);
54 skb->protocol = (*lp->protocol)(skb);
57 lp->stats.rx_bytes += skb->len;
58 lp->stats.rx_packets++;
66 irqreturn_t uml_net_interrupt(int irq, void *dev_id, struct pt_regs *regs)
68 struct net_device *dev = dev_id;
69 struct uml_net_private *lp = dev->priv;
72 if(!netif_running(dev))
76 while((err = uml_net_rx(dev)) > 0) ;
79 "Device '%s' read returned %d, shutting it down\n",
84 reactivate_fd(lp->fd, UM_ETH_IRQ);
87 spin_unlock(&lp->lock);
91 static int uml_net_open(struct net_device *dev)
93 struct uml_net_private *lp = dev->priv;
94 char addr[sizeof("255.255.255.255\0")];
105 dev_ip_addr(dev, addr, &lp->mac[2]);
106 set_ether_mac(dev, lp->mac);
109 lp->fd = (*lp->open)(&lp->user);
115 err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
116 SA_INTERRUPT | SA_SHIRQ, dev->name, dev);
118 printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
119 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
124 lp->tl.data = (unsigned long) &lp->user;
125 netif_start_queue(dev);
127 spin_lock(&opened_lock);
128 list_add(&lp->list, &opened);
129 spin_unlock(&opened_lock);
132 spin_unlock(&lp->lock);
136 static int uml_net_close(struct net_device *dev)
138 struct uml_net_private *lp = dev->priv;
140 netif_stop_queue(dev);
141 spin_lock(&lp->lock);
143 free_irq(dev->irq, dev);
144 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
146 spin_lock(&opened_lock);
148 spin_unlock(&opened_lock);
151 spin_unlock(&lp->lock);
155 static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
157 struct uml_net_private *lp = dev->priv;
161 netif_stop_queue(dev);
163 spin_lock_irqsave(&lp->lock, flags);
165 len = (*lp->write)(lp->fd, &skb, lp);
167 if(len == skb->len) {
168 lp->stats.tx_packets++;
169 lp->stats.tx_bytes += skb->len;
170 dev->trans_start = jiffies;
171 netif_start_queue(dev);
173 /* this is normally done in the interrupt when tx finishes */
174 netif_wake_queue(dev);
177 netif_start_queue(dev);
178 lp->stats.tx_dropped++;
181 netif_start_queue(dev);
182 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
185 spin_unlock_irqrestore(&lp->lock, flags);
192 static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
194 struct uml_net_private *lp = dev->priv;
198 static void uml_net_set_multicast_list(struct net_device *dev)
200 if (dev->flags & IFF_PROMISC) return;
201 else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
202 else dev->flags &= ~IFF_ALLMULTI;
205 static void uml_net_tx_timeout(struct net_device *dev)
207 dev->trans_start = jiffies;
208 netif_wake_queue(dev);
211 static int uml_net_set_mac(struct net_device *dev, void *addr)
213 struct uml_net_private *lp = dev->priv;
214 struct sockaddr *hwaddr = addr;
216 spin_lock(&lp->lock);
217 memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
218 spin_unlock(&lp->lock);
223 static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
225 struct uml_net_private *lp = dev->priv;
228 spin_lock(&lp->lock);
230 new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
239 spin_unlock(&lp->lock);
243 static int uml_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
248 void uml_net_user_timer_expire(unsigned long _conn)
251 struct connection *conn = (struct connection *)_conn;
253 dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
258 static spinlock_t devices_lock = SPIN_LOCK_UNLOCKED;
259 static struct list_head devices = LIST_HEAD_INIT(devices);
261 static int eth_configure(int n, void *init, char *mac,
262 struct transport *transport)
264 struct uml_net *device;
265 struct net_device *dev;
266 struct uml_net_private *lp;
269 size = transport->private_size + sizeof(struct uml_net_private) +
270 sizeof(((struct uml_net_private *) 0)->user);
272 device = kmalloc(sizeof(*device), GFP_KERNEL);
273 if (device == NULL) {
274 printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
278 memset(device, 0, sizeof(*device));
279 INIT_LIST_HEAD(&device->list);
282 spin_lock(&devices_lock);
283 list_add(&device->list, &devices);
284 spin_unlock(&devices_lock);
286 if (setup_etheraddr(mac, device->mac))
287 device->have_mac = 1;
289 printk(KERN_INFO "Netdevice %d ", n);
290 if (device->have_mac)
291 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
292 device->mac[0], device->mac[1],
293 device->mac[2], device->mac[3],
294 device->mac[4], device->mac[5]);
296 dev = alloc_etherdev(size);
298 printk(KERN_ERR "eth_configure: failed to allocate device\n");
302 /* If this name ends up conflicting with an existing registered
303 * netdevice, that is OK, register_netdev{,ice}() will notice this
306 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
309 (*transport->kern->init)(dev, init);
311 dev->mtu = transport->user->max_packet;
312 dev->open = uml_net_open;
313 dev->hard_start_xmit = uml_net_start_xmit;
314 dev->stop = uml_net_close;
315 dev->get_stats = uml_net_get_stats;
316 dev->set_multicast_list = uml_net_set_multicast_list;
317 dev->tx_timeout = uml_net_tx_timeout;
318 dev->set_mac_address = uml_net_set_mac;
319 dev->change_mtu = uml_net_change_mtu;
320 dev->do_ioctl = uml_net_ioctl;
321 dev->watchdog_timeo = (HZ >> 1);
322 dev->irq = UM_ETH_IRQ;
325 err = register_netdevice(dev);
329 /* XXX: should we call ->remove() here? */
335 /* lp.user is the first four bytes of the transport data, which
336 * has already been initialized. This structure assignment will
337 * overwrite that, so we make sure that .user gets overwritten with
338 * what it already has.
341 *lp = ((struct uml_net_private)
342 { .list = LIST_HEAD_INIT(lp->list),
343 .lock = SPIN_LOCK_UNLOCKED,
346 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
347 .have_mac = device->have_mac,
348 .protocol = transport->kern->protocol,
349 .open = transport->user->open,
350 .close = transport->user->close,
351 .remove = transport->user->remove,
352 .read = transport->kern->read,
353 .write = transport->kern->write,
354 .add_address = transport->user->add_address,
355 .delete_address = transport->user->delete_address,
356 .set_mtu = transport->user->set_mtu,
360 lp->tl.function = uml_net_user_timer_expire;
362 memcpy(lp->mac, device->mac, sizeof(lp->mac));
364 if (transport->user->init)
365 (*transport->user->init)(&lp->user, dev);
367 if (device->have_mac)
368 set_ether_mac(dev, device->mac);
372 static struct uml_net *find_device(int n)
374 struct uml_net *device;
375 struct list_head *ele;
377 spin_lock(&devices_lock);
378 list_for_each(ele, &devices){
379 device = list_entry(ele, struct uml_net, list);
380 if(device->index == n)
385 spin_unlock(&devices_lock);
389 static int eth_parse(char *str, int *index_out, char **str_out)
394 n = simple_strtoul(str, &end, 0);
396 printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
400 printk(KERN_ERR "eth_setup: device %d is negative\n", n);
406 "eth_setup: expected '=' after device number\n");
411 printk(KERN_ERR "eth_setup: Device %d already configured\n",
415 if(index_out) *index_out = n;
421 struct list_head list;
426 /* Filled in at boot time. Will need locking if the transports become
429 struct list_head transports = LIST_HEAD_INIT(transports);
431 /* Filled in during early boot */
432 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
434 static int check_transport(struct transport *transport, char *eth, int n,
435 void **init_out, char **mac_out)
439 len = strlen(transport->name);
440 if(strncmp(eth, transport->name, len))
446 else if(*eth != '\0')
449 *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
450 if(*init_out == NULL)
453 if(!transport->setup(eth, mac_out, *init_out)){
460 void register_transport(struct transport *new)
462 struct list_head *ele, *next;
463 struct eth_init *eth;
468 list_add(&new->list, &transports);
470 list_for_each_safe(ele, next, ð_cmd_line){
471 eth = list_entry(ele, struct eth_init, list);
472 match = check_transport(new, eth->init, eth->index, &init,
476 else if(init != NULL){
477 eth_configure(eth->index, init, mac, new);
480 list_del(ð->list);
484 static int eth_setup_common(char *str, int index)
486 struct list_head *ele;
487 struct transport *transport;
491 list_for_each(ele, &transports){
492 transport = list_entry(ele, struct transport, list);
493 if(!check_transport(transport, str, index, &init, &mac))
496 eth_configure(index, init, mac, transport);
504 static int eth_setup(char *str)
506 struct eth_init *new;
509 err = eth_parse(str, &n, &str);
512 new = alloc_bootmem(sizeof(new));
514 printk("eth_init : alloc_bootmem failed\n");
518 INIT_LIST_HEAD(&new->list);
522 list_add_tail(&new->list, ð_cmd_line);
526 __setup("eth", eth_setup);
527 __uml_help(eth_setup,
528 "eth[0-9]+=<transport>,<options>\n"
529 " Configure a network device.\n\n"
532 static int eth_init(void)
534 struct list_head *ele, *next;
535 struct eth_init *eth;
537 list_for_each_safe(ele, next, ð_cmd_line){
538 eth = list_entry(ele, struct eth_init, list);
540 if(eth_setup_common(eth->init, eth->index))
541 list_del(ð->list);
547 __initcall(eth_init);
549 static int net_config(char *str)
553 err = eth_parse(str, &n, &str);
556 str = uml_strdup(str);
558 printk(KERN_ERR "net_config failed to strdup string\n");
561 err = !eth_setup_common(str, n);
567 static int net_remove(char *str)
569 struct uml_net *device;
570 struct net_device *dev;
571 struct uml_net_private *lp;
575 n = simple_strtoul(str, &end, 0);
576 if((*end != '\0') || (end == str))
579 device = find_device(n);
585 if(lp->fd > 0) return(-1);
586 if(lp->remove != NULL) (*lp->remove)(&lp->user);
587 unregister_netdev(dev);
589 list_del(&device->list);
595 static struct mc_device net_mc = {
597 .config = net_config,
599 .remove = net_remove,
602 static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
605 struct in_ifaddr *ifa = ptr;
606 u32 addr = ifa->ifa_address;
607 u32 netmask = ifa->ifa_mask;
608 struct net_device *dev = ifa->ifa_dev->dev;
609 struct uml_net_private *lp;
610 void (*proc)(unsigned char *, unsigned char *, void *);
611 unsigned char addr_buf[4], netmask_buf[4];
613 if(dev->open != uml_net_open) return(NOTIFY_DONE);
620 proc = lp->add_address;
623 proc = lp->delete_address;
627 addr_buf[0] = addr & 0xff;
628 addr_buf[1] = (addr >> 8) & 0xff;
629 addr_buf[2] = (addr >> 16) & 0xff;
630 addr_buf[3] = addr >> 24;
631 netmask_buf[0] = netmask & 0xff;
632 netmask_buf[1] = (netmask >> 8) & 0xff;
633 netmask_buf[2] = (netmask >> 16) & 0xff;
634 netmask_buf[3] = netmask >> 24;
635 (*proc)(addr_buf, netmask_buf, &lp->user);
640 struct notifier_block uml_inetaddr_notifier = {
641 .notifier_call = uml_inetaddr_event,
644 static int uml_net_init(void)
646 struct list_head *ele;
647 struct uml_net_private *lp;
648 struct in_device *ip;
649 struct in_ifaddr *in;
651 mconsole_register_dev(&net_mc);
652 register_inetaddr_notifier(¨_inetaddr_notifier);
654 /* Devices may have been opened already, so the uml_inetaddr_notifier
655 * didn't get a chance to run for them. This fakes it so that
656 * addresses which have already been set up get handled properly.
658 list_for_each(ele, &opened){
659 lp = list_entry(ele, struct uml_net_private, list);
660 ip = lp->dev->ip_ptr;
661 if(ip == NULL) continue;
664 uml_inetaddr_event(NULL, NETDEV_UP, in);
672 __initcall(uml_net_init);
674 static void close_devices(void)
676 struct list_head *ele;
677 struct uml_net_private *lp;
679 list_for_each(ele, &opened){
680 lp = list_entry(ele, struct uml_net_private, list);
681 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
682 if(lp->remove != NULL) (*lp->remove)(&lp->user);
686 __uml_exitcall(close_devices);
688 int setup_etheraddr(char *str, unsigned char *addr)
696 addr[i] = simple_strtoul(str, &end, 16);
698 ((*end != ':') && (*end != ',') && (*end != '\0'))){
700 "setup_etheraddr: failed to parse '%s' "
701 "as an ethernet address\n", str);
708 "Attempt to assign a broadcast ethernet address to a "
709 "device disallowed\n");
715 void dev_ip_addr(void *d, char *buf, char *bin_buf)
717 struct net_device *dev = d;
718 struct in_device *ip = dev->ip_ptr;
719 struct in_ifaddr *in;
722 if((ip == NULL) || ((in = ip->ifa_list) == NULL)){
723 printk(KERN_WARNING "dev_ip_addr - device not assigned an "
727 addr = in->ifa_address;
728 sprintf(buf, "%d.%d.%d.%d", addr & 0xff, (addr >> 8) & 0xff,
729 (addr >> 16) & 0xff, addr >> 24);
731 bin_buf[0] = addr & 0xff;
732 bin_buf[1] = (addr >> 8) & 0xff;
733 bin_buf[2] = (addr >> 16) & 0xff;
734 bin_buf[3] = addr >> 24;
738 void set_ether_mac(void *d, unsigned char *addr)
740 struct net_device *dev = d;
742 memcpy(dev->dev_addr, addr, ETH_ALEN);
745 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
747 if((skb != NULL) && (skb_tailroom(skb) < extra)){
748 struct sk_buff *skb2;
750 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
754 if(skb != NULL) skb_put(skb, extra);
758 void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
762 struct net_device *dev = d;
763 struct in_device *ip = dev->ip_ptr;
764 struct in_ifaddr *in;
765 unsigned char address[4], netmask[4];
767 if(ip == NULL) return;
770 address[0] = in->ifa_address & 0xff;
771 address[1] = (in->ifa_address >> 8) & 0xff;
772 address[2] = (in->ifa_address >> 16) & 0xff;
773 address[3] = in->ifa_address >> 24;
774 netmask[0] = in->ifa_mask & 0xff;
775 netmask[1] = (in->ifa_mask >> 8) & 0xff;
776 netmask[2] = (in->ifa_mask >> 16) & 0xff;
777 netmask[3] = in->ifa_mask >> 24;
778 (*cb)(address, netmask, arg);
783 int dev_netmask(void *d, void *m)
785 struct net_device *dev = d;
786 struct in_device *ip = dev->ip_ptr;
787 struct in_ifaddr *in;
797 *mask_out = in->ifa_mask;
801 void *get_output_buffer(int *len_out)
805 ret = (void *) __get_free_pages(GFP_KERNEL, 0);
806 if(ret) *len_out = PAGE_SIZE;
811 void free_output_buffer(void *buffer)
813 free_pages((unsigned long) buffer, 0);
816 int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
821 remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
823 printk("tap_setup_common - Extra garbage on specification : "
831 unsigned short eth_protocol(struct sk_buff *skb)
833 return(eth_type_trans(skb, skb->dev));
837 * Overrides for Emacs so that we follow Linus's tabbing style.
838 * Emacs will notice this stuff at the end of the file and automatically
839 * adjust the settings for this buffer only. This must remain at the end
841 * ---------------------------------------------------------------------------
843 * c-file-style: "linux"