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