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