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 void tun_set_real_num_queues(struct tun_struct *tun)
200 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
201 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
204 static void __tun_detach(struct tun_file *tfile, bool clean)
206 struct tun_file *ntfile;
207 struct tun_struct *tun;
208 struct net_device *dev;
210 tun = rcu_dereference_protected(tfile->tun,
211 lockdep_rtnl_is_held());
213 u16 index = tfile->queue_index;
214 BUG_ON(index >= tun->numqueues);
217 rcu_assign_pointer(tun->tfiles[index],
218 tun->tfiles[tun->numqueues - 1]);
219 rcu_assign_pointer(tfile->tun, NULL);
220 ntfile = rcu_dereference_protected(tun->tfiles[index],
221 lockdep_rtnl_is_held());
222 ntfile->queue_index = index;
225 sock_put(&tfile->sk);
228 /* Drop read queue */
229 skb_queue_purge(&tfile->sk.sk_receive_queue);
230 tun_set_real_num_queues(tun);
232 if (tun->numqueues == 0 && !(tun->flags & TUN_PERSIST))
233 if (dev->reg_state == NETREG_REGISTERED)
234 unregister_netdevice(dev);
238 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
239 &tfile->socket.flags));
240 sk_release_kernel(&tfile->sk);
244 static void tun_detach(struct tun_file *tfile, bool clean)
247 __tun_detach(tfile, clean);
251 static void tun_detach_all(struct net_device *dev)
253 struct tun_struct *tun = netdev_priv(dev);
254 struct tun_file *tfile;
255 int i, n = tun->numqueues;
257 for (i = 0; i < n; i++) {
258 tfile = rcu_dereference_protected(tun->tfiles[i],
259 lockdep_rtnl_is_held());
261 wake_up_all(&tfile->wq.wait);
262 rcu_assign_pointer(tfile->tun, NULL);
265 BUG_ON(tun->numqueues != 0);
268 for (i = 0; i < n; i++) {
269 tfile = rcu_dereference_protected(tun->tfiles[i],
270 lockdep_rtnl_is_held());
271 /* Drop read queue */
272 skb_queue_purge(&tfile->sk.sk_receive_queue);
273 sock_put(&tfile->sk);
277 static int tun_attach(struct tun_struct *tun, struct file *file)
279 struct tun_file *tfile = file->private_data;
283 if (rcu_dereference_protected(tfile->tun, lockdep_rtnl_is_held()))
287 if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1)
291 if (tun->numqueues == MAX_TAP_QUEUES)
296 /* Re-attach the filter to presist device */
297 if (tun->filter_attached == true) {
298 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
302 tfile->queue_index = tun->numqueues;
303 rcu_assign_pointer(tfile->tun, tun);
304 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
305 sock_hold(&tfile->sk);
308 tun_set_real_num_queues(tun);
310 if (tun->numqueues == 1)
311 netif_carrier_on(tun->dev);
313 /* device is allowed to go away first, so no need to hold extra
321 static struct tun_struct *__tun_get(struct tun_file *tfile)
323 struct tun_struct *tun;
326 tun = rcu_dereference(tfile->tun);
334 static struct tun_struct *tun_get(struct file *file)
336 return __tun_get(file->private_data);
339 static void tun_put(struct tun_struct *tun)
345 static void addr_hash_set(u32 *mask, const u8 *addr)
347 int n = ether_crc(ETH_ALEN, addr) >> 26;
348 mask[n >> 5] |= (1 << (n & 31));
351 static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
353 int n = ether_crc(ETH_ALEN, addr) >> 26;
354 return mask[n >> 5] & (1 << (n & 31));
357 static int update_filter(struct tap_filter *filter, void __user *arg)
359 struct { u8 u[ETH_ALEN]; } *addr;
360 struct tun_filter uf;
361 int err, alen, n, nexact;
363 if (copy_from_user(&uf, arg, sizeof(uf)))
372 alen = ETH_ALEN * uf.count;
373 addr = kmalloc(alen, GFP_KERNEL);
377 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
382 /* The filter is updated without holding any locks. Which is
383 * perfectly safe. We disable it first and in the worst
384 * case we'll accept a few undesired packets. */
388 /* Use first set of addresses as an exact filter */
389 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
390 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
394 /* Remaining multicast addresses are hashed,
395 * unicast will leave the filter disabled. */
396 memset(filter->mask, 0, sizeof(filter->mask));
397 for (; n < uf.count; n++) {
398 if (!is_multicast_ether_addr(addr[n].u)) {
399 err = 0; /* no filter */
402 addr_hash_set(filter->mask, addr[n].u);
405 /* For ALLMULTI just set the mask to all ones.
406 * This overrides the mask populated above. */
407 if ((uf.flags & TUN_FLT_ALLMULTI))
408 memset(filter->mask, ~0, sizeof(filter->mask));
410 /* Now enable the filter */
412 filter->count = nexact;
414 /* Return the number of exact filters */
422 /* Returns: 0 - drop, !=0 - accept */
423 static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
425 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
427 struct ethhdr *eh = (struct ethhdr *) skb->data;
431 for (i = 0; i < filter->count; i++)
432 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
435 /* Inexact match (multicast only) */
436 if (is_multicast_ether_addr(eh->h_dest))
437 return addr_hash_test(filter->mask, eh->h_dest);
443 * Checks whether the packet is accepted or not.
444 * Returns: 0 - drop, !=0 - accept
446 static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
451 return run_filter(filter, skb);
454 /* Network device part of the driver */
456 static const struct ethtool_ops tun_ethtool_ops;
458 /* Net device detach from fd. */
459 static void tun_net_uninit(struct net_device *dev)
464 /* Net device open. */
465 static int tun_net_open(struct net_device *dev)
467 netif_tx_start_all_queues(dev);
471 /* Net device close. */
472 static int tun_net_close(struct net_device *dev)
474 netif_tx_stop_all_queues(dev);
478 /* Net device start xmit */
479 static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
481 struct tun_struct *tun = netdev_priv(dev);
482 int txq = skb->queue_mapping;
483 struct tun_file *tfile;
486 tfile = rcu_dereference(tun->tfiles[txq]);
488 /* Drop packet if interface is not attached */
489 if (txq >= tun->numqueues)
492 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
496 /* Drop if the filter does not like it.
497 * This is a noop if the filter is disabled.
498 * Filter can be enabled only for the TAP devices. */
499 if (!check_filter(&tun->txflt, skb))
502 if (tfile->socket.sk->sk_filter &&
503 sk_filter(tfile->socket.sk, skb))
506 /* Limit the number of packets queued by divining txq length with the
509 if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
510 >= dev->tx_queue_len / tun->numqueues){
511 if (!(tun->flags & TUN_ONE_QUEUE)) {
512 /* Normal queueing mode. */
513 /* Packet scheduler handles dropping of further packets. */
514 netif_stop_subqueue(dev, txq);
516 /* We won't see all dropped packets individually, so overrun
517 * error is more appropriate. */
518 dev->stats.tx_fifo_errors++;
520 /* Single queue mode.
521 * Driver handles dropping of all packets itself. */
526 /* Orphan the skb - required as we might hang on to it
527 * for indefinite time. */
528 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
533 skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
535 /* Notify and wake up reader process */
536 if (tfile->flags & TUN_FASYNC)
537 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
538 wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
539 POLLRDNORM | POLLRDBAND);
545 dev->stats.tx_dropped++;
551 static void tun_net_mclist(struct net_device *dev)
554 * This callback is supposed to deal with mc filter in
555 * _rx_ path and has nothing to do with the _tx_ path.
556 * In rx path we always accept everything userspace gives us.
561 #define MAX_MTU 65535
564 tun_net_change_mtu(struct net_device *dev, int new_mtu)
566 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
572 static netdev_features_t tun_net_fix_features(struct net_device *dev,
573 netdev_features_t features)
575 struct tun_struct *tun = netdev_priv(dev);
577 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
579 #ifdef CONFIG_NET_POLL_CONTROLLER
580 static void tun_poll_controller(struct net_device *dev)
583 * Tun only receives frames when:
584 * 1) the char device endpoint gets data from user space
585 * 2) the tun socket gets a sendmsg call from user space
586 * Since both of those are syncronous operations, we are guaranteed
587 * never to have pending data when we poll for it
588 * so theres nothing to do here but return.
589 * We need this though so netpoll recognizes us as an interface that
590 * supports polling, which enables bridge devices in virt setups to
591 * still use netconsole
596 static const struct net_device_ops tun_netdev_ops = {
597 .ndo_uninit = tun_net_uninit,
598 .ndo_open = tun_net_open,
599 .ndo_stop = tun_net_close,
600 .ndo_start_xmit = tun_net_xmit,
601 .ndo_change_mtu = tun_net_change_mtu,
602 .ndo_fix_features = tun_net_fix_features,
603 .ndo_select_queue = tun_select_queue,
604 #ifdef CONFIG_NET_POLL_CONTROLLER
605 .ndo_poll_controller = tun_poll_controller,
609 static const struct net_device_ops tap_netdev_ops = {
610 .ndo_uninit = tun_net_uninit,
611 .ndo_open = tun_net_open,
612 .ndo_stop = tun_net_close,
613 .ndo_start_xmit = tun_net_xmit,
614 .ndo_change_mtu = tun_net_change_mtu,
615 .ndo_fix_features = tun_net_fix_features,
616 .ndo_set_rx_mode = tun_net_mclist,
617 .ndo_set_mac_address = eth_mac_addr,
618 .ndo_validate_addr = eth_validate_addr,
619 .ndo_select_queue = tun_select_queue,
620 #ifdef CONFIG_NET_POLL_CONTROLLER
621 .ndo_poll_controller = tun_poll_controller,
625 /* Initialize net device. */
626 static void tun_net_init(struct net_device *dev)
628 struct tun_struct *tun = netdev_priv(dev);
630 switch (tun->flags & TUN_TYPE_MASK) {
632 dev->netdev_ops = &tun_netdev_ops;
634 /* Point-to-Point TUN Device */
635 dev->hard_header_len = 0;
639 /* Zero header length */
640 dev->type = ARPHRD_NONE;
641 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
642 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
646 dev->netdev_ops = &tap_netdev_ops;
647 /* Ethernet TAP Device */
649 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
651 eth_hw_addr_random(dev);
653 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
658 /* Character device part */
661 static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
663 struct tun_file *tfile = file->private_data;
664 struct tun_struct *tun = __tun_get(tfile);
666 unsigned int mask = 0;
671 sk = tfile->socket.sk;
673 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
675 poll_wait(file, &tfile->wq.wait, wait);
677 if (!skb_queue_empty(&sk->sk_receive_queue))
678 mask |= POLLIN | POLLRDNORM;
680 if (sock_writeable(sk) ||
681 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
683 mask |= POLLOUT | POLLWRNORM;
685 if (tun->dev->reg_state != NETREG_REGISTERED)
692 /* prepad is the amount to reserve at front. len is length after that.
693 * linear is a hint as to how much to copy (usually headers). */
694 static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
695 size_t prepad, size_t len,
696 size_t linear, int noblock)
698 struct sock *sk = tfile->socket.sk;
702 /* Under a page? Don't bother with paged skb. */
703 if (prepad + len < PAGE_SIZE || !linear)
706 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
711 skb_reserve(skb, prepad);
712 skb_put(skb, linear);
713 skb->data_len = len - linear;
714 skb->len += len - linear;
719 /* set skb frags from iovec, this can move to core network code for reuse */
720 static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
721 int offset, size_t count)
723 int len = iov_length(from, count) - offset;
724 int copy = skb_headlen(skb);
725 int size, offset1 = 0;
728 /* Skip over from offset */
729 while (count && (offset >= from->iov_len)) {
730 offset -= from->iov_len;
735 /* copy up to skb headlen */
736 while (count && (copy > 0)) {
737 size = min_t(unsigned int, copy, from->iov_len - offset);
738 if (copy_from_user(skb->data + offset1, from->iov_base + offset,
755 struct page *page[MAX_SKB_FRAGS];
758 unsigned long truesize;
760 len = from->iov_len - offset;
766 base = (unsigned long)from->iov_base + offset;
767 size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
768 if (i + size > MAX_SKB_FRAGS)
770 num_pages = get_user_pages_fast(base, size, 0, &page[i]);
771 if (num_pages != size) {
772 for (i = 0; i < num_pages; i++)
776 truesize = size * PAGE_SIZE;
777 skb->data_len += len;
779 skb->truesize += truesize;
780 atomic_add(truesize, &skb->sk->sk_wmem_alloc);
782 int off = base & ~PAGE_MASK;
783 int size = min_t(int, len, PAGE_SIZE - off);
784 __skb_fill_page_desc(skb, i, page[i], off, size);
785 skb_shinfo(skb)->nr_frags++;
786 /* increase sk_wmem_alloc */
797 /* Get packet from user space buffer */
798 static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
799 void *msg_control, const struct iovec *iv,
800 size_t total_len, size_t count, int noblock)
802 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
804 size_t len = total_len, align = NET_SKB_PAD;
805 struct virtio_net_hdr gso = { 0 };
808 bool zerocopy = false;
811 if (!(tun->flags & TUN_NO_PI)) {
812 if ((len -= sizeof(pi)) > total_len)
815 if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
817 offset += sizeof(pi);
820 if (tun->flags & TUN_VNET_HDR) {
821 if ((len -= tun->vnet_hdr_sz) > total_len)
824 if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
827 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
828 gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
829 gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
831 if (gso.hdr_len > len)
833 offset += tun->vnet_hdr_sz;
836 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
837 align += NET_IP_ALIGN;
838 if (unlikely(len < ETH_HLEN ||
839 (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
847 /* Userspace may produce vectors with count greater than
848 * MAX_SKB_FRAGS, so we need to linearize parts of the skb
849 * to let the rest of data to be fit in the frags.
851 if (count > MAX_SKB_FRAGS) {
852 copylen = iov_length(iv, count - MAX_SKB_FRAGS);
853 if (copylen < offset)
859 /* There are 256 bytes to be copied in skb, so there is enough
860 * room for skb expand head in case it is used.
861 * The rest of the buffer is mapped from userspace.
863 if (copylen < gso.hdr_len)
864 copylen = gso.hdr_len;
866 copylen = GOODCOPY_LEN;
870 skb = tun_alloc_skb(tfile, align, copylen, gso.hdr_len, noblock);
872 if (PTR_ERR(skb) != -EAGAIN)
873 tun->dev->stats.rx_dropped++;
878 err = zerocopy_sg_from_iovec(skb, iv, offset, count);
880 err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
883 tun->dev->stats.rx_dropped++;
888 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
889 if (!skb_partial_csum_set(skb, gso.csum_start,
891 tun->dev->stats.rx_frame_errors++;
897 switch (tun->flags & TUN_TYPE_MASK) {
899 if (tun->flags & TUN_NO_PI) {
900 switch (skb->data[0] & 0xf0) {
902 pi.proto = htons(ETH_P_IP);
905 pi.proto = htons(ETH_P_IPV6);
908 tun->dev->stats.rx_dropped++;
914 skb_reset_mac_header(skb);
915 skb->protocol = pi.proto;
919 skb->protocol = eth_type_trans(skb, tun->dev);
923 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
925 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
926 case VIRTIO_NET_HDR_GSO_TCPV4:
927 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
929 case VIRTIO_NET_HDR_GSO_TCPV6:
930 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
932 case VIRTIO_NET_HDR_GSO_UDP:
933 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
936 tun->dev->stats.rx_frame_errors++;
941 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
942 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
944 skb_shinfo(skb)->gso_size = gso.gso_size;
945 if (skb_shinfo(skb)->gso_size == 0) {
946 tun->dev->stats.rx_frame_errors++;
951 /* Header must be checked, and gso_segs computed. */
952 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
953 skb_shinfo(skb)->gso_segs = 0;
956 /* copy skb_ubuf_info for callback when skb has no error */
958 skb_shinfo(skb)->destructor_arg = msg_control;
959 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
964 tun->dev->stats.rx_packets++;
965 tun->dev->stats.rx_bytes += len;
970 static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
971 unsigned long count, loff_t pos)
973 struct file *file = iocb->ki_filp;
974 struct tun_struct *tun = tun_get(file);
975 struct tun_file *tfile = file->private_data;
981 tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
983 result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
984 count, file->f_flags & O_NONBLOCK);
990 /* Put packet to the user space buffer */
991 static ssize_t tun_put_user(struct tun_struct *tun,
992 struct tun_file *tfile,
994 const struct iovec *iv, int len)
996 struct tun_pi pi = { 0, skb->protocol };
999 if (!(tun->flags & TUN_NO_PI)) {
1000 if ((len -= sizeof(pi)) < 0)
1003 if (len < skb->len) {
1004 /* Packet will be striped */
1005 pi.flags |= TUN_PKT_STRIP;
1008 if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
1010 total += sizeof(pi);
1013 if (tun->flags & TUN_VNET_HDR) {
1014 struct virtio_net_hdr gso = { 0 }; /* no info leak */
1015 if ((len -= tun->vnet_hdr_sz) < 0)
1018 if (skb_is_gso(skb)) {
1019 struct skb_shared_info *sinfo = skb_shinfo(skb);
1021 /* This is a hint as to how much should be linear. */
1022 gso.hdr_len = skb_headlen(skb);
1023 gso.gso_size = sinfo->gso_size;
1024 if (sinfo->gso_type & SKB_GSO_TCPV4)
1025 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1026 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1027 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1028 else if (sinfo->gso_type & SKB_GSO_UDP)
1029 gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
1031 pr_err("unexpected GSO type: "
1032 "0x%x, gso_size %d, hdr_len %d\n",
1033 sinfo->gso_type, gso.gso_size,
1035 print_hex_dump(KERN_ERR, "tun: ",
1038 min((int)gso.hdr_len, 64), true);
1042 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1043 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1045 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1047 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1048 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
1049 gso.csum_start = skb_checksum_start_offset(skb);
1050 gso.csum_offset = skb->csum_offset;
1051 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1052 gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
1053 } /* else everything is zero */
1055 if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
1058 total += tun->vnet_hdr_sz;
1061 len = min_t(int, skb->len, len);
1063 skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
1066 tun->dev->stats.tx_packets++;
1067 tun->dev->stats.tx_bytes += len;
1072 static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
1073 struct kiocb *iocb, const struct iovec *iv,
1074 ssize_t len, int noblock)
1076 DECLARE_WAITQUEUE(wait, current);
1077 struct sk_buff *skb;
1080 tun_debug(KERN_INFO, tun, "tun_chr_read\n");
1082 if (unlikely(!noblock))
1083 add_wait_queue(&tfile->wq.wait, &wait);
1085 current->state = TASK_INTERRUPTIBLE;
1087 /* Read frames from the queue */
1088 if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
1093 if (signal_pending(current)) {
1097 if (tun->dev->reg_state != NETREG_REGISTERED) {
1102 /* Nothing to read, let's sleep */
1106 netif_wake_subqueue(tun->dev, tfile->queue_index);
1108 ret = tun_put_user(tun, tfile, skb, iv, len);
1113 current->state = TASK_RUNNING;
1114 if (unlikely(!noblock))
1115 remove_wait_queue(&tfile->wq.wait, &wait);
1120 static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
1121 unsigned long count, loff_t pos)
1123 struct file *file = iocb->ki_filp;
1124 struct tun_file *tfile = file->private_data;
1125 struct tun_struct *tun = __tun_get(tfile);
1130 len = iov_length(iv, count);
1136 ret = tun_do_read(tun, tfile, iocb, iv, len,
1137 file->f_flags & O_NONBLOCK);
1138 ret = min_t(ssize_t, ret, len);
1144 static void tun_setup(struct net_device *dev)
1146 struct tun_struct *tun = netdev_priv(dev);
1148 tun->owner = INVALID_UID;
1149 tun->group = INVALID_GID;
1151 dev->ethtool_ops = &tun_ethtool_ops;
1152 dev->destructor = free_netdev;
1155 /* Trivial set of netlink ops to allow deleting tun or tap
1156 * device with netlink.
1158 static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1163 static struct rtnl_link_ops tun_link_ops __read_mostly = {
1165 .priv_size = sizeof(struct tun_struct),
1167 .validate = tun_validate,
1170 static void tun_sock_write_space(struct sock *sk)
1172 struct tun_file *tfile;
1173 wait_queue_head_t *wqueue;
1175 if (!sock_writeable(sk))
1178 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
1181 wqueue = sk_sleep(sk);
1182 if (wqueue && waitqueue_active(wqueue))
1183 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
1184 POLLWRNORM | POLLWRBAND);
1186 tfile = container_of(sk, struct tun_file, sk);
1187 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
1190 static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
1191 struct msghdr *m, size_t total_len)
1194 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1195 struct tun_struct *tun = __tun_get(tfile);
1199 ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
1200 m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
1206 static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
1207 struct msghdr *m, size_t total_len,
1210 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1211 struct tun_struct *tun = __tun_get(tfile);
1217 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
1219 ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
1220 flags & MSG_DONTWAIT);
1221 if (ret > total_len) {
1222 m->msg_flags |= MSG_TRUNC;
1223 ret = flags & MSG_TRUNC ? ret : total_len;
1229 static int tun_release(struct socket *sock)
1236 /* Ops structure to mimic raw sockets with tun */
1237 static const struct proto_ops tun_socket_ops = {
1238 .sendmsg = tun_sendmsg,
1239 .recvmsg = tun_recvmsg,
1240 .release = tun_release,
1243 static struct proto tun_proto = {
1245 .owner = THIS_MODULE,
1246 .obj_size = sizeof(struct tun_file),
1249 static int tun_flags(struct tun_struct *tun)
1253 if (tun->flags & TUN_TUN_DEV)
1258 if (tun->flags & TUN_NO_PI)
1261 if (tun->flags & TUN_ONE_QUEUE)
1262 flags |= IFF_ONE_QUEUE;
1264 if (tun->flags & TUN_VNET_HDR)
1265 flags |= IFF_VNET_HDR;
1267 if (tun->flags & TUN_TAP_MQ)
1268 flags |= IFF_MULTI_QUEUE;
1273 static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1276 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1277 return sprintf(buf, "0x%x\n", tun_flags(tun));
1280 static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1283 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1284 return uid_valid(tun->owner)?
1285 sprintf(buf, "%u\n",
1286 from_kuid_munged(current_user_ns(), tun->owner)):
1287 sprintf(buf, "-1\n");
1290 static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1293 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1294 return gid_valid(tun->group) ?
1295 sprintf(buf, "%u\n",
1296 from_kgid_munged(current_user_ns(), tun->group)):
1297 sprintf(buf, "-1\n");
1300 static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1301 static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1302 static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1304 static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
1306 struct tun_struct *tun;
1307 struct tun_file *tfile = file->private_data;
1308 struct net_device *dev;
1311 dev = __dev_get_by_name(net, ifr->ifr_name);
1313 const struct cred *cred = current_cred();
1315 if (ifr->ifr_flags & IFF_TUN_EXCL)
1317 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
1318 tun = netdev_priv(dev);
1319 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1320 tun = netdev_priv(dev);
1324 if (((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
1325 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
1326 !capable(CAP_NET_ADMIN))
1328 err = security_tun_dev_attach(tfile->socket.sk);
1332 err = tun_attach(tun, file);
1338 unsigned long flags = 0;
1340 if (!capable(CAP_NET_ADMIN))
1342 err = security_tun_dev_create();
1347 if (ifr->ifr_flags & IFF_TUN) {
1349 flags |= TUN_TUN_DEV;
1351 } else if (ifr->ifr_flags & IFF_TAP) {
1353 flags |= TUN_TAP_DEV;
1359 name = ifr->ifr_name;
1361 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
1363 MAX_TAP_QUEUES, MAX_TAP_QUEUES);
1367 dev_net_set(dev, net);
1368 dev->rtnl_link_ops = &tun_link_ops;
1370 tun = netdev_priv(dev);
1373 tun->txflt.count = 0;
1374 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
1376 tun->filter_attached = false;
1377 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
1379 security_tun_dev_post_create(&tfile->sk);
1383 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
1385 dev->features = dev->hw_features;
1387 err = register_netdevice(tun->dev);
1391 if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1392 device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1393 device_create_file(&tun->dev->dev, &dev_attr_group))
1394 pr_err("Failed to create tun sysfs files\n");
1396 err = tun_attach(tun, file);
1401 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
1403 if (ifr->ifr_flags & IFF_NO_PI)
1404 tun->flags |= TUN_NO_PI;
1406 tun->flags &= ~TUN_NO_PI;
1408 if (ifr->ifr_flags & IFF_ONE_QUEUE)
1409 tun->flags |= TUN_ONE_QUEUE;
1411 tun->flags &= ~TUN_ONE_QUEUE;
1413 if (ifr->ifr_flags & IFF_VNET_HDR)
1414 tun->flags |= TUN_VNET_HDR;
1416 tun->flags &= ~TUN_VNET_HDR;
1418 if (ifr->ifr_flags & IFF_MULTI_QUEUE)
1419 tun->flags |= TUN_TAP_MQ;
1421 tun->flags &= ~TUN_TAP_MQ;
1423 /* Make sure persistent devices do not get stuck in
1426 if (netif_running(tun->dev))
1427 netif_tx_wake_all_queues(tun->dev);
1429 strcpy(ifr->ifr_name, tun->dev->name);
1437 static int tun_get_iff(struct net *net, struct tun_struct *tun,
1440 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
1442 strcpy(ifr->ifr_name, tun->dev->name);
1444 ifr->ifr_flags = tun_flags(tun);
1449 /* This is like a cut-down ethtool ops, except done via tun fd so no
1450 * privs required. */
1451 static int set_offload(struct tun_struct *tun, unsigned long arg)
1453 netdev_features_t features = 0;
1455 if (arg & TUN_F_CSUM) {
1456 features |= NETIF_F_HW_CSUM;
1459 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
1460 if (arg & TUN_F_TSO_ECN) {
1461 features |= NETIF_F_TSO_ECN;
1462 arg &= ~TUN_F_TSO_ECN;
1464 if (arg & TUN_F_TSO4)
1465 features |= NETIF_F_TSO;
1466 if (arg & TUN_F_TSO6)
1467 features |= NETIF_F_TSO6;
1468 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
1471 if (arg & TUN_F_UFO) {
1472 features |= NETIF_F_UFO;
1477 /* This gives the user a way to test for new features in future by
1478 * trying to set them. */
1482 tun->set_features = features;
1483 netdev_update_features(tun->dev);
1488 static void tun_detach_filter(struct tun_struct *tun, int n)
1491 struct tun_file *tfile;
1493 for (i = 0; i < n; i++) {
1494 tfile = rcu_dereference_protected(tun->tfiles[i],
1495 lockdep_rtnl_is_held());
1496 sk_detach_filter(tfile->socket.sk);
1499 tun->filter_attached = false;
1502 static int tun_attach_filter(struct tun_struct *tun)
1505 struct tun_file *tfile;
1507 for (i = 0; i < tun->numqueues; i++) {
1508 tfile = rcu_dereference_protected(tun->tfiles[i],
1509 lockdep_rtnl_is_held());
1510 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1512 tun_detach_filter(tun, i);
1517 tun->filter_attached = true;
1521 static void tun_set_sndbuf(struct tun_struct *tun)
1523 struct tun_file *tfile;
1526 for (i = 0; i < tun->numqueues; i++) {
1527 tfile = rcu_dereference_protected(tun->tfiles[i],
1528 lockdep_rtnl_is_held());
1529 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1533 static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1534 unsigned long arg, int ifreq_len)
1536 struct tun_file *tfile = file->private_data;
1537 struct tun_struct *tun;
1538 void __user* argp = (void __user*)arg;
1546 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) {
1547 if (copy_from_user(&ifr, argp, ifreq_len))
1550 memset(&ifr, 0, sizeof(ifr));
1552 if (cmd == TUNGETFEATURES) {
1553 /* Currently this just means: "what IFF flags are valid?".
1554 * This is needed because we never checked for invalid flags on
1556 return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1558 (unsigned int __user*)argp);
1564 tun = __tun_get(tfile);
1565 if (cmd == TUNSETIFF && !tun) {
1566 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1568 ret = tun_set_iff(tfile->net, file, &ifr);
1573 if (copy_to_user(argp, &ifr, ifreq_len))
1582 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
1587 ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
1591 if (copy_to_user(argp, &ifr, ifreq_len))
1596 /* Disable/Enable checksum */
1598 /* [unimplemented] */
1599 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
1600 arg ? "disabled" : "enabled");
1604 /* Disable/Enable persist mode. Keep an extra reference to the
1605 * module to prevent the module being unprobed.
1608 tun->flags |= TUN_PERSIST;
1609 __module_get(THIS_MODULE);
1611 tun->flags &= ~TUN_PERSIST;
1612 module_put(THIS_MODULE);
1615 tun_debug(KERN_INFO, tun, "persist %s\n",
1616 arg ? "enabled" : "disabled");
1620 /* Set owner of the device */
1621 owner = make_kuid(current_user_ns(), arg);
1622 if (!uid_valid(owner)) {
1627 tun_debug(KERN_INFO, tun, "owner set to %u\n",
1628 from_kuid(&init_user_ns, tun->owner));
1632 /* Set group of the device */
1633 group = make_kgid(current_user_ns(), arg);
1634 if (!gid_valid(group)) {
1639 tun_debug(KERN_INFO, tun, "group set to %u\n",
1640 from_kgid(&init_user_ns, tun->group));
1644 /* Only allow setting the type when the interface is down */
1645 if (tun->dev->flags & IFF_UP) {
1646 tun_debug(KERN_INFO, tun,
1647 "Linktype set failed because interface is up\n");
1650 tun->dev->type = (int) arg;
1651 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
1663 ret = set_offload(tun, arg);
1666 case TUNSETTXFILTER:
1667 /* Can be set only for TAPs */
1669 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1671 ret = update_filter(&tun->txflt, (void __user *)arg);
1675 /* Get hw address */
1676 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1677 ifr.ifr_hwaddr.sa_family = tun->dev->type;
1678 if (copy_to_user(argp, &ifr, ifreq_len))
1683 /* Set hw address */
1684 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
1685 ifr.ifr_hwaddr.sa_data);
1687 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
1691 sndbuf = tfile->socket.sk->sk_sndbuf;
1692 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
1697 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
1702 tun->sndbuf = sndbuf;
1703 tun_set_sndbuf(tun);
1706 case TUNGETVNETHDRSZ:
1707 vnet_hdr_sz = tun->vnet_hdr_sz;
1708 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
1712 case TUNSETVNETHDRSZ:
1713 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
1717 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
1722 tun->vnet_hdr_sz = vnet_hdr_sz;
1725 case TUNATTACHFILTER:
1726 /* Can be set only for TAPs */
1728 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1731 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
1734 ret = tun_attach_filter(tun);
1737 case TUNDETACHFILTER:
1738 /* Can be set only for TAPs */
1740 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1743 tun_detach_filter(tun, tun->numqueues);
1758 static long tun_chr_ioctl(struct file *file,
1759 unsigned int cmd, unsigned long arg)
1761 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
1764 #ifdef CONFIG_COMPAT
1765 static long tun_chr_compat_ioctl(struct file *file,
1766 unsigned int cmd, unsigned long arg)
1771 case TUNSETTXFILTER:
1776 arg = (unsigned long)compat_ptr(arg);
1779 arg = (compat_ulong_t)arg;
1784 * compat_ifreq is shorter than ifreq, so we must not access beyond
1785 * the end of that structure. All fields that are used in this
1786 * driver are compatible though, we don't need to convert the
1789 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
1791 #endif /* CONFIG_COMPAT */
1793 static int tun_chr_fasync(int fd, struct file *file, int on)
1795 struct tun_file *tfile = file->private_data;
1798 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
1802 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
1805 tfile->flags |= TUN_FASYNC;
1807 tfile->flags &= ~TUN_FASYNC;
1813 static int tun_chr_open(struct inode *inode, struct file * file)
1815 struct tun_file *tfile;
1817 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
1819 tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
1823 rcu_assign_pointer(tfile->tun, NULL);
1824 tfile->net = get_net(current->nsproxy->net_ns);
1827 rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
1828 init_waitqueue_head(&tfile->wq.wait);
1830 tfile->socket.file = file;
1831 tfile->socket.ops = &tun_socket_ops;
1833 sock_init_data(&tfile->socket, &tfile->sk);
1834 sk_change_net(&tfile->sk, tfile->net);
1836 tfile->sk.sk_write_space = tun_sock_write_space;
1837 tfile->sk.sk_sndbuf = INT_MAX;
1839 file->private_data = tfile;
1840 set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
1845 static int tun_chr_close(struct inode *inode, struct file *file)
1847 struct tun_file *tfile = file->private_data;
1848 struct net *net = tfile->net;
1850 tun_detach(tfile, true);
1856 static const struct file_operations tun_fops = {
1857 .owner = THIS_MODULE,
1858 .llseek = no_llseek,
1859 .read = do_sync_read,
1860 .aio_read = tun_chr_aio_read,
1861 .write = do_sync_write,
1862 .aio_write = tun_chr_aio_write,
1863 .poll = tun_chr_poll,
1864 .unlocked_ioctl = tun_chr_ioctl,
1865 #ifdef CONFIG_COMPAT
1866 .compat_ioctl = tun_chr_compat_ioctl,
1868 .open = tun_chr_open,
1869 .release = tun_chr_close,
1870 .fasync = tun_chr_fasync
1873 static struct miscdevice tun_miscdev = {
1876 .nodename = "net/tun",
1880 /* ethtool interface */
1882 static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1885 cmd->advertising = 0;
1886 ethtool_cmd_speed_set(cmd, SPEED_10);
1887 cmd->duplex = DUPLEX_FULL;
1888 cmd->port = PORT_TP;
1889 cmd->phy_address = 0;
1890 cmd->transceiver = XCVR_INTERNAL;
1891 cmd->autoneg = AUTONEG_DISABLE;
1897 static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1899 struct tun_struct *tun = netdev_priv(dev);
1901 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1902 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1904 switch (tun->flags & TUN_TYPE_MASK) {
1906 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
1909 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
1914 static u32 tun_get_msglevel(struct net_device *dev)
1917 struct tun_struct *tun = netdev_priv(dev);
1924 static void tun_set_msglevel(struct net_device *dev, u32 value)
1927 struct tun_struct *tun = netdev_priv(dev);
1932 static const struct ethtool_ops tun_ethtool_ops = {
1933 .get_settings = tun_get_settings,
1934 .get_drvinfo = tun_get_drvinfo,
1935 .get_msglevel = tun_get_msglevel,
1936 .set_msglevel = tun_set_msglevel,
1937 .get_link = ethtool_op_get_link,
1941 static int __init tun_init(void)
1945 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
1946 pr_info("%s\n", DRV_COPYRIGHT);
1948 ret = rtnl_link_register(&tun_link_ops);
1950 pr_err("Can't register link_ops\n");
1954 ret = misc_register(&tun_miscdev);
1956 pr_err("Can't register misc device %d\n", TUN_MINOR);
1961 rtnl_link_unregister(&tun_link_ops);
1966 static void tun_cleanup(void)
1968 misc_deregister(&tun_miscdev);
1969 rtnl_link_unregister(&tun_link_ops);
1972 /* Get an underlying socket object from tun file. Returns error unless file is
1973 * attached to a device. The returned object works like a packet socket, it
1974 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
1975 * holding a reference to the file for as long as the socket is in use. */
1976 struct socket *tun_get_socket(struct file *file)
1978 struct tun_file *tfile;
1979 if (file->f_op != &tun_fops)
1980 return ERR_PTR(-EINVAL);
1981 tfile = file->private_data;
1983 return ERR_PTR(-EBADFD);
1984 return &tfile->socket;
1986 EXPORT_SYMBOL_GPL(tun_get_socket);
1988 module_init(tun_init);
1989 module_exit(tun_cleanup);
1990 MODULE_DESCRIPTION(DRV_DESCRIPTION);
1991 MODULE_AUTHOR(DRV_COPYRIGHT);
1992 MODULE_LICENSE("GPL");
1993 MODULE_ALIAS_MISCDEV(TUN_MINOR);
1994 MODULE_ALIAS("devname:net/tun");