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> 471da177e4SLinus Torvalds #include <linux/major.h> 481da177e4SLinus Torvalds #include <linux/slab.h> 491da177e4SLinus Torvalds #include <linux/poll.h> 501da177e4SLinus Torvalds #include <linux/fcntl.h> 511da177e4SLinus Torvalds #include <linux/init.h> 521da177e4SLinus Torvalds #include <linux/skbuff.h> 531da177e4SLinus Torvalds #include <linux/netdevice.h> 541da177e4SLinus Torvalds #include <linux/etherdevice.h> 551da177e4SLinus Torvalds #include <linux/miscdevice.h> 561da177e4SLinus Torvalds #include <linux/ethtool.h> 571da177e4SLinus Torvalds #include <linux/rtnetlink.h> 5850857e2aSArnd Bergmann #include <linux/compat.h> 591da177e4SLinus Torvalds #include <linux/if.h> 601da177e4SLinus Torvalds #include <linux/if_arp.h> 611da177e4SLinus Torvalds #include <linux/if_ether.h> 621da177e4SLinus Torvalds #include <linux/if_tun.h> 636680ec68SJason Wang #include <linux/if_vlan.h> 641da177e4SLinus Torvalds #include <linux/crc32.h> 65d647a591SPavel Emelyanov #include <linux/nsproxy.h> 66f43798c2SRusty Russell #include <linux/virtio_net.h> 6799405162SMichael S. Tsirkin #include <linux/rcupdate.h> 68881d966bSEric W. Biederman #include <net/net_namespace.h> 6979d17604SPavel Emelyanov #include <net/netns/generic.h> 70f019a7a5SEric W. Biederman #include <net/rtnetlink.h> 7133dccbb0SHerbert Xu #include <net/sock.h> 7293e14b6dSMasatake YAMATO #include <linux/seq_file.h> 73e0b46d0eSHerbert Xu #include <linux/uio.h> 741576d986SJason Wang #include <linux/skb_array.h> 751da177e4SLinus Torvalds 767c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 771da177e4SLinus Torvalds 7814daa021SRusty Russell /* Uncomment to enable debugging */ 7914daa021SRusty Russell /* #define TUN_DEBUG 1 */ 8014daa021SRusty Russell 811da177e4SLinus Torvalds #ifdef TUN_DEBUG 821da177e4SLinus Torvalds static int debug; 8314daa021SRusty Russell 846b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...) \ 856b8a66eeSJoe Perches do { \ 866b8a66eeSJoe Perches if (tun->debug) \ 876b8a66eeSJoe Perches netdev_printk(level, tun->dev, fmt, ##args); \ 886b8a66eeSJoe Perches } while (0) 896b8a66eeSJoe Perches #define DBG1(level, fmt, args...) \ 906b8a66eeSJoe Perches do { \ 916b8a66eeSJoe Perches if (debug == 2) \ 926b8a66eeSJoe Perches printk(level fmt, ##args); \ 936b8a66eeSJoe Perches } while (0) 9414daa021SRusty Russell #else 956b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...) \ 966b8a66eeSJoe Perches do { \ 976b8a66eeSJoe Perches if (0) \ 986b8a66eeSJoe Perches netdev_printk(level, tun->dev, fmt, ##args); \ 996b8a66eeSJoe Perches } while (0) 1006b8a66eeSJoe Perches #define DBG1(level, fmt, args...) \ 1016b8a66eeSJoe Perches do { \ 1026b8a66eeSJoe Perches if (0) \ 1036b8a66eeSJoe Perches printk(level fmt, ##args); \ 1046b8a66eeSJoe Perches } while (0) 1051da177e4SLinus Torvalds #endif 1061da177e4SLinus Torvalds 107031f5e03SMichael S. Tsirkin /* TUN device flags */ 108031f5e03SMichael S. Tsirkin 109031f5e03SMichael S. Tsirkin /* IFF_ATTACH_QUEUE is never stored in device flags, 110031f5e03SMichael S. Tsirkin * overload it to mean fasync when stored there. 111031f5e03SMichael S. Tsirkin */ 112031f5e03SMichael S. Tsirkin #define TUN_FASYNC IFF_ATTACH_QUEUE 1131cf8e410SMichael S. Tsirkin /* High bits in flags field are unused. */ 1141cf8e410SMichael S. Tsirkin #define TUN_VNET_LE 0x80000000 1158b8e658bSGreg Kurz #define TUN_VNET_BE 0x40000000 116031f5e03SMichael S. Tsirkin 117031f5e03SMichael S. Tsirkin #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \ 1181cf8e410SMichael S. Tsirkin IFF_MULTI_QUEUE) 1190690899bSMichael S. Tsirkin #define GOODCOPY_LEN 128 1200690899bSMichael S. Tsirkin 121f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8 122f271b2ccSMax Krasnyansky struct tap_filter { 123f271b2ccSMax Krasnyansky unsigned int count; /* Number of addrs. Zero means disabled */ 124f271b2ccSMax Krasnyansky u32 mask[2]; /* Mask of the hashed addrs */ 125f271b2ccSMax Krasnyansky unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN]; 126f271b2ccSMax Krasnyansky }; 127f271b2ccSMax Krasnyansky 128baf71c5cSPankaj Gupta /* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal 129baf71c5cSPankaj Gupta * to max number of VCPUs in guest. */ 130baf71c5cSPankaj Gupta #define MAX_TAP_QUEUES 256 131b8732fb7SJason Wang #define MAX_TAP_FLOWS 4096 132c8d68e6bSJason Wang 13396442e42SJason Wang #define TUN_FLOW_EXPIRE (3 * HZ) 13496442e42SJason Wang 135608b9977SPaolo Abeni struct tun_pcpu_stats { 136608b9977SPaolo Abeni u64 rx_packets; 137608b9977SPaolo Abeni u64 rx_bytes; 138608b9977SPaolo Abeni u64 tx_packets; 139608b9977SPaolo Abeni u64 tx_bytes; 140608b9977SPaolo Abeni struct u64_stats_sync syncp; 141608b9977SPaolo Abeni u32 rx_dropped; 142608b9977SPaolo Abeni u32 tx_dropped; 143608b9977SPaolo Abeni u32 rx_frame_errors; 144608b9977SPaolo Abeni }; 145608b9977SPaolo Abeni 14654f968d6SJason Wang /* A tun_file connects an open character device to a tuntap netdevice. It 14792d4ea6eSstephen hemminger * also contains all socket related structures (except sock_fprog and tap_filter) 14854f968d6SJason Wang * to serve as one transmit queue for tuntap device. The sock_fprog and 14954f968d6SJason Wang * tap_filter were kept in tun_struct since they were used for filtering for the 15036fe8c09SRami Rosen * netdevice not for a specific queue (at least I didn't see the requirement for 15154f968d6SJason Wang * this). 1526e914fc7SJason Wang * 1536e914fc7SJason Wang * RCU usage: 15436fe8c09SRami Rosen * The tun_file and tun_struct are loosely coupled, the pointer from one to the 1556e914fc7SJason Wang * other can only be read while rcu_read_lock or rtnl_lock is held. 15654f968d6SJason Wang */ 157631ab46bSEric W. Biederman struct tun_file { 15854f968d6SJason Wang struct sock sk; 15954f968d6SJason Wang struct socket socket; 16054f968d6SJason Wang struct socket_wq wq; 1616e914fc7SJason Wang struct tun_struct __rcu *tun; 16254f968d6SJason Wang struct fasync_struct *fasync; 16354f968d6SJason Wang /* only used for fasnyc */ 16454f968d6SJason Wang unsigned int flags; 165fb7589a1SPavel Emelyanov union { 166c8d68e6bSJason Wang u16 queue_index; 167fb7589a1SPavel Emelyanov unsigned int ifindex; 168fb7589a1SPavel Emelyanov }; 1694008e97fSJason Wang struct list_head next; 1704008e97fSJason Wang struct tun_struct *detached; 1711576d986SJason Wang struct skb_array tx_array; 172631ab46bSEric W. Biederman }; 173631ab46bSEric W. Biederman 17496442e42SJason Wang struct tun_flow_entry { 17596442e42SJason Wang struct hlist_node hash_link; 17696442e42SJason Wang struct rcu_head rcu; 17796442e42SJason Wang struct tun_struct *tun; 17896442e42SJason Wang 17996442e42SJason Wang u32 rxhash; 1809bc88939STom Herbert u32 rps_rxhash; 18196442e42SJason Wang int queue_index; 18296442e42SJason Wang unsigned long updated; 18396442e42SJason Wang }; 18496442e42SJason Wang 18596442e42SJason Wang #define TUN_NUM_FLOW_ENTRIES 1024 18696442e42SJason Wang 18754f968d6SJason Wang /* Since the socket were moved to tun_file, to preserve the behavior of persist 18836fe8c09SRami Rosen * device, socket filter, sndbuf and vnet header size were restore when the 18954f968d6SJason Wang * file were attached to a persist device. 19054f968d6SJason Wang */ 19114daa021SRusty Russell struct tun_struct { 192c8d68e6bSJason Wang struct tun_file __rcu *tfiles[MAX_TAP_QUEUES]; 193c8d68e6bSJason Wang unsigned int numqueues; 194f271b2ccSMax Krasnyansky unsigned int flags; 1950625c883SEric W. Biederman kuid_t owner; 1960625c883SEric W. Biederman kgid_t group; 19714daa021SRusty Russell 19814daa021SRusty Russell struct net_device *dev; 199c8f44affSMichał Mirosław netdev_features_t set_features; 20088255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \ 201e3e3c423SVlad Yasevich NETIF_F_TSO6|NETIF_F_UFO) 202d9d52b51SMichael S. Tsirkin 203eaea34b2SPaolo Abeni int align; 204d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 20554f968d6SJason Wang int sndbuf; 20654f968d6SJason Wang struct tap_filter txflt; 20754f968d6SJason Wang struct sock_fprog fprog; 20854f968d6SJason Wang /* protected by rtnl lock */ 20954f968d6SJason Wang bool filter_attached; 21014daa021SRusty Russell #ifdef TUN_DEBUG 21114daa021SRusty Russell int debug; 21214daa021SRusty Russell #endif 21396442e42SJason Wang spinlock_t lock; 21496442e42SJason Wang struct hlist_head flows[TUN_NUM_FLOW_ENTRIES]; 21596442e42SJason Wang struct timer_list flow_gc_timer; 21696442e42SJason Wang unsigned long ageing_time; 2174008e97fSJason Wang unsigned int numdisabled; 2184008e97fSJason Wang struct list_head disabled; 2195dbbaf2dSPaul Moore void *security; 220b8732fb7SJason Wang u32 flow_count; 221*5503fcecSJason Wang u32 rx_batched; 222608b9977SPaolo Abeni struct tun_pcpu_stats __percpu *pcpu_stats; 22314daa021SRusty Russell }; 22414daa021SRusty Russell 2258b8e658bSGreg Kurz #ifdef CONFIG_TUN_VNET_CROSS_LE 2268b8e658bSGreg Kurz static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) 2278b8e658bSGreg Kurz { 2288b8e658bSGreg Kurz return tun->flags & TUN_VNET_BE ? false : 2298b8e658bSGreg Kurz virtio_legacy_is_little_endian(); 2308b8e658bSGreg Kurz } 2318b8e658bSGreg Kurz 2328b8e658bSGreg Kurz static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) 2338b8e658bSGreg Kurz { 2348b8e658bSGreg Kurz int be = !!(tun->flags & TUN_VNET_BE); 2358b8e658bSGreg Kurz 2368b8e658bSGreg Kurz if (put_user(be, argp)) 2378b8e658bSGreg Kurz return -EFAULT; 2388b8e658bSGreg Kurz 2398b8e658bSGreg Kurz return 0; 2408b8e658bSGreg Kurz } 2418b8e658bSGreg Kurz 2428b8e658bSGreg Kurz static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) 2438b8e658bSGreg Kurz { 2448b8e658bSGreg Kurz int be; 2458b8e658bSGreg Kurz 2468b8e658bSGreg Kurz if (get_user(be, argp)) 2478b8e658bSGreg Kurz return -EFAULT; 2488b8e658bSGreg Kurz 2498b8e658bSGreg Kurz if (be) 2508b8e658bSGreg Kurz tun->flags |= TUN_VNET_BE; 2518b8e658bSGreg Kurz else 2528b8e658bSGreg Kurz tun->flags &= ~TUN_VNET_BE; 2538b8e658bSGreg Kurz 2548b8e658bSGreg Kurz return 0; 2558b8e658bSGreg Kurz } 2568b8e658bSGreg Kurz #else 2578b8e658bSGreg Kurz static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) 2588b8e658bSGreg Kurz { 2598b8e658bSGreg Kurz return virtio_legacy_is_little_endian(); 2608b8e658bSGreg Kurz } 2618b8e658bSGreg Kurz 2628b8e658bSGreg Kurz static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) 2638b8e658bSGreg Kurz { 2648b8e658bSGreg Kurz return -EINVAL; 2658b8e658bSGreg Kurz } 2668b8e658bSGreg Kurz 2678b8e658bSGreg Kurz static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) 2688b8e658bSGreg Kurz { 2698b8e658bSGreg Kurz return -EINVAL; 2708b8e658bSGreg Kurz } 2718b8e658bSGreg Kurz #endif /* CONFIG_TUN_VNET_CROSS_LE */ 2728b8e658bSGreg Kurz 27325bd55bbSGreg Kurz static inline bool tun_is_little_endian(struct tun_struct *tun) 27425bd55bbSGreg Kurz { 2757d824109SGreg Kurz return tun->flags & TUN_VNET_LE || 2768b8e658bSGreg Kurz tun_legacy_is_little_endian(tun); 27725bd55bbSGreg Kurz } 27825bd55bbSGreg Kurz 27956f0dcc5SMichael S. Tsirkin static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val) 28056f0dcc5SMichael S. Tsirkin { 28125bd55bbSGreg Kurz return __virtio16_to_cpu(tun_is_little_endian(tun), val); 28256f0dcc5SMichael S. Tsirkin } 28356f0dcc5SMichael S. Tsirkin 28456f0dcc5SMichael S. Tsirkin static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val) 28556f0dcc5SMichael S. Tsirkin { 28625bd55bbSGreg Kurz return __cpu_to_virtio16(tun_is_little_endian(tun), val); 28756f0dcc5SMichael S. Tsirkin } 28856f0dcc5SMichael S. Tsirkin 28996442e42SJason Wang static inline u32 tun_hashfn(u32 rxhash) 29096442e42SJason Wang { 29196442e42SJason Wang return rxhash & 0x3ff; 29296442e42SJason Wang } 29396442e42SJason Wang 29496442e42SJason Wang static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash) 29596442e42SJason Wang { 29696442e42SJason Wang struct tun_flow_entry *e; 29796442e42SJason Wang 298b67bfe0dSSasha Levin hlist_for_each_entry_rcu(e, head, hash_link) { 29996442e42SJason Wang if (e->rxhash == rxhash) 30096442e42SJason Wang return e; 30196442e42SJason Wang } 30296442e42SJason Wang return NULL; 30396442e42SJason Wang } 30496442e42SJason Wang 30596442e42SJason Wang static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun, 30696442e42SJason Wang struct hlist_head *head, 30796442e42SJason Wang u32 rxhash, u16 queue_index) 30896442e42SJason Wang { 3099fdc6befSEric Dumazet struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC); 3109fdc6befSEric Dumazet 31196442e42SJason Wang if (e) { 31296442e42SJason Wang tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n", 31396442e42SJason Wang rxhash, queue_index); 31496442e42SJason Wang e->updated = jiffies; 31596442e42SJason Wang e->rxhash = rxhash; 3169bc88939STom Herbert e->rps_rxhash = 0; 31796442e42SJason Wang e->queue_index = queue_index; 31896442e42SJason Wang e->tun = tun; 31996442e42SJason Wang hlist_add_head_rcu(&e->hash_link, head); 320b8732fb7SJason Wang ++tun->flow_count; 32196442e42SJason Wang } 32296442e42SJason Wang return e; 32396442e42SJason Wang } 32496442e42SJason Wang 32596442e42SJason Wang static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e) 32696442e42SJason Wang { 32796442e42SJason Wang tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n", 32896442e42SJason Wang e->rxhash, e->queue_index); 32996442e42SJason Wang hlist_del_rcu(&e->hash_link); 3309fdc6befSEric Dumazet kfree_rcu(e, rcu); 331b8732fb7SJason Wang --tun->flow_count; 33296442e42SJason Wang } 33396442e42SJason Wang 33496442e42SJason Wang static void tun_flow_flush(struct tun_struct *tun) 33596442e42SJason Wang { 33696442e42SJason Wang int i; 33796442e42SJason Wang 33896442e42SJason Wang spin_lock_bh(&tun->lock); 33996442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 34096442e42SJason Wang struct tun_flow_entry *e; 341b67bfe0dSSasha Levin struct hlist_node *n; 34296442e42SJason Wang 343b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) 34496442e42SJason Wang tun_flow_delete(tun, e); 34596442e42SJason Wang } 34696442e42SJason Wang spin_unlock_bh(&tun->lock); 34796442e42SJason Wang } 34896442e42SJason Wang 34996442e42SJason Wang static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index) 35096442e42SJason Wang { 35196442e42SJason Wang int i; 35296442e42SJason Wang 35396442e42SJason Wang spin_lock_bh(&tun->lock); 35496442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 35596442e42SJason Wang struct tun_flow_entry *e; 356b67bfe0dSSasha Levin struct hlist_node *n; 35796442e42SJason Wang 358b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { 35996442e42SJason Wang if (e->queue_index == queue_index) 36096442e42SJason Wang tun_flow_delete(tun, e); 36196442e42SJason Wang } 36296442e42SJason Wang } 36396442e42SJason Wang spin_unlock_bh(&tun->lock); 36496442e42SJason Wang } 36596442e42SJason Wang 36696442e42SJason Wang static void tun_flow_cleanup(unsigned long data) 36796442e42SJason Wang { 36896442e42SJason Wang struct tun_struct *tun = (struct tun_struct *)data; 36996442e42SJason Wang unsigned long delay = tun->ageing_time; 37096442e42SJason Wang unsigned long next_timer = jiffies + delay; 37196442e42SJason Wang unsigned long count = 0; 37296442e42SJason Wang int i; 37396442e42SJason Wang 37496442e42SJason Wang tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n"); 37596442e42SJason Wang 37696442e42SJason Wang spin_lock_bh(&tun->lock); 37796442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 37896442e42SJason Wang struct tun_flow_entry *e; 379b67bfe0dSSasha Levin struct hlist_node *n; 38096442e42SJason Wang 381b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { 38296442e42SJason Wang unsigned long this_timer; 38396442e42SJason Wang count++; 38496442e42SJason Wang this_timer = e->updated + delay; 38596442e42SJason Wang if (time_before_eq(this_timer, jiffies)) 38696442e42SJason Wang tun_flow_delete(tun, e); 38796442e42SJason Wang else if (time_before(this_timer, next_timer)) 38896442e42SJason Wang next_timer = this_timer; 38996442e42SJason Wang } 39096442e42SJason Wang } 39196442e42SJason Wang 39296442e42SJason Wang if (count) 39396442e42SJason Wang mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer)); 39496442e42SJason Wang spin_unlock_bh(&tun->lock); 39596442e42SJason Wang } 39696442e42SJason Wang 39749974420SEric Dumazet static void tun_flow_update(struct tun_struct *tun, u32 rxhash, 3989e85722dSJason Wang struct tun_file *tfile) 39996442e42SJason Wang { 40096442e42SJason Wang struct hlist_head *head; 40196442e42SJason Wang struct tun_flow_entry *e; 40296442e42SJason Wang unsigned long delay = tun->ageing_time; 4039e85722dSJason Wang u16 queue_index = tfile->queue_index; 40496442e42SJason Wang 40596442e42SJason Wang if (!rxhash) 40696442e42SJason Wang return; 40796442e42SJason Wang else 40896442e42SJason Wang head = &tun->flows[tun_hashfn(rxhash)]; 40996442e42SJason Wang 41096442e42SJason Wang rcu_read_lock(); 41196442e42SJason Wang 4129e85722dSJason Wang /* We may get a very small possibility of OOO during switching, not 4139e85722dSJason Wang * worth to optimize.*/ 4149e85722dSJason Wang if (tun->numqueues == 1 || tfile->detached) 41596442e42SJason Wang goto unlock; 41696442e42SJason Wang 41796442e42SJason Wang e = tun_flow_find(head, rxhash); 41896442e42SJason Wang if (likely(e)) { 41996442e42SJason Wang /* TODO: keep queueing to old queue until it's empty? */ 42096442e42SJason Wang e->queue_index = queue_index; 42196442e42SJason Wang e->updated = jiffies; 4229bc88939STom Herbert sock_rps_record_flow_hash(e->rps_rxhash); 42396442e42SJason Wang } else { 42496442e42SJason Wang spin_lock_bh(&tun->lock); 425b8732fb7SJason Wang if (!tun_flow_find(head, rxhash) && 426b8732fb7SJason Wang tun->flow_count < MAX_TAP_FLOWS) 42796442e42SJason Wang tun_flow_create(tun, head, rxhash, queue_index); 42896442e42SJason Wang 42996442e42SJason Wang if (!timer_pending(&tun->flow_gc_timer)) 43096442e42SJason Wang mod_timer(&tun->flow_gc_timer, 43196442e42SJason Wang round_jiffies_up(jiffies + delay)); 43296442e42SJason Wang spin_unlock_bh(&tun->lock); 43396442e42SJason Wang } 43496442e42SJason Wang 43596442e42SJason Wang unlock: 43696442e42SJason Wang rcu_read_unlock(); 43796442e42SJason Wang } 43896442e42SJason Wang 4399bc88939STom Herbert /** 4409bc88939STom Herbert * Save the hash received in the stack receive path and update the 4419bc88939STom Herbert * flow_hash table accordingly. 4429bc88939STom Herbert */ 4439bc88939STom Herbert static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash) 4449bc88939STom Herbert { 445567e4b79SEric Dumazet if (unlikely(e->rps_rxhash != hash)) 4469bc88939STom Herbert e->rps_rxhash = hash; 4479bc88939STom Herbert } 4489bc88939STom Herbert 449c8d68e6bSJason Wang /* We try to identify a flow through its rxhash first. The reason that 45092d4ea6eSstephen hemminger * we do not check rxq no. is because some cards(e.g 82599), chooses 451c8d68e6bSJason Wang * the rxq based on the txq where the last packet of the flow comes. As 452c8d68e6bSJason Wang * the userspace application move between processors, we may get a 453c8d68e6bSJason Wang * different rxq no. here. If we could not get rxhash, then we would 454c8d68e6bSJason Wang * hope the rxq no. may help here. 455c8d68e6bSJason Wang */ 456f663dd9aSJason Wang static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb, 45799932d4fSDaniel Borkmann void *accel_priv, select_queue_fallback_t fallback) 458c8d68e6bSJason Wang { 459c8d68e6bSJason Wang struct tun_struct *tun = netdev_priv(dev); 46096442e42SJason Wang struct tun_flow_entry *e; 461c8d68e6bSJason Wang u32 txq = 0; 462c8d68e6bSJason Wang u32 numqueues = 0; 463c8d68e6bSJason Wang 464c8d68e6bSJason Wang rcu_read_lock(); 46592bb73eaSJason Wang numqueues = ACCESS_ONCE(tun->numqueues); 466c8d68e6bSJason Wang 4673958afa1STom Herbert txq = skb_get_hash(skb); 468c8d68e6bSJason Wang if (txq) { 46996442e42SJason Wang e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq); 4709bc88939STom Herbert if (e) { 4719bc88939STom Herbert tun_flow_save_rps_rxhash(e, txq); 472fbe4d456SZhi Yong Wu txq = e->queue_index; 4739bc88939STom Herbert } else 474c8d68e6bSJason Wang /* use multiply and shift instead of expensive divide */ 475c8d68e6bSJason Wang txq = ((u64)txq * numqueues) >> 32; 476c8d68e6bSJason Wang } else if (likely(skb_rx_queue_recorded(skb))) { 477c8d68e6bSJason Wang txq = skb_get_rx_queue(skb); 478c8d68e6bSJason Wang while (unlikely(txq >= numqueues)) 479c8d68e6bSJason Wang txq -= numqueues; 480c8d68e6bSJason Wang } 481c8d68e6bSJason Wang 482c8d68e6bSJason Wang rcu_read_unlock(); 483c8d68e6bSJason Wang return txq; 484c8d68e6bSJason Wang } 485c8d68e6bSJason Wang 486cde8b15fSJason Wang static inline bool tun_not_capable(struct tun_struct *tun) 487cde8b15fSJason Wang { 488cde8b15fSJason Wang const struct cred *cred = current_cred(); 489c260b772SEric W. Biederman struct net *net = dev_net(tun->dev); 490cde8b15fSJason Wang 491cde8b15fSJason Wang return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) || 492cde8b15fSJason Wang (gid_valid(tun->group) && !in_egroup_p(tun->group))) && 493c260b772SEric W. Biederman !ns_capable(net->user_ns, CAP_NET_ADMIN); 494cde8b15fSJason Wang } 495cde8b15fSJason Wang 496c8d68e6bSJason Wang static void tun_set_real_num_queues(struct tun_struct *tun) 497c8d68e6bSJason Wang { 498c8d68e6bSJason Wang netif_set_real_num_tx_queues(tun->dev, tun->numqueues); 499c8d68e6bSJason Wang netif_set_real_num_rx_queues(tun->dev, tun->numqueues); 500c8d68e6bSJason Wang } 501c8d68e6bSJason Wang 5024008e97fSJason Wang static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) 5034008e97fSJason Wang { 5044008e97fSJason Wang tfile->detached = tun; 5054008e97fSJason Wang list_add_tail(&tfile->next, &tun->disabled); 5064008e97fSJason Wang ++tun->numdisabled; 5074008e97fSJason Wang } 5084008e97fSJason Wang 509d32649d1SJason Wang static struct tun_struct *tun_enable_queue(struct tun_file *tfile) 5104008e97fSJason Wang { 5114008e97fSJason Wang struct tun_struct *tun = tfile->detached; 5124008e97fSJason Wang 5134008e97fSJason Wang tfile->detached = NULL; 5144008e97fSJason Wang list_del_init(&tfile->next); 5154008e97fSJason Wang --tun->numdisabled; 5164008e97fSJason Wang return tun; 5174008e97fSJason Wang } 5184008e97fSJason Wang 5194bfb0513SJason Wang static void tun_queue_purge(struct tun_file *tfile) 5204bfb0513SJason Wang { 5211576d986SJason Wang struct sk_buff *skb; 5221576d986SJason Wang 5231576d986SJason Wang while ((skb = skb_array_consume(&tfile->tx_array)) != NULL) 5241576d986SJason Wang kfree_skb(skb); 5251576d986SJason Wang 526*5503fcecSJason Wang skb_queue_purge(&tfile->sk.sk_write_queue); 5274bfb0513SJason Wang skb_queue_purge(&tfile->sk.sk_error_queue); 5284bfb0513SJason Wang } 5294bfb0513SJason Wang 530c8d68e6bSJason Wang static void __tun_detach(struct tun_file *tfile, bool clean) 531c8d68e6bSJason Wang { 532c8d68e6bSJason Wang struct tun_file *ntfile; 533c8d68e6bSJason Wang struct tun_struct *tun; 534c8d68e6bSJason Wang 535b8deabd3SJason Wang tun = rtnl_dereference(tfile->tun); 536b8deabd3SJason Wang 5379e85722dSJason Wang if (tun && !tfile->detached) { 538c8d68e6bSJason Wang u16 index = tfile->queue_index; 539c8d68e6bSJason Wang BUG_ON(index >= tun->numqueues); 540c8d68e6bSJason Wang 541c8d68e6bSJason Wang rcu_assign_pointer(tun->tfiles[index], 542c8d68e6bSJason Wang tun->tfiles[tun->numqueues - 1]); 543b8deabd3SJason Wang ntfile = rtnl_dereference(tun->tfiles[index]); 544c8d68e6bSJason Wang ntfile->queue_index = index; 545c8d68e6bSJason Wang 546c8d68e6bSJason Wang --tun->numqueues; 5479e85722dSJason Wang if (clean) { 548c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 549c8d68e6bSJason Wang sock_put(&tfile->sk); 5509e85722dSJason Wang } else 5514008e97fSJason Wang tun_disable_queue(tun, tfile); 552c8d68e6bSJason Wang 553c8d68e6bSJason Wang synchronize_net(); 55496442e42SJason Wang tun_flow_delete_by_queue(tun, tun->numqueues + 1); 555c8d68e6bSJason Wang /* Drop read queue */ 5564bfb0513SJason Wang tun_queue_purge(tfile); 557c8d68e6bSJason Wang tun_set_real_num_queues(tun); 558dd38bd85SJason Wang } else if (tfile->detached && clean) { 5594008e97fSJason Wang tun = tun_enable_queue(tfile); 560dd38bd85SJason Wang sock_put(&tfile->sk); 561dd38bd85SJason Wang } 562c8d68e6bSJason Wang 563c8d68e6bSJason Wang if (clean) { 564af668b3cSMichael S. Tsirkin if (tun && tun->numqueues == 0 && tun->numdisabled == 0) { 565af668b3cSMichael S. Tsirkin netif_carrier_off(tun->dev); 566af668b3cSMichael S. Tsirkin 56740630b82SMichael S. Tsirkin if (!(tun->flags & IFF_PERSIST) && 568af668b3cSMichael S. Tsirkin tun->dev->reg_state == NETREG_REGISTERED) 5694008e97fSJason Wang unregister_netdevice(tun->dev); 570af668b3cSMichael S. Tsirkin } 5711576d986SJason Wang if (tun) 5721576d986SJason Wang skb_array_cleanup(&tfile->tx_array); 573140e807dSEric W. Biederman sock_put(&tfile->sk); 574c8d68e6bSJason Wang } 575c8d68e6bSJason Wang } 576c8d68e6bSJason Wang 577c8d68e6bSJason Wang static void tun_detach(struct tun_file *tfile, bool clean) 578c8d68e6bSJason Wang { 579c8d68e6bSJason Wang rtnl_lock(); 580c8d68e6bSJason Wang __tun_detach(tfile, clean); 581c8d68e6bSJason Wang rtnl_unlock(); 582c8d68e6bSJason Wang } 583c8d68e6bSJason Wang 584c8d68e6bSJason Wang static void tun_detach_all(struct net_device *dev) 585c8d68e6bSJason Wang { 586c8d68e6bSJason Wang struct tun_struct *tun = netdev_priv(dev); 5874008e97fSJason Wang struct tun_file *tfile, *tmp; 588c8d68e6bSJason Wang int i, n = tun->numqueues; 589c8d68e6bSJason Wang 590c8d68e6bSJason Wang for (i = 0; i < n; i++) { 591b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 592c8d68e6bSJason Wang BUG_ON(!tfile); 593addf8fc4SJason Wang tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; 5949e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 595c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 596c8d68e6bSJason Wang --tun->numqueues; 597c8d68e6bSJason Wang } 5989e85722dSJason Wang list_for_each_entry(tfile, &tun->disabled, next) { 599addf8fc4SJason Wang tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; 6009e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 601c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 6029e85722dSJason Wang } 603c8d68e6bSJason Wang BUG_ON(tun->numqueues != 0); 604c8d68e6bSJason Wang 605c8d68e6bSJason Wang synchronize_net(); 606c8d68e6bSJason Wang for (i = 0; i < n; i++) { 607b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 608c8d68e6bSJason Wang /* Drop read queue */ 6094bfb0513SJason Wang tun_queue_purge(tfile); 610c8d68e6bSJason Wang sock_put(&tfile->sk); 611c8d68e6bSJason Wang } 6124008e97fSJason Wang list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { 6134008e97fSJason Wang tun_enable_queue(tfile); 6144bfb0513SJason Wang tun_queue_purge(tfile); 6154008e97fSJason Wang sock_put(&tfile->sk); 6164008e97fSJason Wang } 6174008e97fSJason Wang BUG_ON(tun->numdisabled != 0); 618dd38bd85SJason Wang 61940630b82SMichael S. Tsirkin if (tun->flags & IFF_PERSIST) 620dd38bd85SJason Wang module_put(THIS_MODULE); 621c8d68e6bSJason Wang } 622c8d68e6bSJason Wang 623849c9b6fSPavel Emelyanov static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter) 624a7385ba2SEric W. Biederman { 625631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 6261576d986SJason Wang struct net_device *dev = tun->dev; 62738231b7aSEric W. Biederman int err; 628a7385ba2SEric W. Biederman 6295dbbaf2dSPaul Moore err = security_tun_dev_attach(tfile->socket.sk, tun->security); 6305dbbaf2dSPaul Moore if (err < 0) 6315dbbaf2dSPaul Moore goto out; 6325dbbaf2dSPaul Moore 63338231b7aSEric W. Biederman err = -EINVAL; 6349e85722dSJason Wang if (rtnl_dereference(tfile->tun) && !tfile->detached) 63538231b7aSEric W. Biederman goto out; 63638231b7aSEric W. Biederman 63738231b7aSEric W. Biederman err = -EBUSY; 63840630b82SMichael S. Tsirkin if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1) 639c8d68e6bSJason Wang goto out; 640c8d68e6bSJason Wang 641c8d68e6bSJason Wang err = -E2BIG; 6424008e97fSJason Wang if (!tfile->detached && 6434008e97fSJason Wang tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES) 64438231b7aSEric W. Biederman goto out; 64538231b7aSEric W. Biederman 64638231b7aSEric W. Biederman err = 0; 64754f968d6SJason Wang 64892d4ea6eSstephen hemminger /* Re-attach the filter to persist device */ 649849c9b6fSPavel Emelyanov if (!skip_filter && (tun->filter_attached == true)) { 6508ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 6518ced425eSHannes Frederic Sowa err = sk_attach_filter(&tun->fprog, tfile->socket.sk); 6528ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 65354f968d6SJason Wang if (!err) 65454f968d6SJason Wang goto out; 65554f968d6SJason Wang } 6561576d986SJason Wang 6571576d986SJason Wang if (!tfile->detached && 6581576d986SJason Wang skb_array_init(&tfile->tx_array, dev->tx_queue_len, GFP_KERNEL)) { 6591576d986SJason Wang err = -ENOMEM; 6601576d986SJason Wang goto out; 6611576d986SJason Wang } 6621576d986SJason Wang 663c8d68e6bSJason Wang tfile->queue_index = tun->numqueues; 664addf8fc4SJason Wang tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN; 6656e914fc7SJason Wang rcu_assign_pointer(tfile->tun, tun); 666c8d68e6bSJason Wang rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); 667c8d68e6bSJason Wang tun->numqueues++; 668c8d68e6bSJason Wang 6694008e97fSJason Wang if (tfile->detached) 6704008e97fSJason Wang tun_enable_queue(tfile); 6714008e97fSJason Wang else 6724008e97fSJason Wang sock_hold(&tfile->sk); 6734008e97fSJason Wang 674c8d68e6bSJason Wang tun_set_real_num_queues(tun); 675c8d68e6bSJason Wang 676c8d68e6bSJason Wang /* device is allowed to go away first, so no need to hold extra 677c8d68e6bSJason Wang * refcnt. 678c8d68e6bSJason Wang */ 679a7385ba2SEric W. Biederman 68038231b7aSEric W. Biederman out: 68138231b7aSEric W. Biederman return err; 682a7385ba2SEric W. Biederman } 683a7385ba2SEric W. Biederman 684631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile) 685631ab46bSEric W. Biederman { 6866e914fc7SJason Wang struct tun_struct *tun; 687c70f1829SEric W. Biederman 6886e914fc7SJason Wang rcu_read_lock(); 6896e914fc7SJason Wang tun = rcu_dereference(tfile->tun); 6906e914fc7SJason Wang if (tun) 6916e914fc7SJason Wang dev_hold(tun->dev); 6926e914fc7SJason Wang rcu_read_unlock(); 693c70f1829SEric W. Biederman 694c70f1829SEric W. Biederman return tun; 695631ab46bSEric W. Biederman } 696631ab46bSEric W. Biederman 697631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file) 698631ab46bSEric W. Biederman { 699631ab46bSEric W. Biederman return __tun_get(file->private_data); 700631ab46bSEric W. Biederman } 701631ab46bSEric W. Biederman 702631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun) 703631ab46bSEric W. Biederman { 7046e914fc7SJason Wang dev_put(tun->dev); 705631ab46bSEric W. Biederman } 706631ab46bSEric W. Biederman 7076b8a66eeSJoe Perches /* TAP filtering */ 708f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr) 709f271b2ccSMax Krasnyansky { 710f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 711f271b2ccSMax Krasnyansky mask[n >> 5] |= (1 << (n & 31)); 712f271b2ccSMax Krasnyansky } 713f271b2ccSMax Krasnyansky 714f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) 715f271b2ccSMax Krasnyansky { 716f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 717f271b2ccSMax Krasnyansky return mask[n >> 5] & (1 << (n & 31)); 718f271b2ccSMax Krasnyansky } 719f271b2ccSMax Krasnyansky 720f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg) 721f271b2ccSMax Krasnyansky { 722f271b2ccSMax Krasnyansky struct { u8 u[ETH_ALEN]; } *addr; 723f271b2ccSMax Krasnyansky struct tun_filter uf; 724f271b2ccSMax Krasnyansky int err, alen, n, nexact; 725f271b2ccSMax Krasnyansky 726f271b2ccSMax Krasnyansky if (copy_from_user(&uf, arg, sizeof(uf))) 727f271b2ccSMax Krasnyansky return -EFAULT; 728f271b2ccSMax Krasnyansky 729f271b2ccSMax Krasnyansky if (!uf.count) { 730f271b2ccSMax Krasnyansky /* Disabled */ 731f271b2ccSMax Krasnyansky filter->count = 0; 732f271b2ccSMax Krasnyansky return 0; 733f271b2ccSMax Krasnyansky } 734f271b2ccSMax Krasnyansky 735f271b2ccSMax Krasnyansky alen = ETH_ALEN * uf.count; 73628e8190dSMarkus Elfring addr = memdup_user(arg + sizeof(uf), alen); 73728e8190dSMarkus Elfring if (IS_ERR(addr)) 73828e8190dSMarkus Elfring return PTR_ERR(addr); 739f271b2ccSMax Krasnyansky 740f271b2ccSMax Krasnyansky /* The filter is updated without holding any locks. Which is 741f271b2ccSMax Krasnyansky * perfectly safe. We disable it first and in the worst 742f271b2ccSMax Krasnyansky * case we'll accept a few undesired packets. */ 743f271b2ccSMax Krasnyansky filter->count = 0; 744f271b2ccSMax Krasnyansky wmb(); 745f271b2ccSMax Krasnyansky 746f271b2ccSMax Krasnyansky /* Use first set of addresses as an exact filter */ 747f271b2ccSMax Krasnyansky for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++) 748f271b2ccSMax Krasnyansky memcpy(filter->addr[n], addr[n].u, ETH_ALEN); 749f271b2ccSMax Krasnyansky 750f271b2ccSMax Krasnyansky nexact = n; 751f271b2ccSMax Krasnyansky 752cfbf84fcSAlex Williamson /* Remaining multicast addresses are hashed, 753cfbf84fcSAlex Williamson * unicast will leave the filter disabled. */ 754f271b2ccSMax Krasnyansky memset(filter->mask, 0, sizeof(filter->mask)); 755cfbf84fcSAlex Williamson for (; n < uf.count; n++) { 756cfbf84fcSAlex Williamson if (!is_multicast_ether_addr(addr[n].u)) { 757cfbf84fcSAlex Williamson err = 0; /* no filter */ 7583b8d2a69SMarkus Elfring goto free_addr; 759cfbf84fcSAlex Williamson } 760f271b2ccSMax Krasnyansky addr_hash_set(filter->mask, addr[n].u); 761cfbf84fcSAlex Williamson } 762f271b2ccSMax Krasnyansky 763f271b2ccSMax Krasnyansky /* For ALLMULTI just set the mask to all ones. 764f271b2ccSMax Krasnyansky * This overrides the mask populated above. */ 765f271b2ccSMax Krasnyansky if ((uf.flags & TUN_FLT_ALLMULTI)) 766f271b2ccSMax Krasnyansky memset(filter->mask, ~0, sizeof(filter->mask)); 767f271b2ccSMax Krasnyansky 768f271b2ccSMax Krasnyansky /* Now enable the filter */ 769f271b2ccSMax Krasnyansky wmb(); 770f271b2ccSMax Krasnyansky filter->count = nexact; 771f271b2ccSMax Krasnyansky 772f271b2ccSMax Krasnyansky /* Return the number of exact filters */ 773f271b2ccSMax Krasnyansky err = nexact; 7743b8d2a69SMarkus Elfring free_addr: 775f271b2ccSMax Krasnyansky kfree(addr); 776f271b2ccSMax Krasnyansky return err; 777f271b2ccSMax Krasnyansky } 778f271b2ccSMax Krasnyansky 779f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */ 780f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb) 781f271b2ccSMax Krasnyansky { 782f271b2ccSMax Krasnyansky /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect 783f271b2ccSMax Krasnyansky * at this point. */ 784f271b2ccSMax Krasnyansky struct ethhdr *eh = (struct ethhdr *) skb->data; 785f271b2ccSMax Krasnyansky int i; 786f271b2ccSMax Krasnyansky 787f271b2ccSMax Krasnyansky /* Exact match */ 788f271b2ccSMax Krasnyansky for (i = 0; i < filter->count; i++) 7892e42e474SJoe Perches if (ether_addr_equal(eh->h_dest, filter->addr[i])) 790f271b2ccSMax Krasnyansky return 1; 791f271b2ccSMax Krasnyansky 792f271b2ccSMax Krasnyansky /* Inexact match (multicast only) */ 793f271b2ccSMax Krasnyansky if (is_multicast_ether_addr(eh->h_dest)) 794f271b2ccSMax Krasnyansky return addr_hash_test(filter->mask, eh->h_dest); 795f271b2ccSMax Krasnyansky 796f271b2ccSMax Krasnyansky return 0; 797f271b2ccSMax Krasnyansky } 798f271b2ccSMax Krasnyansky 799f271b2ccSMax Krasnyansky /* 800f271b2ccSMax Krasnyansky * Checks whether the packet is accepted or not. 801f271b2ccSMax Krasnyansky * Returns: 0 - drop, !=0 - accept 802f271b2ccSMax Krasnyansky */ 803f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb) 804f271b2ccSMax Krasnyansky { 805f271b2ccSMax Krasnyansky if (!filter->count) 806f271b2ccSMax Krasnyansky return 1; 807f271b2ccSMax Krasnyansky 808f271b2ccSMax Krasnyansky return run_filter(filter, skb); 809f271b2ccSMax Krasnyansky } 810f271b2ccSMax Krasnyansky 8111da177e4SLinus Torvalds /* Network device part of the driver */ 8121da177e4SLinus Torvalds 8137282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops; 8141da177e4SLinus Torvalds 815c70f1829SEric W. Biederman /* Net device detach from fd. */ 816c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev) 817c70f1829SEric W. Biederman { 818c8d68e6bSJason Wang tun_detach_all(dev); 819c70f1829SEric W. Biederman } 820c70f1829SEric W. Biederman 8211da177e4SLinus Torvalds /* Net device open. */ 8221da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev) 8231da177e4SLinus Torvalds { 824c8d68e6bSJason Wang netif_tx_start_all_queues(dev); 8251da177e4SLinus Torvalds return 0; 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds /* Net device close. */ 8291da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev) 8301da177e4SLinus Torvalds { 831c8d68e6bSJason Wang netif_tx_stop_all_queues(dev); 8321da177e4SLinus Torvalds return 0; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds /* Net device start xmit */ 836424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) 8371da177e4SLinus Torvalds { 8381da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 839c8d68e6bSJason Wang int txq = skb->queue_mapping; 8406e914fc7SJason Wang struct tun_file *tfile; 841fa35864eSDominic Curran u32 numqueues = 0; 8421da177e4SLinus Torvalds 8436e914fc7SJason Wang rcu_read_lock(); 844c8d68e6bSJason Wang tfile = rcu_dereference(tun->tfiles[txq]); 845fa35864eSDominic Curran numqueues = ACCESS_ONCE(tun->numqueues); 846c8d68e6bSJason Wang 8471da177e4SLinus Torvalds /* Drop packet if interface is not attached */ 848fa35864eSDominic Curran if (txq >= numqueues) 8491da177e4SLinus Torvalds goto drop; 8501da177e4SLinus Torvalds 8513df97ba8SJason Wang #ifdef CONFIG_RPS 8523df97ba8SJason Wang if (numqueues == 1 && static_key_false(&rps_needed)) { 8539bc88939STom Herbert /* Select queue was not called for the skbuff, so we extract the 8549bc88939STom Herbert * RPS hash and save it into the flow_table here. 8559bc88939STom Herbert */ 8569bc88939STom Herbert __u32 rxhash; 8579bc88939STom Herbert 8589bc88939STom Herbert rxhash = skb_get_hash(skb); 8599bc88939STom Herbert if (rxhash) { 8609bc88939STom Herbert struct tun_flow_entry *e; 8619bc88939STom Herbert e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)], 8629bc88939STom Herbert rxhash); 8639bc88939STom Herbert if (e) 8649bc88939STom Herbert tun_flow_save_rps_rxhash(e, rxhash); 8659bc88939STom Herbert } 8669bc88939STom Herbert } 8673df97ba8SJason Wang #endif 8689bc88939STom Herbert 8696e914fc7SJason Wang tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len); 8706e914fc7SJason Wang 871c8d68e6bSJason Wang BUG_ON(!tfile); 872c8d68e6bSJason Wang 873f271b2ccSMax Krasnyansky /* Drop if the filter does not like it. 874f271b2ccSMax Krasnyansky * This is a noop if the filter is disabled. 875f271b2ccSMax Krasnyansky * Filter can be enabled only for the TAP devices. */ 876f271b2ccSMax Krasnyansky if (!check_filter(&tun->txflt, skb)) 877f271b2ccSMax Krasnyansky goto drop; 878f271b2ccSMax Krasnyansky 87954f968d6SJason Wang if (tfile->socket.sk->sk_filter && 88054f968d6SJason Wang sk_filter(tfile->socket.sk, skb)) 88199405162SMichael S. Tsirkin goto drop; 88299405162SMichael S. Tsirkin 8837bf66305SJason Wang if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC))) 8847bf66305SJason Wang goto drop; 8857bf66305SJason Wang 8867b996243SSoheil Hassas Yeganeh skb_tx_timestamp(skb); 887eda29772SRichard Cochran 8880110d6f2SMichael S. Tsirkin /* Orphan the skb - required as we might hang on to it 8897bf66305SJason Wang * for indefinite time. 8907bf66305SJason Wang */ 8910110d6f2SMichael S. Tsirkin skb_orphan(skb); 8920110d6f2SMichael S. Tsirkin 893f8af75f3SEric Dumazet nf_reset(skb); 894f8af75f3SEric Dumazet 8951576d986SJason Wang if (skb_array_produce(&tfile->tx_array, skb)) 8961576d986SJason Wang goto drop; 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds /* Notify and wake up reader process */ 89954f968d6SJason Wang if (tfile->flags & TUN_FASYNC) 90054f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_IN); 9019e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 9026e914fc7SJason Wang 9036e914fc7SJason Wang rcu_read_unlock(); 9046ed10654SPatrick McHardy return NETDEV_TX_OK; 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds drop: 907608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->tx_dropped); 908149d36f7SMichael S. Tsirkin skb_tx_error(skb); 9091da177e4SLinus Torvalds kfree_skb(skb); 9106e914fc7SJason Wang rcu_read_unlock(); 911baeababbSJason Wang return NET_XMIT_DROP; 9121da177e4SLinus Torvalds } 9131da177e4SLinus Torvalds 914f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev) 9151da177e4SLinus Torvalds { 916f271b2ccSMax Krasnyansky /* 917f271b2ccSMax Krasnyansky * This callback is supposed to deal with mc filter in 918f271b2ccSMax Krasnyansky * _rx_ path and has nothing to do with the _tx_ path. 919f271b2ccSMax Krasnyansky * In rx path we always accept everything userspace gives us. 920f271b2ccSMax Krasnyansky */ 9211da177e4SLinus Torvalds } 9221da177e4SLinus Torvalds 923c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev, 924c8f44affSMichał Mirosław netdev_features_t features) 92588255375SMichał Mirosław { 92688255375SMichał Mirosław struct tun_struct *tun = netdev_priv(dev); 92788255375SMichał Mirosław 92888255375SMichał Mirosław return (features & tun->set_features) | (features & ~TUN_USER_FEATURES); 92988255375SMichał Mirosław } 930bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 931bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev) 932bebd097aSNeil Horman { 933bebd097aSNeil Horman /* 934bebd097aSNeil Horman * Tun only receives frames when: 935bebd097aSNeil Horman * 1) the char device endpoint gets data from user space 936bebd097aSNeil Horman * 2) the tun socket gets a sendmsg call from user space 93792d4ea6eSstephen hemminger * Since both of those are synchronous operations, we are guaranteed 938bebd097aSNeil Horman * never to have pending data when we poll for it 93992d4ea6eSstephen hemminger * so there is nothing to do here but return. 940bebd097aSNeil Horman * We need this though so netpoll recognizes us as an interface that 941bebd097aSNeil Horman * supports polling, which enables bridge devices in virt setups to 942bebd097aSNeil Horman * still use netconsole 943bebd097aSNeil Horman */ 944bebd097aSNeil Horman return; 945bebd097aSNeil Horman } 946bebd097aSNeil Horman #endif 947eaea34b2SPaolo Abeni 948eaea34b2SPaolo Abeni static void tun_set_headroom(struct net_device *dev, int new_hr) 949eaea34b2SPaolo Abeni { 950eaea34b2SPaolo Abeni struct tun_struct *tun = netdev_priv(dev); 951eaea34b2SPaolo Abeni 952eaea34b2SPaolo Abeni if (new_hr < NET_SKB_PAD) 953eaea34b2SPaolo Abeni new_hr = NET_SKB_PAD; 954eaea34b2SPaolo Abeni 955eaea34b2SPaolo Abeni tun->align = new_hr; 956eaea34b2SPaolo Abeni } 957eaea34b2SPaolo Abeni 958bc1f4470Sstephen hemminger static void 959608b9977SPaolo Abeni tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 960608b9977SPaolo Abeni { 961608b9977SPaolo Abeni u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0; 962608b9977SPaolo Abeni struct tun_struct *tun = netdev_priv(dev); 963608b9977SPaolo Abeni struct tun_pcpu_stats *p; 964608b9977SPaolo Abeni int i; 965608b9977SPaolo Abeni 966608b9977SPaolo Abeni for_each_possible_cpu(i) { 967608b9977SPaolo Abeni u64 rxpackets, rxbytes, txpackets, txbytes; 968608b9977SPaolo Abeni unsigned int start; 969608b9977SPaolo Abeni 970608b9977SPaolo Abeni p = per_cpu_ptr(tun->pcpu_stats, i); 971608b9977SPaolo Abeni do { 972608b9977SPaolo Abeni start = u64_stats_fetch_begin(&p->syncp); 973608b9977SPaolo Abeni rxpackets = p->rx_packets; 974608b9977SPaolo Abeni rxbytes = p->rx_bytes; 975608b9977SPaolo Abeni txpackets = p->tx_packets; 976608b9977SPaolo Abeni txbytes = p->tx_bytes; 977608b9977SPaolo Abeni } while (u64_stats_fetch_retry(&p->syncp, start)); 978608b9977SPaolo Abeni 979608b9977SPaolo Abeni stats->rx_packets += rxpackets; 980608b9977SPaolo Abeni stats->rx_bytes += rxbytes; 981608b9977SPaolo Abeni stats->tx_packets += txpackets; 982608b9977SPaolo Abeni stats->tx_bytes += txbytes; 983608b9977SPaolo Abeni 984608b9977SPaolo Abeni /* u32 counters */ 985608b9977SPaolo Abeni rx_dropped += p->rx_dropped; 986608b9977SPaolo Abeni rx_frame_errors += p->rx_frame_errors; 987608b9977SPaolo Abeni tx_dropped += p->tx_dropped; 988608b9977SPaolo Abeni } 989608b9977SPaolo Abeni stats->rx_dropped = rx_dropped; 990608b9977SPaolo Abeni stats->rx_frame_errors = rx_frame_errors; 991608b9977SPaolo Abeni stats->tx_dropped = tx_dropped; 992608b9977SPaolo Abeni } 993608b9977SPaolo Abeni 994758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = { 995c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 996758e43b7SStephen Hemminger .ndo_open = tun_net_open, 997758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 99800829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 99988255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 1000c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 1001bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 1002bebd097aSNeil Horman .ndo_poll_controller = tun_poll_controller, 1003bebd097aSNeil Horman #endif 1004eaea34b2SPaolo Abeni .ndo_set_rx_headroom = tun_set_headroom, 1005608b9977SPaolo Abeni .ndo_get_stats64 = tun_net_get_stats64, 1006758e43b7SStephen Hemminger }; 1007758e43b7SStephen Hemminger 1008758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = { 1009c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 1010758e43b7SStephen Hemminger .ndo_open = tun_net_open, 1011758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 101200829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 101388255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 1014afc4b13dSJiri Pirko .ndo_set_rx_mode = tun_net_mclist, 1015758e43b7SStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 1016758e43b7SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 1017c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 1018bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 1019bebd097aSNeil Horman .ndo_poll_controller = tun_poll_controller, 1020bebd097aSNeil Horman #endif 10215e52796aSToshiaki Makita .ndo_features_check = passthru_features_check, 1022eaea34b2SPaolo Abeni .ndo_set_rx_headroom = tun_set_headroom, 1023608b9977SPaolo Abeni .ndo_get_stats64 = tun_net_get_stats64, 1024758e43b7SStephen Hemminger }; 1025758e43b7SStephen Hemminger 1026944a1376SPavel Emelyanov static void tun_flow_init(struct tun_struct *tun) 102796442e42SJason Wang { 102896442e42SJason Wang int i; 102996442e42SJason Wang 103096442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) 103196442e42SJason Wang INIT_HLIST_HEAD(&tun->flows[i]); 103296442e42SJason Wang 103396442e42SJason Wang tun->ageing_time = TUN_FLOW_EXPIRE; 103496442e42SJason Wang setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun); 103596442e42SJason Wang mod_timer(&tun->flow_gc_timer, 103696442e42SJason Wang round_jiffies_up(jiffies + tun->ageing_time)); 103796442e42SJason Wang } 103896442e42SJason Wang 103996442e42SJason Wang static void tun_flow_uninit(struct tun_struct *tun) 104096442e42SJason Wang { 104196442e42SJason Wang del_timer_sync(&tun->flow_gc_timer); 104296442e42SJason Wang tun_flow_flush(tun); 104396442e42SJason Wang } 104496442e42SJason Wang 104591572088SJarod Wilson #define MIN_MTU 68 104691572088SJarod Wilson #define MAX_MTU 65535 104791572088SJarod Wilson 10481da177e4SLinus Torvalds /* Initialize net device. */ 10491da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev) 10501da177e4SLinus Torvalds { 10511da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 105440630b82SMichael S. Tsirkin case IFF_TUN: 1055758e43b7SStephen Hemminger dev->netdev_ops = &tun_netdev_ops; 1056758e43b7SStephen Hemminger 10571da177e4SLinus Torvalds /* Point-to-Point TUN Device */ 10581da177e4SLinus Torvalds dev->hard_header_len = 0; 10591da177e4SLinus Torvalds dev->addr_len = 0; 10601da177e4SLinus Torvalds dev->mtu = 1500; 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds /* Zero header length */ 10631da177e4SLinus Torvalds dev->type = ARPHRD_NONE; 10641da177e4SLinus Torvalds dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 10651da177e4SLinus Torvalds break; 10661da177e4SLinus Torvalds 106740630b82SMichael S. Tsirkin case IFF_TAP: 10687a0a9608SKusanagi Kouichi dev->netdev_ops = &tap_netdev_ops; 10691da177e4SLinus Torvalds /* Ethernet TAP Device */ 10701da177e4SLinus Torvalds ether_setup(dev); 1071550fd08cSNeil Horman dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1072a676847bSstephen hemminger dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 107336226a8dSBrian Braunstein 1074f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 107536226a8dSBrian Braunstein 10761da177e4SLinus Torvalds break; 10771da177e4SLinus Torvalds } 107891572088SJarod Wilson 107991572088SJarod Wilson dev->min_mtu = MIN_MTU; 108091572088SJarod Wilson dev->max_mtu = MAX_MTU - dev->hard_header_len; 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds /* Character device part */ 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds /* Poll */ 10861da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table *wait) 10871da177e4SLinus Torvalds { 1088b2430de3SEric W. Biederman struct tun_file *tfile = file->private_data; 1089b2430de3SEric W. Biederman struct tun_struct *tun = __tun_get(tfile); 10903c8a9c63SMariusz Kozlowski struct sock *sk; 109133dccbb0SHerbert Xu unsigned int mask = 0; 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds if (!tun) 1094eac9e902SEric W. Biederman return POLLERR; 10951da177e4SLinus Torvalds 109654f968d6SJason Wang sk = tfile->socket.sk; 10973c8a9c63SMariusz Kozlowski 10986b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_chr_poll\n"); 10991da177e4SLinus Torvalds 11009e641bdcSXi Wang poll_wait(file, sk_sleep(sk), wait); 11011da177e4SLinus Torvalds 11021576d986SJason Wang if (!skb_array_empty(&tfile->tx_array)) 11031da177e4SLinus Torvalds mask |= POLLIN | POLLRDNORM; 11041da177e4SLinus Torvalds 110533dccbb0SHerbert Xu if (sock_writeable(sk) || 11069cd3e072SEric Dumazet (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) && 110733dccbb0SHerbert Xu sock_writeable(sk))) 110833dccbb0SHerbert Xu mask |= POLLOUT | POLLWRNORM; 110933dccbb0SHerbert Xu 1110c70f1829SEric W. Biederman if (tun->dev->reg_state != NETREG_REGISTERED) 1111c70f1829SEric W. Biederman mask = POLLERR; 1112c70f1829SEric W. Biederman 1113631ab46bSEric W. Biederman tun_put(tun); 11141da177e4SLinus Torvalds return mask; 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds 1117f42157cbSRusty Russell /* prepad is the amount to reserve at front. len is length after that. 1118f42157cbSRusty Russell * linear is a hint as to how much to copy (usually headers). */ 111954f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, 112033dccbb0SHerbert Xu size_t prepad, size_t len, 112133dccbb0SHerbert Xu size_t linear, int noblock) 1122f42157cbSRusty Russell { 112354f968d6SJason Wang struct sock *sk = tfile->socket.sk; 1124f42157cbSRusty Russell struct sk_buff *skb; 112533dccbb0SHerbert Xu int err; 1126f42157cbSRusty Russell 1127f42157cbSRusty Russell /* Under a page? Don't bother with paged skb. */ 11280eca93bcSHerbert Xu if (prepad + len < PAGE_SIZE || !linear) 112933dccbb0SHerbert Xu linear = len; 1130f42157cbSRusty Russell 113133dccbb0SHerbert Xu skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, 113228d64271SEric Dumazet &err, 0); 1133f42157cbSRusty Russell if (!skb) 113433dccbb0SHerbert Xu return ERR_PTR(err); 1135f42157cbSRusty Russell 1136f42157cbSRusty Russell skb_reserve(skb, prepad); 1137f42157cbSRusty Russell skb_put(skb, linear); 113833dccbb0SHerbert Xu skb->data_len = len - linear; 113933dccbb0SHerbert Xu skb->len += len - linear; 1140f42157cbSRusty Russell 1141f42157cbSRusty Russell return skb; 1142f42157cbSRusty Russell } 1143f42157cbSRusty Russell 1144*5503fcecSJason Wang static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile, 1145*5503fcecSJason Wang struct sk_buff *skb, int more) 1146*5503fcecSJason Wang { 1147*5503fcecSJason Wang struct sk_buff_head *queue = &tfile->sk.sk_write_queue; 1148*5503fcecSJason Wang struct sk_buff_head process_queue; 1149*5503fcecSJason Wang u32 rx_batched = tun->rx_batched; 1150*5503fcecSJason Wang bool rcv = false; 1151*5503fcecSJason Wang 1152*5503fcecSJason Wang if (!rx_batched || (!more && skb_queue_empty(queue))) { 1153*5503fcecSJason Wang local_bh_disable(); 1154*5503fcecSJason Wang netif_receive_skb(skb); 1155*5503fcecSJason Wang local_bh_enable(); 1156*5503fcecSJason Wang return; 1157*5503fcecSJason Wang } 1158*5503fcecSJason Wang 1159*5503fcecSJason Wang spin_lock(&queue->lock); 1160*5503fcecSJason Wang if (!more || skb_queue_len(queue) == rx_batched) { 1161*5503fcecSJason Wang __skb_queue_head_init(&process_queue); 1162*5503fcecSJason Wang skb_queue_splice_tail_init(queue, &process_queue); 1163*5503fcecSJason Wang rcv = true; 1164*5503fcecSJason Wang } else { 1165*5503fcecSJason Wang __skb_queue_tail(queue, skb); 1166*5503fcecSJason Wang } 1167*5503fcecSJason Wang spin_unlock(&queue->lock); 1168*5503fcecSJason Wang 1169*5503fcecSJason Wang if (rcv) { 1170*5503fcecSJason Wang struct sk_buff *nskb; 1171*5503fcecSJason Wang 1172*5503fcecSJason Wang local_bh_disable(); 1173*5503fcecSJason Wang while ((nskb = __skb_dequeue(&process_queue))) 1174*5503fcecSJason Wang netif_receive_skb(nskb); 1175*5503fcecSJason Wang netif_receive_skb(skb); 1176*5503fcecSJason Wang local_bh_enable(); 1177*5503fcecSJason Wang } 1178*5503fcecSJason Wang } 1179*5503fcecSJason Wang 11801da177e4SLinus Torvalds /* Get packet from user space buffer */ 118154f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, 1182f5ff53b4SAl Viro void *msg_control, struct iov_iter *from, 1183*5503fcecSJason Wang int noblock, bool more) 11841da177e4SLinus Torvalds { 118509640e63SHarvey Harrison struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; 11861da177e4SLinus Torvalds struct sk_buff *skb; 1187f5ff53b4SAl Viro size_t total_len = iov_iter_count(from); 1188eaea34b2SPaolo Abeni size_t len = total_len, align = tun->align, linear; 1189f43798c2SRusty Russell struct virtio_net_hdr gso = { 0 }; 1190608b9977SPaolo Abeni struct tun_pcpu_stats *stats; 119196f8d9ecSJason Wang int good_linear; 11920690899bSMichael S. Tsirkin int copylen; 11930690899bSMichael S. Tsirkin bool zerocopy = false; 11940690899bSMichael S. Tsirkin int err; 119549974420SEric Dumazet u32 rxhash; 11961da177e4SLinus Torvalds 11971bd4978aSEric Dumazet if (!(tun->dev->flags & IFF_UP)) 11981bd4978aSEric Dumazet return -EIO; 11991bd4978aSEric Dumazet 120040630b82SMichael S. Tsirkin if (!(tun->flags & IFF_NO_PI)) { 120115718ea0SDan Carpenter if (len < sizeof(pi)) 12021da177e4SLinus Torvalds return -EINVAL; 120315718ea0SDan Carpenter len -= sizeof(pi); 12041da177e4SLinus Torvalds 1205cbbd26b8SAl Viro if (!copy_from_iter_full(&pi, sizeof(pi), from)) 12061da177e4SLinus Torvalds return -EFAULT; 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds 120940630b82SMichael S. Tsirkin if (tun->flags & IFF_VNET_HDR) { 121015718ea0SDan Carpenter if (len < tun->vnet_hdr_sz) 1211f43798c2SRusty Russell return -EINVAL; 121215718ea0SDan Carpenter len -= tun->vnet_hdr_sz; 1213f43798c2SRusty Russell 1214cbbd26b8SAl Viro if (!copy_from_iter_full(&gso, sizeof(gso), from)) 1215f43798c2SRusty Russell return -EFAULT; 1216f43798c2SRusty Russell 12174909122fSHerbert Xu if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && 121856f0dcc5SMichael S. Tsirkin tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len)) 121956f0dcc5SMichael S. Tsirkin gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2); 12204909122fSHerbert Xu 122156f0dcc5SMichael S. Tsirkin if (tun16_to_cpu(tun, gso.hdr_len) > len) 1222f43798c2SRusty Russell return -EINVAL; 1223d8febb77SHerbert Xu iov_iter_advance(from, tun->vnet_hdr_sz - sizeof(gso)); 1224f43798c2SRusty Russell } 1225f43798c2SRusty Russell 122640630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) { 1227a504b86eSstephen hemminger align += NET_IP_ALIGN; 12280eca93bcSHerbert Xu if (unlikely(len < ETH_HLEN || 122956f0dcc5SMichael S. Tsirkin (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN))) 1230e01bf1c8SRusty Russell return -EINVAL; 1231e01bf1c8SRusty Russell } 12321da177e4SLinus Torvalds 123396f8d9ecSJason Wang good_linear = SKB_MAX_HEAD(align); 123496f8d9ecSJason Wang 123588529176SJason Wang if (msg_control) { 1236f5ff53b4SAl Viro struct iov_iter i = *from; 1237f5ff53b4SAl Viro 123888529176SJason Wang /* There are 256 bytes to be copied in skb, so there is 123988529176SJason Wang * enough room for skb expand head in case it is used. 12400690899bSMichael S. Tsirkin * The rest of the buffer is mapped from userspace. 12410690899bSMichael S. Tsirkin */ 124256f0dcc5SMichael S. Tsirkin copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN; 124396f8d9ecSJason Wang if (copylen > good_linear) 124496f8d9ecSJason Wang copylen = good_linear; 12453dd5c330SJason Wang linear = copylen; 1246f5ff53b4SAl Viro iov_iter_advance(&i, copylen); 1247f5ff53b4SAl Viro if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS) 124888529176SJason Wang zerocopy = true; 124988529176SJason Wang } 125088529176SJason Wang 125188529176SJason Wang if (!zerocopy) { 12520690899bSMichael S. Tsirkin copylen = len; 125356f0dcc5SMichael S. Tsirkin if (tun16_to_cpu(tun, gso.hdr_len) > good_linear) 125496f8d9ecSJason Wang linear = good_linear; 125596f8d9ecSJason Wang else 125656f0dcc5SMichael S. Tsirkin linear = tun16_to_cpu(tun, gso.hdr_len); 12573dd5c330SJason Wang } 12580690899bSMichael S. Tsirkin 12593dd5c330SJason Wang skb = tun_alloc_skb(tfile, align, copylen, linear, noblock); 126033dccbb0SHerbert Xu if (IS_ERR(skb)) { 126133dccbb0SHerbert Xu if (PTR_ERR(skb) != -EAGAIN) 1262608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 126333dccbb0SHerbert Xu return PTR_ERR(skb); 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds 12660690899bSMichael S. Tsirkin if (zerocopy) 1267f5ff53b4SAl Viro err = zerocopy_sg_from_iter(skb, from); 1268af1cc7a2SJason Wang else 1269f5ff53b4SAl Viro err = skb_copy_datagram_from_iter(skb, 0, from, len); 12700690899bSMichael S. Tsirkin 12710690899bSMichael S. Tsirkin if (err) { 1272608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 12738f22757eSDave Jones kfree_skb(skb); 12741da177e4SLinus Torvalds return -EFAULT; 12758f22757eSDave Jones } 12761da177e4SLinus Torvalds 12773e9e40e7SJarno Rajahalme if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) { 1278df10db98SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_frame_errors); 1279df10db98SPaolo Abeni kfree_skb(skb); 1280df10db98SPaolo Abeni return -EINVAL; 1281df10db98SPaolo Abeni } 1282df10db98SPaolo Abeni 12831da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 128440630b82SMichael S. Tsirkin case IFF_TUN: 128540630b82SMichael S. Tsirkin if (tun->flags & IFF_NO_PI) { 1286f09f7ee2SAng Way Chuang switch (skb->data[0] & 0xf0) { 1287f09f7ee2SAng Way Chuang case 0x40: 1288f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IP); 1289f09f7ee2SAng Way Chuang break; 1290f09f7ee2SAng Way Chuang case 0x60: 1291f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IPV6); 1292f09f7ee2SAng Way Chuang break; 1293f09f7ee2SAng Way Chuang default: 1294608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 1295f09f7ee2SAng Way Chuang kfree_skb(skb); 1296f09f7ee2SAng Way Chuang return -EINVAL; 1297f09f7ee2SAng Way Chuang } 1298f09f7ee2SAng Way Chuang } 1299f09f7ee2SAng Way Chuang 1300459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 13011da177e4SLinus Torvalds skb->protocol = pi.proto; 13024c13eb66SArnaldo Carvalho de Melo skb->dev = tun->dev; 13031da177e4SLinus Torvalds break; 130440630b82SMichael S. Tsirkin case IFF_TAP: 13051da177e4SLinus Torvalds skb->protocol = eth_type_trans(skb, tun->dev); 13061da177e4SLinus Torvalds break; 13076403eab1SJoe Perches } 13081da177e4SLinus Torvalds 13090690899bSMichael S. Tsirkin /* copy skb_ubuf_info for callback when skb has no error */ 13100690899bSMichael S. Tsirkin if (zerocopy) { 13110690899bSMichael S. Tsirkin skb_shinfo(skb)->destructor_arg = msg_control; 13120690899bSMichael S. Tsirkin skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; 1313c9af6db4SPravin B Shelar skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; 1314af1cc7a2SJason Wang } else if (msg_control) { 1315af1cc7a2SJason Wang struct ubuf_info *uarg = msg_control; 1316af1cc7a2SJason Wang uarg->callback(uarg, false); 13170690899bSMichael S. Tsirkin } 13180690899bSMichael S. Tsirkin 131972f65107SVlad Yasevich skb_reset_network_header(skb); 132040893fd0SJason Wang skb_probe_transport_header(skb, 0); 132138502af7SJason Wang 13223958afa1STom Herbert rxhash = skb_get_hash(skb); 1323d4aea20dSAndrey Konovalov #ifndef CONFIG_4KSTACKS 1324*5503fcecSJason Wang tun_rx_batched(tun, tfile, skb, more); 1325d4aea20dSAndrey Konovalov #else 13261da177e4SLinus Torvalds netif_rx_ni(skb); 1327d4aea20dSAndrey Konovalov #endif 13281da177e4SLinus Torvalds 1329608b9977SPaolo Abeni stats = get_cpu_ptr(tun->pcpu_stats); 1330608b9977SPaolo Abeni u64_stats_update_begin(&stats->syncp); 1331608b9977SPaolo Abeni stats->rx_packets++; 1332608b9977SPaolo Abeni stats->rx_bytes += len; 1333608b9977SPaolo Abeni u64_stats_update_end(&stats->syncp); 1334608b9977SPaolo Abeni put_cpu_ptr(stats); 13351da177e4SLinus Torvalds 13369e85722dSJason Wang tun_flow_update(tun, rxhash, tfile); 13370690899bSMichael S. Tsirkin return total_len; 13381da177e4SLinus Torvalds } 13391da177e4SLinus Torvalds 1340f5ff53b4SAl Viro static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from) 13411da177e4SLinus Torvalds { 134233dccbb0SHerbert Xu struct file *file = iocb->ki_filp; 1343ab46d779SHerbert Xu struct tun_struct *tun = tun_get(file); 134454f968d6SJason Wang struct tun_file *tfile = file->private_data; 1345631ab46bSEric W. Biederman ssize_t result; 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds if (!tun) 13481da177e4SLinus Torvalds return -EBADFD; 13491da177e4SLinus Torvalds 1350*5503fcecSJason Wang result = tun_get_user(tun, tfile, NULL, from, 1351*5503fcecSJason Wang file->f_flags & O_NONBLOCK, false); 1352631ab46bSEric W. Biederman 1353631ab46bSEric W. Biederman tun_put(tun); 1354631ab46bSEric W. Biederman return result; 13551da177e4SLinus Torvalds } 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds /* Put packet to the user space buffer */ 13586f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun, 135954f968d6SJason Wang struct tun_file *tfile, 13601da177e4SLinus Torvalds struct sk_buff *skb, 1361e0b46d0eSHerbert Xu struct iov_iter *iter) 13621da177e4SLinus Torvalds { 13631da177e4SLinus Torvalds struct tun_pi pi = { 0, skb->protocol }; 1364608b9977SPaolo Abeni struct tun_pcpu_stats *stats; 1365e0b46d0eSHerbert Xu ssize_t total; 13668c847d25SJason Wang int vlan_offset = 0; 1367a8f9bfdfSHerbert Xu int vlan_hlen = 0; 13682eb783c4SHerbert Xu int vnet_hdr_sz = 0; 1369a8f9bfdfSHerbert Xu 1370df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) 1371a8f9bfdfSHerbert Xu vlan_hlen = VLAN_HLEN; 13721da177e4SLinus Torvalds 137340630b82SMichael S. Tsirkin if (tun->flags & IFF_VNET_HDR) 13742eb783c4SHerbert Xu vnet_hdr_sz = tun->vnet_hdr_sz; 13751da177e4SLinus Torvalds 1376e0b46d0eSHerbert Xu total = skb->len + vlan_hlen + vnet_hdr_sz; 1377e0b46d0eSHerbert Xu 137840630b82SMichael S. Tsirkin if (!(tun->flags & IFF_NO_PI)) { 1379e0b46d0eSHerbert Xu if (iov_iter_count(iter) < sizeof(pi)) 13801da177e4SLinus Torvalds return -EINVAL; 13811da177e4SLinus Torvalds 1382e0b46d0eSHerbert Xu total += sizeof(pi); 1383e0b46d0eSHerbert Xu if (iov_iter_count(iter) < total) { 13841da177e4SLinus Torvalds /* Packet will be striped */ 13851da177e4SLinus Torvalds pi.flags |= TUN_PKT_STRIP; 13861da177e4SLinus Torvalds } 13871da177e4SLinus Torvalds 1388e0b46d0eSHerbert Xu if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi)) 13891da177e4SLinus Torvalds return -EFAULT; 13901da177e4SLinus Torvalds } 13911da177e4SLinus Torvalds 13922eb783c4SHerbert Xu if (vnet_hdr_sz) { 13939403cd7cSJarno Rajahalme struct virtio_net_hdr gso; 139434166093SMike Rapoport 1395e0b46d0eSHerbert Xu if (iov_iter_count(iter) < vnet_hdr_sz) 1396f43798c2SRusty Russell return -EINVAL; 1397f43798c2SRusty Russell 13983e9e40e7SJarno Rajahalme if (virtio_net_hdr_from_skb(skb, &gso, 13993e9e40e7SJarno Rajahalme tun_is_little_endian(tun))) { 1400f43798c2SRusty Russell struct skb_shared_info *sinfo = skb_shinfo(skb); 14016b8a66eeSJoe Perches pr_err("unexpected GSO type: " 1402ef3db4a5SMichael S. Tsirkin "0x%x, gso_size %d, hdr_len %d\n", 140356f0dcc5SMichael S. Tsirkin sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size), 140456f0dcc5SMichael S. Tsirkin tun16_to_cpu(tun, gso.hdr_len)); 1405ef3db4a5SMichael S. Tsirkin print_hex_dump(KERN_ERR, "tun: ", 1406ef3db4a5SMichael S. Tsirkin DUMP_PREFIX_NONE, 1407ef3db4a5SMichael S. Tsirkin 16, 1, skb->head, 140856f0dcc5SMichael S. Tsirkin min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true); 1409ef3db4a5SMichael S. Tsirkin WARN_ON_ONCE(1); 1410ef3db4a5SMichael S. Tsirkin return -EINVAL; 1411ef3db4a5SMichael S. Tsirkin } 1412f43798c2SRusty Russell 1413e0b46d0eSHerbert Xu if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso)) 1414f43798c2SRusty Russell return -EFAULT; 14158c847d25SJason Wang 14168c847d25SJason Wang iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso)); 1417f43798c2SRusty Russell } 1418f43798c2SRusty Russell 1419a8f9bfdfSHerbert Xu if (vlan_hlen) { 1420e0b46d0eSHerbert Xu int ret; 14216680ec68SJason Wang struct { 14226680ec68SJason Wang __be16 h_vlan_proto; 14236680ec68SJason Wang __be16 h_vlan_TCI; 14246680ec68SJason Wang } veth; 14251da177e4SLinus Torvalds 14266680ec68SJason Wang veth.h_vlan_proto = skb->vlan_proto; 1427df8a39deSJiri Pirko veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb)); 14281da177e4SLinus Torvalds 14296680ec68SJason Wang vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); 14306680ec68SJason Wang 1431e0b46d0eSHerbert Xu ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset); 1432e0b46d0eSHerbert Xu if (ret || !iov_iter_count(iter)) 14336680ec68SJason Wang goto done; 14346680ec68SJason Wang 1435e0b46d0eSHerbert Xu ret = copy_to_iter(&veth, sizeof(veth), iter); 1436e0b46d0eSHerbert Xu if (ret != sizeof(veth) || !iov_iter_count(iter)) 14376680ec68SJason Wang goto done; 14386680ec68SJason Wang } 14396680ec68SJason Wang 1440e0b46d0eSHerbert Xu skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset); 14416680ec68SJason Wang 14426680ec68SJason Wang done: 1443608b9977SPaolo Abeni /* caller is in process context, */ 1444608b9977SPaolo Abeni stats = get_cpu_ptr(tun->pcpu_stats); 1445608b9977SPaolo Abeni u64_stats_update_begin(&stats->syncp); 1446608b9977SPaolo Abeni stats->tx_packets++; 1447608b9977SPaolo Abeni stats->tx_bytes += skb->len + vlan_hlen; 1448608b9977SPaolo Abeni u64_stats_update_end(&stats->syncp); 1449608b9977SPaolo Abeni put_cpu_ptr(tun->pcpu_stats); 14501da177e4SLinus Torvalds 14511da177e4SLinus Torvalds return total; 14521da177e4SLinus Torvalds } 14531da177e4SLinus Torvalds 14541576d986SJason Wang static struct sk_buff *tun_ring_recv(struct tun_file *tfile, int noblock, 14551576d986SJason Wang int *err) 14561576d986SJason Wang { 14571576d986SJason Wang DECLARE_WAITQUEUE(wait, current); 14581576d986SJason Wang struct sk_buff *skb = NULL; 1459f48cc6b2SJason Wang int error = 0; 14601576d986SJason Wang 14611576d986SJason Wang skb = skb_array_consume(&tfile->tx_array); 14621576d986SJason Wang if (skb) 14631576d986SJason Wang goto out; 14641576d986SJason Wang if (noblock) { 1465f48cc6b2SJason Wang error = -EAGAIN; 14661576d986SJason Wang goto out; 14671576d986SJason Wang } 14681576d986SJason Wang 14691576d986SJason Wang add_wait_queue(&tfile->wq.wait, &wait); 14701576d986SJason Wang current->state = TASK_INTERRUPTIBLE; 14711576d986SJason Wang 14721576d986SJason Wang while (1) { 14731576d986SJason Wang skb = skb_array_consume(&tfile->tx_array); 14741576d986SJason Wang if (skb) 14751576d986SJason Wang break; 14761576d986SJason Wang if (signal_pending(current)) { 1477f48cc6b2SJason Wang error = -ERESTARTSYS; 14781576d986SJason Wang break; 14791576d986SJason Wang } 14801576d986SJason Wang if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) { 1481f48cc6b2SJason Wang error = -EFAULT; 14821576d986SJason Wang break; 14831576d986SJason Wang } 14841576d986SJason Wang 14851576d986SJason Wang schedule(); 14861576d986SJason Wang } 14871576d986SJason Wang 14881576d986SJason Wang current->state = TASK_RUNNING; 14891576d986SJason Wang remove_wait_queue(&tfile->wq.wait, &wait); 14901576d986SJason Wang 14911576d986SJason Wang out: 1492f48cc6b2SJason Wang *err = error; 14931576d986SJason Wang return skb; 14941576d986SJason Wang } 14951576d986SJason Wang 149654f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, 14979b067034SAl Viro struct iov_iter *to, 14989b067034SAl Viro int noblock) 14991da177e4SLinus Torvalds { 15001da177e4SLinus Torvalds struct sk_buff *skb; 15019b067034SAl Viro ssize_t ret; 15021576d986SJason Wang int err; 15031da177e4SLinus Torvalds 15043872baf6SRami Rosen tun_debug(KERN_INFO, tun, "tun_do_read\n"); 15051da177e4SLinus Torvalds 15069b067034SAl Viro if (!iov_iter_count(to)) 15079b067034SAl Viro return 0; 15081da177e4SLinus Torvalds 15091576d986SJason Wang /* Read frames from ring */ 15101576d986SJason Wang skb = tun_ring_recv(tfile, noblock, &err); 1511e0b46d0eSHerbert Xu if (!skb) 1512957f094fSAlex Gartrell return err; 1513e0b46d0eSHerbert Xu 15149b067034SAl Viro ret = tun_put_user(tun, tfile, skb, to); 1515f51a5e82SJason Wang if (unlikely(ret < 0)) 15161da177e4SLinus Torvalds kfree_skb(skb); 1517f51a5e82SJason Wang else 1518f51a5e82SJason Wang consume_skb(skb); 15191da177e4SLinus Torvalds 152005c2828cSMichael S. Tsirkin return ret; 152105c2828cSMichael S. Tsirkin } 152205c2828cSMichael S. Tsirkin 15239b067034SAl Viro static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to) 152405c2828cSMichael S. Tsirkin { 152505c2828cSMichael S. Tsirkin struct file *file = iocb->ki_filp; 152605c2828cSMichael S. Tsirkin struct tun_file *tfile = file->private_data; 152705c2828cSMichael S. Tsirkin struct tun_struct *tun = __tun_get(tfile); 15289b067034SAl Viro ssize_t len = iov_iter_count(to), ret; 152905c2828cSMichael S. Tsirkin 153005c2828cSMichael S. Tsirkin if (!tun) 153105c2828cSMichael S. Tsirkin return -EBADFD; 15329b067034SAl Viro ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK); 153342404c09SDavid S. Miller ret = min_t(ssize_t, ret, len); 1534d0b7da8aSZhi Yong Wu if (ret > 0) 1535d0b7da8aSZhi Yong Wu iocb->ki_pos = ret; 1536631ab46bSEric W. Biederman tun_put(tun); 15371da177e4SLinus Torvalds return ret; 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds 154096442e42SJason Wang static void tun_free_netdev(struct net_device *dev) 154196442e42SJason Wang { 154296442e42SJason Wang struct tun_struct *tun = netdev_priv(dev); 154396442e42SJason Wang 15444008e97fSJason Wang BUG_ON(!(list_empty(&tun->disabled))); 1545608b9977SPaolo Abeni free_percpu(tun->pcpu_stats); 154696442e42SJason Wang tun_flow_uninit(tun); 15475dbbaf2dSPaul Moore security_tun_dev_free_security(tun->security); 154896442e42SJason Wang free_netdev(dev); 154996442e42SJason Wang } 155096442e42SJason Wang 15511da177e4SLinus Torvalds static void tun_setup(struct net_device *dev) 15521da177e4SLinus Torvalds { 15531da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 15541da177e4SLinus Torvalds 15550625c883SEric W. Biederman tun->owner = INVALID_UID; 15560625c883SEric W. Biederman tun->group = INVALID_GID; 15571da177e4SLinus Torvalds 15581da177e4SLinus Torvalds dev->ethtool_ops = &tun_ethtool_ops; 155996442e42SJason Wang dev->destructor = tun_free_netdev; 1560016adb72SJason Wang /* We prefer our own queue length */ 1561016adb72SJason Wang dev->tx_queue_len = TUN_READQ_SIZE; 15621da177e4SLinus Torvalds } 15631da177e4SLinus Torvalds 1564f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap 1565f019a7a5SEric W. Biederman * device with netlink. 1566f019a7a5SEric W. Biederman */ 1567f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[]) 1568f019a7a5SEric W. Biederman { 1569f019a7a5SEric W. Biederman return -EINVAL; 1570f019a7a5SEric W. Biederman } 1571f019a7a5SEric W. Biederman 1572f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = { 1573f019a7a5SEric W. Biederman .kind = DRV_NAME, 1574f019a7a5SEric W. Biederman .priv_size = sizeof(struct tun_struct), 1575f019a7a5SEric W. Biederman .setup = tun_setup, 1576f019a7a5SEric W. Biederman .validate = tun_validate, 1577f019a7a5SEric W. Biederman }; 1578f019a7a5SEric W. Biederman 157933dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk) 158033dccbb0SHerbert Xu { 158154f968d6SJason Wang struct tun_file *tfile; 158243815482SEric Dumazet wait_queue_head_t *wqueue; 158333dccbb0SHerbert Xu 158433dccbb0SHerbert Xu if (!sock_writeable(sk)) 158533dccbb0SHerbert Xu return; 158633dccbb0SHerbert Xu 15879cd3e072SEric Dumazet if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags)) 158833dccbb0SHerbert Xu return; 158933dccbb0SHerbert Xu 159043815482SEric Dumazet wqueue = sk_sleep(sk); 159143815482SEric Dumazet if (wqueue && waitqueue_active(wqueue)) 159243815482SEric Dumazet wake_up_interruptible_sync_poll(wqueue, POLLOUT | 159305c2828cSMichael S. Tsirkin POLLWRNORM | POLLWRBAND); 1594c722c625SHerbert Xu 159554f968d6SJason Wang tfile = container_of(sk, struct tun_file, sk); 159654f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); 159733dccbb0SHerbert Xu } 159833dccbb0SHerbert Xu 15991b784140SYing Xue static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len) 160005c2828cSMichael S. Tsirkin { 160154f968d6SJason Wang int ret; 160254f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 160354f968d6SJason Wang struct tun_struct *tun = __tun_get(tfile); 160454f968d6SJason Wang 160554f968d6SJason Wang if (!tun) 160654f968d6SJason Wang return -EBADFD; 1607f5ff53b4SAl Viro 1608c0371da6SAl Viro ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter, 1609*5503fcecSJason Wang m->msg_flags & MSG_DONTWAIT, 1610*5503fcecSJason Wang m->msg_flags & MSG_MORE); 161154f968d6SJason Wang tun_put(tun); 161254f968d6SJason Wang return ret; 161305c2828cSMichael S. Tsirkin } 161405c2828cSMichael S. Tsirkin 16151b784140SYing Xue static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, 161605c2828cSMichael S. Tsirkin int flags) 161705c2828cSMichael S. Tsirkin { 161854f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 161954f968d6SJason Wang struct tun_struct *tun = __tun_get(tfile); 162005c2828cSMichael S. Tsirkin int ret; 162154f968d6SJason Wang 162254f968d6SJason Wang if (!tun) 162354f968d6SJason Wang return -EBADFD; 162454f968d6SJason Wang 1625eda29772SRichard Cochran if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) { 16263811ae76SGao feng ret = -EINVAL; 16273811ae76SGao feng goto out; 16283811ae76SGao feng } 1629eda29772SRichard Cochran if (flags & MSG_ERRQUEUE) { 1630eda29772SRichard Cochran ret = sock_recv_errqueue(sock->sk, m, total_len, 1631eda29772SRichard Cochran SOL_PACKET, TUN_TX_TIMESTAMP); 1632eda29772SRichard Cochran goto out; 1633eda29772SRichard Cochran } 1634c0371da6SAl Viro ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT); 163587897931SAlex Gartrell if (ret > (ssize_t)total_len) { 163642404c09SDavid S. Miller m->msg_flags |= MSG_TRUNC; 163742404c09SDavid S. Miller ret = flags & MSG_TRUNC ? ret : total_len; 163842404c09SDavid S. Miller } 16393811ae76SGao feng out: 164054f968d6SJason Wang tun_put(tun); 164105c2828cSMichael S. Tsirkin return ret; 164205c2828cSMichael S. Tsirkin } 164305c2828cSMichael S. Tsirkin 16441576d986SJason Wang static int tun_peek_len(struct socket *sock) 16451576d986SJason Wang { 16461576d986SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 16471576d986SJason Wang struct tun_struct *tun; 16481576d986SJason Wang int ret = 0; 16491576d986SJason Wang 16501576d986SJason Wang tun = __tun_get(tfile); 16511576d986SJason Wang if (!tun) 16521576d986SJason Wang return 0; 16531576d986SJason Wang 16541576d986SJason Wang ret = skb_array_peek_len(&tfile->tx_array); 16551576d986SJason Wang tun_put(tun); 16561576d986SJason Wang 16571576d986SJason Wang return ret; 16581576d986SJason Wang } 16591576d986SJason Wang 166005c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */ 166105c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = { 16621576d986SJason Wang .peek_len = tun_peek_len, 166305c2828cSMichael S. Tsirkin .sendmsg = tun_sendmsg, 166405c2828cSMichael S. Tsirkin .recvmsg = tun_recvmsg, 166505c2828cSMichael S. Tsirkin }; 166605c2828cSMichael S. Tsirkin 166733dccbb0SHerbert Xu static struct proto tun_proto = { 166833dccbb0SHerbert Xu .name = "tun", 166933dccbb0SHerbert Xu .owner = THIS_MODULE, 167054f968d6SJason Wang .obj_size = sizeof(struct tun_file), 167133dccbb0SHerbert Xu }; 1672f019a7a5SEric W. Biederman 1673980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun) 1674980c9e8cSDavid Woodhouse { 1675031f5e03SMichael S. Tsirkin return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP); 1676980c9e8cSDavid Woodhouse } 1677980c9e8cSDavid Woodhouse 1678980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr, 1679980c9e8cSDavid Woodhouse char *buf) 1680980c9e8cSDavid Woodhouse { 1681980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 1682980c9e8cSDavid Woodhouse return sprintf(buf, "0x%x\n", tun_flags(tun)); 1683980c9e8cSDavid Woodhouse } 1684980c9e8cSDavid Woodhouse 1685980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr, 1686980c9e8cSDavid Woodhouse char *buf) 1687980c9e8cSDavid Woodhouse { 1688980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 16890625c883SEric W. Biederman return uid_valid(tun->owner)? 16900625c883SEric W. Biederman sprintf(buf, "%u\n", 16910625c883SEric W. Biederman from_kuid_munged(current_user_ns(), tun->owner)): 16920625c883SEric W. Biederman sprintf(buf, "-1\n"); 1693980c9e8cSDavid Woodhouse } 1694980c9e8cSDavid Woodhouse 1695980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr, 1696980c9e8cSDavid Woodhouse char *buf) 1697980c9e8cSDavid Woodhouse { 1698980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 16990625c883SEric W. Biederman return gid_valid(tun->group) ? 17000625c883SEric W. Biederman sprintf(buf, "%u\n", 17010625c883SEric W. Biederman from_kgid_munged(current_user_ns(), tun->group)): 17020625c883SEric W. Biederman sprintf(buf, "-1\n"); 1703980c9e8cSDavid Woodhouse } 1704980c9e8cSDavid Woodhouse 1705980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL); 1706980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL); 1707980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL); 1708980c9e8cSDavid Woodhouse 1709c4d33e24STakashi Iwai static struct attribute *tun_dev_attrs[] = { 1710c4d33e24STakashi Iwai &dev_attr_tun_flags.attr, 1711c4d33e24STakashi Iwai &dev_attr_owner.attr, 1712c4d33e24STakashi Iwai &dev_attr_group.attr, 1713c4d33e24STakashi Iwai NULL 1714c4d33e24STakashi Iwai }; 1715c4d33e24STakashi Iwai 1716c4d33e24STakashi Iwai static const struct attribute_group tun_attr_group = { 1717c4d33e24STakashi Iwai .attrs = tun_dev_attrs 1718c4d33e24STakashi Iwai }; 1719c4d33e24STakashi Iwai 1720d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) 17211da177e4SLinus Torvalds { 17221da177e4SLinus Torvalds struct tun_struct *tun; 172354f968d6SJason Wang struct tun_file *tfile = file->private_data; 17241da177e4SLinus Torvalds struct net_device *dev; 17251da177e4SLinus Torvalds int err; 17261da177e4SLinus Torvalds 17277c0c3b1aSJason Wang if (tfile->detached) 17287c0c3b1aSJason Wang return -EINVAL; 17297c0c3b1aSJason Wang 173074a3e5a7SEric W. Biederman dev = __dev_get_by_name(net, ifr->ifr_name); 173174a3e5a7SEric W. Biederman if (dev) { 1732f85ba780SDavid Woodhouse if (ifr->ifr_flags & IFF_TUN_EXCL) 1733f85ba780SDavid Woodhouse return -EBUSY; 173474a3e5a7SEric W. Biederman if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) 173574a3e5a7SEric W. Biederman tun = netdev_priv(dev); 173674a3e5a7SEric W. Biederman else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) 173774a3e5a7SEric W. Biederman tun = netdev_priv(dev); 173874a3e5a7SEric W. Biederman else 173974a3e5a7SEric W. Biederman return -EINVAL; 174074a3e5a7SEric W. Biederman 17418e6d91aeSJason Wang if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) != 174240630b82SMichael S. Tsirkin !!(tun->flags & IFF_MULTI_QUEUE)) 17438e6d91aeSJason Wang return -EINVAL; 17448e6d91aeSJason Wang 1745cde8b15fSJason Wang if (tun_not_capable(tun)) 17462b980dbdSPaul Moore return -EPERM; 17475dbbaf2dSPaul Moore err = security_tun_dev_open(tun->security); 17482b980dbdSPaul Moore if (err < 0) 17492b980dbdSPaul Moore return err; 17502b980dbdSPaul Moore 1751849c9b6fSPavel Emelyanov err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER); 1752a7385ba2SEric W. Biederman if (err < 0) 1753a7385ba2SEric W. Biederman return err; 17544008e97fSJason Wang 175540630b82SMichael S. Tsirkin if (tun->flags & IFF_MULTI_QUEUE && 1756e8dbad66SJason Wang (tun->numqueues + tun->numdisabled > 1)) { 1757e8dbad66SJason Wang /* One or more queue has already been attached, no need 1758e8dbad66SJason Wang * to initialize the device again. 1759e8dbad66SJason Wang */ 1760e8dbad66SJason Wang return 0; 1761e8dbad66SJason Wang } 176286a264abSDavid Howells } 17631da177e4SLinus Torvalds else { 17641da177e4SLinus Torvalds char *name; 17651da177e4SLinus Torvalds unsigned long flags = 0; 1766edfb6a14SJason Wang int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ? 1767edfb6a14SJason Wang MAX_TAP_QUEUES : 1; 17681da177e4SLinus Torvalds 1769c260b772SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1770ca6bb5d7SDavid Woodhouse return -EPERM; 17712b980dbdSPaul Moore err = security_tun_dev_create(); 17722b980dbdSPaul Moore if (err < 0) 17732b980dbdSPaul Moore return err; 1774ca6bb5d7SDavid Woodhouse 17751da177e4SLinus Torvalds /* Set dev type */ 17761da177e4SLinus Torvalds if (ifr->ifr_flags & IFF_TUN) { 17771da177e4SLinus Torvalds /* TUN device */ 177840630b82SMichael S. Tsirkin flags |= IFF_TUN; 17791da177e4SLinus Torvalds name = "tun%d"; 17801da177e4SLinus Torvalds } else if (ifr->ifr_flags & IFF_TAP) { 17811da177e4SLinus Torvalds /* TAP device */ 178240630b82SMichael S. Tsirkin flags |= IFF_TAP; 17831da177e4SLinus Torvalds name = "tap%d"; 17841da177e4SLinus Torvalds } else 178536989b90SKusanagi Kouichi return -EINVAL; 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds if (*ifr->ifr_name) 17881da177e4SLinus Torvalds name = ifr->ifr_name; 17891da177e4SLinus Torvalds 1790c8d68e6bSJason Wang dev = alloc_netdev_mqs(sizeof(struct tun_struct), name, 1791c835a677STom Gundersen NET_NAME_UNKNOWN, tun_setup, queues, 1792c835a677STom Gundersen queues); 1793edfb6a14SJason Wang 17941da177e4SLinus Torvalds if (!dev) 17951da177e4SLinus Torvalds return -ENOMEM; 17961da177e4SLinus Torvalds 1797fc54c658SPavel Emelyanov dev_net_set(dev, net); 1798f019a7a5SEric W. Biederman dev->rtnl_link_ops = &tun_link_ops; 1799fb7589a1SPavel Emelyanov dev->ifindex = tfile->ifindex; 1800c4d33e24STakashi Iwai dev->sysfs_groups[0] = &tun_attr_group; 1801758e43b7SStephen Hemminger 18021da177e4SLinus Torvalds tun = netdev_priv(dev); 18031da177e4SLinus Torvalds tun->dev = dev; 18041da177e4SLinus Torvalds tun->flags = flags; 1805f271b2ccSMax Krasnyansky tun->txflt.count = 0; 1806d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr); 18071da177e4SLinus Torvalds 1808eaea34b2SPaolo Abeni tun->align = NET_SKB_PAD; 180954f968d6SJason Wang tun->filter_attached = false; 181054f968d6SJason Wang tun->sndbuf = tfile->socket.sk->sk_sndbuf; 1811*5503fcecSJason Wang tun->rx_batched = 0; 181233dccbb0SHerbert Xu 1813608b9977SPaolo Abeni tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats); 1814608b9977SPaolo Abeni if (!tun->pcpu_stats) { 1815608b9977SPaolo Abeni err = -ENOMEM; 1816608b9977SPaolo Abeni goto err_free_dev; 1817608b9977SPaolo Abeni } 1818608b9977SPaolo Abeni 181996442e42SJason Wang spin_lock_init(&tun->lock); 182096442e42SJason Wang 18215dbbaf2dSPaul Moore err = security_tun_dev_alloc_security(&tun->security); 18225dbbaf2dSPaul Moore if (err < 0) 1823608b9977SPaolo Abeni goto err_free_stat; 18242b980dbdSPaul Moore 18251da177e4SLinus Torvalds tun_net_init(dev); 1826944a1376SPavel Emelyanov tun_flow_init(tun); 182796442e42SJason Wang 182888255375SMichał Mirosław dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | 18296680ec68SJason Wang TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX | 18306680ec68SJason Wang NETIF_F_HW_VLAN_STAG_TX; 18312a2bbf17SPaolo Abeni dev->features = dev->hw_features | NETIF_F_LLTX; 18326671b224SFernando Luis Vazquez Cao dev->vlan_features = dev->features & 18336671b224SFernando Luis Vazquez Cao ~(NETIF_F_HW_VLAN_CTAG_TX | 18346671b224SFernando Luis Vazquez Cao NETIF_F_HW_VLAN_STAG_TX); 183588255375SMichał Mirosław 18364008e97fSJason Wang INIT_LIST_HEAD(&tun->disabled); 1837849c9b6fSPavel Emelyanov err = tun_attach(tun, file, false); 1838eb0fb363SJason Wang if (err < 0) 1839662ca437SJason Wang goto err_free_flow; 1840eb0fb363SJason Wang 18411da177e4SLinus Torvalds err = register_netdevice(tun->dev); 18421da177e4SLinus Torvalds if (err < 0) 1843662ca437SJason Wang goto err_detach; 1844af668b3cSMichael S. Tsirkin } 1845980c9e8cSDavid Woodhouse 1846eb0fb363SJason Wang netif_carrier_on(tun->dev); 18471da177e4SLinus Torvalds 18486b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_set_iff\n"); 18491da177e4SLinus Torvalds 1850031f5e03SMichael S. Tsirkin tun->flags = (tun->flags & ~TUN_FEATURES) | 1851031f5e03SMichael S. Tsirkin (ifr->ifr_flags & TUN_FEATURES); 1852c8d68e6bSJason Wang 1853e35259a9SMax Krasnyansky /* Make sure persistent devices do not get stuck in 1854e35259a9SMax Krasnyansky * xoff state. 1855e35259a9SMax Krasnyansky */ 1856e35259a9SMax Krasnyansky if (netif_running(tun->dev)) 1857c8d68e6bSJason Wang netif_tx_wake_all_queues(tun->dev); 1858e35259a9SMax Krasnyansky 18591da177e4SLinus Torvalds strcpy(ifr->ifr_name, tun->dev->name); 18601da177e4SLinus Torvalds return 0; 18611da177e4SLinus Torvalds 1862662ca437SJason Wang err_detach: 1863662ca437SJason Wang tun_detach_all(dev); 1864662ca437SJason Wang err_free_flow: 1865662ca437SJason Wang tun_flow_uninit(tun); 1866662ca437SJason Wang security_tun_dev_free_security(tun->security); 1867608b9977SPaolo Abeni err_free_stat: 1868608b9977SPaolo Abeni free_percpu(tun->pcpu_stats); 18691da177e4SLinus Torvalds err_free_dev: 18701da177e4SLinus Torvalds free_netdev(dev); 18711da177e4SLinus Torvalds return err; 18721da177e4SLinus Torvalds } 18731da177e4SLinus Torvalds 18749ce99cf6SRami Rosen static void tun_get_iff(struct net *net, struct tun_struct *tun, 1875876bfd4dSHerbert Xu struct ifreq *ifr) 1876e3b99556SMark McLoughlin { 18776b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_get_iff\n"); 1878e3b99556SMark McLoughlin 1879e3b99556SMark McLoughlin strcpy(ifr->ifr_name, tun->dev->name); 1880e3b99556SMark McLoughlin 1881980c9e8cSDavid Woodhouse ifr->ifr_flags = tun_flags(tun); 1882e3b99556SMark McLoughlin 1883e3b99556SMark McLoughlin } 1884e3b99556SMark McLoughlin 18855228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no 18865228ddc9SRusty Russell * privs required. */ 188788255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg) 18885228ddc9SRusty Russell { 1889c8f44affSMichał Mirosław netdev_features_t features = 0; 18905228ddc9SRusty Russell 18915228ddc9SRusty Russell if (arg & TUN_F_CSUM) { 189288255375SMichał Mirosław features |= NETIF_F_HW_CSUM; 18935228ddc9SRusty Russell arg &= ~TUN_F_CSUM; 18945228ddc9SRusty Russell 18955228ddc9SRusty Russell if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { 18965228ddc9SRusty Russell if (arg & TUN_F_TSO_ECN) { 18975228ddc9SRusty Russell features |= NETIF_F_TSO_ECN; 18985228ddc9SRusty Russell arg &= ~TUN_F_TSO_ECN; 18995228ddc9SRusty Russell } 19005228ddc9SRusty Russell if (arg & TUN_F_TSO4) 19015228ddc9SRusty Russell features |= NETIF_F_TSO; 19025228ddc9SRusty Russell if (arg & TUN_F_TSO6) 19035228ddc9SRusty Russell features |= NETIF_F_TSO6; 19045228ddc9SRusty Russell arg &= ~(TUN_F_TSO4|TUN_F_TSO6); 19055228ddc9SRusty Russell } 1906e3e3c423SVlad Yasevich 1907e3e3c423SVlad Yasevich if (arg & TUN_F_UFO) { 1908e3e3c423SVlad Yasevich features |= NETIF_F_UFO; 1909e3e3c423SVlad Yasevich arg &= ~TUN_F_UFO; 1910e3e3c423SVlad Yasevich } 19115228ddc9SRusty Russell } 19125228ddc9SRusty Russell 19135228ddc9SRusty Russell /* This gives the user a way to test for new features in future by 19145228ddc9SRusty Russell * trying to set them. */ 19155228ddc9SRusty Russell if (arg) 19165228ddc9SRusty Russell return -EINVAL; 19175228ddc9SRusty Russell 191888255375SMichał Mirosław tun->set_features = features; 191988255375SMichał Mirosław netdev_update_features(tun->dev); 19205228ddc9SRusty Russell 19215228ddc9SRusty Russell return 0; 19225228ddc9SRusty Russell } 19235228ddc9SRusty Russell 1924c8d68e6bSJason Wang static void tun_detach_filter(struct tun_struct *tun, int n) 1925c8d68e6bSJason Wang { 1926c8d68e6bSJason Wang int i; 1927c8d68e6bSJason Wang struct tun_file *tfile; 1928c8d68e6bSJason Wang 1929c8d68e6bSJason Wang for (i = 0; i < n; i++) { 1930b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 19318ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 19328ced425eSHannes Frederic Sowa sk_detach_filter(tfile->socket.sk); 19338ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 1934c8d68e6bSJason Wang } 1935c8d68e6bSJason Wang 1936c8d68e6bSJason Wang tun->filter_attached = false; 1937c8d68e6bSJason Wang } 1938c8d68e6bSJason Wang 1939c8d68e6bSJason Wang static int tun_attach_filter(struct tun_struct *tun) 1940c8d68e6bSJason Wang { 1941c8d68e6bSJason Wang int i, ret = 0; 1942c8d68e6bSJason Wang struct tun_file *tfile; 1943c8d68e6bSJason Wang 1944c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 1945b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 19468ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 19478ced425eSHannes Frederic Sowa ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); 19488ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 1949c8d68e6bSJason Wang if (ret) { 1950c8d68e6bSJason Wang tun_detach_filter(tun, i); 1951c8d68e6bSJason Wang return ret; 1952c8d68e6bSJason Wang } 1953c8d68e6bSJason Wang } 1954c8d68e6bSJason Wang 1955c8d68e6bSJason Wang tun->filter_attached = true; 1956c8d68e6bSJason Wang return ret; 1957c8d68e6bSJason Wang } 1958c8d68e6bSJason Wang 1959c8d68e6bSJason Wang static void tun_set_sndbuf(struct tun_struct *tun) 1960c8d68e6bSJason Wang { 1961c8d68e6bSJason Wang struct tun_file *tfile; 1962c8d68e6bSJason Wang int i; 1963c8d68e6bSJason Wang 1964c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 1965b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 1966c8d68e6bSJason Wang tfile->socket.sk->sk_sndbuf = tun->sndbuf; 1967c8d68e6bSJason Wang } 1968c8d68e6bSJason Wang } 1969c8d68e6bSJason Wang 1970cde8b15fSJason Wang static int tun_set_queue(struct file *file, struct ifreq *ifr) 1971cde8b15fSJason Wang { 1972cde8b15fSJason Wang struct tun_file *tfile = file->private_data; 1973cde8b15fSJason Wang struct tun_struct *tun; 1974cde8b15fSJason Wang int ret = 0; 1975cde8b15fSJason Wang 1976cde8b15fSJason Wang rtnl_lock(); 1977cde8b15fSJason Wang 1978cde8b15fSJason Wang if (ifr->ifr_flags & IFF_ATTACH_QUEUE) { 19794008e97fSJason Wang tun = tfile->detached; 19805dbbaf2dSPaul Moore if (!tun) { 1981cde8b15fSJason Wang ret = -EINVAL; 19825dbbaf2dSPaul Moore goto unlock; 19835dbbaf2dSPaul Moore } 19845dbbaf2dSPaul Moore ret = security_tun_dev_attach_queue(tun->security); 19855dbbaf2dSPaul Moore if (ret < 0) 19865dbbaf2dSPaul Moore goto unlock; 1987849c9b6fSPavel Emelyanov ret = tun_attach(tun, file, false); 19884008e97fSJason Wang } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) { 1989b8deabd3SJason Wang tun = rtnl_dereference(tfile->tun); 199040630b82SMichael S. Tsirkin if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached) 19914008e97fSJason Wang ret = -EINVAL; 1992cde8b15fSJason Wang else 19934008e97fSJason Wang __tun_detach(tfile, false); 19944008e97fSJason Wang } else 1995cde8b15fSJason Wang ret = -EINVAL; 1996cde8b15fSJason Wang 19975dbbaf2dSPaul Moore unlock: 1998cde8b15fSJason Wang rtnl_unlock(); 1999cde8b15fSJason Wang return ret; 2000cde8b15fSJason Wang } 2001cde8b15fSJason Wang 200250857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd, 200350857e2aSArnd Bergmann unsigned long arg, int ifreq_len) 20041da177e4SLinus Torvalds { 200536b50babSEric W. Biederman struct tun_file *tfile = file->private_data; 2006631ab46bSEric W. Biederman struct tun_struct *tun; 20071da177e4SLinus Torvalds void __user* argp = (void __user*)arg; 20081da177e4SLinus Torvalds struct ifreq ifr; 20090625c883SEric W. Biederman kuid_t owner; 20100625c883SEric W. Biederman kgid_t group; 201133dccbb0SHerbert Xu int sndbuf; 2012d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 2013fb7589a1SPavel Emelyanov unsigned int ifindex; 20141cf8e410SMichael S. Tsirkin int le; 2015f271b2ccSMax Krasnyansky int ret; 20161da177e4SLinus Torvalds 201720861f26SGao Feng if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == SOCK_IOC_TYPE) { 201850857e2aSArnd Bergmann if (copy_from_user(&ifr, argp, ifreq_len)) 20191da177e4SLinus Torvalds return -EFAULT; 20208bbb1813SDavid S. Miller } else { 2021a117dacdSMathias Krause memset(&ifr, 0, sizeof(ifr)); 20228bbb1813SDavid S. Miller } 2023631ab46bSEric W. Biederman if (cmd == TUNGETFEATURES) { 2024631ab46bSEric W. Biederman /* Currently this just means: "what IFF flags are valid?". 2025631ab46bSEric W. Biederman * This is needed because we never checked for invalid flags on 2026031f5e03SMichael S. Tsirkin * TUNSETIFF. 2027031f5e03SMichael S. Tsirkin */ 2028031f5e03SMichael S. Tsirkin return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES, 2029631ab46bSEric W. Biederman (unsigned int __user*)argp); 2030cde8b15fSJason Wang } else if (cmd == TUNSETQUEUE) 2031cde8b15fSJason Wang return tun_set_queue(file, &ifr); 2032631ab46bSEric W. Biederman 2033c8d68e6bSJason Wang ret = 0; 2034876bfd4dSHerbert Xu rtnl_lock(); 2035876bfd4dSHerbert Xu 203636b50babSEric W. Biederman tun = __tun_get(tfile); 20370f16bc13SGao Feng if (cmd == TUNSETIFF) { 20380f16bc13SGao Feng ret = -EEXIST; 20390f16bc13SGao Feng if (tun) 20400f16bc13SGao Feng goto unlock; 20410f16bc13SGao Feng 20421da177e4SLinus Torvalds ifr.ifr_name[IFNAMSIZ-1] = '\0'; 20431da177e4SLinus Torvalds 2044140e807dSEric W. Biederman ret = tun_set_iff(sock_net(&tfile->sk), file, &ifr); 20451da177e4SLinus Torvalds 2046876bfd4dSHerbert Xu if (ret) 2047876bfd4dSHerbert Xu goto unlock; 20481da177e4SLinus Torvalds 204950857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 2050876bfd4dSHerbert Xu ret = -EFAULT; 2051876bfd4dSHerbert Xu goto unlock; 20521da177e4SLinus Torvalds } 2053fb7589a1SPavel Emelyanov if (cmd == TUNSETIFINDEX) { 2054fb7589a1SPavel Emelyanov ret = -EPERM; 2055fb7589a1SPavel Emelyanov if (tun) 2056fb7589a1SPavel Emelyanov goto unlock; 2057fb7589a1SPavel Emelyanov 2058fb7589a1SPavel Emelyanov ret = -EFAULT; 2059fb7589a1SPavel Emelyanov if (copy_from_user(&ifindex, argp, sizeof(ifindex))) 2060fb7589a1SPavel Emelyanov goto unlock; 2061fb7589a1SPavel Emelyanov 2062fb7589a1SPavel Emelyanov ret = 0; 2063fb7589a1SPavel Emelyanov tfile->ifindex = ifindex; 2064fb7589a1SPavel Emelyanov goto unlock; 2065fb7589a1SPavel Emelyanov } 20661da177e4SLinus Torvalds 2067876bfd4dSHerbert Xu ret = -EBADFD; 20681da177e4SLinus Torvalds if (!tun) 2069876bfd4dSHerbert Xu goto unlock; 20701da177e4SLinus Torvalds 20711e588338SJason Wang tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd); 20721da177e4SLinus Torvalds 2073631ab46bSEric W. Biederman ret = 0; 20741da177e4SLinus Torvalds switch (cmd) { 2075e3b99556SMark McLoughlin case TUNGETIFF: 20769ce99cf6SRami Rosen tun_get_iff(current->nsproxy->net_ns, tun, &ifr); 2077e3b99556SMark McLoughlin 20783d407a80SPavel Emelyanov if (tfile->detached) 20793d407a80SPavel Emelyanov ifr.ifr_flags |= IFF_DETACH_QUEUE; 2080849c9b6fSPavel Emelyanov if (!tfile->socket.sk->sk_filter) 2081849c9b6fSPavel Emelyanov ifr.ifr_flags |= IFF_NOFILTER; 20823d407a80SPavel Emelyanov 208350857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 2084631ab46bSEric W. Biederman ret = -EFAULT; 2085e3b99556SMark McLoughlin break; 2086e3b99556SMark McLoughlin 20871da177e4SLinus Torvalds case TUNSETNOCSUM: 20881da177e4SLinus Torvalds /* Disable/Enable checksum */ 20891da177e4SLinus Torvalds 209088255375SMichał Mirosław /* [unimplemented] */ 209188255375SMichał Mirosław tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n", 20926b8a66eeSJoe Perches arg ? "disabled" : "enabled"); 20931da177e4SLinus Torvalds break; 20941da177e4SLinus Torvalds 20951da177e4SLinus Torvalds case TUNSETPERSIST: 209654f968d6SJason Wang /* Disable/Enable persist mode. Keep an extra reference to the 209754f968d6SJason Wang * module to prevent the module being unprobed. 209854f968d6SJason Wang */ 209940630b82SMichael S. Tsirkin if (arg && !(tun->flags & IFF_PERSIST)) { 210040630b82SMichael S. Tsirkin tun->flags |= IFF_PERSIST; 210154f968d6SJason Wang __module_get(THIS_MODULE); 2102dd38bd85SJason Wang } 210340630b82SMichael S. Tsirkin if (!arg && (tun->flags & IFF_PERSIST)) { 210440630b82SMichael S. Tsirkin tun->flags &= ~IFF_PERSIST; 210554f968d6SJason Wang module_put(THIS_MODULE); 210654f968d6SJason Wang } 21071da177e4SLinus Torvalds 21086b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "persist %s\n", 21096b8a66eeSJoe Perches arg ? "enabled" : "disabled"); 21101da177e4SLinus Torvalds break; 21111da177e4SLinus Torvalds 21121da177e4SLinus Torvalds case TUNSETOWNER: 21131da177e4SLinus Torvalds /* Set owner of the device */ 21140625c883SEric W. Biederman owner = make_kuid(current_user_ns(), arg); 21150625c883SEric W. Biederman if (!uid_valid(owner)) { 21160625c883SEric W. Biederman ret = -EINVAL; 21170625c883SEric W. Biederman break; 21180625c883SEric W. Biederman } 21190625c883SEric W. Biederman tun->owner = owner; 21201e588338SJason Wang tun_debug(KERN_INFO, tun, "owner set to %u\n", 21210625c883SEric W. Biederman from_kuid(&init_user_ns, tun->owner)); 21221da177e4SLinus Torvalds break; 21231da177e4SLinus Torvalds 21248c644623SGuido Guenther case TUNSETGROUP: 21258c644623SGuido Guenther /* Set group of the device */ 21260625c883SEric W. Biederman group = make_kgid(current_user_ns(), arg); 21270625c883SEric W. Biederman if (!gid_valid(group)) { 21280625c883SEric W. Biederman ret = -EINVAL; 21290625c883SEric W. Biederman break; 21300625c883SEric W. Biederman } 21310625c883SEric W. Biederman tun->group = group; 21321e588338SJason Wang tun_debug(KERN_INFO, tun, "group set to %u\n", 21330625c883SEric W. Biederman from_kgid(&init_user_ns, tun->group)); 21348c644623SGuido Guenther break; 21358c644623SGuido Guenther 2136ff4cc3acSMike Kershaw case TUNSETLINK: 2137ff4cc3acSMike Kershaw /* Only allow setting the type when the interface is down */ 2138ff4cc3acSMike Kershaw if (tun->dev->flags & IFF_UP) { 21396b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, 21406b8a66eeSJoe Perches "Linktype set failed because interface is up\n"); 214148abfe05SDavid S. Miller ret = -EBUSY; 2142ff4cc3acSMike Kershaw } else { 2143ff4cc3acSMike Kershaw tun->dev->type = (int) arg; 21446b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "linktype set to %d\n", 21456b8a66eeSJoe Perches tun->dev->type); 214648abfe05SDavid S. Miller ret = 0; 2147ff4cc3acSMike Kershaw } 2148631ab46bSEric W. Biederman break; 2149ff4cc3acSMike Kershaw 21501da177e4SLinus Torvalds #ifdef TUN_DEBUG 21511da177e4SLinus Torvalds case TUNSETDEBUG: 21521da177e4SLinus Torvalds tun->debug = arg; 21531da177e4SLinus Torvalds break; 21541da177e4SLinus Torvalds #endif 21555228ddc9SRusty Russell case TUNSETOFFLOAD: 215688255375SMichał Mirosław ret = set_offload(tun, arg); 2157631ab46bSEric W. Biederman break; 21585228ddc9SRusty Russell 2159f271b2ccSMax Krasnyansky case TUNSETTXFILTER: 2160f271b2ccSMax Krasnyansky /* Can be set only for TAPs */ 2161631ab46bSEric W. Biederman ret = -EINVAL; 216240630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 2163631ab46bSEric W. Biederman break; 2164c0e5a8c2SHarvey Harrison ret = update_filter(&tun->txflt, (void __user *)arg); 2165631ab46bSEric W. Biederman break; 21661da177e4SLinus Torvalds 21671da177e4SLinus Torvalds case SIOCGIFHWADDR: 2168b595076aSUwe Kleine-König /* Get hw address */ 2169f271b2ccSMax Krasnyansky memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); 2170f271b2ccSMax Krasnyansky ifr.ifr_hwaddr.sa_family = tun->dev->type; 217150857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 2172631ab46bSEric W. Biederman ret = -EFAULT; 2173631ab46bSEric W. Biederman break; 21741da177e4SLinus Torvalds 21751da177e4SLinus Torvalds case SIOCSIFHWADDR: 2176f271b2ccSMax Krasnyansky /* Set hw address */ 21776b8a66eeSJoe Perches tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n", 21786b8a66eeSJoe Perches ifr.ifr_hwaddr.sa_data); 217940102371SKim B. Heino 218040102371SKim B. Heino ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); 2181631ab46bSEric W. Biederman break; 218233dccbb0SHerbert Xu 218333dccbb0SHerbert Xu case TUNGETSNDBUF: 218454f968d6SJason Wang sndbuf = tfile->socket.sk->sk_sndbuf; 218533dccbb0SHerbert Xu if (copy_to_user(argp, &sndbuf, sizeof(sndbuf))) 218633dccbb0SHerbert Xu ret = -EFAULT; 218733dccbb0SHerbert Xu break; 218833dccbb0SHerbert Xu 218933dccbb0SHerbert Xu case TUNSETSNDBUF: 219033dccbb0SHerbert Xu if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) { 219133dccbb0SHerbert Xu ret = -EFAULT; 219233dccbb0SHerbert Xu break; 219333dccbb0SHerbert Xu } 219433dccbb0SHerbert Xu 2195c8d68e6bSJason Wang tun->sndbuf = sndbuf; 2196c8d68e6bSJason Wang tun_set_sndbuf(tun); 219733dccbb0SHerbert Xu break; 219833dccbb0SHerbert Xu 2199d9d52b51SMichael S. Tsirkin case TUNGETVNETHDRSZ: 2200d9d52b51SMichael S. Tsirkin vnet_hdr_sz = tun->vnet_hdr_sz; 2201d9d52b51SMichael S. Tsirkin if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz))) 2202d9d52b51SMichael S. Tsirkin ret = -EFAULT; 2203d9d52b51SMichael S. Tsirkin break; 2204d9d52b51SMichael S. Tsirkin 2205d9d52b51SMichael S. Tsirkin case TUNSETVNETHDRSZ: 2206d9d52b51SMichael S. Tsirkin if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) { 2207d9d52b51SMichael S. Tsirkin ret = -EFAULT; 2208d9d52b51SMichael S. Tsirkin break; 2209d9d52b51SMichael S. Tsirkin } 2210d9d52b51SMichael S. Tsirkin if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) { 2211d9d52b51SMichael S. Tsirkin ret = -EINVAL; 2212d9d52b51SMichael S. Tsirkin break; 2213d9d52b51SMichael S. Tsirkin } 2214d9d52b51SMichael S. Tsirkin 2215d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = vnet_hdr_sz; 2216d9d52b51SMichael S. Tsirkin break; 2217d9d52b51SMichael S. Tsirkin 22181cf8e410SMichael S. Tsirkin case TUNGETVNETLE: 22191cf8e410SMichael S. Tsirkin le = !!(tun->flags & TUN_VNET_LE); 22201cf8e410SMichael S. Tsirkin if (put_user(le, (int __user *)argp)) 22211cf8e410SMichael S. Tsirkin ret = -EFAULT; 22221cf8e410SMichael S. Tsirkin break; 22231cf8e410SMichael S. Tsirkin 22241cf8e410SMichael S. Tsirkin case TUNSETVNETLE: 22251cf8e410SMichael S. Tsirkin if (get_user(le, (int __user *)argp)) { 22261cf8e410SMichael S. Tsirkin ret = -EFAULT; 22271cf8e410SMichael S. Tsirkin break; 22281cf8e410SMichael S. Tsirkin } 22291cf8e410SMichael S. Tsirkin if (le) 22301cf8e410SMichael S. Tsirkin tun->flags |= TUN_VNET_LE; 22311cf8e410SMichael S. Tsirkin else 22321cf8e410SMichael S. Tsirkin tun->flags &= ~TUN_VNET_LE; 22331cf8e410SMichael S. Tsirkin break; 22341cf8e410SMichael S. Tsirkin 22358b8e658bSGreg Kurz case TUNGETVNETBE: 22368b8e658bSGreg Kurz ret = tun_get_vnet_be(tun, argp); 22378b8e658bSGreg Kurz break; 22388b8e658bSGreg Kurz 22398b8e658bSGreg Kurz case TUNSETVNETBE: 22408b8e658bSGreg Kurz ret = tun_set_vnet_be(tun, argp); 22418b8e658bSGreg Kurz break; 22428b8e658bSGreg Kurz 224399405162SMichael S. Tsirkin case TUNATTACHFILTER: 224499405162SMichael S. Tsirkin /* Can be set only for TAPs */ 224599405162SMichael S. Tsirkin ret = -EINVAL; 224640630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 224799405162SMichael S. Tsirkin break; 224899405162SMichael S. Tsirkin ret = -EFAULT; 224954f968d6SJason Wang if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog))) 225099405162SMichael S. Tsirkin break; 225199405162SMichael S. Tsirkin 2252c8d68e6bSJason Wang ret = tun_attach_filter(tun); 225399405162SMichael S. Tsirkin break; 225499405162SMichael S. Tsirkin 225599405162SMichael S. Tsirkin case TUNDETACHFILTER: 225699405162SMichael S. Tsirkin /* Can be set only for TAPs */ 225799405162SMichael S. Tsirkin ret = -EINVAL; 225840630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 225999405162SMichael S. Tsirkin break; 2260c8d68e6bSJason Wang ret = 0; 2261c8d68e6bSJason Wang tun_detach_filter(tun, tun->numqueues); 226299405162SMichael S. Tsirkin break; 226399405162SMichael S. Tsirkin 226476975e9cSPavel Emelyanov case TUNGETFILTER: 226576975e9cSPavel Emelyanov ret = -EINVAL; 226640630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 226776975e9cSPavel Emelyanov break; 226876975e9cSPavel Emelyanov ret = -EFAULT; 226976975e9cSPavel Emelyanov if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog))) 227076975e9cSPavel Emelyanov break; 227176975e9cSPavel Emelyanov ret = 0; 227276975e9cSPavel Emelyanov break; 227376975e9cSPavel Emelyanov 22741da177e4SLinus Torvalds default: 2275631ab46bSEric W. Biederman ret = -EINVAL; 2276631ab46bSEric W. Biederman break; 2277ee289b64SJoe Perches } 22781da177e4SLinus Torvalds 2279876bfd4dSHerbert Xu unlock: 2280876bfd4dSHerbert Xu rtnl_unlock(); 2281876bfd4dSHerbert Xu if (tun) 2282631ab46bSEric W. Biederman tun_put(tun); 2283631ab46bSEric W. Biederman return ret; 22841da177e4SLinus Torvalds } 22851da177e4SLinus Torvalds 228650857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file, 228750857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 228850857e2aSArnd Bergmann { 228950857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq)); 229050857e2aSArnd Bergmann } 229150857e2aSArnd Bergmann 229250857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 229350857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file, 229450857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 229550857e2aSArnd Bergmann { 229650857e2aSArnd Bergmann switch (cmd) { 229750857e2aSArnd Bergmann case TUNSETIFF: 229850857e2aSArnd Bergmann case TUNGETIFF: 229950857e2aSArnd Bergmann case TUNSETTXFILTER: 230050857e2aSArnd Bergmann case TUNGETSNDBUF: 230150857e2aSArnd Bergmann case TUNSETSNDBUF: 230250857e2aSArnd Bergmann case SIOCGIFHWADDR: 230350857e2aSArnd Bergmann case SIOCSIFHWADDR: 230450857e2aSArnd Bergmann arg = (unsigned long)compat_ptr(arg); 230550857e2aSArnd Bergmann break; 230650857e2aSArnd Bergmann default: 230750857e2aSArnd Bergmann arg = (compat_ulong_t)arg; 230850857e2aSArnd Bergmann break; 230950857e2aSArnd Bergmann } 231050857e2aSArnd Bergmann 231150857e2aSArnd Bergmann /* 231250857e2aSArnd Bergmann * compat_ifreq is shorter than ifreq, so we must not access beyond 231350857e2aSArnd Bergmann * the end of that structure. All fields that are used in this 231450857e2aSArnd Bergmann * driver are compatible though, we don't need to convert the 231550857e2aSArnd Bergmann * contents. 231650857e2aSArnd Bergmann */ 231750857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq)); 231850857e2aSArnd Bergmann } 231950857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */ 232050857e2aSArnd Bergmann 23211da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on) 23221da177e4SLinus Torvalds { 232354f968d6SJason Wang struct tun_file *tfile = file->private_data; 23241da177e4SLinus Torvalds int ret; 23251da177e4SLinus Torvalds 232654f968d6SJason Wang if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) 23279d319522SJonathan Corbet goto out; 23281da177e4SLinus Torvalds 23291da177e4SLinus Torvalds if (on) { 2330e0b93eddSJeff Layton __f_setown(file, task_pid(current), PIDTYPE_PID, 0); 233154f968d6SJason Wang tfile->flags |= TUN_FASYNC; 23321da177e4SLinus Torvalds } else 233354f968d6SJason Wang tfile->flags &= ~TUN_FASYNC; 23349d319522SJonathan Corbet ret = 0; 23359d319522SJonathan Corbet out: 23369d319522SJonathan Corbet return ret; 23371da177e4SLinus Torvalds } 23381da177e4SLinus Torvalds 23391da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file) 23401da177e4SLinus Torvalds { 2341140e807dSEric W. Biederman struct net *net = current->nsproxy->net_ns; 2342631ab46bSEric W. Biederman struct tun_file *tfile; 2343deed49fbSThomas Gleixner 23446b8a66eeSJoe Perches DBG1(KERN_INFO, "tunX: tun_chr_open\n"); 2345631ab46bSEric W. Biederman 2346140e807dSEric W. Biederman tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, 234711aa9c28SEric W. Biederman &tun_proto, 0); 2348631ab46bSEric W. Biederman if (!tfile) 2349631ab46bSEric W. Biederman return -ENOMEM; 2350c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 235154f968d6SJason Wang tfile->flags = 0; 2352fb7589a1SPavel Emelyanov tfile->ifindex = 0; 235354f968d6SJason Wang 235454f968d6SJason Wang init_waitqueue_head(&tfile->wq.wait); 23559e641bdcSXi Wang RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq); 235654f968d6SJason Wang 235754f968d6SJason Wang tfile->socket.file = file; 235854f968d6SJason Wang tfile->socket.ops = &tun_socket_ops; 235954f968d6SJason Wang 236054f968d6SJason Wang sock_init_data(&tfile->socket, &tfile->sk); 236154f968d6SJason Wang 236254f968d6SJason Wang tfile->sk.sk_write_space = tun_sock_write_space; 236354f968d6SJason Wang tfile->sk.sk_sndbuf = INT_MAX; 236454f968d6SJason Wang 2365631ab46bSEric W. Biederman file->private_data = tfile; 23664008e97fSJason Wang INIT_LIST_HEAD(&tfile->next); 236754f968d6SJason Wang 236819a6afb2SJason Wang sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); 236919a6afb2SJason Wang 23701da177e4SLinus Torvalds return 0; 23711da177e4SLinus Torvalds } 23721da177e4SLinus Torvalds 23731da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file) 23741da177e4SLinus Torvalds { 2375631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 23761da177e4SLinus Torvalds 2377c8d68e6bSJason Wang tun_detach(tfile, true); 23781da177e4SLinus Torvalds 23791da177e4SLinus Torvalds return 0; 23801da177e4SLinus Torvalds } 23811da177e4SLinus Torvalds 238293e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS 2383a3816ab0SJoe Perches static void tun_chr_show_fdinfo(struct seq_file *m, struct file *f) 238493e14b6dSMasatake YAMATO { 238593e14b6dSMasatake YAMATO struct tun_struct *tun; 238693e14b6dSMasatake YAMATO struct ifreq ifr; 238793e14b6dSMasatake YAMATO 238893e14b6dSMasatake YAMATO memset(&ifr, 0, sizeof(ifr)); 238993e14b6dSMasatake YAMATO 239093e14b6dSMasatake YAMATO rtnl_lock(); 239193e14b6dSMasatake YAMATO tun = tun_get(f); 239293e14b6dSMasatake YAMATO if (tun) 239393e14b6dSMasatake YAMATO tun_get_iff(current->nsproxy->net_ns, tun, &ifr); 239493e14b6dSMasatake YAMATO rtnl_unlock(); 239593e14b6dSMasatake YAMATO 239693e14b6dSMasatake YAMATO if (tun) 239793e14b6dSMasatake YAMATO tun_put(tun); 239893e14b6dSMasatake YAMATO 2399a3816ab0SJoe Perches seq_printf(m, "iff:\t%s\n", ifr.ifr_name); 240093e14b6dSMasatake YAMATO } 240193e14b6dSMasatake YAMATO #endif 240293e14b6dSMasatake YAMATO 2403d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = { 24041da177e4SLinus Torvalds .owner = THIS_MODULE, 24051da177e4SLinus Torvalds .llseek = no_llseek, 24069b067034SAl Viro .read_iter = tun_chr_read_iter, 2407f5ff53b4SAl Viro .write_iter = tun_chr_write_iter, 24081da177e4SLinus Torvalds .poll = tun_chr_poll, 2409876bfd4dSHerbert Xu .unlocked_ioctl = tun_chr_ioctl, 241050857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 241150857e2aSArnd Bergmann .compat_ioctl = tun_chr_compat_ioctl, 241250857e2aSArnd Bergmann #endif 24131da177e4SLinus Torvalds .open = tun_chr_open, 24141da177e4SLinus Torvalds .release = tun_chr_close, 241593e14b6dSMasatake YAMATO .fasync = tun_chr_fasync, 241693e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS 241793e14b6dSMasatake YAMATO .show_fdinfo = tun_chr_show_fdinfo, 241893e14b6dSMasatake YAMATO #endif 24191da177e4SLinus Torvalds }; 24201da177e4SLinus Torvalds 24211da177e4SLinus Torvalds static struct miscdevice tun_miscdev = { 24221da177e4SLinus Torvalds .minor = TUN_MINOR, 24231da177e4SLinus Torvalds .name = "tun", 2424e454cea2SKay Sievers .nodename = "net/tun", 24251da177e4SLinus Torvalds .fops = &tun_fops, 24261da177e4SLinus Torvalds }; 24271da177e4SLinus Torvalds 24281da177e4SLinus Torvalds /* ethtool interface */ 24291da177e4SLinus Torvalds 24301da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 24311da177e4SLinus Torvalds { 24321da177e4SLinus Torvalds cmd->supported = 0; 24331da177e4SLinus Torvalds cmd->advertising = 0; 243470739497SDavid Decotigny ethtool_cmd_speed_set(cmd, SPEED_10); 24351da177e4SLinus Torvalds cmd->duplex = DUPLEX_FULL; 24361da177e4SLinus Torvalds cmd->port = PORT_TP; 24371da177e4SLinus Torvalds cmd->phy_address = 0; 24381da177e4SLinus Torvalds cmd->transceiver = XCVR_INTERNAL; 24391da177e4SLinus Torvalds cmd->autoneg = AUTONEG_DISABLE; 24401da177e4SLinus Torvalds cmd->maxtxpkt = 0; 24411da177e4SLinus Torvalds cmd->maxrxpkt = 0; 24421da177e4SLinus Torvalds return 0; 24431da177e4SLinus Torvalds } 24441da177e4SLinus Torvalds 24451da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) 24461da177e4SLinus Torvalds { 24471da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 24481da177e4SLinus Torvalds 244933a5ba14SRick Jones strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); 245033a5ba14SRick Jones strlcpy(info->version, DRV_VERSION, sizeof(info->version)); 24511da177e4SLinus Torvalds 24521da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 245340630b82SMichael S. Tsirkin case IFF_TUN: 245433a5ba14SRick Jones strlcpy(info->bus_info, "tun", sizeof(info->bus_info)); 24551da177e4SLinus Torvalds break; 245640630b82SMichael S. Tsirkin case IFF_TAP: 245733a5ba14SRick Jones strlcpy(info->bus_info, "tap", sizeof(info->bus_info)); 24581da177e4SLinus Torvalds break; 24591da177e4SLinus Torvalds } 24601da177e4SLinus Torvalds } 24611da177e4SLinus Torvalds 24621da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev) 24631da177e4SLinus Torvalds { 24641da177e4SLinus Torvalds #ifdef TUN_DEBUG 24651da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 24661da177e4SLinus Torvalds return tun->debug; 24671da177e4SLinus Torvalds #else 24681da177e4SLinus Torvalds return -EOPNOTSUPP; 24691da177e4SLinus Torvalds #endif 24701da177e4SLinus Torvalds } 24711da177e4SLinus Torvalds 24721da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value) 24731da177e4SLinus Torvalds { 24741da177e4SLinus Torvalds #ifdef TUN_DEBUG 24751da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 24761da177e4SLinus Torvalds tun->debug = value; 24771da177e4SLinus Torvalds #endif 24781da177e4SLinus Torvalds } 24791da177e4SLinus Torvalds 2480*5503fcecSJason Wang static int tun_get_coalesce(struct net_device *dev, 2481*5503fcecSJason Wang struct ethtool_coalesce *ec) 2482*5503fcecSJason Wang { 2483*5503fcecSJason Wang struct tun_struct *tun = netdev_priv(dev); 2484*5503fcecSJason Wang 2485*5503fcecSJason Wang ec->rx_max_coalesced_frames = tun->rx_batched; 2486*5503fcecSJason Wang 2487*5503fcecSJason Wang return 0; 2488*5503fcecSJason Wang } 2489*5503fcecSJason Wang 2490*5503fcecSJason Wang static int tun_set_coalesce(struct net_device *dev, 2491*5503fcecSJason Wang struct ethtool_coalesce *ec) 2492*5503fcecSJason Wang { 2493*5503fcecSJason Wang struct tun_struct *tun = netdev_priv(dev); 2494*5503fcecSJason Wang 2495*5503fcecSJason Wang if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT) 2496*5503fcecSJason Wang tun->rx_batched = NAPI_POLL_WEIGHT; 2497*5503fcecSJason Wang else 2498*5503fcecSJason Wang tun->rx_batched = ec->rx_max_coalesced_frames; 2499*5503fcecSJason Wang 2500*5503fcecSJason Wang return 0; 2501*5503fcecSJason Wang } 2502*5503fcecSJason Wang 25037282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = { 25041da177e4SLinus Torvalds .get_settings = tun_get_settings, 25051da177e4SLinus Torvalds .get_drvinfo = tun_get_drvinfo, 25061da177e4SLinus Torvalds .get_msglevel = tun_get_msglevel, 25071da177e4SLinus Torvalds .set_msglevel = tun_set_msglevel, 2508bee31369SNolan Leake .get_link = ethtool_op_get_link, 2509eda29772SRichard Cochran .get_ts_info = ethtool_op_get_ts_info, 2510*5503fcecSJason Wang .get_coalesce = tun_get_coalesce, 2511*5503fcecSJason Wang .set_coalesce = tun_set_coalesce, 25121da177e4SLinus Torvalds }; 25131da177e4SLinus Torvalds 25141576d986SJason Wang static int tun_queue_resize(struct tun_struct *tun) 25151576d986SJason Wang { 25161576d986SJason Wang struct net_device *dev = tun->dev; 25171576d986SJason Wang struct tun_file *tfile; 25181576d986SJason Wang struct skb_array **arrays; 25191576d986SJason Wang int n = tun->numqueues + tun->numdisabled; 25201576d986SJason Wang int ret, i; 25211576d986SJason Wang 25221576d986SJason Wang arrays = kmalloc(sizeof *arrays * n, GFP_KERNEL); 25231576d986SJason Wang if (!arrays) 25241576d986SJason Wang return -ENOMEM; 25251576d986SJason Wang 25261576d986SJason Wang for (i = 0; i < tun->numqueues; i++) { 25271576d986SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 25281576d986SJason Wang arrays[i] = &tfile->tx_array; 25291576d986SJason Wang } 25301576d986SJason Wang list_for_each_entry(tfile, &tun->disabled, next) 25311576d986SJason Wang arrays[i++] = &tfile->tx_array; 25321576d986SJason Wang 25331576d986SJason Wang ret = skb_array_resize_multiple(arrays, n, 25341576d986SJason Wang dev->tx_queue_len, GFP_KERNEL); 25351576d986SJason Wang 25361576d986SJason Wang kfree(arrays); 25371576d986SJason Wang return ret; 25381576d986SJason Wang } 25391576d986SJason Wang 25401576d986SJason Wang static int tun_device_event(struct notifier_block *unused, 25411576d986SJason Wang unsigned long event, void *ptr) 25421576d986SJason Wang { 25431576d986SJason Wang struct net_device *dev = netdev_notifier_info_to_dev(ptr); 25441576d986SJason Wang struct tun_struct *tun = netdev_priv(dev); 25451576d986SJason Wang 254686dfb4acSCraig Gallek if (dev->rtnl_link_ops != &tun_link_ops) 254786dfb4acSCraig Gallek return NOTIFY_DONE; 254886dfb4acSCraig Gallek 25491576d986SJason Wang switch (event) { 25501576d986SJason Wang case NETDEV_CHANGE_TX_QUEUE_LEN: 25511576d986SJason Wang if (tun_queue_resize(tun)) 25521576d986SJason Wang return NOTIFY_BAD; 25531576d986SJason Wang break; 25541576d986SJason Wang default: 25551576d986SJason Wang break; 25561576d986SJason Wang } 25571576d986SJason Wang 25581576d986SJason Wang return NOTIFY_DONE; 25591576d986SJason Wang } 25601576d986SJason Wang 25611576d986SJason Wang static struct notifier_block tun_notifier_block __read_mostly = { 25621576d986SJason Wang .notifier_call = tun_device_event, 25631576d986SJason Wang }; 256479d17604SPavel Emelyanov 25651da177e4SLinus Torvalds static int __init tun_init(void) 25661da177e4SLinus Torvalds { 25671da177e4SLinus Torvalds int ret = 0; 25681da177e4SLinus Torvalds 25696b8a66eeSJoe Perches pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION); 25706b8a66eeSJoe Perches pr_info("%s\n", DRV_COPYRIGHT); 25711da177e4SLinus Torvalds 2572f019a7a5SEric W. Biederman ret = rtnl_link_register(&tun_link_ops); 257379d17604SPavel Emelyanov if (ret) { 25746b8a66eeSJoe Perches pr_err("Can't register link_ops\n"); 2575f019a7a5SEric W. Biederman goto err_linkops; 257679d17604SPavel Emelyanov } 257779d17604SPavel Emelyanov 25781da177e4SLinus Torvalds ret = misc_register(&tun_miscdev); 257979d17604SPavel Emelyanov if (ret) { 25806b8a66eeSJoe Perches pr_err("Can't register misc device %d\n", TUN_MINOR); 258179d17604SPavel Emelyanov goto err_misc; 258279d17604SPavel Emelyanov } 25831576d986SJason Wang 25841576d986SJason Wang register_netdevice_notifier(&tun_notifier_block); 258579d17604SPavel Emelyanov return 0; 258679d17604SPavel Emelyanov err_misc: 2587f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 2588f019a7a5SEric W. Biederman err_linkops: 25891da177e4SLinus Torvalds return ret; 25901da177e4SLinus Torvalds } 25911da177e4SLinus Torvalds 25921da177e4SLinus Torvalds static void tun_cleanup(void) 25931da177e4SLinus Torvalds { 25941da177e4SLinus Torvalds misc_deregister(&tun_miscdev); 2595f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 25961576d986SJason Wang unregister_netdevice_notifier(&tun_notifier_block); 25971da177e4SLinus Torvalds } 25981da177e4SLinus Torvalds 259905c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file. Returns error unless file is 260005c2828cSMichael S. Tsirkin * attached to a device. The returned object works like a packet socket, it 260105c2828cSMichael S. Tsirkin * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for 260205c2828cSMichael S. Tsirkin * holding a reference to the file for as long as the socket is in use. */ 260305c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file) 260405c2828cSMichael S. Tsirkin { 26056e914fc7SJason Wang struct tun_file *tfile; 260605c2828cSMichael S. Tsirkin if (file->f_op != &tun_fops) 260705c2828cSMichael S. Tsirkin return ERR_PTR(-EINVAL); 26086e914fc7SJason Wang tfile = file->private_data; 26096e914fc7SJason Wang if (!tfile) 261005c2828cSMichael S. Tsirkin return ERR_PTR(-EBADFD); 261154f968d6SJason Wang return &tfile->socket; 261205c2828cSMichael S. Tsirkin } 261305c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket); 261405c2828cSMichael S. Tsirkin 26151da177e4SLinus Torvalds module_init(tun_init); 26161da177e4SLinus Torvalds module_exit(tun_cleanup); 26171da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION); 26181da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT); 26191da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 26201da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR); 2621578454ffSKay Sievers MODULE_ALIAS("devname:net/tun"); 2622