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/if_vlan.h>
64 #include <linux/crc32.h>
65 #include <linux/nsproxy.h>
66 #include <linux/virtio_net.h>
67 #include <linux/rcupdate.h>
68 #include <net/net_namespace.h>
69 #include <net/netns/generic.h>
70 #include <net/rtnetlink.h>
72 #include <linux/seq_file.h>
73 #include <linux/uio.h>
75 #include <asm/uaccess.h>
77 /* Uncomment to enable debugging */
78 /* #define TUN_DEBUG 1 */
83 #define tun_debug(level, tun, fmt, args...) \
86 netdev_printk(level, tun->dev, fmt, ##args); \
88 #define DBG1(level, fmt, args...) \
91 printk(level fmt, ##args); \
94 #define tun_debug(level, tun, fmt, args...) \
97 netdev_printk(level, tun->dev, fmt, ##args); \
99 #define DBG1(level, fmt, args...) \
102 printk(level fmt, ##args); \
106 /* TUN device flags */
108 /* IFF_ATTACH_QUEUE is never stored in device flags,
109 * overload it to mean fasync when stored there.
111 #define TUN_FASYNC IFF_ATTACH_QUEUE
112 /* High bits in flags field are unused. */
113 #define TUN_VNET_LE 0x80000000
115 #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
117 #define GOODCOPY_LEN 128
119 #define FLT_EXACT_COUNT 8
121 unsigned int count
; /* Number of addrs. Zero means disabled */
122 u32 mask
[2]; /* Mask of the hashed addrs */
123 unsigned char addr
[FLT_EXACT_COUNT
][ETH_ALEN
];
126 /* DEFAULT_MAX_NUM_RSS_QUEUES were chosen to let the rx/tx queues allocated for
127 * the netdevice to be fit in one page. So we can make sure the success of
128 * memory allocation. TODO: increase the limit. */
129 #define MAX_TAP_QUEUES DEFAULT_MAX_NUM_RSS_QUEUES
130 #define MAX_TAP_FLOWS 4096
132 #define TUN_FLOW_EXPIRE (3 * HZ)
134 /* A tun_file connects an open character device to a tuntap netdevice. It
135 * also contains all socket related structures (except sock_fprog and tap_filter)
136 * to serve as one transmit queue for tuntap device. The sock_fprog and
137 * tap_filter were kept in tun_struct since they were used for filtering for the
138 * netdevice not for a specific queue (at least I didn't see the requirement for
142 * The tun_file and tun_struct are loosely coupled, the pointer from one to the
143 * other can only be read while rcu_read_lock or rtnl_lock is held.
147 struct socket socket
;
149 struct tun_struct __rcu
*tun
;
151 struct fasync_struct
*fasync
;
152 /* only used for fasnyc */
156 unsigned int ifindex
;
158 struct list_head next
;
159 struct tun_struct
*detached
;
162 struct tun_flow_entry
{
163 struct hlist_node hash_link
;
165 struct tun_struct
*tun
;
170 unsigned long updated
;
173 #define TUN_NUM_FLOW_ENTRIES 1024
175 /* Since the socket were moved to tun_file, to preserve the behavior of persist
176 * device, socket filter, sndbuf and vnet header size were restore when the
177 * file were attached to a persist device.
180 struct tun_file __rcu
*tfiles
[MAX_TAP_QUEUES
];
181 unsigned int numqueues
;
186 struct net_device
*dev
;
187 netdev_features_t set_features
;
188 #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
189 NETIF_F_TSO6|NETIF_F_UFO)
193 struct tap_filter txflt
;
194 struct sock_fprog fprog
;
195 /* protected by rtnl lock */
196 bool filter_attached
;
201 struct hlist_head flows
[TUN_NUM_FLOW_ENTRIES
];
202 struct timer_list flow_gc_timer
;
203 unsigned long ageing_time
;
204 unsigned int numdisabled
;
205 struct list_head disabled
;
210 static inline u16
tun16_to_cpu(struct tun_struct
*tun
, __virtio16 val
)
212 return __virtio16_to_cpu(tun
->flags
& TUN_VNET_LE
, val
);
215 static inline __virtio16
cpu_to_tun16(struct tun_struct
*tun
, u16 val
)
217 return __cpu_to_virtio16(tun
->flags
& TUN_VNET_LE
, val
);
220 static inline u32
tun_hashfn(u32 rxhash
)
222 return rxhash
& 0x3ff;
225 static struct tun_flow_entry
*tun_flow_find(struct hlist_head
*head
, u32 rxhash
)
227 struct tun_flow_entry
*e
;
229 hlist_for_each_entry_rcu(e
, head
, hash_link
) {
230 if (e
->rxhash
== rxhash
)
236 static struct tun_flow_entry
*tun_flow_create(struct tun_struct
*tun
,
237 struct hlist_head
*head
,
238 u32 rxhash
, u16 queue_index
)
240 struct tun_flow_entry
*e
= kmalloc(sizeof(*e
), GFP_ATOMIC
);
243 tun_debug(KERN_INFO
, tun
, "create flow: hash %u index %u\n",
244 rxhash
, queue_index
);
245 e
->updated
= jiffies
;
248 e
->queue_index
= queue_index
;
250 hlist_add_head_rcu(&e
->hash_link
, head
);
256 static void tun_flow_delete(struct tun_struct
*tun
, struct tun_flow_entry
*e
)
258 tun_debug(KERN_INFO
, tun
, "delete flow: hash %u index %u\n",
259 e
->rxhash
, e
->queue_index
);
260 sock_rps_reset_flow_hash(e
->rps_rxhash
);
261 hlist_del_rcu(&e
->hash_link
);
266 static void tun_flow_flush(struct tun_struct
*tun
)
270 spin_lock_bh(&tun
->lock
);
271 for (i
= 0; i
< TUN_NUM_FLOW_ENTRIES
; i
++) {
272 struct tun_flow_entry
*e
;
273 struct hlist_node
*n
;
275 hlist_for_each_entry_safe(e
, n
, &tun
->flows
[i
], hash_link
)
276 tun_flow_delete(tun
, e
);
278 spin_unlock_bh(&tun
->lock
);
281 static void tun_flow_delete_by_queue(struct tun_struct
*tun
, u16 queue_index
)
285 spin_lock_bh(&tun
->lock
);
286 for (i
= 0; i
< TUN_NUM_FLOW_ENTRIES
; i
++) {
287 struct tun_flow_entry
*e
;
288 struct hlist_node
*n
;
290 hlist_for_each_entry_safe(e
, n
, &tun
->flows
[i
], hash_link
) {
291 if (e
->queue_index
== queue_index
)
292 tun_flow_delete(tun
, e
);
295 spin_unlock_bh(&tun
->lock
);
298 static void tun_flow_cleanup(unsigned long data
)
300 struct tun_struct
*tun
= (struct tun_struct
*)data
;
301 unsigned long delay
= tun
->ageing_time
;
302 unsigned long next_timer
= jiffies
+ delay
;
303 unsigned long count
= 0;
306 tun_debug(KERN_INFO
, tun
, "tun_flow_cleanup\n");
308 spin_lock_bh(&tun
->lock
);
309 for (i
= 0; i
< TUN_NUM_FLOW_ENTRIES
; i
++) {
310 struct tun_flow_entry
*e
;
311 struct hlist_node
*n
;
313 hlist_for_each_entry_safe(e
, n
, &tun
->flows
[i
], hash_link
) {
314 unsigned long this_timer
;
316 this_timer
= e
->updated
+ delay
;
317 if (time_before_eq(this_timer
, jiffies
))
318 tun_flow_delete(tun
, e
);
319 else if (time_before(this_timer
, next_timer
))
320 next_timer
= this_timer
;
325 mod_timer(&tun
->flow_gc_timer
, round_jiffies_up(next_timer
));
326 spin_unlock_bh(&tun
->lock
);
329 static void tun_flow_update(struct tun_struct
*tun
, u32 rxhash
,
330 struct tun_file
*tfile
)
332 struct hlist_head
*head
;
333 struct tun_flow_entry
*e
;
334 unsigned long delay
= tun
->ageing_time
;
335 u16 queue_index
= tfile
->queue_index
;
340 head
= &tun
->flows
[tun_hashfn(rxhash
)];
344 /* We may get a very small possibility of OOO during switching, not
345 * worth to optimize.*/
346 if (tun
->numqueues
== 1 || tfile
->detached
)
349 e
= tun_flow_find(head
, rxhash
);
351 /* TODO: keep queueing to old queue until it's empty? */
352 e
->queue_index
= queue_index
;
353 e
->updated
= jiffies
;
354 sock_rps_record_flow_hash(e
->rps_rxhash
);
356 spin_lock_bh(&tun
->lock
);
357 if (!tun_flow_find(head
, rxhash
) &&
358 tun
->flow_count
< MAX_TAP_FLOWS
)
359 tun_flow_create(tun
, head
, rxhash
, queue_index
);
361 if (!timer_pending(&tun
->flow_gc_timer
))
362 mod_timer(&tun
->flow_gc_timer
,
363 round_jiffies_up(jiffies
+ delay
));
364 spin_unlock_bh(&tun
->lock
);
372 * Save the hash received in the stack receive path and update the
373 * flow_hash table accordingly.
375 static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry
*e
, u32 hash
)
377 if (unlikely(e
->rps_rxhash
!= hash
)) {
378 sock_rps_reset_flow_hash(e
->rps_rxhash
);
379 e
->rps_rxhash
= hash
;
383 /* We try to identify a flow through its rxhash first. The reason that
384 * we do not check rxq no. is because some cards(e.g 82599), chooses
385 * the rxq based on the txq where the last packet of the flow comes. As
386 * the userspace application move between processors, we may get a
387 * different rxq no. here. If we could not get rxhash, then we would
388 * hope the rxq no. may help here.
390 static u16
tun_select_queue(struct net_device
*dev
, struct sk_buff
*skb
,
391 void *accel_priv
, select_queue_fallback_t fallback
)
393 struct tun_struct
*tun
= netdev_priv(dev
);
394 struct tun_flow_entry
*e
;
399 numqueues
= ACCESS_ONCE(tun
->numqueues
);
401 txq
= skb_get_hash(skb
);
403 e
= tun_flow_find(&tun
->flows
[tun_hashfn(txq
)], txq
);
405 tun_flow_save_rps_rxhash(e
, txq
);
406 txq
= e
->queue_index
;
408 /* use multiply and shift instead of expensive divide */
409 txq
= ((u64
)txq
* numqueues
) >> 32;
410 } else if (likely(skb_rx_queue_recorded(skb
))) {
411 txq
= skb_get_rx_queue(skb
);
412 while (unlikely(txq
>= numqueues
))
420 static inline bool tun_not_capable(struct tun_struct
*tun
)
422 const struct cred
*cred
= current_cred();
423 struct net
*net
= dev_net(tun
->dev
);
425 return ((uid_valid(tun
->owner
) && !uid_eq(cred
->euid
, tun
->owner
)) ||
426 (gid_valid(tun
->group
) && !in_egroup_p(tun
->group
))) &&
427 !ns_capable(net
->user_ns
, CAP_NET_ADMIN
);
430 static void tun_set_real_num_queues(struct tun_struct
*tun
)
432 netif_set_real_num_tx_queues(tun
->dev
, tun
->numqueues
);
433 netif_set_real_num_rx_queues(tun
->dev
, tun
->numqueues
);
436 static void tun_disable_queue(struct tun_struct
*tun
, struct tun_file
*tfile
)
438 tfile
->detached
= tun
;
439 list_add_tail(&tfile
->next
, &tun
->disabled
);
443 static struct tun_struct
*tun_enable_queue(struct tun_file
*tfile
)
445 struct tun_struct
*tun
= tfile
->detached
;
447 tfile
->detached
= NULL
;
448 list_del_init(&tfile
->next
);
453 static void tun_queue_purge(struct tun_file
*tfile
)
455 skb_queue_purge(&tfile
->sk
.sk_receive_queue
);
456 skb_queue_purge(&tfile
->sk
.sk_error_queue
);
459 static void __tun_detach(struct tun_file
*tfile
, bool clean
)
461 struct tun_file
*ntfile
;
462 struct tun_struct
*tun
;
464 tun
= rtnl_dereference(tfile
->tun
);
466 if (tun
&& !tfile
->detached
) {
467 u16 index
= tfile
->queue_index
;
468 BUG_ON(index
>= tun
->numqueues
);
470 rcu_assign_pointer(tun
->tfiles
[index
],
471 tun
->tfiles
[tun
->numqueues
- 1]);
472 ntfile
= rtnl_dereference(tun
->tfiles
[index
]);
473 ntfile
->queue_index
= index
;
477 RCU_INIT_POINTER(tfile
->tun
, NULL
);
478 sock_put(&tfile
->sk
);
480 tun_disable_queue(tun
, tfile
);
483 tun_flow_delete_by_queue(tun
, tun
->numqueues
+ 1);
484 /* Drop read queue */
485 tun_queue_purge(tfile
);
486 tun_set_real_num_queues(tun
);
487 } else if (tfile
->detached
&& clean
) {
488 tun
= tun_enable_queue(tfile
);
489 sock_put(&tfile
->sk
);
493 if (tun
&& tun
->numqueues
== 0 && tun
->numdisabled
== 0) {
494 netif_carrier_off(tun
->dev
);
496 if (!(tun
->flags
& IFF_PERSIST
) &&
497 tun
->dev
->reg_state
== NETREG_REGISTERED
)
498 unregister_netdevice(tun
->dev
);
501 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED
,
502 &tfile
->socket
.flags
));
503 sk_release_kernel(&tfile
->sk
);
507 static void tun_detach(struct tun_file
*tfile
, bool clean
)
510 __tun_detach(tfile
, clean
);
514 static void tun_detach_all(struct net_device
*dev
)
516 struct tun_struct
*tun
= netdev_priv(dev
);
517 struct tun_file
*tfile
, *tmp
;
518 int i
, n
= tun
->numqueues
;
520 for (i
= 0; i
< n
; i
++) {
521 tfile
= rtnl_dereference(tun
->tfiles
[i
]);
523 tfile
->socket
.sk
->sk_data_ready(tfile
->socket
.sk
);
524 RCU_INIT_POINTER(tfile
->tun
, NULL
);
527 list_for_each_entry(tfile
, &tun
->disabled
, next
) {
528 tfile
->socket
.sk
->sk_data_ready(tfile
->socket
.sk
);
529 RCU_INIT_POINTER(tfile
->tun
, NULL
);
531 BUG_ON(tun
->numqueues
!= 0);
534 for (i
= 0; i
< n
; i
++) {
535 tfile
= rtnl_dereference(tun
->tfiles
[i
]);
536 /* Drop read queue */
537 tun_queue_purge(tfile
);
538 sock_put(&tfile
->sk
);
540 list_for_each_entry_safe(tfile
, tmp
, &tun
->disabled
, next
) {
541 tun_enable_queue(tfile
);
542 tun_queue_purge(tfile
);
543 sock_put(&tfile
->sk
);
545 BUG_ON(tun
->numdisabled
!= 0);
547 if (tun
->flags
& IFF_PERSIST
)
548 module_put(THIS_MODULE
);
551 static int tun_attach(struct tun_struct
*tun
, struct file
*file
, bool skip_filter
)
553 struct tun_file
*tfile
= file
->private_data
;
556 err
= security_tun_dev_attach(tfile
->socket
.sk
, tun
->security
);
561 if (rtnl_dereference(tfile
->tun
) && !tfile
->detached
)
565 if (!(tun
->flags
& IFF_MULTI_QUEUE
) && tun
->numqueues
== 1)
569 if (!tfile
->detached
&&
570 tun
->numqueues
+ tun
->numdisabled
== MAX_TAP_QUEUES
)
575 /* Re-attach the filter to persist device */
576 if (!skip_filter
&& (tun
->filter_attached
== true)) {
577 err
= sk_attach_filter(&tun
->fprog
, tfile
->socket
.sk
);
581 tfile
->queue_index
= tun
->numqueues
;
582 rcu_assign_pointer(tfile
->tun
, tun
);
583 rcu_assign_pointer(tun
->tfiles
[tun
->numqueues
], tfile
);
587 tun_enable_queue(tfile
);
589 sock_hold(&tfile
->sk
);
591 tun_set_real_num_queues(tun
);
593 /* device is allowed to go away first, so no need to hold extra
601 static struct tun_struct
*__tun_get(struct tun_file
*tfile
)
603 struct tun_struct
*tun
;
606 tun
= rcu_dereference(tfile
->tun
);
614 static struct tun_struct
*tun_get(struct file
*file
)
616 return __tun_get(file
->private_data
);
619 static void tun_put(struct tun_struct
*tun
)
625 static void addr_hash_set(u32
*mask
, const u8
*addr
)
627 int n
= ether_crc(ETH_ALEN
, addr
) >> 26;
628 mask
[n
>> 5] |= (1 << (n
& 31));
631 static unsigned int addr_hash_test(const u32
*mask
, const u8
*addr
)
633 int n
= ether_crc(ETH_ALEN
, addr
) >> 26;
634 return mask
[n
>> 5] & (1 << (n
& 31));
637 static int update_filter(struct tap_filter
*filter
, void __user
*arg
)
639 struct { u8 u
[ETH_ALEN
]; } *addr
;
640 struct tun_filter uf
;
641 int err
, alen
, n
, nexact
;
643 if (copy_from_user(&uf
, arg
, sizeof(uf
)))
652 alen
= ETH_ALEN
* uf
.count
;
653 addr
= kmalloc(alen
, GFP_KERNEL
);
657 if (copy_from_user(addr
, arg
+ sizeof(uf
), alen
)) {
662 /* The filter is updated without holding any locks. Which is
663 * perfectly safe. We disable it first and in the worst
664 * case we'll accept a few undesired packets. */
668 /* Use first set of addresses as an exact filter */
669 for (n
= 0; n
< uf
.count
&& n
< FLT_EXACT_COUNT
; n
++)
670 memcpy(filter
->addr
[n
], addr
[n
].u
, ETH_ALEN
);
674 /* Remaining multicast addresses are hashed,
675 * unicast will leave the filter disabled. */
676 memset(filter
->mask
, 0, sizeof(filter
->mask
));
677 for (; n
< uf
.count
; n
++) {
678 if (!is_multicast_ether_addr(addr
[n
].u
)) {
679 err
= 0; /* no filter */
682 addr_hash_set(filter
->mask
, addr
[n
].u
);
685 /* For ALLMULTI just set the mask to all ones.
686 * This overrides the mask populated above. */
687 if ((uf
.flags
& TUN_FLT_ALLMULTI
))
688 memset(filter
->mask
, ~0, sizeof(filter
->mask
));
690 /* Now enable the filter */
692 filter
->count
= nexact
;
694 /* Return the number of exact filters */
702 /* Returns: 0 - drop, !=0 - accept */
703 static int run_filter(struct tap_filter
*filter
, const struct sk_buff
*skb
)
705 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
707 struct ethhdr
*eh
= (struct ethhdr
*) skb
->data
;
711 for (i
= 0; i
< filter
->count
; i
++)
712 if (ether_addr_equal(eh
->h_dest
, filter
->addr
[i
]))
715 /* Inexact match (multicast only) */
716 if (is_multicast_ether_addr(eh
->h_dest
))
717 return addr_hash_test(filter
->mask
, eh
->h_dest
);
723 * Checks whether the packet is accepted or not.
724 * Returns: 0 - drop, !=0 - accept
726 static int check_filter(struct tap_filter
*filter
, const struct sk_buff
*skb
)
731 return run_filter(filter
, skb
);
734 /* Network device part of the driver */
736 static const struct ethtool_ops tun_ethtool_ops
;
738 /* Net device detach from fd. */
739 static void tun_net_uninit(struct net_device
*dev
)
744 /* Net device open. */
745 static int tun_net_open(struct net_device
*dev
)
747 netif_tx_start_all_queues(dev
);
751 /* Net device close. */
752 static int tun_net_close(struct net_device
*dev
)
754 netif_tx_stop_all_queues(dev
);
758 /* Net device start xmit */
759 static netdev_tx_t
tun_net_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
761 struct tun_struct
*tun
= netdev_priv(dev
);
762 int txq
= skb
->queue_mapping
;
763 struct tun_file
*tfile
;
767 tfile
= rcu_dereference(tun
->tfiles
[txq
]);
768 numqueues
= ACCESS_ONCE(tun
->numqueues
);
770 /* Drop packet if interface is not attached */
771 if (txq
>= numqueues
)
774 if (numqueues
== 1) {
775 /* Select queue was not called for the skbuff, so we extract the
776 * RPS hash and save it into the flow_table here.
780 rxhash
= skb_get_hash(skb
);
782 struct tun_flow_entry
*e
;
783 e
= tun_flow_find(&tun
->flows
[tun_hashfn(rxhash
)],
786 tun_flow_save_rps_rxhash(e
, rxhash
);
790 tun_debug(KERN_INFO
, tun
, "tun_net_xmit %d\n", skb
->len
);
794 /* Drop if the filter does not like it.
795 * This is a noop if the filter is disabled.
796 * Filter can be enabled only for the TAP devices. */
797 if (!check_filter(&tun
->txflt
, skb
))
800 if (tfile
->socket
.sk
->sk_filter
&&
801 sk_filter(tfile
->socket
.sk
, skb
))
804 /* Limit the number of packets queued by dividing txq length with the
807 if (skb_queue_len(&tfile
->socket
.sk
->sk_receive_queue
) * numqueues
808 >= dev
->tx_queue_len
)
811 if (unlikely(skb_orphan_frags(skb
, GFP_ATOMIC
)))
815 sock_tx_timestamp(skb
->sk
, &skb_shinfo(skb
)->tx_flags
);
816 sw_tx_timestamp(skb
);
819 /* Orphan the skb - required as we might hang on to it
820 * for indefinite time.
827 skb_queue_tail(&tfile
->socket
.sk
->sk_receive_queue
, skb
);
829 /* Notify and wake up reader process */
830 if (tfile
->flags
& TUN_FASYNC
)
831 kill_fasync(&tfile
->fasync
, SIGIO
, POLL_IN
);
832 tfile
->socket
.sk
->sk_data_ready(tfile
->socket
.sk
);
838 dev
->stats
.tx_dropped
++;
842 return NET_XMIT_DROP
;
845 static void tun_net_mclist(struct net_device
*dev
)
848 * This callback is supposed to deal with mc filter in
849 * _rx_ path and has nothing to do with the _tx_ path.
850 * In rx path we always accept everything userspace gives us.
855 #define MAX_MTU 65535
858 tun_net_change_mtu(struct net_device
*dev
, int new_mtu
)
860 if (new_mtu
< MIN_MTU
|| new_mtu
+ dev
->hard_header_len
> MAX_MTU
)
866 static netdev_features_t
tun_net_fix_features(struct net_device
*dev
,
867 netdev_features_t features
)
869 struct tun_struct
*tun
= netdev_priv(dev
);
871 return (features
& tun
->set_features
) | (features
& ~TUN_USER_FEATURES
);
873 #ifdef CONFIG_NET_POLL_CONTROLLER
874 static void tun_poll_controller(struct net_device
*dev
)
877 * Tun only receives frames when:
878 * 1) the char device endpoint gets data from user space
879 * 2) the tun socket gets a sendmsg call from user space
880 * Since both of those are synchronous operations, we are guaranteed
881 * never to have pending data when we poll for it
882 * so there is nothing to do here but return.
883 * We need this though so netpoll recognizes us as an interface that
884 * supports polling, which enables bridge devices in virt setups to
885 * still use netconsole
890 static const struct net_device_ops tun_netdev_ops
= {
891 .ndo_uninit
= tun_net_uninit
,
892 .ndo_open
= tun_net_open
,
893 .ndo_stop
= tun_net_close
,
894 .ndo_start_xmit
= tun_net_xmit
,
895 .ndo_change_mtu
= tun_net_change_mtu
,
896 .ndo_fix_features
= tun_net_fix_features
,
897 .ndo_select_queue
= tun_select_queue
,
898 #ifdef CONFIG_NET_POLL_CONTROLLER
899 .ndo_poll_controller
= tun_poll_controller
,
903 static const struct net_device_ops tap_netdev_ops
= {
904 .ndo_uninit
= tun_net_uninit
,
905 .ndo_open
= tun_net_open
,
906 .ndo_stop
= tun_net_close
,
907 .ndo_start_xmit
= tun_net_xmit
,
908 .ndo_change_mtu
= tun_net_change_mtu
,
909 .ndo_fix_features
= tun_net_fix_features
,
910 .ndo_set_rx_mode
= tun_net_mclist
,
911 .ndo_set_mac_address
= eth_mac_addr
,
912 .ndo_validate_addr
= eth_validate_addr
,
913 .ndo_select_queue
= tun_select_queue
,
914 #ifdef CONFIG_NET_POLL_CONTROLLER
915 .ndo_poll_controller
= tun_poll_controller
,
919 static void tun_flow_init(struct tun_struct
*tun
)
923 for (i
= 0; i
< TUN_NUM_FLOW_ENTRIES
; i
++)
924 INIT_HLIST_HEAD(&tun
->flows
[i
]);
926 tun
->ageing_time
= TUN_FLOW_EXPIRE
;
927 setup_timer(&tun
->flow_gc_timer
, tun_flow_cleanup
, (unsigned long)tun
);
928 mod_timer(&tun
->flow_gc_timer
,
929 round_jiffies_up(jiffies
+ tun
->ageing_time
));
932 static void tun_flow_uninit(struct tun_struct
*tun
)
934 del_timer_sync(&tun
->flow_gc_timer
);
938 /* Initialize net device. */
939 static void tun_net_init(struct net_device
*dev
)
941 struct tun_struct
*tun
= netdev_priv(dev
);
943 switch (tun
->flags
& TUN_TYPE_MASK
) {
945 dev
->netdev_ops
= &tun_netdev_ops
;
947 /* Point-to-Point TUN Device */
948 dev
->hard_header_len
= 0;
952 /* Zero header length */
953 dev
->type
= ARPHRD_NONE
;
954 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
955 dev
->tx_queue_len
= TUN_READQ_SIZE
; /* We prefer our own queue length */
959 dev
->netdev_ops
= &tap_netdev_ops
;
960 /* Ethernet TAP Device */
962 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
963 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
965 eth_hw_addr_random(dev
);
967 dev
->tx_queue_len
= TUN_READQ_SIZE
; /* We prefer our own queue length */
972 /* Character device part */
975 static unsigned int tun_chr_poll(struct file
*file
, poll_table
*wait
)
977 struct tun_file
*tfile
= file
->private_data
;
978 struct tun_struct
*tun
= __tun_get(tfile
);
980 unsigned int mask
= 0;
985 sk
= tfile
->socket
.sk
;
987 tun_debug(KERN_INFO
, tun
, "tun_chr_poll\n");
989 poll_wait(file
, sk_sleep(sk
), wait
);
991 if (!skb_queue_empty(&sk
->sk_receive_queue
))
992 mask
|= POLLIN
| POLLRDNORM
;
994 if (sock_writeable(sk
) ||
995 (!test_and_set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
) &&
997 mask
|= POLLOUT
| POLLWRNORM
;
999 if (tun
->dev
->reg_state
!= NETREG_REGISTERED
)
1006 /* prepad is the amount to reserve at front. len is length after that.
1007 * linear is a hint as to how much to copy (usually headers). */
1008 static struct sk_buff
*tun_alloc_skb(struct tun_file
*tfile
,
1009 size_t prepad
, size_t len
,
1010 size_t linear
, int noblock
)
1012 struct sock
*sk
= tfile
->socket
.sk
;
1013 struct sk_buff
*skb
;
1016 /* Under a page? Don't bother with paged skb. */
1017 if (prepad
+ len
< PAGE_SIZE
|| !linear
)
1020 skb
= sock_alloc_send_pskb(sk
, prepad
+ linear
, len
- linear
, noblock
,
1023 return ERR_PTR(err
);
1025 skb_reserve(skb
, prepad
);
1026 skb_put(skb
, linear
);
1027 skb
->data_len
= len
- linear
;
1028 skb
->len
+= len
- linear
;
1033 /* Get packet from user space buffer */
1034 static ssize_t
tun_get_user(struct tun_struct
*tun
, struct tun_file
*tfile
,
1035 void *msg_control
, struct iov_iter
*from
,
1038 struct tun_pi pi
= { 0, cpu_to_be16(ETH_P_IP
) };
1039 struct sk_buff
*skb
;
1040 size_t total_len
= iov_iter_count(from
);
1041 size_t len
= total_len
, align
= NET_SKB_PAD
, linear
;
1042 struct virtio_net_hdr gso
= { 0 };
1045 bool zerocopy
= false;
1050 if (!(tun
->flags
& IFF_NO_PI
)) {
1051 if (len
< sizeof(pi
))
1055 n
= copy_from_iter(&pi
, sizeof(pi
), from
);
1056 if (n
!= sizeof(pi
))
1060 if (tun
->flags
& IFF_VNET_HDR
) {
1061 if (len
< tun
->vnet_hdr_sz
)
1063 len
-= tun
->vnet_hdr_sz
;
1065 n
= copy_from_iter(&gso
, sizeof(gso
), from
);
1066 if (n
!= sizeof(gso
))
1069 if ((gso
.flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) &&
1070 tun16_to_cpu(tun
, gso
.csum_start
) + tun16_to_cpu(tun
, gso
.csum_offset
) + 2 > tun16_to_cpu(tun
, gso
.hdr_len
))
1071 gso
.hdr_len
= cpu_to_tun16(tun
, tun16_to_cpu(tun
, gso
.csum_start
) + tun16_to_cpu(tun
, gso
.csum_offset
) + 2);
1073 if (tun16_to_cpu(tun
, gso
.hdr_len
) > len
)
1075 iov_iter_advance(from
, tun
->vnet_hdr_sz
- sizeof(gso
));
1078 if ((tun
->flags
& TUN_TYPE_MASK
) == IFF_TAP
) {
1079 align
+= NET_IP_ALIGN
;
1080 if (unlikely(len
< ETH_HLEN
||
1081 (gso
.hdr_len
&& tun16_to_cpu(tun
, gso
.hdr_len
) < ETH_HLEN
)))
1085 good_linear
= SKB_MAX_HEAD(align
);
1088 struct iov_iter i
= *from
;
1090 /* There are 256 bytes to be copied in skb, so there is
1091 * enough room for skb expand head in case it is used.
1092 * The rest of the buffer is mapped from userspace.
1094 copylen
= gso
.hdr_len
? tun16_to_cpu(tun
, gso
.hdr_len
) : GOODCOPY_LEN
;
1095 if (copylen
> good_linear
)
1096 copylen
= good_linear
;
1098 iov_iter_advance(&i
, copylen
);
1099 if (iov_iter_npages(&i
, INT_MAX
) <= MAX_SKB_FRAGS
)
1105 if (tun16_to_cpu(tun
, gso
.hdr_len
) > good_linear
)
1106 linear
= good_linear
;
1108 linear
= tun16_to_cpu(tun
, gso
.hdr_len
);
1111 skb
= tun_alloc_skb(tfile
, align
, copylen
, linear
, noblock
);
1113 if (PTR_ERR(skb
) != -EAGAIN
)
1114 tun
->dev
->stats
.rx_dropped
++;
1115 return PTR_ERR(skb
);
1119 err
= zerocopy_sg_from_iter(skb
, from
);
1121 err
= skb_copy_datagram_from_iter(skb
, 0, from
, len
);
1122 if (!err
&& msg_control
) {
1123 struct ubuf_info
*uarg
= msg_control
;
1124 uarg
->callback(uarg
, false);
1129 tun
->dev
->stats
.rx_dropped
++;
1134 if (gso
.flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) {
1135 if (!skb_partial_csum_set(skb
, tun16_to_cpu(tun
, gso
.csum_start
),
1136 tun16_to_cpu(tun
, gso
.csum_offset
))) {
1137 tun
->dev
->stats
.rx_frame_errors
++;
1143 switch (tun
->flags
& TUN_TYPE_MASK
) {
1145 if (tun
->flags
& IFF_NO_PI
) {
1146 switch (skb
->data
[0] & 0xf0) {
1148 pi
.proto
= htons(ETH_P_IP
);
1151 pi
.proto
= htons(ETH_P_IPV6
);
1154 tun
->dev
->stats
.rx_dropped
++;
1160 skb_reset_mac_header(skb
);
1161 skb
->protocol
= pi
.proto
;
1162 skb
->dev
= tun
->dev
;
1165 skb
->protocol
= eth_type_trans(skb
, tun
->dev
);
1169 if (gso
.gso_type
!= VIRTIO_NET_HDR_GSO_NONE
) {
1171 switch (gso
.gso_type
& ~VIRTIO_NET_HDR_GSO_ECN
) {
1172 case VIRTIO_NET_HDR_GSO_TCPV4
:
1173 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV4
;
1175 case VIRTIO_NET_HDR_GSO_TCPV6
:
1176 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV6
;
1178 case VIRTIO_NET_HDR_GSO_UDP
:
1179 skb_shinfo(skb
)->gso_type
= SKB_GSO_UDP
;
1182 tun
->dev
->stats
.rx_frame_errors
++;
1187 if (gso
.gso_type
& VIRTIO_NET_HDR_GSO_ECN
)
1188 skb_shinfo(skb
)->gso_type
|= SKB_GSO_TCP_ECN
;
1190 skb_shinfo(skb
)->gso_size
= tun16_to_cpu(tun
, gso
.gso_size
);
1191 if (skb_shinfo(skb
)->gso_size
== 0) {
1192 tun
->dev
->stats
.rx_frame_errors
++;
1197 /* Header must be checked, and gso_segs computed. */
1198 skb_shinfo(skb
)->gso_type
|= SKB_GSO_DODGY
;
1199 skb_shinfo(skb
)->gso_segs
= 0;
1202 /* copy skb_ubuf_info for callback when skb has no error */
1204 skb_shinfo(skb
)->destructor_arg
= msg_control
;
1205 skb_shinfo(skb
)->tx_flags
|= SKBTX_DEV_ZEROCOPY
;
1206 skb_shinfo(skb
)->tx_flags
|= SKBTX_SHARED_FRAG
;
1209 skb_reset_network_header(skb
);
1210 skb_probe_transport_header(skb
, 0);
1212 rxhash
= skb_get_hash(skb
);
1215 tun
->dev
->stats
.rx_packets
++;
1216 tun
->dev
->stats
.rx_bytes
+= len
;
1218 tun_flow_update(tun
, rxhash
, tfile
);
1222 static ssize_t
tun_chr_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1224 struct file
*file
= iocb
->ki_filp
;
1225 struct tun_struct
*tun
= tun_get(file
);
1226 struct tun_file
*tfile
= file
->private_data
;
1232 result
= tun_get_user(tun
, tfile
, NULL
, from
, file
->f_flags
& O_NONBLOCK
);
1238 /* Put packet to the user space buffer */
1239 static ssize_t
tun_put_user(struct tun_struct
*tun
,
1240 struct tun_file
*tfile
,
1241 struct sk_buff
*skb
,
1242 struct iov_iter
*iter
)
1244 struct tun_pi pi
= { 0, skb
->protocol
};
1246 int vlan_offset
= 0;
1248 int vnet_hdr_sz
= 0;
1250 if (vlan_tx_tag_present(skb
))
1251 vlan_hlen
= VLAN_HLEN
;
1253 if (tun
->flags
& IFF_VNET_HDR
)
1254 vnet_hdr_sz
= tun
->vnet_hdr_sz
;
1256 total
= skb
->len
+ vlan_hlen
+ vnet_hdr_sz
;
1258 if (!(tun
->flags
& IFF_NO_PI
)) {
1259 if (iov_iter_count(iter
) < sizeof(pi
))
1262 total
+= sizeof(pi
);
1263 if (iov_iter_count(iter
) < total
) {
1264 /* Packet will be striped */
1265 pi
.flags
|= TUN_PKT_STRIP
;
1268 if (copy_to_iter(&pi
, sizeof(pi
), iter
) != sizeof(pi
))
1273 struct virtio_net_hdr gso
= { 0 }; /* no info leak */
1274 if (iov_iter_count(iter
) < vnet_hdr_sz
)
1277 if (skb_is_gso(skb
)) {
1278 struct skb_shared_info
*sinfo
= skb_shinfo(skb
);
1280 /* This is a hint as to how much should be linear. */
1281 gso
.hdr_len
= cpu_to_tun16(tun
, skb_headlen(skb
));
1282 gso
.gso_size
= cpu_to_tun16(tun
, sinfo
->gso_size
);
1283 if (sinfo
->gso_type
& SKB_GSO_TCPV4
)
1284 gso
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
1285 else if (sinfo
->gso_type
& SKB_GSO_TCPV6
)
1286 gso
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
1287 else if (sinfo
->gso_type
& SKB_GSO_UDP
)
1288 gso
.gso_type
= VIRTIO_NET_HDR_GSO_UDP
;
1290 pr_err("unexpected GSO type: "
1291 "0x%x, gso_size %d, hdr_len %d\n",
1292 sinfo
->gso_type
, tun16_to_cpu(tun
, gso
.gso_size
),
1293 tun16_to_cpu(tun
, gso
.hdr_len
));
1294 print_hex_dump(KERN_ERR
, "tun: ",
1297 min((int)tun16_to_cpu(tun
, gso
.hdr_len
), 64), true);
1301 if (sinfo
->gso_type
& SKB_GSO_TCP_ECN
)
1302 gso
.gso_type
|= VIRTIO_NET_HDR_GSO_ECN
;
1304 gso
.gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
1306 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1307 gso
.flags
= VIRTIO_NET_HDR_F_NEEDS_CSUM
;
1308 gso
.csum_start
= cpu_to_tun16(tun
, skb_checksum_start_offset(skb
) +
1310 gso
.csum_offset
= cpu_to_tun16(tun
, skb
->csum_offset
);
1311 } else if (skb
->ip_summed
== CHECKSUM_UNNECESSARY
) {
1312 gso
.flags
= VIRTIO_NET_HDR_F_DATA_VALID
;
1313 } /* else everything is zero */
1315 if (copy_to_iter(&gso
, sizeof(gso
), iter
) != sizeof(gso
))
1318 iov_iter_advance(iter
, vnet_hdr_sz
- sizeof(gso
));
1324 __be16 h_vlan_proto
;
1328 veth
.h_vlan_proto
= skb
->vlan_proto
;
1329 veth
.h_vlan_TCI
= htons(vlan_tx_tag_get(skb
));
1331 vlan_offset
= offsetof(struct vlan_ethhdr
, h_vlan_proto
);
1333 ret
= skb_copy_datagram_iter(skb
, 0, iter
, vlan_offset
);
1334 if (ret
|| !iov_iter_count(iter
))
1337 ret
= copy_to_iter(&veth
, sizeof(veth
), iter
);
1338 if (ret
!= sizeof(veth
) || !iov_iter_count(iter
))
1342 skb_copy_datagram_iter(skb
, vlan_offset
, iter
, skb
->len
- vlan_offset
);
1345 tun
->dev
->stats
.tx_packets
++;
1346 tun
->dev
->stats
.tx_bytes
+= skb
->len
+ vlan_hlen
;
1351 static ssize_t
tun_do_read(struct tun_struct
*tun
, struct tun_file
*tfile
,
1352 struct iov_iter
*to
,
1355 struct sk_buff
*skb
;
1357 int peeked
, err
, off
= 0;
1359 tun_debug(KERN_INFO
, tun
, "tun_do_read\n");
1361 if (!iov_iter_count(to
))
1364 if (tun
->dev
->reg_state
!= NETREG_REGISTERED
)
1367 /* Read frames from queue */
1368 skb
= __skb_recv_datagram(tfile
->socket
.sk
, noblock
? MSG_DONTWAIT
: 0,
1369 &peeked
, &off
, &err
);
1373 ret
= tun_put_user(tun
, tfile
, skb
, to
);
1374 if (unlikely(ret
< 0))
1382 static ssize_t
tun_chr_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1384 struct file
*file
= iocb
->ki_filp
;
1385 struct tun_file
*tfile
= file
->private_data
;
1386 struct tun_struct
*tun
= __tun_get(tfile
);
1387 ssize_t len
= iov_iter_count(to
), ret
;
1391 ret
= tun_do_read(tun
, tfile
, to
, file
->f_flags
& O_NONBLOCK
);
1392 ret
= min_t(ssize_t
, ret
, len
);
1399 static void tun_free_netdev(struct net_device
*dev
)
1401 struct tun_struct
*tun
= netdev_priv(dev
);
1403 BUG_ON(!(list_empty(&tun
->disabled
)));
1404 tun_flow_uninit(tun
);
1405 security_tun_dev_free_security(tun
->security
);
1409 static void tun_setup(struct net_device
*dev
)
1411 struct tun_struct
*tun
= netdev_priv(dev
);
1413 tun
->owner
= INVALID_UID
;
1414 tun
->group
= INVALID_GID
;
1416 dev
->ethtool_ops
= &tun_ethtool_ops
;
1417 dev
->destructor
= tun_free_netdev
;
1420 /* Trivial set of netlink ops to allow deleting tun or tap
1421 * device with netlink.
1423 static int tun_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1428 static struct rtnl_link_ops tun_link_ops __read_mostly
= {
1430 .priv_size
= sizeof(struct tun_struct
),
1432 .validate
= tun_validate
,
1435 static void tun_sock_write_space(struct sock
*sk
)
1437 struct tun_file
*tfile
;
1438 wait_queue_head_t
*wqueue
;
1440 if (!sock_writeable(sk
))
1443 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
))
1446 wqueue
= sk_sleep(sk
);
1447 if (wqueue
&& waitqueue_active(wqueue
))
1448 wake_up_interruptible_sync_poll(wqueue
, POLLOUT
|
1449 POLLWRNORM
| POLLWRBAND
);
1451 tfile
= container_of(sk
, struct tun_file
, sk
);
1452 kill_fasync(&tfile
->fasync
, SIGIO
, POLL_OUT
);
1455 static int tun_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
1456 struct msghdr
*m
, size_t total_len
)
1459 struct tun_file
*tfile
= container_of(sock
, struct tun_file
, socket
);
1460 struct tun_struct
*tun
= __tun_get(tfile
);
1465 ret
= tun_get_user(tun
, tfile
, m
->msg_control
, &m
->msg_iter
,
1466 m
->msg_flags
& MSG_DONTWAIT
);
1471 static int tun_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1472 struct msghdr
*m
, size_t total_len
,
1475 struct tun_file
*tfile
= container_of(sock
, struct tun_file
, socket
);
1476 struct tun_struct
*tun
= __tun_get(tfile
);
1482 if (flags
& ~(MSG_DONTWAIT
|MSG_TRUNC
|MSG_ERRQUEUE
)) {
1486 if (flags
& MSG_ERRQUEUE
) {
1487 ret
= sock_recv_errqueue(sock
->sk
, m
, total_len
,
1488 SOL_PACKET
, TUN_TX_TIMESTAMP
);
1491 ret
= tun_do_read(tun
, tfile
, &m
->msg_iter
, flags
& MSG_DONTWAIT
);
1492 if (ret
> total_len
) {
1493 m
->msg_flags
|= MSG_TRUNC
;
1494 ret
= flags
& MSG_TRUNC
? ret
: total_len
;
1501 static int tun_release(struct socket
*sock
)
1508 /* Ops structure to mimic raw sockets with tun */
1509 static const struct proto_ops tun_socket_ops
= {
1510 .sendmsg
= tun_sendmsg
,
1511 .recvmsg
= tun_recvmsg
,
1512 .release
= tun_release
,
1515 static struct proto tun_proto
= {
1517 .owner
= THIS_MODULE
,
1518 .obj_size
= sizeof(struct tun_file
),
1521 static int tun_flags(struct tun_struct
*tun
)
1523 return tun
->flags
& (TUN_FEATURES
| IFF_PERSIST
| IFF_TUN
| IFF_TAP
);
1526 static ssize_t
tun_show_flags(struct device
*dev
, struct device_attribute
*attr
,
1529 struct tun_struct
*tun
= netdev_priv(to_net_dev(dev
));
1530 return sprintf(buf
, "0x%x\n", tun_flags(tun
));
1533 static ssize_t
tun_show_owner(struct device
*dev
, struct device_attribute
*attr
,
1536 struct tun_struct
*tun
= netdev_priv(to_net_dev(dev
));
1537 return uid_valid(tun
->owner
)?
1538 sprintf(buf
, "%u\n",
1539 from_kuid_munged(current_user_ns(), tun
->owner
)):
1540 sprintf(buf
, "-1\n");
1543 static ssize_t
tun_show_group(struct device
*dev
, struct device_attribute
*attr
,
1546 struct tun_struct
*tun
= netdev_priv(to_net_dev(dev
));
1547 return gid_valid(tun
->group
) ?
1548 sprintf(buf
, "%u\n",
1549 from_kgid_munged(current_user_ns(), tun
->group
)):
1550 sprintf(buf
, "-1\n");
1553 static DEVICE_ATTR(tun_flags
, 0444, tun_show_flags
, NULL
);
1554 static DEVICE_ATTR(owner
, 0444, tun_show_owner
, NULL
);
1555 static DEVICE_ATTR(group
, 0444, tun_show_group
, NULL
);
1557 static int tun_set_iff(struct net
*net
, struct file
*file
, struct ifreq
*ifr
)
1559 struct tun_struct
*tun
;
1560 struct tun_file
*tfile
= file
->private_data
;
1561 struct net_device
*dev
;
1564 if (tfile
->detached
)
1567 dev
= __dev_get_by_name(net
, ifr
->ifr_name
);
1569 if (ifr
->ifr_flags
& IFF_TUN_EXCL
)
1571 if ((ifr
->ifr_flags
& IFF_TUN
) && dev
->netdev_ops
== &tun_netdev_ops
)
1572 tun
= netdev_priv(dev
);
1573 else if ((ifr
->ifr_flags
& IFF_TAP
) && dev
->netdev_ops
== &tap_netdev_ops
)
1574 tun
= netdev_priv(dev
);
1578 if (!!(ifr
->ifr_flags
& IFF_MULTI_QUEUE
) !=
1579 !!(tun
->flags
& IFF_MULTI_QUEUE
))
1582 if (tun_not_capable(tun
))
1584 err
= security_tun_dev_open(tun
->security
);
1588 err
= tun_attach(tun
, file
, ifr
->ifr_flags
& IFF_NOFILTER
);
1592 if (tun
->flags
& IFF_MULTI_QUEUE
&&
1593 (tun
->numqueues
+ tun
->numdisabled
> 1)) {
1594 /* One or more queue has already been attached, no need
1595 * to initialize the device again.
1602 unsigned long flags
= 0;
1603 int queues
= ifr
->ifr_flags
& IFF_MULTI_QUEUE
?
1606 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1608 err
= security_tun_dev_create();
1613 if (ifr
->ifr_flags
& IFF_TUN
) {
1617 } else if (ifr
->ifr_flags
& IFF_TAP
) {
1625 name
= ifr
->ifr_name
;
1627 dev
= alloc_netdev_mqs(sizeof(struct tun_struct
), name
,
1628 NET_NAME_UNKNOWN
, tun_setup
, queues
,
1634 dev_net_set(dev
, net
);
1635 dev
->rtnl_link_ops
= &tun_link_ops
;
1636 dev
->ifindex
= tfile
->ifindex
;
1638 tun
= netdev_priv(dev
);
1641 tun
->txflt
.count
= 0;
1642 tun
->vnet_hdr_sz
= sizeof(struct virtio_net_hdr
);
1644 tun
->filter_attached
= false;
1645 tun
->sndbuf
= tfile
->socket
.sk
->sk_sndbuf
;
1647 spin_lock_init(&tun
->lock
);
1649 err
= security_tun_dev_alloc_security(&tun
->security
);
1656 dev
->hw_features
= NETIF_F_SG
| NETIF_F_FRAGLIST
|
1657 TUN_USER_FEATURES
| NETIF_F_HW_VLAN_CTAG_TX
|
1658 NETIF_F_HW_VLAN_STAG_TX
;
1659 dev
->features
= dev
->hw_features
;
1660 dev
->vlan_features
= dev
->features
&
1661 ~(NETIF_F_HW_VLAN_CTAG_TX
|
1662 NETIF_F_HW_VLAN_STAG_TX
);
1664 INIT_LIST_HEAD(&tun
->disabled
);
1665 err
= tun_attach(tun
, file
, false);
1669 err
= register_netdevice(tun
->dev
);
1673 if (device_create_file(&tun
->dev
->dev
, &dev_attr_tun_flags
) ||
1674 device_create_file(&tun
->dev
->dev
, &dev_attr_owner
) ||
1675 device_create_file(&tun
->dev
->dev
, &dev_attr_group
))
1676 pr_err("Failed to create tun sysfs files\n");
1679 netif_carrier_on(tun
->dev
);
1681 tun_debug(KERN_INFO
, tun
, "tun_set_iff\n");
1683 tun
->flags
= (tun
->flags
& ~TUN_FEATURES
) |
1684 (ifr
->ifr_flags
& TUN_FEATURES
);
1686 /* Make sure persistent devices do not get stuck in
1689 if (netif_running(tun
->dev
))
1690 netif_tx_wake_all_queues(tun
->dev
);
1692 strcpy(ifr
->ifr_name
, tun
->dev
->name
);
1696 tun_detach_all(dev
);
1698 tun_flow_uninit(tun
);
1699 security_tun_dev_free_security(tun
->security
);
1705 static void tun_get_iff(struct net
*net
, struct tun_struct
*tun
,
1708 tun_debug(KERN_INFO
, tun
, "tun_get_iff\n");
1710 strcpy(ifr
->ifr_name
, tun
->dev
->name
);
1712 ifr
->ifr_flags
= tun_flags(tun
);
1716 /* This is like a cut-down ethtool ops, except done via tun fd so no
1717 * privs required. */
1718 static int set_offload(struct tun_struct
*tun
, unsigned long arg
)
1720 netdev_features_t features
= 0;
1722 if (arg
& TUN_F_CSUM
) {
1723 features
|= NETIF_F_HW_CSUM
;
1726 if (arg
& (TUN_F_TSO4
|TUN_F_TSO6
)) {
1727 if (arg
& TUN_F_TSO_ECN
) {
1728 features
|= NETIF_F_TSO_ECN
;
1729 arg
&= ~TUN_F_TSO_ECN
;
1731 if (arg
& TUN_F_TSO4
)
1732 features
|= NETIF_F_TSO
;
1733 if (arg
& TUN_F_TSO6
)
1734 features
|= NETIF_F_TSO6
;
1735 arg
&= ~(TUN_F_TSO4
|TUN_F_TSO6
);
1738 if (arg
& TUN_F_UFO
) {
1739 features
|= NETIF_F_UFO
;
1744 /* This gives the user a way to test for new features in future by
1745 * trying to set them. */
1749 tun
->set_features
= features
;
1750 netdev_update_features(tun
->dev
);
1755 static void tun_detach_filter(struct tun_struct
*tun
, int n
)
1758 struct tun_file
*tfile
;
1760 for (i
= 0; i
< n
; i
++) {
1761 tfile
= rtnl_dereference(tun
->tfiles
[i
]);
1762 sk_detach_filter(tfile
->socket
.sk
);
1765 tun
->filter_attached
= false;
1768 static int tun_attach_filter(struct tun_struct
*tun
)
1771 struct tun_file
*tfile
;
1773 for (i
= 0; i
< tun
->numqueues
; i
++) {
1774 tfile
= rtnl_dereference(tun
->tfiles
[i
]);
1775 ret
= sk_attach_filter(&tun
->fprog
, tfile
->socket
.sk
);
1777 tun_detach_filter(tun
, i
);
1782 tun
->filter_attached
= true;
1786 static void tun_set_sndbuf(struct tun_struct
*tun
)
1788 struct tun_file
*tfile
;
1791 for (i
= 0; i
< tun
->numqueues
; i
++) {
1792 tfile
= rtnl_dereference(tun
->tfiles
[i
]);
1793 tfile
->socket
.sk
->sk_sndbuf
= tun
->sndbuf
;
1797 static int tun_set_queue(struct file
*file
, struct ifreq
*ifr
)
1799 struct tun_file
*tfile
= file
->private_data
;
1800 struct tun_struct
*tun
;
1805 if (ifr
->ifr_flags
& IFF_ATTACH_QUEUE
) {
1806 tun
= tfile
->detached
;
1811 ret
= security_tun_dev_attach_queue(tun
->security
);
1814 ret
= tun_attach(tun
, file
, false);
1815 } else if (ifr
->ifr_flags
& IFF_DETACH_QUEUE
) {
1816 tun
= rtnl_dereference(tfile
->tun
);
1817 if (!tun
|| !(tun
->flags
& IFF_MULTI_QUEUE
) || tfile
->detached
)
1820 __tun_detach(tfile
, false);
1829 static long __tun_chr_ioctl(struct file
*file
, unsigned int cmd
,
1830 unsigned long arg
, int ifreq_len
)
1832 struct tun_file
*tfile
= file
->private_data
;
1833 struct tun_struct
*tun
;
1834 void __user
* argp
= (void __user
*)arg
;
1840 unsigned int ifindex
;
1844 if (cmd
== TUNSETIFF
|| cmd
== TUNSETQUEUE
|| _IOC_TYPE(cmd
) == 0x89) {
1845 if (copy_from_user(&ifr
, argp
, ifreq_len
))
1848 memset(&ifr
, 0, sizeof(ifr
));
1850 if (cmd
== TUNGETFEATURES
) {
1851 /* Currently this just means: "what IFF flags are valid?".
1852 * This is needed because we never checked for invalid flags on
1855 return put_user(IFF_TUN
| IFF_TAP
| TUN_FEATURES
,
1856 (unsigned int __user
*)argp
);
1857 } else if (cmd
== TUNSETQUEUE
)
1858 return tun_set_queue(file
, &ifr
);
1863 tun
= __tun_get(tfile
);
1864 if (cmd
== TUNSETIFF
&& !tun
) {
1865 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1867 ret
= tun_set_iff(tfile
->net
, file
, &ifr
);
1872 if (copy_to_user(argp
, &ifr
, ifreq_len
))
1876 if (cmd
== TUNSETIFINDEX
) {
1882 if (copy_from_user(&ifindex
, argp
, sizeof(ifindex
)))
1886 tfile
->ifindex
= ifindex
;
1894 tun_debug(KERN_INFO
, tun
, "tun_chr_ioctl cmd %u\n", cmd
);
1899 tun_get_iff(current
->nsproxy
->net_ns
, tun
, &ifr
);
1901 if (tfile
->detached
)
1902 ifr
.ifr_flags
|= IFF_DETACH_QUEUE
;
1903 if (!tfile
->socket
.sk
->sk_filter
)
1904 ifr
.ifr_flags
|= IFF_NOFILTER
;
1906 if (copy_to_user(argp
, &ifr
, ifreq_len
))
1911 /* Disable/Enable checksum */
1913 /* [unimplemented] */
1914 tun_debug(KERN_INFO
, tun
, "ignored: set checksum %s\n",
1915 arg
? "disabled" : "enabled");
1919 /* Disable/Enable persist mode. Keep an extra reference to the
1920 * module to prevent the module being unprobed.
1922 if (arg
&& !(tun
->flags
& IFF_PERSIST
)) {
1923 tun
->flags
|= IFF_PERSIST
;
1924 __module_get(THIS_MODULE
);
1926 if (!arg
&& (tun
->flags
& IFF_PERSIST
)) {
1927 tun
->flags
&= ~IFF_PERSIST
;
1928 module_put(THIS_MODULE
);
1931 tun_debug(KERN_INFO
, tun
, "persist %s\n",
1932 arg
? "enabled" : "disabled");
1936 /* Set owner of the device */
1937 owner
= make_kuid(current_user_ns(), arg
);
1938 if (!uid_valid(owner
)) {
1943 tun_debug(KERN_INFO
, tun
, "owner set to %u\n",
1944 from_kuid(&init_user_ns
, tun
->owner
));
1948 /* Set group of the device */
1949 group
= make_kgid(current_user_ns(), arg
);
1950 if (!gid_valid(group
)) {
1955 tun_debug(KERN_INFO
, tun
, "group set to %u\n",
1956 from_kgid(&init_user_ns
, tun
->group
));
1960 /* Only allow setting the type when the interface is down */
1961 if (tun
->dev
->flags
& IFF_UP
) {
1962 tun_debug(KERN_INFO
, tun
,
1963 "Linktype set failed because interface is up\n");
1966 tun
->dev
->type
= (int) arg
;
1967 tun_debug(KERN_INFO
, tun
, "linktype set to %d\n",
1979 ret
= set_offload(tun
, arg
);
1982 case TUNSETTXFILTER
:
1983 /* Can be set only for TAPs */
1985 if ((tun
->flags
& TUN_TYPE_MASK
) != IFF_TAP
)
1987 ret
= update_filter(&tun
->txflt
, (void __user
*)arg
);
1991 /* Get hw address */
1992 memcpy(ifr
.ifr_hwaddr
.sa_data
, tun
->dev
->dev_addr
, ETH_ALEN
);
1993 ifr
.ifr_hwaddr
.sa_family
= tun
->dev
->type
;
1994 if (copy_to_user(argp
, &ifr
, ifreq_len
))
1999 /* Set hw address */
2000 tun_debug(KERN_DEBUG
, tun
, "set hw address: %pM\n",
2001 ifr
.ifr_hwaddr
.sa_data
);
2003 ret
= dev_set_mac_address(tun
->dev
, &ifr
.ifr_hwaddr
);
2007 sndbuf
= tfile
->socket
.sk
->sk_sndbuf
;
2008 if (copy_to_user(argp
, &sndbuf
, sizeof(sndbuf
)))
2013 if (copy_from_user(&sndbuf
, argp
, sizeof(sndbuf
))) {
2018 tun
->sndbuf
= sndbuf
;
2019 tun_set_sndbuf(tun
);
2022 case TUNGETVNETHDRSZ
:
2023 vnet_hdr_sz
= tun
->vnet_hdr_sz
;
2024 if (copy_to_user(argp
, &vnet_hdr_sz
, sizeof(vnet_hdr_sz
)))
2028 case TUNSETVNETHDRSZ
:
2029 if (copy_from_user(&vnet_hdr_sz
, argp
, sizeof(vnet_hdr_sz
))) {
2033 if (vnet_hdr_sz
< (int)sizeof(struct virtio_net_hdr
)) {
2038 tun
->vnet_hdr_sz
= vnet_hdr_sz
;
2042 le
= !!(tun
->flags
& TUN_VNET_LE
);
2043 if (put_user(le
, (int __user
*)argp
))
2048 if (get_user(le
, (int __user
*)argp
)) {
2053 tun
->flags
|= TUN_VNET_LE
;
2055 tun
->flags
&= ~TUN_VNET_LE
;
2058 case TUNATTACHFILTER
:
2059 /* Can be set only for TAPs */
2061 if ((tun
->flags
& TUN_TYPE_MASK
) != IFF_TAP
)
2064 if (copy_from_user(&tun
->fprog
, argp
, sizeof(tun
->fprog
)))
2067 ret
= tun_attach_filter(tun
);
2070 case TUNDETACHFILTER
:
2071 /* Can be set only for TAPs */
2073 if ((tun
->flags
& TUN_TYPE_MASK
) != IFF_TAP
)
2076 tun_detach_filter(tun
, tun
->numqueues
);
2081 if ((tun
->flags
& TUN_TYPE_MASK
) != IFF_TAP
)
2084 if (copy_to_user(argp
, &tun
->fprog
, sizeof(tun
->fprog
)))
2101 static long tun_chr_ioctl(struct file
*file
,
2102 unsigned int cmd
, unsigned long arg
)
2104 return __tun_chr_ioctl(file
, cmd
, arg
, sizeof (struct ifreq
));
2107 #ifdef CONFIG_COMPAT
2108 static long tun_chr_compat_ioctl(struct file
*file
,
2109 unsigned int cmd
, unsigned long arg
)
2114 case TUNSETTXFILTER
:
2119 arg
= (unsigned long)compat_ptr(arg
);
2122 arg
= (compat_ulong_t
)arg
;
2127 * compat_ifreq is shorter than ifreq, so we must not access beyond
2128 * the end of that structure. All fields that are used in this
2129 * driver are compatible though, we don't need to convert the
2132 return __tun_chr_ioctl(file
, cmd
, arg
, sizeof(struct compat_ifreq
));
2134 #endif /* CONFIG_COMPAT */
2136 static int tun_chr_fasync(int fd
, struct file
*file
, int on
)
2138 struct tun_file
*tfile
= file
->private_data
;
2141 if ((ret
= fasync_helper(fd
, file
, on
, &tfile
->fasync
)) < 0)
2145 __f_setown(file
, task_pid(current
), PIDTYPE_PID
, 0);
2146 tfile
->flags
|= TUN_FASYNC
;
2148 tfile
->flags
&= ~TUN_FASYNC
;
2154 static int tun_chr_open(struct inode
*inode
, struct file
* file
)
2156 struct tun_file
*tfile
;
2158 DBG1(KERN_INFO
, "tunX: tun_chr_open\n");
2160 tfile
= (struct tun_file
*)sk_alloc(&init_net
, AF_UNSPEC
, GFP_KERNEL
,
2164 RCU_INIT_POINTER(tfile
->tun
, NULL
);
2165 tfile
->net
= get_net(current
->nsproxy
->net_ns
);
2169 init_waitqueue_head(&tfile
->wq
.wait
);
2170 RCU_INIT_POINTER(tfile
->socket
.wq
, &tfile
->wq
);
2172 tfile
->socket
.file
= file
;
2173 tfile
->socket
.ops
= &tun_socket_ops
;
2175 sock_init_data(&tfile
->socket
, &tfile
->sk
);
2176 sk_change_net(&tfile
->sk
, tfile
->net
);
2178 tfile
->sk
.sk_write_space
= tun_sock_write_space
;
2179 tfile
->sk
.sk_sndbuf
= INT_MAX
;
2181 file
->private_data
= tfile
;
2182 set_bit(SOCK_EXTERNALLY_ALLOCATED
, &tfile
->socket
.flags
);
2183 INIT_LIST_HEAD(&tfile
->next
);
2185 sock_set_flag(&tfile
->sk
, SOCK_ZEROCOPY
);
2190 static int tun_chr_close(struct inode
*inode
, struct file
*file
)
2192 struct tun_file
*tfile
= file
->private_data
;
2193 struct net
*net
= tfile
->net
;
2195 tun_detach(tfile
, true);
2201 #ifdef CONFIG_PROC_FS
2202 static void tun_chr_show_fdinfo(struct seq_file
*m
, struct file
*f
)
2204 struct tun_struct
*tun
;
2207 memset(&ifr
, 0, sizeof(ifr
));
2212 tun_get_iff(current
->nsproxy
->net_ns
, tun
, &ifr
);
2218 seq_printf(m
, "iff:\t%s\n", ifr
.ifr_name
);
2222 static const struct file_operations tun_fops
= {
2223 .owner
= THIS_MODULE
,
2224 .llseek
= no_llseek
,
2225 .read
= new_sync_read
,
2226 .write
= new_sync_write
,
2227 .read_iter
= tun_chr_read_iter
,
2228 .write_iter
= tun_chr_write_iter
,
2229 .poll
= tun_chr_poll
,
2230 .unlocked_ioctl
= tun_chr_ioctl
,
2231 #ifdef CONFIG_COMPAT
2232 .compat_ioctl
= tun_chr_compat_ioctl
,
2234 .open
= tun_chr_open
,
2235 .release
= tun_chr_close
,
2236 .fasync
= tun_chr_fasync
,
2237 #ifdef CONFIG_PROC_FS
2238 .show_fdinfo
= tun_chr_show_fdinfo
,
2242 static struct miscdevice tun_miscdev
= {
2245 .nodename
= "net/tun",
2249 /* ethtool interface */
2251 static int tun_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
2254 cmd
->advertising
= 0;
2255 ethtool_cmd_speed_set(cmd
, SPEED_10
);
2256 cmd
->duplex
= DUPLEX_FULL
;
2257 cmd
->port
= PORT_TP
;
2258 cmd
->phy_address
= 0;
2259 cmd
->transceiver
= XCVR_INTERNAL
;
2260 cmd
->autoneg
= AUTONEG_DISABLE
;
2266 static void tun_get_drvinfo(struct net_device
*dev
, struct ethtool_drvinfo
*info
)
2268 struct tun_struct
*tun
= netdev_priv(dev
);
2270 strlcpy(info
->driver
, DRV_NAME
, sizeof(info
->driver
));
2271 strlcpy(info
->version
, DRV_VERSION
, sizeof(info
->version
));
2273 switch (tun
->flags
& TUN_TYPE_MASK
) {
2275 strlcpy(info
->bus_info
, "tun", sizeof(info
->bus_info
));
2278 strlcpy(info
->bus_info
, "tap", sizeof(info
->bus_info
));
2283 static u32
tun_get_msglevel(struct net_device
*dev
)
2286 struct tun_struct
*tun
= netdev_priv(dev
);
2293 static void tun_set_msglevel(struct net_device
*dev
, u32 value
)
2296 struct tun_struct
*tun
= netdev_priv(dev
);
2301 static const struct ethtool_ops tun_ethtool_ops
= {
2302 .get_settings
= tun_get_settings
,
2303 .get_drvinfo
= tun_get_drvinfo
,
2304 .get_msglevel
= tun_get_msglevel
,
2305 .set_msglevel
= tun_set_msglevel
,
2306 .get_link
= ethtool_op_get_link
,
2307 .get_ts_info
= ethtool_op_get_ts_info
,
2311 static int __init
tun_init(void)
2315 pr_info("%s, %s\n", DRV_DESCRIPTION
, DRV_VERSION
);
2316 pr_info("%s\n", DRV_COPYRIGHT
);
2318 ret
= rtnl_link_register(&tun_link_ops
);
2320 pr_err("Can't register link_ops\n");
2324 ret
= misc_register(&tun_miscdev
);
2326 pr_err("Can't register misc device %d\n", TUN_MINOR
);
2331 rtnl_link_unregister(&tun_link_ops
);
2336 static void tun_cleanup(void)
2338 misc_deregister(&tun_miscdev
);
2339 rtnl_link_unregister(&tun_link_ops
);
2342 /* Get an underlying socket object from tun file. Returns error unless file is
2343 * attached to a device. The returned object works like a packet socket, it
2344 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
2345 * holding a reference to the file for as long as the socket is in use. */
2346 struct socket
*tun_get_socket(struct file
*file
)
2348 struct tun_file
*tfile
;
2349 if (file
->f_op
!= &tun_fops
)
2350 return ERR_PTR(-EINVAL
);
2351 tfile
= file
->private_data
;
2353 return ERR_PTR(-EBADFD
);
2354 return &tfile
->socket
;
2356 EXPORT_SYMBOL_GPL(tun_get_socket
);
2358 module_init(tun_init
);
2359 module_exit(tun_cleanup
);
2360 MODULE_DESCRIPTION(DRV_DESCRIPTION
);
2361 MODULE_AUTHOR(DRV_COPYRIGHT
);
2362 MODULE_LICENSE("GPL");
2363 MODULE_ALIAS_MISCDEV(TUN_MINOR
);
2364 MODULE_ALIAS("devname:net/tun");