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
121 struct socket socket
;
124 struct tun_struct
*tun
;
126 struct fasync_struct
*fasync
;
127 /* only used for fasnyc */
131 /* Since the socket were moved to tun_file, to preserve the behavior of persist
132 * device, socket fileter, sndbuf and vnet header size were restore when the
133 * file were attached to a persist device.
136 struct tun_file
*tfile
;
141 struct net_device
*dev
;
142 netdev_features_t set_features
;
143 #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
144 NETIF_F_TSO6|NETIF_F_UFO)
148 struct tap_filter txflt
;
149 struct sock_fprog fprog
;
150 /* protected by rtnl lock */
151 bool filter_attached
;
157 static int tun_attach(struct tun_struct
*tun
, struct file
*file
)
159 struct tun_file
*tfile
= file
->private_data
;
164 netif_tx_lock_bh(tun
->dev
);
176 /* Re-attach filter when attaching to a persist device */
177 if (tun
->filter_attached
== true) {
178 err
= sk_attach_filter(&tun
->fprog
, tfile
->socket
.sk
);
183 tfile
->socket
.sk
->sk_sndbuf
= tun
->sndbuf
;
185 netif_carrier_on(tun
->dev
);
187 sock_hold(&tfile
->sk
);
188 atomic_inc(&tfile
->count
);
191 netif_tx_unlock_bh(tun
->dev
);
195 static void __tun_detach(struct tun_struct
*tun
)
197 struct tun_file
*tfile
= tun
->tfile
;
198 /* Detach from net device */
199 netif_tx_lock_bh(tun
->dev
);
200 netif_carrier_off(tun
->dev
);
203 netif_tx_unlock_bh(tun
->dev
);
205 /* Drop read queue */
206 skb_queue_purge(&tfile
->socket
.sk
->sk_receive_queue
);
208 /* Drop the extra count on the net device */
212 static void tun_detach(struct tun_struct
*tun
)
219 static struct tun_struct
*__tun_get(struct tun_file
*tfile
)
221 struct tun_struct
*tun
= NULL
;
223 if (atomic_inc_not_zero(&tfile
->count
))
229 static struct tun_struct
*tun_get(struct file
*file
)
231 return __tun_get(file
->private_data
);
234 static void tun_put(struct tun_struct
*tun
)
236 struct tun_file
*tfile
= tun
->tfile
;
238 if (atomic_dec_and_test(&tfile
->count
))
239 tun_detach(tfile
->tun
);
243 static void addr_hash_set(u32
*mask
, const u8
*addr
)
245 int n
= ether_crc(ETH_ALEN
, addr
) >> 26;
246 mask
[n
>> 5] |= (1 << (n
& 31));
249 static unsigned int addr_hash_test(const u32
*mask
, const u8
*addr
)
251 int n
= ether_crc(ETH_ALEN
, addr
) >> 26;
252 return mask
[n
>> 5] & (1 << (n
& 31));
255 static int update_filter(struct tap_filter
*filter
, void __user
*arg
)
257 struct { u8 u
[ETH_ALEN
]; } *addr
;
258 struct tun_filter uf
;
259 int err
, alen
, n
, nexact
;
261 if (copy_from_user(&uf
, arg
, sizeof(uf
)))
270 alen
= ETH_ALEN
* uf
.count
;
271 addr
= kmalloc(alen
, GFP_KERNEL
);
275 if (copy_from_user(addr
, arg
+ sizeof(uf
), alen
)) {
280 /* The filter is updated without holding any locks. Which is
281 * perfectly safe. We disable it first and in the worst
282 * case we'll accept a few undesired packets. */
286 /* Use first set of addresses as an exact filter */
287 for (n
= 0; n
< uf
.count
&& n
< FLT_EXACT_COUNT
; n
++)
288 memcpy(filter
->addr
[n
], addr
[n
].u
, ETH_ALEN
);
292 /* Remaining multicast addresses are hashed,
293 * unicast will leave the filter disabled. */
294 memset(filter
->mask
, 0, sizeof(filter
->mask
));
295 for (; n
< uf
.count
; n
++) {
296 if (!is_multicast_ether_addr(addr
[n
].u
)) {
297 err
= 0; /* no filter */
300 addr_hash_set(filter
->mask
, addr
[n
].u
);
303 /* For ALLMULTI just set the mask to all ones.
304 * This overrides the mask populated above. */
305 if ((uf
.flags
& TUN_FLT_ALLMULTI
))
306 memset(filter
->mask
, ~0, sizeof(filter
->mask
));
308 /* Now enable the filter */
310 filter
->count
= nexact
;
312 /* Return the number of exact filters */
320 /* Returns: 0 - drop, !=0 - accept */
321 static int run_filter(struct tap_filter
*filter
, const struct sk_buff
*skb
)
323 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
325 struct ethhdr
*eh
= (struct ethhdr
*) skb
->data
;
329 for (i
= 0; i
< filter
->count
; i
++)
330 if (ether_addr_equal(eh
->h_dest
, filter
->addr
[i
]))
333 /* Inexact match (multicast only) */
334 if (is_multicast_ether_addr(eh
->h_dest
))
335 return addr_hash_test(filter
->mask
, eh
->h_dest
);
341 * Checks whether the packet is accepted or not.
342 * Returns: 0 - drop, !=0 - accept
344 static int check_filter(struct tap_filter
*filter
, const struct sk_buff
*skb
)
349 return run_filter(filter
, skb
);
352 /* Network device part of the driver */
354 static const struct ethtool_ops tun_ethtool_ops
;
356 /* Net device detach from fd. */
357 static void tun_net_uninit(struct net_device
*dev
)
359 struct tun_struct
*tun
= netdev_priv(dev
);
360 struct tun_file
*tfile
= tun
->tfile
;
362 /* Inform the methods they need to stop using the dev.
365 wake_up_all(&tfile
->wq
.wait
);
366 if (atomic_dec_and_test(&tfile
->count
))
371 /* Net device open. */
372 static int tun_net_open(struct net_device
*dev
)
374 netif_start_queue(dev
);
378 /* Net device close. */
379 static int tun_net_close(struct net_device
*dev
)
381 netif_stop_queue(dev
);
385 /* Net device start xmit */
386 static netdev_tx_t
tun_net_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
388 struct tun_struct
*tun
= netdev_priv(dev
);
389 struct tun_file
*tfile
= tun
->tfile
;
391 tun_debug(KERN_INFO
, tun
, "tun_net_xmit %d\n", skb
->len
);
393 /* Drop packet if interface is not attached */
397 /* Drop if the filter does not like it.
398 * This is a noop if the filter is disabled.
399 * Filter can be enabled only for the TAP devices. */
400 if (!check_filter(&tun
->txflt
, skb
))
403 if (tfile
->socket
.sk
->sk_filter
&&
404 sk_filter(tfile
->socket
.sk
, skb
))
407 if (skb_queue_len(&tfile
->socket
.sk
->sk_receive_queue
)
408 >= dev
->tx_queue_len
) {
409 if (!(tun
->flags
& TUN_ONE_QUEUE
)) {
410 /* Normal queueing mode. */
411 /* Packet scheduler handles dropping of further packets. */
412 netif_stop_queue(dev
);
414 /* We won't see all dropped packets individually, so overrun
415 * error is more appropriate. */
416 dev
->stats
.tx_fifo_errors
++;
418 /* Single queue mode.
419 * Driver handles dropping of all packets itself. */
424 /* Orphan the skb - required as we might hang on to it
425 * for indefinite time. */
426 if (unlikely(skb_orphan_frags(skb
, GFP_ATOMIC
)))
431 skb_queue_tail(&tfile
->socket
.sk
->sk_receive_queue
, skb
);
433 /* Notify and wake up reader process */
434 if (tfile
->flags
& TUN_FASYNC
)
435 kill_fasync(&tfile
->fasync
, SIGIO
, POLL_IN
);
436 wake_up_interruptible_poll(&tfile
->wq
.wait
, POLLIN
|
437 POLLRDNORM
| POLLRDBAND
);
441 dev
->stats
.tx_dropped
++;
446 static void tun_net_mclist(struct net_device
*dev
)
449 * This callback is supposed to deal with mc filter in
450 * _rx_ path and has nothing to do with the _tx_ path.
451 * In rx path we always accept everything userspace gives us.
456 #define MAX_MTU 65535
459 tun_net_change_mtu(struct net_device
*dev
, int new_mtu
)
461 if (new_mtu
< MIN_MTU
|| new_mtu
+ dev
->hard_header_len
> MAX_MTU
)
467 static netdev_features_t
tun_net_fix_features(struct net_device
*dev
,
468 netdev_features_t features
)
470 struct tun_struct
*tun
= netdev_priv(dev
);
472 return (features
& tun
->set_features
) | (features
& ~TUN_USER_FEATURES
);
474 #ifdef CONFIG_NET_POLL_CONTROLLER
475 static void tun_poll_controller(struct net_device
*dev
)
478 * Tun only receives frames when:
479 * 1) the char device endpoint gets data from user space
480 * 2) the tun socket gets a sendmsg call from user space
481 * Since both of those are syncronous operations, we are guaranteed
482 * never to have pending data when we poll for it
483 * so theres nothing to do here but return.
484 * We need this though so netpoll recognizes us as an interface that
485 * supports polling, which enables bridge devices in virt setups to
486 * still use netconsole
491 static const struct net_device_ops tun_netdev_ops
= {
492 .ndo_uninit
= tun_net_uninit
,
493 .ndo_open
= tun_net_open
,
494 .ndo_stop
= tun_net_close
,
495 .ndo_start_xmit
= tun_net_xmit
,
496 .ndo_change_mtu
= tun_net_change_mtu
,
497 .ndo_fix_features
= tun_net_fix_features
,
498 #ifdef CONFIG_NET_POLL_CONTROLLER
499 .ndo_poll_controller
= tun_poll_controller
,
503 static const struct net_device_ops tap_netdev_ops
= {
504 .ndo_uninit
= tun_net_uninit
,
505 .ndo_open
= tun_net_open
,
506 .ndo_stop
= tun_net_close
,
507 .ndo_start_xmit
= tun_net_xmit
,
508 .ndo_change_mtu
= tun_net_change_mtu
,
509 .ndo_fix_features
= tun_net_fix_features
,
510 .ndo_set_rx_mode
= tun_net_mclist
,
511 .ndo_set_mac_address
= eth_mac_addr
,
512 .ndo_validate_addr
= eth_validate_addr
,
513 #ifdef CONFIG_NET_POLL_CONTROLLER
514 .ndo_poll_controller
= tun_poll_controller
,
518 /* Initialize net device. */
519 static void tun_net_init(struct net_device
*dev
)
521 struct tun_struct
*tun
= netdev_priv(dev
);
523 switch (tun
->flags
& TUN_TYPE_MASK
) {
525 dev
->netdev_ops
= &tun_netdev_ops
;
527 /* Point-to-Point TUN Device */
528 dev
->hard_header_len
= 0;
532 /* Zero header length */
533 dev
->type
= ARPHRD_NONE
;
534 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
535 dev
->tx_queue_len
= TUN_READQ_SIZE
; /* We prefer our own queue length */
539 dev
->netdev_ops
= &tap_netdev_ops
;
540 /* Ethernet TAP Device */
542 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
544 eth_hw_addr_random(dev
);
546 dev
->tx_queue_len
= TUN_READQ_SIZE
; /* We prefer our own queue length */
551 /* Character device part */
554 static unsigned int tun_chr_poll(struct file
*file
, poll_table
* wait
)
556 struct tun_file
*tfile
= file
->private_data
;
557 struct tun_struct
*tun
= __tun_get(tfile
);
559 unsigned int mask
= 0;
564 sk
= tfile
->socket
.sk
;
566 tun_debug(KERN_INFO
, tun
, "tun_chr_poll\n");
568 poll_wait(file
, &tfile
->wq
.wait
, wait
);
570 if (!skb_queue_empty(&sk
->sk_receive_queue
))
571 mask
|= POLLIN
| POLLRDNORM
;
573 if (sock_writeable(sk
) ||
574 (!test_and_set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
) &&
576 mask
|= POLLOUT
| POLLWRNORM
;
578 if (tun
->dev
->reg_state
!= NETREG_REGISTERED
)
585 /* prepad is the amount to reserve at front. len is length after that.
586 * linear is a hint as to how much to copy (usually headers). */
587 static struct sk_buff
*tun_alloc_skb(struct tun_file
*tfile
,
588 size_t prepad
, size_t len
,
589 size_t linear
, int noblock
)
591 struct sock
*sk
= tfile
->socket
.sk
;
595 /* Under a page? Don't bother with paged skb. */
596 if (prepad
+ len
< PAGE_SIZE
|| !linear
)
599 skb
= sock_alloc_send_pskb(sk
, prepad
+ linear
, len
- linear
, noblock
,
604 skb_reserve(skb
, prepad
);
605 skb_put(skb
, linear
);
606 skb
->data_len
= len
- linear
;
607 skb
->len
+= len
- linear
;
612 /* set skb frags from iovec, this can move to core network code for reuse */
613 static int zerocopy_sg_from_iovec(struct sk_buff
*skb
, const struct iovec
*from
,
614 int offset
, size_t count
)
616 int len
= iov_length(from
, count
) - offset
;
617 int copy
= skb_headlen(skb
);
618 int size
, offset1
= 0;
621 /* Skip over from offset */
622 while (count
&& (offset
>= from
->iov_len
)) {
623 offset
-= from
->iov_len
;
628 /* copy up to skb headlen */
629 while (count
&& (copy
> 0)) {
630 size
= min_t(unsigned int, copy
, from
->iov_len
- offset
);
631 if (copy_from_user(skb
->data
+ offset1
, from
->iov_base
+ offset
,
648 struct page
*page
[MAX_SKB_FRAGS
];
651 unsigned long truesize
;
653 len
= from
->iov_len
- offset
;
659 base
= (unsigned long)from
->iov_base
+ offset
;
660 size
= ((base
& ~PAGE_MASK
) + len
+ ~PAGE_MASK
) >> PAGE_SHIFT
;
661 if (i
+ size
> MAX_SKB_FRAGS
)
663 num_pages
= get_user_pages_fast(base
, size
, 0, &page
[i
]);
664 if (num_pages
!= size
) {
665 for (i
= 0; i
< num_pages
; i
++)
669 truesize
= size
* PAGE_SIZE
;
670 skb
->data_len
+= len
;
672 skb
->truesize
+= truesize
;
673 atomic_add(truesize
, &skb
->sk
->sk_wmem_alloc
);
675 int off
= base
& ~PAGE_MASK
;
676 int size
= min_t(int, len
, PAGE_SIZE
- off
);
677 __skb_fill_page_desc(skb
, i
, page
[i
], off
, size
);
678 skb_shinfo(skb
)->nr_frags
++;
679 /* increase sk_wmem_alloc */
690 /* Get packet from user space buffer */
691 static ssize_t
tun_get_user(struct tun_struct
*tun
, struct tun_file
*tfile
,
692 void *msg_control
, const struct iovec
*iv
,
693 size_t total_len
, size_t count
, int noblock
)
695 struct tun_pi pi
= { 0, cpu_to_be16(ETH_P_IP
) };
697 size_t len
= total_len
, align
= NET_SKB_PAD
;
698 struct virtio_net_hdr gso
= { 0 };
701 bool zerocopy
= false;
704 if (!(tun
->flags
& TUN_NO_PI
)) {
705 if ((len
-= sizeof(pi
)) > total_len
)
708 if (memcpy_fromiovecend((void *)&pi
, iv
, 0, sizeof(pi
)))
710 offset
+= sizeof(pi
);
713 if (tun
->flags
& TUN_VNET_HDR
) {
714 if ((len
-= tun
->vnet_hdr_sz
) > total_len
)
717 if (memcpy_fromiovecend((void *)&gso
, iv
, offset
, sizeof(gso
)))
720 if ((gso
.flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) &&
721 gso
.csum_start
+ gso
.csum_offset
+ 2 > gso
.hdr_len
)
722 gso
.hdr_len
= gso
.csum_start
+ gso
.csum_offset
+ 2;
724 if (gso
.hdr_len
> len
)
726 offset
+= tun
->vnet_hdr_sz
;
729 if ((tun
->flags
& TUN_TYPE_MASK
) == TUN_TAP_DEV
) {
730 align
+= NET_IP_ALIGN
;
731 if (unlikely(len
< ETH_HLEN
||
732 (gso
.hdr_len
&& gso
.hdr_len
< ETH_HLEN
)))
740 /* Userspace may produce vectors with count greater than
741 * MAX_SKB_FRAGS, so we need to linearize parts of the skb
742 * to let the rest of data to be fit in the frags.
744 if (count
> MAX_SKB_FRAGS
) {
745 copylen
= iov_length(iv
, count
- MAX_SKB_FRAGS
);
746 if (copylen
< offset
)
752 /* There are 256 bytes to be copied in skb, so there is enough
753 * room for skb expand head in case it is used.
754 * The rest of the buffer is mapped from userspace.
756 if (copylen
< gso
.hdr_len
)
757 copylen
= gso
.hdr_len
;
759 copylen
= GOODCOPY_LEN
;
763 skb
= tun_alloc_skb(tfile
, align
, copylen
, gso
.hdr_len
, noblock
);
765 if (PTR_ERR(skb
) != -EAGAIN
)
766 tun
->dev
->stats
.rx_dropped
++;
771 err
= zerocopy_sg_from_iovec(skb
, iv
, offset
, count
);
773 err
= skb_copy_datagram_from_iovec(skb
, 0, iv
, offset
, len
);
776 tun
->dev
->stats
.rx_dropped
++;
781 if (gso
.flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) {
782 if (!skb_partial_csum_set(skb
, gso
.csum_start
,
784 tun
->dev
->stats
.rx_frame_errors
++;
790 switch (tun
->flags
& TUN_TYPE_MASK
) {
792 if (tun
->flags
& TUN_NO_PI
) {
793 switch (skb
->data
[0] & 0xf0) {
795 pi
.proto
= htons(ETH_P_IP
);
798 pi
.proto
= htons(ETH_P_IPV6
);
801 tun
->dev
->stats
.rx_dropped
++;
807 skb_reset_mac_header(skb
);
808 skb
->protocol
= pi
.proto
;
812 skb
->protocol
= eth_type_trans(skb
, tun
->dev
);
816 if (gso
.gso_type
!= VIRTIO_NET_HDR_GSO_NONE
) {
818 switch (gso
.gso_type
& ~VIRTIO_NET_HDR_GSO_ECN
) {
819 case VIRTIO_NET_HDR_GSO_TCPV4
:
820 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV4
;
822 case VIRTIO_NET_HDR_GSO_TCPV6
:
823 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV6
;
825 case VIRTIO_NET_HDR_GSO_UDP
:
826 skb_shinfo(skb
)->gso_type
= SKB_GSO_UDP
;
829 tun
->dev
->stats
.rx_frame_errors
++;
834 if (gso
.gso_type
& VIRTIO_NET_HDR_GSO_ECN
)
835 skb_shinfo(skb
)->gso_type
|= SKB_GSO_TCP_ECN
;
837 skb_shinfo(skb
)->gso_size
= gso
.gso_size
;
838 if (skb_shinfo(skb
)->gso_size
== 0) {
839 tun
->dev
->stats
.rx_frame_errors
++;
844 /* Header must be checked, and gso_segs computed. */
845 skb_shinfo(skb
)->gso_type
|= SKB_GSO_DODGY
;
846 skb_shinfo(skb
)->gso_segs
= 0;
849 /* copy skb_ubuf_info for callback when skb has no error */
851 skb_shinfo(skb
)->destructor_arg
= msg_control
;
852 skb_shinfo(skb
)->tx_flags
|= SKBTX_DEV_ZEROCOPY
;
857 tun
->dev
->stats
.rx_packets
++;
858 tun
->dev
->stats
.rx_bytes
+= len
;
863 static ssize_t
tun_chr_aio_write(struct kiocb
*iocb
, const struct iovec
*iv
,
864 unsigned long count
, loff_t pos
)
866 struct file
*file
= iocb
->ki_filp
;
867 struct tun_struct
*tun
= tun_get(file
);
868 struct tun_file
*tfile
= file
->private_data
;
874 tun_debug(KERN_INFO
, tun
, "tun_chr_write %ld\n", count
);
876 result
= tun_get_user(tun
, tfile
, NULL
, iv
, iov_length(iv
, count
),
877 count
, file
->f_flags
& O_NONBLOCK
);
883 /* Put packet to the user space buffer */
884 static ssize_t
tun_put_user(struct tun_struct
*tun
,
885 struct tun_file
*tfile
,
887 const struct iovec
*iv
, int len
)
889 struct tun_pi pi
= { 0, skb
->protocol
};
892 if (!(tun
->flags
& TUN_NO_PI
)) {
893 if ((len
-= sizeof(pi
)) < 0)
896 if (len
< skb
->len
) {
897 /* Packet will be striped */
898 pi
.flags
|= TUN_PKT_STRIP
;
901 if (memcpy_toiovecend(iv
, (void *) &pi
, 0, sizeof(pi
)))
906 if (tun
->flags
& TUN_VNET_HDR
) {
907 struct virtio_net_hdr gso
= { 0 }; /* no info leak */
908 if ((len
-= tun
->vnet_hdr_sz
) < 0)
911 if (skb_is_gso(skb
)) {
912 struct skb_shared_info
*sinfo
= skb_shinfo(skb
);
914 /* This is a hint as to how much should be linear. */
915 gso
.hdr_len
= skb_headlen(skb
);
916 gso
.gso_size
= sinfo
->gso_size
;
917 if (sinfo
->gso_type
& SKB_GSO_TCPV4
)
918 gso
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
919 else if (sinfo
->gso_type
& SKB_GSO_TCPV6
)
920 gso
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
921 else if (sinfo
->gso_type
& SKB_GSO_UDP
)
922 gso
.gso_type
= VIRTIO_NET_HDR_GSO_UDP
;
924 pr_err("unexpected GSO type: "
925 "0x%x, gso_size %d, hdr_len %d\n",
926 sinfo
->gso_type
, gso
.gso_size
,
928 print_hex_dump(KERN_ERR
, "tun: ",
931 min((int)gso
.hdr_len
, 64), true);
935 if (sinfo
->gso_type
& SKB_GSO_TCP_ECN
)
936 gso
.gso_type
|= VIRTIO_NET_HDR_GSO_ECN
;
938 gso
.gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
940 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
941 gso
.flags
= VIRTIO_NET_HDR_F_NEEDS_CSUM
;
942 gso
.csum_start
= skb_checksum_start_offset(skb
);
943 gso
.csum_offset
= skb
->csum_offset
;
944 } else if (skb
->ip_summed
== CHECKSUM_UNNECESSARY
) {
945 gso
.flags
= VIRTIO_NET_HDR_F_DATA_VALID
;
946 } /* else everything is zero */
948 if (unlikely(memcpy_toiovecend(iv
, (void *)&gso
, total
,
951 total
+= tun
->vnet_hdr_sz
;
954 len
= min_t(int, skb
->len
, len
);
956 skb_copy_datagram_const_iovec(skb
, 0, iv
, total
, len
);
959 tun
->dev
->stats
.tx_packets
++;
960 tun
->dev
->stats
.tx_bytes
+= len
;
965 static ssize_t
tun_do_read(struct tun_struct
*tun
, struct tun_file
*tfile
,
966 struct kiocb
*iocb
, const struct iovec
*iv
,
967 ssize_t len
, int noblock
)
969 DECLARE_WAITQUEUE(wait
, current
);
973 tun_debug(KERN_INFO
, tun
, "tun_chr_read\n");
975 if (unlikely(!noblock
))
976 add_wait_queue(&tfile
->wq
.wait
, &wait
);
978 current
->state
= TASK_INTERRUPTIBLE
;
980 /* Read frames from the queue */
981 if (!(skb
= skb_dequeue(&tfile
->socket
.sk
->sk_receive_queue
))) {
986 if (signal_pending(current
)) {
990 if (tun
->dev
->reg_state
!= NETREG_REGISTERED
) {
995 /* Nothing to read, let's sleep */
999 netif_wake_queue(tun
->dev
);
1001 ret
= tun_put_user(tun
, tfile
, skb
, iv
, len
);
1006 current
->state
= TASK_RUNNING
;
1007 if (unlikely(!noblock
))
1008 remove_wait_queue(&tfile
->wq
.wait
, &wait
);
1013 static ssize_t
tun_chr_aio_read(struct kiocb
*iocb
, const struct iovec
*iv
,
1014 unsigned long count
, loff_t pos
)
1016 struct file
*file
= iocb
->ki_filp
;
1017 struct tun_file
*tfile
= file
->private_data
;
1018 struct tun_struct
*tun
= __tun_get(tfile
);
1023 len
= iov_length(iv
, count
);
1029 ret
= tun_do_read(tun
, tfile
, iocb
, iv
, len
,
1030 file
->f_flags
& O_NONBLOCK
);
1031 ret
= min_t(ssize_t
, ret
, len
);
1037 static void tun_setup(struct net_device
*dev
)
1039 struct tun_struct
*tun
= netdev_priv(dev
);
1041 tun
->owner
= INVALID_UID
;
1042 tun
->group
= INVALID_GID
;
1044 dev
->ethtool_ops
= &tun_ethtool_ops
;
1045 dev
->destructor
= free_netdev
;
1048 /* Trivial set of netlink ops to allow deleting tun or tap
1049 * device with netlink.
1051 static int tun_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1056 static struct rtnl_link_ops tun_link_ops __read_mostly
= {
1058 .priv_size
= sizeof(struct tun_struct
),
1060 .validate
= tun_validate
,
1063 static void tun_sock_write_space(struct sock
*sk
)
1065 struct tun_file
*tfile
;
1066 wait_queue_head_t
*wqueue
;
1068 if (!sock_writeable(sk
))
1071 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
))
1074 wqueue
= sk_sleep(sk
);
1075 if (wqueue
&& waitqueue_active(wqueue
))
1076 wake_up_interruptible_sync_poll(wqueue
, POLLOUT
|
1077 POLLWRNORM
| POLLWRBAND
);
1079 tfile
= container_of(sk
, struct tun_file
, sk
);
1080 kill_fasync(&tfile
->fasync
, SIGIO
, POLL_OUT
);
1083 static int tun_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
1084 struct msghdr
*m
, size_t total_len
)
1087 struct tun_file
*tfile
= container_of(sock
, struct tun_file
, socket
);
1088 struct tun_struct
*tun
= __tun_get(tfile
);
1093 ret
= tun_get_user(tun
, tfile
, m
->msg_control
, m
->msg_iov
, total_len
,
1094 m
->msg_iovlen
, m
->msg_flags
& MSG_DONTWAIT
);
1100 static int tun_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1101 struct msghdr
*m
, size_t total_len
,
1104 struct tun_file
*tfile
= container_of(sock
, struct tun_file
, socket
);
1105 struct tun_struct
*tun
= __tun_get(tfile
);
1111 if (flags
& ~(MSG_DONTWAIT
|MSG_TRUNC
))
1113 ret
= tun_do_read(tun
, tfile
, iocb
, m
->msg_iov
, total_len
,
1114 flags
& MSG_DONTWAIT
);
1115 if (ret
> total_len
) {
1116 m
->msg_flags
|= MSG_TRUNC
;
1117 ret
= flags
& MSG_TRUNC
? ret
: total_len
;
1123 static int tun_release(struct socket
*sock
)
1130 /* Ops structure to mimic raw sockets with tun */
1131 static const struct proto_ops tun_socket_ops
= {
1132 .sendmsg
= tun_sendmsg
,
1133 .recvmsg
= tun_recvmsg
,
1134 .release
= tun_release
,
1137 static struct proto tun_proto
= {
1139 .owner
= THIS_MODULE
,
1140 .obj_size
= sizeof(struct tun_file
),
1143 static int tun_flags(struct tun_struct
*tun
)
1147 if (tun
->flags
& TUN_TUN_DEV
)
1152 if (tun
->flags
& TUN_NO_PI
)
1155 if (tun
->flags
& TUN_ONE_QUEUE
)
1156 flags
|= IFF_ONE_QUEUE
;
1158 if (tun
->flags
& TUN_VNET_HDR
)
1159 flags
|= IFF_VNET_HDR
;
1164 static ssize_t
tun_show_flags(struct device
*dev
, struct device_attribute
*attr
,
1167 struct tun_struct
*tun
= netdev_priv(to_net_dev(dev
));
1168 return sprintf(buf
, "0x%x\n", tun_flags(tun
));
1171 static ssize_t
tun_show_owner(struct device
*dev
, struct device_attribute
*attr
,
1174 struct tun_struct
*tun
= netdev_priv(to_net_dev(dev
));
1175 return uid_valid(tun
->owner
)?
1176 sprintf(buf
, "%u\n",
1177 from_kuid_munged(current_user_ns(), tun
->owner
)):
1178 sprintf(buf
, "-1\n");
1181 static ssize_t
tun_show_group(struct device
*dev
, struct device_attribute
*attr
,
1184 struct tun_struct
*tun
= netdev_priv(to_net_dev(dev
));
1185 return gid_valid(tun
->group
) ?
1186 sprintf(buf
, "%u\n",
1187 from_kgid_munged(current_user_ns(), tun
->group
)):
1188 sprintf(buf
, "-1\n");
1191 static DEVICE_ATTR(tun_flags
, 0444, tun_show_flags
, NULL
);
1192 static DEVICE_ATTR(owner
, 0444, tun_show_owner
, NULL
);
1193 static DEVICE_ATTR(group
, 0444, tun_show_group
, NULL
);
1195 static int tun_set_iff(struct net
*net
, struct file
*file
, struct ifreq
*ifr
)
1197 struct tun_struct
*tun
;
1198 struct tun_file
*tfile
= file
->private_data
;
1199 struct net_device
*dev
;
1202 dev
= __dev_get_by_name(net
, ifr
->ifr_name
);
1204 const struct cred
*cred
= current_cred();
1206 if (ifr
->ifr_flags
& IFF_TUN_EXCL
)
1208 if ((ifr
->ifr_flags
& IFF_TUN
) && dev
->netdev_ops
== &tun_netdev_ops
)
1209 tun
= netdev_priv(dev
);
1210 else if ((ifr
->ifr_flags
& IFF_TAP
) && dev
->netdev_ops
== &tap_netdev_ops
)
1211 tun
= netdev_priv(dev
);
1215 if (((uid_valid(tun
->owner
) && !uid_eq(cred
->euid
, tun
->owner
)) ||
1216 (gid_valid(tun
->group
) && !in_egroup_p(tun
->group
))) &&
1217 !capable(CAP_NET_ADMIN
))
1219 err
= security_tun_dev_attach(tfile
->socket
.sk
);
1223 err
= tun_attach(tun
, file
);
1229 unsigned long flags
= 0;
1231 if (!capable(CAP_NET_ADMIN
))
1233 err
= security_tun_dev_create();
1238 if (ifr
->ifr_flags
& IFF_TUN
) {
1240 flags
|= TUN_TUN_DEV
;
1242 } else if (ifr
->ifr_flags
& IFF_TAP
) {
1244 flags
|= TUN_TAP_DEV
;
1250 name
= ifr
->ifr_name
;
1252 dev
= alloc_netdev(sizeof(struct tun_struct
), name
,
1257 dev_net_set(dev
, net
);
1258 dev
->rtnl_link_ops
= &tun_link_ops
;
1260 tun
= netdev_priv(dev
);
1263 tun
->txflt
.count
= 0;
1264 tun
->vnet_hdr_sz
= sizeof(struct virtio_net_hdr
);
1266 tun
->filter_attached
= false;
1267 tun
->sndbuf
= tfile
->socket
.sk
->sk_sndbuf
;
1269 security_tun_dev_post_create(&tfile
->sk
);
1273 dev
->hw_features
= NETIF_F_SG
| NETIF_F_FRAGLIST
|
1275 dev
->features
= dev
->hw_features
;
1277 err
= register_netdevice(tun
->dev
);
1281 if (device_create_file(&tun
->dev
->dev
, &dev_attr_tun_flags
) ||
1282 device_create_file(&tun
->dev
->dev
, &dev_attr_owner
) ||
1283 device_create_file(&tun
->dev
->dev
, &dev_attr_group
))
1284 pr_err("Failed to create tun sysfs files\n");
1286 err
= tun_attach(tun
, file
);
1291 tun_debug(KERN_INFO
, tun
, "tun_set_iff\n");
1293 if (ifr
->ifr_flags
& IFF_NO_PI
)
1294 tun
->flags
|= TUN_NO_PI
;
1296 tun
->flags
&= ~TUN_NO_PI
;
1298 if (ifr
->ifr_flags
& IFF_ONE_QUEUE
)
1299 tun
->flags
|= TUN_ONE_QUEUE
;
1301 tun
->flags
&= ~TUN_ONE_QUEUE
;
1303 if (ifr
->ifr_flags
& IFF_VNET_HDR
)
1304 tun
->flags
|= TUN_VNET_HDR
;
1306 tun
->flags
&= ~TUN_VNET_HDR
;
1308 /* Make sure persistent devices do not get stuck in
1311 if (netif_running(tun
->dev
))
1312 netif_wake_queue(tun
->dev
);
1314 strcpy(ifr
->ifr_name
, tun
->dev
->name
);
1323 static int tun_get_iff(struct net
*net
, struct tun_struct
*tun
,
1326 tun_debug(KERN_INFO
, tun
, "tun_get_iff\n");
1328 strcpy(ifr
->ifr_name
, tun
->dev
->name
);
1330 ifr
->ifr_flags
= tun_flags(tun
);
1335 /* This is like a cut-down ethtool ops, except done via tun fd so no
1336 * privs required. */
1337 static int set_offload(struct tun_struct
*tun
, unsigned long arg
)
1339 netdev_features_t features
= 0;
1341 if (arg
& TUN_F_CSUM
) {
1342 features
|= NETIF_F_HW_CSUM
;
1345 if (arg
& (TUN_F_TSO4
|TUN_F_TSO6
)) {
1346 if (arg
& TUN_F_TSO_ECN
) {
1347 features
|= NETIF_F_TSO_ECN
;
1348 arg
&= ~TUN_F_TSO_ECN
;
1350 if (arg
& TUN_F_TSO4
)
1351 features
|= NETIF_F_TSO
;
1352 if (arg
& TUN_F_TSO6
)
1353 features
|= NETIF_F_TSO6
;
1354 arg
&= ~(TUN_F_TSO4
|TUN_F_TSO6
);
1357 if (arg
& TUN_F_UFO
) {
1358 features
|= NETIF_F_UFO
;
1363 /* This gives the user a way to test for new features in future by
1364 * trying to set them. */
1368 tun
->set_features
= features
;
1369 netdev_update_features(tun
->dev
);
1374 static long __tun_chr_ioctl(struct file
*file
, unsigned int cmd
,
1375 unsigned long arg
, int ifreq_len
)
1377 struct tun_file
*tfile
= file
->private_data
;
1378 struct tun_struct
*tun
;
1379 void __user
* argp
= (void __user
*)arg
;
1387 if (cmd
== TUNSETIFF
|| _IOC_TYPE(cmd
) == 0x89) {
1388 if (copy_from_user(&ifr
, argp
, ifreq_len
))
1391 memset(&ifr
, 0, sizeof(ifr
));
1393 if (cmd
== TUNGETFEATURES
) {
1394 /* Currently this just means: "what IFF flags are valid?".
1395 * This is needed because we never checked for invalid flags on
1397 return put_user(IFF_TUN
| IFF_TAP
| IFF_NO_PI
| IFF_ONE_QUEUE
|
1399 (unsigned int __user
*)argp
);
1404 tun
= __tun_get(tfile
);
1405 if (cmd
== TUNSETIFF
&& !tun
) {
1406 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1408 ret
= tun_set_iff(tfile
->net
, file
, &ifr
);
1413 if (copy_to_user(argp
, &ifr
, ifreq_len
))
1422 tun_debug(KERN_INFO
, tun
, "tun_chr_ioctl cmd %u\n", cmd
);
1427 ret
= tun_get_iff(current
->nsproxy
->net_ns
, tun
, &ifr
);
1431 if (copy_to_user(argp
, &ifr
, ifreq_len
))
1436 /* Disable/Enable checksum */
1438 /* [unimplemented] */
1439 tun_debug(KERN_INFO
, tun
, "ignored: set checksum %s\n",
1440 arg
? "disabled" : "enabled");
1444 /* Disable/Enable persist mode. Keep an extra reference to the
1445 * module to prevent the module being unprobed.
1448 tun
->flags
|= TUN_PERSIST
;
1449 __module_get(THIS_MODULE
);
1451 tun
->flags
&= ~TUN_PERSIST
;
1452 module_put(THIS_MODULE
);
1455 tun_debug(KERN_INFO
, tun
, "persist %s\n",
1456 arg
? "enabled" : "disabled");
1460 /* Set owner of the device */
1461 owner
= make_kuid(current_user_ns(), arg
);
1462 if (!uid_valid(owner
)) {
1467 tun_debug(KERN_INFO
, tun
, "owner set to %u\n",
1468 from_kuid(&init_user_ns
, tun
->owner
));
1472 /* Set group of the device */
1473 group
= make_kgid(current_user_ns(), arg
);
1474 if (!gid_valid(group
)) {
1479 tun_debug(KERN_INFO
, tun
, "group set to %u\n",
1480 from_kgid(&init_user_ns
, tun
->group
));
1484 /* Only allow setting the type when the interface is down */
1485 if (tun
->dev
->flags
& IFF_UP
) {
1486 tun_debug(KERN_INFO
, tun
,
1487 "Linktype set failed because interface is up\n");
1490 tun
->dev
->type
= (int) arg
;
1491 tun_debug(KERN_INFO
, tun
, "linktype set to %d\n",
1503 ret
= set_offload(tun
, arg
);
1506 case TUNSETTXFILTER
:
1507 /* Can be set only for TAPs */
1509 if ((tun
->flags
& TUN_TYPE_MASK
) != TUN_TAP_DEV
)
1511 ret
= update_filter(&tun
->txflt
, (void __user
*)arg
);
1515 /* Get hw address */
1516 memcpy(ifr
.ifr_hwaddr
.sa_data
, tun
->dev
->dev_addr
, ETH_ALEN
);
1517 ifr
.ifr_hwaddr
.sa_family
= tun
->dev
->type
;
1518 if (copy_to_user(argp
, &ifr
, ifreq_len
))
1523 /* Set hw address */
1524 tun_debug(KERN_DEBUG
, tun
, "set hw address: %pM\n",
1525 ifr
.ifr_hwaddr
.sa_data
);
1527 ret
= dev_set_mac_address(tun
->dev
, &ifr
.ifr_hwaddr
);
1531 sndbuf
= tfile
->socket
.sk
->sk_sndbuf
;
1532 if (copy_to_user(argp
, &sndbuf
, sizeof(sndbuf
)))
1537 if (copy_from_user(&sndbuf
, argp
, sizeof(sndbuf
))) {
1542 tun
->sndbuf
= tfile
->socket
.sk
->sk_sndbuf
= sndbuf
;
1545 case TUNGETVNETHDRSZ
:
1546 vnet_hdr_sz
= tun
->vnet_hdr_sz
;
1547 if (copy_to_user(argp
, &vnet_hdr_sz
, sizeof(vnet_hdr_sz
)))
1551 case TUNSETVNETHDRSZ
:
1552 if (copy_from_user(&vnet_hdr_sz
, argp
, sizeof(vnet_hdr_sz
))) {
1556 if (vnet_hdr_sz
< (int)sizeof(struct virtio_net_hdr
)) {
1561 tun
->vnet_hdr_sz
= vnet_hdr_sz
;
1564 case TUNATTACHFILTER
:
1565 /* Can be set only for TAPs */
1567 if ((tun
->flags
& TUN_TYPE_MASK
) != TUN_TAP_DEV
)
1570 if (copy_from_user(&tun
->fprog
, argp
, sizeof(tun
->fprog
)))
1573 ret
= sk_attach_filter(&tun
->fprog
, tfile
->socket
.sk
);
1575 tun
->filter_attached
= true;
1578 case TUNDETACHFILTER
:
1579 /* Can be set only for TAPs */
1581 if ((tun
->flags
& TUN_TYPE_MASK
) != TUN_TAP_DEV
)
1583 ret
= sk_detach_filter(tfile
->socket
.sk
);
1585 tun
->filter_attached
= false;
1600 static long tun_chr_ioctl(struct file
*file
,
1601 unsigned int cmd
, unsigned long arg
)
1603 return __tun_chr_ioctl(file
, cmd
, arg
, sizeof (struct ifreq
));
1606 #ifdef CONFIG_COMPAT
1607 static long tun_chr_compat_ioctl(struct file
*file
,
1608 unsigned int cmd
, unsigned long arg
)
1613 case TUNSETTXFILTER
:
1618 arg
= (unsigned long)compat_ptr(arg
);
1621 arg
= (compat_ulong_t
)arg
;
1626 * compat_ifreq is shorter than ifreq, so we must not access beyond
1627 * the end of that structure. All fields that are used in this
1628 * driver are compatible though, we don't need to convert the
1631 return __tun_chr_ioctl(file
, cmd
, arg
, sizeof(struct compat_ifreq
));
1633 #endif /* CONFIG_COMPAT */
1635 static int tun_chr_fasync(int fd
, struct file
*file
, int on
)
1637 struct tun_file
*tfile
= file
->private_data
;
1640 if ((ret
= fasync_helper(fd
, file
, on
, &tfile
->fasync
)) < 0)
1644 ret
= __f_setown(file
, task_pid(current
), PIDTYPE_PID
, 0);
1647 tfile
->flags
|= TUN_FASYNC
;
1649 tfile
->flags
&= ~TUN_FASYNC
;
1655 static int tun_chr_open(struct inode
*inode
, struct file
* file
)
1657 struct tun_file
*tfile
;
1659 DBG1(KERN_INFO
, "tunX: tun_chr_open\n");
1661 tfile
= (struct tun_file
*)sk_alloc(&init_net
, AF_UNSPEC
, GFP_KERNEL
,
1665 atomic_set(&tfile
->count
, 0);
1667 tfile
->net
= get_net(current
->nsproxy
->net_ns
);
1670 rcu_assign_pointer(tfile
->socket
.wq
, &tfile
->wq
);
1671 init_waitqueue_head(&tfile
->wq
.wait
);
1673 tfile
->socket
.file
= file
;
1674 tfile
->socket
.ops
= &tun_socket_ops
;
1676 sock_init_data(&tfile
->socket
, &tfile
->sk
);
1677 sk_change_net(&tfile
->sk
, tfile
->net
);
1679 tfile
->sk
.sk_write_space
= tun_sock_write_space
;
1680 tfile
->sk
.sk_sndbuf
= INT_MAX
;
1682 file
->private_data
= tfile
;
1683 set_bit(SOCK_EXTERNALLY_ALLOCATED
, &tfile
->socket
.flags
);
1688 static int tun_chr_close(struct inode
*inode
, struct file
*file
)
1690 struct tun_file
*tfile
= file
->private_data
;
1691 struct tun_struct
*tun
;
1692 struct net
*net
= tfile
->net
;
1694 tun
= __tun_get(tfile
);
1696 struct net_device
*dev
= tun
->dev
;
1698 tun_debug(KERN_INFO
, tun
, "tun_chr_close\n");
1702 /* If desirable, unregister the netdevice. */
1703 if (!(tun
->flags
& TUN_PERSIST
)) {
1705 if (dev
->reg_state
== NETREG_REGISTERED
)
1706 unregister_netdevice(dev
);
1710 /* drop the reference that netdevice holds */
1711 sock_put(&tfile
->sk
);
1714 /* drop the reference that file holds */
1715 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED
,
1716 &tfile
->socket
.flags
));
1717 sk_release_kernel(&tfile
->sk
);
1723 static const struct file_operations tun_fops
= {
1724 .owner
= THIS_MODULE
,
1725 .llseek
= no_llseek
,
1726 .read
= do_sync_read
,
1727 .aio_read
= tun_chr_aio_read
,
1728 .write
= do_sync_write
,
1729 .aio_write
= tun_chr_aio_write
,
1730 .poll
= tun_chr_poll
,
1731 .unlocked_ioctl
= tun_chr_ioctl
,
1732 #ifdef CONFIG_COMPAT
1733 .compat_ioctl
= tun_chr_compat_ioctl
,
1735 .open
= tun_chr_open
,
1736 .release
= tun_chr_close
,
1737 .fasync
= tun_chr_fasync
1740 static struct miscdevice tun_miscdev
= {
1743 .nodename
= "net/tun",
1747 /* ethtool interface */
1749 static int tun_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1752 cmd
->advertising
= 0;
1753 ethtool_cmd_speed_set(cmd
, SPEED_10
);
1754 cmd
->duplex
= DUPLEX_FULL
;
1755 cmd
->port
= PORT_TP
;
1756 cmd
->phy_address
= 0;
1757 cmd
->transceiver
= XCVR_INTERNAL
;
1758 cmd
->autoneg
= AUTONEG_DISABLE
;
1764 static void tun_get_drvinfo(struct net_device
*dev
, struct ethtool_drvinfo
*info
)
1766 struct tun_struct
*tun
= netdev_priv(dev
);
1768 strlcpy(info
->driver
, DRV_NAME
, sizeof(info
->driver
));
1769 strlcpy(info
->version
, DRV_VERSION
, sizeof(info
->version
));
1771 switch (tun
->flags
& TUN_TYPE_MASK
) {
1773 strlcpy(info
->bus_info
, "tun", sizeof(info
->bus_info
));
1776 strlcpy(info
->bus_info
, "tap", sizeof(info
->bus_info
));
1781 static u32
tun_get_msglevel(struct net_device
*dev
)
1784 struct tun_struct
*tun
= netdev_priv(dev
);
1791 static void tun_set_msglevel(struct net_device
*dev
, u32 value
)
1794 struct tun_struct
*tun
= netdev_priv(dev
);
1799 static const struct ethtool_ops tun_ethtool_ops
= {
1800 .get_settings
= tun_get_settings
,
1801 .get_drvinfo
= tun_get_drvinfo
,
1802 .get_msglevel
= tun_get_msglevel
,
1803 .set_msglevel
= tun_set_msglevel
,
1804 .get_link
= ethtool_op_get_link
,
1808 static int __init
tun_init(void)
1812 pr_info("%s, %s\n", DRV_DESCRIPTION
, DRV_VERSION
);
1813 pr_info("%s\n", DRV_COPYRIGHT
);
1815 ret
= rtnl_link_register(&tun_link_ops
);
1817 pr_err("Can't register link_ops\n");
1821 ret
= misc_register(&tun_miscdev
);
1823 pr_err("Can't register misc device %d\n", TUN_MINOR
);
1828 rtnl_link_unregister(&tun_link_ops
);
1833 static void tun_cleanup(void)
1835 misc_deregister(&tun_miscdev
);
1836 rtnl_link_unregister(&tun_link_ops
);
1839 /* Get an underlying socket object from tun file. Returns error unless file is
1840 * attached to a device. The returned object works like a packet socket, it
1841 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
1842 * holding a reference to the file for as long as the socket is in use. */
1843 struct socket
*tun_get_socket(struct file
*file
)
1845 struct tun_struct
*tun
;
1846 struct tun_file
*tfile
= file
->private_data
;
1847 if (file
->f_op
!= &tun_fops
)
1848 return ERR_PTR(-EINVAL
);
1849 tun
= tun_get(file
);
1851 return ERR_PTR(-EBADFD
);
1853 return &tfile
->socket
;
1855 EXPORT_SYMBOL_GPL(tun_get_socket
);
1857 module_init(tun_init
);
1858 module_exit(tun_cleanup
);
1859 MODULE_DESCRIPTION(DRV_DESCRIPTION
);
1860 MODULE_AUTHOR(DRV_COPYRIGHT
);
1861 MODULE_LICENSE("GPL");
1862 MODULE_ALIAS_MISCDEV(TUN_MINOR
);
1863 MODULE_ALIAS("devname:net/tun");