xref: /linux/fs/smb/server/connection.c (revision 466b2d40f60ce874cb9b56dc88bd1a0880a43786)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (C) 2016 Namjae Jeon <namjae.jeon@protocolfreedom.org>
4  *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
5  */
6 
7 #include <linux/mutex.h>
8 #include <linux/freezer.h>
9 #include <linux/module.h>
10 
11 #include "server.h"
12 #include "smb_common.h"
13 #include "mgmt/ksmbd_ida.h"
14 #include "connection.h"
15 #include "transport_tcp.h"
16 #include "transport_rdma.h"
17 
18 static DEFINE_MUTEX(init_lock);
19 
20 static struct ksmbd_conn_ops default_conn_ops;
21 
22 LIST_HEAD(conn_list);
23 DECLARE_RWSEM(conn_list_lock);
24 
25 /**
26  * ksmbd_conn_free() - free resources of the connection instance
27  *
28  * @conn:	connection instance to be cleaned up
29  *
30  * During the thread termination, the corresponding conn instance
31  * resources(sock/memory) are released and finally the conn object is freed.
32  */
ksmbd_conn_free(struct ksmbd_conn * conn)33 void ksmbd_conn_free(struct ksmbd_conn *conn)
34 {
35 	down_write(&conn_list_lock);
36 	list_del(&conn->conns_list);
37 	up_write(&conn_list_lock);
38 
39 	xa_destroy(&conn->sessions);
40 	kvfree(conn->request_buf);
41 	kfree(conn->preauth_info);
42 	if (atomic_dec_and_test(&conn->refcnt))
43 		kfree(conn);
44 }
45 
46 /**
47  * ksmbd_conn_alloc() - initialize a new connection instance
48  *
49  * Return:	ksmbd_conn struct on success, otherwise NULL
50  */
ksmbd_conn_alloc(void)51 struct ksmbd_conn *ksmbd_conn_alloc(void)
52 {
53 	struct ksmbd_conn *conn;
54 
55 	conn = kzalloc(sizeof(struct ksmbd_conn), KSMBD_DEFAULT_GFP);
56 	if (!conn)
57 		return NULL;
58 
59 	conn->need_neg = true;
60 	ksmbd_conn_set_new(conn);
61 	conn->local_nls = load_nls("utf8");
62 	if (!conn->local_nls)
63 		conn->local_nls = load_nls_default();
64 	if (IS_ENABLED(CONFIG_UNICODE))
65 		conn->um = utf8_load(UNICODE_AGE(12, 1, 0));
66 	else
67 		conn->um = ERR_PTR(-EOPNOTSUPP);
68 	if (IS_ERR(conn->um))
69 		conn->um = NULL;
70 	atomic_set(&conn->req_running, 0);
71 	atomic_set(&conn->r_count, 0);
72 	atomic_set(&conn->refcnt, 1);
73 	conn->total_credits = 1;
74 	conn->outstanding_credits = 0;
75 
76 	init_waitqueue_head(&conn->req_running_q);
77 	init_waitqueue_head(&conn->r_count_q);
78 	INIT_LIST_HEAD(&conn->conns_list);
79 	INIT_LIST_HEAD(&conn->requests);
80 	INIT_LIST_HEAD(&conn->async_requests);
81 	spin_lock_init(&conn->request_lock);
82 	spin_lock_init(&conn->credits_lock);
83 	ida_init(&conn->async_ida);
84 	xa_init(&conn->sessions);
85 
86 	spin_lock_init(&conn->llist_lock);
87 	INIT_LIST_HEAD(&conn->lock_list);
88 
89 	init_rwsem(&conn->session_lock);
90 
91 	down_write(&conn_list_lock);
92 	list_add(&conn->conns_list, &conn_list);
93 	up_write(&conn_list_lock);
94 	return conn;
95 }
96 
ksmbd_conn_lookup_dialect(struct ksmbd_conn * c)97 bool ksmbd_conn_lookup_dialect(struct ksmbd_conn *c)
98 {
99 	struct ksmbd_conn *t;
100 	bool ret = false;
101 
102 	down_read(&conn_list_lock);
103 	list_for_each_entry(t, &conn_list, conns_list) {
104 		if (memcmp(t->ClientGUID, c->ClientGUID, SMB2_CLIENT_GUID_SIZE))
105 			continue;
106 
107 		ret = true;
108 		break;
109 	}
110 	up_read(&conn_list_lock);
111 	return ret;
112 }
113 
ksmbd_conn_enqueue_request(struct ksmbd_work * work)114 void ksmbd_conn_enqueue_request(struct ksmbd_work *work)
115 {
116 	struct ksmbd_conn *conn = work->conn;
117 	struct list_head *requests_queue = NULL;
118 
119 	if (conn->ops->get_cmd_val(work) != SMB2_CANCEL_HE)
120 		requests_queue = &conn->requests;
121 
122 	atomic_inc(&conn->req_running);
123 	if (requests_queue) {
124 		spin_lock(&conn->request_lock);
125 		list_add_tail(&work->request_entry, requests_queue);
126 		spin_unlock(&conn->request_lock);
127 	}
128 }
129 
ksmbd_conn_try_dequeue_request(struct ksmbd_work * work)130 void ksmbd_conn_try_dequeue_request(struct ksmbd_work *work)
131 {
132 	struct ksmbd_conn *conn = work->conn;
133 
134 	atomic_dec(&conn->req_running);
135 	if (waitqueue_active(&conn->req_running_q))
136 		wake_up(&conn->req_running_q);
137 
138 	if (list_empty(&work->request_entry) &&
139 	    list_empty(&work->async_request_entry))
140 		return;
141 
142 	spin_lock(&conn->request_lock);
143 	list_del_init(&work->request_entry);
144 	spin_unlock(&conn->request_lock);
145 	if (work->asynchronous)
146 		release_async_work(work);
147 
148 	wake_up_all(&conn->req_running_q);
149 }
150 
ksmbd_conn_lock(struct ksmbd_conn * conn)151 void ksmbd_conn_lock(struct ksmbd_conn *conn)
152 {
153 	mutex_lock(&conn->srv_mutex);
154 }
155 
ksmbd_conn_unlock(struct ksmbd_conn * conn)156 void ksmbd_conn_unlock(struct ksmbd_conn *conn)
157 {
158 	mutex_unlock(&conn->srv_mutex);
159 }
160 
ksmbd_all_conn_set_status(u64 sess_id,u32 status)161 void ksmbd_all_conn_set_status(u64 sess_id, u32 status)
162 {
163 	struct ksmbd_conn *conn;
164 
165 	down_read(&conn_list_lock);
166 	list_for_each_entry(conn, &conn_list, conns_list) {
167 		if (conn->binding || xa_load(&conn->sessions, sess_id))
168 			WRITE_ONCE(conn->status, status);
169 	}
170 	up_read(&conn_list_lock);
171 }
172 
ksmbd_conn_wait_idle(struct ksmbd_conn * conn)173 void ksmbd_conn_wait_idle(struct ksmbd_conn *conn)
174 {
175 	wait_event(conn->req_running_q, atomic_read(&conn->req_running) < 2);
176 }
177 
ksmbd_conn_wait_idle_sess_id(struct ksmbd_conn * curr_conn,u64 sess_id)178 int ksmbd_conn_wait_idle_sess_id(struct ksmbd_conn *curr_conn, u64 sess_id)
179 {
180 	struct ksmbd_conn *conn;
181 	int rc, retry_count = 0, max_timeout = 120;
182 	int rcount = 1;
183 
184 retry_idle:
185 	if (retry_count >= max_timeout)
186 		return -EIO;
187 
188 	down_read(&conn_list_lock);
189 	list_for_each_entry(conn, &conn_list, conns_list) {
190 		if (conn->binding || xa_load(&conn->sessions, sess_id)) {
191 			if (conn == curr_conn)
192 				rcount = 2;
193 			if (atomic_read(&conn->req_running) >= rcount) {
194 				rc = wait_event_timeout(conn->req_running_q,
195 					atomic_read(&conn->req_running) < rcount,
196 					HZ);
197 				if (!rc) {
198 					up_read(&conn_list_lock);
199 					retry_count++;
200 					goto retry_idle;
201 				}
202 			}
203 		}
204 	}
205 	up_read(&conn_list_lock);
206 
207 	return 0;
208 }
209 
ksmbd_conn_write(struct ksmbd_work * work)210 int ksmbd_conn_write(struct ksmbd_work *work)
211 {
212 	struct ksmbd_conn *conn = work->conn;
213 	int sent;
214 
215 	if (!work->response_buf) {
216 		pr_err("NULL response header\n");
217 		return -EINVAL;
218 	}
219 
220 	if (work->send_no_response)
221 		return 0;
222 
223 	if (!work->iov_idx)
224 		return -EINVAL;
225 
226 	ksmbd_conn_lock(conn);
227 	sent = conn->transport->ops->writev(conn->transport, work->iov,
228 			work->iov_cnt,
229 			get_rfc1002_len(work->iov[0].iov_base) + 4,
230 			work->need_invalidate_rkey,
231 			work->remote_key);
232 	ksmbd_conn_unlock(conn);
233 
234 	if (sent < 0) {
235 		pr_err("Failed to send message: %d\n", sent);
236 		return sent;
237 	}
238 
239 	return 0;
240 }
241 
ksmbd_conn_rdma_read(struct ksmbd_conn * conn,void * buf,unsigned int buflen,struct smb2_buffer_desc_v1 * desc,unsigned int desc_len)242 int ksmbd_conn_rdma_read(struct ksmbd_conn *conn,
243 			 void *buf, unsigned int buflen,
244 			 struct smb2_buffer_desc_v1 *desc,
245 			 unsigned int desc_len)
246 {
247 	int ret = -EINVAL;
248 
249 	if (conn->transport->ops->rdma_read)
250 		ret = conn->transport->ops->rdma_read(conn->transport,
251 						      buf, buflen,
252 						      desc, desc_len);
253 	return ret;
254 }
255 
ksmbd_conn_rdma_write(struct ksmbd_conn * conn,void * buf,unsigned int buflen,struct smb2_buffer_desc_v1 * desc,unsigned int desc_len)256 int ksmbd_conn_rdma_write(struct ksmbd_conn *conn,
257 			  void *buf, unsigned int buflen,
258 			  struct smb2_buffer_desc_v1 *desc,
259 			  unsigned int desc_len)
260 {
261 	int ret = -EINVAL;
262 
263 	if (conn->transport->ops->rdma_write)
264 		ret = conn->transport->ops->rdma_write(conn->transport,
265 						       buf, buflen,
266 						       desc, desc_len);
267 	return ret;
268 }
269 
ksmbd_conn_alive(struct ksmbd_conn * conn)270 bool ksmbd_conn_alive(struct ksmbd_conn *conn)
271 {
272 	if (!ksmbd_server_running())
273 		return false;
274 
275 	if (ksmbd_conn_exiting(conn))
276 		return false;
277 
278 	if (kthread_should_stop())
279 		return false;
280 
281 	if (atomic_read(&conn->stats.open_files_count) > 0)
282 		return true;
283 
284 	/*
285 	 * Stop current session if the time that get last request from client
286 	 * is bigger than deadtime user configured and opening file count is
287 	 * zero.
288 	 */
289 	if (server_conf.deadtime > 0 &&
290 	    time_after(jiffies, conn->last_active + server_conf.deadtime)) {
291 		ksmbd_debug(CONN, "No response from client in %lu minutes\n",
292 			    server_conf.deadtime / SMB_ECHO_INTERVAL);
293 		return false;
294 	}
295 	return true;
296 }
297 
298 #define SMB1_MIN_SUPPORTED_HEADER_SIZE (sizeof(struct smb_hdr))
299 #define SMB2_MIN_SUPPORTED_HEADER_SIZE (sizeof(struct smb2_hdr) + 4)
300 
301 /**
302  * ksmbd_conn_handler_loop() - session thread to listen on new smb requests
303  * @p:		connection instance
304  *
305  * One thread each per connection
306  *
307  * Return:	0 on success
308  */
ksmbd_conn_handler_loop(void * p)309 int ksmbd_conn_handler_loop(void *p)
310 {
311 	struct ksmbd_conn *conn = (struct ksmbd_conn *)p;
312 	struct ksmbd_transport *t = conn->transport;
313 	unsigned int pdu_size, max_allowed_pdu_size, max_req;
314 	char hdr_buf[4] = {0,};
315 	int size;
316 
317 	mutex_init(&conn->srv_mutex);
318 	__module_get(THIS_MODULE);
319 
320 	if (t->ops->prepare && t->ops->prepare(t))
321 		goto out;
322 
323 	max_req = server_conf.max_inflight_req;
324 	conn->last_active = jiffies;
325 	set_freezable();
326 	while (ksmbd_conn_alive(conn)) {
327 		if (try_to_freeze())
328 			continue;
329 
330 		kvfree(conn->request_buf);
331 		conn->request_buf = NULL;
332 
333 recheck:
334 		if (atomic_read(&conn->req_running) + 1 > max_req) {
335 			wait_event_interruptible(conn->req_running_q,
336 				atomic_read(&conn->req_running) < max_req);
337 			goto recheck;
338 		}
339 
340 		size = t->ops->read(t, hdr_buf, sizeof(hdr_buf), -1);
341 		if (size != sizeof(hdr_buf))
342 			break;
343 
344 		pdu_size = get_rfc1002_len(hdr_buf);
345 		ksmbd_debug(CONN, "RFC1002 header %u bytes\n", pdu_size);
346 
347 		if (ksmbd_conn_good(conn))
348 			max_allowed_pdu_size =
349 				SMB3_MAX_MSGSIZE + conn->vals->max_write_size;
350 		else
351 			max_allowed_pdu_size = SMB3_MAX_MSGSIZE;
352 
353 		if (pdu_size > max_allowed_pdu_size) {
354 			pr_err_ratelimited("PDU length(%u) exceeded maximum allowed pdu size(%u) on connection(%d)\n",
355 					pdu_size, max_allowed_pdu_size,
356 					READ_ONCE(conn->status));
357 			break;
358 		}
359 
360 		/*
361 		 * Check maximum pdu size(0x00FFFFFF).
362 		 */
363 		if (pdu_size > MAX_STREAM_PROT_LEN)
364 			break;
365 
366 		if (pdu_size < SMB1_MIN_SUPPORTED_HEADER_SIZE)
367 			break;
368 
369 		/* 4 for rfc1002 length field */
370 		/* 1 for implied bcc[0] */
371 		size = pdu_size + 4 + 1;
372 		conn->request_buf = kvmalloc(size, KSMBD_DEFAULT_GFP);
373 		if (!conn->request_buf)
374 			break;
375 
376 		memcpy(conn->request_buf, hdr_buf, sizeof(hdr_buf));
377 
378 		/*
379 		 * We already read 4 bytes to find out PDU size, now
380 		 * read in PDU
381 		 */
382 		size = t->ops->read(t, conn->request_buf + 4, pdu_size, 2);
383 		if (size < 0) {
384 			pr_err("sock_read failed: %d\n", size);
385 			break;
386 		}
387 
388 		if (size != pdu_size) {
389 			pr_err("PDU error. Read: %d, Expected: %d\n",
390 			       size, pdu_size);
391 			continue;
392 		}
393 
394 		if (!ksmbd_smb_request(conn))
395 			break;
396 
397 		if (((struct smb2_hdr *)smb2_get_msg(conn->request_buf))->ProtocolId ==
398 		    SMB2_PROTO_NUMBER) {
399 			if (pdu_size < SMB2_MIN_SUPPORTED_HEADER_SIZE)
400 				break;
401 		}
402 
403 		if (!default_conn_ops.process_fn) {
404 			pr_err("No connection request callback\n");
405 			break;
406 		}
407 
408 		if (default_conn_ops.process_fn(conn)) {
409 			pr_err("Cannot handle request\n");
410 			break;
411 		}
412 	}
413 
414 out:
415 	ksmbd_conn_set_releasing(conn);
416 	/* Wait till all reference dropped to the Server object*/
417 	ksmbd_debug(CONN, "Wait for all pending requests(%d)\n", atomic_read(&conn->r_count));
418 	wait_event(conn->r_count_q, atomic_read(&conn->r_count) == 0);
419 
420 	if (IS_ENABLED(CONFIG_UNICODE))
421 		utf8_unload(conn->um);
422 	unload_nls(conn->local_nls);
423 	if (default_conn_ops.terminate_fn)
424 		default_conn_ops.terminate_fn(conn);
425 	t->ops->disconnect(t);
426 	module_put(THIS_MODULE);
427 	return 0;
428 }
429 
ksmbd_conn_init_server_callbacks(struct ksmbd_conn_ops * ops)430 void ksmbd_conn_init_server_callbacks(struct ksmbd_conn_ops *ops)
431 {
432 	default_conn_ops.process_fn = ops->process_fn;
433 	default_conn_ops.terminate_fn = ops->terminate_fn;
434 }
435 
ksmbd_conn_transport_init(void)436 int ksmbd_conn_transport_init(void)
437 {
438 	int ret;
439 
440 	mutex_lock(&init_lock);
441 	ret = ksmbd_tcp_init();
442 	if (ret) {
443 		pr_err("Failed to init TCP subsystem: %d\n", ret);
444 		goto out;
445 	}
446 
447 	ret = ksmbd_rdma_init();
448 	if (ret) {
449 		pr_err("Failed to init RDMA subsystem: %d\n", ret);
450 		goto out;
451 	}
452 out:
453 	mutex_unlock(&init_lock);
454 	return ret;
455 }
456 
stop_sessions(void)457 static void stop_sessions(void)
458 {
459 	struct ksmbd_conn *conn;
460 	struct ksmbd_transport *t;
461 
462 again:
463 	down_read(&conn_list_lock);
464 	list_for_each_entry(conn, &conn_list, conns_list) {
465 		t = conn->transport;
466 		ksmbd_conn_set_exiting(conn);
467 		if (t->ops->shutdown) {
468 			up_read(&conn_list_lock);
469 			t->ops->shutdown(t);
470 			down_read(&conn_list_lock);
471 		}
472 	}
473 	up_read(&conn_list_lock);
474 
475 	if (!list_empty(&conn_list)) {
476 		msleep(100);
477 		goto again;
478 	}
479 }
480 
ksmbd_conn_transport_destroy(void)481 void ksmbd_conn_transport_destroy(void)
482 {
483 	mutex_lock(&init_lock);
484 	ksmbd_tcp_destroy();
485 	ksmbd_rdma_destroy();
486 	stop_sessions();
487 	mutex_unlock(&init_lock);
488 }
489