tun: Fix races between tun_net_close and free_netdev.
[safe/jmp/linux-2.6] / drivers / net / tun.c
1 /*
2  *  TUN - Universal TUN/TAP device driver.
3  *  Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
16  */
17
18 /*
19  *  Changes:
20  *
21  *  Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
22  *    Add TUNSETLINK ioctl to set the link encapsulation
23  *
24  *  Mark Smith <markzzzsmith@yahoo.com.au>
25  *    Use random_ether_addr() for tap MAC address.
26  *
27  *  Harald Roelle <harald.roelle@ifi.lmu.de>  2004/04/20
28  *    Fixes in packet dropping, queue length setting and queue wakeup.
29  *    Increased default tx queue length.
30  *    Added ethtool API.
31  *    Minor cleanups
32  *
33  *  Daniel Podlejski <underley@underley.eu.org>
34  *    Modifications for 2.3.99-pre5 kernel.
35  */
36
37 #define DRV_NAME        "tun"
38 #define DRV_VERSION     "1.6"
39 #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
40 #define DRV_COPYRIGHT   "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
41
42 #include <linux/module.h>
43 #include <linux/errno.h>
44 #include <linux/kernel.h>
45 #include <linux/major.h>
46 #include <linux/slab.h>
47 #include <linux/smp_lock.h>
48 #include <linux/poll.h>
49 #include <linux/fcntl.h>
50 #include <linux/init.h>
51 #include <linux/skbuff.h>
52 #include <linux/netdevice.h>
53 #include <linux/etherdevice.h>
54 #include <linux/miscdevice.h>
55 #include <linux/ethtool.h>
56 #include <linux/rtnetlink.h>
57 #include <linux/if.h>
58 #include <linux/if_arp.h>
59 #include <linux/if_ether.h>
60 #include <linux/if_tun.h>
61 #include <linux/crc32.h>
62 #include <linux/nsproxy.h>
63 #include <linux/virtio_net.h>
64 #include <net/net_namespace.h>
65 #include <net/netns/generic.h>
66
67 #include <asm/system.h>
68 #include <asm/uaccess.h>
69
70 /* Uncomment to enable debugging */
71 /* #define TUN_DEBUG 1 */
72
73 #ifdef TUN_DEBUG
74 static int debug;
75
76 #define DBG  if(tun->debug)printk
77 #define DBG1 if(debug==2)printk
78 #else
79 #define DBG( a... )
80 #define DBG1( a... )
81 #endif
82
83 #define FLT_EXACT_COUNT 8
84 struct tap_filter {
85         unsigned int    count;    /* Number of addrs. Zero means disabled */
86         u32             mask[2];  /* Mask of the hashed addrs */
87         unsigned char   addr[FLT_EXACT_COUNT][ETH_ALEN];
88 };
89
90 struct tun_file {
91         atomic_t count;
92         struct tun_struct *tun;
93         struct net *net;
94         wait_queue_head_t       read_wait;
95 };
96
97 struct tun_struct {
98         struct tun_file         *tfile;
99         unsigned int            flags;
100         uid_t                   owner;
101         gid_t                   group;
102
103         struct sk_buff_head     readq;
104
105         struct net_device       *dev;
106         struct fasync_struct    *fasync;
107
108         struct tap_filter       txflt;
109
110 #ifdef TUN_DEBUG
111         int debug;
112 #endif
113 };
114
115 static int tun_attach(struct tun_struct *tun, struct file *file)
116 {
117         struct tun_file *tfile = file->private_data;
118         const struct cred *cred = current_cred();
119         int err;
120
121         ASSERT_RTNL();
122
123         /* Check permissions */
124         if (((tun->owner != -1 && cred->euid != tun->owner) ||
125              (tun->group != -1 && cred->egid != tun->group)) &&
126                 !capable(CAP_NET_ADMIN))
127                 return -EPERM;
128
129         netif_tx_lock_bh(tun->dev);
130
131         err = -EINVAL;
132         if (tfile->tun)
133                 goto out;
134
135         err = -EBUSY;
136         if (tun->tfile)
137                 goto out;
138
139         err = 0;
140         tfile->tun = tun;
141         tun->tfile = tfile;
142         dev_hold(tun->dev);
143         atomic_inc(&tfile->count);
144
145 out:
146         netif_tx_unlock_bh(tun->dev);
147         return err;
148 }
149
150 static void __tun_detach(struct tun_struct *tun)
151 {
152         struct tun_file *tfile = tun->tfile;
153
154         /* Detach from net device */
155         netif_tx_lock_bh(tun->dev);
156         tfile->tun = NULL;
157         tun->tfile = NULL;
158         netif_tx_unlock_bh(tun->dev);
159
160         /* Drop read queue */
161         skb_queue_purge(&tun->readq);
162
163         /* Drop the extra count on the net device */
164         dev_put(tun->dev);
165 }
166
167 static void tun_detach(struct tun_struct *tun)
168 {
169         rtnl_lock();
170         __tun_detach(tun);
171         rtnl_unlock();
172 }
173
174 static struct tun_struct *__tun_get(struct tun_file *tfile)
175 {
176         struct tun_struct *tun = NULL;
177
178         if (atomic_inc_not_zero(&tfile->count))
179                 tun = tfile->tun;
180
181         return tun;
182 }
183
184 static struct tun_struct *tun_get(struct file *file)
185 {
186         return __tun_get(file->private_data);
187 }
188
189 static void tun_put(struct tun_struct *tun)
190 {
191         struct tun_file *tfile = tun->tfile;
192
193         if (atomic_dec_and_test(&tfile->count))
194                 tun_detach(tfile->tun);
195 }
196
197 /* TAP filterting */
198 static void addr_hash_set(u32 *mask, const u8 *addr)
199 {
200         int n = ether_crc(ETH_ALEN, addr) >> 26;
201         mask[n >> 5] |= (1 << (n & 31));
202 }
203
204 static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
205 {
206         int n = ether_crc(ETH_ALEN, addr) >> 26;
207         return mask[n >> 5] & (1 << (n & 31));
208 }
209
210 static int update_filter(struct tap_filter *filter, void __user *arg)
211 {
212         struct { u8 u[ETH_ALEN]; } *addr;
213         struct tun_filter uf;
214         int err, alen, n, nexact;
215
216         if (copy_from_user(&uf, arg, sizeof(uf)))
217                 return -EFAULT;
218
219         if (!uf.count) {
220                 /* Disabled */
221                 filter->count = 0;
222                 return 0;
223         }
224
225         alen = ETH_ALEN * uf.count;
226         addr = kmalloc(alen, GFP_KERNEL);
227         if (!addr)
228                 return -ENOMEM;
229
230         if (copy_from_user(addr, arg + sizeof(uf), alen)) {
231                 err = -EFAULT;
232                 goto done;
233         }
234
235         /* The filter is updated without holding any locks. Which is
236          * perfectly safe. We disable it first and in the worst
237          * case we'll accept a few undesired packets. */
238         filter->count = 0;
239         wmb();
240
241         /* Use first set of addresses as an exact filter */
242         for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
243                 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
244
245         nexact = n;
246
247         /* The rest is hashed */
248         memset(filter->mask, 0, sizeof(filter->mask));
249         for (; n < uf.count; n++)
250                 addr_hash_set(filter->mask, addr[n].u);
251
252         /* For ALLMULTI just set the mask to all ones.
253          * This overrides the mask populated above. */
254         if ((uf.flags & TUN_FLT_ALLMULTI))
255                 memset(filter->mask, ~0, sizeof(filter->mask));
256
257         /* Now enable the filter */
258         wmb();
259         filter->count = nexact;
260
261         /* Return the number of exact filters */
262         err = nexact;
263
264 done:
265         kfree(addr);
266         return err;
267 }
268
269 /* Returns: 0 - drop, !=0 - accept */
270 static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
271 {
272         /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
273          * at this point. */
274         struct ethhdr *eh = (struct ethhdr *) skb->data;
275         int i;
276
277         /* Exact match */
278         for (i = 0; i < filter->count; i++)
279                 if (!compare_ether_addr(eh->h_dest, filter->addr[i]))
280                         return 1;
281
282         /* Inexact match (multicast only) */
283         if (is_multicast_ether_addr(eh->h_dest))
284                 return addr_hash_test(filter->mask, eh->h_dest);
285
286         return 0;
287 }
288
289 /*
290  * Checks whether the packet is accepted or not.
291  * Returns: 0 - drop, !=0 - accept
292  */
293 static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
294 {
295         if (!filter->count)
296                 return 1;
297
298         return run_filter(filter, skb);
299 }
300
301 /* Network device part of the driver */
302
303 static const struct ethtool_ops tun_ethtool_ops;
304
305 /* Net device detach from fd. */
306 static void tun_net_uninit(struct net_device *dev)
307 {
308         struct tun_struct *tun = netdev_priv(dev);
309         struct tun_file *tfile = tun->tfile;
310
311         /* Inform the methods they need to stop using the dev.
312          */
313         if (tfile) {
314                 wake_up_all(&tfile->read_wait);
315                 if (atomic_dec_and_test(&tfile->count))
316                         __tun_detach(tun);
317         }
318 }
319
320 /* Net device open. */
321 static int tun_net_open(struct net_device *dev)
322 {
323         netif_start_queue(dev);
324         return 0;
325 }
326
327 /* Net device close. */
328 static int tun_net_close(struct net_device *dev)
329 {
330         netif_stop_queue(dev);
331         return 0;
332 }
333
334 /* Net device start xmit */
335 static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
336 {
337         struct tun_struct *tun = netdev_priv(dev);
338
339         DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
340
341         /* Drop packet if interface is not attached */
342         if (!tun->tfile)
343                 goto drop;
344
345         /* Drop if the filter does not like it.
346          * This is a noop if the filter is disabled.
347          * Filter can be enabled only for the TAP devices. */
348         if (!check_filter(&tun->txflt, skb))
349                 goto drop;
350
351         if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
352                 if (!(tun->flags & TUN_ONE_QUEUE)) {
353                         /* Normal queueing mode. */
354                         /* Packet scheduler handles dropping of further packets. */
355                         netif_stop_queue(dev);
356
357                         /* We won't see all dropped packets individually, so overrun
358                          * error is more appropriate. */
359                         dev->stats.tx_fifo_errors++;
360                 } else {
361                         /* Single queue mode.
362                          * Driver handles dropping of all packets itself. */
363                         goto drop;
364                 }
365         }
366
367         /* Enqueue packet */
368         skb_queue_tail(&tun->readq, skb);
369         dev->trans_start = jiffies;
370
371         /* Notify and wake up reader process */
372         if (tun->flags & TUN_FASYNC)
373                 kill_fasync(&tun->fasync, SIGIO, POLL_IN);
374         wake_up_interruptible(&tun->tfile->read_wait);
375         return 0;
376
377 drop:
378         dev->stats.tx_dropped++;
379         kfree_skb(skb);
380         return 0;
381 }
382
383 static void tun_net_mclist(struct net_device *dev)
384 {
385         /*
386          * This callback is supposed to deal with mc filter in
387          * _rx_ path and has nothing to do with the _tx_ path.
388          * In rx path we always accept everything userspace gives us.
389          */
390         return;
391 }
392
393 #define MIN_MTU 68
394 #define MAX_MTU 65535
395
396 static int
397 tun_net_change_mtu(struct net_device *dev, int new_mtu)
398 {
399         if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
400                 return -EINVAL;
401         dev->mtu = new_mtu;
402         return 0;
403 }
404
405 static const struct net_device_ops tun_netdev_ops = {
406         .ndo_uninit             = tun_net_uninit,
407         .ndo_open               = tun_net_open,
408         .ndo_stop               = tun_net_close,
409         .ndo_start_xmit         = tun_net_xmit,
410         .ndo_change_mtu         = tun_net_change_mtu,
411 };
412
413 static const struct net_device_ops tap_netdev_ops = {
414         .ndo_uninit             = tun_net_uninit,
415         .ndo_open               = tun_net_open,
416         .ndo_stop               = tun_net_close,
417         .ndo_start_xmit         = tun_net_xmit,
418         .ndo_change_mtu         = tun_net_change_mtu,
419         .ndo_set_multicast_list = tun_net_mclist,
420         .ndo_set_mac_address    = eth_mac_addr,
421         .ndo_validate_addr      = eth_validate_addr,
422 };
423
424 /* Initialize net device. */
425 static void tun_net_init(struct net_device *dev)
426 {
427         struct tun_struct *tun = netdev_priv(dev);
428
429         switch (tun->flags & TUN_TYPE_MASK) {
430         case TUN_TUN_DEV:
431                 dev->netdev_ops = &tun_netdev_ops;
432
433                 /* Point-to-Point TUN Device */
434                 dev->hard_header_len = 0;
435                 dev->addr_len = 0;
436                 dev->mtu = 1500;
437
438                 /* Zero header length */
439                 dev->type = ARPHRD_NONE;
440                 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
441                 dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
442                 break;
443
444         case TUN_TAP_DEV:
445                 dev->netdev_ops = &tap_netdev_ops;
446                 /* Ethernet TAP Device */
447                 ether_setup(dev);
448
449                 random_ether_addr(dev->dev_addr);
450
451                 dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
452                 break;
453         }
454 }
455
456 /* Character device part */
457
458 /* Poll */
459 static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
460 {
461         struct tun_file *tfile = file->private_data;
462         struct tun_struct *tun = __tun_get(tfile);
463         unsigned int mask = POLLOUT | POLLWRNORM;
464
465         if (!tun)
466                 return POLLERR;
467
468         DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
469
470         poll_wait(file, &tfile->read_wait, wait);
471
472         if (!skb_queue_empty(&tun->readq))
473                 mask |= POLLIN | POLLRDNORM;
474
475         if (tun->dev->reg_state != NETREG_REGISTERED)
476                 mask = POLLERR;
477
478         tun_put(tun);
479         return mask;
480 }
481
482 /* prepad is the amount to reserve at front.  len is length after that.
483  * linear is a hint as to how much to copy (usually headers). */
484 static struct sk_buff *tun_alloc_skb(size_t prepad, size_t len, size_t linear,
485                                      gfp_t gfp)
486 {
487         struct sk_buff *skb;
488         unsigned int i;
489
490         skb = alloc_skb(prepad + len, gfp|__GFP_NOWARN);
491         if (skb) {
492                 skb_reserve(skb, prepad);
493                 skb_put(skb, len);
494                 return skb;
495         }
496
497         /* Under a page?  Don't bother with paged skb. */
498         if (prepad + len < PAGE_SIZE)
499                 return NULL;
500
501         /* Start with a normal skb, and add pages. */
502         skb = alloc_skb(prepad + linear, gfp);
503         if (!skb)
504                 return NULL;
505
506         skb_reserve(skb, prepad);
507         skb_put(skb, linear);
508
509         len -= linear;
510
511         for (i = 0; i < MAX_SKB_FRAGS; i++) {
512                 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
513
514                 f->page = alloc_page(gfp|__GFP_ZERO);
515                 if (!f->page)
516                         break;
517
518                 f->page_offset = 0;
519                 f->size = PAGE_SIZE;
520
521                 skb->data_len += PAGE_SIZE;
522                 skb->len += PAGE_SIZE;
523                 skb->truesize += PAGE_SIZE;
524                 skb_shinfo(skb)->nr_frags++;
525
526                 if (len < PAGE_SIZE) {
527                         len = 0;
528                         break;
529                 }
530                 len -= PAGE_SIZE;
531         }
532
533         /* Too large, or alloc fail? */
534         if (unlikely(len)) {
535                 kfree_skb(skb);
536                 skb = NULL;
537         }
538
539         return skb;
540 }
541
542 /* Get packet from user space buffer */
543 static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
544 {
545         struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
546         struct sk_buff *skb;
547         size_t len = count, align = 0;
548         struct virtio_net_hdr gso = { 0 };
549
550         if (!(tun->flags & TUN_NO_PI)) {
551                 if ((len -= sizeof(pi)) > count)
552                         return -EINVAL;
553
554                 if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
555                         return -EFAULT;
556         }
557
558         if (tun->flags & TUN_VNET_HDR) {
559                 if ((len -= sizeof(gso)) > count)
560                         return -EINVAL;
561
562                 if (memcpy_fromiovec((void *)&gso, iv, sizeof(gso)))
563                         return -EFAULT;
564
565                 if (gso.hdr_len > len)
566                         return -EINVAL;
567         }
568
569         if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
570                 align = NET_IP_ALIGN;
571                 if (unlikely(len < ETH_HLEN))
572                         return -EINVAL;
573         }
574
575         if (!(skb = tun_alloc_skb(align, len, gso.hdr_len, GFP_KERNEL))) {
576                 tun->dev->stats.rx_dropped++;
577                 return -ENOMEM;
578         }
579
580         if (skb_copy_datagram_from_iovec(skb, 0, iv, len)) {
581                 tun->dev->stats.rx_dropped++;
582                 kfree_skb(skb);
583                 return -EFAULT;
584         }
585
586         if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
587                 if (!skb_partial_csum_set(skb, gso.csum_start,
588                                           gso.csum_offset)) {
589                         tun->dev->stats.rx_frame_errors++;
590                         kfree_skb(skb);
591                         return -EINVAL;
592                 }
593         } else if (tun->flags & TUN_NOCHECKSUM)
594                 skb->ip_summed = CHECKSUM_UNNECESSARY;
595
596         switch (tun->flags & TUN_TYPE_MASK) {
597         case TUN_TUN_DEV:
598                 if (tun->flags & TUN_NO_PI) {
599                         switch (skb->data[0] & 0xf0) {
600                         case 0x40:
601                                 pi.proto = htons(ETH_P_IP);
602                                 break;
603                         case 0x60:
604                                 pi.proto = htons(ETH_P_IPV6);
605                                 break;
606                         default:
607                                 tun->dev->stats.rx_dropped++;
608                                 kfree_skb(skb);
609                                 return -EINVAL;
610                         }
611                 }
612
613                 skb_reset_mac_header(skb);
614                 skb->protocol = pi.proto;
615                 skb->dev = tun->dev;
616                 break;
617         case TUN_TAP_DEV:
618                 skb->protocol = eth_type_trans(skb, tun->dev);
619                 break;
620         };
621
622         if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
623                 pr_debug("GSO!\n");
624                 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
625                 case VIRTIO_NET_HDR_GSO_TCPV4:
626                         skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
627                         break;
628                 case VIRTIO_NET_HDR_GSO_TCPV6:
629                         skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
630                         break;
631                 default:
632                         tun->dev->stats.rx_frame_errors++;
633                         kfree_skb(skb);
634                         return -EINVAL;
635                 }
636
637                 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
638                         skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
639
640                 skb_shinfo(skb)->gso_size = gso.gso_size;
641                 if (skb_shinfo(skb)->gso_size == 0) {
642                         tun->dev->stats.rx_frame_errors++;
643                         kfree_skb(skb);
644                         return -EINVAL;
645                 }
646
647                 /* Header must be checked, and gso_segs computed. */
648                 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
649                 skb_shinfo(skb)->gso_segs = 0;
650         }
651
652         netif_rx_ni(skb);
653
654         tun->dev->stats.rx_packets++;
655         tun->dev->stats.rx_bytes += len;
656
657         return count;
658 }
659
660 static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
661                               unsigned long count, loff_t pos)
662 {
663         struct tun_struct *tun = tun_get(iocb->ki_filp);
664         ssize_t result;
665
666         if (!tun)
667                 return -EBADFD;
668
669         DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
670
671         result = tun_get_user(tun, (struct iovec *) iv, iov_length(iv, count));
672
673         tun_put(tun);
674         return result;
675 }
676
677 /* Put packet to the user space buffer */
678 static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
679                                        struct sk_buff *skb,
680                                        struct iovec *iv, int len)
681 {
682         struct tun_pi pi = { 0, skb->protocol };
683         ssize_t total = 0;
684
685         if (!(tun->flags & TUN_NO_PI)) {
686                 if ((len -= sizeof(pi)) < 0)
687                         return -EINVAL;
688
689                 if (len < skb->len) {
690                         /* Packet will be striped */
691                         pi.flags |= TUN_PKT_STRIP;
692                 }
693
694                 if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
695                         return -EFAULT;
696                 total += sizeof(pi);
697         }
698
699         if (tun->flags & TUN_VNET_HDR) {
700                 struct virtio_net_hdr gso = { 0 }; /* no info leak */
701                 if ((len -= sizeof(gso)) < 0)
702                         return -EINVAL;
703
704                 if (skb_is_gso(skb)) {
705                         struct skb_shared_info *sinfo = skb_shinfo(skb);
706
707                         /* This is a hint as to how much should be linear. */
708                         gso.hdr_len = skb_headlen(skb);
709                         gso.gso_size = sinfo->gso_size;
710                         if (sinfo->gso_type & SKB_GSO_TCPV4)
711                                 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
712                         else if (sinfo->gso_type & SKB_GSO_TCPV6)
713                                 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
714                         else
715                                 BUG();
716                         if (sinfo->gso_type & SKB_GSO_TCP_ECN)
717                                 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
718                 } else
719                         gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
720
721                 if (skb->ip_summed == CHECKSUM_PARTIAL) {
722                         gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
723                         gso.csum_start = skb->csum_start - skb_headroom(skb);
724                         gso.csum_offset = skb->csum_offset;
725                 } /* else everything is zero */
726
727                 if (unlikely(memcpy_toiovec(iv, (void *)&gso, sizeof(gso))))
728                         return -EFAULT;
729                 total += sizeof(gso);
730         }
731
732         len = min_t(int, skb->len, len);
733
734         skb_copy_datagram_iovec(skb, 0, iv, len);
735         total += len;
736
737         tun->dev->stats.tx_packets++;
738         tun->dev->stats.tx_bytes += len;
739
740         return total;
741 }
742
743 static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
744                             unsigned long count, loff_t pos)
745 {
746         struct file *file = iocb->ki_filp;
747         struct tun_file *tfile = file->private_data;
748         struct tun_struct *tun = __tun_get(tfile);
749         DECLARE_WAITQUEUE(wait, current);
750         struct sk_buff *skb;
751         ssize_t len, ret = 0;
752
753         if (!tun)
754                 return -EBADFD;
755
756         DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
757
758         len = iov_length(iv, count);
759         if (len < 0) {
760                 ret = -EINVAL;
761                 goto out;
762         }
763
764         add_wait_queue(&tfile->read_wait, &wait);
765         while (len) {
766                 current->state = TASK_INTERRUPTIBLE;
767
768                 /* Read frames from the queue */
769                 if (!(skb=skb_dequeue(&tun->readq))) {
770                         if (file->f_flags & O_NONBLOCK) {
771                                 ret = -EAGAIN;
772                                 break;
773                         }
774                         if (signal_pending(current)) {
775                                 ret = -ERESTARTSYS;
776                                 break;
777                         }
778                         if (tun->dev->reg_state != NETREG_REGISTERED) {
779                                 ret = -EIO;
780                                 break;
781                         }
782
783                         /* Nothing to read, let's sleep */
784                         schedule();
785                         continue;
786                 }
787                 netif_wake_queue(tun->dev);
788
789                 ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
790                 kfree_skb(skb);
791                 break;
792         }
793
794         current->state = TASK_RUNNING;
795         remove_wait_queue(&tfile->read_wait, &wait);
796
797 out:
798         tun_put(tun);
799         return ret;
800 }
801
802 static void tun_setup(struct net_device *dev)
803 {
804         struct tun_struct *tun = netdev_priv(dev);
805
806         skb_queue_head_init(&tun->readq);
807
808         tun->owner = -1;
809         tun->group = -1;
810
811         dev->ethtool_ops = &tun_ethtool_ops;
812         dev->destructor = free_netdev;
813         dev->features |= NETIF_F_NETNS_LOCAL;
814 }
815
816 static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
817 {
818         struct tun_struct *tun;
819         struct net_device *dev;
820         int err;
821
822         dev = __dev_get_by_name(net, ifr->ifr_name);
823         if (dev) {
824                 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
825                         tun = netdev_priv(dev);
826                 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
827                         tun = netdev_priv(dev);
828                 else
829                         return -EINVAL;
830
831                 err = tun_attach(tun, file);
832                 if (err < 0)
833                         return err;
834         }
835         else {
836                 char *name;
837                 unsigned long flags = 0;
838
839                 err = -EINVAL;
840
841                 if (!capable(CAP_NET_ADMIN))
842                         return -EPERM;
843
844                 /* Set dev type */
845                 if (ifr->ifr_flags & IFF_TUN) {
846                         /* TUN device */
847                         flags |= TUN_TUN_DEV;
848                         name = "tun%d";
849                 } else if (ifr->ifr_flags & IFF_TAP) {
850                         /* TAP device */
851                         flags |= TUN_TAP_DEV;
852                         name = "tap%d";
853                 } else
854                         goto failed;
855
856                 if (*ifr->ifr_name)
857                         name = ifr->ifr_name;
858
859                 dev = alloc_netdev(sizeof(struct tun_struct), name,
860                                    tun_setup);
861                 if (!dev)
862                         return -ENOMEM;
863
864                 dev_net_set(dev, net);
865
866                 tun = netdev_priv(dev);
867                 tun->dev = dev;
868                 tun->flags = flags;
869                 tun->txflt.count = 0;
870
871                 tun_net_init(dev);
872
873                 if (strchr(dev->name, '%')) {
874                         err = dev_alloc_name(dev, dev->name);
875                         if (err < 0)
876                                 goto err_free_dev;
877                 }
878
879                 err = register_netdevice(tun->dev);
880                 if (err < 0)
881                         goto err_free_dev;
882
883                 err = tun_attach(tun, file);
884                 if (err < 0)
885                         goto err_free_dev;
886         }
887
888         DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
889
890         if (ifr->ifr_flags & IFF_NO_PI)
891                 tun->flags |= TUN_NO_PI;
892         else
893                 tun->flags &= ~TUN_NO_PI;
894
895         if (ifr->ifr_flags & IFF_ONE_QUEUE)
896                 tun->flags |= TUN_ONE_QUEUE;
897         else
898                 tun->flags &= ~TUN_ONE_QUEUE;
899
900         if (ifr->ifr_flags & IFF_VNET_HDR)
901                 tun->flags |= TUN_VNET_HDR;
902         else
903                 tun->flags &= ~TUN_VNET_HDR;
904
905         /* Make sure persistent devices do not get stuck in
906          * xoff state.
907          */
908         if (netif_running(tun->dev))
909                 netif_wake_queue(tun->dev);
910
911         strcpy(ifr->ifr_name, tun->dev->name);
912         return 0;
913
914  err_free_dev:
915         free_netdev(dev);
916  failed:
917         return err;
918 }
919
920 static int tun_get_iff(struct net *net, struct file *file, struct ifreq *ifr)
921 {
922         struct tun_struct *tun = tun_get(file);
923
924         if (!tun)
925                 return -EBADFD;
926
927         DBG(KERN_INFO "%s: tun_get_iff\n", tun->dev->name);
928
929         strcpy(ifr->ifr_name, tun->dev->name);
930
931         ifr->ifr_flags = 0;
932
933         if (ifr->ifr_flags & TUN_TUN_DEV)
934                 ifr->ifr_flags |= IFF_TUN;
935         else
936                 ifr->ifr_flags |= IFF_TAP;
937
938         if (tun->flags & TUN_NO_PI)
939                 ifr->ifr_flags |= IFF_NO_PI;
940
941         if (tun->flags & TUN_ONE_QUEUE)
942                 ifr->ifr_flags |= IFF_ONE_QUEUE;
943
944         if (tun->flags & TUN_VNET_HDR)
945                 ifr->ifr_flags |= IFF_VNET_HDR;
946
947         tun_put(tun);
948         return 0;
949 }
950
951 /* This is like a cut-down ethtool ops, except done via tun fd so no
952  * privs required. */
953 static int set_offload(struct net_device *dev, unsigned long arg)
954 {
955         unsigned int old_features, features;
956
957         old_features = dev->features;
958         /* Unset features, set them as we chew on the arg. */
959         features = (old_features & ~(NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST
960                                     |NETIF_F_TSO_ECN|NETIF_F_TSO|NETIF_F_TSO6));
961
962         if (arg & TUN_F_CSUM) {
963                 features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
964                 arg &= ~TUN_F_CSUM;
965
966                 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
967                         if (arg & TUN_F_TSO_ECN) {
968                                 features |= NETIF_F_TSO_ECN;
969                                 arg &= ~TUN_F_TSO_ECN;
970                         }
971                         if (arg & TUN_F_TSO4)
972                                 features |= NETIF_F_TSO;
973                         if (arg & TUN_F_TSO6)
974                                 features |= NETIF_F_TSO6;
975                         arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
976                 }
977         }
978
979         /* This gives the user a way to test for new features in future by
980          * trying to set them. */
981         if (arg)
982                 return -EINVAL;
983
984         dev->features = features;
985         if (old_features != dev->features)
986                 netdev_features_change(dev);
987
988         return 0;
989 }
990
991 static int tun_chr_ioctl(struct inode *inode, struct file *file,
992                          unsigned int cmd, unsigned long arg)
993 {
994         struct tun_file *tfile = file->private_data;
995         struct tun_struct *tun;
996         void __user* argp = (void __user*)arg;
997         struct ifreq ifr;
998         int ret;
999
1000         if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
1001                 if (copy_from_user(&ifr, argp, sizeof ifr))
1002                         return -EFAULT;
1003
1004         if (cmd == TUNGETFEATURES) {
1005                 /* Currently this just means: "what IFF flags are valid?".
1006                  * This is needed because we never checked for invalid flags on
1007                  * TUNSETIFF. */
1008                 return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1009                                 IFF_VNET_HDR,
1010                                 (unsigned int __user*)argp);
1011         }
1012
1013         tun = __tun_get(tfile);
1014         if (cmd == TUNSETIFF && !tun) {
1015                 int err;
1016
1017                 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1018
1019                 rtnl_lock();
1020                 err = tun_set_iff(tfile->net, file, &ifr);
1021                 rtnl_unlock();
1022
1023                 if (err)
1024                         return err;
1025
1026                 if (copy_to_user(argp, &ifr, sizeof(ifr)))
1027                         return -EFAULT;
1028                 return 0;
1029         }
1030
1031
1032         if (!tun)
1033                 return -EBADFD;
1034
1035         DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
1036
1037         ret = 0;
1038         switch (cmd) {
1039         case TUNGETIFF:
1040                 ret = tun_get_iff(current->nsproxy->net_ns, file, &ifr);
1041                 if (ret)
1042                         break;
1043
1044                 if (copy_to_user(argp, &ifr, sizeof(ifr)))
1045                         ret = -EFAULT;
1046                 break;
1047
1048         case TUNSETNOCSUM:
1049                 /* Disable/Enable checksum */
1050                 if (arg)
1051                         tun->flags |= TUN_NOCHECKSUM;
1052                 else
1053                         tun->flags &= ~TUN_NOCHECKSUM;
1054
1055                 DBG(KERN_INFO "%s: checksum %s\n",
1056                     tun->dev->name, arg ? "disabled" : "enabled");
1057                 break;
1058
1059         case TUNSETPERSIST:
1060                 /* Disable/Enable persist mode */
1061                 if (arg)
1062                         tun->flags |= TUN_PERSIST;
1063                 else
1064                         tun->flags &= ~TUN_PERSIST;
1065
1066                 DBG(KERN_INFO "%s: persist %s\n",
1067                     tun->dev->name, arg ? "enabled" : "disabled");
1068                 break;
1069
1070         case TUNSETOWNER:
1071                 /* Set owner of the device */
1072                 tun->owner = (uid_t) arg;
1073
1074                 DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
1075                 break;
1076
1077         case TUNSETGROUP:
1078                 /* Set group of the device */
1079                 tun->group= (gid_t) arg;
1080
1081                 DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
1082                 break;
1083
1084         case TUNSETLINK:
1085                 /* Only allow setting the type when the interface is down */
1086                 rtnl_lock();
1087                 if (tun->dev->flags & IFF_UP) {
1088                         DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
1089                                 tun->dev->name);
1090                         ret = -EBUSY;
1091                 } else {
1092                         tun->dev->type = (int) arg;
1093                         DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
1094                         ret = 0;
1095                 }
1096                 rtnl_unlock();
1097                 break;
1098
1099 #ifdef TUN_DEBUG
1100         case TUNSETDEBUG:
1101                 tun->debug = arg;
1102                 break;
1103 #endif
1104         case TUNSETOFFLOAD:
1105                 rtnl_lock();
1106                 ret = set_offload(tun->dev, arg);
1107                 rtnl_unlock();
1108                 break;
1109
1110         case TUNSETTXFILTER:
1111                 /* Can be set only for TAPs */
1112                 ret = -EINVAL;
1113                 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1114                         break;
1115                 rtnl_lock();
1116                 ret = update_filter(&tun->txflt, (void __user *)arg);
1117                 rtnl_unlock();
1118                 break;
1119
1120         case SIOCGIFHWADDR:
1121                 /* Get hw addres */
1122                 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1123                 ifr.ifr_hwaddr.sa_family = tun->dev->type;
1124                 if (copy_to_user(argp, &ifr, sizeof ifr))
1125                         ret = -EFAULT;
1126                 break;
1127
1128         case SIOCSIFHWADDR:
1129                 /* Set hw address */
1130                 DBG(KERN_DEBUG "%s: set hw address: %pM\n",
1131                         tun->dev->name, ifr.ifr_hwaddr.sa_data);
1132
1133                 rtnl_lock();
1134                 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
1135                 rtnl_unlock();
1136                 break;
1137         default:
1138                 ret = -EINVAL;
1139                 break;
1140         };
1141
1142         tun_put(tun);
1143         return ret;
1144 }
1145
1146 static int tun_chr_fasync(int fd, struct file *file, int on)
1147 {
1148         struct tun_struct *tun = tun_get(file);
1149         int ret;
1150
1151         if (!tun)
1152                 return -EBADFD;
1153
1154         DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
1155
1156         lock_kernel();
1157         if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
1158                 goto out;
1159
1160         if (on) {
1161                 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
1162                 if (ret)
1163                         goto out;
1164                 tun->flags |= TUN_FASYNC;
1165         } else
1166                 tun->flags &= ~TUN_FASYNC;
1167         ret = 0;
1168 out:
1169         unlock_kernel();
1170         tun_put(tun);
1171         return ret;
1172 }
1173
1174 static int tun_chr_open(struct inode *inode, struct file * file)
1175 {
1176         struct tun_file *tfile;
1177         cycle_kernel_lock();
1178         DBG1(KERN_INFO "tunX: tun_chr_open\n");
1179
1180         tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
1181         if (!tfile)
1182                 return -ENOMEM;
1183         atomic_set(&tfile->count, 0);
1184         tfile->tun = NULL;
1185         tfile->net = get_net(current->nsproxy->net_ns);
1186         init_waitqueue_head(&tfile->read_wait);
1187         file->private_data = tfile;
1188         return 0;
1189 }
1190
1191 static int tun_chr_close(struct inode *inode, struct file *file)
1192 {
1193         struct tun_file *tfile = file->private_data;
1194         struct tun_struct *tun = __tun_get(tfile);
1195
1196
1197         if (tun) {
1198                 DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
1199
1200                 rtnl_lock();
1201                 __tun_detach(tun);
1202
1203                 /* If desireable, unregister the netdevice. */
1204                 if (!(tun->flags & TUN_PERSIST))
1205                         unregister_netdevice(tun->dev);
1206
1207                 rtnl_unlock();
1208         }
1209
1210         put_net(tfile->net);
1211         kfree(tfile);
1212
1213         return 0;
1214 }
1215
1216 static const struct file_operations tun_fops = {
1217         .owner  = THIS_MODULE,
1218         .llseek = no_llseek,
1219         .read  = do_sync_read,
1220         .aio_read  = tun_chr_aio_read,
1221         .write = do_sync_write,
1222         .aio_write = tun_chr_aio_write,
1223         .poll   = tun_chr_poll,
1224         .ioctl  = tun_chr_ioctl,
1225         .open   = tun_chr_open,
1226         .release = tun_chr_close,
1227         .fasync = tun_chr_fasync
1228 };
1229
1230 static struct miscdevice tun_miscdev = {
1231         .minor = TUN_MINOR,
1232         .name = "tun",
1233         .fops = &tun_fops,
1234 };
1235
1236 /* ethtool interface */
1237
1238 static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1239 {
1240         cmd->supported          = 0;
1241         cmd->advertising        = 0;
1242         cmd->speed              = SPEED_10;
1243         cmd->duplex             = DUPLEX_FULL;
1244         cmd->port               = PORT_TP;
1245         cmd->phy_address        = 0;
1246         cmd->transceiver        = XCVR_INTERNAL;
1247         cmd->autoneg            = AUTONEG_DISABLE;
1248         cmd->maxtxpkt           = 0;
1249         cmd->maxrxpkt           = 0;
1250         return 0;
1251 }
1252
1253 static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1254 {
1255         struct tun_struct *tun = netdev_priv(dev);
1256
1257         strcpy(info->driver, DRV_NAME);
1258         strcpy(info->version, DRV_VERSION);
1259         strcpy(info->fw_version, "N/A");
1260
1261         switch (tun->flags & TUN_TYPE_MASK) {
1262         case TUN_TUN_DEV:
1263                 strcpy(info->bus_info, "tun");
1264                 break;
1265         case TUN_TAP_DEV:
1266                 strcpy(info->bus_info, "tap");
1267                 break;
1268         }
1269 }
1270
1271 static u32 tun_get_msglevel(struct net_device *dev)
1272 {
1273 #ifdef TUN_DEBUG
1274         struct tun_struct *tun = netdev_priv(dev);
1275         return tun->debug;
1276 #else
1277         return -EOPNOTSUPP;
1278 #endif
1279 }
1280
1281 static void tun_set_msglevel(struct net_device *dev, u32 value)
1282 {
1283 #ifdef TUN_DEBUG
1284         struct tun_struct *tun = netdev_priv(dev);
1285         tun->debug = value;
1286 #endif
1287 }
1288
1289 static u32 tun_get_link(struct net_device *dev)
1290 {
1291         struct tun_struct *tun = netdev_priv(dev);
1292         return !!tun->tfile;
1293 }
1294
1295 static u32 tun_get_rx_csum(struct net_device *dev)
1296 {
1297         struct tun_struct *tun = netdev_priv(dev);
1298         return (tun->flags & TUN_NOCHECKSUM) == 0;
1299 }
1300
1301 static int tun_set_rx_csum(struct net_device *dev, u32 data)
1302 {
1303         struct tun_struct *tun = netdev_priv(dev);
1304         if (data)
1305                 tun->flags &= ~TUN_NOCHECKSUM;
1306         else
1307                 tun->flags |= TUN_NOCHECKSUM;
1308         return 0;
1309 }
1310
1311 static const struct ethtool_ops tun_ethtool_ops = {
1312         .get_settings   = tun_get_settings,
1313         .get_drvinfo    = tun_get_drvinfo,
1314         .get_msglevel   = tun_get_msglevel,
1315         .set_msglevel   = tun_set_msglevel,
1316         .get_link       = tun_get_link,
1317         .get_rx_csum    = tun_get_rx_csum,
1318         .set_rx_csum    = tun_set_rx_csum
1319 };
1320
1321 static int tun_init_net(struct net *net)
1322 {
1323         return 0;
1324 }
1325
1326 static void tun_exit_net(struct net *net)
1327 {
1328         struct net_device *dev, *next;
1329
1330         rtnl_lock();
1331         for_each_netdev_safe(net, dev, next) {
1332                 if (dev->ethtool_ops != &tun_ethtool_ops)
1333                         continue;
1334                 DBG(KERN_INFO "%s cleaned up\n", dev->name);
1335                 unregister_netdevice(dev);
1336         }
1337         rtnl_unlock();
1338 }
1339
1340 static struct pernet_operations tun_net_ops = {
1341         .init = tun_init_net,
1342         .exit = tun_exit_net,
1343 };
1344
1345 static int __init tun_init(void)
1346 {
1347         int ret = 0;
1348
1349         printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
1350         printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
1351
1352         ret = register_pernet_device(&tun_net_ops);
1353         if (ret) {
1354                 printk(KERN_ERR "tun: Can't register pernet ops\n");
1355                 goto err_pernet;
1356         }
1357
1358         ret = misc_register(&tun_miscdev);
1359         if (ret) {
1360                 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
1361                 goto err_misc;
1362         }
1363         return 0;
1364
1365 err_misc:
1366         unregister_pernet_device(&tun_net_ops);
1367 err_pernet:
1368         return ret;
1369 }
1370
1371 static void tun_cleanup(void)
1372 {
1373         misc_deregister(&tun_miscdev);
1374         unregister_pernet_device(&tun_net_ops);
1375 }
1376
1377 module_init(tun_init);
1378 module_exit(tun_cleanup);
1379 MODULE_DESCRIPTION(DRV_DESCRIPTION);
1380 MODULE_AUTHOR(DRV_COPYRIGHT);
1381 MODULE_LICENSE("GPL");
1382 MODULE_ALIAS_MISCDEV(TUN_MINOR);