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 /* A tun_file connects an open character device to a tuntap netdevice. It
113 * also contains all socket related strctures (except sock_fprog and tap_filter)
114 * to serve as one transmit queue for tuntap device. The sock_fprog and
115 * tap_filter were kept in tun_struct since they were used for filtering for the
116 * netdevice not for a specific queue (at least I didn't see the reqirement for
120 * The tun_file and tun_struct are loosely coupled, the pointer from on to the
121 * other can only be read while rcu_read_lock or rtnl_lock is held.
125 struct socket socket
;
127 struct tun_struct __rcu
*tun
;
129 struct fasync_struct
*fasync
;
130 /* only used for fasnyc */
134 /* Since the socket were moved to tun_file, to preserve the behavior of persist
135 * device, socket fileter, sndbuf and vnet header size were restore when the
136 * file were attached to a persist device.
139 struct tun_file __rcu
*tfile
;
144 struct net_device
*dev
;
145 netdev_features_t set_features
;
146 #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
147 NETIF_F_TSO6|NETIF_F_UFO)
151 struct tap_filter txflt
;
152 struct sock_fprog fprog
;
153 /* protected by rtnl lock */
154 bool filter_attached
;
160 static int tun_attach(struct tun_struct
*tun
, struct file
*file
)
162 struct tun_file
*tfile
= file
->private_data
;
167 netif_tx_lock_bh(tun
->dev
);
179 /* Re-attach filter when attaching to a persist device */
180 if (tun
->filter_attached
== true) {
181 err
= sk_attach_filter(&tun
->fprog
, tfile
->socket
.sk
);
185 rcu_assign_pointer(tfile
->tun
, tun
);
186 tfile
->socket
.sk
->sk_sndbuf
= tun
->sndbuf
;
187 rcu_assign_pointer(tun
->tfile
, tfile
);
188 netif_carrier_on(tun
->dev
);
189 sock_hold(&tfile
->sk
);
192 netif_tx_unlock_bh(tun
->dev
);
196 static void __tun_detach(struct tun_struct
*tun
)
198 struct tun_file
*tfile
= rcu_dereference_protected(tun
->tfile
,
199 lockdep_rtnl_is_held());
200 /* Detach from net device */
201 netif_carrier_off(tun
->dev
);
202 rcu_assign_pointer(tun
->tfile
, NULL
);
204 rcu_assign_pointer(tfile
->tun
, NULL
);
207 /* Drop read queue */
208 skb_queue_purge(&tfile
->socket
.sk
->sk_receive_queue
);
212 static struct tun_struct
*__tun_get(struct tun_file
*tfile
)
214 struct tun_struct
*tun
;
217 tun
= rcu_dereference(tfile
->tun
);
225 static struct tun_struct
*tun_get(struct file
*file
)
227 return __tun_get(file
->private_data
);
230 static void tun_put(struct tun_struct
*tun
)
236 static void addr_hash_set(u32
*mask
, const u8
*addr
)
238 int n
= ether_crc(ETH_ALEN
, addr
) >> 26;
239 mask
[n
>> 5] |= (1 << (n
& 31));
242 static unsigned int addr_hash_test(const u32
*mask
, const u8
*addr
)
244 int n
= ether_crc(ETH_ALEN
, addr
) >> 26;
245 return mask
[n
>> 5] & (1 << (n
& 31));
248 static int update_filter(struct tap_filter
*filter
, void __user
*arg
)
250 struct { u8 u
[ETH_ALEN
]; } *addr
;
251 struct tun_filter uf
;
252 int err
, alen
, n
, nexact
;
254 if (copy_from_user(&uf
, arg
, sizeof(uf
)))
263 alen
= ETH_ALEN
* uf
.count
;
264 addr
= kmalloc(alen
, GFP_KERNEL
);
268 if (copy_from_user(addr
, arg
+ sizeof(uf
), alen
)) {
273 /* The filter is updated without holding any locks. Which is
274 * perfectly safe. We disable it first and in the worst
275 * case we'll accept a few undesired packets. */
279 /* Use first set of addresses as an exact filter */
280 for (n
= 0; n
< uf
.count
&& n
< FLT_EXACT_COUNT
; n
++)
281 memcpy(filter
->addr
[n
], addr
[n
].u
, ETH_ALEN
);
285 /* Remaining multicast addresses are hashed,
286 * unicast will leave the filter disabled. */
287 memset(filter
->mask
, 0, sizeof(filter
->mask
));
288 for (; n
< uf
.count
; n
++) {
289 if (!is_multicast_ether_addr(addr
[n
].u
)) {
290 err
= 0; /* no filter */
293 addr_hash_set(filter
->mask
, addr
[n
].u
);
296 /* For ALLMULTI just set the mask to all ones.
297 * This overrides the mask populated above. */
298 if ((uf
.flags
& TUN_FLT_ALLMULTI
))
299 memset(filter
->mask
, ~0, sizeof(filter
->mask
));
301 /* Now enable the filter */
303 filter
->count
= nexact
;
305 /* Return the number of exact filters */
313 /* Returns: 0 - drop, !=0 - accept */
314 static int run_filter(struct tap_filter
*filter
, const struct sk_buff
*skb
)
316 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
318 struct ethhdr
*eh
= (struct ethhdr
*) skb
->data
;
322 for (i
= 0; i
< filter
->count
; i
++)
323 if (ether_addr_equal(eh
->h_dest
, filter
->addr
[i
]))
326 /* Inexact match (multicast only) */
327 if (is_multicast_ether_addr(eh
->h_dest
))
328 return addr_hash_test(filter
->mask
, eh
->h_dest
);
334 * Checks whether the packet is accepted or not.
335 * Returns: 0 - drop, !=0 - accept
337 static int check_filter(struct tap_filter
*filter
, const struct sk_buff
*skb
)
342 return run_filter(filter
, skb
);
345 /* Network device part of the driver */
347 static const struct ethtool_ops tun_ethtool_ops
;
349 /* Net device detach from fd. */
350 static void tun_net_uninit(struct net_device
*dev
)
352 struct tun_struct
*tun
= netdev_priv(dev
);
353 struct tun_file
*tfile
= rcu_dereference_protected(tun
->tfile
,
354 lockdep_rtnl_is_held());
356 /* Inform the methods they need to stop using the dev.
359 wake_up_all(&tfile
->wq
.wait
);
365 /* Net device open. */
366 static int tun_net_open(struct net_device
*dev
)
368 netif_start_queue(dev
);
372 /* Net device close. */
373 static int tun_net_close(struct net_device
*dev
)
375 netif_stop_queue(dev
);
379 /* Net device start xmit */
380 static netdev_tx_t
tun_net_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
382 struct tun_struct
*tun
= netdev_priv(dev
);
383 struct tun_file
*tfile
;
386 tfile
= rcu_dereference(tun
->tfile
);
387 /* Drop packet if interface is not attached */
391 tun_debug(KERN_INFO
, tun
, "tun_net_xmit %d\n", skb
->len
);
393 /* Drop if the filter does not like it.
394 * This is a noop if the filter is disabled.
395 * Filter can be enabled only for the TAP devices. */
396 if (!check_filter(&tun
->txflt
, skb
))
399 if (tfile
->socket
.sk
->sk_filter
&&
400 sk_filter(tfile
->socket
.sk
, skb
))
403 if (skb_queue_len(&tfile
->socket
.sk
->sk_receive_queue
)
404 >= dev
->tx_queue_len
) {
405 if (!(tun
->flags
& TUN_ONE_QUEUE
)) {
406 /* Normal queueing mode. */
407 /* Packet scheduler handles dropping of further packets. */
408 netif_stop_queue(dev
);
410 /* We won't see all dropped packets individually, so overrun
411 * error is more appropriate. */
412 dev
->stats
.tx_fifo_errors
++;
414 /* Single queue mode.
415 * Driver handles dropping of all packets itself. */
420 /* Orphan the skb - required as we might hang on to it
421 * for indefinite time. */
422 if (unlikely(skb_orphan_frags(skb
, GFP_ATOMIC
)))
427 skb_queue_tail(&tfile
->socket
.sk
->sk_receive_queue
, skb
);
429 /* Notify and wake up reader process */
430 if (tfile
->flags
& TUN_FASYNC
)
431 kill_fasync(&tfile
->fasync
, SIGIO
, POLL_IN
);
432 wake_up_interruptible_poll(&tfile
->wq
.wait
, POLLIN
|
433 POLLRDNORM
| POLLRDBAND
);
439 dev
->stats
.tx_dropped
++;
445 static void tun_net_mclist(struct net_device
*dev
)
448 * This callback is supposed to deal with mc filter in
449 * _rx_ path and has nothing to do with the _tx_ path.
450 * In rx path we always accept everything userspace gives us.
455 #define MAX_MTU 65535
458 tun_net_change_mtu(struct net_device
*dev
, int new_mtu
)
460 if (new_mtu
< MIN_MTU
|| new_mtu
+ dev
->hard_header_len
> MAX_MTU
)
466 static netdev_features_t
tun_net_fix_features(struct net_device
*dev
,
467 netdev_features_t features
)
469 struct tun_struct
*tun
= netdev_priv(dev
);
471 return (features
& tun
->set_features
) | (features
& ~TUN_USER_FEATURES
);
473 #ifdef CONFIG_NET_POLL_CONTROLLER
474 static void tun_poll_controller(struct net_device
*dev
)
477 * Tun only receives frames when:
478 * 1) the char device endpoint gets data from user space
479 * 2) the tun socket gets a sendmsg call from user space
480 * Since both of those are syncronous operations, we are guaranteed
481 * never to have pending data when we poll for it
482 * so theres nothing to do here but return.
483 * We need this though so netpoll recognizes us as an interface that
484 * supports polling, which enables bridge devices in virt setups to
485 * still use netconsole
490 static const struct net_device_ops tun_netdev_ops
= {
491 .ndo_uninit
= tun_net_uninit
,
492 .ndo_open
= tun_net_open
,
493 .ndo_stop
= tun_net_close
,
494 .ndo_start_xmit
= tun_net_xmit
,
495 .ndo_change_mtu
= tun_net_change_mtu
,
496 .ndo_fix_features
= tun_net_fix_features
,
497 #ifdef CONFIG_NET_POLL_CONTROLLER
498 .ndo_poll_controller
= tun_poll_controller
,
502 static const struct net_device_ops tap_netdev_ops
= {
503 .ndo_uninit
= tun_net_uninit
,
504 .ndo_open
= tun_net_open
,
505 .ndo_stop
= tun_net_close
,
506 .ndo_start_xmit
= tun_net_xmit
,
507 .ndo_change_mtu
= tun_net_change_mtu
,
508 .ndo_fix_features
= tun_net_fix_features
,
509 .ndo_set_rx_mode
= tun_net_mclist
,
510 .ndo_set_mac_address
= eth_mac_addr
,
511 .ndo_validate_addr
= eth_validate_addr
,
512 #ifdef CONFIG_NET_POLL_CONTROLLER
513 .ndo_poll_controller
= tun_poll_controller
,
517 /* Initialize net device. */
518 static void tun_net_init(struct net_device
*dev
)
520 struct tun_struct
*tun
= netdev_priv(dev
);
522 switch (tun
->flags
& TUN_TYPE_MASK
) {
524 dev
->netdev_ops
= &tun_netdev_ops
;
526 /* Point-to-Point TUN Device */
527 dev
->hard_header_len
= 0;
531 /* Zero header length */
532 dev
->type
= ARPHRD_NONE
;
533 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
534 dev
->tx_queue_len
= TUN_READQ_SIZE
; /* We prefer our own queue length */
538 dev
->netdev_ops
= &tap_netdev_ops
;
539 /* Ethernet TAP Device */
541 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
543 eth_hw_addr_random(dev
);
545 dev
->tx_queue_len
= TUN_READQ_SIZE
; /* We prefer our own queue length */
550 /* Character device part */
553 static unsigned int tun_chr_poll(struct file
*file
, poll_table
* wait
)
555 struct tun_file
*tfile
= file
->private_data
;
556 struct tun_struct
*tun
= __tun_get(tfile
);
558 unsigned int mask
= 0;
563 sk
= tfile
->socket
.sk
;
565 tun_debug(KERN_INFO
, tun
, "tun_chr_poll\n");
567 poll_wait(file
, &tfile
->wq
.wait
, wait
);
569 if (!skb_queue_empty(&sk
->sk_receive_queue
))
570 mask
|= POLLIN
| POLLRDNORM
;
572 if (sock_writeable(sk
) ||
573 (!test_and_set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
) &&
575 mask
|= POLLOUT
| POLLWRNORM
;
577 if (tun
->dev
->reg_state
!= NETREG_REGISTERED
)
584 /* prepad is the amount to reserve at front. len is length after that.
585 * linear is a hint as to how much to copy (usually headers). */
586 static struct sk_buff
*tun_alloc_skb(struct tun_file
*tfile
,
587 size_t prepad
, size_t len
,
588 size_t linear
, int noblock
)
590 struct sock
*sk
= tfile
->socket
.sk
;
594 /* Under a page? Don't bother with paged skb. */
595 if (prepad
+ len
< PAGE_SIZE
|| !linear
)
598 skb
= sock_alloc_send_pskb(sk
, prepad
+ linear
, len
- linear
, noblock
,
603 skb_reserve(skb
, prepad
);
604 skb_put(skb
, linear
);
605 skb
->data_len
= len
- linear
;
606 skb
->len
+= len
- linear
;
611 /* set skb frags from iovec, this can move to core network code for reuse */
612 static int zerocopy_sg_from_iovec(struct sk_buff
*skb
, const struct iovec
*from
,
613 int offset
, size_t count
)
615 int len
= iov_length(from
, count
) - offset
;
616 int copy
= skb_headlen(skb
);
617 int size
, offset1
= 0;
620 /* Skip over from offset */
621 while (count
&& (offset
>= from
->iov_len
)) {
622 offset
-= from
->iov_len
;
627 /* copy up to skb headlen */
628 while (count
&& (copy
> 0)) {
629 size
= min_t(unsigned int, copy
, from
->iov_len
- offset
);
630 if (copy_from_user(skb
->data
+ offset1
, from
->iov_base
+ offset
,
647 struct page
*page
[MAX_SKB_FRAGS
];
650 unsigned long truesize
;
652 len
= from
->iov_len
- offset
;
658 base
= (unsigned long)from
->iov_base
+ offset
;
659 size
= ((base
& ~PAGE_MASK
) + len
+ ~PAGE_MASK
) >> PAGE_SHIFT
;
660 if (i
+ size
> MAX_SKB_FRAGS
)
662 num_pages
= get_user_pages_fast(base
, size
, 0, &page
[i
]);
663 if (num_pages
!= size
) {
664 for (i
= 0; i
< num_pages
; i
++)
668 truesize
= size
* PAGE_SIZE
;
669 skb
->data_len
+= len
;
671 skb
->truesize
+= truesize
;
672 atomic_add(truesize
, &skb
->sk
->sk_wmem_alloc
);
674 int off
= base
& ~PAGE_MASK
;
675 int size
= min_t(int, len
, PAGE_SIZE
- off
);
676 __skb_fill_page_desc(skb
, i
, page
[i
], off
, size
);
677 skb_shinfo(skb
)->nr_frags
++;
678 /* increase sk_wmem_alloc */
689 /* Get packet from user space buffer */
690 static ssize_t
tun_get_user(struct tun_struct
*tun
, struct tun_file
*tfile
,
691 void *msg_control
, const struct iovec
*iv
,
692 size_t total_len
, size_t count
, int noblock
)
694 struct tun_pi pi
= { 0, cpu_to_be16(ETH_P_IP
) };
696 size_t len
= total_len
, align
= NET_SKB_PAD
;
697 struct virtio_net_hdr gso
= { 0 };
700 bool zerocopy
= false;
703 if (!(tun
->flags
& TUN_NO_PI
)) {
704 if ((len
-= sizeof(pi
)) > total_len
)
707 if (memcpy_fromiovecend((void *)&pi
, iv
, 0, sizeof(pi
)))
709 offset
+= sizeof(pi
);
712 if (tun
->flags
& TUN_VNET_HDR
) {
713 if ((len
-= tun
->vnet_hdr_sz
) > total_len
)
716 if (memcpy_fromiovecend((void *)&gso
, iv
, offset
, sizeof(gso
)))
719 if ((gso
.flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) &&
720 gso
.csum_start
+ gso
.csum_offset
+ 2 > gso
.hdr_len
)
721 gso
.hdr_len
= gso
.csum_start
+ gso
.csum_offset
+ 2;
723 if (gso
.hdr_len
> len
)
725 offset
+= tun
->vnet_hdr_sz
;
728 if ((tun
->flags
& TUN_TYPE_MASK
) == TUN_TAP_DEV
) {
729 align
+= NET_IP_ALIGN
;
730 if (unlikely(len
< ETH_HLEN
||
731 (gso
.hdr_len
&& gso
.hdr_len
< ETH_HLEN
)))
739 /* Userspace may produce vectors with count greater than
740 * MAX_SKB_FRAGS, so we need to linearize parts of the skb
741 * to let the rest of data to be fit in the frags.
743 if (count
> MAX_SKB_FRAGS
) {
744 copylen
= iov_length(iv
, count
- MAX_SKB_FRAGS
);
745 if (copylen
< offset
)
751 /* There are 256 bytes to be copied in skb, so there is enough
752 * room for skb expand head in case it is used.
753 * The rest of the buffer is mapped from userspace.
755 if (copylen
< gso
.hdr_len
)
756 copylen
= gso
.hdr_len
;
758 copylen
= GOODCOPY_LEN
;
762 skb
= tun_alloc_skb(tfile
, align
, copylen
, gso
.hdr_len
, noblock
);
764 if (PTR_ERR(skb
) != -EAGAIN
)
765 tun
->dev
->stats
.rx_dropped
++;
770 err
= zerocopy_sg_from_iovec(skb
, iv
, offset
, count
);
772 err
= skb_copy_datagram_from_iovec(skb
, 0, iv
, offset
, len
);
775 tun
->dev
->stats
.rx_dropped
++;
780 if (gso
.flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) {
781 if (!skb_partial_csum_set(skb
, gso
.csum_start
,
783 tun
->dev
->stats
.rx_frame_errors
++;
789 switch (tun
->flags
& TUN_TYPE_MASK
) {
791 if (tun
->flags
& TUN_NO_PI
) {
792 switch (skb
->data
[0] & 0xf0) {
794 pi
.proto
= htons(ETH_P_IP
);
797 pi
.proto
= htons(ETH_P_IPV6
);
800 tun
->dev
->stats
.rx_dropped
++;
806 skb_reset_mac_header(skb
);
807 skb
->protocol
= pi
.proto
;
811 skb
->protocol
= eth_type_trans(skb
, tun
->dev
);
815 if (gso
.gso_type
!= VIRTIO_NET_HDR_GSO_NONE
) {
817 switch (gso
.gso_type
& ~VIRTIO_NET_HDR_GSO_ECN
) {
818 case VIRTIO_NET_HDR_GSO_TCPV4
:
819 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV4
;
821 case VIRTIO_NET_HDR_GSO_TCPV6
:
822 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV6
;
824 case VIRTIO_NET_HDR_GSO_UDP
:
825 skb_shinfo(skb
)->gso_type
= SKB_GSO_UDP
;
828 tun
->dev
->stats
.rx_frame_errors
++;
833 if (gso
.gso_type
& VIRTIO_NET_HDR_GSO_ECN
)
834 skb_shinfo(skb
)->gso_type
|= SKB_GSO_TCP_ECN
;
836 skb_shinfo(skb
)->gso_size
= gso
.gso_size
;
837 if (skb_shinfo(skb
)->gso_size
== 0) {
838 tun
->dev
->stats
.rx_frame_errors
++;
843 /* Header must be checked, and gso_segs computed. */
844 skb_shinfo(skb
)->gso_type
|= SKB_GSO_DODGY
;
845 skb_shinfo(skb
)->gso_segs
= 0;
848 /* copy skb_ubuf_info for callback when skb has no error */
850 skb_shinfo(skb
)->destructor_arg
= msg_control
;
851 skb_shinfo(skb
)->tx_flags
|= SKBTX_DEV_ZEROCOPY
;
856 tun
->dev
->stats
.rx_packets
++;
857 tun
->dev
->stats
.rx_bytes
+= len
;
862 static ssize_t
tun_chr_aio_write(struct kiocb
*iocb
, const struct iovec
*iv
,
863 unsigned long count
, loff_t pos
)
865 struct file
*file
= iocb
->ki_filp
;
866 struct tun_struct
*tun
= tun_get(file
);
867 struct tun_file
*tfile
= file
->private_data
;
873 tun_debug(KERN_INFO
, tun
, "tun_chr_write %ld\n", count
);
875 result
= tun_get_user(tun
, tfile
, NULL
, iv
, iov_length(iv
, count
),
876 count
, file
->f_flags
& O_NONBLOCK
);
882 /* Put packet to the user space buffer */
883 static ssize_t
tun_put_user(struct tun_struct
*tun
,
884 struct tun_file
*tfile
,
886 const struct iovec
*iv
, int len
)
888 struct tun_pi pi
= { 0, skb
->protocol
};
891 if (!(tun
->flags
& TUN_NO_PI
)) {
892 if ((len
-= sizeof(pi
)) < 0)
895 if (len
< skb
->len
) {
896 /* Packet will be striped */
897 pi
.flags
|= TUN_PKT_STRIP
;
900 if (memcpy_toiovecend(iv
, (void *) &pi
, 0, sizeof(pi
)))
905 if (tun
->flags
& TUN_VNET_HDR
) {
906 struct virtio_net_hdr gso
= { 0 }; /* no info leak */
907 if ((len
-= tun
->vnet_hdr_sz
) < 0)
910 if (skb_is_gso(skb
)) {
911 struct skb_shared_info
*sinfo
= skb_shinfo(skb
);
913 /* This is a hint as to how much should be linear. */
914 gso
.hdr_len
= skb_headlen(skb
);
915 gso
.gso_size
= sinfo
->gso_size
;
916 if (sinfo
->gso_type
& SKB_GSO_TCPV4
)
917 gso
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
918 else if (sinfo
->gso_type
& SKB_GSO_TCPV6
)
919 gso
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
920 else if (sinfo
->gso_type
& SKB_GSO_UDP
)
921 gso
.gso_type
= VIRTIO_NET_HDR_GSO_UDP
;
923 pr_err("unexpected GSO type: "
924 "0x%x, gso_size %d, hdr_len %d\n",
925 sinfo
->gso_type
, gso
.gso_size
,
927 print_hex_dump(KERN_ERR
, "tun: ",
930 min((int)gso
.hdr_len
, 64), true);
934 if (sinfo
->gso_type
& SKB_GSO_TCP_ECN
)
935 gso
.gso_type
|= VIRTIO_NET_HDR_GSO_ECN
;
937 gso
.gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
939 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
940 gso
.flags
= VIRTIO_NET_HDR_F_NEEDS_CSUM
;
941 gso
.csum_start
= skb_checksum_start_offset(skb
);
942 gso
.csum_offset
= skb
->csum_offset
;
943 } else if (skb
->ip_summed
== CHECKSUM_UNNECESSARY
) {
944 gso
.flags
= VIRTIO_NET_HDR_F_DATA_VALID
;
945 } /* else everything is zero */
947 if (unlikely(memcpy_toiovecend(iv
, (void *)&gso
, total
,
950 total
+= tun
->vnet_hdr_sz
;
953 len
= min_t(int, skb
->len
, len
);
955 skb_copy_datagram_const_iovec(skb
, 0, iv
, total
, len
);
958 tun
->dev
->stats
.tx_packets
++;
959 tun
->dev
->stats
.tx_bytes
+= len
;
964 static ssize_t
tun_do_read(struct tun_struct
*tun
, struct tun_file
*tfile
,
965 struct kiocb
*iocb
, const struct iovec
*iv
,
966 ssize_t len
, int noblock
)
968 DECLARE_WAITQUEUE(wait
, current
);
972 tun_debug(KERN_INFO
, tun
, "tun_chr_read\n");
974 if (unlikely(!noblock
))
975 add_wait_queue(&tfile
->wq
.wait
, &wait
);
977 current
->state
= TASK_INTERRUPTIBLE
;
979 /* Read frames from the queue */
980 if (!(skb
= skb_dequeue(&tfile
->socket
.sk
->sk_receive_queue
))) {
985 if (signal_pending(current
)) {
989 if (tun
->dev
->reg_state
!= NETREG_REGISTERED
) {
994 /* Nothing to read, let's sleep */
998 netif_wake_queue(tun
->dev
);
1000 ret
= tun_put_user(tun
, tfile
, skb
, iv
, len
);
1005 current
->state
= TASK_RUNNING
;
1006 if (unlikely(!noblock
))
1007 remove_wait_queue(&tfile
->wq
.wait
, &wait
);
1012 static ssize_t
tun_chr_aio_read(struct kiocb
*iocb
, const struct iovec
*iv
,
1013 unsigned long count
, loff_t pos
)
1015 struct file
*file
= iocb
->ki_filp
;
1016 struct tun_file
*tfile
= file
->private_data
;
1017 struct tun_struct
*tun
= __tun_get(tfile
);
1022 len
= iov_length(iv
, count
);
1028 ret
= tun_do_read(tun
, tfile
, iocb
, iv
, len
,
1029 file
->f_flags
& O_NONBLOCK
);
1030 ret
= min_t(ssize_t
, ret
, len
);
1036 static void tun_setup(struct net_device
*dev
)
1038 struct tun_struct
*tun
= netdev_priv(dev
);
1040 tun
->owner
= INVALID_UID
;
1041 tun
->group
= INVALID_GID
;
1043 dev
->ethtool_ops
= &tun_ethtool_ops
;
1044 dev
->destructor
= free_netdev
;
1047 /* Trivial set of netlink ops to allow deleting tun or tap
1048 * device with netlink.
1050 static int tun_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1055 static struct rtnl_link_ops tun_link_ops __read_mostly
= {
1057 .priv_size
= sizeof(struct tun_struct
),
1059 .validate
= tun_validate
,
1062 static void tun_sock_write_space(struct sock
*sk
)
1064 struct tun_file
*tfile
;
1065 wait_queue_head_t
*wqueue
;
1067 if (!sock_writeable(sk
))
1070 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
))
1073 wqueue
= sk_sleep(sk
);
1074 if (wqueue
&& waitqueue_active(wqueue
))
1075 wake_up_interruptible_sync_poll(wqueue
, POLLOUT
|
1076 POLLWRNORM
| POLLWRBAND
);
1078 tfile
= container_of(sk
, struct tun_file
, sk
);
1079 kill_fasync(&tfile
->fasync
, SIGIO
, POLL_OUT
);
1082 static int tun_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
1083 struct msghdr
*m
, size_t total_len
)
1086 struct tun_file
*tfile
= container_of(sock
, struct tun_file
, socket
);
1087 struct tun_struct
*tun
= __tun_get(tfile
);
1091 ret
= tun_get_user(tun
, tfile
, m
->msg_control
, m
->msg_iov
, total_len
,
1092 m
->msg_iovlen
, m
->msg_flags
& MSG_DONTWAIT
);
1098 static int tun_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1099 struct msghdr
*m
, size_t total_len
,
1102 struct tun_file
*tfile
= container_of(sock
, struct tun_file
, socket
);
1103 struct tun_struct
*tun
= __tun_get(tfile
);
1109 if (flags
& ~(MSG_DONTWAIT
|MSG_TRUNC
))
1111 ret
= tun_do_read(tun
, tfile
, iocb
, m
->msg_iov
, total_len
,
1112 flags
& MSG_DONTWAIT
);
1113 if (ret
> total_len
) {
1114 m
->msg_flags
|= MSG_TRUNC
;
1115 ret
= flags
& MSG_TRUNC
? ret
: total_len
;
1121 static int tun_release(struct socket
*sock
)
1128 /* Ops structure to mimic raw sockets with tun */
1129 static const struct proto_ops tun_socket_ops
= {
1130 .sendmsg
= tun_sendmsg
,
1131 .recvmsg
= tun_recvmsg
,
1132 .release
= tun_release
,
1135 static struct proto tun_proto
= {
1137 .owner
= THIS_MODULE
,
1138 .obj_size
= sizeof(struct tun_file
),
1141 static int tun_flags(struct tun_struct
*tun
)
1145 if (tun
->flags
& TUN_TUN_DEV
)
1150 if (tun
->flags
& TUN_NO_PI
)
1153 if (tun
->flags
& TUN_ONE_QUEUE
)
1154 flags
|= IFF_ONE_QUEUE
;
1156 if (tun
->flags
& TUN_VNET_HDR
)
1157 flags
|= IFF_VNET_HDR
;
1162 static ssize_t
tun_show_flags(struct device
*dev
, struct device_attribute
*attr
,
1165 struct tun_struct
*tun
= netdev_priv(to_net_dev(dev
));
1166 return sprintf(buf
, "0x%x\n", tun_flags(tun
));
1169 static ssize_t
tun_show_owner(struct device
*dev
, struct device_attribute
*attr
,
1172 struct tun_struct
*tun
= netdev_priv(to_net_dev(dev
));
1173 return uid_valid(tun
->owner
)?
1174 sprintf(buf
, "%u\n",
1175 from_kuid_munged(current_user_ns(), tun
->owner
)):
1176 sprintf(buf
, "-1\n");
1179 static ssize_t
tun_show_group(struct device
*dev
, struct device_attribute
*attr
,
1182 struct tun_struct
*tun
= netdev_priv(to_net_dev(dev
));
1183 return gid_valid(tun
->group
) ?
1184 sprintf(buf
, "%u\n",
1185 from_kgid_munged(current_user_ns(), tun
->group
)):
1186 sprintf(buf
, "-1\n");
1189 static DEVICE_ATTR(tun_flags
, 0444, tun_show_flags
, NULL
);
1190 static DEVICE_ATTR(owner
, 0444, tun_show_owner
, NULL
);
1191 static DEVICE_ATTR(group
, 0444, tun_show_group
, NULL
);
1193 static int tun_set_iff(struct net
*net
, struct file
*file
, struct ifreq
*ifr
)
1195 struct tun_struct
*tun
;
1196 struct tun_file
*tfile
= file
->private_data
;
1197 struct net_device
*dev
;
1200 dev
= __dev_get_by_name(net
, ifr
->ifr_name
);
1202 const struct cred
*cred
= current_cred();
1204 if (ifr
->ifr_flags
& IFF_TUN_EXCL
)
1206 if ((ifr
->ifr_flags
& IFF_TUN
) && dev
->netdev_ops
== &tun_netdev_ops
)
1207 tun
= netdev_priv(dev
);
1208 else if ((ifr
->ifr_flags
& IFF_TAP
) && dev
->netdev_ops
== &tap_netdev_ops
)
1209 tun
= netdev_priv(dev
);
1213 if (((uid_valid(tun
->owner
) && !uid_eq(cred
->euid
, tun
->owner
)) ||
1214 (gid_valid(tun
->group
) && !in_egroup_p(tun
->group
))) &&
1215 !capable(CAP_NET_ADMIN
))
1217 err
= security_tun_dev_attach(tfile
->socket
.sk
);
1221 err
= tun_attach(tun
, file
);
1227 unsigned long flags
= 0;
1229 if (!capable(CAP_NET_ADMIN
))
1231 err
= security_tun_dev_create();
1236 if (ifr
->ifr_flags
& IFF_TUN
) {
1238 flags
|= TUN_TUN_DEV
;
1240 } else if (ifr
->ifr_flags
& IFF_TAP
) {
1242 flags
|= TUN_TAP_DEV
;
1248 name
= ifr
->ifr_name
;
1250 dev
= alloc_netdev(sizeof(struct tun_struct
), name
,
1255 dev_net_set(dev
, net
);
1256 dev
->rtnl_link_ops
= &tun_link_ops
;
1258 tun
= netdev_priv(dev
);
1261 tun
->txflt
.count
= 0;
1262 tun
->vnet_hdr_sz
= sizeof(struct virtio_net_hdr
);
1264 tun
->filter_attached
= false;
1265 tun
->sndbuf
= tfile
->socket
.sk
->sk_sndbuf
;
1267 security_tun_dev_post_create(&tfile
->sk
);
1271 dev
->hw_features
= NETIF_F_SG
| NETIF_F_FRAGLIST
|
1273 dev
->features
= dev
->hw_features
;
1275 err
= register_netdevice(tun
->dev
);
1279 if (device_create_file(&tun
->dev
->dev
, &dev_attr_tun_flags
) ||
1280 device_create_file(&tun
->dev
->dev
, &dev_attr_owner
) ||
1281 device_create_file(&tun
->dev
->dev
, &dev_attr_group
))
1282 pr_err("Failed to create tun sysfs files\n");
1284 err
= tun_attach(tun
, file
);
1289 tun_debug(KERN_INFO
, tun
, "tun_set_iff\n");
1291 if (ifr
->ifr_flags
& IFF_NO_PI
)
1292 tun
->flags
|= TUN_NO_PI
;
1294 tun
->flags
&= ~TUN_NO_PI
;
1296 if (ifr
->ifr_flags
& IFF_ONE_QUEUE
)
1297 tun
->flags
|= TUN_ONE_QUEUE
;
1299 tun
->flags
&= ~TUN_ONE_QUEUE
;
1301 if (ifr
->ifr_flags
& IFF_VNET_HDR
)
1302 tun
->flags
|= TUN_VNET_HDR
;
1304 tun
->flags
&= ~TUN_VNET_HDR
;
1306 /* Make sure persistent devices do not get stuck in
1309 if (netif_running(tun
->dev
))
1310 netif_wake_queue(tun
->dev
);
1312 strcpy(ifr
->ifr_name
, tun
->dev
->name
);
1321 static int tun_get_iff(struct net
*net
, struct tun_struct
*tun
,
1324 tun_debug(KERN_INFO
, tun
, "tun_get_iff\n");
1326 strcpy(ifr
->ifr_name
, tun
->dev
->name
);
1328 ifr
->ifr_flags
= tun_flags(tun
);
1333 /* This is like a cut-down ethtool ops, except done via tun fd so no
1334 * privs required. */
1335 static int set_offload(struct tun_struct
*tun
, unsigned long arg
)
1337 netdev_features_t features
= 0;
1339 if (arg
& TUN_F_CSUM
) {
1340 features
|= NETIF_F_HW_CSUM
;
1343 if (arg
& (TUN_F_TSO4
|TUN_F_TSO6
)) {
1344 if (arg
& TUN_F_TSO_ECN
) {
1345 features
|= NETIF_F_TSO_ECN
;
1346 arg
&= ~TUN_F_TSO_ECN
;
1348 if (arg
& TUN_F_TSO4
)
1349 features
|= NETIF_F_TSO
;
1350 if (arg
& TUN_F_TSO6
)
1351 features
|= NETIF_F_TSO6
;
1352 arg
&= ~(TUN_F_TSO4
|TUN_F_TSO6
);
1355 if (arg
& TUN_F_UFO
) {
1356 features
|= NETIF_F_UFO
;
1361 /* This gives the user a way to test for new features in future by
1362 * trying to set them. */
1366 tun
->set_features
= features
;
1367 netdev_update_features(tun
->dev
);
1372 static long __tun_chr_ioctl(struct file
*file
, unsigned int cmd
,
1373 unsigned long arg
, int ifreq_len
)
1375 struct tun_file
*tfile
= file
->private_data
;
1376 struct tun_struct
*tun
;
1377 void __user
* argp
= (void __user
*)arg
;
1385 if (cmd
== TUNSETIFF
|| _IOC_TYPE(cmd
) == 0x89) {
1386 if (copy_from_user(&ifr
, argp
, ifreq_len
))
1389 memset(&ifr
, 0, sizeof(ifr
));
1391 if (cmd
== TUNGETFEATURES
) {
1392 /* Currently this just means: "what IFF flags are valid?".
1393 * This is needed because we never checked for invalid flags on
1395 return put_user(IFF_TUN
| IFF_TAP
| IFF_NO_PI
| IFF_ONE_QUEUE
|
1397 (unsigned int __user
*)argp
);
1402 tun
= __tun_get(tfile
);
1403 if (cmd
== TUNSETIFF
&& !tun
) {
1404 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1406 ret
= tun_set_iff(tfile
->net
, file
, &ifr
);
1411 if (copy_to_user(argp
, &ifr
, ifreq_len
))
1420 tun_debug(KERN_INFO
, tun
, "tun_chr_ioctl cmd %u\n", cmd
);
1425 ret
= tun_get_iff(current
->nsproxy
->net_ns
, tun
, &ifr
);
1429 if (copy_to_user(argp
, &ifr
, ifreq_len
))
1434 /* Disable/Enable checksum */
1436 /* [unimplemented] */
1437 tun_debug(KERN_INFO
, tun
, "ignored: set checksum %s\n",
1438 arg
? "disabled" : "enabled");
1442 /* Disable/Enable persist mode. Keep an extra reference to the
1443 * module to prevent the module being unprobed.
1446 tun
->flags
|= TUN_PERSIST
;
1447 __module_get(THIS_MODULE
);
1449 tun
->flags
&= ~TUN_PERSIST
;
1450 module_put(THIS_MODULE
);
1453 tun_debug(KERN_INFO
, tun
, "persist %s\n",
1454 arg
? "enabled" : "disabled");
1458 /* Set owner of the device */
1459 owner
= make_kuid(current_user_ns(), arg
);
1460 if (!uid_valid(owner
)) {
1465 tun_debug(KERN_INFO
, tun
, "owner set to %u\n",
1466 from_kuid(&init_user_ns
, tun
->owner
));
1470 /* Set group of the device */
1471 group
= make_kgid(current_user_ns(), arg
);
1472 if (!gid_valid(group
)) {
1477 tun_debug(KERN_INFO
, tun
, "group set to %u\n",
1478 from_kgid(&init_user_ns
, tun
->group
));
1482 /* Only allow setting the type when the interface is down */
1483 if (tun
->dev
->flags
& IFF_UP
) {
1484 tun_debug(KERN_INFO
, tun
,
1485 "Linktype set failed because interface is up\n");
1488 tun
->dev
->type
= (int) arg
;
1489 tun_debug(KERN_INFO
, tun
, "linktype set to %d\n",
1501 ret
= set_offload(tun
, arg
);
1504 case TUNSETTXFILTER
:
1505 /* Can be set only for TAPs */
1507 if ((tun
->flags
& TUN_TYPE_MASK
) != TUN_TAP_DEV
)
1509 ret
= update_filter(&tun
->txflt
, (void __user
*)arg
);
1513 /* Get hw address */
1514 memcpy(ifr
.ifr_hwaddr
.sa_data
, tun
->dev
->dev_addr
, ETH_ALEN
);
1515 ifr
.ifr_hwaddr
.sa_family
= tun
->dev
->type
;
1516 if (copy_to_user(argp
, &ifr
, ifreq_len
))
1521 /* Set hw address */
1522 tun_debug(KERN_DEBUG
, tun
, "set hw address: %pM\n",
1523 ifr
.ifr_hwaddr
.sa_data
);
1525 ret
= dev_set_mac_address(tun
->dev
, &ifr
.ifr_hwaddr
);
1529 sndbuf
= tfile
->socket
.sk
->sk_sndbuf
;
1530 if (copy_to_user(argp
, &sndbuf
, sizeof(sndbuf
)))
1535 if (copy_from_user(&sndbuf
, argp
, sizeof(sndbuf
))) {
1540 tun
->sndbuf
= tfile
->socket
.sk
->sk_sndbuf
= sndbuf
;
1543 case TUNGETVNETHDRSZ
:
1544 vnet_hdr_sz
= tun
->vnet_hdr_sz
;
1545 if (copy_to_user(argp
, &vnet_hdr_sz
, sizeof(vnet_hdr_sz
)))
1549 case TUNSETVNETHDRSZ
:
1550 if (copy_from_user(&vnet_hdr_sz
, argp
, sizeof(vnet_hdr_sz
))) {
1554 if (vnet_hdr_sz
< (int)sizeof(struct virtio_net_hdr
)) {
1559 tun
->vnet_hdr_sz
= vnet_hdr_sz
;
1562 case TUNATTACHFILTER
:
1563 /* Can be set only for TAPs */
1565 if ((tun
->flags
& TUN_TYPE_MASK
) != TUN_TAP_DEV
)
1568 if (copy_from_user(&tun
->fprog
, argp
, sizeof(tun
->fprog
)))
1571 ret
= sk_attach_filter(&tun
->fprog
, tfile
->socket
.sk
);
1573 tun
->filter_attached
= true;
1576 case TUNDETACHFILTER
:
1577 /* Can be set only for TAPs */
1579 if ((tun
->flags
& TUN_TYPE_MASK
) != TUN_TAP_DEV
)
1581 ret
= sk_detach_filter(tfile
->socket
.sk
);
1583 tun
->filter_attached
= false;
1598 static long tun_chr_ioctl(struct file
*file
,
1599 unsigned int cmd
, unsigned long arg
)
1601 return __tun_chr_ioctl(file
, cmd
, arg
, sizeof (struct ifreq
));
1604 #ifdef CONFIG_COMPAT
1605 static long tun_chr_compat_ioctl(struct file
*file
,
1606 unsigned int cmd
, unsigned long arg
)
1611 case TUNSETTXFILTER
:
1616 arg
= (unsigned long)compat_ptr(arg
);
1619 arg
= (compat_ulong_t
)arg
;
1624 * compat_ifreq is shorter than ifreq, so we must not access beyond
1625 * the end of that structure. All fields that are used in this
1626 * driver are compatible though, we don't need to convert the
1629 return __tun_chr_ioctl(file
, cmd
, arg
, sizeof(struct compat_ifreq
));
1631 #endif /* CONFIG_COMPAT */
1633 static int tun_chr_fasync(int fd
, struct file
*file
, int on
)
1635 struct tun_file
*tfile
= file
->private_data
;
1638 if ((ret
= fasync_helper(fd
, file
, on
, &tfile
->fasync
)) < 0)
1642 ret
= __f_setown(file
, task_pid(current
), PIDTYPE_PID
, 0);
1645 tfile
->flags
|= TUN_FASYNC
;
1647 tfile
->flags
&= ~TUN_FASYNC
;
1653 static int tun_chr_open(struct inode
*inode
, struct file
* file
)
1655 struct tun_file
*tfile
;
1657 DBG1(KERN_INFO
, "tunX: tun_chr_open\n");
1659 tfile
= (struct tun_file
*)sk_alloc(&init_net
, AF_UNSPEC
, GFP_KERNEL
,
1663 rcu_assign_pointer(tfile
->tun
, NULL
);
1664 tfile
->net
= get_net(current
->nsproxy
->net_ns
);
1667 rcu_assign_pointer(tfile
->socket
.wq
, &tfile
->wq
);
1668 init_waitqueue_head(&tfile
->wq
.wait
);
1670 tfile
->socket
.file
= file
;
1671 tfile
->socket
.ops
= &tun_socket_ops
;
1673 sock_init_data(&tfile
->socket
, &tfile
->sk
);
1674 sk_change_net(&tfile
->sk
, tfile
->net
);
1676 tfile
->sk
.sk_write_space
= tun_sock_write_space
;
1677 tfile
->sk
.sk_sndbuf
= INT_MAX
;
1679 file
->private_data
= tfile
;
1680 set_bit(SOCK_EXTERNALLY_ALLOCATED
, &tfile
->socket
.flags
);
1685 static int tun_chr_close(struct inode
*inode
, struct file
*file
)
1687 struct tun_file
*tfile
= file
->private_data
;
1688 struct tun_struct
*tun
;
1689 struct net
*net
= tfile
->net
;
1693 tun
= rcu_dereference_protected(tfile
->tun
, lockdep_rtnl_is_held());
1695 struct net_device
*dev
= tun
->dev
;
1697 tun_debug(KERN_INFO
, tun
, "tun_chr_close\n");
1703 /* If desirable, unregister the netdevice. */
1704 if (!(tun
->flags
& TUN_PERSIST
)) {
1705 if (dev
->reg_state
== NETREG_REGISTERED
)
1706 unregister_netdevice(dev
);
1709 /* drop the reference that netdevice holds */
1710 sock_put(&tfile
->sk
);
1715 /* drop the reference that file holds */
1716 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED
,
1717 &tfile
->socket
.flags
));
1718 sk_release_kernel(&tfile
->sk
);
1724 static const struct file_operations tun_fops
= {
1725 .owner
= THIS_MODULE
,
1726 .llseek
= no_llseek
,
1727 .read
= do_sync_read
,
1728 .aio_read
= tun_chr_aio_read
,
1729 .write
= do_sync_write
,
1730 .aio_write
= tun_chr_aio_write
,
1731 .poll
= tun_chr_poll
,
1732 .unlocked_ioctl
= tun_chr_ioctl
,
1733 #ifdef CONFIG_COMPAT
1734 .compat_ioctl
= tun_chr_compat_ioctl
,
1736 .open
= tun_chr_open
,
1737 .release
= tun_chr_close
,
1738 .fasync
= tun_chr_fasync
1741 static struct miscdevice tun_miscdev
= {
1744 .nodename
= "net/tun",
1748 /* ethtool interface */
1750 static int tun_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1753 cmd
->advertising
= 0;
1754 ethtool_cmd_speed_set(cmd
, SPEED_10
);
1755 cmd
->duplex
= DUPLEX_FULL
;
1756 cmd
->port
= PORT_TP
;
1757 cmd
->phy_address
= 0;
1758 cmd
->transceiver
= XCVR_INTERNAL
;
1759 cmd
->autoneg
= AUTONEG_DISABLE
;
1765 static void tun_get_drvinfo(struct net_device
*dev
, struct ethtool_drvinfo
*info
)
1767 struct tun_struct
*tun
= netdev_priv(dev
);
1769 strlcpy(info
->driver
, DRV_NAME
, sizeof(info
->driver
));
1770 strlcpy(info
->version
, DRV_VERSION
, sizeof(info
->version
));
1772 switch (tun
->flags
& TUN_TYPE_MASK
) {
1774 strlcpy(info
->bus_info
, "tun", sizeof(info
->bus_info
));
1777 strlcpy(info
->bus_info
, "tap", sizeof(info
->bus_info
));
1782 static u32
tun_get_msglevel(struct net_device
*dev
)
1785 struct tun_struct
*tun
= netdev_priv(dev
);
1792 static void tun_set_msglevel(struct net_device
*dev
, u32 value
)
1795 struct tun_struct
*tun
= netdev_priv(dev
);
1800 static const struct ethtool_ops tun_ethtool_ops
= {
1801 .get_settings
= tun_get_settings
,
1802 .get_drvinfo
= tun_get_drvinfo
,
1803 .get_msglevel
= tun_get_msglevel
,
1804 .set_msglevel
= tun_set_msglevel
,
1805 .get_link
= ethtool_op_get_link
,
1809 static int __init
tun_init(void)
1813 pr_info("%s, %s\n", DRV_DESCRIPTION
, DRV_VERSION
);
1814 pr_info("%s\n", DRV_COPYRIGHT
);
1816 ret
= rtnl_link_register(&tun_link_ops
);
1818 pr_err("Can't register link_ops\n");
1822 ret
= misc_register(&tun_miscdev
);
1824 pr_err("Can't register misc device %d\n", TUN_MINOR
);
1829 rtnl_link_unregister(&tun_link_ops
);
1834 static void tun_cleanup(void)
1836 misc_deregister(&tun_miscdev
);
1837 rtnl_link_unregister(&tun_link_ops
);
1840 /* Get an underlying socket object from tun file. Returns error unless file is
1841 * attached to a device. The returned object works like a packet socket, it
1842 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
1843 * holding a reference to the file for as long as the socket is in use. */
1844 struct socket
*tun_get_socket(struct file
*file
)
1846 struct tun_file
*tfile
;
1847 if (file
->f_op
!= &tun_fops
)
1848 return ERR_PTR(-EINVAL
);
1849 tfile
= file
->private_data
;
1851 return ERR_PTR(-EBADFD
);
1852 return &tfile
->socket
;
1854 EXPORT_SYMBOL_GPL(tun_get_socket
);
1856 module_init(tun_init
);
1857 module_exit(tun_cleanup
);
1858 MODULE_DESCRIPTION(DRV_DESCRIPTION
);
1859 MODULE_AUTHOR(DRV_COPYRIGHT
);
1860 MODULE_LICENSE("GPL");
1861 MODULE_ALIAS_MISCDEV(TUN_MINOR
);
1862 MODULE_ALIAS("devname:net/tun");