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> 631da177e4SLinus Torvalds #include <linux/crc32.h> 64d647a591SPavel Emelyanov #include <linux/nsproxy.h> 65f43798c2SRusty Russell #include <linux/virtio_net.h> 6699405162SMichael S. Tsirkin #include <linux/rcupdate.h> 67881d966bSEric W. Biederman #include <net/net_namespace.h> 6879d17604SPavel Emelyanov #include <net/netns/generic.h> 69f019a7a5SEric W. Biederman #include <net/rtnetlink.h> 7033dccbb0SHerbert Xu #include <net/sock.h> 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds #include <asm/uaccess.h> 731da177e4SLinus Torvalds 7414daa021SRusty Russell /* Uncomment to enable debugging */ 7514daa021SRusty Russell /* #define TUN_DEBUG 1 */ 7614daa021SRusty Russell 771da177e4SLinus Torvalds #ifdef TUN_DEBUG 781da177e4SLinus Torvalds static int debug; 7914daa021SRusty Russell 806b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...) \ 816b8a66eeSJoe Perches do { \ 826b8a66eeSJoe Perches if (tun->debug) \ 836b8a66eeSJoe Perches netdev_printk(level, tun->dev, fmt, ##args); \ 846b8a66eeSJoe Perches } while (0) 856b8a66eeSJoe Perches #define DBG1(level, fmt, args...) \ 866b8a66eeSJoe Perches do { \ 876b8a66eeSJoe Perches if (debug == 2) \ 886b8a66eeSJoe Perches printk(level fmt, ##args); \ 896b8a66eeSJoe Perches } while (0) 9014daa021SRusty Russell #else 916b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...) \ 926b8a66eeSJoe Perches do { \ 936b8a66eeSJoe Perches if (0) \ 946b8a66eeSJoe Perches netdev_printk(level, tun->dev, fmt, ##args); \ 956b8a66eeSJoe Perches } while (0) 966b8a66eeSJoe Perches #define DBG1(level, fmt, args...) \ 976b8a66eeSJoe Perches do { \ 986b8a66eeSJoe Perches if (0) \ 996b8a66eeSJoe Perches printk(level fmt, ##args); \ 1006b8a66eeSJoe Perches } while (0) 1011da177e4SLinus Torvalds #endif 1021da177e4SLinus Torvalds 1030690899bSMichael S. Tsirkin #define GOODCOPY_LEN 128 1040690899bSMichael S. Tsirkin 105f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8 106f271b2ccSMax Krasnyansky struct tap_filter { 107f271b2ccSMax Krasnyansky unsigned int count; /* Number of addrs. Zero means disabled */ 108f271b2ccSMax Krasnyansky u32 mask[2]; /* Mask of the hashed addrs */ 109f271b2ccSMax Krasnyansky unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN]; 110f271b2ccSMax Krasnyansky }; 111f271b2ccSMax Krasnyansky 112c8d68e6bSJason Wang /* 1024 is probably a high enough limit: modern hypervisors seem to support on 113c8d68e6bSJason Wang * the order of 100-200 CPUs so this leaves us some breathing space if we want 114c8d68e6bSJason Wang * to match a queue per guest CPU. 115c8d68e6bSJason Wang */ 116c8d68e6bSJason Wang #define MAX_TAP_QUEUES 1024 117c8d68e6bSJason Wang 11896442e42SJason Wang #define TUN_FLOW_EXPIRE (3 * HZ) 11996442e42SJason Wang 12054f968d6SJason Wang /* A tun_file connects an open character device to a tuntap netdevice. It 12154f968d6SJason Wang * also contains all socket related strctures (except sock_fprog and tap_filter) 12254f968d6SJason Wang * to serve as one transmit queue for tuntap device. The sock_fprog and 12354f968d6SJason Wang * tap_filter were kept in tun_struct since they were used for filtering for the 124*36fe8c09SRami Rosen * netdevice not for a specific queue (at least I didn't see the requirement for 12554f968d6SJason Wang * this). 1266e914fc7SJason Wang * 1276e914fc7SJason Wang * RCU usage: 128*36fe8c09SRami Rosen * The tun_file and tun_struct are loosely coupled, the pointer from one to the 1296e914fc7SJason Wang * other can only be read while rcu_read_lock or rtnl_lock is held. 13054f968d6SJason Wang */ 131631ab46bSEric W. Biederman struct tun_file { 13254f968d6SJason Wang struct sock sk; 13354f968d6SJason Wang struct socket socket; 13454f968d6SJason Wang struct socket_wq wq; 1356e914fc7SJason Wang struct tun_struct __rcu *tun; 13636b50babSEric W. Biederman struct net *net; 13754f968d6SJason Wang struct fasync_struct *fasync; 13854f968d6SJason Wang /* only used for fasnyc */ 13954f968d6SJason Wang unsigned int flags; 140c8d68e6bSJason Wang u16 queue_index; 141631ab46bSEric W. Biederman }; 142631ab46bSEric W. Biederman 14396442e42SJason Wang struct tun_flow_entry { 14496442e42SJason Wang struct hlist_node hash_link; 14596442e42SJason Wang struct rcu_head rcu; 14696442e42SJason Wang struct tun_struct *tun; 14796442e42SJason Wang 14896442e42SJason Wang u32 rxhash; 14996442e42SJason Wang int queue_index; 15096442e42SJason Wang unsigned long updated; 15196442e42SJason Wang }; 15296442e42SJason Wang 15396442e42SJason Wang #define TUN_NUM_FLOW_ENTRIES 1024 15496442e42SJason Wang 15554f968d6SJason Wang /* Since the socket were moved to tun_file, to preserve the behavior of persist 156*36fe8c09SRami Rosen * device, socket filter, sndbuf and vnet header size were restore when the 15754f968d6SJason Wang * file were attached to a persist device. 15854f968d6SJason Wang */ 15914daa021SRusty Russell struct tun_struct { 160c8d68e6bSJason Wang struct tun_file __rcu *tfiles[MAX_TAP_QUEUES]; 161c8d68e6bSJason Wang unsigned int numqueues; 162f271b2ccSMax Krasnyansky unsigned int flags; 1630625c883SEric W. Biederman kuid_t owner; 1640625c883SEric W. Biederman kgid_t group; 16514daa021SRusty Russell 16614daa021SRusty Russell struct net_device *dev; 167c8f44affSMichał Mirosław netdev_features_t set_features; 16888255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \ 16988255375SMichał Mirosław NETIF_F_TSO6|NETIF_F_UFO) 170d9d52b51SMichael S. Tsirkin 171d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 17254f968d6SJason Wang int sndbuf; 17354f968d6SJason Wang struct tap_filter txflt; 17454f968d6SJason Wang struct sock_fprog fprog; 17554f968d6SJason Wang /* protected by rtnl lock */ 17654f968d6SJason Wang bool filter_attached; 17714daa021SRusty Russell #ifdef TUN_DEBUG 17814daa021SRusty Russell int debug; 17914daa021SRusty Russell #endif 18096442e42SJason Wang spinlock_t lock; 18196442e42SJason Wang struct kmem_cache *flow_cache; 18296442e42SJason Wang struct hlist_head flows[TUN_NUM_FLOW_ENTRIES]; 18396442e42SJason Wang struct timer_list flow_gc_timer; 18496442e42SJason Wang unsigned long ageing_time; 18514daa021SRusty Russell }; 18614daa021SRusty Russell 18796442e42SJason Wang static inline u32 tun_hashfn(u32 rxhash) 18896442e42SJason Wang { 18996442e42SJason Wang return rxhash & 0x3ff; 19096442e42SJason Wang } 19196442e42SJason Wang 19296442e42SJason Wang static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash) 19396442e42SJason Wang { 19496442e42SJason Wang struct tun_flow_entry *e; 19596442e42SJason Wang struct hlist_node *n; 19696442e42SJason Wang 19796442e42SJason Wang hlist_for_each_entry_rcu(e, n, head, hash_link) { 19896442e42SJason Wang if (e->rxhash == rxhash) 19996442e42SJason Wang return e; 20096442e42SJason Wang } 20196442e42SJason Wang return NULL; 20296442e42SJason Wang } 20396442e42SJason Wang 20496442e42SJason Wang static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun, 20596442e42SJason Wang struct hlist_head *head, 20696442e42SJason Wang u32 rxhash, u16 queue_index) 20796442e42SJason Wang { 20896442e42SJason Wang struct tun_flow_entry *e = kmem_cache_alloc(tun->flow_cache, 20996442e42SJason Wang GFP_ATOMIC); 21096442e42SJason Wang if (e) { 21196442e42SJason Wang tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n", 21296442e42SJason Wang rxhash, queue_index); 21396442e42SJason Wang e->updated = jiffies; 21496442e42SJason Wang e->rxhash = rxhash; 21596442e42SJason Wang e->queue_index = queue_index; 21696442e42SJason Wang e->tun = tun; 21796442e42SJason Wang hlist_add_head_rcu(&e->hash_link, head); 21896442e42SJason Wang } 21996442e42SJason Wang return e; 22096442e42SJason Wang } 22196442e42SJason Wang 22296442e42SJason Wang static void tun_flow_free(struct rcu_head *head) 22396442e42SJason Wang { 22496442e42SJason Wang struct tun_flow_entry *e 22596442e42SJason Wang = container_of(head, struct tun_flow_entry, rcu); 22696442e42SJason Wang kmem_cache_free(e->tun->flow_cache, e); 22796442e42SJason Wang } 22896442e42SJason Wang 22996442e42SJason Wang static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e) 23096442e42SJason Wang { 23196442e42SJason Wang tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n", 23296442e42SJason Wang e->rxhash, e->queue_index); 23396442e42SJason Wang hlist_del_rcu(&e->hash_link); 23496442e42SJason Wang call_rcu(&e->rcu, tun_flow_free); 23596442e42SJason Wang } 23696442e42SJason Wang 23796442e42SJason Wang static void tun_flow_flush(struct tun_struct *tun) 23896442e42SJason Wang { 23996442e42SJason Wang int i; 24096442e42SJason Wang 24196442e42SJason Wang spin_lock_bh(&tun->lock); 24296442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 24396442e42SJason Wang struct tun_flow_entry *e; 24496442e42SJason Wang struct hlist_node *h, *n; 24596442e42SJason Wang 24696442e42SJason Wang hlist_for_each_entry_safe(e, h, n, &tun->flows[i], hash_link) 24796442e42SJason Wang tun_flow_delete(tun, e); 24896442e42SJason Wang } 24996442e42SJason Wang spin_unlock_bh(&tun->lock); 25096442e42SJason Wang } 25196442e42SJason Wang 25296442e42SJason Wang static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index) 25396442e42SJason Wang { 25496442e42SJason Wang int i; 25596442e42SJason Wang 25696442e42SJason Wang spin_lock_bh(&tun->lock); 25796442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 25896442e42SJason Wang struct tun_flow_entry *e; 25996442e42SJason Wang struct hlist_node *h, *n; 26096442e42SJason Wang 26196442e42SJason Wang hlist_for_each_entry_safe(e, h, n, &tun->flows[i], hash_link) { 26296442e42SJason Wang if (e->queue_index == queue_index) 26396442e42SJason Wang tun_flow_delete(tun, e); 26496442e42SJason Wang } 26596442e42SJason Wang } 26696442e42SJason Wang spin_unlock_bh(&tun->lock); 26796442e42SJason Wang } 26896442e42SJason Wang 26996442e42SJason Wang static void tun_flow_cleanup(unsigned long data) 27096442e42SJason Wang { 27196442e42SJason Wang struct tun_struct *tun = (struct tun_struct *)data; 27296442e42SJason Wang unsigned long delay = tun->ageing_time; 27396442e42SJason Wang unsigned long next_timer = jiffies + delay; 27496442e42SJason Wang unsigned long count = 0; 27596442e42SJason Wang int i; 27696442e42SJason Wang 27796442e42SJason Wang tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n"); 27896442e42SJason Wang 27996442e42SJason Wang spin_lock_bh(&tun->lock); 28096442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 28196442e42SJason Wang struct tun_flow_entry *e; 28296442e42SJason Wang struct hlist_node *h, *n; 28396442e42SJason Wang 28496442e42SJason Wang hlist_for_each_entry_safe(e, h, n, &tun->flows[i], hash_link) { 28596442e42SJason Wang unsigned long this_timer; 28696442e42SJason Wang count++; 28796442e42SJason Wang this_timer = e->updated + delay; 28896442e42SJason Wang if (time_before_eq(this_timer, jiffies)) 28996442e42SJason Wang tun_flow_delete(tun, e); 29096442e42SJason Wang else if (time_before(this_timer, next_timer)) 29196442e42SJason Wang next_timer = this_timer; 29296442e42SJason Wang } 29396442e42SJason Wang } 29496442e42SJason Wang 29596442e42SJason Wang if (count) 29696442e42SJason Wang mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer)); 29796442e42SJason Wang spin_unlock_bh(&tun->lock); 29896442e42SJason Wang } 29996442e42SJason Wang 30096442e42SJason Wang static void tun_flow_update(struct tun_struct *tun, struct sk_buff *skb, 30196442e42SJason Wang u16 queue_index) 30296442e42SJason Wang { 30396442e42SJason Wang struct hlist_head *head; 30496442e42SJason Wang struct tun_flow_entry *e; 30596442e42SJason Wang unsigned long delay = tun->ageing_time; 30696442e42SJason Wang u32 rxhash = skb_get_rxhash(skb); 30796442e42SJason Wang 30896442e42SJason Wang if (!rxhash) 30996442e42SJason Wang return; 31096442e42SJason Wang else 31196442e42SJason Wang head = &tun->flows[tun_hashfn(rxhash)]; 31296442e42SJason Wang 31396442e42SJason Wang rcu_read_lock(); 31496442e42SJason Wang 31596442e42SJason Wang if (tun->numqueues == 1) 31696442e42SJason Wang goto unlock; 31796442e42SJason Wang 31896442e42SJason Wang e = tun_flow_find(head, rxhash); 31996442e42SJason Wang if (likely(e)) { 32096442e42SJason Wang /* TODO: keep queueing to old queue until it's empty? */ 32196442e42SJason Wang e->queue_index = queue_index; 32296442e42SJason Wang e->updated = jiffies; 32396442e42SJason Wang } else { 32496442e42SJason Wang spin_lock_bh(&tun->lock); 32596442e42SJason Wang if (!tun_flow_find(head, rxhash)) 32696442e42SJason Wang tun_flow_create(tun, head, rxhash, queue_index); 32796442e42SJason Wang 32896442e42SJason Wang if (!timer_pending(&tun->flow_gc_timer)) 32996442e42SJason Wang mod_timer(&tun->flow_gc_timer, 33096442e42SJason Wang round_jiffies_up(jiffies + delay)); 33196442e42SJason Wang spin_unlock_bh(&tun->lock); 33296442e42SJason Wang } 33396442e42SJason Wang 33496442e42SJason Wang unlock: 33596442e42SJason Wang rcu_read_unlock(); 33696442e42SJason Wang } 33796442e42SJason Wang 338c8d68e6bSJason Wang /* We try to identify a flow through its rxhash first. The reason that 339c8d68e6bSJason Wang * we do not check rxq no. is becuase some cards(e.g 82599), chooses 340c8d68e6bSJason Wang * the rxq based on the txq where the last packet of the flow comes. As 341c8d68e6bSJason Wang * the userspace application move between processors, we may get a 342c8d68e6bSJason Wang * different rxq no. here. If we could not get rxhash, then we would 343c8d68e6bSJason Wang * hope the rxq no. may help here. 344c8d68e6bSJason Wang */ 345c8d68e6bSJason Wang static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb) 346c8d68e6bSJason Wang { 347c8d68e6bSJason Wang struct tun_struct *tun = netdev_priv(dev); 34896442e42SJason Wang struct tun_flow_entry *e; 349c8d68e6bSJason Wang u32 txq = 0; 350c8d68e6bSJason Wang u32 numqueues = 0; 351c8d68e6bSJason Wang 352c8d68e6bSJason Wang rcu_read_lock(); 353c8d68e6bSJason Wang numqueues = tun->numqueues; 354c8d68e6bSJason Wang 355c8d68e6bSJason Wang txq = skb_get_rxhash(skb); 356c8d68e6bSJason Wang if (txq) { 35796442e42SJason Wang e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq); 35896442e42SJason Wang if (e) 35996442e42SJason Wang txq = e->queue_index; 36096442e42SJason Wang else 361c8d68e6bSJason Wang /* use multiply and shift instead of expensive divide */ 362c8d68e6bSJason Wang txq = ((u64)txq * numqueues) >> 32; 363c8d68e6bSJason Wang } else if (likely(skb_rx_queue_recorded(skb))) { 364c8d68e6bSJason Wang txq = skb_get_rx_queue(skb); 365c8d68e6bSJason Wang while (unlikely(txq >= numqueues)) 366c8d68e6bSJason Wang txq -= numqueues; 367c8d68e6bSJason Wang } 368c8d68e6bSJason Wang 369c8d68e6bSJason Wang rcu_read_unlock(); 370c8d68e6bSJason Wang return txq; 371c8d68e6bSJason Wang } 372c8d68e6bSJason Wang 373cde8b15fSJason Wang static inline bool tun_not_capable(struct tun_struct *tun) 374cde8b15fSJason Wang { 375cde8b15fSJason Wang const struct cred *cred = current_cred(); 376c260b772SEric W. Biederman struct net *net = dev_net(tun->dev); 377cde8b15fSJason Wang 378cde8b15fSJason Wang return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) || 379cde8b15fSJason Wang (gid_valid(tun->group) && !in_egroup_p(tun->group))) && 380c260b772SEric W. Biederman !ns_capable(net->user_ns, CAP_NET_ADMIN); 381cde8b15fSJason Wang } 382cde8b15fSJason Wang 383c8d68e6bSJason Wang static void tun_set_real_num_queues(struct tun_struct *tun) 384c8d68e6bSJason Wang { 385c8d68e6bSJason Wang netif_set_real_num_tx_queues(tun->dev, tun->numqueues); 386c8d68e6bSJason Wang netif_set_real_num_rx_queues(tun->dev, tun->numqueues); 387c8d68e6bSJason Wang } 388c8d68e6bSJason Wang 389c8d68e6bSJason Wang static void __tun_detach(struct tun_file *tfile, bool clean) 390c8d68e6bSJason Wang { 391c8d68e6bSJason Wang struct tun_file *ntfile; 392c8d68e6bSJason Wang struct tun_struct *tun; 393c8d68e6bSJason Wang struct net_device *dev; 394c8d68e6bSJason Wang 395c8d68e6bSJason Wang tun = rcu_dereference_protected(tfile->tun, 396c8d68e6bSJason Wang lockdep_rtnl_is_held()); 397c8d68e6bSJason Wang if (tun) { 398c8d68e6bSJason Wang u16 index = tfile->queue_index; 399c8d68e6bSJason Wang BUG_ON(index >= tun->numqueues); 400c8d68e6bSJason Wang dev = tun->dev; 401c8d68e6bSJason Wang 402c8d68e6bSJason Wang rcu_assign_pointer(tun->tfiles[index], 403c8d68e6bSJason Wang tun->tfiles[tun->numqueues - 1]); 404c8d68e6bSJason Wang rcu_assign_pointer(tfile->tun, NULL); 405c8d68e6bSJason Wang ntfile = rcu_dereference_protected(tun->tfiles[index], 406c8d68e6bSJason Wang lockdep_rtnl_is_held()); 407c8d68e6bSJason Wang ntfile->queue_index = index; 408c8d68e6bSJason Wang 409c8d68e6bSJason Wang --tun->numqueues; 410c8d68e6bSJason Wang sock_put(&tfile->sk); 411c8d68e6bSJason Wang 412c8d68e6bSJason Wang synchronize_net(); 41396442e42SJason Wang tun_flow_delete_by_queue(tun, tun->numqueues + 1); 414c8d68e6bSJason Wang /* Drop read queue */ 415c8d68e6bSJason Wang skb_queue_purge(&tfile->sk.sk_receive_queue); 416c8d68e6bSJason Wang tun_set_real_num_queues(tun); 417c8d68e6bSJason Wang 418c8d68e6bSJason Wang if (tun->numqueues == 0 && !(tun->flags & TUN_PERSIST)) 419c8d68e6bSJason Wang if (dev->reg_state == NETREG_REGISTERED) 420c8d68e6bSJason Wang unregister_netdevice(dev); 421c8d68e6bSJason Wang } 422c8d68e6bSJason Wang 423c8d68e6bSJason Wang if (clean) { 424c8d68e6bSJason Wang BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED, 425c8d68e6bSJason Wang &tfile->socket.flags)); 426c8d68e6bSJason Wang sk_release_kernel(&tfile->sk); 427c8d68e6bSJason Wang } 428c8d68e6bSJason Wang } 429c8d68e6bSJason Wang 430c8d68e6bSJason Wang static void tun_detach(struct tun_file *tfile, bool clean) 431c8d68e6bSJason Wang { 432c8d68e6bSJason Wang rtnl_lock(); 433c8d68e6bSJason Wang __tun_detach(tfile, clean); 434c8d68e6bSJason Wang rtnl_unlock(); 435c8d68e6bSJason Wang } 436c8d68e6bSJason Wang 437c8d68e6bSJason Wang static void tun_detach_all(struct net_device *dev) 438c8d68e6bSJason Wang { 439c8d68e6bSJason Wang struct tun_struct *tun = netdev_priv(dev); 440c8d68e6bSJason Wang struct tun_file *tfile; 441c8d68e6bSJason Wang int i, n = tun->numqueues; 442c8d68e6bSJason Wang 443c8d68e6bSJason Wang for (i = 0; i < n; i++) { 444c8d68e6bSJason Wang tfile = rcu_dereference_protected(tun->tfiles[i], 445c8d68e6bSJason Wang lockdep_rtnl_is_held()); 446c8d68e6bSJason Wang BUG_ON(!tfile); 447c8d68e6bSJason Wang wake_up_all(&tfile->wq.wait); 448c8d68e6bSJason Wang rcu_assign_pointer(tfile->tun, NULL); 449c8d68e6bSJason Wang --tun->numqueues; 450c8d68e6bSJason Wang } 451c8d68e6bSJason Wang BUG_ON(tun->numqueues != 0); 452c8d68e6bSJason Wang 453c8d68e6bSJason Wang synchronize_net(); 454c8d68e6bSJason Wang for (i = 0; i < n; i++) { 455c8d68e6bSJason Wang tfile = rcu_dereference_protected(tun->tfiles[i], 456c8d68e6bSJason Wang lockdep_rtnl_is_held()); 457c8d68e6bSJason Wang /* Drop read queue */ 458c8d68e6bSJason Wang skb_queue_purge(&tfile->sk.sk_receive_queue); 459c8d68e6bSJason Wang sock_put(&tfile->sk); 460c8d68e6bSJason Wang } 461c8d68e6bSJason Wang } 462c8d68e6bSJason Wang 463a7385ba2SEric W. Biederman static int tun_attach(struct tun_struct *tun, struct file *file) 464a7385ba2SEric W. Biederman { 465631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 46638231b7aSEric W. Biederman int err; 467a7385ba2SEric W. Biederman 46838231b7aSEric W. Biederman err = -EINVAL; 469c8d68e6bSJason Wang if (rcu_dereference_protected(tfile->tun, lockdep_rtnl_is_held())) 47038231b7aSEric W. Biederman goto out; 47138231b7aSEric W. Biederman 47238231b7aSEric W. Biederman err = -EBUSY; 473c8d68e6bSJason Wang if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1) 474c8d68e6bSJason Wang goto out; 475c8d68e6bSJason Wang 476c8d68e6bSJason Wang err = -E2BIG; 477c8d68e6bSJason Wang if (tun->numqueues == MAX_TAP_QUEUES) 47838231b7aSEric W. Biederman goto out; 47938231b7aSEric W. Biederman 48038231b7aSEric W. Biederman err = 0; 48154f968d6SJason Wang 482c8d68e6bSJason Wang /* Re-attach the filter to presist device */ 48354f968d6SJason Wang if (tun->filter_attached == true) { 48454f968d6SJason Wang err = sk_attach_filter(&tun->fprog, tfile->socket.sk); 48554f968d6SJason Wang if (!err) 48654f968d6SJason Wang goto out; 48754f968d6SJason Wang } 488c8d68e6bSJason Wang tfile->queue_index = tun->numqueues; 4896e914fc7SJason Wang rcu_assign_pointer(tfile->tun, tun); 490c8d68e6bSJason Wang rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); 49154f968d6SJason Wang sock_hold(&tfile->sk); 492c8d68e6bSJason Wang tun->numqueues++; 493c8d68e6bSJason Wang 494c8d68e6bSJason Wang tun_set_real_num_queues(tun); 495c8d68e6bSJason Wang 496c8d68e6bSJason Wang if (tun->numqueues == 1) 497c8d68e6bSJason Wang netif_carrier_on(tun->dev); 498c8d68e6bSJason Wang 499c8d68e6bSJason Wang /* device is allowed to go away first, so no need to hold extra 500c8d68e6bSJason Wang * refcnt. 501c8d68e6bSJason Wang */ 502a7385ba2SEric W. Biederman 50338231b7aSEric W. Biederman out: 50438231b7aSEric W. Biederman return err; 505a7385ba2SEric W. Biederman } 506a7385ba2SEric W. Biederman 507631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile) 508631ab46bSEric W. Biederman { 5096e914fc7SJason Wang struct tun_struct *tun; 510c70f1829SEric W. Biederman 5116e914fc7SJason Wang rcu_read_lock(); 5126e914fc7SJason Wang tun = rcu_dereference(tfile->tun); 5136e914fc7SJason Wang if (tun) 5146e914fc7SJason Wang dev_hold(tun->dev); 5156e914fc7SJason Wang rcu_read_unlock(); 516c70f1829SEric W. Biederman 517c70f1829SEric W. Biederman return tun; 518631ab46bSEric W. Biederman } 519631ab46bSEric W. Biederman 520631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file) 521631ab46bSEric W. Biederman { 522631ab46bSEric W. Biederman return __tun_get(file->private_data); 523631ab46bSEric W. Biederman } 524631ab46bSEric W. Biederman 525631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun) 526631ab46bSEric W. Biederman { 5276e914fc7SJason Wang dev_put(tun->dev); 528631ab46bSEric W. Biederman } 529631ab46bSEric W. Biederman 5306b8a66eeSJoe Perches /* TAP filtering */ 531f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr) 532f271b2ccSMax Krasnyansky { 533f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 534f271b2ccSMax Krasnyansky mask[n >> 5] |= (1 << (n & 31)); 535f271b2ccSMax Krasnyansky } 536f271b2ccSMax Krasnyansky 537f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) 538f271b2ccSMax Krasnyansky { 539f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 540f271b2ccSMax Krasnyansky return mask[n >> 5] & (1 << (n & 31)); 541f271b2ccSMax Krasnyansky } 542f271b2ccSMax Krasnyansky 543f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg) 544f271b2ccSMax Krasnyansky { 545f271b2ccSMax Krasnyansky struct { u8 u[ETH_ALEN]; } *addr; 546f271b2ccSMax Krasnyansky struct tun_filter uf; 547f271b2ccSMax Krasnyansky int err, alen, n, nexact; 548f271b2ccSMax Krasnyansky 549f271b2ccSMax Krasnyansky if (copy_from_user(&uf, arg, sizeof(uf))) 550f271b2ccSMax Krasnyansky return -EFAULT; 551f271b2ccSMax Krasnyansky 552f271b2ccSMax Krasnyansky if (!uf.count) { 553f271b2ccSMax Krasnyansky /* Disabled */ 554f271b2ccSMax Krasnyansky filter->count = 0; 555f271b2ccSMax Krasnyansky return 0; 556f271b2ccSMax Krasnyansky } 557f271b2ccSMax Krasnyansky 558f271b2ccSMax Krasnyansky alen = ETH_ALEN * uf.count; 559f271b2ccSMax Krasnyansky addr = kmalloc(alen, GFP_KERNEL); 560f271b2ccSMax Krasnyansky if (!addr) 561f271b2ccSMax Krasnyansky return -ENOMEM; 562f271b2ccSMax Krasnyansky 563f271b2ccSMax Krasnyansky if (copy_from_user(addr, arg + sizeof(uf), alen)) { 564f271b2ccSMax Krasnyansky err = -EFAULT; 565f271b2ccSMax Krasnyansky goto done; 566f271b2ccSMax Krasnyansky } 567f271b2ccSMax Krasnyansky 568f271b2ccSMax Krasnyansky /* The filter is updated without holding any locks. Which is 569f271b2ccSMax Krasnyansky * perfectly safe. We disable it first and in the worst 570f271b2ccSMax Krasnyansky * case we'll accept a few undesired packets. */ 571f271b2ccSMax Krasnyansky filter->count = 0; 572f271b2ccSMax Krasnyansky wmb(); 573f271b2ccSMax Krasnyansky 574f271b2ccSMax Krasnyansky /* Use first set of addresses as an exact filter */ 575f271b2ccSMax Krasnyansky for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++) 576f271b2ccSMax Krasnyansky memcpy(filter->addr[n], addr[n].u, ETH_ALEN); 577f271b2ccSMax Krasnyansky 578f271b2ccSMax Krasnyansky nexact = n; 579f271b2ccSMax Krasnyansky 580cfbf84fcSAlex Williamson /* Remaining multicast addresses are hashed, 581cfbf84fcSAlex Williamson * unicast will leave the filter disabled. */ 582f271b2ccSMax Krasnyansky memset(filter->mask, 0, sizeof(filter->mask)); 583cfbf84fcSAlex Williamson for (; n < uf.count; n++) { 584cfbf84fcSAlex Williamson if (!is_multicast_ether_addr(addr[n].u)) { 585cfbf84fcSAlex Williamson err = 0; /* no filter */ 586cfbf84fcSAlex Williamson goto done; 587cfbf84fcSAlex Williamson } 588f271b2ccSMax Krasnyansky addr_hash_set(filter->mask, addr[n].u); 589cfbf84fcSAlex Williamson } 590f271b2ccSMax Krasnyansky 591f271b2ccSMax Krasnyansky /* For ALLMULTI just set the mask to all ones. 592f271b2ccSMax Krasnyansky * This overrides the mask populated above. */ 593f271b2ccSMax Krasnyansky if ((uf.flags & TUN_FLT_ALLMULTI)) 594f271b2ccSMax Krasnyansky memset(filter->mask, ~0, sizeof(filter->mask)); 595f271b2ccSMax Krasnyansky 596f271b2ccSMax Krasnyansky /* Now enable the filter */ 597f271b2ccSMax Krasnyansky wmb(); 598f271b2ccSMax Krasnyansky filter->count = nexact; 599f271b2ccSMax Krasnyansky 600f271b2ccSMax Krasnyansky /* Return the number of exact filters */ 601f271b2ccSMax Krasnyansky err = nexact; 602f271b2ccSMax Krasnyansky 603f271b2ccSMax Krasnyansky done: 604f271b2ccSMax Krasnyansky kfree(addr); 605f271b2ccSMax Krasnyansky return err; 606f271b2ccSMax Krasnyansky } 607f271b2ccSMax Krasnyansky 608f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */ 609f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb) 610f271b2ccSMax Krasnyansky { 611f271b2ccSMax Krasnyansky /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect 612f271b2ccSMax Krasnyansky * at this point. */ 613f271b2ccSMax Krasnyansky struct ethhdr *eh = (struct ethhdr *) skb->data; 614f271b2ccSMax Krasnyansky int i; 615f271b2ccSMax Krasnyansky 616f271b2ccSMax Krasnyansky /* Exact match */ 617f271b2ccSMax Krasnyansky for (i = 0; i < filter->count; i++) 6182e42e474SJoe Perches if (ether_addr_equal(eh->h_dest, filter->addr[i])) 619f271b2ccSMax Krasnyansky return 1; 620f271b2ccSMax Krasnyansky 621f271b2ccSMax Krasnyansky /* Inexact match (multicast only) */ 622f271b2ccSMax Krasnyansky if (is_multicast_ether_addr(eh->h_dest)) 623f271b2ccSMax Krasnyansky return addr_hash_test(filter->mask, eh->h_dest); 624f271b2ccSMax Krasnyansky 625f271b2ccSMax Krasnyansky return 0; 626f271b2ccSMax Krasnyansky } 627f271b2ccSMax Krasnyansky 628f271b2ccSMax Krasnyansky /* 629f271b2ccSMax Krasnyansky * Checks whether the packet is accepted or not. 630f271b2ccSMax Krasnyansky * Returns: 0 - drop, !=0 - accept 631f271b2ccSMax Krasnyansky */ 632f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb) 633f271b2ccSMax Krasnyansky { 634f271b2ccSMax Krasnyansky if (!filter->count) 635f271b2ccSMax Krasnyansky return 1; 636f271b2ccSMax Krasnyansky 637f271b2ccSMax Krasnyansky return run_filter(filter, skb); 638f271b2ccSMax Krasnyansky } 639f271b2ccSMax Krasnyansky 6401da177e4SLinus Torvalds /* Network device part of the driver */ 6411da177e4SLinus Torvalds 6427282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops; 6431da177e4SLinus Torvalds 644c70f1829SEric W. Biederman /* Net device detach from fd. */ 645c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev) 646c70f1829SEric W. Biederman { 647c8d68e6bSJason Wang tun_detach_all(dev); 648c70f1829SEric W. Biederman } 649c70f1829SEric W. Biederman 6501da177e4SLinus Torvalds /* Net device open. */ 6511da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev) 6521da177e4SLinus Torvalds { 653c8d68e6bSJason Wang netif_tx_start_all_queues(dev); 6541da177e4SLinus Torvalds return 0; 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds /* Net device close. */ 6581da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev) 6591da177e4SLinus Torvalds { 660c8d68e6bSJason Wang netif_tx_stop_all_queues(dev); 6611da177e4SLinus Torvalds return 0; 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds /* Net device start xmit */ 665424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) 6661da177e4SLinus Torvalds { 6671da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 668c8d68e6bSJason Wang int txq = skb->queue_mapping; 6696e914fc7SJason Wang struct tun_file *tfile; 6701da177e4SLinus Torvalds 6716e914fc7SJason Wang rcu_read_lock(); 672c8d68e6bSJason Wang tfile = rcu_dereference(tun->tfiles[txq]); 673c8d68e6bSJason Wang 6741da177e4SLinus Torvalds /* Drop packet if interface is not attached */ 675c8d68e6bSJason Wang if (txq >= tun->numqueues) 6761da177e4SLinus Torvalds goto drop; 6771da177e4SLinus Torvalds 6786e914fc7SJason Wang tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len); 6796e914fc7SJason Wang 680c8d68e6bSJason Wang BUG_ON(!tfile); 681c8d68e6bSJason Wang 682f271b2ccSMax Krasnyansky /* Drop if the filter does not like it. 683f271b2ccSMax Krasnyansky * This is a noop if the filter is disabled. 684f271b2ccSMax Krasnyansky * Filter can be enabled only for the TAP devices. */ 685f271b2ccSMax Krasnyansky if (!check_filter(&tun->txflt, skb)) 686f271b2ccSMax Krasnyansky goto drop; 687f271b2ccSMax Krasnyansky 68854f968d6SJason Wang if (tfile->socket.sk->sk_filter && 68954f968d6SJason Wang sk_filter(tfile->socket.sk, skb)) 69099405162SMichael S. Tsirkin goto drop; 69199405162SMichael S. Tsirkin 692*36fe8c09SRami Rosen /* Limit the number of packets queued by dividing txq length with the 693c8d68e6bSJason Wang * number of queues. 694c8d68e6bSJason Wang */ 69554f968d6SJason Wang if (skb_queue_len(&tfile->socket.sk->sk_receive_queue) 696c8d68e6bSJason Wang >= dev->tx_queue_len / tun->numqueues){ 6971da177e4SLinus Torvalds if (!(tun->flags & TUN_ONE_QUEUE)) { 6981da177e4SLinus Torvalds /* Normal queueing mode. */ 6991da177e4SLinus Torvalds /* Packet scheduler handles dropping of further packets. */ 700c8d68e6bSJason Wang netif_stop_subqueue(dev, txq); 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds /* We won't see all dropped packets individually, so overrun 7031da177e4SLinus Torvalds * error is more appropriate. */ 70409f75cd7SJeff Garzik dev->stats.tx_fifo_errors++; 7051da177e4SLinus Torvalds } else { 7061da177e4SLinus Torvalds /* Single queue mode. 7071da177e4SLinus Torvalds * Driver handles dropping of all packets itself. */ 7081da177e4SLinus Torvalds goto drop; 7091da177e4SLinus Torvalds } 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds 7120110d6f2SMichael S. Tsirkin /* Orphan the skb - required as we might hang on to it 7130110d6f2SMichael S. Tsirkin * for indefinite time. */ 714868eefebSMichael S. Tsirkin if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC))) 715868eefebSMichael S. Tsirkin goto drop; 7160110d6f2SMichael S. Tsirkin skb_orphan(skb); 7170110d6f2SMichael S. Tsirkin 718f271b2ccSMax Krasnyansky /* Enqueue packet */ 71954f968d6SJason Wang skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb); 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds /* Notify and wake up reader process */ 72254f968d6SJason Wang if (tfile->flags & TUN_FASYNC) 72354f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_IN); 72454f968d6SJason Wang wake_up_interruptible_poll(&tfile->wq.wait, POLLIN | 72505c2828cSMichael S. Tsirkin POLLRDNORM | POLLRDBAND); 7266e914fc7SJason Wang 7276e914fc7SJason Wang rcu_read_unlock(); 7286ed10654SPatrick McHardy return NETDEV_TX_OK; 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds drop: 73109f75cd7SJeff Garzik dev->stats.tx_dropped++; 732149d36f7SMichael S. Tsirkin skb_tx_error(skb); 7331da177e4SLinus Torvalds kfree_skb(skb); 7346e914fc7SJason Wang rcu_read_unlock(); 7356ed10654SPatrick McHardy return NETDEV_TX_OK; 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds 738f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev) 7391da177e4SLinus Torvalds { 740f271b2ccSMax Krasnyansky /* 741f271b2ccSMax Krasnyansky * This callback is supposed to deal with mc filter in 742f271b2ccSMax Krasnyansky * _rx_ path and has nothing to do with the _tx_ path. 743f271b2ccSMax Krasnyansky * In rx path we always accept everything userspace gives us. 744f271b2ccSMax Krasnyansky */ 7451da177e4SLinus Torvalds } 7461da177e4SLinus Torvalds 7474885a504SEd Swierk #define MIN_MTU 68 7484885a504SEd Swierk #define MAX_MTU 65535 7494885a504SEd Swierk 7504885a504SEd Swierk static int 7514885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu) 7524885a504SEd Swierk { 7534885a504SEd Swierk if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU) 7544885a504SEd Swierk return -EINVAL; 7554885a504SEd Swierk dev->mtu = new_mtu; 7564885a504SEd Swierk return 0; 7574885a504SEd Swierk } 7584885a504SEd Swierk 759c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev, 760c8f44affSMichał Mirosław netdev_features_t features) 76188255375SMichał Mirosław { 76288255375SMichał Mirosław struct tun_struct *tun = netdev_priv(dev); 76388255375SMichał Mirosław 76488255375SMichał Mirosław return (features & tun->set_features) | (features & ~TUN_USER_FEATURES); 76588255375SMichał Mirosław } 766bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 767bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev) 768bebd097aSNeil Horman { 769bebd097aSNeil Horman /* 770bebd097aSNeil Horman * Tun only receives frames when: 771bebd097aSNeil Horman * 1) the char device endpoint gets data from user space 772bebd097aSNeil Horman * 2) the tun socket gets a sendmsg call from user space 773bebd097aSNeil Horman * Since both of those are syncronous operations, we are guaranteed 774bebd097aSNeil Horman * never to have pending data when we poll for it 775bebd097aSNeil Horman * so theres nothing to do here but return. 776bebd097aSNeil Horman * We need this though so netpoll recognizes us as an interface that 777bebd097aSNeil Horman * supports polling, which enables bridge devices in virt setups to 778bebd097aSNeil Horman * still use netconsole 779bebd097aSNeil Horman */ 780bebd097aSNeil Horman return; 781bebd097aSNeil Horman } 782bebd097aSNeil Horman #endif 783758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = { 784c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 785758e43b7SStephen Hemminger .ndo_open = tun_net_open, 786758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 78700829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 788758e43b7SStephen Hemminger .ndo_change_mtu = tun_net_change_mtu, 78988255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 790c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 791bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 792bebd097aSNeil Horman .ndo_poll_controller = tun_poll_controller, 793bebd097aSNeil Horman #endif 794758e43b7SStephen Hemminger }; 795758e43b7SStephen Hemminger 796758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = { 797c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 798758e43b7SStephen Hemminger .ndo_open = tun_net_open, 799758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 80000829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 801758e43b7SStephen Hemminger .ndo_change_mtu = tun_net_change_mtu, 80288255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 803afc4b13dSJiri Pirko .ndo_set_rx_mode = tun_net_mclist, 804758e43b7SStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 805758e43b7SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 806c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 807bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 808bebd097aSNeil Horman .ndo_poll_controller = tun_poll_controller, 809bebd097aSNeil Horman #endif 810758e43b7SStephen Hemminger }; 811758e43b7SStephen Hemminger 81296442e42SJason Wang static int tun_flow_init(struct tun_struct *tun) 81396442e42SJason Wang { 81496442e42SJason Wang int i; 81596442e42SJason Wang 81696442e42SJason Wang tun->flow_cache = kmem_cache_create("tun_flow_cache", 81796442e42SJason Wang sizeof(struct tun_flow_entry), 0, 0, 81896442e42SJason Wang NULL); 81996442e42SJason Wang if (!tun->flow_cache) 82096442e42SJason Wang return -ENOMEM; 82196442e42SJason Wang 82296442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) 82396442e42SJason Wang INIT_HLIST_HEAD(&tun->flows[i]); 82496442e42SJason Wang 82596442e42SJason Wang tun->ageing_time = TUN_FLOW_EXPIRE; 82696442e42SJason Wang setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun); 82796442e42SJason Wang mod_timer(&tun->flow_gc_timer, 82896442e42SJason Wang round_jiffies_up(jiffies + tun->ageing_time)); 82996442e42SJason Wang 83096442e42SJason Wang return 0; 83196442e42SJason Wang } 83296442e42SJason Wang 83396442e42SJason Wang static void tun_flow_uninit(struct tun_struct *tun) 83496442e42SJason Wang { 83596442e42SJason Wang del_timer_sync(&tun->flow_gc_timer); 83696442e42SJason Wang tun_flow_flush(tun); 83796442e42SJason Wang 83896442e42SJason Wang /* Wait for completion of call_rcu()'s */ 83996442e42SJason Wang rcu_barrier(); 84096442e42SJason Wang kmem_cache_destroy(tun->flow_cache); 84196442e42SJason Wang } 84296442e42SJason Wang 8431da177e4SLinus Torvalds /* Initialize net device. */ 8441da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev) 8451da177e4SLinus Torvalds { 8461da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 8491da177e4SLinus Torvalds case TUN_TUN_DEV: 850758e43b7SStephen Hemminger dev->netdev_ops = &tun_netdev_ops; 851758e43b7SStephen Hemminger 8521da177e4SLinus Torvalds /* Point-to-Point TUN Device */ 8531da177e4SLinus Torvalds dev->hard_header_len = 0; 8541da177e4SLinus Torvalds dev->addr_len = 0; 8551da177e4SLinus Torvalds dev->mtu = 1500; 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds /* Zero header length */ 8581da177e4SLinus Torvalds dev->type = ARPHRD_NONE; 8591da177e4SLinus Torvalds dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 8601da177e4SLinus Torvalds dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ 8611da177e4SLinus Torvalds break; 8621da177e4SLinus Torvalds 8631da177e4SLinus Torvalds case TUN_TAP_DEV: 8647a0a9608SKusanagi Kouichi dev->netdev_ops = &tap_netdev_ops; 8651da177e4SLinus Torvalds /* Ethernet TAP Device */ 8661da177e4SLinus Torvalds ether_setup(dev); 867550fd08cSNeil Horman dev->priv_flags &= ~IFF_TX_SKB_SHARING; 86836226a8dSBrian Braunstein 869f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 87036226a8dSBrian Braunstein 8711da177e4SLinus Torvalds dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ 8721da177e4SLinus Torvalds break; 8731da177e4SLinus Torvalds } 8741da177e4SLinus Torvalds } 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds /* Character device part */ 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds /* Poll */ 8791da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table *wait) 8801da177e4SLinus Torvalds { 881b2430de3SEric W. Biederman struct tun_file *tfile = file->private_data; 882b2430de3SEric W. Biederman struct tun_struct *tun = __tun_get(tfile); 8833c8a9c63SMariusz Kozlowski struct sock *sk; 88433dccbb0SHerbert Xu unsigned int mask = 0; 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds if (!tun) 887eac9e902SEric W. Biederman return POLLERR; 8881da177e4SLinus Torvalds 88954f968d6SJason Wang sk = tfile->socket.sk; 8903c8a9c63SMariusz Kozlowski 8916b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_chr_poll\n"); 8921da177e4SLinus Torvalds 89354f968d6SJason Wang poll_wait(file, &tfile->wq.wait, wait); 8941da177e4SLinus Torvalds 89589f56d1eSMichael S. Tsirkin if (!skb_queue_empty(&sk->sk_receive_queue)) 8961da177e4SLinus Torvalds mask |= POLLIN | POLLRDNORM; 8971da177e4SLinus Torvalds 89833dccbb0SHerbert Xu if (sock_writeable(sk) || 89933dccbb0SHerbert Xu (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) && 90033dccbb0SHerbert Xu sock_writeable(sk))) 90133dccbb0SHerbert Xu mask |= POLLOUT | POLLWRNORM; 90233dccbb0SHerbert Xu 903c70f1829SEric W. Biederman if (tun->dev->reg_state != NETREG_REGISTERED) 904c70f1829SEric W. Biederman mask = POLLERR; 905c70f1829SEric W. Biederman 906631ab46bSEric W. Biederman tun_put(tun); 9071da177e4SLinus Torvalds return mask; 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds 910f42157cbSRusty Russell /* prepad is the amount to reserve at front. len is length after that. 911f42157cbSRusty Russell * linear is a hint as to how much to copy (usually headers). */ 91254f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, 91333dccbb0SHerbert Xu size_t prepad, size_t len, 91433dccbb0SHerbert Xu size_t linear, int noblock) 915f42157cbSRusty Russell { 91654f968d6SJason Wang struct sock *sk = tfile->socket.sk; 917f42157cbSRusty Russell struct sk_buff *skb; 91833dccbb0SHerbert Xu int err; 919f42157cbSRusty Russell 920f42157cbSRusty Russell /* Under a page? Don't bother with paged skb. */ 9210eca93bcSHerbert Xu if (prepad + len < PAGE_SIZE || !linear) 92233dccbb0SHerbert Xu linear = len; 923f42157cbSRusty Russell 92433dccbb0SHerbert Xu skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, 92533dccbb0SHerbert Xu &err); 926f42157cbSRusty Russell if (!skb) 92733dccbb0SHerbert Xu return ERR_PTR(err); 928f42157cbSRusty Russell 929f42157cbSRusty Russell skb_reserve(skb, prepad); 930f42157cbSRusty Russell skb_put(skb, linear); 93133dccbb0SHerbert Xu skb->data_len = len - linear; 93233dccbb0SHerbert Xu skb->len += len - linear; 933f42157cbSRusty Russell 934f42157cbSRusty Russell return skb; 935f42157cbSRusty Russell } 936f42157cbSRusty Russell 9370690899bSMichael S. Tsirkin /* set skb frags from iovec, this can move to core network code for reuse */ 9380690899bSMichael S. Tsirkin static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from, 9390690899bSMichael S. Tsirkin int offset, size_t count) 9400690899bSMichael S. Tsirkin { 9410690899bSMichael S. Tsirkin int len = iov_length(from, count) - offset; 9420690899bSMichael S. Tsirkin int copy = skb_headlen(skb); 9430690899bSMichael S. Tsirkin int size, offset1 = 0; 9440690899bSMichael S. Tsirkin int i = 0; 9450690899bSMichael S. Tsirkin 9460690899bSMichael S. Tsirkin /* Skip over from offset */ 9470690899bSMichael S. Tsirkin while (count && (offset >= from->iov_len)) { 9480690899bSMichael S. Tsirkin offset -= from->iov_len; 9490690899bSMichael S. Tsirkin ++from; 9500690899bSMichael S. Tsirkin --count; 9510690899bSMichael S. Tsirkin } 9520690899bSMichael S. Tsirkin 9530690899bSMichael S. Tsirkin /* copy up to skb headlen */ 9540690899bSMichael S. Tsirkin while (count && (copy > 0)) { 9550690899bSMichael S. Tsirkin size = min_t(unsigned int, copy, from->iov_len - offset); 9560690899bSMichael S. Tsirkin if (copy_from_user(skb->data + offset1, from->iov_base + offset, 9570690899bSMichael S. Tsirkin size)) 9580690899bSMichael S. Tsirkin return -EFAULT; 9590690899bSMichael S. Tsirkin if (copy > size) { 9600690899bSMichael S. Tsirkin ++from; 9610690899bSMichael S. Tsirkin --count; 9620690899bSMichael S. Tsirkin offset = 0; 9630690899bSMichael S. Tsirkin } else 9640690899bSMichael S. Tsirkin offset += size; 9650690899bSMichael S. Tsirkin copy -= size; 9660690899bSMichael S. Tsirkin offset1 += size; 9670690899bSMichael S. Tsirkin } 9680690899bSMichael S. Tsirkin 9690690899bSMichael S. Tsirkin if (len == offset1) 9700690899bSMichael S. Tsirkin return 0; 9710690899bSMichael S. Tsirkin 9720690899bSMichael S. Tsirkin while (count--) { 9730690899bSMichael S. Tsirkin struct page *page[MAX_SKB_FRAGS]; 9740690899bSMichael S. Tsirkin int num_pages; 9750690899bSMichael S. Tsirkin unsigned long base; 9760690899bSMichael S. Tsirkin unsigned long truesize; 9770690899bSMichael S. Tsirkin 9780690899bSMichael S. Tsirkin len = from->iov_len - offset; 9790690899bSMichael S. Tsirkin if (!len) { 9800690899bSMichael S. Tsirkin offset = 0; 9810690899bSMichael S. Tsirkin ++from; 9820690899bSMichael S. Tsirkin continue; 9830690899bSMichael S. Tsirkin } 9840690899bSMichael S. Tsirkin base = (unsigned long)from->iov_base + offset; 9850690899bSMichael S. Tsirkin size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT; 9860690899bSMichael S. Tsirkin if (i + size > MAX_SKB_FRAGS) 9870690899bSMichael S. Tsirkin return -EMSGSIZE; 9880690899bSMichael S. Tsirkin num_pages = get_user_pages_fast(base, size, 0, &page[i]); 9890690899bSMichael S. Tsirkin if (num_pages != size) { 9900690899bSMichael S. Tsirkin for (i = 0; i < num_pages; i++) 9910690899bSMichael S. Tsirkin put_page(page[i]); 9920690899bSMichael S. Tsirkin return -EFAULT; 9930690899bSMichael S. Tsirkin } 9940690899bSMichael S. Tsirkin truesize = size * PAGE_SIZE; 9950690899bSMichael S. Tsirkin skb->data_len += len; 9960690899bSMichael S. Tsirkin skb->len += len; 9970690899bSMichael S. Tsirkin skb->truesize += truesize; 9980690899bSMichael S. Tsirkin atomic_add(truesize, &skb->sk->sk_wmem_alloc); 9990690899bSMichael S. Tsirkin while (len) { 10000690899bSMichael S. Tsirkin int off = base & ~PAGE_MASK; 10010690899bSMichael S. Tsirkin int size = min_t(int, len, PAGE_SIZE - off); 10020690899bSMichael S. Tsirkin __skb_fill_page_desc(skb, i, page[i], off, size); 10030690899bSMichael S. Tsirkin skb_shinfo(skb)->nr_frags++; 10040690899bSMichael S. Tsirkin /* increase sk_wmem_alloc */ 10050690899bSMichael S. Tsirkin base += size; 10060690899bSMichael S. Tsirkin len -= size; 10070690899bSMichael S. Tsirkin i++; 10080690899bSMichael S. Tsirkin } 10090690899bSMichael S. Tsirkin offset = 0; 10100690899bSMichael S. Tsirkin ++from; 10110690899bSMichael S. Tsirkin } 10120690899bSMichael S. Tsirkin return 0; 10130690899bSMichael S. Tsirkin } 10140690899bSMichael S. Tsirkin 10151da177e4SLinus Torvalds /* Get packet from user space buffer */ 101654f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, 101754f968d6SJason Wang void *msg_control, const struct iovec *iv, 101854f968d6SJason Wang size_t total_len, size_t count, int noblock) 10191da177e4SLinus Torvalds { 102009640e63SHarvey Harrison struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; 10211da177e4SLinus Torvalds struct sk_buff *skb; 10220690899bSMichael S. Tsirkin size_t len = total_len, align = NET_SKB_PAD; 1023f43798c2SRusty Russell struct virtio_net_hdr gso = { 0 }; 10246f26c9a7SMichael S. Tsirkin int offset = 0; 10250690899bSMichael S. Tsirkin int copylen; 10260690899bSMichael S. Tsirkin bool zerocopy = false; 10270690899bSMichael S. Tsirkin int err; 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds if (!(tun->flags & TUN_NO_PI)) { 10300690899bSMichael S. Tsirkin if ((len -= sizeof(pi)) > total_len) 10311da177e4SLinus Torvalds return -EINVAL; 10321da177e4SLinus Torvalds 10336f26c9a7SMichael S. Tsirkin if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi))) 10341da177e4SLinus Torvalds return -EFAULT; 10356f26c9a7SMichael S. Tsirkin offset += sizeof(pi); 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 1038f43798c2SRusty Russell if (tun->flags & TUN_VNET_HDR) { 10390690899bSMichael S. Tsirkin if ((len -= tun->vnet_hdr_sz) > total_len) 1040f43798c2SRusty Russell return -EINVAL; 1041f43798c2SRusty Russell 10426f26c9a7SMichael S. Tsirkin if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso))) 1043f43798c2SRusty Russell return -EFAULT; 1044f43798c2SRusty Russell 10454909122fSHerbert Xu if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && 10464909122fSHerbert Xu gso.csum_start + gso.csum_offset + 2 > gso.hdr_len) 10474909122fSHerbert Xu gso.hdr_len = gso.csum_start + gso.csum_offset + 2; 10484909122fSHerbert Xu 1049f43798c2SRusty Russell if (gso.hdr_len > len) 1050f43798c2SRusty Russell return -EINVAL; 1051d9d52b51SMichael S. Tsirkin offset += tun->vnet_hdr_sz; 1052f43798c2SRusty Russell } 1053f43798c2SRusty Russell 1054e01bf1c8SRusty Russell if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) { 1055a504b86eSstephen hemminger align += NET_IP_ALIGN; 10560eca93bcSHerbert Xu if (unlikely(len < ETH_HLEN || 10570eca93bcSHerbert Xu (gso.hdr_len && gso.hdr_len < ETH_HLEN))) 1058e01bf1c8SRusty Russell return -EINVAL; 1059e01bf1c8SRusty Russell } 10601da177e4SLinus Torvalds 10610690899bSMichael S. Tsirkin if (msg_control) 10620690899bSMichael S. Tsirkin zerocopy = true; 10630690899bSMichael S. Tsirkin 10640690899bSMichael S. Tsirkin if (zerocopy) { 10650690899bSMichael S. Tsirkin /* Userspace may produce vectors with count greater than 10660690899bSMichael S. Tsirkin * MAX_SKB_FRAGS, so we need to linearize parts of the skb 10670690899bSMichael S. Tsirkin * to let the rest of data to be fit in the frags. 10680690899bSMichael S. Tsirkin */ 10690690899bSMichael S. Tsirkin if (count > MAX_SKB_FRAGS) { 10700690899bSMichael S. Tsirkin copylen = iov_length(iv, count - MAX_SKB_FRAGS); 10710690899bSMichael S. Tsirkin if (copylen < offset) 10720690899bSMichael S. Tsirkin copylen = 0; 10730690899bSMichael S. Tsirkin else 10740690899bSMichael S. Tsirkin copylen -= offset; 10750690899bSMichael S. Tsirkin } else 10760690899bSMichael S. Tsirkin copylen = 0; 10770690899bSMichael S. Tsirkin /* There are 256 bytes to be copied in skb, so there is enough 10780690899bSMichael S. Tsirkin * room for skb expand head in case it is used. 10790690899bSMichael S. Tsirkin * The rest of the buffer is mapped from userspace. 10800690899bSMichael S. Tsirkin */ 10810690899bSMichael S. Tsirkin if (copylen < gso.hdr_len) 10820690899bSMichael S. Tsirkin copylen = gso.hdr_len; 10830690899bSMichael S. Tsirkin if (!copylen) 10840690899bSMichael S. Tsirkin copylen = GOODCOPY_LEN; 10850690899bSMichael S. Tsirkin } else 10860690899bSMichael S. Tsirkin copylen = len; 10870690899bSMichael S. Tsirkin 108854f968d6SJason Wang skb = tun_alloc_skb(tfile, align, copylen, gso.hdr_len, noblock); 108933dccbb0SHerbert Xu if (IS_ERR(skb)) { 109033dccbb0SHerbert Xu if (PTR_ERR(skb) != -EAGAIN) 109109f75cd7SJeff Garzik tun->dev->stats.rx_dropped++; 109233dccbb0SHerbert Xu return PTR_ERR(skb); 10931da177e4SLinus Torvalds } 10941da177e4SLinus Torvalds 10950690899bSMichael S. Tsirkin if (zerocopy) 10960690899bSMichael S. Tsirkin err = zerocopy_sg_from_iovec(skb, iv, offset, count); 10970690899bSMichael S. Tsirkin else 10980690899bSMichael S. Tsirkin err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len); 10990690899bSMichael S. Tsirkin 11000690899bSMichael S. Tsirkin if (err) { 110109f75cd7SJeff Garzik tun->dev->stats.rx_dropped++; 11028f22757eSDave Jones kfree_skb(skb); 11031da177e4SLinus Torvalds return -EFAULT; 11048f22757eSDave Jones } 11051da177e4SLinus Torvalds 1106f43798c2SRusty Russell if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { 1107f43798c2SRusty Russell if (!skb_partial_csum_set(skb, gso.csum_start, 1108f43798c2SRusty Russell gso.csum_offset)) { 1109f43798c2SRusty Russell tun->dev->stats.rx_frame_errors++; 1110f43798c2SRusty Russell kfree_skb(skb); 1111f43798c2SRusty Russell return -EINVAL; 1112f43798c2SRusty Russell } 111388255375SMichał Mirosław } 1114f43798c2SRusty Russell 11151da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 11161da177e4SLinus Torvalds case TUN_TUN_DEV: 1117f09f7ee2SAng Way Chuang if (tun->flags & TUN_NO_PI) { 1118f09f7ee2SAng Way Chuang switch (skb->data[0] & 0xf0) { 1119f09f7ee2SAng Way Chuang case 0x40: 1120f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IP); 1121f09f7ee2SAng Way Chuang break; 1122f09f7ee2SAng Way Chuang case 0x60: 1123f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IPV6); 1124f09f7ee2SAng Way Chuang break; 1125f09f7ee2SAng Way Chuang default: 1126f09f7ee2SAng Way Chuang tun->dev->stats.rx_dropped++; 1127f09f7ee2SAng Way Chuang kfree_skb(skb); 1128f09f7ee2SAng Way Chuang return -EINVAL; 1129f09f7ee2SAng Way Chuang } 1130f09f7ee2SAng Way Chuang } 1131f09f7ee2SAng Way Chuang 1132459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 11331da177e4SLinus Torvalds skb->protocol = pi.proto; 11344c13eb66SArnaldo Carvalho de Melo skb->dev = tun->dev; 11351da177e4SLinus Torvalds break; 11361da177e4SLinus Torvalds case TUN_TAP_DEV: 11371da177e4SLinus Torvalds skb->protocol = eth_type_trans(skb, tun->dev); 11381da177e4SLinus Torvalds break; 11396403eab1SJoe Perches } 11401da177e4SLinus Torvalds 1141f43798c2SRusty Russell if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) { 1142f43798c2SRusty Russell pr_debug("GSO!\n"); 1143f43798c2SRusty Russell switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { 1144f43798c2SRusty Russell case VIRTIO_NET_HDR_GSO_TCPV4: 1145f43798c2SRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; 1146f43798c2SRusty Russell break; 1147f43798c2SRusty Russell case VIRTIO_NET_HDR_GSO_TCPV6: 1148f43798c2SRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; 1149f43798c2SRusty Russell break; 1150e36aa25aSSridhar Samudrala case VIRTIO_NET_HDR_GSO_UDP: 1151e36aa25aSSridhar Samudrala skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 1152e36aa25aSSridhar Samudrala break; 1153f43798c2SRusty Russell default: 1154f43798c2SRusty Russell tun->dev->stats.rx_frame_errors++; 1155f43798c2SRusty Russell kfree_skb(skb); 1156f43798c2SRusty Russell return -EINVAL; 1157f43798c2SRusty Russell } 1158f43798c2SRusty Russell 1159f43798c2SRusty Russell if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN) 1160f43798c2SRusty Russell skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; 1161f43798c2SRusty Russell 1162f43798c2SRusty Russell skb_shinfo(skb)->gso_size = gso.gso_size; 1163f43798c2SRusty Russell if (skb_shinfo(skb)->gso_size == 0) { 1164f43798c2SRusty Russell tun->dev->stats.rx_frame_errors++; 1165f43798c2SRusty Russell kfree_skb(skb); 1166f43798c2SRusty Russell return -EINVAL; 1167f43798c2SRusty Russell } 1168f43798c2SRusty Russell 1169f43798c2SRusty Russell /* Header must be checked, and gso_segs computed. */ 1170f43798c2SRusty Russell skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 1171f43798c2SRusty Russell skb_shinfo(skb)->gso_segs = 0; 1172f43798c2SRusty Russell } 11731da177e4SLinus Torvalds 11740690899bSMichael S. Tsirkin /* copy skb_ubuf_info for callback when skb has no error */ 11750690899bSMichael S. Tsirkin if (zerocopy) { 11760690899bSMichael S. Tsirkin skb_shinfo(skb)->destructor_arg = msg_control; 11770690899bSMichael S. Tsirkin skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; 11780690899bSMichael S. Tsirkin } 11790690899bSMichael S. Tsirkin 11801da177e4SLinus Torvalds netif_rx_ni(skb); 11811da177e4SLinus Torvalds 118209f75cd7SJeff Garzik tun->dev->stats.rx_packets++; 118309f75cd7SJeff Garzik tun->dev->stats.rx_bytes += len; 11841da177e4SLinus Torvalds 118596442e42SJason Wang tun_flow_update(tun, skb, tfile->queue_index); 11860690899bSMichael S. Tsirkin return total_len; 11871da177e4SLinus Torvalds } 11881da177e4SLinus Torvalds 1189ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv, 1190ee0b3e67SBadari Pulavarty unsigned long count, loff_t pos) 11911da177e4SLinus Torvalds { 119233dccbb0SHerbert Xu struct file *file = iocb->ki_filp; 1193ab46d779SHerbert Xu struct tun_struct *tun = tun_get(file); 119454f968d6SJason Wang struct tun_file *tfile = file->private_data; 1195631ab46bSEric W. Biederman ssize_t result; 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds if (!tun) 11981da177e4SLinus Torvalds return -EBADFD; 11991da177e4SLinus Torvalds 12006b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count); 12011da177e4SLinus Torvalds 120254f968d6SJason Wang result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count), 120354f968d6SJason Wang count, file->f_flags & O_NONBLOCK); 1204631ab46bSEric W. Biederman 1205631ab46bSEric W. Biederman tun_put(tun); 1206631ab46bSEric W. Biederman return result; 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds 12091da177e4SLinus Torvalds /* Put packet to the user space buffer */ 12106f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun, 121154f968d6SJason Wang struct tun_file *tfile, 12121da177e4SLinus Torvalds struct sk_buff *skb, 121343b39dcdSMichael S. Tsirkin const struct iovec *iv, int len) 12141da177e4SLinus Torvalds { 12151da177e4SLinus Torvalds struct tun_pi pi = { 0, skb->protocol }; 12161da177e4SLinus Torvalds ssize_t total = 0; 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds if (!(tun->flags & TUN_NO_PI)) { 12191da177e4SLinus Torvalds if ((len -= sizeof(pi)) < 0) 12201da177e4SLinus Torvalds return -EINVAL; 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds if (len < skb->len) { 12231da177e4SLinus Torvalds /* Packet will be striped */ 12241da177e4SLinus Torvalds pi.flags |= TUN_PKT_STRIP; 12251da177e4SLinus Torvalds } 12261da177e4SLinus Torvalds 122743b39dcdSMichael S. Tsirkin if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi))) 12281da177e4SLinus Torvalds return -EFAULT; 12291da177e4SLinus Torvalds total += sizeof(pi); 12301da177e4SLinus Torvalds } 12311da177e4SLinus Torvalds 1232f43798c2SRusty Russell if (tun->flags & TUN_VNET_HDR) { 1233f43798c2SRusty Russell struct virtio_net_hdr gso = { 0 }; /* no info leak */ 1234d9d52b51SMichael S. Tsirkin if ((len -= tun->vnet_hdr_sz) < 0) 1235f43798c2SRusty Russell return -EINVAL; 1236f43798c2SRusty Russell 1237f43798c2SRusty Russell if (skb_is_gso(skb)) { 1238f43798c2SRusty Russell struct skb_shared_info *sinfo = skb_shinfo(skb); 1239f43798c2SRusty Russell 1240f43798c2SRusty Russell /* This is a hint as to how much should be linear. */ 1241f43798c2SRusty Russell gso.hdr_len = skb_headlen(skb); 1242f43798c2SRusty Russell gso.gso_size = sinfo->gso_size; 1243f43798c2SRusty Russell if (sinfo->gso_type & SKB_GSO_TCPV4) 1244f43798c2SRusty Russell gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 1245f43798c2SRusty Russell else if (sinfo->gso_type & SKB_GSO_TCPV6) 1246f43798c2SRusty Russell gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 1247e36aa25aSSridhar Samudrala else if (sinfo->gso_type & SKB_GSO_UDP) 1248e36aa25aSSridhar Samudrala gso.gso_type = VIRTIO_NET_HDR_GSO_UDP; 1249ef3db4a5SMichael S. Tsirkin else { 12506b8a66eeSJoe Perches pr_err("unexpected GSO type: " 1251ef3db4a5SMichael S. Tsirkin "0x%x, gso_size %d, hdr_len %d\n", 1252ef3db4a5SMichael S. Tsirkin sinfo->gso_type, gso.gso_size, 1253ef3db4a5SMichael S. Tsirkin gso.hdr_len); 1254ef3db4a5SMichael S. Tsirkin print_hex_dump(KERN_ERR, "tun: ", 1255ef3db4a5SMichael S. Tsirkin DUMP_PREFIX_NONE, 1256ef3db4a5SMichael S. Tsirkin 16, 1, skb->head, 1257ef3db4a5SMichael S. Tsirkin min((int)gso.hdr_len, 64), true); 1258ef3db4a5SMichael S. Tsirkin WARN_ON_ONCE(1); 1259ef3db4a5SMichael S. Tsirkin return -EINVAL; 1260ef3db4a5SMichael S. Tsirkin } 1261f43798c2SRusty Russell if (sinfo->gso_type & SKB_GSO_TCP_ECN) 1262f43798c2SRusty Russell gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN; 1263f43798c2SRusty Russell } else 1264f43798c2SRusty Russell gso.gso_type = VIRTIO_NET_HDR_GSO_NONE; 1265f43798c2SRusty Russell 1266f43798c2SRusty Russell if (skb->ip_summed == CHECKSUM_PARTIAL) { 1267f43798c2SRusty Russell gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; 126855508d60SMichał Mirosław gso.csum_start = skb_checksum_start_offset(skb); 1269f43798c2SRusty Russell gso.csum_offset = skb->csum_offset; 127010a8d94aSJason Wang } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) { 127110a8d94aSJason Wang gso.flags = VIRTIO_NET_HDR_F_DATA_VALID; 1272f43798c2SRusty Russell } /* else everything is zero */ 1273f43798c2SRusty Russell 127443b39dcdSMichael S. Tsirkin if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total, 127543b39dcdSMichael S. Tsirkin sizeof(gso)))) 1276f43798c2SRusty Russell return -EFAULT; 1277d9d52b51SMichael S. Tsirkin total += tun->vnet_hdr_sz; 1278f43798c2SRusty Russell } 1279f43798c2SRusty Russell 12801da177e4SLinus Torvalds len = min_t(int, skb->len, len); 12811da177e4SLinus Torvalds 128243b39dcdSMichael S. Tsirkin skb_copy_datagram_const_iovec(skb, 0, iv, total, len); 128305c2828cSMichael S. Tsirkin total += skb->len; 12841da177e4SLinus Torvalds 128509f75cd7SJeff Garzik tun->dev->stats.tx_packets++; 128609f75cd7SJeff Garzik tun->dev->stats.tx_bytes += len; 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds return total; 12891da177e4SLinus Torvalds } 12901da177e4SLinus Torvalds 129154f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, 129205c2828cSMichael S. Tsirkin struct kiocb *iocb, const struct iovec *iv, 129305c2828cSMichael S. Tsirkin ssize_t len, int noblock) 12941da177e4SLinus Torvalds { 12951da177e4SLinus Torvalds DECLARE_WAITQUEUE(wait, current); 12961da177e4SLinus Torvalds struct sk_buff *skb; 129705c2828cSMichael S. Tsirkin ssize_t ret = 0; 12981da177e4SLinus Torvalds 12996b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_chr_read\n"); 13001da177e4SLinus Torvalds 130161a5ff15SAmos Kong if (unlikely(!noblock)) 130254f968d6SJason Wang add_wait_queue(&tfile->wq.wait, &wait); 13031da177e4SLinus Torvalds while (len) { 13041da177e4SLinus Torvalds current->state = TASK_INTERRUPTIBLE; 13051da177e4SLinus Torvalds 13061da177e4SLinus Torvalds /* Read frames from the queue */ 130754f968d6SJason Wang if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) { 130805c2828cSMichael S. Tsirkin if (noblock) { 13091da177e4SLinus Torvalds ret = -EAGAIN; 13101da177e4SLinus Torvalds break; 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds if (signal_pending(current)) { 13131da177e4SLinus Torvalds ret = -ERESTARTSYS; 13141da177e4SLinus Torvalds break; 13151da177e4SLinus Torvalds } 1316c70f1829SEric W. Biederman if (tun->dev->reg_state != NETREG_REGISTERED) { 1317c70f1829SEric W. Biederman ret = -EIO; 1318c70f1829SEric W. Biederman break; 1319c70f1829SEric W. Biederman } 13201da177e4SLinus Torvalds 13211da177e4SLinus Torvalds /* Nothing to read, let's sleep */ 13221da177e4SLinus Torvalds schedule(); 13231da177e4SLinus Torvalds continue; 13241da177e4SLinus Torvalds } 1325c8d68e6bSJason Wang netif_wake_subqueue(tun->dev, tfile->queue_index); 13261da177e4SLinus Torvalds 132754f968d6SJason Wang ret = tun_put_user(tun, tfile, skb, iv, len); 13281da177e4SLinus Torvalds kfree_skb(skb); 13291da177e4SLinus Torvalds break; 13301da177e4SLinus Torvalds } 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds current->state = TASK_RUNNING; 133361a5ff15SAmos Kong if (unlikely(!noblock)) 133454f968d6SJason Wang remove_wait_queue(&tfile->wq.wait, &wait); 13351da177e4SLinus Torvalds 133605c2828cSMichael S. Tsirkin return ret; 133705c2828cSMichael S. Tsirkin } 133805c2828cSMichael S. Tsirkin 133905c2828cSMichael S. Tsirkin static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv, 134005c2828cSMichael S. Tsirkin unsigned long count, loff_t pos) 134105c2828cSMichael S. Tsirkin { 134205c2828cSMichael S. Tsirkin struct file *file = iocb->ki_filp; 134305c2828cSMichael S. Tsirkin struct tun_file *tfile = file->private_data; 134405c2828cSMichael S. Tsirkin struct tun_struct *tun = __tun_get(tfile); 134505c2828cSMichael S. Tsirkin ssize_t len, ret; 134605c2828cSMichael S. Tsirkin 134705c2828cSMichael S. Tsirkin if (!tun) 134805c2828cSMichael S. Tsirkin return -EBADFD; 134905c2828cSMichael S. Tsirkin len = iov_length(iv, count); 135005c2828cSMichael S. Tsirkin if (len < 0) { 135105c2828cSMichael S. Tsirkin ret = -EINVAL; 135205c2828cSMichael S. Tsirkin goto out; 135305c2828cSMichael S. Tsirkin } 135405c2828cSMichael S. Tsirkin 135554f968d6SJason Wang ret = tun_do_read(tun, tfile, iocb, iv, len, 135654f968d6SJason Wang file->f_flags & O_NONBLOCK); 135705c2828cSMichael S. Tsirkin ret = min_t(ssize_t, ret, len); 1358631ab46bSEric W. Biederman out: 1359631ab46bSEric W. Biederman tun_put(tun); 13601da177e4SLinus Torvalds return ret; 13611da177e4SLinus Torvalds } 13621da177e4SLinus Torvalds 136396442e42SJason Wang static void tun_free_netdev(struct net_device *dev) 136496442e42SJason Wang { 136596442e42SJason Wang struct tun_struct *tun = netdev_priv(dev); 136696442e42SJason Wang 136796442e42SJason Wang tun_flow_uninit(tun); 136896442e42SJason Wang free_netdev(dev); 136996442e42SJason Wang } 137096442e42SJason Wang 13711da177e4SLinus Torvalds static void tun_setup(struct net_device *dev) 13721da177e4SLinus Torvalds { 13731da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 13741da177e4SLinus Torvalds 13750625c883SEric W. Biederman tun->owner = INVALID_UID; 13760625c883SEric W. Biederman tun->group = INVALID_GID; 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds dev->ethtool_ops = &tun_ethtool_ops; 137996442e42SJason Wang dev->destructor = tun_free_netdev; 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds 1382f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap 1383f019a7a5SEric W. Biederman * device with netlink. 1384f019a7a5SEric W. Biederman */ 1385f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[]) 1386f019a7a5SEric W. Biederman { 1387f019a7a5SEric W. Biederman return -EINVAL; 1388f019a7a5SEric W. Biederman } 1389f019a7a5SEric W. Biederman 1390f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = { 1391f019a7a5SEric W. Biederman .kind = DRV_NAME, 1392f019a7a5SEric W. Biederman .priv_size = sizeof(struct tun_struct), 1393f019a7a5SEric W. Biederman .setup = tun_setup, 1394f019a7a5SEric W. Biederman .validate = tun_validate, 1395f019a7a5SEric W. Biederman }; 1396f019a7a5SEric W. Biederman 139733dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk) 139833dccbb0SHerbert Xu { 139954f968d6SJason Wang struct tun_file *tfile; 140043815482SEric Dumazet wait_queue_head_t *wqueue; 140133dccbb0SHerbert Xu 140233dccbb0SHerbert Xu if (!sock_writeable(sk)) 140333dccbb0SHerbert Xu return; 140433dccbb0SHerbert Xu 140533dccbb0SHerbert Xu if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags)) 140633dccbb0SHerbert Xu return; 140733dccbb0SHerbert Xu 140843815482SEric Dumazet wqueue = sk_sleep(sk); 140943815482SEric Dumazet if (wqueue && waitqueue_active(wqueue)) 141043815482SEric Dumazet wake_up_interruptible_sync_poll(wqueue, POLLOUT | 141105c2828cSMichael S. Tsirkin POLLWRNORM | POLLWRBAND); 1412c722c625SHerbert Xu 141354f968d6SJason Wang tfile = container_of(sk, struct tun_file, sk); 141454f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); 141533dccbb0SHerbert Xu } 141633dccbb0SHerbert Xu 141705c2828cSMichael S. Tsirkin static int tun_sendmsg(struct kiocb *iocb, struct socket *sock, 141805c2828cSMichael S. Tsirkin struct msghdr *m, size_t total_len) 141905c2828cSMichael S. Tsirkin { 142054f968d6SJason Wang int ret; 142154f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 142254f968d6SJason Wang struct tun_struct *tun = __tun_get(tfile); 142354f968d6SJason Wang 142454f968d6SJason Wang if (!tun) 142554f968d6SJason Wang return -EBADFD; 142654f968d6SJason Wang ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len, 14270690899bSMichael S. Tsirkin m->msg_iovlen, m->msg_flags & MSG_DONTWAIT); 142854f968d6SJason Wang tun_put(tun); 142954f968d6SJason Wang return ret; 143005c2828cSMichael S. Tsirkin } 143105c2828cSMichael S. Tsirkin 143254f968d6SJason Wang 143305c2828cSMichael S. Tsirkin static int tun_recvmsg(struct kiocb *iocb, struct socket *sock, 143405c2828cSMichael S. Tsirkin struct msghdr *m, size_t total_len, 143505c2828cSMichael S. Tsirkin int flags) 143605c2828cSMichael S. Tsirkin { 143754f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 143854f968d6SJason Wang struct tun_struct *tun = __tun_get(tfile); 143905c2828cSMichael S. Tsirkin int ret; 144054f968d6SJason Wang 144154f968d6SJason Wang if (!tun) 144254f968d6SJason Wang return -EBADFD; 144354f968d6SJason Wang 144405c2828cSMichael S. Tsirkin if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) 144505c2828cSMichael S. Tsirkin return -EINVAL; 144654f968d6SJason Wang ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len, 144705c2828cSMichael S. Tsirkin flags & MSG_DONTWAIT); 144805c2828cSMichael S. Tsirkin if (ret > total_len) { 144905c2828cSMichael S. Tsirkin m->msg_flags |= MSG_TRUNC; 145005c2828cSMichael S. Tsirkin ret = flags & MSG_TRUNC ? ret : total_len; 145105c2828cSMichael S. Tsirkin } 145254f968d6SJason Wang tun_put(tun); 145305c2828cSMichael S. Tsirkin return ret; 145405c2828cSMichael S. Tsirkin } 145505c2828cSMichael S. Tsirkin 14561ab5ecb9SStanislav Kinsbursky static int tun_release(struct socket *sock) 14571ab5ecb9SStanislav Kinsbursky { 14581ab5ecb9SStanislav Kinsbursky if (sock->sk) 14591ab5ecb9SStanislav Kinsbursky sock_put(sock->sk); 14601ab5ecb9SStanislav Kinsbursky return 0; 14611ab5ecb9SStanislav Kinsbursky } 14621ab5ecb9SStanislav Kinsbursky 146305c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */ 146405c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = { 146505c2828cSMichael S. Tsirkin .sendmsg = tun_sendmsg, 146605c2828cSMichael S. Tsirkin .recvmsg = tun_recvmsg, 14671ab5ecb9SStanislav Kinsbursky .release = tun_release, 146805c2828cSMichael S. Tsirkin }; 146905c2828cSMichael S. Tsirkin 147033dccbb0SHerbert Xu static struct proto tun_proto = { 147133dccbb0SHerbert Xu .name = "tun", 147233dccbb0SHerbert Xu .owner = THIS_MODULE, 147354f968d6SJason Wang .obj_size = sizeof(struct tun_file), 147433dccbb0SHerbert Xu }; 1475f019a7a5SEric W. Biederman 1476980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun) 1477980c9e8cSDavid Woodhouse { 1478980c9e8cSDavid Woodhouse int flags = 0; 1479980c9e8cSDavid Woodhouse 1480980c9e8cSDavid Woodhouse if (tun->flags & TUN_TUN_DEV) 1481980c9e8cSDavid Woodhouse flags |= IFF_TUN; 1482980c9e8cSDavid Woodhouse else 1483980c9e8cSDavid Woodhouse flags |= IFF_TAP; 1484980c9e8cSDavid Woodhouse 1485980c9e8cSDavid Woodhouse if (tun->flags & TUN_NO_PI) 1486980c9e8cSDavid Woodhouse flags |= IFF_NO_PI; 1487980c9e8cSDavid Woodhouse 1488980c9e8cSDavid Woodhouse if (tun->flags & TUN_ONE_QUEUE) 1489980c9e8cSDavid Woodhouse flags |= IFF_ONE_QUEUE; 1490980c9e8cSDavid Woodhouse 1491980c9e8cSDavid Woodhouse if (tun->flags & TUN_VNET_HDR) 1492980c9e8cSDavid Woodhouse flags |= IFF_VNET_HDR; 1493980c9e8cSDavid Woodhouse 1494c8d68e6bSJason Wang if (tun->flags & TUN_TAP_MQ) 1495c8d68e6bSJason Wang flags |= IFF_MULTI_QUEUE; 1496c8d68e6bSJason Wang 1497980c9e8cSDavid Woodhouse return flags; 1498980c9e8cSDavid Woodhouse } 1499980c9e8cSDavid Woodhouse 1500980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr, 1501980c9e8cSDavid Woodhouse char *buf) 1502980c9e8cSDavid Woodhouse { 1503980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 1504980c9e8cSDavid Woodhouse return sprintf(buf, "0x%x\n", tun_flags(tun)); 1505980c9e8cSDavid Woodhouse } 1506980c9e8cSDavid Woodhouse 1507980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr, 1508980c9e8cSDavid Woodhouse char *buf) 1509980c9e8cSDavid Woodhouse { 1510980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 15110625c883SEric W. Biederman return uid_valid(tun->owner)? 15120625c883SEric W. Biederman sprintf(buf, "%u\n", 15130625c883SEric W. Biederman from_kuid_munged(current_user_ns(), tun->owner)): 15140625c883SEric W. Biederman sprintf(buf, "-1\n"); 1515980c9e8cSDavid Woodhouse } 1516980c9e8cSDavid Woodhouse 1517980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr, 1518980c9e8cSDavid Woodhouse char *buf) 1519980c9e8cSDavid Woodhouse { 1520980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 15210625c883SEric W. Biederman return gid_valid(tun->group) ? 15220625c883SEric W. Biederman sprintf(buf, "%u\n", 15230625c883SEric W. Biederman from_kgid_munged(current_user_ns(), tun->group)): 15240625c883SEric W. Biederman sprintf(buf, "-1\n"); 1525980c9e8cSDavid Woodhouse } 1526980c9e8cSDavid Woodhouse 1527980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL); 1528980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL); 1529980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL); 1530980c9e8cSDavid Woodhouse 1531d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) 15321da177e4SLinus Torvalds { 15331da177e4SLinus Torvalds struct tun_struct *tun; 153454f968d6SJason Wang struct tun_file *tfile = file->private_data; 15351da177e4SLinus Torvalds struct net_device *dev; 15361da177e4SLinus Torvalds int err; 15371da177e4SLinus Torvalds 153874a3e5a7SEric W. Biederman dev = __dev_get_by_name(net, ifr->ifr_name); 153974a3e5a7SEric W. Biederman if (dev) { 1540f85ba780SDavid Woodhouse if (ifr->ifr_flags & IFF_TUN_EXCL) 1541f85ba780SDavid Woodhouse return -EBUSY; 154274a3e5a7SEric W. Biederman if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) 154374a3e5a7SEric W. Biederman tun = netdev_priv(dev); 154474a3e5a7SEric W. Biederman else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) 154574a3e5a7SEric W. Biederman tun = netdev_priv(dev); 154674a3e5a7SEric W. Biederman else 154774a3e5a7SEric W. Biederman return -EINVAL; 154874a3e5a7SEric W. Biederman 1549cde8b15fSJason Wang if (tun_not_capable(tun)) 15502b980dbdSPaul Moore return -EPERM; 155154f968d6SJason Wang err = security_tun_dev_attach(tfile->socket.sk); 15522b980dbdSPaul Moore if (err < 0) 15532b980dbdSPaul Moore return err; 15542b980dbdSPaul Moore 1555a7385ba2SEric W. Biederman err = tun_attach(tun, file); 1556a7385ba2SEric W. Biederman if (err < 0) 1557a7385ba2SEric W. Biederman return err; 155886a264abSDavid Howells } 15591da177e4SLinus Torvalds else { 15601da177e4SLinus Torvalds char *name; 15611da177e4SLinus Torvalds unsigned long flags = 0; 15621da177e4SLinus Torvalds 1563c260b772SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1564ca6bb5d7SDavid Woodhouse return -EPERM; 15652b980dbdSPaul Moore err = security_tun_dev_create(); 15662b980dbdSPaul Moore if (err < 0) 15672b980dbdSPaul Moore return err; 1568ca6bb5d7SDavid Woodhouse 15691da177e4SLinus Torvalds /* Set dev type */ 15701da177e4SLinus Torvalds if (ifr->ifr_flags & IFF_TUN) { 15711da177e4SLinus Torvalds /* TUN device */ 15721da177e4SLinus Torvalds flags |= TUN_TUN_DEV; 15731da177e4SLinus Torvalds name = "tun%d"; 15741da177e4SLinus Torvalds } else if (ifr->ifr_flags & IFF_TAP) { 15751da177e4SLinus Torvalds /* TAP device */ 15761da177e4SLinus Torvalds flags |= TUN_TAP_DEV; 15771da177e4SLinus Torvalds name = "tap%d"; 15781da177e4SLinus Torvalds } else 157936989b90SKusanagi Kouichi return -EINVAL; 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds if (*ifr->ifr_name) 15821da177e4SLinus Torvalds name = ifr->ifr_name; 15831da177e4SLinus Torvalds 1584c8d68e6bSJason Wang dev = alloc_netdev_mqs(sizeof(struct tun_struct), name, 1585c8d68e6bSJason Wang tun_setup, 1586c8d68e6bSJason Wang MAX_TAP_QUEUES, MAX_TAP_QUEUES); 15871da177e4SLinus Torvalds if (!dev) 15881da177e4SLinus Torvalds return -ENOMEM; 15891da177e4SLinus Torvalds 1590fc54c658SPavel Emelyanov dev_net_set(dev, net); 1591f019a7a5SEric W. Biederman dev->rtnl_link_ops = &tun_link_ops; 1592758e43b7SStephen Hemminger 15931da177e4SLinus Torvalds tun = netdev_priv(dev); 15941da177e4SLinus Torvalds tun->dev = dev; 15951da177e4SLinus Torvalds tun->flags = flags; 1596f271b2ccSMax Krasnyansky tun->txflt.count = 0; 1597d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr); 15981da177e4SLinus Torvalds 159954f968d6SJason Wang tun->filter_attached = false; 160054f968d6SJason Wang tun->sndbuf = tfile->socket.sk->sk_sndbuf; 160133dccbb0SHerbert Xu 160296442e42SJason Wang spin_lock_init(&tun->lock); 160396442e42SJason Wang 160454f968d6SJason Wang security_tun_dev_post_create(&tfile->sk); 16052b980dbdSPaul Moore 16061da177e4SLinus Torvalds tun_net_init(dev); 16071da177e4SLinus Torvalds 160896442e42SJason Wang if (tun_flow_init(tun)) 160996442e42SJason Wang goto err_free_dev; 161096442e42SJason Wang 161188255375SMichał Mirosław dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | 161288255375SMichał Mirosław TUN_USER_FEATURES; 161388255375SMichał Mirosław dev->features = dev->hw_features; 161488255375SMichał Mirosław 16151da177e4SLinus Torvalds err = register_netdevice(tun->dev); 16161da177e4SLinus Torvalds if (err < 0) 161754f968d6SJason Wang goto err_free_dev; 16189c3fea6aSHerbert Xu 1619980c9e8cSDavid Woodhouse if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) || 1620980c9e8cSDavid Woodhouse device_create_file(&tun->dev->dev, &dev_attr_owner) || 1621980c9e8cSDavid Woodhouse device_create_file(&tun->dev->dev, &dev_attr_group)) 16226b8a66eeSJoe Perches pr_err("Failed to create tun sysfs files\n"); 1623980c9e8cSDavid Woodhouse 1624a7385ba2SEric W. Biederman err = tun_attach(tun, file); 1625a7385ba2SEric W. Biederman if (err < 0) 1626c8d68e6bSJason Wang goto err_free_dev; 16271da177e4SLinus Torvalds } 16281da177e4SLinus Torvalds 16296b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_set_iff\n"); 16301da177e4SLinus Torvalds 16311da177e4SLinus Torvalds if (ifr->ifr_flags & IFF_NO_PI) 16321da177e4SLinus Torvalds tun->flags |= TUN_NO_PI; 1633a26af1e0SNathaniel Filardo else 1634a26af1e0SNathaniel Filardo tun->flags &= ~TUN_NO_PI; 16351da177e4SLinus Torvalds 16361da177e4SLinus Torvalds if (ifr->ifr_flags & IFF_ONE_QUEUE) 16371da177e4SLinus Torvalds tun->flags |= TUN_ONE_QUEUE; 1638a26af1e0SNathaniel Filardo else 1639a26af1e0SNathaniel Filardo tun->flags &= ~TUN_ONE_QUEUE; 16401da177e4SLinus Torvalds 1641f43798c2SRusty Russell if (ifr->ifr_flags & IFF_VNET_HDR) 1642f43798c2SRusty Russell tun->flags |= TUN_VNET_HDR; 1643f43798c2SRusty Russell else 1644f43798c2SRusty Russell tun->flags &= ~TUN_VNET_HDR; 1645f43798c2SRusty Russell 1646c8d68e6bSJason Wang if (ifr->ifr_flags & IFF_MULTI_QUEUE) 1647c8d68e6bSJason Wang tun->flags |= TUN_TAP_MQ; 1648c8d68e6bSJason Wang else 1649c8d68e6bSJason Wang tun->flags &= ~TUN_TAP_MQ; 1650c8d68e6bSJason Wang 1651e35259a9SMax Krasnyansky /* Make sure persistent devices do not get stuck in 1652e35259a9SMax Krasnyansky * xoff state. 1653e35259a9SMax Krasnyansky */ 1654e35259a9SMax Krasnyansky if (netif_running(tun->dev)) 1655c8d68e6bSJason Wang netif_tx_wake_all_queues(tun->dev); 1656e35259a9SMax Krasnyansky 16571da177e4SLinus Torvalds strcpy(ifr->ifr_name, tun->dev->name); 16581da177e4SLinus Torvalds return 0; 16591da177e4SLinus Torvalds 16601da177e4SLinus Torvalds err_free_dev: 16611da177e4SLinus Torvalds free_netdev(dev); 16621da177e4SLinus Torvalds return err; 16631da177e4SLinus Torvalds } 16641da177e4SLinus Torvalds 16659ce99cf6SRami Rosen static void tun_get_iff(struct net *net, struct tun_struct *tun, 1666876bfd4dSHerbert Xu struct ifreq *ifr) 1667e3b99556SMark McLoughlin { 16686b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_get_iff\n"); 1669e3b99556SMark McLoughlin 1670e3b99556SMark McLoughlin strcpy(ifr->ifr_name, tun->dev->name); 1671e3b99556SMark McLoughlin 1672980c9e8cSDavid Woodhouse ifr->ifr_flags = tun_flags(tun); 1673e3b99556SMark McLoughlin 1674e3b99556SMark McLoughlin } 1675e3b99556SMark McLoughlin 16765228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no 16775228ddc9SRusty Russell * privs required. */ 167888255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg) 16795228ddc9SRusty Russell { 1680c8f44affSMichał Mirosław netdev_features_t features = 0; 16815228ddc9SRusty Russell 16825228ddc9SRusty Russell if (arg & TUN_F_CSUM) { 168388255375SMichał Mirosław features |= NETIF_F_HW_CSUM; 16845228ddc9SRusty Russell arg &= ~TUN_F_CSUM; 16855228ddc9SRusty Russell 16865228ddc9SRusty Russell if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { 16875228ddc9SRusty Russell if (arg & TUN_F_TSO_ECN) { 16885228ddc9SRusty Russell features |= NETIF_F_TSO_ECN; 16895228ddc9SRusty Russell arg &= ~TUN_F_TSO_ECN; 16905228ddc9SRusty Russell } 16915228ddc9SRusty Russell if (arg & TUN_F_TSO4) 16925228ddc9SRusty Russell features |= NETIF_F_TSO; 16935228ddc9SRusty Russell if (arg & TUN_F_TSO6) 16945228ddc9SRusty Russell features |= NETIF_F_TSO6; 16955228ddc9SRusty Russell arg &= ~(TUN_F_TSO4|TUN_F_TSO6); 16965228ddc9SRusty Russell } 1697e36aa25aSSridhar Samudrala 1698e36aa25aSSridhar Samudrala if (arg & TUN_F_UFO) { 1699e36aa25aSSridhar Samudrala features |= NETIF_F_UFO; 1700e36aa25aSSridhar Samudrala arg &= ~TUN_F_UFO; 1701e36aa25aSSridhar Samudrala } 17025228ddc9SRusty Russell } 17035228ddc9SRusty Russell 17045228ddc9SRusty Russell /* This gives the user a way to test for new features in future by 17055228ddc9SRusty Russell * trying to set them. */ 17065228ddc9SRusty Russell if (arg) 17075228ddc9SRusty Russell return -EINVAL; 17085228ddc9SRusty Russell 170988255375SMichał Mirosław tun->set_features = features; 171088255375SMichał Mirosław netdev_update_features(tun->dev); 17115228ddc9SRusty Russell 17125228ddc9SRusty Russell return 0; 17135228ddc9SRusty Russell } 17145228ddc9SRusty Russell 1715c8d68e6bSJason Wang static void tun_detach_filter(struct tun_struct *tun, int n) 1716c8d68e6bSJason Wang { 1717c8d68e6bSJason Wang int i; 1718c8d68e6bSJason Wang struct tun_file *tfile; 1719c8d68e6bSJason Wang 1720c8d68e6bSJason Wang for (i = 0; i < n; i++) { 1721c8d68e6bSJason Wang tfile = rcu_dereference_protected(tun->tfiles[i], 1722c8d68e6bSJason Wang lockdep_rtnl_is_held()); 1723c8d68e6bSJason Wang sk_detach_filter(tfile->socket.sk); 1724c8d68e6bSJason Wang } 1725c8d68e6bSJason Wang 1726c8d68e6bSJason Wang tun->filter_attached = false; 1727c8d68e6bSJason Wang } 1728c8d68e6bSJason Wang 1729c8d68e6bSJason Wang static int tun_attach_filter(struct tun_struct *tun) 1730c8d68e6bSJason Wang { 1731c8d68e6bSJason Wang int i, ret = 0; 1732c8d68e6bSJason Wang struct tun_file *tfile; 1733c8d68e6bSJason Wang 1734c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 1735c8d68e6bSJason Wang tfile = rcu_dereference_protected(tun->tfiles[i], 1736c8d68e6bSJason Wang lockdep_rtnl_is_held()); 1737c8d68e6bSJason Wang ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); 1738c8d68e6bSJason Wang if (ret) { 1739c8d68e6bSJason Wang tun_detach_filter(tun, i); 1740c8d68e6bSJason Wang return ret; 1741c8d68e6bSJason Wang } 1742c8d68e6bSJason Wang } 1743c8d68e6bSJason Wang 1744c8d68e6bSJason Wang tun->filter_attached = true; 1745c8d68e6bSJason Wang return ret; 1746c8d68e6bSJason Wang } 1747c8d68e6bSJason Wang 1748c8d68e6bSJason Wang static void tun_set_sndbuf(struct tun_struct *tun) 1749c8d68e6bSJason Wang { 1750c8d68e6bSJason Wang struct tun_file *tfile; 1751c8d68e6bSJason Wang int i; 1752c8d68e6bSJason Wang 1753c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 1754c8d68e6bSJason Wang tfile = rcu_dereference_protected(tun->tfiles[i], 1755c8d68e6bSJason Wang lockdep_rtnl_is_held()); 1756c8d68e6bSJason Wang tfile->socket.sk->sk_sndbuf = tun->sndbuf; 1757c8d68e6bSJason Wang } 1758c8d68e6bSJason Wang } 1759c8d68e6bSJason Wang 1760cde8b15fSJason Wang static int tun_set_queue(struct file *file, struct ifreq *ifr) 1761cde8b15fSJason Wang { 1762cde8b15fSJason Wang struct tun_file *tfile = file->private_data; 1763cde8b15fSJason Wang struct tun_struct *tun; 1764cde8b15fSJason Wang struct net_device *dev; 1765cde8b15fSJason Wang int ret = 0; 1766cde8b15fSJason Wang 1767cde8b15fSJason Wang rtnl_lock(); 1768cde8b15fSJason Wang 1769cde8b15fSJason Wang if (ifr->ifr_flags & IFF_ATTACH_QUEUE) { 1770cde8b15fSJason Wang dev = __dev_get_by_name(tfile->net, ifr->ifr_name); 1771cde8b15fSJason Wang if (!dev) { 1772cde8b15fSJason Wang ret = -EINVAL; 1773cde8b15fSJason Wang goto unlock; 1774cde8b15fSJason Wang } 1775cde8b15fSJason Wang 1776cde8b15fSJason Wang tun = netdev_priv(dev); 1777cde8b15fSJason Wang if (dev->netdev_ops != &tap_netdev_ops && 1778cde8b15fSJason Wang dev->netdev_ops != &tun_netdev_ops) 1779cde8b15fSJason Wang ret = -EINVAL; 1780cde8b15fSJason Wang else if (tun_not_capable(tun)) 1781cde8b15fSJason Wang ret = -EPERM; 1782cde8b15fSJason Wang else 1783cde8b15fSJason Wang ret = tun_attach(tun, file); 1784cde8b15fSJason Wang } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) 1785cde8b15fSJason Wang __tun_detach(tfile, false); 1786cde8b15fSJason Wang else 1787cde8b15fSJason Wang ret = -EINVAL; 1788cde8b15fSJason Wang 1789cde8b15fSJason Wang unlock: 1790cde8b15fSJason Wang rtnl_unlock(); 1791cde8b15fSJason Wang return ret; 1792cde8b15fSJason Wang } 1793cde8b15fSJason Wang 179450857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd, 179550857e2aSArnd Bergmann unsigned long arg, int ifreq_len) 17961da177e4SLinus Torvalds { 179736b50babSEric W. Biederman struct tun_file *tfile = file->private_data; 1798631ab46bSEric W. Biederman struct tun_struct *tun; 17991da177e4SLinus Torvalds void __user* argp = (void __user*)arg; 18001da177e4SLinus Torvalds struct ifreq ifr; 18010625c883SEric W. Biederman kuid_t owner; 18020625c883SEric W. Biederman kgid_t group; 180333dccbb0SHerbert Xu int sndbuf; 1804d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 1805f271b2ccSMax Krasnyansky int ret; 18061da177e4SLinus Torvalds 1807cde8b15fSJason Wang if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) { 180850857e2aSArnd Bergmann if (copy_from_user(&ifr, argp, ifreq_len)) 18091da177e4SLinus Torvalds return -EFAULT; 18108bbb1813SDavid S. Miller } else { 1811a117dacdSMathias Krause memset(&ifr, 0, sizeof(ifr)); 18128bbb1813SDavid S. Miller } 1813631ab46bSEric W. Biederman if (cmd == TUNGETFEATURES) { 1814631ab46bSEric W. Biederman /* Currently this just means: "what IFF flags are valid?". 1815631ab46bSEric W. Biederman * This is needed because we never checked for invalid flags on 1816631ab46bSEric W. Biederman * TUNSETIFF. */ 1817631ab46bSEric W. Biederman return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | 1818cde8b15fSJason Wang IFF_VNET_HDR | IFF_MULTI_QUEUE, 1819631ab46bSEric W. Biederman (unsigned int __user*)argp); 1820cde8b15fSJason Wang } else if (cmd == TUNSETQUEUE) 1821cde8b15fSJason Wang return tun_set_queue(file, &ifr); 1822631ab46bSEric W. Biederman 1823c8d68e6bSJason Wang ret = 0; 1824876bfd4dSHerbert Xu rtnl_lock(); 1825876bfd4dSHerbert Xu 182636b50babSEric W. Biederman tun = __tun_get(tfile); 18271da177e4SLinus Torvalds if (cmd == TUNSETIFF && !tun) { 18281da177e4SLinus Torvalds ifr.ifr_name[IFNAMSIZ-1] = '\0'; 18291da177e4SLinus Torvalds 1830876bfd4dSHerbert Xu ret = tun_set_iff(tfile->net, file, &ifr); 18311da177e4SLinus Torvalds 1832876bfd4dSHerbert Xu if (ret) 1833876bfd4dSHerbert Xu goto unlock; 18341da177e4SLinus Torvalds 183550857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 1836876bfd4dSHerbert Xu ret = -EFAULT; 1837876bfd4dSHerbert Xu goto unlock; 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds 1840876bfd4dSHerbert Xu ret = -EBADFD; 18411da177e4SLinus Torvalds if (!tun) 1842876bfd4dSHerbert Xu goto unlock; 18431da177e4SLinus Torvalds 18441e588338SJason Wang tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd); 18451da177e4SLinus Torvalds 1846631ab46bSEric W. Biederman ret = 0; 18471da177e4SLinus Torvalds switch (cmd) { 1848e3b99556SMark McLoughlin case TUNGETIFF: 18499ce99cf6SRami Rosen tun_get_iff(current->nsproxy->net_ns, tun, &ifr); 1850e3b99556SMark McLoughlin 185150857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 1852631ab46bSEric W. Biederman ret = -EFAULT; 1853e3b99556SMark McLoughlin break; 1854e3b99556SMark McLoughlin 18551da177e4SLinus Torvalds case TUNSETNOCSUM: 18561da177e4SLinus Torvalds /* Disable/Enable checksum */ 18571da177e4SLinus Torvalds 185888255375SMichał Mirosław /* [unimplemented] */ 185988255375SMichał Mirosław tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n", 18606b8a66eeSJoe Perches arg ? "disabled" : "enabled"); 18611da177e4SLinus Torvalds break; 18621da177e4SLinus Torvalds 18631da177e4SLinus Torvalds case TUNSETPERSIST: 186454f968d6SJason Wang /* Disable/Enable persist mode. Keep an extra reference to the 186554f968d6SJason Wang * module to prevent the module being unprobed. 186654f968d6SJason Wang */ 186754f968d6SJason Wang if (arg) { 18681da177e4SLinus Torvalds tun->flags |= TUN_PERSIST; 186954f968d6SJason Wang __module_get(THIS_MODULE); 187054f968d6SJason Wang } else { 18711da177e4SLinus Torvalds tun->flags &= ~TUN_PERSIST; 187254f968d6SJason Wang module_put(THIS_MODULE); 187354f968d6SJason Wang } 18741da177e4SLinus Torvalds 18756b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "persist %s\n", 18766b8a66eeSJoe Perches arg ? "enabled" : "disabled"); 18771da177e4SLinus Torvalds break; 18781da177e4SLinus Torvalds 18791da177e4SLinus Torvalds case TUNSETOWNER: 18801da177e4SLinus Torvalds /* Set owner of the device */ 18810625c883SEric W. Biederman owner = make_kuid(current_user_ns(), arg); 18820625c883SEric W. Biederman if (!uid_valid(owner)) { 18830625c883SEric W. Biederman ret = -EINVAL; 18840625c883SEric W. Biederman break; 18850625c883SEric W. Biederman } 18860625c883SEric W. Biederman tun->owner = owner; 18871e588338SJason Wang tun_debug(KERN_INFO, tun, "owner set to %u\n", 18880625c883SEric W. Biederman from_kuid(&init_user_ns, tun->owner)); 18891da177e4SLinus Torvalds break; 18901da177e4SLinus Torvalds 18918c644623SGuido Guenther case TUNSETGROUP: 18928c644623SGuido Guenther /* Set group of the device */ 18930625c883SEric W. Biederman group = make_kgid(current_user_ns(), arg); 18940625c883SEric W. Biederman if (!gid_valid(group)) { 18950625c883SEric W. Biederman ret = -EINVAL; 18960625c883SEric W. Biederman break; 18970625c883SEric W. Biederman } 18980625c883SEric W. Biederman tun->group = group; 18991e588338SJason Wang tun_debug(KERN_INFO, tun, "group set to %u\n", 19000625c883SEric W. Biederman from_kgid(&init_user_ns, tun->group)); 19018c644623SGuido Guenther break; 19028c644623SGuido Guenther 1903ff4cc3acSMike Kershaw case TUNSETLINK: 1904ff4cc3acSMike Kershaw /* Only allow setting the type when the interface is down */ 1905ff4cc3acSMike Kershaw if (tun->dev->flags & IFF_UP) { 19066b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, 19076b8a66eeSJoe Perches "Linktype set failed because interface is up\n"); 190848abfe05SDavid S. Miller ret = -EBUSY; 1909ff4cc3acSMike Kershaw } else { 1910ff4cc3acSMike Kershaw tun->dev->type = (int) arg; 19116b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "linktype set to %d\n", 19126b8a66eeSJoe Perches tun->dev->type); 191348abfe05SDavid S. Miller ret = 0; 1914ff4cc3acSMike Kershaw } 1915631ab46bSEric W. Biederman break; 1916ff4cc3acSMike Kershaw 19171da177e4SLinus Torvalds #ifdef TUN_DEBUG 19181da177e4SLinus Torvalds case TUNSETDEBUG: 19191da177e4SLinus Torvalds tun->debug = arg; 19201da177e4SLinus Torvalds break; 19211da177e4SLinus Torvalds #endif 19225228ddc9SRusty Russell case TUNSETOFFLOAD: 192388255375SMichał Mirosław ret = set_offload(tun, arg); 1924631ab46bSEric W. Biederman break; 19255228ddc9SRusty Russell 1926f271b2ccSMax Krasnyansky case TUNSETTXFILTER: 1927f271b2ccSMax Krasnyansky /* Can be set only for TAPs */ 1928631ab46bSEric W. Biederman ret = -EINVAL; 1929f271b2ccSMax Krasnyansky if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) 1930631ab46bSEric W. Biederman break; 1931c0e5a8c2SHarvey Harrison ret = update_filter(&tun->txflt, (void __user *)arg); 1932631ab46bSEric W. Biederman break; 19331da177e4SLinus Torvalds 19341da177e4SLinus Torvalds case SIOCGIFHWADDR: 1935b595076aSUwe Kleine-König /* Get hw address */ 1936f271b2ccSMax Krasnyansky memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); 1937f271b2ccSMax Krasnyansky ifr.ifr_hwaddr.sa_family = tun->dev->type; 193850857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 1939631ab46bSEric W. Biederman ret = -EFAULT; 1940631ab46bSEric W. Biederman break; 19411da177e4SLinus Torvalds 19421da177e4SLinus Torvalds case SIOCSIFHWADDR: 1943f271b2ccSMax Krasnyansky /* Set hw address */ 19446b8a66eeSJoe Perches tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n", 19456b8a66eeSJoe Perches ifr.ifr_hwaddr.sa_data); 194640102371SKim B. Heino 194740102371SKim B. Heino ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); 1948631ab46bSEric W. Biederman break; 194933dccbb0SHerbert Xu 195033dccbb0SHerbert Xu case TUNGETSNDBUF: 195154f968d6SJason Wang sndbuf = tfile->socket.sk->sk_sndbuf; 195233dccbb0SHerbert Xu if (copy_to_user(argp, &sndbuf, sizeof(sndbuf))) 195333dccbb0SHerbert Xu ret = -EFAULT; 195433dccbb0SHerbert Xu break; 195533dccbb0SHerbert Xu 195633dccbb0SHerbert Xu case TUNSETSNDBUF: 195733dccbb0SHerbert Xu if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) { 195833dccbb0SHerbert Xu ret = -EFAULT; 195933dccbb0SHerbert Xu break; 196033dccbb0SHerbert Xu } 196133dccbb0SHerbert Xu 1962c8d68e6bSJason Wang tun->sndbuf = sndbuf; 1963c8d68e6bSJason Wang tun_set_sndbuf(tun); 196433dccbb0SHerbert Xu break; 196533dccbb0SHerbert Xu 1966d9d52b51SMichael S. Tsirkin case TUNGETVNETHDRSZ: 1967d9d52b51SMichael S. Tsirkin vnet_hdr_sz = tun->vnet_hdr_sz; 1968d9d52b51SMichael S. Tsirkin if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz))) 1969d9d52b51SMichael S. Tsirkin ret = -EFAULT; 1970d9d52b51SMichael S. Tsirkin break; 1971d9d52b51SMichael S. Tsirkin 1972d9d52b51SMichael S. Tsirkin case TUNSETVNETHDRSZ: 1973d9d52b51SMichael S. Tsirkin if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) { 1974d9d52b51SMichael S. Tsirkin ret = -EFAULT; 1975d9d52b51SMichael S. Tsirkin break; 1976d9d52b51SMichael S. Tsirkin } 1977d9d52b51SMichael S. Tsirkin if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) { 1978d9d52b51SMichael S. Tsirkin ret = -EINVAL; 1979d9d52b51SMichael S. Tsirkin break; 1980d9d52b51SMichael S. Tsirkin } 1981d9d52b51SMichael S. Tsirkin 1982d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = vnet_hdr_sz; 1983d9d52b51SMichael S. Tsirkin break; 1984d9d52b51SMichael S. Tsirkin 198599405162SMichael S. Tsirkin case TUNATTACHFILTER: 198699405162SMichael S. Tsirkin /* Can be set only for TAPs */ 198799405162SMichael S. Tsirkin ret = -EINVAL; 198899405162SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) 198999405162SMichael S. Tsirkin break; 199099405162SMichael S. Tsirkin ret = -EFAULT; 199154f968d6SJason Wang if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog))) 199299405162SMichael S. Tsirkin break; 199399405162SMichael S. Tsirkin 1994c8d68e6bSJason Wang ret = tun_attach_filter(tun); 199599405162SMichael S. Tsirkin break; 199699405162SMichael S. Tsirkin 199799405162SMichael S. Tsirkin case TUNDETACHFILTER: 199899405162SMichael S. Tsirkin /* Can be set only for TAPs */ 199999405162SMichael S. Tsirkin ret = -EINVAL; 200099405162SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) 200199405162SMichael S. Tsirkin break; 2002c8d68e6bSJason Wang ret = 0; 2003c8d68e6bSJason Wang tun_detach_filter(tun, tun->numqueues); 200499405162SMichael S. Tsirkin break; 200599405162SMichael S. Tsirkin 20061da177e4SLinus Torvalds default: 2007631ab46bSEric W. Biederman ret = -EINVAL; 2008631ab46bSEric W. Biederman break; 2009ee289b64SJoe Perches } 20101da177e4SLinus Torvalds 2011876bfd4dSHerbert Xu unlock: 2012876bfd4dSHerbert Xu rtnl_unlock(); 2013876bfd4dSHerbert Xu if (tun) 2014631ab46bSEric W. Biederman tun_put(tun); 2015631ab46bSEric W. Biederman return ret; 20161da177e4SLinus Torvalds } 20171da177e4SLinus Torvalds 201850857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file, 201950857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 202050857e2aSArnd Bergmann { 202150857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq)); 202250857e2aSArnd Bergmann } 202350857e2aSArnd Bergmann 202450857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 202550857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file, 202650857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 202750857e2aSArnd Bergmann { 202850857e2aSArnd Bergmann switch (cmd) { 202950857e2aSArnd Bergmann case TUNSETIFF: 203050857e2aSArnd Bergmann case TUNGETIFF: 203150857e2aSArnd Bergmann case TUNSETTXFILTER: 203250857e2aSArnd Bergmann case TUNGETSNDBUF: 203350857e2aSArnd Bergmann case TUNSETSNDBUF: 203450857e2aSArnd Bergmann case SIOCGIFHWADDR: 203550857e2aSArnd Bergmann case SIOCSIFHWADDR: 203650857e2aSArnd Bergmann arg = (unsigned long)compat_ptr(arg); 203750857e2aSArnd Bergmann break; 203850857e2aSArnd Bergmann default: 203950857e2aSArnd Bergmann arg = (compat_ulong_t)arg; 204050857e2aSArnd Bergmann break; 204150857e2aSArnd Bergmann } 204250857e2aSArnd Bergmann 204350857e2aSArnd Bergmann /* 204450857e2aSArnd Bergmann * compat_ifreq is shorter than ifreq, so we must not access beyond 204550857e2aSArnd Bergmann * the end of that structure. All fields that are used in this 204650857e2aSArnd Bergmann * driver are compatible though, we don't need to convert the 204750857e2aSArnd Bergmann * contents. 204850857e2aSArnd Bergmann */ 204950857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq)); 205050857e2aSArnd Bergmann } 205150857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */ 205250857e2aSArnd Bergmann 20531da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on) 20541da177e4SLinus Torvalds { 205554f968d6SJason Wang struct tun_file *tfile = file->private_data; 20561da177e4SLinus Torvalds int ret; 20571da177e4SLinus Torvalds 205854f968d6SJason Wang if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) 20599d319522SJonathan Corbet goto out; 20601da177e4SLinus Torvalds 20611da177e4SLinus Torvalds if (on) { 2062609d7fa9SEric W. Biederman ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0); 20631da177e4SLinus Torvalds if (ret) 20649d319522SJonathan Corbet goto out; 206554f968d6SJason Wang tfile->flags |= TUN_FASYNC; 20661da177e4SLinus Torvalds } else 206754f968d6SJason Wang tfile->flags &= ~TUN_FASYNC; 20689d319522SJonathan Corbet ret = 0; 20699d319522SJonathan Corbet out: 20709d319522SJonathan Corbet return ret; 20711da177e4SLinus Torvalds } 20721da177e4SLinus Torvalds 20731da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file) 20741da177e4SLinus Torvalds { 2075631ab46bSEric W. Biederman struct tun_file *tfile; 2076deed49fbSThomas Gleixner 20776b8a66eeSJoe Perches DBG1(KERN_INFO, "tunX: tun_chr_open\n"); 2078631ab46bSEric W. Biederman 207954f968d6SJason Wang tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL, 208054f968d6SJason Wang &tun_proto); 2081631ab46bSEric W. Biederman if (!tfile) 2082631ab46bSEric W. Biederman return -ENOMEM; 20836e914fc7SJason Wang rcu_assign_pointer(tfile->tun, NULL); 208436b50babSEric W. Biederman tfile->net = get_net(current->nsproxy->net_ns); 208554f968d6SJason Wang tfile->flags = 0; 208654f968d6SJason Wang 208754f968d6SJason Wang rcu_assign_pointer(tfile->socket.wq, &tfile->wq); 208854f968d6SJason Wang init_waitqueue_head(&tfile->wq.wait); 208954f968d6SJason Wang 209054f968d6SJason Wang tfile->socket.file = file; 209154f968d6SJason Wang tfile->socket.ops = &tun_socket_ops; 209254f968d6SJason Wang 209354f968d6SJason Wang sock_init_data(&tfile->socket, &tfile->sk); 209454f968d6SJason Wang sk_change_net(&tfile->sk, tfile->net); 209554f968d6SJason Wang 209654f968d6SJason Wang tfile->sk.sk_write_space = tun_sock_write_space; 209754f968d6SJason Wang tfile->sk.sk_sndbuf = INT_MAX; 209854f968d6SJason Wang 2099631ab46bSEric W. Biederman file->private_data = tfile; 210054f968d6SJason Wang set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags); 210154f968d6SJason Wang 21021da177e4SLinus Torvalds return 0; 21031da177e4SLinus Torvalds } 21041da177e4SLinus Torvalds 21051da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file) 21061da177e4SLinus Torvalds { 2107631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 210854f968d6SJason Wang struct net *net = tfile->net; 21091da177e4SLinus Torvalds 2110c8d68e6bSJason Wang tun_detach(tfile, true); 211154f968d6SJason Wang put_net(net); 21121da177e4SLinus Torvalds 21131da177e4SLinus Torvalds return 0; 21141da177e4SLinus Torvalds } 21151da177e4SLinus Torvalds 2116d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = { 21171da177e4SLinus Torvalds .owner = THIS_MODULE, 21181da177e4SLinus Torvalds .llseek = no_llseek, 2119ee0b3e67SBadari Pulavarty .read = do_sync_read, 2120ee0b3e67SBadari Pulavarty .aio_read = tun_chr_aio_read, 2121ee0b3e67SBadari Pulavarty .write = do_sync_write, 2122ee0b3e67SBadari Pulavarty .aio_write = tun_chr_aio_write, 21231da177e4SLinus Torvalds .poll = tun_chr_poll, 2124876bfd4dSHerbert Xu .unlocked_ioctl = tun_chr_ioctl, 212550857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 212650857e2aSArnd Bergmann .compat_ioctl = tun_chr_compat_ioctl, 212750857e2aSArnd Bergmann #endif 21281da177e4SLinus Torvalds .open = tun_chr_open, 21291da177e4SLinus Torvalds .release = tun_chr_close, 21301da177e4SLinus Torvalds .fasync = tun_chr_fasync 21311da177e4SLinus Torvalds }; 21321da177e4SLinus Torvalds 21331da177e4SLinus Torvalds static struct miscdevice tun_miscdev = { 21341da177e4SLinus Torvalds .minor = TUN_MINOR, 21351da177e4SLinus Torvalds .name = "tun", 2136e454cea2SKay Sievers .nodename = "net/tun", 21371da177e4SLinus Torvalds .fops = &tun_fops, 21381da177e4SLinus Torvalds }; 21391da177e4SLinus Torvalds 21401da177e4SLinus Torvalds /* ethtool interface */ 21411da177e4SLinus Torvalds 21421da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 21431da177e4SLinus Torvalds { 21441da177e4SLinus Torvalds cmd->supported = 0; 21451da177e4SLinus Torvalds cmd->advertising = 0; 214670739497SDavid Decotigny ethtool_cmd_speed_set(cmd, SPEED_10); 21471da177e4SLinus Torvalds cmd->duplex = DUPLEX_FULL; 21481da177e4SLinus Torvalds cmd->port = PORT_TP; 21491da177e4SLinus Torvalds cmd->phy_address = 0; 21501da177e4SLinus Torvalds cmd->transceiver = XCVR_INTERNAL; 21511da177e4SLinus Torvalds cmd->autoneg = AUTONEG_DISABLE; 21521da177e4SLinus Torvalds cmd->maxtxpkt = 0; 21531da177e4SLinus Torvalds cmd->maxrxpkt = 0; 21541da177e4SLinus Torvalds return 0; 21551da177e4SLinus Torvalds } 21561da177e4SLinus Torvalds 21571da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) 21581da177e4SLinus Torvalds { 21591da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 21601da177e4SLinus Torvalds 216133a5ba14SRick Jones strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); 216233a5ba14SRick Jones strlcpy(info->version, DRV_VERSION, sizeof(info->version)); 21631da177e4SLinus Torvalds 21641da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 21651da177e4SLinus Torvalds case TUN_TUN_DEV: 216633a5ba14SRick Jones strlcpy(info->bus_info, "tun", sizeof(info->bus_info)); 21671da177e4SLinus Torvalds break; 21681da177e4SLinus Torvalds case TUN_TAP_DEV: 216933a5ba14SRick Jones strlcpy(info->bus_info, "tap", sizeof(info->bus_info)); 21701da177e4SLinus Torvalds break; 21711da177e4SLinus Torvalds } 21721da177e4SLinus Torvalds } 21731da177e4SLinus Torvalds 21741da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev) 21751da177e4SLinus Torvalds { 21761da177e4SLinus Torvalds #ifdef TUN_DEBUG 21771da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 21781da177e4SLinus Torvalds return tun->debug; 21791da177e4SLinus Torvalds #else 21801da177e4SLinus Torvalds return -EOPNOTSUPP; 21811da177e4SLinus Torvalds #endif 21821da177e4SLinus Torvalds } 21831da177e4SLinus Torvalds 21841da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value) 21851da177e4SLinus Torvalds { 21861da177e4SLinus Torvalds #ifdef TUN_DEBUG 21871da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 21881da177e4SLinus Torvalds tun->debug = value; 21891da177e4SLinus Torvalds #endif 21901da177e4SLinus Torvalds } 21911da177e4SLinus Torvalds 21927282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = { 21931da177e4SLinus Torvalds .get_settings = tun_get_settings, 21941da177e4SLinus Torvalds .get_drvinfo = tun_get_drvinfo, 21951da177e4SLinus Torvalds .get_msglevel = tun_get_msglevel, 21961da177e4SLinus Torvalds .set_msglevel = tun_set_msglevel, 2197bee31369SNolan Leake .get_link = ethtool_op_get_link, 21981da177e4SLinus Torvalds }; 21991da177e4SLinus Torvalds 220079d17604SPavel Emelyanov 22011da177e4SLinus Torvalds static int __init tun_init(void) 22021da177e4SLinus Torvalds { 22031da177e4SLinus Torvalds int ret = 0; 22041da177e4SLinus Torvalds 22056b8a66eeSJoe Perches pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION); 22066b8a66eeSJoe Perches pr_info("%s\n", DRV_COPYRIGHT); 22071da177e4SLinus Torvalds 2208f019a7a5SEric W. Biederman ret = rtnl_link_register(&tun_link_ops); 220979d17604SPavel Emelyanov if (ret) { 22106b8a66eeSJoe Perches pr_err("Can't register link_ops\n"); 2211f019a7a5SEric W. Biederman goto err_linkops; 221279d17604SPavel Emelyanov } 221379d17604SPavel Emelyanov 22141da177e4SLinus Torvalds ret = misc_register(&tun_miscdev); 221579d17604SPavel Emelyanov if (ret) { 22166b8a66eeSJoe Perches pr_err("Can't register misc device %d\n", TUN_MINOR); 221779d17604SPavel Emelyanov goto err_misc; 221879d17604SPavel Emelyanov } 221979d17604SPavel Emelyanov return 0; 222079d17604SPavel Emelyanov err_misc: 2221f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 2222f019a7a5SEric W. Biederman err_linkops: 22231da177e4SLinus Torvalds return ret; 22241da177e4SLinus Torvalds } 22251da177e4SLinus Torvalds 22261da177e4SLinus Torvalds static void tun_cleanup(void) 22271da177e4SLinus Torvalds { 22281da177e4SLinus Torvalds misc_deregister(&tun_miscdev); 2229f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 22301da177e4SLinus Torvalds } 22311da177e4SLinus Torvalds 223205c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file. Returns error unless file is 223305c2828cSMichael S. Tsirkin * attached to a device. The returned object works like a packet socket, it 223405c2828cSMichael S. Tsirkin * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for 223505c2828cSMichael S. Tsirkin * holding a reference to the file for as long as the socket is in use. */ 223605c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file) 223705c2828cSMichael S. Tsirkin { 22386e914fc7SJason Wang struct tun_file *tfile; 223905c2828cSMichael S. Tsirkin if (file->f_op != &tun_fops) 224005c2828cSMichael S. Tsirkin return ERR_PTR(-EINVAL); 22416e914fc7SJason Wang tfile = file->private_data; 22426e914fc7SJason Wang if (!tfile) 224305c2828cSMichael S. Tsirkin return ERR_PTR(-EBADFD); 224454f968d6SJason Wang return &tfile->socket; 224505c2828cSMichael S. Tsirkin } 224605c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket); 224705c2828cSMichael S. Tsirkin 22481da177e4SLinus Torvalds module_init(tun_init); 22491da177e4SLinus Torvalds module_exit(tun_cleanup); 22501da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION); 22511da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT); 22521da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 22531da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR); 2254578454ffSKay Sievers MODULE_ALIAS("devname:net/tun"); 2255