1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Copyright (C) 2018 Samsung Electronics Co., Ltd. 4 */ 5 6 #ifndef __KSMBD_CONNECTION_H__ 7 #define __KSMBD_CONNECTION_H__ 8 9 #include <linux/list.h> 10 #include <linux/inet.h> 11 #include <linux/ip.h> 12 #include <net/sock.h> 13 #include <net/tcp.h> 14 #include <net/inet_connection_sock.h> 15 #include <net/request_sock.h> 16 #include <linux/kthread.h> 17 #include <linux/nls.h> 18 #include <linux/unicode.h> 19 #include <linux/workqueue.h> 20 #include <linux/bitmap.h> 21 22 #include "smb_common.h" 23 #include "ksmbd_work.h" 24 25 struct smbdirect_buffer_descriptor_v1; 26 struct ksmbd_session; 27 28 #define KSMBD_SOCKET_BACKLOG 16 29 30 /* 31 * Size of the per-connection SMB2 command sequence window. This mirrors 32 * SMB2_MAX_CREDITS, the maximum number of credits (and therefore the 33 * maximum number of outstanding sequence numbers) that can be granted on 34 * a connection. It must be a power of two so the window can be indexed as 35 * a ring. 36 */ 37 #define KSMBD_CMD_SEQ_WINDOW 8192 38 39 enum { 40 KSMBD_SESS_NEW = 0, 41 KSMBD_SESS_GOOD, 42 KSMBD_SESS_EXITING, 43 KSMBD_SESS_NEED_RECONNECT, 44 KSMBD_SESS_NEED_NEGOTIATE, 45 KSMBD_SESS_NEED_SETUP, 46 KSMBD_SESS_RELEASING 47 }; 48 49 struct ksmbd_conn_stats { 50 atomic_t open_files_count; 51 atomic64_t request_served; 52 }; 53 54 struct ksmbd_transport; 55 56 struct ksmbd_conn { 57 struct smb_version_values *vals; 58 struct smb_version_ops *ops; 59 struct smb_version_cmds *cmds; 60 unsigned int max_cmds; 61 struct mutex srv_mutex; 62 int status; 63 unsigned int cli_cap; 64 bool stop_called; 65 union { 66 __be32 inet_addr; 67 #if IS_ENABLED(CONFIG_IPV6) 68 u8 inet6_addr[16]; 69 #endif 70 }; 71 #if IS_ENABLED(CONFIG_IPV6) 72 bool is_ipv6; 73 #endif 74 unsigned int inet_hash; 75 char *request_buf; 76 struct ksmbd_transport *transport; 77 struct nls_table *local_nls; 78 struct unicode_map *um; 79 struct hlist_node hlist; 80 struct rw_semaphore session_lock; 81 /* smb session 1 per user */ 82 struct xarray sessions; 83 /* New sessions and SMB3 channels each reserve one connection slot. */ 84 atomic64_t session_count; 85 unsigned long creation_time; 86 unsigned long last_active; 87 /* How many request are running currently */ 88 atomic_t req_running; 89 /* References which are made for this Server object*/ 90 atomic_t r_count; 91 unsigned int total_credits; 92 unsigned int outstanding_credits; 93 spinlock_t credits_lock; 94 /* 95 * Connection command sequence window. [seq_low, seq_high) is the 96 * range of granted sequence numbers (message IDs). seq_bitmap marks 97 * the ones in that range that have been granted but 98 * not yet consumed by a received request. All three are protected by 99 * credits_lock. 100 */ 101 u64 seq_low; 102 u64 seq_high; 103 DECLARE_BITMAP(seq_bitmap, KSMBD_CMD_SEQ_WINDOW); 104 wait_queue_head_t req_running_q; 105 wait_queue_head_t r_count_q; 106 /* Lock to protect requests list*/ 107 spinlock_t request_lock; 108 /* Set under request_lock at teardown start and kept set thereafter. */ 109 bool releasing; 110 struct list_head requests; 111 struct list_head async_requests; 112 int connection_type; 113 struct ksmbd_conn_stats stats; 114 char ClientGUID[SMB2_CLIENT_GUID_SIZE]; 115 struct ntlmssp_auth ntlmssp; 116 117 118 struct preauth_integrity_info *preauth_info; 119 120 bool need_neg; 121 unsigned int auth_mechs; 122 unsigned int preferred_auth_mech; 123 bool sign; 124 bool use_spnego:1; 125 __u16 cli_sec_mode; 126 __u16 srv_sec_mode; 127 /* dialect index that server chose */ 128 __u16 dialect; 129 130 char *mechToken; 131 unsigned int mechTokenLen; 132 133 struct ksmbd_conn_ops *conn_ops; 134 135 /* Preauth Session Table */ 136 struct list_head preauth_sess_table; 137 138 struct sockaddr_storage peer_addr; 139 140 /* Identifier for async message */ 141 struct ida async_ida; 142 143 __le16 cipher_type; 144 __le16 compress_algorithm; 145 /* Negotiated SMB 3.1.1 compression capabilities. */ 146 bool compress_chained; 147 bool compress_pattern; 148 /* Bitmap indexed by SMB2_RDMA_TRANSFORM_* IDs. */ 149 unsigned long rdma_transform_ids; 150 bool rdma_transform_negotiated; 151 bool posix_ext_supported; 152 bool signing_negotiated; 153 __le16 signing_algorithm; 154 bool binding; 155 atomic_t refcnt; 156 bool is_aapl; 157 bool aapl_readdir_attr; /* READDIR_ATTR negotiated */ 158 bool aapl_readdir_attr_v2; /* V2 specifically */ 159 struct work_struct release_work; 160 }; 161 162 struct ksmbd_conn_ops { 163 int (*process_fn)(struct ksmbd_conn *conn); 164 int (*terminate_fn)(struct ksmbd_conn *conn); 165 }; 166 167 struct ksmbd_tx_tracking { 168 /* Required TCP byte count, including transport and socket queues. */ 169 u64 end; 170 unsigned long retry_after; 171 bool valid; 172 }; 173 174 struct ksmbd_transport_write { 175 struct kvec *iov; 176 int iov_cnt; 177 int size; 178 bool need_invalidate_rkey; 179 unsigned int remote_key; 180 int msg_flags; 181 struct ksmbd_tx_tracking *tracking; 182 }; 183 184 struct ksmbd_transport_ops { 185 void (*disconnect)(struct ksmbd_transport *t); 186 void (*shutdown)(struct ksmbd_transport *t); 187 int (*read)(struct ksmbd_transport *t, char *buf, 188 unsigned int size, int max_retries); 189 /* Retained bytes must be privately owned before a full result is returned. */ 190 int (*writev)(struct ksmbd_transport *t, 191 const struct ksmbd_transport_write *tx); 192 /* Optional delivery tracking for nonblocking break sends. */ 193 bool (*is_acked)(struct ksmbd_transport *t, 194 const struct ksmbd_tx_tracking *tracking); 195 int (*rdma_read)(struct ksmbd_transport *t, 196 void *buf, unsigned int len, 197 struct smbdirect_buffer_descriptor_v1 *desc, 198 unsigned int desc_len); 199 int (*rdma_write)(struct ksmbd_transport *t, 200 void *buf, unsigned int len, 201 struct smbdirect_buffer_descriptor_v1 *desc, 202 unsigned int desc_len); 203 void (*free_transport)(struct ksmbd_transport *kt); 204 }; 205 206 struct ksmbd_transport { 207 struct ksmbd_conn *conn; 208 const struct ksmbd_transport_ops *ops; 209 }; 210 211 #define KSMBD_TCP_RECV_TIMEOUT (7 * HZ) 212 #define KSMBD_TCP_SEND_TIMEOUT (5 * HZ) 213 #define KSMBD_SESSION_EXPIRATION_INTERVAL (5 * HZ) 214 #define KSMBD_UNAUTHENTICATED_CONN_TIMEOUT (45 * HZ) 215 #define KSMBD_TCP_PEER_SOCKADDR(c) ((struct sockaddr *)&((c)->peer_addr)) 216 217 #define CONN_HASH_BITS 12 218 extern DECLARE_HASHTABLE(conn_list, CONN_HASH_BITS); 219 extern struct rw_semaphore conn_list_lock; 220 221 bool ksmbd_conn_alive(struct ksmbd_conn *conn); 222 void ksmbd_conn_wait_idle(struct ksmbd_conn *conn); 223 int ksmbd_conn_wait_idle_sess(struct ksmbd_conn *curr_conn, 224 struct ksmbd_session *sess); 225 struct ksmbd_conn *ksmbd_conn_alloc(void); 226 void ksmbd_conn_free(struct ksmbd_conn *conn); 227 struct ksmbd_conn *ksmbd_conn_get(struct ksmbd_conn *conn); 228 void ksmbd_conn_put(struct ksmbd_conn *conn); 229 void ksmbd_conn_abort(struct ksmbd_conn *conn); 230 int ksmbd_conn_wq_init(void); 231 void ksmbd_conn_wq_destroy(void); 232 bool ksmbd_conn_lookup_dialect(struct ksmbd_conn *c); 233 int ksmbd_conn_write(struct ksmbd_work *work); 234 int ksmbd_conn_write_tracked(struct ksmbd_work *work, 235 struct ksmbd_tx_tracking *tracking); 236 int ksmbd_conn_write_eor(struct ksmbd_work *work); 237 int ksmbd_conn_rdma_read(struct ksmbd_conn *conn, 238 void *buf, unsigned int buflen, 239 struct smbdirect_buffer_descriptor_v1 *desc, 240 unsigned int desc_len); 241 int ksmbd_conn_rdma_write(struct ksmbd_conn *conn, 242 void *buf, unsigned int buflen, 243 struct smbdirect_buffer_descriptor_v1 *desc, 244 unsigned int desc_len); 245 void ksmbd_conn_enqueue_request(struct ksmbd_work *work); 246 void ksmbd_conn_try_dequeue_request(struct ksmbd_work *work); 247 void ksmbd_conn_init_server_callbacks(struct ksmbd_conn_ops *ops); 248 int ksmbd_conn_handler_loop(void *p); 249 int ksmbd_conn_transport_init(void); 250 void ksmbd_conn_transport_destroy(void); 251 void ksmbd_conn_lock(struct ksmbd_conn *conn); 252 void ksmbd_conn_unlock(struct ksmbd_conn *conn); 253 void ksmbd_conn_r_count_inc(struct ksmbd_conn *conn); 254 bool ksmbd_conn_r_count_inc_if_good(struct ksmbd_conn *conn); 255 void ksmbd_conn_r_count_dec(struct ksmbd_conn *conn); 256 257 /* 258 * WARNING 259 * 260 * This is a hack. We will move status to a proper place once we land 261 * a multi-sessions support. 262 */ 263 static inline bool ksmbd_conn_new(struct ksmbd_conn *conn) 264 { 265 return READ_ONCE(conn->status) == KSMBD_SESS_NEW; 266 } 267 268 static inline bool ksmbd_conn_good(struct ksmbd_conn *conn) 269 { 270 return !READ_ONCE(conn->releasing) && 271 READ_ONCE(conn->status) == KSMBD_SESS_GOOD; 272 } 273 274 static inline unsigned int 275 ksmbd_max_allowed_pdu_size(struct ksmbd_conn *conn) 276 { 277 if (ksmbd_conn_good(conn)) 278 return SMB3_MAX_MSGSIZE + conn->vals->max_write_size; 279 280 return SMB3_MAX_MSGSIZE; 281 } 282 283 static inline bool ksmbd_conn_need_negotiate(struct ksmbd_conn *conn) 284 { 285 return READ_ONCE(conn->status) == KSMBD_SESS_NEED_NEGOTIATE; 286 } 287 288 static inline bool ksmbd_conn_need_setup(struct ksmbd_conn *conn) 289 { 290 return READ_ONCE(conn->status) == KSMBD_SESS_NEED_SETUP; 291 } 292 293 static inline bool ksmbd_conn_need_reconnect(struct ksmbd_conn *conn) 294 { 295 return READ_ONCE(conn->status) == KSMBD_SESS_NEED_RECONNECT; 296 } 297 298 static inline bool ksmbd_conn_exiting(struct ksmbd_conn *conn) 299 { 300 return READ_ONCE(conn->status) == KSMBD_SESS_EXITING; 301 } 302 303 static inline bool ksmbd_conn_releasing(struct ksmbd_conn *conn) 304 { 305 return READ_ONCE(conn->releasing) || 306 READ_ONCE(conn->status) == KSMBD_SESS_RELEASING; 307 } 308 309 static inline void ksmbd_conn_set_new(struct ksmbd_conn *conn) 310 { 311 WRITE_ONCE(conn->status, KSMBD_SESS_NEW); 312 } 313 314 static inline void ksmbd_conn_set_good(struct ksmbd_conn *conn) 315 { 316 WRITE_ONCE(conn->status, KSMBD_SESS_GOOD); 317 } 318 319 static inline void ksmbd_conn_set_need_negotiate(struct ksmbd_conn *conn) 320 { 321 WRITE_ONCE(conn->status, KSMBD_SESS_NEED_NEGOTIATE); 322 } 323 324 static inline void ksmbd_conn_set_need_setup(struct ksmbd_conn *conn) 325 { 326 WRITE_ONCE(conn->status, KSMBD_SESS_NEED_SETUP); 327 } 328 329 static inline void ksmbd_conn_set_need_reconnect(struct ksmbd_conn *conn) 330 { 331 WRITE_ONCE(conn->status, KSMBD_SESS_NEED_RECONNECT); 332 } 333 334 static inline void ksmbd_conn_set_exiting(struct ksmbd_conn *conn) 335 { 336 WRITE_ONCE(conn->status, KSMBD_SESS_EXITING); 337 } 338 339 static inline void ksmbd_conn_set_releasing(struct ksmbd_conn *conn) 340 { 341 WRITE_ONCE(conn->releasing, true); 342 WRITE_ONCE(conn->status, KSMBD_SESS_RELEASING); 343 } 344 345 void ksmbd_all_conn_set_status(struct ksmbd_session *sess, u32 status); 346 #endif /* __CONNECTION_H__ */ 347