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