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