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