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 unsigned int inet_hash; 72 char *request_buf; 73 struct ksmbd_transport *transport; 74 struct nls_table *local_nls; 75 struct unicode_map *um; 76 struct hlist_node hlist; 77 struct rw_semaphore session_lock; 78 /* smb session 1 per user */ 79 struct xarray sessions; 80 unsigned long creation_time; 81 unsigned long last_active; 82 /* How many request are running currently */ 83 atomic_t req_running; 84 /* References which are made for this Server object*/ 85 atomic_t r_count; 86 unsigned int total_credits; 87 unsigned int outstanding_credits; 88 spinlock_t credits_lock; 89 /* 90 * Connection command sequence window. [seq_low, seq_high) is the 91 * range of granted sequence numbers (message IDs). seq_bitmap marks 92 * the ones in that range that have been granted but 93 * not yet consumed by a received request. All three are protected by 94 * credits_lock. 95 */ 96 u64 seq_low; 97 u64 seq_high; 98 DECLARE_BITMAP(seq_bitmap, KSMBD_CMD_SEQ_WINDOW); 99 wait_queue_head_t req_running_q; 100 wait_queue_head_t r_count_q; 101 /* Lock to protect requests list*/ 102 spinlock_t request_lock; 103 struct list_head requests; 104 struct list_head async_requests; 105 int connection_type; 106 struct ksmbd_conn_stats stats; 107 char ClientGUID[SMB2_CLIENT_GUID_SIZE]; 108 struct ntlmssp_auth ntlmssp; 109 110 spinlock_t llist_lock; 111 struct list_head lock_list; 112 113 struct preauth_integrity_info *preauth_info; 114 115 bool need_neg; 116 unsigned int auth_mechs; 117 unsigned int preferred_auth_mech; 118 bool sign; 119 bool use_spnego:1; 120 __u16 cli_sec_mode; 121 __u16 srv_sec_mode; 122 /* dialect index that server chose */ 123 __u16 dialect; 124 125 char *mechToken; 126 unsigned int mechTokenLen; 127 128 struct ksmbd_conn_ops *conn_ops; 129 130 /* Preauth Session Table */ 131 struct list_head preauth_sess_table; 132 133 struct sockaddr_storage peer_addr; 134 135 /* Identifier for async message */ 136 struct ida async_ida; 137 138 __le16 cipher_type; 139 __le16 compress_algorithm; 140 /* Negotiated SMB 3.1.1 compression capabilities. */ 141 bool compress_chained; 142 bool compress_pattern; 143 /* Bitmap indexed by SMB2_RDMA_TRANSFORM_* IDs. */ 144 unsigned long rdma_transform_ids; 145 bool rdma_transform_negotiated; 146 bool posix_ext_supported; 147 bool signing_negotiated; 148 __le16 signing_algorithm; 149 bool binding; 150 atomic_t refcnt; 151 bool is_aapl; 152 bool aapl_readdir_attr; /* READDIR_ATTR negotiated */ 153 bool aapl_readdir_attr_v2; /* V2 specifically */ 154 struct work_struct release_work; 155 }; 156 157 struct ksmbd_conn_ops { 158 int (*process_fn)(struct ksmbd_conn *conn); 159 int (*terminate_fn)(struct ksmbd_conn *conn); 160 }; 161 162 struct ksmbd_transport_write { 163 struct kvec *iov; 164 int iov_cnt; 165 int size; 166 bool need_invalidate_rkey; 167 unsigned int remote_key; 168 int msg_flags; 169 }; 170 171 struct ksmbd_transport_ops { 172 void (*disconnect)(struct ksmbd_transport *t); 173 void (*shutdown)(struct ksmbd_transport *t); 174 int (*read)(struct ksmbd_transport *t, char *buf, 175 unsigned int size, int max_retries); 176 int (*writev)(struct ksmbd_transport *t, 177 const struct ksmbd_transport_write *tx); 178 int (*rdma_read)(struct ksmbd_transport *t, 179 void *buf, unsigned int len, 180 struct smbdirect_buffer_descriptor_v1 *desc, 181 unsigned int desc_len); 182 int (*rdma_write)(struct ksmbd_transport *t, 183 void *buf, unsigned int len, 184 struct smbdirect_buffer_descriptor_v1 *desc, 185 unsigned int desc_len); 186 void (*free_transport)(struct ksmbd_transport *kt); 187 }; 188 189 struct ksmbd_transport { 190 struct ksmbd_conn *conn; 191 const struct ksmbd_transport_ops *ops; 192 }; 193 194 #define KSMBD_TCP_RECV_TIMEOUT (7 * HZ) 195 #define KSMBD_TCP_SEND_TIMEOUT (5 * HZ) 196 #define KSMBD_SESSION_EXPIRATION_INTERVAL (5 * HZ) 197 #define KSMBD_UNAUTHENTICATED_CONN_TIMEOUT (45 * HZ) 198 #define KSMBD_TCP_PEER_SOCKADDR(c) ((struct sockaddr *)&((c)->peer_addr)) 199 200 #define CONN_HASH_BITS 12 201 extern DECLARE_HASHTABLE(conn_list, CONN_HASH_BITS); 202 extern struct rw_semaphore conn_list_lock; 203 204 bool ksmbd_conn_alive(struct ksmbd_conn *conn); 205 void ksmbd_conn_wait_idle(struct ksmbd_conn *conn); 206 int ksmbd_conn_wait_idle_sess(struct ksmbd_conn *curr_conn, 207 struct ksmbd_session *sess); 208 struct ksmbd_conn *ksmbd_conn_alloc(void); 209 void ksmbd_conn_free(struct ksmbd_conn *conn); 210 struct ksmbd_conn *ksmbd_conn_get(struct ksmbd_conn *conn); 211 void ksmbd_conn_put(struct ksmbd_conn *conn); 212 void ksmbd_conn_abort(struct ksmbd_conn *conn); 213 int ksmbd_conn_wq_init(void); 214 void ksmbd_conn_wq_destroy(void); 215 bool ksmbd_conn_lookup_dialect(struct ksmbd_conn *c); 216 int ksmbd_conn_write(struct ksmbd_work *work); 217 int ksmbd_conn_write_eor(struct ksmbd_work *work); 218 int ksmbd_conn_rdma_read(struct ksmbd_conn *conn, 219 void *buf, unsigned int buflen, 220 struct smbdirect_buffer_descriptor_v1 *desc, 221 unsigned int desc_len); 222 int ksmbd_conn_rdma_write(struct ksmbd_conn *conn, 223 void *buf, unsigned int buflen, 224 struct smbdirect_buffer_descriptor_v1 *desc, 225 unsigned int desc_len); 226 void ksmbd_conn_enqueue_request(struct ksmbd_work *work); 227 void ksmbd_conn_try_dequeue_request(struct ksmbd_work *work); 228 void ksmbd_conn_init_server_callbacks(struct ksmbd_conn_ops *ops); 229 int ksmbd_conn_handler_loop(void *p); 230 int ksmbd_conn_transport_init(void); 231 void ksmbd_conn_transport_destroy(void); 232 void ksmbd_conn_lock(struct ksmbd_conn *conn); 233 void ksmbd_conn_unlock(struct ksmbd_conn *conn); 234 void ksmbd_conn_r_count_inc(struct ksmbd_conn *conn); 235 void ksmbd_conn_r_count_dec(struct ksmbd_conn *conn); 236 237 /* 238 * WARNING 239 * 240 * This is a hack. We will move status to a proper place once we land 241 * a multi-sessions support. 242 */ 243 static inline bool ksmbd_conn_new(struct ksmbd_conn *conn) 244 { 245 return READ_ONCE(conn->status) == KSMBD_SESS_NEW; 246 } 247 248 static inline bool ksmbd_conn_good(struct ksmbd_conn *conn) 249 { 250 return READ_ONCE(conn->status) == KSMBD_SESS_GOOD; 251 } 252 253 static inline unsigned int 254 ksmbd_max_allowed_pdu_size(struct ksmbd_conn *conn) 255 { 256 if (ksmbd_conn_good(conn)) 257 return SMB3_MAX_MSGSIZE + conn->vals->max_write_size; 258 259 return SMB3_MAX_MSGSIZE; 260 } 261 262 static inline bool ksmbd_conn_need_negotiate(struct ksmbd_conn *conn) 263 { 264 return READ_ONCE(conn->status) == KSMBD_SESS_NEED_NEGOTIATE; 265 } 266 267 static inline bool ksmbd_conn_need_setup(struct ksmbd_conn *conn) 268 { 269 return READ_ONCE(conn->status) == KSMBD_SESS_NEED_SETUP; 270 } 271 272 static inline bool ksmbd_conn_need_reconnect(struct ksmbd_conn *conn) 273 { 274 return READ_ONCE(conn->status) == KSMBD_SESS_NEED_RECONNECT; 275 } 276 277 static inline bool ksmbd_conn_exiting(struct ksmbd_conn *conn) 278 { 279 return READ_ONCE(conn->status) == KSMBD_SESS_EXITING; 280 } 281 282 static inline bool ksmbd_conn_releasing(struct ksmbd_conn *conn) 283 { 284 return READ_ONCE(conn->status) == KSMBD_SESS_RELEASING; 285 } 286 287 static inline void ksmbd_conn_set_new(struct ksmbd_conn *conn) 288 { 289 WRITE_ONCE(conn->status, KSMBD_SESS_NEW); 290 } 291 292 static inline void ksmbd_conn_set_good(struct ksmbd_conn *conn) 293 { 294 WRITE_ONCE(conn->status, KSMBD_SESS_GOOD); 295 } 296 297 static inline void ksmbd_conn_set_need_negotiate(struct ksmbd_conn *conn) 298 { 299 WRITE_ONCE(conn->status, KSMBD_SESS_NEED_NEGOTIATE); 300 } 301 302 static inline void ksmbd_conn_set_need_setup(struct ksmbd_conn *conn) 303 { 304 WRITE_ONCE(conn->status, KSMBD_SESS_NEED_SETUP); 305 } 306 307 static inline void ksmbd_conn_set_need_reconnect(struct ksmbd_conn *conn) 308 { 309 WRITE_ONCE(conn->status, KSMBD_SESS_NEED_RECONNECT); 310 } 311 312 static inline void ksmbd_conn_set_exiting(struct ksmbd_conn *conn) 313 { 314 WRITE_ONCE(conn->status, KSMBD_SESS_EXITING); 315 } 316 317 static inline void ksmbd_conn_set_releasing(struct ksmbd_conn *conn) 318 { 319 WRITE_ONCE(conn->status, KSMBD_SESS_RELEASING); 320 } 321 322 void ksmbd_all_conn_set_status(struct ksmbd_session *sess, u32 status); 323 #endif /* __CONNECTION_H__ */ 324