uml: remove user_util.h
[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)
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(addr[0] & 1){
306                 printk(KERN_ERR
307                        "Attempt to assign a broadcast ethernet address to a "
308                        "device disallowed\n");
309                 goto random;
310         }
311         return;
312
313 random:
314         random_ether_addr(addr);
315 }
316
317 static DEFINE_SPINLOCK(devices_lock);
318 static LIST_HEAD(devices);
319
320 static struct platform_driver uml_net_driver = {
321         .driver = {
322                 .name  = DRIVER_NAME,
323         },
324 };
325 static int driver_registered;
326
327 static void eth_configure(int n, void *init, char *mac,
328                           struct transport *transport)
329 {
330         struct uml_net *device;
331         struct net_device *dev;
332         struct uml_net_private *lp;
333         int save, err, size;
334
335         size = transport->private_size + sizeof(struct uml_net_private) +
336                 sizeof(((struct uml_net_private *) 0)->user);
337
338         device = kzalloc(sizeof(*device), GFP_KERNEL);
339         if (device == NULL) {
340                 printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
341                 return;
342         }
343
344         INIT_LIST_HEAD(&device->list);
345         device->index = n;
346
347         setup_etheraddr(mac, device->mac);
348
349         printk(KERN_INFO "Netdevice %d ", n);
350         printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
351                device->mac[0], device->mac[1],
352                device->mac[2], device->mac[3],
353                device->mac[4], device->mac[5]);
354         printk(": ");
355         dev = alloc_etherdev(size);
356         if (dev == NULL) {
357                 printk(KERN_ERR "eth_configure: failed to allocate device\n");
358                 goto out_free_device;
359         }
360
361         lp = dev->priv;
362         /* This points to the transport private data. It's still clear, but we
363          * must memset it to 0 *now*. Let's help the drivers. */
364         memset(lp, 0, size);
365         INIT_WORK(&lp->work, uml_dev_close);
366
367         /* sysfs register */
368         if (!driver_registered) {
369                 platform_driver_register(&uml_net_driver);
370                 driver_registered = 1;
371         }
372         device->pdev.id = n;
373         device->pdev.name = DRIVER_NAME;
374         if(platform_device_register(&device->pdev))
375                 goto out_free_netdev;
376         SET_NETDEV_DEV(dev,&device->pdev.dev);
377
378         /* If this name ends up conflicting with an existing registered
379          * netdevice, that is OK, register_netdev{,ice}() will notice this
380          * and fail.
381          */
382         snprintf(dev->name, sizeof(dev->name), "eth%d", n);
383         device->dev = dev;
384
385         /*
386          * These just fill in a data structure, so there's no failure
387          * to be worried about.
388          */
389         (*transport->kern->init)(dev, init);
390
391         /* lp.user is the first four bytes of the transport data, which
392          * has already been initialized.  This structure assignment will
393          * overwrite that, so we make sure that .user gets overwritten with
394          * what it already has.
395          */
396         save = lp->user[0];
397         *lp = ((struct uml_net_private)
398                 { .list                 = LIST_HEAD_INIT(lp->list),
399                   .dev                  = dev,
400                   .fd                   = -1,
401                   .mac                  = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
402                   .protocol             = transport->kern->protocol,
403                   .open                 = transport->user->open,
404                   .close                = transport->user->close,
405                   .remove               = transport->user->remove,
406                   .read                 = transport->kern->read,
407                   .write                = transport->kern->write,
408                   .add_address          = transport->user->add_address,
409                   .delete_address       = transport->user->delete_address,
410                   .set_mtu              = transport->user->set_mtu,
411                   .user                 = { save } });
412
413         init_timer(&lp->tl);
414         spin_lock_init(&lp->lock);
415         lp->tl.function = uml_net_user_timer_expire;
416         memcpy(lp->mac, device->mac, sizeof(lp->mac));
417
418         if ((transport->user->init != NULL) &&
419             ((*transport->user->init)(&lp->user, dev) != 0))
420                 goto out_unregister;
421
422         set_ether_mac(dev, device->mac);
423         dev->mtu = transport->user->max_packet;
424         dev->open = uml_net_open;
425         dev->hard_start_xmit = uml_net_start_xmit;
426         dev->stop = uml_net_close;
427         dev->get_stats = uml_net_get_stats;
428         dev->set_multicast_list = uml_net_set_multicast_list;
429         dev->tx_timeout = uml_net_tx_timeout;
430         dev->set_mac_address = uml_net_set_mac;
431         dev->change_mtu = uml_net_change_mtu;
432         dev->ethtool_ops = &uml_net_ethtool_ops;
433         dev->watchdog_timeo = (HZ >> 1);
434         dev->irq = UM_ETH_IRQ;
435
436         rtnl_lock();
437         err = register_netdevice(dev);
438         rtnl_unlock();
439         if (err)
440                 goto out_undo_user_init;
441
442         spin_lock(&devices_lock);
443         list_add(&device->list, &devices);
444         spin_unlock(&devices_lock);
445
446         return;
447
448 out_undo_user_init:
449         if (transport->user->init != NULL)
450                 (*transport->user->remove)(&lp->user);
451 out_unregister:
452         platform_device_unregister(&device->pdev);
453 out_free_netdev:
454         free_netdev(dev);
455 out_free_device: ;
456         kfree(device);
457 }
458
459 static struct uml_net *find_device(int n)
460 {
461         struct uml_net *device;
462         struct list_head *ele;
463
464         spin_lock(&devices_lock);
465         list_for_each(ele, &devices){
466                 device = list_entry(ele, struct uml_net, list);
467                 if(device->index == n)
468                         goto out;
469         }
470         device = NULL;
471  out:
472         spin_unlock(&devices_lock);
473         return device;
474 }
475
476 static int eth_parse(char *str, int *index_out, char **str_out,
477                      char **error_out)
478 {
479         char *end;
480         int n, err = -EINVAL;;
481
482         n = simple_strtoul(str, &end, 0);
483         if(end == str){
484                 *error_out = "Bad device number";
485                 return err;
486         }
487
488         str = end;
489         if(*str != '='){
490                 *error_out = "Expected '=' after device number";
491                 return err;
492         }
493
494         str++;
495         if(find_device(n)){
496                 *error_out = "Device already configured";
497                 return err;
498         }
499
500         *index_out = n;
501         *str_out = str;
502         return 0;
503 }
504
505 struct eth_init {
506         struct list_head list;
507         char *init;
508         int index;
509 };
510
511 static DEFINE_SPINLOCK(transports_lock);
512 static LIST_HEAD(transports);
513
514 /* Filled in during early boot */
515 static LIST_HEAD(eth_cmd_line);
516
517 static int check_transport(struct transport *transport, char *eth, int n,
518                            void **init_out, char **mac_out)
519 {
520         int len;
521
522         len = strlen(transport->name);
523         if(strncmp(eth, transport->name, len))
524                 return 0;
525
526         eth += len;
527         if(*eth == ',')
528                 eth++;
529         else if(*eth != '\0')
530                 return 0;
531
532         *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
533         if(*init_out == NULL)
534                 return 1;
535
536         if(!transport->setup(eth, mac_out, *init_out)){
537                 kfree(*init_out);
538                 *init_out = NULL;
539         }
540         return 1;
541 }
542
543 void register_transport(struct transport *new)
544 {
545         struct list_head *ele, *next;
546         struct eth_init *eth;
547         void *init;
548         char *mac = NULL;
549         int match;
550
551         spin_lock(&transports_lock);
552         BUG_ON(!list_empty(&new->list));
553         list_add(&new->list, &transports);
554         spin_unlock(&transports_lock);
555
556         list_for_each_safe(ele, next, &eth_cmd_line){
557                 eth = list_entry(ele, struct eth_init, list);
558                 match = check_transport(new, eth->init, eth->index, &init,
559                                         &mac);
560                 if(!match)
561                         continue;
562                 else if(init != NULL){
563                         eth_configure(eth->index, init, mac, new);
564                         kfree(init);
565                 }
566                 list_del(&eth->list);
567         }
568 }
569
570 static int eth_setup_common(char *str, int index)
571 {
572         struct list_head *ele;
573         struct transport *transport;
574         void *init;
575         char *mac = NULL;
576         int found = 0;
577
578         spin_lock(&transports_lock);
579         list_for_each(ele, &transports){
580                 transport = list_entry(ele, struct transport, list);
581                 if(!check_transport(transport, str, index, &init, &mac))
582                         continue;
583                 if(init != NULL){
584                         eth_configure(index, init, mac, transport);
585                         kfree(init);
586                 }
587                 found = 1;
588                 break;
589         }
590
591         spin_unlock(&transports_lock);
592         return found;
593 }
594
595 static int eth_setup(char *str)
596 {
597         struct eth_init *new;
598         char *error;
599         int n, err;
600
601         err = eth_parse(str, &n, &str, &error);
602         if(err){
603                 printk(KERN_ERR "eth_setup - Couldn't parse '%s' : %s\n",
604                        str, error);
605                 return 1;
606         }
607
608         new = alloc_bootmem(sizeof(*new));
609         if (new == NULL){
610                 printk("eth_init : alloc_bootmem failed\n");
611                 return 1;
612         }
613
614         INIT_LIST_HEAD(&new->list);
615         new->index = n;
616         new->init = str;
617
618         list_add_tail(&new->list, &eth_cmd_line);
619         return 1;
620 }
621
622 __setup("eth", eth_setup);
623 __uml_help(eth_setup,
624 "eth[0-9]+=<transport>,<options>\n"
625 "    Configure a network device.\n\n"
626 );
627
628 static int net_config(char *str, char **error_out)
629 {
630         int n, err;
631
632         err = eth_parse(str, &n, &str, error_out);
633         if(err)
634                 return err;
635
636         /* This string is broken up and the pieces used by the underlying
637          * driver.  So, it is freed only if eth_setup_common fails.
638          */
639         str = kstrdup(str, GFP_KERNEL);
640         if(str == NULL){
641                 *error_out = "net_config failed to strdup string";
642                 return -ENOMEM;
643         }
644         err = !eth_setup_common(str, n);
645         if(err)
646                 kfree(str);
647         return(err);
648 }
649
650 static int net_id(char **str, int *start_out, int *end_out)
651 {
652         char *end;
653         int n;
654
655         n = simple_strtoul(*str, &end, 0);
656         if((*end != '\0') || (end == *str))
657                 return -1;
658
659         *start_out = n;
660         *end_out = n;
661         *str = end;
662         return n;
663 }
664
665 static int net_remove(int n, char **error_out)
666 {
667         struct uml_net *device;
668         struct net_device *dev;
669         struct uml_net_private *lp;
670
671         device = find_device(n);
672         if(device == NULL)
673                 return -ENODEV;
674
675         dev = device->dev;
676         lp = dev->priv;
677         if(lp->fd > 0)
678                 return -EBUSY;
679         if(lp->remove != NULL) (*lp->remove)(&lp->user);
680         unregister_netdev(dev);
681         platform_device_unregister(&device->pdev);
682
683         list_del(&device->list);
684         kfree(device);
685         free_netdev(dev);
686         return 0;
687 }
688
689 static struct mc_device net_mc = {
690         .list           = LIST_HEAD_INIT(net_mc.list),
691         .name           = "eth",
692         .config         = net_config,
693         .get_config     = NULL,
694         .id             = net_id,
695         .remove         = net_remove,
696 };
697
698 static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
699                               void *ptr)
700 {
701         struct in_ifaddr *ifa = ptr;
702         struct net_device *dev = ifa->ifa_dev->dev;
703         struct uml_net_private *lp;
704         void (*proc)(unsigned char *, unsigned char *, void *);
705         unsigned char addr_buf[4], netmask_buf[4];
706
707         if(dev->open != uml_net_open)
708                 return NOTIFY_DONE;
709
710         lp = dev->priv;
711
712         proc = NULL;
713         switch (event){
714         case NETDEV_UP:
715                 proc = lp->add_address;
716                 break;
717         case NETDEV_DOWN:
718                 proc = lp->delete_address;
719                 break;
720         }
721         if(proc != NULL){
722                 memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
723                 memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
724                 (*proc)(addr_buf, netmask_buf, &lp->user);
725         }
726         return NOTIFY_DONE;
727 }
728
729 /* uml_net_init shouldn't be called twice on two CPUs at the same time */
730 struct notifier_block uml_inetaddr_notifier = {
731         .notifier_call          = uml_inetaddr_event,
732 };
733
734 static int uml_net_init(void)
735 {
736         struct list_head *ele;
737         struct uml_net_private *lp;
738         struct in_device *ip;
739         struct in_ifaddr *in;
740
741         mconsole_register_dev(&net_mc);
742         register_inetaddr_notifier(&uml_inetaddr_notifier);
743
744         /* Devices may have been opened already, so the uml_inetaddr_notifier
745          * didn't get a chance to run for them.  This fakes it so that
746          * addresses which have already been set up get handled properly.
747          */
748         spin_lock(&opened_lock);
749         list_for_each(ele, &opened){
750                 lp = list_entry(ele, struct uml_net_private, list);
751                 ip = lp->dev->ip_ptr;
752                 if(ip == NULL)
753                         continue;
754                 in = ip->ifa_list;
755                 while(in != NULL){
756                         uml_inetaddr_event(NULL, NETDEV_UP, in);
757                         in = in->ifa_next;
758                 }
759         }
760         spin_unlock(&opened_lock);
761
762         return 0;
763 }
764
765 __initcall(uml_net_init);
766
767 static void close_devices(void)
768 {
769         struct list_head *ele;
770         struct uml_net_private *lp;
771
772         spin_lock(&opened_lock);
773         list_for_each(ele, &opened){
774                 lp = list_entry(ele, struct uml_net_private, list);
775                 free_irq(lp->dev->irq, lp->dev);
776                 if((lp->close != NULL) && (lp->fd >= 0))
777                         (*lp->close)(lp->fd, &lp->user);
778                 if(lp->remove != NULL)
779                         (*lp->remove)(&lp->user);
780         }
781         spin_unlock(&opened_lock);
782 }
783
784 __uml_exitcall(close_devices);
785
786 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
787 {
788         if((skb != NULL) && (skb_tailroom(skb) < extra)){
789                 struct sk_buff *skb2;
790
791                 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
792                 dev_kfree_skb(skb);
793                 skb = skb2;
794         }
795         if(skb != NULL) skb_put(skb, extra);
796         return(skb);
797 }
798
799 void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
800                                         void *),
801                     void *arg)
802 {
803         struct net_device *dev = d;
804         struct in_device *ip = dev->ip_ptr;
805         struct in_ifaddr *in;
806         unsigned char address[4], netmask[4];
807
808         if(ip == NULL) return;
809         in = ip->ifa_list;
810         while(in != NULL){
811                 memcpy(address, &in->ifa_address, sizeof(address));
812                 memcpy(netmask, &in->ifa_mask, sizeof(netmask));
813                 (*cb)(address, netmask, arg);
814                 in = in->ifa_next;
815         }
816 }
817
818 int dev_netmask(void *d, void *m)
819 {
820         struct net_device *dev = d;
821         struct in_device *ip = dev->ip_ptr;
822         struct in_ifaddr *in;
823         __be32 *mask_out = m;
824
825         if(ip == NULL)
826                 return(1);
827
828         in = ip->ifa_list;
829         if(in == NULL)
830                 return(1);
831
832         *mask_out = in->ifa_mask;
833         return(0);
834 }
835
836 void *get_output_buffer(int *len_out)
837 {
838         void *ret;
839
840         ret = (void *) __get_free_pages(GFP_KERNEL, 0);
841         if(ret) *len_out = PAGE_SIZE;
842         else *len_out = 0;
843         return ret;
844 }
845
846 void free_output_buffer(void *buffer)
847 {
848         free_pages((unsigned long) buffer, 0);
849 }
850
851 int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
852                      char **gate_addr)
853 {
854         char *remain;
855
856         remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
857         if(remain != NULL){
858                 printk("tap_setup_common - Extra garbage on specification : "
859                        "'%s'\n", remain);
860                 return(1);
861         }
862
863         return(0);
864 }
865
866 unsigned short eth_protocol(struct sk_buff *skb)
867 {
868         return(eth_type_trans(skb, skb->dev));
869 }