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