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);
128 spin_unlock(&lp->lock);
132 static int uml_net_close(struct net_device *dev)
134 struct uml_net_private *lp = dev->priv;
136 netif_stop_queue(dev);
137 spin_lock(&lp->lock);
139 free_irq(dev->irq, dev);
140 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
142 spin_lock(&opened_lock);
144 spin_unlock(&opened_lock);
146 spin_unlock(&lp->lock);
150 static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
152 struct uml_net_private *lp = dev->priv;
156 netif_stop_queue(dev);
158 spin_lock_irqsave(&lp->lock, flags);
160 len = (*lp->write)(lp->fd, &skb, lp);
162 if(len == skb->len) {
163 lp->stats.tx_packets++;
164 lp->stats.tx_bytes += skb->len;
165 dev->trans_start = jiffies;
166 netif_start_queue(dev);
168 /* this is normally done in the interrupt when tx finishes */
169 netif_wake_queue(dev);
172 netif_start_queue(dev);
173 lp->stats.tx_dropped++;
176 netif_start_queue(dev);
177 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
180 spin_unlock_irqrestore(&lp->lock, flags);
187 static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
189 struct uml_net_private *lp = dev->priv;
193 static void uml_net_set_multicast_list(struct net_device *dev)
195 if (dev->flags & IFF_PROMISC) return;
196 else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
197 else dev->flags &= ~IFF_ALLMULTI;
200 static void uml_net_tx_timeout(struct net_device *dev)
202 dev->trans_start = jiffies;
203 netif_wake_queue(dev);
206 static int uml_net_set_mac(struct net_device *dev, void *addr)
208 struct uml_net_private *lp = dev->priv;
209 struct sockaddr *hwaddr = addr;
211 spin_lock(&lp->lock);
212 memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
213 spin_unlock(&lp->lock);
218 static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
220 struct uml_net_private *lp = dev->priv;
223 spin_lock(&lp->lock);
225 new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
234 spin_unlock(&lp->lock);
238 static int uml_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
243 void uml_net_user_timer_expire(unsigned long _conn)
246 struct connection *conn = (struct connection *)_conn;
248 dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
254 * default do nothing hard header packet routines for struct net_device init.
255 * real ethernet transports will overwrite with real routines.
257 static int uml_net_hard_header(struct sk_buff *skb, struct net_device *dev,
258 unsigned short type, void *daddr, void *saddr, unsigned len)
260 return(0); /* no change */
263 static int uml_net_rebuild_header(struct sk_buff *skb)
265 return(0); /* ignore */
268 static int uml_net_header_cache(struct neighbour *neigh, struct hh_cache *hh)
270 return(-1); /* fail */
273 static void uml_net_header_cache_update(struct hh_cache *hh,
274 struct net_device *dev, unsigned char * haddr)
279 static int uml_net_header_parse(struct sk_buff *skb, unsigned char *haddr)
281 return(0); /* nothing */
284 static spinlock_t devices_lock = SPIN_LOCK_UNLOCKED;
285 static struct list_head devices = LIST_HEAD_INIT(devices);
287 static int eth_configure(int n, void *init, char *mac,
288 struct transport *transport)
290 struct uml_net *device;
291 struct net_device *dev;
292 struct uml_net_private *lp;
295 size = transport->private_size + sizeof(struct uml_net_private) +
296 sizeof(((struct uml_net_private *) 0)->user);
298 device = kmalloc(sizeof(*device), GFP_KERNEL);
299 if (device == NULL) {
300 printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
304 memset(device, 0, sizeof(*device));
305 INIT_LIST_HEAD(&device->list);
308 spin_lock(&devices_lock);
309 list_add(&device->list, &devices);
310 spin_unlock(&devices_lock);
312 if (setup_etheraddr(mac, device->mac))
313 device->have_mac = 1;
315 printk(KERN_INFO "Netdevice %d ", n);
316 if (device->have_mac)
317 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
318 device->mac[0], device->mac[1],
319 device->mac[2], device->mac[3],
320 device->mac[4], device->mac[5]);
322 dev = alloc_etherdev(size);
324 printk(KERN_ERR "eth_configure: failed to allocate device\n");
328 /* If this name ends up conflicting with an existing registered
329 * netdevice, that is OK, register_netdev{,ice}() will notice this
332 snprintf(dev->name, sizeof(dev->name), "eth%d", n);
335 dev->hard_header = uml_net_hard_header;
336 dev->rebuild_header = uml_net_rebuild_header;
337 dev->hard_header_cache = uml_net_header_cache;
338 dev->header_cache_update= uml_net_header_cache_update;
339 dev->hard_header_parse = uml_net_header_parse;
341 (*transport->kern->init)(dev, init);
343 dev->mtu = transport->user->max_packet;
344 dev->open = uml_net_open;
345 dev->hard_start_xmit = uml_net_start_xmit;
346 dev->stop = uml_net_close;
347 dev->get_stats = uml_net_get_stats;
348 dev->set_multicast_list = uml_net_set_multicast_list;
349 dev->tx_timeout = uml_net_tx_timeout;
350 dev->set_mac_address = uml_net_set_mac;
351 dev->change_mtu = uml_net_change_mtu;
352 dev->do_ioctl = uml_net_ioctl;
353 dev->watchdog_timeo = (HZ >> 1);
354 dev->irq = UM_ETH_IRQ;
357 err = register_netdevice(dev);
361 /* XXX: should we call ->remove() here? */
367 INIT_LIST_HEAD(&lp->list);
368 spin_lock_init(&lp->lock);
371 lp->mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0 };
372 lp->have_mac = device->have_mac;
373 lp->protocol = transport->kern->protocol;
374 lp->open = transport->user->open;
375 lp->close = transport->user->close;
376 lp->remove = transport->user->remove;
377 lp->read = transport->kern->read;
378 lp->write = transport->kern->write;
379 lp->add_address = transport->user->add_address;
380 lp->delete_address = transport->user->delete_address;
381 lp->set_mtu = transport->user->set_mtu;
384 lp->tl.function = uml_net_user_timer_expire;
386 memcpy(lp->mac, device->mac, sizeof(lp->mac));
388 if (transport->user->init)
389 (*transport->user->init)(&lp->user, dev);
391 if (device->have_mac)
392 set_ether_mac(dev, device->mac);
396 static struct uml_net *find_device(int n)
398 struct uml_net *device;
399 struct list_head *ele;
401 spin_lock(&devices_lock);
402 list_for_each(ele, &devices){
403 device = list_entry(ele, struct uml_net, list);
404 if(device->index == n)
409 spin_unlock(&devices_lock);
413 static int eth_parse(char *str, int *index_out, char **str_out)
418 n = simple_strtoul(str, &end, 0);
420 printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
424 printk(KERN_ERR "eth_setup: device %d is negative\n", n);
430 "eth_setup: expected '=' after device number\n");
435 printk(KERN_ERR "eth_setup: Device %d already configured\n",
439 if(index_out) *index_out = n;
445 struct list_head list;
450 /* Filled in at boot time. Will need locking if the transports become
453 struct list_head transports = LIST_HEAD_INIT(transports);
455 /* Filled in during early boot */
456 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
458 static int check_transport(struct transport *transport, char *eth, int n,
459 void **init_out, char **mac_out)
463 len = strlen(transport->name);
464 if(strncmp(eth, transport->name, len))
470 else if(*eth != '\0')
473 *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
474 if(*init_out == NULL)
477 if(!transport->setup(eth, mac_out, *init_out)){
484 void register_transport(struct transport *new)
486 struct list_head *ele, *next;
487 struct eth_init *eth;
492 list_add(&new->list, &transports);
494 list_for_each_safe(ele, next, ð_cmd_line){
495 eth = list_entry(ele, struct eth_init, list);
496 match = check_transport(new, eth->init, eth->index, &init,
500 else if(init != NULL){
501 eth_configure(eth->index, init, mac, new);
504 list_del(ð->list);
508 static int eth_setup_common(char *str, int index)
510 struct list_head *ele;
511 struct transport *transport;
515 list_for_each(ele, &transports){
516 transport = list_entry(ele, struct transport, list);
517 if(!check_transport(transport, str, index, &init, &mac))
520 eth_configure(index, init, mac, transport);
528 static int eth_setup(char *str)
530 struct eth_init *new;
533 err = eth_parse(str, &n, &str);
536 new = alloc_bootmem(sizeof(new));
538 printk("eth_init : alloc_bootmem failed\n");
542 INIT_LIST_HEAD(&new->list);
546 list_add_tail(&new->list, ð_cmd_line);
550 __setup("eth", eth_setup);
551 __uml_help(eth_setup,
552 "eth[0-9]+=<transport>,<options>\n"
553 " Configure a network device.\n\n"
556 static int eth_init(void)
558 struct list_head *ele, *next;
559 struct eth_init *eth;
561 list_for_each_safe(ele, next, ð_cmd_line){
562 eth = list_entry(ele, struct eth_init, list);
564 if(eth_setup_common(eth->init, eth->index))
565 list_del(ð->list);
571 __initcall(eth_init);
573 static int net_config(char *str)
577 err = eth_parse(str, &n, &str);
580 str = uml_strdup(str);
582 printk(KERN_ERR "net_config failed to strdup string\n");
585 err = !eth_setup_common(str, n);
591 static int net_remove(char *str)
593 struct uml_net *device;
594 struct net_device *dev;
595 struct uml_net_private *lp;
599 n = simple_strtoul(str, &end, 0);
600 if((*end != '\0') || (end == str))
603 device = find_device(n);
609 if(lp->fd > 0) return(-1);
610 if(lp->remove != NULL) (*lp->remove)(&lp->user);
611 unregister_netdev(dev);
613 list_del(&device->list);
618 static struct mc_device net_mc = {
620 .config = net_config,
622 .remove = net_remove,
625 static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
628 struct in_ifaddr *ifa = ptr;
629 u32 addr = ifa->ifa_address;
630 u32 netmask = ifa->ifa_mask;
631 struct net_device *dev = ifa->ifa_dev->dev;
632 struct uml_net_private *lp;
633 void (*proc)(unsigned char *, unsigned char *, void *);
634 unsigned char addr_buf[4], netmask_buf[4];
636 if(dev->open != uml_net_open) return(NOTIFY_DONE);
643 proc = lp->add_address;
646 proc = lp->delete_address;
650 addr_buf[0] = addr & 0xff;
651 addr_buf[1] = (addr >> 8) & 0xff;
652 addr_buf[2] = (addr >> 16) & 0xff;
653 addr_buf[3] = addr >> 24;
654 netmask_buf[0] = netmask & 0xff;
655 netmask_buf[1] = (netmask >> 8) & 0xff;
656 netmask_buf[2] = (netmask >> 16) & 0xff;
657 netmask_buf[3] = netmask >> 24;
658 (*proc)(addr_buf, netmask_buf, &lp->user);
663 struct notifier_block uml_inetaddr_notifier = {
664 .notifier_call = uml_inetaddr_event,
667 static int uml_net_init(void)
669 struct list_head *ele;
670 struct uml_net_private *lp;
671 struct in_device *ip;
672 struct in_ifaddr *in;
674 mconsole_register_dev(&net_mc);
675 register_inetaddr_notifier(¨_inetaddr_notifier);
677 /* Devices may have been opened already, so the uml_inetaddr_notifier
678 * didn't get a chance to run for them. This fakes it so that
679 * addresses which have already been set up get handled properly.
681 list_for_each(ele, &opened){
682 lp = list_entry(ele, struct uml_net_private, list);
683 ip = lp->dev->ip_ptr;
684 if(ip == NULL) continue;
687 uml_inetaddr_event(NULL, NETDEV_UP, in);
695 __initcall(uml_net_init);
697 static void close_devices(void)
699 struct list_head *ele;
700 struct uml_net_private *lp;
702 list_for_each(ele, &opened){
703 lp = list_entry(ele, struct uml_net_private, list);
704 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
705 if(lp->remove != NULL) (*lp->remove)(&lp->user);
709 __uml_exitcall(close_devices);
711 int setup_etheraddr(char *str, unsigned char *addr)
719 addr[i] = simple_strtoul(str, &end, 16);
721 ((*end != ':') && (*end != ',') && (*end != '\0'))){
723 "setup_etheraddr: failed to parse '%s' "
724 "as an ethernet address\n", str);
731 "Attempt to assign a broadcast ethernet address to a "
732 "device disallowed\n");
738 void dev_ip_addr(void *d, char *buf, char *bin_buf)
740 struct net_device *dev = d;
741 struct in_device *ip = dev->ip_ptr;
742 struct in_ifaddr *in;
745 if((ip == NULL) || ((in = ip->ifa_list) == NULL)){
746 printk(KERN_WARNING "dev_ip_addr - device not assigned an "
750 addr = in->ifa_address;
751 sprintf(buf, "%d.%d.%d.%d", addr & 0xff, (addr >> 8) & 0xff,
752 (addr >> 16) & 0xff, addr >> 24);
754 bin_buf[0] = addr & 0xff;
755 bin_buf[1] = (addr >> 8) & 0xff;
756 bin_buf[2] = (addr >> 16) & 0xff;
757 bin_buf[3] = addr >> 24;
761 void set_ether_mac(void *d, unsigned char *addr)
763 struct net_device *dev = d;
765 memcpy(dev->dev_addr, addr, ETH_ALEN);
768 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
770 if((skb != NULL) && (skb_tailroom(skb) < extra)){
771 struct sk_buff *skb2;
773 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
777 if(skb != NULL) skb_put(skb, extra);
781 void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
785 struct net_device *dev = d;
786 struct in_device *ip = dev->ip_ptr;
787 struct in_ifaddr *in;
788 unsigned char address[4], netmask[4];
790 if(ip == NULL) return;
793 address[0] = in->ifa_address & 0xff;
794 address[1] = (in->ifa_address >> 8) & 0xff;
795 address[2] = (in->ifa_address >> 16) & 0xff;
796 address[3] = in->ifa_address >> 24;
797 netmask[0] = in->ifa_mask & 0xff;
798 netmask[1] = (in->ifa_mask >> 8) & 0xff;
799 netmask[2] = (in->ifa_mask >> 16) & 0xff;
800 netmask[3] = in->ifa_mask >> 24;
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"