1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96#include <linux/types.h>
97#include <linux/bitops.h>
98#include <linux/cred.h>
99#include <linux/init.h>
100#include <linux/io.h>
101#include <linux/kernel.h>
102#include <linux/kmod.h>
103#include <linux/list.h>
104#include <linux/miscdevice.h>
105#include <linux/module.h>
106#include <linux/mutex.h>
107#include <linux/net.h>
108#include <linux/poll.h>
109#include <linux/skbuff.h>
110#include <linux/smp.h>
111#include <linux/socket.h>
112#include <linux/stddef.h>
113#include <linux/unistd.h>
114#include <linux/wait.h>
115#include <linux/workqueue.h>
116#include <net/sock.h>
117#include <net/af_vsock.h>
118
119static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr);
120static void vsock_sk_destruct(struct sock *sk);
121static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
122
123
124static struct proto vsock_proto = {
125 .name = "AF_VSOCK",
126 .owner = THIS_MODULE,
127 .obj_size = sizeof(struct vsock_sock),
128};
129
130
131
132
133#define VSOCK_DEFAULT_CONNECT_TIMEOUT (2 * HZ)
134
135static const struct vsock_transport *transport;
136static DEFINE_MUTEX(vsock_register_mutex);
137
138
139
140
141
142int vm_sockets_get_local_cid(void)
143{
144 return transport->get_local_cid();
145}
146EXPORT_SYMBOL_GPL(vm_sockets_get_local_cid);
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164#define MAX_PORT_RETRIES 24
165
166#define VSOCK_HASH(addr) ((addr)->svm_port % VSOCK_HASH_SIZE)
167#define vsock_bound_sockets(addr) (&vsock_bind_table[VSOCK_HASH(addr)])
168#define vsock_unbound_sockets (&vsock_bind_table[VSOCK_HASH_SIZE])
169
170
171#define VSOCK_CONN_HASH(src, dst) \
172 (((src)->svm_cid ^ (dst)->svm_port) % VSOCK_HASH_SIZE)
173#define vsock_connected_sockets(src, dst) \
174 (&vsock_connected_table[VSOCK_CONN_HASH(src, dst)])
175#define vsock_connected_sockets_vsk(vsk) \
176 vsock_connected_sockets(&(vsk)->remote_addr, &(vsk)->local_addr)
177
178struct list_head vsock_bind_table[VSOCK_HASH_SIZE + 1];
179EXPORT_SYMBOL_GPL(vsock_bind_table);
180struct list_head vsock_connected_table[VSOCK_HASH_SIZE];
181EXPORT_SYMBOL_GPL(vsock_connected_table);
182DEFINE_SPINLOCK(vsock_table_lock);
183EXPORT_SYMBOL_GPL(vsock_table_lock);
184
185
186static int vsock_auto_bind(struct vsock_sock *vsk)
187{
188 struct sock *sk = sk_vsock(vsk);
189 struct sockaddr_vm local_addr;
190
191 if (vsock_addr_bound(&vsk->local_addr))
192 return 0;
193 vsock_addr_init(&local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
194 return __vsock_bind(sk, &local_addr);
195}
196
197static int __init vsock_init_tables(void)
198{
199 int i;
200
201 for (i = 0; i < ARRAY_SIZE(vsock_bind_table); i++)
202 INIT_LIST_HEAD(&vsock_bind_table[i]);
203
204 for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++)
205 INIT_LIST_HEAD(&vsock_connected_table[i]);
206 return 0;
207}
208
209static void __vsock_insert_bound(struct list_head *list,
210 struct vsock_sock *vsk)
211{
212 sock_hold(&vsk->sk);
213 list_add(&vsk->bound_table, list);
214}
215
216static void __vsock_insert_connected(struct list_head *list,
217 struct vsock_sock *vsk)
218{
219 sock_hold(&vsk->sk);
220 list_add(&vsk->connected_table, list);
221}
222
223static void __vsock_remove_bound(struct vsock_sock *vsk)
224{
225 list_del_init(&vsk->bound_table);
226 sock_put(&vsk->sk);
227}
228
229static void __vsock_remove_connected(struct vsock_sock *vsk)
230{
231 list_del_init(&vsk->connected_table);
232 sock_put(&vsk->sk);
233}
234
235static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
236{
237 struct vsock_sock *vsk;
238
239 list_for_each_entry(vsk, vsock_bound_sockets(addr), bound_table)
240 if (addr->svm_port == vsk->local_addr.svm_port)
241 return sk_vsock(vsk);
242
243 return NULL;
244}
245
246static 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) {
253 if (vsock_addr_equals_addr(src, &vsk->remote_addr) &&
254 dst->svm_port == vsk->local_addr.svm_port) {
255 return sk_vsock(vsk);
256 }
257 }
258
259 return NULL;
260}
261
262static 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
269void 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}
278EXPORT_SYMBOL_GPL(vsock_insert_connected);
279
280void vsock_remove_bound(struct vsock_sock *vsk)
281{
282 spin_lock_bh(&vsock_table_lock);
283 __vsock_remove_bound(vsk);
284 spin_unlock_bh(&vsock_table_lock);
285}
286EXPORT_SYMBOL_GPL(vsock_remove_bound);
287
288void vsock_remove_connected(struct vsock_sock *vsk)
289{
290 spin_lock_bh(&vsock_table_lock);
291 __vsock_remove_connected(vsk);
292 spin_unlock_bh(&vsock_table_lock);
293}
294EXPORT_SYMBOL_GPL(vsock_remove_connected);
295
296struct sock *vsock_find_bound_socket(struct sockaddr_vm *addr)
297{
298 struct sock *sk;
299
300 spin_lock_bh(&vsock_table_lock);
301 sk = __vsock_find_bound_socket(addr);
302 if (sk)
303 sock_hold(sk);
304
305 spin_unlock_bh(&vsock_table_lock);
306
307 return sk;
308}
309EXPORT_SYMBOL_GPL(vsock_find_bound_socket);
310
311struct sock *vsock_find_connected_socket(struct sockaddr_vm *src,
312 struct sockaddr_vm *dst)
313{
314 struct sock *sk;
315
316 spin_lock_bh(&vsock_table_lock);
317 sk = __vsock_find_connected_socket(src, dst);
318 if (sk)
319 sock_hold(sk);
320
321 spin_unlock_bh(&vsock_table_lock);
322
323 return sk;
324}
325EXPORT_SYMBOL_GPL(vsock_find_connected_socket);
326
327static bool vsock_in_bound_table(struct vsock_sock *vsk)
328{
329 bool ret;
330
331 spin_lock_bh(&vsock_table_lock);
332 ret = __vsock_in_bound_table(vsk);
333 spin_unlock_bh(&vsock_table_lock);
334
335 return ret;
336}
337
338static bool vsock_in_connected_table(struct vsock_sock *vsk)
339{
340 bool ret;
341
342 spin_lock_bh(&vsock_table_lock);
343 ret = __vsock_in_connected_table(vsk);
344 spin_unlock_bh(&vsock_table_lock);
345
346 return ret;
347}
348
349void vsock_remove_sock(struct vsock_sock *vsk)
350{
351 if (vsock_in_bound_table(vsk))
352 vsock_remove_bound(vsk);
353
354 if (vsock_in_connected_table(vsk))
355 vsock_remove_connected(vsk);
356}
357EXPORT_SYMBOL_GPL(vsock_remove_sock);
358
359void vsock_for_each_connected_socket(void (*fn)(struct sock *sk))
360{
361 int i;
362
363 spin_lock_bh(&vsock_table_lock);
364
365 for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++) {
366 struct vsock_sock *vsk;
367 list_for_each_entry(vsk, &vsock_connected_table[i],
368 connected_table)
369 fn(sk_vsock(vsk));
370 }
371
372 spin_unlock_bh(&vsock_table_lock);
373}
374EXPORT_SYMBOL_GPL(vsock_for_each_connected_socket);
375
376void vsock_add_pending(struct sock *listener, struct sock *pending)
377{
378 struct vsock_sock *vlistener;
379 struct vsock_sock *vpending;
380
381 vlistener = vsock_sk(listener);
382 vpending = vsock_sk(pending);
383
384 sock_hold(pending);
385 sock_hold(listener);
386 list_add_tail(&vpending->pending_links, &vlistener->pending_links);
387}
388EXPORT_SYMBOL_GPL(vsock_add_pending);
389
390void vsock_remove_pending(struct sock *listener, struct sock *pending)
391{
392 struct vsock_sock *vpending = vsock_sk(pending);
393
394 list_del_init(&vpending->pending_links);
395 sock_put(listener);
396 sock_put(pending);
397}
398EXPORT_SYMBOL_GPL(vsock_remove_pending);
399
400void vsock_enqueue_accept(struct sock *listener, struct sock *connected)
401{
402 struct vsock_sock *vlistener;
403 struct vsock_sock *vconnected;
404
405 vlistener = vsock_sk(listener);
406 vconnected = vsock_sk(connected);
407
408 sock_hold(connected);
409 sock_hold(listener);
410 list_add_tail(&vconnected->accept_queue, &vlistener->accept_queue);
411}
412EXPORT_SYMBOL_GPL(vsock_enqueue_accept);
413
414static struct sock *vsock_dequeue_accept(struct sock *listener)
415{
416 struct vsock_sock *vlistener;
417 struct vsock_sock *vconnected;
418
419 vlistener = vsock_sk(listener);
420
421 if (list_empty(&vlistener->accept_queue))
422 return NULL;
423
424 vconnected = list_entry(vlistener->accept_queue.next,
425 struct vsock_sock, accept_queue);
426
427 list_del_init(&vconnected->accept_queue);
428 sock_put(listener);
429
430
431
432
433 return sk_vsock(vconnected);
434}
435
436static bool vsock_is_accept_queue_empty(struct sock *sk)
437{
438 struct vsock_sock *vsk = vsock_sk(sk);
439 return list_empty(&vsk->accept_queue);
440}
441
442static bool vsock_is_pending(struct sock *sk)
443{
444 struct vsock_sock *vsk = vsock_sk(sk);
445 return !list_empty(&vsk->pending_links);
446}
447
448static int vsock_send_shutdown(struct sock *sk, int mode)
449{
450 return transport->shutdown(vsock_sk(sk), mode);
451}
452
453void vsock_pending_work(struct work_struct *work)
454{
455 struct sock *sk;
456 struct sock *listener;
457 struct vsock_sock *vsk;
458 bool cleanup;
459
460 vsk = container_of(work, struct vsock_sock, dwork.work);
461 sk = sk_vsock(vsk);
462 listener = vsk->listener;
463 cleanup = true;
464
465 lock_sock(listener);
466 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
467
468 if (vsock_is_pending(sk)) {
469 vsock_remove_pending(listener, sk);
470
471 listener->sk_ack_backlog--;
472 } else if (!vsk->rejected) {
473
474
475
476
477
478 cleanup = false;
479 goto out;
480 }
481
482
483
484
485
486 if (vsock_in_connected_table(vsk))
487 vsock_remove_connected(vsk);
488
489 sk->sk_state = TCP_CLOSE;
490
491out:
492 release_sock(sk);
493 release_sock(listener);
494 if (cleanup)
495 sock_put(sk);
496
497 sock_put(sk);
498 sock_put(listener);
499}
500EXPORT_SYMBOL_GPL(vsock_pending_work);
501
502
503
504static int __vsock_bind_stream(struct vsock_sock *vsk,
505 struct sockaddr_vm *addr)
506{
507 static u32 port = LAST_RESERVED_PORT + 1;
508 struct sockaddr_vm new_addr;
509
510 vsock_addr_init(&new_addr, addr->svm_cid, addr->svm_port);
511
512 if (addr->svm_port == VMADDR_PORT_ANY) {
513 bool found = false;
514 unsigned int i;
515
516 for (i = 0; i < MAX_PORT_RETRIES; i++) {
517 if (port <= LAST_RESERVED_PORT)
518 port = LAST_RESERVED_PORT + 1;
519
520 new_addr.svm_port = port++;
521
522 if (!__vsock_find_bound_socket(&new_addr)) {
523 found = true;
524 break;
525 }
526 }
527
528 if (!found)
529 return -EADDRNOTAVAIL;
530 } else {
531
532
533
534 if (addr->svm_port <= LAST_RESERVED_PORT &&
535 !capable(CAP_NET_BIND_SERVICE)) {
536 return -EACCES;
537 }
538
539 if (__vsock_find_bound_socket(&new_addr))
540 return -EADDRINUSE;
541 }
542
543 vsock_addr_init(&vsk->local_addr, new_addr.svm_cid, new_addr.svm_port);
544
545
546
547
548
549 __vsock_remove_bound(vsk);
550 __vsock_insert_bound(vsock_bound_sockets(&vsk->local_addr), vsk);
551
552 return 0;
553}
554
555static int __vsock_bind_dgram(struct vsock_sock *vsk,
556 struct sockaddr_vm *addr)
557{
558 return transport->dgram_bind(vsk, addr);
559}
560
561static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr)
562{
563 struct vsock_sock *vsk = vsock_sk(sk);
564 u32 cid;
565 int retval;
566
567
568 if (vsock_addr_bound(&vsk->local_addr))
569 return -EINVAL;
570
571
572
573
574
575
576 cid = transport->get_local_cid();
577 if (addr->svm_cid != cid && addr->svm_cid != VMADDR_CID_ANY)
578 return -EADDRNOTAVAIL;
579
580 switch (sk->sk_socket->type) {
581 case SOCK_STREAM:
582 spin_lock_bh(&vsock_table_lock);
583 retval = __vsock_bind_stream(vsk, addr);
584 spin_unlock_bh(&vsock_table_lock);
585 break;
586
587 case SOCK_DGRAM:
588 retval = __vsock_bind_dgram(vsk, addr);
589 break;
590
591 default:
592 retval = -EINVAL;
593 break;
594 }
595
596 return retval;
597}
598
599struct sock *__vsock_create(struct net *net,
600 struct socket *sock,
601 struct sock *parent,
602 gfp_t priority,
603 unsigned short type)
604{
605 struct sock *sk;
606 struct vsock_sock *psk;
607 struct vsock_sock *vsk;
608
609 sk = sk_alloc(net, AF_VSOCK, priority, &vsock_proto);
610 if (!sk)
611 return NULL;
612
613 sock_init_data(sock, sk);
614
615
616
617
618
619 if (!sock)
620 sk->sk_type = type;
621
622 vsk = vsock_sk(sk);
623 vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
624 vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
625
626 sk->sk_destruct = vsock_sk_destruct;
627 sk->sk_backlog_rcv = vsock_queue_rcv_skb;
628 sock_reset_flag(sk, SOCK_DONE);
629
630 INIT_LIST_HEAD(&vsk->bound_table);
631 INIT_LIST_HEAD(&vsk->connected_table);
632 vsk->listener = NULL;
633 INIT_LIST_HEAD(&vsk->pending_links);
634 INIT_LIST_HEAD(&vsk->accept_queue);
635 vsk->rejected = false;
636 vsk->sent_request = false;
637 vsk->ignore_connecting_rst = false;
638 vsk->peer_shutdown = 0;
639
640 psk = parent ? vsock_sk(parent) : NULL;
641 if (parent) {
642 vsk->trusted = psk->trusted;
643 vsk->owner = get_cred(psk->owner);
644 vsk->connect_timeout = psk->connect_timeout;
645 } else {
646 vsk->trusted = capable(CAP_NET_ADMIN);
647 vsk->owner = get_current_cred();
648 vsk->connect_timeout = VSOCK_DEFAULT_CONNECT_TIMEOUT;
649 }
650
651 if (transport->init(vsk, psk) < 0) {
652 sk_free(sk);
653 return NULL;
654 }
655
656 if (sock)
657 vsock_insert_unbound(vsk);
658
659 return sk;
660}
661EXPORT_SYMBOL_GPL(__vsock_create);
662
663static void __vsock_release(struct sock *sk)
664{
665 if (sk) {
666 struct sk_buff *skb;
667 struct sock *pending;
668 struct vsock_sock *vsk;
669
670 vsk = vsock_sk(sk);
671 pending = NULL;
672
673 transport->release(vsk);
674
675 lock_sock(sk);
676 sock_orphan(sk);
677 sk->sk_shutdown = SHUTDOWN_MASK;
678
679 while ((skb = skb_dequeue(&sk->sk_receive_queue)))
680 kfree_skb(skb);
681
682
683 while ((pending = vsock_dequeue_accept(sk)) != NULL) {
684 __vsock_release(pending);
685 sock_put(pending);
686 }
687
688 release_sock(sk);
689 sock_put(sk);
690 }
691}
692
693static void vsock_sk_destruct(struct sock *sk)
694{
695 struct vsock_sock *vsk = vsock_sk(sk);
696
697 transport->destruct(vsk);
698
699
700
701
702 vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
703 vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
704
705 put_cred(vsk->owner);
706}
707
708static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
709{
710 int err;
711
712 err = sock_queue_rcv_skb(sk, skb);
713 if (err)
714 kfree_skb(skb);
715
716 return err;
717}
718
719s64 vsock_stream_has_data(struct vsock_sock *vsk)
720{
721 return transport->stream_has_data(vsk);
722}
723EXPORT_SYMBOL_GPL(vsock_stream_has_data);
724
725s64 vsock_stream_has_space(struct vsock_sock *vsk)
726{
727 return transport->stream_has_space(vsk);
728}
729EXPORT_SYMBOL_GPL(vsock_stream_has_space);
730
731static int vsock_release(struct socket *sock)
732{
733 __vsock_release(sock->sk);
734 sock->sk = NULL;
735 sock->state = SS_FREE;
736
737 return 0;
738}
739
740static int
741vsock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
742{
743 int err;
744 struct sock *sk;
745 struct sockaddr_vm *vm_addr;
746
747 sk = sock->sk;
748
749 if (vsock_addr_cast(addr, addr_len, &vm_addr) != 0)
750 return -EINVAL;
751
752 lock_sock(sk);
753 err = __vsock_bind(sk, vm_addr);
754 release_sock(sk);
755
756 return err;
757}
758
759static int vsock_getname(struct socket *sock,
760 struct sockaddr *addr, int *addr_len, int peer)
761{
762 int err;
763 struct sock *sk;
764 struct vsock_sock *vsk;
765 struct sockaddr_vm *vm_addr;
766
767 sk = sock->sk;
768 vsk = vsock_sk(sk);
769 err = 0;
770
771 lock_sock(sk);
772
773 if (peer) {
774 if (sock->state != SS_CONNECTED) {
775 err = -ENOTCONN;
776 goto out;
777 }
778 vm_addr = &vsk->remote_addr;
779 } else {
780 vm_addr = &vsk->local_addr;
781 }
782
783 if (!vm_addr) {
784 err = -EINVAL;
785 goto out;
786 }
787
788
789
790
791
792
793 BUILD_BUG_ON(sizeof(*vm_addr) > 128);
794 memcpy(addr, vm_addr, sizeof(*vm_addr));
795 *addr_len = sizeof(*vm_addr);
796
797out:
798 release_sock(sk);
799 return err;
800}
801
802static int vsock_shutdown(struct socket *sock, int mode)
803{
804 int err;
805 struct sock *sk;
806
807
808
809
810
811
812
813 mode++;
814
815 if ((mode & ~SHUTDOWN_MASK) || !mode)
816 return -EINVAL;
817
818
819
820
821
822
823
824 sk = sock->sk;
825 if (sock->state == SS_UNCONNECTED) {
826 err = -ENOTCONN;
827 if (sk->sk_type == SOCK_STREAM)
828 return err;
829 } else {
830 sock->state = SS_DISCONNECTING;
831 err = 0;
832 }
833
834
835 mode = mode & (RCV_SHUTDOWN | SEND_SHUTDOWN);
836 if (mode) {
837 lock_sock(sk);
838 sk->sk_shutdown |= mode;
839 sk->sk_state_change(sk);
840 release_sock(sk);
841
842 if (sk->sk_type == SOCK_STREAM) {
843 sock_reset_flag(sk, SOCK_DONE);
844 vsock_send_shutdown(sk, mode);
845 }
846 }
847
848 return err;
849}
850
851static unsigned int vsock_poll(struct file *file, struct socket *sock,
852 poll_table *wait)
853{
854 struct sock *sk;
855 unsigned int mask;
856 struct vsock_sock *vsk;
857
858 sk = sock->sk;
859 vsk = vsock_sk(sk);
860
861 poll_wait(file, sk_sleep(sk), wait);
862 mask = 0;
863
864 if (sk->sk_err)
865
866 mask |= POLLERR;
867
868
869
870
871 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
872 ((sk->sk_shutdown & SEND_SHUTDOWN) &&
873 (vsk->peer_shutdown & SEND_SHUTDOWN))) {
874 mask |= POLLHUP;
875 }
876
877 if (sk->sk_shutdown & RCV_SHUTDOWN ||
878 vsk->peer_shutdown & SEND_SHUTDOWN) {
879 mask |= POLLRDHUP;
880 }
881
882 if (sock->type == SOCK_DGRAM) {
883
884
885
886
887 if (!skb_queue_empty(&sk->sk_receive_queue) ||
888 (sk->sk_shutdown & RCV_SHUTDOWN)) {
889 mask |= POLLIN | POLLRDNORM;
890 }
891
892 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
893 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
894
895 } else if (sock->type == SOCK_STREAM) {
896 lock_sock(sk);
897
898
899
900
901 if (sk->sk_state == TCP_LISTEN
902 && !vsock_is_accept_queue_empty(sk))
903 mask |= POLLIN | POLLRDNORM;
904
905
906 if (transport->stream_is_active(vsk) &&
907 !(sk->sk_shutdown & RCV_SHUTDOWN)) {
908 bool data_ready_now = false;
909 int ret = transport->notify_poll_in(
910 vsk, 1, &data_ready_now);
911 if (ret < 0) {
912 mask |= POLLERR;
913 } else {
914 if (data_ready_now)
915 mask |= POLLIN | POLLRDNORM;
916
917 }
918 }
919
920
921
922
923
924 if (sk->sk_shutdown & RCV_SHUTDOWN ||
925 vsk->peer_shutdown & SEND_SHUTDOWN) {
926 mask |= POLLIN | POLLRDNORM;
927 }
928
929
930 if (sk->sk_state == TCP_ESTABLISHED) {
931 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
932 bool space_avail_now = false;
933 int ret = transport->notify_poll_out(
934 vsk, 1, &space_avail_now);
935 if (ret < 0) {
936 mask |= POLLERR;
937 } else {
938 if (space_avail_now)
939
940
941
942 mask |= POLLOUT | POLLWRNORM;
943
944 }
945 }
946 }
947
948
949
950
951
952 if (sk->sk_state == TCP_CLOSE) {
953 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
954 mask |= POLLOUT | POLLWRNORM;
955
956 }
957
958 release_sock(sk);
959 }
960
961 return mask;
962}
963
964static int vsock_dgram_sendmsg(struct kiocb *kiocb, struct socket *sock,
965 struct msghdr *msg, size_t len)
966{
967 int err;
968 struct sock *sk;
969 struct vsock_sock *vsk;
970 struct sockaddr_vm *remote_addr;
971
972 if (msg->msg_flags & MSG_OOB)
973 return -EOPNOTSUPP;
974
975
976 err = 0;
977 sk = sock->sk;
978 vsk = vsock_sk(sk);
979
980 lock_sock(sk);
981
982 err = vsock_auto_bind(vsk);
983 if (err)
984 goto out;
985
986
987
988
989
990 if (msg->msg_name &&
991 vsock_addr_cast(msg->msg_name, msg->msg_namelen,
992 &remote_addr) == 0) {
993
994
995
996
997 if (remote_addr->svm_cid == VMADDR_CID_ANY)
998 remote_addr->svm_cid = transport->get_local_cid();
999
1000 if (!vsock_addr_bound(remote_addr)) {
1001 err = -EINVAL;
1002 goto out;
1003 }
1004 } else if (sock->state == SS_CONNECTED) {
1005 remote_addr = &vsk->remote_addr;
1006
1007 if (remote_addr->svm_cid == VMADDR_CID_ANY)
1008 remote_addr->svm_cid = transport->get_local_cid();
1009
1010
1011
1012
1013 if (!vsock_addr_bound(&vsk->remote_addr)) {
1014 err = -EINVAL;
1015 goto out;
1016 }
1017 } else {
1018 err = -EINVAL;
1019 goto out;
1020 }
1021
1022 if (!transport->dgram_allow(remote_addr->svm_cid,
1023 remote_addr->svm_port)) {
1024 err = -EINVAL;
1025 goto out;
1026 }
1027
1028 err = transport->dgram_enqueue(vsk, remote_addr, msg, len);
1029
1030out:
1031 release_sock(sk);
1032 return err;
1033}
1034
1035static int vsock_dgram_connect(struct socket *sock,
1036 struct sockaddr *addr, int addr_len, int flags)
1037{
1038 int err;
1039 struct sock *sk;
1040 struct vsock_sock *vsk;
1041 struct sockaddr_vm *remote_addr;
1042
1043 sk = sock->sk;
1044 vsk = vsock_sk(sk);
1045
1046 err = vsock_addr_cast(addr, addr_len, &remote_addr);
1047 if (err == -EAFNOSUPPORT && remote_addr->svm_family == AF_UNSPEC) {
1048 lock_sock(sk);
1049 vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY,
1050 VMADDR_PORT_ANY);
1051 sock->state = SS_UNCONNECTED;
1052 release_sock(sk);
1053 return 0;
1054 } else if (err != 0)
1055 return -EINVAL;
1056
1057 lock_sock(sk);
1058
1059 err = vsock_auto_bind(vsk);
1060 if (err)
1061 goto out;
1062
1063 if (!transport->dgram_allow(remote_addr->svm_cid,
1064 remote_addr->svm_port)) {
1065 err = -EINVAL;
1066 goto out;
1067 }
1068
1069 memcpy(&vsk->remote_addr, remote_addr, sizeof(vsk->remote_addr));
1070 sock->state = SS_CONNECTED;
1071
1072out:
1073 release_sock(sk);
1074 return err;
1075}
1076
1077static int vsock_dgram_recvmsg(struct kiocb *kiocb, struct socket *sock,
1078 struct msghdr *msg, size_t len, int flags)
1079{
1080 return transport->dgram_dequeue(kiocb, vsock_sk(sock->sk), msg, len,
1081 flags);
1082}
1083
1084static const struct proto_ops vsock_dgram_ops = {
1085 .family = PF_VSOCK,
1086 .owner = THIS_MODULE,
1087 .release = vsock_release,
1088 .bind = vsock_bind,
1089 .connect = vsock_dgram_connect,
1090 .socketpair = sock_no_socketpair,
1091 .accept = sock_no_accept,
1092 .getname = vsock_getname,
1093 .poll = vsock_poll,
1094 .ioctl = sock_no_ioctl,
1095 .listen = sock_no_listen,
1096 .shutdown = vsock_shutdown,
1097 .setsockopt = sock_no_setsockopt,
1098 .getsockopt = sock_no_getsockopt,
1099 .sendmsg = vsock_dgram_sendmsg,
1100 .recvmsg = vsock_dgram_recvmsg,
1101 .mmap = sock_no_mmap,
1102 .sendpage = sock_no_sendpage,
1103};
1104
1105static void vsock_connect_timeout(struct work_struct *work)
1106{
1107 struct sock *sk;
1108 struct vsock_sock *vsk;
1109
1110 vsk = container_of(work, struct vsock_sock, dwork.work);
1111 sk = sk_vsock(vsk);
1112
1113 lock_sock(sk);
1114 if (sk->sk_state == TCP_SYN_SENT &&
1115 (sk->sk_shutdown != SHUTDOWN_MASK)) {
1116 sk->sk_state = TCP_CLOSE;
1117 sk->sk_err = ETIMEDOUT;
1118 sk->sk_error_report(sk);
1119 }
1120 release_sock(sk);
1121
1122 sock_put(sk);
1123}
1124
1125static int vsock_stream_connect(struct socket *sock, struct sockaddr *addr,
1126 int addr_len, int flags)
1127{
1128 int err;
1129 struct sock *sk;
1130 struct vsock_sock *vsk;
1131 struct sockaddr_vm *remote_addr;
1132 long timeout;
1133 DEFINE_WAIT(wait);
1134
1135 err = 0;
1136 sk = sock->sk;
1137 vsk = vsock_sk(sk);
1138
1139 lock_sock(sk);
1140
1141
1142 switch (sock->state) {
1143 case SS_CONNECTED:
1144 err = -EISCONN;
1145 goto out;
1146 case SS_DISCONNECTING:
1147 err = -EINVAL;
1148 goto out;
1149 case SS_CONNECTING:
1150
1151
1152
1153
1154
1155
1156 err = -EALREADY;
1157 break;
1158 default:
1159 if ((sk->sk_state == TCP_LISTEN) ||
1160 vsock_addr_cast(addr, addr_len, &remote_addr) != 0) {
1161 err = -EINVAL;
1162 goto out;
1163 }
1164
1165
1166
1167
1168 if (!transport->stream_allow(remote_addr->svm_cid,
1169 remote_addr->svm_port)) {
1170 err = -ENETUNREACH;
1171 goto out;
1172 }
1173
1174
1175 memcpy(&vsk->remote_addr, remote_addr,
1176 sizeof(vsk->remote_addr));
1177
1178 err = vsock_auto_bind(vsk);
1179 if (err)
1180 goto out;
1181
1182 sk->sk_state = TCP_SYN_SENT;
1183
1184 err = transport->connect(vsk);
1185 if (err < 0)
1186 goto out;
1187
1188
1189
1190
1191 sock->state = SS_CONNECTING;
1192 err = -EINPROGRESS;
1193 }
1194
1195
1196
1197
1198
1199 timeout = vsk->connect_timeout;
1200 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1201
1202 while (sk->sk_state != TCP_ESTABLISHED && sk->sk_err == 0) {
1203 if (flags & O_NONBLOCK) {
1204
1205
1206
1207
1208
1209
1210 sock_hold(sk);
1211 INIT_DELAYED_WORK(&vsk->dwork,
1212 vsock_connect_timeout);
1213 schedule_delayed_work(&vsk->dwork, timeout);
1214
1215
1216 goto out_wait;
1217 }
1218
1219 release_sock(sk);
1220 timeout = schedule_timeout(timeout);
1221 lock_sock(sk);
1222
1223 if (signal_pending(current)) {
1224 err = sock_intr_errno(timeout);
1225 sk->sk_state = TCP_CLOSE;
1226 sock->state = SS_UNCONNECTED;
1227 goto out_wait;
1228 } else if (timeout == 0) {
1229 err = -ETIMEDOUT;
1230 sk->sk_state = TCP_CLOSE;
1231 sock->state = SS_UNCONNECTED;
1232 goto out_wait;
1233 }
1234
1235 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1236 }
1237
1238 if (sk->sk_err) {
1239 err = -sk->sk_err;
1240 sk->sk_state = TCP_CLOSE;
1241 sock->state = SS_UNCONNECTED;
1242 } else {
1243 err = 0;
1244 }
1245
1246out_wait:
1247 finish_wait(sk_sleep(sk), &wait);
1248out:
1249 release_sock(sk);
1250 return err;
1251}
1252
1253static int vsock_accept(struct socket *sock, struct socket *newsock, int flags)
1254{
1255 struct sock *listener;
1256 int err;
1257 struct sock *connected;
1258 struct vsock_sock *vconnected;
1259 long timeout;
1260 DEFINE_WAIT(wait);
1261
1262 err = 0;
1263 listener = sock->sk;
1264
1265 lock_sock(listener);
1266
1267 if (sock->type != SOCK_STREAM) {
1268 err = -EOPNOTSUPP;
1269 goto out;
1270 }
1271
1272 if (listener->sk_state != TCP_LISTEN) {
1273 err = -EINVAL;
1274 goto out;
1275 }
1276
1277
1278
1279
1280 timeout = sock_sndtimeo(listener, flags & O_NONBLOCK);
1281 prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE);
1282
1283 while ((connected = vsock_dequeue_accept(listener)) == NULL &&
1284 listener->sk_err == 0) {
1285 release_sock(listener);
1286 timeout = schedule_timeout(timeout);
1287 finish_wait(sk_sleep(listener), &wait);
1288 lock_sock(listener);
1289
1290 if (signal_pending(current)) {
1291 err = sock_intr_errno(timeout);
1292 goto out;
1293 } else if (timeout == 0) {
1294 err = -EAGAIN;
1295 goto out;
1296 }
1297
1298 prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE);
1299 }
1300 finish_wait(sk_sleep(listener), &wait);
1301
1302 if (listener->sk_err)
1303 err = -listener->sk_err;
1304
1305 if (connected) {
1306 listener->sk_ack_backlog--;
1307
1308 lock_sock_nested(connected, SINGLE_DEPTH_NESTING);
1309 vconnected = vsock_sk(connected);
1310
1311
1312
1313
1314
1315
1316
1317
1318 if (err) {
1319 vconnected->rejected = true;
1320 } else {
1321 newsock->state = SS_CONNECTED;
1322 sock_graft(connected, newsock);
1323 }
1324
1325 release_sock(connected);
1326 sock_put(connected);
1327 }
1328
1329out:
1330 release_sock(listener);
1331 return err;
1332}
1333
1334static int vsock_listen(struct socket *sock, int backlog)
1335{
1336 int err;
1337 struct sock *sk;
1338 struct vsock_sock *vsk;
1339
1340 sk = sock->sk;
1341
1342 lock_sock(sk);
1343
1344 if (sock->type != SOCK_STREAM) {
1345 err = -EOPNOTSUPP;
1346 goto out;
1347 }
1348
1349 if (sock->state != SS_UNCONNECTED) {
1350 err = -EINVAL;
1351 goto out;
1352 }
1353
1354 vsk = vsock_sk(sk);
1355
1356 if (!vsock_addr_bound(&vsk->local_addr)) {
1357 err = -EINVAL;
1358 goto out;
1359 }
1360
1361 sk->sk_max_ack_backlog = backlog;
1362 sk->sk_state = TCP_LISTEN;
1363
1364 err = 0;
1365
1366out:
1367 release_sock(sk);
1368 return err;
1369}
1370
1371static int vsock_stream_setsockopt(struct socket *sock,
1372 int level,
1373 int optname,
1374 char __user *optval,
1375 unsigned int optlen)
1376{
1377 int err;
1378 struct sock *sk;
1379 struct vsock_sock *vsk;
1380 u64 val;
1381
1382 if (level != AF_VSOCK)
1383 return -ENOPROTOOPT;
1384
1385#define COPY_IN(_v) \
1386 do { \
1387 if (optlen < sizeof(_v)) { \
1388 err = -EINVAL; \
1389 goto exit; \
1390 } \
1391 if (copy_from_user(&_v, optval, sizeof(_v)) != 0) { \
1392 err = -EFAULT; \
1393 goto exit; \
1394 } \
1395 } while (0)
1396
1397 err = 0;
1398 sk = sock->sk;
1399 vsk = vsock_sk(sk);
1400
1401 lock_sock(sk);
1402
1403 switch (optname) {
1404 case SO_VM_SOCKETS_BUFFER_SIZE:
1405 COPY_IN(val);
1406 transport->set_buffer_size(vsk, val);
1407 break;
1408
1409 case SO_VM_SOCKETS_BUFFER_MAX_SIZE:
1410 COPY_IN(val);
1411 transport->set_max_buffer_size(vsk, val);
1412 break;
1413
1414 case SO_VM_SOCKETS_BUFFER_MIN_SIZE:
1415 COPY_IN(val);
1416 transport->set_min_buffer_size(vsk, val);
1417 break;
1418
1419 case SO_VM_SOCKETS_CONNECT_TIMEOUT: {
1420 struct timeval tv;
1421 COPY_IN(tv);
1422 if (tv.tv_sec >= 0 && tv.tv_usec < USEC_PER_SEC &&
1423 tv.tv_sec < (MAX_SCHEDULE_TIMEOUT / HZ - 1)) {
1424 vsk->connect_timeout = tv.tv_sec * HZ +
1425 DIV_ROUND_UP(tv.tv_usec, (1000000 / HZ));
1426 if (vsk->connect_timeout == 0)
1427 vsk->connect_timeout =
1428 VSOCK_DEFAULT_CONNECT_TIMEOUT;
1429
1430 } else {
1431 err = -ERANGE;
1432 }
1433 break;
1434 }
1435
1436 default:
1437 err = -ENOPROTOOPT;
1438 break;
1439 }
1440
1441#undef COPY_IN
1442
1443exit:
1444 release_sock(sk);
1445 return err;
1446}
1447
1448static int vsock_stream_getsockopt(struct socket *sock,
1449 int level, int optname,
1450 char __user *optval,
1451 int __user *optlen)
1452{
1453 int err;
1454 int len;
1455 struct sock *sk;
1456 struct vsock_sock *vsk;
1457 u64 val;
1458
1459 if (level != AF_VSOCK)
1460 return -ENOPROTOOPT;
1461
1462 err = get_user(len, optlen);
1463 if (err != 0)
1464 return err;
1465
1466#define COPY_OUT(_v) \
1467 do { \
1468 if (len < sizeof(_v)) \
1469 return -EINVAL; \
1470 \
1471 len = sizeof(_v); \
1472 if (copy_to_user(optval, &_v, len) != 0) \
1473 return -EFAULT; \
1474 \
1475 } while (0)
1476
1477 err = 0;
1478 sk = sock->sk;
1479 vsk = vsock_sk(sk);
1480
1481 switch (optname) {
1482 case SO_VM_SOCKETS_BUFFER_SIZE:
1483 val = transport->get_buffer_size(vsk);
1484 COPY_OUT(val);
1485 break;
1486
1487 case SO_VM_SOCKETS_BUFFER_MAX_SIZE:
1488 val = transport->get_max_buffer_size(vsk);
1489 COPY_OUT(val);
1490 break;
1491
1492 case SO_VM_SOCKETS_BUFFER_MIN_SIZE:
1493 val = transport->get_min_buffer_size(vsk);
1494 COPY_OUT(val);
1495 break;
1496
1497 case SO_VM_SOCKETS_CONNECT_TIMEOUT: {
1498 struct timeval tv;
1499 tv.tv_sec = vsk->connect_timeout / HZ;
1500 tv.tv_usec =
1501 (vsk->connect_timeout -
1502 tv.tv_sec * HZ) * (1000000 / HZ);
1503 COPY_OUT(tv);
1504 break;
1505 }
1506 default:
1507 return -ENOPROTOOPT;
1508 }
1509
1510 err = put_user(len, optlen);
1511 if (err != 0)
1512 return -EFAULT;
1513
1514#undef COPY_OUT
1515
1516 return 0;
1517}
1518
1519static int vsock_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
1520 struct msghdr *msg, size_t len)
1521{
1522 struct sock *sk;
1523 struct vsock_sock *vsk;
1524 ssize_t total_written;
1525 long timeout;
1526 int err;
1527 struct vsock_transport_send_notify_data send_data;
1528
1529 DEFINE_WAIT(wait);
1530
1531 sk = sock->sk;
1532 vsk = vsock_sk(sk);
1533 total_written = 0;
1534 err = 0;
1535
1536 if (msg->msg_flags & MSG_OOB)
1537 return -EOPNOTSUPP;
1538
1539 lock_sock(sk);
1540
1541
1542 if (msg->msg_namelen) {
1543 err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
1544 goto out;
1545 }
1546
1547
1548 if (sk->sk_shutdown & SEND_SHUTDOWN ||
1549 vsk->peer_shutdown & RCV_SHUTDOWN) {
1550 err = -EPIPE;
1551 goto out;
1552 }
1553
1554 if (sk->sk_state != TCP_ESTABLISHED ||
1555 !vsock_addr_bound(&vsk->local_addr)) {
1556 err = -ENOTCONN;
1557 goto out;
1558 }
1559
1560 if (!vsock_addr_bound(&vsk->remote_addr)) {
1561 err = -EDESTADDRREQ;
1562 goto out;
1563 }
1564
1565
1566 timeout = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1567
1568 err = transport->notify_send_init(vsk, &send_data);
1569 if (err < 0)
1570 goto out;
1571
1572
1573 while (total_written < len) {
1574 ssize_t written;
1575
1576 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1577 while (vsock_stream_has_space(vsk) == 0 &&
1578 sk->sk_err == 0 &&
1579 !(sk->sk_shutdown & SEND_SHUTDOWN) &&
1580 !(vsk->peer_shutdown & RCV_SHUTDOWN)) {
1581
1582
1583 if (timeout == 0) {
1584 err = -EAGAIN;
1585 finish_wait(sk_sleep(sk), &wait);
1586 goto out_err;
1587 }
1588
1589 err = transport->notify_send_pre_block(vsk, &send_data);
1590 if (err < 0) {
1591 finish_wait(sk_sleep(sk), &wait);
1592 goto out_err;
1593 }
1594
1595 release_sock(sk);
1596 timeout = schedule_timeout(timeout);
1597 lock_sock(sk);
1598 if (signal_pending(current)) {
1599 err = sock_intr_errno(timeout);
1600 finish_wait(sk_sleep(sk), &wait);
1601 goto out_err;
1602 } else if (timeout == 0) {
1603 err = -EAGAIN;
1604 finish_wait(sk_sleep(sk), &wait);
1605 goto out_err;
1606 }
1607
1608 prepare_to_wait(sk_sleep(sk), &wait,
1609 TASK_INTERRUPTIBLE);
1610 }
1611 finish_wait(sk_sleep(sk), &wait);
1612
1613
1614
1615
1616
1617 if (sk->sk_err) {
1618 err = -sk->sk_err;
1619 goto out_err;
1620 } else if ((sk->sk_shutdown & SEND_SHUTDOWN) ||
1621 (vsk->peer_shutdown & RCV_SHUTDOWN)) {
1622 err = -EPIPE;
1623 goto out_err;
1624 }
1625
1626 err = transport->notify_send_pre_enqueue(vsk, &send_data);
1627 if (err < 0)
1628 goto out_err;
1629
1630
1631
1632
1633
1634
1635
1636 written = transport->stream_enqueue(
1637 vsk, msg,
1638 len - total_written);
1639 if (written < 0) {
1640 err = -ENOMEM;
1641 goto out_err;
1642 }
1643
1644 total_written += written;
1645
1646 err = transport->notify_send_post_enqueue(
1647 vsk, written, &send_data);
1648 if (err < 0)
1649 goto out_err;
1650
1651 }
1652
1653out_err:
1654 if (total_written > 0)
1655 err = total_written;
1656out:
1657 release_sock(sk);
1658 return err;
1659}
1660
1661
1662static int
1663vsock_stream_recvmsg(struct kiocb *kiocb,
1664 struct socket *sock,
1665 struct msghdr *msg, size_t len, int flags)
1666{
1667 struct sock *sk;
1668 struct vsock_sock *vsk;
1669 int err;
1670 size_t target;
1671 ssize_t copied;
1672 long timeout;
1673 struct vsock_transport_recv_notify_data recv_data;
1674
1675 DEFINE_WAIT(wait);
1676
1677 sk = sock->sk;
1678 vsk = vsock_sk(sk);
1679 err = 0;
1680
1681 lock_sock(sk);
1682
1683 if (sk->sk_state != TCP_ESTABLISHED) {
1684
1685
1686
1687
1688
1689 if (sock_flag(sk, SOCK_DONE))
1690 err = 0;
1691 else
1692 err = -ENOTCONN;
1693
1694 goto out;
1695 }
1696
1697 if (flags & MSG_OOB) {
1698 err = -EOPNOTSUPP;
1699 goto out;
1700 }
1701
1702
1703
1704
1705
1706 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1707 err = 0;
1708 goto out;
1709 }
1710
1711
1712
1713
1714 if (!len) {
1715 err = 0;
1716 goto out;
1717 }
1718
1719
1720
1721
1722
1723
1724
1725 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
1726 if (target >= transport->stream_rcvhiwat(vsk)) {
1727 err = -ENOMEM;
1728 goto out;
1729 }
1730 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1731 copied = 0;
1732
1733 err = transport->notify_recv_init(vsk, target, &recv_data);
1734 if (err < 0)
1735 goto out;
1736
1737
1738 while (1) {
1739 s64 ready;
1740
1741 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1742 ready = vsock_stream_has_data(vsk);
1743
1744 if (ready == 0) {
1745 if (sk->sk_err != 0 ||
1746 (sk->sk_shutdown & RCV_SHUTDOWN) ||
1747 (vsk->peer_shutdown & SEND_SHUTDOWN)) {
1748 finish_wait(sk_sleep(sk), &wait);
1749 break;
1750 }
1751
1752 if (timeout == 0) {
1753 err = -EAGAIN;
1754 finish_wait(sk_sleep(sk), &wait);
1755 break;
1756 }
1757
1758 err = transport->notify_recv_pre_block(
1759 vsk, target, &recv_data);
1760 if (err < 0) {
1761 finish_wait(sk_sleep(sk), &wait);
1762 break;
1763 }
1764 release_sock(sk);
1765 timeout = schedule_timeout(timeout);
1766 lock_sock(sk);
1767
1768 if (signal_pending(current)) {
1769 err = sock_intr_errno(timeout);
1770 finish_wait(sk_sleep(sk), &wait);
1771 break;
1772 } else if (timeout == 0) {
1773 err = -EAGAIN;
1774 finish_wait(sk_sleep(sk), &wait);
1775 break;
1776 }
1777 } else {
1778 ssize_t read;
1779
1780 finish_wait(sk_sleep(sk), &wait);
1781
1782 if (ready < 0) {
1783
1784
1785
1786
1787
1788 err = -ENOMEM;
1789 goto out;
1790 }
1791
1792 err = transport->notify_recv_pre_dequeue(
1793 vsk, target, &recv_data);
1794 if (err < 0)
1795 break;
1796
1797 read = transport->stream_dequeue(
1798 vsk, msg,
1799 len - copied, flags);
1800 if (read < 0) {
1801 err = -ENOMEM;
1802 break;
1803 }
1804
1805 copied += read;
1806
1807 err = transport->notify_recv_post_dequeue(
1808 vsk, target, read,
1809 !(flags & MSG_PEEK), &recv_data);
1810 if (err < 0)
1811 goto out;
1812
1813 if (read >= target || flags & MSG_PEEK)
1814 break;
1815
1816 target -= read;
1817 }
1818 }
1819
1820 if (sk->sk_err)
1821 err = -sk->sk_err;
1822 else if (sk->sk_shutdown & RCV_SHUTDOWN)
1823 err = 0;
1824
1825 if (copied > 0)
1826 err = copied;
1827
1828out:
1829 release_sock(sk);
1830 return err;
1831}
1832
1833static const struct proto_ops vsock_stream_ops = {
1834 .family = PF_VSOCK,
1835 .owner = THIS_MODULE,
1836 .release = vsock_release,
1837 .bind = vsock_bind,
1838 .connect = vsock_stream_connect,
1839 .socketpair = sock_no_socketpair,
1840 .accept = vsock_accept,
1841 .getname = vsock_getname,
1842 .poll = vsock_poll,
1843 .ioctl = sock_no_ioctl,
1844 .listen = vsock_listen,
1845 .shutdown = vsock_shutdown,
1846 .setsockopt = vsock_stream_setsockopt,
1847 .getsockopt = vsock_stream_getsockopt,
1848 .sendmsg = vsock_stream_sendmsg,
1849 .recvmsg = vsock_stream_recvmsg,
1850 .mmap = sock_no_mmap,
1851 .sendpage = sock_no_sendpage,
1852};
1853
1854static int vsock_create(struct net *net, struct socket *sock,
1855 int protocol, int kern)
1856{
1857 if (!sock)
1858 return -EINVAL;
1859
1860 if (protocol && protocol != PF_VSOCK)
1861 return -EPROTONOSUPPORT;
1862
1863 switch (sock->type) {
1864 case SOCK_DGRAM:
1865 sock->ops = &vsock_dgram_ops;
1866 break;
1867 case SOCK_STREAM:
1868 sock->ops = &vsock_stream_ops;
1869 break;
1870 default:
1871 return -ESOCKTNOSUPPORT;
1872 }
1873
1874 sock->state = SS_UNCONNECTED;
1875
1876 return __vsock_create(net, sock, NULL, GFP_KERNEL, 0) ? 0 : -ENOMEM;
1877}
1878
1879static const struct net_proto_family vsock_family_ops = {
1880 .family = AF_VSOCK,
1881 .create = vsock_create,
1882 .owner = THIS_MODULE,
1883};
1884
1885static long vsock_dev_do_ioctl(struct file *filp,
1886 unsigned int cmd, void __user *ptr)
1887{
1888 u32 __user *p = ptr;
1889 int retval = 0;
1890
1891 switch (cmd) {
1892 case IOCTL_VM_SOCKETS_GET_LOCAL_CID:
1893 if (put_user(transport->get_local_cid(), p) != 0)
1894 retval = -EFAULT;
1895 break;
1896
1897 default:
1898 pr_err("Unknown ioctl %d\n", cmd);
1899 retval = -EINVAL;
1900 }
1901
1902 return retval;
1903}
1904
1905static long vsock_dev_ioctl(struct file *filp,
1906 unsigned int cmd, unsigned long arg)
1907{
1908 return vsock_dev_do_ioctl(filp, cmd, (void __user *)arg);
1909}
1910
1911#ifdef CONFIG_COMPAT
1912static long vsock_dev_compat_ioctl(struct file *filp,
1913 unsigned int cmd, unsigned long arg)
1914{
1915 return vsock_dev_do_ioctl(filp, cmd, compat_ptr(arg));
1916}
1917#endif
1918
1919static const struct file_operations vsock_device_ops = {
1920 .owner = THIS_MODULE,
1921 .unlocked_ioctl = vsock_dev_ioctl,
1922#ifdef CONFIG_COMPAT
1923 .compat_ioctl = vsock_dev_compat_ioctl,
1924#endif
1925 .open = nonseekable_open,
1926};
1927
1928static struct miscdevice vsock_device = {
1929 .name = "vsock",
1930 .fops = &vsock_device_ops,
1931};
1932
1933int __vsock_core_init(const struct vsock_transport *t, struct module *owner)
1934{
1935 int err = mutex_lock_interruptible(&vsock_register_mutex);
1936
1937 if (err)
1938 return err;
1939
1940 if (transport) {
1941 err = -EBUSY;
1942 goto err_busy;
1943 }
1944
1945
1946
1947
1948 vsock_proto.owner = owner;
1949 transport = t;
1950
1951 vsock_device.minor = MISC_DYNAMIC_MINOR;
1952 err = misc_register(&vsock_device);
1953 if (err) {
1954 pr_err("Failed to register misc device\n");
1955 goto err_reset_transport;
1956 }
1957
1958 err = proto_register(&vsock_proto, 1);
1959 if (err) {
1960 pr_err("Cannot register vsock protocol\n");
1961 goto err_deregister_misc;
1962 }
1963
1964 err = sock_register(&vsock_family_ops);
1965 if (err) {
1966 pr_err("could not register af_vsock (%d) address family: %d\n",
1967 AF_VSOCK, err);
1968 goto err_unregister_proto;
1969 }
1970
1971 mutex_unlock(&vsock_register_mutex);
1972 return 0;
1973
1974err_unregister_proto:
1975 proto_unregister(&vsock_proto);
1976err_deregister_misc:
1977 misc_deregister(&vsock_device);
1978err_reset_transport:
1979 transport = NULL;
1980err_busy:
1981 mutex_unlock(&vsock_register_mutex);
1982 return err;
1983}
1984EXPORT_SYMBOL_GPL(__vsock_core_init);
1985
1986void vsock_core_exit(void)
1987{
1988 mutex_lock(&vsock_register_mutex);
1989
1990 misc_deregister(&vsock_device);
1991 sock_unregister(AF_VSOCK);
1992 proto_unregister(&vsock_proto);
1993
1994
1995 mb();
1996 transport = NULL;
1997
1998 mutex_unlock(&vsock_register_mutex);
1999}
2000EXPORT_SYMBOL_GPL(vsock_core_exit);
2001
2002const struct vsock_transport *vsock_core_get_transport(void)
2003{
2004
2005
2006
2007 return transport;
2008}
2009EXPORT_SYMBOL_GPL(vsock_core_get_transport);
2010
2011module_init(vsock_init_tables);
2012
2013MODULE_AUTHOR("VMware, Inc.");
2014MODULE_DESCRIPTION("VMware Virtual Socket Family");
2015MODULE_VERSION("1.0.2.0-k");
2016MODULE_LICENSE("GPL v2");
2017