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