1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
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>
17 #include "vfs_cache.h"
18 #include "transport_ipc.h"
20 #include "smb_common.h"
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 #include "transport_rdma.h"
31 #define IPC_WAIT_TIMEOUT (2 * HZ)
33 #define IPC_MSG_HASH_BITS 3
34 static DEFINE_HASHTABLE(ipc_msg_table, IPC_MSG_HASH_BITS);
35 static DECLARE_RWSEM(ipc_msg_table_lock);
36 static DEFINE_MUTEX(startup_lock);
38 static DEFINE_IDA(ipc_ida);
40 static unsigned int ksmbd_tools_pid;
42 static bool ksmbd_ipc_validate_version(struct genl_info *m)
44 if (m->genlhdr->version != KSMBD_GENL_VERSION) {
45 pr_err("%s. ksmbd: %d, kernel module: %d. %s.\n",
46 "Daemon and kernel module version mismatch",
49 "User-space ksmbd should terminate");
55 struct ksmbd_ipc_msg {
58 unsigned char payload[];
61 struct ipc_msg_table_entry {
64 wait_queue_head_t wait;
65 struct hlist_node ipc_table_hlist;
71 static struct delayed_work ipc_timer_work;
73 static int handle_startup_event(struct sk_buff *skb, struct genl_info *info);
74 static int handle_unsupported_event(struct sk_buff *skb, struct genl_info *info);
75 static int handle_generic_event(struct sk_buff *skb, struct genl_info *info);
76 static int ksmbd_ipc_heartbeat_request(void);
78 static const struct nla_policy ksmbd_nl_policy[KSMBD_EVENT_MAX + 1] = {
79 [KSMBD_EVENT_UNSPEC] = {
82 [KSMBD_EVENT_HEARTBEAT_REQUEST] = {
83 .len = sizeof(struct ksmbd_heartbeat),
85 [KSMBD_EVENT_STARTING_UP] = {
86 .len = sizeof(struct ksmbd_startup_request),
88 [KSMBD_EVENT_SHUTTING_DOWN] = {
89 .len = sizeof(struct ksmbd_shutdown_request),
91 [KSMBD_EVENT_LOGIN_REQUEST] = {
92 .len = sizeof(struct ksmbd_login_request),
94 [KSMBD_EVENT_LOGIN_RESPONSE] = {
95 .len = sizeof(struct ksmbd_login_response),
97 [KSMBD_EVENT_SHARE_CONFIG_REQUEST] = {
98 .len = sizeof(struct ksmbd_share_config_request),
100 [KSMBD_EVENT_SHARE_CONFIG_RESPONSE] = {
101 .len = sizeof(struct ksmbd_share_config_response),
103 [KSMBD_EVENT_TREE_CONNECT_REQUEST] = {
104 .len = sizeof(struct ksmbd_tree_connect_request),
106 [KSMBD_EVENT_TREE_CONNECT_RESPONSE] = {
107 .len = sizeof(struct ksmbd_tree_connect_response),
109 [KSMBD_EVENT_TREE_DISCONNECT_REQUEST] = {
110 .len = sizeof(struct ksmbd_tree_disconnect_request),
112 [KSMBD_EVENT_LOGOUT_REQUEST] = {
113 .len = sizeof(struct ksmbd_logout_request),
115 [KSMBD_EVENT_RPC_REQUEST] = {
117 [KSMBD_EVENT_RPC_RESPONSE] = {
119 [KSMBD_EVENT_SPNEGO_AUTHEN_REQUEST] = {
121 [KSMBD_EVENT_SPNEGO_AUTHEN_RESPONSE] = {
125 static struct genl_ops ksmbd_genl_ops[] = {
127 .cmd = KSMBD_EVENT_UNSPEC,
128 .doit = handle_unsupported_event,
131 .cmd = KSMBD_EVENT_HEARTBEAT_REQUEST,
132 .doit = handle_unsupported_event,
135 .cmd = KSMBD_EVENT_STARTING_UP,
136 .doit = handle_startup_event,
139 .cmd = KSMBD_EVENT_SHUTTING_DOWN,
140 .doit = handle_unsupported_event,
143 .cmd = KSMBD_EVENT_LOGIN_REQUEST,
144 .doit = handle_unsupported_event,
147 .cmd = KSMBD_EVENT_LOGIN_RESPONSE,
148 .doit = handle_generic_event,
151 .cmd = KSMBD_EVENT_SHARE_CONFIG_REQUEST,
152 .doit = handle_unsupported_event,
155 .cmd = KSMBD_EVENT_SHARE_CONFIG_RESPONSE,
156 .doit = handle_generic_event,
159 .cmd = KSMBD_EVENT_TREE_CONNECT_REQUEST,
160 .doit = handle_unsupported_event,
163 .cmd = KSMBD_EVENT_TREE_CONNECT_RESPONSE,
164 .doit = handle_generic_event,
167 .cmd = KSMBD_EVENT_TREE_DISCONNECT_REQUEST,
168 .doit = handle_unsupported_event,
171 .cmd = KSMBD_EVENT_LOGOUT_REQUEST,
172 .doit = handle_unsupported_event,
175 .cmd = KSMBD_EVENT_RPC_REQUEST,
176 .doit = handle_unsupported_event,
179 .cmd = KSMBD_EVENT_RPC_RESPONSE,
180 .doit = handle_generic_event,
183 .cmd = KSMBD_EVENT_SPNEGO_AUTHEN_REQUEST,
184 .doit = handle_unsupported_event,
187 .cmd = KSMBD_EVENT_SPNEGO_AUTHEN_RESPONSE,
188 .doit = handle_generic_event,
192 static struct genl_family ksmbd_genl_family = {
193 .name = KSMBD_GENL_NAME,
194 .version = KSMBD_GENL_VERSION,
196 .maxattr = KSMBD_EVENT_MAX,
198 .module = THIS_MODULE,
199 .ops = ksmbd_genl_ops,
200 .n_ops = ARRAY_SIZE(ksmbd_genl_ops),
201 .resv_start_op = KSMBD_EVENT_SPNEGO_AUTHEN_RESPONSE + 1,
204 static void ksmbd_nl_init_fixup(void)
208 for (i = 0; i < ARRAY_SIZE(ksmbd_genl_ops); i++)
209 ksmbd_genl_ops[i].validate = GENL_DONT_VALIDATE_STRICT |
210 GENL_DONT_VALIDATE_DUMP;
212 ksmbd_genl_family.policy = ksmbd_nl_policy;
215 static int rpc_context_flags(struct ksmbd_session *sess)
217 if (user_guest(sess->user))
218 return KSMBD_RPC_RESTRICTED_CONTEXT;
222 static void ipc_update_last_active(void)
224 if (server_conf.ipc_timeout)
225 server_conf.ipc_last_active = jiffies;
228 static struct ksmbd_ipc_msg *ipc_msg_alloc(size_t sz)
230 struct ksmbd_ipc_msg *msg;
231 size_t msg_sz = sz + sizeof(struct ksmbd_ipc_msg);
233 msg = kvzalloc(msg_sz, GFP_KERNEL);
239 static void ipc_msg_free(struct ksmbd_ipc_msg *msg)
244 static void ipc_msg_handle_free(int handle)
247 ksmbd_release_id(&ipc_ida, handle);
250 static int handle_response(int type, void *payload, size_t sz)
252 unsigned int handle = *(unsigned int *)payload;
253 struct ipc_msg_table_entry *entry;
256 ipc_update_last_active();
257 down_read(&ipc_msg_table_lock);
258 hash_for_each_possible(ipc_msg_table, entry, ipc_table_hlist, handle) {
259 if (handle != entry->handle)
262 entry->response = NULL;
264 * Response message type value should be equal to
265 * request message type + 1.
267 if (entry->type + 1 != type) {
268 pr_err("Waiting for IPC type %d, got %d. Ignore.\n",
269 entry->type + 1, type);
272 entry->response = kvzalloc(sz, GFP_KERNEL);
273 if (!entry->response) {
278 memcpy(entry->response, payload, sz);
280 wake_up_interruptible(&entry->wait);
284 up_read(&ipc_msg_table_lock);
289 static int ipc_server_config_on_startup(struct ksmbd_startup_request *req)
293 ksmbd_set_fd_limit(req->file_max);
294 server_conf.flags = req->flags;
295 server_conf.signing = req->signing;
296 server_conf.tcp_port = req->tcp_port;
297 server_conf.ipc_timeout = req->ipc_timeout * HZ;
298 server_conf.deadtime = req->deadtime * SMB_ECHO_INTERVAL;
299 server_conf.share_fake_fscaps = req->share_fake_fscaps;
300 ksmbd_init_domain(req->sub_auth);
302 if (req->smb2_max_read)
303 init_smb2_max_read_size(req->smb2_max_read);
304 if (req->smb2_max_write)
305 init_smb2_max_write_size(req->smb2_max_write);
306 if (req->smb2_max_trans)
307 init_smb2_max_trans_size(req->smb2_max_trans);
308 if (req->smb2_max_credits)
309 init_smb2_max_credits(req->smb2_max_credits);
310 if (req->smbd_max_io_size)
311 init_smbd_max_io_size(req->smbd_max_io_size);
313 if (req->max_connections)
314 server_conf.max_connections = req->max_connections;
316 ret = ksmbd_set_netbios_name(req->netbios_name);
317 ret |= ksmbd_set_server_string(req->server_string);
318 ret |= ksmbd_set_work_group(req->work_group);
319 ret |= ksmbd_tcp_set_interfaces(KSMBD_STARTUP_CONFIG_INTERFACES(req),
322 pr_err("Server configuration error: %s %s %s\n",
323 req->netbios_name, req->server_string,
328 if (req->min_prot[0]) {
329 ret = ksmbd_lookup_protocol_idx(req->min_prot);
331 server_conf.min_protocol = ret;
333 if (req->max_prot[0]) {
334 ret = ksmbd_lookup_protocol_idx(req->max_prot);
336 server_conf.max_protocol = ret;
339 if (server_conf.ipc_timeout)
340 schedule_delayed_work(&ipc_timer_work, server_conf.ipc_timeout);
344 static int handle_startup_event(struct sk_buff *skb, struct genl_info *info)
348 #ifdef CONFIG_SMB_SERVER_CHECK_CAP_NET_ADMIN
349 if (!netlink_capable(skb, CAP_NET_ADMIN))
353 if (!ksmbd_ipc_validate_version(info))
356 if (!info->attrs[KSMBD_EVENT_STARTING_UP])
359 mutex_lock(&startup_lock);
360 if (!ksmbd_server_configurable()) {
361 mutex_unlock(&startup_lock);
362 pr_err("Server reset is in progress, can't start daemon\n");
366 if (ksmbd_tools_pid) {
367 if (ksmbd_ipc_heartbeat_request() == 0) {
372 pr_err("Reconnect to a new user space daemon\n");
374 struct ksmbd_startup_request *req;
376 req = nla_data(info->attrs[info->genlhdr->cmd]);
377 ret = ipc_server_config_on_startup(req);
380 server_queue_ctrl_init_work();
383 ksmbd_tools_pid = info->snd_portid;
384 ipc_update_last_active();
387 mutex_unlock(&startup_lock);
391 static int handle_unsupported_event(struct sk_buff *skb, struct genl_info *info)
393 pr_err("Unknown IPC event: %d, ignore.\n", info->genlhdr->cmd);
397 static int handle_generic_event(struct sk_buff *skb, struct genl_info *info)
401 int type = info->genlhdr->cmd;
403 #ifdef CONFIG_SMB_SERVER_CHECK_CAP_NET_ADMIN
404 if (!netlink_capable(skb, CAP_NET_ADMIN))
408 if (type > KSMBD_EVENT_MAX) {
413 if (!ksmbd_ipc_validate_version(info))
416 if (!info->attrs[type])
419 payload = nla_data(info->attrs[info->genlhdr->cmd]);
420 sz = nla_len(info->attrs[info->genlhdr->cmd]);
421 return handle_response(type, payload, sz);
424 static int ipc_msg_send(struct ksmbd_ipc_msg *msg)
426 struct genlmsghdr *nlh;
430 if (!ksmbd_tools_pid)
433 skb = genlmsg_new(msg->sz, GFP_KERNEL);
437 nlh = genlmsg_put(skb, 0, 0, &ksmbd_genl_family, 0, msg->type);
441 ret = nla_put(skb, msg->type, msg->sz, msg->payload);
443 genlmsg_cancel(skb, nlh);
447 genlmsg_end(skb, nlh);
448 ret = genlmsg_unicast(&init_net, skb, ksmbd_tools_pid);
450 ipc_update_last_active();
458 static int ipc_validate_msg(struct ipc_msg_table_entry *entry)
460 unsigned int msg_sz = entry->msg_sz;
462 if (entry->type == KSMBD_EVENT_RPC_REQUEST) {
463 struct ksmbd_rpc_command *resp = entry->response;
465 msg_sz = sizeof(struct ksmbd_rpc_command) + resp->payload_sz;
466 } else if (entry->type == KSMBD_EVENT_SPNEGO_AUTHEN_REQUEST) {
467 struct ksmbd_spnego_authen_response *resp = entry->response;
469 msg_sz = sizeof(struct ksmbd_spnego_authen_response) +
470 resp->session_key_len + resp->spnego_blob_len;
471 } else if (entry->type == KSMBD_EVENT_SHARE_CONFIG_REQUEST) {
472 struct ksmbd_share_config_response *resp = entry->response;
474 if (resp->payload_sz) {
475 if (resp->payload_sz < resp->veto_list_sz)
478 msg_sz = sizeof(struct ksmbd_share_config_response) +
483 return entry->msg_sz != msg_sz ? -EINVAL : 0;
486 static void *ipc_msg_send_request(struct ksmbd_ipc_msg *msg, unsigned int handle)
488 struct ipc_msg_table_entry entry;
494 entry.type = msg->type;
495 entry.response = NULL;
496 init_waitqueue_head(&entry.wait);
498 down_write(&ipc_msg_table_lock);
499 entry.handle = handle;
500 hash_add(ipc_msg_table, &entry.ipc_table_hlist, entry.handle);
501 up_write(&ipc_msg_table_lock);
503 ret = ipc_msg_send(msg);
507 ret = wait_event_interruptible_timeout(entry.wait,
508 entry.response != NULL,
510 if (entry.response) {
511 ret = ipc_validate_msg(&entry);
513 kvfree(entry.response);
514 entry.response = NULL;
518 down_write(&ipc_msg_table_lock);
519 hash_del(&entry.ipc_table_hlist);
520 up_write(&ipc_msg_table_lock);
521 return entry.response;
524 static int ksmbd_ipc_heartbeat_request(void)
526 struct ksmbd_ipc_msg *msg;
529 msg = ipc_msg_alloc(sizeof(struct ksmbd_heartbeat));
533 msg->type = KSMBD_EVENT_HEARTBEAT_REQUEST;
534 ret = ipc_msg_send(msg);
539 struct ksmbd_login_response *ksmbd_ipc_login_request(const char *account)
541 struct ksmbd_ipc_msg *msg;
542 struct ksmbd_login_request *req;
543 struct ksmbd_login_response *resp;
545 if (strlen(account) >= KSMBD_REQ_MAX_ACCOUNT_NAME_SZ)
548 msg = ipc_msg_alloc(sizeof(struct ksmbd_login_request));
552 msg->type = KSMBD_EVENT_LOGIN_REQUEST;
553 req = (struct ksmbd_login_request *)msg->payload;
554 req->handle = ksmbd_acquire_id(&ipc_ida);
555 strscpy(req->account, account, KSMBD_REQ_MAX_ACCOUNT_NAME_SZ);
557 resp = ipc_msg_send_request(msg, req->handle);
558 ipc_msg_handle_free(req->handle);
563 struct ksmbd_spnego_authen_response *
564 ksmbd_ipc_spnego_authen_request(const char *spnego_blob, int blob_len)
566 struct ksmbd_ipc_msg *msg;
567 struct ksmbd_spnego_authen_request *req;
568 struct ksmbd_spnego_authen_response *resp;
570 msg = ipc_msg_alloc(sizeof(struct ksmbd_spnego_authen_request) +
575 msg->type = KSMBD_EVENT_SPNEGO_AUTHEN_REQUEST;
576 req = (struct ksmbd_spnego_authen_request *)msg->payload;
577 req->handle = ksmbd_acquire_id(&ipc_ida);
578 req->spnego_blob_len = blob_len;
579 memcpy(req->spnego_blob, spnego_blob, blob_len);
581 resp = ipc_msg_send_request(msg, req->handle);
582 ipc_msg_handle_free(req->handle);
587 struct ksmbd_tree_connect_response *
588 ksmbd_ipc_tree_connect_request(struct ksmbd_session *sess,
589 struct ksmbd_share_config *share,
590 struct ksmbd_tree_connect *tree_conn,
591 struct sockaddr *peer_addr)
593 struct ksmbd_ipc_msg *msg;
594 struct ksmbd_tree_connect_request *req;
595 struct ksmbd_tree_connect_response *resp;
597 if (strlen(user_name(sess->user)) >= KSMBD_REQ_MAX_ACCOUNT_NAME_SZ)
600 if (strlen(share->name) >= KSMBD_REQ_MAX_SHARE_NAME)
603 msg = ipc_msg_alloc(sizeof(struct ksmbd_tree_connect_request));
607 msg->type = KSMBD_EVENT_TREE_CONNECT_REQUEST;
608 req = (struct ksmbd_tree_connect_request *)msg->payload;
610 req->handle = ksmbd_acquire_id(&ipc_ida);
611 req->account_flags = sess->user->flags;
612 req->session_id = sess->id;
613 req->connect_id = tree_conn->id;
614 strscpy(req->account, user_name(sess->user), KSMBD_REQ_MAX_ACCOUNT_NAME_SZ);
615 strscpy(req->share, share->name, KSMBD_REQ_MAX_SHARE_NAME);
616 snprintf(req->peer_addr, sizeof(req->peer_addr), "%pIS", peer_addr);
618 if (peer_addr->sa_family == AF_INET6)
619 req->flags |= KSMBD_TREE_CONN_FLAG_REQUEST_IPV6;
620 if (test_session_flag(sess, CIFDS_SESSION_FLAG_SMB2))
621 req->flags |= KSMBD_TREE_CONN_FLAG_REQUEST_SMB2;
623 resp = ipc_msg_send_request(msg, req->handle);
624 ipc_msg_handle_free(req->handle);
629 int ksmbd_ipc_tree_disconnect_request(unsigned long long session_id,
630 unsigned long long connect_id)
632 struct ksmbd_ipc_msg *msg;
633 struct ksmbd_tree_disconnect_request *req;
636 msg = ipc_msg_alloc(sizeof(struct ksmbd_tree_disconnect_request));
640 msg->type = KSMBD_EVENT_TREE_DISCONNECT_REQUEST;
641 req = (struct ksmbd_tree_disconnect_request *)msg->payload;
642 req->session_id = session_id;
643 req->connect_id = connect_id;
645 ret = ipc_msg_send(msg);
650 int ksmbd_ipc_logout_request(const char *account, int flags)
652 struct ksmbd_ipc_msg *msg;
653 struct ksmbd_logout_request *req;
656 if (strlen(account) >= KSMBD_REQ_MAX_ACCOUNT_NAME_SZ)
659 msg = ipc_msg_alloc(sizeof(struct ksmbd_logout_request));
663 msg->type = KSMBD_EVENT_LOGOUT_REQUEST;
664 req = (struct ksmbd_logout_request *)msg->payload;
665 req->account_flags = flags;
666 strscpy(req->account, account, KSMBD_REQ_MAX_ACCOUNT_NAME_SZ);
668 ret = ipc_msg_send(msg);
673 struct ksmbd_share_config_response *
674 ksmbd_ipc_share_config_request(const char *name)
676 struct ksmbd_ipc_msg *msg;
677 struct ksmbd_share_config_request *req;
678 struct ksmbd_share_config_response *resp;
680 if (strlen(name) >= KSMBD_REQ_MAX_SHARE_NAME)
683 msg = ipc_msg_alloc(sizeof(struct ksmbd_share_config_request));
687 msg->type = KSMBD_EVENT_SHARE_CONFIG_REQUEST;
688 req = (struct ksmbd_share_config_request *)msg->payload;
689 req->handle = ksmbd_acquire_id(&ipc_ida);
690 strscpy(req->share_name, name, KSMBD_REQ_MAX_SHARE_NAME);
692 resp = ipc_msg_send_request(msg, req->handle);
693 ipc_msg_handle_free(req->handle);
698 struct ksmbd_rpc_command *ksmbd_rpc_open(struct ksmbd_session *sess, int handle)
700 struct ksmbd_ipc_msg *msg;
701 struct ksmbd_rpc_command *req;
702 struct ksmbd_rpc_command *resp;
704 msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command));
708 msg->type = KSMBD_EVENT_RPC_REQUEST;
709 req = (struct ksmbd_rpc_command *)msg->payload;
710 req->handle = handle;
711 req->flags = ksmbd_session_rpc_method(sess, handle);
712 req->flags |= KSMBD_RPC_OPEN_METHOD;
715 resp = ipc_msg_send_request(msg, req->handle);
720 struct ksmbd_rpc_command *ksmbd_rpc_close(struct ksmbd_session *sess, int handle)
722 struct ksmbd_ipc_msg *msg;
723 struct ksmbd_rpc_command *req;
724 struct ksmbd_rpc_command *resp;
726 msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command));
730 msg->type = KSMBD_EVENT_RPC_REQUEST;
731 req = (struct ksmbd_rpc_command *)msg->payload;
732 req->handle = handle;
733 req->flags = ksmbd_session_rpc_method(sess, handle);
734 req->flags |= KSMBD_RPC_CLOSE_METHOD;
737 resp = ipc_msg_send_request(msg, req->handle);
742 struct ksmbd_rpc_command *ksmbd_rpc_write(struct ksmbd_session *sess, int handle,
743 void *payload, size_t payload_sz)
745 struct ksmbd_ipc_msg *msg;
746 struct ksmbd_rpc_command *req;
747 struct ksmbd_rpc_command *resp;
749 msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command) + payload_sz + 1);
753 msg->type = KSMBD_EVENT_RPC_REQUEST;
754 req = (struct ksmbd_rpc_command *)msg->payload;
755 req->handle = handle;
756 req->flags = ksmbd_session_rpc_method(sess, handle);
757 req->flags |= rpc_context_flags(sess);
758 req->flags |= KSMBD_RPC_WRITE_METHOD;
759 req->payload_sz = payload_sz;
760 memcpy(req->payload, payload, payload_sz);
762 resp = ipc_msg_send_request(msg, req->handle);
767 struct ksmbd_rpc_command *ksmbd_rpc_read(struct ksmbd_session *sess, int handle)
769 struct ksmbd_ipc_msg *msg;
770 struct ksmbd_rpc_command *req;
771 struct ksmbd_rpc_command *resp;
773 msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command));
777 msg->type = KSMBD_EVENT_RPC_REQUEST;
778 req = (struct ksmbd_rpc_command *)msg->payload;
779 req->handle = handle;
780 req->flags = ksmbd_session_rpc_method(sess, handle);
781 req->flags |= rpc_context_flags(sess);
782 req->flags |= KSMBD_RPC_READ_METHOD;
785 resp = ipc_msg_send_request(msg, req->handle);
790 struct ksmbd_rpc_command *ksmbd_rpc_ioctl(struct ksmbd_session *sess, int handle,
791 void *payload, size_t payload_sz)
793 struct ksmbd_ipc_msg *msg;
794 struct ksmbd_rpc_command *req;
795 struct ksmbd_rpc_command *resp;
797 msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command) + payload_sz + 1);
801 msg->type = KSMBD_EVENT_RPC_REQUEST;
802 req = (struct ksmbd_rpc_command *)msg->payload;
803 req->handle = handle;
804 req->flags = ksmbd_session_rpc_method(sess, handle);
805 req->flags |= rpc_context_flags(sess);
806 req->flags |= KSMBD_RPC_IOCTL_METHOD;
807 req->payload_sz = payload_sz;
808 memcpy(req->payload, payload, payload_sz);
810 resp = ipc_msg_send_request(msg, req->handle);
815 struct ksmbd_rpc_command *ksmbd_rpc_rap(struct ksmbd_session *sess, void *payload,
818 struct ksmbd_ipc_msg *msg;
819 struct ksmbd_rpc_command *req;
820 struct ksmbd_rpc_command *resp;
822 msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command) + payload_sz + 1);
826 msg->type = KSMBD_EVENT_RPC_REQUEST;
827 req = (struct ksmbd_rpc_command *)msg->payload;
828 req->handle = ksmbd_acquire_id(&ipc_ida);
829 req->flags = rpc_context_flags(sess);
830 req->flags |= KSMBD_RPC_RAP_METHOD;
831 req->payload_sz = payload_sz;
832 memcpy(req->payload, payload, payload_sz);
834 resp = ipc_msg_send_request(msg, req->handle);
835 ipc_msg_handle_free(req->handle);
840 static int __ipc_heartbeat(void)
844 if (!ksmbd_server_running())
847 if (time_after(jiffies, server_conf.ipc_last_active)) {
848 delta = (jiffies - server_conf.ipc_last_active);
850 ipc_update_last_active();
851 schedule_delayed_work(&ipc_timer_work,
852 server_conf.ipc_timeout);
856 if (delta < server_conf.ipc_timeout) {
857 schedule_delayed_work(&ipc_timer_work,
858 server_conf.ipc_timeout - delta);
862 if (ksmbd_ipc_heartbeat_request() == 0) {
863 schedule_delayed_work(&ipc_timer_work,
864 server_conf.ipc_timeout);
868 mutex_lock(&startup_lock);
869 WRITE_ONCE(server_conf.state, SERVER_STATE_RESETTING);
870 server_conf.ipc_last_active = 0;
872 pr_err("No IPC daemon response for %lus\n", delta / HZ);
873 mutex_unlock(&startup_lock);
877 static void ipc_timer_heartbeat(struct work_struct *w)
879 if (__ipc_heartbeat())
880 server_queue_ctrl_reset_work();
883 int ksmbd_ipc_id_alloc(void)
885 return ksmbd_acquire_id(&ipc_ida);
888 void ksmbd_rpc_id_free(int handle)
890 ksmbd_release_id(&ipc_ida, handle);
893 void ksmbd_ipc_release(void)
895 cancel_delayed_work_sync(&ipc_timer_work);
896 genl_unregister_family(&ksmbd_genl_family);
899 void ksmbd_ipc_soft_reset(void)
901 mutex_lock(&startup_lock);
903 cancel_delayed_work_sync(&ipc_timer_work);
904 mutex_unlock(&startup_lock);
907 int ksmbd_ipc_init(void)
911 ksmbd_nl_init_fixup();
912 INIT_DELAYED_WORK(&ipc_timer_work, ipc_timer_heartbeat);
914 ret = genl_register_family(&ksmbd_genl_family);
916 pr_err("Failed to register KSMBD netlink interface %d\n", ret);
917 cancel_delayed_work_sync(&ipc_timer_work);