Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / net / qrtr / qrtr.c
blobb34358282f3798157206e3836517bba937daa5b9
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2015, Sony Mobile Communications Inc.
4 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
5 */
6 #include <linux/module.h>
7 #include <linux/netlink.h>
8 #include <linux/qrtr.h>
9 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
10 #include <linux/spinlock.h>
11 #include <linux/wait.h>
13 #include <net/sock.h>
15 #include "qrtr.h"
17 #define QRTR_PROTO_VER_1 1
18 #define QRTR_PROTO_VER_2 3
20 /* auto-bind range */
21 #define QRTR_MIN_EPH_SOCKET 0x4000
22 #define QRTR_MAX_EPH_SOCKET 0x7fff
24 /**
25 * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1
26 * @version: protocol version
27 * @type: packet type; one of QRTR_TYPE_*
28 * @src_node_id: source node
29 * @src_port_id: source port
30 * @confirm_rx: boolean; whether a resume-tx packet should be send in reply
31 * @size: length of packet, excluding this header
32 * @dst_node_id: destination node
33 * @dst_port_id: destination port
35 struct qrtr_hdr_v1 {
36 __le32 version;
37 __le32 type;
38 __le32 src_node_id;
39 __le32 src_port_id;
40 __le32 confirm_rx;
41 __le32 size;
42 __le32 dst_node_id;
43 __le32 dst_port_id;
44 } __packed;
46 /**
47 * struct qrtr_hdr_v2 - (I|R)PCrouter packet header later versions
48 * @version: protocol version
49 * @type: packet type; one of QRTR_TYPE_*
50 * @flags: bitmask of QRTR_FLAGS_*
51 * @optlen: length of optional header data
52 * @size: length of packet, excluding this header and optlen
53 * @src_node_id: source node
54 * @src_port_id: source port
55 * @dst_node_id: destination node
56 * @dst_port_id: destination port
58 struct qrtr_hdr_v2 {
59 u8 version;
60 u8 type;
61 u8 flags;
62 u8 optlen;
63 __le32 size;
64 __le16 src_node_id;
65 __le16 src_port_id;
66 __le16 dst_node_id;
67 __le16 dst_port_id;
70 #define QRTR_FLAGS_CONFIRM_RX BIT(0)
72 struct qrtr_cb {
73 u32 src_node;
74 u32 src_port;
75 u32 dst_node;
76 u32 dst_port;
78 u8 type;
79 u8 confirm_rx;
82 #define QRTR_HDR_MAX_SIZE max_t(size_t, sizeof(struct qrtr_hdr_v1), \
83 sizeof(struct qrtr_hdr_v2))
85 struct qrtr_sock {
86 /* WARNING: sk must be the first member */
87 struct sock sk;
88 struct sockaddr_qrtr us;
89 struct sockaddr_qrtr peer;
92 static inline struct qrtr_sock *qrtr_sk(struct sock *sk)
94 BUILD_BUG_ON(offsetof(struct qrtr_sock, sk) != 0);
95 return container_of(sk, struct qrtr_sock, sk);
98 static unsigned int qrtr_local_nid = 1;
100 /* for node ids */
101 static RADIX_TREE(qrtr_nodes, GFP_ATOMIC);
102 static DEFINE_SPINLOCK(qrtr_nodes_lock);
103 /* broadcast list */
104 static LIST_HEAD(qrtr_all_nodes);
105 /* lock for qrtr_all_nodes and node reference */
106 static DEFINE_MUTEX(qrtr_node_lock);
108 /* local port allocation management */
109 static DEFINE_IDR(qrtr_ports);
110 static DEFINE_MUTEX(qrtr_port_lock);
113 * struct qrtr_node - endpoint node
114 * @ep_lock: lock for endpoint management and callbacks
115 * @ep: endpoint
116 * @ref: reference count for node
117 * @nid: node id
118 * @qrtr_tx_flow: tree of qrtr_tx_flow, keyed by node << 32 | port
119 * @qrtr_tx_lock: lock for qrtr_tx_flow inserts
120 * @rx_queue: receive queue
121 * @item: list item for broadcast list
123 struct qrtr_node {
124 struct mutex ep_lock;
125 struct qrtr_endpoint *ep;
126 struct kref ref;
127 unsigned int nid;
129 struct radix_tree_root qrtr_tx_flow;
130 struct mutex qrtr_tx_lock; /* for qrtr_tx_flow */
132 struct sk_buff_head rx_queue;
133 struct list_head item;
137 * struct qrtr_tx_flow - tx flow control
138 * @resume_tx: waiters for a resume tx from the remote
139 * @pending: number of waiting senders
140 * @tx_failed: indicates that a message with confirm_rx flag was lost
142 struct qrtr_tx_flow {
143 struct wait_queue_head resume_tx;
144 int pending;
145 int tx_failed;
148 #define QRTR_TX_FLOW_HIGH 10
149 #define QRTR_TX_FLOW_LOW 5
151 static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,
152 int type, struct sockaddr_qrtr *from,
153 struct sockaddr_qrtr *to);
154 static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,
155 int type, struct sockaddr_qrtr *from,
156 struct sockaddr_qrtr *to);
157 static struct qrtr_sock *qrtr_port_lookup(int port);
158 static void qrtr_port_put(struct qrtr_sock *ipc);
160 /* Release node resources and free the node.
162 * Do not call directly, use qrtr_node_release. To be used with
163 * kref_put_mutex. As such, the node mutex is expected to be locked on call.
165 static void __qrtr_node_release(struct kref *kref)
167 struct qrtr_node *node = container_of(kref, struct qrtr_node, ref);
168 struct radix_tree_iter iter;
169 struct qrtr_tx_flow *flow;
170 unsigned long flags;
171 void __rcu **slot;
173 spin_lock_irqsave(&qrtr_nodes_lock, flags);
174 /* If the node is a bridge for other nodes, there are possibly
175 * multiple entries pointing to our released node, delete them all.
177 radix_tree_for_each_slot(slot, &qrtr_nodes, &iter, 0) {
178 if (*slot == node)
179 radix_tree_iter_delete(&qrtr_nodes, &iter, slot);
181 spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
183 list_del(&node->item);
184 mutex_unlock(&qrtr_node_lock);
186 skb_queue_purge(&node->rx_queue);
188 /* Free tx flow counters */
189 radix_tree_for_each_slot(slot, &node->qrtr_tx_flow, &iter, 0) {
190 flow = *slot;
191 radix_tree_iter_delete(&node->qrtr_tx_flow, &iter, slot);
192 kfree(flow);
194 kfree(node);
197 /* Increment reference to node. */
198 static struct qrtr_node *qrtr_node_acquire(struct qrtr_node *node)
200 if (node)
201 kref_get(&node->ref);
202 return node;
205 /* Decrement reference to node and release as necessary. */
206 static void qrtr_node_release(struct qrtr_node *node)
208 if (!node)
209 return;
210 kref_put_mutex(&node->ref, __qrtr_node_release, &qrtr_node_lock);
214 * qrtr_tx_resume() - reset flow control counter
215 * @node: qrtr_node that the QRTR_TYPE_RESUME_TX packet arrived on
216 * @skb: resume_tx packet
218 static void qrtr_tx_resume(struct qrtr_node *node, struct sk_buff *skb)
220 struct qrtr_ctrl_pkt *pkt = (struct qrtr_ctrl_pkt *)skb->data;
221 u64 remote_node = le32_to_cpu(pkt->client.node);
222 u32 remote_port = le32_to_cpu(pkt->client.port);
223 struct qrtr_tx_flow *flow;
224 unsigned long key;
226 key = remote_node << 32 | remote_port;
228 rcu_read_lock();
229 flow = radix_tree_lookup(&node->qrtr_tx_flow, key);
230 rcu_read_unlock();
231 if (flow) {
232 spin_lock(&flow->resume_tx.lock);
233 flow->pending = 0;
234 spin_unlock(&flow->resume_tx.lock);
235 wake_up_interruptible_all(&flow->resume_tx);
238 consume_skb(skb);
242 * qrtr_tx_wait() - flow control for outgoing packets
243 * @node: qrtr_node that the packet is to be send to
244 * @dest_node: node id of the destination
245 * @dest_port: port number of the destination
246 * @type: type of message
248 * The flow control scheme is based around the low and high "watermarks". When
249 * the low watermark is passed the confirm_rx flag is set on the outgoing
250 * message, which will trigger the remote to send a control message of the type
251 * QRTR_TYPE_RESUME_TX to reset the counter. If the high watermark is hit
252 * further transmision should be paused.
254 * Return: 1 if confirm_rx should be set, 0 otherwise or errno failure
256 static int qrtr_tx_wait(struct qrtr_node *node, int dest_node, int dest_port,
257 int type)
259 unsigned long key = (u64)dest_node << 32 | dest_port;
260 struct qrtr_tx_flow *flow;
261 int confirm_rx = 0;
262 int ret;
264 /* Never set confirm_rx on non-data packets */
265 if (type != QRTR_TYPE_DATA)
266 return 0;
268 mutex_lock(&node->qrtr_tx_lock);
269 flow = radix_tree_lookup(&node->qrtr_tx_flow, key);
270 if (!flow) {
271 flow = kzalloc(sizeof(*flow), GFP_KERNEL);
272 if (flow) {
273 init_waitqueue_head(&flow->resume_tx);
274 radix_tree_insert(&node->qrtr_tx_flow, key, flow);
277 mutex_unlock(&node->qrtr_tx_lock);
279 /* Set confirm_rx if we where unable to find and allocate a flow */
280 if (!flow)
281 return 1;
283 spin_lock_irq(&flow->resume_tx.lock);
284 ret = wait_event_interruptible_locked_irq(flow->resume_tx,
285 flow->pending < QRTR_TX_FLOW_HIGH ||
286 flow->tx_failed ||
287 !node->ep);
288 if (ret < 0) {
289 confirm_rx = ret;
290 } else if (!node->ep) {
291 confirm_rx = -EPIPE;
292 } else if (flow->tx_failed) {
293 flow->tx_failed = 0;
294 confirm_rx = 1;
295 } else {
296 flow->pending++;
297 confirm_rx = flow->pending == QRTR_TX_FLOW_LOW;
299 spin_unlock_irq(&flow->resume_tx.lock);
301 return confirm_rx;
305 * qrtr_tx_flow_failed() - flag that tx of confirm_rx flagged messages failed
306 * @node: qrtr_node that the packet is to be send to
307 * @dest_node: node id of the destination
308 * @dest_port: port number of the destination
310 * Signal that the transmission of a message with confirm_rx flag failed. The
311 * flow's "pending" counter will keep incrementing towards QRTR_TX_FLOW_HIGH,
312 * at which point transmission would stall forever waiting for the resume TX
313 * message associated with the dropped confirm_rx message.
314 * Work around this by marking the flow as having a failed transmission and
315 * cause the next transmission attempt to be sent with the confirm_rx.
317 static void qrtr_tx_flow_failed(struct qrtr_node *node, int dest_node,
318 int dest_port)
320 unsigned long key = (u64)dest_node << 32 | dest_port;
321 struct qrtr_tx_flow *flow;
323 rcu_read_lock();
324 flow = radix_tree_lookup(&node->qrtr_tx_flow, key);
325 rcu_read_unlock();
326 if (flow) {
327 spin_lock_irq(&flow->resume_tx.lock);
328 flow->tx_failed = 1;
329 spin_unlock_irq(&flow->resume_tx.lock);
333 /* Pass an outgoing packet socket buffer to the endpoint driver. */
334 static int qrtr_node_enqueue(struct qrtr_node *node, struct sk_buff *skb,
335 int type, struct sockaddr_qrtr *from,
336 struct sockaddr_qrtr *to)
338 struct qrtr_hdr_v1 *hdr;
339 size_t len = skb->len;
340 int rc, confirm_rx;
342 confirm_rx = qrtr_tx_wait(node, to->sq_node, to->sq_port, type);
343 if (confirm_rx < 0) {
344 kfree_skb(skb);
345 return confirm_rx;
348 hdr = skb_push(skb, sizeof(*hdr));
349 hdr->version = cpu_to_le32(QRTR_PROTO_VER_1);
350 hdr->type = cpu_to_le32(type);
351 hdr->src_node_id = cpu_to_le32(from->sq_node);
352 hdr->src_port_id = cpu_to_le32(from->sq_port);
353 if (to->sq_port == QRTR_PORT_CTRL) {
354 hdr->dst_node_id = cpu_to_le32(node->nid);
355 hdr->dst_port_id = cpu_to_le32(QRTR_PORT_CTRL);
356 } else {
357 hdr->dst_node_id = cpu_to_le32(to->sq_node);
358 hdr->dst_port_id = cpu_to_le32(to->sq_port);
361 hdr->size = cpu_to_le32(len);
362 hdr->confirm_rx = !!confirm_rx;
364 rc = skb_put_padto(skb, ALIGN(len, 4) + sizeof(*hdr));
366 if (!rc) {
367 mutex_lock(&node->ep_lock);
368 rc = -ENODEV;
369 if (node->ep)
370 rc = node->ep->xmit(node->ep, skb);
371 else
372 kfree_skb(skb);
373 mutex_unlock(&node->ep_lock);
375 /* Need to ensure that a subsequent message carries the otherwise lost
376 * confirm_rx flag if we dropped this one */
377 if (rc && confirm_rx)
378 qrtr_tx_flow_failed(node, to->sq_node, to->sq_port);
380 return rc;
383 /* Lookup node by id.
385 * callers must release with qrtr_node_release()
387 static struct qrtr_node *qrtr_node_lookup(unsigned int nid)
389 struct qrtr_node *node;
390 unsigned long flags;
392 spin_lock_irqsave(&qrtr_nodes_lock, flags);
393 node = radix_tree_lookup(&qrtr_nodes, nid);
394 node = qrtr_node_acquire(node);
395 spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
397 return node;
400 /* Assign node id to node.
402 * This is mostly useful for automatic node id assignment, based on
403 * the source id in the incoming packet.
405 static void qrtr_node_assign(struct qrtr_node *node, unsigned int nid)
407 unsigned long flags;
409 if (nid == QRTR_EP_NID_AUTO)
410 return;
412 spin_lock_irqsave(&qrtr_nodes_lock, flags);
413 radix_tree_insert(&qrtr_nodes, nid, node);
414 if (node->nid == QRTR_EP_NID_AUTO)
415 node->nid = nid;
416 spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
420 * qrtr_endpoint_post() - post incoming data
421 * @ep: endpoint handle
422 * @data: data pointer
423 * @len: size of data in bytes
425 * Return: 0 on success; negative error code on failure
427 int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
429 struct qrtr_node *node = ep->node;
430 const struct qrtr_hdr_v1 *v1;
431 const struct qrtr_hdr_v2 *v2;
432 struct qrtr_sock *ipc;
433 struct sk_buff *skb;
434 struct qrtr_cb *cb;
435 unsigned int size;
436 unsigned int ver;
437 size_t hdrlen;
439 if (len == 0 || len & 3)
440 return -EINVAL;
442 skb = netdev_alloc_skb(NULL, len);
443 if (!skb)
444 return -ENOMEM;
446 cb = (struct qrtr_cb *)skb->cb;
448 /* Version field in v1 is little endian, so this works for both cases */
449 ver = *(u8*)data;
451 switch (ver) {
452 case QRTR_PROTO_VER_1:
453 if (len < sizeof(*v1))
454 goto err;
455 v1 = data;
456 hdrlen = sizeof(*v1);
458 cb->type = le32_to_cpu(v1->type);
459 cb->src_node = le32_to_cpu(v1->src_node_id);
460 cb->src_port = le32_to_cpu(v1->src_port_id);
461 cb->confirm_rx = !!v1->confirm_rx;
462 cb->dst_node = le32_to_cpu(v1->dst_node_id);
463 cb->dst_port = le32_to_cpu(v1->dst_port_id);
465 size = le32_to_cpu(v1->size);
466 break;
467 case QRTR_PROTO_VER_2:
468 if (len < sizeof(*v2))
469 goto err;
470 v2 = data;
471 hdrlen = sizeof(*v2) + v2->optlen;
473 cb->type = v2->type;
474 cb->confirm_rx = !!(v2->flags & QRTR_FLAGS_CONFIRM_RX);
475 cb->src_node = le16_to_cpu(v2->src_node_id);
476 cb->src_port = le16_to_cpu(v2->src_port_id);
477 cb->dst_node = le16_to_cpu(v2->dst_node_id);
478 cb->dst_port = le16_to_cpu(v2->dst_port_id);
480 if (cb->src_port == (u16)QRTR_PORT_CTRL)
481 cb->src_port = QRTR_PORT_CTRL;
482 if (cb->dst_port == (u16)QRTR_PORT_CTRL)
483 cb->dst_port = QRTR_PORT_CTRL;
485 size = le32_to_cpu(v2->size);
486 break;
487 default:
488 pr_err("qrtr: Invalid version %d\n", ver);
489 goto err;
492 if (len != ALIGN(size, 4) + hdrlen)
493 goto err;
495 if (cb->dst_port != QRTR_PORT_CTRL && cb->type != QRTR_TYPE_DATA &&
496 cb->type != QRTR_TYPE_RESUME_TX)
497 goto err;
499 skb_put_data(skb, data + hdrlen, size);
501 qrtr_node_assign(node, cb->src_node);
503 if (cb->type == QRTR_TYPE_NEW_SERVER) {
504 /* Remote node endpoint can bridge other distant nodes */
505 const struct qrtr_ctrl_pkt *pkt = data + hdrlen;
507 qrtr_node_assign(node, le32_to_cpu(pkt->server.node));
510 if (cb->type == QRTR_TYPE_RESUME_TX) {
511 qrtr_tx_resume(node, skb);
512 } else {
513 ipc = qrtr_port_lookup(cb->dst_port);
514 if (!ipc)
515 goto err;
517 if (sock_queue_rcv_skb(&ipc->sk, skb))
518 goto err;
520 qrtr_port_put(ipc);
523 return 0;
525 err:
526 kfree_skb(skb);
527 return -EINVAL;
530 EXPORT_SYMBOL_GPL(qrtr_endpoint_post);
533 * qrtr_alloc_ctrl_packet() - allocate control packet skb
534 * @pkt: reference to qrtr_ctrl_pkt pointer
535 * @flags: the type of memory to allocate
537 * Returns newly allocated sk_buff, or NULL on failure
539 * This function allocates a sk_buff large enough to carry a qrtr_ctrl_pkt and
540 * on success returns a reference to the control packet in @pkt.
542 static struct sk_buff *qrtr_alloc_ctrl_packet(struct qrtr_ctrl_pkt **pkt,
543 gfp_t flags)
545 const int pkt_len = sizeof(struct qrtr_ctrl_pkt);
546 struct sk_buff *skb;
548 skb = alloc_skb(QRTR_HDR_MAX_SIZE + pkt_len, flags);
549 if (!skb)
550 return NULL;
552 skb_reserve(skb, QRTR_HDR_MAX_SIZE);
553 *pkt = skb_put_zero(skb, pkt_len);
555 return skb;
559 * qrtr_endpoint_register() - register a new endpoint
560 * @ep: endpoint to register
561 * @nid: desired node id; may be QRTR_EP_NID_AUTO for auto-assignment
562 * Return: 0 on success; negative error code on failure
564 * The specified endpoint must have the xmit function pointer set on call.
566 int qrtr_endpoint_register(struct qrtr_endpoint *ep, unsigned int nid)
568 struct qrtr_node *node;
570 if (!ep || !ep->xmit)
571 return -EINVAL;
573 node = kzalloc(sizeof(*node), GFP_KERNEL);
574 if (!node)
575 return -ENOMEM;
577 kref_init(&node->ref);
578 mutex_init(&node->ep_lock);
579 skb_queue_head_init(&node->rx_queue);
580 node->nid = QRTR_EP_NID_AUTO;
581 node->ep = ep;
583 INIT_RADIX_TREE(&node->qrtr_tx_flow, GFP_KERNEL);
584 mutex_init(&node->qrtr_tx_lock);
586 qrtr_node_assign(node, nid);
588 mutex_lock(&qrtr_node_lock);
589 list_add(&node->item, &qrtr_all_nodes);
590 mutex_unlock(&qrtr_node_lock);
591 ep->node = node;
593 return 0;
595 EXPORT_SYMBOL_GPL(qrtr_endpoint_register);
598 * qrtr_endpoint_unregister - unregister endpoint
599 * @ep: endpoint to unregister
601 void qrtr_endpoint_unregister(struct qrtr_endpoint *ep)
603 struct qrtr_node *node = ep->node;
604 struct sockaddr_qrtr src = {AF_QIPCRTR, node->nid, QRTR_PORT_CTRL};
605 struct sockaddr_qrtr dst = {AF_QIPCRTR, qrtr_local_nid, QRTR_PORT_CTRL};
606 struct radix_tree_iter iter;
607 struct qrtr_ctrl_pkt *pkt;
608 struct qrtr_tx_flow *flow;
609 struct sk_buff *skb;
610 unsigned long flags;
611 void __rcu **slot;
613 mutex_lock(&node->ep_lock);
614 node->ep = NULL;
615 mutex_unlock(&node->ep_lock);
617 /* Notify the local controller about the event */
618 spin_lock_irqsave(&qrtr_nodes_lock, flags);
619 radix_tree_for_each_slot(slot, &qrtr_nodes, &iter, 0) {
620 if (*slot != node)
621 continue;
622 src.sq_node = iter.index;
623 skb = qrtr_alloc_ctrl_packet(&pkt, GFP_ATOMIC);
624 if (skb) {
625 pkt->cmd = cpu_to_le32(QRTR_TYPE_BYE);
626 qrtr_local_enqueue(NULL, skb, QRTR_TYPE_BYE, &src, &dst);
629 spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
631 /* Wake up any transmitters waiting for resume-tx from the node */
632 mutex_lock(&node->qrtr_tx_lock);
633 radix_tree_for_each_slot(slot, &node->qrtr_tx_flow, &iter, 0) {
634 flow = *slot;
635 wake_up_interruptible_all(&flow->resume_tx);
637 mutex_unlock(&node->qrtr_tx_lock);
639 qrtr_node_release(node);
640 ep->node = NULL;
642 EXPORT_SYMBOL_GPL(qrtr_endpoint_unregister);
644 /* Lookup socket by port.
646 * Callers must release with qrtr_port_put()
648 static struct qrtr_sock *qrtr_port_lookup(int port)
650 struct qrtr_sock *ipc;
652 if (port == QRTR_PORT_CTRL)
653 port = 0;
655 rcu_read_lock();
656 ipc = idr_find(&qrtr_ports, port);
657 if (ipc)
658 sock_hold(&ipc->sk);
659 rcu_read_unlock();
661 return ipc;
664 /* Release acquired socket. */
665 static void qrtr_port_put(struct qrtr_sock *ipc)
667 sock_put(&ipc->sk);
670 /* Remove port assignment. */
671 static void qrtr_port_remove(struct qrtr_sock *ipc)
673 struct qrtr_ctrl_pkt *pkt;
674 struct sk_buff *skb;
675 int port = ipc->us.sq_port;
676 struct sockaddr_qrtr to;
678 to.sq_family = AF_QIPCRTR;
679 to.sq_node = QRTR_NODE_BCAST;
680 to.sq_port = QRTR_PORT_CTRL;
682 skb = qrtr_alloc_ctrl_packet(&pkt, GFP_KERNEL);
683 if (skb) {
684 pkt->cmd = cpu_to_le32(QRTR_TYPE_DEL_CLIENT);
685 pkt->client.node = cpu_to_le32(ipc->us.sq_node);
686 pkt->client.port = cpu_to_le32(ipc->us.sq_port);
688 skb_set_owner_w(skb, &ipc->sk);
689 qrtr_bcast_enqueue(NULL, skb, QRTR_TYPE_DEL_CLIENT, &ipc->us,
690 &to);
693 if (port == QRTR_PORT_CTRL)
694 port = 0;
696 __sock_put(&ipc->sk);
698 mutex_lock(&qrtr_port_lock);
699 idr_remove(&qrtr_ports, port);
700 mutex_unlock(&qrtr_port_lock);
702 /* Ensure that if qrtr_port_lookup() did enter the RCU read section we
703 * wait for it to up increment the refcount */
704 synchronize_rcu();
707 /* Assign port number to socket.
709 * Specify port in the integer pointed to by port, and it will be adjusted
710 * on return as necesssary.
712 * Port may be:
713 * 0: Assign ephemeral port in [QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET]
714 * <QRTR_MIN_EPH_SOCKET: Specified; requires CAP_NET_ADMIN
715 * >QRTR_MIN_EPH_SOCKET: Specified; available to all
717 static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
719 u32 min_port;
720 int rc;
722 mutex_lock(&qrtr_port_lock);
723 if (!*port) {
724 min_port = QRTR_MIN_EPH_SOCKET;
725 rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, QRTR_MAX_EPH_SOCKET, GFP_ATOMIC);
726 if (!rc)
727 *port = min_port;
728 } else if (*port < QRTR_MIN_EPH_SOCKET && !capable(CAP_NET_ADMIN)) {
729 rc = -EACCES;
730 } else if (*port == QRTR_PORT_CTRL) {
731 min_port = 0;
732 rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, 0, GFP_ATOMIC);
733 } else {
734 min_port = *port;
735 rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, *port, GFP_ATOMIC);
736 if (!rc)
737 *port = min_port;
739 mutex_unlock(&qrtr_port_lock);
741 if (rc == -ENOSPC)
742 return -EADDRINUSE;
743 else if (rc < 0)
744 return rc;
746 sock_hold(&ipc->sk);
748 return 0;
751 /* Reset all non-control ports */
752 static void qrtr_reset_ports(void)
754 struct qrtr_sock *ipc;
755 int id;
757 mutex_lock(&qrtr_port_lock);
758 idr_for_each_entry(&qrtr_ports, ipc, id) {
759 /* Don't reset control port */
760 if (id == 0)
761 continue;
763 sock_hold(&ipc->sk);
764 ipc->sk.sk_err = ENETRESET;
765 ipc->sk.sk_error_report(&ipc->sk);
766 sock_put(&ipc->sk);
768 mutex_unlock(&qrtr_port_lock);
771 /* Bind socket to address.
773 * Socket should be locked upon call.
775 static int __qrtr_bind(struct socket *sock,
776 const struct sockaddr_qrtr *addr, int zapped)
778 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
779 struct sock *sk = sock->sk;
780 int port;
781 int rc;
783 /* rebinding ok */
784 if (!zapped && addr->sq_port == ipc->us.sq_port)
785 return 0;
787 port = addr->sq_port;
788 rc = qrtr_port_assign(ipc, &port);
789 if (rc)
790 return rc;
792 /* unbind previous, if any */
793 if (!zapped)
794 qrtr_port_remove(ipc);
795 ipc->us.sq_port = port;
797 sock_reset_flag(sk, SOCK_ZAPPED);
799 /* Notify all open ports about the new controller */
800 if (port == QRTR_PORT_CTRL)
801 qrtr_reset_ports();
803 return 0;
806 /* Auto bind to an ephemeral port. */
807 static int qrtr_autobind(struct socket *sock)
809 struct sock *sk = sock->sk;
810 struct sockaddr_qrtr addr;
812 if (!sock_flag(sk, SOCK_ZAPPED))
813 return 0;
815 addr.sq_family = AF_QIPCRTR;
816 addr.sq_node = qrtr_local_nid;
817 addr.sq_port = 0;
819 return __qrtr_bind(sock, &addr, 1);
822 /* Bind socket to specified sockaddr. */
823 static int qrtr_bind(struct socket *sock, struct sockaddr *saddr, int len)
825 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, saddr);
826 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
827 struct sock *sk = sock->sk;
828 int rc;
830 if (len < sizeof(*addr) || addr->sq_family != AF_QIPCRTR)
831 return -EINVAL;
833 if (addr->sq_node != ipc->us.sq_node)
834 return -EINVAL;
836 lock_sock(sk);
837 rc = __qrtr_bind(sock, addr, sock_flag(sk, SOCK_ZAPPED));
838 release_sock(sk);
840 return rc;
843 /* Queue packet to local peer socket. */
844 static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,
845 int type, struct sockaddr_qrtr *from,
846 struct sockaddr_qrtr *to)
848 struct qrtr_sock *ipc;
849 struct qrtr_cb *cb;
851 ipc = qrtr_port_lookup(to->sq_port);
852 if (!ipc || &ipc->sk == skb->sk) { /* do not send to self */
853 kfree_skb(skb);
854 return -ENODEV;
857 cb = (struct qrtr_cb *)skb->cb;
858 cb->src_node = from->sq_node;
859 cb->src_port = from->sq_port;
861 if (sock_queue_rcv_skb(&ipc->sk, skb)) {
862 qrtr_port_put(ipc);
863 kfree_skb(skb);
864 return -ENOSPC;
867 qrtr_port_put(ipc);
869 return 0;
872 /* Queue packet for broadcast. */
873 static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,
874 int type, struct sockaddr_qrtr *from,
875 struct sockaddr_qrtr *to)
877 struct sk_buff *skbn;
879 mutex_lock(&qrtr_node_lock);
880 list_for_each_entry(node, &qrtr_all_nodes, item) {
881 skbn = skb_clone(skb, GFP_KERNEL);
882 if (!skbn)
883 break;
884 skb_set_owner_w(skbn, skb->sk);
885 qrtr_node_enqueue(node, skbn, type, from, to);
887 mutex_unlock(&qrtr_node_lock);
889 qrtr_local_enqueue(NULL, skb, type, from, to);
891 return 0;
894 static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
896 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg->msg_name);
897 int (*enqueue_fn)(struct qrtr_node *, struct sk_buff *, int,
898 struct sockaddr_qrtr *, struct sockaddr_qrtr *);
899 __le32 qrtr_type = cpu_to_le32(QRTR_TYPE_DATA);
900 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
901 struct sock *sk = sock->sk;
902 struct qrtr_node *node;
903 struct sk_buff *skb;
904 size_t plen;
905 u32 type;
906 int rc;
908 if (msg->msg_flags & ~(MSG_DONTWAIT))
909 return -EINVAL;
911 if (len > 65535)
912 return -EMSGSIZE;
914 lock_sock(sk);
916 if (addr) {
917 if (msg->msg_namelen < sizeof(*addr)) {
918 release_sock(sk);
919 return -EINVAL;
922 if (addr->sq_family != AF_QIPCRTR) {
923 release_sock(sk);
924 return -EINVAL;
927 rc = qrtr_autobind(sock);
928 if (rc) {
929 release_sock(sk);
930 return rc;
932 } else if (sk->sk_state == TCP_ESTABLISHED) {
933 addr = &ipc->peer;
934 } else {
935 release_sock(sk);
936 return -ENOTCONN;
939 node = NULL;
940 if (addr->sq_node == QRTR_NODE_BCAST) {
941 if (addr->sq_port != QRTR_PORT_CTRL &&
942 qrtr_local_nid != QRTR_NODE_BCAST) {
943 release_sock(sk);
944 return -ENOTCONN;
946 enqueue_fn = qrtr_bcast_enqueue;
947 } else if (addr->sq_node == ipc->us.sq_node) {
948 enqueue_fn = qrtr_local_enqueue;
949 } else {
950 node = qrtr_node_lookup(addr->sq_node);
951 if (!node) {
952 release_sock(sk);
953 return -ECONNRESET;
955 enqueue_fn = qrtr_node_enqueue;
958 plen = (len + 3) & ~3;
959 skb = sock_alloc_send_skb(sk, plen + QRTR_HDR_MAX_SIZE,
960 msg->msg_flags & MSG_DONTWAIT, &rc);
961 if (!skb)
962 goto out_node;
964 skb_reserve(skb, QRTR_HDR_MAX_SIZE);
966 rc = memcpy_from_msg(skb_put(skb, len), msg, len);
967 if (rc) {
968 kfree_skb(skb);
969 goto out_node;
972 if (ipc->us.sq_port == QRTR_PORT_CTRL) {
973 if (len < 4) {
974 rc = -EINVAL;
975 kfree_skb(skb);
976 goto out_node;
979 /* control messages already require the type as 'command' */
980 skb_copy_bits(skb, 0, &qrtr_type, 4);
983 type = le32_to_cpu(qrtr_type);
984 rc = enqueue_fn(node, skb, type, &ipc->us, addr);
985 if (rc >= 0)
986 rc = len;
988 out_node:
989 qrtr_node_release(node);
990 release_sock(sk);
992 return rc;
995 static int qrtr_send_resume_tx(struct qrtr_cb *cb)
997 struct sockaddr_qrtr remote = { AF_QIPCRTR, cb->src_node, cb->src_port };
998 struct sockaddr_qrtr local = { AF_QIPCRTR, cb->dst_node, cb->dst_port };
999 struct qrtr_ctrl_pkt *pkt;
1000 struct qrtr_node *node;
1001 struct sk_buff *skb;
1002 int ret;
1004 node = qrtr_node_lookup(remote.sq_node);
1005 if (!node)
1006 return -EINVAL;
1008 skb = qrtr_alloc_ctrl_packet(&pkt, GFP_KERNEL);
1009 if (!skb)
1010 return -ENOMEM;
1012 pkt->cmd = cpu_to_le32(QRTR_TYPE_RESUME_TX);
1013 pkt->client.node = cpu_to_le32(cb->dst_node);
1014 pkt->client.port = cpu_to_le32(cb->dst_port);
1016 ret = qrtr_node_enqueue(node, skb, QRTR_TYPE_RESUME_TX, &local, &remote);
1018 qrtr_node_release(node);
1020 return ret;
1023 static int qrtr_recvmsg(struct socket *sock, struct msghdr *msg,
1024 size_t size, int flags)
1026 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg->msg_name);
1027 struct sock *sk = sock->sk;
1028 struct sk_buff *skb;
1029 struct qrtr_cb *cb;
1030 int copied, rc;
1032 lock_sock(sk);
1034 if (sock_flag(sk, SOCK_ZAPPED)) {
1035 release_sock(sk);
1036 return -EADDRNOTAVAIL;
1039 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1040 flags & MSG_DONTWAIT, &rc);
1041 if (!skb) {
1042 release_sock(sk);
1043 return rc;
1045 cb = (struct qrtr_cb *)skb->cb;
1047 copied = skb->len;
1048 if (copied > size) {
1049 copied = size;
1050 msg->msg_flags |= MSG_TRUNC;
1053 rc = skb_copy_datagram_msg(skb, 0, msg, copied);
1054 if (rc < 0)
1055 goto out;
1056 rc = copied;
1058 if (addr) {
1059 addr->sq_family = AF_QIPCRTR;
1060 addr->sq_node = cb->src_node;
1061 addr->sq_port = cb->src_port;
1062 msg->msg_namelen = sizeof(*addr);
1065 out:
1066 if (cb->confirm_rx)
1067 qrtr_send_resume_tx(cb);
1069 skb_free_datagram(sk, skb);
1070 release_sock(sk);
1072 return rc;
1075 static int qrtr_connect(struct socket *sock, struct sockaddr *saddr,
1076 int len, int flags)
1078 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, saddr);
1079 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
1080 struct sock *sk = sock->sk;
1081 int rc;
1083 if (len < sizeof(*addr) || addr->sq_family != AF_QIPCRTR)
1084 return -EINVAL;
1086 lock_sock(sk);
1088 sk->sk_state = TCP_CLOSE;
1089 sock->state = SS_UNCONNECTED;
1091 rc = qrtr_autobind(sock);
1092 if (rc) {
1093 release_sock(sk);
1094 return rc;
1097 ipc->peer = *addr;
1098 sock->state = SS_CONNECTED;
1099 sk->sk_state = TCP_ESTABLISHED;
1101 release_sock(sk);
1103 return 0;
1106 static int qrtr_getname(struct socket *sock, struct sockaddr *saddr,
1107 int peer)
1109 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
1110 struct sockaddr_qrtr qaddr;
1111 struct sock *sk = sock->sk;
1113 lock_sock(sk);
1114 if (peer) {
1115 if (sk->sk_state != TCP_ESTABLISHED) {
1116 release_sock(sk);
1117 return -ENOTCONN;
1120 qaddr = ipc->peer;
1121 } else {
1122 qaddr = ipc->us;
1124 release_sock(sk);
1126 qaddr.sq_family = AF_QIPCRTR;
1128 memcpy(saddr, &qaddr, sizeof(qaddr));
1130 return sizeof(qaddr);
1133 static int qrtr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1135 void __user *argp = (void __user *)arg;
1136 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
1137 struct sock *sk = sock->sk;
1138 struct sockaddr_qrtr *sq;
1139 struct sk_buff *skb;
1140 struct ifreq ifr;
1141 long len = 0;
1142 int rc = 0;
1144 lock_sock(sk);
1146 switch (cmd) {
1147 case TIOCOUTQ:
1148 len = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1149 if (len < 0)
1150 len = 0;
1151 rc = put_user(len, (int __user *)argp);
1152 break;
1153 case TIOCINQ:
1154 skb = skb_peek(&sk->sk_receive_queue);
1155 if (skb)
1156 len = skb->len;
1157 rc = put_user(len, (int __user *)argp);
1158 break;
1159 case SIOCGIFADDR:
1160 if (copy_from_user(&ifr, argp, sizeof(ifr))) {
1161 rc = -EFAULT;
1162 break;
1165 sq = (struct sockaddr_qrtr *)&ifr.ifr_addr;
1166 *sq = ipc->us;
1167 if (copy_to_user(argp, &ifr, sizeof(ifr))) {
1168 rc = -EFAULT;
1169 break;
1171 break;
1172 case SIOCADDRT:
1173 case SIOCDELRT:
1174 case SIOCSIFADDR:
1175 case SIOCGIFDSTADDR:
1176 case SIOCSIFDSTADDR:
1177 case SIOCGIFBRDADDR:
1178 case SIOCSIFBRDADDR:
1179 case SIOCGIFNETMASK:
1180 case SIOCSIFNETMASK:
1181 rc = -EINVAL;
1182 break;
1183 default:
1184 rc = -ENOIOCTLCMD;
1185 break;
1188 release_sock(sk);
1190 return rc;
1193 static int qrtr_release(struct socket *sock)
1195 struct sock *sk = sock->sk;
1196 struct qrtr_sock *ipc;
1198 if (!sk)
1199 return 0;
1201 lock_sock(sk);
1203 ipc = qrtr_sk(sk);
1204 sk->sk_shutdown = SHUTDOWN_MASK;
1205 if (!sock_flag(sk, SOCK_DEAD))
1206 sk->sk_state_change(sk);
1208 sock_set_flag(sk, SOCK_DEAD);
1209 sock_orphan(sk);
1210 sock->sk = NULL;
1212 if (!sock_flag(sk, SOCK_ZAPPED))
1213 qrtr_port_remove(ipc);
1215 skb_queue_purge(&sk->sk_receive_queue);
1217 release_sock(sk);
1218 sock_put(sk);
1220 return 0;
1223 static const struct proto_ops qrtr_proto_ops = {
1224 .owner = THIS_MODULE,
1225 .family = AF_QIPCRTR,
1226 .bind = qrtr_bind,
1227 .connect = qrtr_connect,
1228 .socketpair = sock_no_socketpair,
1229 .accept = sock_no_accept,
1230 .listen = sock_no_listen,
1231 .sendmsg = qrtr_sendmsg,
1232 .recvmsg = qrtr_recvmsg,
1233 .getname = qrtr_getname,
1234 .ioctl = qrtr_ioctl,
1235 .gettstamp = sock_gettstamp,
1236 .poll = datagram_poll,
1237 .shutdown = sock_no_shutdown,
1238 .release = qrtr_release,
1239 .mmap = sock_no_mmap,
1240 .sendpage = sock_no_sendpage,
1243 static struct proto qrtr_proto = {
1244 .name = "QIPCRTR",
1245 .owner = THIS_MODULE,
1246 .obj_size = sizeof(struct qrtr_sock),
1249 static int qrtr_create(struct net *net, struct socket *sock,
1250 int protocol, int kern)
1252 struct qrtr_sock *ipc;
1253 struct sock *sk;
1255 if (sock->type != SOCK_DGRAM)
1256 return -EPROTOTYPE;
1258 sk = sk_alloc(net, AF_QIPCRTR, GFP_KERNEL, &qrtr_proto, kern);
1259 if (!sk)
1260 return -ENOMEM;
1262 sock_set_flag(sk, SOCK_ZAPPED);
1264 sock_init_data(sock, sk);
1265 sock->ops = &qrtr_proto_ops;
1267 ipc = qrtr_sk(sk);
1268 ipc->us.sq_family = AF_QIPCRTR;
1269 ipc->us.sq_node = qrtr_local_nid;
1270 ipc->us.sq_port = 0;
1272 return 0;
1275 static const struct net_proto_family qrtr_family = {
1276 .owner = THIS_MODULE,
1277 .family = AF_QIPCRTR,
1278 .create = qrtr_create,
1281 static int __init qrtr_proto_init(void)
1283 int rc;
1285 rc = proto_register(&qrtr_proto, 1);
1286 if (rc)
1287 return rc;
1289 rc = sock_register(&qrtr_family);
1290 if (rc)
1291 goto err_proto;
1293 rc = qrtr_ns_init();
1294 if (rc)
1295 goto err_sock;
1297 return 0;
1299 err_sock:
1300 sock_unregister(qrtr_family.family);
1301 err_proto:
1302 proto_unregister(&qrtr_proto);
1303 return rc;
1305 postcore_initcall(qrtr_proto_init);
1307 static void __exit qrtr_proto_fini(void)
1309 qrtr_ns_remove();
1310 sock_unregister(qrtr_family.family);
1311 proto_unregister(&qrtr_proto);
1313 module_exit(qrtr_proto_fini);
1315 MODULE_DESCRIPTION("Qualcomm IPC-router driver");
1316 MODULE_LICENSE("GPL v2");
1317 MODULE_ALIAS_NETPROTO(PF_QIPCRTR);