Linux 5.7.7
[linux/fpc-iii.git] / net / qrtr / qrtr.c
blob2d8d6131bc5f7e23b168af2dcd377d55c278cd8e
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 unsigned long flags;
170 void __rcu **slot;
172 spin_lock_irqsave(&qrtr_nodes_lock, flags);
173 if (node->nid != QRTR_EP_NID_AUTO)
174 radix_tree_delete(&qrtr_nodes, node->nid);
175 spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
177 list_del(&node->item);
178 mutex_unlock(&qrtr_node_lock);
180 skb_queue_purge(&node->rx_queue);
182 /* Free tx flow counters */
183 radix_tree_for_each_slot(slot, &node->qrtr_tx_flow, &iter, 0) {
184 radix_tree_iter_delete(&node->qrtr_tx_flow, &iter, slot);
185 kfree(*slot);
187 kfree(node);
190 /* Increment reference to node. */
191 static struct qrtr_node *qrtr_node_acquire(struct qrtr_node *node)
193 if (node)
194 kref_get(&node->ref);
195 return node;
198 /* Decrement reference to node and release as necessary. */
199 static void qrtr_node_release(struct qrtr_node *node)
201 if (!node)
202 return;
203 kref_put_mutex(&node->ref, __qrtr_node_release, &qrtr_node_lock);
207 * qrtr_tx_resume() - reset flow control counter
208 * @node: qrtr_node that the QRTR_TYPE_RESUME_TX packet arrived on
209 * @skb: resume_tx packet
211 static void qrtr_tx_resume(struct qrtr_node *node, struct sk_buff *skb)
213 struct qrtr_ctrl_pkt *pkt = (struct qrtr_ctrl_pkt *)skb->data;
214 u64 remote_node = le32_to_cpu(pkt->client.node);
215 u32 remote_port = le32_to_cpu(pkt->client.port);
216 struct qrtr_tx_flow *flow;
217 unsigned long key;
219 key = remote_node << 32 | remote_port;
221 rcu_read_lock();
222 flow = radix_tree_lookup(&node->qrtr_tx_flow, key);
223 rcu_read_unlock();
224 if (flow) {
225 spin_lock(&flow->resume_tx.lock);
226 flow->pending = 0;
227 spin_unlock(&flow->resume_tx.lock);
228 wake_up_interruptible_all(&flow->resume_tx);
231 consume_skb(skb);
235 * qrtr_tx_wait() - flow control for outgoing packets
236 * @node: qrtr_node that the packet is to be send to
237 * @dest_node: node id of the destination
238 * @dest_port: port number of the destination
239 * @type: type of message
241 * The flow control scheme is based around the low and high "watermarks". When
242 * the low watermark is passed the confirm_rx flag is set on the outgoing
243 * message, which will trigger the remote to send a control message of the type
244 * QRTR_TYPE_RESUME_TX to reset the counter. If the high watermark is hit
245 * further transmision should be paused.
247 * Return: 1 if confirm_rx should be set, 0 otherwise or errno failure
249 static int qrtr_tx_wait(struct qrtr_node *node, int dest_node, int dest_port,
250 int type)
252 unsigned long key = (u64)dest_node << 32 | dest_port;
253 struct qrtr_tx_flow *flow;
254 int confirm_rx = 0;
255 int ret;
257 /* Never set confirm_rx on non-data packets */
258 if (type != QRTR_TYPE_DATA)
259 return 0;
261 mutex_lock(&node->qrtr_tx_lock);
262 flow = radix_tree_lookup(&node->qrtr_tx_flow, key);
263 if (!flow) {
264 flow = kzalloc(sizeof(*flow), GFP_KERNEL);
265 if (flow) {
266 init_waitqueue_head(&flow->resume_tx);
267 radix_tree_insert(&node->qrtr_tx_flow, key, flow);
270 mutex_unlock(&node->qrtr_tx_lock);
272 /* Set confirm_rx if we where unable to find and allocate a flow */
273 if (!flow)
274 return 1;
276 spin_lock_irq(&flow->resume_tx.lock);
277 ret = wait_event_interruptible_locked_irq(flow->resume_tx,
278 flow->pending < QRTR_TX_FLOW_HIGH ||
279 flow->tx_failed ||
280 !node->ep);
281 if (ret < 0) {
282 confirm_rx = ret;
283 } else if (!node->ep) {
284 confirm_rx = -EPIPE;
285 } else if (flow->tx_failed) {
286 flow->tx_failed = 0;
287 confirm_rx = 1;
288 } else {
289 flow->pending++;
290 confirm_rx = flow->pending == QRTR_TX_FLOW_LOW;
292 spin_unlock_irq(&flow->resume_tx.lock);
294 return confirm_rx;
298 * qrtr_tx_flow_failed() - flag that tx of confirm_rx flagged messages failed
299 * @node: qrtr_node that the packet is to be send to
300 * @dest_node: node id of the destination
301 * @dest_port: port number of the destination
303 * Signal that the transmission of a message with confirm_rx flag failed. The
304 * flow's "pending" counter will keep incrementing towards QRTR_TX_FLOW_HIGH,
305 * at which point transmission would stall forever waiting for the resume TX
306 * message associated with the dropped confirm_rx message.
307 * Work around this by marking the flow as having a failed transmission and
308 * cause the next transmission attempt to be sent with the confirm_rx.
310 static void qrtr_tx_flow_failed(struct qrtr_node *node, int dest_node,
311 int dest_port)
313 unsigned long key = (u64)dest_node << 32 | dest_port;
314 struct qrtr_tx_flow *flow;
316 rcu_read_lock();
317 flow = radix_tree_lookup(&node->qrtr_tx_flow, key);
318 rcu_read_unlock();
319 if (flow) {
320 spin_lock_irq(&flow->resume_tx.lock);
321 flow->tx_failed = 1;
322 spin_unlock_irq(&flow->resume_tx.lock);
326 /* Pass an outgoing packet socket buffer to the endpoint driver. */
327 static int qrtr_node_enqueue(struct qrtr_node *node, struct sk_buff *skb,
328 int type, struct sockaddr_qrtr *from,
329 struct sockaddr_qrtr *to)
331 struct qrtr_hdr_v1 *hdr;
332 size_t len = skb->len;
333 int rc = -ENODEV;
334 int confirm_rx;
336 confirm_rx = qrtr_tx_wait(node, to->sq_node, to->sq_port, type);
337 if (confirm_rx < 0) {
338 kfree_skb(skb);
339 return confirm_rx;
342 hdr = skb_push(skb, sizeof(*hdr));
343 hdr->version = cpu_to_le32(QRTR_PROTO_VER_1);
344 hdr->type = cpu_to_le32(type);
345 hdr->src_node_id = cpu_to_le32(from->sq_node);
346 hdr->src_port_id = cpu_to_le32(from->sq_port);
347 if (to->sq_port == QRTR_PORT_CTRL) {
348 hdr->dst_node_id = cpu_to_le32(node->nid);
349 hdr->dst_port_id = cpu_to_le32(QRTR_NODE_BCAST);
350 } else {
351 hdr->dst_node_id = cpu_to_le32(to->sq_node);
352 hdr->dst_port_id = cpu_to_le32(to->sq_port);
355 hdr->size = cpu_to_le32(len);
356 hdr->confirm_rx = !!confirm_rx;
358 skb_put_padto(skb, ALIGN(len, 4) + sizeof(*hdr));
360 mutex_lock(&node->ep_lock);
361 if (node->ep)
362 rc = node->ep->xmit(node->ep, skb);
363 else
364 kfree_skb(skb);
365 mutex_unlock(&node->ep_lock);
367 /* Need to ensure that a subsequent message carries the otherwise lost
368 * confirm_rx flag if we dropped this one */
369 if (rc && confirm_rx)
370 qrtr_tx_flow_failed(node, to->sq_node, to->sq_port);
372 return rc;
375 /* Lookup node by id.
377 * callers must release with qrtr_node_release()
379 static struct qrtr_node *qrtr_node_lookup(unsigned int nid)
381 struct qrtr_node *node;
382 unsigned long flags;
384 spin_lock_irqsave(&qrtr_nodes_lock, flags);
385 node = radix_tree_lookup(&qrtr_nodes, nid);
386 node = qrtr_node_acquire(node);
387 spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
389 return node;
392 /* Assign node id to node.
394 * This is mostly useful for automatic node id assignment, based on
395 * the source id in the incoming packet.
397 static void qrtr_node_assign(struct qrtr_node *node, unsigned int nid)
399 unsigned long flags;
401 if (node->nid != QRTR_EP_NID_AUTO || nid == QRTR_EP_NID_AUTO)
402 return;
404 spin_lock_irqsave(&qrtr_nodes_lock, flags);
405 radix_tree_insert(&qrtr_nodes, nid, node);
406 node->nid = nid;
407 spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
411 * qrtr_endpoint_post() - post incoming data
412 * @ep: endpoint handle
413 * @data: data pointer
414 * @len: size of data in bytes
416 * Return: 0 on success; negative error code on failure
418 int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
420 struct qrtr_node *node = ep->node;
421 const struct qrtr_hdr_v1 *v1;
422 const struct qrtr_hdr_v2 *v2;
423 struct qrtr_sock *ipc;
424 struct sk_buff *skb;
425 struct qrtr_cb *cb;
426 unsigned int size;
427 unsigned int ver;
428 size_t hdrlen;
430 if (len & 3)
431 return -EINVAL;
433 skb = netdev_alloc_skb(NULL, len);
434 if (!skb)
435 return -ENOMEM;
437 cb = (struct qrtr_cb *)skb->cb;
439 /* Version field in v1 is little endian, so this works for both cases */
440 ver = *(u8*)data;
442 switch (ver) {
443 case QRTR_PROTO_VER_1:
444 v1 = data;
445 hdrlen = sizeof(*v1);
447 cb->type = le32_to_cpu(v1->type);
448 cb->src_node = le32_to_cpu(v1->src_node_id);
449 cb->src_port = le32_to_cpu(v1->src_port_id);
450 cb->confirm_rx = !!v1->confirm_rx;
451 cb->dst_node = le32_to_cpu(v1->dst_node_id);
452 cb->dst_port = le32_to_cpu(v1->dst_port_id);
454 size = le32_to_cpu(v1->size);
455 break;
456 case QRTR_PROTO_VER_2:
457 v2 = data;
458 hdrlen = sizeof(*v2) + v2->optlen;
460 cb->type = v2->type;
461 cb->confirm_rx = !!(v2->flags & QRTR_FLAGS_CONFIRM_RX);
462 cb->src_node = le16_to_cpu(v2->src_node_id);
463 cb->src_port = le16_to_cpu(v2->src_port_id);
464 cb->dst_node = le16_to_cpu(v2->dst_node_id);
465 cb->dst_port = le16_to_cpu(v2->dst_port_id);
467 if (cb->src_port == (u16)QRTR_PORT_CTRL)
468 cb->src_port = QRTR_PORT_CTRL;
469 if (cb->dst_port == (u16)QRTR_PORT_CTRL)
470 cb->dst_port = QRTR_PORT_CTRL;
472 size = le32_to_cpu(v2->size);
473 break;
474 default:
475 pr_err("qrtr: Invalid version %d\n", ver);
476 goto err;
479 if (len != ALIGN(size, 4) + hdrlen)
480 goto err;
482 if (cb->dst_port != QRTR_PORT_CTRL && cb->type != QRTR_TYPE_DATA &&
483 cb->type != QRTR_TYPE_RESUME_TX)
484 goto err;
486 skb_put_data(skb, data + hdrlen, size);
488 qrtr_node_assign(node, cb->src_node);
490 if (cb->type == QRTR_TYPE_RESUME_TX) {
491 qrtr_tx_resume(node, skb);
492 } else {
493 ipc = qrtr_port_lookup(cb->dst_port);
494 if (!ipc)
495 goto err;
497 if (sock_queue_rcv_skb(&ipc->sk, skb))
498 goto err;
500 qrtr_port_put(ipc);
503 return 0;
505 err:
506 kfree_skb(skb);
507 return -EINVAL;
510 EXPORT_SYMBOL_GPL(qrtr_endpoint_post);
513 * qrtr_alloc_ctrl_packet() - allocate control packet skb
514 * @pkt: reference to qrtr_ctrl_pkt pointer
516 * Returns newly allocated sk_buff, or NULL on failure
518 * This function allocates a sk_buff large enough to carry a qrtr_ctrl_pkt and
519 * on success returns a reference to the control packet in @pkt.
521 static struct sk_buff *qrtr_alloc_ctrl_packet(struct qrtr_ctrl_pkt **pkt)
523 const int pkt_len = sizeof(struct qrtr_ctrl_pkt);
524 struct sk_buff *skb;
526 skb = alloc_skb(QRTR_HDR_MAX_SIZE + pkt_len, GFP_KERNEL);
527 if (!skb)
528 return NULL;
530 skb_reserve(skb, QRTR_HDR_MAX_SIZE);
531 *pkt = skb_put_zero(skb, pkt_len);
533 return skb;
537 * qrtr_endpoint_register() - register a new endpoint
538 * @ep: endpoint to register
539 * @nid: desired node id; may be QRTR_EP_NID_AUTO for auto-assignment
540 * Return: 0 on success; negative error code on failure
542 * The specified endpoint must have the xmit function pointer set on call.
544 int qrtr_endpoint_register(struct qrtr_endpoint *ep, unsigned int nid)
546 struct qrtr_node *node;
548 if (!ep || !ep->xmit)
549 return -EINVAL;
551 node = kzalloc(sizeof(*node), GFP_KERNEL);
552 if (!node)
553 return -ENOMEM;
555 kref_init(&node->ref);
556 mutex_init(&node->ep_lock);
557 skb_queue_head_init(&node->rx_queue);
558 node->nid = QRTR_EP_NID_AUTO;
559 node->ep = ep;
561 INIT_RADIX_TREE(&node->qrtr_tx_flow, GFP_KERNEL);
562 mutex_init(&node->qrtr_tx_lock);
564 qrtr_node_assign(node, nid);
566 mutex_lock(&qrtr_node_lock);
567 list_add(&node->item, &qrtr_all_nodes);
568 mutex_unlock(&qrtr_node_lock);
569 ep->node = node;
571 return 0;
573 EXPORT_SYMBOL_GPL(qrtr_endpoint_register);
576 * qrtr_endpoint_unregister - unregister endpoint
577 * @ep: endpoint to unregister
579 void qrtr_endpoint_unregister(struct qrtr_endpoint *ep)
581 struct qrtr_node *node = ep->node;
582 struct sockaddr_qrtr src = {AF_QIPCRTR, node->nid, QRTR_PORT_CTRL};
583 struct sockaddr_qrtr dst = {AF_QIPCRTR, qrtr_local_nid, QRTR_PORT_CTRL};
584 struct radix_tree_iter iter;
585 struct qrtr_ctrl_pkt *pkt;
586 struct qrtr_tx_flow *flow;
587 struct sk_buff *skb;
588 void __rcu **slot;
590 mutex_lock(&node->ep_lock);
591 node->ep = NULL;
592 mutex_unlock(&node->ep_lock);
594 /* Notify the local controller about the event */
595 skb = qrtr_alloc_ctrl_packet(&pkt);
596 if (skb) {
597 pkt->cmd = cpu_to_le32(QRTR_TYPE_BYE);
598 qrtr_local_enqueue(NULL, skb, QRTR_TYPE_BYE, &src, &dst);
601 /* Wake up any transmitters waiting for resume-tx from the node */
602 mutex_lock(&node->qrtr_tx_lock);
603 radix_tree_for_each_slot(slot, &node->qrtr_tx_flow, &iter, 0) {
604 flow = *slot;
605 wake_up_interruptible_all(&flow->resume_tx);
607 mutex_unlock(&node->qrtr_tx_lock);
609 qrtr_node_release(node);
610 ep->node = NULL;
612 EXPORT_SYMBOL_GPL(qrtr_endpoint_unregister);
614 /* Lookup socket by port.
616 * Callers must release with qrtr_port_put()
618 static struct qrtr_sock *qrtr_port_lookup(int port)
620 struct qrtr_sock *ipc;
622 if (port == QRTR_PORT_CTRL)
623 port = 0;
625 rcu_read_lock();
626 ipc = idr_find(&qrtr_ports, port);
627 if (ipc)
628 sock_hold(&ipc->sk);
629 rcu_read_unlock();
631 return ipc;
634 /* Release acquired socket. */
635 static void qrtr_port_put(struct qrtr_sock *ipc)
637 sock_put(&ipc->sk);
640 /* Remove port assignment. */
641 static void qrtr_port_remove(struct qrtr_sock *ipc)
643 struct qrtr_ctrl_pkt *pkt;
644 struct sk_buff *skb;
645 int port = ipc->us.sq_port;
646 struct sockaddr_qrtr to;
648 to.sq_family = AF_QIPCRTR;
649 to.sq_node = QRTR_NODE_BCAST;
650 to.sq_port = QRTR_PORT_CTRL;
652 skb = qrtr_alloc_ctrl_packet(&pkt);
653 if (skb) {
654 pkt->cmd = cpu_to_le32(QRTR_TYPE_DEL_CLIENT);
655 pkt->client.node = cpu_to_le32(ipc->us.sq_node);
656 pkt->client.port = cpu_to_le32(ipc->us.sq_port);
658 skb_set_owner_w(skb, &ipc->sk);
659 qrtr_bcast_enqueue(NULL, skb, QRTR_TYPE_DEL_CLIENT, &ipc->us,
660 &to);
663 if (port == QRTR_PORT_CTRL)
664 port = 0;
666 __sock_put(&ipc->sk);
668 mutex_lock(&qrtr_port_lock);
669 idr_remove(&qrtr_ports, port);
670 mutex_unlock(&qrtr_port_lock);
672 /* Ensure that if qrtr_port_lookup() did enter the RCU read section we
673 * wait for it to up increment the refcount */
674 synchronize_rcu();
677 /* Assign port number to socket.
679 * Specify port in the integer pointed to by port, and it will be adjusted
680 * on return as necesssary.
682 * Port may be:
683 * 0: Assign ephemeral port in [QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET]
684 * <QRTR_MIN_EPH_SOCKET: Specified; requires CAP_NET_ADMIN
685 * >QRTR_MIN_EPH_SOCKET: Specified; available to all
687 static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
689 int rc;
691 mutex_lock(&qrtr_port_lock);
692 if (!*port) {
693 rc = idr_alloc(&qrtr_ports, ipc,
694 QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET + 1,
695 GFP_ATOMIC);
696 if (rc >= 0)
697 *port = rc;
698 } else if (*port < QRTR_MIN_EPH_SOCKET && !capable(CAP_NET_ADMIN)) {
699 rc = -EACCES;
700 } else if (*port == QRTR_PORT_CTRL) {
701 rc = idr_alloc(&qrtr_ports, ipc, 0, 1, GFP_ATOMIC);
702 } else {
703 rc = idr_alloc(&qrtr_ports, ipc, *port, *port + 1, GFP_ATOMIC);
704 if (rc >= 0)
705 *port = rc;
707 mutex_unlock(&qrtr_port_lock);
709 if (rc == -ENOSPC)
710 return -EADDRINUSE;
711 else if (rc < 0)
712 return rc;
714 sock_hold(&ipc->sk);
716 return 0;
719 /* Reset all non-control ports */
720 static void qrtr_reset_ports(void)
722 struct qrtr_sock *ipc;
723 int id;
725 mutex_lock(&qrtr_port_lock);
726 idr_for_each_entry(&qrtr_ports, ipc, id) {
727 /* Don't reset control port */
728 if (id == 0)
729 continue;
731 sock_hold(&ipc->sk);
732 ipc->sk.sk_err = ENETRESET;
733 ipc->sk.sk_error_report(&ipc->sk);
734 sock_put(&ipc->sk);
736 mutex_unlock(&qrtr_port_lock);
739 /* Bind socket to address.
741 * Socket should be locked upon call.
743 static int __qrtr_bind(struct socket *sock,
744 const struct sockaddr_qrtr *addr, int zapped)
746 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
747 struct sock *sk = sock->sk;
748 int port;
749 int rc;
751 /* rebinding ok */
752 if (!zapped && addr->sq_port == ipc->us.sq_port)
753 return 0;
755 port = addr->sq_port;
756 rc = qrtr_port_assign(ipc, &port);
757 if (rc)
758 return rc;
760 /* unbind previous, if any */
761 if (!zapped)
762 qrtr_port_remove(ipc);
763 ipc->us.sq_port = port;
765 sock_reset_flag(sk, SOCK_ZAPPED);
767 /* Notify all open ports about the new controller */
768 if (port == QRTR_PORT_CTRL)
769 qrtr_reset_ports();
771 return 0;
774 /* Auto bind to an ephemeral port. */
775 static int qrtr_autobind(struct socket *sock)
777 struct sock *sk = sock->sk;
778 struct sockaddr_qrtr addr;
780 if (!sock_flag(sk, SOCK_ZAPPED))
781 return 0;
783 addr.sq_family = AF_QIPCRTR;
784 addr.sq_node = qrtr_local_nid;
785 addr.sq_port = 0;
787 return __qrtr_bind(sock, &addr, 1);
790 /* Bind socket to specified sockaddr. */
791 static int qrtr_bind(struct socket *sock, struct sockaddr *saddr, int len)
793 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, saddr);
794 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
795 struct sock *sk = sock->sk;
796 int rc;
798 if (len < sizeof(*addr) || addr->sq_family != AF_QIPCRTR)
799 return -EINVAL;
801 if (addr->sq_node != ipc->us.sq_node)
802 return -EINVAL;
804 lock_sock(sk);
805 rc = __qrtr_bind(sock, addr, sock_flag(sk, SOCK_ZAPPED));
806 release_sock(sk);
808 return rc;
811 /* Queue packet to local peer socket. */
812 static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,
813 int type, struct sockaddr_qrtr *from,
814 struct sockaddr_qrtr *to)
816 struct qrtr_sock *ipc;
817 struct qrtr_cb *cb;
819 ipc = qrtr_port_lookup(to->sq_port);
820 if (!ipc || &ipc->sk == skb->sk) { /* do not send to self */
821 kfree_skb(skb);
822 return -ENODEV;
825 cb = (struct qrtr_cb *)skb->cb;
826 cb->src_node = from->sq_node;
827 cb->src_port = from->sq_port;
829 if (sock_queue_rcv_skb(&ipc->sk, skb)) {
830 qrtr_port_put(ipc);
831 kfree_skb(skb);
832 return -ENOSPC;
835 qrtr_port_put(ipc);
837 return 0;
840 /* Queue packet for broadcast. */
841 static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,
842 int type, struct sockaddr_qrtr *from,
843 struct sockaddr_qrtr *to)
845 struct sk_buff *skbn;
847 mutex_lock(&qrtr_node_lock);
848 list_for_each_entry(node, &qrtr_all_nodes, item) {
849 skbn = skb_clone(skb, GFP_KERNEL);
850 if (!skbn)
851 break;
852 skb_set_owner_w(skbn, skb->sk);
853 qrtr_node_enqueue(node, skbn, type, from, to);
855 mutex_unlock(&qrtr_node_lock);
857 qrtr_local_enqueue(NULL, skb, type, from, to);
859 return 0;
862 static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
864 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg->msg_name);
865 int (*enqueue_fn)(struct qrtr_node *, struct sk_buff *, int,
866 struct sockaddr_qrtr *, struct sockaddr_qrtr *);
867 __le32 qrtr_type = cpu_to_le32(QRTR_TYPE_DATA);
868 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
869 struct sock *sk = sock->sk;
870 struct qrtr_node *node;
871 struct sk_buff *skb;
872 size_t plen;
873 u32 type;
874 int rc;
876 if (msg->msg_flags & ~(MSG_DONTWAIT))
877 return -EINVAL;
879 if (len > 65535)
880 return -EMSGSIZE;
882 lock_sock(sk);
884 if (addr) {
885 if (msg->msg_namelen < sizeof(*addr)) {
886 release_sock(sk);
887 return -EINVAL;
890 if (addr->sq_family != AF_QIPCRTR) {
891 release_sock(sk);
892 return -EINVAL;
895 rc = qrtr_autobind(sock);
896 if (rc) {
897 release_sock(sk);
898 return rc;
900 } else if (sk->sk_state == TCP_ESTABLISHED) {
901 addr = &ipc->peer;
902 } else {
903 release_sock(sk);
904 return -ENOTCONN;
907 node = NULL;
908 if (addr->sq_node == QRTR_NODE_BCAST) {
909 if (addr->sq_port != QRTR_PORT_CTRL &&
910 qrtr_local_nid != QRTR_NODE_BCAST) {
911 release_sock(sk);
912 return -ENOTCONN;
914 enqueue_fn = qrtr_bcast_enqueue;
915 } else if (addr->sq_node == ipc->us.sq_node) {
916 enqueue_fn = qrtr_local_enqueue;
917 } else {
918 node = qrtr_node_lookup(addr->sq_node);
919 if (!node) {
920 release_sock(sk);
921 return -ECONNRESET;
923 enqueue_fn = qrtr_node_enqueue;
926 plen = (len + 3) & ~3;
927 skb = sock_alloc_send_skb(sk, plen + QRTR_HDR_MAX_SIZE,
928 msg->msg_flags & MSG_DONTWAIT, &rc);
929 if (!skb)
930 goto out_node;
932 skb_reserve(skb, QRTR_HDR_MAX_SIZE);
934 rc = memcpy_from_msg(skb_put(skb, len), msg, len);
935 if (rc) {
936 kfree_skb(skb);
937 goto out_node;
940 if (ipc->us.sq_port == QRTR_PORT_CTRL) {
941 if (len < 4) {
942 rc = -EINVAL;
943 kfree_skb(skb);
944 goto out_node;
947 /* control messages already require the type as 'command' */
948 skb_copy_bits(skb, 0, &qrtr_type, 4);
951 type = le32_to_cpu(qrtr_type);
952 rc = enqueue_fn(node, skb, type, &ipc->us, addr);
953 if (rc >= 0)
954 rc = len;
956 out_node:
957 qrtr_node_release(node);
958 release_sock(sk);
960 return rc;
963 static int qrtr_send_resume_tx(struct qrtr_cb *cb)
965 struct sockaddr_qrtr remote = { AF_QIPCRTR, cb->src_node, cb->src_port };
966 struct sockaddr_qrtr local = { AF_QIPCRTR, cb->dst_node, cb->dst_port };
967 struct qrtr_ctrl_pkt *pkt;
968 struct qrtr_node *node;
969 struct sk_buff *skb;
970 int ret;
972 node = qrtr_node_lookup(remote.sq_node);
973 if (!node)
974 return -EINVAL;
976 skb = qrtr_alloc_ctrl_packet(&pkt);
977 if (!skb)
978 return -ENOMEM;
980 pkt->cmd = cpu_to_le32(QRTR_TYPE_RESUME_TX);
981 pkt->client.node = cpu_to_le32(cb->dst_node);
982 pkt->client.port = cpu_to_le32(cb->dst_port);
984 ret = qrtr_node_enqueue(node, skb, QRTR_TYPE_RESUME_TX, &local, &remote);
986 qrtr_node_release(node);
988 return ret;
991 static int qrtr_recvmsg(struct socket *sock, struct msghdr *msg,
992 size_t size, int flags)
994 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg->msg_name);
995 struct sock *sk = sock->sk;
996 struct sk_buff *skb;
997 struct qrtr_cb *cb;
998 int copied, rc;
1000 lock_sock(sk);
1002 if (sock_flag(sk, SOCK_ZAPPED)) {
1003 release_sock(sk);
1004 return -EADDRNOTAVAIL;
1007 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1008 flags & MSG_DONTWAIT, &rc);
1009 if (!skb) {
1010 release_sock(sk);
1011 return rc;
1013 cb = (struct qrtr_cb *)skb->cb;
1015 copied = skb->len;
1016 if (copied > size) {
1017 copied = size;
1018 msg->msg_flags |= MSG_TRUNC;
1021 rc = skb_copy_datagram_msg(skb, 0, msg, copied);
1022 if (rc < 0)
1023 goto out;
1024 rc = copied;
1026 if (addr) {
1027 addr->sq_family = AF_QIPCRTR;
1028 addr->sq_node = cb->src_node;
1029 addr->sq_port = cb->src_port;
1030 msg->msg_namelen = sizeof(*addr);
1033 out:
1034 if (cb->confirm_rx)
1035 qrtr_send_resume_tx(cb);
1037 skb_free_datagram(sk, skb);
1038 release_sock(sk);
1040 return rc;
1043 static int qrtr_connect(struct socket *sock, struct sockaddr *saddr,
1044 int len, int flags)
1046 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, saddr);
1047 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
1048 struct sock *sk = sock->sk;
1049 int rc;
1051 if (len < sizeof(*addr) || addr->sq_family != AF_QIPCRTR)
1052 return -EINVAL;
1054 lock_sock(sk);
1056 sk->sk_state = TCP_CLOSE;
1057 sock->state = SS_UNCONNECTED;
1059 rc = qrtr_autobind(sock);
1060 if (rc) {
1061 release_sock(sk);
1062 return rc;
1065 ipc->peer = *addr;
1066 sock->state = SS_CONNECTED;
1067 sk->sk_state = TCP_ESTABLISHED;
1069 release_sock(sk);
1071 return 0;
1074 static int qrtr_getname(struct socket *sock, struct sockaddr *saddr,
1075 int peer)
1077 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
1078 struct sockaddr_qrtr qaddr;
1079 struct sock *sk = sock->sk;
1081 lock_sock(sk);
1082 if (peer) {
1083 if (sk->sk_state != TCP_ESTABLISHED) {
1084 release_sock(sk);
1085 return -ENOTCONN;
1088 qaddr = ipc->peer;
1089 } else {
1090 qaddr = ipc->us;
1092 release_sock(sk);
1094 qaddr.sq_family = AF_QIPCRTR;
1096 memcpy(saddr, &qaddr, sizeof(qaddr));
1098 return sizeof(qaddr);
1101 static int qrtr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1103 void __user *argp = (void __user *)arg;
1104 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
1105 struct sock *sk = sock->sk;
1106 struct sockaddr_qrtr *sq;
1107 struct sk_buff *skb;
1108 struct ifreq ifr;
1109 long len = 0;
1110 int rc = 0;
1112 lock_sock(sk);
1114 switch (cmd) {
1115 case TIOCOUTQ:
1116 len = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1117 if (len < 0)
1118 len = 0;
1119 rc = put_user(len, (int __user *)argp);
1120 break;
1121 case TIOCINQ:
1122 skb = skb_peek(&sk->sk_receive_queue);
1123 if (skb)
1124 len = skb->len;
1125 rc = put_user(len, (int __user *)argp);
1126 break;
1127 case SIOCGIFADDR:
1128 if (copy_from_user(&ifr, argp, sizeof(ifr))) {
1129 rc = -EFAULT;
1130 break;
1133 sq = (struct sockaddr_qrtr *)&ifr.ifr_addr;
1134 *sq = ipc->us;
1135 if (copy_to_user(argp, &ifr, sizeof(ifr))) {
1136 rc = -EFAULT;
1137 break;
1139 break;
1140 case SIOCADDRT:
1141 case SIOCDELRT:
1142 case SIOCSIFADDR:
1143 case SIOCGIFDSTADDR:
1144 case SIOCSIFDSTADDR:
1145 case SIOCGIFBRDADDR:
1146 case SIOCSIFBRDADDR:
1147 case SIOCGIFNETMASK:
1148 case SIOCSIFNETMASK:
1149 rc = -EINVAL;
1150 break;
1151 default:
1152 rc = -ENOIOCTLCMD;
1153 break;
1156 release_sock(sk);
1158 return rc;
1161 static int qrtr_release(struct socket *sock)
1163 struct sock *sk = sock->sk;
1164 struct qrtr_sock *ipc;
1166 if (!sk)
1167 return 0;
1169 lock_sock(sk);
1171 ipc = qrtr_sk(sk);
1172 sk->sk_shutdown = SHUTDOWN_MASK;
1173 if (!sock_flag(sk, SOCK_DEAD))
1174 sk->sk_state_change(sk);
1176 sock_set_flag(sk, SOCK_DEAD);
1177 sock->sk = NULL;
1179 if (!sock_flag(sk, SOCK_ZAPPED))
1180 qrtr_port_remove(ipc);
1182 skb_queue_purge(&sk->sk_receive_queue);
1184 release_sock(sk);
1185 sock_put(sk);
1187 return 0;
1190 static const struct proto_ops qrtr_proto_ops = {
1191 .owner = THIS_MODULE,
1192 .family = AF_QIPCRTR,
1193 .bind = qrtr_bind,
1194 .connect = qrtr_connect,
1195 .socketpair = sock_no_socketpair,
1196 .accept = sock_no_accept,
1197 .listen = sock_no_listen,
1198 .sendmsg = qrtr_sendmsg,
1199 .recvmsg = qrtr_recvmsg,
1200 .getname = qrtr_getname,
1201 .ioctl = qrtr_ioctl,
1202 .gettstamp = sock_gettstamp,
1203 .poll = datagram_poll,
1204 .shutdown = sock_no_shutdown,
1205 .setsockopt = sock_no_setsockopt,
1206 .getsockopt = sock_no_getsockopt,
1207 .release = qrtr_release,
1208 .mmap = sock_no_mmap,
1209 .sendpage = sock_no_sendpage,
1212 static struct proto qrtr_proto = {
1213 .name = "QIPCRTR",
1214 .owner = THIS_MODULE,
1215 .obj_size = sizeof(struct qrtr_sock),
1218 static int qrtr_create(struct net *net, struct socket *sock,
1219 int protocol, int kern)
1221 struct qrtr_sock *ipc;
1222 struct sock *sk;
1224 if (sock->type != SOCK_DGRAM)
1225 return -EPROTOTYPE;
1227 sk = sk_alloc(net, AF_QIPCRTR, GFP_KERNEL, &qrtr_proto, kern);
1228 if (!sk)
1229 return -ENOMEM;
1231 sock_set_flag(sk, SOCK_ZAPPED);
1233 sock_init_data(sock, sk);
1234 sock->ops = &qrtr_proto_ops;
1236 ipc = qrtr_sk(sk);
1237 ipc->us.sq_family = AF_QIPCRTR;
1238 ipc->us.sq_node = qrtr_local_nid;
1239 ipc->us.sq_port = 0;
1241 return 0;
1244 static const struct net_proto_family qrtr_family = {
1245 .owner = THIS_MODULE,
1246 .family = AF_QIPCRTR,
1247 .create = qrtr_create,
1250 static int __init qrtr_proto_init(void)
1252 int rc;
1254 rc = proto_register(&qrtr_proto, 1);
1255 if (rc)
1256 return rc;
1258 rc = sock_register(&qrtr_family);
1259 if (rc) {
1260 proto_unregister(&qrtr_proto);
1261 return rc;
1264 qrtr_ns_init();
1266 return rc;
1268 postcore_initcall(qrtr_proto_init);
1270 static void __exit qrtr_proto_fini(void)
1272 qrtr_ns_remove();
1273 sock_unregister(qrtr_family.family);
1274 proto_unregister(&qrtr_proto);
1276 module_exit(qrtr_proto_fini);
1278 MODULE_DESCRIPTION("Qualcomm IPC-router driver");
1279 MODULE_LICENSE("GPL v2");
1280 MODULE_ALIAS_NETPROTO(PF_QIPCRTR);