1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * TUN - Universal TUN/TAP device driver. 41da177e4SLinus Torvalds * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com> 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $ 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds /* 101da177e4SLinus Torvalds * Changes: 111da177e4SLinus Torvalds * 12ff4cc3acSMike Kershaw * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14 13ff4cc3acSMike Kershaw * Add TUNSETLINK ioctl to set the link encapsulation 14ff4cc3acSMike Kershaw * 151da177e4SLinus Torvalds * Mark Smith <markzzzsmith@yahoo.com.au> 16344dc8edSJoe Perches * Use eth_random_addr() for tap MAC address. 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20 191da177e4SLinus Torvalds * Fixes in packet dropping, queue length setting and queue wakeup. 201da177e4SLinus Torvalds * Increased default tx queue length. 211da177e4SLinus Torvalds * Added ethtool API. 221da177e4SLinus Torvalds * Minor cleanups 231da177e4SLinus Torvalds * 241da177e4SLinus Torvalds * Daniel Podlejski <underley@underley.eu.org> 251da177e4SLinus Torvalds * Modifications for 2.3.99-pre5 kernel. 261da177e4SLinus Torvalds */ 271da177e4SLinus Torvalds 286b8a66eeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 296b8a66eeSJoe Perches 301da177e4SLinus Torvalds #define DRV_NAME "tun" 311da177e4SLinus Torvalds #define DRV_VERSION "1.6" 321da177e4SLinus Torvalds #define DRV_DESCRIPTION "Universal TUN/TAP device driver" 331da177e4SLinus Torvalds #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>" 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds #include <linux/module.h> 361da177e4SLinus Torvalds #include <linux/errno.h> 371da177e4SLinus Torvalds #include <linux/kernel.h> 38174cd4b1SIngo Molnar #include <linux/sched/signal.h> 391da177e4SLinus Torvalds #include <linux/major.h> 401da177e4SLinus Torvalds #include <linux/slab.h> 411da177e4SLinus Torvalds #include <linux/poll.h> 421da177e4SLinus Torvalds #include <linux/fcntl.h> 431da177e4SLinus Torvalds #include <linux/init.h> 441da177e4SLinus Torvalds #include <linux/skbuff.h> 451da177e4SLinus Torvalds #include <linux/netdevice.h> 461da177e4SLinus Torvalds #include <linux/etherdevice.h> 471da177e4SLinus Torvalds #include <linux/miscdevice.h> 481da177e4SLinus Torvalds #include <linux/ethtool.h> 491da177e4SLinus Torvalds #include <linux/rtnetlink.h> 5050857e2aSArnd Bergmann #include <linux/compat.h> 511da177e4SLinus Torvalds #include <linux/if.h> 521da177e4SLinus Torvalds #include <linux/if_arp.h> 531da177e4SLinus Torvalds #include <linux/if_ether.h> 541da177e4SLinus Torvalds #include <linux/if_tun.h> 556680ec68SJason Wang #include <linux/if_vlan.h> 561da177e4SLinus Torvalds #include <linux/crc32.h> 57d647a591SPavel Emelyanov #include <linux/nsproxy.h> 58f43798c2SRusty Russell #include <linux/virtio_net.h> 5999405162SMichael S. Tsirkin #include <linux/rcupdate.h> 60881d966bSEric W. Biederman #include <net/net_namespace.h> 6179d17604SPavel Emelyanov #include <net/netns/generic.h> 62f019a7a5SEric W. Biederman #include <net/rtnetlink.h> 6333dccbb0SHerbert Xu #include <net/sock.h> 64735fc405SJesper Dangaard Brouer #include <net/xdp.h> 6593e14b6dSMasatake YAMATO #include <linux/seq_file.h> 66e0b46d0eSHerbert Xu #include <linux/uio.h> 671576d986SJason Wang #include <linux/skb_array.h> 68761876c8SJason Wang #include <linux/bpf.h> 69761876c8SJason Wang #include <linux/bpf_trace.h> 7090e33d45SPetar Penkov #include <linux/mutex.h> 711da177e4SLinus Torvalds 727c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 73f2780d6dSKirill Tkhai #include <linux/proc_fs.h> 741da177e4SLinus Torvalds 754e24f2ddSChas Williams static void tun_default_link_ksettings(struct net_device *dev, 764e24f2ddSChas Williams struct ethtool_link_ksettings *cmd); 774e24f2ddSChas Williams 787df13219SJason Wang #define TUN_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD) 7966ccbc9cSJason Wang 80031f5e03SMichael S. Tsirkin /* TUN device flags */ 81031f5e03SMichael S. Tsirkin 82031f5e03SMichael S. Tsirkin /* IFF_ATTACH_QUEUE is never stored in device flags, 83031f5e03SMichael S. Tsirkin * overload it to mean fasync when stored there. 84031f5e03SMichael S. Tsirkin */ 85031f5e03SMichael S. Tsirkin #define TUN_FASYNC IFF_ATTACH_QUEUE 861cf8e410SMichael S. Tsirkin /* High bits in flags field are unused. */ 871cf8e410SMichael S. Tsirkin #define TUN_VNET_LE 0x80000000 888b8e658bSGreg Kurz #define TUN_VNET_BE 0x40000000 89031f5e03SMichael S. Tsirkin 90031f5e03SMichael S. Tsirkin #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \ 9190e33d45SPetar Penkov IFF_MULTI_QUEUE | IFF_NAPI | IFF_NAPI_FRAGS) 9290e33d45SPetar Penkov 930690899bSMichael S. Tsirkin #define GOODCOPY_LEN 128 940690899bSMichael S. Tsirkin 95f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8 96f271b2ccSMax Krasnyansky struct tap_filter { 97f271b2ccSMax Krasnyansky unsigned int count; /* Number of addrs. Zero means disabled */ 98f271b2ccSMax Krasnyansky u32 mask[2]; /* Mask of the hashed addrs */ 99f271b2ccSMax Krasnyansky unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN]; 100f271b2ccSMax Krasnyansky }; 101f271b2ccSMax Krasnyansky 102baf71c5cSPankaj Gupta /* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal 103baf71c5cSPankaj Gupta * to max number of VCPUs in guest. */ 104baf71c5cSPankaj Gupta #define MAX_TAP_QUEUES 256 105b8732fb7SJason Wang #define MAX_TAP_FLOWS 4096 106c8d68e6bSJason Wang 10796442e42SJason Wang #define TUN_FLOW_EXPIRE (3 * HZ) 10896442e42SJason Wang 109608b9977SPaolo Abeni struct tun_pcpu_stats { 1105260dd3eSEric Dumazet u64_stats_t rx_packets; 1115260dd3eSEric Dumazet u64_stats_t rx_bytes; 1125260dd3eSEric Dumazet u64_stats_t tx_packets; 1135260dd3eSEric Dumazet u64_stats_t tx_bytes; 114608b9977SPaolo Abeni struct u64_stats_sync syncp; 115608b9977SPaolo Abeni u32 rx_dropped; 116608b9977SPaolo Abeni u32 tx_dropped; 117608b9977SPaolo Abeni u32 rx_frame_errors; 118608b9977SPaolo Abeni }; 119608b9977SPaolo Abeni 12054f968d6SJason Wang /* A tun_file connects an open character device to a tuntap netdevice. It 12192d4ea6eSstephen hemminger * also contains all socket related structures (except sock_fprog and tap_filter) 12254f968d6SJason Wang * to serve as one transmit queue for tuntap device. The sock_fprog and 12354f968d6SJason Wang * tap_filter were kept in tun_struct since they were used for filtering for the 12436fe8c09SRami Rosen * netdevice not for a specific queue (at least I didn't see the requirement for 12554f968d6SJason Wang * this). 1266e914fc7SJason Wang * 1276e914fc7SJason Wang * RCU usage: 12836fe8c09SRami Rosen * The tun_file and tun_struct are loosely coupled, the pointer from one to the 1296e914fc7SJason Wang * other can only be read while rcu_read_lock or rtnl_lock is held. 13054f968d6SJason Wang */ 131631ab46bSEric W. Biederman struct tun_file { 13254f968d6SJason Wang struct sock sk; 13354f968d6SJason Wang struct socket socket; 1346e914fc7SJason Wang struct tun_struct __rcu *tun; 13554f968d6SJason Wang struct fasync_struct *fasync; 13654f968d6SJason Wang /* only used for fasnyc */ 13754f968d6SJason Wang unsigned int flags; 138fb7589a1SPavel Emelyanov union { 139c8d68e6bSJason Wang u16 queue_index; 140fb7589a1SPavel Emelyanov unsigned int ifindex; 141fb7589a1SPavel Emelyanov }; 14294317099SPetar Penkov struct napi_struct napi; 143aec72f33SEric Dumazet bool napi_enabled; 144af3fb24eSEric Dumazet bool napi_frags_enabled; 14590e33d45SPetar Penkov struct mutex napi_mutex; /* Protects access to the above napi */ 1464008e97fSJason Wang struct list_head next; 1474008e97fSJason Wang struct tun_struct *detached; 1485990a305SJason Wang struct ptr_ring tx_ring; 1498bf5c4eeSJesper Dangaard Brouer struct xdp_rxq_info xdp_rxq; 150631ab46bSEric W. Biederman }; 151631ab46bSEric W. Biederman 152f9e06c45SJason Wang struct tun_page { 153f9e06c45SJason Wang struct page *page; 154f9e06c45SJason Wang int count; 155f9e06c45SJason Wang }; 156f9e06c45SJason Wang 15796442e42SJason Wang struct tun_flow_entry { 15896442e42SJason Wang struct hlist_node hash_link; 15996442e42SJason Wang struct rcu_head rcu; 16096442e42SJason Wang struct tun_struct *tun; 16196442e42SJason Wang 16296442e42SJason Wang u32 rxhash; 1639bc88939STom Herbert u32 rps_rxhash; 16496442e42SJason Wang int queue_index; 16583b1bc12SLi RongQing unsigned long updated ____cacheline_aligned_in_smp; 16696442e42SJason Wang }; 16796442e42SJason Wang 16896442e42SJason Wang #define TUN_NUM_FLOW_ENTRIES 1024 169f13b5468SLi RongQing #define TUN_MASK_FLOW_ENTRIES (TUN_NUM_FLOW_ENTRIES - 1) 17096442e42SJason Wang 171cd5681d7SJason Wang struct tun_prog { 17296f84061SJason Wang struct rcu_head rcu; 17396f84061SJason Wang struct bpf_prog *prog; 17496f84061SJason Wang }; 17596f84061SJason Wang 17654f968d6SJason Wang /* Since the socket were moved to tun_file, to preserve the behavior of persist 17736fe8c09SRami Rosen * device, socket filter, sndbuf and vnet header size were restore when the 17854f968d6SJason Wang * file were attached to a persist device. 17954f968d6SJason Wang */ 18014daa021SRusty Russell struct tun_struct { 181c8d68e6bSJason Wang struct tun_file __rcu *tfiles[MAX_TAP_QUEUES]; 182c8d68e6bSJason Wang unsigned int numqueues; 183f271b2ccSMax Krasnyansky unsigned int flags; 1840625c883SEric W. Biederman kuid_t owner; 1850625c883SEric W. Biederman kgid_t group; 18614daa021SRusty Russell 18714daa021SRusty Russell struct net_device *dev; 188c8f44affSMichał Mirosław netdev_features_t set_features; 18988255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \ 190d591a1f3SDavid S. Miller NETIF_F_TSO6) 191d9d52b51SMichael S. Tsirkin 192eaea34b2SPaolo Abeni int align; 193d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 19454f968d6SJason Wang int sndbuf; 19554f968d6SJason Wang struct tap_filter txflt; 19654f968d6SJason Wang struct sock_fprog fprog; 19754f968d6SJason Wang /* protected by rtnl lock */ 19854f968d6SJason Wang bool filter_attached; 1993424170fSMichal Kubecek u32 msg_enable; 20096442e42SJason Wang spinlock_t lock; 20196442e42SJason Wang struct hlist_head flows[TUN_NUM_FLOW_ENTRIES]; 20296442e42SJason Wang struct timer_list flow_gc_timer; 20396442e42SJason Wang unsigned long ageing_time; 2044008e97fSJason Wang unsigned int numdisabled; 2054008e97fSJason Wang struct list_head disabled; 2065dbbaf2dSPaul Moore void *security; 207b8732fb7SJason Wang u32 flow_count; 2085503fcecSJason Wang u32 rx_batched; 209608b9977SPaolo Abeni struct tun_pcpu_stats __percpu *pcpu_stats; 210761876c8SJason Wang struct bpf_prog __rcu *xdp_prog; 211cd5681d7SJason Wang struct tun_prog __rcu *steering_prog; 212aff3d70aSJason Wang struct tun_prog __rcu *filter_prog; 2134e24f2ddSChas Williams struct ethtool_link_ksettings link_ksettings; 21414daa021SRusty Russell }; 21514daa021SRusty Russell 216aff3d70aSJason Wang struct veth { 217aff3d70aSJason Wang __be16 h_vlan_proto; 218aff3d70aSJason Wang __be16 h_vlan_TCI; 2191da177e4SLinus Torvalds }; 2201da177e4SLinus Torvalds 2211ffcbc85SJesper Dangaard Brouer bool tun_is_xdp_frame(void *ptr) 222fc72d1d5SJason Wang { 223fc72d1d5SJason Wang return (unsigned long)ptr & TUN_XDP_FLAG; 224fc72d1d5SJason Wang } 2251ffcbc85SJesper Dangaard Brouer EXPORT_SYMBOL(tun_is_xdp_frame); 226fc72d1d5SJason Wang 227fc72d1d5SJason Wang void *tun_xdp_to_ptr(void *ptr) 228fc72d1d5SJason Wang { 229fc72d1d5SJason Wang return (void *)((unsigned long)ptr | TUN_XDP_FLAG); 230fc72d1d5SJason Wang } 231fc72d1d5SJason Wang EXPORT_SYMBOL(tun_xdp_to_ptr); 232fc72d1d5SJason Wang 233fc72d1d5SJason Wang void *tun_ptr_to_xdp(void *ptr) 234fc72d1d5SJason Wang { 235fc72d1d5SJason Wang return (void *)((unsigned long)ptr & ~TUN_XDP_FLAG); 236fc72d1d5SJason Wang } 237fc72d1d5SJason Wang EXPORT_SYMBOL(tun_ptr_to_xdp); 238fc72d1d5SJason Wang 23994317099SPetar Penkov static int tun_napi_receive(struct napi_struct *napi, int budget) 24094317099SPetar Penkov { 24194317099SPetar Penkov struct tun_file *tfile = container_of(napi, struct tun_file, napi); 24294317099SPetar Penkov struct sk_buff_head *queue = &tfile->sk.sk_write_queue; 24394317099SPetar Penkov struct sk_buff_head process_queue; 24494317099SPetar Penkov struct sk_buff *skb; 24594317099SPetar Penkov int received = 0; 24694317099SPetar Penkov 24794317099SPetar Penkov __skb_queue_head_init(&process_queue); 24894317099SPetar Penkov 24994317099SPetar Penkov spin_lock(&queue->lock); 25094317099SPetar Penkov skb_queue_splice_tail_init(queue, &process_queue); 25194317099SPetar Penkov spin_unlock(&queue->lock); 25294317099SPetar Penkov 25394317099SPetar Penkov while (received < budget && (skb = __skb_dequeue(&process_queue))) { 25494317099SPetar Penkov napi_gro_receive(napi, skb); 25594317099SPetar Penkov ++received; 25694317099SPetar Penkov } 25794317099SPetar Penkov 25894317099SPetar Penkov if (!skb_queue_empty(&process_queue)) { 25994317099SPetar Penkov spin_lock(&queue->lock); 26094317099SPetar Penkov skb_queue_splice(&process_queue, queue); 26194317099SPetar Penkov spin_unlock(&queue->lock); 26294317099SPetar Penkov } 26394317099SPetar Penkov 26494317099SPetar Penkov return received; 26594317099SPetar Penkov } 26694317099SPetar Penkov 26794317099SPetar Penkov static int tun_napi_poll(struct napi_struct *napi, int budget) 26894317099SPetar Penkov { 26994317099SPetar Penkov unsigned int received; 27094317099SPetar Penkov 27194317099SPetar Penkov received = tun_napi_receive(napi, budget); 27294317099SPetar Penkov 27394317099SPetar Penkov if (received < budget) 27494317099SPetar Penkov napi_complete_done(napi, received); 27594317099SPetar Penkov 27694317099SPetar Penkov return received; 27794317099SPetar Penkov } 27894317099SPetar Penkov 27994317099SPetar Penkov static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile, 280af3fb24eSEric Dumazet bool napi_en, bool napi_frags) 28194317099SPetar Penkov { 282aec72f33SEric Dumazet tfile->napi_enabled = napi_en; 283af3fb24eSEric Dumazet tfile->napi_frags_enabled = napi_en && napi_frags; 28494317099SPetar Penkov if (napi_en) { 285c39e342aSPetar Penkov netif_tx_napi_add(tun->dev, &tfile->napi, tun_napi_poll, 28694317099SPetar Penkov NAPI_POLL_WEIGHT); 28794317099SPetar Penkov napi_enable(&tfile->napi); 28894317099SPetar Penkov } 28994317099SPetar Penkov } 29094317099SPetar Penkov 29106e55addSEric Dumazet static void tun_napi_disable(struct tun_file *tfile) 29294317099SPetar Penkov { 293aec72f33SEric Dumazet if (tfile->napi_enabled) 29494317099SPetar Penkov napi_disable(&tfile->napi); 29594317099SPetar Penkov } 29694317099SPetar Penkov 29706e55addSEric Dumazet static void tun_napi_del(struct tun_file *tfile) 29894317099SPetar Penkov { 299aec72f33SEric Dumazet if (tfile->napi_enabled) 30094317099SPetar Penkov netif_napi_del(&tfile->napi); 30194317099SPetar Penkov } 30294317099SPetar Penkov 303af3fb24eSEric Dumazet static bool tun_napi_frags_enabled(const struct tun_file *tfile) 30490e33d45SPetar Penkov { 305af3fb24eSEric Dumazet return tfile->napi_frags_enabled; 30690e33d45SPetar Penkov } 30790e33d45SPetar Penkov 3088b8e658bSGreg Kurz #ifdef CONFIG_TUN_VNET_CROSS_LE 3098b8e658bSGreg Kurz static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) 3108b8e658bSGreg Kurz { 3118b8e658bSGreg Kurz return tun->flags & TUN_VNET_BE ? false : 3128b8e658bSGreg Kurz virtio_legacy_is_little_endian(); 3138b8e658bSGreg Kurz } 3148b8e658bSGreg Kurz 3158b8e658bSGreg Kurz static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) 3168b8e658bSGreg Kurz { 3178b8e658bSGreg Kurz int be = !!(tun->flags & TUN_VNET_BE); 3188b8e658bSGreg Kurz 3198b8e658bSGreg Kurz if (put_user(be, argp)) 3208b8e658bSGreg Kurz return -EFAULT; 3218b8e658bSGreg Kurz 3228b8e658bSGreg Kurz return 0; 3238b8e658bSGreg Kurz } 3248b8e658bSGreg Kurz 3258b8e658bSGreg Kurz static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) 3268b8e658bSGreg Kurz { 3278b8e658bSGreg Kurz int be; 3288b8e658bSGreg Kurz 3298b8e658bSGreg Kurz if (get_user(be, argp)) 3308b8e658bSGreg Kurz return -EFAULT; 3318b8e658bSGreg Kurz 3328b8e658bSGreg Kurz if (be) 3338b8e658bSGreg Kurz tun->flags |= TUN_VNET_BE; 3348b8e658bSGreg Kurz else 3358b8e658bSGreg Kurz tun->flags &= ~TUN_VNET_BE; 3368b8e658bSGreg Kurz 3378b8e658bSGreg Kurz return 0; 3388b8e658bSGreg Kurz } 3398b8e658bSGreg Kurz #else 3408b8e658bSGreg Kurz static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) 3418b8e658bSGreg Kurz { 3428b8e658bSGreg Kurz return virtio_legacy_is_little_endian(); 3438b8e658bSGreg Kurz } 3448b8e658bSGreg Kurz 3458b8e658bSGreg Kurz static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) 3468b8e658bSGreg Kurz { 3478b8e658bSGreg Kurz return -EINVAL; 3488b8e658bSGreg Kurz } 3498b8e658bSGreg Kurz 3508b8e658bSGreg Kurz static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) 3518b8e658bSGreg Kurz { 3528b8e658bSGreg Kurz return -EINVAL; 3538b8e658bSGreg Kurz } 3548b8e658bSGreg Kurz #endif /* CONFIG_TUN_VNET_CROSS_LE */ 3558b8e658bSGreg Kurz 35625bd55bbSGreg Kurz static inline bool tun_is_little_endian(struct tun_struct *tun) 35725bd55bbSGreg Kurz { 3587d824109SGreg Kurz return tun->flags & TUN_VNET_LE || 3598b8e658bSGreg Kurz tun_legacy_is_little_endian(tun); 36025bd55bbSGreg Kurz } 36125bd55bbSGreg Kurz 36256f0dcc5SMichael S. Tsirkin static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val) 36356f0dcc5SMichael S. Tsirkin { 36425bd55bbSGreg Kurz return __virtio16_to_cpu(tun_is_little_endian(tun), val); 36556f0dcc5SMichael S. Tsirkin } 36656f0dcc5SMichael S. Tsirkin 36756f0dcc5SMichael S. Tsirkin static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val) 36856f0dcc5SMichael S. Tsirkin { 36925bd55bbSGreg Kurz return __cpu_to_virtio16(tun_is_little_endian(tun), val); 37056f0dcc5SMichael S. Tsirkin } 37156f0dcc5SMichael S. Tsirkin 37296442e42SJason Wang static inline u32 tun_hashfn(u32 rxhash) 37396442e42SJason Wang { 374f13b5468SLi RongQing return rxhash & TUN_MASK_FLOW_ENTRIES; 37596442e42SJason Wang } 37696442e42SJason Wang 37796442e42SJason Wang static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash) 37896442e42SJason Wang { 37996442e42SJason Wang struct tun_flow_entry *e; 38096442e42SJason Wang 381b67bfe0dSSasha Levin hlist_for_each_entry_rcu(e, head, hash_link) { 38296442e42SJason Wang if (e->rxhash == rxhash) 38396442e42SJason Wang return e; 38496442e42SJason Wang } 38596442e42SJason Wang return NULL; 38696442e42SJason Wang } 38796442e42SJason Wang 38896442e42SJason Wang static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun, 38996442e42SJason Wang struct hlist_head *head, 39096442e42SJason Wang u32 rxhash, u16 queue_index) 39196442e42SJason Wang { 3929fdc6befSEric Dumazet struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC); 3939fdc6befSEric Dumazet 39496442e42SJason Wang if (e) { 3953424170fSMichal Kubecek netif_info(tun, tx_queued, tun->dev, 3963424170fSMichal Kubecek "create flow: hash %u index %u\n", 39796442e42SJason Wang rxhash, queue_index); 39896442e42SJason Wang e->updated = jiffies; 39996442e42SJason Wang e->rxhash = rxhash; 4009bc88939STom Herbert e->rps_rxhash = 0; 40196442e42SJason Wang e->queue_index = queue_index; 40296442e42SJason Wang e->tun = tun; 40396442e42SJason Wang hlist_add_head_rcu(&e->hash_link, head); 404b8732fb7SJason Wang ++tun->flow_count; 40596442e42SJason Wang } 40696442e42SJason Wang return e; 40796442e42SJason Wang } 40896442e42SJason Wang 40996442e42SJason Wang static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e) 41096442e42SJason Wang { 4113424170fSMichal Kubecek netif_info(tun, tx_queued, tun->dev, "delete flow: hash %u index %u\n", 41296442e42SJason Wang e->rxhash, e->queue_index); 41396442e42SJason Wang hlist_del_rcu(&e->hash_link); 4149fdc6befSEric Dumazet kfree_rcu(e, rcu); 415b8732fb7SJason Wang --tun->flow_count; 41696442e42SJason Wang } 41796442e42SJason Wang 41896442e42SJason Wang static void tun_flow_flush(struct tun_struct *tun) 41996442e42SJason Wang { 42096442e42SJason Wang int i; 42196442e42SJason Wang 42296442e42SJason Wang spin_lock_bh(&tun->lock); 42396442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 42496442e42SJason Wang struct tun_flow_entry *e; 425b67bfe0dSSasha Levin struct hlist_node *n; 42696442e42SJason Wang 427b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) 42896442e42SJason Wang tun_flow_delete(tun, e); 42996442e42SJason Wang } 43096442e42SJason Wang spin_unlock_bh(&tun->lock); 43196442e42SJason Wang } 43296442e42SJason Wang 43396442e42SJason Wang static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index) 43496442e42SJason Wang { 43596442e42SJason Wang int i; 43696442e42SJason Wang 43796442e42SJason Wang spin_lock_bh(&tun->lock); 43896442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 43996442e42SJason Wang struct tun_flow_entry *e; 440b67bfe0dSSasha Levin struct hlist_node *n; 44196442e42SJason Wang 442b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { 44396442e42SJason Wang if (e->queue_index == queue_index) 44496442e42SJason Wang tun_flow_delete(tun, e); 44596442e42SJason Wang } 44696442e42SJason Wang } 44796442e42SJason Wang spin_unlock_bh(&tun->lock); 44896442e42SJason Wang } 44996442e42SJason Wang 450e99e88a9SKees Cook static void tun_flow_cleanup(struct timer_list *t) 45196442e42SJason Wang { 452e99e88a9SKees Cook struct tun_struct *tun = from_timer(tun, t, flow_gc_timer); 45396442e42SJason Wang unsigned long delay = tun->ageing_time; 45496442e42SJason Wang unsigned long next_timer = jiffies + delay; 45596442e42SJason Wang unsigned long count = 0; 45696442e42SJason Wang int i; 45796442e42SJason Wang 4587dbfb4efSEric Dumazet spin_lock(&tun->lock); 45996442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 46096442e42SJason Wang struct tun_flow_entry *e; 461b67bfe0dSSasha Levin struct hlist_node *n; 46296442e42SJason Wang 463b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { 46496442e42SJason Wang unsigned long this_timer; 46581d98fa4SEric Dumazet 46696442e42SJason Wang this_timer = e->updated + delay; 46781d98fa4SEric Dumazet if (time_before_eq(this_timer, jiffies)) { 46896442e42SJason Wang tun_flow_delete(tun, e); 46981d98fa4SEric Dumazet continue; 47081d98fa4SEric Dumazet } 47181d98fa4SEric Dumazet count++; 47281d98fa4SEric Dumazet if (time_before(this_timer, next_timer)) 47396442e42SJason Wang next_timer = this_timer; 47496442e42SJason Wang } 47596442e42SJason Wang } 47696442e42SJason Wang 47796442e42SJason Wang if (count) 47896442e42SJason Wang mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer)); 4797dbfb4efSEric Dumazet spin_unlock(&tun->lock); 48096442e42SJason Wang } 48196442e42SJason Wang 48249974420SEric Dumazet static void tun_flow_update(struct tun_struct *tun, u32 rxhash, 4839e85722dSJason Wang struct tun_file *tfile) 48496442e42SJason Wang { 48596442e42SJason Wang struct hlist_head *head; 48696442e42SJason Wang struct tun_flow_entry *e; 48796442e42SJason Wang unsigned long delay = tun->ageing_time; 4889e85722dSJason Wang u16 queue_index = tfile->queue_index; 48996442e42SJason Wang 49096442e42SJason Wang head = &tun->flows[tun_hashfn(rxhash)]; 49196442e42SJason Wang 49296442e42SJason Wang rcu_read_lock(); 49396442e42SJason Wang 49496442e42SJason Wang e = tun_flow_find(head, rxhash); 49596442e42SJason Wang if (likely(e)) { 49696442e42SJason Wang /* TODO: keep queueing to old queue until it's empty? */ 4974ffdd22eSEric Dumazet if (READ_ONCE(e->queue_index) != queue_index) 4984ffdd22eSEric Dumazet WRITE_ONCE(e->queue_index, queue_index); 49983b1bc12SLi RongQing if (e->updated != jiffies) 50096442e42SJason Wang e->updated = jiffies; 5019bc88939STom Herbert sock_rps_record_flow_hash(e->rps_rxhash); 50296442e42SJason Wang } else { 50396442e42SJason Wang spin_lock_bh(&tun->lock); 504b8732fb7SJason Wang if (!tun_flow_find(head, rxhash) && 505b8732fb7SJason Wang tun->flow_count < MAX_TAP_FLOWS) 50696442e42SJason Wang tun_flow_create(tun, head, rxhash, queue_index); 50796442e42SJason Wang 50896442e42SJason Wang if (!timer_pending(&tun->flow_gc_timer)) 50996442e42SJason Wang mod_timer(&tun->flow_gc_timer, 51096442e42SJason Wang round_jiffies_up(jiffies + delay)); 51196442e42SJason Wang spin_unlock_bh(&tun->lock); 51296442e42SJason Wang } 51396442e42SJason Wang 51496442e42SJason Wang rcu_read_unlock(); 51596442e42SJason Wang } 51696442e42SJason Wang 517516c512bSMichal Kubecek /* Save the hash received in the stack receive path and update the 5189bc88939STom Herbert * flow_hash table accordingly. 5199bc88939STom Herbert */ 5209bc88939STom Herbert static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash) 5219bc88939STom Herbert { 522567e4b79SEric Dumazet if (unlikely(e->rps_rxhash != hash)) 5239bc88939STom Herbert e->rps_rxhash = hash; 5249bc88939STom Herbert } 5259bc88939STom Herbert 5264b035271SWang Li /* We try to identify a flow through its rxhash. The reason that 52792d4ea6eSstephen hemminger * we do not check rxq no. is because some cards(e.g 82599), chooses 528c8d68e6bSJason Wang * the rxq based on the txq where the last packet of the flow comes. As 529c8d68e6bSJason Wang * the userspace application move between processors, we may get a 5304b035271SWang Li * different rxq no. here. 531c8d68e6bSJason Wang */ 53296f84061SJason Wang static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb) 533c8d68e6bSJason Wang { 53496442e42SJason Wang struct tun_flow_entry *e; 535c8d68e6bSJason Wang u32 txq = 0; 536c8d68e6bSJason Wang u32 numqueues = 0; 537c8d68e6bSJason Wang 5386aa7de05SMark Rutland numqueues = READ_ONCE(tun->numqueues); 539c8d68e6bSJason Wang 540feec084aSJason Wang txq = __skb_get_hash_symmetric(skb); 54196442e42SJason Wang e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq); 5429bc88939STom Herbert if (e) { 5439bc88939STom Herbert tun_flow_save_rps_rxhash(e, txq); 544fbe4d456SZhi Yong Wu txq = e->queue_index; 5454b035271SWang Li } else { 546c8d68e6bSJason Wang /* use multiply and shift instead of expensive divide */ 547c8d68e6bSJason Wang txq = ((u64)txq * numqueues) >> 32; 548c8d68e6bSJason Wang } 549c8d68e6bSJason Wang 550c8d68e6bSJason Wang return txq; 551c8d68e6bSJason Wang } 552c8d68e6bSJason Wang 55396f84061SJason Wang static u16 tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb) 55496f84061SJason Wang { 555cd5681d7SJason Wang struct tun_prog *prog; 556a35d310fSJason Wang u32 numqueues; 55796f84061SJason Wang u16 ret = 0; 55896f84061SJason Wang 559a35d310fSJason Wang numqueues = READ_ONCE(tun->numqueues); 560a35d310fSJason Wang if (!numqueues) 561a35d310fSJason Wang return 0; 562a35d310fSJason Wang 56396f84061SJason Wang prog = rcu_dereference(tun->steering_prog); 56496f84061SJason Wang if (prog) 56596f84061SJason Wang ret = bpf_prog_run_clear_cb(prog->prog, skb); 56696f84061SJason Wang 567a35d310fSJason Wang return ret % numqueues; 56896f84061SJason Wang } 56996f84061SJason Wang 57096f84061SJason Wang static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb, 571a350ecceSPaolo Abeni struct net_device *sb_dev) 57296f84061SJason Wang { 57396f84061SJason Wang struct tun_struct *tun = netdev_priv(dev); 57496f84061SJason Wang u16 ret; 57596f84061SJason Wang 57696f84061SJason Wang rcu_read_lock(); 57796f84061SJason Wang if (rcu_dereference(tun->steering_prog)) 57896f84061SJason Wang ret = tun_ebpf_select_queue(tun, skb); 57996f84061SJason Wang else 58096f84061SJason Wang ret = tun_automq_select_queue(tun, skb); 58196f84061SJason Wang rcu_read_unlock(); 58296f84061SJason Wang 58396f84061SJason Wang return ret; 58496f84061SJason Wang } 58596f84061SJason Wang 586cde8b15fSJason Wang static inline bool tun_not_capable(struct tun_struct *tun) 587cde8b15fSJason Wang { 588cde8b15fSJason Wang const struct cred *cred = current_cred(); 589c260b772SEric W. Biederman struct net *net = dev_net(tun->dev); 590cde8b15fSJason Wang 591cde8b15fSJason Wang return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) || 592cde8b15fSJason Wang (gid_valid(tun->group) && !in_egroup_p(tun->group))) && 593c260b772SEric W. Biederman !ns_capable(net->user_ns, CAP_NET_ADMIN); 594cde8b15fSJason Wang } 595cde8b15fSJason Wang 596c8d68e6bSJason Wang static void tun_set_real_num_queues(struct tun_struct *tun) 597c8d68e6bSJason Wang { 598c8d68e6bSJason Wang netif_set_real_num_tx_queues(tun->dev, tun->numqueues); 599c8d68e6bSJason Wang netif_set_real_num_rx_queues(tun->dev, tun->numqueues); 600c8d68e6bSJason Wang } 601c8d68e6bSJason Wang 6024008e97fSJason Wang static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) 6034008e97fSJason Wang { 6044008e97fSJason Wang tfile->detached = tun; 6054008e97fSJason Wang list_add_tail(&tfile->next, &tun->disabled); 6064008e97fSJason Wang ++tun->numdisabled; 6074008e97fSJason Wang } 6084008e97fSJason Wang 609d32649d1SJason Wang static struct tun_struct *tun_enable_queue(struct tun_file *tfile) 6104008e97fSJason Wang { 6114008e97fSJason Wang struct tun_struct *tun = tfile->detached; 6124008e97fSJason Wang 6134008e97fSJason Wang tfile->detached = NULL; 6144008e97fSJason Wang list_del_init(&tfile->next); 6154008e97fSJason Wang --tun->numdisabled; 6164008e97fSJason Wang return tun; 6174008e97fSJason Wang } 6184008e97fSJason Wang 6193a403076SJason Wang void tun_ptr_free(void *ptr) 620fc72d1d5SJason Wang { 621fc72d1d5SJason Wang if (!ptr) 622fc72d1d5SJason Wang return; 6231ffcbc85SJesper Dangaard Brouer if (tun_is_xdp_frame(ptr)) { 6241ffcbc85SJesper Dangaard Brouer struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr); 625fc72d1d5SJason Wang 62603993094SJesper Dangaard Brouer xdp_return_frame(xdpf); 627fc72d1d5SJason Wang } else { 628fc72d1d5SJason Wang __skb_array_destroy_skb(ptr); 629fc72d1d5SJason Wang } 630fc72d1d5SJason Wang } 6313a403076SJason Wang EXPORT_SYMBOL_GPL(tun_ptr_free); 632fc72d1d5SJason Wang 6334bfb0513SJason Wang static void tun_queue_purge(struct tun_file *tfile) 6344bfb0513SJason Wang { 635fc72d1d5SJason Wang void *ptr; 6361576d986SJason Wang 637fc72d1d5SJason Wang while ((ptr = ptr_ring_consume(&tfile->tx_ring)) != NULL) 638fc72d1d5SJason Wang tun_ptr_free(ptr); 6391576d986SJason Wang 6405503fcecSJason Wang skb_queue_purge(&tfile->sk.sk_write_queue); 6414bfb0513SJason Wang skb_queue_purge(&tfile->sk.sk_error_queue); 6424bfb0513SJason Wang } 6434bfb0513SJason Wang 644c8d68e6bSJason Wang static void __tun_detach(struct tun_file *tfile, bool clean) 645c8d68e6bSJason Wang { 646c8d68e6bSJason Wang struct tun_file *ntfile; 647c8d68e6bSJason Wang struct tun_struct *tun; 648c8d68e6bSJason Wang 649b8deabd3SJason Wang tun = rtnl_dereference(tfile->tun); 650b8deabd3SJason Wang 65194317099SPetar Penkov if (tun && clean) { 65206e55addSEric Dumazet tun_napi_disable(tfile); 65306e55addSEric Dumazet tun_napi_del(tfile); 65494317099SPetar Penkov } 65594317099SPetar Penkov 6569e85722dSJason Wang if (tun && !tfile->detached) { 657c8d68e6bSJason Wang u16 index = tfile->queue_index; 658c8d68e6bSJason Wang BUG_ON(index >= tun->numqueues); 659c8d68e6bSJason Wang 660c8d68e6bSJason Wang rcu_assign_pointer(tun->tfiles[index], 661c8d68e6bSJason Wang tun->tfiles[tun->numqueues - 1]); 662b8deabd3SJason Wang ntfile = rtnl_dereference(tun->tfiles[index]); 663c8d68e6bSJason Wang ntfile->queue_index = index; 6649871a9e4SJason Wang rcu_assign_pointer(tun->tfiles[tun->numqueues - 1], 6659871a9e4SJason Wang NULL); 666c8d68e6bSJason Wang 667c8d68e6bSJason Wang --tun->numqueues; 6689e85722dSJason Wang if (clean) { 669c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 670c8d68e6bSJason Wang sock_put(&tfile->sk); 6719e85722dSJason Wang } else 6724008e97fSJason Wang tun_disable_queue(tun, tfile); 673c8d68e6bSJason Wang 674c8d68e6bSJason Wang synchronize_net(); 67596442e42SJason Wang tun_flow_delete_by_queue(tun, tun->numqueues + 1); 676c8d68e6bSJason Wang /* Drop read queue */ 6774bfb0513SJason Wang tun_queue_purge(tfile); 678c8d68e6bSJason Wang tun_set_real_num_queues(tun); 679dd38bd85SJason Wang } else if (tfile->detached && clean) { 6804008e97fSJason Wang tun = tun_enable_queue(tfile); 681dd38bd85SJason Wang sock_put(&tfile->sk); 682dd38bd85SJason Wang } 683c8d68e6bSJason Wang 684c8d68e6bSJason Wang if (clean) { 685af668b3cSMichael S. Tsirkin if (tun && tun->numqueues == 0 && tun->numdisabled == 0) { 686af668b3cSMichael S. Tsirkin netif_carrier_off(tun->dev); 687af668b3cSMichael S. Tsirkin 68840630b82SMichael S. Tsirkin if (!(tun->flags & IFF_PERSIST) && 689af668b3cSMichael S. Tsirkin tun->dev->reg_state == NETREG_REGISTERED) 6904008e97fSJason Wang unregister_netdevice(tun->dev); 691af668b3cSMichael S. Tsirkin } 692b196d88aSJason Wang if (tun) 693b196d88aSJason Wang xdp_rxq_info_unreg(&tfile->xdp_rxq); 6947063efd3SJason Wang ptr_ring_cleanup(&tfile->tx_ring, tun_ptr_free); 695140e807dSEric W. Biederman sock_put(&tfile->sk); 696c8d68e6bSJason Wang } 697c8d68e6bSJason Wang } 698c8d68e6bSJason Wang 699c8d68e6bSJason Wang static void tun_detach(struct tun_file *tfile, bool clean) 700c8d68e6bSJason Wang { 70183c1f36fSSabrina Dubroca struct tun_struct *tun; 70283c1f36fSSabrina Dubroca struct net_device *dev; 70383c1f36fSSabrina Dubroca 704c8d68e6bSJason Wang rtnl_lock(); 70583c1f36fSSabrina Dubroca tun = rtnl_dereference(tfile->tun); 70683c1f36fSSabrina Dubroca dev = tun ? tun->dev : NULL; 707c8d68e6bSJason Wang __tun_detach(tfile, clean); 70883c1f36fSSabrina Dubroca if (dev) 70983c1f36fSSabrina Dubroca netdev_state_change(dev); 710c8d68e6bSJason Wang rtnl_unlock(); 711c8d68e6bSJason Wang } 712c8d68e6bSJason Wang 713c8d68e6bSJason Wang static void tun_detach_all(struct net_device *dev) 714c8d68e6bSJason Wang { 715c8d68e6bSJason Wang struct tun_struct *tun = netdev_priv(dev); 7164008e97fSJason Wang struct tun_file *tfile, *tmp; 717c8d68e6bSJason Wang int i, n = tun->numqueues; 718c8d68e6bSJason Wang 719c8d68e6bSJason Wang for (i = 0; i < n; i++) { 720b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 721c8d68e6bSJason Wang BUG_ON(!tfile); 72206e55addSEric Dumazet tun_napi_disable(tfile); 723addf8fc4SJason Wang tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; 7249e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 725c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 726c8d68e6bSJason Wang --tun->numqueues; 727c8d68e6bSJason Wang } 7289e85722dSJason Wang list_for_each_entry(tfile, &tun->disabled, next) { 729addf8fc4SJason Wang tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; 7309e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 731c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 7329e85722dSJason Wang } 733c8d68e6bSJason Wang BUG_ON(tun->numqueues != 0); 734c8d68e6bSJason Wang 735c8d68e6bSJason Wang synchronize_net(); 736c8d68e6bSJason Wang for (i = 0; i < n; i++) { 737b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 73806e55addSEric Dumazet tun_napi_del(tfile); 739c8d68e6bSJason Wang /* Drop read queue */ 7404bfb0513SJason Wang tun_queue_purge(tfile); 741b196d88aSJason Wang xdp_rxq_info_unreg(&tfile->xdp_rxq); 742c8d68e6bSJason Wang sock_put(&tfile->sk); 743c8d68e6bSJason Wang } 7444008e97fSJason Wang list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { 7454008e97fSJason Wang tun_enable_queue(tfile); 7464bfb0513SJason Wang tun_queue_purge(tfile); 747b196d88aSJason Wang xdp_rxq_info_unreg(&tfile->xdp_rxq); 7484008e97fSJason Wang sock_put(&tfile->sk); 7494008e97fSJason Wang } 7504008e97fSJason Wang BUG_ON(tun->numdisabled != 0); 751dd38bd85SJason Wang 75240630b82SMichael S. Tsirkin if (tun->flags & IFF_PERSIST) 753dd38bd85SJason Wang module_put(THIS_MODULE); 754c8d68e6bSJason Wang } 755c8d68e6bSJason Wang 75694317099SPetar Penkov static int tun_attach(struct tun_struct *tun, struct file *file, 75777f22f92SYang Yingliang bool skip_filter, bool napi, bool napi_frags, 75877f22f92SYang Yingliang bool publish_tun) 759a7385ba2SEric W. Biederman { 760631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 7611576d986SJason Wang struct net_device *dev = tun->dev; 76238231b7aSEric W. Biederman int err; 763a7385ba2SEric W. Biederman 7645dbbaf2dSPaul Moore err = security_tun_dev_attach(tfile->socket.sk, tun->security); 7655dbbaf2dSPaul Moore if (err < 0) 7665dbbaf2dSPaul Moore goto out; 7675dbbaf2dSPaul Moore 76838231b7aSEric W. Biederman err = -EINVAL; 7699e85722dSJason Wang if (rtnl_dereference(tfile->tun) && !tfile->detached) 77038231b7aSEric W. Biederman goto out; 77138231b7aSEric W. Biederman 77238231b7aSEric W. Biederman err = -EBUSY; 77340630b82SMichael S. Tsirkin if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1) 774c8d68e6bSJason Wang goto out; 775c8d68e6bSJason Wang 776c8d68e6bSJason Wang err = -E2BIG; 7774008e97fSJason Wang if (!tfile->detached && 7784008e97fSJason Wang tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES) 77938231b7aSEric W. Biederman goto out; 78038231b7aSEric W. Biederman 78138231b7aSEric W. Biederman err = 0; 78254f968d6SJason Wang 78392d4ea6eSstephen hemminger /* Re-attach the filter to persist device */ 784849c9b6fSPavel Emelyanov if (!skip_filter && (tun->filter_attached == true)) { 7858ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 7868ced425eSHannes Frederic Sowa err = sk_attach_filter(&tun->fprog, tfile->socket.sk); 7878ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 78854f968d6SJason Wang if (!err) 78954f968d6SJason Wang goto out; 79054f968d6SJason Wang } 7911576d986SJason Wang 7921576d986SJason Wang if (!tfile->detached && 793b196d88aSJason Wang ptr_ring_resize(&tfile->tx_ring, dev->tx_queue_len, 794b196d88aSJason Wang GFP_KERNEL, tun_ptr_free)) { 7951576d986SJason Wang err = -ENOMEM; 7961576d986SJason Wang goto out; 7971576d986SJason Wang } 7981576d986SJason Wang 799c8d68e6bSJason Wang tfile->queue_index = tun->numqueues; 800addf8fc4SJason Wang tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN; 8018bf5c4eeSJesper Dangaard Brouer 8028bf5c4eeSJesper Dangaard Brouer if (tfile->detached) { 8038bf5c4eeSJesper Dangaard Brouer /* Re-attach detached tfile, updating XDP queue_index */ 8048bf5c4eeSJesper Dangaard Brouer WARN_ON(!xdp_rxq_info_is_reg(&tfile->xdp_rxq)); 8058bf5c4eeSJesper Dangaard Brouer 8068bf5c4eeSJesper Dangaard Brouer if (tfile->xdp_rxq.queue_index != tfile->queue_index) 8078bf5c4eeSJesper Dangaard Brouer tfile->xdp_rxq.queue_index = tfile->queue_index; 8088bf5c4eeSJesper Dangaard Brouer } else { 8098bf5c4eeSJesper Dangaard Brouer /* Setup XDP RX-queue info, for new tfile getting attached */ 8108bf5c4eeSJesper Dangaard Brouer err = xdp_rxq_info_reg(&tfile->xdp_rxq, 8118bf5c4eeSJesper Dangaard Brouer tun->dev, tfile->queue_index); 8128bf5c4eeSJesper Dangaard Brouer if (err < 0) 8138bf5c4eeSJesper Dangaard Brouer goto out; 8148d5d8852SJesper Dangaard Brouer err = xdp_rxq_info_reg_mem_model(&tfile->xdp_rxq, 8158d5d8852SJesper Dangaard Brouer MEM_TYPE_PAGE_SHARED, NULL); 8168d5d8852SJesper Dangaard Brouer if (err < 0) { 8178d5d8852SJesper Dangaard Brouer xdp_rxq_info_unreg(&tfile->xdp_rxq); 8188d5d8852SJesper Dangaard Brouer goto out; 8198d5d8852SJesper Dangaard Brouer } 8208bf5c4eeSJesper Dangaard Brouer err = 0; 8218bf5c4eeSJesper Dangaard Brouer } 8228bf5c4eeSJesper Dangaard Brouer 82394317099SPetar Penkov if (tfile->detached) { 8244008e97fSJason Wang tun_enable_queue(tfile); 82594317099SPetar Penkov } else { 8264008e97fSJason Wang sock_hold(&tfile->sk); 827af3fb24eSEric Dumazet tun_napi_init(tun, tfile, napi, napi_frags); 82894317099SPetar Penkov } 8294008e97fSJason Wang 830e4a2a304SJason Wang if (rtnl_dereference(tun->xdp_prog)) 831e4a2a304SJason Wang sock_set_flag(&tfile->sk, SOCK_XDP); 832e4a2a304SJason Wang 833c8d68e6bSJason Wang /* device is allowed to go away first, so no need to hold extra 834c8d68e6bSJason Wang * refcnt. 835c8d68e6bSJason Wang */ 836a7385ba2SEric W. Biederman 8370b7959b6SStanislav Fomichev /* Publish tfile->tun and tun->tfiles only after we've fully 8380b7959b6SStanislav Fomichev * initialized tfile; otherwise we risk using half-initialized 8390b7959b6SStanislav Fomichev * object. 8400b7959b6SStanislav Fomichev */ 84177f22f92SYang Yingliang if (publish_tun) 8420b7959b6SStanislav Fomichev rcu_assign_pointer(tfile->tun, tun); 8430b7959b6SStanislav Fomichev rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); 8440b7959b6SStanislav Fomichev tun->numqueues++; 8453a03cb84SGeorge Amanakis tun_set_real_num_queues(tun); 84638231b7aSEric W. Biederman out: 84738231b7aSEric W. Biederman return err; 848a7385ba2SEric W. Biederman } 849a7385ba2SEric W. Biederman 8509484dc74Syuan linyu static struct tun_struct *tun_get(struct tun_file *tfile) 851631ab46bSEric W. Biederman { 8526e914fc7SJason Wang struct tun_struct *tun; 853c70f1829SEric W. Biederman 8546e914fc7SJason Wang rcu_read_lock(); 8556e914fc7SJason Wang tun = rcu_dereference(tfile->tun); 8566e914fc7SJason Wang if (tun) 8576e914fc7SJason Wang dev_hold(tun->dev); 8586e914fc7SJason Wang rcu_read_unlock(); 859c70f1829SEric W. Biederman 860c70f1829SEric W. Biederman return tun; 861631ab46bSEric W. Biederman } 862631ab46bSEric W. Biederman 863631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun) 864631ab46bSEric W. Biederman { 8656e914fc7SJason Wang dev_put(tun->dev); 866631ab46bSEric W. Biederman } 867631ab46bSEric W. Biederman 8686b8a66eeSJoe Perches /* TAP filtering */ 869f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr) 870f271b2ccSMax Krasnyansky { 871f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 872f271b2ccSMax Krasnyansky mask[n >> 5] |= (1 << (n & 31)); 873f271b2ccSMax Krasnyansky } 874f271b2ccSMax Krasnyansky 875f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) 876f271b2ccSMax Krasnyansky { 877f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 878f271b2ccSMax Krasnyansky return mask[n >> 5] & (1 << (n & 31)); 879f271b2ccSMax Krasnyansky } 880f271b2ccSMax Krasnyansky 881f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg) 882f271b2ccSMax Krasnyansky { 883f271b2ccSMax Krasnyansky struct { u8 u[ETH_ALEN]; } *addr; 884f271b2ccSMax Krasnyansky struct tun_filter uf; 885f271b2ccSMax Krasnyansky int err, alen, n, nexact; 886f271b2ccSMax Krasnyansky 887f271b2ccSMax Krasnyansky if (copy_from_user(&uf, arg, sizeof(uf))) 888f271b2ccSMax Krasnyansky return -EFAULT; 889f271b2ccSMax Krasnyansky 890f271b2ccSMax Krasnyansky if (!uf.count) { 891f271b2ccSMax Krasnyansky /* Disabled */ 892f271b2ccSMax Krasnyansky filter->count = 0; 893f271b2ccSMax Krasnyansky return 0; 894f271b2ccSMax Krasnyansky } 895f271b2ccSMax Krasnyansky 896f271b2ccSMax Krasnyansky alen = ETH_ALEN * uf.count; 89728e8190dSMarkus Elfring addr = memdup_user(arg + sizeof(uf), alen); 89828e8190dSMarkus Elfring if (IS_ERR(addr)) 89928e8190dSMarkus Elfring return PTR_ERR(addr); 900f271b2ccSMax Krasnyansky 901f271b2ccSMax Krasnyansky /* The filter is updated without holding any locks. Which is 902f271b2ccSMax Krasnyansky * perfectly safe. We disable it first and in the worst 903f271b2ccSMax Krasnyansky * case we'll accept a few undesired packets. */ 904f271b2ccSMax Krasnyansky filter->count = 0; 905f271b2ccSMax Krasnyansky wmb(); 906f271b2ccSMax Krasnyansky 907f271b2ccSMax Krasnyansky /* Use first set of addresses as an exact filter */ 908f271b2ccSMax Krasnyansky for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++) 909f271b2ccSMax Krasnyansky memcpy(filter->addr[n], addr[n].u, ETH_ALEN); 910f271b2ccSMax Krasnyansky 911f271b2ccSMax Krasnyansky nexact = n; 912f271b2ccSMax Krasnyansky 913cfbf84fcSAlex Williamson /* Remaining multicast addresses are hashed, 914cfbf84fcSAlex Williamson * unicast will leave the filter disabled. */ 915f271b2ccSMax Krasnyansky memset(filter->mask, 0, sizeof(filter->mask)); 916cfbf84fcSAlex Williamson for (; n < uf.count; n++) { 917cfbf84fcSAlex Williamson if (!is_multicast_ether_addr(addr[n].u)) { 918cfbf84fcSAlex Williamson err = 0; /* no filter */ 9193b8d2a69SMarkus Elfring goto free_addr; 920cfbf84fcSAlex Williamson } 921f271b2ccSMax Krasnyansky addr_hash_set(filter->mask, addr[n].u); 922cfbf84fcSAlex Williamson } 923f271b2ccSMax Krasnyansky 924f271b2ccSMax Krasnyansky /* For ALLMULTI just set the mask to all ones. 925f271b2ccSMax Krasnyansky * This overrides the mask populated above. */ 926f271b2ccSMax Krasnyansky if ((uf.flags & TUN_FLT_ALLMULTI)) 927f271b2ccSMax Krasnyansky memset(filter->mask, ~0, sizeof(filter->mask)); 928f271b2ccSMax Krasnyansky 929f271b2ccSMax Krasnyansky /* Now enable the filter */ 930f271b2ccSMax Krasnyansky wmb(); 931f271b2ccSMax Krasnyansky filter->count = nexact; 932f271b2ccSMax Krasnyansky 933f271b2ccSMax Krasnyansky /* Return the number of exact filters */ 934f271b2ccSMax Krasnyansky err = nexact; 9353b8d2a69SMarkus Elfring free_addr: 936f271b2ccSMax Krasnyansky kfree(addr); 937f271b2ccSMax Krasnyansky return err; 938f271b2ccSMax Krasnyansky } 939f271b2ccSMax Krasnyansky 940f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */ 941f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb) 942f271b2ccSMax Krasnyansky { 943f271b2ccSMax Krasnyansky /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect 944f271b2ccSMax Krasnyansky * at this point. */ 945f271b2ccSMax Krasnyansky struct ethhdr *eh = (struct ethhdr *) skb->data; 946f271b2ccSMax Krasnyansky int i; 947f271b2ccSMax Krasnyansky 948f271b2ccSMax Krasnyansky /* Exact match */ 949f271b2ccSMax Krasnyansky for (i = 0; i < filter->count; i++) 9502e42e474SJoe Perches if (ether_addr_equal(eh->h_dest, filter->addr[i])) 951f271b2ccSMax Krasnyansky return 1; 952f271b2ccSMax Krasnyansky 953f271b2ccSMax Krasnyansky /* Inexact match (multicast only) */ 954f271b2ccSMax Krasnyansky if (is_multicast_ether_addr(eh->h_dest)) 955f271b2ccSMax Krasnyansky return addr_hash_test(filter->mask, eh->h_dest); 956f271b2ccSMax Krasnyansky 957f271b2ccSMax Krasnyansky return 0; 958f271b2ccSMax Krasnyansky } 959f271b2ccSMax Krasnyansky 960f271b2ccSMax Krasnyansky /* 961f271b2ccSMax Krasnyansky * Checks whether the packet is accepted or not. 962f271b2ccSMax Krasnyansky * Returns: 0 - drop, !=0 - accept 963f271b2ccSMax Krasnyansky */ 964f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb) 965f271b2ccSMax Krasnyansky { 966f271b2ccSMax Krasnyansky if (!filter->count) 967f271b2ccSMax Krasnyansky return 1; 968f271b2ccSMax Krasnyansky 969f271b2ccSMax Krasnyansky return run_filter(filter, skb); 970f271b2ccSMax Krasnyansky } 971f271b2ccSMax Krasnyansky 9721da177e4SLinus Torvalds /* Network device part of the driver */ 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds static const struct ethtool_ops tun_ethtool_ops; 9751da177e4SLinus Torvalds 976c70f1829SEric W. Biederman /* Net device detach from fd. */ 977c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev) 978c70f1829SEric W. Biederman { 979c8d68e6bSJason Wang tun_detach_all(dev); 980c70f1829SEric W. Biederman } 981c70f1829SEric W. Biederman 9821da177e4SLinus Torvalds /* Net device open. */ 9831da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev) 9841da177e4SLinus Torvalds { 985c8d68e6bSJason Wang netif_tx_start_all_queues(dev); 986b20e2d54SHannes Frederic Sowa 9871da177e4SLinus Torvalds return 0; 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds /* Net device close. */ 9911da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev) 9921da177e4SLinus Torvalds { 993c8d68e6bSJason Wang netif_tx_stop_all_queues(dev); 9941da177e4SLinus Torvalds return 0; 9951da177e4SLinus Torvalds } 9961da177e4SLinus Torvalds 9971da177e4SLinus Torvalds /* Net device start xmit */ 99896f84061SJason Wang static void tun_automq_xmit(struct tun_struct *tun, struct sk_buff *skb) 9991da177e4SLinus Torvalds { 10003df97ba8SJason Wang #ifdef CONFIG_RPS 1001dc05360fSEric Dumazet if (tun->numqueues == 1 && static_branch_unlikely(&rps_needed)) { 10029bc88939STom Herbert /* Select queue was not called for the skbuff, so we extract the 10039bc88939STom Herbert * RPS hash and save it into the flow_table here. 10049bc88939STom Herbert */ 10054b035271SWang Li struct tun_flow_entry *e; 10069bc88939STom Herbert __u32 rxhash; 10079bc88939STom Herbert 1008feec084aSJason Wang rxhash = __skb_get_hash_symmetric(skb); 10094b035271SWang Li e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)], rxhash); 10109bc88939STom Herbert if (e) 10119bc88939STom Herbert tun_flow_save_rps_rxhash(e, rxhash); 10129bc88939STom Herbert } 10133df97ba8SJason Wang #endif 101496f84061SJason Wang } 101596f84061SJason Wang 1016aff3d70aSJason Wang static unsigned int run_ebpf_filter(struct tun_struct *tun, 1017aff3d70aSJason Wang struct sk_buff *skb, 1018aff3d70aSJason Wang int len) 1019aff3d70aSJason Wang { 1020aff3d70aSJason Wang struct tun_prog *prog = rcu_dereference(tun->filter_prog); 1021aff3d70aSJason Wang 1022aff3d70aSJason Wang if (prog) 1023aff3d70aSJason Wang len = bpf_prog_run_clear_cb(prog->prog, skb); 1024aff3d70aSJason Wang 1025aff3d70aSJason Wang return len; 1026aff3d70aSJason Wang } 1027aff3d70aSJason Wang 102896f84061SJason Wang /* Net device start xmit */ 102996f84061SJason Wang static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) 103096f84061SJason Wang { 103196f84061SJason Wang struct tun_struct *tun = netdev_priv(dev); 103296f84061SJason Wang int txq = skb->queue_mapping; 103396f84061SJason Wang struct tun_file *tfile; 1034aff3d70aSJason Wang int len = skb->len; 103596f84061SJason Wang 103696f84061SJason Wang rcu_read_lock(); 103796f84061SJason Wang tfile = rcu_dereference(tun->tfiles[txq]); 103896f84061SJason Wang 103996f84061SJason Wang /* Drop packet if interface is not attached */ 10409871a9e4SJason Wang if (!tfile) 104196f84061SJason Wang goto drop; 104296f84061SJason Wang 104396f84061SJason Wang if (!rcu_dereference(tun->steering_prog)) 104496f84061SJason Wang tun_automq_xmit(tun, skb); 10459bc88939STom Herbert 10463424170fSMichal Kubecek netif_info(tun, tx_queued, tun->dev, "%s %d\n", __func__, skb->len); 10476e914fc7SJason Wang 1048f271b2ccSMax Krasnyansky /* Drop if the filter does not like it. 1049f271b2ccSMax Krasnyansky * This is a noop if the filter is disabled. 1050f271b2ccSMax Krasnyansky * Filter can be enabled only for the TAP devices. */ 1051f271b2ccSMax Krasnyansky if (!check_filter(&tun->txflt, skb)) 1052f271b2ccSMax Krasnyansky goto drop; 1053f271b2ccSMax Krasnyansky 105454f968d6SJason Wang if (tfile->socket.sk->sk_filter && 105554f968d6SJason Wang sk_filter(tfile->socket.sk, skb)) 105699405162SMichael S. Tsirkin goto drop; 105799405162SMichael S. Tsirkin 1058aff3d70aSJason Wang len = run_ebpf_filter(tun, skb, len); 105981c89507SBjørn Mork if (len == 0 || pskb_trim(skb, len)) 1060aff3d70aSJason Wang goto drop; 1061aff3d70aSJason Wang 10621f8b977aSWillem de Bruijn if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) 10637bf66305SJason Wang goto drop; 10647bf66305SJason Wang 10657b996243SSoheil Hassas Yeganeh skb_tx_timestamp(skb); 1066eda29772SRichard Cochran 10670110d6f2SMichael S. Tsirkin /* Orphan the skb - required as we might hang on to it 10687bf66305SJason Wang * for indefinite time. 10697bf66305SJason Wang */ 10700110d6f2SMichael S. Tsirkin skb_orphan(skb); 10710110d6f2SMichael S. Tsirkin 1072895b5c9fSFlorian Westphal nf_reset_ct(skb); 1073f8af75f3SEric Dumazet 10745990a305SJason Wang if (ptr_ring_produce(&tfile->tx_ring, skb)) 10751576d986SJason Wang goto drop; 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds /* Notify and wake up reader process */ 107854f968d6SJason Wang if (tfile->flags & TUN_FASYNC) 107954f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_IN); 10809e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 10816e914fc7SJason Wang 10826e914fc7SJason Wang rcu_read_unlock(); 10836ed10654SPatrick McHardy return NETDEV_TX_OK; 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds drop: 1086608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->tx_dropped); 1087149d36f7SMichael S. Tsirkin skb_tx_error(skb); 10881da177e4SLinus Torvalds kfree_skb(skb); 10896e914fc7SJason Wang rcu_read_unlock(); 1090baeababbSJason Wang return NET_XMIT_DROP; 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds 1093f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev) 10941da177e4SLinus Torvalds { 1095f271b2ccSMax Krasnyansky /* 1096f271b2ccSMax Krasnyansky * This callback is supposed to deal with mc filter in 1097f271b2ccSMax Krasnyansky * _rx_ path and has nothing to do with the _tx_ path. 1098f271b2ccSMax Krasnyansky * In rx path we always accept everything userspace gives us. 1099f271b2ccSMax Krasnyansky */ 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds 1102c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev, 1103c8f44affSMichał Mirosław netdev_features_t features) 110488255375SMichał Mirosław { 110588255375SMichał Mirosław struct tun_struct *tun = netdev_priv(dev); 110688255375SMichał Mirosław 110788255375SMichał Mirosław return (features & tun->set_features) | (features & ~TUN_USER_FEATURES); 110888255375SMichał Mirosław } 1109eaea34b2SPaolo Abeni 1110eaea34b2SPaolo Abeni static void tun_set_headroom(struct net_device *dev, int new_hr) 1111eaea34b2SPaolo Abeni { 1112eaea34b2SPaolo Abeni struct tun_struct *tun = netdev_priv(dev); 1113eaea34b2SPaolo Abeni 1114eaea34b2SPaolo Abeni if (new_hr < NET_SKB_PAD) 1115eaea34b2SPaolo Abeni new_hr = NET_SKB_PAD; 1116eaea34b2SPaolo Abeni 1117eaea34b2SPaolo Abeni tun->align = new_hr; 1118eaea34b2SPaolo Abeni } 1119eaea34b2SPaolo Abeni 1120bc1f4470Sstephen hemminger static void 1121608b9977SPaolo Abeni tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 1122608b9977SPaolo Abeni { 1123608b9977SPaolo Abeni u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0; 1124608b9977SPaolo Abeni struct tun_struct *tun = netdev_priv(dev); 1125608b9977SPaolo Abeni struct tun_pcpu_stats *p; 1126608b9977SPaolo Abeni int i; 1127608b9977SPaolo Abeni 1128608b9977SPaolo Abeni for_each_possible_cpu(i) { 1129608b9977SPaolo Abeni u64 rxpackets, rxbytes, txpackets, txbytes; 1130608b9977SPaolo Abeni unsigned int start; 1131608b9977SPaolo Abeni 1132608b9977SPaolo Abeni p = per_cpu_ptr(tun->pcpu_stats, i); 1133608b9977SPaolo Abeni do { 1134608b9977SPaolo Abeni start = u64_stats_fetch_begin(&p->syncp); 11355260dd3eSEric Dumazet rxpackets = u64_stats_read(&p->rx_packets); 11365260dd3eSEric Dumazet rxbytes = u64_stats_read(&p->rx_bytes); 11375260dd3eSEric Dumazet txpackets = u64_stats_read(&p->tx_packets); 11385260dd3eSEric Dumazet txbytes = u64_stats_read(&p->tx_bytes); 1139608b9977SPaolo Abeni } while (u64_stats_fetch_retry(&p->syncp, start)); 1140608b9977SPaolo Abeni 1141608b9977SPaolo Abeni stats->rx_packets += rxpackets; 1142608b9977SPaolo Abeni stats->rx_bytes += rxbytes; 1143608b9977SPaolo Abeni stats->tx_packets += txpackets; 1144608b9977SPaolo Abeni stats->tx_bytes += txbytes; 1145608b9977SPaolo Abeni 1146608b9977SPaolo Abeni /* u32 counters */ 1147608b9977SPaolo Abeni rx_dropped += p->rx_dropped; 1148608b9977SPaolo Abeni rx_frame_errors += p->rx_frame_errors; 1149608b9977SPaolo Abeni tx_dropped += p->tx_dropped; 1150608b9977SPaolo Abeni } 1151608b9977SPaolo Abeni stats->rx_dropped = rx_dropped; 1152608b9977SPaolo Abeni stats->rx_frame_errors = rx_frame_errors; 1153608b9977SPaolo Abeni stats->tx_dropped = tx_dropped; 1154608b9977SPaolo Abeni } 1155608b9977SPaolo Abeni 1156761876c8SJason Wang static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog, 1157761876c8SJason Wang struct netlink_ext_ack *extack) 1158761876c8SJason Wang { 1159761876c8SJason Wang struct tun_struct *tun = netdev_priv(dev); 1160e4a2a304SJason Wang struct tun_file *tfile; 1161761876c8SJason Wang struct bpf_prog *old_prog; 1162e4a2a304SJason Wang int i; 1163761876c8SJason Wang 1164761876c8SJason Wang old_prog = rtnl_dereference(tun->xdp_prog); 1165761876c8SJason Wang rcu_assign_pointer(tun->xdp_prog, prog); 1166761876c8SJason Wang if (old_prog) 1167761876c8SJason Wang bpf_prog_put(old_prog); 1168761876c8SJason Wang 1169e4a2a304SJason Wang for (i = 0; i < tun->numqueues; i++) { 1170e4a2a304SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 1171e4a2a304SJason Wang if (prog) 1172e4a2a304SJason Wang sock_set_flag(&tfile->sk, SOCK_XDP); 1173e4a2a304SJason Wang else 1174e4a2a304SJason Wang sock_reset_flag(&tfile->sk, SOCK_XDP); 1175e4a2a304SJason Wang } 1176e4a2a304SJason Wang list_for_each_entry(tfile, &tun->disabled, next) { 1177e4a2a304SJason Wang if (prog) 1178e4a2a304SJason Wang sock_set_flag(&tfile->sk, SOCK_XDP); 1179e4a2a304SJason Wang else 1180e4a2a304SJason Wang sock_reset_flag(&tfile->sk, SOCK_XDP); 1181e4a2a304SJason Wang } 1182e4a2a304SJason Wang 1183761876c8SJason Wang return 0; 1184761876c8SJason Wang } 1185761876c8SJason Wang 1186761876c8SJason Wang static u32 tun_xdp_query(struct net_device *dev) 1187761876c8SJason Wang { 1188761876c8SJason Wang struct tun_struct *tun = netdev_priv(dev); 1189761876c8SJason Wang const struct bpf_prog *xdp_prog; 1190761876c8SJason Wang 1191761876c8SJason Wang xdp_prog = rtnl_dereference(tun->xdp_prog); 1192761876c8SJason Wang if (xdp_prog) 1193761876c8SJason Wang return xdp_prog->aux->id; 1194761876c8SJason Wang 1195761876c8SJason Wang return 0; 1196761876c8SJason Wang } 1197761876c8SJason Wang 1198f4e63525SJakub Kicinski static int tun_xdp(struct net_device *dev, struct netdev_bpf *xdp) 1199761876c8SJason Wang { 1200761876c8SJason Wang switch (xdp->command) { 1201761876c8SJason Wang case XDP_SETUP_PROG: 1202761876c8SJason Wang return tun_xdp_set(dev, xdp->prog, xdp->extack); 1203761876c8SJason Wang case XDP_QUERY_PROG: 1204761876c8SJason Wang xdp->prog_id = tun_xdp_query(dev); 1205761876c8SJason Wang return 0; 1206761876c8SJason Wang default: 1207761876c8SJason Wang return -EINVAL; 1208761876c8SJason Wang } 1209761876c8SJason Wang } 1210761876c8SJason Wang 121126d31925SNicolas Dichtel static int tun_net_change_carrier(struct net_device *dev, bool new_carrier) 121226d31925SNicolas Dichtel { 121326d31925SNicolas Dichtel if (new_carrier) { 121426d31925SNicolas Dichtel struct tun_struct *tun = netdev_priv(dev); 121526d31925SNicolas Dichtel 121626d31925SNicolas Dichtel if (!tun->numqueues) 121726d31925SNicolas Dichtel return -EPERM; 121826d31925SNicolas Dichtel 121926d31925SNicolas Dichtel netif_carrier_on(dev); 122026d31925SNicolas Dichtel } else { 122126d31925SNicolas Dichtel netif_carrier_off(dev); 122226d31925SNicolas Dichtel } 122326d31925SNicolas Dichtel return 0; 122426d31925SNicolas Dichtel } 122526d31925SNicolas Dichtel 1226758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = { 1227c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 1228758e43b7SStephen Hemminger .ndo_open = tun_net_open, 1229758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 123000829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 123188255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 1232c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 1233eaea34b2SPaolo Abeni .ndo_set_rx_headroom = tun_set_headroom, 1234608b9977SPaolo Abeni .ndo_get_stats64 = tun_net_get_stats64, 123526d31925SNicolas Dichtel .ndo_change_carrier = tun_net_change_carrier, 1236758e43b7SStephen Hemminger }; 1237758e43b7SStephen Hemminger 12380c9d917bSJesper Dangaard Brouer static void __tun_xdp_flush_tfile(struct tun_file *tfile) 12390c9d917bSJesper Dangaard Brouer { 12400c9d917bSJesper Dangaard Brouer /* Notify and wake up reader process */ 12410c9d917bSJesper Dangaard Brouer if (tfile->flags & TUN_FASYNC) 12420c9d917bSJesper Dangaard Brouer kill_fasync(&tfile->fasync, SIGIO, POLL_IN); 12430c9d917bSJesper Dangaard Brouer tfile->socket.sk->sk_data_ready(tfile->socket.sk); 12440c9d917bSJesper Dangaard Brouer } 12450c9d917bSJesper Dangaard Brouer 124642b33468SJesper Dangaard Brouer static int tun_xdp_xmit(struct net_device *dev, int n, 124742b33468SJesper Dangaard Brouer struct xdp_frame **frames, u32 flags) 1248fc72d1d5SJason Wang { 1249fc72d1d5SJason Wang struct tun_struct *tun = netdev_priv(dev); 1250fc72d1d5SJason Wang struct tun_file *tfile; 1251fc72d1d5SJason Wang u32 numqueues; 1252735fc405SJesper Dangaard Brouer int drops = 0; 1253735fc405SJesper Dangaard Brouer int cnt = n; 1254735fc405SJesper Dangaard Brouer int i; 1255fc72d1d5SJason Wang 12560c9d917bSJesper Dangaard Brouer if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) 125742b33468SJesper Dangaard Brouer return -EINVAL; 125842b33468SJesper Dangaard Brouer 1259fc72d1d5SJason Wang rcu_read_lock(); 1260fc72d1d5SJason Wang 12619871a9e4SJason Wang resample: 1262fc72d1d5SJason Wang numqueues = READ_ONCE(tun->numqueues); 1263fc72d1d5SJason Wang if (!numqueues) { 1264735fc405SJesper Dangaard Brouer rcu_read_unlock(); 1265735fc405SJesper Dangaard Brouer return -ENXIO; /* Caller will free/return all frames */ 1266fc72d1d5SJason Wang } 1267fc72d1d5SJason Wang 1268fc72d1d5SJason Wang tfile = rcu_dereference(tun->tfiles[smp_processor_id() % 1269fc72d1d5SJason Wang numqueues]); 12709871a9e4SJason Wang if (unlikely(!tfile)) 12719871a9e4SJason Wang goto resample; 1272735fc405SJesper Dangaard Brouer 1273735fc405SJesper Dangaard Brouer spin_lock(&tfile->tx_ring.producer_lock); 1274735fc405SJesper Dangaard Brouer for (i = 0; i < n; i++) { 1275735fc405SJesper Dangaard Brouer struct xdp_frame *xdp = frames[i]; 1276fc72d1d5SJason Wang /* Encode the XDP flag into lowest bit for consumer to differ 1277fc72d1d5SJason Wang * XDP buffer from sk_buff. 1278fc72d1d5SJason Wang */ 1279735fc405SJesper Dangaard Brouer void *frame = tun_xdp_to_ptr(xdp); 1280fc72d1d5SJason Wang 1281735fc405SJesper Dangaard Brouer if (__ptr_ring_produce(&tfile->tx_ring, frame)) { 1282735fc405SJesper Dangaard Brouer this_cpu_inc(tun->pcpu_stats->tx_dropped); 1283735fc405SJesper Dangaard Brouer xdp_return_frame_rx_napi(xdp); 1284735fc405SJesper Dangaard Brouer drops++; 1285735fc405SJesper Dangaard Brouer } 1286735fc405SJesper Dangaard Brouer } 1287735fc405SJesper Dangaard Brouer spin_unlock(&tfile->tx_ring.producer_lock); 1288735fc405SJesper Dangaard Brouer 12890c9d917bSJesper Dangaard Brouer if (flags & XDP_XMIT_FLUSH) 12900c9d917bSJesper Dangaard Brouer __tun_xdp_flush_tfile(tfile); 12910c9d917bSJesper Dangaard Brouer 1292fc72d1d5SJason Wang rcu_read_unlock(); 1293735fc405SJesper Dangaard Brouer return cnt - drops; 1294fc72d1d5SJason Wang } 1295fc72d1d5SJason Wang 129644fa2dbdSJesper Dangaard Brouer static int tun_xdp_tx(struct net_device *dev, struct xdp_buff *xdp) 129744fa2dbdSJesper Dangaard Brouer { 129844fa2dbdSJesper Dangaard Brouer struct xdp_frame *frame = convert_to_xdp_frame(xdp); 129944fa2dbdSJesper Dangaard Brouer 130044fa2dbdSJesper Dangaard Brouer if (unlikely(!frame)) 130144fa2dbdSJesper Dangaard Brouer return -EOVERFLOW; 130244fa2dbdSJesper Dangaard Brouer 130342421a56SJesper Dangaard Brouer return tun_xdp_xmit(dev, 1, &frame, XDP_XMIT_FLUSH); 1304fc72d1d5SJason Wang } 1305fc72d1d5SJason Wang 1306758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = { 1307c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 1308758e43b7SStephen Hemminger .ndo_open = tun_net_open, 1309758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 131000829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 131188255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 1312afc4b13dSJiri Pirko .ndo_set_rx_mode = tun_net_mclist, 1313758e43b7SStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 1314758e43b7SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 1315c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 13165e52796aSToshiaki Makita .ndo_features_check = passthru_features_check, 1317eaea34b2SPaolo Abeni .ndo_set_rx_headroom = tun_set_headroom, 1318608b9977SPaolo Abeni .ndo_get_stats64 = tun_net_get_stats64, 1319f4e63525SJakub Kicinski .ndo_bpf = tun_xdp, 1320fc72d1d5SJason Wang .ndo_xdp_xmit = tun_xdp_xmit, 132126d31925SNicolas Dichtel .ndo_change_carrier = tun_net_change_carrier, 1322758e43b7SStephen Hemminger }; 1323758e43b7SStephen Hemminger 1324944a1376SPavel Emelyanov static void tun_flow_init(struct tun_struct *tun) 132596442e42SJason Wang { 132696442e42SJason Wang int i; 132796442e42SJason Wang 132896442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) 132996442e42SJason Wang INIT_HLIST_HEAD(&tun->flows[i]); 133096442e42SJason Wang 133196442e42SJason Wang tun->ageing_time = TUN_FLOW_EXPIRE; 1332e99e88a9SKees Cook timer_setup(&tun->flow_gc_timer, tun_flow_cleanup, 0); 1333e99e88a9SKees Cook mod_timer(&tun->flow_gc_timer, 1334e99e88a9SKees Cook round_jiffies_up(jiffies + tun->ageing_time)); 133596442e42SJason Wang } 133696442e42SJason Wang 133796442e42SJason Wang static void tun_flow_uninit(struct tun_struct *tun) 133896442e42SJason Wang { 133996442e42SJason Wang del_timer_sync(&tun->flow_gc_timer); 134096442e42SJason Wang tun_flow_flush(tun); 134196442e42SJason Wang } 134296442e42SJason Wang 134391572088SJarod Wilson #define MIN_MTU 68 134491572088SJarod Wilson #define MAX_MTU 65535 134591572088SJarod Wilson 13461da177e4SLinus Torvalds /* Initialize net device. */ 13471da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev) 13481da177e4SLinus Torvalds { 13491da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 135240630b82SMichael S. Tsirkin case IFF_TUN: 1353758e43b7SStephen Hemminger dev->netdev_ops = &tun_netdev_ops; 1354758e43b7SStephen Hemminger 13551da177e4SLinus Torvalds /* Point-to-Point TUN Device */ 13561da177e4SLinus Torvalds dev->hard_header_len = 0; 13571da177e4SLinus Torvalds dev->addr_len = 0; 13581da177e4SLinus Torvalds dev->mtu = 1500; 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds /* Zero header length */ 13611da177e4SLinus Torvalds dev->type = ARPHRD_NONE; 13621da177e4SLinus Torvalds dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 13631da177e4SLinus Torvalds break; 13641da177e4SLinus Torvalds 136540630b82SMichael S. Tsirkin case IFF_TAP: 13667a0a9608SKusanagi Kouichi dev->netdev_ops = &tap_netdev_ops; 13671da177e4SLinus Torvalds /* Ethernet TAP Device */ 13681da177e4SLinus Torvalds ether_setup(dev); 1369550fd08cSNeil Horman dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1370a676847bSstephen hemminger dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 137136226a8dSBrian Braunstein 1372f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 137336226a8dSBrian Braunstein 13741da177e4SLinus Torvalds break; 13751da177e4SLinus Torvalds } 137691572088SJarod Wilson 137791572088SJarod Wilson dev->min_mtu = MIN_MTU; 137891572088SJarod Wilson dev->max_mtu = MAX_MTU - dev->hard_header_len; 13791da177e4SLinus Torvalds } 13801da177e4SLinus Torvalds 13812f3ab622SJason Wang static bool tun_sock_writeable(struct tun_struct *tun, struct tun_file *tfile) 13822f3ab622SJason Wang { 13832f3ab622SJason Wang struct sock *sk = tfile->socket.sk; 13842f3ab622SJason Wang 13852f3ab622SJason Wang return (tun->dev->flags & IFF_UP) && sock_writeable(sk); 13862f3ab622SJason Wang } 13872f3ab622SJason Wang 13881da177e4SLinus Torvalds /* Character device part */ 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds /* Poll */ 1391afc9a42bSAl Viro static __poll_t tun_chr_poll(struct file *file, poll_table *wait) 13921da177e4SLinus Torvalds { 1393b2430de3SEric W. Biederman struct tun_file *tfile = file->private_data; 13949484dc74Syuan linyu struct tun_struct *tun = tun_get(tfile); 13953c8a9c63SMariusz Kozlowski struct sock *sk; 1396afc9a42bSAl Viro __poll_t mask = 0; 13971da177e4SLinus Torvalds 13981da177e4SLinus Torvalds if (!tun) 1399a9a08845SLinus Torvalds return EPOLLERR; 14001da177e4SLinus Torvalds 140154f968d6SJason Wang sk = tfile->socket.sk; 14023c8a9c63SMariusz Kozlowski 14039e641bdcSXi Wang poll_wait(file, sk_sleep(sk), wait); 14041da177e4SLinus Torvalds 14055990a305SJason Wang if (!ptr_ring_empty(&tfile->tx_ring)) 1406a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM; 14071da177e4SLinus Torvalds 14082f3ab622SJason Wang /* Make sure SOCKWQ_ASYNC_NOSPACE is set if not writable to 14092f3ab622SJason Wang * guarantee EPOLLOUT to be raised by either here or 14102f3ab622SJason Wang * tun_sock_write_space(). Then process could get notification 14112f3ab622SJason Wang * after it writes to a down device and meets -EIO. 14122f3ab622SJason Wang */ 14132f3ab622SJason Wang if (tun_sock_writeable(tun, tfile) || 14149cd3e072SEric Dumazet (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) && 14152f3ab622SJason Wang tun_sock_writeable(tun, tfile))) 1416a9a08845SLinus Torvalds mask |= EPOLLOUT | EPOLLWRNORM; 141733dccbb0SHerbert Xu 1418c70f1829SEric W. Biederman if (tun->dev->reg_state != NETREG_REGISTERED) 1419a9a08845SLinus Torvalds mask = EPOLLERR; 1420c70f1829SEric W. Biederman 1421631ab46bSEric W. Biederman tun_put(tun); 14221da177e4SLinus Torvalds return mask; 14231da177e4SLinus Torvalds } 14241da177e4SLinus Torvalds 142590e33d45SPetar Penkov static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile, 142690e33d45SPetar Penkov size_t len, 142790e33d45SPetar Penkov const struct iov_iter *it) 142890e33d45SPetar Penkov { 142990e33d45SPetar Penkov struct sk_buff *skb; 143090e33d45SPetar Penkov size_t linear; 143190e33d45SPetar Penkov int err; 143290e33d45SPetar Penkov int i; 143390e33d45SPetar Penkov 143490e33d45SPetar Penkov if (it->nr_segs > MAX_SKB_FRAGS + 1) 143590e33d45SPetar Penkov return ERR_PTR(-ENOMEM); 143690e33d45SPetar Penkov 143790e33d45SPetar Penkov local_bh_disable(); 143890e33d45SPetar Penkov skb = napi_get_frags(&tfile->napi); 143990e33d45SPetar Penkov local_bh_enable(); 144090e33d45SPetar Penkov if (!skb) 144190e33d45SPetar Penkov return ERR_PTR(-ENOMEM); 144290e33d45SPetar Penkov 144390e33d45SPetar Penkov linear = iov_iter_single_seg_count(it); 144490e33d45SPetar Penkov err = __skb_grow(skb, linear); 144590e33d45SPetar Penkov if (err) 144690e33d45SPetar Penkov goto free; 144790e33d45SPetar Penkov 144890e33d45SPetar Penkov skb->len = len; 144990e33d45SPetar Penkov skb->data_len = len - linear; 145090e33d45SPetar Penkov skb->truesize += skb->data_len; 145190e33d45SPetar Penkov 145290e33d45SPetar Penkov for (i = 1; i < it->nr_segs; i++) { 145390e33d45SPetar Penkov size_t fragsz = it->iov[i].iov_len; 1454aa6daacaSEric Dumazet struct page *page; 1455aa6daacaSEric Dumazet void *frag; 145690e33d45SPetar Penkov 145790e33d45SPetar Penkov if (fragsz == 0 || fragsz > PAGE_SIZE) { 145890e33d45SPetar Penkov err = -EINVAL; 145990e33d45SPetar Penkov goto free; 146090e33d45SPetar Penkov } 1461aa6daacaSEric Dumazet frag = netdev_alloc_frag(fragsz); 1462aa6daacaSEric Dumazet if (!frag) { 146390e33d45SPetar Penkov err = -ENOMEM; 146490e33d45SPetar Penkov goto free; 146590e33d45SPetar Penkov } 1466aa6daacaSEric Dumazet page = virt_to_head_page(frag); 1467aa6daacaSEric Dumazet skb_fill_page_desc(skb, i - 1, page, 1468aa6daacaSEric Dumazet frag - page_address(page), fragsz); 146990e33d45SPetar Penkov } 147090e33d45SPetar Penkov 147190e33d45SPetar Penkov return skb; 147290e33d45SPetar Penkov free: 147390e33d45SPetar Penkov /* frees skb and all frags allocated with napi_alloc_frag() */ 147490e33d45SPetar Penkov napi_free_frags(&tfile->napi); 147590e33d45SPetar Penkov return ERR_PTR(err); 147690e33d45SPetar Penkov } 147790e33d45SPetar Penkov 1478f42157cbSRusty Russell /* prepad is the amount to reserve at front. len is length after that. 1479f42157cbSRusty Russell * linear is a hint as to how much to copy (usually headers). */ 148054f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, 148133dccbb0SHerbert Xu size_t prepad, size_t len, 148233dccbb0SHerbert Xu size_t linear, int noblock) 1483f42157cbSRusty Russell { 148454f968d6SJason Wang struct sock *sk = tfile->socket.sk; 1485f42157cbSRusty Russell struct sk_buff *skb; 148633dccbb0SHerbert Xu int err; 1487f42157cbSRusty Russell 1488f42157cbSRusty Russell /* Under a page? Don't bother with paged skb. */ 14890eca93bcSHerbert Xu if (prepad + len < PAGE_SIZE || !linear) 149033dccbb0SHerbert Xu linear = len; 1491f42157cbSRusty Russell 149233dccbb0SHerbert Xu skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, 149328d64271SEric Dumazet &err, 0); 1494f42157cbSRusty Russell if (!skb) 149533dccbb0SHerbert Xu return ERR_PTR(err); 1496f42157cbSRusty Russell 1497f42157cbSRusty Russell skb_reserve(skb, prepad); 1498f42157cbSRusty Russell skb_put(skb, linear); 149933dccbb0SHerbert Xu skb->data_len = len - linear; 150033dccbb0SHerbert Xu skb->len += len - linear; 1501f42157cbSRusty Russell 1502f42157cbSRusty Russell return skb; 1503f42157cbSRusty Russell } 1504f42157cbSRusty Russell 15055503fcecSJason Wang static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile, 15065503fcecSJason Wang struct sk_buff *skb, int more) 15075503fcecSJason Wang { 15085503fcecSJason Wang struct sk_buff_head *queue = &tfile->sk.sk_write_queue; 15095503fcecSJason Wang struct sk_buff_head process_queue; 15105503fcecSJason Wang u32 rx_batched = tun->rx_batched; 15115503fcecSJason Wang bool rcv = false; 15125503fcecSJason Wang 15135503fcecSJason Wang if (!rx_batched || (!more && skb_queue_empty(queue))) { 15145503fcecSJason Wang local_bh_disable(); 15158ebebcbaSMatthew Cover skb_record_rx_queue(skb, tfile->queue_index); 15165503fcecSJason Wang netif_receive_skb(skb); 15175503fcecSJason Wang local_bh_enable(); 15185503fcecSJason Wang return; 15195503fcecSJason Wang } 15205503fcecSJason Wang 15215503fcecSJason Wang spin_lock(&queue->lock); 15225503fcecSJason Wang if (!more || skb_queue_len(queue) == rx_batched) { 15235503fcecSJason Wang __skb_queue_head_init(&process_queue); 15245503fcecSJason Wang skb_queue_splice_tail_init(queue, &process_queue); 15255503fcecSJason Wang rcv = true; 15265503fcecSJason Wang } else { 15275503fcecSJason Wang __skb_queue_tail(queue, skb); 15285503fcecSJason Wang } 15295503fcecSJason Wang spin_unlock(&queue->lock); 15305503fcecSJason Wang 15315503fcecSJason Wang if (rcv) { 15325503fcecSJason Wang struct sk_buff *nskb; 15335503fcecSJason Wang 15345503fcecSJason Wang local_bh_disable(); 15358ebebcbaSMatthew Cover while ((nskb = __skb_dequeue(&process_queue))) { 15368ebebcbaSMatthew Cover skb_record_rx_queue(nskb, tfile->queue_index); 15375503fcecSJason Wang netif_receive_skb(nskb); 15388ebebcbaSMatthew Cover } 15398ebebcbaSMatthew Cover skb_record_rx_queue(skb, tfile->queue_index); 15405503fcecSJason Wang netif_receive_skb(skb); 15415503fcecSJason Wang local_bh_enable(); 15425503fcecSJason Wang } 15435503fcecSJason Wang } 15445503fcecSJason Wang 154566ccbc9cSJason Wang static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile, 154666ccbc9cSJason Wang int len, int noblock, bool zerocopy) 154766ccbc9cSJason Wang { 154866ccbc9cSJason Wang if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 154966ccbc9cSJason Wang return false; 155066ccbc9cSJason Wang 155166ccbc9cSJason Wang if (tfile->socket.sk->sk_sndbuf != INT_MAX) 155266ccbc9cSJason Wang return false; 155366ccbc9cSJason Wang 155466ccbc9cSJason Wang if (!noblock) 155566ccbc9cSJason Wang return false; 155666ccbc9cSJason Wang 155766ccbc9cSJason Wang if (zerocopy) 155866ccbc9cSJason Wang return false; 155966ccbc9cSJason Wang 156066ccbc9cSJason Wang if (SKB_DATA_ALIGN(len + TUN_RX_PAD) + 156166ccbc9cSJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE) 156266ccbc9cSJason Wang return false; 156366ccbc9cSJason Wang 156466ccbc9cSJason Wang return true; 156566ccbc9cSJason Wang } 156666ccbc9cSJason Wang 15674b663366SAlexis Bauvin static struct sk_buff *__tun_build_skb(struct tun_file *tfile, 15684b663366SAlexis Bauvin struct page_frag *alloc_frag, char *buf, 15698ae1aff0SJason Wang int buflen, int len, int pad) 1570ac1f1f6cSJason Wang { 1571ac1f1f6cSJason Wang struct sk_buff *skb = build_skb(buf, buflen); 1572ac1f1f6cSJason Wang 1573ac1f1f6cSJason Wang if (!skb) 1574ac1f1f6cSJason Wang return ERR_PTR(-ENOMEM); 1575ac1f1f6cSJason Wang 15768ae1aff0SJason Wang skb_reserve(skb, pad); 1577ac1f1f6cSJason Wang skb_put(skb, len); 15784b663366SAlexis Bauvin skb_set_owner_w(skb, tfile->socket.sk); 1579ac1f1f6cSJason Wang 1580ac1f1f6cSJason Wang get_page(alloc_frag->page); 1581ac1f1f6cSJason Wang alloc_frag->offset += buflen; 1582ac1f1f6cSJason Wang 1583ac1f1f6cSJason Wang return skb; 1584ac1f1f6cSJason Wang } 1585ac1f1f6cSJason Wang 15868ae1aff0SJason Wang static int tun_xdp_act(struct tun_struct *tun, struct bpf_prog *xdp_prog, 15878ae1aff0SJason Wang struct xdp_buff *xdp, u32 act) 15888ae1aff0SJason Wang { 15898ae1aff0SJason Wang int err; 15908ae1aff0SJason Wang 15918ae1aff0SJason Wang switch (act) { 15928ae1aff0SJason Wang case XDP_REDIRECT: 15938ae1aff0SJason Wang err = xdp_do_redirect(tun->dev, xdp, xdp_prog); 15948ae1aff0SJason Wang if (err) 15958ae1aff0SJason Wang return err; 15968ae1aff0SJason Wang break; 15978ae1aff0SJason Wang case XDP_TX: 15988ae1aff0SJason Wang err = tun_xdp_tx(tun->dev, xdp); 15998ae1aff0SJason Wang if (err < 0) 16008ae1aff0SJason Wang return err; 16018ae1aff0SJason Wang break; 16028ae1aff0SJason Wang case XDP_PASS: 16038ae1aff0SJason Wang break; 16048ae1aff0SJason Wang default: 16058ae1aff0SJason Wang bpf_warn_invalid_xdp_action(act); 16068ae1aff0SJason Wang /* fall through */ 16078ae1aff0SJason Wang case XDP_ABORTED: 16088ae1aff0SJason Wang trace_xdp_exception(tun->dev, xdp_prog, act); 16098ae1aff0SJason Wang /* fall through */ 16108ae1aff0SJason Wang case XDP_DROP: 16118ae1aff0SJason Wang this_cpu_inc(tun->pcpu_stats->rx_dropped); 16128ae1aff0SJason Wang break; 16138ae1aff0SJason Wang } 16148ae1aff0SJason Wang 16158ae1aff0SJason Wang return act; 16168ae1aff0SJason Wang } 16178ae1aff0SJason Wang 1618761876c8SJason Wang static struct sk_buff *tun_build_skb(struct tun_struct *tun, 1619761876c8SJason Wang struct tun_file *tfile, 162066ccbc9cSJason Wang struct iov_iter *from, 1621761876c8SJason Wang struct virtio_net_hdr *hdr, 16221cfe6e93SJason Wang int len, int *skb_xdp) 162366ccbc9cSJason Wang { 16240bbd7dadSEric Dumazet struct page_frag *alloc_frag = ¤t->task_frag; 1625761876c8SJason Wang struct bpf_prog *xdp_prog; 16267df13219SJason Wang int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 162766ccbc9cSJason Wang char *buf; 162866ccbc9cSJason Wang size_t copied; 16298ae1aff0SJason Wang int pad = TUN_RX_PAD; 16308ae1aff0SJason Wang int err = 0; 16317df13219SJason Wang 16327df13219SJason Wang rcu_read_lock(); 16337df13219SJason Wang xdp_prog = rcu_dereference(tun->xdp_prog); 16347df13219SJason Wang if (xdp_prog) 16354f23aff8SJason Wang pad += XDP_PACKET_HEADROOM; 16367df13219SJason Wang buflen += SKB_DATA_ALIGN(len + pad); 16377df13219SJason Wang rcu_read_unlock(); 163866ccbc9cSJason Wang 163963b9ab65SJason Wang alloc_frag->offset = ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES); 164066ccbc9cSJason Wang if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL))) 164166ccbc9cSJason Wang return ERR_PTR(-ENOMEM); 164266ccbc9cSJason Wang 164366ccbc9cSJason Wang buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 164466ccbc9cSJason Wang copied = copy_page_from_iter(alloc_frag->page, 16457df13219SJason Wang alloc_frag->offset + pad, 164666ccbc9cSJason Wang len, from); 164766ccbc9cSJason Wang if (copied != len) 164866ccbc9cSJason Wang return ERR_PTR(-EFAULT); 164966ccbc9cSJason Wang 16507df13219SJason Wang /* There's a small window that XDP may be set after the check 16517df13219SJason Wang * of xdp_prog above, this should be rare and for simplicity 16527df13219SJason Wang * we do XDP on skb in case the headroom is not enough. 16537df13219SJason Wang */ 1654ac1f1f6cSJason Wang if (hdr->gso_type || !xdp_prog) { 16551cfe6e93SJason Wang *skb_xdp = 1; 16564b663366SAlexis Bauvin return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, 16574b663366SAlexis Bauvin pad); 1658ac1f1f6cSJason Wang } 1659ac1f1f6cSJason Wang 16601cfe6e93SJason Wang *skb_xdp = 0; 166166ccbc9cSJason Wang 16626547e387SToshiaki Makita local_bh_disable(); 1663761876c8SJason Wang rcu_read_lock(); 1664761876c8SJason Wang xdp_prog = rcu_dereference(tun->xdp_prog); 16658ae1aff0SJason Wang if (xdp_prog) { 1666761876c8SJason Wang struct xdp_buff xdp; 1667761876c8SJason Wang u32 act; 1668761876c8SJason Wang 1669761876c8SJason Wang xdp.data_hard_start = buf; 16707df13219SJason Wang xdp.data = buf + pad; 1671de8f3a83SDaniel Borkmann xdp_set_data_meta_invalid(&xdp); 1672761876c8SJason Wang xdp.data_end = xdp.data + len; 16738bf5c4eeSJesper Dangaard Brouer xdp.rxq = &tfile->xdp_rxq; 1674*fb3e6e93SJesper Dangaard Brouer xdp.frame_sz = buflen; 1675761876c8SJason Wang 16768ae1aff0SJason Wang act = bpf_prog_run_xdp(xdp_prog, &xdp); 16778ae1aff0SJason Wang if (act == XDP_REDIRECT || act == XDP_TX) { 1678761876c8SJason Wang get_page(alloc_frag->page); 1679761876c8SJason Wang alloc_frag->offset += buflen; 1680761876c8SJason Wang } 16818ae1aff0SJason Wang err = tun_xdp_act(tun, xdp_prog, &xdp, act); 1682bee34890SWill Deacon if (err < 0) { 1683bee34890SWill Deacon if (act == XDP_REDIRECT || act == XDP_TX) 1684bee34890SWill Deacon put_page(alloc_frag->page); 1685bee34890SWill Deacon goto out; 1686bee34890SWill Deacon } 1687bee34890SWill Deacon 16881a097910SJason Wang if (err == XDP_REDIRECT) 16891d233886SToke Høiland-Jørgensen xdp_do_flush(); 16908ae1aff0SJason Wang if (err != XDP_PASS) 16918ae1aff0SJason Wang goto out; 16928ae1aff0SJason Wang 16938ae1aff0SJason Wang pad = xdp.data - xdp.data_hard_start; 16948ae1aff0SJason Wang len = xdp.data_end - xdp.data; 1695761876c8SJason Wang } 1696761876c8SJason Wang rcu_read_unlock(); 16976547e387SToshiaki Makita local_bh_enable(); 1698291aeb2bSJason Wang 16994b663366SAlexis Bauvin return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, pad); 1700761876c8SJason Wang 1701f7053b6cSJason Wang out: 1702761876c8SJason Wang rcu_read_unlock(); 17036547e387SToshiaki Makita local_bh_enable(); 1704761876c8SJason Wang return NULL; 170566ccbc9cSJason Wang } 170666ccbc9cSJason Wang 17071da177e4SLinus Torvalds /* Get packet from user space buffer */ 170854f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, 1709f5ff53b4SAl Viro void *msg_control, struct iov_iter *from, 17105503fcecSJason Wang int noblock, bool more) 17111da177e4SLinus Torvalds { 171209640e63SHarvey Harrison struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; 17131da177e4SLinus Torvalds struct sk_buff *skb; 1714f5ff53b4SAl Viro size_t total_len = iov_iter_count(from); 1715eaea34b2SPaolo Abeni size_t len = total_len, align = tun->align, linear; 1716f43798c2SRusty Russell struct virtio_net_hdr gso = { 0 }; 1717608b9977SPaolo Abeni struct tun_pcpu_stats *stats; 171896f8d9ecSJason Wang int good_linear; 17190690899bSMichael S. Tsirkin int copylen; 17200690899bSMichael S. Tsirkin bool zerocopy = false; 17210690899bSMichael S. Tsirkin int err; 172296f84061SJason Wang u32 rxhash = 0; 17231cfe6e93SJason Wang int skb_xdp = 1; 1724af3fb24eSEric Dumazet bool frags = tun_napi_frags_enabled(tfile); 17251da177e4SLinus Torvalds 172640630b82SMichael S. Tsirkin if (!(tun->flags & IFF_NO_PI)) { 172715718ea0SDan Carpenter if (len < sizeof(pi)) 17281da177e4SLinus Torvalds return -EINVAL; 172915718ea0SDan Carpenter len -= sizeof(pi); 17301da177e4SLinus Torvalds 1731cbbd26b8SAl Viro if (!copy_from_iter_full(&pi, sizeof(pi), from)) 17321da177e4SLinus Torvalds return -EFAULT; 17331da177e4SLinus Torvalds } 17341da177e4SLinus Torvalds 173540630b82SMichael S. Tsirkin if (tun->flags & IFF_VNET_HDR) { 1736e1edab87SWillem de Bruijn int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); 1737e1edab87SWillem de Bruijn 1738e1edab87SWillem de Bruijn if (len < vnet_hdr_sz) 1739f43798c2SRusty Russell return -EINVAL; 1740e1edab87SWillem de Bruijn len -= vnet_hdr_sz; 1741f43798c2SRusty Russell 1742cbbd26b8SAl Viro if (!copy_from_iter_full(&gso, sizeof(gso), from)) 1743f43798c2SRusty Russell return -EFAULT; 1744f43798c2SRusty Russell 17454909122fSHerbert Xu if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && 174656f0dcc5SMichael S. Tsirkin tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len)) 174756f0dcc5SMichael S. Tsirkin gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2); 17484909122fSHerbert Xu 174956f0dcc5SMichael S. Tsirkin if (tun16_to_cpu(tun, gso.hdr_len) > len) 1750f43798c2SRusty Russell return -EINVAL; 1751e1edab87SWillem de Bruijn iov_iter_advance(from, vnet_hdr_sz - sizeof(gso)); 1752f43798c2SRusty Russell } 1753f43798c2SRusty Russell 175440630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) { 1755a504b86eSstephen hemminger align += NET_IP_ALIGN; 17560eca93bcSHerbert Xu if (unlikely(len < ETH_HLEN || 175756f0dcc5SMichael S. Tsirkin (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN))) 1758e01bf1c8SRusty Russell return -EINVAL; 1759e01bf1c8SRusty Russell } 17601da177e4SLinus Torvalds 176196f8d9ecSJason Wang good_linear = SKB_MAX_HEAD(align); 176296f8d9ecSJason Wang 176388529176SJason Wang if (msg_control) { 1764f5ff53b4SAl Viro struct iov_iter i = *from; 1765f5ff53b4SAl Viro 176688529176SJason Wang /* There are 256 bytes to be copied in skb, so there is 176788529176SJason Wang * enough room for skb expand head in case it is used. 17680690899bSMichael S. Tsirkin * The rest of the buffer is mapped from userspace. 17690690899bSMichael S. Tsirkin */ 177056f0dcc5SMichael S. Tsirkin copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN; 177196f8d9ecSJason Wang if (copylen > good_linear) 177296f8d9ecSJason Wang copylen = good_linear; 17733dd5c330SJason Wang linear = copylen; 1774f5ff53b4SAl Viro iov_iter_advance(&i, copylen); 1775f5ff53b4SAl Viro if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS) 177688529176SJason Wang zerocopy = true; 177788529176SJason Wang } 177888529176SJason Wang 177990e33d45SPetar Penkov if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) { 17801cfe6e93SJason Wang /* For the packet that is not easy to be processed 17811cfe6e93SJason Wang * (e.g gso or jumbo packet), we will do it at after 17821cfe6e93SJason Wang * skb was created with generic XDP routine. 17831cfe6e93SJason Wang */ 17841cfe6e93SJason Wang skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp); 178566ccbc9cSJason Wang if (IS_ERR(skb)) { 178666ccbc9cSJason Wang this_cpu_inc(tun->pcpu_stats->rx_dropped); 178766ccbc9cSJason Wang return PTR_ERR(skb); 178866ccbc9cSJason Wang } 1789761876c8SJason Wang if (!skb) 1790761876c8SJason Wang return total_len; 179166ccbc9cSJason Wang } else { 179288529176SJason Wang if (!zerocopy) { 17930690899bSMichael S. Tsirkin copylen = len; 179456f0dcc5SMichael S. Tsirkin if (tun16_to_cpu(tun, gso.hdr_len) > good_linear) 179596f8d9ecSJason Wang linear = good_linear; 179696f8d9ecSJason Wang else 179756f0dcc5SMichael S. Tsirkin linear = tun16_to_cpu(tun, gso.hdr_len); 17983dd5c330SJason Wang } 17990690899bSMichael S. Tsirkin 180090e33d45SPetar Penkov if (frags) { 180190e33d45SPetar Penkov mutex_lock(&tfile->napi_mutex); 180290e33d45SPetar Penkov skb = tun_napi_alloc_frags(tfile, copylen, from); 180390e33d45SPetar Penkov /* tun_napi_alloc_frags() enforces a layout for the skb. 180490e33d45SPetar Penkov * If zerocopy is enabled, then this layout will be 180590e33d45SPetar Penkov * overwritten by zerocopy_sg_from_iter(). 180690e33d45SPetar Penkov */ 180790e33d45SPetar Penkov zerocopy = false; 180890e33d45SPetar Penkov } else { 180990e33d45SPetar Penkov skb = tun_alloc_skb(tfile, align, copylen, linear, 181090e33d45SPetar Penkov noblock); 181190e33d45SPetar Penkov } 181290e33d45SPetar Penkov 181333dccbb0SHerbert Xu if (IS_ERR(skb)) { 181433dccbb0SHerbert Xu if (PTR_ERR(skb) != -EAGAIN) 1815608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 181690e33d45SPetar Penkov if (frags) 181790e33d45SPetar Penkov mutex_unlock(&tfile->napi_mutex); 181833dccbb0SHerbert Xu return PTR_ERR(skb); 18191da177e4SLinus Torvalds } 18201da177e4SLinus Torvalds 18210690899bSMichael S. Tsirkin if (zerocopy) 1822f5ff53b4SAl Viro err = zerocopy_sg_from_iter(skb, from); 1823af1cc7a2SJason Wang else 1824f5ff53b4SAl Viro err = skb_copy_datagram_from_iter(skb, 0, from, len); 18250690899bSMichael S. Tsirkin 18260690899bSMichael S. Tsirkin if (err) { 18274477138fSEric Dumazet err = -EFAULT; 18284477138fSEric Dumazet drop: 1829608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 18308f22757eSDave Jones kfree_skb(skb); 183190e33d45SPetar Penkov if (frags) { 183290e33d45SPetar Penkov tfile->napi.skb = NULL; 183390e33d45SPetar Penkov mutex_unlock(&tfile->napi_mutex); 183490e33d45SPetar Penkov } 183590e33d45SPetar Penkov 18364477138fSEric Dumazet return err; 18378f22757eSDave Jones } 183866ccbc9cSJason Wang } 18391da177e4SLinus Torvalds 18403e9e40e7SJarno Rajahalme if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) { 1841df10db98SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_frame_errors); 1842df10db98SPaolo Abeni kfree_skb(skb); 184390e33d45SPetar Penkov if (frags) { 184490e33d45SPetar Penkov tfile->napi.skb = NULL; 184590e33d45SPetar Penkov mutex_unlock(&tfile->napi_mutex); 184690e33d45SPetar Penkov } 184790e33d45SPetar Penkov 1848df10db98SPaolo Abeni return -EINVAL; 1849df10db98SPaolo Abeni } 1850df10db98SPaolo Abeni 18511da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 185240630b82SMichael S. Tsirkin case IFF_TUN: 185340630b82SMichael S. Tsirkin if (tun->flags & IFF_NO_PI) { 18542580c4c1SAlexander Potapenko u8 ip_version = skb->len ? (skb->data[0] >> 4) : 0; 18552580c4c1SAlexander Potapenko 18562580c4c1SAlexander Potapenko switch (ip_version) { 18572580c4c1SAlexander Potapenko case 4: 1858f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IP); 1859f09f7ee2SAng Way Chuang break; 18602580c4c1SAlexander Potapenko case 6: 1861f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IPV6); 1862f09f7ee2SAng Way Chuang break; 1863f09f7ee2SAng Way Chuang default: 1864608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 1865f09f7ee2SAng Way Chuang kfree_skb(skb); 1866f09f7ee2SAng Way Chuang return -EINVAL; 1867f09f7ee2SAng Way Chuang } 1868f09f7ee2SAng Way Chuang } 1869f09f7ee2SAng Way Chuang 1870459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 18711da177e4SLinus Torvalds skb->protocol = pi.proto; 18724c13eb66SArnaldo Carvalho de Melo skb->dev = tun->dev; 18731da177e4SLinus Torvalds break; 187440630b82SMichael S. Tsirkin case IFF_TAP: 187590e33d45SPetar Penkov if (!frags) 18761da177e4SLinus Torvalds skb->protocol = eth_type_trans(skb, tun->dev); 18771da177e4SLinus Torvalds break; 18786403eab1SJoe Perches } 18791da177e4SLinus Torvalds 18800690899bSMichael S. Tsirkin /* copy skb_ubuf_info for callback when skb has no error */ 18810690899bSMichael S. Tsirkin if (zerocopy) { 18820690899bSMichael S. Tsirkin skb_shinfo(skb)->destructor_arg = msg_control; 18830690899bSMichael S. Tsirkin skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; 1884c9af6db4SPravin B Shelar skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; 1885af1cc7a2SJason Wang } else if (msg_control) { 1886af1cc7a2SJason Wang struct ubuf_info *uarg = msg_control; 1887af1cc7a2SJason Wang uarg->callback(uarg, false); 18880690899bSMichael S. Tsirkin } 18890690899bSMichael S. Tsirkin 189072f65107SVlad Yasevich skb_reset_network_header(skb); 1891d2aa125dSMaxim Mikityanskiy skb_probe_transport_header(skb); 18923fe260e0SGilberto Bertin skb_record_rx_queue(skb, tfile->queue_index); 189338502af7SJason Wang 18941cfe6e93SJason Wang if (skb_xdp) { 1895761876c8SJason Wang struct bpf_prog *xdp_prog; 1896761876c8SJason Wang int ret; 1897761876c8SJason Wang 18986547e387SToshiaki Makita local_bh_disable(); 1899761876c8SJason Wang rcu_read_lock(); 1900761876c8SJason Wang xdp_prog = rcu_dereference(tun->xdp_prog); 1901761876c8SJason Wang if (xdp_prog) { 1902761876c8SJason Wang ret = do_xdp_generic(xdp_prog, skb); 1903761876c8SJason Wang if (ret != XDP_PASS) { 1904761876c8SJason Wang rcu_read_unlock(); 19056547e387SToshiaki Makita local_bh_enable(); 19061efba987SEric Dumazet if (frags) { 19071efba987SEric Dumazet tfile->napi.skb = NULL; 19081efba987SEric Dumazet mutex_unlock(&tfile->napi_mutex); 19091efba987SEric Dumazet } 1910761876c8SJason Wang return total_len; 1911761876c8SJason Wang } 1912761876c8SJason Wang } 1913761876c8SJason Wang rcu_read_unlock(); 19146547e387SToshiaki Makita local_bh_enable(); 1915761876c8SJason Wang } 1916761876c8SJason Wang 1917cf1a1e07SPaolo Abeni /* Compute the costly rx hash only if needed for flow updates. 1918cf1a1e07SPaolo Abeni * We may get a very small possibility of OOO during switching, not 1919cf1a1e07SPaolo Abeni * worth to optimize. 1920cf1a1e07SPaolo Abeni */ 1921cf1a1e07SPaolo Abeni if (!rcu_access_pointer(tun->steering_prog) && tun->numqueues > 1 && 1922cf1a1e07SPaolo Abeni !tfile->detached) 1923feec084aSJason Wang rxhash = __skb_get_hash_symmetric(skb); 192494317099SPetar Penkov 19254477138fSEric Dumazet rcu_read_lock(); 19264477138fSEric Dumazet if (unlikely(!(tun->dev->flags & IFF_UP))) { 19274477138fSEric Dumazet err = -EIO; 19289180bb4fSEric Dumazet rcu_read_unlock(); 19294477138fSEric Dumazet goto drop; 19304477138fSEric Dumazet } 19314477138fSEric Dumazet 193290e33d45SPetar Penkov if (frags) { 193390e33d45SPetar Penkov /* Exercise flow dissector code path. */ 1934c43f1255SStanislav Fomichev u32 headlen = eth_get_headlen(tun->dev, skb->data, 1935c43f1255SStanislav Fomichev skb_headlen(skb)); 193690e33d45SPetar Penkov 1937010f245bSEric Dumazet if (unlikely(headlen > skb_headlen(skb))) { 193890e33d45SPetar Penkov this_cpu_inc(tun->pcpu_stats->rx_dropped); 193990e33d45SPetar Penkov napi_free_frags(&tfile->napi); 19404477138fSEric Dumazet rcu_read_unlock(); 194190e33d45SPetar Penkov mutex_unlock(&tfile->napi_mutex); 194290e33d45SPetar Penkov WARN_ON(1); 194390e33d45SPetar Penkov return -ENOMEM; 194490e33d45SPetar Penkov } 194590e33d45SPetar Penkov 194690e33d45SPetar Penkov local_bh_disable(); 194790e33d45SPetar Penkov napi_gro_frags(&tfile->napi); 194890e33d45SPetar Penkov local_bh_enable(); 194990e33d45SPetar Penkov mutex_unlock(&tfile->napi_mutex); 1950aec72f33SEric Dumazet } else if (tfile->napi_enabled) { 195194317099SPetar Penkov struct sk_buff_head *queue = &tfile->sk.sk_write_queue; 195294317099SPetar Penkov int queue_len; 195394317099SPetar Penkov 195494317099SPetar Penkov spin_lock_bh(&queue->lock); 195594317099SPetar Penkov __skb_queue_tail(queue, skb); 195694317099SPetar Penkov queue_len = skb_queue_len(queue); 195794317099SPetar Penkov spin_unlock(&queue->lock); 195894317099SPetar Penkov 195994317099SPetar Penkov if (!more || queue_len > NAPI_POLL_WEIGHT) 196094317099SPetar Penkov napi_schedule(&tfile->napi); 196194317099SPetar Penkov 196294317099SPetar Penkov local_bh_enable(); 196394317099SPetar Penkov } else if (!IS_ENABLED(CONFIG_4KSTACKS)) { 19645503fcecSJason Wang tun_rx_batched(tun, tfile, skb, more); 196594317099SPetar Penkov } else { 19661da177e4SLinus Torvalds netif_rx_ni(skb); 196794317099SPetar Penkov } 19684477138fSEric Dumazet rcu_read_unlock(); 19691da177e4SLinus Torvalds 1970608b9977SPaolo Abeni stats = get_cpu_ptr(tun->pcpu_stats); 1971608b9977SPaolo Abeni u64_stats_update_begin(&stats->syncp); 19725260dd3eSEric Dumazet u64_stats_inc(&stats->rx_packets); 19735260dd3eSEric Dumazet u64_stats_add(&stats->rx_bytes, len); 1974608b9977SPaolo Abeni u64_stats_update_end(&stats->syncp); 1975608b9977SPaolo Abeni put_cpu_ptr(stats); 19761da177e4SLinus Torvalds 197796f84061SJason Wang if (rxhash) 19789e85722dSJason Wang tun_flow_update(tun, rxhash, tfile); 197996f84061SJason Wang 19800690899bSMichael S. Tsirkin return total_len; 19811da177e4SLinus Torvalds } 19821da177e4SLinus Torvalds 1983f5ff53b4SAl Viro static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from) 19841da177e4SLinus Torvalds { 198533dccbb0SHerbert Xu struct file *file = iocb->ki_filp; 198654f968d6SJason Wang struct tun_file *tfile = file->private_data; 19879484dc74Syuan linyu struct tun_struct *tun = tun_get(tfile); 1988631ab46bSEric W. Biederman ssize_t result; 19891da177e4SLinus Torvalds 19901da177e4SLinus Torvalds if (!tun) 19911da177e4SLinus Torvalds return -EBADFD; 19921da177e4SLinus Torvalds 19935503fcecSJason Wang result = tun_get_user(tun, tfile, NULL, from, 19945503fcecSJason Wang file->f_flags & O_NONBLOCK, false); 1995631ab46bSEric W. Biederman 1996631ab46bSEric W. Biederman tun_put(tun); 1997631ab46bSEric W. Biederman return result; 19981da177e4SLinus Torvalds } 19991da177e4SLinus Torvalds 2000fc72d1d5SJason Wang static ssize_t tun_put_user_xdp(struct tun_struct *tun, 2001fc72d1d5SJason Wang struct tun_file *tfile, 20021ffcbc85SJesper Dangaard Brouer struct xdp_frame *xdp_frame, 2003fc72d1d5SJason Wang struct iov_iter *iter) 2004fc72d1d5SJason Wang { 2005fc72d1d5SJason Wang int vnet_hdr_sz = 0; 20061ffcbc85SJesper Dangaard Brouer size_t size = xdp_frame->len; 2007fc72d1d5SJason Wang struct tun_pcpu_stats *stats; 2008fc72d1d5SJason Wang size_t ret; 2009fc72d1d5SJason Wang 2010fc72d1d5SJason Wang if (tun->flags & IFF_VNET_HDR) { 2011fc72d1d5SJason Wang struct virtio_net_hdr gso = { 0 }; 2012fc72d1d5SJason Wang 2013fc72d1d5SJason Wang vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); 2014fc72d1d5SJason Wang if (unlikely(iov_iter_count(iter) < vnet_hdr_sz)) 2015fc72d1d5SJason Wang return -EINVAL; 2016fc72d1d5SJason Wang if (unlikely(copy_to_iter(&gso, sizeof(gso), iter) != 2017fc72d1d5SJason Wang sizeof(gso))) 2018fc72d1d5SJason Wang return -EFAULT; 2019fc72d1d5SJason Wang iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso)); 2020fc72d1d5SJason Wang } 2021fc72d1d5SJason Wang 20221ffcbc85SJesper Dangaard Brouer ret = copy_to_iter(xdp_frame->data, size, iter) + vnet_hdr_sz; 2023fc72d1d5SJason Wang 2024fc72d1d5SJason Wang stats = get_cpu_ptr(tun->pcpu_stats); 2025fc72d1d5SJason Wang u64_stats_update_begin(&stats->syncp); 20265260dd3eSEric Dumazet u64_stats_inc(&stats->tx_packets); 20275260dd3eSEric Dumazet u64_stats_add(&stats->tx_bytes, ret); 2028fc72d1d5SJason Wang u64_stats_update_end(&stats->syncp); 2029fc72d1d5SJason Wang put_cpu_ptr(tun->pcpu_stats); 2030fc72d1d5SJason Wang 2031fc72d1d5SJason Wang return ret; 2032fc72d1d5SJason Wang } 2033fc72d1d5SJason Wang 20341da177e4SLinus Torvalds /* Put packet to the user space buffer */ 20356f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun, 203654f968d6SJason Wang struct tun_file *tfile, 20371da177e4SLinus Torvalds struct sk_buff *skb, 2038e0b46d0eSHerbert Xu struct iov_iter *iter) 20391da177e4SLinus Torvalds { 20401da177e4SLinus Torvalds struct tun_pi pi = { 0, skb->protocol }; 2041608b9977SPaolo Abeni struct tun_pcpu_stats *stats; 2042e0b46d0eSHerbert Xu ssize_t total; 20438c847d25SJason Wang int vlan_offset = 0; 2044a8f9bfdfSHerbert Xu int vlan_hlen = 0; 20452eb783c4SHerbert Xu int vnet_hdr_sz = 0; 2046a8f9bfdfSHerbert Xu 2047df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) 2048a8f9bfdfSHerbert Xu vlan_hlen = VLAN_HLEN; 20491da177e4SLinus Torvalds 205040630b82SMichael S. Tsirkin if (tun->flags & IFF_VNET_HDR) 2051e1edab87SWillem de Bruijn vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); 20521da177e4SLinus Torvalds 2053e0b46d0eSHerbert Xu total = skb->len + vlan_hlen + vnet_hdr_sz; 2054e0b46d0eSHerbert Xu 205540630b82SMichael S. Tsirkin if (!(tun->flags & IFF_NO_PI)) { 2056e0b46d0eSHerbert Xu if (iov_iter_count(iter) < sizeof(pi)) 20571da177e4SLinus Torvalds return -EINVAL; 20581da177e4SLinus Torvalds 2059e0b46d0eSHerbert Xu total += sizeof(pi); 2060e0b46d0eSHerbert Xu if (iov_iter_count(iter) < total) { 20611da177e4SLinus Torvalds /* Packet will be striped */ 20621da177e4SLinus Torvalds pi.flags |= TUN_PKT_STRIP; 20631da177e4SLinus Torvalds } 20641da177e4SLinus Torvalds 2065e0b46d0eSHerbert Xu if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi)) 20661da177e4SLinus Torvalds return -EFAULT; 20671da177e4SLinus Torvalds } 20681da177e4SLinus Torvalds 20692eb783c4SHerbert Xu if (vnet_hdr_sz) { 20709403cd7cSJarno Rajahalme struct virtio_net_hdr gso; 207134166093SMike Rapoport 2072e0b46d0eSHerbert Xu if (iov_iter_count(iter) < vnet_hdr_sz) 2073f43798c2SRusty Russell return -EINVAL; 2074f43798c2SRusty Russell 20753e9e40e7SJarno Rajahalme if (virtio_net_hdr_from_skb(skb, &gso, 2076fd3a8862SWillem de Bruijn tun_is_little_endian(tun), true, 2077fd3a8862SWillem de Bruijn vlan_hlen)) { 2078f43798c2SRusty Russell struct skb_shared_info *sinfo = skb_shinfo(skb); 20796b8a66eeSJoe Perches pr_err("unexpected GSO type: " 2080ef3db4a5SMichael S. Tsirkin "0x%x, gso_size %d, hdr_len %d\n", 208156f0dcc5SMichael S. Tsirkin sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size), 208256f0dcc5SMichael S. Tsirkin tun16_to_cpu(tun, gso.hdr_len)); 2083ef3db4a5SMichael S. Tsirkin print_hex_dump(KERN_ERR, "tun: ", 2084ef3db4a5SMichael S. Tsirkin DUMP_PREFIX_NONE, 2085ef3db4a5SMichael S. Tsirkin 16, 1, skb->head, 208656f0dcc5SMichael S. Tsirkin min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true); 2087ef3db4a5SMichael S. Tsirkin WARN_ON_ONCE(1); 2088ef3db4a5SMichael S. Tsirkin return -EINVAL; 2089ef3db4a5SMichael S. Tsirkin } 2090f43798c2SRusty Russell 2091e0b46d0eSHerbert Xu if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso)) 2092f43798c2SRusty Russell return -EFAULT; 20938c847d25SJason Wang 20948c847d25SJason Wang iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso)); 2095f43798c2SRusty Russell } 2096f43798c2SRusty Russell 2097a8f9bfdfSHerbert Xu if (vlan_hlen) { 2098e0b46d0eSHerbert Xu int ret; 2099aff3d70aSJason Wang struct veth veth; 21001da177e4SLinus Torvalds 21016680ec68SJason Wang veth.h_vlan_proto = skb->vlan_proto; 2102df8a39deSJiri Pirko veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb)); 21031da177e4SLinus Torvalds 21046680ec68SJason Wang vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); 21056680ec68SJason Wang 2106e0b46d0eSHerbert Xu ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset); 2107e0b46d0eSHerbert Xu if (ret || !iov_iter_count(iter)) 21086680ec68SJason Wang goto done; 21096680ec68SJason Wang 2110e0b46d0eSHerbert Xu ret = copy_to_iter(&veth, sizeof(veth), iter); 2111e0b46d0eSHerbert Xu if (ret != sizeof(veth) || !iov_iter_count(iter)) 21126680ec68SJason Wang goto done; 21136680ec68SJason Wang } 21146680ec68SJason Wang 2115e0b46d0eSHerbert Xu skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset); 21166680ec68SJason Wang 21176680ec68SJason Wang done: 2118608b9977SPaolo Abeni /* caller is in process context, */ 2119608b9977SPaolo Abeni stats = get_cpu_ptr(tun->pcpu_stats); 2120608b9977SPaolo Abeni u64_stats_update_begin(&stats->syncp); 21215260dd3eSEric Dumazet u64_stats_inc(&stats->tx_packets); 21225260dd3eSEric Dumazet u64_stats_add(&stats->tx_bytes, skb->len + vlan_hlen); 2123608b9977SPaolo Abeni u64_stats_update_end(&stats->syncp); 2124608b9977SPaolo Abeni put_cpu_ptr(tun->pcpu_stats); 21251da177e4SLinus Torvalds 21261da177e4SLinus Torvalds return total; 21271da177e4SLinus Torvalds } 21281da177e4SLinus Torvalds 2129fc72d1d5SJason Wang static void *tun_ring_recv(struct tun_file *tfile, int noblock, int *err) 21301576d986SJason Wang { 21311576d986SJason Wang DECLARE_WAITQUEUE(wait, current); 2132fc72d1d5SJason Wang void *ptr = NULL; 2133f48cc6b2SJason Wang int error = 0; 21341576d986SJason Wang 2135fc72d1d5SJason Wang ptr = ptr_ring_consume(&tfile->tx_ring); 2136fc72d1d5SJason Wang if (ptr) 21371576d986SJason Wang goto out; 21381576d986SJason Wang if (noblock) { 2139f48cc6b2SJason Wang error = -EAGAIN; 21401576d986SJason Wang goto out; 21411576d986SJason Wang } 21421576d986SJason Wang 2143333f7909SAl Viro add_wait_queue(&tfile->socket.wq.wait, &wait); 21441576d986SJason Wang 21451576d986SJason Wang while (1) { 214671828b22STimur Celik set_current_state(TASK_INTERRUPTIBLE); 2147fc72d1d5SJason Wang ptr = ptr_ring_consume(&tfile->tx_ring); 2148fc72d1d5SJason Wang if (ptr) 21491576d986SJason Wang break; 21501576d986SJason Wang if (signal_pending(current)) { 2151f48cc6b2SJason Wang error = -ERESTARTSYS; 21521576d986SJason Wang break; 21531576d986SJason Wang } 21541576d986SJason Wang if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) { 2155f48cc6b2SJason Wang error = -EFAULT; 21561576d986SJason Wang break; 21571576d986SJason Wang } 21581576d986SJason Wang 21591576d986SJason Wang schedule(); 21601576d986SJason Wang } 21611576d986SJason Wang 2162ecef67cbSTimur Celik __set_current_state(TASK_RUNNING); 2163333f7909SAl Viro remove_wait_queue(&tfile->socket.wq.wait, &wait); 21641576d986SJason Wang 21651576d986SJason Wang out: 2166f48cc6b2SJason Wang *err = error; 2167fc72d1d5SJason Wang return ptr; 21681576d986SJason Wang } 21691576d986SJason Wang 217054f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, 21719b067034SAl Viro struct iov_iter *to, 2172fc72d1d5SJason Wang int noblock, void *ptr) 21731da177e4SLinus Torvalds { 21749b067034SAl Viro ssize_t ret; 21751576d986SJason Wang int err; 21761da177e4SLinus Torvalds 2177c33ee15bSWei Xu if (!iov_iter_count(to)) { 2178fc72d1d5SJason Wang tun_ptr_free(ptr); 21799b067034SAl Viro return 0; 2180c33ee15bSWei Xu } 21811da177e4SLinus Torvalds 2182fc72d1d5SJason Wang if (!ptr) { 21831576d986SJason Wang /* Read frames from ring */ 2184fc72d1d5SJason Wang ptr = tun_ring_recv(tfile, noblock, &err); 2185fc72d1d5SJason Wang if (!ptr) 2186957f094fSAlex Gartrell return err; 2187ac77cfd4SJason Wang } 2188e0b46d0eSHerbert Xu 21891ffcbc85SJesper Dangaard Brouer if (tun_is_xdp_frame(ptr)) { 21901ffcbc85SJesper Dangaard Brouer struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr); 2191fc72d1d5SJason Wang 21921ffcbc85SJesper Dangaard Brouer ret = tun_put_user_xdp(tun, tfile, xdpf, to); 219303993094SJesper Dangaard Brouer xdp_return_frame(xdpf); 2194fc72d1d5SJason Wang } else { 2195fc72d1d5SJason Wang struct sk_buff *skb = ptr; 2196fc72d1d5SJason Wang 21979b067034SAl Viro ret = tun_put_user(tun, tfile, skb, to); 2198f51a5e82SJason Wang if (unlikely(ret < 0)) 21991da177e4SLinus Torvalds kfree_skb(skb); 2200f51a5e82SJason Wang else 2201f51a5e82SJason Wang consume_skb(skb); 2202fc72d1d5SJason Wang } 22031da177e4SLinus Torvalds 220405c2828cSMichael S. Tsirkin return ret; 220505c2828cSMichael S. Tsirkin } 220605c2828cSMichael S. Tsirkin 22079b067034SAl Viro static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to) 220805c2828cSMichael S. Tsirkin { 220905c2828cSMichael S. Tsirkin struct file *file = iocb->ki_filp; 221005c2828cSMichael S. Tsirkin struct tun_file *tfile = file->private_data; 22119484dc74Syuan linyu struct tun_struct *tun = tun_get(tfile); 22129b067034SAl Viro ssize_t len = iov_iter_count(to), ret; 221305c2828cSMichael S. Tsirkin 221405c2828cSMichael S. Tsirkin if (!tun) 221505c2828cSMichael S. Tsirkin return -EBADFD; 2216ac77cfd4SJason Wang ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK, NULL); 221742404c09SDavid S. Miller ret = min_t(ssize_t, ret, len); 2218d0b7da8aSZhi Yong Wu if (ret > 0) 2219d0b7da8aSZhi Yong Wu iocb->ki_pos = ret; 2220631ab46bSEric W. Biederman tun_put(tun); 22211da177e4SLinus Torvalds return ret; 22221da177e4SLinus Torvalds } 22231da177e4SLinus Torvalds 2224cd5681d7SJason Wang static void tun_prog_free(struct rcu_head *rcu) 222596f84061SJason Wang { 2226cd5681d7SJason Wang struct tun_prog *prog = container_of(rcu, struct tun_prog, rcu); 222796f84061SJason Wang 222896f84061SJason Wang bpf_prog_destroy(prog->prog); 222996f84061SJason Wang kfree(prog); 223096f84061SJason Wang } 223196f84061SJason Wang 22329d6474e4SJason Wang static int __tun_set_ebpf(struct tun_struct *tun, 22339d6474e4SJason Wang struct tun_prog __rcu **prog_p, 223496f84061SJason Wang struct bpf_prog *prog) 223596f84061SJason Wang { 2236cd5681d7SJason Wang struct tun_prog *old, *new = NULL; 223796f84061SJason Wang 223896f84061SJason Wang if (prog) { 223996f84061SJason Wang new = kmalloc(sizeof(*new), GFP_KERNEL); 224096f84061SJason Wang if (!new) 224196f84061SJason Wang return -ENOMEM; 224296f84061SJason Wang new->prog = prog; 224396f84061SJason Wang } 224496f84061SJason Wang 2245124da8f6SJason Wang spin_lock_bh(&tun->lock); 2246cd5681d7SJason Wang old = rcu_dereference_protected(*prog_p, 2247124da8f6SJason Wang lockdep_is_held(&tun->lock)); 2248cd5681d7SJason Wang rcu_assign_pointer(*prog_p, new); 2249124da8f6SJason Wang spin_unlock_bh(&tun->lock); 225096f84061SJason Wang 225196f84061SJason Wang if (old) 2252cd5681d7SJason Wang call_rcu(&old->rcu, tun_prog_free); 225396f84061SJason Wang 225496f84061SJason Wang return 0; 225596f84061SJason Wang } 225696f84061SJason Wang 225796442e42SJason Wang static void tun_free_netdev(struct net_device *dev) 225896442e42SJason Wang { 225996442e42SJason Wang struct tun_struct *tun = netdev_priv(dev); 226096442e42SJason Wang 22614008e97fSJason Wang BUG_ON(!(list_empty(&tun->disabled))); 226211fc7d5aSEric Dumazet 2263608b9977SPaolo Abeni free_percpu(tun->pcpu_stats); 226411fc7d5aSEric Dumazet /* We clear pcpu_stats so that tun_set_iff() can tell if 226511fc7d5aSEric Dumazet * tun_free_netdev() has been called from register_netdevice(). 226611fc7d5aSEric Dumazet */ 226711fc7d5aSEric Dumazet tun->pcpu_stats = NULL; 226811fc7d5aSEric Dumazet 226996442e42SJason Wang tun_flow_uninit(tun); 22705dbbaf2dSPaul Moore security_tun_dev_free_security(tun->security); 2271cd5681d7SJason Wang __tun_set_ebpf(tun, &tun->steering_prog, NULL); 2272aff3d70aSJason Wang __tun_set_ebpf(tun, &tun->filter_prog, NULL); 227396442e42SJason Wang } 227496442e42SJason Wang 22751da177e4SLinus Torvalds static void tun_setup(struct net_device *dev) 22761da177e4SLinus Torvalds { 22771da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 22781da177e4SLinus Torvalds 22790625c883SEric W. Biederman tun->owner = INVALID_UID; 22800625c883SEric W. Biederman tun->group = INVALID_GID; 22814e24f2ddSChas Williams tun_default_link_ksettings(dev, &tun->link_ksettings); 22821da177e4SLinus Torvalds 22831da177e4SLinus Torvalds dev->ethtool_ops = &tun_ethtool_ops; 2284cf124db5SDavid S. Miller dev->needs_free_netdev = true; 2285cf124db5SDavid S. Miller dev->priv_destructor = tun_free_netdev; 2286016adb72SJason Wang /* We prefer our own queue length */ 2287016adb72SJason Wang dev->tx_queue_len = TUN_READQ_SIZE; 22881da177e4SLinus Torvalds } 22891da177e4SLinus Torvalds 2290f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap 2291f019a7a5SEric W. Biederman * device with netlink. 2292f019a7a5SEric W. Biederman */ 2293a8b8a889SMatthias Schiffer static int tun_validate(struct nlattr *tb[], struct nlattr *data[], 2294a8b8a889SMatthias Schiffer struct netlink_ext_ack *extack) 2295f019a7a5SEric W. Biederman { 229635b827b6SNicolas Dichtel NL_SET_ERR_MSG(extack, 229735b827b6SNicolas Dichtel "tun/tap creation via rtnetlink is not supported."); 229835b827b6SNicolas Dichtel return -EOPNOTSUPP; 2299f019a7a5SEric W. Biederman } 2300f019a7a5SEric W. Biederman 23011ec010e7SSabrina Dubroca static size_t tun_get_size(const struct net_device *dev) 23021ec010e7SSabrina Dubroca { 23031ec010e7SSabrina Dubroca BUILD_BUG_ON(sizeof(u32) != sizeof(uid_t)); 23041ec010e7SSabrina Dubroca BUILD_BUG_ON(sizeof(u32) != sizeof(gid_t)); 23051ec010e7SSabrina Dubroca 23061ec010e7SSabrina Dubroca return nla_total_size(sizeof(uid_t)) + /* OWNER */ 23071ec010e7SSabrina Dubroca nla_total_size(sizeof(gid_t)) + /* GROUP */ 23081ec010e7SSabrina Dubroca nla_total_size(sizeof(u8)) + /* TYPE */ 23091ec010e7SSabrina Dubroca nla_total_size(sizeof(u8)) + /* PI */ 23101ec010e7SSabrina Dubroca nla_total_size(sizeof(u8)) + /* VNET_HDR */ 23111ec010e7SSabrina Dubroca nla_total_size(sizeof(u8)) + /* PERSIST */ 23121ec010e7SSabrina Dubroca nla_total_size(sizeof(u8)) + /* MULTI_QUEUE */ 23131ec010e7SSabrina Dubroca nla_total_size(sizeof(u32)) + /* NUM_QUEUES */ 23141ec010e7SSabrina Dubroca nla_total_size(sizeof(u32)) + /* NUM_DISABLED_QUEUES */ 23151ec010e7SSabrina Dubroca 0; 23161ec010e7SSabrina Dubroca } 23171ec010e7SSabrina Dubroca 23181ec010e7SSabrina Dubroca static int tun_fill_info(struct sk_buff *skb, const struct net_device *dev) 23191ec010e7SSabrina Dubroca { 23201ec010e7SSabrina Dubroca struct tun_struct *tun = netdev_priv(dev); 23211ec010e7SSabrina Dubroca 23221ec010e7SSabrina Dubroca if (nla_put_u8(skb, IFLA_TUN_TYPE, tun->flags & TUN_TYPE_MASK)) 23231ec010e7SSabrina Dubroca goto nla_put_failure; 23241ec010e7SSabrina Dubroca if (uid_valid(tun->owner) && 23251ec010e7SSabrina Dubroca nla_put_u32(skb, IFLA_TUN_OWNER, 23261ec010e7SSabrina Dubroca from_kuid_munged(current_user_ns(), tun->owner))) 23271ec010e7SSabrina Dubroca goto nla_put_failure; 23281ec010e7SSabrina Dubroca if (gid_valid(tun->group) && 23291ec010e7SSabrina Dubroca nla_put_u32(skb, IFLA_TUN_GROUP, 23301ec010e7SSabrina Dubroca from_kgid_munged(current_user_ns(), tun->group))) 23311ec010e7SSabrina Dubroca goto nla_put_failure; 23321ec010e7SSabrina Dubroca if (nla_put_u8(skb, IFLA_TUN_PI, !(tun->flags & IFF_NO_PI))) 23331ec010e7SSabrina Dubroca goto nla_put_failure; 23341ec010e7SSabrina Dubroca if (nla_put_u8(skb, IFLA_TUN_VNET_HDR, !!(tun->flags & IFF_VNET_HDR))) 23351ec010e7SSabrina Dubroca goto nla_put_failure; 23361ec010e7SSabrina Dubroca if (nla_put_u8(skb, IFLA_TUN_PERSIST, !!(tun->flags & IFF_PERSIST))) 23371ec010e7SSabrina Dubroca goto nla_put_failure; 23381ec010e7SSabrina Dubroca if (nla_put_u8(skb, IFLA_TUN_MULTI_QUEUE, 23391ec010e7SSabrina Dubroca !!(tun->flags & IFF_MULTI_QUEUE))) 23401ec010e7SSabrina Dubroca goto nla_put_failure; 23411ec010e7SSabrina Dubroca if (tun->flags & IFF_MULTI_QUEUE) { 23421ec010e7SSabrina Dubroca if (nla_put_u32(skb, IFLA_TUN_NUM_QUEUES, tun->numqueues)) 23431ec010e7SSabrina Dubroca goto nla_put_failure; 23441ec010e7SSabrina Dubroca if (nla_put_u32(skb, IFLA_TUN_NUM_DISABLED_QUEUES, 23451ec010e7SSabrina Dubroca tun->numdisabled)) 23461ec010e7SSabrina Dubroca goto nla_put_failure; 23471ec010e7SSabrina Dubroca } 23481ec010e7SSabrina Dubroca 23491ec010e7SSabrina Dubroca return 0; 23501ec010e7SSabrina Dubroca 23511ec010e7SSabrina Dubroca nla_put_failure: 23521ec010e7SSabrina Dubroca return -EMSGSIZE; 23531ec010e7SSabrina Dubroca } 23541ec010e7SSabrina Dubroca 2355f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = { 2356f019a7a5SEric W. Biederman .kind = DRV_NAME, 2357f019a7a5SEric W. Biederman .priv_size = sizeof(struct tun_struct), 2358f019a7a5SEric W. Biederman .setup = tun_setup, 2359f019a7a5SEric W. Biederman .validate = tun_validate, 23601ec010e7SSabrina Dubroca .get_size = tun_get_size, 23611ec010e7SSabrina Dubroca .fill_info = tun_fill_info, 2362f019a7a5SEric W. Biederman }; 2363f019a7a5SEric W. Biederman 236433dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk) 236533dccbb0SHerbert Xu { 236654f968d6SJason Wang struct tun_file *tfile; 236743815482SEric Dumazet wait_queue_head_t *wqueue; 236833dccbb0SHerbert Xu 236933dccbb0SHerbert Xu if (!sock_writeable(sk)) 237033dccbb0SHerbert Xu return; 237133dccbb0SHerbert Xu 23729cd3e072SEric Dumazet if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags)) 237333dccbb0SHerbert Xu return; 237433dccbb0SHerbert Xu 237543815482SEric Dumazet wqueue = sk_sleep(sk); 237643815482SEric Dumazet if (wqueue && waitqueue_active(wqueue)) 2377a9a08845SLinus Torvalds wake_up_interruptible_sync_poll(wqueue, EPOLLOUT | 2378a9a08845SLinus Torvalds EPOLLWRNORM | EPOLLWRBAND); 2379c722c625SHerbert Xu 238054f968d6SJason Wang tfile = container_of(sk, struct tun_file, sk); 238154f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); 238233dccbb0SHerbert Xu } 238333dccbb0SHerbert Xu 2384f9e06c45SJason Wang static void tun_put_page(struct tun_page *tpage) 2385f9e06c45SJason Wang { 2386f9e06c45SJason Wang if (tpage->page) 2387f9e06c45SJason Wang __page_frag_cache_drain(tpage->page, tpage->count); 2388f9e06c45SJason Wang } 2389f9e06c45SJason Wang 2390043d222fSJason Wang static int tun_xdp_one(struct tun_struct *tun, 2391043d222fSJason Wang struct tun_file *tfile, 2392f9e06c45SJason Wang struct xdp_buff *xdp, int *flush, 2393f9e06c45SJason Wang struct tun_page *tpage) 2394043d222fSJason Wang { 23954e4b08e5SPrashant Bhole unsigned int datasize = xdp->data_end - xdp->data; 2396043d222fSJason Wang struct tun_xdp_hdr *hdr = xdp->data_hard_start; 2397043d222fSJason Wang struct virtio_net_hdr *gso = &hdr->gso; 2398043d222fSJason Wang struct tun_pcpu_stats *stats; 2399043d222fSJason Wang struct bpf_prog *xdp_prog; 2400043d222fSJason Wang struct sk_buff *skb = NULL; 2401043d222fSJason Wang u32 rxhash = 0, act; 2402043d222fSJason Wang int buflen = hdr->buflen; 2403043d222fSJason Wang int err = 0; 2404043d222fSJason Wang bool skb_xdp = false; 2405f9e06c45SJason Wang struct page *page; 2406043d222fSJason Wang 2407043d222fSJason Wang xdp_prog = rcu_dereference(tun->xdp_prog); 2408043d222fSJason Wang if (xdp_prog) { 2409043d222fSJason Wang if (gso->gso_type) { 2410043d222fSJason Wang skb_xdp = true; 2411043d222fSJason Wang goto build; 2412043d222fSJason Wang } 2413043d222fSJason Wang xdp_set_data_meta_invalid(xdp); 2414043d222fSJason Wang xdp->rxq = &tfile->xdp_rxq; 2415*fb3e6e93SJesper Dangaard Brouer xdp->frame_sz = buflen; 2416043d222fSJason Wang 2417043d222fSJason Wang act = bpf_prog_run_xdp(xdp_prog, xdp); 2418043d222fSJason Wang err = tun_xdp_act(tun, xdp_prog, xdp, act); 2419043d222fSJason Wang if (err < 0) { 2420043d222fSJason Wang put_page(virt_to_head_page(xdp->data)); 2421043d222fSJason Wang return err; 2422043d222fSJason Wang } 2423043d222fSJason Wang 2424043d222fSJason Wang switch (err) { 2425043d222fSJason Wang case XDP_REDIRECT: 2426043d222fSJason Wang *flush = true; 2427043d222fSJason Wang /* fall through */ 2428043d222fSJason Wang case XDP_TX: 2429043d222fSJason Wang return 0; 2430043d222fSJason Wang case XDP_PASS: 2431043d222fSJason Wang break; 2432043d222fSJason Wang default: 2433f9e06c45SJason Wang page = virt_to_head_page(xdp->data); 2434f9e06c45SJason Wang if (tpage->page == page) { 2435f9e06c45SJason Wang ++tpage->count; 2436f9e06c45SJason Wang } else { 2437f9e06c45SJason Wang tun_put_page(tpage); 2438f9e06c45SJason Wang tpage->page = page; 2439f9e06c45SJason Wang tpage->count = 1; 2440f9e06c45SJason Wang } 2441043d222fSJason Wang return 0; 2442043d222fSJason Wang } 2443043d222fSJason Wang } 2444043d222fSJason Wang 2445043d222fSJason Wang build: 2446043d222fSJason Wang skb = build_skb(xdp->data_hard_start, buflen); 2447043d222fSJason Wang if (!skb) { 2448043d222fSJason Wang err = -ENOMEM; 2449043d222fSJason Wang goto out; 2450043d222fSJason Wang } 2451043d222fSJason Wang 2452043d222fSJason Wang skb_reserve(skb, xdp->data - xdp->data_hard_start); 2453043d222fSJason Wang skb_put(skb, xdp->data_end - xdp->data); 2454043d222fSJason Wang 2455043d222fSJason Wang if (virtio_net_hdr_to_skb(skb, gso, tun_is_little_endian(tun))) { 2456043d222fSJason Wang this_cpu_inc(tun->pcpu_stats->rx_frame_errors); 2457043d222fSJason Wang kfree_skb(skb); 2458043d222fSJason Wang err = -EINVAL; 2459043d222fSJason Wang goto out; 2460043d222fSJason Wang } 2461043d222fSJason Wang 2462043d222fSJason Wang skb->protocol = eth_type_trans(skb, tun->dev); 2463043d222fSJason Wang skb_reset_network_header(skb); 2464d2aa125dSMaxim Mikityanskiy skb_probe_transport_header(skb); 24653fe260e0SGilberto Bertin skb_record_rx_queue(skb, tfile->queue_index); 2466043d222fSJason Wang 2467043d222fSJason Wang if (skb_xdp) { 2468043d222fSJason Wang err = do_xdp_generic(xdp_prog, skb); 2469043d222fSJason Wang if (err != XDP_PASS) 2470043d222fSJason Wang goto out; 2471043d222fSJason Wang } 2472043d222fSJason Wang 2473f29eb2a9SPaolo Abeni if (!rcu_dereference(tun->steering_prog) && tun->numqueues > 1 && 2474f29eb2a9SPaolo Abeni !tfile->detached) 2475043d222fSJason Wang rxhash = __skb_get_hash_symmetric(skb); 2476043d222fSJason Wang 2477043d222fSJason Wang netif_receive_skb(skb); 2478043d222fSJason Wang 24796342ca64SPrashant Bhole /* No need for get_cpu_ptr() here since this function is 24806342ca64SPrashant Bhole * always called with bh disabled 24816342ca64SPrashant Bhole */ 24826342ca64SPrashant Bhole stats = this_cpu_ptr(tun->pcpu_stats); 2483043d222fSJason Wang u64_stats_update_begin(&stats->syncp); 24845260dd3eSEric Dumazet u64_stats_inc(&stats->rx_packets); 24855260dd3eSEric Dumazet u64_stats_add(&stats->rx_bytes, datasize); 2486043d222fSJason Wang u64_stats_update_end(&stats->syncp); 2487043d222fSJason Wang 2488043d222fSJason Wang if (rxhash) 2489043d222fSJason Wang tun_flow_update(tun, rxhash, tfile); 2490043d222fSJason Wang 2491043d222fSJason Wang out: 2492043d222fSJason Wang return err; 2493043d222fSJason Wang } 2494043d222fSJason Wang 24951b784140SYing Xue static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len) 249605c2828cSMichael S. Tsirkin { 2497043d222fSJason Wang int ret, i; 249854f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 24999484dc74Syuan linyu struct tun_struct *tun = tun_get(tfile); 2500fe8dd45bSJason Wang struct tun_msg_ctl *ctl = m->msg_control; 2501043d222fSJason Wang struct xdp_buff *xdp; 250254f968d6SJason Wang 250354f968d6SJason Wang if (!tun) 250454f968d6SJason Wang return -EBADFD; 2505f5ff53b4SAl Viro 2506043d222fSJason Wang if (ctl && (ctl->type == TUN_MSG_PTR)) { 25076f0271d9SDavid S. Miller struct tun_page tpage; 2508043d222fSJason Wang int n = ctl->num; 2509043d222fSJason Wang int flush = 0; 2510043d222fSJason Wang 25116f0271d9SDavid S. Miller memset(&tpage, 0, sizeof(tpage)); 25126f0271d9SDavid S. Miller 2513043d222fSJason Wang local_bh_disable(); 2514043d222fSJason Wang rcu_read_lock(); 2515043d222fSJason Wang 2516043d222fSJason Wang for (i = 0; i < n; i++) { 2517043d222fSJason Wang xdp = &((struct xdp_buff *)ctl->ptr)[i]; 2518f9e06c45SJason Wang tun_xdp_one(tun, tfile, xdp, &flush, &tpage); 2519043d222fSJason Wang } 2520043d222fSJason Wang 2521043d222fSJason Wang if (flush) 25221d233886SToke Høiland-Jørgensen xdp_do_flush(); 2523043d222fSJason Wang 2524043d222fSJason Wang rcu_read_unlock(); 2525043d222fSJason Wang local_bh_enable(); 2526043d222fSJason Wang 2527f9e06c45SJason Wang tun_put_page(&tpage); 2528f9e06c45SJason Wang 2529043d222fSJason Wang ret = total_len; 2530043d222fSJason Wang goto out; 2531043d222fSJason Wang } 2532fe8dd45bSJason Wang 2533fe8dd45bSJason Wang ret = tun_get_user(tun, tfile, ctl ? ctl->ptr : NULL, &m->msg_iter, 25345503fcecSJason Wang m->msg_flags & MSG_DONTWAIT, 25355503fcecSJason Wang m->msg_flags & MSG_MORE); 2536043d222fSJason Wang out: 253754f968d6SJason Wang tun_put(tun); 253854f968d6SJason Wang return ret; 253905c2828cSMichael S. Tsirkin } 254005c2828cSMichael S. Tsirkin 25411b784140SYing Xue static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, 254205c2828cSMichael S. Tsirkin int flags) 254305c2828cSMichael S. Tsirkin { 254454f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 25459484dc74Syuan linyu struct tun_struct *tun = tun_get(tfile); 2546fc72d1d5SJason Wang void *ptr = m->msg_control; 254705c2828cSMichael S. Tsirkin int ret; 254854f968d6SJason Wang 2549c33ee15bSWei Xu if (!tun) { 2550c33ee15bSWei Xu ret = -EBADFD; 2551fc72d1d5SJason Wang goto out_free; 2552c33ee15bSWei Xu } 255354f968d6SJason Wang 2554eda29772SRichard Cochran if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) { 25553811ae76SGao feng ret = -EINVAL; 2556c33ee15bSWei Xu goto out_put_tun; 25573811ae76SGao feng } 2558eda29772SRichard Cochran if (flags & MSG_ERRQUEUE) { 2559eda29772SRichard Cochran ret = sock_recv_errqueue(sock->sk, m, total_len, 2560eda29772SRichard Cochran SOL_PACKET, TUN_TX_TIMESTAMP); 2561eda29772SRichard Cochran goto out; 2562eda29772SRichard Cochran } 2563fc72d1d5SJason Wang ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, ptr); 256487897931SAlex Gartrell if (ret > (ssize_t)total_len) { 256542404c09SDavid S. Miller m->msg_flags |= MSG_TRUNC; 256642404c09SDavid S. Miller ret = flags & MSG_TRUNC ? ret : total_len; 256742404c09SDavid S. Miller } 25683811ae76SGao feng out: 256954f968d6SJason Wang tun_put(tun); 257005c2828cSMichael S. Tsirkin return ret; 2571c33ee15bSWei Xu 2572c33ee15bSWei Xu out_put_tun: 2573c33ee15bSWei Xu tun_put(tun); 2574fc72d1d5SJason Wang out_free: 2575fc72d1d5SJason Wang tun_ptr_free(ptr); 2576c33ee15bSWei Xu return ret; 257705c2828cSMichael S. Tsirkin } 257805c2828cSMichael S. Tsirkin 2579fc72d1d5SJason Wang static int tun_ptr_peek_len(void *ptr) 2580fc72d1d5SJason Wang { 2581fc72d1d5SJason Wang if (likely(ptr)) { 25821ffcbc85SJesper Dangaard Brouer if (tun_is_xdp_frame(ptr)) { 25831ffcbc85SJesper Dangaard Brouer struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr); 2584fc72d1d5SJason Wang 25851ffcbc85SJesper Dangaard Brouer return xdpf->len; 2586fc72d1d5SJason Wang } 2587fc72d1d5SJason Wang return __skb_array_len_with_tag(ptr); 2588fc72d1d5SJason Wang } else { 2589fc72d1d5SJason Wang return 0; 2590fc72d1d5SJason Wang } 2591fc72d1d5SJason Wang } 2592fc72d1d5SJason Wang 25931576d986SJason Wang static int tun_peek_len(struct socket *sock) 25941576d986SJason Wang { 25951576d986SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 25961576d986SJason Wang struct tun_struct *tun; 25971576d986SJason Wang int ret = 0; 25981576d986SJason Wang 25999484dc74Syuan linyu tun = tun_get(tfile); 26001576d986SJason Wang if (!tun) 26011576d986SJason Wang return 0; 26021576d986SJason Wang 2603fc72d1d5SJason Wang ret = PTR_RING_PEEK_CALL(&tfile->tx_ring, tun_ptr_peek_len); 26041576d986SJason Wang tun_put(tun); 26051576d986SJason Wang 26061576d986SJason Wang return ret; 26071576d986SJason Wang } 26081576d986SJason Wang 260905c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */ 261005c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = { 26111576d986SJason Wang .peek_len = tun_peek_len, 261205c2828cSMichael S. Tsirkin .sendmsg = tun_sendmsg, 261305c2828cSMichael S. Tsirkin .recvmsg = tun_recvmsg, 261405c2828cSMichael S. Tsirkin }; 261505c2828cSMichael S. Tsirkin 261633dccbb0SHerbert Xu static struct proto tun_proto = { 261733dccbb0SHerbert Xu .name = "tun", 261833dccbb0SHerbert Xu .owner = THIS_MODULE, 261954f968d6SJason Wang .obj_size = sizeof(struct tun_file), 262033dccbb0SHerbert Xu }; 2621f019a7a5SEric W. Biederman 2622980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun) 2623980c9e8cSDavid Woodhouse { 2624031f5e03SMichael S. Tsirkin return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP); 2625980c9e8cSDavid Woodhouse } 2626980c9e8cSDavid Woodhouse 2627980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr, 2628980c9e8cSDavid Woodhouse char *buf) 2629980c9e8cSDavid Woodhouse { 2630980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 2631980c9e8cSDavid Woodhouse return sprintf(buf, "0x%x\n", tun_flags(tun)); 2632980c9e8cSDavid Woodhouse } 2633980c9e8cSDavid Woodhouse 2634980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr, 2635980c9e8cSDavid Woodhouse char *buf) 2636980c9e8cSDavid Woodhouse { 2637980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 26380625c883SEric W. Biederman return uid_valid(tun->owner)? 26390625c883SEric W. Biederman sprintf(buf, "%u\n", 26400625c883SEric W. Biederman from_kuid_munged(current_user_ns(), tun->owner)): 26410625c883SEric W. Biederman sprintf(buf, "-1\n"); 2642980c9e8cSDavid Woodhouse } 2643980c9e8cSDavid Woodhouse 2644980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr, 2645980c9e8cSDavid Woodhouse char *buf) 2646980c9e8cSDavid Woodhouse { 2647980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 26480625c883SEric W. Biederman return gid_valid(tun->group) ? 26490625c883SEric W. Biederman sprintf(buf, "%u\n", 26500625c883SEric W. Biederman from_kgid_munged(current_user_ns(), tun->group)): 26510625c883SEric W. Biederman sprintf(buf, "-1\n"); 2652980c9e8cSDavid Woodhouse } 2653980c9e8cSDavid Woodhouse 2654980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL); 2655980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL); 2656980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL); 2657980c9e8cSDavid Woodhouse 2658c4d33e24STakashi Iwai static struct attribute *tun_dev_attrs[] = { 2659c4d33e24STakashi Iwai &dev_attr_tun_flags.attr, 2660c4d33e24STakashi Iwai &dev_attr_owner.attr, 2661c4d33e24STakashi Iwai &dev_attr_group.attr, 2662c4d33e24STakashi Iwai NULL 2663c4d33e24STakashi Iwai }; 2664c4d33e24STakashi Iwai 2665c4d33e24STakashi Iwai static const struct attribute_group tun_attr_group = { 2666c4d33e24STakashi Iwai .attrs = tun_dev_attrs 2667c4d33e24STakashi Iwai }; 2668c4d33e24STakashi Iwai 2669d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) 26701da177e4SLinus Torvalds { 26711da177e4SLinus Torvalds struct tun_struct *tun; 267254f968d6SJason Wang struct tun_file *tfile = file->private_data; 26731da177e4SLinus Torvalds struct net_device *dev; 26741da177e4SLinus Torvalds int err; 26751da177e4SLinus Torvalds 26767c0c3b1aSJason Wang if (tfile->detached) 26777c0c3b1aSJason Wang return -EINVAL; 26787c0c3b1aSJason Wang 267990e33d45SPetar Penkov if ((ifr->ifr_flags & IFF_NAPI_FRAGS)) { 268090e33d45SPetar Penkov if (!capable(CAP_NET_ADMIN)) 268190e33d45SPetar Penkov return -EPERM; 268290e33d45SPetar Penkov 268390e33d45SPetar Penkov if (!(ifr->ifr_flags & IFF_NAPI) || 268490e33d45SPetar Penkov (ifr->ifr_flags & TUN_TYPE_MASK) != IFF_TAP) 268590e33d45SPetar Penkov return -EINVAL; 268690e33d45SPetar Penkov } 268790e33d45SPetar Penkov 268874a3e5a7SEric W. Biederman dev = __dev_get_by_name(net, ifr->ifr_name); 268974a3e5a7SEric W. Biederman if (dev) { 2690f85ba780SDavid Woodhouse if (ifr->ifr_flags & IFF_TUN_EXCL) 2691f85ba780SDavid Woodhouse return -EBUSY; 269274a3e5a7SEric W. Biederman if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) 269374a3e5a7SEric W. Biederman tun = netdev_priv(dev); 269474a3e5a7SEric W. Biederman else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) 269574a3e5a7SEric W. Biederman tun = netdev_priv(dev); 269674a3e5a7SEric W. Biederman else 269774a3e5a7SEric W. Biederman return -EINVAL; 269874a3e5a7SEric W. Biederman 26998e6d91aeSJason Wang if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) != 270040630b82SMichael S. Tsirkin !!(tun->flags & IFF_MULTI_QUEUE)) 27018e6d91aeSJason Wang return -EINVAL; 27028e6d91aeSJason Wang 2703cde8b15fSJason Wang if (tun_not_capable(tun)) 27042b980dbdSPaul Moore return -EPERM; 27055dbbaf2dSPaul Moore err = security_tun_dev_open(tun->security); 27062b980dbdSPaul Moore if (err < 0) 27072b980dbdSPaul Moore return err; 27082b980dbdSPaul Moore 270994317099SPetar Penkov err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER, 2710af3fb24eSEric Dumazet ifr->ifr_flags & IFF_NAPI, 271177f22f92SYang Yingliang ifr->ifr_flags & IFF_NAPI_FRAGS, true); 2712a7385ba2SEric W. Biederman if (err < 0) 2713a7385ba2SEric W. Biederman return err; 27144008e97fSJason Wang 271540630b82SMichael S. Tsirkin if (tun->flags & IFF_MULTI_QUEUE && 2716e8dbad66SJason Wang (tun->numqueues + tun->numdisabled > 1)) { 2717e8dbad66SJason Wang /* One or more queue has already been attached, no need 2718e8dbad66SJason Wang * to initialize the device again. 2719e8dbad66SJason Wang */ 272083c1f36fSSabrina Dubroca netdev_state_change(dev); 2721e8dbad66SJason Wang return 0; 2722e8dbad66SJason Wang } 27239fffc5c6SSabrina Dubroca 27249fffc5c6SSabrina Dubroca tun->flags = (tun->flags & ~TUN_FEATURES) | 27259fffc5c6SSabrina Dubroca (ifr->ifr_flags & TUN_FEATURES); 272683c1f36fSSabrina Dubroca 272783c1f36fSSabrina Dubroca netdev_state_change(dev); 272883c1f36fSSabrina Dubroca } else { 27291da177e4SLinus Torvalds char *name; 27301da177e4SLinus Torvalds unsigned long flags = 0; 2731edfb6a14SJason Wang int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ? 2732edfb6a14SJason Wang MAX_TAP_QUEUES : 1; 27331da177e4SLinus Torvalds 2734c260b772SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 2735ca6bb5d7SDavid Woodhouse return -EPERM; 27362b980dbdSPaul Moore err = security_tun_dev_create(); 27372b980dbdSPaul Moore if (err < 0) 27382b980dbdSPaul Moore return err; 2739ca6bb5d7SDavid Woodhouse 27401da177e4SLinus Torvalds /* Set dev type */ 27411da177e4SLinus Torvalds if (ifr->ifr_flags & IFF_TUN) { 27421da177e4SLinus Torvalds /* TUN device */ 274340630b82SMichael S. Tsirkin flags |= IFF_TUN; 27441da177e4SLinus Torvalds name = "tun%d"; 27451da177e4SLinus Torvalds } else if (ifr->ifr_flags & IFF_TAP) { 27461da177e4SLinus Torvalds /* TAP device */ 274740630b82SMichael S. Tsirkin flags |= IFF_TAP; 27481da177e4SLinus Torvalds name = "tap%d"; 27491da177e4SLinus Torvalds } else 275036989b90SKusanagi Kouichi return -EINVAL; 27511da177e4SLinus Torvalds 27521da177e4SLinus Torvalds if (*ifr->ifr_name) 27531da177e4SLinus Torvalds name = ifr->ifr_name; 27541da177e4SLinus Torvalds 2755c8d68e6bSJason Wang dev = alloc_netdev_mqs(sizeof(struct tun_struct), name, 2756c835a677STom Gundersen NET_NAME_UNKNOWN, tun_setup, queues, 2757c835a677STom Gundersen queues); 2758edfb6a14SJason Wang 27591da177e4SLinus Torvalds if (!dev) 27601da177e4SLinus Torvalds return -ENOMEM; 27611da177e4SLinus Torvalds 2762fc54c658SPavel Emelyanov dev_net_set(dev, net); 2763f019a7a5SEric W. Biederman dev->rtnl_link_ops = &tun_link_ops; 2764fb7589a1SPavel Emelyanov dev->ifindex = tfile->ifindex; 2765c4d33e24STakashi Iwai dev->sysfs_groups[0] = &tun_attr_group; 2766758e43b7SStephen Hemminger 27671da177e4SLinus Torvalds tun = netdev_priv(dev); 27681da177e4SLinus Torvalds tun->dev = dev; 27691da177e4SLinus Torvalds tun->flags = flags; 2770f271b2ccSMax Krasnyansky tun->txflt.count = 0; 2771d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr); 27721da177e4SLinus Torvalds 2773eaea34b2SPaolo Abeni tun->align = NET_SKB_PAD; 277454f968d6SJason Wang tun->filter_attached = false; 277554f968d6SJason Wang tun->sndbuf = tfile->socket.sk->sk_sndbuf; 27765503fcecSJason Wang tun->rx_batched = 0; 277796f84061SJason Wang RCU_INIT_POINTER(tun->steering_prog, NULL); 277833dccbb0SHerbert Xu 2779608b9977SPaolo Abeni tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats); 2780608b9977SPaolo Abeni if (!tun->pcpu_stats) { 2781608b9977SPaolo Abeni err = -ENOMEM; 2782608b9977SPaolo Abeni goto err_free_dev; 2783608b9977SPaolo Abeni } 2784608b9977SPaolo Abeni 278596442e42SJason Wang spin_lock_init(&tun->lock); 278696442e42SJason Wang 27875dbbaf2dSPaul Moore err = security_tun_dev_alloc_security(&tun->security); 27885dbbaf2dSPaul Moore if (err < 0) 2789608b9977SPaolo Abeni goto err_free_stat; 27902b980dbdSPaul Moore 27911da177e4SLinus Torvalds tun_net_init(dev); 2792944a1376SPavel Emelyanov tun_flow_init(tun); 279396442e42SJason Wang 279488255375SMichał Mirosław dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | 27956680ec68SJason Wang TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX | 27966680ec68SJason Wang NETIF_F_HW_VLAN_STAG_TX; 27972a2bbf17SPaolo Abeni dev->features = dev->hw_features | NETIF_F_LLTX; 27986671b224SFernando Luis Vazquez Cao dev->vlan_features = dev->features & 27996671b224SFernando Luis Vazquez Cao ~(NETIF_F_HW_VLAN_CTAG_TX | 28006671b224SFernando Luis Vazquez Cao NETIF_F_HW_VLAN_STAG_TX); 280188255375SMichał Mirosław 28029fffc5c6SSabrina Dubroca tun->flags = (tun->flags & ~TUN_FEATURES) | 28039fffc5c6SSabrina Dubroca (ifr->ifr_flags & TUN_FEATURES); 28049fffc5c6SSabrina Dubroca 28054008e97fSJason Wang INIT_LIST_HEAD(&tun->disabled); 2806af3fb24eSEric Dumazet err = tun_attach(tun, file, false, ifr->ifr_flags & IFF_NAPI, 280777f22f92SYang Yingliang ifr->ifr_flags & IFF_NAPI_FRAGS, false); 2808eb0fb363SJason Wang if (err < 0) 2809662ca437SJason Wang goto err_free_flow; 2810eb0fb363SJason Wang 28111da177e4SLinus Torvalds err = register_netdevice(tun->dev); 28121da177e4SLinus Torvalds if (err < 0) 2813662ca437SJason Wang goto err_detach; 281477f22f92SYang Yingliang /* free_netdev() won't check refcnt, to aovid race 281577f22f92SYang Yingliang * with dev_put() we need publish tun after registration. 281677f22f92SYang Yingliang */ 281777f22f92SYang Yingliang rcu_assign_pointer(tfile->tun, tun); 2818af668b3cSMichael S. Tsirkin } 2819980c9e8cSDavid Woodhouse 2820eb0fb363SJason Wang netif_carrier_on(tun->dev); 28211da177e4SLinus Torvalds 2822e35259a9SMax Krasnyansky /* Make sure persistent devices do not get stuck in 2823e35259a9SMax Krasnyansky * xoff state. 2824e35259a9SMax Krasnyansky */ 2825e35259a9SMax Krasnyansky if (netif_running(tun->dev)) 2826c8d68e6bSJason Wang netif_tx_wake_all_queues(tun->dev); 2827e35259a9SMax Krasnyansky 28281da177e4SLinus Torvalds strcpy(ifr->ifr_name, tun->dev->name); 28291da177e4SLinus Torvalds return 0; 28301da177e4SLinus Torvalds 2831662ca437SJason Wang err_detach: 2832662ca437SJason Wang tun_detach_all(dev); 283311fc7d5aSEric Dumazet /* We are here because register_netdevice() has failed. 283411fc7d5aSEric Dumazet * If register_netdevice() already called tun_free_netdev() 283511fc7d5aSEric Dumazet * while dealing with the error, tun->pcpu_stats has been cleared. 283611fc7d5aSEric Dumazet */ 283711fc7d5aSEric Dumazet if (!tun->pcpu_stats) 2838ff244c6bSEric Dumazet goto err_free_dev; 2839ff244c6bSEric Dumazet 2840662ca437SJason Wang err_free_flow: 2841662ca437SJason Wang tun_flow_uninit(tun); 2842662ca437SJason Wang security_tun_dev_free_security(tun->security); 2843608b9977SPaolo Abeni err_free_stat: 2844608b9977SPaolo Abeni free_percpu(tun->pcpu_stats); 28451da177e4SLinus Torvalds err_free_dev: 28461da177e4SLinus Torvalds free_netdev(dev); 28471da177e4SLinus Torvalds return err; 28481da177e4SLinus Torvalds } 28491da177e4SLinus Torvalds 285012132768SKirill Tkhai static void tun_get_iff(struct tun_struct *tun, struct ifreq *ifr) 2851e3b99556SMark McLoughlin { 2852e3b99556SMark McLoughlin strcpy(ifr->ifr_name, tun->dev->name); 2853e3b99556SMark McLoughlin 2854980c9e8cSDavid Woodhouse ifr->ifr_flags = tun_flags(tun); 2855e3b99556SMark McLoughlin 2856e3b99556SMark McLoughlin } 2857e3b99556SMark McLoughlin 28585228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no 28595228ddc9SRusty Russell * privs required. */ 286088255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg) 28615228ddc9SRusty Russell { 2862c8f44affSMichał Mirosław netdev_features_t features = 0; 28635228ddc9SRusty Russell 28645228ddc9SRusty Russell if (arg & TUN_F_CSUM) { 286588255375SMichał Mirosław features |= NETIF_F_HW_CSUM; 28665228ddc9SRusty Russell arg &= ~TUN_F_CSUM; 28675228ddc9SRusty Russell 28685228ddc9SRusty Russell if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { 28695228ddc9SRusty Russell if (arg & TUN_F_TSO_ECN) { 28705228ddc9SRusty Russell features |= NETIF_F_TSO_ECN; 28715228ddc9SRusty Russell arg &= ~TUN_F_TSO_ECN; 28725228ddc9SRusty Russell } 28735228ddc9SRusty Russell if (arg & TUN_F_TSO4) 28745228ddc9SRusty Russell features |= NETIF_F_TSO; 28755228ddc9SRusty Russell if (arg & TUN_F_TSO6) 28765228ddc9SRusty Russell features |= NETIF_F_TSO6; 28775228ddc9SRusty Russell arg &= ~(TUN_F_TSO4|TUN_F_TSO6); 28785228ddc9SRusty Russell } 28790c19f846SWillem de Bruijn 28800c19f846SWillem de Bruijn arg &= ~TUN_F_UFO; 28815228ddc9SRusty Russell } 28825228ddc9SRusty Russell 28835228ddc9SRusty Russell /* This gives the user a way to test for new features in future by 28845228ddc9SRusty Russell * trying to set them. */ 28855228ddc9SRusty Russell if (arg) 28865228ddc9SRusty Russell return -EINVAL; 28875228ddc9SRusty Russell 288888255375SMichał Mirosław tun->set_features = features; 288909050957SYaroslav Isakov tun->dev->wanted_features &= ~TUN_USER_FEATURES; 289009050957SYaroslav Isakov tun->dev->wanted_features |= features; 289188255375SMichał Mirosław netdev_update_features(tun->dev); 28925228ddc9SRusty Russell 28935228ddc9SRusty Russell return 0; 28945228ddc9SRusty Russell } 28955228ddc9SRusty Russell 2896c8d68e6bSJason Wang static void tun_detach_filter(struct tun_struct *tun, int n) 2897c8d68e6bSJason Wang { 2898c8d68e6bSJason Wang int i; 2899c8d68e6bSJason Wang struct tun_file *tfile; 2900c8d68e6bSJason Wang 2901c8d68e6bSJason Wang for (i = 0; i < n; i++) { 2902b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 29038ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 29048ced425eSHannes Frederic Sowa sk_detach_filter(tfile->socket.sk); 29058ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 2906c8d68e6bSJason Wang } 2907c8d68e6bSJason Wang 2908c8d68e6bSJason Wang tun->filter_attached = false; 2909c8d68e6bSJason Wang } 2910c8d68e6bSJason Wang 2911c8d68e6bSJason Wang static int tun_attach_filter(struct tun_struct *tun) 2912c8d68e6bSJason Wang { 2913c8d68e6bSJason Wang int i, ret = 0; 2914c8d68e6bSJason Wang struct tun_file *tfile; 2915c8d68e6bSJason Wang 2916c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 2917b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 29188ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 29198ced425eSHannes Frederic Sowa ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); 29208ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 2921c8d68e6bSJason Wang if (ret) { 2922c8d68e6bSJason Wang tun_detach_filter(tun, i); 2923c8d68e6bSJason Wang return ret; 2924c8d68e6bSJason Wang } 2925c8d68e6bSJason Wang } 2926c8d68e6bSJason Wang 2927c8d68e6bSJason Wang tun->filter_attached = true; 2928c8d68e6bSJason Wang return ret; 2929c8d68e6bSJason Wang } 2930c8d68e6bSJason Wang 2931c8d68e6bSJason Wang static void tun_set_sndbuf(struct tun_struct *tun) 2932c8d68e6bSJason Wang { 2933c8d68e6bSJason Wang struct tun_file *tfile; 2934c8d68e6bSJason Wang int i; 2935c8d68e6bSJason Wang 2936c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 2937b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 2938c8d68e6bSJason Wang tfile->socket.sk->sk_sndbuf = tun->sndbuf; 2939c8d68e6bSJason Wang } 2940c8d68e6bSJason Wang } 2941c8d68e6bSJason Wang 2942cde8b15fSJason Wang static int tun_set_queue(struct file *file, struct ifreq *ifr) 2943cde8b15fSJason Wang { 2944cde8b15fSJason Wang struct tun_file *tfile = file->private_data; 2945cde8b15fSJason Wang struct tun_struct *tun; 2946cde8b15fSJason Wang int ret = 0; 2947cde8b15fSJason Wang 2948cde8b15fSJason Wang rtnl_lock(); 2949cde8b15fSJason Wang 2950cde8b15fSJason Wang if (ifr->ifr_flags & IFF_ATTACH_QUEUE) { 29514008e97fSJason Wang tun = tfile->detached; 29525dbbaf2dSPaul Moore if (!tun) { 2953cde8b15fSJason Wang ret = -EINVAL; 29545dbbaf2dSPaul Moore goto unlock; 29555dbbaf2dSPaul Moore } 29565dbbaf2dSPaul Moore ret = security_tun_dev_attach_queue(tun->security); 29575dbbaf2dSPaul Moore if (ret < 0) 29585dbbaf2dSPaul Moore goto unlock; 2959af3fb24eSEric Dumazet ret = tun_attach(tun, file, false, tun->flags & IFF_NAPI, 296077f22f92SYang Yingliang tun->flags & IFF_NAPI_FRAGS, true); 29614008e97fSJason Wang } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) { 2962b8deabd3SJason Wang tun = rtnl_dereference(tfile->tun); 296340630b82SMichael S. Tsirkin if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached) 29644008e97fSJason Wang ret = -EINVAL; 2965cde8b15fSJason Wang else 29664008e97fSJason Wang __tun_detach(tfile, false); 29674008e97fSJason Wang } else 2968cde8b15fSJason Wang ret = -EINVAL; 2969cde8b15fSJason Wang 297083c1f36fSSabrina Dubroca if (ret >= 0) 297183c1f36fSSabrina Dubroca netdev_state_change(tun->dev); 297283c1f36fSSabrina Dubroca 29735dbbaf2dSPaul Moore unlock: 2974cde8b15fSJason Wang rtnl_unlock(); 2975cde8b15fSJason Wang return ret; 2976cde8b15fSJason Wang } 2977cde8b15fSJason Wang 2978cd5681d7SJason Wang static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog **prog_p, 2979cd5681d7SJason Wang void __user *data) 298096f84061SJason Wang { 298196f84061SJason Wang struct bpf_prog *prog; 298296f84061SJason Wang int fd; 298396f84061SJason Wang 298496f84061SJason Wang if (copy_from_user(&fd, data, sizeof(fd))) 298596f84061SJason Wang return -EFAULT; 298696f84061SJason Wang 298796f84061SJason Wang if (fd == -1) { 298896f84061SJason Wang prog = NULL; 298996f84061SJason Wang } else { 299096f84061SJason Wang prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER); 299196f84061SJason Wang if (IS_ERR(prog)) 299296f84061SJason Wang return PTR_ERR(prog); 299396f84061SJason Wang } 299496f84061SJason Wang 2995cd5681d7SJason Wang return __tun_set_ebpf(tun, prog_p, prog); 299696f84061SJason Wang } 299796f84061SJason Wang 299850857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd, 299950857e2aSArnd Bergmann unsigned long arg, int ifreq_len) 30001da177e4SLinus Torvalds { 300136b50babSEric W. Biederman struct tun_file *tfile = file->private_data; 3002f663706aSKirill Tkhai struct net *net = sock_net(&tfile->sk); 3003631ab46bSEric W. Biederman struct tun_struct *tun; 30041da177e4SLinus Torvalds void __user* argp = (void __user*)arg; 300526d31925SNicolas Dichtel unsigned int ifindex, carrier; 30061da177e4SLinus Torvalds struct ifreq ifr; 30070625c883SEric W. Biederman kuid_t owner; 30080625c883SEric W. Biederman kgid_t group; 300933dccbb0SHerbert Xu int sndbuf; 3010d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 30111cf8e410SMichael S. Tsirkin int le; 3012f271b2ccSMax Krasnyansky int ret; 301383c1f36fSSabrina Dubroca bool do_notify = false; 30141da177e4SLinus Torvalds 3015f2780d6dSKirill Tkhai if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || 3016f2780d6dSKirill Tkhai (_IOC_TYPE(cmd) == SOCK_IOC_TYPE && cmd != SIOCGSKNS)) { 301750857e2aSArnd Bergmann if (copy_from_user(&ifr, argp, ifreq_len)) 30181da177e4SLinus Torvalds return -EFAULT; 30198bbb1813SDavid S. Miller } else { 3020a117dacdSMathias Krause memset(&ifr, 0, sizeof(ifr)); 30218bbb1813SDavid S. Miller } 3022631ab46bSEric W. Biederman if (cmd == TUNGETFEATURES) { 3023631ab46bSEric W. Biederman /* Currently this just means: "what IFF flags are valid?". 3024631ab46bSEric W. Biederman * This is needed because we never checked for invalid flags on 3025031f5e03SMichael S. Tsirkin * TUNSETIFF. 3026031f5e03SMichael S. Tsirkin */ 3027031f5e03SMichael S. Tsirkin return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES, 3028631ab46bSEric W. Biederman (unsigned int __user*)argp); 3029f663706aSKirill Tkhai } else if (cmd == TUNSETQUEUE) { 3030cde8b15fSJason Wang return tun_set_queue(file, &ifr); 3031f663706aSKirill Tkhai } else if (cmd == SIOCGSKNS) { 3032f663706aSKirill Tkhai if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 3033f663706aSKirill Tkhai return -EPERM; 3034f663706aSKirill Tkhai return open_related_ns(&net->ns, get_net_ns); 3035f663706aSKirill Tkhai } 3036631ab46bSEric W. Biederman 3037c8d68e6bSJason Wang ret = 0; 3038876bfd4dSHerbert Xu rtnl_lock(); 3039876bfd4dSHerbert Xu 30409484dc74Syuan linyu tun = tun_get(tfile); 30410f16bc13SGao Feng if (cmd == TUNSETIFF) { 30420f16bc13SGao Feng ret = -EEXIST; 30430f16bc13SGao Feng if (tun) 30440f16bc13SGao Feng goto unlock; 30450f16bc13SGao Feng 30461da177e4SLinus Torvalds ifr.ifr_name[IFNAMSIZ-1] = '\0'; 30471da177e4SLinus Torvalds 3048f2780d6dSKirill Tkhai ret = tun_set_iff(net, file, &ifr); 30491da177e4SLinus Torvalds 3050876bfd4dSHerbert Xu if (ret) 3051876bfd4dSHerbert Xu goto unlock; 30521da177e4SLinus Torvalds 305350857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 3054876bfd4dSHerbert Xu ret = -EFAULT; 3055876bfd4dSHerbert Xu goto unlock; 30561da177e4SLinus Torvalds } 3057fb7589a1SPavel Emelyanov if (cmd == TUNSETIFINDEX) { 3058fb7589a1SPavel Emelyanov ret = -EPERM; 3059fb7589a1SPavel Emelyanov if (tun) 3060fb7589a1SPavel Emelyanov goto unlock; 3061fb7589a1SPavel Emelyanov 3062fb7589a1SPavel Emelyanov ret = -EFAULT; 3063fb7589a1SPavel Emelyanov if (copy_from_user(&ifindex, argp, sizeof(ifindex))) 3064fb7589a1SPavel Emelyanov goto unlock; 3065fb7589a1SPavel Emelyanov 3066fb7589a1SPavel Emelyanov ret = 0; 3067fb7589a1SPavel Emelyanov tfile->ifindex = ifindex; 3068fb7589a1SPavel Emelyanov goto unlock; 3069fb7589a1SPavel Emelyanov } 30701da177e4SLinus Torvalds 3071876bfd4dSHerbert Xu ret = -EBADFD; 30721da177e4SLinus Torvalds if (!tun) 3073876bfd4dSHerbert Xu goto unlock; 30741da177e4SLinus Torvalds 30753424170fSMichal Kubecek netif_info(tun, drv, tun->dev, "tun_chr_ioctl cmd %u\n", cmd); 30761da177e4SLinus Torvalds 30770c3e0e3bSKirill Tkhai net = dev_net(tun->dev); 3078631ab46bSEric W. Biederman ret = 0; 30791da177e4SLinus Torvalds switch (cmd) { 3080e3b99556SMark McLoughlin case TUNGETIFF: 308112132768SKirill Tkhai tun_get_iff(tun, &ifr); 3082e3b99556SMark McLoughlin 30833d407a80SPavel Emelyanov if (tfile->detached) 30843d407a80SPavel Emelyanov ifr.ifr_flags |= IFF_DETACH_QUEUE; 3085849c9b6fSPavel Emelyanov if (!tfile->socket.sk->sk_filter) 3086849c9b6fSPavel Emelyanov ifr.ifr_flags |= IFF_NOFILTER; 30873d407a80SPavel Emelyanov 308850857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 3089631ab46bSEric W. Biederman ret = -EFAULT; 3090e3b99556SMark McLoughlin break; 3091e3b99556SMark McLoughlin 30921da177e4SLinus Torvalds case TUNSETNOCSUM: 30931da177e4SLinus Torvalds /* Disable/Enable checksum */ 30941da177e4SLinus Torvalds 309588255375SMichał Mirosław /* [unimplemented] */ 30963424170fSMichal Kubecek netif_info(tun, drv, tun->dev, "ignored: set checksum %s\n", 30976b8a66eeSJoe Perches arg ? "disabled" : "enabled"); 30981da177e4SLinus Torvalds break; 30991da177e4SLinus Torvalds 31001da177e4SLinus Torvalds case TUNSETPERSIST: 310154f968d6SJason Wang /* Disable/Enable persist mode. Keep an extra reference to the 310254f968d6SJason Wang * module to prevent the module being unprobed. 310354f968d6SJason Wang */ 310440630b82SMichael S. Tsirkin if (arg && !(tun->flags & IFF_PERSIST)) { 310540630b82SMichael S. Tsirkin tun->flags |= IFF_PERSIST; 310654f968d6SJason Wang __module_get(THIS_MODULE); 310783c1f36fSSabrina Dubroca do_notify = true; 3108dd38bd85SJason Wang } 310940630b82SMichael S. Tsirkin if (!arg && (tun->flags & IFF_PERSIST)) { 311040630b82SMichael S. Tsirkin tun->flags &= ~IFF_PERSIST; 311154f968d6SJason Wang module_put(THIS_MODULE); 311283c1f36fSSabrina Dubroca do_notify = true; 311354f968d6SJason Wang } 31141da177e4SLinus Torvalds 31153424170fSMichal Kubecek netif_info(tun, drv, tun->dev, "persist %s\n", 31166b8a66eeSJoe Perches arg ? "enabled" : "disabled"); 31171da177e4SLinus Torvalds break; 31181da177e4SLinus Torvalds 31191da177e4SLinus Torvalds case TUNSETOWNER: 31201da177e4SLinus Torvalds /* Set owner of the device */ 31210625c883SEric W. Biederman owner = make_kuid(current_user_ns(), arg); 31220625c883SEric W. Biederman if (!uid_valid(owner)) { 31230625c883SEric W. Biederman ret = -EINVAL; 31240625c883SEric W. Biederman break; 31250625c883SEric W. Biederman } 31260625c883SEric W. Biederman tun->owner = owner; 312783c1f36fSSabrina Dubroca do_notify = true; 31283424170fSMichal Kubecek netif_info(tun, drv, tun->dev, "owner set to %u\n", 31290625c883SEric W. Biederman from_kuid(&init_user_ns, tun->owner)); 31301da177e4SLinus Torvalds break; 31311da177e4SLinus Torvalds 31328c644623SGuido Guenther case TUNSETGROUP: 31338c644623SGuido Guenther /* Set group of the device */ 31340625c883SEric W. Biederman group = make_kgid(current_user_ns(), arg); 31350625c883SEric W. Biederman if (!gid_valid(group)) { 31360625c883SEric W. Biederman ret = -EINVAL; 31370625c883SEric W. Biederman break; 31380625c883SEric W. Biederman } 31390625c883SEric W. Biederman tun->group = group; 314083c1f36fSSabrina Dubroca do_notify = true; 31413424170fSMichal Kubecek netif_info(tun, drv, tun->dev, "group set to %u\n", 31420625c883SEric W. Biederman from_kgid(&init_user_ns, tun->group)); 31438c644623SGuido Guenther break; 31448c644623SGuido Guenther 3145ff4cc3acSMike Kershaw case TUNSETLINK: 3146ff4cc3acSMike Kershaw /* Only allow setting the type when the interface is down */ 3147ff4cc3acSMike Kershaw if (tun->dev->flags & IFF_UP) { 31483424170fSMichal Kubecek netif_info(tun, drv, tun->dev, 31496b8a66eeSJoe Perches "Linktype set failed because interface is up\n"); 315048abfe05SDavid S. Miller ret = -EBUSY; 3151ff4cc3acSMike Kershaw } else { 3152ff4cc3acSMike Kershaw tun->dev->type = (int) arg; 31533424170fSMichal Kubecek netif_info(tun, drv, tun->dev, "linktype set to %d\n", 31546b8a66eeSJoe Perches tun->dev->type); 315548abfe05SDavid S. Miller ret = 0; 3156ff4cc3acSMike Kershaw } 3157631ab46bSEric W. Biederman break; 3158ff4cc3acSMike Kershaw 31591da177e4SLinus Torvalds case TUNSETDEBUG: 31603424170fSMichal Kubecek tun->msg_enable = (u32)arg; 31611da177e4SLinus Torvalds break; 31623424170fSMichal Kubecek 31635228ddc9SRusty Russell case TUNSETOFFLOAD: 316488255375SMichał Mirosław ret = set_offload(tun, arg); 3165631ab46bSEric W. Biederman break; 31665228ddc9SRusty Russell 3167f271b2ccSMax Krasnyansky case TUNSETTXFILTER: 3168f271b2ccSMax Krasnyansky /* Can be set only for TAPs */ 3169631ab46bSEric W. Biederman ret = -EINVAL; 317040630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 3171631ab46bSEric W. Biederman break; 3172c0e5a8c2SHarvey Harrison ret = update_filter(&tun->txflt, (void __user *)arg); 3173631ab46bSEric W. Biederman break; 31741da177e4SLinus Torvalds 31751da177e4SLinus Torvalds case SIOCGIFHWADDR: 3176b595076aSUwe Kleine-König /* Get hw address */ 3177f271b2ccSMax Krasnyansky memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); 3178f271b2ccSMax Krasnyansky ifr.ifr_hwaddr.sa_family = tun->dev->type; 317950857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 3180631ab46bSEric W. Biederman ret = -EFAULT; 3181631ab46bSEric W. Biederman break; 31821da177e4SLinus Torvalds 31831da177e4SLinus Torvalds case SIOCSIFHWADDR: 3184f271b2ccSMax Krasnyansky /* Set hw address */ 31853a37a963SPetr Machata ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr, NULL); 3186631ab46bSEric W. Biederman break; 318733dccbb0SHerbert Xu 318833dccbb0SHerbert Xu case TUNGETSNDBUF: 318954f968d6SJason Wang sndbuf = tfile->socket.sk->sk_sndbuf; 319033dccbb0SHerbert Xu if (copy_to_user(argp, &sndbuf, sizeof(sndbuf))) 319133dccbb0SHerbert Xu ret = -EFAULT; 319233dccbb0SHerbert Xu break; 319333dccbb0SHerbert Xu 319433dccbb0SHerbert Xu case TUNSETSNDBUF: 319533dccbb0SHerbert Xu if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) { 319633dccbb0SHerbert Xu ret = -EFAULT; 319733dccbb0SHerbert Xu break; 319833dccbb0SHerbert Xu } 319993161922SCraig Gallek if (sndbuf <= 0) { 320093161922SCraig Gallek ret = -EINVAL; 320193161922SCraig Gallek break; 320293161922SCraig Gallek } 320333dccbb0SHerbert Xu 3204c8d68e6bSJason Wang tun->sndbuf = sndbuf; 3205c8d68e6bSJason Wang tun_set_sndbuf(tun); 320633dccbb0SHerbert Xu break; 320733dccbb0SHerbert Xu 3208d9d52b51SMichael S. Tsirkin case TUNGETVNETHDRSZ: 3209d9d52b51SMichael S. Tsirkin vnet_hdr_sz = tun->vnet_hdr_sz; 3210d9d52b51SMichael S. Tsirkin if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz))) 3211d9d52b51SMichael S. Tsirkin ret = -EFAULT; 3212d9d52b51SMichael S. Tsirkin break; 3213d9d52b51SMichael S. Tsirkin 3214d9d52b51SMichael S. Tsirkin case TUNSETVNETHDRSZ: 3215d9d52b51SMichael S. Tsirkin if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) { 3216d9d52b51SMichael S. Tsirkin ret = -EFAULT; 3217d9d52b51SMichael S. Tsirkin break; 3218d9d52b51SMichael S. Tsirkin } 3219d9d52b51SMichael S. Tsirkin if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) { 3220d9d52b51SMichael S. Tsirkin ret = -EINVAL; 3221d9d52b51SMichael S. Tsirkin break; 3222d9d52b51SMichael S. Tsirkin } 3223d9d52b51SMichael S. Tsirkin 3224d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = vnet_hdr_sz; 3225d9d52b51SMichael S. Tsirkin break; 3226d9d52b51SMichael S. Tsirkin 32271cf8e410SMichael S. Tsirkin case TUNGETVNETLE: 32281cf8e410SMichael S. Tsirkin le = !!(tun->flags & TUN_VNET_LE); 32291cf8e410SMichael S. Tsirkin if (put_user(le, (int __user *)argp)) 32301cf8e410SMichael S. Tsirkin ret = -EFAULT; 32311cf8e410SMichael S. Tsirkin break; 32321cf8e410SMichael S. Tsirkin 32331cf8e410SMichael S. Tsirkin case TUNSETVNETLE: 32341cf8e410SMichael S. Tsirkin if (get_user(le, (int __user *)argp)) { 32351cf8e410SMichael S. Tsirkin ret = -EFAULT; 32361cf8e410SMichael S. Tsirkin break; 32371cf8e410SMichael S. Tsirkin } 32381cf8e410SMichael S. Tsirkin if (le) 32391cf8e410SMichael S. Tsirkin tun->flags |= TUN_VNET_LE; 32401cf8e410SMichael S. Tsirkin else 32411cf8e410SMichael S. Tsirkin tun->flags &= ~TUN_VNET_LE; 32421cf8e410SMichael S. Tsirkin break; 32431cf8e410SMichael S. Tsirkin 32448b8e658bSGreg Kurz case TUNGETVNETBE: 32458b8e658bSGreg Kurz ret = tun_get_vnet_be(tun, argp); 32468b8e658bSGreg Kurz break; 32478b8e658bSGreg Kurz 32488b8e658bSGreg Kurz case TUNSETVNETBE: 32498b8e658bSGreg Kurz ret = tun_set_vnet_be(tun, argp); 32508b8e658bSGreg Kurz break; 32518b8e658bSGreg Kurz 325299405162SMichael S. Tsirkin case TUNATTACHFILTER: 325399405162SMichael S. Tsirkin /* Can be set only for TAPs */ 325499405162SMichael S. Tsirkin ret = -EINVAL; 325540630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 325699405162SMichael S. Tsirkin break; 325799405162SMichael S. Tsirkin ret = -EFAULT; 325854f968d6SJason Wang if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog))) 325999405162SMichael S. Tsirkin break; 326099405162SMichael S. Tsirkin 3261c8d68e6bSJason Wang ret = tun_attach_filter(tun); 326299405162SMichael S. Tsirkin break; 326399405162SMichael S. Tsirkin 326499405162SMichael S. Tsirkin case TUNDETACHFILTER: 326599405162SMichael S. Tsirkin /* Can be set only for TAPs */ 326699405162SMichael S. Tsirkin ret = -EINVAL; 326740630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 326899405162SMichael S. Tsirkin break; 3269c8d68e6bSJason Wang ret = 0; 3270c8d68e6bSJason Wang tun_detach_filter(tun, tun->numqueues); 327199405162SMichael S. Tsirkin break; 327299405162SMichael S. Tsirkin 327376975e9cSPavel Emelyanov case TUNGETFILTER: 327476975e9cSPavel Emelyanov ret = -EINVAL; 327540630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 327676975e9cSPavel Emelyanov break; 327776975e9cSPavel Emelyanov ret = -EFAULT; 327876975e9cSPavel Emelyanov if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog))) 327976975e9cSPavel Emelyanov break; 328076975e9cSPavel Emelyanov ret = 0; 328176975e9cSPavel Emelyanov break; 328276975e9cSPavel Emelyanov 328396f84061SJason Wang case TUNSETSTEERINGEBPF: 3284cd5681d7SJason Wang ret = tun_set_ebpf(tun, &tun->steering_prog, argp); 328596f84061SJason Wang break; 328696f84061SJason Wang 3287aff3d70aSJason Wang case TUNSETFILTEREBPF: 3288aff3d70aSJason Wang ret = tun_set_ebpf(tun, &tun->filter_prog, argp); 3289aff3d70aSJason Wang break; 3290aff3d70aSJason Wang 329126d31925SNicolas Dichtel case TUNSETCARRIER: 329226d31925SNicolas Dichtel ret = -EFAULT; 329326d31925SNicolas Dichtel if (copy_from_user(&carrier, argp, sizeof(carrier))) 329426d31925SNicolas Dichtel goto unlock; 329526d31925SNicolas Dichtel 329626d31925SNicolas Dichtel ret = tun_net_change_carrier(tun->dev, (bool)carrier); 329726d31925SNicolas Dichtel break; 329826d31925SNicolas Dichtel 32990c3e0e3bSKirill Tkhai case TUNGETDEVNETNS: 33000c3e0e3bSKirill Tkhai ret = -EPERM; 33010c3e0e3bSKirill Tkhai if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 33020c3e0e3bSKirill Tkhai goto unlock; 33030c3e0e3bSKirill Tkhai ret = open_related_ns(&net->ns, get_net_ns); 33040c3e0e3bSKirill Tkhai break; 33050c3e0e3bSKirill Tkhai 33061da177e4SLinus Torvalds default: 3307631ab46bSEric W. Biederman ret = -EINVAL; 3308631ab46bSEric W. Biederman break; 3309ee289b64SJoe Perches } 33101da177e4SLinus Torvalds 331183c1f36fSSabrina Dubroca if (do_notify) 331283c1f36fSSabrina Dubroca netdev_state_change(tun->dev); 331383c1f36fSSabrina Dubroca 3314876bfd4dSHerbert Xu unlock: 3315876bfd4dSHerbert Xu rtnl_unlock(); 3316876bfd4dSHerbert Xu if (tun) 3317631ab46bSEric W. Biederman tun_put(tun); 3318631ab46bSEric W. Biederman return ret; 33191da177e4SLinus Torvalds } 33201da177e4SLinus Torvalds 332150857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file, 332250857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 332350857e2aSArnd Bergmann { 332450857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq)); 332550857e2aSArnd Bergmann } 332650857e2aSArnd Bergmann 332750857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 332850857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file, 332950857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 333050857e2aSArnd Bergmann { 333150857e2aSArnd Bergmann switch (cmd) { 333250857e2aSArnd Bergmann case TUNSETIFF: 333350857e2aSArnd Bergmann case TUNGETIFF: 333450857e2aSArnd Bergmann case TUNSETTXFILTER: 333550857e2aSArnd Bergmann case TUNGETSNDBUF: 333650857e2aSArnd Bergmann case TUNSETSNDBUF: 333750857e2aSArnd Bergmann case SIOCGIFHWADDR: 333850857e2aSArnd Bergmann case SIOCSIFHWADDR: 333950857e2aSArnd Bergmann arg = (unsigned long)compat_ptr(arg); 334050857e2aSArnd Bergmann break; 334150857e2aSArnd Bergmann default: 334250857e2aSArnd Bergmann arg = (compat_ulong_t)arg; 334350857e2aSArnd Bergmann break; 334450857e2aSArnd Bergmann } 334550857e2aSArnd Bergmann 334650857e2aSArnd Bergmann /* 334750857e2aSArnd Bergmann * compat_ifreq is shorter than ifreq, so we must not access beyond 334850857e2aSArnd Bergmann * the end of that structure. All fields that are used in this 334950857e2aSArnd Bergmann * driver are compatible though, we don't need to convert the 335050857e2aSArnd Bergmann * contents. 335150857e2aSArnd Bergmann */ 335250857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq)); 335350857e2aSArnd Bergmann } 335450857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */ 335550857e2aSArnd Bergmann 33561da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on) 33571da177e4SLinus Torvalds { 335854f968d6SJason Wang struct tun_file *tfile = file->private_data; 33591da177e4SLinus Torvalds int ret; 33601da177e4SLinus Torvalds 336154f968d6SJason Wang if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) 33629d319522SJonathan Corbet goto out; 33631da177e4SLinus Torvalds 33641da177e4SLinus Torvalds if (on) { 336501919134SEric W. Biederman __f_setown(file, task_pid(current), PIDTYPE_TGID, 0); 336654f968d6SJason Wang tfile->flags |= TUN_FASYNC; 33671da177e4SLinus Torvalds } else 336854f968d6SJason Wang tfile->flags &= ~TUN_FASYNC; 33699d319522SJonathan Corbet ret = 0; 33709d319522SJonathan Corbet out: 33719d319522SJonathan Corbet return ret; 33721da177e4SLinus Torvalds } 33731da177e4SLinus Torvalds 33741da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file) 33751da177e4SLinus Torvalds { 3376140e807dSEric W. Biederman struct net *net = current->nsproxy->net_ns; 3377631ab46bSEric W. Biederman struct tun_file *tfile; 3378deed49fbSThomas Gleixner 3379140e807dSEric W. Biederman tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, 338011aa9c28SEric W. Biederman &tun_proto, 0); 3381631ab46bSEric W. Biederman if (!tfile) 3382631ab46bSEric W. Biederman return -ENOMEM; 3383b196d88aSJason Wang if (ptr_ring_init(&tfile->tx_ring, 0, GFP_KERNEL)) { 3384b196d88aSJason Wang sk_free(&tfile->sk); 3385b196d88aSJason Wang return -ENOMEM; 3386b196d88aSJason Wang } 3387b196d88aSJason Wang 3388c7256f57SEric Dumazet mutex_init(&tfile->napi_mutex); 3389c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 339054f968d6SJason Wang tfile->flags = 0; 3391fb7589a1SPavel Emelyanov tfile->ifindex = 0; 339254f968d6SJason Wang 3393333f7909SAl Viro init_waitqueue_head(&tfile->socket.wq.wait); 339454f968d6SJason Wang 339554f968d6SJason Wang tfile->socket.file = file; 339654f968d6SJason Wang tfile->socket.ops = &tun_socket_ops; 339754f968d6SJason Wang 339854f968d6SJason Wang sock_init_data(&tfile->socket, &tfile->sk); 339954f968d6SJason Wang 340054f968d6SJason Wang tfile->sk.sk_write_space = tun_sock_write_space; 340154f968d6SJason Wang tfile->sk.sk_sndbuf = INT_MAX; 340254f968d6SJason Wang 3403631ab46bSEric W. Biederman file->private_data = tfile; 34044008e97fSJason Wang INIT_LIST_HEAD(&tfile->next); 340554f968d6SJason Wang 340619a6afb2SJason Wang sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); 340719a6afb2SJason Wang 34081da177e4SLinus Torvalds return 0; 34091da177e4SLinus Torvalds } 34101da177e4SLinus Torvalds 34111da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file) 34121da177e4SLinus Torvalds { 3413631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 34141da177e4SLinus Torvalds 3415c8d68e6bSJason Wang tun_detach(tfile, true); 34161da177e4SLinus Torvalds 34171da177e4SLinus Torvalds return 0; 34181da177e4SLinus Torvalds } 34191da177e4SLinus Torvalds 342093e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS 34219484dc74Syuan linyu static void tun_chr_show_fdinfo(struct seq_file *m, struct file *file) 342293e14b6dSMasatake YAMATO { 34239484dc74Syuan linyu struct tun_file *tfile = file->private_data; 342493e14b6dSMasatake YAMATO struct tun_struct *tun; 342593e14b6dSMasatake YAMATO struct ifreq ifr; 342693e14b6dSMasatake YAMATO 342793e14b6dSMasatake YAMATO memset(&ifr, 0, sizeof(ifr)); 342893e14b6dSMasatake YAMATO 342993e14b6dSMasatake YAMATO rtnl_lock(); 34309484dc74Syuan linyu tun = tun_get(tfile); 343193e14b6dSMasatake YAMATO if (tun) 343212132768SKirill Tkhai tun_get_iff(tun, &ifr); 343393e14b6dSMasatake YAMATO rtnl_unlock(); 343493e14b6dSMasatake YAMATO 343593e14b6dSMasatake YAMATO if (tun) 343693e14b6dSMasatake YAMATO tun_put(tun); 343793e14b6dSMasatake YAMATO 3438a3816ab0SJoe Perches seq_printf(m, "iff:\t%s\n", ifr.ifr_name); 343993e14b6dSMasatake YAMATO } 344093e14b6dSMasatake YAMATO #endif 344193e14b6dSMasatake YAMATO 3442d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = { 34431da177e4SLinus Torvalds .owner = THIS_MODULE, 34441da177e4SLinus Torvalds .llseek = no_llseek, 34459b067034SAl Viro .read_iter = tun_chr_read_iter, 3446f5ff53b4SAl Viro .write_iter = tun_chr_write_iter, 34471da177e4SLinus Torvalds .poll = tun_chr_poll, 3448876bfd4dSHerbert Xu .unlocked_ioctl = tun_chr_ioctl, 344950857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 345050857e2aSArnd Bergmann .compat_ioctl = tun_chr_compat_ioctl, 345150857e2aSArnd Bergmann #endif 34521da177e4SLinus Torvalds .open = tun_chr_open, 34531da177e4SLinus Torvalds .release = tun_chr_close, 345493e14b6dSMasatake YAMATO .fasync = tun_chr_fasync, 345593e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS 345693e14b6dSMasatake YAMATO .show_fdinfo = tun_chr_show_fdinfo, 345793e14b6dSMasatake YAMATO #endif 34581da177e4SLinus Torvalds }; 34591da177e4SLinus Torvalds 34601da177e4SLinus Torvalds static struct miscdevice tun_miscdev = { 34611da177e4SLinus Torvalds .minor = TUN_MINOR, 34621da177e4SLinus Torvalds .name = "tun", 3463e454cea2SKay Sievers .nodename = "net/tun", 34641da177e4SLinus Torvalds .fops = &tun_fops, 34651da177e4SLinus Torvalds }; 34661da177e4SLinus Torvalds 34671da177e4SLinus Torvalds /* ethtool interface */ 34681da177e4SLinus Torvalds 34694e24f2ddSChas Williams static void tun_default_link_ksettings(struct net_device *dev, 347029ccc49dSPhilippe Reynes struct ethtool_link_ksettings *cmd) 34711da177e4SLinus Torvalds { 347229ccc49dSPhilippe Reynes ethtool_link_ksettings_zero_link_mode(cmd, supported); 347329ccc49dSPhilippe Reynes ethtool_link_ksettings_zero_link_mode(cmd, advertising); 347429ccc49dSPhilippe Reynes cmd->base.speed = SPEED_10; 347529ccc49dSPhilippe Reynes cmd->base.duplex = DUPLEX_FULL; 347629ccc49dSPhilippe Reynes cmd->base.port = PORT_TP; 347729ccc49dSPhilippe Reynes cmd->base.phy_address = 0; 347829ccc49dSPhilippe Reynes cmd->base.autoneg = AUTONEG_DISABLE; 34794e24f2ddSChas Williams } 34804e24f2ddSChas Williams 34814e24f2ddSChas Williams static int tun_get_link_ksettings(struct net_device *dev, 34824e24f2ddSChas Williams struct ethtool_link_ksettings *cmd) 34834e24f2ddSChas Williams { 34844e24f2ddSChas Williams struct tun_struct *tun = netdev_priv(dev); 34854e24f2ddSChas Williams 34864e24f2ddSChas Williams memcpy(cmd, &tun->link_ksettings, sizeof(*cmd)); 34874e24f2ddSChas Williams return 0; 34884e24f2ddSChas Williams } 34894e24f2ddSChas Williams 34904e24f2ddSChas Williams static int tun_set_link_ksettings(struct net_device *dev, 34914e24f2ddSChas Williams const struct ethtool_link_ksettings *cmd) 34924e24f2ddSChas Williams { 34934e24f2ddSChas Williams struct tun_struct *tun = netdev_priv(dev); 34944e24f2ddSChas Williams 34954e24f2ddSChas Williams memcpy(&tun->link_ksettings, cmd, sizeof(*cmd)); 34961da177e4SLinus Torvalds return 0; 34971da177e4SLinus Torvalds } 34981da177e4SLinus Torvalds 34991da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) 35001da177e4SLinus Torvalds { 35011da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 35021da177e4SLinus Torvalds 350333a5ba14SRick Jones strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); 350433a5ba14SRick Jones strlcpy(info->version, DRV_VERSION, sizeof(info->version)); 35051da177e4SLinus Torvalds 35061da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 350740630b82SMichael S. Tsirkin case IFF_TUN: 350833a5ba14SRick Jones strlcpy(info->bus_info, "tun", sizeof(info->bus_info)); 35091da177e4SLinus Torvalds break; 351040630b82SMichael S. Tsirkin case IFF_TAP: 351133a5ba14SRick Jones strlcpy(info->bus_info, "tap", sizeof(info->bus_info)); 35121da177e4SLinus Torvalds break; 35131da177e4SLinus Torvalds } 35141da177e4SLinus Torvalds } 35151da177e4SLinus Torvalds 35161da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev) 35171da177e4SLinus Torvalds { 35181da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 35193424170fSMichal Kubecek 35203424170fSMichal Kubecek return tun->msg_enable; 35211da177e4SLinus Torvalds } 35221da177e4SLinus Torvalds 35231da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value) 35241da177e4SLinus Torvalds { 35251da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 35263424170fSMichal Kubecek 35273424170fSMichal Kubecek tun->msg_enable = value; 35281da177e4SLinus Torvalds } 35291da177e4SLinus Torvalds 35305503fcecSJason Wang static int tun_get_coalesce(struct net_device *dev, 35315503fcecSJason Wang struct ethtool_coalesce *ec) 35325503fcecSJason Wang { 35335503fcecSJason Wang struct tun_struct *tun = netdev_priv(dev); 35345503fcecSJason Wang 35355503fcecSJason Wang ec->rx_max_coalesced_frames = tun->rx_batched; 35365503fcecSJason Wang 35375503fcecSJason Wang return 0; 35385503fcecSJason Wang } 35395503fcecSJason Wang 35405503fcecSJason Wang static int tun_set_coalesce(struct net_device *dev, 35415503fcecSJason Wang struct ethtool_coalesce *ec) 35425503fcecSJason Wang { 35435503fcecSJason Wang struct tun_struct *tun = netdev_priv(dev); 35445503fcecSJason Wang 35455503fcecSJason Wang if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT) 35465503fcecSJason Wang tun->rx_batched = NAPI_POLL_WEIGHT; 35475503fcecSJason Wang else 35485503fcecSJason Wang tun->rx_batched = ec->rx_max_coalesced_frames; 35495503fcecSJason Wang 35505503fcecSJason Wang return 0; 35515503fcecSJason Wang } 35525503fcecSJason Wang 35537282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = { 3554e5ad00b3SJakub Kicinski .supported_coalesce_params = ETHTOOL_COALESCE_RX_MAX_FRAMES, 35551da177e4SLinus Torvalds .get_drvinfo = tun_get_drvinfo, 35561da177e4SLinus Torvalds .get_msglevel = tun_get_msglevel, 35571da177e4SLinus Torvalds .set_msglevel = tun_set_msglevel, 3558bee31369SNolan Leake .get_link = ethtool_op_get_link, 3559eda29772SRichard Cochran .get_ts_info = ethtool_op_get_ts_info, 35605503fcecSJason Wang .get_coalesce = tun_get_coalesce, 35615503fcecSJason Wang .set_coalesce = tun_set_coalesce, 356229ccc49dSPhilippe Reynes .get_link_ksettings = tun_get_link_ksettings, 35634e24f2ddSChas Williams .set_link_ksettings = tun_set_link_ksettings, 35641da177e4SLinus Torvalds }; 35651da177e4SLinus Torvalds 35661576d986SJason Wang static int tun_queue_resize(struct tun_struct *tun) 35671576d986SJason Wang { 35681576d986SJason Wang struct net_device *dev = tun->dev; 35691576d986SJason Wang struct tun_file *tfile; 35705990a305SJason Wang struct ptr_ring **rings; 35711576d986SJason Wang int n = tun->numqueues + tun->numdisabled; 35721576d986SJason Wang int ret, i; 35731576d986SJason Wang 35745990a305SJason Wang rings = kmalloc_array(n, sizeof(*rings), GFP_KERNEL); 35755990a305SJason Wang if (!rings) 35761576d986SJason Wang return -ENOMEM; 35771576d986SJason Wang 35781576d986SJason Wang for (i = 0; i < tun->numqueues; i++) { 35791576d986SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 35805990a305SJason Wang rings[i] = &tfile->tx_ring; 35811576d986SJason Wang } 35821576d986SJason Wang list_for_each_entry(tfile, &tun->disabled, next) 35835990a305SJason Wang rings[i++] = &tfile->tx_ring; 35841576d986SJason Wang 35855990a305SJason Wang ret = ptr_ring_resize_multiple(rings, n, 35865990a305SJason Wang dev->tx_queue_len, GFP_KERNEL, 3587fc72d1d5SJason Wang tun_ptr_free); 35881576d986SJason Wang 35895990a305SJason Wang kfree(rings); 35901576d986SJason Wang return ret; 35911576d986SJason Wang } 35921576d986SJason Wang 35931576d986SJason Wang static int tun_device_event(struct notifier_block *unused, 35941576d986SJason Wang unsigned long event, void *ptr) 35951576d986SJason Wang { 35961576d986SJason Wang struct net_device *dev = netdev_notifier_info_to_dev(ptr); 35971576d986SJason Wang struct tun_struct *tun = netdev_priv(dev); 359872b319dcSFei Li int i; 35991576d986SJason Wang 360086dfb4acSCraig Gallek if (dev->rtnl_link_ops != &tun_link_ops) 360186dfb4acSCraig Gallek return NOTIFY_DONE; 360286dfb4acSCraig Gallek 36031576d986SJason Wang switch (event) { 36041576d986SJason Wang case NETDEV_CHANGE_TX_QUEUE_LEN: 36051576d986SJason Wang if (tun_queue_resize(tun)) 36061576d986SJason Wang return NOTIFY_BAD; 36071576d986SJason Wang break; 360872b319dcSFei Li case NETDEV_UP: 360972b319dcSFei Li for (i = 0; i < tun->numqueues; i++) { 361072b319dcSFei Li struct tun_file *tfile; 361172b319dcSFei Li 361272b319dcSFei Li tfile = rtnl_dereference(tun->tfiles[i]); 361372b319dcSFei Li tfile->socket.sk->sk_write_space(tfile->socket.sk); 361472b319dcSFei Li } 361572b319dcSFei Li break; 36161576d986SJason Wang default: 36171576d986SJason Wang break; 36181576d986SJason Wang } 36191576d986SJason Wang 36201576d986SJason Wang return NOTIFY_DONE; 36211576d986SJason Wang } 36221576d986SJason Wang 36231576d986SJason Wang static struct notifier_block tun_notifier_block __read_mostly = { 36241576d986SJason Wang .notifier_call = tun_device_event, 36251576d986SJason Wang }; 362679d17604SPavel Emelyanov 36271da177e4SLinus Torvalds static int __init tun_init(void) 36281da177e4SLinus Torvalds { 36291da177e4SLinus Torvalds int ret = 0; 36301da177e4SLinus Torvalds 36316b8a66eeSJoe Perches pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION); 36321da177e4SLinus Torvalds 3633f019a7a5SEric W. Biederman ret = rtnl_link_register(&tun_link_ops); 363479d17604SPavel Emelyanov if (ret) { 36356b8a66eeSJoe Perches pr_err("Can't register link_ops\n"); 3636f019a7a5SEric W. Biederman goto err_linkops; 363779d17604SPavel Emelyanov } 363879d17604SPavel Emelyanov 36391da177e4SLinus Torvalds ret = misc_register(&tun_miscdev); 364079d17604SPavel Emelyanov if (ret) { 36416b8a66eeSJoe Perches pr_err("Can't register misc device %d\n", TUN_MINOR); 364279d17604SPavel Emelyanov goto err_misc; 364379d17604SPavel Emelyanov } 36441576d986SJason Wang 36455edfbd3cSTonghao Zhang ret = register_netdevice_notifier(&tun_notifier_block); 36465edfbd3cSTonghao Zhang if (ret) { 36475edfbd3cSTonghao Zhang pr_err("Can't register netdevice notifier\n"); 36485edfbd3cSTonghao Zhang goto err_notifier; 36495edfbd3cSTonghao Zhang } 36505edfbd3cSTonghao Zhang 365179d17604SPavel Emelyanov return 0; 36525edfbd3cSTonghao Zhang 36535edfbd3cSTonghao Zhang err_notifier: 36545edfbd3cSTonghao Zhang misc_deregister(&tun_miscdev); 365579d17604SPavel Emelyanov err_misc: 3656f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 3657f019a7a5SEric W. Biederman err_linkops: 36581da177e4SLinus Torvalds return ret; 36591da177e4SLinus Torvalds } 36601da177e4SLinus Torvalds 36611da177e4SLinus Torvalds static void tun_cleanup(void) 36621da177e4SLinus Torvalds { 36631da177e4SLinus Torvalds misc_deregister(&tun_miscdev); 3664f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 36651576d986SJason Wang unregister_netdevice_notifier(&tun_notifier_block); 36661da177e4SLinus Torvalds } 36671da177e4SLinus Torvalds 366805c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file. Returns error unless file is 366905c2828cSMichael S. Tsirkin * attached to a device. The returned object works like a packet socket, it 367005c2828cSMichael S. Tsirkin * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for 367105c2828cSMichael S. Tsirkin * holding a reference to the file for as long as the socket is in use. */ 367205c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file) 367305c2828cSMichael S. Tsirkin { 36746e914fc7SJason Wang struct tun_file *tfile; 367505c2828cSMichael S. Tsirkin if (file->f_op != &tun_fops) 367605c2828cSMichael S. Tsirkin return ERR_PTR(-EINVAL); 36776e914fc7SJason Wang tfile = file->private_data; 36786e914fc7SJason Wang if (!tfile) 367905c2828cSMichael S. Tsirkin return ERR_PTR(-EBADFD); 368054f968d6SJason Wang return &tfile->socket; 368105c2828cSMichael S. Tsirkin } 368205c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket); 368305c2828cSMichael S. Tsirkin 36845990a305SJason Wang struct ptr_ring *tun_get_tx_ring(struct file *file) 368583339c6bSJason Wang { 368683339c6bSJason Wang struct tun_file *tfile; 368783339c6bSJason Wang 368883339c6bSJason Wang if (file->f_op != &tun_fops) 368983339c6bSJason Wang return ERR_PTR(-EINVAL); 369083339c6bSJason Wang tfile = file->private_data; 369183339c6bSJason Wang if (!tfile) 369283339c6bSJason Wang return ERR_PTR(-EBADFD); 36935990a305SJason Wang return &tfile->tx_ring; 369483339c6bSJason Wang } 36955990a305SJason Wang EXPORT_SYMBOL_GPL(tun_get_tx_ring); 369683339c6bSJason Wang 36971da177e4SLinus Torvalds module_init(tun_init); 36981da177e4SLinus Torvalds module_exit(tun_cleanup); 36991da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION); 37001da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT); 37011da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 37021da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR); 3703578454ffSKay Sievers MODULE_ALIAS("devname:net/tun"); 3704