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