Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / arch / um / drivers / net_kern.c
1 /*
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.
6  */
7
8 #include "linux/kernel.h"
9 #include "linux/netdevice.h"
10 #include "linux/rtnetlink.h"
11 #include "linux/skbuff.h"
12 #include "linux/socket.h"
13 #include "linux/spinlock.h"
14 #include "linux/module.h"
15 #include "linux/init.h"
16 #include "linux/etherdevice.h"
17 #include "linux/list.h"
18 #include "linux/inetdevice.h"
19 #include "linux/ctype.h"
20 #include "linux/bootmem.h"
21 #include "linux/ethtool.h"
22 #include "linux/platform_device.h"
23 #include "asm/uaccess.h"
24 #include "kern_util.h"
25 #include "net_kern.h"
26 #include "net_user.h"
27 #include "mconsole_kern.h"
28 #include "init.h"
29 #include "irq_user.h"
30 #include "irq_kern.h"
31
32 static inline void set_ether_mac(struct net_device *dev, unsigned char *addr)
33 {
34         memcpy(dev->dev_addr, addr, ETH_ALEN);
35 }
36
37 #define DRIVER_NAME "uml-netdev"
38
39 static DEFINE_SPINLOCK(opened_lock);
40 static LIST_HEAD(opened);
41
42 static int uml_net_rx(struct net_device *dev)
43 {
44         struct uml_net_private *lp = dev->priv;
45         int pkt_len;
46         struct sk_buff *skb;
47
48         /* If we can't allocate memory, try again next round. */
49         skb = dev_alloc_skb(dev->mtu);
50         if (skb == NULL) {
51                 lp->stats.rx_dropped++;
52                 return 0;
53         }
54
55         skb->dev = dev;
56         skb_put(skb, dev->mtu);
57         skb_reset_mac_header(skb);
58         pkt_len = (*lp->read)(lp->fd, &skb, lp);
59
60         if (pkt_len > 0) {
61                 skb_trim(skb, pkt_len);
62                 skb->protocol = (*lp->protocol)(skb);
63                 netif_rx(skb);
64
65                 lp->stats.rx_bytes += skb->len;
66                 lp->stats.rx_packets++;
67                 return pkt_len;
68         }
69
70         kfree_skb(skb);
71         return pkt_len;
72 }
73
74 static void uml_dev_close(struct work_struct *work)
75 {
76         struct uml_net_private *lp =
77                 container_of(work, struct uml_net_private, work);
78         dev_close(lp->dev);
79 }
80
81 irqreturn_t uml_net_interrupt(int irq, void *dev_id)
82 {
83         struct net_device *dev = dev_id;
84         struct uml_net_private *lp = dev->priv;
85         int err;
86
87         if(!netif_running(dev))
88                 return(IRQ_NONE);
89
90         spin_lock(&lp->lock);
91         while((err = uml_net_rx(dev)) > 0) ;
92         if(err < 0) {
93                 printk(KERN_ERR
94                        "Device '%s' read returned %d, shutting it down\n",
95                        dev->name, err);
96                 /* dev_close can't be called in interrupt context, and takes
97                  * again lp->lock.
98                  * And dev_close() can be safely called multiple times on the
99                  * same device, since it tests for (dev->flags & IFF_UP). So
100                  * there's no harm in delaying the device shutdown.
101                  * Furthermore, the workqueue will not re-enqueue an already
102                  * enqueued work item. */
103                 schedule_work(&lp->work);
104                 goto out;
105         }
106         reactivate_fd(lp->fd, UM_ETH_IRQ);
107
108 out:
109         spin_unlock(&lp->lock);
110         return IRQ_HANDLED;
111 }
112
113 static int uml_net_open(struct net_device *dev)
114 {
115         struct uml_net_private *lp = dev->priv;
116         int err;
117
118         if(lp->fd >= 0){
119                 err = -ENXIO;
120                 goto out;
121         }
122
123         lp->fd = (*lp->open)(&lp->user);
124         if(lp->fd < 0){
125                 err = lp->fd;
126                 goto out;
127         }
128
129         err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
130                              IRQF_DISABLED | IRQF_SHARED, dev->name, dev);
131         if(err != 0){
132                 printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
133                 err = -ENETUNREACH;
134                 goto out_close;
135         }
136
137         lp->tl.data = (unsigned long) &lp->user;
138         netif_start_queue(dev);
139
140         /* clear buffer - it can happen that the host side of the interface
141          * is full when we get here.  In this case, new data is never queued,
142          * SIGIOs never arrive, and the net never works.
143          */
144         while((err = uml_net_rx(dev)) > 0) ;
145
146         spin_lock(&opened_lock);
147         list_add(&lp->list, &opened);
148         spin_unlock(&opened_lock);
149
150         return 0;
151 out_close:
152         if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
153         lp->fd = -1;
154 out:
155         return err;
156 }
157
158 static int uml_net_close(struct net_device *dev)
159 {
160         struct uml_net_private *lp = dev->priv;
161
162         netif_stop_queue(dev);
163
164         free_irq(dev->irq, dev);
165         if(lp->close != NULL)
166                 (*lp->close)(lp->fd, &lp->user);
167         lp->fd = -1;
168
169         spin_lock(&opened_lock);
170         list_del(&lp->list);
171         spin_unlock(&opened_lock);
172
173         return 0;
174 }
175
176 static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
177 {
178         struct uml_net_private *lp = dev->priv;
179         unsigned long flags;
180         int len;
181
182         netif_stop_queue(dev);
183
184         spin_lock_irqsave(&lp->lock, flags);
185
186         len = (*lp->write)(lp->fd, &skb, lp);
187
188         if(len == skb->len) {
189                 lp->stats.tx_packets++;
190                 lp->stats.tx_bytes += skb->len;
191                 dev->trans_start = jiffies;
192                 netif_start_queue(dev);
193
194                 /* this is normally done in the interrupt when tx finishes */
195                 netif_wake_queue(dev);
196         }
197         else if(len == 0){
198                 netif_start_queue(dev);
199                 lp->stats.tx_dropped++;
200         }
201         else {
202                 netif_start_queue(dev);
203                 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
204         }
205
206         spin_unlock_irqrestore(&lp->lock, flags);
207
208         dev_kfree_skb(skb);
209
210         return 0;
211 }
212
213 static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
214 {
215         struct uml_net_private *lp = dev->priv;
216         return &lp->stats;
217 }
218
219 static void uml_net_set_multicast_list(struct net_device *dev)
220 {
221         if (dev->flags & IFF_PROMISC) return;
222         else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
223         else dev->flags &= ~IFF_ALLMULTI;
224 }
225
226 static void uml_net_tx_timeout(struct net_device *dev)
227 {
228         dev->trans_start = jiffies;
229         netif_wake_queue(dev);
230 }
231
232 static int uml_net_set_mac(struct net_device *dev, void *addr)
233 {
234         struct uml_net_private *lp = dev->priv;
235         struct sockaddr *hwaddr = addr;
236
237         spin_lock_irq(&lp->lock);
238         set_ether_mac(dev, hwaddr->sa_data);
239         spin_unlock_irq(&lp->lock);
240
241         return 0;
242 }
243
244 static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
245 {
246         struct uml_net_private *lp = dev->priv;
247         int err = 0;
248
249         spin_lock_irq(&lp->lock);
250
251         new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
252         if(new_mtu < 0){
253                 err = new_mtu;
254                 goto out;
255         }
256
257         dev->mtu = new_mtu;
258
259  out:
260         spin_unlock_irq(&lp->lock);
261         return err;
262 }
263
264 static void uml_net_get_drvinfo(struct net_device *dev,
265                                 struct ethtool_drvinfo *info)
266 {
267         strcpy(info->driver, DRIVER_NAME);
268         strcpy(info->version, "42");
269 }
270
271 static struct ethtool_ops uml_net_ethtool_ops = {
272         .get_drvinfo    = uml_net_get_drvinfo,
273         .get_link       = ethtool_op_get_link,
274 };
275
276 void uml_net_user_timer_expire(unsigned long _conn)
277 {
278 #ifdef undef
279         struct connection *conn = (struct connection *)_conn;
280
281         dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
282         do_connect(conn);
283 #endif
284 }
285
286 static void setup_etheraddr(char *str, unsigned char *addr, char *name)
287 {
288         char *end;
289         int i;
290
291         if(str == NULL)
292                 goto random;
293
294         for(i=0;i<6;i++){
295                 addr[i] = simple_strtoul(str, &end, 16);
296                 if((end == str) ||
297                    ((*end != ':') && (*end != ',') && (*end != '\0'))){
298                         printk(KERN_ERR
299                                "setup_etheraddr: failed to parse '%s' "
300                                "as an ethernet address\n", str);
301                         goto random;
302                 }
303                 str = end + 1;
304         }
305         if (is_multicast_ether_addr(addr)) {
306                 printk(KERN_ERR
307                        "Attempt to assign a multicast ethernet address to a "
308                        "device disallowed\n");
309                 goto random;
310         }
311         if (!is_valid_ether_addr(addr)) {
312                 printk(KERN_ERR
313                        "Attempt to assign an invalid ethernet address to a "
314                        "device disallowed\n");
315                 goto random;
316         }
317         if (!is_local_ether_addr(addr)) {
318                 printk(KERN_WARNING
319                        "Warning: attempt to assign a globally valid ethernet address to a "
320                        "device\n");
321                 printk(KERN_WARNING "You should better enable the 2nd rightmost bit "
322                       "in the first byte of the MAC, i.e. "
323                       "%02x:%02x:%02x:%02x:%02x:%02x\n",
324                       addr[0] | 0x02, addr[1], addr[2], addr[3], addr[4], addr[5]);
325         }
326         return;
327
328 random:
329         printk(KERN_INFO
330                "Choosing a random ethernet address for device %s\n", name);
331         random_ether_addr(addr);
332 }
333
334 static DEFINE_SPINLOCK(devices_lock);
335 static LIST_HEAD(devices);
336
337 static struct platform_driver uml_net_driver = {
338         .driver = {
339                 .name  = DRIVER_NAME,
340         },
341 };
342 static int driver_registered;
343
344 static void net_device_release(struct device *dev)
345 {
346         struct uml_net *device = dev->driver_data;
347         struct net_device *netdev = device->dev;
348         struct uml_net_private *lp = netdev->priv;
349
350         if(lp->remove != NULL)
351                 (*lp->remove)(&lp->user);
352         list_del(&device->list);
353         kfree(device);
354         free_netdev(netdev);
355 }
356
357 static void eth_configure(int n, void *init, char *mac,
358                           struct transport *transport)
359 {
360         struct uml_net *device;
361         struct net_device *dev;
362         struct uml_net_private *lp;
363         int err, size;
364
365         size = transport->private_size + sizeof(struct uml_net_private);
366
367         device = kzalloc(sizeof(*device), GFP_KERNEL);
368         if (device == NULL) {
369                 printk(KERN_ERR "eth_configure failed to allocate struct "
370                        "uml_net\n");
371                 return;
372         }
373
374         dev = alloc_etherdev(size);
375         if (dev == NULL) {
376                 printk(KERN_ERR "eth_configure: failed to allocate struct "
377                        "net_device for eth%d\n", n);
378                 goto out_free_device;
379         }
380
381         INIT_LIST_HEAD(&device->list);
382         device->index = n;
383
384         /* If this name ends up conflicting with an existing registered
385          * netdevice, that is OK, register_netdev{,ice}() will notice this
386          * and fail.
387          */
388         snprintf(dev->name, sizeof(dev->name), "eth%d", n);
389
390         setup_etheraddr(mac, device->mac, dev->name);
391
392         printk(KERN_INFO "Netdevice %d ", n);
393         printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
394                device->mac[0], device->mac[1],
395                device->mac[2], device->mac[3],
396                device->mac[4], device->mac[5]);
397         printk(": ");
398
399         lp = dev->priv;
400         /* This points to the transport private data. It's still clear, but we
401          * must memset it to 0 *now*. Let's help the drivers. */
402         memset(lp, 0, size);
403         INIT_WORK(&lp->work, uml_dev_close);
404
405         /* sysfs register */
406         if (!driver_registered) {
407                 platform_driver_register(&uml_net_driver);
408                 driver_registered = 1;
409         }
410         device->pdev.id = n;
411         device->pdev.name = DRIVER_NAME;
412         device->pdev.dev.release = net_device_release;
413         device->pdev.dev.driver_data = device;
414         if(platform_device_register(&device->pdev))
415                 goto out_free_netdev;
416         SET_NETDEV_DEV(dev,&device->pdev.dev);
417
418         device->dev = dev;
419
420         /*
421          * These just fill in a data structure, so there's no failure
422          * to be worried about.
423          */
424         (*transport->kern->init)(dev, init);
425
426         *lp = ((struct uml_net_private)
427                 { .list                 = LIST_HEAD_INIT(lp->list),
428                   .dev                  = dev,
429                   .fd                   = -1,
430                   .mac                  = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
431                   .protocol             = transport->kern->protocol,
432                   .open                 = transport->user->open,
433                   .close                = transport->user->close,
434                   .remove               = transport->user->remove,
435                   .read                 = transport->kern->read,
436                   .write                = transport->kern->write,
437                   .add_address          = transport->user->add_address,
438                   .delete_address       = transport->user->delete_address,
439                   .set_mtu              = transport->user->set_mtu });
440
441         init_timer(&lp->tl);
442         spin_lock_init(&lp->lock);
443         lp->tl.function = uml_net_user_timer_expire;
444         memcpy(lp->mac, device->mac, sizeof(lp->mac));
445
446         if ((transport->user->init != NULL) &&
447             ((*transport->user->init)(&lp->user, dev) != 0))
448                 goto out_unregister;
449
450         set_ether_mac(dev, device->mac);
451         dev->mtu = transport->user->max_packet;
452         dev->open = uml_net_open;
453         dev->hard_start_xmit = uml_net_start_xmit;
454         dev->stop = uml_net_close;
455         dev->get_stats = uml_net_get_stats;
456         dev->set_multicast_list = uml_net_set_multicast_list;
457         dev->tx_timeout = uml_net_tx_timeout;
458         dev->set_mac_address = uml_net_set_mac;
459         dev->change_mtu = uml_net_change_mtu;
460         dev->ethtool_ops = &uml_net_ethtool_ops;
461         dev->watchdog_timeo = (HZ >> 1);
462         dev->irq = UM_ETH_IRQ;
463
464         rtnl_lock();
465         err = register_netdevice(dev);
466         rtnl_unlock();
467         if (err)
468                 goto out_undo_user_init;
469
470         spin_lock(&devices_lock);
471         list_add(&device->list, &devices);
472         spin_unlock(&devices_lock);
473
474         return;
475
476 out_undo_user_init:
477         if (transport->user->remove != NULL)
478                 (*transport->user->remove)(&lp->user);
479 out_unregister:
480         platform_device_unregister(&device->pdev);
481 out_free_netdev:
482         free_netdev(dev);
483 out_free_device:
484         kfree(device);
485 }
486
487 static struct uml_net *find_device(int n)
488 {
489         struct uml_net *device;
490         struct list_head *ele;
491
492         spin_lock(&devices_lock);
493         list_for_each(ele, &devices){
494                 device = list_entry(ele, struct uml_net, list);
495                 if(device->index == n)
496                         goto out;
497         }
498         device = NULL;
499  out:
500         spin_unlock(&devices_lock);
501         return device;
502 }
503
504 static int eth_parse(char *str, int *index_out, char **str_out,
505                      char **error_out)
506 {
507         char *end;
508         int n, err = -EINVAL;;
509
510         n = simple_strtoul(str, &end, 0);
511         if(end == str){
512                 *error_out = "Bad device number";
513                 return err;
514         }
515
516         str = end;
517         if(*str != '='){
518                 *error_out = "Expected '=' after device number";
519                 return err;
520         }
521
522         str++;
523         if(find_device(n)){
524                 *error_out = "Device already configured";
525                 return err;
526         }
527
528         *index_out = n;
529         *str_out = str;
530         return 0;
531 }
532
533 struct eth_init {
534         struct list_head list;
535         char *init;
536         int index;
537 };
538
539 static DEFINE_SPINLOCK(transports_lock);
540 static LIST_HEAD(transports);
541
542 /* Filled in during early boot */
543 static LIST_HEAD(eth_cmd_line);
544
545 static int check_transport(struct transport *transport, char *eth, int n,
546                            void **init_out, char **mac_out)
547 {
548         int len;
549
550         len = strlen(transport->name);
551         if(strncmp(eth, transport->name, len))
552                 return 0;
553
554         eth += len;
555         if(*eth == ',')
556                 eth++;
557         else if(*eth != '\0')
558                 return 0;
559
560         *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
561         if(*init_out == NULL)
562                 return 1;
563
564         if(!transport->setup(eth, mac_out, *init_out)){
565                 kfree(*init_out);
566                 *init_out = NULL;
567         }
568         return 1;
569 }
570
571 void register_transport(struct transport *new)
572 {
573         struct list_head *ele, *next;
574         struct eth_init *eth;
575         void *init;
576         char *mac = NULL;
577         int match;
578
579         spin_lock(&transports_lock);
580         BUG_ON(!list_empty(&new->list));
581         list_add(&new->list, &transports);
582         spin_unlock(&transports_lock);
583
584         list_for_each_safe(ele, next, &eth_cmd_line){
585                 eth = list_entry(ele, struct eth_init, list);
586                 match = check_transport(new, eth->init, eth->index, &init,
587                                         &mac);
588                 if(!match)
589                         continue;
590                 else if(init != NULL){
591                         eth_configure(eth->index, init, mac, new);
592                         kfree(init);
593                 }
594                 list_del(&eth->list);
595         }
596 }
597
598 static int eth_setup_common(char *str, int index)
599 {
600         struct list_head *ele;
601         struct transport *transport;
602         void *init;
603         char *mac = NULL;
604         int found = 0;
605
606         spin_lock(&transports_lock);
607         list_for_each(ele, &transports){
608                 transport = list_entry(ele, struct transport, list);
609                 if(!check_transport(transport, str, index, &init, &mac))
610                         continue;
611                 if(init != NULL){
612                         eth_configure(index, init, mac, transport);
613                         kfree(init);
614                 }
615                 found = 1;
616                 break;
617         }
618
619         spin_unlock(&transports_lock);
620         return found;
621 }
622
623 static int eth_setup(char *str)
624 {
625         struct eth_init *new;
626         char *error;
627         int n, err;
628
629         err = eth_parse(str, &n, &str, &error);
630         if(err){
631                 printk(KERN_ERR "eth_setup - Couldn't parse '%s' : %s\n",
632                        str, error);
633                 return 1;
634         }
635
636         new = alloc_bootmem(sizeof(*new));
637         if (new == NULL){
638                 printk("eth_init : alloc_bootmem failed\n");
639                 return 1;
640         }
641
642         INIT_LIST_HEAD(&new->list);
643         new->index = n;
644         new->init = str;
645
646         list_add_tail(&new->list, &eth_cmd_line);
647         return 1;
648 }
649
650 __setup("eth", eth_setup);
651 __uml_help(eth_setup,
652 "eth[0-9]+=<transport>,<options>\n"
653 "    Configure a network device.\n\n"
654 );
655
656 static int net_config(char *str, char **error_out)
657 {
658         int n, err;
659
660         err = eth_parse(str, &n, &str, error_out);
661         if(err)
662                 return err;
663
664         /* This string is broken up and the pieces used by the underlying
665          * driver.  So, it is freed only if eth_setup_common fails.
666          */
667         str = kstrdup(str, GFP_KERNEL);
668         if(str == NULL){
669                 *error_out = "net_config failed to strdup string";
670                 return -ENOMEM;
671         }
672         err = !eth_setup_common(str, n);
673         if(err)
674                 kfree(str);
675         return(err);
676 }
677
678 static int net_id(char **str, int *start_out, int *end_out)
679 {
680         char *end;
681         int n;
682
683         n = simple_strtoul(*str, &end, 0);
684         if((*end != '\0') || (end == *str))
685                 return -1;
686
687         *start_out = n;
688         *end_out = n;
689         *str = end;
690         return n;
691 }
692
693 static int net_remove(int n, char **error_out)
694 {
695         struct uml_net *device;
696         struct net_device *dev;
697         struct uml_net_private *lp;
698
699         device = find_device(n);
700         if(device == NULL)
701                 return -ENODEV;
702
703         dev = device->dev;
704         lp = dev->priv;
705         if(lp->fd > 0)
706                 return -EBUSY;
707         unregister_netdev(dev);
708         platform_device_unregister(&device->pdev);
709
710         return 0;
711 }
712
713 static struct mc_device net_mc = {
714         .list           = LIST_HEAD_INIT(net_mc.list),
715         .name           = "eth",
716         .config         = net_config,
717         .get_config     = NULL,
718         .id             = net_id,
719         .remove         = net_remove,
720 };
721
722 static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
723                               void *ptr)
724 {
725         struct in_ifaddr *ifa = ptr;
726         struct net_device *dev = ifa->ifa_dev->dev;
727         struct uml_net_private *lp;
728         void (*proc)(unsigned char *, unsigned char *, void *);
729         unsigned char addr_buf[4], netmask_buf[4];
730
731         if(dev->open != uml_net_open)
732                 return NOTIFY_DONE;
733
734         lp = dev->priv;
735
736         proc = NULL;
737         switch (event){
738         case NETDEV_UP:
739                 proc = lp->add_address;
740                 break;
741         case NETDEV_DOWN:
742                 proc = lp->delete_address;
743                 break;
744         }
745         if(proc != NULL){
746                 memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
747                 memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
748                 (*proc)(addr_buf, netmask_buf, &lp->user);
749         }
750         return NOTIFY_DONE;
751 }
752
753 /* uml_net_init shouldn't be called twice on two CPUs at the same time */
754 struct notifier_block uml_inetaddr_notifier = {
755         .notifier_call          = uml_inetaddr_event,
756 };
757
758 static int uml_net_init(void)
759 {
760         struct list_head *ele;
761         struct uml_net_private *lp;
762         struct in_device *ip;
763         struct in_ifaddr *in;
764
765         mconsole_register_dev(&net_mc);
766         register_inetaddr_notifier(&uml_inetaddr_notifier);
767
768         /* Devices may have been opened already, so the uml_inetaddr_notifier
769          * didn't get a chance to run for them.  This fakes it so that
770          * addresses which have already been set up get handled properly.
771          */
772         spin_lock(&opened_lock);
773         list_for_each(ele, &opened){
774                 lp = list_entry(ele, struct uml_net_private, list);
775                 ip = lp->dev->ip_ptr;
776                 if(ip == NULL)
777                         continue;
778                 in = ip->ifa_list;
779                 while(in != NULL){
780                         uml_inetaddr_event(NULL, NETDEV_UP, in);
781                         in = in->ifa_next;
782                 }
783         }
784         spin_unlock(&opened_lock);
785
786         return 0;
787 }
788
789 __initcall(uml_net_init);
790
791 static void close_devices(void)
792 {
793         struct list_head *ele;
794         struct uml_net_private *lp;
795
796         spin_lock(&opened_lock);
797         list_for_each(ele, &opened){
798                 lp = list_entry(ele, struct uml_net_private, list);
799                 free_irq(lp->dev->irq, lp->dev);
800                 if((lp->close != NULL) && (lp->fd >= 0))
801                         (*lp->close)(lp->fd, &lp->user);
802                 if(lp->remove != NULL)
803                         (*lp->remove)(&lp->user);
804         }
805         spin_unlock(&opened_lock);
806 }
807
808 __uml_exitcall(close_devices);
809
810 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
811 {
812         if((skb != NULL) && (skb_tailroom(skb) < extra)){
813                 struct sk_buff *skb2;
814
815                 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
816                 dev_kfree_skb(skb);
817                 skb = skb2;
818         }
819         if(skb != NULL) skb_put(skb, extra);
820         return(skb);
821 }
822
823 void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
824                                         void *),
825                     void *arg)
826 {
827         struct net_device *dev = d;
828         struct in_device *ip = dev->ip_ptr;
829         struct in_ifaddr *in;
830         unsigned char address[4], netmask[4];
831
832         if(ip == NULL) return;
833         in = ip->ifa_list;
834         while(in != NULL){
835                 memcpy(address, &in->ifa_address, sizeof(address));
836                 memcpy(netmask, &in->ifa_mask, sizeof(netmask));
837                 (*cb)(address, netmask, arg);
838                 in = in->ifa_next;
839         }
840 }
841
842 int dev_netmask(void *d, void *m)
843 {
844         struct net_device *dev = d;
845         struct in_device *ip = dev->ip_ptr;
846         struct in_ifaddr *in;
847         __be32 *mask_out = m;
848
849         if(ip == NULL)
850                 return(1);
851
852         in = ip->ifa_list;
853         if(in == NULL)
854                 return(1);
855
856         *mask_out = in->ifa_mask;
857         return(0);
858 }
859
860 void *get_output_buffer(int *len_out)
861 {
862         void *ret;
863
864         ret = (void *) __get_free_pages(GFP_KERNEL, 0);
865         if(ret) *len_out = PAGE_SIZE;
866         else *len_out = 0;
867         return ret;
868 }
869
870 void free_output_buffer(void *buffer)
871 {
872         free_pages((unsigned long) buffer, 0);
873 }
874
875 int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
876                      char **gate_addr)
877 {
878         char *remain;
879
880         remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
881         if(remain != NULL){
882                 printk("tap_setup_common - Extra garbage on specification : "
883                        "'%s'\n", remain);
884                 return(1);
885         }
886
887         return(0);
888 }
889
890 unsigned short eth_protocol(struct sk_buff *skb)
891 {
892         return(eth_type_trans(skb, skb->dev));
893 }