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