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> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 47 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/major.h> |
| 49 | #include <linux/slab.h> |
| 50 | #include <linux/poll.h> |
| 51 | #include <linux/fcntl.h> |
| 52 | #include <linux/init.h> |
| 53 | #include <linux/skbuff.h> |
| 54 | #include <linux/netdevice.h> |
| 55 | #include <linux/etherdevice.h> |
| 56 | #include <linux/miscdevice.h> |
| 57 | #include <linux/ethtool.h> |
| 58 | #include <linux/rtnetlink.h> |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 59 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #include <linux/if.h> |
| 61 | #include <linux/if_arp.h> |
| 62 | #include <linux/if_ether.h> |
| 63 | #include <linux/if_tun.h> |
Jason Wang | 6680ec6 | 2013-07-25 13:00:33 +0800 | [diff] [blame] | 64 | #include <linux/if_vlan.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | #include <linux/crc32.h> |
Pavel Emelyanov | d647a59 | 2008-04-16 00:41:16 -0700 | [diff] [blame] | 66 | #include <linux/nsproxy.h> |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 67 | #include <linux/virtio_net.h> |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 68 | #include <linux/rcupdate.h> |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 69 | #include <net/net_namespace.h> |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 70 | #include <net/netns/generic.h> |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 71 | #include <net/rtnetlink.h> |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 72 | #include <net/sock.h> |
Masatake YAMATO | 93e14b6 | 2014-01-29 16:43:31 +0900 | [diff] [blame] | 73 | #include <linux/seq_file.h> |
Herbert Xu | e0b46d0 | 2014-11-07 21:22:23 +0800 | [diff] [blame] | 74 | #include <linux/uio.h> |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 75 | #include <linux/skb_array.h> |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 76 | #include <linux/bpf.h> |
| 77 | #include <linux/bpf_trace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 79 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 81 | /* Uncomment to enable debugging */ |
| 82 | /* #define TUN_DEBUG 1 */ |
| 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | #ifdef TUN_DEBUG |
| 85 | static int debug; |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 86 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 87 | #define tun_debug(level, tun, fmt, args...) \ |
| 88 | do { \ |
| 89 | if (tun->debug) \ |
| 90 | netdev_printk(level, tun->dev, fmt, ##args); \ |
| 91 | } while (0) |
| 92 | #define DBG1(level, fmt, args...) \ |
| 93 | do { \ |
| 94 | if (debug == 2) \ |
| 95 | printk(level fmt, ##args); \ |
| 96 | } while (0) |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 97 | #else |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 98 | #define tun_debug(level, tun, fmt, args...) \ |
| 99 | do { \ |
| 100 | if (0) \ |
| 101 | netdev_printk(level, tun->dev, fmt, ##args); \ |
| 102 | } while (0) |
| 103 | #define DBG1(level, fmt, args...) \ |
| 104 | do { \ |
| 105 | if (0) \ |
| 106 | printk(level fmt, ##args); \ |
| 107 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | #endif |
| 109 | |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 110 | #define TUN_HEADROOM 256 |
| 111 | #define TUN_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD + TUN_HEADROOM) |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 112 | |
Michael S. Tsirkin | 031f5e03 | 2014-11-19 14:44:40 +0200 | [diff] [blame] | 113 | /* TUN device flags */ |
| 114 | |
| 115 | /* IFF_ATTACH_QUEUE is never stored in device flags, |
| 116 | * overload it to mean fasync when stored there. |
| 117 | */ |
| 118 | #define TUN_FASYNC IFF_ATTACH_QUEUE |
Michael S. Tsirkin | 1cf8e41 | 2014-12-16 15:05:06 +0200 | [diff] [blame] | 119 | /* High bits in flags field are unused. */ |
| 120 | #define TUN_VNET_LE 0x80000000 |
Greg Kurz | 8b8e658 | 2015-04-24 14:50:36 +0200 | [diff] [blame] | 121 | #define TUN_VNET_BE 0x40000000 |
Michael S. Tsirkin | 031f5e03 | 2014-11-19 14:44:40 +0200 | [diff] [blame] | 122 | |
| 123 | #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \ |
Michael S. Tsirkin | 1cf8e41 | 2014-12-16 15:05:06 +0200 | [diff] [blame] | 124 | IFF_MULTI_QUEUE) |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 125 | #define GOODCOPY_LEN 128 |
| 126 | |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 127 | #define FLT_EXACT_COUNT 8 |
| 128 | struct tap_filter { |
| 129 | unsigned int count; /* Number of addrs. Zero means disabled */ |
| 130 | u32 mask[2]; /* Mask of the hashed addrs */ |
| 131 | unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN]; |
| 132 | }; |
| 133 | |
Pankaj Gupta | baf71c5 | 2015-01-12 11:41:29 +0530 | [diff] [blame] | 134 | /* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal |
| 135 | * to max number of VCPUs in guest. */ |
| 136 | #define MAX_TAP_QUEUES 256 |
Jason Wang | b8732fb | 2013-01-23 03:59:13 +0000 | [diff] [blame] | 137 | #define MAX_TAP_FLOWS 4096 |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 138 | |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 139 | #define TUN_FLOW_EXPIRE (3 * HZ) |
| 140 | |
Paolo Abeni | 608b997 | 2016-04-13 10:52:20 +0200 | [diff] [blame] | 141 | struct tun_pcpu_stats { |
| 142 | u64 rx_packets; |
| 143 | u64 rx_bytes; |
| 144 | u64 tx_packets; |
| 145 | u64 tx_bytes; |
| 146 | struct u64_stats_sync syncp; |
| 147 | u32 rx_dropped; |
| 148 | u32 tx_dropped; |
| 149 | u32 rx_frame_errors; |
| 150 | }; |
| 151 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 152 | /* 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] | 153 | * also contains all socket related structures (except sock_fprog and tap_filter) |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 154 | * to serve as one transmit queue for tuntap device. The sock_fprog and |
| 155 | * 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] | 156 | * 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] | 157 | * this). |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 158 | * |
| 159 | * RCU usage: |
Rami Rosen | 36fe8c09 | 2012-11-25 22:07:40 +0000 | [diff] [blame] | 160 | * 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] | 161 | * 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] | 162 | */ |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 163 | struct tun_file { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 164 | struct sock sk; |
| 165 | struct socket socket; |
| 166 | struct socket_wq wq; |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 167 | struct tun_struct __rcu *tun; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 168 | struct fasync_struct *fasync; |
| 169 | /* only used for fasnyc */ |
| 170 | unsigned int flags; |
Pavel Emelyanov | fb7589a | 2013-08-21 14:31:38 +0400 | [diff] [blame] | 171 | union { |
| 172 | u16 queue_index; |
| 173 | unsigned int ifindex; |
| 174 | }; |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 175 | struct list_head next; |
| 176 | struct tun_struct *detached; |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 177 | struct skb_array tx_array; |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 178 | struct page_frag alloc_frag; |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 181 | struct tun_flow_entry { |
| 182 | struct hlist_node hash_link; |
| 183 | struct rcu_head rcu; |
| 184 | struct tun_struct *tun; |
| 185 | |
| 186 | u32 rxhash; |
Tom Herbert | 9bc8893 | 2013-12-22 18:54:32 +0800 | [diff] [blame] | 187 | u32 rps_rxhash; |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 188 | int queue_index; |
| 189 | unsigned long updated; |
| 190 | }; |
| 191 | |
| 192 | #define TUN_NUM_FLOW_ENTRIES 1024 |
| 193 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 194 | /* 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] | 195 | * device, socket filter, sndbuf and vnet header size were restore when the |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 196 | * file were attached to a persist device. |
| 197 | */ |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 198 | struct tun_struct { |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 199 | struct tun_file __rcu *tfiles[MAX_TAP_QUEUES]; |
| 200 | unsigned int numqueues; |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 201 | unsigned int flags; |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 202 | kuid_t owner; |
| 203 | kgid_t group; |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 204 | |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 205 | struct net_device *dev; |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 206 | netdev_features_t set_features; |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 207 | #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \ |
David S. Miller | d591a1f | 2017-07-03 06:35:32 -0700 | [diff] [blame] | 208 | NETIF_F_TSO6) |
Michael S. Tsirkin | d9d52b5 | 2010-03-17 17:45:01 +0200 | [diff] [blame] | 209 | |
Paolo Abeni | eaea34b | 2016-02-26 10:45:40 +0100 | [diff] [blame] | 210 | int align; |
Michael S. Tsirkin | d9d52b5 | 2010-03-17 17:45:01 +0200 | [diff] [blame] | 211 | int vnet_hdr_sz; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 212 | int sndbuf; |
| 213 | struct tap_filter txflt; |
| 214 | struct sock_fprog fprog; |
| 215 | /* protected by rtnl lock */ |
| 216 | bool filter_attached; |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 217 | #ifdef TUN_DEBUG |
| 218 | int debug; |
| 219 | #endif |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 220 | spinlock_t lock; |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 221 | struct hlist_head flows[TUN_NUM_FLOW_ENTRIES]; |
| 222 | struct timer_list flow_gc_timer; |
| 223 | unsigned long ageing_time; |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 224 | unsigned int numdisabled; |
| 225 | struct list_head disabled; |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 226 | void *security; |
Jason Wang | b8732fb | 2013-01-23 03:59:13 +0000 | [diff] [blame] | 227 | u32 flow_count; |
Jason Wang | 5503fce | 2017-01-18 15:02:03 +0800 | [diff] [blame] | 228 | u32 rx_batched; |
Paolo Abeni | 608b997 | 2016-04-13 10:52:20 +0200 | [diff] [blame] | 229 | struct tun_pcpu_stats __percpu *pcpu_stats; |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 230 | struct bpf_prog __rcu *xdp_prog; |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 231 | }; |
| 232 | |
Greg Kurz | 8b8e658 | 2015-04-24 14:50:36 +0200 | [diff] [blame] | 233 | #ifdef CONFIG_TUN_VNET_CROSS_LE |
| 234 | static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) |
| 235 | { |
| 236 | return tun->flags & TUN_VNET_BE ? false : |
| 237 | virtio_legacy_is_little_endian(); |
| 238 | } |
| 239 | |
| 240 | static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) |
| 241 | { |
| 242 | int be = !!(tun->flags & TUN_VNET_BE); |
| 243 | |
| 244 | if (put_user(be, argp)) |
| 245 | return -EFAULT; |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) |
| 251 | { |
| 252 | int be; |
| 253 | |
| 254 | if (get_user(be, argp)) |
| 255 | return -EFAULT; |
| 256 | |
| 257 | if (be) |
| 258 | tun->flags |= TUN_VNET_BE; |
| 259 | else |
| 260 | tun->flags &= ~TUN_VNET_BE; |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | #else |
| 265 | static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) |
| 266 | { |
| 267 | return virtio_legacy_is_little_endian(); |
| 268 | } |
| 269 | |
| 270 | static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) |
| 271 | { |
| 272 | return -EINVAL; |
| 273 | } |
| 274 | |
| 275 | static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) |
| 276 | { |
| 277 | return -EINVAL; |
| 278 | } |
| 279 | #endif /* CONFIG_TUN_VNET_CROSS_LE */ |
| 280 | |
Greg Kurz | 25bd55bb | 2015-04-24 14:24:38 +0200 | [diff] [blame] | 281 | static inline bool tun_is_little_endian(struct tun_struct *tun) |
| 282 | { |
Greg Kurz | 7d82410 | 2015-04-24 14:26:24 +0200 | [diff] [blame] | 283 | return tun->flags & TUN_VNET_LE || |
Greg Kurz | 8b8e658 | 2015-04-24 14:50:36 +0200 | [diff] [blame] | 284 | tun_legacy_is_little_endian(tun); |
Greg Kurz | 25bd55bb | 2015-04-24 14:24:38 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Michael S. Tsirkin | 56f0dcc | 2014-10-23 22:59:31 +0300 | [diff] [blame] | 287 | static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val) |
| 288 | { |
Greg Kurz | 25bd55bb | 2015-04-24 14:24:38 +0200 | [diff] [blame] | 289 | return __virtio16_to_cpu(tun_is_little_endian(tun), val); |
Michael S. Tsirkin | 56f0dcc | 2014-10-23 22:59:31 +0300 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val) |
| 293 | { |
Greg Kurz | 25bd55bb | 2015-04-24 14:24:38 +0200 | [diff] [blame] | 294 | return __cpu_to_virtio16(tun_is_little_endian(tun), val); |
Michael S. Tsirkin | 56f0dcc | 2014-10-23 22:59:31 +0300 | [diff] [blame] | 295 | } |
| 296 | |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 297 | static inline u32 tun_hashfn(u32 rxhash) |
| 298 | { |
| 299 | return rxhash & 0x3ff; |
| 300 | } |
| 301 | |
| 302 | static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash) |
| 303 | { |
| 304 | struct tun_flow_entry *e; |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 305 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 306 | hlist_for_each_entry_rcu(e, head, hash_link) { |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 307 | if (e->rxhash == rxhash) |
| 308 | return e; |
| 309 | } |
| 310 | return NULL; |
| 311 | } |
| 312 | |
| 313 | static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun, |
| 314 | struct hlist_head *head, |
| 315 | u32 rxhash, u16 queue_index) |
| 316 | { |
Eric Dumazet | 9fdc6be | 2012-12-21 07:17:21 +0000 | [diff] [blame] | 317 | struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC); |
| 318 | |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 319 | if (e) { |
| 320 | tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n", |
| 321 | rxhash, queue_index); |
| 322 | e->updated = jiffies; |
| 323 | e->rxhash = rxhash; |
Tom Herbert | 9bc8893 | 2013-12-22 18:54:32 +0800 | [diff] [blame] | 324 | e->rps_rxhash = 0; |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 325 | e->queue_index = queue_index; |
| 326 | e->tun = tun; |
| 327 | hlist_add_head_rcu(&e->hash_link, head); |
Jason Wang | b8732fb | 2013-01-23 03:59:13 +0000 | [diff] [blame] | 328 | ++tun->flow_count; |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 329 | } |
| 330 | return e; |
| 331 | } |
| 332 | |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 333 | static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e) |
| 334 | { |
| 335 | tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n", |
| 336 | e->rxhash, e->queue_index); |
| 337 | hlist_del_rcu(&e->hash_link); |
Eric Dumazet | 9fdc6be | 2012-12-21 07:17:21 +0000 | [diff] [blame] | 338 | kfree_rcu(e, rcu); |
Jason Wang | b8732fb | 2013-01-23 03:59:13 +0000 | [diff] [blame] | 339 | --tun->flow_count; |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | static void tun_flow_flush(struct tun_struct *tun) |
| 343 | { |
| 344 | int i; |
| 345 | |
| 346 | spin_lock_bh(&tun->lock); |
| 347 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { |
| 348 | struct tun_flow_entry *e; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 349 | struct hlist_node *n; |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 350 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 351 | hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 352 | tun_flow_delete(tun, e); |
| 353 | } |
| 354 | spin_unlock_bh(&tun->lock); |
| 355 | } |
| 356 | |
| 357 | static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index) |
| 358 | { |
| 359 | int i; |
| 360 | |
| 361 | spin_lock_bh(&tun->lock); |
| 362 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { |
| 363 | struct tun_flow_entry *e; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 364 | struct hlist_node *n; |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 365 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 366 | hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 367 | if (e->queue_index == queue_index) |
| 368 | tun_flow_delete(tun, e); |
| 369 | } |
| 370 | } |
| 371 | spin_unlock_bh(&tun->lock); |
| 372 | } |
| 373 | |
| 374 | static void tun_flow_cleanup(unsigned long data) |
| 375 | { |
| 376 | struct tun_struct *tun = (struct tun_struct *)data; |
| 377 | unsigned long delay = tun->ageing_time; |
| 378 | unsigned long next_timer = jiffies + delay; |
| 379 | unsigned long count = 0; |
| 380 | int i; |
| 381 | |
| 382 | tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n"); |
| 383 | |
| 384 | spin_lock_bh(&tun->lock); |
| 385 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { |
| 386 | struct tun_flow_entry *e; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 387 | struct hlist_node *n; |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 388 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 389 | hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 390 | unsigned long this_timer; |
| 391 | count++; |
| 392 | this_timer = e->updated + delay; |
| 393 | if (time_before_eq(this_timer, jiffies)) |
| 394 | tun_flow_delete(tun, e); |
| 395 | else if (time_before(this_timer, next_timer)) |
| 396 | next_timer = this_timer; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | if (count) |
| 401 | mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer)); |
| 402 | spin_unlock_bh(&tun->lock); |
| 403 | } |
| 404 | |
Eric Dumazet | 4997442 | 2012-12-12 19:22:57 +0000 | [diff] [blame] | 405 | static void tun_flow_update(struct tun_struct *tun, u32 rxhash, |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 406 | struct tun_file *tfile) |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 407 | { |
| 408 | struct hlist_head *head; |
| 409 | struct tun_flow_entry *e; |
| 410 | unsigned long delay = tun->ageing_time; |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 411 | u16 queue_index = tfile->queue_index; |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 412 | |
| 413 | if (!rxhash) |
| 414 | return; |
| 415 | else |
| 416 | head = &tun->flows[tun_hashfn(rxhash)]; |
| 417 | |
| 418 | rcu_read_lock(); |
| 419 | |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 420 | /* We may get a very small possibility of OOO during switching, not |
| 421 | * worth to optimize.*/ |
| 422 | if (tun->numqueues == 1 || tfile->detached) |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 423 | goto unlock; |
| 424 | |
| 425 | e = tun_flow_find(head, rxhash); |
| 426 | if (likely(e)) { |
| 427 | /* TODO: keep queueing to old queue until it's empty? */ |
| 428 | e->queue_index = queue_index; |
| 429 | e->updated = jiffies; |
Tom Herbert | 9bc8893 | 2013-12-22 18:54:32 +0800 | [diff] [blame] | 430 | sock_rps_record_flow_hash(e->rps_rxhash); |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 431 | } else { |
| 432 | spin_lock_bh(&tun->lock); |
Jason Wang | b8732fb | 2013-01-23 03:59:13 +0000 | [diff] [blame] | 433 | if (!tun_flow_find(head, rxhash) && |
| 434 | tun->flow_count < MAX_TAP_FLOWS) |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 435 | tun_flow_create(tun, head, rxhash, queue_index); |
| 436 | |
| 437 | if (!timer_pending(&tun->flow_gc_timer)) |
| 438 | mod_timer(&tun->flow_gc_timer, |
| 439 | round_jiffies_up(jiffies + delay)); |
| 440 | spin_unlock_bh(&tun->lock); |
| 441 | } |
| 442 | |
| 443 | unlock: |
| 444 | rcu_read_unlock(); |
| 445 | } |
| 446 | |
Tom Herbert | 9bc8893 | 2013-12-22 18:54:32 +0800 | [diff] [blame] | 447 | /** |
| 448 | * Save the hash received in the stack receive path and update the |
| 449 | * flow_hash table accordingly. |
| 450 | */ |
| 451 | static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash) |
| 452 | { |
Eric Dumazet | 567e4b7 | 2015-02-06 12:59:01 -0800 | [diff] [blame] | 453 | if (unlikely(e->rps_rxhash != hash)) |
Tom Herbert | 9bc8893 | 2013-12-22 18:54:32 +0800 | [diff] [blame] | 454 | e->rps_rxhash = hash; |
Tom Herbert | 9bc8893 | 2013-12-22 18:54:32 +0800 | [diff] [blame] | 455 | } |
| 456 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 457 | /* 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] | 458 | * 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] | 459 | * the rxq based on the txq where the last packet of the flow comes. As |
| 460 | * the userspace application move between processors, we may get a |
| 461 | * different rxq no. here. If we could not get rxhash, then we would |
| 462 | * hope the rxq no. may help here. |
| 463 | */ |
Jason Wang | f663dd9 | 2014-01-10 16:18:26 +0800 | [diff] [blame] | 464 | static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb, |
Daniel Borkmann | 99932d4 | 2014-02-16 15:55:20 +0100 | [diff] [blame] | 465 | void *accel_priv, select_queue_fallback_t fallback) |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 466 | { |
| 467 | struct tun_struct *tun = netdev_priv(dev); |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 468 | struct tun_flow_entry *e; |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 469 | u32 txq = 0; |
| 470 | u32 numqueues = 0; |
| 471 | |
| 472 | rcu_read_lock(); |
Jason Wang | 92bb73e | 2013-06-05 16:44:57 +0800 | [diff] [blame] | 473 | numqueues = ACCESS_ONCE(tun->numqueues); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 474 | |
Jason Wang | feec084 | 2017-06-06 14:09:49 +0800 | [diff] [blame] | 475 | txq = __skb_get_hash_symmetric(skb); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 476 | if (txq) { |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 477 | e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq); |
Tom Herbert | 9bc8893 | 2013-12-22 18:54:32 +0800 | [diff] [blame] | 478 | if (e) { |
Tom Herbert | 9bc8893 | 2013-12-22 18:54:32 +0800 | [diff] [blame] | 479 | tun_flow_save_rps_rxhash(e, txq); |
Zhi Yong Wu | fbe4d45 | 2014-01-02 13:24:28 +0800 | [diff] [blame] | 480 | txq = e->queue_index; |
Tom Herbert | 9bc8893 | 2013-12-22 18:54:32 +0800 | [diff] [blame] | 481 | } else |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 482 | /* use multiply and shift instead of expensive divide */ |
| 483 | txq = ((u64)txq * numqueues) >> 32; |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 484 | } else if (likely(skb_rx_queue_recorded(skb))) { |
| 485 | txq = skb_get_rx_queue(skb); |
| 486 | while (unlikely(txq >= numqueues)) |
| 487 | txq -= numqueues; |
| 488 | } |
| 489 | |
| 490 | rcu_read_unlock(); |
| 491 | return txq; |
| 492 | } |
| 493 | |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 494 | static inline bool tun_not_capable(struct tun_struct *tun) |
| 495 | { |
| 496 | const struct cred *cred = current_cred(); |
Eric W. Biederman | c260b77 | 2012-11-18 21:34:11 +0000 | [diff] [blame] | 497 | struct net *net = dev_net(tun->dev); |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 498 | |
| 499 | return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) || |
| 500 | (gid_valid(tun->group) && !in_egroup_p(tun->group))) && |
Eric W. Biederman | c260b77 | 2012-11-18 21:34:11 +0000 | [diff] [blame] | 501 | !ns_capable(net->user_ns, CAP_NET_ADMIN); |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 504 | static void tun_set_real_num_queues(struct tun_struct *tun) |
| 505 | { |
| 506 | netif_set_real_num_tx_queues(tun->dev, tun->numqueues); |
| 507 | netif_set_real_num_rx_queues(tun->dev, tun->numqueues); |
| 508 | } |
| 509 | |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 510 | static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) |
| 511 | { |
| 512 | tfile->detached = tun; |
| 513 | list_add_tail(&tfile->next, &tun->disabled); |
| 514 | ++tun->numdisabled; |
| 515 | } |
| 516 | |
Jason Wang | d32649d | 2012-12-18 11:00:27 +0800 | [diff] [blame] | 517 | static struct tun_struct *tun_enable_queue(struct tun_file *tfile) |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 518 | { |
| 519 | struct tun_struct *tun = tfile->detached; |
| 520 | |
| 521 | tfile->detached = NULL; |
| 522 | list_del_init(&tfile->next); |
| 523 | --tun->numdisabled; |
| 524 | return tun; |
| 525 | } |
| 526 | |
Jason Wang | 4bfb051 | 2013-09-05 17:53:59 +0800 | [diff] [blame] | 527 | static void tun_queue_purge(struct tun_file *tfile) |
| 528 | { |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 529 | struct sk_buff *skb; |
| 530 | |
| 531 | while ((skb = skb_array_consume(&tfile->tx_array)) != NULL) |
| 532 | kfree_skb(skb); |
| 533 | |
Jason Wang | 5503fce | 2017-01-18 15:02:03 +0800 | [diff] [blame] | 534 | skb_queue_purge(&tfile->sk.sk_write_queue); |
Jason Wang | 4bfb051 | 2013-09-05 17:53:59 +0800 | [diff] [blame] | 535 | skb_queue_purge(&tfile->sk.sk_error_queue); |
| 536 | } |
| 537 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 538 | static void __tun_detach(struct tun_file *tfile, bool clean) |
| 539 | { |
| 540 | struct tun_file *ntfile; |
| 541 | struct tun_struct *tun; |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 542 | |
Jason Wang | b8deabd | 2013-01-11 16:59:32 +0000 | [diff] [blame] | 543 | tun = rtnl_dereference(tfile->tun); |
| 544 | |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 545 | if (tun && !tfile->detached) { |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 546 | u16 index = tfile->queue_index; |
| 547 | BUG_ON(index >= tun->numqueues); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 548 | |
| 549 | rcu_assign_pointer(tun->tfiles[index], |
| 550 | tun->tfiles[tun->numqueues - 1]); |
Jason Wang | b8deabd | 2013-01-11 16:59:32 +0000 | [diff] [blame] | 551 | ntfile = rtnl_dereference(tun->tfiles[index]); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 552 | ntfile->queue_index = index; |
| 553 | |
| 554 | --tun->numqueues; |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 555 | if (clean) { |
Monam Agarwal | c956674 | 2014-03-24 00:02:32 +0530 | [diff] [blame] | 556 | RCU_INIT_POINTER(tfile->tun, NULL); |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 557 | sock_put(&tfile->sk); |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 558 | } else |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 559 | tun_disable_queue(tun, tfile); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 560 | |
| 561 | synchronize_net(); |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 562 | tun_flow_delete_by_queue(tun, tun->numqueues + 1); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 563 | /* Drop read queue */ |
Jason Wang | 4bfb051 | 2013-09-05 17:53:59 +0800 | [diff] [blame] | 564 | tun_queue_purge(tfile); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 565 | tun_set_real_num_queues(tun); |
Jason Wang | dd38bd8 | 2013-01-11 16:59:34 +0000 | [diff] [blame] | 566 | } else if (tfile->detached && clean) { |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 567 | tun = tun_enable_queue(tfile); |
Jason Wang | dd38bd8 | 2013-01-11 16:59:34 +0000 | [diff] [blame] | 568 | sock_put(&tfile->sk); |
| 569 | } |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 570 | |
| 571 | if (clean) { |
Michael S. Tsirkin | af668b3 | 2013-01-28 00:38:02 +0000 | [diff] [blame] | 572 | if (tun && tun->numqueues == 0 && tun->numdisabled == 0) { |
| 573 | netif_carrier_off(tun->dev); |
| 574 | |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 575 | if (!(tun->flags & IFF_PERSIST) && |
Michael S. Tsirkin | af668b3 | 2013-01-28 00:38:02 +0000 | [diff] [blame] | 576 | tun->dev->reg_state == NETREG_REGISTERED) |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 577 | unregister_netdevice(tun->dev); |
Michael S. Tsirkin | af668b3 | 2013-01-28 00:38:02 +0000 | [diff] [blame] | 578 | } |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 579 | if (tun) |
| 580 | skb_array_cleanup(&tfile->tx_array); |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 581 | if (tfile->alloc_frag.page) |
| 582 | put_page(tfile->alloc_frag.page); |
Eric W. Biederman | 140e807da | 2015-05-08 21:07:08 -0500 | [diff] [blame] | 583 | sock_put(&tfile->sk); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
| 587 | static void tun_detach(struct tun_file *tfile, bool clean) |
| 588 | { |
| 589 | rtnl_lock(); |
| 590 | __tun_detach(tfile, clean); |
| 591 | rtnl_unlock(); |
| 592 | } |
| 593 | |
| 594 | static void tun_detach_all(struct net_device *dev) |
| 595 | { |
| 596 | struct tun_struct *tun = netdev_priv(dev); |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 597 | struct bpf_prog *xdp_prog = rtnl_dereference(tun->xdp_prog); |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 598 | struct tun_file *tfile, *tmp; |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 599 | int i, n = tun->numqueues; |
| 600 | |
| 601 | for (i = 0; i < n; i++) { |
Jason Wang | b8deabd | 2013-01-11 16:59:32 +0000 | [diff] [blame] | 602 | tfile = rtnl_dereference(tun->tfiles[i]); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 603 | BUG_ON(!tfile); |
Jason Wang | addf8fc | 2016-05-19 13:36:51 +0800 | [diff] [blame] | 604 | tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; |
Xi Wang | 9e641bd | 2014-05-16 15:11:48 -0700 | [diff] [blame] | 605 | tfile->socket.sk->sk_data_ready(tfile->socket.sk); |
Monam Agarwal | c956674 | 2014-03-24 00:02:32 +0530 | [diff] [blame] | 606 | RCU_INIT_POINTER(tfile->tun, NULL); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 607 | --tun->numqueues; |
| 608 | } |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 609 | list_for_each_entry(tfile, &tun->disabled, next) { |
Jason Wang | addf8fc | 2016-05-19 13:36:51 +0800 | [diff] [blame] | 610 | tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; |
Xi Wang | 9e641bd | 2014-05-16 15:11:48 -0700 | [diff] [blame] | 611 | tfile->socket.sk->sk_data_ready(tfile->socket.sk); |
Monam Agarwal | c956674 | 2014-03-24 00:02:32 +0530 | [diff] [blame] | 612 | RCU_INIT_POINTER(tfile->tun, NULL); |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 613 | } |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 614 | BUG_ON(tun->numqueues != 0); |
| 615 | |
| 616 | synchronize_net(); |
| 617 | for (i = 0; i < n; i++) { |
Jason Wang | b8deabd | 2013-01-11 16:59:32 +0000 | [diff] [blame] | 618 | tfile = rtnl_dereference(tun->tfiles[i]); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 619 | /* Drop read queue */ |
Jason Wang | 4bfb051 | 2013-09-05 17:53:59 +0800 | [diff] [blame] | 620 | tun_queue_purge(tfile); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 621 | sock_put(&tfile->sk); |
| 622 | } |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 623 | list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { |
| 624 | tun_enable_queue(tfile); |
Jason Wang | 4bfb051 | 2013-09-05 17:53:59 +0800 | [diff] [blame] | 625 | tun_queue_purge(tfile); |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 626 | sock_put(&tfile->sk); |
| 627 | } |
| 628 | BUG_ON(tun->numdisabled != 0); |
Jason Wang | dd38bd8 | 2013-01-11 16:59:34 +0000 | [diff] [blame] | 629 | |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 630 | if (xdp_prog) |
| 631 | bpf_prog_put(xdp_prog); |
| 632 | |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 633 | if (tun->flags & IFF_PERSIST) |
Jason Wang | dd38bd8 | 2013-01-11 16:59:34 +0000 | [diff] [blame] | 634 | module_put(THIS_MODULE); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Pavel Emelyanov | 849c9b6 | 2013-08-21 14:32:21 +0400 | [diff] [blame] | 637 | 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] | 638 | { |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 639 | struct tun_file *tfile = file->private_data; |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 640 | struct net_device *dev = tun->dev; |
Eric W. Biederman | 38231b7 | 2009-01-20 11:02:28 +0000 | [diff] [blame] | 641 | int err; |
Eric W. Biederman | a7385ba | 2009-01-20 10:57:48 +0000 | [diff] [blame] | 642 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 643 | err = security_tun_dev_attach(tfile->socket.sk, tun->security); |
| 644 | if (err < 0) |
| 645 | goto out; |
| 646 | |
Eric W. Biederman | 38231b7 | 2009-01-20 11:02:28 +0000 | [diff] [blame] | 647 | err = -EINVAL; |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 648 | if (rtnl_dereference(tfile->tun) && !tfile->detached) |
Eric W. Biederman | 38231b7 | 2009-01-20 11:02:28 +0000 | [diff] [blame] | 649 | goto out; |
| 650 | |
| 651 | err = -EBUSY; |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 652 | if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1) |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 653 | goto out; |
| 654 | |
| 655 | err = -E2BIG; |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 656 | if (!tfile->detached && |
| 657 | tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES) |
Eric W. Biederman | 38231b7 | 2009-01-20 11:02:28 +0000 | [diff] [blame] | 658 | goto out; |
| 659 | |
| 660 | err = 0; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 661 | |
stephen hemminger | 92d4ea6 | 2013-12-05 20:42:58 -0800 | [diff] [blame] | 662 | /* Re-attach the filter to persist device */ |
Pavel Emelyanov | 849c9b6 | 2013-08-21 14:32:21 +0400 | [diff] [blame] | 663 | if (!skip_filter && (tun->filter_attached == true)) { |
Hannes Frederic Sowa | 8ced425 | 2016-04-05 17:10:16 +0200 | [diff] [blame] | 664 | lock_sock(tfile->socket.sk); |
| 665 | err = sk_attach_filter(&tun->fprog, tfile->socket.sk); |
| 666 | release_sock(tfile->socket.sk); |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 667 | if (!err) |
| 668 | goto out; |
| 669 | } |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 670 | |
| 671 | if (!tfile->detached && |
| 672 | skb_array_init(&tfile->tx_array, dev->tx_queue_len, GFP_KERNEL)) { |
| 673 | err = -ENOMEM; |
| 674 | goto out; |
| 675 | } |
| 676 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 677 | tfile->queue_index = tun->numqueues; |
Jason Wang | addf8fc | 2016-05-19 13:36:51 +0800 | [diff] [blame] | 678 | tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN; |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 679 | rcu_assign_pointer(tfile->tun, tun); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 680 | rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 681 | tun->numqueues++; |
| 682 | |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 683 | if (tfile->detached) |
| 684 | tun_enable_queue(tfile); |
| 685 | else |
| 686 | sock_hold(&tfile->sk); |
| 687 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 688 | tun_set_real_num_queues(tun); |
| 689 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 690 | /* device is allowed to go away first, so no need to hold extra |
| 691 | * refcnt. |
| 692 | */ |
Eric W. Biederman | a7385ba | 2009-01-20 10:57:48 +0000 | [diff] [blame] | 693 | |
Eric W. Biederman | 38231b7 | 2009-01-20 11:02:28 +0000 | [diff] [blame] | 694 | out: |
Eric W. Biederman | 38231b7 | 2009-01-20 11:02:28 +0000 | [diff] [blame] | 695 | return err; |
Eric W. Biederman | a7385ba | 2009-01-20 10:57:48 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 698 | static struct tun_struct *__tun_get(struct tun_file *tfile) |
| 699 | { |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 700 | struct tun_struct *tun; |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 701 | |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 702 | rcu_read_lock(); |
| 703 | tun = rcu_dereference(tfile->tun); |
| 704 | if (tun) |
| 705 | dev_hold(tun->dev); |
| 706 | rcu_read_unlock(); |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 707 | |
| 708 | return tun; |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | static struct tun_struct *tun_get(struct file *file) |
| 712 | { |
| 713 | return __tun_get(file->private_data); |
| 714 | } |
| 715 | |
| 716 | static void tun_put(struct tun_struct *tun) |
| 717 | { |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 718 | dev_put(tun->dev); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 721 | /* TAP filtering */ |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 722 | static void addr_hash_set(u32 *mask, const u8 *addr) |
| 723 | { |
| 724 | int n = ether_crc(ETH_ALEN, addr) >> 26; |
| 725 | mask[n >> 5] |= (1 << (n & 31)); |
| 726 | } |
| 727 | |
| 728 | static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) |
| 729 | { |
| 730 | int n = ether_crc(ETH_ALEN, addr) >> 26; |
| 731 | return mask[n >> 5] & (1 << (n & 31)); |
| 732 | } |
| 733 | |
| 734 | static int update_filter(struct tap_filter *filter, void __user *arg) |
| 735 | { |
| 736 | struct { u8 u[ETH_ALEN]; } *addr; |
| 737 | struct tun_filter uf; |
| 738 | int err, alen, n, nexact; |
| 739 | |
| 740 | if (copy_from_user(&uf, arg, sizeof(uf))) |
| 741 | return -EFAULT; |
| 742 | |
| 743 | if (!uf.count) { |
| 744 | /* Disabled */ |
| 745 | filter->count = 0; |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | alen = ETH_ALEN * uf.count; |
Markus Elfring | 28e8190 | 2016-08-20 08:54:15 +0200 | [diff] [blame] | 750 | addr = memdup_user(arg + sizeof(uf), alen); |
| 751 | if (IS_ERR(addr)) |
| 752 | return PTR_ERR(addr); |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 753 | |
| 754 | /* The filter is updated without holding any locks. Which is |
| 755 | * perfectly safe. We disable it first and in the worst |
| 756 | * case we'll accept a few undesired packets. */ |
| 757 | filter->count = 0; |
| 758 | wmb(); |
| 759 | |
| 760 | /* Use first set of addresses as an exact filter */ |
| 761 | for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++) |
| 762 | memcpy(filter->addr[n], addr[n].u, ETH_ALEN); |
| 763 | |
| 764 | nexact = n; |
| 765 | |
Alex Williamson | cfbf84f | 2009-02-08 17:49:17 -0800 | [diff] [blame] | 766 | /* Remaining multicast addresses are hashed, |
| 767 | * unicast will leave the filter disabled. */ |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 768 | memset(filter->mask, 0, sizeof(filter->mask)); |
Alex Williamson | cfbf84f | 2009-02-08 17:49:17 -0800 | [diff] [blame] | 769 | for (; n < uf.count; n++) { |
| 770 | if (!is_multicast_ether_addr(addr[n].u)) { |
| 771 | err = 0; /* no filter */ |
Markus Elfring | 3b8d2a6 | 2016-08-20 09:00:34 +0200 | [diff] [blame] | 772 | goto free_addr; |
Alex Williamson | cfbf84f | 2009-02-08 17:49:17 -0800 | [diff] [blame] | 773 | } |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 774 | addr_hash_set(filter->mask, addr[n].u); |
Alex Williamson | cfbf84f | 2009-02-08 17:49:17 -0800 | [diff] [blame] | 775 | } |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 776 | |
| 777 | /* For ALLMULTI just set the mask to all ones. |
| 778 | * This overrides the mask populated above. */ |
| 779 | if ((uf.flags & TUN_FLT_ALLMULTI)) |
| 780 | memset(filter->mask, ~0, sizeof(filter->mask)); |
| 781 | |
| 782 | /* Now enable the filter */ |
| 783 | wmb(); |
| 784 | filter->count = nexact; |
| 785 | |
| 786 | /* Return the number of exact filters */ |
| 787 | err = nexact; |
Markus Elfring | 3b8d2a6 | 2016-08-20 09:00:34 +0200 | [diff] [blame] | 788 | free_addr: |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 789 | kfree(addr); |
| 790 | return err; |
| 791 | } |
| 792 | |
| 793 | /* Returns: 0 - drop, !=0 - accept */ |
| 794 | static int run_filter(struct tap_filter *filter, const struct sk_buff *skb) |
| 795 | { |
| 796 | /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect |
| 797 | * at this point. */ |
| 798 | struct ethhdr *eh = (struct ethhdr *) skb->data; |
| 799 | int i; |
| 800 | |
| 801 | /* Exact match */ |
| 802 | for (i = 0; i < filter->count; i++) |
Joe Perches | 2e42e47 | 2012-05-09 17:17:46 +0000 | [diff] [blame] | 803 | if (ether_addr_equal(eh->h_dest, filter->addr[i])) |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 804 | return 1; |
| 805 | |
| 806 | /* Inexact match (multicast only) */ |
| 807 | if (is_multicast_ether_addr(eh->h_dest)) |
| 808 | return addr_hash_test(filter->mask, eh->h_dest); |
| 809 | |
| 810 | return 0; |
| 811 | } |
| 812 | |
| 813 | /* |
| 814 | * Checks whether the packet is accepted or not. |
| 815 | * Returns: 0 - drop, !=0 - accept |
| 816 | */ |
| 817 | static int check_filter(struct tap_filter *filter, const struct sk_buff *skb) |
| 818 | { |
| 819 | if (!filter->count) |
| 820 | return 1; |
| 821 | |
| 822 | return run_filter(filter, skb); |
| 823 | } |
| 824 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | /* Network device part of the driver */ |
| 826 | |
Jeff Garzik | 7282d49 | 2006-09-13 14:30:00 -0400 | [diff] [blame] | 827 | static const struct ethtool_ops tun_ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 829 | /* Net device detach from fd. */ |
| 830 | static void tun_net_uninit(struct net_device *dev) |
| 831 | { |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 832 | tun_detach_all(dev); |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | /* Net device open. */ |
| 836 | static int tun_net_open(struct net_device *dev) |
| 837 | { |
Hannes Frederic Sowa | b20e2d5 | 2017-03-13 00:00:26 +0100 | [diff] [blame] | 838 | struct tun_struct *tun = netdev_priv(dev); |
| 839 | int i; |
| 840 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 841 | netif_tx_start_all_queues(dev); |
Hannes Frederic Sowa | b20e2d5 | 2017-03-13 00:00:26 +0100 | [diff] [blame] | 842 | |
| 843 | for (i = 0; i < tun->numqueues; i++) { |
| 844 | struct tun_file *tfile; |
| 845 | |
| 846 | tfile = rtnl_dereference(tun->tfiles[i]); |
| 847 | tfile->socket.sk->sk_write_space(tfile->socket.sk); |
| 848 | } |
| 849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | /* Net device close. */ |
| 854 | static int tun_net_close(struct net_device *dev) |
| 855 | { |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 856 | netif_tx_stop_all_queues(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | return 0; |
| 858 | } |
| 859 | |
| 860 | /* Net device start xmit */ |
Stephen Hemminger | 424efe9 | 2009-08-31 19:50:51 +0000 | [diff] [blame] | 861 | 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] | 862 | { |
| 863 | struct tun_struct *tun = netdev_priv(dev); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 864 | int txq = skb->queue_mapping; |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 865 | struct tun_file *tfile; |
Dominic Curran | fa35864 | 2014-01-22 03:03:23 +0000 | [diff] [blame] | 866 | u32 numqueues = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 868 | rcu_read_lock(); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 869 | tfile = rcu_dereference(tun->tfiles[txq]); |
Dominic Curran | fa35864 | 2014-01-22 03:03:23 +0000 | [diff] [blame] | 870 | numqueues = ACCESS_ONCE(tun->numqueues); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 871 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | /* Drop packet if interface is not attached */ |
Dominic Curran | fa35864 | 2014-01-22 03:03:23 +0000 | [diff] [blame] | 873 | if (txq >= numqueues) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | goto drop; |
| 875 | |
Jason Wang | 3df97ba | 2016-04-25 23:13:42 -0400 | [diff] [blame] | 876 | #ifdef CONFIG_RPS |
| 877 | if (numqueues == 1 && static_key_false(&rps_needed)) { |
Tom Herbert | 9bc8893 | 2013-12-22 18:54:32 +0800 | [diff] [blame] | 878 | /* Select queue was not called for the skbuff, so we extract the |
| 879 | * RPS hash and save it into the flow_table here. |
| 880 | */ |
| 881 | __u32 rxhash; |
| 882 | |
Jason Wang | feec084 | 2017-06-06 14:09:49 +0800 | [diff] [blame] | 883 | rxhash = __skb_get_hash_symmetric(skb); |
Tom Herbert | 9bc8893 | 2013-12-22 18:54:32 +0800 | [diff] [blame] | 884 | if (rxhash) { |
| 885 | struct tun_flow_entry *e; |
| 886 | e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)], |
| 887 | rxhash); |
| 888 | if (e) |
| 889 | tun_flow_save_rps_rxhash(e, rxhash); |
| 890 | } |
| 891 | } |
Jason Wang | 3df97ba | 2016-04-25 23:13:42 -0400 | [diff] [blame] | 892 | #endif |
Tom Herbert | 9bc8893 | 2013-12-22 18:54:32 +0800 | [diff] [blame] | 893 | |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 894 | tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len); |
| 895 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 896 | BUG_ON(!tfile); |
| 897 | |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 898 | /* Drop if the filter does not like it. |
| 899 | * This is a noop if the filter is disabled. |
| 900 | * Filter can be enabled only for the TAP devices. */ |
| 901 | if (!check_filter(&tun->txflt, skb)) |
| 902 | goto drop; |
| 903 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 904 | if (tfile->socket.sk->sk_filter && |
| 905 | sk_filter(tfile->socket.sk, skb)) |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 906 | goto drop; |
| 907 | |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 908 | if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) |
Jason Wang | 7bf6630 | 2013-09-05 17:54:00 +0800 | [diff] [blame] | 909 | goto drop; |
| 910 | |
Soheil Hassas Yeganeh | 7b99624 | 2016-08-23 18:22:33 -0400 | [diff] [blame] | 911 | skb_tx_timestamp(skb); |
Richard Cochran | eda2977 | 2013-07-19 19:40:10 +0200 | [diff] [blame] | 912 | |
Michael S. Tsirkin | 0110d6f | 2010-04-13 04:59:44 +0000 | [diff] [blame] | 913 | /* Orphan the skb - required as we might hang on to it |
Jason Wang | 7bf6630 | 2013-09-05 17:54:00 +0800 | [diff] [blame] | 914 | * for indefinite time. |
| 915 | */ |
Michael S. Tsirkin | 0110d6f | 2010-04-13 04:59:44 +0000 | [diff] [blame] | 916 | skb_orphan(skb); |
| 917 | |
Eric Dumazet | f8af75f | 2013-03-06 11:02:37 +0000 | [diff] [blame] | 918 | nf_reset(skb); |
| 919 | |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 920 | if (skb_array_produce(&tfile->tx_array, skb)) |
| 921 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | |
| 923 | /* Notify and wake up reader process */ |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 924 | if (tfile->flags & TUN_FASYNC) |
| 925 | kill_fasync(&tfile->fasync, SIGIO, POLL_IN); |
Xi Wang | 9e641bd | 2014-05-16 15:11:48 -0700 | [diff] [blame] | 926 | tfile->socket.sk->sk_data_ready(tfile->socket.sk); |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 927 | |
| 928 | rcu_read_unlock(); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 929 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | |
| 931 | drop: |
Paolo Abeni | 608b997 | 2016-04-13 10:52:20 +0200 | [diff] [blame] | 932 | this_cpu_inc(tun->pcpu_stats->tx_dropped); |
Michael S. Tsirkin | 149d36f | 2012-11-01 09:16:32 +0000 | [diff] [blame] | 933 | skb_tx_error(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | kfree_skb(skb); |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 935 | rcu_read_unlock(); |
Jason Wang | baeabab | 2014-11-18 13:20:41 +0800 | [diff] [blame] | 936 | return NET_XMIT_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 939 | static void tun_net_mclist(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | { |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 941 | /* |
| 942 | * This callback is supposed to deal with mc filter in |
| 943 | * _rx_ path and has nothing to do with the _tx_ path. |
| 944 | * In rx path we always accept everything userspace gives us. |
| 945 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 948 | static netdev_features_t tun_net_fix_features(struct net_device *dev, |
| 949 | netdev_features_t features) |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 950 | { |
| 951 | struct tun_struct *tun = netdev_priv(dev); |
| 952 | |
| 953 | return (features & tun->set_features) | (features & ~TUN_USER_FEATURES); |
| 954 | } |
Neil Horman | bebd097 | 2011-06-15 05:25:01 +0000 | [diff] [blame] | 955 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 956 | static void tun_poll_controller(struct net_device *dev) |
| 957 | { |
| 958 | /* |
| 959 | * Tun only receives frames when: |
| 960 | * 1) the char device endpoint gets data from user space |
| 961 | * 2) the tun socket gets a sendmsg call from user space |
stephen hemminger | 92d4ea6 | 2013-12-05 20:42:58 -0800 | [diff] [blame] | 962 | * Since both of those are synchronous operations, we are guaranteed |
Neil Horman | bebd097 | 2011-06-15 05:25:01 +0000 | [diff] [blame] | 963 | * never to have pending data when we poll for it |
stephen hemminger | 92d4ea6 | 2013-12-05 20:42:58 -0800 | [diff] [blame] | 964 | * so there is nothing to do here but return. |
Neil Horman | bebd097 | 2011-06-15 05:25:01 +0000 | [diff] [blame] | 965 | * We need this though so netpoll recognizes us as an interface that |
| 966 | * supports polling, which enables bridge devices in virt setups to |
| 967 | * still use netconsole |
| 968 | */ |
| 969 | return; |
| 970 | } |
| 971 | #endif |
Paolo Abeni | eaea34b | 2016-02-26 10:45:40 +0100 | [diff] [blame] | 972 | |
| 973 | static void tun_set_headroom(struct net_device *dev, int new_hr) |
| 974 | { |
| 975 | struct tun_struct *tun = netdev_priv(dev); |
| 976 | |
| 977 | if (new_hr < NET_SKB_PAD) |
| 978 | new_hr = NET_SKB_PAD; |
| 979 | |
| 980 | tun->align = new_hr; |
| 981 | } |
| 982 | |
stephen hemminger | bc1f447 | 2017-01-06 19:12:52 -0800 | [diff] [blame] | 983 | static void |
Paolo Abeni | 608b997 | 2016-04-13 10:52:20 +0200 | [diff] [blame] | 984 | tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) |
| 985 | { |
| 986 | u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0; |
| 987 | struct tun_struct *tun = netdev_priv(dev); |
| 988 | struct tun_pcpu_stats *p; |
| 989 | int i; |
| 990 | |
| 991 | for_each_possible_cpu(i) { |
| 992 | u64 rxpackets, rxbytes, txpackets, txbytes; |
| 993 | unsigned int start; |
| 994 | |
| 995 | p = per_cpu_ptr(tun->pcpu_stats, i); |
| 996 | do { |
| 997 | start = u64_stats_fetch_begin(&p->syncp); |
| 998 | rxpackets = p->rx_packets; |
| 999 | rxbytes = p->rx_bytes; |
| 1000 | txpackets = p->tx_packets; |
| 1001 | txbytes = p->tx_bytes; |
| 1002 | } while (u64_stats_fetch_retry(&p->syncp, start)); |
| 1003 | |
| 1004 | stats->rx_packets += rxpackets; |
| 1005 | stats->rx_bytes += rxbytes; |
| 1006 | stats->tx_packets += txpackets; |
| 1007 | stats->tx_bytes += txbytes; |
| 1008 | |
| 1009 | /* u32 counters */ |
| 1010 | rx_dropped += p->rx_dropped; |
| 1011 | rx_frame_errors += p->rx_frame_errors; |
| 1012 | tx_dropped += p->tx_dropped; |
| 1013 | } |
| 1014 | stats->rx_dropped = rx_dropped; |
| 1015 | stats->rx_frame_errors = rx_frame_errors; |
| 1016 | stats->tx_dropped = tx_dropped; |
Paolo Abeni | 608b997 | 2016-04-13 10:52:20 +0200 | [diff] [blame] | 1017 | } |
| 1018 | |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 1019 | static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog, |
| 1020 | struct netlink_ext_ack *extack) |
| 1021 | { |
| 1022 | struct tun_struct *tun = netdev_priv(dev); |
| 1023 | struct bpf_prog *old_prog; |
| 1024 | |
| 1025 | old_prog = rtnl_dereference(tun->xdp_prog); |
| 1026 | rcu_assign_pointer(tun->xdp_prog, prog); |
| 1027 | if (old_prog) |
| 1028 | bpf_prog_put(old_prog); |
| 1029 | |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | static u32 tun_xdp_query(struct net_device *dev) |
| 1034 | { |
| 1035 | struct tun_struct *tun = netdev_priv(dev); |
| 1036 | const struct bpf_prog *xdp_prog; |
| 1037 | |
| 1038 | xdp_prog = rtnl_dereference(tun->xdp_prog); |
| 1039 | if (xdp_prog) |
| 1040 | return xdp_prog->aux->id; |
| 1041 | |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | static int tun_xdp(struct net_device *dev, struct netdev_xdp *xdp) |
| 1046 | { |
| 1047 | switch (xdp->command) { |
| 1048 | case XDP_SETUP_PROG: |
| 1049 | return tun_xdp_set(dev, xdp->prog, xdp->extack); |
| 1050 | case XDP_QUERY_PROG: |
| 1051 | xdp->prog_id = tun_xdp_query(dev); |
| 1052 | xdp->prog_attached = !!xdp->prog_id; |
| 1053 | return 0; |
| 1054 | default: |
| 1055 | return -EINVAL; |
| 1056 | } |
| 1057 | } |
| 1058 | |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 1059 | static const struct net_device_ops tun_netdev_ops = { |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 1060 | .ndo_uninit = tun_net_uninit, |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 1061 | .ndo_open = tun_net_open, |
| 1062 | .ndo_stop = tun_net_close, |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 1063 | .ndo_start_xmit = tun_net_xmit, |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 1064 | .ndo_fix_features = tun_net_fix_features, |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 1065 | .ndo_select_queue = tun_select_queue, |
Neil Horman | bebd097 | 2011-06-15 05:25:01 +0000 | [diff] [blame] | 1066 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1067 | .ndo_poll_controller = tun_poll_controller, |
| 1068 | #endif |
Paolo Abeni | eaea34b | 2016-02-26 10:45:40 +0100 | [diff] [blame] | 1069 | .ndo_set_rx_headroom = tun_set_headroom, |
Paolo Abeni | 608b997 | 2016-04-13 10:52:20 +0200 | [diff] [blame] | 1070 | .ndo_get_stats64 = tun_net_get_stats64, |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 1071 | }; |
| 1072 | |
| 1073 | static const struct net_device_ops tap_netdev_ops = { |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 1074 | .ndo_uninit = tun_net_uninit, |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 1075 | .ndo_open = tun_net_open, |
| 1076 | .ndo_stop = tun_net_close, |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 1077 | .ndo_start_xmit = tun_net_xmit, |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 1078 | .ndo_fix_features = tun_net_fix_features, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 1079 | .ndo_set_rx_mode = tun_net_mclist, |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 1080 | .ndo_set_mac_address = eth_mac_addr, |
| 1081 | .ndo_validate_addr = eth_validate_addr, |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 1082 | .ndo_select_queue = tun_select_queue, |
Neil Horman | bebd097 | 2011-06-15 05:25:01 +0000 | [diff] [blame] | 1083 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1084 | .ndo_poll_controller = tun_poll_controller, |
| 1085 | #endif |
Toshiaki Makita | 5e52796 | 2015-07-31 15:03:27 +0900 | [diff] [blame] | 1086 | .ndo_features_check = passthru_features_check, |
Paolo Abeni | eaea34b | 2016-02-26 10:45:40 +0100 | [diff] [blame] | 1087 | .ndo_set_rx_headroom = tun_set_headroom, |
Paolo Abeni | 608b997 | 2016-04-13 10:52:20 +0200 | [diff] [blame] | 1088 | .ndo_get_stats64 = tun_net_get_stats64, |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 1089 | .ndo_xdp = tun_xdp, |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 1090 | }; |
| 1091 | |
Pavel Emelyanov | 944a137 | 2013-06-11 17:01:08 +0400 | [diff] [blame] | 1092 | static void tun_flow_init(struct tun_struct *tun) |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 1093 | { |
| 1094 | int i; |
| 1095 | |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 1096 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) |
| 1097 | INIT_HLIST_HEAD(&tun->flows[i]); |
| 1098 | |
| 1099 | tun->ageing_time = TUN_FLOW_EXPIRE; |
| 1100 | setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun); |
| 1101 | mod_timer(&tun->flow_gc_timer, |
| 1102 | round_jiffies_up(jiffies + tun->ageing_time)); |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | static void tun_flow_uninit(struct tun_struct *tun) |
| 1106 | { |
| 1107 | del_timer_sync(&tun->flow_gc_timer); |
| 1108 | tun_flow_flush(tun); |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Jarod Wilson | 9157208 | 2016-10-20 13:55:20 -0400 | [diff] [blame] | 1111 | #define MIN_MTU 68 |
| 1112 | #define MAX_MTU 65535 |
| 1113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | /* Initialize net device. */ |
| 1115 | static void tun_net_init(struct net_device *dev) |
| 1116 | { |
| 1117 | struct tun_struct *tun = netdev_priv(dev); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | switch (tun->flags & TUN_TYPE_MASK) { |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 1120 | case IFF_TUN: |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 1121 | dev->netdev_ops = &tun_netdev_ops; |
| 1122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | /* Point-to-Point TUN Device */ |
| 1124 | dev->hard_header_len = 0; |
| 1125 | dev->addr_len = 0; |
| 1126 | dev->mtu = 1500; |
| 1127 | |
| 1128 | /* Zero header length */ |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1129 | dev->type = ARPHRD_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | break; |
| 1132 | |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 1133 | case IFF_TAP: |
Kusanagi Kouichi | 7a0a960 | 2008-12-29 18:23:28 -0800 | [diff] [blame] | 1134 | dev->netdev_ops = &tap_netdev_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | /* Ethernet TAP Device */ |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 1136 | ether_setup(dev); |
Neil Horman | 550fd08 | 2011-07-26 06:05:38 +0000 | [diff] [blame] | 1137 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
stephen hemminger | a676847 | 2012-12-10 15:16:00 +0000 | [diff] [blame] | 1138 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | |
Danny Kukawka | f2cedb6 | 2012-02-15 06:45:39 +0000 | [diff] [blame] | 1140 | eth_hw_addr_random(dev); |
Brian Braunstein | 36226a8 | 2007-04-26 01:00:55 -0700 | [diff] [blame] | 1141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | break; |
| 1143 | } |
Jarod Wilson | 9157208 | 2016-10-20 13:55:20 -0400 | [diff] [blame] | 1144 | |
| 1145 | dev->min_mtu = MIN_MTU; |
| 1146 | dev->max_mtu = MAX_MTU - dev->hard_header_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | /* Character device part */ |
| 1150 | |
| 1151 | /* Poll */ |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 1152 | static unsigned int tun_chr_poll(struct file *file, poll_table *wait) |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1153 | { |
Eric W. Biederman | b2430de | 2009-01-20 11:03:21 +0000 | [diff] [blame] | 1154 | struct tun_file *tfile = file->private_data; |
| 1155 | struct tun_struct *tun = __tun_get(tfile); |
Mariusz Kozlowski | 3c8a9c6 | 2009-07-05 19:48:35 +0000 | [diff] [blame] | 1156 | struct sock *sk; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1157 | unsigned int mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | |
| 1159 | if (!tun) |
Eric W. Biederman | eac9e90 | 2009-01-20 10:59:05 +0000 | [diff] [blame] | 1160 | return POLLERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1162 | sk = tfile->socket.sk; |
Mariusz Kozlowski | 3c8a9c6 | 2009-07-05 19:48:35 +0000 | [diff] [blame] | 1163 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1164 | tun_debug(KERN_INFO, tun, "tun_chr_poll\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | |
Xi Wang | 9e641bd | 2014-05-16 15:11:48 -0700 | [diff] [blame] | 1166 | poll_wait(file, sk_sleep(sk), wait); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1167 | |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 1168 | if (!skb_array_empty(&tfile->tx_array)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | mask |= POLLIN | POLLRDNORM; |
| 1170 | |
Hannes Frederic Sowa | b20e2d5 | 2017-03-13 00:00:26 +0100 | [diff] [blame] | 1171 | if (tun->dev->flags & IFF_UP && |
| 1172 | (sock_writeable(sk) || |
| 1173 | (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) && |
| 1174 | sock_writeable(sk)))) |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1175 | mask |= POLLOUT | POLLWRNORM; |
| 1176 | |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 1177 | if (tun->dev->reg_state != NETREG_REGISTERED) |
| 1178 | mask = POLLERR; |
| 1179 | |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1180 | tun_put(tun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | return mask; |
| 1182 | } |
| 1183 | |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 1184 | /* prepad is the amount to reserve at front. len is length after that. |
| 1185 | * linear is a hint as to how much to copy (usually headers). */ |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1186 | static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, |
stephen hemminger | 6f7c156 | 2011-06-08 14:33:08 +0000 | [diff] [blame] | 1187 | size_t prepad, size_t len, |
| 1188 | size_t linear, int noblock) |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 1189 | { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1190 | struct sock *sk = tfile->socket.sk; |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 1191 | struct sk_buff *skb; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1192 | int err; |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 1193 | |
| 1194 | /* Under a page? Don't bother with paged skb. */ |
Herbert Xu | 0eca93b | 2009-04-14 02:09:43 -0700 | [diff] [blame] | 1195 | if (prepad + len < PAGE_SIZE || !linear) |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1196 | linear = len; |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 1197 | |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1198 | skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, |
Eric Dumazet | 28d6427 | 2013-08-08 14:38:47 -0700 | [diff] [blame] | 1199 | &err, 0); |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 1200 | if (!skb) |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1201 | return ERR_PTR(err); |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 1202 | |
| 1203 | skb_reserve(skb, prepad); |
| 1204 | skb_put(skb, linear); |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1205 | skb->data_len = len - linear; |
| 1206 | skb->len += len - linear; |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 1207 | |
| 1208 | return skb; |
| 1209 | } |
| 1210 | |
Jason Wang | 5503fce | 2017-01-18 15:02:03 +0800 | [diff] [blame] | 1211 | static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile, |
| 1212 | struct sk_buff *skb, int more) |
| 1213 | { |
| 1214 | struct sk_buff_head *queue = &tfile->sk.sk_write_queue; |
| 1215 | struct sk_buff_head process_queue; |
| 1216 | u32 rx_batched = tun->rx_batched; |
| 1217 | bool rcv = false; |
| 1218 | |
| 1219 | if (!rx_batched || (!more && skb_queue_empty(queue))) { |
| 1220 | local_bh_disable(); |
| 1221 | netif_receive_skb(skb); |
| 1222 | local_bh_enable(); |
| 1223 | return; |
| 1224 | } |
| 1225 | |
| 1226 | spin_lock(&queue->lock); |
| 1227 | if (!more || skb_queue_len(queue) == rx_batched) { |
| 1228 | __skb_queue_head_init(&process_queue); |
| 1229 | skb_queue_splice_tail_init(queue, &process_queue); |
| 1230 | rcv = true; |
| 1231 | } else { |
| 1232 | __skb_queue_tail(queue, skb); |
| 1233 | } |
| 1234 | spin_unlock(&queue->lock); |
| 1235 | |
| 1236 | if (rcv) { |
| 1237 | struct sk_buff *nskb; |
| 1238 | |
| 1239 | local_bh_disable(); |
| 1240 | while ((nskb = __skb_dequeue(&process_queue))) |
| 1241 | netif_receive_skb(nskb); |
| 1242 | netif_receive_skb(skb); |
| 1243 | local_bh_enable(); |
| 1244 | } |
| 1245 | } |
| 1246 | |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 1247 | static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile, |
| 1248 | int len, int noblock, bool zerocopy) |
| 1249 | { |
| 1250 | if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) |
| 1251 | return false; |
| 1252 | |
| 1253 | if (tfile->socket.sk->sk_sndbuf != INT_MAX) |
| 1254 | return false; |
| 1255 | |
| 1256 | if (!noblock) |
| 1257 | return false; |
| 1258 | |
| 1259 | if (zerocopy) |
| 1260 | return false; |
| 1261 | |
| 1262 | if (SKB_DATA_ALIGN(len + TUN_RX_PAD) + |
| 1263 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE) |
| 1264 | return false; |
| 1265 | |
| 1266 | return true; |
| 1267 | } |
| 1268 | |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 1269 | static struct sk_buff *tun_build_skb(struct tun_struct *tun, |
| 1270 | struct tun_file *tfile, |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 1271 | struct iov_iter *from, |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 1272 | struct virtio_net_hdr *hdr, |
| 1273 | int len, int *generic_xdp) |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 1274 | { |
| 1275 | struct page_frag *alloc_frag = &tfile->alloc_frag; |
| 1276 | struct sk_buff *skb; |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 1277 | struct bpf_prog *xdp_prog; |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 1278 | int buflen = SKB_DATA_ALIGN(len + TUN_RX_PAD) + |
| 1279 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 1280 | unsigned int delta = 0; |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 1281 | char *buf; |
| 1282 | size_t copied; |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 1283 | bool xdp_xmit = false; |
| 1284 | int err; |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 1285 | |
| 1286 | if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL))) |
| 1287 | return ERR_PTR(-ENOMEM); |
| 1288 | |
| 1289 | buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; |
| 1290 | copied = copy_page_from_iter(alloc_frag->page, |
| 1291 | alloc_frag->offset + TUN_RX_PAD, |
| 1292 | len, from); |
| 1293 | if (copied != len) |
| 1294 | return ERR_PTR(-EFAULT); |
| 1295 | |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 1296 | if (hdr->gso_type) |
| 1297 | *generic_xdp = 1; |
| 1298 | else |
| 1299 | *generic_xdp = 0; |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 1300 | |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 1301 | rcu_read_lock(); |
| 1302 | xdp_prog = rcu_dereference(tun->xdp_prog); |
| 1303 | if (xdp_prog && !*generic_xdp) { |
| 1304 | struct xdp_buff xdp; |
| 1305 | void *orig_data; |
| 1306 | u32 act; |
| 1307 | |
| 1308 | xdp.data_hard_start = buf; |
| 1309 | xdp.data = buf + TUN_RX_PAD; |
| 1310 | xdp.data_end = xdp.data + len; |
| 1311 | orig_data = xdp.data; |
| 1312 | act = bpf_prog_run_xdp(xdp_prog, &xdp); |
| 1313 | |
| 1314 | switch (act) { |
| 1315 | case XDP_REDIRECT: |
| 1316 | get_page(alloc_frag->page); |
| 1317 | alloc_frag->offset += buflen; |
| 1318 | err = xdp_do_redirect(tun->dev, &xdp, xdp_prog); |
| 1319 | if (err) |
| 1320 | goto err_redirect; |
| 1321 | return NULL; |
| 1322 | case XDP_TX: |
| 1323 | xdp_xmit = true; |
| 1324 | /* fall through */ |
| 1325 | case XDP_PASS: |
| 1326 | delta = orig_data - xdp.data; |
| 1327 | break; |
| 1328 | default: |
| 1329 | bpf_warn_invalid_xdp_action(act); |
| 1330 | /* fall through */ |
| 1331 | case XDP_ABORTED: |
| 1332 | trace_xdp_exception(tun->dev, xdp_prog, act); |
| 1333 | /* fall through */ |
| 1334 | case XDP_DROP: |
| 1335 | goto err_xdp; |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | skb = build_skb(buf, buflen); |
| 1340 | if (!skb) { |
| 1341 | rcu_read_unlock(); |
| 1342 | return ERR_PTR(-ENOMEM); |
| 1343 | } |
| 1344 | |
| 1345 | skb_reserve(skb, TUN_RX_PAD - delta); |
| 1346 | skb_put(skb, len + delta); |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 1347 | get_page(alloc_frag->page); |
| 1348 | alloc_frag->offset += buflen; |
| 1349 | |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 1350 | if (xdp_xmit) { |
| 1351 | skb->dev = tun->dev; |
| 1352 | generic_xdp_tx(skb, xdp_prog); |
| 1353 | rcu_read_lock(); |
| 1354 | return NULL; |
| 1355 | } |
| 1356 | |
| 1357 | rcu_read_unlock(); |
| 1358 | |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 1359 | return skb; |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 1360 | |
| 1361 | err_redirect: |
| 1362 | put_page(alloc_frag->page); |
| 1363 | err_xdp: |
| 1364 | rcu_read_unlock(); |
| 1365 | this_cpu_inc(tun->pcpu_stats->rx_dropped); |
| 1366 | return NULL; |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 1367 | } |
| 1368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | /* Get packet from user space buffer */ |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1370 | static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, |
Al Viro | f5ff53b | 2014-06-19 15:36:49 -0400 | [diff] [blame] | 1371 | void *msg_control, struct iov_iter *from, |
Jason Wang | 5503fce | 2017-01-18 15:02:03 +0800 | [diff] [blame] | 1372 | int noblock, bool more) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | { |
Harvey Harrison | 09640e63 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 1374 | struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | struct sk_buff *skb; |
Al Viro | f5ff53b | 2014-06-19 15:36:49 -0400 | [diff] [blame] | 1376 | size_t total_len = iov_iter_count(from); |
Paolo Abeni | eaea34b | 2016-02-26 10:45:40 +0100 | [diff] [blame] | 1377 | size_t len = total_len, align = tun->align, linear; |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1378 | struct virtio_net_hdr gso = { 0 }; |
Paolo Abeni | 608b997 | 2016-04-13 10:52:20 +0200 | [diff] [blame] | 1379 | struct tun_pcpu_stats *stats; |
Jason Wang | 96f8d9e | 2013-11-13 14:00:39 +0800 | [diff] [blame] | 1380 | int good_linear; |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 1381 | int copylen; |
| 1382 | bool zerocopy = false; |
| 1383 | int err; |
Eric Dumazet | 4997442 | 2012-12-12 19:22:57 +0000 | [diff] [blame] | 1384 | u32 rxhash; |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 1385 | int generic_xdp = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | |
Eric Dumazet | 1bd4978a | 2015-12-16 08:57:37 -0800 | [diff] [blame] | 1387 | if (!(tun->dev->flags & IFF_UP)) |
| 1388 | return -EIO; |
| 1389 | |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 1390 | if (!(tun->flags & IFF_NO_PI)) { |
Dan Carpenter | 15718ea | 2013-08-15 15:52:57 +0300 | [diff] [blame] | 1391 | if (len < sizeof(pi)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | return -EINVAL; |
Dan Carpenter | 15718ea | 2013-08-15 15:52:57 +0300 | [diff] [blame] | 1393 | len -= sizeof(pi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 1395 | if (!copy_from_iter_full(&pi, sizeof(pi), from)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | return -EFAULT; |
| 1397 | } |
| 1398 | |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 1399 | if (tun->flags & IFF_VNET_HDR) { |
Willem de Bruijn | e1edab8 | 2017-02-03 18:20:48 -0500 | [diff] [blame] | 1400 | int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); |
| 1401 | |
| 1402 | if (len < vnet_hdr_sz) |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1403 | return -EINVAL; |
Willem de Bruijn | e1edab8 | 2017-02-03 18:20:48 -0500 | [diff] [blame] | 1404 | len -= vnet_hdr_sz; |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1405 | |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 1406 | if (!copy_from_iter_full(&gso, sizeof(gso), from)) |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1407 | return -EFAULT; |
| 1408 | |
Herbert Xu | 4909122 | 2009-06-08 00:20:01 -0700 | [diff] [blame] | 1409 | if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && |
Michael S. Tsirkin | 56f0dcc | 2014-10-23 22:59:31 +0300 | [diff] [blame] | 1410 | tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len)) |
| 1411 | gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2); |
Herbert Xu | 4909122 | 2009-06-08 00:20:01 -0700 | [diff] [blame] | 1412 | |
Michael S. Tsirkin | 56f0dcc | 2014-10-23 22:59:31 +0300 | [diff] [blame] | 1413 | if (tun16_to_cpu(tun, gso.hdr_len) > len) |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1414 | return -EINVAL; |
Willem de Bruijn | e1edab8 | 2017-02-03 18:20:48 -0500 | [diff] [blame] | 1415 | iov_iter_advance(from, vnet_hdr_sz - sizeof(gso)); |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1416 | } |
| 1417 | |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 1418 | if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) { |
stephen hemminger | a504b86 | 2011-06-08 14:33:07 +0000 | [diff] [blame] | 1419 | align += NET_IP_ALIGN; |
Herbert Xu | 0eca93b | 2009-04-14 02:09:43 -0700 | [diff] [blame] | 1420 | if (unlikely(len < ETH_HLEN || |
Michael S. Tsirkin | 56f0dcc | 2014-10-23 22:59:31 +0300 | [diff] [blame] | 1421 | (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN))) |
Rusty Russell | e01bf1c | 2008-04-12 18:49:30 -0700 | [diff] [blame] | 1422 | return -EINVAL; |
| 1423 | } |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1424 | |
Jason Wang | 96f8d9e | 2013-11-13 14:00:39 +0800 | [diff] [blame] | 1425 | good_linear = SKB_MAX_HEAD(align); |
| 1426 | |
Jason Wang | 8852917 | 2013-07-18 10:55:15 +0800 | [diff] [blame] | 1427 | if (msg_control) { |
Al Viro | f5ff53b | 2014-06-19 15:36:49 -0400 | [diff] [blame] | 1428 | struct iov_iter i = *from; |
| 1429 | |
Jason Wang | 8852917 | 2013-07-18 10:55:15 +0800 | [diff] [blame] | 1430 | /* There are 256 bytes to be copied in skb, so there is |
| 1431 | * enough room for skb expand head in case it is used. |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 1432 | * The rest of the buffer is mapped from userspace. |
| 1433 | */ |
Michael S. Tsirkin | 56f0dcc | 2014-10-23 22:59:31 +0300 | [diff] [blame] | 1434 | copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN; |
Jason Wang | 96f8d9e | 2013-11-13 14:00:39 +0800 | [diff] [blame] | 1435 | if (copylen > good_linear) |
| 1436 | copylen = good_linear; |
Jason Wang | 3dd5c33 | 2013-07-10 13:43:27 +0800 | [diff] [blame] | 1437 | linear = copylen; |
Al Viro | f5ff53b | 2014-06-19 15:36:49 -0400 | [diff] [blame] | 1438 | iov_iter_advance(&i, copylen); |
| 1439 | if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS) |
Jason Wang | 8852917 | 2013-07-18 10:55:15 +0800 | [diff] [blame] | 1440 | zerocopy = true; |
| 1441 | } |
| 1442 | |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 1443 | if (tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) { |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 1444 | skb = tun_build_skb(tun, tfile, from, &gso, len, &generic_xdp); |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 1445 | if (IS_ERR(skb)) { |
Paolo Abeni | 608b997 | 2016-04-13 10:52:20 +0200 | [diff] [blame] | 1446 | this_cpu_inc(tun->pcpu_stats->rx_dropped); |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 1447 | return PTR_ERR(skb); |
| 1448 | } |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 1449 | if (!skb) |
| 1450 | return total_len; |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 1451 | } else { |
| 1452 | if (!zerocopy) { |
| 1453 | copylen = len; |
| 1454 | if (tun16_to_cpu(tun, gso.hdr_len) > good_linear) |
| 1455 | linear = good_linear; |
| 1456 | else |
| 1457 | linear = tun16_to_cpu(tun, gso.hdr_len); |
| 1458 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 1460 | skb = tun_alloc_skb(tfile, align, copylen, linear, noblock); |
| 1461 | if (IS_ERR(skb)) { |
| 1462 | if (PTR_ERR(skb) != -EAGAIN) |
| 1463 | this_cpu_inc(tun->pcpu_stats->rx_dropped); |
| 1464 | return PTR_ERR(skb); |
| 1465 | } |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 1466 | |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 1467 | if (zerocopy) |
| 1468 | err = zerocopy_sg_from_iter(skb, from); |
| 1469 | else |
| 1470 | err = skb_copy_datagram_from_iter(skb, 0, from, len); |
| 1471 | |
| 1472 | if (err) { |
| 1473 | this_cpu_inc(tun->pcpu_stats->rx_dropped); |
| 1474 | kfree_skb(skb); |
| 1475 | return -EFAULT; |
| 1476 | } |
Dave Jones | 8f22757 | 2006-03-11 18:49:13 -0800 | [diff] [blame] | 1477 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | |
Jarno Rajahalme | 3e9e40e | 2016-11-18 15:40:38 -0800 | [diff] [blame] | 1479 | if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) { |
Paolo Abeni | df10db9 | 2016-06-14 00:00:04 +0200 | [diff] [blame] | 1480 | this_cpu_inc(tun->pcpu_stats->rx_frame_errors); |
| 1481 | kfree_skb(skb); |
| 1482 | return -EINVAL; |
| 1483 | } |
| 1484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | switch (tun->flags & TUN_TYPE_MASK) { |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 1486 | case IFF_TUN: |
| 1487 | if (tun->flags & IFF_NO_PI) { |
Ang Way Chuang | f09f7ee | 2008-06-17 21:10:33 -0700 | [diff] [blame] | 1488 | switch (skb->data[0] & 0xf0) { |
| 1489 | case 0x40: |
| 1490 | pi.proto = htons(ETH_P_IP); |
| 1491 | break; |
| 1492 | case 0x60: |
| 1493 | pi.proto = htons(ETH_P_IPV6); |
| 1494 | break; |
| 1495 | default: |
Paolo Abeni | 608b997 | 2016-04-13 10:52:20 +0200 | [diff] [blame] | 1496 | this_cpu_inc(tun->pcpu_stats->rx_dropped); |
Ang Way Chuang | f09f7ee | 2008-06-17 21:10:33 -0700 | [diff] [blame] | 1497 | kfree_skb(skb); |
| 1498 | return -EINVAL; |
| 1499 | } |
| 1500 | } |
| 1501 | |
Arnaldo Carvalho de Melo | 459a98e | 2007-03-19 15:30:44 -0700 | [diff] [blame] | 1502 | skb_reset_mac_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | skb->protocol = pi.proto; |
Arnaldo Carvalho de Melo | 4c13eb6 | 2007-04-25 17:40:23 -0700 | [diff] [blame] | 1504 | skb->dev = tun->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | break; |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 1506 | case IFF_TAP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | skb->protocol = eth_type_trans(skb, tun->dev); |
| 1508 | break; |
Joe Perches | 6403eab | 2011-06-03 11:51:20 +0000 | [diff] [blame] | 1509 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 1511 | /* copy skb_ubuf_info for callback when skb has no error */ |
| 1512 | if (zerocopy) { |
| 1513 | skb_shinfo(skb)->destructor_arg = msg_control; |
| 1514 | skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; |
Pravin B Shelar | c9af6db | 2013-02-11 09:27:41 +0000 | [diff] [blame] | 1515 | skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; |
Jason Wang | af1cc7a | 2016-11-30 13:17:51 +0800 | [diff] [blame] | 1516 | } else if (msg_control) { |
| 1517 | struct ubuf_info *uarg = msg_control; |
| 1518 | uarg->callback(uarg, false); |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Vlad Yasevich | 72f6510 | 2015-02-03 16:36:16 -0500 | [diff] [blame] | 1521 | skb_reset_network_header(skb); |
Jason Wang | 40893fd | 2013-03-26 23:11:22 +0000 | [diff] [blame] | 1522 | skb_probe_transport_header(skb, 0); |
Jason Wang | 38502af | 2013-03-25 20:19:56 +0000 | [diff] [blame] | 1523 | |
Jason Wang | 761876c | 2017-08-11 19:41:18 +0800 | [diff] [blame] | 1524 | if (generic_xdp) { |
| 1525 | struct bpf_prog *xdp_prog; |
| 1526 | int ret; |
| 1527 | |
| 1528 | rcu_read_lock(); |
| 1529 | xdp_prog = rcu_dereference(tun->xdp_prog); |
| 1530 | if (xdp_prog) { |
| 1531 | ret = do_xdp_generic(xdp_prog, skb); |
| 1532 | if (ret != XDP_PASS) { |
| 1533 | rcu_read_unlock(); |
| 1534 | return total_len; |
| 1535 | } |
| 1536 | } |
| 1537 | rcu_read_unlock(); |
| 1538 | } |
| 1539 | |
Jason Wang | feec084 | 2017-06-06 14:09:49 +0800 | [diff] [blame] | 1540 | rxhash = __skb_get_hash_symmetric(skb); |
Andrey Konovalov | d4aea20 | 2016-12-01 10:34:40 +0100 | [diff] [blame] | 1541 | #ifndef CONFIG_4KSTACKS |
Jason Wang | 5503fce | 2017-01-18 15:02:03 +0800 | [diff] [blame] | 1542 | tun_rx_batched(tun, tfile, skb, more); |
Andrey Konovalov | d4aea20 | 2016-12-01 10:34:40 +0100 | [diff] [blame] | 1543 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | netif_rx_ni(skb); |
Andrey Konovalov | d4aea20 | 2016-12-01 10:34:40 +0100 | [diff] [blame] | 1545 | #endif |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1546 | |
Paolo Abeni | 608b997 | 2016-04-13 10:52:20 +0200 | [diff] [blame] | 1547 | stats = get_cpu_ptr(tun->pcpu_stats); |
| 1548 | u64_stats_update_begin(&stats->syncp); |
| 1549 | stats->rx_packets++; |
| 1550 | stats->rx_bytes += len; |
| 1551 | u64_stats_update_end(&stats->syncp); |
| 1552 | put_cpu_ptr(stats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | |
Jason Wang | 9e85722 | 2013-01-28 01:05:19 +0000 | [diff] [blame] | 1554 | tun_flow_update(tun, rxhash, tfile); |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 1555 | return total_len; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1556 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | |
Al Viro | f5ff53b | 2014-06-19 15:36:49 -0400 | [diff] [blame] | 1558 | static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | { |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1560 | struct file *file = iocb->ki_filp; |
Herbert Xu | ab46d779 | 2009-02-14 20:46:39 -0800 | [diff] [blame] | 1561 | struct tun_struct *tun = tun_get(file); |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1562 | struct tun_file *tfile = file->private_data; |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1563 | ssize_t result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | |
| 1565 | if (!tun) |
| 1566 | return -EBADFD; |
| 1567 | |
Jason Wang | 5503fce | 2017-01-18 15:02:03 +0800 | [diff] [blame] | 1568 | result = tun_get_user(tun, tfile, NULL, from, |
| 1569 | file->f_flags & O_NONBLOCK, false); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1570 | |
| 1571 | tun_put(tun); |
| 1572 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | /* Put packet to the user space buffer */ |
stephen hemminger | 6f7c156 | 2011-06-08 14:33:08 +0000 | [diff] [blame] | 1576 | static ssize_t tun_put_user(struct tun_struct *tun, |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1577 | struct tun_file *tfile, |
stephen hemminger | 6f7c156 | 2011-06-08 14:33:08 +0000 | [diff] [blame] | 1578 | struct sk_buff *skb, |
Herbert Xu | e0b46d0 | 2014-11-07 21:22:23 +0800 | [diff] [blame] | 1579 | struct iov_iter *iter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | { |
| 1581 | struct tun_pi pi = { 0, skb->protocol }; |
Paolo Abeni | 608b997 | 2016-04-13 10:52:20 +0200 | [diff] [blame] | 1582 | struct tun_pcpu_stats *stats; |
Herbert Xu | e0b46d0 | 2014-11-07 21:22:23 +0800 | [diff] [blame] | 1583 | ssize_t total; |
Jason Wang | 8c847d2 | 2014-11-13 16:54:14 +0800 | [diff] [blame] | 1584 | int vlan_offset = 0; |
Herbert Xu | a8f9bfd | 2014-11-03 04:30:13 +0800 | [diff] [blame] | 1585 | int vlan_hlen = 0; |
Herbert Xu | 2eb783c | 2014-11-03 04:30:14 +0800 | [diff] [blame] | 1586 | int vnet_hdr_sz = 0; |
Herbert Xu | a8f9bfd | 2014-11-03 04:30:13 +0800 | [diff] [blame] | 1587 | |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 1588 | if (skb_vlan_tag_present(skb)) |
Herbert Xu | a8f9bfd | 2014-11-03 04:30:13 +0800 | [diff] [blame] | 1589 | vlan_hlen = VLAN_HLEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 1591 | if (tun->flags & IFF_VNET_HDR) |
Willem de Bruijn | e1edab8 | 2017-02-03 18:20:48 -0500 | [diff] [blame] | 1592 | vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | |
Herbert Xu | e0b46d0 | 2014-11-07 21:22:23 +0800 | [diff] [blame] | 1594 | total = skb->len + vlan_hlen + vnet_hdr_sz; |
| 1595 | |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 1596 | if (!(tun->flags & IFF_NO_PI)) { |
Herbert Xu | e0b46d0 | 2014-11-07 21:22:23 +0800 | [diff] [blame] | 1597 | if (iov_iter_count(iter) < sizeof(pi)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | return -EINVAL; |
| 1599 | |
Herbert Xu | e0b46d0 | 2014-11-07 21:22:23 +0800 | [diff] [blame] | 1600 | total += sizeof(pi); |
| 1601 | if (iov_iter_count(iter) < total) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | /* Packet will be striped */ |
| 1603 | pi.flags |= TUN_PKT_STRIP; |
| 1604 | } |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1605 | |
Herbert Xu | e0b46d0 | 2014-11-07 21:22:23 +0800 | [diff] [blame] | 1606 | if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | return -EFAULT; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1608 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | |
Herbert Xu | 2eb783c | 2014-11-03 04:30:14 +0800 | [diff] [blame] | 1610 | if (vnet_hdr_sz) { |
Jarno Rajahalme | 9403cd7 | 2016-11-18 15:40:40 -0800 | [diff] [blame] | 1611 | struct virtio_net_hdr gso; |
Mike Rapoport | 3416609 | 2016-06-08 16:09:20 +0300 | [diff] [blame] | 1612 | |
Herbert Xu | e0b46d0 | 2014-11-07 21:22:23 +0800 | [diff] [blame] | 1613 | if (iov_iter_count(iter) < vnet_hdr_sz) |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1614 | return -EINVAL; |
| 1615 | |
Jarno Rajahalme | 3e9e40e | 2016-11-18 15:40:38 -0800 | [diff] [blame] | 1616 | if (virtio_net_hdr_from_skb(skb, &gso, |
Jason Wang | 6391a44 | 2017-01-20 14:32:42 +0800 | [diff] [blame] | 1617 | tun_is_little_endian(tun), true)) { |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1618 | struct skb_shared_info *sinfo = skb_shinfo(skb); |
Mike Rapoport | 3416609 | 2016-06-08 16:09:20 +0300 | [diff] [blame] | 1619 | pr_err("unexpected GSO type: " |
| 1620 | "0x%x, gso_size %d, hdr_len %d\n", |
| 1621 | sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size), |
| 1622 | tun16_to_cpu(tun, gso.hdr_len)); |
| 1623 | print_hex_dump(KERN_ERR, "tun: ", |
| 1624 | DUMP_PREFIX_NONE, |
| 1625 | 16, 1, skb->head, |
| 1626 | min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true); |
| 1627 | WARN_ON_ONCE(1); |
| 1628 | return -EINVAL; |
| 1629 | } |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1630 | |
Herbert Xu | e0b46d0 | 2014-11-07 21:22:23 +0800 | [diff] [blame] | 1631 | if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso)) |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1632 | return -EFAULT; |
Jason Wang | 8c847d2 | 2014-11-13 16:54:14 +0800 | [diff] [blame] | 1633 | |
| 1634 | iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso)); |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1635 | } |
| 1636 | |
Herbert Xu | a8f9bfd | 2014-11-03 04:30:13 +0800 | [diff] [blame] | 1637 | if (vlan_hlen) { |
Herbert Xu | e0b46d0 | 2014-11-07 21:22:23 +0800 | [diff] [blame] | 1638 | int ret; |
Jason Wang | 6680ec6 | 2013-07-25 13:00:33 +0800 | [diff] [blame] | 1639 | struct { |
| 1640 | __be16 h_vlan_proto; |
| 1641 | __be16 h_vlan_TCI; |
| 1642 | } veth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | |
Jason Wang | 6680ec6 | 2013-07-25 13:00:33 +0800 | [diff] [blame] | 1644 | veth.h_vlan_proto = skb->vlan_proto; |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 1645 | veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | |
Jason Wang | 6680ec6 | 2013-07-25 13:00:33 +0800 | [diff] [blame] | 1647 | vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); |
Jason Wang | 6680ec6 | 2013-07-25 13:00:33 +0800 | [diff] [blame] | 1648 | |
Herbert Xu | e0b46d0 | 2014-11-07 21:22:23 +0800 | [diff] [blame] | 1649 | ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset); |
| 1650 | if (ret || !iov_iter_count(iter)) |
Jason Wang | 6680ec6 | 2013-07-25 13:00:33 +0800 | [diff] [blame] | 1651 | goto done; |
| 1652 | |
Herbert Xu | e0b46d0 | 2014-11-07 21:22:23 +0800 | [diff] [blame] | 1653 | ret = copy_to_iter(&veth, sizeof(veth), iter); |
| 1654 | if (ret != sizeof(veth) || !iov_iter_count(iter)) |
Jason Wang | 6680ec6 | 2013-07-25 13:00:33 +0800 | [diff] [blame] | 1655 | goto done; |
| 1656 | } |
| 1657 | |
Herbert Xu | e0b46d0 | 2014-11-07 21:22:23 +0800 | [diff] [blame] | 1658 | skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset); |
Jason Wang | 6680ec6 | 2013-07-25 13:00:33 +0800 | [diff] [blame] | 1659 | |
| 1660 | done: |
Paolo Abeni | 608b997 | 2016-04-13 10:52:20 +0200 | [diff] [blame] | 1661 | /* caller is in process context, */ |
| 1662 | stats = get_cpu_ptr(tun->pcpu_stats); |
| 1663 | u64_stats_update_begin(&stats->syncp); |
| 1664 | stats->tx_packets++; |
| 1665 | stats->tx_bytes += skb->len + vlan_hlen; |
| 1666 | u64_stats_update_end(&stats->syncp); |
| 1667 | put_cpu_ptr(tun->pcpu_stats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | |
| 1669 | return total; |
| 1670 | } |
| 1671 | |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 1672 | static struct sk_buff *tun_ring_recv(struct tun_file *tfile, int noblock, |
| 1673 | int *err) |
| 1674 | { |
| 1675 | DECLARE_WAITQUEUE(wait, current); |
| 1676 | struct sk_buff *skb = NULL; |
Jason Wang | f48cc6b | 2016-07-04 13:53:38 +0800 | [diff] [blame] | 1677 | int error = 0; |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 1678 | |
| 1679 | skb = skb_array_consume(&tfile->tx_array); |
| 1680 | if (skb) |
| 1681 | goto out; |
| 1682 | if (noblock) { |
Jason Wang | f48cc6b | 2016-07-04 13:53:38 +0800 | [diff] [blame] | 1683 | error = -EAGAIN; |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 1684 | goto out; |
| 1685 | } |
| 1686 | |
| 1687 | add_wait_queue(&tfile->wq.wait, &wait); |
| 1688 | current->state = TASK_INTERRUPTIBLE; |
| 1689 | |
| 1690 | while (1) { |
| 1691 | skb = skb_array_consume(&tfile->tx_array); |
| 1692 | if (skb) |
| 1693 | break; |
| 1694 | if (signal_pending(current)) { |
Jason Wang | f48cc6b | 2016-07-04 13:53:38 +0800 | [diff] [blame] | 1695 | error = -ERESTARTSYS; |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 1696 | break; |
| 1697 | } |
| 1698 | if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) { |
Jason Wang | f48cc6b | 2016-07-04 13:53:38 +0800 | [diff] [blame] | 1699 | error = -EFAULT; |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 1700 | break; |
| 1701 | } |
| 1702 | |
| 1703 | schedule(); |
| 1704 | } |
| 1705 | |
| 1706 | current->state = TASK_RUNNING; |
| 1707 | remove_wait_queue(&tfile->wq.wait, &wait); |
| 1708 | |
| 1709 | out: |
Jason Wang | f48cc6b | 2016-07-04 13:53:38 +0800 | [diff] [blame] | 1710 | *err = error; |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 1711 | return skb; |
| 1712 | } |
| 1713 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1714 | static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, |
Al Viro | 9b06703 | 2014-11-07 13:52:07 -0500 | [diff] [blame] | 1715 | struct iov_iter *to, |
Jason Wang | ac77cfd | 2017-05-17 12:14:43 +0800 | [diff] [blame] | 1716 | int noblock, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | { |
Al Viro | 9b06703 | 2014-11-07 13:52:07 -0500 | [diff] [blame] | 1718 | ssize_t ret; |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 1719 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | |
Rami Rosen | 3872baf | 2012-11-25 22:07:41 +0000 | [diff] [blame] | 1721 | tun_debug(KERN_INFO, tun, "tun_do_read\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | |
Al Viro | 9b06703 | 2014-11-07 13:52:07 -0500 | [diff] [blame] | 1723 | if (!iov_iter_count(to)) |
| 1724 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | |
Jason Wang | ac77cfd | 2017-05-17 12:14:43 +0800 | [diff] [blame] | 1726 | if (!skb) { |
| 1727 | /* Read frames from ring */ |
| 1728 | skb = tun_ring_recv(tfile, noblock, &err); |
| 1729 | if (!skb) |
| 1730 | return err; |
| 1731 | } |
Herbert Xu | e0b46d0 | 2014-11-07 21:22:23 +0800 | [diff] [blame] | 1732 | |
Al Viro | 9b06703 | 2014-11-07 13:52:07 -0500 | [diff] [blame] | 1733 | ret = tun_put_user(tun, tfile, skb, to); |
Jason Wang | f51a5e8 | 2014-12-01 16:53:15 +0800 | [diff] [blame] | 1734 | if (unlikely(ret < 0)) |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 1735 | kfree_skb(skb); |
Jason Wang | f51a5e8 | 2014-12-01 16:53:15 +0800 | [diff] [blame] | 1736 | else |
| 1737 | consume_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1739 | return ret; |
| 1740 | } |
| 1741 | |
Al Viro | 9b06703 | 2014-11-07 13:52:07 -0500 | [diff] [blame] | 1742 | static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to) |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1743 | { |
| 1744 | struct file *file = iocb->ki_filp; |
| 1745 | struct tun_file *tfile = file->private_data; |
| 1746 | struct tun_struct *tun = __tun_get(tfile); |
Al Viro | 9b06703 | 2014-11-07 13:52:07 -0500 | [diff] [blame] | 1747 | ssize_t len = iov_iter_count(to), ret; |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1748 | |
| 1749 | if (!tun) |
| 1750 | return -EBADFD; |
Jason Wang | ac77cfd | 2017-05-17 12:14:43 +0800 | [diff] [blame] | 1751 | ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK, NULL); |
David S. Miller | 42404c0 | 2013-12-10 22:05:45 -0500 | [diff] [blame] | 1752 | ret = min_t(ssize_t, ret, len); |
Zhi Yong Wu | d0b7da8a | 2013-12-06 14:16:51 +0800 | [diff] [blame] | 1753 | if (ret > 0) |
| 1754 | iocb->ki_pos = ret; |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1755 | tun_put(tun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | return ret; |
| 1757 | } |
| 1758 | |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 1759 | static void tun_free_netdev(struct net_device *dev) |
| 1760 | { |
| 1761 | struct tun_struct *tun = netdev_priv(dev); |
| 1762 | |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 1763 | BUG_ON(!(list_empty(&tun->disabled))); |
Paolo Abeni | 608b997 | 2016-04-13 10:52:20 +0200 | [diff] [blame] | 1764 | free_percpu(tun->pcpu_stats); |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 1765 | tun_flow_uninit(tun); |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 1766 | security_tun_dev_free_security(tun->security); |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | static void tun_setup(struct net_device *dev) |
| 1770 | { |
| 1771 | struct tun_struct *tun = netdev_priv(dev); |
| 1772 | |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1773 | tun->owner = INVALID_UID; |
| 1774 | tun->group = INVALID_GID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | dev->ethtool_ops = &tun_ethtool_ops; |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 1777 | dev->needs_free_netdev = true; |
| 1778 | dev->priv_destructor = tun_free_netdev; |
Jason Wang | 016adb7 | 2016-04-08 13:26:48 +0800 | [diff] [blame] | 1779 | /* We prefer our own queue length */ |
| 1780 | dev->tx_queue_len = TUN_READQ_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 1783 | /* Trivial set of netlink ops to allow deleting tun or tap |
| 1784 | * device with netlink. |
| 1785 | */ |
Matthias Schiffer | a8b8a889 | 2017-06-25 23:56:01 +0200 | [diff] [blame] | 1786 | static int tun_validate(struct nlattr *tb[], struct nlattr *data[], |
| 1787 | struct netlink_ext_ack *extack) |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 1788 | { |
| 1789 | return -EINVAL; |
| 1790 | } |
| 1791 | |
| 1792 | static struct rtnl_link_ops tun_link_ops __read_mostly = { |
| 1793 | .kind = DRV_NAME, |
| 1794 | .priv_size = sizeof(struct tun_struct), |
| 1795 | .setup = tun_setup, |
| 1796 | .validate = tun_validate, |
| 1797 | }; |
| 1798 | |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1799 | static void tun_sock_write_space(struct sock *sk) |
| 1800 | { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1801 | struct tun_file *tfile; |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 1802 | wait_queue_head_t *wqueue; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1803 | |
| 1804 | if (!sock_writeable(sk)) |
| 1805 | return; |
| 1806 | |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 1807 | if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags)) |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1808 | return; |
| 1809 | |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 1810 | wqueue = sk_sleep(sk); |
| 1811 | if (wqueue && waitqueue_active(wqueue)) |
| 1812 | wake_up_interruptible_sync_poll(wqueue, POLLOUT | |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1813 | POLLWRNORM | POLLWRBAND); |
Herbert Xu | c722c62 | 2009-06-03 21:45:55 -0700 | [diff] [blame] | 1814 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1815 | tfile = container_of(sk, struct tun_file, sk); |
| 1816 | kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1817 | } |
| 1818 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1819 | static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len) |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1820 | { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1821 | int ret; |
| 1822 | struct tun_file *tfile = container_of(sock, struct tun_file, socket); |
| 1823 | struct tun_struct *tun = __tun_get(tfile); |
| 1824 | |
| 1825 | if (!tun) |
| 1826 | return -EBADFD; |
Al Viro | f5ff53b | 2014-06-19 15:36:49 -0400 | [diff] [blame] | 1827 | |
Al Viro | c0371da | 2014-11-24 10:42:55 -0500 | [diff] [blame] | 1828 | ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter, |
Jason Wang | 5503fce | 2017-01-18 15:02:03 +0800 | [diff] [blame] | 1829 | m->msg_flags & MSG_DONTWAIT, |
| 1830 | m->msg_flags & MSG_MORE); |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1831 | tun_put(tun); |
| 1832 | return ret; |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1835 | static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1836 | int flags) |
| 1837 | { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1838 | struct tun_file *tfile = container_of(sock, struct tun_file, socket); |
| 1839 | struct tun_struct *tun = __tun_get(tfile); |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1840 | int ret; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1841 | |
| 1842 | if (!tun) |
| 1843 | return -EBADFD; |
| 1844 | |
Richard Cochran | eda2977 | 2013-07-19 19:40:10 +0200 | [diff] [blame] | 1845 | if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) { |
Gao feng | 3811ae7 | 2013-04-24 21:59:23 +0000 | [diff] [blame] | 1846 | ret = -EINVAL; |
| 1847 | goto out; |
| 1848 | } |
Richard Cochran | eda2977 | 2013-07-19 19:40:10 +0200 | [diff] [blame] | 1849 | if (flags & MSG_ERRQUEUE) { |
| 1850 | ret = sock_recv_errqueue(sock->sk, m, total_len, |
| 1851 | SOL_PACKET, TUN_TX_TIMESTAMP); |
| 1852 | goto out; |
| 1853 | } |
Jason Wang | ac77cfd | 2017-05-17 12:14:43 +0800 | [diff] [blame] | 1854 | ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, |
| 1855 | m->msg_control); |
Alex Gartrell | 8789793 | 2014-12-25 23:05:03 -0800 | [diff] [blame] | 1856 | if (ret > (ssize_t)total_len) { |
David S. Miller | 42404c0 | 2013-12-10 22:05:45 -0500 | [diff] [blame] | 1857 | m->msg_flags |= MSG_TRUNC; |
| 1858 | ret = flags & MSG_TRUNC ? ret : total_len; |
| 1859 | } |
Gao feng | 3811ae7 | 2013-04-24 21:59:23 +0000 | [diff] [blame] | 1860 | out: |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1861 | tun_put(tun); |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1862 | return ret; |
| 1863 | } |
| 1864 | |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 1865 | static int tun_peek_len(struct socket *sock) |
| 1866 | { |
| 1867 | struct tun_file *tfile = container_of(sock, struct tun_file, socket); |
| 1868 | struct tun_struct *tun; |
| 1869 | int ret = 0; |
| 1870 | |
| 1871 | tun = __tun_get(tfile); |
| 1872 | if (!tun) |
| 1873 | return 0; |
| 1874 | |
| 1875 | ret = skb_array_peek_len(&tfile->tx_array); |
| 1876 | tun_put(tun); |
| 1877 | |
| 1878 | return ret; |
| 1879 | } |
| 1880 | |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1881 | /* Ops structure to mimic raw sockets with tun */ |
| 1882 | static const struct proto_ops tun_socket_ops = { |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 1883 | .peek_len = tun_peek_len, |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1884 | .sendmsg = tun_sendmsg, |
| 1885 | .recvmsg = tun_recvmsg, |
| 1886 | }; |
| 1887 | |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1888 | static struct proto tun_proto = { |
| 1889 | .name = "tun", |
| 1890 | .owner = THIS_MODULE, |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1891 | .obj_size = sizeof(struct tun_file), |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1892 | }; |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 1893 | |
David Woodhouse | 980c9e8 | 2009-05-09 22:54:21 -0700 | [diff] [blame] | 1894 | static int tun_flags(struct tun_struct *tun) |
| 1895 | { |
Michael S. Tsirkin | 031f5e03 | 2014-11-19 14:44:40 +0200 | [diff] [blame] | 1896 | return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP); |
David Woodhouse | 980c9e8 | 2009-05-09 22:54:21 -0700 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr, |
| 1900 | char *buf) |
| 1901 | { |
| 1902 | struct tun_struct *tun = netdev_priv(to_net_dev(dev)); |
| 1903 | return sprintf(buf, "0x%x\n", tun_flags(tun)); |
| 1904 | } |
| 1905 | |
| 1906 | static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr, |
| 1907 | char *buf) |
| 1908 | { |
| 1909 | struct tun_struct *tun = netdev_priv(to_net_dev(dev)); |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1910 | return uid_valid(tun->owner)? |
| 1911 | sprintf(buf, "%u\n", |
| 1912 | from_kuid_munged(current_user_ns(), tun->owner)): |
| 1913 | sprintf(buf, "-1\n"); |
David Woodhouse | 980c9e8 | 2009-05-09 22:54:21 -0700 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr, |
| 1917 | char *buf) |
| 1918 | { |
| 1919 | struct tun_struct *tun = netdev_priv(to_net_dev(dev)); |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1920 | return gid_valid(tun->group) ? |
| 1921 | sprintf(buf, "%u\n", |
| 1922 | from_kgid_munged(current_user_ns(), tun->group)): |
| 1923 | sprintf(buf, "-1\n"); |
David Woodhouse | 980c9e8 | 2009-05-09 22:54:21 -0700 | [diff] [blame] | 1924 | } |
| 1925 | |
| 1926 | static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL); |
| 1927 | static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL); |
| 1928 | static DEVICE_ATTR(group, 0444, tun_show_group, NULL); |
| 1929 | |
Takashi Iwai | c4d33e2 | 2015-02-04 14:37:34 +0100 | [diff] [blame] | 1930 | static struct attribute *tun_dev_attrs[] = { |
| 1931 | &dev_attr_tun_flags.attr, |
| 1932 | &dev_attr_owner.attr, |
| 1933 | &dev_attr_group.attr, |
| 1934 | NULL |
| 1935 | }; |
| 1936 | |
| 1937 | static const struct attribute_group tun_attr_group = { |
| 1938 | .attrs = tun_dev_attrs |
| 1939 | }; |
| 1940 | |
Pavel Emelyanov | d647a59 | 2008-04-16 00:41:16 -0700 | [diff] [blame] | 1941 | 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] | 1942 | { |
| 1943 | struct tun_struct *tun; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 1944 | struct tun_file *tfile = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | struct net_device *dev; |
| 1946 | int err; |
| 1947 | |
Jason Wang | 7c0c3b1 | 2013-01-11 16:59:33 +0000 | [diff] [blame] | 1948 | if (tfile->detached) |
| 1949 | return -EINVAL; |
| 1950 | |
Eric W. Biederman | 74a3e5a | 2009-01-20 10:56:20 +0000 | [diff] [blame] | 1951 | dev = __dev_get_by_name(net, ifr->ifr_name); |
| 1952 | if (dev) { |
David Woodhouse | f85ba78 | 2009-04-27 03:23:54 -0700 | [diff] [blame] | 1953 | if (ifr->ifr_flags & IFF_TUN_EXCL) |
| 1954 | return -EBUSY; |
Eric W. Biederman | 74a3e5a | 2009-01-20 10:56:20 +0000 | [diff] [blame] | 1955 | if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) |
| 1956 | tun = netdev_priv(dev); |
| 1957 | else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) |
| 1958 | tun = netdev_priv(dev); |
| 1959 | else |
| 1960 | return -EINVAL; |
| 1961 | |
Jason Wang | 8e6d91a | 2013-05-28 18:32:11 +0000 | [diff] [blame] | 1962 | if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) != |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 1963 | !!(tun->flags & IFF_MULTI_QUEUE)) |
Jason Wang | 8e6d91a | 2013-05-28 18:32:11 +0000 | [diff] [blame] | 1964 | return -EINVAL; |
| 1965 | |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 1966 | if (tun_not_capable(tun)) |
Paul Moore | 2b980db | 2009-08-28 18:12:43 -0400 | [diff] [blame] | 1967 | return -EPERM; |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 1968 | err = security_tun_dev_open(tun->security); |
Paul Moore | 2b980db | 2009-08-28 18:12:43 -0400 | [diff] [blame] | 1969 | if (err < 0) |
| 1970 | return err; |
| 1971 | |
Pavel Emelyanov | 849c9b6 | 2013-08-21 14:32:21 +0400 | [diff] [blame] | 1972 | err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER); |
Eric W. Biederman | a7385ba | 2009-01-20 10:57:48 +0000 | [diff] [blame] | 1973 | if (err < 0) |
| 1974 | return err; |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 1975 | |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 1976 | if (tun->flags & IFF_MULTI_QUEUE && |
Jason Wang | e8dbad6 | 2013-04-22 20:40:39 +0000 | [diff] [blame] | 1977 | (tun->numqueues + tun->numdisabled > 1)) { |
| 1978 | /* One or more queue has already been attached, no need |
| 1979 | * to initialize the device again. |
| 1980 | */ |
| 1981 | return 0; |
| 1982 | } |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1983 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | else { |
| 1985 | char *name; |
| 1986 | unsigned long flags = 0; |
Jason Wang | edfb6a1 | 2013-01-23 03:59:12 +0000 | [diff] [blame] | 1987 | int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ? |
| 1988 | MAX_TAP_QUEUES : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | |
Eric W. Biederman | c260b77 | 2012-11-18 21:34:11 +0000 | [diff] [blame] | 1990 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
David Woodhouse | ca6bb5d | 2006-06-22 16:07:52 -0700 | [diff] [blame] | 1991 | return -EPERM; |
Paul Moore | 2b980db | 2009-08-28 18:12:43 -0400 | [diff] [blame] | 1992 | err = security_tun_dev_create(); |
| 1993 | if (err < 0) |
| 1994 | return err; |
David Woodhouse | ca6bb5d | 2006-06-22 16:07:52 -0700 | [diff] [blame] | 1995 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | /* Set dev type */ |
| 1997 | if (ifr->ifr_flags & IFF_TUN) { |
| 1998 | /* TUN device */ |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 1999 | flags |= IFF_TUN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | name = "tun%d"; |
| 2001 | } else if (ifr->ifr_flags & IFF_TAP) { |
| 2002 | /* TAP device */ |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 2003 | flags |= IFF_TAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | name = "tap%d"; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 2005 | } else |
Kusanagi Kouichi | 36989b9 | 2009-09-16 21:36:13 +0000 | [diff] [blame] | 2006 | return -EINVAL; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 2007 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | if (*ifr->ifr_name) |
| 2009 | name = ifr->ifr_name; |
| 2010 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 2011 | dev = alloc_netdev_mqs(sizeof(struct tun_struct), name, |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 2012 | NET_NAME_UNKNOWN, tun_setup, queues, |
| 2013 | queues); |
Jason Wang | edfb6a1 | 2013-01-23 03:59:12 +0000 | [diff] [blame] | 2014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | if (!dev) |
| 2016 | return -ENOMEM; |
| 2017 | |
Pavel Emelyanov | fc54c65 | 2008-04-16 00:41:53 -0700 | [diff] [blame] | 2018 | dev_net_set(dev, net); |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 2019 | dev->rtnl_link_ops = &tun_link_ops; |
Pavel Emelyanov | fb7589a | 2013-08-21 14:31:38 +0400 | [diff] [blame] | 2020 | dev->ifindex = tfile->ifindex; |
Takashi Iwai | c4d33e2 | 2015-02-04 14:37:34 +0100 | [diff] [blame] | 2021 | dev->sysfs_groups[0] = &tun_attr_group; |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 2022 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | tun = netdev_priv(dev); |
| 2024 | tun->dev = dev; |
| 2025 | tun->flags = flags; |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 2026 | tun->txflt.count = 0; |
Michael S. Tsirkin | d9d52b5 | 2010-03-17 17:45:01 +0200 | [diff] [blame] | 2027 | tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | |
Paolo Abeni | eaea34b | 2016-02-26 10:45:40 +0100 | [diff] [blame] | 2029 | tun->align = NET_SKB_PAD; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2030 | tun->filter_attached = false; |
| 2031 | tun->sndbuf = tfile->socket.sk->sk_sndbuf; |
Jason Wang | 5503fce | 2017-01-18 15:02:03 +0800 | [diff] [blame] | 2032 | tun->rx_batched = 0; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 2033 | |
Paolo Abeni | 608b997 | 2016-04-13 10:52:20 +0200 | [diff] [blame] | 2034 | tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats); |
| 2035 | if (!tun->pcpu_stats) { |
| 2036 | err = -ENOMEM; |
| 2037 | goto err_free_dev; |
| 2038 | } |
| 2039 | |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 2040 | spin_lock_init(&tun->lock); |
| 2041 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 2042 | err = security_tun_dev_alloc_security(&tun->security); |
| 2043 | if (err < 0) |
Paolo Abeni | 608b997 | 2016-04-13 10:52:20 +0200 | [diff] [blame] | 2044 | goto err_free_stat; |
Paul Moore | 2b980db | 2009-08-28 18:12:43 -0400 | [diff] [blame] | 2045 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | tun_net_init(dev); |
Pavel Emelyanov | 944a137 | 2013-06-11 17:01:08 +0400 | [diff] [blame] | 2047 | tun_flow_init(tun); |
Jason Wang | 96442e42 | 2012-10-31 19:46:02 +0000 | [diff] [blame] | 2048 | |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 2049 | dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | |
Jason Wang | 6680ec6 | 2013-07-25 13:00:33 +0800 | [diff] [blame] | 2050 | TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX | |
| 2051 | NETIF_F_HW_VLAN_STAG_TX; |
Paolo Abeni | 2a2bbf1 | 2016-04-14 18:39:39 +0200 | [diff] [blame] | 2052 | dev->features = dev->hw_features | NETIF_F_LLTX; |
Fernando Luis Vazquez Cao | 6671b22 | 2014-02-18 21:20:09 +0900 | [diff] [blame] | 2053 | dev->vlan_features = dev->features & |
| 2054 | ~(NETIF_F_HW_VLAN_CTAG_TX | |
| 2055 | NETIF_F_HW_VLAN_STAG_TX); |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 2056 | |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 2057 | INIT_LIST_HEAD(&tun->disabled); |
Pavel Emelyanov | 849c9b6 | 2013-08-21 14:32:21 +0400 | [diff] [blame] | 2058 | err = tun_attach(tun, file, false); |
Jason Wang | eb0fb36 | 2012-12-02 17:19:45 +0000 | [diff] [blame] | 2059 | if (err < 0) |
Jason Wang | 662ca43 | 2013-09-11 18:09:48 +0800 | [diff] [blame] | 2060 | goto err_free_flow; |
Jason Wang | eb0fb36 | 2012-12-02 17:19:45 +0000 | [diff] [blame] | 2061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | err = register_netdevice(tun->dev); |
| 2063 | if (err < 0) |
Jason Wang | 662ca43 | 2013-09-11 18:09:48 +0800 | [diff] [blame] | 2064 | goto err_detach; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | } |
| 2066 | |
Michael S. Tsirkin | af668b3 | 2013-01-28 00:38:02 +0000 | [diff] [blame] | 2067 | netif_carrier_on(tun->dev); |
| 2068 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 2069 | tun_debug(KERN_INFO, tun, "tun_set_iff\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | |
Michael S. Tsirkin | 031f5e03 | 2014-11-19 14:44:40 +0200 | [diff] [blame] | 2071 | tun->flags = (tun->flags & ~TUN_FEATURES) | |
| 2072 | (ifr->ifr_flags & TUN_FEATURES); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 2073 | |
Max Krasnyansky | e35259a | 2008-07-10 16:59:11 -0700 | [diff] [blame] | 2074 | /* Make sure persistent devices do not get stuck in |
| 2075 | * xoff state. |
| 2076 | */ |
| 2077 | if (netif_running(tun->dev)) |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 2078 | netif_tx_wake_all_queues(tun->dev); |
Max Krasnyansky | e35259a | 2008-07-10 16:59:11 -0700 | [diff] [blame] | 2079 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | strcpy(ifr->ifr_name, tun->dev->name); |
| 2081 | return 0; |
| 2082 | |
Jason Wang | 662ca43 | 2013-09-11 18:09:48 +0800 | [diff] [blame] | 2083 | err_detach: |
| 2084 | tun_detach_all(dev); |
| 2085 | err_free_flow: |
| 2086 | tun_flow_uninit(tun); |
| 2087 | security_tun_dev_free_security(tun->security); |
Paolo Abeni | 608b997 | 2016-04-13 10:52:20 +0200 | [diff] [blame] | 2088 | err_free_stat: |
| 2089 | free_percpu(tun->pcpu_stats); |
Jason Wang | 662ca43 | 2013-09-11 18:09:48 +0800 | [diff] [blame] | 2090 | err_free_dev: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | free_netdev(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | return err; |
| 2093 | } |
| 2094 | |
Rami Rosen | 9ce99cf | 2012-11-23 03:58:10 +0000 | [diff] [blame] | 2095 | static void tun_get_iff(struct net *net, struct tun_struct *tun, |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 2096 | struct ifreq *ifr) |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 2097 | { |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 2098 | tun_debug(KERN_INFO, tun, "tun_get_iff\n"); |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 2099 | |
| 2100 | strcpy(ifr->ifr_name, tun->dev->name); |
| 2101 | |
David Woodhouse | 980c9e8 | 2009-05-09 22:54:21 -0700 | [diff] [blame] | 2102 | ifr->ifr_flags = tun_flags(tun); |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 2103 | |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 2104 | } |
| 2105 | |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 2106 | /* This is like a cut-down ethtool ops, except done via tun fd so no |
| 2107 | * privs required. */ |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 2108 | static int set_offload(struct tun_struct *tun, unsigned long arg) |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 2109 | { |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 2110 | netdev_features_t features = 0; |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 2111 | |
| 2112 | if (arg & TUN_F_CSUM) { |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 2113 | features |= NETIF_F_HW_CSUM; |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 2114 | arg &= ~TUN_F_CSUM; |
| 2115 | |
| 2116 | if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { |
| 2117 | if (arg & TUN_F_TSO_ECN) { |
| 2118 | features |= NETIF_F_TSO_ECN; |
| 2119 | arg &= ~TUN_F_TSO_ECN; |
| 2120 | } |
| 2121 | if (arg & TUN_F_TSO4) |
| 2122 | features |= NETIF_F_TSO; |
| 2123 | if (arg & TUN_F_TSO6) |
| 2124 | features |= NETIF_F_TSO6; |
| 2125 | arg &= ~(TUN_F_TSO4|TUN_F_TSO6); |
| 2126 | } |
| 2127 | } |
| 2128 | |
| 2129 | /* This gives the user a way to test for new features in future by |
| 2130 | * trying to set them. */ |
| 2131 | if (arg) |
| 2132 | return -EINVAL; |
| 2133 | |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 2134 | tun->set_features = features; |
Yaroslav Isakov | 0905095 | 2017-03-16 22:44:10 +0300 | [diff] [blame] | 2135 | tun->dev->wanted_features &= ~TUN_USER_FEATURES; |
| 2136 | tun->dev->wanted_features |= features; |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 2137 | netdev_update_features(tun->dev); |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 2138 | |
| 2139 | return 0; |
| 2140 | } |
| 2141 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 2142 | static void tun_detach_filter(struct tun_struct *tun, int n) |
| 2143 | { |
| 2144 | int i; |
| 2145 | struct tun_file *tfile; |
| 2146 | |
| 2147 | for (i = 0; i < n; i++) { |
Jason Wang | b8deabd | 2013-01-11 16:59:32 +0000 | [diff] [blame] | 2148 | tfile = rtnl_dereference(tun->tfiles[i]); |
Hannes Frederic Sowa | 8ced425 | 2016-04-05 17:10:16 +0200 | [diff] [blame] | 2149 | lock_sock(tfile->socket.sk); |
| 2150 | sk_detach_filter(tfile->socket.sk); |
| 2151 | release_sock(tfile->socket.sk); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
| 2154 | tun->filter_attached = false; |
| 2155 | } |
| 2156 | |
| 2157 | static int tun_attach_filter(struct tun_struct *tun) |
| 2158 | { |
| 2159 | int i, ret = 0; |
| 2160 | struct tun_file *tfile; |
| 2161 | |
| 2162 | for (i = 0; i < tun->numqueues; i++) { |
Jason Wang | b8deabd | 2013-01-11 16:59:32 +0000 | [diff] [blame] | 2163 | tfile = rtnl_dereference(tun->tfiles[i]); |
Hannes Frederic Sowa | 8ced425 | 2016-04-05 17:10:16 +0200 | [diff] [blame] | 2164 | lock_sock(tfile->socket.sk); |
| 2165 | ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); |
| 2166 | release_sock(tfile->socket.sk); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 2167 | if (ret) { |
| 2168 | tun_detach_filter(tun, i); |
| 2169 | return ret; |
| 2170 | } |
| 2171 | } |
| 2172 | |
| 2173 | tun->filter_attached = true; |
| 2174 | return ret; |
| 2175 | } |
| 2176 | |
| 2177 | static void tun_set_sndbuf(struct tun_struct *tun) |
| 2178 | { |
| 2179 | struct tun_file *tfile; |
| 2180 | int i; |
| 2181 | |
| 2182 | for (i = 0; i < tun->numqueues; i++) { |
Jason Wang | b8deabd | 2013-01-11 16:59:32 +0000 | [diff] [blame] | 2183 | tfile = rtnl_dereference(tun->tfiles[i]); |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 2184 | tfile->socket.sk->sk_sndbuf = tun->sndbuf; |
| 2185 | } |
| 2186 | } |
| 2187 | |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 2188 | static int tun_set_queue(struct file *file, struct ifreq *ifr) |
| 2189 | { |
| 2190 | struct tun_file *tfile = file->private_data; |
| 2191 | struct tun_struct *tun; |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 2192 | int ret = 0; |
| 2193 | |
| 2194 | rtnl_lock(); |
| 2195 | |
| 2196 | if (ifr->ifr_flags & IFF_ATTACH_QUEUE) { |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 2197 | tun = tfile->detached; |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 2198 | if (!tun) { |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 2199 | ret = -EINVAL; |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 2200 | goto unlock; |
| 2201 | } |
| 2202 | ret = security_tun_dev_attach_queue(tun->security); |
| 2203 | if (ret < 0) |
| 2204 | goto unlock; |
Pavel Emelyanov | 849c9b6 | 2013-08-21 14:32:21 +0400 | [diff] [blame] | 2205 | ret = tun_attach(tun, file, false); |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 2206 | } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) { |
Jason Wang | b8deabd | 2013-01-11 16:59:32 +0000 | [diff] [blame] | 2207 | tun = rtnl_dereference(tfile->tun); |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 2208 | if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached) |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 2209 | ret = -EINVAL; |
| 2210 | else |
| 2211 | __tun_detach(tfile, false); |
| 2212 | } else |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 2213 | ret = -EINVAL; |
| 2214 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 2215 | unlock: |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 2216 | rtnl_unlock(); |
| 2217 | return ret; |
| 2218 | } |
| 2219 | |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 2220 | static long __tun_chr_ioctl(struct file *file, unsigned int cmd, |
| 2221 | unsigned long arg, int ifreq_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | { |
Eric W. Biederman | 36b50ba | 2009-01-20 11:01:48 +0000 | [diff] [blame] | 2223 | struct tun_file *tfile = file->private_data; |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2224 | struct tun_struct *tun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | void __user* argp = (void __user*)arg; |
| 2226 | struct ifreq ifr; |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 2227 | kuid_t owner; |
| 2228 | kgid_t group; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 2229 | int sndbuf; |
Michael S. Tsirkin | d9d52b5 | 2010-03-17 17:45:01 +0200 | [diff] [blame] | 2230 | int vnet_hdr_sz; |
Pavel Emelyanov | fb7589a | 2013-08-21 14:31:38 +0400 | [diff] [blame] | 2231 | unsigned int ifindex; |
Michael S. Tsirkin | 1cf8e41 | 2014-12-16 15:05:06 +0200 | [diff] [blame] | 2232 | int le; |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 2233 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | |
Gao Feng | 20861f2 | 2016-10-27 09:05:22 +0800 | [diff] [blame] | 2235 | if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == SOCK_IOC_TYPE) { |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 2236 | if (copy_from_user(&ifr, argp, ifreq_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | return -EFAULT; |
David S. Miller | 8bbb181 | 2012-07-30 14:52:48 -0700 | [diff] [blame] | 2238 | } else { |
Mathias Krause | a117dac | 2012-07-29 19:45:14 +0000 | [diff] [blame] | 2239 | memset(&ifr, 0, sizeof(ifr)); |
David S. Miller | 8bbb181 | 2012-07-30 14:52:48 -0700 | [diff] [blame] | 2240 | } |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2241 | if (cmd == TUNGETFEATURES) { |
| 2242 | /* Currently this just means: "what IFF flags are valid?". |
| 2243 | * This is needed because we never checked for invalid flags on |
Michael S. Tsirkin | 031f5e03 | 2014-11-19 14:44:40 +0200 | [diff] [blame] | 2244 | * TUNSETIFF. |
| 2245 | */ |
| 2246 | return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES, |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2247 | (unsigned int __user*)argp); |
Jason Wang | cde8b15 | 2012-10-31 19:46:01 +0000 | [diff] [blame] | 2248 | } else if (cmd == TUNSETQUEUE) |
| 2249 | return tun_set_queue(file, &ifr); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2250 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 2251 | ret = 0; |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 2252 | rtnl_lock(); |
| 2253 | |
Eric W. Biederman | 36b50ba | 2009-01-20 11:01:48 +0000 | [diff] [blame] | 2254 | tun = __tun_get(tfile); |
Gao Feng | 0f16bc1 | 2016-10-25 22:26:09 +0800 | [diff] [blame] | 2255 | if (cmd == TUNSETIFF) { |
| 2256 | ret = -EEXIST; |
| 2257 | if (tun) |
| 2258 | goto unlock; |
| 2259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | ifr.ifr_name[IFNAMSIZ-1] = '\0'; |
| 2261 | |
Eric W. Biederman | 140e807da | 2015-05-08 21:07:08 -0500 | [diff] [blame] | 2262 | ret = tun_set_iff(sock_net(&tfile->sk), file, &ifr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 2264 | if (ret) |
| 2265 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 2267 | if (copy_to_user(argp, &ifr, ifreq_len)) |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 2268 | ret = -EFAULT; |
| 2269 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | } |
Pavel Emelyanov | fb7589a | 2013-08-21 14:31:38 +0400 | [diff] [blame] | 2271 | if (cmd == TUNSETIFINDEX) { |
| 2272 | ret = -EPERM; |
| 2273 | if (tun) |
| 2274 | goto unlock; |
| 2275 | |
| 2276 | ret = -EFAULT; |
| 2277 | if (copy_from_user(&ifindex, argp, sizeof(ifindex))) |
| 2278 | goto unlock; |
| 2279 | |
| 2280 | ret = 0; |
| 2281 | tfile->ifindex = ifindex; |
| 2282 | goto unlock; |
| 2283 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 2285 | ret = -EBADFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | if (!tun) |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 2287 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | |
Jason Wang | 1e58833 | 2012-10-31 19:45:56 +0000 | [diff] [blame] | 2289 | tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2291 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | switch (cmd) { |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 2293 | case TUNGETIFF: |
Rami Rosen | 9ce99cf | 2012-11-23 03:58:10 +0000 | [diff] [blame] | 2294 | tun_get_iff(current->nsproxy->net_ns, tun, &ifr); |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 2295 | |
Pavel Emelyanov | 3d407a8 | 2013-08-21 14:32:00 +0400 | [diff] [blame] | 2296 | if (tfile->detached) |
| 2297 | ifr.ifr_flags |= IFF_DETACH_QUEUE; |
Pavel Emelyanov | 849c9b6 | 2013-08-21 14:32:21 +0400 | [diff] [blame] | 2298 | if (!tfile->socket.sk->sk_filter) |
| 2299 | ifr.ifr_flags |= IFF_NOFILTER; |
Pavel Emelyanov | 3d407a8 | 2013-08-21 14:32:00 +0400 | [diff] [blame] | 2300 | |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 2301 | if (copy_to_user(argp, &ifr, ifreq_len)) |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2302 | ret = -EFAULT; |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 2303 | break; |
| 2304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | case TUNSETNOCSUM: |
| 2306 | /* Disable/Enable checksum */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2307 | |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 2308 | /* [unimplemented] */ |
| 2309 | tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n", |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 2310 | arg ? "disabled" : "enabled"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2311 | break; |
| 2312 | |
| 2313 | case TUNSETPERSIST: |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2314 | /* Disable/Enable persist mode. Keep an extra reference to the |
| 2315 | * module to prevent the module being unprobed. |
| 2316 | */ |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 2317 | if (arg && !(tun->flags & IFF_PERSIST)) { |
| 2318 | tun->flags |= IFF_PERSIST; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2319 | __module_get(THIS_MODULE); |
Jason Wang | dd38bd8 | 2013-01-11 16:59:34 +0000 | [diff] [blame] | 2320 | } |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 2321 | if (!arg && (tun->flags & IFF_PERSIST)) { |
| 2322 | tun->flags &= ~IFF_PERSIST; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2323 | module_put(THIS_MODULE); |
| 2324 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 2326 | tun_debug(KERN_INFO, tun, "persist %s\n", |
| 2327 | arg ? "enabled" : "disabled"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | break; |
| 2329 | |
| 2330 | case TUNSETOWNER: |
| 2331 | /* Set owner of the device */ |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 2332 | owner = make_kuid(current_user_ns(), arg); |
| 2333 | if (!uid_valid(owner)) { |
| 2334 | ret = -EINVAL; |
| 2335 | break; |
| 2336 | } |
| 2337 | tun->owner = owner; |
Jason Wang | 1e58833 | 2012-10-31 19:45:56 +0000 | [diff] [blame] | 2338 | tun_debug(KERN_INFO, tun, "owner set to %u\n", |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 2339 | from_kuid(&init_user_ns, tun->owner)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | break; |
| 2341 | |
Guido Guenther | 8c64462 | 2007-07-02 22:50:25 -0700 | [diff] [blame] | 2342 | case TUNSETGROUP: |
| 2343 | /* Set group of the device */ |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 2344 | group = make_kgid(current_user_ns(), arg); |
| 2345 | if (!gid_valid(group)) { |
| 2346 | ret = -EINVAL; |
| 2347 | break; |
| 2348 | } |
| 2349 | tun->group = group; |
Jason Wang | 1e58833 | 2012-10-31 19:45:56 +0000 | [diff] [blame] | 2350 | tun_debug(KERN_INFO, tun, "group set to %u\n", |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 2351 | from_kgid(&init_user_ns, tun->group)); |
Guido Guenther | 8c64462 | 2007-07-02 22:50:25 -0700 | [diff] [blame] | 2352 | break; |
| 2353 | |
Mike Kershaw | ff4cc3a | 2005-09-01 17:40:05 -0700 | [diff] [blame] | 2354 | case TUNSETLINK: |
| 2355 | /* Only allow setting the type when the interface is down */ |
| 2356 | if (tun->dev->flags & IFF_UP) { |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 2357 | tun_debug(KERN_INFO, tun, |
| 2358 | "Linktype set failed because interface is up\n"); |
David S. Miller | 48abfe0 | 2008-04-23 19:37:58 -0700 | [diff] [blame] | 2359 | ret = -EBUSY; |
Mike Kershaw | ff4cc3a | 2005-09-01 17:40:05 -0700 | [diff] [blame] | 2360 | } else { |
| 2361 | tun->dev->type = (int) arg; |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 2362 | tun_debug(KERN_INFO, tun, "linktype set to %d\n", |
| 2363 | tun->dev->type); |
David S. Miller | 48abfe0 | 2008-04-23 19:37:58 -0700 | [diff] [blame] | 2364 | ret = 0; |
Mike Kershaw | ff4cc3a | 2005-09-01 17:40:05 -0700 | [diff] [blame] | 2365 | } |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2366 | break; |
Mike Kershaw | ff4cc3a | 2005-09-01 17:40:05 -0700 | [diff] [blame] | 2367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | #ifdef TUN_DEBUG |
| 2369 | case TUNSETDEBUG: |
| 2370 | tun->debug = arg; |
| 2371 | break; |
| 2372 | #endif |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 2373 | case TUNSETOFFLOAD: |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 2374 | ret = set_offload(tun, arg); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2375 | break; |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 2376 | |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 2377 | case TUNSETTXFILTER: |
| 2378 | /* Can be set only for TAPs */ |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2379 | ret = -EINVAL; |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 2380 | if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2381 | break; |
Harvey Harrison | c0e5a8c | 2008-07-16 12:45:34 -0700 | [diff] [blame] | 2382 | ret = update_filter(&tun->txflt, (void __user *)arg); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2383 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2384 | |
| 2385 | case SIOCGIFHWADDR: |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 2386 | /* Get hw address */ |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 2387 | memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); |
| 2388 | ifr.ifr_hwaddr.sa_family = tun->dev->type; |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 2389 | if (copy_to_user(argp, &ifr, ifreq_len)) |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2390 | ret = -EFAULT; |
| 2391 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | |
| 2393 | case SIOCSIFHWADDR: |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 2394 | /* Set hw address */ |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 2395 | tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n", |
| 2396 | ifr.ifr_hwaddr.sa_data); |
Kim B. Heino | 4010237 | 2008-02-29 12:26:21 -0800 | [diff] [blame] | 2397 | |
Kim B. Heino | 4010237 | 2008-02-29 12:26:21 -0800 | [diff] [blame] | 2398 | ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2399 | break; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 2400 | |
| 2401 | case TUNGETSNDBUF: |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2402 | sndbuf = tfile->socket.sk->sk_sndbuf; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 2403 | if (copy_to_user(argp, &sndbuf, sizeof(sndbuf))) |
| 2404 | ret = -EFAULT; |
| 2405 | break; |
| 2406 | |
| 2407 | case TUNSETSNDBUF: |
| 2408 | if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) { |
| 2409 | ret = -EFAULT; |
| 2410 | break; |
| 2411 | } |
| 2412 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 2413 | tun->sndbuf = sndbuf; |
| 2414 | tun_set_sndbuf(tun); |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 2415 | break; |
| 2416 | |
Michael S. Tsirkin | d9d52b5 | 2010-03-17 17:45:01 +0200 | [diff] [blame] | 2417 | case TUNGETVNETHDRSZ: |
| 2418 | vnet_hdr_sz = tun->vnet_hdr_sz; |
| 2419 | if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz))) |
| 2420 | ret = -EFAULT; |
| 2421 | break; |
| 2422 | |
| 2423 | case TUNSETVNETHDRSZ: |
| 2424 | if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) { |
| 2425 | ret = -EFAULT; |
| 2426 | break; |
| 2427 | } |
| 2428 | if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) { |
| 2429 | ret = -EINVAL; |
| 2430 | break; |
| 2431 | } |
| 2432 | |
| 2433 | tun->vnet_hdr_sz = vnet_hdr_sz; |
| 2434 | break; |
| 2435 | |
Michael S. Tsirkin | 1cf8e41 | 2014-12-16 15:05:06 +0200 | [diff] [blame] | 2436 | case TUNGETVNETLE: |
| 2437 | le = !!(tun->flags & TUN_VNET_LE); |
| 2438 | if (put_user(le, (int __user *)argp)) |
| 2439 | ret = -EFAULT; |
| 2440 | break; |
| 2441 | |
| 2442 | case TUNSETVNETLE: |
| 2443 | if (get_user(le, (int __user *)argp)) { |
| 2444 | ret = -EFAULT; |
| 2445 | break; |
| 2446 | } |
| 2447 | if (le) |
| 2448 | tun->flags |= TUN_VNET_LE; |
| 2449 | else |
| 2450 | tun->flags &= ~TUN_VNET_LE; |
| 2451 | break; |
| 2452 | |
Greg Kurz | 8b8e658 | 2015-04-24 14:50:36 +0200 | [diff] [blame] | 2453 | case TUNGETVNETBE: |
| 2454 | ret = tun_get_vnet_be(tun, argp); |
| 2455 | break; |
| 2456 | |
| 2457 | case TUNSETVNETBE: |
| 2458 | ret = tun_set_vnet_be(tun, argp); |
| 2459 | break; |
| 2460 | |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 2461 | case TUNATTACHFILTER: |
| 2462 | /* Can be set only for TAPs */ |
| 2463 | ret = -EINVAL; |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 2464 | if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 2465 | break; |
| 2466 | ret = -EFAULT; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2467 | if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog))) |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 2468 | break; |
| 2469 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 2470 | ret = tun_attach_filter(tun); |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 2471 | break; |
| 2472 | |
| 2473 | case TUNDETACHFILTER: |
| 2474 | /* Can be set only for TAPs */ |
| 2475 | ret = -EINVAL; |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 2476 | if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 2477 | break; |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 2478 | ret = 0; |
| 2479 | tun_detach_filter(tun, tun->numqueues); |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 2480 | break; |
| 2481 | |
Pavel Emelyanov | 76975e9 | 2013-08-21 14:32:39 +0400 | [diff] [blame] | 2482 | case TUNGETFILTER: |
| 2483 | ret = -EINVAL; |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 2484 | if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) |
Pavel Emelyanov | 76975e9 | 2013-08-21 14:32:39 +0400 | [diff] [blame] | 2485 | break; |
| 2486 | ret = -EFAULT; |
| 2487 | if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog))) |
| 2488 | break; |
| 2489 | ret = 0; |
| 2490 | break; |
| 2491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | default: |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2493 | ret = -EINVAL; |
| 2494 | break; |
Joe Perches | ee289b6 | 2010-05-17 22:47:34 -0700 | [diff] [blame] | 2495 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 | |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 2497 | unlock: |
| 2498 | rtnl_unlock(); |
| 2499 | if (tun) |
| 2500 | tun_put(tun); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2501 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | } |
| 2503 | |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 2504 | static long tun_chr_ioctl(struct file *file, |
| 2505 | unsigned int cmd, unsigned long arg) |
| 2506 | { |
| 2507 | return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq)); |
| 2508 | } |
| 2509 | |
| 2510 | #ifdef CONFIG_COMPAT |
| 2511 | static long tun_chr_compat_ioctl(struct file *file, |
| 2512 | unsigned int cmd, unsigned long arg) |
| 2513 | { |
| 2514 | switch (cmd) { |
| 2515 | case TUNSETIFF: |
| 2516 | case TUNGETIFF: |
| 2517 | case TUNSETTXFILTER: |
| 2518 | case TUNGETSNDBUF: |
| 2519 | case TUNSETSNDBUF: |
| 2520 | case SIOCGIFHWADDR: |
| 2521 | case SIOCSIFHWADDR: |
| 2522 | arg = (unsigned long)compat_ptr(arg); |
| 2523 | break; |
| 2524 | default: |
| 2525 | arg = (compat_ulong_t)arg; |
| 2526 | break; |
| 2527 | } |
| 2528 | |
| 2529 | /* |
| 2530 | * compat_ifreq is shorter than ifreq, so we must not access beyond |
| 2531 | * the end of that structure. All fields that are used in this |
| 2532 | * driver are compatible though, we don't need to convert the |
| 2533 | * contents. |
| 2534 | */ |
| 2535 | return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq)); |
| 2536 | } |
| 2537 | #endif /* CONFIG_COMPAT */ |
| 2538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2539 | static int tun_chr_fasync(int fd, struct file *file, int on) |
| 2540 | { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2541 | struct tun_file *tfile = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | int ret; |
| 2543 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2544 | if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) |
Jonathan Corbet | 9d31952 | 2008-06-19 15:50:37 -0600 | [diff] [blame] | 2545 | goto out; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 2546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | if (on) { |
Jeff Layton | e0b93ed | 2014-08-22 11:27:32 -0400 | [diff] [blame] | 2548 | __f_setown(file, task_pid(current), PIDTYPE_PID, 0); |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2549 | tfile->flags |= TUN_FASYNC; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 2550 | } else |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2551 | tfile->flags &= ~TUN_FASYNC; |
Jonathan Corbet | 9d31952 | 2008-06-19 15:50:37 -0600 | [diff] [blame] | 2552 | ret = 0; |
| 2553 | out: |
Jonathan Corbet | 9d31952 | 2008-06-19 15:50:37 -0600 | [diff] [blame] | 2554 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | static int tun_chr_open(struct inode *inode, struct file * file) |
| 2558 | { |
Eric W. Biederman | 140e807da | 2015-05-08 21:07:08 -0500 | [diff] [blame] | 2559 | struct net *net = current->nsproxy->net_ns; |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2560 | struct tun_file *tfile; |
Thomas Gleixner | deed49f | 2009-10-14 01:19:46 -0700 | [diff] [blame] | 2561 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 2562 | DBG1(KERN_INFO, "tunX: tun_chr_open\n"); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2563 | |
Eric W. Biederman | 140e807da | 2015-05-08 21:07:08 -0500 | [diff] [blame] | 2564 | tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 2565 | &tun_proto, 0); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2566 | if (!tfile) |
| 2567 | return -ENOMEM; |
Monam Agarwal | c956674 | 2014-03-24 00:02:32 +0530 | [diff] [blame] | 2568 | RCU_INIT_POINTER(tfile->tun, NULL); |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2569 | tfile->flags = 0; |
Pavel Emelyanov | fb7589a | 2013-08-21 14:31:38 +0400 | [diff] [blame] | 2570 | tfile->ifindex = 0; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2571 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2572 | init_waitqueue_head(&tfile->wq.wait); |
Xi Wang | 9e641bd | 2014-05-16 15:11:48 -0700 | [diff] [blame] | 2573 | RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq); |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2574 | |
| 2575 | tfile->socket.file = file; |
| 2576 | tfile->socket.ops = &tun_socket_ops; |
| 2577 | |
| 2578 | sock_init_data(&tfile->socket, &tfile->sk); |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2579 | |
| 2580 | tfile->sk.sk_write_space = tun_sock_write_space; |
| 2581 | tfile->sk.sk_sndbuf = INT_MAX; |
| 2582 | |
Jason Wang | 66ccbc9 | 2017-08-11 19:41:16 +0800 | [diff] [blame] | 2583 | tfile->alloc_frag.page = NULL; |
| 2584 | |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2585 | file->private_data = tfile; |
Jason Wang | 4008e97 | 2012-12-13 23:53:30 +0000 | [diff] [blame] | 2586 | INIT_LIST_HEAD(&tfile->next); |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2587 | |
Jason Wang | 19a6afb | 2013-06-08 14:17:41 +0800 | [diff] [blame] | 2588 | sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); |
| 2589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | return 0; |
| 2591 | } |
| 2592 | |
| 2593 | static int tun_chr_close(struct inode *inode, struct file *file) |
| 2594 | { |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 2595 | struct tun_file *tfile = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | |
Jason Wang | c8d68e6 | 2012-10-31 19:46:00 +0000 | [diff] [blame] | 2597 | tun_detach(tfile, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | |
| 2599 | return 0; |
| 2600 | } |
| 2601 | |
Masatake YAMATO | 93e14b6 | 2014-01-29 16:43:31 +0900 | [diff] [blame] | 2602 | #ifdef CONFIG_PROC_FS |
Joe Perches | a3816ab | 2014-09-29 16:08:25 -0700 | [diff] [blame] | 2603 | static void tun_chr_show_fdinfo(struct seq_file *m, struct file *f) |
Masatake YAMATO | 93e14b6 | 2014-01-29 16:43:31 +0900 | [diff] [blame] | 2604 | { |
| 2605 | struct tun_struct *tun; |
| 2606 | struct ifreq ifr; |
| 2607 | |
| 2608 | memset(&ifr, 0, sizeof(ifr)); |
| 2609 | |
| 2610 | rtnl_lock(); |
| 2611 | tun = tun_get(f); |
| 2612 | if (tun) |
| 2613 | tun_get_iff(current->nsproxy->net_ns, tun, &ifr); |
| 2614 | rtnl_unlock(); |
| 2615 | |
| 2616 | if (tun) |
| 2617 | tun_put(tun); |
| 2618 | |
Joe Perches | a3816ab | 2014-09-29 16:08:25 -0700 | [diff] [blame] | 2619 | seq_printf(m, "iff:\t%s\n", ifr.ifr_name); |
Masatake YAMATO | 93e14b6 | 2014-01-29 16:43:31 +0900 | [diff] [blame] | 2620 | } |
| 2621 | #endif |
| 2622 | |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 2623 | static const struct file_operations tun_fops = { |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 2624 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | .llseek = no_llseek, |
Al Viro | 9b06703 | 2014-11-07 13:52:07 -0500 | [diff] [blame] | 2626 | .read_iter = tun_chr_read_iter, |
Al Viro | f5ff53b | 2014-06-19 15:36:49 -0400 | [diff] [blame] | 2627 | .write_iter = tun_chr_write_iter, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2628 | .poll = tun_chr_poll, |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 2629 | .unlocked_ioctl = tun_chr_ioctl, |
| 2630 | #ifdef CONFIG_COMPAT |
| 2631 | .compat_ioctl = tun_chr_compat_ioctl, |
| 2632 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2633 | .open = tun_chr_open, |
| 2634 | .release = tun_chr_close, |
Masatake YAMATO | 93e14b6 | 2014-01-29 16:43:31 +0900 | [diff] [blame] | 2635 | .fasync = tun_chr_fasync, |
| 2636 | #ifdef CONFIG_PROC_FS |
| 2637 | .show_fdinfo = tun_chr_show_fdinfo, |
| 2638 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2639 | }; |
| 2640 | |
| 2641 | static struct miscdevice tun_miscdev = { |
| 2642 | .minor = TUN_MINOR, |
| 2643 | .name = "tun", |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 2644 | .nodename = "net/tun", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2645 | .fops = &tun_fops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | }; |
| 2647 | |
| 2648 | /* ethtool interface */ |
| 2649 | |
Philippe Reynes | 29ccc49 | 2017-03-11 22:03:50 +0100 | [diff] [blame] | 2650 | static int tun_get_link_ksettings(struct net_device *dev, |
| 2651 | struct ethtool_link_ksettings *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | { |
Philippe Reynes | 29ccc49 | 2017-03-11 22:03:50 +0100 | [diff] [blame] | 2653 | ethtool_link_ksettings_zero_link_mode(cmd, supported); |
| 2654 | ethtool_link_ksettings_zero_link_mode(cmd, advertising); |
| 2655 | cmd->base.speed = SPEED_10; |
| 2656 | cmd->base.duplex = DUPLEX_FULL; |
| 2657 | cmd->base.port = PORT_TP; |
| 2658 | cmd->base.phy_address = 0; |
| 2659 | cmd->base.autoneg = AUTONEG_DISABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | return 0; |
| 2661 | } |
| 2662 | |
| 2663 | static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) |
| 2664 | { |
| 2665 | struct tun_struct *tun = netdev_priv(dev); |
| 2666 | |
Rick Jones | 33a5ba1 | 2011-11-15 14:59:53 +0000 | [diff] [blame] | 2667 | strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); |
| 2668 | strlcpy(info->version, DRV_VERSION, sizeof(info->version)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | |
| 2670 | switch (tun->flags & TUN_TYPE_MASK) { |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 2671 | case IFF_TUN: |
Rick Jones | 33a5ba1 | 2011-11-15 14:59:53 +0000 | [diff] [blame] | 2672 | strlcpy(info->bus_info, "tun", sizeof(info->bus_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | break; |
Michael S. Tsirkin | 40630b8 | 2014-11-19 15:17:31 +0200 | [diff] [blame] | 2674 | case IFF_TAP: |
Rick Jones | 33a5ba1 | 2011-11-15 14:59:53 +0000 | [diff] [blame] | 2675 | strlcpy(info->bus_info, "tap", sizeof(info->bus_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2676 | break; |
| 2677 | } |
| 2678 | } |
| 2679 | |
| 2680 | static u32 tun_get_msglevel(struct net_device *dev) |
| 2681 | { |
| 2682 | #ifdef TUN_DEBUG |
| 2683 | struct tun_struct *tun = netdev_priv(dev); |
| 2684 | return tun->debug; |
| 2685 | #else |
| 2686 | return -EOPNOTSUPP; |
| 2687 | #endif |
| 2688 | } |
| 2689 | |
| 2690 | static void tun_set_msglevel(struct net_device *dev, u32 value) |
| 2691 | { |
| 2692 | #ifdef TUN_DEBUG |
| 2693 | struct tun_struct *tun = netdev_priv(dev); |
| 2694 | tun->debug = value; |
| 2695 | #endif |
| 2696 | } |
| 2697 | |
Jason Wang | 5503fce | 2017-01-18 15:02:03 +0800 | [diff] [blame] | 2698 | static int tun_get_coalesce(struct net_device *dev, |
| 2699 | struct ethtool_coalesce *ec) |
| 2700 | { |
| 2701 | struct tun_struct *tun = netdev_priv(dev); |
| 2702 | |
| 2703 | ec->rx_max_coalesced_frames = tun->rx_batched; |
| 2704 | |
| 2705 | return 0; |
| 2706 | } |
| 2707 | |
| 2708 | static int tun_set_coalesce(struct net_device *dev, |
| 2709 | struct ethtool_coalesce *ec) |
| 2710 | { |
| 2711 | struct tun_struct *tun = netdev_priv(dev); |
| 2712 | |
| 2713 | if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT) |
| 2714 | tun->rx_batched = NAPI_POLL_WEIGHT; |
| 2715 | else |
| 2716 | tun->rx_batched = ec->rx_max_coalesced_frames; |
| 2717 | |
| 2718 | return 0; |
| 2719 | } |
| 2720 | |
Jeff Garzik | 7282d49 | 2006-09-13 14:30:00 -0400 | [diff] [blame] | 2721 | static const struct ethtool_ops tun_ethtool_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2722 | .get_drvinfo = tun_get_drvinfo, |
| 2723 | .get_msglevel = tun_get_msglevel, |
| 2724 | .set_msglevel = tun_set_msglevel, |
Nolan Leake | bee3136 | 2010-07-27 13:53:43 +0000 | [diff] [blame] | 2725 | .get_link = ethtool_op_get_link, |
Richard Cochran | eda2977 | 2013-07-19 19:40:10 +0200 | [diff] [blame] | 2726 | .get_ts_info = ethtool_op_get_ts_info, |
Jason Wang | 5503fce | 2017-01-18 15:02:03 +0800 | [diff] [blame] | 2727 | .get_coalesce = tun_get_coalesce, |
| 2728 | .set_coalesce = tun_set_coalesce, |
Philippe Reynes | 29ccc49 | 2017-03-11 22:03:50 +0100 | [diff] [blame] | 2729 | .get_link_ksettings = tun_get_link_ksettings, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | }; |
| 2731 | |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 2732 | static int tun_queue_resize(struct tun_struct *tun) |
| 2733 | { |
| 2734 | struct net_device *dev = tun->dev; |
| 2735 | struct tun_file *tfile; |
| 2736 | struct skb_array **arrays; |
| 2737 | int n = tun->numqueues + tun->numdisabled; |
| 2738 | int ret, i; |
| 2739 | |
stephen hemminger | 1203904 | 2017-08-15 10:29:16 -0700 | [diff] [blame] | 2740 | arrays = kmalloc_array(n, sizeof(*arrays), GFP_KERNEL); |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 2741 | if (!arrays) |
| 2742 | return -ENOMEM; |
| 2743 | |
| 2744 | for (i = 0; i < tun->numqueues; i++) { |
| 2745 | tfile = rtnl_dereference(tun->tfiles[i]); |
| 2746 | arrays[i] = &tfile->tx_array; |
| 2747 | } |
| 2748 | list_for_each_entry(tfile, &tun->disabled, next) |
| 2749 | arrays[i++] = &tfile->tx_array; |
| 2750 | |
| 2751 | ret = skb_array_resize_multiple(arrays, n, |
| 2752 | dev->tx_queue_len, GFP_KERNEL); |
| 2753 | |
| 2754 | kfree(arrays); |
| 2755 | return ret; |
| 2756 | } |
| 2757 | |
| 2758 | static int tun_device_event(struct notifier_block *unused, |
| 2759 | unsigned long event, void *ptr) |
| 2760 | { |
| 2761 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 2762 | struct tun_struct *tun = netdev_priv(dev); |
| 2763 | |
Craig Gallek | 86dfb4ac | 2016-07-06 18:44:20 -0400 | [diff] [blame] | 2764 | if (dev->rtnl_link_ops != &tun_link_ops) |
| 2765 | return NOTIFY_DONE; |
| 2766 | |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 2767 | switch (event) { |
| 2768 | case NETDEV_CHANGE_TX_QUEUE_LEN: |
| 2769 | if (tun_queue_resize(tun)) |
| 2770 | return NOTIFY_BAD; |
| 2771 | break; |
| 2772 | default: |
| 2773 | break; |
| 2774 | } |
| 2775 | |
| 2776 | return NOTIFY_DONE; |
| 2777 | } |
| 2778 | |
| 2779 | static struct notifier_block tun_notifier_block __read_mostly = { |
| 2780 | .notifier_call = tun_device_event, |
| 2781 | }; |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 2782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2783 | static int __init tun_init(void) |
| 2784 | { |
| 2785 | int ret = 0; |
| 2786 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 2787 | pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 2789 | ret = rtnl_link_register(&tun_link_ops); |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 2790 | if (ret) { |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 2791 | pr_err("Can't register link_ops\n"); |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 2792 | goto err_linkops; |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 2793 | } |
| 2794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | ret = misc_register(&tun_miscdev); |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 2796 | if (ret) { |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 2797 | pr_err("Can't register misc device %d\n", TUN_MINOR); |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 2798 | goto err_misc; |
| 2799 | } |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 2800 | |
Tonghao Zhang | 5edfbd3 | 2017-07-20 02:41:34 -0700 | [diff] [blame] | 2801 | ret = register_netdevice_notifier(&tun_notifier_block); |
| 2802 | if (ret) { |
| 2803 | pr_err("Can't register netdevice notifier\n"); |
| 2804 | goto err_notifier; |
| 2805 | } |
| 2806 | |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 2807 | return 0; |
Tonghao Zhang | 5edfbd3 | 2017-07-20 02:41:34 -0700 | [diff] [blame] | 2808 | |
| 2809 | err_notifier: |
| 2810 | misc_deregister(&tun_miscdev); |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 2811 | err_misc: |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 2812 | rtnl_link_unregister(&tun_link_ops); |
| 2813 | err_linkops: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2814 | return ret; |
| 2815 | } |
| 2816 | |
| 2817 | static void tun_cleanup(void) |
| 2818 | { |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 2819 | misc_deregister(&tun_miscdev); |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 2820 | rtnl_link_unregister(&tun_link_ops); |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 2821 | unregister_netdevice_notifier(&tun_notifier_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | } |
| 2823 | |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 2824 | /* Get an underlying socket object from tun file. Returns error unless file is |
| 2825 | * attached to a device. The returned object works like a packet socket, it |
| 2826 | * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for |
| 2827 | * holding a reference to the file for as long as the socket is in use. */ |
| 2828 | struct socket *tun_get_socket(struct file *file) |
| 2829 | { |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 2830 | struct tun_file *tfile; |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 2831 | if (file->f_op != &tun_fops) |
| 2832 | return ERR_PTR(-EINVAL); |
Jason Wang | 6e914fc | 2012-10-31 19:45:58 +0000 | [diff] [blame] | 2833 | tfile = file->private_data; |
| 2834 | if (!tfile) |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 2835 | return ERR_PTR(-EBADFD); |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame] | 2836 | return &tfile->socket; |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 2837 | } |
| 2838 | EXPORT_SYMBOL_GPL(tun_get_socket); |
| 2839 | |
Jason Wang | 83339c6 | 2017-05-17 12:14:41 +0800 | [diff] [blame] | 2840 | struct skb_array *tun_get_skb_array(struct file *file) |
| 2841 | { |
| 2842 | struct tun_file *tfile; |
| 2843 | |
| 2844 | if (file->f_op != &tun_fops) |
| 2845 | return ERR_PTR(-EINVAL); |
| 2846 | tfile = file->private_data; |
| 2847 | if (!tfile) |
| 2848 | return ERR_PTR(-EBADFD); |
| 2849 | return &tfile->tx_array; |
| 2850 | } |
| 2851 | EXPORT_SYMBOL_GPL(tun_get_skb_array); |
| 2852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | module_init(tun_init); |
| 2854 | module_exit(tun_cleanup); |
| 2855 | MODULE_DESCRIPTION(DRV_DESCRIPTION); |
| 2856 | MODULE_AUTHOR(DRV_COPYRIGHT); |
| 2857 | MODULE_LICENSE("GPL"); |
| 2858 | MODULE_ALIAS_MISCDEV(TUN_MINOR); |
Kay Sievers | 578454f | 2010-05-20 18:07:20 +0200 | [diff] [blame] | 2859 | MODULE_ALIAS("devname:net/tun"); |