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 int 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));
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));
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);
210 pr_err("failed to announce new service\n");
217 static struct qrtr_server *server_add(unsigned int service,
218 unsigned int instance,
219 unsigned int node_id,
222 struct qrtr_server *srv;
223 struct qrtr_server *old;
224 struct qrtr_node *node;
226 if (!service || !port)
229 srv = kzalloc(sizeof(*srv), GFP_KERNEL);
233 srv->service = service;
234 srv->instance = instance;
238 node = node_get(node_id);
242 /* Delete the old server on the same port */
243 old = xa_store(&node->servers, port, srv, GFP_KERNEL);
245 if (xa_is_err(old)) {
246 pr_err("failed to add server [0x%x:0x%x] ret:%d\n",
247 srv->service, srv->instance, xa_err(old));
254 trace_qrtr_ns_server_add(srv->service, srv->instance,
255 srv->node, srv->port);
264 static int server_del(struct qrtr_node *node, unsigned int port, bool bcast)
266 struct qrtr_lookup *lookup;
267 struct qrtr_server *srv;
268 struct list_head *li;
270 srv = xa_load(&node->servers, port);
274 xa_erase(&node->servers, port);
276 /* Broadcast the removal of local servers */
277 if (srv->node == qrtr_ns.local_node && bcast)
278 service_announce_del(&qrtr_ns.bcast_sq, srv);
280 /* Announce the service's disappearance to observers */
281 list_for_each(li, &qrtr_ns.lookups) {
282 lookup = container_of(li, struct qrtr_lookup, li);
283 if (lookup->service && lookup->service != srv->service)
285 if (lookup->instance && lookup->instance != srv->instance)
288 lookup_notify(&lookup->sq, srv, false);
296 static int say_hello(struct sockaddr_qrtr *dest)
298 struct qrtr_ctrl_pkt pkt;
299 struct msghdr msg = { };
304 iv.iov_len = sizeof(pkt);
306 memset(&pkt, 0, sizeof(pkt));
307 pkt.cmd = cpu_to_le32(QRTR_TYPE_HELLO);
309 msg.msg_name = (struct sockaddr *)dest;
310 msg.msg_namelen = sizeof(*dest);
312 ret = kernel_sendmsg(qrtr_ns.sock, &msg, &iv, 1, sizeof(pkt));
314 pr_err("failed to send hello msg\n");
319 /* Announce the list of servers registered on the local node */
320 static int ctrl_cmd_hello(struct sockaddr_qrtr *sq)
328 return announce_servers(sq);
331 static int ctrl_cmd_bye(struct sockaddr_qrtr *from)
333 struct qrtr_node *local_node;
334 struct qrtr_ctrl_pkt pkt;
335 struct qrtr_server *srv;
336 struct sockaddr_qrtr sq;
337 struct msghdr msg = { };
338 struct qrtr_node *node;
344 iv.iov_len = sizeof(pkt);
346 node = node_get(from->sq_node);
350 /* Advertise removal of this client to all servers of remote node */
351 xa_for_each(&node->servers, index, srv)
352 server_del(node, srv->port, true);
354 /* Advertise the removal of this client to all local servers */
355 local_node = node_get(qrtr_ns.local_node);
359 memset(&pkt, 0, sizeof(pkt));
360 pkt.cmd = cpu_to_le32(QRTR_TYPE_BYE);
361 pkt.client.node = cpu_to_le32(from->sq_node);
363 xa_for_each(&local_node->servers, index, srv) {
364 sq.sq_family = AF_QIPCRTR;
365 sq.sq_node = srv->node;
366 sq.sq_port = srv->port;
368 msg.msg_name = (struct sockaddr *)&sq;
369 msg.msg_namelen = sizeof(sq);
371 ret = kernel_sendmsg(qrtr_ns.sock, &msg, &iv, 1, sizeof(pkt));
373 pr_err("failed to send bye cmd\n");
380 static int ctrl_cmd_del_client(struct sockaddr_qrtr *from,
381 unsigned int node_id, unsigned int port)
383 struct qrtr_node *local_node;
384 struct qrtr_lookup *lookup;
385 struct qrtr_ctrl_pkt pkt;
386 struct msghdr msg = { };
387 struct qrtr_server *srv;
388 struct sockaddr_qrtr sq;
389 struct qrtr_node *node;
390 struct list_head *tmp;
391 struct list_head *li;
397 iv.iov_len = sizeof(pkt);
399 /* Don't accept spoofed messages */
400 if (from->sq_node != node_id)
403 /* Local DEL_CLIENT messages comes from the port being closed */
404 if (from->sq_node == qrtr_ns.local_node && from->sq_port != port)
407 /* Remove any lookups by this client */
408 list_for_each_safe(li, tmp, &qrtr_ns.lookups) {
409 lookup = container_of(li, struct qrtr_lookup, li);
410 if (lookup->sq.sq_node != node_id)
412 if (lookup->sq.sq_port != port)
415 list_del(&lookup->li);
419 /* Remove the server belonging to this port but don't broadcast
420 * DEL_SERVER. Neighbours would've already removed the server belonging
421 * to this port due to the DEL_CLIENT broadcast from qrtr_port_remove().
423 node = node_get(node_id);
425 server_del(node, port, false);
427 /* Advertise the removal of this client to all local servers */
428 local_node = node_get(qrtr_ns.local_node);
432 memset(&pkt, 0, sizeof(pkt));
433 pkt.cmd = cpu_to_le32(QRTR_TYPE_DEL_CLIENT);
434 pkt.client.node = cpu_to_le32(node_id);
435 pkt.client.port = cpu_to_le32(port);
437 xa_for_each(&local_node->servers, index, srv) {
438 sq.sq_family = AF_QIPCRTR;
439 sq.sq_node = srv->node;
440 sq.sq_port = srv->port;
442 msg.msg_name = (struct sockaddr *)&sq;
443 msg.msg_namelen = sizeof(sq);
445 ret = kernel_sendmsg(qrtr_ns.sock, &msg, &iv, 1, sizeof(pkt));
447 pr_err("failed to send del client cmd\n");
454 static int ctrl_cmd_new_server(struct sockaddr_qrtr *from,
455 unsigned int service, unsigned int instance,
456 unsigned int node_id, unsigned int port)
458 struct qrtr_lookup *lookup;
459 struct qrtr_server *srv;
460 struct list_head *li;
463 /* Ignore specified node and port for local servers */
464 if (from->sq_node == qrtr_ns.local_node) {
465 node_id = from->sq_node;
466 port = from->sq_port;
469 srv = server_add(service, instance, node_id, port);
473 if (srv->node == qrtr_ns.local_node) {
474 ret = service_announce_new(&qrtr_ns.bcast_sq, srv);
476 pr_err("failed to announce new service\n");
481 /* Notify any potential lookups about the new server */
482 list_for_each(li, &qrtr_ns.lookups) {
483 lookup = container_of(li, struct qrtr_lookup, li);
484 if (lookup->service && lookup->service != service)
486 if (lookup->instance && lookup->instance != instance)
489 lookup_notify(&lookup->sq, srv, true);
495 static int ctrl_cmd_del_server(struct sockaddr_qrtr *from,
496 unsigned int service, unsigned int instance,
497 unsigned int node_id, unsigned int port)
499 struct qrtr_node *node;
501 /* Ignore specified node and port for local servers*/
502 if (from->sq_node == qrtr_ns.local_node) {
503 node_id = from->sq_node;
504 port = from->sq_port;
507 /* Local servers may only unregister themselves */
508 if (from->sq_node == qrtr_ns.local_node && from->sq_port != port)
511 node = node_get(node_id);
515 return server_del(node, port, true);
518 static int ctrl_cmd_new_lookup(struct sockaddr_qrtr *from,
519 unsigned int service, unsigned int instance)
521 struct qrtr_server_filter filter;
522 struct qrtr_lookup *lookup;
523 struct qrtr_server *srv;
524 struct qrtr_node *node;
525 unsigned long node_idx;
526 unsigned long srv_idx;
528 /* Accept only local observers */
529 if (from->sq_node != qrtr_ns.local_node)
532 lookup = kzalloc(sizeof(*lookup), GFP_KERNEL);
537 lookup->service = service;
538 lookup->instance = instance;
539 list_add_tail(&lookup->li, &qrtr_ns.lookups);
541 memset(&filter, 0, sizeof(filter));
542 filter.service = service;
543 filter.instance = instance;
545 xa_for_each(&nodes, node_idx, node) {
546 xa_for_each(&node->servers, srv_idx, srv) {
547 if (!server_match(srv, &filter))
550 lookup_notify(from, srv, true);
554 /* Empty notification, to indicate end of listing */
555 lookup_notify(from, NULL, true);
560 static void ctrl_cmd_del_lookup(struct sockaddr_qrtr *from,
561 unsigned int service, unsigned int instance)
563 struct qrtr_lookup *lookup;
564 struct list_head *tmp;
565 struct list_head *li;
567 list_for_each_safe(li, tmp, &qrtr_ns.lookups) {
568 lookup = container_of(li, struct qrtr_lookup, li);
569 if (lookup->sq.sq_node != from->sq_node)
571 if (lookup->sq.sq_port != from->sq_port)
573 if (lookup->service != service)
575 if (lookup->instance && lookup->instance != instance)
578 list_del(&lookup->li);
583 static void qrtr_ns_worker(struct work_struct *work)
585 const struct qrtr_ctrl_pkt *pkt;
586 size_t recv_buf_size = 4096;
587 struct sockaddr_qrtr sq;
588 struct msghdr msg = { };
595 msg.msg_name = (struct sockaddr *)&sq;
596 msg.msg_namelen = sizeof(sq);
598 recv_buf = kzalloc(recv_buf_size, GFP_KERNEL);
603 iv.iov_base = recv_buf;
604 iv.iov_len = recv_buf_size;
606 msglen = kernel_recvmsg(qrtr_ns.sock, &msg, &iv, 1,
607 iv.iov_len, MSG_DONTWAIT);
609 if (msglen == -EAGAIN)
613 pr_err("error receiving packet: %zd\n", msglen);
618 cmd = le32_to_cpu(pkt->cmd);
619 if (cmd < ARRAY_SIZE(qrtr_ctrl_pkt_strings) &&
620 qrtr_ctrl_pkt_strings[cmd])
621 trace_qrtr_ns_message(qrtr_ctrl_pkt_strings[cmd],
622 sq.sq_node, sq.sq_port);
626 case QRTR_TYPE_HELLO:
627 ret = ctrl_cmd_hello(&sq);
630 ret = ctrl_cmd_bye(&sq);
632 case QRTR_TYPE_DEL_CLIENT:
633 ret = ctrl_cmd_del_client(&sq,
634 le32_to_cpu(pkt->client.node),
635 le32_to_cpu(pkt->client.port));
637 case QRTR_TYPE_NEW_SERVER:
638 ret = ctrl_cmd_new_server(&sq,
639 le32_to_cpu(pkt->server.service),
640 le32_to_cpu(pkt->server.instance),
641 le32_to_cpu(pkt->server.node),
642 le32_to_cpu(pkt->server.port));
644 case QRTR_TYPE_DEL_SERVER:
645 ret = ctrl_cmd_del_server(&sq,
646 le32_to_cpu(pkt->server.service),
647 le32_to_cpu(pkt->server.instance),
648 le32_to_cpu(pkt->server.node),
649 le32_to_cpu(pkt->server.port));
653 case QRTR_TYPE_RESUME_TX:
655 case QRTR_TYPE_NEW_LOOKUP:
656 ret = ctrl_cmd_new_lookup(&sq,
657 le32_to_cpu(pkt->server.service),
658 le32_to_cpu(pkt->server.instance));
660 case QRTR_TYPE_DEL_LOOKUP:
661 ctrl_cmd_del_lookup(&sq,
662 le32_to_cpu(pkt->server.service),
663 le32_to_cpu(pkt->server.instance));
668 pr_err("failed while handling packet from %d:%d",
669 sq.sq_node, sq.sq_port);
675 static void qrtr_ns_data_ready(struct sock *sk)
677 trace_sk_data_ready(sk);
679 queue_work(qrtr_ns.workqueue, &qrtr_ns.work);
682 int qrtr_ns_init(void)
684 struct sockaddr_qrtr sq;
687 INIT_LIST_HEAD(&qrtr_ns.lookups);
688 INIT_WORK(&qrtr_ns.work, qrtr_ns_worker);
690 ret = sock_create_kern(&init_net, AF_QIPCRTR, SOCK_DGRAM,
691 PF_QIPCRTR, &qrtr_ns.sock);
695 ret = kernel_getsockname(qrtr_ns.sock, (struct sockaddr *)&sq);
697 pr_err("failed to get socket name\n");
701 qrtr_ns.workqueue = alloc_ordered_workqueue("qrtr_ns_handler", 0);
702 if (!qrtr_ns.workqueue) {
707 qrtr_ns.sock->sk->sk_data_ready = qrtr_ns_data_ready;
709 sq.sq_port = QRTR_PORT_CTRL;
710 qrtr_ns.local_node = sq.sq_node;
712 ret = kernel_bind(qrtr_ns.sock, (struct sockaddr *)&sq, sizeof(sq));
714 pr_err("failed to bind to socket\n");
718 qrtr_ns.bcast_sq.sq_family = AF_QIPCRTR;
719 qrtr_ns.bcast_sq.sq_node = QRTR_NODE_BCAST;
720 qrtr_ns.bcast_sq.sq_port = QRTR_PORT_CTRL;
722 ret = say_hello(&qrtr_ns.bcast_sq);
729 destroy_workqueue(qrtr_ns.workqueue);
731 sock_release(qrtr_ns.sock);
734 EXPORT_SYMBOL_GPL(qrtr_ns_init);
736 void qrtr_ns_remove(void)
738 cancel_work_sync(&qrtr_ns.work);
739 destroy_workqueue(qrtr_ns.workqueue);
740 sock_release(qrtr_ns.sock);
742 EXPORT_SYMBOL_GPL(qrtr_ns_remove);
744 MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
745 MODULE_DESCRIPTION("Qualcomm IPC Router Nameservice");
746 MODULE_LICENSE("Dual BSD/GPL");