Thomas Gleixner | 97fb5e8 | 2019-05-29 07:17:58 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015, Sony Mobile Communications Inc. |
| 4 | * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 5 | */ |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/netlink.h> |
| 8 | #include <linux/qrtr.h> |
| 9 | #include <linux/termios.h> /* For TIOCINQ/OUTQ */ |
Bjorn Andersson | 0a7e0d0 | 2020-01-13 23:57:01 -0800 | [diff] [blame] | 10 | #include <linux/spinlock.h> |
Bjorn Andersson | 5fdeb0d | 2020-01-13 23:57:00 -0800 | [diff] [blame] | 11 | #include <linux/wait.h> |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 12 | |
| 13 | #include <net/sock.h> |
| 14 | |
| 15 | #include "qrtr.h" |
| 16 | |
Bjorn Andersson | 194ccc8 | 2017-10-10 23:45:23 -0700 | [diff] [blame] | 17 | #define QRTR_PROTO_VER_1 1 |
| 18 | #define QRTR_PROTO_VER_2 3 |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 19 | |
| 20 | /* auto-bind range */ |
| 21 | #define QRTR_MIN_EPH_SOCKET 0x4000 |
| 22 | #define QRTR_MAX_EPH_SOCKET 0x7fff |
| 23 | |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 24 | /** |
Bjorn Andersson | 194ccc8 | 2017-10-10 23:45:23 -0700 | [diff] [blame] | 25 | * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1 |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 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 |
| 34 | */ |
Bjorn Andersson | 194ccc8 | 2017-10-10 23:45:23 -0700 | [diff] [blame] | 35 | struct qrtr_hdr_v1 { |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 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; |
| 45 | |
Bjorn Andersson | 194ccc8 | 2017-10-10 23:45:23 -0700 | [diff] [blame] | 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 |
| 57 | */ |
| 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; |
| 68 | }; |
| 69 | |
| 70 | #define QRTR_FLAGS_CONFIRM_RX BIT(0) |
| 71 | |
Bjorn Andersson | f507a9b6 | 2017-10-10 23:45:22 -0700 | [diff] [blame] | 72 | struct 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 Andersson | 194ccc8 | 2017-10-10 23:45:23 -0700 | [diff] [blame] | 82 | #define QRTR_HDR_MAX_SIZE max_t(size_t, sizeof(struct qrtr_hdr_v1), \ |
| 83 | sizeof(struct qrtr_hdr_v2)) |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 84 | |
| 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; |
| 90 | }; |
| 91 | |
| 92 | static 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 Sadhasivam | 31d6cbe | 2020-02-20 20:43:27 +0530 | [diff] [blame] | 98 | static unsigned int qrtr_local_nid = 1; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 99 | |
| 100 | /* for node ids */ |
Bjorn Andersson | 0a7e0d0 | 2020-01-13 23:57:01 -0800 | [diff] [blame] | 101 | static RADIX_TREE(qrtr_nodes, GFP_ATOMIC); |
| 102 | static DEFINE_SPINLOCK(qrtr_nodes_lock); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 103 | /* broadcast list */ |
| 104 | static LIST_HEAD(qrtr_all_nodes); |
Bjorn Andersson | 0a7e0d0 | 2020-01-13 23:57:01 -0800 | [diff] [blame] | 105 | /* lock for qrtr_all_nodes and node reference */ |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 106 | static DEFINE_MUTEX(qrtr_node_lock); |
| 107 | |
| 108 | /* local port allocation management */ |
| 109 | static DEFINE_IDR(qrtr_ports); |
| 110 | static 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 Andersson | 5fdeb0d | 2020-01-13 23:57:00 -0800 | [diff] [blame] | 118 | * @qrtr_tx_flow: tree of qrtr_tx_flow, keyed by node << 32 | port |
| 119 | * @qrtr_tx_lock: lock for qrtr_tx_flow inserts |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 120 | * @rx_queue: receive queue |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 121 | * @item: list item for broadcast list |
| 122 | */ |
| 123 | struct qrtr_node { |
| 124 | struct mutex ep_lock; |
| 125 | struct qrtr_endpoint *ep; |
| 126 | struct kref ref; |
| 127 | unsigned int nid; |
| 128 | |
Bjorn Andersson | 5fdeb0d | 2020-01-13 23:57:00 -0800 | [diff] [blame] | 129 | struct radix_tree_root qrtr_tx_flow; |
| 130 | struct mutex qrtr_tx_lock; /* for qrtr_tx_flow */ |
| 131 | |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 132 | struct sk_buff_head rx_queue; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 133 | struct list_head item; |
| 134 | }; |
| 135 | |
Bjorn Andersson | 5fdeb0d | 2020-01-13 23:57:00 -0800 | [diff] [blame] | 136 | /** |
| 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 | */ |
| 142 | struct 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 Andersson | e704448 | 2017-10-10 23:45:20 -0700 | [diff] [blame] | 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); |
Bjorn Andersson | e04df98 | 2020-01-13 23:57:03 -0800 | [diff] [blame] | 157 | static struct qrtr_sock *qrtr_port_lookup(int port); |
| 158 | static void qrtr_port_put(struct qrtr_sock *ipc); |
Bjorn Andersson | 8acc8ee | 2017-06-07 14:07:37 -0700 | [diff] [blame] | 159 | |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 160 | /* 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 | */ |
| 165 | static void __qrtr_node_release(struct kref *kref) |
| 166 | { |
| 167 | struct qrtr_node *node = container_of(kref, struct qrtr_node, ref); |
Bjorn Andersson | 5fdeb0d | 2020-01-13 23:57:00 -0800 | [diff] [blame] | 168 | struct radix_tree_iter iter; |
Bjorn Andersson | 0a7e0d0 | 2020-01-13 23:57:01 -0800 | [diff] [blame] | 169 | unsigned long flags; |
Bjorn Andersson | 5fdeb0d | 2020-01-13 23:57:00 -0800 | [diff] [blame] | 170 | void __rcu **slot; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 171 | |
Bjorn Andersson | 0a7e0d0 | 2020-01-13 23:57:01 -0800 | [diff] [blame] | 172 | spin_lock_irqsave(&qrtr_nodes_lock, flags); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 173 | if (node->nid != QRTR_EP_NID_AUTO) |
| 174 | radix_tree_delete(&qrtr_nodes, node->nid); |
Bjorn Andersson | 0a7e0d0 | 2020-01-13 23:57:01 -0800 | [diff] [blame] | 175 | spin_unlock_irqrestore(&qrtr_nodes_lock, flags); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 176 | |
| 177 | list_del(&node->item); |
| 178 | mutex_unlock(&qrtr_node_lock); |
| 179 | |
| 180 | skb_queue_purge(&node->rx_queue); |
Bjorn Andersson | 5fdeb0d | 2020-01-13 23:57:00 -0800 | [diff] [blame] | 181 | |
| 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 Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 187 | kfree(node); |
| 188 | } |
| 189 | |
| 190 | /* Increment reference to node. */ |
| 191 | static 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. */ |
| 199 | static 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 Andersson | 5fdeb0d | 2020-01-13 23:57:00 -0800 | [diff] [blame] | 206 | /** |
| 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 | */ |
| 211 | static 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 | */ |
| 249 | static 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 | */ |
| 310 | static 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 Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 326 | /* Pass an outgoing packet socket buffer to the endpoint driver. */ |
Bjorn Andersson | e704448 | 2017-10-10 23:45:20 -0700 | [diff] [blame] | 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) |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 330 | { |
Bjorn Andersson | 194ccc8 | 2017-10-10 23:45:23 -0700 | [diff] [blame] | 331 | struct qrtr_hdr_v1 *hdr; |
Bjorn Andersson | e704448 | 2017-10-10 23:45:20 -0700 | [diff] [blame] | 332 | size_t len = skb->len; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 333 | int rc = -ENODEV; |
Bjorn Andersson | 5fdeb0d | 2020-01-13 23:57:00 -0800 | [diff] [blame] | 334 | 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 Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 341 | |
Bjorn Andersson | 194ccc8 | 2017-10-10 23:45:23 -0700 | [diff] [blame] | 342 | hdr = skb_push(skb, sizeof(*hdr)); |
| 343 | hdr->version = cpu_to_le32(QRTR_PROTO_VER_1); |
Bjorn Andersson | e704448 | 2017-10-10 23:45:20 -0700 | [diff] [blame] | 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); |
Arun Kumar Neelakantam | d27e77a | 2018-07-04 19:49:33 +0530 | [diff] [blame] | 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); |
| 353 | } |
Bjorn Andersson | e704448 | 2017-10-10 23:45:20 -0700 | [diff] [blame] | 354 | |
| 355 | hdr->size = cpu_to_le32(len); |
Bjorn Andersson | 5fdeb0d | 2020-01-13 23:57:00 -0800 | [diff] [blame] | 356 | hdr->confirm_rx = !!confirm_rx; |
Bjorn Andersson | e704448 | 2017-10-10 23:45:20 -0700 | [diff] [blame] | 357 | |
Carl Huang | ce57785 | 2020-01-03 12:50:16 +0800 | [diff] [blame] | 358 | skb_put_padto(skb, ALIGN(len, 4) + sizeof(*hdr)); |
Bjorn Andersson | e704448 | 2017-10-10 23:45:20 -0700 | [diff] [blame] | 359 | |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 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); |
| 366 | |
Bjorn Andersson | 5fdeb0d | 2020-01-13 23:57:00 -0800 | [diff] [blame] | 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); |
| 371 | |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 372 | return rc; |
| 373 | } |
| 374 | |
| 375 | /* Lookup node by id. |
| 376 | * |
| 377 | * callers must release with qrtr_node_release() |
| 378 | */ |
| 379 | static struct qrtr_node *qrtr_node_lookup(unsigned int nid) |
| 380 | { |
| 381 | struct qrtr_node *node; |
Bjorn Andersson | 0a7e0d0 | 2020-01-13 23:57:01 -0800 | [diff] [blame] | 382 | unsigned long flags; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 383 | |
Bjorn Andersson | 0a7e0d0 | 2020-01-13 23:57:01 -0800 | [diff] [blame] | 384 | spin_lock_irqsave(&qrtr_nodes_lock, flags); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 385 | node = radix_tree_lookup(&qrtr_nodes, nid); |
| 386 | node = qrtr_node_acquire(node); |
Bjorn Andersson | 0a7e0d0 | 2020-01-13 23:57:01 -0800 | [diff] [blame] | 387 | spin_unlock_irqrestore(&qrtr_nodes_lock, flags); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 388 | |
| 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 | */ |
| 397 | static void qrtr_node_assign(struct qrtr_node *node, unsigned int nid) |
| 398 | { |
Bjorn Andersson | 0a7e0d0 | 2020-01-13 23:57:01 -0800 | [diff] [blame] | 399 | unsigned long flags; |
| 400 | |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 401 | if (node->nid != QRTR_EP_NID_AUTO || nid == QRTR_EP_NID_AUTO) |
| 402 | return; |
| 403 | |
Bjorn Andersson | 0a7e0d0 | 2020-01-13 23:57:01 -0800 | [diff] [blame] | 404 | spin_lock_irqsave(&qrtr_nodes_lock, flags); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 405 | radix_tree_insert(&qrtr_nodes, nid, node); |
| 406 | node->nid = nid; |
Bjorn Andersson | 0a7e0d0 | 2020-01-13 23:57:01 -0800 | [diff] [blame] | 407 | spin_unlock_irqrestore(&qrtr_nodes_lock, flags); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 408 | } |
| 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 | */ |
| 418 | int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len) |
| 419 | { |
| 420 | struct qrtr_node *node = ep->node; |
Bjorn Andersson | 194ccc8 | 2017-10-10 23:45:23 -0700 | [diff] [blame] | 421 | const struct qrtr_hdr_v1 *v1; |
| 422 | const struct qrtr_hdr_v2 *v2; |
Bjorn Andersson | e04df98 | 2020-01-13 23:57:03 -0800 | [diff] [blame] | 423 | struct qrtr_sock *ipc; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 424 | struct sk_buff *skb; |
Bjorn Andersson | f507a9b6 | 2017-10-10 23:45:22 -0700 | [diff] [blame] | 425 | struct qrtr_cb *cb; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 426 | unsigned int size; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 427 | unsigned int ver; |
Bjorn Andersson | 194ccc8 | 2017-10-10 23:45:23 -0700 | [diff] [blame] | 428 | size_t hdrlen; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 429 | |
Bjorn Andersson | 194ccc8 | 2017-10-10 23:45:23 -0700 | [diff] [blame] | 430 | if (len & 3) |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 431 | return -EINVAL; |
| 432 | |
| 433 | skb = netdev_alloc_skb(NULL, len); |
| 434 | if (!skb) |
| 435 | return -ENOMEM; |
| 436 | |
Bjorn Andersson | f507a9b6 | 2017-10-10 23:45:22 -0700 | [diff] [blame] | 437 | cb = (struct qrtr_cb *)skb->cb; |
Bjorn Andersson | f507a9b6 | 2017-10-10 23:45:22 -0700 | [diff] [blame] | 438 | |
Bjorn Andersson | 194ccc8 | 2017-10-10 23:45:23 -0700 | [diff] [blame] | 439 | /* 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 Andersson | 5fdeb0d | 2020-01-13 23:57:00 -0800 | [diff] [blame] | 482 | if (cb->dst_port != QRTR_PORT_CTRL && cb->type != QRTR_TYPE_DATA && |
| 483 | cb->type != QRTR_TYPE_RESUME_TX) |
Bjorn Andersson | 194ccc8 | 2017-10-10 23:45:23 -0700 | [diff] [blame] | 484 | goto err; |
| 485 | |
| 486 | skb_put_data(skb, data + hdrlen, size); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 487 | |
Bjorn Andersson | e04df98 | 2020-01-13 23:57:03 -0800 | [diff] [blame] | 488 | 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 Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 502 | |
| 503 | return 0; |
Bjorn Andersson | 194ccc8 | 2017-10-10 23:45:23 -0700 | [diff] [blame] | 504 | |
| 505 | err: |
| 506 | kfree_skb(skb); |
| 507 | return -EINVAL; |
| 508 | |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 509 | } |
| 510 | EXPORT_SYMBOL_GPL(qrtr_endpoint_post); |
| 511 | |
Bjorn Andersson | 1a7959c | 2017-10-10 23:45:21 -0700 | [diff] [blame] | 512 | /** |
| 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 | */ |
| 521 | static struct sk_buff *qrtr_alloc_ctrl_packet(struct qrtr_ctrl_pkt **pkt) |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 522 | { |
Bjorn Andersson | 1a7959c | 2017-10-10 23:45:21 -0700 | [diff] [blame] | 523 | const int pkt_len = sizeof(struct qrtr_ctrl_pkt); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 524 | struct sk_buff *skb; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 525 | |
Bjorn Andersson | 194ccc8 | 2017-10-10 23:45:23 -0700 | [diff] [blame] | 526 | skb = alloc_skb(QRTR_HDR_MAX_SIZE + pkt_len, GFP_KERNEL); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 527 | if (!skb) |
| 528 | return NULL; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 529 | |
Bjorn Andersson | 194ccc8 | 2017-10-10 23:45:23 -0700 | [diff] [blame] | 530 | skb_reserve(skb, QRTR_HDR_MAX_SIZE); |
Bjorn Andersson | 1a7959c | 2017-10-10 23:45:21 -0700 | [diff] [blame] | 531 | *pkt = skb_put_zero(skb, pkt_len); |
Bjorn Andersson | 1784473 | 2017-06-07 14:07:38 -0700 | [diff] [blame] | 532 | |
| 533 | return skb; |
| 534 | } |
| 535 | |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 536 | /** |
| 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 | */ |
| 544 | int 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 Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 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; |
| 560 | |
Bjorn Andersson | 5fdeb0d | 2020-01-13 23:57:00 -0800 | [diff] [blame] | 561 | INIT_RADIX_TREE(&node->qrtr_tx_flow, GFP_KERNEL); |
| 562 | mutex_init(&node->qrtr_tx_lock); |
| 563 | |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 564 | 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 | } |
| 573 | EXPORT_SYMBOL_GPL(qrtr_endpoint_register); |
| 574 | |
| 575 | /** |
| 576 | * qrtr_endpoint_unregister - unregister endpoint |
| 577 | * @ep: endpoint to unregister |
| 578 | */ |
| 579 | void qrtr_endpoint_unregister(struct qrtr_endpoint *ep) |
| 580 | { |
| 581 | struct qrtr_node *node = ep->node; |
Bjorn Andersson | e704448 | 2017-10-10 23:45:20 -0700 | [diff] [blame] | 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}; |
Bjorn Andersson | 5fdeb0d | 2020-01-13 23:57:00 -0800 | [diff] [blame] | 584 | struct radix_tree_iter iter; |
Bjorn Andersson | 1a7959c | 2017-10-10 23:45:21 -0700 | [diff] [blame] | 585 | struct qrtr_ctrl_pkt *pkt; |
Bjorn Andersson | 5fdeb0d | 2020-01-13 23:57:00 -0800 | [diff] [blame] | 586 | struct qrtr_tx_flow *flow; |
Bjorn Andersson | 8acc8ee | 2017-06-07 14:07:37 -0700 | [diff] [blame] | 587 | struct sk_buff *skb; |
Bjorn Andersson | 5fdeb0d | 2020-01-13 23:57:00 -0800 | [diff] [blame] | 588 | void __rcu **slot; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 589 | |
| 590 | mutex_lock(&node->ep_lock); |
| 591 | node->ep = NULL; |
| 592 | mutex_unlock(&node->ep_lock); |
| 593 | |
Bjorn Andersson | 8acc8ee | 2017-06-07 14:07:37 -0700 | [diff] [blame] | 594 | /* Notify the local controller about the event */ |
Bjorn Andersson | 1a7959c | 2017-10-10 23:45:21 -0700 | [diff] [blame] | 595 | skb = qrtr_alloc_ctrl_packet(&pkt); |
| 596 | if (skb) { |
| 597 | pkt->cmd = cpu_to_le32(QRTR_TYPE_BYE); |
Bjorn Andersson | e704448 | 2017-10-10 23:45:20 -0700 | [diff] [blame] | 598 | qrtr_local_enqueue(NULL, skb, QRTR_TYPE_BYE, &src, &dst); |
Bjorn Andersson | 1a7959c | 2017-10-10 23:45:21 -0700 | [diff] [blame] | 599 | } |
Bjorn Andersson | 8acc8ee | 2017-06-07 14:07:37 -0700 | [diff] [blame] | 600 | |
Bjorn Andersson | 5fdeb0d | 2020-01-13 23:57:00 -0800 | [diff] [blame] | 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); |
| 606 | } |
| 607 | mutex_unlock(&node->qrtr_tx_lock); |
| 608 | |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 609 | qrtr_node_release(node); |
| 610 | ep->node = NULL; |
| 611 | } |
| 612 | EXPORT_SYMBOL_GPL(qrtr_endpoint_unregister); |
| 613 | |
| 614 | /* Lookup socket by port. |
| 615 | * |
| 616 | * Callers must release with qrtr_port_put() |
| 617 | */ |
| 618 | static 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 Andersson | f16a4b26 | 2020-01-13 23:57:02 -0800 | [diff] [blame] | 625 | rcu_read_lock(); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 626 | ipc = idr_find(&qrtr_ports, port); |
| 627 | if (ipc) |
| 628 | sock_hold(&ipc->sk); |
Bjorn Andersson | f16a4b26 | 2020-01-13 23:57:02 -0800 | [diff] [blame] | 629 | rcu_read_unlock(); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 630 | |
| 631 | return ipc; |
| 632 | } |
| 633 | |
| 634 | /* Release acquired socket. */ |
| 635 | static void qrtr_port_put(struct qrtr_sock *ipc) |
| 636 | { |
| 637 | sock_put(&ipc->sk); |
| 638 | } |
| 639 | |
| 640 | /* Remove port assignment. */ |
| 641 | static void qrtr_port_remove(struct qrtr_sock *ipc) |
| 642 | { |
Bjorn Andersson | 1a7959c | 2017-10-10 23:45:21 -0700 | [diff] [blame] | 643 | struct qrtr_ctrl_pkt *pkt; |
Bjorn Andersson | 1784473 | 2017-06-07 14:07:38 -0700 | [diff] [blame] | 644 | struct sk_buff *skb; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 645 | int port = ipc->us.sq_port; |
Bjorn Andersson | e704448 | 2017-10-10 23:45:20 -0700 | [diff] [blame] | 646 | 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 Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 651 | |
Bjorn Andersson | 1a7959c | 2017-10-10 23:45:21 -0700 | [diff] [blame] | 652 | skb = qrtr_alloc_ctrl_packet(&pkt); |
Bjorn Andersson | 1784473 | 2017-06-07 14:07:38 -0700 | [diff] [blame] | 653 | if (skb) { |
Bjorn Andersson | 1a7959c | 2017-10-10 23:45:21 -0700 | [diff] [blame] | 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); |
| 657 | |
Bjorn Andersson | 1784473 | 2017-06-07 14:07:38 -0700 | [diff] [blame] | 658 | skb_set_owner_w(skb, &ipc->sk); |
Bjorn Andersson | e704448 | 2017-10-10 23:45:20 -0700 | [diff] [blame] | 659 | qrtr_bcast_enqueue(NULL, skb, QRTR_TYPE_DEL_CLIENT, &ipc->us, |
| 660 | &to); |
Bjorn Andersson | 1784473 | 2017-06-07 14:07:38 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 663 | 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 Andersson | f16a4b26 | 2020-01-13 23:57:02 -0800 | [diff] [blame] | 671 | |
| 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 Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 675 | } |
| 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 | */ |
| 687 | static 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 Andersson | b24844b | 2017-06-07 14:07:39 -0700 | [diff] [blame] | 719 | /* Reset all non-control ports */ |
| 720 | static 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 Andersson | ae85bfa | 2017-10-10 23:45:17 -0700 | [diff] [blame] | 733 | ipc->sk.sk_error_report(&ipc->sk); |
Bjorn Andersson | b24844b | 2017-06-07 14:07:39 -0700 | [diff] [blame] | 734 | sock_put(&ipc->sk); |
| 735 | } |
| 736 | mutex_unlock(&qrtr_port_lock); |
| 737 | } |
| 738 | |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 739 | /* Bind socket to address. |
| 740 | * |
| 741 | * Socket should be locked upon call. |
| 742 | */ |
| 743 | static 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 Andersson | b24844b | 2017-06-07 14:07:39 -0700 | [diff] [blame] | 767 | /* Notify all open ports about the new controller */ |
| 768 | if (port == QRTR_PORT_CTRL) |
| 769 | qrtr_reset_ports(); |
| 770 | |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | /* Auto bind to an ephemeral port. */ |
| 775 | static 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. */ |
| 791 | static 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 Andersson | e704448 | 2017-10-10 23:45:20 -0700 | [diff] [blame] | 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) |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 815 | { |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 816 | struct qrtr_sock *ipc; |
Bjorn Andersson | f507a9b6 | 2017-10-10 23:45:22 -0700 | [diff] [blame] | 817 | struct qrtr_cb *cb; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 818 | |
Bjorn Andersson | e704448 | 2017-10-10 23:45:20 -0700 | [diff] [blame] | 819 | ipc = qrtr_port_lookup(to->sq_port); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 820 | if (!ipc || &ipc->sk == skb->sk) { /* do not send to self */ |
| 821 | kfree_skb(skb); |
| 822 | return -ENODEV; |
| 823 | } |
| 824 | |
Bjorn Andersson | f507a9b6 | 2017-10-10 23:45:22 -0700 | [diff] [blame] | 825 | cb = (struct qrtr_cb *)skb->cb; |
| 826 | cb->src_node = from->sq_node; |
| 827 | cb->src_port = from->sq_port; |
Bjorn Andersson | e704448 | 2017-10-10 23:45:20 -0700 | [diff] [blame] | 828 | |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 829 | 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 Andersson | e704448 | 2017-10-10 23:45:20 -0700 | [diff] [blame] | 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) |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 844 | { |
| 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 Andersson | e704448 | 2017-10-10 23:45:20 -0700 | [diff] [blame] | 853 | qrtr_node_enqueue(node, skbn, type, from, to); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 854 | } |
| 855 | mutex_unlock(&qrtr_node_lock); |
| 856 | |
Manivannan Sadhasivam | d28ea1f | 2020-05-19 23:44:16 +0530 | [diff] [blame^] | 857 | qrtr_local_enqueue(NULL, skb, type, from, to); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) |
| 863 | { |
| 864 | DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg->msg_name); |
Bjorn Andersson | e704448 | 2017-10-10 23:45:20 -0700 | [diff] [blame] | 865 | int (*enqueue_fn)(struct qrtr_node *, struct sk_buff *, int, |
| 866 | struct sockaddr_qrtr *, struct sockaddr_qrtr *); |
Nicholas Mc Guire | 8f5e245 | 2019-05-11 02:56:33 +0200 | [diff] [blame] | 867 | __le32 qrtr_type = cpu_to_le32(QRTR_TYPE_DATA); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 868 | struct qrtr_sock *ipc = qrtr_sk(sock->sk); |
| 869 | struct sock *sk = sock->sk; |
| 870 | struct qrtr_node *node; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 871 | struct sk_buff *skb; |
| 872 | size_t plen; |
Bjorn Andersson | 7036e62 | 2019-05-20 16:51:56 -0700 | [diff] [blame] | 873 | u32 type; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 874 | 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 Wenhu | 6dbf02a | 2020-04-08 19:53:53 -0700 | [diff] [blame] | 909 | if (addr->sq_port != QRTR_PORT_CTRL && |
| 910 | qrtr_local_nid != QRTR_NODE_BCAST) { |
Arun Kumar Neelakantam | fdf5fd3 | 2018-07-04 19:49:32 +0530 | [diff] [blame] | 911 | release_sock(sk); |
| 912 | return -ENOTCONN; |
| 913 | } |
Wang Wenhu | 6dbf02a | 2020-04-08 19:53:53 -0700 | [diff] [blame] | 914 | enqueue_fn = qrtr_bcast_enqueue; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 915 | } else if (addr->sq_node == ipc->us.sq_node) { |
| 916 | enqueue_fn = qrtr_local_enqueue; |
| 917 | } else { |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 918 | node = qrtr_node_lookup(addr->sq_node); |
| 919 | if (!node) { |
| 920 | release_sock(sk); |
| 921 | return -ECONNRESET; |
| 922 | } |
Wang Wenhu | 6dbf02a | 2020-04-08 19:53:53 -0700 | [diff] [blame] | 923 | enqueue_fn = qrtr_node_enqueue; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | plen = (len + 3) & ~3; |
Bjorn Andersson | 194ccc8 | 2017-10-10 23:45:23 -0700 | [diff] [blame] | 927 | skb = sock_alloc_send_skb(sk, plen + QRTR_HDR_MAX_SIZE, |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 928 | msg->msg_flags & MSG_DONTWAIT, &rc); |
| 929 | if (!skb) |
| 930 | goto out_node; |
| 931 | |
Bjorn Andersson | 194ccc8 | 2017-10-10 23:45:23 -0700 | [diff] [blame] | 932 | skb_reserve(skb, QRTR_HDR_MAX_SIZE); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 933 | |
Bjorn Andersson | e704448 | 2017-10-10 23:45:20 -0700 | [diff] [blame] | 934 | rc = memcpy_from_msg(skb_put(skb, len), msg, len); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 935 | if (rc) { |
| 936 | kfree_skb(skb); |
| 937 | goto out_node; |
| 938 | } |
| 939 | |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 940 | 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 Guire | 8f5e245 | 2019-05-11 02:56:33 +0200 | [diff] [blame] | 948 | skb_copy_bits(skb, 0, &qrtr_type, 4); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 949 | } |
| 950 | |
Bjorn Andersson | 7036e62 | 2019-05-20 16:51:56 -0700 | [diff] [blame] | 951 | type = le32_to_cpu(qrtr_type); |
Bjorn Andersson | e704448 | 2017-10-10 23:45:20 -0700 | [diff] [blame] | 952 | rc = enqueue_fn(node, skb, type, &ipc->us, addr); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 953 | if (rc >= 0) |
| 954 | rc = len; |
| 955 | |
| 956 | out_node: |
| 957 | qrtr_node_release(node); |
| 958 | release_sock(sk); |
| 959 | |
| 960 | return rc; |
| 961 | } |
| 962 | |
Bjorn Andersson | cb6530b | 2020-01-13 23:56:59 -0800 | [diff] [blame] | 963 | static 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 Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 991 | static 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 Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 995 | struct sock *sk = sock->sk; |
| 996 | struct sk_buff *skb; |
Bjorn Andersson | f507a9b6 | 2017-10-10 23:45:22 -0700 | [diff] [blame] | 997 | struct qrtr_cb *cb; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 998 | 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 Andersson | cb6530b | 2020-01-13 23:56:59 -0800 | [diff] [blame] | 1013 | cb = (struct qrtr_cb *)skb->cb; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 1014 | |
Bjorn Andersson | f507a9b6 | 2017-10-10 23:45:22 -0700 | [diff] [blame] | 1015 | copied = skb->len; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 1016 | if (copied > size) { |
| 1017 | copied = size; |
| 1018 | msg->msg_flags |= MSG_TRUNC; |
| 1019 | } |
| 1020 | |
Bjorn Andersson | f507a9b6 | 2017-10-10 23:45:22 -0700 | [diff] [blame] | 1021 | rc = skb_copy_datagram_msg(skb, 0, msg, copied); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 1022 | if (rc < 0) |
| 1023 | goto out; |
| 1024 | rc = copied; |
| 1025 | |
| 1026 | if (addr) { |
| 1027 | addr->sq_family = AF_QIPCRTR; |
Bjorn Andersson | f507a9b6 | 2017-10-10 23:45:22 -0700 | [diff] [blame] | 1028 | addr->sq_node = cb->src_node; |
| 1029 | addr->sq_port = cb->src_port; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 1030 | msg->msg_namelen = sizeof(*addr); |
| 1031 | } |
| 1032 | |
| 1033 | out: |
Bjorn Andersson | cb6530b | 2020-01-13 23:56:59 -0800 | [diff] [blame] | 1034 | if (cb->confirm_rx) |
| 1035 | qrtr_send_resume_tx(cb); |
| 1036 | |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 1037 | skb_free_datagram(sk, skb); |
| 1038 | release_sock(sk); |
| 1039 | |
| 1040 | return rc; |
| 1041 | } |
| 1042 | |
| 1043 | static 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 | |
| 1074 | static int qrtr_getname(struct socket *sock, struct sockaddr *saddr, |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 1075 | int peer) |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 1076 | { |
| 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 Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 1094 | qaddr.sq_family = AF_QIPCRTR; |
| 1095 | |
| 1096 | memcpy(saddr, &qaddr, sizeof(qaddr)); |
| 1097 | |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 1098 | return sizeof(qaddr); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | static 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 Andersson | f507a9b6 | 2017-10-10 23:45:22 -0700 | [diff] [blame] | 1124 | len = skb->len; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 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; |
| 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 Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 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; |
| 1154 | } |
| 1155 | |
| 1156 | release_sock(sk); |
| 1157 | |
| 1158 | return rc; |
| 1159 | } |
| 1160 | |
| 1161 | static 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 | |
| 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, |
Arnd Bergmann | c7cbdbf | 2019-04-17 22:51:48 +0200 | [diff] [blame] | 1202 | .gettstamp = sock_gettstamp, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1203 | .poll = datagram_poll, |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 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, |
| 1210 | }; |
| 1211 | |
| 1212 | static struct proto qrtr_proto = { |
| 1213 | .name = "QIPCRTR", |
| 1214 | .owner = THIS_MODULE, |
| 1215 | .obj_size = sizeof(struct qrtr_sock), |
| 1216 | }; |
| 1217 | |
| 1218 | static 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 Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 1244 | static const struct net_proto_family qrtr_family = { |
| 1245 | .owner = THIS_MODULE, |
| 1246 | .family = AF_QIPCRTR, |
| 1247 | .create = qrtr_create, |
| 1248 | }; |
| 1249 | |
| 1250 | static 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 Andersson | 71046ab | 2020-03-01 23:03:05 -0800 | [diff] [blame] | 1264 | qrtr_ns_init(); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 1265 | |
Florian Westphal | c1c502b | 2017-12-02 21:44:07 +0100 | [diff] [blame] | 1266 | return rc; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 1267 | } |
Bjorn Andersson | b7e732f | 2017-11-06 20:50:35 -0800 | [diff] [blame] | 1268 | postcore_initcall(qrtr_proto_init); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 1269 | |
| 1270 | static void __exit qrtr_proto_fini(void) |
| 1271 | { |
Manivannan Sadhasivam | 0c2204a | 2020-02-20 20:43:26 +0530 | [diff] [blame] | 1272 | qrtr_ns_remove(); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 1273 | sock_unregister(qrtr_family.family); |
| 1274 | proto_unregister(&qrtr_proto); |
| 1275 | } |
| 1276 | module_exit(qrtr_proto_fini); |
| 1277 | |
| 1278 | MODULE_DESCRIPTION("Qualcomm IPC-router driver"); |
| 1279 | MODULE_LICENSE("GPL v2"); |
Nicolas Dechesne | 77ac725 | 2018-04-17 14:03:26 +0200 | [diff] [blame] | 1280 | MODULE_ALIAS_NETPROTO(PF_QIPCRTR); |