11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * TUN - Universal TUN/TAP device driver. 31da177e4SLinus Torvalds * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com> 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 61da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 71da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 81da177e4SLinus Torvalds * (at your option) any later version. 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 111da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 121da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 131da177e4SLinus Torvalds * GNU General Public License for more details. 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $ 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds /* 191da177e4SLinus Torvalds * Changes: 201da177e4SLinus Torvalds * 21ff4cc3acSMike Kershaw * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14 22ff4cc3acSMike Kershaw * Add TUNSETLINK ioctl to set the link encapsulation 23ff4cc3acSMike Kershaw * 241da177e4SLinus Torvalds * Mark Smith <markzzzsmith@yahoo.com.au> 25344dc8edSJoe Perches * Use eth_random_addr() for tap MAC address. 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20 281da177e4SLinus Torvalds * Fixes in packet dropping, queue length setting and queue wakeup. 291da177e4SLinus Torvalds * Increased default tx queue length. 301da177e4SLinus Torvalds * Added ethtool API. 311da177e4SLinus Torvalds * Minor cleanups 321da177e4SLinus Torvalds * 331da177e4SLinus Torvalds * Daniel Podlejski <underley@underley.eu.org> 341da177e4SLinus Torvalds * Modifications for 2.3.99-pre5 kernel. 351da177e4SLinus Torvalds */ 361da177e4SLinus Torvalds 376b8a66eeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 386b8a66eeSJoe Perches 391da177e4SLinus Torvalds #define DRV_NAME "tun" 401da177e4SLinus Torvalds #define DRV_VERSION "1.6" 411da177e4SLinus Torvalds #define DRV_DESCRIPTION "Universal TUN/TAP device driver" 421da177e4SLinus Torvalds #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>" 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds #include <linux/module.h> 451da177e4SLinus Torvalds #include <linux/errno.h> 461da177e4SLinus Torvalds #include <linux/kernel.h> 47174cd4b1SIngo Molnar #include <linux/sched/signal.h> 481da177e4SLinus Torvalds #include <linux/major.h> 491da177e4SLinus Torvalds #include <linux/slab.h> 501da177e4SLinus Torvalds #include <linux/poll.h> 511da177e4SLinus Torvalds #include <linux/fcntl.h> 521da177e4SLinus Torvalds #include <linux/init.h> 531da177e4SLinus Torvalds #include <linux/skbuff.h> 541da177e4SLinus Torvalds #include <linux/netdevice.h> 551da177e4SLinus Torvalds #include <linux/etherdevice.h> 561da177e4SLinus Torvalds #include <linux/miscdevice.h> 571da177e4SLinus Torvalds #include <linux/ethtool.h> 581da177e4SLinus Torvalds #include <linux/rtnetlink.h> 5950857e2aSArnd Bergmann #include <linux/compat.h> 601da177e4SLinus Torvalds #include <linux/if.h> 611da177e4SLinus Torvalds #include <linux/if_arp.h> 621da177e4SLinus Torvalds #include <linux/if_ether.h> 631da177e4SLinus Torvalds #include <linux/if_tun.h> 646680ec68SJason Wang #include <linux/if_vlan.h> 651da177e4SLinus Torvalds #include <linux/crc32.h> 66d647a591SPavel Emelyanov #include <linux/nsproxy.h> 67f43798c2SRusty Russell #include <linux/virtio_net.h> 6899405162SMichael S. Tsirkin #include <linux/rcupdate.h> 69881d966bSEric W. Biederman #include <net/net_namespace.h> 7079d17604SPavel Emelyanov #include <net/netns/generic.h> 71f019a7a5SEric W. Biederman #include <net/rtnetlink.h> 7233dccbb0SHerbert Xu #include <net/sock.h> 7393e14b6dSMasatake YAMATO #include <linux/seq_file.h> 74e0b46d0eSHerbert Xu #include <linux/uio.h> 751576d986SJason Wang #include <linux/skb_array.h> 761da177e4SLinus Torvalds 777c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 781da177e4SLinus Torvalds 7914daa021SRusty Russell /* Uncomment to enable debugging */ 8014daa021SRusty Russell /* #define TUN_DEBUG 1 */ 8114daa021SRusty Russell 821da177e4SLinus Torvalds #ifdef TUN_DEBUG 831da177e4SLinus Torvalds static int debug; 8414daa021SRusty Russell 856b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...) \ 866b8a66eeSJoe Perches do { \ 876b8a66eeSJoe Perches if (tun->debug) \ 886b8a66eeSJoe Perches netdev_printk(level, tun->dev, fmt, ##args); \ 896b8a66eeSJoe Perches } while (0) 906b8a66eeSJoe Perches #define DBG1(level, fmt, args...) \ 916b8a66eeSJoe Perches do { \ 926b8a66eeSJoe Perches if (debug == 2) \ 936b8a66eeSJoe Perches printk(level fmt, ##args); \ 946b8a66eeSJoe Perches } while (0) 9514daa021SRusty Russell #else 966b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...) \ 976b8a66eeSJoe Perches do { \ 986b8a66eeSJoe Perches if (0) \ 996b8a66eeSJoe Perches netdev_printk(level, tun->dev, fmt, ##args); \ 1006b8a66eeSJoe Perches } while (0) 1016b8a66eeSJoe Perches #define DBG1(level, fmt, args...) \ 1026b8a66eeSJoe Perches do { \ 1036b8a66eeSJoe Perches if (0) \ 1046b8a66eeSJoe Perches printk(level fmt, ##args); \ 1056b8a66eeSJoe Perches } while (0) 1061da177e4SLinus Torvalds #endif 1071da177e4SLinus Torvalds 108031f5e03SMichael S. Tsirkin /* TUN device flags */ 109031f5e03SMichael S. Tsirkin 110031f5e03SMichael S. Tsirkin /* IFF_ATTACH_QUEUE is never stored in device flags, 111031f5e03SMichael S. Tsirkin * overload it to mean fasync when stored there. 112031f5e03SMichael S. Tsirkin */ 113031f5e03SMichael S. Tsirkin #define TUN_FASYNC IFF_ATTACH_QUEUE 1141cf8e410SMichael S. Tsirkin /* High bits in flags field are unused. */ 1151cf8e410SMichael S. Tsirkin #define TUN_VNET_LE 0x80000000 1168b8e658bSGreg Kurz #define TUN_VNET_BE 0x40000000 117031f5e03SMichael S. Tsirkin 118031f5e03SMichael S. Tsirkin #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \ 1191cf8e410SMichael S. Tsirkin IFF_MULTI_QUEUE) 1200690899bSMichael S. Tsirkin #define GOODCOPY_LEN 128 1210690899bSMichael S. Tsirkin 122f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8 123f271b2ccSMax Krasnyansky struct tap_filter { 124f271b2ccSMax Krasnyansky unsigned int count; /* Number of addrs. Zero means disabled */ 125f271b2ccSMax Krasnyansky u32 mask[2]; /* Mask of the hashed addrs */ 126f271b2ccSMax Krasnyansky unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN]; 127f271b2ccSMax Krasnyansky }; 128f271b2ccSMax Krasnyansky 129baf71c5cSPankaj Gupta /* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal 130baf71c5cSPankaj Gupta * to max number of VCPUs in guest. */ 131baf71c5cSPankaj Gupta #define MAX_TAP_QUEUES 256 132b8732fb7SJason Wang #define MAX_TAP_FLOWS 4096 133c8d68e6bSJason Wang 13496442e42SJason Wang #define TUN_FLOW_EXPIRE (3 * HZ) 13596442e42SJason Wang 136608b9977SPaolo Abeni struct tun_pcpu_stats { 137608b9977SPaolo Abeni u64 rx_packets; 138608b9977SPaolo Abeni u64 rx_bytes; 139608b9977SPaolo Abeni u64 tx_packets; 140608b9977SPaolo Abeni u64 tx_bytes; 141608b9977SPaolo Abeni struct u64_stats_sync syncp; 142608b9977SPaolo Abeni u32 rx_dropped; 143608b9977SPaolo Abeni u32 tx_dropped; 144608b9977SPaolo Abeni u32 rx_frame_errors; 145608b9977SPaolo Abeni }; 146608b9977SPaolo Abeni 14754f968d6SJason Wang /* A tun_file connects an open character device to a tuntap netdevice. It 14892d4ea6eSstephen hemminger * also contains all socket related structures (except sock_fprog and tap_filter) 14954f968d6SJason Wang * to serve as one transmit queue for tuntap device. The sock_fprog and 15054f968d6SJason Wang * tap_filter were kept in tun_struct since they were used for filtering for the 15136fe8c09SRami Rosen * netdevice not for a specific queue (at least I didn't see the requirement for 15254f968d6SJason Wang * this). 1536e914fc7SJason Wang * 1546e914fc7SJason Wang * RCU usage: 15536fe8c09SRami Rosen * The tun_file and tun_struct are loosely coupled, the pointer from one to the 1566e914fc7SJason Wang * other can only be read while rcu_read_lock or rtnl_lock is held. 15754f968d6SJason Wang */ 158631ab46bSEric W. Biederman struct tun_file { 15954f968d6SJason Wang struct sock sk; 16054f968d6SJason Wang struct socket socket; 16154f968d6SJason Wang struct socket_wq wq; 1626e914fc7SJason Wang struct tun_struct __rcu *tun; 16354f968d6SJason Wang struct fasync_struct *fasync; 16454f968d6SJason Wang /* only used for fasnyc */ 16554f968d6SJason Wang unsigned int flags; 166fb7589a1SPavel Emelyanov union { 167c8d68e6bSJason Wang u16 queue_index; 168fb7589a1SPavel Emelyanov unsigned int ifindex; 169fb7589a1SPavel Emelyanov }; 1704008e97fSJason Wang struct list_head next; 1714008e97fSJason Wang struct tun_struct *detached; 1721576d986SJason Wang struct skb_array tx_array; 173631ab46bSEric W. Biederman }; 174631ab46bSEric W. Biederman 17596442e42SJason Wang struct tun_flow_entry { 17696442e42SJason Wang struct hlist_node hash_link; 17796442e42SJason Wang struct rcu_head rcu; 17896442e42SJason Wang struct tun_struct *tun; 17996442e42SJason Wang 18096442e42SJason Wang u32 rxhash; 1819bc88939STom Herbert u32 rps_rxhash; 18296442e42SJason Wang int queue_index; 18396442e42SJason Wang unsigned long updated; 18496442e42SJason Wang }; 18596442e42SJason Wang 18696442e42SJason Wang #define TUN_NUM_FLOW_ENTRIES 1024 18796442e42SJason Wang 18854f968d6SJason Wang /* Since the socket were moved to tun_file, to preserve the behavior of persist 18936fe8c09SRami Rosen * device, socket filter, sndbuf and vnet header size were restore when the 19054f968d6SJason Wang * file were attached to a persist device. 19154f968d6SJason Wang */ 19214daa021SRusty Russell struct tun_struct { 193c8d68e6bSJason Wang struct tun_file __rcu *tfiles[MAX_TAP_QUEUES]; 194c8d68e6bSJason Wang unsigned int numqueues; 195f271b2ccSMax Krasnyansky unsigned int flags; 1960625c883SEric W. Biederman kuid_t owner; 1970625c883SEric W. Biederman kgid_t group; 19814daa021SRusty Russell 19914daa021SRusty Russell struct net_device *dev; 200c8f44affSMichał Mirosław netdev_features_t set_features; 20188255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \ 202e3e3c423SVlad Yasevich NETIF_F_TSO6|NETIF_F_UFO) 203d9d52b51SMichael S. Tsirkin 204eaea34b2SPaolo Abeni int align; 205d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 20654f968d6SJason Wang int sndbuf; 20754f968d6SJason Wang struct tap_filter txflt; 20854f968d6SJason Wang struct sock_fprog fprog; 20954f968d6SJason Wang /* protected by rtnl lock */ 21054f968d6SJason Wang bool filter_attached; 21114daa021SRusty Russell #ifdef TUN_DEBUG 21214daa021SRusty Russell int debug; 21314daa021SRusty Russell #endif 21496442e42SJason Wang spinlock_t lock; 21596442e42SJason Wang struct hlist_head flows[TUN_NUM_FLOW_ENTRIES]; 21696442e42SJason Wang struct timer_list flow_gc_timer; 21796442e42SJason Wang unsigned long ageing_time; 2184008e97fSJason Wang unsigned int numdisabled; 2194008e97fSJason Wang struct list_head disabled; 2205dbbaf2dSPaul Moore void *security; 221b8732fb7SJason Wang u32 flow_count; 2225503fcecSJason Wang u32 rx_batched; 223608b9977SPaolo Abeni struct tun_pcpu_stats __percpu *pcpu_stats; 22414daa021SRusty Russell }; 22514daa021SRusty Russell 2268b8e658bSGreg Kurz #ifdef CONFIG_TUN_VNET_CROSS_LE 2278b8e658bSGreg Kurz static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) 2288b8e658bSGreg Kurz { 2298b8e658bSGreg Kurz return tun->flags & TUN_VNET_BE ? false : 2308b8e658bSGreg Kurz virtio_legacy_is_little_endian(); 2318b8e658bSGreg Kurz } 2328b8e658bSGreg Kurz 2338b8e658bSGreg Kurz static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) 2348b8e658bSGreg Kurz { 2358b8e658bSGreg Kurz int be = !!(tun->flags & TUN_VNET_BE); 2368b8e658bSGreg Kurz 2378b8e658bSGreg Kurz if (put_user(be, argp)) 2388b8e658bSGreg Kurz return -EFAULT; 2398b8e658bSGreg Kurz 2408b8e658bSGreg Kurz return 0; 2418b8e658bSGreg Kurz } 2428b8e658bSGreg Kurz 2438b8e658bSGreg Kurz static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) 2448b8e658bSGreg Kurz { 2458b8e658bSGreg Kurz int be; 2468b8e658bSGreg Kurz 2478b8e658bSGreg Kurz if (get_user(be, argp)) 2488b8e658bSGreg Kurz return -EFAULT; 2498b8e658bSGreg Kurz 2508b8e658bSGreg Kurz if (be) 2518b8e658bSGreg Kurz tun->flags |= TUN_VNET_BE; 2528b8e658bSGreg Kurz else 2538b8e658bSGreg Kurz tun->flags &= ~TUN_VNET_BE; 2548b8e658bSGreg Kurz 2558b8e658bSGreg Kurz return 0; 2568b8e658bSGreg Kurz } 2578b8e658bSGreg Kurz #else 2588b8e658bSGreg Kurz static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) 2598b8e658bSGreg Kurz { 2608b8e658bSGreg Kurz return virtio_legacy_is_little_endian(); 2618b8e658bSGreg Kurz } 2628b8e658bSGreg Kurz 2638b8e658bSGreg Kurz static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) 2648b8e658bSGreg Kurz { 2658b8e658bSGreg Kurz return -EINVAL; 2668b8e658bSGreg Kurz } 2678b8e658bSGreg Kurz 2688b8e658bSGreg Kurz static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) 2698b8e658bSGreg Kurz { 2708b8e658bSGreg Kurz return -EINVAL; 2718b8e658bSGreg Kurz } 2728b8e658bSGreg Kurz #endif /* CONFIG_TUN_VNET_CROSS_LE */ 2738b8e658bSGreg Kurz 27425bd55bbSGreg Kurz static inline bool tun_is_little_endian(struct tun_struct *tun) 27525bd55bbSGreg Kurz { 2767d824109SGreg Kurz return tun->flags & TUN_VNET_LE || 2778b8e658bSGreg Kurz tun_legacy_is_little_endian(tun); 27825bd55bbSGreg Kurz } 27925bd55bbSGreg Kurz 28056f0dcc5SMichael S. Tsirkin static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val) 28156f0dcc5SMichael S. Tsirkin { 28225bd55bbSGreg Kurz return __virtio16_to_cpu(tun_is_little_endian(tun), val); 28356f0dcc5SMichael S. Tsirkin } 28456f0dcc5SMichael S. Tsirkin 28556f0dcc5SMichael S. Tsirkin static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val) 28656f0dcc5SMichael S. Tsirkin { 28725bd55bbSGreg Kurz return __cpu_to_virtio16(tun_is_little_endian(tun), val); 28856f0dcc5SMichael S. Tsirkin } 28956f0dcc5SMichael S. Tsirkin 29096442e42SJason Wang static inline u32 tun_hashfn(u32 rxhash) 29196442e42SJason Wang { 29296442e42SJason Wang return rxhash & 0x3ff; 29396442e42SJason Wang } 29496442e42SJason Wang 29596442e42SJason Wang static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash) 29696442e42SJason Wang { 29796442e42SJason Wang struct tun_flow_entry *e; 29896442e42SJason Wang 299b67bfe0dSSasha Levin hlist_for_each_entry_rcu(e, head, hash_link) { 30096442e42SJason Wang if (e->rxhash == rxhash) 30196442e42SJason Wang return e; 30296442e42SJason Wang } 30396442e42SJason Wang return NULL; 30496442e42SJason Wang } 30596442e42SJason Wang 30696442e42SJason Wang static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun, 30796442e42SJason Wang struct hlist_head *head, 30896442e42SJason Wang u32 rxhash, u16 queue_index) 30996442e42SJason Wang { 3109fdc6befSEric Dumazet struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC); 3119fdc6befSEric Dumazet 31296442e42SJason Wang if (e) { 31396442e42SJason Wang tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n", 31496442e42SJason Wang rxhash, queue_index); 31596442e42SJason Wang e->updated = jiffies; 31696442e42SJason Wang e->rxhash = rxhash; 3179bc88939STom Herbert e->rps_rxhash = 0; 31896442e42SJason Wang e->queue_index = queue_index; 31996442e42SJason Wang e->tun = tun; 32096442e42SJason Wang hlist_add_head_rcu(&e->hash_link, head); 321b8732fb7SJason Wang ++tun->flow_count; 32296442e42SJason Wang } 32396442e42SJason Wang return e; 32496442e42SJason Wang } 32596442e42SJason Wang 32696442e42SJason Wang static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e) 32796442e42SJason Wang { 32896442e42SJason Wang tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n", 32996442e42SJason Wang e->rxhash, e->queue_index); 33096442e42SJason Wang hlist_del_rcu(&e->hash_link); 3319fdc6befSEric Dumazet kfree_rcu(e, rcu); 332b8732fb7SJason Wang --tun->flow_count; 33396442e42SJason Wang } 33496442e42SJason Wang 33596442e42SJason Wang static void tun_flow_flush(struct tun_struct *tun) 33696442e42SJason Wang { 33796442e42SJason Wang int i; 33896442e42SJason Wang 33996442e42SJason Wang spin_lock_bh(&tun->lock); 34096442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 34196442e42SJason Wang struct tun_flow_entry *e; 342b67bfe0dSSasha Levin struct hlist_node *n; 34396442e42SJason Wang 344b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) 34596442e42SJason Wang tun_flow_delete(tun, e); 34696442e42SJason Wang } 34796442e42SJason Wang spin_unlock_bh(&tun->lock); 34896442e42SJason Wang } 34996442e42SJason Wang 35096442e42SJason Wang static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index) 35196442e42SJason Wang { 35296442e42SJason Wang int i; 35396442e42SJason Wang 35496442e42SJason Wang spin_lock_bh(&tun->lock); 35596442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 35696442e42SJason Wang struct tun_flow_entry *e; 357b67bfe0dSSasha Levin struct hlist_node *n; 35896442e42SJason Wang 359b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { 36096442e42SJason Wang if (e->queue_index == queue_index) 36196442e42SJason Wang tun_flow_delete(tun, e); 36296442e42SJason Wang } 36396442e42SJason Wang } 36496442e42SJason Wang spin_unlock_bh(&tun->lock); 36596442e42SJason Wang } 36696442e42SJason Wang 36796442e42SJason Wang static void tun_flow_cleanup(unsigned long data) 36896442e42SJason Wang { 36996442e42SJason Wang struct tun_struct *tun = (struct tun_struct *)data; 37096442e42SJason Wang unsigned long delay = tun->ageing_time; 37196442e42SJason Wang unsigned long next_timer = jiffies + delay; 37296442e42SJason Wang unsigned long count = 0; 37396442e42SJason Wang int i; 37496442e42SJason Wang 37596442e42SJason Wang tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n"); 37696442e42SJason Wang 37796442e42SJason Wang spin_lock_bh(&tun->lock); 37896442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 37996442e42SJason Wang struct tun_flow_entry *e; 380b67bfe0dSSasha Levin struct hlist_node *n; 38196442e42SJason Wang 382b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { 38396442e42SJason Wang unsigned long this_timer; 38496442e42SJason Wang count++; 38596442e42SJason Wang this_timer = e->updated + delay; 38696442e42SJason Wang if (time_before_eq(this_timer, jiffies)) 38796442e42SJason Wang tun_flow_delete(tun, e); 38896442e42SJason Wang else if (time_before(this_timer, next_timer)) 38996442e42SJason Wang next_timer = this_timer; 39096442e42SJason Wang } 39196442e42SJason Wang } 39296442e42SJason Wang 39396442e42SJason Wang if (count) 39496442e42SJason Wang mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer)); 39596442e42SJason Wang spin_unlock_bh(&tun->lock); 39696442e42SJason Wang } 39796442e42SJason Wang 39849974420SEric Dumazet static void tun_flow_update(struct tun_struct *tun, u32 rxhash, 3999e85722dSJason Wang struct tun_file *tfile) 40096442e42SJason Wang { 40196442e42SJason Wang struct hlist_head *head; 40296442e42SJason Wang struct tun_flow_entry *e; 40396442e42SJason Wang unsigned long delay = tun->ageing_time; 4049e85722dSJason Wang u16 queue_index = tfile->queue_index; 40596442e42SJason Wang 40696442e42SJason Wang if (!rxhash) 40796442e42SJason Wang return; 40896442e42SJason Wang else 40996442e42SJason Wang head = &tun->flows[tun_hashfn(rxhash)]; 41096442e42SJason Wang 41196442e42SJason Wang rcu_read_lock(); 41296442e42SJason Wang 4139e85722dSJason Wang /* We may get a very small possibility of OOO during switching, not 4149e85722dSJason Wang * worth to optimize.*/ 4159e85722dSJason Wang if (tun->numqueues == 1 || tfile->detached) 41696442e42SJason Wang goto unlock; 41796442e42SJason Wang 41896442e42SJason Wang e = tun_flow_find(head, rxhash); 41996442e42SJason Wang if (likely(e)) { 42096442e42SJason Wang /* TODO: keep queueing to old queue until it's empty? */ 42196442e42SJason Wang e->queue_index = queue_index; 42296442e42SJason Wang e->updated = jiffies; 4239bc88939STom Herbert sock_rps_record_flow_hash(e->rps_rxhash); 42496442e42SJason Wang } else { 42596442e42SJason Wang spin_lock_bh(&tun->lock); 426b8732fb7SJason Wang if (!tun_flow_find(head, rxhash) && 427b8732fb7SJason Wang tun->flow_count < MAX_TAP_FLOWS) 42896442e42SJason Wang tun_flow_create(tun, head, rxhash, queue_index); 42996442e42SJason Wang 43096442e42SJason Wang if (!timer_pending(&tun->flow_gc_timer)) 43196442e42SJason Wang mod_timer(&tun->flow_gc_timer, 43296442e42SJason Wang round_jiffies_up(jiffies + delay)); 43396442e42SJason Wang spin_unlock_bh(&tun->lock); 43496442e42SJason Wang } 43596442e42SJason Wang 43696442e42SJason Wang unlock: 43796442e42SJason Wang rcu_read_unlock(); 43896442e42SJason Wang } 43996442e42SJason Wang 4409bc88939STom Herbert /** 4419bc88939STom Herbert * Save the hash received in the stack receive path and update the 4429bc88939STom Herbert * flow_hash table accordingly. 4439bc88939STom Herbert */ 4449bc88939STom Herbert static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash) 4459bc88939STom Herbert { 446567e4b79SEric Dumazet if (unlikely(e->rps_rxhash != hash)) 4479bc88939STom Herbert e->rps_rxhash = hash; 4489bc88939STom Herbert } 4499bc88939STom Herbert 450c8d68e6bSJason Wang /* We try to identify a flow through its rxhash first. The reason that 45192d4ea6eSstephen hemminger * we do not check rxq no. is because some cards(e.g 82599), chooses 452c8d68e6bSJason Wang * the rxq based on the txq where the last packet of the flow comes. As 453c8d68e6bSJason Wang * the userspace application move between processors, we may get a 454c8d68e6bSJason Wang * different rxq no. here. If we could not get rxhash, then we would 455c8d68e6bSJason Wang * hope the rxq no. may help here. 456c8d68e6bSJason Wang */ 457f663dd9aSJason Wang static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb, 45899932d4fSDaniel Borkmann void *accel_priv, select_queue_fallback_t fallback) 459c8d68e6bSJason Wang { 460c8d68e6bSJason Wang struct tun_struct *tun = netdev_priv(dev); 46196442e42SJason Wang struct tun_flow_entry *e; 462c8d68e6bSJason Wang u32 txq = 0; 463c8d68e6bSJason Wang u32 numqueues = 0; 464c8d68e6bSJason Wang 465c8d68e6bSJason Wang rcu_read_lock(); 46692bb73eaSJason Wang numqueues = ACCESS_ONCE(tun->numqueues); 467c8d68e6bSJason Wang 4683958afa1STom Herbert txq = skb_get_hash(skb); 469c8d68e6bSJason Wang if (txq) { 47096442e42SJason Wang e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq); 4719bc88939STom Herbert if (e) { 4729bc88939STom Herbert tun_flow_save_rps_rxhash(e, txq); 473fbe4d456SZhi Yong Wu txq = e->queue_index; 4749bc88939STom Herbert } else 475c8d68e6bSJason Wang /* use multiply and shift instead of expensive divide */ 476c8d68e6bSJason Wang txq = ((u64)txq * numqueues) >> 32; 477c8d68e6bSJason Wang } else if (likely(skb_rx_queue_recorded(skb))) { 478c8d68e6bSJason Wang txq = skb_get_rx_queue(skb); 479c8d68e6bSJason Wang while (unlikely(txq >= numqueues)) 480c8d68e6bSJason Wang txq -= numqueues; 481c8d68e6bSJason Wang } 482c8d68e6bSJason Wang 483c8d68e6bSJason Wang rcu_read_unlock(); 484c8d68e6bSJason Wang return txq; 485c8d68e6bSJason Wang } 486c8d68e6bSJason Wang 487cde8b15fSJason Wang static inline bool tun_not_capable(struct tun_struct *tun) 488cde8b15fSJason Wang { 489cde8b15fSJason Wang const struct cred *cred = current_cred(); 490c260b772SEric W. Biederman struct net *net = dev_net(tun->dev); 491cde8b15fSJason Wang 492cde8b15fSJason Wang return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) || 493cde8b15fSJason Wang (gid_valid(tun->group) && !in_egroup_p(tun->group))) && 494c260b772SEric W. Biederman !ns_capable(net->user_ns, CAP_NET_ADMIN); 495cde8b15fSJason Wang } 496cde8b15fSJason Wang 497c8d68e6bSJason Wang static void tun_set_real_num_queues(struct tun_struct *tun) 498c8d68e6bSJason Wang { 499c8d68e6bSJason Wang netif_set_real_num_tx_queues(tun->dev, tun->numqueues); 500c8d68e6bSJason Wang netif_set_real_num_rx_queues(tun->dev, tun->numqueues); 501c8d68e6bSJason Wang } 502c8d68e6bSJason Wang 5034008e97fSJason Wang static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) 5044008e97fSJason Wang { 5054008e97fSJason Wang tfile->detached = tun; 5064008e97fSJason Wang list_add_tail(&tfile->next, &tun->disabled); 5074008e97fSJason Wang ++tun->numdisabled; 5084008e97fSJason Wang } 5094008e97fSJason Wang 510d32649d1SJason Wang static struct tun_struct *tun_enable_queue(struct tun_file *tfile) 5114008e97fSJason Wang { 5124008e97fSJason Wang struct tun_struct *tun = tfile->detached; 5134008e97fSJason Wang 5144008e97fSJason Wang tfile->detached = NULL; 5154008e97fSJason Wang list_del_init(&tfile->next); 5164008e97fSJason Wang --tun->numdisabled; 5174008e97fSJason Wang return tun; 5184008e97fSJason Wang } 5194008e97fSJason Wang 5204bfb0513SJason Wang static void tun_queue_purge(struct tun_file *tfile) 5214bfb0513SJason Wang { 5221576d986SJason Wang struct sk_buff *skb; 5231576d986SJason Wang 5241576d986SJason Wang while ((skb = skb_array_consume(&tfile->tx_array)) != NULL) 5251576d986SJason Wang kfree_skb(skb); 5261576d986SJason Wang 5275503fcecSJason Wang skb_queue_purge(&tfile->sk.sk_write_queue); 5284bfb0513SJason Wang skb_queue_purge(&tfile->sk.sk_error_queue); 5294bfb0513SJason Wang } 5304bfb0513SJason Wang 531c8d68e6bSJason Wang static void __tun_detach(struct tun_file *tfile, bool clean) 532c8d68e6bSJason Wang { 533c8d68e6bSJason Wang struct tun_file *ntfile; 534c8d68e6bSJason Wang struct tun_struct *tun; 535c8d68e6bSJason Wang 536b8deabd3SJason Wang tun = rtnl_dereference(tfile->tun); 537b8deabd3SJason Wang 5389e85722dSJason Wang if (tun && !tfile->detached) { 539c8d68e6bSJason Wang u16 index = tfile->queue_index; 540c8d68e6bSJason Wang BUG_ON(index >= tun->numqueues); 541c8d68e6bSJason Wang 542c8d68e6bSJason Wang rcu_assign_pointer(tun->tfiles[index], 543c8d68e6bSJason Wang tun->tfiles[tun->numqueues - 1]); 544b8deabd3SJason Wang ntfile = rtnl_dereference(tun->tfiles[index]); 545c8d68e6bSJason Wang ntfile->queue_index = index; 546c8d68e6bSJason Wang 547c8d68e6bSJason Wang --tun->numqueues; 5489e85722dSJason Wang if (clean) { 549c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 550c8d68e6bSJason Wang sock_put(&tfile->sk); 5519e85722dSJason Wang } else 5524008e97fSJason Wang tun_disable_queue(tun, tfile); 553c8d68e6bSJason Wang 554c8d68e6bSJason Wang synchronize_net(); 55596442e42SJason Wang tun_flow_delete_by_queue(tun, tun->numqueues + 1); 556c8d68e6bSJason Wang /* Drop read queue */ 5574bfb0513SJason Wang tun_queue_purge(tfile); 558c8d68e6bSJason Wang tun_set_real_num_queues(tun); 559dd38bd85SJason Wang } else if (tfile->detached && clean) { 5604008e97fSJason Wang tun = tun_enable_queue(tfile); 561dd38bd85SJason Wang sock_put(&tfile->sk); 562dd38bd85SJason Wang } 563c8d68e6bSJason Wang 564c8d68e6bSJason Wang if (clean) { 565af668b3cSMichael S. Tsirkin if (tun && tun->numqueues == 0 && tun->numdisabled == 0) { 566af668b3cSMichael S. Tsirkin netif_carrier_off(tun->dev); 567af668b3cSMichael S. Tsirkin 56840630b82SMichael S. Tsirkin if (!(tun->flags & IFF_PERSIST) && 569af668b3cSMichael S. Tsirkin tun->dev->reg_state == NETREG_REGISTERED) 5704008e97fSJason Wang unregister_netdevice(tun->dev); 571af668b3cSMichael S. Tsirkin } 5721576d986SJason Wang if (tun) 5731576d986SJason Wang skb_array_cleanup(&tfile->tx_array); 574140e807dSEric W. Biederman sock_put(&tfile->sk); 575c8d68e6bSJason Wang } 576c8d68e6bSJason Wang } 577c8d68e6bSJason Wang 578c8d68e6bSJason Wang static void tun_detach(struct tun_file *tfile, bool clean) 579c8d68e6bSJason Wang { 580c8d68e6bSJason Wang rtnl_lock(); 581c8d68e6bSJason Wang __tun_detach(tfile, clean); 582c8d68e6bSJason Wang rtnl_unlock(); 583c8d68e6bSJason Wang } 584c8d68e6bSJason Wang 585c8d68e6bSJason Wang static void tun_detach_all(struct net_device *dev) 586c8d68e6bSJason Wang { 587c8d68e6bSJason Wang struct tun_struct *tun = netdev_priv(dev); 5884008e97fSJason Wang struct tun_file *tfile, *tmp; 589c8d68e6bSJason Wang int i, n = tun->numqueues; 590c8d68e6bSJason Wang 591c8d68e6bSJason Wang for (i = 0; i < n; i++) { 592b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 593c8d68e6bSJason Wang BUG_ON(!tfile); 594addf8fc4SJason Wang tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; 5959e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 596c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 597c8d68e6bSJason Wang --tun->numqueues; 598c8d68e6bSJason Wang } 5999e85722dSJason Wang list_for_each_entry(tfile, &tun->disabled, next) { 600addf8fc4SJason Wang tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; 6019e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 602c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 6039e85722dSJason Wang } 604c8d68e6bSJason Wang BUG_ON(tun->numqueues != 0); 605c8d68e6bSJason Wang 606c8d68e6bSJason Wang synchronize_net(); 607c8d68e6bSJason Wang for (i = 0; i < n; i++) { 608b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 609c8d68e6bSJason Wang /* Drop read queue */ 6104bfb0513SJason Wang tun_queue_purge(tfile); 611c8d68e6bSJason Wang sock_put(&tfile->sk); 612c8d68e6bSJason Wang } 6134008e97fSJason Wang list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { 6144008e97fSJason Wang tun_enable_queue(tfile); 6154bfb0513SJason Wang tun_queue_purge(tfile); 6164008e97fSJason Wang sock_put(&tfile->sk); 6174008e97fSJason Wang } 6184008e97fSJason Wang BUG_ON(tun->numdisabled != 0); 619dd38bd85SJason Wang 62040630b82SMichael S. Tsirkin if (tun->flags & IFF_PERSIST) 621dd38bd85SJason Wang module_put(THIS_MODULE); 622c8d68e6bSJason Wang } 623c8d68e6bSJason Wang 624849c9b6fSPavel Emelyanov static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter) 625a7385ba2SEric W. Biederman { 626631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 6271576d986SJason Wang struct net_device *dev = tun->dev; 62838231b7aSEric W. Biederman int err; 629a7385ba2SEric W. Biederman 6305dbbaf2dSPaul Moore err = security_tun_dev_attach(tfile->socket.sk, tun->security); 6315dbbaf2dSPaul Moore if (err < 0) 6325dbbaf2dSPaul Moore goto out; 6335dbbaf2dSPaul Moore 63438231b7aSEric W. Biederman err = -EINVAL; 6359e85722dSJason Wang if (rtnl_dereference(tfile->tun) && !tfile->detached) 63638231b7aSEric W. Biederman goto out; 63738231b7aSEric W. Biederman 63838231b7aSEric W. Biederman err = -EBUSY; 63940630b82SMichael S. Tsirkin if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1) 640c8d68e6bSJason Wang goto out; 641c8d68e6bSJason Wang 642c8d68e6bSJason Wang err = -E2BIG; 6434008e97fSJason Wang if (!tfile->detached && 6444008e97fSJason Wang tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES) 64538231b7aSEric W. Biederman goto out; 64638231b7aSEric W. Biederman 64738231b7aSEric W. Biederman err = 0; 64854f968d6SJason Wang 64992d4ea6eSstephen hemminger /* Re-attach the filter to persist device */ 650849c9b6fSPavel Emelyanov if (!skip_filter && (tun->filter_attached == true)) { 6518ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 6528ced425eSHannes Frederic Sowa err = sk_attach_filter(&tun->fprog, tfile->socket.sk); 6538ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 65454f968d6SJason Wang if (!err) 65554f968d6SJason Wang goto out; 65654f968d6SJason Wang } 6571576d986SJason Wang 6581576d986SJason Wang if (!tfile->detached && 6591576d986SJason Wang skb_array_init(&tfile->tx_array, dev->tx_queue_len, GFP_KERNEL)) { 6601576d986SJason Wang err = -ENOMEM; 6611576d986SJason Wang goto out; 6621576d986SJason Wang } 6631576d986SJason Wang 664c8d68e6bSJason Wang tfile->queue_index = tun->numqueues; 665addf8fc4SJason Wang tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN; 6666e914fc7SJason Wang rcu_assign_pointer(tfile->tun, tun); 667c8d68e6bSJason Wang rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); 668c8d68e6bSJason Wang tun->numqueues++; 669c8d68e6bSJason Wang 6704008e97fSJason Wang if (tfile->detached) 6714008e97fSJason Wang tun_enable_queue(tfile); 6724008e97fSJason Wang else 6734008e97fSJason Wang sock_hold(&tfile->sk); 6744008e97fSJason Wang 675c8d68e6bSJason Wang tun_set_real_num_queues(tun); 676c8d68e6bSJason Wang 677c8d68e6bSJason Wang /* device is allowed to go away first, so no need to hold extra 678c8d68e6bSJason Wang * refcnt. 679c8d68e6bSJason Wang */ 680a7385ba2SEric W. Biederman 68138231b7aSEric W. Biederman out: 68238231b7aSEric W. Biederman return err; 683a7385ba2SEric W. Biederman } 684a7385ba2SEric W. Biederman 685631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile) 686631ab46bSEric W. Biederman { 6876e914fc7SJason Wang struct tun_struct *tun; 688c70f1829SEric W. Biederman 6896e914fc7SJason Wang rcu_read_lock(); 6906e914fc7SJason Wang tun = rcu_dereference(tfile->tun); 6916e914fc7SJason Wang if (tun) 6926e914fc7SJason Wang dev_hold(tun->dev); 6936e914fc7SJason Wang rcu_read_unlock(); 694c70f1829SEric W. Biederman 695c70f1829SEric W. Biederman return tun; 696631ab46bSEric W. Biederman } 697631ab46bSEric W. Biederman 698631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file) 699631ab46bSEric W. Biederman { 700631ab46bSEric W. Biederman return __tun_get(file->private_data); 701631ab46bSEric W. Biederman } 702631ab46bSEric W. Biederman 703631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun) 704631ab46bSEric W. Biederman { 7056e914fc7SJason Wang dev_put(tun->dev); 706631ab46bSEric W. Biederman } 707631ab46bSEric W. Biederman 7086b8a66eeSJoe Perches /* TAP filtering */ 709f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr) 710f271b2ccSMax Krasnyansky { 711f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 712f271b2ccSMax Krasnyansky mask[n >> 5] |= (1 << (n & 31)); 713f271b2ccSMax Krasnyansky } 714f271b2ccSMax Krasnyansky 715f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) 716f271b2ccSMax Krasnyansky { 717f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 718f271b2ccSMax Krasnyansky return mask[n >> 5] & (1 << (n & 31)); 719f271b2ccSMax Krasnyansky } 720f271b2ccSMax Krasnyansky 721f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg) 722f271b2ccSMax Krasnyansky { 723f271b2ccSMax Krasnyansky struct { u8 u[ETH_ALEN]; } *addr; 724f271b2ccSMax Krasnyansky struct tun_filter uf; 725f271b2ccSMax Krasnyansky int err, alen, n, nexact; 726f271b2ccSMax Krasnyansky 727f271b2ccSMax Krasnyansky if (copy_from_user(&uf, arg, sizeof(uf))) 728f271b2ccSMax Krasnyansky return -EFAULT; 729f271b2ccSMax Krasnyansky 730f271b2ccSMax Krasnyansky if (!uf.count) { 731f271b2ccSMax Krasnyansky /* Disabled */ 732f271b2ccSMax Krasnyansky filter->count = 0; 733f271b2ccSMax Krasnyansky return 0; 734f271b2ccSMax Krasnyansky } 735f271b2ccSMax Krasnyansky 736f271b2ccSMax Krasnyansky alen = ETH_ALEN * uf.count; 73728e8190dSMarkus Elfring addr = memdup_user(arg + sizeof(uf), alen); 73828e8190dSMarkus Elfring if (IS_ERR(addr)) 73928e8190dSMarkus Elfring return PTR_ERR(addr); 740f271b2ccSMax Krasnyansky 741f271b2ccSMax Krasnyansky /* The filter is updated without holding any locks. Which is 742f271b2ccSMax Krasnyansky * perfectly safe. We disable it first and in the worst 743f271b2ccSMax Krasnyansky * case we'll accept a few undesired packets. */ 744f271b2ccSMax Krasnyansky filter->count = 0; 745f271b2ccSMax Krasnyansky wmb(); 746f271b2ccSMax Krasnyansky 747f271b2ccSMax Krasnyansky /* Use first set of addresses as an exact filter */ 748f271b2ccSMax Krasnyansky for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++) 749f271b2ccSMax Krasnyansky memcpy(filter->addr[n], addr[n].u, ETH_ALEN); 750f271b2ccSMax Krasnyansky 751f271b2ccSMax Krasnyansky nexact = n; 752f271b2ccSMax Krasnyansky 753cfbf84fcSAlex Williamson /* Remaining multicast addresses are hashed, 754cfbf84fcSAlex Williamson * unicast will leave the filter disabled. */ 755f271b2ccSMax Krasnyansky memset(filter->mask, 0, sizeof(filter->mask)); 756cfbf84fcSAlex Williamson for (; n < uf.count; n++) { 757cfbf84fcSAlex Williamson if (!is_multicast_ether_addr(addr[n].u)) { 758cfbf84fcSAlex Williamson err = 0; /* no filter */ 7593b8d2a69SMarkus Elfring goto free_addr; 760cfbf84fcSAlex Williamson } 761f271b2ccSMax Krasnyansky addr_hash_set(filter->mask, addr[n].u); 762cfbf84fcSAlex Williamson } 763f271b2ccSMax Krasnyansky 764f271b2ccSMax Krasnyansky /* For ALLMULTI just set the mask to all ones. 765f271b2ccSMax Krasnyansky * This overrides the mask populated above. */ 766f271b2ccSMax Krasnyansky if ((uf.flags & TUN_FLT_ALLMULTI)) 767f271b2ccSMax Krasnyansky memset(filter->mask, ~0, sizeof(filter->mask)); 768f271b2ccSMax Krasnyansky 769f271b2ccSMax Krasnyansky /* Now enable the filter */ 770f271b2ccSMax Krasnyansky wmb(); 771f271b2ccSMax Krasnyansky filter->count = nexact; 772f271b2ccSMax Krasnyansky 773f271b2ccSMax Krasnyansky /* Return the number of exact filters */ 774f271b2ccSMax Krasnyansky err = nexact; 7753b8d2a69SMarkus Elfring free_addr: 776f271b2ccSMax Krasnyansky kfree(addr); 777f271b2ccSMax Krasnyansky return err; 778f271b2ccSMax Krasnyansky } 779f271b2ccSMax Krasnyansky 780f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */ 781f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb) 782f271b2ccSMax Krasnyansky { 783f271b2ccSMax Krasnyansky /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect 784f271b2ccSMax Krasnyansky * at this point. */ 785f271b2ccSMax Krasnyansky struct ethhdr *eh = (struct ethhdr *) skb->data; 786f271b2ccSMax Krasnyansky int i; 787f271b2ccSMax Krasnyansky 788f271b2ccSMax Krasnyansky /* Exact match */ 789f271b2ccSMax Krasnyansky for (i = 0; i < filter->count; i++) 7902e42e474SJoe Perches if (ether_addr_equal(eh->h_dest, filter->addr[i])) 791f271b2ccSMax Krasnyansky return 1; 792f271b2ccSMax Krasnyansky 793f271b2ccSMax Krasnyansky /* Inexact match (multicast only) */ 794f271b2ccSMax Krasnyansky if (is_multicast_ether_addr(eh->h_dest)) 795f271b2ccSMax Krasnyansky return addr_hash_test(filter->mask, eh->h_dest); 796f271b2ccSMax Krasnyansky 797f271b2ccSMax Krasnyansky return 0; 798f271b2ccSMax Krasnyansky } 799f271b2ccSMax Krasnyansky 800f271b2ccSMax Krasnyansky /* 801f271b2ccSMax Krasnyansky * Checks whether the packet is accepted or not. 802f271b2ccSMax Krasnyansky * Returns: 0 - drop, !=0 - accept 803f271b2ccSMax Krasnyansky */ 804f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb) 805f271b2ccSMax Krasnyansky { 806f271b2ccSMax Krasnyansky if (!filter->count) 807f271b2ccSMax Krasnyansky return 1; 808f271b2ccSMax Krasnyansky 809f271b2ccSMax Krasnyansky return run_filter(filter, skb); 810f271b2ccSMax Krasnyansky } 811f271b2ccSMax Krasnyansky 8121da177e4SLinus Torvalds /* Network device part of the driver */ 8131da177e4SLinus Torvalds 8147282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops; 8151da177e4SLinus Torvalds 816c70f1829SEric W. Biederman /* Net device detach from fd. */ 817c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev) 818c70f1829SEric W. Biederman { 819c8d68e6bSJason Wang tun_detach_all(dev); 820c70f1829SEric W. Biederman } 821c70f1829SEric W. Biederman 8221da177e4SLinus Torvalds /* Net device open. */ 8231da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev) 8241da177e4SLinus Torvalds { 825b20e2d54SHannes Frederic Sowa struct tun_struct *tun = netdev_priv(dev); 826b20e2d54SHannes Frederic Sowa int i; 827b20e2d54SHannes Frederic Sowa 828c8d68e6bSJason Wang netif_tx_start_all_queues(dev); 829b20e2d54SHannes Frederic Sowa 830b20e2d54SHannes Frederic Sowa for (i = 0; i < tun->numqueues; i++) { 831b20e2d54SHannes Frederic Sowa struct tun_file *tfile; 832b20e2d54SHannes Frederic Sowa 833b20e2d54SHannes Frederic Sowa tfile = rtnl_dereference(tun->tfiles[i]); 834b20e2d54SHannes Frederic Sowa tfile->socket.sk->sk_write_space(tfile->socket.sk); 835b20e2d54SHannes Frederic Sowa } 836b20e2d54SHannes Frederic Sowa 8371da177e4SLinus Torvalds return 0; 8381da177e4SLinus Torvalds } 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds /* Net device close. */ 8411da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev) 8421da177e4SLinus Torvalds { 843c8d68e6bSJason Wang netif_tx_stop_all_queues(dev); 8441da177e4SLinus Torvalds return 0; 8451da177e4SLinus Torvalds } 8461da177e4SLinus Torvalds 8471da177e4SLinus Torvalds /* Net device start xmit */ 848424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) 8491da177e4SLinus Torvalds { 8501da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 851c8d68e6bSJason Wang int txq = skb->queue_mapping; 8526e914fc7SJason Wang struct tun_file *tfile; 853fa35864eSDominic Curran u32 numqueues = 0; 8541da177e4SLinus Torvalds 8556e914fc7SJason Wang rcu_read_lock(); 856c8d68e6bSJason Wang tfile = rcu_dereference(tun->tfiles[txq]); 857fa35864eSDominic Curran numqueues = ACCESS_ONCE(tun->numqueues); 858c8d68e6bSJason Wang 8591da177e4SLinus Torvalds /* Drop packet if interface is not attached */ 860fa35864eSDominic Curran if (txq >= numqueues) 8611da177e4SLinus Torvalds goto drop; 8621da177e4SLinus Torvalds 8633df97ba8SJason Wang #ifdef CONFIG_RPS 8643df97ba8SJason Wang if (numqueues == 1 && static_key_false(&rps_needed)) { 8659bc88939STom Herbert /* Select queue was not called for the skbuff, so we extract the 8669bc88939STom Herbert * RPS hash and save it into the flow_table here. 8679bc88939STom Herbert */ 8689bc88939STom Herbert __u32 rxhash; 8699bc88939STom Herbert 8709bc88939STom Herbert rxhash = skb_get_hash(skb); 8719bc88939STom Herbert if (rxhash) { 8729bc88939STom Herbert struct tun_flow_entry *e; 8739bc88939STom Herbert e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)], 8749bc88939STom Herbert rxhash); 8759bc88939STom Herbert if (e) 8769bc88939STom Herbert tun_flow_save_rps_rxhash(e, rxhash); 8779bc88939STom Herbert } 8789bc88939STom Herbert } 8793df97ba8SJason Wang #endif 8809bc88939STom Herbert 8816e914fc7SJason Wang tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len); 8826e914fc7SJason Wang 883c8d68e6bSJason Wang BUG_ON(!tfile); 884c8d68e6bSJason Wang 885f271b2ccSMax Krasnyansky /* Drop if the filter does not like it. 886f271b2ccSMax Krasnyansky * This is a noop if the filter is disabled. 887f271b2ccSMax Krasnyansky * Filter can be enabled only for the TAP devices. */ 888f271b2ccSMax Krasnyansky if (!check_filter(&tun->txflt, skb)) 889f271b2ccSMax Krasnyansky goto drop; 890f271b2ccSMax Krasnyansky 89154f968d6SJason Wang if (tfile->socket.sk->sk_filter && 89254f968d6SJason Wang sk_filter(tfile->socket.sk, skb)) 89399405162SMichael S. Tsirkin goto drop; 89499405162SMichael S. Tsirkin 8957bf66305SJason Wang if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC))) 8967bf66305SJason Wang goto drop; 8977bf66305SJason Wang 8987b996243SSoheil Hassas Yeganeh skb_tx_timestamp(skb); 899eda29772SRichard Cochran 9000110d6f2SMichael S. Tsirkin /* Orphan the skb - required as we might hang on to it 9017bf66305SJason Wang * for indefinite time. 9027bf66305SJason Wang */ 9030110d6f2SMichael S. Tsirkin skb_orphan(skb); 9040110d6f2SMichael S. Tsirkin 905f8af75f3SEric Dumazet nf_reset(skb); 906f8af75f3SEric Dumazet 9071576d986SJason Wang if (skb_array_produce(&tfile->tx_array, skb)) 9081576d986SJason Wang goto drop; 9091da177e4SLinus Torvalds 9101da177e4SLinus Torvalds /* Notify and wake up reader process */ 91154f968d6SJason Wang if (tfile->flags & TUN_FASYNC) 91254f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_IN); 9139e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 9146e914fc7SJason Wang 9156e914fc7SJason Wang rcu_read_unlock(); 9166ed10654SPatrick McHardy return NETDEV_TX_OK; 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds drop: 919608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->tx_dropped); 920149d36f7SMichael S. Tsirkin skb_tx_error(skb); 9211da177e4SLinus Torvalds kfree_skb(skb); 9226e914fc7SJason Wang rcu_read_unlock(); 923baeababbSJason Wang return NET_XMIT_DROP; 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds 926f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev) 9271da177e4SLinus Torvalds { 928f271b2ccSMax Krasnyansky /* 929f271b2ccSMax Krasnyansky * This callback is supposed to deal with mc filter in 930f271b2ccSMax Krasnyansky * _rx_ path and has nothing to do with the _tx_ path. 931f271b2ccSMax Krasnyansky * In rx path we always accept everything userspace gives us. 932f271b2ccSMax Krasnyansky */ 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds 935c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev, 936c8f44affSMichał Mirosław netdev_features_t features) 93788255375SMichał Mirosław { 93888255375SMichał Mirosław struct tun_struct *tun = netdev_priv(dev); 93988255375SMichał Mirosław 94088255375SMichał Mirosław return (features & tun->set_features) | (features & ~TUN_USER_FEATURES); 94188255375SMichał Mirosław } 942bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 943bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev) 944bebd097aSNeil Horman { 945bebd097aSNeil Horman /* 946bebd097aSNeil Horman * Tun only receives frames when: 947bebd097aSNeil Horman * 1) the char device endpoint gets data from user space 948bebd097aSNeil Horman * 2) the tun socket gets a sendmsg call from user space 94992d4ea6eSstephen hemminger * Since both of those are synchronous operations, we are guaranteed 950bebd097aSNeil Horman * never to have pending data when we poll for it 95192d4ea6eSstephen hemminger * so there is nothing to do here but return. 952bebd097aSNeil Horman * We need this though so netpoll recognizes us as an interface that 953bebd097aSNeil Horman * supports polling, which enables bridge devices in virt setups to 954bebd097aSNeil Horman * still use netconsole 955bebd097aSNeil Horman */ 956bebd097aSNeil Horman return; 957bebd097aSNeil Horman } 958bebd097aSNeil Horman #endif 959eaea34b2SPaolo Abeni 960eaea34b2SPaolo Abeni static void tun_set_headroom(struct net_device *dev, int new_hr) 961eaea34b2SPaolo Abeni { 962eaea34b2SPaolo Abeni struct tun_struct *tun = netdev_priv(dev); 963eaea34b2SPaolo Abeni 964eaea34b2SPaolo Abeni if (new_hr < NET_SKB_PAD) 965eaea34b2SPaolo Abeni new_hr = NET_SKB_PAD; 966eaea34b2SPaolo Abeni 967eaea34b2SPaolo Abeni tun->align = new_hr; 968eaea34b2SPaolo Abeni } 969eaea34b2SPaolo Abeni 970bc1f4470Sstephen hemminger static void 971608b9977SPaolo Abeni tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 972608b9977SPaolo Abeni { 973608b9977SPaolo Abeni u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0; 974608b9977SPaolo Abeni struct tun_struct *tun = netdev_priv(dev); 975608b9977SPaolo Abeni struct tun_pcpu_stats *p; 976608b9977SPaolo Abeni int i; 977608b9977SPaolo Abeni 978608b9977SPaolo Abeni for_each_possible_cpu(i) { 979608b9977SPaolo Abeni u64 rxpackets, rxbytes, txpackets, txbytes; 980608b9977SPaolo Abeni unsigned int start; 981608b9977SPaolo Abeni 982608b9977SPaolo Abeni p = per_cpu_ptr(tun->pcpu_stats, i); 983608b9977SPaolo Abeni do { 984608b9977SPaolo Abeni start = u64_stats_fetch_begin(&p->syncp); 985608b9977SPaolo Abeni rxpackets = p->rx_packets; 986608b9977SPaolo Abeni rxbytes = p->rx_bytes; 987608b9977SPaolo Abeni txpackets = p->tx_packets; 988608b9977SPaolo Abeni txbytes = p->tx_bytes; 989608b9977SPaolo Abeni } while (u64_stats_fetch_retry(&p->syncp, start)); 990608b9977SPaolo Abeni 991608b9977SPaolo Abeni stats->rx_packets += rxpackets; 992608b9977SPaolo Abeni stats->rx_bytes += rxbytes; 993608b9977SPaolo Abeni stats->tx_packets += txpackets; 994608b9977SPaolo Abeni stats->tx_bytes += txbytes; 995608b9977SPaolo Abeni 996608b9977SPaolo Abeni /* u32 counters */ 997608b9977SPaolo Abeni rx_dropped += p->rx_dropped; 998608b9977SPaolo Abeni rx_frame_errors += p->rx_frame_errors; 999608b9977SPaolo Abeni tx_dropped += p->tx_dropped; 1000608b9977SPaolo Abeni } 1001608b9977SPaolo Abeni stats->rx_dropped = rx_dropped; 1002608b9977SPaolo Abeni stats->rx_frame_errors = rx_frame_errors; 1003608b9977SPaolo Abeni stats->tx_dropped = tx_dropped; 1004608b9977SPaolo Abeni } 1005608b9977SPaolo Abeni 1006758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = { 1007c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 1008758e43b7SStephen Hemminger .ndo_open = tun_net_open, 1009758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 101000829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 101188255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 1012c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 1013bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 1014bebd097aSNeil Horman .ndo_poll_controller = tun_poll_controller, 1015bebd097aSNeil Horman #endif 1016eaea34b2SPaolo Abeni .ndo_set_rx_headroom = tun_set_headroom, 1017608b9977SPaolo Abeni .ndo_get_stats64 = tun_net_get_stats64, 1018758e43b7SStephen Hemminger }; 1019758e43b7SStephen Hemminger 1020758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = { 1021c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 1022758e43b7SStephen Hemminger .ndo_open = tun_net_open, 1023758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 102400829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 102588255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 1026afc4b13dSJiri Pirko .ndo_set_rx_mode = tun_net_mclist, 1027758e43b7SStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 1028758e43b7SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 1029c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 1030bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 1031bebd097aSNeil Horman .ndo_poll_controller = tun_poll_controller, 1032bebd097aSNeil Horman #endif 10335e52796aSToshiaki Makita .ndo_features_check = passthru_features_check, 1034eaea34b2SPaolo Abeni .ndo_set_rx_headroom = tun_set_headroom, 1035608b9977SPaolo Abeni .ndo_get_stats64 = tun_net_get_stats64, 1036758e43b7SStephen Hemminger }; 1037758e43b7SStephen Hemminger 1038944a1376SPavel Emelyanov static void tun_flow_init(struct tun_struct *tun) 103996442e42SJason Wang { 104096442e42SJason Wang int i; 104196442e42SJason Wang 104296442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) 104396442e42SJason Wang INIT_HLIST_HEAD(&tun->flows[i]); 104496442e42SJason Wang 104596442e42SJason Wang tun->ageing_time = TUN_FLOW_EXPIRE; 104696442e42SJason Wang setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun); 104796442e42SJason Wang mod_timer(&tun->flow_gc_timer, 104896442e42SJason Wang round_jiffies_up(jiffies + tun->ageing_time)); 104996442e42SJason Wang } 105096442e42SJason Wang 105196442e42SJason Wang static void tun_flow_uninit(struct tun_struct *tun) 105296442e42SJason Wang { 105396442e42SJason Wang del_timer_sync(&tun->flow_gc_timer); 105496442e42SJason Wang tun_flow_flush(tun); 105596442e42SJason Wang } 105696442e42SJason Wang 105791572088SJarod Wilson #define MIN_MTU 68 105891572088SJarod Wilson #define MAX_MTU 65535 105991572088SJarod Wilson 10601da177e4SLinus Torvalds /* Initialize net device. */ 10611da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev) 10621da177e4SLinus Torvalds { 10631da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 106640630b82SMichael S. Tsirkin case IFF_TUN: 1067758e43b7SStephen Hemminger dev->netdev_ops = &tun_netdev_ops; 1068758e43b7SStephen Hemminger 10691da177e4SLinus Torvalds /* Point-to-Point TUN Device */ 10701da177e4SLinus Torvalds dev->hard_header_len = 0; 10711da177e4SLinus Torvalds dev->addr_len = 0; 10721da177e4SLinus Torvalds dev->mtu = 1500; 10731da177e4SLinus Torvalds 10741da177e4SLinus Torvalds /* Zero header length */ 10751da177e4SLinus Torvalds dev->type = ARPHRD_NONE; 10761da177e4SLinus Torvalds dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 10771da177e4SLinus Torvalds break; 10781da177e4SLinus Torvalds 107940630b82SMichael S. Tsirkin case IFF_TAP: 10807a0a9608SKusanagi Kouichi dev->netdev_ops = &tap_netdev_ops; 10811da177e4SLinus Torvalds /* Ethernet TAP Device */ 10821da177e4SLinus Torvalds ether_setup(dev); 1083550fd08cSNeil Horman dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1084a676847bSstephen hemminger dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 108536226a8dSBrian Braunstein 1086f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 108736226a8dSBrian Braunstein 10881da177e4SLinus Torvalds break; 10891da177e4SLinus Torvalds } 109091572088SJarod Wilson 109191572088SJarod Wilson dev->min_mtu = MIN_MTU; 109291572088SJarod Wilson dev->max_mtu = MAX_MTU - dev->hard_header_len; 10931da177e4SLinus Torvalds } 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds /* Character device part */ 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds /* Poll */ 10981da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table *wait) 10991da177e4SLinus Torvalds { 1100b2430de3SEric W. Biederman struct tun_file *tfile = file->private_data; 1101b2430de3SEric W. Biederman struct tun_struct *tun = __tun_get(tfile); 11023c8a9c63SMariusz Kozlowski struct sock *sk; 110333dccbb0SHerbert Xu unsigned int mask = 0; 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds if (!tun) 1106eac9e902SEric W. Biederman return POLLERR; 11071da177e4SLinus Torvalds 110854f968d6SJason Wang sk = tfile->socket.sk; 11093c8a9c63SMariusz Kozlowski 11106b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_chr_poll\n"); 11111da177e4SLinus Torvalds 11129e641bdcSXi Wang poll_wait(file, sk_sleep(sk), wait); 11131da177e4SLinus Torvalds 11141576d986SJason Wang if (!skb_array_empty(&tfile->tx_array)) 11151da177e4SLinus Torvalds mask |= POLLIN | POLLRDNORM; 11161da177e4SLinus Torvalds 1117b20e2d54SHannes Frederic Sowa if (tun->dev->flags & IFF_UP && 1118b20e2d54SHannes Frederic Sowa (sock_writeable(sk) || 11199cd3e072SEric Dumazet (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) && 1120b20e2d54SHannes Frederic Sowa sock_writeable(sk)))) 112133dccbb0SHerbert Xu mask |= POLLOUT | POLLWRNORM; 112233dccbb0SHerbert Xu 1123c70f1829SEric W. Biederman if (tun->dev->reg_state != NETREG_REGISTERED) 1124c70f1829SEric W. Biederman mask = POLLERR; 1125c70f1829SEric W. Biederman 1126631ab46bSEric W. Biederman tun_put(tun); 11271da177e4SLinus Torvalds return mask; 11281da177e4SLinus Torvalds } 11291da177e4SLinus Torvalds 1130f42157cbSRusty Russell /* prepad is the amount to reserve at front. len is length after that. 1131f42157cbSRusty Russell * linear is a hint as to how much to copy (usually headers). */ 113254f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, 113333dccbb0SHerbert Xu size_t prepad, size_t len, 113433dccbb0SHerbert Xu size_t linear, int noblock) 1135f42157cbSRusty Russell { 113654f968d6SJason Wang struct sock *sk = tfile->socket.sk; 1137f42157cbSRusty Russell struct sk_buff *skb; 113833dccbb0SHerbert Xu int err; 1139f42157cbSRusty Russell 1140f42157cbSRusty Russell /* Under a page? Don't bother with paged skb. */ 11410eca93bcSHerbert Xu if (prepad + len < PAGE_SIZE || !linear) 114233dccbb0SHerbert Xu linear = len; 1143f42157cbSRusty Russell 114433dccbb0SHerbert Xu skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, 114528d64271SEric Dumazet &err, 0); 1146f42157cbSRusty Russell if (!skb) 114733dccbb0SHerbert Xu return ERR_PTR(err); 1148f42157cbSRusty Russell 1149f42157cbSRusty Russell skb_reserve(skb, prepad); 1150f42157cbSRusty Russell skb_put(skb, linear); 115133dccbb0SHerbert Xu skb->data_len = len - linear; 115233dccbb0SHerbert Xu skb->len += len - linear; 1153f42157cbSRusty Russell 1154f42157cbSRusty Russell return skb; 1155f42157cbSRusty Russell } 1156f42157cbSRusty Russell 11575503fcecSJason Wang static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile, 11585503fcecSJason Wang struct sk_buff *skb, int more) 11595503fcecSJason Wang { 11605503fcecSJason Wang struct sk_buff_head *queue = &tfile->sk.sk_write_queue; 11615503fcecSJason Wang struct sk_buff_head process_queue; 11625503fcecSJason Wang u32 rx_batched = tun->rx_batched; 11635503fcecSJason Wang bool rcv = false; 11645503fcecSJason Wang 11655503fcecSJason Wang if (!rx_batched || (!more && skb_queue_empty(queue))) { 11665503fcecSJason Wang local_bh_disable(); 11675503fcecSJason Wang netif_receive_skb(skb); 11685503fcecSJason Wang local_bh_enable(); 11695503fcecSJason Wang return; 11705503fcecSJason Wang } 11715503fcecSJason Wang 11725503fcecSJason Wang spin_lock(&queue->lock); 11735503fcecSJason Wang if (!more || skb_queue_len(queue) == rx_batched) { 11745503fcecSJason Wang __skb_queue_head_init(&process_queue); 11755503fcecSJason Wang skb_queue_splice_tail_init(queue, &process_queue); 11765503fcecSJason Wang rcv = true; 11775503fcecSJason Wang } else { 11785503fcecSJason Wang __skb_queue_tail(queue, skb); 11795503fcecSJason Wang } 11805503fcecSJason Wang spin_unlock(&queue->lock); 11815503fcecSJason Wang 11825503fcecSJason Wang if (rcv) { 11835503fcecSJason Wang struct sk_buff *nskb; 11845503fcecSJason Wang 11855503fcecSJason Wang local_bh_disable(); 11865503fcecSJason Wang while ((nskb = __skb_dequeue(&process_queue))) 11875503fcecSJason Wang netif_receive_skb(nskb); 11885503fcecSJason Wang netif_receive_skb(skb); 11895503fcecSJason Wang local_bh_enable(); 11905503fcecSJason Wang } 11915503fcecSJason Wang } 11925503fcecSJason Wang 11931da177e4SLinus Torvalds /* Get packet from user space buffer */ 119454f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, 1195f5ff53b4SAl Viro void *msg_control, struct iov_iter *from, 11965503fcecSJason Wang int noblock, bool more) 11971da177e4SLinus Torvalds { 119809640e63SHarvey Harrison struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; 11991da177e4SLinus Torvalds struct sk_buff *skb; 1200f5ff53b4SAl Viro size_t total_len = iov_iter_count(from); 1201eaea34b2SPaolo Abeni size_t len = total_len, align = tun->align, linear; 1202f43798c2SRusty Russell struct virtio_net_hdr gso = { 0 }; 1203608b9977SPaolo Abeni struct tun_pcpu_stats *stats; 120496f8d9ecSJason Wang int good_linear; 12050690899bSMichael S. Tsirkin int copylen; 12060690899bSMichael S. Tsirkin bool zerocopy = false; 12070690899bSMichael S. Tsirkin int err; 120849974420SEric Dumazet u32 rxhash; 12091da177e4SLinus Torvalds 12101bd4978aSEric Dumazet if (!(tun->dev->flags & IFF_UP)) 12111bd4978aSEric Dumazet return -EIO; 12121bd4978aSEric Dumazet 121340630b82SMichael S. Tsirkin if (!(tun->flags & IFF_NO_PI)) { 121415718ea0SDan Carpenter if (len < sizeof(pi)) 12151da177e4SLinus Torvalds return -EINVAL; 121615718ea0SDan Carpenter len -= sizeof(pi); 12171da177e4SLinus Torvalds 1218cbbd26b8SAl Viro if (!copy_from_iter_full(&pi, sizeof(pi), from)) 12191da177e4SLinus Torvalds return -EFAULT; 12201da177e4SLinus Torvalds } 12211da177e4SLinus Torvalds 122240630b82SMichael S. Tsirkin if (tun->flags & IFF_VNET_HDR) { 1223e1edab87SWillem de Bruijn int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); 1224e1edab87SWillem de Bruijn 1225e1edab87SWillem de Bruijn if (len < vnet_hdr_sz) 1226f43798c2SRusty Russell return -EINVAL; 1227e1edab87SWillem de Bruijn len -= vnet_hdr_sz; 1228f43798c2SRusty Russell 1229cbbd26b8SAl Viro if (!copy_from_iter_full(&gso, sizeof(gso), from)) 1230f43798c2SRusty Russell return -EFAULT; 1231f43798c2SRusty Russell 12324909122fSHerbert Xu if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && 123356f0dcc5SMichael S. Tsirkin tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len)) 123456f0dcc5SMichael S. Tsirkin gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2); 12354909122fSHerbert Xu 123656f0dcc5SMichael S. Tsirkin if (tun16_to_cpu(tun, gso.hdr_len) > len) 1237f43798c2SRusty Russell return -EINVAL; 1238e1edab87SWillem de Bruijn iov_iter_advance(from, vnet_hdr_sz - sizeof(gso)); 1239f43798c2SRusty Russell } 1240f43798c2SRusty Russell 124140630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) { 1242a504b86eSstephen hemminger align += NET_IP_ALIGN; 12430eca93bcSHerbert Xu if (unlikely(len < ETH_HLEN || 124456f0dcc5SMichael S. Tsirkin (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN))) 1245e01bf1c8SRusty Russell return -EINVAL; 1246e01bf1c8SRusty Russell } 12471da177e4SLinus Torvalds 124896f8d9ecSJason Wang good_linear = SKB_MAX_HEAD(align); 124996f8d9ecSJason Wang 125088529176SJason Wang if (msg_control) { 1251f5ff53b4SAl Viro struct iov_iter i = *from; 1252f5ff53b4SAl Viro 125388529176SJason Wang /* There are 256 bytes to be copied in skb, so there is 125488529176SJason Wang * enough room for skb expand head in case it is used. 12550690899bSMichael S. Tsirkin * The rest of the buffer is mapped from userspace. 12560690899bSMichael S. Tsirkin */ 125756f0dcc5SMichael S. Tsirkin copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN; 125896f8d9ecSJason Wang if (copylen > good_linear) 125996f8d9ecSJason Wang copylen = good_linear; 12603dd5c330SJason Wang linear = copylen; 1261f5ff53b4SAl Viro iov_iter_advance(&i, copylen); 1262f5ff53b4SAl Viro if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS) 126388529176SJason Wang zerocopy = true; 126488529176SJason Wang } 126588529176SJason Wang 126688529176SJason Wang if (!zerocopy) { 12670690899bSMichael S. Tsirkin copylen = len; 126856f0dcc5SMichael S. Tsirkin if (tun16_to_cpu(tun, gso.hdr_len) > good_linear) 126996f8d9ecSJason Wang linear = good_linear; 127096f8d9ecSJason Wang else 127156f0dcc5SMichael S. Tsirkin linear = tun16_to_cpu(tun, gso.hdr_len); 12723dd5c330SJason Wang } 12730690899bSMichael S. Tsirkin 12743dd5c330SJason Wang skb = tun_alloc_skb(tfile, align, copylen, linear, noblock); 127533dccbb0SHerbert Xu if (IS_ERR(skb)) { 127633dccbb0SHerbert Xu if (PTR_ERR(skb) != -EAGAIN) 1277608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 127833dccbb0SHerbert Xu return PTR_ERR(skb); 12791da177e4SLinus Torvalds } 12801da177e4SLinus Torvalds 12810690899bSMichael S. Tsirkin if (zerocopy) 1282f5ff53b4SAl Viro err = zerocopy_sg_from_iter(skb, from); 1283af1cc7a2SJason Wang else 1284f5ff53b4SAl Viro err = skb_copy_datagram_from_iter(skb, 0, from, len); 12850690899bSMichael S. Tsirkin 12860690899bSMichael S. Tsirkin if (err) { 1287608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 12888f22757eSDave Jones kfree_skb(skb); 12891da177e4SLinus Torvalds return -EFAULT; 12908f22757eSDave Jones } 12911da177e4SLinus Torvalds 12923e9e40e7SJarno Rajahalme if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) { 1293df10db98SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_frame_errors); 1294df10db98SPaolo Abeni kfree_skb(skb); 1295df10db98SPaolo Abeni return -EINVAL; 1296df10db98SPaolo Abeni } 1297df10db98SPaolo Abeni 12981da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 129940630b82SMichael S. Tsirkin case IFF_TUN: 130040630b82SMichael S. Tsirkin if (tun->flags & IFF_NO_PI) { 1301f09f7ee2SAng Way Chuang switch (skb->data[0] & 0xf0) { 1302f09f7ee2SAng Way Chuang case 0x40: 1303f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IP); 1304f09f7ee2SAng Way Chuang break; 1305f09f7ee2SAng Way Chuang case 0x60: 1306f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IPV6); 1307f09f7ee2SAng Way Chuang break; 1308f09f7ee2SAng Way Chuang default: 1309608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 1310f09f7ee2SAng Way Chuang kfree_skb(skb); 1311f09f7ee2SAng Way Chuang return -EINVAL; 1312f09f7ee2SAng Way Chuang } 1313f09f7ee2SAng Way Chuang } 1314f09f7ee2SAng Way Chuang 1315459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 13161da177e4SLinus Torvalds skb->protocol = pi.proto; 13174c13eb66SArnaldo Carvalho de Melo skb->dev = tun->dev; 13181da177e4SLinus Torvalds break; 131940630b82SMichael S. Tsirkin case IFF_TAP: 13201da177e4SLinus Torvalds skb->protocol = eth_type_trans(skb, tun->dev); 13211da177e4SLinus Torvalds break; 13226403eab1SJoe Perches } 13231da177e4SLinus Torvalds 13240690899bSMichael S. Tsirkin /* copy skb_ubuf_info for callback when skb has no error */ 13250690899bSMichael S. Tsirkin if (zerocopy) { 13260690899bSMichael S. Tsirkin skb_shinfo(skb)->destructor_arg = msg_control; 13270690899bSMichael S. Tsirkin skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; 1328c9af6db4SPravin B Shelar skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; 1329af1cc7a2SJason Wang } else if (msg_control) { 1330af1cc7a2SJason Wang struct ubuf_info *uarg = msg_control; 1331af1cc7a2SJason Wang uarg->callback(uarg, false); 13320690899bSMichael S. Tsirkin } 13330690899bSMichael S. Tsirkin 133472f65107SVlad Yasevich skb_reset_network_header(skb); 133540893fd0SJason Wang skb_probe_transport_header(skb, 0); 133638502af7SJason Wang 13373958afa1STom Herbert rxhash = skb_get_hash(skb); 1338d4aea20dSAndrey Konovalov #ifndef CONFIG_4KSTACKS 13395503fcecSJason Wang tun_rx_batched(tun, tfile, skb, more); 1340d4aea20dSAndrey Konovalov #else 13411da177e4SLinus Torvalds netif_rx_ni(skb); 1342d4aea20dSAndrey Konovalov #endif 13431da177e4SLinus Torvalds 1344608b9977SPaolo Abeni stats = get_cpu_ptr(tun->pcpu_stats); 1345608b9977SPaolo Abeni u64_stats_update_begin(&stats->syncp); 1346608b9977SPaolo Abeni stats->rx_packets++; 1347608b9977SPaolo Abeni stats->rx_bytes += len; 1348608b9977SPaolo Abeni u64_stats_update_end(&stats->syncp); 1349608b9977SPaolo Abeni put_cpu_ptr(stats); 13501da177e4SLinus Torvalds 13519e85722dSJason Wang tun_flow_update(tun, rxhash, tfile); 13520690899bSMichael S. Tsirkin return total_len; 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds 1355f5ff53b4SAl Viro static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from) 13561da177e4SLinus Torvalds { 135733dccbb0SHerbert Xu struct file *file = iocb->ki_filp; 1358ab46d779SHerbert Xu struct tun_struct *tun = tun_get(file); 135954f968d6SJason Wang struct tun_file *tfile = file->private_data; 1360631ab46bSEric W. Biederman ssize_t result; 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds if (!tun) 13631da177e4SLinus Torvalds return -EBADFD; 13641da177e4SLinus Torvalds 13655503fcecSJason Wang result = tun_get_user(tun, tfile, NULL, from, 13665503fcecSJason Wang file->f_flags & O_NONBLOCK, false); 1367631ab46bSEric W. Biederman 1368631ab46bSEric W. Biederman tun_put(tun); 1369631ab46bSEric W. Biederman return result; 13701da177e4SLinus Torvalds } 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds /* Put packet to the user space buffer */ 13736f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun, 137454f968d6SJason Wang struct tun_file *tfile, 13751da177e4SLinus Torvalds struct sk_buff *skb, 1376e0b46d0eSHerbert Xu struct iov_iter *iter) 13771da177e4SLinus Torvalds { 13781da177e4SLinus Torvalds struct tun_pi pi = { 0, skb->protocol }; 1379608b9977SPaolo Abeni struct tun_pcpu_stats *stats; 1380e0b46d0eSHerbert Xu ssize_t total; 13818c847d25SJason Wang int vlan_offset = 0; 1382a8f9bfdfSHerbert Xu int vlan_hlen = 0; 13832eb783c4SHerbert Xu int vnet_hdr_sz = 0; 1384a8f9bfdfSHerbert Xu 1385df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) 1386a8f9bfdfSHerbert Xu vlan_hlen = VLAN_HLEN; 13871da177e4SLinus Torvalds 138840630b82SMichael S. Tsirkin if (tun->flags & IFF_VNET_HDR) 1389e1edab87SWillem de Bruijn vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); 13901da177e4SLinus Torvalds 1391e0b46d0eSHerbert Xu total = skb->len + vlan_hlen + vnet_hdr_sz; 1392e0b46d0eSHerbert Xu 139340630b82SMichael S. Tsirkin if (!(tun->flags & IFF_NO_PI)) { 1394e0b46d0eSHerbert Xu if (iov_iter_count(iter) < sizeof(pi)) 13951da177e4SLinus Torvalds return -EINVAL; 13961da177e4SLinus Torvalds 1397e0b46d0eSHerbert Xu total += sizeof(pi); 1398e0b46d0eSHerbert Xu if (iov_iter_count(iter) < total) { 13991da177e4SLinus Torvalds /* Packet will be striped */ 14001da177e4SLinus Torvalds pi.flags |= TUN_PKT_STRIP; 14011da177e4SLinus Torvalds } 14021da177e4SLinus Torvalds 1403e0b46d0eSHerbert Xu if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi)) 14041da177e4SLinus Torvalds return -EFAULT; 14051da177e4SLinus Torvalds } 14061da177e4SLinus Torvalds 14072eb783c4SHerbert Xu if (vnet_hdr_sz) { 14089403cd7cSJarno Rajahalme struct virtio_net_hdr gso; 140934166093SMike Rapoport 1410e0b46d0eSHerbert Xu if (iov_iter_count(iter) < vnet_hdr_sz) 1411f43798c2SRusty Russell return -EINVAL; 1412f43798c2SRusty Russell 14133e9e40e7SJarno Rajahalme if (virtio_net_hdr_from_skb(skb, &gso, 14146391a448SJason Wang tun_is_little_endian(tun), true)) { 1415f43798c2SRusty Russell struct skb_shared_info *sinfo = skb_shinfo(skb); 14166b8a66eeSJoe Perches pr_err("unexpected GSO type: " 1417ef3db4a5SMichael S. Tsirkin "0x%x, gso_size %d, hdr_len %d\n", 141856f0dcc5SMichael S. Tsirkin sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size), 141956f0dcc5SMichael S. Tsirkin tun16_to_cpu(tun, gso.hdr_len)); 1420ef3db4a5SMichael S. Tsirkin print_hex_dump(KERN_ERR, "tun: ", 1421ef3db4a5SMichael S. Tsirkin DUMP_PREFIX_NONE, 1422ef3db4a5SMichael S. Tsirkin 16, 1, skb->head, 142356f0dcc5SMichael S. Tsirkin min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true); 1424ef3db4a5SMichael S. Tsirkin WARN_ON_ONCE(1); 1425ef3db4a5SMichael S. Tsirkin return -EINVAL; 1426ef3db4a5SMichael S. Tsirkin } 1427f43798c2SRusty Russell 1428e0b46d0eSHerbert Xu if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso)) 1429f43798c2SRusty Russell return -EFAULT; 14308c847d25SJason Wang 14318c847d25SJason Wang iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso)); 1432f43798c2SRusty Russell } 1433f43798c2SRusty Russell 1434a8f9bfdfSHerbert Xu if (vlan_hlen) { 1435e0b46d0eSHerbert Xu int ret; 14366680ec68SJason Wang struct { 14376680ec68SJason Wang __be16 h_vlan_proto; 14386680ec68SJason Wang __be16 h_vlan_TCI; 14396680ec68SJason Wang } veth; 14401da177e4SLinus Torvalds 14416680ec68SJason Wang veth.h_vlan_proto = skb->vlan_proto; 1442df8a39deSJiri Pirko veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb)); 14431da177e4SLinus Torvalds 14446680ec68SJason Wang vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); 14456680ec68SJason Wang 1446e0b46d0eSHerbert Xu ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset); 1447e0b46d0eSHerbert Xu if (ret || !iov_iter_count(iter)) 14486680ec68SJason Wang goto done; 14496680ec68SJason Wang 1450e0b46d0eSHerbert Xu ret = copy_to_iter(&veth, sizeof(veth), iter); 1451e0b46d0eSHerbert Xu if (ret != sizeof(veth) || !iov_iter_count(iter)) 14526680ec68SJason Wang goto done; 14536680ec68SJason Wang } 14546680ec68SJason Wang 1455e0b46d0eSHerbert Xu skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset); 14566680ec68SJason Wang 14576680ec68SJason Wang done: 1458608b9977SPaolo Abeni /* caller is in process context, */ 1459608b9977SPaolo Abeni stats = get_cpu_ptr(tun->pcpu_stats); 1460608b9977SPaolo Abeni u64_stats_update_begin(&stats->syncp); 1461608b9977SPaolo Abeni stats->tx_packets++; 1462608b9977SPaolo Abeni stats->tx_bytes += skb->len + vlan_hlen; 1463608b9977SPaolo Abeni u64_stats_update_end(&stats->syncp); 1464608b9977SPaolo Abeni put_cpu_ptr(tun->pcpu_stats); 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds return total; 14671da177e4SLinus Torvalds } 14681da177e4SLinus Torvalds 14691576d986SJason Wang static struct sk_buff *tun_ring_recv(struct tun_file *tfile, int noblock, 14701576d986SJason Wang int *err) 14711576d986SJason Wang { 14721576d986SJason Wang DECLARE_WAITQUEUE(wait, current); 14731576d986SJason Wang struct sk_buff *skb = NULL; 1474f48cc6b2SJason Wang int error = 0; 14751576d986SJason Wang 14761576d986SJason Wang skb = skb_array_consume(&tfile->tx_array); 14771576d986SJason Wang if (skb) 14781576d986SJason Wang goto out; 14791576d986SJason Wang if (noblock) { 1480f48cc6b2SJason Wang error = -EAGAIN; 14811576d986SJason Wang goto out; 14821576d986SJason Wang } 14831576d986SJason Wang 14841576d986SJason Wang add_wait_queue(&tfile->wq.wait, &wait); 14851576d986SJason Wang current->state = TASK_INTERRUPTIBLE; 14861576d986SJason Wang 14871576d986SJason Wang while (1) { 14881576d986SJason Wang skb = skb_array_consume(&tfile->tx_array); 14891576d986SJason Wang if (skb) 14901576d986SJason Wang break; 14911576d986SJason Wang if (signal_pending(current)) { 1492f48cc6b2SJason Wang error = -ERESTARTSYS; 14931576d986SJason Wang break; 14941576d986SJason Wang } 14951576d986SJason Wang if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) { 1496f48cc6b2SJason Wang error = -EFAULT; 14971576d986SJason Wang break; 14981576d986SJason Wang } 14991576d986SJason Wang 15001576d986SJason Wang schedule(); 15011576d986SJason Wang } 15021576d986SJason Wang 15031576d986SJason Wang current->state = TASK_RUNNING; 15041576d986SJason Wang remove_wait_queue(&tfile->wq.wait, &wait); 15051576d986SJason Wang 15061576d986SJason Wang out: 1507f48cc6b2SJason Wang *err = error; 15081576d986SJason Wang return skb; 15091576d986SJason Wang } 15101576d986SJason Wang 151154f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, 15129b067034SAl Viro struct iov_iter *to, 15139b067034SAl Viro int noblock) 15141da177e4SLinus Torvalds { 15151da177e4SLinus Torvalds struct sk_buff *skb; 15169b067034SAl Viro ssize_t ret; 15171576d986SJason Wang int err; 15181da177e4SLinus Torvalds 15193872baf6SRami Rosen tun_debug(KERN_INFO, tun, "tun_do_read\n"); 15201da177e4SLinus Torvalds 15219b067034SAl Viro if (!iov_iter_count(to)) 15229b067034SAl Viro return 0; 15231da177e4SLinus Torvalds 15241576d986SJason Wang /* Read frames from ring */ 15251576d986SJason Wang skb = tun_ring_recv(tfile, noblock, &err); 1526e0b46d0eSHerbert Xu if (!skb) 1527957f094fSAlex Gartrell return err; 1528e0b46d0eSHerbert Xu 15299b067034SAl Viro ret = tun_put_user(tun, tfile, skb, to); 1530f51a5e82SJason Wang if (unlikely(ret < 0)) 15311da177e4SLinus Torvalds kfree_skb(skb); 1532f51a5e82SJason Wang else 1533f51a5e82SJason Wang consume_skb(skb); 15341da177e4SLinus Torvalds 153505c2828cSMichael S. Tsirkin return ret; 153605c2828cSMichael S. Tsirkin } 153705c2828cSMichael S. Tsirkin 15389b067034SAl Viro static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to) 153905c2828cSMichael S. Tsirkin { 154005c2828cSMichael S. Tsirkin struct file *file = iocb->ki_filp; 154105c2828cSMichael S. Tsirkin struct tun_file *tfile = file->private_data; 154205c2828cSMichael S. Tsirkin struct tun_struct *tun = __tun_get(tfile); 15439b067034SAl Viro ssize_t len = iov_iter_count(to), ret; 154405c2828cSMichael S. Tsirkin 154505c2828cSMichael S. Tsirkin if (!tun) 154605c2828cSMichael S. Tsirkin return -EBADFD; 15479b067034SAl Viro ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK); 154842404c09SDavid S. Miller ret = min_t(ssize_t, ret, len); 1549d0b7da8aSZhi Yong Wu if (ret > 0) 1550d0b7da8aSZhi Yong Wu iocb->ki_pos = ret; 1551631ab46bSEric W. Biederman tun_put(tun); 15521da177e4SLinus Torvalds return ret; 15531da177e4SLinus Torvalds } 15541da177e4SLinus Torvalds 155596442e42SJason Wang static void tun_free_netdev(struct net_device *dev) 155696442e42SJason Wang { 155796442e42SJason Wang struct tun_struct *tun = netdev_priv(dev); 155896442e42SJason Wang 15594008e97fSJason Wang BUG_ON(!(list_empty(&tun->disabled))); 1560608b9977SPaolo Abeni free_percpu(tun->pcpu_stats); 156196442e42SJason Wang tun_flow_uninit(tun); 15625dbbaf2dSPaul Moore security_tun_dev_free_security(tun->security); 156396442e42SJason Wang } 156496442e42SJason Wang 15651da177e4SLinus Torvalds static void tun_setup(struct net_device *dev) 15661da177e4SLinus Torvalds { 15671da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 15681da177e4SLinus Torvalds 15690625c883SEric W. Biederman tun->owner = INVALID_UID; 15700625c883SEric W. Biederman tun->group = INVALID_GID; 15711da177e4SLinus Torvalds 15721da177e4SLinus Torvalds dev->ethtool_ops = &tun_ethtool_ops; 1573*cf124db5SDavid S. Miller dev->needs_free_netdev = true; 1574*cf124db5SDavid S. Miller dev->priv_destructor = tun_free_netdev; 1575016adb72SJason Wang /* We prefer our own queue length */ 1576016adb72SJason Wang dev->tx_queue_len = TUN_READQ_SIZE; 15771da177e4SLinus Torvalds } 15781da177e4SLinus Torvalds 1579f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap 1580f019a7a5SEric W. Biederman * device with netlink. 1581f019a7a5SEric W. Biederman */ 1582f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[]) 1583f019a7a5SEric W. Biederman { 1584f019a7a5SEric W. Biederman return -EINVAL; 1585f019a7a5SEric W. Biederman } 1586f019a7a5SEric W. Biederman 1587f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = { 1588f019a7a5SEric W. Biederman .kind = DRV_NAME, 1589f019a7a5SEric W. Biederman .priv_size = sizeof(struct tun_struct), 1590f019a7a5SEric W. Biederman .setup = tun_setup, 1591f019a7a5SEric W. Biederman .validate = tun_validate, 1592f019a7a5SEric W. Biederman }; 1593f019a7a5SEric W. Biederman 159433dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk) 159533dccbb0SHerbert Xu { 159654f968d6SJason Wang struct tun_file *tfile; 159743815482SEric Dumazet wait_queue_head_t *wqueue; 159833dccbb0SHerbert Xu 159933dccbb0SHerbert Xu if (!sock_writeable(sk)) 160033dccbb0SHerbert Xu return; 160133dccbb0SHerbert Xu 16029cd3e072SEric Dumazet if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags)) 160333dccbb0SHerbert Xu return; 160433dccbb0SHerbert Xu 160543815482SEric Dumazet wqueue = sk_sleep(sk); 160643815482SEric Dumazet if (wqueue && waitqueue_active(wqueue)) 160743815482SEric Dumazet wake_up_interruptible_sync_poll(wqueue, POLLOUT | 160805c2828cSMichael S. Tsirkin POLLWRNORM | POLLWRBAND); 1609c722c625SHerbert Xu 161054f968d6SJason Wang tfile = container_of(sk, struct tun_file, sk); 161154f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); 161233dccbb0SHerbert Xu } 161333dccbb0SHerbert Xu 16141b784140SYing Xue static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len) 161505c2828cSMichael S. Tsirkin { 161654f968d6SJason Wang int ret; 161754f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 161854f968d6SJason Wang struct tun_struct *tun = __tun_get(tfile); 161954f968d6SJason Wang 162054f968d6SJason Wang if (!tun) 162154f968d6SJason Wang return -EBADFD; 1622f5ff53b4SAl Viro 1623c0371da6SAl Viro ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter, 16245503fcecSJason Wang m->msg_flags & MSG_DONTWAIT, 16255503fcecSJason Wang m->msg_flags & MSG_MORE); 162654f968d6SJason Wang tun_put(tun); 162754f968d6SJason Wang return ret; 162805c2828cSMichael S. Tsirkin } 162905c2828cSMichael S. Tsirkin 16301b784140SYing Xue static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, 163105c2828cSMichael S. Tsirkin int flags) 163205c2828cSMichael S. Tsirkin { 163354f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 163454f968d6SJason Wang struct tun_struct *tun = __tun_get(tfile); 163505c2828cSMichael S. Tsirkin int ret; 163654f968d6SJason Wang 163754f968d6SJason Wang if (!tun) 163854f968d6SJason Wang return -EBADFD; 163954f968d6SJason Wang 1640eda29772SRichard Cochran if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) { 16413811ae76SGao feng ret = -EINVAL; 16423811ae76SGao feng goto out; 16433811ae76SGao feng } 1644eda29772SRichard Cochran if (flags & MSG_ERRQUEUE) { 1645eda29772SRichard Cochran ret = sock_recv_errqueue(sock->sk, m, total_len, 1646eda29772SRichard Cochran SOL_PACKET, TUN_TX_TIMESTAMP); 1647eda29772SRichard Cochran goto out; 1648eda29772SRichard Cochran } 1649c0371da6SAl Viro ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT); 165087897931SAlex Gartrell if (ret > (ssize_t)total_len) { 165142404c09SDavid S. Miller m->msg_flags |= MSG_TRUNC; 165242404c09SDavid S. Miller ret = flags & MSG_TRUNC ? ret : total_len; 165342404c09SDavid S. Miller } 16543811ae76SGao feng out: 165554f968d6SJason Wang tun_put(tun); 165605c2828cSMichael S. Tsirkin return ret; 165705c2828cSMichael S. Tsirkin } 165805c2828cSMichael S. Tsirkin 16591576d986SJason Wang static int tun_peek_len(struct socket *sock) 16601576d986SJason Wang { 16611576d986SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 16621576d986SJason Wang struct tun_struct *tun; 16631576d986SJason Wang int ret = 0; 16641576d986SJason Wang 16651576d986SJason Wang tun = __tun_get(tfile); 16661576d986SJason Wang if (!tun) 16671576d986SJason Wang return 0; 16681576d986SJason Wang 16691576d986SJason Wang ret = skb_array_peek_len(&tfile->tx_array); 16701576d986SJason Wang tun_put(tun); 16711576d986SJason Wang 16721576d986SJason Wang return ret; 16731576d986SJason Wang } 16741576d986SJason Wang 167505c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */ 167605c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = { 16771576d986SJason Wang .peek_len = tun_peek_len, 167805c2828cSMichael S. Tsirkin .sendmsg = tun_sendmsg, 167905c2828cSMichael S. Tsirkin .recvmsg = tun_recvmsg, 168005c2828cSMichael S. Tsirkin }; 168105c2828cSMichael S. Tsirkin 168233dccbb0SHerbert Xu static struct proto tun_proto = { 168333dccbb0SHerbert Xu .name = "tun", 168433dccbb0SHerbert Xu .owner = THIS_MODULE, 168554f968d6SJason Wang .obj_size = sizeof(struct tun_file), 168633dccbb0SHerbert Xu }; 1687f019a7a5SEric W. Biederman 1688980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun) 1689980c9e8cSDavid Woodhouse { 1690031f5e03SMichael S. Tsirkin return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP); 1691980c9e8cSDavid Woodhouse } 1692980c9e8cSDavid Woodhouse 1693980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr, 1694980c9e8cSDavid Woodhouse char *buf) 1695980c9e8cSDavid Woodhouse { 1696980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 1697980c9e8cSDavid Woodhouse return sprintf(buf, "0x%x\n", tun_flags(tun)); 1698980c9e8cSDavid Woodhouse } 1699980c9e8cSDavid Woodhouse 1700980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr, 1701980c9e8cSDavid Woodhouse char *buf) 1702980c9e8cSDavid Woodhouse { 1703980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 17040625c883SEric W. Biederman return uid_valid(tun->owner)? 17050625c883SEric W. Biederman sprintf(buf, "%u\n", 17060625c883SEric W. Biederman from_kuid_munged(current_user_ns(), tun->owner)): 17070625c883SEric W. Biederman sprintf(buf, "-1\n"); 1708980c9e8cSDavid Woodhouse } 1709980c9e8cSDavid Woodhouse 1710980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr, 1711980c9e8cSDavid Woodhouse char *buf) 1712980c9e8cSDavid Woodhouse { 1713980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 17140625c883SEric W. Biederman return gid_valid(tun->group) ? 17150625c883SEric W. Biederman sprintf(buf, "%u\n", 17160625c883SEric W. Biederman from_kgid_munged(current_user_ns(), tun->group)): 17170625c883SEric W. Biederman sprintf(buf, "-1\n"); 1718980c9e8cSDavid Woodhouse } 1719980c9e8cSDavid Woodhouse 1720980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL); 1721980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL); 1722980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL); 1723980c9e8cSDavid Woodhouse 1724c4d33e24STakashi Iwai static struct attribute *tun_dev_attrs[] = { 1725c4d33e24STakashi Iwai &dev_attr_tun_flags.attr, 1726c4d33e24STakashi Iwai &dev_attr_owner.attr, 1727c4d33e24STakashi Iwai &dev_attr_group.attr, 1728c4d33e24STakashi Iwai NULL 1729c4d33e24STakashi Iwai }; 1730c4d33e24STakashi Iwai 1731c4d33e24STakashi Iwai static const struct attribute_group tun_attr_group = { 1732c4d33e24STakashi Iwai .attrs = tun_dev_attrs 1733c4d33e24STakashi Iwai }; 1734c4d33e24STakashi Iwai 1735d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) 17361da177e4SLinus Torvalds { 17371da177e4SLinus Torvalds struct tun_struct *tun; 173854f968d6SJason Wang struct tun_file *tfile = file->private_data; 17391da177e4SLinus Torvalds struct net_device *dev; 17401da177e4SLinus Torvalds int err; 17411da177e4SLinus Torvalds 17427c0c3b1aSJason Wang if (tfile->detached) 17437c0c3b1aSJason Wang return -EINVAL; 17447c0c3b1aSJason Wang 174574a3e5a7SEric W. Biederman dev = __dev_get_by_name(net, ifr->ifr_name); 174674a3e5a7SEric W. Biederman if (dev) { 1747f85ba780SDavid Woodhouse if (ifr->ifr_flags & IFF_TUN_EXCL) 1748f85ba780SDavid Woodhouse return -EBUSY; 174974a3e5a7SEric W. Biederman if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) 175074a3e5a7SEric W. Biederman tun = netdev_priv(dev); 175174a3e5a7SEric W. Biederman else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) 175274a3e5a7SEric W. Biederman tun = netdev_priv(dev); 175374a3e5a7SEric W. Biederman else 175474a3e5a7SEric W. Biederman return -EINVAL; 175574a3e5a7SEric W. Biederman 17568e6d91aeSJason Wang if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) != 175740630b82SMichael S. Tsirkin !!(tun->flags & IFF_MULTI_QUEUE)) 17588e6d91aeSJason Wang return -EINVAL; 17598e6d91aeSJason Wang 1760cde8b15fSJason Wang if (tun_not_capable(tun)) 17612b980dbdSPaul Moore return -EPERM; 17625dbbaf2dSPaul Moore err = security_tun_dev_open(tun->security); 17632b980dbdSPaul Moore if (err < 0) 17642b980dbdSPaul Moore return err; 17652b980dbdSPaul Moore 1766849c9b6fSPavel Emelyanov err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER); 1767a7385ba2SEric W. Biederman if (err < 0) 1768a7385ba2SEric W. Biederman return err; 17694008e97fSJason Wang 177040630b82SMichael S. Tsirkin if (tun->flags & IFF_MULTI_QUEUE && 1771e8dbad66SJason Wang (tun->numqueues + tun->numdisabled > 1)) { 1772e8dbad66SJason Wang /* One or more queue has already been attached, no need 1773e8dbad66SJason Wang * to initialize the device again. 1774e8dbad66SJason Wang */ 1775e8dbad66SJason Wang return 0; 1776e8dbad66SJason Wang } 177786a264abSDavid Howells } 17781da177e4SLinus Torvalds else { 17791da177e4SLinus Torvalds char *name; 17801da177e4SLinus Torvalds unsigned long flags = 0; 1781edfb6a14SJason Wang int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ? 1782edfb6a14SJason Wang MAX_TAP_QUEUES : 1; 17831da177e4SLinus Torvalds 1784c260b772SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1785ca6bb5d7SDavid Woodhouse return -EPERM; 17862b980dbdSPaul Moore err = security_tun_dev_create(); 17872b980dbdSPaul Moore if (err < 0) 17882b980dbdSPaul Moore return err; 1789ca6bb5d7SDavid Woodhouse 17901da177e4SLinus Torvalds /* Set dev type */ 17911da177e4SLinus Torvalds if (ifr->ifr_flags & IFF_TUN) { 17921da177e4SLinus Torvalds /* TUN device */ 179340630b82SMichael S. Tsirkin flags |= IFF_TUN; 17941da177e4SLinus Torvalds name = "tun%d"; 17951da177e4SLinus Torvalds } else if (ifr->ifr_flags & IFF_TAP) { 17961da177e4SLinus Torvalds /* TAP device */ 179740630b82SMichael S. Tsirkin flags |= IFF_TAP; 17981da177e4SLinus Torvalds name = "tap%d"; 17991da177e4SLinus Torvalds } else 180036989b90SKusanagi Kouichi return -EINVAL; 18011da177e4SLinus Torvalds 18021da177e4SLinus Torvalds if (*ifr->ifr_name) 18031da177e4SLinus Torvalds name = ifr->ifr_name; 18041da177e4SLinus Torvalds 1805c8d68e6bSJason Wang dev = alloc_netdev_mqs(sizeof(struct tun_struct), name, 1806c835a677STom Gundersen NET_NAME_UNKNOWN, tun_setup, queues, 1807c835a677STom Gundersen queues); 1808edfb6a14SJason Wang 18091da177e4SLinus Torvalds if (!dev) 18101da177e4SLinus Torvalds return -ENOMEM; 18111da177e4SLinus Torvalds 1812fc54c658SPavel Emelyanov dev_net_set(dev, net); 1813f019a7a5SEric W. Biederman dev->rtnl_link_ops = &tun_link_ops; 1814fb7589a1SPavel Emelyanov dev->ifindex = tfile->ifindex; 1815c4d33e24STakashi Iwai dev->sysfs_groups[0] = &tun_attr_group; 1816758e43b7SStephen Hemminger 18171da177e4SLinus Torvalds tun = netdev_priv(dev); 18181da177e4SLinus Torvalds tun->dev = dev; 18191da177e4SLinus Torvalds tun->flags = flags; 1820f271b2ccSMax Krasnyansky tun->txflt.count = 0; 1821d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr); 18221da177e4SLinus Torvalds 1823eaea34b2SPaolo Abeni tun->align = NET_SKB_PAD; 182454f968d6SJason Wang tun->filter_attached = false; 182554f968d6SJason Wang tun->sndbuf = tfile->socket.sk->sk_sndbuf; 18265503fcecSJason Wang tun->rx_batched = 0; 182733dccbb0SHerbert Xu 1828608b9977SPaolo Abeni tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats); 1829608b9977SPaolo Abeni if (!tun->pcpu_stats) { 1830608b9977SPaolo Abeni err = -ENOMEM; 1831608b9977SPaolo Abeni goto err_free_dev; 1832608b9977SPaolo Abeni } 1833608b9977SPaolo Abeni 183496442e42SJason Wang spin_lock_init(&tun->lock); 183596442e42SJason Wang 18365dbbaf2dSPaul Moore err = security_tun_dev_alloc_security(&tun->security); 18375dbbaf2dSPaul Moore if (err < 0) 1838608b9977SPaolo Abeni goto err_free_stat; 18392b980dbdSPaul Moore 18401da177e4SLinus Torvalds tun_net_init(dev); 1841944a1376SPavel Emelyanov tun_flow_init(tun); 184296442e42SJason Wang 184388255375SMichał Mirosław dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | 18446680ec68SJason Wang TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX | 18456680ec68SJason Wang NETIF_F_HW_VLAN_STAG_TX; 18462a2bbf17SPaolo Abeni dev->features = dev->hw_features | NETIF_F_LLTX; 18476671b224SFernando Luis Vazquez Cao dev->vlan_features = dev->features & 18486671b224SFernando Luis Vazquez Cao ~(NETIF_F_HW_VLAN_CTAG_TX | 18496671b224SFernando Luis Vazquez Cao NETIF_F_HW_VLAN_STAG_TX); 185088255375SMichał Mirosław 18514008e97fSJason Wang INIT_LIST_HEAD(&tun->disabled); 1852849c9b6fSPavel Emelyanov err = tun_attach(tun, file, false); 1853eb0fb363SJason Wang if (err < 0) 1854662ca437SJason Wang goto err_free_flow; 1855eb0fb363SJason Wang 18561da177e4SLinus Torvalds err = register_netdevice(tun->dev); 18571da177e4SLinus Torvalds if (err < 0) 1858662ca437SJason Wang goto err_detach; 1859af668b3cSMichael S. Tsirkin } 1860980c9e8cSDavid Woodhouse 1861eb0fb363SJason Wang netif_carrier_on(tun->dev); 18621da177e4SLinus Torvalds 18636b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_set_iff\n"); 18641da177e4SLinus Torvalds 1865031f5e03SMichael S. Tsirkin tun->flags = (tun->flags & ~TUN_FEATURES) | 1866031f5e03SMichael S. Tsirkin (ifr->ifr_flags & TUN_FEATURES); 1867c8d68e6bSJason Wang 1868e35259a9SMax Krasnyansky /* Make sure persistent devices do not get stuck in 1869e35259a9SMax Krasnyansky * xoff state. 1870e35259a9SMax Krasnyansky */ 1871e35259a9SMax Krasnyansky if (netif_running(tun->dev)) 1872c8d68e6bSJason Wang netif_tx_wake_all_queues(tun->dev); 1873e35259a9SMax Krasnyansky 18741da177e4SLinus Torvalds strcpy(ifr->ifr_name, tun->dev->name); 18751da177e4SLinus Torvalds return 0; 18761da177e4SLinus Torvalds 1877662ca437SJason Wang err_detach: 1878662ca437SJason Wang tun_detach_all(dev); 1879662ca437SJason Wang err_free_flow: 1880662ca437SJason Wang tun_flow_uninit(tun); 1881662ca437SJason Wang security_tun_dev_free_security(tun->security); 1882608b9977SPaolo Abeni err_free_stat: 1883608b9977SPaolo Abeni free_percpu(tun->pcpu_stats); 18841da177e4SLinus Torvalds err_free_dev: 18851da177e4SLinus Torvalds free_netdev(dev); 18861da177e4SLinus Torvalds return err; 18871da177e4SLinus Torvalds } 18881da177e4SLinus Torvalds 18899ce99cf6SRami Rosen static void tun_get_iff(struct net *net, struct tun_struct *tun, 1890876bfd4dSHerbert Xu struct ifreq *ifr) 1891e3b99556SMark McLoughlin { 18926b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_get_iff\n"); 1893e3b99556SMark McLoughlin 1894e3b99556SMark McLoughlin strcpy(ifr->ifr_name, tun->dev->name); 1895e3b99556SMark McLoughlin 1896980c9e8cSDavid Woodhouse ifr->ifr_flags = tun_flags(tun); 1897e3b99556SMark McLoughlin 1898e3b99556SMark McLoughlin } 1899e3b99556SMark McLoughlin 19005228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no 19015228ddc9SRusty Russell * privs required. */ 190288255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg) 19035228ddc9SRusty Russell { 1904c8f44affSMichał Mirosław netdev_features_t features = 0; 19055228ddc9SRusty Russell 19065228ddc9SRusty Russell if (arg & TUN_F_CSUM) { 190788255375SMichał Mirosław features |= NETIF_F_HW_CSUM; 19085228ddc9SRusty Russell arg &= ~TUN_F_CSUM; 19095228ddc9SRusty Russell 19105228ddc9SRusty Russell if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { 19115228ddc9SRusty Russell if (arg & TUN_F_TSO_ECN) { 19125228ddc9SRusty Russell features |= NETIF_F_TSO_ECN; 19135228ddc9SRusty Russell arg &= ~TUN_F_TSO_ECN; 19145228ddc9SRusty Russell } 19155228ddc9SRusty Russell if (arg & TUN_F_TSO4) 19165228ddc9SRusty Russell features |= NETIF_F_TSO; 19175228ddc9SRusty Russell if (arg & TUN_F_TSO6) 19185228ddc9SRusty Russell features |= NETIF_F_TSO6; 19195228ddc9SRusty Russell arg &= ~(TUN_F_TSO4|TUN_F_TSO6); 19205228ddc9SRusty Russell } 1921e3e3c423SVlad Yasevich 1922e3e3c423SVlad Yasevich if (arg & TUN_F_UFO) { 1923e3e3c423SVlad Yasevich features |= NETIF_F_UFO; 1924e3e3c423SVlad Yasevich arg &= ~TUN_F_UFO; 1925e3e3c423SVlad Yasevich } 19265228ddc9SRusty Russell } 19275228ddc9SRusty Russell 19285228ddc9SRusty Russell /* This gives the user a way to test for new features in future by 19295228ddc9SRusty Russell * trying to set them. */ 19305228ddc9SRusty Russell if (arg) 19315228ddc9SRusty Russell return -EINVAL; 19325228ddc9SRusty Russell 193388255375SMichał Mirosław tun->set_features = features; 193409050957SYaroslav Isakov tun->dev->wanted_features &= ~TUN_USER_FEATURES; 193509050957SYaroslav Isakov tun->dev->wanted_features |= features; 193688255375SMichał Mirosław netdev_update_features(tun->dev); 19375228ddc9SRusty Russell 19385228ddc9SRusty Russell return 0; 19395228ddc9SRusty Russell } 19405228ddc9SRusty Russell 1941c8d68e6bSJason Wang static void tun_detach_filter(struct tun_struct *tun, int n) 1942c8d68e6bSJason Wang { 1943c8d68e6bSJason Wang int i; 1944c8d68e6bSJason Wang struct tun_file *tfile; 1945c8d68e6bSJason Wang 1946c8d68e6bSJason Wang for (i = 0; i < n; i++) { 1947b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 19488ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 19498ced425eSHannes Frederic Sowa sk_detach_filter(tfile->socket.sk); 19508ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 1951c8d68e6bSJason Wang } 1952c8d68e6bSJason Wang 1953c8d68e6bSJason Wang tun->filter_attached = false; 1954c8d68e6bSJason Wang } 1955c8d68e6bSJason Wang 1956c8d68e6bSJason Wang static int tun_attach_filter(struct tun_struct *tun) 1957c8d68e6bSJason Wang { 1958c8d68e6bSJason Wang int i, ret = 0; 1959c8d68e6bSJason Wang struct tun_file *tfile; 1960c8d68e6bSJason Wang 1961c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 1962b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 19638ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 19648ced425eSHannes Frederic Sowa ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); 19658ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 1966c8d68e6bSJason Wang if (ret) { 1967c8d68e6bSJason Wang tun_detach_filter(tun, i); 1968c8d68e6bSJason Wang return ret; 1969c8d68e6bSJason Wang } 1970c8d68e6bSJason Wang } 1971c8d68e6bSJason Wang 1972c8d68e6bSJason Wang tun->filter_attached = true; 1973c8d68e6bSJason Wang return ret; 1974c8d68e6bSJason Wang } 1975c8d68e6bSJason Wang 1976c8d68e6bSJason Wang static void tun_set_sndbuf(struct tun_struct *tun) 1977c8d68e6bSJason Wang { 1978c8d68e6bSJason Wang struct tun_file *tfile; 1979c8d68e6bSJason Wang int i; 1980c8d68e6bSJason Wang 1981c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 1982b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 1983c8d68e6bSJason Wang tfile->socket.sk->sk_sndbuf = tun->sndbuf; 1984c8d68e6bSJason Wang } 1985c8d68e6bSJason Wang } 1986c8d68e6bSJason Wang 1987cde8b15fSJason Wang static int tun_set_queue(struct file *file, struct ifreq *ifr) 1988cde8b15fSJason Wang { 1989cde8b15fSJason Wang struct tun_file *tfile = file->private_data; 1990cde8b15fSJason Wang struct tun_struct *tun; 1991cde8b15fSJason Wang int ret = 0; 1992cde8b15fSJason Wang 1993cde8b15fSJason Wang rtnl_lock(); 1994cde8b15fSJason Wang 1995cde8b15fSJason Wang if (ifr->ifr_flags & IFF_ATTACH_QUEUE) { 19964008e97fSJason Wang tun = tfile->detached; 19975dbbaf2dSPaul Moore if (!tun) { 1998cde8b15fSJason Wang ret = -EINVAL; 19995dbbaf2dSPaul Moore goto unlock; 20005dbbaf2dSPaul Moore } 20015dbbaf2dSPaul Moore ret = security_tun_dev_attach_queue(tun->security); 20025dbbaf2dSPaul Moore if (ret < 0) 20035dbbaf2dSPaul Moore goto unlock; 2004849c9b6fSPavel Emelyanov ret = tun_attach(tun, file, false); 20054008e97fSJason Wang } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) { 2006b8deabd3SJason Wang tun = rtnl_dereference(tfile->tun); 200740630b82SMichael S. Tsirkin if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached) 20084008e97fSJason Wang ret = -EINVAL; 2009cde8b15fSJason Wang else 20104008e97fSJason Wang __tun_detach(tfile, false); 20114008e97fSJason Wang } else 2012cde8b15fSJason Wang ret = -EINVAL; 2013cde8b15fSJason Wang 20145dbbaf2dSPaul Moore unlock: 2015cde8b15fSJason Wang rtnl_unlock(); 2016cde8b15fSJason Wang return ret; 2017cde8b15fSJason Wang } 2018cde8b15fSJason Wang 201950857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd, 202050857e2aSArnd Bergmann unsigned long arg, int ifreq_len) 20211da177e4SLinus Torvalds { 202236b50babSEric W. Biederman struct tun_file *tfile = file->private_data; 2023631ab46bSEric W. Biederman struct tun_struct *tun; 20241da177e4SLinus Torvalds void __user* argp = (void __user*)arg; 20251da177e4SLinus Torvalds struct ifreq ifr; 20260625c883SEric W. Biederman kuid_t owner; 20270625c883SEric W. Biederman kgid_t group; 202833dccbb0SHerbert Xu int sndbuf; 2029d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 2030fb7589a1SPavel Emelyanov unsigned int ifindex; 20311cf8e410SMichael S. Tsirkin int le; 2032f271b2ccSMax Krasnyansky int ret; 20331da177e4SLinus Torvalds 203420861f26SGao Feng if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == SOCK_IOC_TYPE) { 203550857e2aSArnd Bergmann if (copy_from_user(&ifr, argp, ifreq_len)) 20361da177e4SLinus Torvalds return -EFAULT; 20378bbb1813SDavid S. Miller } else { 2038a117dacdSMathias Krause memset(&ifr, 0, sizeof(ifr)); 20398bbb1813SDavid S. Miller } 2040631ab46bSEric W. Biederman if (cmd == TUNGETFEATURES) { 2041631ab46bSEric W. Biederman /* Currently this just means: "what IFF flags are valid?". 2042631ab46bSEric W. Biederman * This is needed because we never checked for invalid flags on 2043031f5e03SMichael S. Tsirkin * TUNSETIFF. 2044031f5e03SMichael S. Tsirkin */ 2045031f5e03SMichael S. Tsirkin return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES, 2046631ab46bSEric W. Biederman (unsigned int __user*)argp); 2047cde8b15fSJason Wang } else if (cmd == TUNSETQUEUE) 2048cde8b15fSJason Wang return tun_set_queue(file, &ifr); 2049631ab46bSEric W. Biederman 2050c8d68e6bSJason Wang ret = 0; 2051876bfd4dSHerbert Xu rtnl_lock(); 2052876bfd4dSHerbert Xu 205336b50babSEric W. Biederman tun = __tun_get(tfile); 20540f16bc13SGao Feng if (cmd == TUNSETIFF) { 20550f16bc13SGao Feng ret = -EEXIST; 20560f16bc13SGao Feng if (tun) 20570f16bc13SGao Feng goto unlock; 20580f16bc13SGao Feng 20591da177e4SLinus Torvalds ifr.ifr_name[IFNAMSIZ-1] = '\0'; 20601da177e4SLinus Torvalds 2061140e807dSEric W. Biederman ret = tun_set_iff(sock_net(&tfile->sk), file, &ifr); 20621da177e4SLinus Torvalds 2063876bfd4dSHerbert Xu if (ret) 2064876bfd4dSHerbert Xu goto unlock; 20651da177e4SLinus Torvalds 206650857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 2067876bfd4dSHerbert Xu ret = -EFAULT; 2068876bfd4dSHerbert Xu goto unlock; 20691da177e4SLinus Torvalds } 2070fb7589a1SPavel Emelyanov if (cmd == TUNSETIFINDEX) { 2071fb7589a1SPavel Emelyanov ret = -EPERM; 2072fb7589a1SPavel Emelyanov if (tun) 2073fb7589a1SPavel Emelyanov goto unlock; 2074fb7589a1SPavel Emelyanov 2075fb7589a1SPavel Emelyanov ret = -EFAULT; 2076fb7589a1SPavel Emelyanov if (copy_from_user(&ifindex, argp, sizeof(ifindex))) 2077fb7589a1SPavel Emelyanov goto unlock; 2078fb7589a1SPavel Emelyanov 2079fb7589a1SPavel Emelyanov ret = 0; 2080fb7589a1SPavel Emelyanov tfile->ifindex = ifindex; 2081fb7589a1SPavel Emelyanov goto unlock; 2082fb7589a1SPavel Emelyanov } 20831da177e4SLinus Torvalds 2084876bfd4dSHerbert Xu ret = -EBADFD; 20851da177e4SLinus Torvalds if (!tun) 2086876bfd4dSHerbert Xu goto unlock; 20871da177e4SLinus Torvalds 20881e588338SJason Wang tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd); 20891da177e4SLinus Torvalds 2090631ab46bSEric W. Biederman ret = 0; 20911da177e4SLinus Torvalds switch (cmd) { 2092e3b99556SMark McLoughlin case TUNGETIFF: 20939ce99cf6SRami Rosen tun_get_iff(current->nsproxy->net_ns, tun, &ifr); 2094e3b99556SMark McLoughlin 20953d407a80SPavel Emelyanov if (tfile->detached) 20963d407a80SPavel Emelyanov ifr.ifr_flags |= IFF_DETACH_QUEUE; 2097849c9b6fSPavel Emelyanov if (!tfile->socket.sk->sk_filter) 2098849c9b6fSPavel Emelyanov ifr.ifr_flags |= IFF_NOFILTER; 20993d407a80SPavel Emelyanov 210050857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 2101631ab46bSEric W. Biederman ret = -EFAULT; 2102e3b99556SMark McLoughlin break; 2103e3b99556SMark McLoughlin 21041da177e4SLinus Torvalds case TUNSETNOCSUM: 21051da177e4SLinus Torvalds /* Disable/Enable checksum */ 21061da177e4SLinus Torvalds 210788255375SMichał Mirosław /* [unimplemented] */ 210888255375SMichał Mirosław tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n", 21096b8a66eeSJoe Perches arg ? "disabled" : "enabled"); 21101da177e4SLinus Torvalds break; 21111da177e4SLinus Torvalds 21121da177e4SLinus Torvalds case TUNSETPERSIST: 211354f968d6SJason Wang /* Disable/Enable persist mode. Keep an extra reference to the 211454f968d6SJason Wang * module to prevent the module being unprobed. 211554f968d6SJason Wang */ 211640630b82SMichael S. Tsirkin if (arg && !(tun->flags & IFF_PERSIST)) { 211740630b82SMichael S. Tsirkin tun->flags |= IFF_PERSIST; 211854f968d6SJason Wang __module_get(THIS_MODULE); 2119dd38bd85SJason Wang } 212040630b82SMichael S. Tsirkin if (!arg && (tun->flags & IFF_PERSIST)) { 212140630b82SMichael S. Tsirkin tun->flags &= ~IFF_PERSIST; 212254f968d6SJason Wang module_put(THIS_MODULE); 212354f968d6SJason Wang } 21241da177e4SLinus Torvalds 21256b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "persist %s\n", 21266b8a66eeSJoe Perches arg ? "enabled" : "disabled"); 21271da177e4SLinus Torvalds break; 21281da177e4SLinus Torvalds 21291da177e4SLinus Torvalds case TUNSETOWNER: 21301da177e4SLinus Torvalds /* Set owner of the device */ 21310625c883SEric W. Biederman owner = make_kuid(current_user_ns(), arg); 21320625c883SEric W. Biederman if (!uid_valid(owner)) { 21330625c883SEric W. Biederman ret = -EINVAL; 21340625c883SEric W. Biederman break; 21350625c883SEric W. Biederman } 21360625c883SEric W. Biederman tun->owner = owner; 21371e588338SJason Wang tun_debug(KERN_INFO, tun, "owner set to %u\n", 21380625c883SEric W. Biederman from_kuid(&init_user_ns, tun->owner)); 21391da177e4SLinus Torvalds break; 21401da177e4SLinus Torvalds 21418c644623SGuido Guenther case TUNSETGROUP: 21428c644623SGuido Guenther /* Set group of the device */ 21430625c883SEric W. Biederman group = make_kgid(current_user_ns(), arg); 21440625c883SEric W. Biederman if (!gid_valid(group)) { 21450625c883SEric W. Biederman ret = -EINVAL; 21460625c883SEric W. Biederman break; 21470625c883SEric W. Biederman } 21480625c883SEric W. Biederman tun->group = group; 21491e588338SJason Wang tun_debug(KERN_INFO, tun, "group set to %u\n", 21500625c883SEric W. Biederman from_kgid(&init_user_ns, tun->group)); 21518c644623SGuido Guenther break; 21528c644623SGuido Guenther 2153ff4cc3acSMike Kershaw case TUNSETLINK: 2154ff4cc3acSMike Kershaw /* Only allow setting the type when the interface is down */ 2155ff4cc3acSMike Kershaw if (tun->dev->flags & IFF_UP) { 21566b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, 21576b8a66eeSJoe Perches "Linktype set failed because interface is up\n"); 215848abfe05SDavid S. Miller ret = -EBUSY; 2159ff4cc3acSMike Kershaw } else { 2160ff4cc3acSMike Kershaw tun->dev->type = (int) arg; 21616b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "linktype set to %d\n", 21626b8a66eeSJoe Perches tun->dev->type); 216348abfe05SDavid S. Miller ret = 0; 2164ff4cc3acSMike Kershaw } 2165631ab46bSEric W. Biederman break; 2166ff4cc3acSMike Kershaw 21671da177e4SLinus Torvalds #ifdef TUN_DEBUG 21681da177e4SLinus Torvalds case TUNSETDEBUG: 21691da177e4SLinus Torvalds tun->debug = arg; 21701da177e4SLinus Torvalds break; 21711da177e4SLinus Torvalds #endif 21725228ddc9SRusty Russell case TUNSETOFFLOAD: 217388255375SMichał Mirosław ret = set_offload(tun, arg); 2174631ab46bSEric W. Biederman break; 21755228ddc9SRusty Russell 2176f271b2ccSMax Krasnyansky case TUNSETTXFILTER: 2177f271b2ccSMax Krasnyansky /* Can be set only for TAPs */ 2178631ab46bSEric W. Biederman ret = -EINVAL; 217940630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 2180631ab46bSEric W. Biederman break; 2181c0e5a8c2SHarvey Harrison ret = update_filter(&tun->txflt, (void __user *)arg); 2182631ab46bSEric W. Biederman break; 21831da177e4SLinus Torvalds 21841da177e4SLinus Torvalds case SIOCGIFHWADDR: 2185b595076aSUwe Kleine-König /* Get hw address */ 2186f271b2ccSMax Krasnyansky memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); 2187f271b2ccSMax Krasnyansky ifr.ifr_hwaddr.sa_family = tun->dev->type; 218850857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 2189631ab46bSEric W. Biederman ret = -EFAULT; 2190631ab46bSEric W. Biederman break; 21911da177e4SLinus Torvalds 21921da177e4SLinus Torvalds case SIOCSIFHWADDR: 2193f271b2ccSMax Krasnyansky /* Set hw address */ 21946b8a66eeSJoe Perches tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n", 21956b8a66eeSJoe Perches ifr.ifr_hwaddr.sa_data); 219640102371SKim B. Heino 219740102371SKim B. Heino ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); 2198631ab46bSEric W. Biederman break; 219933dccbb0SHerbert Xu 220033dccbb0SHerbert Xu case TUNGETSNDBUF: 220154f968d6SJason Wang sndbuf = tfile->socket.sk->sk_sndbuf; 220233dccbb0SHerbert Xu if (copy_to_user(argp, &sndbuf, sizeof(sndbuf))) 220333dccbb0SHerbert Xu ret = -EFAULT; 220433dccbb0SHerbert Xu break; 220533dccbb0SHerbert Xu 220633dccbb0SHerbert Xu case TUNSETSNDBUF: 220733dccbb0SHerbert Xu if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) { 220833dccbb0SHerbert Xu ret = -EFAULT; 220933dccbb0SHerbert Xu break; 221033dccbb0SHerbert Xu } 221133dccbb0SHerbert Xu 2212c8d68e6bSJason Wang tun->sndbuf = sndbuf; 2213c8d68e6bSJason Wang tun_set_sndbuf(tun); 221433dccbb0SHerbert Xu break; 221533dccbb0SHerbert Xu 2216d9d52b51SMichael S. Tsirkin case TUNGETVNETHDRSZ: 2217d9d52b51SMichael S. Tsirkin vnet_hdr_sz = tun->vnet_hdr_sz; 2218d9d52b51SMichael S. Tsirkin if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz))) 2219d9d52b51SMichael S. Tsirkin ret = -EFAULT; 2220d9d52b51SMichael S. Tsirkin break; 2221d9d52b51SMichael S. Tsirkin 2222d9d52b51SMichael S. Tsirkin case TUNSETVNETHDRSZ: 2223d9d52b51SMichael S. Tsirkin if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) { 2224d9d52b51SMichael S. Tsirkin ret = -EFAULT; 2225d9d52b51SMichael S. Tsirkin break; 2226d9d52b51SMichael S. Tsirkin } 2227d9d52b51SMichael S. Tsirkin if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) { 2228d9d52b51SMichael S. Tsirkin ret = -EINVAL; 2229d9d52b51SMichael S. Tsirkin break; 2230d9d52b51SMichael S. Tsirkin } 2231d9d52b51SMichael S. Tsirkin 2232d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = vnet_hdr_sz; 2233d9d52b51SMichael S. Tsirkin break; 2234d9d52b51SMichael S. Tsirkin 22351cf8e410SMichael S. Tsirkin case TUNGETVNETLE: 22361cf8e410SMichael S. Tsirkin le = !!(tun->flags & TUN_VNET_LE); 22371cf8e410SMichael S. Tsirkin if (put_user(le, (int __user *)argp)) 22381cf8e410SMichael S. Tsirkin ret = -EFAULT; 22391cf8e410SMichael S. Tsirkin break; 22401cf8e410SMichael S. Tsirkin 22411cf8e410SMichael S. Tsirkin case TUNSETVNETLE: 22421cf8e410SMichael S. Tsirkin if (get_user(le, (int __user *)argp)) { 22431cf8e410SMichael S. Tsirkin ret = -EFAULT; 22441cf8e410SMichael S. Tsirkin break; 22451cf8e410SMichael S. Tsirkin } 22461cf8e410SMichael S. Tsirkin if (le) 22471cf8e410SMichael S. Tsirkin tun->flags |= TUN_VNET_LE; 22481cf8e410SMichael S. Tsirkin else 22491cf8e410SMichael S. Tsirkin tun->flags &= ~TUN_VNET_LE; 22501cf8e410SMichael S. Tsirkin break; 22511cf8e410SMichael S. Tsirkin 22528b8e658bSGreg Kurz case TUNGETVNETBE: 22538b8e658bSGreg Kurz ret = tun_get_vnet_be(tun, argp); 22548b8e658bSGreg Kurz break; 22558b8e658bSGreg Kurz 22568b8e658bSGreg Kurz case TUNSETVNETBE: 22578b8e658bSGreg Kurz ret = tun_set_vnet_be(tun, argp); 22588b8e658bSGreg Kurz break; 22598b8e658bSGreg Kurz 226099405162SMichael S. Tsirkin case TUNATTACHFILTER: 226199405162SMichael S. Tsirkin /* Can be set only for TAPs */ 226299405162SMichael S. Tsirkin ret = -EINVAL; 226340630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 226499405162SMichael S. Tsirkin break; 226599405162SMichael S. Tsirkin ret = -EFAULT; 226654f968d6SJason Wang if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog))) 226799405162SMichael S. Tsirkin break; 226899405162SMichael S. Tsirkin 2269c8d68e6bSJason Wang ret = tun_attach_filter(tun); 227099405162SMichael S. Tsirkin break; 227199405162SMichael S. Tsirkin 227299405162SMichael S. Tsirkin case TUNDETACHFILTER: 227399405162SMichael S. Tsirkin /* Can be set only for TAPs */ 227499405162SMichael S. Tsirkin ret = -EINVAL; 227540630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 227699405162SMichael S. Tsirkin break; 2277c8d68e6bSJason Wang ret = 0; 2278c8d68e6bSJason Wang tun_detach_filter(tun, tun->numqueues); 227999405162SMichael S. Tsirkin break; 228099405162SMichael S. Tsirkin 228176975e9cSPavel Emelyanov case TUNGETFILTER: 228276975e9cSPavel Emelyanov ret = -EINVAL; 228340630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 228476975e9cSPavel Emelyanov break; 228576975e9cSPavel Emelyanov ret = -EFAULT; 228676975e9cSPavel Emelyanov if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog))) 228776975e9cSPavel Emelyanov break; 228876975e9cSPavel Emelyanov ret = 0; 228976975e9cSPavel Emelyanov break; 229076975e9cSPavel Emelyanov 22911da177e4SLinus Torvalds default: 2292631ab46bSEric W. Biederman ret = -EINVAL; 2293631ab46bSEric W. Biederman break; 2294ee289b64SJoe Perches } 22951da177e4SLinus Torvalds 2296876bfd4dSHerbert Xu unlock: 2297876bfd4dSHerbert Xu rtnl_unlock(); 2298876bfd4dSHerbert Xu if (tun) 2299631ab46bSEric W. Biederman tun_put(tun); 2300631ab46bSEric W. Biederman return ret; 23011da177e4SLinus Torvalds } 23021da177e4SLinus Torvalds 230350857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file, 230450857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 230550857e2aSArnd Bergmann { 230650857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq)); 230750857e2aSArnd Bergmann } 230850857e2aSArnd Bergmann 230950857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 231050857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file, 231150857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 231250857e2aSArnd Bergmann { 231350857e2aSArnd Bergmann switch (cmd) { 231450857e2aSArnd Bergmann case TUNSETIFF: 231550857e2aSArnd Bergmann case TUNGETIFF: 231650857e2aSArnd Bergmann case TUNSETTXFILTER: 231750857e2aSArnd Bergmann case TUNGETSNDBUF: 231850857e2aSArnd Bergmann case TUNSETSNDBUF: 231950857e2aSArnd Bergmann case SIOCGIFHWADDR: 232050857e2aSArnd Bergmann case SIOCSIFHWADDR: 232150857e2aSArnd Bergmann arg = (unsigned long)compat_ptr(arg); 232250857e2aSArnd Bergmann break; 232350857e2aSArnd Bergmann default: 232450857e2aSArnd Bergmann arg = (compat_ulong_t)arg; 232550857e2aSArnd Bergmann break; 232650857e2aSArnd Bergmann } 232750857e2aSArnd Bergmann 232850857e2aSArnd Bergmann /* 232950857e2aSArnd Bergmann * compat_ifreq is shorter than ifreq, so we must not access beyond 233050857e2aSArnd Bergmann * the end of that structure. All fields that are used in this 233150857e2aSArnd Bergmann * driver are compatible though, we don't need to convert the 233250857e2aSArnd Bergmann * contents. 233350857e2aSArnd Bergmann */ 233450857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq)); 233550857e2aSArnd Bergmann } 233650857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */ 233750857e2aSArnd Bergmann 23381da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on) 23391da177e4SLinus Torvalds { 234054f968d6SJason Wang struct tun_file *tfile = file->private_data; 23411da177e4SLinus Torvalds int ret; 23421da177e4SLinus Torvalds 234354f968d6SJason Wang if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) 23449d319522SJonathan Corbet goto out; 23451da177e4SLinus Torvalds 23461da177e4SLinus Torvalds if (on) { 2347e0b93eddSJeff Layton __f_setown(file, task_pid(current), PIDTYPE_PID, 0); 234854f968d6SJason Wang tfile->flags |= TUN_FASYNC; 23491da177e4SLinus Torvalds } else 235054f968d6SJason Wang tfile->flags &= ~TUN_FASYNC; 23519d319522SJonathan Corbet ret = 0; 23529d319522SJonathan Corbet out: 23539d319522SJonathan Corbet return ret; 23541da177e4SLinus Torvalds } 23551da177e4SLinus Torvalds 23561da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file) 23571da177e4SLinus Torvalds { 2358140e807dSEric W. Biederman struct net *net = current->nsproxy->net_ns; 2359631ab46bSEric W. Biederman struct tun_file *tfile; 2360deed49fbSThomas Gleixner 23616b8a66eeSJoe Perches DBG1(KERN_INFO, "tunX: tun_chr_open\n"); 2362631ab46bSEric W. Biederman 2363140e807dSEric W. Biederman tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, 236411aa9c28SEric W. Biederman &tun_proto, 0); 2365631ab46bSEric W. Biederman if (!tfile) 2366631ab46bSEric W. Biederman return -ENOMEM; 2367c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 236854f968d6SJason Wang tfile->flags = 0; 2369fb7589a1SPavel Emelyanov tfile->ifindex = 0; 237054f968d6SJason Wang 237154f968d6SJason Wang init_waitqueue_head(&tfile->wq.wait); 23729e641bdcSXi Wang RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq); 237354f968d6SJason Wang 237454f968d6SJason Wang tfile->socket.file = file; 237554f968d6SJason Wang tfile->socket.ops = &tun_socket_ops; 237654f968d6SJason Wang 237754f968d6SJason Wang sock_init_data(&tfile->socket, &tfile->sk); 237854f968d6SJason Wang 237954f968d6SJason Wang tfile->sk.sk_write_space = tun_sock_write_space; 238054f968d6SJason Wang tfile->sk.sk_sndbuf = INT_MAX; 238154f968d6SJason Wang 2382631ab46bSEric W. Biederman file->private_data = tfile; 23834008e97fSJason Wang INIT_LIST_HEAD(&tfile->next); 238454f968d6SJason Wang 238519a6afb2SJason Wang sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); 238619a6afb2SJason Wang 23871da177e4SLinus Torvalds return 0; 23881da177e4SLinus Torvalds } 23891da177e4SLinus Torvalds 23901da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file) 23911da177e4SLinus Torvalds { 2392631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 23931da177e4SLinus Torvalds 2394c8d68e6bSJason Wang tun_detach(tfile, true); 23951da177e4SLinus Torvalds 23961da177e4SLinus Torvalds return 0; 23971da177e4SLinus Torvalds } 23981da177e4SLinus Torvalds 239993e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS 2400a3816ab0SJoe Perches static void tun_chr_show_fdinfo(struct seq_file *m, struct file *f) 240193e14b6dSMasatake YAMATO { 240293e14b6dSMasatake YAMATO struct tun_struct *tun; 240393e14b6dSMasatake YAMATO struct ifreq ifr; 240493e14b6dSMasatake YAMATO 240593e14b6dSMasatake YAMATO memset(&ifr, 0, sizeof(ifr)); 240693e14b6dSMasatake YAMATO 240793e14b6dSMasatake YAMATO rtnl_lock(); 240893e14b6dSMasatake YAMATO tun = tun_get(f); 240993e14b6dSMasatake YAMATO if (tun) 241093e14b6dSMasatake YAMATO tun_get_iff(current->nsproxy->net_ns, tun, &ifr); 241193e14b6dSMasatake YAMATO rtnl_unlock(); 241293e14b6dSMasatake YAMATO 241393e14b6dSMasatake YAMATO if (tun) 241493e14b6dSMasatake YAMATO tun_put(tun); 241593e14b6dSMasatake YAMATO 2416a3816ab0SJoe Perches seq_printf(m, "iff:\t%s\n", ifr.ifr_name); 241793e14b6dSMasatake YAMATO } 241893e14b6dSMasatake YAMATO #endif 241993e14b6dSMasatake YAMATO 2420d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = { 24211da177e4SLinus Torvalds .owner = THIS_MODULE, 24221da177e4SLinus Torvalds .llseek = no_llseek, 24239b067034SAl Viro .read_iter = tun_chr_read_iter, 2424f5ff53b4SAl Viro .write_iter = tun_chr_write_iter, 24251da177e4SLinus Torvalds .poll = tun_chr_poll, 2426876bfd4dSHerbert Xu .unlocked_ioctl = tun_chr_ioctl, 242750857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 242850857e2aSArnd Bergmann .compat_ioctl = tun_chr_compat_ioctl, 242950857e2aSArnd Bergmann #endif 24301da177e4SLinus Torvalds .open = tun_chr_open, 24311da177e4SLinus Torvalds .release = tun_chr_close, 243293e14b6dSMasatake YAMATO .fasync = tun_chr_fasync, 243393e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS 243493e14b6dSMasatake YAMATO .show_fdinfo = tun_chr_show_fdinfo, 243593e14b6dSMasatake YAMATO #endif 24361da177e4SLinus Torvalds }; 24371da177e4SLinus Torvalds 24381da177e4SLinus Torvalds static struct miscdevice tun_miscdev = { 24391da177e4SLinus Torvalds .minor = TUN_MINOR, 24401da177e4SLinus Torvalds .name = "tun", 2441e454cea2SKay Sievers .nodename = "net/tun", 24421da177e4SLinus Torvalds .fops = &tun_fops, 24431da177e4SLinus Torvalds }; 24441da177e4SLinus Torvalds 24451da177e4SLinus Torvalds /* ethtool interface */ 24461da177e4SLinus Torvalds 244729ccc49dSPhilippe Reynes static int tun_get_link_ksettings(struct net_device *dev, 244829ccc49dSPhilippe Reynes struct ethtool_link_ksettings *cmd) 24491da177e4SLinus Torvalds { 245029ccc49dSPhilippe Reynes ethtool_link_ksettings_zero_link_mode(cmd, supported); 245129ccc49dSPhilippe Reynes ethtool_link_ksettings_zero_link_mode(cmd, advertising); 245229ccc49dSPhilippe Reynes cmd->base.speed = SPEED_10; 245329ccc49dSPhilippe Reynes cmd->base.duplex = DUPLEX_FULL; 245429ccc49dSPhilippe Reynes cmd->base.port = PORT_TP; 245529ccc49dSPhilippe Reynes cmd->base.phy_address = 0; 245629ccc49dSPhilippe Reynes cmd->base.autoneg = AUTONEG_DISABLE; 24571da177e4SLinus Torvalds return 0; 24581da177e4SLinus Torvalds } 24591da177e4SLinus Torvalds 24601da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) 24611da177e4SLinus Torvalds { 24621da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 24631da177e4SLinus Torvalds 246433a5ba14SRick Jones strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); 246533a5ba14SRick Jones strlcpy(info->version, DRV_VERSION, sizeof(info->version)); 24661da177e4SLinus Torvalds 24671da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 246840630b82SMichael S. Tsirkin case IFF_TUN: 246933a5ba14SRick Jones strlcpy(info->bus_info, "tun", sizeof(info->bus_info)); 24701da177e4SLinus Torvalds break; 247140630b82SMichael S. Tsirkin case IFF_TAP: 247233a5ba14SRick Jones strlcpy(info->bus_info, "tap", sizeof(info->bus_info)); 24731da177e4SLinus Torvalds break; 24741da177e4SLinus Torvalds } 24751da177e4SLinus Torvalds } 24761da177e4SLinus Torvalds 24771da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev) 24781da177e4SLinus Torvalds { 24791da177e4SLinus Torvalds #ifdef TUN_DEBUG 24801da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 24811da177e4SLinus Torvalds return tun->debug; 24821da177e4SLinus Torvalds #else 24831da177e4SLinus Torvalds return -EOPNOTSUPP; 24841da177e4SLinus Torvalds #endif 24851da177e4SLinus Torvalds } 24861da177e4SLinus Torvalds 24871da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value) 24881da177e4SLinus Torvalds { 24891da177e4SLinus Torvalds #ifdef TUN_DEBUG 24901da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 24911da177e4SLinus Torvalds tun->debug = value; 24921da177e4SLinus Torvalds #endif 24931da177e4SLinus Torvalds } 24941da177e4SLinus Torvalds 24955503fcecSJason Wang static int tun_get_coalesce(struct net_device *dev, 24965503fcecSJason Wang struct ethtool_coalesce *ec) 24975503fcecSJason Wang { 24985503fcecSJason Wang struct tun_struct *tun = netdev_priv(dev); 24995503fcecSJason Wang 25005503fcecSJason Wang ec->rx_max_coalesced_frames = tun->rx_batched; 25015503fcecSJason Wang 25025503fcecSJason Wang return 0; 25035503fcecSJason Wang } 25045503fcecSJason Wang 25055503fcecSJason Wang static int tun_set_coalesce(struct net_device *dev, 25065503fcecSJason Wang struct ethtool_coalesce *ec) 25075503fcecSJason Wang { 25085503fcecSJason Wang struct tun_struct *tun = netdev_priv(dev); 25095503fcecSJason Wang 25105503fcecSJason Wang if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT) 25115503fcecSJason Wang tun->rx_batched = NAPI_POLL_WEIGHT; 25125503fcecSJason Wang else 25135503fcecSJason Wang tun->rx_batched = ec->rx_max_coalesced_frames; 25145503fcecSJason Wang 25155503fcecSJason Wang return 0; 25165503fcecSJason Wang } 25175503fcecSJason Wang 25187282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = { 25191da177e4SLinus Torvalds .get_drvinfo = tun_get_drvinfo, 25201da177e4SLinus Torvalds .get_msglevel = tun_get_msglevel, 25211da177e4SLinus Torvalds .set_msglevel = tun_set_msglevel, 2522bee31369SNolan Leake .get_link = ethtool_op_get_link, 2523eda29772SRichard Cochran .get_ts_info = ethtool_op_get_ts_info, 25245503fcecSJason Wang .get_coalesce = tun_get_coalesce, 25255503fcecSJason Wang .set_coalesce = tun_set_coalesce, 252629ccc49dSPhilippe Reynes .get_link_ksettings = tun_get_link_ksettings, 25271da177e4SLinus Torvalds }; 25281da177e4SLinus Torvalds 25291576d986SJason Wang static int tun_queue_resize(struct tun_struct *tun) 25301576d986SJason Wang { 25311576d986SJason Wang struct net_device *dev = tun->dev; 25321576d986SJason Wang struct tun_file *tfile; 25331576d986SJason Wang struct skb_array **arrays; 25341576d986SJason Wang int n = tun->numqueues + tun->numdisabled; 25351576d986SJason Wang int ret, i; 25361576d986SJason Wang 25371576d986SJason Wang arrays = kmalloc(sizeof *arrays * n, GFP_KERNEL); 25381576d986SJason Wang if (!arrays) 25391576d986SJason Wang return -ENOMEM; 25401576d986SJason Wang 25411576d986SJason Wang for (i = 0; i < tun->numqueues; i++) { 25421576d986SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 25431576d986SJason Wang arrays[i] = &tfile->tx_array; 25441576d986SJason Wang } 25451576d986SJason Wang list_for_each_entry(tfile, &tun->disabled, next) 25461576d986SJason Wang arrays[i++] = &tfile->tx_array; 25471576d986SJason Wang 25481576d986SJason Wang ret = skb_array_resize_multiple(arrays, n, 25491576d986SJason Wang dev->tx_queue_len, GFP_KERNEL); 25501576d986SJason Wang 25511576d986SJason Wang kfree(arrays); 25521576d986SJason Wang return ret; 25531576d986SJason Wang } 25541576d986SJason Wang 25551576d986SJason Wang static int tun_device_event(struct notifier_block *unused, 25561576d986SJason Wang unsigned long event, void *ptr) 25571576d986SJason Wang { 25581576d986SJason Wang struct net_device *dev = netdev_notifier_info_to_dev(ptr); 25591576d986SJason Wang struct tun_struct *tun = netdev_priv(dev); 25601576d986SJason Wang 256186dfb4acSCraig Gallek if (dev->rtnl_link_ops != &tun_link_ops) 256286dfb4acSCraig Gallek return NOTIFY_DONE; 256386dfb4acSCraig Gallek 25641576d986SJason Wang switch (event) { 25651576d986SJason Wang case NETDEV_CHANGE_TX_QUEUE_LEN: 25661576d986SJason Wang if (tun_queue_resize(tun)) 25671576d986SJason Wang return NOTIFY_BAD; 25681576d986SJason Wang break; 25691576d986SJason Wang default: 25701576d986SJason Wang break; 25711576d986SJason Wang } 25721576d986SJason Wang 25731576d986SJason Wang return NOTIFY_DONE; 25741576d986SJason Wang } 25751576d986SJason Wang 25761576d986SJason Wang static struct notifier_block tun_notifier_block __read_mostly = { 25771576d986SJason Wang .notifier_call = tun_device_event, 25781576d986SJason Wang }; 257979d17604SPavel Emelyanov 25801da177e4SLinus Torvalds static int __init tun_init(void) 25811da177e4SLinus Torvalds { 25821da177e4SLinus Torvalds int ret = 0; 25831da177e4SLinus Torvalds 25846b8a66eeSJoe Perches pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION); 25851da177e4SLinus Torvalds 2586f019a7a5SEric W. Biederman ret = rtnl_link_register(&tun_link_ops); 258779d17604SPavel Emelyanov if (ret) { 25886b8a66eeSJoe Perches pr_err("Can't register link_ops\n"); 2589f019a7a5SEric W. Biederman goto err_linkops; 259079d17604SPavel Emelyanov } 259179d17604SPavel Emelyanov 25921da177e4SLinus Torvalds ret = misc_register(&tun_miscdev); 259379d17604SPavel Emelyanov if (ret) { 25946b8a66eeSJoe Perches pr_err("Can't register misc device %d\n", TUN_MINOR); 259579d17604SPavel Emelyanov goto err_misc; 259679d17604SPavel Emelyanov } 25971576d986SJason Wang 25981576d986SJason Wang register_netdevice_notifier(&tun_notifier_block); 259979d17604SPavel Emelyanov return 0; 260079d17604SPavel Emelyanov err_misc: 2601f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 2602f019a7a5SEric W. Biederman err_linkops: 26031da177e4SLinus Torvalds return ret; 26041da177e4SLinus Torvalds } 26051da177e4SLinus Torvalds 26061da177e4SLinus Torvalds static void tun_cleanup(void) 26071da177e4SLinus Torvalds { 26081da177e4SLinus Torvalds misc_deregister(&tun_miscdev); 2609f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 26101576d986SJason Wang unregister_netdevice_notifier(&tun_notifier_block); 26111da177e4SLinus Torvalds } 26121da177e4SLinus Torvalds 261305c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file. Returns error unless file is 261405c2828cSMichael S. Tsirkin * attached to a device. The returned object works like a packet socket, it 261505c2828cSMichael S. Tsirkin * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for 261605c2828cSMichael S. Tsirkin * holding a reference to the file for as long as the socket is in use. */ 261705c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file) 261805c2828cSMichael S. Tsirkin { 26196e914fc7SJason Wang struct tun_file *tfile; 262005c2828cSMichael S. Tsirkin if (file->f_op != &tun_fops) 262105c2828cSMichael S. Tsirkin return ERR_PTR(-EINVAL); 26226e914fc7SJason Wang tfile = file->private_data; 26236e914fc7SJason Wang if (!tfile) 262405c2828cSMichael S. Tsirkin return ERR_PTR(-EBADFD); 262554f968d6SJason Wang return &tfile->socket; 262605c2828cSMichael S. Tsirkin } 262705c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket); 262805c2828cSMichael S. Tsirkin 26291da177e4SLinus Torvalds module_init(tun_init); 26301da177e4SLinus Torvalds module_exit(tun_cleanup); 26311da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION); 26321da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT); 26331da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 26341da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR); 2635578454ffSKay Sievers MODULE_ALIAS("devname:net/tun"); 2636