- update to 2.6.1-rc2 -- first cut.
[linux-flexiantxendom0-3.2.10.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                 return 1;
363         lp = dev->priv;
364
365         INIT_LIST_HEAD(&lp->list);
366         spin_lock_init(&lp->lock);
367         lp->dev = dev;
368         lp->fd = -1;
369         lp->mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0 };
370         lp->have_mac = device->have_mac;
371         lp->protocol = transport->kern->protocol;
372         lp->open = transport->user->open;
373         lp->close = transport->user->close;
374         lp->remove = transport->user->remove;
375         lp->read = transport->kern->read;
376         lp->write = transport->kern->write;
377         lp->add_address = transport->user->add_address;
378         lp->delete_address = transport->user->delete_address;
379         lp->set_mtu = transport->user->set_mtu;
380
381         init_timer(&lp->tl);
382         lp->tl.function = uml_net_user_timer_expire;
383         if (lp->have_mac)
384                 memcpy(lp->mac, device->mac, sizeof(lp->mac));
385
386         if (transport->user->init) 
387                 (*transport->user->init)(&lp->user, dev);
388
389         if (device->have_mac)
390                 set_ether_mac(dev, device->mac);
391         return(0);
392 }
393
394 static struct uml_net *find_device(int n)
395 {
396         struct uml_net *device;
397         struct list_head *ele;
398
399         spin_lock(&devices_lock);
400         list_for_each(ele, &devices){
401                 device = list_entry(ele, struct uml_net, list);
402                 if(device->index == n)
403                         goto out;
404         }
405         device = NULL;
406  out:
407         spin_unlock(&devices_lock);
408         return(device);
409 }
410
411 static int eth_parse(char *str, int *index_out, char **str_out)
412 {
413         char *end;
414         int n;
415
416         n = simple_strtoul(str, &end, 0);
417         if(end == str){
418                 printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
419                 return(1);
420         }
421         if(n < 0){
422                 printk(KERN_ERR "eth_setup: device %d is negative\n", n);
423                 return(1);
424         }
425         str = end;
426         if(*str != '='){
427                 printk(KERN_ERR 
428                        "eth_setup: expected '=' after device number\n");
429                 return(1);
430         }
431         str++;
432         if(find_device(n)){
433                 printk(KERN_ERR "eth_setup: Device %d already configured\n",
434                        n);
435                 return(1);
436         }
437         if(index_out) *index_out = n;
438         *str_out = str;
439         return(0);
440 }
441
442 struct eth_init {
443         struct list_head list;
444         char *init;
445         int index;
446 };
447
448 /* Filled in at boot time.  Will need locking if the transports become
449  * modular.
450  */
451 struct list_head transports = LIST_HEAD_INIT(transports);
452
453 /* Filled in during early boot */
454 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
455
456 static int check_transport(struct transport *transport, char *eth, int n,
457                            void **init_out, char **mac_out)
458 {
459         int len;
460
461         len = strlen(transport->name);
462         if(strncmp(eth, transport->name, len))
463                 return(0);
464
465         eth += len;
466         if(*eth == ',')
467                 eth++;
468         else if(*eth != '\0')
469                 return(0);
470
471         *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
472         if(*init_out == NULL)
473                 return(1);
474
475         if(!transport->setup(eth, mac_out, *init_out)){
476                 kfree(*init_out);
477                 *init_out = NULL;
478         }
479         return(1);
480 }
481
482 void register_transport(struct transport *new)
483 {
484         struct list_head *ele, *next;
485         struct eth_init *eth;
486         void *init;
487         char *mac = NULL;
488         int match;
489
490         list_add(&new->list, &transports);
491
492         list_for_each_safe(ele, next, &eth_cmd_line){
493                 eth = list_entry(ele, struct eth_init, list);
494                 match = check_transport(new, eth->init, eth->index, &init,
495                                         &mac);
496                 if(!match)
497                         continue;
498                 else if(init != NULL){
499                         eth_configure(eth->index, init, mac, new);
500                         kfree(init);
501                 }
502                 list_del(&eth->list);
503         }
504 }
505
506 static int eth_setup_common(char *str, int index)
507 {
508         struct list_head *ele;
509         struct transport *transport;
510         void *init;
511         char *mac = NULL;
512
513         list_for_each(ele, &transports){
514                 transport = list_entry(ele, struct transport, list);
515                 if(!check_transport(transport, str, index, &init, &mac))
516                         continue;
517                 if(init != NULL){
518                         eth_configure(index, init, mac, transport);
519                         kfree(init);
520                 }
521                 return(1);
522         }
523         return(0);
524 }
525
526 static int eth_setup(char *str)
527 {
528         struct eth_init *new;
529         int n, err;
530
531         err = eth_parse(str, &n, &str);
532         if(err) return(1);
533
534         new = alloc_bootmem(sizeof(new));
535         if (new == NULL){
536                 printk("eth_init : alloc_bootmem failed\n");
537                 return(1);
538         }
539
540         INIT_LIST_HEAD(&new->list);
541         new->index = n;
542         new->init = str;
543
544         list_add_tail(&new->list, &eth_cmd_line);
545         return(1);
546 }
547
548 __setup("eth", eth_setup);
549 __uml_help(eth_setup,
550 "eth[0-9]+=<transport>,<options>\n"
551 "    Configure a network device.\n\n"
552 );
553
554 static int eth_init(void)
555 {
556         struct list_head *ele, *next;
557         struct eth_init *eth;
558
559         list_for_each_safe(ele, next, &eth_cmd_line){
560                 eth = list_entry(ele, struct eth_init, list);
561
562                 if(eth_setup_common(eth->init, eth->index))
563                         list_del(&eth->list);
564         }
565         
566         return(1);
567 }
568
569 __initcall(eth_init);
570
571 static int net_config(char *str)
572 {
573         int n, err;
574
575         err = eth_parse(str, &n, &str);
576         if(err) return(err);
577
578         str = uml_strdup(str);
579         if(str == NULL){
580                 printk(KERN_ERR "net_config failed to strdup string\n");
581                 return(-1);
582         }
583         err = !eth_setup_common(str, n);
584         if(err) 
585                 kfree(str);
586         return(err);
587 }
588
589 static int net_remove(char *str)
590 {
591         struct uml_net *device;
592         struct net_device *dev;
593         struct uml_net_private *lp;
594         char *end;
595         int n;
596
597         n = simple_strtoul(str, &end, 0);
598         if((*end != '\0') || (end == str))
599                 return(-1);
600
601         device = find_device(n);
602         if(device == NULL)
603                 return(0);
604
605         dev = device->dev;
606         lp = dev->priv;
607         if(lp->fd > 0) return(-1);
608         if(lp->remove != NULL) (*lp->remove)(&lp->user);
609         unregister_netdev(dev);
610
611         list_del(&device->list);
612         free_netdev(device);
613         return(0);
614 }
615
616 static struct mc_device net_mc = {
617         .name           = "eth",
618         .config         = net_config,
619         .get_config     = NULL,
620         .remove         = net_remove,
621 };
622
623 static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
624                               void *ptr)
625 {
626         struct in_ifaddr *ifa = ptr;
627         u32 addr = ifa->ifa_address;
628         u32 netmask = ifa->ifa_mask;
629         struct net_device *dev = ifa->ifa_dev->dev;
630         struct uml_net_private *lp;
631         void (*proc)(unsigned char *, unsigned char *, void *);
632         unsigned char addr_buf[4], netmask_buf[4];
633
634         if(dev->open != uml_net_open) return(NOTIFY_DONE);
635
636         lp = dev->priv;
637
638         proc = NULL;
639         switch (event){
640         case NETDEV_UP:
641                 proc = lp->add_address;
642                 break;
643         case NETDEV_DOWN:
644                 proc = lp->delete_address;
645                 break;
646         }
647         if(proc != NULL){
648                 addr_buf[0] = addr & 0xff;
649                 addr_buf[1] = (addr >> 8) & 0xff;
650                 addr_buf[2] = (addr >> 16) & 0xff;
651                 addr_buf[3] = addr >> 24;
652                 netmask_buf[0] = netmask & 0xff;
653                 netmask_buf[1] = (netmask >> 8) & 0xff;
654                 netmask_buf[2] = (netmask >> 16) & 0xff;
655                 netmask_buf[3] = netmask >> 24;
656                 (*proc)(addr_buf, netmask_buf, &lp->user);
657         }
658         return(NOTIFY_DONE);
659 }
660
661 struct notifier_block uml_inetaddr_notifier = {
662         .notifier_call          = uml_inetaddr_event,
663 };
664
665 static int uml_net_init(void)
666 {
667         struct list_head *ele;
668         struct uml_net_private *lp;     
669         struct in_device *ip;
670         struct in_ifaddr *in;
671
672         mconsole_register_dev(&net_mc);
673         register_inetaddr_notifier(&uml_inetaddr_notifier);
674
675         /* Devices may have been opened already, so the uml_inetaddr_notifier
676          * didn't get a chance to run for them.  This fakes it so that
677          * addresses which have already been set up get handled properly.
678          */
679         list_for_each(ele, &opened){
680                 lp = list_entry(ele, struct uml_net_private, list);
681                 ip = lp->dev->ip_ptr;
682                 if(ip == NULL) continue;
683                 in = ip->ifa_list;
684                 while(in != NULL){
685                         uml_inetaddr_event(NULL, NETDEV_UP, in);
686                         in = in->ifa_next;
687                 }
688         }       
689
690         return(0);
691 }
692
693 __initcall(uml_net_init);
694
695 static void close_devices(void)
696 {
697         struct list_head *ele;
698         struct uml_net_private *lp;     
699
700         list_for_each(ele, &opened){
701                 lp = list_entry(ele, struct uml_net_private, list);
702                 if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
703                 if(lp->remove != NULL) (*lp->remove)(&lp->user);
704         }
705 }
706
707 __uml_exitcall(close_devices);
708
709 int setup_etheraddr(char *str, unsigned char *addr)
710 {
711         char *end;
712         int i;
713
714         if(str == NULL)
715                 return(0);
716         for(i=0;i<6;i++){
717                 addr[i] = simple_strtoul(str, &end, 16);
718                 if((end == str) ||
719                    ((*end != ':') && (*end != ',') && (*end != '\0'))){
720                         printk(KERN_ERR 
721                                "setup_etheraddr: failed to parse '%s' "
722                                "as an ethernet address\n", str);
723                         return(0);
724                 }
725                 str = end + 1;
726         }
727         if(addr[0] & 1){
728                 printk(KERN_ERR 
729                        "Attempt to assign a broadcast ethernet address to a "
730                        "device disallowed\n");
731                 return(0);
732         }
733         return(1);
734 }
735
736 void dev_ip_addr(void *d, char *buf, char *bin_buf)
737 {
738         struct net_device *dev = d;
739         struct in_device *ip = dev->ip_ptr;
740         struct in_ifaddr *in;
741         u32 addr;
742
743         if((ip == NULL) || ((in = ip->ifa_list) == NULL)){
744                 printk(KERN_WARNING "dev_ip_addr - device not assigned an "
745                        "IP address\n");
746                 return;
747         }
748         addr = in->ifa_address;
749         sprintf(buf, "%d.%d.%d.%d", addr & 0xff, (addr >> 8) & 0xff, 
750                 (addr >> 16) & 0xff, addr >> 24);
751         if(bin_buf){
752                 bin_buf[0] = addr & 0xff;
753                 bin_buf[1] = (addr >> 8) & 0xff;
754                 bin_buf[2] = (addr >> 16) & 0xff;
755                 bin_buf[3] = addr >> 24;
756         }
757 }
758
759 void set_ether_mac(void *d, unsigned char *addr)
760 {
761         struct net_device *dev = d;
762
763         memcpy(dev->dev_addr, addr, ETH_ALEN);  
764 }
765
766 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
767 {
768         if((skb != NULL) && (skb_tailroom(skb) < extra)){
769                 struct sk_buff *skb2;
770
771                 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
772                 dev_kfree_skb(skb);
773                 skb = skb2;
774         }
775         if(skb != NULL) skb_put(skb, extra);
776         return(skb);
777 }
778
779 void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *, 
780                                         void *), 
781                     void *arg)
782 {
783         struct net_device *dev = d;
784         struct in_device *ip = dev->ip_ptr;
785         struct in_ifaddr *in;
786         unsigned char address[4], netmask[4];
787
788         if(ip == NULL) return;
789         in = ip->ifa_list;
790         while(in != NULL){
791                 address[0] = in->ifa_address & 0xff;
792                 address[1] = (in->ifa_address >> 8) & 0xff;
793                 address[2] = (in->ifa_address >> 16) & 0xff;
794                 address[3] = in->ifa_address >> 24;
795                 netmask[0] = in->ifa_mask & 0xff;
796                 netmask[1] = (in->ifa_mask >> 8) & 0xff;
797                 netmask[2] = (in->ifa_mask >> 16) & 0xff;
798                 netmask[3] = in->ifa_mask >> 24;
799                 (*cb)(address, netmask, arg);
800                 in = in->ifa_next;
801         }
802 }
803
804 int dev_netmask(void *d, void *m)
805 {
806         struct net_device *dev = d;
807         struct in_device *ip = dev->ip_ptr;
808         struct in_ifaddr *in;
809         __u32 *mask_out = m;
810
811         if(ip == NULL) 
812                 return(1);
813
814         in = ip->ifa_list;
815         if(in == NULL) 
816                 return(1);
817
818         *mask_out = in->ifa_mask;
819         return(0);
820 }
821
822 void *get_output_buffer(int *len_out)
823 {
824         void *ret;
825
826         ret = (void *) __get_free_pages(GFP_KERNEL, 0);
827         if(ret) *len_out = PAGE_SIZE;
828         else *len_out = 0;
829         return(ret);
830 }
831
832 void free_output_buffer(void *buffer)
833 {
834         free_pages((unsigned long) buffer, 0);
835 }
836
837 int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out, 
838                      char **gate_addr)
839 {
840         char *remain;
841
842         remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
843         if(remain != NULL){
844                 printk("tap_setup_common - Extra garbage on specification : "
845                        "'%s'\n", remain);
846                 return(1);
847         }
848
849         return(0);
850 }
851
852 unsigned short eth_protocol(struct sk_buff *skb)
853 {
854         return(eth_type_trans(skb, skb->dev));
855 }
856
857 /*
858  * Overrides for Emacs so that we follow Linus's tabbing style.
859  * Emacs will notice this stuff at the end of the file and automatically
860  * adjust the settings for this buffer only.  This must remain at the end
861  * of the file.
862  * ---------------------------------------------------------------------------
863  * Local variables:
864  * c-file-style: "linux"
865  * End:
866  */