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