1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Namjae Jeon <namjae.jeon@protocolfreedom.org>
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 #include <linux/mutex.h>
8 #include <linux/freezer.h>
9 #include <linux/module.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"
18 static DEFINE_MUTEX(init_lock);
20 static struct ksmbd_conn_ops default_conn_ops;
23 DECLARE_RWSEM(conn_list_lock);
26 * ksmbd_conn_free() - free resources of the connection instance
28 * @conn: connection instance to be cleand up
30 * During the thread termination, the corresponding conn instance
31 * resources(sock/memory) are released and finally the conn object is freed.
33 void ksmbd_conn_free(struct ksmbd_conn *conn)
35 down_write(&conn_list_lock);
36 list_del(&conn->conns_list);
37 up_write(&conn_list_lock);
39 xa_destroy(&conn->sessions);
40 kvfree(conn->request_buf);
41 kfree(conn->preauth_info);
46 * ksmbd_conn_alloc() - initialize a new connection instance
48 * Return: ksmbd_conn struct on success, otherwise NULL
50 struct ksmbd_conn *ksmbd_conn_alloc(void)
52 struct ksmbd_conn *conn;
54 conn = kzalloc(sizeof(struct ksmbd_conn), GFP_KERNEL);
58 conn->need_neg = true;
59 ksmbd_conn_set_new(conn);
60 conn->local_nls = load_nls("utf8");
62 conn->local_nls = load_nls_default();
63 if (IS_ENABLED(CONFIG_UNICODE))
64 conn->um = utf8_load(UNICODE_AGE(12, 1, 0));
66 conn->um = ERR_PTR(-EOPNOTSUPP);
69 atomic_set(&conn->req_running, 0);
70 atomic_set(&conn->r_count, 0);
71 conn->total_credits = 1;
72 conn->outstanding_credits = 0;
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);
84 spin_lock_init(&conn->llist_lock);
85 INIT_LIST_HEAD(&conn->lock_list);
87 init_rwsem(&conn->session_lock);
89 down_write(&conn_list_lock);
90 list_add(&conn->conns_list, &conn_list);
91 up_write(&conn_list_lock);
95 bool ksmbd_conn_lookup_dialect(struct ksmbd_conn *c)
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))
108 up_read(&conn_list_lock);
112 void ksmbd_conn_enqueue_request(struct ksmbd_work *work)
114 struct ksmbd_conn *conn = work->conn;
115 struct list_head *requests_queue = NULL;
117 if (conn->ops->get_cmd_val(work) != SMB2_CANCEL_HE)
118 requests_queue = &conn->requests;
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);
128 void ksmbd_conn_try_dequeue_request(struct ksmbd_work *work)
130 struct ksmbd_conn *conn = work->conn;
132 if (list_empty(&work->request_entry) &&
133 list_empty(&work->async_request_entry))
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);
143 wake_up_all(&conn->req_running_q);
146 void ksmbd_conn_lock(struct ksmbd_conn *conn)
148 mutex_lock(&conn->srv_mutex);
151 void ksmbd_conn_unlock(struct ksmbd_conn *conn)
153 mutex_unlock(&conn->srv_mutex);
156 void ksmbd_all_conn_set_status(u64 sess_id, u32 status)
158 struct ksmbd_conn *conn;
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);
165 up_read(&conn_list_lock);
168 void ksmbd_conn_wait_idle(struct ksmbd_conn *conn, u64 sess_id)
170 wait_event(conn->req_running_q, atomic_read(&conn->req_running) < 2);
173 int ksmbd_conn_write(struct ksmbd_work *work)
175 struct ksmbd_conn *conn = work->conn;
178 if (!work->response_buf) {
179 pr_err("NULL response header\n");
183 if (work->send_no_response)
189 ksmbd_conn_lock(conn);
190 sent = conn->transport->ops->writev(conn->transport, work->iov,
192 get_rfc1002_len(work->iov[0].iov_base) + 4,
193 work->need_invalidate_rkey,
195 ksmbd_conn_unlock(conn);
198 pr_err("Failed to send message: %d\n", sent);
205 int ksmbd_conn_rdma_read(struct ksmbd_conn *conn,
206 void *buf, unsigned int buflen,
207 struct smb2_buffer_desc_v1 *desc,
208 unsigned int desc_len)
212 if (conn->transport->ops->rdma_read)
213 ret = conn->transport->ops->rdma_read(conn->transport,
219 int ksmbd_conn_rdma_write(struct ksmbd_conn *conn,
220 void *buf, unsigned int buflen,
221 struct smb2_buffer_desc_v1 *desc,
222 unsigned int desc_len)
226 if (conn->transport->ops->rdma_write)
227 ret = conn->transport->ops->rdma_write(conn->transport,
233 bool ksmbd_conn_alive(struct ksmbd_conn *conn)
235 if (!ksmbd_server_running())
238 if (ksmbd_conn_exiting(conn))
241 if (kthread_should_stop())
244 if (atomic_read(&conn->stats.open_files_count) > 0)
248 * Stop current session if the time that get last request from client
249 * is bigger than deadtime user configured and opening file count is
252 if (server_conf.deadtime > 0 &&
253 time_after(jiffies, conn->last_active + server_conf.deadtime)) {
254 ksmbd_debug(CONN, "No response from client in %lu minutes\n",
255 server_conf.deadtime / SMB_ECHO_INTERVAL);
261 #define SMB1_MIN_SUPPORTED_HEADER_SIZE (sizeof(struct smb_hdr))
262 #define SMB2_MIN_SUPPORTED_HEADER_SIZE (sizeof(struct smb2_hdr) + 4)
265 * ksmbd_conn_handler_loop() - session thread to listen on new smb requests
266 * @p: connection instance
268 * One thread each per connection
270 * Return: 0 on success
272 int ksmbd_conn_handler_loop(void *p)
274 struct ksmbd_conn *conn = (struct ksmbd_conn *)p;
275 struct ksmbd_transport *t = conn->transport;
276 unsigned int pdu_size, max_allowed_pdu_size;
277 char hdr_buf[4] = {0,};
280 mutex_init(&conn->srv_mutex);
281 __module_get(THIS_MODULE);
283 if (t->ops->prepare && t->ops->prepare(t))
286 conn->last_active = jiffies;
288 while (ksmbd_conn_alive(conn)) {
292 kvfree(conn->request_buf);
293 conn->request_buf = NULL;
295 size = t->ops->read(t, hdr_buf, sizeof(hdr_buf), -1);
296 if (size != sizeof(hdr_buf))
299 pdu_size = get_rfc1002_len(hdr_buf);
300 ksmbd_debug(CONN, "RFC1002 header %u bytes\n", pdu_size);
302 if (ksmbd_conn_good(conn))
303 max_allowed_pdu_size =
304 SMB3_MAX_MSGSIZE + conn->vals->max_write_size;
306 max_allowed_pdu_size = SMB3_MAX_MSGSIZE;
308 if (pdu_size > max_allowed_pdu_size) {
309 pr_err_ratelimited("PDU length(%u) exceeded maximum allowed pdu size(%u) on connection(%d)\n",
310 pdu_size, max_allowed_pdu_size,
311 READ_ONCE(conn->status));
316 * Check maximum pdu size(0x00FFFFFF).
318 if (pdu_size > MAX_STREAM_PROT_LEN)
321 if (pdu_size < SMB1_MIN_SUPPORTED_HEADER_SIZE)
324 /* 4 for rfc1002 length field */
325 /* 1 for implied bcc[0] */
326 size = pdu_size + 4 + 1;
327 conn->request_buf = kvmalloc(size, GFP_KERNEL);
328 if (!conn->request_buf)
331 memcpy(conn->request_buf, hdr_buf, sizeof(hdr_buf));
334 * We already read 4 bytes to find out PDU size, now
337 size = t->ops->read(t, conn->request_buf + 4, pdu_size, 2);
339 pr_err("sock_read failed: %d\n", size);
343 if (size != pdu_size) {
344 pr_err("PDU error. Read: %d, Expected: %d\n",
349 if (!ksmbd_smb_request(conn))
352 if (((struct smb2_hdr *)smb2_get_msg(conn->request_buf))->ProtocolId ==
354 if (pdu_size < SMB2_MIN_SUPPORTED_HEADER_SIZE)
358 if (!default_conn_ops.process_fn) {
359 pr_err("No connection request callback\n");
363 if (default_conn_ops.process_fn(conn)) {
364 pr_err("Cannot handle request\n");
370 ksmbd_conn_set_releasing(conn);
371 /* Wait till all reference dropped to the Server object*/
372 wait_event(conn->r_count_q, atomic_read(&conn->r_count) == 0);
374 if (IS_ENABLED(CONFIG_UNICODE))
375 utf8_unload(conn->um);
376 unload_nls(conn->local_nls);
377 if (default_conn_ops.terminate_fn)
378 default_conn_ops.terminate_fn(conn);
379 t->ops->disconnect(t);
380 module_put(THIS_MODULE);
384 void ksmbd_conn_init_server_callbacks(struct ksmbd_conn_ops *ops)
386 default_conn_ops.process_fn = ops->process_fn;
387 default_conn_ops.terminate_fn = ops->terminate_fn;
390 int ksmbd_conn_transport_init(void)
394 mutex_lock(&init_lock);
395 ret = ksmbd_tcp_init();
397 pr_err("Failed to init TCP subsystem: %d\n", ret);
401 ret = ksmbd_rdma_init();
403 pr_err("Failed to init RDMA subsystem: %d\n", ret);
407 mutex_unlock(&init_lock);
411 static void stop_sessions(void)
413 struct ksmbd_conn *conn;
414 struct ksmbd_transport *t;
417 down_read(&conn_list_lock);
418 list_for_each_entry(conn, &conn_list, conns_list) {
420 ksmbd_conn_set_exiting(conn);
421 if (t->ops->shutdown) {
422 up_read(&conn_list_lock);
424 down_read(&conn_list_lock);
427 up_read(&conn_list_lock);
429 if (!list_empty(&conn_list)) {
430 schedule_timeout_interruptible(HZ / 10); /* 100ms */
435 void ksmbd_conn_transport_destroy(void)
437 mutex_lock(&init_lock);
439 ksmbd_rdma_destroy();
441 mutex_unlock(&init_lock);