connection.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (C) 2018 Samsung Electronics Co., Ltd.
  4. */
  5. #ifndef __KSMBD_CONNECTION_H__
  6. #define __KSMBD_CONNECTION_H__
  7. #include <linux/list.h>
  8. #include <linux/ip.h>
  9. #include <net/sock.h>
  10. #include <net/tcp.h>
  11. #include <net/inet_connection_sock.h>
  12. #include <net/request_sock.h>
  13. #include <linux/kthread.h>
  14. #include <linux/nls.h>
  15. #include <linux/unicode.h>
  16. #include "smb_common.h"
  17. #include "ksmbd_work.h"
  18. #define KSMBD_SOCKET_BACKLOG 16
  19. enum {
  20. KSMBD_SESS_NEW = 0,
  21. KSMBD_SESS_GOOD,
  22. KSMBD_SESS_EXITING,
  23. KSMBD_SESS_NEED_RECONNECT,
  24. KSMBD_SESS_NEED_NEGOTIATE,
  25. KSMBD_SESS_NEED_SETUP,
  26. KSMBD_SESS_RELEASING
  27. };
  28. struct ksmbd_stats {
  29. atomic_t open_files_count;
  30. atomic64_t request_served;
  31. };
  32. struct ksmbd_transport;
  33. struct ksmbd_conn {
  34. struct smb_version_values *vals;
  35. struct smb_version_ops *ops;
  36. struct smb_version_cmds *cmds;
  37. unsigned int max_cmds;
  38. struct mutex srv_mutex;
  39. int status;
  40. unsigned int cli_cap;
  41. union {
  42. __be32 inet_addr;
  43. #if IS_ENABLED(CONFIG_IPV6)
  44. u8 inet6_addr[16];
  45. #endif
  46. };
  47. char *request_buf;
  48. struct ksmbd_transport *transport;
  49. struct nls_table *local_nls;
  50. struct unicode_map *um;
  51. struct list_head conns_list;
  52. struct rw_semaphore session_lock;
  53. /* smb session 1 per user */
  54. struct xarray sessions;
  55. unsigned long last_active;
  56. /* How many request are running currently */
  57. atomic_t req_running;
  58. /* References which are made for this Server object*/
  59. atomic_t r_count;
  60. unsigned int total_credits;
  61. unsigned int outstanding_credits;
  62. spinlock_t credits_lock;
  63. wait_queue_head_t req_running_q;
  64. wait_queue_head_t r_count_q;
  65. /* Lock to protect requests list*/
  66. spinlock_t request_lock;
  67. struct list_head requests;
  68. struct list_head async_requests;
  69. int connection_type;
  70. struct ksmbd_stats stats;
  71. char ClientGUID[SMB2_CLIENT_GUID_SIZE];
  72. struct ntlmssp_auth ntlmssp;
  73. spinlock_t llist_lock;
  74. struct list_head lock_list;
  75. struct preauth_integrity_info *preauth_info;
  76. bool need_neg;
  77. unsigned int auth_mechs;
  78. unsigned int preferred_auth_mech;
  79. bool sign;
  80. bool use_spnego:1;
  81. __u16 cli_sec_mode;
  82. __u16 srv_sec_mode;
  83. /* dialect index that server chose */
  84. __u16 dialect;
  85. char *mechToken;
  86. unsigned int mechTokenLen;
  87. struct ksmbd_conn_ops *conn_ops;
  88. /* Preauth Session Table */
  89. struct list_head preauth_sess_table;
  90. struct sockaddr_storage peer_addr;
  91. /* Identifier for async message */
  92. struct ida async_ida;
  93. __le16 cipher_type;
  94. __le16 compress_algorithm;
  95. bool posix_ext_supported;
  96. bool signing_negotiated;
  97. __le16 signing_algorithm;
  98. bool binding;
  99. atomic_t refcnt;
  100. bool is_aapl;
  101. };
  102. struct ksmbd_conn_ops {
  103. int (*process_fn)(struct ksmbd_conn *conn);
  104. int (*terminate_fn)(struct ksmbd_conn *conn);
  105. };
  106. struct ksmbd_transport_ops {
  107. int (*prepare)(struct ksmbd_transport *t);
  108. void (*disconnect)(struct ksmbd_transport *t);
  109. void (*shutdown)(struct ksmbd_transport *t);
  110. int (*read)(struct ksmbd_transport *t, char *buf,
  111. unsigned int size, int max_retries);
  112. int (*writev)(struct ksmbd_transport *t, struct kvec *iovs, int niov,
  113. int size, bool need_invalidate_rkey,
  114. unsigned int remote_key);
  115. int (*rdma_read)(struct ksmbd_transport *t,
  116. void *buf, unsigned int len,
  117. struct smb2_buffer_desc_v1 *desc,
  118. unsigned int desc_len);
  119. int (*rdma_write)(struct ksmbd_transport *t,
  120. void *buf, unsigned int len,
  121. struct smb2_buffer_desc_v1 *desc,
  122. unsigned int desc_len);
  123. void (*free_transport)(struct ksmbd_transport *kt);
  124. };
  125. struct ksmbd_transport {
  126. struct ksmbd_conn *conn;
  127. const struct ksmbd_transport_ops *ops;
  128. };
  129. #define KSMBD_TCP_RECV_TIMEOUT (7 * HZ)
  130. #define KSMBD_TCP_SEND_TIMEOUT (5 * HZ)
  131. #define KSMBD_TCP_PEER_SOCKADDR(c) ((struct sockaddr *)&((c)->peer_addr))
  132. extern struct list_head conn_list;
  133. extern struct rw_semaphore conn_list_lock;
  134. bool ksmbd_conn_alive(struct ksmbd_conn *conn);
  135. void ksmbd_conn_wait_idle(struct ksmbd_conn *conn);
  136. int ksmbd_conn_wait_idle_sess_id(struct ksmbd_conn *curr_conn, u64 sess_id);
  137. struct ksmbd_conn *ksmbd_conn_alloc(void);
  138. void ksmbd_conn_free(struct ksmbd_conn *conn);
  139. bool ksmbd_conn_lookup_dialect(struct ksmbd_conn *c);
  140. int ksmbd_conn_write(struct ksmbd_work *work);
  141. int ksmbd_conn_rdma_read(struct ksmbd_conn *conn,
  142. void *buf, unsigned int buflen,
  143. struct smb2_buffer_desc_v1 *desc,
  144. unsigned int desc_len);
  145. int ksmbd_conn_rdma_write(struct ksmbd_conn *conn,
  146. void *buf, unsigned int buflen,
  147. struct smb2_buffer_desc_v1 *desc,
  148. unsigned int desc_len);
  149. void ksmbd_conn_enqueue_request(struct ksmbd_work *work);
  150. void ksmbd_conn_try_dequeue_request(struct ksmbd_work *work);
  151. void ksmbd_conn_init_server_callbacks(struct ksmbd_conn_ops *ops);
  152. int ksmbd_conn_handler_loop(void *p);
  153. int ksmbd_conn_transport_init(void);
  154. void ksmbd_conn_transport_destroy(void);
  155. void ksmbd_conn_lock(struct ksmbd_conn *conn);
  156. void ksmbd_conn_unlock(struct ksmbd_conn *conn);
  157. void ksmbd_conn_r_count_inc(struct ksmbd_conn *conn);
  158. void ksmbd_conn_r_count_dec(struct ksmbd_conn *conn);
  159. /*
  160. * WARNING
  161. *
  162. * This is a hack. We will move status to a proper place once we land
  163. * a multi-sessions support.
  164. */
  165. static inline bool ksmbd_conn_good(struct ksmbd_conn *conn)
  166. {
  167. return READ_ONCE(conn->status) == KSMBD_SESS_GOOD;
  168. }
  169. static inline bool ksmbd_conn_need_negotiate(struct ksmbd_conn *conn)
  170. {
  171. return READ_ONCE(conn->status) == KSMBD_SESS_NEED_NEGOTIATE;
  172. }
  173. static inline bool ksmbd_conn_need_setup(struct ksmbd_conn *conn)
  174. {
  175. return READ_ONCE(conn->status) == KSMBD_SESS_NEED_SETUP;
  176. }
  177. static inline bool ksmbd_conn_need_reconnect(struct ksmbd_conn *conn)
  178. {
  179. return READ_ONCE(conn->status) == KSMBD_SESS_NEED_RECONNECT;
  180. }
  181. static inline bool ksmbd_conn_exiting(struct ksmbd_conn *conn)
  182. {
  183. return READ_ONCE(conn->status) == KSMBD_SESS_EXITING;
  184. }
  185. static inline bool ksmbd_conn_releasing(struct ksmbd_conn *conn)
  186. {
  187. return READ_ONCE(conn->status) == KSMBD_SESS_RELEASING;
  188. }
  189. static inline void ksmbd_conn_set_new(struct ksmbd_conn *conn)
  190. {
  191. WRITE_ONCE(conn->status, KSMBD_SESS_NEW);
  192. }
  193. static inline void ksmbd_conn_set_good(struct ksmbd_conn *conn)
  194. {
  195. WRITE_ONCE(conn->status, KSMBD_SESS_GOOD);
  196. }
  197. static inline void ksmbd_conn_set_need_negotiate(struct ksmbd_conn *conn)
  198. {
  199. WRITE_ONCE(conn->status, KSMBD_SESS_NEED_NEGOTIATE);
  200. }
  201. static inline void ksmbd_conn_set_need_setup(struct ksmbd_conn *conn)
  202. {
  203. WRITE_ONCE(conn->status, KSMBD_SESS_NEED_SETUP);
  204. }
  205. static inline void ksmbd_conn_set_need_reconnect(struct ksmbd_conn *conn)
  206. {
  207. WRITE_ONCE(conn->status, KSMBD_SESS_NEED_RECONNECT);
  208. }
  209. static inline void ksmbd_conn_set_exiting(struct ksmbd_conn *conn)
  210. {
  211. WRITE_ONCE(conn->status, KSMBD_SESS_EXITING);
  212. }
  213. static inline void ksmbd_conn_set_releasing(struct ksmbd_conn *conn)
  214. {
  215. WRITE_ONCE(conn->status, KSMBD_SESS_RELEASING);
  216. }
  217. void ksmbd_all_conn_set_status(u64 sess_id, u32 status);
  218. #endif /* __CONNECTION_H__ */