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 118*96442e42SJason Wang #define TUN_FLOW_EXPIRE (3 * HZ) 119*96442e42SJason 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 12454f968d6SJason Wang * netdevice not for a specific queue (at least I didn't see the reqirement for 12554f968d6SJason Wang * this). 1266e914fc7SJason Wang * 1276e914fc7SJason Wang * RCU usage: 1286e914fc7SJason Wang * The tun_file and tun_struct are loosely coupled, the pointer from on 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 143*96442e42SJason Wang struct tun_flow_entry { 144*96442e42SJason Wang struct hlist_node hash_link; 145*96442e42SJason Wang struct rcu_head rcu; 146*96442e42SJason Wang struct tun_struct *tun; 147*96442e42SJason Wang 148*96442e42SJason Wang u32 rxhash; 149*96442e42SJason Wang int queue_index; 150*96442e42SJason Wang unsigned long updated; 151*96442e42SJason Wang }; 152*96442e42SJason Wang 153*96442e42SJason Wang #define TUN_NUM_FLOW_ENTRIES 1024 154*96442e42SJason Wang 15554f968d6SJason Wang /* Since the socket were moved to tun_file, to preserve the behavior of persist 15654f968d6SJason Wang * device, socket fileter, 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 180*96442e42SJason Wang spinlock_t lock; 181*96442e42SJason Wang struct kmem_cache *flow_cache; 182*96442e42SJason Wang struct hlist_head flows[TUN_NUM_FLOW_ENTRIES]; 183*96442e42SJason Wang struct timer_list flow_gc_timer; 184*96442e42SJason Wang unsigned long ageing_time; 18514daa021SRusty Russell }; 18614daa021SRusty Russell 187*96442e42SJason Wang static inline u32 tun_hashfn(u32 rxhash) 188*96442e42SJason Wang { 189*96442e42SJason Wang return rxhash & 0x3ff; 190*96442e42SJason Wang } 191*96442e42SJason Wang 192*96442e42SJason Wang static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash) 193*96442e42SJason Wang { 194*96442e42SJason Wang struct tun_flow_entry *e; 195*96442e42SJason Wang struct hlist_node *n; 196*96442e42SJason Wang 197*96442e42SJason Wang hlist_for_each_entry_rcu(e, n, head, hash_link) { 198*96442e42SJason Wang if (e->rxhash == rxhash) 199*96442e42SJason Wang return e; 200*96442e42SJason Wang } 201*96442e42SJason Wang return NULL; 202*96442e42SJason Wang } 203*96442e42SJason Wang 204*96442e42SJason Wang static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun, 205*96442e42SJason Wang struct hlist_head *head, 206*96442e42SJason Wang u32 rxhash, u16 queue_index) 207*96442e42SJason Wang { 208*96442e42SJason Wang struct tun_flow_entry *e = kmem_cache_alloc(tun->flow_cache, 209*96442e42SJason Wang GFP_ATOMIC); 210*96442e42SJason Wang if (e) { 211*96442e42SJason Wang tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n", 212*96442e42SJason Wang rxhash, queue_index); 213*96442e42SJason Wang e->updated = jiffies; 214*96442e42SJason Wang e->rxhash = rxhash; 215*96442e42SJason Wang e->queue_index = queue_index; 216*96442e42SJason Wang e->tun = tun; 217*96442e42SJason Wang hlist_add_head_rcu(&e->hash_link, head); 218*96442e42SJason Wang } 219*96442e42SJason Wang return e; 220*96442e42SJason Wang } 221*96442e42SJason Wang 222*96442e42SJason Wang static void tun_flow_free(struct rcu_head *head) 223*96442e42SJason Wang { 224*96442e42SJason Wang struct tun_flow_entry *e 225*96442e42SJason Wang = container_of(head, struct tun_flow_entry, rcu); 226*96442e42SJason Wang kmem_cache_free(e->tun->flow_cache, e); 227*96442e42SJason Wang } 228*96442e42SJason Wang 229*96442e42SJason Wang static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e) 230*96442e42SJason Wang { 231*96442e42SJason Wang tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n", 232*96442e42SJason Wang e->rxhash, e->queue_index); 233*96442e42SJason Wang hlist_del_rcu(&e->hash_link); 234*96442e42SJason Wang call_rcu(&e->rcu, tun_flow_free); 235*96442e42SJason Wang } 236*96442e42SJason Wang 237*96442e42SJason Wang static void tun_flow_flush(struct tun_struct *tun) 238*96442e42SJason Wang { 239*96442e42SJason Wang int i; 240*96442e42SJason Wang 241*96442e42SJason Wang spin_lock_bh(&tun->lock); 242*96442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 243*96442e42SJason Wang struct tun_flow_entry *e; 244*96442e42SJason Wang struct hlist_node *h, *n; 245*96442e42SJason Wang 246*96442e42SJason Wang hlist_for_each_entry_safe(e, h, n, &tun->flows[i], hash_link) 247*96442e42SJason Wang tun_flow_delete(tun, e); 248*96442e42SJason Wang } 249*96442e42SJason Wang spin_unlock_bh(&tun->lock); 250*96442e42SJason Wang } 251*96442e42SJason Wang 252*96442e42SJason Wang static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index) 253*96442e42SJason Wang { 254*96442e42SJason Wang int i; 255*96442e42SJason Wang 256*96442e42SJason Wang spin_lock_bh(&tun->lock); 257*96442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 258*96442e42SJason Wang struct tun_flow_entry *e; 259*96442e42SJason Wang struct hlist_node *h, *n; 260*96442e42SJason Wang 261*96442e42SJason Wang hlist_for_each_entry_safe(e, h, n, &tun->flows[i], hash_link) { 262*96442e42SJason Wang if (e->queue_index == queue_index) 263*96442e42SJason Wang tun_flow_delete(tun, e); 264*96442e42SJason Wang } 265*96442e42SJason Wang } 266*96442e42SJason Wang spin_unlock_bh(&tun->lock); 267*96442e42SJason Wang } 268*96442e42SJason Wang 269*96442e42SJason Wang static void tun_flow_cleanup(unsigned long data) 270*96442e42SJason Wang { 271*96442e42SJason Wang struct tun_struct *tun = (struct tun_struct *)data; 272*96442e42SJason Wang unsigned long delay = tun->ageing_time; 273*96442e42SJason Wang unsigned long next_timer = jiffies + delay; 274*96442e42SJason Wang unsigned long count = 0; 275*96442e42SJason Wang int i; 276*96442e42SJason Wang 277*96442e42SJason Wang tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n"); 278*96442e42SJason Wang 279*96442e42SJason Wang spin_lock_bh(&tun->lock); 280*96442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 281*96442e42SJason Wang struct tun_flow_entry *e; 282*96442e42SJason Wang struct hlist_node *h, *n; 283*96442e42SJason Wang 284*96442e42SJason Wang hlist_for_each_entry_safe(e, h, n, &tun->flows[i], hash_link) { 285*96442e42SJason Wang unsigned long this_timer; 286*96442e42SJason Wang count++; 287*96442e42SJason Wang this_timer = e->updated + delay; 288*96442e42SJason Wang if (time_before_eq(this_timer, jiffies)) 289*96442e42SJason Wang tun_flow_delete(tun, e); 290*96442e42SJason Wang else if (time_before(this_timer, next_timer)) 291*96442e42SJason Wang next_timer = this_timer; 292*96442e42SJason Wang } 293*96442e42SJason Wang } 294*96442e42SJason Wang 295*96442e42SJason Wang if (count) 296*96442e42SJason Wang mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer)); 297*96442e42SJason Wang spin_unlock_bh(&tun->lock); 298*96442e42SJason Wang } 299*96442e42SJason Wang 300*96442e42SJason Wang static void tun_flow_update(struct tun_struct *tun, struct sk_buff *skb, 301*96442e42SJason Wang u16 queue_index) 302*96442e42SJason Wang { 303*96442e42SJason Wang struct hlist_head *head; 304*96442e42SJason Wang struct tun_flow_entry *e; 305*96442e42SJason Wang unsigned long delay = tun->ageing_time; 306*96442e42SJason Wang u32 rxhash = skb_get_rxhash(skb); 307*96442e42SJason Wang 308*96442e42SJason Wang if (!rxhash) 309*96442e42SJason Wang return; 310*96442e42SJason Wang else 311*96442e42SJason Wang head = &tun->flows[tun_hashfn(rxhash)]; 312*96442e42SJason Wang 313*96442e42SJason Wang rcu_read_lock(); 314*96442e42SJason Wang 315*96442e42SJason Wang if (tun->numqueues == 1) 316*96442e42SJason Wang goto unlock; 317*96442e42SJason Wang 318*96442e42SJason Wang e = tun_flow_find(head, rxhash); 319*96442e42SJason Wang if (likely(e)) { 320*96442e42SJason Wang /* TODO: keep queueing to old queue until it's empty? */ 321*96442e42SJason Wang e->queue_index = queue_index; 322*96442e42SJason Wang e->updated = jiffies; 323*96442e42SJason Wang } else { 324*96442e42SJason Wang spin_lock_bh(&tun->lock); 325*96442e42SJason Wang if (!tun_flow_find(head, rxhash)) 326*96442e42SJason Wang tun_flow_create(tun, head, rxhash, queue_index); 327*96442e42SJason Wang 328*96442e42SJason Wang if (!timer_pending(&tun->flow_gc_timer)) 329*96442e42SJason Wang mod_timer(&tun->flow_gc_timer, 330*96442e42SJason Wang round_jiffies_up(jiffies + delay)); 331*96442e42SJason Wang spin_unlock_bh(&tun->lock); 332*96442e42SJason Wang } 333*96442e42SJason Wang 334*96442e42SJason Wang unlock: 335*96442e42SJason Wang rcu_read_unlock(); 336*96442e42SJason Wang } 337*96442e42SJason 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); 348*96442e42SJason 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) { 357*96442e42SJason Wang e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq); 358*96442e42SJason Wang if (e) 359*96442e42SJason Wang txq = e->queue_index; 360*96442e42SJason 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(); 376cde8b15fSJason Wang 377cde8b15fSJason Wang return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) || 378cde8b15fSJason Wang (gid_valid(tun->group) && !in_egroup_p(tun->group))) && 379cde8b15fSJason Wang !capable(CAP_NET_ADMIN); 380cde8b15fSJason Wang } 381cde8b15fSJason Wang 382c8d68e6bSJason Wang static void tun_set_real_num_queues(struct tun_struct *tun) 383c8d68e6bSJason Wang { 384c8d68e6bSJason Wang netif_set_real_num_tx_queues(tun->dev, tun->numqueues); 385c8d68e6bSJason Wang netif_set_real_num_rx_queues(tun->dev, tun->numqueues); 386c8d68e6bSJason Wang } 387c8d68e6bSJason Wang 388c8d68e6bSJason Wang static void __tun_detach(struct tun_file *tfile, bool clean) 389c8d68e6bSJason Wang { 390c8d68e6bSJason Wang struct tun_file *ntfile; 391c8d68e6bSJason Wang struct tun_struct *tun; 392c8d68e6bSJason Wang struct net_device *dev; 393c8d68e6bSJason Wang 394c8d68e6bSJason Wang tun = rcu_dereference_protected(tfile->tun, 395c8d68e6bSJason Wang lockdep_rtnl_is_held()); 396c8d68e6bSJason Wang if (tun) { 397c8d68e6bSJason Wang u16 index = tfile->queue_index; 398c8d68e6bSJason Wang BUG_ON(index >= tun->numqueues); 399c8d68e6bSJason Wang dev = tun->dev; 400c8d68e6bSJason Wang 401c8d68e6bSJason Wang rcu_assign_pointer(tun->tfiles[index], 402c8d68e6bSJason Wang tun->tfiles[tun->numqueues - 1]); 403c8d68e6bSJason Wang rcu_assign_pointer(tfile->tun, NULL); 404c8d68e6bSJason Wang ntfile = rcu_dereference_protected(tun->tfiles[index], 405c8d68e6bSJason Wang lockdep_rtnl_is_held()); 406c8d68e6bSJason Wang ntfile->queue_index = index; 407c8d68e6bSJason Wang 408c8d68e6bSJason Wang --tun->numqueues; 409c8d68e6bSJason Wang sock_put(&tfile->sk); 410c8d68e6bSJason Wang 411c8d68e6bSJason Wang synchronize_net(); 412*96442e42SJason Wang tun_flow_delete_by_queue(tun, tun->numqueues + 1); 413c8d68e6bSJason Wang /* Drop read queue */ 414c8d68e6bSJason Wang skb_queue_purge(&tfile->sk.sk_receive_queue); 415c8d68e6bSJason Wang tun_set_real_num_queues(tun); 416c8d68e6bSJason Wang 417c8d68e6bSJason Wang if (tun->numqueues == 0 && !(tun->flags & TUN_PERSIST)) 418c8d68e6bSJason Wang if (dev->reg_state == NETREG_REGISTERED) 419c8d68e6bSJason Wang unregister_netdevice(dev); 420c8d68e6bSJason Wang } 421c8d68e6bSJason Wang 422c8d68e6bSJason Wang if (clean) { 423c8d68e6bSJason Wang BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED, 424c8d68e6bSJason Wang &tfile->socket.flags)); 425c8d68e6bSJason Wang sk_release_kernel(&tfile->sk); 426c8d68e6bSJason Wang } 427c8d68e6bSJason Wang } 428c8d68e6bSJason Wang 429c8d68e6bSJason Wang static void tun_detach(struct tun_file *tfile, bool clean) 430c8d68e6bSJason Wang { 431c8d68e6bSJason Wang rtnl_lock(); 432c8d68e6bSJason Wang __tun_detach(tfile, clean); 433c8d68e6bSJason Wang rtnl_unlock(); 434c8d68e6bSJason Wang } 435c8d68e6bSJason Wang 436c8d68e6bSJason Wang static void tun_detach_all(struct net_device *dev) 437c8d68e6bSJason Wang { 438c8d68e6bSJason Wang struct tun_struct *tun = netdev_priv(dev); 439c8d68e6bSJason Wang struct tun_file *tfile; 440c8d68e6bSJason Wang int i, n = tun->numqueues; 441c8d68e6bSJason Wang 442c8d68e6bSJason Wang for (i = 0; i < n; i++) { 443c8d68e6bSJason Wang tfile = rcu_dereference_protected(tun->tfiles[i], 444c8d68e6bSJason Wang lockdep_rtnl_is_held()); 445c8d68e6bSJason Wang BUG_ON(!tfile); 446c8d68e6bSJason Wang wake_up_all(&tfile->wq.wait); 447c8d68e6bSJason Wang rcu_assign_pointer(tfile->tun, NULL); 448c8d68e6bSJason Wang --tun->numqueues; 449c8d68e6bSJason Wang } 450c8d68e6bSJason Wang BUG_ON(tun->numqueues != 0); 451c8d68e6bSJason Wang 452c8d68e6bSJason Wang synchronize_net(); 453c8d68e6bSJason Wang for (i = 0; i < n; i++) { 454c8d68e6bSJason Wang tfile = rcu_dereference_protected(tun->tfiles[i], 455c8d68e6bSJason Wang lockdep_rtnl_is_held()); 456c8d68e6bSJason Wang /* Drop read queue */ 457c8d68e6bSJason Wang skb_queue_purge(&tfile->sk.sk_receive_queue); 458c8d68e6bSJason Wang sock_put(&tfile->sk); 459c8d68e6bSJason Wang } 460c8d68e6bSJason Wang } 461c8d68e6bSJason Wang 462a7385ba2SEric W. Biederman static int tun_attach(struct tun_struct *tun, struct file *file) 463a7385ba2SEric W. Biederman { 464631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 46538231b7aSEric W. Biederman int err; 466a7385ba2SEric W. Biederman 46738231b7aSEric W. Biederman err = -EINVAL; 468c8d68e6bSJason Wang if (rcu_dereference_protected(tfile->tun, lockdep_rtnl_is_held())) 46938231b7aSEric W. Biederman goto out; 47038231b7aSEric W. Biederman 47138231b7aSEric W. Biederman err = -EBUSY; 472c8d68e6bSJason Wang if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1) 473c8d68e6bSJason Wang goto out; 474c8d68e6bSJason Wang 475c8d68e6bSJason Wang err = -E2BIG; 476c8d68e6bSJason Wang if (tun->numqueues == MAX_TAP_QUEUES) 47738231b7aSEric W. Biederman goto out; 47838231b7aSEric W. Biederman 47938231b7aSEric W. Biederman err = 0; 48054f968d6SJason Wang 481c8d68e6bSJason Wang /* Re-attach the filter to presist device */ 48254f968d6SJason Wang if (tun->filter_attached == true) { 48354f968d6SJason Wang err = sk_attach_filter(&tun->fprog, tfile->socket.sk); 48454f968d6SJason Wang if (!err) 48554f968d6SJason Wang goto out; 48654f968d6SJason Wang } 487c8d68e6bSJason Wang tfile->queue_index = tun->numqueues; 4886e914fc7SJason Wang rcu_assign_pointer(tfile->tun, tun); 489c8d68e6bSJason Wang rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); 49054f968d6SJason Wang sock_hold(&tfile->sk); 491c8d68e6bSJason Wang tun->numqueues++; 492c8d68e6bSJason Wang 493c8d68e6bSJason Wang tun_set_real_num_queues(tun); 494c8d68e6bSJason Wang 495c8d68e6bSJason Wang if (tun->numqueues == 1) 496c8d68e6bSJason Wang netif_carrier_on(tun->dev); 497c8d68e6bSJason Wang 498c8d68e6bSJason Wang /* device is allowed to go away first, so no need to hold extra 499c8d68e6bSJason Wang * refcnt. 500c8d68e6bSJason Wang */ 501a7385ba2SEric W. Biederman 50238231b7aSEric W. Biederman out: 50338231b7aSEric W. Biederman return err; 504a7385ba2SEric W. Biederman } 505a7385ba2SEric W. Biederman 506631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile) 507631ab46bSEric W. Biederman { 5086e914fc7SJason Wang struct tun_struct *tun; 509c70f1829SEric W. Biederman 5106e914fc7SJason Wang rcu_read_lock(); 5116e914fc7SJason Wang tun = rcu_dereference(tfile->tun); 5126e914fc7SJason Wang if (tun) 5136e914fc7SJason Wang dev_hold(tun->dev); 5146e914fc7SJason Wang rcu_read_unlock(); 515c70f1829SEric W. Biederman 516c70f1829SEric W. Biederman return tun; 517631ab46bSEric W. Biederman } 518631ab46bSEric W. Biederman 519631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file) 520631ab46bSEric W. Biederman { 521631ab46bSEric W. Biederman return __tun_get(file->private_data); 522631ab46bSEric W. Biederman } 523631ab46bSEric W. Biederman 524631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun) 525631ab46bSEric W. Biederman { 5266e914fc7SJason Wang dev_put(tun->dev); 527631ab46bSEric W. Biederman } 528631ab46bSEric W. Biederman 5296b8a66eeSJoe Perches /* TAP filtering */ 530f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr) 531f271b2ccSMax Krasnyansky { 532f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 533f271b2ccSMax Krasnyansky mask[n >> 5] |= (1 << (n & 31)); 534f271b2ccSMax Krasnyansky } 535f271b2ccSMax Krasnyansky 536f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) 537f271b2ccSMax Krasnyansky { 538f271b2ccSMax Krasnyansky int n = ether_crc(ETH_ALEN, addr) >> 26; 539f271b2ccSMax Krasnyansky return mask[n >> 5] & (1 << (n & 31)); 540f271b2ccSMax Krasnyansky } 541f271b2ccSMax Krasnyansky 542f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg) 543f271b2ccSMax Krasnyansky { 544f271b2ccSMax Krasnyansky struct { u8 u[ETH_ALEN]; } *addr; 545f271b2ccSMax Krasnyansky struct tun_filter uf; 546f271b2ccSMax Krasnyansky int err, alen, n, nexact; 547f271b2ccSMax Krasnyansky 548f271b2ccSMax Krasnyansky if (copy_from_user(&uf, arg, sizeof(uf))) 549f271b2ccSMax Krasnyansky return -EFAULT; 550f271b2ccSMax Krasnyansky 551f271b2ccSMax Krasnyansky if (!uf.count) { 552f271b2ccSMax Krasnyansky /* Disabled */ 553f271b2ccSMax Krasnyansky filter->count = 0; 554f271b2ccSMax Krasnyansky return 0; 555f271b2ccSMax Krasnyansky } 556f271b2ccSMax Krasnyansky 557f271b2ccSMax Krasnyansky alen = ETH_ALEN * uf.count; 558f271b2ccSMax Krasnyansky addr = kmalloc(alen, GFP_KERNEL); 559f271b2ccSMax Krasnyansky if (!addr) 560f271b2ccSMax Krasnyansky return -ENOMEM; 561f271b2ccSMax Krasnyansky 562f271b2ccSMax Krasnyansky if (copy_from_user(addr, arg + sizeof(uf), alen)) { 563f271b2ccSMax Krasnyansky err = -EFAULT; 564f271b2ccSMax Krasnyansky goto done; 565f271b2ccSMax Krasnyansky } 566f271b2ccSMax Krasnyansky 567f271b2ccSMax Krasnyansky /* The filter is updated without holding any locks. Which is 568f271b2ccSMax Krasnyansky * perfectly safe. We disable it first and in the worst 569f271b2ccSMax Krasnyansky * case we'll accept a few undesired packets. */ 570f271b2ccSMax Krasnyansky filter->count = 0; 571f271b2ccSMax Krasnyansky wmb(); 572f271b2ccSMax Krasnyansky 573f271b2ccSMax Krasnyansky /* Use first set of addresses as an exact filter */ 574f271b2ccSMax Krasnyansky for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++) 575f271b2ccSMax Krasnyansky memcpy(filter->addr[n], addr[n].u, ETH_ALEN); 576f271b2ccSMax Krasnyansky 577f271b2ccSMax Krasnyansky nexact = n; 578f271b2ccSMax Krasnyansky 579cfbf84fcSAlex Williamson /* Remaining multicast addresses are hashed, 580cfbf84fcSAlex Williamson * unicast will leave the filter disabled. */ 581f271b2ccSMax Krasnyansky memset(filter->mask, 0, sizeof(filter->mask)); 582cfbf84fcSAlex Williamson for (; n < uf.count; n++) { 583cfbf84fcSAlex Williamson if (!is_multicast_ether_addr(addr[n].u)) { 584cfbf84fcSAlex Williamson err = 0; /* no filter */ 585cfbf84fcSAlex Williamson goto done; 586cfbf84fcSAlex Williamson } 587f271b2ccSMax Krasnyansky addr_hash_set(filter->mask, addr[n].u); 588cfbf84fcSAlex Williamson } 589f271b2ccSMax Krasnyansky 590f271b2ccSMax Krasnyansky /* For ALLMULTI just set the mask to all ones. 591f271b2ccSMax Krasnyansky * This overrides the mask populated above. */ 592f271b2ccSMax Krasnyansky if ((uf.flags & TUN_FLT_ALLMULTI)) 593f271b2ccSMax Krasnyansky memset(filter->mask, ~0, sizeof(filter->mask)); 594f271b2ccSMax Krasnyansky 595f271b2ccSMax Krasnyansky /* Now enable the filter */ 596f271b2ccSMax Krasnyansky wmb(); 597f271b2ccSMax Krasnyansky filter->count = nexact; 598f271b2ccSMax Krasnyansky 599f271b2ccSMax Krasnyansky /* Return the number of exact filters */ 600f271b2ccSMax Krasnyansky err = nexact; 601f271b2ccSMax Krasnyansky 602f271b2ccSMax Krasnyansky done: 603f271b2ccSMax Krasnyansky kfree(addr); 604f271b2ccSMax Krasnyansky return err; 605f271b2ccSMax Krasnyansky } 606f271b2ccSMax Krasnyansky 607f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */ 608f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb) 609f271b2ccSMax Krasnyansky { 610f271b2ccSMax Krasnyansky /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect 611f271b2ccSMax Krasnyansky * at this point. */ 612f271b2ccSMax Krasnyansky struct ethhdr *eh = (struct ethhdr *) skb->data; 613f271b2ccSMax Krasnyansky int i; 614f271b2ccSMax Krasnyansky 615f271b2ccSMax Krasnyansky /* Exact match */ 616f271b2ccSMax Krasnyansky for (i = 0; i < filter->count; i++) 6172e42e474SJoe Perches if (ether_addr_equal(eh->h_dest, filter->addr[i])) 618f271b2ccSMax Krasnyansky return 1; 619f271b2ccSMax Krasnyansky 620f271b2ccSMax Krasnyansky /* Inexact match (multicast only) */ 621f271b2ccSMax Krasnyansky if (is_multicast_ether_addr(eh->h_dest)) 622f271b2ccSMax Krasnyansky return addr_hash_test(filter->mask, eh->h_dest); 623f271b2ccSMax Krasnyansky 624f271b2ccSMax Krasnyansky return 0; 625f271b2ccSMax Krasnyansky } 626f271b2ccSMax Krasnyansky 627f271b2ccSMax Krasnyansky /* 628f271b2ccSMax Krasnyansky * Checks whether the packet is accepted or not. 629f271b2ccSMax Krasnyansky * Returns: 0 - drop, !=0 - accept 630f271b2ccSMax Krasnyansky */ 631f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb) 632f271b2ccSMax Krasnyansky { 633f271b2ccSMax Krasnyansky if (!filter->count) 634f271b2ccSMax Krasnyansky return 1; 635f271b2ccSMax Krasnyansky 636f271b2ccSMax Krasnyansky return run_filter(filter, skb); 637f271b2ccSMax Krasnyansky } 638f271b2ccSMax Krasnyansky 6391da177e4SLinus Torvalds /* Network device part of the driver */ 6401da177e4SLinus Torvalds 6417282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops; 6421da177e4SLinus Torvalds 643c70f1829SEric W. Biederman /* Net device detach from fd. */ 644c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev) 645c70f1829SEric W. Biederman { 646c8d68e6bSJason Wang tun_detach_all(dev); 647c70f1829SEric W. Biederman } 648c70f1829SEric W. Biederman 6491da177e4SLinus Torvalds /* Net device open. */ 6501da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev) 6511da177e4SLinus Torvalds { 652c8d68e6bSJason Wang netif_tx_start_all_queues(dev); 6531da177e4SLinus Torvalds return 0; 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds /* Net device close. */ 6571da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev) 6581da177e4SLinus Torvalds { 659c8d68e6bSJason Wang netif_tx_stop_all_queues(dev); 6601da177e4SLinus Torvalds return 0; 6611da177e4SLinus Torvalds } 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds /* Net device start xmit */ 664424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) 6651da177e4SLinus Torvalds { 6661da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 667c8d68e6bSJason Wang int txq = skb->queue_mapping; 6686e914fc7SJason Wang struct tun_file *tfile; 6691da177e4SLinus Torvalds 6706e914fc7SJason Wang rcu_read_lock(); 671c8d68e6bSJason Wang tfile = rcu_dereference(tun->tfiles[txq]); 672c8d68e6bSJason Wang 6731da177e4SLinus Torvalds /* Drop packet if interface is not attached */ 674c8d68e6bSJason Wang if (txq >= tun->numqueues) 6751da177e4SLinus Torvalds goto drop; 6761da177e4SLinus Torvalds 6776e914fc7SJason Wang tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len); 6786e914fc7SJason Wang 679c8d68e6bSJason Wang BUG_ON(!tfile); 680c8d68e6bSJason Wang 681f271b2ccSMax Krasnyansky /* Drop if the filter does not like it. 682f271b2ccSMax Krasnyansky * This is a noop if the filter is disabled. 683f271b2ccSMax Krasnyansky * Filter can be enabled only for the TAP devices. */ 684f271b2ccSMax Krasnyansky if (!check_filter(&tun->txflt, skb)) 685f271b2ccSMax Krasnyansky goto drop; 686f271b2ccSMax Krasnyansky 68754f968d6SJason Wang if (tfile->socket.sk->sk_filter && 68854f968d6SJason Wang sk_filter(tfile->socket.sk, skb)) 68999405162SMichael S. Tsirkin goto drop; 69099405162SMichael S. Tsirkin 691c8d68e6bSJason Wang /* Limit the number of packets queued by divining txq length with the 692c8d68e6bSJason Wang * number of queues. 693c8d68e6bSJason Wang */ 69454f968d6SJason Wang if (skb_queue_len(&tfile->socket.sk->sk_receive_queue) 695c8d68e6bSJason Wang >= dev->tx_queue_len / tun->numqueues){ 6961da177e4SLinus Torvalds if (!(tun->flags & TUN_ONE_QUEUE)) { 6971da177e4SLinus Torvalds /* Normal queueing mode. */ 6981da177e4SLinus Torvalds /* Packet scheduler handles dropping of further packets. */ 699c8d68e6bSJason Wang netif_stop_subqueue(dev, txq); 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds /* We won't see all dropped packets individually, so overrun 7021da177e4SLinus Torvalds * error is more appropriate. */ 70309f75cd7SJeff Garzik dev->stats.tx_fifo_errors++; 7041da177e4SLinus Torvalds } else { 7051da177e4SLinus Torvalds /* Single queue mode. 7061da177e4SLinus Torvalds * Driver handles dropping of all packets itself. */ 7071da177e4SLinus Torvalds goto drop; 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds } 7101da177e4SLinus Torvalds 7110110d6f2SMichael S. Tsirkin /* Orphan the skb - required as we might hang on to it 7120110d6f2SMichael S. Tsirkin * for indefinite time. */ 713868eefebSMichael S. Tsirkin if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC))) 714868eefebSMichael S. Tsirkin goto drop; 7150110d6f2SMichael S. Tsirkin skb_orphan(skb); 7160110d6f2SMichael S. Tsirkin 717f271b2ccSMax Krasnyansky /* Enqueue packet */ 71854f968d6SJason Wang skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb); 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds /* Notify and wake up reader process */ 72154f968d6SJason Wang if (tfile->flags & TUN_FASYNC) 72254f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_IN); 72354f968d6SJason Wang wake_up_interruptible_poll(&tfile->wq.wait, POLLIN | 72405c2828cSMichael S. Tsirkin POLLRDNORM | POLLRDBAND); 7256e914fc7SJason Wang 7266e914fc7SJason Wang rcu_read_unlock(); 7276ed10654SPatrick McHardy return NETDEV_TX_OK; 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds drop: 73009f75cd7SJeff Garzik dev->stats.tx_dropped++; 7311da177e4SLinus Torvalds kfree_skb(skb); 7326e914fc7SJason Wang rcu_read_unlock(); 7336ed10654SPatrick McHardy return NETDEV_TX_OK; 7341da177e4SLinus Torvalds } 7351da177e4SLinus Torvalds 736f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev) 7371da177e4SLinus Torvalds { 738f271b2ccSMax Krasnyansky /* 739f271b2ccSMax Krasnyansky * This callback is supposed to deal with mc filter in 740f271b2ccSMax Krasnyansky * _rx_ path and has nothing to do with the _tx_ path. 741f271b2ccSMax Krasnyansky * In rx path we always accept everything userspace gives us. 742f271b2ccSMax Krasnyansky */ 7431da177e4SLinus Torvalds } 7441da177e4SLinus Torvalds 7454885a504SEd Swierk #define MIN_MTU 68 7464885a504SEd Swierk #define MAX_MTU 65535 7474885a504SEd Swierk 7484885a504SEd Swierk static int 7494885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu) 7504885a504SEd Swierk { 7514885a504SEd Swierk if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU) 7524885a504SEd Swierk return -EINVAL; 7534885a504SEd Swierk dev->mtu = new_mtu; 7544885a504SEd Swierk return 0; 7554885a504SEd Swierk } 7564885a504SEd Swierk 757c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev, 758c8f44affSMichał Mirosław netdev_features_t features) 75988255375SMichał Mirosław { 76088255375SMichał Mirosław struct tun_struct *tun = netdev_priv(dev); 76188255375SMichał Mirosław 76288255375SMichał Mirosław return (features & tun->set_features) | (features & ~TUN_USER_FEATURES); 76388255375SMichał Mirosław } 764bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 765bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev) 766bebd097aSNeil Horman { 767bebd097aSNeil Horman /* 768bebd097aSNeil Horman * Tun only receives frames when: 769bebd097aSNeil Horman * 1) the char device endpoint gets data from user space 770bebd097aSNeil Horman * 2) the tun socket gets a sendmsg call from user space 771bebd097aSNeil Horman * Since both of those are syncronous operations, we are guaranteed 772bebd097aSNeil Horman * never to have pending data when we poll for it 773bebd097aSNeil Horman * so theres nothing to do here but return. 774bebd097aSNeil Horman * We need this though so netpoll recognizes us as an interface that 775bebd097aSNeil Horman * supports polling, which enables bridge devices in virt setups to 776bebd097aSNeil Horman * still use netconsole 777bebd097aSNeil Horman */ 778bebd097aSNeil Horman return; 779bebd097aSNeil Horman } 780bebd097aSNeil Horman #endif 781758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = { 782c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 783758e43b7SStephen Hemminger .ndo_open = tun_net_open, 784758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 78500829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 786758e43b7SStephen Hemminger .ndo_change_mtu = tun_net_change_mtu, 78788255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 788c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 789bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 790bebd097aSNeil Horman .ndo_poll_controller = tun_poll_controller, 791bebd097aSNeil Horman #endif 792758e43b7SStephen Hemminger }; 793758e43b7SStephen Hemminger 794758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = { 795c70f1829SEric W. Biederman .ndo_uninit = tun_net_uninit, 796758e43b7SStephen Hemminger .ndo_open = tun_net_open, 797758e43b7SStephen Hemminger .ndo_stop = tun_net_close, 79800829823SStephen Hemminger .ndo_start_xmit = tun_net_xmit, 799758e43b7SStephen Hemminger .ndo_change_mtu = tun_net_change_mtu, 80088255375SMichał Mirosław .ndo_fix_features = tun_net_fix_features, 801afc4b13dSJiri Pirko .ndo_set_rx_mode = tun_net_mclist, 802758e43b7SStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 803758e43b7SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 804c8d68e6bSJason Wang .ndo_select_queue = tun_select_queue, 805bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER 806bebd097aSNeil Horman .ndo_poll_controller = tun_poll_controller, 807bebd097aSNeil Horman #endif 808758e43b7SStephen Hemminger }; 809758e43b7SStephen Hemminger 810*96442e42SJason Wang static int tun_flow_init(struct tun_struct *tun) 811*96442e42SJason Wang { 812*96442e42SJason Wang int i; 813*96442e42SJason Wang 814*96442e42SJason Wang tun->flow_cache = kmem_cache_create("tun_flow_cache", 815*96442e42SJason Wang sizeof(struct tun_flow_entry), 0, 0, 816*96442e42SJason Wang NULL); 817*96442e42SJason Wang if (!tun->flow_cache) 818*96442e42SJason Wang return -ENOMEM; 819*96442e42SJason Wang 820*96442e42SJason Wang for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) 821*96442e42SJason Wang INIT_HLIST_HEAD(&tun->flows[i]); 822*96442e42SJason Wang 823*96442e42SJason Wang tun->ageing_time = TUN_FLOW_EXPIRE; 824*96442e42SJason Wang setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun); 825*96442e42SJason Wang mod_timer(&tun->flow_gc_timer, 826*96442e42SJason Wang round_jiffies_up(jiffies + tun->ageing_time)); 827*96442e42SJason Wang 828*96442e42SJason Wang return 0; 829*96442e42SJason Wang } 830*96442e42SJason Wang 831*96442e42SJason Wang static void tun_flow_uninit(struct tun_struct *tun) 832*96442e42SJason Wang { 833*96442e42SJason Wang del_timer_sync(&tun->flow_gc_timer); 834*96442e42SJason Wang tun_flow_flush(tun); 835*96442e42SJason Wang 836*96442e42SJason Wang /* Wait for completion of call_rcu()'s */ 837*96442e42SJason Wang rcu_barrier(); 838*96442e42SJason Wang kmem_cache_destroy(tun->flow_cache); 839*96442e42SJason Wang } 840*96442e42SJason Wang 8411da177e4SLinus Torvalds /* Initialize net device. */ 8421da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev) 8431da177e4SLinus Torvalds { 8441da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 8471da177e4SLinus Torvalds case TUN_TUN_DEV: 848758e43b7SStephen Hemminger dev->netdev_ops = &tun_netdev_ops; 849758e43b7SStephen Hemminger 8501da177e4SLinus Torvalds /* Point-to-Point TUN Device */ 8511da177e4SLinus Torvalds dev->hard_header_len = 0; 8521da177e4SLinus Torvalds dev->addr_len = 0; 8531da177e4SLinus Torvalds dev->mtu = 1500; 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds /* Zero header length */ 8561da177e4SLinus Torvalds dev->type = ARPHRD_NONE; 8571da177e4SLinus Torvalds dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 8581da177e4SLinus Torvalds dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ 8591da177e4SLinus Torvalds break; 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds case TUN_TAP_DEV: 8627a0a9608SKusanagi Kouichi dev->netdev_ops = &tap_netdev_ops; 8631da177e4SLinus Torvalds /* Ethernet TAP Device */ 8641da177e4SLinus Torvalds ether_setup(dev); 865550fd08cSNeil Horman dev->priv_flags &= ~IFF_TX_SKB_SHARING; 86636226a8dSBrian Braunstein 867f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 86836226a8dSBrian Braunstein 8691da177e4SLinus Torvalds dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ 8701da177e4SLinus Torvalds break; 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds /* Character device part */ 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds /* Poll */ 8771da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table *wait) 8781da177e4SLinus Torvalds { 879b2430de3SEric W. Biederman struct tun_file *tfile = file->private_data; 880b2430de3SEric W. Biederman struct tun_struct *tun = __tun_get(tfile); 8813c8a9c63SMariusz Kozlowski struct sock *sk; 88233dccbb0SHerbert Xu unsigned int mask = 0; 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds if (!tun) 885eac9e902SEric W. Biederman return POLLERR; 8861da177e4SLinus Torvalds 88754f968d6SJason Wang sk = tfile->socket.sk; 8883c8a9c63SMariusz Kozlowski 8896b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_chr_poll\n"); 8901da177e4SLinus Torvalds 89154f968d6SJason Wang poll_wait(file, &tfile->wq.wait, wait); 8921da177e4SLinus Torvalds 89389f56d1eSMichael S. Tsirkin if (!skb_queue_empty(&sk->sk_receive_queue)) 8941da177e4SLinus Torvalds mask |= POLLIN | POLLRDNORM; 8951da177e4SLinus Torvalds 89633dccbb0SHerbert Xu if (sock_writeable(sk) || 89733dccbb0SHerbert Xu (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) && 89833dccbb0SHerbert Xu sock_writeable(sk))) 89933dccbb0SHerbert Xu mask |= POLLOUT | POLLWRNORM; 90033dccbb0SHerbert Xu 901c70f1829SEric W. Biederman if (tun->dev->reg_state != NETREG_REGISTERED) 902c70f1829SEric W. Biederman mask = POLLERR; 903c70f1829SEric W. Biederman 904631ab46bSEric W. Biederman tun_put(tun); 9051da177e4SLinus Torvalds return mask; 9061da177e4SLinus Torvalds } 9071da177e4SLinus Torvalds 908f42157cbSRusty Russell /* prepad is the amount to reserve at front. len is length after that. 909f42157cbSRusty Russell * linear is a hint as to how much to copy (usually headers). */ 91054f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, 91133dccbb0SHerbert Xu size_t prepad, size_t len, 91233dccbb0SHerbert Xu size_t linear, int noblock) 913f42157cbSRusty Russell { 91454f968d6SJason Wang struct sock *sk = tfile->socket.sk; 915f42157cbSRusty Russell struct sk_buff *skb; 91633dccbb0SHerbert Xu int err; 917f42157cbSRusty Russell 918f42157cbSRusty Russell /* Under a page? Don't bother with paged skb. */ 9190eca93bcSHerbert Xu if (prepad + len < PAGE_SIZE || !linear) 92033dccbb0SHerbert Xu linear = len; 921f42157cbSRusty Russell 92233dccbb0SHerbert Xu skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, 92333dccbb0SHerbert Xu &err); 924f42157cbSRusty Russell if (!skb) 92533dccbb0SHerbert Xu return ERR_PTR(err); 926f42157cbSRusty Russell 927f42157cbSRusty Russell skb_reserve(skb, prepad); 928f42157cbSRusty Russell skb_put(skb, linear); 92933dccbb0SHerbert Xu skb->data_len = len - linear; 93033dccbb0SHerbert Xu skb->len += len - linear; 931f42157cbSRusty Russell 932f42157cbSRusty Russell return skb; 933f42157cbSRusty Russell } 934f42157cbSRusty Russell 9350690899bSMichael S. Tsirkin /* set skb frags from iovec, this can move to core network code for reuse */ 9360690899bSMichael S. Tsirkin static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from, 9370690899bSMichael S. Tsirkin int offset, size_t count) 9380690899bSMichael S. Tsirkin { 9390690899bSMichael S. Tsirkin int len = iov_length(from, count) - offset; 9400690899bSMichael S. Tsirkin int copy = skb_headlen(skb); 9410690899bSMichael S. Tsirkin int size, offset1 = 0; 9420690899bSMichael S. Tsirkin int i = 0; 9430690899bSMichael S. Tsirkin 9440690899bSMichael S. Tsirkin /* Skip over from offset */ 9450690899bSMichael S. Tsirkin while (count && (offset >= from->iov_len)) { 9460690899bSMichael S. Tsirkin offset -= from->iov_len; 9470690899bSMichael S. Tsirkin ++from; 9480690899bSMichael S. Tsirkin --count; 9490690899bSMichael S. Tsirkin } 9500690899bSMichael S. Tsirkin 9510690899bSMichael S. Tsirkin /* copy up to skb headlen */ 9520690899bSMichael S. Tsirkin while (count && (copy > 0)) { 9530690899bSMichael S. Tsirkin size = min_t(unsigned int, copy, from->iov_len - offset); 9540690899bSMichael S. Tsirkin if (copy_from_user(skb->data + offset1, from->iov_base + offset, 9550690899bSMichael S. Tsirkin size)) 9560690899bSMichael S. Tsirkin return -EFAULT; 9570690899bSMichael S. Tsirkin if (copy > size) { 9580690899bSMichael S. Tsirkin ++from; 9590690899bSMichael S. Tsirkin --count; 9600690899bSMichael S. Tsirkin offset = 0; 9610690899bSMichael S. Tsirkin } else 9620690899bSMichael S. Tsirkin offset += size; 9630690899bSMichael S. Tsirkin copy -= size; 9640690899bSMichael S. Tsirkin offset1 += size; 9650690899bSMichael S. Tsirkin } 9660690899bSMichael S. Tsirkin 9670690899bSMichael S. Tsirkin if (len == offset1) 9680690899bSMichael S. Tsirkin return 0; 9690690899bSMichael S. Tsirkin 9700690899bSMichael S. Tsirkin while (count--) { 9710690899bSMichael S. Tsirkin struct page *page[MAX_SKB_FRAGS]; 9720690899bSMichael S. Tsirkin int num_pages; 9730690899bSMichael S. Tsirkin unsigned long base; 9740690899bSMichael S. Tsirkin unsigned long truesize; 9750690899bSMichael S. Tsirkin 9760690899bSMichael S. Tsirkin len = from->iov_len - offset; 9770690899bSMichael S. Tsirkin if (!len) { 9780690899bSMichael S. Tsirkin offset = 0; 9790690899bSMichael S. Tsirkin ++from; 9800690899bSMichael S. Tsirkin continue; 9810690899bSMichael S. Tsirkin } 9820690899bSMichael S. Tsirkin base = (unsigned long)from->iov_base + offset; 9830690899bSMichael S. Tsirkin size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT; 9840690899bSMichael S. Tsirkin if (i + size > MAX_SKB_FRAGS) 9850690899bSMichael S. Tsirkin return -EMSGSIZE; 9860690899bSMichael S. Tsirkin num_pages = get_user_pages_fast(base, size, 0, &page[i]); 9870690899bSMichael S. Tsirkin if (num_pages != size) { 9880690899bSMichael S. Tsirkin for (i = 0; i < num_pages; i++) 9890690899bSMichael S. Tsirkin put_page(page[i]); 9900690899bSMichael S. Tsirkin return -EFAULT; 9910690899bSMichael S. Tsirkin } 9920690899bSMichael S. Tsirkin truesize = size * PAGE_SIZE; 9930690899bSMichael S. Tsirkin skb->data_len += len; 9940690899bSMichael S. Tsirkin skb->len += len; 9950690899bSMichael S. Tsirkin skb->truesize += truesize; 9960690899bSMichael S. Tsirkin atomic_add(truesize, &skb->sk->sk_wmem_alloc); 9970690899bSMichael S. Tsirkin while (len) { 9980690899bSMichael S. Tsirkin int off = base & ~PAGE_MASK; 9990690899bSMichael S. Tsirkin int size = min_t(int, len, PAGE_SIZE - off); 10000690899bSMichael S. Tsirkin __skb_fill_page_desc(skb, i, page[i], off, size); 10010690899bSMichael S. Tsirkin skb_shinfo(skb)->nr_frags++; 10020690899bSMichael S. Tsirkin /* increase sk_wmem_alloc */ 10030690899bSMichael S. Tsirkin base += size; 10040690899bSMichael S. Tsirkin len -= size; 10050690899bSMichael S. Tsirkin i++; 10060690899bSMichael S. Tsirkin } 10070690899bSMichael S. Tsirkin offset = 0; 10080690899bSMichael S. Tsirkin ++from; 10090690899bSMichael S. Tsirkin } 10100690899bSMichael S. Tsirkin return 0; 10110690899bSMichael S. Tsirkin } 10120690899bSMichael S. Tsirkin 10131da177e4SLinus Torvalds /* Get packet from user space buffer */ 101454f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, 101554f968d6SJason Wang void *msg_control, const struct iovec *iv, 101654f968d6SJason Wang size_t total_len, size_t count, int noblock) 10171da177e4SLinus Torvalds { 101809640e63SHarvey Harrison struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; 10191da177e4SLinus Torvalds struct sk_buff *skb; 10200690899bSMichael S. Tsirkin size_t len = total_len, align = NET_SKB_PAD; 1021f43798c2SRusty Russell struct virtio_net_hdr gso = { 0 }; 10226f26c9a7SMichael S. Tsirkin int offset = 0; 10230690899bSMichael S. Tsirkin int copylen; 10240690899bSMichael S. Tsirkin bool zerocopy = false; 10250690899bSMichael S. Tsirkin int err; 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds if (!(tun->flags & TUN_NO_PI)) { 10280690899bSMichael S. Tsirkin if ((len -= sizeof(pi)) > total_len) 10291da177e4SLinus Torvalds return -EINVAL; 10301da177e4SLinus Torvalds 10316f26c9a7SMichael S. Tsirkin if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi))) 10321da177e4SLinus Torvalds return -EFAULT; 10336f26c9a7SMichael S. Tsirkin offset += sizeof(pi); 10341da177e4SLinus Torvalds } 10351da177e4SLinus Torvalds 1036f43798c2SRusty Russell if (tun->flags & TUN_VNET_HDR) { 10370690899bSMichael S. Tsirkin if ((len -= tun->vnet_hdr_sz) > total_len) 1038f43798c2SRusty Russell return -EINVAL; 1039f43798c2SRusty Russell 10406f26c9a7SMichael S. Tsirkin if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso))) 1041f43798c2SRusty Russell return -EFAULT; 1042f43798c2SRusty Russell 10434909122fSHerbert Xu if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && 10444909122fSHerbert Xu gso.csum_start + gso.csum_offset + 2 > gso.hdr_len) 10454909122fSHerbert Xu gso.hdr_len = gso.csum_start + gso.csum_offset + 2; 10464909122fSHerbert Xu 1047f43798c2SRusty Russell if (gso.hdr_len > len) 1048f43798c2SRusty Russell return -EINVAL; 1049d9d52b51SMichael S. Tsirkin offset += tun->vnet_hdr_sz; 1050f43798c2SRusty Russell } 1051f43798c2SRusty Russell 1052e01bf1c8SRusty Russell if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) { 1053a504b86eSstephen hemminger align += NET_IP_ALIGN; 10540eca93bcSHerbert Xu if (unlikely(len < ETH_HLEN || 10550eca93bcSHerbert Xu (gso.hdr_len && gso.hdr_len < ETH_HLEN))) 1056e01bf1c8SRusty Russell return -EINVAL; 1057e01bf1c8SRusty Russell } 10581da177e4SLinus Torvalds 10590690899bSMichael S. Tsirkin if (msg_control) 10600690899bSMichael S. Tsirkin zerocopy = true; 10610690899bSMichael S. Tsirkin 10620690899bSMichael S. Tsirkin if (zerocopy) { 10630690899bSMichael S. Tsirkin /* Userspace may produce vectors with count greater than 10640690899bSMichael S. Tsirkin * MAX_SKB_FRAGS, so we need to linearize parts of the skb 10650690899bSMichael S. Tsirkin * to let the rest of data to be fit in the frags. 10660690899bSMichael S. Tsirkin */ 10670690899bSMichael S. Tsirkin if (count > MAX_SKB_FRAGS) { 10680690899bSMichael S. Tsirkin copylen = iov_length(iv, count - MAX_SKB_FRAGS); 10690690899bSMichael S. Tsirkin if (copylen < offset) 10700690899bSMichael S. Tsirkin copylen = 0; 10710690899bSMichael S. Tsirkin else 10720690899bSMichael S. Tsirkin copylen -= offset; 10730690899bSMichael S. Tsirkin } else 10740690899bSMichael S. Tsirkin copylen = 0; 10750690899bSMichael S. Tsirkin /* There are 256 bytes to be copied in skb, so there is enough 10760690899bSMichael S. Tsirkin * room for skb expand head in case it is used. 10770690899bSMichael S. Tsirkin * The rest of the buffer is mapped from userspace. 10780690899bSMichael S. Tsirkin */ 10790690899bSMichael S. Tsirkin if (copylen < gso.hdr_len) 10800690899bSMichael S. Tsirkin copylen = gso.hdr_len; 10810690899bSMichael S. Tsirkin if (!copylen) 10820690899bSMichael S. Tsirkin copylen = GOODCOPY_LEN; 10830690899bSMichael S. Tsirkin } else 10840690899bSMichael S. Tsirkin copylen = len; 10850690899bSMichael S. Tsirkin 108654f968d6SJason Wang skb = tun_alloc_skb(tfile, align, copylen, gso.hdr_len, noblock); 108733dccbb0SHerbert Xu if (IS_ERR(skb)) { 108833dccbb0SHerbert Xu if (PTR_ERR(skb) != -EAGAIN) 108909f75cd7SJeff Garzik tun->dev->stats.rx_dropped++; 109033dccbb0SHerbert Xu return PTR_ERR(skb); 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds 10930690899bSMichael S. Tsirkin if (zerocopy) 10940690899bSMichael S. Tsirkin err = zerocopy_sg_from_iovec(skb, iv, offset, count); 10950690899bSMichael S. Tsirkin else 10960690899bSMichael S. Tsirkin err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len); 10970690899bSMichael S. Tsirkin 10980690899bSMichael S. Tsirkin if (err) { 109909f75cd7SJeff Garzik tun->dev->stats.rx_dropped++; 11008f22757eSDave Jones kfree_skb(skb); 11011da177e4SLinus Torvalds return -EFAULT; 11028f22757eSDave Jones } 11031da177e4SLinus Torvalds 1104f43798c2SRusty Russell if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { 1105f43798c2SRusty Russell if (!skb_partial_csum_set(skb, gso.csum_start, 1106f43798c2SRusty Russell gso.csum_offset)) { 1107f43798c2SRusty Russell tun->dev->stats.rx_frame_errors++; 1108f43798c2SRusty Russell kfree_skb(skb); 1109f43798c2SRusty Russell return -EINVAL; 1110f43798c2SRusty Russell } 111188255375SMichał Mirosław } 1112f43798c2SRusty Russell 11131da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 11141da177e4SLinus Torvalds case TUN_TUN_DEV: 1115f09f7ee2SAng Way Chuang if (tun->flags & TUN_NO_PI) { 1116f09f7ee2SAng Way Chuang switch (skb->data[0] & 0xf0) { 1117f09f7ee2SAng Way Chuang case 0x40: 1118f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IP); 1119f09f7ee2SAng Way Chuang break; 1120f09f7ee2SAng Way Chuang case 0x60: 1121f09f7ee2SAng Way Chuang pi.proto = htons(ETH_P_IPV6); 1122f09f7ee2SAng Way Chuang break; 1123f09f7ee2SAng Way Chuang default: 1124f09f7ee2SAng Way Chuang tun->dev->stats.rx_dropped++; 1125f09f7ee2SAng Way Chuang kfree_skb(skb); 1126f09f7ee2SAng Way Chuang return -EINVAL; 1127f09f7ee2SAng Way Chuang } 1128f09f7ee2SAng Way Chuang } 1129f09f7ee2SAng Way Chuang 1130459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 11311da177e4SLinus Torvalds skb->protocol = pi.proto; 11324c13eb66SArnaldo Carvalho de Melo skb->dev = tun->dev; 11331da177e4SLinus Torvalds break; 11341da177e4SLinus Torvalds case TUN_TAP_DEV: 11351da177e4SLinus Torvalds skb->protocol = eth_type_trans(skb, tun->dev); 11361da177e4SLinus Torvalds break; 11376403eab1SJoe Perches } 11381da177e4SLinus Torvalds 1139f43798c2SRusty Russell if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) { 1140f43798c2SRusty Russell pr_debug("GSO!\n"); 1141f43798c2SRusty Russell switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { 1142f43798c2SRusty Russell case VIRTIO_NET_HDR_GSO_TCPV4: 1143f43798c2SRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; 1144f43798c2SRusty Russell break; 1145f43798c2SRusty Russell case VIRTIO_NET_HDR_GSO_TCPV6: 1146f43798c2SRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; 1147f43798c2SRusty Russell break; 1148e36aa25aSSridhar Samudrala case VIRTIO_NET_HDR_GSO_UDP: 1149e36aa25aSSridhar Samudrala skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 1150e36aa25aSSridhar Samudrala break; 1151f43798c2SRusty Russell default: 1152f43798c2SRusty Russell tun->dev->stats.rx_frame_errors++; 1153f43798c2SRusty Russell kfree_skb(skb); 1154f43798c2SRusty Russell return -EINVAL; 1155f43798c2SRusty Russell } 1156f43798c2SRusty Russell 1157f43798c2SRusty Russell if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN) 1158f43798c2SRusty Russell skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; 1159f43798c2SRusty Russell 1160f43798c2SRusty Russell skb_shinfo(skb)->gso_size = gso.gso_size; 1161f43798c2SRusty Russell if (skb_shinfo(skb)->gso_size == 0) { 1162f43798c2SRusty Russell tun->dev->stats.rx_frame_errors++; 1163f43798c2SRusty Russell kfree_skb(skb); 1164f43798c2SRusty Russell return -EINVAL; 1165f43798c2SRusty Russell } 1166f43798c2SRusty Russell 1167f43798c2SRusty Russell /* Header must be checked, and gso_segs computed. */ 1168f43798c2SRusty Russell skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 1169f43798c2SRusty Russell skb_shinfo(skb)->gso_segs = 0; 1170f43798c2SRusty Russell } 11711da177e4SLinus Torvalds 11720690899bSMichael S. Tsirkin /* copy skb_ubuf_info for callback when skb has no error */ 11730690899bSMichael S. Tsirkin if (zerocopy) { 11740690899bSMichael S. Tsirkin skb_shinfo(skb)->destructor_arg = msg_control; 11750690899bSMichael S. Tsirkin skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; 11760690899bSMichael S. Tsirkin } 11770690899bSMichael S. Tsirkin 11781da177e4SLinus Torvalds netif_rx_ni(skb); 11791da177e4SLinus Torvalds 118009f75cd7SJeff Garzik tun->dev->stats.rx_packets++; 118109f75cd7SJeff Garzik tun->dev->stats.rx_bytes += len; 11821da177e4SLinus Torvalds 1183*96442e42SJason Wang tun_flow_update(tun, skb, tfile->queue_index); 11840690899bSMichael S. Tsirkin return total_len; 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds 1187ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv, 1188ee0b3e67SBadari Pulavarty unsigned long count, loff_t pos) 11891da177e4SLinus Torvalds { 119033dccbb0SHerbert Xu struct file *file = iocb->ki_filp; 1191ab46d779SHerbert Xu struct tun_struct *tun = tun_get(file); 119254f968d6SJason Wang struct tun_file *tfile = file->private_data; 1193631ab46bSEric W. Biederman ssize_t result; 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds if (!tun) 11961da177e4SLinus Torvalds return -EBADFD; 11971da177e4SLinus Torvalds 11986b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count); 11991da177e4SLinus Torvalds 120054f968d6SJason Wang result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count), 120154f968d6SJason Wang count, file->f_flags & O_NONBLOCK); 1202631ab46bSEric W. Biederman 1203631ab46bSEric W. Biederman tun_put(tun); 1204631ab46bSEric W. Biederman return result; 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds /* Put packet to the user space buffer */ 12086f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun, 120954f968d6SJason Wang struct tun_file *tfile, 12101da177e4SLinus Torvalds struct sk_buff *skb, 121143b39dcdSMichael S. Tsirkin const struct iovec *iv, int len) 12121da177e4SLinus Torvalds { 12131da177e4SLinus Torvalds struct tun_pi pi = { 0, skb->protocol }; 12141da177e4SLinus Torvalds ssize_t total = 0; 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds if (!(tun->flags & TUN_NO_PI)) { 12171da177e4SLinus Torvalds if ((len -= sizeof(pi)) < 0) 12181da177e4SLinus Torvalds return -EINVAL; 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds if (len < skb->len) { 12211da177e4SLinus Torvalds /* Packet will be striped */ 12221da177e4SLinus Torvalds pi.flags |= TUN_PKT_STRIP; 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds 122543b39dcdSMichael S. Tsirkin if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi))) 12261da177e4SLinus Torvalds return -EFAULT; 12271da177e4SLinus Torvalds total += sizeof(pi); 12281da177e4SLinus Torvalds } 12291da177e4SLinus Torvalds 1230f43798c2SRusty Russell if (tun->flags & TUN_VNET_HDR) { 1231f43798c2SRusty Russell struct virtio_net_hdr gso = { 0 }; /* no info leak */ 1232d9d52b51SMichael S. Tsirkin if ((len -= tun->vnet_hdr_sz) < 0) 1233f43798c2SRusty Russell return -EINVAL; 1234f43798c2SRusty Russell 1235f43798c2SRusty Russell if (skb_is_gso(skb)) { 1236f43798c2SRusty Russell struct skb_shared_info *sinfo = skb_shinfo(skb); 1237f43798c2SRusty Russell 1238f43798c2SRusty Russell /* This is a hint as to how much should be linear. */ 1239f43798c2SRusty Russell gso.hdr_len = skb_headlen(skb); 1240f43798c2SRusty Russell gso.gso_size = sinfo->gso_size; 1241f43798c2SRusty Russell if (sinfo->gso_type & SKB_GSO_TCPV4) 1242f43798c2SRusty Russell gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 1243f43798c2SRusty Russell else if (sinfo->gso_type & SKB_GSO_TCPV6) 1244f43798c2SRusty Russell gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 1245e36aa25aSSridhar Samudrala else if (sinfo->gso_type & SKB_GSO_UDP) 1246e36aa25aSSridhar Samudrala gso.gso_type = VIRTIO_NET_HDR_GSO_UDP; 1247ef3db4a5SMichael S. Tsirkin else { 12486b8a66eeSJoe Perches pr_err("unexpected GSO type: " 1249ef3db4a5SMichael S. Tsirkin "0x%x, gso_size %d, hdr_len %d\n", 1250ef3db4a5SMichael S. Tsirkin sinfo->gso_type, gso.gso_size, 1251ef3db4a5SMichael S. Tsirkin gso.hdr_len); 1252ef3db4a5SMichael S. Tsirkin print_hex_dump(KERN_ERR, "tun: ", 1253ef3db4a5SMichael S. Tsirkin DUMP_PREFIX_NONE, 1254ef3db4a5SMichael S. Tsirkin 16, 1, skb->head, 1255ef3db4a5SMichael S. Tsirkin min((int)gso.hdr_len, 64), true); 1256ef3db4a5SMichael S. Tsirkin WARN_ON_ONCE(1); 1257ef3db4a5SMichael S. Tsirkin return -EINVAL; 1258ef3db4a5SMichael S. Tsirkin } 1259f43798c2SRusty Russell if (sinfo->gso_type & SKB_GSO_TCP_ECN) 1260f43798c2SRusty Russell gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN; 1261f43798c2SRusty Russell } else 1262f43798c2SRusty Russell gso.gso_type = VIRTIO_NET_HDR_GSO_NONE; 1263f43798c2SRusty Russell 1264f43798c2SRusty Russell if (skb->ip_summed == CHECKSUM_PARTIAL) { 1265f43798c2SRusty Russell gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; 126655508d60SMichał Mirosław gso.csum_start = skb_checksum_start_offset(skb); 1267f43798c2SRusty Russell gso.csum_offset = skb->csum_offset; 126810a8d94aSJason Wang } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) { 126910a8d94aSJason Wang gso.flags = VIRTIO_NET_HDR_F_DATA_VALID; 1270f43798c2SRusty Russell } /* else everything is zero */ 1271f43798c2SRusty Russell 127243b39dcdSMichael S. Tsirkin if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total, 127343b39dcdSMichael S. Tsirkin sizeof(gso)))) 1274f43798c2SRusty Russell return -EFAULT; 1275d9d52b51SMichael S. Tsirkin total += tun->vnet_hdr_sz; 1276f43798c2SRusty Russell } 1277f43798c2SRusty Russell 12781da177e4SLinus Torvalds len = min_t(int, skb->len, len); 12791da177e4SLinus Torvalds 128043b39dcdSMichael S. Tsirkin skb_copy_datagram_const_iovec(skb, 0, iv, total, len); 128105c2828cSMichael S. Tsirkin total += skb->len; 12821da177e4SLinus Torvalds 128309f75cd7SJeff Garzik tun->dev->stats.tx_packets++; 128409f75cd7SJeff Garzik tun->dev->stats.tx_bytes += len; 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds return total; 12871da177e4SLinus Torvalds } 12881da177e4SLinus Torvalds 128954f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, 129005c2828cSMichael S. Tsirkin struct kiocb *iocb, const struct iovec *iv, 129105c2828cSMichael S. Tsirkin ssize_t len, int noblock) 12921da177e4SLinus Torvalds { 12931da177e4SLinus Torvalds DECLARE_WAITQUEUE(wait, current); 12941da177e4SLinus Torvalds struct sk_buff *skb; 129505c2828cSMichael S. Tsirkin ssize_t ret = 0; 12961da177e4SLinus Torvalds 12976b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_chr_read\n"); 12981da177e4SLinus Torvalds 129961a5ff15SAmos Kong if (unlikely(!noblock)) 130054f968d6SJason Wang add_wait_queue(&tfile->wq.wait, &wait); 13011da177e4SLinus Torvalds while (len) { 13021da177e4SLinus Torvalds current->state = TASK_INTERRUPTIBLE; 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds /* Read frames from the queue */ 130554f968d6SJason Wang if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) { 130605c2828cSMichael S. Tsirkin if (noblock) { 13071da177e4SLinus Torvalds ret = -EAGAIN; 13081da177e4SLinus Torvalds break; 13091da177e4SLinus Torvalds } 13101da177e4SLinus Torvalds if (signal_pending(current)) { 13111da177e4SLinus Torvalds ret = -ERESTARTSYS; 13121da177e4SLinus Torvalds break; 13131da177e4SLinus Torvalds } 1314c70f1829SEric W. Biederman if (tun->dev->reg_state != NETREG_REGISTERED) { 1315c70f1829SEric W. Biederman ret = -EIO; 1316c70f1829SEric W. Biederman break; 1317c70f1829SEric W. Biederman } 13181da177e4SLinus Torvalds 13191da177e4SLinus Torvalds /* Nothing to read, let's sleep */ 13201da177e4SLinus Torvalds schedule(); 13211da177e4SLinus Torvalds continue; 13221da177e4SLinus Torvalds } 1323c8d68e6bSJason Wang netif_wake_subqueue(tun->dev, tfile->queue_index); 13241da177e4SLinus Torvalds 132554f968d6SJason Wang ret = tun_put_user(tun, tfile, skb, iv, len); 13261da177e4SLinus Torvalds kfree_skb(skb); 13271da177e4SLinus Torvalds break; 13281da177e4SLinus Torvalds } 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds current->state = TASK_RUNNING; 133161a5ff15SAmos Kong if (unlikely(!noblock)) 133254f968d6SJason Wang remove_wait_queue(&tfile->wq.wait, &wait); 13331da177e4SLinus Torvalds 133405c2828cSMichael S. Tsirkin return ret; 133505c2828cSMichael S. Tsirkin } 133605c2828cSMichael S. Tsirkin 133705c2828cSMichael S. Tsirkin static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv, 133805c2828cSMichael S. Tsirkin unsigned long count, loff_t pos) 133905c2828cSMichael S. Tsirkin { 134005c2828cSMichael S. Tsirkin struct file *file = iocb->ki_filp; 134105c2828cSMichael S. Tsirkin struct tun_file *tfile = file->private_data; 134205c2828cSMichael S. Tsirkin struct tun_struct *tun = __tun_get(tfile); 134305c2828cSMichael S. Tsirkin ssize_t len, ret; 134405c2828cSMichael S. Tsirkin 134505c2828cSMichael S. Tsirkin if (!tun) 134605c2828cSMichael S. Tsirkin return -EBADFD; 134705c2828cSMichael S. Tsirkin len = iov_length(iv, count); 134805c2828cSMichael S. Tsirkin if (len < 0) { 134905c2828cSMichael S. Tsirkin ret = -EINVAL; 135005c2828cSMichael S. Tsirkin goto out; 135105c2828cSMichael S. Tsirkin } 135205c2828cSMichael S. Tsirkin 135354f968d6SJason Wang ret = tun_do_read(tun, tfile, iocb, iv, len, 135454f968d6SJason Wang file->f_flags & O_NONBLOCK); 135505c2828cSMichael S. Tsirkin ret = min_t(ssize_t, ret, len); 1356631ab46bSEric W. Biederman out: 1357631ab46bSEric W. Biederman tun_put(tun); 13581da177e4SLinus Torvalds return ret; 13591da177e4SLinus Torvalds } 13601da177e4SLinus Torvalds 1361*96442e42SJason Wang static void tun_free_netdev(struct net_device *dev) 1362*96442e42SJason Wang { 1363*96442e42SJason Wang struct tun_struct *tun = netdev_priv(dev); 1364*96442e42SJason Wang 1365*96442e42SJason Wang tun_flow_uninit(tun); 1366*96442e42SJason Wang free_netdev(dev); 1367*96442e42SJason Wang } 1368*96442e42SJason Wang 13691da177e4SLinus Torvalds static void tun_setup(struct net_device *dev) 13701da177e4SLinus Torvalds { 13711da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 13721da177e4SLinus Torvalds 13730625c883SEric W. Biederman tun->owner = INVALID_UID; 13740625c883SEric W. Biederman tun->group = INVALID_GID; 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds dev->ethtool_ops = &tun_ethtool_ops; 1377*96442e42SJason Wang dev->destructor = tun_free_netdev; 13781da177e4SLinus Torvalds } 13791da177e4SLinus Torvalds 1380f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap 1381f019a7a5SEric W. Biederman * device with netlink. 1382f019a7a5SEric W. Biederman */ 1383f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[]) 1384f019a7a5SEric W. Biederman { 1385f019a7a5SEric W. Biederman return -EINVAL; 1386f019a7a5SEric W. Biederman } 1387f019a7a5SEric W. Biederman 1388f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = { 1389f019a7a5SEric W. Biederman .kind = DRV_NAME, 1390f019a7a5SEric W. Biederman .priv_size = sizeof(struct tun_struct), 1391f019a7a5SEric W. Biederman .setup = tun_setup, 1392f019a7a5SEric W. Biederman .validate = tun_validate, 1393f019a7a5SEric W. Biederman }; 1394f019a7a5SEric W. Biederman 139533dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk) 139633dccbb0SHerbert Xu { 139754f968d6SJason Wang struct tun_file *tfile; 139843815482SEric Dumazet wait_queue_head_t *wqueue; 139933dccbb0SHerbert Xu 140033dccbb0SHerbert Xu if (!sock_writeable(sk)) 140133dccbb0SHerbert Xu return; 140233dccbb0SHerbert Xu 140333dccbb0SHerbert Xu if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags)) 140433dccbb0SHerbert Xu return; 140533dccbb0SHerbert Xu 140643815482SEric Dumazet wqueue = sk_sleep(sk); 140743815482SEric Dumazet if (wqueue && waitqueue_active(wqueue)) 140843815482SEric Dumazet wake_up_interruptible_sync_poll(wqueue, POLLOUT | 140905c2828cSMichael S. Tsirkin POLLWRNORM | POLLWRBAND); 1410c722c625SHerbert Xu 141154f968d6SJason Wang tfile = container_of(sk, struct tun_file, sk); 141254f968d6SJason Wang kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); 141333dccbb0SHerbert Xu } 141433dccbb0SHerbert Xu 141505c2828cSMichael S. Tsirkin static int tun_sendmsg(struct kiocb *iocb, struct socket *sock, 141605c2828cSMichael S. Tsirkin struct msghdr *m, size_t total_len) 141705c2828cSMichael S. Tsirkin { 141854f968d6SJason Wang int ret; 141954f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 142054f968d6SJason Wang struct tun_struct *tun = __tun_get(tfile); 142154f968d6SJason Wang 142254f968d6SJason Wang if (!tun) 142354f968d6SJason Wang return -EBADFD; 142454f968d6SJason Wang ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len, 14250690899bSMichael S. Tsirkin m->msg_iovlen, m->msg_flags & MSG_DONTWAIT); 142654f968d6SJason Wang tun_put(tun); 142754f968d6SJason Wang return ret; 142805c2828cSMichael S. Tsirkin } 142905c2828cSMichael S. Tsirkin 143054f968d6SJason Wang 143105c2828cSMichael S. Tsirkin static int tun_recvmsg(struct kiocb *iocb, struct socket *sock, 143205c2828cSMichael S. Tsirkin struct msghdr *m, size_t total_len, 143305c2828cSMichael S. Tsirkin int flags) 143405c2828cSMichael S. Tsirkin { 143554f968d6SJason Wang struct tun_file *tfile = container_of(sock, struct tun_file, socket); 143654f968d6SJason Wang struct tun_struct *tun = __tun_get(tfile); 143705c2828cSMichael S. Tsirkin int ret; 143854f968d6SJason Wang 143954f968d6SJason Wang if (!tun) 144054f968d6SJason Wang return -EBADFD; 144154f968d6SJason Wang 144205c2828cSMichael S. Tsirkin if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) 144305c2828cSMichael S. Tsirkin return -EINVAL; 144454f968d6SJason Wang ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len, 144505c2828cSMichael S. Tsirkin flags & MSG_DONTWAIT); 144605c2828cSMichael S. Tsirkin if (ret > total_len) { 144705c2828cSMichael S. Tsirkin m->msg_flags |= MSG_TRUNC; 144805c2828cSMichael S. Tsirkin ret = flags & MSG_TRUNC ? ret : total_len; 144905c2828cSMichael S. Tsirkin } 145054f968d6SJason Wang tun_put(tun); 145105c2828cSMichael S. Tsirkin return ret; 145205c2828cSMichael S. Tsirkin } 145305c2828cSMichael S. Tsirkin 14541ab5ecb9SStanislav Kinsbursky static int tun_release(struct socket *sock) 14551ab5ecb9SStanislav Kinsbursky { 14561ab5ecb9SStanislav Kinsbursky if (sock->sk) 14571ab5ecb9SStanislav Kinsbursky sock_put(sock->sk); 14581ab5ecb9SStanislav Kinsbursky return 0; 14591ab5ecb9SStanislav Kinsbursky } 14601ab5ecb9SStanislav Kinsbursky 146105c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */ 146205c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = { 146305c2828cSMichael S. Tsirkin .sendmsg = tun_sendmsg, 146405c2828cSMichael S. Tsirkin .recvmsg = tun_recvmsg, 14651ab5ecb9SStanislav Kinsbursky .release = tun_release, 146605c2828cSMichael S. Tsirkin }; 146705c2828cSMichael S. Tsirkin 146833dccbb0SHerbert Xu static struct proto tun_proto = { 146933dccbb0SHerbert Xu .name = "tun", 147033dccbb0SHerbert Xu .owner = THIS_MODULE, 147154f968d6SJason Wang .obj_size = sizeof(struct tun_file), 147233dccbb0SHerbert Xu }; 1473f019a7a5SEric W. Biederman 1474980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun) 1475980c9e8cSDavid Woodhouse { 1476980c9e8cSDavid Woodhouse int flags = 0; 1477980c9e8cSDavid Woodhouse 1478980c9e8cSDavid Woodhouse if (tun->flags & TUN_TUN_DEV) 1479980c9e8cSDavid Woodhouse flags |= IFF_TUN; 1480980c9e8cSDavid Woodhouse else 1481980c9e8cSDavid Woodhouse flags |= IFF_TAP; 1482980c9e8cSDavid Woodhouse 1483980c9e8cSDavid Woodhouse if (tun->flags & TUN_NO_PI) 1484980c9e8cSDavid Woodhouse flags |= IFF_NO_PI; 1485980c9e8cSDavid Woodhouse 1486980c9e8cSDavid Woodhouse if (tun->flags & TUN_ONE_QUEUE) 1487980c9e8cSDavid Woodhouse flags |= IFF_ONE_QUEUE; 1488980c9e8cSDavid Woodhouse 1489980c9e8cSDavid Woodhouse if (tun->flags & TUN_VNET_HDR) 1490980c9e8cSDavid Woodhouse flags |= IFF_VNET_HDR; 1491980c9e8cSDavid Woodhouse 1492c8d68e6bSJason Wang if (tun->flags & TUN_TAP_MQ) 1493c8d68e6bSJason Wang flags |= IFF_MULTI_QUEUE; 1494c8d68e6bSJason Wang 1495980c9e8cSDavid Woodhouse return flags; 1496980c9e8cSDavid Woodhouse } 1497980c9e8cSDavid Woodhouse 1498980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr, 1499980c9e8cSDavid Woodhouse char *buf) 1500980c9e8cSDavid Woodhouse { 1501980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 1502980c9e8cSDavid Woodhouse return sprintf(buf, "0x%x\n", tun_flags(tun)); 1503980c9e8cSDavid Woodhouse } 1504980c9e8cSDavid Woodhouse 1505980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr, 1506980c9e8cSDavid Woodhouse char *buf) 1507980c9e8cSDavid Woodhouse { 1508980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 15090625c883SEric W. Biederman return uid_valid(tun->owner)? 15100625c883SEric W. Biederman sprintf(buf, "%u\n", 15110625c883SEric W. Biederman from_kuid_munged(current_user_ns(), tun->owner)): 15120625c883SEric W. Biederman sprintf(buf, "-1\n"); 1513980c9e8cSDavid Woodhouse } 1514980c9e8cSDavid Woodhouse 1515980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr, 1516980c9e8cSDavid Woodhouse char *buf) 1517980c9e8cSDavid Woodhouse { 1518980c9e8cSDavid Woodhouse struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 15190625c883SEric W. Biederman return gid_valid(tun->group) ? 15200625c883SEric W. Biederman sprintf(buf, "%u\n", 15210625c883SEric W. Biederman from_kgid_munged(current_user_ns(), tun->group)): 15220625c883SEric W. Biederman sprintf(buf, "-1\n"); 1523980c9e8cSDavid Woodhouse } 1524980c9e8cSDavid Woodhouse 1525980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL); 1526980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL); 1527980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL); 1528980c9e8cSDavid Woodhouse 1529d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) 15301da177e4SLinus Torvalds { 15311da177e4SLinus Torvalds struct tun_struct *tun; 153254f968d6SJason Wang struct tun_file *tfile = file->private_data; 15331da177e4SLinus Torvalds struct net_device *dev; 15341da177e4SLinus Torvalds int err; 15351da177e4SLinus Torvalds 153674a3e5a7SEric W. Biederman dev = __dev_get_by_name(net, ifr->ifr_name); 153774a3e5a7SEric W. Biederman if (dev) { 1538f85ba780SDavid Woodhouse if (ifr->ifr_flags & IFF_TUN_EXCL) 1539f85ba780SDavid Woodhouse return -EBUSY; 154074a3e5a7SEric W. Biederman if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) 154174a3e5a7SEric W. Biederman tun = netdev_priv(dev); 154274a3e5a7SEric W. Biederman else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) 154374a3e5a7SEric W. Biederman tun = netdev_priv(dev); 154474a3e5a7SEric W. Biederman else 154574a3e5a7SEric W. Biederman return -EINVAL; 154674a3e5a7SEric W. Biederman 1547cde8b15fSJason Wang if (tun_not_capable(tun)) 15482b980dbdSPaul Moore return -EPERM; 154954f968d6SJason Wang err = security_tun_dev_attach(tfile->socket.sk); 15502b980dbdSPaul Moore if (err < 0) 15512b980dbdSPaul Moore return err; 15522b980dbdSPaul Moore 1553a7385ba2SEric W. Biederman err = tun_attach(tun, file); 1554a7385ba2SEric W. Biederman if (err < 0) 1555a7385ba2SEric W. Biederman return err; 155686a264abSDavid Howells } 15571da177e4SLinus Torvalds else { 15581da177e4SLinus Torvalds char *name; 15591da177e4SLinus Torvalds unsigned long flags = 0; 15601da177e4SLinus Torvalds 1561ca6bb5d7SDavid Woodhouse if (!capable(CAP_NET_ADMIN)) 1562ca6bb5d7SDavid Woodhouse return -EPERM; 15632b980dbdSPaul Moore err = security_tun_dev_create(); 15642b980dbdSPaul Moore if (err < 0) 15652b980dbdSPaul Moore return err; 1566ca6bb5d7SDavid Woodhouse 15671da177e4SLinus Torvalds /* Set dev type */ 15681da177e4SLinus Torvalds if (ifr->ifr_flags & IFF_TUN) { 15691da177e4SLinus Torvalds /* TUN device */ 15701da177e4SLinus Torvalds flags |= TUN_TUN_DEV; 15711da177e4SLinus Torvalds name = "tun%d"; 15721da177e4SLinus Torvalds } else if (ifr->ifr_flags & IFF_TAP) { 15731da177e4SLinus Torvalds /* TAP device */ 15741da177e4SLinus Torvalds flags |= TUN_TAP_DEV; 15751da177e4SLinus Torvalds name = "tap%d"; 15761da177e4SLinus Torvalds } else 157736989b90SKusanagi Kouichi return -EINVAL; 15781da177e4SLinus Torvalds 15791da177e4SLinus Torvalds if (*ifr->ifr_name) 15801da177e4SLinus Torvalds name = ifr->ifr_name; 15811da177e4SLinus Torvalds 1582c8d68e6bSJason Wang dev = alloc_netdev_mqs(sizeof(struct tun_struct), name, 1583c8d68e6bSJason Wang tun_setup, 1584c8d68e6bSJason Wang MAX_TAP_QUEUES, MAX_TAP_QUEUES); 15851da177e4SLinus Torvalds if (!dev) 15861da177e4SLinus Torvalds return -ENOMEM; 15871da177e4SLinus Torvalds 1588fc54c658SPavel Emelyanov dev_net_set(dev, net); 1589f019a7a5SEric W. Biederman dev->rtnl_link_ops = &tun_link_ops; 1590758e43b7SStephen Hemminger 15911da177e4SLinus Torvalds tun = netdev_priv(dev); 15921da177e4SLinus Torvalds tun->dev = dev; 15931da177e4SLinus Torvalds tun->flags = flags; 1594f271b2ccSMax Krasnyansky tun->txflt.count = 0; 1595d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr); 15961da177e4SLinus Torvalds 159754f968d6SJason Wang tun->filter_attached = false; 159854f968d6SJason Wang tun->sndbuf = tfile->socket.sk->sk_sndbuf; 159933dccbb0SHerbert Xu 1600*96442e42SJason Wang spin_lock_init(&tun->lock); 1601*96442e42SJason Wang 160254f968d6SJason Wang security_tun_dev_post_create(&tfile->sk); 16032b980dbdSPaul Moore 16041da177e4SLinus Torvalds tun_net_init(dev); 16051da177e4SLinus Torvalds 1606*96442e42SJason Wang if (tun_flow_init(tun)) 1607*96442e42SJason Wang goto err_free_dev; 1608*96442e42SJason Wang 160988255375SMichał Mirosław dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | 161088255375SMichał Mirosław TUN_USER_FEATURES; 161188255375SMichał Mirosław dev->features = dev->hw_features; 161288255375SMichał Mirosław 16131da177e4SLinus Torvalds err = register_netdevice(tun->dev); 16141da177e4SLinus Torvalds if (err < 0) 161554f968d6SJason Wang goto err_free_dev; 16169c3fea6aSHerbert Xu 1617980c9e8cSDavid Woodhouse if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) || 1618980c9e8cSDavid Woodhouse device_create_file(&tun->dev->dev, &dev_attr_owner) || 1619980c9e8cSDavid Woodhouse device_create_file(&tun->dev->dev, &dev_attr_group)) 16206b8a66eeSJoe Perches pr_err("Failed to create tun sysfs files\n"); 1621980c9e8cSDavid Woodhouse 1622a7385ba2SEric W. Biederman err = tun_attach(tun, file); 1623a7385ba2SEric W. Biederman if (err < 0) 1624c8d68e6bSJason Wang goto err_free_dev; 16251da177e4SLinus Torvalds } 16261da177e4SLinus Torvalds 16276b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_set_iff\n"); 16281da177e4SLinus Torvalds 16291da177e4SLinus Torvalds if (ifr->ifr_flags & IFF_NO_PI) 16301da177e4SLinus Torvalds tun->flags |= TUN_NO_PI; 1631a26af1e0SNathaniel Filardo else 1632a26af1e0SNathaniel Filardo tun->flags &= ~TUN_NO_PI; 16331da177e4SLinus Torvalds 16341da177e4SLinus Torvalds if (ifr->ifr_flags & IFF_ONE_QUEUE) 16351da177e4SLinus Torvalds tun->flags |= TUN_ONE_QUEUE; 1636a26af1e0SNathaniel Filardo else 1637a26af1e0SNathaniel Filardo tun->flags &= ~TUN_ONE_QUEUE; 16381da177e4SLinus Torvalds 1639f43798c2SRusty Russell if (ifr->ifr_flags & IFF_VNET_HDR) 1640f43798c2SRusty Russell tun->flags |= TUN_VNET_HDR; 1641f43798c2SRusty Russell else 1642f43798c2SRusty Russell tun->flags &= ~TUN_VNET_HDR; 1643f43798c2SRusty Russell 1644c8d68e6bSJason Wang if (ifr->ifr_flags & IFF_MULTI_QUEUE) 1645c8d68e6bSJason Wang tun->flags |= TUN_TAP_MQ; 1646c8d68e6bSJason Wang else 1647c8d68e6bSJason Wang tun->flags &= ~TUN_TAP_MQ; 1648c8d68e6bSJason Wang 1649e35259a9SMax Krasnyansky /* Make sure persistent devices do not get stuck in 1650e35259a9SMax Krasnyansky * xoff state. 1651e35259a9SMax Krasnyansky */ 1652e35259a9SMax Krasnyansky if (netif_running(tun->dev)) 1653c8d68e6bSJason Wang netif_tx_wake_all_queues(tun->dev); 1654e35259a9SMax Krasnyansky 16551da177e4SLinus Torvalds strcpy(ifr->ifr_name, tun->dev->name); 16561da177e4SLinus Torvalds return 0; 16571da177e4SLinus Torvalds 16581da177e4SLinus Torvalds err_free_dev: 16591da177e4SLinus Torvalds free_netdev(dev); 16601da177e4SLinus Torvalds return err; 16611da177e4SLinus Torvalds } 16621da177e4SLinus Torvalds 1663876bfd4dSHerbert Xu static int tun_get_iff(struct net *net, struct tun_struct *tun, 1664876bfd4dSHerbert Xu struct ifreq *ifr) 1665e3b99556SMark McLoughlin { 16666b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "tun_get_iff\n"); 1667e3b99556SMark McLoughlin 1668e3b99556SMark McLoughlin strcpy(ifr->ifr_name, tun->dev->name); 1669e3b99556SMark McLoughlin 1670980c9e8cSDavid Woodhouse ifr->ifr_flags = tun_flags(tun); 1671e3b99556SMark McLoughlin 1672e3b99556SMark McLoughlin return 0; 1673e3b99556SMark McLoughlin } 1674e3b99556SMark McLoughlin 16755228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no 16765228ddc9SRusty Russell * privs required. */ 167788255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg) 16785228ddc9SRusty Russell { 1679c8f44affSMichał Mirosław netdev_features_t features = 0; 16805228ddc9SRusty Russell 16815228ddc9SRusty Russell if (arg & TUN_F_CSUM) { 168288255375SMichał Mirosław features |= NETIF_F_HW_CSUM; 16835228ddc9SRusty Russell arg &= ~TUN_F_CSUM; 16845228ddc9SRusty Russell 16855228ddc9SRusty Russell if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { 16865228ddc9SRusty Russell if (arg & TUN_F_TSO_ECN) { 16875228ddc9SRusty Russell features |= NETIF_F_TSO_ECN; 16885228ddc9SRusty Russell arg &= ~TUN_F_TSO_ECN; 16895228ddc9SRusty Russell } 16905228ddc9SRusty Russell if (arg & TUN_F_TSO4) 16915228ddc9SRusty Russell features |= NETIF_F_TSO; 16925228ddc9SRusty Russell if (arg & TUN_F_TSO6) 16935228ddc9SRusty Russell features |= NETIF_F_TSO6; 16945228ddc9SRusty Russell arg &= ~(TUN_F_TSO4|TUN_F_TSO6); 16955228ddc9SRusty Russell } 1696e36aa25aSSridhar Samudrala 1697e36aa25aSSridhar Samudrala if (arg & TUN_F_UFO) { 1698e36aa25aSSridhar Samudrala features |= NETIF_F_UFO; 1699e36aa25aSSridhar Samudrala arg &= ~TUN_F_UFO; 1700e36aa25aSSridhar Samudrala } 17015228ddc9SRusty Russell } 17025228ddc9SRusty Russell 17035228ddc9SRusty Russell /* This gives the user a way to test for new features in future by 17045228ddc9SRusty Russell * trying to set them. */ 17055228ddc9SRusty Russell if (arg) 17065228ddc9SRusty Russell return -EINVAL; 17075228ddc9SRusty Russell 170888255375SMichał Mirosław tun->set_features = features; 170988255375SMichał Mirosław netdev_update_features(tun->dev); 17105228ddc9SRusty Russell 17115228ddc9SRusty Russell return 0; 17125228ddc9SRusty Russell } 17135228ddc9SRusty Russell 1714c8d68e6bSJason Wang static void tun_detach_filter(struct tun_struct *tun, int n) 1715c8d68e6bSJason Wang { 1716c8d68e6bSJason Wang int i; 1717c8d68e6bSJason Wang struct tun_file *tfile; 1718c8d68e6bSJason Wang 1719c8d68e6bSJason Wang for (i = 0; i < n; i++) { 1720c8d68e6bSJason Wang tfile = rcu_dereference_protected(tun->tfiles[i], 1721c8d68e6bSJason Wang lockdep_rtnl_is_held()); 1722c8d68e6bSJason Wang sk_detach_filter(tfile->socket.sk); 1723c8d68e6bSJason Wang } 1724c8d68e6bSJason Wang 1725c8d68e6bSJason Wang tun->filter_attached = false; 1726c8d68e6bSJason Wang } 1727c8d68e6bSJason Wang 1728c8d68e6bSJason Wang static int tun_attach_filter(struct tun_struct *tun) 1729c8d68e6bSJason Wang { 1730c8d68e6bSJason Wang int i, ret = 0; 1731c8d68e6bSJason Wang struct tun_file *tfile; 1732c8d68e6bSJason Wang 1733c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 1734c8d68e6bSJason Wang tfile = rcu_dereference_protected(tun->tfiles[i], 1735c8d68e6bSJason Wang lockdep_rtnl_is_held()); 1736c8d68e6bSJason Wang ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); 1737c8d68e6bSJason Wang if (ret) { 1738c8d68e6bSJason Wang tun_detach_filter(tun, i); 1739c8d68e6bSJason Wang return ret; 1740c8d68e6bSJason Wang } 1741c8d68e6bSJason Wang } 1742c8d68e6bSJason Wang 1743c8d68e6bSJason Wang tun->filter_attached = true; 1744c8d68e6bSJason Wang return ret; 1745c8d68e6bSJason Wang } 1746c8d68e6bSJason Wang 1747c8d68e6bSJason Wang static void tun_set_sndbuf(struct tun_struct *tun) 1748c8d68e6bSJason Wang { 1749c8d68e6bSJason Wang struct tun_file *tfile; 1750c8d68e6bSJason Wang int i; 1751c8d68e6bSJason Wang 1752c8d68e6bSJason Wang for (i = 0; i < tun->numqueues; i++) { 1753c8d68e6bSJason Wang tfile = rcu_dereference_protected(tun->tfiles[i], 1754c8d68e6bSJason Wang lockdep_rtnl_is_held()); 1755c8d68e6bSJason Wang tfile->socket.sk->sk_sndbuf = tun->sndbuf; 1756c8d68e6bSJason Wang } 1757c8d68e6bSJason Wang } 1758c8d68e6bSJason Wang 1759cde8b15fSJason Wang static int tun_set_queue(struct file *file, struct ifreq *ifr) 1760cde8b15fSJason Wang { 1761cde8b15fSJason Wang struct tun_file *tfile = file->private_data; 1762cde8b15fSJason Wang struct tun_struct *tun; 1763cde8b15fSJason Wang struct net_device *dev; 1764cde8b15fSJason Wang int ret = 0; 1765cde8b15fSJason Wang 1766cde8b15fSJason Wang rtnl_lock(); 1767cde8b15fSJason Wang 1768cde8b15fSJason Wang if (ifr->ifr_flags & IFF_ATTACH_QUEUE) { 1769cde8b15fSJason Wang dev = __dev_get_by_name(tfile->net, ifr->ifr_name); 1770cde8b15fSJason Wang if (!dev) { 1771cde8b15fSJason Wang ret = -EINVAL; 1772cde8b15fSJason Wang goto unlock; 1773cde8b15fSJason Wang } 1774cde8b15fSJason Wang 1775cde8b15fSJason Wang tun = netdev_priv(dev); 1776cde8b15fSJason Wang if (dev->netdev_ops != &tap_netdev_ops && 1777cde8b15fSJason Wang dev->netdev_ops != &tun_netdev_ops) 1778cde8b15fSJason Wang ret = -EINVAL; 1779cde8b15fSJason Wang else if (tun_not_capable(tun)) 1780cde8b15fSJason Wang ret = -EPERM; 1781cde8b15fSJason Wang else 1782cde8b15fSJason Wang ret = tun_attach(tun, file); 1783cde8b15fSJason Wang } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) 1784cde8b15fSJason Wang __tun_detach(tfile, false); 1785cde8b15fSJason Wang else 1786cde8b15fSJason Wang ret = -EINVAL; 1787cde8b15fSJason Wang 1788cde8b15fSJason Wang unlock: 1789cde8b15fSJason Wang rtnl_unlock(); 1790cde8b15fSJason Wang return ret; 1791cde8b15fSJason Wang } 1792cde8b15fSJason Wang 179350857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd, 179450857e2aSArnd Bergmann unsigned long arg, int ifreq_len) 17951da177e4SLinus Torvalds { 179636b50babSEric W. Biederman struct tun_file *tfile = file->private_data; 1797631ab46bSEric W. Biederman struct tun_struct *tun; 17981da177e4SLinus Torvalds void __user* argp = (void __user*)arg; 17991da177e4SLinus Torvalds struct ifreq ifr; 18000625c883SEric W. Biederman kuid_t owner; 18010625c883SEric W. Biederman kgid_t group; 180233dccbb0SHerbert Xu int sndbuf; 1803d9d52b51SMichael S. Tsirkin int vnet_hdr_sz; 1804f271b2ccSMax Krasnyansky int ret; 18051da177e4SLinus Torvalds 1806cde8b15fSJason Wang if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) { 180750857e2aSArnd Bergmann if (copy_from_user(&ifr, argp, ifreq_len)) 18081da177e4SLinus Torvalds return -EFAULT; 18098bbb1813SDavid S. Miller } else { 1810a117dacdSMathias Krause memset(&ifr, 0, sizeof(ifr)); 18118bbb1813SDavid S. Miller } 1812631ab46bSEric W. Biederman if (cmd == TUNGETFEATURES) { 1813631ab46bSEric W. Biederman /* Currently this just means: "what IFF flags are valid?". 1814631ab46bSEric W. Biederman * This is needed because we never checked for invalid flags on 1815631ab46bSEric W. Biederman * TUNSETIFF. */ 1816631ab46bSEric W. Biederman return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | 1817cde8b15fSJason Wang IFF_VNET_HDR | IFF_MULTI_QUEUE, 1818631ab46bSEric W. Biederman (unsigned int __user*)argp); 1819cde8b15fSJason Wang } else if (cmd == TUNSETQUEUE) 1820cde8b15fSJason Wang return tun_set_queue(file, &ifr); 1821631ab46bSEric W. Biederman 1822c8d68e6bSJason Wang ret = 0; 1823876bfd4dSHerbert Xu rtnl_lock(); 1824876bfd4dSHerbert Xu 182536b50babSEric W. Biederman tun = __tun_get(tfile); 18261da177e4SLinus Torvalds if (cmd == TUNSETIFF && !tun) { 18271da177e4SLinus Torvalds ifr.ifr_name[IFNAMSIZ-1] = '\0'; 18281da177e4SLinus Torvalds 1829876bfd4dSHerbert Xu ret = tun_set_iff(tfile->net, file, &ifr); 18301da177e4SLinus Torvalds 1831876bfd4dSHerbert Xu if (ret) 1832876bfd4dSHerbert Xu goto unlock; 18331da177e4SLinus Torvalds 183450857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 1835876bfd4dSHerbert Xu ret = -EFAULT; 1836876bfd4dSHerbert Xu goto unlock; 18371da177e4SLinus Torvalds } 18381da177e4SLinus Torvalds 1839876bfd4dSHerbert Xu ret = -EBADFD; 18401da177e4SLinus Torvalds if (!tun) 1841876bfd4dSHerbert Xu goto unlock; 18421da177e4SLinus Torvalds 18431e588338SJason Wang tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd); 18441da177e4SLinus Torvalds 1845631ab46bSEric W. Biederman ret = 0; 18461da177e4SLinus Torvalds switch (cmd) { 1847e3b99556SMark McLoughlin case TUNGETIFF: 1848876bfd4dSHerbert Xu ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr); 1849e3b99556SMark McLoughlin if (ret) 1850631ab46bSEric W. Biederman break; 1851e3b99556SMark McLoughlin 185250857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 1853631ab46bSEric W. Biederman ret = -EFAULT; 1854e3b99556SMark McLoughlin break; 1855e3b99556SMark McLoughlin 18561da177e4SLinus Torvalds case TUNSETNOCSUM: 18571da177e4SLinus Torvalds /* Disable/Enable checksum */ 18581da177e4SLinus Torvalds 185988255375SMichał Mirosław /* [unimplemented] */ 186088255375SMichał Mirosław tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n", 18616b8a66eeSJoe Perches arg ? "disabled" : "enabled"); 18621da177e4SLinus Torvalds break; 18631da177e4SLinus Torvalds 18641da177e4SLinus Torvalds case TUNSETPERSIST: 186554f968d6SJason Wang /* Disable/Enable persist mode. Keep an extra reference to the 186654f968d6SJason Wang * module to prevent the module being unprobed. 186754f968d6SJason Wang */ 186854f968d6SJason Wang if (arg) { 18691da177e4SLinus Torvalds tun->flags |= TUN_PERSIST; 187054f968d6SJason Wang __module_get(THIS_MODULE); 187154f968d6SJason Wang } else { 18721da177e4SLinus Torvalds tun->flags &= ~TUN_PERSIST; 187354f968d6SJason Wang module_put(THIS_MODULE); 187454f968d6SJason Wang } 18751da177e4SLinus Torvalds 18766b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "persist %s\n", 18776b8a66eeSJoe Perches arg ? "enabled" : "disabled"); 18781da177e4SLinus Torvalds break; 18791da177e4SLinus Torvalds 18801da177e4SLinus Torvalds case TUNSETOWNER: 18811da177e4SLinus Torvalds /* Set owner of the device */ 18820625c883SEric W. Biederman owner = make_kuid(current_user_ns(), arg); 18830625c883SEric W. Biederman if (!uid_valid(owner)) { 18840625c883SEric W. Biederman ret = -EINVAL; 18850625c883SEric W. Biederman break; 18860625c883SEric W. Biederman } 18870625c883SEric W. Biederman tun->owner = owner; 18881e588338SJason Wang tun_debug(KERN_INFO, tun, "owner set to %u\n", 18890625c883SEric W. Biederman from_kuid(&init_user_ns, tun->owner)); 18901da177e4SLinus Torvalds break; 18911da177e4SLinus Torvalds 18928c644623SGuido Guenther case TUNSETGROUP: 18938c644623SGuido Guenther /* Set group of the device */ 18940625c883SEric W. Biederman group = make_kgid(current_user_ns(), arg); 18950625c883SEric W. Biederman if (!gid_valid(group)) { 18960625c883SEric W. Biederman ret = -EINVAL; 18970625c883SEric W. Biederman break; 18980625c883SEric W. Biederman } 18990625c883SEric W. Biederman tun->group = group; 19001e588338SJason Wang tun_debug(KERN_INFO, tun, "group set to %u\n", 19010625c883SEric W. Biederman from_kgid(&init_user_ns, tun->group)); 19028c644623SGuido Guenther break; 19038c644623SGuido Guenther 1904ff4cc3acSMike Kershaw case TUNSETLINK: 1905ff4cc3acSMike Kershaw /* Only allow setting the type when the interface is down */ 1906ff4cc3acSMike Kershaw if (tun->dev->flags & IFF_UP) { 19076b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, 19086b8a66eeSJoe Perches "Linktype set failed because interface is up\n"); 190948abfe05SDavid S. Miller ret = -EBUSY; 1910ff4cc3acSMike Kershaw } else { 1911ff4cc3acSMike Kershaw tun->dev->type = (int) arg; 19126b8a66eeSJoe Perches tun_debug(KERN_INFO, tun, "linktype set to %d\n", 19136b8a66eeSJoe Perches tun->dev->type); 191448abfe05SDavid S. Miller ret = 0; 1915ff4cc3acSMike Kershaw } 1916631ab46bSEric W. Biederman break; 1917ff4cc3acSMike Kershaw 19181da177e4SLinus Torvalds #ifdef TUN_DEBUG 19191da177e4SLinus Torvalds case TUNSETDEBUG: 19201da177e4SLinus Torvalds tun->debug = arg; 19211da177e4SLinus Torvalds break; 19221da177e4SLinus Torvalds #endif 19235228ddc9SRusty Russell case TUNSETOFFLOAD: 192488255375SMichał Mirosław ret = set_offload(tun, arg); 1925631ab46bSEric W. Biederman break; 19265228ddc9SRusty Russell 1927f271b2ccSMax Krasnyansky case TUNSETTXFILTER: 1928f271b2ccSMax Krasnyansky /* Can be set only for TAPs */ 1929631ab46bSEric W. Biederman ret = -EINVAL; 1930f271b2ccSMax Krasnyansky if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) 1931631ab46bSEric W. Biederman break; 1932c0e5a8c2SHarvey Harrison ret = update_filter(&tun->txflt, (void __user *)arg); 1933631ab46bSEric W. Biederman break; 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds case SIOCGIFHWADDR: 1936b595076aSUwe Kleine-König /* Get hw address */ 1937f271b2ccSMax Krasnyansky memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); 1938f271b2ccSMax Krasnyansky ifr.ifr_hwaddr.sa_family = tun->dev->type; 193950857e2aSArnd Bergmann if (copy_to_user(argp, &ifr, ifreq_len)) 1940631ab46bSEric W. Biederman ret = -EFAULT; 1941631ab46bSEric W. Biederman break; 19421da177e4SLinus Torvalds 19431da177e4SLinus Torvalds case SIOCSIFHWADDR: 1944f271b2ccSMax Krasnyansky /* Set hw address */ 19456b8a66eeSJoe Perches tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n", 19466b8a66eeSJoe Perches ifr.ifr_hwaddr.sa_data); 194740102371SKim B. Heino 194840102371SKim B. Heino ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); 1949631ab46bSEric W. Biederman break; 195033dccbb0SHerbert Xu 195133dccbb0SHerbert Xu case TUNGETSNDBUF: 195254f968d6SJason Wang sndbuf = tfile->socket.sk->sk_sndbuf; 195333dccbb0SHerbert Xu if (copy_to_user(argp, &sndbuf, sizeof(sndbuf))) 195433dccbb0SHerbert Xu ret = -EFAULT; 195533dccbb0SHerbert Xu break; 195633dccbb0SHerbert Xu 195733dccbb0SHerbert Xu case TUNSETSNDBUF: 195833dccbb0SHerbert Xu if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) { 195933dccbb0SHerbert Xu ret = -EFAULT; 196033dccbb0SHerbert Xu break; 196133dccbb0SHerbert Xu } 196233dccbb0SHerbert Xu 1963c8d68e6bSJason Wang tun->sndbuf = sndbuf; 1964c8d68e6bSJason Wang tun_set_sndbuf(tun); 196533dccbb0SHerbert Xu break; 196633dccbb0SHerbert Xu 1967d9d52b51SMichael S. Tsirkin case TUNGETVNETHDRSZ: 1968d9d52b51SMichael S. Tsirkin vnet_hdr_sz = tun->vnet_hdr_sz; 1969d9d52b51SMichael S. Tsirkin if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz))) 1970d9d52b51SMichael S. Tsirkin ret = -EFAULT; 1971d9d52b51SMichael S. Tsirkin break; 1972d9d52b51SMichael S. Tsirkin 1973d9d52b51SMichael S. Tsirkin case TUNSETVNETHDRSZ: 1974d9d52b51SMichael S. Tsirkin if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) { 1975d9d52b51SMichael S. Tsirkin ret = -EFAULT; 1976d9d52b51SMichael S. Tsirkin break; 1977d9d52b51SMichael S. Tsirkin } 1978d9d52b51SMichael S. Tsirkin if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) { 1979d9d52b51SMichael S. Tsirkin ret = -EINVAL; 1980d9d52b51SMichael S. Tsirkin break; 1981d9d52b51SMichael S. Tsirkin } 1982d9d52b51SMichael S. Tsirkin 1983d9d52b51SMichael S. Tsirkin tun->vnet_hdr_sz = vnet_hdr_sz; 1984d9d52b51SMichael S. Tsirkin break; 1985d9d52b51SMichael S. Tsirkin 198699405162SMichael S. Tsirkin case TUNATTACHFILTER: 198799405162SMichael S. Tsirkin /* Can be set only for TAPs */ 198899405162SMichael S. Tsirkin ret = -EINVAL; 198999405162SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) 199099405162SMichael S. Tsirkin break; 199199405162SMichael S. Tsirkin ret = -EFAULT; 199254f968d6SJason Wang if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog))) 199399405162SMichael S. Tsirkin break; 199499405162SMichael S. Tsirkin 1995c8d68e6bSJason Wang ret = tun_attach_filter(tun); 199699405162SMichael S. Tsirkin break; 199799405162SMichael S. Tsirkin 199899405162SMichael S. Tsirkin case TUNDETACHFILTER: 199999405162SMichael S. Tsirkin /* Can be set only for TAPs */ 200099405162SMichael S. Tsirkin ret = -EINVAL; 200199405162SMichael S. Tsirkin if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) 200299405162SMichael S. Tsirkin break; 2003c8d68e6bSJason Wang ret = 0; 2004c8d68e6bSJason Wang tun_detach_filter(tun, tun->numqueues); 200599405162SMichael S. Tsirkin break; 200699405162SMichael S. Tsirkin 20071da177e4SLinus Torvalds default: 2008631ab46bSEric W. Biederman ret = -EINVAL; 2009631ab46bSEric W. Biederman break; 2010ee289b64SJoe Perches } 20111da177e4SLinus Torvalds 2012876bfd4dSHerbert Xu unlock: 2013876bfd4dSHerbert Xu rtnl_unlock(); 2014876bfd4dSHerbert Xu if (tun) 2015631ab46bSEric W. Biederman tun_put(tun); 2016631ab46bSEric W. Biederman return ret; 20171da177e4SLinus Torvalds } 20181da177e4SLinus Torvalds 201950857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file, 202050857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 202150857e2aSArnd Bergmann { 202250857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq)); 202350857e2aSArnd Bergmann } 202450857e2aSArnd Bergmann 202550857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 202650857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file, 202750857e2aSArnd Bergmann unsigned int cmd, unsigned long arg) 202850857e2aSArnd Bergmann { 202950857e2aSArnd Bergmann switch (cmd) { 203050857e2aSArnd Bergmann case TUNSETIFF: 203150857e2aSArnd Bergmann case TUNGETIFF: 203250857e2aSArnd Bergmann case TUNSETTXFILTER: 203350857e2aSArnd Bergmann case TUNGETSNDBUF: 203450857e2aSArnd Bergmann case TUNSETSNDBUF: 203550857e2aSArnd Bergmann case SIOCGIFHWADDR: 203650857e2aSArnd Bergmann case SIOCSIFHWADDR: 203750857e2aSArnd Bergmann arg = (unsigned long)compat_ptr(arg); 203850857e2aSArnd Bergmann break; 203950857e2aSArnd Bergmann default: 204050857e2aSArnd Bergmann arg = (compat_ulong_t)arg; 204150857e2aSArnd Bergmann break; 204250857e2aSArnd Bergmann } 204350857e2aSArnd Bergmann 204450857e2aSArnd Bergmann /* 204550857e2aSArnd Bergmann * compat_ifreq is shorter than ifreq, so we must not access beyond 204650857e2aSArnd Bergmann * the end of that structure. All fields that are used in this 204750857e2aSArnd Bergmann * driver are compatible though, we don't need to convert the 204850857e2aSArnd Bergmann * contents. 204950857e2aSArnd Bergmann */ 205050857e2aSArnd Bergmann return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq)); 205150857e2aSArnd Bergmann } 205250857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */ 205350857e2aSArnd Bergmann 20541da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on) 20551da177e4SLinus Torvalds { 205654f968d6SJason Wang struct tun_file *tfile = file->private_data; 20571da177e4SLinus Torvalds int ret; 20581da177e4SLinus Torvalds 205954f968d6SJason Wang if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) 20609d319522SJonathan Corbet goto out; 20611da177e4SLinus Torvalds 20621da177e4SLinus Torvalds if (on) { 2063609d7fa9SEric W. Biederman ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0); 20641da177e4SLinus Torvalds if (ret) 20659d319522SJonathan Corbet goto out; 206654f968d6SJason Wang tfile->flags |= TUN_FASYNC; 20671da177e4SLinus Torvalds } else 206854f968d6SJason Wang tfile->flags &= ~TUN_FASYNC; 20699d319522SJonathan Corbet ret = 0; 20709d319522SJonathan Corbet out: 20719d319522SJonathan Corbet return ret; 20721da177e4SLinus Torvalds } 20731da177e4SLinus Torvalds 20741da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file) 20751da177e4SLinus Torvalds { 2076631ab46bSEric W. Biederman struct tun_file *tfile; 2077deed49fbSThomas Gleixner 20786b8a66eeSJoe Perches DBG1(KERN_INFO, "tunX: tun_chr_open\n"); 2079631ab46bSEric W. Biederman 208054f968d6SJason Wang tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL, 208154f968d6SJason Wang &tun_proto); 2082631ab46bSEric W. Biederman if (!tfile) 2083631ab46bSEric W. Biederman return -ENOMEM; 20846e914fc7SJason Wang rcu_assign_pointer(tfile->tun, NULL); 208536b50babSEric W. Biederman tfile->net = get_net(current->nsproxy->net_ns); 208654f968d6SJason Wang tfile->flags = 0; 208754f968d6SJason Wang 208854f968d6SJason Wang rcu_assign_pointer(tfile->socket.wq, &tfile->wq); 208954f968d6SJason Wang init_waitqueue_head(&tfile->wq.wait); 209054f968d6SJason Wang 209154f968d6SJason Wang tfile->socket.file = file; 209254f968d6SJason Wang tfile->socket.ops = &tun_socket_ops; 209354f968d6SJason Wang 209454f968d6SJason Wang sock_init_data(&tfile->socket, &tfile->sk); 209554f968d6SJason Wang sk_change_net(&tfile->sk, tfile->net); 209654f968d6SJason Wang 209754f968d6SJason Wang tfile->sk.sk_write_space = tun_sock_write_space; 209854f968d6SJason Wang tfile->sk.sk_sndbuf = INT_MAX; 209954f968d6SJason Wang 2100631ab46bSEric W. Biederman file->private_data = tfile; 210154f968d6SJason Wang set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags); 210254f968d6SJason Wang 21031da177e4SLinus Torvalds return 0; 21041da177e4SLinus Torvalds } 21051da177e4SLinus Torvalds 21061da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file) 21071da177e4SLinus Torvalds { 2108631ab46bSEric W. Biederman struct tun_file *tfile = file->private_data; 210954f968d6SJason Wang struct net *net = tfile->net; 21101da177e4SLinus Torvalds 2111c8d68e6bSJason Wang tun_detach(tfile, true); 211254f968d6SJason Wang put_net(net); 21131da177e4SLinus Torvalds 21141da177e4SLinus Torvalds return 0; 21151da177e4SLinus Torvalds } 21161da177e4SLinus Torvalds 2117d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = { 21181da177e4SLinus Torvalds .owner = THIS_MODULE, 21191da177e4SLinus Torvalds .llseek = no_llseek, 2120ee0b3e67SBadari Pulavarty .read = do_sync_read, 2121ee0b3e67SBadari Pulavarty .aio_read = tun_chr_aio_read, 2122ee0b3e67SBadari Pulavarty .write = do_sync_write, 2123ee0b3e67SBadari Pulavarty .aio_write = tun_chr_aio_write, 21241da177e4SLinus Torvalds .poll = tun_chr_poll, 2125876bfd4dSHerbert Xu .unlocked_ioctl = tun_chr_ioctl, 212650857e2aSArnd Bergmann #ifdef CONFIG_COMPAT 212750857e2aSArnd Bergmann .compat_ioctl = tun_chr_compat_ioctl, 212850857e2aSArnd Bergmann #endif 21291da177e4SLinus Torvalds .open = tun_chr_open, 21301da177e4SLinus Torvalds .release = tun_chr_close, 21311da177e4SLinus Torvalds .fasync = tun_chr_fasync 21321da177e4SLinus Torvalds }; 21331da177e4SLinus Torvalds 21341da177e4SLinus Torvalds static struct miscdevice tun_miscdev = { 21351da177e4SLinus Torvalds .minor = TUN_MINOR, 21361da177e4SLinus Torvalds .name = "tun", 2137e454cea2SKay Sievers .nodename = "net/tun", 21381da177e4SLinus Torvalds .fops = &tun_fops, 21391da177e4SLinus Torvalds }; 21401da177e4SLinus Torvalds 21411da177e4SLinus Torvalds /* ethtool interface */ 21421da177e4SLinus Torvalds 21431da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 21441da177e4SLinus Torvalds { 21451da177e4SLinus Torvalds cmd->supported = 0; 21461da177e4SLinus Torvalds cmd->advertising = 0; 214770739497SDavid Decotigny ethtool_cmd_speed_set(cmd, SPEED_10); 21481da177e4SLinus Torvalds cmd->duplex = DUPLEX_FULL; 21491da177e4SLinus Torvalds cmd->port = PORT_TP; 21501da177e4SLinus Torvalds cmd->phy_address = 0; 21511da177e4SLinus Torvalds cmd->transceiver = XCVR_INTERNAL; 21521da177e4SLinus Torvalds cmd->autoneg = AUTONEG_DISABLE; 21531da177e4SLinus Torvalds cmd->maxtxpkt = 0; 21541da177e4SLinus Torvalds cmd->maxrxpkt = 0; 21551da177e4SLinus Torvalds return 0; 21561da177e4SLinus Torvalds } 21571da177e4SLinus Torvalds 21581da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) 21591da177e4SLinus Torvalds { 21601da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 21611da177e4SLinus Torvalds 216233a5ba14SRick Jones strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); 216333a5ba14SRick Jones strlcpy(info->version, DRV_VERSION, sizeof(info->version)); 21641da177e4SLinus Torvalds 21651da177e4SLinus Torvalds switch (tun->flags & TUN_TYPE_MASK) { 21661da177e4SLinus Torvalds case TUN_TUN_DEV: 216733a5ba14SRick Jones strlcpy(info->bus_info, "tun", sizeof(info->bus_info)); 21681da177e4SLinus Torvalds break; 21691da177e4SLinus Torvalds case TUN_TAP_DEV: 217033a5ba14SRick Jones strlcpy(info->bus_info, "tap", sizeof(info->bus_info)); 21711da177e4SLinus Torvalds break; 21721da177e4SLinus Torvalds } 21731da177e4SLinus Torvalds } 21741da177e4SLinus Torvalds 21751da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev) 21761da177e4SLinus Torvalds { 21771da177e4SLinus Torvalds #ifdef TUN_DEBUG 21781da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 21791da177e4SLinus Torvalds return tun->debug; 21801da177e4SLinus Torvalds #else 21811da177e4SLinus Torvalds return -EOPNOTSUPP; 21821da177e4SLinus Torvalds #endif 21831da177e4SLinus Torvalds } 21841da177e4SLinus Torvalds 21851da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value) 21861da177e4SLinus Torvalds { 21871da177e4SLinus Torvalds #ifdef TUN_DEBUG 21881da177e4SLinus Torvalds struct tun_struct *tun = netdev_priv(dev); 21891da177e4SLinus Torvalds tun->debug = value; 21901da177e4SLinus Torvalds #endif 21911da177e4SLinus Torvalds } 21921da177e4SLinus Torvalds 21937282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = { 21941da177e4SLinus Torvalds .get_settings = tun_get_settings, 21951da177e4SLinus Torvalds .get_drvinfo = tun_get_drvinfo, 21961da177e4SLinus Torvalds .get_msglevel = tun_get_msglevel, 21971da177e4SLinus Torvalds .set_msglevel = tun_set_msglevel, 2198bee31369SNolan Leake .get_link = ethtool_op_get_link, 21991da177e4SLinus Torvalds }; 22001da177e4SLinus Torvalds 220179d17604SPavel Emelyanov 22021da177e4SLinus Torvalds static int __init tun_init(void) 22031da177e4SLinus Torvalds { 22041da177e4SLinus Torvalds int ret = 0; 22051da177e4SLinus Torvalds 22066b8a66eeSJoe Perches pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION); 22076b8a66eeSJoe Perches pr_info("%s\n", DRV_COPYRIGHT); 22081da177e4SLinus Torvalds 2209f019a7a5SEric W. Biederman ret = rtnl_link_register(&tun_link_ops); 221079d17604SPavel Emelyanov if (ret) { 22116b8a66eeSJoe Perches pr_err("Can't register link_ops\n"); 2212f019a7a5SEric W. Biederman goto err_linkops; 221379d17604SPavel Emelyanov } 221479d17604SPavel Emelyanov 22151da177e4SLinus Torvalds ret = misc_register(&tun_miscdev); 221679d17604SPavel Emelyanov if (ret) { 22176b8a66eeSJoe Perches pr_err("Can't register misc device %d\n", TUN_MINOR); 221879d17604SPavel Emelyanov goto err_misc; 221979d17604SPavel Emelyanov } 222079d17604SPavel Emelyanov return 0; 222179d17604SPavel Emelyanov err_misc: 2222f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 2223f019a7a5SEric W. Biederman err_linkops: 22241da177e4SLinus Torvalds return ret; 22251da177e4SLinus Torvalds } 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds static void tun_cleanup(void) 22281da177e4SLinus Torvalds { 22291da177e4SLinus Torvalds misc_deregister(&tun_miscdev); 2230f019a7a5SEric W. Biederman rtnl_link_unregister(&tun_link_ops); 22311da177e4SLinus Torvalds } 22321da177e4SLinus Torvalds 223305c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file. Returns error unless file is 223405c2828cSMichael S. Tsirkin * attached to a device. The returned object works like a packet socket, it 223505c2828cSMichael S. Tsirkin * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for 223605c2828cSMichael S. Tsirkin * holding a reference to the file for as long as the socket is in use. */ 223705c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file) 223805c2828cSMichael S. Tsirkin { 22396e914fc7SJason Wang struct tun_file *tfile; 224005c2828cSMichael S. Tsirkin if (file->f_op != &tun_fops) 224105c2828cSMichael S. Tsirkin return ERR_PTR(-EINVAL); 22426e914fc7SJason Wang tfile = file->private_data; 22436e914fc7SJason Wang if (!tfile) 224405c2828cSMichael S. Tsirkin return ERR_PTR(-EBADFD); 224554f968d6SJason Wang return &tfile->socket; 224605c2828cSMichael S. Tsirkin } 224705c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket); 224805c2828cSMichael S. Tsirkin 22491da177e4SLinus Torvalds module_init(tun_init); 22501da177e4SLinus Torvalds module_exit(tun_cleanup); 22511da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION); 22521da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT); 22531da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 22541da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR); 2255578454ffSKay Sievers MODULE_ALIAS("devname:net/tun"); 2256