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/numa.h>
16 #define QRTR_PROTO_VER_1 1
17 #define QRTR_PROTO_VER_2 3
20 #define QRTR_MIN_EPH_SOCKET 0x4000
21 #define QRTR_MAX_EPH_SOCKET 0x7fff
24 * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1
25 * @version: protocol version
26 * @type: packet type; one of QRTR_TYPE_*
27 * @src_node_id: source node
28 * @src_port_id: source port
29 * @confirm_rx: boolean; whether a resume-tx packet should be send in reply
30 * @size: length of packet, excluding this header
31 * @dst_node_id: destination node
32 * @dst_port_id: destination port
46 * struct qrtr_hdr_v2 - (I|R)PCrouter packet header later versions
47 * @version: protocol version
48 * @type: packet type; one of QRTR_TYPE_*
49 * @flags: bitmask of QRTR_FLAGS_*
50 * @optlen: length of optional header data
51 * @size: length of packet, excluding this header and optlen
52 * @src_node_id: source node
53 * @src_port_id: source port
54 * @dst_node_id: destination node
55 * @dst_port_id: destination port
69 #define QRTR_FLAGS_CONFIRM_RX BIT(0)
81 #define QRTR_HDR_MAX_SIZE max_t(size_t, sizeof(struct qrtr_hdr_v1), \
82 sizeof(struct qrtr_hdr_v2))
85 /* WARNING: sk must be the first member */
87 struct sockaddr_qrtr us
;
88 struct sockaddr_qrtr peer
;
91 static inline struct qrtr_sock
*qrtr_sk(struct sock
*sk
)
93 BUILD_BUG_ON(offsetof(struct qrtr_sock
, sk
) != 0);
94 return container_of(sk
, struct qrtr_sock
, sk
);
97 static unsigned int qrtr_local_nid
= NUMA_NO_NODE
;
100 static RADIX_TREE(qrtr_nodes
, GFP_KERNEL
);
102 static LIST_HEAD(qrtr_all_nodes
);
103 /* lock for qrtr_nodes, qrtr_all_nodes and node reference */
104 static DEFINE_MUTEX(qrtr_node_lock
);
106 /* local port allocation management */
107 static DEFINE_IDR(qrtr_ports
);
108 static DEFINE_MUTEX(qrtr_port_lock
);
111 * struct qrtr_node - endpoint node
112 * @ep_lock: lock for endpoint management and callbacks
114 * @ref: reference count for node
116 * @rx_queue: receive queue
117 * @work: scheduled work struct for recv work
118 * @item: list item for broadcast list
121 struct mutex ep_lock
;
122 struct qrtr_endpoint
*ep
;
126 struct sk_buff_head rx_queue
;
127 struct work_struct work
;
128 struct list_head item
;
131 static int qrtr_local_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
,
132 int type
, struct sockaddr_qrtr
*from
,
133 struct sockaddr_qrtr
*to
);
134 static int qrtr_bcast_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
,
135 int type
, struct sockaddr_qrtr
*from
,
136 struct sockaddr_qrtr
*to
);
138 /* Release node resources and free the node.
140 * Do not call directly, use qrtr_node_release. To be used with
141 * kref_put_mutex. As such, the node mutex is expected to be locked on call.
143 static void __qrtr_node_release(struct kref
*kref
)
145 struct qrtr_node
*node
= container_of(kref
, struct qrtr_node
, ref
);
147 if (node
->nid
!= QRTR_EP_NID_AUTO
)
148 radix_tree_delete(&qrtr_nodes
, node
->nid
);
150 list_del(&node
->item
);
151 mutex_unlock(&qrtr_node_lock
);
153 cancel_work_sync(&node
->work
);
154 skb_queue_purge(&node
->rx_queue
);
158 /* Increment reference to node. */
159 static struct qrtr_node
*qrtr_node_acquire(struct qrtr_node
*node
)
162 kref_get(&node
->ref
);
166 /* Decrement reference to node and release as necessary. */
167 static void qrtr_node_release(struct qrtr_node
*node
)
171 kref_put_mutex(&node
->ref
, __qrtr_node_release
, &qrtr_node_lock
);
174 /* Pass an outgoing packet socket buffer to the endpoint driver. */
175 static int qrtr_node_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
,
176 int type
, struct sockaddr_qrtr
*from
,
177 struct sockaddr_qrtr
*to
)
179 struct qrtr_hdr_v1
*hdr
;
180 size_t len
= skb
->len
;
183 hdr
= skb_push(skb
, sizeof(*hdr
));
184 hdr
->version
= cpu_to_le32(QRTR_PROTO_VER_1
);
185 hdr
->type
= cpu_to_le32(type
);
186 hdr
->src_node_id
= cpu_to_le32(from
->sq_node
);
187 hdr
->src_port_id
= cpu_to_le32(from
->sq_port
);
188 if (to
->sq_port
== QRTR_PORT_CTRL
) {
189 hdr
->dst_node_id
= cpu_to_le32(node
->nid
);
190 hdr
->dst_port_id
= cpu_to_le32(QRTR_NODE_BCAST
);
192 hdr
->dst_node_id
= cpu_to_le32(to
->sq_node
);
193 hdr
->dst_port_id
= cpu_to_le32(to
->sq_port
);
196 hdr
->size
= cpu_to_le32(len
);
199 skb_put_padto(skb
, ALIGN(len
, 4));
201 mutex_lock(&node
->ep_lock
);
203 rc
= node
->ep
->xmit(node
->ep
, skb
);
206 mutex_unlock(&node
->ep_lock
);
211 /* Lookup node by id.
213 * callers must release with qrtr_node_release()
215 static struct qrtr_node
*qrtr_node_lookup(unsigned int nid
)
217 struct qrtr_node
*node
;
219 mutex_lock(&qrtr_node_lock
);
220 node
= radix_tree_lookup(&qrtr_nodes
, nid
);
221 node
= qrtr_node_acquire(node
);
222 mutex_unlock(&qrtr_node_lock
);
227 /* Assign node id to node.
229 * This is mostly useful for automatic node id assignment, based on
230 * the source id in the incoming packet.
232 static void qrtr_node_assign(struct qrtr_node
*node
, unsigned int nid
)
234 if (node
->nid
!= QRTR_EP_NID_AUTO
|| nid
== QRTR_EP_NID_AUTO
)
237 mutex_lock(&qrtr_node_lock
);
238 radix_tree_insert(&qrtr_nodes
, nid
, node
);
240 mutex_unlock(&qrtr_node_lock
);
244 * qrtr_endpoint_post() - post incoming data
245 * @ep: endpoint handle
246 * @data: data pointer
247 * @len: size of data in bytes
249 * Return: 0 on success; negative error code on failure
251 int qrtr_endpoint_post(struct qrtr_endpoint
*ep
, const void *data
, size_t len
)
253 struct qrtr_node
*node
= ep
->node
;
254 const struct qrtr_hdr_v1
*v1
;
255 const struct qrtr_hdr_v2
*v2
;
265 skb
= netdev_alloc_skb(NULL
, len
);
269 cb
= (struct qrtr_cb
*)skb
->cb
;
271 /* Version field in v1 is little endian, so this works for both cases */
275 case QRTR_PROTO_VER_1
:
277 hdrlen
= sizeof(*v1
);
279 cb
->type
= le32_to_cpu(v1
->type
);
280 cb
->src_node
= le32_to_cpu(v1
->src_node_id
);
281 cb
->src_port
= le32_to_cpu(v1
->src_port_id
);
282 cb
->confirm_rx
= !!v1
->confirm_rx
;
283 cb
->dst_node
= le32_to_cpu(v1
->dst_node_id
);
284 cb
->dst_port
= le32_to_cpu(v1
->dst_port_id
);
286 size
= le32_to_cpu(v1
->size
);
288 case QRTR_PROTO_VER_2
:
290 hdrlen
= sizeof(*v2
) + v2
->optlen
;
293 cb
->confirm_rx
= !!(v2
->flags
& QRTR_FLAGS_CONFIRM_RX
);
294 cb
->src_node
= le16_to_cpu(v2
->src_node_id
);
295 cb
->src_port
= le16_to_cpu(v2
->src_port_id
);
296 cb
->dst_node
= le16_to_cpu(v2
->dst_node_id
);
297 cb
->dst_port
= le16_to_cpu(v2
->dst_port_id
);
299 if (cb
->src_port
== (u16
)QRTR_PORT_CTRL
)
300 cb
->src_port
= QRTR_PORT_CTRL
;
301 if (cb
->dst_port
== (u16
)QRTR_PORT_CTRL
)
302 cb
->dst_port
= QRTR_PORT_CTRL
;
304 size
= le32_to_cpu(v2
->size
);
307 pr_err("qrtr: Invalid version %d\n", ver
);
311 if (len
!= ALIGN(size
, 4) + hdrlen
)
314 if (cb
->dst_port
!= QRTR_PORT_CTRL
&& cb
->type
!= QRTR_TYPE_DATA
)
317 skb_put_data(skb
, data
+ hdrlen
, size
);
319 skb_queue_tail(&node
->rx_queue
, skb
);
320 schedule_work(&node
->work
);
329 EXPORT_SYMBOL_GPL(qrtr_endpoint_post
);
332 * qrtr_alloc_ctrl_packet() - allocate control packet skb
333 * @pkt: reference to qrtr_ctrl_pkt pointer
335 * Returns newly allocated sk_buff, or NULL on failure
337 * This function allocates a sk_buff large enough to carry a qrtr_ctrl_pkt and
338 * on success returns a reference to the control packet in @pkt.
340 static struct sk_buff
*qrtr_alloc_ctrl_packet(struct qrtr_ctrl_pkt
**pkt
)
342 const int pkt_len
= sizeof(struct qrtr_ctrl_pkt
);
345 skb
= alloc_skb(QRTR_HDR_MAX_SIZE
+ pkt_len
, GFP_KERNEL
);
349 skb_reserve(skb
, QRTR_HDR_MAX_SIZE
);
350 *pkt
= skb_put_zero(skb
, pkt_len
);
355 static struct qrtr_sock
*qrtr_port_lookup(int port
);
356 static void qrtr_port_put(struct qrtr_sock
*ipc
);
358 /* Handle and route a received packet.
360 * This will auto-reply with resume-tx packet as necessary.
362 static void qrtr_node_rx_work(struct work_struct
*work
)
364 struct qrtr_node
*node
= container_of(work
, struct qrtr_node
, work
);
365 struct qrtr_ctrl_pkt
*pkt
;
366 struct sockaddr_qrtr dst
;
367 struct sockaddr_qrtr src
;
370 while ((skb
= skb_dequeue(&node
->rx_queue
)) != NULL
) {
371 struct qrtr_sock
*ipc
;
375 cb
= (struct qrtr_cb
*)skb
->cb
;
376 src
.sq_node
= cb
->src_node
;
377 src
.sq_port
= cb
->src_port
;
378 dst
.sq_node
= cb
->dst_node
;
379 dst
.sq_port
= cb
->dst_port
;
380 confirm
= !!cb
->confirm_rx
;
382 qrtr_node_assign(node
, cb
->src_node
);
384 ipc
= qrtr_port_lookup(cb
->dst_port
);
388 if (sock_queue_rcv_skb(&ipc
->sk
, skb
))
395 skb
= qrtr_alloc_ctrl_packet(&pkt
);
399 pkt
->cmd
= cpu_to_le32(QRTR_TYPE_RESUME_TX
);
400 pkt
->client
.node
= cpu_to_le32(dst
.sq_node
);
401 pkt
->client
.port
= cpu_to_le32(dst
.sq_port
);
403 if (qrtr_node_enqueue(node
, skb
, QRTR_TYPE_RESUME_TX
,
411 * qrtr_endpoint_register() - register a new endpoint
412 * @ep: endpoint to register
413 * @nid: desired node id; may be QRTR_EP_NID_AUTO for auto-assignment
414 * Return: 0 on success; negative error code on failure
416 * The specified endpoint must have the xmit function pointer set on call.
418 int qrtr_endpoint_register(struct qrtr_endpoint
*ep
, unsigned int nid
)
420 struct qrtr_node
*node
;
422 if (!ep
|| !ep
->xmit
)
425 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
429 INIT_WORK(&node
->work
, qrtr_node_rx_work
);
430 kref_init(&node
->ref
);
431 mutex_init(&node
->ep_lock
);
432 skb_queue_head_init(&node
->rx_queue
);
433 node
->nid
= QRTR_EP_NID_AUTO
;
436 qrtr_node_assign(node
, nid
);
438 mutex_lock(&qrtr_node_lock
);
439 list_add(&node
->item
, &qrtr_all_nodes
);
440 mutex_unlock(&qrtr_node_lock
);
445 EXPORT_SYMBOL_GPL(qrtr_endpoint_register
);
448 * qrtr_endpoint_unregister - unregister endpoint
449 * @ep: endpoint to unregister
451 void qrtr_endpoint_unregister(struct qrtr_endpoint
*ep
)
453 struct qrtr_node
*node
= ep
->node
;
454 struct sockaddr_qrtr src
= {AF_QIPCRTR
, node
->nid
, QRTR_PORT_CTRL
};
455 struct sockaddr_qrtr dst
= {AF_QIPCRTR
, qrtr_local_nid
, QRTR_PORT_CTRL
};
456 struct qrtr_ctrl_pkt
*pkt
;
459 mutex_lock(&node
->ep_lock
);
461 mutex_unlock(&node
->ep_lock
);
463 /* Notify the local controller about the event */
464 skb
= qrtr_alloc_ctrl_packet(&pkt
);
466 pkt
->cmd
= cpu_to_le32(QRTR_TYPE_BYE
);
467 qrtr_local_enqueue(NULL
, skb
, QRTR_TYPE_BYE
, &src
, &dst
);
470 qrtr_node_release(node
);
473 EXPORT_SYMBOL_GPL(qrtr_endpoint_unregister
);
475 /* Lookup socket by port.
477 * Callers must release with qrtr_port_put()
479 static struct qrtr_sock
*qrtr_port_lookup(int port
)
481 struct qrtr_sock
*ipc
;
483 if (port
== QRTR_PORT_CTRL
)
486 mutex_lock(&qrtr_port_lock
);
487 ipc
= idr_find(&qrtr_ports
, port
);
490 mutex_unlock(&qrtr_port_lock
);
495 /* Release acquired socket. */
496 static void qrtr_port_put(struct qrtr_sock
*ipc
)
501 /* Remove port assignment. */
502 static void qrtr_port_remove(struct qrtr_sock
*ipc
)
504 struct qrtr_ctrl_pkt
*pkt
;
506 int port
= ipc
->us
.sq_port
;
507 struct sockaddr_qrtr to
;
509 to
.sq_family
= AF_QIPCRTR
;
510 to
.sq_node
= QRTR_NODE_BCAST
;
511 to
.sq_port
= QRTR_PORT_CTRL
;
513 skb
= qrtr_alloc_ctrl_packet(&pkt
);
515 pkt
->cmd
= cpu_to_le32(QRTR_TYPE_DEL_CLIENT
);
516 pkt
->client
.node
= cpu_to_le32(ipc
->us
.sq_node
);
517 pkt
->client
.port
= cpu_to_le32(ipc
->us
.sq_port
);
519 skb_set_owner_w(skb
, &ipc
->sk
);
520 qrtr_bcast_enqueue(NULL
, skb
, QRTR_TYPE_DEL_CLIENT
, &ipc
->us
,
524 if (port
== QRTR_PORT_CTRL
)
527 __sock_put(&ipc
->sk
);
529 mutex_lock(&qrtr_port_lock
);
530 idr_remove(&qrtr_ports
, port
);
531 mutex_unlock(&qrtr_port_lock
);
534 /* Assign port number to socket.
536 * Specify port in the integer pointed to by port, and it will be adjusted
537 * on return as necesssary.
540 * 0: Assign ephemeral port in [QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET]
541 * <QRTR_MIN_EPH_SOCKET: Specified; requires CAP_NET_ADMIN
542 * >QRTR_MIN_EPH_SOCKET: Specified; available to all
544 static int qrtr_port_assign(struct qrtr_sock
*ipc
, int *port
)
548 mutex_lock(&qrtr_port_lock
);
550 rc
= idr_alloc(&qrtr_ports
, ipc
,
551 QRTR_MIN_EPH_SOCKET
, QRTR_MAX_EPH_SOCKET
+ 1,
555 } else if (*port
< QRTR_MIN_EPH_SOCKET
&& !capable(CAP_NET_ADMIN
)) {
557 } else if (*port
== QRTR_PORT_CTRL
) {
558 rc
= idr_alloc(&qrtr_ports
, ipc
, 0, 1, GFP_ATOMIC
);
560 rc
= idr_alloc(&qrtr_ports
, ipc
, *port
, *port
+ 1, GFP_ATOMIC
);
564 mutex_unlock(&qrtr_port_lock
);
576 /* Reset all non-control ports */
577 static void qrtr_reset_ports(void)
579 struct qrtr_sock
*ipc
;
582 mutex_lock(&qrtr_port_lock
);
583 idr_for_each_entry(&qrtr_ports
, ipc
, id
) {
584 /* Don't reset control port */
589 ipc
->sk
.sk_err
= ENETRESET
;
590 ipc
->sk
.sk_error_report(&ipc
->sk
);
593 mutex_unlock(&qrtr_port_lock
);
596 /* Bind socket to address.
598 * Socket should be locked upon call.
600 static int __qrtr_bind(struct socket
*sock
,
601 const struct sockaddr_qrtr
*addr
, int zapped
)
603 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
604 struct sock
*sk
= sock
->sk
;
609 if (!zapped
&& addr
->sq_port
== ipc
->us
.sq_port
)
612 port
= addr
->sq_port
;
613 rc
= qrtr_port_assign(ipc
, &port
);
617 /* unbind previous, if any */
619 qrtr_port_remove(ipc
);
620 ipc
->us
.sq_port
= port
;
622 sock_reset_flag(sk
, SOCK_ZAPPED
);
624 /* Notify all open ports about the new controller */
625 if (port
== QRTR_PORT_CTRL
)
631 /* Auto bind to an ephemeral port. */
632 static int qrtr_autobind(struct socket
*sock
)
634 struct sock
*sk
= sock
->sk
;
635 struct sockaddr_qrtr addr
;
637 if (!sock_flag(sk
, SOCK_ZAPPED
))
640 addr
.sq_family
= AF_QIPCRTR
;
641 addr
.sq_node
= qrtr_local_nid
;
644 return __qrtr_bind(sock
, &addr
, 1);
647 /* Bind socket to specified sockaddr. */
648 static int qrtr_bind(struct socket
*sock
, struct sockaddr
*saddr
, int len
)
650 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, saddr
);
651 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
652 struct sock
*sk
= sock
->sk
;
655 if (len
< sizeof(*addr
) || addr
->sq_family
!= AF_QIPCRTR
)
658 if (addr
->sq_node
!= ipc
->us
.sq_node
)
662 rc
= __qrtr_bind(sock
, addr
, sock_flag(sk
, SOCK_ZAPPED
));
668 /* Queue packet to local peer socket. */
669 static int qrtr_local_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
,
670 int type
, struct sockaddr_qrtr
*from
,
671 struct sockaddr_qrtr
*to
)
673 struct qrtr_sock
*ipc
;
676 ipc
= qrtr_port_lookup(to
->sq_port
);
677 if (!ipc
|| &ipc
->sk
== skb
->sk
) { /* do not send to self */
682 cb
= (struct qrtr_cb
*)skb
->cb
;
683 cb
->src_node
= from
->sq_node
;
684 cb
->src_port
= from
->sq_port
;
686 if (sock_queue_rcv_skb(&ipc
->sk
, skb
)) {
697 /* Queue packet for broadcast. */
698 static int qrtr_bcast_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
,
699 int type
, struct sockaddr_qrtr
*from
,
700 struct sockaddr_qrtr
*to
)
702 struct sk_buff
*skbn
;
704 mutex_lock(&qrtr_node_lock
);
705 list_for_each_entry(node
, &qrtr_all_nodes
, item
) {
706 skbn
= skb_clone(skb
, GFP_KERNEL
);
709 skb_set_owner_w(skbn
, skb
->sk
);
710 qrtr_node_enqueue(node
, skbn
, type
, from
, to
);
712 mutex_unlock(&qrtr_node_lock
);
714 qrtr_local_enqueue(node
, skb
, type
, from
, to
);
719 static int qrtr_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
)
721 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, msg
->msg_name
);
722 int (*enqueue_fn
)(struct qrtr_node
*, struct sk_buff
*, int,
723 struct sockaddr_qrtr
*, struct sockaddr_qrtr
*);
724 __le32 qrtr_type
= cpu_to_le32(QRTR_TYPE_DATA
);
725 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
726 struct sock
*sk
= sock
->sk
;
727 struct qrtr_node
*node
;
733 if (msg
->msg_flags
& ~(MSG_DONTWAIT
))
742 if (msg
->msg_namelen
< sizeof(*addr
)) {
747 if (addr
->sq_family
!= AF_QIPCRTR
) {
752 rc
= qrtr_autobind(sock
);
757 } else if (sk
->sk_state
== TCP_ESTABLISHED
) {
765 if (addr
->sq_node
== QRTR_NODE_BCAST
) {
766 enqueue_fn
= qrtr_bcast_enqueue
;
767 if (addr
->sq_port
!= QRTR_PORT_CTRL
) {
771 } else if (addr
->sq_node
== ipc
->us
.sq_node
) {
772 enqueue_fn
= qrtr_local_enqueue
;
774 enqueue_fn
= qrtr_node_enqueue
;
775 node
= qrtr_node_lookup(addr
->sq_node
);
782 plen
= (len
+ 3) & ~3;
783 skb
= sock_alloc_send_skb(sk
, plen
+ QRTR_HDR_MAX_SIZE
,
784 msg
->msg_flags
& MSG_DONTWAIT
, &rc
);
788 skb_reserve(skb
, QRTR_HDR_MAX_SIZE
);
790 rc
= memcpy_from_msg(skb_put(skb
, len
), msg
, len
);
796 if (ipc
->us
.sq_port
== QRTR_PORT_CTRL
) {
803 /* control messages already require the type as 'command' */
804 skb_copy_bits(skb
, 0, &qrtr_type
, 4);
807 type
= le32_to_cpu(qrtr_type
);
808 rc
= enqueue_fn(node
, skb
, type
, &ipc
->us
, addr
);
813 qrtr_node_release(node
);
819 static int qrtr_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
820 size_t size
, int flags
)
822 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, msg
->msg_name
);
823 struct sock
*sk
= sock
->sk
;
830 if (sock_flag(sk
, SOCK_ZAPPED
)) {
832 return -EADDRNOTAVAIL
;
835 skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
,
836 flags
& MSG_DONTWAIT
, &rc
);
845 msg
->msg_flags
|= MSG_TRUNC
;
848 rc
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
854 cb
= (struct qrtr_cb
*)skb
->cb
;
855 addr
->sq_family
= AF_QIPCRTR
;
856 addr
->sq_node
= cb
->src_node
;
857 addr
->sq_port
= cb
->src_port
;
858 msg
->msg_namelen
= sizeof(*addr
);
862 skb_free_datagram(sk
, skb
);
868 static int qrtr_connect(struct socket
*sock
, struct sockaddr
*saddr
,
871 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, saddr
);
872 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
873 struct sock
*sk
= sock
->sk
;
876 if (len
< sizeof(*addr
) || addr
->sq_family
!= AF_QIPCRTR
)
881 sk
->sk_state
= TCP_CLOSE
;
882 sock
->state
= SS_UNCONNECTED
;
884 rc
= qrtr_autobind(sock
);
891 sock
->state
= SS_CONNECTED
;
892 sk
->sk_state
= TCP_ESTABLISHED
;
899 static int qrtr_getname(struct socket
*sock
, struct sockaddr
*saddr
,
902 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
903 struct sockaddr_qrtr qaddr
;
904 struct sock
*sk
= sock
->sk
;
908 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
919 qaddr
.sq_family
= AF_QIPCRTR
;
921 memcpy(saddr
, &qaddr
, sizeof(qaddr
));
923 return sizeof(qaddr
);
926 static int qrtr_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
928 void __user
*argp
= (void __user
*)arg
;
929 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
930 struct sock
*sk
= sock
->sk
;
931 struct sockaddr_qrtr
*sq
;
941 len
= sk
->sk_sndbuf
- sk_wmem_alloc_get(sk
);
944 rc
= put_user(len
, (int __user
*)argp
);
947 skb
= skb_peek(&sk
->sk_receive_queue
);
950 rc
= put_user(len
, (int __user
*)argp
);
953 if (copy_from_user(&ifr
, argp
, sizeof(ifr
))) {
958 sq
= (struct sockaddr_qrtr
*)&ifr
.ifr_addr
;
960 if (copy_to_user(argp
, &ifr
, sizeof(ifr
))) {
986 static int qrtr_release(struct socket
*sock
)
988 struct sock
*sk
= sock
->sk
;
989 struct qrtr_sock
*ipc
;
997 sk
->sk_shutdown
= SHUTDOWN_MASK
;
998 if (!sock_flag(sk
, SOCK_DEAD
))
999 sk
->sk_state_change(sk
);
1001 sock_set_flag(sk
, SOCK_DEAD
);
1004 if (!sock_flag(sk
, SOCK_ZAPPED
))
1005 qrtr_port_remove(ipc
);
1007 skb_queue_purge(&sk
->sk_receive_queue
);
1015 static const struct proto_ops qrtr_proto_ops
= {
1016 .owner
= THIS_MODULE
,
1017 .family
= AF_QIPCRTR
,
1019 .connect
= qrtr_connect
,
1020 .socketpair
= sock_no_socketpair
,
1021 .accept
= sock_no_accept
,
1022 .listen
= sock_no_listen
,
1023 .sendmsg
= qrtr_sendmsg
,
1024 .recvmsg
= qrtr_recvmsg
,
1025 .getname
= qrtr_getname
,
1026 .ioctl
= qrtr_ioctl
,
1027 .gettstamp
= sock_gettstamp
,
1028 .poll
= datagram_poll
,
1029 .shutdown
= sock_no_shutdown
,
1030 .setsockopt
= sock_no_setsockopt
,
1031 .getsockopt
= sock_no_getsockopt
,
1032 .release
= qrtr_release
,
1033 .mmap
= sock_no_mmap
,
1034 .sendpage
= sock_no_sendpage
,
1037 static struct proto qrtr_proto
= {
1039 .owner
= THIS_MODULE
,
1040 .obj_size
= sizeof(struct qrtr_sock
),
1043 static int qrtr_create(struct net
*net
, struct socket
*sock
,
1044 int protocol
, int kern
)
1046 struct qrtr_sock
*ipc
;
1049 if (sock
->type
!= SOCK_DGRAM
)
1052 sk
= sk_alloc(net
, AF_QIPCRTR
, GFP_KERNEL
, &qrtr_proto
, kern
);
1056 sock_set_flag(sk
, SOCK_ZAPPED
);
1058 sock_init_data(sock
, sk
);
1059 sock
->ops
= &qrtr_proto_ops
;
1062 ipc
->us
.sq_family
= AF_QIPCRTR
;
1063 ipc
->us
.sq_node
= qrtr_local_nid
;
1064 ipc
->us
.sq_port
= 0;
1069 static const struct nla_policy qrtr_policy
[IFA_MAX
+ 1] = {
1070 [IFA_LOCAL
] = { .type
= NLA_U32
},
1073 static int qrtr_addr_doit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1074 struct netlink_ext_ack
*extack
)
1076 struct nlattr
*tb
[IFA_MAX
+ 1];
1077 struct ifaddrmsg
*ifm
;
1080 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
1083 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
1088 rc
= nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFA_MAX
,
1089 qrtr_policy
, extack
);
1093 ifm
= nlmsg_data(nlh
);
1097 qrtr_local_nid
= nla_get_u32(tb
[IFA_LOCAL
]);
1101 static const struct net_proto_family qrtr_family
= {
1102 .owner
= THIS_MODULE
,
1103 .family
= AF_QIPCRTR
,
1104 .create
= qrtr_create
,
1107 static int __init
qrtr_proto_init(void)
1111 rc
= proto_register(&qrtr_proto
, 1);
1115 rc
= sock_register(&qrtr_family
);
1117 proto_unregister(&qrtr_proto
);
1121 rc
= rtnl_register_module(THIS_MODULE
, PF_QIPCRTR
, RTM_NEWADDR
, qrtr_addr_doit
, NULL
, 0);
1123 sock_unregister(qrtr_family
.family
);
1124 proto_unregister(&qrtr_proto
);
1129 postcore_initcall(qrtr_proto_init
);
1131 static void __exit
qrtr_proto_fini(void)
1133 rtnl_unregister(PF_QIPCRTR
, RTM_NEWADDR
);
1134 sock_unregister(qrtr_family
.family
);
1135 proto_unregister(&qrtr_proto
);
1137 module_exit(qrtr_proto_fini
);
1139 MODULE_DESCRIPTION("Qualcomm IPC-router driver");
1140 MODULE_LICENSE("GPL v2");
1141 MODULE_ALIAS_NETPROTO(PF_QIPCRTR
);