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