1 // 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"
19 #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 #include "transport_rdma.h"
30
31 #define IPC_WAIT_TIMEOUT (2 * HZ)
32
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);
37
38 static DEFINE_IDA(ipc_ida);
39
40 static unsigned int ksmbd_tools_pid;
41
ksmbd_ipc_validate_version(struct genl_info * m)42 static bool ksmbd_ipc_validate_version(struct genl_info *m)
43 {
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",
47 m->genlhdr->version,
48 KSMBD_GENL_VERSION,
49 "User-space ksmbd should terminate");
50 return false;
51 }
52 return true;
53 }
54
55 struct ksmbd_ipc_msg {
56 unsigned int type;
57 unsigned int sz;
58 unsigned char payload[];
59 };
60
61 struct ipc_msg_table_entry {
62 unsigned int handle;
63 unsigned int type;
64 wait_queue_head_t wait;
65 struct hlist_node ipc_table_hlist;
66
67 void *response;
68 };
69
70 static struct delayed_work ipc_timer_work;
71
72 static int handle_startup_event(struct sk_buff *skb, struct genl_info *info);
73 static int handle_unsupported_event(struct sk_buff *skb, struct genl_info *info);
74 static int handle_generic_event(struct sk_buff *skb, struct genl_info *info);
75 static int ksmbd_ipc_heartbeat_request(void);
76
77 static const struct nla_policy ksmbd_nl_policy[KSMBD_EVENT_MAX + 1] = {
78 [KSMBD_EVENT_UNSPEC] = {
79 .len = 0,
80 },
81 [KSMBD_EVENT_HEARTBEAT_REQUEST] = {
82 .len = sizeof(struct ksmbd_heartbeat),
83 },
84 [KSMBD_EVENT_STARTING_UP] = {
85 .len = sizeof(struct ksmbd_startup_request),
86 },
87 [KSMBD_EVENT_SHUTTING_DOWN] = {
88 .len = sizeof(struct ksmbd_shutdown_request),
89 },
90 [KSMBD_EVENT_LOGIN_REQUEST] = {
91 .len = sizeof(struct ksmbd_login_request),
92 },
93 [KSMBD_EVENT_LOGIN_RESPONSE] = {
94 .len = sizeof(struct ksmbd_login_response),
95 },
96 [KSMBD_EVENT_SHARE_CONFIG_REQUEST] = {
97 .len = sizeof(struct ksmbd_share_config_request),
98 },
99 [KSMBD_EVENT_SHARE_CONFIG_RESPONSE] = {
100 .len = sizeof(struct ksmbd_share_config_response),
101 },
102 [KSMBD_EVENT_TREE_CONNECT_REQUEST] = {
103 .len = sizeof(struct ksmbd_tree_connect_request),
104 },
105 [KSMBD_EVENT_TREE_CONNECT_RESPONSE] = {
106 .len = sizeof(struct ksmbd_tree_connect_response),
107 },
108 [KSMBD_EVENT_TREE_DISCONNECT_REQUEST] = {
109 .len = sizeof(struct ksmbd_tree_disconnect_request),
110 },
111 [KSMBD_EVENT_LOGOUT_REQUEST] = {
112 .len = sizeof(struct ksmbd_logout_request),
113 },
114 [KSMBD_EVENT_RPC_REQUEST] = {
115 },
116 [KSMBD_EVENT_RPC_RESPONSE] = {
117 },
118 [KSMBD_EVENT_SPNEGO_AUTHEN_REQUEST] = {
119 },
120 [KSMBD_EVENT_SPNEGO_AUTHEN_RESPONSE] = {
121 },
122 };
123
124 static struct genl_ops ksmbd_genl_ops[] = {
125 {
126 .cmd = KSMBD_EVENT_UNSPEC,
127 .doit = handle_unsupported_event,
128 },
129 {
130 .cmd = KSMBD_EVENT_HEARTBEAT_REQUEST,
131 .doit = handle_unsupported_event,
132 },
133 {
134 .cmd = KSMBD_EVENT_STARTING_UP,
135 .doit = handle_startup_event,
136 },
137 {
138 .cmd = KSMBD_EVENT_SHUTTING_DOWN,
139 .doit = handle_unsupported_event,
140 },
141 {
142 .cmd = KSMBD_EVENT_LOGIN_REQUEST,
143 .doit = handle_unsupported_event,
144 },
145 {
146 .cmd = KSMBD_EVENT_LOGIN_RESPONSE,
147 .doit = handle_generic_event,
148 },
149 {
150 .cmd = KSMBD_EVENT_SHARE_CONFIG_REQUEST,
151 .doit = handle_unsupported_event,
152 },
153 {
154 .cmd = KSMBD_EVENT_SHARE_CONFIG_RESPONSE,
155 .doit = handle_generic_event,
156 },
157 {
158 .cmd = KSMBD_EVENT_TREE_CONNECT_REQUEST,
159 .doit = handle_unsupported_event,
160 },
161 {
162 .cmd = KSMBD_EVENT_TREE_CONNECT_RESPONSE,
163 .doit = handle_generic_event,
164 },
165 {
166 .cmd = KSMBD_EVENT_TREE_DISCONNECT_REQUEST,
167 .doit = handle_unsupported_event,
168 },
169 {
170 .cmd = KSMBD_EVENT_LOGOUT_REQUEST,
171 .doit = handle_unsupported_event,
172 },
173 {
174 .cmd = KSMBD_EVENT_RPC_REQUEST,
175 .doit = handle_unsupported_event,
176 },
177 {
178 .cmd = KSMBD_EVENT_RPC_RESPONSE,
179 .doit = handle_generic_event,
180 },
181 {
182 .cmd = KSMBD_EVENT_SPNEGO_AUTHEN_REQUEST,
183 .doit = handle_unsupported_event,
184 },
185 {
186 .cmd = KSMBD_EVENT_SPNEGO_AUTHEN_RESPONSE,
187 .doit = handle_generic_event,
188 },
189 };
190
191 static struct genl_family ksmbd_genl_family = {
192 .name = KSMBD_GENL_NAME,
193 .version = KSMBD_GENL_VERSION,
194 .hdrsize = 0,
195 .maxattr = KSMBD_EVENT_MAX,
196 .netnsok = true,
197 .module = THIS_MODULE,
198 .ops = ksmbd_genl_ops,
199 .n_ops = ARRAY_SIZE(ksmbd_genl_ops),
200 };
201
ksmbd_nl_init_fixup(void)202 static void ksmbd_nl_init_fixup(void)
203 {
204 int i;
205
206 for (i = 0; i < ARRAY_SIZE(ksmbd_genl_ops); i++)
207 ksmbd_genl_ops[i].validate = GENL_DONT_VALIDATE_STRICT |
208 GENL_DONT_VALIDATE_DUMP;
209
210 ksmbd_genl_family.policy = ksmbd_nl_policy;
211 }
212
rpc_context_flags(struct ksmbd_session * sess)213 static int rpc_context_flags(struct ksmbd_session *sess)
214 {
215 if (user_guest(sess->user))
216 return KSMBD_RPC_RESTRICTED_CONTEXT;
217 return 0;
218 }
219
ipc_update_last_active(void)220 static void ipc_update_last_active(void)
221 {
222 if (server_conf.ipc_timeout)
223 server_conf.ipc_last_active = jiffies;
224 }
225
ipc_msg_alloc(size_t sz)226 static struct ksmbd_ipc_msg *ipc_msg_alloc(size_t sz)
227 {
228 struct ksmbd_ipc_msg *msg;
229 size_t msg_sz = sz + sizeof(struct ksmbd_ipc_msg);
230
231 msg = kvzalloc(msg_sz, GFP_KERNEL);
232 if (msg)
233 msg->sz = sz;
234 return msg;
235 }
236
ipc_msg_free(struct ksmbd_ipc_msg * msg)237 static void ipc_msg_free(struct ksmbd_ipc_msg *msg)
238 {
239 kvfree(msg);
240 }
241
ipc_msg_handle_free(int handle)242 static void ipc_msg_handle_free(int handle)
243 {
244 if (handle >= 0)
245 ksmbd_release_id(&ipc_ida, handle);
246 }
247
handle_response(int type,void * payload,size_t sz)248 static int handle_response(int type, void *payload, size_t sz)
249 {
250 unsigned int handle = *(unsigned int *)payload;
251 struct ipc_msg_table_entry *entry;
252 int ret = 0;
253
254 ipc_update_last_active();
255 down_read(&ipc_msg_table_lock);
256 hash_for_each_possible(ipc_msg_table, entry, ipc_table_hlist, handle) {
257 if (handle != entry->handle)
258 continue;
259
260 entry->response = NULL;
261 /*
262 * Response message type value should be equal to
263 * request message type + 1.
264 */
265 if (entry->type + 1 != type) {
266 pr_err("Waiting for IPC type %d, got %d. Ignore.\n",
267 entry->type + 1, type);
268 }
269
270 entry->response = kvzalloc(sz, GFP_KERNEL);
271 if (!entry->response) {
272 ret = -ENOMEM;
273 break;
274 }
275
276 memcpy(entry->response, payload, sz);
277 wake_up_interruptible(&entry->wait);
278 ret = 0;
279 break;
280 }
281 up_read(&ipc_msg_table_lock);
282
283 return ret;
284 }
285
ipc_server_config_on_startup(struct ksmbd_startup_request * req)286 static int ipc_server_config_on_startup(struct ksmbd_startup_request *req)
287 {
288 int ret;
289
290 ksmbd_set_fd_limit(req->file_max);
291 server_conf.flags = req->flags;
292 server_conf.signing = req->signing;
293 server_conf.tcp_port = req->tcp_port;
294 server_conf.ipc_timeout = req->ipc_timeout * HZ;
295 server_conf.deadtime = req->deadtime * SMB_ECHO_INTERVAL;
296 server_conf.share_fake_fscaps = req->share_fake_fscaps;
297 ksmbd_init_domain(req->sub_auth);
298
299 if (req->smb2_max_read)
300 init_smb2_max_read_size(req->smb2_max_read);
301 if (req->smb2_max_write)
302 init_smb2_max_write_size(req->smb2_max_write);
303 if (req->smb2_max_trans)
304 init_smb2_max_trans_size(req->smb2_max_trans);
305 if (req->smb2_max_credits)
306 init_smb2_max_credits(req->smb2_max_credits);
307 if (req->smbd_max_io_size)
308 init_smbd_max_io_size(req->smbd_max_io_size);
309
310 if (req->max_connections)
311 server_conf.max_connections = req->max_connections;
312
313 ret = ksmbd_set_netbios_name(req->netbios_name);
314 ret |= ksmbd_set_server_string(req->server_string);
315 ret |= ksmbd_set_work_group(req->work_group);
316 ret |= ksmbd_tcp_set_interfaces(KSMBD_STARTUP_CONFIG_INTERFACES(req),
317 req->ifc_list_sz);
318 if (ret) {
319 pr_err("Server configuration error: %s %s %s\n",
320 req->netbios_name, req->server_string,
321 req->work_group);
322 return ret;
323 }
324
325 if (req->min_prot[0]) {
326 ret = ksmbd_lookup_protocol_idx(req->min_prot);
327 if (ret >= 0)
328 server_conf.min_protocol = ret;
329 }
330 if (req->max_prot[0]) {
331 ret = ksmbd_lookup_protocol_idx(req->max_prot);
332 if (ret >= 0)
333 server_conf.max_protocol = ret;
334 }
335
336 if (server_conf.ipc_timeout)
337 schedule_delayed_work(&ipc_timer_work, server_conf.ipc_timeout);
338 return 0;
339 }
340
handle_startup_event(struct sk_buff * skb,struct genl_info * info)341 static int handle_startup_event(struct sk_buff *skb, struct genl_info *info)
342 {
343 int ret = 0;
344
345 #ifdef CONFIG_SMB_SERVER_CHECK_CAP_NET_ADMIN
346 if (!netlink_capable(skb, CAP_NET_ADMIN))
347 return -EPERM;
348 #endif
349
350 if (!ksmbd_ipc_validate_version(info))
351 return -EINVAL;
352
353 if (!info->attrs[KSMBD_EVENT_STARTING_UP])
354 return -EINVAL;
355
356 mutex_lock(&startup_lock);
357 if (!ksmbd_server_configurable()) {
358 mutex_unlock(&startup_lock);
359 pr_err("Server reset is in progress, can't start daemon\n");
360 return -EINVAL;
361 }
362
363 if (ksmbd_tools_pid) {
364 if (ksmbd_ipc_heartbeat_request() == 0) {
365 ret = -EINVAL;
366 goto out;
367 }
368
369 pr_err("Reconnect to a new user space daemon\n");
370 } else {
371 struct ksmbd_startup_request *req;
372
373 req = nla_data(info->attrs[info->genlhdr->cmd]);
374 ret = ipc_server_config_on_startup(req);
375 if (ret)
376 goto out;
377 server_queue_ctrl_init_work();
378 }
379
380 ksmbd_tools_pid = info->snd_portid;
381 ipc_update_last_active();
382
383 out:
384 mutex_unlock(&startup_lock);
385 return ret;
386 }
387
handle_unsupported_event(struct sk_buff * skb,struct genl_info * info)388 static int handle_unsupported_event(struct sk_buff *skb, struct genl_info *info)
389 {
390 pr_err("Unknown IPC event: %d, ignore.\n", info->genlhdr->cmd);
391 return -EINVAL;
392 }
393
handle_generic_event(struct sk_buff * skb,struct genl_info * info)394 static int handle_generic_event(struct sk_buff *skb, struct genl_info *info)
395 {
396 void *payload;
397 int sz;
398 int type = info->genlhdr->cmd;
399
400 #ifdef CONFIG_SMB_SERVER_CHECK_CAP_NET_ADMIN
401 if (!netlink_capable(skb, CAP_NET_ADMIN))
402 return -EPERM;
403 #endif
404
405 if (type > KSMBD_EVENT_MAX) {
406 WARN_ON(1);
407 return -EINVAL;
408 }
409
410 if (!ksmbd_ipc_validate_version(info))
411 return -EINVAL;
412
413 if (!info->attrs[type])
414 return -EINVAL;
415
416 payload = nla_data(info->attrs[info->genlhdr->cmd]);
417 sz = nla_len(info->attrs[info->genlhdr->cmd]);
418 return handle_response(type, payload, sz);
419 }
420
ipc_msg_send(struct ksmbd_ipc_msg * msg)421 static int ipc_msg_send(struct ksmbd_ipc_msg *msg)
422 {
423 struct genlmsghdr *nlh;
424 struct sk_buff *skb;
425 int ret = -EINVAL;
426
427 if (!ksmbd_tools_pid)
428 return ret;
429
430 skb = genlmsg_new(msg->sz, GFP_KERNEL);
431 if (!skb)
432 return -ENOMEM;
433
434 nlh = genlmsg_put(skb, 0, 0, &ksmbd_genl_family, 0, msg->type);
435 if (!nlh)
436 goto out;
437
438 ret = nla_put(skb, msg->type, msg->sz, msg->payload);
439 if (ret) {
440 genlmsg_cancel(skb, nlh);
441 goto out;
442 }
443
444 genlmsg_end(skb, nlh);
445 ret = genlmsg_unicast(&init_net, skb, ksmbd_tools_pid);
446 if (!ret)
447 ipc_update_last_active();
448 return ret;
449
450 out:
451 nlmsg_free(skb);
452 return ret;
453 }
454
ipc_msg_send_request(struct ksmbd_ipc_msg * msg,unsigned int handle)455 static void *ipc_msg_send_request(struct ksmbd_ipc_msg *msg, unsigned int handle)
456 {
457 struct ipc_msg_table_entry entry;
458 int ret;
459
460 if ((int)handle < 0)
461 return NULL;
462
463 entry.type = msg->type;
464 entry.response = NULL;
465 init_waitqueue_head(&entry.wait);
466
467 down_write(&ipc_msg_table_lock);
468 entry.handle = handle;
469 hash_add(ipc_msg_table, &entry.ipc_table_hlist, entry.handle);
470 up_write(&ipc_msg_table_lock);
471
472 ret = ipc_msg_send(msg);
473 if (ret)
474 goto out;
475
476 ret = wait_event_interruptible_timeout(entry.wait,
477 entry.response != NULL,
478 IPC_WAIT_TIMEOUT);
479 out:
480 down_write(&ipc_msg_table_lock);
481 hash_del(&entry.ipc_table_hlist);
482 up_write(&ipc_msg_table_lock);
483 return entry.response;
484 }
485
ksmbd_ipc_heartbeat_request(void)486 static int ksmbd_ipc_heartbeat_request(void)
487 {
488 struct ksmbd_ipc_msg *msg;
489 int ret;
490
491 msg = ipc_msg_alloc(sizeof(struct ksmbd_heartbeat));
492 if (!msg)
493 return -EINVAL;
494
495 msg->type = KSMBD_EVENT_HEARTBEAT_REQUEST;
496 ret = ipc_msg_send(msg);
497 ipc_msg_free(msg);
498 return ret;
499 }
500
ksmbd_ipc_login_request(const char * account)501 struct ksmbd_login_response *ksmbd_ipc_login_request(const char *account)
502 {
503 struct ksmbd_ipc_msg *msg;
504 struct ksmbd_login_request *req;
505 struct ksmbd_login_response *resp;
506
507 if (strlen(account) >= KSMBD_REQ_MAX_ACCOUNT_NAME_SZ)
508 return NULL;
509
510 msg = ipc_msg_alloc(sizeof(struct ksmbd_login_request));
511 if (!msg)
512 return NULL;
513
514 msg->type = KSMBD_EVENT_LOGIN_REQUEST;
515 req = (struct ksmbd_login_request *)msg->payload;
516 req->handle = ksmbd_acquire_id(&ipc_ida);
517 strscpy(req->account, account, KSMBD_REQ_MAX_ACCOUNT_NAME_SZ);
518
519 resp = ipc_msg_send_request(msg, req->handle);
520 ipc_msg_handle_free(req->handle);
521 ipc_msg_free(msg);
522 return resp;
523 }
524
525 struct ksmbd_spnego_authen_response *
ksmbd_ipc_spnego_authen_request(const char * spnego_blob,int blob_len)526 ksmbd_ipc_spnego_authen_request(const char *spnego_blob, int blob_len)
527 {
528 struct ksmbd_ipc_msg *msg;
529 struct ksmbd_spnego_authen_request *req;
530 struct ksmbd_spnego_authen_response *resp;
531
532 msg = ipc_msg_alloc(sizeof(struct ksmbd_spnego_authen_request) +
533 blob_len + 1);
534 if (!msg)
535 return NULL;
536
537 msg->type = KSMBD_EVENT_SPNEGO_AUTHEN_REQUEST;
538 req = (struct ksmbd_spnego_authen_request *)msg->payload;
539 req->handle = ksmbd_acquire_id(&ipc_ida);
540 req->spnego_blob_len = blob_len;
541 memcpy(req->spnego_blob, spnego_blob, blob_len);
542
543 resp = ipc_msg_send_request(msg, req->handle);
544 ipc_msg_handle_free(req->handle);
545 ipc_msg_free(msg);
546 return resp;
547 }
548
549 struct ksmbd_tree_connect_response *
ksmbd_ipc_tree_connect_request(struct ksmbd_session * sess,struct ksmbd_share_config * share,struct ksmbd_tree_connect * tree_conn,struct sockaddr * peer_addr)550 ksmbd_ipc_tree_connect_request(struct ksmbd_session *sess,
551 struct ksmbd_share_config *share,
552 struct ksmbd_tree_connect *tree_conn,
553 struct sockaddr *peer_addr)
554 {
555 struct ksmbd_ipc_msg *msg;
556 struct ksmbd_tree_connect_request *req;
557 struct ksmbd_tree_connect_response *resp;
558
559 if (strlen(user_name(sess->user)) >= KSMBD_REQ_MAX_ACCOUNT_NAME_SZ)
560 return NULL;
561
562 if (strlen(share->name) >= KSMBD_REQ_MAX_SHARE_NAME)
563 return NULL;
564
565 msg = ipc_msg_alloc(sizeof(struct ksmbd_tree_connect_request));
566 if (!msg)
567 return NULL;
568
569 msg->type = KSMBD_EVENT_TREE_CONNECT_REQUEST;
570 req = (struct ksmbd_tree_connect_request *)msg->payload;
571
572 req->handle = ksmbd_acquire_id(&ipc_ida);
573 req->account_flags = sess->user->flags;
574 req->session_id = sess->id;
575 req->connect_id = tree_conn->id;
576 strscpy(req->account, user_name(sess->user), KSMBD_REQ_MAX_ACCOUNT_NAME_SZ);
577 strscpy(req->share, share->name, KSMBD_REQ_MAX_SHARE_NAME);
578 snprintf(req->peer_addr, sizeof(req->peer_addr), "%pIS", peer_addr);
579
580 if (peer_addr->sa_family == AF_INET6)
581 req->flags |= KSMBD_TREE_CONN_FLAG_REQUEST_IPV6;
582 if (test_session_flag(sess, CIFDS_SESSION_FLAG_SMB2))
583 req->flags |= KSMBD_TREE_CONN_FLAG_REQUEST_SMB2;
584
585 resp = ipc_msg_send_request(msg, req->handle);
586 ipc_msg_handle_free(req->handle);
587 ipc_msg_free(msg);
588 return resp;
589 }
590
ksmbd_ipc_tree_disconnect_request(unsigned long long session_id,unsigned long long connect_id)591 int ksmbd_ipc_tree_disconnect_request(unsigned long long session_id,
592 unsigned long long connect_id)
593 {
594 struct ksmbd_ipc_msg *msg;
595 struct ksmbd_tree_disconnect_request *req;
596 int ret;
597
598 msg = ipc_msg_alloc(sizeof(struct ksmbd_tree_disconnect_request));
599 if (!msg)
600 return -ENOMEM;
601
602 msg->type = KSMBD_EVENT_TREE_DISCONNECT_REQUEST;
603 req = (struct ksmbd_tree_disconnect_request *)msg->payload;
604 req->session_id = session_id;
605 req->connect_id = connect_id;
606
607 ret = ipc_msg_send(msg);
608 ipc_msg_free(msg);
609 return ret;
610 }
611
ksmbd_ipc_logout_request(const char * account,int flags)612 int ksmbd_ipc_logout_request(const char *account, int flags)
613 {
614 struct ksmbd_ipc_msg *msg;
615 struct ksmbd_logout_request *req;
616 int ret;
617
618 if (strlen(account) >= KSMBD_REQ_MAX_ACCOUNT_NAME_SZ)
619 return -EINVAL;
620
621 msg = ipc_msg_alloc(sizeof(struct ksmbd_logout_request));
622 if (!msg)
623 return -ENOMEM;
624
625 msg->type = KSMBD_EVENT_LOGOUT_REQUEST;
626 req = (struct ksmbd_logout_request *)msg->payload;
627 req->account_flags = flags;
628 strscpy(req->account, account, KSMBD_REQ_MAX_ACCOUNT_NAME_SZ);
629
630 ret = ipc_msg_send(msg);
631 ipc_msg_free(msg);
632 return ret;
633 }
634
635 struct ksmbd_share_config_response *
ksmbd_ipc_share_config_request(const char * name)636 ksmbd_ipc_share_config_request(const char *name)
637 {
638 struct ksmbd_ipc_msg *msg;
639 struct ksmbd_share_config_request *req;
640 struct ksmbd_share_config_response *resp;
641
642 if (strlen(name) >= KSMBD_REQ_MAX_SHARE_NAME)
643 return NULL;
644
645 msg = ipc_msg_alloc(sizeof(struct ksmbd_share_config_request));
646 if (!msg)
647 return NULL;
648
649 msg->type = KSMBD_EVENT_SHARE_CONFIG_REQUEST;
650 req = (struct ksmbd_share_config_request *)msg->payload;
651 req->handle = ksmbd_acquire_id(&ipc_ida);
652 strscpy(req->share_name, name, KSMBD_REQ_MAX_SHARE_NAME);
653
654 resp = ipc_msg_send_request(msg, req->handle);
655 ipc_msg_handle_free(req->handle);
656 ipc_msg_free(msg);
657 return resp;
658 }
659
ksmbd_rpc_open(struct ksmbd_session * sess,int handle)660 struct ksmbd_rpc_command *ksmbd_rpc_open(struct ksmbd_session *sess, int handle)
661 {
662 struct ksmbd_ipc_msg *msg;
663 struct ksmbd_rpc_command *req;
664 struct ksmbd_rpc_command *resp;
665
666 msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command));
667 if (!msg)
668 return NULL;
669
670 msg->type = KSMBD_EVENT_RPC_REQUEST;
671 req = (struct ksmbd_rpc_command *)msg->payload;
672 req->handle = handle;
673 req->flags = ksmbd_session_rpc_method(sess, handle);
674 req->flags |= KSMBD_RPC_OPEN_METHOD;
675 req->payload_sz = 0;
676
677 resp = ipc_msg_send_request(msg, req->handle);
678 ipc_msg_free(msg);
679 return resp;
680 }
681
ksmbd_rpc_close(struct ksmbd_session * sess,int handle)682 struct ksmbd_rpc_command *ksmbd_rpc_close(struct ksmbd_session *sess, int handle)
683 {
684 struct ksmbd_ipc_msg *msg;
685 struct ksmbd_rpc_command *req;
686 struct ksmbd_rpc_command *resp;
687
688 msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command));
689 if (!msg)
690 return NULL;
691
692 msg->type = KSMBD_EVENT_RPC_REQUEST;
693 req = (struct ksmbd_rpc_command *)msg->payload;
694 req->handle = handle;
695 req->flags = ksmbd_session_rpc_method(sess, handle);
696 req->flags |= KSMBD_RPC_CLOSE_METHOD;
697 req->payload_sz = 0;
698
699 resp = ipc_msg_send_request(msg, req->handle);
700 ipc_msg_free(msg);
701 return resp;
702 }
703
ksmbd_rpc_write(struct ksmbd_session * sess,int handle,void * payload,size_t payload_sz)704 struct ksmbd_rpc_command *ksmbd_rpc_write(struct ksmbd_session *sess, int handle,
705 void *payload, size_t payload_sz)
706 {
707 struct ksmbd_ipc_msg *msg;
708 struct ksmbd_rpc_command *req;
709 struct ksmbd_rpc_command *resp;
710
711 msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command) + payload_sz + 1);
712 if (!msg)
713 return NULL;
714
715 msg->type = KSMBD_EVENT_RPC_REQUEST;
716 req = (struct ksmbd_rpc_command *)msg->payload;
717 req->handle = handle;
718 req->flags = ksmbd_session_rpc_method(sess, handle);
719 req->flags |= rpc_context_flags(sess);
720 req->flags |= KSMBD_RPC_WRITE_METHOD;
721 req->payload_sz = payload_sz;
722 memcpy(req->payload, payload, payload_sz);
723
724 resp = ipc_msg_send_request(msg, req->handle);
725 ipc_msg_free(msg);
726 return resp;
727 }
728
ksmbd_rpc_read(struct ksmbd_session * sess,int handle)729 struct ksmbd_rpc_command *ksmbd_rpc_read(struct ksmbd_session *sess, int handle)
730 {
731 struct ksmbd_ipc_msg *msg;
732 struct ksmbd_rpc_command *req;
733 struct ksmbd_rpc_command *resp;
734
735 msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command));
736 if (!msg)
737 return NULL;
738
739 msg->type = KSMBD_EVENT_RPC_REQUEST;
740 req = (struct ksmbd_rpc_command *)msg->payload;
741 req->handle = handle;
742 req->flags = ksmbd_session_rpc_method(sess, handle);
743 req->flags |= rpc_context_flags(sess);
744 req->flags |= KSMBD_RPC_READ_METHOD;
745 req->payload_sz = 0;
746
747 resp = ipc_msg_send_request(msg, req->handle);
748 ipc_msg_free(msg);
749 return resp;
750 }
751
ksmbd_rpc_ioctl(struct ksmbd_session * sess,int handle,void * payload,size_t payload_sz)752 struct ksmbd_rpc_command *ksmbd_rpc_ioctl(struct ksmbd_session *sess, int handle,
753 void *payload, size_t payload_sz)
754 {
755 struct ksmbd_ipc_msg *msg;
756 struct ksmbd_rpc_command *req;
757 struct ksmbd_rpc_command *resp;
758
759 msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command) + payload_sz + 1);
760 if (!msg)
761 return NULL;
762
763 msg->type = KSMBD_EVENT_RPC_REQUEST;
764 req = (struct ksmbd_rpc_command *)msg->payload;
765 req->handle = handle;
766 req->flags = ksmbd_session_rpc_method(sess, handle);
767 req->flags |= rpc_context_flags(sess);
768 req->flags |= KSMBD_RPC_IOCTL_METHOD;
769 req->payload_sz = payload_sz;
770 memcpy(req->payload, payload, payload_sz);
771
772 resp = ipc_msg_send_request(msg, req->handle);
773 ipc_msg_free(msg);
774 return resp;
775 }
776
ksmbd_rpc_rap(struct ksmbd_session * sess,void * payload,size_t payload_sz)777 struct ksmbd_rpc_command *ksmbd_rpc_rap(struct ksmbd_session *sess, void *payload,
778 size_t payload_sz)
779 {
780 struct ksmbd_ipc_msg *msg;
781 struct ksmbd_rpc_command *req;
782 struct ksmbd_rpc_command *resp;
783
784 msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command) + payload_sz + 1);
785 if (!msg)
786 return NULL;
787
788 msg->type = KSMBD_EVENT_RPC_REQUEST;
789 req = (struct ksmbd_rpc_command *)msg->payload;
790 req->handle = ksmbd_acquire_id(&ipc_ida);
791 req->flags = rpc_context_flags(sess);
792 req->flags |= KSMBD_RPC_RAP_METHOD;
793 req->payload_sz = payload_sz;
794 memcpy(req->payload, payload, payload_sz);
795
796 resp = ipc_msg_send_request(msg, req->handle);
797 ipc_msg_handle_free(req->handle);
798 ipc_msg_free(msg);
799 return resp;
800 }
801
__ipc_heartbeat(void)802 static int __ipc_heartbeat(void)
803 {
804 unsigned long delta;
805
806 if (!ksmbd_server_running())
807 return 0;
808
809 if (time_after(jiffies, server_conf.ipc_last_active)) {
810 delta = (jiffies - server_conf.ipc_last_active);
811 } else {
812 ipc_update_last_active();
813 schedule_delayed_work(&ipc_timer_work,
814 server_conf.ipc_timeout);
815 return 0;
816 }
817
818 if (delta < server_conf.ipc_timeout) {
819 schedule_delayed_work(&ipc_timer_work,
820 server_conf.ipc_timeout - delta);
821 return 0;
822 }
823
824 if (ksmbd_ipc_heartbeat_request() == 0) {
825 schedule_delayed_work(&ipc_timer_work,
826 server_conf.ipc_timeout);
827 return 0;
828 }
829
830 mutex_lock(&startup_lock);
831 WRITE_ONCE(server_conf.state, SERVER_STATE_RESETTING);
832 server_conf.ipc_last_active = 0;
833 ksmbd_tools_pid = 0;
834 pr_err("No IPC daemon response for %lus\n", delta / HZ);
835 mutex_unlock(&startup_lock);
836 return -EINVAL;
837 }
838
ipc_timer_heartbeat(struct work_struct * w)839 static void ipc_timer_heartbeat(struct work_struct *w)
840 {
841 if (__ipc_heartbeat())
842 server_queue_ctrl_reset_work();
843 }
844
ksmbd_ipc_id_alloc(void)845 int ksmbd_ipc_id_alloc(void)
846 {
847 return ksmbd_acquire_id(&ipc_ida);
848 }
849
ksmbd_rpc_id_free(int handle)850 void ksmbd_rpc_id_free(int handle)
851 {
852 ksmbd_release_id(&ipc_ida, handle);
853 }
854
ksmbd_ipc_release(void)855 void ksmbd_ipc_release(void)
856 {
857 cancel_delayed_work_sync(&ipc_timer_work);
858 genl_unregister_family(&ksmbd_genl_family);
859 }
860
ksmbd_ipc_soft_reset(void)861 void ksmbd_ipc_soft_reset(void)
862 {
863 mutex_lock(&startup_lock);
864 ksmbd_tools_pid = 0;
865 cancel_delayed_work_sync(&ipc_timer_work);
866 mutex_unlock(&startup_lock);
867 }
868
ksmbd_ipc_init(void)869 int ksmbd_ipc_init(void)
870 {
871 int ret = 0;
872
873 ksmbd_nl_init_fixup();
874 INIT_DELAYED_WORK(&ipc_timer_work, ipc_timer_heartbeat);
875
876 ret = genl_register_family(&ksmbd_genl_family);
877 if (ret) {
878 pr_err("Failed to register KSMBD netlink interface %d\n", ret);
879 cancel_delayed_work_sync(&ipc_timer_work);
880 }
881
882 return ret;
883 }
884