blob: 2d8d6131bc5f7e23b168af2dcd377d55c278cd8e [file] [log] [blame]
Thomas Gleixner97fb5e82019-05-29 07:17:58 -07001// SPDX-License-Identifier: GPL-2.0-only
Courtney Cavinbdabad32016-05-06 07:09:08 -07002/*
3 * Copyright (c) 2015, Sony Mobile Communications Inc.
4 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
Courtney Cavinbdabad32016-05-06 07:09:08 -07005 */
6#include <linux/module.h>
7#include <linux/netlink.h>
8#include <linux/qrtr.h>
9#include <linux/termios.h> /* For TIOCINQ/OUTQ */
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -080010#include <linux/spinlock.h>
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -080011#include <linux/wait.h>
Courtney Cavinbdabad32016-05-06 07:09:08 -070012
13#include <net/sock.h>
14
15#include "qrtr.h"
16
Bjorn Andersson194ccc82017-10-10 23:45:23 -070017#define QRTR_PROTO_VER_1 1
18#define QRTR_PROTO_VER_2 3
Courtney Cavinbdabad32016-05-06 07:09:08 -070019
20/* auto-bind range */
21#define QRTR_MIN_EPH_SOCKET 0x4000
22#define QRTR_MAX_EPH_SOCKET 0x7fff
23
Courtney Cavinbdabad32016-05-06 07:09:08 -070024/**
Bjorn Andersson194ccc82017-10-10 23:45:23 -070025 * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1
Courtney Cavinbdabad32016-05-06 07:09:08 -070026 * @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
34 */
Bjorn Andersson194ccc82017-10-10 23:45:23 -070035struct qrtr_hdr_v1 {
Courtney Cavinbdabad32016-05-06 07:09:08 -070036 __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;
45
Bjorn Andersson194ccc82017-10-10 23:45:23 -070046/**
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
57 */
58struct 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;
68};
69
70#define QRTR_FLAGS_CONFIRM_RX BIT(0)
71
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -070072struct qrtr_cb {
73 u32 src_node;
74 u32 src_port;
75 u32 dst_node;
76 u32 dst_port;
77
78 u8 type;
79 u8 confirm_rx;
80};
81
Bjorn Andersson194ccc82017-10-10 23:45:23 -070082#define QRTR_HDR_MAX_SIZE max_t(size_t, sizeof(struct qrtr_hdr_v1), \
83 sizeof(struct qrtr_hdr_v2))
Courtney Cavinbdabad32016-05-06 07:09:08 -070084
85struct qrtr_sock {
86 /* WARNING: sk must be the first member */
87 struct sock sk;
88 struct sockaddr_qrtr us;
89 struct sockaddr_qrtr peer;
90};
91
92static inline struct qrtr_sock *qrtr_sk(struct sock *sk)
93{
94 BUILD_BUG_ON(offsetof(struct qrtr_sock, sk) != 0);
95 return container_of(sk, struct qrtr_sock, sk);
96}
97
Manivannan Sadhasivam31d6cbe2020-02-20 20:43:27 +053098static unsigned int qrtr_local_nid = 1;
Courtney Cavinbdabad32016-05-06 07:09:08 -070099
100/* for node ids */
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800101static RADIX_TREE(qrtr_nodes, GFP_ATOMIC);
102static DEFINE_SPINLOCK(qrtr_nodes_lock);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700103/* broadcast list */
104static LIST_HEAD(qrtr_all_nodes);
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800105/* lock for qrtr_all_nodes and node reference */
Courtney Cavinbdabad32016-05-06 07:09:08 -0700106static DEFINE_MUTEX(qrtr_node_lock);
107
108/* local port allocation management */
109static DEFINE_IDR(qrtr_ports);
110static DEFINE_MUTEX(qrtr_port_lock);
111
112/**
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
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800118 * @qrtr_tx_flow: tree of qrtr_tx_flow, keyed by node << 32 | port
119 * @qrtr_tx_lock: lock for qrtr_tx_flow inserts
Courtney Cavinbdabad32016-05-06 07:09:08 -0700120 * @rx_queue: receive queue
Courtney Cavinbdabad32016-05-06 07:09:08 -0700121 * @item: list item for broadcast list
122 */
123struct qrtr_node {
124 struct mutex ep_lock;
125 struct qrtr_endpoint *ep;
126 struct kref ref;
127 unsigned int nid;
128
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800129 struct radix_tree_root qrtr_tx_flow;
130 struct mutex qrtr_tx_lock; /* for qrtr_tx_flow */
131
Courtney Cavinbdabad32016-05-06 07:09:08 -0700132 struct sk_buff_head rx_queue;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700133 struct list_head item;
134};
135
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800136/**
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
141 */
142struct qrtr_tx_flow {
143 struct wait_queue_head resume_tx;
144 int pending;
145 int tx_failed;
146};
147
148#define QRTR_TX_FLOW_HIGH 10
149#define QRTR_TX_FLOW_LOW 5
150
Bjorn Anderssone7044482017-10-10 23:45:20 -0700151static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,
152 int type, struct sockaddr_qrtr *from,
153 struct sockaddr_qrtr *to);
154static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,
155 int type, struct sockaddr_qrtr *from,
156 struct sockaddr_qrtr *to);
Bjorn Anderssone04df982020-01-13 23:57:03 -0800157static struct qrtr_sock *qrtr_port_lookup(int port);
158static void qrtr_port_put(struct qrtr_sock *ipc);
Bjorn Andersson8acc8ee2017-06-07 14:07:37 -0700159
Courtney Cavinbdabad32016-05-06 07:09:08 -0700160/* Release node resources and free the node.
161 *
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.
164 */
165static void __qrtr_node_release(struct kref *kref)
166{
167 struct qrtr_node *node = container_of(kref, struct qrtr_node, ref);
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800168 struct radix_tree_iter iter;
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800169 unsigned long flags;
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800170 void __rcu **slot;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700171
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800172 spin_lock_irqsave(&qrtr_nodes_lock, flags);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700173 if (node->nid != QRTR_EP_NID_AUTO)
174 radix_tree_delete(&qrtr_nodes, node->nid);
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800175 spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700176
177 list_del(&node->item);
178 mutex_unlock(&qrtr_node_lock);
179
180 skb_queue_purge(&node->rx_queue);
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800181
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);
186 }
Courtney Cavinbdabad32016-05-06 07:09:08 -0700187 kfree(node);
188}
189
190/* Increment reference to node. */
191static struct qrtr_node *qrtr_node_acquire(struct qrtr_node *node)
192{
193 if (node)
194 kref_get(&node->ref);
195 return node;
196}
197
198/* Decrement reference to node and release as necessary. */
199static void qrtr_node_release(struct qrtr_node *node)
200{
201 if (!node)
202 return;
203 kref_put_mutex(&node->ref, __qrtr_node_release, &qrtr_node_lock);
204}
205
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800206/**
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
210 */
211static void qrtr_tx_resume(struct qrtr_node *node, struct sk_buff *skb)
212{
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;
218
219 key = remote_node << 32 | remote_port;
220
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);
229 }
230
231 consume_skb(skb);
232}
233
234/**
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
240 *
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.
246 *
247 * Return: 1 if confirm_rx should be set, 0 otherwise or errno failure
248 */
249static int qrtr_tx_wait(struct qrtr_node *node, int dest_node, int dest_port,
250 int type)
251{
252 unsigned long key = (u64)dest_node << 32 | dest_port;
253 struct qrtr_tx_flow *flow;
254 int confirm_rx = 0;
255 int ret;
256
257 /* Never set confirm_rx on non-data packets */
258 if (type != QRTR_TYPE_DATA)
259 return 0;
260
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);
268 }
269 }
270 mutex_unlock(&node->qrtr_tx_lock);
271
272 /* Set confirm_rx if we where unable to find and allocate a flow */
273 if (!flow)
274 return 1;
275
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;
291 }
292 spin_unlock_irq(&flow->resume_tx.lock);
293
294 return confirm_rx;
295}
296
297/**
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
302 *
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.
309 */
310static void qrtr_tx_flow_failed(struct qrtr_node *node, int dest_node,
311 int dest_port)
312{
313 unsigned long key = (u64)dest_node << 32 | dest_port;
314 struct qrtr_tx_flow *flow;
315
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);
323 }
324}
325
Courtney Cavinbdabad32016-05-06 07:09:08 -0700326/* Pass an outgoing packet socket buffer to the endpoint driver. */
Bjorn Anderssone7044482017-10-10 23:45:20 -0700327static int qrtr_node_enqueue(struct qrtr_node *node, struct sk_buff *skb,
328 int type, struct sockaddr_qrtr *from,
329 struct sockaddr_qrtr *to)
Courtney Cavinbdabad32016-05-06 07:09:08 -0700330{
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700331 struct qrtr_hdr_v1 *hdr;
Bjorn Anderssone7044482017-10-10 23:45:20 -0700332 size_t len = skb->len;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700333 int rc = -ENODEV;
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800334 int confirm_rx;
335
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;
340 }
Courtney Cavinbdabad32016-05-06 07:09:08 -0700341
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700342 hdr = skb_push(skb, sizeof(*hdr));
343 hdr->version = cpu_to_le32(QRTR_PROTO_VER_1);
Bjorn Anderssone7044482017-10-10 23:45:20 -0700344 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);
Arun Kumar Neelakantamd27e77a2018-07-04 19:49:33 +0530347 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);
353 }
Bjorn Anderssone7044482017-10-10 23:45:20 -0700354
355 hdr->size = cpu_to_le32(len);
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800356 hdr->confirm_rx = !!confirm_rx;
Bjorn Anderssone7044482017-10-10 23:45:20 -0700357
Carl Huangce577852020-01-03 12:50:16 +0800358 skb_put_padto(skb, ALIGN(len, 4) + sizeof(*hdr));
Bjorn Anderssone7044482017-10-10 23:45:20 -0700359
Courtney Cavinbdabad32016-05-06 07:09:08 -0700360 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);
366
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800367 /* 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);
371
Courtney Cavinbdabad32016-05-06 07:09:08 -0700372 return rc;
373}
374
375/* Lookup node by id.
376 *
377 * callers must release with qrtr_node_release()
378 */
379static struct qrtr_node *qrtr_node_lookup(unsigned int nid)
380{
381 struct qrtr_node *node;
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800382 unsigned long flags;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700383
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800384 spin_lock_irqsave(&qrtr_nodes_lock, flags);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700385 node = radix_tree_lookup(&qrtr_nodes, nid);
386 node = qrtr_node_acquire(node);
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800387 spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700388
389 return node;
390}
391
392/* Assign node id to node.
393 *
394 * This is mostly useful for automatic node id assignment, based on
395 * the source id in the incoming packet.
396 */
397static void qrtr_node_assign(struct qrtr_node *node, unsigned int nid)
398{
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800399 unsigned long flags;
400
Courtney Cavinbdabad32016-05-06 07:09:08 -0700401 if (node->nid != QRTR_EP_NID_AUTO || nid == QRTR_EP_NID_AUTO)
402 return;
403
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800404 spin_lock_irqsave(&qrtr_nodes_lock, flags);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700405 radix_tree_insert(&qrtr_nodes, nid, node);
406 node->nid = nid;
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800407 spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700408}
409
410/**
411 * qrtr_endpoint_post() - post incoming data
412 * @ep: endpoint handle
413 * @data: data pointer
414 * @len: size of data in bytes
415 *
416 * Return: 0 on success; negative error code on failure
417 */
418int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
419{
420 struct qrtr_node *node = ep->node;
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700421 const struct qrtr_hdr_v1 *v1;
422 const struct qrtr_hdr_v2 *v2;
Bjorn Anderssone04df982020-01-13 23:57:03 -0800423 struct qrtr_sock *ipc;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700424 struct sk_buff *skb;
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -0700425 struct qrtr_cb *cb;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700426 unsigned int size;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700427 unsigned int ver;
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700428 size_t hdrlen;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700429
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700430 if (len & 3)
Courtney Cavinbdabad32016-05-06 07:09:08 -0700431 return -EINVAL;
432
433 skb = netdev_alloc_skb(NULL, len);
434 if (!skb)
435 return -ENOMEM;
436
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -0700437 cb = (struct qrtr_cb *)skb->cb;
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -0700438
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700439 /* Version field in v1 is little endian, so this works for both cases */
440 ver = *(u8*)data;
441
442 switch (ver) {
443 case QRTR_PROTO_VER_1:
444 v1 = data;
445 hdrlen = sizeof(*v1);
446
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);
453
454 size = le32_to_cpu(v1->size);
455 break;
456 case QRTR_PROTO_VER_2:
457 v2 = data;
458 hdrlen = sizeof(*v2) + v2->optlen;
459
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);
466
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;
471
472 size = le32_to_cpu(v2->size);
473 break;
474 default:
475 pr_err("qrtr: Invalid version %d\n", ver);
476 goto err;
477 }
478
479 if (len != ALIGN(size, 4) + hdrlen)
480 goto err;
481
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800482 if (cb->dst_port != QRTR_PORT_CTRL && cb->type != QRTR_TYPE_DATA &&
483 cb->type != QRTR_TYPE_RESUME_TX)
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700484 goto err;
485
486 skb_put_data(skb, data + hdrlen, size);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700487
Bjorn Anderssone04df982020-01-13 23:57:03 -0800488 qrtr_node_assign(node, cb->src_node);
489
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;
496
497 if (sock_queue_rcv_skb(&ipc->sk, skb))
498 goto err;
499
500 qrtr_port_put(ipc);
501 }
Courtney Cavinbdabad32016-05-06 07:09:08 -0700502
503 return 0;
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700504
505err:
506 kfree_skb(skb);
507 return -EINVAL;
508
Courtney Cavinbdabad32016-05-06 07:09:08 -0700509}
510EXPORT_SYMBOL_GPL(qrtr_endpoint_post);
511
Bjorn Andersson1a7959c2017-10-10 23:45:21 -0700512/**
513 * qrtr_alloc_ctrl_packet() - allocate control packet skb
514 * @pkt: reference to qrtr_ctrl_pkt pointer
515 *
516 * Returns newly allocated sk_buff, or NULL on failure
517 *
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.
520 */
521static struct sk_buff *qrtr_alloc_ctrl_packet(struct qrtr_ctrl_pkt **pkt)
Courtney Cavinbdabad32016-05-06 07:09:08 -0700522{
Bjorn Andersson1a7959c2017-10-10 23:45:21 -0700523 const int pkt_len = sizeof(struct qrtr_ctrl_pkt);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700524 struct sk_buff *skb;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700525
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700526 skb = alloc_skb(QRTR_HDR_MAX_SIZE + pkt_len, GFP_KERNEL);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700527 if (!skb)
528 return NULL;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700529
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700530 skb_reserve(skb, QRTR_HDR_MAX_SIZE);
Bjorn Andersson1a7959c2017-10-10 23:45:21 -0700531 *pkt = skb_put_zero(skb, pkt_len);
Bjorn Andersson17844732017-06-07 14:07:38 -0700532
533 return skb;
534}
535
Courtney Cavinbdabad32016-05-06 07:09:08 -0700536/**
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
541 *
542 * The specified endpoint must have the xmit function pointer set on call.
543 */
544int qrtr_endpoint_register(struct qrtr_endpoint *ep, unsigned int nid)
545{
546 struct qrtr_node *node;
547
548 if (!ep || !ep->xmit)
549 return -EINVAL;
550
551 node = kzalloc(sizeof(*node), GFP_KERNEL);
552 if (!node)
553 return -ENOMEM;
554
Courtney Cavinbdabad32016-05-06 07:09:08 -0700555 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;
560
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800561 INIT_RADIX_TREE(&node->qrtr_tx_flow, GFP_KERNEL);
562 mutex_init(&node->qrtr_tx_lock);
563
Courtney Cavinbdabad32016-05-06 07:09:08 -0700564 qrtr_node_assign(node, nid);
565
566 mutex_lock(&qrtr_node_lock);
567 list_add(&node->item, &qrtr_all_nodes);
568 mutex_unlock(&qrtr_node_lock);
569 ep->node = node;
570
571 return 0;
572}
573EXPORT_SYMBOL_GPL(qrtr_endpoint_register);
574
575/**
576 * qrtr_endpoint_unregister - unregister endpoint
577 * @ep: endpoint to unregister
578 */
579void qrtr_endpoint_unregister(struct qrtr_endpoint *ep)
580{
581 struct qrtr_node *node = ep->node;
Bjorn Anderssone7044482017-10-10 23:45:20 -0700582 struct sockaddr_qrtr src = {AF_QIPCRTR, node->nid, QRTR_PORT_CTRL};
583 struct sockaddr_qrtr dst = {AF_QIPCRTR, qrtr_local_nid, QRTR_PORT_CTRL};
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800584 struct radix_tree_iter iter;
Bjorn Andersson1a7959c2017-10-10 23:45:21 -0700585 struct qrtr_ctrl_pkt *pkt;
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800586 struct qrtr_tx_flow *flow;
Bjorn Andersson8acc8ee2017-06-07 14:07:37 -0700587 struct sk_buff *skb;
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800588 void __rcu **slot;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700589
590 mutex_lock(&node->ep_lock);
591 node->ep = NULL;
592 mutex_unlock(&node->ep_lock);
593
Bjorn Andersson8acc8ee2017-06-07 14:07:37 -0700594 /* Notify the local controller about the event */
Bjorn Andersson1a7959c2017-10-10 23:45:21 -0700595 skb = qrtr_alloc_ctrl_packet(&pkt);
596 if (skb) {
597 pkt->cmd = cpu_to_le32(QRTR_TYPE_BYE);
Bjorn Anderssone7044482017-10-10 23:45:20 -0700598 qrtr_local_enqueue(NULL, skb, QRTR_TYPE_BYE, &src, &dst);
Bjorn Andersson1a7959c2017-10-10 23:45:21 -0700599 }
Bjorn Andersson8acc8ee2017-06-07 14:07:37 -0700600
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800601 /* 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);
606 }
607 mutex_unlock(&node->qrtr_tx_lock);
608
Courtney Cavinbdabad32016-05-06 07:09:08 -0700609 qrtr_node_release(node);
610 ep->node = NULL;
611}
612EXPORT_SYMBOL_GPL(qrtr_endpoint_unregister);
613
614/* Lookup socket by port.
615 *
616 * Callers must release with qrtr_port_put()
617 */
618static struct qrtr_sock *qrtr_port_lookup(int port)
619{
620 struct qrtr_sock *ipc;
621
622 if (port == QRTR_PORT_CTRL)
623 port = 0;
624
Bjorn Anderssonf16a4b262020-01-13 23:57:02 -0800625 rcu_read_lock();
Courtney Cavinbdabad32016-05-06 07:09:08 -0700626 ipc = idr_find(&qrtr_ports, port);
627 if (ipc)
628 sock_hold(&ipc->sk);
Bjorn Anderssonf16a4b262020-01-13 23:57:02 -0800629 rcu_read_unlock();
Courtney Cavinbdabad32016-05-06 07:09:08 -0700630
631 return ipc;
632}
633
634/* Release acquired socket. */
635static void qrtr_port_put(struct qrtr_sock *ipc)
636{
637 sock_put(&ipc->sk);
638}
639
640/* Remove port assignment. */
641static void qrtr_port_remove(struct qrtr_sock *ipc)
642{
Bjorn Andersson1a7959c2017-10-10 23:45:21 -0700643 struct qrtr_ctrl_pkt *pkt;
Bjorn Andersson17844732017-06-07 14:07:38 -0700644 struct sk_buff *skb;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700645 int port = ipc->us.sq_port;
Bjorn Anderssone7044482017-10-10 23:45:20 -0700646 struct sockaddr_qrtr to;
647
648 to.sq_family = AF_QIPCRTR;
649 to.sq_node = QRTR_NODE_BCAST;
650 to.sq_port = QRTR_PORT_CTRL;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700651
Bjorn Andersson1a7959c2017-10-10 23:45:21 -0700652 skb = qrtr_alloc_ctrl_packet(&pkt);
Bjorn Andersson17844732017-06-07 14:07:38 -0700653 if (skb) {
Bjorn Andersson1a7959c2017-10-10 23:45:21 -0700654 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);
657
Bjorn Andersson17844732017-06-07 14:07:38 -0700658 skb_set_owner_w(skb, &ipc->sk);
Bjorn Anderssone7044482017-10-10 23:45:20 -0700659 qrtr_bcast_enqueue(NULL, skb, QRTR_TYPE_DEL_CLIENT, &ipc->us,
660 &to);
Bjorn Andersson17844732017-06-07 14:07:38 -0700661 }
662
Courtney Cavinbdabad32016-05-06 07:09:08 -0700663 if (port == QRTR_PORT_CTRL)
664 port = 0;
665
666 __sock_put(&ipc->sk);
667
668 mutex_lock(&qrtr_port_lock);
669 idr_remove(&qrtr_ports, port);
670 mutex_unlock(&qrtr_port_lock);
Bjorn Anderssonf16a4b262020-01-13 23:57:02 -0800671
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();
Courtney Cavinbdabad32016-05-06 07:09:08 -0700675}
676
677/* Assign port number to socket.
678 *
679 * Specify port in the integer pointed to by port, and it will be adjusted
680 * on return as necesssary.
681 *
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
686 */
687static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
688{
689 int rc;
690
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;
706 }
707 mutex_unlock(&qrtr_port_lock);
708
709 if (rc == -ENOSPC)
710 return -EADDRINUSE;
711 else if (rc < 0)
712 return rc;
713
714 sock_hold(&ipc->sk);
715
716 return 0;
717}
718
Bjorn Anderssonb24844b2017-06-07 14:07:39 -0700719/* Reset all non-control ports */
720static void qrtr_reset_ports(void)
721{
722 struct qrtr_sock *ipc;
723 int id;
724
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;
730
731 sock_hold(&ipc->sk);
732 ipc->sk.sk_err = ENETRESET;
Bjorn Anderssonae85bfa2017-10-10 23:45:17 -0700733 ipc->sk.sk_error_report(&ipc->sk);
Bjorn Anderssonb24844b2017-06-07 14:07:39 -0700734 sock_put(&ipc->sk);
735 }
736 mutex_unlock(&qrtr_port_lock);
737}
738
Courtney Cavinbdabad32016-05-06 07:09:08 -0700739/* Bind socket to address.
740 *
741 * Socket should be locked upon call.
742 */
743static int __qrtr_bind(struct socket *sock,
744 const struct sockaddr_qrtr *addr, int zapped)
745{
746 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
747 struct sock *sk = sock->sk;
748 int port;
749 int rc;
750
751 /* rebinding ok */
752 if (!zapped && addr->sq_port == ipc->us.sq_port)
753 return 0;
754
755 port = addr->sq_port;
756 rc = qrtr_port_assign(ipc, &port);
757 if (rc)
758 return rc;
759
760 /* unbind previous, if any */
761 if (!zapped)
762 qrtr_port_remove(ipc);
763 ipc->us.sq_port = port;
764
765 sock_reset_flag(sk, SOCK_ZAPPED);
766
Bjorn Anderssonb24844b2017-06-07 14:07:39 -0700767 /* Notify all open ports about the new controller */
768 if (port == QRTR_PORT_CTRL)
769 qrtr_reset_ports();
770
Courtney Cavinbdabad32016-05-06 07:09:08 -0700771 return 0;
772}
773
774/* Auto bind to an ephemeral port. */
775static int qrtr_autobind(struct socket *sock)
776{
777 struct sock *sk = sock->sk;
778 struct sockaddr_qrtr addr;
779
780 if (!sock_flag(sk, SOCK_ZAPPED))
781 return 0;
782
783 addr.sq_family = AF_QIPCRTR;
784 addr.sq_node = qrtr_local_nid;
785 addr.sq_port = 0;
786
787 return __qrtr_bind(sock, &addr, 1);
788}
789
790/* Bind socket to specified sockaddr. */
791static int qrtr_bind(struct socket *sock, struct sockaddr *saddr, int len)
792{
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;
797
798 if (len < sizeof(*addr) || addr->sq_family != AF_QIPCRTR)
799 return -EINVAL;
800
801 if (addr->sq_node != ipc->us.sq_node)
802 return -EINVAL;
803
804 lock_sock(sk);
805 rc = __qrtr_bind(sock, addr, sock_flag(sk, SOCK_ZAPPED));
806 release_sock(sk);
807
808 return rc;
809}
810
811/* Queue packet to local peer socket. */
Bjorn Anderssone7044482017-10-10 23:45:20 -0700812static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,
813 int type, struct sockaddr_qrtr *from,
814 struct sockaddr_qrtr *to)
Courtney Cavinbdabad32016-05-06 07:09:08 -0700815{
Courtney Cavinbdabad32016-05-06 07:09:08 -0700816 struct qrtr_sock *ipc;
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -0700817 struct qrtr_cb *cb;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700818
Bjorn Anderssone7044482017-10-10 23:45:20 -0700819 ipc = qrtr_port_lookup(to->sq_port);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700820 if (!ipc || &ipc->sk == skb->sk) { /* do not send to self */
821 kfree_skb(skb);
822 return -ENODEV;
823 }
824
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -0700825 cb = (struct qrtr_cb *)skb->cb;
826 cb->src_node = from->sq_node;
827 cb->src_port = from->sq_port;
Bjorn Anderssone7044482017-10-10 23:45:20 -0700828
Courtney Cavinbdabad32016-05-06 07:09:08 -0700829 if (sock_queue_rcv_skb(&ipc->sk, skb)) {
830 qrtr_port_put(ipc);
831 kfree_skb(skb);
832 return -ENOSPC;
833 }
834
835 qrtr_port_put(ipc);
836
837 return 0;
838}
839
840/* Queue packet for broadcast. */
Bjorn Anderssone7044482017-10-10 23:45:20 -0700841static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,
842 int type, struct sockaddr_qrtr *from,
843 struct sockaddr_qrtr *to)
Courtney Cavinbdabad32016-05-06 07:09:08 -0700844{
845 struct sk_buff *skbn;
846
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);
Bjorn Anderssone7044482017-10-10 23:45:20 -0700853 qrtr_node_enqueue(node, skbn, type, from, to);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700854 }
855 mutex_unlock(&qrtr_node_lock);
856
Manivannan Sadhasivamd28ea1f2020-05-19 23:44:16 +0530857 qrtr_local_enqueue(NULL, skb, type, from, to);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700858
859 return 0;
860}
861
862static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
863{
864 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg->msg_name);
Bjorn Anderssone7044482017-10-10 23:45:20 -0700865 int (*enqueue_fn)(struct qrtr_node *, struct sk_buff *, int,
866 struct sockaddr_qrtr *, struct sockaddr_qrtr *);
Nicholas Mc Guire8f5e2452019-05-11 02:56:33 +0200867 __le32 qrtr_type = cpu_to_le32(QRTR_TYPE_DATA);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700868 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
869 struct sock *sk = sock->sk;
870 struct qrtr_node *node;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700871 struct sk_buff *skb;
872 size_t plen;
Bjorn Andersson7036e622019-05-20 16:51:56 -0700873 u32 type;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700874 int rc;
875
876 if (msg->msg_flags & ~(MSG_DONTWAIT))
877 return -EINVAL;
878
879 if (len > 65535)
880 return -EMSGSIZE;
881
882 lock_sock(sk);
883
884 if (addr) {
885 if (msg->msg_namelen < sizeof(*addr)) {
886 release_sock(sk);
887 return -EINVAL;
888 }
889
890 if (addr->sq_family != AF_QIPCRTR) {
891 release_sock(sk);
892 return -EINVAL;
893 }
894
895 rc = qrtr_autobind(sock);
896 if (rc) {
897 release_sock(sk);
898 return rc;
899 }
900 } else if (sk->sk_state == TCP_ESTABLISHED) {
901 addr = &ipc->peer;
902 } else {
903 release_sock(sk);
904 return -ENOTCONN;
905 }
906
907 node = NULL;
908 if (addr->sq_node == QRTR_NODE_BCAST) {
Wang Wenhu6dbf02a2020-04-08 19:53:53 -0700909 if (addr->sq_port != QRTR_PORT_CTRL &&
910 qrtr_local_nid != QRTR_NODE_BCAST) {
Arun Kumar Neelakantamfdf5fd32018-07-04 19:49:32 +0530911 release_sock(sk);
912 return -ENOTCONN;
913 }
Wang Wenhu6dbf02a2020-04-08 19:53:53 -0700914 enqueue_fn = qrtr_bcast_enqueue;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700915 } else if (addr->sq_node == ipc->us.sq_node) {
916 enqueue_fn = qrtr_local_enqueue;
917 } else {
Courtney Cavinbdabad32016-05-06 07:09:08 -0700918 node = qrtr_node_lookup(addr->sq_node);
919 if (!node) {
920 release_sock(sk);
921 return -ECONNRESET;
922 }
Wang Wenhu6dbf02a2020-04-08 19:53:53 -0700923 enqueue_fn = qrtr_node_enqueue;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700924 }
925
926 plen = (len + 3) & ~3;
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700927 skb = sock_alloc_send_skb(sk, plen + QRTR_HDR_MAX_SIZE,
Courtney Cavinbdabad32016-05-06 07:09:08 -0700928 msg->msg_flags & MSG_DONTWAIT, &rc);
929 if (!skb)
930 goto out_node;
931
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700932 skb_reserve(skb, QRTR_HDR_MAX_SIZE);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700933
Bjorn Anderssone7044482017-10-10 23:45:20 -0700934 rc = memcpy_from_msg(skb_put(skb, len), msg, len);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700935 if (rc) {
936 kfree_skb(skb);
937 goto out_node;
938 }
939
Courtney Cavinbdabad32016-05-06 07:09:08 -0700940 if (ipc->us.sq_port == QRTR_PORT_CTRL) {
941 if (len < 4) {
942 rc = -EINVAL;
943 kfree_skb(skb);
944 goto out_node;
945 }
946
947 /* control messages already require the type as 'command' */
Nicholas Mc Guire8f5e2452019-05-11 02:56:33 +0200948 skb_copy_bits(skb, 0, &qrtr_type, 4);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700949 }
950
Bjorn Andersson7036e622019-05-20 16:51:56 -0700951 type = le32_to_cpu(qrtr_type);
Bjorn Anderssone7044482017-10-10 23:45:20 -0700952 rc = enqueue_fn(node, skb, type, &ipc->us, addr);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700953 if (rc >= 0)
954 rc = len;
955
956out_node:
957 qrtr_node_release(node);
958 release_sock(sk);
959
960 return rc;
961}
962
Bjorn Anderssoncb6530b2020-01-13 23:56:59 -0800963static int qrtr_send_resume_tx(struct qrtr_cb *cb)
964{
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;
971
972 node = qrtr_node_lookup(remote.sq_node);
973 if (!node)
974 return -EINVAL;
975
976 skb = qrtr_alloc_ctrl_packet(&pkt);
977 if (!skb)
978 return -ENOMEM;
979
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);
983
984 ret = qrtr_node_enqueue(node, skb, QRTR_TYPE_RESUME_TX, &local, &remote);
985
986 qrtr_node_release(node);
987
988 return ret;
989}
990
Courtney Cavinbdabad32016-05-06 07:09:08 -0700991static int qrtr_recvmsg(struct socket *sock, struct msghdr *msg,
992 size_t size, int flags)
993{
994 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg->msg_name);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700995 struct sock *sk = sock->sk;
996 struct sk_buff *skb;
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -0700997 struct qrtr_cb *cb;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700998 int copied, rc;
999
1000 lock_sock(sk);
1001
1002 if (sock_flag(sk, SOCK_ZAPPED)) {
1003 release_sock(sk);
1004 return -EADDRNOTAVAIL;
1005 }
1006
1007 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1008 flags & MSG_DONTWAIT, &rc);
1009 if (!skb) {
1010 release_sock(sk);
1011 return rc;
1012 }
Bjorn Anderssoncb6530b2020-01-13 23:56:59 -08001013 cb = (struct qrtr_cb *)skb->cb;
Courtney Cavinbdabad32016-05-06 07:09:08 -07001014
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -07001015 copied = skb->len;
Courtney Cavinbdabad32016-05-06 07:09:08 -07001016 if (copied > size) {
1017 copied = size;
1018 msg->msg_flags |= MSG_TRUNC;
1019 }
1020
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -07001021 rc = skb_copy_datagram_msg(skb, 0, msg, copied);
Courtney Cavinbdabad32016-05-06 07:09:08 -07001022 if (rc < 0)
1023 goto out;
1024 rc = copied;
1025
1026 if (addr) {
1027 addr->sq_family = AF_QIPCRTR;
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -07001028 addr->sq_node = cb->src_node;
1029 addr->sq_port = cb->src_port;
Courtney Cavinbdabad32016-05-06 07:09:08 -07001030 msg->msg_namelen = sizeof(*addr);
1031 }
1032
1033out:
Bjorn Anderssoncb6530b2020-01-13 23:56:59 -08001034 if (cb->confirm_rx)
1035 qrtr_send_resume_tx(cb);
1036
Courtney Cavinbdabad32016-05-06 07:09:08 -07001037 skb_free_datagram(sk, skb);
1038 release_sock(sk);
1039
1040 return rc;
1041}
1042
1043static int qrtr_connect(struct socket *sock, struct sockaddr *saddr,
1044 int len, int flags)
1045{
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;
1050
1051 if (len < sizeof(*addr) || addr->sq_family != AF_QIPCRTR)
1052 return -EINVAL;
1053
1054 lock_sock(sk);
1055
1056 sk->sk_state = TCP_CLOSE;
1057 sock->state = SS_UNCONNECTED;
1058
1059 rc = qrtr_autobind(sock);
1060 if (rc) {
1061 release_sock(sk);
1062 return rc;
1063 }
1064
1065 ipc->peer = *addr;
1066 sock->state = SS_CONNECTED;
1067 sk->sk_state = TCP_ESTABLISHED;
1068
1069 release_sock(sk);
1070
1071 return 0;
1072}
1073
1074static int qrtr_getname(struct socket *sock, struct sockaddr *saddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001075 int peer)
Courtney Cavinbdabad32016-05-06 07:09:08 -07001076{
1077 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
1078 struct sockaddr_qrtr qaddr;
1079 struct sock *sk = sock->sk;
1080
1081 lock_sock(sk);
1082 if (peer) {
1083 if (sk->sk_state != TCP_ESTABLISHED) {
1084 release_sock(sk);
1085 return -ENOTCONN;
1086 }
1087
1088 qaddr = ipc->peer;
1089 } else {
1090 qaddr = ipc->us;
1091 }
1092 release_sock(sk);
1093
Courtney Cavinbdabad32016-05-06 07:09:08 -07001094 qaddr.sq_family = AF_QIPCRTR;
1095
1096 memcpy(saddr, &qaddr, sizeof(qaddr));
1097
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001098 return sizeof(qaddr);
Courtney Cavinbdabad32016-05-06 07:09:08 -07001099}
1100
1101static int qrtr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1102{
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;
1111
1112 lock_sock(sk);
1113
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)
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -07001124 len = skb->len;
Courtney Cavinbdabad32016-05-06 07:09:08 -07001125 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;
1131 }
1132
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;
1138 }
1139 break;
Courtney Cavinbdabad32016-05-06 07:09:08 -07001140 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;
1154 }
1155
1156 release_sock(sk);
1157
1158 return rc;
1159}
1160
1161static int qrtr_release(struct socket *sock)
1162{
1163 struct sock *sk = sock->sk;
1164 struct qrtr_sock *ipc;
1165
1166 if (!sk)
1167 return 0;
1168
1169 lock_sock(sk);
1170
1171 ipc = qrtr_sk(sk);
1172 sk->sk_shutdown = SHUTDOWN_MASK;
1173 if (!sock_flag(sk, SOCK_DEAD))
1174 sk->sk_state_change(sk);
1175
1176 sock_set_flag(sk, SOCK_DEAD);
1177 sock->sk = NULL;
1178
1179 if (!sock_flag(sk, SOCK_ZAPPED))
1180 qrtr_port_remove(ipc);
1181
1182 skb_queue_purge(&sk->sk_receive_queue);
1183
1184 release_sock(sk);
1185 sock_put(sk);
1186
1187 return 0;
1188}
1189
1190static 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,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02001202 .gettstamp = sock_gettstamp,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001203 .poll = datagram_poll,
Courtney Cavinbdabad32016-05-06 07:09:08 -07001204 .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,
1210};
1211
1212static struct proto qrtr_proto = {
1213 .name = "QIPCRTR",
1214 .owner = THIS_MODULE,
1215 .obj_size = sizeof(struct qrtr_sock),
1216};
1217
1218static int qrtr_create(struct net *net, struct socket *sock,
1219 int protocol, int kern)
1220{
1221 struct qrtr_sock *ipc;
1222 struct sock *sk;
1223
1224 if (sock->type != SOCK_DGRAM)
1225 return -EPROTOTYPE;
1226
1227 sk = sk_alloc(net, AF_QIPCRTR, GFP_KERNEL, &qrtr_proto, kern);
1228 if (!sk)
1229 return -ENOMEM;
1230
1231 sock_set_flag(sk, SOCK_ZAPPED);
1232
1233 sock_init_data(sock, sk);
1234 sock->ops = &qrtr_proto_ops;
1235
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;
1240
1241 return 0;
1242}
1243
Courtney Cavinbdabad32016-05-06 07:09:08 -07001244static const struct net_proto_family qrtr_family = {
1245 .owner = THIS_MODULE,
1246 .family = AF_QIPCRTR,
1247 .create = qrtr_create,
1248};
1249
1250static int __init qrtr_proto_init(void)
1251{
1252 int rc;
1253
1254 rc = proto_register(&qrtr_proto, 1);
1255 if (rc)
1256 return rc;
1257
1258 rc = sock_register(&qrtr_family);
1259 if (rc) {
1260 proto_unregister(&qrtr_proto);
1261 return rc;
1262 }
1263
Bjorn Andersson71046ab2020-03-01 23:03:05 -08001264 qrtr_ns_init();
Courtney Cavinbdabad32016-05-06 07:09:08 -07001265
Florian Westphalc1c502b2017-12-02 21:44:07 +01001266 return rc;
Courtney Cavinbdabad32016-05-06 07:09:08 -07001267}
Bjorn Anderssonb7e732f2017-11-06 20:50:35 -08001268postcore_initcall(qrtr_proto_init);
Courtney Cavinbdabad32016-05-06 07:09:08 -07001269
1270static void __exit qrtr_proto_fini(void)
1271{
Manivannan Sadhasivam0c2204a2020-02-20 20:43:26 +05301272 qrtr_ns_remove();
Courtney Cavinbdabad32016-05-06 07:09:08 -07001273 sock_unregister(qrtr_family.family);
1274 proto_unregister(&qrtr_proto);
1275}
1276module_exit(qrtr_proto_fini);
1277
1278MODULE_DESCRIPTION("Qualcomm IPC-router driver");
1279MODULE_LICENSE("GPL v2");
Nicolas Dechesne77ac7252018-04-17 14:03:26 +02001280MODULE_ALIAS_NETPROTO(PF_QIPCRTR);