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