11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * TUN - Universal TUN/TAP device driver. 31da177e4SLinus Torvalds * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com> 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 61da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 71da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 81da177e4SLinus Torvalds * (at your option) any later version. 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 111da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 121da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 131da177e4SLinus Torvalds * GNU General Public License for more details. 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $ 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds /* 191da177e4SLinus Torvalds * Changes: 201da177e4SLinus Torvalds * 21ff4cc3acSMike Kershaw * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14 22ff4cc3acSMike Kershaw * Add TUNSETLINK ioctl to set the link encapsulation 23ff4cc3acSMike Kershaw * 241da177e4SLinus Torvalds * Mark Smith <markzzzsmith@yahoo.com.au> 25344dc8edSJoe Perches * Use eth_random_addr() for tap MAC address. 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20 281da177e4SLinus Torvalds * Fixes in packet dropping, queue length setting and queue wakeup. 291da177e4SLinus Torvalds * Increased default tx queue length. 301da177e4SLinus Torvalds * Added ethtool API. 311da177e4SLinus Torvalds * Minor cleanups 321da177e4SLinus Torvalds * 331da177e4SLinus Torvalds * Daniel Podlejski <underley@underley.eu.org> 341da177e4SLinus Torvalds * Modifications for 2.3.99-pre5 kernel. 351da177e4SLinus Torvalds */ 361da177e4SLinus Torvalds 376b8a66eeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 386b8a66eeSJoe Perches 391da177e4SLinus Torvalds #define DRV_NAME "tun" 401da177e4SLinus Torvalds #define DRV_VERSION "1.6" 411da177e4SLinus Torvalds #define DRV_DESCRIPTION "Universal TUN/TAP device driver" 421da177e4SLinus Torvalds #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>" 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds #include <linux/module.h> 451da177e4SLinus Torvalds #include <linux/errno.h> 461da177e4SLinus Torvalds #include <linux/kernel.h> 47174cd4b1SIngo Molnar #include <linux/sched/signal.h> 481da177e4SLinus Torvalds #include <linux/major.h> 491da177e4SLinus Torvalds #include <linux/slab.h> 501da177e4SLinus Torvalds #include <linux/poll.h> 511da177e4SLinus Torvalds #include <linux/fcntl.h> 521da177e4SLinus Torvalds #include <linux/init.h> 531da177e4SLinus Torvalds #include <linux/skbuff.h> 541da177e4SLinus Torvalds #include <linux/netdevice.h> 551da177e4SLinus Torvalds #include <linux/etherdevice.h> 561da177e4SLinus Torvalds #include <linux/miscdevice.h> 571da177e4SLinus Torvalds #include <linux/ethtool.h> 581da177e4SLinus Torvalds #include <linux/rtnetlink.h> 5950857e2aSArnd Bergmann #include <linux/compat.h> 601da177e4SLinus Torvalds #include <linux/if.h> 611da177e4SLinus Torvalds #include <linux/if_arp.h> 621da177e4SLinus Torvalds #include <linux/if_ether.h> 631da177e4SLinus Torvalds #include <linux/if_tun.h> 646680ec68SJason Wang #include <linux/if_vlan.h> 651da177e4SLinus Torvalds #include <linux/crc32.h> 66d647a591SPavel Emelyanov #include <linux/nsproxy.h> 67f43798c2SRusty Russell #include <linux/virtio_net.h> 6899405162SMichael S. Tsirkin #include <linux/rcupdate.h> 69881d966bSEric W. Biederman #include <net/net_namespace.h> 7079d17604SPavel Emelyanov #include <net/netns/generic.h> 71f019a7a5SEric W. Biederman #include <net/rtnetlink.h> 7233dccbb0SHerbert Xu #include <net/sock.h> 7393e14b6dSMasatake YAMATO #include <linux/seq_file.h> 74e0b46d0eSHerbert Xu #include <linux/uio.h> 751576d986SJason Wang #include <linux/skb_array.h> 761da177e4SLinus Torvalds 777c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 781da177e4SLinus Torvalds 7914daa021SRusty Russell /* Uncomment to enable debugging */ 8014daa021SRusty Russell /* #define TUN_DEBUG 1 */ 8114daa021SRusty Russell 821da177e4SLinus Torvalds #ifdef TUN_DEBUG 831da177e4SLinus Torvalds static int debug; 8414daa021SRusty Russell 856b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...) \ 866b8a66eeSJoe Perches do { \ 876b8a66eeSJoe Perches if (tun->debug) \ 886b8a66eeSJoe Perches netdev_printk(level, tun->dev, fmt, ##args); \ 896b8a66eeSJoe Perches } while (0) 906b8a66eeSJoe Perches #define DBG1(level, fmt, args...) \ 916b8a66eeSJoe Perches do { \ 926b8a66eeSJoe Perches if (debug == 2) \ 936b8a66eeSJoe Perches printk(level fmt, ##args); \ 946b8a66eeSJoe Perches } while (0) 9514daa021SRusty Russell #else 966b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...) \ 976b8a66eeSJoe Perches do { \ 986b8a66eeSJoe Perches if (0) \ 996b8a66eeSJoe Perches netdev_printk(level, tun->dev, fmt, ##args); \ 1006b8a66eeSJoe Perches } while (0) 1016b8a66eeSJoe Perches #define DBG1(level, fmt, args...) \ 1026b8a66eeSJoe Perches do { \ 1036b8a66eeSJoe Perches if (0) \ 1046b8a66eeSJoe Perches printk(level fmt, ##args); \ 1056b8a66eeSJoe Perches } while (0) 1061da177e4SLinus Torvalds #endif 1071da177e4SLinus Torvalds 108*66ccbc9cSJason Wang #define TUN_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD) 109*66ccbc9cSJason Wang 110031f5e03SMichael S. Tsirkin /* TUN device flags */ 111031f5e03SMichael S. Tsirkin 112031f5e03SMichael S. Tsirkin /* IFF_ATTACH_QUEUE is never stored in device flags, 113031f5e03SMichael S. Tsirkin * overload it to mean fasync when stored there. 114031f5e03SMichael S. Tsirkin */ 115031f5e03SMichael S. Tsirkin #define TUN_FASYNC IFF_ATTACH_QUEUE 1161cf8e410SMichael S. Tsirkin /* High bits in flags field are unused. */ 1171cf8e410SMichael S. Tsirkin #define TUN_VNET_LE 0x80000000 1188b8e658bSGreg Kurz #define TUN_VNET_BE 0x40000000 119031f5e03SMichael S. Tsirkin 120031f5e03SMichael S. Tsirkin #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \ 1211cf8e410SMichael S. Tsirkin IFF_MULTI_QUEUE) 1220690899bSMichael S. Tsirkin #define GOODCOPY_LEN 128 1230690899bSMichael S. Tsirkin 124f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8 125f271b2ccSMax Krasnyansky struct tap_filter { 126f271b2ccSMax Krasnyansky unsigned int count; /* Number of addrs. Zero means disabled */ 127f271b2ccSMax Krasnyansky u32 mask[2]; /* Mask of the hashed addrs */ 128f271b2ccSMax Krasnyansky unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN]; 129f271b2ccSMax Krasnyansky }; 130f271b2ccSMax Krasnyansky 131baf71c5cSPankaj Gupta /* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal 132baf71c5cSPankaj Gupta * to max number of VCPUs in guest. */ 133baf71c5cSPankaj Gupta #define MAX_TAP_QUEUES 256 134b8732fb7SJason Wang #define MAX_TAP_FLOWS 4096 135c8d68e6bSJason Wang 13696442e42SJason Wang #define TUN_FLOW_EXPIRE (3 * HZ) 13796442e42SJason Wang 138608b9977SPaolo Abeni struct tun_pcpu_stats { 139608b9977SPaolo Abeni u64 rx_packets; 140608b9977SPaolo Abeni u64 rx_bytes; 141608b9977SPaolo Abeni u64 tx_packets; 142608b9977SPaolo Abeni u64 tx_bytes; 143608b9977SPaolo Abeni struct u64_stats_sync syncp; 144608b9977SPaolo Abeni u32 rx_dropped; 145608b9977SPaolo Abeni u32 tx_dropped; 146608b9977SPaolo Abeni u32 rx_frame_errors; 147608b9977SPaolo Abeni }; 148608b9977SPaolo Abeni 14954f968d6SJason Wang /* A tun_file connects an open character device to a tuntap netdevice. It 15092d4ea6eSstephen hemminger * also contains all socket related structures (except sock_fprog and tap_filter) 15154f968d6SJason Wang * to serve as one transmit queue for tuntap device. The sock_fprog and 15254f968d6SJason Wang * tap_filter were kept in tun_struct since they were used for filtering for the 15336fe8c09SRami Rosen * netdevice not for a specific queue (at least I didn't see the requirement for 15454f968d6SJason Wang * this). 1556e914fc7SJason Wang * 1566e914fc7SJason Wang * RCU usage: 15736fe8c09SRami Rosen * The tun_file and tun_struct are loosely coupled, the pointer from one to the 1586e914fc7SJason Wang * other can only be read while rcu_read_lock or rtnl_lock is held. 15954f968d6SJason Wang */ 160631ab46bSEric W. Biederman struct tun_file { 16154f968d6SJason Wang struct sock sk; 16254f968d6SJason Wang struct socket socket; 16354f968d6SJason Wang struct socket_wq wq; 1646e914fc7SJason Wang struct tun_struct __rcu *tun; 16554f968d6SJason Wang struct fasync_struct *fasync; 16654f968d6SJason Wang /* only used for fasnyc */ 16754f968d6SJason Wang unsigned int flags; 168fb7589a1SPavel Emelyanov union { 169c8d68e6bSJason Wang u16 queue_index; 170fb7589a1SPavel Emelyanov unsigned int ifindex; 171fb7589a1SPavel Emelyanov }; 1724008e97fSJason Wang struct list_head next; 1734008e97fSJason Wang struct tun_struct *detached; 1741576d986SJason Wang struct skb_array tx_array; 175*66ccbc9cSJason Wang struct page_frag alloc_frag; 176631ab46bSEric W. Biederman }; 177631ab46bSEric W. Biederman 17896442e42SJason Wang struct tun_flow_entry { 17996442e42SJason Wang struct hlist_node hash_link; 18096442e42SJason Wang struct rcu_head rcu; 18196442e42SJason Wang struct tun_struct *tun; 18296442e42SJason Wang 18396442e42SJason Wang u32 rxhash; 1849bc88939STom Herbert u32 rps_rxhash; 18596442e42SJason Wang int queue_index; 18696442e42SJason Wang unsigned long updated; 18796442e42SJason Wang }; 18896442e42SJason Wang 18996442e42SJason Wang #define TUN_NUM_FLOW_ENTRIES 1024 19096442e42SJason Wang 19154f968d6SJason Wang /* Since the socket were moved to tun_file, to preserve the behavior of persist 19236fe8c09SRami Rosen * device, socket filter, sndbuf and vnet header size were restore when the 19354f968d6SJason Wang * file were attached to a persist device. 19454f968d6SJason Wang */ 19514daa021SRusty Russell struct tun_struct { 196c8d68e6bSJason Wang struct tun_file __rcu *tfiles[MAX_TAP_QUEUES]; 197c8d68e6bSJason Wang unsigned int numqueues; 198f271b2ccSMax Krasnyansky unsigned int flags; 1990625c883SEric W. Biederman kuid_t owner; 2000625c883SEric W. Biederman kgid_t group; 20114daa021SRusty Russell 20214daa021SRusty Russell struct net_device *dev; 203c8f44affSMichał Mirosław netdev_features_t set_features; 20488255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \ 205d591a1f3SDavid S. Miller NETIF_F_TSO6) 206d9d52b51SMichael S. Tsirkin 207eaea34b2SPaolo Abeni int align; 208d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 20954f968d6SJason Wang int sndbuf; 21054f968d6SJason Wang struct tap_filter txflt; 21154f968d6SJason Wang struct sock_fprog fprog; 21254f968d6SJason Wang /* protected by rtnl lock */ 21354f968d6SJason Wang bool filter_attached; 21414daa021SRusty Russell #ifdef TUN_DEBUG 21514daa021SRusty Russell int debug; 21614daa021SRusty Russell #endif 21796442e42SJason Wang spinlock_t lock; 21896442e42SJason Wang struct hlist_head flows[TUN_NUM_FLOW_ENTRIES]; 21996442e42SJason Wang struct timer_list flow_gc_timer; 22096442e42SJason Wang unsigned long ageing_time; 2214008e97fSJason Wang unsigned int numdisabled; 2224008e97fSJason Wang struct list_head disabled; 2235dbbaf2dSPaul Moore void *security; 224b8732fb7SJason Wang u32 flow_count; 2255503fcecSJason Wang u32 rx_batched; 226608b9977SPaolo Abeni struct tun_pcpu_stats __percpu *pcpu_stats; 22714daa021SRusty Russell }; 22814daa021SRusty Russell 2298b8e658bSGreg Kurz #ifdef CONFIG_TUN_VNET_CROSS_LE 2308b8e658bSGreg Kurz static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) 2318b8e658bSGreg Kurz { 2328b8e658bSGreg Kurz return tun->flags & TUN_VNET_BE ? false : 2338b8e658bSGreg Kurz virtio_legacy_is_little_endian(); 2348b8e658bSGreg Kurz } 2358b8e658bSGreg Kurz 2368b8e658bSGreg Kurz static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) 2378b8e658bSGreg Kurz { 2388b8e658bSGreg Kurz int be = !!(tun->flags & TUN_VNET_BE); 2398b8e658bSGreg Kurz 2408b8e658bSGreg Kurz if (put_user(be, argp)) 2418b8e658bSGreg Kurz return -EFAULT; 2428b8e658bSGreg Kurz 2438b8e658bSGreg Kurz return 0; 2448b8e658bSGreg Kurz } 2458b8e658bSGreg Kurz 2468b8e658bSGreg Kurz static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) 2478b8e658bSGreg Kurz { 2488b8e658bSGreg Kurz int be; 2498b8e658bSGreg Kurz 2508b8e658bSGreg Kurz if (get_user(be, argp)) 2518b8e658bSGreg Kurz return -EFAULT; 2528b8e658bSGreg Kurz 2538b8e658bSGreg Kurz if (be) 2548b8e658bSGreg Kurz tun->flags |= TUN_VNET_BE; 2558b8e658bSGreg Kurz else 2568b8e658bSGreg Kurz tun->flags &= ~TUN_VNET_BE; 2578b8e658bSGreg Kurz 2588b8e658bSGreg Kurz return 0; 2598b8e658bSGreg Kurz } 2608b8e658bSGreg Kurz #else 2618b8e658bSGreg Kurz static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) 2628b8e658bSGreg Kurz { 2638b8e658bSGreg Kurz return virtio_legacy_is_little_endian(); 2648b8e658bSGreg Kurz } 2658b8e658bSGreg Kurz 2668b8e658bSGreg Kurz static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) 2678b8e658bSGreg Kurz { 2688b8e658bSGreg Kurz return -EINVAL; 2698b8e658bSGreg Kurz } 2708b8e658bSGreg Kurz 2718b8e658bSGreg Kurz static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) 2728b8e658bSGreg Kurz { 2738b8e658bSGreg Kurz return -EINVAL; 2748b8e658bSGreg Kurz } 2758b8e658bSGreg Kurz #endif /* CONFIG_TUN_VNET_CROSS_LE */ 2768b8e658bSGreg Kurz 27725bd55bbSGreg Kurz static inline bool tun_is_little_endian(struct tun_struct *tun) 27825bd55bbSGreg Kurz { 2797d824109SGreg Kurz return tun->flags & TUN_VNET_LE || 2808b8e658bSGreg Kurz tun_legacy_is_little_endian(tun); 28125bd55bbSGreg Kurz } 28225bd55bbSGreg Kurz 28356f0dcc5SMichael S. Tsirkin static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val) 28456f0dcc5SMichael S. Tsirkin { 28525bd55bbSGreg Kurz return __virtio16_to_cpu(tun_is_little_endian(tun), val); 28656f0dcc5SMichael S. Tsirkin } 28756f0dcc5SMichael S. Tsirkin 28856f0dcc5SMichael S. Tsirkin static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val) 28956f0dcc5SMichael S. Tsirkin { 29025bd55bbSGreg Kurz return __cpu_to_virtio16(tun_is_little_endian(tun), val); 29156f0dcc5SMichael S. Tsirkin } 29256f0dcc5SMichael S. Tsirkin 29396442e42SJason Wang static inline u32 tun_hashfn(u32 rxhash) 29496442e42SJason Wang { 29596442e42SJason Wang return rxhash & 0x3ff; 29696442e42SJason Wang } 29796442e42SJason Wang 29896442e42SJason Wang static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash) 29996442e42SJason Wang { 30096442e42SJason Wang struct tun_flow_entry *e; 30196442e42SJason Wang 302b67bfe0dSSasha Levin hlist_for_each_entry_rcu(e, head, hash_link) { 30396442e42SJason Wang if (e->rxhash == rxhash) 30496442e42SJason Wang return e; 30596442e42SJason Wang } 30696442e42SJason Wang return NULL; 30796442e42SJason Wang } 30896442e42SJason Wang 30996442e42SJason Wang static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun, 31096442e42SJason Wang struct hlist_head *head, 31196442e42SJason Wang u32 rxhash, u16 queue_index) 31296442e42SJason Wang { 3139fdc6befSEric Dumazet struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC); 3149fdc6befSEric Dumazet 31596442e42SJason Wang if (e) { 31696442e42SJason Wang tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n", 31796442e42SJason Wang rxhash, queue_index); 31896442e42SJason Wang e->updated = jiffies; 31996442e42SJason Wang e->rxhash = rxhash; 3209bc88939STom Herbert e->rps_rxhash = 0; 32196442e42SJason Wang e->queue_index = queue_index; 32296442e42SJason Wang e->tun = tun; 32396442e42SJason Wang hlist_add_head_rcu(&e->hash_link, head); 324b8732fb7SJason Wang ++tun->flow_count; 32596442e42SJason Wang } 32696442e42SJason Wang return e; 32796442e42SJason Wang } 32896442e42SJason Wang 32996442e42SJason Wang static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e) 33096442e42SJason Wang { 33196442e42SJason Wang tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n", 33296442e42SJason Wang e->rxhash, e->queue_index); 33396442e42SJason Wang hlist_del_rcu(&e->hash_link); 3349fdc6befSEric Dumazet kfree_rcu(e, rcu); 335b8732fb7SJason Wang --tun->flow_count; 33696442e42SJason Wang } 33796442e42SJason Wang 33896442e42SJason Wang static void tun_flow_flush(struct tun_struct *tun) 33996442e42SJason Wang { 34096442e42SJason Wang int i; 34196442e42SJason Wang 34296442e42SJason Wang spin_lock_bh(&tun->lock); 34396442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 34496442e42SJason Wang struct tun_flow_entry *e; 345b67bfe0dSSasha Levin struct hlist_node *n; 34696442e42SJason Wang 347b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) 34896442e42SJason Wang tun_flow_delete(tun, e); 34996442e42SJason Wang } 35096442e42SJason Wang spin_unlock_bh(&tun->lock); 35196442e42SJason Wang } 35296442e42SJason Wang 35396442e42SJason Wang static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index) 35496442e42SJason Wang { 35596442e42SJason Wang int i; 35696442e42SJason Wang 35796442e42SJason Wang spin_lock_bh(&tun->lock); 35896442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 35996442e42SJason Wang struct tun_flow_entry *e; 360b67bfe0dSSasha Levin struct hlist_node *n; 36196442e42SJason Wang 362b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { 36396442e42SJason Wang if (e->queue_index == queue_index) 36496442e42SJason Wang tun_flow_delete(tun, e); 36596442e42SJason Wang } 36696442e42SJason Wang } 36796442e42SJason Wang spin_unlock_bh(&tun->lock); 36896442e42SJason Wang } 36996442e42SJason Wang 37096442e42SJason Wang static void tun_flow_cleanup(unsigned long data) 37196442e42SJason Wang { 37296442e42SJason Wang struct tun_struct *tun = (struct tun_struct *)data; 37396442e42SJason Wang unsigned long delay = tun->ageing_time; 37496442e42SJason Wang unsigned long next_timer = jiffies + delay; 37596442e42SJason Wang unsigned long count = 0; 37696442e42SJason Wang int i; 37796442e42SJason Wang 37896442e42SJason Wang tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n"); 37996442e42SJason Wang 38096442e42SJason Wang spin_lock_bh(&tun->lock); 38196442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 38296442e42SJason Wang struct tun_flow_entry *e; 383b67bfe0dSSasha Levin struct hlist_node *n; 38496442e42SJason Wang 385b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { 38696442e42SJason Wang unsigned long this_timer; 38796442e42SJason Wang count++; 38896442e42SJason Wang this_timer = e->updated + delay; 38996442e42SJason Wang if (time_before_eq(this_timer, jiffies)) 39096442e42SJason Wang tun_flow_delete(tun, e); 39196442e42SJason Wang else if (time_before(this_timer, next_timer)) 39296442e42SJason Wang next_timer = this_timer; 39396442e42SJason Wang } 39496442e42SJason Wang } 39596442e42SJason Wang 39696442e42SJason Wang if (count) 39796442e42SJason Wang mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer)); 39896442e42SJason Wang spin_unlock_bh(&tun->lock); 39996442e42SJason Wang } 40096442e42SJason Wang 40149974420SEric Dumazet static void tun_flow_update(struct tun_struct *tun, u32 rxhash, 4029e85722dSJason Wang struct tun_file *tfile) 40396442e42SJason Wang { 40496442e42SJason Wang struct hlist_head *head; 40596442e42SJason Wang struct tun_flow_entry *e; 40696442e42SJason Wang unsigned long delay = tun->ageing_time; 4079e85722dSJason Wang u16 queue_index = tfile->queue_index; 40896442e42SJason Wang 40996442e42SJason Wang if (!rxhash) 41096442e42SJason Wang return; 41196442e42SJason Wang else 41296442e42SJason Wang head = &tun->flows[tun_hashfn(rxhash)]; 41396442e42SJason Wang 41496442e42SJason Wang rcu_read_lock(); 41596442e42SJason Wang 4169e85722dSJason Wang /* We may get a very small possibility of OOO during switching, not 4179e85722dSJason Wang * worth to optimize.*/ 4189e85722dSJason Wang if (tun->numqueues == 1 || tfile->detached) 41996442e42SJason Wang goto unlock; 42096442e42SJason Wang 42196442e42SJason Wang e = tun_flow_find(head, rxhash); 42296442e42SJason Wang if (likely(e)) { 42396442e42SJason Wang /* TODO: keep queueing to old queue until it's empty? */ 42496442e42SJason Wang e->queue_index = queue_index; 42596442e42SJason Wang e->updated = jiffies; 4269bc88939STom Herbert sock_rps_record_flow_hash(e->rps_rxhash); 42796442e42SJason Wang } else { 42896442e42SJason Wang spin_lock_bh(&tun->lock); 429b8732fb7SJason Wang if (!tun_flow_find(head, rxhash) && 430b8732fb7SJason Wang tun->flow_count < MAX_TAP_FLOWS) 43196442e42SJason Wang tun_flow_create(tun, head, rxhash, queue_index); 43296442e42SJason Wang 43396442e42SJason Wang if (!timer_pending(&tun->flow_gc_timer)) 43496442e42SJason Wang mod_timer(&tun->flow_gc_timer, 43596442e42SJason Wang round_jiffies_up(jiffies + delay)); 43696442e42SJason Wang spin_unlock_bh(&tun->lock); 43796442e42SJason Wang } 43896442e42SJason Wang 43996442e42SJason Wang unlock: 44096442e42SJason Wang rcu_read_unlock(); 44196442e42SJason Wang } 44296442e42SJason Wang 4439bc88939STom Herbert /** 4449bc88939STom Herbert * Save the hash received in the stack receive path and update the 4459bc88939STom Herbert * flow_hash table accordingly. 4469bc88939STom Herbert */ 4479bc88939STom Herbert static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash) 4489bc88939STom Herbert { 449567e4b79SEric Dumazet if (unlikely(e->rps_rxhash != hash)) 4509bc88939STom Herbert e->rps_rxhash = hash; 4519bc88939STom Herbert } 4529bc88939STom Herbert 453c8d68e6bSJason Wang /* We try to identify a flow through its rxhash first. The reason that 45492d4ea6eSstephen hemminger * we do not check rxq no. is because some cards(e.g 82599), chooses 455c8d68e6bSJason Wang * the rxq based on the txq where the last packet of the flow comes. As 456c8d68e6bSJason Wang * the userspace application move between processors, we may get a 457c8d68e6bSJason Wang * different rxq no. here. If we could not get rxhash, then we would 458c8d68e6bSJason Wang * hope the rxq no. may help here. 459c8d68e6bSJason Wang */ 460f663dd9aSJason Wang static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb, 46199932d4fSDaniel Borkmann void *accel_priv, select_queue_fallback_t fallback) 462c8d68e6bSJason Wang { 463c8d68e6bSJason Wang struct tun_struct *tun = netdev_priv(dev); 46496442e42SJason Wang struct tun_flow_entry *e; 465c8d68e6bSJason Wang u32 txq = 0; 466c8d68e6bSJason Wang u32 numqueues = 0; 467c8d68e6bSJason Wang 468c8d68e6bSJason Wang rcu_read_lock(); 46992bb73eaSJason Wang numqueues = ACCESS_ONCE(tun->numqueues); 470c8d68e6bSJason Wang 471feec084aSJason Wang txq = __skb_get_hash_symmetric(skb); 472c8d68e6bSJason Wang if (txq) { 47396442e42SJason Wang e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq); 4749bc88939STom Herbert if (e) { 4759bc88939STom Herbert tun_flow_save_rps_rxhash(e, txq); 476fbe4d456SZhi Yong Wu txq = e->queue_index; 4779bc88939STom Herbert } else 478c8d68e6bSJason Wang /* use multiply and shift instead of expensive divide */ 479c8d68e6bSJason Wang txq = ((u64)txq * numqueues) >> 32; 480c8d68e6bSJason Wang } else if (likely(skb_rx_queue_recorded(skb))) { 481c8d68e6bSJason Wang txq = skb_get_rx_queue(skb); 482c8d68e6bSJason Wang while (unlikely(txq >= numqueues)) 483c8d68e6bSJason Wang txq -= numqueues; 484c8d68e6bSJason Wang } 485c8d68e6bSJason Wang 486c8d68e6bSJason Wang rcu_read_unlock(); 487c8d68e6bSJason Wang return txq; 488c8d68e6bSJason Wang } 489c8d68e6bSJason Wang 490cde8b15fSJason Wang static inline bool tun_not_capable(struct tun_struct *tun) 491cde8b15fSJason Wang { 492cde8b15fSJason Wang const struct cred *cred = current_cred(); 493c260b772SEric W. Biederman struct net *net = dev_net(tun->dev); 494cde8b15fSJason Wang 495cde8b15fSJason Wang return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) || 496cde8b15fSJason Wang (gid_valid(tun->group) && !in_egroup_p(tun->group))) && 497c260b772SEric W. Biederman !ns_capable(net->user_ns, CAP_NET_ADMIN); 498cde8b15fSJason Wang } 499cde8b15fSJason Wang 500c8d68e6bSJason Wang static void tun_set_real_num_queues(struct tun_struct *tun) 501c8d68e6bSJason Wang { 502c8d68e6bSJason Wang netif_set_real_num_tx_queues(tun->dev, tun->numqueues); 503c8d68e6bSJason Wang netif_set_real_num_rx_queues(tun->dev, tun->numqueues); 504c8d68e6bSJason Wang } 505c8d68e6bSJason Wang 5064008e97fSJason Wang static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) 5074008e97fSJason Wang { 5084008e97fSJason Wang tfile->detached = tun; 5094008e97fSJason Wang list_add_tail(&tfile->next, &tun->disabled); 5104008e97fSJason Wang ++tun->numdisabled; 5114008e97fSJason Wang } 5124008e97fSJason Wang 513d32649d1SJason Wang static struct tun_struct *tun_enable_queue(struct tun_file *tfile) 5144008e97fSJason Wang { 5154008e97fSJason Wang struct tun_struct *tun = tfile->detached; 5164008e97fSJason Wang 5174008e97fSJason Wang tfile->detached = NULL; 5184008e97fSJason Wang list_del_init(&tfile->next); 5194008e97fSJason Wang --tun->numdisabled; 5204008e97fSJason Wang return tun; 5214008e97fSJason Wang } 5224008e97fSJason Wang 5234bfb0513SJason Wang static void tun_queue_purge(struct tun_file *tfile) 5244bfb0513SJason Wang { 5251576d986SJason Wang struct sk_buff *skb; 5261576d986SJason Wang 5271576d986SJason Wang while ((skb = skb_array_consume(&tfile->tx_array)) != NULL) 5281576d986SJason Wang kfree_skb(skb); 5291576d986SJason Wang 5305503fcecSJason Wang skb_queue_purge(&tfile->sk.sk_write_queue); 5314bfb0513SJason Wang skb_queue_purge(&tfile->sk.sk_error_queue); 5324bfb0513SJason Wang } 5334bfb0513SJason Wang 534c8d68e6bSJason Wang static void __tun_detach(struct tun_file *tfile, bool clean) 535c8d68e6bSJason Wang { 536c8d68e6bSJason Wang struct tun_file *ntfile; 537c8d68e6bSJason Wang struct tun_struct *tun; 538c8d68e6bSJason Wang 539b8deabd3SJason Wang tun = rtnl_dereference(tfile->tun); 540b8deabd3SJason Wang 5419e85722dSJason Wang if (tun && !tfile->detached) { 542c8d68e6bSJason Wang u16 index = tfile->queue_index; 543c8d68e6bSJason Wang BUG_ON(index >= tun->numqueues); 544c8d68e6bSJason Wang 545c8d68e6bSJason Wang rcu_assign_pointer(tun->tfiles[index], 546c8d68e6bSJason Wang tun->tfiles[tun->numqueues - 1]); 547b8deabd3SJason Wang ntfile = rtnl_dereference(tun->tfiles[index]); 548c8d68e6bSJason Wang ntfile->queue_index = index; 549c8d68e6bSJason Wang 550c8d68e6bSJason Wang --tun->numqueues; 5519e85722dSJason Wang if (clean) { 552c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 553c8d68e6bSJason Wang sock_put(&tfile->sk); 5549e85722dSJason Wang } else 5554008e97fSJason Wang tun_disable_queue(tun, tfile); 556c8d68e6bSJason Wang 557c8d68e6bSJason Wang synchronize_net(); 55896442e42SJason Wang tun_flow_delete_by_queue(tun, tun->numqueues + 1); 559c8d68e6bSJason Wang /* Drop read queue */ 5604bfb0513SJason Wang tun_queue_purge(tfile); 561c8d68e6bSJason Wang tun_set_real_num_queues(tun); 562dd38bd85SJason Wang } else if (tfile->detached && clean) { 5634008e97fSJason Wang tun = tun_enable_queue(tfile); 564dd38bd85SJason Wang sock_put(&tfile->sk); 565dd38bd85SJason Wang } 566c8d68e6bSJason Wang 567c8d68e6bSJason Wang if (clean) { 568af668b3cSMichael S. Tsirkin if (tun && tun->numqueues == 0 && tun->numdisabled == 0) { 569af668b3cSMichael S. Tsirkin netif_carrier_off(tun->dev); 570af668b3cSMichael S. Tsirkin 57140630b82SMichael S. Tsirkin if (!(tun->flags & IFF_PERSIST) && 572af668b3cSMichael S. Tsirkin tun->dev->reg_state == NETREG_REGISTERED) 5734008e97fSJason Wang unregister_netdevice(tun->dev); 574af668b3cSMichael S. Tsirkin } 5751576d986SJason Wang if (tun) 5761576d986SJason Wang skb_array_cleanup(&tfile->tx_array); 577*66ccbc9cSJason Wang if (tfile->alloc_frag.page) 578*66ccbc9cSJason Wang put_page(tfile->alloc_frag.page); 579140e807dSEric W. Biederman sock_put(&tfile->sk); 580c8d68e6bSJason Wang } 581c8d68e6bSJason Wang } 582c8d68e6bSJason Wang 583c8d68e6bSJason Wang static void tun_detach(struct tun_file *tfile, bool clean) 584c8d68e6bSJason Wang { 585c8d68e6bSJason Wang rtnl_lock(); 586c8d68e6bSJason Wang __tun_detach(tfile, clean); 587c8d68e6bSJason Wang rtnl_unlock(); 588c8d68e6bSJason Wang } 589c8d68e6bSJason Wang 590c8d68e6bSJason Wang static void tun_detach_all(struct net_device *dev) 591c8d68e6bSJason Wang { 592c8d68e6bSJason Wang struct tun_struct *tun = netdev_priv(dev); 5934008e97fSJason Wang struct tun_file *tfile, *tmp; 594c8d68e6bSJason Wang int i, n = tun->numqueues; 595c8d68e6bSJason Wang 596c8d68e6bSJason Wang for (i = 0; i < n; i++) { 597b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 598c8d68e6bSJason Wang BUG_ON(!tfile); 599addf8fc4SJason Wang tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; 6009e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 601c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 602c8d68e6bSJason Wang --tun->numqueues; 603c8d68e6bSJason Wang } 6049e85722dSJason Wang list_for_each_entry(tfile, &tun->disabled, next) { 605addf8fc4SJason Wang tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; 6069e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 607c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 6089e85722dSJason Wang } 609c8d68e6bSJason Wang BUG_ON(tun->numqueues != 0); 610c8d68e6bSJason Wang 611c8d68e6bSJason Wang synchronize_net(); 612c8d68e6bSJason Wang for (i = 0; i < n; i++) { 613b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 614c8d68e6bSJason Wang /* Drop read queue */ 6154bfb0513SJason Wang tun_queue_purge(tfile); 616c8d68e6bSJason Wang sock_put(&tfile->sk); 617c8d68e6bSJason Wang } 6184008e97fSJason Wang list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { 6194008e97fSJason Wang tun_enable_queue(tfile); 6204bfb0513SJason Wang tun_queue_purge(tfile); 6214008e97fSJason Wang sock_put(&tfile->sk); 6224008e97fSJason Wang } 6234008e97fSJason Wang BUG_ON(tun->numdisabled != 0); 624dd38bd85SJason Wang 62540630b82SMichael S. Tsirkin if (tun->flags & IFF_PERSIST) 626dd38bd85SJason Wang module_put(THIS_MODULE); 627c8d68e6bSJason Wang } 628c8d68e6bSJason Wang 629849c9b6fSPavel Emelyanov static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter) 630a7385ba2SEric W. Biederman { 631631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 6321576d986SJason Wang struct net_device *dev = tun->dev; 63338231b7aSEric W. Biederman int err; 634a7385ba2SEric W. Biederman 6355dbbaf2dSPaul Moore err = security_tun_dev_attach(tfile->socket.sk, tun->security); 6365dbbaf2dSPaul Moore if (err < 0) 6375dbbaf2dSPaul Moore goto out; 6385dbbaf2dSPaul Moore 63938231b7aSEric W. Biederman err = -EINVAL; 6409e85722dSJason Wang if (rtnl_dereference(tfile->tun) && !tfile->detached) 64138231b7aSEric W. Biederman goto out; 64238231b7aSEric W. Biederman 64338231b7aSEric W. Biederman err = -EBUSY; 64440630b82SMichael S. Tsirkin if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1) 645c8d68e6bSJason Wang goto out; 646c8d68e6bSJason Wang 647c8d68e6bSJason Wang err = -E2BIG; 6484008e97fSJason Wang if (!tfile->detached && 6494008e97fSJason Wang tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES) 65038231b7aSEric W. Biederman goto out; 65138231b7aSEric W. Biederman 65238231b7aSEric W. Biederman err = 0; 65354f968d6SJason Wang 65492d4ea6eSstephen hemminger /* Re-attach the filter to persist device */ 655849c9b6fSPavel Emelyanov if (!skip_filter && (tun->filter_attached == true)) { 6568ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 6578ced425eSHannes Frederic Sowa err = sk_attach_filter(&tun->fprog, tfile->socket.sk); 6588ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 65954f968d6SJason Wang if (!err) 66054f968d6SJason Wang goto out; 66154f968d6SJason Wang } 6621576d986SJason Wang 6631576d986SJason Wang if (!tfile->detached && 6641576d986SJason Wang skb_array_init(&tfile->tx_array, dev->tx_queue_len, GFP_KERNEL)) { 6651576d986SJason Wang err = -ENOMEM; 6661576d986SJason Wang goto out; 6671576d986SJason Wang } 6681576d986SJason Wang 669c8d68e6bSJason Wang tfile->queue_index = tun->numqueues; 670addf8fc4SJason Wang tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN; 6716e914fc7SJason Wang rcu_assign_pointer(tfile->tun, tun); 672c8d68e6bSJason Wang rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); 673c8d68e6bSJason Wang tun->numqueues++; 674c8d68e6bSJason Wang 6754008e97fSJason Wang if (tfile->detached) 6764008e97fSJason Wang tun_enable_queue(tfile); 6774008e97fSJason Wang else 6784008e97fSJason Wang sock_hold(&tfile->sk); 6794008e97fSJason Wang 680c8d68e6bSJason Wang tun_set_real_num_queues(tun); 681c8d68e6bSJason Wang 682c8d68e6bSJason Wang /* device is allowed to go away first, so no need to hold extra 683c8d68e6bSJason Wang * refcnt. 684c8d68e6bSJason Wang */ 685a7385ba2SEric W. Biederman 68638231b7aSEric W. Biederman out: 68738231b7aSEric W. Biederman return err; 688a7385ba2SEric W. Biederman } 689a7385ba2SEric W. Biederman 690631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile) 691631ab46bSEric W. Biederman { 6926e914fc7SJason Wang struct tun_struct *tun; 693c70f1829SEric W. Biederman 6946e914fc7SJason Wang rcu_read_lock(); 6956e914fc7SJason Wang tun = rcu_dereference(tfile->tun); 6966e914fc7SJason Wang if (tun) 6976e914fc7SJason Wang dev_hold(tun->dev); 6986e914fc7SJason Wang rcu_read_unlock(); 699c70f1829SEric W. Biederman 700c70f1829SEric W. Biederman return tun; 701631ab46bSEric W. Biederman } 702631ab46bSEric W. Biederman 703631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file) 704631ab46bSEric W. Biederman { 705631ab46bSEric W. Biederman return __tun_get(file->private_data); 706631ab46bSEric W. Biederman } 707631ab46bSEric W. Biederman 708631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun) 709631ab46bSEric W. Biederman { 7106e914fc7SJason Wang dev_put(tun->dev); 711631ab46bSEric W. Biederman } 712631ab46bSEric W. Biederman 7136b8a66eeSJoe Perches /* TAP filtering */ 714f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr) 715f271b2ccSMax Krasnyansky { 716f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 717f271b2ccSMax Krasnyansky mask[n >> 5] |= (1 << (n & 31)); 718f271b2ccSMax Krasnyansky } 719f271b2ccSMax Krasnyansky 720f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) 721f271b2ccSMax Krasnyansky { 722f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 723f271b2ccSMax Krasnyansky return mask[n >> 5] & (1 << (n & 31)); 724f271b2ccSMax Krasnyansky } 725f271b2ccSMax Krasnyansky 726f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg) 727f271b2ccSMax Krasnyansky { 728f271b2ccSMax Krasnyansky struct { u8 u[ETH_ALEN]; } *addr; 729f271b2ccSMax Krasnyansky struct tun_filter uf; 730f271b2ccSMax Krasnyansky int err, alen, n, nexact; 731f271b2ccSMax Krasnyansky 732f271b2ccSMax Krasnyansky if (copy_from_user(&uf, arg, sizeof(uf))) 733f271b2ccSMax Krasnyansky return -EFAULT; 734f271b2ccSMax Krasnyansky 735f271b2ccSMax Krasnyansky if (!uf.count) { 736f271b2ccSMax Krasnyansky /* Disabled */ 737f271b2ccSMax Krasnyansky filter->count = 0; 738f271b2ccSMax Krasnyansky return 0; 739f271b2ccSMax Krasnyansky } 740f271b2ccSMax Krasnyansky 741f271b2ccSMax Krasnyansky alen = ETH_ALEN * uf.count; 74228e8190dSMarkus Elfring addr = memdup_user(arg + sizeof(uf), alen); 74328e8190dSMarkus Elfring if (IS_ERR(addr)) 74428e8190dSMarkus Elfring return PTR_ERR(addr); 745f271b2ccSMax Krasnyansky 746f271b2ccSMax Krasnyansky /* The filter is updated without holding any locks. Which is 747f271b2ccSMax Krasnyansky * perfectly safe. We disable it first and in the worst 748f271b2ccSMax Krasnyansky * case we'll accept a few undesired packets. */ 749f271b2ccSMax Krasnyansky filter->count = 0; 750f271b2ccSMax Krasnyansky wmb(); 751f271b2ccSMax Krasnyansky 752f271b2ccSMax Krasnyansky /* Use first set of addresses as an exact filter */ 753f271b2ccSMax Krasnyansky for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++) 754f271b2ccSMax Krasnyansky memcpy(filter->addr[n], addr[n].u, ETH_ALEN); 755f271b2ccSMax Krasnyansky 756f271b2ccSMax Krasnyansky nexact = n; 757f271b2ccSMax Krasnyansky 758cfbf84fcSAlex Williamson /* Remaining multicast addresses are hashed, 759cfbf84fcSAlex Williamson * unicast will leave the filter disabled. */ 760f271b2ccSMax Krasnyansky memset(filter->mask, 0, sizeof(filter->mask)); 761cfbf84fcSAlex Williamson for (; n < uf.count; n++) { 762cfbf84fcSAlex Williamson if (!is_multicast_ether_addr(addr[n].u)) { 763cfbf84fcSAlex Williamson err = 0; /* no filter */ 7643b8d2a69SMarkus Elfring goto free_addr; 765cfbf84fcSAlex Williamson } 766f271b2ccSMax Krasnyansky addr_hash_set(filter->mask, addr[n].u); 767cfbf84fcSAlex Williamson } 768f271b2ccSMax Krasnyansky 769f271b2ccSMax Krasnyansky /* For ALLMULTI just set the mask to all ones. 770f271b2ccSMax Krasnyansky * This overrides the mask populated above. */ 771f271b2ccSMax Krasnyansky if ((uf.flags & TUN_FLT_ALLMULTI)) 772f271b2ccSMax Krasnyansky memset(filter->mask, ~0, sizeof(filter->mask)); 773f271b2ccSMax Krasnyansky 774f271b2ccSMax Krasnyansky /* Now enable the filter */ 775f271b2ccSMax Krasnyansky wmb(); 776f271b2ccSMax Krasnyansky filter->count = nexact; 777f271b2ccSMax Krasnyansky 778f271b2ccSMax Krasnyansky /* Return the number of exact filters */ 779f271b2ccSMax Krasnyansky err = nexact; 7803b8d2a69SMarkus Elfring free_addr: 781f271b2ccSMax Krasnyansky kfree(addr); 782f271b2ccSMax Krasnyansky return err; 783f271b2ccSMax Krasnyansky } 784f271b2ccSMax Krasnyansky 785f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */ 786f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb) 787f271b2ccSMax Krasnyansky { 788f271b2ccSMax Krasnyansky /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect 789f271b2ccSMax Krasnyansky * at this point. */ 790f271b2ccSMax Krasnyansky struct ethhdr *eh = (struct ethhdr *) skb->data; 791f271b2ccSMax Krasnyansky int i; 792f271b2ccSMax Krasnyansky 793f271b2ccSMax Krasnyansky /* Exact match */ 794f271b2ccSMax Krasnyansky for (i = 0; i < filter->count; i++) 7952e42e474SJoe Perches if (ether_addr_equal(eh->h_dest, filter->addr[i])) 796f271b2ccSMax Krasnyansky return 1; 797f271b2ccSMax Krasnyansky 798f271b2ccSMax Krasnyansky /* Inexact match (multicast only) */ 799f271b2ccSMax Krasnyansky if (is_multicast_ether_addr(eh->h_dest)) 800f271b2ccSMax Krasnyansky return addr_hash_test(filter->mask, eh->h_dest); 801f271b2ccSMax Krasnyansky 802f271b2ccSMax Krasnyansky return 0; 803f271b2ccSMax Krasnyansky } 804f271b2ccSMax Krasnyansky 805f271b2ccSMax Krasnyansky /* 806f271b2ccSMax Krasnyansky * Checks whether the packet is accepted or not. 807f271b2ccSMax Krasnyansky * Returns: 0 - drop, !=0 - accept 808f271b2ccSMax Krasnyansky */ 809f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb) 810f271b2ccSMax Krasnyansky { 811f271b2ccSMax Krasnyansky if (!filter->count) 812f271b2ccSMax Krasnyansky return 1; 813f271b2ccSMax Krasnyansky 814f271b2ccSMax Krasnyansky return run_filter(filter, skb); 815f271b2ccSMax Krasnyansky } 816f271b2ccSMax Krasnyansky 8171da177e4SLinus Torvalds /* Network device part of the driver */ 8181da177e4SLinus Torvalds 8197282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops; 8201da177e4SLinus Torvalds 821c70f1829SEric W. Biederman /* Net device detach from fd. */ 822c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev) 823c70f1829SEric W. Biederman { 824c8d68e6bSJason Wang tun_detach_all(dev); 825c70f1829SEric W. Biederman } 826c70f1829SEric W. Biederman 8271da177e4SLinus Torvalds /* Net device open. */ 8281da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev) 8291da177e4SLinus Torvalds { 830b20e2d54SHannes Frederic Sowa struct tun_struct *tun = netdev_priv(dev); 831b20e2d54SHannes Frederic Sowa int i; 832b20e2d54SHannes Frederic Sowa 833c8d68e6bSJason Wang netif_tx_start_all_queues(dev); 834b20e2d54SHannes Frederic Sowa 835b20e2d54SHannes Frederic Sowa for (i = 0; i < tun->numqueues; i++) { 836b20e2d54SHannes Frederic Sowa struct tun_file *tfile; 837b20e2d54SHannes Frederic Sowa 838b20e2d54SHannes Frederic Sowa tfile = rtnl_dereference(tun->tfiles[i]); 839b20e2d54SHannes Frederic Sowa tfile->socket.sk->sk_write_space(tfile->socket.sk); 840b20e2d54SHannes Frederic Sowa } 841b20e2d54SHannes Frederic Sowa 8421da177e4SLinus Torvalds return 0; 8431da177e4SLinus Torvalds } 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds /* Net device close. */ 8461da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev) 8471da177e4SLinus Torvalds { 848c8d68e6bSJason Wang netif_tx_stop_all_queues(dev); 8491da177e4SLinus Torvalds return 0; 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds 8521da177e4SLinus Torvalds /* Net device start xmit */ 853424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) 8541da177e4SLinus Torvalds { 8551da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 856c8d68e6bSJason Wang int txq = skb->queue_mapping; 8576e914fc7SJason Wang struct tun_file *tfile; 858fa35864eSDominic Curran u32 numqueues = 0; 8591da177e4SLinus Torvalds 8606e914fc7SJason Wang rcu_read_lock(); 861c8d68e6bSJason Wang tfile = rcu_dereference(tun->tfiles[txq]); 862fa35864eSDominic Curran numqueues = ACCESS_ONCE(tun->numqueues); 863c8d68e6bSJason Wang 8641da177e4SLinus Torvalds /* Drop packet if interface is not attached */ 865fa35864eSDominic Curran if (txq >= numqueues) 8661da177e4SLinus Torvalds goto drop; 8671da177e4SLinus Torvalds 8683df97ba8SJason Wang #ifdef CONFIG_RPS 8693df97ba8SJason Wang if (numqueues == 1 && static_key_false(&rps_needed)) { 8709bc88939STom Herbert /* Select queue was not called for the skbuff, so we extract the 8719bc88939STom Herbert * RPS hash and save it into the flow_table here. 8729bc88939STom Herbert */ 8739bc88939STom Herbert __u32 rxhash; 8749bc88939STom Herbert 875feec084aSJason Wang rxhash = __skb_get_hash_symmetric(skb); 8769bc88939STom Herbert if (rxhash) { 8779bc88939STom Herbert struct tun_flow_entry *e; 8789bc88939STom Herbert e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)], 8799bc88939STom Herbert rxhash); 8809bc88939STom Herbert if (e) 8819bc88939STom Herbert tun_flow_save_rps_rxhash(e, rxhash); 8829bc88939STom Herbert } 8839bc88939STom Herbert } 8843df97ba8SJason Wang #endif 8859bc88939STom Herbert 8866e914fc7SJason Wang tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len); 8876e914fc7SJason Wang 888c8d68e6bSJason Wang BUG_ON(!tfile); 889c8d68e6bSJason Wang 890f271b2ccSMax Krasnyansky /* Drop if the filter does not like it. 891f271b2ccSMax Krasnyansky * This is a noop if the filter is disabled. 892f271b2ccSMax Krasnyansky * Filter can be enabled only for the TAP devices. */ 893f271b2ccSMax Krasnyansky if (!check_filter(&tun->txflt, skb)) 894f271b2ccSMax Krasnyansky goto drop; 895f271b2ccSMax Krasnyansky 89654f968d6SJason Wang if (tfile->socket.sk->sk_filter && 89754f968d6SJason Wang sk_filter(tfile->socket.sk, skb)) 89899405162SMichael S. Tsirkin goto drop; 89999405162SMichael S. Tsirkin 9001f8b977aSWillem de Bruijn if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) 9017bf66305SJason Wang goto drop; 9027bf66305SJason Wang 9037b996243SSoheil Hassas Yeganeh skb_tx_timestamp(skb); 904eda29772SRichard Cochran 9050110d6f2SMichael S. Tsirkin /* Orphan the skb - required as we might hang on to it 9067bf66305SJason Wang * for indefinite time. 9077bf66305SJason Wang */ 9080110d6f2SMichael S. Tsirkin skb_orphan(skb); 9090110d6f2SMichael S. Tsirkin 910f8af75f3SEric Dumazet nf_reset(skb); 911f8af75f3SEric Dumazet 9121576d986SJason Wang if (skb_array_produce(&tfile->tx_array, skb)) 9131576d986SJason Wang goto drop; 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds /* Notify and wake up reader process */ 91654f968d6SJason Wang if (tfile->flags & TUN_FASYNC) 91754f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_IN); 9189e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 9196e914fc7SJason Wang 9206e914fc7SJason Wang rcu_read_unlock(); 9216ed10654SPatrick McHardy return NETDEV_TX_OK; 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds drop: 924608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->tx_dropped); 925149d36f7SMichael S. Tsirkin skb_tx_error(skb); 9261da177e4SLinus Torvalds kfree_skb(skb); 9276e914fc7SJason Wang rcu_read_unlock(); 928baeababbSJason Wang return NET_XMIT_DROP; 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds 931f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev) 9321da177e4SLinus Torvalds { 933f271b2ccSMax Krasnyansky /* 934f271b2ccSMax Krasnyansky * This callback is supposed to deal with mc filter in 935f271b2ccSMax Krasnyansky * _rx_ path and has nothing to do with the _tx_ path. 936f271b2ccSMax Krasnyansky * In rx path we always accept everything userspace gives us. 937f271b2ccSMax Krasnyansky */ 9381da177e4SLinus Torvalds } 9391da177e4SLinus Torvalds 940c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev, 941c8f44affSMichał Mirosław netdev_features_t features) 94288255375SMichał Mirosław { 94388255375SMichał Mirosław struct tun_struct *tun = netdev_priv(dev); 94488255375SMichał Mirosław 94588255375SMichał Mirosław return (features & tun->set_features) | (features & ~TUN_USER_FEATURES); 94688255375SMichał Mirosław } 947bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 948bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev) 949bebd097aSNeil Horman { 950bebd097aSNeil Horman /* 951bebd097aSNeil Horman * Tun only receives frames when: 952bebd097aSNeil Horman * 1) the char device endpoint gets data from user space 953bebd097aSNeil Horman * 2) the tun socket gets a sendmsg call from user space 95492d4ea6eSstephen hemminger * Since both of those are synchronous operations, we are guaranteed 955bebd097aSNeil Horman * never to have pending data when we poll for it 95692d4ea6eSstephen hemminger * so there is nothing to do here but return. 957bebd097aSNeil Horman * We need this though so netpoll recognizes us as an interface that 958bebd097aSNeil Horman * supports polling, which enables bridge devices in virt setups to 959bebd097aSNeil Horman * still use netconsole 960bebd097aSNeil Horman */ 961bebd097aSNeil Horman return; 962bebd097aSNeil Horman } 963bebd097aSNeil Horman #endif 964eaea34b2SPaolo Abeni 965eaea34b2SPaolo Abeni static void tun_set_headroom(struct net_device *dev, int new_hr) 966eaea34b2SPaolo Abeni { 967eaea34b2SPaolo Abeni struct tun_struct *tun = netdev_priv(dev); 968eaea34b2SPaolo Abeni 969eaea34b2SPaolo Abeni if (new_hr < NET_SKB_PAD) 970eaea34b2SPaolo Abeni new_hr = NET_SKB_PAD; 971eaea34b2SPaolo Abeni 972eaea34b2SPaolo Abeni tun->align = new_hr; 973eaea34b2SPaolo Abeni } 974eaea34b2SPaolo Abeni 975bc1f4470Sstephen hemminger static void 976608b9977SPaolo Abeni tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 977608b9977SPaolo Abeni { 978608b9977SPaolo Abeni u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0; 979608b9977SPaolo Abeni struct tun_struct *tun = netdev_priv(dev); 980608b9977SPaolo Abeni struct tun_pcpu_stats *p; 981608b9977SPaolo Abeni int i; 982608b9977SPaolo Abeni 983608b9977SPaolo Abeni for_each_possible_cpu(i) { 984608b9977SPaolo Abeni u64 rxpackets, rxbytes, txpackets, txbytes; 985608b9977SPaolo Abeni unsigned int start; 986608b9977SPaolo Abeni 987608b9977SPaolo Abeni p = per_cpu_ptr(tun->pcpu_stats, i); 988608b9977SPaolo Abeni do { 989608b9977SPaolo Abeni start = u64_stats_fetch_begin(&p->syncp); 990608b9977SPaolo Abeni rxpackets = p->rx_packets; 991608b9977SPaolo Abeni rxbytes = p->rx_bytes; 992608b9977SPaolo Abeni txpackets = p->tx_packets; 993608b9977SPaolo Abeni txbytes = p->tx_bytes; 994608b9977SPaolo Abeni } while (u64_stats_fetch_retry(&p->syncp, start)); 995608b9977SPaolo Abeni 996608b9977SPaolo Abeni stats->rx_packets += rxpackets; 997608b9977SPaolo Abeni stats->rx_bytes += rxbytes; 998608b9977SPaolo Abeni stats->tx_packets += txpackets; 999608b9977SPaolo Abeni stats->tx_bytes += txbytes; 1000608b9977SPaolo Abeni 1001608b9977SPaolo Abeni /* u32 counters */ 1002608b9977SPaolo Abeni rx_dropped += p->rx_dropped; 1003608b9977SPaolo Abeni rx_frame_errors += p->rx_frame_errors; 1004608b9977SPaolo Abeni tx_dropped += p->tx_dropped; 1005608b9977SPaolo Abeni } 1006608b9977SPaolo Abeni stats->rx_dropped = rx_dropped; 1007608b9977SPaolo Abeni stats->rx_frame_errors = rx_frame_errors; 1008608b9977SPaolo Abeni stats->tx_dropped = tx_dropped; 1009608b9977SPaolo Abeni } 1010608b9977SPaolo Abeni 1011758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = { 1012c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 1013758e43b7SStephen Hemminger .ndo_open = tun_net_open, 1014758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 101500829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 101688255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 1017c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 1018bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 1019bebd097aSNeil Horman .ndo_poll_controller = tun_poll_controller, 1020bebd097aSNeil Horman #endif 1021eaea34b2SPaolo Abeni .ndo_set_rx_headroom = tun_set_headroom, 1022608b9977SPaolo Abeni .ndo_get_stats64 = tun_net_get_stats64, 1023758e43b7SStephen Hemminger }; 1024758e43b7SStephen Hemminger 1025758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = { 1026c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 1027758e43b7SStephen Hemminger .ndo_open = tun_net_open, 1028758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 102900829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 103088255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 1031afc4b13dSJiri Pirko .ndo_set_rx_mode = tun_net_mclist, 1032758e43b7SStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 1033758e43b7SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 1034c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 1035bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 1036bebd097aSNeil Horman .ndo_poll_controller = tun_poll_controller, 1037bebd097aSNeil Horman #endif 10385e52796aSToshiaki Makita .ndo_features_check = passthru_features_check, 1039eaea34b2SPaolo Abeni .ndo_set_rx_headroom = tun_set_headroom, 1040608b9977SPaolo Abeni .ndo_get_stats64 = tun_net_get_stats64, 1041758e43b7SStephen Hemminger }; 1042758e43b7SStephen Hemminger 1043944a1376SPavel Emelyanov static void tun_flow_init(struct tun_struct *tun) 104496442e42SJason Wang { 104596442e42SJason Wang int i; 104696442e42SJason Wang 104796442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) 104896442e42SJason Wang INIT_HLIST_HEAD(&tun->flows[i]); 104996442e42SJason Wang 105096442e42SJason Wang tun->ageing_time = TUN_FLOW_EXPIRE; 105196442e42SJason Wang setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun); 105296442e42SJason Wang mod_timer(&tun->flow_gc_timer, 105396442e42SJason Wang round_jiffies_up(jiffies + tun->ageing_time)); 105496442e42SJason Wang } 105596442e42SJason Wang 105696442e42SJason Wang static void tun_flow_uninit(struct tun_struct *tun) 105796442e42SJason Wang { 105896442e42SJason Wang del_timer_sync(&tun->flow_gc_timer); 105996442e42SJason Wang tun_flow_flush(tun); 106096442e42SJason Wang } 106196442e42SJason Wang 106291572088SJarod Wilson #define MIN_MTU 68 106391572088SJarod Wilson #define MAX_MTU 65535 106491572088SJarod Wilson 10651da177e4SLinus Torvalds /* Initialize net device. */ 10661da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev) 10671da177e4SLinus Torvalds { 10681da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 107140630b82SMichael S. Tsirkin case IFF_TUN: 1072758e43b7SStephen Hemminger dev->netdev_ops = &tun_netdev_ops; 1073758e43b7SStephen Hemminger 10741da177e4SLinus Torvalds /* Point-to-Point TUN Device */ 10751da177e4SLinus Torvalds dev->hard_header_len = 0; 10761da177e4SLinus Torvalds dev->addr_len = 0; 10771da177e4SLinus Torvalds dev->mtu = 1500; 10781da177e4SLinus Torvalds 10791da177e4SLinus Torvalds /* Zero header length */ 10801da177e4SLinus Torvalds dev->type = ARPHRD_NONE; 10811da177e4SLinus Torvalds dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 10821da177e4SLinus Torvalds break; 10831da177e4SLinus Torvalds 108440630b82SMichael S. Tsirkin case IFF_TAP: 10857a0a9608SKusanagi Kouichi dev->netdev_ops = &tap_netdev_ops; 10861da177e4SLinus Torvalds /* Ethernet TAP Device */ 10871da177e4SLinus Torvalds ether_setup(dev); 1088550fd08cSNeil Horman dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1089a676847bSstephen hemminger dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 109036226a8dSBrian Braunstein 1091f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 109236226a8dSBrian Braunstein 10931da177e4SLinus Torvalds break; 10941da177e4SLinus Torvalds } 109591572088SJarod Wilson 109691572088SJarod Wilson dev->min_mtu = MIN_MTU; 109791572088SJarod Wilson dev->max_mtu = MAX_MTU - dev->hard_header_len; 10981da177e4SLinus Torvalds } 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds /* Character device part */ 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds /* Poll */ 11031da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table *wait) 11041da177e4SLinus Torvalds { 1105b2430de3SEric W. Biederman struct tun_file *tfile = file->private_data; 1106b2430de3SEric W. Biederman struct tun_struct *tun = __tun_get(tfile); 11073c8a9c63SMariusz Kozlowski struct sock *sk; 110833dccbb0SHerbert Xu unsigned int mask = 0; 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvalds if (!tun) 1111eac9e902SEric W. Biederman return POLLERR; 11121da177e4SLinus Torvalds 111354f968d6SJason Wang sk = tfile->socket.sk; 11143c8a9c63SMariusz Kozlowski 11156b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_chr_poll\n"); 11161da177e4SLinus Torvalds 11179e641bdcSXi Wang poll_wait(file, sk_sleep(sk), wait); 11181da177e4SLinus Torvalds 11191576d986SJason Wang if (!skb_array_empty(&tfile->tx_array)) 11201da177e4SLinus Torvalds mask |= POLLIN | POLLRDNORM; 11211da177e4SLinus Torvalds 1122b20e2d54SHannes Frederic Sowa if (tun->dev->flags & IFF_UP && 1123b20e2d54SHannes Frederic Sowa (sock_writeable(sk) || 11249cd3e072SEric Dumazet (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) && 1125b20e2d54SHannes Frederic Sowa sock_writeable(sk)))) 112633dccbb0SHerbert Xu mask |= POLLOUT | POLLWRNORM; 112733dccbb0SHerbert Xu 1128c70f1829SEric W. Biederman if (tun->dev->reg_state != NETREG_REGISTERED) 1129c70f1829SEric W. Biederman mask = POLLERR; 1130c70f1829SEric W. Biederman 1131631ab46bSEric W. Biederman tun_put(tun); 11321da177e4SLinus Torvalds return mask; 11331da177e4SLinus Torvalds } 11341da177e4SLinus Torvalds 1135f42157cbSRusty Russell /* prepad is the amount to reserve at front. len is length after that. 1136f42157cbSRusty Russell * linear is a hint as to how much to copy (usually headers). */ 113754f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, 113833dccbb0SHerbert Xu size_t prepad, size_t len, 113933dccbb0SHerbert Xu size_t linear, int noblock) 1140f42157cbSRusty Russell { 114154f968d6SJason Wang struct sock *sk = tfile->socket.sk; 1142f42157cbSRusty Russell struct sk_buff *skb; 114333dccbb0SHerbert Xu int err; 1144f42157cbSRusty Russell 1145f42157cbSRusty Russell /* Under a page? Don't bother with paged skb. */ 11460eca93bcSHerbert Xu if (prepad + len < PAGE_SIZE || !linear) 114733dccbb0SHerbert Xu linear = len; 1148f42157cbSRusty Russell 114933dccbb0SHerbert Xu skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, 115028d64271SEric Dumazet &err, 0); 1151f42157cbSRusty Russell if (!skb) 115233dccbb0SHerbert Xu return ERR_PTR(err); 1153f42157cbSRusty Russell 1154f42157cbSRusty Russell skb_reserve(skb, prepad); 1155f42157cbSRusty Russell skb_put(skb, linear); 115633dccbb0SHerbert Xu skb->data_len = len - linear; 115733dccbb0SHerbert Xu skb->len += len - linear; 1158f42157cbSRusty Russell 1159f42157cbSRusty Russell return skb; 1160f42157cbSRusty Russell } 1161f42157cbSRusty Russell 11625503fcecSJason Wang static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile, 11635503fcecSJason Wang struct sk_buff *skb, int more) 11645503fcecSJason Wang { 11655503fcecSJason Wang struct sk_buff_head *queue = &tfile->sk.sk_write_queue; 11665503fcecSJason Wang struct sk_buff_head process_queue; 11675503fcecSJason Wang u32 rx_batched = tun->rx_batched; 11685503fcecSJason Wang bool rcv = false; 11695503fcecSJason Wang 11705503fcecSJason Wang if (!rx_batched || (!more && skb_queue_empty(queue))) { 11715503fcecSJason Wang local_bh_disable(); 11725503fcecSJason Wang netif_receive_skb(skb); 11735503fcecSJason Wang local_bh_enable(); 11745503fcecSJason Wang return; 11755503fcecSJason Wang } 11765503fcecSJason Wang 11775503fcecSJason Wang spin_lock(&queue->lock); 11785503fcecSJason Wang if (!more || skb_queue_len(queue) == rx_batched) { 11795503fcecSJason Wang __skb_queue_head_init(&process_queue); 11805503fcecSJason Wang skb_queue_splice_tail_init(queue, &process_queue); 11815503fcecSJason Wang rcv = true; 11825503fcecSJason Wang } else { 11835503fcecSJason Wang __skb_queue_tail(queue, skb); 11845503fcecSJason Wang } 11855503fcecSJason Wang spin_unlock(&queue->lock); 11865503fcecSJason Wang 11875503fcecSJason Wang if (rcv) { 11885503fcecSJason Wang struct sk_buff *nskb; 11895503fcecSJason Wang 11905503fcecSJason Wang local_bh_disable(); 11915503fcecSJason Wang while ((nskb = __skb_dequeue(&process_queue))) 11925503fcecSJason Wang netif_receive_skb(nskb); 11935503fcecSJason Wang netif_receive_skb(skb); 11945503fcecSJason Wang local_bh_enable(); 11955503fcecSJason Wang } 11965503fcecSJason Wang } 11975503fcecSJason Wang 1198*66ccbc9cSJason Wang static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile, 1199*66ccbc9cSJason Wang int len, int noblock, bool zerocopy) 1200*66ccbc9cSJason Wang { 1201*66ccbc9cSJason Wang if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 1202*66ccbc9cSJason Wang return false; 1203*66ccbc9cSJason Wang 1204*66ccbc9cSJason Wang if (tfile->socket.sk->sk_sndbuf != INT_MAX) 1205*66ccbc9cSJason Wang return false; 1206*66ccbc9cSJason Wang 1207*66ccbc9cSJason Wang if (!noblock) 1208*66ccbc9cSJason Wang return false; 1209*66ccbc9cSJason Wang 1210*66ccbc9cSJason Wang if (zerocopy) 1211*66ccbc9cSJason Wang return false; 1212*66ccbc9cSJason Wang 1213*66ccbc9cSJason Wang if (SKB_DATA_ALIGN(len + TUN_RX_PAD) + 1214*66ccbc9cSJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE) 1215*66ccbc9cSJason Wang return false; 1216*66ccbc9cSJason Wang 1217*66ccbc9cSJason Wang return true; 1218*66ccbc9cSJason Wang } 1219*66ccbc9cSJason Wang 1220*66ccbc9cSJason Wang static struct sk_buff *tun_build_skb(struct tun_file *tfile, 1221*66ccbc9cSJason Wang struct iov_iter *from, 1222*66ccbc9cSJason Wang int len) 1223*66ccbc9cSJason Wang { 1224*66ccbc9cSJason Wang struct page_frag *alloc_frag = &tfile->alloc_frag; 1225*66ccbc9cSJason Wang struct sk_buff *skb; 1226*66ccbc9cSJason Wang int buflen = SKB_DATA_ALIGN(len + TUN_RX_PAD) + 1227*66ccbc9cSJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1228*66ccbc9cSJason Wang char *buf; 1229*66ccbc9cSJason Wang size_t copied; 1230*66ccbc9cSJason Wang 1231*66ccbc9cSJason Wang if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL))) 1232*66ccbc9cSJason Wang return ERR_PTR(-ENOMEM); 1233*66ccbc9cSJason Wang 1234*66ccbc9cSJason Wang buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 1235*66ccbc9cSJason Wang copied = copy_page_from_iter(alloc_frag->page, 1236*66ccbc9cSJason Wang alloc_frag->offset + TUN_RX_PAD, 1237*66ccbc9cSJason Wang len, from); 1238*66ccbc9cSJason Wang if (copied != len) 1239*66ccbc9cSJason Wang return ERR_PTR(-EFAULT); 1240*66ccbc9cSJason Wang 1241*66ccbc9cSJason Wang skb = build_skb(buf, buflen); 1242*66ccbc9cSJason Wang if (!skb) 1243*66ccbc9cSJason Wang return ERR_PTR(-ENOMEM); 1244*66ccbc9cSJason Wang 1245*66ccbc9cSJason Wang skb_reserve(skb, TUN_RX_PAD); 1246*66ccbc9cSJason Wang skb_put(skb, len); 1247*66ccbc9cSJason Wang get_page(alloc_frag->page); 1248*66ccbc9cSJason Wang alloc_frag->offset += buflen; 1249*66ccbc9cSJason Wang 1250*66ccbc9cSJason Wang return skb; 1251*66ccbc9cSJason Wang } 1252*66ccbc9cSJason Wang 12531da177e4SLinus Torvalds /* Get packet from user space buffer */ 125454f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, 1255f5ff53b4SAl Viro void *msg_control, struct iov_iter *from, 12565503fcecSJason Wang int noblock, bool more) 12571da177e4SLinus Torvalds { 125809640e63SHarvey Harrison struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; 12591da177e4SLinus Torvalds struct sk_buff *skb; 1260f5ff53b4SAl Viro size_t total_len = iov_iter_count(from); 1261eaea34b2SPaolo Abeni size_t len = total_len, align = tun->align, linear; 1262f43798c2SRusty Russell struct virtio_net_hdr gso = { 0 }; 1263608b9977SPaolo Abeni struct tun_pcpu_stats *stats; 126496f8d9ecSJason Wang int good_linear; 12650690899bSMichael S. Tsirkin int copylen; 12660690899bSMichael S. Tsirkin bool zerocopy = false; 12670690899bSMichael S. Tsirkin int err; 126849974420SEric Dumazet u32 rxhash; 12691da177e4SLinus Torvalds 12701bd4978aSEric Dumazet if (!(tun->dev->flags & IFF_UP)) 12711bd4978aSEric Dumazet return -EIO; 12721bd4978aSEric Dumazet 127340630b82SMichael S. Tsirkin if (!(tun->flags & IFF_NO_PI)) { 127415718ea0SDan Carpenter if (len < sizeof(pi)) 12751da177e4SLinus Torvalds return -EINVAL; 127615718ea0SDan Carpenter len -= sizeof(pi); 12771da177e4SLinus Torvalds 1278cbbd26b8SAl Viro if (!copy_from_iter_full(&pi, sizeof(pi), from)) 12791da177e4SLinus Torvalds return -EFAULT; 12801da177e4SLinus Torvalds } 12811da177e4SLinus Torvalds 128240630b82SMichael S. Tsirkin if (tun->flags & IFF_VNET_HDR) { 1283e1edab87SWillem de Bruijn int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); 1284e1edab87SWillem de Bruijn 1285e1edab87SWillem de Bruijn if (len < vnet_hdr_sz) 1286f43798c2SRusty Russell return -EINVAL; 1287e1edab87SWillem de Bruijn len -= vnet_hdr_sz; 1288f43798c2SRusty Russell 1289cbbd26b8SAl Viro if (!copy_from_iter_full(&gso, sizeof(gso), from)) 1290f43798c2SRusty Russell return -EFAULT; 1291f43798c2SRusty Russell 12924909122fSHerbert Xu if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && 129356f0dcc5SMichael S. Tsirkin tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len)) 129456f0dcc5SMichael S. Tsirkin gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2); 12954909122fSHerbert Xu 129656f0dcc5SMichael S. Tsirkin if (tun16_to_cpu(tun, gso.hdr_len) > len) 1297f43798c2SRusty Russell return -EINVAL; 1298e1edab87SWillem de Bruijn iov_iter_advance(from, vnet_hdr_sz - sizeof(gso)); 1299f43798c2SRusty Russell } 1300f43798c2SRusty Russell 130140630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) { 1302a504b86eSstephen hemminger align += NET_IP_ALIGN; 13030eca93bcSHerbert Xu if (unlikely(len < ETH_HLEN || 130456f0dcc5SMichael S. Tsirkin (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN))) 1305e01bf1c8SRusty Russell return -EINVAL; 1306e01bf1c8SRusty Russell } 13071da177e4SLinus Torvalds 130896f8d9ecSJason Wang good_linear = SKB_MAX_HEAD(align); 130996f8d9ecSJason Wang 131088529176SJason Wang if (msg_control) { 1311f5ff53b4SAl Viro struct iov_iter i = *from; 1312f5ff53b4SAl Viro 131388529176SJason Wang /* There are 256 bytes to be copied in skb, so there is 131488529176SJason Wang * enough room for skb expand head in case it is used. 13150690899bSMichael S. Tsirkin * The rest of the buffer is mapped from userspace. 13160690899bSMichael S. Tsirkin */ 131756f0dcc5SMichael S. Tsirkin copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN; 131896f8d9ecSJason Wang if (copylen > good_linear) 131996f8d9ecSJason Wang copylen = good_linear; 13203dd5c330SJason Wang linear = copylen; 1321f5ff53b4SAl Viro iov_iter_advance(&i, copylen); 1322f5ff53b4SAl Viro if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS) 132388529176SJason Wang zerocopy = true; 132488529176SJason Wang } 132588529176SJason Wang 1326*66ccbc9cSJason Wang if (tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) { 1327*66ccbc9cSJason Wang skb = tun_build_skb(tfile, from, len); 1328*66ccbc9cSJason Wang if (IS_ERR(skb)) { 1329*66ccbc9cSJason Wang this_cpu_inc(tun->pcpu_stats->rx_dropped); 1330*66ccbc9cSJason Wang return PTR_ERR(skb); 1331*66ccbc9cSJason Wang } 1332*66ccbc9cSJason Wang } else { 133388529176SJason Wang if (!zerocopy) { 13340690899bSMichael S. Tsirkin copylen = len; 133556f0dcc5SMichael S. Tsirkin if (tun16_to_cpu(tun, gso.hdr_len) > good_linear) 133696f8d9ecSJason Wang linear = good_linear; 133796f8d9ecSJason Wang else 133856f0dcc5SMichael S. Tsirkin linear = tun16_to_cpu(tun, gso.hdr_len); 13393dd5c330SJason Wang } 13400690899bSMichael S. Tsirkin 13413dd5c330SJason Wang skb = tun_alloc_skb(tfile, align, copylen, linear, noblock); 134233dccbb0SHerbert Xu if (IS_ERR(skb)) { 134333dccbb0SHerbert Xu if (PTR_ERR(skb) != -EAGAIN) 1344608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 134533dccbb0SHerbert Xu return PTR_ERR(skb); 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds 13480690899bSMichael S. Tsirkin if (zerocopy) 1349f5ff53b4SAl Viro err = zerocopy_sg_from_iter(skb, from); 1350af1cc7a2SJason Wang else 1351f5ff53b4SAl Viro err = skb_copy_datagram_from_iter(skb, 0, from, len); 13520690899bSMichael S. Tsirkin 13530690899bSMichael S. Tsirkin if (err) { 1354608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 13558f22757eSDave Jones kfree_skb(skb); 13561da177e4SLinus Torvalds return -EFAULT; 13578f22757eSDave Jones } 1358*66ccbc9cSJason Wang } 13591da177e4SLinus Torvalds 13603e9e40e7SJarno Rajahalme if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) { 1361df10db98SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_frame_errors); 1362df10db98SPaolo Abeni kfree_skb(skb); 1363df10db98SPaolo Abeni return -EINVAL; 1364df10db98SPaolo Abeni } 1365df10db98SPaolo Abeni 13661da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 136740630b82SMichael S. Tsirkin case IFF_TUN: 136840630b82SMichael S. Tsirkin if (tun->flags & IFF_NO_PI) { 1369f09f7ee2SAng Way Chuang switch (skb->data[0] & 0xf0) { 1370f09f7ee2SAng Way Chuang case 0x40: 1371f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IP); 1372f09f7ee2SAng Way Chuang break; 1373f09f7ee2SAng Way Chuang case 0x60: 1374f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IPV6); 1375f09f7ee2SAng Way Chuang break; 1376f09f7ee2SAng Way Chuang default: 1377608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 1378f09f7ee2SAng Way Chuang kfree_skb(skb); 1379f09f7ee2SAng Way Chuang return -EINVAL; 1380f09f7ee2SAng Way Chuang } 1381f09f7ee2SAng Way Chuang } 1382f09f7ee2SAng Way Chuang 1383459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 13841da177e4SLinus Torvalds skb->protocol = pi.proto; 13854c13eb66SArnaldo Carvalho de Melo skb->dev = tun->dev; 13861da177e4SLinus Torvalds break; 138740630b82SMichael S. Tsirkin case IFF_TAP: 13881da177e4SLinus Torvalds skb->protocol = eth_type_trans(skb, tun->dev); 13891da177e4SLinus Torvalds break; 13906403eab1SJoe Perches } 13911da177e4SLinus Torvalds 13920690899bSMichael S. Tsirkin /* copy skb_ubuf_info for callback when skb has no error */ 13930690899bSMichael S. Tsirkin if (zerocopy) { 13940690899bSMichael S. Tsirkin skb_shinfo(skb)->destructor_arg = msg_control; 13950690899bSMichael S. Tsirkin skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; 1396c9af6db4SPravin B Shelar skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; 1397af1cc7a2SJason Wang } else if (msg_control) { 1398af1cc7a2SJason Wang struct ubuf_info *uarg = msg_control; 1399af1cc7a2SJason Wang uarg->callback(uarg, false); 14000690899bSMichael S. Tsirkin } 14010690899bSMichael S. Tsirkin 140272f65107SVlad Yasevich skb_reset_network_header(skb); 140340893fd0SJason Wang skb_probe_transport_header(skb, 0); 140438502af7SJason Wang 1405feec084aSJason Wang rxhash = __skb_get_hash_symmetric(skb); 1406d4aea20dSAndrey Konovalov #ifndef CONFIG_4KSTACKS 14075503fcecSJason Wang tun_rx_batched(tun, tfile, skb, more); 1408d4aea20dSAndrey Konovalov #else 14091da177e4SLinus Torvalds netif_rx_ni(skb); 1410d4aea20dSAndrey Konovalov #endif 14111da177e4SLinus Torvalds 1412608b9977SPaolo Abeni stats = get_cpu_ptr(tun->pcpu_stats); 1413608b9977SPaolo Abeni u64_stats_update_begin(&stats->syncp); 1414608b9977SPaolo Abeni stats->rx_packets++; 1415608b9977SPaolo Abeni stats->rx_bytes += len; 1416608b9977SPaolo Abeni u64_stats_update_end(&stats->syncp); 1417608b9977SPaolo Abeni put_cpu_ptr(stats); 14181da177e4SLinus Torvalds 14199e85722dSJason Wang tun_flow_update(tun, rxhash, tfile); 14200690899bSMichael S. Tsirkin return total_len; 14211da177e4SLinus Torvalds } 14221da177e4SLinus Torvalds 1423f5ff53b4SAl Viro static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from) 14241da177e4SLinus Torvalds { 142533dccbb0SHerbert Xu struct file *file = iocb->ki_filp; 1426ab46d779SHerbert Xu struct tun_struct *tun = tun_get(file); 142754f968d6SJason Wang struct tun_file *tfile = file->private_data; 1428631ab46bSEric W. Biederman ssize_t result; 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds if (!tun) 14311da177e4SLinus Torvalds return -EBADFD; 14321da177e4SLinus Torvalds 14335503fcecSJason Wang result = tun_get_user(tun, tfile, NULL, from, 14345503fcecSJason Wang file->f_flags & O_NONBLOCK, false); 1435631ab46bSEric W. Biederman 1436631ab46bSEric W. Biederman tun_put(tun); 1437631ab46bSEric W. Biederman return result; 14381da177e4SLinus Torvalds } 14391da177e4SLinus Torvalds 14401da177e4SLinus Torvalds /* Put packet to the user space buffer */ 14416f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun, 144254f968d6SJason Wang struct tun_file *tfile, 14431da177e4SLinus Torvalds struct sk_buff *skb, 1444e0b46d0eSHerbert Xu struct iov_iter *iter) 14451da177e4SLinus Torvalds { 14461da177e4SLinus Torvalds struct tun_pi pi = { 0, skb->protocol }; 1447608b9977SPaolo Abeni struct tun_pcpu_stats *stats; 1448e0b46d0eSHerbert Xu ssize_t total; 14498c847d25SJason Wang int vlan_offset = 0; 1450a8f9bfdfSHerbert Xu int vlan_hlen = 0; 14512eb783c4SHerbert Xu int vnet_hdr_sz = 0; 1452a8f9bfdfSHerbert Xu 1453df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) 1454a8f9bfdfSHerbert Xu vlan_hlen = VLAN_HLEN; 14551da177e4SLinus Torvalds 145640630b82SMichael S. Tsirkin if (tun->flags & IFF_VNET_HDR) 1457e1edab87SWillem de Bruijn vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); 14581da177e4SLinus Torvalds 1459e0b46d0eSHerbert Xu total = skb->len + vlan_hlen + vnet_hdr_sz; 1460e0b46d0eSHerbert Xu 146140630b82SMichael S. Tsirkin if (!(tun->flags & IFF_NO_PI)) { 1462e0b46d0eSHerbert Xu if (iov_iter_count(iter) < sizeof(pi)) 14631da177e4SLinus Torvalds return -EINVAL; 14641da177e4SLinus Torvalds 1465e0b46d0eSHerbert Xu total += sizeof(pi); 1466e0b46d0eSHerbert Xu if (iov_iter_count(iter) < total) { 14671da177e4SLinus Torvalds /* Packet will be striped */ 14681da177e4SLinus Torvalds pi.flags |= TUN_PKT_STRIP; 14691da177e4SLinus Torvalds } 14701da177e4SLinus Torvalds 1471e0b46d0eSHerbert Xu if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi)) 14721da177e4SLinus Torvalds return -EFAULT; 14731da177e4SLinus Torvalds } 14741da177e4SLinus Torvalds 14752eb783c4SHerbert Xu if (vnet_hdr_sz) { 14769403cd7cSJarno Rajahalme struct virtio_net_hdr gso; 147734166093SMike Rapoport 1478e0b46d0eSHerbert Xu if (iov_iter_count(iter) < vnet_hdr_sz) 1479f43798c2SRusty Russell return -EINVAL; 1480f43798c2SRusty Russell 14813e9e40e7SJarno Rajahalme if (virtio_net_hdr_from_skb(skb, &gso, 14826391a448SJason Wang tun_is_little_endian(tun), true)) { 1483f43798c2SRusty Russell struct skb_shared_info *sinfo = skb_shinfo(skb); 14846b8a66eeSJoe Perches pr_err("unexpected GSO type: " 1485ef3db4a5SMichael S. Tsirkin "0x%x, gso_size %d, hdr_len %d\n", 148656f0dcc5SMichael S. Tsirkin sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size), 148756f0dcc5SMichael S. Tsirkin tun16_to_cpu(tun, gso.hdr_len)); 1488ef3db4a5SMichael S. Tsirkin print_hex_dump(KERN_ERR, "tun: ", 1489ef3db4a5SMichael S. Tsirkin DUMP_PREFIX_NONE, 1490ef3db4a5SMichael S. Tsirkin 16, 1, skb->head, 149156f0dcc5SMichael S. Tsirkin min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true); 1492ef3db4a5SMichael S. Tsirkin WARN_ON_ONCE(1); 1493ef3db4a5SMichael S. Tsirkin return -EINVAL; 1494ef3db4a5SMichael S. Tsirkin } 1495f43798c2SRusty Russell 1496e0b46d0eSHerbert Xu if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso)) 1497f43798c2SRusty Russell return -EFAULT; 14988c847d25SJason Wang 14998c847d25SJason Wang iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso)); 1500f43798c2SRusty Russell } 1501f43798c2SRusty Russell 1502a8f9bfdfSHerbert Xu if (vlan_hlen) { 1503e0b46d0eSHerbert Xu int ret; 15046680ec68SJason Wang struct { 15056680ec68SJason Wang __be16 h_vlan_proto; 15066680ec68SJason Wang __be16 h_vlan_TCI; 15076680ec68SJason Wang } veth; 15081da177e4SLinus Torvalds 15096680ec68SJason Wang veth.h_vlan_proto = skb->vlan_proto; 1510df8a39deSJiri Pirko veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb)); 15111da177e4SLinus Torvalds 15126680ec68SJason Wang vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); 15136680ec68SJason Wang 1514e0b46d0eSHerbert Xu ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset); 1515e0b46d0eSHerbert Xu if (ret || !iov_iter_count(iter)) 15166680ec68SJason Wang goto done; 15176680ec68SJason Wang 1518e0b46d0eSHerbert Xu ret = copy_to_iter(&veth, sizeof(veth), iter); 1519e0b46d0eSHerbert Xu if (ret != sizeof(veth) || !iov_iter_count(iter)) 15206680ec68SJason Wang goto done; 15216680ec68SJason Wang } 15226680ec68SJason Wang 1523e0b46d0eSHerbert Xu skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset); 15246680ec68SJason Wang 15256680ec68SJason Wang done: 1526608b9977SPaolo Abeni /* caller is in process context, */ 1527608b9977SPaolo Abeni stats = get_cpu_ptr(tun->pcpu_stats); 1528608b9977SPaolo Abeni u64_stats_update_begin(&stats->syncp); 1529608b9977SPaolo Abeni stats->tx_packets++; 1530608b9977SPaolo Abeni stats->tx_bytes += skb->len + vlan_hlen; 1531608b9977SPaolo Abeni u64_stats_update_end(&stats->syncp); 1532608b9977SPaolo Abeni put_cpu_ptr(tun->pcpu_stats); 15331da177e4SLinus Torvalds 15341da177e4SLinus Torvalds return total; 15351da177e4SLinus Torvalds } 15361da177e4SLinus Torvalds 15371576d986SJason Wang static struct sk_buff *tun_ring_recv(struct tun_file *tfile, int noblock, 15381576d986SJason Wang int *err) 15391576d986SJason Wang { 15401576d986SJason Wang DECLARE_WAITQUEUE(wait, current); 15411576d986SJason Wang struct sk_buff *skb = NULL; 1542f48cc6b2SJason Wang int error = 0; 15431576d986SJason Wang 15441576d986SJason Wang skb = skb_array_consume(&tfile->tx_array); 15451576d986SJason Wang if (skb) 15461576d986SJason Wang goto out; 15471576d986SJason Wang if (noblock) { 1548f48cc6b2SJason Wang error = -EAGAIN; 15491576d986SJason Wang goto out; 15501576d986SJason Wang } 15511576d986SJason Wang 15521576d986SJason Wang add_wait_queue(&tfile->wq.wait, &wait); 15531576d986SJason Wang current->state = TASK_INTERRUPTIBLE; 15541576d986SJason Wang 15551576d986SJason Wang while (1) { 15561576d986SJason Wang skb = skb_array_consume(&tfile->tx_array); 15571576d986SJason Wang if (skb) 15581576d986SJason Wang break; 15591576d986SJason Wang if (signal_pending(current)) { 1560f48cc6b2SJason Wang error = -ERESTARTSYS; 15611576d986SJason Wang break; 15621576d986SJason Wang } 15631576d986SJason Wang if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) { 1564f48cc6b2SJason Wang error = -EFAULT; 15651576d986SJason Wang break; 15661576d986SJason Wang } 15671576d986SJason Wang 15681576d986SJason Wang schedule(); 15691576d986SJason Wang } 15701576d986SJason Wang 15711576d986SJason Wang current->state = TASK_RUNNING; 15721576d986SJason Wang remove_wait_queue(&tfile->wq.wait, &wait); 15731576d986SJason Wang 15741576d986SJason Wang out: 1575f48cc6b2SJason Wang *err = error; 15761576d986SJason Wang return skb; 15771576d986SJason Wang } 15781576d986SJason Wang 157954f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, 15809b067034SAl Viro struct iov_iter *to, 1581ac77cfd4SJason Wang int noblock, struct sk_buff *skb) 15821da177e4SLinus Torvalds { 15839b067034SAl Viro ssize_t ret; 15841576d986SJason Wang int err; 15851da177e4SLinus Torvalds 15863872baf6SRami Rosen tun_debug(KERN_INFO, tun, "tun_do_read\n"); 15871da177e4SLinus Torvalds 15889b067034SAl Viro if (!iov_iter_count(to)) 15899b067034SAl Viro return 0; 15901da177e4SLinus Torvalds 1591ac77cfd4SJason Wang if (!skb) { 15921576d986SJason Wang /* Read frames from ring */ 15931576d986SJason Wang skb = tun_ring_recv(tfile, noblock, &err); 1594e0b46d0eSHerbert Xu if (!skb) 1595957f094fSAlex Gartrell return err; 1596ac77cfd4SJason Wang } 1597e0b46d0eSHerbert Xu 15989b067034SAl Viro ret = tun_put_user(tun, tfile, skb, to); 1599f51a5e82SJason Wang if (unlikely(ret < 0)) 16001da177e4SLinus Torvalds kfree_skb(skb); 1601f51a5e82SJason Wang else 1602f51a5e82SJason Wang consume_skb(skb); 16031da177e4SLinus Torvalds 160405c2828cSMichael S. Tsirkin return ret; 160505c2828cSMichael S. Tsirkin } 160605c2828cSMichael S. Tsirkin 16079b067034SAl Viro static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to) 160805c2828cSMichael S. Tsirkin { 160905c2828cSMichael S. Tsirkin struct file *file = iocb->ki_filp; 161005c2828cSMichael S. Tsirkin struct tun_file *tfile = file->private_data; 161105c2828cSMichael S. Tsirkin struct tun_struct *tun = __tun_get(tfile); 16129b067034SAl Viro ssize_t len = iov_iter_count(to), ret; 161305c2828cSMichael S. Tsirkin 161405c2828cSMichael S. Tsirkin if (!tun) 161505c2828cSMichael S. Tsirkin return -EBADFD; 1616ac77cfd4SJason Wang ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK, NULL); 161742404c09SDavid S. Miller ret = min_t(ssize_t, ret, len); 1618d0b7da8aSZhi Yong Wu if (ret > 0) 1619d0b7da8aSZhi Yong Wu iocb->ki_pos = ret; 1620631ab46bSEric W. Biederman tun_put(tun); 16211da177e4SLinus Torvalds return ret; 16221da177e4SLinus Torvalds } 16231da177e4SLinus Torvalds 162496442e42SJason Wang static void tun_free_netdev(struct net_device *dev) 162596442e42SJason Wang { 162696442e42SJason Wang struct tun_struct *tun = netdev_priv(dev); 162796442e42SJason Wang 16284008e97fSJason Wang BUG_ON(!(list_empty(&tun->disabled))); 1629608b9977SPaolo Abeni free_percpu(tun->pcpu_stats); 163096442e42SJason Wang tun_flow_uninit(tun); 16315dbbaf2dSPaul Moore security_tun_dev_free_security(tun->security); 163296442e42SJason Wang } 163396442e42SJason Wang 16341da177e4SLinus Torvalds static void tun_setup(struct net_device *dev) 16351da177e4SLinus Torvalds { 16361da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 16371da177e4SLinus Torvalds 16380625c883SEric W. Biederman tun->owner = INVALID_UID; 16390625c883SEric W. Biederman tun->group = INVALID_GID; 16401da177e4SLinus Torvalds 16411da177e4SLinus Torvalds dev->ethtool_ops = &tun_ethtool_ops; 1642cf124db5SDavid S. Miller dev->needs_free_netdev = true; 1643cf124db5SDavid S. Miller dev->priv_destructor = tun_free_netdev; 1644016adb72SJason Wang /* We prefer our own queue length */ 1645016adb72SJason Wang dev->tx_queue_len = TUN_READQ_SIZE; 16461da177e4SLinus Torvalds } 16471da177e4SLinus Torvalds 1648f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap 1649f019a7a5SEric W. Biederman * device with netlink. 1650f019a7a5SEric W. Biederman */ 1651a8b8a889SMatthias Schiffer static int tun_validate(struct nlattr *tb[], struct nlattr *data[], 1652a8b8a889SMatthias Schiffer struct netlink_ext_ack *extack) 1653f019a7a5SEric W. Biederman { 1654f019a7a5SEric W. Biederman return -EINVAL; 1655f019a7a5SEric W. Biederman } 1656f019a7a5SEric W. Biederman 1657f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = { 1658f019a7a5SEric W. Biederman .kind = DRV_NAME, 1659f019a7a5SEric W. Biederman .priv_size = sizeof(struct tun_struct), 1660f019a7a5SEric W. Biederman .setup = tun_setup, 1661f019a7a5SEric W. Biederman .validate = tun_validate, 1662f019a7a5SEric W. Biederman }; 1663f019a7a5SEric W. Biederman 166433dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk) 166533dccbb0SHerbert Xu { 166654f968d6SJason Wang struct tun_file *tfile; 166743815482SEric Dumazet wait_queue_head_t *wqueue; 166833dccbb0SHerbert Xu 166933dccbb0SHerbert Xu if (!sock_writeable(sk)) 167033dccbb0SHerbert Xu return; 167133dccbb0SHerbert Xu 16729cd3e072SEric Dumazet if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags)) 167333dccbb0SHerbert Xu return; 167433dccbb0SHerbert Xu 167543815482SEric Dumazet wqueue = sk_sleep(sk); 167643815482SEric Dumazet if (wqueue && waitqueue_active(wqueue)) 167743815482SEric Dumazet wake_up_interruptible_sync_poll(wqueue, POLLOUT | 167805c2828cSMichael S. Tsirkin POLLWRNORM | POLLWRBAND); 1679c722c625SHerbert Xu 168054f968d6SJason Wang tfile = container_of(sk, struct tun_file, sk); 168154f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); 168233dccbb0SHerbert Xu } 168333dccbb0SHerbert Xu 16841b784140SYing Xue static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len) 168505c2828cSMichael S. Tsirkin { 168654f968d6SJason Wang int ret; 168754f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 168854f968d6SJason Wang struct tun_struct *tun = __tun_get(tfile); 168954f968d6SJason Wang 169054f968d6SJason Wang if (!tun) 169154f968d6SJason Wang return -EBADFD; 1692f5ff53b4SAl Viro 1693c0371da6SAl Viro ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter, 16945503fcecSJason Wang m->msg_flags & MSG_DONTWAIT, 16955503fcecSJason Wang m->msg_flags & MSG_MORE); 169654f968d6SJason Wang tun_put(tun); 169754f968d6SJason Wang return ret; 169805c2828cSMichael S. Tsirkin } 169905c2828cSMichael S. Tsirkin 17001b784140SYing Xue static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, 170105c2828cSMichael S. Tsirkin int flags) 170205c2828cSMichael S. Tsirkin { 170354f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 170454f968d6SJason Wang struct tun_struct *tun = __tun_get(tfile); 170505c2828cSMichael S. Tsirkin int ret; 170654f968d6SJason Wang 170754f968d6SJason Wang if (!tun) 170854f968d6SJason Wang return -EBADFD; 170954f968d6SJason Wang 1710eda29772SRichard Cochran if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) { 17113811ae76SGao feng ret = -EINVAL; 17123811ae76SGao feng goto out; 17133811ae76SGao feng } 1714eda29772SRichard Cochran if (flags & MSG_ERRQUEUE) { 1715eda29772SRichard Cochran ret = sock_recv_errqueue(sock->sk, m, total_len, 1716eda29772SRichard Cochran SOL_PACKET, TUN_TX_TIMESTAMP); 1717eda29772SRichard Cochran goto out; 1718eda29772SRichard Cochran } 1719ac77cfd4SJason Wang ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, 1720ac77cfd4SJason Wang m->msg_control); 172187897931SAlex Gartrell if (ret > (ssize_t)total_len) { 172242404c09SDavid S. Miller m->msg_flags |= MSG_TRUNC; 172342404c09SDavid S. Miller ret = flags & MSG_TRUNC ? ret : total_len; 172442404c09SDavid S. Miller } 17253811ae76SGao feng out: 172654f968d6SJason Wang tun_put(tun); 172705c2828cSMichael S. Tsirkin return ret; 172805c2828cSMichael S. Tsirkin } 172905c2828cSMichael S. Tsirkin 17301576d986SJason Wang static int tun_peek_len(struct socket *sock) 17311576d986SJason Wang { 17321576d986SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 17331576d986SJason Wang struct tun_struct *tun; 17341576d986SJason Wang int ret = 0; 17351576d986SJason Wang 17361576d986SJason Wang tun = __tun_get(tfile); 17371576d986SJason Wang if (!tun) 17381576d986SJason Wang return 0; 17391576d986SJason Wang 17401576d986SJason Wang ret = skb_array_peek_len(&tfile->tx_array); 17411576d986SJason Wang tun_put(tun); 17421576d986SJason Wang 17431576d986SJason Wang return ret; 17441576d986SJason Wang } 17451576d986SJason Wang 174605c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */ 174705c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = { 17481576d986SJason Wang .peek_len = tun_peek_len, 174905c2828cSMichael S. Tsirkin .sendmsg = tun_sendmsg, 175005c2828cSMichael S. Tsirkin .recvmsg = tun_recvmsg, 175105c2828cSMichael S. Tsirkin }; 175205c2828cSMichael S. Tsirkin 175333dccbb0SHerbert Xu static struct proto tun_proto = { 175433dccbb0SHerbert Xu .name = "tun", 175533dccbb0SHerbert Xu .owner = THIS_MODULE, 175654f968d6SJason Wang .obj_size = sizeof(struct tun_file), 175733dccbb0SHerbert Xu }; 1758f019a7a5SEric W. Biederman 1759980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun) 1760980c9e8cSDavid Woodhouse { 1761031f5e03SMichael S. Tsirkin return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP); 1762980c9e8cSDavid Woodhouse } 1763980c9e8cSDavid Woodhouse 1764980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr, 1765980c9e8cSDavid Woodhouse char *buf) 1766980c9e8cSDavid Woodhouse { 1767980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 1768980c9e8cSDavid Woodhouse return sprintf(buf, "0x%x\n", tun_flags(tun)); 1769980c9e8cSDavid Woodhouse } 1770980c9e8cSDavid Woodhouse 1771980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr, 1772980c9e8cSDavid Woodhouse char *buf) 1773980c9e8cSDavid Woodhouse { 1774980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 17750625c883SEric W. Biederman return uid_valid(tun->owner)? 17760625c883SEric W. Biederman sprintf(buf, "%u\n", 17770625c883SEric W. Biederman from_kuid_munged(current_user_ns(), tun->owner)): 17780625c883SEric W. Biederman sprintf(buf, "-1\n"); 1779980c9e8cSDavid Woodhouse } 1780980c9e8cSDavid Woodhouse 1781980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr, 1782980c9e8cSDavid Woodhouse char *buf) 1783980c9e8cSDavid Woodhouse { 1784980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 17850625c883SEric W. Biederman return gid_valid(tun->group) ? 17860625c883SEric W. Biederman sprintf(buf, "%u\n", 17870625c883SEric W. Biederman from_kgid_munged(current_user_ns(), tun->group)): 17880625c883SEric W. Biederman sprintf(buf, "-1\n"); 1789980c9e8cSDavid Woodhouse } 1790980c9e8cSDavid Woodhouse 1791980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL); 1792980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL); 1793980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL); 1794980c9e8cSDavid Woodhouse 1795c4d33e24STakashi Iwai static struct attribute *tun_dev_attrs[] = { 1796c4d33e24STakashi Iwai &dev_attr_tun_flags.attr, 1797c4d33e24STakashi Iwai &dev_attr_owner.attr, 1798c4d33e24STakashi Iwai &dev_attr_group.attr, 1799c4d33e24STakashi Iwai NULL 1800c4d33e24STakashi Iwai }; 1801c4d33e24STakashi Iwai 1802c4d33e24STakashi Iwai static const struct attribute_group tun_attr_group = { 1803c4d33e24STakashi Iwai .attrs = tun_dev_attrs 1804c4d33e24STakashi Iwai }; 1805c4d33e24STakashi Iwai 1806d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) 18071da177e4SLinus Torvalds { 18081da177e4SLinus Torvalds struct tun_struct *tun; 180954f968d6SJason Wang struct tun_file *tfile = file->private_data; 18101da177e4SLinus Torvalds struct net_device *dev; 18111da177e4SLinus Torvalds int err; 18121da177e4SLinus Torvalds 18137c0c3b1aSJason Wang if (tfile->detached) 18147c0c3b1aSJason Wang return -EINVAL; 18157c0c3b1aSJason Wang 181674a3e5a7SEric W. Biederman dev = __dev_get_by_name(net, ifr->ifr_name); 181774a3e5a7SEric W. Biederman if (dev) { 1818f85ba780SDavid Woodhouse if (ifr->ifr_flags & IFF_TUN_EXCL) 1819f85ba780SDavid Woodhouse return -EBUSY; 182074a3e5a7SEric W. Biederman if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) 182174a3e5a7SEric W. Biederman tun = netdev_priv(dev); 182274a3e5a7SEric W. Biederman else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) 182374a3e5a7SEric W. Biederman tun = netdev_priv(dev); 182474a3e5a7SEric W. Biederman else 182574a3e5a7SEric W. Biederman return -EINVAL; 182674a3e5a7SEric W. Biederman 18278e6d91aeSJason Wang if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) != 182840630b82SMichael S. Tsirkin !!(tun->flags & IFF_MULTI_QUEUE)) 18298e6d91aeSJason Wang return -EINVAL; 18308e6d91aeSJason Wang 1831cde8b15fSJason Wang if (tun_not_capable(tun)) 18322b980dbdSPaul Moore return -EPERM; 18335dbbaf2dSPaul Moore err = security_tun_dev_open(tun->security); 18342b980dbdSPaul Moore if (err < 0) 18352b980dbdSPaul Moore return err; 18362b980dbdSPaul Moore 1837849c9b6fSPavel Emelyanov err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER); 1838a7385ba2SEric W. Biederman if (err < 0) 1839a7385ba2SEric W. Biederman return err; 18404008e97fSJason Wang 184140630b82SMichael S. Tsirkin if (tun->flags & IFF_MULTI_QUEUE && 1842e8dbad66SJason Wang (tun->numqueues + tun->numdisabled > 1)) { 1843e8dbad66SJason Wang /* One or more queue has already been attached, no need 1844e8dbad66SJason Wang * to initialize the device again. 1845e8dbad66SJason Wang */ 1846e8dbad66SJason Wang return 0; 1847e8dbad66SJason Wang } 184886a264abSDavid Howells } 18491da177e4SLinus Torvalds else { 18501da177e4SLinus Torvalds char *name; 18511da177e4SLinus Torvalds unsigned long flags = 0; 1852edfb6a14SJason Wang int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ? 1853edfb6a14SJason Wang MAX_TAP_QUEUES : 1; 18541da177e4SLinus Torvalds 1855c260b772SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1856ca6bb5d7SDavid Woodhouse return -EPERM; 18572b980dbdSPaul Moore err = security_tun_dev_create(); 18582b980dbdSPaul Moore if (err < 0) 18592b980dbdSPaul Moore return err; 1860ca6bb5d7SDavid Woodhouse 18611da177e4SLinus Torvalds /* Set dev type */ 18621da177e4SLinus Torvalds if (ifr->ifr_flags & IFF_TUN) { 18631da177e4SLinus Torvalds /* TUN device */ 186440630b82SMichael S. Tsirkin flags |= IFF_TUN; 18651da177e4SLinus Torvalds name = "tun%d"; 18661da177e4SLinus Torvalds } else if (ifr->ifr_flags & IFF_TAP) { 18671da177e4SLinus Torvalds /* TAP device */ 186840630b82SMichael S. Tsirkin flags |= IFF_TAP; 18691da177e4SLinus Torvalds name = "tap%d"; 18701da177e4SLinus Torvalds } else 187136989b90SKusanagi Kouichi return -EINVAL; 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds if (*ifr->ifr_name) 18741da177e4SLinus Torvalds name = ifr->ifr_name; 18751da177e4SLinus Torvalds 1876c8d68e6bSJason Wang dev = alloc_netdev_mqs(sizeof(struct tun_struct), name, 1877c835a677STom Gundersen NET_NAME_UNKNOWN, tun_setup, queues, 1878c835a677STom Gundersen queues); 1879edfb6a14SJason Wang 18801da177e4SLinus Torvalds if (!dev) 18811da177e4SLinus Torvalds return -ENOMEM; 18821da177e4SLinus Torvalds 1883fc54c658SPavel Emelyanov dev_net_set(dev, net); 1884f019a7a5SEric W. Biederman dev->rtnl_link_ops = &tun_link_ops; 1885fb7589a1SPavel Emelyanov dev->ifindex = tfile->ifindex; 1886c4d33e24STakashi Iwai dev->sysfs_groups[0] = &tun_attr_group; 1887758e43b7SStephen Hemminger 18881da177e4SLinus Torvalds tun = netdev_priv(dev); 18891da177e4SLinus Torvalds tun->dev = dev; 18901da177e4SLinus Torvalds tun->flags = flags; 1891f271b2ccSMax Krasnyansky tun->txflt.count = 0; 1892d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr); 18931da177e4SLinus Torvalds 1894eaea34b2SPaolo Abeni tun->align = NET_SKB_PAD; 189554f968d6SJason Wang tun->filter_attached = false; 189654f968d6SJason Wang tun->sndbuf = tfile->socket.sk->sk_sndbuf; 18975503fcecSJason Wang tun->rx_batched = 0; 189833dccbb0SHerbert Xu 1899608b9977SPaolo Abeni tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats); 1900608b9977SPaolo Abeni if (!tun->pcpu_stats) { 1901608b9977SPaolo Abeni err = -ENOMEM; 1902608b9977SPaolo Abeni goto err_free_dev; 1903608b9977SPaolo Abeni } 1904608b9977SPaolo Abeni 190596442e42SJason Wang spin_lock_init(&tun->lock); 190696442e42SJason Wang 19075dbbaf2dSPaul Moore err = security_tun_dev_alloc_security(&tun->security); 19085dbbaf2dSPaul Moore if (err < 0) 1909608b9977SPaolo Abeni goto err_free_stat; 19102b980dbdSPaul Moore 19111da177e4SLinus Torvalds tun_net_init(dev); 1912944a1376SPavel Emelyanov tun_flow_init(tun); 191396442e42SJason Wang 191488255375SMichał Mirosław dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | 19156680ec68SJason Wang TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX | 19166680ec68SJason Wang NETIF_F_HW_VLAN_STAG_TX; 19172a2bbf17SPaolo Abeni dev->features = dev->hw_features | NETIF_F_LLTX; 19186671b224SFernando Luis Vazquez Cao dev->vlan_features = dev->features & 19196671b224SFernando Luis Vazquez Cao ~(NETIF_F_HW_VLAN_CTAG_TX | 19206671b224SFernando Luis Vazquez Cao NETIF_F_HW_VLAN_STAG_TX); 192188255375SMichał Mirosław 19224008e97fSJason Wang INIT_LIST_HEAD(&tun->disabled); 1923849c9b6fSPavel Emelyanov err = tun_attach(tun, file, false); 1924eb0fb363SJason Wang if (err < 0) 1925662ca437SJason Wang goto err_free_flow; 1926eb0fb363SJason Wang 19271da177e4SLinus Torvalds err = register_netdevice(tun->dev); 19281da177e4SLinus Torvalds if (err < 0) 1929662ca437SJason Wang goto err_detach; 1930af668b3cSMichael S. Tsirkin } 1931980c9e8cSDavid Woodhouse 1932eb0fb363SJason Wang netif_carrier_on(tun->dev); 19331da177e4SLinus Torvalds 19346b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_set_iff\n"); 19351da177e4SLinus Torvalds 1936031f5e03SMichael S. Tsirkin tun->flags = (tun->flags & ~TUN_FEATURES) | 1937031f5e03SMichael S. Tsirkin (ifr->ifr_flags & TUN_FEATURES); 1938c8d68e6bSJason Wang 1939e35259a9SMax Krasnyansky /* Make sure persistent devices do not get stuck in 1940e35259a9SMax Krasnyansky * xoff state. 1941e35259a9SMax Krasnyansky */ 1942e35259a9SMax Krasnyansky if (netif_running(tun->dev)) 1943c8d68e6bSJason Wang netif_tx_wake_all_queues(tun->dev); 1944e35259a9SMax Krasnyansky 19451da177e4SLinus Torvalds strcpy(ifr->ifr_name, tun->dev->name); 19461da177e4SLinus Torvalds return 0; 19471da177e4SLinus Torvalds 1948662ca437SJason Wang err_detach: 1949662ca437SJason Wang tun_detach_all(dev); 1950662ca437SJason Wang err_free_flow: 1951662ca437SJason Wang tun_flow_uninit(tun); 1952662ca437SJason Wang security_tun_dev_free_security(tun->security); 1953608b9977SPaolo Abeni err_free_stat: 1954608b9977SPaolo Abeni free_percpu(tun->pcpu_stats); 19551da177e4SLinus Torvalds err_free_dev: 19561da177e4SLinus Torvalds free_netdev(dev); 19571da177e4SLinus Torvalds return err; 19581da177e4SLinus Torvalds } 19591da177e4SLinus Torvalds 19609ce99cf6SRami Rosen static void tun_get_iff(struct net *net, struct tun_struct *tun, 1961876bfd4dSHerbert Xu struct ifreq *ifr) 1962e3b99556SMark McLoughlin { 19636b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_get_iff\n"); 1964e3b99556SMark McLoughlin 1965e3b99556SMark McLoughlin strcpy(ifr->ifr_name, tun->dev->name); 1966e3b99556SMark McLoughlin 1967980c9e8cSDavid Woodhouse ifr->ifr_flags = tun_flags(tun); 1968e3b99556SMark McLoughlin 1969e3b99556SMark McLoughlin } 1970e3b99556SMark McLoughlin 19715228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no 19725228ddc9SRusty Russell * privs required. */ 197388255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg) 19745228ddc9SRusty Russell { 1975c8f44affSMichał Mirosław netdev_features_t features = 0; 19765228ddc9SRusty Russell 19775228ddc9SRusty Russell if (arg & TUN_F_CSUM) { 197888255375SMichał Mirosław features |= NETIF_F_HW_CSUM; 19795228ddc9SRusty Russell arg &= ~TUN_F_CSUM; 19805228ddc9SRusty Russell 19815228ddc9SRusty Russell if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { 19825228ddc9SRusty Russell if (arg & TUN_F_TSO_ECN) { 19835228ddc9SRusty Russell features |= NETIF_F_TSO_ECN; 19845228ddc9SRusty Russell arg &= ~TUN_F_TSO_ECN; 19855228ddc9SRusty Russell } 19865228ddc9SRusty Russell if (arg & TUN_F_TSO4) 19875228ddc9SRusty Russell features |= NETIF_F_TSO; 19885228ddc9SRusty Russell if (arg & TUN_F_TSO6) 19895228ddc9SRusty Russell features |= NETIF_F_TSO6; 19905228ddc9SRusty Russell arg &= ~(TUN_F_TSO4|TUN_F_TSO6); 19915228ddc9SRusty Russell } 19925228ddc9SRusty Russell } 19935228ddc9SRusty Russell 19945228ddc9SRusty Russell /* This gives the user a way to test for new features in future by 19955228ddc9SRusty Russell * trying to set them. */ 19965228ddc9SRusty Russell if (arg) 19975228ddc9SRusty Russell return -EINVAL; 19985228ddc9SRusty Russell 199988255375SMichał Mirosław tun->set_features = features; 200009050957SYaroslav Isakov tun->dev->wanted_features &= ~TUN_USER_FEATURES; 200109050957SYaroslav Isakov tun->dev->wanted_features |= features; 200288255375SMichał Mirosław netdev_update_features(tun->dev); 20035228ddc9SRusty Russell 20045228ddc9SRusty Russell return 0; 20055228ddc9SRusty Russell } 20065228ddc9SRusty Russell 2007c8d68e6bSJason Wang static void tun_detach_filter(struct tun_struct *tun, int n) 2008c8d68e6bSJason Wang { 2009c8d68e6bSJason Wang int i; 2010c8d68e6bSJason Wang struct tun_file *tfile; 2011c8d68e6bSJason Wang 2012c8d68e6bSJason Wang for (i = 0; i < n; i++) { 2013b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 20148ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 20158ced425eSHannes Frederic Sowa sk_detach_filter(tfile->socket.sk); 20168ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 2017c8d68e6bSJason Wang } 2018c8d68e6bSJason Wang 2019c8d68e6bSJason Wang tun->filter_attached = false; 2020c8d68e6bSJason Wang } 2021c8d68e6bSJason Wang 2022c8d68e6bSJason Wang static int tun_attach_filter(struct tun_struct *tun) 2023c8d68e6bSJason Wang { 2024c8d68e6bSJason Wang int i, ret = 0; 2025c8d68e6bSJason Wang struct tun_file *tfile; 2026c8d68e6bSJason Wang 2027c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 2028b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 20298ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 20308ced425eSHannes Frederic Sowa ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); 20318ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 2032c8d68e6bSJason Wang if (ret) { 2033c8d68e6bSJason Wang tun_detach_filter(tun, i); 2034c8d68e6bSJason Wang return ret; 2035c8d68e6bSJason Wang } 2036c8d68e6bSJason Wang } 2037c8d68e6bSJason Wang 2038c8d68e6bSJason Wang tun->filter_attached = true; 2039c8d68e6bSJason Wang return ret; 2040c8d68e6bSJason Wang } 2041c8d68e6bSJason Wang 2042c8d68e6bSJason Wang static void tun_set_sndbuf(struct tun_struct *tun) 2043c8d68e6bSJason Wang { 2044c8d68e6bSJason Wang struct tun_file *tfile; 2045c8d68e6bSJason Wang int i; 2046c8d68e6bSJason Wang 2047c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 2048b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 2049c8d68e6bSJason Wang tfile->socket.sk->sk_sndbuf = tun->sndbuf; 2050c8d68e6bSJason Wang } 2051c8d68e6bSJason Wang } 2052c8d68e6bSJason Wang 2053cde8b15fSJason Wang static int tun_set_queue(struct file *file, struct ifreq *ifr) 2054cde8b15fSJason Wang { 2055cde8b15fSJason Wang struct tun_file *tfile = file->private_data; 2056cde8b15fSJason Wang struct tun_struct *tun; 2057cde8b15fSJason Wang int ret = 0; 2058cde8b15fSJason Wang 2059cde8b15fSJason Wang rtnl_lock(); 2060cde8b15fSJason Wang 2061cde8b15fSJason Wang if (ifr->ifr_flags & IFF_ATTACH_QUEUE) { 20624008e97fSJason Wang tun = tfile->detached; 20635dbbaf2dSPaul Moore if (!tun) { 2064cde8b15fSJason Wang ret = -EINVAL; 20655dbbaf2dSPaul Moore goto unlock; 20665dbbaf2dSPaul Moore } 20675dbbaf2dSPaul Moore ret = security_tun_dev_attach_queue(tun->security); 20685dbbaf2dSPaul Moore if (ret < 0) 20695dbbaf2dSPaul Moore goto unlock; 2070849c9b6fSPavel Emelyanov ret = tun_attach(tun, file, false); 20714008e97fSJason Wang } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) { 2072b8deabd3SJason Wang tun = rtnl_dereference(tfile->tun); 207340630b82SMichael S. Tsirkin if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached) 20744008e97fSJason Wang ret = -EINVAL; 2075cde8b15fSJason Wang else 20764008e97fSJason Wang __tun_detach(tfile, false); 20774008e97fSJason Wang } else 2078cde8b15fSJason Wang ret = -EINVAL; 2079cde8b15fSJason Wang 20805dbbaf2dSPaul Moore unlock: 2081cde8b15fSJason Wang rtnl_unlock(); 2082cde8b15fSJason Wang return ret; 2083cde8b15fSJason Wang } 2084cde8b15fSJason Wang 208550857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd, 208650857e2aSArnd Bergmann unsigned long arg, int ifreq_len) 20871da177e4SLinus Torvalds { 208836b50babSEric W. Biederman struct tun_file *tfile = file->private_data; 2089631ab46bSEric W. Biederman struct tun_struct *tun; 20901da177e4SLinus Torvalds void __user* argp = (void __user*)arg; 20911da177e4SLinus Torvalds struct ifreq ifr; 20920625c883SEric W. Biederman kuid_t owner; 20930625c883SEric W. Biederman kgid_t group; 209433dccbb0SHerbert Xu int sndbuf; 2095d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 2096fb7589a1SPavel Emelyanov unsigned int ifindex; 20971cf8e410SMichael S. Tsirkin int le; 2098f271b2ccSMax Krasnyansky int ret; 20991da177e4SLinus Torvalds 210020861f26SGao Feng if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == SOCK_IOC_TYPE) { 210150857e2aSArnd Bergmann if (copy_from_user(&ifr, argp, ifreq_len)) 21021da177e4SLinus Torvalds return -EFAULT; 21038bbb1813SDavid S. Miller } else { 2104a117dacdSMathias Krause memset(&ifr, 0, sizeof(ifr)); 21058bbb1813SDavid S. Miller } 2106631ab46bSEric W. Biederman if (cmd == TUNGETFEATURES) { 2107631ab46bSEric W. Biederman /* Currently this just means: "what IFF flags are valid?". 2108631ab46bSEric W. Biederman * This is needed because we never checked for invalid flags on 2109031f5e03SMichael S. Tsirkin * TUNSETIFF. 2110031f5e03SMichael S. Tsirkin */ 2111031f5e03SMichael S. Tsirkin return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES, 2112631ab46bSEric W. Biederman (unsigned int __user*)argp); 2113cde8b15fSJason Wang } else if (cmd == TUNSETQUEUE) 2114cde8b15fSJason Wang return tun_set_queue(file, &ifr); 2115631ab46bSEric W. Biederman 2116c8d68e6bSJason Wang ret = 0; 2117876bfd4dSHerbert Xu rtnl_lock(); 2118876bfd4dSHerbert Xu 211936b50babSEric W. Biederman tun = __tun_get(tfile); 21200f16bc13SGao Feng if (cmd == TUNSETIFF) { 21210f16bc13SGao Feng ret = -EEXIST; 21220f16bc13SGao Feng if (tun) 21230f16bc13SGao Feng goto unlock; 21240f16bc13SGao Feng 21251da177e4SLinus Torvalds ifr.ifr_name[IFNAMSIZ-1] = '\0'; 21261da177e4SLinus Torvalds 2127140e807dSEric W. Biederman ret = tun_set_iff(sock_net(&tfile->sk), file, &ifr); 21281da177e4SLinus Torvalds 2129876bfd4dSHerbert Xu if (ret) 2130876bfd4dSHerbert Xu goto unlock; 21311da177e4SLinus Torvalds 213250857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 2133876bfd4dSHerbert Xu ret = -EFAULT; 2134876bfd4dSHerbert Xu goto unlock; 21351da177e4SLinus Torvalds } 2136fb7589a1SPavel Emelyanov if (cmd == TUNSETIFINDEX) { 2137fb7589a1SPavel Emelyanov ret = -EPERM; 2138fb7589a1SPavel Emelyanov if (tun) 2139fb7589a1SPavel Emelyanov goto unlock; 2140fb7589a1SPavel Emelyanov 2141fb7589a1SPavel Emelyanov ret = -EFAULT; 2142fb7589a1SPavel Emelyanov if (copy_from_user(&ifindex, argp, sizeof(ifindex))) 2143fb7589a1SPavel Emelyanov goto unlock; 2144fb7589a1SPavel Emelyanov 2145fb7589a1SPavel Emelyanov ret = 0; 2146fb7589a1SPavel Emelyanov tfile->ifindex = ifindex; 2147fb7589a1SPavel Emelyanov goto unlock; 2148fb7589a1SPavel Emelyanov } 21491da177e4SLinus Torvalds 2150876bfd4dSHerbert Xu ret = -EBADFD; 21511da177e4SLinus Torvalds if (!tun) 2152876bfd4dSHerbert Xu goto unlock; 21531da177e4SLinus Torvalds 21541e588338SJason Wang tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd); 21551da177e4SLinus Torvalds 2156631ab46bSEric W. Biederman ret = 0; 21571da177e4SLinus Torvalds switch (cmd) { 2158e3b99556SMark McLoughlin case TUNGETIFF: 21599ce99cf6SRami Rosen tun_get_iff(current->nsproxy->net_ns, tun, &ifr); 2160e3b99556SMark McLoughlin 21613d407a80SPavel Emelyanov if (tfile->detached) 21623d407a80SPavel Emelyanov ifr.ifr_flags |= IFF_DETACH_QUEUE; 2163849c9b6fSPavel Emelyanov if (!tfile->socket.sk->sk_filter) 2164849c9b6fSPavel Emelyanov ifr.ifr_flags |= IFF_NOFILTER; 21653d407a80SPavel Emelyanov 216650857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 2167631ab46bSEric W. Biederman ret = -EFAULT; 2168e3b99556SMark McLoughlin break; 2169e3b99556SMark McLoughlin 21701da177e4SLinus Torvalds case TUNSETNOCSUM: 21711da177e4SLinus Torvalds /* Disable/Enable checksum */ 21721da177e4SLinus Torvalds 217388255375SMichał Mirosław /* [unimplemented] */ 217488255375SMichał Mirosław tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n", 21756b8a66eeSJoe Perches arg ? "disabled" : "enabled"); 21761da177e4SLinus Torvalds break; 21771da177e4SLinus Torvalds 21781da177e4SLinus Torvalds case TUNSETPERSIST: 217954f968d6SJason Wang /* Disable/Enable persist mode. Keep an extra reference to the 218054f968d6SJason Wang * module to prevent the module being unprobed. 218154f968d6SJason Wang */ 218240630b82SMichael S. Tsirkin if (arg && !(tun->flags & IFF_PERSIST)) { 218340630b82SMichael S. Tsirkin tun->flags |= IFF_PERSIST; 218454f968d6SJason Wang __module_get(THIS_MODULE); 2185dd38bd85SJason Wang } 218640630b82SMichael S. Tsirkin if (!arg && (tun->flags & IFF_PERSIST)) { 218740630b82SMichael S. Tsirkin tun->flags &= ~IFF_PERSIST; 218854f968d6SJason Wang module_put(THIS_MODULE); 218954f968d6SJason Wang } 21901da177e4SLinus Torvalds 21916b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "persist %s\n", 21926b8a66eeSJoe Perches arg ? "enabled" : "disabled"); 21931da177e4SLinus Torvalds break; 21941da177e4SLinus Torvalds 21951da177e4SLinus Torvalds case TUNSETOWNER: 21961da177e4SLinus Torvalds /* Set owner of the device */ 21970625c883SEric W. Biederman owner = make_kuid(current_user_ns(), arg); 21980625c883SEric W. Biederman if (!uid_valid(owner)) { 21990625c883SEric W. Biederman ret = -EINVAL; 22000625c883SEric W. Biederman break; 22010625c883SEric W. Biederman } 22020625c883SEric W. Biederman tun->owner = owner; 22031e588338SJason Wang tun_debug(KERN_INFO, tun, "owner set to %u\n", 22040625c883SEric W. Biederman from_kuid(&init_user_ns, tun->owner)); 22051da177e4SLinus Torvalds break; 22061da177e4SLinus Torvalds 22078c644623SGuido Guenther case TUNSETGROUP: 22088c644623SGuido Guenther /* Set group of the device */ 22090625c883SEric W. Biederman group = make_kgid(current_user_ns(), arg); 22100625c883SEric W. Biederman if (!gid_valid(group)) { 22110625c883SEric W. Biederman ret = -EINVAL; 22120625c883SEric W. Biederman break; 22130625c883SEric W. Biederman } 22140625c883SEric W. Biederman tun->group = group; 22151e588338SJason Wang tun_debug(KERN_INFO, tun, "group set to %u\n", 22160625c883SEric W. Biederman from_kgid(&init_user_ns, tun->group)); 22178c644623SGuido Guenther break; 22188c644623SGuido Guenther 2219ff4cc3acSMike Kershaw case TUNSETLINK: 2220ff4cc3acSMike Kershaw /* Only allow setting the type when the interface is down */ 2221ff4cc3acSMike Kershaw if (tun->dev->flags & IFF_UP) { 22226b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, 22236b8a66eeSJoe Perches "Linktype set failed because interface is up\n"); 222448abfe05SDavid S. Miller ret = -EBUSY; 2225ff4cc3acSMike Kershaw } else { 2226ff4cc3acSMike Kershaw tun->dev->type = (int) arg; 22276b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "linktype set to %d\n", 22286b8a66eeSJoe Perches tun->dev->type); 222948abfe05SDavid S. Miller ret = 0; 2230ff4cc3acSMike Kershaw } 2231631ab46bSEric W. Biederman break; 2232ff4cc3acSMike Kershaw 22331da177e4SLinus Torvalds #ifdef TUN_DEBUG 22341da177e4SLinus Torvalds case TUNSETDEBUG: 22351da177e4SLinus Torvalds tun->debug = arg; 22361da177e4SLinus Torvalds break; 22371da177e4SLinus Torvalds #endif 22385228ddc9SRusty Russell case TUNSETOFFLOAD: 223988255375SMichał Mirosław ret = set_offload(tun, arg); 2240631ab46bSEric W. Biederman break; 22415228ddc9SRusty Russell 2242f271b2ccSMax Krasnyansky case TUNSETTXFILTER: 2243f271b2ccSMax Krasnyansky /* Can be set only for TAPs */ 2244631ab46bSEric W. Biederman ret = -EINVAL; 224540630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 2246631ab46bSEric W. Biederman break; 2247c0e5a8c2SHarvey Harrison ret = update_filter(&tun->txflt, (void __user *)arg); 2248631ab46bSEric W. Biederman break; 22491da177e4SLinus Torvalds 22501da177e4SLinus Torvalds case SIOCGIFHWADDR: 2251b595076aSUwe Kleine-König /* Get hw address */ 2252f271b2ccSMax Krasnyansky memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); 2253f271b2ccSMax Krasnyansky ifr.ifr_hwaddr.sa_family = tun->dev->type; 225450857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 2255631ab46bSEric W. Biederman ret = -EFAULT; 2256631ab46bSEric W. Biederman break; 22571da177e4SLinus Torvalds 22581da177e4SLinus Torvalds case SIOCSIFHWADDR: 2259f271b2ccSMax Krasnyansky /* Set hw address */ 22606b8a66eeSJoe Perches tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n", 22616b8a66eeSJoe Perches ifr.ifr_hwaddr.sa_data); 226240102371SKim B. Heino 226340102371SKim B. Heino ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); 2264631ab46bSEric W. Biederman break; 226533dccbb0SHerbert Xu 226633dccbb0SHerbert Xu case TUNGETSNDBUF: 226754f968d6SJason Wang sndbuf = tfile->socket.sk->sk_sndbuf; 226833dccbb0SHerbert Xu if (copy_to_user(argp, &sndbuf, sizeof(sndbuf))) 226933dccbb0SHerbert Xu ret = -EFAULT; 227033dccbb0SHerbert Xu break; 227133dccbb0SHerbert Xu 227233dccbb0SHerbert Xu case TUNSETSNDBUF: 227333dccbb0SHerbert Xu if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) { 227433dccbb0SHerbert Xu ret = -EFAULT; 227533dccbb0SHerbert Xu break; 227633dccbb0SHerbert Xu } 227733dccbb0SHerbert Xu 2278c8d68e6bSJason Wang tun->sndbuf = sndbuf; 2279c8d68e6bSJason Wang tun_set_sndbuf(tun); 228033dccbb0SHerbert Xu break; 228133dccbb0SHerbert Xu 2282d9d52b51SMichael S. Tsirkin case TUNGETVNETHDRSZ: 2283d9d52b51SMichael S. Tsirkin vnet_hdr_sz = tun->vnet_hdr_sz; 2284d9d52b51SMichael S. Tsirkin if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz))) 2285d9d52b51SMichael S. Tsirkin ret = -EFAULT; 2286d9d52b51SMichael S. Tsirkin break; 2287d9d52b51SMichael S. Tsirkin 2288d9d52b51SMichael S. Tsirkin case TUNSETVNETHDRSZ: 2289d9d52b51SMichael S. Tsirkin if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) { 2290d9d52b51SMichael S. Tsirkin ret = -EFAULT; 2291d9d52b51SMichael S. Tsirkin break; 2292d9d52b51SMichael S. Tsirkin } 2293d9d52b51SMichael S. Tsirkin if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) { 2294d9d52b51SMichael S. Tsirkin ret = -EINVAL; 2295d9d52b51SMichael S. Tsirkin break; 2296d9d52b51SMichael S. Tsirkin } 2297d9d52b51SMichael S. Tsirkin 2298d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = vnet_hdr_sz; 2299d9d52b51SMichael S. Tsirkin break; 2300d9d52b51SMichael S. Tsirkin 23011cf8e410SMichael S. Tsirkin case TUNGETVNETLE: 23021cf8e410SMichael S. Tsirkin le = !!(tun->flags & TUN_VNET_LE); 23031cf8e410SMichael S. Tsirkin if (put_user(le, (int __user *)argp)) 23041cf8e410SMichael S. Tsirkin ret = -EFAULT; 23051cf8e410SMichael S. Tsirkin break; 23061cf8e410SMichael S. Tsirkin 23071cf8e410SMichael S. Tsirkin case TUNSETVNETLE: 23081cf8e410SMichael S. Tsirkin if (get_user(le, (int __user *)argp)) { 23091cf8e410SMichael S. Tsirkin ret = -EFAULT; 23101cf8e410SMichael S. Tsirkin break; 23111cf8e410SMichael S. Tsirkin } 23121cf8e410SMichael S. Tsirkin if (le) 23131cf8e410SMichael S. Tsirkin tun->flags |= TUN_VNET_LE; 23141cf8e410SMichael S. Tsirkin else 23151cf8e410SMichael S. Tsirkin tun->flags &= ~TUN_VNET_LE; 23161cf8e410SMichael S. Tsirkin break; 23171cf8e410SMichael S. Tsirkin 23188b8e658bSGreg Kurz case TUNGETVNETBE: 23198b8e658bSGreg Kurz ret = tun_get_vnet_be(tun, argp); 23208b8e658bSGreg Kurz break; 23218b8e658bSGreg Kurz 23228b8e658bSGreg Kurz case TUNSETVNETBE: 23238b8e658bSGreg Kurz ret = tun_set_vnet_be(tun, argp); 23248b8e658bSGreg Kurz break; 23258b8e658bSGreg Kurz 232699405162SMichael S. Tsirkin case TUNATTACHFILTER: 232799405162SMichael S. Tsirkin /* Can be set only for TAPs */ 232899405162SMichael S. Tsirkin ret = -EINVAL; 232940630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 233099405162SMichael S. Tsirkin break; 233199405162SMichael S. Tsirkin ret = -EFAULT; 233254f968d6SJason Wang if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog))) 233399405162SMichael S. Tsirkin break; 233499405162SMichael S. Tsirkin 2335c8d68e6bSJason Wang ret = tun_attach_filter(tun); 233699405162SMichael S. Tsirkin break; 233799405162SMichael S. Tsirkin 233899405162SMichael S. Tsirkin case TUNDETACHFILTER: 233999405162SMichael S. Tsirkin /* Can be set only for TAPs */ 234099405162SMichael S. Tsirkin ret = -EINVAL; 234140630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 234299405162SMichael S. Tsirkin break; 2343c8d68e6bSJason Wang ret = 0; 2344c8d68e6bSJason Wang tun_detach_filter(tun, tun->numqueues); 234599405162SMichael S. Tsirkin break; 234699405162SMichael S. Tsirkin 234776975e9cSPavel Emelyanov case TUNGETFILTER: 234876975e9cSPavel Emelyanov ret = -EINVAL; 234940630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 235076975e9cSPavel Emelyanov break; 235176975e9cSPavel Emelyanov ret = -EFAULT; 235276975e9cSPavel Emelyanov if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog))) 235376975e9cSPavel Emelyanov break; 235476975e9cSPavel Emelyanov ret = 0; 235576975e9cSPavel Emelyanov break; 235676975e9cSPavel Emelyanov 23571da177e4SLinus Torvalds default: 2358631ab46bSEric W. Biederman ret = -EINVAL; 2359631ab46bSEric W. Biederman break; 2360ee289b64SJoe Perches } 23611da177e4SLinus Torvalds 2362876bfd4dSHerbert Xu unlock: 2363876bfd4dSHerbert Xu rtnl_unlock(); 2364876bfd4dSHerbert Xu if (tun) 2365631ab46bSEric W. Biederman tun_put(tun); 2366631ab46bSEric W. Biederman return ret; 23671da177e4SLinus Torvalds } 23681da177e4SLinus Torvalds 236950857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file, 237050857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 237150857e2aSArnd Bergmann { 237250857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq)); 237350857e2aSArnd Bergmann } 237450857e2aSArnd Bergmann 237550857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 237650857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file, 237750857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 237850857e2aSArnd Bergmann { 237950857e2aSArnd Bergmann switch (cmd) { 238050857e2aSArnd Bergmann case TUNSETIFF: 238150857e2aSArnd Bergmann case TUNGETIFF: 238250857e2aSArnd Bergmann case TUNSETTXFILTER: 238350857e2aSArnd Bergmann case TUNGETSNDBUF: 238450857e2aSArnd Bergmann case TUNSETSNDBUF: 238550857e2aSArnd Bergmann case SIOCGIFHWADDR: 238650857e2aSArnd Bergmann case SIOCSIFHWADDR: 238750857e2aSArnd Bergmann arg = (unsigned long)compat_ptr(arg); 238850857e2aSArnd Bergmann break; 238950857e2aSArnd Bergmann default: 239050857e2aSArnd Bergmann arg = (compat_ulong_t)arg; 239150857e2aSArnd Bergmann break; 239250857e2aSArnd Bergmann } 239350857e2aSArnd Bergmann 239450857e2aSArnd Bergmann /* 239550857e2aSArnd Bergmann * compat_ifreq is shorter than ifreq, so we must not access beyond 239650857e2aSArnd Bergmann * the end of that structure. All fields that are used in this 239750857e2aSArnd Bergmann * driver are compatible though, we don't need to convert the 239850857e2aSArnd Bergmann * contents. 239950857e2aSArnd Bergmann */ 240050857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq)); 240150857e2aSArnd Bergmann } 240250857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */ 240350857e2aSArnd Bergmann 24041da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on) 24051da177e4SLinus Torvalds { 240654f968d6SJason Wang struct tun_file *tfile = file->private_data; 24071da177e4SLinus Torvalds int ret; 24081da177e4SLinus Torvalds 240954f968d6SJason Wang if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) 24109d319522SJonathan Corbet goto out; 24111da177e4SLinus Torvalds 24121da177e4SLinus Torvalds if (on) { 2413e0b93eddSJeff Layton __f_setown(file, task_pid(current), PIDTYPE_PID, 0); 241454f968d6SJason Wang tfile->flags |= TUN_FASYNC; 24151da177e4SLinus Torvalds } else 241654f968d6SJason Wang tfile->flags &= ~TUN_FASYNC; 24179d319522SJonathan Corbet ret = 0; 24189d319522SJonathan Corbet out: 24199d319522SJonathan Corbet return ret; 24201da177e4SLinus Torvalds } 24211da177e4SLinus Torvalds 24221da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file) 24231da177e4SLinus Torvalds { 2424140e807dSEric W. Biederman struct net *net = current->nsproxy->net_ns; 2425631ab46bSEric W. Biederman struct tun_file *tfile; 2426deed49fbSThomas Gleixner 24276b8a66eeSJoe Perches DBG1(KERN_INFO, "tunX: tun_chr_open\n"); 2428631ab46bSEric W. Biederman 2429140e807dSEric W. Biederman tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, 243011aa9c28SEric W. Biederman &tun_proto, 0); 2431631ab46bSEric W. Biederman if (!tfile) 2432631ab46bSEric W. Biederman return -ENOMEM; 2433c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 243454f968d6SJason Wang tfile->flags = 0; 2435fb7589a1SPavel Emelyanov tfile->ifindex = 0; 243654f968d6SJason Wang 243754f968d6SJason Wang init_waitqueue_head(&tfile->wq.wait); 24389e641bdcSXi Wang RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq); 243954f968d6SJason Wang 244054f968d6SJason Wang tfile->socket.file = file; 244154f968d6SJason Wang tfile->socket.ops = &tun_socket_ops; 244254f968d6SJason Wang 244354f968d6SJason Wang sock_init_data(&tfile->socket, &tfile->sk); 244454f968d6SJason Wang 244554f968d6SJason Wang tfile->sk.sk_write_space = tun_sock_write_space; 244654f968d6SJason Wang tfile->sk.sk_sndbuf = INT_MAX; 244754f968d6SJason Wang 2448*66ccbc9cSJason Wang tfile->alloc_frag.page = NULL; 2449*66ccbc9cSJason Wang 2450631ab46bSEric W. Biederman file->private_data = tfile; 24514008e97fSJason Wang INIT_LIST_HEAD(&tfile->next); 245254f968d6SJason Wang 245319a6afb2SJason Wang sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); 245419a6afb2SJason Wang 24551da177e4SLinus Torvalds return 0; 24561da177e4SLinus Torvalds } 24571da177e4SLinus Torvalds 24581da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file) 24591da177e4SLinus Torvalds { 2460631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 24611da177e4SLinus Torvalds 2462c8d68e6bSJason Wang tun_detach(tfile, true); 24631da177e4SLinus Torvalds 24641da177e4SLinus Torvalds return 0; 24651da177e4SLinus Torvalds } 24661da177e4SLinus Torvalds 246793e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS 2468a3816ab0SJoe Perches static void tun_chr_show_fdinfo(struct seq_file *m, struct file *f) 246993e14b6dSMasatake YAMATO { 247093e14b6dSMasatake YAMATO struct tun_struct *tun; 247193e14b6dSMasatake YAMATO struct ifreq ifr; 247293e14b6dSMasatake YAMATO 247393e14b6dSMasatake YAMATO memset(&ifr, 0, sizeof(ifr)); 247493e14b6dSMasatake YAMATO 247593e14b6dSMasatake YAMATO rtnl_lock(); 247693e14b6dSMasatake YAMATO tun = tun_get(f); 247793e14b6dSMasatake YAMATO if (tun) 247893e14b6dSMasatake YAMATO tun_get_iff(current->nsproxy->net_ns, tun, &ifr); 247993e14b6dSMasatake YAMATO rtnl_unlock(); 248093e14b6dSMasatake YAMATO 248193e14b6dSMasatake YAMATO if (tun) 248293e14b6dSMasatake YAMATO tun_put(tun); 248393e14b6dSMasatake YAMATO 2484a3816ab0SJoe Perches seq_printf(m, "iff:\t%s\n", ifr.ifr_name); 248593e14b6dSMasatake YAMATO } 248693e14b6dSMasatake YAMATO #endif 248793e14b6dSMasatake YAMATO 2488d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = { 24891da177e4SLinus Torvalds .owner = THIS_MODULE, 24901da177e4SLinus Torvalds .llseek = no_llseek, 24919b067034SAl Viro .read_iter = tun_chr_read_iter, 2492f5ff53b4SAl Viro .write_iter = tun_chr_write_iter, 24931da177e4SLinus Torvalds .poll = tun_chr_poll, 2494876bfd4dSHerbert Xu .unlocked_ioctl = tun_chr_ioctl, 249550857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 249650857e2aSArnd Bergmann .compat_ioctl = tun_chr_compat_ioctl, 249750857e2aSArnd Bergmann #endif 24981da177e4SLinus Torvalds .open = tun_chr_open, 24991da177e4SLinus Torvalds .release = tun_chr_close, 250093e14b6dSMasatake YAMATO .fasync = tun_chr_fasync, 250193e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS 250293e14b6dSMasatake YAMATO .show_fdinfo = tun_chr_show_fdinfo, 250393e14b6dSMasatake YAMATO #endif 25041da177e4SLinus Torvalds }; 25051da177e4SLinus Torvalds 25061da177e4SLinus Torvalds static struct miscdevice tun_miscdev = { 25071da177e4SLinus Torvalds .minor = TUN_MINOR, 25081da177e4SLinus Torvalds .name = "tun", 2509e454cea2SKay Sievers .nodename = "net/tun", 25101da177e4SLinus Torvalds .fops = &tun_fops, 25111da177e4SLinus Torvalds }; 25121da177e4SLinus Torvalds 25131da177e4SLinus Torvalds /* ethtool interface */ 25141da177e4SLinus Torvalds 251529ccc49dSPhilippe Reynes static int tun_get_link_ksettings(struct net_device *dev, 251629ccc49dSPhilippe Reynes struct ethtool_link_ksettings *cmd) 25171da177e4SLinus Torvalds { 251829ccc49dSPhilippe Reynes ethtool_link_ksettings_zero_link_mode(cmd, supported); 251929ccc49dSPhilippe Reynes ethtool_link_ksettings_zero_link_mode(cmd, advertising); 252029ccc49dSPhilippe Reynes cmd->base.speed = SPEED_10; 252129ccc49dSPhilippe Reynes cmd->base.duplex = DUPLEX_FULL; 252229ccc49dSPhilippe Reynes cmd->base.port = PORT_TP; 252329ccc49dSPhilippe Reynes cmd->base.phy_address = 0; 252429ccc49dSPhilippe Reynes cmd->base.autoneg = AUTONEG_DISABLE; 25251da177e4SLinus Torvalds return 0; 25261da177e4SLinus Torvalds } 25271da177e4SLinus Torvalds 25281da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) 25291da177e4SLinus Torvalds { 25301da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 25311da177e4SLinus Torvalds 253233a5ba14SRick Jones strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); 253333a5ba14SRick Jones strlcpy(info->version, DRV_VERSION, sizeof(info->version)); 25341da177e4SLinus Torvalds 25351da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 253640630b82SMichael S. Tsirkin case IFF_TUN: 253733a5ba14SRick Jones strlcpy(info->bus_info, "tun", sizeof(info->bus_info)); 25381da177e4SLinus Torvalds break; 253940630b82SMichael S. Tsirkin case IFF_TAP: 254033a5ba14SRick Jones strlcpy(info->bus_info, "tap", sizeof(info->bus_info)); 25411da177e4SLinus Torvalds break; 25421da177e4SLinus Torvalds } 25431da177e4SLinus Torvalds } 25441da177e4SLinus Torvalds 25451da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev) 25461da177e4SLinus Torvalds { 25471da177e4SLinus Torvalds #ifdef TUN_DEBUG 25481da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 25491da177e4SLinus Torvalds return tun->debug; 25501da177e4SLinus Torvalds #else 25511da177e4SLinus Torvalds return -EOPNOTSUPP; 25521da177e4SLinus Torvalds #endif 25531da177e4SLinus Torvalds } 25541da177e4SLinus Torvalds 25551da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value) 25561da177e4SLinus Torvalds { 25571da177e4SLinus Torvalds #ifdef TUN_DEBUG 25581da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 25591da177e4SLinus Torvalds tun->debug = value; 25601da177e4SLinus Torvalds #endif 25611da177e4SLinus Torvalds } 25621da177e4SLinus Torvalds 25635503fcecSJason Wang static int tun_get_coalesce(struct net_device *dev, 25645503fcecSJason Wang struct ethtool_coalesce *ec) 25655503fcecSJason Wang { 25665503fcecSJason Wang struct tun_struct *tun = netdev_priv(dev); 25675503fcecSJason Wang 25685503fcecSJason Wang ec->rx_max_coalesced_frames = tun->rx_batched; 25695503fcecSJason Wang 25705503fcecSJason Wang return 0; 25715503fcecSJason Wang } 25725503fcecSJason Wang 25735503fcecSJason Wang static int tun_set_coalesce(struct net_device *dev, 25745503fcecSJason Wang struct ethtool_coalesce *ec) 25755503fcecSJason Wang { 25765503fcecSJason Wang struct tun_struct *tun = netdev_priv(dev); 25775503fcecSJason Wang 25785503fcecSJason Wang if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT) 25795503fcecSJason Wang tun->rx_batched = NAPI_POLL_WEIGHT; 25805503fcecSJason Wang else 25815503fcecSJason Wang tun->rx_batched = ec->rx_max_coalesced_frames; 25825503fcecSJason Wang 25835503fcecSJason Wang return 0; 25845503fcecSJason Wang } 25855503fcecSJason Wang 25867282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = { 25871da177e4SLinus Torvalds .get_drvinfo = tun_get_drvinfo, 25881da177e4SLinus Torvalds .get_msglevel = tun_get_msglevel, 25891da177e4SLinus Torvalds .set_msglevel = tun_set_msglevel, 2590bee31369SNolan Leake .get_link = ethtool_op_get_link, 2591eda29772SRichard Cochran .get_ts_info = ethtool_op_get_ts_info, 25925503fcecSJason Wang .get_coalesce = tun_get_coalesce, 25935503fcecSJason Wang .set_coalesce = tun_set_coalesce, 259429ccc49dSPhilippe Reynes .get_link_ksettings = tun_get_link_ksettings, 25951da177e4SLinus Torvalds }; 25961da177e4SLinus Torvalds 25971576d986SJason Wang static int tun_queue_resize(struct tun_struct *tun) 25981576d986SJason Wang { 25991576d986SJason Wang struct net_device *dev = tun->dev; 26001576d986SJason Wang struct tun_file *tfile; 26011576d986SJason Wang struct skb_array **arrays; 26021576d986SJason Wang int n = tun->numqueues + tun->numdisabled; 26031576d986SJason Wang int ret, i; 26041576d986SJason Wang 26051576d986SJason Wang arrays = kmalloc(sizeof *arrays * n, GFP_KERNEL); 26061576d986SJason Wang if (!arrays) 26071576d986SJason Wang return -ENOMEM; 26081576d986SJason Wang 26091576d986SJason Wang for (i = 0; i < tun->numqueues; i++) { 26101576d986SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 26111576d986SJason Wang arrays[i] = &tfile->tx_array; 26121576d986SJason Wang } 26131576d986SJason Wang list_for_each_entry(tfile, &tun->disabled, next) 26141576d986SJason Wang arrays[i++] = &tfile->tx_array; 26151576d986SJason Wang 26161576d986SJason Wang ret = skb_array_resize_multiple(arrays, n, 26171576d986SJason Wang dev->tx_queue_len, GFP_KERNEL); 26181576d986SJason Wang 26191576d986SJason Wang kfree(arrays); 26201576d986SJason Wang return ret; 26211576d986SJason Wang } 26221576d986SJason Wang 26231576d986SJason Wang static int tun_device_event(struct notifier_block *unused, 26241576d986SJason Wang unsigned long event, void *ptr) 26251576d986SJason Wang { 26261576d986SJason Wang struct net_device *dev = netdev_notifier_info_to_dev(ptr); 26271576d986SJason Wang struct tun_struct *tun = netdev_priv(dev); 26281576d986SJason Wang 262986dfb4acSCraig Gallek if (dev->rtnl_link_ops != &tun_link_ops) 263086dfb4acSCraig Gallek return NOTIFY_DONE; 263186dfb4acSCraig Gallek 26321576d986SJason Wang switch (event) { 26331576d986SJason Wang case NETDEV_CHANGE_TX_QUEUE_LEN: 26341576d986SJason Wang if (tun_queue_resize(tun)) 26351576d986SJason Wang return NOTIFY_BAD; 26361576d986SJason Wang break; 26371576d986SJason Wang default: 26381576d986SJason Wang break; 26391576d986SJason Wang } 26401576d986SJason Wang 26411576d986SJason Wang return NOTIFY_DONE; 26421576d986SJason Wang } 26431576d986SJason Wang 26441576d986SJason Wang static struct notifier_block tun_notifier_block __read_mostly = { 26451576d986SJason Wang .notifier_call = tun_device_event, 26461576d986SJason Wang }; 264779d17604SPavel Emelyanov 26481da177e4SLinus Torvalds static int __init tun_init(void) 26491da177e4SLinus Torvalds { 26501da177e4SLinus Torvalds int ret = 0; 26511da177e4SLinus Torvalds 26526b8a66eeSJoe Perches pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION); 26531da177e4SLinus Torvalds 2654f019a7a5SEric W. Biederman ret = rtnl_link_register(&tun_link_ops); 265579d17604SPavel Emelyanov if (ret) { 26566b8a66eeSJoe Perches pr_err("Can't register link_ops\n"); 2657f019a7a5SEric W. Biederman goto err_linkops; 265879d17604SPavel Emelyanov } 265979d17604SPavel Emelyanov 26601da177e4SLinus Torvalds ret = misc_register(&tun_miscdev); 266179d17604SPavel Emelyanov if (ret) { 26626b8a66eeSJoe Perches pr_err("Can't register misc device %d\n", TUN_MINOR); 266379d17604SPavel Emelyanov goto err_misc; 266479d17604SPavel Emelyanov } 26651576d986SJason Wang 26665edfbd3cSTonghao Zhang ret = register_netdevice_notifier(&tun_notifier_block); 26675edfbd3cSTonghao Zhang if (ret) { 26685edfbd3cSTonghao Zhang pr_err("Can't register netdevice notifier\n"); 26695edfbd3cSTonghao Zhang goto err_notifier; 26705edfbd3cSTonghao Zhang } 26715edfbd3cSTonghao Zhang 267279d17604SPavel Emelyanov return 0; 26735edfbd3cSTonghao Zhang 26745edfbd3cSTonghao Zhang err_notifier: 26755edfbd3cSTonghao Zhang misc_deregister(&tun_miscdev); 267679d17604SPavel Emelyanov err_misc: 2677f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 2678f019a7a5SEric W. Biederman err_linkops: 26791da177e4SLinus Torvalds return ret; 26801da177e4SLinus Torvalds } 26811da177e4SLinus Torvalds 26821da177e4SLinus Torvalds static void tun_cleanup(void) 26831da177e4SLinus Torvalds { 26841da177e4SLinus Torvalds misc_deregister(&tun_miscdev); 2685f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 26861576d986SJason Wang unregister_netdevice_notifier(&tun_notifier_block); 26871da177e4SLinus Torvalds } 26881da177e4SLinus Torvalds 268905c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file. Returns error unless file is 269005c2828cSMichael S. Tsirkin * attached to a device. The returned object works like a packet socket, it 269105c2828cSMichael S. Tsirkin * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for 269205c2828cSMichael S. Tsirkin * holding a reference to the file for as long as the socket is in use. */ 269305c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file) 269405c2828cSMichael S. Tsirkin { 26956e914fc7SJason Wang struct tun_file *tfile; 269605c2828cSMichael S. Tsirkin if (file->f_op != &tun_fops) 269705c2828cSMichael S. Tsirkin return ERR_PTR(-EINVAL); 26986e914fc7SJason Wang tfile = file->private_data; 26996e914fc7SJason Wang if (!tfile) 270005c2828cSMichael S. Tsirkin return ERR_PTR(-EBADFD); 270154f968d6SJason Wang return &tfile->socket; 270205c2828cSMichael S. Tsirkin } 270305c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket); 270405c2828cSMichael S. Tsirkin 270583339c6bSJason Wang struct skb_array *tun_get_skb_array(struct file *file) 270683339c6bSJason Wang { 270783339c6bSJason Wang struct tun_file *tfile; 270883339c6bSJason Wang 270983339c6bSJason Wang if (file->f_op != &tun_fops) 271083339c6bSJason Wang return ERR_PTR(-EINVAL); 271183339c6bSJason Wang tfile = file->private_data; 271283339c6bSJason Wang if (!tfile) 271383339c6bSJason Wang return ERR_PTR(-EBADFD); 271483339c6bSJason Wang return &tfile->tx_array; 271583339c6bSJason Wang } 271683339c6bSJason Wang EXPORT_SYMBOL_GPL(tun_get_skb_array); 271783339c6bSJason Wang 27181da177e4SLinus Torvalds module_init(tun_init); 27191da177e4SLinus Torvalds module_exit(tun_cleanup); 27201da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION); 27211da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT); 27221da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 27231da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR); 2724578454ffSKay Sievers MODULE_ALIAS("devname:net/tun"); 2725