GNU Linux-libre 6.0.2-gnu
[releases.git] / fs / ksmbd / server.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
4  *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
5  */
6
7 #include "glob.h"
8 #include "oplock.h"
9 #include "misc.h"
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>
15
16 #include "server.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"
23 #include "auth.h"
24
25 int ksmbd_debug_types;
26
27 struct ksmbd_server_config server_conf;
28
29 enum SERVER_CTRL_TYPE {
30         SERVER_CTRL_TYPE_INIT,
31         SERVER_CTRL_TYPE_RESET,
32 };
33
34 struct server_ctrl_struct {
35         int                     type;
36         struct work_struct      ctrl_work;
37 };
38
39 static DEFINE_MUTEX(ctrl_lock);
40
41 static int ___server_conf_set(int idx, char *val)
42 {
43         if (idx >= ARRAY_SIZE(server_conf.conf))
44                 return -EINVAL;
45
46         if (!val || val[0] == 0x00)
47                 return -EINVAL;
48
49         kfree(server_conf.conf[idx]);
50         server_conf.conf[idx] = kstrdup(val, GFP_KERNEL);
51         if (!server_conf.conf[idx])
52                 return -ENOMEM;
53         return 0;
54 }
55
56 int ksmbd_set_netbios_name(char *v)
57 {
58         return ___server_conf_set(SERVER_CONF_NETBIOS_NAME, v);
59 }
60
61 int ksmbd_set_server_string(char *v)
62 {
63         return ___server_conf_set(SERVER_CONF_SERVER_STRING, v);
64 }
65
66 int ksmbd_set_work_group(char *v)
67 {
68         return ___server_conf_set(SERVER_CONF_WORK_GROUP, v);
69 }
70
71 char *ksmbd_netbios_name(void)
72 {
73         return server_conf.conf[SERVER_CONF_NETBIOS_NAME];
74 }
75
76 char *ksmbd_server_string(void)
77 {
78         return server_conf.conf[SERVER_CONF_SERVER_STRING];
79 }
80
81 char *ksmbd_work_group(void)
82 {
83         return server_conf.conf[SERVER_CONF_WORK_GROUP];
84 }
85
86 /**
87  * check_conn_state() - check state of server thread connection
88  * @work:     smb work containing server thread information
89  *
90  * Return:      0 on valid connection, otherwise 1 to reconnect
91  */
92 static inline int check_conn_state(struct ksmbd_work *work)
93 {
94         struct smb_hdr *rsp_hdr;
95
96         if (ksmbd_conn_exiting(work) || ksmbd_conn_need_reconnect(work)) {
97                 rsp_hdr = work->response_buf;
98                 rsp_hdr->Status.CifsError = STATUS_CONNECTION_DISCONNECTED;
99                 return 1;
100         }
101         return 0;
102 }
103
104 #define SERVER_HANDLER_CONTINUE         0
105 #define SERVER_HANDLER_ABORT            1
106
107 static int __process_request(struct ksmbd_work *work, struct ksmbd_conn *conn,
108                              u16 *cmd)
109 {
110         struct smb_version_cmds *cmds;
111         u16 command;
112         int ret;
113
114         if (check_conn_state(work))
115                 return SERVER_HANDLER_CONTINUE;
116
117         if (ksmbd_verify_smb_message(work))
118                 return SERVER_HANDLER_ABORT;
119
120         command = conn->ops->get_cmd_val(work);
121         *cmd = command;
122
123 andx_again:
124         if (command >= conn->max_cmds) {
125                 conn->ops->set_rsp_status(work, STATUS_INVALID_PARAMETER);
126                 return SERVER_HANDLER_CONTINUE;
127         }
128
129         cmds = &conn->cmds[command];
130         if (!cmds->proc) {
131                 ksmbd_debug(SMB, "*** not implemented yet cmd = %x\n", command);
132                 conn->ops->set_rsp_status(work, STATUS_NOT_IMPLEMENTED);
133                 return SERVER_HANDLER_CONTINUE;
134         }
135
136         if (work->sess && conn->ops->is_sign_req(work, command)) {
137                 ret = conn->ops->check_sign_req(work);
138                 if (!ret) {
139                         conn->ops->set_rsp_status(work, STATUS_ACCESS_DENIED);
140                         return SERVER_HANDLER_CONTINUE;
141                 }
142         }
143
144         ret = cmds->proc(work);
145
146         if (ret < 0)
147                 ksmbd_debug(CONN, "Failed to process %u [%d]\n", command, ret);
148         /* AndX commands - chained request can return positive values */
149         else if (ret > 0) {
150                 command = ret;
151                 *cmd = command;
152                 goto andx_again;
153         }
154
155         if (work->send_no_response)
156                 return SERVER_HANDLER_ABORT;
157         return SERVER_HANDLER_CONTINUE;
158 }
159
160 static void __handle_ksmbd_work(struct ksmbd_work *work,
161                                 struct ksmbd_conn *conn)
162 {
163         u16 command = 0;
164         int rc;
165
166         if (conn->ops->allocate_rsp_buf(work))
167                 return;
168
169         if (conn->ops->is_transform_hdr &&
170             conn->ops->is_transform_hdr(work->request_buf)) {
171                 rc = conn->ops->decrypt_req(work);
172                 if (rc < 0) {
173                         conn->ops->set_rsp_status(work, STATUS_DATA_ERROR);
174                         goto send;
175                 }
176
177                 work->encrypted = true;
178         }
179
180         rc = conn->ops->init_rsp_hdr(work);
181         if (rc) {
182                 /* either uid or tid is not correct */
183                 conn->ops->set_rsp_status(work, STATUS_INVALID_HANDLE);
184                 goto send;
185         }
186
187         if (conn->ops->check_user_session) {
188                 rc = conn->ops->check_user_session(work);
189                 if (rc < 0) {
190                         command = conn->ops->get_cmd_val(work);
191                         conn->ops->set_rsp_status(work,
192                                         STATUS_USER_SESSION_DELETED);
193                         goto send;
194                 } else if (rc > 0) {
195                         rc = conn->ops->get_ksmbd_tcon(work);
196                         if (rc < 0) {
197                                 conn->ops->set_rsp_status(work,
198                                         STATUS_NETWORK_NAME_DELETED);
199                                 goto send;
200                         }
201                 }
202         }
203
204         do {
205                 rc = __process_request(work, conn, &command);
206                 if (rc == SERVER_HANDLER_ABORT)
207                         break;
208
209                 /*
210                  * Call smb2_set_rsp_credits() function to set number of credits
211                  * granted in hdr of smb2 response.
212                  */
213                 if (conn->ops->set_rsp_credits) {
214                         spin_lock(&conn->credits_lock);
215                         rc = conn->ops->set_rsp_credits(work);
216                         spin_unlock(&conn->credits_lock);
217                         if (rc < 0) {
218                                 conn->ops->set_rsp_status(work,
219                                         STATUS_INVALID_PARAMETER);
220                                 goto send;
221                         }
222                 }
223
224                 if (work->sess &&
225                     (work->sess->sign || smb3_11_final_sess_setup_resp(work) ||
226                      conn->ops->is_sign_req(work, command)))
227                         conn->ops->set_sign_rsp(work);
228         } while (is_chained_smb2_message(work));
229
230         if (work->send_no_response)
231                 return;
232
233 send:
234         smb3_preauth_hash_rsp(work);
235         if (work->sess && work->sess->enc && work->encrypted &&
236             conn->ops->encrypt_resp) {
237                 rc = conn->ops->encrypt_resp(work);
238                 if (rc < 0) {
239                         conn->ops->set_rsp_status(work, STATUS_DATA_ERROR);
240                         goto send;
241                 }
242         }
243
244         ksmbd_conn_write(work);
245 }
246
247 /**
248  * handle_ksmbd_work() - process pending smb work requests
249  * @wk: smb work containing request command buffer
250  *
251  * called by kworker threads to processing remaining smb work requests
252  */
253 static void handle_ksmbd_work(struct work_struct *wk)
254 {
255         struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work);
256         struct ksmbd_conn *conn = work->conn;
257
258         atomic64_inc(&conn->stats.request_served);
259
260         __handle_ksmbd_work(work, conn);
261
262         ksmbd_conn_try_dequeue_request(work);
263         ksmbd_free_work_struct(work);
264         /*
265          * Checking waitqueue to dropping pending requests on
266          * disconnection. waitqueue_active is safe because it
267          * uses atomic operation for condition.
268          */
269         if (!atomic_dec_return(&conn->r_count) && waitqueue_active(&conn->r_count_q))
270                 wake_up(&conn->r_count_q);
271 }
272
273 /**
274  * queue_ksmbd_work() - queue a smb request to worker thread queue
275  *              for proccessing smb command and sending response
276  * @conn:       connection instance
277  *
278  * read remaining data from socket create and submit work.
279  */
280 static int queue_ksmbd_work(struct ksmbd_conn *conn)
281 {
282         struct ksmbd_work *work;
283
284         work = ksmbd_alloc_work_struct();
285         if (!work) {
286                 pr_err("allocation for work failed\n");
287                 return -ENOMEM;
288         }
289
290         work->conn = conn;
291         work->request_buf = conn->request_buf;
292         conn->request_buf = NULL;
293
294         if (ksmbd_init_smb_server(work)) {
295                 ksmbd_free_work_struct(work);
296                 return -EINVAL;
297         }
298
299         ksmbd_conn_enqueue_request(work);
300         atomic_inc(&conn->r_count);
301         /* update activity on connection */
302         conn->last_active = jiffies;
303         INIT_WORK(&work->work, handle_ksmbd_work);
304         ksmbd_queue_work(work);
305         return 0;
306 }
307
308 static int ksmbd_server_process_request(struct ksmbd_conn *conn)
309 {
310         return queue_ksmbd_work(conn);
311 }
312
313 static int ksmbd_server_terminate_conn(struct ksmbd_conn *conn)
314 {
315         ksmbd_sessions_deregister(conn);
316         destroy_lease_table(conn);
317         return 0;
318 }
319
320 static void ksmbd_server_tcp_callbacks_init(void)
321 {
322         struct ksmbd_conn_ops ops;
323
324         ops.process_fn = ksmbd_server_process_request;
325         ops.terminate_fn = ksmbd_server_terminate_conn;
326
327         ksmbd_conn_init_server_callbacks(&ops);
328 }
329
330 static void server_conf_free(void)
331 {
332         int i;
333
334         for (i = 0; i < ARRAY_SIZE(server_conf.conf); i++) {
335                 kfree(server_conf.conf[i]);
336                 server_conf.conf[i] = NULL;
337         }
338 }
339
340 static int server_conf_init(void)
341 {
342         WRITE_ONCE(server_conf.state, SERVER_STATE_STARTING_UP);
343         server_conf.enforced_signing = 0;
344         server_conf.min_protocol = ksmbd_min_protocol();
345         server_conf.max_protocol = ksmbd_max_protocol();
346         server_conf.auth_mechs = KSMBD_AUTH_NTLMSSP;
347 #ifdef CONFIG_SMB_SERVER_KERBEROS5
348         server_conf.auth_mechs |= KSMBD_AUTH_KRB5 |
349                                 KSMBD_AUTH_MSKRB5;
350 #endif
351         return 0;
352 }
353
354 static void server_ctrl_handle_init(struct server_ctrl_struct *ctrl)
355 {
356         int ret;
357
358         ret = ksmbd_conn_transport_init();
359         if (ret) {
360                 server_queue_ctrl_reset_work();
361                 return;
362         }
363
364         WRITE_ONCE(server_conf.state, SERVER_STATE_RUNNING);
365 }
366
367 static void server_ctrl_handle_reset(struct server_ctrl_struct *ctrl)
368 {
369         ksmbd_ipc_soft_reset();
370         ksmbd_conn_transport_destroy();
371         server_conf_free();
372         server_conf_init();
373         WRITE_ONCE(server_conf.state, SERVER_STATE_STARTING_UP);
374 }
375
376 static void server_ctrl_handle_work(struct work_struct *work)
377 {
378         struct server_ctrl_struct *ctrl;
379
380         ctrl = container_of(work, struct server_ctrl_struct, ctrl_work);
381
382         mutex_lock(&ctrl_lock);
383         switch (ctrl->type) {
384         case SERVER_CTRL_TYPE_INIT:
385                 server_ctrl_handle_init(ctrl);
386                 break;
387         case SERVER_CTRL_TYPE_RESET:
388                 server_ctrl_handle_reset(ctrl);
389                 break;
390         default:
391                 pr_err("Unknown server work type: %d\n", ctrl->type);
392         }
393         mutex_unlock(&ctrl_lock);
394         kfree(ctrl);
395         module_put(THIS_MODULE);
396 }
397
398 static int __queue_ctrl_work(int type)
399 {
400         struct server_ctrl_struct *ctrl;
401
402         ctrl = kmalloc(sizeof(struct server_ctrl_struct), GFP_KERNEL);
403         if (!ctrl)
404                 return -ENOMEM;
405
406         __module_get(THIS_MODULE);
407         ctrl->type = type;
408         INIT_WORK(&ctrl->ctrl_work, server_ctrl_handle_work);
409         queue_work(system_long_wq, &ctrl->ctrl_work);
410         return 0;
411 }
412
413 int server_queue_ctrl_init_work(void)
414 {
415         return __queue_ctrl_work(SERVER_CTRL_TYPE_INIT);
416 }
417
418 int server_queue_ctrl_reset_work(void)
419 {
420         return __queue_ctrl_work(SERVER_CTRL_TYPE_RESET);
421 }
422
423 static ssize_t stats_show(struct class *class, struct class_attribute *attr,
424                           char *buf)
425 {
426         /*
427          * Inc this each time you change stats output format,
428          * so user space will know what to do.
429          */
430         static int stats_version = 2;
431         static const char * const state[] = {
432                 "startup",
433                 "running",
434                 "reset",
435                 "shutdown"
436         };
437
438         ssize_t sz = scnprintf(buf, PAGE_SIZE, "%d %s %d %lu\n", stats_version,
439                                state[server_conf.state], server_conf.tcp_port,
440                                server_conf.ipc_last_active / HZ);
441         return sz;
442 }
443
444 static ssize_t kill_server_store(struct class *class,
445                                  struct class_attribute *attr, const char *buf,
446                                  size_t len)
447 {
448         if (!sysfs_streq(buf, "hard"))
449                 return len;
450
451         pr_info("kill command received\n");
452         mutex_lock(&ctrl_lock);
453         WRITE_ONCE(server_conf.state, SERVER_STATE_RESETTING);
454         __module_get(THIS_MODULE);
455         server_ctrl_handle_reset(NULL);
456         module_put(THIS_MODULE);
457         mutex_unlock(&ctrl_lock);
458         return len;
459 }
460
461 static const char * const debug_type_strings[] = {"smb", "auth", "vfs",
462                                                   "oplock", "ipc", "conn",
463                                                   "rdma"};
464
465 static ssize_t debug_show(struct class *class, struct class_attribute *attr,
466                           char *buf)
467 {
468         ssize_t sz = 0;
469         int i, pos = 0;
470
471         for (i = 0; i < ARRAY_SIZE(debug_type_strings); i++) {
472                 if ((ksmbd_debug_types >> i) & 1) {
473                         pos = scnprintf(buf + sz,
474                                         PAGE_SIZE - sz,
475                                         "[%s] ",
476                                         debug_type_strings[i]);
477                 } else {
478                         pos = scnprintf(buf + sz,
479                                         PAGE_SIZE - sz,
480                                         "%s ",
481                                         debug_type_strings[i]);
482                 }
483                 sz += pos;
484         }
485         sz += scnprintf(buf + sz, PAGE_SIZE - sz, "\n");
486         return sz;
487 }
488
489 static ssize_t debug_store(struct class *class, struct class_attribute *attr,
490                            const char *buf, size_t len)
491 {
492         int i;
493
494         for (i = 0; i < ARRAY_SIZE(debug_type_strings); i++) {
495                 if (sysfs_streq(buf, "all")) {
496                         if (ksmbd_debug_types == KSMBD_DEBUG_ALL)
497                                 ksmbd_debug_types = 0;
498                         else
499                                 ksmbd_debug_types = KSMBD_DEBUG_ALL;
500                         break;
501                 }
502
503                 if (sysfs_streq(buf, debug_type_strings[i])) {
504                         if (ksmbd_debug_types & (1 << i))
505                                 ksmbd_debug_types &= ~(1 << i);
506                         else
507                                 ksmbd_debug_types |= (1 << i);
508                         break;
509                 }
510         }
511
512         return len;
513 }
514
515 static CLASS_ATTR_RO(stats);
516 static CLASS_ATTR_WO(kill_server);
517 static CLASS_ATTR_RW(debug);
518
519 static struct attribute *ksmbd_control_class_attrs[] = {
520         &class_attr_stats.attr,
521         &class_attr_kill_server.attr,
522         &class_attr_debug.attr,
523         NULL,
524 };
525 ATTRIBUTE_GROUPS(ksmbd_control_class);
526
527 static struct class ksmbd_control_class = {
528         .name           = "ksmbd-control",
529         .owner          = THIS_MODULE,
530         .class_groups   = ksmbd_control_class_groups,
531 };
532
533 static int ksmbd_server_shutdown(void)
534 {
535         WRITE_ONCE(server_conf.state, SERVER_STATE_SHUTTING_DOWN);
536
537         class_unregister(&ksmbd_control_class);
538         ksmbd_workqueue_destroy();
539         ksmbd_ipc_release();
540         ksmbd_conn_transport_destroy();
541         ksmbd_crypto_destroy();
542         ksmbd_free_global_file_table();
543         destroy_lease_table(NULL);
544         ksmbd_work_pool_destroy();
545         ksmbd_exit_file_cache();
546         server_conf_free();
547         return 0;
548 }
549
550 static int __init ksmbd_server_init(void)
551 {
552         int ret;
553
554         ret = class_register(&ksmbd_control_class);
555         if (ret) {
556                 pr_err("Unable to register ksmbd-control class\n");
557                 return ret;
558         }
559
560         ksmbd_server_tcp_callbacks_init();
561
562         ret = server_conf_init();
563         if (ret)
564                 goto err_unregister;
565
566         ret = ksmbd_work_pool_init();
567         if (ret)
568                 goto err_unregister;
569
570         ret = ksmbd_init_file_cache();
571         if (ret)
572                 goto err_destroy_work_pools;
573
574         ret = ksmbd_ipc_init();
575         if (ret)
576                 goto err_exit_file_cache;
577
578         ret = ksmbd_init_global_file_table();
579         if (ret)
580                 goto err_ipc_release;
581
582         ret = ksmbd_inode_hash_init();
583         if (ret)
584                 goto err_destroy_file_table;
585
586         ret = ksmbd_crypto_create();
587         if (ret)
588                 goto err_release_inode_hash;
589
590         ret = ksmbd_workqueue_init();
591         if (ret)
592                 goto err_crypto_destroy;
593
594         pr_warn_once("The ksmbd server is experimental\n");
595
596         return 0;
597
598 err_crypto_destroy:
599         ksmbd_crypto_destroy();
600 err_release_inode_hash:
601         ksmbd_release_inode_hash();
602 err_destroy_file_table:
603         ksmbd_free_global_file_table();
604 err_ipc_release:
605         ksmbd_ipc_release();
606 err_exit_file_cache:
607         ksmbd_exit_file_cache();
608 err_destroy_work_pools:
609         ksmbd_work_pool_destroy();
610 err_unregister:
611         class_unregister(&ksmbd_control_class);
612
613         return ret;
614 }
615
616 /**
617  * ksmbd_server_exit() - shutdown forker thread and free memory at module exit
618  */
619 static void __exit ksmbd_server_exit(void)
620 {
621         ksmbd_server_shutdown();
622         ksmbd_release_inode_hash();
623 }
624
625 MODULE_AUTHOR("Namjae Jeon <linkinjeon@kernel.org>");
626 MODULE_VERSION(KSMBD_VERSION);
627 MODULE_DESCRIPTION("Linux kernel CIFS/SMB SERVER");
628 MODULE_LICENSE("GPL");
629 MODULE_SOFTDEP("pre: ecb");
630 MODULE_SOFTDEP("pre: hmac");
631 MODULE_SOFTDEP("pre: md5");
632 MODULE_SOFTDEP("pre: nls");
633 MODULE_SOFTDEP("pre: aes");
634 MODULE_SOFTDEP("pre: cmac");
635 MODULE_SOFTDEP("pre: sha256");
636 MODULE_SOFTDEP("pre: sha512");
637 MODULE_SOFTDEP("pre: aead2");
638 MODULE_SOFTDEP("pre: ccm");
639 MODULE_SOFTDEP("pre: gcm");
640 MODULE_SOFTDEP("pre: crc32");
641 module_init(ksmbd_server_init)
642 module_exit(ksmbd_server_exit)