patch-2_6_7-vs1_9_1_12
[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  out:
128         spin_unlock(&lp->lock);
129         return(err);
130 }
131
132 static int uml_net_close(struct net_device *dev)
133 {
134         struct uml_net_private *lp = dev->priv;
135         
136         netif_stop_queue(dev);
137         spin_lock(&lp->lock);
138
139         free_irq(dev->irq, dev);
140         if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
141         lp->fd = -1;
142         spin_lock(&opened_lock);
143         list_del(&lp->list);
144         spin_unlock(&opened_lock);
145
146         spin_unlock(&lp->lock);
147         return 0;
148 }
149
150 static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
151 {
152         struct uml_net_private *lp = dev->priv;
153         unsigned long flags;
154         int len;
155
156         netif_stop_queue(dev);
157
158         spin_lock_irqsave(&lp->lock, flags);
159
160         len = (*lp->write)(lp->fd, &skb, lp);
161
162         if(len == skb->len) {
163                 lp->stats.tx_packets++;
164                 lp->stats.tx_bytes += skb->len;
165                 dev->trans_start = jiffies;
166                 netif_start_queue(dev);
167
168                 /* this is normally done in the interrupt when tx finishes */
169                 netif_wake_queue(dev);
170         } 
171         else if(len == 0){
172                 netif_start_queue(dev);
173                 lp->stats.tx_dropped++;
174         }
175         else {
176                 netif_start_queue(dev);
177                 printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
178         }
179
180         spin_unlock_irqrestore(&lp->lock, flags);
181
182         dev_kfree_skb(skb);
183
184         return 0;
185 }
186
187 static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
188 {
189         struct uml_net_private *lp = dev->priv;
190         return &lp->stats;
191 }
192
193 static void uml_net_set_multicast_list(struct net_device *dev)
194 {
195         if (dev->flags & IFF_PROMISC) return;
196         else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
197         else dev->flags &= ~IFF_ALLMULTI;
198 }
199
200 static void uml_net_tx_timeout(struct net_device *dev)
201 {
202         dev->trans_start = jiffies;
203         netif_wake_queue(dev);
204 }
205
206 static int uml_net_set_mac(struct net_device *dev, void *addr)
207 {
208         struct uml_net_private *lp = dev->priv;
209         struct sockaddr *hwaddr = addr;
210
211         spin_lock(&lp->lock);
212         memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
213         spin_unlock(&lp->lock);
214
215         return(0);
216 }
217
218 static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
219 {
220         struct uml_net_private *lp = dev->priv;
221         int err = 0;
222
223         spin_lock(&lp->lock);
224
225         new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
226         if(new_mtu < 0){
227                 err = new_mtu;
228                 goto out;
229         }
230
231         dev->mtu = new_mtu;
232
233  out:
234         spin_unlock(&lp->lock);
235         return err;
236 }
237
238 static int uml_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
239 {
240         return(-EINVAL);
241 }
242
243 void uml_net_user_timer_expire(unsigned long _conn)
244 {
245 #ifdef undef
246         struct connection *conn = (struct connection *)_conn;
247
248         dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
249         do_connect(conn);
250 #endif
251 }
252
253 /*
254  * default do nothing hard header packet routines for struct net_device init.
255  * real ethernet transports will overwrite with real routines.
256  */
257 static int uml_net_hard_header(struct sk_buff *skb, struct net_device *dev,
258                  unsigned short type, void *daddr, void *saddr, unsigned len)
259 {
260         return(0); /* no change */
261 }
262
263 static int uml_net_rebuild_header(struct sk_buff *skb)
264 {
265         return(0); /* ignore */ 
266 }
267
268 static int uml_net_header_cache(struct neighbour *neigh, struct hh_cache *hh)
269 {
270         return(-1); /* fail */
271 }
272
273 static void uml_net_header_cache_update(struct hh_cache *hh,
274                  struct net_device *dev, unsigned char * haddr)
275 {
276         /* ignore */
277 }
278
279 static int uml_net_header_parse(struct sk_buff *skb, unsigned char *haddr)
280 {
281         return(0); /* nothing */
282 }
283
284 static spinlock_t devices_lock = SPIN_LOCK_UNLOCKED;
285 static struct list_head devices = LIST_HEAD_INIT(devices);
286
287 static int eth_configure(int n, void *init, char *mac,
288                          struct transport *transport)
289 {
290         struct uml_net *device;
291         struct net_device *dev;
292         struct uml_net_private *lp;
293         int err, size;
294
295         size = transport->private_size + sizeof(struct uml_net_private) + 
296                 sizeof(((struct uml_net_private *) 0)->user);
297
298         device = kmalloc(sizeof(*device), GFP_KERNEL);
299         if (device == NULL) {
300                 printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
301                 return(1);
302         }
303
304         memset(device, 0, sizeof(*device));
305         INIT_LIST_HEAD(&device->list);
306         device->index = n;
307
308         spin_lock(&devices_lock);
309         list_add(&device->list, &devices);
310         spin_unlock(&devices_lock);
311
312         if (setup_etheraddr(mac, device->mac))
313                 device->have_mac = 1;
314
315         printk(KERN_INFO "Netdevice %d ", n);
316         if (device->have_mac)
317                 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
318                        device->mac[0], device->mac[1],
319                        device->mac[2], device->mac[3],
320                        device->mac[4], device->mac[5]);
321         printk(": ");
322         dev = alloc_etherdev(size);
323         if (dev == NULL) {
324                 printk(KERN_ERR "eth_configure: failed to allocate device\n");
325                 return 1;
326         }
327
328         /* If this name ends up conflicting with an existing registered
329          * netdevice, that is OK, register_netdev{,ice}() will notice this
330          * and fail.
331          */
332         snprintf(dev->name, sizeof(dev->name), "eth%d", n);
333         device->dev = dev;
334
335         dev->hard_header = uml_net_hard_header;
336         dev->rebuild_header = uml_net_rebuild_header;
337         dev->hard_header_cache = uml_net_header_cache;
338         dev->header_cache_update= uml_net_header_cache_update;
339         dev->hard_header_parse = uml_net_header_parse;
340
341         (*transport->kern->init)(dev, init);
342
343         dev->mtu = transport->user->max_packet;
344         dev->open = uml_net_open;
345         dev->hard_start_xmit = uml_net_start_xmit;
346         dev->stop = uml_net_close;
347         dev->get_stats = uml_net_get_stats;
348         dev->set_multicast_list = uml_net_set_multicast_list;
349         dev->tx_timeout = uml_net_tx_timeout;
350         dev->set_mac_address = uml_net_set_mac;
351         dev->change_mtu = uml_net_change_mtu;
352         dev->do_ioctl = uml_net_ioctl;
353         dev->watchdog_timeo = (HZ >> 1);
354         dev->irq = UM_ETH_IRQ;
355
356         rtnl_lock();
357         err = register_netdevice(dev);
358         rtnl_unlock();
359         if (err) {
360                 device->dev = NULL;
361                 /* XXX: should we call ->remove() here? */
362                 free_netdev(dev);
363                 return 1;
364         }
365         lp = dev->priv;
366
367         INIT_LIST_HEAD(&lp->list);
368         spin_lock_init(&lp->lock);
369         lp->dev = dev;
370         lp->fd = -1;
371         lp->mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0 };
372         lp->have_mac = device->have_mac;
373         lp->protocol = transport->kern->protocol;
374         lp->open = transport->user->open;
375         lp->close = transport->user->close;
376         lp->remove = transport->user->remove;
377         lp->read = transport->kern->read;
378         lp->write = transport->kern->write;
379         lp->add_address = transport->user->add_address;
380         lp->delete_address = transport->user->delete_address;
381         lp->set_mtu = transport->user->set_mtu;
382
383         init_timer(&lp->tl);
384         lp->tl.function = uml_net_user_timer_expire;
385         if (lp->have_mac)
386                 memcpy(lp->mac, device->mac, sizeof(lp->mac));
387
388         if (transport->user->init) 
389                 (*transport->user->init)(&lp->user, dev);
390
391         if (device->have_mac)
392                 set_ether_mac(dev, device->mac);
393         return(0);
394 }
395
396 static struct uml_net *find_device(int n)
397 {
398         struct uml_net *device;
399         struct list_head *ele;
400
401         spin_lock(&devices_lock);
402         list_for_each(ele, &devices){
403                 device = list_entry(ele, struct uml_net, list);
404                 if(device->index == n)
405                         goto out;
406         }
407         device = NULL;
408  out:
409         spin_unlock(&devices_lock);
410         return(device);
411 }
412
413 static int eth_parse(char *str, int *index_out, char **str_out)
414 {
415         char *end;
416         int n;
417
418         n = simple_strtoul(str, &end, 0);
419         if(end == str){
420                 printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
421                 return(1);
422         }
423         if(n < 0){
424                 printk(KERN_ERR "eth_setup: device %d is negative\n", n);
425                 return(1);
426         }
427         str = end;
428         if(*str != '='){
429                 printk(KERN_ERR 
430                        "eth_setup: expected '=' after device number\n");
431                 return(1);
432         }
433         str++;
434         if(find_device(n)){
435                 printk(KERN_ERR "eth_setup: Device %d already configured\n",
436                        n);
437                 return(1);
438         }
439         if(index_out) *index_out = n;
440         *str_out = str;
441         return(0);
442 }
443
444 struct eth_init {
445         struct list_head list;
446         char *init;
447         int index;
448 };
449
450 /* Filled in at boot time.  Will need locking if the transports become
451  * modular.
452  */
453 struct list_head transports = LIST_HEAD_INIT(transports);
454
455 /* Filled in during early boot */
456 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
457
458 static int check_transport(struct transport *transport, char *eth, int n,
459                            void **init_out, char **mac_out)
460 {
461         int len;
462
463         len = strlen(transport->name);
464         if(strncmp(eth, transport->name, len))
465                 return(0);
466
467         eth += len;
468         if(*eth == ',')
469                 eth++;
470         else if(*eth != '\0')
471                 return(0);
472
473         *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
474         if(*init_out == NULL)
475                 return(1);
476
477         if(!transport->setup(eth, mac_out, *init_out)){
478                 kfree(*init_out);
479                 *init_out = NULL;
480         }
481         return(1);
482 }
483
484 void register_transport(struct transport *new)
485 {
486         struct list_head *ele, *next;
487         struct eth_init *eth;
488         void *init;
489         char *mac = NULL;
490         int match;
491
492         list_add(&new->list, &transports);
493
494         list_for_each_safe(ele, next, &eth_cmd_line){
495                 eth = list_entry(ele, struct eth_init, list);
496                 match = check_transport(new, eth->init, eth->index, &init,
497                                         &mac);
498                 if(!match)
499                         continue;
500                 else if(init != NULL){
501                         eth_configure(eth->index, init, mac, new);
502                         kfree(init);
503                 }
504                 list_del(&eth->list);
505         }
506 }
507
508 static int eth_setup_common(char *str, int index)
509 {
510         struct list_head *ele;
511         struct transport *transport;
512         void *init;
513         char *mac = NULL;
514
515         list_for_each(ele, &transports){
516                 transport = list_entry(ele, struct transport, list);
517                 if(!check_transport(transport, str, index, &init, &mac))
518                         continue;
519                 if(init != NULL){
520                         eth_configure(index, init, mac, transport);
521                         kfree(init);
522                 }
523                 return(1);
524         }
525         return(0);
526 }
527
528 static int eth_setup(char *str)
529 {
530         struct eth_init *new;
531         int n, err;
532
533         err = eth_parse(str, &n, &str);
534         if(err) return(1);
535
536         new = alloc_bootmem(sizeof(new));
537         if (new == NULL){
538                 printk("eth_init : alloc_bootmem failed\n");
539                 return(1);
540         }
541
542         INIT_LIST_HEAD(&new->list);
543         new->index = n;
544         new->init = str;
545
546         list_add_tail(&new->list, &eth_cmd_line);
547         return(1);
548 }
549
550 __setup("eth", eth_setup);
551 __uml_help(eth_setup,
552 "eth[0-9]+=<transport>,<options>\n"
553 "    Configure a network device.\n\n"
554 );
555
556 static int eth_init(void)
557 {
558         struct list_head *ele, *next;
559         struct eth_init *eth;
560
561         list_for_each_safe(ele, next, &eth_cmd_line){
562                 eth = list_entry(ele, struct eth_init, list);
563
564                 if(eth_setup_common(eth->init, eth->index))
565                         list_del(&eth->list);
566         }
567         
568         return(1);
569 }
570
571 __initcall(eth_init);
572
573 static int net_config(char *str)
574 {
575         int n, err;
576
577         err = eth_parse(str, &n, &str);
578         if(err) return(err);
579
580         str = uml_strdup(str);
581         if(str == NULL){
582                 printk(KERN_ERR "net_config failed to strdup string\n");
583                 return(-1);
584         }
585         err = !eth_setup_common(str, n);
586         if(err) 
587                 kfree(str);
588         return(err);
589 }
590
591 static int net_remove(char *str)
592 {
593         struct uml_net *device;
594         struct net_device *dev;
595         struct uml_net_private *lp;
596         char *end;
597         int n;
598
599         n = simple_strtoul(str, &end, 0);
600         if((*end != '\0') || (end == str))
601                 return(-1);
602
603         device = find_device(n);
604         if(device == NULL)
605                 return(0);
606
607         dev = device->dev;
608         lp = dev->priv;
609         if(lp->fd > 0) return(-1);
610         if(lp->remove != NULL) (*lp->remove)(&lp->user);
611         unregister_netdev(dev);
612
613         list_del(&device->list);
614         free_netdev(device);
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  */