1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright (c) 2015, Sony Mobile Communications Inc.
4 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
5 * Copyright (c) 2020, Linaro Ltd.
8 #include <linux/module.h>
9 #include <linux/qrtr.h>
10 #include <linux/workqueue.h>
15 #include <trace/events/sock.h>
16 #define CREATE_TRACE_POINTS
17 #include <trace/events/qrtr.h>
19 static DEFINE_XARRAY(nodes
);
23 struct sockaddr_qrtr bcast_sq
;
24 struct list_head lookups
;
25 struct workqueue_struct
*workqueue
;
26 struct work_struct work
;
30 static const char * const qrtr_ctrl_pkt_strings
[] = {
31 [QRTR_TYPE_HELLO
] = "hello",
32 [QRTR_TYPE_BYE
] = "bye",
33 [QRTR_TYPE_NEW_SERVER
] = "new-server",
34 [QRTR_TYPE_DEL_SERVER
] = "del-server",
35 [QRTR_TYPE_DEL_CLIENT
] = "del-client",
36 [QRTR_TYPE_RESUME_TX
] = "resume-tx",
37 [QRTR_TYPE_EXIT
] = "exit",
38 [QRTR_TYPE_PING
] = "ping",
39 [QRTR_TYPE_NEW_LOOKUP
] = "new-lookup",
40 [QRTR_TYPE_DEL_LOOKUP
] = "del-lookup",
43 struct qrtr_server_filter
{
45 unsigned int instance
;
51 unsigned int instance
;
53 struct sockaddr_qrtr sq
;
59 unsigned int instance
;
69 struct xarray servers
;
72 static struct qrtr_node
*node_get(unsigned int node_id
)
74 struct qrtr_node
*node
;
76 node
= xa_load(&nodes
, node_id
);
80 /* If node didn't exist, allocate and insert it to the tree */
81 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
86 xa_init(&node
->servers
);
88 if (xa_store(&nodes
, node_id
, node
, GFP_KERNEL
)) {
96 static int server_match(const struct qrtr_server
*srv
,
97 const struct qrtr_server_filter
*f
)
99 unsigned int ifilter
= f
->ifilter
;
101 if (f
->service
!= 0 && srv
->service
!= f
->service
)
103 if (!ifilter
&& f
->instance
)
106 return (srv
->instance
& ifilter
) == f
->instance
;
109 static int service_announce_new(struct sockaddr_qrtr
*dest
,
110 struct qrtr_server
*srv
)
112 struct qrtr_ctrl_pkt pkt
;
113 struct msghdr msg
= { };
116 trace_qrtr_ns_service_announce_new(srv
->service
, srv
->instance
,
117 srv
->node
, srv
->port
);
120 iv
.iov_len
= sizeof(pkt
);
122 memset(&pkt
, 0, sizeof(pkt
));
123 pkt
.cmd
= cpu_to_le32(QRTR_TYPE_NEW_SERVER
);
124 pkt
.server
.service
= cpu_to_le32(srv
->service
);
125 pkt
.server
.instance
= cpu_to_le32(srv
->instance
);
126 pkt
.server
.node
= cpu_to_le32(srv
->node
);
127 pkt
.server
.port
= cpu_to_le32(srv
->port
);
129 msg
.msg_name
= (struct sockaddr
*)dest
;
130 msg
.msg_namelen
= sizeof(*dest
);
132 return kernel_sendmsg(qrtr_ns
.sock
, &msg
, &iv
, 1, sizeof(pkt
));
135 static void service_announce_del(struct sockaddr_qrtr
*dest
,
136 struct qrtr_server
*srv
)
138 struct qrtr_ctrl_pkt pkt
;
139 struct msghdr msg
= { };
143 trace_qrtr_ns_service_announce_del(srv
->service
, srv
->instance
,
144 srv
->node
, srv
->port
);
147 iv
.iov_len
= sizeof(pkt
);
149 memset(&pkt
, 0, sizeof(pkt
));
150 pkt
.cmd
= cpu_to_le32(QRTR_TYPE_DEL_SERVER
);
151 pkt
.server
.service
= cpu_to_le32(srv
->service
);
152 pkt
.server
.instance
= cpu_to_le32(srv
->instance
);
153 pkt
.server
.node
= cpu_to_le32(srv
->node
);
154 pkt
.server
.port
= cpu_to_le32(srv
->port
);
156 msg
.msg_name
= (struct sockaddr
*)dest
;
157 msg
.msg_namelen
= sizeof(*dest
);
159 ret
= kernel_sendmsg(qrtr_ns
.sock
, &msg
, &iv
, 1, sizeof(pkt
));
160 if (ret
< 0 && ret
!= -ENODEV
)
161 pr_err("failed to announce del service\n");
166 static void lookup_notify(struct sockaddr_qrtr
*to
, struct qrtr_server
*srv
,
169 struct qrtr_ctrl_pkt pkt
;
170 struct msghdr msg
= { };
175 iv
.iov_len
= sizeof(pkt
);
177 memset(&pkt
, 0, sizeof(pkt
));
178 pkt
.cmd
= new ? cpu_to_le32(QRTR_TYPE_NEW_SERVER
) :
179 cpu_to_le32(QRTR_TYPE_DEL_SERVER
);
181 pkt
.server
.service
= cpu_to_le32(srv
->service
);
182 pkt
.server
.instance
= cpu_to_le32(srv
->instance
);
183 pkt
.server
.node
= cpu_to_le32(srv
->node
);
184 pkt
.server
.port
= cpu_to_le32(srv
->port
);
187 msg
.msg_name
= (struct sockaddr
*)to
;
188 msg
.msg_namelen
= sizeof(*to
);
190 ret
= kernel_sendmsg(qrtr_ns
.sock
, &msg
, &iv
, 1, sizeof(pkt
));
191 if (ret
< 0 && ret
!= -ENODEV
)
192 pr_err("failed to send lookup notification\n");
195 static int announce_servers(struct sockaddr_qrtr
*sq
)
197 struct qrtr_server
*srv
;
198 struct qrtr_node
*node
;
202 node
= node_get(qrtr_ns
.local_node
);
206 /* Announce the list of servers registered in this node */
207 xa_for_each(&node
->servers
, index
, srv
) {
208 ret
= service_announce_new(sq
, srv
);
213 pr_err("failed to announce new service\n");
220 static struct qrtr_server
*server_add(unsigned int service
,
221 unsigned int instance
,
222 unsigned int node_id
,
225 struct qrtr_server
*srv
;
226 struct qrtr_server
*old
;
227 struct qrtr_node
*node
;
229 if (!service
|| !port
)
232 srv
= kzalloc(sizeof(*srv
), GFP_KERNEL
);
236 srv
->service
= service
;
237 srv
->instance
= instance
;
241 node
= node_get(node_id
);
245 /* Delete the old server on the same port */
246 old
= xa_store(&node
->servers
, port
, srv
, GFP_KERNEL
);
248 if (xa_is_err(old
)) {
249 pr_err("failed to add server [0x%x:0x%x] ret:%d\n",
250 srv
->service
, srv
->instance
, xa_err(old
));
257 trace_qrtr_ns_server_add(srv
->service
, srv
->instance
,
258 srv
->node
, srv
->port
);
267 static int server_del(struct qrtr_node
*node
, unsigned int port
, bool bcast
)
269 struct qrtr_lookup
*lookup
;
270 struct qrtr_server
*srv
;
271 struct list_head
*li
;
273 srv
= xa_load(&node
->servers
, port
);
277 xa_erase(&node
->servers
, port
);
279 /* Broadcast the removal of local servers */
280 if (srv
->node
== qrtr_ns
.local_node
&& bcast
)
281 service_announce_del(&qrtr_ns
.bcast_sq
, srv
);
283 /* Announce the service's disappearance to observers */
284 list_for_each(li
, &qrtr_ns
.lookups
) {
285 lookup
= container_of(li
, struct qrtr_lookup
, li
);
286 if (lookup
->service
&& lookup
->service
!= srv
->service
)
288 if (lookup
->instance
&& lookup
->instance
!= srv
->instance
)
291 lookup_notify(&lookup
->sq
, srv
, false);
299 static int say_hello(struct sockaddr_qrtr
*dest
)
301 struct qrtr_ctrl_pkt pkt
;
302 struct msghdr msg
= { };
307 iv
.iov_len
= sizeof(pkt
);
309 memset(&pkt
, 0, sizeof(pkt
));
310 pkt
.cmd
= cpu_to_le32(QRTR_TYPE_HELLO
);
312 msg
.msg_name
= (struct sockaddr
*)dest
;
313 msg
.msg_namelen
= sizeof(*dest
);
315 ret
= kernel_sendmsg(qrtr_ns
.sock
, &msg
, &iv
, 1, sizeof(pkt
));
317 pr_err("failed to send hello msg\n");
322 /* Announce the list of servers registered on the local node */
323 static int ctrl_cmd_hello(struct sockaddr_qrtr
*sq
)
331 return announce_servers(sq
);
334 static int ctrl_cmd_bye(struct sockaddr_qrtr
*from
)
336 struct qrtr_node
*local_node
;
337 struct qrtr_ctrl_pkt pkt
;
338 struct qrtr_server
*srv
;
339 struct sockaddr_qrtr sq
;
340 struct msghdr msg
= { };
341 struct qrtr_node
*node
;
347 iv
.iov_len
= sizeof(pkt
);
349 node
= node_get(from
->sq_node
);
353 /* Advertise removal of this client to all servers of remote node */
354 xa_for_each(&node
->servers
, index
, srv
)
355 server_del(node
, srv
->port
, true);
357 /* Advertise the removal of this client to all local servers */
358 local_node
= node_get(qrtr_ns
.local_node
);
362 memset(&pkt
, 0, sizeof(pkt
));
363 pkt
.cmd
= cpu_to_le32(QRTR_TYPE_BYE
);
364 pkt
.client
.node
= cpu_to_le32(from
->sq_node
);
366 xa_for_each(&local_node
->servers
, index
, srv
) {
367 sq
.sq_family
= AF_QIPCRTR
;
368 sq
.sq_node
= srv
->node
;
369 sq
.sq_port
= srv
->port
;
371 msg
.msg_name
= (struct sockaddr
*)&sq
;
372 msg
.msg_namelen
= sizeof(sq
);
374 ret
= kernel_sendmsg(qrtr_ns
.sock
, &msg
, &iv
, 1, sizeof(pkt
));
375 if (ret
< 0 && ret
!= -ENODEV
) {
376 pr_err("failed to send bye cmd\n");
383 static int ctrl_cmd_del_client(struct sockaddr_qrtr
*from
,
384 unsigned int node_id
, unsigned int port
)
386 struct qrtr_node
*local_node
;
387 struct qrtr_lookup
*lookup
;
388 struct qrtr_ctrl_pkt pkt
;
389 struct msghdr msg
= { };
390 struct qrtr_server
*srv
;
391 struct sockaddr_qrtr sq
;
392 struct qrtr_node
*node
;
393 struct list_head
*tmp
;
394 struct list_head
*li
;
400 iv
.iov_len
= sizeof(pkt
);
402 /* Don't accept spoofed messages */
403 if (from
->sq_node
!= node_id
)
406 /* Local DEL_CLIENT messages comes from the port being closed */
407 if (from
->sq_node
== qrtr_ns
.local_node
&& from
->sq_port
!= port
)
410 /* Remove any lookups by this client */
411 list_for_each_safe(li
, tmp
, &qrtr_ns
.lookups
) {
412 lookup
= container_of(li
, struct qrtr_lookup
, li
);
413 if (lookup
->sq
.sq_node
!= node_id
)
415 if (lookup
->sq
.sq_port
!= port
)
418 list_del(&lookup
->li
);
422 /* Remove the server belonging to this port but don't broadcast
423 * DEL_SERVER. Neighbours would've already removed the server belonging
424 * to this port due to the DEL_CLIENT broadcast from qrtr_port_remove().
426 node
= node_get(node_id
);
428 server_del(node
, port
, false);
430 /* Advertise the removal of this client to all local servers */
431 local_node
= node_get(qrtr_ns
.local_node
);
435 memset(&pkt
, 0, sizeof(pkt
));
436 pkt
.cmd
= cpu_to_le32(QRTR_TYPE_DEL_CLIENT
);
437 pkt
.client
.node
= cpu_to_le32(node_id
);
438 pkt
.client
.port
= cpu_to_le32(port
);
440 xa_for_each(&local_node
->servers
, index
, srv
) {
441 sq
.sq_family
= AF_QIPCRTR
;
442 sq
.sq_node
= srv
->node
;
443 sq
.sq_port
= srv
->port
;
445 msg
.msg_name
= (struct sockaddr
*)&sq
;
446 msg
.msg_namelen
= sizeof(sq
);
448 ret
= kernel_sendmsg(qrtr_ns
.sock
, &msg
, &iv
, 1, sizeof(pkt
));
449 if (ret
< 0 && ret
!= -ENODEV
) {
450 pr_err("failed to send del client cmd\n");
457 static int ctrl_cmd_new_server(struct sockaddr_qrtr
*from
,
458 unsigned int service
, unsigned int instance
,
459 unsigned int node_id
, unsigned int port
)
461 struct qrtr_lookup
*lookup
;
462 struct qrtr_server
*srv
;
463 struct list_head
*li
;
466 /* Ignore specified node and port for local servers */
467 if (from
->sq_node
== qrtr_ns
.local_node
) {
468 node_id
= from
->sq_node
;
469 port
= from
->sq_port
;
472 srv
= server_add(service
, instance
, node_id
, port
);
476 if (srv
->node
== qrtr_ns
.local_node
) {
477 ret
= service_announce_new(&qrtr_ns
.bcast_sq
, srv
);
479 pr_err("failed to announce new service\n");
484 /* Notify any potential lookups about the new server */
485 list_for_each(li
, &qrtr_ns
.lookups
) {
486 lookup
= container_of(li
, struct qrtr_lookup
, li
);
487 if (lookup
->service
&& lookup
->service
!= service
)
489 if (lookup
->instance
&& lookup
->instance
!= instance
)
492 lookup_notify(&lookup
->sq
, srv
, true);
498 static int ctrl_cmd_del_server(struct sockaddr_qrtr
*from
,
499 unsigned int service
, unsigned int instance
,
500 unsigned int node_id
, unsigned int port
)
502 struct qrtr_node
*node
;
504 /* Ignore specified node and port for local servers*/
505 if (from
->sq_node
== qrtr_ns
.local_node
) {
506 node_id
= from
->sq_node
;
507 port
= from
->sq_port
;
510 /* Local servers may only unregister themselves */
511 if (from
->sq_node
== qrtr_ns
.local_node
&& from
->sq_port
!= port
)
514 node
= node_get(node_id
);
518 server_del(node
, port
, true);
523 static int ctrl_cmd_new_lookup(struct sockaddr_qrtr
*from
,
524 unsigned int service
, unsigned int instance
)
526 struct qrtr_server_filter filter
;
527 struct qrtr_lookup
*lookup
;
528 struct qrtr_server
*srv
;
529 struct qrtr_node
*node
;
530 unsigned long node_idx
;
531 unsigned long srv_idx
;
533 /* Accept only local observers */
534 if (from
->sq_node
!= qrtr_ns
.local_node
)
537 lookup
= kzalloc(sizeof(*lookup
), GFP_KERNEL
);
542 lookup
->service
= service
;
543 lookup
->instance
= instance
;
544 list_add_tail(&lookup
->li
, &qrtr_ns
.lookups
);
546 memset(&filter
, 0, sizeof(filter
));
547 filter
.service
= service
;
548 filter
.instance
= instance
;
550 xa_for_each(&nodes
, node_idx
, node
) {
551 xa_for_each(&node
->servers
, srv_idx
, srv
) {
552 if (!server_match(srv
, &filter
))
555 lookup_notify(from
, srv
, true);
559 /* Empty notification, to indicate end of listing */
560 lookup_notify(from
, NULL
, true);
565 static void ctrl_cmd_del_lookup(struct sockaddr_qrtr
*from
,
566 unsigned int service
, unsigned int instance
)
568 struct qrtr_lookup
*lookup
;
569 struct list_head
*tmp
;
570 struct list_head
*li
;
572 list_for_each_safe(li
, tmp
, &qrtr_ns
.lookups
) {
573 lookup
= container_of(li
, struct qrtr_lookup
, li
);
574 if (lookup
->sq
.sq_node
!= from
->sq_node
)
576 if (lookup
->sq
.sq_port
!= from
->sq_port
)
578 if (lookup
->service
!= service
)
580 if (lookup
->instance
&& lookup
->instance
!= instance
)
583 list_del(&lookup
->li
);
588 static void qrtr_ns_worker(struct work_struct
*work
)
590 const struct qrtr_ctrl_pkt
*pkt
;
591 size_t recv_buf_size
= 4096;
592 struct sockaddr_qrtr sq
;
593 struct msghdr msg
= { };
600 msg
.msg_name
= (struct sockaddr
*)&sq
;
601 msg
.msg_namelen
= sizeof(sq
);
603 recv_buf
= kzalloc(recv_buf_size
, GFP_KERNEL
);
608 iv
.iov_base
= recv_buf
;
609 iv
.iov_len
= recv_buf_size
;
611 msglen
= kernel_recvmsg(qrtr_ns
.sock
, &msg
, &iv
, 1,
612 iv
.iov_len
, MSG_DONTWAIT
);
614 if (msglen
== -EAGAIN
)
618 pr_err("error receiving packet: %zd\n", msglen
);
623 cmd
= le32_to_cpu(pkt
->cmd
);
624 if (cmd
< ARRAY_SIZE(qrtr_ctrl_pkt_strings
) &&
625 qrtr_ctrl_pkt_strings
[cmd
])
626 trace_qrtr_ns_message(qrtr_ctrl_pkt_strings
[cmd
],
627 sq
.sq_node
, sq
.sq_port
);
631 case QRTR_TYPE_HELLO
:
632 ret
= ctrl_cmd_hello(&sq
);
635 ret
= ctrl_cmd_bye(&sq
);
637 case QRTR_TYPE_DEL_CLIENT
:
638 ret
= ctrl_cmd_del_client(&sq
,
639 le32_to_cpu(pkt
->client
.node
),
640 le32_to_cpu(pkt
->client
.port
));
642 case QRTR_TYPE_NEW_SERVER
:
643 ret
= ctrl_cmd_new_server(&sq
,
644 le32_to_cpu(pkt
->server
.service
),
645 le32_to_cpu(pkt
->server
.instance
),
646 le32_to_cpu(pkt
->server
.node
),
647 le32_to_cpu(pkt
->server
.port
));
649 case QRTR_TYPE_DEL_SERVER
:
650 ret
= ctrl_cmd_del_server(&sq
,
651 le32_to_cpu(pkt
->server
.service
),
652 le32_to_cpu(pkt
->server
.instance
),
653 le32_to_cpu(pkt
->server
.node
),
654 le32_to_cpu(pkt
->server
.port
));
658 case QRTR_TYPE_RESUME_TX
:
660 case QRTR_TYPE_NEW_LOOKUP
:
661 ret
= ctrl_cmd_new_lookup(&sq
,
662 le32_to_cpu(pkt
->server
.service
),
663 le32_to_cpu(pkt
->server
.instance
));
665 case QRTR_TYPE_DEL_LOOKUP
:
666 ctrl_cmd_del_lookup(&sq
,
667 le32_to_cpu(pkt
->server
.service
),
668 le32_to_cpu(pkt
->server
.instance
));
673 pr_err("failed while handling packet from %d:%d",
674 sq
.sq_node
, sq
.sq_port
);
680 static void qrtr_ns_data_ready(struct sock
*sk
)
682 trace_sk_data_ready(sk
);
684 queue_work(qrtr_ns
.workqueue
, &qrtr_ns
.work
);
687 int qrtr_ns_init(void)
689 struct sockaddr_qrtr sq
;
692 INIT_LIST_HEAD(&qrtr_ns
.lookups
);
693 INIT_WORK(&qrtr_ns
.work
, qrtr_ns_worker
);
695 ret
= sock_create_kern(&init_net
, AF_QIPCRTR
, SOCK_DGRAM
,
696 PF_QIPCRTR
, &qrtr_ns
.sock
);
700 ret
= kernel_getsockname(qrtr_ns
.sock
, (struct sockaddr
*)&sq
);
702 pr_err("failed to get socket name\n");
706 qrtr_ns
.workqueue
= alloc_ordered_workqueue("qrtr_ns_handler", 0);
707 if (!qrtr_ns
.workqueue
) {
712 qrtr_ns
.sock
->sk
->sk_data_ready
= qrtr_ns_data_ready
;
714 sq
.sq_port
= QRTR_PORT_CTRL
;
715 qrtr_ns
.local_node
= sq
.sq_node
;
717 ret
= kernel_bind(qrtr_ns
.sock
, (struct sockaddr
*)&sq
, sizeof(sq
));
719 pr_err("failed to bind to socket\n");
723 qrtr_ns
.bcast_sq
.sq_family
= AF_QIPCRTR
;
724 qrtr_ns
.bcast_sq
.sq_node
= QRTR_NODE_BCAST
;
725 qrtr_ns
.bcast_sq
.sq_port
= QRTR_PORT_CTRL
;
727 ret
= say_hello(&qrtr_ns
.bcast_sq
);
731 /* As the qrtr ns socket owner and creator is the same module, we have
732 * to decrease the qrtr module reference count to guarantee that it
733 * remains zero after the ns socket is created, otherwise, executing
734 * "rmmod" command is unable to make the qrtr module deleted after the
735 * qrtr module is inserted successfully.
737 * However, the reference count is increased twice in
738 * sock_create_kern(): one is to increase the reference count of owner
739 * of qrtr socket's proto_ops struct; another is to increment the
740 * reference count of owner of qrtr proto struct. Therefore, we must
741 * decrement the module reference count twice to ensure that it keeps
742 * zero after server's listening socket is created. Of course, we
743 * must bump the module reference count twice as well before the socket
746 module_put(qrtr_ns
.sock
->ops
->owner
);
747 module_put(qrtr_ns
.sock
->sk
->sk_prot_creator
->owner
);
752 destroy_workqueue(qrtr_ns
.workqueue
);
754 sock_release(qrtr_ns
.sock
);
757 EXPORT_SYMBOL_GPL(qrtr_ns_init
);
759 void qrtr_ns_remove(void)
761 cancel_work_sync(&qrtr_ns
.work
);
762 destroy_workqueue(qrtr_ns
.workqueue
);
764 /* sock_release() expects the two references that were put during
765 * qrtr_ns_init(). This function is only called during module remove,
766 * so try_stop_module() has already set the refcnt to 0. Use
767 * __module_get() instead of try_module_get() to successfully take two
770 __module_get(qrtr_ns
.sock
->ops
->owner
);
771 __module_get(qrtr_ns
.sock
->sk
->sk_prot_creator
->owner
);
772 sock_release(qrtr_ns
.sock
);
774 EXPORT_SYMBOL_GPL(qrtr_ns_remove
);
776 MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
777 MODULE_DESCRIPTION("Qualcomm IPC Router Nameservice");
778 MODULE_LICENSE("Dual BSD/GPL");