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