blob: 300a104b9a0fb02ba3e7224a0f11a91acb6e05d6 [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;
Carl Huang28541f32020-06-30 14:52:51 +0800169 struct qrtr_tx_flow *flow;
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800170 unsigned long flags;
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800171 void __rcu **slot;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700172
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800173 spin_lock_irqsave(&qrtr_nodes_lock, flags);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700174 if (node->nid != QRTR_EP_NID_AUTO)
175 radix_tree_delete(&qrtr_nodes, node->nid);
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800176 spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700177
178 list_del(&node->item);
179 mutex_unlock(&qrtr_node_lock);
180
181 skb_queue_purge(&node->rx_queue);
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800182
183 /* Free tx flow counters */
184 radix_tree_for_each_slot(slot, &node->qrtr_tx_flow, &iter, 0) {
Carl Huang28541f32020-06-30 14:52:51 +0800185 flow = *slot;
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800186 radix_tree_iter_delete(&node->qrtr_tx_flow, &iter, slot);
Carl Huang28541f32020-06-30 14:52:51 +0800187 kfree(flow);
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800188 }
Courtney Cavinbdabad32016-05-06 07:09:08 -0700189 kfree(node);
190}
191
192/* Increment reference to node. */
193static struct qrtr_node *qrtr_node_acquire(struct qrtr_node *node)
194{
195 if (node)
196 kref_get(&node->ref);
197 return node;
198}
199
200/* Decrement reference to node and release as necessary. */
201static void qrtr_node_release(struct qrtr_node *node)
202{
203 if (!node)
204 return;
205 kref_put_mutex(&node->ref, __qrtr_node_release, &qrtr_node_lock);
206}
207
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800208/**
209 * qrtr_tx_resume() - reset flow control counter
210 * @node: qrtr_node that the QRTR_TYPE_RESUME_TX packet arrived on
211 * @skb: resume_tx packet
212 */
213static void qrtr_tx_resume(struct qrtr_node *node, struct sk_buff *skb)
214{
215 struct qrtr_ctrl_pkt *pkt = (struct qrtr_ctrl_pkt *)skb->data;
216 u64 remote_node = le32_to_cpu(pkt->client.node);
217 u32 remote_port = le32_to_cpu(pkt->client.port);
218 struct qrtr_tx_flow *flow;
219 unsigned long key;
220
221 key = remote_node << 32 | remote_port;
222
223 rcu_read_lock();
224 flow = radix_tree_lookup(&node->qrtr_tx_flow, key);
225 rcu_read_unlock();
226 if (flow) {
227 spin_lock(&flow->resume_tx.lock);
228 flow->pending = 0;
229 spin_unlock(&flow->resume_tx.lock);
230 wake_up_interruptible_all(&flow->resume_tx);
231 }
232
233 consume_skb(skb);
234}
235
236/**
237 * qrtr_tx_wait() - flow control for outgoing packets
238 * @node: qrtr_node that the packet is to be send to
239 * @dest_node: node id of the destination
240 * @dest_port: port number of the destination
241 * @type: type of message
242 *
243 * The flow control scheme is based around the low and high "watermarks". When
244 * the low watermark is passed the confirm_rx flag is set on the outgoing
245 * message, which will trigger the remote to send a control message of the type
246 * QRTR_TYPE_RESUME_TX to reset the counter. If the high watermark is hit
247 * further transmision should be paused.
248 *
249 * Return: 1 if confirm_rx should be set, 0 otherwise or errno failure
250 */
251static int qrtr_tx_wait(struct qrtr_node *node, int dest_node, int dest_port,
252 int type)
253{
254 unsigned long key = (u64)dest_node << 32 | dest_port;
255 struct qrtr_tx_flow *flow;
256 int confirm_rx = 0;
257 int ret;
258
259 /* Never set confirm_rx on non-data packets */
260 if (type != QRTR_TYPE_DATA)
261 return 0;
262
263 mutex_lock(&node->qrtr_tx_lock);
264 flow = radix_tree_lookup(&node->qrtr_tx_flow, key);
265 if (!flow) {
266 flow = kzalloc(sizeof(*flow), GFP_KERNEL);
267 if (flow) {
268 init_waitqueue_head(&flow->resume_tx);
269 radix_tree_insert(&node->qrtr_tx_flow, key, flow);
270 }
271 }
272 mutex_unlock(&node->qrtr_tx_lock);
273
274 /* Set confirm_rx if we where unable to find and allocate a flow */
275 if (!flow)
276 return 1;
277
278 spin_lock_irq(&flow->resume_tx.lock);
279 ret = wait_event_interruptible_locked_irq(flow->resume_tx,
280 flow->pending < QRTR_TX_FLOW_HIGH ||
281 flow->tx_failed ||
282 !node->ep);
283 if (ret < 0) {
284 confirm_rx = ret;
285 } else if (!node->ep) {
286 confirm_rx = -EPIPE;
287 } else if (flow->tx_failed) {
288 flow->tx_failed = 0;
289 confirm_rx = 1;
290 } else {
291 flow->pending++;
292 confirm_rx = flow->pending == QRTR_TX_FLOW_LOW;
293 }
294 spin_unlock_irq(&flow->resume_tx.lock);
295
296 return confirm_rx;
297}
298
299/**
300 * qrtr_tx_flow_failed() - flag that tx of confirm_rx flagged messages failed
301 * @node: qrtr_node that the packet is to be send to
302 * @dest_node: node id of the destination
303 * @dest_port: port number of the destination
304 *
305 * Signal that the transmission of a message with confirm_rx flag failed. The
306 * flow's "pending" counter will keep incrementing towards QRTR_TX_FLOW_HIGH,
307 * at which point transmission would stall forever waiting for the resume TX
308 * message associated with the dropped confirm_rx message.
309 * Work around this by marking the flow as having a failed transmission and
310 * cause the next transmission attempt to be sent with the confirm_rx.
311 */
312static void qrtr_tx_flow_failed(struct qrtr_node *node, int dest_node,
313 int dest_port)
314{
315 unsigned long key = (u64)dest_node << 32 | dest_port;
316 struct qrtr_tx_flow *flow;
317
318 rcu_read_lock();
319 flow = radix_tree_lookup(&node->qrtr_tx_flow, key);
320 rcu_read_unlock();
321 if (flow) {
322 spin_lock_irq(&flow->resume_tx.lock);
323 flow->tx_failed = 1;
324 spin_unlock_irq(&flow->resume_tx.lock);
325 }
326}
327
Courtney Cavinbdabad32016-05-06 07:09:08 -0700328/* Pass an outgoing packet socket buffer to the endpoint driver. */
Bjorn Anderssone7044482017-10-10 23:45:20 -0700329static int qrtr_node_enqueue(struct qrtr_node *node, struct sk_buff *skb,
330 int type, struct sockaddr_qrtr *from,
331 struct sockaddr_qrtr *to)
Courtney Cavinbdabad32016-05-06 07:09:08 -0700332{
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700333 struct qrtr_hdr_v1 *hdr;
Bjorn Anderssone7044482017-10-10 23:45:20 -0700334 size_t len = skb->len;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700335 int rc = -ENODEV;
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800336 int confirm_rx;
337
338 confirm_rx = qrtr_tx_wait(node, to->sq_node, to->sq_port, type);
339 if (confirm_rx < 0) {
340 kfree_skb(skb);
341 return confirm_rx;
342 }
Courtney Cavinbdabad32016-05-06 07:09:08 -0700343
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700344 hdr = skb_push(skb, sizeof(*hdr));
345 hdr->version = cpu_to_le32(QRTR_PROTO_VER_1);
Bjorn Anderssone7044482017-10-10 23:45:20 -0700346 hdr->type = cpu_to_le32(type);
347 hdr->src_node_id = cpu_to_le32(from->sq_node);
348 hdr->src_port_id = cpu_to_le32(from->sq_port);
Arun Kumar Neelakantamd27e77a2018-07-04 19:49:33 +0530349 if (to->sq_port == QRTR_PORT_CTRL) {
350 hdr->dst_node_id = cpu_to_le32(node->nid);
351 hdr->dst_port_id = cpu_to_le32(QRTR_NODE_BCAST);
352 } else {
353 hdr->dst_node_id = cpu_to_le32(to->sq_node);
354 hdr->dst_port_id = cpu_to_le32(to->sq_port);
355 }
Bjorn Anderssone7044482017-10-10 23:45:20 -0700356
357 hdr->size = cpu_to_le32(len);
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800358 hdr->confirm_rx = !!confirm_rx;
Bjorn Anderssone7044482017-10-10 23:45:20 -0700359
Carl Huangce577852020-01-03 12:50:16 +0800360 skb_put_padto(skb, ALIGN(len, 4) + sizeof(*hdr));
Bjorn Anderssone7044482017-10-10 23:45:20 -0700361
Courtney Cavinbdabad32016-05-06 07:09:08 -0700362 mutex_lock(&node->ep_lock);
363 if (node->ep)
364 rc = node->ep->xmit(node->ep, skb);
365 else
366 kfree_skb(skb);
367 mutex_unlock(&node->ep_lock);
368
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800369 /* Need to ensure that a subsequent message carries the otherwise lost
370 * confirm_rx flag if we dropped this one */
371 if (rc && confirm_rx)
372 qrtr_tx_flow_failed(node, to->sq_node, to->sq_port);
373
Courtney Cavinbdabad32016-05-06 07:09:08 -0700374 return rc;
375}
376
377/* Lookup node by id.
378 *
379 * callers must release with qrtr_node_release()
380 */
381static struct qrtr_node *qrtr_node_lookup(unsigned int nid)
382{
383 struct qrtr_node *node;
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800384 unsigned long flags;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700385
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800386 spin_lock_irqsave(&qrtr_nodes_lock, flags);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700387 node = radix_tree_lookup(&qrtr_nodes, nid);
388 node = qrtr_node_acquire(node);
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800389 spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700390
391 return node;
392}
393
394/* Assign node id to node.
395 *
396 * This is mostly useful for automatic node id assignment, based on
397 * the source id in the incoming packet.
398 */
399static void qrtr_node_assign(struct qrtr_node *node, unsigned int nid)
400{
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800401 unsigned long flags;
402
Courtney Cavinbdabad32016-05-06 07:09:08 -0700403 if (node->nid != QRTR_EP_NID_AUTO || nid == QRTR_EP_NID_AUTO)
404 return;
405
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800406 spin_lock_irqsave(&qrtr_nodes_lock, flags);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700407 radix_tree_insert(&qrtr_nodes, nid, node);
408 node->nid = nid;
Bjorn Andersson0a7e0d02020-01-13 23:57:01 -0800409 spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700410}
411
412/**
413 * qrtr_endpoint_post() - post incoming data
414 * @ep: endpoint handle
415 * @data: data pointer
416 * @len: size of data in bytes
417 *
418 * Return: 0 on success; negative error code on failure
419 */
420int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
421{
422 struct qrtr_node *node = ep->node;
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700423 const struct qrtr_hdr_v1 *v1;
424 const struct qrtr_hdr_v2 *v2;
Bjorn Anderssone04df982020-01-13 23:57:03 -0800425 struct qrtr_sock *ipc;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700426 struct sk_buff *skb;
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -0700427 struct qrtr_cb *cb;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700428 unsigned int size;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700429 unsigned int ver;
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700430 size_t hdrlen;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700431
Dan Carpenter8ff41cc2020-06-30 14:46:15 +0300432 if (len == 0 || len & 3)
Courtney Cavinbdabad32016-05-06 07:09:08 -0700433 return -EINVAL;
434
435 skb = netdev_alloc_skb(NULL, len);
436 if (!skb)
437 return -ENOMEM;
438
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -0700439 cb = (struct qrtr_cb *)skb->cb;
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -0700440
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700441 /* Version field in v1 is little endian, so this works for both cases */
442 ver = *(u8*)data;
443
444 switch (ver) {
445 case QRTR_PROTO_VER_1:
Dan Carpenter8ff41cc2020-06-30 14:46:15 +0300446 if (len < sizeof(*v1))
447 goto err;
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700448 v1 = data;
449 hdrlen = sizeof(*v1);
450
451 cb->type = le32_to_cpu(v1->type);
452 cb->src_node = le32_to_cpu(v1->src_node_id);
453 cb->src_port = le32_to_cpu(v1->src_port_id);
454 cb->confirm_rx = !!v1->confirm_rx;
455 cb->dst_node = le32_to_cpu(v1->dst_node_id);
456 cb->dst_port = le32_to_cpu(v1->dst_port_id);
457
458 size = le32_to_cpu(v1->size);
459 break;
460 case QRTR_PROTO_VER_2:
Dan Carpenter8ff41cc2020-06-30 14:46:15 +0300461 if (len < sizeof(*v2))
462 goto err;
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700463 v2 = data;
464 hdrlen = sizeof(*v2) + v2->optlen;
465
466 cb->type = v2->type;
467 cb->confirm_rx = !!(v2->flags & QRTR_FLAGS_CONFIRM_RX);
468 cb->src_node = le16_to_cpu(v2->src_node_id);
469 cb->src_port = le16_to_cpu(v2->src_port_id);
470 cb->dst_node = le16_to_cpu(v2->dst_node_id);
471 cb->dst_port = le16_to_cpu(v2->dst_port_id);
472
473 if (cb->src_port == (u16)QRTR_PORT_CTRL)
474 cb->src_port = QRTR_PORT_CTRL;
475 if (cb->dst_port == (u16)QRTR_PORT_CTRL)
476 cb->dst_port = QRTR_PORT_CTRL;
477
478 size = le32_to_cpu(v2->size);
479 break;
480 default:
481 pr_err("qrtr: Invalid version %d\n", ver);
482 goto err;
483 }
484
485 if (len != ALIGN(size, 4) + hdrlen)
486 goto err;
487
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800488 if (cb->dst_port != QRTR_PORT_CTRL && cb->type != QRTR_TYPE_DATA &&
489 cb->type != QRTR_TYPE_RESUME_TX)
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700490 goto err;
491
492 skb_put_data(skb, data + hdrlen, size);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700493
Bjorn Anderssone04df982020-01-13 23:57:03 -0800494 qrtr_node_assign(node, cb->src_node);
495
496 if (cb->type == QRTR_TYPE_RESUME_TX) {
497 qrtr_tx_resume(node, skb);
498 } else {
499 ipc = qrtr_port_lookup(cb->dst_port);
500 if (!ipc)
501 goto err;
502
503 if (sock_queue_rcv_skb(&ipc->sk, skb))
504 goto err;
505
506 qrtr_port_put(ipc);
507 }
Courtney Cavinbdabad32016-05-06 07:09:08 -0700508
509 return 0;
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700510
511err:
512 kfree_skb(skb);
513 return -EINVAL;
514
Courtney Cavinbdabad32016-05-06 07:09:08 -0700515}
516EXPORT_SYMBOL_GPL(qrtr_endpoint_post);
517
Bjorn Andersson1a7959c2017-10-10 23:45:21 -0700518/**
519 * qrtr_alloc_ctrl_packet() - allocate control packet skb
520 * @pkt: reference to qrtr_ctrl_pkt pointer
521 *
522 * Returns newly allocated sk_buff, or NULL on failure
523 *
524 * This function allocates a sk_buff large enough to carry a qrtr_ctrl_pkt and
525 * on success returns a reference to the control packet in @pkt.
526 */
527static struct sk_buff *qrtr_alloc_ctrl_packet(struct qrtr_ctrl_pkt **pkt)
Courtney Cavinbdabad32016-05-06 07:09:08 -0700528{
Bjorn Andersson1a7959c2017-10-10 23:45:21 -0700529 const int pkt_len = sizeof(struct qrtr_ctrl_pkt);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700530 struct sk_buff *skb;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700531
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700532 skb = alloc_skb(QRTR_HDR_MAX_SIZE + pkt_len, GFP_KERNEL);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700533 if (!skb)
534 return NULL;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700535
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700536 skb_reserve(skb, QRTR_HDR_MAX_SIZE);
Bjorn Andersson1a7959c2017-10-10 23:45:21 -0700537 *pkt = skb_put_zero(skb, pkt_len);
Bjorn Andersson17844732017-06-07 14:07:38 -0700538
539 return skb;
540}
541
Courtney Cavinbdabad32016-05-06 07:09:08 -0700542/**
543 * qrtr_endpoint_register() - register a new endpoint
544 * @ep: endpoint to register
545 * @nid: desired node id; may be QRTR_EP_NID_AUTO for auto-assignment
546 * Return: 0 on success; negative error code on failure
547 *
548 * The specified endpoint must have the xmit function pointer set on call.
549 */
550int qrtr_endpoint_register(struct qrtr_endpoint *ep, unsigned int nid)
551{
552 struct qrtr_node *node;
553
554 if (!ep || !ep->xmit)
555 return -EINVAL;
556
557 node = kzalloc(sizeof(*node), GFP_KERNEL);
558 if (!node)
559 return -ENOMEM;
560
Courtney Cavinbdabad32016-05-06 07:09:08 -0700561 kref_init(&node->ref);
562 mutex_init(&node->ep_lock);
563 skb_queue_head_init(&node->rx_queue);
564 node->nid = QRTR_EP_NID_AUTO;
565 node->ep = ep;
566
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800567 INIT_RADIX_TREE(&node->qrtr_tx_flow, GFP_KERNEL);
568 mutex_init(&node->qrtr_tx_lock);
569
Courtney Cavinbdabad32016-05-06 07:09:08 -0700570 qrtr_node_assign(node, nid);
571
572 mutex_lock(&qrtr_node_lock);
573 list_add(&node->item, &qrtr_all_nodes);
574 mutex_unlock(&qrtr_node_lock);
575 ep->node = node;
576
577 return 0;
578}
579EXPORT_SYMBOL_GPL(qrtr_endpoint_register);
580
581/**
582 * qrtr_endpoint_unregister - unregister endpoint
583 * @ep: endpoint to unregister
584 */
585void qrtr_endpoint_unregister(struct qrtr_endpoint *ep)
586{
587 struct qrtr_node *node = ep->node;
Bjorn Anderssone7044482017-10-10 23:45:20 -0700588 struct sockaddr_qrtr src = {AF_QIPCRTR, node->nid, QRTR_PORT_CTRL};
589 struct sockaddr_qrtr dst = {AF_QIPCRTR, qrtr_local_nid, QRTR_PORT_CTRL};
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800590 struct radix_tree_iter iter;
Bjorn Andersson1a7959c2017-10-10 23:45:21 -0700591 struct qrtr_ctrl_pkt *pkt;
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800592 struct qrtr_tx_flow *flow;
Bjorn Andersson8acc8ee2017-06-07 14:07:37 -0700593 struct sk_buff *skb;
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800594 void __rcu **slot;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700595
596 mutex_lock(&node->ep_lock);
597 node->ep = NULL;
598 mutex_unlock(&node->ep_lock);
599
Bjorn Andersson8acc8ee2017-06-07 14:07:37 -0700600 /* Notify the local controller about the event */
Bjorn Andersson1a7959c2017-10-10 23:45:21 -0700601 skb = qrtr_alloc_ctrl_packet(&pkt);
602 if (skb) {
603 pkt->cmd = cpu_to_le32(QRTR_TYPE_BYE);
Bjorn Anderssone7044482017-10-10 23:45:20 -0700604 qrtr_local_enqueue(NULL, skb, QRTR_TYPE_BYE, &src, &dst);
Bjorn Andersson1a7959c2017-10-10 23:45:21 -0700605 }
Bjorn Andersson8acc8ee2017-06-07 14:07:37 -0700606
Bjorn Andersson5fdeb0d2020-01-13 23:57:00 -0800607 /* Wake up any transmitters waiting for resume-tx from the node */
608 mutex_lock(&node->qrtr_tx_lock);
609 radix_tree_for_each_slot(slot, &node->qrtr_tx_flow, &iter, 0) {
610 flow = *slot;
611 wake_up_interruptible_all(&flow->resume_tx);
612 }
613 mutex_unlock(&node->qrtr_tx_lock);
614
Courtney Cavinbdabad32016-05-06 07:09:08 -0700615 qrtr_node_release(node);
616 ep->node = NULL;
617}
618EXPORT_SYMBOL_GPL(qrtr_endpoint_unregister);
619
620/* Lookup socket by port.
621 *
622 * Callers must release with qrtr_port_put()
623 */
624static struct qrtr_sock *qrtr_port_lookup(int port)
625{
626 struct qrtr_sock *ipc;
627
628 if (port == QRTR_PORT_CTRL)
629 port = 0;
630
Bjorn Anderssonf16a4b262020-01-13 23:57:02 -0800631 rcu_read_lock();
Courtney Cavinbdabad32016-05-06 07:09:08 -0700632 ipc = idr_find(&qrtr_ports, port);
633 if (ipc)
634 sock_hold(&ipc->sk);
Bjorn Anderssonf16a4b262020-01-13 23:57:02 -0800635 rcu_read_unlock();
Courtney Cavinbdabad32016-05-06 07:09:08 -0700636
637 return ipc;
638}
639
640/* Release acquired socket. */
641static void qrtr_port_put(struct qrtr_sock *ipc)
642{
643 sock_put(&ipc->sk);
644}
645
646/* Remove port assignment. */
647static void qrtr_port_remove(struct qrtr_sock *ipc)
648{
Bjorn Andersson1a7959c2017-10-10 23:45:21 -0700649 struct qrtr_ctrl_pkt *pkt;
Bjorn Andersson17844732017-06-07 14:07:38 -0700650 struct sk_buff *skb;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700651 int port = ipc->us.sq_port;
Bjorn Anderssone7044482017-10-10 23:45:20 -0700652 struct sockaddr_qrtr to;
653
654 to.sq_family = AF_QIPCRTR;
655 to.sq_node = QRTR_NODE_BCAST;
656 to.sq_port = QRTR_PORT_CTRL;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700657
Bjorn Andersson1a7959c2017-10-10 23:45:21 -0700658 skb = qrtr_alloc_ctrl_packet(&pkt);
Bjorn Andersson17844732017-06-07 14:07:38 -0700659 if (skb) {
Bjorn Andersson1a7959c2017-10-10 23:45:21 -0700660 pkt->cmd = cpu_to_le32(QRTR_TYPE_DEL_CLIENT);
661 pkt->client.node = cpu_to_le32(ipc->us.sq_node);
662 pkt->client.port = cpu_to_le32(ipc->us.sq_port);
663
Bjorn Andersson17844732017-06-07 14:07:38 -0700664 skb_set_owner_w(skb, &ipc->sk);
Bjorn Anderssone7044482017-10-10 23:45:20 -0700665 qrtr_bcast_enqueue(NULL, skb, QRTR_TYPE_DEL_CLIENT, &ipc->us,
666 &to);
Bjorn Andersson17844732017-06-07 14:07:38 -0700667 }
668
Courtney Cavinbdabad32016-05-06 07:09:08 -0700669 if (port == QRTR_PORT_CTRL)
670 port = 0;
671
672 __sock_put(&ipc->sk);
673
674 mutex_lock(&qrtr_port_lock);
675 idr_remove(&qrtr_ports, port);
676 mutex_unlock(&qrtr_port_lock);
Bjorn Anderssonf16a4b262020-01-13 23:57:02 -0800677
678 /* Ensure that if qrtr_port_lookup() did enter the RCU read section we
679 * wait for it to up increment the refcount */
680 synchronize_rcu();
Courtney Cavinbdabad32016-05-06 07:09:08 -0700681}
682
683/* Assign port number to socket.
684 *
685 * Specify port in the integer pointed to by port, and it will be adjusted
686 * on return as necesssary.
687 *
688 * Port may be:
689 * 0: Assign ephemeral port in [QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET]
690 * <QRTR_MIN_EPH_SOCKET: Specified; requires CAP_NET_ADMIN
691 * >QRTR_MIN_EPH_SOCKET: Specified; available to all
692 */
693static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
694{
695 int rc;
696
697 mutex_lock(&qrtr_port_lock);
698 if (!*port) {
699 rc = idr_alloc(&qrtr_ports, ipc,
700 QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET + 1,
701 GFP_ATOMIC);
702 if (rc >= 0)
703 *port = rc;
704 } else if (*port < QRTR_MIN_EPH_SOCKET && !capable(CAP_NET_ADMIN)) {
705 rc = -EACCES;
706 } else if (*port == QRTR_PORT_CTRL) {
707 rc = idr_alloc(&qrtr_ports, ipc, 0, 1, GFP_ATOMIC);
708 } else {
709 rc = idr_alloc(&qrtr_ports, ipc, *port, *port + 1, GFP_ATOMIC);
710 if (rc >= 0)
711 *port = rc;
712 }
713 mutex_unlock(&qrtr_port_lock);
714
715 if (rc == -ENOSPC)
716 return -EADDRINUSE;
717 else if (rc < 0)
718 return rc;
719
720 sock_hold(&ipc->sk);
721
722 return 0;
723}
724
Bjorn Anderssonb24844b2017-06-07 14:07:39 -0700725/* Reset all non-control ports */
726static void qrtr_reset_ports(void)
727{
728 struct qrtr_sock *ipc;
729 int id;
730
731 mutex_lock(&qrtr_port_lock);
732 idr_for_each_entry(&qrtr_ports, ipc, id) {
733 /* Don't reset control port */
734 if (id == 0)
735 continue;
736
737 sock_hold(&ipc->sk);
738 ipc->sk.sk_err = ENETRESET;
Bjorn Anderssonae85bfa2017-10-10 23:45:17 -0700739 ipc->sk.sk_error_report(&ipc->sk);
Bjorn Anderssonb24844b2017-06-07 14:07:39 -0700740 sock_put(&ipc->sk);
741 }
742 mutex_unlock(&qrtr_port_lock);
743}
744
Courtney Cavinbdabad32016-05-06 07:09:08 -0700745/* Bind socket to address.
746 *
747 * Socket should be locked upon call.
748 */
749static int __qrtr_bind(struct socket *sock,
750 const struct sockaddr_qrtr *addr, int zapped)
751{
752 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
753 struct sock *sk = sock->sk;
754 int port;
755 int rc;
756
757 /* rebinding ok */
758 if (!zapped && addr->sq_port == ipc->us.sq_port)
759 return 0;
760
761 port = addr->sq_port;
762 rc = qrtr_port_assign(ipc, &port);
763 if (rc)
764 return rc;
765
766 /* unbind previous, if any */
767 if (!zapped)
768 qrtr_port_remove(ipc);
769 ipc->us.sq_port = port;
770
771 sock_reset_flag(sk, SOCK_ZAPPED);
772
Bjorn Anderssonb24844b2017-06-07 14:07:39 -0700773 /* Notify all open ports about the new controller */
774 if (port == QRTR_PORT_CTRL)
775 qrtr_reset_ports();
776
Courtney Cavinbdabad32016-05-06 07:09:08 -0700777 return 0;
778}
779
780/* Auto bind to an ephemeral port. */
781static int qrtr_autobind(struct socket *sock)
782{
783 struct sock *sk = sock->sk;
784 struct sockaddr_qrtr addr;
785
786 if (!sock_flag(sk, SOCK_ZAPPED))
787 return 0;
788
789 addr.sq_family = AF_QIPCRTR;
790 addr.sq_node = qrtr_local_nid;
791 addr.sq_port = 0;
792
793 return __qrtr_bind(sock, &addr, 1);
794}
795
796/* Bind socket to specified sockaddr. */
797static int qrtr_bind(struct socket *sock, struct sockaddr *saddr, int len)
798{
799 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, saddr);
800 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
801 struct sock *sk = sock->sk;
802 int rc;
803
804 if (len < sizeof(*addr) || addr->sq_family != AF_QIPCRTR)
805 return -EINVAL;
806
807 if (addr->sq_node != ipc->us.sq_node)
808 return -EINVAL;
809
810 lock_sock(sk);
811 rc = __qrtr_bind(sock, addr, sock_flag(sk, SOCK_ZAPPED));
812 release_sock(sk);
813
814 return rc;
815}
816
817/* Queue packet to local peer socket. */
Bjorn Anderssone7044482017-10-10 23:45:20 -0700818static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,
819 int type, struct sockaddr_qrtr *from,
820 struct sockaddr_qrtr *to)
Courtney Cavinbdabad32016-05-06 07:09:08 -0700821{
Courtney Cavinbdabad32016-05-06 07:09:08 -0700822 struct qrtr_sock *ipc;
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -0700823 struct qrtr_cb *cb;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700824
Bjorn Anderssone7044482017-10-10 23:45:20 -0700825 ipc = qrtr_port_lookup(to->sq_port);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700826 if (!ipc || &ipc->sk == skb->sk) { /* do not send to self */
827 kfree_skb(skb);
828 return -ENODEV;
829 }
830
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -0700831 cb = (struct qrtr_cb *)skb->cb;
832 cb->src_node = from->sq_node;
833 cb->src_port = from->sq_port;
Bjorn Anderssone7044482017-10-10 23:45:20 -0700834
Courtney Cavinbdabad32016-05-06 07:09:08 -0700835 if (sock_queue_rcv_skb(&ipc->sk, skb)) {
836 qrtr_port_put(ipc);
837 kfree_skb(skb);
838 return -ENOSPC;
839 }
840
841 qrtr_port_put(ipc);
842
843 return 0;
844}
845
846/* Queue packet for broadcast. */
Bjorn Anderssone7044482017-10-10 23:45:20 -0700847static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,
848 int type, struct sockaddr_qrtr *from,
849 struct sockaddr_qrtr *to)
Courtney Cavinbdabad32016-05-06 07:09:08 -0700850{
851 struct sk_buff *skbn;
852
853 mutex_lock(&qrtr_node_lock);
854 list_for_each_entry(node, &qrtr_all_nodes, item) {
855 skbn = skb_clone(skb, GFP_KERNEL);
856 if (!skbn)
857 break;
858 skb_set_owner_w(skbn, skb->sk);
Bjorn Anderssone7044482017-10-10 23:45:20 -0700859 qrtr_node_enqueue(node, skbn, type, from, to);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700860 }
861 mutex_unlock(&qrtr_node_lock);
862
Manivannan Sadhasivamd28ea1f2020-05-19 23:44:16 +0530863 qrtr_local_enqueue(NULL, skb, type, from, to);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700864
865 return 0;
866}
867
868static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
869{
870 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg->msg_name);
Bjorn Anderssone7044482017-10-10 23:45:20 -0700871 int (*enqueue_fn)(struct qrtr_node *, struct sk_buff *, int,
872 struct sockaddr_qrtr *, struct sockaddr_qrtr *);
Nicholas Mc Guire8f5e2452019-05-11 02:56:33 +0200873 __le32 qrtr_type = cpu_to_le32(QRTR_TYPE_DATA);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700874 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
875 struct sock *sk = sock->sk;
876 struct qrtr_node *node;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700877 struct sk_buff *skb;
878 size_t plen;
Bjorn Andersson7036e622019-05-20 16:51:56 -0700879 u32 type;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700880 int rc;
881
882 if (msg->msg_flags & ~(MSG_DONTWAIT))
883 return -EINVAL;
884
885 if (len > 65535)
886 return -EMSGSIZE;
887
888 lock_sock(sk);
889
890 if (addr) {
891 if (msg->msg_namelen < sizeof(*addr)) {
892 release_sock(sk);
893 return -EINVAL;
894 }
895
896 if (addr->sq_family != AF_QIPCRTR) {
897 release_sock(sk);
898 return -EINVAL;
899 }
900
901 rc = qrtr_autobind(sock);
902 if (rc) {
903 release_sock(sk);
904 return rc;
905 }
906 } else if (sk->sk_state == TCP_ESTABLISHED) {
907 addr = &ipc->peer;
908 } else {
909 release_sock(sk);
910 return -ENOTCONN;
911 }
912
913 node = NULL;
914 if (addr->sq_node == QRTR_NODE_BCAST) {
Wang Wenhu6dbf02a2020-04-08 19:53:53 -0700915 if (addr->sq_port != QRTR_PORT_CTRL &&
916 qrtr_local_nid != QRTR_NODE_BCAST) {
Arun Kumar Neelakantamfdf5fd32018-07-04 19:49:32 +0530917 release_sock(sk);
918 return -ENOTCONN;
919 }
Wang Wenhu6dbf02a2020-04-08 19:53:53 -0700920 enqueue_fn = qrtr_bcast_enqueue;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700921 } else if (addr->sq_node == ipc->us.sq_node) {
922 enqueue_fn = qrtr_local_enqueue;
923 } else {
Courtney Cavinbdabad32016-05-06 07:09:08 -0700924 node = qrtr_node_lookup(addr->sq_node);
925 if (!node) {
926 release_sock(sk);
927 return -ECONNRESET;
928 }
Wang Wenhu6dbf02a2020-04-08 19:53:53 -0700929 enqueue_fn = qrtr_node_enqueue;
Courtney Cavinbdabad32016-05-06 07:09:08 -0700930 }
931
932 plen = (len + 3) & ~3;
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700933 skb = sock_alloc_send_skb(sk, plen + QRTR_HDR_MAX_SIZE,
Courtney Cavinbdabad32016-05-06 07:09:08 -0700934 msg->msg_flags & MSG_DONTWAIT, &rc);
935 if (!skb)
936 goto out_node;
937
Bjorn Andersson194ccc82017-10-10 23:45:23 -0700938 skb_reserve(skb, QRTR_HDR_MAX_SIZE);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700939
Bjorn Anderssone7044482017-10-10 23:45:20 -0700940 rc = memcpy_from_msg(skb_put(skb, len), msg, len);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700941 if (rc) {
942 kfree_skb(skb);
943 goto out_node;
944 }
945
Courtney Cavinbdabad32016-05-06 07:09:08 -0700946 if (ipc->us.sq_port == QRTR_PORT_CTRL) {
947 if (len < 4) {
948 rc = -EINVAL;
949 kfree_skb(skb);
950 goto out_node;
951 }
952
953 /* control messages already require the type as 'command' */
Nicholas Mc Guire8f5e2452019-05-11 02:56:33 +0200954 skb_copy_bits(skb, 0, &qrtr_type, 4);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700955 }
956
Bjorn Andersson7036e622019-05-20 16:51:56 -0700957 type = le32_to_cpu(qrtr_type);
Bjorn Anderssone7044482017-10-10 23:45:20 -0700958 rc = enqueue_fn(node, skb, type, &ipc->us, addr);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700959 if (rc >= 0)
960 rc = len;
961
962out_node:
963 qrtr_node_release(node);
964 release_sock(sk);
965
966 return rc;
967}
968
Bjorn Anderssoncb6530b2020-01-13 23:56:59 -0800969static int qrtr_send_resume_tx(struct qrtr_cb *cb)
970{
971 struct sockaddr_qrtr remote = { AF_QIPCRTR, cb->src_node, cb->src_port };
972 struct sockaddr_qrtr local = { AF_QIPCRTR, cb->dst_node, cb->dst_port };
973 struct qrtr_ctrl_pkt *pkt;
974 struct qrtr_node *node;
975 struct sk_buff *skb;
976 int ret;
977
978 node = qrtr_node_lookup(remote.sq_node);
979 if (!node)
980 return -EINVAL;
981
982 skb = qrtr_alloc_ctrl_packet(&pkt);
983 if (!skb)
984 return -ENOMEM;
985
986 pkt->cmd = cpu_to_le32(QRTR_TYPE_RESUME_TX);
987 pkt->client.node = cpu_to_le32(cb->dst_node);
988 pkt->client.port = cpu_to_le32(cb->dst_port);
989
990 ret = qrtr_node_enqueue(node, skb, QRTR_TYPE_RESUME_TX, &local, &remote);
991
992 qrtr_node_release(node);
993
994 return ret;
995}
996
Courtney Cavinbdabad32016-05-06 07:09:08 -0700997static int qrtr_recvmsg(struct socket *sock, struct msghdr *msg,
998 size_t size, int flags)
999{
1000 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg->msg_name);
Courtney Cavinbdabad32016-05-06 07:09:08 -07001001 struct sock *sk = sock->sk;
1002 struct sk_buff *skb;
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -07001003 struct qrtr_cb *cb;
Courtney Cavinbdabad32016-05-06 07:09:08 -07001004 int copied, rc;
1005
1006 lock_sock(sk);
1007
1008 if (sock_flag(sk, SOCK_ZAPPED)) {
1009 release_sock(sk);
1010 return -EADDRNOTAVAIL;
1011 }
1012
1013 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1014 flags & MSG_DONTWAIT, &rc);
1015 if (!skb) {
1016 release_sock(sk);
1017 return rc;
1018 }
Bjorn Anderssoncb6530b2020-01-13 23:56:59 -08001019 cb = (struct qrtr_cb *)skb->cb;
Courtney Cavinbdabad32016-05-06 07:09:08 -07001020
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -07001021 copied = skb->len;
Courtney Cavinbdabad32016-05-06 07:09:08 -07001022 if (copied > size) {
1023 copied = size;
1024 msg->msg_flags |= MSG_TRUNC;
1025 }
1026
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -07001027 rc = skb_copy_datagram_msg(skb, 0, msg, copied);
Courtney Cavinbdabad32016-05-06 07:09:08 -07001028 if (rc < 0)
1029 goto out;
1030 rc = copied;
1031
1032 if (addr) {
1033 addr->sq_family = AF_QIPCRTR;
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -07001034 addr->sq_node = cb->src_node;
1035 addr->sq_port = cb->src_port;
Courtney Cavinbdabad32016-05-06 07:09:08 -07001036 msg->msg_namelen = sizeof(*addr);
1037 }
1038
1039out:
Bjorn Anderssoncb6530b2020-01-13 23:56:59 -08001040 if (cb->confirm_rx)
1041 qrtr_send_resume_tx(cb);
1042
Courtney Cavinbdabad32016-05-06 07:09:08 -07001043 skb_free_datagram(sk, skb);
1044 release_sock(sk);
1045
1046 return rc;
1047}
1048
1049static int qrtr_connect(struct socket *sock, struct sockaddr *saddr,
1050 int len, int flags)
1051{
1052 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, saddr);
1053 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
1054 struct sock *sk = sock->sk;
1055 int rc;
1056
1057 if (len < sizeof(*addr) || addr->sq_family != AF_QIPCRTR)
1058 return -EINVAL;
1059
1060 lock_sock(sk);
1061
1062 sk->sk_state = TCP_CLOSE;
1063 sock->state = SS_UNCONNECTED;
1064
1065 rc = qrtr_autobind(sock);
1066 if (rc) {
1067 release_sock(sk);
1068 return rc;
1069 }
1070
1071 ipc->peer = *addr;
1072 sock->state = SS_CONNECTED;
1073 sk->sk_state = TCP_ESTABLISHED;
1074
1075 release_sock(sk);
1076
1077 return 0;
1078}
1079
1080static int qrtr_getname(struct socket *sock, struct sockaddr *saddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001081 int peer)
Courtney Cavinbdabad32016-05-06 07:09:08 -07001082{
1083 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
1084 struct sockaddr_qrtr qaddr;
1085 struct sock *sk = sock->sk;
1086
1087 lock_sock(sk);
1088 if (peer) {
1089 if (sk->sk_state != TCP_ESTABLISHED) {
1090 release_sock(sk);
1091 return -ENOTCONN;
1092 }
1093
1094 qaddr = ipc->peer;
1095 } else {
1096 qaddr = ipc->us;
1097 }
1098 release_sock(sk);
1099
Courtney Cavinbdabad32016-05-06 07:09:08 -07001100 qaddr.sq_family = AF_QIPCRTR;
1101
1102 memcpy(saddr, &qaddr, sizeof(qaddr));
1103
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001104 return sizeof(qaddr);
Courtney Cavinbdabad32016-05-06 07:09:08 -07001105}
1106
1107static int qrtr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1108{
1109 void __user *argp = (void __user *)arg;
1110 struct qrtr_sock *ipc = qrtr_sk(sock->sk);
1111 struct sock *sk = sock->sk;
1112 struct sockaddr_qrtr *sq;
1113 struct sk_buff *skb;
1114 struct ifreq ifr;
1115 long len = 0;
1116 int rc = 0;
1117
1118 lock_sock(sk);
1119
1120 switch (cmd) {
1121 case TIOCOUTQ:
1122 len = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1123 if (len < 0)
1124 len = 0;
1125 rc = put_user(len, (int __user *)argp);
1126 break;
1127 case TIOCINQ:
1128 skb = skb_peek(&sk->sk_receive_queue);
1129 if (skb)
Bjorn Anderssonf507a9b62017-10-10 23:45:22 -07001130 len = skb->len;
Courtney Cavinbdabad32016-05-06 07:09:08 -07001131 rc = put_user(len, (int __user *)argp);
1132 break;
1133 case SIOCGIFADDR:
1134 if (copy_from_user(&ifr, argp, sizeof(ifr))) {
1135 rc = -EFAULT;
1136 break;
1137 }
1138
1139 sq = (struct sockaddr_qrtr *)&ifr.ifr_addr;
1140 *sq = ipc->us;
1141 if (copy_to_user(argp, &ifr, sizeof(ifr))) {
1142 rc = -EFAULT;
1143 break;
1144 }
1145 break;
Courtney Cavinbdabad32016-05-06 07:09:08 -07001146 case SIOCADDRT:
1147 case SIOCDELRT:
1148 case SIOCSIFADDR:
1149 case SIOCGIFDSTADDR:
1150 case SIOCSIFDSTADDR:
1151 case SIOCGIFBRDADDR:
1152 case SIOCSIFBRDADDR:
1153 case SIOCGIFNETMASK:
1154 case SIOCSIFNETMASK:
1155 rc = -EINVAL;
1156 break;
1157 default:
1158 rc = -ENOIOCTLCMD;
1159 break;
1160 }
1161
1162 release_sock(sk);
1163
1164 return rc;
1165}
1166
1167static int qrtr_release(struct socket *sock)
1168{
1169 struct sock *sk = sock->sk;
1170 struct qrtr_sock *ipc;
1171
1172 if (!sk)
1173 return 0;
1174
1175 lock_sock(sk);
1176
1177 ipc = qrtr_sk(sk);
1178 sk->sk_shutdown = SHUTDOWN_MASK;
1179 if (!sock_flag(sk, SOCK_DEAD))
1180 sk->sk_state_change(sk);
1181
1182 sock_set_flag(sk, SOCK_DEAD);
Cong Wangaf9f6912020-07-24 09:45:51 -07001183 sock_orphan(sk);
Courtney Cavinbdabad32016-05-06 07:09:08 -07001184 sock->sk = NULL;
1185
1186 if (!sock_flag(sk, SOCK_ZAPPED))
1187 qrtr_port_remove(ipc);
1188
1189 skb_queue_purge(&sk->sk_receive_queue);
1190
1191 release_sock(sk);
1192 sock_put(sk);
1193
1194 return 0;
1195}
1196
1197static const struct proto_ops qrtr_proto_ops = {
1198 .owner = THIS_MODULE,
1199 .family = AF_QIPCRTR,
1200 .bind = qrtr_bind,
1201 .connect = qrtr_connect,
1202 .socketpair = sock_no_socketpair,
1203 .accept = sock_no_accept,
1204 .listen = sock_no_listen,
1205 .sendmsg = qrtr_sendmsg,
1206 .recvmsg = qrtr_recvmsg,
1207 .getname = qrtr_getname,
1208 .ioctl = qrtr_ioctl,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02001209 .gettstamp = sock_gettstamp,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001210 .poll = datagram_poll,
Courtney Cavinbdabad32016-05-06 07:09:08 -07001211 .shutdown = sock_no_shutdown,
1212 .setsockopt = sock_no_setsockopt,
1213 .getsockopt = sock_no_getsockopt,
1214 .release = qrtr_release,
1215 .mmap = sock_no_mmap,
1216 .sendpage = sock_no_sendpage,
1217};
1218
1219static struct proto qrtr_proto = {
1220 .name = "QIPCRTR",
1221 .owner = THIS_MODULE,
1222 .obj_size = sizeof(struct qrtr_sock),
1223};
1224
1225static int qrtr_create(struct net *net, struct socket *sock,
1226 int protocol, int kern)
1227{
1228 struct qrtr_sock *ipc;
1229 struct sock *sk;
1230
1231 if (sock->type != SOCK_DGRAM)
1232 return -EPROTOTYPE;
1233
1234 sk = sk_alloc(net, AF_QIPCRTR, GFP_KERNEL, &qrtr_proto, kern);
1235 if (!sk)
1236 return -ENOMEM;
1237
1238 sock_set_flag(sk, SOCK_ZAPPED);
1239
1240 sock_init_data(sock, sk);
1241 sock->ops = &qrtr_proto_ops;
1242
1243 ipc = qrtr_sk(sk);
1244 ipc->us.sq_family = AF_QIPCRTR;
1245 ipc->us.sq_node = qrtr_local_nid;
1246 ipc->us.sq_port = 0;
1247
1248 return 0;
1249}
1250
Courtney Cavinbdabad32016-05-06 07:09:08 -07001251static const struct net_proto_family qrtr_family = {
1252 .owner = THIS_MODULE,
1253 .family = AF_QIPCRTR,
1254 .create = qrtr_create,
1255};
1256
1257static int __init qrtr_proto_init(void)
1258{
1259 int rc;
1260
1261 rc = proto_register(&qrtr_proto, 1);
1262 if (rc)
1263 return rc;
1264
1265 rc = sock_register(&qrtr_family);
1266 if (rc) {
1267 proto_unregister(&qrtr_proto);
1268 return rc;
1269 }
1270
Bjorn Andersson71046ab2020-03-01 23:03:05 -08001271 qrtr_ns_init();
Courtney Cavinbdabad32016-05-06 07:09:08 -07001272
Florian Westphalc1c502b2017-12-02 21:44:07 +01001273 return rc;
Courtney Cavinbdabad32016-05-06 07:09:08 -07001274}
Bjorn Anderssonb7e732f2017-11-06 20:50:35 -08001275postcore_initcall(qrtr_proto_init);
Courtney Cavinbdabad32016-05-06 07:09:08 -07001276
1277static void __exit qrtr_proto_fini(void)
1278{
Manivannan Sadhasivam0c2204a2020-02-20 20:43:26 +05301279 qrtr_ns_remove();
Courtney Cavinbdabad32016-05-06 07:09:08 -07001280 sock_unregister(qrtr_family.family);
1281 proto_unregister(&qrtr_proto);
1282}
1283module_exit(qrtr_proto_fini);
1284
1285MODULE_DESCRIPTION("Qualcomm IPC-router driver");
1286MODULE_LICENSE("GPL v2");
Nicolas Dechesne77ac7252018-04-17 14:03:26 +02001287MODULE_ALIAS_NETPROTO(PF_QIPCRTR);