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> 47*174cd4b1SIngo 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 { 825c8d68e6bSJason Wang netif_tx_start_all_queues(dev); 8261da177e4SLinus Torvalds return 0; 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds /* Net device close. */ 8301da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev) 8311da177e4SLinus Torvalds { 832c8d68e6bSJason Wang netif_tx_stop_all_queues(dev); 8331da177e4SLinus Torvalds return 0; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds /* Net device start xmit */ 837424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) 8381da177e4SLinus Torvalds { 8391da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 840c8d68e6bSJason Wang int txq = skb->queue_mapping; 8416e914fc7SJason Wang struct tun_file *tfile; 842fa35864eSDominic Curran u32 numqueues = 0; 8431da177e4SLinus Torvalds 8446e914fc7SJason Wang rcu_read_lock(); 845c8d68e6bSJason Wang tfile = rcu_dereference(tun->tfiles[txq]); 846fa35864eSDominic Curran numqueues = ACCESS_ONCE(tun->numqueues); 847c8d68e6bSJason Wang 8481da177e4SLinus Torvalds /* Drop packet if interface is not attached */ 849fa35864eSDominic Curran if (txq >= numqueues) 8501da177e4SLinus Torvalds goto drop; 8511da177e4SLinus Torvalds 8523df97ba8SJason Wang #ifdef CONFIG_RPS 8533df97ba8SJason Wang if (numqueues == 1 && static_key_false(&rps_needed)) { 8549bc88939STom Herbert /* Select queue was not called for the skbuff, so we extract the 8559bc88939STom Herbert * RPS hash and save it into the flow_table here. 8569bc88939STom Herbert */ 8579bc88939STom Herbert __u32 rxhash; 8589bc88939STom Herbert 8599bc88939STom Herbert rxhash = skb_get_hash(skb); 8609bc88939STom Herbert if (rxhash) { 8619bc88939STom Herbert struct tun_flow_entry *e; 8629bc88939STom Herbert e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)], 8639bc88939STom Herbert rxhash); 8649bc88939STom Herbert if (e) 8659bc88939STom Herbert tun_flow_save_rps_rxhash(e, rxhash); 8669bc88939STom Herbert } 8679bc88939STom Herbert } 8683df97ba8SJason Wang #endif 8699bc88939STom Herbert 8706e914fc7SJason Wang tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len); 8716e914fc7SJason Wang 872c8d68e6bSJason Wang BUG_ON(!tfile); 873c8d68e6bSJason Wang 874f271b2ccSMax Krasnyansky /* Drop if the filter does not like it. 875f271b2ccSMax Krasnyansky * This is a noop if the filter is disabled. 876f271b2ccSMax Krasnyansky * Filter can be enabled only for the TAP devices. */ 877f271b2ccSMax Krasnyansky if (!check_filter(&tun->txflt, skb)) 878f271b2ccSMax Krasnyansky goto drop; 879f271b2ccSMax Krasnyansky 88054f968d6SJason Wang if (tfile->socket.sk->sk_filter && 88154f968d6SJason Wang sk_filter(tfile->socket.sk, skb)) 88299405162SMichael S. Tsirkin goto drop; 88399405162SMichael S. Tsirkin 8847bf66305SJason Wang if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC))) 8857bf66305SJason Wang goto drop; 8867bf66305SJason Wang 8877b996243SSoheil Hassas Yeganeh skb_tx_timestamp(skb); 888eda29772SRichard Cochran 8890110d6f2SMichael S. Tsirkin /* Orphan the skb - required as we might hang on to it 8907bf66305SJason Wang * for indefinite time. 8917bf66305SJason Wang */ 8920110d6f2SMichael S. Tsirkin skb_orphan(skb); 8930110d6f2SMichael S. Tsirkin 894f8af75f3SEric Dumazet nf_reset(skb); 895f8af75f3SEric Dumazet 8961576d986SJason Wang if (skb_array_produce(&tfile->tx_array, skb)) 8971576d986SJason Wang goto drop; 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds /* Notify and wake up reader process */ 90054f968d6SJason Wang if (tfile->flags & TUN_FASYNC) 90154f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_IN); 9029e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 9036e914fc7SJason Wang 9046e914fc7SJason Wang rcu_read_unlock(); 9056ed10654SPatrick McHardy return NETDEV_TX_OK; 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds drop: 908608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->tx_dropped); 909149d36f7SMichael S. Tsirkin skb_tx_error(skb); 9101da177e4SLinus Torvalds kfree_skb(skb); 9116e914fc7SJason Wang rcu_read_unlock(); 912baeababbSJason Wang return NET_XMIT_DROP; 9131da177e4SLinus Torvalds } 9141da177e4SLinus Torvalds 915f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev) 9161da177e4SLinus Torvalds { 917f271b2ccSMax Krasnyansky /* 918f271b2ccSMax Krasnyansky * This callback is supposed to deal with mc filter in 919f271b2ccSMax Krasnyansky * _rx_ path and has nothing to do with the _tx_ path. 920f271b2ccSMax Krasnyansky * In rx path we always accept everything userspace gives us. 921f271b2ccSMax Krasnyansky */ 9221da177e4SLinus Torvalds } 9231da177e4SLinus Torvalds 924c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev, 925c8f44affSMichał Mirosław netdev_features_t features) 92688255375SMichał Mirosław { 92788255375SMichał Mirosław struct tun_struct *tun = netdev_priv(dev); 92888255375SMichał Mirosław 92988255375SMichał Mirosław return (features & tun->set_features) | (features & ~TUN_USER_FEATURES); 93088255375SMichał Mirosław } 931bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 932bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev) 933bebd097aSNeil Horman { 934bebd097aSNeil Horman /* 935bebd097aSNeil Horman * Tun only receives frames when: 936bebd097aSNeil Horman * 1) the char device endpoint gets data from user space 937bebd097aSNeil Horman * 2) the tun socket gets a sendmsg call from user space 93892d4ea6eSstephen hemminger * Since both of those are synchronous operations, we are guaranteed 939bebd097aSNeil Horman * never to have pending data when we poll for it 94092d4ea6eSstephen hemminger * so there is nothing to do here but return. 941bebd097aSNeil Horman * We need this though so netpoll recognizes us as an interface that 942bebd097aSNeil Horman * supports polling, which enables bridge devices in virt setups to 943bebd097aSNeil Horman * still use netconsole 944bebd097aSNeil Horman */ 945bebd097aSNeil Horman return; 946bebd097aSNeil Horman } 947bebd097aSNeil Horman #endif 948eaea34b2SPaolo Abeni 949eaea34b2SPaolo Abeni static void tun_set_headroom(struct net_device *dev, int new_hr) 950eaea34b2SPaolo Abeni { 951eaea34b2SPaolo Abeni struct tun_struct *tun = netdev_priv(dev); 952eaea34b2SPaolo Abeni 953eaea34b2SPaolo Abeni if (new_hr < NET_SKB_PAD) 954eaea34b2SPaolo Abeni new_hr = NET_SKB_PAD; 955eaea34b2SPaolo Abeni 956eaea34b2SPaolo Abeni tun->align = new_hr; 957eaea34b2SPaolo Abeni } 958eaea34b2SPaolo Abeni 959bc1f4470Sstephen hemminger static void 960608b9977SPaolo Abeni tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 961608b9977SPaolo Abeni { 962608b9977SPaolo Abeni u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0; 963608b9977SPaolo Abeni struct tun_struct *tun = netdev_priv(dev); 964608b9977SPaolo Abeni struct tun_pcpu_stats *p; 965608b9977SPaolo Abeni int i; 966608b9977SPaolo Abeni 967608b9977SPaolo Abeni for_each_possible_cpu(i) { 968608b9977SPaolo Abeni u64 rxpackets, rxbytes, txpackets, txbytes; 969608b9977SPaolo Abeni unsigned int start; 970608b9977SPaolo Abeni 971608b9977SPaolo Abeni p = per_cpu_ptr(tun->pcpu_stats, i); 972608b9977SPaolo Abeni do { 973608b9977SPaolo Abeni start = u64_stats_fetch_begin(&p->syncp); 974608b9977SPaolo Abeni rxpackets = p->rx_packets; 975608b9977SPaolo Abeni rxbytes = p->rx_bytes; 976608b9977SPaolo Abeni txpackets = p->tx_packets; 977608b9977SPaolo Abeni txbytes = p->tx_bytes; 978608b9977SPaolo Abeni } while (u64_stats_fetch_retry(&p->syncp, start)); 979608b9977SPaolo Abeni 980608b9977SPaolo Abeni stats->rx_packets += rxpackets; 981608b9977SPaolo Abeni stats->rx_bytes += rxbytes; 982608b9977SPaolo Abeni stats->tx_packets += txpackets; 983608b9977SPaolo Abeni stats->tx_bytes += txbytes; 984608b9977SPaolo Abeni 985608b9977SPaolo Abeni /* u32 counters */ 986608b9977SPaolo Abeni rx_dropped += p->rx_dropped; 987608b9977SPaolo Abeni rx_frame_errors += p->rx_frame_errors; 988608b9977SPaolo Abeni tx_dropped += p->tx_dropped; 989608b9977SPaolo Abeni } 990608b9977SPaolo Abeni stats->rx_dropped = rx_dropped; 991608b9977SPaolo Abeni stats->rx_frame_errors = rx_frame_errors; 992608b9977SPaolo Abeni stats->tx_dropped = tx_dropped; 993608b9977SPaolo Abeni } 994608b9977SPaolo Abeni 995758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = { 996c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 997758e43b7SStephen Hemminger .ndo_open = tun_net_open, 998758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 99900829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 100088255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 1001c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 1002bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 1003bebd097aSNeil Horman .ndo_poll_controller = tun_poll_controller, 1004bebd097aSNeil Horman #endif 1005eaea34b2SPaolo Abeni .ndo_set_rx_headroom = tun_set_headroom, 1006608b9977SPaolo Abeni .ndo_get_stats64 = tun_net_get_stats64, 1007758e43b7SStephen Hemminger }; 1008758e43b7SStephen Hemminger 1009758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = { 1010c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 1011758e43b7SStephen Hemminger .ndo_open = tun_net_open, 1012758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 101300829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 101488255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 1015afc4b13dSJiri Pirko .ndo_set_rx_mode = tun_net_mclist, 1016758e43b7SStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 1017758e43b7SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 1018c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 1019bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 1020bebd097aSNeil Horman .ndo_poll_controller = tun_poll_controller, 1021bebd097aSNeil Horman #endif 10225e52796aSToshiaki Makita .ndo_features_check = passthru_features_check, 1023eaea34b2SPaolo Abeni .ndo_set_rx_headroom = tun_set_headroom, 1024608b9977SPaolo Abeni .ndo_get_stats64 = tun_net_get_stats64, 1025758e43b7SStephen Hemminger }; 1026758e43b7SStephen Hemminger 1027944a1376SPavel Emelyanov static void tun_flow_init(struct tun_struct *tun) 102896442e42SJason Wang { 102996442e42SJason Wang int i; 103096442e42SJason Wang 103196442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) 103296442e42SJason Wang INIT_HLIST_HEAD(&tun->flows[i]); 103396442e42SJason Wang 103496442e42SJason Wang tun->ageing_time = TUN_FLOW_EXPIRE; 103596442e42SJason Wang setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun); 103696442e42SJason Wang mod_timer(&tun->flow_gc_timer, 103796442e42SJason Wang round_jiffies_up(jiffies + tun->ageing_time)); 103896442e42SJason Wang } 103996442e42SJason Wang 104096442e42SJason Wang static void tun_flow_uninit(struct tun_struct *tun) 104196442e42SJason Wang { 104296442e42SJason Wang del_timer_sync(&tun->flow_gc_timer); 104396442e42SJason Wang tun_flow_flush(tun); 104496442e42SJason Wang } 104596442e42SJason Wang 104691572088SJarod Wilson #define MIN_MTU 68 104791572088SJarod Wilson #define MAX_MTU 65535 104891572088SJarod Wilson 10491da177e4SLinus Torvalds /* Initialize net device. */ 10501da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev) 10511da177e4SLinus Torvalds { 10521da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 105540630b82SMichael S. Tsirkin case IFF_TUN: 1056758e43b7SStephen Hemminger dev->netdev_ops = &tun_netdev_ops; 1057758e43b7SStephen Hemminger 10581da177e4SLinus Torvalds /* Point-to-Point TUN Device */ 10591da177e4SLinus Torvalds dev->hard_header_len = 0; 10601da177e4SLinus Torvalds dev->addr_len = 0; 10611da177e4SLinus Torvalds dev->mtu = 1500; 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds /* Zero header length */ 10641da177e4SLinus Torvalds dev->type = ARPHRD_NONE; 10651da177e4SLinus Torvalds dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 10661da177e4SLinus Torvalds break; 10671da177e4SLinus Torvalds 106840630b82SMichael S. Tsirkin case IFF_TAP: 10697a0a9608SKusanagi Kouichi dev->netdev_ops = &tap_netdev_ops; 10701da177e4SLinus Torvalds /* Ethernet TAP Device */ 10711da177e4SLinus Torvalds ether_setup(dev); 1072550fd08cSNeil Horman dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1073a676847bSstephen hemminger dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 107436226a8dSBrian Braunstein 1075f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 107636226a8dSBrian Braunstein 10771da177e4SLinus Torvalds break; 10781da177e4SLinus Torvalds } 107991572088SJarod Wilson 108091572088SJarod Wilson dev->min_mtu = MIN_MTU; 108191572088SJarod Wilson dev->max_mtu = MAX_MTU - dev->hard_header_len; 10821da177e4SLinus Torvalds } 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds /* Character device part */ 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds /* Poll */ 10871da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table *wait) 10881da177e4SLinus Torvalds { 1089b2430de3SEric W. Biederman struct tun_file *tfile = file->private_data; 1090b2430de3SEric W. Biederman struct tun_struct *tun = __tun_get(tfile); 10913c8a9c63SMariusz Kozlowski struct sock *sk; 109233dccbb0SHerbert Xu unsigned int mask = 0; 10931da177e4SLinus Torvalds 10941da177e4SLinus Torvalds if (!tun) 1095eac9e902SEric W. Biederman return POLLERR; 10961da177e4SLinus Torvalds 109754f968d6SJason Wang sk = tfile->socket.sk; 10983c8a9c63SMariusz Kozlowski 10996b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_chr_poll\n"); 11001da177e4SLinus Torvalds 11019e641bdcSXi Wang poll_wait(file, sk_sleep(sk), wait); 11021da177e4SLinus Torvalds 11031576d986SJason Wang if (!skb_array_empty(&tfile->tx_array)) 11041da177e4SLinus Torvalds mask |= POLLIN | POLLRDNORM; 11051da177e4SLinus Torvalds 110633dccbb0SHerbert Xu if (sock_writeable(sk) || 11079cd3e072SEric Dumazet (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) && 110833dccbb0SHerbert Xu sock_writeable(sk))) 110933dccbb0SHerbert Xu mask |= POLLOUT | POLLWRNORM; 111033dccbb0SHerbert Xu 1111c70f1829SEric W. Biederman if (tun->dev->reg_state != NETREG_REGISTERED) 1112c70f1829SEric W. Biederman mask = POLLERR; 1113c70f1829SEric W. Biederman 1114631ab46bSEric W. Biederman tun_put(tun); 11151da177e4SLinus Torvalds return mask; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds 1118f42157cbSRusty Russell /* prepad is the amount to reserve at front. len is length after that. 1119f42157cbSRusty Russell * linear is a hint as to how much to copy (usually headers). */ 112054f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, 112133dccbb0SHerbert Xu size_t prepad, size_t len, 112233dccbb0SHerbert Xu size_t linear, int noblock) 1123f42157cbSRusty Russell { 112454f968d6SJason Wang struct sock *sk = tfile->socket.sk; 1125f42157cbSRusty Russell struct sk_buff *skb; 112633dccbb0SHerbert Xu int err; 1127f42157cbSRusty Russell 1128f42157cbSRusty Russell /* Under a page? Don't bother with paged skb. */ 11290eca93bcSHerbert Xu if (prepad + len < PAGE_SIZE || !linear) 113033dccbb0SHerbert Xu linear = len; 1131f42157cbSRusty Russell 113233dccbb0SHerbert Xu skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, 113328d64271SEric Dumazet &err, 0); 1134f42157cbSRusty Russell if (!skb) 113533dccbb0SHerbert Xu return ERR_PTR(err); 1136f42157cbSRusty Russell 1137f42157cbSRusty Russell skb_reserve(skb, prepad); 1138f42157cbSRusty Russell skb_put(skb, linear); 113933dccbb0SHerbert Xu skb->data_len = len - linear; 114033dccbb0SHerbert Xu skb->len += len - linear; 1141f42157cbSRusty Russell 1142f42157cbSRusty Russell return skb; 1143f42157cbSRusty Russell } 1144f42157cbSRusty Russell 11455503fcecSJason Wang static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile, 11465503fcecSJason Wang struct sk_buff *skb, int more) 11475503fcecSJason Wang { 11485503fcecSJason Wang struct sk_buff_head *queue = &tfile->sk.sk_write_queue; 11495503fcecSJason Wang struct sk_buff_head process_queue; 11505503fcecSJason Wang u32 rx_batched = tun->rx_batched; 11515503fcecSJason Wang bool rcv = false; 11525503fcecSJason Wang 11535503fcecSJason Wang if (!rx_batched || (!more && skb_queue_empty(queue))) { 11545503fcecSJason Wang local_bh_disable(); 11555503fcecSJason Wang netif_receive_skb(skb); 11565503fcecSJason Wang local_bh_enable(); 11575503fcecSJason Wang return; 11585503fcecSJason Wang } 11595503fcecSJason Wang 11605503fcecSJason Wang spin_lock(&queue->lock); 11615503fcecSJason Wang if (!more || skb_queue_len(queue) == rx_batched) { 11625503fcecSJason Wang __skb_queue_head_init(&process_queue); 11635503fcecSJason Wang skb_queue_splice_tail_init(queue, &process_queue); 11645503fcecSJason Wang rcv = true; 11655503fcecSJason Wang } else { 11665503fcecSJason Wang __skb_queue_tail(queue, skb); 11675503fcecSJason Wang } 11685503fcecSJason Wang spin_unlock(&queue->lock); 11695503fcecSJason Wang 11705503fcecSJason Wang if (rcv) { 11715503fcecSJason Wang struct sk_buff *nskb; 11725503fcecSJason Wang 11735503fcecSJason Wang local_bh_disable(); 11745503fcecSJason Wang while ((nskb = __skb_dequeue(&process_queue))) 11755503fcecSJason Wang netif_receive_skb(nskb); 11765503fcecSJason Wang netif_receive_skb(skb); 11775503fcecSJason Wang local_bh_enable(); 11785503fcecSJason Wang } 11795503fcecSJason Wang } 11805503fcecSJason Wang 11811da177e4SLinus Torvalds /* Get packet from user space buffer */ 118254f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, 1183f5ff53b4SAl Viro void *msg_control, struct iov_iter *from, 11845503fcecSJason Wang int noblock, bool more) 11851da177e4SLinus Torvalds { 118609640e63SHarvey Harrison struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; 11871da177e4SLinus Torvalds struct sk_buff *skb; 1188f5ff53b4SAl Viro size_t total_len = iov_iter_count(from); 1189eaea34b2SPaolo Abeni size_t len = total_len, align = tun->align, linear; 1190f43798c2SRusty Russell struct virtio_net_hdr gso = { 0 }; 1191608b9977SPaolo Abeni struct tun_pcpu_stats *stats; 119296f8d9ecSJason Wang int good_linear; 11930690899bSMichael S. Tsirkin int copylen; 11940690899bSMichael S. Tsirkin bool zerocopy = false; 11950690899bSMichael S. Tsirkin int err; 119649974420SEric Dumazet u32 rxhash; 11971da177e4SLinus Torvalds 11981bd4978aSEric Dumazet if (!(tun->dev->flags & IFF_UP)) 11991bd4978aSEric Dumazet return -EIO; 12001bd4978aSEric Dumazet 120140630b82SMichael S. Tsirkin if (!(tun->flags & IFF_NO_PI)) { 120215718ea0SDan Carpenter if (len < sizeof(pi)) 12031da177e4SLinus Torvalds return -EINVAL; 120415718ea0SDan Carpenter len -= sizeof(pi); 12051da177e4SLinus Torvalds 1206cbbd26b8SAl Viro if (!copy_from_iter_full(&pi, sizeof(pi), from)) 12071da177e4SLinus Torvalds return -EFAULT; 12081da177e4SLinus Torvalds } 12091da177e4SLinus Torvalds 121040630b82SMichael S. Tsirkin if (tun->flags & IFF_VNET_HDR) { 1211e1edab87SWillem de Bruijn int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); 1212e1edab87SWillem de Bruijn 1213e1edab87SWillem de Bruijn if (len < vnet_hdr_sz) 1214f43798c2SRusty Russell return -EINVAL; 1215e1edab87SWillem de Bruijn len -= vnet_hdr_sz; 1216f43798c2SRusty Russell 1217cbbd26b8SAl Viro if (!copy_from_iter_full(&gso, sizeof(gso), from)) 1218f43798c2SRusty Russell return -EFAULT; 1219f43798c2SRusty Russell 12204909122fSHerbert Xu if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && 122156f0dcc5SMichael S. Tsirkin tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len)) 122256f0dcc5SMichael S. Tsirkin gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2); 12234909122fSHerbert Xu 122456f0dcc5SMichael S. Tsirkin if (tun16_to_cpu(tun, gso.hdr_len) > len) 1225f43798c2SRusty Russell return -EINVAL; 1226e1edab87SWillem de Bruijn iov_iter_advance(from, vnet_hdr_sz - sizeof(gso)); 1227f43798c2SRusty Russell } 1228f43798c2SRusty Russell 122940630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) { 1230a504b86eSstephen hemminger align += NET_IP_ALIGN; 12310eca93bcSHerbert Xu if (unlikely(len < ETH_HLEN || 123256f0dcc5SMichael S. Tsirkin (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN))) 1233e01bf1c8SRusty Russell return -EINVAL; 1234e01bf1c8SRusty Russell } 12351da177e4SLinus Torvalds 123696f8d9ecSJason Wang good_linear = SKB_MAX_HEAD(align); 123796f8d9ecSJason Wang 123888529176SJason Wang if (msg_control) { 1239f5ff53b4SAl Viro struct iov_iter i = *from; 1240f5ff53b4SAl Viro 124188529176SJason Wang /* There are 256 bytes to be copied in skb, so there is 124288529176SJason Wang * enough room for skb expand head in case it is used. 12430690899bSMichael S. Tsirkin * The rest of the buffer is mapped from userspace. 12440690899bSMichael S. Tsirkin */ 124556f0dcc5SMichael S. Tsirkin copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN; 124696f8d9ecSJason Wang if (copylen > good_linear) 124796f8d9ecSJason Wang copylen = good_linear; 12483dd5c330SJason Wang linear = copylen; 1249f5ff53b4SAl Viro iov_iter_advance(&i, copylen); 1250f5ff53b4SAl Viro if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS) 125188529176SJason Wang zerocopy = true; 125288529176SJason Wang } 125388529176SJason Wang 125488529176SJason Wang if (!zerocopy) { 12550690899bSMichael S. Tsirkin copylen = len; 125656f0dcc5SMichael S. Tsirkin if (tun16_to_cpu(tun, gso.hdr_len) > good_linear) 125796f8d9ecSJason Wang linear = good_linear; 125896f8d9ecSJason Wang else 125956f0dcc5SMichael S. Tsirkin linear = tun16_to_cpu(tun, gso.hdr_len); 12603dd5c330SJason Wang } 12610690899bSMichael S. Tsirkin 12623dd5c330SJason Wang skb = tun_alloc_skb(tfile, align, copylen, linear, noblock); 126333dccbb0SHerbert Xu if (IS_ERR(skb)) { 126433dccbb0SHerbert Xu if (PTR_ERR(skb) != -EAGAIN) 1265608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 126633dccbb0SHerbert Xu return PTR_ERR(skb); 12671da177e4SLinus Torvalds } 12681da177e4SLinus Torvalds 12690690899bSMichael S. Tsirkin if (zerocopy) 1270f5ff53b4SAl Viro err = zerocopy_sg_from_iter(skb, from); 1271af1cc7a2SJason Wang else 1272f5ff53b4SAl Viro err = skb_copy_datagram_from_iter(skb, 0, from, len); 12730690899bSMichael S. Tsirkin 12740690899bSMichael S. Tsirkin if (err) { 1275608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 12768f22757eSDave Jones kfree_skb(skb); 12771da177e4SLinus Torvalds return -EFAULT; 12788f22757eSDave Jones } 12791da177e4SLinus Torvalds 12803e9e40e7SJarno Rajahalme if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) { 1281df10db98SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_frame_errors); 1282df10db98SPaolo Abeni kfree_skb(skb); 1283df10db98SPaolo Abeni return -EINVAL; 1284df10db98SPaolo Abeni } 1285df10db98SPaolo Abeni 12861da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 128740630b82SMichael S. Tsirkin case IFF_TUN: 128840630b82SMichael S. Tsirkin if (tun->flags & IFF_NO_PI) { 1289f09f7ee2SAng Way Chuang switch (skb->data[0] & 0xf0) { 1290f09f7ee2SAng Way Chuang case 0x40: 1291f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IP); 1292f09f7ee2SAng Way Chuang break; 1293f09f7ee2SAng Way Chuang case 0x60: 1294f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IPV6); 1295f09f7ee2SAng Way Chuang break; 1296f09f7ee2SAng Way Chuang default: 1297608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 1298f09f7ee2SAng Way Chuang kfree_skb(skb); 1299f09f7ee2SAng Way Chuang return -EINVAL; 1300f09f7ee2SAng Way Chuang } 1301f09f7ee2SAng Way Chuang } 1302f09f7ee2SAng Way Chuang 1303459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 13041da177e4SLinus Torvalds skb->protocol = pi.proto; 13054c13eb66SArnaldo Carvalho de Melo skb->dev = tun->dev; 13061da177e4SLinus Torvalds break; 130740630b82SMichael S. Tsirkin case IFF_TAP: 13081da177e4SLinus Torvalds skb->protocol = eth_type_trans(skb, tun->dev); 13091da177e4SLinus Torvalds break; 13106403eab1SJoe Perches } 13111da177e4SLinus Torvalds 13120690899bSMichael S. Tsirkin /* copy skb_ubuf_info for callback when skb has no error */ 13130690899bSMichael S. Tsirkin if (zerocopy) { 13140690899bSMichael S. Tsirkin skb_shinfo(skb)->destructor_arg = msg_control; 13150690899bSMichael S. Tsirkin skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; 1316c9af6db4SPravin B Shelar skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; 1317af1cc7a2SJason Wang } else if (msg_control) { 1318af1cc7a2SJason Wang struct ubuf_info *uarg = msg_control; 1319af1cc7a2SJason Wang uarg->callback(uarg, false); 13200690899bSMichael S. Tsirkin } 13210690899bSMichael S. Tsirkin 132272f65107SVlad Yasevich skb_reset_network_header(skb); 132340893fd0SJason Wang skb_probe_transport_header(skb, 0); 132438502af7SJason Wang 13253958afa1STom Herbert rxhash = skb_get_hash(skb); 1326d4aea20dSAndrey Konovalov #ifndef CONFIG_4KSTACKS 13275503fcecSJason Wang tun_rx_batched(tun, tfile, skb, more); 1328d4aea20dSAndrey Konovalov #else 13291da177e4SLinus Torvalds netif_rx_ni(skb); 1330d4aea20dSAndrey Konovalov #endif 13311da177e4SLinus Torvalds 1332608b9977SPaolo Abeni stats = get_cpu_ptr(tun->pcpu_stats); 1333608b9977SPaolo Abeni u64_stats_update_begin(&stats->syncp); 1334608b9977SPaolo Abeni stats->rx_packets++; 1335608b9977SPaolo Abeni stats->rx_bytes += len; 1336608b9977SPaolo Abeni u64_stats_update_end(&stats->syncp); 1337608b9977SPaolo Abeni put_cpu_ptr(stats); 13381da177e4SLinus Torvalds 13399e85722dSJason Wang tun_flow_update(tun, rxhash, tfile); 13400690899bSMichael S. Tsirkin return total_len; 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds 1343f5ff53b4SAl Viro static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from) 13441da177e4SLinus Torvalds { 134533dccbb0SHerbert Xu struct file *file = iocb->ki_filp; 1346ab46d779SHerbert Xu struct tun_struct *tun = tun_get(file); 134754f968d6SJason Wang struct tun_file *tfile = file->private_data; 1348631ab46bSEric W. Biederman ssize_t result; 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds if (!tun) 13511da177e4SLinus Torvalds return -EBADFD; 13521da177e4SLinus Torvalds 13535503fcecSJason Wang result = tun_get_user(tun, tfile, NULL, from, 13545503fcecSJason Wang file->f_flags & O_NONBLOCK, false); 1355631ab46bSEric W. Biederman 1356631ab46bSEric W. Biederman tun_put(tun); 1357631ab46bSEric W. Biederman return result; 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds /* Put packet to the user space buffer */ 13616f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun, 136254f968d6SJason Wang struct tun_file *tfile, 13631da177e4SLinus Torvalds struct sk_buff *skb, 1364e0b46d0eSHerbert Xu struct iov_iter *iter) 13651da177e4SLinus Torvalds { 13661da177e4SLinus Torvalds struct tun_pi pi = { 0, skb->protocol }; 1367608b9977SPaolo Abeni struct tun_pcpu_stats *stats; 1368e0b46d0eSHerbert Xu ssize_t total; 13698c847d25SJason Wang int vlan_offset = 0; 1370a8f9bfdfSHerbert Xu int vlan_hlen = 0; 13712eb783c4SHerbert Xu int vnet_hdr_sz = 0; 1372a8f9bfdfSHerbert Xu 1373df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) 1374a8f9bfdfSHerbert Xu vlan_hlen = VLAN_HLEN; 13751da177e4SLinus Torvalds 137640630b82SMichael S. Tsirkin if (tun->flags & IFF_VNET_HDR) 1377e1edab87SWillem de Bruijn vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); 13781da177e4SLinus Torvalds 1379e0b46d0eSHerbert Xu total = skb->len + vlan_hlen + vnet_hdr_sz; 1380e0b46d0eSHerbert Xu 138140630b82SMichael S. Tsirkin if (!(tun->flags & IFF_NO_PI)) { 1382e0b46d0eSHerbert Xu if (iov_iter_count(iter) < sizeof(pi)) 13831da177e4SLinus Torvalds return -EINVAL; 13841da177e4SLinus Torvalds 1385e0b46d0eSHerbert Xu total += sizeof(pi); 1386e0b46d0eSHerbert Xu if (iov_iter_count(iter) < total) { 13871da177e4SLinus Torvalds /* Packet will be striped */ 13881da177e4SLinus Torvalds pi.flags |= TUN_PKT_STRIP; 13891da177e4SLinus Torvalds } 13901da177e4SLinus Torvalds 1391e0b46d0eSHerbert Xu if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi)) 13921da177e4SLinus Torvalds return -EFAULT; 13931da177e4SLinus Torvalds } 13941da177e4SLinus Torvalds 13952eb783c4SHerbert Xu if (vnet_hdr_sz) { 13969403cd7cSJarno Rajahalme struct virtio_net_hdr gso; 139734166093SMike Rapoport 1398e0b46d0eSHerbert Xu if (iov_iter_count(iter) < vnet_hdr_sz) 1399f43798c2SRusty Russell return -EINVAL; 1400f43798c2SRusty Russell 14013e9e40e7SJarno Rajahalme if (virtio_net_hdr_from_skb(skb, &gso, 14026391a448SJason Wang tun_is_little_endian(tun), true)) { 1403f43798c2SRusty Russell struct skb_shared_info *sinfo = skb_shinfo(skb); 14046b8a66eeSJoe Perches pr_err("unexpected GSO type: " 1405ef3db4a5SMichael S. Tsirkin "0x%x, gso_size %d, hdr_len %d\n", 140656f0dcc5SMichael S. Tsirkin sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size), 140756f0dcc5SMichael S. Tsirkin tun16_to_cpu(tun, gso.hdr_len)); 1408ef3db4a5SMichael S. Tsirkin print_hex_dump(KERN_ERR, "tun: ", 1409ef3db4a5SMichael S. Tsirkin DUMP_PREFIX_NONE, 1410ef3db4a5SMichael S. Tsirkin 16, 1, skb->head, 141156f0dcc5SMichael S. Tsirkin min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true); 1412ef3db4a5SMichael S. Tsirkin WARN_ON_ONCE(1); 1413ef3db4a5SMichael S. Tsirkin return -EINVAL; 1414ef3db4a5SMichael S. Tsirkin } 1415f43798c2SRusty Russell 1416e0b46d0eSHerbert Xu if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso)) 1417f43798c2SRusty Russell return -EFAULT; 14188c847d25SJason Wang 14198c847d25SJason Wang iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso)); 1420f43798c2SRusty Russell } 1421f43798c2SRusty Russell 1422a8f9bfdfSHerbert Xu if (vlan_hlen) { 1423e0b46d0eSHerbert Xu int ret; 14246680ec68SJason Wang struct { 14256680ec68SJason Wang __be16 h_vlan_proto; 14266680ec68SJason Wang __be16 h_vlan_TCI; 14276680ec68SJason Wang } veth; 14281da177e4SLinus Torvalds 14296680ec68SJason Wang veth.h_vlan_proto = skb->vlan_proto; 1430df8a39deSJiri Pirko veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb)); 14311da177e4SLinus Torvalds 14326680ec68SJason Wang vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); 14336680ec68SJason Wang 1434e0b46d0eSHerbert Xu ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset); 1435e0b46d0eSHerbert Xu if (ret || !iov_iter_count(iter)) 14366680ec68SJason Wang goto done; 14376680ec68SJason Wang 1438e0b46d0eSHerbert Xu ret = copy_to_iter(&veth, sizeof(veth), iter); 1439e0b46d0eSHerbert Xu if (ret != sizeof(veth) || !iov_iter_count(iter)) 14406680ec68SJason Wang goto done; 14416680ec68SJason Wang } 14426680ec68SJason Wang 1443e0b46d0eSHerbert Xu skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset); 14446680ec68SJason Wang 14456680ec68SJason Wang done: 1446608b9977SPaolo Abeni /* caller is in process context, */ 1447608b9977SPaolo Abeni stats = get_cpu_ptr(tun->pcpu_stats); 1448608b9977SPaolo Abeni u64_stats_update_begin(&stats->syncp); 1449608b9977SPaolo Abeni stats->tx_packets++; 1450608b9977SPaolo Abeni stats->tx_bytes += skb->len + vlan_hlen; 1451608b9977SPaolo Abeni u64_stats_update_end(&stats->syncp); 1452608b9977SPaolo Abeni put_cpu_ptr(tun->pcpu_stats); 14531da177e4SLinus Torvalds 14541da177e4SLinus Torvalds return total; 14551da177e4SLinus Torvalds } 14561da177e4SLinus Torvalds 14571576d986SJason Wang static struct sk_buff *tun_ring_recv(struct tun_file *tfile, int noblock, 14581576d986SJason Wang int *err) 14591576d986SJason Wang { 14601576d986SJason Wang DECLARE_WAITQUEUE(wait, current); 14611576d986SJason Wang struct sk_buff *skb = NULL; 1462f48cc6b2SJason Wang int error = 0; 14631576d986SJason Wang 14641576d986SJason Wang skb = skb_array_consume(&tfile->tx_array); 14651576d986SJason Wang if (skb) 14661576d986SJason Wang goto out; 14671576d986SJason Wang if (noblock) { 1468f48cc6b2SJason Wang error = -EAGAIN; 14691576d986SJason Wang goto out; 14701576d986SJason Wang } 14711576d986SJason Wang 14721576d986SJason Wang add_wait_queue(&tfile->wq.wait, &wait); 14731576d986SJason Wang current->state = TASK_INTERRUPTIBLE; 14741576d986SJason Wang 14751576d986SJason Wang while (1) { 14761576d986SJason Wang skb = skb_array_consume(&tfile->tx_array); 14771576d986SJason Wang if (skb) 14781576d986SJason Wang break; 14791576d986SJason Wang if (signal_pending(current)) { 1480f48cc6b2SJason Wang error = -ERESTARTSYS; 14811576d986SJason Wang break; 14821576d986SJason Wang } 14831576d986SJason Wang if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) { 1484f48cc6b2SJason Wang error = -EFAULT; 14851576d986SJason Wang break; 14861576d986SJason Wang } 14871576d986SJason Wang 14881576d986SJason Wang schedule(); 14891576d986SJason Wang } 14901576d986SJason Wang 14911576d986SJason Wang current->state = TASK_RUNNING; 14921576d986SJason Wang remove_wait_queue(&tfile->wq.wait, &wait); 14931576d986SJason Wang 14941576d986SJason Wang out: 1495f48cc6b2SJason Wang *err = error; 14961576d986SJason Wang return skb; 14971576d986SJason Wang } 14981576d986SJason Wang 149954f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, 15009b067034SAl Viro struct iov_iter *to, 15019b067034SAl Viro int noblock) 15021da177e4SLinus Torvalds { 15031da177e4SLinus Torvalds struct sk_buff *skb; 15049b067034SAl Viro ssize_t ret; 15051576d986SJason Wang int err; 15061da177e4SLinus Torvalds 15073872baf6SRami Rosen tun_debug(KERN_INFO, tun, "tun_do_read\n"); 15081da177e4SLinus Torvalds 15099b067034SAl Viro if (!iov_iter_count(to)) 15109b067034SAl Viro return 0; 15111da177e4SLinus Torvalds 15121576d986SJason Wang /* Read frames from ring */ 15131576d986SJason Wang skb = tun_ring_recv(tfile, noblock, &err); 1514e0b46d0eSHerbert Xu if (!skb) 1515957f094fSAlex Gartrell return err; 1516e0b46d0eSHerbert Xu 15179b067034SAl Viro ret = tun_put_user(tun, tfile, skb, to); 1518f51a5e82SJason Wang if (unlikely(ret < 0)) 15191da177e4SLinus Torvalds kfree_skb(skb); 1520f51a5e82SJason Wang else 1521f51a5e82SJason Wang consume_skb(skb); 15221da177e4SLinus Torvalds 152305c2828cSMichael S. Tsirkin return ret; 152405c2828cSMichael S. Tsirkin } 152505c2828cSMichael S. Tsirkin 15269b067034SAl Viro static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to) 152705c2828cSMichael S. Tsirkin { 152805c2828cSMichael S. Tsirkin struct file *file = iocb->ki_filp; 152905c2828cSMichael S. Tsirkin struct tun_file *tfile = file->private_data; 153005c2828cSMichael S. Tsirkin struct tun_struct *tun = __tun_get(tfile); 15319b067034SAl Viro ssize_t len = iov_iter_count(to), ret; 153205c2828cSMichael S. Tsirkin 153305c2828cSMichael S. Tsirkin if (!tun) 153405c2828cSMichael S. Tsirkin return -EBADFD; 15359b067034SAl Viro ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK); 153642404c09SDavid S. Miller ret = min_t(ssize_t, ret, len); 1537d0b7da8aSZhi Yong Wu if (ret > 0) 1538d0b7da8aSZhi Yong Wu iocb->ki_pos = ret; 1539631ab46bSEric W. Biederman tun_put(tun); 15401da177e4SLinus Torvalds return ret; 15411da177e4SLinus Torvalds } 15421da177e4SLinus Torvalds 154396442e42SJason Wang static void tun_free_netdev(struct net_device *dev) 154496442e42SJason Wang { 154596442e42SJason Wang struct tun_struct *tun = netdev_priv(dev); 154696442e42SJason Wang 15474008e97fSJason Wang BUG_ON(!(list_empty(&tun->disabled))); 1548608b9977SPaolo Abeni free_percpu(tun->pcpu_stats); 154996442e42SJason Wang tun_flow_uninit(tun); 15505dbbaf2dSPaul Moore security_tun_dev_free_security(tun->security); 155196442e42SJason Wang free_netdev(dev); 155296442e42SJason Wang } 155396442e42SJason Wang 15541da177e4SLinus Torvalds static void tun_setup(struct net_device *dev) 15551da177e4SLinus Torvalds { 15561da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 15571da177e4SLinus Torvalds 15580625c883SEric W. Biederman tun->owner = INVALID_UID; 15590625c883SEric W. Biederman tun->group = INVALID_GID; 15601da177e4SLinus Torvalds 15611da177e4SLinus Torvalds dev->ethtool_ops = &tun_ethtool_ops; 156296442e42SJason Wang dev->destructor = tun_free_netdev; 1563016adb72SJason Wang /* We prefer our own queue length */ 1564016adb72SJason Wang dev->tx_queue_len = TUN_READQ_SIZE; 15651da177e4SLinus Torvalds } 15661da177e4SLinus Torvalds 1567f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap 1568f019a7a5SEric W. Biederman * device with netlink. 1569f019a7a5SEric W. Biederman */ 1570f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[]) 1571f019a7a5SEric W. Biederman { 1572f019a7a5SEric W. Biederman return -EINVAL; 1573f019a7a5SEric W. Biederman } 1574f019a7a5SEric W. Biederman 1575f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = { 1576f019a7a5SEric W. Biederman .kind = DRV_NAME, 1577f019a7a5SEric W. Biederman .priv_size = sizeof(struct tun_struct), 1578f019a7a5SEric W. Biederman .setup = tun_setup, 1579f019a7a5SEric W. Biederman .validate = tun_validate, 1580f019a7a5SEric W. Biederman }; 1581f019a7a5SEric W. Biederman 158233dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk) 158333dccbb0SHerbert Xu { 158454f968d6SJason Wang struct tun_file *tfile; 158543815482SEric Dumazet wait_queue_head_t *wqueue; 158633dccbb0SHerbert Xu 158733dccbb0SHerbert Xu if (!sock_writeable(sk)) 158833dccbb0SHerbert Xu return; 158933dccbb0SHerbert Xu 15909cd3e072SEric Dumazet if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags)) 159133dccbb0SHerbert Xu return; 159233dccbb0SHerbert Xu 159343815482SEric Dumazet wqueue = sk_sleep(sk); 159443815482SEric Dumazet if (wqueue && waitqueue_active(wqueue)) 159543815482SEric Dumazet wake_up_interruptible_sync_poll(wqueue, POLLOUT | 159605c2828cSMichael S. Tsirkin POLLWRNORM | POLLWRBAND); 1597c722c625SHerbert Xu 159854f968d6SJason Wang tfile = container_of(sk, struct tun_file, sk); 159954f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); 160033dccbb0SHerbert Xu } 160133dccbb0SHerbert Xu 16021b784140SYing Xue static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len) 160305c2828cSMichael S. Tsirkin { 160454f968d6SJason Wang int ret; 160554f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 160654f968d6SJason Wang struct tun_struct *tun = __tun_get(tfile); 160754f968d6SJason Wang 160854f968d6SJason Wang if (!tun) 160954f968d6SJason Wang return -EBADFD; 1610f5ff53b4SAl Viro 1611c0371da6SAl Viro ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter, 16125503fcecSJason Wang m->msg_flags & MSG_DONTWAIT, 16135503fcecSJason Wang m->msg_flags & MSG_MORE); 161454f968d6SJason Wang tun_put(tun); 161554f968d6SJason Wang return ret; 161605c2828cSMichael S. Tsirkin } 161705c2828cSMichael S. Tsirkin 16181b784140SYing Xue static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, 161905c2828cSMichael S. Tsirkin int flags) 162005c2828cSMichael S. Tsirkin { 162154f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 162254f968d6SJason Wang struct tun_struct *tun = __tun_get(tfile); 162305c2828cSMichael S. Tsirkin int ret; 162454f968d6SJason Wang 162554f968d6SJason Wang if (!tun) 162654f968d6SJason Wang return -EBADFD; 162754f968d6SJason Wang 1628eda29772SRichard Cochran if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) { 16293811ae76SGao feng ret = -EINVAL; 16303811ae76SGao feng goto out; 16313811ae76SGao feng } 1632eda29772SRichard Cochran if (flags & MSG_ERRQUEUE) { 1633eda29772SRichard Cochran ret = sock_recv_errqueue(sock->sk, m, total_len, 1634eda29772SRichard Cochran SOL_PACKET, TUN_TX_TIMESTAMP); 1635eda29772SRichard Cochran goto out; 1636eda29772SRichard Cochran } 1637c0371da6SAl Viro ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT); 163887897931SAlex Gartrell if (ret > (ssize_t)total_len) { 163942404c09SDavid S. Miller m->msg_flags |= MSG_TRUNC; 164042404c09SDavid S. Miller ret = flags & MSG_TRUNC ? ret : total_len; 164142404c09SDavid S. Miller } 16423811ae76SGao feng out: 164354f968d6SJason Wang tun_put(tun); 164405c2828cSMichael S. Tsirkin return ret; 164505c2828cSMichael S. Tsirkin } 164605c2828cSMichael S. Tsirkin 16471576d986SJason Wang static int tun_peek_len(struct socket *sock) 16481576d986SJason Wang { 16491576d986SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 16501576d986SJason Wang struct tun_struct *tun; 16511576d986SJason Wang int ret = 0; 16521576d986SJason Wang 16531576d986SJason Wang tun = __tun_get(tfile); 16541576d986SJason Wang if (!tun) 16551576d986SJason Wang return 0; 16561576d986SJason Wang 16571576d986SJason Wang ret = skb_array_peek_len(&tfile->tx_array); 16581576d986SJason Wang tun_put(tun); 16591576d986SJason Wang 16601576d986SJason Wang return ret; 16611576d986SJason Wang } 16621576d986SJason Wang 166305c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */ 166405c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = { 16651576d986SJason Wang .peek_len = tun_peek_len, 166605c2828cSMichael S. Tsirkin .sendmsg = tun_sendmsg, 166705c2828cSMichael S. Tsirkin .recvmsg = tun_recvmsg, 166805c2828cSMichael S. Tsirkin }; 166905c2828cSMichael S. Tsirkin 167033dccbb0SHerbert Xu static struct proto tun_proto = { 167133dccbb0SHerbert Xu .name = "tun", 167233dccbb0SHerbert Xu .owner = THIS_MODULE, 167354f968d6SJason Wang .obj_size = sizeof(struct tun_file), 167433dccbb0SHerbert Xu }; 1675f019a7a5SEric W. Biederman 1676980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun) 1677980c9e8cSDavid Woodhouse { 1678031f5e03SMichael S. Tsirkin return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP); 1679980c9e8cSDavid Woodhouse } 1680980c9e8cSDavid Woodhouse 1681980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr, 1682980c9e8cSDavid Woodhouse char *buf) 1683980c9e8cSDavid Woodhouse { 1684980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 1685980c9e8cSDavid Woodhouse return sprintf(buf, "0x%x\n", tun_flags(tun)); 1686980c9e8cSDavid Woodhouse } 1687980c9e8cSDavid Woodhouse 1688980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr, 1689980c9e8cSDavid Woodhouse char *buf) 1690980c9e8cSDavid Woodhouse { 1691980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 16920625c883SEric W. Biederman return uid_valid(tun->owner)? 16930625c883SEric W. Biederman sprintf(buf, "%u\n", 16940625c883SEric W. Biederman from_kuid_munged(current_user_ns(), tun->owner)): 16950625c883SEric W. Biederman sprintf(buf, "-1\n"); 1696980c9e8cSDavid Woodhouse } 1697980c9e8cSDavid Woodhouse 1698980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr, 1699980c9e8cSDavid Woodhouse char *buf) 1700980c9e8cSDavid Woodhouse { 1701980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 17020625c883SEric W. Biederman return gid_valid(tun->group) ? 17030625c883SEric W. Biederman sprintf(buf, "%u\n", 17040625c883SEric W. Biederman from_kgid_munged(current_user_ns(), tun->group)): 17050625c883SEric W. Biederman sprintf(buf, "-1\n"); 1706980c9e8cSDavid Woodhouse } 1707980c9e8cSDavid Woodhouse 1708980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL); 1709980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL); 1710980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL); 1711980c9e8cSDavid Woodhouse 1712c4d33e24STakashi Iwai static struct attribute *tun_dev_attrs[] = { 1713c4d33e24STakashi Iwai &dev_attr_tun_flags.attr, 1714c4d33e24STakashi Iwai &dev_attr_owner.attr, 1715c4d33e24STakashi Iwai &dev_attr_group.attr, 1716c4d33e24STakashi Iwai NULL 1717c4d33e24STakashi Iwai }; 1718c4d33e24STakashi Iwai 1719c4d33e24STakashi Iwai static const struct attribute_group tun_attr_group = { 1720c4d33e24STakashi Iwai .attrs = tun_dev_attrs 1721c4d33e24STakashi Iwai }; 1722c4d33e24STakashi Iwai 1723d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) 17241da177e4SLinus Torvalds { 17251da177e4SLinus Torvalds struct tun_struct *tun; 172654f968d6SJason Wang struct tun_file *tfile = file->private_data; 17271da177e4SLinus Torvalds struct net_device *dev; 17281da177e4SLinus Torvalds int err; 17291da177e4SLinus Torvalds 17307c0c3b1aSJason Wang if (tfile->detached) 17317c0c3b1aSJason Wang return -EINVAL; 17327c0c3b1aSJason Wang 173374a3e5a7SEric W. Biederman dev = __dev_get_by_name(net, ifr->ifr_name); 173474a3e5a7SEric W. Biederman if (dev) { 1735f85ba780SDavid Woodhouse if (ifr->ifr_flags & IFF_TUN_EXCL) 1736f85ba780SDavid Woodhouse return -EBUSY; 173774a3e5a7SEric W. Biederman if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) 173874a3e5a7SEric W. Biederman tun = netdev_priv(dev); 173974a3e5a7SEric W. Biederman else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) 174074a3e5a7SEric W. Biederman tun = netdev_priv(dev); 174174a3e5a7SEric W. Biederman else 174274a3e5a7SEric W. Biederman return -EINVAL; 174374a3e5a7SEric W. Biederman 17448e6d91aeSJason Wang if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) != 174540630b82SMichael S. Tsirkin !!(tun->flags & IFF_MULTI_QUEUE)) 17468e6d91aeSJason Wang return -EINVAL; 17478e6d91aeSJason Wang 1748cde8b15fSJason Wang if (tun_not_capable(tun)) 17492b980dbdSPaul Moore return -EPERM; 17505dbbaf2dSPaul Moore err = security_tun_dev_open(tun->security); 17512b980dbdSPaul Moore if (err < 0) 17522b980dbdSPaul Moore return err; 17532b980dbdSPaul Moore 1754849c9b6fSPavel Emelyanov err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER); 1755a7385ba2SEric W. Biederman if (err < 0) 1756a7385ba2SEric W. Biederman return err; 17574008e97fSJason Wang 175840630b82SMichael S. Tsirkin if (tun->flags & IFF_MULTI_QUEUE && 1759e8dbad66SJason Wang (tun->numqueues + tun->numdisabled > 1)) { 1760e8dbad66SJason Wang /* One or more queue has already been attached, no need 1761e8dbad66SJason Wang * to initialize the device again. 1762e8dbad66SJason Wang */ 1763e8dbad66SJason Wang return 0; 1764e8dbad66SJason Wang } 176586a264abSDavid Howells } 17661da177e4SLinus Torvalds else { 17671da177e4SLinus Torvalds char *name; 17681da177e4SLinus Torvalds unsigned long flags = 0; 1769edfb6a14SJason Wang int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ? 1770edfb6a14SJason Wang MAX_TAP_QUEUES : 1; 17711da177e4SLinus Torvalds 1772c260b772SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1773ca6bb5d7SDavid Woodhouse return -EPERM; 17742b980dbdSPaul Moore err = security_tun_dev_create(); 17752b980dbdSPaul Moore if (err < 0) 17762b980dbdSPaul Moore return err; 1777ca6bb5d7SDavid Woodhouse 17781da177e4SLinus Torvalds /* Set dev type */ 17791da177e4SLinus Torvalds if (ifr->ifr_flags & IFF_TUN) { 17801da177e4SLinus Torvalds /* TUN device */ 178140630b82SMichael S. Tsirkin flags |= IFF_TUN; 17821da177e4SLinus Torvalds name = "tun%d"; 17831da177e4SLinus Torvalds } else if (ifr->ifr_flags & IFF_TAP) { 17841da177e4SLinus Torvalds /* TAP device */ 178540630b82SMichael S. Tsirkin flags |= IFF_TAP; 17861da177e4SLinus Torvalds name = "tap%d"; 17871da177e4SLinus Torvalds } else 178836989b90SKusanagi Kouichi return -EINVAL; 17891da177e4SLinus Torvalds 17901da177e4SLinus Torvalds if (*ifr->ifr_name) 17911da177e4SLinus Torvalds name = ifr->ifr_name; 17921da177e4SLinus Torvalds 1793c8d68e6bSJason Wang dev = alloc_netdev_mqs(sizeof(struct tun_struct), name, 1794c835a677STom Gundersen NET_NAME_UNKNOWN, tun_setup, queues, 1795c835a677STom Gundersen queues); 1796edfb6a14SJason Wang 17971da177e4SLinus Torvalds if (!dev) 17981da177e4SLinus Torvalds return -ENOMEM; 17991da177e4SLinus Torvalds 1800fc54c658SPavel Emelyanov dev_net_set(dev, net); 1801f019a7a5SEric W. Biederman dev->rtnl_link_ops = &tun_link_ops; 1802fb7589a1SPavel Emelyanov dev->ifindex = tfile->ifindex; 1803c4d33e24STakashi Iwai dev->sysfs_groups[0] = &tun_attr_group; 1804758e43b7SStephen Hemminger 18051da177e4SLinus Torvalds tun = netdev_priv(dev); 18061da177e4SLinus Torvalds tun->dev = dev; 18071da177e4SLinus Torvalds tun->flags = flags; 1808f271b2ccSMax Krasnyansky tun->txflt.count = 0; 1809d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr); 18101da177e4SLinus Torvalds 1811eaea34b2SPaolo Abeni tun->align = NET_SKB_PAD; 181254f968d6SJason Wang tun->filter_attached = false; 181354f968d6SJason Wang tun->sndbuf = tfile->socket.sk->sk_sndbuf; 18145503fcecSJason Wang tun->rx_batched = 0; 181533dccbb0SHerbert Xu 1816608b9977SPaolo Abeni tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats); 1817608b9977SPaolo Abeni if (!tun->pcpu_stats) { 1818608b9977SPaolo Abeni err = -ENOMEM; 1819608b9977SPaolo Abeni goto err_free_dev; 1820608b9977SPaolo Abeni } 1821608b9977SPaolo Abeni 182296442e42SJason Wang spin_lock_init(&tun->lock); 182396442e42SJason Wang 18245dbbaf2dSPaul Moore err = security_tun_dev_alloc_security(&tun->security); 18255dbbaf2dSPaul Moore if (err < 0) 1826608b9977SPaolo Abeni goto err_free_stat; 18272b980dbdSPaul Moore 18281da177e4SLinus Torvalds tun_net_init(dev); 1829944a1376SPavel Emelyanov tun_flow_init(tun); 183096442e42SJason Wang 183188255375SMichał Mirosław dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | 18326680ec68SJason Wang TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX | 18336680ec68SJason Wang NETIF_F_HW_VLAN_STAG_TX; 18342a2bbf17SPaolo Abeni dev->features = dev->hw_features | NETIF_F_LLTX; 18356671b224SFernando Luis Vazquez Cao dev->vlan_features = dev->features & 18366671b224SFernando Luis Vazquez Cao ~(NETIF_F_HW_VLAN_CTAG_TX | 18376671b224SFernando Luis Vazquez Cao NETIF_F_HW_VLAN_STAG_TX); 183888255375SMichał Mirosław 18394008e97fSJason Wang INIT_LIST_HEAD(&tun->disabled); 1840849c9b6fSPavel Emelyanov err = tun_attach(tun, file, false); 1841eb0fb363SJason Wang if (err < 0) 1842662ca437SJason Wang goto err_free_flow; 1843eb0fb363SJason Wang 18441da177e4SLinus Torvalds err = register_netdevice(tun->dev); 18451da177e4SLinus Torvalds if (err < 0) 1846662ca437SJason Wang goto err_detach; 1847af668b3cSMichael S. Tsirkin } 1848980c9e8cSDavid Woodhouse 1849eb0fb363SJason Wang netif_carrier_on(tun->dev); 18501da177e4SLinus Torvalds 18516b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_set_iff\n"); 18521da177e4SLinus Torvalds 1853031f5e03SMichael S. Tsirkin tun->flags = (tun->flags & ~TUN_FEATURES) | 1854031f5e03SMichael S. Tsirkin (ifr->ifr_flags & TUN_FEATURES); 1855c8d68e6bSJason Wang 1856e35259a9SMax Krasnyansky /* Make sure persistent devices do not get stuck in 1857e35259a9SMax Krasnyansky * xoff state. 1858e35259a9SMax Krasnyansky */ 1859e35259a9SMax Krasnyansky if (netif_running(tun->dev)) 1860c8d68e6bSJason Wang netif_tx_wake_all_queues(tun->dev); 1861e35259a9SMax Krasnyansky 18621da177e4SLinus Torvalds strcpy(ifr->ifr_name, tun->dev->name); 18631da177e4SLinus Torvalds return 0; 18641da177e4SLinus Torvalds 1865662ca437SJason Wang err_detach: 1866662ca437SJason Wang tun_detach_all(dev); 1867662ca437SJason Wang err_free_flow: 1868662ca437SJason Wang tun_flow_uninit(tun); 1869662ca437SJason Wang security_tun_dev_free_security(tun->security); 1870608b9977SPaolo Abeni err_free_stat: 1871608b9977SPaolo Abeni free_percpu(tun->pcpu_stats); 18721da177e4SLinus Torvalds err_free_dev: 18731da177e4SLinus Torvalds free_netdev(dev); 18741da177e4SLinus Torvalds return err; 18751da177e4SLinus Torvalds } 18761da177e4SLinus Torvalds 18779ce99cf6SRami Rosen static void tun_get_iff(struct net *net, struct tun_struct *tun, 1878876bfd4dSHerbert Xu struct ifreq *ifr) 1879e3b99556SMark McLoughlin { 18806b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_get_iff\n"); 1881e3b99556SMark McLoughlin 1882e3b99556SMark McLoughlin strcpy(ifr->ifr_name, tun->dev->name); 1883e3b99556SMark McLoughlin 1884980c9e8cSDavid Woodhouse ifr->ifr_flags = tun_flags(tun); 1885e3b99556SMark McLoughlin 1886e3b99556SMark McLoughlin } 1887e3b99556SMark McLoughlin 18885228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no 18895228ddc9SRusty Russell * privs required. */ 189088255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg) 18915228ddc9SRusty Russell { 1892c8f44affSMichał Mirosław netdev_features_t features = 0; 18935228ddc9SRusty Russell 18945228ddc9SRusty Russell if (arg & TUN_F_CSUM) { 189588255375SMichał Mirosław features |= NETIF_F_HW_CSUM; 18965228ddc9SRusty Russell arg &= ~TUN_F_CSUM; 18975228ddc9SRusty Russell 18985228ddc9SRusty Russell if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { 18995228ddc9SRusty Russell if (arg & TUN_F_TSO_ECN) { 19005228ddc9SRusty Russell features |= NETIF_F_TSO_ECN; 19015228ddc9SRusty Russell arg &= ~TUN_F_TSO_ECN; 19025228ddc9SRusty Russell } 19035228ddc9SRusty Russell if (arg & TUN_F_TSO4) 19045228ddc9SRusty Russell features |= NETIF_F_TSO; 19055228ddc9SRusty Russell if (arg & TUN_F_TSO6) 19065228ddc9SRusty Russell features |= NETIF_F_TSO6; 19075228ddc9SRusty Russell arg &= ~(TUN_F_TSO4|TUN_F_TSO6); 19085228ddc9SRusty Russell } 1909e3e3c423SVlad Yasevich 1910e3e3c423SVlad Yasevich if (arg & TUN_F_UFO) { 1911e3e3c423SVlad Yasevich features |= NETIF_F_UFO; 1912e3e3c423SVlad Yasevich arg &= ~TUN_F_UFO; 1913e3e3c423SVlad Yasevich } 19145228ddc9SRusty Russell } 19155228ddc9SRusty Russell 19165228ddc9SRusty Russell /* This gives the user a way to test for new features in future by 19175228ddc9SRusty Russell * trying to set them. */ 19185228ddc9SRusty Russell if (arg) 19195228ddc9SRusty Russell return -EINVAL; 19205228ddc9SRusty Russell 192188255375SMichał Mirosław tun->set_features = features; 192288255375SMichał Mirosław netdev_update_features(tun->dev); 19235228ddc9SRusty Russell 19245228ddc9SRusty Russell return 0; 19255228ddc9SRusty Russell } 19265228ddc9SRusty Russell 1927c8d68e6bSJason Wang static void tun_detach_filter(struct tun_struct *tun, int n) 1928c8d68e6bSJason Wang { 1929c8d68e6bSJason Wang int i; 1930c8d68e6bSJason Wang struct tun_file *tfile; 1931c8d68e6bSJason Wang 1932c8d68e6bSJason Wang for (i = 0; i < n; i++) { 1933b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 19348ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 19358ced425eSHannes Frederic Sowa sk_detach_filter(tfile->socket.sk); 19368ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 1937c8d68e6bSJason Wang } 1938c8d68e6bSJason Wang 1939c8d68e6bSJason Wang tun->filter_attached = false; 1940c8d68e6bSJason Wang } 1941c8d68e6bSJason Wang 1942c8d68e6bSJason Wang static int tun_attach_filter(struct tun_struct *tun) 1943c8d68e6bSJason Wang { 1944c8d68e6bSJason Wang int i, ret = 0; 1945c8d68e6bSJason Wang struct tun_file *tfile; 1946c8d68e6bSJason Wang 1947c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 1948b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 19498ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 19508ced425eSHannes Frederic Sowa ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); 19518ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 1952c8d68e6bSJason Wang if (ret) { 1953c8d68e6bSJason Wang tun_detach_filter(tun, i); 1954c8d68e6bSJason Wang return ret; 1955c8d68e6bSJason Wang } 1956c8d68e6bSJason Wang } 1957c8d68e6bSJason Wang 1958c8d68e6bSJason Wang tun->filter_attached = true; 1959c8d68e6bSJason Wang return ret; 1960c8d68e6bSJason Wang } 1961c8d68e6bSJason Wang 1962c8d68e6bSJason Wang static void tun_set_sndbuf(struct tun_struct *tun) 1963c8d68e6bSJason Wang { 1964c8d68e6bSJason Wang struct tun_file *tfile; 1965c8d68e6bSJason Wang int i; 1966c8d68e6bSJason Wang 1967c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 1968b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 1969c8d68e6bSJason Wang tfile->socket.sk->sk_sndbuf = tun->sndbuf; 1970c8d68e6bSJason Wang } 1971c8d68e6bSJason Wang } 1972c8d68e6bSJason Wang 1973cde8b15fSJason Wang static int tun_set_queue(struct file *file, struct ifreq *ifr) 1974cde8b15fSJason Wang { 1975cde8b15fSJason Wang struct tun_file *tfile = file->private_data; 1976cde8b15fSJason Wang struct tun_struct *tun; 1977cde8b15fSJason Wang int ret = 0; 1978cde8b15fSJason Wang 1979cde8b15fSJason Wang rtnl_lock(); 1980cde8b15fSJason Wang 1981cde8b15fSJason Wang if (ifr->ifr_flags & IFF_ATTACH_QUEUE) { 19824008e97fSJason Wang tun = tfile->detached; 19835dbbaf2dSPaul Moore if (!tun) { 1984cde8b15fSJason Wang ret = -EINVAL; 19855dbbaf2dSPaul Moore goto unlock; 19865dbbaf2dSPaul Moore } 19875dbbaf2dSPaul Moore ret = security_tun_dev_attach_queue(tun->security); 19885dbbaf2dSPaul Moore if (ret < 0) 19895dbbaf2dSPaul Moore goto unlock; 1990849c9b6fSPavel Emelyanov ret = tun_attach(tun, file, false); 19914008e97fSJason Wang } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) { 1992b8deabd3SJason Wang tun = rtnl_dereference(tfile->tun); 199340630b82SMichael S. Tsirkin if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached) 19944008e97fSJason Wang ret = -EINVAL; 1995cde8b15fSJason Wang else 19964008e97fSJason Wang __tun_detach(tfile, false); 19974008e97fSJason Wang } else 1998cde8b15fSJason Wang ret = -EINVAL; 1999cde8b15fSJason Wang 20005dbbaf2dSPaul Moore unlock: 2001cde8b15fSJason Wang rtnl_unlock(); 2002cde8b15fSJason Wang return ret; 2003cde8b15fSJason Wang } 2004cde8b15fSJason Wang 200550857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd, 200650857e2aSArnd Bergmann unsigned long arg, int ifreq_len) 20071da177e4SLinus Torvalds { 200836b50babSEric W. Biederman struct tun_file *tfile = file->private_data; 2009631ab46bSEric W. Biederman struct tun_struct *tun; 20101da177e4SLinus Torvalds void __user* argp = (void __user*)arg; 20111da177e4SLinus Torvalds struct ifreq ifr; 20120625c883SEric W. Biederman kuid_t owner; 20130625c883SEric W. Biederman kgid_t group; 201433dccbb0SHerbert Xu int sndbuf; 2015d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 2016fb7589a1SPavel Emelyanov unsigned int ifindex; 20171cf8e410SMichael S. Tsirkin int le; 2018f271b2ccSMax Krasnyansky int ret; 20191da177e4SLinus Torvalds 202020861f26SGao Feng if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == SOCK_IOC_TYPE) { 202150857e2aSArnd Bergmann if (copy_from_user(&ifr, argp, ifreq_len)) 20221da177e4SLinus Torvalds return -EFAULT; 20238bbb1813SDavid S. Miller } else { 2024a117dacdSMathias Krause memset(&ifr, 0, sizeof(ifr)); 20258bbb1813SDavid S. Miller } 2026631ab46bSEric W. Biederman if (cmd == TUNGETFEATURES) { 2027631ab46bSEric W. Biederman /* Currently this just means: "what IFF flags are valid?". 2028631ab46bSEric W. Biederman * This is needed because we never checked for invalid flags on 2029031f5e03SMichael S. Tsirkin * TUNSETIFF. 2030031f5e03SMichael S. Tsirkin */ 2031031f5e03SMichael S. Tsirkin return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES, 2032631ab46bSEric W. Biederman (unsigned int __user*)argp); 2033cde8b15fSJason Wang } else if (cmd == TUNSETQUEUE) 2034cde8b15fSJason Wang return tun_set_queue(file, &ifr); 2035631ab46bSEric W. Biederman 2036c8d68e6bSJason Wang ret = 0; 2037876bfd4dSHerbert Xu rtnl_lock(); 2038876bfd4dSHerbert Xu 203936b50babSEric W. Biederman tun = __tun_get(tfile); 20400f16bc13SGao Feng if (cmd == TUNSETIFF) { 20410f16bc13SGao Feng ret = -EEXIST; 20420f16bc13SGao Feng if (tun) 20430f16bc13SGao Feng goto unlock; 20440f16bc13SGao Feng 20451da177e4SLinus Torvalds ifr.ifr_name[IFNAMSIZ-1] = '\0'; 20461da177e4SLinus Torvalds 2047140e807dSEric W. Biederman ret = tun_set_iff(sock_net(&tfile->sk), file, &ifr); 20481da177e4SLinus Torvalds 2049876bfd4dSHerbert Xu if (ret) 2050876bfd4dSHerbert Xu goto unlock; 20511da177e4SLinus Torvalds 205250857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 2053876bfd4dSHerbert Xu ret = -EFAULT; 2054876bfd4dSHerbert Xu goto unlock; 20551da177e4SLinus Torvalds } 2056fb7589a1SPavel Emelyanov if (cmd == TUNSETIFINDEX) { 2057fb7589a1SPavel Emelyanov ret = -EPERM; 2058fb7589a1SPavel Emelyanov if (tun) 2059fb7589a1SPavel Emelyanov goto unlock; 2060fb7589a1SPavel Emelyanov 2061fb7589a1SPavel Emelyanov ret = -EFAULT; 2062fb7589a1SPavel Emelyanov if (copy_from_user(&ifindex, argp, sizeof(ifindex))) 2063fb7589a1SPavel Emelyanov goto unlock; 2064fb7589a1SPavel Emelyanov 2065fb7589a1SPavel Emelyanov ret = 0; 2066fb7589a1SPavel Emelyanov tfile->ifindex = ifindex; 2067fb7589a1SPavel Emelyanov goto unlock; 2068fb7589a1SPavel Emelyanov } 20691da177e4SLinus Torvalds 2070876bfd4dSHerbert Xu ret = -EBADFD; 20711da177e4SLinus Torvalds if (!tun) 2072876bfd4dSHerbert Xu goto unlock; 20731da177e4SLinus Torvalds 20741e588338SJason Wang tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd); 20751da177e4SLinus Torvalds 2076631ab46bSEric W. Biederman ret = 0; 20771da177e4SLinus Torvalds switch (cmd) { 2078e3b99556SMark McLoughlin case TUNGETIFF: 20799ce99cf6SRami Rosen tun_get_iff(current->nsproxy->net_ns, tun, &ifr); 2080e3b99556SMark McLoughlin 20813d407a80SPavel Emelyanov if (tfile->detached) 20823d407a80SPavel Emelyanov ifr.ifr_flags |= IFF_DETACH_QUEUE; 2083849c9b6fSPavel Emelyanov if (!tfile->socket.sk->sk_filter) 2084849c9b6fSPavel Emelyanov ifr.ifr_flags |= IFF_NOFILTER; 20853d407a80SPavel Emelyanov 208650857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 2087631ab46bSEric W. Biederman ret = -EFAULT; 2088e3b99556SMark McLoughlin break; 2089e3b99556SMark McLoughlin 20901da177e4SLinus Torvalds case TUNSETNOCSUM: 20911da177e4SLinus Torvalds /* Disable/Enable checksum */ 20921da177e4SLinus Torvalds 209388255375SMichał Mirosław /* [unimplemented] */ 209488255375SMichał Mirosław tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n", 20956b8a66eeSJoe Perches arg ? "disabled" : "enabled"); 20961da177e4SLinus Torvalds break; 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds case TUNSETPERSIST: 209954f968d6SJason Wang /* Disable/Enable persist mode. Keep an extra reference to the 210054f968d6SJason Wang * module to prevent the module being unprobed. 210154f968d6SJason Wang */ 210240630b82SMichael S. Tsirkin if (arg && !(tun->flags & IFF_PERSIST)) { 210340630b82SMichael S. Tsirkin tun->flags |= IFF_PERSIST; 210454f968d6SJason Wang __module_get(THIS_MODULE); 2105dd38bd85SJason Wang } 210640630b82SMichael S. Tsirkin if (!arg && (tun->flags & IFF_PERSIST)) { 210740630b82SMichael S. Tsirkin tun->flags &= ~IFF_PERSIST; 210854f968d6SJason Wang module_put(THIS_MODULE); 210954f968d6SJason Wang } 21101da177e4SLinus Torvalds 21116b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "persist %s\n", 21126b8a66eeSJoe Perches arg ? "enabled" : "disabled"); 21131da177e4SLinus Torvalds break; 21141da177e4SLinus Torvalds 21151da177e4SLinus Torvalds case TUNSETOWNER: 21161da177e4SLinus Torvalds /* Set owner of the device */ 21170625c883SEric W. Biederman owner = make_kuid(current_user_ns(), arg); 21180625c883SEric W. Biederman if (!uid_valid(owner)) { 21190625c883SEric W. Biederman ret = -EINVAL; 21200625c883SEric W. Biederman break; 21210625c883SEric W. Biederman } 21220625c883SEric W. Biederman tun->owner = owner; 21231e588338SJason Wang tun_debug(KERN_INFO, tun, "owner set to %u\n", 21240625c883SEric W. Biederman from_kuid(&init_user_ns, tun->owner)); 21251da177e4SLinus Torvalds break; 21261da177e4SLinus Torvalds 21278c644623SGuido Guenther case TUNSETGROUP: 21288c644623SGuido Guenther /* Set group of the device */ 21290625c883SEric W. Biederman group = make_kgid(current_user_ns(), arg); 21300625c883SEric W. Biederman if (!gid_valid(group)) { 21310625c883SEric W. Biederman ret = -EINVAL; 21320625c883SEric W. Biederman break; 21330625c883SEric W. Biederman } 21340625c883SEric W. Biederman tun->group = group; 21351e588338SJason Wang tun_debug(KERN_INFO, tun, "group set to %u\n", 21360625c883SEric W. Biederman from_kgid(&init_user_ns, tun->group)); 21378c644623SGuido Guenther break; 21388c644623SGuido Guenther 2139ff4cc3acSMike Kershaw case TUNSETLINK: 2140ff4cc3acSMike Kershaw /* Only allow setting the type when the interface is down */ 2141ff4cc3acSMike Kershaw if (tun->dev->flags & IFF_UP) { 21426b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, 21436b8a66eeSJoe Perches "Linktype set failed because interface is up\n"); 214448abfe05SDavid S. Miller ret = -EBUSY; 2145ff4cc3acSMike Kershaw } else { 2146ff4cc3acSMike Kershaw tun->dev->type = (int) arg; 21476b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "linktype set to %d\n", 21486b8a66eeSJoe Perches tun->dev->type); 214948abfe05SDavid S. Miller ret = 0; 2150ff4cc3acSMike Kershaw } 2151631ab46bSEric W. Biederman break; 2152ff4cc3acSMike Kershaw 21531da177e4SLinus Torvalds #ifdef TUN_DEBUG 21541da177e4SLinus Torvalds case TUNSETDEBUG: 21551da177e4SLinus Torvalds tun->debug = arg; 21561da177e4SLinus Torvalds break; 21571da177e4SLinus Torvalds #endif 21585228ddc9SRusty Russell case TUNSETOFFLOAD: 215988255375SMichał Mirosław ret = set_offload(tun, arg); 2160631ab46bSEric W. Biederman break; 21615228ddc9SRusty Russell 2162f271b2ccSMax Krasnyansky case TUNSETTXFILTER: 2163f271b2ccSMax Krasnyansky /* Can be set only for TAPs */ 2164631ab46bSEric W. Biederman ret = -EINVAL; 216540630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 2166631ab46bSEric W. Biederman break; 2167c0e5a8c2SHarvey Harrison ret = update_filter(&tun->txflt, (void __user *)arg); 2168631ab46bSEric W. Biederman break; 21691da177e4SLinus Torvalds 21701da177e4SLinus Torvalds case SIOCGIFHWADDR: 2171b595076aSUwe Kleine-König /* Get hw address */ 2172f271b2ccSMax Krasnyansky memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); 2173f271b2ccSMax Krasnyansky ifr.ifr_hwaddr.sa_family = tun->dev->type; 217450857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 2175631ab46bSEric W. Biederman ret = -EFAULT; 2176631ab46bSEric W. Biederman break; 21771da177e4SLinus Torvalds 21781da177e4SLinus Torvalds case SIOCSIFHWADDR: 2179f271b2ccSMax Krasnyansky /* Set hw address */ 21806b8a66eeSJoe Perches tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n", 21816b8a66eeSJoe Perches ifr.ifr_hwaddr.sa_data); 218240102371SKim B. Heino 218340102371SKim B. Heino ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); 2184631ab46bSEric W. Biederman break; 218533dccbb0SHerbert Xu 218633dccbb0SHerbert Xu case TUNGETSNDBUF: 218754f968d6SJason Wang sndbuf = tfile->socket.sk->sk_sndbuf; 218833dccbb0SHerbert Xu if (copy_to_user(argp, &sndbuf, sizeof(sndbuf))) 218933dccbb0SHerbert Xu ret = -EFAULT; 219033dccbb0SHerbert Xu break; 219133dccbb0SHerbert Xu 219233dccbb0SHerbert Xu case TUNSETSNDBUF: 219333dccbb0SHerbert Xu if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) { 219433dccbb0SHerbert Xu ret = -EFAULT; 219533dccbb0SHerbert Xu break; 219633dccbb0SHerbert Xu } 219733dccbb0SHerbert Xu 2198c8d68e6bSJason Wang tun->sndbuf = sndbuf; 2199c8d68e6bSJason Wang tun_set_sndbuf(tun); 220033dccbb0SHerbert Xu break; 220133dccbb0SHerbert Xu 2202d9d52b51SMichael S. Tsirkin case TUNGETVNETHDRSZ: 2203d9d52b51SMichael S. Tsirkin vnet_hdr_sz = tun->vnet_hdr_sz; 2204d9d52b51SMichael S. Tsirkin if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz))) 2205d9d52b51SMichael S. Tsirkin ret = -EFAULT; 2206d9d52b51SMichael S. Tsirkin break; 2207d9d52b51SMichael S. Tsirkin 2208d9d52b51SMichael S. Tsirkin case TUNSETVNETHDRSZ: 2209d9d52b51SMichael S. Tsirkin if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) { 2210d9d52b51SMichael S. Tsirkin ret = -EFAULT; 2211d9d52b51SMichael S. Tsirkin break; 2212d9d52b51SMichael S. Tsirkin } 2213d9d52b51SMichael S. Tsirkin if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) { 2214d9d52b51SMichael S. Tsirkin ret = -EINVAL; 2215d9d52b51SMichael S. Tsirkin break; 2216d9d52b51SMichael S. Tsirkin } 2217d9d52b51SMichael S. Tsirkin 2218d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = vnet_hdr_sz; 2219d9d52b51SMichael S. Tsirkin break; 2220d9d52b51SMichael S. Tsirkin 22211cf8e410SMichael S. Tsirkin case TUNGETVNETLE: 22221cf8e410SMichael S. Tsirkin le = !!(tun->flags & TUN_VNET_LE); 22231cf8e410SMichael S. Tsirkin if (put_user(le, (int __user *)argp)) 22241cf8e410SMichael S. Tsirkin ret = -EFAULT; 22251cf8e410SMichael S. Tsirkin break; 22261cf8e410SMichael S. Tsirkin 22271cf8e410SMichael S. Tsirkin case TUNSETVNETLE: 22281cf8e410SMichael S. Tsirkin if (get_user(le, (int __user *)argp)) { 22291cf8e410SMichael S. Tsirkin ret = -EFAULT; 22301cf8e410SMichael S. Tsirkin break; 22311cf8e410SMichael S. Tsirkin } 22321cf8e410SMichael S. Tsirkin if (le) 22331cf8e410SMichael S. Tsirkin tun->flags |= TUN_VNET_LE; 22341cf8e410SMichael S. Tsirkin else 22351cf8e410SMichael S. Tsirkin tun->flags &= ~TUN_VNET_LE; 22361cf8e410SMichael S. Tsirkin break; 22371cf8e410SMichael S. Tsirkin 22388b8e658bSGreg Kurz case TUNGETVNETBE: 22398b8e658bSGreg Kurz ret = tun_get_vnet_be(tun, argp); 22408b8e658bSGreg Kurz break; 22418b8e658bSGreg Kurz 22428b8e658bSGreg Kurz case TUNSETVNETBE: 22438b8e658bSGreg Kurz ret = tun_set_vnet_be(tun, argp); 22448b8e658bSGreg Kurz break; 22458b8e658bSGreg Kurz 224699405162SMichael S. Tsirkin case TUNATTACHFILTER: 224799405162SMichael S. Tsirkin /* Can be set only for TAPs */ 224899405162SMichael S. Tsirkin ret = -EINVAL; 224940630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 225099405162SMichael S. Tsirkin break; 225199405162SMichael S. Tsirkin ret = -EFAULT; 225254f968d6SJason Wang if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog))) 225399405162SMichael S. Tsirkin break; 225499405162SMichael S. Tsirkin 2255c8d68e6bSJason Wang ret = tun_attach_filter(tun); 225699405162SMichael S. Tsirkin break; 225799405162SMichael S. Tsirkin 225899405162SMichael S. Tsirkin case TUNDETACHFILTER: 225999405162SMichael S. Tsirkin /* Can be set only for TAPs */ 226099405162SMichael S. Tsirkin ret = -EINVAL; 226140630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 226299405162SMichael S. Tsirkin break; 2263c8d68e6bSJason Wang ret = 0; 2264c8d68e6bSJason Wang tun_detach_filter(tun, tun->numqueues); 226599405162SMichael S. Tsirkin break; 226699405162SMichael S. Tsirkin 226776975e9cSPavel Emelyanov case TUNGETFILTER: 226876975e9cSPavel Emelyanov ret = -EINVAL; 226940630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 227076975e9cSPavel Emelyanov break; 227176975e9cSPavel Emelyanov ret = -EFAULT; 227276975e9cSPavel Emelyanov if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog))) 227376975e9cSPavel Emelyanov break; 227476975e9cSPavel Emelyanov ret = 0; 227576975e9cSPavel Emelyanov break; 227676975e9cSPavel Emelyanov 22771da177e4SLinus Torvalds default: 2278631ab46bSEric W. Biederman ret = -EINVAL; 2279631ab46bSEric W. Biederman break; 2280ee289b64SJoe Perches } 22811da177e4SLinus Torvalds 2282876bfd4dSHerbert Xu unlock: 2283876bfd4dSHerbert Xu rtnl_unlock(); 2284876bfd4dSHerbert Xu if (tun) 2285631ab46bSEric W. Biederman tun_put(tun); 2286631ab46bSEric W. Biederman return ret; 22871da177e4SLinus Torvalds } 22881da177e4SLinus Torvalds 228950857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file, 229050857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 229150857e2aSArnd Bergmann { 229250857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq)); 229350857e2aSArnd Bergmann } 229450857e2aSArnd Bergmann 229550857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 229650857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file, 229750857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 229850857e2aSArnd Bergmann { 229950857e2aSArnd Bergmann switch (cmd) { 230050857e2aSArnd Bergmann case TUNSETIFF: 230150857e2aSArnd Bergmann case TUNGETIFF: 230250857e2aSArnd Bergmann case TUNSETTXFILTER: 230350857e2aSArnd Bergmann case TUNGETSNDBUF: 230450857e2aSArnd Bergmann case TUNSETSNDBUF: 230550857e2aSArnd Bergmann case SIOCGIFHWADDR: 230650857e2aSArnd Bergmann case SIOCSIFHWADDR: 230750857e2aSArnd Bergmann arg = (unsigned long)compat_ptr(arg); 230850857e2aSArnd Bergmann break; 230950857e2aSArnd Bergmann default: 231050857e2aSArnd Bergmann arg = (compat_ulong_t)arg; 231150857e2aSArnd Bergmann break; 231250857e2aSArnd Bergmann } 231350857e2aSArnd Bergmann 231450857e2aSArnd Bergmann /* 231550857e2aSArnd Bergmann * compat_ifreq is shorter than ifreq, so we must not access beyond 231650857e2aSArnd Bergmann * the end of that structure. All fields that are used in this 231750857e2aSArnd Bergmann * driver are compatible though, we don't need to convert the 231850857e2aSArnd Bergmann * contents. 231950857e2aSArnd Bergmann */ 232050857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq)); 232150857e2aSArnd Bergmann } 232250857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */ 232350857e2aSArnd Bergmann 23241da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on) 23251da177e4SLinus Torvalds { 232654f968d6SJason Wang struct tun_file *tfile = file->private_data; 23271da177e4SLinus Torvalds int ret; 23281da177e4SLinus Torvalds 232954f968d6SJason Wang if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) 23309d319522SJonathan Corbet goto out; 23311da177e4SLinus Torvalds 23321da177e4SLinus Torvalds if (on) { 2333e0b93eddSJeff Layton __f_setown(file, task_pid(current), PIDTYPE_PID, 0); 233454f968d6SJason Wang tfile->flags |= TUN_FASYNC; 23351da177e4SLinus Torvalds } else 233654f968d6SJason Wang tfile->flags &= ~TUN_FASYNC; 23379d319522SJonathan Corbet ret = 0; 23389d319522SJonathan Corbet out: 23399d319522SJonathan Corbet return ret; 23401da177e4SLinus Torvalds } 23411da177e4SLinus Torvalds 23421da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file) 23431da177e4SLinus Torvalds { 2344140e807dSEric W. Biederman struct net *net = current->nsproxy->net_ns; 2345631ab46bSEric W. Biederman struct tun_file *tfile; 2346deed49fbSThomas Gleixner 23476b8a66eeSJoe Perches DBG1(KERN_INFO, "tunX: tun_chr_open\n"); 2348631ab46bSEric W. Biederman 2349140e807dSEric W. Biederman tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, 235011aa9c28SEric W. Biederman &tun_proto, 0); 2351631ab46bSEric W. Biederman if (!tfile) 2352631ab46bSEric W. Biederman return -ENOMEM; 2353c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 235454f968d6SJason Wang tfile->flags = 0; 2355fb7589a1SPavel Emelyanov tfile->ifindex = 0; 235654f968d6SJason Wang 235754f968d6SJason Wang init_waitqueue_head(&tfile->wq.wait); 23589e641bdcSXi Wang RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq); 235954f968d6SJason Wang 236054f968d6SJason Wang tfile->socket.file = file; 236154f968d6SJason Wang tfile->socket.ops = &tun_socket_ops; 236254f968d6SJason Wang 236354f968d6SJason Wang sock_init_data(&tfile->socket, &tfile->sk); 236454f968d6SJason Wang 236554f968d6SJason Wang tfile->sk.sk_write_space = tun_sock_write_space; 236654f968d6SJason Wang tfile->sk.sk_sndbuf = INT_MAX; 236754f968d6SJason Wang 2368631ab46bSEric W. Biederman file->private_data = tfile; 23694008e97fSJason Wang INIT_LIST_HEAD(&tfile->next); 237054f968d6SJason Wang 237119a6afb2SJason Wang sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); 237219a6afb2SJason Wang 23731da177e4SLinus Torvalds return 0; 23741da177e4SLinus Torvalds } 23751da177e4SLinus Torvalds 23761da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file) 23771da177e4SLinus Torvalds { 2378631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 23791da177e4SLinus Torvalds 2380c8d68e6bSJason Wang tun_detach(tfile, true); 23811da177e4SLinus Torvalds 23821da177e4SLinus Torvalds return 0; 23831da177e4SLinus Torvalds } 23841da177e4SLinus Torvalds 238593e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS 2386a3816ab0SJoe Perches static void tun_chr_show_fdinfo(struct seq_file *m, struct file *f) 238793e14b6dSMasatake YAMATO { 238893e14b6dSMasatake YAMATO struct tun_struct *tun; 238993e14b6dSMasatake YAMATO struct ifreq ifr; 239093e14b6dSMasatake YAMATO 239193e14b6dSMasatake YAMATO memset(&ifr, 0, sizeof(ifr)); 239293e14b6dSMasatake YAMATO 239393e14b6dSMasatake YAMATO rtnl_lock(); 239493e14b6dSMasatake YAMATO tun = tun_get(f); 239593e14b6dSMasatake YAMATO if (tun) 239693e14b6dSMasatake YAMATO tun_get_iff(current->nsproxy->net_ns, tun, &ifr); 239793e14b6dSMasatake YAMATO rtnl_unlock(); 239893e14b6dSMasatake YAMATO 239993e14b6dSMasatake YAMATO if (tun) 240093e14b6dSMasatake YAMATO tun_put(tun); 240193e14b6dSMasatake YAMATO 2402a3816ab0SJoe Perches seq_printf(m, "iff:\t%s\n", ifr.ifr_name); 240393e14b6dSMasatake YAMATO } 240493e14b6dSMasatake YAMATO #endif 240593e14b6dSMasatake YAMATO 2406d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = { 24071da177e4SLinus Torvalds .owner = THIS_MODULE, 24081da177e4SLinus Torvalds .llseek = no_llseek, 24099b067034SAl Viro .read_iter = tun_chr_read_iter, 2410f5ff53b4SAl Viro .write_iter = tun_chr_write_iter, 24111da177e4SLinus Torvalds .poll = tun_chr_poll, 2412876bfd4dSHerbert Xu .unlocked_ioctl = tun_chr_ioctl, 241350857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 241450857e2aSArnd Bergmann .compat_ioctl = tun_chr_compat_ioctl, 241550857e2aSArnd Bergmann #endif 24161da177e4SLinus Torvalds .open = tun_chr_open, 24171da177e4SLinus Torvalds .release = tun_chr_close, 241893e14b6dSMasatake YAMATO .fasync = tun_chr_fasync, 241993e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS 242093e14b6dSMasatake YAMATO .show_fdinfo = tun_chr_show_fdinfo, 242193e14b6dSMasatake YAMATO #endif 24221da177e4SLinus Torvalds }; 24231da177e4SLinus Torvalds 24241da177e4SLinus Torvalds static struct miscdevice tun_miscdev = { 24251da177e4SLinus Torvalds .minor = TUN_MINOR, 24261da177e4SLinus Torvalds .name = "tun", 2427e454cea2SKay Sievers .nodename = "net/tun", 24281da177e4SLinus Torvalds .fops = &tun_fops, 24291da177e4SLinus Torvalds }; 24301da177e4SLinus Torvalds 24311da177e4SLinus Torvalds /* ethtool interface */ 24321da177e4SLinus Torvalds 24331da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 24341da177e4SLinus Torvalds { 24351da177e4SLinus Torvalds cmd->supported = 0; 24361da177e4SLinus Torvalds cmd->advertising = 0; 243770739497SDavid Decotigny ethtool_cmd_speed_set(cmd, SPEED_10); 24381da177e4SLinus Torvalds cmd->duplex = DUPLEX_FULL; 24391da177e4SLinus Torvalds cmd->port = PORT_TP; 24401da177e4SLinus Torvalds cmd->phy_address = 0; 24411da177e4SLinus Torvalds cmd->transceiver = XCVR_INTERNAL; 24421da177e4SLinus Torvalds cmd->autoneg = AUTONEG_DISABLE; 24431da177e4SLinus Torvalds cmd->maxtxpkt = 0; 24441da177e4SLinus Torvalds cmd->maxrxpkt = 0; 24451da177e4SLinus Torvalds return 0; 24461da177e4SLinus Torvalds } 24471da177e4SLinus Torvalds 24481da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) 24491da177e4SLinus Torvalds { 24501da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 24511da177e4SLinus Torvalds 245233a5ba14SRick Jones strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); 245333a5ba14SRick Jones strlcpy(info->version, DRV_VERSION, sizeof(info->version)); 24541da177e4SLinus Torvalds 24551da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 245640630b82SMichael S. Tsirkin case IFF_TUN: 245733a5ba14SRick Jones strlcpy(info->bus_info, "tun", sizeof(info->bus_info)); 24581da177e4SLinus Torvalds break; 245940630b82SMichael S. Tsirkin case IFF_TAP: 246033a5ba14SRick Jones strlcpy(info->bus_info, "tap", sizeof(info->bus_info)); 24611da177e4SLinus Torvalds break; 24621da177e4SLinus Torvalds } 24631da177e4SLinus Torvalds } 24641da177e4SLinus Torvalds 24651da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev) 24661da177e4SLinus Torvalds { 24671da177e4SLinus Torvalds #ifdef TUN_DEBUG 24681da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 24691da177e4SLinus Torvalds return tun->debug; 24701da177e4SLinus Torvalds #else 24711da177e4SLinus Torvalds return -EOPNOTSUPP; 24721da177e4SLinus Torvalds #endif 24731da177e4SLinus Torvalds } 24741da177e4SLinus Torvalds 24751da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value) 24761da177e4SLinus Torvalds { 24771da177e4SLinus Torvalds #ifdef TUN_DEBUG 24781da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 24791da177e4SLinus Torvalds tun->debug = value; 24801da177e4SLinus Torvalds #endif 24811da177e4SLinus Torvalds } 24821da177e4SLinus Torvalds 24835503fcecSJason Wang static int tun_get_coalesce(struct net_device *dev, 24845503fcecSJason Wang struct ethtool_coalesce *ec) 24855503fcecSJason Wang { 24865503fcecSJason Wang struct tun_struct *tun = netdev_priv(dev); 24875503fcecSJason Wang 24885503fcecSJason Wang ec->rx_max_coalesced_frames = tun->rx_batched; 24895503fcecSJason Wang 24905503fcecSJason Wang return 0; 24915503fcecSJason Wang } 24925503fcecSJason Wang 24935503fcecSJason Wang static int tun_set_coalesce(struct net_device *dev, 24945503fcecSJason Wang struct ethtool_coalesce *ec) 24955503fcecSJason Wang { 24965503fcecSJason Wang struct tun_struct *tun = netdev_priv(dev); 24975503fcecSJason Wang 24985503fcecSJason Wang if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT) 24995503fcecSJason Wang tun->rx_batched = NAPI_POLL_WEIGHT; 25005503fcecSJason Wang else 25015503fcecSJason Wang tun->rx_batched = ec->rx_max_coalesced_frames; 25025503fcecSJason Wang 25035503fcecSJason Wang return 0; 25045503fcecSJason Wang } 25055503fcecSJason Wang 25067282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = { 25071da177e4SLinus Torvalds .get_settings = tun_get_settings, 25081da177e4SLinus Torvalds .get_drvinfo = tun_get_drvinfo, 25091da177e4SLinus Torvalds .get_msglevel = tun_get_msglevel, 25101da177e4SLinus Torvalds .set_msglevel = tun_set_msglevel, 2511bee31369SNolan Leake .get_link = ethtool_op_get_link, 2512eda29772SRichard Cochran .get_ts_info = ethtool_op_get_ts_info, 25135503fcecSJason Wang .get_coalesce = tun_get_coalesce, 25145503fcecSJason Wang .set_coalesce = tun_set_coalesce, 25151da177e4SLinus Torvalds }; 25161da177e4SLinus Torvalds 25171576d986SJason Wang static int tun_queue_resize(struct tun_struct *tun) 25181576d986SJason Wang { 25191576d986SJason Wang struct net_device *dev = tun->dev; 25201576d986SJason Wang struct tun_file *tfile; 25211576d986SJason Wang struct skb_array **arrays; 25221576d986SJason Wang int n = tun->numqueues + tun->numdisabled; 25231576d986SJason Wang int ret, i; 25241576d986SJason Wang 25251576d986SJason Wang arrays = kmalloc(sizeof *arrays * n, GFP_KERNEL); 25261576d986SJason Wang if (!arrays) 25271576d986SJason Wang return -ENOMEM; 25281576d986SJason Wang 25291576d986SJason Wang for (i = 0; i < tun->numqueues; i++) { 25301576d986SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 25311576d986SJason Wang arrays[i] = &tfile->tx_array; 25321576d986SJason Wang } 25331576d986SJason Wang list_for_each_entry(tfile, &tun->disabled, next) 25341576d986SJason Wang arrays[i++] = &tfile->tx_array; 25351576d986SJason Wang 25361576d986SJason Wang ret = skb_array_resize_multiple(arrays, n, 25371576d986SJason Wang dev->tx_queue_len, GFP_KERNEL); 25381576d986SJason Wang 25391576d986SJason Wang kfree(arrays); 25401576d986SJason Wang return ret; 25411576d986SJason Wang } 25421576d986SJason Wang 25431576d986SJason Wang static int tun_device_event(struct notifier_block *unused, 25441576d986SJason Wang unsigned long event, void *ptr) 25451576d986SJason Wang { 25461576d986SJason Wang struct net_device *dev = netdev_notifier_info_to_dev(ptr); 25471576d986SJason Wang struct tun_struct *tun = netdev_priv(dev); 25481576d986SJason Wang 254986dfb4acSCraig Gallek if (dev->rtnl_link_ops != &tun_link_ops) 255086dfb4acSCraig Gallek return NOTIFY_DONE; 255186dfb4acSCraig Gallek 25521576d986SJason Wang switch (event) { 25531576d986SJason Wang case NETDEV_CHANGE_TX_QUEUE_LEN: 25541576d986SJason Wang if (tun_queue_resize(tun)) 25551576d986SJason Wang return NOTIFY_BAD; 25561576d986SJason Wang break; 25571576d986SJason Wang default: 25581576d986SJason Wang break; 25591576d986SJason Wang } 25601576d986SJason Wang 25611576d986SJason Wang return NOTIFY_DONE; 25621576d986SJason Wang } 25631576d986SJason Wang 25641576d986SJason Wang static struct notifier_block tun_notifier_block __read_mostly = { 25651576d986SJason Wang .notifier_call = tun_device_event, 25661576d986SJason Wang }; 256779d17604SPavel Emelyanov 25681da177e4SLinus Torvalds static int __init tun_init(void) 25691da177e4SLinus Torvalds { 25701da177e4SLinus Torvalds int ret = 0; 25711da177e4SLinus Torvalds 25726b8a66eeSJoe Perches pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION); 25736b8a66eeSJoe Perches pr_info("%s\n", DRV_COPYRIGHT); 25741da177e4SLinus Torvalds 2575f019a7a5SEric W. Biederman ret = rtnl_link_register(&tun_link_ops); 257679d17604SPavel Emelyanov if (ret) { 25776b8a66eeSJoe Perches pr_err("Can't register link_ops\n"); 2578f019a7a5SEric W. Biederman goto err_linkops; 257979d17604SPavel Emelyanov } 258079d17604SPavel Emelyanov 25811da177e4SLinus Torvalds ret = misc_register(&tun_miscdev); 258279d17604SPavel Emelyanov if (ret) { 25836b8a66eeSJoe Perches pr_err("Can't register misc device %d\n", TUN_MINOR); 258479d17604SPavel Emelyanov goto err_misc; 258579d17604SPavel Emelyanov } 25861576d986SJason Wang 25871576d986SJason Wang register_netdevice_notifier(&tun_notifier_block); 258879d17604SPavel Emelyanov return 0; 258979d17604SPavel Emelyanov err_misc: 2590f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 2591f019a7a5SEric W. Biederman err_linkops: 25921da177e4SLinus Torvalds return ret; 25931da177e4SLinus Torvalds } 25941da177e4SLinus Torvalds 25951da177e4SLinus Torvalds static void tun_cleanup(void) 25961da177e4SLinus Torvalds { 25971da177e4SLinus Torvalds misc_deregister(&tun_miscdev); 2598f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 25991576d986SJason Wang unregister_netdevice_notifier(&tun_notifier_block); 26001da177e4SLinus Torvalds } 26011da177e4SLinus Torvalds 260205c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file. Returns error unless file is 260305c2828cSMichael S. Tsirkin * attached to a device. The returned object works like a packet socket, it 260405c2828cSMichael S. Tsirkin * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for 260505c2828cSMichael S. Tsirkin * holding a reference to the file for as long as the socket is in use. */ 260605c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file) 260705c2828cSMichael S. Tsirkin { 26086e914fc7SJason Wang struct tun_file *tfile; 260905c2828cSMichael S. Tsirkin if (file->f_op != &tun_fops) 261005c2828cSMichael S. Tsirkin return ERR_PTR(-EINVAL); 26116e914fc7SJason Wang tfile = file->private_data; 26126e914fc7SJason Wang if (!tfile) 261305c2828cSMichael S. Tsirkin return ERR_PTR(-EBADFD); 261454f968d6SJason Wang return &tfile->socket; 261505c2828cSMichael S. Tsirkin } 261605c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket); 261705c2828cSMichael S. Tsirkin 26181da177e4SLinus Torvalds module_init(tun_init); 26191da177e4SLinus Torvalds module_exit(tun_cleanup); 26201da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION); 26211da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT); 26221da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 26231da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR); 2624578454ffSKay Sievers MODULE_ALIAS("devname:net/tun"); 2625