Thomas Gleixner | 685a6bf | 2019-05-29 16:57:36 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2 | /* |
| 3 | * VMware vSockets Driver |
| 4 | * |
| 5 | * Copyright (C) 2007-2013 VMware, Inc. All rights reserved. |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* Implementation notes: |
| 9 | * |
| 10 | * - There are two kinds of sockets: those created by user action (such as |
| 11 | * calling socket(2)) and those created by incoming connection request packets. |
| 12 | * |
| 13 | * - There are two "global" tables, one for bound sockets (sockets that have |
| 14 | * specified an address that they are responsible for) and one for connected |
| 15 | * sockets (sockets that have established a connection with another socket). |
| 16 | * These tables are "global" in that all sockets on the system are placed |
| 17 | * within them. - Note, though, that the bound table contains an extra entry |
| 18 | * for a list of unbound sockets and SOCK_DGRAM sockets will always remain in |
| 19 | * that list. The bound table is used solely for lookup of sockets when packets |
| 20 | * are received and that's not necessary for SOCK_DGRAM sockets since we create |
| 21 | * a datagram handle for each and need not perform a lookup. Keeping SOCK_DGRAM |
| 22 | * sockets out of the bound hash buckets will reduce the chance of collisions |
| 23 | * when looking for SOCK_STREAM sockets and prevents us from having to check the |
| 24 | * socket type in the hash table lookups. |
| 25 | * |
| 26 | * - Sockets created by user action will either be "client" sockets that |
| 27 | * initiate a connection or "server" sockets that listen for connections; we do |
| 28 | * not support simultaneous connects (two "client" sockets connecting). |
| 29 | * |
| 30 | * - "Server" sockets are referred to as listener sockets throughout this |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 31 | * implementation because they are in the TCP_LISTEN state. When a |
Stefan Hajnoczi | ea3803c | 2015-10-29 11:57:42 +0000 | [diff] [blame] | 32 | * connection request is received (the second kind of socket mentioned above), |
| 33 | * we create a new socket and refer to it as a pending socket. These pending |
| 34 | * sockets are placed on the pending connection list of the listener socket. |
| 35 | * When future packets are received for the address the listener socket is |
| 36 | * bound to, we check if the source of the packet is from one that has an |
| 37 | * existing pending connection. If it does, we process the packet for the |
| 38 | * pending socket. When that socket reaches the connected state, it is removed |
| 39 | * from the listener socket's pending list and enqueued in the listener |
| 40 | * socket's accept queue. Callers of accept(2) will accept connected sockets |
| 41 | * from the listener socket's accept queue. If the socket cannot be accepted |
| 42 | * for some reason then it is marked rejected. Once the connection is |
| 43 | * accepted, it is owned by the user process and the responsibility for cleanup |
| 44 | * falls with that user process. |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 45 | * |
| 46 | * - It is possible that these pending sockets will never reach the connected |
| 47 | * state; in fact, we may never receive another packet after the connection |
| 48 | * request. Because of this, we must schedule a cleanup function to run in the |
| 49 | * future, after some amount of time passes where a connection should have been |
| 50 | * established. This function ensures that the socket is off all lists so it |
| 51 | * cannot be retrieved, then drops all references to the socket so it is cleaned |
| 52 | * up (sock_put() -> sk_free() -> our sk_destruct implementation). Note this |
| 53 | * function will also cleanup rejected sockets, those that reach the connected |
| 54 | * state but leave it before they have been accepted. |
| 55 | * |
Stefan Hajnoczi | 4192f67 | 2016-06-23 16:28:58 +0100 | [diff] [blame] | 56 | * - Lock ordering for pending or accept queue sockets is: |
| 57 | * |
| 58 | * lock_sock(listener); |
| 59 | * lock_sock_nested(pending, SINGLE_DEPTH_NESTING); |
| 60 | * |
| 61 | * Using explicit nested locking keeps lockdep happy since normally only one |
| 62 | * lock of a given class may be taken at a time. |
| 63 | * |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 64 | * - Sockets created by user action will be cleaned up when the user process |
| 65 | * calls close(2), causing our release implementation to be called. Our release |
| 66 | * implementation will perform some cleanup then drop the last reference so our |
| 67 | * sk_destruct implementation is invoked. Our sk_destruct implementation will |
| 68 | * perform additional cleanup that's common for both types of sockets. |
| 69 | * |
| 70 | * - A socket's reference count is what ensures that the structure won't be |
| 71 | * freed. Each entry in a list (such as the "global" bound and connected tables |
| 72 | * and the listener socket's pending list and connected queue) ensures a |
| 73 | * reference. When we defer work until process context and pass a socket as our |
| 74 | * argument, we must ensure the reference count is increased to ensure the |
| 75 | * socket isn't freed before the function is run; the deferred function will |
| 76 | * then drop the reference. |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 77 | * |
| 78 | * - sk->sk_state uses the TCP state constants because they are widely used by |
| 79 | * other address families and exposed to userspace tools like ss(8): |
| 80 | * |
| 81 | * TCP_CLOSE - unconnected |
| 82 | * TCP_SYN_SENT - connecting |
| 83 | * TCP_ESTABLISHED - connected |
| 84 | * TCP_CLOSING - disconnecting |
| 85 | * TCP_LISTEN - listening |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 86 | */ |
| 87 | |
| 88 | #include <linux/types.h> |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 89 | #include <linux/bitops.h> |
| 90 | #include <linux/cred.h> |
| 91 | #include <linux/init.h> |
| 92 | #include <linux/io.h> |
| 93 | #include <linux/kernel.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 94 | #include <linux/sched/signal.h> |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 95 | #include <linux/kmod.h> |
| 96 | #include <linux/list.h> |
| 97 | #include <linux/miscdevice.h> |
| 98 | #include <linux/module.h> |
| 99 | #include <linux/mutex.h> |
| 100 | #include <linux/net.h> |
| 101 | #include <linux/poll.h> |
Lepton Wu | 8236b08 | 2018-12-11 11:12:55 -0800 | [diff] [blame] | 102 | #include <linux/random.h> |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 103 | #include <linux/skbuff.h> |
| 104 | #include <linux/smp.h> |
| 105 | #include <linux/socket.h> |
| 106 | #include <linux/stddef.h> |
| 107 | #include <linux/unistd.h> |
| 108 | #include <linux/wait.h> |
| 109 | #include <linux/workqueue.h> |
| 110 | #include <net/sock.h> |
Asias He | 82a54d0 | 2013-07-25 17:39:34 +0800 | [diff] [blame] | 111 | #include <net/af_vsock.h> |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 112 | |
| 113 | static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr); |
| 114 | static void vsock_sk_destruct(struct sock *sk); |
| 115 | static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb); |
| 116 | |
| 117 | /* Protocol family. */ |
| 118 | static struct proto vsock_proto = { |
| 119 | .name = "AF_VSOCK", |
| 120 | .owner = THIS_MODULE, |
| 121 | .obj_size = sizeof(struct vsock_sock), |
| 122 | }; |
| 123 | |
| 124 | /* The default peer timeout indicates how long we will wait for a peer response |
| 125 | * to a control message. |
| 126 | */ |
| 127 | #define VSOCK_DEFAULT_CONNECT_TIMEOUT (2 * HZ) |
| 128 | |
Stefano Garzarella | b9f2b0f | 2019-11-14 10:57:42 +0100 | [diff] [blame] | 129 | #define VSOCK_DEFAULT_BUFFER_SIZE (1024 * 256) |
| 130 | #define VSOCK_DEFAULT_BUFFER_MAX_SIZE (1024 * 256) |
| 131 | #define VSOCK_DEFAULT_BUFFER_MIN_SIZE 128 |
| 132 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 133 | /* Transport used for host->guest communication */ |
| 134 | static const struct vsock_transport *transport_h2g; |
| 135 | /* Transport used for guest->host communication */ |
| 136 | static const struct vsock_transport *transport_g2h; |
| 137 | /* Transport used for DGRAM communication */ |
| 138 | static const struct vsock_transport *transport_dgram; |
Stefano Garzarella | 0e12190 | 2019-12-10 11:43:04 +0100 | [diff] [blame] | 139 | /* Transport used for local communication */ |
| 140 | static const struct vsock_transport *transport_local; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 141 | static DEFINE_MUTEX(vsock_register_mutex); |
| 142 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 143 | /**** UTILS ****/ |
| 144 | |
| 145 | /* Each bound VSocket is stored in the bind hash table and each connected |
| 146 | * VSocket is stored in the connected hash table. |
| 147 | * |
| 148 | * Unbound sockets are all put on the same list attached to the end of the hash |
| 149 | * table (vsock_unbound_sockets). Bound sockets are added to the hash table in |
| 150 | * the bucket that their local address hashes to (vsock_bound_sockets(addr) |
| 151 | * represents the list that addr hashes to). |
| 152 | * |
| 153 | * Specifically, we initialize the vsock_bind_table array to a size of |
| 154 | * VSOCK_HASH_SIZE + 1 so that vsock_bind_table[0] through |
| 155 | * vsock_bind_table[VSOCK_HASH_SIZE - 1] are for bound sockets and |
| 156 | * vsock_bind_table[VSOCK_HASH_SIZE] is for unbound sockets. The hash function |
Asias He | a49dd9d | 2013-06-20 17:20:33 +0800 | [diff] [blame] | 157 | * mods with VSOCK_HASH_SIZE to ensure this. |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 158 | */ |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 159 | #define MAX_PORT_RETRIES 24 |
| 160 | |
Asias He | a49dd9d | 2013-06-20 17:20:33 +0800 | [diff] [blame] | 161 | #define VSOCK_HASH(addr) ((addr)->svm_port % VSOCK_HASH_SIZE) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 162 | #define vsock_bound_sockets(addr) (&vsock_bind_table[VSOCK_HASH(addr)]) |
| 163 | #define vsock_unbound_sockets (&vsock_bind_table[VSOCK_HASH_SIZE]) |
| 164 | |
| 165 | /* XXX This can probably be implemented in a better way. */ |
| 166 | #define VSOCK_CONN_HASH(src, dst) \ |
Asias He | a49dd9d | 2013-06-20 17:20:33 +0800 | [diff] [blame] | 167 | (((src)->svm_cid ^ (dst)->svm_port) % VSOCK_HASH_SIZE) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 168 | #define vsock_connected_sockets(src, dst) \ |
| 169 | (&vsock_connected_table[VSOCK_CONN_HASH(src, dst)]) |
| 170 | #define vsock_connected_sockets_vsk(vsk) \ |
| 171 | vsock_connected_sockets(&(vsk)->remote_addr, &(vsk)->local_addr) |
| 172 | |
Stefan Hajnoczi | 44f2098 | 2017-10-05 16:46:50 -0400 | [diff] [blame] | 173 | struct list_head vsock_bind_table[VSOCK_HASH_SIZE + 1]; |
| 174 | EXPORT_SYMBOL_GPL(vsock_bind_table); |
| 175 | struct list_head vsock_connected_table[VSOCK_HASH_SIZE]; |
| 176 | EXPORT_SYMBOL_GPL(vsock_connected_table); |
| 177 | DEFINE_SPINLOCK(vsock_table_lock); |
| 178 | EXPORT_SYMBOL_GPL(vsock_table_lock); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 179 | |
Asias He | b3a6dfe | 2013-06-20 17:20:30 +0800 | [diff] [blame] | 180 | /* Autobind this socket to the local address if necessary. */ |
| 181 | static int vsock_auto_bind(struct vsock_sock *vsk) |
| 182 | { |
| 183 | struct sock *sk = sk_vsock(vsk); |
| 184 | struct sockaddr_vm local_addr; |
| 185 | |
| 186 | if (vsock_addr_bound(&vsk->local_addr)) |
| 187 | return 0; |
| 188 | vsock_addr_init(&local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY); |
| 189 | return __vsock_bind(sk, &local_addr); |
| 190 | } |
| 191 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 192 | static void vsock_init_tables(void) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 193 | { |
| 194 | int i; |
| 195 | |
| 196 | for (i = 0; i < ARRAY_SIZE(vsock_bind_table); i++) |
| 197 | INIT_LIST_HEAD(&vsock_bind_table[i]); |
| 198 | |
| 199 | for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++) |
| 200 | INIT_LIST_HEAD(&vsock_connected_table[i]); |
| 201 | } |
| 202 | |
| 203 | static void __vsock_insert_bound(struct list_head *list, |
| 204 | struct vsock_sock *vsk) |
| 205 | { |
| 206 | sock_hold(&vsk->sk); |
| 207 | list_add(&vsk->bound_table, list); |
| 208 | } |
| 209 | |
| 210 | static void __vsock_insert_connected(struct list_head *list, |
| 211 | struct vsock_sock *vsk) |
| 212 | { |
| 213 | sock_hold(&vsk->sk); |
| 214 | list_add(&vsk->connected_table, list); |
| 215 | } |
| 216 | |
| 217 | static void __vsock_remove_bound(struct vsock_sock *vsk) |
| 218 | { |
| 219 | list_del_init(&vsk->bound_table); |
| 220 | sock_put(&vsk->sk); |
| 221 | } |
| 222 | |
| 223 | static void __vsock_remove_connected(struct vsock_sock *vsk) |
| 224 | { |
| 225 | list_del_init(&vsk->connected_table); |
| 226 | sock_put(&vsk->sk); |
| 227 | } |
| 228 | |
| 229 | static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr) |
| 230 | { |
| 231 | struct vsock_sock *vsk; |
| 232 | |
Stefano Garzarella | 36c5b48 | 2019-11-14 10:57:49 +0100 | [diff] [blame] | 233 | list_for_each_entry(vsk, vsock_bound_sockets(addr), bound_table) { |
| 234 | if (vsock_addr_equals_addr(addr, &vsk->local_addr)) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 235 | return sk_vsock(vsk); |
| 236 | |
Stefano Garzarella | 36c5b48 | 2019-11-14 10:57:49 +0100 | [diff] [blame] | 237 | if (addr->svm_port == vsk->local_addr.svm_port && |
| 238 | (vsk->local_addr.svm_cid == VMADDR_CID_ANY || |
| 239 | addr->svm_cid == VMADDR_CID_ANY)) |
| 240 | return sk_vsock(vsk); |
| 241 | } |
| 242 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 243 | return NULL; |
| 244 | } |
| 245 | |
| 246 | static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src, |
| 247 | struct sockaddr_vm *dst) |
| 248 | { |
| 249 | struct vsock_sock *vsk; |
| 250 | |
| 251 | list_for_each_entry(vsk, vsock_connected_sockets(src, dst), |
| 252 | connected_table) { |
Reilly Grant | 990454b | 2013-04-01 11:41:52 -0700 | [diff] [blame] | 253 | if (vsock_addr_equals_addr(src, &vsk->remote_addr) && |
| 254 | dst->svm_port == vsk->local_addr.svm_port) { |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 255 | return sk_vsock(vsk); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | return NULL; |
| 260 | } |
| 261 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 262 | static void vsock_insert_unbound(struct vsock_sock *vsk) |
| 263 | { |
| 264 | spin_lock_bh(&vsock_table_lock); |
| 265 | __vsock_insert_bound(vsock_unbound_sockets, vsk); |
| 266 | spin_unlock_bh(&vsock_table_lock); |
| 267 | } |
| 268 | |
| 269 | void vsock_insert_connected(struct vsock_sock *vsk) |
| 270 | { |
| 271 | struct list_head *list = vsock_connected_sockets( |
| 272 | &vsk->remote_addr, &vsk->local_addr); |
| 273 | |
| 274 | spin_lock_bh(&vsock_table_lock); |
| 275 | __vsock_insert_connected(list, vsk); |
| 276 | spin_unlock_bh(&vsock_table_lock); |
| 277 | } |
| 278 | EXPORT_SYMBOL_GPL(vsock_insert_connected); |
| 279 | |
| 280 | void vsock_remove_bound(struct vsock_sock *vsk) |
| 281 | { |
| 282 | spin_lock_bh(&vsock_table_lock); |
Sunil Muthuswamy | d5afa82 | 2019-06-13 03:52:27 +0000 | [diff] [blame] | 283 | if (__vsock_in_bound_table(vsk)) |
| 284 | __vsock_remove_bound(vsk); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 285 | spin_unlock_bh(&vsock_table_lock); |
| 286 | } |
| 287 | EXPORT_SYMBOL_GPL(vsock_remove_bound); |
| 288 | |
| 289 | void vsock_remove_connected(struct vsock_sock *vsk) |
| 290 | { |
| 291 | spin_lock_bh(&vsock_table_lock); |
Sunil Muthuswamy | d5afa82 | 2019-06-13 03:52:27 +0000 | [diff] [blame] | 292 | if (__vsock_in_connected_table(vsk)) |
| 293 | __vsock_remove_connected(vsk); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 294 | spin_unlock_bh(&vsock_table_lock); |
| 295 | } |
| 296 | EXPORT_SYMBOL_GPL(vsock_remove_connected); |
| 297 | |
| 298 | struct sock *vsock_find_bound_socket(struct sockaddr_vm *addr) |
| 299 | { |
| 300 | struct sock *sk; |
| 301 | |
| 302 | spin_lock_bh(&vsock_table_lock); |
| 303 | sk = __vsock_find_bound_socket(addr); |
| 304 | if (sk) |
| 305 | sock_hold(sk); |
| 306 | |
| 307 | spin_unlock_bh(&vsock_table_lock); |
| 308 | |
| 309 | return sk; |
| 310 | } |
| 311 | EXPORT_SYMBOL_GPL(vsock_find_bound_socket); |
| 312 | |
| 313 | struct sock *vsock_find_connected_socket(struct sockaddr_vm *src, |
| 314 | struct sockaddr_vm *dst) |
| 315 | { |
| 316 | struct sock *sk; |
| 317 | |
| 318 | spin_lock_bh(&vsock_table_lock); |
| 319 | sk = __vsock_find_connected_socket(src, dst); |
| 320 | if (sk) |
| 321 | sock_hold(sk); |
| 322 | |
| 323 | spin_unlock_bh(&vsock_table_lock); |
| 324 | |
| 325 | return sk; |
| 326 | } |
| 327 | EXPORT_SYMBOL_GPL(vsock_find_connected_socket); |
| 328 | |
Stefan Hajnoczi | 6773b7d | 2016-07-28 15:36:31 +0100 | [diff] [blame] | 329 | void vsock_remove_sock(struct vsock_sock *vsk) |
| 330 | { |
Sunil Muthuswamy | d5afa82 | 2019-06-13 03:52:27 +0000 | [diff] [blame] | 331 | vsock_remove_bound(vsk); |
| 332 | vsock_remove_connected(vsk); |
Stefan Hajnoczi | 6773b7d | 2016-07-28 15:36:31 +0100 | [diff] [blame] | 333 | } |
| 334 | EXPORT_SYMBOL_GPL(vsock_remove_sock); |
| 335 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 336 | void vsock_for_each_connected_socket(void (*fn)(struct sock *sk)) |
| 337 | { |
| 338 | int i; |
| 339 | |
| 340 | spin_lock_bh(&vsock_table_lock); |
| 341 | |
| 342 | for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++) { |
| 343 | struct vsock_sock *vsk; |
| 344 | list_for_each_entry(vsk, &vsock_connected_table[i], |
Julia Lawall | d9af2d6 | 2013-08-05 16:47:38 +0200 | [diff] [blame] | 345 | connected_table) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 346 | fn(sk_vsock(vsk)); |
| 347 | } |
| 348 | |
| 349 | spin_unlock_bh(&vsock_table_lock); |
| 350 | } |
| 351 | EXPORT_SYMBOL_GPL(vsock_for_each_connected_socket); |
| 352 | |
| 353 | void vsock_add_pending(struct sock *listener, struct sock *pending) |
| 354 | { |
| 355 | struct vsock_sock *vlistener; |
| 356 | struct vsock_sock *vpending; |
| 357 | |
| 358 | vlistener = vsock_sk(listener); |
| 359 | vpending = vsock_sk(pending); |
| 360 | |
| 361 | sock_hold(pending); |
| 362 | sock_hold(listener); |
| 363 | list_add_tail(&vpending->pending_links, &vlistener->pending_links); |
| 364 | } |
| 365 | EXPORT_SYMBOL_GPL(vsock_add_pending); |
| 366 | |
| 367 | void vsock_remove_pending(struct sock *listener, struct sock *pending) |
| 368 | { |
| 369 | struct vsock_sock *vpending = vsock_sk(pending); |
| 370 | |
| 371 | list_del_init(&vpending->pending_links); |
| 372 | sock_put(listener); |
| 373 | sock_put(pending); |
| 374 | } |
| 375 | EXPORT_SYMBOL_GPL(vsock_remove_pending); |
| 376 | |
| 377 | void vsock_enqueue_accept(struct sock *listener, struct sock *connected) |
| 378 | { |
| 379 | struct vsock_sock *vlistener; |
| 380 | struct vsock_sock *vconnected; |
| 381 | |
| 382 | vlistener = vsock_sk(listener); |
| 383 | vconnected = vsock_sk(connected); |
| 384 | |
| 385 | sock_hold(connected); |
| 386 | sock_hold(listener); |
| 387 | list_add_tail(&vconnected->accept_queue, &vlistener->accept_queue); |
| 388 | } |
| 389 | EXPORT_SYMBOL_GPL(vsock_enqueue_accept); |
| 390 | |
Stefano Garzarella | 6a2c0962 | 2019-11-14 10:57:48 +0100 | [diff] [blame] | 391 | static void vsock_deassign_transport(struct vsock_sock *vsk) |
| 392 | { |
| 393 | if (!vsk->transport) |
| 394 | return; |
| 395 | |
| 396 | vsk->transport->destruct(vsk); |
| 397 | module_put(vsk->transport->module); |
| 398 | vsk->transport = NULL; |
| 399 | } |
| 400 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 401 | /* Assign a transport to a socket and call the .init transport callback. |
| 402 | * |
| 403 | * Note: for stream socket this must be called when vsk->remote_addr is set |
| 404 | * (e.g. during the connect() or when a connection request on a listener |
| 405 | * socket is received). |
| 406 | * The vsk->remote_addr is used to decide which transport to use: |
| 407 | * - remote CID <= VMADDR_CID_HOST will use guest->host transport; |
| 408 | * - remote CID == local_cid (guest->host transport) will use guest->host |
| 409 | * transport for loopback (host->guest transports don't support loopback); |
| 410 | * - remote CID > VMADDR_CID_HOST will use host->guest transport; |
| 411 | */ |
| 412 | int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk) |
| 413 | { |
| 414 | const struct vsock_transport *new_transport; |
| 415 | struct sock *sk = sk_vsock(vsk); |
| 416 | unsigned int remote_cid = vsk->remote_addr.svm_cid; |
Stefano Garzarella | 039fccc | 2019-11-21 10:06:09 +0100 | [diff] [blame] | 417 | int ret; |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 418 | |
| 419 | switch (sk->sk_type) { |
| 420 | case SOCK_DGRAM: |
| 421 | new_transport = transport_dgram; |
| 422 | break; |
| 423 | case SOCK_STREAM: |
| 424 | if (remote_cid <= VMADDR_CID_HOST || |
| 425 | (transport_g2h && |
| 426 | remote_cid == transport_g2h->get_local_cid())) |
| 427 | new_transport = transport_g2h; |
| 428 | else |
| 429 | new_transport = transport_h2g; |
| 430 | break; |
| 431 | default: |
| 432 | return -ESOCKTNOSUPPORT; |
| 433 | } |
| 434 | |
| 435 | if (vsk->transport) { |
| 436 | if (vsk->transport == new_transport) |
| 437 | return 0; |
| 438 | |
| 439 | vsk->transport->release(vsk); |
Stefano Garzarella | 6a2c0962 | 2019-11-14 10:57:48 +0100 | [diff] [blame] | 440 | vsock_deassign_transport(vsk); |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 441 | } |
| 442 | |
Stefano Garzarella | 6a2c0962 | 2019-11-14 10:57:48 +0100 | [diff] [blame] | 443 | /* We increase the module refcnt to prevent the transport unloading |
| 444 | * while there are open sockets assigned to it. |
| 445 | */ |
| 446 | if (!new_transport || !try_module_get(new_transport->module)) |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 447 | return -ENODEV; |
| 448 | |
Stefano Garzarella | 039fccc | 2019-11-21 10:06:09 +0100 | [diff] [blame] | 449 | ret = new_transport->init(vsk, psk); |
| 450 | if (ret) { |
| 451 | module_put(new_transport->module); |
| 452 | return ret; |
| 453 | } |
| 454 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 455 | vsk->transport = new_transport; |
| 456 | |
Stefano Garzarella | 039fccc | 2019-11-21 10:06:09 +0100 | [diff] [blame] | 457 | return 0; |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 458 | } |
| 459 | EXPORT_SYMBOL_GPL(vsock_assign_transport); |
| 460 | |
| 461 | bool vsock_find_cid(unsigned int cid) |
| 462 | { |
| 463 | if (transport_g2h && cid == transport_g2h->get_local_cid()) |
| 464 | return true; |
| 465 | |
| 466 | if (transport_h2g && cid == VMADDR_CID_HOST) |
| 467 | return true; |
| 468 | |
| 469 | return false; |
| 470 | } |
| 471 | EXPORT_SYMBOL_GPL(vsock_find_cid); |
| 472 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 473 | static struct sock *vsock_dequeue_accept(struct sock *listener) |
| 474 | { |
| 475 | struct vsock_sock *vlistener; |
| 476 | struct vsock_sock *vconnected; |
| 477 | |
| 478 | vlistener = vsock_sk(listener); |
| 479 | |
| 480 | if (list_empty(&vlistener->accept_queue)) |
| 481 | return NULL; |
| 482 | |
| 483 | vconnected = list_entry(vlistener->accept_queue.next, |
| 484 | struct vsock_sock, accept_queue); |
| 485 | |
| 486 | list_del_init(&vconnected->accept_queue); |
| 487 | sock_put(listener); |
| 488 | /* The caller will need a reference on the connected socket so we let |
| 489 | * it call sock_put(). |
| 490 | */ |
| 491 | |
| 492 | return sk_vsock(vconnected); |
| 493 | } |
| 494 | |
| 495 | static bool vsock_is_accept_queue_empty(struct sock *sk) |
| 496 | { |
| 497 | struct vsock_sock *vsk = vsock_sk(sk); |
| 498 | return list_empty(&vsk->accept_queue); |
| 499 | } |
| 500 | |
| 501 | static bool vsock_is_pending(struct sock *sk) |
| 502 | { |
| 503 | struct vsock_sock *vsk = vsock_sk(sk); |
| 504 | return !list_empty(&vsk->pending_links); |
| 505 | } |
| 506 | |
| 507 | static int vsock_send_shutdown(struct sock *sk, int mode) |
| 508 | { |
Stefano Garzarella | fe502c4 | 2019-11-14 10:57:39 +0100 | [diff] [blame] | 509 | struct vsock_sock *vsk = vsock_sk(sk); |
| 510 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 511 | if (!vsk->transport) |
| 512 | return -ENODEV; |
| 513 | |
Stefano Garzarella | fe502c4 | 2019-11-14 10:57:39 +0100 | [diff] [blame] | 514 | return vsk->transport->shutdown(vsk, mode); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Cong Wang | 455f05e | 2018-08-06 11:06:02 -0700 | [diff] [blame] | 517 | static void vsock_pending_work(struct work_struct *work) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 518 | { |
| 519 | struct sock *sk; |
| 520 | struct sock *listener; |
| 521 | struct vsock_sock *vsk; |
| 522 | bool cleanup; |
| 523 | |
Cong Wang | 455f05e | 2018-08-06 11:06:02 -0700 | [diff] [blame] | 524 | vsk = container_of(work, struct vsock_sock, pending_work.work); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 525 | sk = sk_vsock(vsk); |
| 526 | listener = vsk->listener; |
| 527 | cleanup = true; |
| 528 | |
| 529 | lock_sock(listener); |
Stefan Hajnoczi | 4192f67 | 2016-06-23 16:28:58 +0100 | [diff] [blame] | 530 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 531 | |
| 532 | if (vsock_is_pending(sk)) { |
| 533 | vsock_remove_pending(listener, sk); |
Jorgen Hansen | 1190cfd | 2016-09-26 23:59:53 -0700 | [diff] [blame] | 534 | |
Eric Dumazet | 7976a11 | 2019-11-05 14:11:52 -0800 | [diff] [blame] | 535 | sk_acceptq_removed(listener); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 536 | } else if (!vsk->rejected) { |
| 537 | /* We are not on the pending list and accept() did not reject |
| 538 | * us, so we must have been accepted by our user process. We |
| 539 | * just need to drop our references to the sockets and be on |
| 540 | * our way. |
| 541 | */ |
| 542 | cleanup = false; |
| 543 | goto out; |
| 544 | } |
| 545 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 546 | /* We need to remove ourself from the global connected sockets list so |
| 547 | * incoming packets can't find this socket, and to reduce the reference |
| 548 | * count. |
| 549 | */ |
Sunil Muthuswamy | d5afa82 | 2019-06-13 03:52:27 +0000 | [diff] [blame] | 550 | vsock_remove_connected(vsk); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 551 | |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 552 | sk->sk_state = TCP_CLOSE; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 553 | |
| 554 | out: |
| 555 | release_sock(sk); |
| 556 | release_sock(listener); |
| 557 | if (cleanup) |
| 558 | sock_put(sk); |
| 559 | |
| 560 | sock_put(sk); |
| 561 | sock_put(listener); |
| 562 | } |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 563 | |
| 564 | /**** SOCKET OPERATIONS ****/ |
| 565 | |
| 566 | static int __vsock_bind_stream(struct vsock_sock *vsk, |
| 567 | struct sockaddr_vm *addr) |
| 568 | { |
Lepton Wu | a22d325 | 2019-01-09 15:45:41 -0800 | [diff] [blame] | 569 | static u32 port; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 570 | struct sockaddr_vm new_addr; |
| 571 | |
Lepton Wu | 8236b08 | 2018-12-11 11:12:55 -0800 | [diff] [blame] | 572 | if (!port) |
| 573 | port = LAST_RESERVED_PORT + 1 + |
| 574 | prandom_u32_max(U32_MAX - LAST_RESERVED_PORT); |
| 575 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 576 | vsock_addr_init(&new_addr, addr->svm_cid, addr->svm_port); |
| 577 | |
| 578 | if (addr->svm_port == VMADDR_PORT_ANY) { |
| 579 | bool found = false; |
| 580 | unsigned int i; |
| 581 | |
| 582 | for (i = 0; i < MAX_PORT_RETRIES; i++) { |
| 583 | if (port <= LAST_RESERVED_PORT) |
| 584 | port = LAST_RESERVED_PORT + 1; |
| 585 | |
| 586 | new_addr.svm_port = port++; |
| 587 | |
| 588 | if (!__vsock_find_bound_socket(&new_addr)) { |
| 589 | found = true; |
| 590 | break; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | if (!found) |
| 595 | return -EADDRNOTAVAIL; |
| 596 | } else { |
| 597 | /* If port is in reserved range, ensure caller |
| 598 | * has necessary privileges. |
| 599 | */ |
| 600 | if (addr->svm_port <= LAST_RESERVED_PORT && |
| 601 | !capable(CAP_NET_BIND_SERVICE)) { |
| 602 | return -EACCES; |
| 603 | } |
| 604 | |
| 605 | if (__vsock_find_bound_socket(&new_addr)) |
| 606 | return -EADDRINUSE; |
| 607 | } |
| 608 | |
| 609 | vsock_addr_init(&vsk->local_addr, new_addr.svm_cid, new_addr.svm_port); |
| 610 | |
| 611 | /* Remove stream sockets from the unbound list and add them to the hash |
| 612 | * table for easy lookup by its address. The unbound list is simply an |
| 613 | * extra entry at the end of the hash table, a trick used by AF_UNIX. |
| 614 | */ |
| 615 | __vsock_remove_bound(vsk); |
| 616 | __vsock_insert_bound(vsock_bound_sockets(&vsk->local_addr), vsk); |
| 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | static int __vsock_bind_dgram(struct vsock_sock *vsk, |
| 622 | struct sockaddr_vm *addr) |
| 623 | { |
Stefano Garzarella | fe502c4 | 2019-11-14 10:57:39 +0100 | [diff] [blame] | 624 | return vsk->transport->dgram_bind(vsk, addr); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr) |
| 628 | { |
| 629 | struct vsock_sock *vsk = vsock_sk(sk); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 630 | int retval; |
| 631 | |
| 632 | /* First ensure this socket isn't already bound. */ |
| 633 | if (vsock_addr_bound(&vsk->local_addr)) |
| 634 | return -EINVAL; |
| 635 | |
| 636 | /* Now bind to the provided address or select appropriate values if |
| 637 | * none are provided (VMADDR_CID_ANY and VMADDR_PORT_ANY). Note that |
| 638 | * like AF_INET prevents binding to a non-local IP address (in most |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 639 | * cases), we only allow binding to a local CID. |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 640 | */ |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 641 | if (addr->svm_cid != VMADDR_CID_ANY && !vsock_find_cid(addr->svm_cid)) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 642 | return -EADDRNOTAVAIL; |
| 643 | |
| 644 | switch (sk->sk_socket->type) { |
| 645 | case SOCK_STREAM: |
| 646 | spin_lock_bh(&vsock_table_lock); |
| 647 | retval = __vsock_bind_stream(vsk, addr); |
| 648 | spin_unlock_bh(&vsock_table_lock); |
| 649 | break; |
| 650 | |
| 651 | case SOCK_DGRAM: |
| 652 | retval = __vsock_bind_dgram(vsk, addr); |
| 653 | break; |
| 654 | |
| 655 | default: |
| 656 | retval = -EINVAL; |
| 657 | break; |
| 658 | } |
| 659 | |
| 660 | return retval; |
| 661 | } |
| 662 | |
Cong Wang | 455f05e | 2018-08-06 11:06:02 -0700 | [diff] [blame] | 663 | static void vsock_connect_timeout(struct work_struct *work); |
| 664 | |
Stefano Garzarella | b9ca2f5 | 2019-11-14 10:57:43 +0100 | [diff] [blame] | 665 | static struct sock *__vsock_create(struct net *net, |
| 666 | struct socket *sock, |
| 667 | struct sock *parent, |
| 668 | gfp_t priority, |
| 669 | unsigned short type, |
| 670 | int kern) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 671 | { |
| 672 | struct sock *sk; |
| 673 | struct vsock_sock *psk; |
| 674 | struct vsock_sock *vsk; |
| 675 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 676 | sk = sk_alloc(net, AF_VSOCK, priority, &vsock_proto, kern); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 677 | if (!sk) |
| 678 | return NULL; |
| 679 | |
| 680 | sock_init_data(sock, sk); |
| 681 | |
| 682 | /* sk->sk_type is normally set in sock_init_data, but only if sock is |
| 683 | * non-NULL. We make sure that our sockets always have a type by |
| 684 | * setting it here if needed. |
| 685 | */ |
| 686 | if (!sock) |
| 687 | sk->sk_type = type; |
| 688 | |
| 689 | vsk = vsock_sk(sk); |
| 690 | vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY); |
| 691 | vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY); |
| 692 | |
| 693 | sk->sk_destruct = vsock_sk_destruct; |
| 694 | sk->sk_backlog_rcv = vsock_queue_rcv_skb; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 695 | sock_reset_flag(sk, SOCK_DONE); |
| 696 | |
| 697 | INIT_LIST_HEAD(&vsk->bound_table); |
| 698 | INIT_LIST_HEAD(&vsk->connected_table); |
| 699 | vsk->listener = NULL; |
| 700 | INIT_LIST_HEAD(&vsk->pending_links); |
| 701 | INIT_LIST_HEAD(&vsk->accept_queue); |
| 702 | vsk->rejected = false; |
| 703 | vsk->sent_request = false; |
| 704 | vsk->ignore_connecting_rst = false; |
| 705 | vsk->peer_shutdown = 0; |
Cong Wang | 455f05e | 2018-08-06 11:06:02 -0700 | [diff] [blame] | 706 | INIT_DELAYED_WORK(&vsk->connect_work, vsock_connect_timeout); |
| 707 | INIT_DELAYED_WORK(&vsk->pending_work, vsock_pending_work); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 708 | |
| 709 | psk = parent ? vsock_sk(parent) : NULL; |
| 710 | if (parent) { |
| 711 | vsk->trusted = psk->trusted; |
| 712 | vsk->owner = get_cred(psk->owner); |
| 713 | vsk->connect_timeout = psk->connect_timeout; |
Stefano Garzarella | b9f2b0f | 2019-11-14 10:57:42 +0100 | [diff] [blame] | 714 | vsk->buffer_size = psk->buffer_size; |
| 715 | vsk->buffer_min_size = psk->buffer_min_size; |
| 716 | vsk->buffer_max_size = psk->buffer_max_size; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 717 | } else { |
| 718 | vsk->trusted = capable(CAP_NET_ADMIN); |
| 719 | vsk->owner = get_current_cred(); |
| 720 | vsk->connect_timeout = VSOCK_DEFAULT_CONNECT_TIMEOUT; |
Stefano Garzarella | b9f2b0f | 2019-11-14 10:57:42 +0100 | [diff] [blame] | 721 | vsk->buffer_size = VSOCK_DEFAULT_BUFFER_SIZE; |
| 722 | vsk->buffer_min_size = VSOCK_DEFAULT_BUFFER_MIN_SIZE; |
| 723 | vsk->buffer_max_size = VSOCK_DEFAULT_BUFFER_MAX_SIZE; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 726 | return sk; |
| 727 | } |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 728 | |
Dexuan Cui | 0d9138f | 2019-09-30 18:43:50 +0000 | [diff] [blame] | 729 | static void __vsock_release(struct sock *sk, int level) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 730 | { |
| 731 | if (sk) { |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 732 | struct sock *pending; |
| 733 | struct vsock_sock *vsk; |
| 734 | |
| 735 | vsk = vsock_sk(sk); |
| 736 | pending = NULL; /* Compiler warning. */ |
| 737 | |
Dexuan Cui | 0d9138f | 2019-09-30 18:43:50 +0000 | [diff] [blame] | 738 | /* The release call is supposed to use lock_sock_nested() |
| 739 | * rather than lock_sock(), if a sock lock should be acquired. |
| 740 | */ |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 741 | if (vsk->transport) |
| 742 | vsk->transport->release(vsk); |
| 743 | else if (sk->sk_type == SOCK_STREAM) |
| 744 | vsock_remove_sock(vsk); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 745 | |
Dexuan Cui | 0d9138f | 2019-09-30 18:43:50 +0000 | [diff] [blame] | 746 | /* When "level" is SINGLE_DEPTH_NESTING, use the nested |
| 747 | * version to avoid the warning "possible recursive locking |
| 748 | * detected". When "level" is 0, lock_sock_nested(sk, level) |
| 749 | * is the same as lock_sock(sk). |
| 750 | */ |
| 751 | lock_sock_nested(sk, level); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 752 | sock_orphan(sk); |
| 753 | sk->sk_shutdown = SHUTDOWN_MASK; |
| 754 | |
Christophe JAILLET | 3b7ad08 | 2019-11-03 07:11:11 +0100 | [diff] [blame] | 755 | skb_queue_purge(&sk->sk_receive_queue); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 756 | |
| 757 | /* Clean up any sockets that never were accepted. */ |
| 758 | while ((pending = vsock_dequeue_accept(sk)) != NULL) { |
Dexuan Cui | 0d9138f | 2019-09-30 18:43:50 +0000 | [diff] [blame] | 759 | __vsock_release(pending, SINGLE_DEPTH_NESTING); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 760 | sock_put(pending); |
| 761 | } |
| 762 | |
| 763 | release_sock(sk); |
| 764 | sock_put(sk); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | static void vsock_sk_destruct(struct sock *sk) |
| 769 | { |
| 770 | struct vsock_sock *vsk = vsock_sk(sk); |
| 771 | |
Stefano Garzarella | 6a2c0962 | 2019-11-14 10:57:48 +0100 | [diff] [blame] | 772 | vsock_deassign_transport(vsk); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 773 | |
| 774 | /* When clearing these addresses, there's no need to set the family and |
| 775 | * possibly register the address family with the kernel. |
| 776 | */ |
| 777 | vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY); |
| 778 | vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY); |
| 779 | |
| 780 | put_cred(vsk->owner); |
| 781 | } |
| 782 | |
| 783 | static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) |
| 784 | { |
| 785 | int err; |
| 786 | |
| 787 | err = sock_queue_rcv_skb(sk, skb); |
| 788 | if (err) |
| 789 | kfree_skb(skb); |
| 790 | |
| 791 | return err; |
| 792 | } |
| 793 | |
Stefano Garzarella | b9ca2f5 | 2019-11-14 10:57:43 +0100 | [diff] [blame] | 794 | struct sock *vsock_create_connected(struct sock *parent) |
| 795 | { |
| 796 | return __vsock_create(sock_net(parent), NULL, parent, GFP_KERNEL, |
| 797 | parent->sk_type, 0); |
| 798 | } |
| 799 | EXPORT_SYMBOL_GPL(vsock_create_connected); |
| 800 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 801 | s64 vsock_stream_has_data(struct vsock_sock *vsk) |
| 802 | { |
Stefano Garzarella | fe502c4 | 2019-11-14 10:57:39 +0100 | [diff] [blame] | 803 | return vsk->transport->stream_has_data(vsk); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 804 | } |
| 805 | EXPORT_SYMBOL_GPL(vsock_stream_has_data); |
| 806 | |
| 807 | s64 vsock_stream_has_space(struct vsock_sock *vsk) |
| 808 | { |
Stefano Garzarella | fe502c4 | 2019-11-14 10:57:39 +0100 | [diff] [blame] | 809 | return vsk->transport->stream_has_space(vsk); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 810 | } |
| 811 | EXPORT_SYMBOL_GPL(vsock_stream_has_space); |
| 812 | |
| 813 | static int vsock_release(struct socket *sock) |
| 814 | { |
Dexuan Cui | 0d9138f | 2019-09-30 18:43:50 +0000 | [diff] [blame] | 815 | __vsock_release(sock->sk, 0); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 816 | sock->sk = NULL; |
| 817 | sock->state = SS_FREE; |
| 818 | |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | static int |
| 823 | vsock_bind(struct socket *sock, struct sockaddr *addr, int addr_len) |
| 824 | { |
| 825 | int err; |
| 826 | struct sock *sk; |
| 827 | struct sockaddr_vm *vm_addr; |
| 828 | |
| 829 | sk = sock->sk; |
| 830 | |
| 831 | if (vsock_addr_cast(addr, addr_len, &vm_addr) != 0) |
| 832 | return -EINVAL; |
| 833 | |
| 834 | lock_sock(sk); |
| 835 | err = __vsock_bind(sk, vm_addr); |
| 836 | release_sock(sk); |
| 837 | |
| 838 | return err; |
| 839 | } |
| 840 | |
| 841 | static int vsock_getname(struct socket *sock, |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 842 | struct sockaddr *addr, int peer) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 843 | { |
| 844 | int err; |
| 845 | struct sock *sk; |
| 846 | struct vsock_sock *vsk; |
| 847 | struct sockaddr_vm *vm_addr; |
| 848 | |
| 849 | sk = sock->sk; |
| 850 | vsk = vsock_sk(sk); |
| 851 | err = 0; |
| 852 | |
| 853 | lock_sock(sk); |
| 854 | |
| 855 | if (peer) { |
| 856 | if (sock->state != SS_CONNECTED) { |
| 857 | err = -ENOTCONN; |
| 858 | goto out; |
| 859 | } |
| 860 | vm_addr = &vsk->remote_addr; |
| 861 | } else { |
| 862 | vm_addr = &vsk->local_addr; |
| 863 | } |
| 864 | |
| 865 | if (!vm_addr) { |
| 866 | err = -EINVAL; |
| 867 | goto out; |
| 868 | } |
| 869 | |
| 870 | /* sys_getsockname() and sys_getpeername() pass us a |
| 871 | * MAX_SOCK_ADDR-sized buffer and don't set addr_len. Unfortunately |
| 872 | * that macro is defined in socket.c instead of .h, so we hardcode its |
| 873 | * value here. |
| 874 | */ |
| 875 | BUILD_BUG_ON(sizeof(*vm_addr) > 128); |
| 876 | memcpy(addr, vm_addr, sizeof(*vm_addr)); |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 877 | err = sizeof(*vm_addr); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 878 | |
| 879 | out: |
| 880 | release_sock(sk); |
| 881 | return err; |
| 882 | } |
| 883 | |
| 884 | static int vsock_shutdown(struct socket *sock, int mode) |
| 885 | { |
| 886 | int err; |
| 887 | struct sock *sk; |
| 888 | |
| 889 | /* User level uses SHUT_RD (0) and SHUT_WR (1), but the kernel uses |
| 890 | * RCV_SHUTDOWN (1) and SEND_SHUTDOWN (2), so we must increment mode |
| 891 | * here like the other address families do. Note also that the |
| 892 | * increment makes SHUT_RDWR (2) into RCV_SHUTDOWN | SEND_SHUTDOWN (3), |
| 893 | * which is what we want. |
| 894 | */ |
| 895 | mode++; |
| 896 | |
| 897 | if ((mode & ~SHUTDOWN_MASK) || !mode) |
| 898 | return -EINVAL; |
| 899 | |
| 900 | /* If this is a STREAM socket and it is not connected then bail out |
| 901 | * immediately. If it is a DGRAM socket then we must first kick the |
| 902 | * socket so that it wakes up from any sleeping calls, for example |
| 903 | * recv(), and then afterwards return the error. |
| 904 | */ |
| 905 | |
| 906 | sk = sock->sk; |
| 907 | if (sock->state == SS_UNCONNECTED) { |
| 908 | err = -ENOTCONN; |
| 909 | if (sk->sk_type == SOCK_STREAM) |
| 910 | return err; |
| 911 | } else { |
| 912 | sock->state = SS_DISCONNECTING; |
| 913 | err = 0; |
| 914 | } |
| 915 | |
| 916 | /* Receive and send shutdowns are treated alike. */ |
| 917 | mode = mode & (RCV_SHUTDOWN | SEND_SHUTDOWN); |
| 918 | if (mode) { |
| 919 | lock_sock(sk); |
| 920 | sk->sk_shutdown |= mode; |
| 921 | sk->sk_state_change(sk); |
| 922 | release_sock(sk); |
| 923 | |
| 924 | if (sk->sk_type == SOCK_STREAM) { |
| 925 | sock_reset_flag(sk, SOCK_DONE); |
| 926 | vsock_send_shutdown(sk, mode); |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | return err; |
| 931 | } |
| 932 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 933 | static __poll_t vsock_poll(struct file *file, struct socket *sock, |
| 934 | poll_table *wait) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 935 | { |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 936 | struct sock *sk; |
| 937 | __poll_t mask; |
| 938 | struct vsock_sock *vsk; |
| 939 | |
| 940 | sk = sock->sk; |
| 941 | vsk = vsock_sk(sk); |
| 942 | |
| 943 | poll_wait(file, sk_sleep(sk), wait); |
| 944 | mask = 0; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 945 | |
| 946 | if (sk->sk_err) |
| 947 | /* Signify that there has been an error on this socket. */ |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 948 | mask |= EPOLLERR; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 949 | |
| 950 | /* INET sockets treat local write shutdown and peer write shutdown as a |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 951 | * case of EPOLLHUP set. |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 952 | */ |
| 953 | if ((sk->sk_shutdown == SHUTDOWN_MASK) || |
| 954 | ((sk->sk_shutdown & SEND_SHUTDOWN) && |
| 955 | (vsk->peer_shutdown & SEND_SHUTDOWN))) { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 956 | mask |= EPOLLHUP; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | if (sk->sk_shutdown & RCV_SHUTDOWN || |
| 960 | vsk->peer_shutdown & SEND_SHUTDOWN) { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 961 | mask |= EPOLLRDHUP; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | if (sock->type == SOCK_DGRAM) { |
| 965 | /* For datagram sockets we can read if there is something in |
| 966 | * the queue and write as long as the socket isn't shutdown for |
| 967 | * sending. |
| 968 | */ |
Eric Dumazet | 3ef7cf5 | 2019-10-23 22:44:50 -0700 | [diff] [blame] | 969 | if (!skb_queue_empty_lockless(&sk->sk_receive_queue) || |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 970 | (sk->sk_shutdown & RCV_SHUTDOWN)) { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 971 | mask |= EPOLLIN | EPOLLRDNORM; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | if (!(sk->sk_shutdown & SEND_SHUTDOWN)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 975 | mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 976 | |
| 977 | } else if (sock->type == SOCK_STREAM) { |
Stefano Garzarella | fe502c4 | 2019-11-14 10:57:39 +0100 | [diff] [blame] | 978 | const struct vsock_transport *transport = vsk->transport; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 979 | lock_sock(sk); |
| 980 | |
| 981 | /* Listening sockets that have connections in their accept |
| 982 | * queue can be read. |
| 983 | */ |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 984 | if (sk->sk_state == TCP_LISTEN |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 985 | && !vsock_is_accept_queue_empty(sk)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 986 | mask |= EPOLLIN | EPOLLRDNORM; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 987 | |
| 988 | /* If there is something in the queue then we can read. */ |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 989 | if (transport && transport->stream_is_active(vsk) && |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 990 | !(sk->sk_shutdown & RCV_SHUTDOWN)) { |
| 991 | bool data_ready_now = false; |
| 992 | int ret = transport->notify_poll_in( |
| 993 | vsk, 1, &data_ready_now); |
| 994 | if (ret < 0) { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 995 | mask |= EPOLLERR; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 996 | } else { |
| 997 | if (data_ready_now) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 998 | mask |= EPOLLIN | EPOLLRDNORM; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 999 | |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | /* Sockets whose connections have been closed, reset, or |
| 1004 | * terminated should also be considered read, and we check the |
| 1005 | * shutdown flag for that. |
| 1006 | */ |
| 1007 | if (sk->sk_shutdown & RCV_SHUTDOWN || |
| 1008 | vsk->peer_shutdown & SEND_SHUTDOWN) { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1009 | mask |= EPOLLIN | EPOLLRDNORM; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | /* Connected sockets that can produce data can be written. */ |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1013 | if (sk->sk_state == TCP_ESTABLISHED) { |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1014 | if (!(sk->sk_shutdown & SEND_SHUTDOWN)) { |
| 1015 | bool space_avail_now = false; |
| 1016 | int ret = transport->notify_poll_out( |
| 1017 | vsk, 1, &space_avail_now); |
| 1018 | if (ret < 0) { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1019 | mask |= EPOLLERR; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1020 | } else { |
| 1021 | if (space_avail_now) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1022 | /* Remove EPOLLWRBAND since INET |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1023 | * sockets are not setting it. |
| 1024 | */ |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1025 | mask |= EPOLLOUT | EPOLLWRNORM; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1026 | |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | /* Simulate INET socket poll behaviors, which sets |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1032 | * EPOLLOUT|EPOLLWRNORM when peer is closed and nothing to read, |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1033 | * but local send is not shutdown. |
| 1034 | */ |
Stefan Hajnoczi | ba3169f | 2018-01-26 11:48:25 +0000 | [diff] [blame] | 1035 | if (sk->sk_state == TCP_CLOSE || sk->sk_state == TCP_CLOSING) { |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1036 | if (!(sk->sk_shutdown & SEND_SHUTDOWN)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1037 | mask |= EPOLLOUT | EPOLLWRNORM; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1038 | |
| 1039 | } |
| 1040 | |
| 1041 | release_sock(sk); |
| 1042 | } |
| 1043 | |
| 1044 | return mask; |
| 1045 | } |
| 1046 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1047 | static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg, |
| 1048 | size_t len) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1049 | { |
| 1050 | int err; |
| 1051 | struct sock *sk; |
| 1052 | struct vsock_sock *vsk; |
| 1053 | struct sockaddr_vm *remote_addr; |
Stefano Garzarella | fe502c4 | 2019-11-14 10:57:39 +0100 | [diff] [blame] | 1054 | const struct vsock_transport *transport; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1055 | |
| 1056 | if (msg->msg_flags & MSG_OOB) |
| 1057 | return -EOPNOTSUPP; |
| 1058 | |
| 1059 | /* For now, MSG_DONTWAIT is always assumed... */ |
| 1060 | err = 0; |
| 1061 | sk = sock->sk; |
| 1062 | vsk = vsock_sk(sk); |
Stefano Garzarella | fe502c4 | 2019-11-14 10:57:39 +0100 | [diff] [blame] | 1063 | transport = vsk->transport; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1064 | |
| 1065 | lock_sock(sk); |
| 1066 | |
Asias He | b3a6dfe | 2013-06-20 17:20:30 +0800 | [diff] [blame] | 1067 | err = vsock_auto_bind(vsk); |
| 1068 | if (err) |
| 1069 | goto out; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1070 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1071 | |
| 1072 | /* If the provided message contains an address, use that. Otherwise |
| 1073 | * fall back on the socket's remote handle (if it has been connected). |
| 1074 | */ |
| 1075 | if (msg->msg_name && |
| 1076 | vsock_addr_cast(msg->msg_name, msg->msg_namelen, |
| 1077 | &remote_addr) == 0) { |
| 1078 | /* Ensure this address is of the right type and is a valid |
| 1079 | * destination. |
| 1080 | */ |
| 1081 | |
| 1082 | if (remote_addr->svm_cid == VMADDR_CID_ANY) |
| 1083 | remote_addr->svm_cid = transport->get_local_cid(); |
| 1084 | |
| 1085 | if (!vsock_addr_bound(remote_addr)) { |
| 1086 | err = -EINVAL; |
| 1087 | goto out; |
| 1088 | } |
| 1089 | } else if (sock->state == SS_CONNECTED) { |
| 1090 | remote_addr = &vsk->remote_addr; |
| 1091 | |
| 1092 | if (remote_addr->svm_cid == VMADDR_CID_ANY) |
| 1093 | remote_addr->svm_cid = transport->get_local_cid(); |
| 1094 | |
| 1095 | /* XXX Should connect() or this function ensure remote_addr is |
| 1096 | * bound? |
| 1097 | */ |
| 1098 | if (!vsock_addr_bound(&vsk->remote_addr)) { |
| 1099 | err = -EINVAL; |
| 1100 | goto out; |
| 1101 | } |
| 1102 | } else { |
| 1103 | err = -EINVAL; |
| 1104 | goto out; |
| 1105 | } |
| 1106 | |
| 1107 | if (!transport->dgram_allow(remote_addr->svm_cid, |
| 1108 | remote_addr->svm_port)) { |
| 1109 | err = -EINVAL; |
| 1110 | goto out; |
| 1111 | } |
| 1112 | |
Al Viro | 0f7db23 | 2014-11-20 04:05:34 -0500 | [diff] [blame] | 1113 | err = transport->dgram_enqueue(vsk, remote_addr, msg, len); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1114 | |
| 1115 | out: |
| 1116 | release_sock(sk); |
| 1117 | return err; |
| 1118 | } |
| 1119 | |
| 1120 | static int vsock_dgram_connect(struct socket *sock, |
| 1121 | struct sockaddr *addr, int addr_len, int flags) |
| 1122 | { |
| 1123 | int err; |
| 1124 | struct sock *sk; |
| 1125 | struct vsock_sock *vsk; |
| 1126 | struct sockaddr_vm *remote_addr; |
| 1127 | |
| 1128 | sk = sock->sk; |
| 1129 | vsk = vsock_sk(sk); |
| 1130 | |
| 1131 | err = vsock_addr_cast(addr, addr_len, &remote_addr); |
| 1132 | if (err == -EAFNOSUPPORT && remote_addr->svm_family == AF_UNSPEC) { |
| 1133 | lock_sock(sk); |
| 1134 | vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, |
| 1135 | VMADDR_PORT_ANY); |
| 1136 | sock->state = SS_UNCONNECTED; |
| 1137 | release_sock(sk); |
| 1138 | return 0; |
| 1139 | } else if (err != 0) |
| 1140 | return -EINVAL; |
| 1141 | |
| 1142 | lock_sock(sk); |
| 1143 | |
Asias He | b3a6dfe | 2013-06-20 17:20:30 +0800 | [diff] [blame] | 1144 | err = vsock_auto_bind(vsk); |
| 1145 | if (err) |
| 1146 | goto out; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1147 | |
Stefano Garzarella | fe502c4 | 2019-11-14 10:57:39 +0100 | [diff] [blame] | 1148 | if (!vsk->transport->dgram_allow(remote_addr->svm_cid, |
| 1149 | remote_addr->svm_port)) { |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1150 | err = -EINVAL; |
| 1151 | goto out; |
| 1152 | } |
| 1153 | |
| 1154 | memcpy(&vsk->remote_addr, remote_addr, sizeof(vsk->remote_addr)); |
| 1155 | sock->state = SS_CONNECTED; |
| 1156 | |
| 1157 | out: |
| 1158 | release_sock(sk); |
| 1159 | return err; |
| 1160 | } |
| 1161 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1162 | static int vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg, |
| 1163 | size_t len, int flags) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1164 | { |
Stefano Garzarella | fe502c4 | 2019-11-14 10:57:39 +0100 | [diff] [blame] | 1165 | struct vsock_sock *vsk = vsock_sk(sock->sk); |
| 1166 | |
| 1167 | return vsk->transport->dgram_dequeue(vsk, msg, len, flags); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | static const struct proto_ops vsock_dgram_ops = { |
| 1171 | .family = PF_VSOCK, |
| 1172 | .owner = THIS_MODULE, |
| 1173 | .release = vsock_release, |
| 1174 | .bind = vsock_bind, |
| 1175 | .connect = vsock_dgram_connect, |
| 1176 | .socketpair = sock_no_socketpair, |
| 1177 | .accept = sock_no_accept, |
| 1178 | .getname = vsock_getname, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1179 | .poll = vsock_poll, |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1180 | .ioctl = sock_no_ioctl, |
| 1181 | .listen = sock_no_listen, |
| 1182 | .shutdown = vsock_shutdown, |
| 1183 | .setsockopt = sock_no_setsockopt, |
| 1184 | .getsockopt = sock_no_getsockopt, |
| 1185 | .sendmsg = vsock_dgram_sendmsg, |
| 1186 | .recvmsg = vsock_dgram_recvmsg, |
| 1187 | .mmap = sock_no_mmap, |
| 1188 | .sendpage = sock_no_sendpage, |
| 1189 | }; |
| 1190 | |
Peng Tao | 380feae | 2017-03-15 09:32:17 +0800 | [diff] [blame] | 1191 | static int vsock_transport_cancel_pkt(struct vsock_sock *vsk) |
| 1192 | { |
Stefano Garzarella | fe502c4 | 2019-11-14 10:57:39 +0100 | [diff] [blame] | 1193 | const struct vsock_transport *transport = vsk->transport; |
| 1194 | |
Peng Tao | 380feae | 2017-03-15 09:32:17 +0800 | [diff] [blame] | 1195 | if (!transport->cancel_pkt) |
| 1196 | return -EOPNOTSUPP; |
| 1197 | |
| 1198 | return transport->cancel_pkt(vsk); |
| 1199 | } |
| 1200 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1201 | static void vsock_connect_timeout(struct work_struct *work) |
| 1202 | { |
| 1203 | struct sock *sk; |
| 1204 | struct vsock_sock *vsk; |
Peng Tao | 380feae | 2017-03-15 09:32:17 +0800 | [diff] [blame] | 1205 | int cancel = 0; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1206 | |
Cong Wang | 455f05e | 2018-08-06 11:06:02 -0700 | [diff] [blame] | 1207 | vsk = container_of(work, struct vsock_sock, connect_work.work); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1208 | sk = sk_vsock(vsk); |
| 1209 | |
| 1210 | lock_sock(sk); |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1211 | if (sk->sk_state == TCP_SYN_SENT && |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1212 | (sk->sk_shutdown != SHUTDOWN_MASK)) { |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1213 | sk->sk_state = TCP_CLOSE; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1214 | sk->sk_err = ETIMEDOUT; |
| 1215 | sk->sk_error_report(sk); |
Peng Tao | 380feae | 2017-03-15 09:32:17 +0800 | [diff] [blame] | 1216 | cancel = 1; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1217 | } |
| 1218 | release_sock(sk); |
Peng Tao | 380feae | 2017-03-15 09:32:17 +0800 | [diff] [blame] | 1219 | if (cancel) |
| 1220 | vsock_transport_cancel_pkt(vsk); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1221 | |
| 1222 | sock_put(sk); |
| 1223 | } |
| 1224 | |
| 1225 | static int vsock_stream_connect(struct socket *sock, struct sockaddr *addr, |
| 1226 | int addr_len, int flags) |
| 1227 | { |
| 1228 | int err; |
| 1229 | struct sock *sk; |
| 1230 | struct vsock_sock *vsk; |
Stefano Garzarella | fe502c4 | 2019-11-14 10:57:39 +0100 | [diff] [blame] | 1231 | const struct vsock_transport *transport; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1232 | struct sockaddr_vm *remote_addr; |
| 1233 | long timeout; |
| 1234 | DEFINE_WAIT(wait); |
| 1235 | |
| 1236 | err = 0; |
| 1237 | sk = sock->sk; |
| 1238 | vsk = vsock_sk(sk); |
| 1239 | |
| 1240 | lock_sock(sk); |
| 1241 | |
| 1242 | /* XXX AF_UNSPEC should make us disconnect like AF_INET. */ |
| 1243 | switch (sock->state) { |
| 1244 | case SS_CONNECTED: |
| 1245 | err = -EISCONN; |
| 1246 | goto out; |
| 1247 | case SS_DISCONNECTING: |
| 1248 | err = -EINVAL; |
| 1249 | goto out; |
| 1250 | case SS_CONNECTING: |
| 1251 | /* This continues on so we can move sock into the SS_CONNECTED |
| 1252 | * state once the connection has completed (at which point err |
| 1253 | * will be set to zero also). Otherwise, we will either wait |
| 1254 | * for the connection or return -EALREADY should this be a |
| 1255 | * non-blocking call. |
| 1256 | */ |
| 1257 | err = -EALREADY; |
| 1258 | break; |
| 1259 | default: |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1260 | if ((sk->sk_state == TCP_LISTEN) || |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1261 | vsock_addr_cast(addr, addr_len, &remote_addr) != 0) { |
| 1262 | err = -EINVAL; |
| 1263 | goto out; |
| 1264 | } |
| 1265 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 1266 | /* Set the remote address that we are connecting to. */ |
| 1267 | memcpy(&vsk->remote_addr, remote_addr, |
| 1268 | sizeof(vsk->remote_addr)); |
| 1269 | |
| 1270 | err = vsock_assign_transport(vsk, NULL); |
| 1271 | if (err) |
| 1272 | goto out; |
| 1273 | |
| 1274 | transport = vsk->transport; |
| 1275 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1276 | /* The hypervisor and well-known contexts do not have socket |
| 1277 | * endpoints. |
| 1278 | */ |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 1279 | if (!transport || |
| 1280 | !transport->stream_allow(remote_addr->svm_cid, |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1281 | remote_addr->svm_port)) { |
| 1282 | err = -ENETUNREACH; |
| 1283 | goto out; |
| 1284 | } |
| 1285 | |
Asias He | b3a6dfe | 2013-06-20 17:20:30 +0800 | [diff] [blame] | 1286 | err = vsock_auto_bind(vsk); |
| 1287 | if (err) |
| 1288 | goto out; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1289 | |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1290 | sk->sk_state = TCP_SYN_SENT; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1291 | |
| 1292 | err = transport->connect(vsk); |
| 1293 | if (err < 0) |
| 1294 | goto out; |
| 1295 | |
| 1296 | /* Mark sock as connecting and set the error code to in |
| 1297 | * progress in case this is a non-blocking connect. |
| 1298 | */ |
| 1299 | sock->state = SS_CONNECTING; |
| 1300 | err = -EINPROGRESS; |
| 1301 | } |
| 1302 | |
| 1303 | /* The receive path will handle all communication until we are able to |
| 1304 | * enter the connected state. Here we wait for the connection to be |
| 1305 | * completed or a notification of an error. |
| 1306 | */ |
| 1307 | timeout = vsk->connect_timeout; |
| 1308 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
| 1309 | |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1310 | while (sk->sk_state != TCP_ESTABLISHED && sk->sk_err == 0) { |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1311 | if (flags & O_NONBLOCK) { |
| 1312 | /* If we're not going to block, we schedule a timeout |
| 1313 | * function to generate a timeout on the connection |
| 1314 | * attempt, in case the peer doesn't respond in a |
| 1315 | * timely manner. We hold on to the socket until the |
| 1316 | * timeout fires. |
| 1317 | */ |
| 1318 | sock_hold(sk); |
Cong Wang | 455f05e | 2018-08-06 11:06:02 -0700 | [diff] [blame] | 1319 | schedule_delayed_work(&vsk->connect_work, timeout); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1320 | |
| 1321 | /* Skip ahead to preserve error code set above. */ |
| 1322 | goto out_wait; |
| 1323 | } |
| 1324 | |
| 1325 | release_sock(sk); |
| 1326 | timeout = schedule_timeout(timeout); |
| 1327 | lock_sock(sk); |
| 1328 | |
| 1329 | if (signal_pending(current)) { |
| 1330 | err = sock_intr_errno(timeout); |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1331 | sk->sk_state = TCP_CLOSE; |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1332 | sock->state = SS_UNCONNECTED; |
Peng Tao | 380feae | 2017-03-15 09:32:17 +0800 | [diff] [blame] | 1333 | vsock_transport_cancel_pkt(vsk); |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1334 | goto out_wait; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1335 | } else if (timeout == 0) { |
| 1336 | err = -ETIMEDOUT; |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1337 | sk->sk_state = TCP_CLOSE; |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1338 | sock->state = SS_UNCONNECTED; |
Peng Tao | 380feae | 2017-03-15 09:32:17 +0800 | [diff] [blame] | 1339 | vsock_transport_cancel_pkt(vsk); |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1340 | goto out_wait; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
| 1344 | } |
| 1345 | |
| 1346 | if (sk->sk_err) { |
| 1347 | err = -sk->sk_err; |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1348 | sk->sk_state = TCP_CLOSE; |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1349 | sock->state = SS_UNCONNECTED; |
| 1350 | } else { |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1351 | err = 0; |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1352 | } |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1353 | |
| 1354 | out_wait: |
| 1355 | finish_wait(sk_sleep(sk), &wait); |
| 1356 | out: |
| 1357 | release_sock(sk); |
| 1358 | return err; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 1361 | static int vsock_accept(struct socket *sock, struct socket *newsock, int flags, |
| 1362 | bool kern) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1363 | { |
| 1364 | struct sock *listener; |
| 1365 | int err; |
| 1366 | struct sock *connected; |
| 1367 | struct vsock_sock *vconnected; |
| 1368 | long timeout; |
| 1369 | DEFINE_WAIT(wait); |
| 1370 | |
| 1371 | err = 0; |
| 1372 | listener = sock->sk; |
| 1373 | |
| 1374 | lock_sock(listener); |
| 1375 | |
| 1376 | if (sock->type != SOCK_STREAM) { |
| 1377 | err = -EOPNOTSUPP; |
| 1378 | goto out; |
| 1379 | } |
| 1380 | |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1381 | if (listener->sk_state != TCP_LISTEN) { |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1382 | err = -EINVAL; |
| 1383 | goto out; |
| 1384 | } |
| 1385 | |
| 1386 | /* Wait for children sockets to appear; these are the new sockets |
| 1387 | * created upon connection establishment. |
| 1388 | */ |
| 1389 | timeout = sock_sndtimeo(listener, flags & O_NONBLOCK); |
| 1390 | prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE); |
| 1391 | |
| 1392 | while ((connected = vsock_dequeue_accept(listener)) == NULL && |
| 1393 | listener->sk_err == 0) { |
| 1394 | release_sock(listener); |
| 1395 | timeout = schedule_timeout(timeout); |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1396 | finish_wait(sk_sleep(listener), &wait); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1397 | lock_sock(listener); |
| 1398 | |
| 1399 | if (signal_pending(current)) { |
| 1400 | err = sock_intr_errno(timeout); |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1401 | goto out; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1402 | } else if (timeout == 0) { |
| 1403 | err = -EAGAIN; |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1404 | goto out; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE); |
| 1408 | } |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1409 | finish_wait(sk_sleep(listener), &wait); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1410 | |
| 1411 | if (listener->sk_err) |
| 1412 | err = -listener->sk_err; |
| 1413 | |
| 1414 | if (connected) { |
Eric Dumazet | 7976a11 | 2019-11-05 14:11:52 -0800 | [diff] [blame] | 1415 | sk_acceptq_removed(listener); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1416 | |
Stefan Hajnoczi | 4192f67 | 2016-06-23 16:28:58 +0100 | [diff] [blame] | 1417 | lock_sock_nested(connected, SINGLE_DEPTH_NESTING); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1418 | vconnected = vsock_sk(connected); |
| 1419 | |
| 1420 | /* If the listener socket has received an error, then we should |
| 1421 | * reject this socket and return. Note that we simply mark the |
| 1422 | * socket rejected, drop our reference, and let the cleanup |
| 1423 | * function handle the cleanup; the fact that we found it in |
| 1424 | * the listener's accept queue guarantees that the cleanup |
| 1425 | * function hasn't run yet. |
| 1426 | */ |
| 1427 | if (err) { |
| 1428 | vconnected->rejected = true; |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1429 | } else { |
| 1430 | newsock->state = SS_CONNECTED; |
| 1431 | sock_graft(connected, newsock); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1434 | release_sock(connected); |
| 1435 | sock_put(connected); |
| 1436 | } |
| 1437 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1438 | out: |
| 1439 | release_sock(listener); |
| 1440 | return err; |
| 1441 | } |
| 1442 | |
| 1443 | static int vsock_listen(struct socket *sock, int backlog) |
| 1444 | { |
| 1445 | int err; |
| 1446 | struct sock *sk; |
| 1447 | struct vsock_sock *vsk; |
| 1448 | |
| 1449 | sk = sock->sk; |
| 1450 | |
| 1451 | lock_sock(sk); |
| 1452 | |
| 1453 | if (sock->type != SOCK_STREAM) { |
| 1454 | err = -EOPNOTSUPP; |
| 1455 | goto out; |
| 1456 | } |
| 1457 | |
| 1458 | if (sock->state != SS_UNCONNECTED) { |
| 1459 | err = -EINVAL; |
| 1460 | goto out; |
| 1461 | } |
| 1462 | |
| 1463 | vsk = vsock_sk(sk); |
| 1464 | |
| 1465 | if (!vsock_addr_bound(&vsk->local_addr)) { |
| 1466 | err = -EINVAL; |
| 1467 | goto out; |
| 1468 | } |
| 1469 | |
| 1470 | sk->sk_max_ack_backlog = backlog; |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1471 | sk->sk_state = TCP_LISTEN; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1472 | |
| 1473 | err = 0; |
| 1474 | |
| 1475 | out: |
| 1476 | release_sock(sk); |
| 1477 | return err; |
| 1478 | } |
| 1479 | |
Stefano Garzarella | b9f2b0f | 2019-11-14 10:57:42 +0100 | [diff] [blame] | 1480 | static void vsock_update_buffer_size(struct vsock_sock *vsk, |
| 1481 | const struct vsock_transport *transport, |
| 1482 | u64 val) |
| 1483 | { |
| 1484 | if (val > vsk->buffer_max_size) |
| 1485 | val = vsk->buffer_max_size; |
| 1486 | |
| 1487 | if (val < vsk->buffer_min_size) |
| 1488 | val = vsk->buffer_min_size; |
| 1489 | |
| 1490 | if (val != vsk->buffer_size && |
| 1491 | transport && transport->notify_buffer_size) |
| 1492 | transport->notify_buffer_size(vsk, &val); |
| 1493 | |
| 1494 | vsk->buffer_size = val; |
| 1495 | } |
| 1496 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1497 | static int vsock_stream_setsockopt(struct socket *sock, |
| 1498 | int level, |
| 1499 | int optname, |
| 1500 | char __user *optval, |
| 1501 | unsigned int optlen) |
| 1502 | { |
| 1503 | int err; |
| 1504 | struct sock *sk; |
| 1505 | struct vsock_sock *vsk; |
Stefano Garzarella | fe502c4 | 2019-11-14 10:57:39 +0100 | [diff] [blame] | 1506 | const struct vsock_transport *transport; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1507 | u64 val; |
| 1508 | |
| 1509 | if (level != AF_VSOCK) |
| 1510 | return -ENOPROTOOPT; |
| 1511 | |
| 1512 | #define COPY_IN(_v) \ |
| 1513 | do { \ |
| 1514 | if (optlen < sizeof(_v)) { \ |
| 1515 | err = -EINVAL; \ |
| 1516 | goto exit; \ |
| 1517 | } \ |
| 1518 | if (copy_from_user(&_v, optval, sizeof(_v)) != 0) { \ |
| 1519 | err = -EFAULT; \ |
| 1520 | goto exit; \ |
| 1521 | } \ |
| 1522 | } while (0) |
| 1523 | |
| 1524 | err = 0; |
| 1525 | sk = sock->sk; |
| 1526 | vsk = vsock_sk(sk); |
Stefano Garzarella | fe502c4 | 2019-11-14 10:57:39 +0100 | [diff] [blame] | 1527 | transport = vsk->transport; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1528 | |
| 1529 | lock_sock(sk); |
| 1530 | |
| 1531 | switch (optname) { |
| 1532 | case SO_VM_SOCKETS_BUFFER_SIZE: |
| 1533 | COPY_IN(val); |
Stefano Garzarella | b9f2b0f | 2019-11-14 10:57:42 +0100 | [diff] [blame] | 1534 | vsock_update_buffer_size(vsk, transport, val); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1535 | break; |
| 1536 | |
| 1537 | case SO_VM_SOCKETS_BUFFER_MAX_SIZE: |
| 1538 | COPY_IN(val); |
Stefano Garzarella | b9f2b0f | 2019-11-14 10:57:42 +0100 | [diff] [blame] | 1539 | vsk->buffer_max_size = val; |
| 1540 | vsock_update_buffer_size(vsk, transport, vsk->buffer_size); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1541 | break; |
| 1542 | |
| 1543 | case SO_VM_SOCKETS_BUFFER_MIN_SIZE: |
| 1544 | COPY_IN(val); |
Stefano Garzarella | b9f2b0f | 2019-11-14 10:57:42 +0100 | [diff] [blame] | 1545 | vsk->buffer_min_size = val; |
| 1546 | vsock_update_buffer_size(vsk, transport, vsk->buffer_size); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1547 | break; |
| 1548 | |
| 1549 | case SO_VM_SOCKETS_CONNECT_TIMEOUT: { |
Arnd Bergmann | fe0c72f | 2019-02-02 07:34:44 -0800 | [diff] [blame] | 1550 | struct __kernel_old_timeval tv; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1551 | COPY_IN(tv); |
| 1552 | if (tv.tv_sec >= 0 && tv.tv_usec < USEC_PER_SEC && |
| 1553 | tv.tv_sec < (MAX_SCHEDULE_TIMEOUT / HZ - 1)) { |
| 1554 | vsk->connect_timeout = tv.tv_sec * HZ + |
| 1555 | DIV_ROUND_UP(tv.tv_usec, (1000000 / HZ)); |
| 1556 | if (vsk->connect_timeout == 0) |
| 1557 | vsk->connect_timeout = |
| 1558 | VSOCK_DEFAULT_CONNECT_TIMEOUT; |
| 1559 | |
| 1560 | } else { |
| 1561 | err = -ERANGE; |
| 1562 | } |
| 1563 | break; |
| 1564 | } |
| 1565 | |
| 1566 | default: |
| 1567 | err = -ENOPROTOOPT; |
| 1568 | break; |
| 1569 | } |
| 1570 | |
| 1571 | #undef COPY_IN |
| 1572 | |
| 1573 | exit: |
| 1574 | release_sock(sk); |
| 1575 | return err; |
| 1576 | } |
| 1577 | |
| 1578 | static int vsock_stream_getsockopt(struct socket *sock, |
| 1579 | int level, int optname, |
| 1580 | char __user *optval, |
| 1581 | int __user *optlen) |
| 1582 | { |
| 1583 | int err; |
| 1584 | int len; |
| 1585 | struct sock *sk; |
| 1586 | struct vsock_sock *vsk; |
| 1587 | u64 val; |
| 1588 | |
| 1589 | if (level != AF_VSOCK) |
| 1590 | return -ENOPROTOOPT; |
| 1591 | |
| 1592 | err = get_user(len, optlen); |
| 1593 | if (err != 0) |
| 1594 | return err; |
| 1595 | |
| 1596 | #define COPY_OUT(_v) \ |
| 1597 | do { \ |
| 1598 | if (len < sizeof(_v)) \ |
| 1599 | return -EINVAL; \ |
| 1600 | \ |
| 1601 | len = sizeof(_v); \ |
| 1602 | if (copy_to_user(optval, &_v, len) != 0) \ |
| 1603 | return -EFAULT; \ |
| 1604 | \ |
| 1605 | } while (0) |
| 1606 | |
| 1607 | err = 0; |
| 1608 | sk = sock->sk; |
| 1609 | vsk = vsock_sk(sk); |
| 1610 | |
| 1611 | switch (optname) { |
| 1612 | case SO_VM_SOCKETS_BUFFER_SIZE: |
Stefano Garzarella | b9f2b0f | 2019-11-14 10:57:42 +0100 | [diff] [blame] | 1613 | val = vsk->buffer_size; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1614 | COPY_OUT(val); |
| 1615 | break; |
| 1616 | |
| 1617 | case SO_VM_SOCKETS_BUFFER_MAX_SIZE: |
Stefano Garzarella | b9f2b0f | 2019-11-14 10:57:42 +0100 | [diff] [blame] | 1618 | val = vsk->buffer_max_size; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1619 | COPY_OUT(val); |
| 1620 | break; |
| 1621 | |
| 1622 | case SO_VM_SOCKETS_BUFFER_MIN_SIZE: |
Stefano Garzarella | b9f2b0f | 2019-11-14 10:57:42 +0100 | [diff] [blame] | 1623 | val = vsk->buffer_min_size; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1624 | COPY_OUT(val); |
| 1625 | break; |
| 1626 | |
| 1627 | case SO_VM_SOCKETS_CONNECT_TIMEOUT: { |
Arnd Bergmann | fe0c72f | 2019-02-02 07:34:44 -0800 | [diff] [blame] | 1628 | struct __kernel_old_timeval tv; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1629 | tv.tv_sec = vsk->connect_timeout / HZ; |
| 1630 | tv.tv_usec = |
| 1631 | (vsk->connect_timeout - |
| 1632 | tv.tv_sec * HZ) * (1000000 / HZ); |
| 1633 | COPY_OUT(tv); |
| 1634 | break; |
| 1635 | } |
| 1636 | default: |
| 1637 | return -ENOPROTOOPT; |
| 1638 | } |
| 1639 | |
| 1640 | err = put_user(len, optlen); |
| 1641 | if (err != 0) |
| 1642 | return -EFAULT; |
| 1643 | |
| 1644 | #undef COPY_OUT |
| 1645 | |
| 1646 | return 0; |
| 1647 | } |
| 1648 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1649 | static int vsock_stream_sendmsg(struct socket *sock, struct msghdr *msg, |
| 1650 | size_t len) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1651 | { |
| 1652 | struct sock *sk; |
| 1653 | struct vsock_sock *vsk; |
Stefano Garzarella | fe502c4 | 2019-11-14 10:57:39 +0100 | [diff] [blame] | 1654 | const struct vsock_transport *transport; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1655 | ssize_t total_written; |
| 1656 | long timeout; |
| 1657 | int err; |
| 1658 | struct vsock_transport_send_notify_data send_data; |
WANG Cong | 499fde6 | 2017-05-19 11:21:59 -0700 | [diff] [blame] | 1659 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1660 | |
| 1661 | sk = sock->sk; |
| 1662 | vsk = vsock_sk(sk); |
Stefano Garzarella | fe502c4 | 2019-11-14 10:57:39 +0100 | [diff] [blame] | 1663 | transport = vsk->transport; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1664 | total_written = 0; |
| 1665 | err = 0; |
| 1666 | |
| 1667 | if (msg->msg_flags & MSG_OOB) |
| 1668 | return -EOPNOTSUPP; |
| 1669 | |
| 1670 | lock_sock(sk); |
| 1671 | |
| 1672 | /* Callers should not provide a destination with stream sockets. */ |
| 1673 | if (msg->msg_namelen) { |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1674 | err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1675 | goto out; |
| 1676 | } |
| 1677 | |
| 1678 | /* Send data only if both sides are not shutdown in the direction. */ |
| 1679 | if (sk->sk_shutdown & SEND_SHUTDOWN || |
| 1680 | vsk->peer_shutdown & RCV_SHUTDOWN) { |
| 1681 | err = -EPIPE; |
| 1682 | goto out; |
| 1683 | } |
| 1684 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 1685 | if (!transport || sk->sk_state != TCP_ESTABLISHED || |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1686 | !vsock_addr_bound(&vsk->local_addr)) { |
| 1687 | err = -ENOTCONN; |
| 1688 | goto out; |
| 1689 | } |
| 1690 | |
| 1691 | if (!vsock_addr_bound(&vsk->remote_addr)) { |
| 1692 | err = -EDESTADDRREQ; |
| 1693 | goto out; |
| 1694 | } |
| 1695 | |
| 1696 | /* Wait for room in the produce queue to enqueue our user's data. */ |
| 1697 | timeout = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
| 1698 | |
| 1699 | err = transport->notify_send_init(vsk, &send_data); |
| 1700 | if (err < 0) |
| 1701 | goto out; |
| 1702 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1703 | while (total_written < len) { |
| 1704 | ssize_t written; |
| 1705 | |
WANG Cong | 499fde6 | 2017-05-19 11:21:59 -0700 | [diff] [blame] | 1706 | add_wait_queue(sk_sleep(sk), &wait); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1707 | while (vsock_stream_has_space(vsk) == 0 && |
| 1708 | sk->sk_err == 0 && |
| 1709 | !(sk->sk_shutdown & SEND_SHUTDOWN) && |
| 1710 | !(vsk->peer_shutdown & RCV_SHUTDOWN)) { |
| 1711 | |
| 1712 | /* Don't wait for non-blocking sockets. */ |
| 1713 | if (timeout == 0) { |
| 1714 | err = -EAGAIN; |
WANG Cong | 499fde6 | 2017-05-19 11:21:59 -0700 | [diff] [blame] | 1715 | remove_wait_queue(sk_sleep(sk), &wait); |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1716 | goto out_err; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | err = transport->notify_send_pre_block(vsk, &send_data); |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1720 | if (err < 0) { |
WANG Cong | 499fde6 | 2017-05-19 11:21:59 -0700 | [diff] [blame] | 1721 | remove_wait_queue(sk_sleep(sk), &wait); |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1722 | goto out_err; |
| 1723 | } |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1724 | |
| 1725 | release_sock(sk); |
WANG Cong | 499fde6 | 2017-05-19 11:21:59 -0700 | [diff] [blame] | 1726 | timeout = wait_woken(&wait, TASK_INTERRUPTIBLE, timeout); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1727 | lock_sock(sk); |
| 1728 | if (signal_pending(current)) { |
| 1729 | err = sock_intr_errno(timeout); |
WANG Cong | 499fde6 | 2017-05-19 11:21:59 -0700 | [diff] [blame] | 1730 | remove_wait_queue(sk_sleep(sk), &wait); |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1731 | goto out_err; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1732 | } else if (timeout == 0) { |
| 1733 | err = -EAGAIN; |
WANG Cong | 499fde6 | 2017-05-19 11:21:59 -0700 | [diff] [blame] | 1734 | remove_wait_queue(sk_sleep(sk), &wait); |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1735 | goto out_err; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1736 | } |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1737 | } |
WANG Cong | 499fde6 | 2017-05-19 11:21:59 -0700 | [diff] [blame] | 1738 | remove_wait_queue(sk_sleep(sk), &wait); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1739 | |
| 1740 | /* These checks occur both as part of and after the loop |
| 1741 | * conditional since we need to check before and after |
| 1742 | * sleeping. |
| 1743 | */ |
| 1744 | if (sk->sk_err) { |
| 1745 | err = -sk->sk_err; |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1746 | goto out_err; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1747 | } else if ((sk->sk_shutdown & SEND_SHUTDOWN) || |
| 1748 | (vsk->peer_shutdown & RCV_SHUTDOWN)) { |
| 1749 | err = -EPIPE; |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1750 | goto out_err; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | err = transport->notify_send_pre_enqueue(vsk, &send_data); |
| 1754 | if (err < 0) |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1755 | goto out_err; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1756 | |
| 1757 | /* Note that enqueue will only write as many bytes as are free |
| 1758 | * in the produce queue, so we don't need to ensure len is |
| 1759 | * smaller than the queue size. It is the caller's |
| 1760 | * responsibility to check how many bytes we were able to send. |
| 1761 | */ |
| 1762 | |
| 1763 | written = transport->stream_enqueue( |
Al Viro | 0f7db23 | 2014-11-20 04:05:34 -0500 | [diff] [blame] | 1764 | vsk, msg, |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1765 | len - total_written); |
| 1766 | if (written < 0) { |
| 1767 | err = -ENOMEM; |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1768 | goto out_err; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | total_written += written; |
| 1772 | |
| 1773 | err = transport->notify_send_post_enqueue( |
| 1774 | vsk, written, &send_data); |
| 1775 | if (err < 0) |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1776 | goto out_err; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1777 | |
| 1778 | } |
| 1779 | |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1780 | out_err: |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1781 | if (total_written > 0) |
| 1782 | err = total_written; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1783 | out: |
| 1784 | release_sock(sk); |
| 1785 | return err; |
| 1786 | } |
| 1787 | |
| 1788 | |
| 1789 | static int |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1790 | vsock_stream_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, |
| 1791 | int flags) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1792 | { |
| 1793 | struct sock *sk; |
| 1794 | struct vsock_sock *vsk; |
Stefano Garzarella | fe502c4 | 2019-11-14 10:57:39 +0100 | [diff] [blame] | 1795 | const struct vsock_transport *transport; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1796 | int err; |
| 1797 | size_t target; |
| 1798 | ssize_t copied; |
| 1799 | long timeout; |
| 1800 | struct vsock_transport_recv_notify_data recv_data; |
| 1801 | |
| 1802 | DEFINE_WAIT(wait); |
| 1803 | |
| 1804 | sk = sock->sk; |
| 1805 | vsk = vsock_sk(sk); |
Stefano Garzarella | fe502c4 | 2019-11-14 10:57:39 +0100 | [diff] [blame] | 1806 | transport = vsk->transport; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1807 | err = 0; |
| 1808 | |
| 1809 | lock_sock(sk); |
| 1810 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 1811 | if (!transport || sk->sk_state != TCP_ESTABLISHED) { |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1812 | /* Recvmsg is supposed to return 0 if a peer performs an |
| 1813 | * orderly shutdown. Differentiate between that case and when a |
| 1814 | * peer has not connected or a local shutdown occured with the |
| 1815 | * SOCK_DONE flag. |
| 1816 | */ |
| 1817 | if (sock_flag(sk, SOCK_DONE)) |
| 1818 | err = 0; |
| 1819 | else |
| 1820 | err = -ENOTCONN; |
| 1821 | |
| 1822 | goto out; |
| 1823 | } |
| 1824 | |
| 1825 | if (flags & MSG_OOB) { |
| 1826 | err = -EOPNOTSUPP; |
| 1827 | goto out; |
| 1828 | } |
| 1829 | |
| 1830 | /* We don't check peer_shutdown flag here since peer may actually shut |
| 1831 | * down, but there can be data in the queue that a local socket can |
| 1832 | * receive. |
| 1833 | */ |
| 1834 | if (sk->sk_shutdown & RCV_SHUTDOWN) { |
| 1835 | err = 0; |
| 1836 | goto out; |
| 1837 | } |
| 1838 | |
| 1839 | /* It is valid on Linux to pass in a zero-length receive buffer. This |
| 1840 | * is not an error. We may as well bail out now. |
| 1841 | */ |
| 1842 | if (!len) { |
| 1843 | err = 0; |
| 1844 | goto out; |
| 1845 | } |
| 1846 | |
| 1847 | /* We must not copy less than target bytes into the user's buffer |
| 1848 | * before returning successfully, so we wait for the consume queue to |
| 1849 | * have that much data to consume before dequeueing. Note that this |
| 1850 | * makes it impossible to handle cases where target is greater than the |
| 1851 | * queue size. |
| 1852 | */ |
| 1853 | target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); |
| 1854 | if (target >= transport->stream_rcvhiwat(vsk)) { |
| 1855 | err = -ENOMEM; |
| 1856 | goto out; |
| 1857 | } |
| 1858 | timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
| 1859 | copied = 0; |
| 1860 | |
| 1861 | err = transport->notify_recv_init(vsk, target, &recv_data); |
| 1862 | if (err < 0) |
| 1863 | goto out; |
| 1864 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1865 | |
| 1866 | while (1) { |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1867 | s64 ready; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1868 | |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1869 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
| 1870 | ready = vsock_stream_has_data(vsk); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1871 | |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1872 | if (ready == 0) { |
| 1873 | if (sk->sk_err != 0 || |
| 1874 | (sk->sk_shutdown & RCV_SHUTDOWN) || |
| 1875 | (vsk->peer_shutdown & SEND_SHUTDOWN)) { |
| 1876 | finish_wait(sk_sleep(sk), &wait); |
| 1877 | break; |
| 1878 | } |
| 1879 | /* Don't wait for non-blocking sockets. */ |
| 1880 | if (timeout == 0) { |
| 1881 | err = -EAGAIN; |
| 1882 | finish_wait(sk_sleep(sk), &wait); |
| 1883 | break; |
| 1884 | } |
| 1885 | |
| 1886 | err = transport->notify_recv_pre_block( |
| 1887 | vsk, target, &recv_data); |
| 1888 | if (err < 0) { |
| 1889 | finish_wait(sk_sleep(sk), &wait); |
| 1890 | break; |
| 1891 | } |
| 1892 | release_sock(sk); |
| 1893 | timeout = schedule_timeout(timeout); |
| 1894 | lock_sock(sk); |
| 1895 | |
| 1896 | if (signal_pending(current)) { |
| 1897 | err = sock_intr_errno(timeout); |
| 1898 | finish_wait(sk_sleep(sk), &wait); |
| 1899 | break; |
| 1900 | } else if (timeout == 0) { |
| 1901 | err = -EAGAIN; |
| 1902 | finish_wait(sk_sleep(sk), &wait); |
| 1903 | break; |
| 1904 | } |
| 1905 | } else { |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1906 | ssize_t read; |
| 1907 | |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1908 | finish_wait(sk_sleep(sk), &wait); |
| 1909 | |
| 1910 | if (ready < 0) { |
| 1911 | /* Invalid queue pair content. XXX This should |
| 1912 | * be changed to a connection reset in a later |
| 1913 | * change. |
| 1914 | */ |
| 1915 | |
| 1916 | err = -ENOMEM; |
| 1917 | goto out; |
| 1918 | } |
| 1919 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1920 | err = transport->notify_recv_pre_dequeue( |
| 1921 | vsk, target, &recv_data); |
| 1922 | if (err < 0) |
| 1923 | break; |
| 1924 | |
| 1925 | read = transport->stream_dequeue( |
Al Viro | 0f7db23 | 2014-11-20 04:05:34 -0500 | [diff] [blame] | 1926 | vsk, msg, |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1927 | len - copied, flags); |
| 1928 | if (read < 0) { |
| 1929 | err = -ENOMEM; |
| 1930 | break; |
| 1931 | } |
| 1932 | |
| 1933 | copied += read; |
| 1934 | |
| 1935 | err = transport->notify_recv_post_dequeue( |
| 1936 | vsk, target, read, |
| 1937 | !(flags & MSG_PEEK), &recv_data); |
| 1938 | if (err < 0) |
Claudio Imbrenda | f7f9b5e | 2016-03-22 17:05:52 +0100 | [diff] [blame] | 1939 | goto out; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1940 | |
| 1941 | if (read >= target || flags & MSG_PEEK) |
| 1942 | break; |
| 1943 | |
| 1944 | target -= read; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | if (sk->sk_err) |
| 1949 | err = -sk->sk_err; |
| 1950 | else if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 1951 | err = 0; |
| 1952 | |
Ian Campbell | dedc58e | 2016-05-04 14:21:53 +0100 | [diff] [blame] | 1953 | if (copied > 0) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1954 | err = copied; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1955 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1956 | out: |
| 1957 | release_sock(sk); |
| 1958 | return err; |
| 1959 | } |
| 1960 | |
| 1961 | static const struct proto_ops vsock_stream_ops = { |
| 1962 | .family = PF_VSOCK, |
| 1963 | .owner = THIS_MODULE, |
| 1964 | .release = vsock_release, |
| 1965 | .bind = vsock_bind, |
| 1966 | .connect = vsock_stream_connect, |
| 1967 | .socketpair = sock_no_socketpair, |
| 1968 | .accept = vsock_accept, |
| 1969 | .getname = vsock_getname, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1970 | .poll = vsock_poll, |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1971 | .ioctl = sock_no_ioctl, |
| 1972 | .listen = vsock_listen, |
| 1973 | .shutdown = vsock_shutdown, |
| 1974 | .setsockopt = vsock_stream_setsockopt, |
| 1975 | .getsockopt = vsock_stream_getsockopt, |
| 1976 | .sendmsg = vsock_stream_sendmsg, |
| 1977 | .recvmsg = vsock_stream_recvmsg, |
| 1978 | .mmap = sock_no_mmap, |
| 1979 | .sendpage = sock_no_sendpage, |
| 1980 | }; |
| 1981 | |
| 1982 | static int vsock_create(struct net *net, struct socket *sock, |
| 1983 | int protocol, int kern) |
| 1984 | { |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 1985 | struct vsock_sock *vsk; |
Stefano Garzarella | 55f3e14 | 2019-11-14 10:57:44 +0100 | [diff] [blame] | 1986 | struct sock *sk; |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 1987 | int ret; |
Stefano Garzarella | 55f3e14 | 2019-11-14 10:57:44 +0100 | [diff] [blame] | 1988 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1989 | if (!sock) |
| 1990 | return -EINVAL; |
| 1991 | |
Andy King | 6cf1c5f | 2013-02-18 06:04:13 +0000 | [diff] [blame] | 1992 | if (protocol && protocol != PF_VSOCK) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1993 | return -EPROTONOSUPPORT; |
| 1994 | |
| 1995 | switch (sock->type) { |
| 1996 | case SOCK_DGRAM: |
| 1997 | sock->ops = &vsock_dgram_ops; |
| 1998 | break; |
| 1999 | case SOCK_STREAM: |
| 2000 | sock->ops = &vsock_stream_ops; |
| 2001 | break; |
| 2002 | default: |
| 2003 | return -ESOCKTNOSUPPORT; |
| 2004 | } |
| 2005 | |
| 2006 | sock->state = SS_UNCONNECTED; |
| 2007 | |
Stefano Garzarella | 55f3e14 | 2019-11-14 10:57:44 +0100 | [diff] [blame] | 2008 | sk = __vsock_create(net, sock, NULL, GFP_KERNEL, 0, kern); |
| 2009 | if (!sk) |
| 2010 | return -ENOMEM; |
| 2011 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 2012 | vsk = vsock_sk(sk); |
| 2013 | |
| 2014 | if (sock->type == SOCK_DGRAM) { |
| 2015 | ret = vsock_assign_transport(vsk, NULL); |
| 2016 | if (ret < 0) { |
| 2017 | sock_put(sk); |
| 2018 | return ret; |
| 2019 | } |
| 2020 | } |
| 2021 | |
| 2022 | vsock_insert_unbound(vsk); |
Stefano Garzarella | 55f3e14 | 2019-11-14 10:57:44 +0100 | [diff] [blame] | 2023 | |
| 2024 | return 0; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | static const struct net_proto_family vsock_family_ops = { |
| 2028 | .family = AF_VSOCK, |
| 2029 | .create = vsock_create, |
| 2030 | .owner = THIS_MODULE, |
| 2031 | }; |
| 2032 | |
| 2033 | static long vsock_dev_do_ioctl(struct file *filp, |
| 2034 | unsigned int cmd, void __user *ptr) |
| 2035 | { |
| 2036 | u32 __user *p = ptr; |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 2037 | u32 cid = VMADDR_CID_ANY; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2038 | int retval = 0; |
| 2039 | |
| 2040 | switch (cmd) { |
| 2041 | case IOCTL_VM_SOCKETS_GET_LOCAL_CID: |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 2042 | /* To be compatible with the VMCI behavior, we prioritize the |
| 2043 | * guest CID instead of well-know host CID (VMADDR_CID_HOST). |
| 2044 | */ |
| 2045 | if (transport_g2h) |
| 2046 | cid = transport_g2h->get_local_cid(); |
| 2047 | else if (transport_h2g) |
| 2048 | cid = transport_h2g->get_local_cid(); |
| 2049 | |
| 2050 | if (put_user(cid, p) != 0) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2051 | retval = -EFAULT; |
| 2052 | break; |
| 2053 | |
| 2054 | default: |
| 2055 | pr_err("Unknown ioctl %d\n", cmd); |
| 2056 | retval = -EINVAL; |
| 2057 | } |
| 2058 | |
| 2059 | return retval; |
| 2060 | } |
| 2061 | |
| 2062 | static long vsock_dev_ioctl(struct file *filp, |
| 2063 | unsigned int cmd, unsigned long arg) |
| 2064 | { |
| 2065 | return vsock_dev_do_ioctl(filp, cmd, (void __user *)arg); |
| 2066 | } |
| 2067 | |
| 2068 | #ifdef CONFIG_COMPAT |
| 2069 | static long vsock_dev_compat_ioctl(struct file *filp, |
| 2070 | unsigned int cmd, unsigned long arg) |
| 2071 | { |
| 2072 | return vsock_dev_do_ioctl(filp, cmd, compat_ptr(arg)); |
| 2073 | } |
| 2074 | #endif |
| 2075 | |
| 2076 | static const struct file_operations vsock_device_ops = { |
| 2077 | .owner = THIS_MODULE, |
| 2078 | .unlocked_ioctl = vsock_dev_ioctl, |
| 2079 | #ifdef CONFIG_COMPAT |
| 2080 | .compat_ioctl = vsock_dev_compat_ioctl, |
| 2081 | #endif |
| 2082 | .open = nonseekable_open, |
| 2083 | }; |
| 2084 | |
| 2085 | static struct miscdevice vsock_device = { |
| 2086 | .name = "vsock", |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2087 | .fops = &vsock_device_ops, |
| 2088 | }; |
| 2089 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 2090 | static int __init vsock_init(void) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2091 | { |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 2092 | int err = 0; |
Andy King | 2c4a336 | 2014-05-01 15:20:43 -0700 | [diff] [blame] | 2093 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 2094 | vsock_init_tables(); |
Andy King | 2c4a336 | 2014-05-01 15:20:43 -0700 | [diff] [blame] | 2095 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 2096 | vsock_proto.owner = THIS_MODULE; |
Asias He | 6ad0b2f | 2013-04-23 20:33:52 +0000 | [diff] [blame] | 2097 | vsock_device.minor = MISC_DYNAMIC_MINOR; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2098 | err = misc_register(&vsock_device); |
| 2099 | if (err) { |
| 2100 | pr_err("Failed to register misc device\n"); |
Gao feng | f6a835b | 2015-10-18 23:35:56 +0800 | [diff] [blame] | 2101 | goto err_reset_transport; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | err = proto_register(&vsock_proto, 1); /* we want our slab */ |
| 2105 | if (err) { |
| 2106 | pr_err("Cannot register vsock protocol\n"); |
Gao feng | f6a835b | 2015-10-18 23:35:56 +0800 | [diff] [blame] | 2107 | goto err_deregister_misc; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | err = sock_register(&vsock_family_ops); |
| 2111 | if (err) { |
| 2112 | pr_err("could not register af_vsock (%d) address family: %d\n", |
| 2113 | AF_VSOCK, err); |
| 2114 | goto err_unregister_proto; |
| 2115 | } |
| 2116 | |
| 2117 | return 0; |
| 2118 | |
| 2119 | err_unregister_proto: |
| 2120 | proto_unregister(&vsock_proto); |
Gao feng | f6a835b | 2015-10-18 23:35:56 +0800 | [diff] [blame] | 2121 | err_deregister_misc: |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2122 | misc_deregister(&vsock_device); |
Gao feng | f6a835b | 2015-10-18 23:35:56 +0800 | [diff] [blame] | 2123 | err_reset_transport: |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2124 | return err; |
| 2125 | } |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2126 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 2127 | static void __exit vsock_exit(void) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2128 | { |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2129 | misc_deregister(&vsock_device); |
| 2130 | sock_unregister(AF_VSOCK); |
| 2131 | proto_unregister(&vsock_proto); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2132 | } |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2133 | |
Stefano Garzarella | daabfbc | 2019-11-14 10:57:41 +0100 | [diff] [blame] | 2134 | const struct vsock_transport *vsock_core_get_transport(struct vsock_sock *vsk) |
Stefan Hajnoczi | 0b01aeb | 2016-07-28 15:36:30 +0100 | [diff] [blame] | 2135 | { |
Stefano Garzarella | daabfbc | 2019-11-14 10:57:41 +0100 | [diff] [blame] | 2136 | return vsk->transport; |
Stefan Hajnoczi | 0b01aeb | 2016-07-28 15:36:30 +0100 | [diff] [blame] | 2137 | } |
| 2138 | EXPORT_SYMBOL_GPL(vsock_core_get_transport); |
| 2139 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 2140 | int vsock_core_register(const struct vsock_transport *t, int features) |
Stefan Hajnoczi | 05e489b | 2018-04-17 14:25:58 +0800 | [diff] [blame] | 2141 | { |
Stefano Garzarella | 0e12190 | 2019-12-10 11:43:04 +0100 | [diff] [blame] | 2142 | const struct vsock_transport *t_h2g, *t_g2h, *t_dgram, *t_local; |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 2143 | int err = mutex_lock_interruptible(&vsock_register_mutex); |
Stefan Hajnoczi | 05e489b | 2018-04-17 14:25:58 +0800 | [diff] [blame] | 2144 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 2145 | if (err) |
| 2146 | return err; |
| 2147 | |
| 2148 | t_h2g = transport_h2g; |
| 2149 | t_g2h = transport_g2h; |
| 2150 | t_dgram = transport_dgram; |
Stefano Garzarella | 0e12190 | 2019-12-10 11:43:04 +0100 | [diff] [blame] | 2151 | t_local = transport_local; |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 2152 | |
| 2153 | if (features & VSOCK_TRANSPORT_F_H2G) { |
| 2154 | if (t_h2g) { |
| 2155 | err = -EBUSY; |
| 2156 | goto err_busy; |
| 2157 | } |
| 2158 | t_h2g = t; |
| 2159 | } |
| 2160 | |
| 2161 | if (features & VSOCK_TRANSPORT_F_G2H) { |
| 2162 | if (t_g2h) { |
| 2163 | err = -EBUSY; |
| 2164 | goto err_busy; |
| 2165 | } |
| 2166 | t_g2h = t; |
| 2167 | } |
| 2168 | |
| 2169 | if (features & VSOCK_TRANSPORT_F_DGRAM) { |
| 2170 | if (t_dgram) { |
| 2171 | err = -EBUSY; |
| 2172 | goto err_busy; |
| 2173 | } |
| 2174 | t_dgram = t; |
| 2175 | } |
| 2176 | |
Stefano Garzarella | 0e12190 | 2019-12-10 11:43:04 +0100 | [diff] [blame] | 2177 | if (features & VSOCK_TRANSPORT_F_LOCAL) { |
| 2178 | if (t_local) { |
| 2179 | err = -EBUSY; |
| 2180 | goto err_busy; |
| 2181 | } |
| 2182 | t_local = t; |
| 2183 | } |
| 2184 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 2185 | transport_h2g = t_h2g; |
| 2186 | transport_g2h = t_g2h; |
| 2187 | transport_dgram = t_dgram; |
Stefano Garzarella | 0e12190 | 2019-12-10 11:43:04 +0100 | [diff] [blame] | 2188 | transport_local = t_local; |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 2189 | |
| 2190 | err_busy: |
| 2191 | mutex_unlock(&vsock_register_mutex); |
| 2192 | return err; |
| 2193 | } |
| 2194 | EXPORT_SYMBOL_GPL(vsock_core_register); |
| 2195 | |
| 2196 | void vsock_core_unregister(const struct vsock_transport *t) |
| 2197 | { |
| 2198 | mutex_lock(&vsock_register_mutex); |
| 2199 | |
| 2200 | if (transport_h2g == t) |
| 2201 | transport_h2g = NULL; |
| 2202 | |
| 2203 | if (transport_g2h == t) |
| 2204 | transport_g2h = NULL; |
| 2205 | |
| 2206 | if (transport_dgram == t) |
| 2207 | transport_dgram = NULL; |
| 2208 | |
Stefano Garzarella | 0e12190 | 2019-12-10 11:43:04 +0100 | [diff] [blame] | 2209 | if (transport_local == t) |
| 2210 | transport_local = NULL; |
| 2211 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 2212 | mutex_unlock(&vsock_register_mutex); |
| 2213 | } |
| 2214 | EXPORT_SYMBOL_GPL(vsock_core_unregister); |
| 2215 | |
| 2216 | module_init(vsock_init); |
Stefan Hajnoczi | 05e489b | 2018-04-17 14:25:58 +0800 | [diff] [blame] | 2217 | module_exit(vsock_exit); |
Cong Wang | c1eef22 | 2017-10-24 15:30:37 -0700 | [diff] [blame] | 2218 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2219 | MODULE_AUTHOR("VMware, Inc."); |
| 2220 | MODULE_DESCRIPTION("VMware Virtual Socket Family"); |
Jorgen Hansen | 1190cfd | 2016-09-26 23:59:53 -0700 | [diff] [blame] | 2221 | MODULE_VERSION("1.0.2.0-k"); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2222 | MODULE_LICENSE("GPL v2"); |