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