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