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>
69 #include <net/net_namespace.h>
70 #include <net/netns/generic.h>
71 #include <net/rtnetlink.h>
73 #include <linux/seq_file.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 #define TUN_NO_PI IFF_NO_PI
113 /* This flag has no real effect */
114 #define TUN_ONE_QUEUE IFF_ONE_QUEUE
115 #define TUN_PERSIST IFF_PERSIST
116 #define TUN_VNET_HDR IFF_VNET_HDR
117 #define TUN_TAP_MQ IFF_MULTI_QUEUE
119 #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
121 #define GOODCOPY_LEN 128
123 #define FLT_EXACT_COUNT 8
125 unsigned int count
; /* Number of addrs. Zero means disabled */
126 u32 mask
[2]; /* Mask of the hashed addrs */
127 unsigned char addr
[FLT_EXACT_COUNT
][ETH_ALEN
];
130 /* DEFAULT_MAX_NUM_RSS_QUEUES were chosen to let the rx/tx queues allocated for
131 * the netdevice to be fit in one page. So we can make sure the success of
132 * memory allocation. TODO: increase the limit. */
133 #define MAX_TAP_QUEUES DEFAULT_MAX_NUM_RSS_QUEUES
134 #define MAX_TAP_FLOWS 4096
136 #define TUN_FLOW_EXPIRE (3 * HZ)
138 /* A tun_file connects an open character device to a tuntap netdevice. It
139 * also contains all socket related structures (except sock_fprog and tap_filter)
140 * to serve as one transmit queue for tuntap device. The sock_fprog and
141 * tap_filter were kept in tun_struct since they were used for filtering for the
142 * netdevice not for a specific queue (at least I didn't see the requirement for
146 * The tun_file and tun_struct are loosely coupled, the pointer from one to the
147 * other can only be read while rcu_read_lock or rtnl_lock is held.
151 struct socket socket
;
153 struct tun_struct __rcu
*tun
;
155 struct fasync_struct
*fasync
;
156 /* only used for fasnyc */
160 unsigned int ifindex
;
162 struct list_head next
;
163 struct tun_struct
*detached
;
166 struct tun_flow_entry
{
167 struct hlist_node hash_link
;
169 struct tun_struct
*tun
;
174 unsigned long updated
;
177 #define TUN_NUM_FLOW_ENTRIES 1024
179 /* Since the socket were moved to tun_file, to preserve the behavior of persist
180 * device, socket filter, sndbuf and vnet header size were restore when the
181 * file were attached to a persist device.
184 struct tun_file __rcu
*tfiles
[MAX_TAP_QUEUES
];
185 unsigned int numqueues
;
190 struct net_device
*dev
;
191 netdev_features_t set_features
;
192 #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
197 struct tap_filter txflt
;
198 struct sock_fprog fprog
;
199 /* protected by rtnl lock */
200 bool filter_attached
;
205 struct hlist_head flows
[TUN_NUM_FLOW_ENTRIES
];
206 struct timer_list flow_gc_timer
;
207 unsigned long ageing_time
;
208 unsigned int numdisabled
;
209 struct list_head disabled
;
214 static inline u32
tun_hashfn(u32 rxhash
)
216 return rxhash
& 0x3ff;
219 static struct tun_flow_entry
*tun_flow_find(struct hlist_head
*head
, u32 rxhash
)
221 struct tun_flow_entry
*e
;
223 hlist_for_each_entry_rcu(e
, head
, hash_link
) {
224 if (e
->rxhash
== rxhash
)
230 static struct tun_flow_entry
*tun_flow_create(struct tun_struct
*tun
,
231 struct hlist_head
*head
,
232 u32 rxhash
, u16 queue_index
)
234 struct tun_flow_entry
*e
= kmalloc(sizeof(*e
), GFP_ATOMIC
);
237 tun_debug(KERN_INFO
, tun
, "create flow: hash %u index %u\n",
238 rxhash
, queue_index
);
239 e
->updated
= jiffies
;
242 e
->queue_index
= queue_index
;
244 hlist_add_head_rcu(&e
->hash_link
, head
);
250 static void tun_flow_delete(struct tun_struct
*tun
, struct tun_flow_entry
*e
)
252 tun_debug(KERN_INFO
, tun
, "delete flow: hash %u index %u\n",
253 e
->rxhash
, e
->queue_index
);
254 sock_rps_reset_flow_hash(e
->rps_rxhash
);
255 hlist_del_rcu(&e
->hash_link
);
260 static void tun_flow_flush(struct tun_struct
*tun
)
264 spin_lock_bh(&tun
->lock
);
265 for (i
= 0; i
< TUN_NUM_FLOW_ENTRIES
; i
++) {
266 struct tun_flow_entry
*e
;
267 struct hlist_node
*n
;
269 hlist_for_each_entry_safe(e
, n
, &tun
->flows
[i
], hash_link
)
270 tun_flow_delete(tun
, e
);
272 spin_unlock_bh(&tun
->lock
);
275 static void tun_flow_delete_by_queue(struct tun_struct
*tun
, u16 queue_index
)
279 spin_lock_bh(&tun
->lock
);
280 for (i
= 0; i
< TUN_NUM_FLOW_ENTRIES
; i
++) {
281 struct tun_flow_entry
*e
;
282 struct hlist_node
*n
;
284 hlist_for_each_entry_safe(e
, n
, &tun
->flows
[i
], hash_link
) {
285 if (e
->queue_index
== queue_index
)
286 tun_flow_delete(tun
, e
);
289 spin_unlock_bh(&tun
->lock
);
292 static void tun_flow_cleanup(unsigned long data
)
294 struct tun_struct
*tun
= (struct tun_struct
*)data
;
295 unsigned long delay
= tun
->ageing_time
;
296 unsigned long next_timer
= jiffies
+ delay
;
297 unsigned long count
= 0;
300 tun_debug(KERN_INFO
, tun
, "tun_flow_cleanup\n");
302 spin_lock_bh(&tun
->lock
);
303 for (i
= 0; i
< TUN_NUM_FLOW_ENTRIES
; i
++) {
304 struct tun_flow_entry
*e
;
305 struct hlist_node
*n
;
307 hlist_for_each_entry_safe(e
, n
, &tun
->flows
[i
], hash_link
) {
308 unsigned long this_timer
;
310 this_timer
= e
->updated
+ delay
;
311 if (time_before_eq(this_timer
, jiffies
))
312 tun_flow_delete(tun
, e
);
313 else if (time_before(this_timer
, next_timer
))
314 next_timer
= this_timer
;
319 mod_timer(&tun
->flow_gc_timer
, round_jiffies_up(next_timer
));
320 spin_unlock_bh(&tun
->lock
);
323 static void tun_flow_update(struct tun_struct
*tun
, u32 rxhash
,
324 struct tun_file
*tfile
)
326 struct hlist_head
*head
;
327 struct tun_flow_entry
*e
;
328 unsigned long delay
= tun
->ageing_time
;
329 u16 queue_index
= tfile
->queue_index
;
334 head
= &tun
->flows
[tun_hashfn(rxhash
)];
338 /* We may get a very small possibility of OOO during switching, not
339 * worth to optimize.*/
340 if (tun
->numqueues
== 1 || tfile
->detached
)
343 e
= tun_flow_find(head
, rxhash
);
345 /* TODO: keep queueing to old queue until it's empty? */
346 e
->queue_index
= queue_index
;
347 e
->updated
= jiffies
;
348 sock_rps_record_flow_hash(e
->rps_rxhash
);
350 spin_lock_bh(&tun
->lock
);
351 if (!tun_flow_find(head
, rxhash
) &&
352 tun
->flow_count
< MAX_TAP_FLOWS
)
353 tun_flow_create(tun
, head
, rxhash
, queue_index
);
355 if (!timer_pending(&tun
->flow_gc_timer
))
356 mod_timer(&tun
->flow_gc_timer
,
357 round_jiffies_up(jiffies
+ delay
));
358 spin_unlock_bh(&tun
->lock
);
366 * Save the hash received in the stack receive path and update the
367 * flow_hash table accordingly.
369 static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry
*e
, u32 hash
)
371 if (unlikely(e
->rps_rxhash
!= hash
)) {
372 sock_rps_reset_flow_hash(e
->rps_rxhash
);
373 e
->rps_rxhash
= hash
;
377 /* We try to identify a flow through its rxhash first. The reason that
378 * we do not check rxq no. is because some cards(e.g 82599), chooses
379 * the rxq based on the txq where the last packet of the flow comes. As
380 * the userspace application move between processors, we may get a
381 * different rxq no. here. If we could not get rxhash, then we would
382 * hope the rxq no. may help here.
384 static u16
tun_select_queue(struct net_device
*dev
, struct sk_buff
*skb
,
385 void *accel_priv
, select_queue_fallback_t fallback
)
387 struct tun_struct
*tun
= netdev_priv(dev
);
388 struct tun_flow_entry
*e
;
393 numqueues
= ACCESS_ONCE(tun
->numqueues
);
395 txq
= skb_get_hash(skb
);
397 e
= tun_flow_find(&tun
->flows
[tun_hashfn(txq
)], txq
);
399 tun_flow_save_rps_rxhash(e
, txq
);
400 txq
= e
->queue_index
;
402 /* use multiply and shift instead of expensive divide */
403 txq
= ((u64
)txq
* numqueues
) >> 32;
404 } else if (likely(skb_rx_queue_recorded(skb
))) {
405 txq
= skb_get_rx_queue(skb
);
406 while (unlikely(txq
>= numqueues
))
414 static inline bool tun_not_capable(struct tun_struct
*tun
)
416 const struct cred
*cred
= current_cred();
417 struct net
*net
= dev_net(tun
->dev
);
419 return ((uid_valid(tun
->owner
) && !uid_eq(cred
->euid
, tun
->owner
)) ||
420 (gid_valid(tun
->group
) && !in_egroup_p(tun
->group
))) &&
421 !ns_capable(net
->user_ns
, CAP_NET_ADMIN
);
424 static void tun_set_real_num_queues(struct tun_struct
*tun
)
426 netif_set_real_num_tx_queues(tun
->dev
, tun
->numqueues
);
427 netif_set_real_num_rx_queues(tun
->dev
, tun
->numqueues
);
430 static void tun_disable_queue(struct tun_struct
*tun
, struct tun_file
*tfile
)
432 tfile
->detached
= tun
;
433 list_add_tail(&tfile
->next
, &tun
->disabled
);
437 static struct tun_struct
*tun_enable_queue(struct tun_file
*tfile
)
439 struct tun_struct
*tun
= tfile
->detached
;
441 tfile
->detached
= NULL
;
442 list_del_init(&tfile
->next
);
447 static void tun_queue_purge(struct tun_file
*tfile
)
449 skb_queue_purge(&tfile
->sk
.sk_receive_queue
);
450 skb_queue_purge(&tfile
->sk
.sk_error_queue
);
453 static void __tun_detach(struct tun_file
*tfile
, bool clean
)
455 struct tun_file
*ntfile
;
456 struct tun_struct
*tun
;
458 tun
= rtnl_dereference(tfile
->tun
);
460 if (tun
&& !tfile
->detached
) {
461 u16 index
= tfile
->queue_index
;
462 BUG_ON(index
>= tun
->numqueues
);
464 rcu_assign_pointer(tun
->tfiles
[index
],
465 tun
->tfiles
[tun
->numqueues
- 1]);
466 ntfile
= rtnl_dereference(tun
->tfiles
[index
]);
467 ntfile
->queue_index
= index
;
471 RCU_INIT_POINTER(tfile
->tun
, NULL
);
472 sock_put(&tfile
->sk
);
474 tun_disable_queue(tun
, tfile
);
477 tun_flow_delete_by_queue(tun
, tun
->numqueues
+ 1);
478 /* Drop read queue */
479 tun_queue_purge(tfile
);
480 tun_set_real_num_queues(tun
);
481 } else if (tfile
->detached
&& clean
) {
482 tun
= tun_enable_queue(tfile
);
483 sock_put(&tfile
->sk
);
487 if (tun
&& tun
->numqueues
== 0 && tun
->numdisabled
== 0) {
488 netif_carrier_off(tun
->dev
);
490 if (!(tun
->flags
& TUN_PERSIST
) &&
491 tun
->dev
->reg_state
== NETREG_REGISTERED
)
492 unregister_netdevice(tun
->dev
);
495 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED
,
496 &tfile
->socket
.flags
));
497 sk_release_kernel(&tfile
->sk
);
501 static void tun_detach(struct tun_file
*tfile
, bool clean
)
504 __tun_detach(tfile
, clean
);
508 static void tun_detach_all(struct net_device
*dev
)
510 struct tun_struct
*tun
= netdev_priv(dev
);
511 struct tun_file
*tfile
, *tmp
;
512 int i
, n
= tun
->numqueues
;
514 for (i
= 0; i
< n
; i
++) {
515 tfile
= rtnl_dereference(tun
->tfiles
[i
]);
517 tfile
->socket
.sk
->sk_data_ready(tfile
->socket
.sk
);
518 RCU_INIT_POINTER(tfile
->tun
, NULL
);
521 list_for_each_entry(tfile
, &tun
->disabled
, next
) {
522 tfile
->socket
.sk
->sk_data_ready(tfile
->socket
.sk
);
523 RCU_INIT_POINTER(tfile
->tun
, NULL
);
525 BUG_ON(tun
->numqueues
!= 0);
528 for (i
= 0; i
< n
; i
++) {
529 tfile
= rtnl_dereference(tun
->tfiles
[i
]);
530 /* Drop read queue */
531 tun_queue_purge(tfile
);
532 sock_put(&tfile
->sk
);
534 list_for_each_entry_safe(tfile
, tmp
, &tun
->disabled
, next
) {
535 tun_enable_queue(tfile
);
536 tun_queue_purge(tfile
);
537 sock_put(&tfile
->sk
);
539 BUG_ON(tun
->numdisabled
!= 0);
541 if (tun
->flags
& TUN_PERSIST
)
542 module_put(THIS_MODULE
);
545 static int tun_attach(struct tun_struct
*tun
, struct file
*file
, bool skip_filter
)
547 struct tun_file
*tfile
= file
->private_data
;
550 err
= security_tun_dev_attach(tfile
->socket
.sk
, tun
->security
);
555 if (rtnl_dereference(tfile
->tun
) && !tfile
->detached
)
559 if (!(tun
->flags
& TUN_TAP_MQ
) && tun
->numqueues
== 1)
563 if (!tfile
->detached
&&
564 tun
->numqueues
+ tun
->numdisabled
== MAX_TAP_QUEUES
)
569 /* Re-attach the filter to persist device */
570 if (!skip_filter
&& (tun
->filter_attached
== true)) {
571 err
= sk_attach_filter(&tun
->fprog
, tfile
->socket
.sk
);
575 tfile
->queue_index
= tun
->numqueues
;
576 rcu_assign_pointer(tfile
->tun
, tun
);
577 rcu_assign_pointer(tun
->tfiles
[tun
->numqueues
], tfile
);
581 tun_enable_queue(tfile
);
583 sock_hold(&tfile
->sk
);
585 tun_set_real_num_queues(tun
);
587 /* device is allowed to go away first, so no need to hold extra
595 static struct tun_struct
*__tun_get(struct tun_file
*tfile
)
597 struct tun_struct
*tun
;
600 tun
= rcu_dereference(tfile
->tun
);
608 static struct tun_struct
*tun_get(struct file
*file
)
610 return __tun_get(file
->private_data
);
613 static void tun_put(struct tun_struct
*tun
)
619 static void addr_hash_set(u32
*mask
, const u8
*addr
)
621 int n
= ether_crc(ETH_ALEN
, addr
) >> 26;
622 mask
[n
>> 5] |= (1 << (n
& 31));
625 static unsigned int addr_hash_test(const u32
*mask
, const u8
*addr
)
627 int n
= ether_crc(ETH_ALEN
, addr
) >> 26;
628 return mask
[n
>> 5] & (1 << (n
& 31));
631 static int update_filter(struct tap_filter
*filter
, void __user
*arg
)
633 struct { u8 u
[ETH_ALEN
]; } *addr
;
634 struct tun_filter uf
;
635 int err
, alen
, n
, nexact
;
637 if (copy_from_user(&uf
, arg
, sizeof(uf
)))
646 alen
= ETH_ALEN
* uf
.count
;
647 addr
= kmalloc(alen
, GFP_KERNEL
);
651 if (copy_from_user(addr
, arg
+ sizeof(uf
), alen
)) {
656 /* The filter is updated without holding any locks. Which is
657 * perfectly safe. We disable it first and in the worst
658 * case we'll accept a few undesired packets. */
662 /* Use first set of addresses as an exact filter */
663 for (n
= 0; n
< uf
.count
&& n
< FLT_EXACT_COUNT
; n
++)
664 memcpy(filter
->addr
[n
], addr
[n
].u
, ETH_ALEN
);
668 /* Remaining multicast addresses are hashed,
669 * unicast will leave the filter disabled. */
670 memset(filter
->mask
, 0, sizeof(filter
->mask
));
671 for (; n
< uf
.count
; n
++) {
672 if (!is_multicast_ether_addr(addr
[n
].u
)) {
673 err
= 0; /* no filter */
676 addr_hash_set(filter
->mask
, addr
[n
].u
);
679 /* For ALLMULTI just set the mask to all ones.
680 * This overrides the mask populated above. */
681 if ((uf
.flags
& TUN_FLT_ALLMULTI
))
682 memset(filter
->mask
, ~0, sizeof(filter
->mask
));
684 /* Now enable the filter */
686 filter
->count
= nexact
;
688 /* Return the number of exact filters */
696 /* Returns: 0 - drop, !=0 - accept */
697 static int run_filter(struct tap_filter
*filter
, const struct sk_buff
*skb
)
699 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
701 struct ethhdr
*eh
= (struct ethhdr
*) skb
->data
;
705 for (i
= 0; i
< filter
->count
; i
++)
706 if (ether_addr_equal(eh
->h_dest
, filter
->addr
[i
]))
709 /* Inexact match (multicast only) */
710 if (is_multicast_ether_addr(eh
->h_dest
))
711 return addr_hash_test(filter
->mask
, eh
->h_dest
);
717 * Checks whether the packet is accepted or not.
718 * Returns: 0 - drop, !=0 - accept
720 static int check_filter(struct tap_filter
*filter
, const struct sk_buff
*skb
)
725 return run_filter(filter
, skb
);
728 /* Network device part of the driver */
730 static const struct ethtool_ops tun_ethtool_ops
;
732 /* Net device detach from fd. */
733 static void tun_net_uninit(struct net_device
*dev
)
738 /* Net device open. */
739 static int tun_net_open(struct net_device
*dev
)
741 netif_tx_start_all_queues(dev
);
745 /* Net device close. */
746 static int tun_net_close(struct net_device
*dev
)
748 netif_tx_stop_all_queues(dev
);
752 /* Net device start xmit */
753 static netdev_tx_t
tun_net_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
755 struct tun_struct
*tun
= netdev_priv(dev
);
756 int txq
= skb
->queue_mapping
;
757 struct tun_file
*tfile
;
761 tfile
= rcu_dereference(tun
->tfiles
[txq
]);
762 numqueues
= ACCESS_ONCE(tun
->numqueues
);
764 /* Drop packet if interface is not attached */
765 if (txq
>= numqueues
)
768 if (numqueues
== 1) {
769 /* Select queue was not called for the skbuff, so we extract the
770 * RPS hash and save it into the flow_table here.
774 rxhash
= skb_get_hash(skb
);
776 struct tun_flow_entry
*e
;
777 e
= tun_flow_find(&tun
->flows
[tun_hashfn(rxhash
)],
780 tun_flow_save_rps_rxhash(e
, rxhash
);
784 tun_debug(KERN_INFO
, tun
, "tun_net_xmit %d\n", skb
->len
);
788 /* Drop if the filter does not like it.
789 * This is a noop if the filter is disabled.
790 * Filter can be enabled only for the TAP devices. */
791 if (!check_filter(&tun
->txflt
, skb
))
794 if (tfile
->socket
.sk
->sk_filter
&&
795 sk_filter(tfile
->socket
.sk
, skb
))
798 /* Limit the number of packets queued by dividing txq length with the
801 if (skb_queue_len(&tfile
->socket
.sk
->sk_receive_queue
) * numqueues
802 >= dev
->tx_queue_len
)
805 if (unlikely(skb_orphan_frags(skb
, GFP_ATOMIC
)))
809 sock_tx_timestamp(skb
->sk
, &skb_shinfo(skb
)->tx_flags
);
810 sw_tx_timestamp(skb
);
813 /* Orphan the skb - required as we might hang on to it
814 * for indefinite time.
821 skb_queue_tail(&tfile
->socket
.sk
->sk_receive_queue
, skb
);
823 /* Notify and wake up reader process */
824 if (tfile
->flags
& TUN_FASYNC
)
825 kill_fasync(&tfile
->fasync
, SIGIO
, POLL_IN
);
826 tfile
->socket
.sk
->sk_data_ready(tfile
->socket
.sk
);
832 dev
->stats
.tx_dropped
++;
839 static void tun_net_mclist(struct net_device
*dev
)
842 * This callback is supposed to deal with mc filter in
843 * _rx_ path and has nothing to do with the _tx_ path.
844 * In rx path we always accept everything userspace gives us.
849 #define MAX_MTU 65535
852 tun_net_change_mtu(struct net_device
*dev
, int new_mtu
)
854 if (new_mtu
< MIN_MTU
|| new_mtu
+ dev
->hard_header_len
> MAX_MTU
)
860 static netdev_features_t
tun_net_fix_features(struct net_device
*dev
,
861 netdev_features_t features
)
863 struct tun_struct
*tun
= netdev_priv(dev
);
865 return (features
& tun
->set_features
) | (features
& ~TUN_USER_FEATURES
);
867 #ifdef CONFIG_NET_POLL_CONTROLLER
868 static void tun_poll_controller(struct net_device
*dev
)
871 * Tun only receives frames when:
872 * 1) the char device endpoint gets data from user space
873 * 2) the tun socket gets a sendmsg call from user space
874 * Since both of those are synchronous operations, we are guaranteed
875 * never to have pending data when we poll for it
876 * so there is nothing to do here but return.
877 * We need this though so netpoll recognizes us as an interface that
878 * supports polling, which enables bridge devices in virt setups to
879 * still use netconsole
884 static const struct net_device_ops tun_netdev_ops
= {
885 .ndo_uninit
= tun_net_uninit
,
886 .ndo_open
= tun_net_open
,
887 .ndo_stop
= tun_net_close
,
888 .ndo_start_xmit
= tun_net_xmit
,
889 .ndo_change_mtu
= tun_net_change_mtu
,
890 .ndo_fix_features
= tun_net_fix_features
,
891 .ndo_select_queue
= tun_select_queue
,
892 #ifdef CONFIG_NET_POLL_CONTROLLER
893 .ndo_poll_controller
= tun_poll_controller
,
897 static const struct net_device_ops tap_netdev_ops
= {
898 .ndo_uninit
= tun_net_uninit
,
899 .ndo_open
= tun_net_open
,
900 .ndo_stop
= tun_net_close
,
901 .ndo_start_xmit
= tun_net_xmit
,
902 .ndo_change_mtu
= tun_net_change_mtu
,
903 .ndo_fix_features
= tun_net_fix_features
,
904 .ndo_set_rx_mode
= tun_net_mclist
,
905 .ndo_set_mac_address
= eth_mac_addr
,
906 .ndo_validate_addr
= eth_validate_addr
,
907 .ndo_select_queue
= tun_select_queue
,
908 #ifdef CONFIG_NET_POLL_CONTROLLER
909 .ndo_poll_controller
= tun_poll_controller
,
913 static void tun_flow_init(struct tun_struct
*tun
)
917 for (i
= 0; i
< TUN_NUM_FLOW_ENTRIES
; i
++)
918 INIT_HLIST_HEAD(&tun
->flows
[i
]);
920 tun
->ageing_time
= TUN_FLOW_EXPIRE
;
921 setup_timer(&tun
->flow_gc_timer
, tun_flow_cleanup
, (unsigned long)tun
);
922 mod_timer(&tun
->flow_gc_timer
,
923 round_jiffies_up(jiffies
+ tun
->ageing_time
));
926 static void tun_flow_uninit(struct tun_struct
*tun
)
928 del_timer_sync(&tun
->flow_gc_timer
);
932 /* Initialize net device. */
933 static void tun_net_init(struct net_device
*dev
)
935 struct tun_struct
*tun
= netdev_priv(dev
);
937 switch (tun
->flags
& TUN_TYPE_MASK
) {
939 dev
->netdev_ops
= &tun_netdev_ops
;
941 /* Point-to-Point TUN Device */
942 dev
->hard_header_len
= 0;
946 /* Zero header length */
947 dev
->type
= ARPHRD_NONE
;
948 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
949 dev
->tx_queue_len
= TUN_READQ_SIZE
; /* We prefer our own queue length */
953 dev
->netdev_ops
= &tap_netdev_ops
;
954 /* Ethernet TAP Device */
956 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
957 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
959 eth_hw_addr_random(dev
);
961 dev
->tx_queue_len
= TUN_READQ_SIZE
; /* We prefer our own queue length */
966 /* Character device part */
969 static unsigned int tun_chr_poll(struct file
*file
, poll_table
*wait
)
971 struct tun_file
*tfile
= file
->private_data
;
972 struct tun_struct
*tun
= __tun_get(tfile
);
974 unsigned int mask
= 0;
979 sk
= tfile
->socket
.sk
;
981 tun_debug(KERN_INFO
, tun
, "tun_chr_poll\n");
983 poll_wait(file
, sk_sleep(sk
), wait
);
985 if (!skb_queue_empty(&sk
->sk_receive_queue
))
986 mask
|= POLLIN
| POLLRDNORM
;
988 if (sock_writeable(sk
) ||
989 (!test_and_set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
) &&
991 mask
|= POLLOUT
| POLLWRNORM
;
993 if (tun
->dev
->reg_state
!= NETREG_REGISTERED
)
1000 /* prepad is the amount to reserve at front. len is length after that.
1001 * linear is a hint as to how much to copy (usually headers). */
1002 static struct sk_buff
*tun_alloc_skb(struct tun_file
*tfile
,
1003 size_t prepad
, size_t len
,
1004 size_t linear
, int noblock
)
1006 struct sock
*sk
= tfile
->socket
.sk
;
1007 struct sk_buff
*skb
;
1010 /* Under a page? Don't bother with paged skb. */
1011 if (prepad
+ len
< PAGE_SIZE
|| !linear
)
1014 skb
= sock_alloc_send_pskb(sk
, prepad
+ linear
, len
- linear
, noblock
,
1017 return ERR_PTR(err
);
1019 skb_reserve(skb
, prepad
);
1020 skb_put(skb
, linear
);
1021 skb
->data_len
= len
- linear
;
1022 skb
->len
+= len
- linear
;
1027 /* Get packet from user space buffer */
1028 static ssize_t
tun_get_user(struct tun_struct
*tun
, struct tun_file
*tfile
,
1029 void *msg_control
, const struct iovec
*iv
,
1030 size_t total_len
, size_t count
, int noblock
)
1032 struct tun_pi pi
= { 0, cpu_to_be16(ETH_P_IP
) };
1033 struct sk_buff
*skb
;
1034 size_t len
= total_len
, align
= NET_SKB_PAD
, linear
;
1035 struct virtio_net_hdr gso
= { 0 };
1039 bool zerocopy
= false;
1043 if (!(tun
->flags
& TUN_NO_PI
)) {
1044 if (len
< sizeof(pi
))
1048 if (memcpy_fromiovecend((void *)&pi
, iv
, 0, sizeof(pi
)))
1050 offset
+= sizeof(pi
);
1053 if (tun
->flags
& TUN_VNET_HDR
) {
1054 if (len
< tun
->vnet_hdr_sz
)
1056 len
-= tun
->vnet_hdr_sz
;
1058 if (memcpy_fromiovecend((void *)&gso
, iv
, offset
, sizeof(gso
)))
1061 if ((gso
.flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) &&
1062 gso
.csum_start
+ gso
.csum_offset
+ 2 > gso
.hdr_len
)
1063 gso
.hdr_len
= gso
.csum_start
+ gso
.csum_offset
+ 2;
1065 if (gso
.hdr_len
> len
)
1067 offset
+= tun
->vnet_hdr_sz
;
1070 if ((tun
->flags
& TUN_TYPE_MASK
) == TUN_TAP_DEV
) {
1071 align
+= NET_IP_ALIGN
;
1072 if (unlikely(len
< ETH_HLEN
||
1073 (gso
.hdr_len
&& gso
.hdr_len
< ETH_HLEN
)))
1077 good_linear
= SKB_MAX_HEAD(align
);
1080 /* There are 256 bytes to be copied in skb, so there is
1081 * enough room for skb expand head in case it is used.
1082 * The rest of the buffer is mapped from userspace.
1084 copylen
= gso
.hdr_len
? gso
.hdr_len
: GOODCOPY_LEN
;
1085 if (copylen
> good_linear
)
1086 copylen
= good_linear
;
1088 if (iov_pages(iv
, offset
+ copylen
, count
) <= MAX_SKB_FRAGS
)
1094 if (gso
.hdr_len
> good_linear
)
1095 linear
= good_linear
;
1097 linear
= gso
.hdr_len
;
1100 skb
= tun_alloc_skb(tfile
, align
, copylen
, linear
, noblock
);
1102 if (PTR_ERR(skb
) != -EAGAIN
)
1103 tun
->dev
->stats
.rx_dropped
++;
1104 return PTR_ERR(skb
);
1108 err
= zerocopy_sg_from_iovec(skb
, iv
, offset
, count
);
1110 err
= skb_copy_datagram_from_iovec(skb
, 0, iv
, offset
, len
);
1111 if (!err
&& msg_control
) {
1112 struct ubuf_info
*uarg
= msg_control
;
1113 uarg
->callback(uarg
, false);
1118 tun
->dev
->stats
.rx_dropped
++;
1123 if (gso
.flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) {
1124 if (!skb_partial_csum_set(skb
, gso
.csum_start
,
1126 tun
->dev
->stats
.rx_frame_errors
++;
1132 switch (tun
->flags
& TUN_TYPE_MASK
) {
1134 if (tun
->flags
& TUN_NO_PI
) {
1135 switch (skb
->data
[0] & 0xf0) {
1137 pi
.proto
= htons(ETH_P_IP
);
1140 pi
.proto
= htons(ETH_P_IPV6
);
1143 tun
->dev
->stats
.rx_dropped
++;
1149 skb_reset_mac_header(skb
);
1150 skb
->protocol
= pi
.proto
;
1151 skb
->dev
= tun
->dev
;
1154 skb
->protocol
= eth_type_trans(skb
, tun
->dev
);
1158 skb_reset_network_header(skb
);
1160 if (gso
.gso_type
!= VIRTIO_NET_HDR_GSO_NONE
) {
1162 switch (gso
.gso_type
& ~VIRTIO_NET_HDR_GSO_ECN
) {
1163 case VIRTIO_NET_HDR_GSO_TCPV4
:
1164 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV4
;
1166 case VIRTIO_NET_HDR_GSO_TCPV6
:
1167 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV6
;
1169 case VIRTIO_NET_HDR_GSO_UDP
:
1175 netdev_warn(tun
->dev
,
1176 "%s: using disabled UFO feature; please fix this program\n",
1179 skb_shinfo(skb
)->gso_type
= SKB_GSO_UDP
;
1180 if (skb
->protocol
== htons(ETH_P_IPV6
))
1181 ipv6_proxy_select_ident(skb
);
1185 tun
->dev
->stats
.rx_frame_errors
++;
1190 if (gso
.gso_type
& VIRTIO_NET_HDR_GSO_ECN
)
1191 skb_shinfo(skb
)->gso_type
|= SKB_GSO_TCP_ECN
;
1193 skb_shinfo(skb
)->gso_size
= gso
.gso_size
;
1194 if (skb_shinfo(skb
)->gso_size
== 0) {
1195 tun
->dev
->stats
.rx_frame_errors
++;
1200 /* Header must be checked, and gso_segs computed. */
1201 skb_shinfo(skb
)->gso_type
|= SKB_GSO_DODGY
;
1202 skb_shinfo(skb
)->gso_segs
= 0;
1205 /* copy skb_ubuf_info for callback when skb has no error */
1207 skb_shinfo(skb
)->destructor_arg
= msg_control
;
1208 skb_shinfo(skb
)->tx_flags
|= SKBTX_DEV_ZEROCOPY
;
1209 skb_shinfo(skb
)->tx_flags
|= SKBTX_SHARED_FRAG
;
1212 skb_probe_transport_header(skb
, 0);
1214 rxhash
= skb_get_hash(skb
);
1217 tun
->dev
->stats
.rx_packets
++;
1218 tun
->dev
->stats
.rx_bytes
+= len
;
1220 tun_flow_update(tun
, rxhash
, tfile
);
1224 static ssize_t
tun_chr_aio_write(struct kiocb
*iocb
, const struct iovec
*iv
,
1225 unsigned long count
, loff_t pos
)
1227 struct file
*file
= iocb
->ki_filp
;
1228 struct tun_struct
*tun
= tun_get(file
);
1229 struct tun_file
*tfile
= file
->private_data
;
1235 tun_debug(KERN_INFO
, tun
, "tun_chr_write %ld\n", count
);
1237 result
= tun_get_user(tun
, tfile
, NULL
, iv
, iov_length(iv
, count
),
1238 count
, file
->f_flags
& O_NONBLOCK
);
1244 /* Put packet to the user space buffer */
1245 static ssize_t
tun_put_user(struct tun_struct
*tun
,
1246 struct tun_file
*tfile
,
1247 struct sk_buff
*skb
,
1248 const struct iovec
*iv
, int len
)
1250 struct tun_pi pi
= { 0, skb
->protocol
};
1252 int vlan_offset
= 0, copied
;
1254 int vnet_hdr_sz
= 0;
1256 if (vlan_tx_tag_present(skb
))
1257 vlan_hlen
= VLAN_HLEN
;
1259 if (tun
->flags
& TUN_VNET_HDR
)
1260 vnet_hdr_sz
= tun
->vnet_hdr_sz
;
1262 if (!(tun
->flags
& TUN_NO_PI
)) {
1263 if ((len
-= sizeof(pi
)) < 0)
1266 if (len
< skb
->len
+ vlan_hlen
+ vnet_hdr_sz
) {
1267 /* Packet will be striped */
1268 pi
.flags
|= TUN_PKT_STRIP
;
1271 if (memcpy_toiovecend(iv
, (void *) &pi
, 0, sizeof(pi
)))
1273 total
+= sizeof(pi
);
1277 struct virtio_net_hdr gso
= { 0 }; /* no info leak */
1278 if ((len
-= vnet_hdr_sz
) < 0)
1281 if (skb_is_gso(skb
)) {
1282 struct skb_shared_info
*sinfo
= skb_shinfo(skb
);
1284 /* This is a hint as to how much should be linear. */
1285 gso
.hdr_len
= skb_headlen(skb
);
1286 gso
.gso_size
= sinfo
->gso_size
;
1287 if (sinfo
->gso_type
& SKB_GSO_TCPV4
)
1288 gso
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
1289 else if (sinfo
->gso_type
& SKB_GSO_TCPV6
)
1290 gso
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
1292 pr_err("unexpected GSO type: "
1293 "0x%x, gso_size %d, hdr_len %d\n",
1294 sinfo
->gso_type
, gso
.gso_size
,
1296 print_hex_dump(KERN_ERR
, "tun: ",
1299 min((int)gso
.hdr_len
, 64), true);
1303 if (sinfo
->gso_type
& SKB_GSO_TCP_ECN
)
1304 gso
.gso_type
|= VIRTIO_NET_HDR_GSO_ECN
;
1306 gso
.gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
1308 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1309 gso
.flags
= VIRTIO_NET_HDR_F_NEEDS_CSUM
;
1310 gso
.csum_start
= skb_checksum_start_offset(skb
) +
1312 gso
.csum_offset
= skb
->csum_offset
;
1313 } else if (skb
->ip_summed
== CHECKSUM_UNNECESSARY
) {
1314 gso
.flags
= VIRTIO_NET_HDR_F_DATA_VALID
;
1315 } /* else everything is zero */
1317 if (unlikely(memcpy_toiovecend(iv
, (void *)&gso
, total
,
1320 total
+= vnet_hdr_sz
;
1324 len
= min_t(int, skb
->len
+ vlan_hlen
, len
);
1325 total
+= skb
->len
+ vlan_hlen
;
1329 __be16 h_vlan_proto
;
1333 veth
.h_vlan_proto
= skb
->vlan_proto
;
1334 veth
.h_vlan_TCI
= htons(vlan_tx_tag_get(skb
));
1336 vlan_offset
= offsetof(struct vlan_ethhdr
, h_vlan_proto
);
1338 copy
= min_t(int, vlan_offset
, len
);
1339 ret
= skb_copy_datagram_const_iovec(skb
, 0, iv
, copied
, copy
);
1345 copy
= min_t(int, sizeof(veth
), len
);
1346 ret
= memcpy_toiovecend(iv
, (void *)&veth
, copied
, copy
);
1353 skb_copy_datagram_const_iovec(skb
, vlan_offset
, iv
, copied
, len
);
1356 tun
->dev
->stats
.tx_packets
++;
1357 tun
->dev
->stats
.tx_bytes
+= len
;
1362 static ssize_t
tun_do_read(struct tun_struct
*tun
, struct tun_file
*tfile
,
1363 const struct iovec
*iv
, ssize_t len
, int noblock
)
1365 struct sk_buff
*skb
;
1367 int peeked
, err
, off
= 0;
1369 tun_debug(KERN_INFO
, tun
, "tun_do_read\n");
1374 if (tun
->dev
->reg_state
!= NETREG_REGISTERED
)
1377 /* Read frames from queue */
1378 skb
= __skb_recv_datagram(tfile
->socket
.sk
, noblock
? MSG_DONTWAIT
: 0,
1379 &peeked
, &off
, &err
);
1381 ret
= tun_put_user(tun
, tfile
, skb
, iv
, len
);
1389 static ssize_t
tun_chr_aio_read(struct kiocb
*iocb
, const struct iovec
*iv
,
1390 unsigned long count
, loff_t pos
)
1392 struct file
*file
= iocb
->ki_filp
;
1393 struct tun_file
*tfile
= file
->private_data
;
1394 struct tun_struct
*tun
= __tun_get(tfile
);
1399 len
= iov_length(iv
, count
);
1405 ret
= tun_do_read(tun
, tfile
, iv
, len
,
1406 file
->f_flags
& O_NONBLOCK
);
1407 ret
= min_t(ssize_t
, ret
, len
);
1415 static void tun_free_netdev(struct net_device
*dev
)
1417 struct tun_struct
*tun
= netdev_priv(dev
);
1419 BUG_ON(!(list_empty(&tun
->disabled
)));
1420 tun_flow_uninit(tun
);
1421 security_tun_dev_free_security(tun
->security
);
1425 static void tun_setup(struct net_device
*dev
)
1427 struct tun_struct
*tun
= netdev_priv(dev
);
1429 tun
->owner
= INVALID_UID
;
1430 tun
->group
= INVALID_GID
;
1432 dev
->ethtool_ops
= &tun_ethtool_ops
;
1433 dev
->destructor
= tun_free_netdev
;
1436 /* Trivial set of netlink ops to allow deleting tun or tap
1437 * device with netlink.
1439 static int tun_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1444 static struct rtnl_link_ops tun_link_ops __read_mostly
= {
1446 .priv_size
= sizeof(struct tun_struct
),
1448 .validate
= tun_validate
,
1451 static void tun_sock_write_space(struct sock
*sk
)
1453 struct tun_file
*tfile
;
1454 wait_queue_head_t
*wqueue
;
1456 if (!sock_writeable(sk
))
1459 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
))
1462 wqueue
= sk_sleep(sk
);
1463 if (wqueue
&& waitqueue_active(wqueue
))
1464 wake_up_interruptible_sync_poll(wqueue
, POLLOUT
|
1465 POLLWRNORM
| POLLWRBAND
);
1467 tfile
= container_of(sk
, struct tun_file
, sk
);
1468 kill_fasync(&tfile
->fasync
, SIGIO
, POLL_OUT
);
1471 static int tun_sendmsg(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
);
1480 ret
= tun_get_user(tun
, tfile
, m
->msg_control
, m
->msg_iov
, total_len
,
1481 m
->msg_iovlen
, m
->msg_flags
& MSG_DONTWAIT
);
1486 static int tun_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1487 struct msghdr
*m
, size_t total_len
,
1490 struct tun_file
*tfile
= container_of(sock
, struct tun_file
, socket
);
1491 struct tun_struct
*tun
= __tun_get(tfile
);
1497 if (flags
& ~(MSG_DONTWAIT
|MSG_TRUNC
|MSG_ERRQUEUE
)) {
1501 if (flags
& MSG_ERRQUEUE
) {
1502 ret
= sock_recv_errqueue(sock
->sk
, m
, total_len
,
1503 SOL_PACKET
, TUN_TX_TIMESTAMP
);
1506 ret
= tun_do_read(tun
, tfile
, m
->msg_iov
, total_len
,
1507 flags
& MSG_DONTWAIT
);
1508 if (ret
> total_len
) {
1509 m
->msg_flags
|= MSG_TRUNC
;
1510 ret
= flags
& MSG_TRUNC
? ret
: total_len
;
1517 static int tun_release(struct socket
*sock
)
1524 /* Ops structure to mimic raw sockets with tun */
1525 static const struct proto_ops tun_socket_ops
= {
1526 .sendmsg
= tun_sendmsg
,
1527 .recvmsg
= tun_recvmsg
,
1528 .release
= tun_release
,
1531 static struct proto tun_proto
= {
1533 .owner
= THIS_MODULE
,
1534 .obj_size
= sizeof(struct tun_file
),
1537 static int tun_flags(struct tun_struct
*tun
)
1539 return tun
->flags
& (TUN_FEATURES
| IFF_PERSIST
| IFF_TUN
| IFF_TAP
);
1542 static ssize_t
tun_show_flags(struct device
*dev
, struct device_attribute
*attr
,
1545 struct tun_struct
*tun
= netdev_priv(to_net_dev(dev
));
1546 return sprintf(buf
, "0x%x\n", tun_flags(tun
));
1549 static ssize_t
tun_show_owner(struct device
*dev
, struct device_attribute
*attr
,
1552 struct tun_struct
*tun
= netdev_priv(to_net_dev(dev
));
1553 return uid_valid(tun
->owner
)?
1554 sprintf(buf
, "%u\n",
1555 from_kuid_munged(current_user_ns(), tun
->owner
)):
1556 sprintf(buf
, "-1\n");
1559 static ssize_t
tun_show_group(struct device
*dev
, struct device_attribute
*attr
,
1562 struct tun_struct
*tun
= netdev_priv(to_net_dev(dev
));
1563 return gid_valid(tun
->group
) ?
1564 sprintf(buf
, "%u\n",
1565 from_kgid_munged(current_user_ns(), tun
->group
)):
1566 sprintf(buf
, "-1\n");
1569 static DEVICE_ATTR(tun_flags
, 0444, tun_show_flags
, NULL
);
1570 static DEVICE_ATTR(owner
, 0444, tun_show_owner
, NULL
);
1571 static DEVICE_ATTR(group
, 0444, tun_show_group
, NULL
);
1573 static int tun_set_iff(struct net
*net
, struct file
*file
, struct ifreq
*ifr
)
1575 struct tun_struct
*tun
;
1576 struct tun_file
*tfile
= file
->private_data
;
1577 struct net_device
*dev
;
1580 if (tfile
->detached
)
1583 dev
= __dev_get_by_name(net
, ifr
->ifr_name
);
1585 if (ifr
->ifr_flags
& IFF_TUN_EXCL
)
1587 if ((ifr
->ifr_flags
& IFF_TUN
) && dev
->netdev_ops
== &tun_netdev_ops
)
1588 tun
= netdev_priv(dev
);
1589 else if ((ifr
->ifr_flags
& IFF_TAP
) && dev
->netdev_ops
== &tap_netdev_ops
)
1590 tun
= netdev_priv(dev
);
1594 if (!!(ifr
->ifr_flags
& IFF_MULTI_QUEUE
) !=
1595 !!(tun
->flags
& TUN_TAP_MQ
))
1598 if (tun_not_capable(tun
))
1600 err
= security_tun_dev_open(tun
->security
);
1604 err
= tun_attach(tun
, file
, ifr
->ifr_flags
& IFF_NOFILTER
);
1608 if (tun
->flags
& TUN_TAP_MQ
&&
1609 (tun
->numqueues
+ tun
->numdisabled
> 1)) {
1610 /* One or more queue has already been attached, no need
1611 * to initialize the device again.
1618 unsigned long flags
= 0;
1619 int queues
= ifr
->ifr_flags
& IFF_MULTI_QUEUE
?
1622 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1624 err
= security_tun_dev_create();
1629 if (ifr
->ifr_flags
& IFF_TUN
) {
1631 flags
|= TUN_TUN_DEV
;
1633 } else if (ifr
->ifr_flags
& IFF_TAP
) {
1635 flags
|= TUN_TAP_DEV
;
1641 name
= ifr
->ifr_name
;
1643 dev
= alloc_netdev_mqs(sizeof(struct tun_struct
), name
,
1644 NET_NAME_UNKNOWN
, tun_setup
, queues
,
1650 dev_net_set(dev
, net
);
1651 dev
->rtnl_link_ops
= &tun_link_ops
;
1652 dev
->ifindex
= tfile
->ifindex
;
1654 tun
= netdev_priv(dev
);
1657 tun
->txflt
.count
= 0;
1658 tun
->vnet_hdr_sz
= sizeof(struct virtio_net_hdr
);
1660 tun
->filter_attached
= false;
1661 tun
->sndbuf
= tfile
->socket
.sk
->sk_sndbuf
;
1663 spin_lock_init(&tun
->lock
);
1665 err
= security_tun_dev_alloc_security(&tun
->security
);
1672 dev
->hw_features
= NETIF_F_SG
| NETIF_F_FRAGLIST
|
1673 TUN_USER_FEATURES
| NETIF_F_HW_VLAN_CTAG_TX
|
1674 NETIF_F_HW_VLAN_STAG_TX
;
1675 dev
->features
= dev
->hw_features
;
1676 dev
->vlan_features
= dev
->features
&
1677 ~(NETIF_F_HW_VLAN_CTAG_TX
|
1678 NETIF_F_HW_VLAN_STAG_TX
);
1680 INIT_LIST_HEAD(&tun
->disabled
);
1681 err
= tun_attach(tun
, file
, false);
1685 err
= register_netdevice(tun
->dev
);
1689 if (device_create_file(&tun
->dev
->dev
, &dev_attr_tun_flags
) ||
1690 device_create_file(&tun
->dev
->dev
, &dev_attr_owner
) ||
1691 device_create_file(&tun
->dev
->dev
, &dev_attr_group
))
1692 pr_err("Failed to create tun sysfs files\n");
1695 netif_carrier_on(tun
->dev
);
1697 tun_debug(KERN_INFO
, tun
, "tun_set_iff\n");
1699 tun
->flags
= (tun
->flags
& ~TUN_FEATURES
) |
1700 (ifr
->ifr_flags
& TUN_FEATURES
);
1702 /* Make sure persistent devices do not get stuck in
1705 if (netif_running(tun
->dev
))
1706 netif_tx_wake_all_queues(tun
->dev
);
1708 strcpy(ifr
->ifr_name
, tun
->dev
->name
);
1712 tun_detach_all(dev
);
1714 tun_flow_uninit(tun
);
1715 security_tun_dev_free_security(tun
->security
);
1721 static void tun_get_iff(struct net
*net
, struct tun_struct
*tun
,
1724 tun_debug(KERN_INFO
, tun
, "tun_get_iff\n");
1726 strcpy(ifr
->ifr_name
, tun
->dev
->name
);
1728 ifr
->ifr_flags
= tun_flags(tun
);
1732 /* This is like a cut-down ethtool ops, except done via tun fd so no
1733 * privs required. */
1734 static int set_offload(struct tun_struct
*tun
, unsigned long arg
)
1736 netdev_features_t features
= 0;
1738 if (arg
& TUN_F_CSUM
) {
1739 features
|= NETIF_F_HW_CSUM
;
1742 if (arg
& (TUN_F_TSO4
|TUN_F_TSO6
)) {
1743 if (arg
& TUN_F_TSO_ECN
) {
1744 features
|= NETIF_F_TSO_ECN
;
1745 arg
&= ~TUN_F_TSO_ECN
;
1747 if (arg
& TUN_F_TSO4
)
1748 features
|= NETIF_F_TSO
;
1749 if (arg
& TUN_F_TSO6
)
1750 features
|= NETIF_F_TSO6
;
1751 arg
&= ~(TUN_F_TSO4
|TUN_F_TSO6
);
1755 /* This gives the user a way to test for new features in future by
1756 * trying to set them. */
1760 tun
->set_features
= features
;
1761 netdev_update_features(tun
->dev
);
1766 static void tun_detach_filter(struct tun_struct
*tun
, int n
)
1769 struct tun_file
*tfile
;
1771 for (i
= 0; i
< n
; i
++) {
1772 tfile
= rtnl_dereference(tun
->tfiles
[i
]);
1773 sk_detach_filter(tfile
->socket
.sk
);
1776 tun
->filter_attached
= false;
1779 static int tun_attach_filter(struct tun_struct
*tun
)
1782 struct tun_file
*tfile
;
1784 for (i
= 0; i
< tun
->numqueues
; i
++) {
1785 tfile
= rtnl_dereference(tun
->tfiles
[i
]);
1786 ret
= sk_attach_filter(&tun
->fprog
, tfile
->socket
.sk
);
1788 tun_detach_filter(tun
, i
);
1793 tun
->filter_attached
= true;
1797 static void tun_set_sndbuf(struct tun_struct
*tun
)
1799 struct tun_file
*tfile
;
1802 for (i
= 0; i
< tun
->numqueues
; i
++) {
1803 tfile
= rtnl_dereference(tun
->tfiles
[i
]);
1804 tfile
->socket
.sk
->sk_sndbuf
= tun
->sndbuf
;
1808 static int tun_set_queue(struct file
*file
, struct ifreq
*ifr
)
1810 struct tun_file
*tfile
= file
->private_data
;
1811 struct tun_struct
*tun
;
1816 if (ifr
->ifr_flags
& IFF_ATTACH_QUEUE
) {
1817 tun
= tfile
->detached
;
1822 ret
= security_tun_dev_attach_queue(tun
->security
);
1825 ret
= tun_attach(tun
, file
, false);
1826 } else if (ifr
->ifr_flags
& IFF_DETACH_QUEUE
) {
1827 tun
= rtnl_dereference(tfile
->tun
);
1828 if (!tun
|| !(tun
->flags
& TUN_TAP_MQ
) || tfile
->detached
)
1831 __tun_detach(tfile
, false);
1840 static long __tun_chr_ioctl(struct file
*file
, unsigned int cmd
,
1841 unsigned long arg
, int ifreq_len
)
1843 struct tun_file
*tfile
= file
->private_data
;
1844 struct tun_struct
*tun
;
1845 void __user
* argp
= (void __user
*)arg
;
1851 unsigned int ifindex
;
1854 if (cmd
== TUNSETIFF
|| cmd
== TUNSETQUEUE
|| _IOC_TYPE(cmd
) == 0x89) {
1855 if (copy_from_user(&ifr
, argp
, ifreq_len
))
1858 memset(&ifr
, 0, sizeof(ifr
));
1860 if (cmd
== TUNGETFEATURES
) {
1861 /* Currently this just means: "what IFF flags are valid?".
1862 * This is needed because we never checked for invalid flags on
1865 return put_user(IFF_TUN
| IFF_TAP
| TUN_FEATURES
,
1866 (unsigned int __user
*)argp
);
1867 } else if (cmd
== TUNSETQUEUE
)
1868 return tun_set_queue(file
, &ifr
);
1873 tun
= __tun_get(tfile
);
1874 if (cmd
== TUNSETIFF
&& !tun
) {
1875 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1877 ret
= tun_set_iff(tfile
->net
, file
, &ifr
);
1882 if (copy_to_user(argp
, &ifr
, ifreq_len
))
1886 if (cmd
== TUNSETIFINDEX
) {
1892 if (copy_from_user(&ifindex
, argp
, sizeof(ifindex
)))
1896 tfile
->ifindex
= ifindex
;
1904 tun_debug(KERN_INFO
, tun
, "tun_chr_ioctl cmd %u\n", cmd
);
1909 tun_get_iff(current
->nsproxy
->net_ns
, tun
, &ifr
);
1911 if (tfile
->detached
)
1912 ifr
.ifr_flags
|= IFF_DETACH_QUEUE
;
1913 if (!tfile
->socket
.sk
->sk_filter
)
1914 ifr
.ifr_flags
|= IFF_NOFILTER
;
1916 if (copy_to_user(argp
, &ifr
, ifreq_len
))
1921 /* Disable/Enable checksum */
1923 /* [unimplemented] */
1924 tun_debug(KERN_INFO
, tun
, "ignored: set checksum %s\n",
1925 arg
? "disabled" : "enabled");
1929 /* Disable/Enable persist mode. Keep an extra reference to the
1930 * module to prevent the module being unprobed.
1932 if (arg
&& !(tun
->flags
& TUN_PERSIST
)) {
1933 tun
->flags
|= TUN_PERSIST
;
1934 __module_get(THIS_MODULE
);
1936 if (!arg
&& (tun
->flags
& TUN_PERSIST
)) {
1937 tun
->flags
&= ~TUN_PERSIST
;
1938 module_put(THIS_MODULE
);
1941 tun_debug(KERN_INFO
, tun
, "persist %s\n",
1942 arg
? "enabled" : "disabled");
1946 /* Set owner of the device */
1947 owner
= make_kuid(current_user_ns(), arg
);
1948 if (!uid_valid(owner
)) {
1953 tun_debug(KERN_INFO
, tun
, "owner set to %u\n",
1954 from_kuid(&init_user_ns
, tun
->owner
));
1958 /* Set group of the device */
1959 group
= make_kgid(current_user_ns(), arg
);
1960 if (!gid_valid(group
)) {
1965 tun_debug(KERN_INFO
, tun
, "group set to %u\n",
1966 from_kgid(&init_user_ns
, tun
->group
));
1970 /* Only allow setting the type when the interface is down */
1971 if (tun
->dev
->flags
& IFF_UP
) {
1972 tun_debug(KERN_INFO
, tun
,
1973 "Linktype set failed because interface is up\n");
1976 tun
->dev
->type
= (int) arg
;
1977 tun_debug(KERN_INFO
, tun
, "linktype set to %d\n",
1989 ret
= set_offload(tun
, arg
);
1992 case TUNSETTXFILTER
:
1993 /* Can be set only for TAPs */
1995 if ((tun
->flags
& TUN_TYPE_MASK
) != TUN_TAP_DEV
)
1997 ret
= update_filter(&tun
->txflt
, (void __user
*)arg
);
2001 /* Get hw address */
2002 memcpy(ifr
.ifr_hwaddr
.sa_data
, tun
->dev
->dev_addr
, ETH_ALEN
);
2003 ifr
.ifr_hwaddr
.sa_family
= tun
->dev
->type
;
2004 if (copy_to_user(argp
, &ifr
, ifreq_len
))
2009 /* Set hw address */
2010 tun_debug(KERN_DEBUG
, tun
, "set hw address: %pM\n",
2011 ifr
.ifr_hwaddr
.sa_data
);
2013 ret
= dev_set_mac_address(tun
->dev
, &ifr
.ifr_hwaddr
);
2017 sndbuf
= tfile
->socket
.sk
->sk_sndbuf
;
2018 if (copy_to_user(argp
, &sndbuf
, sizeof(sndbuf
)))
2023 if (copy_from_user(&sndbuf
, argp
, sizeof(sndbuf
))) {
2028 tun
->sndbuf
= sndbuf
;
2029 tun_set_sndbuf(tun
);
2032 case TUNGETVNETHDRSZ
:
2033 vnet_hdr_sz
= tun
->vnet_hdr_sz
;
2034 if (copy_to_user(argp
, &vnet_hdr_sz
, sizeof(vnet_hdr_sz
)))
2038 case TUNSETVNETHDRSZ
:
2039 if (copy_from_user(&vnet_hdr_sz
, argp
, sizeof(vnet_hdr_sz
))) {
2043 if (vnet_hdr_sz
< (int)sizeof(struct virtio_net_hdr
)) {
2048 tun
->vnet_hdr_sz
= vnet_hdr_sz
;
2051 case TUNATTACHFILTER
:
2052 /* Can be set only for TAPs */
2054 if ((tun
->flags
& TUN_TYPE_MASK
) != TUN_TAP_DEV
)
2057 if (copy_from_user(&tun
->fprog
, argp
, sizeof(tun
->fprog
)))
2060 ret
= tun_attach_filter(tun
);
2063 case TUNDETACHFILTER
:
2064 /* Can be set only for TAPs */
2066 if ((tun
->flags
& TUN_TYPE_MASK
) != TUN_TAP_DEV
)
2069 tun_detach_filter(tun
, tun
->numqueues
);
2074 if ((tun
->flags
& TUN_TYPE_MASK
) != TUN_TAP_DEV
)
2077 if (copy_to_user(argp
, &tun
->fprog
, sizeof(tun
->fprog
)))
2094 static long tun_chr_ioctl(struct file
*file
,
2095 unsigned int cmd
, unsigned long arg
)
2097 return __tun_chr_ioctl(file
, cmd
, arg
, sizeof (struct ifreq
));
2100 #ifdef CONFIG_COMPAT
2101 static long tun_chr_compat_ioctl(struct file
*file
,
2102 unsigned int cmd
, unsigned long arg
)
2107 case TUNSETTXFILTER
:
2112 arg
= (unsigned long)compat_ptr(arg
);
2115 arg
= (compat_ulong_t
)arg
;
2120 * compat_ifreq is shorter than ifreq, so we must not access beyond
2121 * the end of that structure. All fields that are used in this
2122 * driver are compatible though, we don't need to convert the
2125 return __tun_chr_ioctl(file
, cmd
, arg
, sizeof(struct compat_ifreq
));
2127 #endif /* CONFIG_COMPAT */
2129 static int tun_chr_fasync(int fd
, struct file
*file
, int on
)
2131 struct tun_file
*tfile
= file
->private_data
;
2134 if ((ret
= fasync_helper(fd
, file
, on
, &tfile
->fasync
)) < 0)
2138 __f_setown(file
, task_pid(current
), PIDTYPE_PID
, 0);
2139 tfile
->flags
|= TUN_FASYNC
;
2141 tfile
->flags
&= ~TUN_FASYNC
;
2147 static int tun_chr_open(struct inode
*inode
, struct file
* file
)
2149 struct tun_file
*tfile
;
2151 DBG1(KERN_INFO
, "tunX: tun_chr_open\n");
2153 tfile
= (struct tun_file
*)sk_alloc(&init_net
, AF_UNSPEC
, GFP_KERNEL
,
2157 RCU_INIT_POINTER(tfile
->tun
, NULL
);
2158 tfile
->net
= get_net(current
->nsproxy
->net_ns
);
2162 init_waitqueue_head(&tfile
->wq
.wait
);
2163 RCU_INIT_POINTER(tfile
->socket
.wq
, &tfile
->wq
);
2165 tfile
->socket
.file
= file
;
2166 tfile
->socket
.ops
= &tun_socket_ops
;
2168 sock_init_data(&tfile
->socket
, &tfile
->sk
);
2169 sk_change_net(&tfile
->sk
, tfile
->net
);
2171 tfile
->sk
.sk_write_space
= tun_sock_write_space
;
2172 tfile
->sk
.sk_sndbuf
= INT_MAX
;
2174 file
->private_data
= tfile
;
2175 set_bit(SOCK_EXTERNALLY_ALLOCATED
, &tfile
->socket
.flags
);
2176 INIT_LIST_HEAD(&tfile
->next
);
2178 sock_set_flag(&tfile
->sk
, SOCK_ZEROCOPY
);
2183 static int tun_chr_close(struct inode
*inode
, struct file
*file
)
2185 struct tun_file
*tfile
= file
->private_data
;
2186 struct net
*net
= tfile
->net
;
2188 tun_detach(tfile
, true);
2194 #ifdef CONFIG_PROC_FS
2195 static int tun_chr_show_fdinfo(struct seq_file
*m
, struct file
*f
)
2197 struct tun_struct
*tun
;
2200 memset(&ifr
, 0, sizeof(ifr
));
2205 tun_get_iff(current
->nsproxy
->net_ns
, tun
, &ifr
);
2211 return seq_printf(m
, "iff:\t%s\n", ifr
.ifr_name
);
2215 static const struct file_operations tun_fops
= {
2216 .owner
= THIS_MODULE
,
2217 .llseek
= no_llseek
,
2218 .read
= do_sync_read
,
2219 .aio_read
= tun_chr_aio_read
,
2220 .write
= do_sync_write
,
2221 .aio_write
= tun_chr_aio_write
,
2222 .poll
= tun_chr_poll
,
2223 .unlocked_ioctl
= tun_chr_ioctl
,
2224 #ifdef CONFIG_COMPAT
2225 .compat_ioctl
= tun_chr_compat_ioctl
,
2227 .open
= tun_chr_open
,
2228 .release
= tun_chr_close
,
2229 .fasync
= tun_chr_fasync
,
2230 #ifdef CONFIG_PROC_FS
2231 .show_fdinfo
= tun_chr_show_fdinfo
,
2235 static struct miscdevice tun_miscdev
= {
2238 .nodename
= "net/tun",
2242 /* ethtool interface */
2244 static int tun_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
2247 cmd
->advertising
= 0;
2248 ethtool_cmd_speed_set(cmd
, SPEED_10
);
2249 cmd
->duplex
= DUPLEX_FULL
;
2250 cmd
->port
= PORT_TP
;
2251 cmd
->phy_address
= 0;
2252 cmd
->transceiver
= XCVR_INTERNAL
;
2253 cmd
->autoneg
= AUTONEG_DISABLE
;
2259 static void tun_get_drvinfo(struct net_device
*dev
, struct ethtool_drvinfo
*info
)
2261 struct tun_struct
*tun
= netdev_priv(dev
);
2263 strlcpy(info
->driver
, DRV_NAME
, sizeof(info
->driver
));
2264 strlcpy(info
->version
, DRV_VERSION
, sizeof(info
->version
));
2266 switch (tun
->flags
& TUN_TYPE_MASK
) {
2268 strlcpy(info
->bus_info
, "tun", sizeof(info
->bus_info
));
2271 strlcpy(info
->bus_info
, "tap", sizeof(info
->bus_info
));
2276 static u32
tun_get_msglevel(struct net_device
*dev
)
2279 struct tun_struct
*tun
= netdev_priv(dev
);
2286 static void tun_set_msglevel(struct net_device
*dev
, u32 value
)
2289 struct tun_struct
*tun
= netdev_priv(dev
);
2294 static const struct ethtool_ops tun_ethtool_ops
= {
2295 .get_settings
= tun_get_settings
,
2296 .get_drvinfo
= tun_get_drvinfo
,
2297 .get_msglevel
= tun_get_msglevel
,
2298 .set_msglevel
= tun_set_msglevel
,
2299 .get_link
= ethtool_op_get_link
,
2300 .get_ts_info
= ethtool_op_get_ts_info
,
2304 static int __init
tun_init(void)
2308 pr_info("%s, %s\n", DRV_DESCRIPTION
, DRV_VERSION
);
2309 pr_info("%s\n", DRV_COPYRIGHT
);
2311 ret
= rtnl_link_register(&tun_link_ops
);
2313 pr_err("Can't register link_ops\n");
2317 ret
= misc_register(&tun_miscdev
);
2319 pr_err("Can't register misc device %d\n", TUN_MINOR
);
2324 rtnl_link_unregister(&tun_link_ops
);
2329 static void tun_cleanup(void)
2331 misc_deregister(&tun_miscdev
);
2332 rtnl_link_unregister(&tun_link_ops
);
2335 /* Get an underlying socket object from tun file. Returns error unless file is
2336 * attached to a device. The returned object works like a packet socket, it
2337 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
2338 * holding a reference to the file for as long as the socket is in use. */
2339 struct socket
*tun_get_socket(struct file
*file
)
2341 struct tun_file
*tfile
;
2342 if (file
->f_op
!= &tun_fops
)
2343 return ERR_PTR(-EINVAL
);
2344 tfile
= file
->private_data
;
2346 return ERR_PTR(-EBADFD
);
2347 return &tfile
->socket
;
2349 EXPORT_SYMBOL_GPL(tun_get_socket
);
2351 module_init(tun_init
);
2352 module_exit(tun_cleanup
);
2353 MODULE_DESCRIPTION(DRV_DESCRIPTION
);
2354 MODULE_AUTHOR(DRV_COPYRIGHT
);
2355 MODULE_LICENSE("GPL");
2356 MODULE_ALIAS_MISCDEV(TUN_MINOR
);
2357 MODULE_ALIAS("devname:net/tun");