1685a6bf8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d021c344SAndy King /*
3d021c344SAndy King * VMware vSockets Driver
4d021c344SAndy King *
5d021c344SAndy King * Copyright (C) 2007-2013 VMware, Inc. All rights reserved.
6d021c344SAndy King */
7d021c344SAndy King
8d021c344SAndy King #include <linux/types.h>
9d021c344SAndy King #include <linux/bitops.h>
10d021c344SAndy King #include <linux/cred.h>
11d021c344SAndy King #include <linux/init.h>
12d021c344SAndy King #include <linux/io.h>
13d021c344SAndy King #include <linux/kernel.h>
14d021c344SAndy King #include <linux/kmod.h>
15d021c344SAndy King #include <linux/list.h>
16d021c344SAndy King #include <linux/module.h>
17d021c344SAndy King #include <linux/mutex.h>
18d021c344SAndy King #include <linux/net.h>
19d021c344SAndy King #include <linux/poll.h>
20d021c344SAndy King #include <linux/skbuff.h>
21d021c344SAndy King #include <linux/smp.h>
22d021c344SAndy King #include <linux/socket.h>
23d021c344SAndy King #include <linux/stddef.h>
24d021c344SAndy King #include <linux/unistd.h>
25d021c344SAndy King #include <linux/wait.h>
26d021c344SAndy King #include <linux/workqueue.h>
27d021c344SAndy King #include <net/sock.h>
2882a54d0eSAsias He #include <net/af_vsock.h>
29d021c344SAndy King
30d021c344SAndy King #include "vmci_transport_notify.h"
31d021c344SAndy King
32d021c344SAndy King static int vmci_transport_recv_dgram_cb(void *data, struct vmci_datagram *dg);
33d021c344SAndy King static int vmci_transport_recv_stream_cb(void *data, struct vmci_datagram *dg);
34d021c344SAndy King static void vmci_transport_peer_detach_cb(u32 sub_id,
35d021c344SAndy King const struct vmci_event_data *ed,
36d021c344SAndy King void *client_data);
37d021c344SAndy King static void vmci_transport_recv_pkt_work(struct work_struct *work);
384ef7ea91SJorgen Hansen static void vmci_transport_cleanup(struct work_struct *work);
39d021c344SAndy King static int vmci_transport_recv_listen(struct sock *sk,
40d021c344SAndy King struct vmci_transport_packet *pkt);
41d021c344SAndy King static int vmci_transport_recv_connecting_server(
42d021c344SAndy King struct sock *sk,
43d021c344SAndy King struct sock *pending,
44d021c344SAndy King struct vmci_transport_packet *pkt);
45d021c344SAndy King static int vmci_transport_recv_connecting_client(
46d021c344SAndy King struct sock *sk,
47d021c344SAndy King struct vmci_transport_packet *pkt);
48d021c344SAndy King static int vmci_transport_recv_connecting_client_negotiate(
49d021c344SAndy King struct sock *sk,
50d021c344SAndy King struct vmci_transport_packet *pkt);
51d021c344SAndy King static int vmci_transport_recv_connecting_client_invalid(
52d021c344SAndy King struct sock *sk,
53d021c344SAndy King struct vmci_transport_packet *pkt);
54d021c344SAndy King static int vmci_transport_recv_connected(struct sock *sk,
55d021c344SAndy King struct vmci_transport_packet *pkt);
56d021c344SAndy King static bool vmci_transport_old_proto_override(bool *old_pkt_proto);
57d021c344SAndy King static u16 vmci_transport_new_proto_supported_versions(void);
58d021c344SAndy King static bool vmci_transport_proto_to_notify_struct(struct sock *sk, u16 *proto,
59d021c344SAndy King bool old_pkt_proto);
60c0cfa2d8SStefano Garzarella static bool vmci_check_transport(struct vsock_sock *vsk);
61d021c344SAndy King
62d021c344SAndy King struct vmci_transport_recv_pkt_info {
63d021c344SAndy King struct work_struct work;
64d021c344SAndy King struct sock *sk;
65d021c344SAndy King struct vmci_transport_packet pkt;
66d021c344SAndy King };
67d021c344SAndy King
684ef7ea91SJorgen Hansen static LIST_HEAD(vmci_transport_cleanup_list);
694ef7ea91SJorgen Hansen static DEFINE_SPINLOCK(vmci_transport_cleanup_lock);
704ef7ea91SJorgen Hansen static DECLARE_WORK(vmci_transport_cleanup_work, vmci_transport_cleanup);
714ef7ea91SJorgen Hansen
72d021c344SAndy King static struct vmci_handle vmci_transport_stream_handle = { VMCI_INVALID_ID,
73d021c344SAndy King VMCI_INVALID_ID };
74d021c344SAndy King static u32 vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID;
75d021c344SAndy King
76d021c344SAndy King static int PROTOCOL_OVERRIDE = -1;
77d021c344SAndy King
788e6ed963SJiyong Park static struct vsock_transport vmci_transport; /* forward declaration */
798e6ed963SJiyong Park
80d021c344SAndy King /* Helper function to convert from a VMCI error code to a VSock error code. */
81d021c344SAndy King
vmci_transport_error_to_vsock_error(s32 vmci_error)82d021c344SAndy King static s32 vmci_transport_error_to_vsock_error(s32 vmci_error)
83d021c344SAndy King {
84d021c344SAndy King switch (vmci_error) {
85d021c344SAndy King case VMCI_ERROR_NO_MEM:
86ed8bfd5cSColin Ian King return -ENOMEM;
87d021c344SAndy King case VMCI_ERROR_DUPLICATE_ENTRY:
88d021c344SAndy King case VMCI_ERROR_ALREADY_EXISTS:
89ed8bfd5cSColin Ian King return -EADDRINUSE;
90d021c344SAndy King case VMCI_ERROR_NO_ACCESS:
91ed8bfd5cSColin Ian King return -EPERM;
92d021c344SAndy King case VMCI_ERROR_NO_RESOURCES:
93ed8bfd5cSColin Ian King return -ENOBUFS;
94d021c344SAndy King case VMCI_ERROR_INVALID_RESOURCE:
95ed8bfd5cSColin Ian King return -EHOSTUNREACH;
96d021c344SAndy King case VMCI_ERROR_INVALID_ARGS:
97d021c344SAndy King default:
98ed8bfd5cSColin Ian King break;
99d021c344SAndy King }
100ed8bfd5cSColin Ian King return -EINVAL;
101d021c344SAndy King }
102d021c344SAndy King
vmci_transport_peer_rid(u32 peer_cid)1032a89f924SReilly Grant static u32 vmci_transport_peer_rid(u32 peer_cid)
1042a89f924SReilly Grant {
1052a89f924SReilly Grant if (VMADDR_CID_HYPERVISOR == peer_cid)
1062a89f924SReilly Grant return VMCI_TRANSPORT_HYPERVISOR_PACKET_RID;
1072a89f924SReilly Grant
1082a89f924SReilly Grant return VMCI_TRANSPORT_PACKET_RID;
1092a89f924SReilly Grant }
1102a89f924SReilly Grant
111d021c344SAndy King static inline void
vmci_transport_packet_init(struct vmci_transport_packet * pkt,struct sockaddr_vm * src,struct sockaddr_vm * dst,u8 type,u64 size,u64 mode,struct vmci_transport_waiting_info * wait,u16 proto,struct vmci_handle handle)112d021c344SAndy King vmci_transport_packet_init(struct vmci_transport_packet *pkt,
113d021c344SAndy King struct sockaddr_vm *src,
114d021c344SAndy King struct sockaddr_vm *dst,
115d021c344SAndy King u8 type,
116d021c344SAndy King u64 size,
117d021c344SAndy King u64 mode,
118d021c344SAndy King struct vmci_transport_waiting_info *wait,
119d021c344SAndy King u16 proto,
120d021c344SAndy King struct vmci_handle handle)
121d021c344SAndy King {
122d021c344SAndy King /* We register the stream control handler as an any cid handle so we
123d021c344SAndy King * must always send from a source address of VMADDR_CID_ANY
124d021c344SAndy King */
125d021c344SAndy King pkt->dg.src = vmci_make_handle(VMADDR_CID_ANY,
126d021c344SAndy King VMCI_TRANSPORT_PACKET_RID);
127d021c344SAndy King pkt->dg.dst = vmci_make_handle(dst->svm_cid,
1282a89f924SReilly Grant vmci_transport_peer_rid(dst->svm_cid));
129d021c344SAndy King pkt->dg.payload_size = sizeof(*pkt) - sizeof(pkt->dg);
130d021c344SAndy King pkt->version = VMCI_TRANSPORT_PACKET_VERSION;
131d021c344SAndy King pkt->type = type;
132d021c344SAndy King pkt->src_port = src->svm_port;
133d021c344SAndy King pkt->dst_port = dst->svm_port;
134d021c344SAndy King memset(&pkt->proto, 0, sizeof(pkt->proto));
135d021c344SAndy King memset(&pkt->_reserved2, 0, sizeof(pkt->_reserved2));
136d021c344SAndy King
137d021c344SAndy King switch (pkt->type) {
138d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_INVALID:
139d021c344SAndy King pkt->u.size = 0;
140d021c344SAndy King break;
141d021c344SAndy King
142d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_REQUEST:
143d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE:
144d021c344SAndy King pkt->u.size = size;
145d021c344SAndy King break;
146d021c344SAndy King
147d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_OFFER:
148d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_ATTACH:
149d021c344SAndy King pkt->u.handle = handle;
150d021c344SAndy King break;
151d021c344SAndy King
152d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_WROTE:
153d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_READ:
154d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_RST:
155d021c344SAndy King pkt->u.size = 0;
156d021c344SAndy King break;
157d021c344SAndy King
158d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN:
159d021c344SAndy King pkt->u.mode = mode;
160d021c344SAndy King break;
161d021c344SAndy King
162d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ:
163d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE:
164d021c344SAndy King memcpy(&pkt->u.wait, wait, sizeof(pkt->u.wait));
165d021c344SAndy King break;
166d021c344SAndy King
167d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_REQUEST2:
168d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2:
169d021c344SAndy King pkt->u.size = size;
170d021c344SAndy King pkt->proto = proto;
171d021c344SAndy King break;
172d021c344SAndy King }
173d021c344SAndy King }
174d021c344SAndy King
175d021c344SAndy King static inline void
vmci_transport_packet_get_addresses(struct vmci_transport_packet * pkt,struct sockaddr_vm * local,struct sockaddr_vm * remote)176d021c344SAndy King vmci_transport_packet_get_addresses(struct vmci_transport_packet *pkt,
177d021c344SAndy King struct sockaddr_vm *local,
178d021c344SAndy King struct sockaddr_vm *remote)
179d021c344SAndy King {
180d021c344SAndy King vsock_addr_init(local, pkt->dg.dst.context, pkt->dst_port);
181d021c344SAndy King vsock_addr_init(remote, pkt->dg.src.context, pkt->src_port);
182d021c344SAndy King }
183d021c344SAndy King
184d021c344SAndy King static int
__vmci_transport_send_control_pkt(struct vmci_transport_packet * pkt,struct sockaddr_vm * src,struct sockaddr_vm * dst,enum vmci_transport_packet_type type,u64 size,u64 mode,struct vmci_transport_waiting_info * wait,u16 proto,struct vmci_handle handle,bool convert_error)185d021c344SAndy King __vmci_transport_send_control_pkt(struct vmci_transport_packet *pkt,
186d021c344SAndy King struct sockaddr_vm *src,
187d021c344SAndy King struct sockaddr_vm *dst,
188d021c344SAndy King enum vmci_transport_packet_type type,
189d021c344SAndy King u64 size,
190d021c344SAndy King u64 mode,
191d021c344SAndy King struct vmci_transport_waiting_info *wait,
192d021c344SAndy King u16 proto,
193d021c344SAndy King struct vmci_handle handle,
194d021c344SAndy King bool convert_error)
195d021c344SAndy King {
196d021c344SAndy King int err;
197d021c344SAndy King
198d021c344SAndy King vmci_transport_packet_init(pkt, src, dst, type, size, mode, wait,
199d021c344SAndy King proto, handle);
200d021c344SAndy King err = vmci_datagram_send(&pkt->dg);
201d021c344SAndy King if (convert_error && (err < 0))
202d021c344SAndy King return vmci_transport_error_to_vsock_error(err);
203d021c344SAndy King
204d021c344SAndy King return err;
205d021c344SAndy King }
206d021c344SAndy King
207d021c344SAndy King static int
vmci_transport_reply_control_pkt_fast(struct vmci_transport_packet * pkt,enum vmci_transport_packet_type type,u64 size,u64 mode,struct vmci_transport_waiting_info * wait,struct vmci_handle handle)208d021c344SAndy King vmci_transport_reply_control_pkt_fast(struct vmci_transport_packet *pkt,
209d021c344SAndy King enum vmci_transport_packet_type type,
210d021c344SAndy King u64 size,
211d021c344SAndy King u64 mode,
212d021c344SAndy King struct vmci_transport_waiting_info *wait,
213d021c344SAndy King struct vmci_handle handle)
214d021c344SAndy King {
215d021c344SAndy King struct vmci_transport_packet reply;
216d021c344SAndy King struct sockaddr_vm src, dst;
217d021c344SAndy King
218d021c344SAndy King if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST) {
219d021c344SAndy King return 0;
220d021c344SAndy King } else {
221d021c344SAndy King vmci_transport_packet_get_addresses(pkt, &src, &dst);
222d021c344SAndy King return __vmci_transport_send_control_pkt(&reply, &src, &dst,
223d021c344SAndy King type,
224d021c344SAndy King size, mode, wait,
225d021c344SAndy King VSOCK_PROTO_INVALID,
226d021c344SAndy King handle, true);
227d021c344SAndy King }
228d021c344SAndy King }
229d021c344SAndy King
230d021c344SAndy King static int
vmci_transport_send_control_pkt_bh(struct sockaddr_vm * src,struct sockaddr_vm * dst,enum vmci_transport_packet_type type,u64 size,u64 mode,struct vmci_transport_waiting_info * wait,struct vmci_handle handle)231d021c344SAndy King vmci_transport_send_control_pkt_bh(struct sockaddr_vm *src,
232d021c344SAndy King struct sockaddr_vm *dst,
233d021c344SAndy King enum vmci_transport_packet_type type,
234d021c344SAndy King u64 size,
235d021c344SAndy King u64 mode,
236d021c344SAndy King struct vmci_transport_waiting_info *wait,
237d021c344SAndy King struct vmci_handle handle)
238d021c344SAndy King {
239d021c344SAndy King /* Note that it is safe to use a single packet across all CPUs since
240d021c344SAndy King * two tasklets of the same type are guaranteed to not ever run
241d021c344SAndy King * simultaneously. If that ever changes, or VMCI stops using tasklets,
242d021c344SAndy King * we can use per-cpu packets.
243d021c344SAndy King */
244d021c344SAndy King static struct vmci_transport_packet pkt;
245d021c344SAndy King
246d021c344SAndy King return __vmci_transport_send_control_pkt(&pkt, src, dst, type,
247d021c344SAndy King size, mode, wait,
248d021c344SAndy King VSOCK_PROTO_INVALID, handle,
249d021c344SAndy King false);
250d021c344SAndy King }
251d021c344SAndy King
252d021c344SAndy King static int
vmci_transport_alloc_send_control_pkt(struct sockaddr_vm * src,struct sockaddr_vm * dst,enum vmci_transport_packet_type type,u64 size,u64 mode,struct vmci_transport_waiting_info * wait,u16 proto,struct vmci_handle handle)253a915b982SJorgen Hansen vmci_transport_alloc_send_control_pkt(struct sockaddr_vm *src,
254a915b982SJorgen Hansen struct sockaddr_vm *dst,
255d021c344SAndy King enum vmci_transport_packet_type type,
256d021c344SAndy King u64 size,
257d021c344SAndy King u64 mode,
258d021c344SAndy King struct vmci_transport_waiting_info *wait,
259d021c344SAndy King u16 proto,
260d021c344SAndy King struct vmci_handle handle)
261d021c344SAndy King {
262d021c344SAndy King struct vmci_transport_packet *pkt;
263d021c344SAndy King int err;
264d021c344SAndy King
265a915b982SJorgen Hansen pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
266a915b982SJorgen Hansen if (!pkt)
267a915b982SJorgen Hansen return -ENOMEM;
268a915b982SJorgen Hansen
269a915b982SJorgen Hansen err = __vmci_transport_send_control_pkt(pkt, src, dst, type, size,
270a915b982SJorgen Hansen mode, wait, proto, handle,
271a915b982SJorgen Hansen true);
272a915b982SJorgen Hansen kfree(pkt);
273a915b982SJorgen Hansen
274a915b982SJorgen Hansen return err;
275a915b982SJorgen Hansen }
276a915b982SJorgen Hansen
277a915b982SJorgen Hansen static int
vmci_transport_send_control_pkt(struct sock * sk,enum vmci_transport_packet_type type,u64 size,u64 mode,struct vmci_transport_waiting_info * wait,u16 proto,struct vmci_handle handle)278a915b982SJorgen Hansen vmci_transport_send_control_pkt(struct sock *sk,
279a915b982SJorgen Hansen enum vmci_transport_packet_type type,
280a915b982SJorgen Hansen u64 size,
281a915b982SJorgen Hansen u64 mode,
282a915b982SJorgen Hansen struct vmci_transport_waiting_info *wait,
283a915b982SJorgen Hansen u16 proto,
284a915b982SJorgen Hansen struct vmci_handle handle)
285a915b982SJorgen Hansen {
286a915b982SJorgen Hansen struct vsock_sock *vsk;
287a915b982SJorgen Hansen
288d021c344SAndy King vsk = vsock_sk(sk);
289d021c344SAndy King
290d021c344SAndy King if (!vsock_addr_bound(&vsk->local_addr))
291d021c344SAndy King return -EINVAL;
292d021c344SAndy King
293d021c344SAndy King if (!vsock_addr_bound(&vsk->remote_addr))
294d021c344SAndy King return -EINVAL;
295d021c344SAndy King
296a915b982SJorgen Hansen return vmci_transport_alloc_send_control_pkt(&vsk->local_addr,
297a915b982SJorgen Hansen &vsk->remote_addr,
298a915b982SJorgen Hansen type, size, mode,
299a915b982SJorgen Hansen wait, proto, handle);
300d021c344SAndy King }
301d021c344SAndy King
vmci_transport_send_reset_bh(struct sockaddr_vm * dst,struct sockaddr_vm * src,struct vmci_transport_packet * pkt)302d021c344SAndy King static int vmci_transport_send_reset_bh(struct sockaddr_vm *dst,
303d021c344SAndy King struct sockaddr_vm *src,
304d021c344SAndy King struct vmci_transport_packet *pkt)
305d021c344SAndy King {
306d021c344SAndy King if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST)
307d021c344SAndy King return 0;
308d021c344SAndy King return vmci_transport_send_control_pkt_bh(
309d021c344SAndy King dst, src,
310d021c344SAndy King VMCI_TRANSPORT_PACKET_TYPE_RST, 0,
311d021c344SAndy King 0, NULL, VMCI_INVALID_HANDLE);
312d021c344SAndy King }
313d021c344SAndy King
vmci_transport_send_reset(struct sock * sk,struct vmci_transport_packet * pkt)314d021c344SAndy King static int vmci_transport_send_reset(struct sock *sk,
315d021c344SAndy King struct vmci_transport_packet *pkt)
316d021c344SAndy King {
317a915b982SJorgen Hansen struct sockaddr_vm *dst_ptr;
318a915b982SJorgen Hansen struct sockaddr_vm dst;
319a915b982SJorgen Hansen struct vsock_sock *vsk;
320a915b982SJorgen Hansen
321d021c344SAndy King if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST)
322d021c344SAndy King return 0;
323a915b982SJorgen Hansen
324a915b982SJorgen Hansen vsk = vsock_sk(sk);
325a915b982SJorgen Hansen
326a915b982SJorgen Hansen if (!vsock_addr_bound(&vsk->local_addr))
327a915b982SJorgen Hansen return -EINVAL;
328a915b982SJorgen Hansen
329a915b982SJorgen Hansen if (vsock_addr_bound(&vsk->remote_addr)) {
330a915b982SJorgen Hansen dst_ptr = &vsk->remote_addr;
331a915b982SJorgen Hansen } else {
332a915b982SJorgen Hansen vsock_addr_init(&dst, pkt->dg.src.context,
333a915b982SJorgen Hansen pkt->src_port);
334a915b982SJorgen Hansen dst_ptr = &dst;
335a915b982SJorgen Hansen }
336a915b982SJorgen Hansen return vmci_transport_alloc_send_control_pkt(&vsk->local_addr, dst_ptr,
337d021c344SAndy King VMCI_TRANSPORT_PACKET_TYPE_RST,
338d021c344SAndy King 0, 0, NULL, VSOCK_PROTO_INVALID,
339d021c344SAndy King VMCI_INVALID_HANDLE);
340d021c344SAndy King }
341d021c344SAndy King
vmci_transport_send_negotiate(struct sock * sk,size_t size)342d021c344SAndy King static int vmci_transport_send_negotiate(struct sock *sk, size_t size)
343d021c344SAndy King {
344d021c344SAndy King return vmci_transport_send_control_pkt(
345d021c344SAndy King sk,
346d021c344SAndy King VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE,
347d021c344SAndy King size, 0, NULL,
348d021c344SAndy King VSOCK_PROTO_INVALID,
349d021c344SAndy King VMCI_INVALID_HANDLE);
350d021c344SAndy King }
351d021c344SAndy King
vmci_transport_send_negotiate2(struct sock * sk,size_t size,u16 version)352d021c344SAndy King static int vmci_transport_send_negotiate2(struct sock *sk, size_t size,
353d021c344SAndy King u16 version)
354d021c344SAndy King {
355d021c344SAndy King return vmci_transport_send_control_pkt(
356d021c344SAndy King sk,
357d021c344SAndy King VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2,
358d021c344SAndy King size, 0, NULL, version,
359d021c344SAndy King VMCI_INVALID_HANDLE);
360d021c344SAndy King }
361d021c344SAndy King
vmci_transport_send_qp_offer(struct sock * sk,struct vmci_handle handle)362d021c344SAndy King static int vmci_transport_send_qp_offer(struct sock *sk,
363d021c344SAndy King struct vmci_handle handle)
364d021c344SAndy King {
365d021c344SAndy King return vmci_transport_send_control_pkt(
366d021c344SAndy King sk, VMCI_TRANSPORT_PACKET_TYPE_OFFER, 0,
367d021c344SAndy King 0, NULL,
368d021c344SAndy King VSOCK_PROTO_INVALID, handle);
369d021c344SAndy King }
370d021c344SAndy King
vmci_transport_send_attach(struct sock * sk,struct vmci_handle handle)371d021c344SAndy King static int vmci_transport_send_attach(struct sock *sk,
372d021c344SAndy King struct vmci_handle handle)
373d021c344SAndy King {
374d021c344SAndy King return vmci_transport_send_control_pkt(
375d021c344SAndy King sk, VMCI_TRANSPORT_PACKET_TYPE_ATTACH,
376d021c344SAndy King 0, 0, NULL, VSOCK_PROTO_INVALID,
377d021c344SAndy King handle);
378d021c344SAndy King }
379d021c344SAndy King
vmci_transport_reply_reset(struct vmci_transport_packet * pkt)380d021c344SAndy King static int vmci_transport_reply_reset(struct vmci_transport_packet *pkt)
381d021c344SAndy King {
382d021c344SAndy King return vmci_transport_reply_control_pkt_fast(
383d021c344SAndy King pkt,
384d021c344SAndy King VMCI_TRANSPORT_PACKET_TYPE_RST,
385d021c344SAndy King 0, 0, NULL,
386d021c344SAndy King VMCI_INVALID_HANDLE);
387d021c344SAndy King }
388d021c344SAndy King
vmci_transport_send_invalid_bh(struct sockaddr_vm * dst,struct sockaddr_vm * src)389d021c344SAndy King static int vmci_transport_send_invalid_bh(struct sockaddr_vm *dst,
390d021c344SAndy King struct sockaddr_vm *src)
391d021c344SAndy King {
392d021c344SAndy King return vmci_transport_send_control_pkt_bh(
393d021c344SAndy King dst, src,
394d021c344SAndy King VMCI_TRANSPORT_PACKET_TYPE_INVALID,
395d021c344SAndy King 0, 0, NULL, VMCI_INVALID_HANDLE);
396d021c344SAndy King }
397d021c344SAndy King
vmci_transport_send_wrote_bh(struct sockaddr_vm * dst,struct sockaddr_vm * src)398d021c344SAndy King int vmci_transport_send_wrote_bh(struct sockaddr_vm *dst,
399d021c344SAndy King struct sockaddr_vm *src)
400d021c344SAndy King {
401d021c344SAndy King return vmci_transport_send_control_pkt_bh(
402d021c344SAndy King dst, src,
403d021c344SAndy King VMCI_TRANSPORT_PACKET_TYPE_WROTE, 0,
404d021c344SAndy King 0, NULL, VMCI_INVALID_HANDLE);
405d021c344SAndy King }
406d021c344SAndy King
vmci_transport_send_read_bh(struct sockaddr_vm * dst,struct sockaddr_vm * src)407d021c344SAndy King int vmci_transport_send_read_bh(struct sockaddr_vm *dst,
408d021c344SAndy King struct sockaddr_vm *src)
409d021c344SAndy King {
410d021c344SAndy King return vmci_transport_send_control_pkt_bh(
411d021c344SAndy King dst, src,
412d021c344SAndy King VMCI_TRANSPORT_PACKET_TYPE_READ, 0,
413d021c344SAndy King 0, NULL, VMCI_INVALID_HANDLE);
414d021c344SAndy King }
415d021c344SAndy King
vmci_transport_send_wrote(struct sock * sk)416d021c344SAndy King int vmci_transport_send_wrote(struct sock *sk)
417d021c344SAndy King {
418d021c344SAndy King return vmci_transport_send_control_pkt(
419d021c344SAndy King sk, VMCI_TRANSPORT_PACKET_TYPE_WROTE, 0,
420d021c344SAndy King 0, NULL, VSOCK_PROTO_INVALID,
421d021c344SAndy King VMCI_INVALID_HANDLE);
422d021c344SAndy King }
423d021c344SAndy King
vmci_transport_send_read(struct sock * sk)424d021c344SAndy King int vmci_transport_send_read(struct sock *sk)
425d021c344SAndy King {
426d021c344SAndy King return vmci_transport_send_control_pkt(
427d021c344SAndy King sk, VMCI_TRANSPORT_PACKET_TYPE_READ, 0,
428d021c344SAndy King 0, NULL, VSOCK_PROTO_INVALID,
429d021c344SAndy King VMCI_INVALID_HANDLE);
430d021c344SAndy King }
431d021c344SAndy King
vmci_transport_send_waiting_write(struct sock * sk,struct vmci_transport_waiting_info * wait)432d021c344SAndy King int vmci_transport_send_waiting_write(struct sock *sk,
433d021c344SAndy King struct vmci_transport_waiting_info *wait)
434d021c344SAndy King {
435d021c344SAndy King return vmci_transport_send_control_pkt(
436d021c344SAndy King sk, VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE,
437d021c344SAndy King 0, 0, wait, VSOCK_PROTO_INVALID,
438d021c344SAndy King VMCI_INVALID_HANDLE);
439d021c344SAndy King }
440d021c344SAndy King
vmci_transport_send_waiting_read(struct sock * sk,struct vmci_transport_waiting_info * wait)441d021c344SAndy King int vmci_transport_send_waiting_read(struct sock *sk,
442d021c344SAndy King struct vmci_transport_waiting_info *wait)
443d021c344SAndy King {
444d021c344SAndy King return vmci_transport_send_control_pkt(
445d021c344SAndy King sk, VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ,
446d021c344SAndy King 0, 0, wait, VSOCK_PROTO_INVALID,
447d021c344SAndy King VMCI_INVALID_HANDLE);
448d021c344SAndy King }
449d021c344SAndy King
vmci_transport_shutdown(struct vsock_sock * vsk,int mode)450d021c344SAndy King static int vmci_transport_shutdown(struct vsock_sock *vsk, int mode)
451d021c344SAndy King {
452d021c344SAndy King return vmci_transport_send_control_pkt(
453d021c344SAndy King &vsk->sk,
454d021c344SAndy King VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN,
455d021c344SAndy King 0, mode, NULL,
456d021c344SAndy King VSOCK_PROTO_INVALID,
457d021c344SAndy King VMCI_INVALID_HANDLE);
458d021c344SAndy King }
459d021c344SAndy King
vmci_transport_send_conn_request(struct sock * sk,size_t size)460d021c344SAndy King static int vmci_transport_send_conn_request(struct sock *sk, size_t size)
461d021c344SAndy King {
462d021c344SAndy King return vmci_transport_send_control_pkt(sk,
463d021c344SAndy King VMCI_TRANSPORT_PACKET_TYPE_REQUEST,
464d021c344SAndy King size, 0, NULL,
465d021c344SAndy King VSOCK_PROTO_INVALID,
466d021c344SAndy King VMCI_INVALID_HANDLE);
467d021c344SAndy King }
468d021c344SAndy King
vmci_transport_send_conn_request2(struct sock * sk,size_t size,u16 version)469d021c344SAndy King static int vmci_transport_send_conn_request2(struct sock *sk, size_t size,
470d021c344SAndy King u16 version)
471d021c344SAndy King {
472d021c344SAndy King return vmci_transport_send_control_pkt(
473d021c344SAndy King sk, VMCI_TRANSPORT_PACKET_TYPE_REQUEST2,
474d021c344SAndy King size, 0, NULL, version,
475d021c344SAndy King VMCI_INVALID_HANDLE);
476d021c344SAndy King }
477d021c344SAndy King
vmci_transport_get_pending(struct sock * listener,struct vmci_transport_packet * pkt)478d021c344SAndy King static struct sock *vmci_transport_get_pending(
479d021c344SAndy King struct sock *listener,
480d021c344SAndy King struct vmci_transport_packet *pkt)
481d021c344SAndy King {
482d021c344SAndy King struct vsock_sock *vlistener;
483d021c344SAndy King struct vsock_sock *vpending;
484d021c344SAndy King struct sock *pending;
485990454b5SReilly Grant struct sockaddr_vm src;
486990454b5SReilly Grant
487990454b5SReilly Grant vsock_addr_init(&src, pkt->dg.src.context, pkt->src_port);
488d021c344SAndy King
489d021c344SAndy King vlistener = vsock_sk(listener);
490d021c344SAndy King
491d021c344SAndy King list_for_each_entry(vpending, &vlistener->pending_links,
492d021c344SAndy King pending_links) {
493d021c344SAndy King if (vsock_addr_equals_addr(&src, &vpending->remote_addr) &&
494990454b5SReilly Grant pkt->dst_port == vpending->local_addr.svm_port) {
495d021c344SAndy King pending = sk_vsock(vpending);
496d021c344SAndy King sock_hold(pending);
497d021c344SAndy King goto found;
498d021c344SAndy King }
499d021c344SAndy King }
500d021c344SAndy King
501d021c344SAndy King pending = NULL;
502d021c344SAndy King found:
503d021c344SAndy King return pending;
504d021c344SAndy King
505d021c344SAndy King }
506d021c344SAndy King
vmci_transport_release_pending(struct sock * pending)507d021c344SAndy King static void vmci_transport_release_pending(struct sock *pending)
508d021c344SAndy King {
509d021c344SAndy King sock_put(pending);
510d021c344SAndy King }
511d021c344SAndy King
512d021c344SAndy King /* We allow two kinds of sockets to communicate with a restricted VM: 1)
513d021c344SAndy King * trusted sockets 2) sockets from applications running as the same user as the
514d021c344SAndy King * VM (this is only true for the host side and only when using hosted products)
515d021c344SAndy King */
516d021c344SAndy King
vmci_transport_is_trusted(struct vsock_sock * vsock,u32 peer_cid)517d021c344SAndy King static bool vmci_transport_is_trusted(struct vsock_sock *vsock, u32 peer_cid)
518d021c344SAndy King {
519d021c344SAndy King return vsock->trusted ||
520d021c344SAndy King vmci_is_context_owner(peer_cid, vsock->owner->uid);
521d021c344SAndy King }
522d021c344SAndy King
523d021c344SAndy King /* We allow sending datagrams to and receiving datagrams from a restricted VM
524d021c344SAndy King * only if it is trusted as described in vmci_transport_is_trusted.
525d021c344SAndy King */
526d021c344SAndy King
vmci_transport_allow_dgram(struct vsock_sock * vsock,u32 peer_cid)527d021c344SAndy King static bool vmci_transport_allow_dgram(struct vsock_sock *vsock, u32 peer_cid)
528d021c344SAndy King {
5292a89f924SReilly Grant if (VMADDR_CID_HYPERVISOR == peer_cid)
5302a89f924SReilly Grant return true;
5312a89f924SReilly Grant
532d021c344SAndy King if (vsock->cached_peer != peer_cid) {
533d021c344SAndy King vsock->cached_peer = peer_cid;
534d021c344SAndy King if (!vmci_transport_is_trusted(vsock, peer_cid) &&
535d021c344SAndy King (vmci_context_get_priv_flags(peer_cid) &
536d021c344SAndy King VMCI_PRIVILEGE_FLAG_RESTRICTED)) {
537d021c344SAndy King vsock->cached_peer_allow_dgram = false;
538d021c344SAndy King } else {
539d021c344SAndy King vsock->cached_peer_allow_dgram = true;
540d021c344SAndy King }
541d021c344SAndy King }
542d021c344SAndy King
543d021c344SAndy King return vsock->cached_peer_allow_dgram;
544d021c344SAndy King }
545d021c344SAndy King
546d021c344SAndy King static int
vmci_transport_queue_pair_alloc(struct vmci_qp ** qpair,struct vmci_handle * handle,u64 produce_size,u64 consume_size,u32 peer,u32 flags,bool trusted)547d021c344SAndy King vmci_transport_queue_pair_alloc(struct vmci_qp **qpair,
548d021c344SAndy King struct vmci_handle *handle,
549d021c344SAndy King u64 produce_size,
550d021c344SAndy King u64 consume_size,
551d021c344SAndy King u32 peer, u32 flags, bool trusted)
552d021c344SAndy King {
553d021c344SAndy King int err = 0;
554d021c344SAndy King
555d021c344SAndy King if (trusted) {
556d021c344SAndy King /* Try to allocate our queue pair as trusted. This will only
557d021c344SAndy King * work if vsock is running in the host.
558d021c344SAndy King */
559d021c344SAndy King
560d021c344SAndy King err = vmci_qpair_alloc(qpair, handle, produce_size,
561d021c344SAndy King consume_size,
562d021c344SAndy King peer, flags,
563d021c344SAndy King VMCI_PRIVILEGE_FLAG_TRUSTED);
564d021c344SAndy King if (err != VMCI_ERROR_NO_ACCESS)
565d021c344SAndy King goto out;
566d021c344SAndy King
567d021c344SAndy King }
568d021c344SAndy King
569d021c344SAndy King err = vmci_qpair_alloc(qpair, handle, produce_size, consume_size,
570d021c344SAndy King peer, flags, VMCI_NO_PRIVILEGE_FLAGS);
571d021c344SAndy King out:
572d021c344SAndy King if (err < 0) {
573e16edc99SStefano Garzarella pr_err_once("Could not attach to queue pair with %d\n", err);
574d021c344SAndy King err = vmci_transport_error_to_vsock_error(err);
575d021c344SAndy King }
576d021c344SAndy King
577d021c344SAndy King return err;
578d021c344SAndy King }
579d021c344SAndy King
580d021c344SAndy King static int
vmci_transport_datagram_create_hnd(u32 resource_id,u32 flags,vmci_datagram_recv_cb recv_cb,void * client_data,struct vmci_handle * out_handle)581d021c344SAndy King vmci_transport_datagram_create_hnd(u32 resource_id,
582d021c344SAndy King u32 flags,
583d021c344SAndy King vmci_datagram_recv_cb recv_cb,
584d021c344SAndy King void *client_data,
585d021c344SAndy King struct vmci_handle *out_handle)
586d021c344SAndy King {
587d021c344SAndy King int err = 0;
588d021c344SAndy King
589d021c344SAndy King /* Try to allocate our datagram handler as trusted. This will only work
590d021c344SAndy King * if vsock is running in the host.
591d021c344SAndy King */
592d021c344SAndy King
593d021c344SAndy King err = vmci_datagram_create_handle_priv(resource_id, flags,
594d021c344SAndy King VMCI_PRIVILEGE_FLAG_TRUSTED,
595d021c344SAndy King recv_cb,
596d021c344SAndy King client_data, out_handle);
597d021c344SAndy King
598d021c344SAndy King if (err == VMCI_ERROR_NO_ACCESS)
599d021c344SAndy King err = vmci_datagram_create_handle(resource_id, flags,
600d021c344SAndy King recv_cb, client_data,
601d021c344SAndy King out_handle);
602d021c344SAndy King
603d021c344SAndy King return err;
604d021c344SAndy King }
605d021c344SAndy King
606d021c344SAndy King /* This is invoked as part of a tasklet that's scheduled when the VMCI
607d021c344SAndy King * interrupt fires. This is run in bottom-half context and if it ever needs to
608d021c344SAndy King * sleep it should defer that work to a work queue.
609d021c344SAndy King */
610d021c344SAndy King
vmci_transport_recv_dgram_cb(void * data,struct vmci_datagram * dg)611d021c344SAndy King static int vmci_transport_recv_dgram_cb(void *data, struct vmci_datagram *dg)
612d021c344SAndy King {
613d021c344SAndy King struct sock *sk;
614d021c344SAndy King size_t size;
615d021c344SAndy King struct sk_buff *skb;
616d021c344SAndy King struct vsock_sock *vsk;
617d021c344SAndy King
618d021c344SAndy King sk = (struct sock *)data;
619d021c344SAndy King
620d021c344SAndy King /* This handler is privileged when this module is running on the host.
621d021c344SAndy King * We will get datagrams from all endpoints (even VMs that are in a
622d021c344SAndy King * restricted context). If we get one from a restricted context then
623d021c344SAndy King * the destination socket must be trusted.
624d021c344SAndy King *
625d021c344SAndy King * NOTE: We access the socket struct without holding the lock here.
626d021c344SAndy King * This is ok because the field we are interested is never modified
627d021c344SAndy King * outside of the create and destruct socket functions.
628d021c344SAndy King */
629d021c344SAndy King vsk = vsock_sk(sk);
630d021c344SAndy King if (!vmci_transport_allow_dgram(vsk, dg->src.context))
631d021c344SAndy King return VMCI_ERROR_NO_ACCESS;
632d021c344SAndy King
633d021c344SAndy King size = VMCI_DG_SIZE(dg);
634d021c344SAndy King
635d021c344SAndy King /* Attach the packet to the socket's receive queue as an sk_buff. */
636d021c344SAndy King skb = alloc_skb(size, GFP_ATOMIC);
637dce1a287SAsias He if (!skb)
638dce1a287SAsias He return VMCI_ERROR_NO_MEM;
639dce1a287SAsias He
640d021c344SAndy King /* sk_receive_skb() will do a sock_put(), so hold here. */
641d021c344SAndy King sock_hold(sk);
642d021c344SAndy King skb_put(skb, size);
643d021c344SAndy King memcpy(skb->data, dg, size);
644d021c344SAndy King sk_receive_skb(sk, skb, 0);
645d021c344SAndy King
646d021c344SAndy King return VMCI_SUCCESS;
647d021c344SAndy King }
648d021c344SAndy King
vmci_transport_stream_allow(u32 cid,u32 port)649d021c344SAndy King static bool vmci_transport_stream_allow(u32 cid, u32 port)
650d021c344SAndy King {
651d021c344SAndy King static const u32 non_socket_contexts[] = {
652ef343b35SStefano Garzarella VMADDR_CID_LOCAL,
653d021c344SAndy King };
654d021c344SAndy King int i;
655d021c344SAndy King
656d021c344SAndy King BUILD_BUG_ON(sizeof(cid) != sizeof(*non_socket_contexts));
657d021c344SAndy King
658d021c344SAndy King for (i = 0; i < ARRAY_SIZE(non_socket_contexts); i++) {
659d021c344SAndy King if (cid == non_socket_contexts[i])
660d021c344SAndy King return false;
661d021c344SAndy King }
662d021c344SAndy King
663d021c344SAndy King return true;
664d021c344SAndy King }
665d021c344SAndy King
666d021c344SAndy King /* This is invoked as part of a tasklet that's scheduled when the VMCI
667d021c344SAndy King * interrupt fires. This is run in bottom-half context but it defers most of
668d021c344SAndy King * its work to the packet handling work queue.
669d021c344SAndy King */
670d021c344SAndy King
vmci_transport_recv_stream_cb(void * data,struct vmci_datagram * dg)671d021c344SAndy King static int vmci_transport_recv_stream_cb(void *data, struct vmci_datagram *dg)
672d021c344SAndy King {
673d021c344SAndy King struct sock *sk;
674d021c344SAndy King struct sockaddr_vm dst;
675d021c344SAndy King struct sockaddr_vm src;
676d021c344SAndy King struct vmci_transport_packet *pkt;
677d021c344SAndy King struct vsock_sock *vsk;
678d021c344SAndy King bool bh_process_pkt;
679d021c344SAndy King int err;
680d021c344SAndy King
681d021c344SAndy King sk = NULL;
682d021c344SAndy King err = VMCI_SUCCESS;
683d021c344SAndy King bh_process_pkt = false;
684d021c344SAndy King
685d021c344SAndy King /* Ignore incoming packets from contexts without sockets, or resources
686d021c344SAndy King * that aren't vsock implementations.
687d021c344SAndy King */
688d021c344SAndy King
689d021c344SAndy King if (!vmci_transport_stream_allow(dg->src.context, -1)
6902a89f924SReilly Grant || vmci_transport_peer_rid(dg->src.context) != dg->src.resource)
691d021c344SAndy King return VMCI_ERROR_NO_ACCESS;
692d021c344SAndy King
693d021c344SAndy King if (VMCI_DG_SIZE(dg) < sizeof(*pkt))
694d021c344SAndy King /* Drop datagrams that do not contain full VSock packets. */
695d021c344SAndy King return VMCI_ERROR_INVALID_ARGS;
696d021c344SAndy King
697d021c344SAndy King pkt = (struct vmci_transport_packet *)dg;
698d021c344SAndy King
699d021c344SAndy King /* Find the socket that should handle this packet. First we look for a
700d021c344SAndy King * connected socket and if there is none we look for a socket bound to
701d021c344SAndy King * the destintation address.
702d021c344SAndy King */
703d021c344SAndy King vsock_addr_init(&src, pkt->dg.src.context, pkt->src_port);
704d021c344SAndy King vsock_addr_init(&dst, pkt->dg.dst.context, pkt->dst_port);
705d021c344SAndy King
706d021c344SAndy King sk = vsock_find_connected_socket(&src, &dst);
707d021c344SAndy King if (!sk) {
708d021c344SAndy King sk = vsock_find_bound_socket(&dst);
709d021c344SAndy King if (!sk) {
710d021c344SAndy King /* We could not find a socket for this specified
711d021c344SAndy King * address. If this packet is a RST, we just drop it.
712d021c344SAndy King * If it is another packet, we send a RST. Note that
713d021c344SAndy King * we do not send a RST reply to RSTs so that we do not
714d021c344SAndy King * continually send RSTs between two endpoints.
715d021c344SAndy King *
716d021c344SAndy King * Note that since this is a reply, dst is src and src
717d021c344SAndy King * is dst.
718d021c344SAndy King */
719d021c344SAndy King if (vmci_transport_send_reset_bh(&dst, &src, pkt) < 0)
720d021c344SAndy King pr_err("unable to send reset\n");
721d021c344SAndy King
722d021c344SAndy King err = VMCI_ERROR_NOT_FOUND;
723d021c344SAndy King goto out;
724d021c344SAndy King }
725d021c344SAndy King }
726d021c344SAndy King
727d021c344SAndy King /* If the received packet type is beyond all types known to this
728d021c344SAndy King * implementation, reply with an invalid message. Hopefully this will
729d021c344SAndy King * help when implementing backwards compatibility in the future.
730d021c344SAndy King */
731d021c344SAndy King if (pkt->type >= VMCI_TRANSPORT_PACKET_TYPE_MAX) {
732d021c344SAndy King vmci_transport_send_invalid_bh(&dst, &src);
733d021c344SAndy King err = VMCI_ERROR_INVALID_ARGS;
734d021c344SAndy King goto out;
735d021c344SAndy King }
736d021c344SAndy King
737d021c344SAndy King /* This handler is privileged when this module is running on the host.
738d021c344SAndy King * We will get datagram connect requests from all endpoints (even VMs
739d021c344SAndy King * that are in a restricted context). If we get one from a restricted
740d021c344SAndy King * context then the destination socket must be trusted.
741d021c344SAndy King *
742d021c344SAndy King * NOTE: We access the socket struct without holding the lock here.
743d021c344SAndy King * This is ok because the field we are interested is never modified
744d021c344SAndy King * outside of the create and destruct socket functions.
745d021c344SAndy King */
746d021c344SAndy King vsk = vsock_sk(sk);
747d021c344SAndy King if (!vmci_transport_allow_dgram(vsk, pkt->dg.src.context)) {
748d021c344SAndy King err = VMCI_ERROR_NO_ACCESS;
749d021c344SAndy King goto out;
750d021c344SAndy King }
751d021c344SAndy King
752d021c344SAndy King /* We do most everything in a work queue, but let's fast path the
753d021c344SAndy King * notification of reads and writes to help data transfer performance.
754d021c344SAndy King * We can only do this if there is no process context code executing
755d021c344SAndy King * for this socket since that may change the state.
756d021c344SAndy King */
757d021c344SAndy King bh_lock_sock(sk);
758d021c344SAndy King
759990454b5SReilly Grant if (!sock_owned_by_user(sk)) {
760990454b5SReilly Grant /* The local context ID may be out of date, update it. */
761990454b5SReilly Grant vsk->local_addr.svm_cid = dst.svm_cid;
762990454b5SReilly Grant
7633b4477d2SStefan Hajnoczi if (sk->sk_state == TCP_ESTABLISHED)
764d021c344SAndy King vmci_trans(vsk)->notify_ops->handle_notify_pkt(
765d021c344SAndy King sk, pkt, true, &dst, &src,
766d021c344SAndy King &bh_process_pkt);
767990454b5SReilly Grant }
768d021c344SAndy King
769d021c344SAndy King bh_unlock_sock(sk);
770d021c344SAndy King
771d021c344SAndy King if (!bh_process_pkt) {
772d021c344SAndy King struct vmci_transport_recv_pkt_info *recv_pkt_info;
773d021c344SAndy King
774d021c344SAndy King recv_pkt_info = kmalloc(sizeof(*recv_pkt_info), GFP_ATOMIC);
775d021c344SAndy King if (!recv_pkt_info) {
776d021c344SAndy King if (vmci_transport_send_reset_bh(&dst, &src, pkt) < 0)
777d021c344SAndy King pr_err("unable to send reset\n");
778d021c344SAndy King
779d021c344SAndy King err = VMCI_ERROR_NO_MEM;
780d021c344SAndy King goto out;
781d021c344SAndy King }
782d021c344SAndy King
783d021c344SAndy King recv_pkt_info->sk = sk;
784d021c344SAndy King memcpy(&recv_pkt_info->pkt, pkt, sizeof(recv_pkt_info->pkt));
785d021c344SAndy King INIT_WORK(&recv_pkt_info->work, vmci_transport_recv_pkt_work);
786d021c344SAndy King
787d021c344SAndy King schedule_work(&recv_pkt_info->work);
788d021c344SAndy King /* Clear sk so that the reference count incremented by one of
789d021c344SAndy King * the Find functions above is not decremented below. We need
790d021c344SAndy King * that reference count for the packet handler we've scheduled
791d021c344SAndy King * to run.
792d021c344SAndy King */
793d021c344SAndy King sk = NULL;
794d021c344SAndy King }
795d021c344SAndy King
796d021c344SAndy King out:
797d021c344SAndy King if (sk)
798d021c344SAndy King sock_put(sk);
799d021c344SAndy King
800d021c344SAndy King return err;
801d021c344SAndy King }
802d021c344SAndy King
vmci_transport_handle_detach(struct sock * sk)803d021c344SAndy King static void vmci_transport_handle_detach(struct sock *sk)
804d021c344SAndy King {
805d021c344SAndy King struct vsock_sock *vsk;
806d021c344SAndy King
807d021c344SAndy King vsk = vsock_sk(sk);
808d021c344SAndy King if (!vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle)) {
809d021c344SAndy King sock_set_flag(sk, SOCK_DONE);
810d021c344SAndy King
811d021c344SAndy King /* On a detach the peer will not be sending or receiving
812d021c344SAndy King * anymore.
813d021c344SAndy King */
814d021c344SAndy King vsk->peer_shutdown = SHUTDOWN_MASK;
815d021c344SAndy King
816d021c344SAndy King /* We should not be sending anymore since the peer won't be
817d021c344SAndy King * there to receive, but we can still receive if there is data
818afbea2cdSJorgen Hansen * left in our consume queue. If the local endpoint is a host,
819afbea2cdSJorgen Hansen * we can't call vsock_stream_has_data, since that may block,
820afbea2cdSJorgen Hansen * but a host endpoint can't read data once the VM has
821afbea2cdSJorgen Hansen * detached, so there is no available data in that case.
822d021c344SAndy King */
823afbea2cdSJorgen Hansen if (vsk->local_addr.svm_cid == VMADDR_CID_HOST ||
824afbea2cdSJorgen Hansen vsock_stream_has_data(vsk) <= 0) {
8253b4477d2SStefan Hajnoczi if (sk->sk_state == TCP_SYN_SENT) {
826d021c344SAndy King /* The peer may detach from a queue pair while
827d021c344SAndy King * we are still in the connecting state, i.e.,
828d021c344SAndy King * if the peer VM is killed after attaching to
829d021c344SAndy King * a queue pair, but before we complete the
830d021c344SAndy King * handshake. In that case, we treat the detach
831d021c344SAndy King * event like a reset.
832d021c344SAndy King */
833d021c344SAndy King
8344a5def7fSJorgen Hansen sk->sk_state = TCP_CLOSE;
835d021c344SAndy King sk->sk_err = ECONNRESET;
836e3ae2365SAlexander Aring sk_error_report(sk);
837d021c344SAndy King return;
838d021c344SAndy King }
8394a5def7fSJorgen Hansen sk->sk_state = TCP_CLOSE;
840d021c344SAndy King }
841d021c344SAndy King sk->sk_state_change(sk);
842d021c344SAndy King }
843d021c344SAndy King }
844d021c344SAndy King
vmci_transport_peer_detach_cb(u32 sub_id,const struct vmci_event_data * e_data,void * client_data)845d021c344SAndy King static void vmci_transport_peer_detach_cb(u32 sub_id,
846d021c344SAndy King const struct vmci_event_data *e_data,
847d021c344SAndy King void *client_data)
848d021c344SAndy King {
8494ef7ea91SJorgen Hansen struct vmci_transport *trans = client_data;
850d021c344SAndy King const struct vmci_event_payload_qp *e_payload;
851d021c344SAndy King
852d021c344SAndy King e_payload = vmci_event_data_const_payload(e_data);
853d021c344SAndy King
854d021c344SAndy King /* XXX This is lame, we should provide a way to lookup sockets by
855d021c344SAndy King * qp_handle.
856d021c344SAndy King */
8574ef7ea91SJorgen Hansen if (vmci_handle_is_invalid(e_payload->handle) ||
8588ab18d71SJorgen Hansen !vmci_handle_is_equal(trans->qp_handle, e_payload->handle))
8594ef7ea91SJorgen Hansen return;
860d021c344SAndy King
8614ef7ea91SJorgen Hansen /* We don't ask for delayed CBs when we subscribe to this event (we
8624ef7ea91SJorgen Hansen * pass 0 as flags to vmci_event_subscribe()). VMCI makes no
8634ef7ea91SJorgen Hansen * guarantees in that case about what context we might be running in,
8644ef7ea91SJorgen Hansen * so it could be BH or process, blockable or non-blockable. So we
8654ef7ea91SJorgen Hansen * need to account for all possible contexts here.
8664ef7ea91SJorgen Hansen */
8674ef7ea91SJorgen Hansen spin_lock_bh(&trans->lock);
8684ef7ea91SJorgen Hansen if (!trans->sk)
8694ef7ea91SJorgen Hansen goto out;
8704ef7ea91SJorgen Hansen
8714ef7ea91SJorgen Hansen /* Apart from here, trans->lock is only grabbed as part of sk destruct,
8724ef7ea91SJorgen Hansen * where trans->sk isn't locked.
8734ef7ea91SJorgen Hansen */
8744ef7ea91SJorgen Hansen bh_lock_sock(trans->sk);
8754ef7ea91SJorgen Hansen
8764ef7ea91SJorgen Hansen vmci_transport_handle_detach(trans->sk);
8774ef7ea91SJorgen Hansen
8784ef7ea91SJorgen Hansen bh_unlock_sock(trans->sk);
8794ef7ea91SJorgen Hansen out:
8804ef7ea91SJorgen Hansen spin_unlock_bh(&trans->lock);
881d021c344SAndy King }
882d021c344SAndy King
vmci_transport_qp_resumed_cb(u32 sub_id,const struct vmci_event_data * e_data,void * client_data)883d021c344SAndy King static void vmci_transport_qp_resumed_cb(u32 sub_id,
884d021c344SAndy King const struct vmci_event_data *e_data,
885d021c344SAndy King void *client_data)
886d021c344SAndy King {
8878e6ed963SJiyong Park vsock_for_each_connected_socket(&vmci_transport,
8888e6ed963SJiyong Park vmci_transport_handle_detach);
889d021c344SAndy King }
890d021c344SAndy King
vmci_transport_recv_pkt_work(struct work_struct * work)891d021c344SAndy King static void vmci_transport_recv_pkt_work(struct work_struct *work)
892d021c344SAndy King {
893d021c344SAndy King struct vmci_transport_recv_pkt_info *recv_pkt_info;
894d021c344SAndy King struct vmci_transport_packet *pkt;
895d021c344SAndy King struct sock *sk;
896d021c344SAndy King
897d021c344SAndy King recv_pkt_info =
898d021c344SAndy King container_of(work, struct vmci_transport_recv_pkt_info, work);
899d021c344SAndy King sk = recv_pkt_info->sk;
900d021c344SAndy King pkt = &recv_pkt_info->pkt;
901d021c344SAndy King
902d021c344SAndy King lock_sock(sk);
903d021c344SAndy King
904990454b5SReilly Grant /* The local context ID may be out of date. */
905990454b5SReilly Grant vsock_sk(sk)->local_addr.svm_cid = pkt->dg.dst.context;
906990454b5SReilly Grant
907d021c344SAndy King switch (sk->sk_state) {
9083b4477d2SStefan Hajnoczi case TCP_LISTEN:
909d021c344SAndy King vmci_transport_recv_listen(sk, pkt);
910d021c344SAndy King break;
9113b4477d2SStefan Hajnoczi case TCP_SYN_SENT:
912d021c344SAndy King /* Processing of pending connections for servers goes through
913d021c344SAndy King * the listening socket, so see vmci_transport_recv_listen()
914d021c344SAndy King * for that path.
915d021c344SAndy King */
916d021c344SAndy King vmci_transport_recv_connecting_client(sk, pkt);
917d021c344SAndy King break;
9183b4477d2SStefan Hajnoczi case TCP_ESTABLISHED:
919d021c344SAndy King vmci_transport_recv_connected(sk, pkt);
920d021c344SAndy King break;
921d021c344SAndy King default:
922d021c344SAndy King /* Because this function does not run in the same context as
923d021c344SAndy King * vmci_transport_recv_stream_cb it is possible that the
924d021c344SAndy King * socket has closed. We need to let the other side know or it
925d021c344SAndy King * could be sitting in a connect and hang forever. Send a
926d021c344SAndy King * reset to prevent that.
927d021c344SAndy King */
928d021c344SAndy King vmci_transport_send_reset(sk, pkt);
9290fc93246SAsias He break;
930d021c344SAndy King }
931d021c344SAndy King
932d021c344SAndy King release_sock(sk);
933d021c344SAndy King kfree(recv_pkt_info);
934d021c344SAndy King /* Release reference obtained in the stream callback when we fetched
935d021c344SAndy King * this socket out of the bound or connected list.
936d021c344SAndy King */
937d021c344SAndy King sock_put(sk);
938d021c344SAndy King }
939d021c344SAndy King
vmci_transport_recv_listen(struct sock * sk,struct vmci_transport_packet * pkt)940d021c344SAndy King static int vmci_transport_recv_listen(struct sock *sk,
941d021c344SAndy King struct vmci_transport_packet *pkt)
942d021c344SAndy King {
943d021c344SAndy King struct sock *pending;
944d021c344SAndy King struct vsock_sock *vpending;
945d021c344SAndy King int err;
946d021c344SAndy King u64 qp_size;
947d021c344SAndy King bool old_request = false;
948d021c344SAndy King bool old_pkt_proto = false;
949d021c344SAndy King
950d021c344SAndy King /* Because we are in the listen state, we could be receiving a packet
951d021c344SAndy King * for ourself or any previous connection requests that we received.
952d021c344SAndy King * If it's the latter, we try to find a socket in our list of pending
953d021c344SAndy King * connections and, if we do, call the appropriate handler for the
954454e7b13SJilin Yuan * state that socket is in. Otherwise we try to service the
955d021c344SAndy King * connection request.
956d021c344SAndy King */
957d021c344SAndy King pending = vmci_transport_get_pending(sk, pkt);
958d021c344SAndy King if (pending) {
959d021c344SAndy King lock_sock(pending);
960990454b5SReilly Grant
961990454b5SReilly Grant /* The local context ID may be out of date. */
962990454b5SReilly Grant vsock_sk(pending)->local_addr.svm_cid = pkt->dg.dst.context;
963990454b5SReilly Grant
964d021c344SAndy King switch (pending->sk_state) {
9653b4477d2SStefan Hajnoczi case TCP_SYN_SENT:
966d021c344SAndy King err = vmci_transport_recv_connecting_server(sk,
967d021c344SAndy King pending,
968d021c344SAndy King pkt);
969d021c344SAndy King break;
970d021c344SAndy King default:
971d021c344SAndy King vmci_transport_send_reset(pending, pkt);
972d021c344SAndy King err = -EINVAL;
973d021c344SAndy King }
974d021c344SAndy King
975d021c344SAndy King if (err < 0)
976d021c344SAndy King vsock_remove_pending(sk, pending);
977d021c344SAndy King
978d021c344SAndy King release_sock(pending);
979d021c344SAndy King vmci_transport_release_pending(pending);
980d021c344SAndy King
981d021c344SAndy King return err;
982d021c344SAndy King }
983d021c344SAndy King
984d021c344SAndy King /* The listen state only accepts connection requests. Reply with a
985d021c344SAndy King * reset unless we received a reset.
986d021c344SAndy King */
987d021c344SAndy King
988d021c344SAndy King if (!(pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST ||
989d021c344SAndy King pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST2)) {
990d021c344SAndy King vmci_transport_reply_reset(pkt);
991d021c344SAndy King return -EINVAL;
992d021c344SAndy King }
993d021c344SAndy King
994d021c344SAndy King if (pkt->u.size == 0) {
995d021c344SAndy King vmci_transport_reply_reset(pkt);
996d021c344SAndy King return -EINVAL;
997d021c344SAndy King }
998d021c344SAndy King
999d021c344SAndy King /* If this socket can't accommodate this connection request, we send a
1000d021c344SAndy King * reset. Otherwise we create and initialize a child socket and reply
1001d021c344SAndy King * with a connection negotiation.
1002d021c344SAndy King */
1003d021c344SAndy King if (sk->sk_ack_backlog >= sk->sk_max_ack_backlog) {
1004d021c344SAndy King vmci_transport_reply_reset(pkt);
1005d021c344SAndy King return -ECONNREFUSED;
1006d021c344SAndy King }
1007d021c344SAndy King
1008b9ca2f5fSStefano Garzarella pending = vsock_create_connected(sk);
1009d021c344SAndy King if (!pending) {
1010d021c344SAndy King vmci_transport_send_reset(sk, pkt);
1011d021c344SAndy King return -ENOMEM;
1012d021c344SAndy King }
1013d021c344SAndy King
1014d021c344SAndy King vpending = vsock_sk(pending);
1015d021c344SAndy King
1016d021c344SAndy King vsock_addr_init(&vpending->local_addr, pkt->dg.dst.context,
1017d021c344SAndy King pkt->dst_port);
1018d021c344SAndy King vsock_addr_init(&vpending->remote_addr, pkt->dg.src.context,
1019d021c344SAndy King pkt->src_port);
1020d021c344SAndy King
1021c0cfa2d8SStefano Garzarella err = vsock_assign_transport(vpending, vsock_sk(sk));
1022c0cfa2d8SStefano Garzarella /* Transport assigned (looking at remote_addr) must be the same
1023c0cfa2d8SStefano Garzarella * where we received the request.
1024c0cfa2d8SStefano Garzarella */
1025c0cfa2d8SStefano Garzarella if (err || !vmci_check_transport(vpending)) {
1026c0cfa2d8SStefano Garzarella vmci_transport_send_reset(sk, pkt);
1027c0cfa2d8SStefano Garzarella sock_put(pending);
1028c0cfa2d8SStefano Garzarella return err;
1029c0cfa2d8SStefano Garzarella }
1030c0cfa2d8SStefano Garzarella
1031d021c344SAndy King /* If the proposed size fits within our min/max, accept it. Otherwise
1032d021c344SAndy King * propose our own size.
1033d021c344SAndy King */
1034b9f2b0ffSStefano Garzarella if (pkt->u.size >= vpending->buffer_min_size &&
1035b9f2b0ffSStefano Garzarella pkt->u.size <= vpending->buffer_max_size) {
1036d021c344SAndy King qp_size = pkt->u.size;
1037d021c344SAndy King } else {
1038b9f2b0ffSStefano Garzarella qp_size = vpending->buffer_size;
1039d021c344SAndy King }
1040d021c344SAndy King
1041d021c344SAndy King /* Figure out if we are using old or new requests based on the
1042d021c344SAndy King * overrides pkt types sent by our peer.
1043d021c344SAndy King */
1044d021c344SAndy King if (vmci_transport_old_proto_override(&old_pkt_proto)) {
1045d021c344SAndy King old_request = old_pkt_proto;
1046d021c344SAndy King } else {
1047d021c344SAndy King if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST)
1048d021c344SAndy King old_request = true;
1049d021c344SAndy King else if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST2)
1050d021c344SAndy King old_request = false;
1051d021c344SAndy King
1052d021c344SAndy King }
1053d021c344SAndy King
1054d021c344SAndy King if (old_request) {
1055d021c344SAndy King /* Handle a REQUEST (or override) */
1056d021c344SAndy King u16 version = VSOCK_PROTO_INVALID;
1057d021c344SAndy King if (vmci_transport_proto_to_notify_struct(
1058d021c344SAndy King pending, &version, true))
1059d021c344SAndy King err = vmci_transport_send_negotiate(pending, qp_size);
1060d021c344SAndy King else
1061d021c344SAndy King err = -EINVAL;
1062d021c344SAndy King
1063d021c344SAndy King } else {
1064d021c344SAndy King /* Handle a REQUEST2 (or override) */
1065d021c344SAndy King int proto_int = pkt->proto;
1066d021c344SAndy King int pos;
1067d021c344SAndy King u16 active_proto_version = 0;
1068d021c344SAndy King
1069d021c344SAndy King /* The list of possible protocols is the intersection of all
1070d021c344SAndy King * protocols the client supports ... plus all the protocols we
1071d021c344SAndy King * support.
1072d021c344SAndy King */
1073d021c344SAndy King proto_int &= vmci_transport_new_proto_supported_versions();
1074d021c344SAndy King
1075d021c344SAndy King /* We choose the highest possible protocol version and use that
1076d021c344SAndy King * one.
1077d021c344SAndy King */
1078d021c344SAndy King pos = fls(proto_int);
1079d021c344SAndy King if (pos) {
1080d021c344SAndy King active_proto_version = (1 << (pos - 1));
1081d021c344SAndy King if (vmci_transport_proto_to_notify_struct(
1082d021c344SAndy King pending, &active_proto_version, false))
1083d021c344SAndy King err = vmci_transport_send_negotiate2(pending,
1084d021c344SAndy King qp_size,
1085d021c344SAndy King active_proto_version);
1086d021c344SAndy King else
1087d021c344SAndy King err = -EINVAL;
1088d021c344SAndy King
1089d021c344SAndy King } else {
1090d021c344SAndy King err = -EINVAL;
1091d021c344SAndy King }
1092d021c344SAndy King }
1093d021c344SAndy King
1094d021c344SAndy King if (err < 0) {
1095d021c344SAndy King vmci_transport_send_reset(sk, pkt);
1096d021c344SAndy King sock_put(pending);
1097d021c344SAndy King err = vmci_transport_error_to_vsock_error(err);
1098d021c344SAndy King goto out;
1099d021c344SAndy King }
1100d021c344SAndy King
1101d021c344SAndy King vsock_add_pending(sk, pending);
11027976a11bSEric Dumazet sk_acceptq_added(sk);
1103d021c344SAndy King
11043b4477d2SStefan Hajnoczi pending->sk_state = TCP_SYN_SENT;
1105d021c344SAndy King vmci_trans(vpending)->produce_size =
1106d021c344SAndy King vmci_trans(vpending)->consume_size = qp_size;
1107b9f2b0ffSStefano Garzarella vpending->buffer_size = qp_size;
1108d021c344SAndy King
1109d021c344SAndy King vmci_trans(vpending)->notify_ops->process_request(pending);
1110d021c344SAndy King
1111d021c344SAndy King /* We might never receive another message for this socket and it's not
1112d021c344SAndy King * connected to any process, so we have to ensure it gets cleaned up
1113d021c344SAndy King * ourself. Our delayed work function will take care of that. Note
1114d021c344SAndy King * that we do not ever cancel this function since we have few
1115d021c344SAndy King * guarantees about its state when calling cancel_delayed_work().
1116d021c344SAndy King * Instead we hold a reference on the socket for that function and make
1117d021c344SAndy King * it capable of handling cases where it needs to do nothing but
1118d021c344SAndy King * release that reference.
1119d021c344SAndy King */
1120d021c344SAndy King vpending->listener = sk;
1121d021c344SAndy King sock_hold(sk);
1122d021c344SAndy King sock_hold(pending);
1123455f05ecSCong Wang schedule_delayed_work(&vpending->pending_work, HZ);
1124d021c344SAndy King
1125d021c344SAndy King out:
1126d021c344SAndy King return err;
1127d021c344SAndy King }
1128d021c344SAndy King
1129d021c344SAndy King static int
vmci_transport_recv_connecting_server(struct sock * listener,struct sock * pending,struct vmci_transport_packet * pkt)1130d021c344SAndy King vmci_transport_recv_connecting_server(struct sock *listener,
1131d021c344SAndy King struct sock *pending,
1132d021c344SAndy King struct vmci_transport_packet *pkt)
1133d021c344SAndy King {
1134d021c344SAndy King struct vsock_sock *vpending;
1135d021c344SAndy King struct vmci_handle handle;
1136d021c344SAndy King struct vmci_qp *qpair;
1137d021c344SAndy King bool is_local;
1138d021c344SAndy King u32 flags;
1139d021c344SAndy King u32 detach_sub_id;
1140d021c344SAndy King int err;
1141d021c344SAndy King int skerr;
1142d021c344SAndy King
1143d021c344SAndy King vpending = vsock_sk(pending);
1144d021c344SAndy King detach_sub_id = VMCI_INVALID_ID;
1145d021c344SAndy King
1146d021c344SAndy King switch (pkt->type) {
1147d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_OFFER:
1148d021c344SAndy King if (vmci_handle_is_invalid(pkt->u.handle)) {
1149d021c344SAndy King vmci_transport_send_reset(pending, pkt);
1150d021c344SAndy King skerr = EPROTO;
1151d021c344SAndy King err = -EINVAL;
1152d021c344SAndy King goto destroy;
1153d021c344SAndy King }
1154d021c344SAndy King break;
1155d021c344SAndy King default:
1156d021c344SAndy King /* Close and cleanup the connection. */
1157d021c344SAndy King vmci_transport_send_reset(pending, pkt);
1158d021c344SAndy King skerr = EPROTO;
1159d021c344SAndy King err = pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST ? 0 : -EINVAL;
1160d021c344SAndy King goto destroy;
1161d021c344SAndy King }
1162d021c344SAndy King
1163d021c344SAndy King /* In order to complete the connection we need to attach to the offered
1164d021c344SAndy King * queue pair and send an attach notification. We also subscribe to the
1165d021c344SAndy King * detach event so we know when our peer goes away, and we do that
1166d021c344SAndy King * before attaching so we don't miss an event. If all this succeeds,
1167d021c344SAndy King * we update our state and wakeup anything waiting in accept() for a
1168d021c344SAndy King * connection.
1169d021c344SAndy King */
1170d021c344SAndy King
1171d021c344SAndy King /* We don't care about attach since we ensure the other side has
1172d021c344SAndy King * attached by specifying the ATTACH_ONLY flag below.
1173d021c344SAndy King */
1174d021c344SAndy King err = vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH,
1175d021c344SAndy King vmci_transport_peer_detach_cb,
11764ef7ea91SJorgen Hansen vmci_trans(vpending), &detach_sub_id);
1177d021c344SAndy King if (err < VMCI_SUCCESS) {
1178d021c344SAndy King vmci_transport_send_reset(pending, pkt);
1179d021c344SAndy King err = vmci_transport_error_to_vsock_error(err);
1180d021c344SAndy King skerr = -err;
1181d021c344SAndy King goto destroy;
1182d021c344SAndy King }
1183d021c344SAndy King
1184d021c344SAndy King vmci_trans(vpending)->detach_sub_id = detach_sub_id;
1185d021c344SAndy King
1186d021c344SAndy King /* Now attach to the queue pair the client created. */
1187d021c344SAndy King handle = pkt->u.handle;
1188d021c344SAndy King
1189d021c344SAndy King /* vpending->local_addr always has a context id so we do not need to
1190d021c344SAndy King * worry about VMADDR_CID_ANY in this case.
1191d021c344SAndy King */
1192d021c344SAndy King is_local =
1193d021c344SAndy King vpending->remote_addr.svm_cid == vpending->local_addr.svm_cid;
1194d021c344SAndy King flags = VMCI_QPFLAG_ATTACH_ONLY;
1195d021c344SAndy King flags |= is_local ? VMCI_QPFLAG_LOCAL : 0;
1196d021c344SAndy King
1197d021c344SAndy King err = vmci_transport_queue_pair_alloc(
1198d021c344SAndy King &qpair,
1199d021c344SAndy King &handle,
1200d021c344SAndy King vmci_trans(vpending)->produce_size,
1201d021c344SAndy King vmci_trans(vpending)->consume_size,
1202d021c344SAndy King pkt->dg.src.context,
1203d021c344SAndy King flags,
1204d021c344SAndy King vmci_transport_is_trusted(
1205d021c344SAndy King vpending,
1206d021c344SAndy King vpending->remote_addr.svm_cid));
1207d021c344SAndy King if (err < 0) {
1208d021c344SAndy King vmci_transport_send_reset(pending, pkt);
1209d021c344SAndy King skerr = -err;
1210d021c344SAndy King goto destroy;
1211d021c344SAndy King }
1212d021c344SAndy King
1213d021c344SAndy King vmci_trans(vpending)->qp_handle = handle;
1214d021c344SAndy King vmci_trans(vpending)->qpair = qpair;
1215d021c344SAndy King
1216d021c344SAndy King /* When we send the attach message, we must be ready to handle incoming
1217d021c344SAndy King * control messages on the newly connected socket. So we move the
1218d021c344SAndy King * pending socket to the connected state before sending the attach
1219d021c344SAndy King * message. Otherwise, an incoming packet triggered by the attach being
1220d021c344SAndy King * received by the peer may be processed concurrently with what happens
1221d021c344SAndy King * below after sending the attach message, and that incoming packet
1222d021c344SAndy King * will find the listening socket instead of the (currently) pending
1223d021c344SAndy King * socket. Note that enqueueing the socket increments the reference
1224d021c344SAndy King * count, so even if a reset comes before the connection is accepted,
1225d021c344SAndy King * the socket will be valid until it is removed from the queue.
1226d021c344SAndy King *
1227d021c344SAndy King * If we fail sending the attach below, we remove the socket from the
12283b4477d2SStefan Hajnoczi * connected list and move the socket to TCP_CLOSE before
1229d021c344SAndy King * releasing the lock, so a pending slow path processing of an incoming
1230d021c344SAndy King * packet will not see the socket in the connected state in that case.
1231d021c344SAndy King */
12323b4477d2SStefan Hajnoczi pending->sk_state = TCP_ESTABLISHED;
1233d021c344SAndy King
1234d021c344SAndy King vsock_insert_connected(vpending);
1235d021c344SAndy King
1236d021c344SAndy King /* Notify our peer of our attach. */
1237d021c344SAndy King err = vmci_transport_send_attach(pending, handle);
1238d021c344SAndy King if (err < 0) {
1239d021c344SAndy King vsock_remove_connected(vpending);
1240d021c344SAndy King pr_err("Could not send attach\n");
1241d021c344SAndy King vmci_transport_send_reset(pending, pkt);
1242d021c344SAndy King err = vmci_transport_error_to_vsock_error(err);
1243d021c344SAndy King skerr = -err;
1244d021c344SAndy King goto destroy;
1245d021c344SAndy King }
1246d021c344SAndy King
1247d021c344SAndy King /* We have a connection. Move the now connected socket from the
1248d021c344SAndy King * listener's pending list to the accept queue so callers of accept()
1249d021c344SAndy King * can find it.
1250d021c344SAndy King */
1251d021c344SAndy King vsock_remove_pending(listener, pending);
1252d021c344SAndy King vsock_enqueue_accept(listener, pending);
1253d021c344SAndy King
125426855150Sgushengxian /* Callers of accept() will be waiting on the listening socket, not
1255d021c344SAndy King * the pending socket.
1256d021c344SAndy King */
12577362945aSStefan Hajnoczi listener->sk_data_ready(listener);
1258d021c344SAndy King
1259d021c344SAndy King return 0;
1260d021c344SAndy King
1261d021c344SAndy King destroy:
1262d021c344SAndy King pending->sk_err = skerr;
12633b4477d2SStefan Hajnoczi pending->sk_state = TCP_CLOSE;
1264d021c344SAndy King /* As long as we drop our reference, all necessary cleanup will handle
1265d021c344SAndy King * when the cleanup function drops its reference and our destruct
1266d021c344SAndy King * implementation is called. Note that since the listen handler will
1267d021c344SAndy King * remove pending from the pending list upon our failure, the cleanup
1268d021c344SAndy King * function won't drop the additional reference, which is why we do it
1269d021c344SAndy King * here.
1270d021c344SAndy King */
1271d021c344SAndy King sock_put(pending);
1272d021c344SAndy King
1273d021c344SAndy King return err;
1274d021c344SAndy King }
1275d021c344SAndy King
1276d021c344SAndy King static int
vmci_transport_recv_connecting_client(struct sock * sk,struct vmci_transport_packet * pkt)1277d021c344SAndy King vmci_transport_recv_connecting_client(struct sock *sk,
1278d021c344SAndy King struct vmci_transport_packet *pkt)
1279d021c344SAndy King {
1280d021c344SAndy King struct vsock_sock *vsk;
1281d021c344SAndy King int err;
1282d021c344SAndy King int skerr;
1283d021c344SAndy King
1284d021c344SAndy King vsk = vsock_sk(sk);
1285d021c344SAndy King
1286d021c344SAndy King switch (pkt->type) {
1287d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_ATTACH:
1288d021c344SAndy King if (vmci_handle_is_invalid(pkt->u.handle) ||
1289d021c344SAndy King !vmci_handle_is_equal(pkt->u.handle,
1290d021c344SAndy King vmci_trans(vsk)->qp_handle)) {
1291d021c344SAndy King skerr = EPROTO;
1292d021c344SAndy King err = -EINVAL;
1293d021c344SAndy King goto destroy;
1294d021c344SAndy King }
1295d021c344SAndy King
1296d021c344SAndy King /* Signify the socket is connected and wakeup the waiter in
1297d021c344SAndy King * connect(). Also place the socket in the connected table for
1298d021c344SAndy King * accounting (it can already be found since it's in the bound
1299d021c344SAndy King * table).
1300d021c344SAndy King */
13013b4477d2SStefan Hajnoczi sk->sk_state = TCP_ESTABLISHED;
1302d021c344SAndy King sk->sk_socket->state = SS_CONNECTED;
1303d021c344SAndy King vsock_insert_connected(vsk);
1304d021c344SAndy King sk->sk_state_change(sk);
1305d021c344SAndy King
1306d021c344SAndy King break;
1307d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE:
1308d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2:
1309d021c344SAndy King if (pkt->u.size == 0
1310d021c344SAndy King || pkt->dg.src.context != vsk->remote_addr.svm_cid
1311d021c344SAndy King || pkt->src_port != vsk->remote_addr.svm_port
1312d021c344SAndy King || !vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle)
1313d021c344SAndy King || vmci_trans(vsk)->qpair
1314d021c344SAndy King || vmci_trans(vsk)->produce_size != 0
1315d021c344SAndy King || vmci_trans(vsk)->consume_size != 0
1316d021c344SAndy King || vmci_trans(vsk)->detach_sub_id != VMCI_INVALID_ID) {
1317d021c344SAndy King skerr = EPROTO;
1318d021c344SAndy King err = -EINVAL;
1319d021c344SAndy King
1320d021c344SAndy King goto destroy;
1321d021c344SAndy King }
1322d021c344SAndy King
1323d021c344SAndy King err = vmci_transport_recv_connecting_client_negotiate(sk, pkt);
1324d021c344SAndy King if (err) {
1325d021c344SAndy King skerr = -err;
1326d021c344SAndy King goto destroy;
1327d021c344SAndy King }
1328d021c344SAndy King
1329d021c344SAndy King break;
1330d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_INVALID:
1331d021c344SAndy King err = vmci_transport_recv_connecting_client_invalid(sk, pkt);
1332d021c344SAndy King if (err) {
1333d021c344SAndy King skerr = -err;
1334d021c344SAndy King goto destroy;
1335d021c344SAndy King }
1336d021c344SAndy King
1337d021c344SAndy King break;
1338d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_RST:
1339d021c344SAndy King /* Older versions of the linux code (WS 6.5 / ESX 4.0) used to
1340d021c344SAndy King * continue processing here after they sent an INVALID packet.
1341d021c344SAndy King * This meant that we got a RST after the INVALID. We ignore a
1342d021c344SAndy King * RST after an INVALID. The common code doesn't send the RST
1343d021c344SAndy King * ... so we can hang if an old version of the common code
1344d021c344SAndy King * fails between getting a REQUEST and sending an OFFER back.
1345d021c344SAndy King * Not much we can do about it... except hope that it doesn't
1346d021c344SAndy King * happen.
1347d021c344SAndy King */
1348d021c344SAndy King if (vsk->ignore_connecting_rst) {
1349d021c344SAndy King vsk->ignore_connecting_rst = false;
1350d021c344SAndy King } else {
1351d021c344SAndy King skerr = ECONNRESET;
1352d021c344SAndy King err = 0;
1353d021c344SAndy King goto destroy;
1354d021c344SAndy King }
1355d021c344SAndy King
1356d021c344SAndy King break;
1357d021c344SAndy King default:
1358d021c344SAndy King /* Close and cleanup the connection. */
1359d021c344SAndy King skerr = EPROTO;
1360d021c344SAndy King err = -EINVAL;
1361d021c344SAndy King goto destroy;
1362d021c344SAndy King }
1363d021c344SAndy King
1364d021c344SAndy King return 0;
1365d021c344SAndy King
1366d021c344SAndy King destroy:
1367d021c344SAndy King vmci_transport_send_reset(sk, pkt);
1368d021c344SAndy King
13693b4477d2SStefan Hajnoczi sk->sk_state = TCP_CLOSE;
1370d021c344SAndy King sk->sk_err = skerr;
1371e3ae2365SAlexander Aring sk_error_report(sk);
1372d021c344SAndy King return err;
1373d021c344SAndy King }
1374d021c344SAndy King
vmci_transport_recv_connecting_client_negotiate(struct sock * sk,struct vmci_transport_packet * pkt)1375d021c344SAndy King static int vmci_transport_recv_connecting_client_negotiate(
1376d021c344SAndy King struct sock *sk,
1377d021c344SAndy King struct vmci_transport_packet *pkt)
1378d021c344SAndy King {
1379d021c344SAndy King int err;
1380d021c344SAndy King struct vsock_sock *vsk;
1381d021c344SAndy King struct vmci_handle handle;
1382d021c344SAndy King struct vmci_qp *qpair;
1383d021c344SAndy King u32 detach_sub_id;
1384d021c344SAndy King bool is_local;
1385d021c344SAndy King u32 flags;
1386d021c344SAndy King bool old_proto = true;
1387d021c344SAndy King bool old_pkt_proto;
1388d021c344SAndy King u16 version;
1389d021c344SAndy King
1390d021c344SAndy King vsk = vsock_sk(sk);
1391d021c344SAndy King handle = VMCI_INVALID_HANDLE;
1392d021c344SAndy King detach_sub_id = VMCI_INVALID_ID;
1393d021c344SAndy King
1394d021c344SAndy King /* If we have gotten here then we should be past the point where old
1395d021c344SAndy King * linux vsock could have sent the bogus rst.
1396d021c344SAndy King */
1397d021c344SAndy King vsk->sent_request = false;
1398d021c344SAndy King vsk->ignore_connecting_rst = false;
1399d021c344SAndy King
1400d021c344SAndy King /* Verify that we're OK with the proposed queue pair size */
1401b9f2b0ffSStefano Garzarella if (pkt->u.size < vsk->buffer_min_size ||
1402b9f2b0ffSStefano Garzarella pkt->u.size > vsk->buffer_max_size) {
1403d021c344SAndy King err = -EINVAL;
1404d021c344SAndy King goto destroy;
1405d021c344SAndy King }
1406d021c344SAndy King
1407d021c344SAndy King /* At this point we know the CID the peer is using to talk to us. */
1408d021c344SAndy King
1409d021c344SAndy King if (vsk->local_addr.svm_cid == VMADDR_CID_ANY)
1410d021c344SAndy King vsk->local_addr.svm_cid = pkt->dg.dst.context;
1411d021c344SAndy King
1412d021c344SAndy King /* Setup the notify ops to be the highest supported version that both
1413d021c344SAndy King * the server and the client support.
1414d021c344SAndy King */
1415d021c344SAndy King
1416d021c344SAndy King if (vmci_transport_old_proto_override(&old_pkt_proto)) {
1417d021c344SAndy King old_proto = old_pkt_proto;
1418d021c344SAndy King } else {
1419d021c344SAndy King if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE)
1420d021c344SAndy King old_proto = true;
1421d021c344SAndy King else if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2)
1422d021c344SAndy King old_proto = false;
1423d021c344SAndy King
1424d021c344SAndy King }
1425d021c344SAndy King
1426d021c344SAndy King if (old_proto)
1427d021c344SAndy King version = VSOCK_PROTO_INVALID;
1428d021c344SAndy King else
1429d021c344SAndy King version = pkt->proto;
1430d021c344SAndy King
1431d021c344SAndy King if (!vmci_transport_proto_to_notify_struct(sk, &version, old_proto)) {
1432d021c344SAndy King err = -EINVAL;
1433d021c344SAndy King goto destroy;
1434d021c344SAndy King }
1435d021c344SAndy King
14364ef7ea91SJorgen Hansen /* Subscribe to detach events first.
1437d021c344SAndy King *
1438d021c344SAndy King * XXX We attach once for each queue pair created for now so it is easy
1439d021c344SAndy King * to find the socket (it's provided), but later we should only
1440d021c344SAndy King * subscribe once and add a way to lookup sockets by queue pair handle.
1441d021c344SAndy King */
1442d021c344SAndy King err = vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH,
1443d021c344SAndy King vmci_transport_peer_detach_cb,
14444ef7ea91SJorgen Hansen vmci_trans(vsk), &detach_sub_id);
1445d021c344SAndy King if (err < VMCI_SUCCESS) {
1446d021c344SAndy King err = vmci_transport_error_to_vsock_error(err);
1447d021c344SAndy King goto destroy;
1448d021c344SAndy King }
1449d021c344SAndy King
1450d021c344SAndy King /* Make VMCI select the handle for us. */
1451d021c344SAndy King handle = VMCI_INVALID_HANDLE;
1452d021c344SAndy King is_local = vsk->remote_addr.svm_cid == vsk->local_addr.svm_cid;
1453d021c344SAndy King flags = is_local ? VMCI_QPFLAG_LOCAL : 0;
1454d021c344SAndy King
1455d021c344SAndy King err = vmci_transport_queue_pair_alloc(&qpair,
1456d021c344SAndy King &handle,
1457d021c344SAndy King pkt->u.size,
1458d021c344SAndy King pkt->u.size,
1459d021c344SAndy King vsk->remote_addr.svm_cid,
1460d021c344SAndy King flags,
1461d021c344SAndy King vmci_transport_is_trusted(
1462d021c344SAndy King vsk,
1463d021c344SAndy King vsk->
1464d021c344SAndy King remote_addr.svm_cid));
1465d021c344SAndy King if (err < 0)
1466d021c344SAndy King goto destroy;
1467d021c344SAndy King
1468d021c344SAndy King err = vmci_transport_send_qp_offer(sk, handle);
1469d021c344SAndy King if (err < 0) {
1470d021c344SAndy King err = vmci_transport_error_to_vsock_error(err);
1471d021c344SAndy King goto destroy;
1472d021c344SAndy King }
1473d021c344SAndy King
1474d021c344SAndy King vmci_trans(vsk)->qp_handle = handle;
1475d021c344SAndy King vmci_trans(vsk)->qpair = qpair;
1476d021c344SAndy King
1477d021c344SAndy King vmci_trans(vsk)->produce_size = vmci_trans(vsk)->consume_size =
1478d021c344SAndy King pkt->u.size;
1479d021c344SAndy King
1480d021c344SAndy King vmci_trans(vsk)->detach_sub_id = detach_sub_id;
1481d021c344SAndy King
1482d021c344SAndy King vmci_trans(vsk)->notify_ops->process_negotiate(sk);
1483d021c344SAndy King
1484d021c344SAndy King return 0;
1485d021c344SAndy King
1486d021c344SAndy King destroy:
1487d021c344SAndy King if (detach_sub_id != VMCI_INVALID_ID)
1488d021c344SAndy King vmci_event_unsubscribe(detach_sub_id);
1489d021c344SAndy King
1490d021c344SAndy King if (!vmci_handle_is_invalid(handle))
1491d021c344SAndy King vmci_qpair_detach(&qpair);
1492d021c344SAndy King
1493d021c344SAndy King return err;
1494d021c344SAndy King }
1495d021c344SAndy King
1496d021c344SAndy King static int
vmci_transport_recv_connecting_client_invalid(struct sock * sk,struct vmci_transport_packet * pkt)1497d021c344SAndy King vmci_transport_recv_connecting_client_invalid(struct sock *sk,
1498d021c344SAndy King struct vmci_transport_packet *pkt)
1499d021c344SAndy King {
1500d021c344SAndy King int err = 0;
1501d021c344SAndy King struct vsock_sock *vsk = vsock_sk(sk);
1502d021c344SAndy King
1503d021c344SAndy King if (vsk->sent_request) {
1504d021c344SAndy King vsk->sent_request = false;
1505d021c344SAndy King vsk->ignore_connecting_rst = true;
1506d021c344SAndy King
1507b9f2b0ffSStefano Garzarella err = vmci_transport_send_conn_request(sk, vsk->buffer_size);
1508d021c344SAndy King if (err < 0)
1509d021c344SAndy King err = vmci_transport_error_to_vsock_error(err);
1510d021c344SAndy King else
1511d021c344SAndy King err = 0;
1512d021c344SAndy King
1513d021c344SAndy King }
1514d021c344SAndy King
1515d021c344SAndy King return err;
1516d021c344SAndy King }
1517d021c344SAndy King
vmci_transport_recv_connected(struct sock * sk,struct vmci_transport_packet * pkt)1518d021c344SAndy King static int vmci_transport_recv_connected(struct sock *sk,
1519d021c344SAndy King struct vmci_transport_packet *pkt)
1520d021c344SAndy King {
1521d021c344SAndy King struct vsock_sock *vsk;
1522d021c344SAndy King bool pkt_processed = false;
1523d021c344SAndy King
1524d021c344SAndy King /* In cases where we are closing the connection, it's sufficient to
1525d021c344SAndy King * mark the state change (and maybe error) and wake up any waiting
1526d021c344SAndy King * threads. Since this is a connected socket, it's owned by a user
1527d021c344SAndy King * process and will be cleaned up when the failure is passed back on
1528d021c344SAndy King * the current or next system call. Our system call implementations
1529d021c344SAndy King * must therefore check for error and state changes on entry and when
1530d021c344SAndy King * being awoken.
1531d021c344SAndy King */
1532d021c344SAndy King switch (pkt->type) {
1533d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN:
1534d021c344SAndy King if (pkt->u.mode) {
1535d021c344SAndy King vsk = vsock_sk(sk);
1536d021c344SAndy King
1537d021c344SAndy King vsk->peer_shutdown |= pkt->u.mode;
1538d021c344SAndy King sk->sk_state_change(sk);
1539d021c344SAndy King }
1540d021c344SAndy King break;
1541d021c344SAndy King
1542d021c344SAndy King case VMCI_TRANSPORT_PACKET_TYPE_RST:
1543d021c344SAndy King vsk = vsock_sk(sk);
1544d021c344SAndy King /* It is possible that we sent our peer a message (e.g a
1545d021c344SAndy King * WAITING_READ) right before we got notified that the peer had
1546d021c344SAndy King * detached. If that happens then we can get a RST pkt back
1547d021c344SAndy King * from our peer even though there is data available for us to
1548d021c344SAndy King * read. In that case, don't shutdown the socket completely but
1549d021c344SAndy King * instead allow the local client to finish reading data off
1550d021c344SAndy King * the queuepair. Always treat a RST pkt in connected mode like
1551d021c344SAndy King * a clean shutdown.
1552d021c344SAndy King */
1553d021c344SAndy King sock_set_flag(sk, SOCK_DONE);
1554d021c344SAndy King vsk->peer_shutdown = SHUTDOWN_MASK;
1555d021c344SAndy King if (vsock_stream_has_data(vsk) <= 0)
15563b4477d2SStefan Hajnoczi sk->sk_state = TCP_CLOSING;
1557d021c344SAndy King
1558d021c344SAndy King sk->sk_state_change(sk);
1559d021c344SAndy King break;
1560d021c344SAndy King
1561d021c344SAndy King default:
1562d021c344SAndy King vsk = vsock_sk(sk);
1563d021c344SAndy King vmci_trans(vsk)->notify_ops->handle_notify_pkt(
1564d021c344SAndy King sk, pkt, false, NULL, NULL,
1565d021c344SAndy King &pkt_processed);
1566d021c344SAndy King if (!pkt_processed)
1567d021c344SAndy King return -EINVAL;
1568d021c344SAndy King
1569d021c344SAndy King break;
1570d021c344SAndy King }
1571d021c344SAndy King
1572d021c344SAndy King return 0;
1573d021c344SAndy King }
1574d021c344SAndy King
vmci_transport_socket_init(struct vsock_sock * vsk,struct vsock_sock * psk)1575d021c344SAndy King static int vmci_transport_socket_init(struct vsock_sock *vsk,
1576d021c344SAndy King struct vsock_sock *psk)
1577d021c344SAndy King {
1578d021c344SAndy King vsk->trans = kmalloc(sizeof(struct vmci_transport), GFP_KERNEL);
1579d021c344SAndy King if (!vsk->trans)
1580d021c344SAndy King return -ENOMEM;
1581d021c344SAndy King
1582d021c344SAndy King vmci_trans(vsk)->dg_handle = VMCI_INVALID_HANDLE;
1583d021c344SAndy King vmci_trans(vsk)->qp_handle = VMCI_INVALID_HANDLE;
1584d021c344SAndy King vmci_trans(vsk)->qpair = NULL;
1585d021c344SAndy King vmci_trans(vsk)->produce_size = vmci_trans(vsk)->consume_size = 0;
15864ef7ea91SJorgen Hansen vmci_trans(vsk)->detach_sub_id = VMCI_INVALID_ID;
1587d021c344SAndy King vmci_trans(vsk)->notify_ops = NULL;
15884ef7ea91SJorgen Hansen INIT_LIST_HEAD(&vmci_trans(vsk)->elem);
15894ef7ea91SJorgen Hansen vmci_trans(vsk)->sk = &vsk->sk;
15908566b86aSJorgen Hansen spin_lock_init(&vmci_trans(vsk)->lock);
1591d021c344SAndy King
1592d021c344SAndy King return 0;
1593d021c344SAndy King }
1594d021c344SAndy King
vmci_transport_free_resources(struct list_head * transport_list)15954ef7ea91SJorgen Hansen static void vmci_transport_free_resources(struct list_head *transport_list)
15964ef7ea91SJorgen Hansen {
15974ef7ea91SJorgen Hansen while (!list_empty(transport_list)) {
15984ef7ea91SJorgen Hansen struct vmci_transport *transport =
15994ef7ea91SJorgen Hansen list_first_entry(transport_list, struct vmci_transport,
16004ef7ea91SJorgen Hansen elem);
16014ef7ea91SJorgen Hansen list_del(&transport->elem);
16024ef7ea91SJorgen Hansen
16034ef7ea91SJorgen Hansen if (transport->detach_sub_id != VMCI_INVALID_ID) {
16044ef7ea91SJorgen Hansen vmci_event_unsubscribe(transport->detach_sub_id);
16054ef7ea91SJorgen Hansen transport->detach_sub_id = VMCI_INVALID_ID;
16064ef7ea91SJorgen Hansen }
16074ef7ea91SJorgen Hansen
16084ef7ea91SJorgen Hansen if (!vmci_handle_is_invalid(transport->qp_handle)) {
16094ef7ea91SJorgen Hansen vmci_qpair_detach(&transport->qpair);
16104ef7ea91SJorgen Hansen transport->qp_handle = VMCI_INVALID_HANDLE;
16114ef7ea91SJorgen Hansen transport->produce_size = 0;
16124ef7ea91SJorgen Hansen transport->consume_size = 0;
16134ef7ea91SJorgen Hansen }
16144ef7ea91SJorgen Hansen
16154ef7ea91SJorgen Hansen kfree(transport);
16164ef7ea91SJorgen Hansen }
16174ef7ea91SJorgen Hansen }
16184ef7ea91SJorgen Hansen
vmci_transport_cleanup(struct work_struct * work)16194ef7ea91SJorgen Hansen static void vmci_transport_cleanup(struct work_struct *work)
16204ef7ea91SJorgen Hansen {
16214ef7ea91SJorgen Hansen LIST_HEAD(pending);
16224ef7ea91SJorgen Hansen
16234ef7ea91SJorgen Hansen spin_lock_bh(&vmci_transport_cleanup_lock);
16244ef7ea91SJorgen Hansen list_replace_init(&vmci_transport_cleanup_list, &pending);
16254ef7ea91SJorgen Hansen spin_unlock_bh(&vmci_transport_cleanup_lock);
16264ef7ea91SJorgen Hansen vmci_transport_free_resources(&pending);
16274ef7ea91SJorgen Hansen }
16284ef7ea91SJorgen Hansen
vmci_transport_destruct(struct vsock_sock * vsk)1629d021c344SAndy King static void vmci_transport_destruct(struct vsock_sock *vsk)
1630d021c344SAndy King {
1631225d9464SPaolo Abeni /* transport can be NULL if we hit a failure at init() time */
1632225d9464SPaolo Abeni if (!vmci_trans(vsk))
1633225d9464SPaolo Abeni return;
1634225d9464SPaolo Abeni
16354ef7ea91SJorgen Hansen /* Ensure that the detach callback doesn't use the sk/vsk
16364ef7ea91SJorgen Hansen * we are about to destruct.
16374ef7ea91SJorgen Hansen */
16384ef7ea91SJorgen Hansen spin_lock_bh(&vmci_trans(vsk)->lock);
16394ef7ea91SJorgen Hansen vmci_trans(vsk)->sk = NULL;
16404ef7ea91SJorgen Hansen spin_unlock_bh(&vmci_trans(vsk)->lock);
1641d021c344SAndy King
1642d021c344SAndy King if (vmci_trans(vsk)->notify_ops)
1643d021c344SAndy King vmci_trans(vsk)->notify_ops->socket_destruct(vsk);
1644d021c344SAndy King
16454ef7ea91SJorgen Hansen spin_lock_bh(&vmci_transport_cleanup_lock);
16464ef7ea91SJorgen Hansen list_add(&vmci_trans(vsk)->elem, &vmci_transport_cleanup_list);
16474ef7ea91SJorgen Hansen spin_unlock_bh(&vmci_transport_cleanup_lock);
16484ef7ea91SJorgen Hansen schedule_work(&vmci_transport_cleanup_work);
16494ef7ea91SJorgen Hansen
1650d021c344SAndy King vsk->trans = NULL;
1651d021c344SAndy King }
1652d021c344SAndy King
vmci_transport_release(struct vsock_sock * vsk)1653d021c344SAndy King static void vmci_transport_release(struct vsock_sock *vsk)
1654d021c344SAndy King {
16556773b7dcSStefan Hajnoczi vsock_remove_sock(vsk);
16566773b7dcSStefan Hajnoczi
1657d021c344SAndy King if (!vmci_handle_is_invalid(vmci_trans(vsk)->dg_handle)) {
1658d021c344SAndy King vmci_datagram_destroy_handle(vmci_trans(vsk)->dg_handle);
1659d021c344SAndy King vmci_trans(vsk)->dg_handle = VMCI_INVALID_HANDLE;
1660d021c344SAndy King }
1661d021c344SAndy King }
1662d021c344SAndy King
vmci_transport_dgram_bind(struct vsock_sock * vsk,struct sockaddr_vm * addr)1663d021c344SAndy King static int vmci_transport_dgram_bind(struct vsock_sock *vsk,
1664d021c344SAndy King struct sockaddr_vm *addr)
1665d021c344SAndy King {
1666d021c344SAndy King u32 port;
1667d021c344SAndy King u32 flags;
1668d021c344SAndy King int err;
1669d021c344SAndy King
1670d021c344SAndy King /* VMCI will select a resource ID for us if we provide
1671d021c344SAndy King * VMCI_INVALID_ID.
1672d021c344SAndy King */
1673d021c344SAndy King port = addr->svm_port == VMADDR_PORT_ANY ?
1674d021c344SAndy King VMCI_INVALID_ID : addr->svm_port;
1675d021c344SAndy King
1676d021c344SAndy King if (port <= LAST_RESERVED_PORT && !capable(CAP_NET_BIND_SERVICE))
1677d021c344SAndy King return -EACCES;
1678d021c344SAndy King
1679d021c344SAndy King flags = addr->svm_cid == VMADDR_CID_ANY ?
1680d021c344SAndy King VMCI_FLAG_ANYCID_DG_HND : 0;
1681d021c344SAndy King
1682d021c344SAndy King err = vmci_transport_datagram_create_hnd(port, flags,
1683d021c344SAndy King vmci_transport_recv_dgram_cb,
1684d021c344SAndy King &vsk->sk,
1685d021c344SAndy King &vmci_trans(vsk)->dg_handle);
1686d021c344SAndy King if (err < VMCI_SUCCESS)
1687d021c344SAndy King return vmci_transport_error_to_vsock_error(err);
1688d021c344SAndy King vsock_addr_init(&vsk->local_addr, addr->svm_cid,
1689d021c344SAndy King vmci_trans(vsk)->dg_handle.resource);
1690d021c344SAndy King
1691d021c344SAndy King return 0;
1692d021c344SAndy King }
1693d021c344SAndy King
vmci_transport_dgram_enqueue(struct vsock_sock * vsk,struct sockaddr_vm * remote_addr,struct msghdr * msg,size_t len)1694d021c344SAndy King static int vmci_transport_dgram_enqueue(
1695d021c344SAndy King struct vsock_sock *vsk,
1696d021c344SAndy King struct sockaddr_vm *remote_addr,
16970f7db23aSAl Viro struct msghdr *msg,
1698d021c344SAndy King size_t len)
1699d021c344SAndy King {
1700d021c344SAndy King int err;
1701d021c344SAndy King struct vmci_datagram *dg;
1702d021c344SAndy King
1703d021c344SAndy King if (len > VMCI_MAX_DG_PAYLOAD_SIZE)
1704d021c344SAndy King return -EMSGSIZE;
1705d021c344SAndy King
1706d021c344SAndy King if (!vmci_transport_allow_dgram(vsk, remote_addr->svm_cid))
1707d021c344SAndy King return -EPERM;
1708d021c344SAndy King
1709d021c344SAndy King /* Allocate a buffer for the user's message and our packet header. */
1710d021c344SAndy King dg = kmalloc(len + sizeof(*dg), GFP_KERNEL);
1711d021c344SAndy King if (!dg)
1712d021c344SAndy King return -ENOMEM;
1713d021c344SAndy King
171444aa5a6dSArtem Chernyshev err = memcpy_from_msg(VMCI_DG_PAYLOAD(dg), msg, len);
171544aa5a6dSArtem Chernyshev if (err) {
171644aa5a6dSArtem Chernyshev kfree(dg);
171744aa5a6dSArtem Chernyshev return err;
171844aa5a6dSArtem Chernyshev }
1719d021c344SAndy King
1720d021c344SAndy King dg->dst = vmci_make_handle(remote_addr->svm_cid,
1721d021c344SAndy King remote_addr->svm_port);
1722d021c344SAndy King dg->src = vmci_make_handle(vsk->local_addr.svm_cid,
1723d021c344SAndy King vsk->local_addr.svm_port);
1724d021c344SAndy King dg->payload_size = len;
1725d021c344SAndy King
1726d021c344SAndy King err = vmci_datagram_send(dg);
1727d021c344SAndy King kfree(dg);
1728d021c344SAndy King if (err < 0)
1729d021c344SAndy King return vmci_transport_error_to_vsock_error(err);
1730d021c344SAndy King
1731d021c344SAndy King return err - sizeof(*dg);
1732d021c344SAndy King }
1733d021c344SAndy King
vmci_transport_dgram_dequeue(struct vsock_sock * vsk,struct msghdr * msg,size_t len,int flags)17341b784140SYing Xue static int vmci_transport_dgram_dequeue(struct vsock_sock *vsk,
1735d021c344SAndy King struct msghdr *msg, size_t len,
1736d021c344SAndy King int flags)
1737d021c344SAndy King {
1738d021c344SAndy King int err;
1739d021c344SAndy King struct vmci_datagram *dg;
1740d021c344SAndy King size_t payload_len;
1741d021c344SAndy King struct sk_buff *skb;
1742d021c344SAndy King
1743d021c344SAndy King if (flags & MSG_OOB || flags & MSG_ERRQUEUE)
1744d021c344SAndy King return -EOPNOTSUPP;
1745d021c344SAndy King
1746d021c344SAndy King /* Retrieve the head sk_buff from the socket's receive queue. */
1747d021c344SAndy King err = 0;
1748f4b41f06SOliver Hartkopp skb = skb_recv_datagram(&vsk->sk, flags, &err);
1749d021c344SAndy King if (!skb)
17509c995cc9SJorgen Hansen return err;
1751d021c344SAndy King
1752d021c344SAndy King dg = (struct vmci_datagram *)skb->data;
1753d021c344SAndy King if (!dg)
1754d021c344SAndy King /* err is 0, meaning we read zero bytes. */
1755d021c344SAndy King goto out;
1756d021c344SAndy King
1757d021c344SAndy King payload_len = dg->payload_size;
1758d021c344SAndy King /* Ensure the sk_buff matches the payload size claimed in the packet. */
1759d021c344SAndy King if (payload_len != skb->len - sizeof(*dg)) {
1760d021c344SAndy King err = -EINVAL;
1761d021c344SAndy King goto out;
1762d021c344SAndy King }
1763d021c344SAndy King
1764d021c344SAndy King if (payload_len > len) {
1765d021c344SAndy King payload_len = len;
1766d021c344SAndy King msg->msg_flags |= MSG_TRUNC;
1767d021c344SAndy King }
1768d021c344SAndy King
1769d021c344SAndy King /* Place the datagram payload in the user's iovec. */
177051f3d02bSDavid S. Miller err = skb_copy_datagram_msg(skb, sizeof(*dg), msg, payload_len);
1771d021c344SAndy King if (err)
1772d021c344SAndy King goto out;
1773d021c344SAndy King
1774d021c344SAndy King if (msg->msg_name) {
1775d021c344SAndy King /* Provide the address of the sender. */
1776342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_vm *, vm_addr, msg->msg_name);
1777d021c344SAndy King vsock_addr_init(vm_addr, dg->src.context, dg->src.resource);
1778d021c344SAndy King msg->msg_namelen = sizeof(*vm_addr);
1779d021c344SAndy King }
1780d021c344SAndy King err = payload_len;
1781d021c344SAndy King
1782d021c344SAndy King out:
1783d021c344SAndy King skb_free_datagram(&vsk->sk, skb);
1784d021c344SAndy King return err;
1785d021c344SAndy King }
1786d021c344SAndy King
vmci_transport_dgram_allow(u32 cid,u32 port)1787d021c344SAndy King static bool vmci_transport_dgram_allow(u32 cid, u32 port)
1788d021c344SAndy King {
1789d021c344SAndy King if (cid == VMADDR_CID_HYPERVISOR) {
1790d021c344SAndy King /* Registrations of PBRPC Servers do not modify VMX/Hypervisor
1791d021c344SAndy King * state and are allowed.
1792d021c344SAndy King */
1793d021c344SAndy King return port == VMCI_UNITY_PBRPC_REGISTER;
1794d021c344SAndy King }
1795d021c344SAndy King
1796d021c344SAndy King return true;
1797d021c344SAndy King }
1798d021c344SAndy King
vmci_transport_connect(struct vsock_sock * vsk)1799d021c344SAndy King static int vmci_transport_connect(struct vsock_sock *vsk)
1800d021c344SAndy King {
1801d021c344SAndy King int err;
1802d021c344SAndy King bool old_pkt_proto = false;
1803d021c344SAndy King struct sock *sk = &vsk->sk;
1804d021c344SAndy King
1805d021c344SAndy King if (vmci_transport_old_proto_override(&old_pkt_proto) &&
1806d021c344SAndy King old_pkt_proto) {
1807b9f2b0ffSStefano Garzarella err = vmci_transport_send_conn_request(sk, vsk->buffer_size);
1808d021c344SAndy King if (err < 0) {
18093b4477d2SStefan Hajnoczi sk->sk_state = TCP_CLOSE;
1810d021c344SAndy King return err;
1811d021c344SAndy King }
1812d021c344SAndy King } else {
1813d021c344SAndy King int supported_proto_versions =
1814d021c344SAndy King vmci_transport_new_proto_supported_versions();
1815b9f2b0ffSStefano Garzarella err = vmci_transport_send_conn_request2(sk, vsk->buffer_size,
1816d021c344SAndy King supported_proto_versions);
1817d021c344SAndy King if (err < 0) {
18183b4477d2SStefan Hajnoczi sk->sk_state = TCP_CLOSE;
1819d021c344SAndy King return err;
1820d021c344SAndy King }
1821d021c344SAndy King
1822d021c344SAndy King vsk->sent_request = true;
1823d021c344SAndy King }
1824d021c344SAndy King
1825d021c344SAndy King return err;
1826d021c344SAndy King }
1827d021c344SAndy King
vmci_transport_stream_dequeue(struct vsock_sock * vsk,struct msghdr * msg,size_t len,int flags)1828d021c344SAndy King static ssize_t vmci_transport_stream_dequeue(
1829d021c344SAndy King struct vsock_sock *vsk,
18300f7db23aSAl Viro struct msghdr *msg,
1831d021c344SAndy King size_t len,
1832d021c344SAndy King int flags)
1833d021c344SAndy King {
1834*f59f3006SArseniy Krasnov ssize_t err;
1835*f59f3006SArseniy Krasnov
1836d021c344SAndy King if (flags & MSG_PEEK)
1837*f59f3006SArseniy Krasnov err = vmci_qpair_peekv(vmci_trans(vsk)->qpair, msg, len, 0);
1838d021c344SAndy King else
1839*f59f3006SArseniy Krasnov err = vmci_qpair_dequev(vmci_trans(vsk)->qpair, msg, len, 0);
1840*f59f3006SArseniy Krasnov
1841*f59f3006SArseniy Krasnov if (err < 0)
1842*f59f3006SArseniy Krasnov err = -ENOMEM;
1843*f59f3006SArseniy Krasnov
1844*f59f3006SArseniy Krasnov return err;
1845d021c344SAndy King }
1846d021c344SAndy King
vmci_transport_stream_enqueue(struct vsock_sock * vsk,struct msghdr * msg,size_t len)1847d021c344SAndy King static ssize_t vmci_transport_stream_enqueue(
1848d021c344SAndy King struct vsock_sock *vsk,
18490f7db23aSAl Viro struct msghdr *msg,
1850d021c344SAndy King size_t len)
1851d021c344SAndy King {
1852ffa5395aSArseniy Krasnov ssize_t err;
1853ffa5395aSArseniy Krasnov
1854ffa5395aSArseniy Krasnov err = vmci_qpair_enquev(vmci_trans(vsk)->qpair, msg, len, 0);
1855ffa5395aSArseniy Krasnov if (err < 0)
1856ffa5395aSArseniy Krasnov err = -ENOMEM;
1857ffa5395aSArseniy Krasnov
1858ffa5395aSArseniy Krasnov return err;
1859d021c344SAndy King }
1860d021c344SAndy King
vmci_transport_stream_has_data(struct vsock_sock * vsk)1861d021c344SAndy King static s64 vmci_transport_stream_has_data(struct vsock_sock *vsk)
1862d021c344SAndy King {
1863d021c344SAndy King return vmci_qpair_consume_buf_ready(vmci_trans(vsk)->qpair);
1864d021c344SAndy King }
1865d021c344SAndy King
vmci_transport_stream_has_space(struct vsock_sock * vsk)1866d021c344SAndy King static s64 vmci_transport_stream_has_space(struct vsock_sock *vsk)
1867d021c344SAndy King {
1868d021c344SAndy King return vmci_qpair_produce_free_space(vmci_trans(vsk)->qpair);
1869d021c344SAndy King }
1870d021c344SAndy King
vmci_transport_stream_rcvhiwat(struct vsock_sock * vsk)1871d021c344SAndy King static u64 vmci_transport_stream_rcvhiwat(struct vsock_sock *vsk)
1872d021c344SAndy King {
1873d021c344SAndy King return vmci_trans(vsk)->consume_size;
1874d021c344SAndy King }
1875d021c344SAndy King
vmci_transport_stream_is_active(struct vsock_sock * vsk)1876d021c344SAndy King static bool vmci_transport_stream_is_active(struct vsock_sock *vsk)
1877d021c344SAndy King {
1878d021c344SAndy King return !vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle);
1879d021c344SAndy King }
1880d021c344SAndy King
vmci_transport_notify_poll_in(struct vsock_sock * vsk,size_t target,bool * data_ready_now)1881d021c344SAndy King static int vmci_transport_notify_poll_in(
1882d021c344SAndy King struct vsock_sock *vsk,
1883d021c344SAndy King size_t target,
1884d021c344SAndy King bool *data_ready_now)
1885d021c344SAndy King {
1886d021c344SAndy King return vmci_trans(vsk)->notify_ops->poll_in(
1887d021c344SAndy King &vsk->sk, target, data_ready_now);
1888d021c344SAndy King }
1889d021c344SAndy King
vmci_transport_notify_poll_out(struct vsock_sock * vsk,size_t target,bool * space_available_now)1890d021c344SAndy King static int vmci_transport_notify_poll_out(
1891d021c344SAndy King struct vsock_sock *vsk,
1892d021c344SAndy King size_t target,
1893d021c344SAndy King bool *space_available_now)
1894d021c344SAndy King {
1895d021c344SAndy King return vmci_trans(vsk)->notify_ops->poll_out(
1896d021c344SAndy King &vsk->sk, target, space_available_now);
1897d021c344SAndy King }
1898d021c344SAndy King
vmci_transport_notify_recv_init(struct vsock_sock * vsk,size_t target,struct vsock_transport_recv_notify_data * data)1899d021c344SAndy King static int vmci_transport_notify_recv_init(
1900d021c344SAndy King struct vsock_sock *vsk,
1901d021c344SAndy King size_t target,
1902d021c344SAndy King struct vsock_transport_recv_notify_data *data)
1903d021c344SAndy King {
1904d021c344SAndy King return vmci_trans(vsk)->notify_ops->recv_init(
1905d021c344SAndy King &vsk->sk, target,
1906d021c344SAndy King (struct vmci_transport_recv_notify_data *)data);
1907d021c344SAndy King }
1908d021c344SAndy King
vmci_transport_notify_recv_pre_block(struct vsock_sock * vsk,size_t target,struct vsock_transport_recv_notify_data * data)1909d021c344SAndy King static int vmci_transport_notify_recv_pre_block(
1910d021c344SAndy King struct vsock_sock *vsk,
1911d021c344SAndy King size_t target,
1912d021c344SAndy King struct vsock_transport_recv_notify_data *data)
1913d021c344SAndy King {
1914d021c344SAndy King return vmci_trans(vsk)->notify_ops->recv_pre_block(
1915d021c344SAndy King &vsk->sk, target,
1916d021c344SAndy King (struct vmci_transport_recv_notify_data *)data);
1917d021c344SAndy King }
1918d021c344SAndy King
vmci_transport_notify_recv_pre_dequeue(struct vsock_sock * vsk,size_t target,struct vsock_transport_recv_notify_data * data)1919d021c344SAndy King static int vmci_transport_notify_recv_pre_dequeue(
1920d021c344SAndy King struct vsock_sock *vsk,
1921d021c344SAndy King size_t target,
1922d021c344SAndy King struct vsock_transport_recv_notify_data *data)
1923d021c344SAndy King {
1924d021c344SAndy King return vmci_trans(vsk)->notify_ops->recv_pre_dequeue(
1925d021c344SAndy King &vsk->sk, target,
1926d021c344SAndy King (struct vmci_transport_recv_notify_data *)data);
1927d021c344SAndy King }
1928d021c344SAndy King
vmci_transport_notify_recv_post_dequeue(struct vsock_sock * vsk,size_t target,ssize_t copied,bool data_read,struct vsock_transport_recv_notify_data * data)1929d021c344SAndy King static int vmci_transport_notify_recv_post_dequeue(
1930d021c344SAndy King struct vsock_sock *vsk,
1931d021c344SAndy King size_t target,
1932d021c344SAndy King ssize_t copied,
1933d021c344SAndy King bool data_read,
1934d021c344SAndy King struct vsock_transport_recv_notify_data *data)
1935d021c344SAndy King {
1936d021c344SAndy King return vmci_trans(vsk)->notify_ops->recv_post_dequeue(
1937d021c344SAndy King &vsk->sk, target, copied, data_read,
1938d021c344SAndy King (struct vmci_transport_recv_notify_data *)data);
1939d021c344SAndy King }
1940d021c344SAndy King
vmci_transport_notify_send_init(struct vsock_sock * vsk,struct vsock_transport_send_notify_data * data)1941d021c344SAndy King static int vmci_transport_notify_send_init(
1942d021c344SAndy King struct vsock_sock *vsk,
1943d021c344SAndy King struct vsock_transport_send_notify_data *data)
1944d021c344SAndy King {
1945d021c344SAndy King return vmci_trans(vsk)->notify_ops->send_init(
1946d021c344SAndy King &vsk->sk,
1947d021c344SAndy King (struct vmci_transport_send_notify_data *)data);
1948d021c344SAndy King }
1949d021c344SAndy King
vmci_transport_notify_send_pre_block(struct vsock_sock * vsk,struct vsock_transport_send_notify_data * data)1950d021c344SAndy King static int vmci_transport_notify_send_pre_block(
1951d021c344SAndy King struct vsock_sock *vsk,
1952d021c344SAndy King struct vsock_transport_send_notify_data *data)
1953d021c344SAndy King {
1954d021c344SAndy King return vmci_trans(vsk)->notify_ops->send_pre_block(
1955d021c344SAndy King &vsk->sk,
1956d021c344SAndy King (struct vmci_transport_send_notify_data *)data);
1957d021c344SAndy King }
1958d021c344SAndy King
vmci_transport_notify_send_pre_enqueue(struct vsock_sock * vsk,struct vsock_transport_send_notify_data * data)1959d021c344SAndy King static int vmci_transport_notify_send_pre_enqueue(
1960d021c344SAndy King struct vsock_sock *vsk,
1961d021c344SAndy King struct vsock_transport_send_notify_data *data)
1962d021c344SAndy King {
1963d021c344SAndy King return vmci_trans(vsk)->notify_ops->send_pre_enqueue(
1964d021c344SAndy King &vsk->sk,
1965d021c344SAndy King (struct vmci_transport_send_notify_data *)data);
1966d021c344SAndy King }
1967d021c344SAndy King
vmci_transport_notify_send_post_enqueue(struct vsock_sock * vsk,ssize_t written,struct vsock_transport_send_notify_data * data)1968d021c344SAndy King static int vmci_transport_notify_send_post_enqueue(
1969d021c344SAndy King struct vsock_sock *vsk,
1970d021c344SAndy King ssize_t written,
1971d021c344SAndy King struct vsock_transport_send_notify_data *data)
1972d021c344SAndy King {
1973d021c344SAndy King return vmci_trans(vsk)->notify_ops->send_post_enqueue(
1974d021c344SAndy King &vsk->sk, written,
1975d021c344SAndy King (struct vmci_transport_send_notify_data *)data);
1976d021c344SAndy King }
1977d021c344SAndy King
vmci_transport_old_proto_override(bool * old_pkt_proto)1978d021c344SAndy King static bool vmci_transport_old_proto_override(bool *old_pkt_proto)
1979d021c344SAndy King {
1980d021c344SAndy King if (PROTOCOL_OVERRIDE != -1) {
1981d021c344SAndy King if (PROTOCOL_OVERRIDE == 0)
1982d021c344SAndy King *old_pkt_proto = true;
1983d021c344SAndy King else
1984d021c344SAndy King *old_pkt_proto = false;
1985d021c344SAndy King
1986d021c344SAndy King pr_info("Proto override in use\n");
1987d021c344SAndy King return true;
1988d021c344SAndy King }
1989d021c344SAndy King
1990d021c344SAndy King return false;
1991d021c344SAndy King }
1992d021c344SAndy King
vmci_transport_proto_to_notify_struct(struct sock * sk,u16 * proto,bool old_pkt_proto)1993d021c344SAndy King static bool vmci_transport_proto_to_notify_struct(struct sock *sk,
1994d021c344SAndy King u16 *proto,
1995d021c344SAndy King bool old_pkt_proto)
1996d021c344SAndy King {
1997d021c344SAndy King struct vsock_sock *vsk = vsock_sk(sk);
1998d021c344SAndy King
1999d021c344SAndy King if (old_pkt_proto) {
2000d021c344SAndy King if (*proto != VSOCK_PROTO_INVALID) {
2001d021c344SAndy King pr_err("Can't set both an old and new protocol\n");
2002d021c344SAndy King return false;
2003d021c344SAndy King }
2004d021c344SAndy King vmci_trans(vsk)->notify_ops = &vmci_transport_notify_pkt_ops;
2005d021c344SAndy King goto exit;
2006d021c344SAndy King }
2007d021c344SAndy King
2008d021c344SAndy King switch (*proto) {
2009d021c344SAndy King case VSOCK_PROTO_PKT_ON_NOTIFY:
2010d021c344SAndy King vmci_trans(vsk)->notify_ops =
2011d021c344SAndy King &vmci_transport_notify_pkt_q_state_ops;
2012d021c344SAndy King break;
2013d021c344SAndy King default:
2014d021c344SAndy King pr_err("Unknown notify protocol version\n");
2015d021c344SAndy King return false;
2016d021c344SAndy King }
2017d021c344SAndy King
2018d021c344SAndy King exit:
2019d021c344SAndy King vmci_trans(vsk)->notify_ops->socket_init(sk);
2020d021c344SAndy King return true;
2021d021c344SAndy King }
2022d021c344SAndy King
vmci_transport_new_proto_supported_versions(void)2023d021c344SAndy King static u16 vmci_transport_new_proto_supported_versions(void)
2024d021c344SAndy King {
2025d021c344SAndy King if (PROTOCOL_OVERRIDE != -1)
2026d021c344SAndy King return PROTOCOL_OVERRIDE;
2027d021c344SAndy King
2028d021c344SAndy King return VSOCK_PROTO_ALL_SUPPORTED;
2029d021c344SAndy King }
2030d021c344SAndy King
vmci_transport_get_local_cid(void)2031d021c344SAndy King static u32 vmci_transport_get_local_cid(void)
2032d021c344SAndy King {
2033d021c344SAndy King return vmci_get_context_id();
2034d021c344SAndy King }
2035d021c344SAndy King
2036c0cfa2d8SStefano Garzarella static struct vsock_transport vmci_transport = {
20376a2c0962SStefano Garzarella .module = THIS_MODULE,
2038d021c344SAndy King .init = vmci_transport_socket_init,
2039d021c344SAndy King .destruct = vmci_transport_destruct,
2040d021c344SAndy King .release = vmci_transport_release,
2041d021c344SAndy King .connect = vmci_transport_connect,
2042d021c344SAndy King .dgram_bind = vmci_transport_dgram_bind,
2043d021c344SAndy King .dgram_dequeue = vmci_transport_dgram_dequeue,
2044d021c344SAndy King .dgram_enqueue = vmci_transport_dgram_enqueue,
2045d021c344SAndy King .dgram_allow = vmci_transport_dgram_allow,
2046d021c344SAndy King .stream_dequeue = vmci_transport_stream_dequeue,
2047d021c344SAndy King .stream_enqueue = vmci_transport_stream_enqueue,
2048d021c344SAndy King .stream_has_data = vmci_transport_stream_has_data,
2049d021c344SAndy King .stream_has_space = vmci_transport_stream_has_space,
2050d021c344SAndy King .stream_rcvhiwat = vmci_transport_stream_rcvhiwat,
2051d021c344SAndy King .stream_is_active = vmci_transport_stream_is_active,
2052d021c344SAndy King .stream_allow = vmci_transport_stream_allow,
2053d021c344SAndy King .notify_poll_in = vmci_transport_notify_poll_in,
2054d021c344SAndy King .notify_poll_out = vmci_transport_notify_poll_out,
2055d021c344SAndy King .notify_recv_init = vmci_transport_notify_recv_init,
2056d021c344SAndy King .notify_recv_pre_block = vmci_transport_notify_recv_pre_block,
2057d021c344SAndy King .notify_recv_pre_dequeue = vmci_transport_notify_recv_pre_dequeue,
2058d021c344SAndy King .notify_recv_post_dequeue = vmci_transport_notify_recv_post_dequeue,
2059d021c344SAndy King .notify_send_init = vmci_transport_notify_send_init,
2060d021c344SAndy King .notify_send_pre_block = vmci_transport_notify_send_pre_block,
2061d021c344SAndy King .notify_send_pre_enqueue = vmci_transport_notify_send_pre_enqueue,
2062d021c344SAndy King .notify_send_post_enqueue = vmci_transport_notify_send_post_enqueue,
2063d021c344SAndy King .shutdown = vmci_transport_shutdown,
2064d021c344SAndy King .get_local_cid = vmci_transport_get_local_cid,
2065d021c344SAndy King };
2066d021c344SAndy King
vmci_check_transport(struct vsock_sock * vsk)2067c0cfa2d8SStefano Garzarella static bool vmci_check_transport(struct vsock_sock *vsk)
2068c0cfa2d8SStefano Garzarella {
2069c0cfa2d8SStefano Garzarella return vsk->transport == &vmci_transport;
2070c0cfa2d8SStefano Garzarella }
2071c0cfa2d8SStefano Garzarella
vmci_vsock_transport_cb(bool is_host)2072fdb4276aSStefano Garzarella static void vmci_vsock_transport_cb(bool is_host)
2073b1bba80aSStefano Garzarella {
2074b1bba80aSStefano Garzarella int features;
2075b1bba80aSStefano Garzarella
2076b1bba80aSStefano Garzarella if (is_host)
2077b1bba80aSStefano Garzarella features = VSOCK_TRANSPORT_F_H2G;
2078b1bba80aSStefano Garzarella else
2079b1bba80aSStefano Garzarella features = VSOCK_TRANSPORT_F_G2H;
2080b1bba80aSStefano Garzarella
2081b1bba80aSStefano Garzarella vsock_core_register(&vmci_transport, features);
2082b1bba80aSStefano Garzarella }
2083b1bba80aSStefano Garzarella
vmci_transport_init(void)2084d021c344SAndy King static int __init vmci_transport_init(void)
2085d021c344SAndy King {
2086d021c344SAndy King int err;
2087d021c344SAndy King
2088d021c344SAndy King /* Create the datagram handle that we will use to send and receive all
2089d021c344SAndy King * VSocket control messages for this context.
2090d021c344SAndy King */
2091d021c344SAndy King err = vmci_transport_datagram_create_hnd(VMCI_TRANSPORT_PACKET_RID,
2092d021c344SAndy King VMCI_FLAG_ANYCID_DG_HND,
2093d021c344SAndy King vmci_transport_recv_stream_cb,
2094d021c344SAndy King NULL,
2095d021c344SAndy King &vmci_transport_stream_handle);
2096d021c344SAndy King if (err < VMCI_SUCCESS) {
2097d021c344SAndy King pr_err("Unable to create datagram handle. (%d)\n", err);
2098d021c344SAndy King return vmci_transport_error_to_vsock_error(err);
2099d021c344SAndy King }
2100d021c344SAndy King err = vmci_event_subscribe(VMCI_EVENT_QP_RESUMED,
2101d021c344SAndy King vmci_transport_qp_resumed_cb,
2102d021c344SAndy King NULL, &vmci_transport_qp_resumed_sub_id);
2103d021c344SAndy King if (err < VMCI_SUCCESS) {
2104d021c344SAndy King pr_err("Unable to subscribe to resumed event. (%d)\n", err);
2105d021c344SAndy King err = vmci_transport_error_to_vsock_error(err);
2106d021c344SAndy King vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID;
2107d021c344SAndy King goto err_destroy_stream_handle;
2108d021c344SAndy King }
2109d021c344SAndy King
2110b1bba80aSStefano Garzarella /* Register only with dgram feature, other features (H2G, G2H) will be
2111b1bba80aSStefano Garzarella * registered when the first host or guest becomes active.
2112b1bba80aSStefano Garzarella */
2113b1bba80aSStefano Garzarella err = vsock_core_register(&vmci_transport, VSOCK_TRANSPORT_F_DGRAM);
2114d021c344SAndy King if (err < 0)
2115d021c344SAndy King goto err_unsubscribe;
2116d021c344SAndy King
2117b1bba80aSStefano Garzarella err = vmci_register_vsock_callback(vmci_vsock_transport_cb);
2118b1bba80aSStefano Garzarella if (err < 0)
2119b1bba80aSStefano Garzarella goto err_unregister;
2120b1bba80aSStefano Garzarella
2121d021c344SAndy King return 0;
2122d021c344SAndy King
2123b1bba80aSStefano Garzarella err_unregister:
2124b1bba80aSStefano Garzarella vsock_core_unregister(&vmci_transport);
2125d021c344SAndy King err_unsubscribe:
2126d021c344SAndy King vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id);
2127d021c344SAndy King err_destroy_stream_handle:
2128d021c344SAndy King vmci_datagram_destroy_handle(vmci_transport_stream_handle);
2129d021c344SAndy King return err;
2130d021c344SAndy King }
2131d021c344SAndy King module_init(vmci_transport_init);
2132d021c344SAndy King
vmci_transport_exit(void)2133d021c344SAndy King static void __exit vmci_transport_exit(void)
2134d021c344SAndy King {
21354ef7ea91SJorgen Hansen cancel_work_sync(&vmci_transport_cleanup_work);
21364ef7ea91SJorgen Hansen vmci_transport_free_resources(&vmci_transport_cleanup_list);
21374ef7ea91SJorgen Hansen
2138d021c344SAndy King if (!vmci_handle_is_invalid(vmci_transport_stream_handle)) {
2139d021c344SAndy King if (vmci_datagram_destroy_handle(
2140d021c344SAndy King vmci_transport_stream_handle) != VMCI_SUCCESS)
2141d021c344SAndy King pr_err("Couldn't destroy datagram handle\n");
2142d021c344SAndy King vmci_transport_stream_handle = VMCI_INVALID_HANDLE;
2143d021c344SAndy King }
2144d021c344SAndy King
2145d021c344SAndy King if (vmci_transport_qp_resumed_sub_id != VMCI_INVALID_ID) {
2146d021c344SAndy King vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id);
2147d021c344SAndy King vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID;
2148d021c344SAndy King }
2149d021c344SAndy King
2150b1bba80aSStefano Garzarella vmci_register_vsock_callback(NULL);
2151c0cfa2d8SStefano Garzarella vsock_core_unregister(&vmci_transport);
2152d021c344SAndy King }
2153d021c344SAndy King module_exit(vmci_transport_exit);
2154d021c344SAndy King
2155d021c344SAndy King MODULE_AUTHOR("VMware, Inc.");
2156d021c344SAndy King MODULE_DESCRIPTION("VMCI transport for Virtual Sockets");
2157afbea2cdSJorgen Hansen MODULE_VERSION("1.0.5.0-k");
2158d021c344SAndy King MODULE_LICENSE("GPL v2");
2159d021c344SAndy King MODULE_ALIAS("vmware_vsock");
2160d021c344SAndy King MODULE_ALIAS_NETPROTO(PF_VSOCK);
2161