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);
131 spin_unlock(&lp->lock);
135 static int uml_net_close(struct net_device *dev)
137 struct uml_net_private *lp = dev->priv;
139 netif_stop_queue(dev);
140 spin_lock(&lp->lock);
142 free_irq(dev->irq, dev);
143 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
145 spin_lock(&opened_lock);
147 spin_unlock(&opened_lock);
149 spin_unlock(&lp->lock);
153 static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
155 struct uml_net_private *lp = dev->priv;
159 netif_stop_queue(dev);
161 spin_lock_irqsave(&lp->lock, flags);
163 len = (*lp->write)(lp->fd, &skb, lp);
165 if(len == skb->len) {
166 lp->stats.tx_packets++;
167 lp->stats.tx_bytes += skb->len;
168 dev->trans_start = jiffies;
169 netif_start_queue(dev);
171 /* this is normally done in the interrupt when tx finishes */
172 netif_wake_queue(dev);
175 netif_start_queue(dev);
176 lp->stats.tx_dropped++;
179 netif_start_queue(dev);
180 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
183 spin_unlock_irqrestore(&lp->lock, flags);
190 static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
192 struct uml_net_private *lp = dev->priv;
196 static void uml_net_set_multicast_list(struct net_device *dev)
198 if (dev->flags & IFF_PROMISC) return;
199 else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
200 else dev->flags &= ~IFF_ALLMULTI;
203 static void uml_net_tx_timeout(struct net_device *dev)
205 dev->trans_start = jiffies;
206 netif_wake_queue(dev);
209 static int uml_net_set_mac(struct net_device *dev, void *addr)
211 struct uml_net_private *lp = dev->priv;
212 struct sockaddr *hwaddr = addr;
214 spin_lock(&lp->lock);
215 memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
216 spin_unlock(&lp->lock);
221 static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
223 struct uml_net_private *lp = dev->priv;
226 spin_lock(&lp->lock);
228 new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
237 spin_unlock(&lp->lock);
241 static int uml_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
246 void uml_net_user_timer_expire(unsigned long _conn)
249 struct connection *conn = (struct connection *)_conn;
251 dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
256 static spinlock_t devices_lock = SPIN_LOCK_UNLOCKED;
257 static struct list_head devices = LIST_HEAD_INIT(devices);
259 static int eth_configure(int n, void *init, char *mac,
260 struct transport *transport)
262 struct uml_net *device;
263 struct net_device *dev;
264 struct uml_net_private *lp;
267 size = transport->private_size + sizeof(struct uml_net_private) +
268 sizeof(((struct uml_net_private *) 0)->user);
270 device = kmalloc(sizeof(*device), GFP_KERNEL);
271 if (device == NULL) {
272 printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
276 memset(device, 0, sizeof(*device));
277 INIT_LIST_HEAD(&device->list);
280 spin_lock(&devices_lock);
281 list_add(&device->list, &devices);
282 spin_unlock(&devices_lock);
284 if (setup_etheraddr(mac, device->mac))
285 device->have_mac = 1;
287 printk(KERN_INFO "Netdevice %d ", n);
288 if (device->have_mac)
289 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
290 device->mac[0], device->mac[1],
291 device->mac[2], device->mac[3],
292 device->mac[4], device->mac[5]);
294 dev = alloc_etherdev(size);
296 printk(KERN_ERR "eth_configure: failed to allocate device\n");
300 /* If this name ends up conflicting with an existing registered
301 * netdevice, that is OK, register_netdev{,ice}() will notice this
304 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
307 (*transport->kern->init)(dev, init);
309 dev->mtu = transport->user->max_packet;
310 dev->open = uml_net_open;
311 dev->hard_start_xmit = uml_net_start_xmit;
312 dev->stop = uml_net_close;
313 dev->get_stats = uml_net_get_stats;
314 dev->set_multicast_list = uml_net_set_multicast_list;
315 dev->tx_timeout = uml_net_tx_timeout;
316 dev->set_mac_address = uml_net_set_mac;
317 dev->change_mtu = uml_net_change_mtu;
318 dev->do_ioctl = uml_net_ioctl;
319 dev->watchdog_timeo = (HZ >> 1);
320 dev->irq = UM_ETH_IRQ;
323 err = register_netdevice(dev);
327 /* XXX: should we call ->remove() here? */
333 /* lp.user is the first four bytes of the transport data, which
334 * has already been initialized. This structure assignment will
335 * overwrite that, so we make sure that .user gets overwritten with
336 * what it already has.
339 *lp = ((struct uml_net_private)
340 { .list = LIST_HEAD_INIT(lp->list),
341 .lock = SPIN_LOCK_UNLOCKED,
344 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
345 .have_mac = device->have_mac,
346 .protocol = transport->kern->protocol,
347 .open = transport->user->open,
348 .close = transport->user->close,
349 .remove = transport->user->remove,
350 .read = transport->kern->read,
351 .write = transport->kern->write,
352 .add_address = transport->user->add_address,
353 .delete_address = transport->user->delete_address,
354 .set_mtu = transport->user->set_mtu,
358 lp->tl.function = uml_net_user_timer_expire;
360 memcpy(lp->mac, device->mac, sizeof(lp->mac));
362 if (transport->user->init)
363 (*transport->user->init)(&lp->user, dev);
365 if (device->have_mac)
366 set_ether_mac(dev, device->mac);
370 static struct uml_net *find_device(int n)
372 struct uml_net *device;
373 struct list_head *ele;
375 spin_lock(&devices_lock);
376 list_for_each(ele, &devices){
377 device = list_entry(ele, struct uml_net, list);
378 if(device->index == n)
383 spin_unlock(&devices_lock);
387 static int eth_parse(char *str, int *index_out, char **str_out)
392 n = simple_strtoul(str, &end, 0);
394 printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
398 printk(KERN_ERR "eth_setup: device %d is negative\n", n);
404 "eth_setup: expected '=' after device number\n");
409 printk(KERN_ERR "eth_setup: Device %d already configured\n",
413 if(index_out) *index_out = n;
419 struct list_head list;
424 /* Filled in at boot time. Will need locking if the transports become
427 struct list_head transports = LIST_HEAD_INIT(transports);
429 /* Filled in during early boot */
430 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
432 static int check_transport(struct transport *transport, char *eth, int n,
433 void **init_out, char **mac_out)
437 len = strlen(transport->name);
438 if(strncmp(eth, transport->name, len))
444 else if(*eth != '\0')
447 *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
448 if(*init_out == NULL)
451 if(!transport->setup(eth, mac_out, *init_out)){
458 void register_transport(struct transport *new)
460 struct list_head *ele, *next;
461 struct eth_init *eth;
466 list_add(&new->list, &transports);
468 list_for_each_safe(ele, next, ð_cmd_line){
469 eth = list_entry(ele, struct eth_init, list);
470 match = check_transport(new, eth->init, eth->index, &init,
474 else if(init != NULL){
475 eth_configure(eth->index, init, mac, new);
478 list_del(ð->list);
482 static int eth_setup_common(char *str, int index)
484 struct list_head *ele;
485 struct transport *transport;
489 list_for_each(ele, &transports){
490 transport = list_entry(ele, struct transport, list);
491 if(!check_transport(transport, str, index, &init, &mac))
494 eth_configure(index, init, mac, transport);
502 static int eth_setup(char *str)
504 struct eth_init *new;
507 err = eth_parse(str, &n, &str);
510 new = alloc_bootmem(sizeof(new));
512 printk("eth_init : alloc_bootmem failed\n");
516 INIT_LIST_HEAD(&new->list);
520 list_add_tail(&new->list, ð_cmd_line);
524 __setup("eth", eth_setup);
525 __uml_help(eth_setup,
526 "eth[0-9]+=<transport>,<options>\n"
527 " Configure a network device.\n\n"
530 static int eth_init(void)
532 struct list_head *ele, *next;
533 struct eth_init *eth;
535 list_for_each_safe(ele, next, ð_cmd_line){
536 eth = list_entry(ele, struct eth_init, list);
538 if(eth_setup_common(eth->init, eth->index))
539 list_del(ð->list);
545 __initcall(eth_init);
547 static int net_config(char *str)
551 err = eth_parse(str, &n, &str);
554 str = uml_strdup(str);
556 printk(KERN_ERR "net_config failed to strdup string\n");
559 err = !eth_setup_common(str, n);
565 static int net_remove(char *str)
567 struct uml_net *device;
568 struct net_device *dev;
569 struct uml_net_private *lp;
573 n = simple_strtoul(str, &end, 0);
574 if((*end != '\0') || (end == str))
577 device = find_device(n);
583 if(lp->fd > 0) return(-1);
584 if(lp->remove != NULL) (*lp->remove)(&lp->user);
585 unregister_netdev(dev);
587 list_del(&device->list);
593 static struct mc_device net_mc = {
595 .config = net_config,
597 .remove = net_remove,
600 static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
603 struct in_ifaddr *ifa = ptr;
604 u32 addr = ifa->ifa_address;
605 u32 netmask = ifa->ifa_mask;
606 struct net_device *dev = ifa->ifa_dev->dev;
607 struct uml_net_private *lp;
608 void (*proc)(unsigned char *, unsigned char *, void *);
609 unsigned char addr_buf[4], netmask_buf[4];
611 if(dev->open != uml_net_open) return(NOTIFY_DONE);
618 proc = lp->add_address;
621 proc = lp->delete_address;
625 addr_buf[0] = addr & 0xff;
626 addr_buf[1] = (addr >> 8) & 0xff;
627 addr_buf[2] = (addr >> 16) & 0xff;
628 addr_buf[3] = addr >> 24;
629 netmask_buf[0] = netmask & 0xff;
630 netmask_buf[1] = (netmask >> 8) & 0xff;
631 netmask_buf[2] = (netmask >> 16) & 0xff;
632 netmask_buf[3] = netmask >> 24;
633 (*proc)(addr_buf, netmask_buf, &lp->user);
638 struct notifier_block uml_inetaddr_notifier = {
639 .notifier_call = uml_inetaddr_event,
642 static int uml_net_init(void)
644 struct list_head *ele;
645 struct uml_net_private *lp;
646 struct in_device *ip;
647 struct in_ifaddr *in;
649 mconsole_register_dev(&net_mc);
650 register_inetaddr_notifier(¨_inetaddr_notifier);
652 /* Devices may have been opened already, so the uml_inetaddr_notifier
653 * didn't get a chance to run for them. This fakes it so that
654 * addresses which have already been set up get handled properly.
656 list_for_each(ele, &opened){
657 lp = list_entry(ele, struct uml_net_private, list);
658 ip = lp->dev->ip_ptr;
659 if(ip == NULL) continue;
662 uml_inetaddr_event(NULL, NETDEV_UP, in);
670 __initcall(uml_net_init);
672 static void close_devices(void)
674 struct list_head *ele;
675 struct uml_net_private *lp;
677 list_for_each(ele, &opened){
678 lp = list_entry(ele, struct uml_net_private, list);
679 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
680 if(lp->remove != NULL) (*lp->remove)(&lp->user);
684 __uml_exitcall(close_devices);
686 int setup_etheraddr(char *str, unsigned char *addr)
694 addr[i] = simple_strtoul(str, &end, 16);
696 ((*end != ':') && (*end != ',') && (*end != '\0'))){
698 "setup_etheraddr: failed to parse '%s' "
699 "as an ethernet address\n", str);
706 "Attempt to assign a broadcast ethernet address to a "
707 "device disallowed\n");
713 void dev_ip_addr(void *d, char *buf, char *bin_buf)
715 struct net_device *dev = d;
716 struct in_device *ip = dev->ip_ptr;
717 struct in_ifaddr *in;
720 if((ip == NULL) || ((in = ip->ifa_list) == NULL)){
721 printk(KERN_WARNING "dev_ip_addr - device not assigned an "
725 addr = in->ifa_address;
726 sprintf(buf, "%d.%d.%d.%d", addr & 0xff, (addr >> 8) & 0xff,
727 (addr >> 16) & 0xff, addr >> 24);
729 bin_buf[0] = addr & 0xff;
730 bin_buf[1] = (addr >> 8) & 0xff;
731 bin_buf[2] = (addr >> 16) & 0xff;
732 bin_buf[3] = addr >> 24;
736 void set_ether_mac(void *d, unsigned char *addr)
738 struct net_device *dev = d;
740 memcpy(dev->dev_addr, addr, ETH_ALEN);
743 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
745 if((skb != NULL) && (skb_tailroom(skb) < extra)){
746 struct sk_buff *skb2;
748 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
752 if(skb != NULL) skb_put(skb, extra);
756 void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
760 struct net_device *dev = d;
761 struct in_device *ip = dev->ip_ptr;
762 struct in_ifaddr *in;
763 unsigned char address[4], netmask[4];
765 if(ip == NULL) return;
768 address[0] = in->ifa_address & 0xff;
769 address[1] = (in->ifa_address >> 8) & 0xff;
770 address[2] = (in->ifa_address >> 16) & 0xff;
771 address[3] = in->ifa_address >> 24;
772 netmask[0] = in->ifa_mask & 0xff;
773 netmask[1] = (in->ifa_mask >> 8) & 0xff;
774 netmask[2] = (in->ifa_mask >> 16) & 0xff;
775 netmask[3] = in->ifa_mask >> 24;
776 (*cb)(address, netmask, arg);
781 int dev_netmask(void *d, void *m)
783 struct net_device *dev = d;
784 struct in_device *ip = dev->ip_ptr;
785 struct in_ifaddr *in;
795 *mask_out = in->ifa_mask;
799 void *get_output_buffer(int *len_out)
803 ret = (void *) __get_free_pages(GFP_KERNEL, 0);
804 if(ret) *len_out = PAGE_SIZE;
809 void free_output_buffer(void *buffer)
811 free_pages((unsigned long) buffer, 0);
814 int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
819 remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
821 printk("tap_setup_common - Extra garbage on specification : "
829 unsigned short eth_protocol(struct sk_buff *skb)
831 return(eth_type_trans(skb, skb->dev));
835 * Overrides for Emacs so that we follow Linus's tabbing style.
836 * Emacs will notice this stuff at the end of the file and automatically
837 * adjust the settings for this buffer only. This must remain at the end
839 * ---------------------------------------------------------------------------
841 * c-file-style: "linux"