1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
10 #include <linux/sched/signal.h>
11 #include <linux/workqueue.h>
12 #include <linux/sysfs.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
17 #include "smb_common.h"
18 #include "smbstatus.h"
19 #include "connection.h"
20 #include "transport_ipc.h"
21 #include "mgmt/user_session.h"
22 #include "crypto_ctx.h"
25 int ksmbd_debug_types;
27 struct ksmbd_server_config server_conf;
29 enum SERVER_CTRL_TYPE {
30 SERVER_CTRL_TYPE_INIT,
31 SERVER_CTRL_TYPE_RESET,
34 struct server_ctrl_struct {
36 struct work_struct ctrl_work;
39 static DEFINE_MUTEX(ctrl_lock);
41 static int ___server_conf_set(int idx, char *val)
43 if (idx >= ARRAY_SIZE(server_conf.conf))
46 if (!val || val[0] == 0x00)
49 kfree(server_conf.conf[idx]);
50 server_conf.conf[idx] = kstrdup(val, GFP_KERNEL);
51 if (!server_conf.conf[idx])
56 int ksmbd_set_netbios_name(char *v)
58 return ___server_conf_set(SERVER_CONF_NETBIOS_NAME, v);
61 int ksmbd_set_server_string(char *v)
63 return ___server_conf_set(SERVER_CONF_SERVER_STRING, v);
66 int ksmbd_set_work_group(char *v)
68 return ___server_conf_set(SERVER_CONF_WORK_GROUP, v);
71 char *ksmbd_netbios_name(void)
73 return server_conf.conf[SERVER_CONF_NETBIOS_NAME];
76 char *ksmbd_server_string(void)
78 return server_conf.conf[SERVER_CONF_SERVER_STRING];
81 char *ksmbd_work_group(void)
83 return server_conf.conf[SERVER_CONF_WORK_GROUP];
87 * check_conn_state() - check state of server thread connection
88 * @work: smb work containing server thread information
90 * Return: 0 on valid connection, otherwise 1 to reconnect
92 static inline int check_conn_state(struct ksmbd_work *work)
94 struct smb_hdr *rsp_hdr;
96 if (ksmbd_conn_exiting(work->conn) ||
97 ksmbd_conn_need_reconnect(work->conn)) {
98 rsp_hdr = work->response_buf;
99 rsp_hdr->Status.CifsError = STATUS_CONNECTION_DISCONNECTED;
105 #define SERVER_HANDLER_CONTINUE 0
106 #define SERVER_HANDLER_ABORT 1
108 static int __process_request(struct ksmbd_work *work, struct ksmbd_conn *conn,
111 struct smb_version_cmds *cmds;
115 if (check_conn_state(work))
116 return SERVER_HANDLER_CONTINUE;
118 if (ksmbd_verify_smb_message(work)) {
119 conn->ops->set_rsp_status(work, STATUS_INVALID_PARAMETER);
120 return SERVER_HANDLER_ABORT;
123 command = conn->ops->get_cmd_val(work);
127 if (command >= conn->max_cmds) {
128 conn->ops->set_rsp_status(work, STATUS_INVALID_PARAMETER);
129 return SERVER_HANDLER_CONTINUE;
132 cmds = &conn->cmds[command];
134 ksmbd_debug(SMB, "*** not implemented yet cmd = %x\n", command);
135 conn->ops->set_rsp_status(work, STATUS_NOT_IMPLEMENTED);
136 return SERVER_HANDLER_CONTINUE;
139 if (work->sess && conn->ops->is_sign_req(work, command)) {
140 ret = conn->ops->check_sign_req(work);
142 conn->ops->set_rsp_status(work, STATUS_ACCESS_DENIED);
143 return SERVER_HANDLER_CONTINUE;
147 ret = cmds->proc(work);
150 ksmbd_debug(CONN, "Failed to process %u [%d]\n", command, ret);
151 /* AndX commands - chained request can return positive values */
158 if (work->send_no_response)
159 return SERVER_HANDLER_ABORT;
160 return SERVER_HANDLER_CONTINUE;
163 static void __handle_ksmbd_work(struct ksmbd_work *work,
164 struct ksmbd_conn *conn)
168 bool is_chained = false;
170 if (conn->ops->allocate_rsp_buf(work))
173 if (conn->ops->is_transform_hdr &&
174 conn->ops->is_transform_hdr(work->request_buf)) {
175 rc = conn->ops->decrypt_req(work);
177 conn->ops->set_rsp_status(work, STATUS_DATA_ERROR);
181 work->encrypted = true;
184 rc = conn->ops->init_rsp_hdr(work);
186 /* either uid or tid is not correct */
187 conn->ops->set_rsp_status(work, STATUS_INVALID_HANDLE);
192 if (conn->ops->check_user_session) {
193 rc = conn->ops->check_user_session(work);
196 conn->ops->set_rsp_status(work,
197 STATUS_INVALID_PARAMETER);
199 conn->ops->set_rsp_status(work,
200 STATUS_USER_SESSION_DELETED);
203 rc = conn->ops->get_ksmbd_tcon(work);
206 conn->ops->set_rsp_status(work,
207 STATUS_INVALID_PARAMETER);
209 conn->ops->set_rsp_status(work,
210 STATUS_NETWORK_NAME_DELETED);
216 rc = __process_request(work, conn, &command);
217 if (rc == SERVER_HANDLER_ABORT)
221 * Call smb2_set_rsp_credits() function to set number of credits
222 * granted in hdr of smb2 response.
224 if (conn->ops->set_rsp_credits) {
225 spin_lock(&conn->credits_lock);
226 rc = conn->ops->set_rsp_credits(work);
227 spin_unlock(&conn->credits_lock);
229 conn->ops->set_rsp_status(work,
230 STATUS_INVALID_PARAMETER);
235 is_chained = is_chained_smb2_message(work);
238 (work->sess->sign || smb3_11_final_sess_setup_resp(work) ||
239 conn->ops->is_sign_req(work, command)))
240 conn->ops->set_sign_rsp(work);
241 } while (is_chained == true);
245 ksmbd_tree_connect_put(work->tcon);
246 smb3_preauth_hash_rsp(work);
247 if (work->sess && work->sess->enc && work->encrypted &&
248 conn->ops->encrypt_resp) {
249 rc = conn->ops->encrypt_resp(work);
251 conn->ops->set_rsp_status(work, STATUS_DATA_ERROR);
254 ksmbd_conn_write(work);
258 * handle_ksmbd_work() - process pending smb work requests
259 * @wk: smb work containing request command buffer
261 * called by kworker threads to processing remaining smb work requests
263 static void handle_ksmbd_work(struct work_struct *wk)
265 struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work);
266 struct ksmbd_conn *conn = work->conn;
268 atomic64_inc(&conn->stats.request_served);
270 __handle_ksmbd_work(work, conn);
272 ksmbd_conn_try_dequeue_request(work);
273 ksmbd_free_work_struct(work);
275 * Checking waitqueue to dropping pending requests on
276 * disconnection. waitqueue_active is safe because it
277 * uses atomic operation for condition.
279 if (!atomic_dec_return(&conn->r_count) && waitqueue_active(&conn->r_count_q))
280 wake_up(&conn->r_count_q);
284 * queue_ksmbd_work() - queue a smb request to worker thread queue
285 * for proccessing smb command and sending response
286 * @conn: connection instance
288 * read remaining data from socket create and submit work.
290 static int queue_ksmbd_work(struct ksmbd_conn *conn)
292 struct ksmbd_work *work;
295 work = ksmbd_alloc_work_struct();
297 pr_err("allocation for work failed\n");
302 work->request_buf = conn->request_buf;
303 conn->request_buf = NULL;
305 err = ksmbd_init_smb_server(work);
307 ksmbd_free_work_struct(work);
311 ksmbd_conn_enqueue_request(work);
312 atomic_inc(&conn->r_count);
313 /* update activity on connection */
314 conn->last_active = jiffies;
315 INIT_WORK(&work->work, handle_ksmbd_work);
316 ksmbd_queue_work(work);
320 static int ksmbd_server_process_request(struct ksmbd_conn *conn)
322 return queue_ksmbd_work(conn);
325 static int ksmbd_server_terminate_conn(struct ksmbd_conn *conn)
327 ksmbd_sessions_deregister(conn);
328 destroy_lease_table(conn);
332 static void ksmbd_server_tcp_callbacks_init(void)
334 struct ksmbd_conn_ops ops;
336 ops.process_fn = ksmbd_server_process_request;
337 ops.terminate_fn = ksmbd_server_terminate_conn;
339 ksmbd_conn_init_server_callbacks(&ops);
342 static void server_conf_free(void)
346 for (i = 0; i < ARRAY_SIZE(server_conf.conf); i++) {
347 kfree(server_conf.conf[i]);
348 server_conf.conf[i] = NULL;
352 static int server_conf_init(void)
354 WRITE_ONCE(server_conf.state, SERVER_STATE_STARTING_UP);
355 server_conf.enforced_signing = 0;
356 server_conf.min_protocol = ksmbd_min_protocol();
357 server_conf.max_protocol = ksmbd_max_protocol();
358 server_conf.auth_mechs = KSMBD_AUTH_NTLMSSP;
359 #ifdef CONFIG_SMB_SERVER_KERBEROS5
360 server_conf.auth_mechs |= KSMBD_AUTH_KRB5 |
366 static void server_ctrl_handle_init(struct server_ctrl_struct *ctrl)
370 ret = ksmbd_conn_transport_init();
372 server_queue_ctrl_reset_work();
376 WRITE_ONCE(server_conf.state, SERVER_STATE_RUNNING);
379 static void server_ctrl_handle_reset(struct server_ctrl_struct *ctrl)
381 ksmbd_ipc_soft_reset();
382 ksmbd_conn_transport_destroy();
385 WRITE_ONCE(server_conf.state, SERVER_STATE_STARTING_UP);
388 static void server_ctrl_handle_work(struct work_struct *work)
390 struct server_ctrl_struct *ctrl;
392 ctrl = container_of(work, struct server_ctrl_struct, ctrl_work);
394 mutex_lock(&ctrl_lock);
395 switch (ctrl->type) {
396 case SERVER_CTRL_TYPE_INIT:
397 server_ctrl_handle_init(ctrl);
399 case SERVER_CTRL_TYPE_RESET:
400 server_ctrl_handle_reset(ctrl);
403 pr_err("Unknown server work type: %d\n", ctrl->type);
405 mutex_unlock(&ctrl_lock);
407 module_put(THIS_MODULE);
410 static int __queue_ctrl_work(int type)
412 struct server_ctrl_struct *ctrl;
414 ctrl = kmalloc(sizeof(struct server_ctrl_struct), GFP_KERNEL);
418 __module_get(THIS_MODULE);
420 INIT_WORK(&ctrl->ctrl_work, server_ctrl_handle_work);
421 queue_work(system_long_wq, &ctrl->ctrl_work);
425 int server_queue_ctrl_init_work(void)
427 return __queue_ctrl_work(SERVER_CTRL_TYPE_INIT);
430 int server_queue_ctrl_reset_work(void)
432 return __queue_ctrl_work(SERVER_CTRL_TYPE_RESET);
435 static ssize_t stats_show(const struct class *class, const struct class_attribute *attr,
439 * Inc this each time you change stats output format,
440 * so user space will know what to do.
442 static int stats_version = 2;
443 static const char * const state[] = {
449 return sysfs_emit(buf, "%d %s %d %lu\n", stats_version,
450 state[server_conf.state], server_conf.tcp_port,
451 server_conf.ipc_last_active / HZ);
454 static ssize_t kill_server_store(const struct class *class,
455 const struct class_attribute *attr, const char *buf,
458 if (!sysfs_streq(buf, "hard"))
461 pr_info("kill command received\n");
462 mutex_lock(&ctrl_lock);
463 WRITE_ONCE(server_conf.state, SERVER_STATE_RESETTING);
464 __module_get(THIS_MODULE);
465 server_ctrl_handle_reset(NULL);
466 module_put(THIS_MODULE);
467 mutex_unlock(&ctrl_lock);
471 static const char * const debug_type_strings[] = {"smb", "auth", "vfs",
472 "oplock", "ipc", "conn",
475 static ssize_t debug_show(const struct class *class, const struct class_attribute *attr,
481 for (i = 0; i < ARRAY_SIZE(debug_type_strings); i++) {
482 if ((ksmbd_debug_types >> i) & 1) {
483 pos = sysfs_emit_at(buf, sz, "[%s] ", debug_type_strings[i]);
485 pos = sysfs_emit_at(buf, sz, "%s ", debug_type_strings[i]);
489 sz += sysfs_emit_at(buf, sz, "\n");
493 static ssize_t debug_store(const struct class *class, const struct class_attribute *attr,
494 const char *buf, size_t len)
498 for (i = 0; i < ARRAY_SIZE(debug_type_strings); i++) {
499 if (sysfs_streq(buf, "all")) {
500 if (ksmbd_debug_types == KSMBD_DEBUG_ALL)
501 ksmbd_debug_types = 0;
503 ksmbd_debug_types = KSMBD_DEBUG_ALL;
507 if (sysfs_streq(buf, debug_type_strings[i])) {
508 if (ksmbd_debug_types & (1 << i))
509 ksmbd_debug_types &= ~(1 << i);
511 ksmbd_debug_types |= (1 << i);
519 static CLASS_ATTR_RO(stats);
520 static CLASS_ATTR_WO(kill_server);
521 static CLASS_ATTR_RW(debug);
523 static struct attribute *ksmbd_control_class_attrs[] = {
524 &class_attr_stats.attr,
525 &class_attr_kill_server.attr,
526 &class_attr_debug.attr,
529 ATTRIBUTE_GROUPS(ksmbd_control_class);
531 static struct class ksmbd_control_class = {
532 .name = "ksmbd-control",
533 .class_groups = ksmbd_control_class_groups,
536 static int ksmbd_server_shutdown(void)
538 WRITE_ONCE(server_conf.state, SERVER_STATE_SHUTTING_DOWN);
540 class_unregister(&ksmbd_control_class);
541 ksmbd_workqueue_destroy();
543 ksmbd_conn_transport_destroy();
544 ksmbd_crypto_destroy();
545 ksmbd_free_global_file_table();
546 destroy_lease_table(NULL);
547 ksmbd_work_pool_destroy();
548 ksmbd_exit_file_cache();
553 static int __init ksmbd_server_init(void)
557 ret = class_register(&ksmbd_control_class);
559 pr_err("Unable to register ksmbd-control class\n");
563 ksmbd_server_tcp_callbacks_init();
565 ret = server_conf_init();
569 ret = ksmbd_work_pool_init();
573 ret = ksmbd_init_file_cache();
575 goto err_destroy_work_pools;
577 ret = ksmbd_ipc_init();
579 goto err_exit_file_cache;
581 ret = ksmbd_init_global_file_table();
583 goto err_ipc_release;
585 ret = ksmbd_inode_hash_init();
587 goto err_destroy_file_table;
589 ret = ksmbd_crypto_create();
591 goto err_release_inode_hash;
593 ret = ksmbd_workqueue_init();
595 goto err_crypto_destroy;
600 ksmbd_crypto_destroy();
601 err_release_inode_hash:
602 ksmbd_release_inode_hash();
603 err_destroy_file_table:
604 ksmbd_free_global_file_table();
608 ksmbd_exit_file_cache();
609 err_destroy_work_pools:
610 ksmbd_work_pool_destroy();
612 class_unregister(&ksmbd_control_class);
618 * ksmbd_server_exit() - shutdown forker thread and free memory at module exit
620 static void __exit ksmbd_server_exit(void)
622 ksmbd_server_shutdown();
624 ksmbd_release_inode_hash();
627 MODULE_AUTHOR("Namjae Jeon <linkinjeon@kernel.org>");
628 MODULE_VERSION(KSMBD_VERSION);
629 MODULE_DESCRIPTION("Linux kernel CIFS/SMB SERVER");
630 MODULE_LICENSE("GPL");
631 MODULE_SOFTDEP("pre: ecb");
632 MODULE_SOFTDEP("pre: hmac");
633 MODULE_SOFTDEP("pre: md5");
634 MODULE_SOFTDEP("pre: nls");
635 MODULE_SOFTDEP("pre: aes");
636 MODULE_SOFTDEP("pre: cmac");
637 MODULE_SOFTDEP("pre: sha256");
638 MODULE_SOFTDEP("pre: sha512");
639 MODULE_SOFTDEP("pre: aead2");
640 MODULE_SOFTDEP("pre: ccm");
641 MODULE_SOFTDEP("pre: gcm");
642 MODULE_SOFTDEP("pre: crc32");
643 module_init(ksmbd_server_init)
644 module_exit(ksmbd_server_exit)