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> 73735fc405SJesper Dangaard Brouer #include <net/xdp.h> 7493e14b6dSMasatake YAMATO #include <linux/seq_file.h> 75e0b46d0eSHerbert Xu #include <linux/uio.h> 761576d986SJason Wang #include <linux/skb_array.h> 77761876c8SJason Wang #include <linux/bpf.h> 78761876c8SJason Wang #include <linux/bpf_trace.h> 7990e33d45SPetar Penkov #include <linux/mutex.h> 801da177e4SLinus Torvalds 817c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 82f2780d6dSKirill Tkhai #include <linux/proc_fs.h> 831da177e4SLinus Torvalds 844e24f2ddSChas Williams static void tun_default_link_ksettings(struct net_device *dev, 854e24f2ddSChas Williams struct ethtool_link_ksettings *cmd); 864e24f2ddSChas Williams 8714daa021SRusty Russell /* Uncomment to enable debugging */ 8814daa021SRusty Russell /* #define TUN_DEBUG 1 */ 8914daa021SRusty Russell 901da177e4SLinus Torvalds #ifdef TUN_DEBUG 911da177e4SLinus Torvalds static int debug; 9214daa021SRusty Russell 936b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...) \ 946b8a66eeSJoe Perches do { \ 956b8a66eeSJoe Perches if (tun->debug) \ 966b8a66eeSJoe Perches netdev_printk(level, tun->dev, fmt, ##args); \ 976b8a66eeSJoe Perches } while (0) 986b8a66eeSJoe Perches #define DBG1(level, fmt, args...) \ 996b8a66eeSJoe Perches do { \ 1006b8a66eeSJoe Perches if (debug == 2) \ 1016b8a66eeSJoe Perches printk(level fmt, ##args); \ 1026b8a66eeSJoe Perches } while (0) 10314daa021SRusty Russell #else 1046b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...) \ 1056b8a66eeSJoe Perches do { \ 1066b8a66eeSJoe Perches if (0) \ 1076b8a66eeSJoe Perches netdev_printk(level, tun->dev, fmt, ##args); \ 1086b8a66eeSJoe Perches } while (0) 1096b8a66eeSJoe Perches #define DBG1(level, fmt, args...) \ 1106b8a66eeSJoe Perches do { \ 1116b8a66eeSJoe Perches if (0) \ 1126b8a66eeSJoe Perches printk(level fmt, ##args); \ 1136b8a66eeSJoe Perches } while (0) 1141da177e4SLinus Torvalds #endif 1151da177e4SLinus Torvalds 1167df13219SJason Wang #define TUN_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD) 11766ccbc9cSJason Wang 118031f5e03SMichael S. Tsirkin /* TUN device flags */ 119031f5e03SMichael S. Tsirkin 120031f5e03SMichael S. Tsirkin /* IFF_ATTACH_QUEUE is never stored in device flags, 121031f5e03SMichael S. Tsirkin * overload it to mean fasync when stored there. 122031f5e03SMichael S. Tsirkin */ 123031f5e03SMichael S. Tsirkin #define TUN_FASYNC IFF_ATTACH_QUEUE 1241cf8e410SMichael S. Tsirkin /* High bits in flags field are unused. */ 1251cf8e410SMichael S. Tsirkin #define TUN_VNET_LE 0x80000000 1268b8e658bSGreg Kurz #define TUN_VNET_BE 0x40000000 127031f5e03SMichael S. Tsirkin 128031f5e03SMichael S. Tsirkin #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \ 12990e33d45SPetar Penkov IFF_MULTI_QUEUE | IFF_NAPI | IFF_NAPI_FRAGS) 13090e33d45SPetar Penkov 1310690899bSMichael S. Tsirkin #define GOODCOPY_LEN 128 1320690899bSMichael S. Tsirkin 133f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8 134f271b2ccSMax Krasnyansky struct tap_filter { 135f271b2ccSMax Krasnyansky unsigned int count; /* Number of addrs. Zero means disabled */ 136f271b2ccSMax Krasnyansky u32 mask[2]; /* Mask of the hashed addrs */ 137f271b2ccSMax Krasnyansky unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN]; 138f271b2ccSMax Krasnyansky }; 139f271b2ccSMax Krasnyansky 140baf71c5cSPankaj Gupta /* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal 141baf71c5cSPankaj Gupta * to max number of VCPUs in guest. */ 142baf71c5cSPankaj Gupta #define MAX_TAP_QUEUES 256 143b8732fb7SJason Wang #define MAX_TAP_FLOWS 4096 144c8d68e6bSJason Wang 14596442e42SJason Wang #define TUN_FLOW_EXPIRE (3 * HZ) 14696442e42SJason Wang 147608b9977SPaolo Abeni struct tun_pcpu_stats { 148608b9977SPaolo Abeni u64 rx_packets; 149608b9977SPaolo Abeni u64 rx_bytes; 150608b9977SPaolo Abeni u64 tx_packets; 151608b9977SPaolo Abeni u64 tx_bytes; 152608b9977SPaolo Abeni struct u64_stats_sync syncp; 153608b9977SPaolo Abeni u32 rx_dropped; 154608b9977SPaolo Abeni u32 tx_dropped; 155608b9977SPaolo Abeni u32 rx_frame_errors; 156608b9977SPaolo Abeni }; 157608b9977SPaolo Abeni 15854f968d6SJason Wang /* A tun_file connects an open character device to a tuntap netdevice. It 15992d4ea6eSstephen hemminger * also contains all socket related structures (except sock_fprog and tap_filter) 16054f968d6SJason Wang * to serve as one transmit queue for tuntap device. The sock_fprog and 16154f968d6SJason Wang * tap_filter were kept in tun_struct since they were used for filtering for the 16236fe8c09SRami Rosen * netdevice not for a specific queue (at least I didn't see the requirement for 16354f968d6SJason Wang * this). 1646e914fc7SJason Wang * 1656e914fc7SJason Wang * RCU usage: 16636fe8c09SRami Rosen * The tun_file and tun_struct are loosely coupled, the pointer from one to the 1676e914fc7SJason Wang * other can only be read while rcu_read_lock or rtnl_lock is held. 16854f968d6SJason Wang */ 169631ab46bSEric W. Biederman struct tun_file { 17054f968d6SJason Wang struct sock sk; 17154f968d6SJason Wang struct socket socket; 17254f968d6SJason Wang struct socket_wq wq; 1736e914fc7SJason Wang struct tun_struct __rcu *tun; 17454f968d6SJason Wang struct fasync_struct *fasync; 17554f968d6SJason Wang /* only used for fasnyc */ 17654f968d6SJason Wang unsigned int flags; 177fb7589a1SPavel Emelyanov union { 178c8d68e6bSJason Wang u16 queue_index; 179fb7589a1SPavel Emelyanov unsigned int ifindex; 180fb7589a1SPavel Emelyanov }; 18194317099SPetar Penkov struct napi_struct napi; 182aec72f33SEric Dumazet bool napi_enabled; 183af3fb24eSEric Dumazet bool napi_frags_enabled; 18490e33d45SPetar Penkov struct mutex napi_mutex; /* Protects access to the above napi */ 1854008e97fSJason Wang struct list_head next; 1864008e97fSJason Wang struct tun_struct *detached; 1875990a305SJason Wang struct ptr_ring tx_ring; 1888bf5c4eeSJesper Dangaard Brouer struct xdp_rxq_info xdp_rxq; 189631ab46bSEric W. Biederman }; 190631ab46bSEric W. Biederman 191f9e06c45SJason Wang struct tun_page { 192f9e06c45SJason Wang struct page *page; 193f9e06c45SJason Wang int count; 194f9e06c45SJason Wang }; 195f9e06c45SJason Wang 19696442e42SJason Wang struct tun_flow_entry { 19796442e42SJason Wang struct hlist_node hash_link; 19896442e42SJason Wang struct rcu_head rcu; 19996442e42SJason Wang struct tun_struct *tun; 20096442e42SJason Wang 20196442e42SJason Wang u32 rxhash; 2029bc88939STom Herbert u32 rps_rxhash; 20396442e42SJason Wang int queue_index; 20483b1bc12SLi RongQing unsigned long updated ____cacheline_aligned_in_smp; 20596442e42SJason Wang }; 20696442e42SJason Wang 20796442e42SJason Wang #define TUN_NUM_FLOW_ENTRIES 1024 208f13b5468SLi RongQing #define TUN_MASK_FLOW_ENTRIES (TUN_NUM_FLOW_ENTRIES - 1) 20996442e42SJason Wang 210cd5681d7SJason Wang struct tun_prog { 21196f84061SJason Wang struct rcu_head rcu; 21296f84061SJason Wang struct bpf_prog *prog; 21396f84061SJason Wang }; 21496f84061SJason Wang 21554f968d6SJason Wang /* Since the socket were moved to tun_file, to preserve the behavior of persist 21636fe8c09SRami Rosen * device, socket filter, sndbuf and vnet header size were restore when the 21754f968d6SJason Wang * file were attached to a persist device. 21854f968d6SJason Wang */ 21914daa021SRusty Russell struct tun_struct { 220c8d68e6bSJason Wang struct tun_file __rcu *tfiles[MAX_TAP_QUEUES]; 221c8d68e6bSJason Wang unsigned int numqueues; 222f271b2ccSMax Krasnyansky unsigned int flags; 2230625c883SEric W. Biederman kuid_t owner; 2240625c883SEric W. Biederman kgid_t group; 22514daa021SRusty Russell 22614daa021SRusty Russell struct net_device *dev; 227c8f44affSMichał Mirosław netdev_features_t set_features; 22888255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \ 229d591a1f3SDavid S. Miller NETIF_F_TSO6) 230d9d52b51SMichael S. Tsirkin 231eaea34b2SPaolo Abeni int align; 232d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 23354f968d6SJason Wang int sndbuf; 23454f968d6SJason Wang struct tap_filter txflt; 23554f968d6SJason Wang struct sock_fprog fprog; 23654f968d6SJason Wang /* protected by rtnl lock */ 23754f968d6SJason Wang bool filter_attached; 23814daa021SRusty Russell #ifdef TUN_DEBUG 23914daa021SRusty Russell int debug; 24014daa021SRusty Russell #endif 24196442e42SJason Wang spinlock_t lock; 24296442e42SJason Wang struct hlist_head flows[TUN_NUM_FLOW_ENTRIES]; 24396442e42SJason Wang struct timer_list flow_gc_timer; 24496442e42SJason Wang unsigned long ageing_time; 2454008e97fSJason Wang unsigned int numdisabled; 2464008e97fSJason Wang struct list_head disabled; 2475dbbaf2dSPaul Moore void *security; 248b8732fb7SJason Wang u32 flow_count; 2495503fcecSJason Wang u32 rx_batched; 250608b9977SPaolo Abeni struct tun_pcpu_stats __percpu *pcpu_stats; 251761876c8SJason Wang struct bpf_prog __rcu *xdp_prog; 252cd5681d7SJason Wang struct tun_prog __rcu *steering_prog; 253aff3d70aSJason Wang struct tun_prog __rcu *filter_prog; 2544e24f2ddSChas Williams struct ethtool_link_ksettings link_ksettings; 25514daa021SRusty Russell }; 25614daa021SRusty Russell 257aff3d70aSJason Wang struct veth { 258aff3d70aSJason Wang __be16 h_vlan_proto; 259aff3d70aSJason Wang __be16 h_vlan_TCI; 2601da177e4SLinus Torvalds }; 2611da177e4SLinus Torvalds 2621ffcbc85SJesper Dangaard Brouer bool tun_is_xdp_frame(void *ptr) 263fc72d1d5SJason Wang { 264fc72d1d5SJason Wang return (unsigned long)ptr & TUN_XDP_FLAG; 265fc72d1d5SJason Wang } 2661ffcbc85SJesper Dangaard Brouer EXPORT_SYMBOL(tun_is_xdp_frame); 267fc72d1d5SJason Wang 268fc72d1d5SJason Wang void *tun_xdp_to_ptr(void *ptr) 269fc72d1d5SJason Wang { 270fc72d1d5SJason Wang return (void *)((unsigned long)ptr | TUN_XDP_FLAG); 271fc72d1d5SJason Wang } 272fc72d1d5SJason Wang EXPORT_SYMBOL(tun_xdp_to_ptr); 273fc72d1d5SJason Wang 274fc72d1d5SJason Wang void *tun_ptr_to_xdp(void *ptr) 275fc72d1d5SJason Wang { 276fc72d1d5SJason Wang return (void *)((unsigned long)ptr & ~TUN_XDP_FLAG); 277fc72d1d5SJason Wang } 278fc72d1d5SJason Wang EXPORT_SYMBOL(tun_ptr_to_xdp); 279fc72d1d5SJason Wang 28094317099SPetar Penkov static int tun_napi_receive(struct napi_struct *napi, int budget) 28194317099SPetar Penkov { 28294317099SPetar Penkov struct tun_file *tfile = container_of(napi, struct tun_file, napi); 28394317099SPetar Penkov struct sk_buff_head *queue = &tfile->sk.sk_write_queue; 28494317099SPetar Penkov struct sk_buff_head process_queue; 28594317099SPetar Penkov struct sk_buff *skb; 28694317099SPetar Penkov int received = 0; 28794317099SPetar Penkov 28894317099SPetar Penkov __skb_queue_head_init(&process_queue); 28994317099SPetar Penkov 29094317099SPetar Penkov spin_lock(&queue->lock); 29194317099SPetar Penkov skb_queue_splice_tail_init(queue, &process_queue); 29294317099SPetar Penkov spin_unlock(&queue->lock); 29394317099SPetar Penkov 29494317099SPetar Penkov while (received < budget && (skb = __skb_dequeue(&process_queue))) { 29594317099SPetar Penkov napi_gro_receive(napi, skb); 29694317099SPetar Penkov ++received; 29794317099SPetar Penkov } 29894317099SPetar Penkov 29994317099SPetar Penkov if (!skb_queue_empty(&process_queue)) { 30094317099SPetar Penkov spin_lock(&queue->lock); 30194317099SPetar Penkov skb_queue_splice(&process_queue, queue); 30294317099SPetar Penkov spin_unlock(&queue->lock); 30394317099SPetar Penkov } 30494317099SPetar Penkov 30594317099SPetar Penkov return received; 30694317099SPetar Penkov } 30794317099SPetar Penkov 30894317099SPetar Penkov static int tun_napi_poll(struct napi_struct *napi, int budget) 30994317099SPetar Penkov { 31094317099SPetar Penkov unsigned int received; 31194317099SPetar Penkov 31294317099SPetar Penkov received = tun_napi_receive(napi, budget); 31394317099SPetar Penkov 31494317099SPetar Penkov if (received < budget) 31594317099SPetar Penkov napi_complete_done(napi, received); 31694317099SPetar Penkov 31794317099SPetar Penkov return received; 31894317099SPetar Penkov } 31994317099SPetar Penkov 32094317099SPetar Penkov static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile, 321af3fb24eSEric Dumazet bool napi_en, bool napi_frags) 32294317099SPetar Penkov { 323aec72f33SEric Dumazet tfile->napi_enabled = napi_en; 324af3fb24eSEric Dumazet tfile->napi_frags_enabled = napi_en && napi_frags; 32594317099SPetar Penkov if (napi_en) { 32694317099SPetar Penkov netif_napi_add(tun->dev, &tfile->napi, tun_napi_poll, 32794317099SPetar Penkov NAPI_POLL_WEIGHT); 32894317099SPetar Penkov napi_enable(&tfile->napi); 32994317099SPetar Penkov } 33094317099SPetar Penkov } 33194317099SPetar Penkov 33206e55addSEric Dumazet static void tun_napi_disable(struct tun_file *tfile) 33394317099SPetar Penkov { 334aec72f33SEric Dumazet if (tfile->napi_enabled) 33594317099SPetar Penkov napi_disable(&tfile->napi); 33694317099SPetar Penkov } 33794317099SPetar Penkov 33806e55addSEric Dumazet static void tun_napi_del(struct tun_file *tfile) 33994317099SPetar Penkov { 340aec72f33SEric Dumazet if (tfile->napi_enabled) 34194317099SPetar Penkov netif_napi_del(&tfile->napi); 34294317099SPetar Penkov } 34394317099SPetar Penkov 344af3fb24eSEric Dumazet static bool tun_napi_frags_enabled(const struct tun_file *tfile) 34590e33d45SPetar Penkov { 346af3fb24eSEric Dumazet return tfile->napi_frags_enabled; 34790e33d45SPetar Penkov } 34890e33d45SPetar Penkov 3498b8e658bSGreg Kurz #ifdef CONFIG_TUN_VNET_CROSS_LE 3508b8e658bSGreg Kurz static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) 3518b8e658bSGreg Kurz { 3528b8e658bSGreg Kurz return tun->flags & TUN_VNET_BE ? false : 3538b8e658bSGreg Kurz virtio_legacy_is_little_endian(); 3548b8e658bSGreg Kurz } 3558b8e658bSGreg Kurz 3568b8e658bSGreg Kurz static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) 3578b8e658bSGreg Kurz { 3588b8e658bSGreg Kurz int be = !!(tun->flags & TUN_VNET_BE); 3598b8e658bSGreg Kurz 3608b8e658bSGreg Kurz if (put_user(be, argp)) 3618b8e658bSGreg Kurz return -EFAULT; 3628b8e658bSGreg Kurz 3638b8e658bSGreg Kurz return 0; 3648b8e658bSGreg Kurz } 3658b8e658bSGreg Kurz 3668b8e658bSGreg Kurz static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) 3678b8e658bSGreg Kurz { 3688b8e658bSGreg Kurz int be; 3698b8e658bSGreg Kurz 3708b8e658bSGreg Kurz if (get_user(be, argp)) 3718b8e658bSGreg Kurz return -EFAULT; 3728b8e658bSGreg Kurz 3738b8e658bSGreg Kurz if (be) 3748b8e658bSGreg Kurz tun->flags |= TUN_VNET_BE; 3758b8e658bSGreg Kurz else 3768b8e658bSGreg Kurz tun->flags &= ~TUN_VNET_BE; 3778b8e658bSGreg Kurz 3788b8e658bSGreg Kurz return 0; 3798b8e658bSGreg Kurz } 3808b8e658bSGreg Kurz #else 3818b8e658bSGreg Kurz static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) 3828b8e658bSGreg Kurz { 3838b8e658bSGreg Kurz return virtio_legacy_is_little_endian(); 3848b8e658bSGreg Kurz } 3858b8e658bSGreg Kurz 3868b8e658bSGreg Kurz static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) 3878b8e658bSGreg Kurz { 3888b8e658bSGreg Kurz return -EINVAL; 3898b8e658bSGreg Kurz } 3908b8e658bSGreg Kurz 3918b8e658bSGreg Kurz static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) 3928b8e658bSGreg Kurz { 3938b8e658bSGreg Kurz return -EINVAL; 3948b8e658bSGreg Kurz } 3958b8e658bSGreg Kurz #endif /* CONFIG_TUN_VNET_CROSS_LE */ 3968b8e658bSGreg Kurz 39725bd55bbSGreg Kurz static inline bool tun_is_little_endian(struct tun_struct *tun) 39825bd55bbSGreg Kurz { 3997d824109SGreg Kurz return tun->flags & TUN_VNET_LE || 4008b8e658bSGreg Kurz tun_legacy_is_little_endian(tun); 40125bd55bbSGreg Kurz } 40225bd55bbSGreg Kurz 40356f0dcc5SMichael S. Tsirkin static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val) 40456f0dcc5SMichael S. Tsirkin { 40525bd55bbSGreg Kurz return __virtio16_to_cpu(tun_is_little_endian(tun), val); 40656f0dcc5SMichael S. Tsirkin } 40756f0dcc5SMichael S. Tsirkin 40856f0dcc5SMichael S. Tsirkin static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val) 40956f0dcc5SMichael S. Tsirkin { 41025bd55bbSGreg Kurz return __cpu_to_virtio16(tun_is_little_endian(tun), val); 41156f0dcc5SMichael S. Tsirkin } 41256f0dcc5SMichael S. Tsirkin 41396442e42SJason Wang static inline u32 tun_hashfn(u32 rxhash) 41496442e42SJason Wang { 415f13b5468SLi RongQing return rxhash & TUN_MASK_FLOW_ENTRIES; 41696442e42SJason Wang } 41796442e42SJason Wang 41896442e42SJason Wang static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash) 41996442e42SJason Wang { 42096442e42SJason Wang struct tun_flow_entry *e; 42196442e42SJason Wang 422b67bfe0dSSasha Levin hlist_for_each_entry_rcu(e, head, hash_link) { 42396442e42SJason Wang if (e->rxhash == rxhash) 42496442e42SJason Wang return e; 42596442e42SJason Wang } 42696442e42SJason Wang return NULL; 42796442e42SJason Wang } 42896442e42SJason Wang 42996442e42SJason Wang static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun, 43096442e42SJason Wang struct hlist_head *head, 43196442e42SJason Wang u32 rxhash, u16 queue_index) 43296442e42SJason Wang { 4339fdc6befSEric Dumazet struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC); 4349fdc6befSEric Dumazet 43596442e42SJason Wang if (e) { 43696442e42SJason Wang tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n", 43796442e42SJason Wang rxhash, queue_index); 43896442e42SJason Wang e->updated = jiffies; 43996442e42SJason Wang e->rxhash = rxhash; 4409bc88939STom Herbert e->rps_rxhash = 0; 44196442e42SJason Wang e->queue_index = queue_index; 44296442e42SJason Wang e->tun = tun; 44396442e42SJason Wang hlist_add_head_rcu(&e->hash_link, head); 444b8732fb7SJason Wang ++tun->flow_count; 44596442e42SJason Wang } 44696442e42SJason Wang return e; 44796442e42SJason Wang } 44896442e42SJason Wang 44996442e42SJason Wang static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e) 45096442e42SJason Wang { 45196442e42SJason Wang tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n", 45296442e42SJason Wang e->rxhash, e->queue_index); 45396442e42SJason Wang hlist_del_rcu(&e->hash_link); 4549fdc6befSEric Dumazet kfree_rcu(e, rcu); 455b8732fb7SJason Wang --tun->flow_count; 45696442e42SJason Wang } 45796442e42SJason Wang 45896442e42SJason Wang static void tun_flow_flush(struct tun_struct *tun) 45996442e42SJason Wang { 46096442e42SJason Wang int i; 46196442e42SJason Wang 46296442e42SJason Wang spin_lock_bh(&tun->lock); 46396442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 46496442e42SJason Wang struct tun_flow_entry *e; 465b67bfe0dSSasha Levin struct hlist_node *n; 46696442e42SJason Wang 467b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) 46896442e42SJason Wang tun_flow_delete(tun, e); 46996442e42SJason Wang } 47096442e42SJason Wang spin_unlock_bh(&tun->lock); 47196442e42SJason Wang } 47296442e42SJason Wang 47396442e42SJason Wang static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index) 47496442e42SJason Wang { 47596442e42SJason Wang int i; 47696442e42SJason Wang 47796442e42SJason Wang spin_lock_bh(&tun->lock); 47896442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 47996442e42SJason Wang struct tun_flow_entry *e; 480b67bfe0dSSasha Levin struct hlist_node *n; 48196442e42SJason Wang 482b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { 48396442e42SJason Wang if (e->queue_index == queue_index) 48496442e42SJason Wang tun_flow_delete(tun, e); 48596442e42SJason Wang } 48696442e42SJason Wang } 48796442e42SJason Wang spin_unlock_bh(&tun->lock); 48896442e42SJason Wang } 48996442e42SJason Wang 490e99e88a9SKees Cook static void tun_flow_cleanup(struct timer_list *t) 49196442e42SJason Wang { 492e99e88a9SKees Cook struct tun_struct *tun = from_timer(tun, t, flow_gc_timer); 49396442e42SJason Wang unsigned long delay = tun->ageing_time; 49496442e42SJason Wang unsigned long next_timer = jiffies + delay; 49596442e42SJason Wang unsigned long count = 0; 49696442e42SJason Wang int i; 49796442e42SJason Wang 49896442e42SJason Wang tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n"); 49996442e42SJason Wang 5007dbfb4efSEric Dumazet spin_lock(&tun->lock); 50196442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 50296442e42SJason Wang struct tun_flow_entry *e; 503b67bfe0dSSasha Levin struct hlist_node *n; 50496442e42SJason Wang 505b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { 50696442e42SJason Wang unsigned long this_timer; 50781d98fa4SEric Dumazet 50896442e42SJason Wang this_timer = e->updated + delay; 50981d98fa4SEric Dumazet if (time_before_eq(this_timer, jiffies)) { 51096442e42SJason Wang tun_flow_delete(tun, e); 51181d98fa4SEric Dumazet continue; 51281d98fa4SEric Dumazet } 51381d98fa4SEric Dumazet count++; 51481d98fa4SEric Dumazet if (time_before(this_timer, next_timer)) 51596442e42SJason Wang next_timer = this_timer; 51696442e42SJason Wang } 51796442e42SJason Wang } 51896442e42SJason Wang 51996442e42SJason Wang if (count) 52096442e42SJason Wang mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer)); 5217dbfb4efSEric Dumazet spin_unlock(&tun->lock); 52296442e42SJason Wang } 52396442e42SJason Wang 52449974420SEric Dumazet static void tun_flow_update(struct tun_struct *tun, u32 rxhash, 5259e85722dSJason Wang struct tun_file *tfile) 52696442e42SJason Wang { 52796442e42SJason Wang struct hlist_head *head; 52896442e42SJason Wang struct tun_flow_entry *e; 52996442e42SJason Wang unsigned long delay = tun->ageing_time; 5309e85722dSJason Wang u16 queue_index = tfile->queue_index; 53196442e42SJason Wang 53296442e42SJason Wang head = &tun->flows[tun_hashfn(rxhash)]; 53396442e42SJason Wang 53496442e42SJason Wang rcu_read_lock(); 53596442e42SJason Wang 53696442e42SJason Wang e = tun_flow_find(head, rxhash); 53796442e42SJason Wang if (likely(e)) { 53896442e42SJason Wang /* TODO: keep queueing to old queue until it's empty? */ 53983b1bc12SLi RongQing if (e->queue_index != queue_index) 54096442e42SJason Wang e->queue_index = queue_index; 54183b1bc12SLi RongQing if (e->updated != jiffies) 54296442e42SJason Wang e->updated = jiffies; 5439bc88939STom Herbert sock_rps_record_flow_hash(e->rps_rxhash); 54496442e42SJason Wang } else { 54596442e42SJason Wang spin_lock_bh(&tun->lock); 546b8732fb7SJason Wang if (!tun_flow_find(head, rxhash) && 547b8732fb7SJason Wang tun->flow_count < MAX_TAP_FLOWS) 54896442e42SJason Wang tun_flow_create(tun, head, rxhash, queue_index); 54996442e42SJason Wang 55096442e42SJason Wang if (!timer_pending(&tun->flow_gc_timer)) 55196442e42SJason Wang mod_timer(&tun->flow_gc_timer, 55296442e42SJason Wang round_jiffies_up(jiffies + delay)); 55396442e42SJason Wang spin_unlock_bh(&tun->lock); 55496442e42SJason Wang } 55596442e42SJason Wang 55696442e42SJason Wang rcu_read_unlock(); 55796442e42SJason Wang } 55896442e42SJason Wang 5599bc88939STom Herbert /** 5609bc88939STom Herbert * Save the hash received in the stack receive path and update the 5619bc88939STom Herbert * flow_hash table accordingly. 5629bc88939STom Herbert */ 5639bc88939STom Herbert static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash) 5649bc88939STom Herbert { 565567e4b79SEric Dumazet if (unlikely(e->rps_rxhash != hash)) 5669bc88939STom Herbert e->rps_rxhash = hash; 5679bc88939STom Herbert } 5689bc88939STom Herbert 5694b035271SWang Li /* We try to identify a flow through its rxhash. The reason that 57092d4ea6eSstephen hemminger * we do not check rxq no. is because some cards(e.g 82599), chooses 571c8d68e6bSJason Wang * the rxq based on the txq where the last packet of the flow comes. As 572c8d68e6bSJason Wang * the userspace application move between processors, we may get a 5734b035271SWang Li * different rxq no. here. 574c8d68e6bSJason Wang */ 57596f84061SJason Wang static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb) 576c8d68e6bSJason Wang { 57796442e42SJason Wang struct tun_flow_entry *e; 578c8d68e6bSJason Wang u32 txq = 0; 579c8d68e6bSJason Wang u32 numqueues = 0; 580c8d68e6bSJason Wang 5816aa7de05SMark Rutland numqueues = READ_ONCE(tun->numqueues); 582c8d68e6bSJason Wang 583feec084aSJason Wang txq = __skb_get_hash_symmetric(skb); 58496442e42SJason Wang e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq); 5859bc88939STom Herbert if (e) { 5869bc88939STom Herbert tun_flow_save_rps_rxhash(e, txq); 587fbe4d456SZhi Yong Wu txq = e->queue_index; 5884b035271SWang Li } else { 589c8d68e6bSJason Wang /* use multiply and shift instead of expensive divide */ 590c8d68e6bSJason Wang txq = ((u64)txq * numqueues) >> 32; 591c8d68e6bSJason Wang } 592c8d68e6bSJason Wang 593c8d68e6bSJason Wang return txq; 594c8d68e6bSJason Wang } 595c8d68e6bSJason Wang 59696f84061SJason Wang static u16 tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb) 59796f84061SJason Wang { 598cd5681d7SJason Wang struct tun_prog *prog; 599a35d310fSJason Wang u32 numqueues; 60096f84061SJason Wang u16 ret = 0; 60196f84061SJason Wang 602a35d310fSJason Wang numqueues = READ_ONCE(tun->numqueues); 603a35d310fSJason Wang if (!numqueues) 604a35d310fSJason Wang return 0; 605a35d310fSJason Wang 60696f84061SJason Wang prog = rcu_dereference(tun->steering_prog); 60796f84061SJason Wang if (prog) 60896f84061SJason Wang ret = bpf_prog_run_clear_cb(prog->prog, skb); 60996f84061SJason Wang 610a35d310fSJason Wang return ret % numqueues; 61196f84061SJason Wang } 61296f84061SJason Wang 61396f84061SJason Wang static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb, 614a350ecceSPaolo Abeni struct net_device *sb_dev) 61596f84061SJason Wang { 61696f84061SJason Wang struct tun_struct *tun = netdev_priv(dev); 61796f84061SJason Wang u16 ret; 61896f84061SJason Wang 61996f84061SJason Wang rcu_read_lock(); 62096f84061SJason Wang if (rcu_dereference(tun->steering_prog)) 62196f84061SJason Wang ret = tun_ebpf_select_queue(tun, skb); 62296f84061SJason Wang else 62396f84061SJason Wang ret = tun_automq_select_queue(tun, skb); 62496f84061SJason Wang rcu_read_unlock(); 62596f84061SJason Wang 62696f84061SJason Wang return ret; 62796f84061SJason Wang } 62896f84061SJason Wang 629cde8b15fSJason Wang static inline bool tun_not_capable(struct tun_struct *tun) 630cde8b15fSJason Wang { 631cde8b15fSJason Wang const struct cred *cred = current_cred(); 632c260b772SEric W. Biederman struct net *net = dev_net(tun->dev); 633cde8b15fSJason Wang 634cde8b15fSJason Wang return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) || 635cde8b15fSJason Wang (gid_valid(tun->group) && !in_egroup_p(tun->group))) && 636c260b772SEric W. Biederman !ns_capable(net->user_ns, CAP_NET_ADMIN); 637cde8b15fSJason Wang } 638cde8b15fSJason Wang 639c8d68e6bSJason Wang static void tun_set_real_num_queues(struct tun_struct *tun) 640c8d68e6bSJason Wang { 641c8d68e6bSJason Wang netif_set_real_num_tx_queues(tun->dev, tun->numqueues); 642c8d68e6bSJason Wang netif_set_real_num_rx_queues(tun->dev, tun->numqueues); 643c8d68e6bSJason Wang } 644c8d68e6bSJason Wang 6454008e97fSJason Wang static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) 6464008e97fSJason Wang { 6474008e97fSJason Wang tfile->detached = tun; 6484008e97fSJason Wang list_add_tail(&tfile->next, &tun->disabled); 6494008e97fSJason Wang ++tun->numdisabled; 6504008e97fSJason Wang } 6514008e97fSJason Wang 652d32649d1SJason Wang static struct tun_struct *tun_enable_queue(struct tun_file *tfile) 6534008e97fSJason Wang { 6544008e97fSJason Wang struct tun_struct *tun = tfile->detached; 6554008e97fSJason Wang 6564008e97fSJason Wang tfile->detached = NULL; 6574008e97fSJason Wang list_del_init(&tfile->next); 6584008e97fSJason Wang --tun->numdisabled; 6594008e97fSJason Wang return tun; 6604008e97fSJason Wang } 6614008e97fSJason Wang 6623a403076SJason Wang void tun_ptr_free(void *ptr) 663fc72d1d5SJason Wang { 664fc72d1d5SJason Wang if (!ptr) 665fc72d1d5SJason Wang return; 6661ffcbc85SJesper Dangaard Brouer if (tun_is_xdp_frame(ptr)) { 6671ffcbc85SJesper Dangaard Brouer struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr); 668fc72d1d5SJason Wang 66903993094SJesper Dangaard Brouer xdp_return_frame(xdpf); 670fc72d1d5SJason Wang } else { 671fc72d1d5SJason Wang __skb_array_destroy_skb(ptr); 672fc72d1d5SJason Wang } 673fc72d1d5SJason Wang } 6743a403076SJason Wang EXPORT_SYMBOL_GPL(tun_ptr_free); 675fc72d1d5SJason Wang 6764bfb0513SJason Wang static void tun_queue_purge(struct tun_file *tfile) 6774bfb0513SJason Wang { 678fc72d1d5SJason Wang void *ptr; 6791576d986SJason Wang 680fc72d1d5SJason Wang while ((ptr = ptr_ring_consume(&tfile->tx_ring)) != NULL) 681fc72d1d5SJason Wang tun_ptr_free(ptr); 6821576d986SJason Wang 6835503fcecSJason Wang skb_queue_purge(&tfile->sk.sk_write_queue); 6844bfb0513SJason Wang skb_queue_purge(&tfile->sk.sk_error_queue); 6854bfb0513SJason Wang } 6864bfb0513SJason Wang 687c8d68e6bSJason Wang static void __tun_detach(struct tun_file *tfile, bool clean) 688c8d68e6bSJason Wang { 689c8d68e6bSJason Wang struct tun_file *ntfile; 690c8d68e6bSJason Wang struct tun_struct *tun; 691c8d68e6bSJason Wang 692b8deabd3SJason Wang tun = rtnl_dereference(tfile->tun); 693b8deabd3SJason Wang 69494317099SPetar Penkov if (tun && clean) { 69506e55addSEric Dumazet tun_napi_disable(tfile); 69606e55addSEric Dumazet tun_napi_del(tfile); 69794317099SPetar Penkov } 69894317099SPetar Penkov 6999e85722dSJason Wang if (tun && !tfile->detached) { 700c8d68e6bSJason Wang u16 index = tfile->queue_index; 701c8d68e6bSJason Wang BUG_ON(index >= tun->numqueues); 702c8d68e6bSJason Wang 703c8d68e6bSJason Wang rcu_assign_pointer(tun->tfiles[index], 704c8d68e6bSJason Wang tun->tfiles[tun->numqueues - 1]); 705b8deabd3SJason Wang ntfile = rtnl_dereference(tun->tfiles[index]); 706c8d68e6bSJason Wang ntfile->queue_index = index; 707*9871a9e4SJason Wang rcu_assign_pointer(tun->tfiles[tun->numqueues - 1], 708*9871a9e4SJason Wang NULL); 709c8d68e6bSJason Wang 710c8d68e6bSJason Wang --tun->numqueues; 7119e85722dSJason Wang if (clean) { 712c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 713c8d68e6bSJason Wang sock_put(&tfile->sk); 7149e85722dSJason Wang } else 7154008e97fSJason Wang tun_disable_queue(tun, tfile); 716c8d68e6bSJason Wang 717c8d68e6bSJason Wang synchronize_net(); 71896442e42SJason Wang tun_flow_delete_by_queue(tun, tun->numqueues + 1); 719c8d68e6bSJason Wang /* Drop read queue */ 7204bfb0513SJason Wang tun_queue_purge(tfile); 721c8d68e6bSJason Wang tun_set_real_num_queues(tun); 722dd38bd85SJason Wang } else if (tfile->detached && clean) { 7234008e97fSJason Wang tun = tun_enable_queue(tfile); 724dd38bd85SJason Wang sock_put(&tfile->sk); 725dd38bd85SJason Wang } 726c8d68e6bSJason Wang 727c8d68e6bSJason Wang if (clean) { 728af668b3cSMichael S. Tsirkin if (tun && tun->numqueues == 0 && tun->numdisabled == 0) { 729af668b3cSMichael S. Tsirkin netif_carrier_off(tun->dev); 730af668b3cSMichael S. Tsirkin 73140630b82SMichael S. Tsirkin if (!(tun->flags & IFF_PERSIST) && 732af668b3cSMichael S. Tsirkin tun->dev->reg_state == NETREG_REGISTERED) 7334008e97fSJason Wang unregister_netdevice(tun->dev); 734af668b3cSMichael S. Tsirkin } 735b196d88aSJason Wang if (tun) 736b196d88aSJason Wang xdp_rxq_info_unreg(&tfile->xdp_rxq); 7377063efd3SJason Wang ptr_ring_cleanup(&tfile->tx_ring, tun_ptr_free); 738140e807dSEric W. Biederman sock_put(&tfile->sk); 739c8d68e6bSJason Wang } 740c8d68e6bSJason Wang } 741c8d68e6bSJason Wang 742c8d68e6bSJason Wang static void tun_detach(struct tun_file *tfile, bool clean) 743c8d68e6bSJason Wang { 74483c1f36fSSabrina Dubroca struct tun_struct *tun; 74583c1f36fSSabrina Dubroca struct net_device *dev; 74683c1f36fSSabrina Dubroca 747c8d68e6bSJason Wang rtnl_lock(); 74883c1f36fSSabrina Dubroca tun = rtnl_dereference(tfile->tun); 74983c1f36fSSabrina Dubroca dev = tun ? tun->dev : NULL; 750c8d68e6bSJason Wang __tun_detach(tfile, clean); 75183c1f36fSSabrina Dubroca if (dev) 75283c1f36fSSabrina Dubroca netdev_state_change(dev); 753c8d68e6bSJason Wang rtnl_unlock(); 754c8d68e6bSJason Wang } 755c8d68e6bSJason Wang 756c8d68e6bSJason Wang static void tun_detach_all(struct net_device *dev) 757c8d68e6bSJason Wang { 758c8d68e6bSJason Wang struct tun_struct *tun = netdev_priv(dev); 7594008e97fSJason Wang struct tun_file *tfile, *tmp; 760c8d68e6bSJason Wang int i, n = tun->numqueues; 761c8d68e6bSJason Wang 762c8d68e6bSJason Wang for (i = 0; i < n; i++) { 763b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 764c8d68e6bSJason Wang BUG_ON(!tfile); 76506e55addSEric Dumazet tun_napi_disable(tfile); 766addf8fc4SJason Wang tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; 7679e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 768c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 769c8d68e6bSJason Wang --tun->numqueues; 770c8d68e6bSJason Wang } 7719e85722dSJason Wang list_for_each_entry(tfile, &tun->disabled, next) { 772addf8fc4SJason Wang tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; 7739e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 774c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 7759e85722dSJason Wang } 776c8d68e6bSJason Wang BUG_ON(tun->numqueues != 0); 777c8d68e6bSJason Wang 778c8d68e6bSJason Wang synchronize_net(); 779c8d68e6bSJason Wang for (i = 0; i < n; i++) { 780b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 78106e55addSEric Dumazet tun_napi_del(tfile); 782c8d68e6bSJason Wang /* Drop read queue */ 7834bfb0513SJason Wang tun_queue_purge(tfile); 784b196d88aSJason Wang xdp_rxq_info_unreg(&tfile->xdp_rxq); 785c8d68e6bSJason Wang sock_put(&tfile->sk); 786c8d68e6bSJason Wang } 7874008e97fSJason Wang list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { 7884008e97fSJason Wang tun_enable_queue(tfile); 7894bfb0513SJason Wang tun_queue_purge(tfile); 790b196d88aSJason Wang xdp_rxq_info_unreg(&tfile->xdp_rxq); 7914008e97fSJason Wang sock_put(&tfile->sk); 7924008e97fSJason Wang } 7934008e97fSJason Wang BUG_ON(tun->numdisabled != 0); 794dd38bd85SJason Wang 79540630b82SMichael S. Tsirkin if (tun->flags & IFF_PERSIST) 796dd38bd85SJason Wang module_put(THIS_MODULE); 797c8d68e6bSJason Wang } 798c8d68e6bSJason Wang 79994317099SPetar Penkov static int tun_attach(struct tun_struct *tun, struct file *file, 800af3fb24eSEric Dumazet bool skip_filter, bool napi, bool napi_frags) 801a7385ba2SEric W. Biederman { 802631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 8031576d986SJason Wang struct net_device *dev = tun->dev; 80438231b7aSEric W. Biederman int err; 805a7385ba2SEric W. Biederman 8065dbbaf2dSPaul Moore err = security_tun_dev_attach(tfile->socket.sk, tun->security); 8075dbbaf2dSPaul Moore if (err < 0) 8085dbbaf2dSPaul Moore goto out; 8095dbbaf2dSPaul Moore 81038231b7aSEric W. Biederman err = -EINVAL; 8119e85722dSJason Wang if (rtnl_dereference(tfile->tun) && !tfile->detached) 81238231b7aSEric W. Biederman goto out; 81338231b7aSEric W. Biederman 81438231b7aSEric W. Biederman err = -EBUSY; 81540630b82SMichael S. Tsirkin if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1) 816c8d68e6bSJason Wang goto out; 817c8d68e6bSJason Wang 818c8d68e6bSJason Wang err = -E2BIG; 8194008e97fSJason Wang if (!tfile->detached && 8204008e97fSJason Wang tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES) 82138231b7aSEric W. Biederman goto out; 82238231b7aSEric W. Biederman 82338231b7aSEric W. Biederman err = 0; 82454f968d6SJason Wang 82592d4ea6eSstephen hemminger /* Re-attach the filter to persist device */ 826849c9b6fSPavel Emelyanov if (!skip_filter && (tun->filter_attached == true)) { 8278ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 8288ced425eSHannes Frederic Sowa err = sk_attach_filter(&tun->fprog, tfile->socket.sk); 8298ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 83054f968d6SJason Wang if (!err) 83154f968d6SJason Wang goto out; 83254f968d6SJason Wang } 8331576d986SJason Wang 8341576d986SJason Wang if (!tfile->detached && 835b196d88aSJason Wang ptr_ring_resize(&tfile->tx_ring, dev->tx_queue_len, 836b196d88aSJason Wang GFP_KERNEL, tun_ptr_free)) { 8371576d986SJason Wang err = -ENOMEM; 8381576d986SJason Wang goto out; 8391576d986SJason Wang } 8401576d986SJason Wang 841c8d68e6bSJason Wang tfile->queue_index = tun->numqueues; 842addf8fc4SJason Wang tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN; 8438bf5c4eeSJesper Dangaard Brouer 8448bf5c4eeSJesper Dangaard Brouer if (tfile->detached) { 8458bf5c4eeSJesper Dangaard Brouer /* Re-attach detached tfile, updating XDP queue_index */ 8468bf5c4eeSJesper Dangaard Brouer WARN_ON(!xdp_rxq_info_is_reg(&tfile->xdp_rxq)); 8478bf5c4eeSJesper Dangaard Brouer 8488bf5c4eeSJesper Dangaard Brouer if (tfile->xdp_rxq.queue_index != tfile->queue_index) 8498bf5c4eeSJesper Dangaard Brouer tfile->xdp_rxq.queue_index = tfile->queue_index; 8508bf5c4eeSJesper Dangaard Brouer } else { 8518bf5c4eeSJesper Dangaard Brouer /* Setup XDP RX-queue info, for new tfile getting attached */ 8528bf5c4eeSJesper Dangaard Brouer err = xdp_rxq_info_reg(&tfile->xdp_rxq, 8538bf5c4eeSJesper Dangaard Brouer tun->dev, tfile->queue_index); 8548bf5c4eeSJesper Dangaard Brouer if (err < 0) 8558bf5c4eeSJesper Dangaard Brouer goto out; 8568d5d8852SJesper Dangaard Brouer err = xdp_rxq_info_reg_mem_model(&tfile->xdp_rxq, 8578d5d8852SJesper Dangaard Brouer MEM_TYPE_PAGE_SHARED, NULL); 8588d5d8852SJesper Dangaard Brouer if (err < 0) { 8598d5d8852SJesper Dangaard Brouer xdp_rxq_info_unreg(&tfile->xdp_rxq); 8608d5d8852SJesper Dangaard Brouer goto out; 8618d5d8852SJesper Dangaard Brouer } 8628bf5c4eeSJesper Dangaard Brouer err = 0; 8638bf5c4eeSJesper Dangaard Brouer } 8648bf5c4eeSJesper Dangaard Brouer 86594317099SPetar Penkov if (tfile->detached) { 8664008e97fSJason Wang tun_enable_queue(tfile); 86794317099SPetar Penkov } else { 8684008e97fSJason Wang sock_hold(&tfile->sk); 869af3fb24eSEric Dumazet tun_napi_init(tun, tfile, napi, napi_frags); 87094317099SPetar Penkov } 8714008e97fSJason Wang 872e4a2a304SJason Wang if (rtnl_dereference(tun->xdp_prog)) 873e4a2a304SJason Wang sock_set_flag(&tfile->sk, SOCK_XDP); 874e4a2a304SJason Wang 875c8d68e6bSJason Wang /* device is allowed to go away first, so no need to hold extra 876c8d68e6bSJason Wang * refcnt. 877c8d68e6bSJason Wang */ 878a7385ba2SEric W. Biederman 8790b7959b6SStanislav Fomichev /* Publish tfile->tun and tun->tfiles only after we've fully 8800b7959b6SStanislav Fomichev * initialized tfile; otherwise we risk using half-initialized 8810b7959b6SStanislav Fomichev * object. 8820b7959b6SStanislav Fomichev */ 8830b7959b6SStanislav Fomichev rcu_assign_pointer(tfile->tun, tun); 8840b7959b6SStanislav Fomichev rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); 8850b7959b6SStanislav Fomichev tun->numqueues++; 8863a03cb84SGeorge Amanakis tun_set_real_num_queues(tun); 88738231b7aSEric W. Biederman out: 88838231b7aSEric W. Biederman return err; 889a7385ba2SEric W. Biederman } 890a7385ba2SEric W. Biederman 8919484dc74Syuan linyu static struct tun_struct *tun_get(struct tun_file *tfile) 892631ab46bSEric W. Biederman { 8936e914fc7SJason Wang struct tun_struct *tun; 894c70f1829SEric W. Biederman 8956e914fc7SJason Wang rcu_read_lock(); 8966e914fc7SJason Wang tun = rcu_dereference(tfile->tun); 8976e914fc7SJason Wang if (tun) 8986e914fc7SJason Wang dev_hold(tun->dev); 8996e914fc7SJason Wang rcu_read_unlock(); 900c70f1829SEric W. Biederman 901c70f1829SEric W. Biederman return tun; 902631ab46bSEric W. Biederman } 903631ab46bSEric W. Biederman 904631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun) 905631ab46bSEric W. Biederman { 9066e914fc7SJason Wang dev_put(tun->dev); 907631ab46bSEric W. Biederman } 908631ab46bSEric W. Biederman 9096b8a66eeSJoe Perches /* TAP filtering */ 910f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr) 911f271b2ccSMax Krasnyansky { 912f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 913f271b2ccSMax Krasnyansky mask[n >> 5] |= (1 << (n & 31)); 914f271b2ccSMax Krasnyansky } 915f271b2ccSMax Krasnyansky 916f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) 917f271b2ccSMax Krasnyansky { 918f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 919f271b2ccSMax Krasnyansky return mask[n >> 5] & (1 << (n & 31)); 920f271b2ccSMax Krasnyansky } 921f271b2ccSMax Krasnyansky 922f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg) 923f271b2ccSMax Krasnyansky { 924f271b2ccSMax Krasnyansky struct { u8 u[ETH_ALEN]; } *addr; 925f271b2ccSMax Krasnyansky struct tun_filter uf; 926f271b2ccSMax Krasnyansky int err, alen, n, nexact; 927f271b2ccSMax Krasnyansky 928f271b2ccSMax Krasnyansky if (copy_from_user(&uf, arg, sizeof(uf))) 929f271b2ccSMax Krasnyansky return -EFAULT; 930f271b2ccSMax Krasnyansky 931f271b2ccSMax Krasnyansky if (!uf.count) { 932f271b2ccSMax Krasnyansky /* Disabled */ 933f271b2ccSMax Krasnyansky filter->count = 0; 934f271b2ccSMax Krasnyansky return 0; 935f271b2ccSMax Krasnyansky } 936f271b2ccSMax Krasnyansky 937f271b2ccSMax Krasnyansky alen = ETH_ALEN * uf.count; 93828e8190dSMarkus Elfring addr = memdup_user(arg + sizeof(uf), alen); 93928e8190dSMarkus Elfring if (IS_ERR(addr)) 94028e8190dSMarkus Elfring return PTR_ERR(addr); 941f271b2ccSMax Krasnyansky 942f271b2ccSMax Krasnyansky /* The filter is updated without holding any locks. Which is 943f271b2ccSMax Krasnyansky * perfectly safe. We disable it first and in the worst 944f271b2ccSMax Krasnyansky * case we'll accept a few undesired packets. */ 945f271b2ccSMax Krasnyansky filter->count = 0; 946f271b2ccSMax Krasnyansky wmb(); 947f271b2ccSMax Krasnyansky 948f271b2ccSMax Krasnyansky /* Use first set of addresses as an exact filter */ 949f271b2ccSMax Krasnyansky for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++) 950f271b2ccSMax Krasnyansky memcpy(filter->addr[n], addr[n].u, ETH_ALEN); 951f271b2ccSMax Krasnyansky 952f271b2ccSMax Krasnyansky nexact = n; 953f271b2ccSMax Krasnyansky 954cfbf84fcSAlex Williamson /* Remaining multicast addresses are hashed, 955cfbf84fcSAlex Williamson * unicast will leave the filter disabled. */ 956f271b2ccSMax Krasnyansky memset(filter->mask, 0, sizeof(filter->mask)); 957cfbf84fcSAlex Williamson for (; n < uf.count; n++) { 958cfbf84fcSAlex Williamson if (!is_multicast_ether_addr(addr[n].u)) { 959cfbf84fcSAlex Williamson err = 0; /* no filter */ 9603b8d2a69SMarkus Elfring goto free_addr; 961cfbf84fcSAlex Williamson } 962f271b2ccSMax Krasnyansky addr_hash_set(filter->mask, addr[n].u); 963cfbf84fcSAlex Williamson } 964f271b2ccSMax Krasnyansky 965f271b2ccSMax Krasnyansky /* For ALLMULTI just set the mask to all ones. 966f271b2ccSMax Krasnyansky * This overrides the mask populated above. */ 967f271b2ccSMax Krasnyansky if ((uf.flags & TUN_FLT_ALLMULTI)) 968f271b2ccSMax Krasnyansky memset(filter->mask, ~0, sizeof(filter->mask)); 969f271b2ccSMax Krasnyansky 970f271b2ccSMax Krasnyansky /* Now enable the filter */ 971f271b2ccSMax Krasnyansky wmb(); 972f271b2ccSMax Krasnyansky filter->count = nexact; 973f271b2ccSMax Krasnyansky 974f271b2ccSMax Krasnyansky /* Return the number of exact filters */ 975f271b2ccSMax Krasnyansky err = nexact; 9763b8d2a69SMarkus Elfring free_addr: 977f271b2ccSMax Krasnyansky kfree(addr); 978f271b2ccSMax Krasnyansky return err; 979f271b2ccSMax Krasnyansky } 980f271b2ccSMax Krasnyansky 981f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */ 982f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb) 983f271b2ccSMax Krasnyansky { 984f271b2ccSMax Krasnyansky /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect 985f271b2ccSMax Krasnyansky * at this point. */ 986f271b2ccSMax Krasnyansky struct ethhdr *eh = (struct ethhdr *) skb->data; 987f271b2ccSMax Krasnyansky int i; 988f271b2ccSMax Krasnyansky 989f271b2ccSMax Krasnyansky /* Exact match */ 990f271b2ccSMax Krasnyansky for (i = 0; i < filter->count; i++) 9912e42e474SJoe Perches if (ether_addr_equal(eh->h_dest, filter->addr[i])) 992f271b2ccSMax Krasnyansky return 1; 993f271b2ccSMax Krasnyansky 994f271b2ccSMax Krasnyansky /* Inexact match (multicast only) */ 995f271b2ccSMax Krasnyansky if (is_multicast_ether_addr(eh->h_dest)) 996f271b2ccSMax Krasnyansky return addr_hash_test(filter->mask, eh->h_dest); 997f271b2ccSMax Krasnyansky 998f271b2ccSMax Krasnyansky return 0; 999f271b2ccSMax Krasnyansky } 1000f271b2ccSMax Krasnyansky 1001f271b2ccSMax Krasnyansky /* 1002f271b2ccSMax Krasnyansky * Checks whether the packet is accepted or not. 1003f271b2ccSMax Krasnyansky * Returns: 0 - drop, !=0 - accept 1004f271b2ccSMax Krasnyansky */ 1005f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb) 1006f271b2ccSMax Krasnyansky { 1007f271b2ccSMax Krasnyansky if (!filter->count) 1008f271b2ccSMax Krasnyansky return 1; 1009f271b2ccSMax Krasnyansky 1010f271b2ccSMax Krasnyansky return run_filter(filter, skb); 1011f271b2ccSMax Krasnyansky } 1012f271b2ccSMax Krasnyansky 10131da177e4SLinus Torvalds /* Network device part of the driver */ 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds static const struct ethtool_ops tun_ethtool_ops; 10161da177e4SLinus Torvalds 1017c70f1829SEric W. Biederman /* Net device detach from fd. */ 1018c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev) 1019c70f1829SEric W. Biederman { 1020c8d68e6bSJason Wang tun_detach_all(dev); 1021c70f1829SEric W. Biederman } 1022c70f1829SEric W. Biederman 10231da177e4SLinus Torvalds /* Net device open. */ 10241da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev) 10251da177e4SLinus Torvalds { 1026b20e2d54SHannes Frederic Sowa struct tun_struct *tun = netdev_priv(dev); 1027b20e2d54SHannes Frederic Sowa int i; 1028b20e2d54SHannes Frederic Sowa 1029c8d68e6bSJason Wang netif_tx_start_all_queues(dev); 1030b20e2d54SHannes Frederic Sowa 1031b20e2d54SHannes Frederic Sowa for (i = 0; i < tun->numqueues; i++) { 1032b20e2d54SHannes Frederic Sowa struct tun_file *tfile; 1033b20e2d54SHannes Frederic Sowa 1034b20e2d54SHannes Frederic Sowa tfile = rtnl_dereference(tun->tfiles[i]); 1035b20e2d54SHannes Frederic Sowa tfile->socket.sk->sk_write_space(tfile->socket.sk); 1036b20e2d54SHannes Frederic Sowa } 1037b20e2d54SHannes Frederic Sowa 10381da177e4SLinus Torvalds return 0; 10391da177e4SLinus Torvalds } 10401da177e4SLinus Torvalds 10411da177e4SLinus Torvalds /* Net device close. */ 10421da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev) 10431da177e4SLinus Torvalds { 1044c8d68e6bSJason Wang netif_tx_stop_all_queues(dev); 10451da177e4SLinus Torvalds return 0; 10461da177e4SLinus Torvalds } 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds /* Net device start xmit */ 104996f84061SJason Wang static void tun_automq_xmit(struct tun_struct *tun, struct sk_buff *skb) 10501da177e4SLinus Torvalds { 10513df97ba8SJason Wang #ifdef CONFIG_RPS 1052dc05360fSEric Dumazet if (tun->numqueues == 1 && static_branch_unlikely(&rps_needed)) { 10539bc88939STom Herbert /* Select queue was not called for the skbuff, so we extract the 10549bc88939STom Herbert * RPS hash and save it into the flow_table here. 10559bc88939STom Herbert */ 10564b035271SWang Li struct tun_flow_entry *e; 10579bc88939STom Herbert __u32 rxhash; 10589bc88939STom Herbert 1059feec084aSJason Wang rxhash = __skb_get_hash_symmetric(skb); 10604b035271SWang Li e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)], rxhash); 10619bc88939STom Herbert if (e) 10629bc88939STom Herbert tun_flow_save_rps_rxhash(e, rxhash); 10639bc88939STom Herbert } 10643df97ba8SJason Wang #endif 106596f84061SJason Wang } 106696f84061SJason Wang 1067aff3d70aSJason Wang static unsigned int run_ebpf_filter(struct tun_struct *tun, 1068aff3d70aSJason Wang struct sk_buff *skb, 1069aff3d70aSJason Wang int len) 1070aff3d70aSJason Wang { 1071aff3d70aSJason Wang struct tun_prog *prog = rcu_dereference(tun->filter_prog); 1072aff3d70aSJason Wang 1073aff3d70aSJason Wang if (prog) 1074aff3d70aSJason Wang len = bpf_prog_run_clear_cb(prog->prog, skb); 1075aff3d70aSJason Wang 1076aff3d70aSJason Wang return len; 1077aff3d70aSJason Wang } 1078aff3d70aSJason Wang 107996f84061SJason Wang /* Net device start xmit */ 108096f84061SJason Wang static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) 108196f84061SJason Wang { 108296f84061SJason Wang struct tun_struct *tun = netdev_priv(dev); 108396f84061SJason Wang int txq = skb->queue_mapping; 108496f84061SJason Wang struct tun_file *tfile; 1085aff3d70aSJason Wang int len = skb->len; 108696f84061SJason Wang 108796f84061SJason Wang rcu_read_lock(); 108896f84061SJason Wang tfile = rcu_dereference(tun->tfiles[txq]); 108996f84061SJason Wang 109096f84061SJason Wang /* Drop packet if interface is not attached */ 1091*9871a9e4SJason Wang if (!tfile) 109296f84061SJason Wang goto drop; 109396f84061SJason Wang 109496f84061SJason Wang if (!rcu_dereference(tun->steering_prog)) 109596f84061SJason Wang tun_automq_xmit(tun, skb); 10969bc88939STom Herbert 10976e914fc7SJason Wang tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len); 10986e914fc7SJason Wang 1099c8d68e6bSJason Wang BUG_ON(!tfile); 1100c8d68e6bSJason Wang 1101f271b2ccSMax Krasnyansky /* Drop if the filter does not like it. 1102f271b2ccSMax Krasnyansky * This is a noop if the filter is disabled. 1103f271b2ccSMax Krasnyansky * Filter can be enabled only for the TAP devices. */ 1104f271b2ccSMax Krasnyansky if (!check_filter(&tun->txflt, skb)) 1105f271b2ccSMax Krasnyansky goto drop; 1106f271b2ccSMax Krasnyansky 110754f968d6SJason Wang if (tfile->socket.sk->sk_filter && 110854f968d6SJason Wang sk_filter(tfile->socket.sk, skb)) 110999405162SMichael S. Tsirkin goto drop; 111099405162SMichael S. Tsirkin 1111aff3d70aSJason Wang len = run_ebpf_filter(tun, skb, len); 111281c89507SBjørn Mork if (len == 0 || pskb_trim(skb, len)) 1113aff3d70aSJason Wang goto drop; 1114aff3d70aSJason Wang 11151f8b977aSWillem de Bruijn if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) 11167bf66305SJason Wang goto drop; 11177bf66305SJason Wang 11187b996243SSoheil Hassas Yeganeh skb_tx_timestamp(skb); 1119eda29772SRichard Cochran 11200110d6f2SMichael S. Tsirkin /* Orphan the skb - required as we might hang on to it 11217bf66305SJason Wang * for indefinite time. 11227bf66305SJason Wang */ 11230110d6f2SMichael S. Tsirkin skb_orphan(skb); 11240110d6f2SMichael S. Tsirkin 1125f8af75f3SEric Dumazet nf_reset(skb); 1126f8af75f3SEric Dumazet 11275990a305SJason Wang if (ptr_ring_produce(&tfile->tx_ring, skb)) 11281576d986SJason Wang goto drop; 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds /* Notify and wake up reader process */ 113154f968d6SJason Wang if (tfile->flags & TUN_FASYNC) 113254f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_IN); 11339e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 11346e914fc7SJason Wang 11356e914fc7SJason Wang rcu_read_unlock(); 11366ed10654SPatrick McHardy return NETDEV_TX_OK; 11371da177e4SLinus Torvalds 11381da177e4SLinus Torvalds drop: 1139608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->tx_dropped); 1140149d36f7SMichael S. Tsirkin skb_tx_error(skb); 11411da177e4SLinus Torvalds kfree_skb(skb); 11426e914fc7SJason Wang rcu_read_unlock(); 1143baeababbSJason Wang return NET_XMIT_DROP; 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds 1146f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev) 11471da177e4SLinus Torvalds { 1148f271b2ccSMax Krasnyansky /* 1149f271b2ccSMax Krasnyansky * This callback is supposed to deal with mc filter in 1150f271b2ccSMax Krasnyansky * _rx_ path and has nothing to do with the _tx_ path. 1151f271b2ccSMax Krasnyansky * In rx path we always accept everything userspace gives us. 1152f271b2ccSMax Krasnyansky */ 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds 1155c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev, 1156c8f44affSMichał Mirosław netdev_features_t features) 115788255375SMichał Mirosław { 115888255375SMichał Mirosław struct tun_struct *tun = netdev_priv(dev); 115988255375SMichał Mirosław 116088255375SMichał Mirosław return (features & tun->set_features) | (features & ~TUN_USER_FEATURES); 116188255375SMichał Mirosław } 1162eaea34b2SPaolo Abeni 1163eaea34b2SPaolo Abeni static void tun_set_headroom(struct net_device *dev, int new_hr) 1164eaea34b2SPaolo Abeni { 1165eaea34b2SPaolo Abeni struct tun_struct *tun = netdev_priv(dev); 1166eaea34b2SPaolo Abeni 1167eaea34b2SPaolo Abeni if (new_hr < NET_SKB_PAD) 1168eaea34b2SPaolo Abeni new_hr = NET_SKB_PAD; 1169eaea34b2SPaolo Abeni 1170eaea34b2SPaolo Abeni tun->align = new_hr; 1171eaea34b2SPaolo Abeni } 1172eaea34b2SPaolo Abeni 1173bc1f4470Sstephen hemminger static void 1174608b9977SPaolo Abeni tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 1175608b9977SPaolo Abeni { 1176608b9977SPaolo Abeni u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0; 1177608b9977SPaolo Abeni struct tun_struct *tun = netdev_priv(dev); 1178608b9977SPaolo Abeni struct tun_pcpu_stats *p; 1179608b9977SPaolo Abeni int i; 1180608b9977SPaolo Abeni 1181608b9977SPaolo Abeni for_each_possible_cpu(i) { 1182608b9977SPaolo Abeni u64 rxpackets, rxbytes, txpackets, txbytes; 1183608b9977SPaolo Abeni unsigned int start; 1184608b9977SPaolo Abeni 1185608b9977SPaolo Abeni p = per_cpu_ptr(tun->pcpu_stats, i); 1186608b9977SPaolo Abeni do { 1187608b9977SPaolo Abeni start = u64_stats_fetch_begin(&p->syncp); 1188608b9977SPaolo Abeni rxpackets = p->rx_packets; 1189608b9977SPaolo Abeni rxbytes = p->rx_bytes; 1190608b9977SPaolo Abeni txpackets = p->tx_packets; 1191608b9977SPaolo Abeni txbytes = p->tx_bytes; 1192608b9977SPaolo Abeni } while (u64_stats_fetch_retry(&p->syncp, start)); 1193608b9977SPaolo Abeni 1194608b9977SPaolo Abeni stats->rx_packets += rxpackets; 1195608b9977SPaolo Abeni stats->rx_bytes += rxbytes; 1196608b9977SPaolo Abeni stats->tx_packets += txpackets; 1197608b9977SPaolo Abeni stats->tx_bytes += txbytes; 1198608b9977SPaolo Abeni 1199608b9977SPaolo Abeni /* u32 counters */ 1200608b9977SPaolo Abeni rx_dropped += p->rx_dropped; 1201608b9977SPaolo Abeni rx_frame_errors += p->rx_frame_errors; 1202608b9977SPaolo Abeni tx_dropped += p->tx_dropped; 1203608b9977SPaolo Abeni } 1204608b9977SPaolo Abeni stats->rx_dropped = rx_dropped; 1205608b9977SPaolo Abeni stats->rx_frame_errors = rx_frame_errors; 1206608b9977SPaolo Abeni stats->tx_dropped = tx_dropped; 1207608b9977SPaolo Abeni } 1208608b9977SPaolo Abeni 1209761876c8SJason Wang static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog, 1210761876c8SJason Wang struct netlink_ext_ack *extack) 1211761876c8SJason Wang { 1212761876c8SJason Wang struct tun_struct *tun = netdev_priv(dev); 1213e4a2a304SJason Wang struct tun_file *tfile; 1214761876c8SJason Wang struct bpf_prog *old_prog; 1215e4a2a304SJason Wang int i; 1216761876c8SJason Wang 1217761876c8SJason Wang old_prog = rtnl_dereference(tun->xdp_prog); 1218761876c8SJason Wang rcu_assign_pointer(tun->xdp_prog, prog); 1219761876c8SJason Wang if (old_prog) 1220761876c8SJason Wang bpf_prog_put(old_prog); 1221761876c8SJason Wang 1222e4a2a304SJason Wang for (i = 0; i < tun->numqueues; i++) { 1223e4a2a304SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 1224e4a2a304SJason Wang if (prog) 1225e4a2a304SJason Wang sock_set_flag(&tfile->sk, SOCK_XDP); 1226e4a2a304SJason Wang else 1227e4a2a304SJason Wang sock_reset_flag(&tfile->sk, SOCK_XDP); 1228e4a2a304SJason Wang } 1229e4a2a304SJason Wang list_for_each_entry(tfile, &tun->disabled, next) { 1230e4a2a304SJason Wang if (prog) 1231e4a2a304SJason Wang sock_set_flag(&tfile->sk, SOCK_XDP); 1232e4a2a304SJason Wang else 1233e4a2a304SJason Wang sock_reset_flag(&tfile->sk, SOCK_XDP); 1234e4a2a304SJason Wang } 1235e4a2a304SJason Wang 1236761876c8SJason Wang return 0; 1237761876c8SJason Wang } 1238761876c8SJason Wang 1239761876c8SJason Wang static u32 tun_xdp_query(struct net_device *dev) 1240761876c8SJason Wang { 1241761876c8SJason Wang struct tun_struct *tun = netdev_priv(dev); 1242761876c8SJason Wang const struct bpf_prog *xdp_prog; 1243761876c8SJason Wang 1244761876c8SJason Wang xdp_prog = rtnl_dereference(tun->xdp_prog); 1245761876c8SJason Wang if (xdp_prog) 1246761876c8SJason Wang return xdp_prog->aux->id; 1247761876c8SJason Wang 1248761876c8SJason Wang return 0; 1249761876c8SJason Wang } 1250761876c8SJason Wang 1251f4e63525SJakub Kicinski static int tun_xdp(struct net_device *dev, struct netdev_bpf *xdp) 1252761876c8SJason Wang { 1253761876c8SJason Wang switch (xdp->command) { 1254761876c8SJason Wang case XDP_SETUP_PROG: 1255761876c8SJason Wang return tun_xdp_set(dev, xdp->prog, xdp->extack); 1256761876c8SJason Wang case XDP_QUERY_PROG: 1257761876c8SJason Wang xdp->prog_id = tun_xdp_query(dev); 1258761876c8SJason Wang return 0; 1259761876c8SJason Wang default: 1260761876c8SJason Wang return -EINVAL; 1261761876c8SJason Wang } 1262761876c8SJason Wang } 1263761876c8SJason Wang 126426d31925SNicolas Dichtel static int tun_net_change_carrier(struct net_device *dev, bool new_carrier) 126526d31925SNicolas Dichtel { 126626d31925SNicolas Dichtel if (new_carrier) { 126726d31925SNicolas Dichtel struct tun_struct *tun = netdev_priv(dev); 126826d31925SNicolas Dichtel 126926d31925SNicolas Dichtel if (!tun->numqueues) 127026d31925SNicolas Dichtel return -EPERM; 127126d31925SNicolas Dichtel 127226d31925SNicolas Dichtel netif_carrier_on(dev); 127326d31925SNicolas Dichtel } else { 127426d31925SNicolas Dichtel netif_carrier_off(dev); 127526d31925SNicolas Dichtel } 127626d31925SNicolas Dichtel return 0; 127726d31925SNicolas Dichtel } 127826d31925SNicolas Dichtel 1279758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = { 1280c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 1281758e43b7SStephen Hemminger .ndo_open = tun_net_open, 1282758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 128300829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 128488255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 1285c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 1286eaea34b2SPaolo Abeni .ndo_set_rx_headroom = tun_set_headroom, 1287608b9977SPaolo Abeni .ndo_get_stats64 = tun_net_get_stats64, 128826d31925SNicolas Dichtel .ndo_change_carrier = tun_net_change_carrier, 1289758e43b7SStephen Hemminger }; 1290758e43b7SStephen Hemminger 12910c9d917bSJesper Dangaard Brouer static void __tun_xdp_flush_tfile(struct tun_file *tfile) 12920c9d917bSJesper Dangaard Brouer { 12930c9d917bSJesper Dangaard Brouer /* Notify and wake up reader process */ 12940c9d917bSJesper Dangaard Brouer if (tfile->flags & TUN_FASYNC) 12950c9d917bSJesper Dangaard Brouer kill_fasync(&tfile->fasync, SIGIO, POLL_IN); 12960c9d917bSJesper Dangaard Brouer tfile->socket.sk->sk_data_ready(tfile->socket.sk); 12970c9d917bSJesper Dangaard Brouer } 12980c9d917bSJesper Dangaard Brouer 129942b33468SJesper Dangaard Brouer static int tun_xdp_xmit(struct net_device *dev, int n, 130042b33468SJesper Dangaard Brouer struct xdp_frame **frames, u32 flags) 1301fc72d1d5SJason Wang { 1302fc72d1d5SJason Wang struct tun_struct *tun = netdev_priv(dev); 1303fc72d1d5SJason Wang struct tun_file *tfile; 1304fc72d1d5SJason Wang u32 numqueues; 1305735fc405SJesper Dangaard Brouer int drops = 0; 1306735fc405SJesper Dangaard Brouer int cnt = n; 1307735fc405SJesper Dangaard Brouer int i; 1308fc72d1d5SJason Wang 13090c9d917bSJesper Dangaard Brouer if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) 131042b33468SJesper Dangaard Brouer return -EINVAL; 131142b33468SJesper Dangaard Brouer 1312fc72d1d5SJason Wang rcu_read_lock(); 1313fc72d1d5SJason Wang 1314*9871a9e4SJason Wang resample: 1315fc72d1d5SJason Wang numqueues = READ_ONCE(tun->numqueues); 1316fc72d1d5SJason Wang if (!numqueues) { 1317735fc405SJesper Dangaard Brouer rcu_read_unlock(); 1318735fc405SJesper Dangaard Brouer return -ENXIO; /* Caller will free/return all frames */ 1319fc72d1d5SJason Wang } 1320fc72d1d5SJason Wang 1321fc72d1d5SJason Wang tfile = rcu_dereference(tun->tfiles[smp_processor_id() % 1322fc72d1d5SJason Wang numqueues]); 1323*9871a9e4SJason Wang if (unlikely(!tfile)) 1324*9871a9e4SJason Wang goto resample; 1325735fc405SJesper Dangaard Brouer 1326735fc405SJesper Dangaard Brouer spin_lock(&tfile->tx_ring.producer_lock); 1327735fc405SJesper Dangaard Brouer for (i = 0; i < n; i++) { 1328735fc405SJesper Dangaard Brouer struct xdp_frame *xdp = frames[i]; 1329fc72d1d5SJason Wang /* Encode the XDP flag into lowest bit for consumer to differ 1330fc72d1d5SJason Wang * XDP buffer from sk_buff. 1331fc72d1d5SJason Wang */ 1332735fc405SJesper Dangaard Brouer void *frame = tun_xdp_to_ptr(xdp); 1333fc72d1d5SJason Wang 1334735fc405SJesper Dangaard Brouer if (__ptr_ring_produce(&tfile->tx_ring, frame)) { 1335735fc405SJesper Dangaard Brouer this_cpu_inc(tun->pcpu_stats->tx_dropped); 1336735fc405SJesper Dangaard Brouer xdp_return_frame_rx_napi(xdp); 1337735fc405SJesper Dangaard Brouer drops++; 1338735fc405SJesper Dangaard Brouer } 1339735fc405SJesper Dangaard Brouer } 1340735fc405SJesper Dangaard Brouer spin_unlock(&tfile->tx_ring.producer_lock); 1341735fc405SJesper Dangaard Brouer 13420c9d917bSJesper Dangaard Brouer if (flags & XDP_XMIT_FLUSH) 13430c9d917bSJesper Dangaard Brouer __tun_xdp_flush_tfile(tfile); 13440c9d917bSJesper Dangaard Brouer 1345fc72d1d5SJason Wang rcu_read_unlock(); 1346735fc405SJesper Dangaard Brouer return cnt - drops; 1347fc72d1d5SJason Wang } 1348fc72d1d5SJason Wang 134944fa2dbdSJesper Dangaard Brouer static int tun_xdp_tx(struct net_device *dev, struct xdp_buff *xdp) 135044fa2dbdSJesper Dangaard Brouer { 135144fa2dbdSJesper Dangaard Brouer struct xdp_frame *frame = convert_to_xdp_frame(xdp); 135244fa2dbdSJesper Dangaard Brouer 135344fa2dbdSJesper Dangaard Brouer if (unlikely(!frame)) 135444fa2dbdSJesper Dangaard Brouer return -EOVERFLOW; 135544fa2dbdSJesper Dangaard Brouer 135642421a56SJesper Dangaard Brouer return tun_xdp_xmit(dev, 1, &frame, XDP_XMIT_FLUSH); 1357fc72d1d5SJason Wang } 1358fc72d1d5SJason Wang 1359758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = { 1360c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 1361758e43b7SStephen Hemminger .ndo_open = tun_net_open, 1362758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 136300829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 136488255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 1365afc4b13dSJiri Pirko .ndo_set_rx_mode = tun_net_mclist, 1366758e43b7SStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 1367758e43b7SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 1368c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 13695e52796aSToshiaki Makita .ndo_features_check = passthru_features_check, 1370eaea34b2SPaolo Abeni .ndo_set_rx_headroom = tun_set_headroom, 1371608b9977SPaolo Abeni .ndo_get_stats64 = tun_net_get_stats64, 1372f4e63525SJakub Kicinski .ndo_bpf = tun_xdp, 1373fc72d1d5SJason Wang .ndo_xdp_xmit = tun_xdp_xmit, 137426d31925SNicolas Dichtel .ndo_change_carrier = tun_net_change_carrier, 1375758e43b7SStephen Hemminger }; 1376758e43b7SStephen Hemminger 1377944a1376SPavel Emelyanov static void tun_flow_init(struct tun_struct *tun) 137896442e42SJason Wang { 137996442e42SJason Wang int i; 138096442e42SJason Wang 138196442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) 138296442e42SJason Wang INIT_HLIST_HEAD(&tun->flows[i]); 138396442e42SJason Wang 138496442e42SJason Wang tun->ageing_time = TUN_FLOW_EXPIRE; 1385e99e88a9SKees Cook timer_setup(&tun->flow_gc_timer, tun_flow_cleanup, 0); 1386e99e88a9SKees Cook mod_timer(&tun->flow_gc_timer, 1387e99e88a9SKees Cook round_jiffies_up(jiffies + tun->ageing_time)); 138896442e42SJason Wang } 138996442e42SJason Wang 139096442e42SJason Wang static void tun_flow_uninit(struct tun_struct *tun) 139196442e42SJason Wang { 139296442e42SJason Wang del_timer_sync(&tun->flow_gc_timer); 139396442e42SJason Wang tun_flow_flush(tun); 139496442e42SJason Wang } 139596442e42SJason Wang 139691572088SJarod Wilson #define MIN_MTU 68 139791572088SJarod Wilson #define MAX_MTU 65535 139891572088SJarod Wilson 13991da177e4SLinus Torvalds /* Initialize net device. */ 14001da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev) 14011da177e4SLinus Torvalds { 14021da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 140540630b82SMichael S. Tsirkin case IFF_TUN: 1406758e43b7SStephen Hemminger dev->netdev_ops = &tun_netdev_ops; 1407758e43b7SStephen Hemminger 14081da177e4SLinus Torvalds /* Point-to-Point TUN Device */ 14091da177e4SLinus Torvalds dev->hard_header_len = 0; 14101da177e4SLinus Torvalds dev->addr_len = 0; 14111da177e4SLinus Torvalds dev->mtu = 1500; 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds /* Zero header length */ 14141da177e4SLinus Torvalds dev->type = ARPHRD_NONE; 14151da177e4SLinus Torvalds dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 14161da177e4SLinus Torvalds break; 14171da177e4SLinus Torvalds 141840630b82SMichael S. Tsirkin case IFF_TAP: 14197a0a9608SKusanagi Kouichi dev->netdev_ops = &tap_netdev_ops; 14201da177e4SLinus Torvalds /* Ethernet TAP Device */ 14211da177e4SLinus Torvalds ether_setup(dev); 1422550fd08cSNeil Horman dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1423a676847bSstephen hemminger dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 142436226a8dSBrian Braunstein 1425f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 142636226a8dSBrian Braunstein 14271da177e4SLinus Torvalds break; 14281da177e4SLinus Torvalds } 142991572088SJarod Wilson 143091572088SJarod Wilson dev->min_mtu = MIN_MTU; 143191572088SJarod Wilson dev->max_mtu = MAX_MTU - dev->hard_header_len; 14321da177e4SLinus Torvalds } 14331da177e4SLinus Torvalds 14342f3ab622SJason Wang static bool tun_sock_writeable(struct tun_struct *tun, struct tun_file *tfile) 14352f3ab622SJason Wang { 14362f3ab622SJason Wang struct sock *sk = tfile->socket.sk; 14372f3ab622SJason Wang 14382f3ab622SJason Wang return (tun->dev->flags & IFF_UP) && sock_writeable(sk); 14392f3ab622SJason Wang } 14402f3ab622SJason Wang 14411da177e4SLinus Torvalds /* Character device part */ 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds /* Poll */ 1444afc9a42bSAl Viro static __poll_t tun_chr_poll(struct file *file, poll_table *wait) 14451da177e4SLinus Torvalds { 1446b2430de3SEric W. Biederman struct tun_file *tfile = file->private_data; 14479484dc74Syuan linyu struct tun_struct *tun = tun_get(tfile); 14483c8a9c63SMariusz Kozlowski struct sock *sk; 1449afc9a42bSAl Viro __poll_t mask = 0; 14501da177e4SLinus Torvalds 14511da177e4SLinus Torvalds if (!tun) 1452a9a08845SLinus Torvalds return EPOLLERR; 14531da177e4SLinus Torvalds 145454f968d6SJason Wang sk = tfile->socket.sk; 14553c8a9c63SMariusz Kozlowski 14566b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_chr_poll\n"); 14571da177e4SLinus Torvalds 14589e641bdcSXi Wang poll_wait(file, sk_sleep(sk), wait); 14591da177e4SLinus Torvalds 14605990a305SJason Wang if (!ptr_ring_empty(&tfile->tx_ring)) 1461a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM; 14621da177e4SLinus Torvalds 14632f3ab622SJason Wang /* Make sure SOCKWQ_ASYNC_NOSPACE is set if not writable to 14642f3ab622SJason Wang * guarantee EPOLLOUT to be raised by either here or 14652f3ab622SJason Wang * tun_sock_write_space(). Then process could get notification 14662f3ab622SJason Wang * after it writes to a down device and meets -EIO. 14672f3ab622SJason Wang */ 14682f3ab622SJason Wang if (tun_sock_writeable(tun, tfile) || 14699cd3e072SEric Dumazet (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) && 14702f3ab622SJason Wang tun_sock_writeable(tun, tfile))) 1471a9a08845SLinus Torvalds mask |= EPOLLOUT | EPOLLWRNORM; 147233dccbb0SHerbert Xu 1473c70f1829SEric W. Biederman if (tun->dev->reg_state != NETREG_REGISTERED) 1474a9a08845SLinus Torvalds mask = EPOLLERR; 1475c70f1829SEric W. Biederman 1476631ab46bSEric W. Biederman tun_put(tun); 14771da177e4SLinus Torvalds return mask; 14781da177e4SLinus Torvalds } 14791da177e4SLinus Torvalds 148090e33d45SPetar Penkov static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile, 148190e33d45SPetar Penkov size_t len, 148290e33d45SPetar Penkov const struct iov_iter *it) 148390e33d45SPetar Penkov { 148490e33d45SPetar Penkov struct sk_buff *skb; 148590e33d45SPetar Penkov size_t linear; 148690e33d45SPetar Penkov int err; 148790e33d45SPetar Penkov int i; 148890e33d45SPetar Penkov 148990e33d45SPetar Penkov if (it->nr_segs > MAX_SKB_FRAGS + 1) 149090e33d45SPetar Penkov return ERR_PTR(-ENOMEM); 149190e33d45SPetar Penkov 149290e33d45SPetar Penkov local_bh_disable(); 149390e33d45SPetar Penkov skb = napi_get_frags(&tfile->napi); 149490e33d45SPetar Penkov local_bh_enable(); 149590e33d45SPetar Penkov if (!skb) 149690e33d45SPetar Penkov return ERR_PTR(-ENOMEM); 149790e33d45SPetar Penkov 149890e33d45SPetar Penkov linear = iov_iter_single_seg_count(it); 149990e33d45SPetar Penkov err = __skb_grow(skb, linear); 150090e33d45SPetar Penkov if (err) 150190e33d45SPetar Penkov goto free; 150290e33d45SPetar Penkov 150390e33d45SPetar Penkov skb->len = len; 150490e33d45SPetar Penkov skb->data_len = len - linear; 150590e33d45SPetar Penkov skb->truesize += skb->data_len; 150690e33d45SPetar Penkov 150790e33d45SPetar Penkov for (i = 1; i < it->nr_segs; i++) { 150890e33d45SPetar Penkov size_t fragsz = it->iov[i].iov_len; 1509aa6daacaSEric Dumazet struct page *page; 1510aa6daacaSEric Dumazet void *frag; 151190e33d45SPetar Penkov 151290e33d45SPetar Penkov if (fragsz == 0 || fragsz > PAGE_SIZE) { 151390e33d45SPetar Penkov err = -EINVAL; 151490e33d45SPetar Penkov goto free; 151590e33d45SPetar Penkov } 1516aa6daacaSEric Dumazet frag = netdev_alloc_frag(fragsz); 1517aa6daacaSEric Dumazet if (!frag) { 151890e33d45SPetar Penkov err = -ENOMEM; 151990e33d45SPetar Penkov goto free; 152090e33d45SPetar Penkov } 1521aa6daacaSEric Dumazet page = virt_to_head_page(frag); 1522aa6daacaSEric Dumazet skb_fill_page_desc(skb, i - 1, page, 1523aa6daacaSEric Dumazet frag - page_address(page), fragsz); 152490e33d45SPetar Penkov } 152590e33d45SPetar Penkov 152690e33d45SPetar Penkov return skb; 152790e33d45SPetar Penkov free: 152890e33d45SPetar Penkov /* frees skb and all frags allocated with napi_alloc_frag() */ 152990e33d45SPetar Penkov napi_free_frags(&tfile->napi); 153090e33d45SPetar Penkov return ERR_PTR(err); 153190e33d45SPetar Penkov } 153290e33d45SPetar Penkov 1533f42157cbSRusty Russell /* prepad is the amount to reserve at front. len is length after that. 1534f42157cbSRusty Russell * linear is a hint as to how much to copy (usually headers). */ 153554f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, 153633dccbb0SHerbert Xu size_t prepad, size_t len, 153733dccbb0SHerbert Xu size_t linear, int noblock) 1538f42157cbSRusty Russell { 153954f968d6SJason Wang struct sock *sk = tfile->socket.sk; 1540f42157cbSRusty Russell struct sk_buff *skb; 154133dccbb0SHerbert Xu int err; 1542f42157cbSRusty Russell 1543f42157cbSRusty Russell /* Under a page? Don't bother with paged skb. */ 15440eca93bcSHerbert Xu if (prepad + len < PAGE_SIZE || !linear) 154533dccbb0SHerbert Xu linear = len; 1546f42157cbSRusty Russell 154733dccbb0SHerbert Xu skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, 154828d64271SEric Dumazet &err, 0); 1549f42157cbSRusty Russell if (!skb) 155033dccbb0SHerbert Xu return ERR_PTR(err); 1551f42157cbSRusty Russell 1552f42157cbSRusty Russell skb_reserve(skb, prepad); 1553f42157cbSRusty Russell skb_put(skb, linear); 155433dccbb0SHerbert Xu skb->data_len = len - linear; 155533dccbb0SHerbert Xu skb->len += len - linear; 1556f42157cbSRusty Russell 1557f42157cbSRusty Russell return skb; 1558f42157cbSRusty Russell } 1559f42157cbSRusty Russell 15605503fcecSJason Wang static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile, 15615503fcecSJason Wang struct sk_buff *skb, int more) 15625503fcecSJason Wang { 15635503fcecSJason Wang struct sk_buff_head *queue = &tfile->sk.sk_write_queue; 15645503fcecSJason Wang struct sk_buff_head process_queue; 15655503fcecSJason Wang u32 rx_batched = tun->rx_batched; 15665503fcecSJason Wang bool rcv = false; 15675503fcecSJason Wang 15685503fcecSJason Wang if (!rx_batched || (!more && skb_queue_empty(queue))) { 15695503fcecSJason Wang local_bh_disable(); 15708ebebcbaSMatthew Cover skb_record_rx_queue(skb, tfile->queue_index); 15715503fcecSJason Wang netif_receive_skb(skb); 15725503fcecSJason Wang local_bh_enable(); 15735503fcecSJason Wang return; 15745503fcecSJason Wang } 15755503fcecSJason Wang 15765503fcecSJason Wang spin_lock(&queue->lock); 15775503fcecSJason Wang if (!more || skb_queue_len(queue) == rx_batched) { 15785503fcecSJason Wang __skb_queue_head_init(&process_queue); 15795503fcecSJason Wang skb_queue_splice_tail_init(queue, &process_queue); 15805503fcecSJason Wang rcv = true; 15815503fcecSJason Wang } else { 15825503fcecSJason Wang __skb_queue_tail(queue, skb); 15835503fcecSJason Wang } 15845503fcecSJason Wang spin_unlock(&queue->lock); 15855503fcecSJason Wang 15865503fcecSJason Wang if (rcv) { 15875503fcecSJason Wang struct sk_buff *nskb; 15885503fcecSJason Wang 15895503fcecSJason Wang local_bh_disable(); 15908ebebcbaSMatthew Cover while ((nskb = __skb_dequeue(&process_queue))) { 15918ebebcbaSMatthew Cover skb_record_rx_queue(nskb, tfile->queue_index); 15925503fcecSJason Wang netif_receive_skb(nskb); 15938ebebcbaSMatthew Cover } 15948ebebcbaSMatthew Cover skb_record_rx_queue(skb, tfile->queue_index); 15955503fcecSJason Wang netif_receive_skb(skb); 15965503fcecSJason Wang local_bh_enable(); 15975503fcecSJason Wang } 15985503fcecSJason Wang } 15995503fcecSJason Wang 160066ccbc9cSJason Wang static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile, 160166ccbc9cSJason Wang int len, int noblock, bool zerocopy) 160266ccbc9cSJason Wang { 160366ccbc9cSJason Wang if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 160466ccbc9cSJason Wang return false; 160566ccbc9cSJason Wang 160666ccbc9cSJason Wang if (tfile->socket.sk->sk_sndbuf != INT_MAX) 160766ccbc9cSJason Wang return false; 160866ccbc9cSJason Wang 160966ccbc9cSJason Wang if (!noblock) 161066ccbc9cSJason Wang return false; 161166ccbc9cSJason Wang 161266ccbc9cSJason Wang if (zerocopy) 161366ccbc9cSJason Wang return false; 161466ccbc9cSJason Wang 161566ccbc9cSJason Wang if (SKB_DATA_ALIGN(len + TUN_RX_PAD) + 161666ccbc9cSJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE) 161766ccbc9cSJason Wang return false; 161866ccbc9cSJason Wang 161966ccbc9cSJason Wang return true; 162066ccbc9cSJason Wang } 162166ccbc9cSJason Wang 1622ac1f1f6cSJason Wang static struct sk_buff *__tun_build_skb(struct page_frag *alloc_frag, char *buf, 16238ae1aff0SJason Wang int buflen, int len, int pad) 1624ac1f1f6cSJason Wang { 1625ac1f1f6cSJason Wang struct sk_buff *skb = build_skb(buf, buflen); 1626ac1f1f6cSJason Wang 1627ac1f1f6cSJason Wang if (!skb) 1628ac1f1f6cSJason Wang return ERR_PTR(-ENOMEM); 1629ac1f1f6cSJason Wang 16308ae1aff0SJason Wang skb_reserve(skb, pad); 1631ac1f1f6cSJason Wang skb_put(skb, len); 1632ac1f1f6cSJason Wang 1633ac1f1f6cSJason Wang get_page(alloc_frag->page); 1634ac1f1f6cSJason Wang alloc_frag->offset += buflen; 1635ac1f1f6cSJason Wang 1636ac1f1f6cSJason Wang return skb; 1637ac1f1f6cSJason Wang } 1638ac1f1f6cSJason Wang 16398ae1aff0SJason Wang static int tun_xdp_act(struct tun_struct *tun, struct bpf_prog *xdp_prog, 16408ae1aff0SJason Wang struct xdp_buff *xdp, u32 act) 16418ae1aff0SJason Wang { 16428ae1aff0SJason Wang int err; 16438ae1aff0SJason Wang 16448ae1aff0SJason Wang switch (act) { 16458ae1aff0SJason Wang case XDP_REDIRECT: 16468ae1aff0SJason Wang err = xdp_do_redirect(tun->dev, xdp, xdp_prog); 16478ae1aff0SJason Wang if (err) 16488ae1aff0SJason Wang return err; 16498ae1aff0SJason Wang break; 16508ae1aff0SJason Wang case XDP_TX: 16518ae1aff0SJason Wang err = tun_xdp_tx(tun->dev, xdp); 16528ae1aff0SJason Wang if (err < 0) 16538ae1aff0SJason Wang return err; 16548ae1aff0SJason Wang break; 16558ae1aff0SJason Wang case XDP_PASS: 16568ae1aff0SJason Wang break; 16578ae1aff0SJason Wang default: 16588ae1aff0SJason Wang bpf_warn_invalid_xdp_action(act); 16598ae1aff0SJason Wang /* fall through */ 16608ae1aff0SJason Wang case XDP_ABORTED: 16618ae1aff0SJason Wang trace_xdp_exception(tun->dev, xdp_prog, act); 16628ae1aff0SJason Wang /* fall through */ 16638ae1aff0SJason Wang case XDP_DROP: 16648ae1aff0SJason Wang this_cpu_inc(tun->pcpu_stats->rx_dropped); 16658ae1aff0SJason Wang break; 16668ae1aff0SJason Wang } 16678ae1aff0SJason Wang 16688ae1aff0SJason Wang return act; 16698ae1aff0SJason Wang } 16708ae1aff0SJason Wang 1671761876c8SJason Wang static struct sk_buff *tun_build_skb(struct tun_struct *tun, 1672761876c8SJason Wang struct tun_file *tfile, 167366ccbc9cSJason Wang struct iov_iter *from, 1674761876c8SJason Wang struct virtio_net_hdr *hdr, 16751cfe6e93SJason Wang int len, int *skb_xdp) 167666ccbc9cSJason Wang { 16770bbd7dadSEric Dumazet struct page_frag *alloc_frag = ¤t->task_frag; 1678761876c8SJason Wang struct bpf_prog *xdp_prog; 16797df13219SJason Wang int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 168066ccbc9cSJason Wang char *buf; 168166ccbc9cSJason Wang size_t copied; 16828ae1aff0SJason Wang int pad = TUN_RX_PAD; 16838ae1aff0SJason Wang int err = 0; 16847df13219SJason Wang 16857df13219SJason Wang rcu_read_lock(); 16867df13219SJason Wang xdp_prog = rcu_dereference(tun->xdp_prog); 16877df13219SJason Wang if (xdp_prog) 16884f23aff8SJason Wang pad += XDP_PACKET_HEADROOM; 16897df13219SJason Wang buflen += SKB_DATA_ALIGN(len + pad); 16907df13219SJason Wang rcu_read_unlock(); 169166ccbc9cSJason Wang 169263b9ab65SJason Wang alloc_frag->offset = ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES); 169366ccbc9cSJason Wang if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL))) 169466ccbc9cSJason Wang return ERR_PTR(-ENOMEM); 169566ccbc9cSJason Wang 169666ccbc9cSJason Wang buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 169766ccbc9cSJason Wang copied = copy_page_from_iter(alloc_frag->page, 16987df13219SJason Wang alloc_frag->offset + pad, 169966ccbc9cSJason Wang len, from); 170066ccbc9cSJason Wang if (copied != len) 170166ccbc9cSJason Wang return ERR_PTR(-EFAULT); 170266ccbc9cSJason Wang 17037df13219SJason Wang /* There's a small window that XDP may be set after the check 17047df13219SJason Wang * of xdp_prog above, this should be rare and for simplicity 17057df13219SJason Wang * we do XDP on skb in case the headroom is not enough. 17067df13219SJason Wang */ 1707ac1f1f6cSJason Wang if (hdr->gso_type || !xdp_prog) { 17081cfe6e93SJason Wang *skb_xdp = 1; 17098ae1aff0SJason Wang return __tun_build_skb(alloc_frag, buf, buflen, len, pad); 1710ac1f1f6cSJason Wang } 1711ac1f1f6cSJason Wang 17121cfe6e93SJason Wang *skb_xdp = 0; 171366ccbc9cSJason Wang 17146547e387SToshiaki Makita local_bh_disable(); 1715761876c8SJason Wang rcu_read_lock(); 1716761876c8SJason Wang xdp_prog = rcu_dereference(tun->xdp_prog); 17178ae1aff0SJason Wang if (xdp_prog) { 1718761876c8SJason Wang struct xdp_buff xdp; 1719761876c8SJason Wang u32 act; 1720761876c8SJason Wang 1721761876c8SJason Wang xdp.data_hard_start = buf; 17227df13219SJason Wang xdp.data = buf + pad; 1723de8f3a83SDaniel Borkmann xdp_set_data_meta_invalid(&xdp); 1724761876c8SJason Wang xdp.data_end = xdp.data + len; 17258bf5c4eeSJesper Dangaard Brouer xdp.rxq = &tfile->xdp_rxq; 1726761876c8SJason Wang 17278ae1aff0SJason Wang act = bpf_prog_run_xdp(xdp_prog, &xdp); 17288ae1aff0SJason Wang if (act == XDP_REDIRECT || act == XDP_TX) { 1729761876c8SJason Wang get_page(alloc_frag->page); 1730761876c8SJason Wang alloc_frag->offset += buflen; 1731761876c8SJason Wang } 17328ae1aff0SJason Wang err = tun_xdp_act(tun, xdp_prog, &xdp, act); 17338ae1aff0SJason Wang if (err < 0) 17348ae1aff0SJason Wang goto err_xdp; 17351a097910SJason Wang if (err == XDP_REDIRECT) 17361a097910SJason Wang xdp_do_flush_map(); 17378ae1aff0SJason Wang if (err != XDP_PASS) 17388ae1aff0SJason Wang goto out; 17398ae1aff0SJason Wang 17408ae1aff0SJason Wang pad = xdp.data - xdp.data_hard_start; 17418ae1aff0SJason Wang len = xdp.data_end - xdp.data; 1742761876c8SJason Wang } 1743761876c8SJason Wang rcu_read_unlock(); 17446547e387SToshiaki Makita local_bh_enable(); 1745291aeb2bSJason Wang 17468ae1aff0SJason Wang return __tun_build_skb(alloc_frag, buf, buflen, len, pad); 1747761876c8SJason Wang 17488ae1aff0SJason Wang err_xdp: 1749761876c8SJason Wang put_page(alloc_frag->page); 1750f7053b6cSJason Wang out: 1751761876c8SJason Wang rcu_read_unlock(); 17526547e387SToshiaki Makita local_bh_enable(); 1753761876c8SJason Wang return NULL; 175466ccbc9cSJason Wang } 175566ccbc9cSJason Wang 17561da177e4SLinus Torvalds /* Get packet from user space buffer */ 175754f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, 1758f5ff53b4SAl Viro void *msg_control, struct iov_iter *from, 17595503fcecSJason Wang int noblock, bool more) 17601da177e4SLinus Torvalds { 176109640e63SHarvey Harrison struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; 17621da177e4SLinus Torvalds struct sk_buff *skb; 1763f5ff53b4SAl Viro size_t total_len = iov_iter_count(from); 1764eaea34b2SPaolo Abeni size_t len = total_len, align = tun->align, linear; 1765f43798c2SRusty Russell struct virtio_net_hdr gso = { 0 }; 1766608b9977SPaolo Abeni struct tun_pcpu_stats *stats; 176796f8d9ecSJason Wang int good_linear; 17680690899bSMichael S. Tsirkin int copylen; 17690690899bSMichael S. Tsirkin bool zerocopy = false; 17700690899bSMichael S. Tsirkin int err; 177196f84061SJason Wang u32 rxhash = 0; 17721cfe6e93SJason Wang int skb_xdp = 1; 1773af3fb24eSEric Dumazet bool frags = tun_napi_frags_enabled(tfile); 17741da177e4SLinus Torvalds 177540630b82SMichael S. Tsirkin if (!(tun->flags & IFF_NO_PI)) { 177615718ea0SDan Carpenter if (len < sizeof(pi)) 17771da177e4SLinus Torvalds return -EINVAL; 177815718ea0SDan Carpenter len -= sizeof(pi); 17791da177e4SLinus Torvalds 1780cbbd26b8SAl Viro if (!copy_from_iter_full(&pi, sizeof(pi), from)) 17811da177e4SLinus Torvalds return -EFAULT; 17821da177e4SLinus Torvalds } 17831da177e4SLinus Torvalds 178440630b82SMichael S. Tsirkin if (tun->flags & IFF_VNET_HDR) { 1785e1edab87SWillem de Bruijn int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); 1786e1edab87SWillem de Bruijn 1787e1edab87SWillem de Bruijn if (len < vnet_hdr_sz) 1788f43798c2SRusty Russell return -EINVAL; 1789e1edab87SWillem de Bruijn len -= vnet_hdr_sz; 1790f43798c2SRusty Russell 1791cbbd26b8SAl Viro if (!copy_from_iter_full(&gso, sizeof(gso), from)) 1792f43798c2SRusty Russell return -EFAULT; 1793f43798c2SRusty Russell 17944909122fSHerbert Xu if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && 179556f0dcc5SMichael S. Tsirkin tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len)) 179656f0dcc5SMichael S. Tsirkin gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2); 17974909122fSHerbert Xu 179856f0dcc5SMichael S. Tsirkin if (tun16_to_cpu(tun, gso.hdr_len) > len) 1799f43798c2SRusty Russell return -EINVAL; 1800e1edab87SWillem de Bruijn iov_iter_advance(from, vnet_hdr_sz - sizeof(gso)); 1801f43798c2SRusty Russell } 1802f43798c2SRusty Russell 180340630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) { 1804a504b86eSstephen hemminger align += NET_IP_ALIGN; 18050eca93bcSHerbert Xu if (unlikely(len < ETH_HLEN || 180656f0dcc5SMichael S. Tsirkin (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN))) 1807e01bf1c8SRusty Russell return -EINVAL; 1808e01bf1c8SRusty Russell } 18091da177e4SLinus Torvalds 181096f8d9ecSJason Wang good_linear = SKB_MAX_HEAD(align); 181196f8d9ecSJason Wang 181288529176SJason Wang if (msg_control) { 1813f5ff53b4SAl Viro struct iov_iter i = *from; 1814f5ff53b4SAl Viro 181588529176SJason Wang /* There are 256 bytes to be copied in skb, so there is 181688529176SJason Wang * enough room for skb expand head in case it is used. 18170690899bSMichael S. Tsirkin * The rest of the buffer is mapped from userspace. 18180690899bSMichael S. Tsirkin */ 181956f0dcc5SMichael S. Tsirkin copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN; 182096f8d9ecSJason Wang if (copylen > good_linear) 182196f8d9ecSJason Wang copylen = good_linear; 18223dd5c330SJason Wang linear = copylen; 1823f5ff53b4SAl Viro iov_iter_advance(&i, copylen); 1824f5ff53b4SAl Viro if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS) 182588529176SJason Wang zerocopy = true; 182688529176SJason Wang } 182788529176SJason Wang 182890e33d45SPetar Penkov if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) { 18291cfe6e93SJason Wang /* For the packet that is not easy to be processed 18301cfe6e93SJason Wang * (e.g gso or jumbo packet), we will do it at after 18311cfe6e93SJason Wang * skb was created with generic XDP routine. 18321cfe6e93SJason Wang */ 18331cfe6e93SJason Wang skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp); 183466ccbc9cSJason Wang if (IS_ERR(skb)) { 183566ccbc9cSJason Wang this_cpu_inc(tun->pcpu_stats->rx_dropped); 183666ccbc9cSJason Wang return PTR_ERR(skb); 183766ccbc9cSJason Wang } 1838761876c8SJason Wang if (!skb) 1839761876c8SJason Wang return total_len; 184066ccbc9cSJason Wang } else { 184188529176SJason Wang if (!zerocopy) { 18420690899bSMichael S. Tsirkin copylen = len; 184356f0dcc5SMichael S. Tsirkin if (tun16_to_cpu(tun, gso.hdr_len) > good_linear) 184496f8d9ecSJason Wang linear = good_linear; 184596f8d9ecSJason Wang else 184656f0dcc5SMichael S. Tsirkin linear = tun16_to_cpu(tun, gso.hdr_len); 18473dd5c330SJason Wang } 18480690899bSMichael S. Tsirkin 184990e33d45SPetar Penkov if (frags) { 185090e33d45SPetar Penkov mutex_lock(&tfile->napi_mutex); 185190e33d45SPetar Penkov skb = tun_napi_alloc_frags(tfile, copylen, from); 185290e33d45SPetar Penkov /* tun_napi_alloc_frags() enforces a layout for the skb. 185390e33d45SPetar Penkov * If zerocopy is enabled, then this layout will be 185490e33d45SPetar Penkov * overwritten by zerocopy_sg_from_iter(). 185590e33d45SPetar Penkov */ 185690e33d45SPetar Penkov zerocopy = false; 185790e33d45SPetar Penkov } else { 185890e33d45SPetar Penkov skb = tun_alloc_skb(tfile, align, copylen, linear, 185990e33d45SPetar Penkov noblock); 186090e33d45SPetar Penkov } 186190e33d45SPetar Penkov 186233dccbb0SHerbert Xu if (IS_ERR(skb)) { 186333dccbb0SHerbert Xu if (PTR_ERR(skb) != -EAGAIN) 1864608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 186590e33d45SPetar Penkov if (frags) 186690e33d45SPetar Penkov mutex_unlock(&tfile->napi_mutex); 186733dccbb0SHerbert Xu return PTR_ERR(skb); 18681da177e4SLinus Torvalds } 18691da177e4SLinus Torvalds 18700690899bSMichael S. Tsirkin if (zerocopy) 1871f5ff53b4SAl Viro err = zerocopy_sg_from_iter(skb, from); 1872af1cc7a2SJason Wang else 1873f5ff53b4SAl Viro err = skb_copy_datagram_from_iter(skb, 0, from, len); 18740690899bSMichael S. Tsirkin 18750690899bSMichael S. Tsirkin if (err) { 18764477138fSEric Dumazet err = -EFAULT; 18774477138fSEric Dumazet drop: 1878608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 18798f22757eSDave Jones kfree_skb(skb); 188090e33d45SPetar Penkov if (frags) { 188190e33d45SPetar Penkov tfile->napi.skb = NULL; 188290e33d45SPetar Penkov mutex_unlock(&tfile->napi_mutex); 188390e33d45SPetar Penkov } 188490e33d45SPetar Penkov 18854477138fSEric Dumazet return err; 18868f22757eSDave Jones } 188766ccbc9cSJason Wang } 18881da177e4SLinus Torvalds 18893e9e40e7SJarno Rajahalme if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) { 1890df10db98SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_frame_errors); 1891df10db98SPaolo Abeni kfree_skb(skb); 189290e33d45SPetar Penkov if (frags) { 189390e33d45SPetar Penkov tfile->napi.skb = NULL; 189490e33d45SPetar Penkov mutex_unlock(&tfile->napi_mutex); 189590e33d45SPetar Penkov } 189690e33d45SPetar Penkov 1897df10db98SPaolo Abeni return -EINVAL; 1898df10db98SPaolo Abeni } 1899df10db98SPaolo Abeni 19001da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 190140630b82SMichael S. Tsirkin case IFF_TUN: 190240630b82SMichael S. Tsirkin if (tun->flags & IFF_NO_PI) { 19032580c4c1SAlexander Potapenko u8 ip_version = skb->len ? (skb->data[0] >> 4) : 0; 19042580c4c1SAlexander Potapenko 19052580c4c1SAlexander Potapenko switch (ip_version) { 19062580c4c1SAlexander Potapenko case 4: 1907f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IP); 1908f09f7ee2SAng Way Chuang break; 19092580c4c1SAlexander Potapenko case 6: 1910f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IPV6); 1911f09f7ee2SAng Way Chuang break; 1912f09f7ee2SAng Way Chuang default: 1913608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 1914f09f7ee2SAng Way Chuang kfree_skb(skb); 1915f09f7ee2SAng Way Chuang return -EINVAL; 1916f09f7ee2SAng Way Chuang } 1917f09f7ee2SAng Way Chuang } 1918f09f7ee2SAng Way Chuang 1919459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 19201da177e4SLinus Torvalds skb->protocol = pi.proto; 19214c13eb66SArnaldo Carvalho de Melo skb->dev = tun->dev; 19221da177e4SLinus Torvalds break; 192340630b82SMichael S. Tsirkin case IFF_TAP: 192490e33d45SPetar Penkov if (!frags) 19251da177e4SLinus Torvalds skb->protocol = eth_type_trans(skb, tun->dev); 19261da177e4SLinus Torvalds break; 19276403eab1SJoe Perches } 19281da177e4SLinus Torvalds 19290690899bSMichael S. Tsirkin /* copy skb_ubuf_info for callback when skb has no error */ 19300690899bSMichael S. Tsirkin if (zerocopy) { 19310690899bSMichael S. Tsirkin skb_shinfo(skb)->destructor_arg = msg_control; 19320690899bSMichael S. Tsirkin skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; 1933c9af6db4SPravin B Shelar skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; 1934af1cc7a2SJason Wang } else if (msg_control) { 1935af1cc7a2SJason Wang struct ubuf_info *uarg = msg_control; 1936af1cc7a2SJason Wang uarg->callback(uarg, false); 19370690899bSMichael S. Tsirkin } 19380690899bSMichael S. Tsirkin 193972f65107SVlad Yasevich skb_reset_network_header(skb); 1940d2aa125dSMaxim Mikityanskiy skb_probe_transport_header(skb); 194138502af7SJason Wang 19421cfe6e93SJason Wang if (skb_xdp) { 1943761876c8SJason Wang struct bpf_prog *xdp_prog; 1944761876c8SJason Wang int ret; 1945761876c8SJason Wang 19466547e387SToshiaki Makita local_bh_disable(); 1947761876c8SJason Wang rcu_read_lock(); 1948761876c8SJason Wang xdp_prog = rcu_dereference(tun->xdp_prog); 1949761876c8SJason Wang if (xdp_prog) { 1950761876c8SJason Wang ret = do_xdp_generic(xdp_prog, skb); 1951761876c8SJason Wang if (ret != XDP_PASS) { 1952761876c8SJason Wang rcu_read_unlock(); 19536547e387SToshiaki Makita local_bh_enable(); 1954761876c8SJason Wang return total_len; 1955761876c8SJason Wang } 1956761876c8SJason Wang } 1957761876c8SJason Wang rcu_read_unlock(); 19586547e387SToshiaki Makita local_bh_enable(); 1959761876c8SJason Wang } 1960761876c8SJason Wang 1961cf1a1e07SPaolo Abeni /* Compute the costly rx hash only if needed for flow updates. 1962cf1a1e07SPaolo Abeni * We may get a very small possibility of OOO during switching, not 1963cf1a1e07SPaolo Abeni * worth to optimize. 1964cf1a1e07SPaolo Abeni */ 1965cf1a1e07SPaolo Abeni if (!rcu_access_pointer(tun->steering_prog) && tun->numqueues > 1 && 1966cf1a1e07SPaolo Abeni !tfile->detached) 1967feec084aSJason Wang rxhash = __skb_get_hash_symmetric(skb); 196894317099SPetar Penkov 19694477138fSEric Dumazet rcu_read_lock(); 19704477138fSEric Dumazet if (unlikely(!(tun->dev->flags & IFF_UP))) { 19714477138fSEric Dumazet err = -EIO; 19729180bb4fSEric Dumazet rcu_read_unlock(); 19734477138fSEric Dumazet goto drop; 19744477138fSEric Dumazet } 19754477138fSEric Dumazet 197690e33d45SPetar Penkov if (frags) { 197790e33d45SPetar Penkov /* Exercise flow dissector code path. */ 1978c43f1255SStanislav Fomichev u32 headlen = eth_get_headlen(tun->dev, skb->data, 1979c43f1255SStanislav Fomichev skb_headlen(skb)); 198090e33d45SPetar Penkov 1981010f245bSEric Dumazet if (unlikely(headlen > skb_headlen(skb))) { 198290e33d45SPetar Penkov this_cpu_inc(tun->pcpu_stats->rx_dropped); 198390e33d45SPetar Penkov napi_free_frags(&tfile->napi); 19844477138fSEric Dumazet rcu_read_unlock(); 198590e33d45SPetar Penkov mutex_unlock(&tfile->napi_mutex); 198690e33d45SPetar Penkov WARN_ON(1); 198790e33d45SPetar Penkov return -ENOMEM; 198890e33d45SPetar Penkov } 198990e33d45SPetar Penkov 199090e33d45SPetar Penkov local_bh_disable(); 199190e33d45SPetar Penkov napi_gro_frags(&tfile->napi); 199290e33d45SPetar Penkov local_bh_enable(); 199390e33d45SPetar Penkov mutex_unlock(&tfile->napi_mutex); 1994aec72f33SEric Dumazet } else if (tfile->napi_enabled) { 199594317099SPetar Penkov struct sk_buff_head *queue = &tfile->sk.sk_write_queue; 199694317099SPetar Penkov int queue_len; 199794317099SPetar Penkov 199894317099SPetar Penkov spin_lock_bh(&queue->lock); 199994317099SPetar Penkov __skb_queue_tail(queue, skb); 200094317099SPetar Penkov queue_len = skb_queue_len(queue); 200194317099SPetar Penkov spin_unlock(&queue->lock); 200294317099SPetar Penkov 200394317099SPetar Penkov if (!more || queue_len > NAPI_POLL_WEIGHT) 200494317099SPetar Penkov napi_schedule(&tfile->napi); 200594317099SPetar Penkov 200694317099SPetar Penkov local_bh_enable(); 200794317099SPetar Penkov } else if (!IS_ENABLED(CONFIG_4KSTACKS)) { 20085503fcecSJason Wang tun_rx_batched(tun, tfile, skb, more); 200994317099SPetar Penkov } else { 20101da177e4SLinus Torvalds netif_rx_ni(skb); 201194317099SPetar Penkov } 20124477138fSEric Dumazet rcu_read_unlock(); 20131da177e4SLinus Torvalds 2014608b9977SPaolo Abeni stats = get_cpu_ptr(tun->pcpu_stats); 2015608b9977SPaolo Abeni u64_stats_update_begin(&stats->syncp); 2016608b9977SPaolo Abeni stats->rx_packets++; 2017608b9977SPaolo Abeni stats->rx_bytes += len; 2018608b9977SPaolo Abeni u64_stats_update_end(&stats->syncp); 2019608b9977SPaolo Abeni put_cpu_ptr(stats); 20201da177e4SLinus Torvalds 202196f84061SJason Wang if (rxhash) 20229e85722dSJason Wang tun_flow_update(tun, rxhash, tfile); 202396f84061SJason Wang 20240690899bSMichael S. Tsirkin return total_len; 20251da177e4SLinus Torvalds } 20261da177e4SLinus Torvalds 2027f5ff53b4SAl Viro static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from) 20281da177e4SLinus Torvalds { 202933dccbb0SHerbert Xu struct file *file = iocb->ki_filp; 203054f968d6SJason Wang struct tun_file *tfile = file->private_data; 20319484dc74Syuan linyu struct tun_struct *tun = tun_get(tfile); 2032631ab46bSEric W. Biederman ssize_t result; 20331da177e4SLinus Torvalds 20341da177e4SLinus Torvalds if (!tun) 20351da177e4SLinus Torvalds return -EBADFD; 20361da177e4SLinus Torvalds 20375503fcecSJason Wang result = tun_get_user(tun, tfile, NULL, from, 20385503fcecSJason Wang file->f_flags & O_NONBLOCK, false); 2039631ab46bSEric W. Biederman 2040631ab46bSEric W. Biederman tun_put(tun); 2041631ab46bSEric W. Biederman return result; 20421da177e4SLinus Torvalds } 20431da177e4SLinus Torvalds 2044fc72d1d5SJason Wang static ssize_t tun_put_user_xdp(struct tun_struct *tun, 2045fc72d1d5SJason Wang struct tun_file *tfile, 20461ffcbc85SJesper Dangaard Brouer struct xdp_frame *xdp_frame, 2047fc72d1d5SJason Wang struct iov_iter *iter) 2048fc72d1d5SJason Wang { 2049fc72d1d5SJason Wang int vnet_hdr_sz = 0; 20501ffcbc85SJesper Dangaard Brouer size_t size = xdp_frame->len; 2051fc72d1d5SJason Wang struct tun_pcpu_stats *stats; 2052fc72d1d5SJason Wang size_t ret; 2053fc72d1d5SJason Wang 2054fc72d1d5SJason Wang if (tun->flags & IFF_VNET_HDR) { 2055fc72d1d5SJason Wang struct virtio_net_hdr gso = { 0 }; 2056fc72d1d5SJason Wang 2057fc72d1d5SJason Wang vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); 2058fc72d1d5SJason Wang if (unlikely(iov_iter_count(iter) < vnet_hdr_sz)) 2059fc72d1d5SJason Wang return -EINVAL; 2060fc72d1d5SJason Wang if (unlikely(copy_to_iter(&gso, sizeof(gso), iter) != 2061fc72d1d5SJason Wang sizeof(gso))) 2062fc72d1d5SJason Wang return -EFAULT; 2063fc72d1d5SJason Wang iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso)); 2064fc72d1d5SJason Wang } 2065fc72d1d5SJason Wang 20661ffcbc85SJesper Dangaard Brouer ret = copy_to_iter(xdp_frame->data, size, iter) + vnet_hdr_sz; 2067fc72d1d5SJason Wang 2068fc72d1d5SJason Wang stats = get_cpu_ptr(tun->pcpu_stats); 2069fc72d1d5SJason Wang u64_stats_update_begin(&stats->syncp); 2070fc72d1d5SJason Wang stats->tx_packets++; 2071fc72d1d5SJason Wang stats->tx_bytes += ret; 2072fc72d1d5SJason Wang u64_stats_update_end(&stats->syncp); 2073fc72d1d5SJason Wang put_cpu_ptr(tun->pcpu_stats); 2074fc72d1d5SJason Wang 2075fc72d1d5SJason Wang return ret; 2076fc72d1d5SJason Wang } 2077fc72d1d5SJason Wang 20781da177e4SLinus Torvalds /* Put packet to the user space buffer */ 20796f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun, 208054f968d6SJason Wang struct tun_file *tfile, 20811da177e4SLinus Torvalds struct sk_buff *skb, 2082e0b46d0eSHerbert Xu struct iov_iter *iter) 20831da177e4SLinus Torvalds { 20841da177e4SLinus Torvalds struct tun_pi pi = { 0, skb->protocol }; 2085608b9977SPaolo Abeni struct tun_pcpu_stats *stats; 2086e0b46d0eSHerbert Xu ssize_t total; 20878c847d25SJason Wang int vlan_offset = 0; 2088a8f9bfdfSHerbert Xu int vlan_hlen = 0; 20892eb783c4SHerbert Xu int vnet_hdr_sz = 0; 2090a8f9bfdfSHerbert Xu 2091df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) 2092a8f9bfdfSHerbert Xu vlan_hlen = VLAN_HLEN; 20931da177e4SLinus Torvalds 209440630b82SMichael S. Tsirkin if (tun->flags & IFF_VNET_HDR) 2095e1edab87SWillem de Bruijn vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); 20961da177e4SLinus Torvalds 2097e0b46d0eSHerbert Xu total = skb->len + vlan_hlen + vnet_hdr_sz; 2098e0b46d0eSHerbert Xu 209940630b82SMichael S. Tsirkin if (!(tun->flags & IFF_NO_PI)) { 2100e0b46d0eSHerbert Xu if (iov_iter_count(iter) < sizeof(pi)) 21011da177e4SLinus Torvalds return -EINVAL; 21021da177e4SLinus Torvalds 2103e0b46d0eSHerbert Xu total += sizeof(pi); 2104e0b46d0eSHerbert Xu if (iov_iter_count(iter) < total) { 21051da177e4SLinus Torvalds /* Packet will be striped */ 21061da177e4SLinus Torvalds pi.flags |= TUN_PKT_STRIP; 21071da177e4SLinus Torvalds } 21081da177e4SLinus Torvalds 2109e0b46d0eSHerbert Xu if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi)) 21101da177e4SLinus Torvalds return -EFAULT; 21111da177e4SLinus Torvalds } 21121da177e4SLinus Torvalds 21132eb783c4SHerbert Xu if (vnet_hdr_sz) { 21149403cd7cSJarno Rajahalme struct virtio_net_hdr gso; 211534166093SMike Rapoport 2116e0b46d0eSHerbert Xu if (iov_iter_count(iter) < vnet_hdr_sz) 2117f43798c2SRusty Russell return -EINVAL; 2118f43798c2SRusty Russell 21193e9e40e7SJarno Rajahalme if (virtio_net_hdr_from_skb(skb, &gso, 2120fd3a8862SWillem de Bruijn tun_is_little_endian(tun), true, 2121fd3a8862SWillem de Bruijn vlan_hlen)) { 2122f43798c2SRusty Russell struct skb_shared_info *sinfo = skb_shinfo(skb); 21236b8a66eeSJoe Perches pr_err("unexpected GSO type: " 2124ef3db4a5SMichael S. Tsirkin "0x%x, gso_size %d, hdr_len %d\n", 212556f0dcc5SMichael S. Tsirkin sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size), 212656f0dcc5SMichael S. Tsirkin tun16_to_cpu(tun, gso.hdr_len)); 2127ef3db4a5SMichael S. Tsirkin print_hex_dump(KERN_ERR, "tun: ", 2128ef3db4a5SMichael S. Tsirkin DUMP_PREFIX_NONE, 2129ef3db4a5SMichael S. Tsirkin 16, 1, skb->head, 213056f0dcc5SMichael S. Tsirkin min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true); 2131ef3db4a5SMichael S. Tsirkin WARN_ON_ONCE(1); 2132ef3db4a5SMichael S. Tsirkin return -EINVAL; 2133ef3db4a5SMichael S. Tsirkin } 2134f43798c2SRusty Russell 2135e0b46d0eSHerbert Xu if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso)) 2136f43798c2SRusty Russell return -EFAULT; 21378c847d25SJason Wang 21388c847d25SJason Wang iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso)); 2139f43798c2SRusty Russell } 2140f43798c2SRusty Russell 2141a8f9bfdfSHerbert Xu if (vlan_hlen) { 2142e0b46d0eSHerbert Xu int ret; 2143aff3d70aSJason Wang struct veth veth; 21441da177e4SLinus Torvalds 21456680ec68SJason Wang veth.h_vlan_proto = skb->vlan_proto; 2146df8a39deSJiri Pirko veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb)); 21471da177e4SLinus Torvalds 21486680ec68SJason Wang vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); 21496680ec68SJason Wang 2150e0b46d0eSHerbert Xu ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset); 2151e0b46d0eSHerbert Xu if (ret || !iov_iter_count(iter)) 21526680ec68SJason Wang goto done; 21536680ec68SJason Wang 2154e0b46d0eSHerbert Xu ret = copy_to_iter(&veth, sizeof(veth), iter); 2155e0b46d0eSHerbert Xu if (ret != sizeof(veth) || !iov_iter_count(iter)) 21566680ec68SJason Wang goto done; 21576680ec68SJason Wang } 21586680ec68SJason Wang 2159e0b46d0eSHerbert Xu skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset); 21606680ec68SJason Wang 21616680ec68SJason Wang done: 2162608b9977SPaolo Abeni /* caller is in process context, */ 2163608b9977SPaolo Abeni stats = get_cpu_ptr(tun->pcpu_stats); 2164608b9977SPaolo Abeni u64_stats_update_begin(&stats->syncp); 2165608b9977SPaolo Abeni stats->tx_packets++; 2166608b9977SPaolo Abeni stats->tx_bytes += skb->len + vlan_hlen; 2167608b9977SPaolo Abeni u64_stats_update_end(&stats->syncp); 2168608b9977SPaolo Abeni put_cpu_ptr(tun->pcpu_stats); 21691da177e4SLinus Torvalds 21701da177e4SLinus Torvalds return total; 21711da177e4SLinus Torvalds } 21721da177e4SLinus Torvalds 2173fc72d1d5SJason Wang static void *tun_ring_recv(struct tun_file *tfile, int noblock, int *err) 21741576d986SJason Wang { 21751576d986SJason Wang DECLARE_WAITQUEUE(wait, current); 2176fc72d1d5SJason Wang void *ptr = NULL; 2177f48cc6b2SJason Wang int error = 0; 21781576d986SJason Wang 2179fc72d1d5SJason Wang ptr = ptr_ring_consume(&tfile->tx_ring); 2180fc72d1d5SJason Wang if (ptr) 21811576d986SJason Wang goto out; 21821576d986SJason Wang if (noblock) { 2183f48cc6b2SJason Wang error = -EAGAIN; 21841576d986SJason Wang goto out; 21851576d986SJason Wang } 21861576d986SJason Wang 21871576d986SJason Wang add_wait_queue(&tfile->wq.wait, &wait); 21881576d986SJason Wang 21891576d986SJason Wang while (1) { 219071828b22STimur Celik set_current_state(TASK_INTERRUPTIBLE); 2191fc72d1d5SJason Wang ptr = ptr_ring_consume(&tfile->tx_ring); 2192fc72d1d5SJason Wang if (ptr) 21931576d986SJason Wang break; 21941576d986SJason Wang if (signal_pending(current)) { 2195f48cc6b2SJason Wang error = -ERESTARTSYS; 21961576d986SJason Wang break; 21971576d986SJason Wang } 21981576d986SJason Wang if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) { 2199f48cc6b2SJason Wang error = -EFAULT; 22001576d986SJason Wang break; 22011576d986SJason Wang } 22021576d986SJason Wang 22031576d986SJason Wang schedule(); 22041576d986SJason Wang } 22051576d986SJason Wang 2206ecef67cbSTimur Celik __set_current_state(TASK_RUNNING); 22071576d986SJason Wang remove_wait_queue(&tfile->wq.wait, &wait); 22081576d986SJason Wang 22091576d986SJason Wang out: 2210f48cc6b2SJason Wang *err = error; 2211fc72d1d5SJason Wang return ptr; 22121576d986SJason Wang } 22131576d986SJason Wang 221454f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, 22159b067034SAl Viro struct iov_iter *to, 2216fc72d1d5SJason Wang int noblock, void *ptr) 22171da177e4SLinus Torvalds { 22189b067034SAl Viro ssize_t ret; 22191576d986SJason Wang int err; 22201da177e4SLinus Torvalds 22213872baf6SRami Rosen tun_debug(KERN_INFO, tun, "tun_do_read\n"); 22221da177e4SLinus Torvalds 2223c33ee15bSWei Xu if (!iov_iter_count(to)) { 2224fc72d1d5SJason Wang tun_ptr_free(ptr); 22259b067034SAl Viro return 0; 2226c33ee15bSWei Xu } 22271da177e4SLinus Torvalds 2228fc72d1d5SJason Wang if (!ptr) { 22291576d986SJason Wang /* Read frames from ring */ 2230fc72d1d5SJason Wang ptr = tun_ring_recv(tfile, noblock, &err); 2231fc72d1d5SJason Wang if (!ptr) 2232957f094fSAlex Gartrell return err; 2233ac77cfd4SJason Wang } 2234e0b46d0eSHerbert Xu 22351ffcbc85SJesper Dangaard Brouer if (tun_is_xdp_frame(ptr)) { 22361ffcbc85SJesper Dangaard Brouer struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr); 2237fc72d1d5SJason Wang 22381ffcbc85SJesper Dangaard Brouer ret = tun_put_user_xdp(tun, tfile, xdpf, to); 223903993094SJesper Dangaard Brouer xdp_return_frame(xdpf); 2240fc72d1d5SJason Wang } else { 2241fc72d1d5SJason Wang struct sk_buff *skb = ptr; 2242fc72d1d5SJason Wang 22439b067034SAl Viro ret = tun_put_user(tun, tfile, skb, to); 2244f51a5e82SJason Wang if (unlikely(ret < 0)) 22451da177e4SLinus Torvalds kfree_skb(skb); 2246f51a5e82SJason Wang else 2247f51a5e82SJason Wang consume_skb(skb); 2248fc72d1d5SJason Wang } 22491da177e4SLinus Torvalds 225005c2828cSMichael S. Tsirkin return ret; 225105c2828cSMichael S. Tsirkin } 225205c2828cSMichael S. Tsirkin 22539b067034SAl Viro static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to) 225405c2828cSMichael S. Tsirkin { 225505c2828cSMichael S. Tsirkin struct file *file = iocb->ki_filp; 225605c2828cSMichael S. Tsirkin struct tun_file *tfile = file->private_data; 22579484dc74Syuan linyu struct tun_struct *tun = tun_get(tfile); 22589b067034SAl Viro ssize_t len = iov_iter_count(to), ret; 225905c2828cSMichael S. Tsirkin 226005c2828cSMichael S. Tsirkin if (!tun) 226105c2828cSMichael S. Tsirkin return -EBADFD; 2262ac77cfd4SJason Wang ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK, NULL); 226342404c09SDavid S. Miller ret = min_t(ssize_t, ret, len); 2264d0b7da8aSZhi Yong Wu if (ret > 0) 2265d0b7da8aSZhi Yong Wu iocb->ki_pos = ret; 2266631ab46bSEric W. Biederman tun_put(tun); 22671da177e4SLinus Torvalds return ret; 22681da177e4SLinus Torvalds } 22691da177e4SLinus Torvalds 2270cd5681d7SJason Wang static void tun_prog_free(struct rcu_head *rcu) 227196f84061SJason Wang { 2272cd5681d7SJason Wang struct tun_prog *prog = container_of(rcu, struct tun_prog, rcu); 227396f84061SJason Wang 227496f84061SJason Wang bpf_prog_destroy(prog->prog); 227596f84061SJason Wang kfree(prog); 227696f84061SJason Wang } 227796f84061SJason Wang 22789d6474e4SJason Wang static int __tun_set_ebpf(struct tun_struct *tun, 22799d6474e4SJason Wang struct tun_prog __rcu **prog_p, 228096f84061SJason Wang struct bpf_prog *prog) 228196f84061SJason Wang { 2282cd5681d7SJason Wang struct tun_prog *old, *new = NULL; 228396f84061SJason Wang 228496f84061SJason Wang if (prog) { 228596f84061SJason Wang new = kmalloc(sizeof(*new), GFP_KERNEL); 228696f84061SJason Wang if (!new) 228796f84061SJason Wang return -ENOMEM; 228896f84061SJason Wang new->prog = prog; 228996f84061SJason Wang } 229096f84061SJason Wang 2291124da8f6SJason Wang spin_lock_bh(&tun->lock); 2292cd5681d7SJason Wang old = rcu_dereference_protected(*prog_p, 2293124da8f6SJason Wang lockdep_is_held(&tun->lock)); 2294cd5681d7SJason Wang rcu_assign_pointer(*prog_p, new); 2295124da8f6SJason Wang spin_unlock_bh(&tun->lock); 229696f84061SJason Wang 229796f84061SJason Wang if (old) 2298cd5681d7SJason Wang call_rcu(&old->rcu, tun_prog_free); 229996f84061SJason Wang 230096f84061SJason Wang return 0; 230196f84061SJason Wang } 230296f84061SJason Wang 230396442e42SJason Wang static void tun_free_netdev(struct net_device *dev) 230496442e42SJason Wang { 230596442e42SJason Wang struct tun_struct *tun = netdev_priv(dev); 230696442e42SJason Wang 23074008e97fSJason Wang BUG_ON(!(list_empty(&tun->disabled))); 2308608b9977SPaolo Abeni free_percpu(tun->pcpu_stats); 230996442e42SJason Wang tun_flow_uninit(tun); 23105dbbaf2dSPaul Moore security_tun_dev_free_security(tun->security); 2311cd5681d7SJason Wang __tun_set_ebpf(tun, &tun->steering_prog, NULL); 2312aff3d70aSJason Wang __tun_set_ebpf(tun, &tun->filter_prog, NULL); 231396442e42SJason Wang } 231496442e42SJason Wang 23151da177e4SLinus Torvalds static void tun_setup(struct net_device *dev) 23161da177e4SLinus Torvalds { 23171da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 23181da177e4SLinus Torvalds 23190625c883SEric W. Biederman tun->owner = INVALID_UID; 23200625c883SEric W. Biederman tun->group = INVALID_GID; 23214e24f2ddSChas Williams tun_default_link_ksettings(dev, &tun->link_ksettings); 23221da177e4SLinus Torvalds 23231da177e4SLinus Torvalds dev->ethtool_ops = &tun_ethtool_ops; 2324cf124db5SDavid S. Miller dev->needs_free_netdev = true; 2325cf124db5SDavid S. Miller dev->priv_destructor = tun_free_netdev; 2326016adb72SJason Wang /* We prefer our own queue length */ 2327016adb72SJason Wang dev->tx_queue_len = TUN_READQ_SIZE; 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds 2330f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap 2331f019a7a5SEric W. Biederman * device with netlink. 2332f019a7a5SEric W. Biederman */ 2333a8b8a889SMatthias Schiffer static int tun_validate(struct nlattr *tb[], struct nlattr *data[], 2334a8b8a889SMatthias Schiffer struct netlink_ext_ack *extack) 2335f019a7a5SEric W. Biederman { 233635b827b6SNicolas Dichtel NL_SET_ERR_MSG(extack, 233735b827b6SNicolas Dichtel "tun/tap creation via rtnetlink is not supported."); 233835b827b6SNicolas Dichtel return -EOPNOTSUPP; 2339f019a7a5SEric W. Biederman } 2340f019a7a5SEric W. Biederman 23411ec010e7SSabrina Dubroca static size_t tun_get_size(const struct net_device *dev) 23421ec010e7SSabrina Dubroca { 23431ec010e7SSabrina Dubroca BUILD_BUG_ON(sizeof(u32) != sizeof(uid_t)); 23441ec010e7SSabrina Dubroca BUILD_BUG_ON(sizeof(u32) != sizeof(gid_t)); 23451ec010e7SSabrina Dubroca 23461ec010e7SSabrina Dubroca return nla_total_size(sizeof(uid_t)) + /* OWNER */ 23471ec010e7SSabrina Dubroca nla_total_size(sizeof(gid_t)) + /* GROUP */ 23481ec010e7SSabrina Dubroca nla_total_size(sizeof(u8)) + /* TYPE */ 23491ec010e7SSabrina Dubroca nla_total_size(sizeof(u8)) + /* PI */ 23501ec010e7SSabrina Dubroca nla_total_size(sizeof(u8)) + /* VNET_HDR */ 23511ec010e7SSabrina Dubroca nla_total_size(sizeof(u8)) + /* PERSIST */ 23521ec010e7SSabrina Dubroca nla_total_size(sizeof(u8)) + /* MULTI_QUEUE */ 23531ec010e7SSabrina Dubroca nla_total_size(sizeof(u32)) + /* NUM_QUEUES */ 23541ec010e7SSabrina Dubroca nla_total_size(sizeof(u32)) + /* NUM_DISABLED_QUEUES */ 23551ec010e7SSabrina Dubroca 0; 23561ec010e7SSabrina Dubroca } 23571ec010e7SSabrina Dubroca 23581ec010e7SSabrina Dubroca static int tun_fill_info(struct sk_buff *skb, const struct net_device *dev) 23591ec010e7SSabrina Dubroca { 23601ec010e7SSabrina Dubroca struct tun_struct *tun = netdev_priv(dev); 23611ec010e7SSabrina Dubroca 23621ec010e7SSabrina Dubroca if (nla_put_u8(skb, IFLA_TUN_TYPE, tun->flags & TUN_TYPE_MASK)) 23631ec010e7SSabrina Dubroca goto nla_put_failure; 23641ec010e7SSabrina Dubroca if (uid_valid(tun->owner) && 23651ec010e7SSabrina Dubroca nla_put_u32(skb, IFLA_TUN_OWNER, 23661ec010e7SSabrina Dubroca from_kuid_munged(current_user_ns(), tun->owner))) 23671ec010e7SSabrina Dubroca goto nla_put_failure; 23681ec010e7SSabrina Dubroca if (gid_valid(tun->group) && 23691ec010e7SSabrina Dubroca nla_put_u32(skb, IFLA_TUN_GROUP, 23701ec010e7SSabrina Dubroca from_kgid_munged(current_user_ns(), tun->group))) 23711ec010e7SSabrina Dubroca goto nla_put_failure; 23721ec010e7SSabrina Dubroca if (nla_put_u8(skb, IFLA_TUN_PI, !(tun->flags & IFF_NO_PI))) 23731ec010e7SSabrina Dubroca goto nla_put_failure; 23741ec010e7SSabrina Dubroca if (nla_put_u8(skb, IFLA_TUN_VNET_HDR, !!(tun->flags & IFF_VNET_HDR))) 23751ec010e7SSabrina Dubroca goto nla_put_failure; 23761ec010e7SSabrina Dubroca if (nla_put_u8(skb, IFLA_TUN_PERSIST, !!(tun->flags & IFF_PERSIST))) 23771ec010e7SSabrina Dubroca goto nla_put_failure; 23781ec010e7SSabrina Dubroca if (nla_put_u8(skb, IFLA_TUN_MULTI_QUEUE, 23791ec010e7SSabrina Dubroca !!(tun->flags & IFF_MULTI_QUEUE))) 23801ec010e7SSabrina Dubroca goto nla_put_failure; 23811ec010e7SSabrina Dubroca if (tun->flags & IFF_MULTI_QUEUE) { 23821ec010e7SSabrina Dubroca if (nla_put_u32(skb, IFLA_TUN_NUM_QUEUES, tun->numqueues)) 23831ec010e7SSabrina Dubroca goto nla_put_failure; 23841ec010e7SSabrina Dubroca if (nla_put_u32(skb, IFLA_TUN_NUM_DISABLED_QUEUES, 23851ec010e7SSabrina Dubroca tun->numdisabled)) 23861ec010e7SSabrina Dubroca goto nla_put_failure; 23871ec010e7SSabrina Dubroca } 23881ec010e7SSabrina Dubroca 23891ec010e7SSabrina Dubroca return 0; 23901ec010e7SSabrina Dubroca 23911ec010e7SSabrina Dubroca nla_put_failure: 23921ec010e7SSabrina Dubroca return -EMSGSIZE; 23931ec010e7SSabrina Dubroca } 23941ec010e7SSabrina Dubroca 2395f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = { 2396f019a7a5SEric W. Biederman .kind = DRV_NAME, 2397f019a7a5SEric W. Biederman .priv_size = sizeof(struct tun_struct), 2398f019a7a5SEric W. Biederman .setup = tun_setup, 2399f019a7a5SEric W. Biederman .validate = tun_validate, 24001ec010e7SSabrina Dubroca .get_size = tun_get_size, 24011ec010e7SSabrina Dubroca .fill_info = tun_fill_info, 2402f019a7a5SEric W. Biederman }; 2403f019a7a5SEric W. Biederman 240433dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk) 240533dccbb0SHerbert Xu { 240654f968d6SJason Wang struct tun_file *tfile; 240743815482SEric Dumazet wait_queue_head_t *wqueue; 240833dccbb0SHerbert Xu 240933dccbb0SHerbert Xu if (!sock_writeable(sk)) 241033dccbb0SHerbert Xu return; 241133dccbb0SHerbert Xu 24129cd3e072SEric Dumazet if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags)) 241333dccbb0SHerbert Xu return; 241433dccbb0SHerbert Xu 241543815482SEric Dumazet wqueue = sk_sleep(sk); 241643815482SEric Dumazet if (wqueue && waitqueue_active(wqueue)) 2417a9a08845SLinus Torvalds wake_up_interruptible_sync_poll(wqueue, EPOLLOUT | 2418a9a08845SLinus Torvalds EPOLLWRNORM | EPOLLWRBAND); 2419c722c625SHerbert Xu 242054f968d6SJason Wang tfile = container_of(sk, struct tun_file, sk); 242154f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); 242233dccbb0SHerbert Xu } 242333dccbb0SHerbert Xu 2424f9e06c45SJason Wang static void tun_put_page(struct tun_page *tpage) 2425f9e06c45SJason Wang { 2426f9e06c45SJason Wang if (tpage->page) 2427f9e06c45SJason Wang __page_frag_cache_drain(tpage->page, tpage->count); 2428f9e06c45SJason Wang } 2429f9e06c45SJason Wang 2430043d222fSJason Wang static int tun_xdp_one(struct tun_struct *tun, 2431043d222fSJason Wang struct tun_file *tfile, 2432f9e06c45SJason Wang struct xdp_buff *xdp, int *flush, 2433f9e06c45SJason Wang struct tun_page *tpage) 2434043d222fSJason Wang { 24354e4b08e5SPrashant Bhole unsigned int datasize = xdp->data_end - xdp->data; 2436043d222fSJason Wang struct tun_xdp_hdr *hdr = xdp->data_hard_start; 2437043d222fSJason Wang struct virtio_net_hdr *gso = &hdr->gso; 2438043d222fSJason Wang struct tun_pcpu_stats *stats; 2439043d222fSJason Wang struct bpf_prog *xdp_prog; 2440043d222fSJason Wang struct sk_buff *skb = NULL; 2441043d222fSJason Wang u32 rxhash = 0, act; 2442043d222fSJason Wang int buflen = hdr->buflen; 2443043d222fSJason Wang int err = 0; 2444043d222fSJason Wang bool skb_xdp = false; 2445f9e06c45SJason Wang struct page *page; 2446043d222fSJason Wang 2447043d222fSJason Wang xdp_prog = rcu_dereference(tun->xdp_prog); 2448043d222fSJason Wang if (xdp_prog) { 2449043d222fSJason Wang if (gso->gso_type) { 2450043d222fSJason Wang skb_xdp = true; 2451043d222fSJason Wang goto build; 2452043d222fSJason Wang } 2453043d222fSJason Wang xdp_set_data_meta_invalid(xdp); 2454043d222fSJason Wang xdp->rxq = &tfile->xdp_rxq; 2455043d222fSJason Wang 2456043d222fSJason Wang act = bpf_prog_run_xdp(xdp_prog, xdp); 2457043d222fSJason Wang err = tun_xdp_act(tun, xdp_prog, xdp, act); 2458043d222fSJason Wang if (err < 0) { 2459043d222fSJason Wang put_page(virt_to_head_page(xdp->data)); 2460043d222fSJason Wang return err; 2461043d222fSJason Wang } 2462043d222fSJason Wang 2463043d222fSJason Wang switch (err) { 2464043d222fSJason Wang case XDP_REDIRECT: 2465043d222fSJason Wang *flush = true; 2466043d222fSJason Wang /* fall through */ 2467043d222fSJason Wang case XDP_TX: 2468043d222fSJason Wang return 0; 2469043d222fSJason Wang case XDP_PASS: 2470043d222fSJason Wang break; 2471043d222fSJason Wang default: 2472f9e06c45SJason Wang page = virt_to_head_page(xdp->data); 2473f9e06c45SJason Wang if (tpage->page == page) { 2474f9e06c45SJason Wang ++tpage->count; 2475f9e06c45SJason Wang } else { 2476f9e06c45SJason Wang tun_put_page(tpage); 2477f9e06c45SJason Wang tpage->page = page; 2478f9e06c45SJason Wang tpage->count = 1; 2479f9e06c45SJason Wang } 2480043d222fSJason Wang return 0; 2481043d222fSJason Wang } 2482043d222fSJason Wang } 2483043d222fSJason Wang 2484043d222fSJason Wang build: 2485043d222fSJason Wang skb = build_skb(xdp->data_hard_start, buflen); 2486043d222fSJason Wang if (!skb) { 2487043d222fSJason Wang err = -ENOMEM; 2488043d222fSJason Wang goto out; 2489043d222fSJason Wang } 2490043d222fSJason Wang 2491043d222fSJason Wang skb_reserve(skb, xdp->data - xdp->data_hard_start); 2492043d222fSJason Wang skb_put(skb, xdp->data_end - xdp->data); 2493043d222fSJason Wang 2494043d222fSJason Wang if (virtio_net_hdr_to_skb(skb, gso, tun_is_little_endian(tun))) { 2495043d222fSJason Wang this_cpu_inc(tun->pcpu_stats->rx_frame_errors); 2496043d222fSJason Wang kfree_skb(skb); 2497043d222fSJason Wang err = -EINVAL; 2498043d222fSJason Wang goto out; 2499043d222fSJason Wang } 2500043d222fSJason Wang 2501043d222fSJason Wang skb->protocol = eth_type_trans(skb, tun->dev); 2502043d222fSJason Wang skb_reset_network_header(skb); 2503d2aa125dSMaxim Mikityanskiy skb_probe_transport_header(skb); 2504043d222fSJason Wang 2505043d222fSJason Wang if (skb_xdp) { 2506043d222fSJason Wang err = do_xdp_generic(xdp_prog, skb); 2507043d222fSJason Wang if (err != XDP_PASS) 2508043d222fSJason Wang goto out; 2509043d222fSJason Wang } 2510043d222fSJason Wang 2511f29eb2a9SPaolo Abeni if (!rcu_dereference(tun->steering_prog) && tun->numqueues > 1 && 2512f29eb2a9SPaolo Abeni !tfile->detached) 2513043d222fSJason Wang rxhash = __skb_get_hash_symmetric(skb); 2514043d222fSJason Wang 25158ebebcbaSMatthew Cover skb_record_rx_queue(skb, tfile->queue_index); 2516043d222fSJason Wang netif_receive_skb(skb); 2517043d222fSJason Wang 25186342ca64SPrashant Bhole /* No need for get_cpu_ptr() here since this function is 25196342ca64SPrashant Bhole * always called with bh disabled 25206342ca64SPrashant Bhole */ 25216342ca64SPrashant Bhole stats = this_cpu_ptr(tun->pcpu_stats); 2522043d222fSJason Wang u64_stats_update_begin(&stats->syncp); 2523043d222fSJason Wang stats->rx_packets++; 25244e4b08e5SPrashant Bhole stats->rx_bytes += datasize; 2525043d222fSJason Wang u64_stats_update_end(&stats->syncp); 2526043d222fSJason Wang 2527043d222fSJason Wang if (rxhash) 2528043d222fSJason Wang tun_flow_update(tun, rxhash, tfile); 2529043d222fSJason Wang 2530043d222fSJason Wang out: 2531043d222fSJason Wang return err; 2532043d222fSJason Wang } 2533043d222fSJason Wang 25341b784140SYing Xue static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len) 253505c2828cSMichael S. Tsirkin { 2536043d222fSJason Wang int ret, i; 253754f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 25389484dc74Syuan linyu struct tun_struct *tun = tun_get(tfile); 2539fe8dd45bSJason Wang struct tun_msg_ctl *ctl = m->msg_control; 2540043d222fSJason Wang struct xdp_buff *xdp; 254154f968d6SJason Wang 254254f968d6SJason Wang if (!tun) 254354f968d6SJason Wang return -EBADFD; 2544f5ff53b4SAl Viro 2545043d222fSJason Wang if (ctl && (ctl->type == TUN_MSG_PTR)) { 25466f0271d9SDavid S. Miller struct tun_page tpage; 2547043d222fSJason Wang int n = ctl->num; 2548043d222fSJason Wang int flush = 0; 2549043d222fSJason Wang 25506f0271d9SDavid S. Miller memset(&tpage, 0, sizeof(tpage)); 25516f0271d9SDavid S. Miller 2552043d222fSJason Wang local_bh_disable(); 2553043d222fSJason Wang rcu_read_lock(); 2554043d222fSJason Wang 2555043d222fSJason Wang for (i = 0; i < n; i++) { 2556043d222fSJason Wang xdp = &((struct xdp_buff *)ctl->ptr)[i]; 2557f9e06c45SJason Wang tun_xdp_one(tun, tfile, xdp, &flush, &tpage); 2558043d222fSJason Wang } 2559043d222fSJason Wang 2560043d222fSJason Wang if (flush) 2561043d222fSJason Wang xdp_do_flush_map(); 2562043d222fSJason Wang 2563043d222fSJason Wang rcu_read_unlock(); 2564043d222fSJason Wang local_bh_enable(); 2565043d222fSJason Wang 2566f9e06c45SJason Wang tun_put_page(&tpage); 2567f9e06c45SJason Wang 2568043d222fSJason Wang ret = total_len; 2569043d222fSJason Wang goto out; 2570043d222fSJason Wang } 2571fe8dd45bSJason Wang 2572fe8dd45bSJason Wang ret = tun_get_user(tun, tfile, ctl ? ctl->ptr : NULL, &m->msg_iter, 25735503fcecSJason Wang m->msg_flags & MSG_DONTWAIT, 25745503fcecSJason Wang m->msg_flags & MSG_MORE); 2575043d222fSJason Wang out: 257654f968d6SJason Wang tun_put(tun); 257754f968d6SJason Wang return ret; 257805c2828cSMichael S. Tsirkin } 257905c2828cSMichael S. Tsirkin 25801b784140SYing Xue static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, 258105c2828cSMichael S. Tsirkin int flags) 258205c2828cSMichael S. Tsirkin { 258354f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 25849484dc74Syuan linyu struct tun_struct *tun = tun_get(tfile); 2585fc72d1d5SJason Wang void *ptr = m->msg_control; 258605c2828cSMichael S. Tsirkin int ret; 258754f968d6SJason Wang 2588c33ee15bSWei Xu if (!tun) { 2589c33ee15bSWei Xu ret = -EBADFD; 2590fc72d1d5SJason Wang goto out_free; 2591c33ee15bSWei Xu } 259254f968d6SJason Wang 2593eda29772SRichard Cochran if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) { 25943811ae76SGao feng ret = -EINVAL; 2595c33ee15bSWei Xu goto out_put_tun; 25963811ae76SGao feng } 2597eda29772SRichard Cochran if (flags & MSG_ERRQUEUE) { 2598eda29772SRichard Cochran ret = sock_recv_errqueue(sock->sk, m, total_len, 2599eda29772SRichard Cochran SOL_PACKET, TUN_TX_TIMESTAMP); 2600eda29772SRichard Cochran goto out; 2601eda29772SRichard Cochran } 2602fc72d1d5SJason Wang ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, ptr); 260387897931SAlex Gartrell if (ret > (ssize_t)total_len) { 260442404c09SDavid S. Miller m->msg_flags |= MSG_TRUNC; 260542404c09SDavid S. Miller ret = flags & MSG_TRUNC ? ret : total_len; 260642404c09SDavid S. Miller } 26073811ae76SGao feng out: 260854f968d6SJason Wang tun_put(tun); 260905c2828cSMichael S. Tsirkin return ret; 2610c33ee15bSWei Xu 2611c33ee15bSWei Xu out_put_tun: 2612c33ee15bSWei Xu tun_put(tun); 2613fc72d1d5SJason Wang out_free: 2614fc72d1d5SJason Wang tun_ptr_free(ptr); 2615c33ee15bSWei Xu return ret; 261605c2828cSMichael S. Tsirkin } 261705c2828cSMichael S. Tsirkin 2618fc72d1d5SJason Wang static int tun_ptr_peek_len(void *ptr) 2619fc72d1d5SJason Wang { 2620fc72d1d5SJason Wang if (likely(ptr)) { 26211ffcbc85SJesper Dangaard Brouer if (tun_is_xdp_frame(ptr)) { 26221ffcbc85SJesper Dangaard Brouer struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr); 2623fc72d1d5SJason Wang 26241ffcbc85SJesper Dangaard Brouer return xdpf->len; 2625fc72d1d5SJason Wang } 2626fc72d1d5SJason Wang return __skb_array_len_with_tag(ptr); 2627fc72d1d5SJason Wang } else { 2628fc72d1d5SJason Wang return 0; 2629fc72d1d5SJason Wang } 2630fc72d1d5SJason Wang } 2631fc72d1d5SJason Wang 26321576d986SJason Wang static int tun_peek_len(struct socket *sock) 26331576d986SJason Wang { 26341576d986SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 26351576d986SJason Wang struct tun_struct *tun; 26361576d986SJason Wang int ret = 0; 26371576d986SJason Wang 26389484dc74Syuan linyu tun = tun_get(tfile); 26391576d986SJason Wang if (!tun) 26401576d986SJason Wang return 0; 26411576d986SJason Wang 2642fc72d1d5SJason Wang ret = PTR_RING_PEEK_CALL(&tfile->tx_ring, tun_ptr_peek_len); 26431576d986SJason Wang tun_put(tun); 26441576d986SJason Wang 26451576d986SJason Wang return ret; 26461576d986SJason Wang } 26471576d986SJason Wang 264805c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */ 264905c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = { 26501576d986SJason Wang .peek_len = tun_peek_len, 265105c2828cSMichael S. Tsirkin .sendmsg = tun_sendmsg, 265205c2828cSMichael S. Tsirkin .recvmsg = tun_recvmsg, 265305c2828cSMichael S. Tsirkin }; 265405c2828cSMichael S. Tsirkin 265533dccbb0SHerbert Xu static struct proto tun_proto = { 265633dccbb0SHerbert Xu .name = "tun", 265733dccbb0SHerbert Xu .owner = THIS_MODULE, 265854f968d6SJason Wang .obj_size = sizeof(struct tun_file), 265933dccbb0SHerbert Xu }; 2660f019a7a5SEric W. Biederman 2661980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun) 2662980c9e8cSDavid Woodhouse { 2663031f5e03SMichael S. Tsirkin return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP); 2664980c9e8cSDavid Woodhouse } 2665980c9e8cSDavid Woodhouse 2666980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr, 2667980c9e8cSDavid Woodhouse char *buf) 2668980c9e8cSDavid Woodhouse { 2669980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 2670980c9e8cSDavid Woodhouse return sprintf(buf, "0x%x\n", tun_flags(tun)); 2671980c9e8cSDavid Woodhouse } 2672980c9e8cSDavid Woodhouse 2673980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr, 2674980c9e8cSDavid Woodhouse char *buf) 2675980c9e8cSDavid Woodhouse { 2676980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 26770625c883SEric W. Biederman return uid_valid(tun->owner)? 26780625c883SEric W. Biederman sprintf(buf, "%u\n", 26790625c883SEric W. Biederman from_kuid_munged(current_user_ns(), tun->owner)): 26800625c883SEric W. Biederman sprintf(buf, "-1\n"); 2681980c9e8cSDavid Woodhouse } 2682980c9e8cSDavid Woodhouse 2683980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr, 2684980c9e8cSDavid Woodhouse char *buf) 2685980c9e8cSDavid Woodhouse { 2686980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 26870625c883SEric W. Biederman return gid_valid(tun->group) ? 26880625c883SEric W. Biederman sprintf(buf, "%u\n", 26890625c883SEric W. Biederman from_kgid_munged(current_user_ns(), tun->group)): 26900625c883SEric W. Biederman sprintf(buf, "-1\n"); 2691980c9e8cSDavid Woodhouse } 2692980c9e8cSDavid Woodhouse 2693980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL); 2694980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL); 2695980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL); 2696980c9e8cSDavid Woodhouse 2697c4d33e24STakashi Iwai static struct attribute *tun_dev_attrs[] = { 2698c4d33e24STakashi Iwai &dev_attr_tun_flags.attr, 2699c4d33e24STakashi Iwai &dev_attr_owner.attr, 2700c4d33e24STakashi Iwai &dev_attr_group.attr, 2701c4d33e24STakashi Iwai NULL 2702c4d33e24STakashi Iwai }; 2703c4d33e24STakashi Iwai 2704c4d33e24STakashi Iwai static const struct attribute_group tun_attr_group = { 2705c4d33e24STakashi Iwai .attrs = tun_dev_attrs 2706c4d33e24STakashi Iwai }; 2707c4d33e24STakashi Iwai 2708d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) 27091da177e4SLinus Torvalds { 27101da177e4SLinus Torvalds struct tun_struct *tun; 271154f968d6SJason Wang struct tun_file *tfile = file->private_data; 27121da177e4SLinus Torvalds struct net_device *dev; 27131da177e4SLinus Torvalds int err; 27141da177e4SLinus Torvalds 27157c0c3b1aSJason Wang if (tfile->detached) 27167c0c3b1aSJason Wang return -EINVAL; 27177c0c3b1aSJason Wang 271890e33d45SPetar Penkov if ((ifr->ifr_flags & IFF_NAPI_FRAGS)) { 271990e33d45SPetar Penkov if (!capable(CAP_NET_ADMIN)) 272090e33d45SPetar Penkov return -EPERM; 272190e33d45SPetar Penkov 272290e33d45SPetar Penkov if (!(ifr->ifr_flags & IFF_NAPI) || 272390e33d45SPetar Penkov (ifr->ifr_flags & TUN_TYPE_MASK) != IFF_TAP) 272490e33d45SPetar Penkov return -EINVAL; 272590e33d45SPetar Penkov } 272690e33d45SPetar Penkov 272774a3e5a7SEric W. Biederman dev = __dev_get_by_name(net, ifr->ifr_name); 272874a3e5a7SEric W. Biederman if (dev) { 2729f85ba780SDavid Woodhouse if (ifr->ifr_flags & IFF_TUN_EXCL) 2730f85ba780SDavid Woodhouse return -EBUSY; 273174a3e5a7SEric W. Biederman if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) 273274a3e5a7SEric W. Biederman tun = netdev_priv(dev); 273374a3e5a7SEric W. Biederman else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) 273474a3e5a7SEric W. Biederman tun = netdev_priv(dev); 273574a3e5a7SEric W. Biederman else 273674a3e5a7SEric W. Biederman return -EINVAL; 273774a3e5a7SEric W. Biederman 27388e6d91aeSJason Wang if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) != 273940630b82SMichael S. Tsirkin !!(tun->flags & IFF_MULTI_QUEUE)) 27408e6d91aeSJason Wang return -EINVAL; 27418e6d91aeSJason Wang 2742cde8b15fSJason Wang if (tun_not_capable(tun)) 27432b980dbdSPaul Moore return -EPERM; 27445dbbaf2dSPaul Moore err = security_tun_dev_open(tun->security); 27452b980dbdSPaul Moore if (err < 0) 27462b980dbdSPaul Moore return err; 27472b980dbdSPaul Moore 274894317099SPetar Penkov err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER, 2749af3fb24eSEric Dumazet ifr->ifr_flags & IFF_NAPI, 2750af3fb24eSEric Dumazet ifr->ifr_flags & IFF_NAPI_FRAGS); 2751a7385ba2SEric W. Biederman if (err < 0) 2752a7385ba2SEric W. Biederman return err; 27534008e97fSJason Wang 275440630b82SMichael S. Tsirkin if (tun->flags & IFF_MULTI_QUEUE && 2755e8dbad66SJason Wang (tun->numqueues + tun->numdisabled > 1)) { 2756e8dbad66SJason Wang /* One or more queue has already been attached, no need 2757e8dbad66SJason Wang * to initialize the device again. 2758e8dbad66SJason Wang */ 275983c1f36fSSabrina Dubroca netdev_state_change(dev); 2760e8dbad66SJason Wang return 0; 2761e8dbad66SJason Wang } 27629fffc5c6SSabrina Dubroca 27639fffc5c6SSabrina Dubroca tun->flags = (tun->flags & ~TUN_FEATURES) | 27649fffc5c6SSabrina Dubroca (ifr->ifr_flags & TUN_FEATURES); 276583c1f36fSSabrina Dubroca 276683c1f36fSSabrina Dubroca netdev_state_change(dev); 276783c1f36fSSabrina Dubroca } else { 27681da177e4SLinus Torvalds char *name; 27691da177e4SLinus Torvalds unsigned long flags = 0; 2770edfb6a14SJason Wang int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ? 2771edfb6a14SJason Wang MAX_TAP_QUEUES : 1; 27721da177e4SLinus Torvalds 2773c260b772SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 2774ca6bb5d7SDavid Woodhouse return -EPERM; 27752b980dbdSPaul Moore err = security_tun_dev_create(); 27762b980dbdSPaul Moore if (err < 0) 27772b980dbdSPaul Moore return err; 2778ca6bb5d7SDavid Woodhouse 27791da177e4SLinus Torvalds /* Set dev type */ 27801da177e4SLinus Torvalds if (ifr->ifr_flags & IFF_TUN) { 27811da177e4SLinus Torvalds /* TUN device */ 278240630b82SMichael S. Tsirkin flags |= IFF_TUN; 27831da177e4SLinus Torvalds name = "tun%d"; 27841da177e4SLinus Torvalds } else if (ifr->ifr_flags & IFF_TAP) { 27851da177e4SLinus Torvalds /* TAP device */ 278640630b82SMichael S. Tsirkin flags |= IFF_TAP; 27871da177e4SLinus Torvalds name = "tap%d"; 27881da177e4SLinus Torvalds } else 278936989b90SKusanagi Kouichi return -EINVAL; 27901da177e4SLinus Torvalds 27911da177e4SLinus Torvalds if (*ifr->ifr_name) 27921da177e4SLinus Torvalds name = ifr->ifr_name; 27931da177e4SLinus Torvalds 2794c8d68e6bSJason Wang dev = alloc_netdev_mqs(sizeof(struct tun_struct), name, 2795c835a677STom Gundersen NET_NAME_UNKNOWN, tun_setup, queues, 2796c835a677STom Gundersen queues); 2797edfb6a14SJason Wang 27981da177e4SLinus Torvalds if (!dev) 27991da177e4SLinus Torvalds return -ENOMEM; 28000ad646c8SCong Wang err = dev_get_valid_name(net, dev, name); 28015c25f65fSJulien Gomes if (err < 0) 28020ad646c8SCong Wang goto err_free_dev; 28031da177e4SLinus Torvalds 2804fc54c658SPavel Emelyanov dev_net_set(dev, net); 2805f019a7a5SEric W. Biederman dev->rtnl_link_ops = &tun_link_ops; 2806fb7589a1SPavel Emelyanov dev->ifindex = tfile->ifindex; 2807c4d33e24STakashi Iwai dev->sysfs_groups[0] = &tun_attr_group; 2808758e43b7SStephen Hemminger 28091da177e4SLinus Torvalds tun = netdev_priv(dev); 28101da177e4SLinus Torvalds tun->dev = dev; 28111da177e4SLinus Torvalds tun->flags = flags; 2812f271b2ccSMax Krasnyansky tun->txflt.count = 0; 2813d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr); 28141da177e4SLinus Torvalds 2815eaea34b2SPaolo Abeni tun->align = NET_SKB_PAD; 281654f968d6SJason Wang tun->filter_attached = false; 281754f968d6SJason Wang tun->sndbuf = tfile->socket.sk->sk_sndbuf; 28185503fcecSJason Wang tun->rx_batched = 0; 281996f84061SJason Wang RCU_INIT_POINTER(tun->steering_prog, NULL); 282033dccbb0SHerbert Xu 2821608b9977SPaolo Abeni tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats); 2822608b9977SPaolo Abeni if (!tun->pcpu_stats) { 2823608b9977SPaolo Abeni err = -ENOMEM; 2824608b9977SPaolo Abeni goto err_free_dev; 2825608b9977SPaolo Abeni } 2826608b9977SPaolo Abeni 282796442e42SJason Wang spin_lock_init(&tun->lock); 282896442e42SJason Wang 28295dbbaf2dSPaul Moore err = security_tun_dev_alloc_security(&tun->security); 28305dbbaf2dSPaul Moore if (err < 0) 2831608b9977SPaolo Abeni goto err_free_stat; 28322b980dbdSPaul Moore 28331da177e4SLinus Torvalds tun_net_init(dev); 2834944a1376SPavel Emelyanov tun_flow_init(tun); 283596442e42SJason Wang 283688255375SMichał Mirosław dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | 28376680ec68SJason Wang TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX | 28386680ec68SJason Wang NETIF_F_HW_VLAN_STAG_TX; 28392a2bbf17SPaolo Abeni dev->features = dev->hw_features | NETIF_F_LLTX; 28406671b224SFernando Luis Vazquez Cao dev->vlan_features = dev->features & 28416671b224SFernando Luis Vazquez Cao ~(NETIF_F_HW_VLAN_CTAG_TX | 28426671b224SFernando Luis Vazquez Cao NETIF_F_HW_VLAN_STAG_TX); 284388255375SMichał Mirosław 28449fffc5c6SSabrina Dubroca tun->flags = (tun->flags & ~TUN_FEATURES) | 28459fffc5c6SSabrina Dubroca (ifr->ifr_flags & TUN_FEATURES); 28469fffc5c6SSabrina Dubroca 28474008e97fSJason Wang INIT_LIST_HEAD(&tun->disabled); 2848af3fb24eSEric Dumazet err = tun_attach(tun, file, false, ifr->ifr_flags & IFF_NAPI, 2849af3fb24eSEric Dumazet ifr->ifr_flags & IFF_NAPI_FRAGS); 2850eb0fb363SJason Wang if (err < 0) 2851662ca437SJason Wang goto err_free_flow; 2852eb0fb363SJason Wang 28531da177e4SLinus Torvalds err = register_netdevice(tun->dev); 28541da177e4SLinus Torvalds if (err < 0) 2855662ca437SJason Wang goto err_detach; 2856af668b3cSMichael S. Tsirkin } 2857980c9e8cSDavid Woodhouse 2858eb0fb363SJason Wang netif_carrier_on(tun->dev); 28591da177e4SLinus Torvalds 28606b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_set_iff\n"); 28611da177e4SLinus Torvalds 2862e35259a9SMax Krasnyansky /* Make sure persistent devices do not get stuck in 2863e35259a9SMax Krasnyansky * xoff state. 2864e35259a9SMax Krasnyansky */ 2865e35259a9SMax Krasnyansky if (netif_running(tun->dev)) 2866c8d68e6bSJason Wang netif_tx_wake_all_queues(tun->dev); 2867e35259a9SMax Krasnyansky 28681da177e4SLinus Torvalds strcpy(ifr->ifr_name, tun->dev->name); 28691da177e4SLinus Torvalds return 0; 28701da177e4SLinus Torvalds 2871662ca437SJason Wang err_detach: 2872662ca437SJason Wang tun_detach_all(dev); 2873ff244c6bSEric Dumazet /* register_netdevice() already called tun_free_netdev() */ 2874ff244c6bSEric Dumazet goto err_free_dev; 2875ff244c6bSEric Dumazet 2876662ca437SJason Wang err_free_flow: 2877662ca437SJason Wang tun_flow_uninit(tun); 2878662ca437SJason Wang security_tun_dev_free_security(tun->security); 2879608b9977SPaolo Abeni err_free_stat: 2880608b9977SPaolo Abeni free_percpu(tun->pcpu_stats); 28811da177e4SLinus Torvalds err_free_dev: 28821da177e4SLinus Torvalds free_netdev(dev); 28831da177e4SLinus Torvalds return err; 28841da177e4SLinus Torvalds } 28851da177e4SLinus Torvalds 288612132768SKirill Tkhai static void tun_get_iff(struct tun_struct *tun, struct ifreq *ifr) 2887e3b99556SMark McLoughlin { 28886b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_get_iff\n"); 2889e3b99556SMark McLoughlin 2890e3b99556SMark McLoughlin strcpy(ifr->ifr_name, tun->dev->name); 2891e3b99556SMark McLoughlin 2892980c9e8cSDavid Woodhouse ifr->ifr_flags = tun_flags(tun); 2893e3b99556SMark McLoughlin 2894e3b99556SMark McLoughlin } 2895e3b99556SMark McLoughlin 28965228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no 28975228ddc9SRusty Russell * privs required. */ 289888255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg) 28995228ddc9SRusty Russell { 2900c8f44affSMichał Mirosław netdev_features_t features = 0; 29015228ddc9SRusty Russell 29025228ddc9SRusty Russell if (arg & TUN_F_CSUM) { 290388255375SMichał Mirosław features |= NETIF_F_HW_CSUM; 29045228ddc9SRusty Russell arg &= ~TUN_F_CSUM; 29055228ddc9SRusty Russell 29065228ddc9SRusty Russell if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { 29075228ddc9SRusty Russell if (arg & TUN_F_TSO_ECN) { 29085228ddc9SRusty Russell features |= NETIF_F_TSO_ECN; 29095228ddc9SRusty Russell arg &= ~TUN_F_TSO_ECN; 29105228ddc9SRusty Russell } 29115228ddc9SRusty Russell if (arg & TUN_F_TSO4) 29125228ddc9SRusty Russell features |= NETIF_F_TSO; 29135228ddc9SRusty Russell if (arg & TUN_F_TSO6) 29145228ddc9SRusty Russell features |= NETIF_F_TSO6; 29155228ddc9SRusty Russell arg &= ~(TUN_F_TSO4|TUN_F_TSO6); 29165228ddc9SRusty Russell } 29170c19f846SWillem de Bruijn 29180c19f846SWillem de Bruijn arg &= ~TUN_F_UFO; 29195228ddc9SRusty Russell } 29205228ddc9SRusty Russell 29215228ddc9SRusty Russell /* This gives the user a way to test for new features in future by 29225228ddc9SRusty Russell * trying to set them. */ 29235228ddc9SRusty Russell if (arg) 29245228ddc9SRusty Russell return -EINVAL; 29255228ddc9SRusty Russell 292688255375SMichał Mirosław tun->set_features = features; 292709050957SYaroslav Isakov tun->dev->wanted_features &= ~TUN_USER_FEATURES; 292809050957SYaroslav Isakov tun->dev->wanted_features |= features; 292988255375SMichał Mirosław netdev_update_features(tun->dev); 29305228ddc9SRusty Russell 29315228ddc9SRusty Russell return 0; 29325228ddc9SRusty Russell } 29335228ddc9SRusty Russell 2934c8d68e6bSJason Wang static void tun_detach_filter(struct tun_struct *tun, int n) 2935c8d68e6bSJason Wang { 2936c8d68e6bSJason Wang int i; 2937c8d68e6bSJason Wang struct tun_file *tfile; 2938c8d68e6bSJason Wang 2939c8d68e6bSJason Wang for (i = 0; i < n; i++) { 2940b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 29418ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 29428ced425eSHannes Frederic Sowa sk_detach_filter(tfile->socket.sk); 29438ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 2944c8d68e6bSJason Wang } 2945c8d68e6bSJason Wang 2946c8d68e6bSJason Wang tun->filter_attached = false; 2947c8d68e6bSJason Wang } 2948c8d68e6bSJason Wang 2949c8d68e6bSJason Wang static int tun_attach_filter(struct tun_struct *tun) 2950c8d68e6bSJason Wang { 2951c8d68e6bSJason Wang int i, ret = 0; 2952c8d68e6bSJason Wang struct tun_file *tfile; 2953c8d68e6bSJason Wang 2954c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 2955b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 29568ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 29578ced425eSHannes Frederic Sowa ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); 29588ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 2959c8d68e6bSJason Wang if (ret) { 2960c8d68e6bSJason Wang tun_detach_filter(tun, i); 2961c8d68e6bSJason Wang return ret; 2962c8d68e6bSJason Wang } 2963c8d68e6bSJason Wang } 2964c8d68e6bSJason Wang 2965c8d68e6bSJason Wang tun->filter_attached = true; 2966c8d68e6bSJason Wang return ret; 2967c8d68e6bSJason Wang } 2968c8d68e6bSJason Wang 2969c8d68e6bSJason Wang static void tun_set_sndbuf(struct tun_struct *tun) 2970c8d68e6bSJason Wang { 2971c8d68e6bSJason Wang struct tun_file *tfile; 2972c8d68e6bSJason Wang int i; 2973c8d68e6bSJason Wang 2974c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 2975b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 2976c8d68e6bSJason Wang tfile->socket.sk->sk_sndbuf = tun->sndbuf; 2977c8d68e6bSJason Wang } 2978c8d68e6bSJason Wang } 2979c8d68e6bSJason Wang 2980cde8b15fSJason Wang static int tun_set_queue(struct file *file, struct ifreq *ifr) 2981cde8b15fSJason Wang { 2982cde8b15fSJason Wang struct tun_file *tfile = file->private_data; 2983cde8b15fSJason Wang struct tun_struct *tun; 2984cde8b15fSJason Wang int ret = 0; 2985cde8b15fSJason Wang 2986cde8b15fSJason Wang rtnl_lock(); 2987cde8b15fSJason Wang 2988cde8b15fSJason Wang if (ifr->ifr_flags & IFF_ATTACH_QUEUE) { 29894008e97fSJason Wang tun = tfile->detached; 29905dbbaf2dSPaul Moore if (!tun) { 2991cde8b15fSJason Wang ret = -EINVAL; 29925dbbaf2dSPaul Moore goto unlock; 29935dbbaf2dSPaul Moore } 29945dbbaf2dSPaul Moore ret = security_tun_dev_attach_queue(tun->security); 29955dbbaf2dSPaul Moore if (ret < 0) 29965dbbaf2dSPaul Moore goto unlock; 2997af3fb24eSEric Dumazet ret = tun_attach(tun, file, false, tun->flags & IFF_NAPI, 2998af3fb24eSEric Dumazet tun->flags & IFF_NAPI_FRAGS); 29994008e97fSJason Wang } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) { 3000b8deabd3SJason Wang tun = rtnl_dereference(tfile->tun); 300140630b82SMichael S. Tsirkin if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached) 30024008e97fSJason Wang ret = -EINVAL; 3003cde8b15fSJason Wang else 30044008e97fSJason Wang __tun_detach(tfile, false); 30054008e97fSJason Wang } else 3006cde8b15fSJason Wang ret = -EINVAL; 3007cde8b15fSJason Wang 300883c1f36fSSabrina Dubroca if (ret >= 0) 300983c1f36fSSabrina Dubroca netdev_state_change(tun->dev); 301083c1f36fSSabrina Dubroca 30115dbbaf2dSPaul Moore unlock: 3012cde8b15fSJason Wang rtnl_unlock(); 3013cde8b15fSJason Wang return ret; 3014cde8b15fSJason Wang } 3015cde8b15fSJason Wang 3016cd5681d7SJason Wang static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog **prog_p, 3017cd5681d7SJason Wang void __user *data) 301896f84061SJason Wang { 301996f84061SJason Wang struct bpf_prog *prog; 302096f84061SJason Wang int fd; 302196f84061SJason Wang 302296f84061SJason Wang if (copy_from_user(&fd, data, sizeof(fd))) 302396f84061SJason Wang return -EFAULT; 302496f84061SJason Wang 302596f84061SJason Wang if (fd == -1) { 302696f84061SJason Wang prog = NULL; 302796f84061SJason Wang } else { 302896f84061SJason Wang prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER); 302996f84061SJason Wang if (IS_ERR(prog)) 303096f84061SJason Wang return PTR_ERR(prog); 303196f84061SJason Wang } 303296f84061SJason Wang 3033cd5681d7SJason Wang return __tun_set_ebpf(tun, prog_p, prog); 303496f84061SJason Wang } 303596f84061SJason Wang 303650857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd, 303750857e2aSArnd Bergmann unsigned long arg, int ifreq_len) 30381da177e4SLinus Torvalds { 303936b50babSEric W. Biederman struct tun_file *tfile = file->private_data; 3040f663706aSKirill Tkhai struct net *net = sock_net(&tfile->sk); 3041631ab46bSEric W. Biederman struct tun_struct *tun; 30421da177e4SLinus Torvalds void __user* argp = (void __user*)arg; 304326d31925SNicolas Dichtel unsigned int ifindex, carrier; 30441da177e4SLinus Torvalds struct ifreq ifr; 30450625c883SEric W. Biederman kuid_t owner; 30460625c883SEric W. Biederman kgid_t group; 304733dccbb0SHerbert Xu int sndbuf; 3048d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 30491cf8e410SMichael S. Tsirkin int le; 3050f271b2ccSMax Krasnyansky int ret; 305183c1f36fSSabrina Dubroca bool do_notify = false; 30521da177e4SLinus Torvalds 3053f2780d6dSKirill Tkhai if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || 3054f2780d6dSKirill Tkhai (_IOC_TYPE(cmd) == SOCK_IOC_TYPE && cmd != SIOCGSKNS)) { 305550857e2aSArnd Bergmann if (copy_from_user(&ifr, argp, ifreq_len)) 30561da177e4SLinus Torvalds return -EFAULT; 30578bbb1813SDavid S. Miller } else { 3058a117dacdSMathias Krause memset(&ifr, 0, sizeof(ifr)); 30598bbb1813SDavid S. Miller } 3060631ab46bSEric W. Biederman if (cmd == TUNGETFEATURES) { 3061631ab46bSEric W. Biederman /* Currently this just means: "what IFF flags are valid?". 3062631ab46bSEric W. Biederman * This is needed because we never checked for invalid flags on 3063031f5e03SMichael S. Tsirkin * TUNSETIFF. 3064031f5e03SMichael S. Tsirkin */ 3065031f5e03SMichael S. Tsirkin return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES, 3066631ab46bSEric W. Biederman (unsigned int __user*)argp); 3067f663706aSKirill Tkhai } else if (cmd == TUNSETQUEUE) { 3068cde8b15fSJason Wang return tun_set_queue(file, &ifr); 3069f663706aSKirill Tkhai } else if (cmd == SIOCGSKNS) { 3070f663706aSKirill Tkhai if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 3071f663706aSKirill Tkhai return -EPERM; 3072f663706aSKirill Tkhai return open_related_ns(&net->ns, get_net_ns); 3073f663706aSKirill Tkhai } 3074631ab46bSEric W. Biederman 3075c8d68e6bSJason Wang ret = 0; 3076876bfd4dSHerbert Xu rtnl_lock(); 3077876bfd4dSHerbert Xu 30789484dc74Syuan linyu tun = tun_get(tfile); 30790f16bc13SGao Feng if (cmd == TUNSETIFF) { 30800f16bc13SGao Feng ret = -EEXIST; 30810f16bc13SGao Feng if (tun) 30820f16bc13SGao Feng goto unlock; 30830f16bc13SGao Feng 30841da177e4SLinus Torvalds ifr.ifr_name[IFNAMSIZ-1] = '\0'; 30851da177e4SLinus Torvalds 3086f2780d6dSKirill Tkhai ret = tun_set_iff(net, file, &ifr); 30871da177e4SLinus Torvalds 3088876bfd4dSHerbert Xu if (ret) 3089876bfd4dSHerbert Xu goto unlock; 30901da177e4SLinus Torvalds 309150857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 3092876bfd4dSHerbert Xu ret = -EFAULT; 3093876bfd4dSHerbert Xu goto unlock; 30941da177e4SLinus Torvalds } 3095fb7589a1SPavel Emelyanov if (cmd == TUNSETIFINDEX) { 3096fb7589a1SPavel Emelyanov ret = -EPERM; 3097fb7589a1SPavel Emelyanov if (tun) 3098fb7589a1SPavel Emelyanov goto unlock; 3099fb7589a1SPavel Emelyanov 3100fb7589a1SPavel Emelyanov ret = -EFAULT; 3101fb7589a1SPavel Emelyanov if (copy_from_user(&ifindex, argp, sizeof(ifindex))) 3102fb7589a1SPavel Emelyanov goto unlock; 3103fb7589a1SPavel Emelyanov 3104fb7589a1SPavel Emelyanov ret = 0; 3105fb7589a1SPavel Emelyanov tfile->ifindex = ifindex; 3106fb7589a1SPavel Emelyanov goto unlock; 3107fb7589a1SPavel Emelyanov } 31081da177e4SLinus Torvalds 3109876bfd4dSHerbert Xu ret = -EBADFD; 31101da177e4SLinus Torvalds if (!tun) 3111876bfd4dSHerbert Xu goto unlock; 31121da177e4SLinus Torvalds 31131e588338SJason Wang tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd); 31141da177e4SLinus Torvalds 31150c3e0e3bSKirill Tkhai net = dev_net(tun->dev); 3116631ab46bSEric W. Biederman ret = 0; 31171da177e4SLinus Torvalds switch (cmd) { 3118e3b99556SMark McLoughlin case TUNGETIFF: 311912132768SKirill Tkhai tun_get_iff(tun, &ifr); 3120e3b99556SMark McLoughlin 31213d407a80SPavel Emelyanov if (tfile->detached) 31223d407a80SPavel Emelyanov ifr.ifr_flags |= IFF_DETACH_QUEUE; 3123849c9b6fSPavel Emelyanov if (!tfile->socket.sk->sk_filter) 3124849c9b6fSPavel Emelyanov ifr.ifr_flags |= IFF_NOFILTER; 31253d407a80SPavel Emelyanov 312650857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 3127631ab46bSEric W. Biederman ret = -EFAULT; 3128e3b99556SMark McLoughlin break; 3129e3b99556SMark McLoughlin 31301da177e4SLinus Torvalds case TUNSETNOCSUM: 31311da177e4SLinus Torvalds /* Disable/Enable checksum */ 31321da177e4SLinus Torvalds 313388255375SMichał Mirosław /* [unimplemented] */ 313488255375SMichał Mirosław tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n", 31356b8a66eeSJoe Perches arg ? "disabled" : "enabled"); 31361da177e4SLinus Torvalds break; 31371da177e4SLinus Torvalds 31381da177e4SLinus Torvalds case TUNSETPERSIST: 313954f968d6SJason Wang /* Disable/Enable persist mode. Keep an extra reference to the 314054f968d6SJason Wang * module to prevent the module being unprobed. 314154f968d6SJason Wang */ 314240630b82SMichael S. Tsirkin if (arg && !(tun->flags & IFF_PERSIST)) { 314340630b82SMichael S. Tsirkin tun->flags |= IFF_PERSIST; 314454f968d6SJason Wang __module_get(THIS_MODULE); 314583c1f36fSSabrina Dubroca do_notify = true; 3146dd38bd85SJason Wang } 314740630b82SMichael S. Tsirkin if (!arg && (tun->flags & IFF_PERSIST)) { 314840630b82SMichael S. Tsirkin tun->flags &= ~IFF_PERSIST; 314954f968d6SJason Wang module_put(THIS_MODULE); 315083c1f36fSSabrina Dubroca do_notify = true; 315154f968d6SJason Wang } 31521da177e4SLinus Torvalds 31536b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "persist %s\n", 31546b8a66eeSJoe Perches arg ? "enabled" : "disabled"); 31551da177e4SLinus Torvalds break; 31561da177e4SLinus Torvalds 31571da177e4SLinus Torvalds case TUNSETOWNER: 31581da177e4SLinus Torvalds /* Set owner of the device */ 31590625c883SEric W. Biederman owner = make_kuid(current_user_ns(), arg); 31600625c883SEric W. Biederman if (!uid_valid(owner)) { 31610625c883SEric W. Biederman ret = -EINVAL; 31620625c883SEric W. Biederman break; 31630625c883SEric W. Biederman } 31640625c883SEric W. Biederman tun->owner = owner; 316583c1f36fSSabrina Dubroca do_notify = true; 31661e588338SJason Wang tun_debug(KERN_INFO, tun, "owner set to %u\n", 31670625c883SEric W. Biederman from_kuid(&init_user_ns, tun->owner)); 31681da177e4SLinus Torvalds break; 31691da177e4SLinus Torvalds 31708c644623SGuido Guenther case TUNSETGROUP: 31718c644623SGuido Guenther /* Set group of the device */ 31720625c883SEric W. Biederman group = make_kgid(current_user_ns(), arg); 31730625c883SEric W. Biederman if (!gid_valid(group)) { 31740625c883SEric W. Biederman ret = -EINVAL; 31750625c883SEric W. Biederman break; 31760625c883SEric W. Biederman } 31770625c883SEric W. Biederman tun->group = group; 317883c1f36fSSabrina Dubroca do_notify = true; 31791e588338SJason Wang tun_debug(KERN_INFO, tun, "group set to %u\n", 31800625c883SEric W. Biederman from_kgid(&init_user_ns, tun->group)); 31818c644623SGuido Guenther break; 31828c644623SGuido Guenther 3183ff4cc3acSMike Kershaw case TUNSETLINK: 3184ff4cc3acSMike Kershaw /* Only allow setting the type when the interface is down */ 3185ff4cc3acSMike Kershaw if (tun->dev->flags & IFF_UP) { 31866b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, 31876b8a66eeSJoe Perches "Linktype set failed because interface is up\n"); 318848abfe05SDavid S. Miller ret = -EBUSY; 3189ff4cc3acSMike Kershaw } else { 3190ff4cc3acSMike Kershaw tun->dev->type = (int) arg; 31916b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "linktype set to %d\n", 31926b8a66eeSJoe Perches tun->dev->type); 319348abfe05SDavid S. Miller ret = 0; 3194ff4cc3acSMike Kershaw } 3195631ab46bSEric W. Biederman break; 3196ff4cc3acSMike Kershaw 31971da177e4SLinus Torvalds #ifdef TUN_DEBUG 31981da177e4SLinus Torvalds case TUNSETDEBUG: 31991da177e4SLinus Torvalds tun->debug = arg; 32001da177e4SLinus Torvalds break; 32011da177e4SLinus Torvalds #endif 32025228ddc9SRusty Russell case TUNSETOFFLOAD: 320388255375SMichał Mirosław ret = set_offload(tun, arg); 3204631ab46bSEric W. Biederman break; 32055228ddc9SRusty Russell 3206f271b2ccSMax Krasnyansky case TUNSETTXFILTER: 3207f271b2ccSMax Krasnyansky /* Can be set only for TAPs */ 3208631ab46bSEric W. Biederman ret = -EINVAL; 320940630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 3210631ab46bSEric W. Biederman break; 3211c0e5a8c2SHarvey Harrison ret = update_filter(&tun->txflt, (void __user *)arg); 3212631ab46bSEric W. Biederman break; 32131da177e4SLinus Torvalds 32141da177e4SLinus Torvalds case SIOCGIFHWADDR: 3215b595076aSUwe Kleine-König /* Get hw address */ 3216f271b2ccSMax Krasnyansky memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); 3217f271b2ccSMax Krasnyansky ifr.ifr_hwaddr.sa_family = tun->dev->type; 321850857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 3219631ab46bSEric W. Biederman ret = -EFAULT; 3220631ab46bSEric W. Biederman break; 32211da177e4SLinus Torvalds 32221da177e4SLinus Torvalds case SIOCSIFHWADDR: 3223f271b2ccSMax Krasnyansky /* Set hw address */ 32246b8a66eeSJoe Perches tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n", 32256b8a66eeSJoe Perches ifr.ifr_hwaddr.sa_data); 322640102371SKim B. Heino 32273a37a963SPetr Machata ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr, NULL); 3228631ab46bSEric W. Biederman break; 322933dccbb0SHerbert Xu 323033dccbb0SHerbert Xu case TUNGETSNDBUF: 323154f968d6SJason Wang sndbuf = tfile->socket.sk->sk_sndbuf; 323233dccbb0SHerbert Xu if (copy_to_user(argp, &sndbuf, sizeof(sndbuf))) 323333dccbb0SHerbert Xu ret = -EFAULT; 323433dccbb0SHerbert Xu break; 323533dccbb0SHerbert Xu 323633dccbb0SHerbert Xu case TUNSETSNDBUF: 323733dccbb0SHerbert Xu if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) { 323833dccbb0SHerbert Xu ret = -EFAULT; 323933dccbb0SHerbert Xu break; 324033dccbb0SHerbert Xu } 324193161922SCraig Gallek if (sndbuf <= 0) { 324293161922SCraig Gallek ret = -EINVAL; 324393161922SCraig Gallek break; 324493161922SCraig Gallek } 324533dccbb0SHerbert Xu 3246c8d68e6bSJason Wang tun->sndbuf = sndbuf; 3247c8d68e6bSJason Wang tun_set_sndbuf(tun); 324833dccbb0SHerbert Xu break; 324933dccbb0SHerbert Xu 3250d9d52b51SMichael S. Tsirkin case TUNGETVNETHDRSZ: 3251d9d52b51SMichael S. Tsirkin vnet_hdr_sz = tun->vnet_hdr_sz; 3252d9d52b51SMichael S. Tsirkin if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz))) 3253d9d52b51SMichael S. Tsirkin ret = -EFAULT; 3254d9d52b51SMichael S. Tsirkin break; 3255d9d52b51SMichael S. Tsirkin 3256d9d52b51SMichael S. Tsirkin case TUNSETVNETHDRSZ: 3257d9d52b51SMichael S. Tsirkin if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) { 3258d9d52b51SMichael S. Tsirkin ret = -EFAULT; 3259d9d52b51SMichael S. Tsirkin break; 3260d9d52b51SMichael S. Tsirkin } 3261d9d52b51SMichael S. Tsirkin if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) { 3262d9d52b51SMichael S. Tsirkin ret = -EINVAL; 3263d9d52b51SMichael S. Tsirkin break; 3264d9d52b51SMichael S. Tsirkin } 3265d9d52b51SMichael S. Tsirkin 3266d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = vnet_hdr_sz; 3267d9d52b51SMichael S. Tsirkin break; 3268d9d52b51SMichael S. Tsirkin 32691cf8e410SMichael S. Tsirkin case TUNGETVNETLE: 32701cf8e410SMichael S. Tsirkin le = !!(tun->flags & TUN_VNET_LE); 32711cf8e410SMichael S. Tsirkin if (put_user(le, (int __user *)argp)) 32721cf8e410SMichael S. Tsirkin ret = -EFAULT; 32731cf8e410SMichael S. Tsirkin break; 32741cf8e410SMichael S. Tsirkin 32751cf8e410SMichael S. Tsirkin case TUNSETVNETLE: 32761cf8e410SMichael S. Tsirkin if (get_user(le, (int __user *)argp)) { 32771cf8e410SMichael S. Tsirkin ret = -EFAULT; 32781cf8e410SMichael S. Tsirkin break; 32791cf8e410SMichael S. Tsirkin } 32801cf8e410SMichael S. Tsirkin if (le) 32811cf8e410SMichael S. Tsirkin tun->flags |= TUN_VNET_LE; 32821cf8e410SMichael S. Tsirkin else 32831cf8e410SMichael S. Tsirkin tun->flags &= ~TUN_VNET_LE; 32841cf8e410SMichael S. Tsirkin break; 32851cf8e410SMichael S. Tsirkin 32868b8e658bSGreg Kurz case TUNGETVNETBE: 32878b8e658bSGreg Kurz ret = tun_get_vnet_be(tun, argp); 32888b8e658bSGreg Kurz break; 32898b8e658bSGreg Kurz 32908b8e658bSGreg Kurz case TUNSETVNETBE: 32918b8e658bSGreg Kurz ret = tun_set_vnet_be(tun, argp); 32928b8e658bSGreg Kurz break; 32938b8e658bSGreg Kurz 329499405162SMichael S. Tsirkin case TUNATTACHFILTER: 329599405162SMichael S. Tsirkin /* Can be set only for TAPs */ 329699405162SMichael S. Tsirkin ret = -EINVAL; 329740630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 329899405162SMichael S. Tsirkin break; 329999405162SMichael S. Tsirkin ret = -EFAULT; 330054f968d6SJason Wang if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog))) 330199405162SMichael S. Tsirkin break; 330299405162SMichael S. Tsirkin 3303c8d68e6bSJason Wang ret = tun_attach_filter(tun); 330499405162SMichael S. Tsirkin break; 330599405162SMichael S. Tsirkin 330699405162SMichael S. Tsirkin case TUNDETACHFILTER: 330799405162SMichael S. Tsirkin /* Can be set only for TAPs */ 330899405162SMichael S. Tsirkin ret = -EINVAL; 330940630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 331099405162SMichael S. Tsirkin break; 3311c8d68e6bSJason Wang ret = 0; 3312c8d68e6bSJason Wang tun_detach_filter(tun, tun->numqueues); 331399405162SMichael S. Tsirkin break; 331499405162SMichael S. Tsirkin 331576975e9cSPavel Emelyanov case TUNGETFILTER: 331676975e9cSPavel Emelyanov ret = -EINVAL; 331740630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 331876975e9cSPavel Emelyanov break; 331976975e9cSPavel Emelyanov ret = -EFAULT; 332076975e9cSPavel Emelyanov if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog))) 332176975e9cSPavel Emelyanov break; 332276975e9cSPavel Emelyanov ret = 0; 332376975e9cSPavel Emelyanov break; 332476975e9cSPavel Emelyanov 332596f84061SJason Wang case TUNSETSTEERINGEBPF: 3326cd5681d7SJason Wang ret = tun_set_ebpf(tun, &tun->steering_prog, argp); 332796f84061SJason Wang break; 332896f84061SJason Wang 3329aff3d70aSJason Wang case TUNSETFILTEREBPF: 3330aff3d70aSJason Wang ret = tun_set_ebpf(tun, &tun->filter_prog, argp); 3331aff3d70aSJason Wang break; 3332aff3d70aSJason Wang 333326d31925SNicolas Dichtel case TUNSETCARRIER: 333426d31925SNicolas Dichtel ret = -EFAULT; 333526d31925SNicolas Dichtel if (copy_from_user(&carrier, argp, sizeof(carrier))) 333626d31925SNicolas Dichtel goto unlock; 333726d31925SNicolas Dichtel 333826d31925SNicolas Dichtel ret = tun_net_change_carrier(tun->dev, (bool)carrier); 333926d31925SNicolas Dichtel break; 334026d31925SNicolas Dichtel 33410c3e0e3bSKirill Tkhai case TUNGETDEVNETNS: 33420c3e0e3bSKirill Tkhai ret = -EPERM; 33430c3e0e3bSKirill Tkhai if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 33440c3e0e3bSKirill Tkhai goto unlock; 33450c3e0e3bSKirill Tkhai ret = open_related_ns(&net->ns, get_net_ns); 33460c3e0e3bSKirill Tkhai break; 33470c3e0e3bSKirill Tkhai 33481da177e4SLinus Torvalds default: 3349631ab46bSEric W. Biederman ret = -EINVAL; 3350631ab46bSEric W. Biederman break; 3351ee289b64SJoe Perches } 33521da177e4SLinus Torvalds 335383c1f36fSSabrina Dubroca if (do_notify) 335483c1f36fSSabrina Dubroca netdev_state_change(tun->dev); 335583c1f36fSSabrina Dubroca 3356876bfd4dSHerbert Xu unlock: 3357876bfd4dSHerbert Xu rtnl_unlock(); 3358876bfd4dSHerbert Xu if (tun) 3359631ab46bSEric W. Biederman tun_put(tun); 3360631ab46bSEric W. Biederman return ret; 33611da177e4SLinus Torvalds } 33621da177e4SLinus Torvalds 336350857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file, 336450857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 336550857e2aSArnd Bergmann { 336650857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq)); 336750857e2aSArnd Bergmann } 336850857e2aSArnd Bergmann 336950857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 337050857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file, 337150857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 337250857e2aSArnd Bergmann { 337350857e2aSArnd Bergmann switch (cmd) { 337450857e2aSArnd Bergmann case TUNSETIFF: 337550857e2aSArnd Bergmann case TUNGETIFF: 337650857e2aSArnd Bergmann case TUNSETTXFILTER: 337750857e2aSArnd Bergmann case TUNGETSNDBUF: 337850857e2aSArnd Bergmann case TUNSETSNDBUF: 337950857e2aSArnd Bergmann case SIOCGIFHWADDR: 338050857e2aSArnd Bergmann case SIOCSIFHWADDR: 338150857e2aSArnd Bergmann arg = (unsigned long)compat_ptr(arg); 338250857e2aSArnd Bergmann break; 338350857e2aSArnd Bergmann default: 338450857e2aSArnd Bergmann arg = (compat_ulong_t)arg; 338550857e2aSArnd Bergmann break; 338650857e2aSArnd Bergmann } 338750857e2aSArnd Bergmann 338850857e2aSArnd Bergmann /* 338950857e2aSArnd Bergmann * compat_ifreq is shorter than ifreq, so we must not access beyond 339050857e2aSArnd Bergmann * the end of that structure. All fields that are used in this 339150857e2aSArnd Bergmann * driver are compatible though, we don't need to convert the 339250857e2aSArnd Bergmann * contents. 339350857e2aSArnd Bergmann */ 339450857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq)); 339550857e2aSArnd Bergmann } 339650857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */ 339750857e2aSArnd Bergmann 33981da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on) 33991da177e4SLinus Torvalds { 340054f968d6SJason Wang struct tun_file *tfile = file->private_data; 34011da177e4SLinus Torvalds int ret; 34021da177e4SLinus Torvalds 340354f968d6SJason Wang if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) 34049d319522SJonathan Corbet goto out; 34051da177e4SLinus Torvalds 34061da177e4SLinus Torvalds if (on) { 340701919134SEric W. Biederman __f_setown(file, task_pid(current), PIDTYPE_TGID, 0); 340854f968d6SJason Wang tfile->flags |= TUN_FASYNC; 34091da177e4SLinus Torvalds } else 341054f968d6SJason Wang tfile->flags &= ~TUN_FASYNC; 34119d319522SJonathan Corbet ret = 0; 34129d319522SJonathan Corbet out: 34139d319522SJonathan Corbet return ret; 34141da177e4SLinus Torvalds } 34151da177e4SLinus Torvalds 34161da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file) 34171da177e4SLinus Torvalds { 3418140e807dSEric W. Biederman struct net *net = current->nsproxy->net_ns; 3419631ab46bSEric W. Biederman struct tun_file *tfile; 3420deed49fbSThomas Gleixner 34216b8a66eeSJoe Perches DBG1(KERN_INFO, "tunX: tun_chr_open\n"); 3422631ab46bSEric W. Biederman 3423140e807dSEric W. Biederman tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, 342411aa9c28SEric W. Biederman &tun_proto, 0); 3425631ab46bSEric W. Biederman if (!tfile) 3426631ab46bSEric W. Biederman return -ENOMEM; 3427b196d88aSJason Wang if (ptr_ring_init(&tfile->tx_ring, 0, GFP_KERNEL)) { 3428b196d88aSJason Wang sk_free(&tfile->sk); 3429b196d88aSJason Wang return -ENOMEM; 3430b196d88aSJason Wang } 3431b196d88aSJason Wang 3432c7256f57SEric Dumazet mutex_init(&tfile->napi_mutex); 3433c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 343454f968d6SJason Wang tfile->flags = 0; 3435fb7589a1SPavel Emelyanov tfile->ifindex = 0; 343654f968d6SJason Wang 343754f968d6SJason Wang init_waitqueue_head(&tfile->wq.wait); 34389e641bdcSXi Wang RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq); 343954f968d6SJason Wang 344054f968d6SJason Wang tfile->socket.file = file; 344154f968d6SJason Wang tfile->socket.ops = &tun_socket_ops; 344254f968d6SJason Wang 344354f968d6SJason Wang sock_init_data(&tfile->socket, &tfile->sk); 344454f968d6SJason Wang 344554f968d6SJason Wang tfile->sk.sk_write_space = tun_sock_write_space; 344654f968d6SJason Wang tfile->sk.sk_sndbuf = INT_MAX; 344754f968d6SJason Wang 3448631ab46bSEric W. Biederman file->private_data = tfile; 34494008e97fSJason Wang INIT_LIST_HEAD(&tfile->next); 345054f968d6SJason Wang 345119a6afb2SJason Wang sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); 345219a6afb2SJason Wang 34531da177e4SLinus Torvalds return 0; 34541da177e4SLinus Torvalds } 34551da177e4SLinus Torvalds 34561da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file) 34571da177e4SLinus Torvalds { 3458631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 34591da177e4SLinus Torvalds 3460c8d68e6bSJason Wang tun_detach(tfile, true); 34611da177e4SLinus Torvalds 34621da177e4SLinus Torvalds return 0; 34631da177e4SLinus Torvalds } 34641da177e4SLinus Torvalds 346593e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS 34669484dc74Syuan linyu static void tun_chr_show_fdinfo(struct seq_file *m, struct file *file) 346793e14b6dSMasatake YAMATO { 34689484dc74Syuan linyu struct tun_file *tfile = file->private_data; 346993e14b6dSMasatake YAMATO struct tun_struct *tun; 347093e14b6dSMasatake YAMATO struct ifreq ifr; 347193e14b6dSMasatake YAMATO 347293e14b6dSMasatake YAMATO memset(&ifr, 0, sizeof(ifr)); 347393e14b6dSMasatake YAMATO 347493e14b6dSMasatake YAMATO rtnl_lock(); 34759484dc74Syuan linyu tun = tun_get(tfile); 347693e14b6dSMasatake YAMATO if (tun) 347712132768SKirill Tkhai tun_get_iff(tun, &ifr); 347893e14b6dSMasatake YAMATO rtnl_unlock(); 347993e14b6dSMasatake YAMATO 348093e14b6dSMasatake YAMATO if (tun) 348193e14b6dSMasatake YAMATO tun_put(tun); 348293e14b6dSMasatake YAMATO 3483a3816ab0SJoe Perches seq_printf(m, "iff:\t%s\n", ifr.ifr_name); 348493e14b6dSMasatake YAMATO } 348593e14b6dSMasatake YAMATO #endif 348693e14b6dSMasatake YAMATO 3487d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = { 34881da177e4SLinus Torvalds .owner = THIS_MODULE, 34891da177e4SLinus Torvalds .llseek = no_llseek, 34909b067034SAl Viro .read_iter = tun_chr_read_iter, 3491f5ff53b4SAl Viro .write_iter = tun_chr_write_iter, 34921da177e4SLinus Torvalds .poll = tun_chr_poll, 3493876bfd4dSHerbert Xu .unlocked_ioctl = tun_chr_ioctl, 349450857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 349550857e2aSArnd Bergmann .compat_ioctl = tun_chr_compat_ioctl, 349650857e2aSArnd Bergmann #endif 34971da177e4SLinus Torvalds .open = tun_chr_open, 34981da177e4SLinus Torvalds .release = tun_chr_close, 349993e14b6dSMasatake YAMATO .fasync = tun_chr_fasync, 350093e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS 350193e14b6dSMasatake YAMATO .show_fdinfo = tun_chr_show_fdinfo, 350293e14b6dSMasatake YAMATO #endif 35031da177e4SLinus Torvalds }; 35041da177e4SLinus Torvalds 35051da177e4SLinus Torvalds static struct miscdevice tun_miscdev = { 35061da177e4SLinus Torvalds .minor = TUN_MINOR, 35071da177e4SLinus Torvalds .name = "tun", 3508e454cea2SKay Sievers .nodename = "net/tun", 35091da177e4SLinus Torvalds .fops = &tun_fops, 35101da177e4SLinus Torvalds }; 35111da177e4SLinus Torvalds 35121da177e4SLinus Torvalds /* ethtool interface */ 35131da177e4SLinus Torvalds 35144e24f2ddSChas Williams static void tun_default_link_ksettings(struct net_device *dev, 351529ccc49dSPhilippe Reynes struct ethtool_link_ksettings *cmd) 35161da177e4SLinus Torvalds { 351729ccc49dSPhilippe Reynes ethtool_link_ksettings_zero_link_mode(cmd, supported); 351829ccc49dSPhilippe Reynes ethtool_link_ksettings_zero_link_mode(cmd, advertising); 351929ccc49dSPhilippe Reynes cmd->base.speed = SPEED_10; 352029ccc49dSPhilippe Reynes cmd->base.duplex = DUPLEX_FULL; 352129ccc49dSPhilippe Reynes cmd->base.port = PORT_TP; 352229ccc49dSPhilippe Reynes cmd->base.phy_address = 0; 352329ccc49dSPhilippe Reynes cmd->base.autoneg = AUTONEG_DISABLE; 35244e24f2ddSChas Williams } 35254e24f2ddSChas Williams 35264e24f2ddSChas Williams static int tun_get_link_ksettings(struct net_device *dev, 35274e24f2ddSChas Williams struct ethtool_link_ksettings *cmd) 35284e24f2ddSChas Williams { 35294e24f2ddSChas Williams struct tun_struct *tun = netdev_priv(dev); 35304e24f2ddSChas Williams 35314e24f2ddSChas Williams memcpy(cmd, &tun->link_ksettings, sizeof(*cmd)); 35324e24f2ddSChas Williams return 0; 35334e24f2ddSChas Williams } 35344e24f2ddSChas Williams 35354e24f2ddSChas Williams static int tun_set_link_ksettings(struct net_device *dev, 35364e24f2ddSChas Williams const struct ethtool_link_ksettings *cmd) 35374e24f2ddSChas Williams { 35384e24f2ddSChas Williams struct tun_struct *tun = netdev_priv(dev); 35394e24f2ddSChas Williams 35404e24f2ddSChas Williams memcpy(&tun->link_ksettings, cmd, sizeof(*cmd)); 35411da177e4SLinus Torvalds return 0; 35421da177e4SLinus Torvalds } 35431da177e4SLinus Torvalds 35441da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) 35451da177e4SLinus Torvalds { 35461da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 35471da177e4SLinus Torvalds 354833a5ba14SRick Jones strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); 354933a5ba14SRick Jones strlcpy(info->version, DRV_VERSION, sizeof(info->version)); 35501da177e4SLinus Torvalds 35511da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 355240630b82SMichael S. Tsirkin case IFF_TUN: 355333a5ba14SRick Jones strlcpy(info->bus_info, "tun", sizeof(info->bus_info)); 35541da177e4SLinus Torvalds break; 355540630b82SMichael S. Tsirkin case IFF_TAP: 355633a5ba14SRick Jones strlcpy(info->bus_info, "tap", sizeof(info->bus_info)); 35571da177e4SLinus Torvalds break; 35581da177e4SLinus Torvalds } 35591da177e4SLinus Torvalds } 35601da177e4SLinus Torvalds 35611da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev) 35621da177e4SLinus Torvalds { 35631da177e4SLinus Torvalds #ifdef TUN_DEBUG 35641da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 35651da177e4SLinus Torvalds return tun->debug; 35661da177e4SLinus Torvalds #else 35671da177e4SLinus Torvalds return -EOPNOTSUPP; 35681da177e4SLinus Torvalds #endif 35691da177e4SLinus Torvalds } 35701da177e4SLinus Torvalds 35711da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value) 35721da177e4SLinus Torvalds { 35731da177e4SLinus Torvalds #ifdef TUN_DEBUG 35741da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 35751da177e4SLinus Torvalds tun->debug = value; 35761da177e4SLinus Torvalds #endif 35771da177e4SLinus Torvalds } 35781da177e4SLinus Torvalds 35795503fcecSJason Wang static int tun_get_coalesce(struct net_device *dev, 35805503fcecSJason Wang struct ethtool_coalesce *ec) 35815503fcecSJason Wang { 35825503fcecSJason Wang struct tun_struct *tun = netdev_priv(dev); 35835503fcecSJason Wang 35845503fcecSJason Wang ec->rx_max_coalesced_frames = tun->rx_batched; 35855503fcecSJason Wang 35865503fcecSJason Wang return 0; 35875503fcecSJason Wang } 35885503fcecSJason Wang 35895503fcecSJason Wang static int tun_set_coalesce(struct net_device *dev, 35905503fcecSJason Wang struct ethtool_coalesce *ec) 35915503fcecSJason Wang { 35925503fcecSJason Wang struct tun_struct *tun = netdev_priv(dev); 35935503fcecSJason Wang 35945503fcecSJason Wang if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT) 35955503fcecSJason Wang tun->rx_batched = NAPI_POLL_WEIGHT; 35965503fcecSJason Wang else 35975503fcecSJason Wang tun->rx_batched = ec->rx_max_coalesced_frames; 35985503fcecSJason Wang 35995503fcecSJason Wang return 0; 36005503fcecSJason Wang } 36015503fcecSJason Wang 36027282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = { 36031da177e4SLinus Torvalds .get_drvinfo = tun_get_drvinfo, 36041da177e4SLinus Torvalds .get_msglevel = tun_get_msglevel, 36051da177e4SLinus Torvalds .set_msglevel = tun_set_msglevel, 3606bee31369SNolan Leake .get_link = ethtool_op_get_link, 3607eda29772SRichard Cochran .get_ts_info = ethtool_op_get_ts_info, 36085503fcecSJason Wang .get_coalesce = tun_get_coalesce, 36095503fcecSJason Wang .set_coalesce = tun_set_coalesce, 361029ccc49dSPhilippe Reynes .get_link_ksettings = tun_get_link_ksettings, 36114e24f2ddSChas Williams .set_link_ksettings = tun_set_link_ksettings, 36121da177e4SLinus Torvalds }; 36131da177e4SLinus Torvalds 36141576d986SJason Wang static int tun_queue_resize(struct tun_struct *tun) 36151576d986SJason Wang { 36161576d986SJason Wang struct net_device *dev = tun->dev; 36171576d986SJason Wang struct tun_file *tfile; 36185990a305SJason Wang struct ptr_ring **rings; 36191576d986SJason Wang int n = tun->numqueues + tun->numdisabled; 36201576d986SJason Wang int ret, i; 36211576d986SJason Wang 36225990a305SJason Wang rings = kmalloc_array(n, sizeof(*rings), GFP_KERNEL); 36235990a305SJason Wang if (!rings) 36241576d986SJason Wang return -ENOMEM; 36251576d986SJason Wang 36261576d986SJason Wang for (i = 0; i < tun->numqueues; i++) { 36271576d986SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 36285990a305SJason Wang rings[i] = &tfile->tx_ring; 36291576d986SJason Wang } 36301576d986SJason Wang list_for_each_entry(tfile, &tun->disabled, next) 36315990a305SJason Wang rings[i++] = &tfile->tx_ring; 36321576d986SJason Wang 36335990a305SJason Wang ret = ptr_ring_resize_multiple(rings, n, 36345990a305SJason Wang dev->tx_queue_len, GFP_KERNEL, 3635fc72d1d5SJason Wang tun_ptr_free); 36361576d986SJason Wang 36375990a305SJason Wang kfree(rings); 36381576d986SJason Wang return ret; 36391576d986SJason Wang } 36401576d986SJason Wang 36411576d986SJason Wang static int tun_device_event(struct notifier_block *unused, 36421576d986SJason Wang unsigned long event, void *ptr) 36431576d986SJason Wang { 36441576d986SJason Wang struct net_device *dev = netdev_notifier_info_to_dev(ptr); 36451576d986SJason Wang struct tun_struct *tun = netdev_priv(dev); 36461576d986SJason Wang 364786dfb4acSCraig Gallek if (dev->rtnl_link_ops != &tun_link_ops) 364886dfb4acSCraig Gallek return NOTIFY_DONE; 364986dfb4acSCraig Gallek 36501576d986SJason Wang switch (event) { 36511576d986SJason Wang case NETDEV_CHANGE_TX_QUEUE_LEN: 36521576d986SJason Wang if (tun_queue_resize(tun)) 36531576d986SJason Wang return NOTIFY_BAD; 36541576d986SJason Wang break; 36551576d986SJason Wang default: 36561576d986SJason Wang break; 36571576d986SJason Wang } 36581576d986SJason Wang 36591576d986SJason Wang return NOTIFY_DONE; 36601576d986SJason Wang } 36611576d986SJason Wang 36621576d986SJason Wang static struct notifier_block tun_notifier_block __read_mostly = { 36631576d986SJason Wang .notifier_call = tun_device_event, 36641576d986SJason Wang }; 366579d17604SPavel Emelyanov 36661da177e4SLinus Torvalds static int __init tun_init(void) 36671da177e4SLinus Torvalds { 36681da177e4SLinus Torvalds int ret = 0; 36691da177e4SLinus Torvalds 36706b8a66eeSJoe Perches pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION); 36711da177e4SLinus Torvalds 3672f019a7a5SEric W. Biederman ret = rtnl_link_register(&tun_link_ops); 367379d17604SPavel Emelyanov if (ret) { 36746b8a66eeSJoe Perches pr_err("Can't register link_ops\n"); 3675f019a7a5SEric W. Biederman goto err_linkops; 367679d17604SPavel Emelyanov } 367779d17604SPavel Emelyanov 36781da177e4SLinus Torvalds ret = misc_register(&tun_miscdev); 367979d17604SPavel Emelyanov if (ret) { 36806b8a66eeSJoe Perches pr_err("Can't register misc device %d\n", TUN_MINOR); 368179d17604SPavel Emelyanov goto err_misc; 368279d17604SPavel Emelyanov } 36831576d986SJason Wang 36845edfbd3cSTonghao Zhang ret = register_netdevice_notifier(&tun_notifier_block); 36855edfbd3cSTonghao Zhang if (ret) { 36865edfbd3cSTonghao Zhang pr_err("Can't register netdevice notifier\n"); 36875edfbd3cSTonghao Zhang goto err_notifier; 36885edfbd3cSTonghao Zhang } 36895edfbd3cSTonghao Zhang 369079d17604SPavel Emelyanov return 0; 36915edfbd3cSTonghao Zhang 36925edfbd3cSTonghao Zhang err_notifier: 36935edfbd3cSTonghao Zhang misc_deregister(&tun_miscdev); 369479d17604SPavel Emelyanov err_misc: 3695f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 3696f019a7a5SEric W. Biederman err_linkops: 36971da177e4SLinus Torvalds return ret; 36981da177e4SLinus Torvalds } 36991da177e4SLinus Torvalds 37001da177e4SLinus Torvalds static void tun_cleanup(void) 37011da177e4SLinus Torvalds { 37021da177e4SLinus Torvalds misc_deregister(&tun_miscdev); 3703f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 37041576d986SJason Wang unregister_netdevice_notifier(&tun_notifier_block); 37051da177e4SLinus Torvalds } 37061da177e4SLinus Torvalds 370705c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file. Returns error unless file is 370805c2828cSMichael S. Tsirkin * attached to a device. The returned object works like a packet socket, it 370905c2828cSMichael S. Tsirkin * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for 371005c2828cSMichael S. Tsirkin * holding a reference to the file for as long as the socket is in use. */ 371105c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file) 371205c2828cSMichael S. Tsirkin { 37136e914fc7SJason Wang struct tun_file *tfile; 371405c2828cSMichael S. Tsirkin if (file->f_op != &tun_fops) 371505c2828cSMichael S. Tsirkin return ERR_PTR(-EINVAL); 37166e914fc7SJason Wang tfile = file->private_data; 37176e914fc7SJason Wang if (!tfile) 371805c2828cSMichael S. Tsirkin return ERR_PTR(-EBADFD); 371954f968d6SJason Wang return &tfile->socket; 372005c2828cSMichael S. Tsirkin } 372105c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket); 372205c2828cSMichael S. Tsirkin 37235990a305SJason Wang struct ptr_ring *tun_get_tx_ring(struct file *file) 372483339c6bSJason Wang { 372583339c6bSJason Wang struct tun_file *tfile; 372683339c6bSJason Wang 372783339c6bSJason Wang if (file->f_op != &tun_fops) 372883339c6bSJason Wang return ERR_PTR(-EINVAL); 372983339c6bSJason Wang tfile = file->private_data; 373083339c6bSJason Wang if (!tfile) 373183339c6bSJason Wang return ERR_PTR(-EBADFD); 37325990a305SJason Wang return &tfile->tx_ring; 373383339c6bSJason Wang } 37345990a305SJason Wang EXPORT_SYMBOL_GPL(tun_get_tx_ring); 373583339c6bSJason Wang 37361da177e4SLinus Torvalds module_init(tun_init); 37371da177e4SLinus Torvalds module_exit(tun_cleanup); 37381da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION); 37391da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT); 37401da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 37411da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR); 3742578454ffSKay Sievers MODULE_ALIAS("devname:net/tun"); 3743