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