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
;
172 spin_lock_irqsave(&qrtr_nodes_lock
, flags
);
173 if (node
->nid
!= QRTR_EP_NID_AUTO
)
174 radix_tree_delete(&qrtr_nodes
, node
->nid
);
175 spin_unlock_irqrestore(&qrtr_nodes_lock
, flags
);
177 list_del(&node
->item
);
178 mutex_unlock(&qrtr_node_lock
);
180 skb_queue_purge(&node
->rx_queue
);
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
);
190 /* Increment reference to node. */
191 static struct qrtr_node
*qrtr_node_acquire(struct qrtr_node
*node
)
194 kref_get(&node
->ref
);
198 /* Decrement reference to node and release as necessary. */
199 static void qrtr_node_release(struct qrtr_node
*node
)
203 kref_put_mutex(&node
->ref
, __qrtr_node_release
, &qrtr_node_lock
);
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
211 static void qrtr_tx_resume(struct qrtr_node
*node
, struct sk_buff
*skb
)
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
;
219 key
= remote_node
<< 32 | remote_port
;
222 flow
= radix_tree_lookup(&node
->qrtr_tx_flow
, key
);
225 spin_lock(&flow
->resume_tx
.lock
);
227 spin_unlock(&flow
->resume_tx
.lock
);
228 wake_up_interruptible_all(&flow
->resume_tx
);
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
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.
247 * Return: 1 if confirm_rx should be set, 0 otherwise or errno failure
249 static int qrtr_tx_wait(struct qrtr_node
*node
, int dest_node
, int dest_port
,
252 unsigned long key
= (u64
)dest_node
<< 32 | dest_port
;
253 struct qrtr_tx_flow
*flow
;
257 /* Never set confirm_rx on non-data packets */
258 if (type
!= QRTR_TYPE_DATA
)
261 mutex_lock(&node
->qrtr_tx_lock
);
262 flow
= radix_tree_lookup(&node
->qrtr_tx_flow
, key
);
264 flow
= kzalloc(sizeof(*flow
), GFP_KERNEL
);
266 init_waitqueue_head(&flow
->resume_tx
);
267 radix_tree_insert(&node
->qrtr_tx_flow
, key
, flow
);
270 mutex_unlock(&node
->qrtr_tx_lock
);
272 /* Set confirm_rx if we where unable to find and allocate a flow */
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
||
283 } else if (!node
->ep
) {
285 } else if (flow
->tx_failed
) {
290 confirm_rx
= flow
->pending
== QRTR_TX_FLOW_LOW
;
292 spin_unlock_irq(&flow
->resume_tx
.lock
);
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
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.
310 static void qrtr_tx_flow_failed(struct qrtr_node
*node
, int dest_node
,
313 unsigned long key
= (u64
)dest_node
<< 32 | dest_port
;
314 struct qrtr_tx_flow
*flow
;
317 flow
= radix_tree_lookup(&node
->qrtr_tx_flow
, key
);
320 spin_lock_irq(&flow
->resume_tx
.lock
);
322 spin_unlock_irq(&flow
->resume_tx
.lock
);
326 /* Pass an outgoing packet socket buffer to the endpoint driver. */
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
)
331 struct qrtr_hdr_v1
*hdr
;
332 size_t len
= skb
->len
;
336 confirm_rx
= qrtr_tx_wait(node
, to
->sq_node
, to
->sq_port
, type
);
337 if (confirm_rx
< 0) {
342 hdr
= skb_push(skb
, sizeof(*hdr
));
343 hdr
->version
= cpu_to_le32(QRTR_PROTO_VER_1
);
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
);
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
);
351 hdr
->dst_node_id
= cpu_to_le32(to
->sq_node
);
352 hdr
->dst_port_id
= cpu_to_le32(to
->sq_port
);
355 hdr
->size
= cpu_to_le32(len
);
356 hdr
->confirm_rx
= !!confirm_rx
;
358 skb_put_padto(skb
, ALIGN(len
, 4) + sizeof(*hdr
));
360 mutex_lock(&node
->ep_lock
);
362 rc
= node
->ep
->xmit(node
->ep
, skb
);
365 mutex_unlock(&node
->ep_lock
);
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
);
375 /* Lookup node by id.
377 * callers must release with qrtr_node_release()
379 static struct qrtr_node
*qrtr_node_lookup(unsigned int nid
)
381 struct qrtr_node
*node
;
384 spin_lock_irqsave(&qrtr_nodes_lock
, flags
);
385 node
= radix_tree_lookup(&qrtr_nodes
, nid
);
386 node
= qrtr_node_acquire(node
);
387 spin_unlock_irqrestore(&qrtr_nodes_lock
, flags
);
392 /* Assign node id to node.
394 * This is mostly useful for automatic node id assignment, based on
395 * the source id in the incoming packet.
397 static void qrtr_node_assign(struct qrtr_node
*node
, unsigned int nid
)
401 if (node
->nid
!= QRTR_EP_NID_AUTO
|| nid
== QRTR_EP_NID_AUTO
)
404 spin_lock_irqsave(&qrtr_nodes_lock
, flags
);
405 radix_tree_insert(&qrtr_nodes
, nid
, node
);
407 spin_unlock_irqrestore(&qrtr_nodes_lock
, flags
);
411 * qrtr_endpoint_post() - post incoming data
412 * @ep: endpoint handle
413 * @data: data pointer
414 * @len: size of data in bytes
416 * Return: 0 on success; negative error code on failure
418 int qrtr_endpoint_post(struct qrtr_endpoint
*ep
, const void *data
, size_t len
)
420 struct qrtr_node
*node
= ep
->node
;
421 const struct qrtr_hdr_v1
*v1
;
422 const struct qrtr_hdr_v2
*v2
;
423 struct qrtr_sock
*ipc
;
433 skb
= netdev_alloc_skb(NULL
, len
);
437 cb
= (struct qrtr_cb
*)skb
->cb
;
439 /* Version field in v1 is little endian, so this works for both cases */
443 case QRTR_PROTO_VER_1
:
445 hdrlen
= sizeof(*v1
);
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
);
454 size
= le32_to_cpu(v1
->size
);
456 case QRTR_PROTO_VER_2
:
458 hdrlen
= sizeof(*v2
) + v2
->optlen
;
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
);
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
;
472 size
= le32_to_cpu(v2
->size
);
475 pr_err("qrtr: Invalid version %d\n", ver
);
479 if (len
!= ALIGN(size
, 4) + hdrlen
)
482 if (cb
->dst_port
!= QRTR_PORT_CTRL
&& cb
->type
!= QRTR_TYPE_DATA
&&
483 cb
->type
!= QRTR_TYPE_RESUME_TX
)
486 skb_put_data(skb
, data
+ hdrlen
, size
);
488 qrtr_node_assign(node
, cb
->src_node
);
490 if (cb
->type
== QRTR_TYPE_RESUME_TX
) {
491 qrtr_tx_resume(node
, skb
);
493 ipc
= qrtr_port_lookup(cb
->dst_port
);
497 if (sock_queue_rcv_skb(&ipc
->sk
, skb
))
510 EXPORT_SYMBOL_GPL(qrtr_endpoint_post
);
513 * qrtr_alloc_ctrl_packet() - allocate control packet skb
514 * @pkt: reference to qrtr_ctrl_pkt pointer
516 * Returns newly allocated sk_buff, or NULL on failure
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.
521 static struct sk_buff
*qrtr_alloc_ctrl_packet(struct qrtr_ctrl_pkt
**pkt
)
523 const int pkt_len
= sizeof(struct qrtr_ctrl_pkt
);
526 skb
= alloc_skb(QRTR_HDR_MAX_SIZE
+ pkt_len
, GFP_KERNEL
);
530 skb_reserve(skb
, QRTR_HDR_MAX_SIZE
);
531 *pkt
= skb_put_zero(skb
, pkt_len
);
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
542 * The specified endpoint must have the xmit function pointer set on call.
544 int qrtr_endpoint_register(struct qrtr_endpoint
*ep
, unsigned int nid
)
546 struct qrtr_node
*node
;
548 if (!ep
|| !ep
->xmit
)
551 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
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
;
561 INIT_RADIX_TREE(&node
->qrtr_tx_flow
, GFP_KERNEL
);
562 mutex_init(&node
->qrtr_tx_lock
);
564 qrtr_node_assign(node
, nid
);
566 mutex_lock(&qrtr_node_lock
);
567 list_add(&node
->item
, &qrtr_all_nodes
);
568 mutex_unlock(&qrtr_node_lock
);
573 EXPORT_SYMBOL_GPL(qrtr_endpoint_register
);
576 * qrtr_endpoint_unregister - unregister endpoint
577 * @ep: endpoint to unregister
579 void qrtr_endpoint_unregister(struct qrtr_endpoint
*ep
)
581 struct qrtr_node
*node
= ep
->node
;
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
};
584 struct radix_tree_iter iter
;
585 struct qrtr_ctrl_pkt
*pkt
;
586 struct qrtr_tx_flow
*flow
;
590 mutex_lock(&node
->ep_lock
);
592 mutex_unlock(&node
->ep_lock
);
594 /* Notify the local controller about the event */
595 skb
= qrtr_alloc_ctrl_packet(&pkt
);
597 pkt
->cmd
= cpu_to_le32(QRTR_TYPE_BYE
);
598 qrtr_local_enqueue(NULL
, skb
, QRTR_TYPE_BYE
, &src
, &dst
);
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) {
605 wake_up_interruptible_all(&flow
->resume_tx
);
607 mutex_unlock(&node
->qrtr_tx_lock
);
609 qrtr_node_release(node
);
612 EXPORT_SYMBOL_GPL(qrtr_endpoint_unregister
);
614 /* Lookup socket by port.
616 * Callers must release with qrtr_port_put()
618 static struct qrtr_sock
*qrtr_port_lookup(int port
)
620 struct qrtr_sock
*ipc
;
622 if (port
== QRTR_PORT_CTRL
)
626 ipc
= idr_find(&qrtr_ports
, port
);
634 /* Release acquired socket. */
635 static void qrtr_port_put(struct qrtr_sock
*ipc
)
640 /* Remove port assignment. */
641 static void qrtr_port_remove(struct qrtr_sock
*ipc
)
643 struct qrtr_ctrl_pkt
*pkt
;
645 int port
= ipc
->us
.sq_port
;
646 struct sockaddr_qrtr to
;
648 to
.sq_family
= AF_QIPCRTR
;
649 to
.sq_node
= QRTR_NODE_BCAST
;
650 to
.sq_port
= QRTR_PORT_CTRL
;
652 skb
= qrtr_alloc_ctrl_packet(&pkt
);
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
);
658 skb_set_owner_w(skb
, &ipc
->sk
);
659 qrtr_bcast_enqueue(NULL
, skb
, QRTR_TYPE_DEL_CLIENT
, &ipc
->us
,
663 if (port
== QRTR_PORT_CTRL
)
666 __sock_put(&ipc
->sk
);
668 mutex_lock(&qrtr_port_lock
);
669 idr_remove(&qrtr_ports
, port
);
670 mutex_unlock(&qrtr_port_lock
);
672 /* Ensure that if qrtr_port_lookup() did enter the RCU read section we
673 * wait for it to up increment the refcount */
677 /* Assign port number to socket.
679 * Specify port in the integer pointed to by port, and it will be adjusted
680 * on return as necesssary.
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
687 static int qrtr_port_assign(struct qrtr_sock
*ipc
, int *port
)
691 mutex_lock(&qrtr_port_lock
);
693 rc
= idr_alloc(&qrtr_ports
, ipc
,
694 QRTR_MIN_EPH_SOCKET
, QRTR_MAX_EPH_SOCKET
+ 1,
698 } else if (*port
< QRTR_MIN_EPH_SOCKET
&& !capable(CAP_NET_ADMIN
)) {
700 } else if (*port
== QRTR_PORT_CTRL
) {
701 rc
= idr_alloc(&qrtr_ports
, ipc
, 0, 1, GFP_ATOMIC
);
703 rc
= idr_alloc(&qrtr_ports
, ipc
, *port
, *port
+ 1, GFP_ATOMIC
);
707 mutex_unlock(&qrtr_port_lock
);
719 /* Reset all non-control ports */
720 static void qrtr_reset_ports(void)
722 struct qrtr_sock
*ipc
;
725 mutex_lock(&qrtr_port_lock
);
726 idr_for_each_entry(&qrtr_ports
, ipc
, id
) {
727 /* Don't reset control port */
732 ipc
->sk
.sk_err
= ENETRESET
;
733 ipc
->sk
.sk_error_report(&ipc
->sk
);
736 mutex_unlock(&qrtr_port_lock
);
739 /* Bind socket to address.
741 * Socket should be locked upon call.
743 static int __qrtr_bind(struct socket
*sock
,
744 const struct sockaddr_qrtr
*addr
, int zapped
)
746 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
747 struct sock
*sk
= sock
->sk
;
752 if (!zapped
&& addr
->sq_port
== ipc
->us
.sq_port
)
755 port
= addr
->sq_port
;
756 rc
= qrtr_port_assign(ipc
, &port
);
760 /* unbind previous, if any */
762 qrtr_port_remove(ipc
);
763 ipc
->us
.sq_port
= port
;
765 sock_reset_flag(sk
, SOCK_ZAPPED
);
767 /* Notify all open ports about the new controller */
768 if (port
== QRTR_PORT_CTRL
)
774 /* Auto bind to an ephemeral port. */
775 static int qrtr_autobind(struct socket
*sock
)
777 struct sock
*sk
= sock
->sk
;
778 struct sockaddr_qrtr addr
;
780 if (!sock_flag(sk
, SOCK_ZAPPED
))
783 addr
.sq_family
= AF_QIPCRTR
;
784 addr
.sq_node
= qrtr_local_nid
;
787 return __qrtr_bind(sock
, &addr
, 1);
790 /* Bind socket to specified sockaddr. */
791 static int qrtr_bind(struct socket
*sock
, struct sockaddr
*saddr
, int len
)
793 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, saddr
);
794 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
795 struct sock
*sk
= sock
->sk
;
798 if (len
< sizeof(*addr
) || addr
->sq_family
!= AF_QIPCRTR
)
801 if (addr
->sq_node
!= ipc
->us
.sq_node
)
805 rc
= __qrtr_bind(sock
, addr
, sock_flag(sk
, SOCK_ZAPPED
));
811 /* Queue packet to local peer socket. */
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
)
816 struct qrtr_sock
*ipc
;
819 ipc
= qrtr_port_lookup(to
->sq_port
);
820 if (!ipc
|| &ipc
->sk
== skb
->sk
) { /* do not send to self */
825 cb
= (struct qrtr_cb
*)skb
->cb
;
826 cb
->src_node
= from
->sq_node
;
827 cb
->src_port
= from
->sq_port
;
829 if (sock_queue_rcv_skb(&ipc
->sk
, skb
)) {
840 /* Queue packet for broadcast. */
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
)
845 struct sk_buff
*skbn
;
847 mutex_lock(&qrtr_node_lock
);
848 list_for_each_entry(node
, &qrtr_all_nodes
, item
) {
849 skbn
= skb_clone(skb
, GFP_KERNEL
);
852 skb_set_owner_w(skbn
, skb
->sk
);
853 qrtr_node_enqueue(node
, skbn
, type
, from
, to
);
855 mutex_unlock(&qrtr_node_lock
);
857 qrtr_local_enqueue(NULL
, skb
, type
, from
, to
);
862 static int qrtr_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
)
864 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, msg
->msg_name
);
865 int (*enqueue_fn
)(struct qrtr_node
*, struct sk_buff
*, int,
866 struct sockaddr_qrtr
*, struct sockaddr_qrtr
*);
867 __le32 qrtr_type
= cpu_to_le32(QRTR_TYPE_DATA
);
868 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
869 struct sock
*sk
= sock
->sk
;
870 struct qrtr_node
*node
;
876 if (msg
->msg_flags
& ~(MSG_DONTWAIT
))
885 if (msg
->msg_namelen
< sizeof(*addr
)) {
890 if (addr
->sq_family
!= AF_QIPCRTR
) {
895 rc
= qrtr_autobind(sock
);
900 } else if (sk
->sk_state
== TCP_ESTABLISHED
) {
908 if (addr
->sq_node
== QRTR_NODE_BCAST
) {
909 if (addr
->sq_port
!= QRTR_PORT_CTRL
&&
910 qrtr_local_nid
!= QRTR_NODE_BCAST
) {
914 enqueue_fn
= qrtr_bcast_enqueue
;
915 } else if (addr
->sq_node
== ipc
->us
.sq_node
) {
916 enqueue_fn
= qrtr_local_enqueue
;
918 node
= qrtr_node_lookup(addr
->sq_node
);
923 enqueue_fn
= qrtr_node_enqueue
;
926 plen
= (len
+ 3) & ~3;
927 skb
= sock_alloc_send_skb(sk
, plen
+ QRTR_HDR_MAX_SIZE
,
928 msg
->msg_flags
& MSG_DONTWAIT
, &rc
);
932 skb_reserve(skb
, QRTR_HDR_MAX_SIZE
);
934 rc
= memcpy_from_msg(skb_put(skb
, len
), msg
, len
);
940 if (ipc
->us
.sq_port
== QRTR_PORT_CTRL
) {
947 /* control messages already require the type as 'command' */
948 skb_copy_bits(skb
, 0, &qrtr_type
, 4);
951 type
= le32_to_cpu(qrtr_type
);
952 rc
= enqueue_fn(node
, skb
, type
, &ipc
->us
, addr
);
957 qrtr_node_release(node
);
963 static int qrtr_send_resume_tx(struct qrtr_cb
*cb
)
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
;
972 node
= qrtr_node_lookup(remote
.sq_node
);
976 skb
= qrtr_alloc_ctrl_packet(&pkt
);
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
);
984 ret
= qrtr_node_enqueue(node
, skb
, QRTR_TYPE_RESUME_TX
, &local
, &remote
);
986 qrtr_node_release(node
);
991 static int qrtr_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
992 size_t size
, int flags
)
994 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, msg
->msg_name
);
995 struct sock
*sk
= sock
->sk
;
1002 if (sock_flag(sk
, SOCK_ZAPPED
)) {
1004 return -EADDRNOTAVAIL
;
1007 skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
,
1008 flags
& MSG_DONTWAIT
, &rc
);
1013 cb
= (struct qrtr_cb
*)skb
->cb
;
1016 if (copied
> size
) {
1018 msg
->msg_flags
|= MSG_TRUNC
;
1021 rc
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
1027 addr
->sq_family
= AF_QIPCRTR
;
1028 addr
->sq_node
= cb
->src_node
;
1029 addr
->sq_port
= cb
->src_port
;
1030 msg
->msg_namelen
= sizeof(*addr
);
1035 qrtr_send_resume_tx(cb
);
1037 skb_free_datagram(sk
, skb
);
1043 static int qrtr_connect(struct socket
*sock
, struct sockaddr
*saddr
,
1046 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, saddr
);
1047 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
1048 struct sock
*sk
= sock
->sk
;
1051 if (len
< sizeof(*addr
) || addr
->sq_family
!= AF_QIPCRTR
)
1056 sk
->sk_state
= TCP_CLOSE
;
1057 sock
->state
= SS_UNCONNECTED
;
1059 rc
= qrtr_autobind(sock
);
1066 sock
->state
= SS_CONNECTED
;
1067 sk
->sk_state
= TCP_ESTABLISHED
;
1074 static int qrtr_getname(struct socket
*sock
, struct sockaddr
*saddr
,
1077 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
1078 struct sockaddr_qrtr qaddr
;
1079 struct sock
*sk
= sock
->sk
;
1083 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
1094 qaddr
.sq_family
= AF_QIPCRTR
;
1096 memcpy(saddr
, &qaddr
, sizeof(qaddr
));
1098 return sizeof(qaddr
);
1101 static int qrtr_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
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
;
1116 len
= sk
->sk_sndbuf
- sk_wmem_alloc_get(sk
);
1119 rc
= put_user(len
, (int __user
*)argp
);
1122 skb
= skb_peek(&sk
->sk_receive_queue
);
1125 rc
= put_user(len
, (int __user
*)argp
);
1128 if (copy_from_user(&ifr
, argp
, sizeof(ifr
))) {
1133 sq
= (struct sockaddr_qrtr
*)&ifr
.ifr_addr
;
1135 if (copy_to_user(argp
, &ifr
, sizeof(ifr
))) {
1143 case SIOCGIFDSTADDR
:
1144 case SIOCSIFDSTADDR
:
1145 case SIOCGIFBRDADDR
:
1146 case SIOCSIFBRDADDR
:
1147 case SIOCGIFNETMASK
:
1148 case SIOCSIFNETMASK
:
1161 static int qrtr_release(struct socket
*sock
)
1163 struct sock
*sk
= sock
->sk
;
1164 struct qrtr_sock
*ipc
;
1172 sk
->sk_shutdown
= SHUTDOWN_MASK
;
1173 if (!sock_flag(sk
, SOCK_DEAD
))
1174 sk
->sk_state_change(sk
);
1176 sock_set_flag(sk
, SOCK_DEAD
);
1179 if (!sock_flag(sk
, SOCK_ZAPPED
))
1180 qrtr_port_remove(ipc
);
1182 skb_queue_purge(&sk
->sk_receive_queue
);
1190 static const struct proto_ops qrtr_proto_ops
= {
1191 .owner
= THIS_MODULE
,
1192 .family
= AF_QIPCRTR
,
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
,
1202 .gettstamp
= sock_gettstamp
,
1203 .poll
= datagram_poll
,
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
,
1212 static struct proto qrtr_proto
= {
1214 .owner
= THIS_MODULE
,
1215 .obj_size
= sizeof(struct qrtr_sock
),
1218 static int qrtr_create(struct net
*net
, struct socket
*sock
,
1219 int protocol
, int kern
)
1221 struct qrtr_sock
*ipc
;
1224 if (sock
->type
!= SOCK_DGRAM
)
1227 sk
= sk_alloc(net
, AF_QIPCRTR
, GFP_KERNEL
, &qrtr_proto
, kern
);
1231 sock_set_flag(sk
, SOCK_ZAPPED
);
1233 sock_init_data(sock
, sk
);
1234 sock
->ops
= &qrtr_proto_ops
;
1237 ipc
->us
.sq_family
= AF_QIPCRTR
;
1238 ipc
->us
.sq_node
= qrtr_local_nid
;
1239 ipc
->us
.sq_port
= 0;
1244 static const struct net_proto_family qrtr_family
= {
1245 .owner
= THIS_MODULE
,
1246 .family
= AF_QIPCRTR
,
1247 .create
= qrtr_create
,
1250 static int __init
qrtr_proto_init(void)
1254 rc
= proto_register(&qrtr_proto
, 1);
1258 rc
= sock_register(&qrtr_family
);
1260 proto_unregister(&qrtr_proto
);
1268 postcore_initcall(qrtr_proto_init
);
1270 static void __exit
qrtr_proto_fini(void)
1273 sock_unregister(qrtr_family
.family
);
1274 proto_unregister(&qrtr_proto
);
1276 module_exit(qrtr_proto_fini
);
1278 MODULE_DESCRIPTION("Qualcomm IPC-router driver");
1279 MODULE_LICENSE("GPL v2");
1280 MODULE_ALIAS_NETPROTO(PF_QIPCRTR
);