162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * VMware vSockets Driver 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2007-2013 VMware, Inc. All rights reserved. 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci#ifndef __AF_VSOCK_H__ 962306a36Sopenharmony_ci#define __AF_VSOCK_H__ 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci#include <linux/kernel.h> 1262306a36Sopenharmony_ci#include <linux/workqueue.h> 1362306a36Sopenharmony_ci#include <net/sock.h> 1462306a36Sopenharmony_ci#include <uapi/linux/vm_sockets.h> 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ci#include "vsock_addr.h" 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci#define LAST_RESERVED_PORT 1023 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci#define VSOCK_HASH_SIZE 251 2162306a36Sopenharmony_ciextern struct list_head vsock_bind_table[VSOCK_HASH_SIZE + 1]; 2262306a36Sopenharmony_ciextern struct list_head vsock_connected_table[VSOCK_HASH_SIZE]; 2362306a36Sopenharmony_ciextern spinlock_t vsock_table_lock; 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci#define vsock_sk(__sk) ((struct vsock_sock *)__sk) 2662306a36Sopenharmony_ci#define sk_vsock(__vsk) (&(__vsk)->sk) 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_cistruct vsock_sock { 2962306a36Sopenharmony_ci /* sk must be the first member. */ 3062306a36Sopenharmony_ci struct sock sk; 3162306a36Sopenharmony_ci const struct vsock_transport *transport; 3262306a36Sopenharmony_ci struct sockaddr_vm local_addr; 3362306a36Sopenharmony_ci struct sockaddr_vm remote_addr; 3462306a36Sopenharmony_ci /* Links for the global tables of bound and connected sockets. */ 3562306a36Sopenharmony_ci struct list_head bound_table; 3662306a36Sopenharmony_ci struct list_head connected_table; 3762306a36Sopenharmony_ci /* Accessed without the socket lock held. This means it can never be 3862306a36Sopenharmony_ci * modified outsided of socket create or destruct. 3962306a36Sopenharmony_ci */ 4062306a36Sopenharmony_ci bool trusted; 4162306a36Sopenharmony_ci bool cached_peer_allow_dgram; /* Dgram communication allowed to 4262306a36Sopenharmony_ci * cached peer? 4362306a36Sopenharmony_ci */ 4462306a36Sopenharmony_ci u32 cached_peer; /* Context ID of last dgram destination check. */ 4562306a36Sopenharmony_ci const struct cred *owner; 4662306a36Sopenharmony_ci /* Rest are SOCK_STREAM only. */ 4762306a36Sopenharmony_ci long connect_timeout; 4862306a36Sopenharmony_ci /* Listening socket that this came from. */ 4962306a36Sopenharmony_ci struct sock *listener; 5062306a36Sopenharmony_ci /* Used for pending list and accept queue during connection handshake. 5162306a36Sopenharmony_ci * The listening socket is the head for both lists. Sockets created 5262306a36Sopenharmony_ci * for connection requests are placed in the pending list until they 5362306a36Sopenharmony_ci * are connected, at which point they are put in the accept queue list 5462306a36Sopenharmony_ci * so they can be accepted in accept(). If accept() cannot accept the 5562306a36Sopenharmony_ci * connection, it is marked as rejected so the cleanup function knows 5662306a36Sopenharmony_ci * to clean up the socket. 5762306a36Sopenharmony_ci */ 5862306a36Sopenharmony_ci struct list_head pending_links; 5962306a36Sopenharmony_ci struct list_head accept_queue; 6062306a36Sopenharmony_ci bool rejected; 6162306a36Sopenharmony_ci struct delayed_work connect_work; 6262306a36Sopenharmony_ci struct delayed_work pending_work; 6362306a36Sopenharmony_ci struct delayed_work close_work; 6462306a36Sopenharmony_ci bool close_work_scheduled; 6562306a36Sopenharmony_ci u32 peer_shutdown; 6662306a36Sopenharmony_ci bool sent_request; 6762306a36Sopenharmony_ci bool ignore_connecting_rst; 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci /* Protected by lock_sock(sk) */ 7062306a36Sopenharmony_ci u64 buffer_size; 7162306a36Sopenharmony_ci u64 buffer_min_size; 7262306a36Sopenharmony_ci u64 buffer_max_size; 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci /* Private to transport. */ 7562306a36Sopenharmony_ci void *trans; 7662306a36Sopenharmony_ci}; 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_cis64 vsock_connectible_has_data(struct vsock_sock *vsk); 7962306a36Sopenharmony_cis64 vsock_stream_has_data(struct vsock_sock *vsk); 8062306a36Sopenharmony_cis64 vsock_stream_has_space(struct vsock_sock *vsk); 8162306a36Sopenharmony_cistruct sock *vsock_create_connected(struct sock *parent); 8262306a36Sopenharmony_civoid vsock_data_ready(struct sock *sk); 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci/**** TRANSPORT ****/ 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_cistruct vsock_transport_recv_notify_data { 8762306a36Sopenharmony_ci u64 data1; /* Transport-defined. */ 8862306a36Sopenharmony_ci u64 data2; /* Transport-defined. */ 8962306a36Sopenharmony_ci bool notify_on_block; 9062306a36Sopenharmony_ci}; 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_cistruct vsock_transport_send_notify_data { 9362306a36Sopenharmony_ci u64 data1; /* Transport-defined. */ 9462306a36Sopenharmony_ci u64 data2; /* Transport-defined. */ 9562306a36Sopenharmony_ci}; 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ci/* Transport features flags */ 9862306a36Sopenharmony_ci/* Transport provides host->guest communication */ 9962306a36Sopenharmony_ci#define VSOCK_TRANSPORT_F_H2G 0x00000001 10062306a36Sopenharmony_ci/* Transport provides guest->host communication */ 10162306a36Sopenharmony_ci#define VSOCK_TRANSPORT_F_G2H 0x00000002 10262306a36Sopenharmony_ci/* Transport provides DGRAM communication */ 10362306a36Sopenharmony_ci#define VSOCK_TRANSPORT_F_DGRAM 0x00000004 10462306a36Sopenharmony_ci/* Transport provides local (loopback) communication */ 10562306a36Sopenharmony_ci#define VSOCK_TRANSPORT_F_LOCAL 0x00000008 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_cistruct vsock_transport { 10862306a36Sopenharmony_ci struct module *module; 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci /* Initialize/tear-down socket. */ 11162306a36Sopenharmony_ci int (*init)(struct vsock_sock *, struct vsock_sock *); 11262306a36Sopenharmony_ci void (*destruct)(struct vsock_sock *); 11362306a36Sopenharmony_ci void (*release)(struct vsock_sock *); 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci /* Cancel all pending packets sent on vsock. */ 11662306a36Sopenharmony_ci int (*cancel_pkt)(struct vsock_sock *vsk); 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci /* Connections. */ 11962306a36Sopenharmony_ci int (*connect)(struct vsock_sock *); 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci /* DGRAM. */ 12262306a36Sopenharmony_ci int (*dgram_bind)(struct vsock_sock *, struct sockaddr_vm *); 12362306a36Sopenharmony_ci int (*dgram_dequeue)(struct vsock_sock *vsk, struct msghdr *msg, 12462306a36Sopenharmony_ci size_t len, int flags); 12562306a36Sopenharmony_ci int (*dgram_enqueue)(struct vsock_sock *, struct sockaddr_vm *, 12662306a36Sopenharmony_ci struct msghdr *, size_t len); 12762306a36Sopenharmony_ci bool (*dgram_allow)(u32 cid, u32 port); 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ci /* STREAM. */ 13062306a36Sopenharmony_ci /* TODO: stream_bind() */ 13162306a36Sopenharmony_ci ssize_t (*stream_dequeue)(struct vsock_sock *, struct msghdr *, 13262306a36Sopenharmony_ci size_t len, int flags); 13362306a36Sopenharmony_ci ssize_t (*stream_enqueue)(struct vsock_sock *, struct msghdr *, 13462306a36Sopenharmony_ci size_t len); 13562306a36Sopenharmony_ci s64 (*stream_has_data)(struct vsock_sock *); 13662306a36Sopenharmony_ci s64 (*stream_has_space)(struct vsock_sock *); 13762306a36Sopenharmony_ci u64 (*stream_rcvhiwat)(struct vsock_sock *); 13862306a36Sopenharmony_ci bool (*stream_is_active)(struct vsock_sock *); 13962306a36Sopenharmony_ci bool (*stream_allow)(u32 cid, u32 port); 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ci /* SEQ_PACKET. */ 14262306a36Sopenharmony_ci ssize_t (*seqpacket_dequeue)(struct vsock_sock *vsk, struct msghdr *msg, 14362306a36Sopenharmony_ci int flags); 14462306a36Sopenharmony_ci int (*seqpacket_enqueue)(struct vsock_sock *vsk, struct msghdr *msg, 14562306a36Sopenharmony_ci size_t len); 14662306a36Sopenharmony_ci bool (*seqpacket_allow)(u32 remote_cid); 14762306a36Sopenharmony_ci u32 (*seqpacket_has_data)(struct vsock_sock *vsk); 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci /* Notification. */ 15062306a36Sopenharmony_ci int (*notify_poll_in)(struct vsock_sock *, size_t, bool *); 15162306a36Sopenharmony_ci int (*notify_poll_out)(struct vsock_sock *, size_t, bool *); 15262306a36Sopenharmony_ci int (*notify_recv_init)(struct vsock_sock *, size_t, 15362306a36Sopenharmony_ci struct vsock_transport_recv_notify_data *); 15462306a36Sopenharmony_ci int (*notify_recv_pre_block)(struct vsock_sock *, size_t, 15562306a36Sopenharmony_ci struct vsock_transport_recv_notify_data *); 15662306a36Sopenharmony_ci int (*notify_recv_pre_dequeue)(struct vsock_sock *, size_t, 15762306a36Sopenharmony_ci struct vsock_transport_recv_notify_data *); 15862306a36Sopenharmony_ci int (*notify_recv_post_dequeue)(struct vsock_sock *, size_t, 15962306a36Sopenharmony_ci ssize_t, bool, struct vsock_transport_recv_notify_data *); 16062306a36Sopenharmony_ci int (*notify_send_init)(struct vsock_sock *, 16162306a36Sopenharmony_ci struct vsock_transport_send_notify_data *); 16262306a36Sopenharmony_ci int (*notify_send_pre_block)(struct vsock_sock *, 16362306a36Sopenharmony_ci struct vsock_transport_send_notify_data *); 16462306a36Sopenharmony_ci int (*notify_send_pre_enqueue)(struct vsock_sock *, 16562306a36Sopenharmony_ci struct vsock_transport_send_notify_data *); 16662306a36Sopenharmony_ci int (*notify_send_post_enqueue)(struct vsock_sock *, ssize_t, 16762306a36Sopenharmony_ci struct vsock_transport_send_notify_data *); 16862306a36Sopenharmony_ci /* sk_lock held by the caller */ 16962306a36Sopenharmony_ci void (*notify_buffer_size)(struct vsock_sock *, u64 *); 17062306a36Sopenharmony_ci int (*notify_set_rcvlowat)(struct vsock_sock *vsk, int val); 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci /* Shutdown. */ 17362306a36Sopenharmony_ci int (*shutdown)(struct vsock_sock *, int); 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci /* Addressing. */ 17662306a36Sopenharmony_ci u32 (*get_local_cid)(void); 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci /* Read a single skb */ 17962306a36Sopenharmony_ci int (*read_skb)(struct vsock_sock *, skb_read_actor_t); 18062306a36Sopenharmony_ci}; 18162306a36Sopenharmony_ci 18262306a36Sopenharmony_ci/**** CORE ****/ 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ciint vsock_core_register(const struct vsock_transport *t, int features); 18562306a36Sopenharmony_civoid vsock_core_unregister(const struct vsock_transport *t); 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ci/* The transport may downcast this to access transport-specific functions */ 18862306a36Sopenharmony_ciconst struct vsock_transport *vsock_core_get_transport(struct vsock_sock *vsk); 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci/**** UTILS ****/ 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci/* vsock_table_lock must be held */ 19362306a36Sopenharmony_cistatic inline bool __vsock_in_bound_table(struct vsock_sock *vsk) 19462306a36Sopenharmony_ci{ 19562306a36Sopenharmony_ci return !list_empty(&vsk->bound_table); 19662306a36Sopenharmony_ci} 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_ci/* vsock_table_lock must be held */ 19962306a36Sopenharmony_cistatic inline bool __vsock_in_connected_table(struct vsock_sock *vsk) 20062306a36Sopenharmony_ci{ 20162306a36Sopenharmony_ci return !list_empty(&vsk->connected_table); 20262306a36Sopenharmony_ci} 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_civoid vsock_add_pending(struct sock *listener, struct sock *pending); 20562306a36Sopenharmony_civoid vsock_remove_pending(struct sock *listener, struct sock *pending); 20662306a36Sopenharmony_civoid vsock_enqueue_accept(struct sock *listener, struct sock *connected); 20762306a36Sopenharmony_civoid vsock_insert_connected(struct vsock_sock *vsk); 20862306a36Sopenharmony_civoid vsock_remove_bound(struct vsock_sock *vsk); 20962306a36Sopenharmony_civoid vsock_remove_connected(struct vsock_sock *vsk); 21062306a36Sopenharmony_cistruct sock *vsock_find_bound_socket(struct sockaddr_vm *addr); 21162306a36Sopenharmony_cistruct sock *vsock_find_connected_socket(struct sockaddr_vm *src, 21262306a36Sopenharmony_ci struct sockaddr_vm *dst); 21362306a36Sopenharmony_civoid vsock_remove_sock(struct vsock_sock *vsk); 21462306a36Sopenharmony_civoid vsock_for_each_connected_socket(struct vsock_transport *transport, 21562306a36Sopenharmony_ci void (*fn)(struct sock *sk)); 21662306a36Sopenharmony_ciint vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk); 21762306a36Sopenharmony_cibool vsock_find_cid(unsigned int cid); 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_ci/**** TAP ****/ 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_cistruct vsock_tap { 22262306a36Sopenharmony_ci struct net_device *dev; 22362306a36Sopenharmony_ci struct module *module; 22462306a36Sopenharmony_ci struct list_head list; 22562306a36Sopenharmony_ci}; 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ciint vsock_add_tap(struct vsock_tap *vt); 22862306a36Sopenharmony_ciint vsock_remove_tap(struct vsock_tap *vt); 22962306a36Sopenharmony_civoid vsock_deliver_tap(struct sk_buff *build_skb(void *opaque), void *opaque); 23062306a36Sopenharmony_ciint vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, 23162306a36Sopenharmony_ci int flags); 23262306a36Sopenharmony_ciint vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg, 23362306a36Sopenharmony_ci size_t len, int flags); 23462306a36Sopenharmony_ci 23562306a36Sopenharmony_ci#ifdef CONFIG_BPF_SYSCALL 23662306a36Sopenharmony_ciextern struct proto vsock_proto; 23762306a36Sopenharmony_ciint vsock_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore); 23862306a36Sopenharmony_civoid __init vsock_bpf_build_proto(void); 23962306a36Sopenharmony_ci#else 24062306a36Sopenharmony_cistatic inline void __init vsock_bpf_build_proto(void) 24162306a36Sopenharmony_ci{} 24262306a36Sopenharmony_ci#endif 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ci#endif /* __AF_VSOCK_H__ */ 245