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 }; 137 138 struct ksmbd_transport { 139 struct ksmbd_conn *conn; 140 const struct ksmbd_transport_ops *ops; 141 }; 142 143 #define KSMBD_TCP_RECV_TIMEOUT (7 * HZ) 144 #define KSMBD_TCP_SEND_TIMEOUT (5 * HZ) 145 #define KSMBD_TCP_PEER_SOCKADDR(c) ((struct sockaddr *)&((c)->peer_addr)) 146 147 extern struct list_head conn_list; 148 extern struct rw_semaphore conn_list_lock; 149 150 bool ksmbd_conn_alive(struct ksmbd_conn *conn); 151 void ksmbd_conn_wait_idle(struct ksmbd_conn *conn); 152 int ksmbd_conn_wait_idle_sess_id(struct ksmbd_conn *curr_conn, u64 sess_id); 153 struct ksmbd_conn *ksmbd_conn_alloc(void); 154 void ksmbd_conn_free(struct ksmbd_conn *conn); 155 bool ksmbd_conn_lookup_dialect(struct ksmbd_conn *c); 156 int ksmbd_conn_write(struct ksmbd_work *work); 157 int ksmbd_conn_rdma_read(struct ksmbd_conn *conn, 158 void *buf, unsigned int buflen, 159 struct smb2_buffer_desc_v1 *desc, 160 unsigned int desc_len); 161 int ksmbd_conn_rdma_write(struct ksmbd_conn *conn, 162 void *buf, unsigned int buflen, 163 struct smb2_buffer_desc_v1 *desc, 164 unsigned int desc_len); 165 void ksmbd_conn_enqueue_request(struct ksmbd_work *work); 166 void ksmbd_conn_try_dequeue_request(struct ksmbd_work *work); 167 void ksmbd_conn_init_server_callbacks(struct ksmbd_conn_ops *ops); 168 int ksmbd_conn_handler_loop(void *p); 169 int ksmbd_conn_transport_init(void); 170 void ksmbd_conn_transport_destroy(void); 171 void ksmbd_conn_lock(struct ksmbd_conn *conn); 172 void ksmbd_conn_unlock(struct ksmbd_conn *conn); 173 void ksmbd_conn_r_count_inc(struct ksmbd_conn *conn); 174 void ksmbd_conn_r_count_dec(struct ksmbd_conn *conn); 175 176 /* 177 * WARNING 178 * 179 * This is a hack. We will move status to a proper place once we land 180 * a multi-sessions support. 181 */ 182 static inline bool ksmbd_conn_good(struct ksmbd_conn *conn) 183 { 184 return READ_ONCE(conn->status) == KSMBD_SESS_GOOD; 185 } 186 187 static inline bool ksmbd_conn_need_negotiate(struct ksmbd_conn *conn) 188 { 189 return READ_ONCE(conn->status) == KSMBD_SESS_NEED_NEGOTIATE; 190 } 191 192 static inline bool ksmbd_conn_need_setup(struct ksmbd_conn *conn) 193 { 194 return READ_ONCE(conn->status) == KSMBD_SESS_NEED_SETUP; 195 } 196 197 static inline bool ksmbd_conn_need_reconnect(struct ksmbd_conn *conn) 198 { 199 return READ_ONCE(conn->status) == KSMBD_SESS_NEED_RECONNECT; 200 } 201 202 static inline bool ksmbd_conn_exiting(struct ksmbd_conn *conn) 203 { 204 return READ_ONCE(conn->status) == KSMBD_SESS_EXITING; 205 } 206 207 static inline bool ksmbd_conn_releasing(struct ksmbd_conn *conn) 208 { 209 return READ_ONCE(conn->status) == KSMBD_SESS_RELEASING; 210 } 211 212 static inline void ksmbd_conn_set_new(struct ksmbd_conn *conn) 213 { 214 WRITE_ONCE(conn->status, KSMBD_SESS_NEW); 215 } 216 217 static inline void ksmbd_conn_set_good(struct ksmbd_conn *conn) 218 { 219 WRITE_ONCE(conn->status, KSMBD_SESS_GOOD); 220 } 221 222 static inline void ksmbd_conn_set_need_negotiate(struct ksmbd_conn *conn) 223 { 224 WRITE_ONCE(conn->status, KSMBD_SESS_NEED_NEGOTIATE); 225 } 226 227 static inline void ksmbd_conn_set_need_setup(struct ksmbd_conn *conn) 228 { 229 WRITE_ONCE(conn->status, KSMBD_SESS_NEED_SETUP); 230 } 231 232 static inline void ksmbd_conn_set_need_reconnect(struct ksmbd_conn *conn) 233 { 234 WRITE_ONCE(conn->status, KSMBD_SESS_NEED_RECONNECT); 235 } 236 237 static inline void ksmbd_conn_set_exiting(struct ksmbd_conn *conn) 238 { 239 WRITE_ONCE(conn->status, KSMBD_SESS_EXITING); 240 } 241 242 static inline void ksmbd_conn_set_releasing(struct ksmbd_conn *conn) 243 { 244 WRITE_ONCE(conn->status, KSMBD_SESS_RELEASING); 245 } 246 247 void ksmbd_all_conn_set_status(u64 sess_id, u32 status); 248 #endif /* __CONNECTION_H__ */ 249