11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * TUN - Universal TUN/TAP device driver. 31da177e4SLinus Torvalds * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com> 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 61da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 71da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 81da177e4SLinus Torvalds * (at your option) any later version. 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 111da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 121da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 131da177e4SLinus Torvalds * GNU General Public License for more details. 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $ 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds /* 191da177e4SLinus Torvalds * Changes: 201da177e4SLinus Torvalds * 21ff4cc3acSMike Kershaw * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14 22ff4cc3acSMike Kershaw * Add TUNSETLINK ioctl to set the link encapsulation 23ff4cc3acSMike Kershaw * 241da177e4SLinus Torvalds * Mark Smith <markzzzsmith@yahoo.com.au> 25344dc8edSJoe Perches * Use eth_random_addr() for tap MAC address. 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20 281da177e4SLinus Torvalds * Fixes in packet dropping, queue length setting and queue wakeup. 291da177e4SLinus Torvalds * Increased default tx queue length. 301da177e4SLinus Torvalds * Added ethtool API. 311da177e4SLinus Torvalds * Minor cleanups 321da177e4SLinus Torvalds * 331da177e4SLinus Torvalds * Daniel Podlejski <underley@underley.eu.org> 341da177e4SLinus Torvalds * Modifications for 2.3.99-pre5 kernel. 351da177e4SLinus Torvalds */ 361da177e4SLinus Torvalds 376b8a66eeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 386b8a66eeSJoe Perches 391da177e4SLinus Torvalds #define DRV_NAME "tun" 401da177e4SLinus Torvalds #define DRV_VERSION "1.6" 411da177e4SLinus Torvalds #define DRV_DESCRIPTION "Universal TUN/TAP device driver" 421da177e4SLinus Torvalds #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>" 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds #include <linux/module.h> 451da177e4SLinus Torvalds #include <linux/errno.h> 461da177e4SLinus Torvalds #include <linux/kernel.h> 47174cd4b1SIngo Molnar #include <linux/sched/signal.h> 481da177e4SLinus Torvalds #include <linux/major.h> 491da177e4SLinus Torvalds #include <linux/slab.h> 501da177e4SLinus Torvalds #include <linux/poll.h> 511da177e4SLinus Torvalds #include <linux/fcntl.h> 521da177e4SLinus Torvalds #include <linux/init.h> 531da177e4SLinus Torvalds #include <linux/skbuff.h> 541da177e4SLinus Torvalds #include <linux/netdevice.h> 551da177e4SLinus Torvalds #include <linux/etherdevice.h> 561da177e4SLinus Torvalds #include <linux/miscdevice.h> 571da177e4SLinus Torvalds #include <linux/ethtool.h> 581da177e4SLinus Torvalds #include <linux/rtnetlink.h> 5950857e2aSArnd Bergmann #include <linux/compat.h> 601da177e4SLinus Torvalds #include <linux/if.h> 611da177e4SLinus Torvalds #include <linux/if_arp.h> 621da177e4SLinus Torvalds #include <linux/if_ether.h> 631da177e4SLinus Torvalds #include <linux/if_tun.h> 646680ec68SJason Wang #include <linux/if_vlan.h> 651da177e4SLinus Torvalds #include <linux/crc32.h> 66d647a591SPavel Emelyanov #include <linux/nsproxy.h> 67f43798c2SRusty Russell #include <linux/virtio_net.h> 6899405162SMichael S. Tsirkin #include <linux/rcupdate.h> 69881d966bSEric W. Biederman #include <net/net_namespace.h> 7079d17604SPavel Emelyanov #include <net/netns/generic.h> 71f019a7a5SEric W. Biederman #include <net/rtnetlink.h> 7233dccbb0SHerbert Xu #include <net/sock.h> 7393e14b6dSMasatake YAMATO #include <linux/seq_file.h> 74e0b46d0eSHerbert Xu #include <linux/uio.h> 751576d986SJason Wang #include <linux/skb_array.h> 76761876c8SJason Wang #include <linux/bpf.h> 77761876c8SJason Wang #include <linux/bpf_trace.h> 7890e33d45SPetar Penkov #include <linux/mutex.h> 791da177e4SLinus Torvalds 807c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 81f2780d6dSKirill Tkhai #include <linux/proc_fs.h> 821da177e4SLinus Torvalds 8314daa021SRusty Russell /* Uncomment to enable debugging */ 8414daa021SRusty Russell /* #define TUN_DEBUG 1 */ 8514daa021SRusty Russell 861da177e4SLinus Torvalds #ifdef TUN_DEBUG 871da177e4SLinus Torvalds static int debug; 8814daa021SRusty Russell 896b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...) \ 906b8a66eeSJoe Perches do { \ 916b8a66eeSJoe Perches if (tun->debug) \ 926b8a66eeSJoe Perches netdev_printk(level, tun->dev, fmt, ##args); \ 936b8a66eeSJoe Perches } while (0) 946b8a66eeSJoe Perches #define DBG1(level, fmt, args...) \ 956b8a66eeSJoe Perches do { \ 966b8a66eeSJoe Perches if (debug == 2) \ 976b8a66eeSJoe Perches printk(level fmt, ##args); \ 986b8a66eeSJoe Perches } while (0) 9914daa021SRusty Russell #else 1006b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...) \ 1016b8a66eeSJoe Perches do { \ 1026b8a66eeSJoe Perches if (0) \ 1036b8a66eeSJoe Perches netdev_printk(level, tun->dev, fmt, ##args); \ 1046b8a66eeSJoe Perches } while (0) 1056b8a66eeSJoe Perches #define DBG1(level, fmt, args...) \ 1066b8a66eeSJoe Perches do { \ 1076b8a66eeSJoe Perches if (0) \ 1086b8a66eeSJoe Perches printk(level fmt, ##args); \ 1096b8a66eeSJoe Perches } while (0) 1101da177e4SLinus Torvalds #endif 1111da177e4SLinus Torvalds 112761876c8SJason Wang #define TUN_HEADROOM 256 1137df13219SJason Wang #define TUN_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD) 11466ccbc9cSJason Wang 115031f5e03SMichael S. Tsirkin /* TUN device flags */ 116031f5e03SMichael S. Tsirkin 117031f5e03SMichael S. Tsirkin /* IFF_ATTACH_QUEUE is never stored in device flags, 118031f5e03SMichael S. Tsirkin * overload it to mean fasync when stored there. 119031f5e03SMichael S. Tsirkin */ 120031f5e03SMichael S. Tsirkin #define TUN_FASYNC IFF_ATTACH_QUEUE 1211cf8e410SMichael S. Tsirkin /* High bits in flags field are unused. */ 1221cf8e410SMichael S. Tsirkin #define TUN_VNET_LE 0x80000000 1238b8e658bSGreg Kurz #define TUN_VNET_BE 0x40000000 124031f5e03SMichael S. Tsirkin 125031f5e03SMichael S. Tsirkin #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \ 12690e33d45SPetar Penkov IFF_MULTI_QUEUE | IFF_NAPI | IFF_NAPI_FRAGS) 12790e33d45SPetar Penkov 1280690899bSMichael S. Tsirkin #define GOODCOPY_LEN 128 1290690899bSMichael S. Tsirkin 130f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8 131f271b2ccSMax Krasnyansky struct tap_filter { 132f271b2ccSMax Krasnyansky unsigned int count; /* Number of addrs. Zero means disabled */ 133f271b2ccSMax Krasnyansky u32 mask[2]; /* Mask of the hashed addrs */ 134f271b2ccSMax Krasnyansky unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN]; 135f271b2ccSMax Krasnyansky }; 136f271b2ccSMax Krasnyansky 137baf71c5cSPankaj Gupta /* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal 138baf71c5cSPankaj Gupta * to max number of VCPUs in guest. */ 139baf71c5cSPankaj Gupta #define MAX_TAP_QUEUES 256 140b8732fb7SJason Wang #define MAX_TAP_FLOWS 4096 141c8d68e6bSJason Wang 14296442e42SJason Wang #define TUN_FLOW_EXPIRE (3 * HZ) 14396442e42SJason Wang 144608b9977SPaolo Abeni struct tun_pcpu_stats { 145608b9977SPaolo Abeni u64 rx_packets; 146608b9977SPaolo Abeni u64 rx_bytes; 147608b9977SPaolo Abeni u64 tx_packets; 148608b9977SPaolo Abeni u64 tx_bytes; 149608b9977SPaolo Abeni struct u64_stats_sync syncp; 150608b9977SPaolo Abeni u32 rx_dropped; 151608b9977SPaolo Abeni u32 tx_dropped; 152608b9977SPaolo Abeni u32 rx_frame_errors; 153608b9977SPaolo Abeni }; 154608b9977SPaolo Abeni 15554f968d6SJason Wang /* A tun_file connects an open character device to a tuntap netdevice. It 15692d4ea6eSstephen hemminger * also contains all socket related structures (except sock_fprog and tap_filter) 15754f968d6SJason Wang * to serve as one transmit queue for tuntap device. The sock_fprog and 15854f968d6SJason Wang * tap_filter were kept in tun_struct since they were used for filtering for the 15936fe8c09SRami Rosen * netdevice not for a specific queue (at least I didn't see the requirement for 16054f968d6SJason Wang * this). 1616e914fc7SJason Wang * 1626e914fc7SJason Wang * RCU usage: 16336fe8c09SRami Rosen * The tun_file and tun_struct are loosely coupled, the pointer from one to the 1646e914fc7SJason Wang * other can only be read while rcu_read_lock or rtnl_lock is held. 16554f968d6SJason Wang */ 166631ab46bSEric W. Biederman struct tun_file { 16754f968d6SJason Wang struct sock sk; 16854f968d6SJason Wang struct socket socket; 16954f968d6SJason Wang struct socket_wq wq; 1706e914fc7SJason Wang struct tun_struct __rcu *tun; 17154f968d6SJason Wang struct fasync_struct *fasync; 17254f968d6SJason Wang /* only used for fasnyc */ 17354f968d6SJason Wang unsigned int flags; 174fb7589a1SPavel Emelyanov union { 175c8d68e6bSJason Wang u16 queue_index; 176fb7589a1SPavel Emelyanov unsigned int ifindex; 177fb7589a1SPavel Emelyanov }; 17894317099SPetar Penkov struct napi_struct napi; 179aec72f33SEric Dumazet bool napi_enabled; 18090e33d45SPetar Penkov struct mutex napi_mutex; /* Protects access to the above napi */ 1814008e97fSJason Wang struct list_head next; 1824008e97fSJason Wang struct tun_struct *detached; 1835990a305SJason Wang struct ptr_ring tx_ring; 1848bf5c4eeSJesper Dangaard Brouer struct xdp_rxq_info xdp_rxq; 185631ab46bSEric W. Biederman }; 186631ab46bSEric W. Biederman 18796442e42SJason Wang struct tun_flow_entry { 18896442e42SJason Wang struct hlist_node hash_link; 18996442e42SJason Wang struct rcu_head rcu; 19096442e42SJason Wang struct tun_struct *tun; 19196442e42SJason Wang 19296442e42SJason Wang u32 rxhash; 1939bc88939STom Herbert u32 rps_rxhash; 19496442e42SJason Wang int queue_index; 19596442e42SJason Wang unsigned long updated; 19696442e42SJason Wang }; 19796442e42SJason Wang 19896442e42SJason Wang #define TUN_NUM_FLOW_ENTRIES 1024 19996442e42SJason Wang 200cd5681d7SJason Wang struct tun_prog { 20196f84061SJason Wang struct rcu_head rcu; 20296f84061SJason Wang struct bpf_prog *prog; 20396f84061SJason Wang }; 20496f84061SJason Wang 20554f968d6SJason Wang /* Since the socket were moved to tun_file, to preserve the behavior of persist 20636fe8c09SRami Rosen * device, socket filter, sndbuf and vnet header size were restore when the 20754f968d6SJason Wang * file were attached to a persist device. 20854f968d6SJason Wang */ 20914daa021SRusty Russell struct tun_struct { 210c8d68e6bSJason Wang struct tun_file __rcu *tfiles[MAX_TAP_QUEUES]; 211c8d68e6bSJason Wang unsigned int numqueues; 212f271b2ccSMax Krasnyansky unsigned int flags; 2130625c883SEric W. Biederman kuid_t owner; 2140625c883SEric W. Biederman kgid_t group; 21514daa021SRusty Russell 21614daa021SRusty Russell struct net_device *dev; 217c8f44affSMichał Mirosław netdev_features_t set_features; 21888255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \ 219d591a1f3SDavid S. Miller NETIF_F_TSO6) 220d9d52b51SMichael S. Tsirkin 221eaea34b2SPaolo Abeni int align; 222d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 22354f968d6SJason Wang int sndbuf; 22454f968d6SJason Wang struct tap_filter txflt; 22554f968d6SJason Wang struct sock_fprog fprog; 22654f968d6SJason Wang /* protected by rtnl lock */ 22754f968d6SJason Wang bool filter_attached; 22814daa021SRusty Russell #ifdef TUN_DEBUG 22914daa021SRusty Russell int debug; 23014daa021SRusty Russell #endif 23196442e42SJason Wang spinlock_t lock; 23296442e42SJason Wang struct hlist_head flows[TUN_NUM_FLOW_ENTRIES]; 23396442e42SJason Wang struct timer_list flow_gc_timer; 23496442e42SJason Wang unsigned long ageing_time; 2354008e97fSJason Wang unsigned int numdisabled; 2364008e97fSJason Wang struct list_head disabled; 2375dbbaf2dSPaul Moore void *security; 238b8732fb7SJason Wang u32 flow_count; 2395503fcecSJason Wang u32 rx_batched; 240608b9977SPaolo Abeni struct tun_pcpu_stats __percpu *pcpu_stats; 241761876c8SJason Wang struct bpf_prog __rcu *xdp_prog; 242cd5681d7SJason Wang struct tun_prog __rcu *steering_prog; 243aff3d70aSJason Wang struct tun_prog __rcu *filter_prog; 24414daa021SRusty Russell }; 24514daa021SRusty Russell 246aff3d70aSJason Wang struct veth { 247aff3d70aSJason Wang __be16 h_vlan_proto; 248aff3d70aSJason Wang __be16 h_vlan_TCI; 2491da177e4SLinus Torvalds }; 2501da177e4SLinus Torvalds 251fc72d1d5SJason Wang bool tun_is_xdp_buff(void *ptr) 252fc72d1d5SJason Wang { 253fc72d1d5SJason Wang return (unsigned long)ptr & TUN_XDP_FLAG; 254fc72d1d5SJason Wang } 255fc72d1d5SJason Wang EXPORT_SYMBOL(tun_is_xdp_buff); 256fc72d1d5SJason Wang 257fc72d1d5SJason Wang void *tun_xdp_to_ptr(void *ptr) 258fc72d1d5SJason Wang { 259fc72d1d5SJason Wang return (void *)((unsigned long)ptr | TUN_XDP_FLAG); 260fc72d1d5SJason Wang } 261fc72d1d5SJason Wang EXPORT_SYMBOL(tun_xdp_to_ptr); 262fc72d1d5SJason Wang 263fc72d1d5SJason Wang void *tun_ptr_to_xdp(void *ptr) 264fc72d1d5SJason Wang { 265fc72d1d5SJason Wang return (void *)((unsigned long)ptr & ~TUN_XDP_FLAG); 266fc72d1d5SJason Wang } 267fc72d1d5SJason Wang EXPORT_SYMBOL(tun_ptr_to_xdp); 268fc72d1d5SJason Wang 26994317099SPetar Penkov static int tun_napi_receive(struct napi_struct *napi, int budget) 27094317099SPetar Penkov { 27194317099SPetar Penkov struct tun_file *tfile = container_of(napi, struct tun_file, napi); 27294317099SPetar Penkov struct sk_buff_head *queue = &tfile->sk.sk_write_queue; 27394317099SPetar Penkov struct sk_buff_head process_queue; 27494317099SPetar Penkov struct sk_buff *skb; 27594317099SPetar Penkov int received = 0; 27694317099SPetar Penkov 27794317099SPetar Penkov __skb_queue_head_init(&process_queue); 27894317099SPetar Penkov 27994317099SPetar Penkov spin_lock(&queue->lock); 28094317099SPetar Penkov skb_queue_splice_tail_init(queue, &process_queue); 28194317099SPetar Penkov spin_unlock(&queue->lock); 28294317099SPetar Penkov 28394317099SPetar Penkov while (received < budget && (skb = __skb_dequeue(&process_queue))) { 28494317099SPetar Penkov napi_gro_receive(napi, skb); 28594317099SPetar Penkov ++received; 28694317099SPetar Penkov } 28794317099SPetar Penkov 28894317099SPetar Penkov if (!skb_queue_empty(&process_queue)) { 28994317099SPetar Penkov spin_lock(&queue->lock); 29094317099SPetar Penkov skb_queue_splice(&process_queue, queue); 29194317099SPetar Penkov spin_unlock(&queue->lock); 29294317099SPetar Penkov } 29394317099SPetar Penkov 29494317099SPetar Penkov return received; 29594317099SPetar Penkov } 29694317099SPetar Penkov 29794317099SPetar Penkov static int tun_napi_poll(struct napi_struct *napi, int budget) 29894317099SPetar Penkov { 29994317099SPetar Penkov unsigned int received; 30094317099SPetar Penkov 30194317099SPetar Penkov received = tun_napi_receive(napi, budget); 30294317099SPetar Penkov 30394317099SPetar Penkov if (received < budget) 30494317099SPetar Penkov napi_complete_done(napi, received); 30594317099SPetar Penkov 30694317099SPetar Penkov return received; 30794317099SPetar Penkov } 30894317099SPetar Penkov 30994317099SPetar Penkov static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile, 31094317099SPetar Penkov bool napi_en) 31194317099SPetar Penkov { 312aec72f33SEric Dumazet tfile->napi_enabled = napi_en; 31394317099SPetar Penkov if (napi_en) { 31494317099SPetar Penkov netif_napi_add(tun->dev, &tfile->napi, tun_napi_poll, 31594317099SPetar Penkov NAPI_POLL_WEIGHT); 31694317099SPetar Penkov napi_enable(&tfile->napi); 31790e33d45SPetar Penkov mutex_init(&tfile->napi_mutex); 31894317099SPetar Penkov } 31994317099SPetar Penkov } 32094317099SPetar Penkov 32194317099SPetar Penkov static void tun_napi_disable(struct tun_struct *tun, struct tun_file *tfile) 32294317099SPetar Penkov { 323aec72f33SEric Dumazet if (tfile->napi_enabled) 32494317099SPetar Penkov napi_disable(&tfile->napi); 32594317099SPetar Penkov } 32694317099SPetar Penkov 32794317099SPetar Penkov static void tun_napi_del(struct tun_struct *tun, struct tun_file *tfile) 32894317099SPetar Penkov { 329aec72f33SEric Dumazet if (tfile->napi_enabled) 33094317099SPetar Penkov netif_napi_del(&tfile->napi); 33194317099SPetar Penkov } 33294317099SPetar Penkov 33390e33d45SPetar Penkov static bool tun_napi_frags_enabled(const struct tun_struct *tun) 33490e33d45SPetar Penkov { 33590e33d45SPetar Penkov return READ_ONCE(tun->flags) & IFF_NAPI_FRAGS; 33690e33d45SPetar Penkov } 33790e33d45SPetar Penkov 3388b8e658bSGreg Kurz #ifdef CONFIG_TUN_VNET_CROSS_LE 3398b8e658bSGreg Kurz static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) 3408b8e658bSGreg Kurz { 3418b8e658bSGreg Kurz return tun->flags & TUN_VNET_BE ? false : 3428b8e658bSGreg Kurz 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 int be = !!(tun->flags & TUN_VNET_BE); 3488b8e658bSGreg Kurz 3498b8e658bSGreg Kurz if (put_user(be, argp)) 3508b8e658bSGreg Kurz return -EFAULT; 3518b8e658bSGreg Kurz 3528b8e658bSGreg Kurz return 0; 3538b8e658bSGreg Kurz } 3548b8e658bSGreg Kurz 3558b8e658bSGreg Kurz static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) 3568b8e658bSGreg Kurz { 3578b8e658bSGreg Kurz int be; 3588b8e658bSGreg Kurz 3598b8e658bSGreg Kurz if (get_user(be, argp)) 3608b8e658bSGreg Kurz return -EFAULT; 3618b8e658bSGreg Kurz 3628b8e658bSGreg Kurz if (be) 3638b8e658bSGreg Kurz tun->flags |= TUN_VNET_BE; 3648b8e658bSGreg Kurz else 3658b8e658bSGreg Kurz tun->flags &= ~TUN_VNET_BE; 3668b8e658bSGreg Kurz 3678b8e658bSGreg Kurz return 0; 3688b8e658bSGreg Kurz } 3698b8e658bSGreg Kurz #else 3708b8e658bSGreg Kurz static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) 3718b8e658bSGreg Kurz { 3728b8e658bSGreg Kurz return virtio_legacy_is_little_endian(); 3738b8e658bSGreg Kurz } 3748b8e658bSGreg Kurz 3758b8e658bSGreg Kurz static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) 3768b8e658bSGreg Kurz { 3778b8e658bSGreg Kurz return -EINVAL; 3788b8e658bSGreg Kurz } 3798b8e658bSGreg Kurz 3808b8e658bSGreg Kurz static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) 3818b8e658bSGreg Kurz { 3828b8e658bSGreg Kurz return -EINVAL; 3838b8e658bSGreg Kurz } 3848b8e658bSGreg Kurz #endif /* CONFIG_TUN_VNET_CROSS_LE */ 3858b8e658bSGreg Kurz 38625bd55bbSGreg Kurz static inline bool tun_is_little_endian(struct tun_struct *tun) 38725bd55bbSGreg Kurz { 3887d824109SGreg Kurz return tun->flags & TUN_VNET_LE || 3898b8e658bSGreg Kurz tun_legacy_is_little_endian(tun); 39025bd55bbSGreg Kurz } 39125bd55bbSGreg Kurz 39256f0dcc5SMichael S. Tsirkin static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val) 39356f0dcc5SMichael S. Tsirkin { 39425bd55bbSGreg Kurz return __virtio16_to_cpu(tun_is_little_endian(tun), val); 39556f0dcc5SMichael S. Tsirkin } 39656f0dcc5SMichael S. Tsirkin 39756f0dcc5SMichael S. Tsirkin static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val) 39856f0dcc5SMichael S. Tsirkin { 39925bd55bbSGreg Kurz return __cpu_to_virtio16(tun_is_little_endian(tun), val); 40056f0dcc5SMichael S. Tsirkin } 40156f0dcc5SMichael S. Tsirkin 40296442e42SJason Wang static inline u32 tun_hashfn(u32 rxhash) 40396442e42SJason Wang { 40496442e42SJason Wang return rxhash & 0x3ff; 40596442e42SJason Wang } 40696442e42SJason Wang 40796442e42SJason Wang static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash) 40896442e42SJason Wang { 40996442e42SJason Wang struct tun_flow_entry *e; 41096442e42SJason Wang 411b67bfe0dSSasha Levin hlist_for_each_entry_rcu(e, head, hash_link) { 41296442e42SJason Wang if (e->rxhash == rxhash) 41396442e42SJason Wang return e; 41496442e42SJason Wang } 41596442e42SJason Wang return NULL; 41696442e42SJason Wang } 41796442e42SJason Wang 41896442e42SJason Wang static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun, 41996442e42SJason Wang struct hlist_head *head, 42096442e42SJason Wang u32 rxhash, u16 queue_index) 42196442e42SJason Wang { 4229fdc6befSEric Dumazet struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC); 4239fdc6befSEric Dumazet 42496442e42SJason Wang if (e) { 42596442e42SJason Wang tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n", 42696442e42SJason Wang rxhash, queue_index); 42796442e42SJason Wang e->updated = jiffies; 42896442e42SJason Wang e->rxhash = rxhash; 4299bc88939STom Herbert e->rps_rxhash = 0; 43096442e42SJason Wang e->queue_index = queue_index; 43196442e42SJason Wang e->tun = tun; 43296442e42SJason Wang hlist_add_head_rcu(&e->hash_link, head); 433b8732fb7SJason Wang ++tun->flow_count; 43496442e42SJason Wang } 43596442e42SJason Wang return e; 43696442e42SJason Wang } 43796442e42SJason Wang 43896442e42SJason Wang static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e) 43996442e42SJason Wang { 44096442e42SJason Wang tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n", 44196442e42SJason Wang e->rxhash, e->queue_index); 44296442e42SJason Wang hlist_del_rcu(&e->hash_link); 4439fdc6befSEric Dumazet kfree_rcu(e, rcu); 444b8732fb7SJason Wang --tun->flow_count; 44596442e42SJason Wang } 44696442e42SJason Wang 44796442e42SJason Wang static void tun_flow_flush(struct tun_struct *tun) 44896442e42SJason Wang { 44996442e42SJason Wang int i; 45096442e42SJason Wang 45196442e42SJason Wang spin_lock_bh(&tun->lock); 45296442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 45396442e42SJason Wang struct tun_flow_entry *e; 454b67bfe0dSSasha Levin struct hlist_node *n; 45596442e42SJason Wang 456b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) 45796442e42SJason Wang tun_flow_delete(tun, e); 45896442e42SJason Wang } 45996442e42SJason Wang spin_unlock_bh(&tun->lock); 46096442e42SJason Wang } 46196442e42SJason Wang 46296442e42SJason Wang static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index) 46396442e42SJason Wang { 46496442e42SJason Wang int i; 46596442e42SJason Wang 46696442e42SJason Wang spin_lock_bh(&tun->lock); 46796442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 46896442e42SJason Wang struct tun_flow_entry *e; 469b67bfe0dSSasha Levin struct hlist_node *n; 47096442e42SJason Wang 471b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { 47296442e42SJason Wang if (e->queue_index == queue_index) 47396442e42SJason Wang tun_flow_delete(tun, e); 47496442e42SJason Wang } 47596442e42SJason Wang } 47696442e42SJason Wang spin_unlock_bh(&tun->lock); 47796442e42SJason Wang } 47896442e42SJason Wang 479e99e88a9SKees Cook static void tun_flow_cleanup(struct timer_list *t) 48096442e42SJason Wang { 481e99e88a9SKees Cook struct tun_struct *tun = from_timer(tun, t, flow_gc_timer); 48296442e42SJason Wang unsigned long delay = tun->ageing_time; 48396442e42SJason Wang unsigned long next_timer = jiffies + delay; 48496442e42SJason Wang unsigned long count = 0; 48596442e42SJason Wang int i; 48696442e42SJason Wang 48796442e42SJason Wang tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n"); 48896442e42SJason Wang 4897dbfb4efSEric Dumazet spin_lock(&tun->lock); 49096442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 49196442e42SJason Wang struct tun_flow_entry *e; 492b67bfe0dSSasha Levin struct hlist_node *n; 49396442e42SJason Wang 494b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { 49596442e42SJason Wang unsigned long this_timer; 49681d98fa4SEric Dumazet 49796442e42SJason Wang this_timer = e->updated + delay; 49881d98fa4SEric Dumazet if (time_before_eq(this_timer, jiffies)) { 49996442e42SJason Wang tun_flow_delete(tun, e); 50081d98fa4SEric Dumazet continue; 50181d98fa4SEric Dumazet } 50281d98fa4SEric Dumazet count++; 50381d98fa4SEric Dumazet if (time_before(this_timer, next_timer)) 50496442e42SJason Wang next_timer = this_timer; 50596442e42SJason Wang } 50696442e42SJason Wang } 50796442e42SJason Wang 50896442e42SJason Wang if (count) 50996442e42SJason Wang mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer)); 5107dbfb4efSEric Dumazet spin_unlock(&tun->lock); 51196442e42SJason Wang } 51296442e42SJason Wang 51349974420SEric Dumazet static void tun_flow_update(struct tun_struct *tun, u32 rxhash, 5149e85722dSJason Wang struct tun_file *tfile) 51596442e42SJason Wang { 51696442e42SJason Wang struct hlist_head *head; 51796442e42SJason Wang struct tun_flow_entry *e; 51896442e42SJason Wang unsigned long delay = tun->ageing_time; 5199e85722dSJason Wang u16 queue_index = tfile->queue_index; 52096442e42SJason Wang 52196442e42SJason Wang if (!rxhash) 52296442e42SJason Wang return; 52396442e42SJason Wang else 52496442e42SJason Wang head = &tun->flows[tun_hashfn(rxhash)]; 52596442e42SJason Wang 52696442e42SJason Wang rcu_read_lock(); 52796442e42SJason Wang 5289e85722dSJason Wang /* We may get a very small possibility of OOO during switching, not 5299e85722dSJason Wang * worth to optimize.*/ 5309e85722dSJason Wang if (tun->numqueues == 1 || tfile->detached) 53196442e42SJason Wang goto unlock; 53296442e42SJason Wang 53396442e42SJason Wang e = tun_flow_find(head, rxhash); 53496442e42SJason Wang if (likely(e)) { 53596442e42SJason Wang /* TODO: keep queueing to old queue until it's empty? */ 53696442e42SJason Wang e->queue_index = queue_index; 53796442e42SJason Wang e->updated = jiffies; 5389bc88939STom Herbert sock_rps_record_flow_hash(e->rps_rxhash); 53996442e42SJason Wang } else { 54096442e42SJason Wang spin_lock_bh(&tun->lock); 541b8732fb7SJason Wang if (!tun_flow_find(head, rxhash) && 542b8732fb7SJason Wang tun->flow_count < MAX_TAP_FLOWS) 54396442e42SJason Wang tun_flow_create(tun, head, rxhash, queue_index); 54496442e42SJason Wang 54596442e42SJason Wang if (!timer_pending(&tun->flow_gc_timer)) 54696442e42SJason Wang mod_timer(&tun->flow_gc_timer, 54796442e42SJason Wang round_jiffies_up(jiffies + delay)); 54896442e42SJason Wang spin_unlock_bh(&tun->lock); 54996442e42SJason Wang } 55096442e42SJason Wang 55196442e42SJason Wang unlock: 55296442e42SJason Wang rcu_read_unlock(); 55396442e42SJason Wang } 55496442e42SJason Wang 5559bc88939STom Herbert /** 5569bc88939STom Herbert * Save the hash received in the stack receive path and update the 5579bc88939STom Herbert * flow_hash table accordingly. 5589bc88939STom Herbert */ 5599bc88939STom Herbert static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash) 5609bc88939STom Herbert { 561567e4b79SEric Dumazet if (unlikely(e->rps_rxhash != hash)) 5629bc88939STom Herbert e->rps_rxhash = hash; 5639bc88939STom Herbert } 5649bc88939STom Herbert 565c8d68e6bSJason Wang /* We try to identify a flow through its rxhash first. The reason that 56692d4ea6eSstephen hemminger * we do not check rxq no. is because some cards(e.g 82599), chooses 567c8d68e6bSJason Wang * the rxq based on the txq where the last packet of the flow comes. As 568c8d68e6bSJason Wang * the userspace application move between processors, we may get a 569c8d68e6bSJason Wang * different rxq no. here. If we could not get rxhash, then we would 570c8d68e6bSJason Wang * hope the rxq no. may help here. 571c8d68e6bSJason Wang */ 57296f84061SJason Wang static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb) 573c8d68e6bSJason Wang { 57496442e42SJason Wang struct tun_flow_entry *e; 575c8d68e6bSJason Wang u32 txq = 0; 576c8d68e6bSJason Wang u32 numqueues = 0; 577c8d68e6bSJason Wang 5786aa7de05SMark Rutland numqueues = READ_ONCE(tun->numqueues); 579c8d68e6bSJason Wang 580feec084aSJason Wang txq = __skb_get_hash_symmetric(skb); 581c8d68e6bSJason Wang if (txq) { 58296442e42SJason Wang e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq); 5839bc88939STom Herbert if (e) { 5849bc88939STom Herbert tun_flow_save_rps_rxhash(e, txq); 585fbe4d456SZhi Yong Wu txq = e->queue_index; 5869bc88939STom Herbert } else 587c8d68e6bSJason Wang /* use multiply and shift instead of expensive divide */ 588c8d68e6bSJason Wang txq = ((u64)txq * numqueues) >> 32; 589c8d68e6bSJason Wang } else if (likely(skb_rx_queue_recorded(skb))) { 590c8d68e6bSJason Wang txq = skb_get_rx_queue(skb); 591c8d68e6bSJason Wang while (unlikely(txq >= numqueues)) 592c8d68e6bSJason Wang txq -= numqueues; 593c8d68e6bSJason Wang } 594c8d68e6bSJason Wang 595c8d68e6bSJason Wang return txq; 596c8d68e6bSJason Wang } 597c8d68e6bSJason Wang 59896f84061SJason Wang static u16 tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb) 59996f84061SJason Wang { 600cd5681d7SJason Wang struct tun_prog *prog; 60196f84061SJason Wang u16 ret = 0; 60296f84061SJason Wang 60396f84061SJason Wang prog = rcu_dereference(tun->steering_prog); 60496f84061SJason Wang if (prog) 60596f84061SJason Wang ret = bpf_prog_run_clear_cb(prog->prog, skb); 60696f84061SJason Wang 60796f84061SJason Wang return ret % tun->numqueues; 60896f84061SJason Wang } 60996f84061SJason Wang 61096f84061SJason Wang static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb, 61196f84061SJason Wang void *accel_priv, select_queue_fallback_t fallback) 61296f84061SJason Wang { 61396f84061SJason Wang struct tun_struct *tun = netdev_priv(dev); 61496f84061SJason Wang u16 ret; 61596f84061SJason Wang 61696f84061SJason Wang rcu_read_lock(); 61796f84061SJason Wang if (rcu_dereference(tun->steering_prog)) 61896f84061SJason Wang ret = tun_ebpf_select_queue(tun, skb); 61996f84061SJason Wang else 62096f84061SJason Wang ret = tun_automq_select_queue(tun, skb); 62196f84061SJason Wang rcu_read_unlock(); 62296f84061SJason Wang 62396f84061SJason Wang return ret; 62496f84061SJason Wang } 62596f84061SJason Wang 626cde8b15fSJason Wang static inline bool tun_not_capable(struct tun_struct *tun) 627cde8b15fSJason Wang { 628cde8b15fSJason Wang const struct cred *cred = current_cred(); 629c260b772SEric W. Biederman struct net *net = dev_net(tun->dev); 630cde8b15fSJason Wang 631cde8b15fSJason Wang return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) || 632cde8b15fSJason Wang (gid_valid(tun->group) && !in_egroup_p(tun->group))) && 633c260b772SEric W. Biederman !ns_capable(net->user_ns, CAP_NET_ADMIN); 634cde8b15fSJason Wang } 635cde8b15fSJason Wang 636c8d68e6bSJason Wang static void tun_set_real_num_queues(struct tun_struct *tun) 637c8d68e6bSJason Wang { 638c8d68e6bSJason Wang netif_set_real_num_tx_queues(tun->dev, tun->numqueues); 639c8d68e6bSJason Wang netif_set_real_num_rx_queues(tun->dev, tun->numqueues); 640c8d68e6bSJason Wang } 641c8d68e6bSJason Wang 6424008e97fSJason Wang static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) 6434008e97fSJason Wang { 6444008e97fSJason Wang tfile->detached = tun; 6454008e97fSJason Wang list_add_tail(&tfile->next, &tun->disabled); 6464008e97fSJason Wang ++tun->numdisabled; 6474008e97fSJason Wang } 6484008e97fSJason Wang 649d32649d1SJason Wang static struct tun_struct *tun_enable_queue(struct tun_file *tfile) 6504008e97fSJason Wang { 6514008e97fSJason Wang struct tun_struct *tun = tfile->detached; 6524008e97fSJason Wang 6534008e97fSJason Wang tfile->detached = NULL; 6544008e97fSJason Wang list_del_init(&tfile->next); 6554008e97fSJason Wang --tun->numdisabled; 6564008e97fSJason Wang return tun; 6574008e97fSJason Wang } 6584008e97fSJason Wang 6593a403076SJason Wang void tun_ptr_free(void *ptr) 660fc72d1d5SJason Wang { 661fc72d1d5SJason Wang if (!ptr) 662fc72d1d5SJason Wang return; 663fc72d1d5SJason Wang if (tun_is_xdp_buff(ptr)) { 664fc72d1d5SJason Wang struct xdp_buff *xdp = tun_ptr_to_xdp(ptr); 665fc72d1d5SJason Wang 666fc72d1d5SJason Wang put_page(virt_to_head_page(xdp->data)); 667fc72d1d5SJason Wang } else { 668fc72d1d5SJason Wang __skb_array_destroy_skb(ptr); 669fc72d1d5SJason Wang } 670fc72d1d5SJason Wang } 6713a403076SJason Wang EXPORT_SYMBOL_GPL(tun_ptr_free); 672fc72d1d5SJason Wang 6734bfb0513SJason Wang static void tun_queue_purge(struct tun_file *tfile) 6744bfb0513SJason Wang { 675fc72d1d5SJason Wang void *ptr; 6761576d986SJason Wang 677fc72d1d5SJason Wang while ((ptr = ptr_ring_consume(&tfile->tx_ring)) != NULL) 678fc72d1d5SJason Wang tun_ptr_free(ptr); 6791576d986SJason Wang 6805503fcecSJason Wang skb_queue_purge(&tfile->sk.sk_write_queue); 6814bfb0513SJason Wang skb_queue_purge(&tfile->sk.sk_error_queue); 6824bfb0513SJason Wang } 6834bfb0513SJason Wang 684c8d68e6bSJason Wang static void __tun_detach(struct tun_file *tfile, bool clean) 685c8d68e6bSJason Wang { 686c8d68e6bSJason Wang struct tun_file *ntfile; 687c8d68e6bSJason Wang struct tun_struct *tun; 688c8d68e6bSJason Wang 689b8deabd3SJason Wang tun = rtnl_dereference(tfile->tun); 690b8deabd3SJason Wang 69194317099SPetar Penkov if (tun && clean) { 69294317099SPetar Penkov tun_napi_disable(tun, tfile); 69394317099SPetar Penkov tun_napi_del(tun, tfile); 69494317099SPetar Penkov } 69594317099SPetar Penkov 6969e85722dSJason Wang if (tun && !tfile->detached) { 697c8d68e6bSJason Wang u16 index = tfile->queue_index; 698c8d68e6bSJason Wang BUG_ON(index >= tun->numqueues); 699c8d68e6bSJason Wang 700c8d68e6bSJason Wang rcu_assign_pointer(tun->tfiles[index], 701c8d68e6bSJason Wang tun->tfiles[tun->numqueues - 1]); 702b8deabd3SJason Wang ntfile = rtnl_dereference(tun->tfiles[index]); 703c8d68e6bSJason Wang ntfile->queue_index = index; 704c8d68e6bSJason Wang 705c8d68e6bSJason Wang --tun->numqueues; 7069e85722dSJason Wang if (clean) { 707c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 708c8d68e6bSJason Wang sock_put(&tfile->sk); 7099e85722dSJason Wang } else 7104008e97fSJason Wang tun_disable_queue(tun, tfile); 711c8d68e6bSJason Wang 712c8d68e6bSJason Wang synchronize_net(); 71396442e42SJason Wang tun_flow_delete_by_queue(tun, tun->numqueues + 1); 714c8d68e6bSJason Wang /* Drop read queue */ 7154bfb0513SJason Wang tun_queue_purge(tfile); 716c8d68e6bSJason Wang tun_set_real_num_queues(tun); 717dd38bd85SJason Wang } else if (tfile->detached && clean) { 7184008e97fSJason Wang tun = tun_enable_queue(tfile); 719dd38bd85SJason Wang sock_put(&tfile->sk); 720dd38bd85SJason Wang } 721c8d68e6bSJason Wang 722c8d68e6bSJason Wang if (clean) { 723af668b3cSMichael S. Tsirkin if (tun && tun->numqueues == 0 && tun->numdisabled == 0) { 724af668b3cSMichael S. Tsirkin netif_carrier_off(tun->dev); 725af668b3cSMichael S. Tsirkin 72640630b82SMichael S. Tsirkin if (!(tun->flags & IFF_PERSIST) && 727af668b3cSMichael S. Tsirkin tun->dev->reg_state == NETREG_REGISTERED) 7284008e97fSJason Wang unregister_netdevice(tun->dev); 729af668b3cSMichael S. Tsirkin } 730b196d88aSJason Wang if (tun) 731b196d88aSJason Wang xdp_rxq_info_unreg(&tfile->xdp_rxq); 732*7063efd3SJason Wang ptr_ring_cleanup(&tfile->tx_ring, tun_ptr_free); 733140e807dSEric W. Biederman sock_put(&tfile->sk); 734c8d68e6bSJason Wang } 735c8d68e6bSJason Wang } 736c8d68e6bSJason Wang 737c8d68e6bSJason Wang static void tun_detach(struct tun_file *tfile, bool clean) 738c8d68e6bSJason Wang { 73983c1f36fSSabrina Dubroca struct tun_struct *tun; 74083c1f36fSSabrina Dubroca struct net_device *dev; 74183c1f36fSSabrina Dubroca 742c8d68e6bSJason Wang rtnl_lock(); 74383c1f36fSSabrina Dubroca tun = rtnl_dereference(tfile->tun); 74483c1f36fSSabrina Dubroca dev = tun ? tun->dev : NULL; 745c8d68e6bSJason Wang __tun_detach(tfile, clean); 74683c1f36fSSabrina Dubroca if (dev) 74783c1f36fSSabrina Dubroca netdev_state_change(dev); 748c8d68e6bSJason Wang rtnl_unlock(); 749c8d68e6bSJason Wang } 750c8d68e6bSJason Wang 751c8d68e6bSJason Wang static void tun_detach_all(struct net_device *dev) 752c8d68e6bSJason Wang { 753c8d68e6bSJason Wang struct tun_struct *tun = netdev_priv(dev); 7544008e97fSJason Wang struct tun_file *tfile, *tmp; 755c8d68e6bSJason Wang int i, n = tun->numqueues; 756c8d68e6bSJason Wang 757c8d68e6bSJason Wang for (i = 0; i < n; i++) { 758b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 759c8d68e6bSJason Wang BUG_ON(!tfile); 76094317099SPetar Penkov tun_napi_disable(tun, tfile); 761addf8fc4SJason Wang tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; 7629e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 763c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 764c8d68e6bSJason Wang --tun->numqueues; 765c8d68e6bSJason Wang } 7669e85722dSJason Wang list_for_each_entry(tfile, &tun->disabled, next) { 767addf8fc4SJason Wang tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; 7689e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 769c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 7709e85722dSJason Wang } 771c8d68e6bSJason Wang BUG_ON(tun->numqueues != 0); 772c8d68e6bSJason Wang 773c8d68e6bSJason Wang synchronize_net(); 774c8d68e6bSJason Wang for (i = 0; i < n; i++) { 775b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 77694317099SPetar Penkov tun_napi_del(tun, tfile); 777c8d68e6bSJason Wang /* Drop read queue */ 7784bfb0513SJason Wang tun_queue_purge(tfile); 779b196d88aSJason Wang xdp_rxq_info_unreg(&tfile->xdp_rxq); 780c8d68e6bSJason Wang sock_put(&tfile->sk); 781c8d68e6bSJason Wang } 7824008e97fSJason Wang list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { 7834008e97fSJason Wang tun_enable_queue(tfile); 7844bfb0513SJason Wang tun_queue_purge(tfile); 785b196d88aSJason Wang xdp_rxq_info_unreg(&tfile->xdp_rxq); 7864008e97fSJason Wang sock_put(&tfile->sk); 7874008e97fSJason Wang } 7884008e97fSJason Wang BUG_ON(tun->numdisabled != 0); 789dd38bd85SJason Wang 79040630b82SMichael S. Tsirkin if (tun->flags & IFF_PERSIST) 791dd38bd85SJason Wang module_put(THIS_MODULE); 792c8d68e6bSJason Wang } 793c8d68e6bSJason Wang 79494317099SPetar Penkov static int tun_attach(struct tun_struct *tun, struct file *file, 79594317099SPetar Penkov bool skip_filter, bool napi) 796a7385ba2SEric W. Biederman { 797631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 7981576d986SJason Wang struct net_device *dev = tun->dev; 79938231b7aSEric W. Biederman int err; 800a7385ba2SEric W. Biederman 8015dbbaf2dSPaul Moore err = security_tun_dev_attach(tfile->socket.sk, tun->security); 8025dbbaf2dSPaul Moore if (err < 0) 8035dbbaf2dSPaul Moore goto out; 8045dbbaf2dSPaul Moore 80538231b7aSEric W. Biederman err = -EINVAL; 8069e85722dSJason Wang if (rtnl_dereference(tfile->tun) && !tfile->detached) 80738231b7aSEric W. Biederman goto out; 80838231b7aSEric W. Biederman 80938231b7aSEric W. Biederman err = -EBUSY; 81040630b82SMichael S. Tsirkin if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1) 811c8d68e6bSJason Wang goto out; 812c8d68e6bSJason Wang 813c8d68e6bSJason Wang err = -E2BIG; 8144008e97fSJason Wang if (!tfile->detached && 8154008e97fSJason Wang tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES) 81638231b7aSEric W. Biederman goto out; 81738231b7aSEric W. Biederman 81838231b7aSEric W. Biederman err = 0; 81954f968d6SJason Wang 82092d4ea6eSstephen hemminger /* Re-attach the filter to persist device */ 821849c9b6fSPavel Emelyanov if (!skip_filter && (tun->filter_attached == true)) { 8228ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 8238ced425eSHannes Frederic Sowa err = sk_attach_filter(&tun->fprog, tfile->socket.sk); 8248ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 82554f968d6SJason Wang if (!err) 82654f968d6SJason Wang goto out; 82754f968d6SJason Wang } 8281576d986SJason Wang 8291576d986SJason Wang if (!tfile->detached && 830b196d88aSJason Wang ptr_ring_resize(&tfile->tx_ring, dev->tx_queue_len, 831b196d88aSJason Wang GFP_KERNEL, tun_ptr_free)) { 8321576d986SJason Wang err = -ENOMEM; 8331576d986SJason Wang goto out; 8341576d986SJason Wang } 8351576d986SJason Wang 836c8d68e6bSJason Wang tfile->queue_index = tun->numqueues; 837addf8fc4SJason Wang tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN; 8388bf5c4eeSJesper Dangaard Brouer 8398bf5c4eeSJesper Dangaard Brouer if (tfile->detached) { 8408bf5c4eeSJesper Dangaard Brouer /* Re-attach detached tfile, updating XDP queue_index */ 8418bf5c4eeSJesper Dangaard Brouer WARN_ON(!xdp_rxq_info_is_reg(&tfile->xdp_rxq)); 8428bf5c4eeSJesper Dangaard Brouer 8438bf5c4eeSJesper Dangaard Brouer if (tfile->xdp_rxq.queue_index != tfile->queue_index) 8448bf5c4eeSJesper Dangaard Brouer tfile->xdp_rxq.queue_index = tfile->queue_index; 8458bf5c4eeSJesper Dangaard Brouer } else { 8468bf5c4eeSJesper Dangaard Brouer /* Setup XDP RX-queue info, for new tfile getting attached */ 8478bf5c4eeSJesper Dangaard Brouer err = xdp_rxq_info_reg(&tfile->xdp_rxq, 8488bf5c4eeSJesper Dangaard Brouer tun->dev, tfile->queue_index); 8498bf5c4eeSJesper Dangaard Brouer if (err < 0) 8508bf5c4eeSJesper Dangaard Brouer goto out; 8518bf5c4eeSJesper Dangaard Brouer err = 0; 8528bf5c4eeSJesper Dangaard Brouer } 8538bf5c4eeSJesper Dangaard Brouer 8546e914fc7SJason Wang rcu_assign_pointer(tfile->tun, tun); 855c8d68e6bSJason Wang rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); 856c8d68e6bSJason Wang tun->numqueues++; 857c8d68e6bSJason Wang 85894317099SPetar Penkov if (tfile->detached) { 8594008e97fSJason Wang tun_enable_queue(tfile); 86094317099SPetar Penkov } else { 8614008e97fSJason Wang sock_hold(&tfile->sk); 86294317099SPetar Penkov tun_napi_init(tun, tfile, napi); 86394317099SPetar Penkov } 8644008e97fSJason Wang 865c8d68e6bSJason Wang tun_set_real_num_queues(tun); 866c8d68e6bSJason Wang 867c8d68e6bSJason Wang /* device is allowed to go away first, so no need to hold extra 868c8d68e6bSJason Wang * refcnt. 869c8d68e6bSJason Wang */ 870a7385ba2SEric W. Biederman 87138231b7aSEric W. Biederman out: 87238231b7aSEric W. Biederman return err; 873a7385ba2SEric W. Biederman } 874a7385ba2SEric W. Biederman 8759484dc74Syuan linyu static struct tun_struct *tun_get(struct tun_file *tfile) 876631ab46bSEric W. Biederman { 8776e914fc7SJason Wang struct tun_struct *tun; 878c70f1829SEric W. Biederman 8796e914fc7SJason Wang rcu_read_lock(); 8806e914fc7SJason Wang tun = rcu_dereference(tfile->tun); 8816e914fc7SJason Wang if (tun) 8826e914fc7SJason Wang dev_hold(tun->dev); 8836e914fc7SJason Wang rcu_read_unlock(); 884c70f1829SEric W. Biederman 885c70f1829SEric W. Biederman return tun; 886631ab46bSEric W. Biederman } 887631ab46bSEric W. Biederman 888631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun) 889631ab46bSEric W. Biederman { 8906e914fc7SJason Wang dev_put(tun->dev); 891631ab46bSEric W. Biederman } 892631ab46bSEric W. Biederman 8936b8a66eeSJoe Perches /* TAP filtering */ 894f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr) 895f271b2ccSMax Krasnyansky { 896f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 897f271b2ccSMax Krasnyansky mask[n >> 5] |= (1 << (n & 31)); 898f271b2ccSMax Krasnyansky } 899f271b2ccSMax Krasnyansky 900f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) 901f271b2ccSMax Krasnyansky { 902f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 903f271b2ccSMax Krasnyansky return mask[n >> 5] & (1 << (n & 31)); 904f271b2ccSMax Krasnyansky } 905f271b2ccSMax Krasnyansky 906f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg) 907f271b2ccSMax Krasnyansky { 908f271b2ccSMax Krasnyansky struct { u8 u[ETH_ALEN]; } *addr; 909f271b2ccSMax Krasnyansky struct tun_filter uf; 910f271b2ccSMax Krasnyansky int err, alen, n, nexact; 911f271b2ccSMax Krasnyansky 912f271b2ccSMax Krasnyansky if (copy_from_user(&uf, arg, sizeof(uf))) 913f271b2ccSMax Krasnyansky return -EFAULT; 914f271b2ccSMax Krasnyansky 915f271b2ccSMax Krasnyansky if (!uf.count) { 916f271b2ccSMax Krasnyansky /* Disabled */ 917f271b2ccSMax Krasnyansky filter->count = 0; 918f271b2ccSMax Krasnyansky return 0; 919f271b2ccSMax Krasnyansky } 920f271b2ccSMax Krasnyansky 921f271b2ccSMax Krasnyansky alen = ETH_ALEN * uf.count; 92228e8190dSMarkus Elfring addr = memdup_user(arg + sizeof(uf), alen); 92328e8190dSMarkus Elfring if (IS_ERR(addr)) 92428e8190dSMarkus Elfring return PTR_ERR(addr); 925f271b2ccSMax Krasnyansky 926f271b2ccSMax Krasnyansky /* The filter is updated without holding any locks. Which is 927f271b2ccSMax Krasnyansky * perfectly safe. We disable it first and in the worst 928f271b2ccSMax Krasnyansky * case we'll accept a few undesired packets. */ 929f271b2ccSMax Krasnyansky filter->count = 0; 930f271b2ccSMax Krasnyansky wmb(); 931f271b2ccSMax Krasnyansky 932f271b2ccSMax Krasnyansky /* Use first set of addresses as an exact filter */ 933f271b2ccSMax Krasnyansky for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++) 934f271b2ccSMax Krasnyansky memcpy(filter->addr[n], addr[n].u, ETH_ALEN); 935f271b2ccSMax Krasnyansky 936f271b2ccSMax Krasnyansky nexact = n; 937f271b2ccSMax Krasnyansky 938cfbf84fcSAlex Williamson /* Remaining multicast addresses are hashed, 939cfbf84fcSAlex Williamson * unicast will leave the filter disabled. */ 940f271b2ccSMax Krasnyansky memset(filter->mask, 0, sizeof(filter->mask)); 941cfbf84fcSAlex Williamson for (; n < uf.count; n++) { 942cfbf84fcSAlex Williamson if (!is_multicast_ether_addr(addr[n].u)) { 943cfbf84fcSAlex Williamson err = 0; /* no filter */ 9443b8d2a69SMarkus Elfring goto free_addr; 945cfbf84fcSAlex Williamson } 946f271b2ccSMax Krasnyansky addr_hash_set(filter->mask, addr[n].u); 947cfbf84fcSAlex Williamson } 948f271b2ccSMax Krasnyansky 949f271b2ccSMax Krasnyansky /* For ALLMULTI just set the mask to all ones. 950f271b2ccSMax Krasnyansky * This overrides the mask populated above. */ 951f271b2ccSMax Krasnyansky if ((uf.flags & TUN_FLT_ALLMULTI)) 952f271b2ccSMax Krasnyansky memset(filter->mask, ~0, sizeof(filter->mask)); 953f271b2ccSMax Krasnyansky 954f271b2ccSMax Krasnyansky /* Now enable the filter */ 955f271b2ccSMax Krasnyansky wmb(); 956f271b2ccSMax Krasnyansky filter->count = nexact; 957f271b2ccSMax Krasnyansky 958f271b2ccSMax Krasnyansky /* Return the number of exact filters */ 959f271b2ccSMax Krasnyansky err = nexact; 9603b8d2a69SMarkus Elfring free_addr: 961f271b2ccSMax Krasnyansky kfree(addr); 962f271b2ccSMax Krasnyansky return err; 963f271b2ccSMax Krasnyansky } 964f271b2ccSMax Krasnyansky 965f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */ 966f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb) 967f271b2ccSMax Krasnyansky { 968f271b2ccSMax Krasnyansky /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect 969f271b2ccSMax Krasnyansky * at this point. */ 970f271b2ccSMax Krasnyansky struct ethhdr *eh = (struct ethhdr *) skb->data; 971f271b2ccSMax Krasnyansky int i; 972f271b2ccSMax Krasnyansky 973f271b2ccSMax Krasnyansky /* Exact match */ 974f271b2ccSMax Krasnyansky for (i = 0; i < filter->count; i++) 9752e42e474SJoe Perches if (ether_addr_equal(eh->h_dest, filter->addr[i])) 976f271b2ccSMax Krasnyansky return 1; 977f271b2ccSMax Krasnyansky 978f271b2ccSMax Krasnyansky /* Inexact match (multicast only) */ 979f271b2ccSMax Krasnyansky if (is_multicast_ether_addr(eh->h_dest)) 980f271b2ccSMax Krasnyansky return addr_hash_test(filter->mask, eh->h_dest); 981f271b2ccSMax Krasnyansky 982f271b2ccSMax Krasnyansky return 0; 983f271b2ccSMax Krasnyansky } 984f271b2ccSMax Krasnyansky 985f271b2ccSMax Krasnyansky /* 986f271b2ccSMax Krasnyansky * Checks whether the packet is accepted or not. 987f271b2ccSMax Krasnyansky * Returns: 0 - drop, !=0 - accept 988f271b2ccSMax Krasnyansky */ 989f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb) 990f271b2ccSMax Krasnyansky { 991f271b2ccSMax Krasnyansky if (!filter->count) 992f271b2ccSMax Krasnyansky return 1; 993f271b2ccSMax Krasnyansky 994f271b2ccSMax Krasnyansky return run_filter(filter, skb); 995f271b2ccSMax Krasnyansky } 996f271b2ccSMax Krasnyansky 9971da177e4SLinus Torvalds /* Network device part of the driver */ 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds static const struct ethtool_ops tun_ethtool_ops; 10001da177e4SLinus Torvalds 1001c70f1829SEric W. Biederman /* Net device detach from fd. */ 1002c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev) 1003c70f1829SEric W. Biederman { 1004c8d68e6bSJason Wang tun_detach_all(dev); 1005c70f1829SEric W. Biederman } 1006c70f1829SEric W. Biederman 10071da177e4SLinus Torvalds /* Net device open. */ 10081da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev) 10091da177e4SLinus Torvalds { 1010b20e2d54SHannes Frederic Sowa struct tun_struct *tun = netdev_priv(dev); 1011b20e2d54SHannes Frederic Sowa int i; 1012b20e2d54SHannes Frederic Sowa 1013c8d68e6bSJason Wang netif_tx_start_all_queues(dev); 1014b20e2d54SHannes Frederic Sowa 1015b20e2d54SHannes Frederic Sowa for (i = 0; i < tun->numqueues; i++) { 1016b20e2d54SHannes Frederic Sowa struct tun_file *tfile; 1017b20e2d54SHannes Frederic Sowa 1018b20e2d54SHannes Frederic Sowa tfile = rtnl_dereference(tun->tfiles[i]); 1019b20e2d54SHannes Frederic Sowa tfile->socket.sk->sk_write_space(tfile->socket.sk); 1020b20e2d54SHannes Frederic Sowa } 1021b20e2d54SHannes Frederic Sowa 10221da177e4SLinus Torvalds return 0; 10231da177e4SLinus Torvalds } 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds /* Net device close. */ 10261da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev) 10271da177e4SLinus Torvalds { 1028c8d68e6bSJason Wang netif_tx_stop_all_queues(dev); 10291da177e4SLinus Torvalds return 0; 10301da177e4SLinus Torvalds } 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds /* Net device start xmit */ 103396f84061SJason Wang static void tun_automq_xmit(struct tun_struct *tun, struct sk_buff *skb) 10341da177e4SLinus Torvalds { 10353df97ba8SJason Wang #ifdef CONFIG_RPS 103696f84061SJason Wang if (tun->numqueues == 1 && static_key_false(&rps_needed)) { 10379bc88939STom Herbert /* Select queue was not called for the skbuff, so we extract the 10389bc88939STom Herbert * RPS hash and save it into the flow_table here. 10399bc88939STom Herbert */ 10409bc88939STom Herbert __u32 rxhash; 10419bc88939STom Herbert 1042feec084aSJason Wang rxhash = __skb_get_hash_symmetric(skb); 10439bc88939STom Herbert if (rxhash) { 10449bc88939STom Herbert struct tun_flow_entry *e; 10459bc88939STom Herbert e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)], 10469bc88939STom Herbert rxhash); 10479bc88939STom Herbert if (e) 10489bc88939STom Herbert tun_flow_save_rps_rxhash(e, rxhash); 10499bc88939STom Herbert } 10509bc88939STom Herbert } 10513df97ba8SJason Wang #endif 105296f84061SJason Wang } 105396f84061SJason Wang 1054aff3d70aSJason Wang static unsigned int run_ebpf_filter(struct tun_struct *tun, 1055aff3d70aSJason Wang struct sk_buff *skb, 1056aff3d70aSJason Wang int len) 1057aff3d70aSJason Wang { 1058aff3d70aSJason Wang struct tun_prog *prog = rcu_dereference(tun->filter_prog); 1059aff3d70aSJason Wang 1060aff3d70aSJason Wang if (prog) 1061aff3d70aSJason Wang len = bpf_prog_run_clear_cb(prog->prog, skb); 1062aff3d70aSJason Wang 1063aff3d70aSJason Wang return len; 1064aff3d70aSJason Wang } 1065aff3d70aSJason Wang 106696f84061SJason Wang /* Net device start xmit */ 106796f84061SJason Wang static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) 106896f84061SJason Wang { 106996f84061SJason Wang struct tun_struct *tun = netdev_priv(dev); 107096f84061SJason Wang int txq = skb->queue_mapping; 107196f84061SJason Wang struct tun_file *tfile; 1072aff3d70aSJason Wang int len = skb->len; 107396f84061SJason Wang 107496f84061SJason Wang rcu_read_lock(); 107596f84061SJason Wang tfile = rcu_dereference(tun->tfiles[txq]); 107696f84061SJason Wang 107796f84061SJason Wang /* Drop packet if interface is not attached */ 1078cc166427SWillem de Bruijn if (txq >= tun->numqueues) 107996f84061SJason Wang goto drop; 108096f84061SJason Wang 108196f84061SJason Wang if (!rcu_dereference(tun->steering_prog)) 108296f84061SJason Wang tun_automq_xmit(tun, skb); 10839bc88939STom Herbert 10846e914fc7SJason Wang tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len); 10856e914fc7SJason Wang 1086c8d68e6bSJason Wang BUG_ON(!tfile); 1087c8d68e6bSJason Wang 1088f271b2ccSMax Krasnyansky /* Drop if the filter does not like it. 1089f271b2ccSMax Krasnyansky * This is a noop if the filter is disabled. 1090f271b2ccSMax Krasnyansky * Filter can be enabled only for the TAP devices. */ 1091f271b2ccSMax Krasnyansky if (!check_filter(&tun->txflt, skb)) 1092f271b2ccSMax Krasnyansky goto drop; 1093f271b2ccSMax Krasnyansky 109454f968d6SJason Wang if (tfile->socket.sk->sk_filter && 109554f968d6SJason Wang sk_filter(tfile->socket.sk, skb)) 109699405162SMichael S. Tsirkin goto drop; 109799405162SMichael S. Tsirkin 1098aff3d70aSJason Wang len = run_ebpf_filter(tun, skb, len); 109981c89507SBjørn Mork if (len == 0 || pskb_trim(skb, len)) 1100aff3d70aSJason Wang goto drop; 1101aff3d70aSJason Wang 11021f8b977aSWillem de Bruijn if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) 11037bf66305SJason Wang goto drop; 11047bf66305SJason Wang 11057b996243SSoheil Hassas Yeganeh skb_tx_timestamp(skb); 1106eda29772SRichard Cochran 11070110d6f2SMichael S. Tsirkin /* Orphan the skb - required as we might hang on to it 11087bf66305SJason Wang * for indefinite time. 11097bf66305SJason Wang */ 11100110d6f2SMichael S. Tsirkin skb_orphan(skb); 11110110d6f2SMichael S. Tsirkin 1112f8af75f3SEric Dumazet nf_reset(skb); 1113f8af75f3SEric Dumazet 11145990a305SJason Wang if (ptr_ring_produce(&tfile->tx_ring, skb)) 11151576d986SJason Wang goto drop; 11161da177e4SLinus Torvalds 11171da177e4SLinus Torvalds /* Notify and wake up reader process */ 111854f968d6SJason Wang if (tfile->flags & TUN_FASYNC) 111954f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_IN); 11209e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 11216e914fc7SJason Wang 11226e914fc7SJason Wang rcu_read_unlock(); 11236ed10654SPatrick McHardy return NETDEV_TX_OK; 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds drop: 1126608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->tx_dropped); 1127149d36f7SMichael S. Tsirkin skb_tx_error(skb); 11281da177e4SLinus Torvalds kfree_skb(skb); 11296e914fc7SJason Wang rcu_read_unlock(); 1130baeababbSJason Wang return NET_XMIT_DROP; 11311da177e4SLinus Torvalds } 11321da177e4SLinus Torvalds 1133f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev) 11341da177e4SLinus Torvalds { 1135f271b2ccSMax Krasnyansky /* 1136f271b2ccSMax Krasnyansky * This callback is supposed to deal with mc filter in 1137f271b2ccSMax Krasnyansky * _rx_ path and has nothing to do with the _tx_ path. 1138f271b2ccSMax Krasnyansky * In rx path we always accept everything userspace gives us. 1139f271b2ccSMax Krasnyansky */ 11401da177e4SLinus Torvalds } 11411da177e4SLinus Torvalds 1142c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev, 1143c8f44affSMichał Mirosław netdev_features_t features) 114488255375SMichał Mirosław { 114588255375SMichał Mirosław struct tun_struct *tun = netdev_priv(dev); 114688255375SMichał Mirosław 114788255375SMichał Mirosław return (features & tun->set_features) | (features & ~TUN_USER_FEATURES); 114888255375SMichał Mirosław } 1149bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 1150bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev) 1151bebd097aSNeil Horman { 1152bebd097aSNeil Horman /* 1153bebd097aSNeil Horman * Tun only receives frames when: 1154bebd097aSNeil Horman * 1) the char device endpoint gets data from user space 1155bebd097aSNeil Horman * 2) the tun socket gets a sendmsg call from user space 115694317099SPetar Penkov * If NAPI is not enabled, since both of those are synchronous 115794317099SPetar Penkov * operations, we are guaranteed never to have pending data when we poll 115894317099SPetar Penkov * for it so there is nothing to do here but return. 1159bebd097aSNeil Horman * We need this though so netpoll recognizes us as an interface that 1160bebd097aSNeil Horman * supports polling, which enables bridge devices in virt setups to 1161bebd097aSNeil Horman * still use netconsole 116294317099SPetar Penkov * If NAPI is enabled, however, we need to schedule polling for all 116390e33d45SPetar Penkov * queues unless we are using napi_gro_frags(), which we call in 116490e33d45SPetar Penkov * process context and not in NAPI context. 1165bebd097aSNeil Horman */ 116694317099SPetar Penkov struct tun_struct *tun = netdev_priv(dev); 116794317099SPetar Penkov 116894317099SPetar Penkov if (tun->flags & IFF_NAPI) { 116994317099SPetar Penkov struct tun_file *tfile; 117094317099SPetar Penkov int i; 117194317099SPetar Penkov 117290e33d45SPetar Penkov if (tun_napi_frags_enabled(tun)) 117390e33d45SPetar Penkov return; 117490e33d45SPetar Penkov 117594317099SPetar Penkov rcu_read_lock(); 117694317099SPetar Penkov for (i = 0; i < tun->numqueues; i++) { 117794317099SPetar Penkov tfile = rcu_dereference(tun->tfiles[i]); 1178aec72f33SEric Dumazet if (tfile->napi_enabled) 117994317099SPetar Penkov napi_schedule(&tfile->napi); 118094317099SPetar Penkov } 118194317099SPetar Penkov rcu_read_unlock(); 118294317099SPetar Penkov } 1183bebd097aSNeil Horman return; 1184bebd097aSNeil Horman } 1185bebd097aSNeil Horman #endif 1186eaea34b2SPaolo Abeni 1187eaea34b2SPaolo Abeni static void tun_set_headroom(struct net_device *dev, int new_hr) 1188eaea34b2SPaolo Abeni { 1189eaea34b2SPaolo Abeni struct tun_struct *tun = netdev_priv(dev); 1190eaea34b2SPaolo Abeni 1191eaea34b2SPaolo Abeni if (new_hr < NET_SKB_PAD) 1192eaea34b2SPaolo Abeni new_hr = NET_SKB_PAD; 1193eaea34b2SPaolo Abeni 1194eaea34b2SPaolo Abeni tun->align = new_hr; 1195eaea34b2SPaolo Abeni } 1196eaea34b2SPaolo Abeni 1197bc1f4470Sstephen hemminger static void 1198608b9977SPaolo Abeni tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 1199608b9977SPaolo Abeni { 1200608b9977SPaolo Abeni u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0; 1201608b9977SPaolo Abeni struct tun_struct *tun = netdev_priv(dev); 1202608b9977SPaolo Abeni struct tun_pcpu_stats *p; 1203608b9977SPaolo Abeni int i; 1204608b9977SPaolo Abeni 1205608b9977SPaolo Abeni for_each_possible_cpu(i) { 1206608b9977SPaolo Abeni u64 rxpackets, rxbytes, txpackets, txbytes; 1207608b9977SPaolo Abeni unsigned int start; 1208608b9977SPaolo Abeni 1209608b9977SPaolo Abeni p = per_cpu_ptr(tun->pcpu_stats, i); 1210608b9977SPaolo Abeni do { 1211608b9977SPaolo Abeni start = u64_stats_fetch_begin(&p->syncp); 1212608b9977SPaolo Abeni rxpackets = p->rx_packets; 1213608b9977SPaolo Abeni rxbytes = p->rx_bytes; 1214608b9977SPaolo Abeni txpackets = p->tx_packets; 1215608b9977SPaolo Abeni txbytes = p->tx_bytes; 1216608b9977SPaolo Abeni } while (u64_stats_fetch_retry(&p->syncp, start)); 1217608b9977SPaolo Abeni 1218608b9977SPaolo Abeni stats->rx_packets += rxpackets; 1219608b9977SPaolo Abeni stats->rx_bytes += rxbytes; 1220608b9977SPaolo Abeni stats->tx_packets += txpackets; 1221608b9977SPaolo Abeni stats->tx_bytes += txbytes; 1222608b9977SPaolo Abeni 1223608b9977SPaolo Abeni /* u32 counters */ 1224608b9977SPaolo Abeni rx_dropped += p->rx_dropped; 1225608b9977SPaolo Abeni rx_frame_errors += p->rx_frame_errors; 1226608b9977SPaolo Abeni tx_dropped += p->tx_dropped; 1227608b9977SPaolo Abeni } 1228608b9977SPaolo Abeni stats->rx_dropped = rx_dropped; 1229608b9977SPaolo Abeni stats->rx_frame_errors = rx_frame_errors; 1230608b9977SPaolo Abeni stats->tx_dropped = tx_dropped; 1231608b9977SPaolo Abeni } 1232608b9977SPaolo Abeni 1233761876c8SJason Wang static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog, 1234761876c8SJason Wang struct netlink_ext_ack *extack) 1235761876c8SJason Wang { 1236761876c8SJason Wang struct tun_struct *tun = netdev_priv(dev); 1237761876c8SJason Wang struct bpf_prog *old_prog; 1238761876c8SJason Wang 1239761876c8SJason Wang old_prog = rtnl_dereference(tun->xdp_prog); 1240761876c8SJason Wang rcu_assign_pointer(tun->xdp_prog, prog); 1241761876c8SJason Wang if (old_prog) 1242761876c8SJason Wang bpf_prog_put(old_prog); 1243761876c8SJason Wang 1244761876c8SJason Wang return 0; 1245761876c8SJason Wang } 1246761876c8SJason Wang 1247761876c8SJason Wang static u32 tun_xdp_query(struct net_device *dev) 1248761876c8SJason Wang { 1249761876c8SJason Wang struct tun_struct *tun = netdev_priv(dev); 1250761876c8SJason Wang const struct bpf_prog *xdp_prog; 1251761876c8SJason Wang 1252761876c8SJason Wang xdp_prog = rtnl_dereference(tun->xdp_prog); 1253761876c8SJason Wang if (xdp_prog) 1254761876c8SJason Wang return xdp_prog->aux->id; 1255761876c8SJason Wang 1256761876c8SJason Wang return 0; 1257761876c8SJason Wang } 1258761876c8SJason Wang 1259f4e63525SJakub Kicinski static int tun_xdp(struct net_device *dev, struct netdev_bpf *xdp) 1260761876c8SJason Wang { 1261761876c8SJason Wang switch (xdp->command) { 1262761876c8SJason Wang case XDP_SETUP_PROG: 1263761876c8SJason Wang return tun_xdp_set(dev, xdp->prog, xdp->extack); 1264761876c8SJason Wang case XDP_QUERY_PROG: 1265761876c8SJason Wang xdp->prog_id = tun_xdp_query(dev); 1266761876c8SJason Wang xdp->prog_attached = !!xdp->prog_id; 1267761876c8SJason Wang return 0; 1268761876c8SJason Wang default: 1269761876c8SJason Wang return -EINVAL; 1270761876c8SJason Wang } 1271761876c8SJason Wang } 1272761876c8SJason Wang 1273758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = { 1274c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 1275758e43b7SStephen Hemminger .ndo_open = tun_net_open, 1276758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 127700829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 127888255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 1279c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 1280bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 1281bebd097aSNeil Horman .ndo_poll_controller = tun_poll_controller, 1282bebd097aSNeil Horman #endif 1283eaea34b2SPaolo Abeni .ndo_set_rx_headroom = tun_set_headroom, 1284608b9977SPaolo Abeni .ndo_get_stats64 = tun_net_get_stats64, 1285758e43b7SStephen Hemminger }; 1286758e43b7SStephen Hemminger 1287fc72d1d5SJason Wang static int tun_xdp_xmit(struct net_device *dev, struct xdp_buff *xdp) 1288fc72d1d5SJason Wang { 1289fc72d1d5SJason Wang struct tun_struct *tun = netdev_priv(dev); 1290fc72d1d5SJason Wang struct xdp_buff *buff = xdp->data_hard_start; 1291fc72d1d5SJason Wang int headroom = xdp->data - xdp->data_hard_start; 1292fc72d1d5SJason Wang struct tun_file *tfile; 1293fc72d1d5SJason Wang u32 numqueues; 1294fc72d1d5SJason Wang int ret = 0; 1295fc72d1d5SJason Wang 1296fc72d1d5SJason Wang /* Assure headroom is available and buff is properly aligned */ 1297fc72d1d5SJason Wang if (unlikely(headroom < sizeof(*xdp) || tun_is_xdp_buff(xdp))) 1298fc72d1d5SJason Wang return -ENOSPC; 1299fc72d1d5SJason Wang 1300fc72d1d5SJason Wang *buff = *xdp; 1301fc72d1d5SJason Wang 1302fc72d1d5SJason Wang rcu_read_lock(); 1303fc72d1d5SJason Wang 1304fc72d1d5SJason Wang numqueues = READ_ONCE(tun->numqueues); 1305fc72d1d5SJason Wang if (!numqueues) { 1306fc72d1d5SJason Wang ret = -ENOSPC; 1307fc72d1d5SJason Wang goto out; 1308fc72d1d5SJason Wang } 1309fc72d1d5SJason Wang 1310fc72d1d5SJason Wang tfile = rcu_dereference(tun->tfiles[smp_processor_id() % 1311fc72d1d5SJason Wang numqueues]); 1312fc72d1d5SJason Wang /* Encode the XDP flag into lowest bit for consumer to differ 1313fc72d1d5SJason Wang * XDP buffer from sk_buff. 1314fc72d1d5SJason Wang */ 1315fc72d1d5SJason Wang if (ptr_ring_produce(&tfile->tx_ring, tun_xdp_to_ptr(buff))) { 1316fc72d1d5SJason Wang this_cpu_inc(tun->pcpu_stats->tx_dropped); 1317fc72d1d5SJason Wang ret = -ENOSPC; 1318fc72d1d5SJason Wang } 1319fc72d1d5SJason Wang 1320fc72d1d5SJason Wang out: 1321fc72d1d5SJason Wang rcu_read_unlock(); 1322fc72d1d5SJason Wang return ret; 1323fc72d1d5SJason Wang } 1324fc72d1d5SJason Wang 1325fc72d1d5SJason Wang static void tun_xdp_flush(struct net_device *dev) 1326fc72d1d5SJason Wang { 1327fc72d1d5SJason Wang struct tun_struct *tun = netdev_priv(dev); 1328fc72d1d5SJason Wang struct tun_file *tfile; 1329fc72d1d5SJason Wang u32 numqueues; 1330fc72d1d5SJason Wang 1331fc72d1d5SJason Wang rcu_read_lock(); 1332fc72d1d5SJason Wang 1333fc72d1d5SJason Wang numqueues = READ_ONCE(tun->numqueues); 1334fc72d1d5SJason Wang if (!numqueues) 1335fc72d1d5SJason Wang goto out; 1336fc72d1d5SJason Wang 1337fc72d1d5SJason Wang tfile = rcu_dereference(tun->tfiles[smp_processor_id() % 1338fc72d1d5SJason Wang numqueues]); 1339fc72d1d5SJason Wang /* Notify and wake up reader process */ 1340fc72d1d5SJason Wang if (tfile->flags & TUN_FASYNC) 1341fc72d1d5SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_IN); 1342fc72d1d5SJason Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 1343fc72d1d5SJason Wang 1344fc72d1d5SJason Wang out: 1345fc72d1d5SJason Wang rcu_read_unlock(); 1346fc72d1d5SJason Wang } 1347fc72d1d5SJason Wang 1348758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = { 1349c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 1350758e43b7SStephen Hemminger .ndo_open = tun_net_open, 1351758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 135200829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 135388255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 1354afc4b13dSJiri Pirko .ndo_set_rx_mode = tun_net_mclist, 1355758e43b7SStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 1356758e43b7SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 1357c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 1358bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 1359bebd097aSNeil Horman .ndo_poll_controller = tun_poll_controller, 1360bebd097aSNeil Horman #endif 13615e52796aSToshiaki Makita .ndo_features_check = passthru_features_check, 1362eaea34b2SPaolo Abeni .ndo_set_rx_headroom = tun_set_headroom, 1363608b9977SPaolo Abeni .ndo_get_stats64 = tun_net_get_stats64, 1364f4e63525SJakub Kicinski .ndo_bpf = tun_xdp, 1365fc72d1d5SJason Wang .ndo_xdp_xmit = tun_xdp_xmit, 1366fc72d1d5SJason Wang .ndo_xdp_flush = tun_xdp_flush, 1367758e43b7SStephen Hemminger }; 1368758e43b7SStephen Hemminger 1369944a1376SPavel Emelyanov static void tun_flow_init(struct tun_struct *tun) 137096442e42SJason Wang { 137196442e42SJason Wang int i; 137296442e42SJason Wang 137396442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) 137496442e42SJason Wang INIT_HLIST_HEAD(&tun->flows[i]); 137596442e42SJason Wang 137696442e42SJason Wang tun->ageing_time = TUN_FLOW_EXPIRE; 1377e99e88a9SKees Cook timer_setup(&tun->flow_gc_timer, tun_flow_cleanup, 0); 1378e99e88a9SKees Cook mod_timer(&tun->flow_gc_timer, 1379e99e88a9SKees Cook round_jiffies_up(jiffies + tun->ageing_time)); 138096442e42SJason Wang } 138196442e42SJason Wang 138296442e42SJason Wang static void tun_flow_uninit(struct tun_struct *tun) 138396442e42SJason Wang { 138496442e42SJason Wang del_timer_sync(&tun->flow_gc_timer); 138596442e42SJason Wang tun_flow_flush(tun); 138696442e42SJason Wang } 138796442e42SJason Wang 138891572088SJarod Wilson #define MIN_MTU 68 138991572088SJarod Wilson #define MAX_MTU 65535 139091572088SJarod Wilson 13911da177e4SLinus Torvalds /* Initialize net device. */ 13921da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev) 13931da177e4SLinus Torvalds { 13941da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 139740630b82SMichael S. Tsirkin case IFF_TUN: 1398758e43b7SStephen Hemminger dev->netdev_ops = &tun_netdev_ops; 1399758e43b7SStephen Hemminger 14001da177e4SLinus Torvalds /* Point-to-Point TUN Device */ 14011da177e4SLinus Torvalds dev->hard_header_len = 0; 14021da177e4SLinus Torvalds dev->addr_len = 0; 14031da177e4SLinus Torvalds dev->mtu = 1500; 14041da177e4SLinus Torvalds 14051da177e4SLinus Torvalds /* Zero header length */ 14061da177e4SLinus Torvalds dev->type = ARPHRD_NONE; 14071da177e4SLinus Torvalds dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 14081da177e4SLinus Torvalds break; 14091da177e4SLinus Torvalds 141040630b82SMichael S. Tsirkin case IFF_TAP: 14117a0a9608SKusanagi Kouichi dev->netdev_ops = &tap_netdev_ops; 14121da177e4SLinus Torvalds /* Ethernet TAP Device */ 14131da177e4SLinus Torvalds ether_setup(dev); 1414550fd08cSNeil Horman dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1415a676847bSstephen hemminger dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 141636226a8dSBrian Braunstein 1417f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 141836226a8dSBrian Braunstein 14191da177e4SLinus Torvalds break; 14201da177e4SLinus Torvalds } 142191572088SJarod Wilson 142291572088SJarod Wilson dev->min_mtu = MIN_MTU; 142391572088SJarod Wilson dev->max_mtu = MAX_MTU - dev->hard_header_len; 14241da177e4SLinus Torvalds } 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds /* Character device part */ 14271da177e4SLinus Torvalds 14281da177e4SLinus Torvalds /* Poll */ 1429afc9a42bSAl Viro static __poll_t tun_chr_poll(struct file *file, poll_table *wait) 14301da177e4SLinus Torvalds { 1431b2430de3SEric W. Biederman struct tun_file *tfile = file->private_data; 14329484dc74Syuan linyu struct tun_struct *tun = tun_get(tfile); 14333c8a9c63SMariusz Kozlowski struct sock *sk; 1434afc9a42bSAl Viro __poll_t mask = 0; 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds if (!tun) 1437a9a08845SLinus Torvalds return EPOLLERR; 14381da177e4SLinus Torvalds 143954f968d6SJason Wang sk = tfile->socket.sk; 14403c8a9c63SMariusz Kozlowski 14416b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_chr_poll\n"); 14421da177e4SLinus Torvalds 14439e641bdcSXi Wang poll_wait(file, sk_sleep(sk), wait); 14441da177e4SLinus Torvalds 14455990a305SJason Wang if (!ptr_ring_empty(&tfile->tx_ring)) 1446a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM; 14471da177e4SLinus Torvalds 1448b20e2d54SHannes Frederic Sowa if (tun->dev->flags & IFF_UP && 1449b20e2d54SHannes Frederic Sowa (sock_writeable(sk) || 14509cd3e072SEric Dumazet (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) && 1451b20e2d54SHannes Frederic Sowa sock_writeable(sk)))) 1452a9a08845SLinus Torvalds mask |= EPOLLOUT | EPOLLWRNORM; 145333dccbb0SHerbert Xu 1454c70f1829SEric W. Biederman if (tun->dev->reg_state != NETREG_REGISTERED) 1455a9a08845SLinus Torvalds mask = EPOLLERR; 1456c70f1829SEric W. Biederman 1457631ab46bSEric W. Biederman tun_put(tun); 14581da177e4SLinus Torvalds return mask; 14591da177e4SLinus Torvalds } 14601da177e4SLinus Torvalds 146190e33d45SPetar Penkov static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile, 146290e33d45SPetar Penkov size_t len, 146390e33d45SPetar Penkov const struct iov_iter *it) 146490e33d45SPetar Penkov { 146590e33d45SPetar Penkov struct sk_buff *skb; 146690e33d45SPetar Penkov size_t linear; 146790e33d45SPetar Penkov int err; 146890e33d45SPetar Penkov int i; 146990e33d45SPetar Penkov 147090e33d45SPetar Penkov if (it->nr_segs > MAX_SKB_FRAGS + 1) 147190e33d45SPetar Penkov return ERR_PTR(-ENOMEM); 147290e33d45SPetar Penkov 147390e33d45SPetar Penkov local_bh_disable(); 147490e33d45SPetar Penkov skb = napi_get_frags(&tfile->napi); 147590e33d45SPetar Penkov local_bh_enable(); 147690e33d45SPetar Penkov if (!skb) 147790e33d45SPetar Penkov return ERR_PTR(-ENOMEM); 147890e33d45SPetar Penkov 147990e33d45SPetar Penkov linear = iov_iter_single_seg_count(it); 148090e33d45SPetar Penkov err = __skb_grow(skb, linear); 148190e33d45SPetar Penkov if (err) 148290e33d45SPetar Penkov goto free; 148390e33d45SPetar Penkov 148490e33d45SPetar Penkov skb->len = len; 148590e33d45SPetar Penkov skb->data_len = len - linear; 148690e33d45SPetar Penkov skb->truesize += skb->data_len; 148790e33d45SPetar Penkov 148890e33d45SPetar Penkov for (i = 1; i < it->nr_segs; i++) { 148943a08e0fSEric Dumazet struct page_frag *pfrag = ¤t->task_frag; 149090e33d45SPetar Penkov size_t fragsz = it->iov[i].iov_len; 149190e33d45SPetar Penkov 149290e33d45SPetar Penkov if (fragsz == 0 || fragsz > PAGE_SIZE) { 149390e33d45SPetar Penkov err = -EINVAL; 149490e33d45SPetar Penkov goto free; 149590e33d45SPetar Penkov } 149690e33d45SPetar Penkov 149743a08e0fSEric Dumazet if (!skb_page_frag_refill(fragsz, pfrag, GFP_KERNEL)) { 149890e33d45SPetar Penkov err = -ENOMEM; 149990e33d45SPetar Penkov goto free; 150090e33d45SPetar Penkov } 150190e33d45SPetar Penkov 150243a08e0fSEric Dumazet skb_fill_page_desc(skb, i - 1, pfrag->page, 150343a08e0fSEric Dumazet pfrag->offset, fragsz); 150443a08e0fSEric Dumazet page_ref_inc(pfrag->page); 150543a08e0fSEric Dumazet pfrag->offset += fragsz; 150690e33d45SPetar Penkov } 150790e33d45SPetar Penkov 150890e33d45SPetar Penkov return skb; 150990e33d45SPetar Penkov free: 151090e33d45SPetar Penkov /* frees skb and all frags allocated with napi_alloc_frag() */ 151190e33d45SPetar Penkov napi_free_frags(&tfile->napi); 151290e33d45SPetar Penkov return ERR_PTR(err); 151390e33d45SPetar Penkov } 151490e33d45SPetar Penkov 1515f42157cbSRusty Russell /* prepad is the amount to reserve at front. len is length after that. 1516f42157cbSRusty Russell * linear is a hint as to how much to copy (usually headers). */ 151754f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, 151833dccbb0SHerbert Xu size_t prepad, size_t len, 151933dccbb0SHerbert Xu size_t linear, int noblock) 1520f42157cbSRusty Russell { 152154f968d6SJason Wang struct sock *sk = tfile->socket.sk; 1522f42157cbSRusty Russell struct sk_buff *skb; 152333dccbb0SHerbert Xu int err; 1524f42157cbSRusty Russell 1525f42157cbSRusty Russell /* Under a page? Don't bother with paged skb. */ 15260eca93bcSHerbert Xu if (prepad + len < PAGE_SIZE || !linear) 152733dccbb0SHerbert Xu linear = len; 1528f42157cbSRusty Russell 152933dccbb0SHerbert Xu skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, 153028d64271SEric Dumazet &err, 0); 1531f42157cbSRusty Russell if (!skb) 153233dccbb0SHerbert Xu return ERR_PTR(err); 1533f42157cbSRusty Russell 1534f42157cbSRusty Russell skb_reserve(skb, prepad); 1535f42157cbSRusty Russell skb_put(skb, linear); 153633dccbb0SHerbert Xu skb->data_len = len - linear; 153733dccbb0SHerbert Xu skb->len += len - linear; 1538f42157cbSRusty Russell 1539f42157cbSRusty Russell return skb; 1540f42157cbSRusty Russell } 1541f42157cbSRusty Russell 15425503fcecSJason Wang static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile, 15435503fcecSJason Wang struct sk_buff *skb, int more) 15445503fcecSJason Wang { 15455503fcecSJason Wang struct sk_buff_head *queue = &tfile->sk.sk_write_queue; 15465503fcecSJason Wang struct sk_buff_head process_queue; 15475503fcecSJason Wang u32 rx_batched = tun->rx_batched; 15485503fcecSJason Wang bool rcv = false; 15495503fcecSJason Wang 15505503fcecSJason Wang if (!rx_batched || (!more && skb_queue_empty(queue))) { 15515503fcecSJason Wang local_bh_disable(); 15525503fcecSJason Wang netif_receive_skb(skb); 15535503fcecSJason Wang local_bh_enable(); 15545503fcecSJason Wang return; 15555503fcecSJason Wang } 15565503fcecSJason Wang 15575503fcecSJason Wang spin_lock(&queue->lock); 15585503fcecSJason Wang if (!more || skb_queue_len(queue) == rx_batched) { 15595503fcecSJason Wang __skb_queue_head_init(&process_queue); 15605503fcecSJason Wang skb_queue_splice_tail_init(queue, &process_queue); 15615503fcecSJason Wang rcv = true; 15625503fcecSJason Wang } else { 15635503fcecSJason Wang __skb_queue_tail(queue, skb); 15645503fcecSJason Wang } 15655503fcecSJason Wang spin_unlock(&queue->lock); 15665503fcecSJason Wang 15675503fcecSJason Wang if (rcv) { 15685503fcecSJason Wang struct sk_buff *nskb; 15695503fcecSJason Wang 15705503fcecSJason Wang local_bh_disable(); 15715503fcecSJason Wang while ((nskb = __skb_dequeue(&process_queue))) 15725503fcecSJason Wang netif_receive_skb(nskb); 15735503fcecSJason Wang netif_receive_skb(skb); 15745503fcecSJason Wang local_bh_enable(); 15755503fcecSJason Wang } 15765503fcecSJason Wang } 15775503fcecSJason Wang 157866ccbc9cSJason Wang static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile, 157966ccbc9cSJason Wang int len, int noblock, bool zerocopy) 158066ccbc9cSJason Wang { 158166ccbc9cSJason Wang if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 158266ccbc9cSJason Wang return false; 158366ccbc9cSJason Wang 158466ccbc9cSJason Wang if (tfile->socket.sk->sk_sndbuf != INT_MAX) 158566ccbc9cSJason Wang return false; 158666ccbc9cSJason Wang 158766ccbc9cSJason Wang if (!noblock) 158866ccbc9cSJason Wang return false; 158966ccbc9cSJason Wang 159066ccbc9cSJason Wang if (zerocopy) 159166ccbc9cSJason Wang return false; 159266ccbc9cSJason Wang 159366ccbc9cSJason Wang if (SKB_DATA_ALIGN(len + TUN_RX_PAD) + 159466ccbc9cSJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE) 159566ccbc9cSJason Wang return false; 159666ccbc9cSJason Wang 159766ccbc9cSJason Wang return true; 159866ccbc9cSJason Wang } 159966ccbc9cSJason Wang 1600761876c8SJason Wang static struct sk_buff *tun_build_skb(struct tun_struct *tun, 1601761876c8SJason Wang struct tun_file *tfile, 160266ccbc9cSJason Wang struct iov_iter *from, 1603761876c8SJason Wang struct virtio_net_hdr *hdr, 16041cfe6e93SJason Wang int len, int *skb_xdp) 160566ccbc9cSJason Wang { 16060bbd7dadSEric Dumazet struct page_frag *alloc_frag = ¤t->task_frag; 160766ccbc9cSJason Wang struct sk_buff *skb; 1608761876c8SJason Wang struct bpf_prog *xdp_prog; 16097df13219SJason Wang int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1610761876c8SJason Wang unsigned int delta = 0; 161166ccbc9cSJason Wang char *buf; 161266ccbc9cSJason Wang size_t copied; 16137df13219SJason Wang int err, pad = TUN_RX_PAD; 16147df13219SJason Wang 16157df13219SJason Wang rcu_read_lock(); 16167df13219SJason Wang xdp_prog = rcu_dereference(tun->xdp_prog); 16177df13219SJason Wang if (xdp_prog) 16187df13219SJason Wang pad += TUN_HEADROOM; 16197df13219SJason Wang buflen += SKB_DATA_ALIGN(len + pad); 16207df13219SJason Wang rcu_read_unlock(); 162166ccbc9cSJason Wang 162263b9ab65SJason Wang alloc_frag->offset = ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES); 162366ccbc9cSJason Wang if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL))) 162466ccbc9cSJason Wang return ERR_PTR(-ENOMEM); 162566ccbc9cSJason Wang 162666ccbc9cSJason Wang buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 162766ccbc9cSJason Wang copied = copy_page_from_iter(alloc_frag->page, 16287df13219SJason Wang alloc_frag->offset + pad, 162966ccbc9cSJason Wang len, from); 163066ccbc9cSJason Wang if (copied != len) 163166ccbc9cSJason Wang return ERR_PTR(-EFAULT); 163266ccbc9cSJason Wang 16337df13219SJason Wang /* There's a small window that XDP may be set after the check 16347df13219SJason Wang * of xdp_prog above, this should be rare and for simplicity 16357df13219SJason Wang * we do XDP on skb in case the headroom is not enough. 16367df13219SJason Wang */ 16377df13219SJason Wang if (hdr->gso_type || !xdp_prog) 16381cfe6e93SJason Wang *skb_xdp = 1; 1639761876c8SJason Wang else 16401cfe6e93SJason Wang *skb_xdp = 0; 164166ccbc9cSJason Wang 164223e43f07SJason Wang preempt_disable(); 1643761876c8SJason Wang rcu_read_lock(); 1644761876c8SJason Wang xdp_prog = rcu_dereference(tun->xdp_prog); 16451cfe6e93SJason Wang if (xdp_prog && !*skb_xdp) { 1646761876c8SJason Wang struct xdp_buff xdp; 1647761876c8SJason Wang void *orig_data; 1648761876c8SJason Wang u32 act; 1649761876c8SJason Wang 1650761876c8SJason Wang xdp.data_hard_start = buf; 16517df13219SJason Wang xdp.data = buf + pad; 1652de8f3a83SDaniel Borkmann xdp_set_data_meta_invalid(&xdp); 1653761876c8SJason Wang xdp.data_end = xdp.data + len; 16548bf5c4eeSJesper Dangaard Brouer xdp.rxq = &tfile->xdp_rxq; 1655761876c8SJason Wang orig_data = xdp.data; 1656761876c8SJason Wang act = bpf_prog_run_xdp(xdp_prog, &xdp); 1657761876c8SJason Wang 1658761876c8SJason Wang switch (act) { 1659761876c8SJason Wang case XDP_REDIRECT: 1660761876c8SJason Wang get_page(alloc_frag->page); 1661761876c8SJason Wang alloc_frag->offset += buflen; 1662761876c8SJason Wang err = xdp_do_redirect(tun->dev, &xdp, xdp_prog); 16631bb4f2e8SJason Wang xdp_do_flush_map(); 1664761876c8SJason Wang if (err) 1665761876c8SJason Wang goto err_redirect; 1666654d5738SXin Long rcu_read_unlock(); 166723e43f07SJason Wang preempt_enable(); 1668761876c8SJason Wang return NULL; 1669761876c8SJason Wang case XDP_TX: 167059655a5bSJason Wang get_page(alloc_frag->page); 167159655a5bSJason Wang alloc_frag->offset += buflen; 167259655a5bSJason Wang if (tun_xdp_xmit(tun->dev, &xdp)) 167359655a5bSJason Wang goto err_redirect; 167459655a5bSJason Wang tun_xdp_flush(tun->dev); 167559655a5bSJason Wang rcu_read_unlock(); 167659655a5bSJason Wang preempt_enable(); 167759655a5bSJason Wang return NULL; 1678761876c8SJason Wang case XDP_PASS: 1679761876c8SJason Wang delta = orig_data - xdp.data; 1680761876c8SJason Wang break; 1681761876c8SJason Wang default: 1682761876c8SJason Wang bpf_warn_invalid_xdp_action(act); 1683761876c8SJason Wang /* fall through */ 1684761876c8SJason Wang case XDP_ABORTED: 1685761876c8SJason Wang trace_xdp_exception(tun->dev, xdp_prog, act); 1686761876c8SJason Wang /* fall through */ 1687761876c8SJason Wang case XDP_DROP: 1688761876c8SJason Wang goto err_xdp; 1689761876c8SJason Wang } 1690761876c8SJason Wang } 1691761876c8SJason Wang 1692761876c8SJason Wang skb = build_skb(buf, buflen); 1693761876c8SJason Wang if (!skb) { 1694761876c8SJason Wang rcu_read_unlock(); 169523e43f07SJason Wang preempt_enable(); 1696761876c8SJason Wang return ERR_PTR(-ENOMEM); 1697761876c8SJason Wang } 1698761876c8SJason Wang 16997df13219SJason Wang skb_reserve(skb, pad - delta); 1700761876c8SJason Wang skb_put(skb, len + delta); 170166ccbc9cSJason Wang get_page(alloc_frag->page); 170266ccbc9cSJason Wang alloc_frag->offset += buflen; 170366ccbc9cSJason Wang 1704761876c8SJason Wang rcu_read_unlock(); 170523e43f07SJason Wang preempt_enable(); 1706761876c8SJason Wang 170766ccbc9cSJason Wang return skb; 1708761876c8SJason Wang 1709761876c8SJason Wang err_redirect: 1710761876c8SJason Wang put_page(alloc_frag->page); 1711761876c8SJason Wang err_xdp: 1712761876c8SJason Wang rcu_read_unlock(); 171323e43f07SJason Wang preempt_enable(); 1714761876c8SJason Wang this_cpu_inc(tun->pcpu_stats->rx_dropped); 1715761876c8SJason Wang return NULL; 171666ccbc9cSJason Wang } 171766ccbc9cSJason Wang 17181da177e4SLinus Torvalds /* Get packet from user space buffer */ 171954f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, 1720f5ff53b4SAl Viro void *msg_control, struct iov_iter *from, 17215503fcecSJason Wang int noblock, bool more) 17221da177e4SLinus Torvalds { 172309640e63SHarvey Harrison struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; 17241da177e4SLinus Torvalds struct sk_buff *skb; 1725f5ff53b4SAl Viro size_t total_len = iov_iter_count(from); 1726eaea34b2SPaolo Abeni size_t len = total_len, align = tun->align, linear; 1727f43798c2SRusty Russell struct virtio_net_hdr gso = { 0 }; 1728608b9977SPaolo Abeni struct tun_pcpu_stats *stats; 172996f8d9ecSJason Wang int good_linear; 17300690899bSMichael S. Tsirkin int copylen; 17310690899bSMichael S. Tsirkin bool zerocopy = false; 17320690899bSMichael S. Tsirkin int err; 173396f84061SJason Wang u32 rxhash = 0; 17341cfe6e93SJason Wang int skb_xdp = 1; 173590e33d45SPetar Penkov bool frags = tun_napi_frags_enabled(tun); 17361da177e4SLinus Torvalds 17371bd4978aSEric Dumazet if (!(tun->dev->flags & IFF_UP)) 17381bd4978aSEric Dumazet return -EIO; 17391bd4978aSEric Dumazet 174040630b82SMichael S. Tsirkin if (!(tun->flags & IFF_NO_PI)) { 174115718ea0SDan Carpenter if (len < sizeof(pi)) 17421da177e4SLinus Torvalds return -EINVAL; 174315718ea0SDan Carpenter len -= sizeof(pi); 17441da177e4SLinus Torvalds 1745cbbd26b8SAl Viro if (!copy_from_iter_full(&pi, sizeof(pi), from)) 17461da177e4SLinus Torvalds return -EFAULT; 17471da177e4SLinus Torvalds } 17481da177e4SLinus Torvalds 174940630b82SMichael S. Tsirkin if (tun->flags & IFF_VNET_HDR) { 1750e1edab87SWillem de Bruijn int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); 1751e1edab87SWillem de Bruijn 1752e1edab87SWillem de Bruijn if (len < vnet_hdr_sz) 1753f43798c2SRusty Russell return -EINVAL; 1754e1edab87SWillem de Bruijn len -= vnet_hdr_sz; 1755f43798c2SRusty Russell 1756cbbd26b8SAl Viro if (!copy_from_iter_full(&gso, sizeof(gso), from)) 1757f43798c2SRusty Russell return -EFAULT; 1758f43798c2SRusty Russell 17594909122fSHerbert Xu if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && 176056f0dcc5SMichael S. Tsirkin tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len)) 176156f0dcc5SMichael S. Tsirkin gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2); 17624909122fSHerbert Xu 176356f0dcc5SMichael S. Tsirkin if (tun16_to_cpu(tun, gso.hdr_len) > len) 1764f43798c2SRusty Russell return -EINVAL; 1765e1edab87SWillem de Bruijn iov_iter_advance(from, vnet_hdr_sz - sizeof(gso)); 1766f43798c2SRusty Russell } 1767f43798c2SRusty Russell 176840630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) { 1769a504b86eSstephen hemminger align += NET_IP_ALIGN; 17700eca93bcSHerbert Xu if (unlikely(len < ETH_HLEN || 177156f0dcc5SMichael S. Tsirkin (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN))) 1772e01bf1c8SRusty Russell return -EINVAL; 1773e01bf1c8SRusty Russell } 17741da177e4SLinus Torvalds 177596f8d9ecSJason Wang good_linear = SKB_MAX_HEAD(align); 177696f8d9ecSJason Wang 177788529176SJason Wang if (msg_control) { 1778f5ff53b4SAl Viro struct iov_iter i = *from; 1779f5ff53b4SAl Viro 178088529176SJason Wang /* There are 256 bytes to be copied in skb, so there is 178188529176SJason Wang * enough room for skb expand head in case it is used. 17820690899bSMichael S. Tsirkin * The rest of the buffer is mapped from userspace. 17830690899bSMichael S. Tsirkin */ 178456f0dcc5SMichael S. Tsirkin copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN; 178596f8d9ecSJason Wang if (copylen > good_linear) 178696f8d9ecSJason Wang copylen = good_linear; 17873dd5c330SJason Wang linear = copylen; 1788f5ff53b4SAl Viro iov_iter_advance(&i, copylen); 1789f5ff53b4SAl Viro if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS) 179088529176SJason Wang zerocopy = true; 179188529176SJason Wang } 179288529176SJason Wang 179390e33d45SPetar Penkov if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) { 17941cfe6e93SJason Wang /* For the packet that is not easy to be processed 17951cfe6e93SJason Wang * (e.g gso or jumbo packet), we will do it at after 17961cfe6e93SJason Wang * skb was created with generic XDP routine. 17971cfe6e93SJason Wang */ 17981cfe6e93SJason Wang skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp); 179966ccbc9cSJason Wang if (IS_ERR(skb)) { 180066ccbc9cSJason Wang this_cpu_inc(tun->pcpu_stats->rx_dropped); 180166ccbc9cSJason Wang return PTR_ERR(skb); 180266ccbc9cSJason Wang } 1803761876c8SJason Wang if (!skb) 1804761876c8SJason Wang return total_len; 180566ccbc9cSJason Wang } else { 180688529176SJason Wang if (!zerocopy) { 18070690899bSMichael S. Tsirkin copylen = len; 180856f0dcc5SMichael S. Tsirkin if (tun16_to_cpu(tun, gso.hdr_len) > good_linear) 180996f8d9ecSJason Wang linear = good_linear; 181096f8d9ecSJason Wang else 181156f0dcc5SMichael S. Tsirkin linear = tun16_to_cpu(tun, gso.hdr_len); 18123dd5c330SJason Wang } 18130690899bSMichael S. Tsirkin 181490e33d45SPetar Penkov if (frags) { 181590e33d45SPetar Penkov mutex_lock(&tfile->napi_mutex); 181690e33d45SPetar Penkov skb = tun_napi_alloc_frags(tfile, copylen, from); 181790e33d45SPetar Penkov /* tun_napi_alloc_frags() enforces a layout for the skb. 181890e33d45SPetar Penkov * If zerocopy is enabled, then this layout will be 181990e33d45SPetar Penkov * overwritten by zerocopy_sg_from_iter(). 182090e33d45SPetar Penkov */ 182190e33d45SPetar Penkov zerocopy = false; 182290e33d45SPetar Penkov } else { 182390e33d45SPetar Penkov skb = tun_alloc_skb(tfile, align, copylen, linear, 182490e33d45SPetar Penkov noblock); 182590e33d45SPetar Penkov } 182690e33d45SPetar Penkov 182733dccbb0SHerbert Xu if (IS_ERR(skb)) { 182833dccbb0SHerbert Xu if (PTR_ERR(skb) != -EAGAIN) 1829608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 183090e33d45SPetar Penkov if (frags) 183190e33d45SPetar Penkov mutex_unlock(&tfile->napi_mutex); 183233dccbb0SHerbert Xu return PTR_ERR(skb); 18331da177e4SLinus Torvalds } 18341da177e4SLinus Torvalds 18350690899bSMichael S. Tsirkin if (zerocopy) 1836f5ff53b4SAl Viro err = zerocopy_sg_from_iter(skb, from); 1837af1cc7a2SJason Wang else 1838f5ff53b4SAl Viro err = skb_copy_datagram_from_iter(skb, 0, from, len); 18390690899bSMichael S. Tsirkin 18400690899bSMichael S. Tsirkin if (err) { 1841608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 18428f22757eSDave Jones kfree_skb(skb); 184390e33d45SPetar Penkov if (frags) { 184490e33d45SPetar Penkov tfile->napi.skb = NULL; 184590e33d45SPetar Penkov mutex_unlock(&tfile->napi_mutex); 184690e33d45SPetar Penkov } 184790e33d45SPetar Penkov 18481da177e4SLinus Torvalds return -EFAULT; 18498f22757eSDave Jones } 185066ccbc9cSJason Wang } 18511da177e4SLinus Torvalds 18523e9e40e7SJarno Rajahalme if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) { 1853df10db98SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_frame_errors); 1854df10db98SPaolo Abeni kfree_skb(skb); 185590e33d45SPetar Penkov if (frags) { 185690e33d45SPetar Penkov tfile->napi.skb = NULL; 185790e33d45SPetar Penkov mutex_unlock(&tfile->napi_mutex); 185890e33d45SPetar Penkov } 185990e33d45SPetar Penkov 1860df10db98SPaolo Abeni return -EINVAL; 1861df10db98SPaolo Abeni } 1862df10db98SPaolo Abeni 18631da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 186440630b82SMichael S. Tsirkin case IFF_TUN: 186540630b82SMichael S. Tsirkin if (tun->flags & IFF_NO_PI) { 18662580c4c1SAlexander Potapenko u8 ip_version = skb->len ? (skb->data[0] >> 4) : 0; 18672580c4c1SAlexander Potapenko 18682580c4c1SAlexander Potapenko switch (ip_version) { 18692580c4c1SAlexander Potapenko case 4: 1870f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IP); 1871f09f7ee2SAng Way Chuang break; 18722580c4c1SAlexander Potapenko case 6: 1873f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IPV6); 1874f09f7ee2SAng Way Chuang break; 1875f09f7ee2SAng Way Chuang default: 1876608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 1877f09f7ee2SAng Way Chuang kfree_skb(skb); 1878f09f7ee2SAng Way Chuang return -EINVAL; 1879f09f7ee2SAng Way Chuang } 1880f09f7ee2SAng Way Chuang } 1881f09f7ee2SAng Way Chuang 1882459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 18831da177e4SLinus Torvalds skb->protocol = pi.proto; 18844c13eb66SArnaldo Carvalho de Melo skb->dev = tun->dev; 18851da177e4SLinus Torvalds break; 188640630b82SMichael S. Tsirkin case IFF_TAP: 188790e33d45SPetar Penkov if (!frags) 18881da177e4SLinus Torvalds skb->protocol = eth_type_trans(skb, tun->dev); 18891da177e4SLinus Torvalds break; 18906403eab1SJoe Perches } 18911da177e4SLinus Torvalds 18920690899bSMichael S. Tsirkin /* copy skb_ubuf_info for callback when skb has no error */ 18930690899bSMichael S. Tsirkin if (zerocopy) { 18940690899bSMichael S. Tsirkin skb_shinfo(skb)->destructor_arg = msg_control; 18950690899bSMichael S. Tsirkin skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; 1896c9af6db4SPravin B Shelar skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; 1897af1cc7a2SJason Wang } else if (msg_control) { 1898af1cc7a2SJason Wang struct ubuf_info *uarg = msg_control; 1899af1cc7a2SJason Wang uarg->callback(uarg, false); 19000690899bSMichael S. Tsirkin } 19010690899bSMichael S. Tsirkin 190272f65107SVlad Yasevich skb_reset_network_header(skb); 190340893fd0SJason Wang skb_probe_transport_header(skb, 0); 190438502af7SJason Wang 19051cfe6e93SJason Wang if (skb_xdp) { 1906761876c8SJason Wang struct bpf_prog *xdp_prog; 1907761876c8SJason Wang int ret; 1908761876c8SJason Wang 1909761876c8SJason Wang rcu_read_lock(); 1910761876c8SJason Wang xdp_prog = rcu_dereference(tun->xdp_prog); 1911761876c8SJason Wang if (xdp_prog) { 1912761876c8SJason Wang ret = do_xdp_generic(xdp_prog, skb); 1913761876c8SJason Wang if (ret != XDP_PASS) { 1914761876c8SJason Wang rcu_read_unlock(); 1915761876c8SJason Wang return total_len; 1916761876c8SJason Wang } 1917761876c8SJason Wang } 1918761876c8SJason Wang rcu_read_unlock(); 1919761876c8SJason Wang } 1920761876c8SJason Wang 192196f84061SJason Wang rcu_read_lock(); 192296f84061SJason Wang if (!rcu_dereference(tun->steering_prog)) 1923feec084aSJason Wang rxhash = __skb_get_hash_symmetric(skb); 192496f84061SJason Wang rcu_read_unlock(); 192594317099SPetar Penkov 192690e33d45SPetar Penkov if (frags) { 192790e33d45SPetar Penkov /* Exercise flow dissector code path. */ 192890e33d45SPetar Penkov u32 headlen = eth_get_headlen(skb->data, skb_headlen(skb)); 192990e33d45SPetar Penkov 1930010f245bSEric Dumazet if (unlikely(headlen > skb_headlen(skb))) { 193190e33d45SPetar Penkov this_cpu_inc(tun->pcpu_stats->rx_dropped); 193290e33d45SPetar Penkov napi_free_frags(&tfile->napi); 193390e33d45SPetar Penkov mutex_unlock(&tfile->napi_mutex); 193490e33d45SPetar Penkov WARN_ON(1); 193590e33d45SPetar Penkov return -ENOMEM; 193690e33d45SPetar Penkov } 193790e33d45SPetar Penkov 193890e33d45SPetar Penkov local_bh_disable(); 193990e33d45SPetar Penkov napi_gro_frags(&tfile->napi); 194090e33d45SPetar Penkov local_bh_enable(); 194190e33d45SPetar Penkov mutex_unlock(&tfile->napi_mutex); 1942aec72f33SEric Dumazet } else if (tfile->napi_enabled) { 194394317099SPetar Penkov struct sk_buff_head *queue = &tfile->sk.sk_write_queue; 194494317099SPetar Penkov int queue_len; 194594317099SPetar Penkov 194694317099SPetar Penkov spin_lock_bh(&queue->lock); 194794317099SPetar Penkov __skb_queue_tail(queue, skb); 194894317099SPetar Penkov queue_len = skb_queue_len(queue); 194994317099SPetar Penkov spin_unlock(&queue->lock); 195094317099SPetar Penkov 195194317099SPetar Penkov if (!more || queue_len > NAPI_POLL_WEIGHT) 195294317099SPetar Penkov napi_schedule(&tfile->napi); 195394317099SPetar Penkov 195494317099SPetar Penkov local_bh_enable(); 195594317099SPetar Penkov } else if (!IS_ENABLED(CONFIG_4KSTACKS)) { 19565503fcecSJason Wang tun_rx_batched(tun, tfile, skb, more); 195794317099SPetar Penkov } else { 19581da177e4SLinus Torvalds netif_rx_ni(skb); 195994317099SPetar Penkov } 19601da177e4SLinus Torvalds 1961608b9977SPaolo Abeni stats = get_cpu_ptr(tun->pcpu_stats); 1962608b9977SPaolo Abeni u64_stats_update_begin(&stats->syncp); 1963608b9977SPaolo Abeni stats->rx_packets++; 1964608b9977SPaolo Abeni stats->rx_bytes += len; 1965608b9977SPaolo Abeni u64_stats_update_end(&stats->syncp); 1966608b9977SPaolo Abeni put_cpu_ptr(stats); 19671da177e4SLinus Torvalds 196896f84061SJason Wang if (rxhash) 19699e85722dSJason Wang tun_flow_update(tun, rxhash, tfile); 197096f84061SJason Wang 19710690899bSMichael S. Tsirkin return total_len; 19721da177e4SLinus Torvalds } 19731da177e4SLinus Torvalds 1974f5ff53b4SAl Viro static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from) 19751da177e4SLinus Torvalds { 197633dccbb0SHerbert Xu struct file *file = iocb->ki_filp; 197754f968d6SJason Wang struct tun_file *tfile = file->private_data; 19789484dc74Syuan linyu struct tun_struct *tun = tun_get(tfile); 1979631ab46bSEric W. Biederman ssize_t result; 19801da177e4SLinus Torvalds 19811da177e4SLinus Torvalds if (!tun) 19821da177e4SLinus Torvalds return -EBADFD; 19831da177e4SLinus Torvalds 19845503fcecSJason Wang result = tun_get_user(tun, tfile, NULL, from, 19855503fcecSJason Wang file->f_flags & O_NONBLOCK, false); 1986631ab46bSEric W. Biederman 1987631ab46bSEric W. Biederman tun_put(tun); 1988631ab46bSEric W. Biederman return result; 19891da177e4SLinus Torvalds } 19901da177e4SLinus Torvalds 1991fc72d1d5SJason Wang static ssize_t tun_put_user_xdp(struct tun_struct *tun, 1992fc72d1d5SJason Wang struct tun_file *tfile, 1993fc72d1d5SJason Wang struct xdp_buff *xdp, 1994fc72d1d5SJason Wang struct iov_iter *iter) 1995fc72d1d5SJason Wang { 1996fc72d1d5SJason Wang int vnet_hdr_sz = 0; 1997fc72d1d5SJason Wang size_t size = xdp->data_end - xdp->data; 1998fc72d1d5SJason Wang struct tun_pcpu_stats *stats; 1999fc72d1d5SJason Wang size_t ret; 2000fc72d1d5SJason Wang 2001fc72d1d5SJason Wang if (tun->flags & IFF_VNET_HDR) { 2002fc72d1d5SJason Wang struct virtio_net_hdr gso = { 0 }; 2003fc72d1d5SJason Wang 2004fc72d1d5SJason Wang vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); 2005fc72d1d5SJason Wang if (unlikely(iov_iter_count(iter) < vnet_hdr_sz)) 2006fc72d1d5SJason Wang return -EINVAL; 2007fc72d1d5SJason Wang if (unlikely(copy_to_iter(&gso, sizeof(gso), iter) != 2008fc72d1d5SJason Wang sizeof(gso))) 2009fc72d1d5SJason Wang return -EFAULT; 2010fc72d1d5SJason Wang iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso)); 2011fc72d1d5SJason Wang } 2012fc72d1d5SJason Wang 2013fc72d1d5SJason Wang ret = copy_to_iter(xdp->data, size, iter) + vnet_hdr_sz; 2014fc72d1d5SJason Wang 2015fc72d1d5SJason Wang stats = get_cpu_ptr(tun->pcpu_stats); 2016fc72d1d5SJason Wang u64_stats_update_begin(&stats->syncp); 2017fc72d1d5SJason Wang stats->tx_packets++; 2018fc72d1d5SJason Wang stats->tx_bytes += ret; 2019fc72d1d5SJason Wang u64_stats_update_end(&stats->syncp); 2020fc72d1d5SJason Wang put_cpu_ptr(tun->pcpu_stats); 2021fc72d1d5SJason Wang 2022fc72d1d5SJason Wang return ret; 2023fc72d1d5SJason Wang } 2024fc72d1d5SJason Wang 20251da177e4SLinus Torvalds /* Put packet to the user space buffer */ 20266f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun, 202754f968d6SJason Wang struct tun_file *tfile, 20281da177e4SLinus Torvalds struct sk_buff *skb, 2029e0b46d0eSHerbert Xu struct iov_iter *iter) 20301da177e4SLinus Torvalds { 20311da177e4SLinus Torvalds struct tun_pi pi = { 0, skb->protocol }; 2032608b9977SPaolo Abeni struct tun_pcpu_stats *stats; 2033e0b46d0eSHerbert Xu ssize_t total; 20348c847d25SJason Wang int vlan_offset = 0; 2035a8f9bfdfSHerbert Xu int vlan_hlen = 0; 20362eb783c4SHerbert Xu int vnet_hdr_sz = 0; 2037a8f9bfdfSHerbert Xu 2038df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) 2039a8f9bfdfSHerbert Xu vlan_hlen = VLAN_HLEN; 20401da177e4SLinus Torvalds 204140630b82SMichael S. Tsirkin if (tun->flags & IFF_VNET_HDR) 2042e1edab87SWillem de Bruijn vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); 20431da177e4SLinus Torvalds 2044e0b46d0eSHerbert Xu total = skb->len + vlan_hlen + vnet_hdr_sz; 2045e0b46d0eSHerbert Xu 204640630b82SMichael S. Tsirkin if (!(tun->flags & IFF_NO_PI)) { 2047e0b46d0eSHerbert Xu if (iov_iter_count(iter) < sizeof(pi)) 20481da177e4SLinus Torvalds return -EINVAL; 20491da177e4SLinus Torvalds 2050e0b46d0eSHerbert Xu total += sizeof(pi); 2051e0b46d0eSHerbert Xu if (iov_iter_count(iter) < total) { 20521da177e4SLinus Torvalds /* Packet will be striped */ 20531da177e4SLinus Torvalds pi.flags |= TUN_PKT_STRIP; 20541da177e4SLinus Torvalds } 20551da177e4SLinus Torvalds 2056e0b46d0eSHerbert Xu if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi)) 20571da177e4SLinus Torvalds return -EFAULT; 20581da177e4SLinus Torvalds } 20591da177e4SLinus Torvalds 20602eb783c4SHerbert Xu if (vnet_hdr_sz) { 20619403cd7cSJarno Rajahalme struct virtio_net_hdr gso; 206234166093SMike Rapoport 2063e0b46d0eSHerbert Xu if (iov_iter_count(iter) < vnet_hdr_sz) 2064f43798c2SRusty Russell return -EINVAL; 2065f43798c2SRusty Russell 20663e9e40e7SJarno Rajahalme if (virtio_net_hdr_from_skb(skb, &gso, 20676391a448SJason Wang tun_is_little_endian(tun), true)) { 2068f43798c2SRusty Russell struct skb_shared_info *sinfo = skb_shinfo(skb); 20696b8a66eeSJoe Perches pr_err("unexpected GSO type: " 2070ef3db4a5SMichael S. Tsirkin "0x%x, gso_size %d, hdr_len %d\n", 207156f0dcc5SMichael S. Tsirkin sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size), 207256f0dcc5SMichael S. Tsirkin tun16_to_cpu(tun, gso.hdr_len)); 2073ef3db4a5SMichael S. Tsirkin print_hex_dump(KERN_ERR, "tun: ", 2074ef3db4a5SMichael S. Tsirkin DUMP_PREFIX_NONE, 2075ef3db4a5SMichael S. Tsirkin 16, 1, skb->head, 207656f0dcc5SMichael S. Tsirkin min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true); 2077ef3db4a5SMichael S. Tsirkin WARN_ON_ONCE(1); 2078ef3db4a5SMichael S. Tsirkin return -EINVAL; 2079ef3db4a5SMichael S. Tsirkin } 2080f43798c2SRusty Russell 2081e0b46d0eSHerbert Xu if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso)) 2082f43798c2SRusty Russell return -EFAULT; 20838c847d25SJason Wang 20848c847d25SJason Wang iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso)); 2085f43798c2SRusty Russell } 2086f43798c2SRusty Russell 2087a8f9bfdfSHerbert Xu if (vlan_hlen) { 2088e0b46d0eSHerbert Xu int ret; 2089aff3d70aSJason Wang struct veth veth; 20901da177e4SLinus Torvalds 20916680ec68SJason Wang veth.h_vlan_proto = skb->vlan_proto; 2092df8a39deSJiri Pirko veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb)); 20931da177e4SLinus Torvalds 20946680ec68SJason Wang vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); 20956680ec68SJason Wang 2096e0b46d0eSHerbert Xu ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset); 2097e0b46d0eSHerbert Xu if (ret || !iov_iter_count(iter)) 20986680ec68SJason Wang goto done; 20996680ec68SJason Wang 2100e0b46d0eSHerbert Xu ret = copy_to_iter(&veth, sizeof(veth), iter); 2101e0b46d0eSHerbert Xu if (ret != sizeof(veth) || !iov_iter_count(iter)) 21026680ec68SJason Wang goto done; 21036680ec68SJason Wang } 21046680ec68SJason Wang 2105e0b46d0eSHerbert Xu skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset); 21066680ec68SJason Wang 21076680ec68SJason Wang done: 2108608b9977SPaolo Abeni /* caller is in process context, */ 2109608b9977SPaolo Abeni stats = get_cpu_ptr(tun->pcpu_stats); 2110608b9977SPaolo Abeni u64_stats_update_begin(&stats->syncp); 2111608b9977SPaolo Abeni stats->tx_packets++; 2112608b9977SPaolo Abeni stats->tx_bytes += skb->len + vlan_hlen; 2113608b9977SPaolo Abeni u64_stats_update_end(&stats->syncp); 2114608b9977SPaolo Abeni put_cpu_ptr(tun->pcpu_stats); 21151da177e4SLinus Torvalds 21161da177e4SLinus Torvalds return total; 21171da177e4SLinus Torvalds } 21181da177e4SLinus Torvalds 2119fc72d1d5SJason Wang static void *tun_ring_recv(struct tun_file *tfile, int noblock, int *err) 21201576d986SJason Wang { 21211576d986SJason Wang DECLARE_WAITQUEUE(wait, current); 2122fc72d1d5SJason Wang void *ptr = NULL; 2123f48cc6b2SJason Wang int error = 0; 21241576d986SJason Wang 2125fc72d1d5SJason Wang ptr = ptr_ring_consume(&tfile->tx_ring); 2126fc72d1d5SJason Wang if (ptr) 21271576d986SJason Wang goto out; 21281576d986SJason Wang if (noblock) { 2129f48cc6b2SJason Wang error = -EAGAIN; 21301576d986SJason Wang goto out; 21311576d986SJason Wang } 21321576d986SJason Wang 21331576d986SJason Wang add_wait_queue(&tfile->wq.wait, &wait); 21341576d986SJason Wang current->state = TASK_INTERRUPTIBLE; 21351576d986SJason Wang 21361576d986SJason Wang while (1) { 2137fc72d1d5SJason Wang ptr = ptr_ring_consume(&tfile->tx_ring); 2138fc72d1d5SJason Wang if (ptr) 21391576d986SJason Wang break; 21401576d986SJason Wang if (signal_pending(current)) { 2141f48cc6b2SJason Wang error = -ERESTARTSYS; 21421576d986SJason Wang break; 21431576d986SJason Wang } 21441576d986SJason Wang if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) { 2145f48cc6b2SJason Wang error = -EFAULT; 21461576d986SJason Wang break; 21471576d986SJason Wang } 21481576d986SJason Wang 21491576d986SJason Wang schedule(); 21501576d986SJason Wang } 21511576d986SJason Wang 21521576d986SJason Wang current->state = TASK_RUNNING; 21531576d986SJason Wang remove_wait_queue(&tfile->wq.wait, &wait); 21541576d986SJason Wang 21551576d986SJason Wang out: 2156f48cc6b2SJason Wang *err = error; 2157fc72d1d5SJason Wang return ptr; 21581576d986SJason Wang } 21591576d986SJason Wang 216054f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, 21619b067034SAl Viro struct iov_iter *to, 2162fc72d1d5SJason Wang int noblock, void *ptr) 21631da177e4SLinus Torvalds { 21649b067034SAl Viro ssize_t ret; 21651576d986SJason Wang int err; 21661da177e4SLinus Torvalds 21673872baf6SRami Rosen tun_debug(KERN_INFO, tun, "tun_do_read\n"); 21681da177e4SLinus Torvalds 2169c33ee15bSWei Xu if (!iov_iter_count(to)) { 2170fc72d1d5SJason Wang tun_ptr_free(ptr); 21719b067034SAl Viro return 0; 2172c33ee15bSWei Xu } 21731da177e4SLinus Torvalds 2174fc72d1d5SJason Wang if (!ptr) { 21751576d986SJason Wang /* Read frames from ring */ 2176fc72d1d5SJason Wang ptr = tun_ring_recv(tfile, noblock, &err); 2177fc72d1d5SJason Wang if (!ptr) 2178957f094fSAlex Gartrell return err; 2179ac77cfd4SJason Wang } 2180e0b46d0eSHerbert Xu 2181fc72d1d5SJason Wang if (tun_is_xdp_buff(ptr)) { 2182fc72d1d5SJason Wang struct xdp_buff *xdp = tun_ptr_to_xdp(ptr); 2183fc72d1d5SJason Wang 2184fc72d1d5SJason Wang ret = tun_put_user_xdp(tun, tfile, xdp, to); 2185fc72d1d5SJason Wang put_page(virt_to_head_page(xdp->data)); 2186fc72d1d5SJason Wang } else { 2187fc72d1d5SJason Wang struct sk_buff *skb = ptr; 2188fc72d1d5SJason Wang 21899b067034SAl Viro ret = tun_put_user(tun, tfile, skb, to); 2190f51a5e82SJason Wang if (unlikely(ret < 0)) 21911da177e4SLinus Torvalds kfree_skb(skb); 2192f51a5e82SJason Wang else 2193f51a5e82SJason Wang consume_skb(skb); 2194fc72d1d5SJason Wang } 21951da177e4SLinus Torvalds 219605c2828cSMichael S. Tsirkin return ret; 219705c2828cSMichael S. Tsirkin } 219805c2828cSMichael S. Tsirkin 21999b067034SAl Viro static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to) 220005c2828cSMichael S. Tsirkin { 220105c2828cSMichael S. Tsirkin struct file *file = iocb->ki_filp; 220205c2828cSMichael S. Tsirkin struct tun_file *tfile = file->private_data; 22039484dc74Syuan linyu struct tun_struct *tun = tun_get(tfile); 22049b067034SAl Viro ssize_t len = iov_iter_count(to), ret; 220505c2828cSMichael S. Tsirkin 220605c2828cSMichael S. Tsirkin if (!tun) 220705c2828cSMichael S. Tsirkin return -EBADFD; 2208ac77cfd4SJason Wang ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK, NULL); 220942404c09SDavid S. Miller ret = min_t(ssize_t, ret, len); 2210d0b7da8aSZhi Yong Wu if (ret > 0) 2211d0b7da8aSZhi Yong Wu iocb->ki_pos = ret; 2212631ab46bSEric W. Biederman tun_put(tun); 22131da177e4SLinus Torvalds return ret; 22141da177e4SLinus Torvalds } 22151da177e4SLinus Torvalds 2216cd5681d7SJason Wang static void tun_prog_free(struct rcu_head *rcu) 221796f84061SJason Wang { 2218cd5681d7SJason Wang struct tun_prog *prog = container_of(rcu, struct tun_prog, rcu); 221996f84061SJason Wang 222096f84061SJason Wang bpf_prog_destroy(prog->prog); 222196f84061SJason Wang kfree(prog); 222296f84061SJason Wang } 222396f84061SJason Wang 22249d6474e4SJason Wang static int __tun_set_ebpf(struct tun_struct *tun, 22259d6474e4SJason Wang struct tun_prog __rcu **prog_p, 222696f84061SJason Wang struct bpf_prog *prog) 222796f84061SJason Wang { 2228cd5681d7SJason Wang struct tun_prog *old, *new = NULL; 222996f84061SJason Wang 223096f84061SJason Wang if (prog) { 223196f84061SJason Wang new = kmalloc(sizeof(*new), GFP_KERNEL); 223296f84061SJason Wang if (!new) 223396f84061SJason Wang return -ENOMEM; 223496f84061SJason Wang new->prog = prog; 223596f84061SJason Wang } 223696f84061SJason Wang 2237124da8f6SJason Wang spin_lock_bh(&tun->lock); 2238cd5681d7SJason Wang old = rcu_dereference_protected(*prog_p, 2239124da8f6SJason Wang lockdep_is_held(&tun->lock)); 2240cd5681d7SJason Wang rcu_assign_pointer(*prog_p, new); 2241124da8f6SJason Wang spin_unlock_bh(&tun->lock); 224296f84061SJason Wang 224396f84061SJason Wang if (old) 2244cd5681d7SJason Wang call_rcu(&old->rcu, tun_prog_free); 224596f84061SJason Wang 224696f84061SJason Wang return 0; 224796f84061SJason Wang } 224896f84061SJason Wang 224996442e42SJason Wang static void tun_free_netdev(struct net_device *dev) 225096442e42SJason Wang { 225196442e42SJason Wang struct tun_struct *tun = netdev_priv(dev); 225296442e42SJason Wang 22534008e97fSJason Wang BUG_ON(!(list_empty(&tun->disabled))); 2254608b9977SPaolo Abeni free_percpu(tun->pcpu_stats); 225596442e42SJason Wang tun_flow_uninit(tun); 22565dbbaf2dSPaul Moore security_tun_dev_free_security(tun->security); 2257cd5681d7SJason Wang __tun_set_ebpf(tun, &tun->steering_prog, NULL); 2258aff3d70aSJason Wang __tun_set_ebpf(tun, &tun->filter_prog, NULL); 225996442e42SJason Wang } 226096442e42SJason Wang 22611da177e4SLinus Torvalds static void tun_setup(struct net_device *dev) 22621da177e4SLinus Torvalds { 22631da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 22641da177e4SLinus Torvalds 22650625c883SEric W. Biederman tun->owner = INVALID_UID; 22660625c883SEric W. Biederman tun->group = INVALID_GID; 22671da177e4SLinus Torvalds 22681da177e4SLinus Torvalds dev->ethtool_ops = &tun_ethtool_ops; 2269cf124db5SDavid S. Miller dev->needs_free_netdev = true; 2270cf124db5SDavid S. Miller dev->priv_destructor = tun_free_netdev; 2271016adb72SJason Wang /* We prefer our own queue length */ 2272016adb72SJason Wang dev->tx_queue_len = TUN_READQ_SIZE; 22731da177e4SLinus Torvalds } 22741da177e4SLinus Torvalds 2275f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap 2276f019a7a5SEric W. Biederman * device with netlink. 2277f019a7a5SEric W. Biederman */ 2278a8b8a889SMatthias Schiffer static int tun_validate(struct nlattr *tb[], struct nlattr *data[], 2279a8b8a889SMatthias Schiffer struct netlink_ext_ack *extack) 2280f019a7a5SEric W. Biederman { 2281f019a7a5SEric W. Biederman return -EINVAL; 2282f019a7a5SEric W. Biederman } 2283f019a7a5SEric W. Biederman 22841ec010e7SSabrina Dubroca static size_t tun_get_size(const struct net_device *dev) 22851ec010e7SSabrina Dubroca { 22861ec010e7SSabrina Dubroca BUILD_BUG_ON(sizeof(u32) != sizeof(uid_t)); 22871ec010e7SSabrina Dubroca BUILD_BUG_ON(sizeof(u32) != sizeof(gid_t)); 22881ec010e7SSabrina Dubroca 22891ec010e7SSabrina Dubroca return nla_total_size(sizeof(uid_t)) + /* OWNER */ 22901ec010e7SSabrina Dubroca nla_total_size(sizeof(gid_t)) + /* GROUP */ 22911ec010e7SSabrina Dubroca nla_total_size(sizeof(u8)) + /* TYPE */ 22921ec010e7SSabrina Dubroca nla_total_size(sizeof(u8)) + /* PI */ 22931ec010e7SSabrina Dubroca nla_total_size(sizeof(u8)) + /* VNET_HDR */ 22941ec010e7SSabrina Dubroca nla_total_size(sizeof(u8)) + /* PERSIST */ 22951ec010e7SSabrina Dubroca nla_total_size(sizeof(u8)) + /* MULTI_QUEUE */ 22961ec010e7SSabrina Dubroca nla_total_size(sizeof(u32)) + /* NUM_QUEUES */ 22971ec010e7SSabrina Dubroca nla_total_size(sizeof(u32)) + /* NUM_DISABLED_QUEUES */ 22981ec010e7SSabrina Dubroca 0; 22991ec010e7SSabrina Dubroca } 23001ec010e7SSabrina Dubroca 23011ec010e7SSabrina Dubroca static int tun_fill_info(struct sk_buff *skb, const struct net_device *dev) 23021ec010e7SSabrina Dubroca { 23031ec010e7SSabrina Dubroca struct tun_struct *tun = netdev_priv(dev); 23041ec010e7SSabrina Dubroca 23051ec010e7SSabrina Dubroca if (nla_put_u8(skb, IFLA_TUN_TYPE, tun->flags & TUN_TYPE_MASK)) 23061ec010e7SSabrina Dubroca goto nla_put_failure; 23071ec010e7SSabrina Dubroca if (uid_valid(tun->owner) && 23081ec010e7SSabrina Dubroca nla_put_u32(skb, IFLA_TUN_OWNER, 23091ec010e7SSabrina Dubroca from_kuid_munged(current_user_ns(), tun->owner))) 23101ec010e7SSabrina Dubroca goto nla_put_failure; 23111ec010e7SSabrina Dubroca if (gid_valid(tun->group) && 23121ec010e7SSabrina Dubroca nla_put_u32(skb, IFLA_TUN_GROUP, 23131ec010e7SSabrina Dubroca from_kgid_munged(current_user_ns(), tun->group))) 23141ec010e7SSabrina Dubroca goto nla_put_failure; 23151ec010e7SSabrina Dubroca if (nla_put_u8(skb, IFLA_TUN_PI, !(tun->flags & IFF_NO_PI))) 23161ec010e7SSabrina Dubroca goto nla_put_failure; 23171ec010e7SSabrina Dubroca if (nla_put_u8(skb, IFLA_TUN_VNET_HDR, !!(tun->flags & IFF_VNET_HDR))) 23181ec010e7SSabrina Dubroca goto nla_put_failure; 23191ec010e7SSabrina Dubroca if (nla_put_u8(skb, IFLA_TUN_PERSIST, !!(tun->flags & IFF_PERSIST))) 23201ec010e7SSabrina Dubroca goto nla_put_failure; 23211ec010e7SSabrina Dubroca if (nla_put_u8(skb, IFLA_TUN_MULTI_QUEUE, 23221ec010e7SSabrina Dubroca !!(tun->flags & IFF_MULTI_QUEUE))) 23231ec010e7SSabrina Dubroca goto nla_put_failure; 23241ec010e7SSabrina Dubroca if (tun->flags & IFF_MULTI_QUEUE) { 23251ec010e7SSabrina Dubroca if (nla_put_u32(skb, IFLA_TUN_NUM_QUEUES, tun->numqueues)) 23261ec010e7SSabrina Dubroca goto nla_put_failure; 23271ec010e7SSabrina Dubroca if (nla_put_u32(skb, IFLA_TUN_NUM_DISABLED_QUEUES, 23281ec010e7SSabrina Dubroca tun->numdisabled)) 23291ec010e7SSabrina Dubroca goto nla_put_failure; 23301ec010e7SSabrina Dubroca } 23311ec010e7SSabrina Dubroca 23321ec010e7SSabrina Dubroca return 0; 23331ec010e7SSabrina Dubroca 23341ec010e7SSabrina Dubroca nla_put_failure: 23351ec010e7SSabrina Dubroca return -EMSGSIZE; 23361ec010e7SSabrina Dubroca } 23371ec010e7SSabrina Dubroca 2338f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = { 2339f019a7a5SEric W. Biederman .kind = DRV_NAME, 2340f019a7a5SEric W. Biederman .priv_size = sizeof(struct tun_struct), 2341f019a7a5SEric W. Biederman .setup = tun_setup, 2342f019a7a5SEric W. Biederman .validate = tun_validate, 23431ec010e7SSabrina Dubroca .get_size = tun_get_size, 23441ec010e7SSabrina Dubroca .fill_info = tun_fill_info, 2345f019a7a5SEric W. Biederman }; 2346f019a7a5SEric W. Biederman 234733dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk) 234833dccbb0SHerbert Xu { 234954f968d6SJason Wang struct tun_file *tfile; 235043815482SEric Dumazet wait_queue_head_t *wqueue; 235133dccbb0SHerbert Xu 235233dccbb0SHerbert Xu if (!sock_writeable(sk)) 235333dccbb0SHerbert Xu return; 235433dccbb0SHerbert Xu 23559cd3e072SEric Dumazet if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags)) 235633dccbb0SHerbert Xu return; 235733dccbb0SHerbert Xu 235843815482SEric Dumazet wqueue = sk_sleep(sk); 235943815482SEric Dumazet if (wqueue && waitqueue_active(wqueue)) 2360a9a08845SLinus Torvalds wake_up_interruptible_sync_poll(wqueue, EPOLLOUT | 2361a9a08845SLinus Torvalds EPOLLWRNORM | EPOLLWRBAND); 2362c722c625SHerbert Xu 236354f968d6SJason Wang tfile = container_of(sk, struct tun_file, sk); 236454f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); 236533dccbb0SHerbert Xu } 236633dccbb0SHerbert Xu 23671b784140SYing Xue static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len) 236805c2828cSMichael S. Tsirkin { 236954f968d6SJason Wang int ret; 237054f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 23719484dc74Syuan linyu struct tun_struct *tun = tun_get(tfile); 237254f968d6SJason Wang 237354f968d6SJason Wang if (!tun) 237454f968d6SJason Wang return -EBADFD; 2375f5ff53b4SAl Viro 2376c0371da6SAl Viro ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter, 23775503fcecSJason Wang m->msg_flags & MSG_DONTWAIT, 23785503fcecSJason Wang m->msg_flags & MSG_MORE); 237954f968d6SJason Wang tun_put(tun); 238054f968d6SJason Wang return ret; 238105c2828cSMichael S. Tsirkin } 238205c2828cSMichael S. Tsirkin 23831b784140SYing Xue static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, 238405c2828cSMichael S. Tsirkin int flags) 238505c2828cSMichael S. Tsirkin { 238654f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 23879484dc74Syuan linyu struct tun_struct *tun = tun_get(tfile); 2388fc72d1d5SJason Wang void *ptr = m->msg_control; 238905c2828cSMichael S. Tsirkin int ret; 239054f968d6SJason Wang 2391c33ee15bSWei Xu if (!tun) { 2392c33ee15bSWei Xu ret = -EBADFD; 2393fc72d1d5SJason Wang goto out_free; 2394c33ee15bSWei Xu } 239554f968d6SJason Wang 2396eda29772SRichard Cochran if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) { 23973811ae76SGao feng ret = -EINVAL; 2398c33ee15bSWei Xu goto out_put_tun; 23993811ae76SGao feng } 2400eda29772SRichard Cochran if (flags & MSG_ERRQUEUE) { 2401eda29772SRichard Cochran ret = sock_recv_errqueue(sock->sk, m, total_len, 2402eda29772SRichard Cochran SOL_PACKET, TUN_TX_TIMESTAMP); 2403eda29772SRichard Cochran goto out; 2404eda29772SRichard Cochran } 2405fc72d1d5SJason Wang ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, ptr); 240687897931SAlex Gartrell if (ret > (ssize_t)total_len) { 240742404c09SDavid S. Miller m->msg_flags |= MSG_TRUNC; 240842404c09SDavid S. Miller ret = flags & MSG_TRUNC ? ret : total_len; 240942404c09SDavid S. Miller } 24103811ae76SGao feng out: 241154f968d6SJason Wang tun_put(tun); 241205c2828cSMichael S. Tsirkin return ret; 2413c33ee15bSWei Xu 2414c33ee15bSWei Xu out_put_tun: 2415c33ee15bSWei Xu tun_put(tun); 2416fc72d1d5SJason Wang out_free: 2417fc72d1d5SJason Wang tun_ptr_free(ptr); 2418c33ee15bSWei Xu return ret; 241905c2828cSMichael S. Tsirkin } 242005c2828cSMichael S. Tsirkin 2421fc72d1d5SJason Wang static int tun_ptr_peek_len(void *ptr) 2422fc72d1d5SJason Wang { 2423fc72d1d5SJason Wang if (likely(ptr)) { 2424fc72d1d5SJason Wang if (tun_is_xdp_buff(ptr)) { 2425fc72d1d5SJason Wang struct xdp_buff *xdp = tun_ptr_to_xdp(ptr); 2426fc72d1d5SJason Wang 2427fc72d1d5SJason Wang return xdp->data_end - xdp->data; 2428fc72d1d5SJason Wang } 2429fc72d1d5SJason Wang return __skb_array_len_with_tag(ptr); 2430fc72d1d5SJason Wang } else { 2431fc72d1d5SJason Wang return 0; 2432fc72d1d5SJason Wang } 2433fc72d1d5SJason Wang } 2434fc72d1d5SJason Wang 24351576d986SJason Wang static int tun_peek_len(struct socket *sock) 24361576d986SJason Wang { 24371576d986SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 24381576d986SJason Wang struct tun_struct *tun; 24391576d986SJason Wang int ret = 0; 24401576d986SJason Wang 24419484dc74Syuan linyu tun = tun_get(tfile); 24421576d986SJason Wang if (!tun) 24431576d986SJason Wang return 0; 24441576d986SJason Wang 2445fc72d1d5SJason Wang ret = PTR_RING_PEEK_CALL(&tfile->tx_ring, tun_ptr_peek_len); 24461576d986SJason Wang tun_put(tun); 24471576d986SJason Wang 24481576d986SJason Wang return ret; 24491576d986SJason Wang } 24501576d986SJason Wang 245105c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */ 245205c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = { 24531576d986SJason Wang .peek_len = tun_peek_len, 245405c2828cSMichael S. Tsirkin .sendmsg = tun_sendmsg, 245505c2828cSMichael S. Tsirkin .recvmsg = tun_recvmsg, 245605c2828cSMichael S. Tsirkin }; 245705c2828cSMichael S. Tsirkin 245833dccbb0SHerbert Xu static struct proto tun_proto = { 245933dccbb0SHerbert Xu .name = "tun", 246033dccbb0SHerbert Xu .owner = THIS_MODULE, 246154f968d6SJason Wang .obj_size = sizeof(struct tun_file), 246233dccbb0SHerbert Xu }; 2463f019a7a5SEric W. Biederman 2464980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun) 2465980c9e8cSDavid Woodhouse { 2466031f5e03SMichael S. Tsirkin return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP); 2467980c9e8cSDavid Woodhouse } 2468980c9e8cSDavid Woodhouse 2469980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr, 2470980c9e8cSDavid Woodhouse char *buf) 2471980c9e8cSDavid Woodhouse { 2472980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 2473980c9e8cSDavid Woodhouse return sprintf(buf, "0x%x\n", tun_flags(tun)); 2474980c9e8cSDavid Woodhouse } 2475980c9e8cSDavid Woodhouse 2476980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr, 2477980c9e8cSDavid Woodhouse char *buf) 2478980c9e8cSDavid Woodhouse { 2479980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 24800625c883SEric W. Biederman return uid_valid(tun->owner)? 24810625c883SEric W. Biederman sprintf(buf, "%u\n", 24820625c883SEric W. Biederman from_kuid_munged(current_user_ns(), tun->owner)): 24830625c883SEric W. Biederman sprintf(buf, "-1\n"); 2484980c9e8cSDavid Woodhouse } 2485980c9e8cSDavid Woodhouse 2486980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr, 2487980c9e8cSDavid Woodhouse char *buf) 2488980c9e8cSDavid Woodhouse { 2489980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 24900625c883SEric W. Biederman return gid_valid(tun->group) ? 24910625c883SEric W. Biederman sprintf(buf, "%u\n", 24920625c883SEric W. Biederman from_kgid_munged(current_user_ns(), tun->group)): 24930625c883SEric W. Biederman sprintf(buf, "-1\n"); 2494980c9e8cSDavid Woodhouse } 2495980c9e8cSDavid Woodhouse 2496980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL); 2497980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL); 2498980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL); 2499980c9e8cSDavid Woodhouse 2500c4d33e24STakashi Iwai static struct attribute *tun_dev_attrs[] = { 2501c4d33e24STakashi Iwai &dev_attr_tun_flags.attr, 2502c4d33e24STakashi Iwai &dev_attr_owner.attr, 2503c4d33e24STakashi Iwai &dev_attr_group.attr, 2504c4d33e24STakashi Iwai NULL 2505c4d33e24STakashi Iwai }; 2506c4d33e24STakashi Iwai 2507c4d33e24STakashi Iwai static const struct attribute_group tun_attr_group = { 2508c4d33e24STakashi Iwai .attrs = tun_dev_attrs 2509c4d33e24STakashi Iwai }; 2510c4d33e24STakashi Iwai 2511d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) 25121da177e4SLinus Torvalds { 25131da177e4SLinus Torvalds struct tun_struct *tun; 251454f968d6SJason Wang struct tun_file *tfile = file->private_data; 25151da177e4SLinus Torvalds struct net_device *dev; 25161da177e4SLinus Torvalds int err; 25171da177e4SLinus Torvalds 25187c0c3b1aSJason Wang if (tfile->detached) 25197c0c3b1aSJason Wang return -EINVAL; 25207c0c3b1aSJason Wang 252190e33d45SPetar Penkov if ((ifr->ifr_flags & IFF_NAPI_FRAGS)) { 252290e33d45SPetar Penkov if (!capable(CAP_NET_ADMIN)) 252390e33d45SPetar Penkov return -EPERM; 252490e33d45SPetar Penkov 252590e33d45SPetar Penkov if (!(ifr->ifr_flags & IFF_NAPI) || 252690e33d45SPetar Penkov (ifr->ifr_flags & TUN_TYPE_MASK) != IFF_TAP) 252790e33d45SPetar Penkov return -EINVAL; 252890e33d45SPetar Penkov } 252990e33d45SPetar Penkov 253074a3e5a7SEric W. Biederman dev = __dev_get_by_name(net, ifr->ifr_name); 253174a3e5a7SEric W. Biederman if (dev) { 2532f85ba780SDavid Woodhouse if (ifr->ifr_flags & IFF_TUN_EXCL) 2533f85ba780SDavid Woodhouse return -EBUSY; 253474a3e5a7SEric W. Biederman if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) 253574a3e5a7SEric W. Biederman tun = netdev_priv(dev); 253674a3e5a7SEric W. Biederman else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) 253774a3e5a7SEric W. Biederman tun = netdev_priv(dev); 253874a3e5a7SEric W. Biederman else 253974a3e5a7SEric W. Biederman return -EINVAL; 254074a3e5a7SEric W. Biederman 25418e6d91aeSJason Wang if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) != 254240630b82SMichael S. Tsirkin !!(tun->flags & IFF_MULTI_QUEUE)) 25438e6d91aeSJason Wang return -EINVAL; 25448e6d91aeSJason Wang 2545cde8b15fSJason Wang if (tun_not_capable(tun)) 25462b980dbdSPaul Moore return -EPERM; 25475dbbaf2dSPaul Moore err = security_tun_dev_open(tun->security); 25482b980dbdSPaul Moore if (err < 0) 25492b980dbdSPaul Moore return err; 25502b980dbdSPaul Moore 255194317099SPetar Penkov err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER, 255294317099SPetar Penkov ifr->ifr_flags & IFF_NAPI); 2553a7385ba2SEric W. Biederman if (err < 0) 2554a7385ba2SEric W. Biederman return err; 25554008e97fSJason Wang 255640630b82SMichael S. Tsirkin if (tun->flags & IFF_MULTI_QUEUE && 2557e8dbad66SJason Wang (tun->numqueues + tun->numdisabled > 1)) { 2558e8dbad66SJason Wang /* One or more queue has already been attached, no need 2559e8dbad66SJason Wang * to initialize the device again. 2560e8dbad66SJason Wang */ 256183c1f36fSSabrina Dubroca netdev_state_change(dev); 2562e8dbad66SJason Wang return 0; 2563e8dbad66SJason Wang } 25649fffc5c6SSabrina Dubroca 25659fffc5c6SSabrina Dubroca tun->flags = (tun->flags & ~TUN_FEATURES) | 25669fffc5c6SSabrina Dubroca (ifr->ifr_flags & TUN_FEATURES); 256783c1f36fSSabrina Dubroca 256883c1f36fSSabrina Dubroca netdev_state_change(dev); 256983c1f36fSSabrina Dubroca } else { 25701da177e4SLinus Torvalds char *name; 25711da177e4SLinus Torvalds unsigned long flags = 0; 2572edfb6a14SJason Wang int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ? 2573edfb6a14SJason Wang MAX_TAP_QUEUES : 1; 25741da177e4SLinus Torvalds 2575c260b772SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 2576ca6bb5d7SDavid Woodhouse return -EPERM; 25772b980dbdSPaul Moore err = security_tun_dev_create(); 25782b980dbdSPaul Moore if (err < 0) 25792b980dbdSPaul Moore return err; 2580ca6bb5d7SDavid Woodhouse 25811da177e4SLinus Torvalds /* Set dev type */ 25821da177e4SLinus Torvalds if (ifr->ifr_flags & IFF_TUN) { 25831da177e4SLinus Torvalds /* TUN device */ 258440630b82SMichael S. Tsirkin flags |= IFF_TUN; 25851da177e4SLinus Torvalds name = "tun%d"; 25861da177e4SLinus Torvalds } else if (ifr->ifr_flags & IFF_TAP) { 25871da177e4SLinus Torvalds /* TAP device */ 258840630b82SMichael S. Tsirkin flags |= IFF_TAP; 25891da177e4SLinus Torvalds name = "tap%d"; 25901da177e4SLinus Torvalds } else 259136989b90SKusanagi Kouichi return -EINVAL; 25921da177e4SLinus Torvalds 25931da177e4SLinus Torvalds if (*ifr->ifr_name) 25941da177e4SLinus Torvalds name = ifr->ifr_name; 25951da177e4SLinus Torvalds 2596c8d68e6bSJason Wang dev = alloc_netdev_mqs(sizeof(struct tun_struct), name, 2597c835a677STom Gundersen NET_NAME_UNKNOWN, tun_setup, queues, 2598c835a677STom Gundersen queues); 2599edfb6a14SJason Wang 26001da177e4SLinus Torvalds if (!dev) 26011da177e4SLinus Torvalds return -ENOMEM; 26020ad646c8SCong Wang err = dev_get_valid_name(net, dev, name); 26035c25f65fSJulien Gomes if (err < 0) 26040ad646c8SCong Wang goto err_free_dev; 26051da177e4SLinus Torvalds 2606fc54c658SPavel Emelyanov dev_net_set(dev, net); 2607f019a7a5SEric W. Biederman dev->rtnl_link_ops = &tun_link_ops; 2608fb7589a1SPavel Emelyanov dev->ifindex = tfile->ifindex; 2609c4d33e24STakashi Iwai dev->sysfs_groups[0] = &tun_attr_group; 2610758e43b7SStephen Hemminger 26111da177e4SLinus Torvalds tun = netdev_priv(dev); 26121da177e4SLinus Torvalds tun->dev = dev; 26131da177e4SLinus Torvalds tun->flags = flags; 2614f271b2ccSMax Krasnyansky tun->txflt.count = 0; 2615d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr); 26161da177e4SLinus Torvalds 2617eaea34b2SPaolo Abeni tun->align = NET_SKB_PAD; 261854f968d6SJason Wang tun->filter_attached = false; 261954f968d6SJason Wang tun->sndbuf = tfile->socket.sk->sk_sndbuf; 26205503fcecSJason Wang tun->rx_batched = 0; 262196f84061SJason Wang RCU_INIT_POINTER(tun->steering_prog, NULL); 262233dccbb0SHerbert Xu 2623608b9977SPaolo Abeni tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats); 2624608b9977SPaolo Abeni if (!tun->pcpu_stats) { 2625608b9977SPaolo Abeni err = -ENOMEM; 2626608b9977SPaolo Abeni goto err_free_dev; 2627608b9977SPaolo Abeni } 2628608b9977SPaolo Abeni 262996442e42SJason Wang spin_lock_init(&tun->lock); 263096442e42SJason Wang 26315dbbaf2dSPaul Moore err = security_tun_dev_alloc_security(&tun->security); 26325dbbaf2dSPaul Moore if (err < 0) 2633608b9977SPaolo Abeni goto err_free_stat; 26342b980dbdSPaul Moore 26351da177e4SLinus Torvalds tun_net_init(dev); 2636944a1376SPavel Emelyanov tun_flow_init(tun); 263796442e42SJason Wang 263888255375SMichał Mirosław dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | 26396680ec68SJason Wang TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX | 26406680ec68SJason Wang NETIF_F_HW_VLAN_STAG_TX; 26412a2bbf17SPaolo Abeni dev->features = dev->hw_features | NETIF_F_LLTX; 26426671b224SFernando Luis Vazquez Cao dev->vlan_features = dev->features & 26436671b224SFernando Luis Vazquez Cao ~(NETIF_F_HW_VLAN_CTAG_TX | 26446671b224SFernando Luis Vazquez Cao NETIF_F_HW_VLAN_STAG_TX); 264588255375SMichał Mirosław 26469fffc5c6SSabrina Dubroca tun->flags = (tun->flags & ~TUN_FEATURES) | 26479fffc5c6SSabrina Dubroca (ifr->ifr_flags & TUN_FEATURES); 26489fffc5c6SSabrina Dubroca 26494008e97fSJason Wang INIT_LIST_HEAD(&tun->disabled); 265094317099SPetar Penkov err = tun_attach(tun, file, false, ifr->ifr_flags & IFF_NAPI); 2651eb0fb363SJason Wang if (err < 0) 2652662ca437SJason Wang goto err_free_flow; 2653eb0fb363SJason Wang 26541da177e4SLinus Torvalds err = register_netdevice(tun->dev); 26551da177e4SLinus Torvalds if (err < 0) 2656662ca437SJason Wang goto err_detach; 2657af668b3cSMichael S. Tsirkin } 2658980c9e8cSDavid Woodhouse 2659eb0fb363SJason Wang netif_carrier_on(tun->dev); 26601da177e4SLinus Torvalds 26616b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_set_iff\n"); 26621da177e4SLinus Torvalds 2663e35259a9SMax Krasnyansky /* Make sure persistent devices do not get stuck in 2664e35259a9SMax Krasnyansky * xoff state. 2665e35259a9SMax Krasnyansky */ 2666e35259a9SMax Krasnyansky if (netif_running(tun->dev)) 2667c8d68e6bSJason Wang netif_tx_wake_all_queues(tun->dev); 2668e35259a9SMax Krasnyansky 26691da177e4SLinus Torvalds strcpy(ifr->ifr_name, tun->dev->name); 26701da177e4SLinus Torvalds return 0; 26711da177e4SLinus Torvalds 2672662ca437SJason Wang err_detach: 2673662ca437SJason Wang tun_detach_all(dev); 2674ff244c6bSEric Dumazet /* register_netdevice() already called tun_free_netdev() */ 2675ff244c6bSEric Dumazet goto err_free_dev; 2676ff244c6bSEric Dumazet 2677662ca437SJason Wang err_free_flow: 2678662ca437SJason Wang tun_flow_uninit(tun); 2679662ca437SJason Wang security_tun_dev_free_security(tun->security); 2680608b9977SPaolo Abeni err_free_stat: 2681608b9977SPaolo Abeni free_percpu(tun->pcpu_stats); 26821da177e4SLinus Torvalds err_free_dev: 26831da177e4SLinus Torvalds free_netdev(dev); 26841da177e4SLinus Torvalds return err; 26851da177e4SLinus Torvalds } 26861da177e4SLinus Torvalds 26879ce99cf6SRami Rosen static void tun_get_iff(struct net *net, struct tun_struct *tun, 2688876bfd4dSHerbert Xu struct ifreq *ifr) 2689e3b99556SMark McLoughlin { 26906b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_get_iff\n"); 2691e3b99556SMark McLoughlin 2692e3b99556SMark McLoughlin strcpy(ifr->ifr_name, tun->dev->name); 2693e3b99556SMark McLoughlin 2694980c9e8cSDavid Woodhouse ifr->ifr_flags = tun_flags(tun); 2695e3b99556SMark McLoughlin 2696e3b99556SMark McLoughlin } 2697e3b99556SMark McLoughlin 26985228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no 26995228ddc9SRusty Russell * privs required. */ 270088255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg) 27015228ddc9SRusty Russell { 2702c8f44affSMichał Mirosław netdev_features_t features = 0; 27035228ddc9SRusty Russell 27045228ddc9SRusty Russell if (arg & TUN_F_CSUM) { 270588255375SMichał Mirosław features |= NETIF_F_HW_CSUM; 27065228ddc9SRusty Russell arg &= ~TUN_F_CSUM; 27075228ddc9SRusty Russell 27085228ddc9SRusty Russell if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { 27095228ddc9SRusty Russell if (arg & TUN_F_TSO_ECN) { 27105228ddc9SRusty Russell features |= NETIF_F_TSO_ECN; 27115228ddc9SRusty Russell arg &= ~TUN_F_TSO_ECN; 27125228ddc9SRusty Russell } 27135228ddc9SRusty Russell if (arg & TUN_F_TSO4) 27145228ddc9SRusty Russell features |= NETIF_F_TSO; 27155228ddc9SRusty Russell if (arg & TUN_F_TSO6) 27165228ddc9SRusty Russell features |= NETIF_F_TSO6; 27175228ddc9SRusty Russell arg &= ~(TUN_F_TSO4|TUN_F_TSO6); 27185228ddc9SRusty Russell } 27190c19f846SWillem de Bruijn 27200c19f846SWillem de Bruijn arg &= ~TUN_F_UFO; 27215228ddc9SRusty Russell } 27225228ddc9SRusty Russell 27235228ddc9SRusty Russell /* This gives the user a way to test for new features in future by 27245228ddc9SRusty Russell * trying to set them. */ 27255228ddc9SRusty Russell if (arg) 27265228ddc9SRusty Russell return -EINVAL; 27275228ddc9SRusty Russell 272888255375SMichał Mirosław tun->set_features = features; 272909050957SYaroslav Isakov tun->dev->wanted_features &= ~TUN_USER_FEATURES; 273009050957SYaroslav Isakov tun->dev->wanted_features |= features; 273188255375SMichał Mirosław netdev_update_features(tun->dev); 27325228ddc9SRusty Russell 27335228ddc9SRusty Russell return 0; 27345228ddc9SRusty Russell } 27355228ddc9SRusty Russell 2736c8d68e6bSJason Wang static void tun_detach_filter(struct tun_struct *tun, int n) 2737c8d68e6bSJason Wang { 2738c8d68e6bSJason Wang int i; 2739c8d68e6bSJason Wang struct tun_file *tfile; 2740c8d68e6bSJason Wang 2741c8d68e6bSJason Wang for (i = 0; i < n; i++) { 2742b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 27438ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 27448ced425eSHannes Frederic Sowa sk_detach_filter(tfile->socket.sk); 27458ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 2746c8d68e6bSJason Wang } 2747c8d68e6bSJason Wang 2748c8d68e6bSJason Wang tun->filter_attached = false; 2749c8d68e6bSJason Wang } 2750c8d68e6bSJason Wang 2751c8d68e6bSJason Wang static int tun_attach_filter(struct tun_struct *tun) 2752c8d68e6bSJason Wang { 2753c8d68e6bSJason Wang int i, ret = 0; 2754c8d68e6bSJason Wang struct tun_file *tfile; 2755c8d68e6bSJason Wang 2756c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 2757b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 27588ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 27598ced425eSHannes Frederic Sowa ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); 27608ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 2761c8d68e6bSJason Wang if (ret) { 2762c8d68e6bSJason Wang tun_detach_filter(tun, i); 2763c8d68e6bSJason Wang return ret; 2764c8d68e6bSJason Wang } 2765c8d68e6bSJason Wang } 2766c8d68e6bSJason Wang 2767c8d68e6bSJason Wang tun->filter_attached = true; 2768c8d68e6bSJason Wang return ret; 2769c8d68e6bSJason Wang } 2770c8d68e6bSJason Wang 2771c8d68e6bSJason Wang static void tun_set_sndbuf(struct tun_struct *tun) 2772c8d68e6bSJason Wang { 2773c8d68e6bSJason Wang struct tun_file *tfile; 2774c8d68e6bSJason Wang int i; 2775c8d68e6bSJason Wang 2776c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 2777b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 2778c8d68e6bSJason Wang tfile->socket.sk->sk_sndbuf = tun->sndbuf; 2779c8d68e6bSJason Wang } 2780c8d68e6bSJason Wang } 2781c8d68e6bSJason Wang 2782cde8b15fSJason Wang static int tun_set_queue(struct file *file, struct ifreq *ifr) 2783cde8b15fSJason Wang { 2784cde8b15fSJason Wang struct tun_file *tfile = file->private_data; 2785cde8b15fSJason Wang struct tun_struct *tun; 2786cde8b15fSJason Wang int ret = 0; 2787cde8b15fSJason Wang 2788cde8b15fSJason Wang rtnl_lock(); 2789cde8b15fSJason Wang 2790cde8b15fSJason Wang if (ifr->ifr_flags & IFF_ATTACH_QUEUE) { 27914008e97fSJason Wang tun = tfile->detached; 27925dbbaf2dSPaul Moore if (!tun) { 2793cde8b15fSJason Wang ret = -EINVAL; 27945dbbaf2dSPaul Moore goto unlock; 27955dbbaf2dSPaul Moore } 27965dbbaf2dSPaul Moore ret = security_tun_dev_attach_queue(tun->security); 27975dbbaf2dSPaul Moore if (ret < 0) 27985dbbaf2dSPaul Moore goto unlock; 279994317099SPetar Penkov ret = tun_attach(tun, file, false, tun->flags & IFF_NAPI); 28004008e97fSJason Wang } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) { 2801b8deabd3SJason Wang tun = rtnl_dereference(tfile->tun); 280240630b82SMichael S. Tsirkin if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached) 28034008e97fSJason Wang ret = -EINVAL; 2804cde8b15fSJason Wang else 28054008e97fSJason Wang __tun_detach(tfile, false); 28064008e97fSJason Wang } else 2807cde8b15fSJason Wang ret = -EINVAL; 2808cde8b15fSJason Wang 280983c1f36fSSabrina Dubroca if (ret >= 0) 281083c1f36fSSabrina Dubroca netdev_state_change(tun->dev); 281183c1f36fSSabrina Dubroca 28125dbbaf2dSPaul Moore unlock: 2813cde8b15fSJason Wang rtnl_unlock(); 2814cde8b15fSJason Wang return ret; 2815cde8b15fSJason Wang } 2816cde8b15fSJason Wang 2817cd5681d7SJason Wang static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog **prog_p, 2818cd5681d7SJason Wang void __user *data) 281996f84061SJason Wang { 282096f84061SJason Wang struct bpf_prog *prog; 282196f84061SJason Wang int fd; 282296f84061SJason Wang 282396f84061SJason Wang if (copy_from_user(&fd, data, sizeof(fd))) 282496f84061SJason Wang return -EFAULT; 282596f84061SJason Wang 282696f84061SJason Wang if (fd == -1) { 282796f84061SJason Wang prog = NULL; 282896f84061SJason Wang } else { 282996f84061SJason Wang prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER); 283096f84061SJason Wang if (IS_ERR(prog)) 283196f84061SJason Wang return PTR_ERR(prog); 283296f84061SJason Wang } 283396f84061SJason Wang 2834cd5681d7SJason Wang return __tun_set_ebpf(tun, prog_p, prog); 283596f84061SJason Wang } 283696f84061SJason Wang 283750857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd, 283850857e2aSArnd Bergmann unsigned long arg, int ifreq_len) 28391da177e4SLinus Torvalds { 284036b50babSEric W. Biederman struct tun_file *tfile = file->private_data; 2841631ab46bSEric W. Biederman struct tun_struct *tun; 28421da177e4SLinus Torvalds void __user* argp = (void __user*)arg; 28431da177e4SLinus Torvalds struct ifreq ifr; 2844f2780d6dSKirill Tkhai struct net *net; 28450625c883SEric W. Biederman kuid_t owner; 28460625c883SEric W. Biederman kgid_t group; 284733dccbb0SHerbert Xu int sndbuf; 2848d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 2849fb7589a1SPavel Emelyanov unsigned int ifindex; 28501cf8e410SMichael S. Tsirkin int le; 2851f271b2ccSMax Krasnyansky int ret; 285283c1f36fSSabrina Dubroca bool do_notify = false; 28531da177e4SLinus Torvalds 2854f2780d6dSKirill Tkhai if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || 2855f2780d6dSKirill Tkhai (_IOC_TYPE(cmd) == SOCK_IOC_TYPE && cmd != SIOCGSKNS)) { 285650857e2aSArnd Bergmann if (copy_from_user(&ifr, argp, ifreq_len)) 28571da177e4SLinus Torvalds return -EFAULT; 28588bbb1813SDavid S. Miller } else { 2859a117dacdSMathias Krause memset(&ifr, 0, sizeof(ifr)); 28608bbb1813SDavid S. Miller } 2861631ab46bSEric W. Biederman if (cmd == TUNGETFEATURES) { 2862631ab46bSEric W. Biederman /* Currently this just means: "what IFF flags are valid?". 2863631ab46bSEric W. Biederman * This is needed because we never checked for invalid flags on 2864031f5e03SMichael S. Tsirkin * TUNSETIFF. 2865031f5e03SMichael S. Tsirkin */ 2866031f5e03SMichael S. Tsirkin return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES, 2867631ab46bSEric W. Biederman (unsigned int __user*)argp); 2868cde8b15fSJason Wang } else if (cmd == TUNSETQUEUE) 2869cde8b15fSJason Wang return tun_set_queue(file, &ifr); 2870631ab46bSEric W. Biederman 2871c8d68e6bSJason Wang ret = 0; 2872876bfd4dSHerbert Xu rtnl_lock(); 2873876bfd4dSHerbert Xu 28749484dc74Syuan linyu tun = tun_get(tfile); 2875f2780d6dSKirill Tkhai net = sock_net(&tfile->sk); 28760f16bc13SGao Feng if (cmd == TUNSETIFF) { 28770f16bc13SGao Feng ret = -EEXIST; 28780f16bc13SGao Feng if (tun) 28790f16bc13SGao Feng goto unlock; 28800f16bc13SGao Feng 28811da177e4SLinus Torvalds ifr.ifr_name[IFNAMSIZ-1] = '\0'; 28821da177e4SLinus Torvalds 2883f2780d6dSKirill Tkhai ret = tun_set_iff(net, file, &ifr); 28841da177e4SLinus Torvalds 2885876bfd4dSHerbert Xu if (ret) 2886876bfd4dSHerbert Xu goto unlock; 28871da177e4SLinus Torvalds 288850857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 2889876bfd4dSHerbert Xu ret = -EFAULT; 2890876bfd4dSHerbert Xu goto unlock; 28911da177e4SLinus Torvalds } 2892fb7589a1SPavel Emelyanov if (cmd == TUNSETIFINDEX) { 2893fb7589a1SPavel Emelyanov ret = -EPERM; 2894fb7589a1SPavel Emelyanov if (tun) 2895fb7589a1SPavel Emelyanov goto unlock; 2896fb7589a1SPavel Emelyanov 2897fb7589a1SPavel Emelyanov ret = -EFAULT; 2898fb7589a1SPavel Emelyanov if (copy_from_user(&ifindex, argp, sizeof(ifindex))) 2899fb7589a1SPavel Emelyanov goto unlock; 2900fb7589a1SPavel Emelyanov 2901fb7589a1SPavel Emelyanov ret = 0; 2902fb7589a1SPavel Emelyanov tfile->ifindex = ifindex; 2903fb7589a1SPavel Emelyanov goto unlock; 2904fb7589a1SPavel Emelyanov } 2905f2780d6dSKirill Tkhai if (cmd == SIOCGSKNS) { 2906f2780d6dSKirill Tkhai ret = -EPERM; 2907f2780d6dSKirill Tkhai if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 2908f2780d6dSKirill Tkhai goto unlock; 2909f2780d6dSKirill Tkhai 2910f2780d6dSKirill Tkhai ret = open_related_ns(&net->ns, get_net_ns); 2911f2780d6dSKirill Tkhai goto unlock; 2912f2780d6dSKirill Tkhai } 29131da177e4SLinus Torvalds 2914876bfd4dSHerbert Xu ret = -EBADFD; 29151da177e4SLinus Torvalds if (!tun) 2916876bfd4dSHerbert Xu goto unlock; 29171da177e4SLinus Torvalds 29181e588338SJason Wang tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd); 29191da177e4SLinus Torvalds 2920631ab46bSEric W. Biederman ret = 0; 29211da177e4SLinus Torvalds switch (cmd) { 2922e3b99556SMark McLoughlin case TUNGETIFF: 29239ce99cf6SRami Rosen tun_get_iff(current->nsproxy->net_ns, tun, &ifr); 2924e3b99556SMark McLoughlin 29253d407a80SPavel Emelyanov if (tfile->detached) 29263d407a80SPavel Emelyanov ifr.ifr_flags |= IFF_DETACH_QUEUE; 2927849c9b6fSPavel Emelyanov if (!tfile->socket.sk->sk_filter) 2928849c9b6fSPavel Emelyanov ifr.ifr_flags |= IFF_NOFILTER; 29293d407a80SPavel Emelyanov 293050857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 2931631ab46bSEric W. Biederman ret = -EFAULT; 2932e3b99556SMark McLoughlin break; 2933e3b99556SMark McLoughlin 29341da177e4SLinus Torvalds case TUNSETNOCSUM: 29351da177e4SLinus Torvalds /* Disable/Enable checksum */ 29361da177e4SLinus Torvalds 293788255375SMichał Mirosław /* [unimplemented] */ 293888255375SMichał Mirosław tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n", 29396b8a66eeSJoe Perches arg ? "disabled" : "enabled"); 29401da177e4SLinus Torvalds break; 29411da177e4SLinus Torvalds 29421da177e4SLinus Torvalds case TUNSETPERSIST: 294354f968d6SJason Wang /* Disable/Enable persist mode. Keep an extra reference to the 294454f968d6SJason Wang * module to prevent the module being unprobed. 294554f968d6SJason Wang */ 294640630b82SMichael S. Tsirkin if (arg && !(tun->flags & IFF_PERSIST)) { 294740630b82SMichael S. Tsirkin tun->flags |= IFF_PERSIST; 294854f968d6SJason Wang __module_get(THIS_MODULE); 294983c1f36fSSabrina Dubroca do_notify = true; 2950dd38bd85SJason Wang } 295140630b82SMichael S. Tsirkin if (!arg && (tun->flags & IFF_PERSIST)) { 295240630b82SMichael S. Tsirkin tun->flags &= ~IFF_PERSIST; 295354f968d6SJason Wang module_put(THIS_MODULE); 295483c1f36fSSabrina Dubroca do_notify = true; 295554f968d6SJason Wang } 29561da177e4SLinus Torvalds 29576b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "persist %s\n", 29586b8a66eeSJoe Perches arg ? "enabled" : "disabled"); 29591da177e4SLinus Torvalds break; 29601da177e4SLinus Torvalds 29611da177e4SLinus Torvalds case TUNSETOWNER: 29621da177e4SLinus Torvalds /* Set owner of the device */ 29630625c883SEric W. Biederman owner = make_kuid(current_user_ns(), arg); 29640625c883SEric W. Biederman if (!uid_valid(owner)) { 29650625c883SEric W. Biederman ret = -EINVAL; 29660625c883SEric W. Biederman break; 29670625c883SEric W. Biederman } 29680625c883SEric W. Biederman tun->owner = owner; 296983c1f36fSSabrina Dubroca do_notify = true; 29701e588338SJason Wang tun_debug(KERN_INFO, tun, "owner set to %u\n", 29710625c883SEric W. Biederman from_kuid(&init_user_ns, tun->owner)); 29721da177e4SLinus Torvalds break; 29731da177e4SLinus Torvalds 29748c644623SGuido Guenther case TUNSETGROUP: 29758c644623SGuido Guenther /* Set group of the device */ 29760625c883SEric W. Biederman group = make_kgid(current_user_ns(), arg); 29770625c883SEric W. Biederman if (!gid_valid(group)) { 29780625c883SEric W. Biederman ret = -EINVAL; 29790625c883SEric W. Biederman break; 29800625c883SEric W. Biederman } 29810625c883SEric W. Biederman tun->group = group; 298283c1f36fSSabrina Dubroca do_notify = true; 29831e588338SJason Wang tun_debug(KERN_INFO, tun, "group set to %u\n", 29840625c883SEric W. Biederman from_kgid(&init_user_ns, tun->group)); 29858c644623SGuido Guenther break; 29868c644623SGuido Guenther 2987ff4cc3acSMike Kershaw case TUNSETLINK: 2988ff4cc3acSMike Kershaw /* Only allow setting the type when the interface is down */ 2989ff4cc3acSMike Kershaw if (tun->dev->flags & IFF_UP) { 29906b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, 29916b8a66eeSJoe Perches "Linktype set failed because interface is up\n"); 299248abfe05SDavid S. Miller ret = -EBUSY; 2993ff4cc3acSMike Kershaw } else { 2994ff4cc3acSMike Kershaw tun->dev->type = (int) arg; 29956b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "linktype set to %d\n", 29966b8a66eeSJoe Perches tun->dev->type); 299748abfe05SDavid S. Miller ret = 0; 2998ff4cc3acSMike Kershaw } 2999631ab46bSEric W. Biederman break; 3000ff4cc3acSMike Kershaw 30011da177e4SLinus Torvalds #ifdef TUN_DEBUG 30021da177e4SLinus Torvalds case TUNSETDEBUG: 30031da177e4SLinus Torvalds tun->debug = arg; 30041da177e4SLinus Torvalds break; 30051da177e4SLinus Torvalds #endif 30065228ddc9SRusty Russell case TUNSETOFFLOAD: 300788255375SMichał Mirosław ret = set_offload(tun, arg); 3008631ab46bSEric W. Biederman break; 30095228ddc9SRusty Russell 3010f271b2ccSMax Krasnyansky case TUNSETTXFILTER: 3011f271b2ccSMax Krasnyansky /* Can be set only for TAPs */ 3012631ab46bSEric W. Biederman ret = -EINVAL; 301340630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 3014631ab46bSEric W. Biederman break; 3015c0e5a8c2SHarvey Harrison ret = update_filter(&tun->txflt, (void __user *)arg); 3016631ab46bSEric W. Biederman break; 30171da177e4SLinus Torvalds 30181da177e4SLinus Torvalds case SIOCGIFHWADDR: 3019b595076aSUwe Kleine-König /* Get hw address */ 3020f271b2ccSMax Krasnyansky memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); 3021f271b2ccSMax Krasnyansky ifr.ifr_hwaddr.sa_family = tun->dev->type; 302250857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 3023631ab46bSEric W. Biederman ret = -EFAULT; 3024631ab46bSEric W. Biederman break; 30251da177e4SLinus Torvalds 30261da177e4SLinus Torvalds case SIOCSIFHWADDR: 3027f271b2ccSMax Krasnyansky /* Set hw address */ 30286b8a66eeSJoe Perches tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n", 30296b8a66eeSJoe Perches ifr.ifr_hwaddr.sa_data); 303040102371SKim B. Heino 303140102371SKim B. Heino ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); 3032631ab46bSEric W. Biederman break; 303333dccbb0SHerbert Xu 303433dccbb0SHerbert Xu case TUNGETSNDBUF: 303554f968d6SJason Wang sndbuf = tfile->socket.sk->sk_sndbuf; 303633dccbb0SHerbert Xu if (copy_to_user(argp, &sndbuf, sizeof(sndbuf))) 303733dccbb0SHerbert Xu ret = -EFAULT; 303833dccbb0SHerbert Xu break; 303933dccbb0SHerbert Xu 304033dccbb0SHerbert Xu case TUNSETSNDBUF: 304133dccbb0SHerbert Xu if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) { 304233dccbb0SHerbert Xu ret = -EFAULT; 304333dccbb0SHerbert Xu break; 304433dccbb0SHerbert Xu } 304593161922SCraig Gallek if (sndbuf <= 0) { 304693161922SCraig Gallek ret = -EINVAL; 304793161922SCraig Gallek break; 304893161922SCraig Gallek } 304933dccbb0SHerbert Xu 3050c8d68e6bSJason Wang tun->sndbuf = sndbuf; 3051c8d68e6bSJason Wang tun_set_sndbuf(tun); 305233dccbb0SHerbert Xu break; 305333dccbb0SHerbert Xu 3054d9d52b51SMichael S. Tsirkin case TUNGETVNETHDRSZ: 3055d9d52b51SMichael S. Tsirkin vnet_hdr_sz = tun->vnet_hdr_sz; 3056d9d52b51SMichael S. Tsirkin if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz))) 3057d9d52b51SMichael S. Tsirkin ret = -EFAULT; 3058d9d52b51SMichael S. Tsirkin break; 3059d9d52b51SMichael S. Tsirkin 3060d9d52b51SMichael S. Tsirkin case TUNSETVNETHDRSZ: 3061d9d52b51SMichael S. Tsirkin if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) { 3062d9d52b51SMichael S. Tsirkin ret = -EFAULT; 3063d9d52b51SMichael S. Tsirkin break; 3064d9d52b51SMichael S. Tsirkin } 3065d9d52b51SMichael S. Tsirkin if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) { 3066d9d52b51SMichael S. Tsirkin ret = -EINVAL; 3067d9d52b51SMichael S. Tsirkin break; 3068d9d52b51SMichael S. Tsirkin } 3069d9d52b51SMichael S. Tsirkin 3070d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = vnet_hdr_sz; 3071d9d52b51SMichael S. Tsirkin break; 3072d9d52b51SMichael S. Tsirkin 30731cf8e410SMichael S. Tsirkin case TUNGETVNETLE: 30741cf8e410SMichael S. Tsirkin le = !!(tun->flags & TUN_VNET_LE); 30751cf8e410SMichael S. Tsirkin if (put_user(le, (int __user *)argp)) 30761cf8e410SMichael S. Tsirkin ret = -EFAULT; 30771cf8e410SMichael S. Tsirkin break; 30781cf8e410SMichael S. Tsirkin 30791cf8e410SMichael S. Tsirkin case TUNSETVNETLE: 30801cf8e410SMichael S. Tsirkin if (get_user(le, (int __user *)argp)) { 30811cf8e410SMichael S. Tsirkin ret = -EFAULT; 30821cf8e410SMichael S. Tsirkin break; 30831cf8e410SMichael S. Tsirkin } 30841cf8e410SMichael S. Tsirkin if (le) 30851cf8e410SMichael S. Tsirkin tun->flags |= TUN_VNET_LE; 30861cf8e410SMichael S. Tsirkin else 30871cf8e410SMichael S. Tsirkin tun->flags &= ~TUN_VNET_LE; 30881cf8e410SMichael S. Tsirkin break; 30891cf8e410SMichael S. Tsirkin 30908b8e658bSGreg Kurz case TUNGETVNETBE: 30918b8e658bSGreg Kurz ret = tun_get_vnet_be(tun, argp); 30928b8e658bSGreg Kurz break; 30938b8e658bSGreg Kurz 30948b8e658bSGreg Kurz case TUNSETVNETBE: 30958b8e658bSGreg Kurz ret = tun_set_vnet_be(tun, argp); 30968b8e658bSGreg Kurz break; 30978b8e658bSGreg Kurz 309899405162SMichael S. Tsirkin case TUNATTACHFILTER: 309999405162SMichael S. Tsirkin /* Can be set only for TAPs */ 310099405162SMichael S. Tsirkin ret = -EINVAL; 310140630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 310299405162SMichael S. Tsirkin break; 310399405162SMichael S. Tsirkin ret = -EFAULT; 310454f968d6SJason Wang if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog))) 310599405162SMichael S. Tsirkin break; 310699405162SMichael S. Tsirkin 3107c8d68e6bSJason Wang ret = tun_attach_filter(tun); 310899405162SMichael S. Tsirkin break; 310999405162SMichael S. Tsirkin 311099405162SMichael S. Tsirkin case TUNDETACHFILTER: 311199405162SMichael S. Tsirkin /* Can be set only for TAPs */ 311299405162SMichael S. Tsirkin ret = -EINVAL; 311340630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 311499405162SMichael S. Tsirkin break; 3115c8d68e6bSJason Wang ret = 0; 3116c8d68e6bSJason Wang tun_detach_filter(tun, tun->numqueues); 311799405162SMichael S. Tsirkin break; 311899405162SMichael S. Tsirkin 311976975e9cSPavel Emelyanov case TUNGETFILTER: 312076975e9cSPavel Emelyanov ret = -EINVAL; 312140630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 312276975e9cSPavel Emelyanov break; 312376975e9cSPavel Emelyanov ret = -EFAULT; 312476975e9cSPavel Emelyanov if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog))) 312576975e9cSPavel Emelyanov break; 312676975e9cSPavel Emelyanov ret = 0; 312776975e9cSPavel Emelyanov break; 312876975e9cSPavel Emelyanov 312996f84061SJason Wang case TUNSETSTEERINGEBPF: 3130cd5681d7SJason Wang ret = tun_set_ebpf(tun, &tun->steering_prog, argp); 313196f84061SJason Wang break; 313296f84061SJason Wang 3133aff3d70aSJason Wang case TUNSETFILTEREBPF: 3134aff3d70aSJason Wang ret = tun_set_ebpf(tun, &tun->filter_prog, argp); 3135aff3d70aSJason Wang break; 3136aff3d70aSJason Wang 31371da177e4SLinus Torvalds default: 3138631ab46bSEric W. Biederman ret = -EINVAL; 3139631ab46bSEric W. Biederman break; 3140ee289b64SJoe Perches } 31411da177e4SLinus Torvalds 314283c1f36fSSabrina Dubroca if (do_notify) 314383c1f36fSSabrina Dubroca netdev_state_change(tun->dev); 314483c1f36fSSabrina Dubroca 3145876bfd4dSHerbert Xu unlock: 3146876bfd4dSHerbert Xu rtnl_unlock(); 3147876bfd4dSHerbert Xu if (tun) 3148631ab46bSEric W. Biederman tun_put(tun); 3149631ab46bSEric W. Biederman return ret; 31501da177e4SLinus Torvalds } 31511da177e4SLinus Torvalds 315250857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file, 315350857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 315450857e2aSArnd Bergmann { 315550857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq)); 315650857e2aSArnd Bergmann } 315750857e2aSArnd Bergmann 315850857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 315950857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file, 316050857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 316150857e2aSArnd Bergmann { 316250857e2aSArnd Bergmann switch (cmd) { 316350857e2aSArnd Bergmann case TUNSETIFF: 316450857e2aSArnd Bergmann case TUNGETIFF: 316550857e2aSArnd Bergmann case TUNSETTXFILTER: 316650857e2aSArnd Bergmann case TUNGETSNDBUF: 316750857e2aSArnd Bergmann case TUNSETSNDBUF: 316850857e2aSArnd Bergmann case SIOCGIFHWADDR: 316950857e2aSArnd Bergmann case SIOCSIFHWADDR: 317050857e2aSArnd Bergmann arg = (unsigned long)compat_ptr(arg); 317150857e2aSArnd Bergmann break; 317250857e2aSArnd Bergmann default: 317350857e2aSArnd Bergmann arg = (compat_ulong_t)arg; 317450857e2aSArnd Bergmann break; 317550857e2aSArnd Bergmann } 317650857e2aSArnd Bergmann 317750857e2aSArnd Bergmann /* 317850857e2aSArnd Bergmann * compat_ifreq is shorter than ifreq, so we must not access beyond 317950857e2aSArnd Bergmann * the end of that structure. All fields that are used in this 318050857e2aSArnd Bergmann * driver are compatible though, we don't need to convert the 318150857e2aSArnd Bergmann * contents. 318250857e2aSArnd Bergmann */ 318350857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq)); 318450857e2aSArnd Bergmann } 318550857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */ 318650857e2aSArnd Bergmann 31871da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on) 31881da177e4SLinus Torvalds { 318954f968d6SJason Wang struct tun_file *tfile = file->private_data; 31901da177e4SLinus Torvalds int ret; 31911da177e4SLinus Torvalds 319254f968d6SJason Wang if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) 31939d319522SJonathan Corbet goto out; 31941da177e4SLinus Torvalds 31951da177e4SLinus Torvalds if (on) { 3196e0b93eddSJeff Layton __f_setown(file, task_pid(current), PIDTYPE_PID, 0); 319754f968d6SJason Wang tfile->flags |= TUN_FASYNC; 31981da177e4SLinus Torvalds } else 319954f968d6SJason Wang tfile->flags &= ~TUN_FASYNC; 32009d319522SJonathan Corbet ret = 0; 32019d319522SJonathan Corbet out: 32029d319522SJonathan Corbet return ret; 32031da177e4SLinus Torvalds } 32041da177e4SLinus Torvalds 32051da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file) 32061da177e4SLinus Torvalds { 3207140e807dSEric W. Biederman struct net *net = current->nsproxy->net_ns; 3208631ab46bSEric W. Biederman struct tun_file *tfile; 3209deed49fbSThomas Gleixner 32106b8a66eeSJoe Perches DBG1(KERN_INFO, "tunX: tun_chr_open\n"); 3211631ab46bSEric W. Biederman 3212140e807dSEric W. Biederman tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, 321311aa9c28SEric W. Biederman &tun_proto, 0); 3214631ab46bSEric W. Biederman if (!tfile) 3215631ab46bSEric W. Biederman return -ENOMEM; 3216b196d88aSJason Wang if (ptr_ring_init(&tfile->tx_ring, 0, GFP_KERNEL)) { 3217b196d88aSJason Wang sk_free(&tfile->sk); 3218b196d88aSJason Wang return -ENOMEM; 3219b196d88aSJason Wang } 3220b196d88aSJason Wang 3221c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 322254f968d6SJason Wang tfile->flags = 0; 3223fb7589a1SPavel Emelyanov tfile->ifindex = 0; 322454f968d6SJason Wang 322554f968d6SJason Wang init_waitqueue_head(&tfile->wq.wait); 32269e641bdcSXi Wang RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq); 322754f968d6SJason Wang 322854f968d6SJason Wang tfile->socket.file = file; 322954f968d6SJason Wang tfile->socket.ops = &tun_socket_ops; 323054f968d6SJason Wang 323154f968d6SJason Wang sock_init_data(&tfile->socket, &tfile->sk); 323254f968d6SJason Wang 323354f968d6SJason Wang tfile->sk.sk_write_space = tun_sock_write_space; 323454f968d6SJason Wang tfile->sk.sk_sndbuf = INT_MAX; 323554f968d6SJason Wang 3236631ab46bSEric W. Biederman file->private_data = tfile; 32374008e97fSJason Wang INIT_LIST_HEAD(&tfile->next); 323854f968d6SJason Wang 323919a6afb2SJason Wang sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); 324019a6afb2SJason Wang 32411da177e4SLinus Torvalds return 0; 32421da177e4SLinus Torvalds } 32431da177e4SLinus Torvalds 32441da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file) 32451da177e4SLinus Torvalds { 3246631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 32471da177e4SLinus Torvalds 3248c8d68e6bSJason Wang tun_detach(tfile, true); 32491da177e4SLinus Torvalds 32501da177e4SLinus Torvalds return 0; 32511da177e4SLinus Torvalds } 32521da177e4SLinus Torvalds 325393e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS 32549484dc74Syuan linyu static void tun_chr_show_fdinfo(struct seq_file *m, struct file *file) 325593e14b6dSMasatake YAMATO { 32569484dc74Syuan linyu struct tun_file *tfile = file->private_data; 325793e14b6dSMasatake YAMATO struct tun_struct *tun; 325893e14b6dSMasatake YAMATO struct ifreq ifr; 325993e14b6dSMasatake YAMATO 326093e14b6dSMasatake YAMATO memset(&ifr, 0, sizeof(ifr)); 326193e14b6dSMasatake YAMATO 326293e14b6dSMasatake YAMATO rtnl_lock(); 32639484dc74Syuan linyu tun = tun_get(tfile); 326493e14b6dSMasatake YAMATO if (tun) 326593e14b6dSMasatake YAMATO tun_get_iff(current->nsproxy->net_ns, tun, &ifr); 326693e14b6dSMasatake YAMATO rtnl_unlock(); 326793e14b6dSMasatake YAMATO 326893e14b6dSMasatake YAMATO if (tun) 326993e14b6dSMasatake YAMATO tun_put(tun); 327093e14b6dSMasatake YAMATO 3271a3816ab0SJoe Perches seq_printf(m, "iff:\t%s\n", ifr.ifr_name); 327293e14b6dSMasatake YAMATO } 327393e14b6dSMasatake YAMATO #endif 327493e14b6dSMasatake YAMATO 3275d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = { 32761da177e4SLinus Torvalds .owner = THIS_MODULE, 32771da177e4SLinus Torvalds .llseek = no_llseek, 32789b067034SAl Viro .read_iter = tun_chr_read_iter, 3279f5ff53b4SAl Viro .write_iter = tun_chr_write_iter, 32801da177e4SLinus Torvalds .poll = tun_chr_poll, 3281876bfd4dSHerbert Xu .unlocked_ioctl = tun_chr_ioctl, 328250857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 328350857e2aSArnd Bergmann .compat_ioctl = tun_chr_compat_ioctl, 328450857e2aSArnd Bergmann #endif 32851da177e4SLinus Torvalds .open = tun_chr_open, 32861da177e4SLinus Torvalds .release = tun_chr_close, 328793e14b6dSMasatake YAMATO .fasync = tun_chr_fasync, 328893e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS 328993e14b6dSMasatake YAMATO .show_fdinfo = tun_chr_show_fdinfo, 329093e14b6dSMasatake YAMATO #endif 32911da177e4SLinus Torvalds }; 32921da177e4SLinus Torvalds 32931da177e4SLinus Torvalds static struct miscdevice tun_miscdev = { 32941da177e4SLinus Torvalds .minor = TUN_MINOR, 32951da177e4SLinus Torvalds .name = "tun", 3296e454cea2SKay Sievers .nodename = "net/tun", 32971da177e4SLinus Torvalds .fops = &tun_fops, 32981da177e4SLinus Torvalds }; 32991da177e4SLinus Torvalds 33001da177e4SLinus Torvalds /* ethtool interface */ 33011da177e4SLinus Torvalds 330229ccc49dSPhilippe Reynes static int tun_get_link_ksettings(struct net_device *dev, 330329ccc49dSPhilippe Reynes struct ethtool_link_ksettings *cmd) 33041da177e4SLinus Torvalds { 330529ccc49dSPhilippe Reynes ethtool_link_ksettings_zero_link_mode(cmd, supported); 330629ccc49dSPhilippe Reynes ethtool_link_ksettings_zero_link_mode(cmd, advertising); 330729ccc49dSPhilippe Reynes cmd->base.speed = SPEED_10; 330829ccc49dSPhilippe Reynes cmd->base.duplex = DUPLEX_FULL; 330929ccc49dSPhilippe Reynes cmd->base.port = PORT_TP; 331029ccc49dSPhilippe Reynes cmd->base.phy_address = 0; 331129ccc49dSPhilippe Reynes cmd->base.autoneg = AUTONEG_DISABLE; 33121da177e4SLinus Torvalds return 0; 33131da177e4SLinus Torvalds } 33141da177e4SLinus Torvalds 33151da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) 33161da177e4SLinus Torvalds { 33171da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 33181da177e4SLinus Torvalds 331933a5ba14SRick Jones strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); 332033a5ba14SRick Jones strlcpy(info->version, DRV_VERSION, sizeof(info->version)); 33211da177e4SLinus Torvalds 33221da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 332340630b82SMichael S. Tsirkin case IFF_TUN: 332433a5ba14SRick Jones strlcpy(info->bus_info, "tun", sizeof(info->bus_info)); 33251da177e4SLinus Torvalds break; 332640630b82SMichael S. Tsirkin case IFF_TAP: 332733a5ba14SRick Jones strlcpy(info->bus_info, "tap", sizeof(info->bus_info)); 33281da177e4SLinus Torvalds break; 33291da177e4SLinus Torvalds } 33301da177e4SLinus Torvalds } 33311da177e4SLinus Torvalds 33321da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev) 33331da177e4SLinus Torvalds { 33341da177e4SLinus Torvalds #ifdef TUN_DEBUG 33351da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 33361da177e4SLinus Torvalds return tun->debug; 33371da177e4SLinus Torvalds #else 33381da177e4SLinus Torvalds return -EOPNOTSUPP; 33391da177e4SLinus Torvalds #endif 33401da177e4SLinus Torvalds } 33411da177e4SLinus Torvalds 33421da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value) 33431da177e4SLinus Torvalds { 33441da177e4SLinus Torvalds #ifdef TUN_DEBUG 33451da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 33461da177e4SLinus Torvalds tun->debug = value; 33471da177e4SLinus Torvalds #endif 33481da177e4SLinus Torvalds } 33491da177e4SLinus Torvalds 33505503fcecSJason Wang static int tun_get_coalesce(struct net_device *dev, 33515503fcecSJason Wang struct ethtool_coalesce *ec) 33525503fcecSJason Wang { 33535503fcecSJason Wang struct tun_struct *tun = netdev_priv(dev); 33545503fcecSJason Wang 33555503fcecSJason Wang ec->rx_max_coalesced_frames = tun->rx_batched; 33565503fcecSJason Wang 33575503fcecSJason Wang return 0; 33585503fcecSJason Wang } 33595503fcecSJason Wang 33605503fcecSJason Wang static int tun_set_coalesce(struct net_device *dev, 33615503fcecSJason Wang struct ethtool_coalesce *ec) 33625503fcecSJason Wang { 33635503fcecSJason Wang struct tun_struct *tun = netdev_priv(dev); 33645503fcecSJason Wang 33655503fcecSJason Wang if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT) 33665503fcecSJason Wang tun->rx_batched = NAPI_POLL_WEIGHT; 33675503fcecSJason Wang else 33685503fcecSJason Wang tun->rx_batched = ec->rx_max_coalesced_frames; 33695503fcecSJason Wang 33705503fcecSJason Wang return 0; 33715503fcecSJason Wang } 33725503fcecSJason Wang 33737282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = { 33741da177e4SLinus Torvalds .get_drvinfo = tun_get_drvinfo, 33751da177e4SLinus Torvalds .get_msglevel = tun_get_msglevel, 33761da177e4SLinus Torvalds .set_msglevel = tun_set_msglevel, 3377bee31369SNolan Leake .get_link = ethtool_op_get_link, 3378eda29772SRichard Cochran .get_ts_info = ethtool_op_get_ts_info, 33795503fcecSJason Wang .get_coalesce = tun_get_coalesce, 33805503fcecSJason Wang .set_coalesce = tun_set_coalesce, 338129ccc49dSPhilippe Reynes .get_link_ksettings = tun_get_link_ksettings, 33821da177e4SLinus Torvalds }; 33831da177e4SLinus Torvalds 33841576d986SJason Wang static int tun_queue_resize(struct tun_struct *tun) 33851576d986SJason Wang { 33861576d986SJason Wang struct net_device *dev = tun->dev; 33871576d986SJason Wang struct tun_file *tfile; 33885990a305SJason Wang struct ptr_ring **rings; 33891576d986SJason Wang int n = tun->numqueues + tun->numdisabled; 33901576d986SJason Wang int ret, i; 33911576d986SJason Wang 33925990a305SJason Wang rings = kmalloc_array(n, sizeof(*rings), GFP_KERNEL); 33935990a305SJason Wang if (!rings) 33941576d986SJason Wang return -ENOMEM; 33951576d986SJason Wang 33961576d986SJason Wang for (i = 0; i < tun->numqueues; i++) { 33971576d986SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 33985990a305SJason Wang rings[i] = &tfile->tx_ring; 33991576d986SJason Wang } 34001576d986SJason Wang list_for_each_entry(tfile, &tun->disabled, next) 34015990a305SJason Wang rings[i++] = &tfile->tx_ring; 34021576d986SJason Wang 34035990a305SJason Wang ret = ptr_ring_resize_multiple(rings, n, 34045990a305SJason Wang dev->tx_queue_len, GFP_KERNEL, 3405fc72d1d5SJason Wang tun_ptr_free); 34061576d986SJason Wang 34075990a305SJason Wang kfree(rings); 34081576d986SJason Wang return ret; 34091576d986SJason Wang } 34101576d986SJason Wang 34111576d986SJason Wang static int tun_device_event(struct notifier_block *unused, 34121576d986SJason Wang unsigned long event, void *ptr) 34131576d986SJason Wang { 34141576d986SJason Wang struct net_device *dev = netdev_notifier_info_to_dev(ptr); 34151576d986SJason Wang struct tun_struct *tun = netdev_priv(dev); 34161576d986SJason Wang 341786dfb4acSCraig Gallek if (dev->rtnl_link_ops != &tun_link_ops) 341886dfb4acSCraig Gallek return NOTIFY_DONE; 341986dfb4acSCraig Gallek 34201576d986SJason Wang switch (event) { 34211576d986SJason Wang case NETDEV_CHANGE_TX_QUEUE_LEN: 34221576d986SJason Wang if (tun_queue_resize(tun)) 34231576d986SJason Wang return NOTIFY_BAD; 34241576d986SJason Wang break; 34251576d986SJason Wang default: 34261576d986SJason Wang break; 34271576d986SJason Wang } 34281576d986SJason Wang 34291576d986SJason Wang return NOTIFY_DONE; 34301576d986SJason Wang } 34311576d986SJason Wang 34321576d986SJason Wang static struct notifier_block tun_notifier_block __read_mostly = { 34331576d986SJason Wang .notifier_call = tun_device_event, 34341576d986SJason Wang }; 343579d17604SPavel Emelyanov 34361da177e4SLinus Torvalds static int __init tun_init(void) 34371da177e4SLinus Torvalds { 34381da177e4SLinus Torvalds int ret = 0; 34391da177e4SLinus Torvalds 34406b8a66eeSJoe Perches pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION); 34411da177e4SLinus Torvalds 3442f019a7a5SEric W. Biederman ret = rtnl_link_register(&tun_link_ops); 344379d17604SPavel Emelyanov if (ret) { 34446b8a66eeSJoe Perches pr_err("Can't register link_ops\n"); 3445f019a7a5SEric W. Biederman goto err_linkops; 344679d17604SPavel Emelyanov } 344779d17604SPavel Emelyanov 34481da177e4SLinus Torvalds ret = misc_register(&tun_miscdev); 344979d17604SPavel Emelyanov if (ret) { 34506b8a66eeSJoe Perches pr_err("Can't register misc device %d\n", TUN_MINOR); 345179d17604SPavel Emelyanov goto err_misc; 345279d17604SPavel Emelyanov } 34531576d986SJason Wang 34545edfbd3cSTonghao Zhang ret = register_netdevice_notifier(&tun_notifier_block); 34555edfbd3cSTonghao Zhang if (ret) { 34565edfbd3cSTonghao Zhang pr_err("Can't register netdevice notifier\n"); 34575edfbd3cSTonghao Zhang goto err_notifier; 34585edfbd3cSTonghao Zhang } 34595edfbd3cSTonghao Zhang 346079d17604SPavel Emelyanov return 0; 34615edfbd3cSTonghao Zhang 34625edfbd3cSTonghao Zhang err_notifier: 34635edfbd3cSTonghao Zhang misc_deregister(&tun_miscdev); 346479d17604SPavel Emelyanov err_misc: 3465f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 3466f019a7a5SEric W. Biederman err_linkops: 34671da177e4SLinus Torvalds return ret; 34681da177e4SLinus Torvalds } 34691da177e4SLinus Torvalds 34701da177e4SLinus Torvalds static void tun_cleanup(void) 34711da177e4SLinus Torvalds { 34721da177e4SLinus Torvalds misc_deregister(&tun_miscdev); 3473f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 34741576d986SJason Wang unregister_netdevice_notifier(&tun_notifier_block); 34751da177e4SLinus Torvalds } 34761da177e4SLinus Torvalds 347705c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file. Returns error unless file is 347805c2828cSMichael S. Tsirkin * attached to a device. The returned object works like a packet socket, it 347905c2828cSMichael S. Tsirkin * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for 348005c2828cSMichael S. Tsirkin * holding a reference to the file for as long as the socket is in use. */ 348105c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file) 348205c2828cSMichael S. Tsirkin { 34836e914fc7SJason Wang struct tun_file *tfile; 348405c2828cSMichael S. Tsirkin if (file->f_op != &tun_fops) 348505c2828cSMichael S. Tsirkin return ERR_PTR(-EINVAL); 34866e914fc7SJason Wang tfile = file->private_data; 34876e914fc7SJason Wang if (!tfile) 348805c2828cSMichael S. Tsirkin return ERR_PTR(-EBADFD); 348954f968d6SJason Wang return &tfile->socket; 349005c2828cSMichael S. Tsirkin } 349105c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket); 349205c2828cSMichael S. Tsirkin 34935990a305SJason Wang struct ptr_ring *tun_get_tx_ring(struct file *file) 349483339c6bSJason Wang { 349583339c6bSJason Wang struct tun_file *tfile; 349683339c6bSJason Wang 349783339c6bSJason Wang if (file->f_op != &tun_fops) 349883339c6bSJason Wang return ERR_PTR(-EINVAL); 349983339c6bSJason Wang tfile = file->private_data; 350083339c6bSJason Wang if (!tfile) 350183339c6bSJason Wang return ERR_PTR(-EBADFD); 35025990a305SJason Wang return &tfile->tx_ring; 350383339c6bSJason Wang } 35045990a305SJason Wang EXPORT_SYMBOL_GPL(tun_get_tx_ring); 350583339c6bSJason Wang 35061da177e4SLinus Torvalds module_init(tun_init); 35071da177e4SLinus Torvalds module_exit(tun_cleanup); 35081da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION); 35091da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT); 35101da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 35111da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR); 3512578454ffSKay Sievers MODULE_ALIAS("devname:net/tun"); 3513