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