18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * VMware vSockets Driver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2007-2013 VMware, Inc. All rights reserved. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#ifndef __AF_VSOCK_H__ 98c2ecf20Sopenharmony_ci#define __AF_VSOCK_H__ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/kernel.h> 128c2ecf20Sopenharmony_ci#include <linux/workqueue.h> 138c2ecf20Sopenharmony_ci#include <uapi/linux/vm_sockets.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include "vsock_addr.h" 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#define LAST_RESERVED_PORT 1023 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#define VSOCK_HASH_SIZE 251 208c2ecf20Sopenharmony_ciextern struct list_head vsock_bind_table[VSOCK_HASH_SIZE + 1]; 218c2ecf20Sopenharmony_ciextern struct list_head vsock_connected_table[VSOCK_HASH_SIZE]; 228c2ecf20Sopenharmony_ciextern spinlock_t vsock_table_lock; 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define vsock_sk(__sk) ((struct vsock_sock *)__sk) 258c2ecf20Sopenharmony_ci#define sk_vsock(__vsk) (&(__vsk)->sk) 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_cistruct vsock_sock { 288c2ecf20Sopenharmony_ci /* sk must be the first member. */ 298c2ecf20Sopenharmony_ci struct sock sk; 308c2ecf20Sopenharmony_ci const struct vsock_transport *transport; 318c2ecf20Sopenharmony_ci struct sockaddr_vm local_addr; 328c2ecf20Sopenharmony_ci struct sockaddr_vm remote_addr; 338c2ecf20Sopenharmony_ci /* Links for the global tables of bound and connected sockets. */ 348c2ecf20Sopenharmony_ci struct list_head bound_table; 358c2ecf20Sopenharmony_ci struct list_head connected_table; 368c2ecf20Sopenharmony_ci /* Accessed without the socket lock held. This means it can never be 378c2ecf20Sopenharmony_ci * modified outsided of socket create or destruct. 388c2ecf20Sopenharmony_ci */ 398c2ecf20Sopenharmony_ci bool trusted; 408c2ecf20Sopenharmony_ci bool cached_peer_allow_dgram; /* Dgram communication allowed to 418c2ecf20Sopenharmony_ci * cached peer? 428c2ecf20Sopenharmony_ci */ 438c2ecf20Sopenharmony_ci u32 cached_peer; /* Context ID of last dgram destination check. */ 448c2ecf20Sopenharmony_ci const struct cred *owner; 458c2ecf20Sopenharmony_ci /* Rest are SOCK_STREAM only. */ 468c2ecf20Sopenharmony_ci long connect_timeout; 478c2ecf20Sopenharmony_ci /* Listening socket that this came from. */ 488c2ecf20Sopenharmony_ci struct sock *listener; 498c2ecf20Sopenharmony_ci /* Used for pending list and accept queue during connection handshake. 508c2ecf20Sopenharmony_ci * The listening socket is the head for both lists. Sockets created 518c2ecf20Sopenharmony_ci * for connection requests are placed in the pending list until they 528c2ecf20Sopenharmony_ci * are connected, at which point they are put in the accept queue list 538c2ecf20Sopenharmony_ci * so they can be accepted in accept(). If accept() cannot accept the 548c2ecf20Sopenharmony_ci * connection, it is marked as rejected so the cleanup function knows 558c2ecf20Sopenharmony_ci * to clean up the socket. 568c2ecf20Sopenharmony_ci */ 578c2ecf20Sopenharmony_ci struct list_head pending_links; 588c2ecf20Sopenharmony_ci struct list_head accept_queue; 598c2ecf20Sopenharmony_ci bool rejected; 608c2ecf20Sopenharmony_ci struct delayed_work connect_work; 618c2ecf20Sopenharmony_ci struct delayed_work pending_work; 628c2ecf20Sopenharmony_ci struct delayed_work close_work; 638c2ecf20Sopenharmony_ci bool close_work_scheduled; 648c2ecf20Sopenharmony_ci u32 peer_shutdown; 658c2ecf20Sopenharmony_ci bool sent_request; 668c2ecf20Sopenharmony_ci bool ignore_connecting_rst; 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci /* Protected by lock_sock(sk) */ 698c2ecf20Sopenharmony_ci u64 buffer_size; 708c2ecf20Sopenharmony_ci u64 buffer_min_size; 718c2ecf20Sopenharmony_ci u64 buffer_max_size; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci /* Private to transport. */ 748c2ecf20Sopenharmony_ci void *trans; 758c2ecf20Sopenharmony_ci}; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_cis64 vsock_stream_has_data(struct vsock_sock *vsk); 788c2ecf20Sopenharmony_cis64 vsock_stream_has_space(struct vsock_sock *vsk); 798c2ecf20Sopenharmony_cistruct sock *vsock_create_connected(struct sock *parent); 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci/**** TRANSPORT ****/ 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_cistruct vsock_transport_recv_notify_data { 848c2ecf20Sopenharmony_ci u64 data1; /* Transport-defined. */ 858c2ecf20Sopenharmony_ci u64 data2; /* Transport-defined. */ 868c2ecf20Sopenharmony_ci bool notify_on_block; 878c2ecf20Sopenharmony_ci}; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_cistruct vsock_transport_send_notify_data { 908c2ecf20Sopenharmony_ci u64 data1; /* Transport-defined. */ 918c2ecf20Sopenharmony_ci u64 data2; /* Transport-defined. */ 928c2ecf20Sopenharmony_ci}; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci/* Transport features flags */ 958c2ecf20Sopenharmony_ci/* Transport provides host->guest communication */ 968c2ecf20Sopenharmony_ci#define VSOCK_TRANSPORT_F_H2G 0x00000001 978c2ecf20Sopenharmony_ci/* Transport provides guest->host communication */ 988c2ecf20Sopenharmony_ci#define VSOCK_TRANSPORT_F_G2H 0x00000002 998c2ecf20Sopenharmony_ci/* Transport provides DGRAM communication */ 1008c2ecf20Sopenharmony_ci#define VSOCK_TRANSPORT_F_DGRAM 0x00000004 1018c2ecf20Sopenharmony_ci/* Transport provides local (loopback) communication */ 1028c2ecf20Sopenharmony_ci#define VSOCK_TRANSPORT_F_LOCAL 0x00000008 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cistruct vsock_transport { 1058c2ecf20Sopenharmony_ci struct module *module; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci /* Initialize/tear-down socket. */ 1088c2ecf20Sopenharmony_ci int (*init)(struct vsock_sock *, struct vsock_sock *); 1098c2ecf20Sopenharmony_ci void (*destruct)(struct vsock_sock *); 1108c2ecf20Sopenharmony_ci void (*release)(struct vsock_sock *); 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci /* Cancel all pending packets sent on vsock. */ 1138c2ecf20Sopenharmony_ci int (*cancel_pkt)(struct vsock_sock *vsk); 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci /* Connections. */ 1168c2ecf20Sopenharmony_ci int (*connect)(struct vsock_sock *); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci /* DGRAM. */ 1198c2ecf20Sopenharmony_ci int (*dgram_bind)(struct vsock_sock *, struct sockaddr_vm *); 1208c2ecf20Sopenharmony_ci int (*dgram_dequeue)(struct vsock_sock *vsk, struct msghdr *msg, 1218c2ecf20Sopenharmony_ci size_t len, int flags); 1228c2ecf20Sopenharmony_ci int (*dgram_enqueue)(struct vsock_sock *, struct sockaddr_vm *, 1238c2ecf20Sopenharmony_ci struct msghdr *, size_t len); 1248c2ecf20Sopenharmony_ci bool (*dgram_allow)(u32 cid, u32 port); 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci /* STREAM. */ 1278c2ecf20Sopenharmony_ci /* TODO: stream_bind() */ 1288c2ecf20Sopenharmony_ci ssize_t (*stream_dequeue)(struct vsock_sock *, struct msghdr *, 1298c2ecf20Sopenharmony_ci size_t len, int flags); 1308c2ecf20Sopenharmony_ci ssize_t (*stream_enqueue)(struct vsock_sock *, struct msghdr *, 1318c2ecf20Sopenharmony_ci size_t len); 1328c2ecf20Sopenharmony_ci s64 (*stream_has_data)(struct vsock_sock *); 1338c2ecf20Sopenharmony_ci s64 (*stream_has_space)(struct vsock_sock *); 1348c2ecf20Sopenharmony_ci u64 (*stream_rcvhiwat)(struct vsock_sock *); 1358c2ecf20Sopenharmony_ci bool (*stream_is_active)(struct vsock_sock *); 1368c2ecf20Sopenharmony_ci bool (*stream_allow)(u32 cid, u32 port); 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci /* Notification. */ 1398c2ecf20Sopenharmony_ci int (*notify_poll_in)(struct vsock_sock *, size_t, bool *); 1408c2ecf20Sopenharmony_ci int (*notify_poll_out)(struct vsock_sock *, size_t, bool *); 1418c2ecf20Sopenharmony_ci int (*notify_recv_init)(struct vsock_sock *, size_t, 1428c2ecf20Sopenharmony_ci struct vsock_transport_recv_notify_data *); 1438c2ecf20Sopenharmony_ci int (*notify_recv_pre_block)(struct vsock_sock *, size_t, 1448c2ecf20Sopenharmony_ci struct vsock_transport_recv_notify_data *); 1458c2ecf20Sopenharmony_ci int (*notify_recv_pre_dequeue)(struct vsock_sock *, size_t, 1468c2ecf20Sopenharmony_ci struct vsock_transport_recv_notify_data *); 1478c2ecf20Sopenharmony_ci int (*notify_recv_post_dequeue)(struct vsock_sock *, size_t, 1488c2ecf20Sopenharmony_ci ssize_t, bool, struct vsock_transport_recv_notify_data *); 1498c2ecf20Sopenharmony_ci int (*notify_send_init)(struct vsock_sock *, 1508c2ecf20Sopenharmony_ci struct vsock_transport_send_notify_data *); 1518c2ecf20Sopenharmony_ci int (*notify_send_pre_block)(struct vsock_sock *, 1528c2ecf20Sopenharmony_ci struct vsock_transport_send_notify_data *); 1538c2ecf20Sopenharmony_ci int (*notify_send_pre_enqueue)(struct vsock_sock *, 1548c2ecf20Sopenharmony_ci struct vsock_transport_send_notify_data *); 1558c2ecf20Sopenharmony_ci int (*notify_send_post_enqueue)(struct vsock_sock *, ssize_t, 1568c2ecf20Sopenharmony_ci struct vsock_transport_send_notify_data *); 1578c2ecf20Sopenharmony_ci /* sk_lock held by the caller */ 1588c2ecf20Sopenharmony_ci void (*notify_buffer_size)(struct vsock_sock *, u64 *); 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci /* Shutdown. */ 1618c2ecf20Sopenharmony_ci int (*shutdown)(struct vsock_sock *, int); 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci /* Addressing. */ 1648c2ecf20Sopenharmony_ci u32 (*get_local_cid)(void); 1658c2ecf20Sopenharmony_ci}; 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci/**** CORE ****/ 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ciint vsock_core_register(const struct vsock_transport *t, int features); 1708c2ecf20Sopenharmony_civoid vsock_core_unregister(const struct vsock_transport *t); 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci/* The transport may downcast this to access transport-specific functions */ 1738c2ecf20Sopenharmony_ciconst struct vsock_transport *vsock_core_get_transport(struct vsock_sock *vsk); 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci/**** UTILS ****/ 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci/* vsock_table_lock must be held */ 1788c2ecf20Sopenharmony_cistatic inline bool __vsock_in_bound_table(struct vsock_sock *vsk) 1798c2ecf20Sopenharmony_ci{ 1808c2ecf20Sopenharmony_ci return !list_empty(&vsk->bound_table); 1818c2ecf20Sopenharmony_ci} 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci/* vsock_table_lock must be held */ 1848c2ecf20Sopenharmony_cistatic inline bool __vsock_in_connected_table(struct vsock_sock *vsk) 1858c2ecf20Sopenharmony_ci{ 1868c2ecf20Sopenharmony_ci return !list_empty(&vsk->connected_table); 1878c2ecf20Sopenharmony_ci} 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_civoid vsock_release_pending(struct sock *pending); 1908c2ecf20Sopenharmony_civoid vsock_add_pending(struct sock *listener, struct sock *pending); 1918c2ecf20Sopenharmony_civoid vsock_remove_pending(struct sock *listener, struct sock *pending); 1928c2ecf20Sopenharmony_civoid vsock_enqueue_accept(struct sock *listener, struct sock *connected); 1938c2ecf20Sopenharmony_civoid vsock_insert_connected(struct vsock_sock *vsk); 1948c2ecf20Sopenharmony_civoid vsock_remove_bound(struct vsock_sock *vsk); 1958c2ecf20Sopenharmony_civoid vsock_remove_connected(struct vsock_sock *vsk); 1968c2ecf20Sopenharmony_cistruct sock *vsock_find_bound_socket(struct sockaddr_vm *addr); 1978c2ecf20Sopenharmony_cistruct sock *vsock_find_connected_socket(struct sockaddr_vm *src, 1988c2ecf20Sopenharmony_ci struct sockaddr_vm *dst); 1998c2ecf20Sopenharmony_civoid vsock_remove_sock(struct vsock_sock *vsk); 2008c2ecf20Sopenharmony_civoid vsock_for_each_connected_socket(struct vsock_transport *transport, 2018c2ecf20Sopenharmony_ci void (*fn)(struct sock *sk)); 2028c2ecf20Sopenharmony_ciint vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk); 2038c2ecf20Sopenharmony_cibool vsock_find_cid(unsigned int cid); 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci/**** TAP ****/ 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_cistruct vsock_tap { 2088c2ecf20Sopenharmony_ci struct net_device *dev; 2098c2ecf20Sopenharmony_ci struct module *module; 2108c2ecf20Sopenharmony_ci struct list_head list; 2118c2ecf20Sopenharmony_ci}; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ciint vsock_init_tap(void); 2148c2ecf20Sopenharmony_ciint vsock_add_tap(struct vsock_tap *vt); 2158c2ecf20Sopenharmony_ciint vsock_remove_tap(struct vsock_tap *vt); 2168c2ecf20Sopenharmony_civoid vsock_deliver_tap(struct sk_buff *build_skb(void *opaque), void *opaque); 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci#endif /* __AF_VSOCK_H__ */ 219