af_vsock.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * VMware vSockets Driver
  4. *
  5. * Copyright (C) 2007-2013 VMware, Inc. All rights reserved.
  6. */
  7. #ifndef __AF_VSOCK_H__
  8. #define __AF_VSOCK_H__
  9. #include <linux/kernel.h>
  10. #include <linux/workqueue.h>
  11. #include <net/netns/vsock.h>
  12. #include <net/sock.h>
  13. #include <uapi/linux/vm_sockets.h>
  14. #include "vsock_addr.h"
  15. #define LAST_RESERVED_PORT 1023
  16. #define VSOCK_HASH_SIZE 251
  17. extern struct list_head vsock_bind_table[VSOCK_HASH_SIZE + 1];
  18. extern struct list_head vsock_connected_table[VSOCK_HASH_SIZE];
  19. extern spinlock_t vsock_table_lock;
  20. #define vsock_sk(__sk) ((struct vsock_sock *)__sk)
  21. #define sk_vsock(__vsk) (&(__vsk)->sk)
  22. struct vsock_sock {
  23. /* sk must be the first member. */
  24. struct sock sk;
  25. const struct vsock_transport *transport;
  26. struct sockaddr_vm local_addr;
  27. struct sockaddr_vm remote_addr;
  28. /* Links for the global tables of bound and connected sockets. */
  29. struct list_head bound_table;
  30. struct list_head connected_table;
  31. /* Accessed without the socket lock held. This means it can never be
  32. * modified outsided of socket create or destruct.
  33. */
  34. bool trusted;
  35. bool cached_peer_allow_dgram; /* Dgram communication allowed to
  36. * cached peer?
  37. */
  38. u32 cached_peer; /* Context ID of last dgram destination check. */
  39. const struct cred *owner;
  40. /* Rest are SOCK_STREAM only. */
  41. long connect_timeout;
  42. /* Listening socket that this came from. */
  43. struct sock *listener;
  44. /* Used for pending list and accept queue during connection handshake.
  45. * The listening socket is the head for both lists. Sockets created
  46. * for connection requests are placed in the pending list until they
  47. * are connected, at which point they are put in the accept queue list
  48. * so they can be accepted in accept(). If accept() cannot accept the
  49. * connection, it is marked as rejected so the cleanup function knows
  50. * to clean up the socket.
  51. */
  52. struct list_head pending_links;
  53. struct list_head accept_queue;
  54. bool rejected;
  55. struct delayed_work connect_work;
  56. struct delayed_work pending_work;
  57. struct delayed_work close_work;
  58. bool close_work_scheduled;
  59. u32 peer_shutdown;
  60. bool sent_request;
  61. bool ignore_connecting_rst;
  62. /* Protected by lock_sock(sk) */
  63. u64 buffer_size;
  64. u64 buffer_min_size;
  65. u64 buffer_max_size;
  66. /* Private to transport. */
  67. void *trans;
  68. };
  69. s64 vsock_connectible_has_data(struct vsock_sock *vsk);
  70. s64 vsock_stream_has_data(struct vsock_sock *vsk);
  71. s64 vsock_stream_has_space(struct vsock_sock *vsk);
  72. struct sock *vsock_create_connected(struct sock *parent);
  73. void vsock_data_ready(struct sock *sk);
  74. /**** TRANSPORT ****/
  75. struct vsock_transport_recv_notify_data {
  76. u64 data1; /* Transport-defined. */
  77. u64 data2; /* Transport-defined. */
  78. bool notify_on_block;
  79. };
  80. struct vsock_transport_send_notify_data {
  81. u64 data1; /* Transport-defined. */
  82. u64 data2; /* Transport-defined. */
  83. };
  84. /* Transport features flags */
  85. /* Transport provides host->guest communication */
  86. #define VSOCK_TRANSPORT_F_H2G 0x00000001
  87. /* Transport provides guest->host communication */
  88. #define VSOCK_TRANSPORT_F_G2H 0x00000002
  89. /* Transport provides DGRAM communication */
  90. #define VSOCK_TRANSPORT_F_DGRAM 0x00000004
  91. /* Transport provides local (loopback) communication */
  92. #define VSOCK_TRANSPORT_F_LOCAL 0x00000008
  93. struct vsock_transport {
  94. struct module *module;
  95. /* Initialize/tear-down socket. */
  96. int (*init)(struct vsock_sock *, struct vsock_sock *);
  97. void (*destruct)(struct vsock_sock *);
  98. void (*release)(struct vsock_sock *);
  99. /* Cancel all pending packets sent on vsock. */
  100. int (*cancel_pkt)(struct vsock_sock *vsk);
  101. /* Connections. */
  102. int (*connect)(struct vsock_sock *);
  103. /* DGRAM. */
  104. int (*dgram_bind)(struct vsock_sock *, struct sockaddr_vm *);
  105. int (*dgram_dequeue)(struct vsock_sock *vsk, struct msghdr *msg,
  106. size_t len, int flags);
  107. int (*dgram_enqueue)(struct vsock_sock *, struct sockaddr_vm *,
  108. struct msghdr *, size_t len);
  109. bool (*dgram_allow)(struct vsock_sock *vsk, u32 cid, u32 port);
  110. /* STREAM. */
  111. /* TODO: stream_bind() */
  112. ssize_t (*stream_dequeue)(struct vsock_sock *, struct msghdr *,
  113. size_t len, int flags);
  114. ssize_t (*stream_enqueue)(struct vsock_sock *, struct msghdr *,
  115. size_t len);
  116. s64 (*stream_has_data)(struct vsock_sock *);
  117. s64 (*stream_has_space)(struct vsock_sock *);
  118. u64 (*stream_rcvhiwat)(struct vsock_sock *);
  119. bool (*stream_is_active)(struct vsock_sock *);
  120. bool (*stream_allow)(struct vsock_sock *vsk, u32 cid, u32 port);
  121. /* SEQ_PACKET. */
  122. ssize_t (*seqpacket_dequeue)(struct vsock_sock *vsk, struct msghdr *msg,
  123. int flags);
  124. int (*seqpacket_enqueue)(struct vsock_sock *vsk, struct msghdr *msg,
  125. size_t len);
  126. bool (*seqpacket_allow)(struct vsock_sock *vsk, u32 remote_cid);
  127. u32 (*seqpacket_has_data)(struct vsock_sock *vsk);
  128. /* Notification. */
  129. int (*notify_poll_in)(struct vsock_sock *, size_t, bool *);
  130. int (*notify_poll_out)(struct vsock_sock *, size_t, bool *);
  131. int (*notify_recv_init)(struct vsock_sock *, size_t,
  132. struct vsock_transport_recv_notify_data *);
  133. int (*notify_recv_pre_block)(struct vsock_sock *, size_t,
  134. struct vsock_transport_recv_notify_data *);
  135. int (*notify_recv_pre_dequeue)(struct vsock_sock *, size_t,
  136. struct vsock_transport_recv_notify_data *);
  137. int (*notify_recv_post_dequeue)(struct vsock_sock *, size_t,
  138. ssize_t, bool, struct vsock_transport_recv_notify_data *);
  139. int (*notify_send_init)(struct vsock_sock *,
  140. struct vsock_transport_send_notify_data *);
  141. int (*notify_send_pre_block)(struct vsock_sock *,
  142. struct vsock_transport_send_notify_data *);
  143. int (*notify_send_pre_enqueue)(struct vsock_sock *,
  144. struct vsock_transport_send_notify_data *);
  145. int (*notify_send_post_enqueue)(struct vsock_sock *, ssize_t,
  146. struct vsock_transport_send_notify_data *);
  147. /* sk_lock held by the caller */
  148. void (*notify_buffer_size)(struct vsock_sock *, u64 *);
  149. int (*notify_set_rcvlowat)(struct vsock_sock *vsk, int val);
  150. /* SIOCOUTQ ioctl */
  151. ssize_t (*unsent_bytes)(struct vsock_sock *vsk);
  152. /* Shutdown. */
  153. int (*shutdown)(struct vsock_sock *, int);
  154. /* Addressing. */
  155. u32 (*get_local_cid)(void);
  156. /* Read a single skb */
  157. int (*read_skb)(struct vsock_sock *, skb_read_actor_t);
  158. /* Zero-copy. */
  159. bool (*msgzerocopy_allow)(void);
  160. };
  161. /**** CORE ****/
  162. int vsock_core_register(const struct vsock_transport *t, int features);
  163. void vsock_core_unregister(const struct vsock_transport *t);
  164. /* The transport may downcast this to access transport-specific functions */
  165. const struct vsock_transport *vsock_core_get_transport(struct vsock_sock *vsk);
  166. /**** UTILS ****/
  167. /* vsock_table_lock must be held */
  168. static inline bool __vsock_in_bound_table(struct vsock_sock *vsk)
  169. {
  170. return !list_empty(&vsk->bound_table);
  171. }
  172. /* vsock_table_lock must be held */
  173. static inline bool __vsock_in_connected_table(struct vsock_sock *vsk)
  174. {
  175. return !list_empty(&vsk->connected_table);
  176. }
  177. void vsock_add_pending(struct sock *listener, struct sock *pending);
  178. void vsock_remove_pending(struct sock *listener, struct sock *pending);
  179. void vsock_enqueue_accept(struct sock *listener, struct sock *connected);
  180. void vsock_insert_connected(struct vsock_sock *vsk);
  181. void vsock_remove_bound(struct vsock_sock *vsk);
  182. void vsock_remove_connected(struct vsock_sock *vsk);
  183. struct sock *vsock_find_bound_socket(struct sockaddr_vm *addr);
  184. struct sock *vsock_find_connected_socket(struct sockaddr_vm *src,
  185. struct sockaddr_vm *dst);
  186. struct sock *vsock_find_bound_socket_net(struct sockaddr_vm *addr,
  187. struct net *net);
  188. struct sock *vsock_find_connected_socket_net(struct sockaddr_vm *src,
  189. struct sockaddr_vm *dst,
  190. struct net *net);
  191. void vsock_remove_sock(struct vsock_sock *vsk);
  192. void vsock_for_each_connected_socket(struct vsock_transport *transport,
  193. void (*fn)(struct sock *sk));
  194. int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk);
  195. bool vsock_find_cid(unsigned int cid);
  196. void vsock_linger(struct sock *sk);
  197. /**** TAP ****/
  198. struct vsock_tap {
  199. struct net_device *dev;
  200. struct module *module;
  201. struct list_head list;
  202. };
  203. int vsock_add_tap(struct vsock_tap *vt);
  204. int vsock_remove_tap(struct vsock_tap *vt);
  205. void vsock_deliver_tap(struct sk_buff *build_skb(void *opaque), void *opaque);
  206. int __vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  207. int flags);
  208. int vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  209. int flags);
  210. int __vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg,
  211. size_t len, int flags);
  212. int vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg,
  213. size_t len, int flags);
  214. extern struct proto vsock_proto;
  215. #ifdef CONFIG_BPF_SYSCALL
  216. int vsock_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore);
  217. void __init vsock_bpf_build_proto(void);
  218. #else
  219. static inline void __init vsock_bpf_build_proto(void)
  220. {}
  221. #endif
  222. static inline bool vsock_msgzerocopy_allow(const struct vsock_transport *t)
  223. {
  224. return t->msgzerocopy_allow && t->msgzerocopy_allow();
  225. }
  226. static inline enum vsock_net_mode vsock_net_mode(struct net *net)
  227. {
  228. if (!net)
  229. return VSOCK_NET_MODE_GLOBAL;
  230. return READ_ONCE(net->vsock.mode);
  231. }
  232. static inline bool vsock_net_mode_global(struct vsock_sock *vsk)
  233. {
  234. return vsock_net_mode(sock_net(sk_vsock(vsk))) == VSOCK_NET_MODE_GLOBAL;
  235. }
  236. static inline bool vsock_net_set_child_mode(struct net *net,
  237. enum vsock_net_mode mode)
  238. {
  239. int new_locked = mode + 1;
  240. int old_locked = 0; /* unlocked */
  241. if (try_cmpxchg(&net->vsock.child_ns_mode_locked,
  242. &old_locked, new_locked)) {
  243. WRITE_ONCE(net->vsock.child_ns_mode, mode);
  244. return true;
  245. }
  246. return old_locked == new_locked;
  247. }
  248. static inline enum vsock_net_mode vsock_net_child_mode(struct net *net)
  249. {
  250. return READ_ONCE(net->vsock.child_ns_mode);
  251. }
  252. /* Return true if two namespaces pass the mode rules. Otherwise, return false.
  253. *
  254. * A NULL namespace is treated as VSOCK_NET_MODE_GLOBAL.
  255. *
  256. * Read more about modes in the comment header of net/vmw_vsock/af_vsock.c.
  257. */
  258. static inline bool vsock_net_check_mode(struct net *ns0, struct net *ns1)
  259. {
  260. enum vsock_net_mode mode0, mode1;
  261. /* Any vsocks within the same network namespace are always reachable,
  262. * regardless of the mode.
  263. */
  264. if (net_eq(ns0, ns1))
  265. return true;
  266. mode0 = vsock_net_mode(ns0);
  267. mode1 = vsock_net_mode(ns1);
  268. /* Different namespaces are only reachable if they are both
  269. * global mode.
  270. */
  271. return mode0 == VSOCK_NET_MODE_GLOBAL && mode0 == mode1;
  272. }
  273. #endif /* __AF_VSOCK_H__ */