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