xref: /linux/fs/smb/server/transport_ipc.c (revision e814f3fd16acfb7f9966773953de8f740a1e3202)
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 	server_conf.bind_interfaces_only = req->bind_interfaces_only;
337 	ret |= ksmbd_tcp_set_interfaces(KSMBD_STARTUP_CONFIG_INTERFACES(req),
338 					req->ifc_list_sz);
339 	if (ret) {
340 		pr_err("Server configuration error: %s %s %s\n",
341 		       req->netbios_name, req->server_string,
342 		       req->work_group);
343 		return ret;
344 	}
345 
346 	if (req->min_prot[0]) {
347 		ret = ksmbd_lookup_protocol_idx(req->min_prot);
348 		if (ret >= 0)
349 			server_conf.min_protocol = ret;
350 	}
351 	if (req->max_prot[0]) {
352 		ret = ksmbd_lookup_protocol_idx(req->max_prot);
353 		if (ret >= 0)
354 			server_conf.max_protocol = ret;
355 	}
356 
357 	if (server_conf.ipc_timeout)
358 		schedule_delayed_work(&ipc_timer_work, server_conf.ipc_timeout);
359 	return 0;
360 }
361 
handle_startup_event(struct sk_buff * skb,struct genl_info * info)362 static int handle_startup_event(struct sk_buff *skb, struct genl_info *info)
363 {
364 	int ret = 0;
365 
366 #ifdef CONFIG_SMB_SERVER_CHECK_CAP_NET_ADMIN
367 	if (!netlink_capable(skb, CAP_NET_ADMIN))
368 		return -EPERM;
369 #endif
370 
371 	if (!ksmbd_ipc_validate_version(info))
372 		return -EINVAL;
373 
374 	if (!info->attrs[KSMBD_EVENT_STARTING_UP])
375 		return -EINVAL;
376 
377 	mutex_lock(&startup_lock);
378 	if (!ksmbd_server_configurable()) {
379 		mutex_unlock(&startup_lock);
380 		pr_err("Server reset is in progress, can't start daemon\n");
381 		return -EINVAL;
382 	}
383 
384 	if (ksmbd_tools_pid) {
385 		if (ksmbd_ipc_heartbeat_request() == 0) {
386 			ret = -EINVAL;
387 			goto out;
388 		}
389 
390 		pr_err("Reconnect to a new user space daemon\n");
391 	} else {
392 		struct ksmbd_startup_request *req;
393 
394 		req = nla_data(info->attrs[info->genlhdr->cmd]);
395 		ret = ipc_server_config_on_startup(req);
396 		if (ret)
397 			goto out;
398 		server_queue_ctrl_init_work();
399 	}
400 
401 	ksmbd_tools_pid = info->snd_portid;
402 	ipc_update_last_active();
403 
404 out:
405 	mutex_unlock(&startup_lock);
406 	return ret;
407 }
408 
handle_unsupported_event(struct sk_buff * skb,struct genl_info * info)409 static int handle_unsupported_event(struct sk_buff *skb, struct genl_info *info)
410 {
411 	pr_err("Unknown IPC event: %d, ignore.\n", info->genlhdr->cmd);
412 	return -EINVAL;
413 }
414 
handle_generic_event(struct sk_buff * skb,struct genl_info * info)415 static int handle_generic_event(struct sk_buff *skb, struct genl_info *info)
416 {
417 	void *payload;
418 	int sz;
419 	int type = info->genlhdr->cmd;
420 
421 #ifdef CONFIG_SMB_SERVER_CHECK_CAP_NET_ADMIN
422 	if (!netlink_capable(skb, CAP_NET_ADMIN))
423 		return -EPERM;
424 #endif
425 
426 	if (type > KSMBD_EVENT_MAX) {
427 		WARN_ON(1);
428 		return -EINVAL;
429 	}
430 
431 	if (!ksmbd_ipc_validate_version(info))
432 		return -EINVAL;
433 
434 	if (!info->attrs[type])
435 		return -EINVAL;
436 
437 	payload = nla_data(info->attrs[info->genlhdr->cmd]);
438 	sz = nla_len(info->attrs[info->genlhdr->cmd]);
439 	return handle_response(type, payload, sz);
440 }
441 
ipc_msg_send(struct ksmbd_ipc_msg * msg)442 static int ipc_msg_send(struct ksmbd_ipc_msg *msg)
443 {
444 	struct genlmsghdr *nlh;
445 	struct sk_buff *skb;
446 	int ret = -EINVAL;
447 
448 	if (!ksmbd_tools_pid)
449 		return ret;
450 
451 	skb = genlmsg_new(msg->sz, KSMBD_DEFAULT_GFP);
452 	if (!skb)
453 		return -ENOMEM;
454 
455 	nlh = genlmsg_put(skb, 0, 0, &ksmbd_genl_family, 0, msg->type);
456 	if (!nlh)
457 		goto out;
458 
459 	ret = nla_put(skb, msg->type, msg->sz, msg->payload);
460 	if (ret) {
461 		genlmsg_cancel(skb, nlh);
462 		goto out;
463 	}
464 
465 	genlmsg_end(skb, nlh);
466 	ret = genlmsg_unicast(&init_net, skb, ksmbd_tools_pid);
467 	if (!ret)
468 		ipc_update_last_active();
469 	return ret;
470 
471 out:
472 	nlmsg_free(skb);
473 	return ret;
474 }
475 
ipc_validate_msg(struct ipc_msg_table_entry * entry)476 static int ipc_validate_msg(struct ipc_msg_table_entry *entry)
477 {
478 	unsigned int msg_sz = entry->msg_sz;
479 
480 	switch (entry->type) {
481 	case KSMBD_EVENT_RPC_REQUEST:
482 	{
483 		struct ksmbd_rpc_command *resp = entry->response;
484 
485 		msg_sz = sizeof(struct ksmbd_rpc_command) + resp->payload_sz;
486 		break;
487 	}
488 	case KSMBD_EVENT_SPNEGO_AUTHEN_REQUEST:
489 	{
490 		struct ksmbd_spnego_authen_response *resp = entry->response;
491 
492 		msg_sz = sizeof(struct ksmbd_spnego_authen_response) +
493 				resp->session_key_len + resp->spnego_blob_len;
494 		break;
495 	}
496 	case KSMBD_EVENT_SHARE_CONFIG_REQUEST:
497 	{
498 		struct ksmbd_share_config_response *resp = entry->response;
499 
500 		if (resp->payload_sz) {
501 			if (resp->payload_sz < resp->veto_list_sz)
502 				return -EINVAL;
503 
504 			msg_sz = sizeof(struct ksmbd_share_config_response) +
505 					resp->payload_sz;
506 		}
507 		break;
508 	}
509 	case KSMBD_EVENT_LOGIN_REQUEST_EXT:
510 	{
511 		struct ksmbd_login_response_ext *resp = entry->response;
512 
513 		if (resp->ngroups) {
514 			msg_sz = sizeof(struct ksmbd_login_response_ext) +
515 					resp->ngroups * sizeof(gid_t);
516 		}
517 	}
518 	}
519 
520 	return entry->msg_sz != msg_sz ? -EINVAL : 0;
521 }
522 
ipc_msg_send_request(struct ksmbd_ipc_msg * msg,unsigned int handle)523 static void *ipc_msg_send_request(struct ksmbd_ipc_msg *msg, unsigned int handle)
524 {
525 	struct ipc_msg_table_entry entry;
526 	int ret;
527 
528 	if ((int)handle < 0)
529 		return NULL;
530 
531 	entry.type = msg->type;
532 	entry.response = NULL;
533 	init_waitqueue_head(&entry.wait);
534 
535 	down_write(&ipc_msg_table_lock);
536 	entry.handle = handle;
537 	hash_add(ipc_msg_table, &entry.ipc_table_hlist, entry.handle);
538 	up_write(&ipc_msg_table_lock);
539 
540 	ret = ipc_msg_send(msg);
541 	if (ret)
542 		goto out;
543 
544 	ret = wait_event_interruptible_timeout(entry.wait,
545 					       entry.response != NULL,
546 					       IPC_WAIT_TIMEOUT);
547 	if (entry.response) {
548 		ret = ipc_validate_msg(&entry);
549 		if (ret) {
550 			kvfree(entry.response);
551 			entry.response = NULL;
552 		}
553 	}
554 out:
555 	down_write(&ipc_msg_table_lock);
556 	hash_del(&entry.ipc_table_hlist);
557 	up_write(&ipc_msg_table_lock);
558 	return entry.response;
559 }
560 
ksmbd_ipc_heartbeat_request(void)561 static int ksmbd_ipc_heartbeat_request(void)
562 {
563 	struct ksmbd_ipc_msg *msg;
564 	int ret;
565 
566 	msg = ipc_msg_alloc(sizeof(struct ksmbd_heartbeat));
567 	if (!msg)
568 		return -EINVAL;
569 
570 	msg->type = KSMBD_EVENT_HEARTBEAT_REQUEST;
571 	ret = ipc_msg_send(msg);
572 	ipc_msg_free(msg);
573 	return ret;
574 }
575 
ksmbd_ipc_login_request(const char * account)576 struct ksmbd_login_response *ksmbd_ipc_login_request(const char *account)
577 {
578 	struct ksmbd_ipc_msg *msg;
579 	struct ksmbd_login_request *req;
580 	struct ksmbd_login_response *resp;
581 
582 	if (strlen(account) >= KSMBD_REQ_MAX_ACCOUNT_NAME_SZ)
583 		return NULL;
584 
585 	msg = ipc_msg_alloc(sizeof(struct ksmbd_login_request));
586 	if (!msg)
587 		return NULL;
588 
589 	msg->type = KSMBD_EVENT_LOGIN_REQUEST;
590 	req = (struct ksmbd_login_request *)msg->payload;
591 	req->handle = ksmbd_acquire_id(&ipc_ida);
592 	strscpy(req->account, account, KSMBD_REQ_MAX_ACCOUNT_NAME_SZ);
593 
594 	resp = ipc_msg_send_request(msg, req->handle);
595 	ipc_msg_handle_free(req->handle);
596 	ipc_msg_free(msg);
597 	return resp;
598 }
599 
ksmbd_ipc_login_request_ext(const char * account)600 struct ksmbd_login_response_ext *ksmbd_ipc_login_request_ext(const char *account)
601 {
602 	struct ksmbd_ipc_msg *msg;
603 	struct ksmbd_login_request *req;
604 	struct ksmbd_login_response_ext *resp;
605 
606 	if (strlen(account) >= KSMBD_REQ_MAX_ACCOUNT_NAME_SZ)
607 		return NULL;
608 
609 	msg = ipc_msg_alloc(sizeof(struct ksmbd_login_request));
610 	if (!msg)
611 		return NULL;
612 
613 	msg->type = KSMBD_EVENT_LOGIN_REQUEST_EXT;
614 	req = (struct ksmbd_login_request *)msg->payload;
615 	req->handle = ksmbd_acquire_id(&ipc_ida);
616 	strscpy(req->account, account, KSMBD_REQ_MAX_ACCOUNT_NAME_SZ);
617 	resp = ipc_msg_send_request(msg, req->handle);
618 	ipc_msg_handle_free(req->handle);
619 	ipc_msg_free(msg);
620 	return resp;
621 }
622 
623 struct ksmbd_spnego_authen_response *
ksmbd_ipc_spnego_authen_request(const char * spnego_blob,int blob_len)624 ksmbd_ipc_spnego_authen_request(const char *spnego_blob, int blob_len)
625 {
626 	struct ksmbd_ipc_msg *msg;
627 	struct ksmbd_spnego_authen_request *req;
628 	struct ksmbd_spnego_authen_response *resp;
629 
630 	if (blob_len > KSMBD_IPC_MAX_PAYLOAD)
631 		return NULL;
632 
633 	msg = ipc_msg_alloc(sizeof(struct ksmbd_spnego_authen_request) +
634 			blob_len + 1);
635 	if (!msg)
636 		return NULL;
637 
638 	msg->type = KSMBD_EVENT_SPNEGO_AUTHEN_REQUEST;
639 	req = (struct ksmbd_spnego_authen_request *)msg->payload;
640 	req->handle = ksmbd_acquire_id(&ipc_ida);
641 	req->spnego_blob_len = blob_len;
642 	memcpy(req->spnego_blob, spnego_blob, blob_len);
643 
644 	resp = ipc_msg_send_request(msg, req->handle);
645 	ipc_msg_handle_free(req->handle);
646 	ipc_msg_free(msg);
647 	return resp;
648 }
649 
650 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)651 ksmbd_ipc_tree_connect_request(struct ksmbd_session *sess,
652 			       struct ksmbd_share_config *share,
653 			       struct ksmbd_tree_connect *tree_conn,
654 			       struct sockaddr *peer_addr)
655 {
656 	struct ksmbd_ipc_msg *msg;
657 	struct ksmbd_tree_connect_request *req;
658 	struct ksmbd_tree_connect_response *resp;
659 
660 	if (strlen(user_name(sess->user)) >= KSMBD_REQ_MAX_ACCOUNT_NAME_SZ)
661 		return NULL;
662 
663 	if (strlen(share->name) >= KSMBD_REQ_MAX_SHARE_NAME)
664 		return NULL;
665 
666 	msg = ipc_msg_alloc(sizeof(struct ksmbd_tree_connect_request));
667 	if (!msg)
668 		return NULL;
669 
670 	msg->type = KSMBD_EVENT_TREE_CONNECT_REQUEST;
671 	req = (struct ksmbd_tree_connect_request *)msg->payload;
672 
673 	req->handle = ksmbd_acquire_id(&ipc_ida);
674 	req->account_flags = sess->user->flags;
675 	req->session_id = sess->id;
676 	req->connect_id = tree_conn->id;
677 	strscpy(req->account, user_name(sess->user), KSMBD_REQ_MAX_ACCOUNT_NAME_SZ);
678 	strscpy(req->share, share->name, KSMBD_REQ_MAX_SHARE_NAME);
679 	snprintf(req->peer_addr, sizeof(req->peer_addr), "%pIS", peer_addr);
680 
681 	if (peer_addr->sa_family == AF_INET6)
682 		req->flags |= KSMBD_TREE_CONN_FLAG_REQUEST_IPV6;
683 	if (test_session_flag(sess, CIFDS_SESSION_FLAG_SMB2))
684 		req->flags |= KSMBD_TREE_CONN_FLAG_REQUEST_SMB2;
685 
686 	resp = ipc_msg_send_request(msg, req->handle);
687 	ipc_msg_handle_free(req->handle);
688 	ipc_msg_free(msg);
689 	return resp;
690 }
691 
ksmbd_ipc_tree_disconnect_request(unsigned long long session_id,unsigned long long connect_id)692 int ksmbd_ipc_tree_disconnect_request(unsigned long long session_id,
693 				      unsigned long long connect_id)
694 {
695 	struct ksmbd_ipc_msg *msg;
696 	struct ksmbd_tree_disconnect_request *req;
697 	int ret;
698 
699 	msg = ipc_msg_alloc(sizeof(struct ksmbd_tree_disconnect_request));
700 	if (!msg)
701 		return -ENOMEM;
702 
703 	msg->type = KSMBD_EVENT_TREE_DISCONNECT_REQUEST;
704 	req = (struct ksmbd_tree_disconnect_request *)msg->payload;
705 	req->session_id = session_id;
706 	req->connect_id = connect_id;
707 
708 	ret = ipc_msg_send(msg);
709 	ipc_msg_free(msg);
710 	return ret;
711 }
712 
ksmbd_ipc_logout_request(const char * account,int flags)713 int ksmbd_ipc_logout_request(const char *account, int flags)
714 {
715 	struct ksmbd_ipc_msg *msg;
716 	struct ksmbd_logout_request *req;
717 	int ret;
718 
719 	if (strlen(account) >= KSMBD_REQ_MAX_ACCOUNT_NAME_SZ)
720 		return -EINVAL;
721 
722 	msg = ipc_msg_alloc(sizeof(struct ksmbd_logout_request));
723 	if (!msg)
724 		return -ENOMEM;
725 
726 	msg->type = KSMBD_EVENT_LOGOUT_REQUEST;
727 	req = (struct ksmbd_logout_request *)msg->payload;
728 	req->account_flags = flags;
729 	strscpy(req->account, account, KSMBD_REQ_MAX_ACCOUNT_NAME_SZ);
730 
731 	ret = ipc_msg_send(msg);
732 	ipc_msg_free(msg);
733 	return ret;
734 }
735 
736 struct ksmbd_share_config_response *
ksmbd_ipc_share_config_request(const char * name)737 ksmbd_ipc_share_config_request(const char *name)
738 {
739 	struct ksmbd_ipc_msg *msg;
740 	struct ksmbd_share_config_request *req;
741 	struct ksmbd_share_config_response *resp;
742 
743 	if (strlen(name) >= KSMBD_REQ_MAX_SHARE_NAME)
744 		return NULL;
745 
746 	msg = ipc_msg_alloc(sizeof(struct ksmbd_share_config_request));
747 	if (!msg)
748 		return NULL;
749 
750 	msg->type = KSMBD_EVENT_SHARE_CONFIG_REQUEST;
751 	req = (struct ksmbd_share_config_request *)msg->payload;
752 	req->handle = ksmbd_acquire_id(&ipc_ida);
753 	strscpy(req->share_name, name, KSMBD_REQ_MAX_SHARE_NAME);
754 
755 	resp = ipc_msg_send_request(msg, req->handle);
756 	ipc_msg_handle_free(req->handle);
757 	ipc_msg_free(msg);
758 	return resp;
759 }
760 
ksmbd_rpc_open(struct ksmbd_session * sess,int handle)761 struct ksmbd_rpc_command *ksmbd_rpc_open(struct ksmbd_session *sess, int handle)
762 {
763 	struct ksmbd_ipc_msg *msg;
764 	struct ksmbd_rpc_command *req;
765 	struct ksmbd_rpc_command *resp;
766 
767 	msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command));
768 	if (!msg)
769 		return NULL;
770 
771 	msg->type = KSMBD_EVENT_RPC_REQUEST;
772 	req = (struct ksmbd_rpc_command *)msg->payload;
773 	req->handle = handle;
774 	req->flags = ksmbd_session_rpc_method(sess, handle);
775 	req->flags |= KSMBD_RPC_OPEN_METHOD;
776 	req->payload_sz = 0;
777 
778 	resp = ipc_msg_send_request(msg, req->handle);
779 	ipc_msg_free(msg);
780 	return resp;
781 }
782 
ksmbd_rpc_close(struct ksmbd_session * sess,int handle)783 struct ksmbd_rpc_command *ksmbd_rpc_close(struct ksmbd_session *sess, int handle)
784 {
785 	struct ksmbd_ipc_msg *msg;
786 	struct ksmbd_rpc_command *req;
787 	struct ksmbd_rpc_command *resp;
788 
789 	msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command));
790 	if (!msg)
791 		return NULL;
792 
793 	msg->type = KSMBD_EVENT_RPC_REQUEST;
794 	req = (struct ksmbd_rpc_command *)msg->payload;
795 	req->handle = handle;
796 	req->flags = ksmbd_session_rpc_method(sess, handle);
797 	req->flags |= KSMBD_RPC_CLOSE_METHOD;
798 	req->payload_sz = 0;
799 
800 	resp = ipc_msg_send_request(msg, req->handle);
801 	ipc_msg_free(msg);
802 	return resp;
803 }
804 
ksmbd_rpc_write(struct ksmbd_session * sess,int handle,void * payload,size_t payload_sz)805 struct ksmbd_rpc_command *ksmbd_rpc_write(struct ksmbd_session *sess, int handle,
806 					  void *payload, size_t payload_sz)
807 {
808 	struct ksmbd_ipc_msg *msg;
809 	struct ksmbd_rpc_command *req;
810 	struct ksmbd_rpc_command *resp;
811 
812 	if (payload_sz > KSMBD_IPC_MAX_PAYLOAD)
813 		return NULL;
814 
815 	msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command) + payload_sz + 1);
816 	if (!msg)
817 		return NULL;
818 
819 	msg->type = KSMBD_EVENT_RPC_REQUEST;
820 	req = (struct ksmbd_rpc_command *)msg->payload;
821 	req->handle = handle;
822 	req->flags = ksmbd_session_rpc_method(sess, handle);
823 	req->flags |= rpc_context_flags(sess);
824 	req->flags |= KSMBD_RPC_WRITE_METHOD;
825 	req->payload_sz = payload_sz;
826 	memcpy(req->payload, payload, payload_sz);
827 
828 	resp = ipc_msg_send_request(msg, req->handle);
829 	ipc_msg_free(msg);
830 	return resp;
831 }
832 
ksmbd_rpc_read(struct ksmbd_session * sess,int handle)833 struct ksmbd_rpc_command *ksmbd_rpc_read(struct ksmbd_session *sess, int handle)
834 {
835 	struct ksmbd_ipc_msg *msg;
836 	struct ksmbd_rpc_command *req;
837 	struct ksmbd_rpc_command *resp;
838 
839 	msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command));
840 	if (!msg)
841 		return NULL;
842 
843 	msg->type = KSMBD_EVENT_RPC_REQUEST;
844 	req = (struct ksmbd_rpc_command *)msg->payload;
845 	req->handle = handle;
846 	req->flags = ksmbd_session_rpc_method(sess, handle);
847 	req->flags |= rpc_context_flags(sess);
848 	req->flags |= KSMBD_RPC_READ_METHOD;
849 	req->payload_sz = 0;
850 
851 	resp = ipc_msg_send_request(msg, req->handle);
852 	ipc_msg_free(msg);
853 	return resp;
854 }
855 
ksmbd_rpc_ioctl(struct ksmbd_session * sess,int handle,void * payload,size_t payload_sz)856 struct ksmbd_rpc_command *ksmbd_rpc_ioctl(struct ksmbd_session *sess, int handle,
857 					  void *payload, size_t payload_sz)
858 {
859 	struct ksmbd_ipc_msg *msg;
860 	struct ksmbd_rpc_command *req;
861 	struct ksmbd_rpc_command *resp;
862 
863 	if (payload_sz > KSMBD_IPC_MAX_PAYLOAD)
864 		return NULL;
865 
866 	msg = ipc_msg_alloc(sizeof(struct ksmbd_rpc_command) + payload_sz + 1);
867 	if (!msg)
868 		return NULL;
869 
870 	msg->type = KSMBD_EVENT_RPC_REQUEST;
871 	req = (struct ksmbd_rpc_command *)msg->payload;
872 	req->handle = handle;
873 	req->flags = ksmbd_session_rpc_method(sess, handle);
874 	req->flags |= rpc_context_flags(sess);
875 	req->flags |= KSMBD_RPC_IOCTL_METHOD;
876 	req->payload_sz = payload_sz;
877 	memcpy(req->payload, payload, payload_sz);
878 
879 	resp = ipc_msg_send_request(msg, req->handle);
880 	ipc_msg_free(msg);
881 	return resp;
882 }
883 
__ipc_heartbeat(void)884 static int __ipc_heartbeat(void)
885 {
886 	unsigned long delta;
887 
888 	if (!ksmbd_server_running())
889 		return 0;
890 
891 	if (time_after(jiffies, server_conf.ipc_last_active)) {
892 		delta = (jiffies - server_conf.ipc_last_active);
893 	} else {
894 		ipc_update_last_active();
895 		schedule_delayed_work(&ipc_timer_work,
896 				      server_conf.ipc_timeout);
897 		return 0;
898 	}
899 
900 	if (delta < server_conf.ipc_timeout) {
901 		schedule_delayed_work(&ipc_timer_work,
902 				      server_conf.ipc_timeout - delta);
903 		return 0;
904 	}
905 
906 	if (ksmbd_ipc_heartbeat_request() == 0) {
907 		schedule_delayed_work(&ipc_timer_work,
908 				      server_conf.ipc_timeout);
909 		return 0;
910 	}
911 
912 	mutex_lock(&startup_lock);
913 	WRITE_ONCE(server_conf.state, SERVER_STATE_RESETTING);
914 	server_conf.ipc_last_active = 0;
915 	ksmbd_tools_pid = 0;
916 	pr_err("No IPC daemon response for %lus\n", delta / HZ);
917 	mutex_unlock(&startup_lock);
918 	return -EINVAL;
919 }
920 
ipc_timer_heartbeat(struct work_struct * w)921 static void ipc_timer_heartbeat(struct work_struct *w)
922 {
923 	if (__ipc_heartbeat())
924 		server_queue_ctrl_reset_work();
925 }
926 
ksmbd_ipc_id_alloc(void)927 int ksmbd_ipc_id_alloc(void)
928 {
929 	return ksmbd_acquire_id(&ipc_ida);
930 }
931 
ksmbd_rpc_id_free(int handle)932 void ksmbd_rpc_id_free(int handle)
933 {
934 	ksmbd_release_id(&ipc_ida, handle);
935 }
936 
ksmbd_ipc_release(void)937 void ksmbd_ipc_release(void)
938 {
939 	cancel_delayed_work_sync(&ipc_timer_work);
940 	genl_unregister_family(&ksmbd_genl_family);
941 }
942 
ksmbd_ipc_soft_reset(void)943 void ksmbd_ipc_soft_reset(void)
944 {
945 	mutex_lock(&startup_lock);
946 	ksmbd_tools_pid = 0;
947 	cancel_delayed_work_sync(&ipc_timer_work);
948 	mutex_unlock(&startup_lock);
949 }
950 
ksmbd_ipc_init(void)951 int ksmbd_ipc_init(void)
952 {
953 	int ret = 0;
954 
955 	ksmbd_nl_init_fixup();
956 	INIT_DELAYED_WORK(&ipc_timer_work, ipc_timer_heartbeat);
957 
958 	ret = genl_register_family(&ksmbd_genl_family);
959 	if (ret) {
960 		pr_err("Failed to register KSMBD netlink interface %d\n", ret);
961 		cancel_delayed_work_sync(&ipc_timer_work);
962 	}
963 
964 	return ret;
965 }
966