1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015, Sony Mobile Communications Inc.
4 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
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>
17 #define QRTR_PROTO_VER_1 1
18 #define QRTR_PROTO_VER_2 3
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)
26 #define QRTR_PORT_CTRL_LEGACY 0xffff
29 * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1
30 * @version: protocol version
31 * @type: packet type; one of QRTR_TYPE_*
32 * @src_node_id: source node
33 * @src_port_id: source port
34 * @confirm_rx: boolean; whether a resume-tx packet should be send in reply
35 * @size: length of packet, excluding this header
36 * @dst_node_id: destination node
37 * @dst_port_id: destination port
51 * struct qrtr_hdr_v2 - (I|R)PCrouter packet header later versions
52 * @version: protocol version
53 * @type: packet type; one of QRTR_TYPE_*
54 * @flags: bitmask of QRTR_FLAGS_*
55 * @optlen: length of optional header data
56 * @size: length of packet, excluding this header and optlen
57 * @src_node_id: source node
58 * @src_port_id: source port
59 * @dst_node_id: destination node
60 * @dst_port_id: destination port
74 #define QRTR_FLAGS_CONFIRM_RX BIT(0)
86 #define QRTR_HDR_MAX_SIZE max_t(size_t, sizeof(struct qrtr_hdr_v1), \
87 sizeof(struct qrtr_hdr_v2))
90 /* WARNING: sk must be the first member */
92 struct sockaddr_qrtr us
;
93 struct sockaddr_qrtr peer
;
96 static inline struct qrtr_sock
*qrtr_sk(struct sock
*sk
)
98 BUILD_BUG_ON(offsetof(struct qrtr_sock
, sk
) != 0);
99 return container_of(sk
, struct qrtr_sock
, sk
);
102 static unsigned int qrtr_local_nid
= 1;
105 static RADIX_TREE(qrtr_nodes
, GFP_ATOMIC
);
106 static DEFINE_SPINLOCK(qrtr_nodes_lock
);
108 static LIST_HEAD(qrtr_all_nodes
);
109 /* lock for qrtr_all_nodes and node reference */
110 static DEFINE_MUTEX(qrtr_node_lock
);
112 /* local port allocation management */
113 static DEFINE_XARRAY_ALLOC(qrtr_ports
);
116 * struct qrtr_node - endpoint node
117 * @ep_lock: lock for endpoint management and callbacks
119 * @ref: reference count for node
121 * @qrtr_tx_flow: tree of qrtr_tx_flow, keyed by node << 32 | port
122 * @qrtr_tx_lock: lock for qrtr_tx_flow inserts
123 * @rx_queue: receive queue
124 * @item: list item for broadcast list
127 struct mutex ep_lock
;
128 struct qrtr_endpoint
*ep
;
132 struct radix_tree_root qrtr_tx_flow
;
133 struct mutex qrtr_tx_lock
; /* for qrtr_tx_flow */
135 struct sk_buff_head rx_queue
;
136 struct list_head item
;
140 * struct qrtr_tx_flow - tx flow control
141 * @resume_tx: waiters for a resume tx from the remote
142 * @pending: number of waiting senders
143 * @tx_failed: indicates that a message with confirm_rx flag was lost
145 struct qrtr_tx_flow
{
146 struct wait_queue_head resume_tx
;
151 #define QRTR_TX_FLOW_HIGH 10
152 #define QRTR_TX_FLOW_LOW 5
154 static int qrtr_local_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
,
155 int type
, struct sockaddr_qrtr
*from
,
156 struct sockaddr_qrtr
*to
);
157 static int qrtr_bcast_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
,
158 int type
, struct sockaddr_qrtr
*from
,
159 struct sockaddr_qrtr
*to
);
160 static struct qrtr_sock
*qrtr_port_lookup(int port
);
161 static void qrtr_port_put(struct qrtr_sock
*ipc
);
163 /* Release node resources and free the node.
165 * Do not call directly, use qrtr_node_release. To be used with
166 * kref_put_mutex. As such, the node mutex is expected to be locked on call.
168 static void __qrtr_node_release(struct kref
*kref
)
170 struct qrtr_node
*node
= container_of(kref
, struct qrtr_node
, ref
);
171 struct radix_tree_iter iter
;
172 struct qrtr_tx_flow
*flow
;
176 spin_lock_irqsave(&qrtr_nodes_lock
, flags
);
177 /* If the node is a bridge for other nodes, there are possibly
178 * multiple entries pointing to our released node, delete them all.
180 radix_tree_for_each_slot(slot
, &qrtr_nodes
, &iter
, 0) {
182 radix_tree_iter_delete(&qrtr_nodes
, &iter
, slot
);
184 spin_unlock_irqrestore(&qrtr_nodes_lock
, flags
);
186 list_del(&node
->item
);
187 mutex_unlock(&qrtr_node_lock
);
189 skb_queue_purge(&node
->rx_queue
);
191 /* Free tx flow counters */
192 radix_tree_for_each_slot(slot
, &node
->qrtr_tx_flow
, &iter
, 0) {
194 radix_tree_iter_delete(&node
->qrtr_tx_flow
, &iter
, slot
);
200 /* Increment reference to node. */
201 static struct qrtr_node
*qrtr_node_acquire(struct qrtr_node
*node
)
204 kref_get(&node
->ref
);
208 /* Decrement reference to node and release as necessary. */
209 static void qrtr_node_release(struct qrtr_node
*node
)
213 kref_put_mutex(&node
->ref
, __qrtr_node_release
, &qrtr_node_lock
);
217 * qrtr_tx_resume() - reset flow control counter
218 * @node: qrtr_node that the QRTR_TYPE_RESUME_TX packet arrived on
219 * @skb: resume_tx packet
221 static void qrtr_tx_resume(struct qrtr_node
*node
, struct sk_buff
*skb
)
223 struct qrtr_ctrl_pkt
*pkt
= (struct qrtr_ctrl_pkt
*)skb
->data
;
224 u64 remote_node
= le32_to_cpu(pkt
->client
.node
);
225 u32 remote_port
= le32_to_cpu(pkt
->client
.port
);
226 struct qrtr_tx_flow
*flow
;
229 key
= remote_node
<< 32 | remote_port
;
232 flow
= radix_tree_lookup(&node
->qrtr_tx_flow
, key
);
235 spin_lock(&flow
->resume_tx
.lock
);
237 spin_unlock(&flow
->resume_tx
.lock
);
238 wake_up_interruptible_all(&flow
->resume_tx
);
245 * qrtr_tx_wait() - flow control for outgoing packets
246 * @node: qrtr_node that the packet is to be send to
247 * @dest_node: node id of the destination
248 * @dest_port: port number of the destination
249 * @type: type of message
251 * The flow control scheme is based around the low and high "watermarks". When
252 * the low watermark is passed the confirm_rx flag is set on the outgoing
253 * message, which will trigger the remote to send a control message of the type
254 * QRTR_TYPE_RESUME_TX to reset the counter. If the high watermark is hit
255 * further transmision should be paused.
257 * Return: 1 if confirm_rx should be set, 0 otherwise or errno failure
259 static int qrtr_tx_wait(struct qrtr_node
*node
, int dest_node
, int dest_port
,
262 unsigned long key
= (u64
)dest_node
<< 32 | dest_port
;
263 struct qrtr_tx_flow
*flow
;
267 /* Never set confirm_rx on non-data packets */
268 if (type
!= QRTR_TYPE_DATA
)
271 mutex_lock(&node
->qrtr_tx_lock
);
272 flow
= radix_tree_lookup(&node
->qrtr_tx_flow
, key
);
274 flow
= kzalloc(sizeof(*flow
), GFP_KERNEL
);
276 init_waitqueue_head(&flow
->resume_tx
);
277 if (radix_tree_insert(&node
->qrtr_tx_flow
, key
, flow
)) {
283 mutex_unlock(&node
->qrtr_tx_lock
);
285 /* Set confirm_rx if we where unable to find and allocate a flow */
289 spin_lock_irq(&flow
->resume_tx
.lock
);
290 ret
= wait_event_interruptible_locked_irq(flow
->resume_tx
,
291 flow
->pending
< QRTR_TX_FLOW_HIGH
||
296 } else if (!node
->ep
) {
298 } else if (flow
->tx_failed
) {
303 confirm_rx
= flow
->pending
== QRTR_TX_FLOW_LOW
;
305 spin_unlock_irq(&flow
->resume_tx
.lock
);
311 * qrtr_tx_flow_failed() - flag that tx of confirm_rx flagged messages failed
312 * @node: qrtr_node that the packet is to be send to
313 * @dest_node: node id of the destination
314 * @dest_port: port number of the destination
316 * Signal that the transmission of a message with confirm_rx flag failed. The
317 * flow's "pending" counter will keep incrementing towards QRTR_TX_FLOW_HIGH,
318 * at which point transmission would stall forever waiting for the resume TX
319 * message associated with the dropped confirm_rx message.
320 * Work around this by marking the flow as having a failed transmission and
321 * cause the next transmission attempt to be sent with the confirm_rx.
323 static void qrtr_tx_flow_failed(struct qrtr_node
*node
, int dest_node
,
326 unsigned long key
= (u64
)dest_node
<< 32 | dest_port
;
327 struct qrtr_tx_flow
*flow
;
330 flow
= radix_tree_lookup(&node
->qrtr_tx_flow
, key
);
333 spin_lock_irq(&flow
->resume_tx
.lock
);
335 spin_unlock_irq(&flow
->resume_tx
.lock
);
339 /* Pass an outgoing packet socket buffer to the endpoint driver. */
340 static int qrtr_node_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
,
341 int type
, struct sockaddr_qrtr
*from
,
342 struct sockaddr_qrtr
*to
)
344 struct qrtr_hdr_v1
*hdr
;
345 size_t len
= skb
->len
;
348 confirm_rx
= qrtr_tx_wait(node
, to
->sq_node
, to
->sq_port
, type
);
349 if (confirm_rx
< 0) {
354 hdr
= skb_push(skb
, sizeof(*hdr
));
355 hdr
->version
= cpu_to_le32(QRTR_PROTO_VER_1
);
356 hdr
->type
= cpu_to_le32(type
);
357 hdr
->src_node_id
= cpu_to_le32(from
->sq_node
);
358 hdr
->src_port_id
= cpu_to_le32(from
->sq_port
);
359 if (to
->sq_port
== QRTR_PORT_CTRL
) {
360 hdr
->dst_node_id
= cpu_to_le32(node
->nid
);
361 hdr
->dst_port_id
= cpu_to_le32(QRTR_PORT_CTRL
);
363 hdr
->dst_node_id
= cpu_to_le32(to
->sq_node
);
364 hdr
->dst_port_id
= cpu_to_le32(to
->sq_port
);
367 hdr
->size
= cpu_to_le32(len
);
368 hdr
->confirm_rx
= !!confirm_rx
;
370 rc
= skb_put_padto(skb
, ALIGN(len
, 4) + sizeof(*hdr
));
373 mutex_lock(&node
->ep_lock
);
376 rc
= node
->ep
->xmit(node
->ep
, skb
);
379 mutex_unlock(&node
->ep_lock
);
381 /* Need to ensure that a subsequent message carries the otherwise lost
382 * confirm_rx flag if we dropped this one */
383 if (rc
&& confirm_rx
)
384 qrtr_tx_flow_failed(node
, to
->sq_node
, to
->sq_port
);
389 /* Lookup node by id.
391 * callers must release with qrtr_node_release()
393 static struct qrtr_node
*qrtr_node_lookup(unsigned int nid
)
395 struct qrtr_node
*node
;
398 mutex_lock(&qrtr_node_lock
);
399 spin_lock_irqsave(&qrtr_nodes_lock
, flags
);
400 node
= radix_tree_lookup(&qrtr_nodes
, nid
);
401 node
= qrtr_node_acquire(node
);
402 spin_unlock_irqrestore(&qrtr_nodes_lock
, flags
);
403 mutex_unlock(&qrtr_node_lock
);
408 /* Assign node id to node.
410 * This is mostly useful for automatic node id assignment, based on
411 * the source id in the incoming packet.
413 static void qrtr_node_assign(struct qrtr_node
*node
, unsigned int nid
)
417 if (nid
== QRTR_EP_NID_AUTO
)
420 spin_lock_irqsave(&qrtr_nodes_lock
, flags
);
421 radix_tree_insert(&qrtr_nodes
, nid
, node
);
422 if (node
->nid
== QRTR_EP_NID_AUTO
)
424 spin_unlock_irqrestore(&qrtr_nodes_lock
, flags
);
428 * qrtr_endpoint_post() - post incoming data
429 * @ep: endpoint handle
430 * @data: data pointer
431 * @len: size of data in bytes
433 * Return: 0 on success; negative error code on failure
435 int qrtr_endpoint_post(struct qrtr_endpoint
*ep
, const void *data
, size_t len
)
437 struct qrtr_node
*node
= ep
->node
;
438 const struct qrtr_hdr_v1
*v1
;
439 const struct qrtr_hdr_v2
*v2
;
440 struct qrtr_sock
*ipc
;
447 if (len
== 0 || len
& 3)
450 skb
= __netdev_alloc_skb(NULL
, len
, GFP_ATOMIC
| __GFP_NOWARN
);
454 cb
= (struct qrtr_cb
*)skb
->cb
;
456 /* Version field in v1 is little endian, so this works for both cases */
460 case QRTR_PROTO_VER_1
:
461 if (len
< sizeof(*v1
))
464 hdrlen
= sizeof(*v1
);
466 cb
->type
= le32_to_cpu(v1
->type
);
467 cb
->src_node
= le32_to_cpu(v1
->src_node_id
);
468 cb
->src_port
= le32_to_cpu(v1
->src_port_id
);
469 cb
->confirm_rx
= !!v1
->confirm_rx
;
470 cb
->dst_node
= le32_to_cpu(v1
->dst_node_id
);
471 cb
->dst_port
= le32_to_cpu(v1
->dst_port_id
);
473 size
= le32_to_cpu(v1
->size
);
475 case QRTR_PROTO_VER_2
:
476 if (len
< sizeof(*v2
))
479 hdrlen
= sizeof(*v2
) + v2
->optlen
;
482 cb
->confirm_rx
= !!(v2
->flags
& QRTR_FLAGS_CONFIRM_RX
);
483 cb
->src_node
= le16_to_cpu(v2
->src_node_id
);
484 cb
->src_port
= le16_to_cpu(v2
->src_port_id
);
485 cb
->dst_node
= le16_to_cpu(v2
->dst_node_id
);
486 cb
->dst_port
= le16_to_cpu(v2
->dst_port_id
);
488 if (cb
->src_port
== (u16
)QRTR_PORT_CTRL
)
489 cb
->src_port
= QRTR_PORT_CTRL
;
490 if (cb
->dst_port
== (u16
)QRTR_PORT_CTRL
)
491 cb
->dst_port
= QRTR_PORT_CTRL
;
493 size
= le32_to_cpu(v2
->size
);
496 pr_err("qrtr: Invalid version %d\n", ver
);
500 if (cb
->dst_port
== QRTR_PORT_CTRL_LEGACY
)
501 cb
->dst_port
= QRTR_PORT_CTRL
;
503 if (!size
|| len
!= ALIGN(size
, 4) + hdrlen
)
506 if ((cb
->type
== QRTR_TYPE_NEW_SERVER
||
507 cb
->type
== QRTR_TYPE_RESUME_TX
) &&
508 size
< sizeof(struct qrtr_ctrl_pkt
))
511 if (cb
->dst_port
!= QRTR_PORT_CTRL
&& cb
->type
!= QRTR_TYPE_DATA
&&
512 cb
->type
!= QRTR_TYPE_RESUME_TX
)
515 skb_put_data(skb
, data
+ hdrlen
, size
);
517 qrtr_node_assign(node
, cb
->src_node
);
519 if (cb
->type
== QRTR_TYPE_NEW_SERVER
) {
520 /* Remote node endpoint can bridge other distant nodes */
521 const struct qrtr_ctrl_pkt
*pkt
;
524 qrtr_node_assign(node
, le32_to_cpu(pkt
->server
.node
));
527 if (cb
->type
== QRTR_TYPE_RESUME_TX
) {
528 qrtr_tx_resume(node
, skb
);
530 ipc
= qrtr_port_lookup(cb
->dst_port
);
534 if (sock_queue_rcv_skb(&ipc
->sk
, skb
)) {
549 EXPORT_SYMBOL_GPL(qrtr_endpoint_post
);
552 * qrtr_alloc_ctrl_packet() - allocate control packet skb
553 * @pkt: reference to qrtr_ctrl_pkt pointer
554 * @flags: the type of memory to allocate
556 * Returns newly allocated sk_buff, or NULL on failure
558 * This function allocates a sk_buff large enough to carry a qrtr_ctrl_pkt and
559 * on success returns a reference to the control packet in @pkt.
561 static struct sk_buff
*qrtr_alloc_ctrl_packet(struct qrtr_ctrl_pkt
**pkt
,
564 const int pkt_len
= sizeof(struct qrtr_ctrl_pkt
);
567 skb
= alloc_skb(QRTR_HDR_MAX_SIZE
+ pkt_len
, flags
);
571 skb_reserve(skb
, QRTR_HDR_MAX_SIZE
);
572 *pkt
= skb_put_zero(skb
, pkt_len
);
578 * qrtr_endpoint_register() - register a new endpoint
579 * @ep: endpoint to register
580 * @nid: desired node id; may be QRTR_EP_NID_AUTO for auto-assignment
581 * Return: 0 on success; negative error code on failure
583 * The specified endpoint must have the xmit function pointer set on call.
585 int qrtr_endpoint_register(struct qrtr_endpoint
*ep
, unsigned int nid
)
587 struct qrtr_node
*node
;
589 if (!ep
|| !ep
->xmit
)
592 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
596 kref_init(&node
->ref
);
597 mutex_init(&node
->ep_lock
);
598 skb_queue_head_init(&node
->rx_queue
);
599 node
->nid
= QRTR_EP_NID_AUTO
;
602 INIT_RADIX_TREE(&node
->qrtr_tx_flow
, GFP_KERNEL
);
603 mutex_init(&node
->qrtr_tx_lock
);
605 qrtr_node_assign(node
, nid
);
607 mutex_lock(&qrtr_node_lock
);
608 list_add(&node
->item
, &qrtr_all_nodes
);
609 mutex_unlock(&qrtr_node_lock
);
614 EXPORT_SYMBOL_GPL(qrtr_endpoint_register
);
617 * qrtr_endpoint_unregister - unregister endpoint
618 * @ep: endpoint to unregister
620 void qrtr_endpoint_unregister(struct qrtr_endpoint
*ep
)
622 struct qrtr_node
*node
= ep
->node
;
623 struct sockaddr_qrtr src
= {AF_QIPCRTR
, node
->nid
, QRTR_PORT_CTRL
};
624 struct sockaddr_qrtr dst
= {AF_QIPCRTR
, qrtr_local_nid
, QRTR_PORT_CTRL
};
625 struct radix_tree_iter iter
;
626 struct qrtr_ctrl_pkt
*pkt
;
627 struct qrtr_tx_flow
*flow
;
632 mutex_lock(&node
->ep_lock
);
634 mutex_unlock(&node
->ep_lock
);
636 /* Notify the local controller about the event */
637 spin_lock_irqsave(&qrtr_nodes_lock
, flags
);
638 radix_tree_for_each_slot(slot
, &qrtr_nodes
, &iter
, 0) {
641 src
.sq_node
= iter
.index
;
642 skb
= qrtr_alloc_ctrl_packet(&pkt
, GFP_ATOMIC
);
644 pkt
->cmd
= cpu_to_le32(QRTR_TYPE_BYE
);
645 qrtr_local_enqueue(NULL
, skb
, QRTR_TYPE_BYE
, &src
, &dst
);
648 spin_unlock_irqrestore(&qrtr_nodes_lock
, flags
);
650 /* Wake up any transmitters waiting for resume-tx from the node */
651 mutex_lock(&node
->qrtr_tx_lock
);
652 radix_tree_for_each_slot(slot
, &node
->qrtr_tx_flow
, &iter
, 0) {
654 wake_up_interruptible_all(&flow
->resume_tx
);
656 mutex_unlock(&node
->qrtr_tx_lock
);
658 qrtr_node_release(node
);
661 EXPORT_SYMBOL_GPL(qrtr_endpoint_unregister
);
663 /* Lookup socket by port.
665 * Callers must release with qrtr_port_put()
667 static struct qrtr_sock
*qrtr_port_lookup(int port
)
669 struct qrtr_sock
*ipc
;
671 if (port
== QRTR_PORT_CTRL
)
675 ipc
= xa_load(&qrtr_ports
, port
);
683 /* Release acquired socket. */
684 static void qrtr_port_put(struct qrtr_sock
*ipc
)
689 /* Remove port assignment. */
690 static void qrtr_port_remove(struct qrtr_sock
*ipc
)
692 struct qrtr_ctrl_pkt
*pkt
;
694 int port
= ipc
->us
.sq_port
;
695 struct sockaddr_qrtr to
;
697 to
.sq_family
= AF_QIPCRTR
;
698 to
.sq_node
= QRTR_NODE_BCAST
;
699 to
.sq_port
= QRTR_PORT_CTRL
;
701 skb
= qrtr_alloc_ctrl_packet(&pkt
, GFP_KERNEL
);
703 pkt
->cmd
= cpu_to_le32(QRTR_TYPE_DEL_CLIENT
);
704 pkt
->client
.node
= cpu_to_le32(ipc
->us
.sq_node
);
705 pkt
->client
.port
= cpu_to_le32(ipc
->us
.sq_port
);
707 skb_set_owner_w(skb
, &ipc
->sk
);
708 qrtr_bcast_enqueue(NULL
, skb
, QRTR_TYPE_DEL_CLIENT
, &ipc
->us
,
712 if (port
== QRTR_PORT_CTRL
)
715 __sock_put(&ipc
->sk
);
717 xa_erase(&qrtr_ports
, port
);
719 /* Ensure that if qrtr_port_lookup() did enter the RCU read section we
720 * wait for it to up increment the refcount */
724 /* Assign port number to socket.
726 * Specify port in the integer pointed to by port, and it will be adjusted
727 * on return as necesssary.
730 * 0: Assign ephemeral port in [QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET]
731 * <QRTR_MIN_EPH_SOCKET: Specified; requires CAP_NET_ADMIN
732 * >QRTR_MIN_EPH_SOCKET: Specified; available to all
734 static int qrtr_port_assign(struct qrtr_sock
*ipc
, int *port
)
739 rc
= xa_alloc(&qrtr_ports
, port
, ipc
, QRTR_EPH_PORT_RANGE
,
741 } else if (*port
< QRTR_MIN_EPH_SOCKET
&& !capable(CAP_NET_ADMIN
)) {
743 } else if (*port
== QRTR_PORT_CTRL
) {
744 rc
= xa_insert(&qrtr_ports
, 0, ipc
, GFP_KERNEL
);
746 rc
= xa_insert(&qrtr_ports
, *port
, ipc
, GFP_KERNEL
);
759 /* Reset all non-control ports */
760 static void qrtr_reset_ports(void)
762 struct qrtr_sock
*ipc
;
766 xa_for_each_start(&qrtr_ports
, index
, ipc
, 1) {
768 ipc
->sk
.sk_err
= ENETRESET
;
769 sk_error_report(&ipc
->sk
);
775 /* Bind socket to address.
777 * Socket should be locked upon call.
779 static int __qrtr_bind(struct socket
*sock
,
780 const struct sockaddr_qrtr
*addr
, int zapped
)
782 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
783 struct sock
*sk
= sock
->sk
;
788 if (!zapped
&& addr
->sq_port
== ipc
->us
.sq_port
)
791 port
= addr
->sq_port
;
792 rc
= qrtr_port_assign(ipc
, &port
);
796 /* unbind previous, if any */
798 qrtr_port_remove(ipc
);
799 ipc
->us
.sq_port
= port
;
801 sock_reset_flag(sk
, SOCK_ZAPPED
);
803 /* Notify all open ports about the new controller */
804 if (port
== QRTR_PORT_CTRL
)
810 /* Auto bind to an ephemeral port. */
811 static int qrtr_autobind(struct socket
*sock
)
813 struct sock
*sk
= sock
->sk
;
814 struct sockaddr_qrtr addr
;
816 if (!sock_flag(sk
, SOCK_ZAPPED
))
819 addr
.sq_family
= AF_QIPCRTR
;
820 addr
.sq_node
= qrtr_local_nid
;
823 return __qrtr_bind(sock
, &addr
, 1);
826 /* Bind socket to specified sockaddr. */
827 static int qrtr_bind(struct socket
*sock
, struct sockaddr
*saddr
, int len
)
829 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, saddr
);
830 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
831 struct sock
*sk
= sock
->sk
;
834 if (len
< sizeof(*addr
) || addr
->sq_family
!= AF_QIPCRTR
)
837 if (addr
->sq_node
!= ipc
->us
.sq_node
)
841 rc
= __qrtr_bind(sock
, addr
, sock_flag(sk
, SOCK_ZAPPED
));
847 /* Queue packet to local peer socket. */
848 static int qrtr_local_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
,
849 int type
, struct sockaddr_qrtr
*from
,
850 struct sockaddr_qrtr
*to
)
852 struct qrtr_sock
*ipc
;
855 ipc
= qrtr_port_lookup(to
->sq_port
);
856 if (!ipc
|| &ipc
->sk
== skb
->sk
) { /* do not send to self */
863 cb
= (struct qrtr_cb
*)skb
->cb
;
864 cb
->src_node
= from
->sq_node
;
865 cb
->src_port
= from
->sq_port
;
867 if (sock_queue_rcv_skb(&ipc
->sk
, skb
)) {
878 /* Queue packet for broadcast. */
879 static int qrtr_bcast_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
,
880 int type
, struct sockaddr_qrtr
*from
,
881 struct sockaddr_qrtr
*to
)
883 struct sk_buff
*skbn
;
885 mutex_lock(&qrtr_node_lock
);
886 list_for_each_entry(node
, &qrtr_all_nodes
, item
) {
887 skbn
= pskb_copy(skb
, GFP_KERNEL
);
890 skb_set_owner_w(skbn
, skb
->sk
);
891 qrtr_node_enqueue(node
, skbn
, type
, from
, to
);
893 mutex_unlock(&qrtr_node_lock
);
895 qrtr_local_enqueue(NULL
, skb
, type
, from
, to
);
900 static int qrtr_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
)
902 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, msg
->msg_name
);
903 int (*enqueue_fn
)(struct qrtr_node
*, struct sk_buff
*, int,
904 struct sockaddr_qrtr
*, struct sockaddr_qrtr
*);
905 __le32 qrtr_type
= cpu_to_le32(QRTR_TYPE_DATA
);
906 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
907 struct sock
*sk
= sock
->sk
;
908 struct qrtr_node
*node
;
914 if (msg
->msg_flags
& ~(MSG_DONTWAIT
))
923 if (msg
->msg_namelen
< sizeof(*addr
)) {
928 if (addr
->sq_family
!= AF_QIPCRTR
) {
933 rc
= qrtr_autobind(sock
);
938 } else if (sk
->sk_state
== TCP_ESTABLISHED
) {
946 if (addr
->sq_node
== QRTR_NODE_BCAST
) {
947 if (addr
->sq_port
!= QRTR_PORT_CTRL
&&
948 qrtr_local_nid
!= QRTR_NODE_BCAST
) {
952 enqueue_fn
= qrtr_bcast_enqueue
;
953 } else if (addr
->sq_node
== ipc
->us
.sq_node
) {
954 enqueue_fn
= qrtr_local_enqueue
;
956 node
= qrtr_node_lookup(addr
->sq_node
);
961 enqueue_fn
= qrtr_node_enqueue
;
964 plen
= (len
+ 3) & ~3;
965 skb
= sock_alloc_send_skb(sk
, plen
+ QRTR_HDR_MAX_SIZE
,
966 msg
->msg_flags
& MSG_DONTWAIT
, &rc
);
972 skb_reserve(skb
, QRTR_HDR_MAX_SIZE
);
974 rc
= memcpy_from_msg(skb_put(skb
, len
), msg
, len
);
980 if (ipc
->us
.sq_port
== QRTR_PORT_CTRL
) {
987 /* control messages already require the type as 'command' */
988 skb_copy_bits(skb
, 0, &qrtr_type
, 4);
991 type
= le32_to_cpu(qrtr_type
);
992 rc
= enqueue_fn(node
, skb
, type
, &ipc
->us
, addr
);
997 qrtr_node_release(node
);
1003 static int qrtr_send_resume_tx(struct qrtr_cb
*cb
)
1005 struct sockaddr_qrtr remote
= { AF_QIPCRTR
, cb
->src_node
, cb
->src_port
};
1006 struct sockaddr_qrtr local
= { AF_QIPCRTR
, cb
->dst_node
, cb
->dst_port
};
1007 struct qrtr_ctrl_pkt
*pkt
;
1008 struct qrtr_node
*node
;
1009 struct sk_buff
*skb
;
1012 node
= qrtr_node_lookup(remote
.sq_node
);
1016 skb
= qrtr_alloc_ctrl_packet(&pkt
, GFP_KERNEL
);
1020 pkt
->cmd
= cpu_to_le32(QRTR_TYPE_RESUME_TX
);
1021 pkt
->client
.node
= cpu_to_le32(cb
->dst_node
);
1022 pkt
->client
.port
= cpu_to_le32(cb
->dst_port
);
1024 ret
= qrtr_node_enqueue(node
, skb
, QRTR_TYPE_RESUME_TX
, &local
, &remote
);
1026 qrtr_node_release(node
);
1031 static int qrtr_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
1032 size_t size
, int flags
)
1034 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, msg
->msg_name
);
1035 struct sock
*sk
= sock
->sk
;
1036 struct sk_buff
*skb
;
1042 if (sock_flag(sk
, SOCK_ZAPPED
)) {
1044 return -EADDRNOTAVAIL
;
1047 skb
= skb_recv_datagram(sk
, flags
, &rc
);
1052 cb
= (struct qrtr_cb
*)skb
->cb
;
1055 if (copied
> size
) {
1057 msg
->msg_flags
|= MSG_TRUNC
;
1060 rc
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
1066 /* There is an anonymous 2-byte hole after sq_family,
1067 * make sure to clear it.
1069 memset(addr
, 0, sizeof(*addr
));
1071 addr
->sq_family
= AF_QIPCRTR
;
1072 addr
->sq_node
= cb
->src_node
;
1073 addr
->sq_port
= cb
->src_port
;
1074 msg
->msg_namelen
= sizeof(*addr
);
1079 qrtr_send_resume_tx(cb
);
1081 skb_free_datagram(sk
, skb
);
1087 static int qrtr_connect(struct socket
*sock
, struct sockaddr
*saddr
,
1090 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, saddr
);
1091 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
1092 struct sock
*sk
= sock
->sk
;
1095 if (len
< sizeof(*addr
) || addr
->sq_family
!= AF_QIPCRTR
)
1100 sk
->sk_state
= TCP_CLOSE
;
1101 sock
->state
= SS_UNCONNECTED
;
1103 rc
= qrtr_autobind(sock
);
1110 sock
->state
= SS_CONNECTED
;
1111 sk
->sk_state
= TCP_ESTABLISHED
;
1118 static int qrtr_getname(struct socket
*sock
, struct sockaddr
*saddr
,
1121 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
1122 struct sockaddr_qrtr qaddr
;
1123 struct sock
*sk
= sock
->sk
;
1127 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
1138 qaddr
.sq_family
= AF_QIPCRTR
;
1140 memcpy(saddr
, &qaddr
, sizeof(qaddr
));
1142 return sizeof(qaddr
);
1145 static int qrtr_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1147 void __user
*argp
= (void __user
*)arg
;
1148 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
1149 struct sock
*sk
= sock
->sk
;
1150 struct sockaddr_qrtr
*sq
;
1151 struct sk_buff
*skb
;
1160 len
= sk
->sk_sndbuf
- sk_wmem_alloc_get(sk
);
1163 rc
= put_user(len
, (int __user
*)argp
);
1166 skb
= skb_peek(&sk
->sk_receive_queue
);
1169 rc
= put_user(len
, (int __user
*)argp
);
1172 if (get_user_ifreq(&ifr
, NULL
, argp
)) {
1177 sq
= (struct sockaddr_qrtr
*)&ifr
.ifr_addr
;
1179 if (put_user_ifreq(&ifr
, argp
)) {
1187 case SIOCGIFDSTADDR
:
1188 case SIOCSIFDSTADDR
:
1189 case SIOCGIFBRDADDR
:
1190 case SIOCSIFBRDADDR
:
1191 case SIOCGIFNETMASK
:
1192 case SIOCSIFNETMASK
:
1205 static int qrtr_release(struct socket
*sock
)
1207 struct sock
*sk
= sock
->sk
;
1208 struct qrtr_sock
*ipc
;
1216 sk
->sk_shutdown
= SHUTDOWN_MASK
;
1217 if (!sock_flag(sk
, SOCK_DEAD
))
1218 sk
->sk_state_change(sk
);
1220 sock_set_flag(sk
, SOCK_DEAD
);
1224 if (!sock_flag(sk
, SOCK_ZAPPED
))
1225 qrtr_port_remove(ipc
);
1227 skb_queue_purge(&sk
->sk_receive_queue
);
1235 static const struct proto_ops qrtr_proto_ops
= {
1236 .owner
= THIS_MODULE
,
1237 .family
= AF_QIPCRTR
,
1239 .connect
= qrtr_connect
,
1240 .socketpair
= sock_no_socketpair
,
1241 .accept
= sock_no_accept
,
1242 .listen
= sock_no_listen
,
1243 .sendmsg
= qrtr_sendmsg
,
1244 .recvmsg
= qrtr_recvmsg
,
1245 .getname
= qrtr_getname
,
1246 .ioctl
= qrtr_ioctl
,
1247 .gettstamp
= sock_gettstamp
,
1248 .poll
= datagram_poll
,
1249 .shutdown
= sock_no_shutdown
,
1250 .release
= qrtr_release
,
1251 .mmap
= sock_no_mmap
,
1254 static struct proto qrtr_proto
= {
1256 .owner
= THIS_MODULE
,
1257 .obj_size
= sizeof(struct qrtr_sock
),
1260 static int qrtr_create(struct net
*net
, struct socket
*sock
,
1261 int protocol
, int kern
)
1263 struct qrtr_sock
*ipc
;
1266 if (sock
->type
!= SOCK_DGRAM
)
1269 sk
= sk_alloc(net
, AF_QIPCRTR
, GFP_KERNEL
, &qrtr_proto
, kern
);
1273 sock_set_flag(sk
, SOCK_ZAPPED
);
1275 sock_init_data(sock
, sk
);
1276 sock
->ops
= &qrtr_proto_ops
;
1279 ipc
->us
.sq_family
= AF_QIPCRTR
;
1280 ipc
->us
.sq_node
= qrtr_local_nid
;
1281 ipc
->us
.sq_port
= 0;
1286 static const struct net_proto_family qrtr_family
= {
1287 .owner
= THIS_MODULE
,
1288 .family
= AF_QIPCRTR
,
1289 .create
= qrtr_create
,
1292 static int __init
qrtr_proto_init(void)
1296 rc
= proto_register(&qrtr_proto
, 1);
1300 rc
= sock_register(&qrtr_family
);
1304 rc
= qrtr_ns_init();
1311 sock_unregister(qrtr_family
.family
);
1313 proto_unregister(&qrtr_proto
);
1316 postcore_initcall(qrtr_proto_init
);
1318 static void __exit
qrtr_proto_fini(void)
1321 sock_unregister(qrtr_family
.family
);
1322 proto_unregister(&qrtr_proto
);
1324 module_exit(qrtr_proto_fini
);
1326 MODULE_DESCRIPTION("Qualcomm IPC-router driver");
1327 MODULE_LICENSE("GPL v2");
1328 MODULE_ALIAS_NETPROTO(PF_QIPCRTR
);