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