]>
Commit | Line | Data |
---|---|---|
685a6bf8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
d021c344 AK |
2 | /* |
3 | * VMware vSockets Driver | |
4 | * | |
5 | * Copyright (C) 2007-2013 VMware, Inc. All rights reserved. | |
d021c344 AK |
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 | |
3b4477d2 | 31 | * implementation because they are in the TCP_LISTEN state. When a |
ea3803c1 SH |
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. | |
d021c344 AK |
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 | * | |
4192f672 SH |
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 | * | |
d021c344 AK |
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. | |
3b4477d2 SH |
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 | |
d021c344 AK |
86 | */ |
87 | ||
88 | #include <linux/types.h> | |
d021c344 AK |
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> | |
174cd4b1 | 94 | #include <linux/sched/signal.h> |
d021c344 AK |
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> | |
8236b08c | 102 | #include <linux/random.h> |
d021c344 AK |
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> | |
82a54d0e | 111 | #include <net/af_vsock.h> |
d021c344 AK |
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 | ||
b9f2b0ff SG |
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 | ||
c0cfa2d8 SG |
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; | |
0e121905 SG |
139 | /* Transport used for local communication */ |
140 | static const struct vsock_transport *transport_local; | |
d021c344 AK |
141 | static DEFINE_MUTEX(vsock_register_mutex); |
142 | ||
d021c344 AK |
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 | |
a49dd9dc | 157 | * mods with VSOCK_HASH_SIZE to ensure this. |
d021c344 | 158 | */ |
d021c344 AK |
159 | #define MAX_PORT_RETRIES 24 |
160 | ||
a49dd9dc | 161 | #define VSOCK_HASH(addr) ((addr)->svm_port % VSOCK_HASH_SIZE) |
d021c344 AK |
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) \ | |
a49dd9dc | 167 | (((src)->svm_cid ^ (dst)->svm_port) % VSOCK_HASH_SIZE) |
d021c344 AK |
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 | ||
44f20980 SH |
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); | |
d021c344 | 179 | |
b3a6dfe8 AH |
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 | ||
c0cfa2d8 | 192 | static void vsock_init_tables(void) |
d021c344 AK |
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 | ||
36c5b48b SG |
233 | list_for_each_entry(vsk, vsock_bound_sockets(addr), bound_table) { |
234 | if (vsock_addr_equals_addr(addr, &vsk->local_addr)) | |
d021c344 AK |
235 | return sk_vsock(vsk); |
236 | ||
36c5b48b SG |
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 | ||
d021c344 AK |
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) { | |
990454b5 RG |
253 | if (vsock_addr_equals_addr(src, &vsk->remote_addr) && |
254 | dst->svm_port == vsk->local_addr.svm_port) { | |
d021c344 AK |
255 | return sk_vsock(vsk); |
256 | } | |
257 | } | |
258 | ||
259 | return NULL; | |
260 | } | |
261 | ||
d021c344 AK |
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); | |
d5afa82c SM |
283 | if (__vsock_in_bound_table(vsk)) |
284 | __vsock_remove_bound(vsk); | |
d021c344 AK |
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); | |
d5afa82c SM |
292 | if (__vsock_in_connected_table(vsk)) |
293 | __vsock_remove_connected(vsk); | |
d021c344 AK |
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 | ||
6773b7dc SH |
329 | void vsock_remove_sock(struct vsock_sock *vsk) |
330 | { | |
d5afa82c SM |
331 | vsock_remove_bound(vsk); |
332 | vsock_remove_connected(vsk); | |
6773b7dc SH |
333 | } |
334 | EXPORT_SYMBOL_GPL(vsock_remove_sock); | |
335 | ||
ab768bde JP |
336 | void vsock_for_each_connected_socket(struct vsock_transport *transport, |
337 | void (*fn)(struct sock *sk)) | |
d021c344 AK |
338 | { |
339 | int i; | |
340 | ||
341 | spin_lock_bh(&vsock_table_lock); | |
342 | ||
343 | for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++) { | |
344 | struct vsock_sock *vsk; | |
345 | list_for_each_entry(vsk, &vsock_connected_table[i], | |
ab768bde JP |
346 | connected_table) { |
347 | if (vsk->transport != transport) | |
348 | continue; | |
349 | ||
d021c344 | 350 | fn(sk_vsock(vsk)); |
ab768bde | 351 | } |
d021c344 AK |
352 | } |
353 | ||
354 | spin_unlock_bh(&vsock_table_lock); | |
355 | } | |
356 | EXPORT_SYMBOL_GPL(vsock_for_each_connected_socket); | |
357 | ||
358 | void vsock_add_pending(struct sock *listener, struct sock *pending) | |
359 | { | |
360 | struct vsock_sock *vlistener; | |
361 | struct vsock_sock *vpending; | |
362 | ||
363 | vlistener = vsock_sk(listener); | |
364 | vpending = vsock_sk(pending); | |
365 | ||
366 | sock_hold(pending); | |
367 | sock_hold(listener); | |
368 | list_add_tail(&vpending->pending_links, &vlistener->pending_links); | |
369 | } | |
370 | EXPORT_SYMBOL_GPL(vsock_add_pending); | |
371 | ||
372 | void vsock_remove_pending(struct sock *listener, struct sock *pending) | |
373 | { | |
374 | struct vsock_sock *vpending = vsock_sk(pending); | |
375 | ||
376 | list_del_init(&vpending->pending_links); | |
377 | sock_put(listener); | |
378 | sock_put(pending); | |
379 | } | |
380 | EXPORT_SYMBOL_GPL(vsock_remove_pending); | |
381 | ||
382 | void vsock_enqueue_accept(struct sock *listener, struct sock *connected) | |
383 | { | |
384 | struct vsock_sock *vlistener; | |
385 | struct vsock_sock *vconnected; | |
386 | ||
387 | vlistener = vsock_sk(listener); | |
388 | vconnected = vsock_sk(connected); | |
389 | ||
390 | sock_hold(connected); | |
391 | sock_hold(listener); | |
392 | list_add_tail(&vconnected->accept_queue, &vlistener->accept_queue); | |
393 | } | |
394 | EXPORT_SYMBOL_GPL(vsock_enqueue_accept); | |
395 | ||
408624af SG |
396 | static bool vsock_use_local_transport(unsigned int remote_cid) |
397 | { | |
398 | if (!transport_local) | |
399 | return false; | |
400 | ||
401 | if (remote_cid == VMADDR_CID_LOCAL) | |
402 | return true; | |
403 | ||
404 | if (transport_g2h) { | |
405 | return remote_cid == transport_g2h->get_local_cid(); | |
406 | } else { | |
407 | return remote_cid == VMADDR_CID_HOST; | |
408 | } | |
409 | } | |
410 | ||
6a2c0962 SG |
411 | static void vsock_deassign_transport(struct vsock_sock *vsk) |
412 | { | |
413 | if (!vsk->transport) | |
414 | return; | |
415 | ||
416 | vsk->transport->destruct(vsk); | |
417 | module_put(vsk->transport->module); | |
418 | vsk->transport = NULL; | |
419 | } | |
420 | ||
c0cfa2d8 SG |
421 | /* Assign a transport to a socket and call the .init transport callback. |
422 | * | |
8cb48554 AK |
423 | * Note: for connection oriented socket this must be called when vsk->remote_addr |
424 | * is set (e.g. during the connect() or when a connection request on a listener | |
c0cfa2d8 SG |
425 | * socket is received). |
426 | * The vsk->remote_addr is used to decide which transport to use: | |
408624af SG |
427 | * - remote CID == VMADDR_CID_LOCAL or g2h->local_cid or VMADDR_CID_HOST if |
428 | * g2h is not loaded, will use local transport; | |
7f816984 AP |
429 | * - remote CID <= VMADDR_CID_HOST or h2g is not loaded or remote flags field |
430 | * includes VMADDR_FLAG_TO_HOST flag value, will use guest->host transport; | |
c0cfa2d8 SG |
431 | * - remote CID > VMADDR_CID_HOST will use host->guest transport; |
432 | */ | |
433 | int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk) | |
434 | { | |
435 | const struct vsock_transport *new_transport; | |
436 | struct sock *sk = sk_vsock(vsk); | |
437 | unsigned int remote_cid = vsk->remote_addr.svm_cid; | |
7f816984 | 438 | __u8 remote_flags; |
039fccca | 439 | int ret; |
c0cfa2d8 | 440 | |
1b5f2ab9 AP |
441 | /* If the packet is coming with the source and destination CIDs higher |
442 | * than VMADDR_CID_HOST, then a vsock channel where all the packets are | |
443 | * forwarded to the host should be established. Then the host will | |
444 | * need to forward the packets to the guest. | |
445 | * | |
446 | * The flag is set on the (listen) receive path (psk is not NULL). On | |
447 | * the connect path the flag can be set by the user space application. | |
448 | */ | |
449 | if (psk && vsk->local_addr.svm_cid > VMADDR_CID_HOST && | |
450 | vsk->remote_addr.svm_cid > VMADDR_CID_HOST) | |
451 | vsk->remote_addr.svm_flags |= VMADDR_FLAG_TO_HOST; | |
452 | ||
7f816984 AP |
453 | remote_flags = vsk->remote_addr.svm_flags; |
454 | ||
c0cfa2d8 SG |
455 | switch (sk->sk_type) { |
456 | case SOCK_DGRAM: | |
457 | new_transport = transport_dgram; | |
458 | break; | |
459 | case SOCK_STREAM: | |
0798e78b | 460 | case SOCK_SEQPACKET: |
408624af SG |
461 | if (vsock_use_local_transport(remote_cid)) |
462 | new_transport = transport_local; | |
7f816984 AP |
463 | else if (remote_cid <= VMADDR_CID_HOST || !transport_h2g || |
464 | (remote_flags & VMADDR_FLAG_TO_HOST)) | |
c0cfa2d8 SG |
465 | new_transport = transport_g2h; |
466 | else | |
467 | new_transport = transport_h2g; | |
468 | break; | |
469 | default: | |
470 | return -ESOCKTNOSUPPORT; | |
471 | } | |
472 | ||
473 | if (vsk->transport) { | |
474 | if (vsk->transport == new_transport) | |
475 | return 0; | |
476 | ||
3f74957f | 477 | /* transport->release() must be called with sock lock acquired. |
8cb48554 AK |
478 | * This path can only be taken during vsock_connect(), where we |
479 | * have already held the sock lock. In the other cases, this | |
480 | * function is called on a new socket which is not assigned to | |
481 | * any transport. | |
3f74957f | 482 | */ |
c0cfa2d8 | 483 | vsk->transport->release(vsk); |
6a2c0962 | 484 | vsock_deassign_transport(vsk); |
c0cfa2d8 SG |
485 | } |
486 | ||
6a2c0962 SG |
487 | /* We increase the module refcnt to prevent the transport unloading |
488 | * while there are open sockets assigned to it. | |
489 | */ | |
490 | if (!new_transport || !try_module_get(new_transport->module)) | |
c0cfa2d8 SG |
491 | return -ENODEV; |
492 | ||
0798e78b AK |
493 | if (sk->sk_type == SOCK_SEQPACKET) { |
494 | if (!new_transport->seqpacket_allow || | |
495 | !new_transport->seqpacket_allow(remote_cid)) { | |
496 | module_put(new_transport->module); | |
497 | return -ESOCKTNOSUPPORT; | |
498 | } | |
499 | } | |
500 | ||
039fccca SG |
501 | ret = new_transport->init(vsk, psk); |
502 | if (ret) { | |
503 | module_put(new_transport->module); | |
504 | return ret; | |
505 | } | |
506 | ||
c0cfa2d8 SG |
507 | vsk->transport = new_transport; |
508 | ||
039fccca | 509 | return 0; |
c0cfa2d8 SG |
510 | } |
511 | EXPORT_SYMBOL_GPL(vsock_assign_transport); | |
512 | ||
513 | bool vsock_find_cid(unsigned int cid) | |
514 | { | |
515 | if (transport_g2h && cid == transport_g2h->get_local_cid()) | |
516 | return true; | |
517 | ||
518 | if (transport_h2g && cid == VMADDR_CID_HOST) | |
519 | return true; | |
520 | ||
408624af SG |
521 | if (transport_local && cid == VMADDR_CID_LOCAL) |
522 | return true; | |
523 | ||
c0cfa2d8 SG |
524 | return false; |
525 | } | |
526 | EXPORT_SYMBOL_GPL(vsock_find_cid); | |
527 | ||
d021c344 AK |
528 | static struct sock *vsock_dequeue_accept(struct sock *listener) |
529 | { | |
530 | struct vsock_sock *vlistener; | |
531 | struct vsock_sock *vconnected; | |
532 | ||
533 | vlistener = vsock_sk(listener); | |
534 | ||
535 | if (list_empty(&vlistener->accept_queue)) | |
536 | return NULL; | |
537 | ||
538 | vconnected = list_entry(vlistener->accept_queue.next, | |
539 | struct vsock_sock, accept_queue); | |
540 | ||
541 | list_del_init(&vconnected->accept_queue); | |
542 | sock_put(listener); | |
543 | /* The caller will need a reference on the connected socket so we let | |
544 | * it call sock_put(). | |
545 | */ | |
546 | ||
547 | return sk_vsock(vconnected); | |
548 | } | |
549 | ||
550 | static bool vsock_is_accept_queue_empty(struct sock *sk) | |
551 | { | |
552 | struct vsock_sock *vsk = vsock_sk(sk); | |
553 | return list_empty(&vsk->accept_queue); | |
554 | } | |
555 | ||
556 | static bool vsock_is_pending(struct sock *sk) | |
557 | { | |
558 | struct vsock_sock *vsk = vsock_sk(sk); | |
559 | return !list_empty(&vsk->pending_links); | |
560 | } | |
561 | ||
562 | static int vsock_send_shutdown(struct sock *sk, int mode) | |
563 | { | |
fe502c4a SG |
564 | struct vsock_sock *vsk = vsock_sk(sk); |
565 | ||
c0cfa2d8 SG |
566 | if (!vsk->transport) |
567 | return -ENODEV; | |
568 | ||
fe502c4a | 569 | return vsk->transport->shutdown(vsk, mode); |
d021c344 AK |
570 | } |
571 | ||
455f05ec | 572 | static void vsock_pending_work(struct work_struct *work) |
d021c344 AK |
573 | { |
574 | struct sock *sk; | |
575 | struct sock *listener; | |
576 | struct vsock_sock *vsk; | |
577 | bool cleanup; | |
578 | ||
455f05ec | 579 | vsk = container_of(work, struct vsock_sock, pending_work.work); |
d021c344 AK |
580 | sk = sk_vsock(vsk); |
581 | listener = vsk->listener; | |
582 | cleanup = true; | |
583 | ||
584 | lock_sock(listener); | |
4192f672 | 585 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
d021c344 AK |
586 | |
587 | if (vsock_is_pending(sk)) { | |
588 | vsock_remove_pending(listener, sk); | |
1190cfdb | 589 | |
7976a11b | 590 | sk_acceptq_removed(listener); |
d021c344 AK |
591 | } else if (!vsk->rejected) { |
592 | /* We are not on the pending list and accept() did not reject | |
593 | * us, so we must have been accepted by our user process. We | |
594 | * just need to drop our references to the sockets and be on | |
595 | * our way. | |
596 | */ | |
597 | cleanup = false; | |
598 | goto out; | |
599 | } | |
600 | ||
d021c344 AK |
601 | /* We need to remove ourself from the global connected sockets list so |
602 | * incoming packets can't find this socket, and to reduce the reference | |
603 | * count. | |
604 | */ | |
d5afa82c | 605 | vsock_remove_connected(vsk); |
d021c344 | 606 | |
3b4477d2 | 607 | sk->sk_state = TCP_CLOSE; |
d021c344 AK |
608 | |
609 | out: | |
610 | release_sock(sk); | |
611 | release_sock(listener); | |
612 | if (cleanup) | |
613 | sock_put(sk); | |
614 | ||
615 | sock_put(sk); | |
616 | sock_put(listener); | |
617 | } | |
d021c344 AK |
618 | |
619 | /**** SOCKET OPERATIONS ****/ | |
620 | ||
a9e29e55 AK |
621 | static int __vsock_bind_connectible(struct vsock_sock *vsk, |
622 | struct sockaddr_vm *addr) | |
d021c344 | 623 | { |
a22d3251 | 624 | static u32 port; |
d021c344 AK |
625 | struct sockaddr_vm new_addr; |
626 | ||
8236b08c LW |
627 | if (!port) |
628 | port = LAST_RESERVED_PORT + 1 + | |
629 | prandom_u32_max(U32_MAX - LAST_RESERVED_PORT); | |
630 | ||
d021c344 AK |
631 | vsock_addr_init(&new_addr, addr->svm_cid, addr->svm_port); |
632 | ||
633 | if (addr->svm_port == VMADDR_PORT_ANY) { | |
634 | bool found = false; | |
635 | unsigned int i; | |
636 | ||
637 | for (i = 0; i < MAX_PORT_RETRIES; i++) { | |
638 | if (port <= LAST_RESERVED_PORT) | |
639 | port = LAST_RESERVED_PORT + 1; | |
640 | ||
641 | new_addr.svm_port = port++; | |
642 | ||
643 | if (!__vsock_find_bound_socket(&new_addr)) { | |
644 | found = true; | |
645 | break; | |
646 | } | |
647 | } | |
648 | ||
649 | if (!found) | |
650 | return -EADDRNOTAVAIL; | |
651 | } else { | |
652 | /* If port is in reserved range, ensure caller | |
653 | * has necessary privileges. | |
654 | */ | |
655 | if (addr->svm_port <= LAST_RESERVED_PORT && | |
656 | !capable(CAP_NET_BIND_SERVICE)) { | |
657 | return -EACCES; | |
658 | } | |
659 | ||
660 | if (__vsock_find_bound_socket(&new_addr)) | |
661 | return -EADDRINUSE; | |
662 | } | |
663 | ||
664 | vsock_addr_init(&vsk->local_addr, new_addr.svm_cid, new_addr.svm_port); | |
665 | ||
8cb48554 AK |
666 | /* Remove connection oriented sockets from the unbound list and add them |
667 | * to the hash table for easy lookup by its address. The unbound list | |
668 | * is simply an extra entry at the end of the hash table, a trick used | |
669 | * by AF_UNIX. | |
d021c344 AK |
670 | */ |
671 | __vsock_remove_bound(vsk); | |
672 | __vsock_insert_bound(vsock_bound_sockets(&vsk->local_addr), vsk); | |
673 | ||
674 | return 0; | |
675 | } | |
676 | ||
677 | static int __vsock_bind_dgram(struct vsock_sock *vsk, | |
678 | struct sockaddr_vm *addr) | |
679 | { | |
fe502c4a | 680 | return vsk->transport->dgram_bind(vsk, addr); |
d021c344 AK |
681 | } |
682 | ||
683 | static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr) | |
684 | { | |
685 | struct vsock_sock *vsk = vsock_sk(sk); | |
d021c344 AK |
686 | int retval; |
687 | ||
688 | /* First ensure this socket isn't already bound. */ | |
689 | if (vsock_addr_bound(&vsk->local_addr)) | |
690 | return -EINVAL; | |
691 | ||
692 | /* Now bind to the provided address or select appropriate values if | |
693 | * none are provided (VMADDR_CID_ANY and VMADDR_PORT_ANY). Note that | |
694 | * like AF_INET prevents binding to a non-local IP address (in most | |
c0cfa2d8 | 695 | * cases), we only allow binding to a local CID. |
d021c344 | 696 | */ |
c0cfa2d8 | 697 | if (addr->svm_cid != VMADDR_CID_ANY && !vsock_find_cid(addr->svm_cid)) |
d021c344 AK |
698 | return -EADDRNOTAVAIL; |
699 | ||
700 | switch (sk->sk_socket->type) { | |
701 | case SOCK_STREAM: | |
0798e78b | 702 | case SOCK_SEQPACKET: |
d021c344 | 703 | spin_lock_bh(&vsock_table_lock); |
a9e29e55 | 704 | retval = __vsock_bind_connectible(vsk, addr); |
d021c344 AK |
705 | spin_unlock_bh(&vsock_table_lock); |
706 | break; | |
707 | ||
708 | case SOCK_DGRAM: | |
709 | retval = __vsock_bind_dgram(vsk, addr); | |
710 | break; | |
711 | ||
712 | default: | |
713 | retval = -EINVAL; | |
714 | break; | |
715 | } | |
716 | ||
717 | return retval; | |
718 | } | |
719 | ||
455f05ec CW |
720 | static void vsock_connect_timeout(struct work_struct *work); |
721 | ||
b9ca2f5f SG |
722 | static struct sock *__vsock_create(struct net *net, |
723 | struct socket *sock, | |
724 | struct sock *parent, | |
725 | gfp_t priority, | |
726 | unsigned short type, | |
727 | int kern) | |
d021c344 AK |
728 | { |
729 | struct sock *sk; | |
730 | struct vsock_sock *psk; | |
731 | struct vsock_sock *vsk; | |
732 | ||
11aa9c28 | 733 | sk = sk_alloc(net, AF_VSOCK, priority, &vsock_proto, kern); |
d021c344 AK |
734 | if (!sk) |
735 | return NULL; | |
736 | ||
737 | sock_init_data(sock, sk); | |
738 | ||
739 | /* sk->sk_type is normally set in sock_init_data, but only if sock is | |
740 | * non-NULL. We make sure that our sockets always have a type by | |
741 | * setting it here if needed. | |
742 | */ | |
743 | if (!sock) | |
744 | sk->sk_type = type; | |
745 | ||
746 | vsk = vsock_sk(sk); | |
747 | vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY); | |
748 | vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY); | |
749 | ||
750 | sk->sk_destruct = vsock_sk_destruct; | |
751 | sk->sk_backlog_rcv = vsock_queue_rcv_skb; | |
d021c344 AK |
752 | sock_reset_flag(sk, SOCK_DONE); |
753 | ||
754 | INIT_LIST_HEAD(&vsk->bound_table); | |
755 | INIT_LIST_HEAD(&vsk->connected_table); | |
756 | vsk->listener = NULL; | |
757 | INIT_LIST_HEAD(&vsk->pending_links); | |
758 | INIT_LIST_HEAD(&vsk->accept_queue); | |
759 | vsk->rejected = false; | |
760 | vsk->sent_request = false; | |
761 | vsk->ignore_connecting_rst = false; | |
762 | vsk->peer_shutdown = 0; | |
455f05ec CW |
763 | INIT_DELAYED_WORK(&vsk->connect_work, vsock_connect_timeout); |
764 | INIT_DELAYED_WORK(&vsk->pending_work, vsock_pending_work); | |
d021c344 AK |
765 | |
766 | psk = parent ? vsock_sk(parent) : NULL; | |
767 | if (parent) { | |
768 | vsk->trusted = psk->trusted; | |
769 | vsk->owner = get_cred(psk->owner); | |
770 | vsk->connect_timeout = psk->connect_timeout; | |
b9f2b0ff SG |
771 | vsk->buffer_size = psk->buffer_size; |
772 | vsk->buffer_min_size = psk->buffer_min_size; | |
773 | vsk->buffer_max_size = psk->buffer_max_size; | |
1f935e8e | 774 | security_sk_clone(parent, sk); |
d021c344 | 775 | } else { |
af545bb5 | 776 | vsk->trusted = ns_capable_noaudit(&init_user_ns, CAP_NET_ADMIN); |
d021c344 AK |
777 | vsk->owner = get_current_cred(); |
778 | vsk->connect_timeout = VSOCK_DEFAULT_CONNECT_TIMEOUT; | |
b9f2b0ff SG |
779 | vsk->buffer_size = VSOCK_DEFAULT_BUFFER_SIZE; |
780 | vsk->buffer_min_size = VSOCK_DEFAULT_BUFFER_MIN_SIZE; | |
781 | vsk->buffer_max_size = VSOCK_DEFAULT_BUFFER_MAX_SIZE; | |
d021c344 AK |
782 | } |
783 | ||
d021c344 AK |
784 | return sk; |
785 | } | |
d021c344 | 786 | |
a9e29e55 AK |
787 | static bool sock_type_connectible(u16 type) |
788 | { | |
0798e78b | 789 | return (type == SOCK_STREAM) || (type == SOCK_SEQPACKET); |
a9e29e55 AK |
790 | } |
791 | ||
0d9138ff | 792 | static void __vsock_release(struct sock *sk, int level) |
d021c344 AK |
793 | { |
794 | if (sk) { | |
d021c344 AK |
795 | struct sock *pending; |
796 | struct vsock_sock *vsk; | |
797 | ||
798 | vsk = vsock_sk(sk); | |
799 | pending = NULL; /* Compiler warning. */ | |
800 | ||
0d9138ff DC |
801 | /* When "level" is SINGLE_DEPTH_NESTING, use the nested |
802 | * version to avoid the warning "possible recursive locking | |
803 | * detected". When "level" is 0, lock_sock_nested(sk, level) | |
804 | * is the same as lock_sock(sk). | |
805 | */ | |
806 | lock_sock_nested(sk, level); | |
3f74957f SG |
807 | |
808 | if (vsk->transport) | |
809 | vsk->transport->release(vsk); | |
a9e29e55 | 810 | else if (sock_type_connectible(sk->sk_type)) |
3f74957f SG |
811 | vsock_remove_sock(vsk); |
812 | ||
d021c344 AK |
813 | sock_orphan(sk); |
814 | sk->sk_shutdown = SHUTDOWN_MASK; | |
815 | ||
3b7ad08b | 816 | skb_queue_purge(&sk->sk_receive_queue); |
d021c344 AK |
817 | |
818 | /* Clean up any sockets that never were accepted. */ | |
819 | while ((pending = vsock_dequeue_accept(sk)) != NULL) { | |
0d9138ff | 820 | __vsock_release(pending, SINGLE_DEPTH_NESTING); |
d021c344 AK |
821 | sock_put(pending); |
822 | } | |
823 | ||
824 | release_sock(sk); | |
825 | sock_put(sk); | |
826 | } | |
827 | } | |
828 | ||
829 | static void vsock_sk_destruct(struct sock *sk) | |
830 | { | |
831 | struct vsock_sock *vsk = vsock_sk(sk); | |
832 | ||
6a2c0962 | 833 | vsock_deassign_transport(vsk); |
d021c344 AK |
834 | |
835 | /* When clearing these addresses, there's no need to set the family and | |
836 | * possibly register the address family with the kernel. | |
837 | */ | |
838 | vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY); | |
839 | vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY); | |
840 | ||
841 | put_cred(vsk->owner); | |
842 | } | |
843 | ||
844 | static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) | |
845 | { | |
846 | int err; | |
847 | ||
848 | err = sock_queue_rcv_skb(sk, skb); | |
849 | if (err) | |
850 | kfree_skb(skb); | |
851 | ||
852 | return err; | |
853 | } | |
854 | ||
b9ca2f5f SG |
855 | struct sock *vsock_create_connected(struct sock *parent) |
856 | { | |
857 | return __vsock_create(sock_net(parent), NULL, parent, GFP_KERNEL, | |
858 | parent->sk_type, 0); | |
859 | } | |
860 | EXPORT_SYMBOL_GPL(vsock_create_connected); | |
861 | ||
d021c344 AK |
862 | s64 vsock_stream_has_data(struct vsock_sock *vsk) |
863 | { | |
fe502c4a | 864 | return vsk->transport->stream_has_data(vsk); |
d021c344 AK |
865 | } |
866 | EXPORT_SYMBOL_GPL(vsock_stream_has_data); | |
867 | ||
cc97141a | 868 | static s64 vsock_connectible_has_data(struct vsock_sock *vsk) |
0798e78b AK |
869 | { |
870 | struct sock *sk = sk_vsock(vsk); | |
871 | ||
872 | if (sk->sk_type == SOCK_SEQPACKET) | |
873 | return vsk->transport->seqpacket_has_data(vsk); | |
874 | else | |
875 | return vsock_stream_has_data(vsk); | |
876 | } | |
877 | ||
d021c344 AK |
878 | s64 vsock_stream_has_space(struct vsock_sock *vsk) |
879 | { | |
fe502c4a | 880 | return vsk->transport->stream_has_space(vsk); |
d021c344 AK |
881 | } |
882 | EXPORT_SYMBOL_GPL(vsock_stream_has_space); | |
883 | ||
884 | static int vsock_release(struct socket *sock) | |
885 | { | |
0d9138ff | 886 | __vsock_release(sock->sk, 0); |
d021c344 AK |
887 | sock->sk = NULL; |
888 | sock->state = SS_FREE; | |
889 | ||
890 | return 0; | |
891 | } | |
892 | ||
893 | static int | |
894 | vsock_bind(struct socket *sock, struct sockaddr *addr, int addr_len) | |
895 | { | |
896 | int err; | |
897 | struct sock *sk; | |
898 | struct sockaddr_vm *vm_addr; | |
899 | ||
900 | sk = sock->sk; | |
901 | ||
902 | if (vsock_addr_cast(addr, addr_len, &vm_addr) != 0) | |
903 | return -EINVAL; | |
904 | ||
905 | lock_sock(sk); | |
906 | err = __vsock_bind(sk, vm_addr); | |
907 | release_sock(sk); | |
908 | ||
909 | return err; | |
910 | } | |
911 | ||
912 | static int vsock_getname(struct socket *sock, | |
9b2c45d4 | 913 | struct sockaddr *addr, int peer) |
d021c344 AK |
914 | { |
915 | int err; | |
916 | struct sock *sk; | |
917 | struct vsock_sock *vsk; | |
918 | struct sockaddr_vm *vm_addr; | |
919 | ||
920 | sk = sock->sk; | |
921 | vsk = vsock_sk(sk); | |
922 | err = 0; | |
923 | ||
924 | lock_sock(sk); | |
925 | ||
926 | if (peer) { | |
927 | if (sock->state != SS_CONNECTED) { | |
928 | err = -ENOTCONN; | |
929 | goto out; | |
930 | } | |
931 | vm_addr = &vsk->remote_addr; | |
932 | } else { | |
933 | vm_addr = &vsk->local_addr; | |
934 | } | |
935 | ||
936 | if (!vm_addr) { | |
937 | err = -EINVAL; | |
938 | goto out; | |
939 | } | |
940 | ||
941 | /* sys_getsockname() and sys_getpeername() pass us a | |
942 | * MAX_SOCK_ADDR-sized buffer and don't set addr_len. Unfortunately | |
943 | * that macro is defined in socket.c instead of .h, so we hardcode its | |
944 | * value here. | |
945 | */ | |
946 | BUILD_BUG_ON(sizeof(*vm_addr) > 128); | |
947 | memcpy(addr, vm_addr, sizeof(*vm_addr)); | |
9b2c45d4 | 948 | err = sizeof(*vm_addr); |
d021c344 AK |
949 | |
950 | out: | |
951 | release_sock(sk); | |
952 | return err; | |
953 | } | |
954 | ||
955 | static int vsock_shutdown(struct socket *sock, int mode) | |
956 | { | |
957 | int err; | |
958 | struct sock *sk; | |
959 | ||
960 | /* User level uses SHUT_RD (0) and SHUT_WR (1), but the kernel uses | |
961 | * RCV_SHUTDOWN (1) and SEND_SHUTDOWN (2), so we must increment mode | |
962 | * here like the other address families do. Note also that the | |
963 | * increment makes SHUT_RDWR (2) into RCV_SHUTDOWN | SEND_SHUTDOWN (3), | |
964 | * which is what we want. | |
965 | */ | |
966 | mode++; | |
967 | ||
968 | if ((mode & ~SHUTDOWN_MASK) || !mode) | |
969 | return -EINVAL; | |
970 | ||
8cb48554 AK |
971 | /* If this is a connection oriented socket and it is not connected then |
972 | * bail out immediately. If it is a DGRAM socket then we must first | |
973 | * kick the socket so that it wakes up from any sleeping calls, for | |
974 | * example recv(), and then afterwards return the error. | |
d021c344 AK |
975 | */ |
976 | ||
977 | sk = sock->sk; | |
1c5fae9c SG |
978 | |
979 | lock_sock(sk); | |
d021c344 AK |
980 | if (sock->state == SS_UNCONNECTED) { |
981 | err = -ENOTCONN; | |
a9e29e55 | 982 | if (sock_type_connectible(sk->sk_type)) |
1c5fae9c | 983 | goto out; |
d021c344 AK |
984 | } else { |
985 | sock->state = SS_DISCONNECTING; | |
986 | err = 0; | |
987 | } | |
988 | ||
989 | /* Receive and send shutdowns are treated alike. */ | |
990 | mode = mode & (RCV_SHUTDOWN | SEND_SHUTDOWN); | |
991 | if (mode) { | |
d021c344 AK |
992 | sk->sk_shutdown |= mode; |
993 | sk->sk_state_change(sk); | |
d021c344 | 994 | |
a9e29e55 | 995 | if (sock_type_connectible(sk->sk_type)) { |
d021c344 AK |
996 | sock_reset_flag(sk, SOCK_DONE); |
997 | vsock_send_shutdown(sk, mode); | |
998 | } | |
999 | } | |
1000 | ||
1c5fae9c SG |
1001 | out: |
1002 | release_sock(sk); | |
d021c344 AK |
1003 | return err; |
1004 | } | |
1005 | ||
a11e1d43 LT |
1006 | static __poll_t vsock_poll(struct file *file, struct socket *sock, |
1007 | poll_table *wait) | |
d021c344 | 1008 | { |
a11e1d43 LT |
1009 | struct sock *sk; |
1010 | __poll_t mask; | |
1011 | struct vsock_sock *vsk; | |
1012 | ||
1013 | sk = sock->sk; | |
1014 | vsk = vsock_sk(sk); | |
1015 | ||
1016 | poll_wait(file, sk_sleep(sk), wait); | |
1017 | mask = 0; | |
d021c344 AK |
1018 | |
1019 | if (sk->sk_err) | |
1020 | /* Signify that there has been an error on this socket. */ | |
a9a08845 | 1021 | mask |= EPOLLERR; |
d021c344 AK |
1022 | |
1023 | /* INET sockets treat local write shutdown and peer write shutdown as a | |
a9a08845 | 1024 | * case of EPOLLHUP set. |
d021c344 AK |
1025 | */ |
1026 | if ((sk->sk_shutdown == SHUTDOWN_MASK) || | |
1027 | ((sk->sk_shutdown & SEND_SHUTDOWN) && | |
1028 | (vsk->peer_shutdown & SEND_SHUTDOWN))) { | |
a9a08845 | 1029 | mask |= EPOLLHUP; |
d021c344 AK |
1030 | } |
1031 | ||
1032 | if (sk->sk_shutdown & RCV_SHUTDOWN || | |
1033 | vsk->peer_shutdown & SEND_SHUTDOWN) { | |
a9a08845 | 1034 | mask |= EPOLLRDHUP; |
d021c344 AK |
1035 | } |
1036 | ||
1037 | if (sock->type == SOCK_DGRAM) { | |
1038 | /* For datagram sockets we can read if there is something in | |
1039 | * the queue and write as long as the socket isn't shutdown for | |
1040 | * sending. | |
1041 | */ | |
3ef7cf57 | 1042 | if (!skb_queue_empty_lockless(&sk->sk_receive_queue) || |
d021c344 | 1043 | (sk->sk_shutdown & RCV_SHUTDOWN)) { |
a9a08845 | 1044 | mask |= EPOLLIN | EPOLLRDNORM; |
d021c344 AK |
1045 | } |
1046 | ||
1047 | if (!(sk->sk_shutdown & SEND_SHUTDOWN)) | |
a9a08845 | 1048 | mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND; |
d021c344 | 1049 | |
a9e29e55 | 1050 | } else if (sock_type_connectible(sk->sk_type)) { |
c518adaf AP |
1051 | const struct vsock_transport *transport; |
1052 | ||
d021c344 AK |
1053 | lock_sock(sk); |
1054 | ||
c518adaf AP |
1055 | transport = vsk->transport; |
1056 | ||
d021c344 AK |
1057 | /* Listening sockets that have connections in their accept |
1058 | * queue can be read. | |
1059 | */ | |
3b4477d2 | 1060 | if (sk->sk_state == TCP_LISTEN |
d021c344 | 1061 | && !vsock_is_accept_queue_empty(sk)) |
a9a08845 | 1062 | mask |= EPOLLIN | EPOLLRDNORM; |
d021c344 AK |
1063 | |
1064 | /* If there is something in the queue then we can read. */ | |
c0cfa2d8 | 1065 | if (transport && transport->stream_is_active(vsk) && |
d021c344 AK |
1066 | !(sk->sk_shutdown & RCV_SHUTDOWN)) { |
1067 | bool data_ready_now = false; | |
1068 | int ret = transport->notify_poll_in( | |
1069 | vsk, 1, &data_ready_now); | |
1070 | if (ret < 0) { | |
a9a08845 | 1071 | mask |= EPOLLERR; |
d021c344 AK |
1072 | } else { |
1073 | if (data_ready_now) | |
a9a08845 | 1074 | mask |= EPOLLIN | EPOLLRDNORM; |
d021c344 AK |
1075 | |
1076 | } | |
1077 | } | |
1078 | ||
1079 | /* Sockets whose connections have been closed, reset, or | |
1080 | * terminated should also be considered read, and we check the | |
1081 | * shutdown flag for that. | |
1082 | */ | |
1083 | if (sk->sk_shutdown & RCV_SHUTDOWN || | |
1084 | vsk->peer_shutdown & SEND_SHUTDOWN) { | |
a9a08845 | 1085 | mask |= EPOLLIN | EPOLLRDNORM; |
d021c344 AK |
1086 | } |
1087 | ||
1088 | /* Connected sockets that can produce data can be written. */ | |
1980c058 | 1089 | if (transport && sk->sk_state == TCP_ESTABLISHED) { |
d021c344 AK |
1090 | if (!(sk->sk_shutdown & SEND_SHUTDOWN)) { |
1091 | bool space_avail_now = false; | |
1092 | int ret = transport->notify_poll_out( | |
1093 | vsk, 1, &space_avail_now); | |
1094 | if (ret < 0) { | |
a9a08845 | 1095 | mask |= EPOLLERR; |
d021c344 AK |
1096 | } else { |
1097 | if (space_avail_now) | |
a9a08845 | 1098 | /* Remove EPOLLWRBAND since INET |
d021c344 AK |
1099 | * sockets are not setting it. |
1100 | */ | |
a9a08845 | 1101 | mask |= EPOLLOUT | EPOLLWRNORM; |
d021c344 AK |
1102 | |
1103 | } | |
1104 | } | |
1105 | } | |
1106 | ||
1107 | /* Simulate INET socket poll behaviors, which sets | |
a9a08845 | 1108 | * EPOLLOUT|EPOLLWRNORM when peer is closed and nothing to read, |
d021c344 AK |
1109 | * but local send is not shutdown. |
1110 | */ | |
ba3169fc | 1111 | if (sk->sk_state == TCP_CLOSE || sk->sk_state == TCP_CLOSING) { |
d021c344 | 1112 | if (!(sk->sk_shutdown & SEND_SHUTDOWN)) |
a9a08845 | 1113 | mask |= EPOLLOUT | EPOLLWRNORM; |
d021c344 AK |
1114 | |
1115 | } | |
1116 | ||
1117 | release_sock(sk); | |
1118 | } | |
1119 | ||
1120 | return mask; | |
1121 | } | |
1122 | ||
1b784140 YX |
1123 | static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg, |
1124 | size_t len) | |
d021c344 AK |
1125 | { |
1126 | int err; | |
1127 | struct sock *sk; | |
1128 | struct vsock_sock *vsk; | |
1129 | struct sockaddr_vm *remote_addr; | |
fe502c4a | 1130 | const struct vsock_transport *transport; |
d021c344 AK |
1131 | |
1132 | if (msg->msg_flags & MSG_OOB) | |
1133 | return -EOPNOTSUPP; | |
1134 | ||
1135 | /* For now, MSG_DONTWAIT is always assumed... */ | |
1136 | err = 0; | |
1137 | sk = sock->sk; | |
1138 | vsk = vsock_sk(sk); | |
1139 | ||
1140 | lock_sock(sk); | |
1141 | ||
c518adaf AP |
1142 | transport = vsk->transport; |
1143 | ||
b3a6dfe8 AH |
1144 | err = vsock_auto_bind(vsk); |
1145 | if (err) | |
1146 | goto out; | |
d021c344 | 1147 | |
d021c344 AK |
1148 | |
1149 | /* If the provided message contains an address, use that. Otherwise | |
1150 | * fall back on the socket's remote handle (if it has been connected). | |
1151 | */ | |
1152 | if (msg->msg_name && | |
1153 | vsock_addr_cast(msg->msg_name, msg->msg_namelen, | |
1154 | &remote_addr) == 0) { | |
1155 | /* Ensure this address is of the right type and is a valid | |
1156 | * destination. | |
1157 | */ | |
1158 | ||
1159 | if (remote_addr->svm_cid == VMADDR_CID_ANY) | |
1160 | remote_addr->svm_cid = transport->get_local_cid(); | |
1161 | ||
1162 | if (!vsock_addr_bound(remote_addr)) { | |
1163 | err = -EINVAL; | |
1164 | goto out; | |
1165 | } | |
1166 | } else if (sock->state == SS_CONNECTED) { | |
1167 | remote_addr = &vsk->remote_addr; | |
1168 | ||
1169 | if (remote_addr->svm_cid == VMADDR_CID_ANY) | |
1170 | remote_addr->svm_cid = transport->get_local_cid(); | |
1171 | ||
1172 | /* XXX Should connect() or this function ensure remote_addr is | |
1173 | * bound? | |
1174 | */ | |
1175 | if (!vsock_addr_bound(&vsk->remote_addr)) { | |
1176 | err = -EINVAL; | |
1177 | goto out; | |
1178 | } | |
1179 | } else { | |
1180 | err = -EINVAL; | |
1181 | goto out; | |
1182 | } | |
1183 | ||
1184 | if (!transport->dgram_allow(remote_addr->svm_cid, | |
1185 | remote_addr->svm_port)) { | |
1186 | err = -EINVAL; | |
1187 | goto out; | |
1188 | } | |
1189 | ||
0f7db23a | 1190 | err = transport->dgram_enqueue(vsk, remote_addr, msg, len); |
d021c344 AK |
1191 | |
1192 | out: | |
1193 | release_sock(sk); | |
1194 | return err; | |
1195 | } | |
1196 | ||
1197 | static int vsock_dgram_connect(struct socket *sock, | |
1198 | struct sockaddr *addr, int addr_len, int flags) | |
1199 | { | |
1200 | int err; | |
1201 | struct sock *sk; | |
1202 | struct vsock_sock *vsk; | |
1203 | struct sockaddr_vm *remote_addr; | |
1204 | ||
1205 | sk = sock->sk; | |
1206 | vsk = vsock_sk(sk); | |
1207 | ||
1208 | err = vsock_addr_cast(addr, addr_len, &remote_addr); | |
1209 | if (err == -EAFNOSUPPORT && remote_addr->svm_family == AF_UNSPEC) { | |
1210 | lock_sock(sk); | |
1211 | vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, | |
1212 | VMADDR_PORT_ANY); | |
1213 | sock->state = SS_UNCONNECTED; | |
1214 | release_sock(sk); | |
1215 | return 0; | |
1216 | } else if (err != 0) | |
1217 | return -EINVAL; | |
1218 | ||
1219 | lock_sock(sk); | |
1220 | ||
b3a6dfe8 AH |
1221 | err = vsock_auto_bind(vsk); |
1222 | if (err) | |
1223 | goto out; | |
d021c344 | 1224 | |
fe502c4a SG |
1225 | if (!vsk->transport->dgram_allow(remote_addr->svm_cid, |
1226 | remote_addr->svm_port)) { | |
d021c344 AK |
1227 | err = -EINVAL; |
1228 | goto out; | |
1229 | } | |
1230 | ||
1231 | memcpy(&vsk->remote_addr, remote_addr, sizeof(vsk->remote_addr)); | |
1232 | sock->state = SS_CONNECTED; | |
1233 | ||
1234 | out: | |
1235 | release_sock(sk); | |
1236 | return err; | |
1237 | } | |
1238 | ||
1b784140 YX |
1239 | static int vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg, |
1240 | size_t len, int flags) | |
d021c344 | 1241 | { |
fe502c4a SG |
1242 | struct vsock_sock *vsk = vsock_sk(sock->sk); |
1243 | ||
1244 | return vsk->transport->dgram_dequeue(vsk, msg, len, flags); | |
d021c344 AK |
1245 | } |
1246 | ||
1247 | static const struct proto_ops vsock_dgram_ops = { | |
1248 | .family = PF_VSOCK, | |
1249 | .owner = THIS_MODULE, | |
1250 | .release = vsock_release, | |
1251 | .bind = vsock_bind, | |
1252 | .connect = vsock_dgram_connect, | |
1253 | .socketpair = sock_no_socketpair, | |
1254 | .accept = sock_no_accept, | |
1255 | .getname = vsock_getname, | |
a11e1d43 | 1256 | .poll = vsock_poll, |
d021c344 AK |
1257 | .ioctl = sock_no_ioctl, |
1258 | .listen = sock_no_listen, | |
1259 | .shutdown = vsock_shutdown, | |
d021c344 AK |
1260 | .sendmsg = vsock_dgram_sendmsg, |
1261 | .recvmsg = vsock_dgram_recvmsg, | |
1262 | .mmap = sock_no_mmap, | |
1263 | .sendpage = sock_no_sendpage, | |
1264 | }; | |
1265 | ||
380feae0 PT |
1266 | static int vsock_transport_cancel_pkt(struct vsock_sock *vsk) |
1267 | { | |
fe502c4a SG |
1268 | const struct vsock_transport *transport = vsk->transport; |
1269 | ||
5d1cbcc9 | 1270 | if (!transport || !transport->cancel_pkt) |
380feae0 PT |
1271 | return -EOPNOTSUPP; |
1272 | ||
1273 | return transport->cancel_pkt(vsk); | |
1274 | } | |
1275 | ||
d021c344 AK |
1276 | static void vsock_connect_timeout(struct work_struct *work) |
1277 | { | |
1278 | struct sock *sk; | |
1279 | struct vsock_sock *vsk; | |
1280 | ||
455f05ec | 1281 | vsk = container_of(work, struct vsock_sock, connect_work.work); |
d021c344 AK |
1282 | sk = sk_vsock(vsk); |
1283 | ||
1284 | lock_sock(sk); | |
3b4477d2 | 1285 | if (sk->sk_state == TCP_SYN_SENT && |
d021c344 | 1286 | (sk->sk_shutdown != SHUTDOWN_MASK)) { |
3b4477d2 | 1287 | sk->sk_state = TCP_CLOSE; |
d021c344 | 1288 | sk->sk_err = ETIMEDOUT; |
e3ae2365 | 1289 | sk_error_report(sk); |
3d0bc44d | 1290 | vsock_transport_cancel_pkt(vsk); |
d021c344 AK |
1291 | } |
1292 | release_sock(sk); | |
1293 | ||
1294 | sock_put(sk); | |
1295 | } | |
1296 | ||
a9e29e55 AK |
1297 | static int vsock_connect(struct socket *sock, struct sockaddr *addr, |
1298 | int addr_len, int flags) | |
d021c344 AK |
1299 | { |
1300 | int err; | |
1301 | struct sock *sk; | |
1302 | struct vsock_sock *vsk; | |
fe502c4a | 1303 | const struct vsock_transport *transport; |
d021c344 AK |
1304 | struct sockaddr_vm *remote_addr; |
1305 | long timeout; | |
1306 | DEFINE_WAIT(wait); | |
1307 | ||
1308 | err = 0; | |
1309 | sk = sock->sk; | |
1310 | vsk = vsock_sk(sk); | |
1311 | ||
1312 | lock_sock(sk); | |
1313 | ||
1314 | /* XXX AF_UNSPEC should make us disconnect like AF_INET. */ | |
1315 | switch (sock->state) { | |
1316 | case SS_CONNECTED: | |
1317 | err = -EISCONN; | |
1318 | goto out; | |
1319 | case SS_DISCONNECTING: | |
1320 | err = -EINVAL; | |
1321 | goto out; | |
1322 | case SS_CONNECTING: | |
1323 | /* This continues on so we can move sock into the SS_CONNECTED | |
1324 | * state once the connection has completed (at which point err | |
1325 | * will be set to zero also). Otherwise, we will either wait | |
1326 | * for the connection or return -EALREADY should this be a | |
1327 | * non-blocking call. | |
1328 | */ | |
1329 | err = -EALREADY; | |
275a511a ET |
1330 | if (flags & O_NONBLOCK) |
1331 | goto out; | |
d021c344 AK |
1332 | break; |
1333 | default: | |
3b4477d2 | 1334 | if ((sk->sk_state == TCP_LISTEN) || |
d021c344 AK |
1335 | vsock_addr_cast(addr, addr_len, &remote_addr) != 0) { |
1336 | err = -EINVAL; | |
1337 | goto out; | |
1338 | } | |
1339 | ||
c0cfa2d8 SG |
1340 | /* Set the remote address that we are connecting to. */ |
1341 | memcpy(&vsk->remote_addr, remote_addr, | |
1342 | sizeof(vsk->remote_addr)); | |
1343 | ||
1344 | err = vsock_assign_transport(vsk, NULL); | |
1345 | if (err) | |
1346 | goto out; | |
1347 | ||
1348 | transport = vsk->transport; | |
1349 | ||
d021c344 AK |
1350 | /* The hypervisor and well-known contexts do not have socket |
1351 | * endpoints. | |
1352 | */ | |
c0cfa2d8 SG |
1353 | if (!transport || |
1354 | !transport->stream_allow(remote_addr->svm_cid, | |
d021c344 AK |
1355 | remote_addr->svm_port)) { |
1356 | err = -ENETUNREACH; | |
1357 | goto out; | |
1358 | } | |
1359 | ||
b3a6dfe8 AH |
1360 | err = vsock_auto_bind(vsk); |
1361 | if (err) | |
1362 | goto out; | |
d021c344 | 1363 | |
3b4477d2 | 1364 | sk->sk_state = TCP_SYN_SENT; |
d021c344 AK |
1365 | |
1366 | err = transport->connect(vsk); | |
1367 | if (err < 0) | |
1368 | goto out; | |
1369 | ||
1370 | /* Mark sock as connecting and set the error code to in | |
1371 | * progress in case this is a non-blocking connect. | |
1372 | */ | |
1373 | sock->state = SS_CONNECTING; | |
1374 | err = -EINPROGRESS; | |
1375 | } | |
1376 | ||
1377 | /* The receive path will handle all communication until we are able to | |
1378 | * enter the connected state. Here we wait for the connection to be | |
1379 | * completed or a notification of an error. | |
1380 | */ | |
1381 | timeout = vsk->connect_timeout; | |
1382 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | |
1383 | ||
3b4477d2 | 1384 | while (sk->sk_state != TCP_ESTABLISHED && sk->sk_err == 0) { |
d021c344 AK |
1385 | if (flags & O_NONBLOCK) { |
1386 | /* If we're not going to block, we schedule a timeout | |
1387 | * function to generate a timeout on the connection | |
1388 | * attempt, in case the peer doesn't respond in a | |
1389 | * timely manner. We hold on to the socket until the | |
1390 | * timeout fires. | |
1391 | */ | |
1392 | sock_hold(sk); | |
455f05ec | 1393 | schedule_delayed_work(&vsk->connect_work, timeout); |
d021c344 AK |
1394 | |
1395 | /* Skip ahead to preserve error code set above. */ | |
1396 | goto out_wait; | |
1397 | } | |
1398 | ||
1399 | release_sock(sk); | |
1400 | timeout = schedule_timeout(timeout); | |
1401 | lock_sock(sk); | |
1402 | ||
1403 | if (signal_pending(current)) { | |
1404 | err = sock_intr_errno(timeout); | |
c7ff9cff | 1405 | sk->sk_state = sk->sk_state == TCP_ESTABLISHED ? TCP_CLOSING : TCP_CLOSE; |
f7f9b5e7 | 1406 | sock->state = SS_UNCONNECTED; |
380feae0 | 1407 | vsock_transport_cancel_pkt(vsk); |
af906f9c | 1408 | vsock_remove_connected(vsk); |
f7f9b5e7 | 1409 | goto out_wait; |
d021c344 AK |
1410 | } else if (timeout == 0) { |
1411 | err = -ETIMEDOUT; | |
3b4477d2 | 1412 | sk->sk_state = TCP_CLOSE; |
f7f9b5e7 | 1413 | sock->state = SS_UNCONNECTED; |
380feae0 | 1414 | vsock_transport_cancel_pkt(vsk); |
f7f9b5e7 | 1415 | goto out_wait; |
d021c344 AK |
1416 | } |
1417 | ||
1418 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | |
1419 | } | |
1420 | ||
1421 | if (sk->sk_err) { | |
1422 | err = -sk->sk_err; | |
3b4477d2 | 1423 | sk->sk_state = TCP_CLOSE; |
f7f9b5e7 CI |
1424 | sock->state = SS_UNCONNECTED; |
1425 | } else { | |
d021c344 | 1426 | err = 0; |
f7f9b5e7 | 1427 | } |
d021c344 AK |
1428 | |
1429 | out_wait: | |
1430 | finish_wait(sk_sleep(sk), &wait); | |
1431 | out: | |
1432 | release_sock(sk); | |
1433 | return err; | |
d021c344 AK |
1434 | } |
1435 | ||
cdfbabfb DH |
1436 | static int vsock_accept(struct socket *sock, struct socket *newsock, int flags, |
1437 | bool kern) | |
d021c344 AK |
1438 | { |
1439 | struct sock *listener; | |
1440 | int err; | |
1441 | struct sock *connected; | |
1442 | struct vsock_sock *vconnected; | |
1443 | long timeout; | |
1444 | DEFINE_WAIT(wait); | |
1445 | ||
1446 | err = 0; | |
1447 | listener = sock->sk; | |
1448 | ||
1449 | lock_sock(listener); | |
1450 | ||
a9e29e55 | 1451 | if (!sock_type_connectible(sock->type)) { |
d021c344 AK |
1452 | err = -EOPNOTSUPP; |
1453 | goto out; | |
1454 | } | |
1455 | ||
3b4477d2 | 1456 | if (listener->sk_state != TCP_LISTEN) { |
d021c344 AK |
1457 | err = -EINVAL; |
1458 | goto out; | |
1459 | } | |
1460 | ||
1461 | /* Wait for children sockets to appear; these are the new sockets | |
1462 | * created upon connection establishment. | |
1463 | */ | |
7e0afbdf | 1464 | timeout = sock_rcvtimeo(listener, flags & O_NONBLOCK); |
d021c344 AK |
1465 | prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE); |
1466 | ||
1467 | while ((connected = vsock_dequeue_accept(listener)) == NULL && | |
1468 | listener->sk_err == 0) { | |
1469 | release_sock(listener); | |
1470 | timeout = schedule_timeout(timeout); | |
f7f9b5e7 | 1471 | finish_wait(sk_sleep(listener), &wait); |
d021c344 AK |
1472 | lock_sock(listener); |
1473 | ||
1474 | if (signal_pending(current)) { | |
1475 | err = sock_intr_errno(timeout); | |
f7f9b5e7 | 1476 | goto out; |
d021c344 AK |
1477 | } else if (timeout == 0) { |
1478 | err = -EAGAIN; | |
f7f9b5e7 | 1479 | goto out; |
d021c344 AK |
1480 | } |
1481 | ||
1482 | prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE); | |
1483 | } | |
f7f9b5e7 | 1484 | finish_wait(sk_sleep(listener), &wait); |
d021c344 AK |
1485 | |
1486 | if (listener->sk_err) | |
1487 | err = -listener->sk_err; | |
1488 | ||
1489 | if (connected) { | |
7976a11b | 1490 | sk_acceptq_removed(listener); |
d021c344 | 1491 | |
4192f672 | 1492 | lock_sock_nested(connected, SINGLE_DEPTH_NESTING); |
d021c344 AK |
1493 | vconnected = vsock_sk(connected); |
1494 | ||
1495 | /* If the listener socket has received an error, then we should | |
1496 | * reject this socket and return. Note that we simply mark the | |
1497 | * socket rejected, drop our reference, and let the cleanup | |
1498 | * function handle the cleanup; the fact that we found it in | |
1499 | * the listener's accept queue guarantees that the cleanup | |
1500 | * function hasn't run yet. | |
1501 | */ | |
1502 | if (err) { | |
1503 | vconnected->rejected = true; | |
f7f9b5e7 CI |
1504 | } else { |
1505 | newsock->state = SS_CONNECTED; | |
1506 | sock_graft(connected, newsock); | |
d021c344 AK |
1507 | } |
1508 | ||
d021c344 AK |
1509 | release_sock(connected); |
1510 | sock_put(connected); | |
1511 | } | |
1512 | ||
d021c344 AK |
1513 | out: |
1514 | release_sock(listener); | |
1515 | return err; | |
1516 | } | |
1517 | ||
1518 | static int vsock_listen(struct socket *sock, int backlog) | |
1519 | { | |
1520 | int err; | |
1521 | struct sock *sk; | |
1522 | struct vsock_sock *vsk; | |
1523 | ||
1524 | sk = sock->sk; | |
1525 | ||
1526 | lock_sock(sk); | |
1527 | ||
a9e29e55 | 1528 | if (!sock_type_connectible(sk->sk_type)) { |
d021c344 AK |
1529 | err = -EOPNOTSUPP; |
1530 | goto out; | |
1531 | } | |
1532 | ||
1533 | if (sock->state != SS_UNCONNECTED) { | |
1534 | err = -EINVAL; | |
1535 | goto out; | |
1536 | } | |
1537 | ||
1538 | vsk = vsock_sk(sk); | |
1539 | ||
1540 | if (!vsock_addr_bound(&vsk->local_addr)) { | |
1541 | err = -EINVAL; | |
1542 | goto out; | |
1543 | } | |
1544 | ||
1545 | sk->sk_max_ack_backlog = backlog; | |
3b4477d2 | 1546 | sk->sk_state = TCP_LISTEN; |
d021c344 AK |
1547 | |
1548 | err = 0; | |
1549 | ||
1550 | out: | |
1551 | release_sock(sk); | |
1552 | return err; | |
1553 | } | |
1554 | ||
b9f2b0ff SG |
1555 | static void vsock_update_buffer_size(struct vsock_sock *vsk, |
1556 | const struct vsock_transport *transport, | |
1557 | u64 val) | |
1558 | { | |
1559 | if (val > vsk->buffer_max_size) | |
1560 | val = vsk->buffer_max_size; | |
1561 | ||
1562 | if (val < vsk->buffer_min_size) | |
1563 | val = vsk->buffer_min_size; | |
1564 | ||
1565 | if (val != vsk->buffer_size && | |
1566 | transport && transport->notify_buffer_size) | |
1567 | transport->notify_buffer_size(vsk, &val); | |
1568 | ||
1569 | vsk->buffer_size = val; | |
1570 | } | |
1571 | ||
a9e29e55 AK |
1572 | static int vsock_connectible_setsockopt(struct socket *sock, |
1573 | int level, | |
1574 | int optname, | |
1575 | sockptr_t optval, | |
1576 | unsigned int optlen) | |
d021c344 AK |
1577 | { |
1578 | int err; | |
1579 | struct sock *sk; | |
1580 | struct vsock_sock *vsk; | |
fe502c4a | 1581 | const struct vsock_transport *transport; |
d021c344 AK |
1582 | u64 val; |
1583 | ||
1584 | if (level != AF_VSOCK) | |
1585 | return -ENOPROTOOPT; | |
1586 | ||
1587 | #define COPY_IN(_v) \ | |
1588 | do { \ | |
1589 | if (optlen < sizeof(_v)) { \ | |
1590 | err = -EINVAL; \ | |
1591 | goto exit; \ | |
1592 | } \ | |
a7b75c5a | 1593 | if (copy_from_sockptr(&_v, optval, sizeof(_v)) != 0) { \ |
d021c344 AK |
1594 | err = -EFAULT; \ |
1595 | goto exit; \ | |
1596 | } \ | |
1597 | } while (0) | |
1598 | ||
1599 | err = 0; | |
1600 | sk = sock->sk; | |
1601 | vsk = vsock_sk(sk); | |
1602 | ||
1603 | lock_sock(sk); | |
1604 | ||
c518adaf AP |
1605 | transport = vsk->transport; |
1606 | ||
d021c344 AK |
1607 | switch (optname) { |
1608 | case SO_VM_SOCKETS_BUFFER_SIZE: | |
1609 | COPY_IN(val); | |
b9f2b0ff | 1610 | vsock_update_buffer_size(vsk, transport, val); |
d021c344 AK |
1611 | break; |
1612 | ||
1613 | case SO_VM_SOCKETS_BUFFER_MAX_SIZE: | |
1614 | COPY_IN(val); | |
b9f2b0ff SG |
1615 | vsk->buffer_max_size = val; |
1616 | vsock_update_buffer_size(vsk, transport, vsk->buffer_size); | |
d021c344 AK |
1617 | break; |
1618 | ||
1619 | case SO_VM_SOCKETS_BUFFER_MIN_SIZE: | |
1620 | COPY_IN(val); | |
b9f2b0ff SG |
1621 | vsk->buffer_min_size = val; |
1622 | vsock_update_buffer_size(vsk, transport, vsk->buffer_size); | |
d021c344 AK |
1623 | break; |
1624 | ||
1625 | case SO_VM_SOCKETS_CONNECT_TIMEOUT: { | |
fe0c72f3 | 1626 | struct __kernel_old_timeval tv; |
d021c344 AK |
1627 | COPY_IN(tv); |
1628 | if (tv.tv_sec >= 0 && tv.tv_usec < USEC_PER_SEC && | |
1629 | tv.tv_sec < (MAX_SCHEDULE_TIMEOUT / HZ - 1)) { | |
1630 | vsk->connect_timeout = tv.tv_sec * HZ + | |
1631 | DIV_ROUND_UP(tv.tv_usec, (1000000 / HZ)); | |
1632 | if (vsk->connect_timeout == 0) | |
1633 | vsk->connect_timeout = | |
1634 | VSOCK_DEFAULT_CONNECT_TIMEOUT; | |
1635 | ||
1636 | } else { | |
1637 | err = -ERANGE; | |
1638 | } | |
1639 | break; | |
1640 | } | |
1641 | ||
1642 | default: | |
1643 | err = -ENOPROTOOPT; | |
1644 | break; | |
1645 | } | |
1646 | ||
1647 | #undef COPY_IN | |
1648 | ||
1649 | exit: | |
1650 | release_sock(sk); | |
1651 | return err; | |
1652 | } | |
1653 | ||
a9e29e55 AK |
1654 | static int vsock_connectible_getsockopt(struct socket *sock, |
1655 | int level, int optname, | |
1656 | char __user *optval, | |
1657 | int __user *optlen) | |
d021c344 AK |
1658 | { |
1659 | int err; | |
1660 | int len; | |
1661 | struct sock *sk; | |
1662 | struct vsock_sock *vsk; | |
1663 | u64 val; | |
1664 | ||
1665 | if (level != AF_VSOCK) | |
1666 | return -ENOPROTOOPT; | |
1667 | ||
1668 | err = get_user(len, optlen); | |
1669 | if (err != 0) | |
1670 | return err; | |
1671 | ||
1672 | #define COPY_OUT(_v) \ | |
1673 | do { \ | |
1674 | if (len < sizeof(_v)) \ | |
1675 | return -EINVAL; \ | |
1676 | \ | |
1677 | len = sizeof(_v); \ | |
1678 | if (copy_to_user(optval, &_v, len) != 0) \ | |
1679 | return -EFAULT; \ | |
1680 | \ | |
1681 | } while (0) | |
1682 | ||
1683 | err = 0; | |
1684 | sk = sock->sk; | |
1685 | vsk = vsock_sk(sk); | |
1686 | ||
1687 | switch (optname) { | |
1688 | case SO_VM_SOCKETS_BUFFER_SIZE: | |
b9f2b0ff | 1689 | val = vsk->buffer_size; |
d021c344 AK |
1690 | COPY_OUT(val); |
1691 | break; | |
1692 | ||
1693 | case SO_VM_SOCKETS_BUFFER_MAX_SIZE: | |
b9f2b0ff | 1694 | val = vsk->buffer_max_size; |
d021c344 AK |
1695 | COPY_OUT(val); |
1696 | break; | |
1697 | ||
1698 | case SO_VM_SOCKETS_BUFFER_MIN_SIZE: | |
b9f2b0ff | 1699 | val = vsk->buffer_min_size; |
d021c344 AK |
1700 | COPY_OUT(val); |
1701 | break; | |
1702 | ||
1703 | case SO_VM_SOCKETS_CONNECT_TIMEOUT: { | |
fe0c72f3 | 1704 | struct __kernel_old_timeval tv; |
d021c344 AK |
1705 | tv.tv_sec = vsk->connect_timeout / HZ; |
1706 | tv.tv_usec = | |
1707 | (vsk->connect_timeout - | |
1708 | tv.tv_sec * HZ) * (1000000 / HZ); | |
1709 | COPY_OUT(tv); | |
1710 | break; | |
1711 | } | |
1712 | default: | |
1713 | return -ENOPROTOOPT; | |
1714 | } | |
1715 | ||
1716 | err = put_user(len, optlen); | |
1717 | if (err != 0) | |
1718 | return -EFAULT; | |
1719 | ||
1720 | #undef COPY_OUT | |
1721 | ||
1722 | return 0; | |
1723 | } | |
1724 | ||
a9e29e55 AK |
1725 | static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg, |
1726 | size_t len) | |
d021c344 AK |
1727 | { |
1728 | struct sock *sk; | |
1729 | struct vsock_sock *vsk; | |
fe502c4a | 1730 | const struct vsock_transport *transport; |
d021c344 AK |
1731 | ssize_t total_written; |
1732 | long timeout; | |
1733 | int err; | |
1734 | struct vsock_transport_send_notify_data send_data; | |
499fde66 | 1735 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
d021c344 AK |
1736 | |
1737 | sk = sock->sk; | |
1738 | vsk = vsock_sk(sk); | |
1739 | total_written = 0; | |
1740 | err = 0; | |
1741 | ||
1742 | if (msg->msg_flags & MSG_OOB) | |
1743 | return -EOPNOTSUPP; | |
1744 | ||
1745 | lock_sock(sk); | |
1746 | ||
c518adaf AP |
1747 | transport = vsk->transport; |
1748 | ||
8cb48554 AK |
1749 | /* Callers should not provide a destination with connection oriented |
1750 | * sockets. | |
1751 | */ | |
d021c344 | 1752 | if (msg->msg_namelen) { |
3b4477d2 | 1753 | err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP; |
d021c344 AK |
1754 | goto out; |
1755 | } | |
1756 | ||
1757 | /* Send data only if both sides are not shutdown in the direction. */ | |
1758 | if (sk->sk_shutdown & SEND_SHUTDOWN || | |
1759 | vsk->peer_shutdown & RCV_SHUTDOWN) { | |
1760 | err = -EPIPE; | |
1761 | goto out; | |
1762 | } | |
1763 | ||
c0cfa2d8 | 1764 | if (!transport || sk->sk_state != TCP_ESTABLISHED || |
d021c344 AK |
1765 | !vsock_addr_bound(&vsk->local_addr)) { |
1766 | err = -ENOTCONN; | |
1767 | goto out; | |
1768 | } | |
1769 | ||
1770 | if (!vsock_addr_bound(&vsk->remote_addr)) { | |
1771 | err = -EDESTADDRREQ; | |
1772 | goto out; | |
1773 | } | |
1774 | ||
1775 | /* Wait for room in the produce queue to enqueue our user's data. */ | |
1776 | timeout = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); | |
1777 | ||
1778 | err = transport->notify_send_init(vsk, &send_data); | |
1779 | if (err < 0) | |
1780 | goto out; | |
1781 | ||
d021c344 AK |
1782 | while (total_written < len) { |
1783 | ssize_t written; | |
1784 | ||
499fde66 | 1785 | add_wait_queue(sk_sleep(sk), &wait); |
d021c344 AK |
1786 | while (vsock_stream_has_space(vsk) == 0 && |
1787 | sk->sk_err == 0 && | |
1788 | !(sk->sk_shutdown & SEND_SHUTDOWN) && | |
1789 | !(vsk->peer_shutdown & RCV_SHUTDOWN)) { | |
1790 | ||
1791 | /* Don't wait for non-blocking sockets. */ | |
1792 | if (timeout == 0) { | |
1793 | err = -EAGAIN; | |
499fde66 | 1794 | remove_wait_queue(sk_sleep(sk), &wait); |
f7f9b5e7 | 1795 | goto out_err; |
d021c344 AK |
1796 | } |
1797 | ||
1798 | err = transport->notify_send_pre_block(vsk, &send_data); | |
f7f9b5e7 | 1799 | if (err < 0) { |
499fde66 | 1800 | remove_wait_queue(sk_sleep(sk), &wait); |
f7f9b5e7 CI |
1801 | goto out_err; |
1802 | } | |
d021c344 AK |
1803 | |
1804 | release_sock(sk); | |
499fde66 | 1805 | timeout = wait_woken(&wait, TASK_INTERRUPTIBLE, timeout); |
d021c344 AK |
1806 | lock_sock(sk); |
1807 | if (signal_pending(current)) { | |
1808 | err = sock_intr_errno(timeout); | |
499fde66 | 1809 | remove_wait_queue(sk_sleep(sk), &wait); |
f7f9b5e7 | 1810 | goto out_err; |
d021c344 AK |
1811 | } else if (timeout == 0) { |
1812 | err = -EAGAIN; | |
499fde66 | 1813 | remove_wait_queue(sk_sleep(sk), &wait); |
f7f9b5e7 | 1814 | goto out_err; |
d021c344 | 1815 | } |
d021c344 | 1816 | } |
499fde66 | 1817 | remove_wait_queue(sk_sleep(sk), &wait); |
d021c344 AK |
1818 | |
1819 | /* These checks occur both as part of and after the loop | |
1820 | * conditional since we need to check before and after | |
1821 | * sleeping. | |
1822 | */ | |
1823 | if (sk->sk_err) { | |
1824 | err = -sk->sk_err; | |
f7f9b5e7 | 1825 | goto out_err; |
d021c344 AK |
1826 | } else if ((sk->sk_shutdown & SEND_SHUTDOWN) || |
1827 | (vsk->peer_shutdown & RCV_SHUTDOWN)) { | |
1828 | err = -EPIPE; | |
f7f9b5e7 | 1829 | goto out_err; |
d021c344 AK |
1830 | } |
1831 | ||
1832 | err = transport->notify_send_pre_enqueue(vsk, &send_data); | |
1833 | if (err < 0) | |
f7f9b5e7 | 1834 | goto out_err; |
d021c344 AK |
1835 | |
1836 | /* Note that enqueue will only write as many bytes as are free | |
1837 | * in the produce queue, so we don't need to ensure len is | |
1838 | * smaller than the queue size. It is the caller's | |
1839 | * responsibility to check how many bytes we were able to send. | |
1840 | */ | |
1841 | ||
fbe70c48 AK |
1842 | if (sk->sk_type == SOCK_SEQPACKET) { |
1843 | written = transport->seqpacket_enqueue(vsk, | |
1844 | msg, len - total_written); | |
1845 | } else { | |
1846 | written = transport->stream_enqueue(vsk, | |
1847 | msg, len - total_written); | |
1848 | } | |
d021c344 AK |
1849 | if (written < 0) { |
1850 | err = -ENOMEM; | |
f7f9b5e7 | 1851 | goto out_err; |
d021c344 AK |
1852 | } |
1853 | ||
1854 | total_written += written; | |
1855 | ||
1856 | err = transport->notify_send_post_enqueue( | |
1857 | vsk, written, &send_data); | |
1858 | if (err < 0) | |
f7f9b5e7 | 1859 | goto out_err; |
d021c344 AK |
1860 | |
1861 | } | |
1862 | ||
f7f9b5e7 | 1863 | out_err: |
fbe70c48 AK |
1864 | if (total_written > 0) { |
1865 | /* Return number of written bytes only if: | |
1866 | * 1) SOCK_STREAM socket. | |
1867 | * 2) SOCK_SEQPACKET socket when whole buffer is sent. | |
1868 | */ | |
1869 | if (sk->sk_type == SOCK_STREAM || total_written == len) | |
1870 | err = total_written; | |
1871 | } | |
d021c344 AK |
1872 | out: |
1873 | release_sock(sk); | |
1874 | return err; | |
1875 | } | |
1876 | ||
0de5b2e6 SG |
1877 | static int vsock_connectible_wait_data(struct sock *sk, |
1878 | struct wait_queue_entry *wait, | |
1879 | long timeout, | |
1880 | struct vsock_transport_recv_notify_data *recv_data, | |
1881 | size_t target) | |
b3f7fd54 AK |
1882 | { |
1883 | const struct vsock_transport *transport; | |
1884 | struct vsock_sock *vsk; | |
1885 | s64 data; | |
1886 | int err; | |
1887 | ||
1888 | vsk = vsock_sk(sk); | |
1889 | err = 0; | |
1890 | transport = vsk->transport; | |
1891 | ||
cc97141a | 1892 | while ((data = vsock_connectible_has_data(vsk)) == 0) { |
b3f7fd54 AK |
1893 | prepare_to_wait(sk_sleep(sk), wait, TASK_INTERRUPTIBLE); |
1894 | ||
1895 | if (sk->sk_err != 0 || | |
1896 | (sk->sk_shutdown & RCV_SHUTDOWN) || | |
1897 | (vsk->peer_shutdown & SEND_SHUTDOWN)) { | |
1898 | break; | |
1899 | } | |
1900 | ||
1901 | /* Don't wait for non-blocking sockets. */ | |
1902 | if (timeout == 0) { | |
1903 | err = -EAGAIN; | |
1904 | break; | |
1905 | } | |
1906 | ||
1907 | if (recv_data) { | |
1908 | err = transport->notify_recv_pre_block(vsk, target, recv_data); | |
1909 | if (err < 0) | |
1910 | break; | |
1911 | } | |
1912 | ||
1913 | release_sock(sk); | |
1914 | timeout = schedule_timeout(timeout); | |
1915 | lock_sock(sk); | |
1916 | ||
1917 | if (signal_pending(current)) { | |
1918 | err = sock_intr_errno(timeout); | |
1919 | break; | |
1920 | } else if (timeout == 0) { | |
1921 | err = -EAGAIN; | |
1922 | break; | |
1923 | } | |
1924 | } | |
1925 | ||
1926 | finish_wait(sk_sleep(sk), wait); | |
1927 | ||
1928 | if (err) | |
1929 | return err; | |
1930 | ||
1931 | /* Internal transport error when checking for available | |
1932 | * data. XXX This should be changed to a connection | |
1933 | * reset in a later change. | |
1934 | */ | |
1935 | if (data < 0) | |
1936 | return -ENOMEM; | |
1937 | ||
1938 | return data; | |
1939 | } | |
1940 | ||
19c1b90e AK |
1941 | static int __vsock_stream_recvmsg(struct sock *sk, struct msghdr *msg, |
1942 | size_t len, int flags) | |
d021c344 | 1943 | { |
19c1b90e | 1944 | struct vsock_transport_recv_notify_data recv_data; |
fe502c4a | 1945 | const struct vsock_transport *transport; |
19c1b90e | 1946 | struct vsock_sock *vsk; |
d021c344 | 1947 | ssize_t copied; |
19c1b90e | 1948 | size_t target; |
d021c344 | 1949 | long timeout; |
19c1b90e | 1950 | int err; |
d021c344 AK |
1951 | |
1952 | DEFINE_WAIT(wait); | |
1953 | ||
d021c344 | 1954 | vsk = vsock_sk(sk); |
c518adaf AP |
1955 | transport = vsk->transport; |
1956 | ||
d021c344 AK |
1957 | /* We must not copy less than target bytes into the user's buffer |
1958 | * before returning successfully, so we wait for the consume queue to | |
1959 | * have that much data to consume before dequeueing. Note that this | |
1960 | * makes it impossible to handle cases where target is greater than the | |
1961 | * queue size. | |
1962 | */ | |
1963 | target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); | |
1964 | if (target >= transport->stream_rcvhiwat(vsk)) { | |
1965 | err = -ENOMEM; | |
1966 | goto out; | |
1967 | } | |
1968 | timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); | |
1969 | copied = 0; | |
1970 | ||
1971 | err = transport->notify_recv_init(vsk, target, &recv_data); | |
1972 | if (err < 0) | |
1973 | goto out; | |
1974 | ||
d021c344 AK |
1975 | |
1976 | while (1) { | |
b3f7fd54 | 1977 | ssize_t read; |
f7f9b5e7 | 1978 | |
0de5b2e6 SG |
1979 | err = vsock_connectible_wait_data(sk, &wait, timeout, |
1980 | &recv_data, target); | |
b3f7fd54 AK |
1981 | if (err <= 0) |
1982 | break; | |
f7f9b5e7 | 1983 | |
b3f7fd54 AK |
1984 | err = transport->notify_recv_pre_dequeue(vsk, target, |
1985 | &recv_data); | |
1986 | if (err < 0) | |
1987 | break; | |
d021c344 | 1988 | |
b3f7fd54 AK |
1989 | read = transport->stream_dequeue(vsk, msg, len - copied, flags); |
1990 | if (read < 0) { | |
1991 | err = -ENOMEM; | |
1992 | break; | |
1993 | } | |
d021c344 | 1994 | |
b3f7fd54 | 1995 | copied += read; |
d021c344 | 1996 | |
b3f7fd54 AK |
1997 | err = transport->notify_recv_post_dequeue(vsk, target, read, |
1998 | !(flags & MSG_PEEK), &recv_data); | |
1999 | if (err < 0) | |
2000 | goto out; | |
d021c344 | 2001 | |
b3f7fd54 AK |
2002 | if (read >= target || flags & MSG_PEEK) |
2003 | break; | |
d021c344 | 2004 | |
b3f7fd54 | 2005 | target -= read; |
d021c344 AK |
2006 | } |
2007 | ||
2008 | if (sk->sk_err) | |
2009 | err = -sk->sk_err; | |
2010 | else if (sk->sk_shutdown & RCV_SHUTDOWN) | |
2011 | err = 0; | |
2012 | ||
dedc58e0 | 2013 | if (copied > 0) |
d021c344 | 2014 | err = copied; |
d021c344 | 2015 | |
19c1b90e AK |
2016 | out: |
2017 | return err; | |
2018 | } | |
2019 | ||
9942c192 AK |
2020 | static int __vsock_seqpacket_recvmsg(struct sock *sk, struct msghdr *msg, |
2021 | size_t len, int flags) | |
2022 | { | |
2023 | const struct vsock_transport *transport; | |
2024 | struct vsock_sock *vsk; | |
8fc92b7c | 2025 | ssize_t msg_len; |
9942c192 AK |
2026 | long timeout; |
2027 | int err = 0; | |
2028 | DEFINE_WAIT(wait); | |
2029 | ||
2030 | vsk = vsock_sk(sk); | |
2031 | transport = vsk->transport; | |
2032 | ||
2033 | timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); | |
2034 | ||
0de5b2e6 | 2035 | err = vsock_connectible_wait_data(sk, &wait, timeout, NULL, 0); |
9942c192 AK |
2036 | if (err <= 0) |
2037 | goto out; | |
2038 | ||
8fc92b7c | 2039 | msg_len = transport->seqpacket_dequeue(vsk, msg, flags); |
9942c192 | 2040 | |
8fc92b7c | 2041 | if (msg_len < 0) { |
9942c192 AK |
2042 | err = -ENOMEM; |
2043 | goto out; | |
2044 | } | |
2045 | ||
2046 | if (sk->sk_err) { | |
2047 | err = -sk->sk_err; | |
2048 | } else if (sk->sk_shutdown & RCV_SHUTDOWN) { | |
2049 | err = 0; | |
2050 | } else { | |
2051 | /* User sets MSG_TRUNC, so return real length of | |
2052 | * packet. | |
2053 | */ | |
2054 | if (flags & MSG_TRUNC) | |
8fc92b7c | 2055 | err = msg_len; |
9942c192 AK |
2056 | else |
2057 | err = len - msg_data_left(msg); | |
2058 | ||
2059 | /* Always set MSG_TRUNC if real length of packet is | |
2060 | * bigger than user's buffer. | |
2061 | */ | |
8fc92b7c | 2062 | if (msg_len > len) |
9942c192 AK |
2063 | msg->msg_flags |= MSG_TRUNC; |
2064 | } | |
2065 | ||
2066 | out: | |
2067 | return err; | |
2068 | } | |
2069 | ||
19c1b90e AK |
2070 | static int |
2071 | vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, | |
2072 | int flags) | |
2073 | { | |
2074 | struct sock *sk; | |
2075 | struct vsock_sock *vsk; | |
2076 | const struct vsock_transport *transport; | |
2077 | int err; | |
2078 | ||
2079 | DEFINE_WAIT(wait); | |
2080 | ||
2081 | sk = sock->sk; | |
2082 | vsk = vsock_sk(sk); | |
2083 | err = 0; | |
2084 | ||
2085 | lock_sock(sk); | |
2086 | ||
2087 | transport = vsk->transport; | |
2088 | ||
2089 | if (!transport || sk->sk_state != TCP_ESTABLISHED) { | |
2090 | /* Recvmsg is supposed to return 0 if a peer performs an | |
2091 | * orderly shutdown. Differentiate between that case and when a | |
2092 | * peer has not connected or a local shutdown occurred with the | |
2093 | * SOCK_DONE flag. | |
2094 | */ | |
2095 | if (sock_flag(sk, SOCK_DONE)) | |
2096 | err = 0; | |
2097 | else | |
2098 | err = -ENOTCONN; | |
2099 | ||
2100 | goto out; | |
2101 | } | |
2102 | ||
2103 | if (flags & MSG_OOB) { | |
2104 | err = -EOPNOTSUPP; | |
2105 | goto out; | |
2106 | } | |
2107 | ||
2108 | /* We don't check peer_shutdown flag here since peer may actually shut | |
2109 | * down, but there can be data in the queue that a local socket can | |
2110 | * receive. | |
2111 | */ | |
2112 | if (sk->sk_shutdown & RCV_SHUTDOWN) { | |
2113 | err = 0; | |
2114 | goto out; | |
2115 | } | |
2116 | ||
2117 | /* It is valid on Linux to pass in a zero-length receive buffer. This | |
2118 | * is not an error. We may as well bail out now. | |
2119 | */ | |
2120 | if (!len) { | |
2121 | err = 0; | |
2122 | goto out; | |
2123 | } | |
2124 | ||
9942c192 AK |
2125 | if (sk->sk_type == SOCK_STREAM) |
2126 | err = __vsock_stream_recvmsg(sk, msg, len, flags); | |
2127 | else | |
2128 | err = __vsock_seqpacket_recvmsg(sk, msg, len, flags); | |
19c1b90e | 2129 | |
d021c344 AK |
2130 | out: |
2131 | release_sock(sk); | |
2132 | return err; | |
2133 | } | |
2134 | ||
2135 | static const struct proto_ops vsock_stream_ops = { | |
2136 | .family = PF_VSOCK, | |
2137 | .owner = THIS_MODULE, | |
2138 | .release = vsock_release, | |
2139 | .bind = vsock_bind, | |
a9e29e55 | 2140 | .connect = vsock_connect, |
d021c344 AK |
2141 | .socketpair = sock_no_socketpair, |
2142 | .accept = vsock_accept, | |
2143 | .getname = vsock_getname, | |
a11e1d43 | 2144 | .poll = vsock_poll, |
d021c344 AK |
2145 | .ioctl = sock_no_ioctl, |
2146 | .listen = vsock_listen, | |
2147 | .shutdown = vsock_shutdown, | |
a9e29e55 AK |
2148 | .setsockopt = vsock_connectible_setsockopt, |
2149 | .getsockopt = vsock_connectible_getsockopt, | |
2150 | .sendmsg = vsock_connectible_sendmsg, | |
2151 | .recvmsg = vsock_connectible_recvmsg, | |
d021c344 AK |
2152 | .mmap = sock_no_mmap, |
2153 | .sendpage = sock_no_sendpage, | |
2154 | }; | |
2155 | ||
0798e78b AK |
2156 | static const struct proto_ops vsock_seqpacket_ops = { |
2157 | .family = PF_VSOCK, | |
2158 | .owner = THIS_MODULE, | |
2159 | .release = vsock_release, | |
2160 | .bind = vsock_bind, | |
2161 | .connect = vsock_connect, | |
2162 | .socketpair = sock_no_socketpair, | |
2163 | .accept = vsock_accept, | |
2164 | .getname = vsock_getname, | |
2165 | .poll = vsock_poll, | |
2166 | .ioctl = sock_no_ioctl, | |
2167 | .listen = vsock_listen, | |
2168 | .shutdown = vsock_shutdown, | |
2169 | .setsockopt = vsock_connectible_setsockopt, | |
2170 | .getsockopt = vsock_connectible_getsockopt, | |
2171 | .sendmsg = vsock_connectible_sendmsg, | |
2172 | .recvmsg = vsock_connectible_recvmsg, | |
2173 | .mmap = sock_no_mmap, | |
2174 | .sendpage = sock_no_sendpage, | |
2175 | }; | |
2176 | ||
d021c344 AK |
2177 | static int vsock_create(struct net *net, struct socket *sock, |
2178 | int protocol, int kern) | |
2179 | { | |
c0cfa2d8 | 2180 | struct vsock_sock *vsk; |
55f3e149 | 2181 | struct sock *sk; |
c0cfa2d8 | 2182 | int ret; |
55f3e149 | 2183 | |
d021c344 AK |
2184 | if (!sock) |
2185 | return -EINVAL; | |
2186 | ||
6cf1c5fc | 2187 | if (protocol && protocol != PF_VSOCK) |
d021c344 AK |
2188 | return -EPROTONOSUPPORT; |
2189 | ||
2190 | switch (sock->type) { | |
2191 | case SOCK_DGRAM: | |
2192 | sock->ops = &vsock_dgram_ops; | |
2193 | break; | |
2194 | case SOCK_STREAM: | |
2195 | sock->ops = &vsock_stream_ops; | |
2196 | break; | |
0798e78b AK |
2197 | case SOCK_SEQPACKET: |
2198 | sock->ops = &vsock_seqpacket_ops; | |
2199 | break; | |
d021c344 AK |
2200 | default: |
2201 | return -ESOCKTNOSUPPORT; | |
2202 | } | |
2203 | ||
2204 | sock->state = SS_UNCONNECTED; | |
2205 | ||
55f3e149 SG |
2206 | sk = __vsock_create(net, sock, NULL, GFP_KERNEL, 0, kern); |
2207 | if (!sk) | |
2208 | return -ENOMEM; | |
2209 | ||
c0cfa2d8 SG |
2210 | vsk = vsock_sk(sk); |
2211 | ||
2212 | if (sock->type == SOCK_DGRAM) { | |
2213 | ret = vsock_assign_transport(vsk, NULL); | |
2214 | if (ret < 0) { | |
2215 | sock_put(sk); | |
2216 | return ret; | |
2217 | } | |
2218 | } | |
2219 | ||
2220 | vsock_insert_unbound(vsk); | |
55f3e149 SG |
2221 | |
2222 | return 0; | |
d021c344 AK |
2223 | } |
2224 | ||
2225 | static const struct net_proto_family vsock_family_ops = { | |
2226 | .family = AF_VSOCK, | |
2227 | .create = vsock_create, | |
2228 | .owner = THIS_MODULE, | |
2229 | }; | |
2230 | ||
2231 | static long vsock_dev_do_ioctl(struct file *filp, | |
2232 | unsigned int cmd, void __user *ptr) | |
2233 | { | |
2234 | u32 __user *p = ptr; | |
c0cfa2d8 | 2235 | u32 cid = VMADDR_CID_ANY; |
d021c344 AK |
2236 | int retval = 0; |
2237 | ||
2238 | switch (cmd) { | |
2239 | case IOCTL_VM_SOCKETS_GET_LOCAL_CID: | |
c0cfa2d8 SG |
2240 | /* To be compatible with the VMCI behavior, we prioritize the |
2241 | * guest CID instead of well-know host CID (VMADDR_CID_HOST). | |
2242 | */ | |
2243 | if (transport_g2h) | |
2244 | cid = transport_g2h->get_local_cid(); | |
2245 | else if (transport_h2g) | |
2246 | cid = transport_h2g->get_local_cid(); | |
2247 | ||
2248 | if (put_user(cid, p) != 0) | |
d021c344 AK |
2249 | retval = -EFAULT; |
2250 | break; | |
2251 | ||
2252 | default: | |
c3e448cd | 2253 | retval = -ENOIOCTLCMD; |
d021c344 AK |
2254 | } |
2255 | ||
2256 | return retval; | |
2257 | } | |
2258 | ||
2259 | static long vsock_dev_ioctl(struct file *filp, | |
2260 | unsigned int cmd, unsigned long arg) | |
2261 | { | |
2262 | return vsock_dev_do_ioctl(filp, cmd, (void __user *)arg); | |
2263 | } | |
2264 | ||
2265 | #ifdef CONFIG_COMPAT | |
2266 | static long vsock_dev_compat_ioctl(struct file *filp, | |
2267 | unsigned int cmd, unsigned long arg) | |
2268 | { | |
2269 | return vsock_dev_do_ioctl(filp, cmd, compat_ptr(arg)); | |
2270 | } | |
2271 | #endif | |
2272 | ||
2273 | static const struct file_operations vsock_device_ops = { | |
2274 | .owner = THIS_MODULE, | |
2275 | .unlocked_ioctl = vsock_dev_ioctl, | |
2276 | #ifdef CONFIG_COMPAT | |
2277 | .compat_ioctl = vsock_dev_compat_ioctl, | |
2278 | #endif | |
2279 | .open = nonseekable_open, | |
2280 | }; | |
2281 | ||
2282 | static struct miscdevice vsock_device = { | |
2283 | .name = "vsock", | |
d021c344 AK |
2284 | .fops = &vsock_device_ops, |
2285 | }; | |
2286 | ||
c0cfa2d8 | 2287 | static int __init vsock_init(void) |
d021c344 | 2288 | { |
c0cfa2d8 | 2289 | int err = 0; |
2c4a336e | 2290 | |
c0cfa2d8 | 2291 | vsock_init_tables(); |
d021c344 | 2292 | |
c0cfa2d8 | 2293 | vsock_proto.owner = THIS_MODULE; |
6ad0b2f7 | 2294 | vsock_device.minor = MISC_DYNAMIC_MINOR; |
d021c344 AK |
2295 | err = misc_register(&vsock_device); |
2296 | if (err) { | |
2297 | pr_err("Failed to register misc device\n"); | |
f6a835bb | 2298 | goto err_reset_transport; |
d021c344 AK |
2299 | } |
2300 | ||
2301 | err = proto_register(&vsock_proto, 1); /* we want our slab */ | |
2302 | if (err) { | |
2303 | pr_err("Cannot register vsock protocol\n"); | |
f6a835bb | 2304 | goto err_deregister_misc; |
d021c344 AK |
2305 | } |
2306 | ||
2307 | err = sock_register(&vsock_family_ops); | |
2308 | if (err) { | |
2309 | pr_err("could not register af_vsock (%d) address family: %d\n", | |
2310 | AF_VSOCK, err); | |
2311 | goto err_unregister_proto; | |
2312 | } | |
2313 | ||
2314 | return 0; | |
2315 | ||
2316 | err_unregister_proto: | |
2317 | proto_unregister(&vsock_proto); | |
f6a835bb | 2318 | err_deregister_misc: |
d021c344 | 2319 | misc_deregister(&vsock_device); |
f6a835bb | 2320 | err_reset_transport: |
2c4a336e | 2321 | return err; |
d021c344 | 2322 | } |
d021c344 | 2323 | |
c0cfa2d8 | 2324 | static void __exit vsock_exit(void) |
d021c344 | 2325 | { |
d021c344 AK |
2326 | misc_deregister(&vsock_device); |
2327 | sock_unregister(AF_VSOCK); | |
2328 | proto_unregister(&vsock_proto); | |
d021c344 | 2329 | } |
d021c344 | 2330 | |
daabfbca | 2331 | const struct vsock_transport *vsock_core_get_transport(struct vsock_sock *vsk) |
0b01aeb3 | 2332 | { |
daabfbca | 2333 | return vsk->transport; |
0b01aeb3 SH |
2334 | } |
2335 | EXPORT_SYMBOL_GPL(vsock_core_get_transport); | |
2336 | ||
c0cfa2d8 SG |
2337 | int vsock_core_register(const struct vsock_transport *t, int features) |
2338 | { | |
0e121905 | 2339 | const struct vsock_transport *t_h2g, *t_g2h, *t_dgram, *t_local; |
c0cfa2d8 SG |
2340 | int err = mutex_lock_interruptible(&vsock_register_mutex); |
2341 | ||
2342 | if (err) | |
2343 | return err; | |
2344 | ||
2345 | t_h2g = transport_h2g; | |
2346 | t_g2h = transport_g2h; | |
2347 | t_dgram = transport_dgram; | |
0e121905 | 2348 | t_local = transport_local; |
c0cfa2d8 SG |
2349 | |
2350 | if (features & VSOCK_TRANSPORT_F_H2G) { | |
2351 | if (t_h2g) { | |
2352 | err = -EBUSY; | |
2353 | goto err_busy; | |
2354 | } | |
2355 | t_h2g = t; | |
2356 | } | |
2357 | ||
2358 | if (features & VSOCK_TRANSPORT_F_G2H) { | |
2359 | if (t_g2h) { | |
2360 | err = -EBUSY; | |
2361 | goto err_busy; | |
2362 | } | |
2363 | t_g2h = t; | |
2364 | } | |
2365 | ||
2366 | if (features & VSOCK_TRANSPORT_F_DGRAM) { | |
2367 | if (t_dgram) { | |
2368 | err = -EBUSY; | |
2369 | goto err_busy; | |
2370 | } | |
2371 | t_dgram = t; | |
2372 | } | |
2373 | ||
0e121905 SG |
2374 | if (features & VSOCK_TRANSPORT_F_LOCAL) { |
2375 | if (t_local) { | |
2376 | err = -EBUSY; | |
2377 | goto err_busy; | |
2378 | } | |
2379 | t_local = t; | |
2380 | } | |
2381 | ||
c0cfa2d8 SG |
2382 | transport_h2g = t_h2g; |
2383 | transport_g2h = t_g2h; | |
2384 | transport_dgram = t_dgram; | |
0e121905 | 2385 | transport_local = t_local; |
c0cfa2d8 SG |
2386 | |
2387 | err_busy: | |
2388 | mutex_unlock(&vsock_register_mutex); | |
2389 | return err; | |
2390 | } | |
2391 | EXPORT_SYMBOL_GPL(vsock_core_register); | |
2392 | ||
2393 | void vsock_core_unregister(const struct vsock_transport *t) | |
05e489b1 | 2394 | { |
c0cfa2d8 SG |
2395 | mutex_lock(&vsock_register_mutex); |
2396 | ||
2397 | if (transport_h2g == t) | |
2398 | transport_h2g = NULL; | |
2399 | ||
2400 | if (transport_g2h == t) | |
2401 | transport_g2h = NULL; | |
2402 | ||
2403 | if (transport_dgram == t) | |
2404 | transport_dgram = NULL; | |
2405 | ||
0e121905 SG |
2406 | if (transport_local == t) |
2407 | transport_local = NULL; | |
2408 | ||
c0cfa2d8 | 2409 | mutex_unlock(&vsock_register_mutex); |
05e489b1 | 2410 | } |
c0cfa2d8 | 2411 | EXPORT_SYMBOL_GPL(vsock_core_unregister); |
05e489b1 | 2412 | |
c0cfa2d8 | 2413 | module_init(vsock_init); |
05e489b1 | 2414 | module_exit(vsock_exit); |
c1eef220 | 2415 | |
d021c344 AK |
2416 | MODULE_AUTHOR("VMware, Inc."); |
2417 | MODULE_DESCRIPTION("VMware Virtual Socket Family"); | |
1190cfdb | 2418 | MODULE_VERSION("1.0.2.0-k"); |
d021c344 | 2419 | MODULE_LICENSE("GPL v2"); |