11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * TUN - Universal TUN/TAP device driver. 31da177e4SLinus Torvalds * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com> 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 61da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 71da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 81da177e4SLinus Torvalds * (at your option) any later version. 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 111da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 121da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 131da177e4SLinus Torvalds * GNU General Public License for more details. 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $ 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds /* 191da177e4SLinus Torvalds * Changes: 201da177e4SLinus Torvalds * 21ff4cc3acSMike Kershaw * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14 22ff4cc3acSMike Kershaw * Add TUNSETLINK ioctl to set the link encapsulation 23ff4cc3acSMike Kershaw * 241da177e4SLinus Torvalds * Mark Smith <markzzzsmith@yahoo.com.au> 25344dc8edSJoe Perches * Use eth_random_addr() for tap MAC address. 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20 281da177e4SLinus Torvalds * Fixes in packet dropping, queue length setting and queue wakeup. 291da177e4SLinus Torvalds * Increased default tx queue length. 301da177e4SLinus Torvalds * Added ethtool API. 311da177e4SLinus Torvalds * Minor cleanups 321da177e4SLinus Torvalds * 331da177e4SLinus Torvalds * Daniel Podlejski <underley@underley.eu.org> 341da177e4SLinus Torvalds * Modifications for 2.3.99-pre5 kernel. 351da177e4SLinus Torvalds */ 361da177e4SLinus Torvalds 376b8a66eeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 386b8a66eeSJoe Perches 391da177e4SLinus Torvalds #define DRV_NAME "tun" 401da177e4SLinus Torvalds #define DRV_VERSION "1.6" 411da177e4SLinus Torvalds #define DRV_DESCRIPTION "Universal TUN/TAP device driver" 421da177e4SLinus Torvalds #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>" 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds #include <linux/module.h> 451da177e4SLinus Torvalds #include <linux/errno.h> 461da177e4SLinus Torvalds #include <linux/kernel.h> 471da177e4SLinus Torvalds #include <linux/major.h> 481da177e4SLinus Torvalds #include <linux/slab.h> 491da177e4SLinus Torvalds #include <linux/poll.h> 501da177e4SLinus Torvalds #include <linux/fcntl.h> 511da177e4SLinus Torvalds #include <linux/init.h> 521da177e4SLinus Torvalds #include <linux/skbuff.h> 531da177e4SLinus Torvalds #include <linux/netdevice.h> 541da177e4SLinus Torvalds #include <linux/etherdevice.h> 551da177e4SLinus Torvalds #include <linux/miscdevice.h> 561da177e4SLinus Torvalds #include <linux/ethtool.h> 571da177e4SLinus Torvalds #include <linux/rtnetlink.h> 5850857e2aSArnd Bergmann #include <linux/compat.h> 591da177e4SLinus Torvalds #include <linux/if.h> 601da177e4SLinus Torvalds #include <linux/if_arp.h> 611da177e4SLinus Torvalds #include <linux/if_ether.h> 621da177e4SLinus Torvalds #include <linux/if_tun.h> 636680ec68SJason Wang #include <linux/if_vlan.h> 641da177e4SLinus Torvalds #include <linux/crc32.h> 65d647a591SPavel Emelyanov #include <linux/nsproxy.h> 66f43798c2SRusty Russell #include <linux/virtio_net.h> 6799405162SMichael S. Tsirkin #include <linux/rcupdate.h> 68881d966bSEric W. Biederman #include <net/net_namespace.h> 6979d17604SPavel Emelyanov #include <net/netns/generic.h> 70f019a7a5SEric W. Biederman #include <net/rtnetlink.h> 7133dccbb0SHerbert Xu #include <net/sock.h> 7293e14b6dSMasatake YAMATO #include <linux/seq_file.h> 73e0b46d0eSHerbert Xu #include <linux/uio.h> 741576d986SJason Wang #include <linux/skb_array.h> 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds #include <asm/uaccess.h> 771da177e4SLinus Torvalds 7814daa021SRusty Russell /* Uncomment to enable debugging */ 7914daa021SRusty Russell /* #define TUN_DEBUG 1 */ 8014daa021SRusty Russell 811da177e4SLinus Torvalds #ifdef TUN_DEBUG 821da177e4SLinus Torvalds static int debug; 8314daa021SRusty Russell 846b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...) \ 856b8a66eeSJoe Perches do { \ 866b8a66eeSJoe Perches if (tun->debug) \ 876b8a66eeSJoe Perches netdev_printk(level, tun->dev, fmt, ##args); \ 886b8a66eeSJoe Perches } while (0) 896b8a66eeSJoe Perches #define DBG1(level, fmt, args...) \ 906b8a66eeSJoe Perches do { \ 916b8a66eeSJoe Perches if (debug == 2) \ 926b8a66eeSJoe Perches printk(level fmt, ##args); \ 936b8a66eeSJoe Perches } while (0) 9414daa021SRusty Russell #else 956b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...) \ 966b8a66eeSJoe Perches do { \ 976b8a66eeSJoe Perches if (0) \ 986b8a66eeSJoe Perches netdev_printk(level, tun->dev, fmt, ##args); \ 996b8a66eeSJoe Perches } while (0) 1006b8a66eeSJoe Perches #define DBG1(level, fmt, args...) \ 1016b8a66eeSJoe Perches do { \ 1026b8a66eeSJoe Perches if (0) \ 1036b8a66eeSJoe Perches printk(level fmt, ##args); \ 1046b8a66eeSJoe Perches } while (0) 1051da177e4SLinus Torvalds #endif 1061da177e4SLinus Torvalds 107031f5e03SMichael S. Tsirkin /* TUN device flags */ 108031f5e03SMichael S. Tsirkin 109031f5e03SMichael S. Tsirkin /* IFF_ATTACH_QUEUE is never stored in device flags, 110031f5e03SMichael S. Tsirkin * overload it to mean fasync when stored there. 111031f5e03SMichael S. Tsirkin */ 112031f5e03SMichael S. Tsirkin #define TUN_FASYNC IFF_ATTACH_QUEUE 1131cf8e410SMichael S. Tsirkin /* High bits in flags field are unused. */ 1141cf8e410SMichael S. Tsirkin #define TUN_VNET_LE 0x80000000 1158b8e658bSGreg Kurz #define TUN_VNET_BE 0x40000000 116031f5e03SMichael S. Tsirkin 117031f5e03SMichael S. Tsirkin #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \ 1181cf8e410SMichael S. Tsirkin IFF_MULTI_QUEUE) 1190690899bSMichael S. Tsirkin #define GOODCOPY_LEN 128 1200690899bSMichael S. Tsirkin 121f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8 122f271b2ccSMax Krasnyansky struct tap_filter { 123f271b2ccSMax Krasnyansky unsigned int count; /* Number of addrs. Zero means disabled */ 124f271b2ccSMax Krasnyansky u32 mask[2]; /* Mask of the hashed addrs */ 125f271b2ccSMax Krasnyansky unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN]; 126f271b2ccSMax Krasnyansky }; 127f271b2ccSMax Krasnyansky 128baf71c5cSPankaj Gupta /* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal 129baf71c5cSPankaj Gupta * to max number of VCPUs in guest. */ 130baf71c5cSPankaj Gupta #define MAX_TAP_QUEUES 256 131b8732fb7SJason Wang #define MAX_TAP_FLOWS 4096 132c8d68e6bSJason Wang 13396442e42SJason Wang #define TUN_FLOW_EXPIRE (3 * HZ) 13496442e42SJason Wang 135608b9977SPaolo Abeni struct tun_pcpu_stats { 136608b9977SPaolo Abeni u64 rx_packets; 137608b9977SPaolo Abeni u64 rx_bytes; 138608b9977SPaolo Abeni u64 tx_packets; 139608b9977SPaolo Abeni u64 tx_bytes; 140608b9977SPaolo Abeni struct u64_stats_sync syncp; 141608b9977SPaolo Abeni u32 rx_dropped; 142608b9977SPaolo Abeni u32 tx_dropped; 143608b9977SPaolo Abeni u32 rx_frame_errors; 144608b9977SPaolo Abeni }; 145608b9977SPaolo Abeni 14654f968d6SJason Wang /* A tun_file connects an open character device to a tuntap netdevice. It 14792d4ea6eSstephen hemminger * also contains all socket related structures (except sock_fprog and tap_filter) 14854f968d6SJason Wang * to serve as one transmit queue for tuntap device. The sock_fprog and 14954f968d6SJason Wang * tap_filter were kept in tun_struct since they were used for filtering for the 15036fe8c09SRami Rosen * netdevice not for a specific queue (at least I didn't see the requirement for 15154f968d6SJason Wang * this). 1526e914fc7SJason Wang * 1536e914fc7SJason Wang * RCU usage: 15436fe8c09SRami Rosen * The tun_file and tun_struct are loosely coupled, the pointer from one to the 1556e914fc7SJason Wang * other can only be read while rcu_read_lock or rtnl_lock is held. 15654f968d6SJason Wang */ 157631ab46bSEric W. Biederman struct tun_file { 15854f968d6SJason Wang struct sock sk; 15954f968d6SJason Wang struct socket socket; 16054f968d6SJason Wang struct socket_wq wq; 1616e914fc7SJason Wang struct tun_struct __rcu *tun; 16254f968d6SJason Wang struct fasync_struct *fasync; 16354f968d6SJason Wang /* only used for fasnyc */ 16454f968d6SJason Wang unsigned int flags; 165fb7589a1SPavel Emelyanov union { 166c8d68e6bSJason Wang u16 queue_index; 167fb7589a1SPavel Emelyanov unsigned int ifindex; 168fb7589a1SPavel Emelyanov }; 1694008e97fSJason Wang struct list_head next; 1704008e97fSJason Wang struct tun_struct *detached; 1711576d986SJason Wang struct skb_array tx_array; 172631ab46bSEric W. Biederman }; 173631ab46bSEric W. Biederman 17496442e42SJason Wang struct tun_flow_entry { 17596442e42SJason Wang struct hlist_node hash_link; 17696442e42SJason Wang struct rcu_head rcu; 17796442e42SJason Wang struct tun_struct *tun; 17896442e42SJason Wang 17996442e42SJason Wang u32 rxhash; 1809bc88939STom Herbert u32 rps_rxhash; 18196442e42SJason Wang int queue_index; 18296442e42SJason Wang unsigned long updated; 18396442e42SJason Wang }; 18496442e42SJason Wang 18596442e42SJason Wang #define TUN_NUM_FLOW_ENTRIES 1024 18696442e42SJason Wang 18754f968d6SJason Wang /* Since the socket were moved to tun_file, to preserve the behavior of persist 18836fe8c09SRami Rosen * device, socket filter, sndbuf and vnet header size were restore when the 18954f968d6SJason Wang * file were attached to a persist device. 19054f968d6SJason Wang */ 19114daa021SRusty Russell struct tun_struct { 192c8d68e6bSJason Wang struct tun_file __rcu *tfiles[MAX_TAP_QUEUES]; 193c8d68e6bSJason Wang unsigned int numqueues; 194f271b2ccSMax Krasnyansky unsigned int flags; 1950625c883SEric W. Biederman kuid_t owner; 1960625c883SEric W. Biederman kgid_t group; 19714daa021SRusty Russell 19814daa021SRusty Russell struct net_device *dev; 199c8f44affSMichał Mirosław netdev_features_t set_features; 20088255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \ 201e3e3c423SVlad Yasevich NETIF_F_TSO6|NETIF_F_UFO) 202d9d52b51SMichael S. Tsirkin 203eaea34b2SPaolo Abeni int align; 204d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 20554f968d6SJason Wang int sndbuf; 20654f968d6SJason Wang struct tap_filter txflt; 20754f968d6SJason Wang struct sock_fprog fprog; 20854f968d6SJason Wang /* protected by rtnl lock */ 20954f968d6SJason Wang bool filter_attached; 21014daa021SRusty Russell #ifdef TUN_DEBUG 21114daa021SRusty Russell int debug; 21214daa021SRusty Russell #endif 21396442e42SJason Wang spinlock_t lock; 21496442e42SJason Wang struct hlist_head flows[TUN_NUM_FLOW_ENTRIES]; 21596442e42SJason Wang struct timer_list flow_gc_timer; 21696442e42SJason Wang unsigned long ageing_time; 2174008e97fSJason Wang unsigned int numdisabled; 2184008e97fSJason Wang struct list_head disabled; 2195dbbaf2dSPaul Moore void *security; 220b8732fb7SJason Wang u32 flow_count; 221608b9977SPaolo Abeni struct tun_pcpu_stats __percpu *pcpu_stats; 22214daa021SRusty Russell }; 22314daa021SRusty Russell 2248b8e658bSGreg Kurz #ifdef CONFIG_TUN_VNET_CROSS_LE 2258b8e658bSGreg Kurz static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) 2268b8e658bSGreg Kurz { 2278b8e658bSGreg Kurz return tun->flags & TUN_VNET_BE ? false : 2288b8e658bSGreg Kurz virtio_legacy_is_little_endian(); 2298b8e658bSGreg Kurz } 2308b8e658bSGreg Kurz 2318b8e658bSGreg Kurz static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) 2328b8e658bSGreg Kurz { 2338b8e658bSGreg Kurz int be = !!(tun->flags & TUN_VNET_BE); 2348b8e658bSGreg Kurz 2358b8e658bSGreg Kurz if (put_user(be, argp)) 2368b8e658bSGreg Kurz return -EFAULT; 2378b8e658bSGreg Kurz 2388b8e658bSGreg Kurz return 0; 2398b8e658bSGreg Kurz } 2408b8e658bSGreg Kurz 2418b8e658bSGreg Kurz static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) 2428b8e658bSGreg Kurz { 2438b8e658bSGreg Kurz int be; 2448b8e658bSGreg Kurz 2458b8e658bSGreg Kurz if (get_user(be, argp)) 2468b8e658bSGreg Kurz return -EFAULT; 2478b8e658bSGreg Kurz 2488b8e658bSGreg Kurz if (be) 2498b8e658bSGreg Kurz tun->flags |= TUN_VNET_BE; 2508b8e658bSGreg Kurz else 2518b8e658bSGreg Kurz tun->flags &= ~TUN_VNET_BE; 2528b8e658bSGreg Kurz 2538b8e658bSGreg Kurz return 0; 2548b8e658bSGreg Kurz } 2558b8e658bSGreg Kurz #else 2568b8e658bSGreg Kurz static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) 2578b8e658bSGreg Kurz { 2588b8e658bSGreg Kurz return virtio_legacy_is_little_endian(); 2598b8e658bSGreg Kurz } 2608b8e658bSGreg Kurz 2618b8e658bSGreg Kurz static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) 2628b8e658bSGreg Kurz { 2638b8e658bSGreg Kurz return -EINVAL; 2648b8e658bSGreg Kurz } 2658b8e658bSGreg Kurz 2668b8e658bSGreg Kurz static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) 2678b8e658bSGreg Kurz { 2688b8e658bSGreg Kurz return -EINVAL; 2698b8e658bSGreg Kurz } 2708b8e658bSGreg Kurz #endif /* CONFIG_TUN_VNET_CROSS_LE */ 2718b8e658bSGreg Kurz 27225bd55bbSGreg Kurz static inline bool tun_is_little_endian(struct tun_struct *tun) 27325bd55bbSGreg Kurz { 2747d824109SGreg Kurz return tun->flags & TUN_VNET_LE || 2758b8e658bSGreg Kurz tun_legacy_is_little_endian(tun); 27625bd55bbSGreg Kurz } 27725bd55bbSGreg Kurz 27856f0dcc5SMichael S. Tsirkin static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val) 27956f0dcc5SMichael S. Tsirkin { 28025bd55bbSGreg Kurz return __virtio16_to_cpu(tun_is_little_endian(tun), val); 28156f0dcc5SMichael S. Tsirkin } 28256f0dcc5SMichael S. Tsirkin 28356f0dcc5SMichael S. Tsirkin static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val) 28456f0dcc5SMichael S. Tsirkin { 28525bd55bbSGreg Kurz return __cpu_to_virtio16(tun_is_little_endian(tun), val); 28656f0dcc5SMichael S. Tsirkin } 28756f0dcc5SMichael S. Tsirkin 28896442e42SJason Wang static inline u32 tun_hashfn(u32 rxhash) 28996442e42SJason Wang { 29096442e42SJason Wang return rxhash & 0x3ff; 29196442e42SJason Wang } 29296442e42SJason Wang 29396442e42SJason Wang static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash) 29496442e42SJason Wang { 29596442e42SJason Wang struct tun_flow_entry *e; 29696442e42SJason Wang 297b67bfe0dSSasha Levin hlist_for_each_entry_rcu(e, head, hash_link) { 29896442e42SJason Wang if (e->rxhash == rxhash) 29996442e42SJason Wang return e; 30096442e42SJason Wang } 30196442e42SJason Wang return NULL; 30296442e42SJason Wang } 30396442e42SJason Wang 30496442e42SJason Wang static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun, 30596442e42SJason Wang struct hlist_head *head, 30696442e42SJason Wang u32 rxhash, u16 queue_index) 30796442e42SJason Wang { 3089fdc6befSEric Dumazet struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC); 3099fdc6befSEric Dumazet 31096442e42SJason Wang if (e) { 31196442e42SJason Wang tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n", 31296442e42SJason Wang rxhash, queue_index); 31396442e42SJason Wang e->updated = jiffies; 31496442e42SJason Wang e->rxhash = rxhash; 3159bc88939STom Herbert e->rps_rxhash = 0; 31696442e42SJason Wang e->queue_index = queue_index; 31796442e42SJason Wang e->tun = tun; 31896442e42SJason Wang hlist_add_head_rcu(&e->hash_link, head); 319b8732fb7SJason Wang ++tun->flow_count; 32096442e42SJason Wang } 32196442e42SJason Wang return e; 32296442e42SJason Wang } 32396442e42SJason Wang 32496442e42SJason Wang static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e) 32596442e42SJason Wang { 32696442e42SJason Wang tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n", 32796442e42SJason Wang e->rxhash, e->queue_index); 32896442e42SJason Wang hlist_del_rcu(&e->hash_link); 3299fdc6befSEric Dumazet kfree_rcu(e, rcu); 330b8732fb7SJason Wang --tun->flow_count; 33196442e42SJason Wang } 33296442e42SJason Wang 33396442e42SJason Wang static void tun_flow_flush(struct tun_struct *tun) 33496442e42SJason Wang { 33596442e42SJason Wang int i; 33696442e42SJason Wang 33796442e42SJason Wang spin_lock_bh(&tun->lock); 33896442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 33996442e42SJason Wang struct tun_flow_entry *e; 340b67bfe0dSSasha Levin struct hlist_node *n; 34196442e42SJason Wang 342b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) 34396442e42SJason Wang tun_flow_delete(tun, e); 34496442e42SJason Wang } 34596442e42SJason Wang spin_unlock_bh(&tun->lock); 34696442e42SJason Wang } 34796442e42SJason Wang 34896442e42SJason Wang static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index) 34996442e42SJason Wang { 35096442e42SJason Wang int i; 35196442e42SJason Wang 35296442e42SJason Wang spin_lock_bh(&tun->lock); 35396442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 35496442e42SJason Wang struct tun_flow_entry *e; 355b67bfe0dSSasha Levin struct hlist_node *n; 35696442e42SJason Wang 357b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { 35896442e42SJason Wang if (e->queue_index == queue_index) 35996442e42SJason Wang tun_flow_delete(tun, e); 36096442e42SJason Wang } 36196442e42SJason Wang } 36296442e42SJason Wang spin_unlock_bh(&tun->lock); 36396442e42SJason Wang } 36496442e42SJason Wang 36596442e42SJason Wang static void tun_flow_cleanup(unsigned long data) 36696442e42SJason Wang { 36796442e42SJason Wang struct tun_struct *tun = (struct tun_struct *)data; 36896442e42SJason Wang unsigned long delay = tun->ageing_time; 36996442e42SJason Wang unsigned long next_timer = jiffies + delay; 37096442e42SJason Wang unsigned long count = 0; 37196442e42SJason Wang int i; 37296442e42SJason Wang 37396442e42SJason Wang tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n"); 37496442e42SJason Wang 37596442e42SJason Wang spin_lock_bh(&tun->lock); 37696442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 37796442e42SJason Wang struct tun_flow_entry *e; 378b67bfe0dSSasha Levin struct hlist_node *n; 37996442e42SJason Wang 380b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { 38196442e42SJason Wang unsigned long this_timer; 38296442e42SJason Wang count++; 38396442e42SJason Wang this_timer = e->updated + delay; 38496442e42SJason Wang if (time_before_eq(this_timer, jiffies)) 38596442e42SJason Wang tun_flow_delete(tun, e); 38696442e42SJason Wang else if (time_before(this_timer, next_timer)) 38796442e42SJason Wang next_timer = this_timer; 38896442e42SJason Wang } 38996442e42SJason Wang } 39096442e42SJason Wang 39196442e42SJason Wang if (count) 39296442e42SJason Wang mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer)); 39396442e42SJason Wang spin_unlock_bh(&tun->lock); 39496442e42SJason Wang } 39596442e42SJason Wang 39649974420SEric Dumazet static void tun_flow_update(struct tun_struct *tun, u32 rxhash, 3979e85722dSJason Wang struct tun_file *tfile) 39896442e42SJason Wang { 39996442e42SJason Wang struct hlist_head *head; 40096442e42SJason Wang struct tun_flow_entry *e; 40196442e42SJason Wang unsigned long delay = tun->ageing_time; 4029e85722dSJason Wang u16 queue_index = tfile->queue_index; 40396442e42SJason Wang 40496442e42SJason Wang if (!rxhash) 40596442e42SJason Wang return; 40696442e42SJason Wang else 40796442e42SJason Wang head = &tun->flows[tun_hashfn(rxhash)]; 40896442e42SJason Wang 40996442e42SJason Wang rcu_read_lock(); 41096442e42SJason Wang 4119e85722dSJason Wang /* We may get a very small possibility of OOO during switching, not 4129e85722dSJason Wang * worth to optimize.*/ 4139e85722dSJason Wang if (tun->numqueues == 1 || tfile->detached) 41496442e42SJason Wang goto unlock; 41596442e42SJason Wang 41696442e42SJason Wang e = tun_flow_find(head, rxhash); 41796442e42SJason Wang if (likely(e)) { 41896442e42SJason Wang /* TODO: keep queueing to old queue until it's empty? */ 41996442e42SJason Wang e->queue_index = queue_index; 42096442e42SJason Wang e->updated = jiffies; 4219bc88939STom Herbert sock_rps_record_flow_hash(e->rps_rxhash); 42296442e42SJason Wang } else { 42396442e42SJason Wang spin_lock_bh(&tun->lock); 424b8732fb7SJason Wang if (!tun_flow_find(head, rxhash) && 425b8732fb7SJason Wang tun->flow_count < MAX_TAP_FLOWS) 42696442e42SJason Wang tun_flow_create(tun, head, rxhash, queue_index); 42796442e42SJason Wang 42896442e42SJason Wang if (!timer_pending(&tun->flow_gc_timer)) 42996442e42SJason Wang mod_timer(&tun->flow_gc_timer, 43096442e42SJason Wang round_jiffies_up(jiffies + delay)); 43196442e42SJason Wang spin_unlock_bh(&tun->lock); 43296442e42SJason Wang } 43396442e42SJason Wang 43496442e42SJason Wang unlock: 43596442e42SJason Wang rcu_read_unlock(); 43696442e42SJason Wang } 43796442e42SJason Wang 4389bc88939STom Herbert /** 4399bc88939STom Herbert * Save the hash received in the stack receive path and update the 4409bc88939STom Herbert * flow_hash table accordingly. 4419bc88939STom Herbert */ 4429bc88939STom Herbert static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash) 4439bc88939STom Herbert { 444567e4b79SEric Dumazet if (unlikely(e->rps_rxhash != hash)) 4459bc88939STom Herbert e->rps_rxhash = hash; 4469bc88939STom Herbert } 4479bc88939STom Herbert 448c8d68e6bSJason Wang /* We try to identify a flow through its rxhash first. The reason that 44992d4ea6eSstephen hemminger * we do not check rxq no. is because some cards(e.g 82599), chooses 450c8d68e6bSJason Wang * the rxq based on the txq where the last packet of the flow comes. As 451c8d68e6bSJason Wang * the userspace application move between processors, we may get a 452c8d68e6bSJason Wang * different rxq no. here. If we could not get rxhash, then we would 453c8d68e6bSJason Wang * hope the rxq no. may help here. 454c8d68e6bSJason Wang */ 455f663dd9aSJason Wang static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb, 45699932d4fSDaniel Borkmann void *accel_priv, select_queue_fallback_t fallback) 457c8d68e6bSJason Wang { 458c8d68e6bSJason Wang struct tun_struct *tun = netdev_priv(dev); 45996442e42SJason Wang struct tun_flow_entry *e; 460c8d68e6bSJason Wang u32 txq = 0; 461c8d68e6bSJason Wang u32 numqueues = 0; 462c8d68e6bSJason Wang 463c8d68e6bSJason Wang rcu_read_lock(); 46492bb73eaSJason Wang numqueues = ACCESS_ONCE(tun->numqueues); 465c8d68e6bSJason Wang 4663958afa1STom Herbert txq = skb_get_hash(skb); 467c8d68e6bSJason Wang if (txq) { 46896442e42SJason Wang e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq); 4699bc88939STom Herbert if (e) { 4709bc88939STom Herbert tun_flow_save_rps_rxhash(e, txq); 471fbe4d456SZhi Yong Wu txq = e->queue_index; 4729bc88939STom Herbert } else 473c8d68e6bSJason Wang /* use multiply and shift instead of expensive divide */ 474c8d68e6bSJason Wang txq = ((u64)txq * numqueues) >> 32; 475c8d68e6bSJason Wang } else if (likely(skb_rx_queue_recorded(skb))) { 476c8d68e6bSJason Wang txq = skb_get_rx_queue(skb); 477c8d68e6bSJason Wang while (unlikely(txq >= numqueues)) 478c8d68e6bSJason Wang txq -= numqueues; 479c8d68e6bSJason Wang } 480c8d68e6bSJason Wang 481c8d68e6bSJason Wang rcu_read_unlock(); 482c8d68e6bSJason Wang return txq; 483c8d68e6bSJason Wang } 484c8d68e6bSJason Wang 485cde8b15fSJason Wang static inline bool tun_not_capable(struct tun_struct *tun) 486cde8b15fSJason Wang { 487cde8b15fSJason Wang const struct cred *cred = current_cred(); 488c260b772SEric W. Biederman struct net *net = dev_net(tun->dev); 489cde8b15fSJason Wang 490cde8b15fSJason Wang return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) || 491cde8b15fSJason Wang (gid_valid(tun->group) && !in_egroup_p(tun->group))) && 492c260b772SEric W. Biederman !ns_capable(net->user_ns, CAP_NET_ADMIN); 493cde8b15fSJason Wang } 494cde8b15fSJason Wang 495c8d68e6bSJason Wang static void tun_set_real_num_queues(struct tun_struct *tun) 496c8d68e6bSJason Wang { 497c8d68e6bSJason Wang netif_set_real_num_tx_queues(tun->dev, tun->numqueues); 498c8d68e6bSJason Wang netif_set_real_num_rx_queues(tun->dev, tun->numqueues); 499c8d68e6bSJason Wang } 500c8d68e6bSJason Wang 5014008e97fSJason Wang static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) 5024008e97fSJason Wang { 5034008e97fSJason Wang tfile->detached = tun; 5044008e97fSJason Wang list_add_tail(&tfile->next, &tun->disabled); 5054008e97fSJason Wang ++tun->numdisabled; 5064008e97fSJason Wang } 5074008e97fSJason Wang 508d32649d1SJason Wang static struct tun_struct *tun_enable_queue(struct tun_file *tfile) 5094008e97fSJason Wang { 5104008e97fSJason Wang struct tun_struct *tun = tfile->detached; 5114008e97fSJason Wang 5124008e97fSJason Wang tfile->detached = NULL; 5134008e97fSJason Wang list_del_init(&tfile->next); 5144008e97fSJason Wang --tun->numdisabled; 5154008e97fSJason Wang return tun; 5164008e97fSJason Wang } 5174008e97fSJason Wang 5184bfb0513SJason Wang static void tun_queue_purge(struct tun_file *tfile) 5194bfb0513SJason Wang { 5201576d986SJason Wang struct sk_buff *skb; 5211576d986SJason Wang 5221576d986SJason Wang while ((skb = skb_array_consume(&tfile->tx_array)) != NULL) 5231576d986SJason Wang kfree_skb(skb); 5241576d986SJason Wang 5254bfb0513SJason Wang skb_queue_purge(&tfile->sk.sk_error_queue); 5264bfb0513SJason Wang } 5274bfb0513SJason Wang 528c8d68e6bSJason Wang static void __tun_detach(struct tun_file *tfile, bool clean) 529c8d68e6bSJason Wang { 530c8d68e6bSJason Wang struct tun_file *ntfile; 531c8d68e6bSJason Wang struct tun_struct *tun; 532c8d68e6bSJason Wang 533b8deabd3SJason Wang tun = rtnl_dereference(tfile->tun); 534b8deabd3SJason Wang 5359e85722dSJason Wang if (tun && !tfile->detached) { 536c8d68e6bSJason Wang u16 index = tfile->queue_index; 537c8d68e6bSJason Wang BUG_ON(index >= tun->numqueues); 538c8d68e6bSJason Wang 539c8d68e6bSJason Wang rcu_assign_pointer(tun->tfiles[index], 540c8d68e6bSJason Wang tun->tfiles[tun->numqueues - 1]); 541b8deabd3SJason Wang ntfile = rtnl_dereference(tun->tfiles[index]); 542c8d68e6bSJason Wang ntfile->queue_index = index; 543c8d68e6bSJason Wang 544c8d68e6bSJason Wang --tun->numqueues; 5459e85722dSJason Wang if (clean) { 546c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 547c8d68e6bSJason Wang sock_put(&tfile->sk); 5489e85722dSJason Wang } else 5494008e97fSJason Wang tun_disable_queue(tun, tfile); 550c8d68e6bSJason Wang 551c8d68e6bSJason Wang synchronize_net(); 55296442e42SJason Wang tun_flow_delete_by_queue(tun, tun->numqueues + 1); 553c8d68e6bSJason Wang /* Drop read queue */ 5544bfb0513SJason Wang tun_queue_purge(tfile); 555c8d68e6bSJason Wang tun_set_real_num_queues(tun); 556dd38bd85SJason Wang } else if (tfile->detached && clean) { 5574008e97fSJason Wang tun = tun_enable_queue(tfile); 558dd38bd85SJason Wang sock_put(&tfile->sk); 559dd38bd85SJason Wang } 560c8d68e6bSJason Wang 561c8d68e6bSJason Wang if (clean) { 562af668b3cSMichael S. Tsirkin if (tun && tun->numqueues == 0 && tun->numdisabled == 0) { 563af668b3cSMichael S. Tsirkin netif_carrier_off(tun->dev); 564af668b3cSMichael S. Tsirkin 56540630b82SMichael S. Tsirkin if (!(tun->flags & IFF_PERSIST) && 566af668b3cSMichael S. Tsirkin tun->dev->reg_state == NETREG_REGISTERED) 5674008e97fSJason Wang unregister_netdevice(tun->dev); 568af668b3cSMichael S. Tsirkin } 5691576d986SJason Wang if (tun) 5701576d986SJason Wang skb_array_cleanup(&tfile->tx_array); 571140e807dSEric W. Biederman sock_put(&tfile->sk); 572c8d68e6bSJason Wang } 573c8d68e6bSJason Wang } 574c8d68e6bSJason Wang 575c8d68e6bSJason Wang static void tun_detach(struct tun_file *tfile, bool clean) 576c8d68e6bSJason Wang { 577c8d68e6bSJason Wang rtnl_lock(); 578c8d68e6bSJason Wang __tun_detach(tfile, clean); 579c8d68e6bSJason Wang rtnl_unlock(); 580c8d68e6bSJason Wang } 581c8d68e6bSJason Wang 582c8d68e6bSJason Wang static void tun_detach_all(struct net_device *dev) 583c8d68e6bSJason Wang { 584c8d68e6bSJason Wang struct tun_struct *tun = netdev_priv(dev); 5854008e97fSJason Wang struct tun_file *tfile, *tmp; 586c8d68e6bSJason Wang int i, n = tun->numqueues; 587c8d68e6bSJason Wang 588c8d68e6bSJason Wang for (i = 0; i < n; i++) { 589b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 590c8d68e6bSJason Wang BUG_ON(!tfile); 591addf8fc4SJason Wang tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; 5929e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 593c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 594c8d68e6bSJason Wang --tun->numqueues; 595c8d68e6bSJason Wang } 5969e85722dSJason Wang list_for_each_entry(tfile, &tun->disabled, next) { 597addf8fc4SJason Wang tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; 5989e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 599c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 6009e85722dSJason Wang } 601c8d68e6bSJason Wang BUG_ON(tun->numqueues != 0); 602c8d68e6bSJason Wang 603c8d68e6bSJason Wang synchronize_net(); 604c8d68e6bSJason Wang for (i = 0; i < n; i++) { 605b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 606c8d68e6bSJason Wang /* Drop read queue */ 6074bfb0513SJason Wang tun_queue_purge(tfile); 608c8d68e6bSJason Wang sock_put(&tfile->sk); 609c8d68e6bSJason Wang } 6104008e97fSJason Wang list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { 6114008e97fSJason Wang tun_enable_queue(tfile); 6124bfb0513SJason Wang tun_queue_purge(tfile); 6134008e97fSJason Wang sock_put(&tfile->sk); 6144008e97fSJason Wang } 6154008e97fSJason Wang BUG_ON(tun->numdisabled != 0); 616dd38bd85SJason Wang 61740630b82SMichael S. Tsirkin if (tun->flags & IFF_PERSIST) 618dd38bd85SJason Wang module_put(THIS_MODULE); 619c8d68e6bSJason Wang } 620c8d68e6bSJason Wang 621849c9b6fSPavel Emelyanov static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter) 622a7385ba2SEric W. Biederman { 623631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 6241576d986SJason Wang struct net_device *dev = tun->dev; 62538231b7aSEric W. Biederman int err; 626a7385ba2SEric W. Biederman 6275dbbaf2dSPaul Moore err = security_tun_dev_attach(tfile->socket.sk, tun->security); 6285dbbaf2dSPaul Moore if (err < 0) 6295dbbaf2dSPaul Moore goto out; 6305dbbaf2dSPaul Moore 63138231b7aSEric W. Biederman err = -EINVAL; 6329e85722dSJason Wang if (rtnl_dereference(tfile->tun) && !tfile->detached) 63338231b7aSEric W. Biederman goto out; 63438231b7aSEric W. Biederman 63538231b7aSEric W. Biederman err = -EBUSY; 63640630b82SMichael S. Tsirkin if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1) 637c8d68e6bSJason Wang goto out; 638c8d68e6bSJason Wang 639c8d68e6bSJason Wang err = -E2BIG; 6404008e97fSJason Wang if (!tfile->detached && 6414008e97fSJason Wang tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES) 64238231b7aSEric W. Biederman goto out; 64338231b7aSEric W. Biederman 64438231b7aSEric W. Biederman err = 0; 64554f968d6SJason Wang 64692d4ea6eSstephen hemminger /* Re-attach the filter to persist device */ 647849c9b6fSPavel Emelyanov if (!skip_filter && (tun->filter_attached == true)) { 6488ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 6498ced425eSHannes Frederic Sowa err = sk_attach_filter(&tun->fprog, tfile->socket.sk); 6508ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 65154f968d6SJason Wang if (!err) 65254f968d6SJason Wang goto out; 65354f968d6SJason Wang } 6541576d986SJason Wang 6551576d986SJason Wang if (!tfile->detached && 6561576d986SJason Wang skb_array_init(&tfile->tx_array, dev->tx_queue_len, GFP_KERNEL)) { 6571576d986SJason Wang err = -ENOMEM; 6581576d986SJason Wang goto out; 6591576d986SJason Wang } 6601576d986SJason Wang 661c8d68e6bSJason Wang tfile->queue_index = tun->numqueues; 662addf8fc4SJason Wang tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN; 6636e914fc7SJason Wang rcu_assign_pointer(tfile->tun, tun); 664c8d68e6bSJason Wang rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); 665c8d68e6bSJason Wang tun->numqueues++; 666c8d68e6bSJason Wang 6674008e97fSJason Wang if (tfile->detached) 6684008e97fSJason Wang tun_enable_queue(tfile); 6694008e97fSJason Wang else 6704008e97fSJason Wang sock_hold(&tfile->sk); 6714008e97fSJason Wang 672c8d68e6bSJason Wang tun_set_real_num_queues(tun); 673c8d68e6bSJason Wang 674c8d68e6bSJason Wang /* device is allowed to go away first, so no need to hold extra 675c8d68e6bSJason Wang * refcnt. 676c8d68e6bSJason Wang */ 677a7385ba2SEric W. Biederman 67838231b7aSEric W. Biederman out: 67938231b7aSEric W. Biederman return err; 680a7385ba2SEric W. Biederman } 681a7385ba2SEric W. Biederman 682631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile) 683631ab46bSEric W. Biederman { 6846e914fc7SJason Wang struct tun_struct *tun; 685c70f1829SEric W. Biederman 6866e914fc7SJason Wang rcu_read_lock(); 6876e914fc7SJason Wang tun = rcu_dereference(tfile->tun); 6886e914fc7SJason Wang if (tun) 6896e914fc7SJason Wang dev_hold(tun->dev); 6906e914fc7SJason Wang rcu_read_unlock(); 691c70f1829SEric W. Biederman 692c70f1829SEric W. Biederman return tun; 693631ab46bSEric W. Biederman } 694631ab46bSEric W. Biederman 695631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file) 696631ab46bSEric W. Biederman { 697631ab46bSEric W. Biederman return __tun_get(file->private_data); 698631ab46bSEric W. Biederman } 699631ab46bSEric W. Biederman 700631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun) 701631ab46bSEric W. Biederman { 7026e914fc7SJason Wang dev_put(tun->dev); 703631ab46bSEric W. Biederman } 704631ab46bSEric W. Biederman 7056b8a66eeSJoe Perches /* TAP filtering */ 706f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr) 707f271b2ccSMax Krasnyansky { 708f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 709f271b2ccSMax Krasnyansky mask[n >> 5] |= (1 << (n & 31)); 710f271b2ccSMax Krasnyansky } 711f271b2ccSMax Krasnyansky 712f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) 713f271b2ccSMax Krasnyansky { 714f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 715f271b2ccSMax Krasnyansky return mask[n >> 5] & (1 << (n & 31)); 716f271b2ccSMax Krasnyansky } 717f271b2ccSMax Krasnyansky 718f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg) 719f271b2ccSMax Krasnyansky { 720f271b2ccSMax Krasnyansky struct { u8 u[ETH_ALEN]; } *addr; 721f271b2ccSMax Krasnyansky struct tun_filter uf; 722f271b2ccSMax Krasnyansky int err, alen, n, nexact; 723f271b2ccSMax Krasnyansky 724f271b2ccSMax Krasnyansky if (copy_from_user(&uf, arg, sizeof(uf))) 725f271b2ccSMax Krasnyansky return -EFAULT; 726f271b2ccSMax Krasnyansky 727f271b2ccSMax Krasnyansky if (!uf.count) { 728f271b2ccSMax Krasnyansky /* Disabled */ 729f271b2ccSMax Krasnyansky filter->count = 0; 730f271b2ccSMax Krasnyansky return 0; 731f271b2ccSMax Krasnyansky } 732f271b2ccSMax Krasnyansky 733f271b2ccSMax Krasnyansky alen = ETH_ALEN * uf.count; 734f271b2ccSMax Krasnyansky addr = kmalloc(alen, GFP_KERNEL); 735f271b2ccSMax Krasnyansky if (!addr) 736f271b2ccSMax Krasnyansky return -ENOMEM; 737f271b2ccSMax Krasnyansky 738f271b2ccSMax Krasnyansky if (copy_from_user(addr, arg + sizeof(uf), alen)) { 739f271b2ccSMax Krasnyansky err = -EFAULT; 740f271b2ccSMax Krasnyansky goto done; 741f271b2ccSMax Krasnyansky } 742f271b2ccSMax Krasnyansky 743f271b2ccSMax Krasnyansky /* The filter is updated without holding any locks. Which is 744f271b2ccSMax Krasnyansky * perfectly safe. We disable it first and in the worst 745f271b2ccSMax Krasnyansky * case we'll accept a few undesired packets. */ 746f271b2ccSMax Krasnyansky filter->count = 0; 747f271b2ccSMax Krasnyansky wmb(); 748f271b2ccSMax Krasnyansky 749f271b2ccSMax Krasnyansky /* Use first set of addresses as an exact filter */ 750f271b2ccSMax Krasnyansky for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++) 751f271b2ccSMax Krasnyansky memcpy(filter->addr[n], addr[n].u, ETH_ALEN); 752f271b2ccSMax Krasnyansky 753f271b2ccSMax Krasnyansky nexact = n; 754f271b2ccSMax Krasnyansky 755cfbf84fcSAlex Williamson /* Remaining multicast addresses are hashed, 756cfbf84fcSAlex Williamson * unicast will leave the filter disabled. */ 757f271b2ccSMax Krasnyansky memset(filter->mask, 0, sizeof(filter->mask)); 758cfbf84fcSAlex Williamson for (; n < uf.count; n++) { 759cfbf84fcSAlex Williamson if (!is_multicast_ether_addr(addr[n].u)) { 760cfbf84fcSAlex Williamson err = 0; /* no filter */ 761cfbf84fcSAlex Williamson goto done; 762cfbf84fcSAlex Williamson } 763f271b2ccSMax Krasnyansky addr_hash_set(filter->mask, addr[n].u); 764cfbf84fcSAlex Williamson } 765f271b2ccSMax Krasnyansky 766f271b2ccSMax Krasnyansky /* For ALLMULTI just set the mask to all ones. 767f271b2ccSMax Krasnyansky * This overrides the mask populated above. */ 768f271b2ccSMax Krasnyansky if ((uf.flags & TUN_FLT_ALLMULTI)) 769f271b2ccSMax Krasnyansky memset(filter->mask, ~0, sizeof(filter->mask)); 770f271b2ccSMax Krasnyansky 771f271b2ccSMax Krasnyansky /* Now enable the filter */ 772f271b2ccSMax Krasnyansky wmb(); 773f271b2ccSMax Krasnyansky filter->count = nexact; 774f271b2ccSMax Krasnyansky 775f271b2ccSMax Krasnyansky /* Return the number of exact filters */ 776f271b2ccSMax Krasnyansky err = nexact; 777f271b2ccSMax Krasnyansky 778f271b2ccSMax Krasnyansky done: 779f271b2ccSMax Krasnyansky kfree(addr); 780f271b2ccSMax Krasnyansky return err; 781f271b2ccSMax Krasnyansky } 782f271b2ccSMax Krasnyansky 783f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */ 784f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb) 785f271b2ccSMax Krasnyansky { 786f271b2ccSMax Krasnyansky /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect 787f271b2ccSMax Krasnyansky * at this point. */ 788f271b2ccSMax Krasnyansky struct ethhdr *eh = (struct ethhdr *) skb->data; 789f271b2ccSMax Krasnyansky int i; 790f271b2ccSMax Krasnyansky 791f271b2ccSMax Krasnyansky /* Exact match */ 792f271b2ccSMax Krasnyansky for (i = 0; i < filter->count; i++) 7932e42e474SJoe Perches if (ether_addr_equal(eh->h_dest, filter->addr[i])) 794f271b2ccSMax Krasnyansky return 1; 795f271b2ccSMax Krasnyansky 796f271b2ccSMax Krasnyansky /* Inexact match (multicast only) */ 797f271b2ccSMax Krasnyansky if (is_multicast_ether_addr(eh->h_dest)) 798f271b2ccSMax Krasnyansky return addr_hash_test(filter->mask, eh->h_dest); 799f271b2ccSMax Krasnyansky 800f271b2ccSMax Krasnyansky return 0; 801f271b2ccSMax Krasnyansky } 802f271b2ccSMax Krasnyansky 803f271b2ccSMax Krasnyansky /* 804f271b2ccSMax Krasnyansky * Checks whether the packet is accepted or not. 805f271b2ccSMax Krasnyansky * Returns: 0 - drop, !=0 - accept 806f271b2ccSMax Krasnyansky */ 807f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb) 808f271b2ccSMax Krasnyansky { 809f271b2ccSMax Krasnyansky if (!filter->count) 810f271b2ccSMax Krasnyansky return 1; 811f271b2ccSMax Krasnyansky 812f271b2ccSMax Krasnyansky return run_filter(filter, skb); 813f271b2ccSMax Krasnyansky } 814f271b2ccSMax Krasnyansky 8151da177e4SLinus Torvalds /* Network device part of the driver */ 8161da177e4SLinus Torvalds 8177282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops; 8181da177e4SLinus Torvalds 819c70f1829SEric W. Biederman /* Net device detach from fd. */ 820c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev) 821c70f1829SEric W. Biederman { 822c8d68e6bSJason Wang tun_detach_all(dev); 823c70f1829SEric W. Biederman } 824c70f1829SEric W. Biederman 8251da177e4SLinus Torvalds /* Net device open. */ 8261da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev) 8271da177e4SLinus Torvalds { 828c8d68e6bSJason Wang netif_tx_start_all_queues(dev); 8291da177e4SLinus Torvalds return 0; 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds /* Net device close. */ 8331da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev) 8341da177e4SLinus Torvalds { 835c8d68e6bSJason Wang netif_tx_stop_all_queues(dev); 8361da177e4SLinus Torvalds return 0; 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds /* Net device start xmit */ 840424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) 8411da177e4SLinus Torvalds { 8421da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 843c8d68e6bSJason Wang int txq = skb->queue_mapping; 8446e914fc7SJason Wang struct tun_file *tfile; 845fa35864eSDominic Curran u32 numqueues = 0; 8461da177e4SLinus Torvalds 8476e914fc7SJason Wang rcu_read_lock(); 848c8d68e6bSJason Wang tfile = rcu_dereference(tun->tfiles[txq]); 849fa35864eSDominic Curran numqueues = ACCESS_ONCE(tun->numqueues); 850c8d68e6bSJason Wang 8511da177e4SLinus Torvalds /* Drop packet if interface is not attached */ 852fa35864eSDominic Curran if (txq >= numqueues) 8531da177e4SLinus Torvalds goto drop; 8541da177e4SLinus Torvalds 8553df97ba8SJason Wang #ifdef CONFIG_RPS 8563df97ba8SJason Wang if (numqueues == 1 && static_key_false(&rps_needed)) { 8579bc88939STom Herbert /* Select queue was not called for the skbuff, so we extract the 8589bc88939STom Herbert * RPS hash and save it into the flow_table here. 8599bc88939STom Herbert */ 8609bc88939STom Herbert __u32 rxhash; 8619bc88939STom Herbert 8629bc88939STom Herbert rxhash = skb_get_hash(skb); 8639bc88939STom Herbert if (rxhash) { 8649bc88939STom Herbert struct tun_flow_entry *e; 8659bc88939STom Herbert e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)], 8669bc88939STom Herbert rxhash); 8679bc88939STom Herbert if (e) 8689bc88939STom Herbert tun_flow_save_rps_rxhash(e, rxhash); 8699bc88939STom Herbert } 8709bc88939STom Herbert } 8713df97ba8SJason Wang #endif 8729bc88939STom Herbert 8736e914fc7SJason Wang tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len); 8746e914fc7SJason Wang 875c8d68e6bSJason Wang BUG_ON(!tfile); 876c8d68e6bSJason Wang 877f271b2ccSMax Krasnyansky /* Drop if the filter does not like it. 878f271b2ccSMax Krasnyansky * This is a noop if the filter is disabled. 879f271b2ccSMax Krasnyansky * Filter can be enabled only for the TAP devices. */ 880f271b2ccSMax Krasnyansky if (!check_filter(&tun->txflt, skb)) 881f271b2ccSMax Krasnyansky goto drop; 882f271b2ccSMax Krasnyansky 88354f968d6SJason Wang if (tfile->socket.sk->sk_filter && 88454f968d6SJason Wang sk_filter(tfile->socket.sk, skb)) 88599405162SMichael S. Tsirkin goto drop; 88699405162SMichael S. Tsirkin 88736fe8c09SRami Rosen /* Limit the number of packets queued by dividing txq length with the 888c8d68e6bSJason Wang * number of queues. 889c8d68e6bSJason Wang */ 890fa35864eSDominic Curran if (skb_queue_len(&tfile->socket.sk->sk_receive_queue) * numqueues 891fa35864eSDominic Curran >= dev->tx_queue_len) 8921da177e4SLinus Torvalds goto drop; 8931da177e4SLinus Torvalds 8947bf66305SJason Wang if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC))) 8957bf66305SJason Wang goto drop; 8967bf66305SJason Wang 8975fcd2d8bSEric Dumazet if (skb->sk && sk_fullsock(skb->sk)) { 898c14ac945SSoheil Hassas Yeganeh sock_tx_timestamp(skb->sk, skb->sk->sk_tsflags, 899c14ac945SSoheil Hassas Yeganeh &skb_shinfo(skb)->tx_flags); 900eda29772SRichard Cochran sw_tx_timestamp(skb); 901eda29772SRichard Cochran } 902eda29772SRichard Cochran 9030110d6f2SMichael S. Tsirkin /* Orphan the skb - required as we might hang on to it 9047bf66305SJason Wang * for indefinite time. 9057bf66305SJason Wang */ 9060110d6f2SMichael S. Tsirkin skb_orphan(skb); 9070110d6f2SMichael S. Tsirkin 908f8af75f3SEric Dumazet nf_reset(skb); 909f8af75f3SEric Dumazet 9101576d986SJason Wang if (skb_array_produce(&tfile->tx_array, skb)) 9111576d986SJason Wang goto drop; 9121da177e4SLinus Torvalds 9131da177e4SLinus Torvalds /* Notify and wake up reader process */ 91454f968d6SJason Wang if (tfile->flags & TUN_FASYNC) 91554f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_IN); 9169e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 9176e914fc7SJason Wang 9186e914fc7SJason Wang rcu_read_unlock(); 9196ed10654SPatrick McHardy return NETDEV_TX_OK; 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds drop: 922608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->tx_dropped); 923149d36f7SMichael S. Tsirkin skb_tx_error(skb); 9241da177e4SLinus Torvalds kfree_skb(skb); 9256e914fc7SJason Wang rcu_read_unlock(); 926baeababbSJason Wang return NET_XMIT_DROP; 9271da177e4SLinus Torvalds } 9281da177e4SLinus Torvalds 929f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev) 9301da177e4SLinus Torvalds { 931f271b2ccSMax Krasnyansky /* 932f271b2ccSMax Krasnyansky * This callback is supposed to deal with mc filter in 933f271b2ccSMax Krasnyansky * _rx_ path and has nothing to do with the _tx_ path. 934f271b2ccSMax Krasnyansky * In rx path we always accept everything userspace gives us. 935f271b2ccSMax Krasnyansky */ 9361da177e4SLinus Torvalds } 9371da177e4SLinus Torvalds 9384885a504SEd Swierk #define MIN_MTU 68 9394885a504SEd Swierk #define MAX_MTU 65535 9404885a504SEd Swierk 9414885a504SEd Swierk static int 9424885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu) 9434885a504SEd Swierk { 9444885a504SEd Swierk if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU) 9454885a504SEd Swierk return -EINVAL; 9464885a504SEd Swierk dev->mtu = new_mtu; 9474885a504SEd Swierk return 0; 9484885a504SEd Swierk } 9494885a504SEd Swierk 950c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev, 951c8f44affSMichał Mirosław netdev_features_t features) 95288255375SMichał Mirosław { 95388255375SMichał Mirosław struct tun_struct *tun = netdev_priv(dev); 95488255375SMichał Mirosław 95588255375SMichał Mirosław return (features & tun->set_features) | (features & ~TUN_USER_FEATURES); 95688255375SMichał Mirosław } 957bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 958bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev) 959bebd097aSNeil Horman { 960bebd097aSNeil Horman /* 961bebd097aSNeil Horman * Tun only receives frames when: 962bebd097aSNeil Horman * 1) the char device endpoint gets data from user space 963bebd097aSNeil Horman * 2) the tun socket gets a sendmsg call from user space 96492d4ea6eSstephen hemminger * Since both of those are synchronous operations, we are guaranteed 965bebd097aSNeil Horman * never to have pending data when we poll for it 96692d4ea6eSstephen hemminger * so there is nothing to do here but return. 967bebd097aSNeil Horman * We need this though so netpoll recognizes us as an interface that 968bebd097aSNeil Horman * supports polling, which enables bridge devices in virt setups to 969bebd097aSNeil Horman * still use netconsole 970bebd097aSNeil Horman */ 971bebd097aSNeil Horman return; 972bebd097aSNeil Horman } 973bebd097aSNeil Horman #endif 974eaea34b2SPaolo Abeni 975eaea34b2SPaolo Abeni static void tun_set_headroom(struct net_device *dev, int new_hr) 976eaea34b2SPaolo Abeni { 977eaea34b2SPaolo Abeni struct tun_struct *tun = netdev_priv(dev); 978eaea34b2SPaolo Abeni 979eaea34b2SPaolo Abeni if (new_hr < NET_SKB_PAD) 980eaea34b2SPaolo Abeni new_hr = NET_SKB_PAD; 981eaea34b2SPaolo Abeni 982eaea34b2SPaolo Abeni tun->align = new_hr; 983eaea34b2SPaolo Abeni } 984eaea34b2SPaolo Abeni 985608b9977SPaolo Abeni static struct rtnl_link_stats64 * 986608b9977SPaolo Abeni tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 987608b9977SPaolo Abeni { 988608b9977SPaolo Abeni u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0; 989608b9977SPaolo Abeni struct tun_struct *tun = netdev_priv(dev); 990608b9977SPaolo Abeni struct tun_pcpu_stats *p; 991608b9977SPaolo Abeni int i; 992608b9977SPaolo Abeni 993608b9977SPaolo Abeni for_each_possible_cpu(i) { 994608b9977SPaolo Abeni u64 rxpackets, rxbytes, txpackets, txbytes; 995608b9977SPaolo Abeni unsigned int start; 996608b9977SPaolo Abeni 997608b9977SPaolo Abeni p = per_cpu_ptr(tun->pcpu_stats, i); 998608b9977SPaolo Abeni do { 999608b9977SPaolo Abeni start = u64_stats_fetch_begin(&p->syncp); 1000608b9977SPaolo Abeni rxpackets = p->rx_packets; 1001608b9977SPaolo Abeni rxbytes = p->rx_bytes; 1002608b9977SPaolo Abeni txpackets = p->tx_packets; 1003608b9977SPaolo Abeni txbytes = p->tx_bytes; 1004608b9977SPaolo Abeni } while (u64_stats_fetch_retry(&p->syncp, start)); 1005608b9977SPaolo Abeni 1006608b9977SPaolo Abeni stats->rx_packets += rxpackets; 1007608b9977SPaolo Abeni stats->rx_bytes += rxbytes; 1008608b9977SPaolo Abeni stats->tx_packets += txpackets; 1009608b9977SPaolo Abeni stats->tx_bytes += txbytes; 1010608b9977SPaolo Abeni 1011608b9977SPaolo Abeni /* u32 counters */ 1012608b9977SPaolo Abeni rx_dropped += p->rx_dropped; 1013608b9977SPaolo Abeni rx_frame_errors += p->rx_frame_errors; 1014608b9977SPaolo Abeni tx_dropped += p->tx_dropped; 1015608b9977SPaolo Abeni } 1016608b9977SPaolo Abeni stats->rx_dropped = rx_dropped; 1017608b9977SPaolo Abeni stats->rx_frame_errors = rx_frame_errors; 1018608b9977SPaolo Abeni stats->tx_dropped = tx_dropped; 1019608b9977SPaolo Abeni return stats; 1020608b9977SPaolo Abeni } 1021608b9977SPaolo Abeni 1022758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = { 1023c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 1024758e43b7SStephen Hemminger .ndo_open = tun_net_open, 1025758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 102600829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 1027758e43b7SStephen Hemminger .ndo_change_mtu = tun_net_change_mtu, 102888255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 1029c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 1030bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 1031bebd097aSNeil Horman .ndo_poll_controller = tun_poll_controller, 1032bebd097aSNeil Horman #endif 1033eaea34b2SPaolo Abeni .ndo_set_rx_headroom = tun_set_headroom, 1034608b9977SPaolo Abeni .ndo_get_stats64 = tun_net_get_stats64, 1035758e43b7SStephen Hemminger }; 1036758e43b7SStephen Hemminger 1037758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = { 1038c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 1039758e43b7SStephen Hemminger .ndo_open = tun_net_open, 1040758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 104100829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 1042758e43b7SStephen Hemminger .ndo_change_mtu = tun_net_change_mtu, 104388255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 1044afc4b13dSJiri Pirko .ndo_set_rx_mode = tun_net_mclist, 1045758e43b7SStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 1046758e43b7SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 1047c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 1048bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 1049bebd097aSNeil Horman .ndo_poll_controller = tun_poll_controller, 1050bebd097aSNeil Horman #endif 10515e52796aSToshiaki Makita .ndo_features_check = passthru_features_check, 1052eaea34b2SPaolo Abeni .ndo_set_rx_headroom = tun_set_headroom, 1053608b9977SPaolo Abeni .ndo_get_stats64 = tun_net_get_stats64, 1054758e43b7SStephen Hemminger }; 1055758e43b7SStephen Hemminger 1056944a1376SPavel Emelyanov static void tun_flow_init(struct tun_struct *tun) 105796442e42SJason Wang { 105896442e42SJason Wang int i; 105996442e42SJason Wang 106096442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) 106196442e42SJason Wang INIT_HLIST_HEAD(&tun->flows[i]); 106296442e42SJason Wang 106396442e42SJason Wang tun->ageing_time = TUN_FLOW_EXPIRE; 106496442e42SJason Wang setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun); 106596442e42SJason Wang mod_timer(&tun->flow_gc_timer, 106696442e42SJason Wang round_jiffies_up(jiffies + tun->ageing_time)); 106796442e42SJason Wang } 106896442e42SJason Wang 106996442e42SJason Wang static void tun_flow_uninit(struct tun_struct *tun) 107096442e42SJason Wang { 107196442e42SJason Wang del_timer_sync(&tun->flow_gc_timer); 107296442e42SJason Wang tun_flow_flush(tun); 107396442e42SJason Wang } 107496442e42SJason Wang 10751da177e4SLinus Torvalds /* Initialize net device. */ 10761da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev) 10771da177e4SLinus Torvalds { 10781da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 108140630b82SMichael S. Tsirkin case IFF_TUN: 1082758e43b7SStephen Hemminger dev->netdev_ops = &tun_netdev_ops; 1083758e43b7SStephen Hemminger 10841da177e4SLinus Torvalds /* Point-to-Point TUN Device */ 10851da177e4SLinus Torvalds dev->hard_header_len = 0; 10861da177e4SLinus Torvalds dev->addr_len = 0; 10871da177e4SLinus Torvalds dev->mtu = 1500; 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds /* Zero header length */ 10901da177e4SLinus Torvalds dev->type = ARPHRD_NONE; 10911da177e4SLinus Torvalds dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 10921da177e4SLinus Torvalds break; 10931da177e4SLinus Torvalds 109440630b82SMichael S. Tsirkin case IFF_TAP: 10957a0a9608SKusanagi Kouichi dev->netdev_ops = &tap_netdev_ops; 10961da177e4SLinus Torvalds /* Ethernet TAP Device */ 10971da177e4SLinus Torvalds ether_setup(dev); 1098550fd08cSNeil Horman dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1099a676847bSstephen hemminger dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 110036226a8dSBrian Braunstein 1101f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 110236226a8dSBrian Braunstein 11031da177e4SLinus Torvalds break; 11041da177e4SLinus Torvalds } 11051da177e4SLinus Torvalds } 11061da177e4SLinus Torvalds 11071da177e4SLinus Torvalds /* Character device part */ 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds /* Poll */ 11101da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table *wait) 11111da177e4SLinus Torvalds { 1112b2430de3SEric W. Biederman struct tun_file *tfile = file->private_data; 1113b2430de3SEric W. Biederman struct tun_struct *tun = __tun_get(tfile); 11143c8a9c63SMariusz Kozlowski struct sock *sk; 111533dccbb0SHerbert Xu unsigned int mask = 0; 11161da177e4SLinus Torvalds 11171da177e4SLinus Torvalds if (!tun) 1118eac9e902SEric W. Biederman return POLLERR; 11191da177e4SLinus Torvalds 112054f968d6SJason Wang sk = tfile->socket.sk; 11213c8a9c63SMariusz Kozlowski 11226b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_chr_poll\n"); 11231da177e4SLinus Torvalds 11249e641bdcSXi Wang poll_wait(file, sk_sleep(sk), wait); 11251da177e4SLinus Torvalds 11261576d986SJason Wang if (!skb_array_empty(&tfile->tx_array)) 11271da177e4SLinus Torvalds mask |= POLLIN | POLLRDNORM; 11281da177e4SLinus Torvalds 112933dccbb0SHerbert Xu if (sock_writeable(sk) || 11309cd3e072SEric Dumazet (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) && 113133dccbb0SHerbert Xu sock_writeable(sk))) 113233dccbb0SHerbert Xu mask |= POLLOUT | POLLWRNORM; 113333dccbb0SHerbert Xu 1134c70f1829SEric W. Biederman if (tun->dev->reg_state != NETREG_REGISTERED) 1135c70f1829SEric W. Biederman mask = POLLERR; 1136c70f1829SEric W. Biederman 1137631ab46bSEric W. Biederman tun_put(tun); 11381da177e4SLinus Torvalds return mask; 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds 1141f42157cbSRusty Russell /* prepad is the amount to reserve at front. len is length after that. 1142f42157cbSRusty Russell * linear is a hint as to how much to copy (usually headers). */ 114354f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, 114433dccbb0SHerbert Xu size_t prepad, size_t len, 114533dccbb0SHerbert Xu size_t linear, int noblock) 1146f42157cbSRusty Russell { 114754f968d6SJason Wang struct sock *sk = tfile->socket.sk; 1148f42157cbSRusty Russell struct sk_buff *skb; 114933dccbb0SHerbert Xu int err; 1150f42157cbSRusty Russell 1151f42157cbSRusty Russell /* Under a page? Don't bother with paged skb. */ 11520eca93bcSHerbert Xu if (prepad + len < PAGE_SIZE || !linear) 115333dccbb0SHerbert Xu linear = len; 1154f42157cbSRusty Russell 115533dccbb0SHerbert Xu skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, 115628d64271SEric Dumazet &err, 0); 1157f42157cbSRusty Russell if (!skb) 115833dccbb0SHerbert Xu return ERR_PTR(err); 1159f42157cbSRusty Russell 1160f42157cbSRusty Russell skb_reserve(skb, prepad); 1161f42157cbSRusty Russell skb_put(skb, linear); 116233dccbb0SHerbert Xu skb->data_len = len - linear; 116333dccbb0SHerbert Xu skb->len += len - linear; 1164f42157cbSRusty Russell 1165f42157cbSRusty Russell return skb; 1166f42157cbSRusty Russell } 1167f42157cbSRusty Russell 11681da177e4SLinus Torvalds /* Get packet from user space buffer */ 116954f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, 1170f5ff53b4SAl Viro void *msg_control, struct iov_iter *from, 1171f5ff53b4SAl Viro int noblock) 11721da177e4SLinus Torvalds { 117309640e63SHarvey Harrison struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; 11741da177e4SLinus Torvalds struct sk_buff *skb; 1175f5ff53b4SAl Viro size_t total_len = iov_iter_count(from); 1176eaea34b2SPaolo Abeni size_t len = total_len, align = tun->align, linear; 1177f43798c2SRusty Russell struct virtio_net_hdr gso = { 0 }; 1178608b9977SPaolo Abeni struct tun_pcpu_stats *stats; 117996f8d9ecSJason Wang int good_linear; 11800690899bSMichael S. Tsirkin int copylen; 11810690899bSMichael S. Tsirkin bool zerocopy = false; 11820690899bSMichael S. Tsirkin int err; 118349974420SEric Dumazet u32 rxhash; 1184f5ff53b4SAl Viro ssize_t n; 11851da177e4SLinus Torvalds 11861bd4978aSEric Dumazet if (!(tun->dev->flags & IFF_UP)) 11871bd4978aSEric Dumazet return -EIO; 11881bd4978aSEric Dumazet 118940630b82SMichael S. Tsirkin if (!(tun->flags & IFF_NO_PI)) { 119015718ea0SDan Carpenter if (len < sizeof(pi)) 11911da177e4SLinus Torvalds return -EINVAL; 119215718ea0SDan Carpenter len -= sizeof(pi); 11931da177e4SLinus Torvalds 1194f5ff53b4SAl Viro n = copy_from_iter(&pi, sizeof(pi), from); 1195f5ff53b4SAl Viro if (n != sizeof(pi)) 11961da177e4SLinus Torvalds return -EFAULT; 11971da177e4SLinus Torvalds } 11981da177e4SLinus Torvalds 119940630b82SMichael S. Tsirkin if (tun->flags & IFF_VNET_HDR) { 120015718ea0SDan Carpenter if (len < tun->vnet_hdr_sz) 1201f43798c2SRusty Russell return -EINVAL; 120215718ea0SDan Carpenter len -= tun->vnet_hdr_sz; 1203f43798c2SRusty Russell 1204f5ff53b4SAl Viro n = copy_from_iter(&gso, sizeof(gso), from); 1205f5ff53b4SAl Viro if (n != sizeof(gso)) 1206f43798c2SRusty Russell return -EFAULT; 1207f43798c2SRusty Russell 12084909122fSHerbert Xu if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && 120956f0dcc5SMichael S. Tsirkin tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len)) 121056f0dcc5SMichael S. Tsirkin gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2); 12114909122fSHerbert Xu 121256f0dcc5SMichael S. Tsirkin if (tun16_to_cpu(tun, gso.hdr_len) > len) 1213f43798c2SRusty Russell return -EINVAL; 1214d8febb77SHerbert Xu iov_iter_advance(from, tun->vnet_hdr_sz - sizeof(gso)); 1215f43798c2SRusty Russell } 1216f43798c2SRusty Russell 121740630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) { 1218a504b86eSstephen hemminger align += NET_IP_ALIGN; 12190eca93bcSHerbert Xu if (unlikely(len < ETH_HLEN || 122056f0dcc5SMichael S. Tsirkin (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN))) 1221e01bf1c8SRusty Russell return -EINVAL; 1222e01bf1c8SRusty Russell } 12231da177e4SLinus Torvalds 122496f8d9ecSJason Wang good_linear = SKB_MAX_HEAD(align); 122596f8d9ecSJason Wang 122688529176SJason Wang if (msg_control) { 1227f5ff53b4SAl Viro struct iov_iter i = *from; 1228f5ff53b4SAl Viro 122988529176SJason Wang /* There are 256 bytes to be copied in skb, so there is 123088529176SJason Wang * enough room for skb expand head in case it is used. 12310690899bSMichael S. Tsirkin * The rest of the buffer is mapped from userspace. 12320690899bSMichael S. Tsirkin */ 123356f0dcc5SMichael S. Tsirkin copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN; 123496f8d9ecSJason Wang if (copylen > good_linear) 123596f8d9ecSJason Wang copylen = good_linear; 12363dd5c330SJason Wang linear = copylen; 1237f5ff53b4SAl Viro iov_iter_advance(&i, copylen); 1238f5ff53b4SAl Viro if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS) 123988529176SJason Wang zerocopy = true; 124088529176SJason Wang } 124188529176SJason Wang 124288529176SJason Wang if (!zerocopy) { 12430690899bSMichael S. Tsirkin copylen = len; 124456f0dcc5SMichael S. Tsirkin if (tun16_to_cpu(tun, gso.hdr_len) > good_linear) 124596f8d9ecSJason Wang linear = good_linear; 124696f8d9ecSJason Wang else 124756f0dcc5SMichael S. Tsirkin linear = tun16_to_cpu(tun, gso.hdr_len); 12483dd5c330SJason Wang } 12490690899bSMichael S. Tsirkin 12503dd5c330SJason Wang skb = tun_alloc_skb(tfile, align, copylen, linear, noblock); 125133dccbb0SHerbert Xu if (IS_ERR(skb)) { 125233dccbb0SHerbert Xu if (PTR_ERR(skb) != -EAGAIN) 1253608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 125433dccbb0SHerbert Xu return PTR_ERR(skb); 12551da177e4SLinus Torvalds } 12561da177e4SLinus Torvalds 12570690899bSMichael S. Tsirkin if (zerocopy) 1258f5ff53b4SAl Viro err = zerocopy_sg_from_iter(skb, from); 125988529176SJason Wang else { 1260f5ff53b4SAl Viro err = skb_copy_datagram_from_iter(skb, 0, from, len); 126188529176SJason Wang if (!err && msg_control) { 126288529176SJason Wang struct ubuf_info *uarg = msg_control; 126388529176SJason Wang uarg->callback(uarg, false); 126488529176SJason Wang } 126588529176SJason Wang } 12660690899bSMichael S. Tsirkin 12670690899bSMichael S. Tsirkin if (err) { 1268608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 12698f22757eSDave Jones kfree_skb(skb); 12701da177e4SLinus Torvalds return -EFAULT; 12718f22757eSDave Jones } 12721da177e4SLinus Torvalds 1273df10db98SPaolo Abeni err = virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun)); 1274df10db98SPaolo Abeni if (err) { 1275df10db98SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_frame_errors); 1276df10db98SPaolo Abeni kfree_skb(skb); 1277df10db98SPaolo Abeni return -EINVAL; 1278df10db98SPaolo Abeni } 1279df10db98SPaolo Abeni 12801da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 128140630b82SMichael S. Tsirkin case IFF_TUN: 128240630b82SMichael S. Tsirkin if (tun->flags & IFF_NO_PI) { 1283f09f7ee2SAng Way Chuang switch (skb->data[0] & 0xf0) { 1284f09f7ee2SAng Way Chuang case 0x40: 1285f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IP); 1286f09f7ee2SAng Way Chuang break; 1287f09f7ee2SAng Way Chuang case 0x60: 1288f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IPV6); 1289f09f7ee2SAng Way Chuang break; 1290f09f7ee2SAng Way Chuang default: 1291608b9977SPaolo Abeni this_cpu_inc(tun->pcpu_stats->rx_dropped); 1292f09f7ee2SAng Way Chuang kfree_skb(skb); 1293f09f7ee2SAng Way Chuang return -EINVAL; 1294f09f7ee2SAng Way Chuang } 1295f09f7ee2SAng Way Chuang } 1296f09f7ee2SAng Way Chuang 1297459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 12981da177e4SLinus Torvalds skb->protocol = pi.proto; 12994c13eb66SArnaldo Carvalho de Melo skb->dev = tun->dev; 13001da177e4SLinus Torvalds break; 130140630b82SMichael S. Tsirkin case IFF_TAP: 13021da177e4SLinus Torvalds skb->protocol = eth_type_trans(skb, tun->dev); 13031da177e4SLinus Torvalds break; 13046403eab1SJoe Perches } 13051da177e4SLinus Torvalds 13060690899bSMichael S. Tsirkin /* copy skb_ubuf_info for callback when skb has no error */ 13070690899bSMichael S. Tsirkin if (zerocopy) { 13080690899bSMichael S. Tsirkin skb_shinfo(skb)->destructor_arg = msg_control; 13090690899bSMichael S. Tsirkin skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; 1310c9af6db4SPravin B Shelar skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; 13110690899bSMichael S. Tsirkin } 13120690899bSMichael S. Tsirkin 131372f65107SVlad Yasevich skb_reset_network_header(skb); 131440893fd0SJason Wang skb_probe_transport_header(skb, 0); 131538502af7SJason Wang 13163958afa1STom Herbert rxhash = skb_get_hash(skb); 13171da177e4SLinus Torvalds netif_rx_ni(skb); 13181da177e4SLinus Torvalds 1319608b9977SPaolo Abeni stats = get_cpu_ptr(tun->pcpu_stats); 1320608b9977SPaolo Abeni u64_stats_update_begin(&stats->syncp); 1321608b9977SPaolo Abeni stats->rx_packets++; 1322608b9977SPaolo Abeni stats->rx_bytes += len; 1323608b9977SPaolo Abeni u64_stats_update_end(&stats->syncp); 1324608b9977SPaolo Abeni put_cpu_ptr(stats); 13251da177e4SLinus Torvalds 13269e85722dSJason Wang tun_flow_update(tun, rxhash, tfile); 13270690899bSMichael S. Tsirkin return total_len; 13281da177e4SLinus Torvalds } 13291da177e4SLinus Torvalds 1330f5ff53b4SAl Viro static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from) 13311da177e4SLinus Torvalds { 133233dccbb0SHerbert Xu struct file *file = iocb->ki_filp; 1333ab46d779SHerbert Xu struct tun_struct *tun = tun_get(file); 133454f968d6SJason Wang struct tun_file *tfile = file->private_data; 1335631ab46bSEric W. Biederman ssize_t result; 13361da177e4SLinus Torvalds 13371da177e4SLinus Torvalds if (!tun) 13381da177e4SLinus Torvalds return -EBADFD; 13391da177e4SLinus Torvalds 1340f5ff53b4SAl Viro result = tun_get_user(tun, tfile, NULL, from, file->f_flags & O_NONBLOCK); 1341631ab46bSEric W. Biederman 1342631ab46bSEric W. Biederman tun_put(tun); 1343631ab46bSEric W. Biederman return result; 13441da177e4SLinus Torvalds } 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds /* Put packet to the user space buffer */ 13476f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun, 134854f968d6SJason Wang struct tun_file *tfile, 13491da177e4SLinus Torvalds struct sk_buff *skb, 1350e0b46d0eSHerbert Xu struct iov_iter *iter) 13511da177e4SLinus Torvalds { 13521da177e4SLinus Torvalds struct tun_pi pi = { 0, skb->protocol }; 1353608b9977SPaolo Abeni struct tun_pcpu_stats *stats; 1354e0b46d0eSHerbert Xu ssize_t total; 13558c847d25SJason Wang int vlan_offset = 0; 1356a8f9bfdfSHerbert Xu int vlan_hlen = 0; 13572eb783c4SHerbert Xu int vnet_hdr_sz = 0; 1358a8f9bfdfSHerbert Xu 1359df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) 1360a8f9bfdfSHerbert Xu vlan_hlen = VLAN_HLEN; 13611da177e4SLinus Torvalds 136240630b82SMichael S. Tsirkin if (tun->flags & IFF_VNET_HDR) 13632eb783c4SHerbert Xu vnet_hdr_sz = tun->vnet_hdr_sz; 13641da177e4SLinus Torvalds 1365e0b46d0eSHerbert Xu total = skb->len + vlan_hlen + vnet_hdr_sz; 1366e0b46d0eSHerbert Xu 136740630b82SMichael S. Tsirkin if (!(tun->flags & IFF_NO_PI)) { 1368e0b46d0eSHerbert Xu if (iov_iter_count(iter) < sizeof(pi)) 13691da177e4SLinus Torvalds return -EINVAL; 13701da177e4SLinus Torvalds 1371e0b46d0eSHerbert Xu total += sizeof(pi); 1372e0b46d0eSHerbert Xu if (iov_iter_count(iter) < total) { 13731da177e4SLinus Torvalds /* Packet will be striped */ 13741da177e4SLinus Torvalds pi.flags |= TUN_PKT_STRIP; 13751da177e4SLinus Torvalds } 13761da177e4SLinus Torvalds 1377e0b46d0eSHerbert Xu if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi)) 13781da177e4SLinus Torvalds return -EFAULT; 13791da177e4SLinus Torvalds } 13801da177e4SLinus Torvalds 13812eb783c4SHerbert Xu if (vnet_hdr_sz) { 1382f43798c2SRusty Russell struct virtio_net_hdr gso = { 0 }; /* no info leak */ 138334166093SMike Rapoport int ret; 138434166093SMike Rapoport 1385e0b46d0eSHerbert Xu if (iov_iter_count(iter) < vnet_hdr_sz) 1386f43798c2SRusty Russell return -EINVAL; 1387f43798c2SRusty Russell 138834166093SMike Rapoport ret = virtio_net_hdr_from_skb(skb, &gso, 138934166093SMike Rapoport tun_is_little_endian(tun)); 139034166093SMike Rapoport if (ret) { 1391f43798c2SRusty Russell struct skb_shared_info *sinfo = skb_shinfo(skb); 13926b8a66eeSJoe Perches pr_err("unexpected GSO type: " 1393ef3db4a5SMichael S. Tsirkin "0x%x, gso_size %d, hdr_len %d\n", 139456f0dcc5SMichael S. Tsirkin sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size), 139556f0dcc5SMichael S. Tsirkin tun16_to_cpu(tun, gso.hdr_len)); 1396ef3db4a5SMichael S. Tsirkin print_hex_dump(KERN_ERR, "tun: ", 1397ef3db4a5SMichael S. Tsirkin DUMP_PREFIX_NONE, 1398ef3db4a5SMichael S. Tsirkin 16, 1, skb->head, 139956f0dcc5SMichael S. Tsirkin min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true); 1400ef3db4a5SMichael S. Tsirkin WARN_ON_ONCE(1); 1401ef3db4a5SMichael S. Tsirkin return -EINVAL; 1402ef3db4a5SMichael S. Tsirkin } 1403f43798c2SRusty Russell 1404e0b46d0eSHerbert Xu if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso)) 1405f43798c2SRusty Russell return -EFAULT; 14068c847d25SJason Wang 14078c847d25SJason Wang iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso)); 1408f43798c2SRusty Russell } 1409f43798c2SRusty Russell 1410a8f9bfdfSHerbert Xu if (vlan_hlen) { 1411e0b46d0eSHerbert Xu int ret; 14126680ec68SJason Wang struct { 14136680ec68SJason Wang __be16 h_vlan_proto; 14146680ec68SJason Wang __be16 h_vlan_TCI; 14156680ec68SJason Wang } veth; 14161da177e4SLinus Torvalds 14176680ec68SJason Wang veth.h_vlan_proto = skb->vlan_proto; 1418df8a39deSJiri Pirko veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb)); 14191da177e4SLinus Torvalds 14206680ec68SJason Wang vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); 14216680ec68SJason Wang 1422e0b46d0eSHerbert Xu ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset); 1423e0b46d0eSHerbert Xu if (ret || !iov_iter_count(iter)) 14246680ec68SJason Wang goto done; 14256680ec68SJason Wang 1426e0b46d0eSHerbert Xu ret = copy_to_iter(&veth, sizeof(veth), iter); 1427e0b46d0eSHerbert Xu if (ret != sizeof(veth) || !iov_iter_count(iter)) 14286680ec68SJason Wang goto done; 14296680ec68SJason Wang } 14306680ec68SJason Wang 1431e0b46d0eSHerbert Xu skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset); 14326680ec68SJason Wang 14336680ec68SJason Wang done: 1434608b9977SPaolo Abeni /* caller is in process context, */ 1435608b9977SPaolo Abeni stats = get_cpu_ptr(tun->pcpu_stats); 1436608b9977SPaolo Abeni u64_stats_update_begin(&stats->syncp); 1437608b9977SPaolo Abeni stats->tx_packets++; 1438608b9977SPaolo Abeni stats->tx_bytes += skb->len + vlan_hlen; 1439608b9977SPaolo Abeni u64_stats_update_end(&stats->syncp); 1440608b9977SPaolo Abeni put_cpu_ptr(tun->pcpu_stats); 14411da177e4SLinus Torvalds 14421da177e4SLinus Torvalds return total; 14431da177e4SLinus Torvalds } 14441da177e4SLinus Torvalds 14451576d986SJason Wang static struct sk_buff *tun_ring_recv(struct tun_file *tfile, int noblock, 14461576d986SJason Wang int *err) 14471576d986SJason Wang { 14481576d986SJason Wang DECLARE_WAITQUEUE(wait, current); 14491576d986SJason Wang struct sk_buff *skb = NULL; 1450*f48cc6b2SJason Wang int error = 0; 14511576d986SJason Wang 14521576d986SJason Wang skb = skb_array_consume(&tfile->tx_array); 14531576d986SJason Wang if (skb) 14541576d986SJason Wang goto out; 14551576d986SJason Wang if (noblock) { 1456*f48cc6b2SJason Wang error = -EAGAIN; 14571576d986SJason Wang goto out; 14581576d986SJason Wang } 14591576d986SJason Wang 14601576d986SJason Wang add_wait_queue(&tfile->wq.wait, &wait); 14611576d986SJason Wang current->state = TASK_INTERRUPTIBLE; 14621576d986SJason Wang 14631576d986SJason Wang while (1) { 14641576d986SJason Wang skb = skb_array_consume(&tfile->tx_array); 14651576d986SJason Wang if (skb) 14661576d986SJason Wang break; 14671576d986SJason Wang if (signal_pending(current)) { 1468*f48cc6b2SJason Wang error = -ERESTARTSYS; 14691576d986SJason Wang break; 14701576d986SJason Wang } 14711576d986SJason Wang if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) { 1472*f48cc6b2SJason Wang error = -EFAULT; 14731576d986SJason Wang break; 14741576d986SJason Wang } 14751576d986SJason Wang 14761576d986SJason Wang schedule(); 14771576d986SJason Wang } 14781576d986SJason Wang 14791576d986SJason Wang current->state = TASK_RUNNING; 14801576d986SJason Wang remove_wait_queue(&tfile->wq.wait, &wait); 14811576d986SJason Wang 14821576d986SJason Wang out: 1483*f48cc6b2SJason Wang *err = error; 14841576d986SJason Wang return skb; 14851576d986SJason Wang } 14861576d986SJason Wang 148754f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, 14889b067034SAl Viro struct iov_iter *to, 14899b067034SAl Viro int noblock) 14901da177e4SLinus Torvalds { 14911da177e4SLinus Torvalds struct sk_buff *skb; 14929b067034SAl Viro ssize_t ret; 14931576d986SJason Wang int err; 14941da177e4SLinus Torvalds 14953872baf6SRami Rosen tun_debug(KERN_INFO, tun, "tun_do_read\n"); 14961da177e4SLinus Torvalds 14979b067034SAl Viro if (!iov_iter_count(to)) 14989b067034SAl Viro return 0; 14991da177e4SLinus Torvalds 15001576d986SJason Wang /* Read frames from ring */ 15011576d986SJason Wang skb = tun_ring_recv(tfile, noblock, &err); 1502e0b46d0eSHerbert Xu if (!skb) 1503957f094fSAlex Gartrell return err; 1504e0b46d0eSHerbert Xu 15059b067034SAl Viro ret = tun_put_user(tun, tfile, skb, to); 1506f51a5e82SJason Wang if (unlikely(ret < 0)) 15071da177e4SLinus Torvalds kfree_skb(skb); 1508f51a5e82SJason Wang else 1509f51a5e82SJason Wang consume_skb(skb); 15101da177e4SLinus Torvalds 151105c2828cSMichael S. Tsirkin return ret; 151205c2828cSMichael S. Tsirkin } 151305c2828cSMichael S. Tsirkin 15149b067034SAl Viro static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to) 151505c2828cSMichael S. Tsirkin { 151605c2828cSMichael S. Tsirkin struct file *file = iocb->ki_filp; 151705c2828cSMichael S. Tsirkin struct tun_file *tfile = file->private_data; 151805c2828cSMichael S. Tsirkin struct tun_struct *tun = __tun_get(tfile); 15199b067034SAl Viro ssize_t len = iov_iter_count(to), ret; 152005c2828cSMichael S. Tsirkin 152105c2828cSMichael S. Tsirkin if (!tun) 152205c2828cSMichael S. Tsirkin return -EBADFD; 15239b067034SAl Viro ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK); 152442404c09SDavid S. Miller ret = min_t(ssize_t, ret, len); 1525d0b7da8aSZhi Yong Wu if (ret > 0) 1526d0b7da8aSZhi Yong Wu iocb->ki_pos = ret; 1527631ab46bSEric W. Biederman tun_put(tun); 15281da177e4SLinus Torvalds return ret; 15291da177e4SLinus Torvalds } 15301da177e4SLinus Torvalds 153196442e42SJason Wang static void tun_free_netdev(struct net_device *dev) 153296442e42SJason Wang { 153396442e42SJason Wang struct tun_struct *tun = netdev_priv(dev); 153496442e42SJason Wang 15354008e97fSJason Wang BUG_ON(!(list_empty(&tun->disabled))); 1536608b9977SPaolo Abeni free_percpu(tun->pcpu_stats); 153796442e42SJason Wang tun_flow_uninit(tun); 15385dbbaf2dSPaul Moore security_tun_dev_free_security(tun->security); 153996442e42SJason Wang free_netdev(dev); 154096442e42SJason Wang } 154196442e42SJason Wang 15421da177e4SLinus Torvalds static void tun_setup(struct net_device *dev) 15431da177e4SLinus Torvalds { 15441da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 15451da177e4SLinus Torvalds 15460625c883SEric W. Biederman tun->owner = INVALID_UID; 15470625c883SEric W. Biederman tun->group = INVALID_GID; 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds dev->ethtool_ops = &tun_ethtool_ops; 155096442e42SJason Wang dev->destructor = tun_free_netdev; 1551016adb72SJason Wang /* We prefer our own queue length */ 1552016adb72SJason Wang dev->tx_queue_len = TUN_READQ_SIZE; 15531da177e4SLinus Torvalds } 15541da177e4SLinus Torvalds 1555f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap 1556f019a7a5SEric W. Biederman * device with netlink. 1557f019a7a5SEric W. Biederman */ 1558f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[]) 1559f019a7a5SEric W. Biederman { 1560f019a7a5SEric W. Biederman return -EINVAL; 1561f019a7a5SEric W. Biederman } 1562f019a7a5SEric W. Biederman 1563f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = { 1564f019a7a5SEric W. Biederman .kind = DRV_NAME, 1565f019a7a5SEric W. Biederman .priv_size = sizeof(struct tun_struct), 1566f019a7a5SEric W. Biederman .setup = tun_setup, 1567f019a7a5SEric W. Biederman .validate = tun_validate, 1568f019a7a5SEric W. Biederman }; 1569f019a7a5SEric W. Biederman 157033dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk) 157133dccbb0SHerbert Xu { 157254f968d6SJason Wang struct tun_file *tfile; 157343815482SEric Dumazet wait_queue_head_t *wqueue; 157433dccbb0SHerbert Xu 157533dccbb0SHerbert Xu if (!sock_writeable(sk)) 157633dccbb0SHerbert Xu return; 157733dccbb0SHerbert Xu 15789cd3e072SEric Dumazet if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags)) 157933dccbb0SHerbert Xu return; 158033dccbb0SHerbert Xu 158143815482SEric Dumazet wqueue = sk_sleep(sk); 158243815482SEric Dumazet if (wqueue && waitqueue_active(wqueue)) 158343815482SEric Dumazet wake_up_interruptible_sync_poll(wqueue, POLLOUT | 158405c2828cSMichael S. Tsirkin POLLWRNORM | POLLWRBAND); 1585c722c625SHerbert Xu 158654f968d6SJason Wang tfile = container_of(sk, struct tun_file, sk); 158754f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); 158833dccbb0SHerbert Xu } 158933dccbb0SHerbert Xu 15901b784140SYing Xue static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len) 159105c2828cSMichael S. Tsirkin { 159254f968d6SJason Wang int ret; 159354f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 159454f968d6SJason Wang struct tun_struct *tun = __tun_get(tfile); 159554f968d6SJason Wang 159654f968d6SJason Wang if (!tun) 159754f968d6SJason Wang return -EBADFD; 1598f5ff53b4SAl Viro 1599c0371da6SAl Viro ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter, 1600f5ff53b4SAl Viro m->msg_flags & MSG_DONTWAIT); 160154f968d6SJason Wang tun_put(tun); 160254f968d6SJason Wang return ret; 160305c2828cSMichael S. Tsirkin } 160405c2828cSMichael S. Tsirkin 16051b784140SYing Xue static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, 160605c2828cSMichael S. Tsirkin int flags) 160705c2828cSMichael S. Tsirkin { 160854f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 160954f968d6SJason Wang struct tun_struct *tun = __tun_get(tfile); 161005c2828cSMichael S. Tsirkin int ret; 161154f968d6SJason Wang 161254f968d6SJason Wang if (!tun) 161354f968d6SJason Wang return -EBADFD; 161454f968d6SJason Wang 1615eda29772SRichard Cochran if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) { 16163811ae76SGao feng ret = -EINVAL; 16173811ae76SGao feng goto out; 16183811ae76SGao feng } 1619eda29772SRichard Cochran if (flags & MSG_ERRQUEUE) { 1620eda29772SRichard Cochran ret = sock_recv_errqueue(sock->sk, m, total_len, 1621eda29772SRichard Cochran SOL_PACKET, TUN_TX_TIMESTAMP); 1622eda29772SRichard Cochran goto out; 1623eda29772SRichard Cochran } 1624c0371da6SAl Viro ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT); 162587897931SAlex Gartrell if (ret > (ssize_t)total_len) { 162642404c09SDavid S. Miller m->msg_flags |= MSG_TRUNC; 162742404c09SDavid S. Miller ret = flags & MSG_TRUNC ? ret : total_len; 162842404c09SDavid S. Miller } 16293811ae76SGao feng out: 163054f968d6SJason Wang tun_put(tun); 163105c2828cSMichael S. Tsirkin return ret; 163205c2828cSMichael S. Tsirkin } 163305c2828cSMichael S. Tsirkin 16341576d986SJason Wang static int tun_peek_len(struct socket *sock) 16351576d986SJason Wang { 16361576d986SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 16371576d986SJason Wang struct tun_struct *tun; 16381576d986SJason Wang int ret = 0; 16391576d986SJason Wang 16401576d986SJason Wang tun = __tun_get(tfile); 16411576d986SJason Wang if (!tun) 16421576d986SJason Wang return 0; 16431576d986SJason Wang 16441576d986SJason Wang ret = skb_array_peek_len(&tfile->tx_array); 16451576d986SJason Wang tun_put(tun); 16461576d986SJason Wang 16471576d986SJason Wang return ret; 16481576d986SJason Wang } 16491576d986SJason Wang 165005c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */ 165105c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = { 16521576d986SJason Wang .peek_len = tun_peek_len, 165305c2828cSMichael S. Tsirkin .sendmsg = tun_sendmsg, 165405c2828cSMichael S. Tsirkin .recvmsg = tun_recvmsg, 165505c2828cSMichael S. Tsirkin }; 165605c2828cSMichael S. Tsirkin 165733dccbb0SHerbert Xu static struct proto tun_proto = { 165833dccbb0SHerbert Xu .name = "tun", 165933dccbb0SHerbert Xu .owner = THIS_MODULE, 166054f968d6SJason Wang .obj_size = sizeof(struct tun_file), 166133dccbb0SHerbert Xu }; 1662f019a7a5SEric W. Biederman 1663980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun) 1664980c9e8cSDavid Woodhouse { 1665031f5e03SMichael S. Tsirkin return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP); 1666980c9e8cSDavid Woodhouse } 1667980c9e8cSDavid Woodhouse 1668980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr, 1669980c9e8cSDavid Woodhouse char *buf) 1670980c9e8cSDavid Woodhouse { 1671980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 1672980c9e8cSDavid Woodhouse return sprintf(buf, "0x%x\n", tun_flags(tun)); 1673980c9e8cSDavid Woodhouse } 1674980c9e8cSDavid Woodhouse 1675980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr, 1676980c9e8cSDavid Woodhouse char *buf) 1677980c9e8cSDavid Woodhouse { 1678980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 16790625c883SEric W. Biederman return uid_valid(tun->owner)? 16800625c883SEric W. Biederman sprintf(buf, "%u\n", 16810625c883SEric W. Biederman from_kuid_munged(current_user_ns(), tun->owner)): 16820625c883SEric W. Biederman sprintf(buf, "-1\n"); 1683980c9e8cSDavid Woodhouse } 1684980c9e8cSDavid Woodhouse 1685980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr, 1686980c9e8cSDavid Woodhouse char *buf) 1687980c9e8cSDavid Woodhouse { 1688980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 16890625c883SEric W. Biederman return gid_valid(tun->group) ? 16900625c883SEric W. Biederman sprintf(buf, "%u\n", 16910625c883SEric W. Biederman from_kgid_munged(current_user_ns(), tun->group)): 16920625c883SEric W. Biederman sprintf(buf, "-1\n"); 1693980c9e8cSDavid Woodhouse } 1694980c9e8cSDavid Woodhouse 1695980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL); 1696980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL); 1697980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL); 1698980c9e8cSDavid Woodhouse 1699c4d33e24STakashi Iwai static struct attribute *tun_dev_attrs[] = { 1700c4d33e24STakashi Iwai &dev_attr_tun_flags.attr, 1701c4d33e24STakashi Iwai &dev_attr_owner.attr, 1702c4d33e24STakashi Iwai &dev_attr_group.attr, 1703c4d33e24STakashi Iwai NULL 1704c4d33e24STakashi Iwai }; 1705c4d33e24STakashi Iwai 1706c4d33e24STakashi Iwai static const struct attribute_group tun_attr_group = { 1707c4d33e24STakashi Iwai .attrs = tun_dev_attrs 1708c4d33e24STakashi Iwai }; 1709c4d33e24STakashi Iwai 1710d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) 17111da177e4SLinus Torvalds { 17121da177e4SLinus Torvalds struct tun_struct *tun; 171354f968d6SJason Wang struct tun_file *tfile = file->private_data; 17141da177e4SLinus Torvalds struct net_device *dev; 17151da177e4SLinus Torvalds int err; 17161da177e4SLinus Torvalds 17177c0c3b1aSJason Wang if (tfile->detached) 17187c0c3b1aSJason Wang return -EINVAL; 17197c0c3b1aSJason Wang 172074a3e5a7SEric W. Biederman dev = __dev_get_by_name(net, ifr->ifr_name); 172174a3e5a7SEric W. Biederman if (dev) { 1722f85ba780SDavid Woodhouse if (ifr->ifr_flags & IFF_TUN_EXCL) 1723f85ba780SDavid Woodhouse return -EBUSY; 172474a3e5a7SEric W. Biederman if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) 172574a3e5a7SEric W. Biederman tun = netdev_priv(dev); 172674a3e5a7SEric W. Biederman else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) 172774a3e5a7SEric W. Biederman tun = netdev_priv(dev); 172874a3e5a7SEric W. Biederman else 172974a3e5a7SEric W. Biederman return -EINVAL; 173074a3e5a7SEric W. Biederman 17318e6d91aeSJason Wang if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) != 173240630b82SMichael S. Tsirkin !!(tun->flags & IFF_MULTI_QUEUE)) 17338e6d91aeSJason Wang return -EINVAL; 17348e6d91aeSJason Wang 1735cde8b15fSJason Wang if (tun_not_capable(tun)) 17362b980dbdSPaul Moore return -EPERM; 17375dbbaf2dSPaul Moore err = security_tun_dev_open(tun->security); 17382b980dbdSPaul Moore if (err < 0) 17392b980dbdSPaul Moore return err; 17402b980dbdSPaul Moore 1741849c9b6fSPavel Emelyanov err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER); 1742a7385ba2SEric W. Biederman if (err < 0) 1743a7385ba2SEric W. Biederman return err; 17444008e97fSJason Wang 174540630b82SMichael S. Tsirkin if (tun->flags & IFF_MULTI_QUEUE && 1746e8dbad66SJason Wang (tun->numqueues + tun->numdisabled > 1)) { 1747e8dbad66SJason Wang /* One or more queue has already been attached, no need 1748e8dbad66SJason Wang * to initialize the device again. 1749e8dbad66SJason Wang */ 1750e8dbad66SJason Wang return 0; 1751e8dbad66SJason Wang } 175286a264abSDavid Howells } 17531da177e4SLinus Torvalds else { 17541da177e4SLinus Torvalds char *name; 17551da177e4SLinus Torvalds unsigned long flags = 0; 1756edfb6a14SJason Wang int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ? 1757edfb6a14SJason Wang MAX_TAP_QUEUES : 1; 17581da177e4SLinus Torvalds 1759c260b772SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1760ca6bb5d7SDavid Woodhouse return -EPERM; 17612b980dbdSPaul Moore err = security_tun_dev_create(); 17622b980dbdSPaul Moore if (err < 0) 17632b980dbdSPaul Moore return err; 1764ca6bb5d7SDavid Woodhouse 17651da177e4SLinus Torvalds /* Set dev type */ 17661da177e4SLinus Torvalds if (ifr->ifr_flags & IFF_TUN) { 17671da177e4SLinus Torvalds /* TUN device */ 176840630b82SMichael S. Tsirkin flags |= IFF_TUN; 17691da177e4SLinus Torvalds name = "tun%d"; 17701da177e4SLinus Torvalds } else if (ifr->ifr_flags & IFF_TAP) { 17711da177e4SLinus Torvalds /* TAP device */ 177240630b82SMichael S. Tsirkin flags |= IFF_TAP; 17731da177e4SLinus Torvalds name = "tap%d"; 17741da177e4SLinus Torvalds } else 177536989b90SKusanagi Kouichi return -EINVAL; 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds if (*ifr->ifr_name) 17781da177e4SLinus Torvalds name = ifr->ifr_name; 17791da177e4SLinus Torvalds 1780c8d68e6bSJason Wang dev = alloc_netdev_mqs(sizeof(struct tun_struct), name, 1781c835a677STom Gundersen NET_NAME_UNKNOWN, tun_setup, queues, 1782c835a677STom Gundersen queues); 1783edfb6a14SJason Wang 17841da177e4SLinus Torvalds if (!dev) 17851da177e4SLinus Torvalds return -ENOMEM; 17861da177e4SLinus Torvalds 1787fc54c658SPavel Emelyanov dev_net_set(dev, net); 1788f019a7a5SEric W. Biederman dev->rtnl_link_ops = &tun_link_ops; 1789fb7589a1SPavel Emelyanov dev->ifindex = tfile->ifindex; 1790c4d33e24STakashi Iwai dev->sysfs_groups[0] = &tun_attr_group; 1791758e43b7SStephen Hemminger 17921da177e4SLinus Torvalds tun = netdev_priv(dev); 17931da177e4SLinus Torvalds tun->dev = dev; 17941da177e4SLinus Torvalds tun->flags = flags; 1795f271b2ccSMax Krasnyansky tun->txflt.count = 0; 1796d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr); 17971da177e4SLinus Torvalds 1798eaea34b2SPaolo Abeni tun->align = NET_SKB_PAD; 179954f968d6SJason Wang tun->filter_attached = false; 180054f968d6SJason Wang tun->sndbuf = tfile->socket.sk->sk_sndbuf; 180133dccbb0SHerbert Xu 1802608b9977SPaolo Abeni tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats); 1803608b9977SPaolo Abeni if (!tun->pcpu_stats) { 1804608b9977SPaolo Abeni err = -ENOMEM; 1805608b9977SPaolo Abeni goto err_free_dev; 1806608b9977SPaolo Abeni } 1807608b9977SPaolo Abeni 180896442e42SJason Wang spin_lock_init(&tun->lock); 180996442e42SJason Wang 18105dbbaf2dSPaul Moore err = security_tun_dev_alloc_security(&tun->security); 18115dbbaf2dSPaul Moore if (err < 0) 1812608b9977SPaolo Abeni goto err_free_stat; 18132b980dbdSPaul Moore 18141da177e4SLinus Torvalds tun_net_init(dev); 1815944a1376SPavel Emelyanov tun_flow_init(tun); 181696442e42SJason Wang 181788255375SMichał Mirosław dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | 18186680ec68SJason Wang TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX | 18196680ec68SJason Wang NETIF_F_HW_VLAN_STAG_TX; 18202a2bbf17SPaolo Abeni dev->features = dev->hw_features | NETIF_F_LLTX; 18216671b224SFernando Luis Vazquez Cao dev->vlan_features = dev->features & 18226671b224SFernando Luis Vazquez Cao ~(NETIF_F_HW_VLAN_CTAG_TX | 18236671b224SFernando Luis Vazquez Cao NETIF_F_HW_VLAN_STAG_TX); 182488255375SMichał Mirosław 18254008e97fSJason Wang INIT_LIST_HEAD(&tun->disabled); 1826849c9b6fSPavel Emelyanov err = tun_attach(tun, file, false); 1827eb0fb363SJason Wang if (err < 0) 1828662ca437SJason Wang goto err_free_flow; 1829eb0fb363SJason Wang 18301da177e4SLinus Torvalds err = register_netdevice(tun->dev); 18311da177e4SLinus Torvalds if (err < 0) 1832662ca437SJason Wang goto err_detach; 1833af668b3cSMichael S. Tsirkin } 1834980c9e8cSDavid Woodhouse 1835eb0fb363SJason Wang netif_carrier_on(tun->dev); 18361da177e4SLinus Torvalds 18376b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_set_iff\n"); 18381da177e4SLinus Torvalds 1839031f5e03SMichael S. Tsirkin tun->flags = (tun->flags & ~TUN_FEATURES) | 1840031f5e03SMichael S. Tsirkin (ifr->ifr_flags & TUN_FEATURES); 1841c8d68e6bSJason Wang 1842e35259a9SMax Krasnyansky /* Make sure persistent devices do not get stuck in 1843e35259a9SMax Krasnyansky * xoff state. 1844e35259a9SMax Krasnyansky */ 1845e35259a9SMax Krasnyansky if (netif_running(tun->dev)) 1846c8d68e6bSJason Wang netif_tx_wake_all_queues(tun->dev); 1847e35259a9SMax Krasnyansky 18481da177e4SLinus Torvalds strcpy(ifr->ifr_name, tun->dev->name); 18491da177e4SLinus Torvalds return 0; 18501da177e4SLinus Torvalds 1851662ca437SJason Wang err_detach: 1852662ca437SJason Wang tun_detach_all(dev); 1853662ca437SJason Wang err_free_flow: 1854662ca437SJason Wang tun_flow_uninit(tun); 1855662ca437SJason Wang security_tun_dev_free_security(tun->security); 1856608b9977SPaolo Abeni err_free_stat: 1857608b9977SPaolo Abeni free_percpu(tun->pcpu_stats); 18581da177e4SLinus Torvalds err_free_dev: 18591da177e4SLinus Torvalds free_netdev(dev); 18601da177e4SLinus Torvalds return err; 18611da177e4SLinus Torvalds } 18621da177e4SLinus Torvalds 18639ce99cf6SRami Rosen static void tun_get_iff(struct net *net, struct tun_struct *tun, 1864876bfd4dSHerbert Xu struct ifreq *ifr) 1865e3b99556SMark McLoughlin { 18666b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_get_iff\n"); 1867e3b99556SMark McLoughlin 1868e3b99556SMark McLoughlin strcpy(ifr->ifr_name, tun->dev->name); 1869e3b99556SMark McLoughlin 1870980c9e8cSDavid Woodhouse ifr->ifr_flags = tun_flags(tun); 1871e3b99556SMark McLoughlin 1872e3b99556SMark McLoughlin } 1873e3b99556SMark McLoughlin 18745228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no 18755228ddc9SRusty Russell * privs required. */ 187688255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg) 18775228ddc9SRusty Russell { 1878c8f44affSMichał Mirosław netdev_features_t features = 0; 18795228ddc9SRusty Russell 18805228ddc9SRusty Russell if (arg & TUN_F_CSUM) { 188188255375SMichał Mirosław features |= NETIF_F_HW_CSUM; 18825228ddc9SRusty Russell arg &= ~TUN_F_CSUM; 18835228ddc9SRusty Russell 18845228ddc9SRusty Russell if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { 18855228ddc9SRusty Russell if (arg & TUN_F_TSO_ECN) { 18865228ddc9SRusty Russell features |= NETIF_F_TSO_ECN; 18875228ddc9SRusty Russell arg &= ~TUN_F_TSO_ECN; 18885228ddc9SRusty Russell } 18895228ddc9SRusty Russell if (arg & TUN_F_TSO4) 18905228ddc9SRusty Russell features |= NETIF_F_TSO; 18915228ddc9SRusty Russell if (arg & TUN_F_TSO6) 18925228ddc9SRusty Russell features |= NETIF_F_TSO6; 18935228ddc9SRusty Russell arg &= ~(TUN_F_TSO4|TUN_F_TSO6); 18945228ddc9SRusty Russell } 1895e3e3c423SVlad Yasevich 1896e3e3c423SVlad Yasevich if (arg & TUN_F_UFO) { 1897e3e3c423SVlad Yasevich features |= NETIF_F_UFO; 1898e3e3c423SVlad Yasevich arg &= ~TUN_F_UFO; 1899e3e3c423SVlad Yasevich } 19005228ddc9SRusty Russell } 19015228ddc9SRusty Russell 19025228ddc9SRusty Russell /* This gives the user a way to test for new features in future by 19035228ddc9SRusty Russell * trying to set them. */ 19045228ddc9SRusty Russell if (arg) 19055228ddc9SRusty Russell return -EINVAL; 19065228ddc9SRusty Russell 190788255375SMichał Mirosław tun->set_features = features; 190888255375SMichał Mirosław netdev_update_features(tun->dev); 19095228ddc9SRusty Russell 19105228ddc9SRusty Russell return 0; 19115228ddc9SRusty Russell } 19125228ddc9SRusty Russell 1913c8d68e6bSJason Wang static void tun_detach_filter(struct tun_struct *tun, int n) 1914c8d68e6bSJason Wang { 1915c8d68e6bSJason Wang int i; 1916c8d68e6bSJason Wang struct tun_file *tfile; 1917c8d68e6bSJason Wang 1918c8d68e6bSJason Wang for (i = 0; i < n; i++) { 1919b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 19208ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 19218ced425eSHannes Frederic Sowa sk_detach_filter(tfile->socket.sk); 19228ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 1923c8d68e6bSJason Wang } 1924c8d68e6bSJason Wang 1925c8d68e6bSJason Wang tun->filter_attached = false; 1926c8d68e6bSJason Wang } 1927c8d68e6bSJason Wang 1928c8d68e6bSJason Wang static int tun_attach_filter(struct tun_struct *tun) 1929c8d68e6bSJason Wang { 1930c8d68e6bSJason Wang int i, ret = 0; 1931c8d68e6bSJason Wang struct tun_file *tfile; 1932c8d68e6bSJason Wang 1933c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 1934b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 19358ced425eSHannes Frederic Sowa lock_sock(tfile->socket.sk); 19368ced425eSHannes Frederic Sowa ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); 19378ced425eSHannes Frederic Sowa release_sock(tfile->socket.sk); 1938c8d68e6bSJason Wang if (ret) { 1939c8d68e6bSJason Wang tun_detach_filter(tun, i); 1940c8d68e6bSJason Wang return ret; 1941c8d68e6bSJason Wang } 1942c8d68e6bSJason Wang } 1943c8d68e6bSJason Wang 1944c8d68e6bSJason Wang tun->filter_attached = true; 1945c8d68e6bSJason Wang return ret; 1946c8d68e6bSJason Wang } 1947c8d68e6bSJason Wang 1948c8d68e6bSJason Wang static void tun_set_sndbuf(struct tun_struct *tun) 1949c8d68e6bSJason Wang { 1950c8d68e6bSJason Wang struct tun_file *tfile; 1951c8d68e6bSJason Wang int i; 1952c8d68e6bSJason Wang 1953c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 1954b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 1955c8d68e6bSJason Wang tfile->socket.sk->sk_sndbuf = tun->sndbuf; 1956c8d68e6bSJason Wang } 1957c8d68e6bSJason Wang } 1958c8d68e6bSJason Wang 1959cde8b15fSJason Wang static int tun_set_queue(struct file *file, struct ifreq *ifr) 1960cde8b15fSJason Wang { 1961cde8b15fSJason Wang struct tun_file *tfile = file->private_data; 1962cde8b15fSJason Wang struct tun_struct *tun; 1963cde8b15fSJason Wang int ret = 0; 1964cde8b15fSJason Wang 1965cde8b15fSJason Wang rtnl_lock(); 1966cde8b15fSJason Wang 1967cde8b15fSJason Wang if (ifr->ifr_flags & IFF_ATTACH_QUEUE) { 19684008e97fSJason Wang tun = tfile->detached; 19695dbbaf2dSPaul Moore if (!tun) { 1970cde8b15fSJason Wang ret = -EINVAL; 19715dbbaf2dSPaul Moore goto unlock; 19725dbbaf2dSPaul Moore } 19735dbbaf2dSPaul Moore ret = security_tun_dev_attach_queue(tun->security); 19745dbbaf2dSPaul Moore if (ret < 0) 19755dbbaf2dSPaul Moore goto unlock; 1976849c9b6fSPavel Emelyanov ret = tun_attach(tun, file, false); 19774008e97fSJason Wang } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) { 1978b8deabd3SJason Wang tun = rtnl_dereference(tfile->tun); 197940630b82SMichael S. Tsirkin if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached) 19804008e97fSJason Wang ret = -EINVAL; 1981cde8b15fSJason Wang else 19824008e97fSJason Wang __tun_detach(tfile, false); 19834008e97fSJason Wang } else 1984cde8b15fSJason Wang ret = -EINVAL; 1985cde8b15fSJason Wang 19865dbbaf2dSPaul Moore unlock: 1987cde8b15fSJason Wang rtnl_unlock(); 1988cde8b15fSJason Wang return ret; 1989cde8b15fSJason Wang } 1990cde8b15fSJason Wang 199150857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd, 199250857e2aSArnd Bergmann unsigned long arg, int ifreq_len) 19931da177e4SLinus Torvalds { 199436b50babSEric W. Biederman struct tun_file *tfile = file->private_data; 1995631ab46bSEric W. Biederman struct tun_struct *tun; 19961da177e4SLinus Torvalds void __user* argp = (void __user*)arg; 19971da177e4SLinus Torvalds struct ifreq ifr; 19980625c883SEric W. Biederman kuid_t owner; 19990625c883SEric W. Biederman kgid_t group; 200033dccbb0SHerbert Xu int sndbuf; 2001d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 2002fb7589a1SPavel Emelyanov unsigned int ifindex; 20031cf8e410SMichael S. Tsirkin int le; 2004f271b2ccSMax Krasnyansky int ret; 20051da177e4SLinus Torvalds 2006cde8b15fSJason Wang if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) { 200750857e2aSArnd Bergmann if (copy_from_user(&ifr, argp, ifreq_len)) 20081da177e4SLinus Torvalds return -EFAULT; 20098bbb1813SDavid S. Miller } else { 2010a117dacdSMathias Krause memset(&ifr, 0, sizeof(ifr)); 20118bbb1813SDavid S. Miller } 2012631ab46bSEric W. Biederman if (cmd == TUNGETFEATURES) { 2013631ab46bSEric W. Biederman /* Currently this just means: "what IFF flags are valid?". 2014631ab46bSEric W. Biederman * This is needed because we never checked for invalid flags on 2015031f5e03SMichael S. Tsirkin * TUNSETIFF. 2016031f5e03SMichael S. Tsirkin */ 2017031f5e03SMichael S. Tsirkin return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES, 2018631ab46bSEric W. Biederman (unsigned int __user*)argp); 2019cde8b15fSJason Wang } else if (cmd == TUNSETQUEUE) 2020cde8b15fSJason Wang return tun_set_queue(file, &ifr); 2021631ab46bSEric W. Biederman 2022c8d68e6bSJason Wang ret = 0; 2023876bfd4dSHerbert Xu rtnl_lock(); 2024876bfd4dSHerbert Xu 202536b50babSEric W. Biederman tun = __tun_get(tfile); 20261da177e4SLinus Torvalds if (cmd == TUNSETIFF && !tun) { 20271da177e4SLinus Torvalds ifr.ifr_name[IFNAMSIZ-1] = '\0'; 20281da177e4SLinus Torvalds 2029140e807dSEric W. Biederman ret = tun_set_iff(sock_net(&tfile->sk), file, &ifr); 20301da177e4SLinus Torvalds 2031876bfd4dSHerbert Xu if (ret) 2032876bfd4dSHerbert Xu goto unlock; 20331da177e4SLinus Torvalds 203450857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 2035876bfd4dSHerbert Xu ret = -EFAULT; 2036876bfd4dSHerbert Xu goto unlock; 20371da177e4SLinus Torvalds } 2038fb7589a1SPavel Emelyanov if (cmd == TUNSETIFINDEX) { 2039fb7589a1SPavel Emelyanov ret = -EPERM; 2040fb7589a1SPavel Emelyanov if (tun) 2041fb7589a1SPavel Emelyanov goto unlock; 2042fb7589a1SPavel Emelyanov 2043fb7589a1SPavel Emelyanov ret = -EFAULT; 2044fb7589a1SPavel Emelyanov if (copy_from_user(&ifindex, argp, sizeof(ifindex))) 2045fb7589a1SPavel Emelyanov goto unlock; 2046fb7589a1SPavel Emelyanov 2047fb7589a1SPavel Emelyanov ret = 0; 2048fb7589a1SPavel Emelyanov tfile->ifindex = ifindex; 2049fb7589a1SPavel Emelyanov goto unlock; 2050fb7589a1SPavel Emelyanov } 20511da177e4SLinus Torvalds 2052876bfd4dSHerbert Xu ret = -EBADFD; 20531da177e4SLinus Torvalds if (!tun) 2054876bfd4dSHerbert Xu goto unlock; 20551da177e4SLinus Torvalds 20561e588338SJason Wang tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd); 20571da177e4SLinus Torvalds 2058631ab46bSEric W. Biederman ret = 0; 20591da177e4SLinus Torvalds switch (cmd) { 2060e3b99556SMark McLoughlin case TUNGETIFF: 20619ce99cf6SRami Rosen tun_get_iff(current->nsproxy->net_ns, tun, &ifr); 2062e3b99556SMark McLoughlin 20633d407a80SPavel Emelyanov if (tfile->detached) 20643d407a80SPavel Emelyanov ifr.ifr_flags |= IFF_DETACH_QUEUE; 2065849c9b6fSPavel Emelyanov if (!tfile->socket.sk->sk_filter) 2066849c9b6fSPavel Emelyanov ifr.ifr_flags |= IFF_NOFILTER; 20673d407a80SPavel Emelyanov 206850857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 2069631ab46bSEric W. Biederman ret = -EFAULT; 2070e3b99556SMark McLoughlin break; 2071e3b99556SMark McLoughlin 20721da177e4SLinus Torvalds case TUNSETNOCSUM: 20731da177e4SLinus Torvalds /* Disable/Enable checksum */ 20741da177e4SLinus Torvalds 207588255375SMichał Mirosław /* [unimplemented] */ 207688255375SMichał Mirosław tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n", 20776b8a66eeSJoe Perches arg ? "disabled" : "enabled"); 20781da177e4SLinus Torvalds break; 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds case TUNSETPERSIST: 208154f968d6SJason Wang /* Disable/Enable persist mode. Keep an extra reference to the 208254f968d6SJason Wang * module to prevent the module being unprobed. 208354f968d6SJason Wang */ 208440630b82SMichael S. Tsirkin if (arg && !(tun->flags & IFF_PERSIST)) { 208540630b82SMichael S. Tsirkin tun->flags |= IFF_PERSIST; 208654f968d6SJason Wang __module_get(THIS_MODULE); 2087dd38bd85SJason Wang } 208840630b82SMichael S. Tsirkin if (!arg && (tun->flags & IFF_PERSIST)) { 208940630b82SMichael S. Tsirkin tun->flags &= ~IFF_PERSIST; 209054f968d6SJason Wang module_put(THIS_MODULE); 209154f968d6SJason Wang } 20921da177e4SLinus Torvalds 20936b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "persist %s\n", 20946b8a66eeSJoe Perches arg ? "enabled" : "disabled"); 20951da177e4SLinus Torvalds break; 20961da177e4SLinus Torvalds 20971da177e4SLinus Torvalds case TUNSETOWNER: 20981da177e4SLinus Torvalds /* Set owner of the device */ 20990625c883SEric W. Biederman owner = make_kuid(current_user_ns(), arg); 21000625c883SEric W. Biederman if (!uid_valid(owner)) { 21010625c883SEric W. Biederman ret = -EINVAL; 21020625c883SEric W. Biederman break; 21030625c883SEric W. Biederman } 21040625c883SEric W. Biederman tun->owner = owner; 21051e588338SJason Wang tun_debug(KERN_INFO, tun, "owner set to %u\n", 21060625c883SEric W. Biederman from_kuid(&init_user_ns, tun->owner)); 21071da177e4SLinus Torvalds break; 21081da177e4SLinus Torvalds 21098c644623SGuido Guenther case TUNSETGROUP: 21108c644623SGuido Guenther /* Set group of the device */ 21110625c883SEric W. Biederman group = make_kgid(current_user_ns(), arg); 21120625c883SEric W. Biederman if (!gid_valid(group)) { 21130625c883SEric W. Biederman ret = -EINVAL; 21140625c883SEric W. Biederman break; 21150625c883SEric W. Biederman } 21160625c883SEric W. Biederman tun->group = group; 21171e588338SJason Wang tun_debug(KERN_INFO, tun, "group set to %u\n", 21180625c883SEric W. Biederman from_kgid(&init_user_ns, tun->group)); 21198c644623SGuido Guenther break; 21208c644623SGuido Guenther 2121ff4cc3acSMike Kershaw case TUNSETLINK: 2122ff4cc3acSMike Kershaw /* Only allow setting the type when the interface is down */ 2123ff4cc3acSMike Kershaw if (tun->dev->flags & IFF_UP) { 21246b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, 21256b8a66eeSJoe Perches "Linktype set failed because interface is up\n"); 212648abfe05SDavid S. Miller ret = -EBUSY; 2127ff4cc3acSMike Kershaw } else { 2128ff4cc3acSMike Kershaw tun->dev->type = (int) arg; 21296b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "linktype set to %d\n", 21306b8a66eeSJoe Perches tun->dev->type); 213148abfe05SDavid S. Miller ret = 0; 2132ff4cc3acSMike Kershaw } 2133631ab46bSEric W. Biederman break; 2134ff4cc3acSMike Kershaw 21351da177e4SLinus Torvalds #ifdef TUN_DEBUG 21361da177e4SLinus Torvalds case TUNSETDEBUG: 21371da177e4SLinus Torvalds tun->debug = arg; 21381da177e4SLinus Torvalds break; 21391da177e4SLinus Torvalds #endif 21405228ddc9SRusty Russell case TUNSETOFFLOAD: 214188255375SMichał Mirosław ret = set_offload(tun, arg); 2142631ab46bSEric W. Biederman break; 21435228ddc9SRusty Russell 2144f271b2ccSMax Krasnyansky case TUNSETTXFILTER: 2145f271b2ccSMax Krasnyansky /* Can be set only for TAPs */ 2146631ab46bSEric W. Biederman ret = -EINVAL; 214740630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 2148631ab46bSEric W. Biederman break; 2149c0e5a8c2SHarvey Harrison ret = update_filter(&tun->txflt, (void __user *)arg); 2150631ab46bSEric W. Biederman break; 21511da177e4SLinus Torvalds 21521da177e4SLinus Torvalds case SIOCGIFHWADDR: 2153b595076aSUwe Kleine-König /* Get hw address */ 2154f271b2ccSMax Krasnyansky memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); 2155f271b2ccSMax Krasnyansky ifr.ifr_hwaddr.sa_family = tun->dev->type; 215650857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 2157631ab46bSEric W. Biederman ret = -EFAULT; 2158631ab46bSEric W. Biederman break; 21591da177e4SLinus Torvalds 21601da177e4SLinus Torvalds case SIOCSIFHWADDR: 2161f271b2ccSMax Krasnyansky /* Set hw address */ 21626b8a66eeSJoe Perches tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n", 21636b8a66eeSJoe Perches ifr.ifr_hwaddr.sa_data); 216440102371SKim B. Heino 216540102371SKim B. Heino ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); 2166631ab46bSEric W. Biederman break; 216733dccbb0SHerbert Xu 216833dccbb0SHerbert Xu case TUNGETSNDBUF: 216954f968d6SJason Wang sndbuf = tfile->socket.sk->sk_sndbuf; 217033dccbb0SHerbert Xu if (copy_to_user(argp, &sndbuf, sizeof(sndbuf))) 217133dccbb0SHerbert Xu ret = -EFAULT; 217233dccbb0SHerbert Xu break; 217333dccbb0SHerbert Xu 217433dccbb0SHerbert Xu case TUNSETSNDBUF: 217533dccbb0SHerbert Xu if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) { 217633dccbb0SHerbert Xu ret = -EFAULT; 217733dccbb0SHerbert Xu break; 217833dccbb0SHerbert Xu } 217933dccbb0SHerbert Xu 2180c8d68e6bSJason Wang tun->sndbuf = sndbuf; 2181c8d68e6bSJason Wang tun_set_sndbuf(tun); 218233dccbb0SHerbert Xu break; 218333dccbb0SHerbert Xu 2184d9d52b51SMichael S. Tsirkin case TUNGETVNETHDRSZ: 2185d9d52b51SMichael S. Tsirkin vnet_hdr_sz = tun->vnet_hdr_sz; 2186d9d52b51SMichael S. Tsirkin if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz))) 2187d9d52b51SMichael S. Tsirkin ret = -EFAULT; 2188d9d52b51SMichael S. Tsirkin break; 2189d9d52b51SMichael S. Tsirkin 2190d9d52b51SMichael S. Tsirkin case TUNSETVNETHDRSZ: 2191d9d52b51SMichael S. Tsirkin if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) { 2192d9d52b51SMichael S. Tsirkin ret = -EFAULT; 2193d9d52b51SMichael S. Tsirkin break; 2194d9d52b51SMichael S. Tsirkin } 2195d9d52b51SMichael S. Tsirkin if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) { 2196d9d52b51SMichael S. Tsirkin ret = -EINVAL; 2197d9d52b51SMichael S. Tsirkin break; 2198d9d52b51SMichael S. Tsirkin } 2199d9d52b51SMichael S. Tsirkin 2200d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = vnet_hdr_sz; 2201d9d52b51SMichael S. Tsirkin break; 2202d9d52b51SMichael S. Tsirkin 22031cf8e410SMichael S. Tsirkin case TUNGETVNETLE: 22041cf8e410SMichael S. Tsirkin le = !!(tun->flags & TUN_VNET_LE); 22051cf8e410SMichael S. Tsirkin if (put_user(le, (int __user *)argp)) 22061cf8e410SMichael S. Tsirkin ret = -EFAULT; 22071cf8e410SMichael S. Tsirkin break; 22081cf8e410SMichael S. Tsirkin 22091cf8e410SMichael S. Tsirkin case TUNSETVNETLE: 22101cf8e410SMichael S. Tsirkin if (get_user(le, (int __user *)argp)) { 22111cf8e410SMichael S. Tsirkin ret = -EFAULT; 22121cf8e410SMichael S. Tsirkin break; 22131cf8e410SMichael S. Tsirkin } 22141cf8e410SMichael S. Tsirkin if (le) 22151cf8e410SMichael S. Tsirkin tun->flags |= TUN_VNET_LE; 22161cf8e410SMichael S. Tsirkin else 22171cf8e410SMichael S. Tsirkin tun->flags &= ~TUN_VNET_LE; 22181cf8e410SMichael S. Tsirkin break; 22191cf8e410SMichael S. Tsirkin 22208b8e658bSGreg Kurz case TUNGETVNETBE: 22218b8e658bSGreg Kurz ret = tun_get_vnet_be(tun, argp); 22228b8e658bSGreg Kurz break; 22238b8e658bSGreg Kurz 22248b8e658bSGreg Kurz case TUNSETVNETBE: 22258b8e658bSGreg Kurz ret = tun_set_vnet_be(tun, argp); 22268b8e658bSGreg Kurz break; 22278b8e658bSGreg Kurz 222899405162SMichael S. Tsirkin case TUNATTACHFILTER: 222999405162SMichael S. Tsirkin /* Can be set only for TAPs */ 223099405162SMichael S. Tsirkin ret = -EINVAL; 223140630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 223299405162SMichael S. Tsirkin break; 223399405162SMichael S. Tsirkin ret = -EFAULT; 223454f968d6SJason Wang if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog))) 223599405162SMichael S. Tsirkin break; 223699405162SMichael S. Tsirkin 2237c8d68e6bSJason Wang ret = tun_attach_filter(tun); 223899405162SMichael S. Tsirkin break; 223999405162SMichael S. Tsirkin 224099405162SMichael S. Tsirkin case TUNDETACHFILTER: 224199405162SMichael S. Tsirkin /* Can be set only for TAPs */ 224299405162SMichael S. Tsirkin ret = -EINVAL; 224340630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 224499405162SMichael S. Tsirkin break; 2245c8d68e6bSJason Wang ret = 0; 2246c8d68e6bSJason Wang tun_detach_filter(tun, tun->numqueues); 224799405162SMichael S. Tsirkin break; 224899405162SMichael S. Tsirkin 224976975e9cSPavel Emelyanov case TUNGETFILTER: 225076975e9cSPavel Emelyanov ret = -EINVAL; 225140630b82SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 225276975e9cSPavel Emelyanov break; 225376975e9cSPavel Emelyanov ret = -EFAULT; 225476975e9cSPavel Emelyanov if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog))) 225576975e9cSPavel Emelyanov break; 225676975e9cSPavel Emelyanov ret = 0; 225776975e9cSPavel Emelyanov break; 225876975e9cSPavel Emelyanov 22591da177e4SLinus Torvalds default: 2260631ab46bSEric W. Biederman ret = -EINVAL; 2261631ab46bSEric W. Biederman break; 2262ee289b64SJoe Perches } 22631da177e4SLinus Torvalds 2264876bfd4dSHerbert Xu unlock: 2265876bfd4dSHerbert Xu rtnl_unlock(); 2266876bfd4dSHerbert Xu if (tun) 2267631ab46bSEric W. Biederman tun_put(tun); 2268631ab46bSEric W. Biederman return ret; 22691da177e4SLinus Torvalds } 22701da177e4SLinus Torvalds 227150857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file, 227250857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 227350857e2aSArnd Bergmann { 227450857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq)); 227550857e2aSArnd Bergmann } 227650857e2aSArnd Bergmann 227750857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 227850857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file, 227950857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 228050857e2aSArnd Bergmann { 228150857e2aSArnd Bergmann switch (cmd) { 228250857e2aSArnd Bergmann case TUNSETIFF: 228350857e2aSArnd Bergmann case TUNGETIFF: 228450857e2aSArnd Bergmann case TUNSETTXFILTER: 228550857e2aSArnd Bergmann case TUNGETSNDBUF: 228650857e2aSArnd Bergmann case TUNSETSNDBUF: 228750857e2aSArnd Bergmann case SIOCGIFHWADDR: 228850857e2aSArnd Bergmann case SIOCSIFHWADDR: 228950857e2aSArnd Bergmann arg = (unsigned long)compat_ptr(arg); 229050857e2aSArnd Bergmann break; 229150857e2aSArnd Bergmann default: 229250857e2aSArnd Bergmann arg = (compat_ulong_t)arg; 229350857e2aSArnd Bergmann break; 229450857e2aSArnd Bergmann } 229550857e2aSArnd Bergmann 229650857e2aSArnd Bergmann /* 229750857e2aSArnd Bergmann * compat_ifreq is shorter than ifreq, so we must not access beyond 229850857e2aSArnd Bergmann * the end of that structure. All fields that are used in this 229950857e2aSArnd Bergmann * driver are compatible though, we don't need to convert the 230050857e2aSArnd Bergmann * contents. 230150857e2aSArnd Bergmann */ 230250857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq)); 230350857e2aSArnd Bergmann } 230450857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */ 230550857e2aSArnd Bergmann 23061da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on) 23071da177e4SLinus Torvalds { 230854f968d6SJason Wang struct tun_file *tfile = file->private_data; 23091da177e4SLinus Torvalds int ret; 23101da177e4SLinus Torvalds 231154f968d6SJason Wang if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) 23129d319522SJonathan Corbet goto out; 23131da177e4SLinus Torvalds 23141da177e4SLinus Torvalds if (on) { 2315e0b93eddSJeff Layton __f_setown(file, task_pid(current), PIDTYPE_PID, 0); 231654f968d6SJason Wang tfile->flags |= TUN_FASYNC; 23171da177e4SLinus Torvalds } else 231854f968d6SJason Wang tfile->flags &= ~TUN_FASYNC; 23199d319522SJonathan Corbet ret = 0; 23209d319522SJonathan Corbet out: 23219d319522SJonathan Corbet return ret; 23221da177e4SLinus Torvalds } 23231da177e4SLinus Torvalds 23241da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file) 23251da177e4SLinus Torvalds { 2326140e807dSEric W. Biederman struct net *net = current->nsproxy->net_ns; 2327631ab46bSEric W. Biederman struct tun_file *tfile; 2328deed49fbSThomas Gleixner 23296b8a66eeSJoe Perches DBG1(KERN_INFO, "tunX: tun_chr_open\n"); 2330631ab46bSEric W. Biederman 2331140e807dSEric W. Biederman tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, 233211aa9c28SEric W. Biederman &tun_proto, 0); 2333631ab46bSEric W. Biederman if (!tfile) 2334631ab46bSEric W. Biederman return -ENOMEM; 2335c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 233654f968d6SJason Wang tfile->flags = 0; 2337fb7589a1SPavel Emelyanov tfile->ifindex = 0; 233854f968d6SJason Wang 233954f968d6SJason Wang init_waitqueue_head(&tfile->wq.wait); 23409e641bdcSXi Wang RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq); 234154f968d6SJason Wang 234254f968d6SJason Wang tfile->socket.file = file; 234354f968d6SJason Wang tfile->socket.ops = &tun_socket_ops; 234454f968d6SJason Wang 234554f968d6SJason Wang sock_init_data(&tfile->socket, &tfile->sk); 234654f968d6SJason Wang 234754f968d6SJason Wang tfile->sk.sk_write_space = tun_sock_write_space; 234854f968d6SJason Wang tfile->sk.sk_sndbuf = INT_MAX; 234954f968d6SJason Wang 2350631ab46bSEric W. Biederman file->private_data = tfile; 23514008e97fSJason Wang INIT_LIST_HEAD(&tfile->next); 235254f968d6SJason Wang 235319a6afb2SJason Wang sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); 235419a6afb2SJason Wang 23551da177e4SLinus Torvalds return 0; 23561da177e4SLinus Torvalds } 23571da177e4SLinus Torvalds 23581da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file) 23591da177e4SLinus Torvalds { 2360631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 23611da177e4SLinus Torvalds 2362c8d68e6bSJason Wang tun_detach(tfile, true); 23631da177e4SLinus Torvalds 23641da177e4SLinus Torvalds return 0; 23651da177e4SLinus Torvalds } 23661da177e4SLinus Torvalds 236793e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS 2368a3816ab0SJoe Perches static void tun_chr_show_fdinfo(struct seq_file *m, struct file *f) 236993e14b6dSMasatake YAMATO { 237093e14b6dSMasatake YAMATO struct tun_struct *tun; 237193e14b6dSMasatake YAMATO struct ifreq ifr; 237293e14b6dSMasatake YAMATO 237393e14b6dSMasatake YAMATO memset(&ifr, 0, sizeof(ifr)); 237493e14b6dSMasatake YAMATO 237593e14b6dSMasatake YAMATO rtnl_lock(); 237693e14b6dSMasatake YAMATO tun = tun_get(f); 237793e14b6dSMasatake YAMATO if (tun) 237893e14b6dSMasatake YAMATO tun_get_iff(current->nsproxy->net_ns, tun, &ifr); 237993e14b6dSMasatake YAMATO rtnl_unlock(); 238093e14b6dSMasatake YAMATO 238193e14b6dSMasatake YAMATO if (tun) 238293e14b6dSMasatake YAMATO tun_put(tun); 238393e14b6dSMasatake YAMATO 2384a3816ab0SJoe Perches seq_printf(m, "iff:\t%s\n", ifr.ifr_name); 238593e14b6dSMasatake YAMATO } 238693e14b6dSMasatake YAMATO #endif 238793e14b6dSMasatake YAMATO 2388d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = { 23891da177e4SLinus Torvalds .owner = THIS_MODULE, 23901da177e4SLinus Torvalds .llseek = no_llseek, 23919b067034SAl Viro .read_iter = tun_chr_read_iter, 2392f5ff53b4SAl Viro .write_iter = tun_chr_write_iter, 23931da177e4SLinus Torvalds .poll = tun_chr_poll, 2394876bfd4dSHerbert Xu .unlocked_ioctl = tun_chr_ioctl, 239550857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 239650857e2aSArnd Bergmann .compat_ioctl = tun_chr_compat_ioctl, 239750857e2aSArnd Bergmann #endif 23981da177e4SLinus Torvalds .open = tun_chr_open, 23991da177e4SLinus Torvalds .release = tun_chr_close, 240093e14b6dSMasatake YAMATO .fasync = tun_chr_fasync, 240193e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS 240293e14b6dSMasatake YAMATO .show_fdinfo = tun_chr_show_fdinfo, 240393e14b6dSMasatake YAMATO #endif 24041da177e4SLinus Torvalds }; 24051da177e4SLinus Torvalds 24061da177e4SLinus Torvalds static struct miscdevice tun_miscdev = { 24071da177e4SLinus Torvalds .minor = TUN_MINOR, 24081da177e4SLinus Torvalds .name = "tun", 2409e454cea2SKay Sievers .nodename = "net/tun", 24101da177e4SLinus Torvalds .fops = &tun_fops, 24111da177e4SLinus Torvalds }; 24121da177e4SLinus Torvalds 24131da177e4SLinus Torvalds /* ethtool interface */ 24141da177e4SLinus Torvalds 24151da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 24161da177e4SLinus Torvalds { 24171da177e4SLinus Torvalds cmd->supported = 0; 24181da177e4SLinus Torvalds cmd->advertising = 0; 241970739497SDavid Decotigny ethtool_cmd_speed_set(cmd, SPEED_10); 24201da177e4SLinus Torvalds cmd->duplex = DUPLEX_FULL; 24211da177e4SLinus Torvalds cmd->port = PORT_TP; 24221da177e4SLinus Torvalds cmd->phy_address = 0; 24231da177e4SLinus Torvalds cmd->transceiver = XCVR_INTERNAL; 24241da177e4SLinus Torvalds cmd->autoneg = AUTONEG_DISABLE; 24251da177e4SLinus Torvalds cmd->maxtxpkt = 0; 24261da177e4SLinus Torvalds cmd->maxrxpkt = 0; 24271da177e4SLinus Torvalds return 0; 24281da177e4SLinus Torvalds } 24291da177e4SLinus Torvalds 24301da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) 24311da177e4SLinus Torvalds { 24321da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 24331da177e4SLinus Torvalds 243433a5ba14SRick Jones strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); 243533a5ba14SRick Jones strlcpy(info->version, DRV_VERSION, sizeof(info->version)); 24361da177e4SLinus Torvalds 24371da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 243840630b82SMichael S. Tsirkin case IFF_TUN: 243933a5ba14SRick Jones strlcpy(info->bus_info, "tun", sizeof(info->bus_info)); 24401da177e4SLinus Torvalds break; 244140630b82SMichael S. Tsirkin case IFF_TAP: 244233a5ba14SRick Jones strlcpy(info->bus_info, "tap", sizeof(info->bus_info)); 24431da177e4SLinus Torvalds break; 24441da177e4SLinus Torvalds } 24451da177e4SLinus Torvalds } 24461da177e4SLinus Torvalds 24471da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev) 24481da177e4SLinus Torvalds { 24491da177e4SLinus Torvalds #ifdef TUN_DEBUG 24501da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 24511da177e4SLinus Torvalds return tun->debug; 24521da177e4SLinus Torvalds #else 24531da177e4SLinus Torvalds return -EOPNOTSUPP; 24541da177e4SLinus Torvalds #endif 24551da177e4SLinus Torvalds } 24561da177e4SLinus Torvalds 24571da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value) 24581da177e4SLinus Torvalds { 24591da177e4SLinus Torvalds #ifdef TUN_DEBUG 24601da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 24611da177e4SLinus Torvalds tun->debug = value; 24621da177e4SLinus Torvalds #endif 24631da177e4SLinus Torvalds } 24641da177e4SLinus Torvalds 24657282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = { 24661da177e4SLinus Torvalds .get_settings = tun_get_settings, 24671da177e4SLinus Torvalds .get_drvinfo = tun_get_drvinfo, 24681da177e4SLinus Torvalds .get_msglevel = tun_get_msglevel, 24691da177e4SLinus Torvalds .set_msglevel = tun_set_msglevel, 2470bee31369SNolan Leake .get_link = ethtool_op_get_link, 2471eda29772SRichard Cochran .get_ts_info = ethtool_op_get_ts_info, 24721da177e4SLinus Torvalds }; 24731da177e4SLinus Torvalds 24741576d986SJason Wang static int tun_queue_resize(struct tun_struct *tun) 24751576d986SJason Wang { 24761576d986SJason Wang struct net_device *dev = tun->dev; 24771576d986SJason Wang struct tun_file *tfile; 24781576d986SJason Wang struct skb_array **arrays; 24791576d986SJason Wang int n = tun->numqueues + tun->numdisabled; 24801576d986SJason Wang int ret, i; 24811576d986SJason Wang 24821576d986SJason Wang arrays = kmalloc(sizeof *arrays * n, GFP_KERNEL); 24831576d986SJason Wang if (!arrays) 24841576d986SJason Wang return -ENOMEM; 24851576d986SJason Wang 24861576d986SJason Wang for (i = 0; i < tun->numqueues; i++) { 24871576d986SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 24881576d986SJason Wang arrays[i] = &tfile->tx_array; 24891576d986SJason Wang } 24901576d986SJason Wang list_for_each_entry(tfile, &tun->disabled, next) 24911576d986SJason Wang arrays[i++] = &tfile->tx_array; 24921576d986SJason Wang 24931576d986SJason Wang ret = skb_array_resize_multiple(arrays, n, 24941576d986SJason Wang dev->tx_queue_len, GFP_KERNEL); 24951576d986SJason Wang 24961576d986SJason Wang kfree(arrays); 24971576d986SJason Wang return ret; 24981576d986SJason Wang } 24991576d986SJason Wang 25001576d986SJason Wang static int tun_device_event(struct notifier_block *unused, 25011576d986SJason Wang unsigned long event, void *ptr) 25021576d986SJason Wang { 25031576d986SJason Wang struct net_device *dev = netdev_notifier_info_to_dev(ptr); 25041576d986SJason Wang struct tun_struct *tun = netdev_priv(dev); 25051576d986SJason Wang 25061576d986SJason Wang switch (event) { 25071576d986SJason Wang case NETDEV_CHANGE_TX_QUEUE_LEN: 25081576d986SJason Wang if (tun_queue_resize(tun)) 25091576d986SJason Wang return NOTIFY_BAD; 25101576d986SJason Wang break; 25111576d986SJason Wang default: 25121576d986SJason Wang break; 25131576d986SJason Wang } 25141576d986SJason Wang 25151576d986SJason Wang return NOTIFY_DONE; 25161576d986SJason Wang } 25171576d986SJason Wang 25181576d986SJason Wang static struct notifier_block tun_notifier_block __read_mostly = { 25191576d986SJason Wang .notifier_call = tun_device_event, 25201576d986SJason Wang }; 252179d17604SPavel Emelyanov 25221da177e4SLinus Torvalds static int __init tun_init(void) 25231da177e4SLinus Torvalds { 25241da177e4SLinus Torvalds int ret = 0; 25251da177e4SLinus Torvalds 25266b8a66eeSJoe Perches pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION); 25276b8a66eeSJoe Perches pr_info("%s\n", DRV_COPYRIGHT); 25281da177e4SLinus Torvalds 2529f019a7a5SEric W. Biederman ret = rtnl_link_register(&tun_link_ops); 253079d17604SPavel Emelyanov if (ret) { 25316b8a66eeSJoe Perches pr_err("Can't register link_ops\n"); 2532f019a7a5SEric W. Biederman goto err_linkops; 253379d17604SPavel Emelyanov } 253479d17604SPavel Emelyanov 25351da177e4SLinus Torvalds ret = misc_register(&tun_miscdev); 253679d17604SPavel Emelyanov if (ret) { 25376b8a66eeSJoe Perches pr_err("Can't register misc device %d\n", TUN_MINOR); 253879d17604SPavel Emelyanov goto err_misc; 253979d17604SPavel Emelyanov } 25401576d986SJason Wang 25411576d986SJason Wang register_netdevice_notifier(&tun_notifier_block); 254279d17604SPavel Emelyanov return 0; 254379d17604SPavel Emelyanov err_misc: 2544f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 2545f019a7a5SEric W. Biederman err_linkops: 25461da177e4SLinus Torvalds return ret; 25471da177e4SLinus Torvalds } 25481da177e4SLinus Torvalds 25491da177e4SLinus Torvalds static void tun_cleanup(void) 25501da177e4SLinus Torvalds { 25511da177e4SLinus Torvalds misc_deregister(&tun_miscdev); 2552f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 25531576d986SJason Wang unregister_netdevice_notifier(&tun_notifier_block); 25541da177e4SLinus Torvalds } 25551da177e4SLinus Torvalds 255605c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file. Returns error unless file is 255705c2828cSMichael S. Tsirkin * attached to a device. The returned object works like a packet socket, it 255805c2828cSMichael S. Tsirkin * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for 255905c2828cSMichael S. Tsirkin * holding a reference to the file for as long as the socket is in use. */ 256005c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file) 256105c2828cSMichael S. Tsirkin { 25626e914fc7SJason Wang struct tun_file *tfile; 256305c2828cSMichael S. Tsirkin if (file->f_op != &tun_fops) 256405c2828cSMichael S. Tsirkin return ERR_PTR(-EINVAL); 25656e914fc7SJason Wang tfile = file->private_data; 25666e914fc7SJason Wang if (!tfile) 256705c2828cSMichael S. Tsirkin return ERR_PTR(-EBADFD); 256854f968d6SJason Wang return &tfile->socket; 256905c2828cSMichael S. Tsirkin } 257005c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket); 257105c2828cSMichael S. Tsirkin 25721da177e4SLinus Torvalds module_init(tun_init); 25731da177e4SLinus Torvalds module_exit(tun_cleanup); 25741da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION); 25751da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT); 25761da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 25771da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR); 2578578454ffSKay Sievers MODULE_ALIAS("devname:net/tun"); 2579