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/sched/signal.h>
48 #include <linux/major.h>
49 #include <linux/slab.h>
50 #include <linux/poll.h>
51 #include <linux/fcntl.h>
52 #include <linux/init.h>
53 #include <linux/skbuff.h>
54 #include <linux/netdevice.h>
55 #include <linux/etherdevice.h>
56 #include <linux/miscdevice.h>
57 #include <linux/ethtool.h>
58 #include <linux/rtnetlink.h>
59 #include <linux/compat.h>
61 #include <linux/if_arp.h>
62 #include <linux/if_ether.h>
63 #include <linux/if_tun.h>
64 #include <linux/if_vlan.h>
65 #include <linux/crc32.h>
66 #include <linux/nsproxy.h>
67 #include <linux/virtio_net.h>
68 #include <linux/rcupdate.h>
69 #include <net/net_namespace.h>
70 #include <net/netns/generic.h>
71 #include <net/rtnetlink.h>
74 #include <linux/seq_file.h>
75 #include <linux/uio.h>
76 #include <linux/skb_array.h>
77 #include <linux/bpf.h>
78 #include <linux/bpf_trace.h>
79 #include <linux/mutex.h>
80 #include <linux/ieee802154.h>
81 #include <linux/if_ltalk.h>
82 #include <uapi/linux/if_fddi.h>
83 #include <uapi/linux/if_hippi.h>
84 #include <uapi/linux/if_fc.h>
87 #include <net/6lowpan.h>
89 #include <linux/uaccess.h>
90 #include <linux/proc_fs.h>
92 static void tun_default_link_ksettings(struct net_device *dev,
93 struct ethtool_link_ksettings *cmd);
95 /* Uncomment to enable debugging */
96 /* #define TUN_DEBUG 1 */
101 #define tun_debug(level, tun, fmt, args...) \
104 netdev_printk(level, tun->dev, fmt, ##args); \
106 #define DBG1(level, fmt, args...) \
109 printk(level fmt, ##args); \
112 #define tun_debug(level, tun, fmt, args...) \
115 netdev_printk(level, tun->dev, fmt, ##args); \
117 #define DBG1(level, fmt, args...) \
120 printk(level fmt, ##args); \
124 #define TUN_HEADROOM 256
125 #define TUN_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
127 /* TUN device flags */
129 /* IFF_ATTACH_QUEUE is never stored in device flags,
130 * overload it to mean fasync when stored there.
132 #define TUN_FASYNC IFF_ATTACH_QUEUE
133 /* High bits in flags field are unused. */
134 #define TUN_VNET_LE 0x80000000
135 #define TUN_VNET_BE 0x40000000
137 #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
138 IFF_MULTI_QUEUE | IFF_NAPI | IFF_NAPI_FRAGS)
140 #define GOODCOPY_LEN 128
142 #define FLT_EXACT_COUNT 8
144 unsigned int count; /* Number of addrs. Zero means disabled */
145 u32 mask[2]; /* Mask of the hashed addrs */
146 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
149 /* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal
150 * to max number of VCPUs in guest. */
151 #define MAX_TAP_QUEUES 256
152 #define MAX_TAP_FLOWS 4096
154 #define TUN_FLOW_EXPIRE (3 * HZ)
156 struct tun_pcpu_stats {
161 struct u64_stats_sync syncp;
167 /* A tun_file connects an open character device to a tuntap netdevice. It
168 * also contains all socket related structures (except sock_fprog and tap_filter)
169 * to serve as one transmit queue for tuntap device. The sock_fprog and
170 * tap_filter were kept in tun_struct since they were used for filtering for the
171 * netdevice not for a specific queue (at least I didn't see the requirement for
175 * The tun_file and tun_struct are loosely coupled, the pointer from one to the
176 * other can only be read while rcu_read_lock or rtnl_lock is held.
180 struct socket socket;
182 struct tun_struct __rcu *tun;
183 struct fasync_struct *fasync;
184 /* only used for fasnyc */
188 unsigned int ifindex;
190 struct napi_struct napi;
192 bool napi_frags_enabled;
193 struct mutex napi_mutex; /* Protects access to the above napi */
194 struct list_head next;
195 struct tun_struct *detached;
196 struct ptr_ring tx_ring;
197 struct xdp_rxq_info xdp_rxq;
200 struct tun_flow_entry {
201 struct hlist_node hash_link;
203 struct tun_struct *tun;
208 unsigned long updated;
211 #define TUN_NUM_FLOW_ENTRIES 1024
212 #define TUN_MASK_FLOW_ENTRIES (TUN_NUM_FLOW_ENTRIES - 1)
216 struct bpf_prog *prog;
219 /* Since the socket were moved to tun_file, to preserve the behavior of persist
220 * device, socket filter, sndbuf and vnet header size were restore when the
221 * file were attached to a persist device.
224 struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
225 unsigned int numqueues;
230 struct net_device *dev;
231 netdev_features_t set_features;
232 #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
238 struct tap_filter txflt;
239 struct sock_fprog fprog;
240 /* protected by rtnl lock */
241 bool filter_attached;
246 struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
247 struct timer_list flow_gc_timer;
248 unsigned long ageing_time;
249 unsigned int numdisabled;
250 struct list_head disabled;
254 struct tun_pcpu_stats __percpu *pcpu_stats;
255 struct bpf_prog __rcu *xdp_prog;
256 struct tun_prog __rcu *steering_prog;
257 struct tun_prog __rcu *filter_prog;
258 struct ethtool_link_ksettings link_ksettings;
266 bool tun_is_xdp_frame(void *ptr)
268 return (unsigned long)ptr & TUN_XDP_FLAG;
270 EXPORT_SYMBOL(tun_is_xdp_frame);
272 void *tun_xdp_to_ptr(void *ptr)
274 return (void *)((unsigned long)ptr | TUN_XDP_FLAG);
276 EXPORT_SYMBOL(tun_xdp_to_ptr);
278 void *tun_ptr_to_xdp(void *ptr)
280 return (void *)((unsigned long)ptr & ~TUN_XDP_FLAG);
282 EXPORT_SYMBOL(tun_ptr_to_xdp);
284 static int tun_napi_receive(struct napi_struct *napi, int budget)
286 struct tun_file *tfile = container_of(napi, struct tun_file, napi);
287 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
288 struct sk_buff_head process_queue;
292 __skb_queue_head_init(&process_queue);
294 spin_lock(&queue->lock);
295 skb_queue_splice_tail_init(queue, &process_queue);
296 spin_unlock(&queue->lock);
298 while (received < budget && (skb = __skb_dequeue(&process_queue))) {
299 napi_gro_receive(napi, skb);
303 if (!skb_queue_empty(&process_queue)) {
304 spin_lock(&queue->lock);
305 skb_queue_splice(&process_queue, queue);
306 spin_unlock(&queue->lock);
312 static int tun_napi_poll(struct napi_struct *napi, int budget)
314 unsigned int received;
316 received = tun_napi_receive(napi, budget);
318 if (received < budget)
319 napi_complete_done(napi, received);
324 static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile,
325 bool napi_en, bool napi_frags)
327 tfile->napi_enabled = napi_en;
328 tfile->napi_frags_enabled = napi_en && napi_frags;
330 netif_tx_napi_add(tun->dev, &tfile->napi, tun_napi_poll,
332 napi_enable(&tfile->napi);
336 static void tun_napi_disable(struct tun_file *tfile)
338 if (tfile->napi_enabled)
339 napi_disable(&tfile->napi);
342 static void tun_napi_del(struct tun_file *tfile)
344 if (tfile->napi_enabled)
345 netif_napi_del(&tfile->napi);
348 static bool tun_napi_frags_enabled(const struct tun_file *tfile)
350 return tfile->napi_frags_enabled;
353 #ifdef CONFIG_TUN_VNET_CROSS_LE
354 static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
356 return tun->flags & TUN_VNET_BE ? false :
357 virtio_legacy_is_little_endian();
360 static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
362 int be = !!(tun->flags & TUN_VNET_BE);
364 if (put_user(be, argp))
370 static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
374 if (get_user(be, argp))
378 tun->flags |= TUN_VNET_BE;
380 tun->flags &= ~TUN_VNET_BE;
385 static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
387 return virtio_legacy_is_little_endian();
390 static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
395 static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
399 #endif /* CONFIG_TUN_VNET_CROSS_LE */
401 static inline bool tun_is_little_endian(struct tun_struct *tun)
403 return tun->flags & TUN_VNET_LE ||
404 tun_legacy_is_little_endian(tun);
407 static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)
409 return __virtio16_to_cpu(tun_is_little_endian(tun), val);
412 static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val)
414 return __cpu_to_virtio16(tun_is_little_endian(tun), val);
417 static inline u32 tun_hashfn(u32 rxhash)
419 return rxhash & TUN_MASK_FLOW_ENTRIES;
422 static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
424 struct tun_flow_entry *e;
426 hlist_for_each_entry_rcu(e, head, hash_link) {
427 if (e->rxhash == rxhash)
433 static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
434 struct hlist_head *head,
435 u32 rxhash, u16 queue_index)
437 struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
440 tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
441 rxhash, queue_index);
442 e->updated = jiffies;
445 e->queue_index = queue_index;
447 hlist_add_head_rcu(&e->hash_link, head);
453 static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
455 tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
456 e->rxhash, e->queue_index);
457 hlist_del_rcu(&e->hash_link);
462 static void tun_flow_flush(struct tun_struct *tun)
466 spin_lock_bh(&tun->lock);
467 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
468 struct tun_flow_entry *e;
469 struct hlist_node *n;
471 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
472 tun_flow_delete(tun, e);
474 spin_unlock_bh(&tun->lock);
477 static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
481 spin_lock_bh(&tun->lock);
482 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
483 struct tun_flow_entry *e;
484 struct hlist_node *n;
486 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
487 if (e->queue_index == queue_index)
488 tun_flow_delete(tun, e);
491 spin_unlock_bh(&tun->lock);
494 static void tun_flow_cleanup(struct timer_list *t)
496 struct tun_struct *tun = from_timer(tun, t, flow_gc_timer);
497 unsigned long delay = tun->ageing_time;
498 unsigned long next_timer = jiffies + delay;
499 unsigned long count = 0;
502 tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
504 spin_lock(&tun->lock);
505 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
506 struct tun_flow_entry *e;
507 struct hlist_node *n;
509 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
510 unsigned long this_timer;
512 this_timer = e->updated + delay;
513 if (time_before_eq(this_timer, jiffies)) {
514 tun_flow_delete(tun, e);
518 if (time_before(this_timer, next_timer))
519 next_timer = this_timer;
524 mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
525 spin_unlock(&tun->lock);
528 static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
529 struct tun_file *tfile)
531 struct hlist_head *head;
532 struct tun_flow_entry *e;
533 unsigned long delay = tun->ageing_time;
534 u16 queue_index = tfile->queue_index;
539 head = &tun->flows[tun_hashfn(rxhash)];
543 e = tun_flow_find(head, rxhash);
545 /* TODO: keep queueing to old queue until it's empty? */
546 e->queue_index = queue_index;
547 e->updated = jiffies;
548 sock_rps_record_flow_hash(e->rps_rxhash);
550 spin_lock_bh(&tun->lock);
551 if (!tun_flow_find(head, rxhash) &&
552 tun->flow_count < MAX_TAP_FLOWS)
553 tun_flow_create(tun, head, rxhash, queue_index);
555 if (!timer_pending(&tun->flow_gc_timer))
556 mod_timer(&tun->flow_gc_timer,
557 round_jiffies_up(jiffies + delay));
558 spin_unlock_bh(&tun->lock);
565 * Save the hash received in the stack receive path and update the
566 * flow_hash table accordingly.
568 static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
570 if (unlikely(e->rps_rxhash != hash))
571 e->rps_rxhash = hash;
574 /* We try to identify a flow through its rxhash first. The reason that
575 * we do not check rxq no. is because some cards(e.g 82599), chooses
576 * the rxq based on the txq where the last packet of the flow comes. As
577 * the userspace application move between processors, we may get a
578 * different rxq no. here. If we could not get rxhash, then we would
579 * hope the rxq no. may help here.
581 static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
583 struct tun_flow_entry *e;
587 numqueues = READ_ONCE(tun->numqueues);
589 txq = __skb_get_hash_symmetric(skb);
591 e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
593 tun_flow_save_rps_rxhash(e, txq);
594 txq = e->queue_index;
596 /* use multiply and shift instead of expensive divide */
597 txq = ((u64)txq * numqueues) >> 32;
598 } else if (likely(skb_rx_queue_recorded(skb))) {
599 txq = skb_get_rx_queue(skb);
600 while (unlikely(txq >= numqueues))
607 static u16 tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb)
609 struct tun_prog *prog;
613 numqueues = READ_ONCE(tun->numqueues);
617 prog = rcu_dereference(tun->steering_prog);
619 ret = bpf_prog_run_clear_cb(prog->prog, skb);
621 return ret % numqueues;
624 static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
625 struct net_device *sb_dev,
626 select_queue_fallback_t fallback)
628 struct tun_struct *tun = netdev_priv(dev);
632 if (rcu_dereference(tun->steering_prog))
633 ret = tun_ebpf_select_queue(tun, skb);
635 ret = tun_automq_select_queue(tun, skb);
641 static inline bool tun_not_capable(struct tun_struct *tun)
643 const struct cred *cred = current_cred();
644 struct net *net = dev_net(tun->dev);
646 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
647 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
648 !ns_capable(net->user_ns, CAP_NET_ADMIN);
651 static void tun_set_real_num_queues(struct tun_struct *tun)
653 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
654 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
657 static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
659 tfile->detached = tun;
660 list_add_tail(&tfile->next, &tun->disabled);
664 static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
666 struct tun_struct *tun = tfile->detached;
668 tfile->detached = NULL;
669 list_del_init(&tfile->next);
674 void tun_ptr_free(void *ptr)
678 if (tun_is_xdp_frame(ptr)) {
679 struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
681 xdp_return_frame(xdpf);
683 __skb_array_destroy_skb(ptr);
686 EXPORT_SYMBOL_GPL(tun_ptr_free);
688 static void tun_queue_purge(struct tun_file *tfile)
692 while ((ptr = ptr_ring_consume(&tfile->tx_ring)) != NULL)
695 skb_queue_purge(&tfile->sk.sk_write_queue);
696 skb_queue_purge(&tfile->sk.sk_error_queue);
699 static void __tun_detach(struct tun_file *tfile, bool clean)
701 struct tun_file *ntfile;
702 struct tun_struct *tun;
704 tun = rtnl_dereference(tfile->tun);
707 tun_napi_disable(tfile);
711 if (tun && !tfile->detached) {
712 u16 index = tfile->queue_index;
713 BUG_ON(index >= tun->numqueues);
715 rcu_assign_pointer(tun->tfiles[index],
716 tun->tfiles[tun->numqueues - 1]);
717 ntfile = rtnl_dereference(tun->tfiles[index]);
718 ntfile->queue_index = index;
719 rcu_assign_pointer(tun->tfiles[tun->numqueues - 1],
724 RCU_INIT_POINTER(tfile->tun, NULL);
725 sock_put(&tfile->sk);
727 tun_disable_queue(tun, tfile);
730 tun_flow_delete_by_queue(tun, tun->numqueues + 1);
731 /* Drop read queue */
732 tun_queue_purge(tfile);
733 tun_set_real_num_queues(tun);
734 } else if (tfile->detached && clean) {
735 tun = tun_enable_queue(tfile);
736 sock_put(&tfile->sk);
740 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
741 netif_carrier_off(tun->dev);
743 if (!(tun->flags & IFF_PERSIST) &&
744 tun->dev->reg_state == NETREG_REGISTERED)
745 unregister_netdevice(tun->dev);
748 xdp_rxq_info_unreg(&tfile->xdp_rxq);
749 ptr_ring_cleanup(&tfile->tx_ring, tun_ptr_free);
750 sock_put(&tfile->sk);
754 static void tun_detach(struct tun_file *tfile, bool clean)
756 struct tun_struct *tun;
757 struct net_device *dev;
760 tun = rtnl_dereference(tfile->tun);
761 dev = tun ? tun->dev : NULL;
762 __tun_detach(tfile, clean);
764 netdev_state_change(dev);
768 static void tun_detach_all(struct net_device *dev)
770 struct tun_struct *tun = netdev_priv(dev);
771 struct tun_file *tfile, *tmp;
772 int i, n = tun->numqueues;
774 for (i = 0; i < n; i++) {
775 tfile = rtnl_dereference(tun->tfiles[i]);
777 tun_napi_disable(tfile);
778 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
779 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
780 RCU_INIT_POINTER(tfile->tun, NULL);
783 list_for_each_entry(tfile, &tun->disabled, next) {
784 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
785 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
786 RCU_INIT_POINTER(tfile->tun, NULL);
788 BUG_ON(tun->numqueues != 0);
791 for (i = 0; i < n; i++) {
792 tfile = rtnl_dereference(tun->tfiles[i]);
794 /* Drop read queue */
795 tun_queue_purge(tfile);
796 xdp_rxq_info_unreg(&tfile->xdp_rxq);
797 sock_put(&tfile->sk);
799 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
800 tun_enable_queue(tfile);
801 tun_queue_purge(tfile);
802 xdp_rxq_info_unreg(&tfile->xdp_rxq);
803 sock_put(&tfile->sk);
805 BUG_ON(tun->numdisabled != 0);
807 if (tun->flags & IFF_PERSIST)
808 module_put(THIS_MODULE);
811 static int tun_attach(struct tun_struct *tun, struct file *file,
812 bool skip_filter, bool napi, bool napi_frags,
815 struct tun_file *tfile = file->private_data;
816 struct net_device *dev = tun->dev;
819 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
824 if (rtnl_dereference(tfile->tun) && !tfile->detached)
828 if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
832 if (!tfile->detached &&
833 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
838 /* Re-attach the filter to persist device */
839 if (!skip_filter && (tun->filter_attached == true)) {
840 lock_sock(tfile->socket.sk);
841 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
842 release_sock(tfile->socket.sk);
847 if (!tfile->detached &&
848 ptr_ring_resize(&tfile->tx_ring, dev->tx_queue_len,
849 GFP_KERNEL, tun_ptr_free)) {
854 tfile->queue_index = tun->numqueues;
855 tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
857 if (tfile->detached) {
858 /* Re-attach detached tfile, updating XDP queue_index */
859 WARN_ON(!xdp_rxq_info_is_reg(&tfile->xdp_rxq));
861 if (tfile->xdp_rxq.queue_index != tfile->queue_index)
862 tfile->xdp_rxq.queue_index = tfile->queue_index;
864 /* Setup XDP RX-queue info, for new tfile getting attached */
865 err = xdp_rxq_info_reg(&tfile->xdp_rxq,
866 tun->dev, tfile->queue_index);
869 err = xdp_rxq_info_reg_mem_model(&tfile->xdp_rxq,
870 MEM_TYPE_PAGE_SHARED, NULL);
872 xdp_rxq_info_unreg(&tfile->xdp_rxq);
878 if (tfile->detached) {
879 tun_enable_queue(tfile);
881 sock_hold(&tfile->sk);
882 tun_napi_init(tun, tfile, napi, napi_frags);
885 /* device is allowed to go away first, so no need to hold extra
889 /* Publish tfile->tun and tun->tfiles only after we've fully
890 * initialized tfile; otherwise we risk using half-initialized
894 rcu_assign_pointer(tfile->tun, tun);
895 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
897 tun_set_real_num_queues(tun);
902 static struct tun_struct *tun_get(struct tun_file *tfile)
904 struct tun_struct *tun;
907 tun = rcu_dereference(tfile->tun);
915 static void tun_put(struct tun_struct *tun)
921 static void addr_hash_set(u32 *mask, const u8 *addr)
923 int n = ether_crc(ETH_ALEN, addr) >> 26;
924 mask[n >> 5] |= (1 << (n & 31));
927 static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
929 int n = ether_crc(ETH_ALEN, addr) >> 26;
930 return mask[n >> 5] & (1 << (n & 31));
933 static int update_filter(struct tap_filter *filter, void __user *arg)
935 struct { u8 u[ETH_ALEN]; } *addr;
936 struct tun_filter uf;
937 int err, alen, n, nexact;
939 if (copy_from_user(&uf, arg, sizeof(uf)))
948 alen = ETH_ALEN * uf.count;
949 addr = memdup_user(arg + sizeof(uf), alen);
951 return PTR_ERR(addr);
953 /* The filter is updated without holding any locks. Which is
954 * perfectly safe. We disable it first and in the worst
955 * case we'll accept a few undesired packets. */
959 /* Use first set of addresses as an exact filter */
960 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
961 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
965 /* Remaining multicast addresses are hashed,
966 * unicast will leave the filter disabled. */
967 memset(filter->mask, 0, sizeof(filter->mask));
968 for (; n < uf.count; n++) {
969 if (!is_multicast_ether_addr(addr[n].u)) {
970 err = 0; /* no filter */
973 addr_hash_set(filter->mask, addr[n].u);
976 /* For ALLMULTI just set the mask to all ones.
977 * This overrides the mask populated above. */
978 if ((uf.flags & TUN_FLT_ALLMULTI))
979 memset(filter->mask, ~0, sizeof(filter->mask));
981 /* Now enable the filter */
983 filter->count = nexact;
985 /* Return the number of exact filters */
992 /* Returns: 0 - drop, !=0 - accept */
993 static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
995 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
997 struct ethhdr *eh = (struct ethhdr *) skb->data;
1001 for (i = 0; i < filter->count; i++)
1002 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
1005 /* Inexact match (multicast only) */
1006 if (is_multicast_ether_addr(eh->h_dest))
1007 return addr_hash_test(filter->mask, eh->h_dest);
1013 * Checks whether the packet is accepted or not.
1014 * Returns: 0 - drop, !=0 - accept
1016 static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
1021 return run_filter(filter, skb);
1024 /* Network device part of the driver */
1026 static const struct ethtool_ops tun_ethtool_ops;
1028 /* Net device detach from fd. */
1029 static void tun_net_uninit(struct net_device *dev)
1031 tun_detach_all(dev);
1034 /* Net device open. */
1035 static int tun_net_open(struct net_device *dev)
1037 netif_tx_start_all_queues(dev);
1042 /* Net device close. */
1043 static int tun_net_close(struct net_device *dev)
1045 netif_tx_stop_all_queues(dev);
1049 /* Net device start xmit */
1050 static void tun_automq_xmit(struct tun_struct *tun, struct sk_buff *skb)
1053 if (tun->numqueues == 1 && static_key_false(&rps_needed)) {
1054 /* Select queue was not called for the skbuff, so we extract the
1055 * RPS hash and save it into the flow_table here.
1059 rxhash = __skb_get_hash_symmetric(skb);
1061 struct tun_flow_entry *e;
1062 e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)],
1065 tun_flow_save_rps_rxhash(e, rxhash);
1071 static unsigned int run_ebpf_filter(struct tun_struct *tun,
1072 struct sk_buff *skb,
1075 struct tun_prog *prog = rcu_dereference(tun->filter_prog);
1078 len = bpf_prog_run_clear_cb(prog->prog, skb);
1083 /* Net device start xmit */
1084 static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
1086 struct tun_struct *tun = netdev_priv(dev);
1087 int txq = skb->queue_mapping;
1088 struct netdev_queue *queue;
1089 struct tun_file *tfile;
1093 tfile = rcu_dereference(tun->tfiles[txq]);
1095 /* Drop packet if interface is not attached */
1099 if (!rcu_dereference(tun->steering_prog))
1100 tun_automq_xmit(tun, skb);
1102 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
1106 /* Drop if the filter does not like it.
1107 * This is a noop if the filter is disabled.
1108 * Filter can be enabled only for the TAP devices. */
1109 if (!check_filter(&tun->txflt, skb))
1112 if (tfile->socket.sk->sk_filter &&
1113 sk_filter(tfile->socket.sk, skb))
1116 len = run_ebpf_filter(tun, skb, len);
1117 if (len == 0 || pskb_trim(skb, len))
1120 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
1123 skb_tx_timestamp(skb);
1125 /* Orphan the skb - required as we might hang on to it
1126 * for indefinite time.
1132 if (ptr_ring_produce(&tfile->tx_ring, skb))
1135 /* NETIF_F_LLTX requires to do our own update of trans_start */
1136 queue = netdev_get_tx_queue(dev, txq);
1137 queue->trans_start = jiffies;
1139 /* Notify and wake up reader process */
1140 if (tfile->flags & TUN_FASYNC)
1141 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
1142 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
1145 return NETDEV_TX_OK;
1148 this_cpu_inc(tun->pcpu_stats->tx_dropped);
1152 return NET_XMIT_DROP;
1155 static void tun_net_mclist(struct net_device *dev)
1158 * This callback is supposed to deal with mc filter in
1159 * _rx_ path and has nothing to do with the _tx_ path.
1160 * In rx path we always accept everything userspace gives us.
1164 static netdev_features_t tun_net_fix_features(struct net_device *dev,
1165 netdev_features_t features)
1167 struct tun_struct *tun = netdev_priv(dev);
1169 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
1172 static void tun_set_headroom(struct net_device *dev, int new_hr)
1174 struct tun_struct *tun = netdev_priv(dev);
1176 if (new_hr < NET_SKB_PAD)
1177 new_hr = NET_SKB_PAD;
1179 tun->align = new_hr;
1183 tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1185 u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0;
1186 struct tun_struct *tun = netdev_priv(dev);
1187 struct tun_pcpu_stats *p;
1190 for_each_possible_cpu(i) {
1191 u64 rxpackets, rxbytes, txpackets, txbytes;
1194 p = per_cpu_ptr(tun->pcpu_stats, i);
1196 start = u64_stats_fetch_begin(&p->syncp);
1197 rxpackets = p->rx_packets;
1198 rxbytes = p->rx_bytes;
1199 txpackets = p->tx_packets;
1200 txbytes = p->tx_bytes;
1201 } while (u64_stats_fetch_retry(&p->syncp, start));
1203 stats->rx_packets += rxpackets;
1204 stats->rx_bytes += rxbytes;
1205 stats->tx_packets += txpackets;
1206 stats->tx_bytes += txbytes;
1209 rx_dropped += p->rx_dropped;
1210 rx_frame_errors += p->rx_frame_errors;
1211 tx_dropped += p->tx_dropped;
1213 stats->rx_dropped = rx_dropped;
1214 stats->rx_frame_errors = rx_frame_errors;
1215 stats->tx_dropped = tx_dropped;
1218 static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1219 struct netlink_ext_ack *extack)
1221 struct tun_struct *tun = netdev_priv(dev);
1222 struct bpf_prog *old_prog;
1224 old_prog = rtnl_dereference(tun->xdp_prog);
1225 rcu_assign_pointer(tun->xdp_prog, prog);
1227 bpf_prog_put(old_prog);
1232 static u32 tun_xdp_query(struct net_device *dev)
1234 struct tun_struct *tun = netdev_priv(dev);
1235 const struct bpf_prog *xdp_prog;
1237 xdp_prog = rtnl_dereference(tun->xdp_prog);
1239 return xdp_prog->aux->id;
1244 static int tun_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1246 switch (xdp->command) {
1247 case XDP_SETUP_PROG:
1248 return tun_xdp_set(dev, xdp->prog, xdp->extack);
1249 case XDP_QUERY_PROG:
1250 xdp->prog_id = tun_xdp_query(dev);
1257 static const struct net_device_ops tun_netdev_ops = {
1258 .ndo_uninit = tun_net_uninit,
1259 .ndo_open = tun_net_open,
1260 .ndo_stop = tun_net_close,
1261 .ndo_start_xmit = tun_net_xmit,
1262 .ndo_fix_features = tun_net_fix_features,
1263 .ndo_select_queue = tun_select_queue,
1264 .ndo_set_rx_headroom = tun_set_headroom,
1265 .ndo_get_stats64 = tun_net_get_stats64,
1268 static void __tun_xdp_flush_tfile(struct tun_file *tfile)
1270 /* Notify and wake up reader process */
1271 if (tfile->flags & TUN_FASYNC)
1272 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
1273 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
1276 static int tun_xdp_xmit(struct net_device *dev, int n,
1277 struct xdp_frame **frames, u32 flags)
1279 struct tun_struct *tun = netdev_priv(dev);
1280 struct tun_file *tfile;
1286 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
1292 numqueues = READ_ONCE(tun->numqueues);
1295 return -ENXIO; /* Caller will free/return all frames */
1298 tfile = rcu_dereference(tun->tfiles[smp_processor_id() %
1300 if (unlikely(!tfile))
1303 spin_lock(&tfile->tx_ring.producer_lock);
1304 for (i = 0; i < n; i++) {
1305 struct xdp_frame *xdp = frames[i];
1306 /* Encode the XDP flag into lowest bit for consumer to differ
1307 * XDP buffer from sk_buff.
1309 void *frame = tun_xdp_to_ptr(xdp);
1311 if (__ptr_ring_produce(&tfile->tx_ring, frame)) {
1312 this_cpu_inc(tun->pcpu_stats->tx_dropped);
1313 xdp_return_frame_rx_napi(xdp);
1317 spin_unlock(&tfile->tx_ring.producer_lock);
1319 if (flags & XDP_XMIT_FLUSH)
1320 __tun_xdp_flush_tfile(tfile);
1326 static int tun_xdp_tx(struct net_device *dev, struct xdp_buff *xdp)
1328 struct xdp_frame *frame = convert_to_xdp_frame(xdp);
1330 if (unlikely(!frame))
1333 return tun_xdp_xmit(dev, 1, &frame, XDP_XMIT_FLUSH);
1336 static const struct net_device_ops tap_netdev_ops = {
1337 .ndo_uninit = tun_net_uninit,
1338 .ndo_open = tun_net_open,
1339 .ndo_stop = tun_net_close,
1340 .ndo_start_xmit = tun_net_xmit,
1341 .ndo_fix_features = tun_net_fix_features,
1342 .ndo_set_rx_mode = tun_net_mclist,
1343 .ndo_set_mac_address = eth_mac_addr,
1344 .ndo_validate_addr = eth_validate_addr,
1345 .ndo_select_queue = tun_select_queue,
1346 .ndo_features_check = passthru_features_check,
1347 .ndo_set_rx_headroom = tun_set_headroom,
1348 .ndo_get_stats64 = tun_net_get_stats64,
1350 .ndo_xdp_xmit = tun_xdp_xmit,
1353 static void tun_flow_init(struct tun_struct *tun)
1357 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
1358 INIT_HLIST_HEAD(&tun->flows[i]);
1360 tun->ageing_time = TUN_FLOW_EXPIRE;
1361 timer_setup(&tun->flow_gc_timer, tun_flow_cleanup, 0);
1362 mod_timer(&tun->flow_gc_timer,
1363 round_jiffies_up(jiffies + tun->ageing_time));
1366 static void tun_flow_uninit(struct tun_struct *tun)
1368 del_timer_sync(&tun->flow_gc_timer);
1369 tun_flow_flush(tun);
1373 #define MAX_MTU 65535
1375 /* Initialize net device. */
1376 static void tun_net_init(struct net_device *dev)
1378 struct tun_struct *tun = netdev_priv(dev);
1380 switch (tun->flags & TUN_TYPE_MASK) {
1382 dev->netdev_ops = &tun_netdev_ops;
1384 /* Point-to-Point TUN Device */
1385 dev->hard_header_len = 0;
1389 /* Zero header length */
1390 dev->type = ARPHRD_NONE;
1391 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
1395 dev->netdev_ops = &tap_netdev_ops;
1396 /* Ethernet TAP Device */
1398 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1399 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1401 eth_hw_addr_random(dev);
1406 dev->min_mtu = MIN_MTU;
1407 dev->max_mtu = MAX_MTU - dev->hard_header_len;
1410 static bool tun_sock_writeable(struct tun_struct *tun, struct tun_file *tfile)
1412 struct sock *sk = tfile->socket.sk;
1414 return (tun->dev->flags & IFF_UP) && sock_writeable(sk);
1417 /* Character device part */
1420 static __poll_t tun_chr_poll(struct file *file, poll_table *wait)
1422 struct tun_file *tfile = file->private_data;
1423 struct tun_struct *tun = tun_get(tfile);
1430 sk = tfile->socket.sk;
1432 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
1434 poll_wait(file, sk_sleep(sk), wait);
1436 if (!ptr_ring_empty(&tfile->tx_ring))
1437 mask |= EPOLLIN | EPOLLRDNORM;
1439 /* Make sure SOCKWQ_ASYNC_NOSPACE is set if not writable to
1440 * guarantee EPOLLOUT to be raised by either here or
1441 * tun_sock_write_space(). Then process could get notification
1442 * after it writes to a down device and meets -EIO.
1444 if (tun_sock_writeable(tun, tfile) ||
1445 (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
1446 tun_sock_writeable(tun, tfile)))
1447 mask |= EPOLLOUT | EPOLLWRNORM;
1449 if (tun->dev->reg_state != NETREG_REGISTERED)
1456 static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile,
1458 const struct iov_iter *it)
1460 struct sk_buff *skb;
1465 if (it->nr_segs > MAX_SKB_FRAGS + 1)
1466 return ERR_PTR(-EMSGSIZE);
1469 skb = napi_get_frags(&tfile->napi);
1472 return ERR_PTR(-ENOMEM);
1474 linear = iov_iter_single_seg_count(it);
1475 err = __skb_grow(skb, linear);
1480 skb->data_len = len - linear;
1481 skb->truesize += skb->data_len;
1483 for (i = 1; i < it->nr_segs; i++) {
1484 struct page_frag *pfrag = ¤t->task_frag;
1485 size_t fragsz = it->iov[i].iov_len;
1487 if (fragsz == 0 || fragsz > PAGE_SIZE) {
1492 if (!skb_page_frag_refill(fragsz, pfrag, GFP_KERNEL)) {
1497 skb_fill_page_desc(skb, i - 1, pfrag->page,
1498 pfrag->offset, fragsz);
1499 page_ref_inc(pfrag->page);
1500 pfrag->offset += fragsz;
1505 /* frees skb and all frags allocated with napi_alloc_frag() */
1506 napi_free_frags(&tfile->napi);
1507 return ERR_PTR(err);
1510 /* prepad is the amount to reserve at front. len is length after that.
1511 * linear is a hint as to how much to copy (usually headers). */
1512 static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
1513 size_t prepad, size_t len,
1514 size_t linear, int noblock)
1516 struct sock *sk = tfile->socket.sk;
1517 struct sk_buff *skb;
1520 /* Under a page? Don't bother with paged skb. */
1521 if (prepad + len < PAGE_SIZE || !linear)
1524 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
1527 return ERR_PTR(err);
1529 skb_reserve(skb, prepad);
1530 skb_put(skb, linear);
1531 skb->data_len = len - linear;
1532 skb->len += len - linear;
1537 static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile,
1538 struct sk_buff *skb, int more)
1540 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
1541 struct sk_buff_head process_queue;
1542 u32 rx_batched = tun->rx_batched;
1545 if (!rx_batched || (!more && skb_queue_empty(queue))) {
1547 skb_record_rx_queue(skb, tfile->queue_index);
1548 netif_receive_skb(skb);
1553 spin_lock(&queue->lock);
1554 if (!more || skb_queue_len(queue) == rx_batched) {
1555 __skb_queue_head_init(&process_queue);
1556 skb_queue_splice_tail_init(queue, &process_queue);
1559 __skb_queue_tail(queue, skb);
1561 spin_unlock(&queue->lock);
1564 struct sk_buff *nskb;
1567 while ((nskb = __skb_dequeue(&process_queue))) {
1568 skb_record_rx_queue(nskb, tfile->queue_index);
1569 netif_receive_skb(nskb);
1571 skb_record_rx_queue(skb, tfile->queue_index);
1572 netif_receive_skb(skb);
1577 static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile,
1578 int len, int noblock, bool zerocopy)
1580 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
1583 if (tfile->socket.sk->sk_sndbuf != INT_MAX)
1592 if (SKB_DATA_ALIGN(len + TUN_RX_PAD) +
1593 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE)
1599 static struct sk_buff *tun_build_skb(struct tun_struct *tun,
1600 struct tun_file *tfile,
1601 struct iov_iter *from,
1602 struct virtio_net_hdr *hdr,
1603 int len, int *skb_xdp)
1605 struct page_frag *alloc_frag = ¤t->task_frag;
1606 struct sk_buff *skb;
1607 struct bpf_prog *xdp_prog;
1608 int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1609 unsigned int delta = 0;
1612 int err, pad = TUN_RX_PAD;
1615 xdp_prog = rcu_dereference(tun->xdp_prog);
1617 pad += TUN_HEADROOM;
1618 buflen += SKB_DATA_ALIGN(len + pad);
1621 alloc_frag->offset = ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES);
1622 if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL)))
1623 return ERR_PTR(-ENOMEM);
1625 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1626 copied = copy_page_from_iter(alloc_frag->page,
1627 alloc_frag->offset + pad,
1630 return ERR_PTR(-EFAULT);
1632 /* There's a small window that XDP may be set after the check
1633 * of xdp_prog above, this should be rare and for simplicity
1634 * we do XDP on skb in case the headroom is not enough.
1636 if (hdr->gso_type || !xdp_prog)
1643 xdp_prog = rcu_dereference(tun->xdp_prog);
1644 if (xdp_prog && !*skb_xdp) {
1645 struct xdp_buff xdp;
1649 xdp.data_hard_start = buf;
1650 xdp.data = buf + pad;
1651 xdp_set_data_meta_invalid(&xdp);
1652 xdp.data_end = xdp.data + len;
1653 xdp.rxq = &tfile->xdp_rxq;
1654 orig_data = xdp.data;
1655 act = bpf_prog_run_xdp(xdp_prog, &xdp);
1659 get_page(alloc_frag->page);
1660 alloc_frag->offset += buflen;
1661 err = xdp_do_redirect(tun->dev, &xdp, xdp_prog);
1669 get_page(alloc_frag->page);
1670 alloc_frag->offset += buflen;
1671 if (tun_xdp_tx(tun->dev, &xdp) < 0)
1677 delta = orig_data - xdp.data;
1678 len = xdp.data_end - xdp.data;
1681 bpf_warn_invalid_xdp_action(act);
1684 trace_xdp_exception(tun->dev, xdp_prog, act);
1691 skb = build_skb(buf, buflen);
1695 return ERR_PTR(-ENOMEM);
1698 skb_reserve(skb, pad - delta);
1700 skb_set_owner_w(skb, tfile->socket.sk);
1701 get_page(alloc_frag->page);
1702 alloc_frag->offset += buflen;
1710 put_page(alloc_frag->page);
1714 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1718 /* Get packet from user space buffer */
1719 static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
1720 void *msg_control, struct iov_iter *from,
1721 int noblock, bool more)
1723 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
1724 struct sk_buff *skb;
1725 size_t total_len = iov_iter_count(from);
1726 size_t len = total_len, align = tun->align, linear;
1727 struct virtio_net_hdr gso = { 0 };
1728 struct tun_pcpu_stats *stats;
1731 bool zerocopy = false;
1735 bool frags = tun_napi_frags_enabled(tfile);
1737 if (!(tun->flags & IFF_NO_PI)) {
1738 if (len < sizeof(pi))
1742 if (!copy_from_iter_full(&pi, sizeof(pi), from))
1746 if (tun->flags & IFF_VNET_HDR) {
1747 int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
1749 if (len < vnet_hdr_sz)
1753 if (!copy_from_iter_full(&gso, sizeof(gso), from))
1756 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
1757 tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
1758 gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2);
1760 if (tun16_to_cpu(tun, gso.hdr_len) > len)
1762 iov_iter_advance(from, vnet_hdr_sz - sizeof(gso));
1765 if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
1766 align += NET_IP_ALIGN;
1767 if (unlikely(len < ETH_HLEN ||
1768 (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
1772 good_linear = SKB_MAX_HEAD(align);
1775 struct iov_iter i = *from;
1777 /* There are 256 bytes to be copied in skb, so there is
1778 * enough room for skb expand head in case it is used.
1779 * The rest of the buffer is mapped from userspace.
1781 copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
1782 if (copylen > good_linear)
1783 copylen = good_linear;
1785 iov_iter_advance(&i, copylen);
1786 if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
1790 if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) {
1791 /* For the packet that is not easy to be processed
1792 * (e.g gso or jumbo packet), we will do it at after
1793 * skb was created with generic XDP routine.
1795 skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp);
1797 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1798 return PTR_ERR(skb);
1805 if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
1806 linear = good_linear;
1808 linear = tun16_to_cpu(tun, gso.hdr_len);
1812 mutex_lock(&tfile->napi_mutex);
1813 skb = tun_napi_alloc_frags(tfile, copylen, from);
1814 /* tun_napi_alloc_frags() enforces a layout for the skb.
1815 * If zerocopy is enabled, then this layout will be
1816 * overwritten by zerocopy_sg_from_iter().
1820 skb = tun_alloc_skb(tfile, align, copylen, linear,
1825 if (PTR_ERR(skb) != -EAGAIN)
1826 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1828 mutex_unlock(&tfile->napi_mutex);
1829 return PTR_ERR(skb);
1833 err = zerocopy_sg_from_iter(skb, from);
1835 err = skb_copy_datagram_from_iter(skb, 0, from, len);
1840 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1843 tfile->napi.skb = NULL;
1844 mutex_unlock(&tfile->napi_mutex);
1851 if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) {
1852 this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
1855 tfile->napi.skb = NULL;
1856 mutex_unlock(&tfile->napi_mutex);
1862 switch (tun->flags & TUN_TYPE_MASK) {
1864 if (tun->flags & IFF_NO_PI) {
1865 u8 ip_version = skb->len ? (skb->data[0] >> 4) : 0;
1867 switch (ip_version) {
1869 pi.proto = htons(ETH_P_IP);
1872 pi.proto = htons(ETH_P_IPV6);
1875 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1881 skb_reset_mac_header(skb);
1882 skb->protocol = pi.proto;
1883 skb->dev = tun->dev;
1886 if (frags && !pskb_may_pull(skb, ETH_HLEN)) {
1890 skb->protocol = eth_type_trans(skb, tun->dev);
1894 /* copy skb_ubuf_info for callback when skb has no error */
1896 skb_shinfo(skb)->destructor_arg = msg_control;
1897 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1898 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
1899 } else if (msg_control) {
1900 struct ubuf_info *uarg = msg_control;
1901 uarg->callback(uarg, false);
1904 skb_reset_network_header(skb);
1905 skb_probe_transport_header(skb, 0);
1908 struct bpf_prog *xdp_prog;
1913 xdp_prog = rcu_dereference(tun->xdp_prog);
1915 ret = do_xdp_generic(xdp_prog, skb);
1916 if (ret != XDP_PASS) {
1920 tfile->napi.skb = NULL;
1921 mutex_unlock(&tfile->napi_mutex);
1930 /* Compute the costly rx hash only if needed for flow updates.
1931 * We may get a very small possibility of OOO during switching, not
1932 * worth to optimize.
1934 if (!rcu_access_pointer(tun->steering_prog) && tun->numqueues > 1 &&
1936 rxhash = __skb_get_hash_symmetric(skb);
1939 if (unlikely(!(tun->dev->flags & IFF_UP))) {
1948 /* Exercise flow dissector code path. */
1949 skb_push(skb, ETH_HLEN);
1950 headlen = eth_get_headlen(skb->data, skb_headlen(skb));
1952 if (unlikely(headlen > skb_headlen(skb))) {
1953 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1954 napi_free_frags(&tfile->napi);
1956 mutex_unlock(&tfile->napi_mutex);
1962 napi_gro_frags(&tfile->napi);
1964 mutex_unlock(&tfile->napi_mutex);
1965 } else if (tfile->napi_enabled) {
1966 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
1969 spin_lock_bh(&queue->lock);
1970 __skb_queue_tail(queue, skb);
1971 queue_len = skb_queue_len(queue);
1972 spin_unlock(&queue->lock);
1974 if (!more || queue_len > NAPI_POLL_WEIGHT)
1975 napi_schedule(&tfile->napi);
1978 } else if (!IS_ENABLED(CONFIG_4KSTACKS)) {
1979 tun_rx_batched(tun, tfile, skb, more);
1985 stats = get_cpu_ptr(tun->pcpu_stats);
1986 u64_stats_update_begin(&stats->syncp);
1987 stats->rx_packets++;
1988 stats->rx_bytes += len;
1989 u64_stats_update_end(&stats->syncp);
1993 tun_flow_update(tun, rxhash, tfile);
1998 static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
2000 struct file *file = iocb->ki_filp;
2001 struct tun_file *tfile = file->private_data;
2002 struct tun_struct *tun = tun_get(tfile);
2009 if ((file->f_flags & O_NONBLOCK) || (iocb->ki_flags & IOCB_NOWAIT))
2012 result = tun_get_user(tun, tfile, NULL, from, noblock, false);
2018 static ssize_t tun_put_user_xdp(struct tun_struct *tun,
2019 struct tun_file *tfile,
2020 struct xdp_frame *xdp_frame,
2021 struct iov_iter *iter)
2023 int vnet_hdr_sz = 0;
2024 size_t size = xdp_frame->len;
2025 struct tun_pcpu_stats *stats;
2028 if (tun->flags & IFF_VNET_HDR) {
2029 struct virtio_net_hdr gso = { 0 };
2031 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
2032 if (unlikely(iov_iter_count(iter) < vnet_hdr_sz))
2034 if (unlikely(copy_to_iter(&gso, sizeof(gso), iter) !=
2037 iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
2040 ret = copy_to_iter(xdp_frame->data, size, iter) + vnet_hdr_sz;
2042 stats = get_cpu_ptr(tun->pcpu_stats);
2043 u64_stats_update_begin(&stats->syncp);
2044 stats->tx_packets++;
2045 stats->tx_bytes += ret;
2046 u64_stats_update_end(&stats->syncp);
2047 put_cpu_ptr(tun->pcpu_stats);
2052 /* Put packet to the user space buffer */
2053 static ssize_t tun_put_user(struct tun_struct *tun,
2054 struct tun_file *tfile,
2055 struct sk_buff *skb,
2056 struct iov_iter *iter)
2058 struct tun_pi pi = { 0, skb->protocol };
2059 struct tun_pcpu_stats *stats;
2061 int vlan_offset = 0;
2063 int vnet_hdr_sz = 0;
2065 if (skb_vlan_tag_present(skb))
2066 vlan_hlen = VLAN_HLEN;
2068 if (tun->flags & IFF_VNET_HDR)
2069 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
2071 total = skb->len + vlan_hlen + vnet_hdr_sz;
2073 if (!(tun->flags & IFF_NO_PI)) {
2074 if (iov_iter_count(iter) < sizeof(pi))
2077 total += sizeof(pi);
2078 if (iov_iter_count(iter) < total) {
2079 /* Packet will be striped */
2080 pi.flags |= TUN_PKT_STRIP;
2083 if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
2088 struct virtio_net_hdr gso;
2090 if (iov_iter_count(iter) < vnet_hdr_sz)
2093 if (virtio_net_hdr_from_skb(skb, &gso,
2094 tun_is_little_endian(tun), true,
2096 struct skb_shared_info *sinfo = skb_shinfo(skb);
2097 pr_err("unexpected GSO type: "
2098 "0x%x, gso_size %d, hdr_len %d\n",
2099 sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
2100 tun16_to_cpu(tun, gso.hdr_len));
2101 print_hex_dump(KERN_ERR, "tun: ",
2104 min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
2109 if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
2112 iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
2119 veth.h_vlan_proto = skb->vlan_proto;
2120 veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
2122 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
2124 ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
2125 if (ret || !iov_iter_count(iter))
2128 ret = copy_to_iter(&veth, sizeof(veth), iter);
2129 if (ret != sizeof(veth) || !iov_iter_count(iter))
2133 skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset);
2136 /* caller is in process context, */
2137 stats = get_cpu_ptr(tun->pcpu_stats);
2138 u64_stats_update_begin(&stats->syncp);
2139 stats->tx_packets++;
2140 stats->tx_bytes += skb->len + vlan_hlen;
2141 u64_stats_update_end(&stats->syncp);
2142 put_cpu_ptr(tun->pcpu_stats);
2147 static void *tun_ring_recv(struct tun_file *tfile, int noblock, int *err)
2149 DECLARE_WAITQUEUE(wait, current);
2153 ptr = ptr_ring_consume(&tfile->tx_ring);
2161 add_wait_queue(&tfile->wq.wait, &wait);
2164 set_current_state(TASK_INTERRUPTIBLE);
2165 ptr = ptr_ring_consume(&tfile->tx_ring);
2168 if (signal_pending(current)) {
2169 error = -ERESTARTSYS;
2172 if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) {
2180 __set_current_state(TASK_RUNNING);
2181 remove_wait_queue(&tfile->wq.wait, &wait);
2188 static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
2189 struct iov_iter *to,
2190 int noblock, void *ptr)
2195 tun_debug(KERN_INFO, tun, "tun_do_read\n");
2197 if (!iov_iter_count(to)) {
2203 /* Read frames from ring */
2204 ptr = tun_ring_recv(tfile, noblock, &err);
2209 if (tun_is_xdp_frame(ptr)) {
2210 struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
2212 ret = tun_put_user_xdp(tun, tfile, xdpf, to);
2213 xdp_return_frame(xdpf);
2215 struct sk_buff *skb = ptr;
2217 ret = tun_put_user(tun, tfile, skb, to);
2218 if (unlikely(ret < 0))
2227 static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
2229 struct file *file = iocb->ki_filp;
2230 struct tun_file *tfile = file->private_data;
2231 struct tun_struct *tun = tun_get(tfile);
2232 ssize_t len = iov_iter_count(to), ret;
2238 if ((file->f_flags & O_NONBLOCK) || (iocb->ki_flags & IOCB_NOWAIT))
2241 ret = tun_do_read(tun, tfile, to, noblock, NULL);
2242 ret = min_t(ssize_t, ret, len);
2249 static void tun_prog_free(struct rcu_head *rcu)
2251 struct tun_prog *prog = container_of(rcu, struct tun_prog, rcu);
2253 bpf_prog_destroy(prog->prog);
2257 static int __tun_set_ebpf(struct tun_struct *tun,
2258 struct tun_prog __rcu **prog_p,
2259 struct bpf_prog *prog)
2261 struct tun_prog *old, *new = NULL;
2264 new = kmalloc(sizeof(*new), GFP_KERNEL);
2270 spin_lock_bh(&tun->lock);
2271 old = rcu_dereference_protected(*prog_p,
2272 lockdep_is_held(&tun->lock));
2273 rcu_assign_pointer(*prog_p, new);
2274 spin_unlock_bh(&tun->lock);
2277 call_rcu(&old->rcu, tun_prog_free);
2282 static void tun_free_netdev(struct net_device *dev)
2284 struct tun_struct *tun = netdev_priv(dev);
2286 BUG_ON(!(list_empty(&tun->disabled)));
2287 free_percpu(tun->pcpu_stats);
2288 tun_flow_uninit(tun);
2289 security_tun_dev_free_security(tun->security);
2290 __tun_set_ebpf(tun, &tun->steering_prog, NULL);
2291 __tun_set_ebpf(tun, &tun->filter_prog, NULL);
2294 static void tun_setup(struct net_device *dev)
2296 struct tun_struct *tun = netdev_priv(dev);
2298 tun->owner = INVALID_UID;
2299 tun->group = INVALID_GID;
2300 tun_default_link_ksettings(dev, &tun->link_ksettings);
2302 dev->ethtool_ops = &tun_ethtool_ops;
2303 dev->needs_free_netdev = true;
2304 dev->priv_destructor = tun_free_netdev;
2305 /* We prefer our own queue length */
2306 dev->tx_queue_len = TUN_READQ_SIZE;
2309 /* Trivial set of netlink ops to allow deleting tun or tap
2310 * device with netlink.
2312 static int tun_validate(struct nlattr *tb[], struct nlattr *data[],
2313 struct netlink_ext_ack *extack)
2315 NL_SET_ERR_MSG(extack,
2316 "tun/tap creation via rtnetlink is not supported.");
2320 static size_t tun_get_size(const struct net_device *dev)
2322 BUILD_BUG_ON(sizeof(u32) != sizeof(uid_t));
2323 BUILD_BUG_ON(sizeof(u32) != sizeof(gid_t));
2325 return nla_total_size(sizeof(uid_t)) + /* OWNER */
2326 nla_total_size(sizeof(gid_t)) + /* GROUP */
2327 nla_total_size(sizeof(u8)) + /* TYPE */
2328 nla_total_size(sizeof(u8)) + /* PI */
2329 nla_total_size(sizeof(u8)) + /* VNET_HDR */
2330 nla_total_size(sizeof(u8)) + /* PERSIST */
2331 nla_total_size(sizeof(u8)) + /* MULTI_QUEUE */
2332 nla_total_size(sizeof(u32)) + /* NUM_QUEUES */
2333 nla_total_size(sizeof(u32)) + /* NUM_DISABLED_QUEUES */
2337 static int tun_fill_info(struct sk_buff *skb, const struct net_device *dev)
2339 struct tun_struct *tun = netdev_priv(dev);
2341 if (nla_put_u8(skb, IFLA_TUN_TYPE, tun->flags & TUN_TYPE_MASK))
2342 goto nla_put_failure;
2343 if (uid_valid(tun->owner) &&
2344 nla_put_u32(skb, IFLA_TUN_OWNER,
2345 from_kuid_munged(current_user_ns(), tun->owner)))
2346 goto nla_put_failure;
2347 if (gid_valid(tun->group) &&
2348 nla_put_u32(skb, IFLA_TUN_GROUP,
2349 from_kgid_munged(current_user_ns(), tun->group)))
2350 goto nla_put_failure;
2351 if (nla_put_u8(skb, IFLA_TUN_PI, !(tun->flags & IFF_NO_PI)))
2352 goto nla_put_failure;
2353 if (nla_put_u8(skb, IFLA_TUN_VNET_HDR, !!(tun->flags & IFF_VNET_HDR)))
2354 goto nla_put_failure;
2355 if (nla_put_u8(skb, IFLA_TUN_PERSIST, !!(tun->flags & IFF_PERSIST)))
2356 goto nla_put_failure;
2357 if (nla_put_u8(skb, IFLA_TUN_MULTI_QUEUE,
2358 !!(tun->flags & IFF_MULTI_QUEUE)))
2359 goto nla_put_failure;
2360 if (tun->flags & IFF_MULTI_QUEUE) {
2361 if (nla_put_u32(skb, IFLA_TUN_NUM_QUEUES, tun->numqueues))
2362 goto nla_put_failure;
2363 if (nla_put_u32(skb, IFLA_TUN_NUM_DISABLED_QUEUES,
2365 goto nla_put_failure;
2374 static struct rtnl_link_ops tun_link_ops __read_mostly = {
2376 .priv_size = sizeof(struct tun_struct),
2378 .validate = tun_validate,
2379 .get_size = tun_get_size,
2380 .fill_info = tun_fill_info,
2383 static void tun_sock_write_space(struct sock *sk)
2385 struct tun_file *tfile;
2386 wait_queue_head_t *wqueue;
2388 if (!sock_writeable(sk))
2391 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
2394 wqueue = sk_sleep(sk);
2395 if (wqueue && waitqueue_active(wqueue))
2396 wake_up_interruptible_sync_poll(wqueue, EPOLLOUT |
2397 EPOLLWRNORM | EPOLLWRBAND);
2399 tfile = container_of(sk, struct tun_file, sk);
2400 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
2403 static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
2406 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
2407 struct tun_struct *tun = tun_get(tfile);
2412 ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter,
2413 m->msg_flags & MSG_DONTWAIT,
2414 m->msg_flags & MSG_MORE);
2419 static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
2422 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
2423 struct tun_struct *tun = tun_get(tfile);
2424 void *ptr = m->msg_control;
2432 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
2436 if (flags & MSG_ERRQUEUE) {
2437 ret = sock_recv_errqueue(sock->sk, m, total_len,
2438 SOL_PACKET, TUN_TX_TIMESTAMP);
2441 ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, ptr);
2442 if (ret > (ssize_t)total_len) {
2443 m->msg_flags |= MSG_TRUNC;
2444 ret = flags & MSG_TRUNC ? ret : total_len;
2457 static int tun_ptr_peek_len(void *ptr)
2460 if (tun_is_xdp_frame(ptr)) {
2461 struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
2465 return __skb_array_len_with_tag(ptr);
2471 static int tun_peek_len(struct socket *sock)
2473 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
2474 struct tun_struct *tun;
2477 tun = tun_get(tfile);
2481 ret = PTR_RING_PEEK_CALL(&tfile->tx_ring, tun_ptr_peek_len);
2487 /* Ops structure to mimic raw sockets with tun */
2488 static const struct proto_ops tun_socket_ops = {
2489 .peek_len = tun_peek_len,
2490 .sendmsg = tun_sendmsg,
2491 .recvmsg = tun_recvmsg,
2494 static struct proto tun_proto = {
2496 .owner = THIS_MODULE,
2497 .obj_size = sizeof(struct tun_file),
2500 static int tun_flags(struct tun_struct *tun)
2502 return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
2505 static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
2508 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
2509 return sprintf(buf, "0x%x\n", tun_flags(tun));
2512 static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
2515 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
2516 return uid_valid(tun->owner)?
2517 sprintf(buf, "%u\n",
2518 from_kuid_munged(current_user_ns(), tun->owner)):
2519 sprintf(buf, "-1\n");
2522 static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
2525 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
2526 return gid_valid(tun->group) ?
2527 sprintf(buf, "%u\n",
2528 from_kgid_munged(current_user_ns(), tun->group)):
2529 sprintf(buf, "-1\n");
2532 static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
2533 static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
2534 static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
2536 static struct attribute *tun_dev_attrs[] = {
2537 &dev_attr_tun_flags.attr,
2538 &dev_attr_owner.attr,
2539 &dev_attr_group.attr,
2543 static const struct attribute_group tun_attr_group = {
2544 .attrs = tun_dev_attrs
2547 static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
2549 struct tun_struct *tun;
2550 struct tun_file *tfile = file->private_data;
2551 struct net_device *dev;
2554 if (tfile->detached)
2557 if ((ifr->ifr_flags & IFF_NAPI_FRAGS)) {
2558 if (!capable(CAP_NET_ADMIN))
2561 if (!(ifr->ifr_flags & IFF_NAPI) ||
2562 (ifr->ifr_flags & TUN_TYPE_MASK) != IFF_TAP)
2566 dev = __dev_get_by_name(net, ifr->ifr_name);
2568 if (ifr->ifr_flags & IFF_TUN_EXCL)
2570 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
2571 tun = netdev_priv(dev);
2572 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
2573 tun = netdev_priv(dev);
2577 if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
2578 !!(tun->flags & IFF_MULTI_QUEUE))
2581 if (tun_not_capable(tun))
2583 err = security_tun_dev_open(tun->security);
2587 err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER,
2588 ifr->ifr_flags & IFF_NAPI,
2589 ifr->ifr_flags & IFF_NAPI_FRAGS, true);
2593 if (tun->flags & IFF_MULTI_QUEUE &&
2594 (tun->numqueues + tun->numdisabled > 1)) {
2595 /* One or more queue has already been attached, no need
2596 * to initialize the device again.
2598 netdev_state_change(dev);
2602 tun->flags = (tun->flags & ~TUN_FEATURES) |
2603 (ifr->ifr_flags & TUN_FEATURES);
2605 netdev_state_change(dev);
2608 unsigned long flags = 0;
2609 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
2612 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2614 err = security_tun_dev_create();
2619 if (ifr->ifr_flags & IFF_TUN) {
2623 } else if (ifr->ifr_flags & IFF_TAP) {
2631 name = ifr->ifr_name;
2633 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
2634 NET_NAME_UNKNOWN, tun_setup, queues,
2639 err = dev_get_valid_name(net, dev, name);
2643 dev_net_set(dev, net);
2644 dev->rtnl_link_ops = &tun_link_ops;
2645 dev->ifindex = tfile->ifindex;
2646 dev->sysfs_groups[0] = &tun_attr_group;
2648 tun = netdev_priv(dev);
2651 tun->txflt.count = 0;
2652 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
2654 tun->align = NET_SKB_PAD;
2655 tun->filter_attached = false;
2656 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
2657 tun->rx_batched = 0;
2658 RCU_INIT_POINTER(tun->steering_prog, NULL);
2660 tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats);
2661 if (!tun->pcpu_stats) {
2666 spin_lock_init(&tun->lock);
2668 err = security_tun_dev_alloc_security(&tun->security);
2675 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
2676 TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
2677 NETIF_F_HW_VLAN_STAG_TX;
2678 dev->features = dev->hw_features | NETIF_F_LLTX;
2679 dev->vlan_features = dev->features &
2680 ~(NETIF_F_HW_VLAN_CTAG_TX |
2681 NETIF_F_HW_VLAN_STAG_TX);
2683 tun->flags = (tun->flags & ~TUN_FEATURES) |
2684 (ifr->ifr_flags & TUN_FEATURES);
2686 INIT_LIST_HEAD(&tun->disabled);
2687 err = tun_attach(tun, file, false, ifr->ifr_flags & IFF_NAPI,
2688 ifr->ifr_flags & IFF_NAPI_FRAGS, false);
2692 err = register_netdevice(tun->dev);
2695 /* free_netdev() won't check refcnt, to aovid race
2696 * with dev_put() we need publish tun after registration.
2698 rcu_assign_pointer(tfile->tun, tun);
2701 netif_carrier_on(tun->dev);
2703 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
2705 /* Make sure persistent devices do not get stuck in
2708 if (netif_running(tun->dev))
2709 netif_tx_wake_all_queues(tun->dev);
2711 strcpy(ifr->ifr_name, tun->dev->name);
2715 tun_detach_all(dev);
2716 /* register_netdevice() already called tun_free_netdev() */
2720 tun_flow_uninit(tun);
2721 security_tun_dev_free_security(tun->security);
2723 free_percpu(tun->pcpu_stats);
2729 static void tun_get_iff(struct net *net, struct tun_struct *tun,
2732 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
2734 strcpy(ifr->ifr_name, tun->dev->name);
2736 ifr->ifr_flags = tun_flags(tun);
2740 /* This is like a cut-down ethtool ops, except done via tun fd so no
2741 * privs required. */
2742 static int set_offload(struct tun_struct *tun, unsigned long arg)
2744 netdev_features_t features = 0;
2746 if (arg & TUN_F_CSUM) {
2747 features |= NETIF_F_HW_CSUM;
2750 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
2751 if (arg & TUN_F_TSO_ECN) {
2752 features |= NETIF_F_TSO_ECN;
2753 arg &= ~TUN_F_TSO_ECN;
2755 if (arg & TUN_F_TSO4)
2756 features |= NETIF_F_TSO;
2757 if (arg & TUN_F_TSO6)
2758 features |= NETIF_F_TSO6;
2759 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
2765 /* This gives the user a way to test for new features in future by
2766 * trying to set them. */
2770 tun->set_features = features;
2771 tun->dev->wanted_features &= ~TUN_USER_FEATURES;
2772 tun->dev->wanted_features |= features;
2773 netdev_update_features(tun->dev);
2778 static void tun_detach_filter(struct tun_struct *tun, int n)
2781 struct tun_file *tfile;
2783 for (i = 0; i < n; i++) {
2784 tfile = rtnl_dereference(tun->tfiles[i]);
2785 lock_sock(tfile->socket.sk);
2786 sk_detach_filter(tfile->socket.sk);
2787 release_sock(tfile->socket.sk);
2790 tun->filter_attached = false;
2793 static int tun_attach_filter(struct tun_struct *tun)
2796 struct tun_file *tfile;
2798 for (i = 0; i < tun->numqueues; i++) {
2799 tfile = rtnl_dereference(tun->tfiles[i]);
2800 lock_sock(tfile->socket.sk);
2801 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
2802 release_sock(tfile->socket.sk);
2804 tun_detach_filter(tun, i);
2809 tun->filter_attached = true;
2813 static void tun_set_sndbuf(struct tun_struct *tun)
2815 struct tun_file *tfile;
2818 for (i = 0; i < tun->numqueues; i++) {
2819 tfile = rtnl_dereference(tun->tfiles[i]);
2820 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
2824 static int tun_set_queue(struct file *file, struct ifreq *ifr)
2826 struct tun_file *tfile = file->private_data;
2827 struct tun_struct *tun;
2832 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
2833 tun = tfile->detached;
2838 ret = security_tun_dev_attach_queue(tun->security);
2841 ret = tun_attach(tun, file, false, tun->flags & IFF_NAPI,
2842 tun->flags & IFF_NAPI_FRAGS, true);
2843 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
2844 tun = rtnl_dereference(tfile->tun);
2845 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
2848 __tun_detach(tfile, false);
2853 netdev_state_change(tun->dev);
2860 static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog **prog_p,
2863 struct bpf_prog *prog;
2866 if (copy_from_user(&fd, data, sizeof(fd)))
2872 prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
2874 return PTR_ERR(prog);
2877 return __tun_set_ebpf(tun, prog_p, prog);
2880 /* Return correct value for tun->dev->addr_len based on tun->dev->type. */
2881 static unsigned char tun_get_addr_len(unsigned short type)
2885 case ARPHRD_TUNNEL6:
2886 return sizeof(struct in6_addr);
2893 case ARPHRD_IEEE802154:
2894 case ARPHRD_IEEE802154_MONITOR:
2895 return IEEE802154_EXTENDED_ADDR_LEN;
2896 case ARPHRD_PHONET_PIPE:
2900 case ARPHRD_6LOWPAN:
2901 return EUI64_ADDR_LEN;
2906 case ARPHRD_IEEE802:
2909 return ROSE_ADDR_LEN;
2911 return AX25_ADDR_LEN;
2912 case ARPHRD_LOCALTLK:
2919 static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
2920 unsigned long arg, int ifreq_len)
2922 struct tun_file *tfile = file->private_data;
2923 struct net *net = sock_net(&tfile->sk);
2924 struct tun_struct *tun;
2925 void __user* argp = (void __user*)arg;
2931 unsigned int ifindex;
2934 bool do_notify = false;
2936 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE ||
2937 (_IOC_TYPE(cmd) == SOCK_IOC_TYPE && cmd != SIOCGSKNS)) {
2938 if (copy_from_user(&ifr, argp, ifreq_len))
2941 memset(&ifr, 0, sizeof(ifr));
2943 if (cmd == TUNGETFEATURES) {
2944 /* Currently this just means: "what IFF flags are valid?".
2945 * This is needed because we never checked for invalid flags on
2948 return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
2949 (unsigned int __user*)argp);
2950 } else if (cmd == TUNSETQUEUE) {
2951 return tun_set_queue(file, &ifr);
2952 } else if (cmd == SIOCGSKNS) {
2953 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2955 return open_related_ns(&net->ns, get_net_ns);
2961 tun = tun_get(tfile);
2962 if (cmd == TUNSETIFF) {
2967 ifr.ifr_name[IFNAMSIZ-1] = '\0';
2969 ret = tun_set_iff(net, file, &ifr);
2974 if (copy_to_user(argp, &ifr, ifreq_len))
2978 if (cmd == TUNSETIFINDEX) {
2984 if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
2988 tfile->ifindex = ifindex;
2996 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
3001 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
3003 if (tfile->detached)
3004 ifr.ifr_flags |= IFF_DETACH_QUEUE;
3005 if (!tfile->socket.sk->sk_filter)
3006 ifr.ifr_flags |= IFF_NOFILTER;
3008 if (copy_to_user(argp, &ifr, ifreq_len))
3013 /* Disable/Enable checksum */
3015 /* [unimplemented] */
3016 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
3017 arg ? "disabled" : "enabled");
3021 /* Disable/Enable persist mode. Keep an extra reference to the
3022 * module to prevent the module being unprobed.
3024 if (arg && !(tun->flags & IFF_PERSIST)) {
3025 tun->flags |= IFF_PERSIST;
3026 __module_get(THIS_MODULE);
3029 if (!arg && (tun->flags & IFF_PERSIST)) {
3030 tun->flags &= ~IFF_PERSIST;
3031 module_put(THIS_MODULE);
3035 tun_debug(KERN_INFO, tun, "persist %s\n",
3036 arg ? "enabled" : "disabled");
3040 /* Set owner of the device */
3041 owner = make_kuid(current_user_ns(), arg);
3042 if (!uid_valid(owner)) {
3048 tun_debug(KERN_INFO, tun, "owner set to %u\n",
3049 from_kuid(&init_user_ns, tun->owner));
3053 /* Set group of the device */
3054 group = make_kgid(current_user_ns(), arg);
3055 if (!gid_valid(group)) {
3061 tun_debug(KERN_INFO, tun, "group set to %u\n",
3062 from_kgid(&init_user_ns, tun->group));
3066 /* Only allow setting the type when the interface is down */
3067 if (tun->dev->flags & IFF_UP) {
3068 tun_debug(KERN_INFO, tun,
3069 "Linktype set failed because interface is up\n");
3072 tun->dev->type = (int) arg;
3073 tun->dev->addr_len = tun_get_addr_len(tun->dev->type);
3074 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
3086 ret = set_offload(tun, arg);
3089 case TUNSETTXFILTER:
3090 /* Can be set only for TAPs */
3092 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
3094 ret = update_filter(&tun->txflt, (void __user *)arg);
3098 /* Get hw address */
3099 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
3100 ifr.ifr_hwaddr.sa_family = tun->dev->type;
3101 if (copy_to_user(argp, &ifr, ifreq_len))
3106 /* Set hw address */
3107 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
3108 ifr.ifr_hwaddr.sa_data);
3110 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
3114 sndbuf = tfile->socket.sk->sk_sndbuf;
3115 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
3120 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
3129 tun->sndbuf = sndbuf;
3130 tun_set_sndbuf(tun);
3133 case TUNGETVNETHDRSZ:
3134 vnet_hdr_sz = tun->vnet_hdr_sz;
3135 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
3139 case TUNSETVNETHDRSZ:
3140 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
3144 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
3149 tun->vnet_hdr_sz = vnet_hdr_sz;
3153 le = !!(tun->flags & TUN_VNET_LE);
3154 if (put_user(le, (int __user *)argp))
3159 if (get_user(le, (int __user *)argp)) {
3164 tun->flags |= TUN_VNET_LE;
3166 tun->flags &= ~TUN_VNET_LE;
3170 ret = tun_get_vnet_be(tun, argp);
3174 ret = tun_set_vnet_be(tun, argp);
3177 case TUNATTACHFILTER:
3178 /* Can be set only for TAPs */
3180 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
3183 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
3186 ret = tun_attach_filter(tun);
3189 case TUNDETACHFILTER:
3190 /* Can be set only for TAPs */
3192 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
3195 tun_detach_filter(tun, tun->numqueues);
3200 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
3203 if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
3208 case TUNSETSTEERINGEBPF:
3209 ret = tun_set_ebpf(tun, &tun->steering_prog, argp);
3212 case TUNSETFILTEREBPF:
3213 ret = tun_set_ebpf(tun, &tun->filter_prog, argp);
3222 netdev_state_change(tun->dev);
3231 static long tun_chr_ioctl(struct file *file,
3232 unsigned int cmd, unsigned long arg)
3234 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
3237 #ifdef CONFIG_COMPAT
3238 static long tun_chr_compat_ioctl(struct file *file,
3239 unsigned int cmd, unsigned long arg)
3244 case TUNSETTXFILTER:
3249 arg = (unsigned long)compat_ptr(arg);
3252 arg = (compat_ulong_t)arg;
3257 * compat_ifreq is shorter than ifreq, so we must not access beyond
3258 * the end of that structure. All fields that are used in this
3259 * driver are compatible though, we don't need to convert the
3262 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
3264 #endif /* CONFIG_COMPAT */
3266 static int tun_chr_fasync(int fd, struct file *file, int on)
3268 struct tun_file *tfile = file->private_data;
3271 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
3275 __f_setown(file, task_pid(current), PIDTYPE_TGID, 0);
3276 tfile->flags |= TUN_FASYNC;
3278 tfile->flags &= ~TUN_FASYNC;
3284 static int tun_chr_open(struct inode *inode, struct file * file)
3286 struct net *net = current->nsproxy->net_ns;
3287 struct tun_file *tfile;
3289 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
3291 tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
3295 if (ptr_ring_init(&tfile->tx_ring, 0, GFP_KERNEL)) {
3296 sk_free(&tfile->sk);
3300 mutex_init(&tfile->napi_mutex);
3301 RCU_INIT_POINTER(tfile->tun, NULL);
3305 init_waitqueue_head(&tfile->wq.wait);
3306 RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq);
3308 tfile->socket.file = file;
3309 tfile->socket.ops = &tun_socket_ops;
3311 sock_init_data(&tfile->socket, &tfile->sk);
3313 tfile->sk.sk_write_space = tun_sock_write_space;
3314 tfile->sk.sk_sndbuf = INT_MAX;
3316 file->private_data = tfile;
3317 INIT_LIST_HEAD(&tfile->next);
3319 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
3324 static int tun_chr_close(struct inode *inode, struct file *file)
3326 struct tun_file *tfile = file->private_data;
3328 tun_detach(tfile, true);
3333 #ifdef CONFIG_PROC_FS
3334 static void tun_chr_show_fdinfo(struct seq_file *m, struct file *file)
3336 struct tun_file *tfile = file->private_data;
3337 struct tun_struct *tun;
3340 memset(&ifr, 0, sizeof(ifr));
3343 tun = tun_get(tfile);
3345 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
3351 seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
3355 static const struct file_operations tun_fops = {
3356 .owner = THIS_MODULE,
3357 .llseek = no_llseek,
3358 .read_iter = tun_chr_read_iter,
3359 .write_iter = tun_chr_write_iter,
3360 .poll = tun_chr_poll,
3361 .unlocked_ioctl = tun_chr_ioctl,
3362 #ifdef CONFIG_COMPAT
3363 .compat_ioctl = tun_chr_compat_ioctl,
3365 .open = tun_chr_open,
3366 .release = tun_chr_close,
3367 .fasync = tun_chr_fasync,
3368 #ifdef CONFIG_PROC_FS
3369 .show_fdinfo = tun_chr_show_fdinfo,
3373 static struct miscdevice tun_miscdev = {
3376 .nodename = "net/tun",
3380 /* ethtool interface */
3382 static void tun_default_link_ksettings(struct net_device *dev,
3383 struct ethtool_link_ksettings *cmd)
3385 ethtool_link_ksettings_zero_link_mode(cmd, supported);
3386 ethtool_link_ksettings_zero_link_mode(cmd, advertising);
3387 cmd->base.speed = SPEED_10;
3388 cmd->base.duplex = DUPLEX_FULL;
3389 cmd->base.port = PORT_TP;
3390 cmd->base.phy_address = 0;
3391 cmd->base.autoneg = AUTONEG_DISABLE;
3394 static int tun_get_link_ksettings(struct net_device *dev,
3395 struct ethtool_link_ksettings *cmd)
3397 struct tun_struct *tun = netdev_priv(dev);
3399 memcpy(cmd, &tun->link_ksettings, sizeof(*cmd));
3403 static int tun_set_link_ksettings(struct net_device *dev,
3404 const struct ethtool_link_ksettings *cmd)
3406 struct tun_struct *tun = netdev_priv(dev);
3408 memcpy(&tun->link_ksettings, cmd, sizeof(*cmd));
3412 static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
3414 struct tun_struct *tun = netdev_priv(dev);
3416 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
3417 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
3419 switch (tun->flags & TUN_TYPE_MASK) {
3421 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
3424 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
3429 static u32 tun_get_msglevel(struct net_device *dev)
3432 struct tun_struct *tun = netdev_priv(dev);
3439 static void tun_set_msglevel(struct net_device *dev, u32 value)
3442 struct tun_struct *tun = netdev_priv(dev);
3447 static int tun_get_coalesce(struct net_device *dev,
3448 struct ethtool_coalesce *ec)
3450 struct tun_struct *tun = netdev_priv(dev);
3452 ec->rx_max_coalesced_frames = tun->rx_batched;
3457 static int tun_set_coalesce(struct net_device *dev,
3458 struct ethtool_coalesce *ec)
3460 struct tun_struct *tun = netdev_priv(dev);
3462 if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT)
3463 tun->rx_batched = NAPI_POLL_WEIGHT;
3465 tun->rx_batched = ec->rx_max_coalesced_frames;
3470 static const struct ethtool_ops tun_ethtool_ops = {
3471 .get_drvinfo = tun_get_drvinfo,
3472 .get_msglevel = tun_get_msglevel,
3473 .set_msglevel = tun_set_msglevel,
3474 .get_link = ethtool_op_get_link,
3475 .get_ts_info = ethtool_op_get_ts_info,
3476 .get_coalesce = tun_get_coalesce,
3477 .set_coalesce = tun_set_coalesce,
3478 .get_link_ksettings = tun_get_link_ksettings,
3479 .set_link_ksettings = tun_set_link_ksettings,
3482 static int tun_queue_resize(struct tun_struct *tun)
3484 struct net_device *dev = tun->dev;
3485 struct tun_file *tfile;
3486 struct ptr_ring **rings;
3487 int n = tun->numqueues + tun->numdisabled;
3490 rings = kmalloc_array(n, sizeof(*rings), GFP_KERNEL);
3494 for (i = 0; i < tun->numqueues; i++) {
3495 tfile = rtnl_dereference(tun->tfiles[i]);
3496 rings[i] = &tfile->tx_ring;
3498 list_for_each_entry(tfile, &tun->disabled, next)
3499 rings[i++] = &tfile->tx_ring;
3501 ret = ptr_ring_resize_multiple(rings, n,
3502 dev->tx_queue_len, GFP_KERNEL,
3509 static int tun_device_event(struct notifier_block *unused,
3510 unsigned long event, void *ptr)
3512 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3513 struct tun_struct *tun = netdev_priv(dev);
3516 if (dev->rtnl_link_ops != &tun_link_ops)
3520 case NETDEV_CHANGE_TX_QUEUE_LEN:
3521 if (tun_queue_resize(tun))
3525 for (i = 0; i < tun->numqueues; i++) {
3526 struct tun_file *tfile;
3528 tfile = rtnl_dereference(tun->tfiles[i]);
3529 tfile->socket.sk->sk_write_space(tfile->socket.sk);
3539 static struct notifier_block tun_notifier_block __read_mostly = {
3540 .notifier_call = tun_device_event,
3543 static int __init tun_init(void)
3547 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
3549 ret = rtnl_link_register(&tun_link_ops);
3551 pr_err("Can't register link_ops\n");
3555 ret = misc_register(&tun_miscdev);
3557 pr_err("Can't register misc device %d\n", TUN_MINOR);
3561 ret = register_netdevice_notifier(&tun_notifier_block);
3563 pr_err("Can't register netdevice notifier\n");
3570 misc_deregister(&tun_miscdev);
3572 rtnl_link_unregister(&tun_link_ops);
3577 static void tun_cleanup(void)
3579 misc_deregister(&tun_miscdev);
3580 rtnl_link_unregister(&tun_link_ops);
3581 unregister_netdevice_notifier(&tun_notifier_block);
3584 /* Get an underlying socket object from tun file. Returns error unless file is
3585 * attached to a device. The returned object works like a packet socket, it
3586 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
3587 * holding a reference to the file for as long as the socket is in use. */
3588 struct socket *tun_get_socket(struct file *file)
3590 struct tun_file *tfile;
3591 if (file->f_op != &tun_fops)
3592 return ERR_PTR(-EINVAL);
3593 tfile = file->private_data;
3595 return ERR_PTR(-EBADFD);
3596 return &tfile->socket;
3598 EXPORT_SYMBOL_GPL(tun_get_socket);
3600 struct ptr_ring *tun_get_tx_ring(struct file *file)
3602 struct tun_file *tfile;
3604 if (file->f_op != &tun_fops)
3605 return ERR_PTR(-EINVAL);
3606 tfile = file->private_data;
3608 return ERR_PTR(-EBADFD);
3609 return &tfile->tx_ring;
3611 EXPORT_SYMBOL_GPL(tun_get_tx_ring);
3613 module_init(tun_init);
3614 module_exit(tun_cleanup);
3615 MODULE_DESCRIPTION(DRV_DESCRIPTION);
3616 MODULE_AUTHOR(DRV_COPYRIGHT);
3617 MODULE_LICENSE("GPL");
3618 MODULE_ALIAS_MISCDEV(TUN_MINOR);
3619 MODULE_ALIAS("devname:net/tun");