1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2015, Sony Mobile Communications Inc. 4 * Copyright (c) 2013, The Linux Foundation. All rights reserved. 5 */ 6 #include <linux/module.h> 7 #include <linux/netlink.h> 8 #include <linux/qrtr.h> 9 #include <linux/termios.h> /* For TIOCINQ/OUTQ */ 10 #include <linux/spinlock.h> 11 #include <linux/wait.h> 12 13 #include <net/sock.h> 14 15 #include "qrtr.h" 16 17 #define QRTR_PROTO_VER_1 1 18 #define QRTR_PROTO_VER_2 3 19 20 /* auto-bind range */ 21 #define QRTR_MIN_EPH_SOCKET 0x4000 22 #define QRTR_MAX_EPH_SOCKET 0x7fff 23 #define QRTR_EPH_PORT_RANGE \ 24 XA_LIMIT(QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET) 25 26 /** 27 * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1 28 * @version: protocol version 29 * @type: packet type; one of QRTR_TYPE_* 30 * @src_node_id: source node 31 * @src_port_id: source port 32 * @confirm_rx: boolean; whether a resume-tx packet should be send in reply 33 * @size: length of packet, excluding this header 34 * @dst_node_id: destination node 35 * @dst_port_id: destination port 36 */ 37 struct qrtr_hdr_v1 { 38 __le32 version; 39 __le32 type; 40 __le32 src_node_id; 41 __le32 src_port_id; 42 __le32 confirm_rx; 43 __le32 size; 44 __le32 dst_node_id; 45 __le32 dst_port_id; 46 } __packed; 47 48 /** 49 * struct qrtr_hdr_v2 - (I|R)PCrouter packet header later versions 50 * @version: protocol version 51 * @type: packet type; one of QRTR_TYPE_* 52 * @flags: bitmask of QRTR_FLAGS_* 53 * @optlen: length of optional header data 54 * @size: length of packet, excluding this header and optlen 55 * @src_node_id: source node 56 * @src_port_id: source port 57 * @dst_node_id: destination node 58 * @dst_port_id: destination port 59 */ 60 struct qrtr_hdr_v2 { 61 u8 version; 62 u8 type; 63 u8 flags; 64 u8 optlen; 65 __le32 size; 66 __le16 src_node_id; 67 __le16 src_port_id; 68 __le16 dst_node_id; 69 __le16 dst_port_id; 70 }; 71 72 #define QRTR_FLAGS_CONFIRM_RX BIT(0) 73 74 struct qrtr_cb { 75 u32 src_node; 76 u32 src_port; 77 u32 dst_node; 78 u32 dst_port; 79 80 u8 type; 81 u8 confirm_rx; 82 }; 83 84 #define QRTR_HDR_MAX_SIZE max_t(size_t, sizeof(struct qrtr_hdr_v1), \ 85 sizeof(struct qrtr_hdr_v2)) 86 87 struct qrtr_sock { 88 /* WARNING: sk must be the first member */ 89 struct sock sk; 90 struct sockaddr_qrtr us; 91 struct sockaddr_qrtr peer; 92 }; 93 94 static inline struct qrtr_sock *qrtr_sk(struct sock *sk) 95 { 96 BUILD_BUG_ON(offsetof(struct qrtr_sock, sk) != 0); 97 return container_of(sk, struct qrtr_sock, sk); 98 } 99 100 static unsigned int qrtr_local_nid = 1; 101 102 /* for node ids */ 103 static RADIX_TREE(qrtr_nodes, GFP_ATOMIC); 104 static DEFINE_SPINLOCK(qrtr_nodes_lock); 105 /* broadcast list */ 106 static LIST_HEAD(qrtr_all_nodes); 107 /* lock for qrtr_all_nodes and node reference */ 108 static DEFINE_MUTEX(qrtr_node_lock); 109 110 /* local port allocation management */ 111 static DEFINE_XARRAY_ALLOC(qrtr_ports); 112 113 /** 114 * struct qrtr_node - endpoint node 115 * @ep_lock: lock for endpoint management and callbacks 116 * @ep: endpoint 117 * @ref: reference count for node 118 * @nid: node id 119 * @qrtr_tx_flow: tree of qrtr_tx_flow, keyed by node << 32 | port 120 * @qrtr_tx_lock: lock for qrtr_tx_flow inserts 121 * @rx_queue: receive queue 122 * @item: list item for broadcast list 123 */ 124 struct qrtr_node { 125 struct mutex ep_lock; 126 struct qrtr_endpoint *ep; 127 struct kref ref; 128 unsigned int nid; 129 130 struct radix_tree_root qrtr_tx_flow; 131 struct mutex qrtr_tx_lock; /* for qrtr_tx_flow */ 132 133 struct sk_buff_head rx_queue; 134 struct list_head item; 135 }; 136 137 /** 138 * struct qrtr_tx_flow - tx flow control 139 * @resume_tx: waiters for a resume tx from the remote 140 * @pending: number of waiting senders 141 * @tx_failed: indicates that a message with confirm_rx flag was lost 142 */ 143 struct qrtr_tx_flow { 144 struct wait_queue_head resume_tx; 145 int pending; 146 int tx_failed; 147 }; 148 149 #define QRTR_TX_FLOW_HIGH 10 150 #define QRTR_TX_FLOW_LOW 5 151 152 static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb, 153 int type, struct sockaddr_qrtr *from, 154 struct sockaddr_qrtr *to); 155 static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb, 156 int type, struct sockaddr_qrtr *from, 157 struct sockaddr_qrtr *to); 158 static struct qrtr_sock *qrtr_port_lookup(int port); 159 static void qrtr_port_put(struct qrtr_sock *ipc); 160 161 /* Release node resources and free the node. 162 * 163 * Do not call directly, use qrtr_node_release. To be used with 164 * kref_put_mutex. As such, the node mutex is expected to be locked on call. 165 */ 166 static void __qrtr_node_release(struct kref *kref) 167 { 168 struct qrtr_node *node = container_of(kref, struct qrtr_node, ref); 169 struct radix_tree_iter iter; 170 struct qrtr_tx_flow *flow; 171 unsigned long flags; 172 void __rcu **slot; 173 174 spin_lock_irqsave(&qrtr_nodes_lock, flags); 175 /* If the node is a bridge for other nodes, there are possibly 176 * multiple entries pointing to our released node, delete them all. 177 */ 178 radix_tree_for_each_slot(slot, &qrtr_nodes, &iter, 0) { 179 if (*slot == node) 180 radix_tree_iter_delete(&qrtr_nodes, &iter, slot); 181 } 182 spin_unlock_irqrestore(&qrtr_nodes_lock, flags); 183 184 list_del(&node->item); 185 mutex_unlock(&qrtr_node_lock); 186 187 skb_queue_purge(&node->rx_queue); 188 189 /* Free tx flow counters */ 190 radix_tree_for_each_slot(slot, &node->qrtr_tx_flow, &iter, 0) { 191 flow = *slot; 192 radix_tree_iter_delete(&node->qrtr_tx_flow, &iter, slot); 193 kfree(flow); 194 } 195 kfree(node); 196 } 197 198 /* Increment reference to node. */ 199 static struct qrtr_node *qrtr_node_acquire(struct qrtr_node *node) 200 { 201 if (node) 202 kref_get(&node->ref); 203 return node; 204 } 205 206 /* Decrement reference to node and release as necessary. */ 207 static void qrtr_node_release(struct qrtr_node *node) 208 { 209 if (!node) 210 return; 211 kref_put_mutex(&node->ref, __qrtr_node_release, &qrtr_node_lock); 212 } 213 214 /** 215 * qrtr_tx_resume() - reset flow control counter 216 * @node: qrtr_node that the QRTR_TYPE_RESUME_TX packet arrived on 217 * @skb: resume_tx packet 218 */ 219 static void qrtr_tx_resume(struct qrtr_node *node, struct sk_buff *skb) 220 { 221 struct qrtr_ctrl_pkt *pkt = (struct qrtr_ctrl_pkt *)skb->data; 222 u64 remote_node = le32_to_cpu(pkt->client.node); 223 u32 remote_port = le32_to_cpu(pkt->client.port); 224 struct qrtr_tx_flow *flow; 225 unsigned long key; 226 227 key = remote_node << 32 | remote_port; 228 229 rcu_read_lock(); 230 flow = radix_tree_lookup(&node->qrtr_tx_flow, key); 231 rcu_read_unlock(); 232 if (flow) { 233 spin_lock(&flow->resume_tx.lock); 234 flow->pending = 0; 235 spin_unlock(&flow->resume_tx.lock); 236 wake_up_interruptible_all(&flow->resume_tx); 237 } 238 239 consume_skb(skb); 240 } 241 242 /** 243 * qrtr_tx_wait() - flow control for outgoing packets 244 * @node: qrtr_node that the packet is to be send to 245 * @dest_node: node id of the destination 246 * @dest_port: port number of the destination 247 * @type: type of message 248 * 249 * The flow control scheme is based around the low and high "watermarks". When 250 * the low watermark is passed the confirm_rx flag is set on the outgoing 251 * message, which will trigger the remote to send a control message of the type 252 * QRTR_TYPE_RESUME_TX to reset the counter. If the high watermark is hit 253 * further transmision should be paused. 254 * 255 * Return: 1 if confirm_rx should be set, 0 otherwise or errno failure 256 */ 257 static int qrtr_tx_wait(struct qrtr_node *node, int dest_node, int dest_port, 258 int type) 259 { 260 unsigned long key = (u64)dest_node << 32 | dest_port; 261 struct qrtr_tx_flow *flow; 262 int confirm_rx = 0; 263 int ret; 264 265 /* Never set confirm_rx on non-data packets */ 266 if (type != QRTR_TYPE_DATA) 267 return 0; 268 269 mutex_lock(&node->qrtr_tx_lock); 270 flow = radix_tree_lookup(&node->qrtr_tx_flow, key); 271 if (!flow) { 272 flow = kzalloc(sizeof(*flow), GFP_KERNEL); 273 if (flow) { 274 init_waitqueue_head(&flow->resume_tx); 275 if (radix_tree_insert(&node->qrtr_tx_flow, key, flow)) { 276 kfree(flow); 277 flow = NULL; 278 } 279 } 280 } 281 mutex_unlock(&node->qrtr_tx_lock); 282 283 /* Set confirm_rx if we where unable to find and allocate a flow */ 284 if (!flow) 285 return 1; 286 287 spin_lock_irq(&flow->resume_tx.lock); 288 ret = wait_event_interruptible_locked_irq(flow->resume_tx, 289 flow->pending < QRTR_TX_FLOW_HIGH || 290 flow->tx_failed || 291 !node->ep); 292 if (ret < 0) { 293 confirm_rx = ret; 294 } else if (!node->ep) { 295 confirm_rx = -EPIPE; 296 } else if (flow->tx_failed) { 297 flow->tx_failed = 0; 298 confirm_rx = 1; 299 } else { 300 flow->pending++; 301 confirm_rx = flow->pending == QRTR_TX_FLOW_LOW; 302 } 303 spin_unlock_irq(&flow->resume_tx.lock); 304 305 return confirm_rx; 306 } 307 308 /** 309 * qrtr_tx_flow_failed() - flag that tx of confirm_rx flagged messages failed 310 * @node: qrtr_node that the packet is to be send to 311 * @dest_node: node id of the destination 312 * @dest_port: port number of the destination 313 * 314 * Signal that the transmission of a message with confirm_rx flag failed. The 315 * flow's "pending" counter will keep incrementing towards QRTR_TX_FLOW_HIGH, 316 * at which point transmission would stall forever waiting for the resume TX 317 * message associated with the dropped confirm_rx message. 318 * Work around this by marking the flow as having a failed transmission and 319 * cause the next transmission attempt to be sent with the confirm_rx. 320 */ 321 static void qrtr_tx_flow_failed(struct qrtr_node *node, int dest_node, 322 int dest_port) 323 { 324 unsigned long key = (u64)dest_node << 32 | dest_port; 325 struct qrtr_tx_flow *flow; 326 327 rcu_read_lock(); 328 flow = radix_tree_lookup(&node->qrtr_tx_flow, key); 329 rcu_read_unlock(); 330 if (flow) { 331 spin_lock_irq(&flow->resume_tx.lock); 332 flow->tx_failed = 1; 333 spin_unlock_irq(&flow->resume_tx.lock); 334 } 335 } 336 337 /* Pass an outgoing packet socket buffer to the endpoint driver. */ 338 static int qrtr_node_enqueue(struct qrtr_node *node, struct sk_buff *skb, 339 int type, struct sockaddr_qrtr *from, 340 struct sockaddr_qrtr *to) 341 { 342 struct qrtr_hdr_v1 *hdr; 343 size_t len = skb->len; 344 int rc, confirm_rx; 345 346 confirm_rx = qrtr_tx_wait(node, to->sq_node, to->sq_port, type); 347 if (confirm_rx < 0) { 348 kfree_skb(skb); 349 return confirm_rx; 350 } 351 352 hdr = skb_push(skb, sizeof(*hdr)); 353 hdr->version = cpu_to_le32(QRTR_PROTO_VER_1); 354 hdr->type = cpu_to_le32(type); 355 hdr->src_node_id = cpu_to_le32(from->sq_node); 356 hdr->src_port_id = cpu_to_le32(from->sq_port); 357 if (to->sq_port == QRTR_PORT_CTRL) { 358 hdr->dst_node_id = cpu_to_le32(node->nid); 359 hdr->dst_port_id = cpu_to_le32(QRTR_PORT_CTRL); 360 } else { 361 hdr->dst_node_id = cpu_to_le32(to->sq_node); 362 hdr->dst_port_id = cpu_to_le32(to->sq_port); 363 } 364 365 hdr->size = cpu_to_le32(len); 366 hdr->confirm_rx = !!confirm_rx; 367 368 rc = skb_put_padto(skb, ALIGN(len, 4) + sizeof(*hdr)); 369 370 if (!rc) { 371 mutex_lock(&node->ep_lock); 372 rc = -ENODEV; 373 if (node->ep) 374 rc = node->ep->xmit(node->ep, skb); 375 else 376 kfree_skb(skb); 377 mutex_unlock(&node->ep_lock); 378 } 379 /* Need to ensure that a subsequent message carries the otherwise lost 380 * confirm_rx flag if we dropped this one */ 381 if (rc && confirm_rx) 382 qrtr_tx_flow_failed(node, to->sq_node, to->sq_port); 383 384 return rc; 385 } 386 387 /* Lookup node by id. 388 * 389 * callers must release with qrtr_node_release() 390 */ 391 static struct qrtr_node *qrtr_node_lookup(unsigned int nid) 392 { 393 struct qrtr_node *node; 394 unsigned long flags; 395 396 mutex_lock(&qrtr_node_lock); 397 spin_lock_irqsave(&qrtr_nodes_lock, flags); 398 node = radix_tree_lookup(&qrtr_nodes, nid); 399 node = qrtr_node_acquire(node); 400 spin_unlock_irqrestore(&qrtr_nodes_lock, flags); 401 mutex_unlock(&qrtr_node_lock); 402 403 return node; 404 } 405 406 /* Assign node id to node. 407 * 408 * This is mostly useful for automatic node id assignment, based on 409 * the source id in the incoming packet. 410 */ 411 static void qrtr_node_assign(struct qrtr_node *node, unsigned int nid) 412 { 413 unsigned long flags; 414 415 if (nid == QRTR_EP_NID_AUTO) 416 return; 417 418 spin_lock_irqsave(&qrtr_nodes_lock, flags); 419 radix_tree_insert(&qrtr_nodes, nid, node); 420 if (node->nid == QRTR_EP_NID_AUTO) 421 node->nid = nid; 422 spin_unlock_irqrestore(&qrtr_nodes_lock, flags); 423 } 424 425 /** 426 * qrtr_endpoint_post() - post incoming data 427 * @ep: endpoint handle 428 * @data: data pointer 429 * @len: size of data in bytes 430 * 431 * Return: 0 on success; negative error code on failure 432 */ 433 int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len) 434 { 435 struct qrtr_node *node = ep->node; 436 const struct qrtr_hdr_v1 *v1; 437 const struct qrtr_hdr_v2 *v2; 438 struct qrtr_sock *ipc; 439 struct sk_buff *skb; 440 struct qrtr_cb *cb; 441 size_t size; 442 unsigned int ver; 443 size_t hdrlen; 444 445 if (len == 0 || len & 3) 446 return -EINVAL; 447 448 skb = __netdev_alloc_skb(NULL, len, GFP_ATOMIC | __GFP_NOWARN); 449 if (!skb) 450 return -ENOMEM; 451 452 cb = (struct qrtr_cb *)skb->cb; 453 454 /* Version field in v1 is little endian, so this works for both cases */ 455 ver = *(u8*)data; 456 457 switch (ver) { 458 case QRTR_PROTO_VER_1: 459 if (len < sizeof(*v1)) 460 goto err; 461 v1 = data; 462 hdrlen = sizeof(*v1); 463 464 cb->type = le32_to_cpu(v1->type); 465 cb->src_node = le32_to_cpu(v1->src_node_id); 466 cb->src_port = le32_to_cpu(v1->src_port_id); 467 cb->confirm_rx = !!v1->confirm_rx; 468 cb->dst_node = le32_to_cpu(v1->dst_node_id); 469 cb->dst_port = le32_to_cpu(v1->dst_port_id); 470 471 size = le32_to_cpu(v1->size); 472 break; 473 case QRTR_PROTO_VER_2: 474 if (len < sizeof(*v2)) 475 goto err; 476 v2 = data; 477 hdrlen = sizeof(*v2) + v2->optlen; 478 479 cb->type = v2->type; 480 cb->confirm_rx = !!(v2->flags & QRTR_FLAGS_CONFIRM_RX); 481 cb->src_node = le16_to_cpu(v2->src_node_id); 482 cb->src_port = le16_to_cpu(v2->src_port_id); 483 cb->dst_node = le16_to_cpu(v2->dst_node_id); 484 cb->dst_port = le16_to_cpu(v2->dst_port_id); 485 486 if (cb->src_port == (u16)QRTR_PORT_CTRL) 487 cb->src_port = QRTR_PORT_CTRL; 488 if (cb->dst_port == (u16)QRTR_PORT_CTRL) 489 cb->dst_port = QRTR_PORT_CTRL; 490 491 size = le32_to_cpu(v2->size); 492 break; 493 default: 494 pr_err("qrtr: Invalid version %d\n", ver); 495 goto err; 496 } 497 498 if (!size || len != ALIGN(size, 4) + hdrlen) 499 goto err; 500 501 if ((cb->type == QRTR_TYPE_NEW_SERVER || 502 cb->type == QRTR_TYPE_RESUME_TX) && 503 size < sizeof(struct qrtr_ctrl_pkt)) 504 goto err; 505 506 if (cb->dst_port != QRTR_PORT_CTRL && cb->type != QRTR_TYPE_DATA && 507 cb->type != QRTR_TYPE_RESUME_TX) 508 goto err; 509 510 skb_put_data(skb, data + hdrlen, size); 511 512 qrtr_node_assign(node, cb->src_node); 513 514 if (cb->type == QRTR_TYPE_NEW_SERVER) { 515 /* Remote node endpoint can bridge other distant nodes */ 516 const struct qrtr_ctrl_pkt *pkt; 517 518 pkt = data + hdrlen; 519 qrtr_node_assign(node, le32_to_cpu(pkt->server.node)); 520 } 521 522 if (cb->type == QRTR_TYPE_RESUME_TX) { 523 qrtr_tx_resume(node, skb); 524 } else { 525 ipc = qrtr_port_lookup(cb->dst_port); 526 if (!ipc) 527 goto err; 528 529 if (sock_queue_rcv_skb(&ipc->sk, skb)) { 530 qrtr_port_put(ipc); 531 goto err; 532 } 533 534 qrtr_port_put(ipc); 535 } 536 537 return 0; 538 539 err: 540 kfree_skb(skb); 541 return -EINVAL; 542 543 } 544 EXPORT_SYMBOL_GPL(qrtr_endpoint_post); 545 546 /** 547 * qrtr_alloc_ctrl_packet() - allocate control packet skb 548 * @pkt: reference to qrtr_ctrl_pkt pointer 549 * @flags: the type of memory to allocate 550 * 551 * Returns newly allocated sk_buff, or NULL on failure 552 * 553 * This function allocates a sk_buff large enough to carry a qrtr_ctrl_pkt and 554 * on success returns a reference to the control packet in @pkt. 555 */ 556 static struct sk_buff *qrtr_alloc_ctrl_packet(struct qrtr_ctrl_pkt **pkt, 557 gfp_t flags) 558 { 559 const int pkt_len = sizeof(struct qrtr_ctrl_pkt); 560 struct sk_buff *skb; 561 562 skb = alloc_skb(QRTR_HDR_MAX_SIZE + pkt_len, flags); 563 if (!skb) 564 return NULL; 565 566 skb_reserve(skb, QRTR_HDR_MAX_SIZE); 567 *pkt = skb_put_zero(skb, pkt_len); 568 569 return skb; 570 } 571 572 /** 573 * qrtr_endpoint_register() - register a new endpoint 574 * @ep: endpoint to register 575 * @nid: desired node id; may be QRTR_EP_NID_AUTO for auto-assignment 576 * Return: 0 on success; negative error code on failure 577 * 578 * The specified endpoint must have the xmit function pointer set on call. 579 */ 580 int qrtr_endpoint_register(struct qrtr_endpoint *ep, unsigned int nid) 581 { 582 struct qrtr_node *node; 583 584 if (!ep || !ep->xmit) 585 return -EINVAL; 586 587 node = kzalloc(sizeof(*node), GFP_KERNEL); 588 if (!node) 589 return -ENOMEM; 590 591 kref_init(&node->ref); 592 mutex_init(&node->ep_lock); 593 skb_queue_head_init(&node->rx_queue); 594 node->nid = QRTR_EP_NID_AUTO; 595 node->ep = ep; 596 597 INIT_RADIX_TREE(&node->qrtr_tx_flow, GFP_KERNEL); 598 mutex_init(&node->qrtr_tx_lock); 599 600 qrtr_node_assign(node, nid); 601 602 mutex_lock(&qrtr_node_lock); 603 list_add(&node->item, &qrtr_all_nodes); 604 mutex_unlock(&qrtr_node_lock); 605 ep->node = node; 606 607 return 0; 608 } 609 EXPORT_SYMBOL_GPL(qrtr_endpoint_register); 610 611 /** 612 * qrtr_endpoint_unregister - unregister endpoint 613 * @ep: endpoint to unregister 614 */ 615 void qrtr_endpoint_unregister(struct qrtr_endpoint *ep) 616 { 617 struct qrtr_node *node = ep->node; 618 struct sockaddr_qrtr src = {AF_QIPCRTR, node->nid, QRTR_PORT_CTRL}; 619 struct sockaddr_qrtr dst = {AF_QIPCRTR, qrtr_local_nid, QRTR_PORT_CTRL}; 620 struct radix_tree_iter iter; 621 struct qrtr_ctrl_pkt *pkt; 622 struct qrtr_tx_flow *flow; 623 struct sk_buff *skb; 624 unsigned long flags; 625 void __rcu **slot; 626 627 mutex_lock(&node->ep_lock); 628 node->ep = NULL; 629 mutex_unlock(&node->ep_lock); 630 631 /* Notify the local controller about the event */ 632 spin_lock_irqsave(&qrtr_nodes_lock, flags); 633 radix_tree_for_each_slot(slot, &qrtr_nodes, &iter, 0) { 634 if (*slot != node) 635 continue; 636 src.sq_node = iter.index; 637 skb = qrtr_alloc_ctrl_packet(&pkt, GFP_ATOMIC); 638 if (skb) { 639 pkt->cmd = cpu_to_le32(QRTR_TYPE_BYE); 640 qrtr_local_enqueue(NULL, skb, QRTR_TYPE_BYE, &src, &dst); 641 } 642 } 643 spin_unlock_irqrestore(&qrtr_nodes_lock, flags); 644 645 /* Wake up any transmitters waiting for resume-tx from the node */ 646 mutex_lock(&node->qrtr_tx_lock); 647 radix_tree_for_each_slot(slot, &node->qrtr_tx_flow, &iter, 0) { 648 flow = *slot; 649 wake_up_interruptible_all(&flow->resume_tx); 650 } 651 mutex_unlock(&node->qrtr_tx_lock); 652 653 qrtr_node_release(node); 654 ep->node = NULL; 655 } 656 EXPORT_SYMBOL_GPL(qrtr_endpoint_unregister); 657 658 /* Lookup socket by port. 659 * 660 * Callers must release with qrtr_port_put() 661 */ 662 static struct qrtr_sock *qrtr_port_lookup(int port) 663 { 664 struct qrtr_sock *ipc; 665 666 if (port == QRTR_PORT_CTRL) 667 port = 0; 668 669 rcu_read_lock(); 670 ipc = xa_load(&qrtr_ports, port); 671 if (ipc) 672 sock_hold(&ipc->sk); 673 rcu_read_unlock(); 674 675 return ipc; 676 } 677 678 /* Release acquired socket. */ 679 static void qrtr_port_put(struct qrtr_sock *ipc) 680 { 681 sock_put(&ipc->sk); 682 } 683 684 /* Remove port assignment. */ 685 static void qrtr_port_remove(struct qrtr_sock *ipc) 686 { 687 struct qrtr_ctrl_pkt *pkt; 688 struct sk_buff *skb; 689 int port = ipc->us.sq_port; 690 struct sockaddr_qrtr to; 691 692 to.sq_family = AF_QIPCRTR; 693 to.sq_node = QRTR_NODE_BCAST; 694 to.sq_port = QRTR_PORT_CTRL; 695 696 skb = qrtr_alloc_ctrl_packet(&pkt, GFP_KERNEL); 697 if (skb) { 698 pkt->cmd = cpu_to_le32(QRTR_TYPE_DEL_CLIENT); 699 pkt->client.node = cpu_to_le32(ipc->us.sq_node); 700 pkt->client.port = cpu_to_le32(ipc->us.sq_port); 701 702 skb_set_owner_w(skb, &ipc->sk); 703 qrtr_bcast_enqueue(NULL, skb, QRTR_TYPE_DEL_CLIENT, &ipc->us, 704 &to); 705 } 706 707 if (port == QRTR_PORT_CTRL) 708 port = 0; 709 710 __sock_put(&ipc->sk); 711 712 xa_erase(&qrtr_ports, port); 713 714 /* Ensure that if qrtr_port_lookup() did enter the RCU read section we 715 * wait for it to up increment the refcount */ 716 synchronize_rcu(); 717 } 718 719 /* Assign port number to socket. 720 * 721 * Specify port in the integer pointed to by port, and it will be adjusted 722 * on return as necesssary. 723 * 724 * Port may be: 725 * 0: Assign ephemeral port in [QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET] 726 * <QRTR_MIN_EPH_SOCKET: Specified; requires CAP_NET_ADMIN 727 * >QRTR_MIN_EPH_SOCKET: Specified; available to all 728 */ 729 static int qrtr_port_assign(struct qrtr_sock *ipc, int *port) 730 { 731 int rc; 732 733 if (!*port) { 734 rc = xa_alloc(&qrtr_ports, port, ipc, QRTR_EPH_PORT_RANGE, 735 GFP_KERNEL); 736 } else if (*port < QRTR_MIN_EPH_SOCKET && !capable(CAP_NET_ADMIN)) { 737 rc = -EACCES; 738 } else if (*port == QRTR_PORT_CTRL) { 739 rc = xa_insert(&qrtr_ports, 0, ipc, GFP_KERNEL); 740 } else { 741 rc = xa_insert(&qrtr_ports, *port, ipc, GFP_KERNEL); 742 } 743 744 if (rc == -EBUSY) 745 return -EADDRINUSE; 746 else if (rc < 0) 747 return rc; 748 749 sock_hold(&ipc->sk); 750 751 return 0; 752 } 753 754 /* Reset all non-control ports */ 755 static void qrtr_reset_ports(void) 756 { 757 struct qrtr_sock *ipc; 758 unsigned long index; 759 760 rcu_read_lock(); 761 xa_for_each_start(&qrtr_ports, index, ipc, 1) { 762 sock_hold(&ipc->sk); 763 ipc->sk.sk_err = ENETRESET; 764 sk_error_report(&ipc->sk); 765 sock_put(&ipc->sk); 766 } 767 rcu_read_unlock(); 768 } 769 770 /* Bind socket to address. 771 * 772 * Socket should be locked upon call. 773 */ 774 static int __qrtr_bind(struct socket *sock, 775 const struct sockaddr_qrtr *addr, int zapped) 776 { 777 struct qrtr_sock *ipc = qrtr_sk(sock->sk); 778 struct sock *sk = sock->sk; 779 int port; 780 int rc; 781 782 /* rebinding ok */ 783 if (!zapped && addr->sq_port == ipc->us.sq_port) 784 return 0; 785 786 port = addr->sq_port; 787 rc = qrtr_port_assign(ipc, &port); 788 if (rc) 789 return rc; 790 791 /* unbind previous, if any */ 792 if (!zapped) 793 qrtr_port_remove(ipc); 794 ipc->us.sq_port = port; 795 796 sock_reset_flag(sk, SOCK_ZAPPED); 797 798 /* Notify all open ports about the new controller */ 799 if (port == QRTR_PORT_CTRL) 800 qrtr_reset_ports(); 801 802 return 0; 803 } 804 805 /* Auto bind to an ephemeral port. */ 806 static int qrtr_autobind(struct socket *sock) 807 { 808 struct sock *sk = sock->sk; 809 struct sockaddr_qrtr addr; 810 811 if (!sock_flag(sk, SOCK_ZAPPED)) 812 return 0; 813 814 addr.sq_family = AF_QIPCRTR; 815 addr.sq_node = qrtr_local_nid; 816 addr.sq_port = 0; 817 818 return __qrtr_bind(sock, &addr, 1); 819 } 820 821 /* Bind socket to specified sockaddr. */ 822 static int qrtr_bind(struct socket *sock, struct sockaddr *saddr, int len) 823 { 824 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, saddr); 825 struct qrtr_sock *ipc = qrtr_sk(sock->sk); 826 struct sock *sk = sock->sk; 827 int rc; 828 829 if (len < sizeof(*addr) || addr->sq_family != AF_QIPCRTR) 830 return -EINVAL; 831 832 if (addr->sq_node != ipc->us.sq_node) 833 return -EINVAL; 834 835 lock_sock(sk); 836 rc = __qrtr_bind(sock, addr, sock_flag(sk, SOCK_ZAPPED)); 837 release_sock(sk); 838 839 return rc; 840 } 841 842 /* Queue packet to local peer socket. */ 843 static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb, 844 int type, struct sockaddr_qrtr *from, 845 struct sockaddr_qrtr *to) 846 { 847 struct qrtr_sock *ipc; 848 struct qrtr_cb *cb; 849 850 ipc = qrtr_port_lookup(to->sq_port); 851 if (!ipc || &ipc->sk == skb->sk) { /* do not send to self */ 852 if (ipc) 853 qrtr_port_put(ipc); 854 kfree_skb(skb); 855 return -ENODEV; 856 } 857 858 cb = (struct qrtr_cb *)skb->cb; 859 cb->src_node = from->sq_node; 860 cb->src_port = from->sq_port; 861 862 if (sock_queue_rcv_skb(&ipc->sk, skb)) { 863 qrtr_port_put(ipc); 864 kfree_skb(skb); 865 return -ENOSPC; 866 } 867 868 qrtr_port_put(ipc); 869 870 return 0; 871 } 872 873 /* Queue packet for broadcast. */ 874 static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb, 875 int type, struct sockaddr_qrtr *from, 876 struct sockaddr_qrtr *to) 877 { 878 struct sk_buff *skbn; 879 880 mutex_lock(&qrtr_node_lock); 881 list_for_each_entry(node, &qrtr_all_nodes, item) { 882 skbn = skb_clone(skb, GFP_KERNEL); 883 if (!skbn) 884 break; 885 skb_set_owner_w(skbn, skb->sk); 886 qrtr_node_enqueue(node, skbn, type, from, to); 887 } 888 mutex_unlock(&qrtr_node_lock); 889 890 qrtr_local_enqueue(NULL, skb, type, from, to); 891 892 return 0; 893 } 894 895 static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) 896 { 897 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg->msg_name); 898 int (*enqueue_fn)(struct qrtr_node *, struct sk_buff *, int, 899 struct sockaddr_qrtr *, struct sockaddr_qrtr *); 900 __le32 qrtr_type = cpu_to_le32(QRTR_TYPE_DATA); 901 struct qrtr_sock *ipc = qrtr_sk(sock->sk); 902 struct sock *sk = sock->sk; 903 struct qrtr_node *node; 904 struct sk_buff *skb; 905 size_t plen; 906 u32 type; 907 int rc; 908 909 if (msg->msg_flags & ~(MSG_DONTWAIT)) 910 return -EINVAL; 911 912 if (len > 65535) 913 return -EMSGSIZE; 914 915 lock_sock(sk); 916 917 if (addr) { 918 if (msg->msg_namelen < sizeof(*addr)) { 919 release_sock(sk); 920 return -EINVAL; 921 } 922 923 if (addr->sq_family != AF_QIPCRTR) { 924 release_sock(sk); 925 return -EINVAL; 926 } 927 928 rc = qrtr_autobind(sock); 929 if (rc) { 930 release_sock(sk); 931 return rc; 932 } 933 } else if (sk->sk_state == TCP_ESTABLISHED) { 934 addr = &ipc->peer; 935 } else { 936 release_sock(sk); 937 return -ENOTCONN; 938 } 939 940 node = NULL; 941 if (addr->sq_node == QRTR_NODE_BCAST) { 942 if (addr->sq_port != QRTR_PORT_CTRL && 943 qrtr_local_nid != QRTR_NODE_BCAST) { 944 release_sock(sk); 945 return -ENOTCONN; 946 } 947 enqueue_fn = qrtr_bcast_enqueue; 948 } else if (addr->sq_node == ipc->us.sq_node) { 949 enqueue_fn = qrtr_local_enqueue; 950 } else { 951 node = qrtr_node_lookup(addr->sq_node); 952 if (!node) { 953 release_sock(sk); 954 return -ECONNRESET; 955 } 956 enqueue_fn = qrtr_node_enqueue; 957 } 958 959 plen = (len + 3) & ~3; 960 skb = sock_alloc_send_skb(sk, plen + QRTR_HDR_MAX_SIZE, 961 msg->msg_flags & MSG_DONTWAIT, &rc); 962 if (!skb) { 963 rc = -ENOMEM; 964 goto out_node; 965 } 966 967 skb_reserve(skb, QRTR_HDR_MAX_SIZE); 968 969 rc = memcpy_from_msg(skb_put(skb, len), msg, len); 970 if (rc) { 971 kfree_skb(skb); 972 goto out_node; 973 } 974 975 if (ipc->us.sq_port == QRTR_PORT_CTRL) { 976 if (len < 4) { 977 rc = -EINVAL; 978 kfree_skb(skb); 979 goto out_node; 980 } 981 982 /* control messages already require the type as 'command' */ 983 skb_copy_bits(skb, 0, &qrtr_type, 4); 984 } 985 986 type = le32_to_cpu(qrtr_type); 987 rc = enqueue_fn(node, skb, type, &ipc->us, addr); 988 if (rc >= 0) 989 rc = len; 990 991 out_node: 992 qrtr_node_release(node); 993 release_sock(sk); 994 995 return rc; 996 } 997 998 static int qrtr_send_resume_tx(struct qrtr_cb *cb) 999 { 1000 struct sockaddr_qrtr remote = { AF_QIPCRTR, cb->src_node, cb->src_port }; 1001 struct sockaddr_qrtr local = { AF_QIPCRTR, cb->dst_node, cb->dst_port }; 1002 struct qrtr_ctrl_pkt *pkt; 1003 struct qrtr_node *node; 1004 struct sk_buff *skb; 1005 int ret; 1006 1007 node = qrtr_node_lookup(remote.sq_node); 1008 if (!node) 1009 return -EINVAL; 1010 1011 skb = qrtr_alloc_ctrl_packet(&pkt, GFP_KERNEL); 1012 if (!skb) 1013 return -ENOMEM; 1014 1015 pkt->cmd = cpu_to_le32(QRTR_TYPE_RESUME_TX); 1016 pkt->client.node = cpu_to_le32(cb->dst_node); 1017 pkt->client.port = cpu_to_le32(cb->dst_port); 1018 1019 ret = qrtr_node_enqueue(node, skb, QRTR_TYPE_RESUME_TX, &local, &remote); 1020 1021 qrtr_node_release(node); 1022 1023 return ret; 1024 } 1025 1026 static int qrtr_recvmsg(struct socket *sock, struct msghdr *msg, 1027 size_t size, int flags) 1028 { 1029 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg->msg_name); 1030 struct sock *sk = sock->sk; 1031 struct sk_buff *skb; 1032 struct qrtr_cb *cb; 1033 int copied, rc; 1034 1035 lock_sock(sk); 1036 1037 if (sock_flag(sk, SOCK_ZAPPED)) { 1038 release_sock(sk); 1039 return -EADDRNOTAVAIL; 1040 } 1041 1042 skb = skb_recv_datagram(sk, flags, &rc); 1043 if (!skb) { 1044 release_sock(sk); 1045 return rc; 1046 } 1047 cb = (struct qrtr_cb *)skb->cb; 1048 1049 copied = skb->len; 1050 if (copied > size) { 1051 copied = size; 1052 msg->msg_flags |= MSG_TRUNC; 1053 } 1054 1055 rc = skb_copy_datagram_msg(skb, 0, msg, copied); 1056 if (rc < 0) 1057 goto out; 1058 rc = copied; 1059 1060 if (addr) { 1061 /* There is an anonymous 2-byte hole after sq_family, 1062 * make sure to clear it. 1063 */ 1064 memset(addr, 0, sizeof(*addr)); 1065 1066 addr->sq_family = AF_QIPCRTR; 1067 addr->sq_node = cb->src_node; 1068 addr->sq_port = cb->src_port; 1069 msg->msg_namelen = sizeof(*addr); 1070 } 1071 1072 out: 1073 if (cb->confirm_rx) 1074 qrtr_send_resume_tx(cb); 1075 1076 skb_free_datagram(sk, skb); 1077 release_sock(sk); 1078 1079 return rc; 1080 } 1081 1082 static int qrtr_connect(struct socket *sock, struct sockaddr *saddr, 1083 int len, int flags) 1084 { 1085 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, saddr); 1086 struct qrtr_sock *ipc = qrtr_sk(sock->sk); 1087 struct sock *sk = sock->sk; 1088 int rc; 1089 1090 if (len < sizeof(*addr) || addr->sq_family != AF_QIPCRTR) 1091 return -EINVAL; 1092 1093 lock_sock(sk); 1094 1095 sk->sk_state = TCP_CLOSE; 1096 sock->state = SS_UNCONNECTED; 1097 1098 rc = qrtr_autobind(sock); 1099 if (rc) { 1100 release_sock(sk); 1101 return rc; 1102 } 1103 1104 ipc->peer = *addr; 1105 sock->state = SS_CONNECTED; 1106 sk->sk_state = TCP_ESTABLISHED; 1107 1108 release_sock(sk); 1109 1110 return 0; 1111 } 1112 1113 static int qrtr_getname(struct socket *sock, struct sockaddr *saddr, 1114 int peer) 1115 { 1116 struct qrtr_sock *ipc = qrtr_sk(sock->sk); 1117 struct sockaddr_qrtr qaddr; 1118 struct sock *sk = sock->sk; 1119 1120 lock_sock(sk); 1121 if (peer) { 1122 if (sk->sk_state != TCP_ESTABLISHED) { 1123 release_sock(sk); 1124 return -ENOTCONN; 1125 } 1126 1127 qaddr = ipc->peer; 1128 } else { 1129 qaddr = ipc->us; 1130 } 1131 release_sock(sk); 1132 1133 qaddr.sq_family = AF_QIPCRTR; 1134 1135 memcpy(saddr, &qaddr, sizeof(qaddr)); 1136 1137 return sizeof(qaddr); 1138 } 1139 1140 static int qrtr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 1141 { 1142 void __user *argp = (void __user *)arg; 1143 struct qrtr_sock *ipc = qrtr_sk(sock->sk); 1144 struct sock *sk = sock->sk; 1145 struct sockaddr_qrtr *sq; 1146 struct sk_buff *skb; 1147 struct ifreq ifr; 1148 long len = 0; 1149 int rc = 0; 1150 1151 lock_sock(sk); 1152 1153 switch (cmd) { 1154 case TIOCOUTQ: 1155 len = sk->sk_sndbuf - sk_wmem_alloc_get(sk); 1156 if (len < 0) 1157 len = 0; 1158 rc = put_user(len, (int __user *)argp); 1159 break; 1160 case TIOCINQ: 1161 skb = skb_peek(&sk->sk_receive_queue); 1162 if (skb) 1163 len = skb->len; 1164 rc = put_user(len, (int __user *)argp); 1165 break; 1166 case SIOCGIFADDR: 1167 if (get_user_ifreq(&ifr, NULL, argp)) { 1168 rc = -EFAULT; 1169 break; 1170 } 1171 1172 sq = (struct sockaddr_qrtr *)&ifr.ifr_addr; 1173 *sq = ipc->us; 1174 if (put_user_ifreq(&ifr, argp)) { 1175 rc = -EFAULT; 1176 break; 1177 } 1178 break; 1179 case SIOCADDRT: 1180 case SIOCDELRT: 1181 case SIOCSIFADDR: 1182 case SIOCGIFDSTADDR: 1183 case SIOCSIFDSTADDR: 1184 case SIOCGIFBRDADDR: 1185 case SIOCSIFBRDADDR: 1186 case SIOCGIFNETMASK: 1187 case SIOCSIFNETMASK: 1188 rc = -EINVAL; 1189 break; 1190 default: 1191 rc = -ENOIOCTLCMD; 1192 break; 1193 } 1194 1195 release_sock(sk); 1196 1197 return rc; 1198 } 1199 1200 static int qrtr_release(struct socket *sock) 1201 { 1202 struct sock *sk = sock->sk; 1203 struct qrtr_sock *ipc; 1204 1205 if (!sk) 1206 return 0; 1207 1208 lock_sock(sk); 1209 1210 ipc = qrtr_sk(sk); 1211 sk->sk_shutdown = SHUTDOWN_MASK; 1212 if (!sock_flag(sk, SOCK_DEAD)) 1213 sk->sk_state_change(sk); 1214 1215 sock_set_flag(sk, SOCK_DEAD); 1216 sock_orphan(sk); 1217 sock->sk = NULL; 1218 1219 if (!sock_flag(sk, SOCK_ZAPPED)) 1220 qrtr_port_remove(ipc); 1221 1222 skb_queue_purge(&sk->sk_receive_queue); 1223 1224 release_sock(sk); 1225 sock_put(sk); 1226 1227 return 0; 1228 } 1229 1230 static const struct proto_ops qrtr_proto_ops = { 1231 .owner = THIS_MODULE, 1232 .family = AF_QIPCRTR, 1233 .bind = qrtr_bind, 1234 .connect = qrtr_connect, 1235 .socketpair = sock_no_socketpair, 1236 .accept = sock_no_accept, 1237 .listen = sock_no_listen, 1238 .sendmsg = qrtr_sendmsg, 1239 .recvmsg = qrtr_recvmsg, 1240 .getname = qrtr_getname, 1241 .ioctl = qrtr_ioctl, 1242 .gettstamp = sock_gettstamp, 1243 .poll = datagram_poll, 1244 .shutdown = sock_no_shutdown, 1245 .release = qrtr_release, 1246 .mmap = sock_no_mmap, 1247 .sendpage = sock_no_sendpage, 1248 }; 1249 1250 static struct proto qrtr_proto = { 1251 .name = "QIPCRTR", 1252 .owner = THIS_MODULE, 1253 .obj_size = sizeof(struct qrtr_sock), 1254 }; 1255 1256 static int qrtr_create(struct net *net, struct socket *sock, 1257 int protocol, int kern) 1258 { 1259 struct qrtr_sock *ipc; 1260 struct sock *sk; 1261 1262 if (sock->type != SOCK_DGRAM) 1263 return -EPROTOTYPE; 1264 1265 sk = sk_alloc(net, AF_QIPCRTR, GFP_KERNEL, &qrtr_proto, kern); 1266 if (!sk) 1267 return -ENOMEM; 1268 1269 sock_set_flag(sk, SOCK_ZAPPED); 1270 1271 sock_init_data(sock, sk); 1272 sock->ops = &qrtr_proto_ops; 1273 1274 ipc = qrtr_sk(sk); 1275 ipc->us.sq_family = AF_QIPCRTR; 1276 ipc->us.sq_node = qrtr_local_nid; 1277 ipc->us.sq_port = 0; 1278 1279 return 0; 1280 } 1281 1282 static const struct net_proto_family qrtr_family = { 1283 .owner = THIS_MODULE, 1284 .family = AF_QIPCRTR, 1285 .create = qrtr_create, 1286 }; 1287 1288 static int __init qrtr_proto_init(void) 1289 { 1290 int rc; 1291 1292 rc = proto_register(&qrtr_proto, 1); 1293 if (rc) 1294 return rc; 1295 1296 rc = sock_register(&qrtr_family); 1297 if (rc) 1298 goto err_proto; 1299 1300 rc = qrtr_ns_init(); 1301 if (rc) 1302 goto err_sock; 1303 1304 return 0; 1305 1306 err_sock: 1307 sock_unregister(qrtr_family.family); 1308 err_proto: 1309 proto_unregister(&qrtr_proto); 1310 return rc; 1311 } 1312 postcore_initcall(qrtr_proto_init); 1313 1314 static void __exit qrtr_proto_fini(void) 1315 { 1316 qrtr_ns_remove(); 1317 sock_unregister(qrtr_family.family); 1318 proto_unregister(&qrtr_proto); 1319 } 1320 module_exit(qrtr_proto_fini); 1321 1322 MODULE_DESCRIPTION("Qualcomm IPC-router driver"); 1323 MODULE_LICENSE("GPL v2"); 1324 MODULE_ALIAS_NETPROTO(PF_QIPCRTR); 1325