xref: /linux/fs/smb/server/connection.h (revision ec0611868f2fcf29e4c2bebdc6702d3e1f272fec)
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 	bool				stop_called;
53 	union {
54 		__be32			inet_addr;
55 #if IS_ENABLED(CONFIG_IPV6)
56 		u8			inet6_addr[16];
57 #endif
58 	};
59 	unsigned int			inet_hash;
60 	char				*request_buf;
61 	struct ksmbd_transport		*transport;
62 	struct nls_table		*local_nls;
63 	struct unicode_map		*um;
64 	struct hlist_node		hlist;
65 	struct rw_semaphore		session_lock;
66 	/* smb session 1 per user */
67 	struct xarray			sessions;
68 	unsigned long			last_active;
69 	/* How many request are running currently */
70 	atomic_t			req_running;
71 	/* References which are made for this Server object*/
72 	atomic_t			r_count;
73 	unsigned int			total_credits;
74 	unsigned int			outstanding_credits;
75 	spinlock_t			credits_lock;
76 	wait_queue_head_t		req_running_q;
77 	wait_queue_head_t		r_count_q;
78 	/* Lock to protect requests list*/
79 	spinlock_t			request_lock;
80 	struct list_head		requests;
81 	struct list_head		async_requests;
82 	int				connection_type;
83 	struct ksmbd_conn_stats		stats;
84 	char				ClientGUID[SMB2_CLIENT_GUID_SIZE];
85 	struct ntlmssp_auth		ntlmssp;
86 
87 	spinlock_t			llist_lock;
88 	struct list_head		lock_list;
89 
90 	struct preauth_integrity_info	*preauth_info;
91 
92 	bool				need_neg;
93 	unsigned int			auth_mechs;
94 	unsigned int			preferred_auth_mech;
95 	bool				sign;
96 	bool				use_spnego:1;
97 	__u16				cli_sec_mode;
98 	__u16				srv_sec_mode;
99 	/* dialect index that server chose */
100 	__u16				dialect;
101 
102 	char				*mechToken;
103 	unsigned int			mechTokenLen;
104 
105 	struct ksmbd_conn_ops	*conn_ops;
106 
107 	/* Preauth Session Table */
108 	struct list_head		preauth_sess_table;
109 
110 	struct sockaddr_storage		peer_addr;
111 
112 	/* Identifier for async message */
113 	struct ida			async_ida;
114 
115 	__le16				cipher_type;
116 	__le16				compress_algorithm;
117 	bool				posix_ext_supported;
118 	bool				signing_negotiated;
119 	__le16				signing_algorithm;
120 	bool				binding;
121 	atomic_t			refcnt;
122 	bool				is_aapl;
123 };
124 
125 struct ksmbd_conn_ops {
126 	int	(*process_fn)(struct ksmbd_conn *conn);
127 	int	(*terminate_fn)(struct ksmbd_conn *conn);
128 };
129 
130 struct ksmbd_transport_ops {
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