Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * TUN - Universal TUN/TAP device driver. |
| 3 | * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $ |
| 16 | */ |
| 17 | |
| 18 | /* |
| 19 | * Changes: |
| 20 | * |
Mike Kershaw | ff4cc3a | 2005-09-01 17:40:05 -0700 | [diff] [blame] | 21 | * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14 |
| 22 | * Add TUNSETLINK ioctl to set the link encapsulation |
| 23 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * Mark Smith <markzzzsmith@yahoo.com.au> |
Joe Perches | 344dc8e | 2012-07-12 19:33:09 +0000 | [diff] [blame] | 25 | * Use eth_random_addr() for tap MAC address. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | * |
| 27 | * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20 |
| 28 | * Fixes in packet dropping, queue length setting and queue wakeup. |
| 29 | * Increased default tx queue length. |
| 30 | * Added ethtool API. |
| 31 | * Minor cleanups |
| 32 | * |
| 33 | * Daniel Podlejski <underley@underley.eu.org> |
| 34 | * Modifications for 2.3.99-pre5 kernel. |
| 35 | */ |
| 36 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 37 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #define DRV_NAME "tun" |
| 40 | #define DRV_VERSION "1.6" |
| 41 | #define DRV_DESCRIPTION "Universal TUN/TAP device driver" |
| 42 | #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>" |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/module.h> |
| 45 | #include <linux/errno.h> |
| 46 | #include <linux/kernel.h> |
| 47 | #include <linux/major.h> |
| 48 | #include <linux/slab.h> |
| 49 | #include <linux/poll.h> |
| 50 | #include <linux/fcntl.h> |
| 51 | #include <linux/init.h> |
| 52 | #include <linux/skbuff.h> |
| 53 | #include <linux/netdevice.h> |
| 54 | #include <linux/etherdevice.h> |
| 55 | #include <linux/miscdevice.h> |
| 56 | #include <linux/ethtool.h> |
| 57 | #include <linux/rtnetlink.h> |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 58 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <linux/if.h> |
| 60 | #include <linux/if_arp.h> |
| 61 | #include <linux/if_ether.h> |
| 62 | #include <linux/if_tun.h> |
Jason Wang | 6680ec6 | 2013-07-25 13:00:33 +0800 | [diff] [blame] | 63 | #include <linux/if_vlan.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | #include <linux/crc32.h> |
Pavel Emelyanov | d647a59 | 2008-04-16 00:41:16 -0700 | [diff] [blame] | 65 | #include <linux/nsproxy.h> |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 66 | #include <linux/virtio_net.h> |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 67 | #include <linux/rcupdate.h> |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 68 | #include <net/net_namespace.h> |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 69 | #include <net/netns/generic.h> |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 70 | #include <net/rtnetlink.h> |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 71 | #include <net/sock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | #include <asm/uaccess.h> |
| 74 | |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 75 | /* Uncomment to enable debugging */ |
| 76 | /* #define TUN_DEBUG 1 */ |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | #ifdef TUN_DEBUG |
| 79 | static int debug; |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 80 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 81 | #define tun_debug(level, tun, fmt, args...) \ |
| 82 | do { \ |
| 83 | if (tun->debug) \ |
| 84 | netdev_printk(level, tun->dev, fmt, ##args); \ |
| 85 | } while (0) |
| 86 | #define DBG1(level, fmt, args...) \ |
| 87 | do { \ |
| 88 | if (debug == 2) \ |
| 89 | printk(level fmt, ##args); \ |
| 90 | } while (0) |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 91 | #else |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 92 | #define tun_debug(level, tun, fmt, args...) \ |
| 93 | do { \ |
| 94 | if (0) \ |
| 95 | netdev_printk(level, tun->dev, fmt, ##args); \ |
| 96 | } while (0) |
| 97 | #define DBG1(level, fmt, args...) \ |
| 98 | do { \ |
| 99 | if (0) \ |
| 100 | printk(level fmt, ##args); \ |
| 101 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | #endif |
| 103 | |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 104 | #define GOODCOPY_LEN 128 |
| 105 | |
Max Krasnyansky | f271b2c | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 106 | #define FLT_EXACT_COUNT 8 |
| 107 | struct tap_filter { |
| 108 | unsigned int count; /* Number of addrs. Zero means disabled */ |
| 109 | u32 mask[2]; /* Mask of the hashed addrs */ |
| 110 | unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN]; |
| 111 | }; |
| 112 | |
stephen hemminger | 92d4ea6 | 2013-12-05 20:42:58 -0800 | [diff] [blame] | 113 | /* DEFAULT_MAX_NUM_RSS_QUEUES were chosen to let the rx/tx queues allocated for |
Jason Wang | edfb6a1 | 2013-01-23 03:59:12 +0000 | [diff] [blame] | 114 | * the netdevice to be fit in one page. So we can make sure the success of |
| 115 | * memory allocation. TODO: increase the limit. */ |
| 116 | #define MAX_TAP_QUEUES DEFAULT_MAX_NUM_RSS_QUEUES |
Jason Wang | b8732fb | 2013-01-23 03:59:13 +0000 | [diff] [blame] | 117 | #define MAX_TAP_FLOWS 4096 |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 118 | |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 119 | #define TUN_FLOW_EXPIRE (3 * HZ) |
| 120 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 121 | /* A tun_file connects an open character device to a tuntap netdevice. It |
stephen hemminger | 92d4ea6 | 2013-12-05 20:42:58 -0800 | [diff] [blame] | 122 | * also contains all socket related structures (except sock_fprog and tap_filter) |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 123 | * to serve as one transmit queue for tuntap device. The sock_fprog and |
| 124 | * tap_filter were kept in tun_struct since they were used for filtering for the |
Rami Rosen | 36fe8c09 | 2012-11-25 22:07:40 +0000 | [diff] [blame] | 125 | * netdevice not for a specific queue (at least I didn't see the requirement for |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 126 | * this). |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 127 | * |
| 128 | * RCU usage: |
Rami Rosen | 36fe8c09 | 2012-11-25 22:07:40 +0000 | [diff] [blame] | 129 | * The tun_file and tun_struct are loosely coupled, the pointer from one to the |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 130 | * other can only be read while rcu_read_lock or rtnl_lock is held. |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 131 | */ |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 132 | struct tun_file { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 133 | struct sock sk; |
| 134 | struct socket socket; |
| 135 | struct socket_wq wq; |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 136 | struct tun_struct __rcu *tun; |
Eric W. Biederman | 36b50ba | 2009-01-20 11:01:48 +0000 | [diff] [blame] | 137 | struct net *net; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 138 | struct fasync_struct *fasync; |
| 139 | /* only used for fasnyc */ |
| 140 | unsigned int flags; |
Pavel Emelyanov | fb7589a | 2013-08-21 14:31:38 +0400 | [diff] [blame] | 141 | union { |
| 142 | u16 queue_index; |
| 143 | unsigned int ifindex; |
| 144 | }; |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 145 | struct list_head next; |
| 146 | struct tun_struct *detached; |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 149 | struct tun_flow_entry { |
| 150 | struct hlist_node hash_link; |
| 151 | struct rcu_head rcu; |
| 152 | struct tun_struct *tun; |
| 153 | |
| 154 | u32 rxhash; |
| 155 | int queue_index; |
| 156 | unsigned long updated; |
| 157 | }; |
| 158 | |
| 159 | #define TUN_NUM_FLOW_ENTRIES 1024 |
| 160 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 161 | /* Since the socket were moved to tun_file, to preserve the behavior of persist |
Rami Rosen | 36fe8c09 | 2012-11-25 22:07:40 +0000 | [diff] [blame] | 162 | * device, socket filter, sndbuf and vnet header size were restore when the |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 163 | * file were attached to a persist device. |
| 164 | */ |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 165 | struct tun_struct { |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 166 | struct tun_file __rcu *tfiles[MAX_TAP_QUEUES]; |
| 167 | unsigned int numqueues; |
Max Krasnyansky | f271b2c | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 168 | unsigned int flags; |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 169 | kuid_t owner; |
| 170 | kgid_t group; |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 171 | |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 172 | struct net_device *dev; |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 173 | netdev_features_t set_features; |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 174 | #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \ |
| 175 | NETIF_F_TSO6|NETIF_F_UFO) |
Michael S. Tsirkin | d9d52b5 | 2010-03-17 17:45:01 +0200 | [diff] [blame] | 176 | |
| 177 | int vnet_hdr_sz; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 178 | int sndbuf; |
| 179 | struct tap_filter txflt; |
| 180 | struct sock_fprog fprog; |
| 181 | /* protected by rtnl lock */ |
| 182 | bool filter_attached; |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 183 | #ifdef TUN_DEBUG |
| 184 | int debug; |
| 185 | #endif |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 186 | spinlock_t lock; |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 187 | struct hlist_head flows[TUN_NUM_FLOW_ENTRIES]; |
| 188 | struct timer_list flow_gc_timer; |
| 189 | unsigned long ageing_time; |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 190 | unsigned int numdisabled; |
| 191 | struct list_head disabled; |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 192 | void *security; |
Jason Wang | b8732fb | 2013-01-23 03:59:13 +0000 | [diff] [blame] | 193 | u32 flow_count; |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 194 | }; |
| 195 | |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 196 | static inline u32 tun_hashfn(u32 rxhash) |
| 197 | { |
| 198 | return rxhash & 0x3ff; |
| 199 | } |
| 200 | |
| 201 | static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash) |
| 202 | { |
| 203 | struct tun_flow_entry *e; |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 204 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 205 | hlist_for_each_entry_rcu(e, head, hash_link) { |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 206 | if (e->rxhash == rxhash) |
| 207 | return e; |
| 208 | } |
| 209 | return NULL; |
| 210 | } |
| 211 | |
| 212 | static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun, |
| 213 | struct hlist_head *head, |
| 214 | u32 rxhash, u16 queue_index) |
| 215 | { |
Eric Dumazet | 9fdc6be | 2012-12-21 07:17:21 +0000 | [diff] [blame] | 216 | struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC); |
| 217 | |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 218 | if (e) { |
| 219 | tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n", |
| 220 | rxhash, queue_index); |
| 221 | e->updated = jiffies; |
| 222 | e->rxhash = rxhash; |
| 223 | e->queue_index = queue_index; |
| 224 | e->tun = tun; |
| 225 | hlist_add_head_rcu(&e->hash_link, head); |
Jason Wang | b8732fb | 2013-01-23 03:59:13 +0000 | [diff] [blame] | 226 | ++tun->flow_count; |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 227 | } |
| 228 | return e; |
| 229 | } |
| 230 | |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 231 | static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e) |
| 232 | { |
| 233 | tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n", |
| 234 | e->rxhash, e->queue_index); |
| 235 | hlist_del_rcu(&e->hash_link); |
Eric Dumazet | 9fdc6be | 2012-12-21 07:17:21 +0000 | [diff] [blame] | 236 | kfree_rcu(e, rcu); |
Jason Wang | b8732fb | 2013-01-23 03:59:13 +0000 | [diff] [blame] | 237 | --tun->flow_count; |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static void tun_flow_flush(struct tun_struct *tun) |
| 241 | { |
| 242 | int i; |
| 243 | |
| 244 | spin_lock_bh(&tun->lock); |
| 245 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { |
| 246 | struct tun_flow_entry *e; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 247 | struct hlist_node *n; |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 248 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 249 | hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 250 | tun_flow_delete(tun, e); |
| 251 | } |
| 252 | spin_unlock_bh(&tun->lock); |
| 253 | } |
| 254 | |
| 255 | static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index) |
| 256 | { |
| 257 | int i; |
| 258 | |
| 259 | spin_lock_bh(&tun->lock); |
| 260 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { |
| 261 | struct tun_flow_entry *e; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 262 | struct hlist_node *n; |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 263 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 264 | hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 265 | if (e->queue_index == queue_index) |
| 266 | tun_flow_delete(tun, e); |
| 267 | } |
| 268 | } |
| 269 | spin_unlock_bh(&tun->lock); |
| 270 | } |
| 271 | |
| 272 | static void tun_flow_cleanup(unsigned long data) |
| 273 | { |
| 274 | struct tun_struct *tun = (struct tun_struct *)data; |
| 275 | unsigned long delay = tun->ageing_time; |
| 276 | unsigned long next_timer = jiffies + delay; |
| 277 | unsigned long count = 0; |
| 278 | int i; |
| 279 | |
| 280 | tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n"); |
| 281 | |
| 282 | spin_lock_bh(&tun->lock); |
| 283 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { |
| 284 | struct tun_flow_entry *e; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 285 | struct hlist_node *n; |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 286 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 287 | hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 288 | unsigned long this_timer; |
| 289 | count++; |
| 290 | this_timer = e->updated + delay; |
| 291 | if (time_before_eq(this_timer, jiffies)) |
| 292 | tun_flow_delete(tun, e); |
| 293 | else if (time_before(this_timer, next_timer)) |
| 294 | next_timer = this_timer; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if (count) |
| 299 | mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer)); |
| 300 | spin_unlock_bh(&tun->lock); |
| 301 | } |
| 302 | |
Eric Dumazet | 4997442 | 2012-12-12 19:22:57 +0000 | [diff] [blame] | 303 | static void tun_flow_update(struct tun_struct *tun, u32 rxhash, |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 304 | struct tun_file *tfile) |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 305 | { |
| 306 | struct hlist_head *head; |
| 307 | struct tun_flow_entry *e; |
| 308 | unsigned long delay = tun->ageing_time; |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 309 | u16 queue_index = tfile->queue_index; |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 310 | |
| 311 | if (!rxhash) |
| 312 | return; |
| 313 | else |
| 314 | head = &tun->flows[tun_hashfn(rxhash)]; |
| 315 | |
| 316 | rcu_read_lock(); |
| 317 | |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 318 | /* We may get a very small possibility of OOO during switching, not |
| 319 | * worth to optimize.*/ |
| 320 | if (tun->numqueues == 1 || tfile->detached) |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 321 | goto unlock; |
| 322 | |
| 323 | e = tun_flow_find(head, rxhash); |
| 324 | if (likely(e)) { |
| 325 | /* TODO: keep queueing to old queue until it's empty? */ |
| 326 | e->queue_index = queue_index; |
| 327 | e->updated = jiffies; |
| 328 | } else { |
| 329 | spin_lock_bh(&tun->lock); |
Jason Wang | b8732fb | 2013-01-23 03:59:13 +0000 | [diff] [blame] | 330 | if (!tun_flow_find(head, rxhash) && |
| 331 | tun->flow_count < MAX_TAP_FLOWS) |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 332 | tun_flow_create(tun, head, rxhash, queue_index); |
| 333 | |
| 334 | if (!timer_pending(&tun->flow_gc_timer)) |
| 335 | mod_timer(&tun->flow_gc_timer, |
| 336 | round_jiffies_up(jiffies + delay)); |
| 337 | spin_unlock_bh(&tun->lock); |
| 338 | } |
| 339 | |
| 340 | unlock: |
| 341 | rcu_read_unlock(); |
| 342 | } |
| 343 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 344 | /* We try to identify a flow through its rxhash first. The reason that |
stephen hemminger | 92d4ea6 | 2013-12-05 20:42:58 -0800 | [diff] [blame] | 345 | * we do not check rxq no. is because some cards(e.g 82599), chooses |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 346 | * the rxq based on the txq where the last packet of the flow comes. As |
| 347 | * the userspace application move between processors, we may get a |
| 348 | * different rxq no. here. If we could not get rxhash, then we would |
| 349 | * hope the rxq no. may help here. |
| 350 | */ |
| 351 | static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb) |
| 352 | { |
| 353 | struct tun_struct *tun = netdev_priv(dev); |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 354 | struct tun_flow_entry *e; |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 355 | u32 txq = 0; |
| 356 | u32 numqueues = 0; |
| 357 | |
| 358 | rcu_read_lock(); |
Jason Wang | 92bb73e | 2013-06-05 16:44:57 +0800 | [diff] [blame] | 359 | numqueues = ACCESS_ONCE(tun->numqueues); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 360 | |
| 361 | txq = skb_get_rxhash(skb); |
| 362 | if (txq) { |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 363 | e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq); |
| 364 | if (e) |
| 365 | txq = e->queue_index; |
| 366 | else |
| 367 | /* use multiply and shift instead of expensive divide */ |
| 368 | txq = ((u64)txq * numqueues) >> 32; |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 369 | } else if (likely(skb_rx_queue_recorded(skb))) { |
| 370 | txq = skb_get_rx_queue(skb); |
| 371 | while (unlikely(txq >= numqueues)) |
| 372 | txq -= numqueues; |
| 373 | } |
| 374 | |
| 375 | rcu_read_unlock(); |
| 376 | return txq; |
| 377 | } |
| 378 | |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 379 | static inline bool tun_not_capable(struct tun_struct *tun) |
| 380 | { |
| 381 | const struct cred *cred = current_cred(); |
Eric W. Biederman | c260b77 | 2012-11-18 21:34:11 +0000 | [diff] [blame] | 382 | struct net *net = dev_net(tun->dev); |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 383 | |
| 384 | return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) || |
| 385 | (gid_valid(tun->group) && !in_egroup_p(tun->group))) && |
Eric W. Biederman | c260b77 | 2012-11-18 21:34:11 +0000 | [diff] [blame] | 386 | !ns_capable(net->user_ns, CAP_NET_ADMIN); |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 389 | static void tun_set_real_num_queues(struct tun_struct *tun) |
| 390 | { |
| 391 | netif_set_real_num_tx_queues(tun->dev, tun->numqueues); |
| 392 | netif_set_real_num_rx_queues(tun->dev, tun->numqueues); |
| 393 | } |
| 394 | |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 395 | static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) |
| 396 | { |
| 397 | tfile->detached = tun; |
| 398 | list_add_tail(&tfile->next, &tun->disabled); |
| 399 | ++tun->numdisabled; |
| 400 | } |
| 401 | |
Jason Wang | d32649d | 2012-12-18 11:00:27 +0800 | [diff] [blame] | 402 | static struct tun_struct *tun_enable_queue(struct tun_file *tfile) |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 403 | { |
| 404 | struct tun_struct *tun = tfile->detached; |
| 405 | |
| 406 | tfile->detached = NULL; |
| 407 | list_del_init(&tfile->next); |
| 408 | --tun->numdisabled; |
| 409 | return tun; |
| 410 | } |
| 411 | |
Jason Wang | 4bfb051 | 2013-09-05 17:53:59 +0800 | [diff] [blame] | 412 | static void tun_queue_purge(struct tun_file *tfile) |
| 413 | { |
| 414 | skb_queue_purge(&tfile->sk.sk_receive_queue); |
| 415 | skb_queue_purge(&tfile->sk.sk_error_queue); |
| 416 | } |
| 417 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 418 | static void __tun_detach(struct tun_file *tfile, bool clean) |
| 419 | { |
| 420 | struct tun_file *ntfile; |
| 421 | struct tun_struct *tun; |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 422 | |
Jason Wang | b8deabd | 2013-01-11 16:59:32 +0000 | [diff] [blame] | 423 | tun = rtnl_dereference(tfile->tun); |
| 424 | |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 425 | if (tun && !tfile->detached) { |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 426 | u16 index = tfile->queue_index; |
| 427 | BUG_ON(index >= tun->numqueues); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 428 | |
| 429 | rcu_assign_pointer(tun->tfiles[index], |
| 430 | tun->tfiles[tun->numqueues - 1]); |
Jason Wang | b8deabd | 2013-01-11 16:59:32 +0000 | [diff] [blame] | 431 | ntfile = rtnl_dereference(tun->tfiles[index]); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 432 | ntfile->queue_index = index; |
| 433 | |
| 434 | --tun->numqueues; |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 435 | if (clean) { |
| 436 | rcu_assign_pointer(tfile->tun, NULL); |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 437 | sock_put(&tfile->sk); |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 438 | } else |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 439 | tun_disable_queue(tun, tfile); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 440 | |
| 441 | synchronize_net(); |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 442 | tun_flow_delete_by_queue(tun, tun->numqueues + 1); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 443 | /* Drop read queue */ |
Jason Wang | 4bfb051 | 2013-09-05 17:53:59 +0800 | [diff] [blame] | 444 | tun_queue_purge(tfile); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 445 | tun_set_real_num_queues(tun); |
Jason Wang | dd38bd8 | 2013-01-11 16:59:34 +0000 | [diff] [blame] | 446 | } else if (tfile->detached && clean) { |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 447 | tun = tun_enable_queue(tfile); |
Jason Wang | dd38bd8 | 2013-01-11 16:59:34 +0000 | [diff] [blame] | 448 | sock_put(&tfile->sk); |
| 449 | } |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 450 | |
| 451 | if (clean) { |
Michael S. Tsirkin | af668b3 | 2013-01-28 00:38:02 +0000 | [diff] [blame] | 452 | if (tun && tun->numqueues == 0 && tun->numdisabled == 0) { |
| 453 | netif_carrier_off(tun->dev); |
| 454 | |
| 455 | if (!(tun->flags & TUN_PERSIST) && |
| 456 | tun->dev->reg_state == NETREG_REGISTERED) |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 457 | unregister_netdevice(tun->dev); |
Michael S. Tsirkin | af668b3 | 2013-01-28 00:38:02 +0000 | [diff] [blame] | 458 | } |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 459 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 460 | BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED, |
| 461 | &tfile->socket.flags)); |
| 462 | sk_release_kernel(&tfile->sk); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | static void tun_detach(struct tun_file *tfile, bool clean) |
| 467 | { |
| 468 | rtnl_lock(); |
| 469 | __tun_detach(tfile, clean); |
| 470 | rtnl_unlock(); |
| 471 | } |
| 472 | |
| 473 | static void tun_detach_all(struct net_device *dev) |
| 474 | { |
| 475 | struct tun_struct *tun = netdev_priv(dev); |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 476 | struct tun_file *tfile, *tmp; |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 477 | int i, n = tun->numqueues; |
| 478 | |
| 479 | for (i = 0; i < n; i++) { |
Jason Wang | b8deabd | 2013-01-11 16:59:32 +0000 | [diff] [blame] | 480 | tfile = rtnl_dereference(tun->tfiles[i]); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 481 | BUG_ON(!tfile); |
| 482 | wake_up_all(&tfile->wq.wait); |
| 483 | rcu_assign_pointer(tfile->tun, NULL); |
| 484 | --tun->numqueues; |
| 485 | } |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 486 | list_for_each_entry(tfile, &tun->disabled, next) { |
| 487 | wake_up_all(&tfile->wq.wait); |
| 488 | rcu_assign_pointer(tfile->tun, NULL); |
| 489 | } |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 490 | BUG_ON(tun->numqueues != 0); |
| 491 | |
| 492 | synchronize_net(); |
| 493 | for (i = 0; i < n; i++) { |
Jason Wang | b8deabd | 2013-01-11 16:59:32 +0000 | [diff] [blame] | 494 | tfile = rtnl_dereference(tun->tfiles[i]); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 495 | /* Drop read queue */ |
Jason Wang | 4bfb051 | 2013-09-05 17:53:59 +0800 | [diff] [blame] | 496 | tun_queue_purge(tfile); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 497 | sock_put(&tfile->sk); |
| 498 | } |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 499 | list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { |
| 500 | tun_enable_queue(tfile); |
Jason Wang | 4bfb051 | 2013-09-05 17:53:59 +0800 | [diff] [blame] | 501 | tun_queue_purge(tfile); |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 502 | sock_put(&tfile->sk); |
| 503 | } |
| 504 | BUG_ON(tun->numdisabled != 0); |
Jason Wang | dd38bd8 | 2013-01-11 16:59:34 +0000 | [diff] [blame] | 505 | |
| 506 | if (tun->flags & TUN_PERSIST) |
| 507 | module_put(THIS_MODULE); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Pavel Emelyanov | 849c9b6 | 2013-08-21 14:32:21 +0400 | [diff] [blame] | 510 | static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter) |
Eric W. Biederman | a7385ba | 2009-01-20 10:57:48 +0000 | [diff] [blame] | 511 | { |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 512 | struct tun_file *tfile = file->private_data; |
Eric W. Biederman | 38231b7 | 2009-01-20 11:02:28 +0000 | [diff] [blame] | 513 | int err; |
Eric W. Biederman | a7385ba | 2009-01-20 10:57:48 +0000 | [diff] [blame] | 514 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 515 | err = security_tun_dev_attach(tfile->socket.sk, tun->security); |
| 516 | if (err < 0) |
| 517 | goto out; |
| 518 | |
Eric W. Biederman | 38231b7 | 2009-01-20 11:02:28 +0000 | [diff] [blame] | 519 | err = -EINVAL; |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 520 | if (rtnl_dereference(tfile->tun) && !tfile->detached) |
Eric W. Biederman | 38231b7 | 2009-01-20 11:02:28 +0000 | [diff] [blame] | 521 | goto out; |
| 522 | |
| 523 | err = -EBUSY; |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 524 | if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1) |
| 525 | goto out; |
| 526 | |
| 527 | err = -E2BIG; |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 528 | if (!tfile->detached && |
| 529 | tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES) |
Eric W. Biederman | 38231b7 | 2009-01-20 11:02:28 +0000 | [diff] [blame] | 530 | goto out; |
| 531 | |
| 532 | err = 0; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 533 | |
stephen hemminger | 92d4ea6 | 2013-12-05 20:42:58 -0800 | [diff] [blame] | 534 | /* Re-attach the filter to persist device */ |
Pavel Emelyanov | 849c9b6 | 2013-08-21 14:32:21 +0400 | [diff] [blame] | 535 | if (!skip_filter && (tun->filter_attached == true)) { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 536 | err = sk_attach_filter(&tun->fprog, tfile->socket.sk); |
| 537 | if (!err) |
| 538 | goto out; |
| 539 | } |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 540 | tfile->queue_index = tun->numqueues; |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 541 | rcu_assign_pointer(tfile->tun, tun); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 542 | rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 543 | tun->numqueues++; |
| 544 | |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 545 | if (tfile->detached) |
| 546 | tun_enable_queue(tfile); |
| 547 | else |
| 548 | sock_hold(&tfile->sk); |
| 549 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 550 | tun_set_real_num_queues(tun); |
| 551 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 552 | /* device is allowed to go away first, so no need to hold extra |
| 553 | * refcnt. |
| 554 | */ |
Eric W. Biederman | a7385ba | 2009-01-20 10:57:48 +0000 | [diff] [blame] | 555 | |
Eric W. Biederman | 38231b7 | 2009-01-20 11:02:28 +0000 | [diff] [blame] | 556 | out: |
Eric W. Biederman | 38231b7 | 2009-01-20 11:02:28 +0000 | [diff] [blame] | 557 | return err; |
Eric W. Biederman | a7385ba | 2009-01-20 10:57:48 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 560 | static struct tun_struct *__tun_get(struct tun_file *tfile) |
| 561 | { |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 562 | struct tun_struct *tun; |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 563 | |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 564 | rcu_read_lock(); |
| 565 | tun = rcu_dereference(tfile->tun); |
| 566 | if (tun) |
| 567 | dev_hold(tun->dev); |
| 568 | rcu_read_unlock(); |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 569 | |
| 570 | return tun; |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | static struct tun_struct *tun_get(struct file *file) |
| 574 | { |
| 575 | return __tun_get(file->private_data); |
| 576 | } |
| 577 | |
| 578 | static void tun_put(struct tun_struct *tun) |
| 579 | { |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 580 | dev_put(tun->dev); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 583 | /* TAP filtering */ |
Max Krasnyansky | f271b2c | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 584 | static void addr_hash_set(u32 *mask, const u8 *addr) |
| 585 | { |
| 586 | int n = ether_crc(ETH_ALEN, addr) >> 26; |
| 587 | mask[n >> 5] |= (1 << (n & 31)); |
| 588 | } |
| 589 | |
| 590 | static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) |
| 591 | { |
| 592 | int n = ether_crc(ETH_ALEN, addr) >> 26; |
| 593 | return mask[n >> 5] & (1 << (n & 31)); |
| 594 | } |
| 595 | |
| 596 | static int update_filter(struct tap_filter *filter, void __user *arg) |
| 597 | { |
| 598 | struct { u8 u[ETH_ALEN]; } *addr; |
| 599 | struct tun_filter uf; |
| 600 | int err, alen, n, nexact; |
| 601 | |
| 602 | if (copy_from_user(&uf, arg, sizeof(uf))) |
| 603 | return -EFAULT; |
| 604 | |
| 605 | if (!uf.count) { |
| 606 | /* Disabled */ |
| 607 | filter->count = 0; |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | alen = ETH_ALEN * uf.count; |
| 612 | addr = kmalloc(alen, GFP_KERNEL); |
| 613 | if (!addr) |
| 614 | return -ENOMEM; |
| 615 | |
| 616 | if (copy_from_user(addr, arg + sizeof(uf), alen)) { |
| 617 | err = -EFAULT; |
| 618 | goto done; |
| 619 | } |
| 620 | |
| 621 | /* The filter is updated without holding any locks. Which is |
| 622 | * perfectly safe. We disable it first and in the worst |
| 623 | * case we'll accept a few undesired packets. */ |
| 624 | filter->count = 0; |
| 625 | wmb(); |
| 626 | |
| 627 | /* Use first set of addresses as an exact filter */ |
| 628 | for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++) |
| 629 | memcpy(filter->addr[n], addr[n].u, ETH_ALEN); |
| 630 | |
| 631 | nexact = n; |
| 632 | |
Alex Williamson | cfbf84f | 2009-02-08 17:49:17 -0800 | [diff] [blame] | 633 | /* Remaining multicast addresses are hashed, |
| 634 | * unicast will leave the filter disabled. */ |
Max Krasnyansky | f271b2c | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 635 | memset(filter->mask, 0, sizeof(filter->mask)); |
Alex Williamson | cfbf84f | 2009-02-08 17:49:17 -0800 | [diff] [blame] | 636 | for (; n < uf.count; n++) { |
| 637 | if (!is_multicast_ether_addr(addr[n].u)) { |
| 638 | err = 0; /* no filter */ |
| 639 | goto done; |
| 640 | } |
Max Krasnyansky | f271b2c | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 641 | addr_hash_set(filter->mask, addr[n].u); |
Alex Williamson | cfbf84f | 2009-02-08 17:49:17 -0800 | [diff] [blame] | 642 | } |
Max Krasnyansky | f271b2c | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 643 | |
| 644 | /* For ALLMULTI just set the mask to all ones. |
| 645 | * This overrides the mask populated above. */ |
| 646 | if ((uf.flags & TUN_FLT_ALLMULTI)) |
| 647 | memset(filter->mask, ~0, sizeof(filter->mask)); |
| 648 | |
| 649 | /* Now enable the filter */ |
| 650 | wmb(); |
| 651 | filter->count = nexact; |
| 652 | |
| 653 | /* Return the number of exact filters */ |
| 654 | err = nexact; |
| 655 | |
| 656 | done: |
| 657 | kfree(addr); |
| 658 | return err; |
| 659 | } |
| 660 | |
| 661 | /* Returns: 0 - drop, !=0 - accept */ |
| 662 | static int run_filter(struct tap_filter *filter, const struct sk_buff *skb) |
| 663 | { |
| 664 | /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect |
| 665 | * at this point. */ |
| 666 | struct ethhdr *eh = (struct ethhdr *) skb->data; |
| 667 | int i; |
| 668 | |
| 669 | /* Exact match */ |
| 670 | for (i = 0; i < filter->count; i++) |
Joe Perches | 2e42e47 | 2012-05-09 17:17:46 +0000 | [diff] [blame] | 671 | if (ether_addr_equal(eh->h_dest, filter->addr[i])) |
Max Krasnyansky | f271b2c | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 672 | return 1; |
| 673 | |
| 674 | /* Inexact match (multicast only) */ |
| 675 | if (is_multicast_ether_addr(eh->h_dest)) |
| 676 | return addr_hash_test(filter->mask, eh->h_dest); |
| 677 | |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | * Checks whether the packet is accepted or not. |
| 683 | * Returns: 0 - drop, !=0 - accept |
| 684 | */ |
| 685 | static int check_filter(struct tap_filter *filter, const struct sk_buff *skb) |
| 686 | { |
| 687 | if (!filter->count) |
| 688 | return 1; |
| 689 | |
| 690 | return run_filter(filter, skb); |
| 691 | } |
| 692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | /* Network device part of the driver */ |
| 694 | |
Jeff Garzik | 7282d49 | 2006-09-13 14:30:00 -0400 | [diff] [blame] | 695 | static const struct ethtool_ops tun_ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 697 | /* Net device detach from fd. */ |
| 698 | static void tun_net_uninit(struct net_device *dev) |
| 699 | { |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 700 | tun_detach_all(dev); |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | /* Net device open. */ |
| 704 | static int tun_net_open(struct net_device *dev) |
| 705 | { |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 706 | netif_tx_start_all_queues(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | /* Net device close. */ |
| 711 | static int tun_net_close(struct net_device *dev) |
| 712 | { |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 713 | netif_tx_stop_all_queues(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | /* Net device start xmit */ |
Stephen Hemminger | 424efe9 | 2009-08-31 19:50:51 +0000 | [diff] [blame] | 718 | static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | { |
| 720 | struct tun_struct *tun = netdev_priv(dev); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 721 | int txq = skb->queue_mapping; |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 722 | struct tun_file *tfile; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 724 | rcu_read_lock(); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 725 | tfile = rcu_dereference(tun->tfiles[txq]); |
| 726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | /* Drop packet if interface is not attached */ |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 728 | if (txq >= tun->numqueues) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | goto drop; |
| 730 | |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 731 | tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len); |
| 732 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 733 | BUG_ON(!tfile); |
| 734 | |
Max Krasnyansky | f271b2c | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 735 | /* Drop if the filter does not like it. |
| 736 | * This is a noop if the filter is disabled. |
| 737 | * Filter can be enabled only for the TAP devices. */ |
| 738 | if (!check_filter(&tun->txflt, skb)) |
| 739 | goto drop; |
| 740 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 741 | if (tfile->socket.sk->sk_filter && |
| 742 | sk_filter(tfile->socket.sk, skb)) |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 743 | goto drop; |
| 744 | |
Rami Rosen | 36fe8c09 | 2012-11-25 22:07:40 +0000 | [diff] [blame] | 745 | /* Limit the number of packets queued by dividing txq length with the |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 746 | * number of queues. |
| 747 | */ |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 748 | if (skb_queue_len(&tfile->socket.sk->sk_receive_queue) |
Michael S. Tsirkin | 5d09710 | 2012-12-03 10:07:14 +0000 | [diff] [blame] | 749 | >= dev->tx_queue_len / tun->numqueues) |
| 750 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | |
Jason Wang | 7bf6630 | 2013-09-05 17:54:00 +0800 | [diff] [blame] | 752 | if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC))) |
| 753 | goto drop; |
| 754 | |
Richard Cochran | eda2977 | 2013-07-19 19:40:10 +0200 | [diff] [blame] | 755 | if (skb->sk) { |
| 756 | sock_tx_timestamp(skb->sk, &skb_shinfo(skb)->tx_flags); |
| 757 | sw_tx_timestamp(skb); |
| 758 | } |
| 759 | |
Michael S. Tsirkin | 0110d6f | 2010-04-13 04:59:44 +0000 | [diff] [blame] | 760 | /* Orphan the skb - required as we might hang on to it |
Jason Wang | 7bf6630 | 2013-09-05 17:54:00 +0800 | [diff] [blame] | 761 | * for indefinite time. |
| 762 | */ |
Michael S. Tsirkin | 0110d6f | 2010-04-13 04:59:44 +0000 | [diff] [blame] | 763 | skb_orphan(skb); |
| 764 | |
Eric Dumazet | f8af75f | 2013-03-06 11:02:37 +0000 | [diff] [blame] | 765 | nf_reset(skb); |
| 766 | |
Max Krasnyansky | f271b2c | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 767 | /* Enqueue packet */ |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 768 | skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
| 770 | /* Notify and wake up reader process */ |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 771 | if (tfile->flags & TUN_FASYNC) |
| 772 | kill_fasync(&tfile->fasync, SIGIO, POLL_IN); |
| 773 | wake_up_interruptible_poll(&tfile->wq.wait, POLLIN | |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 774 | POLLRDNORM | POLLRDBAND); |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 775 | |
| 776 | rcu_read_unlock(); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 777 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
| 779 | drop: |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 780 | dev->stats.tx_dropped++; |
Michael S. Tsirkin | 149d36f | 2012-11-01 09:16:32 +0000 | [diff] [blame] | 781 | skb_tx_error(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | kfree_skb(skb); |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 783 | rcu_read_unlock(); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 784 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Max Krasnyansky | f271b2c | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 787 | static void tun_net_mclist(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | { |
Max Krasnyansky | f271b2c | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 789 | /* |
| 790 | * This callback is supposed to deal with mc filter in |
| 791 | * _rx_ path and has nothing to do with the _tx_ path. |
| 792 | * In rx path we always accept everything userspace gives us. |
| 793 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Ed Swierk | 4885a50 | 2007-09-16 12:21:38 -0700 | [diff] [blame] | 796 | #define MIN_MTU 68 |
| 797 | #define MAX_MTU 65535 |
| 798 | |
| 799 | static int |
| 800 | tun_net_change_mtu(struct net_device *dev, int new_mtu) |
| 801 | { |
| 802 | if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU) |
| 803 | return -EINVAL; |
| 804 | dev->mtu = new_mtu; |
| 805 | return 0; |
| 806 | } |
| 807 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 808 | static netdev_features_t tun_net_fix_features(struct net_device *dev, |
| 809 | netdev_features_t features) |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 810 | { |
| 811 | struct tun_struct *tun = netdev_priv(dev); |
| 812 | |
| 813 | return (features & tun->set_features) | (features & ~TUN_USER_FEATURES); |
| 814 | } |
Neil Horman | bebd097 | 2011-06-15 05:25:01 +0000 | [diff] [blame] | 815 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 816 | static void tun_poll_controller(struct net_device *dev) |
| 817 | { |
| 818 | /* |
| 819 | * Tun only receives frames when: |
| 820 | * 1) the char device endpoint gets data from user space |
| 821 | * 2) the tun socket gets a sendmsg call from user space |
stephen hemminger | 92d4ea6 | 2013-12-05 20:42:58 -0800 | [diff] [blame] | 822 | * Since both of those are synchronous operations, we are guaranteed |
Neil Horman | bebd097 | 2011-06-15 05:25:01 +0000 | [diff] [blame] | 823 | * never to have pending data when we poll for it |
stephen hemminger | 92d4ea6 | 2013-12-05 20:42:58 -0800 | [diff] [blame] | 824 | * so there is nothing to do here but return. |
Neil Horman | bebd097 | 2011-06-15 05:25:01 +0000 | [diff] [blame] | 825 | * We need this though so netpoll recognizes us as an interface that |
| 826 | * supports polling, which enables bridge devices in virt setups to |
| 827 | * still use netconsole |
| 828 | */ |
| 829 | return; |
| 830 | } |
| 831 | #endif |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 832 | static const struct net_device_ops tun_netdev_ops = { |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 833 | .ndo_uninit = tun_net_uninit, |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 834 | .ndo_open = tun_net_open, |
| 835 | .ndo_stop = tun_net_close, |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 836 | .ndo_start_xmit = tun_net_xmit, |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 837 | .ndo_change_mtu = tun_net_change_mtu, |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 838 | .ndo_fix_features = tun_net_fix_features, |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 839 | .ndo_select_queue = tun_select_queue, |
Neil Horman | bebd097 | 2011-06-15 05:25:01 +0000 | [diff] [blame] | 840 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 841 | .ndo_poll_controller = tun_poll_controller, |
| 842 | #endif |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 843 | }; |
| 844 | |
| 845 | static const struct net_device_ops tap_netdev_ops = { |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 846 | .ndo_uninit = tun_net_uninit, |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 847 | .ndo_open = tun_net_open, |
| 848 | .ndo_stop = tun_net_close, |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 849 | .ndo_start_xmit = tun_net_xmit, |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 850 | .ndo_change_mtu = tun_net_change_mtu, |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 851 | .ndo_fix_features = tun_net_fix_features, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 852 | .ndo_set_rx_mode = tun_net_mclist, |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 853 | .ndo_set_mac_address = eth_mac_addr, |
| 854 | .ndo_validate_addr = eth_validate_addr, |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 855 | .ndo_select_queue = tun_select_queue, |
Neil Horman | bebd097 | 2011-06-15 05:25:01 +0000 | [diff] [blame] | 856 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 857 | .ndo_poll_controller = tun_poll_controller, |
| 858 | #endif |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 859 | }; |
| 860 | |
Pavel Emelyanov | 944a137 | 2013-06-11 17:01:08 +0400 | [diff] [blame] | 861 | static void tun_flow_init(struct tun_struct *tun) |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 862 | { |
| 863 | int i; |
| 864 | |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 865 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) |
| 866 | INIT_HLIST_HEAD(&tun->flows[i]); |
| 867 | |
| 868 | tun->ageing_time = TUN_FLOW_EXPIRE; |
| 869 | setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun); |
| 870 | mod_timer(&tun->flow_gc_timer, |
| 871 | round_jiffies_up(jiffies + tun->ageing_time)); |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | static void tun_flow_uninit(struct tun_struct *tun) |
| 875 | { |
| 876 | del_timer_sync(&tun->flow_gc_timer); |
| 877 | tun_flow_flush(tun); |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | /* Initialize net device. */ |
| 881 | static void tun_net_init(struct net_device *dev) |
| 882 | { |
| 883 | struct tun_struct *tun = netdev_priv(dev); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | switch (tun->flags & TUN_TYPE_MASK) { |
| 886 | case TUN_TUN_DEV: |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 887 | dev->netdev_ops = &tun_netdev_ops; |
| 888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | /* Point-to-Point TUN Device */ |
| 890 | dev->hard_header_len = 0; |
| 891 | dev->addr_len = 0; |
| 892 | dev->mtu = 1500; |
| 893 | |
| 894 | /* Zero header length */ |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 895 | dev->type = ARPHRD_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; |
| 897 | dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ |
| 898 | break; |
| 899 | |
| 900 | case TUN_TAP_DEV: |
Kusanagi Kouichi | 7a0a960 | 2008-12-29 18:23:28 -0800 | [diff] [blame] | 901 | dev->netdev_ops = &tap_netdev_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | /* Ethernet TAP Device */ |
Max Krasnyansky | f271b2c | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 903 | ether_setup(dev); |
Neil Horman | 550fd08 | 2011-07-26 06:05:38 +0000 | [diff] [blame] | 904 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
stephen hemminger | a676847 | 2012-12-10 15:16:00 +0000 | [diff] [blame] | 905 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | |
Danny Kukawka | f2cedb6 | 2012-02-15 06:45:39 +0000 | [diff] [blame] | 907 | eth_hw_addr_random(dev); |
Brian Braunstein | 36226a8 | 2007-04-26 01:00:55 -0700 | [diff] [blame] | 908 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ |
| 910 | break; |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | /* Character device part */ |
| 915 | |
| 916 | /* Poll */ |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 917 | static unsigned int tun_chr_poll(struct file *file, poll_table *wait) |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 918 | { |
Eric W. Biederman | b2430de | 2009-01-20 11:03:21 +0000 | [diff] [blame] | 919 | struct tun_file *tfile = file->private_data; |
| 920 | struct tun_struct *tun = __tun_get(tfile); |
Mariusz Kozlowski | 3c8a9c6 | 2009-07-05 19:48:35 +0000 | [diff] [blame] | 921 | struct sock *sk; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 922 | unsigned int mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
| 924 | if (!tun) |
Eric W. Biederman | eac9e90 | 2009-01-20 10:59:05 +0000 | [diff] [blame] | 925 | return POLLERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 927 | sk = tfile->socket.sk; |
Mariusz Kozlowski | 3c8a9c6 | 2009-07-05 19:48:35 +0000 | [diff] [blame] | 928 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 929 | tun_debug(KERN_INFO, tun, "tun_chr_poll\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 931 | poll_wait(file, &tfile->wq.wait, wait); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 932 | |
Michael S. Tsirkin | 89f56d1 | 2009-08-30 07:04:42 +0000 | [diff] [blame] | 933 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | mask |= POLLIN | POLLRDNORM; |
| 935 | |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 936 | if (sock_writeable(sk) || |
| 937 | (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) && |
| 938 | sock_writeable(sk))) |
| 939 | mask |= POLLOUT | POLLWRNORM; |
| 940 | |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 941 | if (tun->dev->reg_state != NETREG_REGISTERED) |
| 942 | mask = POLLERR; |
| 943 | |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 944 | tun_put(tun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | return mask; |
| 946 | } |
| 947 | |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 948 | /* prepad is the amount to reserve at front. len is length after that. |
| 949 | * linear is a hint as to how much to copy (usually headers). */ |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 950 | static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, |
stephen hemminger | 6f7c156 | 2011-06-08 14:33:08 +0000 | [diff] [blame] | 951 | size_t prepad, size_t len, |
| 952 | size_t linear, int noblock) |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 953 | { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 954 | struct sock *sk = tfile->socket.sk; |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 955 | struct sk_buff *skb; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 956 | int err; |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 957 | |
| 958 | /* Under a page? Don't bother with paged skb. */ |
Herbert Xu | 0eca93b | 2009-04-14 02:09:43 -0700 | [diff] [blame] | 959 | if (prepad + len < PAGE_SIZE || !linear) |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 960 | linear = len; |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 961 | |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 962 | skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, |
Eric Dumazet | 28d6427 | 2013-08-08 14:38:47 -0700 | [diff] [blame] | 963 | &err, 0); |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 964 | if (!skb) |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 965 | return ERR_PTR(err); |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 966 | |
| 967 | skb_reserve(skb, prepad); |
| 968 | skb_put(skb, linear); |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 969 | skb->data_len = len - linear; |
| 970 | skb->len += len - linear; |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 971 | |
| 972 | return skb; |
| 973 | } |
| 974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | /* Get packet from user space buffer */ |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 976 | static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, |
| 977 | void *msg_control, const struct iovec *iv, |
| 978 | size_t total_len, size_t count, int noblock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | { |
Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 980 | struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | struct sk_buff *skb; |
Jason Wang | 3dd5c33 | 2013-07-10 13:43:27 +0800 | [diff] [blame] | 982 | size_t len = total_len, align = NET_SKB_PAD, linear; |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 983 | struct virtio_net_hdr gso = { 0 }; |
Jason Wang | 96f8d9e | 2013-11-13 14:00:39 +0800 | [diff] [blame] | 984 | int good_linear; |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 985 | int offset = 0; |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 986 | int copylen; |
| 987 | bool zerocopy = false; |
| 988 | int err; |
Eric Dumazet | 4997442 | 2012-12-12 19:22:57 +0000 | [diff] [blame] | 989 | u32 rxhash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
| 991 | if (!(tun->flags & TUN_NO_PI)) { |
Dan Carpenter | 15718ea | 2013-08-15 15:52:57 +0300 | [diff] [blame] | 992 | if (len < sizeof(pi)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | return -EINVAL; |
Dan Carpenter | 15718ea | 2013-08-15 15:52:57 +0300 | [diff] [blame] | 994 | len -= sizeof(pi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 996 | if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | return -EFAULT; |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 998 | offset += sizeof(pi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1001 | if (tun->flags & TUN_VNET_HDR) { |
Dan Carpenter | 15718ea | 2013-08-15 15:52:57 +0300 | [diff] [blame] | 1002 | if (len < tun->vnet_hdr_sz) |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1003 | return -EINVAL; |
Dan Carpenter | 15718ea | 2013-08-15 15:52:57 +0300 | [diff] [blame] | 1004 | len -= tun->vnet_hdr_sz; |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1005 | |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 1006 | if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso))) |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1007 | return -EFAULT; |
| 1008 | |
Herbert Xu | 4909122 | 2009-06-08 00:20:01 -0700 | [diff] [blame] | 1009 | if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && |
| 1010 | gso.csum_start + gso.csum_offset + 2 > gso.hdr_len) |
| 1011 | gso.hdr_len = gso.csum_start + gso.csum_offset + 2; |
| 1012 | |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1013 | if (gso.hdr_len > len) |
| 1014 | return -EINVAL; |
Michael S. Tsirkin | d9d52b5 | 2010-03-17 17:45:01 +0200 | [diff] [blame] | 1015 | offset += tun->vnet_hdr_sz; |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Rusty Russell | e01bf1c | 2008-04-12 18:49:30 -0700 | [diff] [blame] | 1018 | if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) { |
stephen hemminger | a504b86 | 2011-06-08 14:33:07 +0000 | [diff] [blame] | 1019 | align += NET_IP_ALIGN; |
Herbert Xu | 0eca93b | 2009-04-14 02:09:43 -0700 | [diff] [blame] | 1020 | if (unlikely(len < ETH_HLEN || |
| 1021 | (gso.hdr_len && gso.hdr_len < ETH_HLEN))) |
Rusty Russell | e01bf1c | 2008-04-12 18:49:30 -0700 | [diff] [blame] | 1022 | return -EINVAL; |
| 1023 | } |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1024 | |
Jason Wang | 96f8d9e | 2013-11-13 14:00:39 +0800 | [diff] [blame] | 1025 | good_linear = SKB_MAX_HEAD(align); |
| 1026 | |
Jason Wang | 8852917 | 2013-07-18 10:55:15 +0800 | [diff] [blame] | 1027 | if (msg_control) { |
| 1028 | /* There are 256 bytes to be copied in skb, so there is |
| 1029 | * enough room for skb expand head in case it is used. |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 1030 | * The rest of the buffer is mapped from userspace. |
| 1031 | */ |
Jason Wang | 8852917 | 2013-07-18 10:55:15 +0800 | [diff] [blame] | 1032 | copylen = gso.hdr_len ? gso.hdr_len : GOODCOPY_LEN; |
Jason Wang | 96f8d9e | 2013-11-13 14:00:39 +0800 | [diff] [blame] | 1033 | if (copylen > good_linear) |
| 1034 | copylen = good_linear; |
Jason Wang | 3dd5c33 | 2013-07-10 13:43:27 +0800 | [diff] [blame] | 1035 | linear = copylen; |
Jason Wang | 8852917 | 2013-07-18 10:55:15 +0800 | [diff] [blame] | 1036 | if (iov_pages(iv, offset + copylen, count) <= MAX_SKB_FRAGS) |
| 1037 | zerocopy = true; |
| 1038 | } |
| 1039 | |
| 1040 | if (!zerocopy) { |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 1041 | copylen = len; |
Jason Wang | 96f8d9e | 2013-11-13 14:00:39 +0800 | [diff] [blame] | 1042 | if (gso.hdr_len > good_linear) |
| 1043 | linear = good_linear; |
| 1044 | else |
| 1045 | linear = gso.hdr_len; |
Jason Wang | 3dd5c33 | 2013-07-10 13:43:27 +0800 | [diff] [blame] | 1046 | } |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 1047 | |
Jason Wang | 3dd5c33 | 2013-07-10 13:43:27 +0800 | [diff] [blame] | 1048 | skb = tun_alloc_skb(tfile, align, copylen, linear, noblock); |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1049 | if (IS_ERR(skb)) { |
| 1050 | if (PTR_ERR(skb) != -EAGAIN) |
| 1051 | tun->dev->stats.rx_dropped++; |
| 1052 | return PTR_ERR(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 1055 | if (zerocopy) |
| 1056 | err = zerocopy_sg_from_iovec(skb, iv, offset, count); |
Jason Wang | 8852917 | 2013-07-18 10:55:15 +0800 | [diff] [blame] | 1057 | else { |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 1058 | err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len); |
Jason Wang | 8852917 | 2013-07-18 10:55:15 +0800 | [diff] [blame] | 1059 | if (!err && msg_control) { |
| 1060 | struct ubuf_info *uarg = msg_control; |
| 1061 | uarg->callback(uarg, false); |
| 1062 | } |
| 1063 | } |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 1064 | |
| 1065 | if (err) { |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 1066 | tun->dev->stats.rx_dropped++; |
Dave Jones | 8f22757 | 2006-03-11 18:49:13 -0800 | [diff] [blame] | 1067 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | return -EFAULT; |
Dave Jones | 8f22757 | 2006-03-11 18:49:13 -0800 | [diff] [blame] | 1069 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1071 | if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { |
| 1072 | if (!skb_partial_csum_set(skb, gso.csum_start, |
| 1073 | gso.csum_offset)) { |
| 1074 | tun->dev->stats.rx_frame_errors++; |
| 1075 | kfree_skb(skb); |
| 1076 | return -EINVAL; |
| 1077 | } |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 1078 | } |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1079 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | switch (tun->flags & TUN_TYPE_MASK) { |
| 1081 | case TUN_TUN_DEV: |
Ang Way Chuang | f09f7ee | 2008-06-17 21:10:33 -0700 | [diff] [blame] | 1082 | if (tun->flags & TUN_NO_PI) { |
| 1083 | switch (skb->data[0] & 0xf0) { |
| 1084 | case 0x40: |
| 1085 | pi.proto = htons(ETH_P_IP); |
| 1086 | break; |
| 1087 | case 0x60: |
| 1088 | pi.proto = htons(ETH_P_IPV6); |
| 1089 | break; |
| 1090 | default: |
| 1091 | tun->dev->stats.rx_dropped++; |
| 1092 | kfree_skb(skb); |
| 1093 | return -EINVAL; |
| 1094 | } |
| 1095 | } |
| 1096 | |
Arnaldo Carvalho de Melo | 459a98e | 2007-03-19 15:30:44 -0700 | [diff] [blame] | 1097 | skb_reset_mac_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | skb->protocol = pi.proto; |
Arnaldo Carvalho de Melo | 4c13eb6 | 2007-04-25 17:40:23 -0700 | [diff] [blame] | 1099 | skb->dev = tun->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | break; |
| 1101 | case TUN_TAP_DEV: |
| 1102 | skb->protocol = eth_type_trans(skb, tun->dev); |
| 1103 | break; |
Joe Perches | 6403eab | 2011-06-03 11:51:20 +0000 | [diff] [blame] | 1104 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1106 | if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) { |
| 1107 | pr_debug("GSO!\n"); |
| 1108 | switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { |
| 1109 | case VIRTIO_NET_HDR_GSO_TCPV4: |
Pravin B Shelar | c9af6db | 2013-02-11 09:27:41 +0000 | [diff] [blame] | 1110 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1111 | break; |
| 1112 | case VIRTIO_NET_HDR_GSO_TCPV6: |
Pravin B Shelar | c9af6db | 2013-02-11 09:27:41 +0000 | [diff] [blame] | 1113 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1114 | break; |
Sridhar Samudrala | e36aa25 | 2009-07-14 14:21:04 +0000 | [diff] [blame] | 1115 | case VIRTIO_NET_HDR_GSO_UDP: |
Pravin B Shelar | c9af6db | 2013-02-11 09:27:41 +0000 | [diff] [blame] | 1116 | skb_shinfo(skb)->gso_type = SKB_GSO_UDP; |
Sridhar Samudrala | e36aa25 | 2009-07-14 14:21:04 +0000 | [diff] [blame] | 1117 | break; |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1118 | default: |
| 1119 | tun->dev->stats.rx_frame_errors++; |
| 1120 | kfree_skb(skb); |
| 1121 | return -EINVAL; |
| 1122 | } |
| 1123 | |
| 1124 | if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN) |
Pravin B Shelar | c9af6db | 2013-02-11 09:27:41 +0000 | [diff] [blame] | 1125 | skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1126 | |
| 1127 | skb_shinfo(skb)->gso_size = gso.gso_size; |
| 1128 | if (skb_shinfo(skb)->gso_size == 0) { |
| 1129 | tun->dev->stats.rx_frame_errors++; |
| 1130 | kfree_skb(skb); |
| 1131 | return -EINVAL; |
| 1132 | } |
| 1133 | |
| 1134 | /* Header must be checked, and gso_segs computed. */ |
| 1135 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; |
| 1136 | skb_shinfo(skb)->gso_segs = 0; |
| 1137 | } |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1138 | |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 1139 | /* copy skb_ubuf_info for callback when skb has no error */ |
| 1140 | if (zerocopy) { |
| 1141 | skb_shinfo(skb)->destructor_arg = msg_control; |
| 1142 | skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; |
Pravin B Shelar | c9af6db | 2013-02-11 09:27:41 +0000 | [diff] [blame] | 1143 | skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Eric Dumazet | 76fe458 | 2012-12-17 04:39:20 +0000 | [diff] [blame] | 1146 | skb_reset_network_header(skb); |
Jason Wang | 40893fd | 2013-03-26 23:11:22 +0000 | [diff] [blame] | 1147 | skb_probe_transport_header(skb, 0); |
Jason Wang | 38502af | 2013-03-25 20:19:56 +0000 | [diff] [blame] | 1148 | |
Eric Dumazet | 4997442 | 2012-12-12 19:22:57 +0000 | [diff] [blame] | 1149 | rxhash = skb_get_rxhash(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | netif_rx_ni(skb); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1151 | |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 1152 | tun->dev->stats.rx_packets++; |
| 1153 | tun->dev->stats.rx_bytes += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 1155 | tun_flow_update(tun, rxhash, tfile); |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 1156 | return total_len; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1157 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 1159 | static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv, |
| 1160 | unsigned long count, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | { |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1162 | struct file *file = iocb->ki_filp; |
Herbert Xu | ab46d77 | 2009-02-14 20:46:39 -0800 | [diff] [blame] | 1163 | struct tun_struct *tun = tun_get(file); |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1164 | struct tun_file *tfile = file->private_data; |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1165 | ssize_t result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | |
| 1167 | if (!tun) |
| 1168 | return -EBADFD; |
| 1169 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1170 | tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1172 | result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count), |
| 1173 | count, file->f_flags & O_NONBLOCK); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1174 | |
| 1175 | tun_put(tun); |
| 1176 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | /* Put packet to the user space buffer */ |
stephen hemminger | 6f7c156 | 2011-06-08 14:33:08 +0000 | [diff] [blame] | 1180 | static ssize_t tun_put_user(struct tun_struct *tun, |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1181 | struct tun_file *tfile, |
stephen hemminger | 6f7c156 | 2011-06-08 14:33:08 +0000 | [diff] [blame] | 1182 | struct sk_buff *skb, |
| 1183 | const struct iovec *iv, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | { |
| 1185 | struct tun_pi pi = { 0, skb->protocol }; |
| 1186 | ssize_t total = 0; |
Jason Wang | 6680ec6 | 2013-07-25 13:00:33 +0800 | [diff] [blame] | 1187 | int vlan_offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | |
| 1189 | if (!(tun->flags & TUN_NO_PI)) { |
| 1190 | if ((len -= sizeof(pi)) < 0) |
| 1191 | return -EINVAL; |
| 1192 | |
| 1193 | if (len < skb->len) { |
| 1194 | /* Packet will be striped */ |
| 1195 | pi.flags |= TUN_PKT_STRIP; |
| 1196 | } |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1197 | |
Michael S. Tsirkin | 43b39dc | 2009-04-20 01:25:59 +0000 | [diff] [blame] | 1198 | if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | return -EFAULT; |
| 1200 | total += sizeof(pi); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1201 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1203 | if (tun->flags & TUN_VNET_HDR) { |
| 1204 | struct virtio_net_hdr gso = { 0 }; /* no info leak */ |
Michael S. Tsirkin | d9d52b5 | 2010-03-17 17:45:01 +0200 | [diff] [blame] | 1205 | if ((len -= tun->vnet_hdr_sz) < 0) |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1206 | return -EINVAL; |
| 1207 | |
| 1208 | if (skb_is_gso(skb)) { |
| 1209 | struct skb_shared_info *sinfo = skb_shinfo(skb); |
| 1210 | |
| 1211 | /* This is a hint as to how much should be linear. */ |
| 1212 | gso.hdr_len = skb_headlen(skb); |
| 1213 | gso.gso_size = sinfo->gso_size; |
| 1214 | if (sinfo->gso_type & SKB_GSO_TCPV4) |
| 1215 | gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4; |
| 1216 | else if (sinfo->gso_type & SKB_GSO_TCPV6) |
| 1217 | gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6; |
Sridhar Samudrala | e36aa25 | 2009-07-14 14:21:04 +0000 | [diff] [blame] | 1218 | else if (sinfo->gso_type & SKB_GSO_UDP) |
| 1219 | gso.gso_type = VIRTIO_NET_HDR_GSO_UDP; |
Michael S. Tsirkin | ef3db4a | 2010-07-21 04:32:45 +0000 | [diff] [blame] | 1220 | else { |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1221 | pr_err("unexpected GSO type: " |
Michael S. Tsirkin | ef3db4a | 2010-07-21 04:32:45 +0000 | [diff] [blame] | 1222 | "0x%x, gso_size %d, hdr_len %d\n", |
| 1223 | sinfo->gso_type, gso.gso_size, |
| 1224 | gso.hdr_len); |
| 1225 | print_hex_dump(KERN_ERR, "tun: ", |
| 1226 | DUMP_PREFIX_NONE, |
| 1227 | 16, 1, skb->head, |
| 1228 | min((int)gso.hdr_len, 64), true); |
| 1229 | WARN_ON_ONCE(1); |
| 1230 | return -EINVAL; |
| 1231 | } |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1232 | if (sinfo->gso_type & SKB_GSO_TCP_ECN) |
| 1233 | gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN; |
| 1234 | } else |
| 1235 | gso.gso_type = VIRTIO_NET_HDR_GSO_NONE; |
| 1236 | |
| 1237 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 1238 | gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; |
Michał Mirosław | 55508d6 | 2010-12-14 15:24:08 +0000 | [diff] [blame] | 1239 | gso.csum_start = skb_checksum_start_offset(skb); |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1240 | gso.csum_offset = skb->csum_offset; |
Jason Wang | 10a8d94 | 2011-06-10 00:56:17 +0000 | [diff] [blame] | 1241 | } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) { |
| 1242 | gso.flags = VIRTIO_NET_HDR_F_DATA_VALID; |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1243 | } /* else everything is zero */ |
| 1244 | |
Michael S. Tsirkin | 43b39dc | 2009-04-20 01:25:59 +0000 | [diff] [blame] | 1245 | if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total, |
| 1246 | sizeof(gso)))) |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1247 | return -EFAULT; |
Michael S. Tsirkin | d9d52b5 | 2010-03-17 17:45:01 +0200 | [diff] [blame] | 1248 | total += tun->vnet_hdr_sz; |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1249 | } |
| 1250 | |
Jason Wang | 6680ec6 | 2013-07-25 13:00:33 +0800 | [diff] [blame] | 1251 | if (!vlan_tx_tag_present(skb)) { |
| 1252 | len = min_t(int, skb->len, len); |
| 1253 | } else { |
| 1254 | int copy, ret; |
| 1255 | struct { |
| 1256 | __be16 h_vlan_proto; |
| 1257 | __be16 h_vlan_TCI; |
| 1258 | } veth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | |
Jason Wang | 6680ec6 | 2013-07-25 13:00:33 +0800 | [diff] [blame] | 1260 | veth.h_vlan_proto = skb->vlan_proto; |
| 1261 | veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
Jason Wang | 6680ec6 | 2013-07-25 13:00:33 +0800 | [diff] [blame] | 1263 | vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); |
| 1264 | len = min_t(int, skb->len + VLAN_HLEN, len); |
| 1265 | |
| 1266 | copy = min_t(int, vlan_offset, len); |
| 1267 | ret = skb_copy_datagram_const_iovec(skb, 0, iv, total, copy); |
| 1268 | len -= copy; |
| 1269 | total += copy; |
| 1270 | if (ret || !len) |
| 1271 | goto done; |
| 1272 | |
| 1273 | copy = min_t(int, sizeof(veth), len); |
| 1274 | ret = memcpy_toiovecend(iv, (void *)&veth, total, copy); |
| 1275 | len -= copy; |
| 1276 | total += copy; |
| 1277 | if (ret || !len) |
| 1278 | goto done; |
| 1279 | } |
| 1280 | |
| 1281 | skb_copy_datagram_const_iovec(skb, vlan_offset, iv, total, len); |
| 1282 | total += len; |
| 1283 | |
| 1284 | done: |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 1285 | tun->dev->stats.tx_packets++; |
| 1286 | tun->dev->stats.tx_bytes += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | |
| 1288 | return total; |
| 1289 | } |
| 1290 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1291 | static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, |
Zhi Yong Wu | f96eb74 | 2013-12-07 04:13:06 +0800 | [diff] [blame^] | 1292 | const struct iovec *iv, ssize_t len, int noblock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | DECLARE_WAITQUEUE(wait, current); |
| 1295 | struct sk_buff *skb; |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1296 | ssize_t ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | |
Rami Rosen | 3872baf | 2012-11-25 22:07:41 +0000 | [diff] [blame] | 1298 | tun_debug(KERN_INFO, tun, "tun_do_read\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | |
Amos Kong | 61a5ff1 | 2011-06-09 00:27:10 -0700 | [diff] [blame] | 1300 | if (unlikely(!noblock)) |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1301 | add_wait_queue(&tfile->wq.wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | while (len) { |
Michael S. Tsirkin | 5c0c52c | 2013-10-06 21:25:12 +0300 | [diff] [blame] | 1303 | if (unlikely(!noblock)) |
| 1304 | current->state = TASK_INTERRUPTIBLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | |
| 1306 | /* Read frames from the queue */ |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1307 | if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) { |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1308 | if (noblock) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | ret = -EAGAIN; |
| 1310 | break; |
| 1311 | } |
| 1312 | if (signal_pending(current)) { |
| 1313 | ret = -ERESTARTSYS; |
| 1314 | break; |
| 1315 | } |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 1316 | if (tun->dev->reg_state != NETREG_REGISTERED) { |
| 1317 | ret = -EIO; |
| 1318 | break; |
| 1319 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | |
| 1321 | /* Nothing to read, let's sleep */ |
| 1322 | schedule(); |
| 1323 | continue; |
| 1324 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1326 | ret = tun_put_user(tun, tfile, skb, iv, len); |
Max Krasnyansky | f271b2c | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 1327 | kfree_skb(skb); |
| 1328 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Michael S. Tsirkin | 5c0c52c | 2013-10-06 21:25:12 +0300 | [diff] [blame] | 1331 | if (unlikely(!noblock)) { |
| 1332 | current->state = TASK_RUNNING; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1333 | remove_wait_queue(&tfile->wq.wait, &wait); |
Michael S. Tsirkin | 5c0c52c | 2013-10-06 21:25:12 +0300 | [diff] [blame] | 1334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1336 | return ret; |
| 1337 | } |
| 1338 | |
| 1339 | static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv, |
| 1340 | unsigned long count, loff_t pos) |
| 1341 | { |
| 1342 | struct file *file = iocb->ki_filp; |
| 1343 | struct tun_file *tfile = file->private_data; |
| 1344 | struct tun_struct *tun = __tun_get(tfile); |
| 1345 | ssize_t len, ret; |
| 1346 | |
| 1347 | if (!tun) |
| 1348 | return -EBADFD; |
| 1349 | len = iov_length(iv, count); |
| 1350 | if (len < 0) { |
| 1351 | ret = -EINVAL; |
| 1352 | goto out; |
| 1353 | } |
| 1354 | |
Zhi Yong Wu | f96eb74 | 2013-12-07 04:13:06 +0800 | [diff] [blame^] | 1355 | ret = tun_do_read(tun, tfile, iv, len, |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1356 | file->f_flags & O_NONBLOCK); |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1357 | ret = min_t(ssize_t, ret, len); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1358 | out: |
| 1359 | tun_put(tun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | return ret; |
| 1361 | } |
| 1362 | |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 1363 | static void tun_free_netdev(struct net_device *dev) |
| 1364 | { |
| 1365 | struct tun_struct *tun = netdev_priv(dev); |
| 1366 | |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 1367 | BUG_ON(!(list_empty(&tun->disabled))); |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 1368 | tun_flow_uninit(tun); |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 1369 | security_tun_dev_free_security(tun->security); |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 1370 | free_netdev(dev); |
| 1371 | } |
| 1372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | static void tun_setup(struct net_device *dev) |
| 1374 | { |
| 1375 | struct tun_struct *tun = netdev_priv(dev); |
| 1376 | |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1377 | tun->owner = INVALID_UID; |
| 1378 | tun->group = INVALID_GID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | dev->ethtool_ops = &tun_ethtool_ops; |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 1381 | dev->destructor = tun_free_netdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | } |
| 1383 | |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 1384 | /* Trivial set of netlink ops to allow deleting tun or tap |
| 1385 | * device with netlink. |
| 1386 | */ |
| 1387 | static int tun_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 1388 | { |
| 1389 | return -EINVAL; |
| 1390 | } |
| 1391 | |
| 1392 | static struct rtnl_link_ops tun_link_ops __read_mostly = { |
| 1393 | .kind = DRV_NAME, |
| 1394 | .priv_size = sizeof(struct tun_struct), |
| 1395 | .setup = tun_setup, |
| 1396 | .validate = tun_validate, |
| 1397 | }; |
| 1398 | |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1399 | static void tun_sock_write_space(struct sock *sk) |
| 1400 | { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1401 | struct tun_file *tfile; |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 1402 | wait_queue_head_t *wqueue; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1403 | |
| 1404 | if (!sock_writeable(sk)) |
| 1405 | return; |
| 1406 | |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1407 | if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags)) |
| 1408 | return; |
| 1409 | |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 1410 | wqueue = sk_sleep(sk); |
| 1411 | if (wqueue && waitqueue_active(wqueue)) |
| 1412 | wake_up_interruptible_sync_poll(wqueue, POLLOUT | |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1413 | POLLWRNORM | POLLWRBAND); |
Herbert Xu | c722c62 | 2009-06-03 21:45:55 -0700 | [diff] [blame] | 1414 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1415 | tfile = container_of(sk, struct tun_file, sk); |
| 1416 | kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1417 | } |
| 1418 | |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1419 | static int tun_sendmsg(struct kiocb *iocb, struct socket *sock, |
| 1420 | struct msghdr *m, size_t total_len) |
| 1421 | { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1422 | int ret; |
| 1423 | struct tun_file *tfile = container_of(sock, struct tun_file, socket); |
| 1424 | struct tun_struct *tun = __tun_get(tfile); |
| 1425 | |
| 1426 | if (!tun) |
| 1427 | return -EBADFD; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1428 | ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len, |
| 1429 | m->msg_iovlen, m->msg_flags & MSG_DONTWAIT); |
| 1430 | tun_put(tun); |
| 1431 | return ret; |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | static int tun_recvmsg(struct kiocb *iocb, struct socket *sock, |
| 1435 | struct msghdr *m, size_t total_len, |
| 1436 | int flags) |
| 1437 | { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1438 | struct tun_file *tfile = container_of(sock, struct tun_file, socket); |
| 1439 | struct tun_struct *tun = __tun_get(tfile); |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1440 | int ret; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1441 | |
| 1442 | if (!tun) |
| 1443 | return -EBADFD; |
| 1444 | |
Richard Cochran | eda2977 | 2013-07-19 19:40:10 +0200 | [diff] [blame] | 1445 | if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) { |
Gao feng | 3811ae7 | 2013-04-24 21:59:23 +0000 | [diff] [blame] | 1446 | ret = -EINVAL; |
| 1447 | goto out; |
| 1448 | } |
Richard Cochran | eda2977 | 2013-07-19 19:40:10 +0200 | [diff] [blame] | 1449 | if (flags & MSG_ERRQUEUE) { |
| 1450 | ret = sock_recv_errqueue(sock->sk, m, total_len, |
| 1451 | SOL_PACKET, TUN_TX_TIMESTAMP); |
| 1452 | goto out; |
| 1453 | } |
Zhi Yong Wu | f96eb74 | 2013-12-07 04:13:06 +0800 | [diff] [blame^] | 1454 | ret = tun_do_read(tun, tfile, m->msg_iov, total_len, |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1455 | flags & MSG_DONTWAIT); |
| 1456 | if (ret > total_len) { |
| 1457 | m->msg_flags |= MSG_TRUNC; |
| 1458 | ret = flags & MSG_TRUNC ? ret : total_len; |
| 1459 | } |
Gao feng | 3811ae7 | 2013-04-24 21:59:23 +0000 | [diff] [blame] | 1460 | out: |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1461 | tun_put(tun); |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1462 | return ret; |
| 1463 | } |
| 1464 | |
Stanislav Kinsbursky | 1ab5ecb | 2012-03-12 02:59:41 +0000 | [diff] [blame] | 1465 | static int tun_release(struct socket *sock) |
| 1466 | { |
| 1467 | if (sock->sk) |
| 1468 | sock_put(sock->sk); |
| 1469 | return 0; |
| 1470 | } |
| 1471 | |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1472 | /* Ops structure to mimic raw sockets with tun */ |
| 1473 | static const struct proto_ops tun_socket_ops = { |
| 1474 | .sendmsg = tun_sendmsg, |
| 1475 | .recvmsg = tun_recvmsg, |
Stanislav Kinsbursky | 1ab5ecb | 2012-03-12 02:59:41 +0000 | [diff] [blame] | 1476 | .release = tun_release, |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1477 | }; |
| 1478 | |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1479 | static struct proto tun_proto = { |
| 1480 | .name = "tun", |
| 1481 | .owner = THIS_MODULE, |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1482 | .obj_size = sizeof(struct tun_file), |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1483 | }; |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 1484 | |
David Woodhouse | 980c9e8 | 2009-05-09 22:54:21 -0700 | [diff] [blame] | 1485 | static int tun_flags(struct tun_struct *tun) |
| 1486 | { |
| 1487 | int flags = 0; |
| 1488 | |
| 1489 | if (tun->flags & TUN_TUN_DEV) |
| 1490 | flags |= IFF_TUN; |
| 1491 | else |
| 1492 | flags |= IFF_TAP; |
| 1493 | |
| 1494 | if (tun->flags & TUN_NO_PI) |
| 1495 | flags |= IFF_NO_PI; |
| 1496 | |
Michael S. Tsirkin | 5d09710 | 2012-12-03 10:07:14 +0000 | [diff] [blame] | 1497 | /* This flag has no real effect. We track the value for backwards |
| 1498 | * compatibility. |
| 1499 | */ |
David Woodhouse | 980c9e8 | 2009-05-09 22:54:21 -0700 | [diff] [blame] | 1500 | if (tun->flags & TUN_ONE_QUEUE) |
| 1501 | flags |= IFF_ONE_QUEUE; |
| 1502 | |
| 1503 | if (tun->flags & TUN_VNET_HDR) |
| 1504 | flags |= IFF_VNET_HDR; |
| 1505 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 1506 | if (tun->flags & TUN_TAP_MQ) |
| 1507 | flags |= IFF_MULTI_QUEUE; |
| 1508 | |
Pavel Emelyanov | 274038f | 2013-06-11 14:41:24 +0400 | [diff] [blame] | 1509 | if (tun->flags & TUN_PERSIST) |
| 1510 | flags |= IFF_PERSIST; |
| 1511 | |
David Woodhouse | 980c9e8 | 2009-05-09 22:54:21 -0700 | [diff] [blame] | 1512 | return flags; |
| 1513 | } |
| 1514 | |
| 1515 | static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr, |
| 1516 | char *buf) |
| 1517 | { |
| 1518 | struct tun_struct *tun = netdev_priv(to_net_dev(dev)); |
| 1519 | return sprintf(buf, "0x%x\n", tun_flags(tun)); |
| 1520 | } |
| 1521 | |
| 1522 | static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr, |
| 1523 | char *buf) |
| 1524 | { |
| 1525 | struct tun_struct *tun = netdev_priv(to_net_dev(dev)); |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1526 | return uid_valid(tun->owner)? |
| 1527 | sprintf(buf, "%u\n", |
| 1528 | from_kuid_munged(current_user_ns(), tun->owner)): |
| 1529 | sprintf(buf, "-1\n"); |
David Woodhouse | 980c9e8 | 2009-05-09 22:54:21 -0700 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr, |
| 1533 | char *buf) |
| 1534 | { |
| 1535 | struct tun_struct *tun = netdev_priv(to_net_dev(dev)); |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1536 | return gid_valid(tun->group) ? |
| 1537 | sprintf(buf, "%u\n", |
| 1538 | from_kgid_munged(current_user_ns(), tun->group)): |
| 1539 | sprintf(buf, "-1\n"); |
David Woodhouse | 980c9e8 | 2009-05-09 22:54:21 -0700 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL); |
| 1543 | static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL); |
| 1544 | static DEVICE_ATTR(group, 0444, tun_show_group, NULL); |
| 1545 | |
Pavel Emelyanov | d647a59 | 2008-04-16 00:41:16 -0700 | [diff] [blame] | 1546 | static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | { |
| 1548 | struct tun_struct *tun; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1549 | struct tun_file *tfile = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | struct net_device *dev; |
| 1551 | int err; |
| 1552 | |
Jason Wang | 7c0c3b1 | 2013-01-11 16:59:33 +0000 | [diff] [blame] | 1553 | if (tfile->detached) |
| 1554 | return -EINVAL; |
| 1555 | |
Eric W. Biederman | 74a3e5a | 2009-01-20 10:56:20 +0000 | [diff] [blame] | 1556 | dev = __dev_get_by_name(net, ifr->ifr_name); |
| 1557 | if (dev) { |
David Woodhouse | f85ba78 | 2009-04-27 03:23:54 -0700 | [diff] [blame] | 1558 | if (ifr->ifr_flags & IFF_TUN_EXCL) |
| 1559 | return -EBUSY; |
Eric W. Biederman | 74a3e5a | 2009-01-20 10:56:20 +0000 | [diff] [blame] | 1560 | if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) |
| 1561 | tun = netdev_priv(dev); |
| 1562 | else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) |
| 1563 | tun = netdev_priv(dev); |
| 1564 | else |
| 1565 | return -EINVAL; |
| 1566 | |
Jason Wang | 8e6d91a | 2013-05-28 18:32:11 +0000 | [diff] [blame] | 1567 | if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) != |
| 1568 | !!(tun->flags & TUN_TAP_MQ)) |
| 1569 | return -EINVAL; |
| 1570 | |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 1571 | if (tun_not_capable(tun)) |
Paul Moore | 2b980db | 2009-08-28 18:12:43 -0400 | [diff] [blame] | 1572 | return -EPERM; |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 1573 | err = security_tun_dev_open(tun->security); |
Paul Moore | 2b980db | 2009-08-28 18:12:43 -0400 | [diff] [blame] | 1574 | if (err < 0) |
| 1575 | return err; |
| 1576 | |
Pavel Emelyanov | 849c9b6 | 2013-08-21 14:32:21 +0400 | [diff] [blame] | 1577 | err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER); |
Eric W. Biederman | a7385ba | 2009-01-20 10:57:48 +0000 | [diff] [blame] | 1578 | if (err < 0) |
| 1579 | return err; |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 1580 | |
| 1581 | if (tun->flags & TUN_TAP_MQ && |
Jason Wang | e8dbad6 | 2013-04-22 20:40:39 +0000 | [diff] [blame] | 1582 | (tun->numqueues + tun->numdisabled > 1)) { |
| 1583 | /* One or more queue has already been attached, no need |
| 1584 | * to initialize the device again. |
| 1585 | */ |
| 1586 | return 0; |
| 1587 | } |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1588 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | else { |
| 1590 | char *name; |
| 1591 | unsigned long flags = 0; |
Jason Wang | edfb6a1 | 2013-01-23 03:59:12 +0000 | [diff] [blame] | 1592 | int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ? |
| 1593 | MAX_TAP_QUEUES : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | |
Eric W. Biederman | c260b77 | 2012-11-18 21:34:11 +0000 | [diff] [blame] | 1595 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
David Woodhouse | ca6bb5d | 2006-06-22 16:07:52 -0700 | [diff] [blame] | 1596 | return -EPERM; |
Paul Moore | 2b980db | 2009-08-28 18:12:43 -0400 | [diff] [blame] | 1597 | err = security_tun_dev_create(); |
| 1598 | if (err < 0) |
| 1599 | return err; |
David Woodhouse | ca6bb5d | 2006-06-22 16:07:52 -0700 | [diff] [blame] | 1600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | /* Set dev type */ |
| 1602 | if (ifr->ifr_flags & IFF_TUN) { |
| 1603 | /* TUN device */ |
| 1604 | flags |= TUN_TUN_DEV; |
| 1605 | name = "tun%d"; |
| 1606 | } else if (ifr->ifr_flags & IFF_TAP) { |
| 1607 | /* TAP device */ |
| 1608 | flags |= TUN_TAP_DEV; |
| 1609 | name = "tap%d"; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1610 | } else |
Kusanagi Kouichi | 36989b9 | 2009-09-16 21:36:13 +0000 | [diff] [blame] | 1611 | return -EINVAL; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | if (*ifr->ifr_name) |
| 1614 | name = ifr->ifr_name; |
| 1615 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 1616 | dev = alloc_netdev_mqs(sizeof(struct tun_struct), name, |
Jason Wang | edfb6a1 | 2013-01-23 03:59:12 +0000 | [diff] [blame] | 1617 | tun_setup, queues, queues); |
| 1618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | if (!dev) |
| 1620 | return -ENOMEM; |
| 1621 | |
Pavel Emelyanov | fc54c65 | 2008-04-16 00:41:53 -0700 | [diff] [blame] | 1622 | dev_net_set(dev, net); |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 1623 | dev->rtnl_link_ops = &tun_link_ops; |
Pavel Emelyanov | fb7589a | 2013-08-21 14:31:38 +0400 | [diff] [blame] | 1624 | dev->ifindex = tfile->ifindex; |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 1625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | tun = netdev_priv(dev); |
| 1627 | tun->dev = dev; |
| 1628 | tun->flags = flags; |
Max Krasnyansky | f271b2c | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 1629 | tun->txflt.count = 0; |
Michael S. Tsirkin | d9d52b5 | 2010-03-17 17:45:01 +0200 | [diff] [blame] | 1630 | tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1632 | tun->filter_attached = false; |
| 1633 | tun->sndbuf = tfile->socket.sk->sk_sndbuf; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1634 | |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 1635 | spin_lock_init(&tun->lock); |
| 1636 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 1637 | err = security_tun_dev_alloc_security(&tun->security); |
| 1638 | if (err < 0) |
| 1639 | goto err_free_dev; |
Paul Moore | 2b980db | 2009-08-28 18:12:43 -0400 | [diff] [blame] | 1640 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | tun_net_init(dev); |
Pavel Emelyanov | 944a137 | 2013-06-11 17:01:08 +0400 | [diff] [blame] | 1642 | tun_flow_init(tun); |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 1643 | |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 1644 | dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | |
Jason Wang | 6680ec6 | 2013-07-25 13:00:33 +0800 | [diff] [blame] | 1645 | TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX | |
| 1646 | NETIF_F_HW_VLAN_STAG_TX; |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 1647 | dev->features = dev->hw_features; |
Jason Wang | c031799 | 2013-04-10 23:32:22 +0000 | [diff] [blame] | 1648 | dev->vlan_features = dev->features; |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 1649 | |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 1650 | INIT_LIST_HEAD(&tun->disabled); |
Pavel Emelyanov | 849c9b6 | 2013-08-21 14:32:21 +0400 | [diff] [blame] | 1651 | err = tun_attach(tun, file, false); |
Jason Wang | eb0fb36 | 2012-12-02 17:19:45 +0000 | [diff] [blame] | 1652 | if (err < 0) |
Jason Wang | 662ca43 | 2013-09-11 18:09:48 +0800 | [diff] [blame] | 1653 | goto err_free_flow; |
Jason Wang | eb0fb36 | 2012-12-02 17:19:45 +0000 | [diff] [blame] | 1654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | err = register_netdevice(tun->dev); |
| 1656 | if (err < 0) |
Jason Wang | 662ca43 | 2013-09-11 18:09:48 +0800 | [diff] [blame] | 1657 | goto err_detach; |
Herbert Xu | 9c3fea6 | 2009-04-18 14:15:52 +0000 | [diff] [blame] | 1658 | |
David Woodhouse | 980c9e8 | 2009-05-09 22:54:21 -0700 | [diff] [blame] | 1659 | if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) || |
| 1660 | device_create_file(&tun->dev->dev, &dev_attr_owner) || |
| 1661 | device_create_file(&tun->dev->dev, &dev_attr_group)) |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1662 | pr_err("Failed to create tun sysfs files\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | } |
| 1664 | |
Michael S. Tsirkin | af668b3 | 2013-01-28 00:38:02 +0000 | [diff] [blame] | 1665 | netif_carrier_on(tun->dev); |
| 1666 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1667 | tun_debug(KERN_INFO, tun, "tun_set_iff\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | |
| 1669 | if (ifr->ifr_flags & IFF_NO_PI) |
| 1670 | tun->flags |= TUN_NO_PI; |
Nathaniel Filardo | a26af1e | 2008-02-05 03:05:07 -0800 | [diff] [blame] | 1671 | else |
| 1672 | tun->flags &= ~TUN_NO_PI; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | |
Michael S. Tsirkin | 5d09710 | 2012-12-03 10:07:14 +0000 | [diff] [blame] | 1674 | /* This flag has no real effect. We track the value for backwards |
| 1675 | * compatibility. |
| 1676 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | if (ifr->ifr_flags & IFF_ONE_QUEUE) |
| 1678 | tun->flags |= TUN_ONE_QUEUE; |
Nathaniel Filardo | a26af1e | 2008-02-05 03:05:07 -0800 | [diff] [blame] | 1679 | else |
| 1680 | tun->flags &= ~TUN_ONE_QUEUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1682 | if (ifr->ifr_flags & IFF_VNET_HDR) |
| 1683 | tun->flags |= TUN_VNET_HDR; |
| 1684 | else |
| 1685 | tun->flags &= ~TUN_VNET_HDR; |
| 1686 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 1687 | if (ifr->ifr_flags & IFF_MULTI_QUEUE) |
| 1688 | tun->flags |= TUN_TAP_MQ; |
| 1689 | else |
| 1690 | tun->flags &= ~TUN_TAP_MQ; |
| 1691 | |
Max Krasnyansky | e35259a | 2008-07-10 16:59:11 -0700 | [diff] [blame] | 1692 | /* Make sure persistent devices do not get stuck in |
| 1693 | * xoff state. |
| 1694 | */ |
| 1695 | if (netif_running(tun->dev)) |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 1696 | netif_tx_wake_all_queues(tun->dev); |
Max Krasnyansky | e35259a | 2008-07-10 16:59:11 -0700 | [diff] [blame] | 1697 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | strcpy(ifr->ifr_name, tun->dev->name); |
| 1699 | return 0; |
| 1700 | |
Jason Wang | 662ca43 | 2013-09-11 18:09:48 +0800 | [diff] [blame] | 1701 | err_detach: |
| 1702 | tun_detach_all(dev); |
| 1703 | err_free_flow: |
| 1704 | tun_flow_uninit(tun); |
| 1705 | security_tun_dev_free_security(tun->security); |
| 1706 | err_free_dev: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | free_netdev(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | return err; |
| 1709 | } |
| 1710 | |
Rami Rosen | 9ce99cf | 2012-11-23 03:58:10 +0000 | [diff] [blame] | 1711 | static void tun_get_iff(struct net *net, struct tun_struct *tun, |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 1712 | struct ifreq *ifr) |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 1713 | { |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1714 | tun_debug(KERN_INFO, tun, "tun_get_iff\n"); |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 1715 | |
| 1716 | strcpy(ifr->ifr_name, tun->dev->name); |
| 1717 | |
David Woodhouse | 980c9e8 | 2009-05-09 22:54:21 -0700 | [diff] [blame] | 1718 | ifr->ifr_flags = tun_flags(tun); |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 1719 | |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 1720 | } |
| 1721 | |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 1722 | /* This is like a cut-down ethtool ops, except done via tun fd so no |
| 1723 | * privs required. */ |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 1724 | static int set_offload(struct tun_struct *tun, unsigned long arg) |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 1725 | { |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1726 | netdev_features_t features = 0; |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 1727 | |
| 1728 | if (arg & TUN_F_CSUM) { |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 1729 | features |= NETIF_F_HW_CSUM; |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 1730 | arg &= ~TUN_F_CSUM; |
| 1731 | |
| 1732 | if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { |
| 1733 | if (arg & TUN_F_TSO_ECN) { |
| 1734 | features |= NETIF_F_TSO_ECN; |
| 1735 | arg &= ~TUN_F_TSO_ECN; |
| 1736 | } |
| 1737 | if (arg & TUN_F_TSO4) |
| 1738 | features |= NETIF_F_TSO; |
| 1739 | if (arg & TUN_F_TSO6) |
| 1740 | features |= NETIF_F_TSO6; |
| 1741 | arg &= ~(TUN_F_TSO4|TUN_F_TSO6); |
| 1742 | } |
Sridhar Samudrala | e36aa25 | 2009-07-14 14:21:04 +0000 | [diff] [blame] | 1743 | |
| 1744 | if (arg & TUN_F_UFO) { |
| 1745 | features |= NETIF_F_UFO; |
| 1746 | arg &= ~TUN_F_UFO; |
| 1747 | } |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | /* This gives the user a way to test for new features in future by |
| 1751 | * trying to set them. */ |
| 1752 | if (arg) |
| 1753 | return -EINVAL; |
| 1754 | |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 1755 | tun->set_features = features; |
| 1756 | netdev_update_features(tun->dev); |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 1757 | |
| 1758 | return 0; |
| 1759 | } |
| 1760 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 1761 | static void tun_detach_filter(struct tun_struct *tun, int n) |
| 1762 | { |
| 1763 | int i; |
| 1764 | struct tun_file *tfile; |
| 1765 | |
| 1766 | for (i = 0; i < n; i++) { |
Jason Wang | b8deabd | 2013-01-11 16:59:32 +0000 | [diff] [blame] | 1767 | tfile = rtnl_dereference(tun->tfiles[i]); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 1768 | sk_detach_filter(tfile->socket.sk); |
| 1769 | } |
| 1770 | |
| 1771 | tun->filter_attached = false; |
| 1772 | } |
| 1773 | |
| 1774 | static int tun_attach_filter(struct tun_struct *tun) |
| 1775 | { |
| 1776 | int i, ret = 0; |
| 1777 | struct tun_file *tfile; |
| 1778 | |
| 1779 | for (i = 0; i < tun->numqueues; i++) { |
Jason Wang | b8deabd | 2013-01-11 16:59:32 +0000 | [diff] [blame] | 1780 | tfile = rtnl_dereference(tun->tfiles[i]); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 1781 | ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); |
| 1782 | if (ret) { |
| 1783 | tun_detach_filter(tun, i); |
| 1784 | return ret; |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | tun->filter_attached = true; |
| 1789 | return ret; |
| 1790 | } |
| 1791 | |
| 1792 | static void tun_set_sndbuf(struct tun_struct *tun) |
| 1793 | { |
| 1794 | struct tun_file *tfile; |
| 1795 | int i; |
| 1796 | |
| 1797 | for (i = 0; i < tun->numqueues; i++) { |
Jason Wang | b8deabd | 2013-01-11 16:59:32 +0000 | [diff] [blame] | 1798 | tfile = rtnl_dereference(tun->tfiles[i]); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 1799 | tfile->socket.sk->sk_sndbuf = tun->sndbuf; |
| 1800 | } |
| 1801 | } |
| 1802 | |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 1803 | static int tun_set_queue(struct file *file, struct ifreq *ifr) |
| 1804 | { |
| 1805 | struct tun_file *tfile = file->private_data; |
| 1806 | struct tun_struct *tun; |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 1807 | int ret = 0; |
| 1808 | |
| 1809 | rtnl_lock(); |
| 1810 | |
| 1811 | if (ifr->ifr_flags & IFF_ATTACH_QUEUE) { |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 1812 | tun = tfile->detached; |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 1813 | if (!tun) { |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 1814 | ret = -EINVAL; |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 1815 | goto unlock; |
| 1816 | } |
| 1817 | ret = security_tun_dev_attach_queue(tun->security); |
| 1818 | if (ret < 0) |
| 1819 | goto unlock; |
Pavel Emelyanov | 849c9b6 | 2013-08-21 14:32:21 +0400 | [diff] [blame] | 1820 | ret = tun_attach(tun, file, false); |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 1821 | } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) { |
Jason Wang | b8deabd | 2013-01-11 16:59:32 +0000 | [diff] [blame] | 1822 | tun = rtnl_dereference(tfile->tun); |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 1823 | if (!tun || !(tun->flags & TUN_TAP_MQ) || tfile->detached) |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 1824 | ret = -EINVAL; |
| 1825 | else |
| 1826 | __tun_detach(tfile, false); |
| 1827 | } else |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 1828 | ret = -EINVAL; |
| 1829 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 1830 | unlock: |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 1831 | rtnl_unlock(); |
| 1832 | return ret; |
| 1833 | } |
| 1834 | |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 1835 | static long __tun_chr_ioctl(struct file *file, unsigned int cmd, |
| 1836 | unsigned long arg, int ifreq_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | { |
Eric W. Biederman | 36b50ba | 2009-01-20 11:01:48 +0000 | [diff] [blame] | 1838 | struct tun_file *tfile = file->private_data; |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1839 | struct tun_struct *tun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | void __user* argp = (void __user*)arg; |
| 1841 | struct ifreq ifr; |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1842 | kuid_t owner; |
| 1843 | kgid_t group; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1844 | int sndbuf; |
Michael S. Tsirkin | d9d52b5 | 2010-03-17 17:45:01 +0200 | [diff] [blame] | 1845 | int vnet_hdr_sz; |
Pavel Emelyanov | fb7589a | 2013-08-21 14:31:38 +0400 | [diff] [blame] | 1846 | unsigned int ifindex; |
Max Krasnyansky | f271b2c | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 1847 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 1849 | if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) { |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 1850 | if (copy_from_user(&ifr, argp, ifreq_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | return -EFAULT; |
David S. Miller | 8bbb181 | 2012-07-30 14:52:48 -0700 | [diff] [blame] | 1852 | } else { |
Mathias Krause | a117dac | 2012-07-29 19:45:14 +0000 | [diff] [blame] | 1853 | memset(&ifr, 0, sizeof(ifr)); |
David S. Miller | 8bbb181 | 2012-07-30 14:52:48 -0700 | [diff] [blame] | 1854 | } |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1855 | if (cmd == TUNGETFEATURES) { |
| 1856 | /* Currently this just means: "what IFF flags are valid?". |
| 1857 | * This is needed because we never checked for invalid flags on |
| 1858 | * TUNSETIFF. */ |
| 1859 | return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 1860 | IFF_VNET_HDR | IFF_MULTI_QUEUE, |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1861 | (unsigned int __user*)argp); |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 1862 | } else if (cmd == TUNSETQUEUE) |
| 1863 | return tun_set_queue(file, &ifr); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1864 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 1865 | ret = 0; |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 1866 | rtnl_lock(); |
| 1867 | |
Eric W. Biederman | 36b50ba | 2009-01-20 11:01:48 +0000 | [diff] [blame] | 1868 | tun = __tun_get(tfile); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | if (cmd == TUNSETIFF && !tun) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | ifr.ifr_name[IFNAMSIZ-1] = '\0'; |
| 1871 | |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 1872 | ret = tun_set_iff(tfile->net, file, &ifr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 1874 | if (ret) |
| 1875 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 1877 | if (copy_to_user(argp, &ifr, ifreq_len)) |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 1878 | ret = -EFAULT; |
| 1879 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | } |
Pavel Emelyanov | fb7589a | 2013-08-21 14:31:38 +0400 | [diff] [blame] | 1881 | if (cmd == TUNSETIFINDEX) { |
| 1882 | ret = -EPERM; |
| 1883 | if (tun) |
| 1884 | goto unlock; |
| 1885 | |
| 1886 | ret = -EFAULT; |
| 1887 | if (copy_from_user(&ifindex, argp, sizeof(ifindex))) |
| 1888 | goto unlock; |
| 1889 | |
| 1890 | ret = 0; |
| 1891 | tfile->ifindex = ifindex; |
| 1892 | goto unlock; |
| 1893 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 1895 | ret = -EBADFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | if (!tun) |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 1897 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | |
Jason Wang | 1e58833 | 2012-10-31 19:45:56 +0000 | [diff] [blame] | 1899 | tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1901 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | switch (cmd) { |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 1903 | case TUNGETIFF: |
Rami Rosen | 9ce99cf | 2012-11-23 03:58:10 +0000 | [diff] [blame] | 1904 | tun_get_iff(current->nsproxy->net_ns, tun, &ifr); |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 1905 | |
Pavel Emelyanov | 3d407a8 | 2013-08-21 14:32:00 +0400 | [diff] [blame] | 1906 | if (tfile->detached) |
| 1907 | ifr.ifr_flags |= IFF_DETACH_QUEUE; |
Pavel Emelyanov | 849c9b6 | 2013-08-21 14:32:21 +0400 | [diff] [blame] | 1908 | if (!tfile->socket.sk->sk_filter) |
| 1909 | ifr.ifr_flags |= IFF_NOFILTER; |
Pavel Emelyanov | 3d407a8 | 2013-08-21 14:32:00 +0400 | [diff] [blame] | 1910 | |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 1911 | if (copy_to_user(argp, &ifr, ifreq_len)) |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1912 | ret = -EFAULT; |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 1913 | break; |
| 1914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | case TUNSETNOCSUM: |
| 1916 | /* Disable/Enable checksum */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 1918 | /* [unimplemented] */ |
| 1919 | tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n", |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1920 | arg ? "disabled" : "enabled"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | break; |
| 1922 | |
| 1923 | case TUNSETPERSIST: |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1924 | /* Disable/Enable persist mode. Keep an extra reference to the |
| 1925 | * module to prevent the module being unprobed. |
| 1926 | */ |
Jason Wang | dd38bd8 | 2013-01-11 16:59:34 +0000 | [diff] [blame] | 1927 | if (arg && !(tun->flags & TUN_PERSIST)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | tun->flags |= TUN_PERSIST; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1929 | __module_get(THIS_MODULE); |
Jason Wang | dd38bd8 | 2013-01-11 16:59:34 +0000 | [diff] [blame] | 1930 | } |
| 1931 | if (!arg && (tun->flags & TUN_PERSIST)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | tun->flags &= ~TUN_PERSIST; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1933 | module_put(THIS_MODULE); |
| 1934 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1936 | tun_debug(KERN_INFO, tun, "persist %s\n", |
| 1937 | arg ? "enabled" : "disabled"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | break; |
| 1939 | |
| 1940 | case TUNSETOWNER: |
| 1941 | /* Set owner of the device */ |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1942 | owner = make_kuid(current_user_ns(), arg); |
| 1943 | if (!uid_valid(owner)) { |
| 1944 | ret = -EINVAL; |
| 1945 | break; |
| 1946 | } |
| 1947 | tun->owner = owner; |
Jason Wang | 1e58833 | 2012-10-31 19:45:56 +0000 | [diff] [blame] | 1948 | tun_debug(KERN_INFO, tun, "owner set to %u\n", |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1949 | from_kuid(&init_user_ns, tun->owner)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | break; |
| 1951 | |
Guido Guenther | 8c64462 | 2007-07-02 22:50:25 -0700 | [diff] [blame] | 1952 | case TUNSETGROUP: |
| 1953 | /* Set group of the device */ |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1954 | group = make_kgid(current_user_ns(), arg); |
| 1955 | if (!gid_valid(group)) { |
| 1956 | ret = -EINVAL; |
| 1957 | break; |
| 1958 | } |
| 1959 | tun->group = group; |
Jason Wang | 1e58833 | 2012-10-31 19:45:56 +0000 | [diff] [blame] | 1960 | tun_debug(KERN_INFO, tun, "group set to %u\n", |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1961 | from_kgid(&init_user_ns, tun->group)); |
Guido Guenther | 8c64462 | 2007-07-02 22:50:25 -0700 | [diff] [blame] | 1962 | break; |
| 1963 | |
Mike Kershaw | ff4cc3a | 2005-09-01 17:40:05 -0700 | [diff] [blame] | 1964 | case TUNSETLINK: |
| 1965 | /* Only allow setting the type when the interface is down */ |
| 1966 | if (tun->dev->flags & IFF_UP) { |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1967 | tun_debug(KERN_INFO, tun, |
| 1968 | "Linktype set failed because interface is up\n"); |
David S. Miller | 48abfe0 | 2008-04-23 19:37:58 -0700 | [diff] [blame] | 1969 | ret = -EBUSY; |
Mike Kershaw | ff4cc3a | 2005-09-01 17:40:05 -0700 | [diff] [blame] | 1970 | } else { |
| 1971 | tun->dev->type = (int) arg; |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1972 | tun_debug(KERN_INFO, tun, "linktype set to %d\n", |
| 1973 | tun->dev->type); |
David S. Miller | 48abfe0 | 2008-04-23 19:37:58 -0700 | [diff] [blame] | 1974 | ret = 0; |
Mike Kershaw | ff4cc3a | 2005-09-01 17:40:05 -0700 | [diff] [blame] | 1975 | } |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1976 | break; |
Mike Kershaw | ff4cc3a | 2005-09-01 17:40:05 -0700 | [diff] [blame] | 1977 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | #ifdef TUN_DEBUG |
| 1979 | case TUNSETDEBUG: |
| 1980 | tun->debug = arg; |
| 1981 | break; |
| 1982 | #endif |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 1983 | case TUNSETOFFLOAD: |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 1984 | ret = set_offload(tun, arg); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1985 | break; |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 1986 | |
Max Krasnyansky | f271b2c | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 1987 | case TUNSETTXFILTER: |
| 1988 | /* Can be set only for TAPs */ |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1989 | ret = -EINVAL; |
Max Krasnyansky | f271b2c | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 1990 | if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1991 | break; |
Harvey Harrison | c0e5a8c | 2008-07-16 12:45:34 -0700 | [diff] [blame] | 1992 | ret = update_filter(&tun->txflt, (void __user *)arg); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1993 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | |
| 1995 | case SIOCGIFHWADDR: |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 1996 | /* Get hw address */ |
Max Krasnyansky | f271b2c | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 1997 | memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); |
| 1998 | ifr.ifr_hwaddr.sa_family = tun->dev->type; |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 1999 | if (copy_to_user(argp, &ifr, ifreq_len)) |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2000 | ret = -EFAULT; |
| 2001 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | |
| 2003 | case SIOCSIFHWADDR: |
Max Krasnyansky | f271b2c | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 2004 | /* Set hw address */ |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 2005 | tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n", |
| 2006 | ifr.ifr_hwaddr.sa_data); |
Kim B. Heino | 4010237 | 2008-02-29 12:26:21 -0800 | [diff] [blame] | 2007 | |
Kim B. Heino | 4010237 | 2008-02-29 12:26:21 -0800 | [diff] [blame] | 2008 | ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2009 | break; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 2010 | |
| 2011 | case TUNGETSNDBUF: |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2012 | sndbuf = tfile->socket.sk->sk_sndbuf; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 2013 | if (copy_to_user(argp, &sndbuf, sizeof(sndbuf))) |
| 2014 | ret = -EFAULT; |
| 2015 | break; |
| 2016 | |
| 2017 | case TUNSETSNDBUF: |
| 2018 | if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) { |
| 2019 | ret = -EFAULT; |
| 2020 | break; |
| 2021 | } |
| 2022 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 2023 | tun->sndbuf = sndbuf; |
| 2024 | tun_set_sndbuf(tun); |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 2025 | break; |
| 2026 | |
Michael S. Tsirkin | d9d52b5 | 2010-03-17 17:45:01 +0200 | [diff] [blame] | 2027 | case TUNGETVNETHDRSZ: |
| 2028 | vnet_hdr_sz = tun->vnet_hdr_sz; |
| 2029 | if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz))) |
| 2030 | ret = -EFAULT; |
| 2031 | break; |
| 2032 | |
| 2033 | case TUNSETVNETHDRSZ: |
| 2034 | if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) { |
| 2035 | ret = -EFAULT; |
| 2036 | break; |
| 2037 | } |
| 2038 | if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) { |
| 2039 | ret = -EINVAL; |
| 2040 | break; |
| 2041 | } |
| 2042 | |
| 2043 | tun->vnet_hdr_sz = vnet_hdr_sz; |
| 2044 | break; |
| 2045 | |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 2046 | case TUNATTACHFILTER: |
| 2047 | /* Can be set only for TAPs */ |
| 2048 | ret = -EINVAL; |
| 2049 | if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) |
| 2050 | break; |
| 2051 | ret = -EFAULT; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2052 | if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog))) |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 2053 | break; |
| 2054 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 2055 | ret = tun_attach_filter(tun); |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 2056 | break; |
| 2057 | |
| 2058 | case TUNDETACHFILTER: |
| 2059 | /* Can be set only for TAPs */ |
| 2060 | ret = -EINVAL; |
| 2061 | if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) |
| 2062 | break; |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 2063 | ret = 0; |
| 2064 | tun_detach_filter(tun, tun->numqueues); |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 2065 | break; |
| 2066 | |
Pavel Emelyanov | 76975e9 | 2013-08-21 14:32:39 +0400 | [diff] [blame] | 2067 | case TUNGETFILTER: |
| 2068 | ret = -EINVAL; |
| 2069 | if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) |
| 2070 | break; |
| 2071 | ret = -EFAULT; |
| 2072 | if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog))) |
| 2073 | break; |
| 2074 | ret = 0; |
| 2075 | break; |
| 2076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | default: |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2078 | ret = -EINVAL; |
| 2079 | break; |
Joe Perches | ee289b6 | 2010-05-17 22:47:34 -0700 | [diff] [blame] | 2080 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 2082 | unlock: |
| 2083 | rtnl_unlock(); |
| 2084 | if (tun) |
| 2085 | tun_put(tun); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2086 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | } |
| 2088 | |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 2089 | static long tun_chr_ioctl(struct file *file, |
| 2090 | unsigned int cmd, unsigned long arg) |
| 2091 | { |
| 2092 | return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq)); |
| 2093 | } |
| 2094 | |
| 2095 | #ifdef CONFIG_COMPAT |
| 2096 | static long tun_chr_compat_ioctl(struct file *file, |
| 2097 | unsigned int cmd, unsigned long arg) |
| 2098 | { |
| 2099 | switch (cmd) { |
| 2100 | case TUNSETIFF: |
| 2101 | case TUNGETIFF: |
| 2102 | case TUNSETTXFILTER: |
| 2103 | case TUNGETSNDBUF: |
| 2104 | case TUNSETSNDBUF: |
| 2105 | case SIOCGIFHWADDR: |
| 2106 | case SIOCSIFHWADDR: |
| 2107 | arg = (unsigned long)compat_ptr(arg); |
| 2108 | break; |
| 2109 | default: |
| 2110 | arg = (compat_ulong_t)arg; |
| 2111 | break; |
| 2112 | } |
| 2113 | |
| 2114 | /* |
| 2115 | * compat_ifreq is shorter than ifreq, so we must not access beyond |
| 2116 | * the end of that structure. All fields that are used in this |
| 2117 | * driver are compatible though, we don't need to convert the |
| 2118 | * contents. |
| 2119 | */ |
| 2120 | return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq)); |
| 2121 | } |
| 2122 | #endif /* CONFIG_COMPAT */ |
| 2123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | static int tun_chr_fasync(int fd, struct file *file, int on) |
| 2125 | { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2126 | struct tun_file *tfile = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | int ret; |
| 2128 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2129 | if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) |
Jonathan Corbet | 9d31952 | 2008-06-19 15:50:37 -0600 | [diff] [blame] | 2130 | goto out; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 2131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | if (on) { |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 2133 | ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | if (ret) |
Jonathan Corbet | 9d31952 | 2008-06-19 15:50:37 -0600 | [diff] [blame] | 2135 | goto out; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2136 | tfile->flags |= TUN_FASYNC; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 2137 | } else |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2138 | tfile->flags &= ~TUN_FASYNC; |
Jonathan Corbet | 9d31952 | 2008-06-19 15:50:37 -0600 | [diff] [blame] | 2139 | ret = 0; |
| 2140 | out: |
Jonathan Corbet | 9d31952 | 2008-06-19 15:50:37 -0600 | [diff] [blame] | 2141 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | } |
| 2143 | |
| 2144 | static int tun_chr_open(struct inode *inode, struct file * file) |
| 2145 | { |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2146 | struct tun_file *tfile; |
Thomas Gleixner | deed49f | 2009-10-14 01:19:46 -0700 | [diff] [blame] | 2147 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 2148 | DBG1(KERN_INFO, "tunX: tun_chr_open\n"); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2149 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2150 | tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL, |
| 2151 | &tun_proto); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2152 | if (!tfile) |
| 2153 | return -ENOMEM; |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 2154 | rcu_assign_pointer(tfile->tun, NULL); |
Eric W. Biederman | 36b50ba | 2009-01-20 11:01:48 +0000 | [diff] [blame] | 2155 | tfile->net = get_net(current->nsproxy->net_ns); |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2156 | tfile->flags = 0; |
Pavel Emelyanov | fb7589a | 2013-08-21 14:31:38 +0400 | [diff] [blame] | 2157 | tfile->ifindex = 0; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2158 | |
| 2159 | rcu_assign_pointer(tfile->socket.wq, &tfile->wq); |
| 2160 | init_waitqueue_head(&tfile->wq.wait); |
| 2161 | |
| 2162 | tfile->socket.file = file; |
| 2163 | tfile->socket.ops = &tun_socket_ops; |
| 2164 | |
| 2165 | sock_init_data(&tfile->socket, &tfile->sk); |
| 2166 | sk_change_net(&tfile->sk, tfile->net); |
| 2167 | |
| 2168 | tfile->sk.sk_write_space = tun_sock_write_space; |
| 2169 | tfile->sk.sk_sndbuf = INT_MAX; |
| 2170 | |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2171 | file->private_data = tfile; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2172 | set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags); |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 2173 | INIT_LIST_HEAD(&tfile->next); |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2174 | |
Jason Wang | 19a6afb | 2013-06-08 14:17:41 +0800 | [diff] [blame] | 2175 | sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); |
| 2176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | return 0; |
| 2178 | } |
| 2179 | |
| 2180 | static int tun_chr_close(struct inode *inode, struct file *file) |
| 2181 | { |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2182 | struct tun_file *tfile = file->private_data; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2183 | struct net *net = tfile->net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 2185 | tun_detach(tfile, true); |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2186 | put_net(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | |
| 2188 | return 0; |
| 2189 | } |
| 2190 | |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 2191 | static const struct file_operations tun_fops = { |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 2192 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | .llseek = no_llseek, |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 2194 | .read = do_sync_read, |
| 2195 | .aio_read = tun_chr_aio_read, |
| 2196 | .write = do_sync_write, |
| 2197 | .aio_write = tun_chr_aio_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | .poll = tun_chr_poll, |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 2199 | .unlocked_ioctl = tun_chr_ioctl, |
| 2200 | #ifdef CONFIG_COMPAT |
| 2201 | .compat_ioctl = tun_chr_compat_ioctl, |
| 2202 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | .open = tun_chr_open, |
| 2204 | .release = tun_chr_close, |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 2205 | .fasync = tun_chr_fasync |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | }; |
| 2207 | |
| 2208 | static struct miscdevice tun_miscdev = { |
| 2209 | .minor = TUN_MINOR, |
| 2210 | .name = "tun", |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 2211 | .nodename = "net/tun", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | .fops = &tun_fops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | }; |
| 2214 | |
| 2215 | /* ethtool interface */ |
| 2216 | |
| 2217 | static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 2218 | { |
| 2219 | cmd->supported = 0; |
| 2220 | cmd->advertising = 0; |
David Decotigny | 7073949 | 2011-04-27 18:32:40 +0000 | [diff] [blame] | 2221 | ethtool_cmd_speed_set(cmd, SPEED_10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | cmd->duplex = DUPLEX_FULL; |
| 2223 | cmd->port = PORT_TP; |
| 2224 | cmd->phy_address = 0; |
| 2225 | cmd->transceiver = XCVR_INTERNAL; |
| 2226 | cmd->autoneg = AUTONEG_DISABLE; |
| 2227 | cmd->maxtxpkt = 0; |
| 2228 | cmd->maxrxpkt = 0; |
| 2229 | return 0; |
| 2230 | } |
| 2231 | |
| 2232 | static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) |
| 2233 | { |
| 2234 | struct tun_struct *tun = netdev_priv(dev); |
| 2235 | |
Rick Jones | 33a5ba1 | 2011-11-15 14:59:53 +0000 | [diff] [blame] | 2236 | strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); |
| 2237 | strlcpy(info->version, DRV_VERSION, sizeof(info->version)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | |
| 2239 | switch (tun->flags & TUN_TYPE_MASK) { |
| 2240 | case TUN_TUN_DEV: |
Rick Jones | 33a5ba1 | 2011-11-15 14:59:53 +0000 | [diff] [blame] | 2241 | strlcpy(info->bus_info, "tun", sizeof(info->bus_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | break; |
| 2243 | case TUN_TAP_DEV: |
Rick Jones | 33a5ba1 | 2011-11-15 14:59:53 +0000 | [diff] [blame] | 2244 | strlcpy(info->bus_info, "tap", sizeof(info->bus_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | break; |
| 2246 | } |
| 2247 | } |
| 2248 | |
| 2249 | static u32 tun_get_msglevel(struct net_device *dev) |
| 2250 | { |
| 2251 | #ifdef TUN_DEBUG |
| 2252 | struct tun_struct *tun = netdev_priv(dev); |
| 2253 | return tun->debug; |
| 2254 | #else |
| 2255 | return -EOPNOTSUPP; |
| 2256 | #endif |
| 2257 | } |
| 2258 | |
| 2259 | static void tun_set_msglevel(struct net_device *dev, u32 value) |
| 2260 | { |
| 2261 | #ifdef TUN_DEBUG |
| 2262 | struct tun_struct *tun = netdev_priv(dev); |
| 2263 | tun->debug = value; |
| 2264 | #endif |
| 2265 | } |
| 2266 | |
Jeff Garzik | 7282d49 | 2006-09-13 14:30:00 -0400 | [diff] [blame] | 2267 | static const struct ethtool_ops tun_ethtool_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | .get_settings = tun_get_settings, |
| 2269 | .get_drvinfo = tun_get_drvinfo, |
| 2270 | .get_msglevel = tun_get_msglevel, |
| 2271 | .set_msglevel = tun_set_msglevel, |
Nolan Leake | bee3136 | 2010-07-27 13:53:43 +0000 | [diff] [blame] | 2272 | .get_link = ethtool_op_get_link, |
Richard Cochran | eda2977 | 2013-07-19 19:40:10 +0200 | [diff] [blame] | 2273 | .get_ts_info = ethtool_op_get_ts_info, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | }; |
| 2275 | |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 2276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | static int __init tun_init(void) |
| 2278 | { |
| 2279 | int ret = 0; |
| 2280 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 2281 | pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION); |
| 2282 | pr_info("%s\n", DRV_COPYRIGHT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 2284 | ret = rtnl_link_register(&tun_link_ops); |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 2285 | if (ret) { |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 2286 | pr_err("Can't register link_ops\n"); |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 2287 | goto err_linkops; |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 2288 | } |
| 2289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | ret = misc_register(&tun_miscdev); |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 2291 | if (ret) { |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 2292 | pr_err("Can't register misc device %d\n", TUN_MINOR); |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 2293 | goto err_misc; |
| 2294 | } |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 2295 | return 0; |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 2296 | err_misc: |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 2297 | rtnl_link_unregister(&tun_link_ops); |
| 2298 | err_linkops: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | return ret; |
| 2300 | } |
| 2301 | |
| 2302 | static void tun_cleanup(void) |
| 2303 | { |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 2304 | misc_deregister(&tun_miscdev); |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 2305 | rtnl_link_unregister(&tun_link_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | } |
| 2307 | |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 2308 | /* Get an underlying socket object from tun file. Returns error unless file is |
| 2309 | * attached to a device. The returned object works like a packet socket, it |
| 2310 | * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for |
| 2311 | * holding a reference to the file for as long as the socket is in use. */ |
| 2312 | struct socket *tun_get_socket(struct file *file) |
| 2313 | { |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 2314 | struct tun_file *tfile; |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 2315 | if (file->f_op != &tun_fops) |
| 2316 | return ERR_PTR(-EINVAL); |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 2317 | tfile = file->private_data; |
| 2318 | if (!tfile) |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 2319 | return ERR_PTR(-EBADFD); |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2320 | return &tfile->socket; |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 2321 | } |
| 2322 | EXPORT_SYMBOL_GPL(tun_get_socket); |
| 2323 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | module_init(tun_init); |
| 2325 | module_exit(tun_cleanup); |
| 2326 | MODULE_DESCRIPTION(DRV_DESCRIPTION); |
| 2327 | MODULE_AUTHOR(DRV_COPYRIGHT); |
| 2328 | MODULE_LICENSE("GPL"); |
| 2329 | MODULE_ALIAS_MISCDEV(TUN_MINOR); |
Kay Sievers | 578454f | 2010-05-20 18:07:20 +0200 | [diff] [blame] | 2330 | MODULE_ALIAS("devname:net/tun"); |