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
25 * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1
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
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
70 #define QRTR_FLAGS_CONFIRM_RX BIT(0)
82 #define QRTR_HDR_MAX_SIZE max_t(size_t, sizeof(struct qrtr_hdr_v1), \
83 sizeof(struct qrtr_hdr_v2))
86 /* WARNING: sk must be the first member */
88 struct sockaddr_qrtr us
;
89 struct sockaddr_qrtr peer
;
92 static inline struct qrtr_sock
*qrtr_sk(struct sock
*sk
)
94 BUILD_BUG_ON(offsetof(struct qrtr_sock
, sk
) != 0);
95 return container_of(sk
, struct qrtr_sock
, sk
);
98 static unsigned int qrtr_local_nid
= 1;
101 static RADIX_TREE(qrtr_nodes
, GFP_ATOMIC
);
102 static DEFINE_SPINLOCK(qrtr_nodes_lock
);
104 static LIST_HEAD(qrtr_all_nodes
);
105 /* lock for qrtr_all_nodes and node reference */
106 static DEFINE_MUTEX(qrtr_node_lock
);
108 /* local port allocation management */
109 static DEFINE_IDR(qrtr_ports
);
110 static DEFINE_MUTEX(qrtr_port_lock
);
113 * struct qrtr_node - endpoint node
114 * @ep_lock: lock for endpoint management and callbacks
116 * @ref: reference count for node
118 * @qrtr_tx_flow: tree of qrtr_tx_flow, keyed by node << 32 | port
119 * @qrtr_tx_lock: lock for qrtr_tx_flow inserts
120 * @rx_queue: receive queue
121 * @item: list item for broadcast list
124 struct mutex ep_lock
;
125 struct qrtr_endpoint
*ep
;
129 struct radix_tree_root qrtr_tx_flow
;
130 struct mutex qrtr_tx_lock
; /* for qrtr_tx_flow */
132 struct sk_buff_head rx_queue
;
133 struct list_head item
;
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
142 struct qrtr_tx_flow
{
143 struct wait_queue_head resume_tx
;
148 #define QRTR_TX_FLOW_HIGH 10
149 #define QRTR_TX_FLOW_LOW 5
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
);
157 static struct qrtr_sock
*qrtr_port_lookup(int port
);
158 static void qrtr_port_put(struct qrtr_sock
*ipc
);
160 /* Release node resources and free the node.
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.
165 static void __qrtr_node_release(struct kref
*kref
)
167 struct qrtr_node
*node
= container_of(kref
, struct qrtr_node
, ref
);
168 struct radix_tree_iter iter
;
169 struct qrtr_tx_flow
*flow
;
173 spin_lock_irqsave(&qrtr_nodes_lock
, flags
);
174 /* If the node is a bridge for other nodes, there are possibly
175 * multiple entries pointing to our released node, delete them all.
177 radix_tree_for_each_slot(slot
, &qrtr_nodes
, &iter
, 0) {
179 radix_tree_iter_delete(&qrtr_nodes
, &iter
, slot
);
181 spin_unlock_irqrestore(&qrtr_nodes_lock
, flags
);
183 list_del(&node
->item
);
184 mutex_unlock(&qrtr_node_lock
);
186 skb_queue_purge(&node
->rx_queue
);
188 /* Free tx flow counters */
189 radix_tree_for_each_slot(slot
, &node
->qrtr_tx_flow
, &iter
, 0) {
191 radix_tree_iter_delete(&node
->qrtr_tx_flow
, &iter
, slot
);
197 /* Increment reference to node. */
198 static struct qrtr_node
*qrtr_node_acquire(struct qrtr_node
*node
)
201 kref_get(&node
->ref
);
205 /* Decrement reference to node and release as necessary. */
206 static void qrtr_node_release(struct qrtr_node
*node
)
210 kref_put_mutex(&node
->ref
, __qrtr_node_release
, &qrtr_node_lock
);
214 * qrtr_tx_resume() - reset flow control counter
215 * @node: qrtr_node that the QRTR_TYPE_RESUME_TX packet arrived on
216 * @skb: resume_tx packet
218 static void qrtr_tx_resume(struct qrtr_node
*node
, struct sk_buff
*skb
)
220 struct qrtr_ctrl_pkt
*pkt
= (struct qrtr_ctrl_pkt
*)skb
->data
;
221 u64 remote_node
= le32_to_cpu(pkt
->client
.node
);
222 u32 remote_port
= le32_to_cpu(pkt
->client
.port
);
223 struct qrtr_tx_flow
*flow
;
226 key
= remote_node
<< 32 | remote_port
;
229 flow
= radix_tree_lookup(&node
->qrtr_tx_flow
, key
);
232 spin_lock(&flow
->resume_tx
.lock
);
234 spin_unlock(&flow
->resume_tx
.lock
);
235 wake_up_interruptible_all(&flow
->resume_tx
);
242 * qrtr_tx_wait() - flow control for outgoing packets
243 * @node: qrtr_node that the packet is to be send to
244 * @dest_node: node id of the destination
245 * @dest_port: port number of the destination
246 * @type: type of message
248 * The flow control scheme is based around the low and high "watermarks". When
249 * the low watermark is passed the confirm_rx flag is set on the outgoing
250 * message, which will trigger the remote to send a control message of the type
251 * QRTR_TYPE_RESUME_TX to reset the counter. If the high watermark is hit
252 * further transmision should be paused.
254 * Return: 1 if confirm_rx should be set, 0 otherwise or errno failure
256 static int qrtr_tx_wait(struct qrtr_node
*node
, int dest_node
, int dest_port
,
259 unsigned long key
= (u64
)dest_node
<< 32 | dest_port
;
260 struct qrtr_tx_flow
*flow
;
264 /* Never set confirm_rx on non-data packets */
265 if (type
!= QRTR_TYPE_DATA
)
268 mutex_lock(&node
->qrtr_tx_lock
);
269 flow
= radix_tree_lookup(&node
->qrtr_tx_flow
, key
);
271 flow
= kzalloc(sizeof(*flow
), GFP_KERNEL
);
273 init_waitqueue_head(&flow
->resume_tx
);
274 radix_tree_insert(&node
->qrtr_tx_flow
, key
, flow
);
277 mutex_unlock(&node
->qrtr_tx_lock
);
279 /* Set confirm_rx if we where unable to find and allocate a flow */
283 spin_lock_irq(&flow
->resume_tx
.lock
);
284 ret
= wait_event_interruptible_locked_irq(flow
->resume_tx
,
285 flow
->pending
< QRTR_TX_FLOW_HIGH
||
290 } else if (!node
->ep
) {
292 } else if (flow
->tx_failed
) {
297 confirm_rx
= flow
->pending
== QRTR_TX_FLOW_LOW
;
299 spin_unlock_irq(&flow
->resume_tx
.lock
);
305 * qrtr_tx_flow_failed() - flag that tx of confirm_rx flagged messages failed
306 * @node: qrtr_node that the packet is to be send to
307 * @dest_node: node id of the destination
308 * @dest_port: port number of the destination
310 * Signal that the transmission of a message with confirm_rx flag failed. The
311 * flow's "pending" counter will keep incrementing towards QRTR_TX_FLOW_HIGH,
312 * at which point transmission would stall forever waiting for the resume TX
313 * message associated with the dropped confirm_rx message.
314 * Work around this by marking the flow as having a failed transmission and
315 * cause the next transmission attempt to be sent with the confirm_rx.
317 static void qrtr_tx_flow_failed(struct qrtr_node
*node
, int dest_node
,
320 unsigned long key
= (u64
)dest_node
<< 32 | dest_port
;
321 struct qrtr_tx_flow
*flow
;
324 flow
= radix_tree_lookup(&node
->qrtr_tx_flow
, key
);
327 spin_lock_irq(&flow
->resume_tx
.lock
);
329 spin_unlock_irq(&flow
->resume_tx
.lock
);
333 /* Pass an outgoing packet socket buffer to the endpoint driver. */
334 static int qrtr_node_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
,
335 int type
, struct sockaddr_qrtr
*from
,
336 struct sockaddr_qrtr
*to
)
338 struct qrtr_hdr_v1
*hdr
;
339 size_t len
= skb
->len
;
342 confirm_rx
= qrtr_tx_wait(node
, to
->sq_node
, to
->sq_port
, type
);
343 if (confirm_rx
< 0) {
348 hdr
= skb_push(skb
, sizeof(*hdr
));
349 hdr
->version
= cpu_to_le32(QRTR_PROTO_VER_1
);
350 hdr
->type
= cpu_to_le32(type
);
351 hdr
->src_node_id
= cpu_to_le32(from
->sq_node
);
352 hdr
->src_port_id
= cpu_to_le32(from
->sq_port
);
353 if (to
->sq_port
== QRTR_PORT_CTRL
) {
354 hdr
->dst_node_id
= cpu_to_le32(node
->nid
);
355 hdr
->dst_port_id
= cpu_to_le32(QRTR_PORT_CTRL
);
357 hdr
->dst_node_id
= cpu_to_le32(to
->sq_node
);
358 hdr
->dst_port_id
= cpu_to_le32(to
->sq_port
);
361 hdr
->size
= cpu_to_le32(len
);
362 hdr
->confirm_rx
= !!confirm_rx
;
364 rc
= skb_put_padto(skb
, ALIGN(len
, 4) + sizeof(*hdr
));
367 mutex_lock(&node
->ep_lock
);
370 rc
= node
->ep
->xmit(node
->ep
, skb
);
373 mutex_unlock(&node
->ep_lock
);
375 /* Need to ensure that a subsequent message carries the otherwise lost
376 * confirm_rx flag if we dropped this one */
377 if (rc
&& confirm_rx
)
378 qrtr_tx_flow_failed(node
, to
->sq_node
, to
->sq_port
);
383 /* Lookup node by id.
385 * callers must release with qrtr_node_release()
387 static struct qrtr_node
*qrtr_node_lookup(unsigned int nid
)
389 struct qrtr_node
*node
;
392 spin_lock_irqsave(&qrtr_nodes_lock
, flags
);
393 node
= radix_tree_lookup(&qrtr_nodes
, nid
);
394 node
= qrtr_node_acquire(node
);
395 spin_unlock_irqrestore(&qrtr_nodes_lock
, flags
);
400 /* Assign node id to node.
402 * This is mostly useful for automatic node id assignment, based on
403 * the source id in the incoming packet.
405 static void qrtr_node_assign(struct qrtr_node
*node
, unsigned int nid
)
409 if (nid
== QRTR_EP_NID_AUTO
)
412 spin_lock_irqsave(&qrtr_nodes_lock
, flags
);
413 radix_tree_insert(&qrtr_nodes
, nid
, node
);
414 if (node
->nid
== QRTR_EP_NID_AUTO
)
416 spin_unlock_irqrestore(&qrtr_nodes_lock
, flags
);
420 * qrtr_endpoint_post() - post incoming data
421 * @ep: endpoint handle
422 * @data: data pointer
423 * @len: size of data in bytes
425 * Return: 0 on success; negative error code on failure
427 int qrtr_endpoint_post(struct qrtr_endpoint
*ep
, const void *data
, size_t len
)
429 struct qrtr_node
*node
= ep
->node
;
430 const struct qrtr_hdr_v1
*v1
;
431 const struct qrtr_hdr_v2
*v2
;
432 struct qrtr_sock
*ipc
;
439 if (len
== 0 || len
& 3)
442 skb
= netdev_alloc_skb(NULL
, len
);
446 cb
= (struct qrtr_cb
*)skb
->cb
;
448 /* Version field in v1 is little endian, so this works for both cases */
452 case QRTR_PROTO_VER_1
:
453 if (len
< sizeof(*v1
))
456 hdrlen
= sizeof(*v1
);
458 cb
->type
= le32_to_cpu(v1
->type
);
459 cb
->src_node
= le32_to_cpu(v1
->src_node_id
);
460 cb
->src_port
= le32_to_cpu(v1
->src_port_id
);
461 cb
->confirm_rx
= !!v1
->confirm_rx
;
462 cb
->dst_node
= le32_to_cpu(v1
->dst_node_id
);
463 cb
->dst_port
= le32_to_cpu(v1
->dst_port_id
);
465 size
= le32_to_cpu(v1
->size
);
467 case QRTR_PROTO_VER_2
:
468 if (len
< sizeof(*v2
))
471 hdrlen
= sizeof(*v2
) + v2
->optlen
;
474 cb
->confirm_rx
= !!(v2
->flags
& QRTR_FLAGS_CONFIRM_RX
);
475 cb
->src_node
= le16_to_cpu(v2
->src_node_id
);
476 cb
->src_port
= le16_to_cpu(v2
->src_port_id
);
477 cb
->dst_node
= le16_to_cpu(v2
->dst_node_id
);
478 cb
->dst_port
= le16_to_cpu(v2
->dst_port_id
);
480 if (cb
->src_port
== (u16
)QRTR_PORT_CTRL
)
481 cb
->src_port
= QRTR_PORT_CTRL
;
482 if (cb
->dst_port
== (u16
)QRTR_PORT_CTRL
)
483 cb
->dst_port
= QRTR_PORT_CTRL
;
485 size
= le32_to_cpu(v2
->size
);
488 pr_err("qrtr: Invalid version %d\n", ver
);
492 if (len
!= ALIGN(size
, 4) + hdrlen
)
495 if (cb
->dst_port
!= QRTR_PORT_CTRL
&& cb
->type
!= QRTR_TYPE_DATA
&&
496 cb
->type
!= QRTR_TYPE_RESUME_TX
)
499 skb_put_data(skb
, data
+ hdrlen
, size
);
501 qrtr_node_assign(node
, cb
->src_node
);
503 if (cb
->type
== QRTR_TYPE_NEW_SERVER
) {
504 /* Remote node endpoint can bridge other distant nodes */
505 const struct qrtr_ctrl_pkt
*pkt
= data
+ hdrlen
;
507 qrtr_node_assign(node
, le32_to_cpu(pkt
->server
.node
));
510 if (cb
->type
== QRTR_TYPE_RESUME_TX
) {
511 qrtr_tx_resume(node
, skb
);
513 ipc
= qrtr_port_lookup(cb
->dst_port
);
517 if (sock_queue_rcv_skb(&ipc
->sk
, skb
))
530 EXPORT_SYMBOL_GPL(qrtr_endpoint_post
);
533 * qrtr_alloc_ctrl_packet() - allocate control packet skb
534 * @pkt: reference to qrtr_ctrl_pkt pointer
535 * @flags: the type of memory to allocate
537 * Returns newly allocated sk_buff, or NULL on failure
539 * This function allocates a sk_buff large enough to carry a qrtr_ctrl_pkt and
540 * on success returns a reference to the control packet in @pkt.
542 static struct sk_buff
*qrtr_alloc_ctrl_packet(struct qrtr_ctrl_pkt
**pkt
,
545 const int pkt_len
= sizeof(struct qrtr_ctrl_pkt
);
548 skb
= alloc_skb(QRTR_HDR_MAX_SIZE
+ pkt_len
, flags
);
552 skb_reserve(skb
, QRTR_HDR_MAX_SIZE
);
553 *pkt
= skb_put_zero(skb
, pkt_len
);
559 * qrtr_endpoint_register() - register a new endpoint
560 * @ep: endpoint to register
561 * @nid: desired node id; may be QRTR_EP_NID_AUTO for auto-assignment
562 * Return: 0 on success; negative error code on failure
564 * The specified endpoint must have the xmit function pointer set on call.
566 int qrtr_endpoint_register(struct qrtr_endpoint
*ep
, unsigned int nid
)
568 struct qrtr_node
*node
;
570 if (!ep
|| !ep
->xmit
)
573 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
577 kref_init(&node
->ref
);
578 mutex_init(&node
->ep_lock
);
579 skb_queue_head_init(&node
->rx_queue
);
580 node
->nid
= QRTR_EP_NID_AUTO
;
583 INIT_RADIX_TREE(&node
->qrtr_tx_flow
, GFP_KERNEL
);
584 mutex_init(&node
->qrtr_tx_lock
);
586 qrtr_node_assign(node
, nid
);
588 mutex_lock(&qrtr_node_lock
);
589 list_add(&node
->item
, &qrtr_all_nodes
);
590 mutex_unlock(&qrtr_node_lock
);
595 EXPORT_SYMBOL_GPL(qrtr_endpoint_register
);
598 * qrtr_endpoint_unregister - unregister endpoint
599 * @ep: endpoint to unregister
601 void qrtr_endpoint_unregister(struct qrtr_endpoint
*ep
)
603 struct qrtr_node
*node
= ep
->node
;
604 struct sockaddr_qrtr src
= {AF_QIPCRTR
, node
->nid
, QRTR_PORT_CTRL
};
605 struct sockaddr_qrtr dst
= {AF_QIPCRTR
, qrtr_local_nid
, QRTR_PORT_CTRL
};
606 struct radix_tree_iter iter
;
607 struct qrtr_ctrl_pkt
*pkt
;
608 struct qrtr_tx_flow
*flow
;
613 mutex_lock(&node
->ep_lock
);
615 mutex_unlock(&node
->ep_lock
);
617 /* Notify the local controller about the event */
618 spin_lock_irqsave(&qrtr_nodes_lock
, flags
);
619 radix_tree_for_each_slot(slot
, &qrtr_nodes
, &iter
, 0) {
622 src
.sq_node
= iter
.index
;
623 skb
= qrtr_alloc_ctrl_packet(&pkt
, GFP_ATOMIC
);
625 pkt
->cmd
= cpu_to_le32(QRTR_TYPE_BYE
);
626 qrtr_local_enqueue(NULL
, skb
, QRTR_TYPE_BYE
, &src
, &dst
);
629 spin_unlock_irqrestore(&qrtr_nodes_lock
, flags
);
631 /* Wake up any transmitters waiting for resume-tx from the node */
632 mutex_lock(&node
->qrtr_tx_lock
);
633 radix_tree_for_each_slot(slot
, &node
->qrtr_tx_flow
, &iter
, 0) {
635 wake_up_interruptible_all(&flow
->resume_tx
);
637 mutex_unlock(&node
->qrtr_tx_lock
);
639 qrtr_node_release(node
);
642 EXPORT_SYMBOL_GPL(qrtr_endpoint_unregister
);
644 /* Lookup socket by port.
646 * Callers must release with qrtr_port_put()
648 static struct qrtr_sock
*qrtr_port_lookup(int port
)
650 struct qrtr_sock
*ipc
;
652 if (port
== QRTR_PORT_CTRL
)
656 ipc
= idr_find(&qrtr_ports
, port
);
664 /* Release acquired socket. */
665 static void qrtr_port_put(struct qrtr_sock
*ipc
)
670 /* Remove port assignment. */
671 static void qrtr_port_remove(struct qrtr_sock
*ipc
)
673 struct qrtr_ctrl_pkt
*pkt
;
675 int port
= ipc
->us
.sq_port
;
676 struct sockaddr_qrtr to
;
678 to
.sq_family
= AF_QIPCRTR
;
679 to
.sq_node
= QRTR_NODE_BCAST
;
680 to
.sq_port
= QRTR_PORT_CTRL
;
682 skb
= qrtr_alloc_ctrl_packet(&pkt
, GFP_KERNEL
);
684 pkt
->cmd
= cpu_to_le32(QRTR_TYPE_DEL_CLIENT
);
685 pkt
->client
.node
= cpu_to_le32(ipc
->us
.sq_node
);
686 pkt
->client
.port
= cpu_to_le32(ipc
->us
.sq_port
);
688 skb_set_owner_w(skb
, &ipc
->sk
);
689 qrtr_bcast_enqueue(NULL
, skb
, QRTR_TYPE_DEL_CLIENT
, &ipc
->us
,
693 if (port
== QRTR_PORT_CTRL
)
696 __sock_put(&ipc
->sk
);
698 mutex_lock(&qrtr_port_lock
);
699 idr_remove(&qrtr_ports
, port
);
700 mutex_unlock(&qrtr_port_lock
);
702 /* Ensure that if qrtr_port_lookup() did enter the RCU read section we
703 * wait for it to up increment the refcount */
707 /* Assign port number to socket.
709 * Specify port in the integer pointed to by port, and it will be adjusted
710 * on return as necesssary.
713 * 0: Assign ephemeral port in [QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET]
714 * <QRTR_MIN_EPH_SOCKET: Specified; requires CAP_NET_ADMIN
715 * >QRTR_MIN_EPH_SOCKET: Specified; available to all
717 static int qrtr_port_assign(struct qrtr_sock
*ipc
, int *port
)
722 mutex_lock(&qrtr_port_lock
);
724 min_port
= QRTR_MIN_EPH_SOCKET
;
725 rc
= idr_alloc_u32(&qrtr_ports
, ipc
, &min_port
, QRTR_MAX_EPH_SOCKET
, GFP_ATOMIC
);
728 } else if (*port
< QRTR_MIN_EPH_SOCKET
&& !capable(CAP_NET_ADMIN
)) {
730 } else if (*port
== QRTR_PORT_CTRL
) {
732 rc
= idr_alloc_u32(&qrtr_ports
, ipc
, &min_port
, 0, GFP_ATOMIC
);
735 rc
= idr_alloc_u32(&qrtr_ports
, ipc
, &min_port
, *port
, GFP_ATOMIC
);
739 mutex_unlock(&qrtr_port_lock
);
751 /* Reset all non-control ports */
752 static void qrtr_reset_ports(void)
754 struct qrtr_sock
*ipc
;
757 mutex_lock(&qrtr_port_lock
);
758 idr_for_each_entry(&qrtr_ports
, ipc
, id
) {
759 /* Don't reset control port */
764 ipc
->sk
.sk_err
= ENETRESET
;
765 ipc
->sk
.sk_error_report(&ipc
->sk
);
768 mutex_unlock(&qrtr_port_lock
);
771 /* Bind socket to address.
773 * Socket should be locked upon call.
775 static int __qrtr_bind(struct socket
*sock
,
776 const struct sockaddr_qrtr
*addr
, int zapped
)
778 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
779 struct sock
*sk
= sock
->sk
;
784 if (!zapped
&& addr
->sq_port
== ipc
->us
.sq_port
)
787 port
= addr
->sq_port
;
788 rc
= qrtr_port_assign(ipc
, &port
);
792 /* unbind previous, if any */
794 qrtr_port_remove(ipc
);
795 ipc
->us
.sq_port
= port
;
797 sock_reset_flag(sk
, SOCK_ZAPPED
);
799 /* Notify all open ports about the new controller */
800 if (port
== QRTR_PORT_CTRL
)
806 /* Auto bind to an ephemeral port. */
807 static int qrtr_autobind(struct socket
*sock
)
809 struct sock
*sk
= sock
->sk
;
810 struct sockaddr_qrtr addr
;
812 if (!sock_flag(sk
, SOCK_ZAPPED
))
815 addr
.sq_family
= AF_QIPCRTR
;
816 addr
.sq_node
= qrtr_local_nid
;
819 return __qrtr_bind(sock
, &addr
, 1);
822 /* Bind socket to specified sockaddr. */
823 static int qrtr_bind(struct socket
*sock
, struct sockaddr
*saddr
, int len
)
825 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, saddr
);
826 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
827 struct sock
*sk
= sock
->sk
;
830 if (len
< sizeof(*addr
) || addr
->sq_family
!= AF_QIPCRTR
)
833 if (addr
->sq_node
!= ipc
->us
.sq_node
)
837 rc
= __qrtr_bind(sock
, addr
, sock_flag(sk
, SOCK_ZAPPED
));
843 /* Queue packet to local peer socket. */
844 static int qrtr_local_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
,
845 int type
, struct sockaddr_qrtr
*from
,
846 struct sockaddr_qrtr
*to
)
848 struct qrtr_sock
*ipc
;
851 ipc
= qrtr_port_lookup(to
->sq_port
);
852 if (!ipc
|| &ipc
->sk
== skb
->sk
) { /* do not send to self */
857 cb
= (struct qrtr_cb
*)skb
->cb
;
858 cb
->src_node
= from
->sq_node
;
859 cb
->src_port
= from
->sq_port
;
861 if (sock_queue_rcv_skb(&ipc
->sk
, skb
)) {
872 /* Queue packet for broadcast. */
873 static int qrtr_bcast_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
,
874 int type
, struct sockaddr_qrtr
*from
,
875 struct sockaddr_qrtr
*to
)
877 struct sk_buff
*skbn
;
879 mutex_lock(&qrtr_node_lock
);
880 list_for_each_entry(node
, &qrtr_all_nodes
, item
) {
881 skbn
= skb_clone(skb
, GFP_KERNEL
);
884 skb_set_owner_w(skbn
, skb
->sk
);
885 qrtr_node_enqueue(node
, skbn
, type
, from
, to
);
887 mutex_unlock(&qrtr_node_lock
);
889 qrtr_local_enqueue(NULL
, skb
, type
, from
, to
);
894 static int qrtr_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
)
896 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, msg
->msg_name
);
897 int (*enqueue_fn
)(struct qrtr_node
*, struct sk_buff
*, int,
898 struct sockaddr_qrtr
*, struct sockaddr_qrtr
*);
899 __le32 qrtr_type
= cpu_to_le32(QRTR_TYPE_DATA
);
900 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
901 struct sock
*sk
= sock
->sk
;
902 struct qrtr_node
*node
;
908 if (msg
->msg_flags
& ~(MSG_DONTWAIT
))
917 if (msg
->msg_namelen
< sizeof(*addr
)) {
922 if (addr
->sq_family
!= AF_QIPCRTR
) {
927 rc
= qrtr_autobind(sock
);
932 } else if (sk
->sk_state
== TCP_ESTABLISHED
) {
940 if (addr
->sq_node
== QRTR_NODE_BCAST
) {
941 if (addr
->sq_port
!= QRTR_PORT_CTRL
&&
942 qrtr_local_nid
!= QRTR_NODE_BCAST
) {
946 enqueue_fn
= qrtr_bcast_enqueue
;
947 } else if (addr
->sq_node
== ipc
->us
.sq_node
) {
948 enqueue_fn
= qrtr_local_enqueue
;
950 node
= qrtr_node_lookup(addr
->sq_node
);
955 enqueue_fn
= qrtr_node_enqueue
;
958 plen
= (len
+ 3) & ~3;
959 skb
= sock_alloc_send_skb(sk
, plen
+ QRTR_HDR_MAX_SIZE
,
960 msg
->msg_flags
& MSG_DONTWAIT
, &rc
);
964 skb_reserve(skb
, QRTR_HDR_MAX_SIZE
);
966 rc
= memcpy_from_msg(skb_put(skb
, len
), msg
, len
);
972 if (ipc
->us
.sq_port
== QRTR_PORT_CTRL
) {
979 /* control messages already require the type as 'command' */
980 skb_copy_bits(skb
, 0, &qrtr_type
, 4);
983 type
= le32_to_cpu(qrtr_type
);
984 rc
= enqueue_fn(node
, skb
, type
, &ipc
->us
, addr
);
989 qrtr_node_release(node
);
995 static int qrtr_send_resume_tx(struct qrtr_cb
*cb
)
997 struct sockaddr_qrtr remote
= { AF_QIPCRTR
, cb
->src_node
, cb
->src_port
};
998 struct sockaddr_qrtr local
= { AF_QIPCRTR
, cb
->dst_node
, cb
->dst_port
};
999 struct qrtr_ctrl_pkt
*pkt
;
1000 struct qrtr_node
*node
;
1001 struct sk_buff
*skb
;
1004 node
= qrtr_node_lookup(remote
.sq_node
);
1008 skb
= qrtr_alloc_ctrl_packet(&pkt
, GFP_KERNEL
);
1012 pkt
->cmd
= cpu_to_le32(QRTR_TYPE_RESUME_TX
);
1013 pkt
->client
.node
= cpu_to_le32(cb
->dst_node
);
1014 pkt
->client
.port
= cpu_to_le32(cb
->dst_port
);
1016 ret
= qrtr_node_enqueue(node
, skb
, QRTR_TYPE_RESUME_TX
, &local
, &remote
);
1018 qrtr_node_release(node
);
1023 static int qrtr_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
1024 size_t size
, int flags
)
1026 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, msg
->msg_name
);
1027 struct sock
*sk
= sock
->sk
;
1028 struct sk_buff
*skb
;
1034 if (sock_flag(sk
, SOCK_ZAPPED
)) {
1036 return -EADDRNOTAVAIL
;
1039 skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
,
1040 flags
& MSG_DONTWAIT
, &rc
);
1045 cb
= (struct qrtr_cb
*)skb
->cb
;
1048 if (copied
> size
) {
1050 msg
->msg_flags
|= MSG_TRUNC
;
1053 rc
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
1059 addr
->sq_family
= AF_QIPCRTR
;
1060 addr
->sq_node
= cb
->src_node
;
1061 addr
->sq_port
= cb
->src_port
;
1062 msg
->msg_namelen
= sizeof(*addr
);
1067 qrtr_send_resume_tx(cb
);
1069 skb_free_datagram(sk
, skb
);
1075 static int qrtr_connect(struct socket
*sock
, struct sockaddr
*saddr
,
1078 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, saddr
);
1079 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
1080 struct sock
*sk
= sock
->sk
;
1083 if (len
< sizeof(*addr
) || addr
->sq_family
!= AF_QIPCRTR
)
1088 sk
->sk_state
= TCP_CLOSE
;
1089 sock
->state
= SS_UNCONNECTED
;
1091 rc
= qrtr_autobind(sock
);
1098 sock
->state
= SS_CONNECTED
;
1099 sk
->sk_state
= TCP_ESTABLISHED
;
1106 static int qrtr_getname(struct socket
*sock
, struct sockaddr
*saddr
,
1109 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
1110 struct sockaddr_qrtr qaddr
;
1111 struct sock
*sk
= sock
->sk
;
1115 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
1126 qaddr
.sq_family
= AF_QIPCRTR
;
1128 memcpy(saddr
, &qaddr
, sizeof(qaddr
));
1130 return sizeof(qaddr
);
1133 static int qrtr_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1135 void __user
*argp
= (void __user
*)arg
;
1136 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
1137 struct sock
*sk
= sock
->sk
;
1138 struct sockaddr_qrtr
*sq
;
1139 struct sk_buff
*skb
;
1148 len
= sk
->sk_sndbuf
- sk_wmem_alloc_get(sk
);
1151 rc
= put_user(len
, (int __user
*)argp
);
1154 skb
= skb_peek(&sk
->sk_receive_queue
);
1157 rc
= put_user(len
, (int __user
*)argp
);
1160 if (copy_from_user(&ifr
, argp
, sizeof(ifr
))) {
1165 sq
= (struct sockaddr_qrtr
*)&ifr
.ifr_addr
;
1167 if (copy_to_user(argp
, &ifr
, sizeof(ifr
))) {
1175 case SIOCGIFDSTADDR
:
1176 case SIOCSIFDSTADDR
:
1177 case SIOCGIFBRDADDR
:
1178 case SIOCSIFBRDADDR
:
1179 case SIOCGIFNETMASK
:
1180 case SIOCSIFNETMASK
:
1193 static int qrtr_release(struct socket
*sock
)
1195 struct sock
*sk
= sock
->sk
;
1196 struct qrtr_sock
*ipc
;
1204 sk
->sk_shutdown
= SHUTDOWN_MASK
;
1205 if (!sock_flag(sk
, SOCK_DEAD
))
1206 sk
->sk_state_change(sk
);
1208 sock_set_flag(sk
, SOCK_DEAD
);
1212 if (!sock_flag(sk
, SOCK_ZAPPED
))
1213 qrtr_port_remove(ipc
);
1215 skb_queue_purge(&sk
->sk_receive_queue
);
1223 static const struct proto_ops qrtr_proto_ops
= {
1224 .owner
= THIS_MODULE
,
1225 .family
= AF_QIPCRTR
,
1227 .connect
= qrtr_connect
,
1228 .socketpair
= sock_no_socketpair
,
1229 .accept
= sock_no_accept
,
1230 .listen
= sock_no_listen
,
1231 .sendmsg
= qrtr_sendmsg
,
1232 .recvmsg
= qrtr_recvmsg
,
1233 .getname
= qrtr_getname
,
1234 .ioctl
= qrtr_ioctl
,
1235 .gettstamp
= sock_gettstamp
,
1236 .poll
= datagram_poll
,
1237 .shutdown
= sock_no_shutdown
,
1238 .release
= qrtr_release
,
1239 .mmap
= sock_no_mmap
,
1240 .sendpage
= sock_no_sendpage
,
1243 static struct proto qrtr_proto
= {
1245 .owner
= THIS_MODULE
,
1246 .obj_size
= sizeof(struct qrtr_sock
),
1249 static int qrtr_create(struct net
*net
, struct socket
*sock
,
1250 int protocol
, int kern
)
1252 struct qrtr_sock
*ipc
;
1255 if (sock
->type
!= SOCK_DGRAM
)
1258 sk
= sk_alloc(net
, AF_QIPCRTR
, GFP_KERNEL
, &qrtr_proto
, kern
);
1262 sock_set_flag(sk
, SOCK_ZAPPED
);
1264 sock_init_data(sock
, sk
);
1265 sock
->ops
= &qrtr_proto_ops
;
1268 ipc
->us
.sq_family
= AF_QIPCRTR
;
1269 ipc
->us
.sq_node
= qrtr_local_nid
;
1270 ipc
->us
.sq_port
= 0;
1275 static const struct net_proto_family qrtr_family
= {
1276 .owner
= THIS_MODULE
,
1277 .family
= AF_QIPCRTR
,
1278 .create
= qrtr_create
,
1281 static int __init
qrtr_proto_init(void)
1285 rc
= proto_register(&qrtr_proto
, 1);
1289 rc
= sock_register(&qrtr_family
);
1293 rc
= qrtr_ns_init();
1300 sock_unregister(qrtr_family
.family
);
1302 proto_unregister(&qrtr_proto
);
1305 postcore_initcall(qrtr_proto_init
);
1307 static void __exit
qrtr_proto_fini(void)
1310 sock_unregister(qrtr_family
.family
);
1311 proto_unregister(&qrtr_proto
);
1313 module_exit(qrtr_proto_fini
);
1315 MODULE_DESCRIPTION("Qualcomm IPC-router driver");
1316 MODULE_LICENSE("GPL v2");
1317 MODULE_ALIAS_NETPROTO(PF_QIPCRTR
);