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