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