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> 685188cd44SBen Hutchings #include <net/ipv6.h> 69881d966bSEric W. Biederman #include <net/net_namespace.h> 7079d17604SPavel Emelyanov #include <net/netns/generic.h> 71f019a7a5SEric W. Biederman #include <net/rtnetlink.h> 7233dccbb0SHerbert Xu #include <net/sock.h> 7393e14b6dSMasatake YAMATO #include <linux/seq_file.h> 74e0b46d0eSHerbert Xu #include <linux/uio.h> 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 1070690899bSMichael S. Tsirkin #define GOODCOPY_LEN 128 1080690899bSMichael S. Tsirkin 109f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8 110f271b2ccSMax Krasnyansky struct tap_filter { 111f271b2ccSMax Krasnyansky unsigned int count; /* Number of addrs. Zero means disabled */ 112f271b2ccSMax Krasnyansky u32 mask[2]; /* Mask of the hashed addrs */ 113f271b2ccSMax Krasnyansky unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN]; 114f271b2ccSMax Krasnyansky }; 115f271b2ccSMax Krasnyansky 11692d4ea6eSstephen hemminger /* DEFAULT_MAX_NUM_RSS_QUEUES were chosen to let the rx/tx queues allocated for 117edfb6a14SJason Wang * the netdevice to be fit in one page. So we can make sure the success of 118edfb6a14SJason Wang * memory allocation. TODO: increase the limit. */ 119edfb6a14SJason Wang #define MAX_TAP_QUEUES DEFAULT_MAX_NUM_RSS_QUEUES 120b8732fb7SJason Wang #define MAX_TAP_FLOWS 4096 121c8d68e6bSJason Wang 12296442e42SJason Wang #define TUN_FLOW_EXPIRE (3 * HZ) 12396442e42SJason Wang 12454f968d6SJason Wang /* A tun_file connects an open character device to a tuntap netdevice. It 12592d4ea6eSstephen hemminger * also contains all socket related structures (except sock_fprog and tap_filter) 12654f968d6SJason Wang * to serve as one transmit queue for tuntap device. The sock_fprog and 12754f968d6SJason Wang * tap_filter were kept in tun_struct since they were used for filtering for the 12836fe8c09SRami Rosen * netdevice not for a specific queue (at least I didn't see the requirement for 12954f968d6SJason Wang * this). 1306e914fc7SJason Wang * 1316e914fc7SJason Wang * RCU usage: 13236fe8c09SRami Rosen * The tun_file and tun_struct are loosely coupled, the pointer from one to the 1336e914fc7SJason Wang * other can only be read while rcu_read_lock or rtnl_lock is held. 13454f968d6SJason Wang */ 135631ab46bSEric W. Biederman struct tun_file { 13654f968d6SJason Wang struct sock sk; 13754f968d6SJason Wang struct socket socket; 13854f968d6SJason Wang struct socket_wq wq; 1396e914fc7SJason Wang struct tun_struct __rcu *tun; 14036b50babSEric W. Biederman struct net *net; 14154f968d6SJason Wang struct fasync_struct *fasync; 14254f968d6SJason Wang /* only used for fasnyc */ 14354f968d6SJason Wang unsigned int flags; 144fb7589a1SPavel Emelyanov union { 145c8d68e6bSJason Wang u16 queue_index; 146fb7589a1SPavel Emelyanov unsigned int ifindex; 147fb7589a1SPavel Emelyanov }; 1484008e97fSJason Wang struct list_head next; 1494008e97fSJason Wang struct tun_struct *detached; 150631ab46bSEric W. Biederman }; 151631ab46bSEric W. Biederman 15296442e42SJason Wang struct tun_flow_entry { 15396442e42SJason Wang struct hlist_node hash_link; 15496442e42SJason Wang struct rcu_head rcu; 15596442e42SJason Wang struct tun_struct *tun; 15696442e42SJason Wang 15796442e42SJason Wang u32 rxhash; 1589bc88939STom Herbert u32 rps_rxhash; 15996442e42SJason Wang int queue_index; 16096442e42SJason Wang unsigned long updated; 16196442e42SJason Wang }; 16296442e42SJason Wang 16396442e42SJason Wang #define TUN_NUM_FLOW_ENTRIES 1024 16496442e42SJason Wang 16554f968d6SJason Wang /* Since the socket were moved to tun_file, to preserve the behavior of persist 16636fe8c09SRami Rosen * device, socket filter, sndbuf and vnet header size were restore when the 16754f968d6SJason Wang * file were attached to a persist device. 16854f968d6SJason Wang */ 16914daa021SRusty Russell struct tun_struct { 170c8d68e6bSJason Wang struct tun_file __rcu *tfiles[MAX_TAP_QUEUES]; 171c8d68e6bSJason Wang unsigned int numqueues; 172f271b2ccSMax Krasnyansky unsigned int flags; 1730625c883SEric W. Biederman kuid_t owner; 1740625c883SEric W. Biederman kgid_t group; 17514daa021SRusty Russell 17614daa021SRusty Russell struct net_device *dev; 177c8f44affSMichał Mirosław netdev_features_t set_features; 17888255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \ 1793d0ad094SBen Hutchings NETIF_F_TSO6) 180d9d52b51SMichael S. Tsirkin 181d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 18254f968d6SJason Wang int sndbuf; 18354f968d6SJason Wang struct tap_filter txflt; 18454f968d6SJason Wang struct sock_fprog fprog; 18554f968d6SJason Wang /* protected by rtnl lock */ 18654f968d6SJason Wang bool filter_attached; 18714daa021SRusty Russell #ifdef TUN_DEBUG 18814daa021SRusty Russell int debug; 18914daa021SRusty Russell #endif 19096442e42SJason Wang spinlock_t lock; 19196442e42SJason Wang struct hlist_head flows[TUN_NUM_FLOW_ENTRIES]; 19296442e42SJason Wang struct timer_list flow_gc_timer; 19396442e42SJason Wang unsigned long ageing_time; 1944008e97fSJason Wang unsigned int numdisabled; 1954008e97fSJason Wang struct list_head disabled; 1965dbbaf2dSPaul Moore void *security; 197b8732fb7SJason Wang u32 flow_count; 19814daa021SRusty Russell }; 19914daa021SRusty Russell 20096442e42SJason Wang static inline u32 tun_hashfn(u32 rxhash) 20196442e42SJason Wang { 20296442e42SJason Wang return rxhash & 0x3ff; 20396442e42SJason Wang } 20496442e42SJason Wang 20596442e42SJason Wang static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash) 20696442e42SJason Wang { 20796442e42SJason Wang struct tun_flow_entry *e; 20896442e42SJason Wang 209b67bfe0dSSasha Levin hlist_for_each_entry_rcu(e, head, hash_link) { 21096442e42SJason Wang if (e->rxhash == rxhash) 21196442e42SJason Wang return e; 21296442e42SJason Wang } 21396442e42SJason Wang return NULL; 21496442e42SJason Wang } 21596442e42SJason Wang 21696442e42SJason Wang static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun, 21796442e42SJason Wang struct hlist_head *head, 21896442e42SJason Wang u32 rxhash, u16 queue_index) 21996442e42SJason Wang { 2209fdc6befSEric Dumazet struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC); 2219fdc6befSEric Dumazet 22296442e42SJason Wang if (e) { 22396442e42SJason Wang tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n", 22496442e42SJason Wang rxhash, queue_index); 22596442e42SJason Wang e->updated = jiffies; 22696442e42SJason Wang e->rxhash = rxhash; 2279bc88939STom Herbert e->rps_rxhash = 0; 22896442e42SJason Wang e->queue_index = queue_index; 22996442e42SJason Wang e->tun = tun; 23096442e42SJason Wang hlist_add_head_rcu(&e->hash_link, head); 231b8732fb7SJason Wang ++tun->flow_count; 23296442e42SJason Wang } 23396442e42SJason Wang return e; 23496442e42SJason Wang } 23596442e42SJason Wang 23696442e42SJason Wang static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e) 23796442e42SJason Wang { 23896442e42SJason Wang tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n", 23996442e42SJason Wang e->rxhash, e->queue_index); 2409bc88939STom Herbert sock_rps_reset_flow_hash(e->rps_rxhash); 24196442e42SJason Wang hlist_del_rcu(&e->hash_link); 2429fdc6befSEric Dumazet kfree_rcu(e, rcu); 243b8732fb7SJason Wang --tun->flow_count; 24496442e42SJason Wang } 24596442e42SJason Wang 24696442e42SJason Wang static void tun_flow_flush(struct tun_struct *tun) 24796442e42SJason Wang { 24896442e42SJason Wang int i; 24996442e42SJason Wang 25096442e42SJason Wang spin_lock_bh(&tun->lock); 25196442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 25296442e42SJason Wang struct tun_flow_entry *e; 253b67bfe0dSSasha Levin struct hlist_node *n; 25496442e42SJason Wang 255b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) 25696442e42SJason Wang tun_flow_delete(tun, e); 25796442e42SJason Wang } 25896442e42SJason Wang spin_unlock_bh(&tun->lock); 25996442e42SJason Wang } 26096442e42SJason Wang 26196442e42SJason Wang static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index) 26296442e42SJason Wang { 26396442e42SJason Wang int i; 26496442e42SJason Wang 26596442e42SJason Wang spin_lock_bh(&tun->lock); 26696442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 26796442e42SJason Wang struct tun_flow_entry *e; 268b67bfe0dSSasha Levin struct hlist_node *n; 26996442e42SJason Wang 270b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { 27196442e42SJason Wang if (e->queue_index == queue_index) 27296442e42SJason Wang tun_flow_delete(tun, e); 27396442e42SJason Wang } 27496442e42SJason Wang } 27596442e42SJason Wang spin_unlock_bh(&tun->lock); 27696442e42SJason Wang } 27796442e42SJason Wang 27896442e42SJason Wang static void tun_flow_cleanup(unsigned long data) 27996442e42SJason Wang { 28096442e42SJason Wang struct tun_struct *tun = (struct tun_struct *)data; 28196442e42SJason Wang unsigned long delay = tun->ageing_time; 28296442e42SJason Wang unsigned long next_timer = jiffies + delay; 28396442e42SJason Wang unsigned long count = 0; 28496442e42SJason Wang int i; 28596442e42SJason Wang 28696442e42SJason Wang tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n"); 28796442e42SJason Wang 28896442e42SJason Wang spin_lock_bh(&tun->lock); 28996442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 29096442e42SJason Wang struct tun_flow_entry *e; 291b67bfe0dSSasha Levin struct hlist_node *n; 29296442e42SJason Wang 293b67bfe0dSSasha Levin hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { 29496442e42SJason Wang unsigned long this_timer; 29596442e42SJason Wang count++; 29696442e42SJason Wang this_timer = e->updated + delay; 29796442e42SJason Wang if (time_before_eq(this_timer, jiffies)) 29896442e42SJason Wang tun_flow_delete(tun, e); 29996442e42SJason Wang else if (time_before(this_timer, next_timer)) 30096442e42SJason Wang next_timer = this_timer; 30196442e42SJason Wang } 30296442e42SJason Wang } 30396442e42SJason Wang 30496442e42SJason Wang if (count) 30596442e42SJason Wang mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer)); 30696442e42SJason Wang spin_unlock_bh(&tun->lock); 30796442e42SJason Wang } 30896442e42SJason Wang 30949974420SEric Dumazet static void tun_flow_update(struct tun_struct *tun, u32 rxhash, 3109e85722dSJason Wang struct tun_file *tfile) 31196442e42SJason Wang { 31296442e42SJason Wang struct hlist_head *head; 31396442e42SJason Wang struct tun_flow_entry *e; 31496442e42SJason Wang unsigned long delay = tun->ageing_time; 3159e85722dSJason Wang u16 queue_index = tfile->queue_index; 31696442e42SJason Wang 31796442e42SJason Wang if (!rxhash) 31896442e42SJason Wang return; 31996442e42SJason Wang else 32096442e42SJason Wang head = &tun->flows[tun_hashfn(rxhash)]; 32196442e42SJason Wang 32296442e42SJason Wang rcu_read_lock(); 32396442e42SJason Wang 3249e85722dSJason Wang /* We may get a very small possibility of OOO during switching, not 3259e85722dSJason Wang * worth to optimize.*/ 3269e85722dSJason Wang if (tun->numqueues == 1 || tfile->detached) 32796442e42SJason Wang goto unlock; 32896442e42SJason Wang 32996442e42SJason Wang e = tun_flow_find(head, rxhash); 33096442e42SJason Wang if (likely(e)) { 33196442e42SJason Wang /* TODO: keep queueing to old queue until it's empty? */ 33296442e42SJason Wang e->queue_index = queue_index; 33396442e42SJason Wang e->updated = jiffies; 3349bc88939STom Herbert sock_rps_record_flow_hash(e->rps_rxhash); 33596442e42SJason Wang } else { 33696442e42SJason Wang spin_lock_bh(&tun->lock); 337b8732fb7SJason Wang if (!tun_flow_find(head, rxhash) && 338b8732fb7SJason Wang tun->flow_count < MAX_TAP_FLOWS) 33996442e42SJason Wang tun_flow_create(tun, head, rxhash, queue_index); 34096442e42SJason Wang 34196442e42SJason Wang if (!timer_pending(&tun->flow_gc_timer)) 34296442e42SJason Wang mod_timer(&tun->flow_gc_timer, 34396442e42SJason Wang round_jiffies_up(jiffies + delay)); 34496442e42SJason Wang spin_unlock_bh(&tun->lock); 34596442e42SJason Wang } 34696442e42SJason Wang 34796442e42SJason Wang unlock: 34896442e42SJason Wang rcu_read_unlock(); 34996442e42SJason Wang } 35096442e42SJason Wang 3519bc88939STom Herbert /** 3529bc88939STom Herbert * Save the hash received in the stack receive path and update the 3539bc88939STom Herbert * flow_hash table accordingly. 3549bc88939STom Herbert */ 3559bc88939STom Herbert static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash) 3569bc88939STom Herbert { 3579bc88939STom Herbert if (unlikely(e->rps_rxhash != hash)) { 3589bc88939STom Herbert sock_rps_reset_flow_hash(e->rps_rxhash); 3599bc88939STom Herbert e->rps_rxhash = hash; 3609bc88939STom Herbert } 3619bc88939STom Herbert } 3629bc88939STom Herbert 363c8d68e6bSJason Wang /* We try to identify a flow through its rxhash first. The reason that 36492d4ea6eSstephen hemminger * we do not check rxq no. is because some cards(e.g 82599), chooses 365c8d68e6bSJason Wang * the rxq based on the txq where the last packet of the flow comes. As 366c8d68e6bSJason Wang * the userspace application move between processors, we may get a 367c8d68e6bSJason Wang * different rxq no. here. If we could not get rxhash, then we would 368c8d68e6bSJason Wang * hope the rxq no. may help here. 369c8d68e6bSJason Wang */ 370f663dd9aSJason Wang static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb, 37199932d4fSDaniel Borkmann void *accel_priv, select_queue_fallback_t fallback) 372c8d68e6bSJason Wang { 373c8d68e6bSJason Wang struct tun_struct *tun = netdev_priv(dev); 37496442e42SJason Wang struct tun_flow_entry *e; 375c8d68e6bSJason Wang u32 txq = 0; 376c8d68e6bSJason Wang u32 numqueues = 0; 377c8d68e6bSJason Wang 378c8d68e6bSJason Wang rcu_read_lock(); 37992bb73eaSJason Wang numqueues = ACCESS_ONCE(tun->numqueues); 380c8d68e6bSJason Wang 3813958afa1STom Herbert txq = skb_get_hash(skb); 382c8d68e6bSJason Wang if (txq) { 38396442e42SJason Wang e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq); 3849bc88939STom Herbert if (e) { 3859bc88939STom Herbert tun_flow_save_rps_rxhash(e, txq); 386fbe4d456SZhi Yong Wu txq = e->queue_index; 3879bc88939STom Herbert } else 388c8d68e6bSJason Wang /* use multiply and shift instead of expensive divide */ 389c8d68e6bSJason Wang txq = ((u64)txq * numqueues) >> 32; 390c8d68e6bSJason Wang } else if (likely(skb_rx_queue_recorded(skb))) { 391c8d68e6bSJason Wang txq = skb_get_rx_queue(skb); 392c8d68e6bSJason Wang while (unlikely(txq >= numqueues)) 393c8d68e6bSJason Wang txq -= numqueues; 394c8d68e6bSJason Wang } 395c8d68e6bSJason Wang 396c8d68e6bSJason Wang rcu_read_unlock(); 397c8d68e6bSJason Wang return txq; 398c8d68e6bSJason Wang } 399c8d68e6bSJason Wang 400cde8b15fSJason Wang static inline bool tun_not_capable(struct tun_struct *tun) 401cde8b15fSJason Wang { 402cde8b15fSJason Wang const struct cred *cred = current_cred(); 403c260b772SEric W. Biederman struct net *net = dev_net(tun->dev); 404cde8b15fSJason Wang 405cde8b15fSJason Wang return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) || 406cde8b15fSJason Wang (gid_valid(tun->group) && !in_egroup_p(tun->group))) && 407c260b772SEric W. Biederman !ns_capable(net->user_ns, CAP_NET_ADMIN); 408cde8b15fSJason Wang } 409cde8b15fSJason Wang 410c8d68e6bSJason Wang static void tun_set_real_num_queues(struct tun_struct *tun) 411c8d68e6bSJason Wang { 412c8d68e6bSJason Wang netif_set_real_num_tx_queues(tun->dev, tun->numqueues); 413c8d68e6bSJason Wang netif_set_real_num_rx_queues(tun->dev, tun->numqueues); 414c8d68e6bSJason Wang } 415c8d68e6bSJason Wang 4164008e97fSJason Wang static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) 4174008e97fSJason Wang { 4184008e97fSJason Wang tfile->detached = tun; 4194008e97fSJason Wang list_add_tail(&tfile->next, &tun->disabled); 4204008e97fSJason Wang ++tun->numdisabled; 4214008e97fSJason Wang } 4224008e97fSJason Wang 423d32649d1SJason Wang static struct tun_struct *tun_enable_queue(struct tun_file *tfile) 4244008e97fSJason Wang { 4254008e97fSJason Wang struct tun_struct *tun = tfile->detached; 4264008e97fSJason Wang 4274008e97fSJason Wang tfile->detached = NULL; 4284008e97fSJason Wang list_del_init(&tfile->next); 4294008e97fSJason Wang --tun->numdisabled; 4304008e97fSJason Wang return tun; 4314008e97fSJason Wang } 4324008e97fSJason Wang 4334bfb0513SJason Wang static void tun_queue_purge(struct tun_file *tfile) 4344bfb0513SJason Wang { 4354bfb0513SJason Wang skb_queue_purge(&tfile->sk.sk_receive_queue); 4364bfb0513SJason Wang skb_queue_purge(&tfile->sk.sk_error_queue); 4374bfb0513SJason Wang } 4384bfb0513SJason Wang 439c8d68e6bSJason Wang static void __tun_detach(struct tun_file *tfile, bool clean) 440c8d68e6bSJason Wang { 441c8d68e6bSJason Wang struct tun_file *ntfile; 442c8d68e6bSJason Wang struct tun_struct *tun; 443c8d68e6bSJason Wang 444b8deabd3SJason Wang tun = rtnl_dereference(tfile->tun); 445b8deabd3SJason Wang 4469e85722dSJason Wang if (tun && !tfile->detached) { 447c8d68e6bSJason Wang u16 index = tfile->queue_index; 448c8d68e6bSJason Wang BUG_ON(index >= tun->numqueues); 449c8d68e6bSJason Wang 450c8d68e6bSJason Wang rcu_assign_pointer(tun->tfiles[index], 451c8d68e6bSJason Wang tun->tfiles[tun->numqueues - 1]); 452b8deabd3SJason Wang ntfile = rtnl_dereference(tun->tfiles[index]); 453c8d68e6bSJason Wang ntfile->queue_index = index; 454c8d68e6bSJason Wang 455c8d68e6bSJason Wang --tun->numqueues; 4569e85722dSJason Wang if (clean) { 457c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 458c8d68e6bSJason Wang sock_put(&tfile->sk); 4599e85722dSJason Wang } else 4604008e97fSJason Wang tun_disable_queue(tun, tfile); 461c8d68e6bSJason Wang 462c8d68e6bSJason Wang synchronize_net(); 46396442e42SJason Wang tun_flow_delete_by_queue(tun, tun->numqueues + 1); 464c8d68e6bSJason Wang /* Drop read queue */ 4654bfb0513SJason Wang tun_queue_purge(tfile); 466c8d68e6bSJason Wang tun_set_real_num_queues(tun); 467dd38bd85SJason Wang } else if (tfile->detached && clean) { 4684008e97fSJason Wang tun = tun_enable_queue(tfile); 469dd38bd85SJason Wang sock_put(&tfile->sk); 470dd38bd85SJason Wang } 471c8d68e6bSJason Wang 472c8d68e6bSJason Wang if (clean) { 473af668b3cSMichael S. Tsirkin if (tun && tun->numqueues == 0 && tun->numdisabled == 0) { 474af668b3cSMichael S. Tsirkin netif_carrier_off(tun->dev); 475af668b3cSMichael S. Tsirkin 476af668b3cSMichael S. Tsirkin if (!(tun->flags & TUN_PERSIST) && 477af668b3cSMichael S. Tsirkin tun->dev->reg_state == NETREG_REGISTERED) 4784008e97fSJason Wang unregister_netdevice(tun->dev); 479af668b3cSMichael S. Tsirkin } 4804008e97fSJason Wang 481c8d68e6bSJason Wang BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED, 482c8d68e6bSJason Wang &tfile->socket.flags)); 483c8d68e6bSJason Wang sk_release_kernel(&tfile->sk); 484c8d68e6bSJason Wang } 485c8d68e6bSJason Wang } 486c8d68e6bSJason Wang 487c8d68e6bSJason Wang static void tun_detach(struct tun_file *tfile, bool clean) 488c8d68e6bSJason Wang { 489c8d68e6bSJason Wang rtnl_lock(); 490c8d68e6bSJason Wang __tun_detach(tfile, clean); 491c8d68e6bSJason Wang rtnl_unlock(); 492c8d68e6bSJason Wang } 493c8d68e6bSJason Wang 494c8d68e6bSJason Wang static void tun_detach_all(struct net_device *dev) 495c8d68e6bSJason Wang { 496c8d68e6bSJason Wang struct tun_struct *tun = netdev_priv(dev); 4974008e97fSJason Wang struct tun_file *tfile, *tmp; 498c8d68e6bSJason Wang int i, n = tun->numqueues; 499c8d68e6bSJason Wang 500c8d68e6bSJason Wang for (i = 0; i < n; i++) { 501b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 502c8d68e6bSJason Wang BUG_ON(!tfile); 5039e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 504c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 505c8d68e6bSJason Wang --tun->numqueues; 506c8d68e6bSJason Wang } 5079e85722dSJason Wang list_for_each_entry(tfile, &tun->disabled, next) { 5089e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 509c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 5109e85722dSJason Wang } 511c8d68e6bSJason Wang BUG_ON(tun->numqueues != 0); 512c8d68e6bSJason Wang 513c8d68e6bSJason Wang synchronize_net(); 514c8d68e6bSJason Wang for (i = 0; i < n; i++) { 515b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 516c8d68e6bSJason Wang /* Drop read queue */ 5174bfb0513SJason Wang tun_queue_purge(tfile); 518c8d68e6bSJason Wang sock_put(&tfile->sk); 519c8d68e6bSJason Wang } 5204008e97fSJason Wang list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { 5214008e97fSJason Wang tun_enable_queue(tfile); 5224bfb0513SJason Wang tun_queue_purge(tfile); 5234008e97fSJason Wang sock_put(&tfile->sk); 5244008e97fSJason Wang } 5254008e97fSJason Wang BUG_ON(tun->numdisabled != 0); 526dd38bd85SJason Wang 527dd38bd85SJason Wang if (tun->flags & TUN_PERSIST) 528dd38bd85SJason Wang module_put(THIS_MODULE); 529c8d68e6bSJason Wang } 530c8d68e6bSJason Wang 531849c9b6fSPavel Emelyanov static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter) 532a7385ba2SEric W. Biederman { 533631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 53438231b7aSEric W. Biederman int err; 535a7385ba2SEric W. Biederman 5365dbbaf2dSPaul Moore err = security_tun_dev_attach(tfile->socket.sk, tun->security); 5375dbbaf2dSPaul Moore if (err < 0) 5385dbbaf2dSPaul Moore goto out; 5395dbbaf2dSPaul Moore 54038231b7aSEric W. Biederman err = -EINVAL; 5419e85722dSJason Wang if (rtnl_dereference(tfile->tun) && !tfile->detached) 54238231b7aSEric W. Biederman goto out; 54338231b7aSEric W. Biederman 54438231b7aSEric W. Biederman err = -EBUSY; 545c8d68e6bSJason Wang if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1) 546c8d68e6bSJason Wang goto out; 547c8d68e6bSJason Wang 548c8d68e6bSJason Wang err = -E2BIG; 5494008e97fSJason Wang if (!tfile->detached && 5504008e97fSJason Wang tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES) 55138231b7aSEric W. Biederman goto out; 55238231b7aSEric W. Biederman 55338231b7aSEric W. Biederman err = 0; 55454f968d6SJason Wang 55592d4ea6eSstephen hemminger /* Re-attach the filter to persist device */ 556849c9b6fSPavel Emelyanov if (!skip_filter && (tun->filter_attached == true)) { 55754f968d6SJason Wang err = sk_attach_filter(&tun->fprog, tfile->socket.sk); 55854f968d6SJason Wang if (!err) 55954f968d6SJason Wang goto out; 56054f968d6SJason Wang } 561c8d68e6bSJason Wang tfile->queue_index = tun->numqueues; 5626e914fc7SJason Wang rcu_assign_pointer(tfile->tun, tun); 563c8d68e6bSJason Wang rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); 564c8d68e6bSJason Wang tun->numqueues++; 565c8d68e6bSJason Wang 5664008e97fSJason Wang if (tfile->detached) 5674008e97fSJason Wang tun_enable_queue(tfile); 5684008e97fSJason Wang else 5694008e97fSJason Wang sock_hold(&tfile->sk); 5704008e97fSJason Wang 571c8d68e6bSJason Wang tun_set_real_num_queues(tun); 572c8d68e6bSJason Wang 573c8d68e6bSJason Wang /* device is allowed to go away first, so no need to hold extra 574c8d68e6bSJason Wang * refcnt. 575c8d68e6bSJason Wang */ 576a7385ba2SEric W. Biederman 57738231b7aSEric W. Biederman out: 57838231b7aSEric W. Biederman return err; 579a7385ba2SEric W. Biederman } 580a7385ba2SEric W. Biederman 581631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile) 582631ab46bSEric W. Biederman { 5836e914fc7SJason Wang struct tun_struct *tun; 584c70f1829SEric W. Biederman 5856e914fc7SJason Wang rcu_read_lock(); 5866e914fc7SJason Wang tun = rcu_dereference(tfile->tun); 5876e914fc7SJason Wang if (tun) 5886e914fc7SJason Wang dev_hold(tun->dev); 5896e914fc7SJason Wang rcu_read_unlock(); 590c70f1829SEric W. Biederman 591c70f1829SEric W. Biederman return tun; 592631ab46bSEric W. Biederman } 593631ab46bSEric W. Biederman 594631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file) 595631ab46bSEric W. Biederman { 596631ab46bSEric W. Biederman return __tun_get(file->private_data); 597631ab46bSEric W. Biederman } 598631ab46bSEric W. Biederman 599631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun) 600631ab46bSEric W. Biederman { 6016e914fc7SJason Wang dev_put(tun->dev); 602631ab46bSEric W. Biederman } 603631ab46bSEric W. Biederman 6046b8a66eeSJoe Perches /* TAP filtering */ 605f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr) 606f271b2ccSMax Krasnyansky { 607f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 608f271b2ccSMax Krasnyansky mask[n >> 5] |= (1 << (n & 31)); 609f271b2ccSMax Krasnyansky } 610f271b2ccSMax Krasnyansky 611f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) 612f271b2ccSMax Krasnyansky { 613f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 614f271b2ccSMax Krasnyansky return mask[n >> 5] & (1 << (n & 31)); 615f271b2ccSMax Krasnyansky } 616f271b2ccSMax Krasnyansky 617f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg) 618f271b2ccSMax Krasnyansky { 619f271b2ccSMax Krasnyansky struct { u8 u[ETH_ALEN]; } *addr; 620f271b2ccSMax Krasnyansky struct tun_filter uf; 621f271b2ccSMax Krasnyansky int err, alen, n, nexact; 622f271b2ccSMax Krasnyansky 623f271b2ccSMax Krasnyansky if (copy_from_user(&uf, arg, sizeof(uf))) 624f271b2ccSMax Krasnyansky return -EFAULT; 625f271b2ccSMax Krasnyansky 626f271b2ccSMax Krasnyansky if (!uf.count) { 627f271b2ccSMax Krasnyansky /* Disabled */ 628f271b2ccSMax Krasnyansky filter->count = 0; 629f271b2ccSMax Krasnyansky return 0; 630f271b2ccSMax Krasnyansky } 631f271b2ccSMax Krasnyansky 632f271b2ccSMax Krasnyansky alen = ETH_ALEN * uf.count; 633f271b2ccSMax Krasnyansky addr = kmalloc(alen, GFP_KERNEL); 634f271b2ccSMax Krasnyansky if (!addr) 635f271b2ccSMax Krasnyansky return -ENOMEM; 636f271b2ccSMax Krasnyansky 637f271b2ccSMax Krasnyansky if (copy_from_user(addr, arg + sizeof(uf), alen)) { 638f271b2ccSMax Krasnyansky err = -EFAULT; 639f271b2ccSMax Krasnyansky goto done; 640f271b2ccSMax Krasnyansky } 641f271b2ccSMax Krasnyansky 642f271b2ccSMax Krasnyansky /* The filter is updated without holding any locks. Which is 643f271b2ccSMax Krasnyansky * perfectly safe. We disable it first and in the worst 644f271b2ccSMax Krasnyansky * case we'll accept a few undesired packets. */ 645f271b2ccSMax Krasnyansky filter->count = 0; 646f271b2ccSMax Krasnyansky wmb(); 647f271b2ccSMax Krasnyansky 648f271b2ccSMax Krasnyansky /* Use first set of addresses as an exact filter */ 649f271b2ccSMax Krasnyansky for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++) 650f271b2ccSMax Krasnyansky memcpy(filter->addr[n], addr[n].u, ETH_ALEN); 651f271b2ccSMax Krasnyansky 652f271b2ccSMax Krasnyansky nexact = n; 653f271b2ccSMax Krasnyansky 654cfbf84fcSAlex Williamson /* Remaining multicast addresses are hashed, 655cfbf84fcSAlex Williamson * unicast will leave the filter disabled. */ 656f271b2ccSMax Krasnyansky memset(filter->mask, 0, sizeof(filter->mask)); 657cfbf84fcSAlex Williamson for (; n < uf.count; n++) { 658cfbf84fcSAlex Williamson if (!is_multicast_ether_addr(addr[n].u)) { 659cfbf84fcSAlex Williamson err = 0; /* no filter */ 660cfbf84fcSAlex Williamson goto done; 661cfbf84fcSAlex Williamson } 662f271b2ccSMax Krasnyansky addr_hash_set(filter->mask, addr[n].u); 663cfbf84fcSAlex Williamson } 664f271b2ccSMax Krasnyansky 665f271b2ccSMax Krasnyansky /* For ALLMULTI just set the mask to all ones. 666f271b2ccSMax Krasnyansky * This overrides the mask populated above. */ 667f271b2ccSMax Krasnyansky if ((uf.flags & TUN_FLT_ALLMULTI)) 668f271b2ccSMax Krasnyansky memset(filter->mask, ~0, sizeof(filter->mask)); 669f271b2ccSMax Krasnyansky 670f271b2ccSMax Krasnyansky /* Now enable the filter */ 671f271b2ccSMax Krasnyansky wmb(); 672f271b2ccSMax Krasnyansky filter->count = nexact; 673f271b2ccSMax Krasnyansky 674f271b2ccSMax Krasnyansky /* Return the number of exact filters */ 675f271b2ccSMax Krasnyansky err = nexact; 676f271b2ccSMax Krasnyansky 677f271b2ccSMax Krasnyansky done: 678f271b2ccSMax Krasnyansky kfree(addr); 679f271b2ccSMax Krasnyansky return err; 680f271b2ccSMax Krasnyansky } 681f271b2ccSMax Krasnyansky 682f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */ 683f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb) 684f271b2ccSMax Krasnyansky { 685f271b2ccSMax Krasnyansky /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect 686f271b2ccSMax Krasnyansky * at this point. */ 687f271b2ccSMax Krasnyansky struct ethhdr *eh = (struct ethhdr *) skb->data; 688f271b2ccSMax Krasnyansky int i; 689f271b2ccSMax Krasnyansky 690f271b2ccSMax Krasnyansky /* Exact match */ 691f271b2ccSMax Krasnyansky for (i = 0; i < filter->count; i++) 6922e42e474SJoe Perches if (ether_addr_equal(eh->h_dest, filter->addr[i])) 693f271b2ccSMax Krasnyansky return 1; 694f271b2ccSMax Krasnyansky 695f271b2ccSMax Krasnyansky /* Inexact match (multicast only) */ 696f271b2ccSMax Krasnyansky if (is_multicast_ether_addr(eh->h_dest)) 697f271b2ccSMax Krasnyansky return addr_hash_test(filter->mask, eh->h_dest); 698f271b2ccSMax Krasnyansky 699f271b2ccSMax Krasnyansky return 0; 700f271b2ccSMax Krasnyansky } 701f271b2ccSMax Krasnyansky 702f271b2ccSMax Krasnyansky /* 703f271b2ccSMax Krasnyansky * Checks whether the packet is accepted or not. 704f271b2ccSMax Krasnyansky * Returns: 0 - drop, !=0 - accept 705f271b2ccSMax Krasnyansky */ 706f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb) 707f271b2ccSMax Krasnyansky { 708f271b2ccSMax Krasnyansky if (!filter->count) 709f271b2ccSMax Krasnyansky return 1; 710f271b2ccSMax Krasnyansky 711f271b2ccSMax Krasnyansky return run_filter(filter, skb); 712f271b2ccSMax Krasnyansky } 713f271b2ccSMax Krasnyansky 7141da177e4SLinus Torvalds /* Network device part of the driver */ 7151da177e4SLinus Torvalds 7167282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops; 7171da177e4SLinus Torvalds 718c70f1829SEric W. Biederman /* Net device detach from fd. */ 719c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev) 720c70f1829SEric W. Biederman { 721c8d68e6bSJason Wang tun_detach_all(dev); 722c70f1829SEric W. Biederman } 723c70f1829SEric W. Biederman 7241da177e4SLinus Torvalds /* Net device open. */ 7251da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev) 7261da177e4SLinus Torvalds { 727c8d68e6bSJason Wang netif_tx_start_all_queues(dev); 7281da177e4SLinus Torvalds return 0; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds /* Net device close. */ 7321da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev) 7331da177e4SLinus Torvalds { 734c8d68e6bSJason Wang netif_tx_stop_all_queues(dev); 7351da177e4SLinus Torvalds return 0; 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds /* Net device start xmit */ 739424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) 7401da177e4SLinus Torvalds { 7411da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 742c8d68e6bSJason Wang int txq = skb->queue_mapping; 7436e914fc7SJason Wang struct tun_file *tfile; 744fa35864eSDominic Curran u32 numqueues = 0; 7451da177e4SLinus Torvalds 7466e914fc7SJason Wang rcu_read_lock(); 747c8d68e6bSJason Wang tfile = rcu_dereference(tun->tfiles[txq]); 748fa35864eSDominic Curran numqueues = ACCESS_ONCE(tun->numqueues); 749c8d68e6bSJason Wang 7501da177e4SLinus Torvalds /* Drop packet if interface is not attached */ 751fa35864eSDominic Curran if (txq >= numqueues) 7521da177e4SLinus Torvalds goto drop; 7531da177e4SLinus Torvalds 754fa35864eSDominic Curran if (numqueues == 1) { 7559bc88939STom Herbert /* Select queue was not called for the skbuff, so we extract the 7569bc88939STom Herbert * RPS hash and save it into the flow_table here. 7579bc88939STom Herbert */ 7589bc88939STom Herbert __u32 rxhash; 7599bc88939STom Herbert 7609bc88939STom Herbert rxhash = skb_get_hash(skb); 7619bc88939STom Herbert if (rxhash) { 7629bc88939STom Herbert struct tun_flow_entry *e; 7639bc88939STom Herbert e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)], 7649bc88939STom Herbert rxhash); 7659bc88939STom Herbert if (e) 7669bc88939STom Herbert tun_flow_save_rps_rxhash(e, rxhash); 7679bc88939STom Herbert } 7689bc88939STom Herbert } 7699bc88939STom Herbert 7706e914fc7SJason Wang tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len); 7716e914fc7SJason Wang 772c8d68e6bSJason Wang BUG_ON(!tfile); 773c8d68e6bSJason Wang 774f271b2ccSMax Krasnyansky /* Drop if the filter does not like it. 775f271b2ccSMax Krasnyansky * This is a noop if the filter is disabled. 776f271b2ccSMax Krasnyansky * Filter can be enabled only for the TAP devices. */ 777f271b2ccSMax Krasnyansky if (!check_filter(&tun->txflt, skb)) 778f271b2ccSMax Krasnyansky goto drop; 779f271b2ccSMax Krasnyansky 78054f968d6SJason Wang if (tfile->socket.sk->sk_filter && 78154f968d6SJason Wang sk_filter(tfile->socket.sk, skb)) 78299405162SMichael S. Tsirkin goto drop; 78399405162SMichael S. Tsirkin 78436fe8c09SRami Rosen /* Limit the number of packets queued by dividing txq length with the 785c8d68e6bSJason Wang * number of queues. 786c8d68e6bSJason Wang */ 787fa35864eSDominic Curran if (skb_queue_len(&tfile->socket.sk->sk_receive_queue) * numqueues 788fa35864eSDominic Curran >= dev->tx_queue_len) 7891da177e4SLinus Torvalds goto drop; 7901da177e4SLinus Torvalds 7917bf66305SJason Wang if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC))) 7927bf66305SJason Wang goto drop; 7937bf66305SJason Wang 794eda29772SRichard Cochran if (skb->sk) { 795eda29772SRichard Cochran sock_tx_timestamp(skb->sk, &skb_shinfo(skb)->tx_flags); 796eda29772SRichard Cochran sw_tx_timestamp(skb); 797eda29772SRichard Cochran } 798eda29772SRichard Cochran 7990110d6f2SMichael S. Tsirkin /* Orphan the skb - required as we might hang on to it 8007bf66305SJason Wang * for indefinite time. 8017bf66305SJason Wang */ 8020110d6f2SMichael S. Tsirkin skb_orphan(skb); 8030110d6f2SMichael S. Tsirkin 804f8af75f3SEric Dumazet nf_reset(skb); 805f8af75f3SEric Dumazet 806f271b2ccSMax Krasnyansky /* Enqueue packet */ 80754f968d6SJason Wang skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb); 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds /* Notify and wake up reader process */ 81054f968d6SJason Wang if (tfile->flags & TUN_FASYNC) 81154f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_IN); 8129e641bdcSXi Wang tfile->socket.sk->sk_data_ready(tfile->socket.sk); 8136e914fc7SJason Wang 8146e914fc7SJason Wang rcu_read_unlock(); 8156ed10654SPatrick McHardy return NETDEV_TX_OK; 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds drop: 81809f75cd7SJeff Garzik dev->stats.tx_dropped++; 819149d36f7SMichael S. Tsirkin skb_tx_error(skb); 8201da177e4SLinus Torvalds kfree_skb(skb); 8216e914fc7SJason Wang rcu_read_unlock(); 822*baeababbSJason Wang return NET_XMIT_DROP; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 825f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev) 8261da177e4SLinus Torvalds { 827f271b2ccSMax Krasnyansky /* 828f271b2ccSMax Krasnyansky * This callback is supposed to deal with mc filter in 829f271b2ccSMax Krasnyansky * _rx_ path and has nothing to do with the _tx_ path. 830f271b2ccSMax Krasnyansky * In rx path we always accept everything userspace gives us. 831f271b2ccSMax Krasnyansky */ 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds 8344885a504SEd Swierk #define MIN_MTU 68 8354885a504SEd Swierk #define MAX_MTU 65535 8364885a504SEd Swierk 8374885a504SEd Swierk static int 8384885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu) 8394885a504SEd Swierk { 8404885a504SEd Swierk if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU) 8414885a504SEd Swierk return -EINVAL; 8424885a504SEd Swierk dev->mtu = new_mtu; 8434885a504SEd Swierk return 0; 8444885a504SEd Swierk } 8454885a504SEd Swierk 846c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev, 847c8f44affSMichał Mirosław netdev_features_t features) 84888255375SMichał Mirosław { 84988255375SMichał Mirosław struct tun_struct *tun = netdev_priv(dev); 85088255375SMichał Mirosław 85188255375SMichał Mirosław return (features & tun->set_features) | (features & ~TUN_USER_FEATURES); 85288255375SMichał Mirosław } 853bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 854bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev) 855bebd097aSNeil Horman { 856bebd097aSNeil Horman /* 857bebd097aSNeil Horman * Tun only receives frames when: 858bebd097aSNeil Horman * 1) the char device endpoint gets data from user space 859bebd097aSNeil Horman * 2) the tun socket gets a sendmsg call from user space 86092d4ea6eSstephen hemminger * Since both of those are synchronous operations, we are guaranteed 861bebd097aSNeil Horman * never to have pending data when we poll for it 86292d4ea6eSstephen hemminger * so there is nothing to do here but return. 863bebd097aSNeil Horman * We need this though so netpoll recognizes us as an interface that 864bebd097aSNeil Horman * supports polling, which enables bridge devices in virt setups to 865bebd097aSNeil Horman * still use netconsole 866bebd097aSNeil Horman */ 867bebd097aSNeil Horman return; 868bebd097aSNeil Horman } 869bebd097aSNeil Horman #endif 870758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = { 871c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 872758e43b7SStephen Hemminger .ndo_open = tun_net_open, 873758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 87400829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 875758e43b7SStephen Hemminger .ndo_change_mtu = tun_net_change_mtu, 87688255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 877c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 878bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 879bebd097aSNeil Horman .ndo_poll_controller = tun_poll_controller, 880bebd097aSNeil Horman #endif 881758e43b7SStephen Hemminger }; 882758e43b7SStephen Hemminger 883758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = { 884c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 885758e43b7SStephen Hemminger .ndo_open = tun_net_open, 886758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 88700829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 888758e43b7SStephen Hemminger .ndo_change_mtu = tun_net_change_mtu, 88988255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 890afc4b13dSJiri Pirko .ndo_set_rx_mode = tun_net_mclist, 891758e43b7SStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 892758e43b7SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 893c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 894bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 895bebd097aSNeil Horman .ndo_poll_controller = tun_poll_controller, 896bebd097aSNeil Horman #endif 897758e43b7SStephen Hemminger }; 898758e43b7SStephen Hemminger 899944a1376SPavel Emelyanov static void tun_flow_init(struct tun_struct *tun) 90096442e42SJason Wang { 90196442e42SJason Wang int i; 90296442e42SJason Wang 90396442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) 90496442e42SJason Wang INIT_HLIST_HEAD(&tun->flows[i]); 90596442e42SJason Wang 90696442e42SJason Wang tun->ageing_time = TUN_FLOW_EXPIRE; 90796442e42SJason Wang setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun); 90896442e42SJason Wang mod_timer(&tun->flow_gc_timer, 90996442e42SJason Wang round_jiffies_up(jiffies + tun->ageing_time)); 91096442e42SJason Wang } 91196442e42SJason Wang 91296442e42SJason Wang static void tun_flow_uninit(struct tun_struct *tun) 91396442e42SJason Wang { 91496442e42SJason Wang del_timer_sync(&tun->flow_gc_timer); 91596442e42SJason Wang tun_flow_flush(tun); 91696442e42SJason Wang } 91796442e42SJason Wang 9181da177e4SLinus Torvalds /* Initialize net device. */ 9191da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev) 9201da177e4SLinus Torvalds { 9211da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 9241da177e4SLinus Torvalds case TUN_TUN_DEV: 925758e43b7SStephen Hemminger dev->netdev_ops = &tun_netdev_ops; 926758e43b7SStephen Hemminger 9271da177e4SLinus Torvalds /* Point-to-Point TUN Device */ 9281da177e4SLinus Torvalds dev->hard_header_len = 0; 9291da177e4SLinus Torvalds dev->addr_len = 0; 9301da177e4SLinus Torvalds dev->mtu = 1500; 9311da177e4SLinus Torvalds 9321da177e4SLinus Torvalds /* Zero header length */ 9331da177e4SLinus Torvalds dev->type = ARPHRD_NONE; 9341da177e4SLinus Torvalds dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 9351da177e4SLinus Torvalds dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ 9361da177e4SLinus Torvalds break; 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds case TUN_TAP_DEV: 9397a0a9608SKusanagi Kouichi dev->netdev_ops = &tap_netdev_ops; 9401da177e4SLinus Torvalds /* Ethernet TAP Device */ 9411da177e4SLinus Torvalds ether_setup(dev); 942550fd08cSNeil Horman dev->priv_flags &= ~IFF_TX_SKB_SHARING; 943a676847bSstephen hemminger dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 94436226a8dSBrian Braunstein 945f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 94636226a8dSBrian Braunstein 9471da177e4SLinus Torvalds dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ 9481da177e4SLinus Torvalds break; 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds /* Character device part */ 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds /* Poll */ 9551da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table *wait) 9561da177e4SLinus Torvalds { 957b2430de3SEric W. Biederman struct tun_file *tfile = file->private_data; 958b2430de3SEric W. Biederman struct tun_struct *tun = __tun_get(tfile); 9593c8a9c63SMariusz Kozlowski struct sock *sk; 96033dccbb0SHerbert Xu unsigned int mask = 0; 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds if (!tun) 963eac9e902SEric W. Biederman return POLLERR; 9641da177e4SLinus Torvalds 96554f968d6SJason Wang sk = tfile->socket.sk; 9663c8a9c63SMariusz Kozlowski 9676b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_chr_poll\n"); 9681da177e4SLinus Torvalds 9699e641bdcSXi Wang poll_wait(file, sk_sleep(sk), wait); 9701da177e4SLinus Torvalds 97189f56d1eSMichael S. Tsirkin if (!skb_queue_empty(&sk->sk_receive_queue)) 9721da177e4SLinus Torvalds mask |= POLLIN | POLLRDNORM; 9731da177e4SLinus Torvalds 97433dccbb0SHerbert Xu if (sock_writeable(sk) || 97533dccbb0SHerbert Xu (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) && 97633dccbb0SHerbert Xu sock_writeable(sk))) 97733dccbb0SHerbert Xu mask |= POLLOUT | POLLWRNORM; 97833dccbb0SHerbert Xu 979c70f1829SEric W. Biederman if (tun->dev->reg_state != NETREG_REGISTERED) 980c70f1829SEric W. Biederman mask = POLLERR; 981c70f1829SEric W. Biederman 982631ab46bSEric W. Biederman tun_put(tun); 9831da177e4SLinus Torvalds return mask; 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds 986f42157cbSRusty Russell /* prepad is the amount to reserve at front. len is length after that. 987f42157cbSRusty Russell * linear is a hint as to how much to copy (usually headers). */ 98854f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, 98933dccbb0SHerbert Xu size_t prepad, size_t len, 99033dccbb0SHerbert Xu size_t linear, int noblock) 991f42157cbSRusty Russell { 99254f968d6SJason Wang struct sock *sk = tfile->socket.sk; 993f42157cbSRusty Russell struct sk_buff *skb; 99433dccbb0SHerbert Xu int err; 995f42157cbSRusty Russell 996f42157cbSRusty Russell /* Under a page? Don't bother with paged skb. */ 9970eca93bcSHerbert Xu if (prepad + len < PAGE_SIZE || !linear) 99833dccbb0SHerbert Xu linear = len; 999f42157cbSRusty Russell 100033dccbb0SHerbert Xu skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, 100128d64271SEric Dumazet &err, 0); 1002f42157cbSRusty Russell if (!skb) 100333dccbb0SHerbert Xu return ERR_PTR(err); 1004f42157cbSRusty Russell 1005f42157cbSRusty Russell skb_reserve(skb, prepad); 1006f42157cbSRusty Russell skb_put(skb, linear); 100733dccbb0SHerbert Xu skb->data_len = len - linear; 100833dccbb0SHerbert Xu skb->len += len - linear; 1009f42157cbSRusty Russell 1010f42157cbSRusty Russell return skb; 1011f42157cbSRusty Russell } 1012f42157cbSRusty Russell 10131da177e4SLinus Torvalds /* Get packet from user space buffer */ 101454f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, 101554f968d6SJason Wang void *msg_control, const struct iovec *iv, 101654f968d6SJason Wang size_t total_len, size_t count, int noblock) 10171da177e4SLinus Torvalds { 101809640e63SHarvey Harrison struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; 10191da177e4SLinus Torvalds struct sk_buff *skb; 10203dd5c330SJason Wang size_t len = total_len, align = NET_SKB_PAD, linear; 1021f43798c2SRusty Russell struct virtio_net_hdr gso = { 0 }; 102296f8d9ecSJason Wang int good_linear; 10236f26c9a7SMichael S. Tsirkin int offset = 0; 10240690899bSMichael S. Tsirkin int copylen; 10250690899bSMichael S. Tsirkin bool zerocopy = false; 10260690899bSMichael S. Tsirkin int err; 102749974420SEric Dumazet u32 rxhash; 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds if (!(tun->flags & TUN_NO_PI)) { 103015718ea0SDan Carpenter if (len < sizeof(pi)) 10311da177e4SLinus Torvalds return -EINVAL; 103215718ea0SDan Carpenter len -= sizeof(pi); 10331da177e4SLinus Torvalds 10346f26c9a7SMichael S. Tsirkin if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi))) 10351da177e4SLinus Torvalds return -EFAULT; 10366f26c9a7SMichael S. Tsirkin offset += sizeof(pi); 10371da177e4SLinus Torvalds } 10381da177e4SLinus Torvalds 1039f43798c2SRusty Russell if (tun->flags & TUN_VNET_HDR) { 104015718ea0SDan Carpenter if (len < tun->vnet_hdr_sz) 1041f43798c2SRusty Russell return -EINVAL; 104215718ea0SDan Carpenter len -= tun->vnet_hdr_sz; 1043f43798c2SRusty Russell 10446f26c9a7SMichael S. Tsirkin if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso))) 1045f43798c2SRusty Russell return -EFAULT; 1046f43798c2SRusty Russell 10474909122fSHerbert Xu if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && 10484909122fSHerbert Xu gso.csum_start + gso.csum_offset + 2 > gso.hdr_len) 10494909122fSHerbert Xu gso.hdr_len = gso.csum_start + gso.csum_offset + 2; 10504909122fSHerbert Xu 1051f43798c2SRusty Russell if (gso.hdr_len > len) 1052f43798c2SRusty Russell return -EINVAL; 1053d9d52b51SMichael S. Tsirkin offset += tun->vnet_hdr_sz; 1054f43798c2SRusty Russell } 1055f43798c2SRusty Russell 1056e01bf1c8SRusty Russell if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) { 1057a504b86eSstephen hemminger align += NET_IP_ALIGN; 10580eca93bcSHerbert Xu if (unlikely(len < ETH_HLEN || 10590eca93bcSHerbert Xu (gso.hdr_len && gso.hdr_len < ETH_HLEN))) 1060e01bf1c8SRusty Russell return -EINVAL; 1061e01bf1c8SRusty Russell } 10621da177e4SLinus Torvalds 106396f8d9ecSJason Wang good_linear = SKB_MAX_HEAD(align); 106496f8d9ecSJason Wang 106588529176SJason Wang if (msg_control) { 106688529176SJason Wang /* There are 256 bytes to be copied in skb, so there is 106788529176SJason Wang * enough room for skb expand head in case it is used. 10680690899bSMichael S. Tsirkin * The rest of the buffer is mapped from userspace. 10690690899bSMichael S. Tsirkin */ 107088529176SJason Wang copylen = gso.hdr_len ? gso.hdr_len : GOODCOPY_LEN; 107196f8d9ecSJason Wang if (copylen > good_linear) 107296f8d9ecSJason Wang copylen = good_linear; 10733dd5c330SJason Wang linear = copylen; 107488529176SJason Wang if (iov_pages(iv, offset + copylen, count) <= MAX_SKB_FRAGS) 107588529176SJason Wang zerocopy = true; 107688529176SJason Wang } 107788529176SJason Wang 107888529176SJason Wang if (!zerocopy) { 10790690899bSMichael S. Tsirkin copylen = len; 108096f8d9ecSJason Wang if (gso.hdr_len > good_linear) 108196f8d9ecSJason Wang linear = good_linear; 108296f8d9ecSJason Wang else 10833dd5c330SJason Wang linear = gso.hdr_len; 10843dd5c330SJason Wang } 10850690899bSMichael S. Tsirkin 10863dd5c330SJason Wang skb = tun_alloc_skb(tfile, align, copylen, linear, noblock); 108733dccbb0SHerbert Xu if (IS_ERR(skb)) { 108833dccbb0SHerbert Xu if (PTR_ERR(skb) != -EAGAIN) 108909f75cd7SJeff Garzik tun->dev->stats.rx_dropped++; 109033dccbb0SHerbert Xu return PTR_ERR(skb); 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds 10930690899bSMichael S. Tsirkin if (zerocopy) 10940690899bSMichael S. Tsirkin err = zerocopy_sg_from_iovec(skb, iv, offset, count); 109588529176SJason Wang else { 10960690899bSMichael S. Tsirkin err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len); 109788529176SJason Wang if (!err && msg_control) { 109888529176SJason Wang struct ubuf_info *uarg = msg_control; 109988529176SJason Wang uarg->callback(uarg, false); 110088529176SJason Wang } 110188529176SJason Wang } 11020690899bSMichael S. Tsirkin 11030690899bSMichael S. Tsirkin if (err) { 110409f75cd7SJeff Garzik tun->dev->stats.rx_dropped++; 11058f22757eSDave Jones kfree_skb(skb); 11061da177e4SLinus Torvalds return -EFAULT; 11078f22757eSDave Jones } 11081da177e4SLinus Torvalds 1109f43798c2SRusty Russell if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { 1110f43798c2SRusty Russell if (!skb_partial_csum_set(skb, gso.csum_start, 1111f43798c2SRusty Russell gso.csum_offset)) { 1112f43798c2SRusty Russell tun->dev->stats.rx_frame_errors++; 1113f43798c2SRusty Russell kfree_skb(skb); 1114f43798c2SRusty Russell return -EINVAL; 1115f43798c2SRusty Russell } 111688255375SMichał Mirosław } 1117f43798c2SRusty Russell 11181da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 11191da177e4SLinus Torvalds case TUN_TUN_DEV: 1120f09f7ee2SAng Way Chuang if (tun->flags & TUN_NO_PI) { 1121f09f7ee2SAng Way Chuang switch (skb->data[0] & 0xf0) { 1122f09f7ee2SAng Way Chuang case 0x40: 1123f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IP); 1124f09f7ee2SAng Way Chuang break; 1125f09f7ee2SAng Way Chuang case 0x60: 1126f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IPV6); 1127f09f7ee2SAng Way Chuang break; 1128f09f7ee2SAng Way Chuang default: 1129f09f7ee2SAng Way Chuang tun->dev->stats.rx_dropped++; 1130f09f7ee2SAng Way Chuang kfree_skb(skb); 1131f09f7ee2SAng Way Chuang return -EINVAL; 1132f09f7ee2SAng Way Chuang } 1133f09f7ee2SAng Way Chuang } 1134f09f7ee2SAng Way Chuang 1135459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 11361da177e4SLinus Torvalds skb->protocol = pi.proto; 11374c13eb66SArnaldo Carvalho de Melo skb->dev = tun->dev; 11381da177e4SLinus Torvalds break; 11391da177e4SLinus Torvalds case TUN_TAP_DEV: 11401da177e4SLinus Torvalds skb->protocol = eth_type_trans(skb, tun->dev); 11411da177e4SLinus Torvalds break; 11426403eab1SJoe Perches } 11431da177e4SLinus Torvalds 11445188cd44SBen Hutchings skb_reset_network_header(skb); 11455188cd44SBen Hutchings 1146f43798c2SRusty Russell if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) { 1147f43798c2SRusty Russell pr_debug("GSO!\n"); 1148f43798c2SRusty Russell switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { 1149f43798c2SRusty Russell case VIRTIO_NET_HDR_GSO_TCPV4: 1150c9af6db4SPravin B Shelar skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; 1151f43798c2SRusty Russell break; 1152f43798c2SRusty Russell case VIRTIO_NET_HDR_GSO_TCPV6: 1153c9af6db4SPravin B Shelar skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; 1154f43798c2SRusty Russell break; 1155e36aa25aSSridhar Samudrala case VIRTIO_NET_HDR_GSO_UDP: 11563d0ad094SBen Hutchings { 11573d0ad094SBen Hutchings static bool warned; 11583d0ad094SBen Hutchings 11593d0ad094SBen Hutchings if (!warned) { 11603d0ad094SBen Hutchings warned = true; 11613d0ad094SBen Hutchings netdev_warn(tun->dev, 11623d0ad094SBen Hutchings "%s: using disabled UFO feature; please fix this program\n", 11633d0ad094SBen Hutchings current->comm); 11643d0ad094SBen Hutchings } 1165c9af6db4SPravin B Shelar skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 11665188cd44SBen Hutchings if (skb->protocol == htons(ETH_P_IPV6)) 11675188cd44SBen Hutchings ipv6_proxy_select_ident(skb); 1168e36aa25aSSridhar Samudrala break; 11693d0ad094SBen Hutchings } 1170f43798c2SRusty Russell default: 1171f43798c2SRusty Russell tun->dev->stats.rx_frame_errors++; 1172f43798c2SRusty Russell kfree_skb(skb); 1173f43798c2SRusty Russell return -EINVAL; 1174f43798c2SRusty Russell } 1175f43798c2SRusty Russell 1176f43798c2SRusty Russell if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN) 1177c9af6db4SPravin B Shelar skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; 1178f43798c2SRusty Russell 1179f43798c2SRusty Russell skb_shinfo(skb)->gso_size = gso.gso_size; 1180f43798c2SRusty Russell if (skb_shinfo(skb)->gso_size == 0) { 1181f43798c2SRusty Russell tun->dev->stats.rx_frame_errors++; 1182f43798c2SRusty Russell kfree_skb(skb); 1183f43798c2SRusty Russell return -EINVAL; 1184f43798c2SRusty Russell } 1185f43798c2SRusty Russell 1186f43798c2SRusty Russell /* Header must be checked, and gso_segs computed. */ 1187f43798c2SRusty Russell skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 1188f43798c2SRusty Russell skb_shinfo(skb)->gso_segs = 0; 1189f43798c2SRusty Russell } 11901da177e4SLinus Torvalds 11910690899bSMichael S. Tsirkin /* copy skb_ubuf_info for callback when skb has no error */ 11920690899bSMichael S. Tsirkin if (zerocopy) { 11930690899bSMichael S. Tsirkin skb_shinfo(skb)->destructor_arg = msg_control; 11940690899bSMichael S. Tsirkin skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; 1195c9af6db4SPravin B Shelar skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; 11960690899bSMichael S. Tsirkin } 11970690899bSMichael S. Tsirkin 119840893fd0SJason Wang skb_probe_transport_header(skb, 0); 119938502af7SJason Wang 12003958afa1STom Herbert rxhash = skb_get_hash(skb); 12011da177e4SLinus Torvalds netif_rx_ni(skb); 12021da177e4SLinus Torvalds 120309f75cd7SJeff Garzik tun->dev->stats.rx_packets++; 120409f75cd7SJeff Garzik tun->dev->stats.rx_bytes += len; 12051da177e4SLinus Torvalds 12069e85722dSJason Wang tun_flow_update(tun, rxhash, tfile); 12070690899bSMichael S. Tsirkin return total_len; 12081da177e4SLinus Torvalds } 12091da177e4SLinus Torvalds 1210ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv, 1211ee0b3e67SBadari Pulavarty unsigned long count, loff_t pos) 12121da177e4SLinus Torvalds { 121333dccbb0SHerbert Xu struct file *file = iocb->ki_filp; 1214ab46d779SHerbert Xu struct tun_struct *tun = tun_get(file); 121554f968d6SJason Wang struct tun_file *tfile = file->private_data; 1216631ab46bSEric W. Biederman ssize_t result; 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds if (!tun) 12191da177e4SLinus Torvalds return -EBADFD; 12201da177e4SLinus Torvalds 12216b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count); 12221da177e4SLinus Torvalds 122354f968d6SJason Wang result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count), 122454f968d6SJason Wang count, file->f_flags & O_NONBLOCK); 1225631ab46bSEric W. Biederman 1226631ab46bSEric W. Biederman tun_put(tun); 1227631ab46bSEric W. Biederman return result; 12281da177e4SLinus Torvalds } 12291da177e4SLinus Torvalds 12301da177e4SLinus Torvalds /* Put packet to the user space buffer */ 12316f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun, 123254f968d6SJason Wang struct tun_file *tfile, 12331da177e4SLinus Torvalds struct sk_buff *skb, 1234e0b46d0eSHerbert Xu struct iov_iter *iter) 12351da177e4SLinus Torvalds { 12361da177e4SLinus Torvalds struct tun_pi pi = { 0, skb->protocol }; 1237e0b46d0eSHerbert Xu ssize_t total; 12388c847d25SJason Wang int vlan_offset = 0; 1239a8f9bfdfSHerbert Xu int vlan_hlen = 0; 12402eb783c4SHerbert Xu int vnet_hdr_sz = 0; 1241a8f9bfdfSHerbert Xu 1242a8f9bfdfSHerbert Xu if (vlan_tx_tag_present(skb)) 1243a8f9bfdfSHerbert Xu vlan_hlen = VLAN_HLEN; 12441da177e4SLinus Torvalds 12452eb783c4SHerbert Xu if (tun->flags & TUN_VNET_HDR) 12462eb783c4SHerbert Xu vnet_hdr_sz = tun->vnet_hdr_sz; 12472eb783c4SHerbert Xu 1248e0b46d0eSHerbert Xu total = skb->len + vlan_hlen + vnet_hdr_sz; 1249e0b46d0eSHerbert Xu 12501da177e4SLinus Torvalds if (!(tun->flags & TUN_NO_PI)) { 1251e0b46d0eSHerbert Xu if (iov_iter_count(iter) < sizeof(pi)) 12521da177e4SLinus Torvalds return -EINVAL; 12531da177e4SLinus Torvalds 1254e0b46d0eSHerbert Xu total += sizeof(pi); 1255e0b46d0eSHerbert Xu if (iov_iter_count(iter) < total) { 12561da177e4SLinus Torvalds /* Packet will be striped */ 12571da177e4SLinus Torvalds pi.flags |= TUN_PKT_STRIP; 12581da177e4SLinus Torvalds } 12591da177e4SLinus Torvalds 1260e0b46d0eSHerbert Xu if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi)) 12611da177e4SLinus Torvalds return -EFAULT; 12621da177e4SLinus Torvalds } 12631da177e4SLinus Torvalds 12642eb783c4SHerbert Xu if (vnet_hdr_sz) { 1265f43798c2SRusty Russell struct virtio_net_hdr gso = { 0 }; /* no info leak */ 1266e0b46d0eSHerbert Xu if (iov_iter_count(iter) < vnet_hdr_sz) 1267f43798c2SRusty Russell return -EINVAL; 1268f43798c2SRusty Russell 1269f43798c2SRusty Russell if (skb_is_gso(skb)) { 1270f43798c2SRusty Russell struct skb_shared_info *sinfo = skb_shinfo(skb); 1271f43798c2SRusty Russell 1272f43798c2SRusty Russell /* This is a hint as to how much should be linear. */ 1273f43798c2SRusty Russell gso.hdr_len = skb_headlen(skb); 1274f43798c2SRusty Russell gso.gso_size = sinfo->gso_size; 1275f43798c2SRusty Russell if (sinfo->gso_type & SKB_GSO_TCPV4) 1276f43798c2SRusty Russell gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 1277f43798c2SRusty Russell else if (sinfo->gso_type & SKB_GSO_TCPV6) 1278f43798c2SRusty Russell gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 1279ef3db4a5SMichael S. Tsirkin else { 12806b8a66eeSJoe Perches pr_err("unexpected GSO type: " 1281ef3db4a5SMichael S. Tsirkin "0x%x, gso_size %d, hdr_len %d\n", 1282ef3db4a5SMichael S. Tsirkin sinfo->gso_type, gso.gso_size, 1283ef3db4a5SMichael S. Tsirkin gso.hdr_len); 1284ef3db4a5SMichael S. Tsirkin print_hex_dump(KERN_ERR, "tun: ", 1285ef3db4a5SMichael S. Tsirkin DUMP_PREFIX_NONE, 1286ef3db4a5SMichael S. Tsirkin 16, 1, skb->head, 1287ef3db4a5SMichael S. Tsirkin min((int)gso.hdr_len, 64), true); 1288ef3db4a5SMichael S. Tsirkin WARN_ON_ONCE(1); 1289ef3db4a5SMichael S. Tsirkin return -EINVAL; 1290ef3db4a5SMichael S. Tsirkin } 1291f43798c2SRusty Russell if (sinfo->gso_type & SKB_GSO_TCP_ECN) 1292f43798c2SRusty Russell gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN; 1293f43798c2SRusty Russell } else 1294f43798c2SRusty Russell gso.gso_type = VIRTIO_NET_HDR_GSO_NONE; 1295f43798c2SRusty Russell 1296f43798c2SRusty Russell if (skb->ip_summed == CHECKSUM_PARTIAL) { 1297f43798c2SRusty Russell gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; 1298a8f9bfdfSHerbert Xu gso.csum_start = skb_checksum_start_offset(skb) + 1299a8f9bfdfSHerbert Xu vlan_hlen; 1300f43798c2SRusty Russell gso.csum_offset = skb->csum_offset; 130110a8d94aSJason Wang } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) { 130210a8d94aSJason Wang gso.flags = VIRTIO_NET_HDR_F_DATA_VALID; 1303f43798c2SRusty Russell } /* else everything is zero */ 1304f43798c2SRusty Russell 1305e0b46d0eSHerbert Xu if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso)) 1306f43798c2SRusty Russell return -EFAULT; 13078c847d25SJason Wang 13088c847d25SJason Wang iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso)); 1309f43798c2SRusty Russell } 1310f43798c2SRusty Russell 1311a8f9bfdfSHerbert Xu if (vlan_hlen) { 1312e0b46d0eSHerbert Xu int ret; 13136680ec68SJason Wang struct { 13146680ec68SJason Wang __be16 h_vlan_proto; 13156680ec68SJason Wang __be16 h_vlan_TCI; 13166680ec68SJason Wang } veth; 13171da177e4SLinus Torvalds 13186680ec68SJason Wang veth.h_vlan_proto = skb->vlan_proto; 13196680ec68SJason Wang veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb)); 13201da177e4SLinus Torvalds 13216680ec68SJason Wang vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); 13226680ec68SJason Wang 1323e0b46d0eSHerbert Xu ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset); 1324e0b46d0eSHerbert Xu if (ret || !iov_iter_count(iter)) 13256680ec68SJason Wang goto done; 13266680ec68SJason Wang 1327e0b46d0eSHerbert Xu ret = copy_to_iter(&veth, sizeof(veth), iter); 1328e0b46d0eSHerbert Xu if (ret != sizeof(veth) || !iov_iter_count(iter)) 13296680ec68SJason Wang goto done; 13306680ec68SJason Wang } 13316680ec68SJason Wang 1332e0b46d0eSHerbert Xu skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset); 13336680ec68SJason Wang 13346680ec68SJason Wang done: 133509f75cd7SJeff Garzik tun->dev->stats.tx_packets++; 1336e0b46d0eSHerbert Xu tun->dev->stats.tx_bytes += skb->len + vlan_hlen; 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds return total; 13391da177e4SLinus Torvalds } 13401da177e4SLinus Torvalds 134154f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, 1342e0b46d0eSHerbert Xu const struct iovec *iv, unsigned long segs, 1343e0b46d0eSHerbert Xu ssize_t len, int noblock) 13441da177e4SLinus Torvalds { 13451da177e4SLinus Torvalds struct sk_buff *skb; 134605c2828cSMichael S. Tsirkin ssize_t ret = 0; 13479e641bdcSXi Wang int peeked, err, off = 0; 1348e0b46d0eSHerbert Xu struct iov_iter iter; 13491da177e4SLinus Torvalds 13503872baf6SRami Rosen tun_debug(KERN_INFO, tun, "tun_do_read\n"); 13511da177e4SLinus Torvalds 13529e641bdcSXi Wang if (!len) 13539e641bdcSXi Wang return ret; 13541da177e4SLinus Torvalds 13559e641bdcSXi Wang if (tun->dev->reg_state != NETREG_REGISTERED) 13569e641bdcSXi Wang return -EIO; 13571da177e4SLinus Torvalds 13589e641bdcSXi Wang /* Read frames from queue */ 13599e641bdcSXi Wang skb = __skb_recv_datagram(tfile->socket.sk, noblock ? MSG_DONTWAIT : 0, 13609e641bdcSXi Wang &peeked, &off, &err); 1361e0b46d0eSHerbert Xu if (!skb) 1362e0b46d0eSHerbert Xu return ret; 1363e0b46d0eSHerbert Xu 1364e0b46d0eSHerbert Xu iov_iter_init(&iter, READ, iv, segs, len); 1365e0b46d0eSHerbert Xu ret = tun_put_user(tun, tfile, skb, &iter); 13661da177e4SLinus Torvalds kfree_skb(skb); 13671da177e4SLinus Torvalds 136805c2828cSMichael S. Tsirkin return ret; 136905c2828cSMichael S. Tsirkin } 137005c2828cSMichael S. Tsirkin 137105c2828cSMichael S. Tsirkin static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv, 137205c2828cSMichael S. Tsirkin unsigned long count, loff_t pos) 137305c2828cSMichael S. Tsirkin { 137405c2828cSMichael S. Tsirkin struct file *file = iocb->ki_filp; 137505c2828cSMichael S. Tsirkin struct tun_file *tfile = file->private_data; 137605c2828cSMichael S. Tsirkin struct tun_struct *tun = __tun_get(tfile); 137705c2828cSMichael S. Tsirkin ssize_t len, ret; 137805c2828cSMichael S. Tsirkin 137905c2828cSMichael S. Tsirkin if (!tun) 138005c2828cSMichael S. Tsirkin return -EBADFD; 138105c2828cSMichael S. Tsirkin len = iov_length(iv, count); 138205c2828cSMichael S. Tsirkin if (len < 0) { 138305c2828cSMichael S. Tsirkin ret = -EINVAL; 138405c2828cSMichael S. Tsirkin goto out; 138505c2828cSMichael S. Tsirkin } 138605c2828cSMichael S. Tsirkin 1387e0b46d0eSHerbert Xu ret = tun_do_read(tun, tfile, iv, count, len, 138854f968d6SJason Wang file->f_flags & O_NONBLOCK); 138942404c09SDavid S. Miller ret = min_t(ssize_t, ret, len); 1390d0b7da8aSZhi Yong Wu if (ret > 0) 1391d0b7da8aSZhi Yong Wu iocb->ki_pos = ret; 1392631ab46bSEric W. Biederman out: 1393631ab46bSEric W. Biederman tun_put(tun); 13941da177e4SLinus Torvalds return ret; 13951da177e4SLinus Torvalds } 13961da177e4SLinus Torvalds 139796442e42SJason Wang static void tun_free_netdev(struct net_device *dev) 139896442e42SJason Wang { 139996442e42SJason Wang struct tun_struct *tun = netdev_priv(dev); 140096442e42SJason Wang 14014008e97fSJason Wang BUG_ON(!(list_empty(&tun->disabled))); 140296442e42SJason Wang tun_flow_uninit(tun); 14035dbbaf2dSPaul Moore security_tun_dev_free_security(tun->security); 140496442e42SJason Wang free_netdev(dev); 140596442e42SJason Wang } 140696442e42SJason Wang 14071da177e4SLinus Torvalds static void tun_setup(struct net_device *dev) 14081da177e4SLinus Torvalds { 14091da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 14101da177e4SLinus Torvalds 14110625c883SEric W. Biederman tun->owner = INVALID_UID; 14120625c883SEric W. Biederman tun->group = INVALID_GID; 14131da177e4SLinus Torvalds 14141da177e4SLinus Torvalds dev->ethtool_ops = &tun_ethtool_ops; 141596442e42SJason Wang dev->destructor = tun_free_netdev; 14161da177e4SLinus Torvalds } 14171da177e4SLinus Torvalds 1418f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap 1419f019a7a5SEric W. Biederman * device with netlink. 1420f019a7a5SEric W. Biederman */ 1421f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[]) 1422f019a7a5SEric W. Biederman { 1423f019a7a5SEric W. Biederman return -EINVAL; 1424f019a7a5SEric W. Biederman } 1425f019a7a5SEric W. Biederman 1426f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = { 1427f019a7a5SEric W. Biederman .kind = DRV_NAME, 1428f019a7a5SEric W. Biederman .priv_size = sizeof(struct tun_struct), 1429f019a7a5SEric W. Biederman .setup = tun_setup, 1430f019a7a5SEric W. Biederman .validate = tun_validate, 1431f019a7a5SEric W. Biederman }; 1432f019a7a5SEric W. Biederman 143333dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk) 143433dccbb0SHerbert Xu { 143554f968d6SJason Wang struct tun_file *tfile; 143643815482SEric Dumazet wait_queue_head_t *wqueue; 143733dccbb0SHerbert Xu 143833dccbb0SHerbert Xu if (!sock_writeable(sk)) 143933dccbb0SHerbert Xu return; 144033dccbb0SHerbert Xu 144133dccbb0SHerbert Xu if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags)) 144233dccbb0SHerbert Xu return; 144333dccbb0SHerbert Xu 144443815482SEric Dumazet wqueue = sk_sleep(sk); 144543815482SEric Dumazet if (wqueue && waitqueue_active(wqueue)) 144643815482SEric Dumazet wake_up_interruptible_sync_poll(wqueue, POLLOUT | 144705c2828cSMichael S. Tsirkin POLLWRNORM | POLLWRBAND); 1448c722c625SHerbert Xu 144954f968d6SJason Wang tfile = container_of(sk, struct tun_file, sk); 145054f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); 145133dccbb0SHerbert Xu } 145233dccbb0SHerbert Xu 145305c2828cSMichael S. Tsirkin static int tun_sendmsg(struct kiocb *iocb, struct socket *sock, 145405c2828cSMichael S. Tsirkin struct msghdr *m, size_t total_len) 145505c2828cSMichael S. Tsirkin { 145654f968d6SJason Wang int ret; 145754f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 145854f968d6SJason Wang struct tun_struct *tun = __tun_get(tfile); 145954f968d6SJason Wang 146054f968d6SJason Wang if (!tun) 146154f968d6SJason Wang return -EBADFD; 146254f968d6SJason Wang ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len, 14630690899bSMichael S. Tsirkin m->msg_iovlen, m->msg_flags & MSG_DONTWAIT); 146454f968d6SJason Wang tun_put(tun); 146554f968d6SJason Wang return ret; 146605c2828cSMichael S. Tsirkin } 146705c2828cSMichael S. Tsirkin 146805c2828cSMichael S. Tsirkin static int tun_recvmsg(struct kiocb *iocb, struct socket *sock, 146905c2828cSMichael S. Tsirkin struct msghdr *m, size_t total_len, 147005c2828cSMichael S. Tsirkin int flags) 147105c2828cSMichael S. Tsirkin { 147254f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 147354f968d6SJason Wang struct tun_struct *tun = __tun_get(tfile); 147405c2828cSMichael S. Tsirkin int ret; 147554f968d6SJason Wang 147654f968d6SJason Wang if (!tun) 147754f968d6SJason Wang return -EBADFD; 147854f968d6SJason Wang 1479eda29772SRichard Cochran if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) { 14803811ae76SGao feng ret = -EINVAL; 14813811ae76SGao feng goto out; 14823811ae76SGao feng } 1483eda29772SRichard Cochran if (flags & MSG_ERRQUEUE) { 1484eda29772SRichard Cochran ret = sock_recv_errqueue(sock->sk, m, total_len, 1485eda29772SRichard Cochran SOL_PACKET, TUN_TX_TIMESTAMP); 1486eda29772SRichard Cochran goto out; 1487eda29772SRichard Cochran } 1488e0b46d0eSHerbert Xu ret = tun_do_read(tun, tfile, m->msg_iov, m->msg_iovlen, total_len, 148905c2828cSMichael S. Tsirkin flags & MSG_DONTWAIT); 149042404c09SDavid S. Miller if (ret > total_len) { 149142404c09SDavid S. Miller m->msg_flags |= MSG_TRUNC; 149242404c09SDavid S. Miller ret = flags & MSG_TRUNC ? ret : total_len; 149342404c09SDavid S. Miller } 14943811ae76SGao feng out: 149554f968d6SJason Wang tun_put(tun); 149605c2828cSMichael S. Tsirkin return ret; 149705c2828cSMichael S. Tsirkin } 149805c2828cSMichael S. Tsirkin 14991ab5ecb9SStanislav Kinsbursky static int tun_release(struct socket *sock) 15001ab5ecb9SStanislav Kinsbursky { 15011ab5ecb9SStanislav Kinsbursky if (sock->sk) 15021ab5ecb9SStanislav Kinsbursky sock_put(sock->sk); 15031ab5ecb9SStanislav Kinsbursky return 0; 15041ab5ecb9SStanislav Kinsbursky } 15051ab5ecb9SStanislav Kinsbursky 150605c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */ 150705c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = { 150805c2828cSMichael S. Tsirkin .sendmsg = tun_sendmsg, 150905c2828cSMichael S. Tsirkin .recvmsg = tun_recvmsg, 15101ab5ecb9SStanislav Kinsbursky .release = tun_release, 151105c2828cSMichael S. Tsirkin }; 151205c2828cSMichael S. Tsirkin 151333dccbb0SHerbert Xu static struct proto tun_proto = { 151433dccbb0SHerbert Xu .name = "tun", 151533dccbb0SHerbert Xu .owner = THIS_MODULE, 151654f968d6SJason Wang .obj_size = sizeof(struct tun_file), 151733dccbb0SHerbert Xu }; 1518f019a7a5SEric W. Biederman 1519980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun) 1520980c9e8cSDavid Woodhouse { 1521980c9e8cSDavid Woodhouse int flags = 0; 1522980c9e8cSDavid Woodhouse 1523980c9e8cSDavid Woodhouse if (tun->flags & TUN_TUN_DEV) 1524980c9e8cSDavid Woodhouse flags |= IFF_TUN; 1525980c9e8cSDavid Woodhouse else 1526980c9e8cSDavid Woodhouse flags |= IFF_TAP; 1527980c9e8cSDavid Woodhouse 1528980c9e8cSDavid Woodhouse if (tun->flags & TUN_NO_PI) 1529980c9e8cSDavid Woodhouse flags |= IFF_NO_PI; 1530980c9e8cSDavid Woodhouse 15315d097109SMichael S. Tsirkin /* This flag has no real effect. We track the value for backwards 15325d097109SMichael S. Tsirkin * compatibility. 15335d097109SMichael S. Tsirkin */ 1534980c9e8cSDavid Woodhouse if (tun->flags & TUN_ONE_QUEUE) 1535980c9e8cSDavid Woodhouse flags |= IFF_ONE_QUEUE; 1536980c9e8cSDavid Woodhouse 1537980c9e8cSDavid Woodhouse if (tun->flags & TUN_VNET_HDR) 1538980c9e8cSDavid Woodhouse flags |= IFF_VNET_HDR; 1539980c9e8cSDavid Woodhouse 1540c8d68e6bSJason Wang if (tun->flags & TUN_TAP_MQ) 1541c8d68e6bSJason Wang flags |= IFF_MULTI_QUEUE; 1542c8d68e6bSJason Wang 1543274038f8SPavel Emelyanov if (tun->flags & TUN_PERSIST) 1544274038f8SPavel Emelyanov flags |= IFF_PERSIST; 1545274038f8SPavel Emelyanov 1546980c9e8cSDavid Woodhouse return flags; 1547980c9e8cSDavid Woodhouse } 1548980c9e8cSDavid Woodhouse 1549980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr, 1550980c9e8cSDavid Woodhouse char *buf) 1551980c9e8cSDavid Woodhouse { 1552980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 1553980c9e8cSDavid Woodhouse return sprintf(buf, "0x%x\n", tun_flags(tun)); 1554980c9e8cSDavid Woodhouse } 1555980c9e8cSDavid Woodhouse 1556980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr, 1557980c9e8cSDavid Woodhouse char *buf) 1558980c9e8cSDavid Woodhouse { 1559980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 15600625c883SEric W. Biederman return uid_valid(tun->owner)? 15610625c883SEric W. Biederman sprintf(buf, "%u\n", 15620625c883SEric W. Biederman from_kuid_munged(current_user_ns(), tun->owner)): 15630625c883SEric W. Biederman sprintf(buf, "-1\n"); 1564980c9e8cSDavid Woodhouse } 1565980c9e8cSDavid Woodhouse 1566980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr, 1567980c9e8cSDavid Woodhouse char *buf) 1568980c9e8cSDavid Woodhouse { 1569980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 15700625c883SEric W. Biederman return gid_valid(tun->group) ? 15710625c883SEric W. Biederman sprintf(buf, "%u\n", 15720625c883SEric W. Biederman from_kgid_munged(current_user_ns(), tun->group)): 15730625c883SEric W. Biederman sprintf(buf, "-1\n"); 1574980c9e8cSDavid Woodhouse } 1575980c9e8cSDavid Woodhouse 1576980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL); 1577980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL); 1578980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL); 1579980c9e8cSDavid Woodhouse 1580d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) 15811da177e4SLinus Torvalds { 15821da177e4SLinus Torvalds struct tun_struct *tun; 158354f968d6SJason Wang struct tun_file *tfile = file->private_data; 15841da177e4SLinus Torvalds struct net_device *dev; 15851da177e4SLinus Torvalds int err; 15861da177e4SLinus Torvalds 15877c0c3b1aSJason Wang if (tfile->detached) 15887c0c3b1aSJason Wang return -EINVAL; 15897c0c3b1aSJason Wang 159074a3e5a7SEric W. Biederman dev = __dev_get_by_name(net, ifr->ifr_name); 159174a3e5a7SEric W. Biederman if (dev) { 1592f85ba780SDavid Woodhouse if (ifr->ifr_flags & IFF_TUN_EXCL) 1593f85ba780SDavid Woodhouse return -EBUSY; 159474a3e5a7SEric W. Biederman if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) 159574a3e5a7SEric W. Biederman tun = netdev_priv(dev); 159674a3e5a7SEric W. Biederman else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) 159774a3e5a7SEric W. Biederman tun = netdev_priv(dev); 159874a3e5a7SEric W. Biederman else 159974a3e5a7SEric W. Biederman return -EINVAL; 160074a3e5a7SEric W. Biederman 16018e6d91aeSJason Wang if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) != 16028e6d91aeSJason Wang !!(tun->flags & TUN_TAP_MQ)) 16038e6d91aeSJason Wang return -EINVAL; 16048e6d91aeSJason Wang 1605cde8b15fSJason Wang if (tun_not_capable(tun)) 16062b980dbdSPaul Moore return -EPERM; 16075dbbaf2dSPaul Moore err = security_tun_dev_open(tun->security); 16082b980dbdSPaul Moore if (err < 0) 16092b980dbdSPaul Moore return err; 16102b980dbdSPaul Moore 1611849c9b6fSPavel Emelyanov err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER); 1612a7385ba2SEric W. Biederman if (err < 0) 1613a7385ba2SEric W. Biederman return err; 16144008e97fSJason Wang 16154008e97fSJason Wang if (tun->flags & TUN_TAP_MQ && 1616e8dbad66SJason Wang (tun->numqueues + tun->numdisabled > 1)) { 1617e8dbad66SJason Wang /* One or more queue has already been attached, no need 1618e8dbad66SJason Wang * to initialize the device again. 1619e8dbad66SJason Wang */ 1620e8dbad66SJason Wang return 0; 1621e8dbad66SJason Wang } 162286a264abSDavid Howells } 16231da177e4SLinus Torvalds else { 16241da177e4SLinus Torvalds char *name; 16251da177e4SLinus Torvalds unsigned long flags = 0; 1626edfb6a14SJason Wang int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ? 1627edfb6a14SJason Wang MAX_TAP_QUEUES : 1; 16281da177e4SLinus Torvalds 1629c260b772SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1630ca6bb5d7SDavid Woodhouse return -EPERM; 16312b980dbdSPaul Moore err = security_tun_dev_create(); 16322b980dbdSPaul Moore if (err < 0) 16332b980dbdSPaul Moore return err; 1634ca6bb5d7SDavid Woodhouse 16351da177e4SLinus Torvalds /* Set dev type */ 16361da177e4SLinus Torvalds if (ifr->ifr_flags & IFF_TUN) { 16371da177e4SLinus Torvalds /* TUN device */ 16381da177e4SLinus Torvalds flags |= TUN_TUN_DEV; 16391da177e4SLinus Torvalds name = "tun%d"; 16401da177e4SLinus Torvalds } else if (ifr->ifr_flags & IFF_TAP) { 16411da177e4SLinus Torvalds /* TAP device */ 16421da177e4SLinus Torvalds flags |= TUN_TAP_DEV; 16431da177e4SLinus Torvalds name = "tap%d"; 16441da177e4SLinus Torvalds } else 164536989b90SKusanagi Kouichi return -EINVAL; 16461da177e4SLinus Torvalds 16471da177e4SLinus Torvalds if (*ifr->ifr_name) 16481da177e4SLinus Torvalds name = ifr->ifr_name; 16491da177e4SLinus Torvalds 1650c8d68e6bSJason Wang dev = alloc_netdev_mqs(sizeof(struct tun_struct), name, 1651c835a677STom Gundersen NET_NAME_UNKNOWN, tun_setup, queues, 1652c835a677STom Gundersen queues); 1653edfb6a14SJason Wang 16541da177e4SLinus Torvalds if (!dev) 16551da177e4SLinus Torvalds return -ENOMEM; 16561da177e4SLinus Torvalds 1657fc54c658SPavel Emelyanov dev_net_set(dev, net); 1658f019a7a5SEric W. Biederman dev->rtnl_link_ops = &tun_link_ops; 1659fb7589a1SPavel Emelyanov dev->ifindex = tfile->ifindex; 1660758e43b7SStephen Hemminger 16611da177e4SLinus Torvalds tun = netdev_priv(dev); 16621da177e4SLinus Torvalds tun->dev = dev; 16631da177e4SLinus Torvalds tun->flags = flags; 1664f271b2ccSMax Krasnyansky tun->txflt.count = 0; 1665d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr); 16661da177e4SLinus Torvalds 166754f968d6SJason Wang tun->filter_attached = false; 166854f968d6SJason Wang tun->sndbuf = tfile->socket.sk->sk_sndbuf; 166933dccbb0SHerbert Xu 167096442e42SJason Wang spin_lock_init(&tun->lock); 167196442e42SJason Wang 16725dbbaf2dSPaul Moore err = security_tun_dev_alloc_security(&tun->security); 16735dbbaf2dSPaul Moore if (err < 0) 16745dbbaf2dSPaul Moore goto err_free_dev; 16752b980dbdSPaul Moore 16761da177e4SLinus Torvalds tun_net_init(dev); 1677944a1376SPavel Emelyanov tun_flow_init(tun); 167896442e42SJason Wang 167988255375SMichał Mirosław dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | 16806680ec68SJason Wang TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX | 16816680ec68SJason Wang NETIF_F_HW_VLAN_STAG_TX; 168288255375SMichał Mirosław dev->features = dev->hw_features; 16836671b224SFernando Luis Vazquez Cao dev->vlan_features = dev->features & 16846671b224SFernando Luis Vazquez Cao ~(NETIF_F_HW_VLAN_CTAG_TX | 16856671b224SFernando Luis Vazquez Cao NETIF_F_HW_VLAN_STAG_TX); 168688255375SMichał Mirosław 16874008e97fSJason Wang INIT_LIST_HEAD(&tun->disabled); 1688849c9b6fSPavel Emelyanov err = tun_attach(tun, file, false); 1689eb0fb363SJason Wang if (err < 0) 1690662ca437SJason Wang goto err_free_flow; 1691eb0fb363SJason Wang 16921da177e4SLinus Torvalds err = register_netdevice(tun->dev); 16931da177e4SLinus Torvalds if (err < 0) 1694662ca437SJason Wang goto err_detach; 16959c3fea6aSHerbert Xu 1696980c9e8cSDavid Woodhouse if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) || 1697980c9e8cSDavid Woodhouse device_create_file(&tun->dev->dev, &dev_attr_owner) || 1698980c9e8cSDavid Woodhouse device_create_file(&tun->dev->dev, &dev_attr_group)) 16996b8a66eeSJoe Perches pr_err("Failed to create tun sysfs files\n"); 1700af668b3cSMichael S. Tsirkin } 1701980c9e8cSDavid Woodhouse 1702eb0fb363SJason Wang netif_carrier_on(tun->dev); 17031da177e4SLinus Torvalds 17046b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_set_iff\n"); 17051da177e4SLinus Torvalds 17061da177e4SLinus Torvalds if (ifr->ifr_flags & IFF_NO_PI) 17071da177e4SLinus Torvalds tun->flags |= TUN_NO_PI; 1708a26af1e0SNathaniel Filardo else 1709a26af1e0SNathaniel Filardo tun->flags &= ~TUN_NO_PI; 17101da177e4SLinus Torvalds 17115d097109SMichael S. Tsirkin /* This flag has no real effect. We track the value for backwards 17125d097109SMichael S. Tsirkin * compatibility. 17135d097109SMichael S. Tsirkin */ 17141da177e4SLinus Torvalds if (ifr->ifr_flags & IFF_ONE_QUEUE) 17151da177e4SLinus Torvalds tun->flags |= TUN_ONE_QUEUE; 1716a26af1e0SNathaniel Filardo else 1717a26af1e0SNathaniel Filardo tun->flags &= ~TUN_ONE_QUEUE; 17181da177e4SLinus Torvalds 1719f43798c2SRusty Russell if (ifr->ifr_flags & IFF_VNET_HDR) 1720f43798c2SRusty Russell tun->flags |= TUN_VNET_HDR; 1721f43798c2SRusty Russell else 1722f43798c2SRusty Russell tun->flags &= ~TUN_VNET_HDR; 1723f43798c2SRusty Russell 1724c8d68e6bSJason Wang if (ifr->ifr_flags & IFF_MULTI_QUEUE) 1725c8d68e6bSJason Wang tun->flags |= TUN_TAP_MQ; 1726c8d68e6bSJason Wang else 1727c8d68e6bSJason Wang tun->flags &= ~TUN_TAP_MQ; 1728c8d68e6bSJason Wang 1729e35259a9SMax Krasnyansky /* Make sure persistent devices do not get stuck in 1730e35259a9SMax Krasnyansky * xoff state. 1731e35259a9SMax Krasnyansky */ 1732e35259a9SMax Krasnyansky if (netif_running(tun->dev)) 1733c8d68e6bSJason Wang netif_tx_wake_all_queues(tun->dev); 1734e35259a9SMax Krasnyansky 17351da177e4SLinus Torvalds strcpy(ifr->ifr_name, tun->dev->name); 17361da177e4SLinus Torvalds return 0; 17371da177e4SLinus Torvalds 1738662ca437SJason Wang err_detach: 1739662ca437SJason Wang tun_detach_all(dev); 1740662ca437SJason Wang err_free_flow: 1741662ca437SJason Wang tun_flow_uninit(tun); 1742662ca437SJason Wang security_tun_dev_free_security(tun->security); 17431da177e4SLinus Torvalds err_free_dev: 17441da177e4SLinus Torvalds free_netdev(dev); 17451da177e4SLinus Torvalds return err; 17461da177e4SLinus Torvalds } 17471da177e4SLinus Torvalds 17489ce99cf6SRami Rosen static void tun_get_iff(struct net *net, struct tun_struct *tun, 1749876bfd4dSHerbert Xu struct ifreq *ifr) 1750e3b99556SMark McLoughlin { 17516b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_get_iff\n"); 1752e3b99556SMark McLoughlin 1753e3b99556SMark McLoughlin strcpy(ifr->ifr_name, tun->dev->name); 1754e3b99556SMark McLoughlin 1755980c9e8cSDavid Woodhouse ifr->ifr_flags = tun_flags(tun); 1756e3b99556SMark McLoughlin 1757e3b99556SMark McLoughlin } 1758e3b99556SMark McLoughlin 17595228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no 17605228ddc9SRusty Russell * privs required. */ 176188255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg) 17625228ddc9SRusty Russell { 1763c8f44affSMichał Mirosław netdev_features_t features = 0; 17645228ddc9SRusty Russell 17655228ddc9SRusty Russell if (arg & TUN_F_CSUM) { 176688255375SMichał Mirosław features |= NETIF_F_HW_CSUM; 17675228ddc9SRusty Russell arg &= ~TUN_F_CSUM; 17685228ddc9SRusty Russell 17695228ddc9SRusty Russell if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { 17705228ddc9SRusty Russell if (arg & TUN_F_TSO_ECN) { 17715228ddc9SRusty Russell features |= NETIF_F_TSO_ECN; 17725228ddc9SRusty Russell arg &= ~TUN_F_TSO_ECN; 17735228ddc9SRusty Russell } 17745228ddc9SRusty Russell if (arg & TUN_F_TSO4) 17755228ddc9SRusty Russell features |= NETIF_F_TSO; 17765228ddc9SRusty Russell if (arg & TUN_F_TSO6) 17775228ddc9SRusty Russell features |= NETIF_F_TSO6; 17785228ddc9SRusty Russell arg &= ~(TUN_F_TSO4|TUN_F_TSO6); 17795228ddc9SRusty Russell } 17805228ddc9SRusty Russell } 17815228ddc9SRusty Russell 17825228ddc9SRusty Russell /* This gives the user a way to test for new features in future by 17835228ddc9SRusty Russell * trying to set them. */ 17845228ddc9SRusty Russell if (arg) 17855228ddc9SRusty Russell return -EINVAL; 17865228ddc9SRusty Russell 178788255375SMichał Mirosław tun->set_features = features; 178888255375SMichał Mirosław netdev_update_features(tun->dev); 17895228ddc9SRusty Russell 17905228ddc9SRusty Russell return 0; 17915228ddc9SRusty Russell } 17925228ddc9SRusty Russell 1793c8d68e6bSJason Wang static void tun_detach_filter(struct tun_struct *tun, int n) 1794c8d68e6bSJason Wang { 1795c8d68e6bSJason Wang int i; 1796c8d68e6bSJason Wang struct tun_file *tfile; 1797c8d68e6bSJason Wang 1798c8d68e6bSJason Wang for (i = 0; i < n; i++) { 1799b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 1800c8d68e6bSJason Wang sk_detach_filter(tfile->socket.sk); 1801c8d68e6bSJason Wang } 1802c8d68e6bSJason Wang 1803c8d68e6bSJason Wang tun->filter_attached = false; 1804c8d68e6bSJason Wang } 1805c8d68e6bSJason Wang 1806c8d68e6bSJason Wang static int tun_attach_filter(struct tun_struct *tun) 1807c8d68e6bSJason Wang { 1808c8d68e6bSJason Wang int i, ret = 0; 1809c8d68e6bSJason Wang struct tun_file *tfile; 1810c8d68e6bSJason Wang 1811c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 1812b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 1813c8d68e6bSJason Wang ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); 1814c8d68e6bSJason Wang if (ret) { 1815c8d68e6bSJason Wang tun_detach_filter(tun, i); 1816c8d68e6bSJason Wang return ret; 1817c8d68e6bSJason Wang } 1818c8d68e6bSJason Wang } 1819c8d68e6bSJason Wang 1820c8d68e6bSJason Wang tun->filter_attached = true; 1821c8d68e6bSJason Wang return ret; 1822c8d68e6bSJason Wang } 1823c8d68e6bSJason Wang 1824c8d68e6bSJason Wang static void tun_set_sndbuf(struct tun_struct *tun) 1825c8d68e6bSJason Wang { 1826c8d68e6bSJason Wang struct tun_file *tfile; 1827c8d68e6bSJason Wang int i; 1828c8d68e6bSJason Wang 1829c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 1830b8deabd3SJason Wang tfile = rtnl_dereference(tun->tfiles[i]); 1831c8d68e6bSJason Wang tfile->socket.sk->sk_sndbuf = tun->sndbuf; 1832c8d68e6bSJason Wang } 1833c8d68e6bSJason Wang } 1834c8d68e6bSJason Wang 1835cde8b15fSJason Wang static int tun_set_queue(struct file *file, struct ifreq *ifr) 1836cde8b15fSJason Wang { 1837cde8b15fSJason Wang struct tun_file *tfile = file->private_data; 1838cde8b15fSJason Wang struct tun_struct *tun; 1839cde8b15fSJason Wang int ret = 0; 1840cde8b15fSJason Wang 1841cde8b15fSJason Wang rtnl_lock(); 1842cde8b15fSJason Wang 1843cde8b15fSJason Wang if (ifr->ifr_flags & IFF_ATTACH_QUEUE) { 18444008e97fSJason Wang tun = tfile->detached; 18455dbbaf2dSPaul Moore if (!tun) { 1846cde8b15fSJason Wang ret = -EINVAL; 18475dbbaf2dSPaul Moore goto unlock; 18485dbbaf2dSPaul Moore } 18495dbbaf2dSPaul Moore ret = security_tun_dev_attach_queue(tun->security); 18505dbbaf2dSPaul Moore if (ret < 0) 18515dbbaf2dSPaul Moore goto unlock; 1852849c9b6fSPavel Emelyanov ret = tun_attach(tun, file, false); 18534008e97fSJason Wang } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) { 1854b8deabd3SJason Wang tun = rtnl_dereference(tfile->tun); 18559e85722dSJason Wang if (!tun || !(tun->flags & TUN_TAP_MQ) || tfile->detached) 18564008e97fSJason Wang ret = -EINVAL; 1857cde8b15fSJason Wang else 18584008e97fSJason Wang __tun_detach(tfile, false); 18594008e97fSJason Wang } else 1860cde8b15fSJason Wang ret = -EINVAL; 1861cde8b15fSJason Wang 18625dbbaf2dSPaul Moore unlock: 1863cde8b15fSJason Wang rtnl_unlock(); 1864cde8b15fSJason Wang return ret; 1865cde8b15fSJason Wang } 1866cde8b15fSJason Wang 186750857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd, 186850857e2aSArnd Bergmann unsigned long arg, int ifreq_len) 18691da177e4SLinus Torvalds { 187036b50babSEric W. Biederman struct tun_file *tfile = file->private_data; 1871631ab46bSEric W. Biederman struct tun_struct *tun; 18721da177e4SLinus Torvalds void __user* argp = (void __user*)arg; 18731da177e4SLinus Torvalds struct ifreq ifr; 18740625c883SEric W. Biederman kuid_t owner; 18750625c883SEric W. Biederman kgid_t group; 187633dccbb0SHerbert Xu int sndbuf; 1877d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 1878fb7589a1SPavel Emelyanov unsigned int ifindex; 1879f271b2ccSMax Krasnyansky int ret; 18801da177e4SLinus Torvalds 1881cde8b15fSJason Wang if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) { 188250857e2aSArnd Bergmann if (copy_from_user(&ifr, argp, ifreq_len)) 18831da177e4SLinus Torvalds return -EFAULT; 18848bbb1813SDavid S. Miller } else { 1885a117dacdSMathias Krause memset(&ifr, 0, sizeof(ifr)); 18868bbb1813SDavid S. Miller } 1887631ab46bSEric W. Biederman if (cmd == TUNGETFEATURES) { 1888631ab46bSEric W. Biederman /* Currently this just means: "what IFF flags are valid?". 1889631ab46bSEric W. Biederman * This is needed because we never checked for invalid flags on 1890631ab46bSEric W. Biederman * TUNSETIFF. */ 1891631ab46bSEric W. Biederman return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | 1892cde8b15fSJason Wang IFF_VNET_HDR | IFF_MULTI_QUEUE, 1893631ab46bSEric W. Biederman (unsigned int __user*)argp); 1894cde8b15fSJason Wang } else if (cmd == TUNSETQUEUE) 1895cde8b15fSJason Wang return tun_set_queue(file, &ifr); 1896631ab46bSEric W. Biederman 1897c8d68e6bSJason Wang ret = 0; 1898876bfd4dSHerbert Xu rtnl_lock(); 1899876bfd4dSHerbert Xu 190036b50babSEric W. Biederman tun = __tun_get(tfile); 19011da177e4SLinus Torvalds if (cmd == TUNSETIFF && !tun) { 19021da177e4SLinus Torvalds ifr.ifr_name[IFNAMSIZ-1] = '\0'; 19031da177e4SLinus Torvalds 1904876bfd4dSHerbert Xu ret = tun_set_iff(tfile->net, file, &ifr); 19051da177e4SLinus Torvalds 1906876bfd4dSHerbert Xu if (ret) 1907876bfd4dSHerbert Xu goto unlock; 19081da177e4SLinus Torvalds 190950857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 1910876bfd4dSHerbert Xu ret = -EFAULT; 1911876bfd4dSHerbert Xu goto unlock; 19121da177e4SLinus Torvalds } 1913fb7589a1SPavel Emelyanov if (cmd == TUNSETIFINDEX) { 1914fb7589a1SPavel Emelyanov ret = -EPERM; 1915fb7589a1SPavel Emelyanov if (tun) 1916fb7589a1SPavel Emelyanov goto unlock; 1917fb7589a1SPavel Emelyanov 1918fb7589a1SPavel Emelyanov ret = -EFAULT; 1919fb7589a1SPavel Emelyanov if (copy_from_user(&ifindex, argp, sizeof(ifindex))) 1920fb7589a1SPavel Emelyanov goto unlock; 1921fb7589a1SPavel Emelyanov 1922fb7589a1SPavel Emelyanov ret = 0; 1923fb7589a1SPavel Emelyanov tfile->ifindex = ifindex; 1924fb7589a1SPavel Emelyanov goto unlock; 1925fb7589a1SPavel Emelyanov } 19261da177e4SLinus Torvalds 1927876bfd4dSHerbert Xu ret = -EBADFD; 19281da177e4SLinus Torvalds if (!tun) 1929876bfd4dSHerbert Xu goto unlock; 19301da177e4SLinus Torvalds 19311e588338SJason Wang tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd); 19321da177e4SLinus Torvalds 1933631ab46bSEric W. Biederman ret = 0; 19341da177e4SLinus Torvalds switch (cmd) { 1935e3b99556SMark McLoughlin case TUNGETIFF: 19369ce99cf6SRami Rosen tun_get_iff(current->nsproxy->net_ns, tun, &ifr); 1937e3b99556SMark McLoughlin 19383d407a80SPavel Emelyanov if (tfile->detached) 19393d407a80SPavel Emelyanov ifr.ifr_flags |= IFF_DETACH_QUEUE; 1940849c9b6fSPavel Emelyanov if (!tfile->socket.sk->sk_filter) 1941849c9b6fSPavel Emelyanov ifr.ifr_flags |= IFF_NOFILTER; 19423d407a80SPavel Emelyanov 194350857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 1944631ab46bSEric W. Biederman ret = -EFAULT; 1945e3b99556SMark McLoughlin break; 1946e3b99556SMark McLoughlin 19471da177e4SLinus Torvalds case TUNSETNOCSUM: 19481da177e4SLinus Torvalds /* Disable/Enable checksum */ 19491da177e4SLinus Torvalds 195088255375SMichał Mirosław /* [unimplemented] */ 195188255375SMichał Mirosław tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n", 19526b8a66eeSJoe Perches arg ? "disabled" : "enabled"); 19531da177e4SLinus Torvalds break; 19541da177e4SLinus Torvalds 19551da177e4SLinus Torvalds case TUNSETPERSIST: 195654f968d6SJason Wang /* Disable/Enable persist mode. Keep an extra reference to the 195754f968d6SJason Wang * module to prevent the module being unprobed. 195854f968d6SJason Wang */ 1959dd38bd85SJason Wang if (arg && !(tun->flags & TUN_PERSIST)) { 19601da177e4SLinus Torvalds tun->flags |= TUN_PERSIST; 196154f968d6SJason Wang __module_get(THIS_MODULE); 1962dd38bd85SJason Wang } 1963dd38bd85SJason Wang if (!arg && (tun->flags & TUN_PERSIST)) { 19641da177e4SLinus Torvalds tun->flags &= ~TUN_PERSIST; 196554f968d6SJason Wang module_put(THIS_MODULE); 196654f968d6SJason Wang } 19671da177e4SLinus Torvalds 19686b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "persist %s\n", 19696b8a66eeSJoe Perches arg ? "enabled" : "disabled"); 19701da177e4SLinus Torvalds break; 19711da177e4SLinus Torvalds 19721da177e4SLinus Torvalds case TUNSETOWNER: 19731da177e4SLinus Torvalds /* Set owner of the device */ 19740625c883SEric W. Biederman owner = make_kuid(current_user_ns(), arg); 19750625c883SEric W. Biederman if (!uid_valid(owner)) { 19760625c883SEric W. Biederman ret = -EINVAL; 19770625c883SEric W. Biederman break; 19780625c883SEric W. Biederman } 19790625c883SEric W. Biederman tun->owner = owner; 19801e588338SJason Wang tun_debug(KERN_INFO, tun, "owner set to %u\n", 19810625c883SEric W. Biederman from_kuid(&init_user_ns, tun->owner)); 19821da177e4SLinus Torvalds break; 19831da177e4SLinus Torvalds 19848c644623SGuido Guenther case TUNSETGROUP: 19858c644623SGuido Guenther /* Set group of the device */ 19860625c883SEric W. Biederman group = make_kgid(current_user_ns(), arg); 19870625c883SEric W. Biederman if (!gid_valid(group)) { 19880625c883SEric W. Biederman ret = -EINVAL; 19890625c883SEric W. Biederman break; 19900625c883SEric W. Biederman } 19910625c883SEric W. Biederman tun->group = group; 19921e588338SJason Wang tun_debug(KERN_INFO, tun, "group set to %u\n", 19930625c883SEric W. Biederman from_kgid(&init_user_ns, tun->group)); 19948c644623SGuido Guenther break; 19958c644623SGuido Guenther 1996ff4cc3acSMike Kershaw case TUNSETLINK: 1997ff4cc3acSMike Kershaw /* Only allow setting the type when the interface is down */ 1998ff4cc3acSMike Kershaw if (tun->dev->flags & IFF_UP) { 19996b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, 20006b8a66eeSJoe Perches "Linktype set failed because interface is up\n"); 200148abfe05SDavid S. Miller ret = -EBUSY; 2002ff4cc3acSMike Kershaw } else { 2003ff4cc3acSMike Kershaw tun->dev->type = (int) arg; 20046b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "linktype set to %d\n", 20056b8a66eeSJoe Perches tun->dev->type); 200648abfe05SDavid S. Miller ret = 0; 2007ff4cc3acSMike Kershaw } 2008631ab46bSEric W. Biederman break; 2009ff4cc3acSMike Kershaw 20101da177e4SLinus Torvalds #ifdef TUN_DEBUG 20111da177e4SLinus Torvalds case TUNSETDEBUG: 20121da177e4SLinus Torvalds tun->debug = arg; 20131da177e4SLinus Torvalds break; 20141da177e4SLinus Torvalds #endif 20155228ddc9SRusty Russell case TUNSETOFFLOAD: 201688255375SMichał Mirosław ret = set_offload(tun, arg); 2017631ab46bSEric W. Biederman break; 20185228ddc9SRusty Russell 2019f271b2ccSMax Krasnyansky case TUNSETTXFILTER: 2020f271b2ccSMax Krasnyansky /* Can be set only for TAPs */ 2021631ab46bSEric W. Biederman ret = -EINVAL; 2022f271b2ccSMax Krasnyansky if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) 2023631ab46bSEric W. Biederman break; 2024c0e5a8c2SHarvey Harrison ret = update_filter(&tun->txflt, (void __user *)arg); 2025631ab46bSEric W. Biederman break; 20261da177e4SLinus Torvalds 20271da177e4SLinus Torvalds case SIOCGIFHWADDR: 2028b595076aSUwe Kleine-König /* Get hw address */ 2029f271b2ccSMax Krasnyansky memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); 2030f271b2ccSMax Krasnyansky ifr.ifr_hwaddr.sa_family = tun->dev->type; 203150857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 2032631ab46bSEric W. Biederman ret = -EFAULT; 2033631ab46bSEric W. Biederman break; 20341da177e4SLinus Torvalds 20351da177e4SLinus Torvalds case SIOCSIFHWADDR: 2036f271b2ccSMax Krasnyansky /* Set hw address */ 20376b8a66eeSJoe Perches tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n", 20386b8a66eeSJoe Perches ifr.ifr_hwaddr.sa_data); 203940102371SKim B. Heino 204040102371SKim B. Heino ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); 2041631ab46bSEric W. Biederman break; 204233dccbb0SHerbert Xu 204333dccbb0SHerbert Xu case TUNGETSNDBUF: 204454f968d6SJason Wang sndbuf = tfile->socket.sk->sk_sndbuf; 204533dccbb0SHerbert Xu if (copy_to_user(argp, &sndbuf, sizeof(sndbuf))) 204633dccbb0SHerbert Xu ret = -EFAULT; 204733dccbb0SHerbert Xu break; 204833dccbb0SHerbert Xu 204933dccbb0SHerbert Xu case TUNSETSNDBUF: 205033dccbb0SHerbert Xu if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) { 205133dccbb0SHerbert Xu ret = -EFAULT; 205233dccbb0SHerbert Xu break; 205333dccbb0SHerbert Xu } 205433dccbb0SHerbert Xu 2055c8d68e6bSJason Wang tun->sndbuf = sndbuf; 2056c8d68e6bSJason Wang tun_set_sndbuf(tun); 205733dccbb0SHerbert Xu break; 205833dccbb0SHerbert Xu 2059d9d52b51SMichael S. Tsirkin case TUNGETVNETHDRSZ: 2060d9d52b51SMichael S. Tsirkin vnet_hdr_sz = tun->vnet_hdr_sz; 2061d9d52b51SMichael S. Tsirkin if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz))) 2062d9d52b51SMichael S. Tsirkin ret = -EFAULT; 2063d9d52b51SMichael S. Tsirkin break; 2064d9d52b51SMichael S. Tsirkin 2065d9d52b51SMichael S. Tsirkin case TUNSETVNETHDRSZ: 2066d9d52b51SMichael S. Tsirkin if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) { 2067d9d52b51SMichael S. Tsirkin ret = -EFAULT; 2068d9d52b51SMichael S. Tsirkin break; 2069d9d52b51SMichael S. Tsirkin } 2070d9d52b51SMichael S. Tsirkin if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) { 2071d9d52b51SMichael S. Tsirkin ret = -EINVAL; 2072d9d52b51SMichael S. Tsirkin break; 2073d9d52b51SMichael S. Tsirkin } 2074d9d52b51SMichael S. Tsirkin 2075d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = vnet_hdr_sz; 2076d9d52b51SMichael S. Tsirkin break; 2077d9d52b51SMichael S. Tsirkin 207899405162SMichael S. Tsirkin case TUNATTACHFILTER: 207999405162SMichael S. Tsirkin /* Can be set only for TAPs */ 208099405162SMichael S. Tsirkin ret = -EINVAL; 208199405162SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) 208299405162SMichael S. Tsirkin break; 208399405162SMichael S. Tsirkin ret = -EFAULT; 208454f968d6SJason Wang if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog))) 208599405162SMichael S. Tsirkin break; 208699405162SMichael S. Tsirkin 2087c8d68e6bSJason Wang ret = tun_attach_filter(tun); 208899405162SMichael S. Tsirkin break; 208999405162SMichael S. Tsirkin 209099405162SMichael S. Tsirkin case TUNDETACHFILTER: 209199405162SMichael S. Tsirkin /* Can be set only for TAPs */ 209299405162SMichael S. Tsirkin ret = -EINVAL; 209399405162SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) 209499405162SMichael S. Tsirkin break; 2095c8d68e6bSJason Wang ret = 0; 2096c8d68e6bSJason Wang tun_detach_filter(tun, tun->numqueues); 209799405162SMichael S. Tsirkin break; 209899405162SMichael S. Tsirkin 209976975e9cSPavel Emelyanov case TUNGETFILTER: 210076975e9cSPavel Emelyanov ret = -EINVAL; 210176975e9cSPavel Emelyanov if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) 210276975e9cSPavel Emelyanov break; 210376975e9cSPavel Emelyanov ret = -EFAULT; 210476975e9cSPavel Emelyanov if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog))) 210576975e9cSPavel Emelyanov break; 210676975e9cSPavel Emelyanov ret = 0; 210776975e9cSPavel Emelyanov break; 210876975e9cSPavel Emelyanov 21091da177e4SLinus Torvalds default: 2110631ab46bSEric W. Biederman ret = -EINVAL; 2111631ab46bSEric W. Biederman break; 2112ee289b64SJoe Perches } 21131da177e4SLinus Torvalds 2114876bfd4dSHerbert Xu unlock: 2115876bfd4dSHerbert Xu rtnl_unlock(); 2116876bfd4dSHerbert Xu if (tun) 2117631ab46bSEric W. Biederman tun_put(tun); 2118631ab46bSEric W. Biederman return ret; 21191da177e4SLinus Torvalds } 21201da177e4SLinus Torvalds 212150857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file, 212250857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 212350857e2aSArnd Bergmann { 212450857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq)); 212550857e2aSArnd Bergmann } 212650857e2aSArnd Bergmann 212750857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 212850857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file, 212950857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 213050857e2aSArnd Bergmann { 213150857e2aSArnd Bergmann switch (cmd) { 213250857e2aSArnd Bergmann case TUNSETIFF: 213350857e2aSArnd Bergmann case TUNGETIFF: 213450857e2aSArnd Bergmann case TUNSETTXFILTER: 213550857e2aSArnd Bergmann case TUNGETSNDBUF: 213650857e2aSArnd Bergmann case TUNSETSNDBUF: 213750857e2aSArnd Bergmann case SIOCGIFHWADDR: 213850857e2aSArnd Bergmann case SIOCSIFHWADDR: 213950857e2aSArnd Bergmann arg = (unsigned long)compat_ptr(arg); 214050857e2aSArnd Bergmann break; 214150857e2aSArnd Bergmann default: 214250857e2aSArnd Bergmann arg = (compat_ulong_t)arg; 214350857e2aSArnd Bergmann break; 214450857e2aSArnd Bergmann } 214550857e2aSArnd Bergmann 214650857e2aSArnd Bergmann /* 214750857e2aSArnd Bergmann * compat_ifreq is shorter than ifreq, so we must not access beyond 214850857e2aSArnd Bergmann * the end of that structure. All fields that are used in this 214950857e2aSArnd Bergmann * driver are compatible though, we don't need to convert the 215050857e2aSArnd Bergmann * contents. 215150857e2aSArnd Bergmann */ 215250857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq)); 215350857e2aSArnd Bergmann } 215450857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */ 215550857e2aSArnd Bergmann 21561da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on) 21571da177e4SLinus Torvalds { 215854f968d6SJason Wang struct tun_file *tfile = file->private_data; 21591da177e4SLinus Torvalds int ret; 21601da177e4SLinus Torvalds 216154f968d6SJason Wang if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) 21629d319522SJonathan Corbet goto out; 21631da177e4SLinus Torvalds 21641da177e4SLinus Torvalds if (on) { 2165e0b93eddSJeff Layton __f_setown(file, task_pid(current), PIDTYPE_PID, 0); 216654f968d6SJason Wang tfile->flags |= TUN_FASYNC; 21671da177e4SLinus Torvalds } else 216854f968d6SJason Wang tfile->flags &= ~TUN_FASYNC; 21699d319522SJonathan Corbet ret = 0; 21709d319522SJonathan Corbet out: 21719d319522SJonathan Corbet return ret; 21721da177e4SLinus Torvalds } 21731da177e4SLinus Torvalds 21741da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file) 21751da177e4SLinus Torvalds { 2176631ab46bSEric W. Biederman struct tun_file *tfile; 2177deed49fbSThomas Gleixner 21786b8a66eeSJoe Perches DBG1(KERN_INFO, "tunX: tun_chr_open\n"); 2179631ab46bSEric W. Biederman 218054f968d6SJason Wang tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL, 218154f968d6SJason Wang &tun_proto); 2182631ab46bSEric W. Biederman if (!tfile) 2183631ab46bSEric W. Biederman return -ENOMEM; 2184c956674bSMonam Agarwal RCU_INIT_POINTER(tfile->tun, NULL); 218536b50babSEric W. Biederman tfile->net = get_net(current->nsproxy->net_ns); 218654f968d6SJason Wang tfile->flags = 0; 2187fb7589a1SPavel Emelyanov tfile->ifindex = 0; 218854f968d6SJason Wang 218954f968d6SJason Wang init_waitqueue_head(&tfile->wq.wait); 21909e641bdcSXi Wang RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq); 219154f968d6SJason Wang 219254f968d6SJason Wang tfile->socket.file = file; 219354f968d6SJason Wang tfile->socket.ops = &tun_socket_ops; 219454f968d6SJason Wang 219554f968d6SJason Wang sock_init_data(&tfile->socket, &tfile->sk); 219654f968d6SJason Wang sk_change_net(&tfile->sk, tfile->net); 219754f968d6SJason Wang 219854f968d6SJason Wang tfile->sk.sk_write_space = tun_sock_write_space; 219954f968d6SJason Wang tfile->sk.sk_sndbuf = INT_MAX; 220054f968d6SJason Wang 2201631ab46bSEric W. Biederman file->private_data = tfile; 220254f968d6SJason Wang set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags); 22034008e97fSJason Wang INIT_LIST_HEAD(&tfile->next); 220454f968d6SJason Wang 220519a6afb2SJason Wang sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); 220619a6afb2SJason Wang 22071da177e4SLinus Torvalds return 0; 22081da177e4SLinus Torvalds } 22091da177e4SLinus Torvalds 22101da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file) 22111da177e4SLinus Torvalds { 2212631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 221354f968d6SJason Wang struct net *net = tfile->net; 22141da177e4SLinus Torvalds 2215c8d68e6bSJason Wang tun_detach(tfile, true); 221654f968d6SJason Wang put_net(net); 22171da177e4SLinus Torvalds 22181da177e4SLinus Torvalds return 0; 22191da177e4SLinus Torvalds } 22201da177e4SLinus Torvalds 222193e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS 222293e14b6dSMasatake YAMATO static int tun_chr_show_fdinfo(struct seq_file *m, struct file *f) 222393e14b6dSMasatake YAMATO { 222493e14b6dSMasatake YAMATO struct tun_struct *tun; 222593e14b6dSMasatake YAMATO struct ifreq ifr; 222693e14b6dSMasatake YAMATO 222793e14b6dSMasatake YAMATO memset(&ifr, 0, sizeof(ifr)); 222893e14b6dSMasatake YAMATO 222993e14b6dSMasatake YAMATO rtnl_lock(); 223093e14b6dSMasatake YAMATO tun = tun_get(f); 223193e14b6dSMasatake YAMATO if (tun) 223293e14b6dSMasatake YAMATO tun_get_iff(current->nsproxy->net_ns, tun, &ifr); 223393e14b6dSMasatake YAMATO rtnl_unlock(); 223493e14b6dSMasatake YAMATO 223593e14b6dSMasatake YAMATO if (tun) 223693e14b6dSMasatake YAMATO tun_put(tun); 223793e14b6dSMasatake YAMATO 223893e14b6dSMasatake YAMATO return seq_printf(m, "iff:\t%s\n", ifr.ifr_name); 223993e14b6dSMasatake YAMATO } 224093e14b6dSMasatake YAMATO #endif 224193e14b6dSMasatake YAMATO 2242d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = { 22431da177e4SLinus Torvalds .owner = THIS_MODULE, 22441da177e4SLinus Torvalds .llseek = no_llseek, 2245ee0b3e67SBadari Pulavarty .read = do_sync_read, 2246ee0b3e67SBadari Pulavarty .aio_read = tun_chr_aio_read, 2247ee0b3e67SBadari Pulavarty .write = do_sync_write, 2248ee0b3e67SBadari Pulavarty .aio_write = tun_chr_aio_write, 22491da177e4SLinus Torvalds .poll = tun_chr_poll, 2250876bfd4dSHerbert Xu .unlocked_ioctl = tun_chr_ioctl, 225150857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 225250857e2aSArnd Bergmann .compat_ioctl = tun_chr_compat_ioctl, 225350857e2aSArnd Bergmann #endif 22541da177e4SLinus Torvalds .open = tun_chr_open, 22551da177e4SLinus Torvalds .release = tun_chr_close, 225693e14b6dSMasatake YAMATO .fasync = tun_chr_fasync, 225793e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS 225893e14b6dSMasatake YAMATO .show_fdinfo = tun_chr_show_fdinfo, 225993e14b6dSMasatake YAMATO #endif 22601da177e4SLinus Torvalds }; 22611da177e4SLinus Torvalds 22621da177e4SLinus Torvalds static struct miscdevice tun_miscdev = { 22631da177e4SLinus Torvalds .minor = TUN_MINOR, 22641da177e4SLinus Torvalds .name = "tun", 2265e454cea2SKay Sievers .nodename = "net/tun", 22661da177e4SLinus Torvalds .fops = &tun_fops, 22671da177e4SLinus Torvalds }; 22681da177e4SLinus Torvalds 22691da177e4SLinus Torvalds /* ethtool interface */ 22701da177e4SLinus Torvalds 22711da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 22721da177e4SLinus Torvalds { 22731da177e4SLinus Torvalds cmd->supported = 0; 22741da177e4SLinus Torvalds cmd->advertising = 0; 227570739497SDavid Decotigny ethtool_cmd_speed_set(cmd, SPEED_10); 22761da177e4SLinus Torvalds cmd->duplex = DUPLEX_FULL; 22771da177e4SLinus Torvalds cmd->port = PORT_TP; 22781da177e4SLinus Torvalds cmd->phy_address = 0; 22791da177e4SLinus Torvalds cmd->transceiver = XCVR_INTERNAL; 22801da177e4SLinus Torvalds cmd->autoneg = AUTONEG_DISABLE; 22811da177e4SLinus Torvalds cmd->maxtxpkt = 0; 22821da177e4SLinus Torvalds cmd->maxrxpkt = 0; 22831da177e4SLinus Torvalds return 0; 22841da177e4SLinus Torvalds } 22851da177e4SLinus Torvalds 22861da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) 22871da177e4SLinus Torvalds { 22881da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 22891da177e4SLinus Torvalds 229033a5ba14SRick Jones strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); 229133a5ba14SRick Jones strlcpy(info->version, DRV_VERSION, sizeof(info->version)); 22921da177e4SLinus Torvalds 22931da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 22941da177e4SLinus Torvalds case TUN_TUN_DEV: 229533a5ba14SRick Jones strlcpy(info->bus_info, "tun", sizeof(info->bus_info)); 22961da177e4SLinus Torvalds break; 22971da177e4SLinus Torvalds case TUN_TAP_DEV: 229833a5ba14SRick Jones strlcpy(info->bus_info, "tap", sizeof(info->bus_info)); 22991da177e4SLinus Torvalds break; 23001da177e4SLinus Torvalds } 23011da177e4SLinus Torvalds } 23021da177e4SLinus Torvalds 23031da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev) 23041da177e4SLinus Torvalds { 23051da177e4SLinus Torvalds #ifdef TUN_DEBUG 23061da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 23071da177e4SLinus Torvalds return tun->debug; 23081da177e4SLinus Torvalds #else 23091da177e4SLinus Torvalds return -EOPNOTSUPP; 23101da177e4SLinus Torvalds #endif 23111da177e4SLinus Torvalds } 23121da177e4SLinus Torvalds 23131da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value) 23141da177e4SLinus Torvalds { 23151da177e4SLinus Torvalds #ifdef TUN_DEBUG 23161da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 23171da177e4SLinus Torvalds tun->debug = value; 23181da177e4SLinus Torvalds #endif 23191da177e4SLinus Torvalds } 23201da177e4SLinus Torvalds 23217282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = { 23221da177e4SLinus Torvalds .get_settings = tun_get_settings, 23231da177e4SLinus Torvalds .get_drvinfo = tun_get_drvinfo, 23241da177e4SLinus Torvalds .get_msglevel = tun_get_msglevel, 23251da177e4SLinus Torvalds .set_msglevel = tun_set_msglevel, 2326bee31369SNolan Leake .get_link = ethtool_op_get_link, 2327eda29772SRichard Cochran .get_ts_info = ethtool_op_get_ts_info, 23281da177e4SLinus Torvalds }; 23291da177e4SLinus Torvalds 233079d17604SPavel Emelyanov 23311da177e4SLinus Torvalds static int __init tun_init(void) 23321da177e4SLinus Torvalds { 23331da177e4SLinus Torvalds int ret = 0; 23341da177e4SLinus Torvalds 23356b8a66eeSJoe Perches pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION); 23366b8a66eeSJoe Perches pr_info("%s\n", DRV_COPYRIGHT); 23371da177e4SLinus Torvalds 2338f019a7a5SEric W. Biederman ret = rtnl_link_register(&tun_link_ops); 233979d17604SPavel Emelyanov if (ret) { 23406b8a66eeSJoe Perches pr_err("Can't register link_ops\n"); 2341f019a7a5SEric W. Biederman goto err_linkops; 234279d17604SPavel Emelyanov } 234379d17604SPavel Emelyanov 23441da177e4SLinus Torvalds ret = misc_register(&tun_miscdev); 234579d17604SPavel Emelyanov if (ret) { 23466b8a66eeSJoe Perches pr_err("Can't register misc device %d\n", TUN_MINOR); 234779d17604SPavel Emelyanov goto err_misc; 234879d17604SPavel Emelyanov } 234979d17604SPavel Emelyanov return 0; 235079d17604SPavel Emelyanov err_misc: 2351f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 2352f019a7a5SEric W. Biederman err_linkops: 23531da177e4SLinus Torvalds return ret; 23541da177e4SLinus Torvalds } 23551da177e4SLinus Torvalds 23561da177e4SLinus Torvalds static void tun_cleanup(void) 23571da177e4SLinus Torvalds { 23581da177e4SLinus Torvalds misc_deregister(&tun_miscdev); 2359f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 23601da177e4SLinus Torvalds } 23611da177e4SLinus Torvalds 236205c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file. Returns error unless file is 236305c2828cSMichael S. Tsirkin * attached to a device. The returned object works like a packet socket, it 236405c2828cSMichael S. Tsirkin * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for 236505c2828cSMichael S. Tsirkin * holding a reference to the file for as long as the socket is in use. */ 236605c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file) 236705c2828cSMichael S. Tsirkin { 23686e914fc7SJason Wang struct tun_file *tfile; 236905c2828cSMichael S. Tsirkin if (file->f_op != &tun_fops) 237005c2828cSMichael S. Tsirkin return ERR_PTR(-EINVAL); 23716e914fc7SJason Wang tfile = file->private_data; 23726e914fc7SJason Wang if (!tfile) 237305c2828cSMichael S. Tsirkin return ERR_PTR(-EBADFD); 237454f968d6SJason Wang return &tfile->socket; 237505c2828cSMichael S. Tsirkin } 237605c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket); 237705c2828cSMichael S. Tsirkin 23781da177e4SLinus Torvalds module_init(tun_init); 23791da177e4SLinus Torvalds module_exit(tun_cleanup); 23801da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION); 23811da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT); 23821da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 23831da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR); 2384578454ffSKay Sievers MODULE_ALIAS("devname:net/tun"); 2385