blob: 1acf1892a466c10acefcb6ee9700e79268abf7fa [file] [log] [blame]
Namjae Jeon0626e662021-03-16 13:07:11 +09001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
4 */
5
6#include <linux/jhash.h>
7#include <linux/slab.h>
8#include <linux/rwsem.h>
9#include <linux/mutex.h>
10#include <linux/wait.h>
11#include <linux/hashtable.h>
12#include <net/net_namespace.h>
13#include <net/genetlink.h>
14#include <linux/socket.h>
15#include <linux/workqueue.h>
16
17#include "vfs_cache.h"
18#include "transport_ipc.h"
Namjae Jeon0626e662021-03-16 13:07:11 +090019#include "server.h"
20#include "smb_common.h"
21
22#include "mgmt/user_config.h"
23#include "mgmt/share_config.h"
24#include "mgmt/user_session.h"
25#include "mgmt/tree_connect.h"
26#include "mgmt/ksmbd_ida.h"
27#include "connection.h"
28#include "transport_tcp.h"
29
Namjae Jeon0626e662021-03-16 13:07:11 +090030#define IPC_WAIT_TIMEOUT (2 * HZ)
31
32#define IPC_MSG_HASH_BITS 3
33static DEFINE_HASHTABLE(ipc_msg_table, IPC_MSG_HASH_BITS);
34static DECLARE_RWSEM(ipc_msg_table_lock);
35static DEFINE_MUTEX(startup_lock);
36
Namjae Jeond40012a2021-04-13 13:06:30 +090037static DEFINE_IDA(ipc_ida);
Namjae Jeon0626e662021-03-16 13:07:11 +090038
39static unsigned int ksmbd_tools_pid;
40
Namjae Jeon0626e662021-03-16 13:07:11 +090041static bool ksmbd_ipc_validate_version(struct genl_info *m)
42{
43 if (m->genlhdr->version != KSMBD_GENL_VERSION) {
Namjae Jeonbde16942021-06-28 15:23:19 +090044 pr_err("%s. ksmbd: %d, kernel module: %d. %s.\n",
45 "Daemon and kernel module version mismatch",
46 m->genlhdr->version,
47 KSMBD_GENL_VERSION,
48 "User-space ksmbd should terminate");
Namjae Jeon0626e662021-03-16 13:07:11 +090049 return false;
50 }
51 return true;
52}
53
54struct ksmbd_ipc_msg {
55 unsigned int type;
56 unsigned int sz;
Hyunchul Leeb6229482021-06-25 07:02:06 +090057 unsigned char payload[];
Namjae Jeon0626e662021-03-16 13:07:11 +090058};
59
Namjae Jeon0626e662021-03-16 13:07:11 +090060struct ipc_msg_table_entry {
61 unsigned int handle;
62 unsigned int type;
63 wait_queue_head_t wait;
64 struct hlist_node ipc_table_hlist;
65
66 void *response;
67};
68
69static struct delayed_work ipc_timer_work;
70
71static int handle_startup_event(struct sk_buff *skb, struct genl_info *info);
Namjae Jeon64b39f42021-03-30 14:25:35 +090072static int handle_unsupported_event(struct sk_buff *skb, struct genl_info *info);
Namjae Jeon0626e662021-03-16 13:07:11 +090073static int handle_generic_event(struct sk_buff *skb, struct genl_info *info);
74static int ksmbd_ipc_heartbeat_request(void);
75
76static const struct nla_policy ksmbd_nl_policy[KSMBD_EVENT_MAX] = {
77 [KSMBD_EVENT_UNSPEC] = {
78 .len = 0,
79 },
80 [KSMBD_EVENT_HEARTBEAT_REQUEST] = {
81 .len = sizeof(struct ksmbd_heartbeat),
82 },
83 [KSMBD_EVENT_STARTING_UP] = {
84 .len = sizeof(struct ksmbd_startup_request),
85 },
86 [KSMBD_EVENT_SHUTTING_DOWN] = {
87 .len = sizeof(struct ksmbd_shutdown_request),
88 },
89 [KSMBD_EVENT_LOGIN_REQUEST] = {
90 .len = sizeof(struct ksmbd_login_request),
91 },
92 [KSMBD_EVENT_LOGIN_RESPONSE] = {
93 .len = sizeof(struct ksmbd_login_response),
94 },
95 [KSMBD_EVENT_SHARE_CONFIG_REQUEST] = {
96 .len = sizeof(struct ksmbd_share_config_request),
97 },
98 [KSMBD_EVENT_SHARE_CONFIG_RESPONSE] = {
99 .len = sizeof(struct ksmbd_share_config_response),
100 },
101 [KSMBD_EVENT_TREE_CONNECT_REQUEST] = {
102 .len = sizeof(struct ksmbd_tree_connect_request),
103 },
104 [KSMBD_EVENT_TREE_CONNECT_RESPONSE] = {
105 .len = sizeof(struct ksmbd_tree_connect_response),
106 },
107 [KSMBD_EVENT_TREE_DISCONNECT_REQUEST] = {
108 .len = sizeof(struct ksmbd_tree_disconnect_request),
109 },
110 [KSMBD_EVENT_LOGOUT_REQUEST] = {
111 .len = sizeof(struct ksmbd_logout_request),
112 },
113 [KSMBD_EVENT_RPC_REQUEST] = {
114 },
115 [KSMBD_EVENT_RPC_RESPONSE] = {
116 },
117 [KSMBD_EVENT_SPNEGO_AUTHEN_REQUEST] = {
118 },
119 [KSMBD_EVENT_SPNEGO_AUTHEN_RESPONSE] = {
120 },
121};
122
123static struct genl_ops ksmbd_genl_ops[] = {
124 {
125 .cmd = KSMBD_EVENT_UNSPEC,
126 .doit = handle_unsupported_event,
127 },
128 {
129 .cmd = KSMBD_EVENT_HEARTBEAT_REQUEST,
130 .doit = handle_unsupported_event,
131 },
132 {
133 .cmd = KSMBD_EVENT_STARTING_UP,
134 .doit = handle_startup_event,
135 },
136 {
137 .cmd = KSMBD_EVENT_SHUTTING_DOWN,
138 .doit = handle_unsupported_event,
139 },
140 {
141 .cmd = KSMBD_EVENT_LOGIN_REQUEST,
142 .doit = handle_unsupported_event,
143 },
144 {
145 .cmd = KSMBD_EVENT_LOGIN_RESPONSE,
146 .doit = handle_generic_event,
147 },
148 {
149 .cmd = KSMBD_EVENT_SHARE_CONFIG_REQUEST,
150 .doit = handle_unsupported_event,
151 },
152 {
153 .cmd = KSMBD_EVENT_SHARE_CONFIG_RESPONSE,
154 .doit = handle_generic_event,
155 },
156 {
157 .cmd = KSMBD_EVENT_TREE_CONNECT_REQUEST,
158 .doit = handle_unsupported_event,
159 },
160 {
161 .cmd = KSMBD_EVENT_TREE_CONNECT_RESPONSE,
162 .doit = handle_generic_event,
163 },
164 {
165 .cmd = KSMBD_EVENT_TREE_DISCONNECT_REQUEST,
166 .doit = handle_unsupported_event,
167 },
168 {
169 .cmd = KSMBD_EVENT_LOGOUT_REQUEST,
170 .doit = handle_unsupported_event,
171 },
172 {
173 .cmd = KSMBD_EVENT_RPC_REQUEST,
174 .doit = handle_unsupported_event,
175 },
176 {
177 .cmd = KSMBD_EVENT_RPC_RESPONSE,
178 .doit = handle_generic_event,
179 },
180 {
181 .cmd = KSMBD_EVENT_SPNEGO_AUTHEN_REQUEST,
182 .doit = handle_unsupported_event,
183 },
184 {
185 .cmd = KSMBD_EVENT_SPNEGO_AUTHEN_RESPONSE,
186 .doit = handle_generic_event,
187 },
188};
189
190static struct genl_family ksmbd_genl_family = {
191 .name = KSMBD_GENL_NAME,
192 .version = KSMBD_GENL_VERSION,
193 .hdrsize = 0,
194 .maxattr = KSMBD_EVENT_MAX,
195 .netnsok = true,
196 .module = THIS_MODULE,
197 .ops = ksmbd_genl_ops,
198 .n_ops = ARRAY_SIZE(ksmbd_genl_ops),
199};
200
201static void ksmbd_nl_init_fixup(void)
202{
203 int i;
204
205 for (i = 0; i < ARRAY_SIZE(ksmbd_genl_ops); i++)
206 ksmbd_genl_ops[i].validate = GENL_DONT_VALIDATE_STRICT |
207 GENL_DONT_VALIDATE_DUMP;
208
209 ksmbd_genl_family.policy = ksmbd_nl_policy;
210}
211
212static int rpc_context_flags(struct ksmbd_session *sess)
213{
214 if (user_guest(sess->user))
215 return KSMBD_RPC_RESTRICTED_CONTEXT;
216 return 0;
217}
218
219static void ipc_update_last_active(void)
220{
221 if (server_conf.ipc_timeout)
222 server_conf.ipc_last_active = jiffies;
223}
224
225static struct ksmbd_ipc_msg *ipc_msg_alloc(size_t sz)
226{
227 struct ksmbd_ipc_msg *msg;
228 size_t msg_sz = sz + sizeof(struct ksmbd_ipc_msg);
229
Namjae Jeon79f6b112021-04-02 12:47:14 +0900230 msg = kvmalloc(msg_sz, GFP_KERNEL | __GFP_ZERO);
Namjae Jeon0626e662021-03-16 13:07:11 +0900231 if (msg)
232 msg->sz = sz;
233 return msg;
234}
235
236static void ipc_msg_free(struct ksmbd_ipc_msg *msg)
237{
Namjae Jeon79f6b112021-04-02 12:47:14 +0900238 kvfree(msg);
Namjae Jeon0626e662021-03-16 13:07:11 +0900239}
240
241static void ipc_msg_handle_free(int handle)
242{
243 if (handle >= 0)
Namjae Jeond40012a2021-04-13 13:06:30 +0900244 ksmbd_release_id(&ipc_ida, handle);
Namjae Jeon0626e662021-03-16 13:07:11 +0900245}
246
247static int handle_response(int type, void *payload, size_t sz)
248{
Hyunchul Leeb6229482021-06-25 07:02:06 +0900249 unsigned int handle = *(unsigned int *)payload;
Namjae Jeon0626e662021-03-16 13:07:11 +0900250 struct ipc_msg_table_entry *entry;
251 int ret = 0;
252
253 ipc_update_last_active();
254 down_read(&ipc_msg_table_lock);
255 hash_for_each_possible(ipc_msg_table, entry, ipc_table_hlist, handle) {
256 if (handle != entry->handle)
257 continue;
258
259 entry->response = NULL;
260 /*
261 * Response message type value should be equal to
262 * request message type + 1.
263 */
264 if (entry->type + 1 != type) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900265 pr_err("Waiting for IPC type %d, got %d. Ignore.\n",
266 entry->type + 1, type);
Namjae Jeon0626e662021-03-16 13:07:11 +0900267 }
268
Namjae Jeon79f6b112021-04-02 12:47:14 +0900269 entry->response = kvmalloc(sz, GFP_KERNEL | __GFP_ZERO);
Namjae Jeon0626e662021-03-16 13:07:11 +0900270 if (!entry->response) {
271 ret = -ENOMEM;
272 break;
273 }
274
275 memcpy(entry->response, payload, sz);
276 wake_up_interruptible(&entry->wait);
277 ret = 0;
278 break;
279 }
280 up_read(&ipc_msg_table_lock);
281
282 return ret;
283}
284
285static int ipc_server_config_on_startup(struct ksmbd_startup_request *req)
286{
287 int ret;
288
289 ksmbd_set_fd_limit(req->file_max);
290 server_conf.flags = req->flags;
291 server_conf.signing = req->signing;
292 server_conf.tcp_port = req->tcp_port;
293 server_conf.ipc_timeout = req->ipc_timeout * HZ;
294 server_conf.deadtime = req->deadtime * SMB_ECHO_INTERVAL;
295 server_conf.share_fake_fscaps = req->share_fake_fscaps;
296 ksmbd_init_domain(req->sub_auth);
297
298 if (req->smb2_max_read)
299 init_smb2_max_read_size(req->smb2_max_read);
300 if (req->smb2_max_write)
301 init_smb2_max_write_size(req->smb2_max_write);
302 if (req->smb2_max_trans)
303 init_smb2_max_trans_size(req->smb2_max_trans);
304
305 ret = ksmbd_set_netbios_name(req->netbios_name);
306 ret |= ksmbd_set_server_string(req->server_string);
307 ret |= ksmbd_set_work_group(req->work_group);
308 ret |= ksmbd_tcp_set_interfaces(KSMBD_STARTUP_CONFIG_INTERFACES(req),
309 req->ifc_list_sz);
310 if (ret) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900311 pr_err("Server configuration error: %s %s %s\n",
312 req->netbios_name, req->server_string,
313 req->work_group);
Namjae Jeon0626e662021-03-16 13:07:11 +0900314 return ret;
315 }
316
317 if (req->min_prot[0]) {
318 ret = ksmbd_lookup_protocol_idx(req->min_prot);
319 if (ret >= 0)
320 server_conf.min_protocol = ret;
321 }
322 if (req->max_prot[0]) {
323 ret = ksmbd_lookup_protocol_idx(req->max_prot);
324 if (ret >= 0)
325 server_conf.max_protocol = ret;
326 }
327
328 if (server_conf.ipc_timeout)
329 schedule_delayed_work(&ipc_timer_work, server_conf.ipc_timeout);
330 return 0;
331}
332
333static int handle_startup_event(struct sk_buff *skb, struct genl_info *info)
334{
335 int ret = 0;
336
337#ifdef CONFIG_SMB_SERVER_CHECK_CAP_NET_ADMIN
338 if (!netlink_capable(skb, CAP_NET_ADMIN))
339 return -EPERM;
340#endif
341
342 if (!ksmbd_ipc_validate_version(info))
343 return -EINVAL;
344
345 if (!info->attrs[KSMBD_EVENT_STARTING_UP])
346 return -EINVAL;
347
348 mutex_lock(&startup_lock);
349 if (!ksmbd_server_configurable()) {
350 mutex_unlock(&startup_lock);
Namjae Jeonbde16942021-06-28 15:23:19 +0900351 pr_err("Server reset is in progress, can't start daemon\n");
Namjae Jeon0626e662021-03-16 13:07:11 +0900352 return -EINVAL;
353 }
354
355 if (ksmbd_tools_pid) {
356 if (ksmbd_ipc_heartbeat_request() == 0) {
357 ret = -EINVAL;
358 goto out;
359 }
360
Namjae Jeonbde16942021-06-28 15:23:19 +0900361 pr_err("Reconnect to a new user space daemon\n");
Namjae Jeon0626e662021-03-16 13:07:11 +0900362 } else {
363 struct ksmbd_startup_request *req;
364
365 req = nla_data(info->attrs[info->genlhdr->cmd]);
366 ret = ipc_server_config_on_startup(req);
367 if (ret)
368 goto out;
369 server_queue_ctrl_init_work();
370 }
371
372 ksmbd_tools_pid = info->snd_portid;
373 ipc_update_last_active();
374
375out:
376 mutex_unlock(&startup_lock);
377 return ret;
378}
379
Namjae Jeon64b39f42021-03-30 14:25:35 +0900380static int handle_unsupported_event(struct sk_buff *skb, struct genl_info *info)
Namjae Jeon0626e662021-03-16 13:07:11 +0900381{
Namjae Jeonbde16942021-06-28 15:23:19 +0900382 pr_err("Unknown IPC event: %d, ignore.\n", info->genlhdr->cmd);
Namjae Jeon0626e662021-03-16 13:07:11 +0900383 return -EINVAL;
384}
385
386static int handle_generic_event(struct sk_buff *skb, struct genl_info *info)
387{
388 void *payload;
389 int sz;
390 int type = info->genlhdr->cmd;
391
392#ifdef CONFIG_SMB_SERVER_CHECK_CAP_NET_ADMIN
393 if (!netlink_capable(skb, CAP_NET_ADMIN))
394 return -EPERM;
395#endif
396
397 if (type >= KSMBD_EVENT_MAX) {
398 WARN_ON(1);
399 return -EINVAL;
400 }
401
402 if (!ksmbd_ipc_validate_version(info))
403 return -EINVAL;
404
405 if (!info->attrs[type])
406 return -EINVAL;
407
408 payload = nla_data(info->attrs[info->genlhdr->cmd]);
409 sz = nla_len(info->attrs[info->genlhdr->cmd]);
410 return handle_response(type, payload, sz);
411}
412
413static int ipc_msg_send(struct ksmbd_ipc_msg *msg)
414{
415 struct genlmsghdr *nlh;
416 struct sk_buff *skb;
417 int ret = -EINVAL;
418
419 if (!ksmbd_tools_pid)
420 return ret;
421
422 skb = genlmsg_new(msg->sz, GFP_KERNEL);
423 if (!skb)
424 return -ENOMEM;
425
426 nlh = genlmsg_put(skb, 0, 0, &ksmbd_genl_family, 0, msg->type);
427 if (!nlh)
428 goto out;
429
Hyunchul Leeb6229482021-06-25 07:02:06 +0900430 ret = nla_put(skb, msg->type, msg->sz, msg->payload);
Namjae Jeon0626e662021-03-16 13:07:11 +0900431 if (ret) {
432 genlmsg_cancel(skb, nlh);
433 goto out;
434 }
435
436 genlmsg_end(skb, nlh);
437 ret = genlmsg_unicast(&init_net, skb, ksmbd_tools_pid);
438 if (!ret)
439 ipc_update_last_active();
440 return ret;
441
442out:
443 nlmsg_free(skb);
444 return ret;
445}
446
Namjae Jeon64b39f42021-03-30 14:25:35 +0900447static void *ipc_msg_send_request(struct ksmbd_ipc_msg *msg, unsigned int handle)
Namjae Jeon0626e662021-03-16 13:07:11 +0900448{
449 struct ipc_msg_table_entry entry;
450 int ret;
451
452 if ((int)handle < 0)
453 return NULL;
454
455 entry.type = msg->type;
456 entry.response = NULL;
457 init_waitqueue_head(&entry.wait);
458
459 down_write(&ipc_msg_table_lock);
460 entry.handle = handle;
461 hash_add(ipc_msg_table, &entry.ipc_table_hlist, entry.handle);
462 up_write(&ipc_msg_table_lock);
463
464 ret = ipc_msg_send(msg);
465 if (ret)
466 goto out;
467
468 ret = wait_event_interruptible_timeout(entry.wait,
469 entry.response != NULL,
470 IPC_WAIT_TIMEOUT);
471out:
472 down_write(&ipc_msg_table_lock);
473 hash_del(&entry.ipc_table_hlist);
474 up_write(&ipc_msg_table_lock);
475 return entry.response;
476}
477
478static int ksmbd_ipc_heartbeat_request(void)
479{
480 struct ksmbd_ipc_msg *msg;
481 int ret;
482
483 msg = ipc_msg_alloc(sizeof(struct ksmbd_heartbeat));
484 if (!msg)
485 return -EINVAL;
486
487 msg->type = KSMBD_EVENT_HEARTBEAT_REQUEST;
488 ret = ipc_msg_send(msg);
489 ipc_msg_free(msg);
490 return ret;
491}
492
493struct ksmbd_login_response *ksmbd_ipc_login_request(const char *account)
494{
495 struct ksmbd_ipc_msg *msg;
496 struct ksmbd_login_request *req;
497 struct ksmbd_login_response *resp;
498
499 if (strlen(account) >= KSMBD_REQ_MAX_ACCOUNT_NAME_SZ)
500 return NULL;
501
502 msg = ipc_msg_alloc(sizeof(struct ksmbd_login_request));
503 if (!msg)
504 return NULL;
505
506 msg->type = KSMBD_EVENT_LOGIN_REQUEST;
Hyunchul Leeb6229482021-06-25 07:02:06 +0900507 req = (struct ksmbd_login_request *)msg->payload;
Namjae Jeond40012a2021-04-13 13:06:30 +0900508 req->handle = ksmbd_acquire_id(&ipc_ida);
Namjae Jeon0626e662021-03-16 13:07:11 +0900509 strscpy(req->account, account, KSMBD_REQ_MAX_ACCOUNT_NAME_SZ);
510
511 resp = ipc_msg_send_request(msg, req->handle);
512 ipc_msg_handle_free(req->handle);
513 ipc_msg_free(msg);
514 return resp;
515}
516
517struct ksmbd_spnego_authen_response *
518ksmbd_ipc_spnego_authen_request(const char *spnego_blob, int blob_len)
519{
520 struct ksmbd_ipc_msg *msg;
521 struct ksmbd_spnego_authen_request *req;
522 struct ksmbd_spnego_authen_response *resp;
523
524 msg = ipc_msg_alloc(sizeof(struct ksmbd_spnego_authen_request) +
525 blob_len + 1);
526 if (!msg)
527 return NULL;
528
529 msg->type = KSMBD_EVENT_SPNEGO_AUTHEN_REQUEST;
Hyunchul Leeb6229482021-06-25 07:02:06 +0900530 req = (struct ksmbd_spnego_authen_request *)msg->payload;
Namjae Jeond40012a2021-04-13 13:06:30 +0900531 req->handle = ksmbd_acquire_id(&ipc_ida);
Namjae Jeon0626e662021-03-16 13:07:11 +0900532 req->spnego_blob_len = blob_len;
533 memcpy(req->spnego_blob, spnego_blob, blob_len);
534
535 resp = ipc_msg_send_request(msg, req->handle);
536 ipc_msg_handle_free(req->handle);
537 ipc_msg_free(msg);
538 return resp;
539}
540
541struct ksmbd_tree_connect_response *
542ksmbd_ipc_tree_connect_request(struct ksmbd_session *sess,
Namjae Jeon070fb212021-05-26 17:57:12 +0900543 struct ksmbd_share_config *share,
544 struct ksmbd_tree_connect *tree_conn,
545 struct sockaddr *peer_addr)
Namjae Jeon0626e662021-03-16 13:07:11 +0900546{
547 struct ksmbd_ipc_msg *msg;
548 struct ksmbd_tree_connect_request *req;
549 struct ksmbd_tree_connect_response *resp;
550
551 if (strlen(user_name(sess->user)) >= KSMBD_REQ_MAX_ACCOUNT_NAME_SZ)
552 return NULL;
553
554 if (strlen(share->name) >= KSMBD_REQ_MAX_SHARE_NAME)
555 return NULL;
556
557 msg = ipc_msg_alloc(sizeof(struct ksmbd_tree_connect_request));
558 if (!msg)
559 return NULL;
560
561 msg->type = KSMBD_EVENT_TREE_CONNECT_REQUEST;
Hyunchul Leeb6229482021-06-25 07:02:06 +0900562 req = (struct ksmbd_tree_connect_request *)msg->payload;
Namjae Jeon0626e662021-03-16 13:07:11 +0900563
Namjae Jeond40012a2021-04-13 13:06:30 +0900564 req->handle = ksmbd_acquire_id(&ipc_ida);
Namjae Jeon0626e662021-03-16 13:07:11 +0900565 req->account_flags = sess->user->flags;
566 req->session_id = sess->id;
567 req->connect_id = tree_conn->id;
568 strscpy(req->account, user_name(sess->user), KSMBD_REQ_MAX_ACCOUNT_NAME_SZ);
569 strscpy(req->share, share->name, KSMBD_REQ_MAX_SHARE_NAME);
570 snprintf(req->peer_addr, sizeof(req->peer_addr), "%pIS", peer_addr);
571
572 if (peer_addr->sa_family == AF_INET6)
573 req->flags |= KSMBD_TREE_CONN_FLAG_REQUEST_IPV6;
574 if (test_session_flag(sess, CIFDS_SESSION_FLAG_SMB2))
575 req->flags |= KSMBD_TREE_CONN_FLAG_REQUEST_SMB2;
576
577 resp = ipc_msg_send_request(msg, req->handle);
578 ipc_msg_handle_free(req->handle);
579 ipc_msg_free(msg);
580 return resp;
581}
582
583int ksmbd_ipc_tree_disconnect_request(unsigned long long session_id,
Namjae Jeon070fb212021-05-26 17:57:12 +0900584 unsigned long long connect_id)
Namjae Jeon0626e662021-03-16 13:07:11 +0900585{
586 struct ksmbd_ipc_msg *msg;
587 struct ksmbd_tree_disconnect_request *req;
588 int ret;
589
590 msg = ipc_msg_alloc(sizeof(struct ksmbd_tree_disconnect_request));
591 if (!msg)
592 return -ENOMEM;
593
594 msg->type = KSMBD_EVENT_TREE_DISCONNECT_REQUEST;
Hyunchul Leeb6229482021-06-25 07:02:06 +0900595 req = (struct ksmbd_tree_disconnect_request *)msg->payload;
Namjae Jeon0626e662021-03-16 13:07:11 +0900596 req->session_id = session_id;
597 req->connect_id = connect_id;
598
599 ret = ipc_msg_send(msg);
600 ipc_msg_free(msg);
601 return ret;
602}
603
Namjae Jeon621be842021-10-13 17:28:31 +0900604int ksmbd_ipc_logout_request(const char *account, int flags)
Namjae Jeon0626e662021-03-16 13:07:11 +0900605{
606 struct ksmbd_ipc_msg *msg;
607 struct ksmbd_logout_request *req;
608 int ret;
609
610 if (strlen(account) >= KSMBD_REQ_MAX_ACCOUNT_NAME_SZ)
611 return -EINVAL;
612
613 msg = ipc_msg_alloc(sizeof(struct ksmbd_logout_request));
614 if (!msg)
615 return -ENOMEM;
616
617 msg->type = KSMBD_EVENT_LOGOUT_REQUEST;
Hyunchul Leeb6229482021-06-25 07:02:06 +0900618 req = (struct ksmbd_logout_request *)msg->payload;
Namjae Jeon621be842021-10-13 17:28:31 +0900619 req->account_flags = flags;
Namjae Jeon0626e662021-03-16 13:07:11 +0900620 strscpy(req->account, account, KSMBD_REQ_MAX_ACCOUNT_NAME_SZ);
621
622 ret = ipc_msg_send(msg);
623 ipc_msg_free(msg);
624 return ret;
625}
626
627struct ksmbd_share_config_response *
628ksmbd_ipc_share_config_request(const char *name)
629{
630 struct ksmbd_ipc_msg *msg;
631 struct ksmbd_share_config_request *req;
632 struct ksmbd_share_config_response *resp;
633
634 if (strlen(name) >= KSMBD_REQ_MAX_SHARE_NAME)
635 return NULL;
636
637 msg = ipc_msg_alloc(sizeof(struct ksmbd_share_config_request));
638 if (!msg)
639 return NULL;
640
641 msg->type = KSMBD_EVENT_SHARE_CONFIG_REQUEST;
Hyunchul Leeb6229482021-06-25 07:02:06 +0900642 req = (struct ksmbd_share_config_request *)msg->payload;
Namjae Jeond40012a2021-04-13 13:06:30 +0900643 req->handle = ksmbd_acquire_id(&ipc_ida);
Namjae Jeon0626e662021-03-16 13:07:11 +0900644 strscpy(req->share_name, name, KSMBD_REQ_MAX_SHARE_NAME);
645
646 resp = ipc_msg_send_request(msg, req->handle);
647 ipc_msg_handle_free(req->handle);
648 ipc_msg_free(msg);
649 return resp;
650}
651
Namjae Jeon64b39f42021-03-30 14:25:35 +0900652struct ksmbd_rpc_command *ksmbd_rpc_open(struct ksmbd_session *sess, int handle)
Namjae Jeon0626e662021-03-16 13:07:11 +0900653{
654 struct ksmbd_ipc_msg *msg;
655 struct ksmbd_rpc_command *req;
656 struct ksmbd_rpc_command *resp;
657
658 msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command));
659 if (!msg)
660 return NULL;
661
662 msg->type = KSMBD_EVENT_RPC_REQUEST;
Hyunchul Leeb6229482021-06-25 07:02:06 +0900663 req = (struct ksmbd_rpc_command *)msg->payload;
Namjae Jeon0626e662021-03-16 13:07:11 +0900664 req->handle = handle;
665 req->flags = ksmbd_session_rpc_method(sess, handle);
666 req->flags |= KSMBD_RPC_OPEN_METHOD;
667 req->payload_sz = 0;
668
669 resp = ipc_msg_send_request(msg, req->handle);
670 ipc_msg_free(msg);
671 return resp;
672}
673
Namjae Jeon64b39f42021-03-30 14:25:35 +0900674struct ksmbd_rpc_command *ksmbd_rpc_close(struct ksmbd_session *sess, int handle)
Namjae Jeon0626e662021-03-16 13:07:11 +0900675{
676 struct ksmbd_ipc_msg *msg;
677 struct ksmbd_rpc_command *req;
678 struct ksmbd_rpc_command *resp;
679
680 msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command));
681 if (!msg)
682 return NULL;
683
684 msg->type = KSMBD_EVENT_RPC_REQUEST;
Hyunchul Leeb6229482021-06-25 07:02:06 +0900685 req = (struct ksmbd_rpc_command *)msg->payload;
Namjae Jeon0626e662021-03-16 13:07:11 +0900686 req->handle = handle;
687 req->flags = ksmbd_session_rpc_method(sess, handle);
688 req->flags |= KSMBD_RPC_CLOSE_METHOD;
689 req->payload_sz = 0;
690
691 resp = ipc_msg_send_request(msg, req->handle);
692 ipc_msg_free(msg);
693 return resp;
694}
695
Namjae Jeon64b39f42021-03-30 14:25:35 +0900696struct ksmbd_rpc_command *ksmbd_rpc_write(struct ksmbd_session *sess, int handle,
Namjae Jeon070fb212021-05-26 17:57:12 +0900697 void *payload, size_t payload_sz)
Namjae Jeon0626e662021-03-16 13:07:11 +0900698{
699 struct ksmbd_ipc_msg *msg;
700 struct ksmbd_rpc_command *req;
701 struct ksmbd_rpc_command *resp;
702
703 msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command) + payload_sz + 1);
704 if (!msg)
705 return NULL;
706
707 msg->type = KSMBD_EVENT_RPC_REQUEST;
Hyunchul Leeb6229482021-06-25 07:02:06 +0900708 req = (struct ksmbd_rpc_command *)msg->payload;
Namjae Jeon0626e662021-03-16 13:07:11 +0900709 req->handle = handle;
710 req->flags = ksmbd_session_rpc_method(sess, handle);
711 req->flags |= rpc_context_flags(sess);
712 req->flags |= KSMBD_RPC_WRITE_METHOD;
713 req->payload_sz = payload_sz;
714 memcpy(req->payload, payload, payload_sz);
715
716 resp = ipc_msg_send_request(msg, req->handle);
717 ipc_msg_free(msg);
718 return resp;
719}
720
Namjae Jeon64b39f42021-03-30 14:25:35 +0900721struct ksmbd_rpc_command *ksmbd_rpc_read(struct ksmbd_session *sess, int handle)
Namjae Jeon0626e662021-03-16 13:07:11 +0900722{
723 struct ksmbd_ipc_msg *msg;
724 struct ksmbd_rpc_command *req;
725 struct ksmbd_rpc_command *resp;
726
727 msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command));
728 if (!msg)
729 return NULL;
730
731 msg->type = KSMBD_EVENT_RPC_REQUEST;
Hyunchul Leeb6229482021-06-25 07:02:06 +0900732 req = (struct ksmbd_rpc_command *)msg->payload;
Namjae Jeon0626e662021-03-16 13:07:11 +0900733 req->handle = handle;
734 req->flags = ksmbd_session_rpc_method(sess, handle);
735 req->flags |= rpc_context_flags(sess);
736 req->flags |= KSMBD_RPC_READ_METHOD;
737 req->payload_sz = 0;
738
739 resp = ipc_msg_send_request(msg, req->handle);
740 ipc_msg_free(msg);
741 return resp;
742}
743
Namjae Jeon64b39f42021-03-30 14:25:35 +0900744struct ksmbd_rpc_command *ksmbd_rpc_ioctl(struct ksmbd_session *sess, int handle,
Namjae Jeon070fb212021-05-26 17:57:12 +0900745 void *payload, size_t payload_sz)
Namjae Jeon0626e662021-03-16 13:07:11 +0900746{
747 struct ksmbd_ipc_msg *msg;
748 struct ksmbd_rpc_command *req;
749 struct ksmbd_rpc_command *resp;
750
751 msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command) + payload_sz + 1);
752 if (!msg)
753 return NULL;
754
755 msg->type = KSMBD_EVENT_RPC_REQUEST;
Namjae Jeon690f9692021-07-03 08:00:06 +0900756 req = (struct ksmbd_rpc_command *)msg->payload;
Namjae Jeon0626e662021-03-16 13:07:11 +0900757 req->handle = handle;
758 req->flags = ksmbd_session_rpc_method(sess, handle);
759 req->flags |= rpc_context_flags(sess);
760 req->flags |= KSMBD_RPC_IOCTL_METHOD;
761 req->payload_sz = payload_sz;
762 memcpy(req->payload, payload, payload_sz);
763
764 resp = ipc_msg_send_request(msg, req->handle);
765 ipc_msg_free(msg);
766 return resp;
767}
768
Namjae Jeon64b39f42021-03-30 14:25:35 +0900769struct ksmbd_rpc_command *ksmbd_rpc_rap(struct ksmbd_session *sess, void *payload,
Namjae Jeon070fb212021-05-26 17:57:12 +0900770 size_t payload_sz)
Namjae Jeon0626e662021-03-16 13:07:11 +0900771{
772 struct ksmbd_ipc_msg *msg;
773 struct ksmbd_rpc_command *req;
774 struct ksmbd_rpc_command *resp;
775
776 msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command) + payload_sz + 1);
777 if (!msg)
778 return NULL;
779
780 msg->type = KSMBD_EVENT_RPC_REQUEST;
Namjae Jeon690f9692021-07-03 08:00:06 +0900781 req = (struct ksmbd_rpc_command *)msg->payload;
Namjae Jeond40012a2021-04-13 13:06:30 +0900782 req->handle = ksmbd_acquire_id(&ipc_ida);
Namjae Jeon0626e662021-03-16 13:07:11 +0900783 req->flags = rpc_context_flags(sess);
784 req->flags |= KSMBD_RPC_RAP_METHOD;
785 req->payload_sz = payload_sz;
786 memcpy(req->payload, payload, payload_sz);
787
788 resp = ipc_msg_send_request(msg, req->handle);
789 ipc_msg_handle_free(req->handle);
790 ipc_msg_free(msg);
791 return resp;
792}
793
794static int __ipc_heartbeat(void)
795{
796 unsigned long delta;
797
798 if (!ksmbd_server_running())
799 return 0;
800
801 if (time_after(jiffies, server_conf.ipc_last_active)) {
802 delta = (jiffies - server_conf.ipc_last_active);
803 } else {
804 ipc_update_last_active();
805 schedule_delayed_work(&ipc_timer_work,
806 server_conf.ipc_timeout);
807 return 0;
808 }
809
810 if (delta < server_conf.ipc_timeout) {
811 schedule_delayed_work(&ipc_timer_work,
812 server_conf.ipc_timeout - delta);
813 return 0;
814 }
815
816 if (ksmbd_ipc_heartbeat_request() == 0) {
817 schedule_delayed_work(&ipc_timer_work,
818 server_conf.ipc_timeout);
819 return 0;
820 }
821
822 mutex_lock(&startup_lock);
823 WRITE_ONCE(server_conf.state, SERVER_STATE_RESETTING);
824 server_conf.ipc_last_active = 0;
825 ksmbd_tools_pid = 0;
Namjae Jeonbde16942021-06-28 15:23:19 +0900826 pr_err("No IPC daemon response for %lus\n", delta / HZ);
Namjae Jeon0626e662021-03-16 13:07:11 +0900827 mutex_unlock(&startup_lock);
828 return -EINVAL;
829}
830
831static void ipc_timer_heartbeat(struct work_struct *w)
832{
833 if (__ipc_heartbeat())
834 server_queue_ctrl_reset_work();
835}
836
837int ksmbd_ipc_id_alloc(void)
838{
Namjae Jeond40012a2021-04-13 13:06:30 +0900839 return ksmbd_acquire_id(&ipc_ida);
Namjae Jeon0626e662021-03-16 13:07:11 +0900840}
841
842void ksmbd_rpc_id_free(int handle)
843{
Namjae Jeond40012a2021-04-13 13:06:30 +0900844 ksmbd_release_id(&ipc_ida, handle);
Namjae Jeon0626e662021-03-16 13:07:11 +0900845}
846
847void ksmbd_ipc_release(void)
848{
849 cancel_delayed_work_sync(&ipc_timer_work);
Namjae Jeon0626e662021-03-16 13:07:11 +0900850 genl_unregister_family(&ksmbd_genl_family);
851}
852
853void ksmbd_ipc_soft_reset(void)
854{
855 mutex_lock(&startup_lock);
856 ksmbd_tools_pid = 0;
857 cancel_delayed_work_sync(&ipc_timer_work);
858 mutex_unlock(&startup_lock);
859}
860
861int ksmbd_ipc_init(void)
862{
Namjae Jeond40012a2021-04-13 13:06:30 +0900863 int ret = 0;
Namjae Jeon0626e662021-03-16 13:07:11 +0900864
865 ksmbd_nl_init_fixup();
866 INIT_DELAYED_WORK(&ipc_timer_work, ipc_timer_heartbeat);
867
868 ret = genl_register_family(&ksmbd_genl_family);
869 if (ret) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900870 pr_err("Failed to register KSMBD netlink interface %d\n", ret);
Namjae Jeond40012a2021-04-13 13:06:30 +0900871 cancel_delayed_work_sync(&ipc_timer_work);
Namjae Jeon0626e662021-03-16 13:07:11 +0900872 }
873
Dan Carpenter849f59e2021-03-23 16:27:04 +0300874 return ret;
Namjae Jeon0626e662021-03-16 13:07:11 +0900875}