2 * TUN - Universal TUN/TAP device driver.
3 * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
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.
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.
15 * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
21 * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
22 * Add TUNSETLINK ioctl to set the link encapsulation
24 * Mark Smith <markzzzsmith@yahoo.com.au>
25 * Use eth_random_addr() for tap MAC address.
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.
33 * Daniel Podlejski <underley@underley.eu.org>
34 * Modifications for 2.3.99-pre5 kernel.
37 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>"
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>
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>
72 #include <asm/uaccess.h>
74 /* Uncomment to enable debugging */
75 /* #define TUN_DEBUG 1 */
80 #define tun_debug(level, tun, fmt, args...) \
83 netdev_printk(level, tun->dev, fmt, ##args); \
85 #define DBG1(level, fmt, args...) \
88 printk(level fmt, ##args); \
91 #define tun_debug(level, tun, fmt, args...) \
94 netdev_printk(level, tun->dev, fmt, ##args); \
96 #define DBG1(level, fmt, args...) \
99 printk(level fmt, ##args); \
103 #define GOODCOPY_LEN 128
105 #define FLT_EXACT_COUNT 8
107 unsigned int count; /* Number of addrs. Zero means disabled */
108 u32 mask[2]; /* Mask of the hashed addrs */
109 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
112 /* 1024 is probably a high enough limit: modern hypervisors seem to support on
113 * the order of 100-200 CPUs so this leaves us some breathing space if we want
114 * to match a queue per guest CPU.
116 #define MAX_TAP_QUEUES 1024
118 /* A tun_file connects an open character device to a tuntap netdevice. It
119 * also contains all socket related strctures (except sock_fprog and tap_filter)
120 * to serve as one transmit queue for tuntap device. The sock_fprog and
121 * tap_filter were kept in tun_struct since they were used for filtering for the
122 * netdevice not for a specific queue (at least I didn't see the reqirement for
126 * The tun_file and tun_struct are loosely coupled, the pointer from on to the
127 * other can only be read while rcu_read_lock or rtnl_lock is held.
131 struct socket socket;
133 struct tun_struct __rcu *tun;
135 struct fasync_struct *fasync;
136 /* only used for fasnyc */
141 /* Since the socket were moved to tun_file, to preserve the behavior of persist
142 * device, socket fileter, sndbuf and vnet header size were restore when the
143 * file were attached to a persist device.
146 struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
147 unsigned int numqueues;
152 struct net_device *dev;
153 netdev_features_t set_features;
154 #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
155 NETIF_F_TSO6|NETIF_F_UFO)
159 struct tap_filter txflt;
160 struct sock_fprog fprog;
161 /* protected by rtnl lock */
162 bool filter_attached;
168 /* We try to identify a flow through its rxhash first. The reason that
169 * we do not check rxq no. is becuase some cards(e.g 82599), chooses
170 * the rxq based on the txq where the last packet of the flow comes. As
171 * the userspace application move between processors, we may get a
172 * different rxq no. here. If we could not get rxhash, then we would
173 * hope the rxq no. may help here.
175 static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb)
177 struct tun_struct *tun = netdev_priv(dev);
182 numqueues = tun->numqueues;
184 txq = skb_get_rxhash(skb);
186 /* use multiply and shift instead of expensive divide */
187 txq = ((u64)txq * numqueues) >> 32;
188 } else if (likely(skb_rx_queue_recorded(skb))) {
189 txq = skb_get_rx_queue(skb);
190 while (unlikely(txq >= numqueues))
198 static inline bool tun_not_capable(struct tun_struct *tun)
200 const struct cred *cred = current_cred();
202 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
203 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
204 !capable(CAP_NET_ADMIN);
207 static void tun_set_real_num_queues(struct tun_struct *tun)
209 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
210 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
213 static void __tun_detach(struct tun_file *tfile, bool clean)
215 struct tun_file *ntfile;
216 struct tun_struct *tun;
217 struct net_device *dev;
219 tun = rcu_dereference_protected(tfile->tun,
220 lockdep_rtnl_is_held());
222 u16 index = tfile->queue_index;
223 BUG_ON(index >= tun->numqueues);
226 rcu_assign_pointer(tun->tfiles[index],
227 tun->tfiles[tun->numqueues - 1]);
228 rcu_assign_pointer(tfile->tun, NULL);
229 ntfile = rcu_dereference_protected(tun->tfiles[index],
230 lockdep_rtnl_is_held());
231 ntfile->queue_index = index;
234 sock_put(&tfile->sk);
237 /* Drop read queue */
238 skb_queue_purge(&tfile->sk.sk_receive_queue);
239 tun_set_real_num_queues(tun);
241 if (tun->numqueues == 0 && !(tun->flags & TUN_PERSIST))
242 if (dev->reg_state == NETREG_REGISTERED)
243 unregister_netdevice(dev);
247 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
248 &tfile->socket.flags));
249 sk_release_kernel(&tfile->sk);
253 static void tun_detach(struct tun_file *tfile, bool clean)
256 __tun_detach(tfile, clean);
260 static void tun_detach_all(struct net_device *dev)
262 struct tun_struct *tun = netdev_priv(dev);
263 struct tun_file *tfile;
264 int i, n = tun->numqueues;
266 for (i = 0; i < n; i++) {
267 tfile = rcu_dereference_protected(tun->tfiles[i],
268 lockdep_rtnl_is_held());
270 wake_up_all(&tfile->wq.wait);
271 rcu_assign_pointer(tfile->tun, NULL);
274 BUG_ON(tun->numqueues != 0);
277 for (i = 0; i < n; i++) {
278 tfile = rcu_dereference_protected(tun->tfiles[i],
279 lockdep_rtnl_is_held());
280 /* Drop read queue */
281 skb_queue_purge(&tfile->sk.sk_receive_queue);
282 sock_put(&tfile->sk);
286 static int tun_attach(struct tun_struct *tun, struct file *file)
288 struct tun_file *tfile = file->private_data;
292 if (rcu_dereference_protected(tfile->tun, lockdep_rtnl_is_held()))
296 if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1)
300 if (tun->numqueues == MAX_TAP_QUEUES)
305 /* Re-attach the filter to presist device */
306 if (tun->filter_attached == true) {
307 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
311 tfile->queue_index = tun->numqueues;
312 rcu_assign_pointer(tfile->tun, tun);
313 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
314 sock_hold(&tfile->sk);
317 tun_set_real_num_queues(tun);
319 if (tun->numqueues == 1)
320 netif_carrier_on(tun->dev);
322 /* device is allowed to go away first, so no need to hold extra
330 static struct tun_struct *__tun_get(struct tun_file *tfile)
332 struct tun_struct *tun;
335 tun = rcu_dereference(tfile->tun);
343 static struct tun_struct *tun_get(struct file *file)
345 return __tun_get(file->private_data);
348 static void tun_put(struct tun_struct *tun)
354 static void addr_hash_set(u32 *mask, const u8 *addr)
356 int n = ether_crc(ETH_ALEN, addr) >> 26;
357 mask[n >> 5] |= (1 << (n & 31));
360 static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
362 int n = ether_crc(ETH_ALEN, addr) >> 26;
363 return mask[n >> 5] & (1 << (n & 31));
366 static int update_filter(struct tap_filter *filter, void __user *arg)
368 struct { u8 u[ETH_ALEN]; } *addr;
369 struct tun_filter uf;
370 int err, alen, n, nexact;
372 if (copy_from_user(&uf, arg, sizeof(uf)))
381 alen = ETH_ALEN * uf.count;
382 addr = kmalloc(alen, GFP_KERNEL);
386 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
391 /* The filter is updated without holding any locks. Which is
392 * perfectly safe. We disable it first and in the worst
393 * case we'll accept a few undesired packets. */
397 /* Use first set of addresses as an exact filter */
398 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
399 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
403 /* Remaining multicast addresses are hashed,
404 * unicast will leave the filter disabled. */
405 memset(filter->mask, 0, sizeof(filter->mask));
406 for (; n < uf.count; n++) {
407 if (!is_multicast_ether_addr(addr[n].u)) {
408 err = 0; /* no filter */
411 addr_hash_set(filter->mask, addr[n].u);
414 /* For ALLMULTI just set the mask to all ones.
415 * This overrides the mask populated above. */
416 if ((uf.flags & TUN_FLT_ALLMULTI))
417 memset(filter->mask, ~0, sizeof(filter->mask));
419 /* Now enable the filter */
421 filter->count = nexact;
423 /* Return the number of exact filters */
431 /* Returns: 0 - drop, !=0 - accept */
432 static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
434 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
436 struct ethhdr *eh = (struct ethhdr *) skb->data;
440 for (i = 0; i < filter->count; i++)
441 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
444 /* Inexact match (multicast only) */
445 if (is_multicast_ether_addr(eh->h_dest))
446 return addr_hash_test(filter->mask, eh->h_dest);
452 * Checks whether the packet is accepted or not.
453 * Returns: 0 - drop, !=0 - accept
455 static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
460 return run_filter(filter, skb);
463 /* Network device part of the driver */
465 static const struct ethtool_ops tun_ethtool_ops;
467 /* Net device detach from fd. */
468 static void tun_net_uninit(struct net_device *dev)
473 /* Net device open. */
474 static int tun_net_open(struct net_device *dev)
476 netif_tx_start_all_queues(dev);
480 /* Net device close. */
481 static int tun_net_close(struct net_device *dev)
483 netif_tx_stop_all_queues(dev);
487 /* Net device start xmit */
488 static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
490 struct tun_struct *tun = netdev_priv(dev);
491 int txq = skb->queue_mapping;
492 struct tun_file *tfile;
495 tfile = rcu_dereference(tun->tfiles[txq]);
497 /* Drop packet if interface is not attached */
498 if (txq >= tun->numqueues)
501 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
505 /* Drop if the filter does not like it.
506 * This is a noop if the filter is disabled.
507 * Filter can be enabled only for the TAP devices. */
508 if (!check_filter(&tun->txflt, skb))
511 if (tfile->socket.sk->sk_filter &&
512 sk_filter(tfile->socket.sk, skb))
515 /* Limit the number of packets queued by divining txq length with the
518 if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
519 >= dev->tx_queue_len / tun->numqueues){
520 if (!(tun->flags & TUN_ONE_QUEUE)) {
521 /* Normal queueing mode. */
522 /* Packet scheduler handles dropping of further packets. */
523 netif_stop_subqueue(dev, txq);
525 /* We won't see all dropped packets individually, so overrun
526 * error is more appropriate. */
527 dev->stats.tx_fifo_errors++;
529 /* Single queue mode.
530 * Driver handles dropping of all packets itself. */
535 /* Orphan the skb - required as we might hang on to it
536 * for indefinite time. */
537 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
542 skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
544 /* Notify and wake up reader process */
545 if (tfile->flags & TUN_FASYNC)
546 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
547 wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
548 POLLRDNORM | POLLRDBAND);
554 dev->stats.tx_dropped++;
560 static void tun_net_mclist(struct net_device *dev)
563 * This callback is supposed to deal with mc filter in
564 * _rx_ path and has nothing to do with the _tx_ path.
565 * In rx path we always accept everything userspace gives us.
570 #define MAX_MTU 65535
573 tun_net_change_mtu(struct net_device *dev, int new_mtu)
575 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
581 static netdev_features_t tun_net_fix_features(struct net_device *dev,
582 netdev_features_t features)
584 struct tun_struct *tun = netdev_priv(dev);
586 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
588 #ifdef CONFIG_NET_POLL_CONTROLLER
589 static void tun_poll_controller(struct net_device *dev)
592 * Tun only receives frames when:
593 * 1) the char device endpoint gets data from user space
594 * 2) the tun socket gets a sendmsg call from user space
595 * Since both of those are syncronous operations, we are guaranteed
596 * never to have pending data when we poll for it
597 * so theres nothing to do here but return.
598 * We need this though so netpoll recognizes us as an interface that
599 * supports polling, which enables bridge devices in virt setups to
600 * still use netconsole
605 static const struct net_device_ops tun_netdev_ops = {
606 .ndo_uninit = tun_net_uninit,
607 .ndo_open = tun_net_open,
608 .ndo_stop = tun_net_close,
609 .ndo_start_xmit = tun_net_xmit,
610 .ndo_change_mtu = tun_net_change_mtu,
611 .ndo_fix_features = tun_net_fix_features,
612 .ndo_select_queue = tun_select_queue,
613 #ifdef CONFIG_NET_POLL_CONTROLLER
614 .ndo_poll_controller = tun_poll_controller,
618 static const struct net_device_ops tap_netdev_ops = {
619 .ndo_uninit = tun_net_uninit,
620 .ndo_open = tun_net_open,
621 .ndo_stop = tun_net_close,
622 .ndo_start_xmit = tun_net_xmit,
623 .ndo_change_mtu = tun_net_change_mtu,
624 .ndo_fix_features = tun_net_fix_features,
625 .ndo_set_rx_mode = tun_net_mclist,
626 .ndo_set_mac_address = eth_mac_addr,
627 .ndo_validate_addr = eth_validate_addr,
628 .ndo_select_queue = tun_select_queue,
629 #ifdef CONFIG_NET_POLL_CONTROLLER
630 .ndo_poll_controller = tun_poll_controller,
634 /* Initialize net device. */
635 static void tun_net_init(struct net_device *dev)
637 struct tun_struct *tun = netdev_priv(dev);
639 switch (tun->flags & TUN_TYPE_MASK) {
641 dev->netdev_ops = &tun_netdev_ops;
643 /* Point-to-Point TUN Device */
644 dev->hard_header_len = 0;
648 /* Zero header length */
649 dev->type = ARPHRD_NONE;
650 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
651 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
655 dev->netdev_ops = &tap_netdev_ops;
656 /* Ethernet TAP Device */
658 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
660 eth_hw_addr_random(dev);
662 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
667 /* Character device part */
670 static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
672 struct tun_file *tfile = file->private_data;
673 struct tun_struct *tun = __tun_get(tfile);
675 unsigned int mask = 0;
680 sk = tfile->socket.sk;
682 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
684 poll_wait(file, &tfile->wq.wait, wait);
686 if (!skb_queue_empty(&sk->sk_receive_queue))
687 mask |= POLLIN | POLLRDNORM;
689 if (sock_writeable(sk) ||
690 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
692 mask |= POLLOUT | POLLWRNORM;
694 if (tun->dev->reg_state != NETREG_REGISTERED)
701 /* prepad is the amount to reserve at front. len is length after that.
702 * linear is a hint as to how much to copy (usually headers). */
703 static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
704 size_t prepad, size_t len,
705 size_t linear, int noblock)
707 struct sock *sk = tfile->socket.sk;
711 /* Under a page? Don't bother with paged skb. */
712 if (prepad + len < PAGE_SIZE || !linear)
715 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
720 skb_reserve(skb, prepad);
721 skb_put(skb, linear);
722 skb->data_len = len - linear;
723 skb->len += len - linear;
728 /* set skb frags from iovec, this can move to core network code for reuse */
729 static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
730 int offset, size_t count)
732 int len = iov_length(from, count) - offset;
733 int copy = skb_headlen(skb);
734 int size, offset1 = 0;
737 /* Skip over from offset */
738 while (count && (offset >= from->iov_len)) {
739 offset -= from->iov_len;
744 /* copy up to skb headlen */
745 while (count && (copy > 0)) {
746 size = min_t(unsigned int, copy, from->iov_len - offset);
747 if (copy_from_user(skb->data + offset1, from->iov_base + offset,
764 struct page *page[MAX_SKB_FRAGS];
767 unsigned long truesize;
769 len = from->iov_len - offset;
775 base = (unsigned long)from->iov_base + offset;
776 size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
777 if (i + size > MAX_SKB_FRAGS)
779 num_pages = get_user_pages_fast(base, size, 0, &page[i]);
780 if (num_pages != size) {
781 for (i = 0; i < num_pages; i++)
785 truesize = size * PAGE_SIZE;
786 skb->data_len += len;
788 skb->truesize += truesize;
789 atomic_add(truesize, &skb->sk->sk_wmem_alloc);
791 int off = base & ~PAGE_MASK;
792 int size = min_t(int, len, PAGE_SIZE - off);
793 __skb_fill_page_desc(skb, i, page[i], off, size);
794 skb_shinfo(skb)->nr_frags++;
795 /* increase sk_wmem_alloc */
806 /* Get packet from user space buffer */
807 static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
808 void *msg_control, const struct iovec *iv,
809 size_t total_len, size_t count, int noblock)
811 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
813 size_t len = total_len, align = NET_SKB_PAD;
814 struct virtio_net_hdr gso = { 0 };
817 bool zerocopy = false;
820 if (!(tun->flags & TUN_NO_PI)) {
821 if ((len -= sizeof(pi)) > total_len)
824 if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
826 offset += sizeof(pi);
829 if (tun->flags & TUN_VNET_HDR) {
830 if ((len -= tun->vnet_hdr_sz) > total_len)
833 if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
836 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
837 gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
838 gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
840 if (gso.hdr_len > len)
842 offset += tun->vnet_hdr_sz;
845 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
846 align += NET_IP_ALIGN;
847 if (unlikely(len < ETH_HLEN ||
848 (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
856 /* Userspace may produce vectors with count greater than
857 * MAX_SKB_FRAGS, so we need to linearize parts of the skb
858 * to let the rest of data to be fit in the frags.
860 if (count > MAX_SKB_FRAGS) {
861 copylen = iov_length(iv, count - MAX_SKB_FRAGS);
862 if (copylen < offset)
868 /* There are 256 bytes to be copied in skb, so there is enough
869 * room for skb expand head in case it is used.
870 * The rest of the buffer is mapped from userspace.
872 if (copylen < gso.hdr_len)
873 copylen = gso.hdr_len;
875 copylen = GOODCOPY_LEN;
879 skb = tun_alloc_skb(tfile, align, copylen, gso.hdr_len, noblock);
881 if (PTR_ERR(skb) != -EAGAIN)
882 tun->dev->stats.rx_dropped++;
887 err = zerocopy_sg_from_iovec(skb, iv, offset, count);
889 err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
892 tun->dev->stats.rx_dropped++;
897 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
898 if (!skb_partial_csum_set(skb, gso.csum_start,
900 tun->dev->stats.rx_frame_errors++;
906 switch (tun->flags & TUN_TYPE_MASK) {
908 if (tun->flags & TUN_NO_PI) {
909 switch (skb->data[0] & 0xf0) {
911 pi.proto = htons(ETH_P_IP);
914 pi.proto = htons(ETH_P_IPV6);
917 tun->dev->stats.rx_dropped++;
923 skb_reset_mac_header(skb);
924 skb->protocol = pi.proto;
928 skb->protocol = eth_type_trans(skb, tun->dev);
932 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
934 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
935 case VIRTIO_NET_HDR_GSO_TCPV4:
936 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
938 case VIRTIO_NET_HDR_GSO_TCPV6:
939 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
941 case VIRTIO_NET_HDR_GSO_UDP:
942 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
945 tun->dev->stats.rx_frame_errors++;
950 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
951 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
953 skb_shinfo(skb)->gso_size = gso.gso_size;
954 if (skb_shinfo(skb)->gso_size == 0) {
955 tun->dev->stats.rx_frame_errors++;
960 /* Header must be checked, and gso_segs computed. */
961 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
962 skb_shinfo(skb)->gso_segs = 0;
965 /* copy skb_ubuf_info for callback when skb has no error */
967 skb_shinfo(skb)->destructor_arg = msg_control;
968 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
973 tun->dev->stats.rx_packets++;
974 tun->dev->stats.rx_bytes += len;
979 static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
980 unsigned long count, loff_t pos)
982 struct file *file = iocb->ki_filp;
983 struct tun_struct *tun = tun_get(file);
984 struct tun_file *tfile = file->private_data;
990 tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
992 result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
993 count, file->f_flags & O_NONBLOCK);
999 /* Put packet to the user space buffer */
1000 static ssize_t tun_put_user(struct tun_struct *tun,
1001 struct tun_file *tfile,
1002 struct sk_buff *skb,
1003 const struct iovec *iv, int len)
1005 struct tun_pi pi = { 0, skb->protocol };
1008 if (!(tun->flags & TUN_NO_PI)) {
1009 if ((len -= sizeof(pi)) < 0)
1012 if (len < skb->len) {
1013 /* Packet will be striped */
1014 pi.flags |= TUN_PKT_STRIP;
1017 if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
1019 total += sizeof(pi);
1022 if (tun->flags & TUN_VNET_HDR) {
1023 struct virtio_net_hdr gso = { 0 }; /* no info leak */
1024 if ((len -= tun->vnet_hdr_sz) < 0)
1027 if (skb_is_gso(skb)) {
1028 struct skb_shared_info *sinfo = skb_shinfo(skb);
1030 /* This is a hint as to how much should be linear. */
1031 gso.hdr_len = skb_headlen(skb);
1032 gso.gso_size = sinfo->gso_size;
1033 if (sinfo->gso_type & SKB_GSO_TCPV4)
1034 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1035 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1036 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1037 else if (sinfo->gso_type & SKB_GSO_UDP)
1038 gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
1040 pr_err("unexpected GSO type: "
1041 "0x%x, gso_size %d, hdr_len %d\n",
1042 sinfo->gso_type, gso.gso_size,
1044 print_hex_dump(KERN_ERR, "tun: ",
1047 min((int)gso.hdr_len, 64), true);
1051 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1052 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1054 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1056 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1057 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
1058 gso.csum_start = skb_checksum_start_offset(skb);
1059 gso.csum_offset = skb->csum_offset;
1060 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1061 gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
1062 } /* else everything is zero */
1064 if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
1067 total += tun->vnet_hdr_sz;
1070 len = min_t(int, skb->len, len);
1072 skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
1075 tun->dev->stats.tx_packets++;
1076 tun->dev->stats.tx_bytes += len;
1081 static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
1082 struct kiocb *iocb, const struct iovec *iv,
1083 ssize_t len, int noblock)
1085 DECLARE_WAITQUEUE(wait, current);
1086 struct sk_buff *skb;
1089 tun_debug(KERN_INFO, tun, "tun_chr_read\n");
1091 if (unlikely(!noblock))
1092 add_wait_queue(&tfile->wq.wait, &wait);
1094 current->state = TASK_INTERRUPTIBLE;
1096 /* Read frames from the queue */
1097 if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
1102 if (signal_pending(current)) {
1106 if (tun->dev->reg_state != NETREG_REGISTERED) {
1111 /* Nothing to read, let's sleep */
1115 netif_wake_subqueue(tun->dev, tfile->queue_index);
1117 ret = tun_put_user(tun, tfile, skb, iv, len);
1122 current->state = TASK_RUNNING;
1123 if (unlikely(!noblock))
1124 remove_wait_queue(&tfile->wq.wait, &wait);
1129 static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
1130 unsigned long count, loff_t pos)
1132 struct file *file = iocb->ki_filp;
1133 struct tun_file *tfile = file->private_data;
1134 struct tun_struct *tun = __tun_get(tfile);
1139 len = iov_length(iv, count);
1145 ret = tun_do_read(tun, tfile, iocb, iv, len,
1146 file->f_flags & O_NONBLOCK);
1147 ret = min_t(ssize_t, ret, len);
1153 static void tun_setup(struct net_device *dev)
1155 struct tun_struct *tun = netdev_priv(dev);
1157 tun->owner = INVALID_UID;
1158 tun->group = INVALID_GID;
1160 dev->ethtool_ops = &tun_ethtool_ops;
1161 dev->destructor = free_netdev;
1164 /* Trivial set of netlink ops to allow deleting tun or tap
1165 * device with netlink.
1167 static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1172 static struct rtnl_link_ops tun_link_ops __read_mostly = {
1174 .priv_size = sizeof(struct tun_struct),
1176 .validate = tun_validate,
1179 static void tun_sock_write_space(struct sock *sk)
1181 struct tun_file *tfile;
1182 wait_queue_head_t *wqueue;
1184 if (!sock_writeable(sk))
1187 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
1190 wqueue = sk_sleep(sk);
1191 if (wqueue && waitqueue_active(wqueue))
1192 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
1193 POLLWRNORM | POLLWRBAND);
1195 tfile = container_of(sk, struct tun_file, sk);
1196 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
1199 static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
1200 struct msghdr *m, size_t total_len)
1203 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1204 struct tun_struct *tun = __tun_get(tfile);
1208 ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
1209 m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
1215 static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
1216 struct msghdr *m, size_t total_len,
1219 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1220 struct tun_struct *tun = __tun_get(tfile);
1226 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
1228 ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
1229 flags & MSG_DONTWAIT);
1230 if (ret > total_len) {
1231 m->msg_flags |= MSG_TRUNC;
1232 ret = flags & MSG_TRUNC ? ret : total_len;
1238 static int tun_release(struct socket *sock)
1245 /* Ops structure to mimic raw sockets with tun */
1246 static const struct proto_ops tun_socket_ops = {
1247 .sendmsg = tun_sendmsg,
1248 .recvmsg = tun_recvmsg,
1249 .release = tun_release,
1252 static struct proto tun_proto = {
1254 .owner = THIS_MODULE,
1255 .obj_size = sizeof(struct tun_file),
1258 static int tun_flags(struct tun_struct *tun)
1262 if (tun->flags & TUN_TUN_DEV)
1267 if (tun->flags & TUN_NO_PI)
1270 if (tun->flags & TUN_ONE_QUEUE)
1271 flags |= IFF_ONE_QUEUE;
1273 if (tun->flags & TUN_VNET_HDR)
1274 flags |= IFF_VNET_HDR;
1276 if (tun->flags & TUN_TAP_MQ)
1277 flags |= IFF_MULTI_QUEUE;
1282 static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1285 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1286 return sprintf(buf, "0x%x\n", tun_flags(tun));
1289 static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1292 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1293 return uid_valid(tun->owner)?
1294 sprintf(buf, "%u\n",
1295 from_kuid_munged(current_user_ns(), tun->owner)):
1296 sprintf(buf, "-1\n");
1299 static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1302 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1303 return gid_valid(tun->group) ?
1304 sprintf(buf, "%u\n",
1305 from_kgid_munged(current_user_ns(), tun->group)):
1306 sprintf(buf, "-1\n");
1309 static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1310 static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1311 static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1313 static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
1315 struct tun_struct *tun;
1316 struct tun_file *tfile = file->private_data;
1317 struct net_device *dev;
1320 dev = __dev_get_by_name(net, ifr->ifr_name);
1322 if (ifr->ifr_flags & IFF_TUN_EXCL)
1324 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
1325 tun = netdev_priv(dev);
1326 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1327 tun = netdev_priv(dev);
1331 if (tun_not_capable(tun))
1333 err = security_tun_dev_attach(tfile->socket.sk);
1337 err = tun_attach(tun, file);
1343 unsigned long flags = 0;
1345 if (!capable(CAP_NET_ADMIN))
1347 err = security_tun_dev_create();
1352 if (ifr->ifr_flags & IFF_TUN) {
1354 flags |= TUN_TUN_DEV;
1356 } else if (ifr->ifr_flags & IFF_TAP) {
1358 flags |= TUN_TAP_DEV;
1364 name = ifr->ifr_name;
1366 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
1368 MAX_TAP_QUEUES, MAX_TAP_QUEUES);
1372 dev_net_set(dev, net);
1373 dev->rtnl_link_ops = &tun_link_ops;
1375 tun = netdev_priv(dev);
1378 tun->txflt.count = 0;
1379 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
1381 tun->filter_attached = false;
1382 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
1384 security_tun_dev_post_create(&tfile->sk);
1388 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
1390 dev->features = dev->hw_features;
1392 err = register_netdevice(tun->dev);
1396 if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1397 device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1398 device_create_file(&tun->dev->dev, &dev_attr_group))
1399 pr_err("Failed to create tun sysfs files\n");
1401 err = tun_attach(tun, file);
1406 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
1408 if (ifr->ifr_flags & IFF_NO_PI)
1409 tun->flags |= TUN_NO_PI;
1411 tun->flags &= ~TUN_NO_PI;
1413 if (ifr->ifr_flags & IFF_ONE_QUEUE)
1414 tun->flags |= TUN_ONE_QUEUE;
1416 tun->flags &= ~TUN_ONE_QUEUE;
1418 if (ifr->ifr_flags & IFF_VNET_HDR)
1419 tun->flags |= TUN_VNET_HDR;
1421 tun->flags &= ~TUN_VNET_HDR;
1423 if (ifr->ifr_flags & IFF_MULTI_QUEUE)
1424 tun->flags |= TUN_TAP_MQ;
1426 tun->flags &= ~TUN_TAP_MQ;
1428 /* Make sure persistent devices do not get stuck in
1431 if (netif_running(tun->dev))
1432 netif_tx_wake_all_queues(tun->dev);
1434 strcpy(ifr->ifr_name, tun->dev->name);
1442 static int tun_get_iff(struct net *net, struct tun_struct *tun,
1445 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
1447 strcpy(ifr->ifr_name, tun->dev->name);
1449 ifr->ifr_flags = tun_flags(tun);
1454 /* This is like a cut-down ethtool ops, except done via tun fd so no
1455 * privs required. */
1456 static int set_offload(struct tun_struct *tun, unsigned long arg)
1458 netdev_features_t features = 0;
1460 if (arg & TUN_F_CSUM) {
1461 features |= NETIF_F_HW_CSUM;
1464 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
1465 if (arg & TUN_F_TSO_ECN) {
1466 features |= NETIF_F_TSO_ECN;
1467 arg &= ~TUN_F_TSO_ECN;
1469 if (arg & TUN_F_TSO4)
1470 features |= NETIF_F_TSO;
1471 if (arg & TUN_F_TSO6)
1472 features |= NETIF_F_TSO6;
1473 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
1476 if (arg & TUN_F_UFO) {
1477 features |= NETIF_F_UFO;
1482 /* This gives the user a way to test for new features in future by
1483 * trying to set them. */
1487 tun->set_features = features;
1488 netdev_update_features(tun->dev);
1493 static void tun_detach_filter(struct tun_struct *tun, int n)
1496 struct tun_file *tfile;
1498 for (i = 0; i < n; i++) {
1499 tfile = rcu_dereference_protected(tun->tfiles[i],
1500 lockdep_rtnl_is_held());
1501 sk_detach_filter(tfile->socket.sk);
1504 tun->filter_attached = false;
1507 static int tun_attach_filter(struct tun_struct *tun)
1510 struct tun_file *tfile;
1512 for (i = 0; i < tun->numqueues; i++) {
1513 tfile = rcu_dereference_protected(tun->tfiles[i],
1514 lockdep_rtnl_is_held());
1515 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1517 tun_detach_filter(tun, i);
1522 tun->filter_attached = true;
1526 static void tun_set_sndbuf(struct tun_struct *tun)
1528 struct tun_file *tfile;
1531 for (i = 0; i < tun->numqueues; i++) {
1532 tfile = rcu_dereference_protected(tun->tfiles[i],
1533 lockdep_rtnl_is_held());
1534 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1538 static int tun_set_queue(struct file *file, struct ifreq *ifr)
1540 struct tun_file *tfile = file->private_data;
1541 struct tun_struct *tun;
1542 struct net_device *dev;
1547 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
1548 dev = __dev_get_by_name(tfile->net, ifr->ifr_name);
1554 tun = netdev_priv(dev);
1555 if (dev->netdev_ops != &tap_netdev_ops &&
1556 dev->netdev_ops != &tun_netdev_ops)
1558 else if (tun_not_capable(tun))
1561 ret = tun_attach(tun, file);
1562 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE)
1563 __tun_detach(tfile, false);
1572 static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1573 unsigned long arg, int ifreq_len)
1575 struct tun_file *tfile = file->private_data;
1576 struct tun_struct *tun;
1577 void __user* argp = (void __user*)arg;
1585 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
1586 if (copy_from_user(&ifr, argp, ifreq_len))
1589 memset(&ifr, 0, sizeof(ifr));
1591 if (cmd == TUNGETFEATURES) {
1592 /* Currently this just means: "what IFF flags are valid?".
1593 * This is needed because we never checked for invalid flags on
1595 return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1596 IFF_VNET_HDR | IFF_MULTI_QUEUE,
1597 (unsigned int __user*)argp);
1598 } else if (cmd == TUNSETQUEUE)
1599 return tun_set_queue(file, &ifr);
1604 tun = __tun_get(tfile);
1605 if (cmd == TUNSETIFF && !tun) {
1606 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1608 ret = tun_set_iff(tfile->net, file, &ifr);
1613 if (copy_to_user(argp, &ifr, ifreq_len))
1622 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
1627 ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
1631 if (copy_to_user(argp, &ifr, ifreq_len))
1636 /* Disable/Enable checksum */
1638 /* [unimplemented] */
1639 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
1640 arg ? "disabled" : "enabled");
1644 /* Disable/Enable persist mode. Keep an extra reference to the
1645 * module to prevent the module being unprobed.
1648 tun->flags |= TUN_PERSIST;
1649 __module_get(THIS_MODULE);
1651 tun->flags &= ~TUN_PERSIST;
1652 module_put(THIS_MODULE);
1655 tun_debug(KERN_INFO, tun, "persist %s\n",
1656 arg ? "enabled" : "disabled");
1660 /* Set owner of the device */
1661 owner = make_kuid(current_user_ns(), arg);
1662 if (!uid_valid(owner)) {
1667 tun_debug(KERN_INFO, tun, "owner set to %u\n",
1668 from_kuid(&init_user_ns, tun->owner));
1672 /* Set group of the device */
1673 group = make_kgid(current_user_ns(), arg);
1674 if (!gid_valid(group)) {
1679 tun_debug(KERN_INFO, tun, "group set to %u\n",
1680 from_kgid(&init_user_ns, tun->group));
1684 /* Only allow setting the type when the interface is down */
1685 if (tun->dev->flags & IFF_UP) {
1686 tun_debug(KERN_INFO, tun,
1687 "Linktype set failed because interface is up\n");
1690 tun->dev->type = (int) arg;
1691 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
1703 ret = set_offload(tun, arg);
1706 case TUNSETTXFILTER:
1707 /* Can be set only for TAPs */
1709 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1711 ret = update_filter(&tun->txflt, (void __user *)arg);
1715 /* Get hw address */
1716 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1717 ifr.ifr_hwaddr.sa_family = tun->dev->type;
1718 if (copy_to_user(argp, &ifr, ifreq_len))
1723 /* Set hw address */
1724 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
1725 ifr.ifr_hwaddr.sa_data);
1727 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
1731 sndbuf = tfile->socket.sk->sk_sndbuf;
1732 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
1737 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
1742 tun->sndbuf = sndbuf;
1743 tun_set_sndbuf(tun);
1746 case TUNGETVNETHDRSZ:
1747 vnet_hdr_sz = tun->vnet_hdr_sz;
1748 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
1752 case TUNSETVNETHDRSZ:
1753 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
1757 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
1762 tun->vnet_hdr_sz = vnet_hdr_sz;
1765 case TUNATTACHFILTER:
1766 /* Can be set only for TAPs */
1768 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1771 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
1774 ret = tun_attach_filter(tun);
1777 case TUNDETACHFILTER:
1778 /* Can be set only for TAPs */
1780 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1783 tun_detach_filter(tun, tun->numqueues);
1798 static long tun_chr_ioctl(struct file *file,
1799 unsigned int cmd, unsigned long arg)
1801 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
1804 #ifdef CONFIG_COMPAT
1805 static long tun_chr_compat_ioctl(struct file *file,
1806 unsigned int cmd, unsigned long arg)
1811 case TUNSETTXFILTER:
1816 arg = (unsigned long)compat_ptr(arg);
1819 arg = (compat_ulong_t)arg;
1824 * compat_ifreq is shorter than ifreq, so we must not access beyond
1825 * the end of that structure. All fields that are used in this
1826 * driver are compatible though, we don't need to convert the
1829 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
1831 #endif /* CONFIG_COMPAT */
1833 static int tun_chr_fasync(int fd, struct file *file, int on)
1835 struct tun_file *tfile = file->private_data;
1838 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
1842 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
1845 tfile->flags |= TUN_FASYNC;
1847 tfile->flags &= ~TUN_FASYNC;
1853 static int tun_chr_open(struct inode *inode, struct file * file)
1855 struct tun_file *tfile;
1857 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
1859 tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
1863 rcu_assign_pointer(tfile->tun, NULL);
1864 tfile->net = get_net(current->nsproxy->net_ns);
1867 rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
1868 init_waitqueue_head(&tfile->wq.wait);
1870 tfile->socket.file = file;
1871 tfile->socket.ops = &tun_socket_ops;
1873 sock_init_data(&tfile->socket, &tfile->sk);
1874 sk_change_net(&tfile->sk, tfile->net);
1876 tfile->sk.sk_write_space = tun_sock_write_space;
1877 tfile->sk.sk_sndbuf = INT_MAX;
1879 file->private_data = tfile;
1880 set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
1885 static int tun_chr_close(struct inode *inode, struct file *file)
1887 struct tun_file *tfile = file->private_data;
1888 struct net *net = tfile->net;
1890 tun_detach(tfile, true);
1896 static const struct file_operations tun_fops = {
1897 .owner = THIS_MODULE,
1898 .llseek = no_llseek,
1899 .read = do_sync_read,
1900 .aio_read = tun_chr_aio_read,
1901 .write = do_sync_write,
1902 .aio_write = tun_chr_aio_write,
1903 .poll = tun_chr_poll,
1904 .unlocked_ioctl = tun_chr_ioctl,
1905 #ifdef CONFIG_COMPAT
1906 .compat_ioctl = tun_chr_compat_ioctl,
1908 .open = tun_chr_open,
1909 .release = tun_chr_close,
1910 .fasync = tun_chr_fasync
1913 static struct miscdevice tun_miscdev = {
1916 .nodename = "net/tun",
1920 /* ethtool interface */
1922 static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1925 cmd->advertising = 0;
1926 ethtool_cmd_speed_set(cmd, SPEED_10);
1927 cmd->duplex = DUPLEX_FULL;
1928 cmd->port = PORT_TP;
1929 cmd->phy_address = 0;
1930 cmd->transceiver = XCVR_INTERNAL;
1931 cmd->autoneg = AUTONEG_DISABLE;
1937 static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1939 struct tun_struct *tun = netdev_priv(dev);
1941 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1942 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1944 switch (tun->flags & TUN_TYPE_MASK) {
1946 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
1949 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
1954 static u32 tun_get_msglevel(struct net_device *dev)
1957 struct tun_struct *tun = netdev_priv(dev);
1964 static void tun_set_msglevel(struct net_device *dev, u32 value)
1967 struct tun_struct *tun = netdev_priv(dev);
1972 static const struct ethtool_ops tun_ethtool_ops = {
1973 .get_settings = tun_get_settings,
1974 .get_drvinfo = tun_get_drvinfo,
1975 .get_msglevel = tun_get_msglevel,
1976 .set_msglevel = tun_set_msglevel,
1977 .get_link = ethtool_op_get_link,
1981 static int __init tun_init(void)
1985 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
1986 pr_info("%s\n", DRV_COPYRIGHT);
1988 ret = rtnl_link_register(&tun_link_ops);
1990 pr_err("Can't register link_ops\n");
1994 ret = misc_register(&tun_miscdev);
1996 pr_err("Can't register misc device %d\n", TUN_MINOR);
2001 rtnl_link_unregister(&tun_link_ops);
2006 static void tun_cleanup(void)
2008 misc_deregister(&tun_miscdev);
2009 rtnl_link_unregister(&tun_link_ops);
2012 /* Get an underlying socket object from tun file. Returns error unless file is
2013 * attached to a device. The returned object works like a packet socket, it
2014 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
2015 * holding a reference to the file for as long as the socket is in use. */
2016 struct socket *tun_get_socket(struct file *file)
2018 struct tun_file *tfile;
2019 if (file->f_op != &tun_fops)
2020 return ERR_PTR(-EINVAL);
2021 tfile = file->private_data;
2023 return ERR_PTR(-EBADFD);
2024 return &tfile->socket;
2026 EXPORT_SYMBOL_GPL(tun_get_socket);
2028 module_init(tun_init);
2029 module_exit(tun_cleanup);
2030 MODULE_DESCRIPTION(DRV_DESCRIPTION);
2031 MODULE_AUTHOR(DRV_COPYRIGHT);
2032 MODULE_LICENSE("GPL");
2033 MODULE_ALIAS_MISCDEV(TUN_MINOR);
2034 MODULE_ALIAS("devname:net/tun");