ixgbe: fix EICR write in ixgbe_msix_other
[pandora-kernel.git] / 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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
39 #define DRV_NAME        "tun"
40 #define DRV_VERSION     "1.6"
41 #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
42 #define DRV_COPYRIGHT   "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
43
44 #include <linux/module.h>
45 #include <linux/errno.h>
46 #include <linux/kernel.h>
47 #include <linux/major.h>
48 #include <linux/slab.h>
49 #include <linux/poll.h>
50 #include <linux/fcntl.h>
51 #include <linux/init.h>
52 #include <linux/skbuff.h>
53 #include <linux/netdevice.h>
54 #include <linux/etherdevice.h>
55 #include <linux/miscdevice.h>
56 #include <linux/ethtool.h>
57 #include <linux/rtnetlink.h>
58 #include <linux/compat.h>
59 #include <linux/if.h>
60 #include <linux/if_arp.h>
61 #include <linux/if_ether.h>
62 #include <linux/if_tun.h>
63 #include <linux/crc32.h>
64 #include <linux/nsproxy.h>
65 #include <linux/virtio_net.h>
66 #include <linux/rcupdate.h>
67 #include <net/net_namespace.h>
68 #include <net/netns/generic.h>
69 #include <net/rtnetlink.h>
70 #include <net/sock.h>
71
72 #include <asm/system.h>
73 #include <asm/uaccess.h>
74
75 /* Uncomment to enable debugging */
76 /* #define TUN_DEBUG 1 */
77
78 #ifdef TUN_DEBUG
79 static int debug;
80
81 #define tun_debug(level, tun, fmt, args...)                     \
82 do {                                                            \
83         if (tun->debug)                                         \
84                 netdev_printk(level, tun->dev, fmt, ##args);    \
85 } while (0)
86 #define DBG1(level, fmt, args...)                               \
87 do {                                                            \
88         if (debug == 2)                                         \
89                 printk(level fmt, ##args);                      \
90 } while (0)
91 #else
92 #define tun_debug(level, tun, fmt, args...)                     \
93 do {                                                            \
94         if (0)                                                  \
95                 netdev_printk(level, tun->dev, fmt, ##args);    \
96 } while (0)
97 #define DBG1(level, fmt, args...)                               \
98 do {                                                            \
99         if (0)                                                  \
100                 printk(level fmt, ##args);                      \
101 } while (0)
102 #endif
103
104 #define FLT_EXACT_COUNT 8
105 struct tap_filter {
106         unsigned int    count;    /* Number of addrs. Zero means disabled */
107         u32             mask[2];  /* Mask of the hashed addrs */
108         unsigned char   addr[FLT_EXACT_COUNT][ETH_ALEN];
109 };
110
111 struct tun_file {
112         atomic_t count;
113         struct tun_struct *tun;
114         struct net *net;
115 };
116
117 struct tun_sock;
118
119 struct tun_struct {
120         struct tun_file         *tfile;
121         unsigned int            flags;
122         uid_t                   owner;
123         gid_t                   group;
124
125         struct net_device       *dev;
126         u32                     set_features;
127 #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
128                           NETIF_F_TSO6|NETIF_F_UFO)
129         struct fasync_struct    *fasync;
130
131         struct tap_filter       txflt;
132         struct socket           socket;
133         struct socket_wq        wq;
134
135         int                     vnet_hdr_sz;
136
137 #ifdef TUN_DEBUG
138         int debug;
139 #endif
140 };
141
142 struct tun_sock {
143         struct sock             sk;
144         struct tun_struct       *tun;
145 };
146
147 static inline struct tun_sock *tun_sk(struct sock *sk)
148 {
149         return container_of(sk, struct tun_sock, sk);
150 }
151
152 static int tun_attach(struct tun_struct *tun, struct file *file)
153 {
154         struct tun_file *tfile = file->private_data;
155         int err;
156
157         ASSERT_RTNL();
158
159         netif_tx_lock_bh(tun->dev);
160
161         err = -EINVAL;
162         if (tfile->tun)
163                 goto out;
164
165         err = -EBUSY;
166         if (tun->tfile)
167                 goto out;
168
169         err = 0;
170         tfile->tun = tun;
171         tun->tfile = tfile;
172         tun->socket.file = file;
173         netif_carrier_on(tun->dev);
174         dev_hold(tun->dev);
175         sock_hold(tun->socket.sk);
176         atomic_inc(&tfile->count);
177
178 out:
179         netif_tx_unlock_bh(tun->dev);
180         return err;
181 }
182
183 static void __tun_detach(struct tun_struct *tun)
184 {
185         /* Detach from net device */
186         netif_tx_lock_bh(tun->dev);
187         netif_carrier_off(tun->dev);
188         tun->tfile = NULL;
189         tun->socket.file = NULL;
190         netif_tx_unlock_bh(tun->dev);
191
192         /* Drop read queue */
193         skb_queue_purge(&tun->socket.sk->sk_receive_queue);
194
195         /* Drop the extra count on the net device */
196         dev_put(tun->dev);
197 }
198
199 static void tun_detach(struct tun_struct *tun)
200 {
201         rtnl_lock();
202         __tun_detach(tun);
203         rtnl_unlock();
204 }
205
206 static struct tun_struct *__tun_get(struct tun_file *tfile)
207 {
208         struct tun_struct *tun = NULL;
209
210         if (atomic_inc_not_zero(&tfile->count))
211                 tun = tfile->tun;
212
213         return tun;
214 }
215
216 static struct tun_struct *tun_get(struct file *file)
217 {
218         return __tun_get(file->private_data);
219 }
220
221 static void tun_put(struct tun_struct *tun)
222 {
223         struct tun_file *tfile = tun->tfile;
224
225         if (atomic_dec_and_test(&tfile->count))
226                 tun_detach(tfile->tun);
227 }
228
229 /* TAP filtering */
230 static void addr_hash_set(u32 *mask, const u8 *addr)
231 {
232         int n = ether_crc(ETH_ALEN, addr) >> 26;
233         mask[n >> 5] |= (1 << (n & 31));
234 }
235
236 static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
237 {
238         int n = ether_crc(ETH_ALEN, addr) >> 26;
239         return mask[n >> 5] & (1 << (n & 31));
240 }
241
242 static int update_filter(struct tap_filter *filter, void __user *arg)
243 {
244         struct { u8 u[ETH_ALEN]; } *addr;
245         struct tun_filter uf;
246         int err, alen, n, nexact;
247
248         if (copy_from_user(&uf, arg, sizeof(uf)))
249                 return -EFAULT;
250
251         if (!uf.count) {
252                 /* Disabled */
253                 filter->count = 0;
254                 return 0;
255         }
256
257         alen = ETH_ALEN * uf.count;
258         addr = kmalloc(alen, GFP_KERNEL);
259         if (!addr)
260                 return -ENOMEM;
261
262         if (copy_from_user(addr, arg + sizeof(uf), alen)) {
263                 err = -EFAULT;
264                 goto done;
265         }
266
267         /* The filter is updated without holding any locks. Which is
268          * perfectly safe. We disable it first and in the worst
269          * case we'll accept a few undesired packets. */
270         filter->count = 0;
271         wmb();
272
273         /* Use first set of addresses as an exact filter */
274         for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
275                 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
276
277         nexact = n;
278
279         /* Remaining multicast addresses are hashed,
280          * unicast will leave the filter disabled. */
281         memset(filter->mask, 0, sizeof(filter->mask));
282         for (; n < uf.count; n++) {
283                 if (!is_multicast_ether_addr(addr[n].u)) {
284                         err = 0; /* no filter */
285                         goto done;
286                 }
287                 addr_hash_set(filter->mask, addr[n].u);
288         }
289
290         /* For ALLMULTI just set the mask to all ones.
291          * This overrides the mask populated above. */
292         if ((uf.flags & TUN_FLT_ALLMULTI))
293                 memset(filter->mask, ~0, sizeof(filter->mask));
294
295         /* Now enable the filter */
296         wmb();
297         filter->count = nexact;
298
299         /* Return the number of exact filters */
300         err = nexact;
301
302 done:
303         kfree(addr);
304         return err;
305 }
306
307 /* Returns: 0 - drop, !=0 - accept */
308 static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
309 {
310         /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
311          * at this point. */
312         struct ethhdr *eh = (struct ethhdr *) skb->data;
313         int i;
314
315         /* Exact match */
316         for (i = 0; i < filter->count; i++)
317                 if (!compare_ether_addr(eh->h_dest, filter->addr[i]))
318                         return 1;
319
320         /* Inexact match (multicast only) */
321         if (is_multicast_ether_addr(eh->h_dest))
322                 return addr_hash_test(filter->mask, eh->h_dest);
323
324         return 0;
325 }
326
327 /*
328  * Checks whether the packet is accepted or not.
329  * Returns: 0 - drop, !=0 - accept
330  */
331 static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
332 {
333         if (!filter->count)
334                 return 1;
335
336         return run_filter(filter, skb);
337 }
338
339 /* Network device part of the driver */
340
341 static const struct ethtool_ops tun_ethtool_ops;
342
343 /* Net device detach from fd. */
344 static void tun_net_uninit(struct net_device *dev)
345 {
346         struct tun_struct *tun = netdev_priv(dev);
347         struct tun_file *tfile = tun->tfile;
348
349         /* Inform the methods they need to stop using the dev.
350          */
351         if (tfile) {
352                 wake_up_all(&tun->wq.wait);
353                 if (atomic_dec_and_test(&tfile->count))
354                         __tun_detach(tun);
355         }
356 }
357
358 static void tun_free_netdev(struct net_device *dev)
359 {
360         struct tun_struct *tun = netdev_priv(dev);
361
362         sock_put(tun->socket.sk);
363 }
364
365 /* Net device open. */
366 static int tun_net_open(struct net_device *dev)
367 {
368         netif_start_queue(dev);
369         return 0;
370 }
371
372 /* Net device close. */
373 static int tun_net_close(struct net_device *dev)
374 {
375         netif_stop_queue(dev);
376         return 0;
377 }
378
379 /* Net device start xmit */
380 static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
381 {
382         struct tun_struct *tun = netdev_priv(dev);
383
384         tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
385
386         /* Drop packet if interface is not attached */
387         if (!tun->tfile)
388                 goto drop;
389
390         /* Drop if the filter does not like it.
391          * This is a noop if the filter is disabled.
392          * Filter can be enabled only for the TAP devices. */
393         if (!check_filter(&tun->txflt, skb))
394                 goto drop;
395
396         if (tun->socket.sk->sk_filter &&
397             sk_filter(tun->socket.sk, skb))
398                 goto drop;
399
400         if (skb_queue_len(&tun->socket.sk->sk_receive_queue) >= dev->tx_queue_len) {
401                 if (!(tun->flags & TUN_ONE_QUEUE)) {
402                         /* Normal queueing mode. */
403                         /* Packet scheduler handles dropping of further packets. */
404                         netif_stop_queue(dev);
405
406                         /* We won't see all dropped packets individually, so overrun
407                          * error is more appropriate. */
408                         dev->stats.tx_fifo_errors++;
409                 } else {
410                         /* Single queue mode.
411                          * Driver handles dropping of all packets itself. */
412                         goto drop;
413                 }
414         }
415
416         /* Orphan the skb - required as we might hang on to it
417          * for indefinite time. */
418         skb_orphan(skb);
419
420         nf_reset(skb);
421
422         /* Enqueue packet */
423         skb_queue_tail(&tun->socket.sk->sk_receive_queue, skb);
424
425         /* Notify and wake up reader process */
426         if (tun->flags & TUN_FASYNC)
427                 kill_fasync(&tun->fasync, SIGIO, POLL_IN);
428         wake_up_interruptible_poll(&tun->wq.wait, POLLIN |
429                                    POLLRDNORM | POLLRDBAND);
430         return NETDEV_TX_OK;
431
432 drop:
433         dev->stats.tx_dropped++;
434         kfree_skb(skb);
435         return NETDEV_TX_OK;
436 }
437
438 static void tun_net_mclist(struct net_device *dev)
439 {
440         /*
441          * This callback is supposed to deal with mc filter in
442          * _rx_ path and has nothing to do with the _tx_ path.
443          * In rx path we always accept everything userspace gives us.
444          */
445 }
446
447 #define MIN_MTU 68
448 #define MAX_MTU 65535
449
450 static int
451 tun_net_change_mtu(struct net_device *dev, int new_mtu)
452 {
453         if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
454                 return -EINVAL;
455         dev->mtu = new_mtu;
456         return 0;
457 }
458
459 static u32 tun_net_fix_features(struct net_device *dev, u32 features)
460 {
461         struct tun_struct *tun = netdev_priv(dev);
462
463         return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
464 }
465 #ifdef CONFIG_NET_POLL_CONTROLLER
466 static void tun_poll_controller(struct net_device *dev)
467 {
468         /*
469          * Tun only receives frames when:
470          * 1) the char device endpoint gets data from user space
471          * 2) the tun socket gets a sendmsg call from user space
472          * Since both of those are syncronous operations, we are guaranteed
473          * never to have pending data when we poll for it
474          * so theres nothing to do here but return.
475          * We need this though so netpoll recognizes us as an interface that
476          * supports polling, which enables bridge devices in virt setups to
477          * still use netconsole
478          */
479         return;
480 }
481 #endif
482 static const struct net_device_ops tun_netdev_ops = {
483         .ndo_uninit             = tun_net_uninit,
484         .ndo_open               = tun_net_open,
485         .ndo_stop               = tun_net_close,
486         .ndo_start_xmit         = tun_net_xmit,
487         .ndo_change_mtu         = tun_net_change_mtu,
488         .ndo_fix_features       = tun_net_fix_features,
489 #ifdef CONFIG_NET_POLL_CONTROLLER
490         .ndo_poll_controller    = tun_poll_controller,
491 #endif
492 };
493
494 static const struct net_device_ops tap_netdev_ops = {
495         .ndo_uninit             = tun_net_uninit,
496         .ndo_open               = tun_net_open,
497         .ndo_stop               = tun_net_close,
498         .ndo_start_xmit         = tun_net_xmit,
499         .ndo_change_mtu         = tun_net_change_mtu,
500         .ndo_fix_features       = tun_net_fix_features,
501         .ndo_set_rx_mode        = tun_net_mclist,
502         .ndo_set_mac_address    = eth_mac_addr,
503         .ndo_validate_addr      = eth_validate_addr,
504 #ifdef CONFIG_NET_POLL_CONTROLLER
505         .ndo_poll_controller    = tun_poll_controller,
506 #endif
507 };
508
509 /* Initialize net device. */
510 static void tun_net_init(struct net_device *dev)
511 {
512         struct tun_struct *tun = netdev_priv(dev);
513
514         switch (tun->flags & TUN_TYPE_MASK) {
515         case TUN_TUN_DEV:
516                 dev->netdev_ops = &tun_netdev_ops;
517
518                 /* Point-to-Point TUN Device */
519                 dev->hard_header_len = 0;
520                 dev->addr_len = 0;
521                 dev->mtu = 1500;
522
523                 /* Zero header length */
524                 dev->type = ARPHRD_NONE;
525                 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
526                 dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
527                 break;
528
529         case TUN_TAP_DEV:
530                 dev->netdev_ops = &tap_netdev_ops;
531                 /* Ethernet TAP Device */
532                 ether_setup(dev);
533                 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
534
535                 random_ether_addr(dev->dev_addr);
536
537                 dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
538                 break;
539         }
540 }
541
542 /* Character device part */
543
544 /* Poll */
545 static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
546 {
547         struct tun_file *tfile = file->private_data;
548         struct tun_struct *tun = __tun_get(tfile);
549         struct sock *sk;
550         unsigned int mask = 0;
551
552         if (!tun)
553                 return POLLERR;
554
555         sk = tun->socket.sk;
556
557         tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
558
559         poll_wait(file, &tun->wq.wait, wait);
560
561         if (!skb_queue_empty(&sk->sk_receive_queue))
562                 mask |= POLLIN | POLLRDNORM;
563
564         if (sock_writeable(sk) ||
565             (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
566              sock_writeable(sk)))
567                 mask |= POLLOUT | POLLWRNORM;
568
569         if (tun->dev->reg_state != NETREG_REGISTERED)
570                 mask = POLLERR;
571
572         tun_put(tun);
573         return mask;
574 }
575
576 /* prepad is the amount to reserve at front.  len is length after that.
577  * linear is a hint as to how much to copy (usually headers). */
578 static struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
579                                      size_t prepad, size_t len,
580                                      size_t linear, int noblock)
581 {
582         struct sock *sk = tun->socket.sk;
583         struct sk_buff *skb;
584         int err;
585
586         sock_update_classid(sk);
587
588         /* Under a page?  Don't bother with paged skb. */
589         if (prepad + len < PAGE_SIZE || !linear)
590                 linear = len;
591
592         skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
593                                    &err);
594         if (!skb)
595                 return ERR_PTR(err);
596
597         skb_reserve(skb, prepad);
598         skb_put(skb, linear);
599         skb->data_len = len - linear;
600         skb->len += len - linear;
601
602         return skb;
603 }
604
605 /* Get packet from user space buffer */
606 static ssize_t tun_get_user(struct tun_struct *tun,
607                             const struct iovec *iv, size_t count,
608                             int noblock)
609 {
610         struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
611         struct sk_buff *skb;
612         size_t len = count, align = NET_SKB_PAD;
613         struct virtio_net_hdr gso = { 0 };
614         int offset = 0;
615
616         if (!(tun->flags & TUN_NO_PI)) {
617                 if ((len -= sizeof(pi)) > count)
618                         return -EINVAL;
619
620                 if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
621                         return -EFAULT;
622                 offset += sizeof(pi);
623         }
624
625         if (tun->flags & TUN_VNET_HDR) {
626                 if ((len -= tun->vnet_hdr_sz) > count)
627                         return -EINVAL;
628
629                 if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
630                         return -EFAULT;
631
632                 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
633                     gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
634                         gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
635
636                 if (gso.hdr_len > len)
637                         return -EINVAL;
638                 offset += tun->vnet_hdr_sz;
639         }
640
641         if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
642                 align += NET_IP_ALIGN;
643                 if (unlikely(len < ETH_HLEN ||
644                              (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
645                         return -EINVAL;
646         }
647
648         skb = tun_alloc_skb(tun, align, len, gso.hdr_len, noblock);
649         if (IS_ERR(skb)) {
650                 if (PTR_ERR(skb) != -EAGAIN)
651                         tun->dev->stats.rx_dropped++;
652                 return PTR_ERR(skb);
653         }
654
655         if (skb_copy_datagram_from_iovec(skb, 0, iv, offset, len)) {
656                 tun->dev->stats.rx_dropped++;
657                 kfree_skb(skb);
658                 return -EFAULT;
659         }
660
661         if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
662                 if (!skb_partial_csum_set(skb, gso.csum_start,
663                                           gso.csum_offset)) {
664                         tun->dev->stats.rx_frame_errors++;
665                         kfree_skb(skb);
666                         return -EINVAL;
667                 }
668         }
669
670         switch (tun->flags & TUN_TYPE_MASK) {
671         case TUN_TUN_DEV:
672                 if (tun->flags & TUN_NO_PI) {
673                         switch (skb->data[0] & 0xf0) {
674                         case 0x40:
675                                 pi.proto = htons(ETH_P_IP);
676                                 break;
677                         case 0x60:
678                                 pi.proto = htons(ETH_P_IPV6);
679                                 break;
680                         default:
681                                 tun->dev->stats.rx_dropped++;
682                                 kfree_skb(skb);
683                                 return -EINVAL;
684                         }
685                 }
686
687                 skb_reset_mac_header(skb);
688                 skb->protocol = pi.proto;
689                 skb->dev = tun->dev;
690                 break;
691         case TUN_TAP_DEV:
692                 skb->protocol = eth_type_trans(skb, tun->dev);
693                 break;
694         }
695
696         if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
697                 pr_debug("GSO!\n");
698                 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
699                 case VIRTIO_NET_HDR_GSO_TCPV4:
700                         skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
701                         break;
702                 case VIRTIO_NET_HDR_GSO_TCPV6:
703                         skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
704                         break;
705                 case VIRTIO_NET_HDR_GSO_UDP:
706                         skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
707                         break;
708                 default:
709                         tun->dev->stats.rx_frame_errors++;
710                         kfree_skb(skb);
711                         return -EINVAL;
712                 }
713
714                 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
715                         skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
716
717                 skb_shinfo(skb)->gso_size = gso.gso_size;
718                 if (skb_shinfo(skb)->gso_size == 0) {
719                         tun->dev->stats.rx_frame_errors++;
720                         kfree_skb(skb);
721                         return -EINVAL;
722                 }
723
724                 /* Header must be checked, and gso_segs computed. */
725                 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
726                 skb_shinfo(skb)->gso_segs = 0;
727         }
728
729         netif_rx_ni(skb);
730
731         tun->dev->stats.rx_packets++;
732         tun->dev->stats.rx_bytes += len;
733
734         return count;
735 }
736
737 static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
738                               unsigned long count, loff_t pos)
739 {
740         struct file *file = iocb->ki_filp;
741         struct tun_struct *tun = tun_get(file);
742         ssize_t result;
743
744         if (!tun)
745                 return -EBADFD;
746
747         tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
748
749         result = tun_get_user(tun, iv, iov_length(iv, count),
750                               file->f_flags & O_NONBLOCK);
751
752         tun_put(tun);
753         return result;
754 }
755
756 /* Put packet to the user space buffer */
757 static ssize_t tun_put_user(struct tun_struct *tun,
758                             struct sk_buff *skb,
759                             const struct iovec *iv, int len)
760 {
761         struct tun_pi pi = { 0, skb->protocol };
762         ssize_t total = 0;
763
764         if (!(tun->flags & TUN_NO_PI)) {
765                 if ((len -= sizeof(pi)) < 0)
766                         return -EINVAL;
767
768                 if (len < skb->len) {
769                         /* Packet will be striped */
770                         pi.flags |= TUN_PKT_STRIP;
771                 }
772
773                 if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
774                         return -EFAULT;
775                 total += sizeof(pi);
776         }
777
778         if (tun->flags & TUN_VNET_HDR) {
779                 struct virtio_net_hdr gso = { 0 }; /* no info leak */
780                 if ((len -= tun->vnet_hdr_sz) < 0)
781                         return -EINVAL;
782
783                 if (skb_is_gso(skb)) {
784                         struct skb_shared_info *sinfo = skb_shinfo(skb);
785
786                         /* This is a hint as to how much should be linear. */
787                         gso.hdr_len = skb_headlen(skb);
788                         gso.gso_size = sinfo->gso_size;
789                         if (sinfo->gso_type & SKB_GSO_TCPV4)
790                                 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
791                         else if (sinfo->gso_type & SKB_GSO_TCPV6)
792                                 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
793                         else if (sinfo->gso_type & SKB_GSO_UDP)
794                                 gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
795                         else {
796                                 pr_err("unexpected GSO type: "
797                                        "0x%x, gso_size %d, hdr_len %d\n",
798                                        sinfo->gso_type, gso.gso_size,
799                                        gso.hdr_len);
800                                 print_hex_dump(KERN_ERR, "tun: ",
801                                                DUMP_PREFIX_NONE,
802                                                16, 1, skb->head,
803                                                min((int)gso.hdr_len, 64), true);
804                                 WARN_ON_ONCE(1);
805                                 return -EINVAL;
806                         }
807                         if (sinfo->gso_type & SKB_GSO_TCP_ECN)
808                                 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
809                 } else
810                         gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
811
812                 if (skb->ip_summed == CHECKSUM_PARTIAL) {
813                         gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
814                         gso.csum_start = skb_checksum_start_offset(skb);
815                         gso.csum_offset = skb->csum_offset;
816                 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
817                         gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
818                 } /* else everything is zero */
819
820                 if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
821                                                sizeof(gso))))
822                         return -EFAULT;
823                 total += tun->vnet_hdr_sz;
824         }
825
826         len = min_t(int, skb->len, len);
827
828         skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
829         total += skb->len;
830
831         tun->dev->stats.tx_packets++;
832         tun->dev->stats.tx_bytes += len;
833
834         return total;
835 }
836
837 static ssize_t tun_do_read(struct tun_struct *tun,
838                            struct kiocb *iocb, const struct iovec *iv,
839                            ssize_t len, int noblock)
840 {
841         DECLARE_WAITQUEUE(wait, current);
842         struct sk_buff *skb;
843         ssize_t ret = 0;
844
845         tun_debug(KERN_INFO, tun, "tun_chr_read\n");
846
847         if (unlikely(!noblock))
848                 add_wait_queue(&tun->wq.wait, &wait);
849         while (len) {
850                 current->state = TASK_INTERRUPTIBLE;
851
852                 /* Read frames from the queue */
853                 if (!(skb=skb_dequeue(&tun->socket.sk->sk_receive_queue))) {
854                         if (noblock) {
855                                 ret = -EAGAIN;
856                                 break;
857                         }
858                         if (signal_pending(current)) {
859                                 ret = -ERESTARTSYS;
860                                 break;
861                         }
862                         if (tun->dev->reg_state != NETREG_REGISTERED) {
863                                 ret = -EIO;
864                                 break;
865                         }
866
867                         /* Nothing to read, let's sleep */
868                         schedule();
869                         continue;
870                 }
871                 netif_wake_queue(tun->dev);
872
873                 ret = tun_put_user(tun, skb, iv, len);
874                 kfree_skb(skb);
875                 break;
876         }
877
878         current->state = TASK_RUNNING;
879         if (unlikely(!noblock))
880                 remove_wait_queue(&tun->wq.wait, &wait);
881
882         return ret;
883 }
884
885 static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
886                             unsigned long count, loff_t pos)
887 {
888         struct file *file = iocb->ki_filp;
889         struct tun_file *tfile = file->private_data;
890         struct tun_struct *tun = __tun_get(tfile);
891         ssize_t len, ret;
892
893         if (!tun)
894                 return -EBADFD;
895         len = iov_length(iv, count);
896         if (len < 0) {
897                 ret = -EINVAL;
898                 goto out;
899         }
900
901         ret = tun_do_read(tun, iocb, iv, len, file->f_flags & O_NONBLOCK);
902         ret = min_t(ssize_t, ret, len);
903 out:
904         tun_put(tun);
905         return ret;
906 }
907
908 static void tun_setup(struct net_device *dev)
909 {
910         struct tun_struct *tun = netdev_priv(dev);
911
912         tun->owner = -1;
913         tun->group = -1;
914
915         dev->ethtool_ops = &tun_ethtool_ops;
916         dev->destructor = tun_free_netdev;
917 }
918
919 /* Trivial set of netlink ops to allow deleting tun or tap
920  * device with netlink.
921  */
922 static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
923 {
924         return -EINVAL;
925 }
926
927 static struct rtnl_link_ops tun_link_ops __read_mostly = {
928         .kind           = DRV_NAME,
929         .priv_size      = sizeof(struct tun_struct),
930         .setup          = tun_setup,
931         .validate       = tun_validate,
932 };
933
934 static void tun_sock_write_space(struct sock *sk)
935 {
936         struct tun_struct *tun;
937         wait_queue_head_t *wqueue;
938
939         if (!sock_writeable(sk))
940                 return;
941
942         if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
943                 return;
944
945         wqueue = sk_sleep(sk);
946         if (wqueue && waitqueue_active(wqueue))
947                 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
948                                                 POLLWRNORM | POLLWRBAND);
949
950         tun = tun_sk(sk)->tun;
951         kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
952 }
953
954 static void tun_sock_destruct(struct sock *sk)
955 {
956         free_netdev(tun_sk(sk)->tun->dev);
957 }
958
959 static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
960                        struct msghdr *m, size_t total_len)
961 {
962         struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
963         return tun_get_user(tun, m->msg_iov, total_len,
964                             m->msg_flags & MSG_DONTWAIT);
965 }
966
967 static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
968                        struct msghdr *m, size_t total_len,
969                        int flags)
970 {
971         struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
972         int ret;
973         if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
974                 return -EINVAL;
975         ret = tun_do_read(tun, iocb, m->msg_iov, total_len,
976                           flags & MSG_DONTWAIT);
977         if (ret > total_len) {
978                 m->msg_flags |= MSG_TRUNC;
979                 ret = flags & MSG_TRUNC ? ret : total_len;
980         }
981         return ret;
982 }
983
984 /* Ops structure to mimic raw sockets with tun */
985 static const struct proto_ops tun_socket_ops = {
986         .sendmsg = tun_sendmsg,
987         .recvmsg = tun_recvmsg,
988 };
989
990 static struct proto tun_proto = {
991         .name           = "tun",
992         .owner          = THIS_MODULE,
993         .obj_size       = sizeof(struct tun_sock),
994 };
995
996 static int tun_flags(struct tun_struct *tun)
997 {
998         int flags = 0;
999
1000         if (tun->flags & TUN_TUN_DEV)
1001                 flags |= IFF_TUN;
1002         else
1003                 flags |= IFF_TAP;
1004
1005         if (tun->flags & TUN_NO_PI)
1006                 flags |= IFF_NO_PI;
1007
1008         if (tun->flags & TUN_ONE_QUEUE)
1009                 flags |= IFF_ONE_QUEUE;
1010
1011         if (tun->flags & TUN_VNET_HDR)
1012                 flags |= IFF_VNET_HDR;
1013
1014         return flags;
1015 }
1016
1017 static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1018                               char *buf)
1019 {
1020         struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1021         return sprintf(buf, "0x%x\n", tun_flags(tun));
1022 }
1023
1024 static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1025                               char *buf)
1026 {
1027         struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1028         return sprintf(buf, "%d\n", tun->owner);
1029 }
1030
1031 static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1032                               char *buf)
1033 {
1034         struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1035         return sprintf(buf, "%d\n", tun->group);
1036 }
1037
1038 static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1039 static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1040 static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1041
1042 static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
1043 {
1044         struct sock *sk;
1045         struct tun_struct *tun;
1046         struct net_device *dev;
1047         int err;
1048
1049         dev = __dev_get_by_name(net, ifr->ifr_name);
1050         if (dev) {
1051                 const struct cred *cred = current_cred();
1052
1053                 if (ifr->ifr_flags & IFF_TUN_EXCL)
1054                         return -EBUSY;
1055                 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
1056                         tun = netdev_priv(dev);
1057                 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1058                         tun = netdev_priv(dev);
1059                 else
1060                         return -EINVAL;
1061
1062                 if (((tun->owner != -1 && cred->euid != tun->owner) ||
1063                      (tun->group != -1 && !in_egroup_p(tun->group))) &&
1064                     !capable(CAP_NET_ADMIN))
1065                         return -EPERM;
1066                 err = security_tun_dev_attach(tun->socket.sk);
1067                 if (err < 0)
1068                         return err;
1069
1070                 err = tun_attach(tun, file);
1071                 if (err < 0)
1072                         return err;
1073         }
1074         else {
1075                 char *name;
1076                 unsigned long flags = 0;
1077
1078                 if (!capable(CAP_NET_ADMIN))
1079                         return -EPERM;
1080                 err = security_tun_dev_create();
1081                 if (err < 0)
1082                         return err;
1083
1084                 /* Set dev type */
1085                 if (ifr->ifr_flags & IFF_TUN) {
1086                         /* TUN device */
1087                         flags |= TUN_TUN_DEV;
1088                         name = "tun%d";
1089                 } else if (ifr->ifr_flags & IFF_TAP) {
1090                         /* TAP device */
1091                         flags |= TUN_TAP_DEV;
1092                         name = "tap%d";
1093                 } else
1094                         return -EINVAL;
1095
1096                 if (*ifr->ifr_name)
1097                         name = ifr->ifr_name;
1098
1099                 dev = alloc_netdev(sizeof(struct tun_struct), name,
1100                                    tun_setup);
1101                 if (!dev)
1102                         return -ENOMEM;
1103
1104                 dev_net_set(dev, net);
1105                 dev->rtnl_link_ops = &tun_link_ops;
1106
1107                 tun = netdev_priv(dev);
1108                 tun->dev = dev;
1109                 tun->flags = flags;
1110                 tun->txflt.count = 0;
1111                 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
1112
1113                 err = -ENOMEM;
1114                 sk = sk_alloc(net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
1115                 if (!sk)
1116                         goto err_free_dev;
1117
1118                 tun->socket.wq = &tun->wq;
1119                 init_waitqueue_head(&tun->wq.wait);
1120                 tun->socket.ops = &tun_socket_ops;
1121                 sock_init_data(&tun->socket, sk);
1122                 sk->sk_write_space = tun_sock_write_space;
1123                 sk->sk_sndbuf = INT_MAX;
1124
1125                 tun_sk(sk)->tun = tun;
1126
1127                 security_tun_dev_post_create(sk);
1128
1129                 tun_net_init(dev);
1130
1131                 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
1132                         TUN_USER_FEATURES;
1133                 dev->features = dev->hw_features;
1134
1135                 err = register_netdevice(tun->dev);
1136                 if (err < 0)
1137                         goto err_free_sk;
1138
1139                 if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1140                     device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1141                     device_create_file(&tun->dev->dev, &dev_attr_group))
1142                         pr_err("Failed to create tun sysfs files\n");
1143
1144                 sk->sk_destruct = tun_sock_destruct;
1145
1146                 err = tun_attach(tun, file);
1147                 if (err < 0)
1148                         goto failed;
1149         }
1150
1151         tun_debug(KERN_INFO, tun, "tun_set_iff\n");
1152
1153         if (ifr->ifr_flags & IFF_NO_PI)
1154                 tun->flags |= TUN_NO_PI;
1155         else
1156                 tun->flags &= ~TUN_NO_PI;
1157
1158         if (ifr->ifr_flags & IFF_ONE_QUEUE)
1159                 tun->flags |= TUN_ONE_QUEUE;
1160         else
1161                 tun->flags &= ~TUN_ONE_QUEUE;
1162
1163         if (ifr->ifr_flags & IFF_VNET_HDR)
1164                 tun->flags |= TUN_VNET_HDR;
1165         else
1166                 tun->flags &= ~TUN_VNET_HDR;
1167
1168         /* Make sure persistent devices do not get stuck in
1169          * xoff state.
1170          */
1171         if (netif_running(tun->dev))
1172                 netif_wake_queue(tun->dev);
1173
1174         strcpy(ifr->ifr_name, tun->dev->name);
1175         return 0;
1176
1177  err_free_sk:
1178         sock_put(sk);
1179  err_free_dev:
1180         free_netdev(dev);
1181  failed:
1182         return err;
1183 }
1184
1185 static int tun_get_iff(struct net *net, struct tun_struct *tun,
1186                        struct ifreq *ifr)
1187 {
1188         tun_debug(KERN_INFO, tun, "tun_get_iff\n");
1189
1190         strcpy(ifr->ifr_name, tun->dev->name);
1191
1192         ifr->ifr_flags = tun_flags(tun);
1193
1194         return 0;
1195 }
1196
1197 /* This is like a cut-down ethtool ops, except done via tun fd so no
1198  * privs required. */
1199 static int set_offload(struct tun_struct *tun, unsigned long arg)
1200 {
1201         u32 features = 0;
1202
1203         if (arg & TUN_F_CSUM) {
1204                 features |= NETIF_F_HW_CSUM;
1205                 arg &= ~TUN_F_CSUM;
1206
1207                 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
1208                         if (arg & TUN_F_TSO_ECN) {
1209                                 features |= NETIF_F_TSO_ECN;
1210                                 arg &= ~TUN_F_TSO_ECN;
1211                         }
1212                         if (arg & TUN_F_TSO4)
1213                                 features |= NETIF_F_TSO;
1214                         if (arg & TUN_F_TSO6)
1215                                 features |= NETIF_F_TSO6;
1216                         arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
1217                 }
1218
1219                 if (arg & TUN_F_UFO) {
1220                         features |= NETIF_F_UFO;
1221                         arg &= ~TUN_F_UFO;
1222                 }
1223         }
1224
1225         /* This gives the user a way to test for new features in future by
1226          * trying to set them. */
1227         if (arg)
1228                 return -EINVAL;
1229
1230         tun->set_features = features;
1231         netdev_update_features(tun->dev);
1232
1233         return 0;
1234 }
1235
1236 static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1237                             unsigned long arg, int ifreq_len)
1238 {
1239         struct tun_file *tfile = file->private_data;
1240         struct tun_struct *tun;
1241         void __user* argp = (void __user*)arg;
1242         struct sock_fprog fprog;
1243         struct ifreq ifr;
1244         int sndbuf;
1245         int vnet_hdr_sz;
1246         int ret;
1247
1248         if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) {
1249                 if (copy_from_user(&ifr, argp, ifreq_len))
1250                         return -EFAULT;
1251         } else {
1252                 memset(&ifr, 0, sizeof(ifr));
1253         }
1254         if (cmd == TUNGETFEATURES) {
1255                 /* Currently this just means: "what IFF flags are valid?".
1256                  * This is needed because we never checked for invalid flags on
1257                  * TUNSETIFF. */
1258                 return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1259                                 IFF_VNET_HDR,
1260                                 (unsigned int __user*)argp);
1261         }
1262
1263         rtnl_lock();
1264
1265         tun = __tun_get(tfile);
1266         if (cmd == TUNSETIFF && !tun) {
1267                 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1268
1269                 ret = tun_set_iff(tfile->net, file, &ifr);
1270
1271                 if (ret)
1272                         goto unlock;
1273
1274                 if (copy_to_user(argp, &ifr, ifreq_len))
1275                         ret = -EFAULT;
1276                 goto unlock;
1277         }
1278
1279         ret = -EBADFD;
1280         if (!tun)
1281                 goto unlock;
1282
1283         tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %d\n", cmd);
1284
1285         ret = 0;
1286         switch (cmd) {
1287         case TUNGETIFF:
1288                 ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
1289                 if (ret)
1290                         break;
1291
1292                 if (copy_to_user(argp, &ifr, ifreq_len))
1293                         ret = -EFAULT;
1294                 break;
1295
1296         case TUNSETNOCSUM:
1297                 /* Disable/Enable checksum */
1298
1299                 /* [unimplemented] */
1300                 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
1301                           arg ? "disabled" : "enabled");
1302                 break;
1303
1304         case TUNSETPERSIST:
1305                 /* Disable/Enable persist mode */
1306                 if (arg)
1307                         tun->flags |= TUN_PERSIST;
1308                 else
1309                         tun->flags &= ~TUN_PERSIST;
1310
1311                 tun_debug(KERN_INFO, tun, "persist %s\n",
1312                           arg ? "enabled" : "disabled");
1313                 break;
1314
1315         case TUNSETOWNER:
1316                 /* Set owner of the device */
1317                 tun->owner = (uid_t) arg;
1318
1319                 tun_debug(KERN_INFO, tun, "owner set to %d\n", tun->owner);
1320                 break;
1321
1322         case TUNSETGROUP:
1323                 /* Set group of the device */
1324                 tun->group= (gid_t) arg;
1325
1326                 tun_debug(KERN_INFO, tun, "group set to %d\n", tun->group);
1327                 break;
1328
1329         case TUNSETLINK:
1330                 /* Only allow setting the type when the interface is down */
1331                 if (tun->dev->flags & IFF_UP) {
1332                         tun_debug(KERN_INFO, tun,
1333                                   "Linktype set failed because interface is up\n");
1334                         ret = -EBUSY;
1335                 } else {
1336                         tun->dev->type = (int) arg;
1337                         tun_debug(KERN_INFO, tun, "linktype set to %d\n",
1338                                   tun->dev->type);
1339                         ret = 0;
1340                 }
1341                 break;
1342
1343 #ifdef TUN_DEBUG
1344         case TUNSETDEBUG:
1345                 tun->debug = arg;
1346                 break;
1347 #endif
1348         case TUNSETOFFLOAD:
1349                 ret = set_offload(tun, arg);
1350                 break;
1351
1352         case TUNSETTXFILTER:
1353                 /* Can be set only for TAPs */
1354                 ret = -EINVAL;
1355                 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1356                         break;
1357                 ret = update_filter(&tun->txflt, (void __user *)arg);
1358                 break;
1359
1360         case SIOCGIFHWADDR:
1361                 /* Get hw address */
1362                 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1363                 ifr.ifr_hwaddr.sa_family = tun->dev->type;
1364                 if (copy_to_user(argp, &ifr, ifreq_len))
1365                         ret = -EFAULT;
1366                 break;
1367
1368         case SIOCSIFHWADDR:
1369                 /* Set hw address */
1370                 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
1371                           ifr.ifr_hwaddr.sa_data);
1372
1373                 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
1374                 break;
1375
1376         case TUNGETSNDBUF:
1377                 sndbuf = tun->socket.sk->sk_sndbuf;
1378                 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
1379                         ret = -EFAULT;
1380                 break;
1381
1382         case TUNSETSNDBUF:
1383                 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
1384                         ret = -EFAULT;
1385                         break;
1386                 }
1387
1388                 tun->socket.sk->sk_sndbuf = sndbuf;
1389                 break;
1390
1391         case TUNGETVNETHDRSZ:
1392                 vnet_hdr_sz = tun->vnet_hdr_sz;
1393                 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
1394                         ret = -EFAULT;
1395                 break;
1396
1397         case TUNSETVNETHDRSZ:
1398                 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
1399                         ret = -EFAULT;
1400                         break;
1401                 }
1402                 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
1403                         ret = -EINVAL;
1404                         break;
1405                 }
1406
1407                 tun->vnet_hdr_sz = vnet_hdr_sz;
1408                 break;
1409
1410         case TUNATTACHFILTER:
1411                 /* Can be set only for TAPs */
1412                 ret = -EINVAL;
1413                 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1414                         break;
1415                 ret = -EFAULT;
1416                 if (copy_from_user(&fprog, argp, sizeof(fprog)))
1417                         break;
1418
1419                 ret = sk_attach_filter(&fprog, tun->socket.sk);
1420                 break;
1421
1422         case TUNDETACHFILTER:
1423                 /* Can be set only for TAPs */
1424                 ret = -EINVAL;
1425                 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1426                         break;
1427                 ret = sk_detach_filter(tun->socket.sk);
1428                 break;
1429
1430         default:
1431                 ret = -EINVAL;
1432                 break;
1433         }
1434
1435 unlock:
1436         rtnl_unlock();
1437         if (tun)
1438                 tun_put(tun);
1439         return ret;
1440 }
1441
1442 static long tun_chr_ioctl(struct file *file,
1443                           unsigned int cmd, unsigned long arg)
1444 {
1445         return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
1446 }
1447
1448 #ifdef CONFIG_COMPAT
1449 static long tun_chr_compat_ioctl(struct file *file,
1450                          unsigned int cmd, unsigned long arg)
1451 {
1452         switch (cmd) {
1453         case TUNSETIFF:
1454         case TUNGETIFF:
1455         case TUNSETTXFILTER:
1456         case TUNGETSNDBUF:
1457         case TUNSETSNDBUF:
1458         case SIOCGIFHWADDR:
1459         case SIOCSIFHWADDR:
1460                 arg = (unsigned long)compat_ptr(arg);
1461                 break;
1462         default:
1463                 arg = (compat_ulong_t)arg;
1464                 break;
1465         }
1466
1467         /*
1468          * compat_ifreq is shorter than ifreq, so we must not access beyond
1469          * the end of that structure. All fields that are used in this
1470          * driver are compatible though, we don't need to convert the
1471          * contents.
1472          */
1473         return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
1474 }
1475 #endif /* CONFIG_COMPAT */
1476
1477 static int tun_chr_fasync(int fd, struct file *file, int on)
1478 {
1479         struct tun_struct *tun = tun_get(file);
1480         int ret;
1481
1482         if (!tun)
1483                 return -EBADFD;
1484
1485         tun_debug(KERN_INFO, tun, "tun_chr_fasync %d\n", on);
1486
1487         if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
1488                 goto out;
1489
1490         if (on) {
1491                 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
1492                 if (ret)
1493                         goto out;
1494                 tun->flags |= TUN_FASYNC;
1495         } else
1496                 tun->flags &= ~TUN_FASYNC;
1497         ret = 0;
1498 out:
1499         tun_put(tun);
1500         return ret;
1501 }
1502
1503 static int tun_chr_open(struct inode *inode, struct file * file)
1504 {
1505         struct tun_file *tfile;
1506
1507         DBG1(KERN_INFO, "tunX: tun_chr_open\n");
1508
1509         tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
1510         if (!tfile)
1511                 return -ENOMEM;
1512         atomic_set(&tfile->count, 0);
1513         tfile->tun = NULL;
1514         tfile->net = get_net(current->nsproxy->net_ns);
1515         file->private_data = tfile;
1516         return 0;
1517 }
1518
1519 static int tun_chr_close(struct inode *inode, struct file *file)
1520 {
1521         struct tun_file *tfile = file->private_data;
1522         struct tun_struct *tun;
1523
1524         tun = __tun_get(tfile);
1525         if (tun) {
1526                 struct net_device *dev = tun->dev;
1527
1528                 tun_debug(KERN_INFO, tun, "tun_chr_close\n");
1529
1530                 __tun_detach(tun);
1531
1532                 /* If desirable, unregister the netdevice. */
1533                 if (!(tun->flags & TUN_PERSIST)) {
1534                         rtnl_lock();
1535                         if (dev->reg_state == NETREG_REGISTERED)
1536                                 unregister_netdevice(dev);
1537                         rtnl_unlock();
1538                 }
1539         }
1540
1541         tun = tfile->tun;
1542         if (tun)
1543                 sock_put(tun->socket.sk);
1544
1545         put_net(tfile->net);
1546         kfree(tfile);
1547
1548         return 0;
1549 }
1550
1551 static const struct file_operations tun_fops = {
1552         .owner  = THIS_MODULE,
1553         .llseek = no_llseek,
1554         .read  = do_sync_read,
1555         .aio_read  = tun_chr_aio_read,
1556         .write = do_sync_write,
1557         .aio_write = tun_chr_aio_write,
1558         .poll   = tun_chr_poll,
1559         .unlocked_ioctl = tun_chr_ioctl,
1560 #ifdef CONFIG_COMPAT
1561         .compat_ioctl = tun_chr_compat_ioctl,
1562 #endif
1563         .open   = tun_chr_open,
1564         .release = tun_chr_close,
1565         .fasync = tun_chr_fasync
1566 };
1567
1568 static struct miscdevice tun_miscdev = {
1569         .minor = TUN_MINOR,
1570         .name = "tun",
1571         .nodename = "net/tun",
1572         .fops = &tun_fops,
1573 };
1574
1575 /* ethtool interface */
1576
1577 static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1578 {
1579         cmd->supported          = 0;
1580         cmd->advertising        = 0;
1581         ethtool_cmd_speed_set(cmd, SPEED_10);
1582         cmd->duplex             = DUPLEX_FULL;
1583         cmd->port               = PORT_TP;
1584         cmd->phy_address        = 0;
1585         cmd->transceiver        = XCVR_INTERNAL;
1586         cmd->autoneg            = AUTONEG_DISABLE;
1587         cmd->maxtxpkt           = 0;
1588         cmd->maxrxpkt           = 0;
1589         return 0;
1590 }
1591
1592 static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1593 {
1594         struct tun_struct *tun = netdev_priv(dev);
1595
1596         strcpy(info->driver, DRV_NAME);
1597         strcpy(info->version, DRV_VERSION);
1598         strcpy(info->fw_version, "N/A");
1599
1600         switch (tun->flags & TUN_TYPE_MASK) {
1601         case TUN_TUN_DEV:
1602                 strcpy(info->bus_info, "tun");
1603                 break;
1604         case TUN_TAP_DEV:
1605                 strcpy(info->bus_info, "tap");
1606                 break;
1607         }
1608 }
1609
1610 static u32 tun_get_msglevel(struct net_device *dev)
1611 {
1612 #ifdef TUN_DEBUG
1613         struct tun_struct *tun = netdev_priv(dev);
1614         return tun->debug;
1615 #else
1616         return -EOPNOTSUPP;
1617 #endif
1618 }
1619
1620 static void tun_set_msglevel(struct net_device *dev, u32 value)
1621 {
1622 #ifdef TUN_DEBUG
1623         struct tun_struct *tun = netdev_priv(dev);
1624         tun->debug = value;
1625 #endif
1626 }
1627
1628 static const struct ethtool_ops tun_ethtool_ops = {
1629         .get_settings   = tun_get_settings,
1630         .get_drvinfo    = tun_get_drvinfo,
1631         .get_msglevel   = tun_get_msglevel,
1632         .set_msglevel   = tun_set_msglevel,
1633         .get_link       = ethtool_op_get_link,
1634 };
1635
1636
1637 static int __init tun_init(void)
1638 {
1639         int ret = 0;
1640
1641         pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
1642         pr_info("%s\n", DRV_COPYRIGHT);
1643
1644         ret = rtnl_link_register(&tun_link_ops);
1645         if (ret) {
1646                 pr_err("Can't register link_ops\n");
1647                 goto err_linkops;
1648         }
1649
1650         ret = misc_register(&tun_miscdev);
1651         if (ret) {
1652                 pr_err("Can't register misc device %d\n", TUN_MINOR);
1653                 goto err_misc;
1654         }
1655         return  0;
1656 err_misc:
1657         rtnl_link_unregister(&tun_link_ops);
1658 err_linkops:
1659         return ret;
1660 }
1661
1662 static void tun_cleanup(void)
1663 {
1664         misc_deregister(&tun_miscdev);
1665         rtnl_link_unregister(&tun_link_ops);
1666 }
1667
1668 /* Get an underlying socket object from tun file.  Returns error unless file is
1669  * attached to a device.  The returned object works like a packet socket, it
1670  * can be used for sock_sendmsg/sock_recvmsg.  The caller is responsible for
1671  * holding a reference to the file for as long as the socket is in use. */
1672 struct socket *tun_get_socket(struct file *file)
1673 {
1674         struct tun_struct *tun;
1675         if (file->f_op != &tun_fops)
1676                 return ERR_PTR(-EINVAL);
1677         tun = tun_get(file);
1678         if (!tun)
1679                 return ERR_PTR(-EBADFD);
1680         tun_put(tun);
1681         return &tun->socket;
1682 }
1683 EXPORT_SYMBOL_GPL(tun_get_socket);
1684
1685 module_init(tun_init);
1686 module_exit(tun_cleanup);
1687 MODULE_DESCRIPTION(DRV_DESCRIPTION);
1688 MODULE_AUTHOR(DRV_COPYRIGHT);
1689 MODULE_LICENSE("GPL");
1690 MODULE_ALIAS_MISCDEV(TUN_MINOR);
1691 MODULE_ALIAS("devname:net/tun");