Linux 4.19.133
[linux/fpc-iii.git] / net / qrtr / qrtr.c
blob0baffc9666e605eb384018c6565b1f2af9dfface
1 /*
2 * Copyright (c) 2015, Sony Mobile Communications Inc.
3 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 #include <linux/module.h>
15 #include <linux/netlink.h>
16 #include <linux/qrtr.h>
17 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
19 #include <net/sock.h>
21 #include "qrtr.h"
23 #define QRTR_PROTO_VER_1 1
24 #define QRTR_PROTO_VER_2 3
26 /* auto-bind range */
27 #define QRTR_MIN_EPH_SOCKET 0x4000
28 #define QRTR_MAX_EPH_SOCKET 0x7fff
30 /**
31 * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1
32 * @version: protocol version
33 * @type: packet type; one of QRTR_TYPE_*
34 * @src_node_id: source node
35 * @src_port_id: source port
36 * @confirm_rx: boolean; whether a resume-tx packet should be send in reply
37 * @size: length of packet, excluding this header
38 * @dst_node_id: destination node
39 * @dst_port_id: destination port
41 struct qrtr_hdr_v1 {
42 __le32 version;
43 __le32 type;
44 __le32 src_node_id;
45 __le32 src_port_id;
46 __le32 confirm_rx;
47 __le32 size;
48 __le32 dst_node_id;
49 __le32 dst_port_id;
50 } __packed;
52 /**
53 * struct qrtr_hdr_v2 - (I|R)PCrouter packet header later versions
54 * @version: protocol version
55 * @type: packet type; one of QRTR_TYPE_*
56 * @flags: bitmask of QRTR_FLAGS_*
57 * @optlen: length of optional header data
58 * @size: length of packet, excluding this header and optlen
59 * @src_node_id: source node
60 * @src_port_id: source port
61 * @dst_node_id: destination node
62 * @dst_port_id: destination port
64 struct qrtr_hdr_v2 {
65 u8 version;
66 u8 type;
67 u8 flags;
68 u8 optlen;
69 __le32 size;
70 __le16 src_node_id;
71 __le16 src_port_id;
72 __le16 dst_node_id;
73 __le16 dst_port_id;
76 #define QRTR_FLAGS_CONFIRM_RX BIT(0)
78 struct qrtr_cb {
79 u32 src_node;
80 u32 src_port;
81 u32 dst_node;
82 u32 dst_port;
84 u8 type;
85 u8 confirm_rx;
88 #define QRTR_HDR_MAX_SIZE max_t(size_t, sizeof(struct qrtr_hdr_v1), \
89 sizeof(struct qrtr_hdr_v2))
91 struct qrtr_sock {
92 /* WARNING: sk must be the first member */
93 struct sock sk;
94 struct sockaddr_qrtr us;
95 struct sockaddr_qrtr peer;
98 static inline struct qrtr_sock *qrtr_sk(struct sock *sk)
100 BUILD_BUG_ON(offsetof(struct qrtr_sock, sk) != 0);
101 return container_of(sk, struct qrtr_sock, sk);
104 static unsigned int qrtr_local_nid = -1;
106 /* for node ids */
107 static RADIX_TREE(qrtr_nodes, GFP_KERNEL);
108 /* broadcast list */
109 static LIST_HEAD(qrtr_all_nodes);
110 /* lock for qrtr_nodes, qrtr_all_nodes and node reference */
111 static DEFINE_MUTEX(qrtr_node_lock);
113 /* local port allocation management */
114 static DEFINE_IDR(qrtr_ports);
115 static DEFINE_MUTEX(qrtr_port_lock);
118 * struct qrtr_node - endpoint node
119 * @ep_lock: lock for endpoint management and callbacks
120 * @ep: endpoint
121 * @ref: reference count for node
122 * @nid: node id
123 * @rx_queue: receive queue
124 * @work: scheduled work struct for recv work
125 * @item: list item for broadcast list
127 struct qrtr_node {
128 struct mutex ep_lock;
129 struct qrtr_endpoint *ep;
130 struct kref ref;
131 unsigned int nid;
133 struct sk_buff_head rx_queue;
134 struct work_struct work;
135 struct list_head item;
138 static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,
139 int type, struct sockaddr_qrtr *from,
140 struct sockaddr_qrtr *to);
141 static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,
142 int type, struct sockaddr_qrtr *from,
143 struct sockaddr_qrtr *to);
145 /* Release node resources and free the node.
147 * Do not call directly, use qrtr_node_release. To be used with
148 * kref_put_mutex. As such, the node mutex is expected to be locked on call.
150 static void __qrtr_node_release(struct kref *kref)
152 struct qrtr_node *node = container_of(kref, struct qrtr_node, ref);
154 if (node->nid != QRTR_EP_NID_AUTO)
155 radix_tree_delete(&qrtr_nodes, node->nid);
157 list_del(&node->item);
158 mutex_unlock(&qrtr_node_lock);
160 cancel_work_sync(&node->work);
161 skb_queue_purge(&node->rx_queue);
162 kfree(node);
165 /* Increment reference to node. */
166 static struct qrtr_node *qrtr_node_acquire(struct qrtr_node *node)
168 if (node)
169 kref_get(&node->ref);
170 return node;
173 /* Decrement reference to node and release as necessary. */
174 static void qrtr_node_release(struct qrtr_node *node)
176 if (!node)
177 return;
178 kref_put_mutex(&node->ref, __qrtr_node_release, &qrtr_node_lock);
181 /* Pass an outgoing packet socket buffer to the endpoint driver. */
182 static int qrtr_node_enqueue(struct qrtr_node *node, struct sk_buff *skb,
183 int type, struct sockaddr_qrtr *from,
184 struct sockaddr_qrtr *to)
186 struct qrtr_hdr_v1 *hdr;
187 size_t len = skb->len;
188 int rc = -ENODEV;
190 hdr = skb_push(skb, sizeof(*hdr));
191 hdr->version = cpu_to_le32(QRTR_PROTO_VER_1);
192 hdr->type = cpu_to_le32(type);
193 hdr->src_node_id = cpu_to_le32(from->sq_node);
194 hdr->src_port_id = cpu_to_le32(from->sq_port);
195 if (to->sq_port == QRTR_PORT_CTRL) {
196 hdr->dst_node_id = cpu_to_le32(node->nid);
197 hdr->dst_port_id = cpu_to_le32(QRTR_NODE_BCAST);
198 } else {
199 hdr->dst_node_id = cpu_to_le32(to->sq_node);
200 hdr->dst_port_id = cpu_to_le32(to->sq_port);
203 hdr->size = cpu_to_le32(len);
204 hdr->confirm_rx = 0;
206 skb_put_padto(skb, ALIGN(len, 4) + sizeof(*hdr));
208 mutex_lock(&node->ep_lock);
209 if (node->ep)
210 rc = node->ep->xmit(node->ep, skb);
211 else
212 kfree_skb(skb);
213 mutex_unlock(&node->ep_lock);
215 return rc;
218 /* Lookup node by id.
220 * callers must release with qrtr_node_release()
222 static struct qrtr_node *qrtr_node_lookup(unsigned int nid)
224 struct qrtr_node *node;
226 mutex_lock(&qrtr_node_lock);
227 node = radix_tree_lookup(&qrtr_nodes, nid);
228 node = qrtr_node_acquire(node);
229 mutex_unlock(&qrtr_node_lock);
231 return node;
234 /* Assign node id to node.
236 * This is mostly useful for automatic node id assignment, based on
237 * the source id in the incoming packet.
239 static void qrtr_node_assign(struct qrtr_node *node, unsigned int nid)
241 if (node->nid != QRTR_EP_NID_AUTO || nid == QRTR_EP_NID_AUTO)
242 return;
244 mutex_lock(&qrtr_node_lock);
245 radix_tree_insert(&qrtr_nodes, nid, node);
246 node->nid = nid;
247 mutex_unlock(&qrtr_node_lock);
251 * qrtr_endpoint_post() - post incoming data
252 * @ep: endpoint handle
253 * @data: data pointer
254 * @len: size of data in bytes
256 * Return: 0 on success; negative error code on failure
258 int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
260 struct qrtr_node *node = ep->node;
261 const struct qrtr_hdr_v1 *v1;
262 const struct qrtr_hdr_v2 *v2;
263 struct sk_buff *skb;
264 struct qrtr_cb *cb;
265 unsigned int size;
266 unsigned int ver;
267 size_t hdrlen;
269 if (len == 0 || len & 3)
270 return -EINVAL;
272 skb = netdev_alloc_skb(NULL, len);
273 if (!skb)
274 return -ENOMEM;
276 cb = (struct qrtr_cb *)skb->cb;
278 /* Version field in v1 is little endian, so this works for both cases */
279 ver = *(u8*)data;
281 switch (ver) {
282 case QRTR_PROTO_VER_1:
283 if (len < sizeof(*v1))
284 goto err;
285 v1 = data;
286 hdrlen = sizeof(*v1);
288 cb->type = le32_to_cpu(v1->type);
289 cb->src_node = le32_to_cpu(v1->src_node_id);
290 cb->src_port = le32_to_cpu(v1->src_port_id);
291 cb->confirm_rx = !!v1->confirm_rx;
292 cb->dst_node = le32_to_cpu(v1->dst_node_id);
293 cb->dst_port = le32_to_cpu(v1->dst_port_id);
295 size = le32_to_cpu(v1->size);
296 break;
297 case QRTR_PROTO_VER_2:
298 if (len < sizeof(*v2))
299 goto err;
300 v2 = data;
301 hdrlen = sizeof(*v2) + v2->optlen;
303 cb->type = v2->type;
304 cb->confirm_rx = !!(v2->flags & QRTR_FLAGS_CONFIRM_RX);
305 cb->src_node = le16_to_cpu(v2->src_node_id);
306 cb->src_port = le16_to_cpu(v2->src_port_id);
307 cb->dst_node = le16_to_cpu(v2->dst_node_id);
308 cb->dst_port = le16_to_cpu(v2->dst_port_id);
310 if (cb->src_port == (u16)QRTR_PORT_CTRL)
311 cb->src_port = QRTR_PORT_CTRL;
312 if (cb->dst_port == (u16)QRTR_PORT_CTRL)
313 cb->dst_port = QRTR_PORT_CTRL;
315 size = le32_to_cpu(v2->size);
316 break;
317 default:
318 pr_err("qrtr: Invalid version %d\n", ver);
319 goto err;
322 if (len != ALIGN(size, 4) + hdrlen)
323 goto err;
325 if (cb->dst_port != QRTR_PORT_CTRL && cb->type != QRTR_TYPE_DATA)
326 goto err;
328 skb_put_data(skb, data + hdrlen, size);
330 skb_queue_tail(&node->rx_queue, skb);
331 schedule_work(&node->work);
333 return 0;
335 err:
336 kfree_skb(skb);
337 return -EINVAL;
340 EXPORT_SYMBOL_GPL(qrtr_endpoint_post);
343 * qrtr_alloc_ctrl_packet() - allocate control packet skb
344 * @pkt: reference to qrtr_ctrl_pkt pointer
346 * Returns newly allocated sk_buff, or NULL on failure
348 * This function allocates a sk_buff large enough to carry a qrtr_ctrl_pkt and
349 * on success returns a reference to the control packet in @pkt.
351 static struct sk_buff *qrtr_alloc_ctrl_packet(struct qrtr_ctrl_pkt **pkt)
353 const int pkt_len = sizeof(struct qrtr_ctrl_pkt);
354 struct sk_buff *skb;
356 skb = alloc_skb(QRTR_HDR_MAX_SIZE + pkt_len, GFP_KERNEL);
357 if (!skb)
358 return NULL;
360 skb_reserve(skb, QRTR_HDR_MAX_SIZE);
361 *pkt = skb_put_zero(skb, pkt_len);
363 return skb;
366 static struct qrtr_sock *qrtr_port_lookup(int port);
367 static void qrtr_port_put(struct qrtr_sock *ipc);
369 /* Handle and route a received packet.
371 * This will auto-reply with resume-tx packet as necessary.
373 static void qrtr_node_rx_work(struct work_struct *work)
375 struct qrtr_node *node = container_of(work, struct qrtr_node, work);
376 struct qrtr_ctrl_pkt *pkt;
377 struct sockaddr_qrtr dst;
378 struct sockaddr_qrtr src;
379 struct sk_buff *skb;
381 while ((skb = skb_dequeue(&node->rx_queue)) != NULL) {
382 struct qrtr_sock *ipc;
383 struct qrtr_cb *cb;
384 int confirm;
386 cb = (struct qrtr_cb *)skb->cb;
387 src.sq_node = cb->src_node;
388 src.sq_port = cb->src_port;
389 dst.sq_node = cb->dst_node;
390 dst.sq_port = cb->dst_port;
391 confirm = !!cb->confirm_rx;
393 qrtr_node_assign(node, cb->src_node);
395 ipc = qrtr_port_lookup(cb->dst_port);
396 if (!ipc) {
397 kfree_skb(skb);
398 } else {
399 if (sock_queue_rcv_skb(&ipc->sk, skb))
400 kfree_skb(skb);
402 qrtr_port_put(ipc);
405 if (confirm) {
406 skb = qrtr_alloc_ctrl_packet(&pkt);
407 if (!skb)
408 break;
410 pkt->cmd = cpu_to_le32(QRTR_TYPE_RESUME_TX);
411 pkt->client.node = cpu_to_le32(dst.sq_node);
412 pkt->client.port = cpu_to_le32(dst.sq_port);
414 if (qrtr_node_enqueue(node, skb, QRTR_TYPE_RESUME_TX,
415 &dst, &src))
416 break;
422 * qrtr_endpoint_register() - register a new endpoint
423 * @ep: endpoint to register
424 * @nid: desired node id; may be QRTR_EP_NID_AUTO for auto-assignment
425 * Return: 0 on success; negative error code on failure
427 * The specified endpoint must have the xmit function pointer set on call.
429 int qrtr_endpoint_register(struct qrtr_endpoint *ep, unsigned int nid)
431 struct qrtr_node *node;
433 if (!ep || !ep->xmit)
434 return -EINVAL;
436 node = kzalloc(sizeof(*node), GFP_KERNEL);
437 if (!node)
438 return -ENOMEM;
440 INIT_WORK(&node->work, qrtr_node_rx_work);
441 kref_init(&node->ref);
442 mutex_init(&node->ep_lock);
443 skb_queue_head_init(&node->rx_queue);
444 node->nid = QRTR_EP_NID_AUTO;
445 node->ep = ep;
447 qrtr_node_assign(node, nid);
449 mutex_lock(&qrtr_node_lock);
450 list_add(&node->item, &qrtr_all_nodes);
451 mutex_unlock(&qrtr_node_lock);
452 ep->node = node;
454 return 0;
456 EXPORT_SYMBOL_GPL(qrtr_endpoint_register);
459 * qrtr_endpoint_unregister - unregister endpoint
460 * @ep: endpoint to unregister
462 void qrtr_endpoint_unregister(struct qrtr_endpoint *ep)
464 struct qrtr_node *node = ep->node;
465 struct sockaddr_qrtr src = {AF_QIPCRTR, node->nid, QRTR_PORT_CTRL};
466 struct sockaddr_qrtr dst = {AF_QIPCRTR, qrtr_local_nid, QRTR_PORT_CTRL};
467 struct qrtr_ctrl_pkt *pkt;
468 struct sk_buff *skb;
470 mutex_lock(&node->ep_lock);
471 node->ep = NULL;
472 mutex_unlock(&node->ep_lock);
474 /* Notify the local controller about the event */
475 skb = qrtr_alloc_ctrl_packet(&pkt);
476 if (skb) {
477 pkt->cmd = cpu_to_le32(QRTR_TYPE_BYE);
478 qrtr_local_enqueue(NULL, skb, QRTR_TYPE_BYE, &src, &dst);
481 qrtr_node_release(node);
482 ep->node = NULL;
484 EXPORT_SYMBOL_GPL(qrtr_endpoint_unregister);
486 /* Lookup socket by port.
488 * Callers must release with qrtr_port_put()
490 static struct qrtr_sock *qrtr_port_lookup(int port)
492 struct qrtr_sock *ipc;
494 if (port == QRTR_PORT_CTRL)
495 port = 0;
497 mutex_lock(&qrtr_port_lock);
498 ipc = idr_find(&qrtr_ports, port);
499 if (ipc)
500 sock_hold(&ipc->sk);
501 mutex_unlock(&qrtr_port_lock);
503 return ipc;
506 /* Release acquired socket. */
507 static void qrtr_port_put(struct qrtr_sock *ipc)
509 sock_put(&ipc->sk);
512 /* Remove port assignment. */
513 static void qrtr_port_remove(struct qrtr_sock *ipc)
515 struct qrtr_ctrl_pkt *pkt;
516 struct sk_buff *skb;
517 int port = ipc->us.sq_port;
518 struct sockaddr_qrtr to;
520 to.sq_family = AF_QIPCRTR;
521 to.sq_node = QRTR_NODE_BCAST;
522 to.sq_port = QRTR_PORT_CTRL;
524 skb = qrtr_alloc_ctrl_packet(&pkt);
525 if (skb) {
526 pkt->cmd = cpu_to_le32(QRTR_TYPE_DEL_CLIENT);
527 pkt->client.node = cpu_to_le32(ipc->us.sq_node);
528 pkt->client.port = cpu_to_le32(ipc->us.sq_port);
530 skb_set_owner_w(skb, &ipc->sk);
531 qrtr_bcast_enqueue(NULL, skb, QRTR_TYPE_DEL_CLIENT, &ipc->us,
532 &to);
535 if (port == QRTR_PORT_CTRL)
536 port = 0;
538 __sock_put(&ipc->sk);
540 mutex_lock(&qrtr_port_lock);
541 idr_remove(&qrtr_ports, port);
542 mutex_unlock(&qrtr_port_lock);
545 /* Assign port number to socket.
547 * Specify port in the integer pointed to by port, and it will be adjusted
548 * on return as necesssary.
550 * Port may be:
551 * 0: Assign ephemeral port in [QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET]
552 * <QRTR_MIN_EPH_SOCKET: Specified; requires CAP_NET_ADMIN
553 * >QRTR_MIN_EPH_SOCKET: Specified; available to all
555 static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
557 int rc;
559 mutex_lock(&qrtr_port_lock);
560 if (!*port) {
561 rc = idr_alloc(&qrtr_ports, ipc,
562 QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET + 1,
563 GFP_ATOMIC);
564 if (rc >= 0)
565 *port = rc;
566 } else if (*port < QRTR_MIN_EPH_SOCKET && !capable(CAP_NET_ADMIN)) {
567 rc = -EACCES;
568 } else if (*port == QRTR_PORT_CTRL) {
569 rc = idr_alloc(&qrtr_ports, ipc, 0, 1, GFP_ATOMIC);
570 } else {
571 rc = idr_alloc(&qrtr_ports, ipc, *port, *port + 1, GFP_ATOMIC);
572 if (rc >= 0)
573 *port = rc;
575 mutex_unlock(&qrtr_port_lock);
577 if (rc == -ENOSPC)
578 return -EADDRINUSE;
579 else if (rc < 0)
580 return rc;
582 sock_hold(&ipc->sk);
584 return 0;
587 /* Reset all non-control ports */
588 static void qrtr_reset_ports(void)
590 struct qrtr_sock *ipc;
591 int id;
593 mutex_lock(&qrtr_port_lock);
594 idr_for_each_entry(&qrtr_ports, ipc, id) {
595 /* Don't reset control port */
596 if (id == 0)
597 continue;
599 sock_hold(&ipc->sk);
600 ipc->sk.sk_err = ENETRESET;
601 ipc->sk.sk_error_report(&ipc->sk);
602 sock_put(&ipc->sk);
604 mutex_unlock(&qrtr_port_lock);
607 /* Bind socket to address.
609 * Socket should be locked upon call.
611 static int __qrtr_bind(struct socket *sock,
612 const struct sockaddr_qrtr *addr, int zapped)
614 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
615 struct sock *sk = sock->sk;
616 int port;
617 int rc;
619 /* rebinding ok */
620 if (!zapped && addr->sq_port == ipc->us.sq_port)
621 return 0;
623 port = addr->sq_port;
624 rc = qrtr_port_assign(ipc, &port);
625 if (rc)
626 return rc;
628 /* unbind previous, if any */
629 if (!zapped)
630 qrtr_port_remove(ipc);
631 ipc->us.sq_port = port;
633 sock_reset_flag(sk, SOCK_ZAPPED);
635 /* Notify all open ports about the new controller */
636 if (port == QRTR_PORT_CTRL)
637 qrtr_reset_ports();
639 return 0;
642 /* Auto bind to an ephemeral port. */
643 static int qrtr_autobind(struct socket *sock)
645 struct sock *sk = sock->sk;
646 struct sockaddr_qrtr addr;
648 if (!sock_flag(sk, SOCK_ZAPPED))
649 return 0;
651 addr.sq_family = AF_QIPCRTR;
652 addr.sq_node = qrtr_local_nid;
653 addr.sq_port = 0;
655 return __qrtr_bind(sock, &addr, 1);
658 /* Bind socket to specified sockaddr. */
659 static int qrtr_bind(struct socket *sock, struct sockaddr *saddr, int len)
661 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, saddr);
662 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
663 struct sock *sk = sock->sk;
664 int rc;
666 if (len < sizeof(*addr) || addr->sq_family != AF_QIPCRTR)
667 return -EINVAL;
669 if (addr->sq_node != ipc->us.sq_node)
670 return -EINVAL;
672 lock_sock(sk);
673 rc = __qrtr_bind(sock, addr, sock_flag(sk, SOCK_ZAPPED));
674 release_sock(sk);
676 return rc;
679 /* Queue packet to local peer socket. */
680 static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,
681 int type, struct sockaddr_qrtr *from,
682 struct sockaddr_qrtr *to)
684 struct qrtr_sock *ipc;
685 struct qrtr_cb *cb;
687 ipc = qrtr_port_lookup(to->sq_port);
688 if (!ipc || &ipc->sk == skb->sk) { /* do not send to self */
689 kfree_skb(skb);
690 return -ENODEV;
693 cb = (struct qrtr_cb *)skb->cb;
694 cb->src_node = from->sq_node;
695 cb->src_port = from->sq_port;
697 if (sock_queue_rcv_skb(&ipc->sk, skb)) {
698 qrtr_port_put(ipc);
699 kfree_skb(skb);
700 return -ENOSPC;
703 qrtr_port_put(ipc);
705 return 0;
708 /* Queue packet for broadcast. */
709 static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,
710 int type, struct sockaddr_qrtr *from,
711 struct sockaddr_qrtr *to)
713 struct sk_buff *skbn;
715 mutex_lock(&qrtr_node_lock);
716 list_for_each_entry(node, &qrtr_all_nodes, item) {
717 skbn = skb_clone(skb, GFP_KERNEL);
718 if (!skbn)
719 break;
720 skb_set_owner_w(skbn, skb->sk);
721 qrtr_node_enqueue(node, skbn, type, from, to);
723 mutex_unlock(&qrtr_node_lock);
725 qrtr_local_enqueue(NULL, skb, type, from, to);
727 return 0;
730 static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
732 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg->msg_name);
733 int (*enqueue_fn)(struct qrtr_node *, struct sk_buff *, int,
734 struct sockaddr_qrtr *, struct sockaddr_qrtr *);
735 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
736 struct sock *sk = sock->sk;
737 struct qrtr_node *node;
738 struct sk_buff *skb;
739 size_t plen;
740 u32 type = QRTR_TYPE_DATA;
741 int rc;
743 if (msg->msg_flags & ~(MSG_DONTWAIT))
744 return -EINVAL;
746 if (len > 65535)
747 return -EMSGSIZE;
749 lock_sock(sk);
751 if (addr) {
752 if (msg->msg_namelen < sizeof(*addr)) {
753 release_sock(sk);
754 return -EINVAL;
757 if (addr->sq_family != AF_QIPCRTR) {
758 release_sock(sk);
759 return -EINVAL;
762 rc = qrtr_autobind(sock);
763 if (rc) {
764 release_sock(sk);
765 return rc;
767 } else if (sk->sk_state == TCP_ESTABLISHED) {
768 addr = &ipc->peer;
769 } else {
770 release_sock(sk);
771 return -ENOTCONN;
774 node = NULL;
775 if (addr->sq_node == QRTR_NODE_BCAST) {
776 if (addr->sq_port != QRTR_PORT_CTRL &&
777 qrtr_local_nid != QRTR_NODE_BCAST) {
778 release_sock(sk);
779 return -ENOTCONN;
781 enqueue_fn = qrtr_bcast_enqueue;
782 } else if (addr->sq_node == ipc->us.sq_node) {
783 enqueue_fn = qrtr_local_enqueue;
784 } else {
785 node = qrtr_node_lookup(addr->sq_node);
786 if (!node) {
787 release_sock(sk);
788 return -ECONNRESET;
790 enqueue_fn = qrtr_node_enqueue;
793 plen = (len + 3) & ~3;
794 skb = sock_alloc_send_skb(sk, plen + QRTR_HDR_MAX_SIZE,
795 msg->msg_flags & MSG_DONTWAIT, &rc);
796 if (!skb)
797 goto out_node;
799 skb_reserve(skb, QRTR_HDR_MAX_SIZE);
801 rc = memcpy_from_msg(skb_put(skb, len), msg, len);
802 if (rc) {
803 kfree_skb(skb);
804 goto out_node;
807 if (ipc->us.sq_port == QRTR_PORT_CTRL) {
808 if (len < 4) {
809 rc = -EINVAL;
810 kfree_skb(skb);
811 goto out_node;
814 /* control messages already require the type as 'command' */
815 skb_copy_bits(skb, 0, &type, 4);
816 type = le32_to_cpu(type);
819 rc = enqueue_fn(node, skb, type, &ipc->us, addr);
820 if (rc >= 0)
821 rc = len;
823 out_node:
824 qrtr_node_release(node);
825 release_sock(sk);
827 return rc;
830 static int qrtr_recvmsg(struct socket *sock, struct msghdr *msg,
831 size_t size, int flags)
833 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg->msg_name);
834 struct sock *sk = sock->sk;
835 struct sk_buff *skb;
836 struct qrtr_cb *cb;
837 int copied, rc;
839 lock_sock(sk);
841 if (sock_flag(sk, SOCK_ZAPPED)) {
842 release_sock(sk);
843 return -EADDRNOTAVAIL;
846 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
847 flags & MSG_DONTWAIT, &rc);
848 if (!skb) {
849 release_sock(sk);
850 return rc;
853 copied = skb->len;
854 if (copied > size) {
855 copied = size;
856 msg->msg_flags |= MSG_TRUNC;
859 rc = skb_copy_datagram_msg(skb, 0, msg, copied);
860 if (rc < 0)
861 goto out;
862 rc = copied;
864 if (addr) {
865 cb = (struct qrtr_cb *)skb->cb;
866 addr->sq_family = AF_QIPCRTR;
867 addr->sq_node = cb->src_node;
868 addr->sq_port = cb->src_port;
869 msg->msg_namelen = sizeof(*addr);
872 out:
873 skb_free_datagram(sk, skb);
874 release_sock(sk);
876 return rc;
879 static int qrtr_connect(struct socket *sock, struct sockaddr *saddr,
880 int len, int flags)
882 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, saddr);
883 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
884 struct sock *sk = sock->sk;
885 int rc;
887 if (len < sizeof(*addr) || addr->sq_family != AF_QIPCRTR)
888 return -EINVAL;
890 lock_sock(sk);
892 sk->sk_state = TCP_CLOSE;
893 sock->state = SS_UNCONNECTED;
895 rc = qrtr_autobind(sock);
896 if (rc) {
897 release_sock(sk);
898 return rc;
901 ipc->peer = *addr;
902 sock->state = SS_CONNECTED;
903 sk->sk_state = TCP_ESTABLISHED;
905 release_sock(sk);
907 return 0;
910 static int qrtr_getname(struct socket *sock, struct sockaddr *saddr,
911 int peer)
913 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
914 struct sockaddr_qrtr qaddr;
915 struct sock *sk = sock->sk;
917 lock_sock(sk);
918 if (peer) {
919 if (sk->sk_state != TCP_ESTABLISHED) {
920 release_sock(sk);
921 return -ENOTCONN;
924 qaddr = ipc->peer;
925 } else {
926 qaddr = ipc->us;
928 release_sock(sk);
930 qaddr.sq_family = AF_QIPCRTR;
932 memcpy(saddr, &qaddr, sizeof(qaddr));
934 return sizeof(qaddr);
937 static int qrtr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
939 void __user *argp = (void __user *)arg;
940 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
941 struct sock *sk = sock->sk;
942 struct sockaddr_qrtr *sq;
943 struct sk_buff *skb;
944 struct ifreq ifr;
945 long len = 0;
946 int rc = 0;
948 lock_sock(sk);
950 switch (cmd) {
951 case TIOCOUTQ:
952 len = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
953 if (len < 0)
954 len = 0;
955 rc = put_user(len, (int __user *)argp);
956 break;
957 case TIOCINQ:
958 skb = skb_peek(&sk->sk_receive_queue);
959 if (skb)
960 len = skb->len;
961 rc = put_user(len, (int __user *)argp);
962 break;
963 case SIOCGIFADDR:
964 if (copy_from_user(&ifr, argp, sizeof(ifr))) {
965 rc = -EFAULT;
966 break;
969 sq = (struct sockaddr_qrtr *)&ifr.ifr_addr;
970 *sq = ipc->us;
971 if (copy_to_user(argp, &ifr, sizeof(ifr))) {
972 rc = -EFAULT;
973 break;
975 break;
976 case SIOCGSTAMP:
977 rc = sock_get_timestamp(sk, argp);
978 break;
979 case SIOCADDRT:
980 case SIOCDELRT:
981 case SIOCSIFADDR:
982 case SIOCGIFDSTADDR:
983 case SIOCSIFDSTADDR:
984 case SIOCGIFBRDADDR:
985 case SIOCSIFBRDADDR:
986 case SIOCGIFNETMASK:
987 case SIOCSIFNETMASK:
988 rc = -EINVAL;
989 break;
990 default:
991 rc = -ENOIOCTLCMD;
992 break;
995 release_sock(sk);
997 return rc;
1000 static int qrtr_release(struct socket *sock)
1002 struct sock *sk = sock->sk;
1003 struct qrtr_sock *ipc;
1005 if (!sk)
1006 return 0;
1008 lock_sock(sk);
1010 ipc = qrtr_sk(sk);
1011 sk->sk_shutdown = SHUTDOWN_MASK;
1012 if (!sock_flag(sk, SOCK_DEAD))
1013 sk->sk_state_change(sk);
1015 sock_set_flag(sk, SOCK_DEAD);
1016 sock->sk = NULL;
1018 if (!sock_flag(sk, SOCK_ZAPPED))
1019 qrtr_port_remove(ipc);
1021 skb_queue_purge(&sk->sk_receive_queue);
1023 release_sock(sk);
1024 sock_put(sk);
1026 return 0;
1029 static const struct proto_ops qrtr_proto_ops = {
1030 .owner = THIS_MODULE,
1031 .family = AF_QIPCRTR,
1032 .bind = qrtr_bind,
1033 .connect = qrtr_connect,
1034 .socketpair = sock_no_socketpair,
1035 .accept = sock_no_accept,
1036 .listen = sock_no_listen,
1037 .sendmsg = qrtr_sendmsg,
1038 .recvmsg = qrtr_recvmsg,
1039 .getname = qrtr_getname,
1040 .ioctl = qrtr_ioctl,
1041 .poll = datagram_poll,
1042 .shutdown = sock_no_shutdown,
1043 .setsockopt = sock_no_setsockopt,
1044 .getsockopt = sock_no_getsockopt,
1045 .release = qrtr_release,
1046 .mmap = sock_no_mmap,
1047 .sendpage = sock_no_sendpage,
1050 static struct proto qrtr_proto = {
1051 .name = "QIPCRTR",
1052 .owner = THIS_MODULE,
1053 .obj_size = sizeof(struct qrtr_sock),
1056 static int qrtr_create(struct net *net, struct socket *sock,
1057 int protocol, int kern)
1059 struct qrtr_sock *ipc;
1060 struct sock *sk;
1062 if (sock->type != SOCK_DGRAM)
1063 return -EPROTOTYPE;
1065 sk = sk_alloc(net, AF_QIPCRTR, GFP_KERNEL, &qrtr_proto, kern);
1066 if (!sk)
1067 return -ENOMEM;
1069 sock_set_flag(sk, SOCK_ZAPPED);
1071 sock_init_data(sock, sk);
1072 sock->ops = &qrtr_proto_ops;
1074 ipc = qrtr_sk(sk);
1075 ipc->us.sq_family = AF_QIPCRTR;
1076 ipc->us.sq_node = qrtr_local_nid;
1077 ipc->us.sq_port = 0;
1079 return 0;
1082 static const struct nla_policy qrtr_policy[IFA_MAX + 1] = {
1083 [IFA_LOCAL] = { .type = NLA_U32 },
1086 static int qrtr_addr_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1087 struct netlink_ext_ack *extack)
1089 struct nlattr *tb[IFA_MAX + 1];
1090 struct ifaddrmsg *ifm;
1091 int rc;
1093 if (!netlink_capable(skb, CAP_NET_ADMIN))
1094 return -EPERM;
1096 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1097 return -EPERM;
1099 ASSERT_RTNL();
1101 rc = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, qrtr_policy, extack);
1102 if (rc < 0)
1103 return rc;
1105 ifm = nlmsg_data(nlh);
1106 if (!tb[IFA_LOCAL])
1107 return -EINVAL;
1109 qrtr_local_nid = nla_get_u32(tb[IFA_LOCAL]);
1110 return 0;
1113 static const struct net_proto_family qrtr_family = {
1114 .owner = THIS_MODULE,
1115 .family = AF_QIPCRTR,
1116 .create = qrtr_create,
1119 static int __init qrtr_proto_init(void)
1121 int rc;
1123 rc = proto_register(&qrtr_proto, 1);
1124 if (rc)
1125 return rc;
1127 rc = sock_register(&qrtr_family);
1128 if (rc) {
1129 proto_unregister(&qrtr_proto);
1130 return rc;
1133 rc = rtnl_register_module(THIS_MODULE, PF_QIPCRTR, RTM_NEWADDR, qrtr_addr_doit, NULL, 0);
1134 if (rc) {
1135 sock_unregister(qrtr_family.family);
1136 proto_unregister(&qrtr_proto);
1139 return rc;
1141 postcore_initcall(qrtr_proto_init);
1143 static void __exit qrtr_proto_fini(void)
1145 rtnl_unregister(PF_QIPCRTR, RTM_NEWADDR);
1146 sock_unregister(qrtr_family.family);
1147 proto_unregister(&qrtr_proto);
1149 module_exit(qrtr_proto_fini);
1151 MODULE_DESCRIPTION("Qualcomm IPC-router driver");
1152 MODULE_LICENSE("GPL v2");
1153 MODULE_ALIAS_NETPROTO(PF_QIPCRTR);