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