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 skb_queue_purge(&node
->rx_queue
);
157 /* Increment reference to node. */
158 static struct qrtr_node
*qrtr_node_acquire(struct qrtr_node
*node
)
161 kref_get(&node
->ref
);
165 /* Decrement reference to node and release as necessary. */
166 static void qrtr_node_release(struct qrtr_node
*node
)
170 kref_put_mutex(&node
->ref
, __qrtr_node_release
, &qrtr_node_lock
);
173 /* Pass an outgoing packet socket buffer to the endpoint driver. */
174 static int qrtr_node_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
,
175 int type
, struct sockaddr_qrtr
*from
,
176 struct sockaddr_qrtr
*to
)
178 struct qrtr_hdr_v1
*hdr
;
179 size_t len
= skb
->len
;
182 hdr
= skb_push(skb
, sizeof(*hdr
));
183 hdr
->version
= cpu_to_le32(QRTR_PROTO_VER_1
);
184 hdr
->type
= cpu_to_le32(type
);
185 hdr
->src_node_id
= cpu_to_le32(from
->sq_node
);
186 hdr
->src_port_id
= cpu_to_le32(from
->sq_port
);
187 if (to
->sq_port
== QRTR_PORT_CTRL
) {
188 hdr
->dst_node_id
= cpu_to_le32(node
->nid
);
189 hdr
->dst_port_id
= cpu_to_le32(QRTR_NODE_BCAST
);
191 hdr
->dst_node_id
= cpu_to_le32(to
->sq_node
);
192 hdr
->dst_port_id
= cpu_to_le32(to
->sq_port
);
195 hdr
->size
= cpu_to_le32(len
);
198 skb_put_padto(skb
, ALIGN(len
, 4));
200 mutex_lock(&node
->ep_lock
);
202 rc
= node
->ep
->xmit(node
->ep
, skb
);
205 mutex_unlock(&node
->ep_lock
);
210 /* Lookup node by id.
212 * callers must release with qrtr_node_release()
214 static struct qrtr_node
*qrtr_node_lookup(unsigned int nid
)
216 struct qrtr_node
*node
;
218 mutex_lock(&qrtr_node_lock
);
219 node
= radix_tree_lookup(&qrtr_nodes
, nid
);
220 node
= qrtr_node_acquire(node
);
221 mutex_unlock(&qrtr_node_lock
);
226 /* Assign node id to node.
228 * This is mostly useful for automatic node id assignment, based on
229 * the source id in the incoming packet.
231 static void qrtr_node_assign(struct qrtr_node
*node
, unsigned int nid
)
233 if (node
->nid
!= QRTR_EP_NID_AUTO
|| nid
== QRTR_EP_NID_AUTO
)
236 mutex_lock(&qrtr_node_lock
);
237 radix_tree_insert(&qrtr_nodes
, nid
, node
);
239 mutex_unlock(&qrtr_node_lock
);
243 * qrtr_endpoint_post() - post incoming data
244 * @ep: endpoint handle
245 * @data: data pointer
246 * @len: size of data in bytes
248 * Return: 0 on success; negative error code on failure
250 int qrtr_endpoint_post(struct qrtr_endpoint
*ep
, const void *data
, size_t len
)
252 struct qrtr_node
*node
= ep
->node
;
253 const struct qrtr_hdr_v1
*v1
;
254 const struct qrtr_hdr_v2
*v2
;
264 skb
= netdev_alloc_skb(NULL
, len
);
268 cb
= (struct qrtr_cb
*)skb
->cb
;
270 /* Version field in v1 is little endian, so this works for both cases */
274 case QRTR_PROTO_VER_1
:
276 hdrlen
= sizeof(*v1
);
278 cb
->type
= le32_to_cpu(v1
->type
);
279 cb
->src_node
= le32_to_cpu(v1
->src_node_id
);
280 cb
->src_port
= le32_to_cpu(v1
->src_port_id
);
281 cb
->confirm_rx
= !!v1
->confirm_rx
;
282 cb
->dst_node
= le32_to_cpu(v1
->dst_node_id
);
283 cb
->dst_port
= le32_to_cpu(v1
->dst_port_id
);
285 size
= le32_to_cpu(v1
->size
);
287 case QRTR_PROTO_VER_2
:
289 hdrlen
= sizeof(*v2
) + v2
->optlen
;
292 cb
->confirm_rx
= !!(v2
->flags
& QRTR_FLAGS_CONFIRM_RX
);
293 cb
->src_node
= le16_to_cpu(v2
->src_node_id
);
294 cb
->src_port
= le16_to_cpu(v2
->src_port_id
);
295 cb
->dst_node
= le16_to_cpu(v2
->dst_node_id
);
296 cb
->dst_port
= le16_to_cpu(v2
->dst_port_id
);
298 if (cb
->src_port
== (u16
)QRTR_PORT_CTRL
)
299 cb
->src_port
= QRTR_PORT_CTRL
;
300 if (cb
->dst_port
== (u16
)QRTR_PORT_CTRL
)
301 cb
->dst_port
= QRTR_PORT_CTRL
;
303 size
= le32_to_cpu(v2
->size
);
306 pr_err("qrtr: Invalid version %d\n", ver
);
310 if (len
!= ALIGN(size
, 4) + hdrlen
)
313 if (cb
->dst_port
!= QRTR_PORT_CTRL
&& cb
->type
!= QRTR_TYPE_DATA
)
316 skb_put_data(skb
, data
+ hdrlen
, size
);
318 skb_queue_tail(&node
->rx_queue
, skb
);
319 schedule_work(&node
->work
);
328 EXPORT_SYMBOL_GPL(qrtr_endpoint_post
);
331 * qrtr_alloc_ctrl_packet() - allocate control packet skb
332 * @pkt: reference to qrtr_ctrl_pkt pointer
334 * Returns newly allocated sk_buff, or NULL on failure
336 * This function allocates a sk_buff large enough to carry a qrtr_ctrl_pkt and
337 * on success returns a reference to the control packet in @pkt.
339 static struct sk_buff
*qrtr_alloc_ctrl_packet(struct qrtr_ctrl_pkt
**pkt
)
341 const int pkt_len
= sizeof(struct qrtr_ctrl_pkt
);
344 skb
= alloc_skb(QRTR_HDR_MAX_SIZE
+ pkt_len
, GFP_KERNEL
);
348 skb_reserve(skb
, QRTR_HDR_MAX_SIZE
);
349 *pkt
= skb_put_zero(skb
, pkt_len
);
354 static struct qrtr_sock
*qrtr_port_lookup(int port
);
355 static void qrtr_port_put(struct qrtr_sock
*ipc
);
357 /* Handle and route a received packet.
359 * This will auto-reply with resume-tx packet as necessary.
361 static void qrtr_node_rx_work(struct work_struct
*work
)
363 struct qrtr_node
*node
= container_of(work
, struct qrtr_node
, work
);
364 struct qrtr_ctrl_pkt
*pkt
;
365 struct sockaddr_qrtr dst
;
366 struct sockaddr_qrtr src
;
369 while ((skb
= skb_dequeue(&node
->rx_queue
)) != NULL
) {
370 struct qrtr_sock
*ipc
;
374 cb
= (struct qrtr_cb
*)skb
->cb
;
375 src
.sq_node
= cb
->src_node
;
376 src
.sq_port
= cb
->src_port
;
377 dst
.sq_node
= cb
->dst_node
;
378 dst
.sq_port
= cb
->dst_port
;
379 confirm
= !!cb
->confirm_rx
;
381 qrtr_node_assign(node
, cb
->src_node
);
383 ipc
= qrtr_port_lookup(cb
->dst_port
);
387 if (sock_queue_rcv_skb(&ipc
->sk
, skb
))
394 skb
= qrtr_alloc_ctrl_packet(&pkt
);
398 pkt
->cmd
= cpu_to_le32(QRTR_TYPE_RESUME_TX
);
399 pkt
->client
.node
= cpu_to_le32(dst
.sq_node
);
400 pkt
->client
.port
= cpu_to_le32(dst
.sq_port
);
402 if (qrtr_node_enqueue(node
, skb
, QRTR_TYPE_RESUME_TX
,
410 * qrtr_endpoint_register() - register a new endpoint
411 * @ep: endpoint to register
412 * @nid: desired node id; may be QRTR_EP_NID_AUTO for auto-assignment
413 * Return: 0 on success; negative error code on failure
415 * The specified endpoint must have the xmit function pointer set on call.
417 int qrtr_endpoint_register(struct qrtr_endpoint
*ep
, unsigned int nid
)
419 struct qrtr_node
*node
;
421 if (!ep
|| !ep
->xmit
)
424 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
428 INIT_WORK(&node
->work
, qrtr_node_rx_work
);
429 kref_init(&node
->ref
);
430 mutex_init(&node
->ep_lock
);
431 skb_queue_head_init(&node
->rx_queue
);
432 node
->nid
= QRTR_EP_NID_AUTO
;
435 qrtr_node_assign(node
, nid
);
437 mutex_lock(&qrtr_node_lock
);
438 list_add(&node
->item
, &qrtr_all_nodes
);
439 mutex_unlock(&qrtr_node_lock
);
444 EXPORT_SYMBOL_GPL(qrtr_endpoint_register
);
447 * qrtr_endpoint_unregister - unregister endpoint
448 * @ep: endpoint to unregister
450 void qrtr_endpoint_unregister(struct qrtr_endpoint
*ep
)
452 struct qrtr_node
*node
= ep
->node
;
453 struct sockaddr_qrtr src
= {AF_QIPCRTR
, node
->nid
, QRTR_PORT_CTRL
};
454 struct sockaddr_qrtr dst
= {AF_QIPCRTR
, qrtr_local_nid
, QRTR_PORT_CTRL
};
455 struct qrtr_ctrl_pkt
*pkt
;
458 mutex_lock(&node
->ep_lock
);
460 mutex_unlock(&node
->ep_lock
);
462 /* Notify the local controller about the event */
463 skb
= qrtr_alloc_ctrl_packet(&pkt
);
465 pkt
->cmd
= cpu_to_le32(QRTR_TYPE_BYE
);
466 qrtr_local_enqueue(NULL
, skb
, QRTR_TYPE_BYE
, &src
, &dst
);
469 qrtr_node_release(node
);
472 EXPORT_SYMBOL_GPL(qrtr_endpoint_unregister
);
474 /* Lookup socket by port.
476 * Callers must release with qrtr_port_put()
478 static struct qrtr_sock
*qrtr_port_lookup(int port
)
480 struct qrtr_sock
*ipc
;
482 if (port
== QRTR_PORT_CTRL
)
485 mutex_lock(&qrtr_port_lock
);
486 ipc
= idr_find(&qrtr_ports
, port
);
489 mutex_unlock(&qrtr_port_lock
);
494 /* Release acquired socket. */
495 static void qrtr_port_put(struct qrtr_sock
*ipc
)
500 /* Remove port assignment. */
501 static void qrtr_port_remove(struct qrtr_sock
*ipc
)
503 struct qrtr_ctrl_pkt
*pkt
;
505 int port
= ipc
->us
.sq_port
;
506 struct sockaddr_qrtr to
;
508 to
.sq_family
= AF_QIPCRTR
;
509 to
.sq_node
= QRTR_NODE_BCAST
;
510 to
.sq_port
= QRTR_PORT_CTRL
;
512 skb
= qrtr_alloc_ctrl_packet(&pkt
);
514 pkt
->cmd
= cpu_to_le32(QRTR_TYPE_DEL_CLIENT
);
515 pkt
->client
.node
= cpu_to_le32(ipc
->us
.sq_node
);
516 pkt
->client
.port
= cpu_to_le32(ipc
->us
.sq_port
);
518 skb_set_owner_w(skb
, &ipc
->sk
);
519 qrtr_bcast_enqueue(NULL
, skb
, QRTR_TYPE_DEL_CLIENT
, &ipc
->us
,
523 if (port
== QRTR_PORT_CTRL
)
526 __sock_put(&ipc
->sk
);
528 mutex_lock(&qrtr_port_lock
);
529 idr_remove(&qrtr_ports
, port
);
530 mutex_unlock(&qrtr_port_lock
);
533 /* Assign port number to socket.
535 * Specify port in the integer pointed to by port, and it will be adjusted
536 * on return as necesssary.
539 * 0: Assign ephemeral port in [QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET]
540 * <QRTR_MIN_EPH_SOCKET: Specified; requires CAP_NET_ADMIN
541 * >QRTR_MIN_EPH_SOCKET: Specified; available to all
543 static int qrtr_port_assign(struct qrtr_sock
*ipc
, int *port
)
547 mutex_lock(&qrtr_port_lock
);
549 rc
= idr_alloc(&qrtr_ports
, ipc
,
550 QRTR_MIN_EPH_SOCKET
, QRTR_MAX_EPH_SOCKET
+ 1,
554 } else if (*port
< QRTR_MIN_EPH_SOCKET
&& !capable(CAP_NET_ADMIN
)) {
556 } else if (*port
== QRTR_PORT_CTRL
) {
557 rc
= idr_alloc(&qrtr_ports
, ipc
, 0, 1, GFP_ATOMIC
);
559 rc
= idr_alloc(&qrtr_ports
, ipc
, *port
, *port
+ 1, GFP_ATOMIC
);
563 mutex_unlock(&qrtr_port_lock
);
575 /* Reset all non-control ports */
576 static void qrtr_reset_ports(void)
578 struct qrtr_sock
*ipc
;
581 mutex_lock(&qrtr_port_lock
);
582 idr_for_each_entry(&qrtr_ports
, ipc
, id
) {
583 /* Don't reset control port */
588 ipc
->sk
.sk_err
= ENETRESET
;
589 ipc
->sk
.sk_error_report(&ipc
->sk
);
592 mutex_unlock(&qrtr_port_lock
);
595 /* Bind socket to address.
597 * Socket should be locked upon call.
599 static int __qrtr_bind(struct socket
*sock
,
600 const struct sockaddr_qrtr
*addr
, int zapped
)
602 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
603 struct sock
*sk
= sock
->sk
;
608 if (!zapped
&& addr
->sq_port
== ipc
->us
.sq_port
)
611 port
= addr
->sq_port
;
612 rc
= qrtr_port_assign(ipc
, &port
);
616 /* unbind previous, if any */
618 qrtr_port_remove(ipc
);
619 ipc
->us
.sq_port
= port
;
621 sock_reset_flag(sk
, SOCK_ZAPPED
);
623 /* Notify all open ports about the new controller */
624 if (port
== QRTR_PORT_CTRL
)
630 /* Auto bind to an ephemeral port. */
631 static int qrtr_autobind(struct socket
*sock
)
633 struct sock
*sk
= sock
->sk
;
634 struct sockaddr_qrtr addr
;
636 if (!sock_flag(sk
, SOCK_ZAPPED
))
639 addr
.sq_family
= AF_QIPCRTR
;
640 addr
.sq_node
= qrtr_local_nid
;
643 return __qrtr_bind(sock
, &addr
, 1);
646 /* Bind socket to specified sockaddr. */
647 static int qrtr_bind(struct socket
*sock
, struct sockaddr
*saddr
, int len
)
649 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, saddr
);
650 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
651 struct sock
*sk
= sock
->sk
;
654 if (len
< sizeof(*addr
) || addr
->sq_family
!= AF_QIPCRTR
)
657 if (addr
->sq_node
!= ipc
->us
.sq_node
)
661 rc
= __qrtr_bind(sock
, addr
, sock_flag(sk
, SOCK_ZAPPED
));
667 /* Queue packet to local peer socket. */
668 static int qrtr_local_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
,
669 int type
, struct sockaddr_qrtr
*from
,
670 struct sockaddr_qrtr
*to
)
672 struct qrtr_sock
*ipc
;
675 ipc
= qrtr_port_lookup(to
->sq_port
);
676 if (!ipc
|| &ipc
->sk
== skb
->sk
) { /* do not send to self */
681 cb
= (struct qrtr_cb
*)skb
->cb
;
682 cb
->src_node
= from
->sq_node
;
683 cb
->src_port
= from
->sq_port
;
685 if (sock_queue_rcv_skb(&ipc
->sk
, skb
)) {
696 /* Queue packet for broadcast. */
697 static int qrtr_bcast_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
,
698 int type
, struct sockaddr_qrtr
*from
,
699 struct sockaddr_qrtr
*to
)
701 struct sk_buff
*skbn
;
703 mutex_lock(&qrtr_node_lock
);
704 list_for_each_entry(node
, &qrtr_all_nodes
, item
) {
705 skbn
= skb_clone(skb
, GFP_KERNEL
);
708 skb_set_owner_w(skbn
, skb
->sk
);
709 qrtr_node_enqueue(node
, skbn
, type
, from
, to
);
711 mutex_unlock(&qrtr_node_lock
);
713 qrtr_local_enqueue(node
, skb
, type
, from
, to
);
718 static int qrtr_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
)
720 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, msg
->msg_name
);
721 int (*enqueue_fn
)(struct qrtr_node
*, struct sk_buff
*, int,
722 struct sockaddr_qrtr
*, struct sockaddr_qrtr
*);
723 __le32 qrtr_type
= cpu_to_le32(QRTR_TYPE_DATA
);
724 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
725 struct sock
*sk
= sock
->sk
;
726 struct qrtr_node
*node
;
732 if (msg
->msg_flags
& ~(MSG_DONTWAIT
))
741 if (msg
->msg_namelen
< sizeof(*addr
)) {
746 if (addr
->sq_family
!= AF_QIPCRTR
) {
751 rc
= qrtr_autobind(sock
);
756 } else if (sk
->sk_state
== TCP_ESTABLISHED
) {
764 if (addr
->sq_node
== QRTR_NODE_BCAST
) {
765 enqueue_fn
= qrtr_bcast_enqueue
;
766 if (addr
->sq_port
!= QRTR_PORT_CTRL
) {
770 } else if (addr
->sq_node
== ipc
->us
.sq_node
) {
771 enqueue_fn
= qrtr_local_enqueue
;
773 enqueue_fn
= qrtr_node_enqueue
;
774 node
= qrtr_node_lookup(addr
->sq_node
);
781 plen
= (len
+ 3) & ~3;
782 skb
= sock_alloc_send_skb(sk
, plen
+ QRTR_HDR_MAX_SIZE
,
783 msg
->msg_flags
& MSG_DONTWAIT
, &rc
);
787 skb_reserve(skb
, QRTR_HDR_MAX_SIZE
);
789 rc
= memcpy_from_msg(skb_put(skb
, len
), msg
, len
);
795 if (ipc
->us
.sq_port
== QRTR_PORT_CTRL
) {
802 /* control messages already require the type as 'command' */
803 skb_copy_bits(skb
, 0, &qrtr_type
, 4);
806 type
= le32_to_cpu(qrtr_type
);
807 rc
= enqueue_fn(node
, skb
, type
, &ipc
->us
, addr
);
812 qrtr_node_release(node
);
818 static int qrtr_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
819 size_t size
, int flags
)
821 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, msg
->msg_name
);
822 struct sock
*sk
= sock
->sk
;
829 if (sock_flag(sk
, SOCK_ZAPPED
)) {
831 return -EADDRNOTAVAIL
;
834 skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
,
835 flags
& MSG_DONTWAIT
, &rc
);
844 msg
->msg_flags
|= MSG_TRUNC
;
847 rc
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
853 cb
= (struct qrtr_cb
*)skb
->cb
;
854 addr
->sq_family
= AF_QIPCRTR
;
855 addr
->sq_node
= cb
->src_node
;
856 addr
->sq_port
= cb
->src_port
;
857 msg
->msg_namelen
= sizeof(*addr
);
861 skb_free_datagram(sk
, skb
);
867 static int qrtr_connect(struct socket
*sock
, struct sockaddr
*saddr
,
870 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, saddr
);
871 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
872 struct sock
*sk
= sock
->sk
;
875 if (len
< sizeof(*addr
) || addr
->sq_family
!= AF_QIPCRTR
)
880 sk
->sk_state
= TCP_CLOSE
;
881 sock
->state
= SS_UNCONNECTED
;
883 rc
= qrtr_autobind(sock
);
890 sock
->state
= SS_CONNECTED
;
891 sk
->sk_state
= TCP_ESTABLISHED
;
898 static int qrtr_getname(struct socket
*sock
, struct sockaddr
*saddr
,
901 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
902 struct sockaddr_qrtr qaddr
;
903 struct sock
*sk
= sock
->sk
;
907 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
918 qaddr
.sq_family
= AF_QIPCRTR
;
920 memcpy(saddr
, &qaddr
, sizeof(qaddr
));
922 return sizeof(qaddr
);
925 static int qrtr_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
927 void __user
*argp
= (void __user
*)arg
;
928 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
929 struct sock
*sk
= sock
->sk
;
930 struct sockaddr_qrtr
*sq
;
940 len
= sk
->sk_sndbuf
- sk_wmem_alloc_get(sk
);
943 rc
= put_user(len
, (int __user
*)argp
);
946 skb
= skb_peek(&sk
->sk_receive_queue
);
949 rc
= put_user(len
, (int __user
*)argp
);
952 if (copy_from_user(&ifr
, argp
, sizeof(ifr
))) {
957 sq
= (struct sockaddr_qrtr
*)&ifr
.ifr_addr
;
959 if (copy_to_user(argp
, &ifr
, sizeof(ifr
))) {
985 static int qrtr_release(struct socket
*sock
)
987 struct sock
*sk
= sock
->sk
;
988 struct qrtr_sock
*ipc
;
996 sk
->sk_shutdown
= SHUTDOWN_MASK
;
997 if (!sock_flag(sk
, SOCK_DEAD
))
998 sk
->sk_state_change(sk
);
1000 sock_set_flag(sk
, SOCK_DEAD
);
1003 if (!sock_flag(sk
, SOCK_ZAPPED
))
1004 qrtr_port_remove(ipc
);
1006 skb_queue_purge(&sk
->sk_receive_queue
);
1014 static const struct proto_ops qrtr_proto_ops
= {
1015 .owner
= THIS_MODULE
,
1016 .family
= AF_QIPCRTR
,
1018 .connect
= qrtr_connect
,
1019 .socketpair
= sock_no_socketpair
,
1020 .accept
= sock_no_accept
,
1021 .listen
= sock_no_listen
,
1022 .sendmsg
= qrtr_sendmsg
,
1023 .recvmsg
= qrtr_recvmsg
,
1024 .getname
= qrtr_getname
,
1025 .ioctl
= qrtr_ioctl
,
1026 .gettstamp
= sock_gettstamp
,
1027 .poll
= datagram_poll
,
1028 .shutdown
= sock_no_shutdown
,
1029 .setsockopt
= sock_no_setsockopt
,
1030 .getsockopt
= sock_no_getsockopt
,
1031 .release
= qrtr_release
,
1032 .mmap
= sock_no_mmap
,
1033 .sendpage
= sock_no_sendpage
,
1036 static struct proto qrtr_proto
= {
1038 .owner
= THIS_MODULE
,
1039 .obj_size
= sizeof(struct qrtr_sock
),
1042 static int qrtr_create(struct net
*net
, struct socket
*sock
,
1043 int protocol
, int kern
)
1045 struct qrtr_sock
*ipc
;
1048 if (sock
->type
!= SOCK_DGRAM
)
1051 sk
= sk_alloc(net
, AF_QIPCRTR
, GFP_KERNEL
, &qrtr_proto
, kern
);
1055 sock_set_flag(sk
, SOCK_ZAPPED
);
1057 sock_init_data(sock
, sk
);
1058 sock
->ops
= &qrtr_proto_ops
;
1061 ipc
->us
.sq_family
= AF_QIPCRTR
;
1062 ipc
->us
.sq_node
= qrtr_local_nid
;
1063 ipc
->us
.sq_port
= 0;
1068 static const struct nla_policy qrtr_policy
[IFA_MAX
+ 1] = {
1069 [IFA_LOCAL
] = { .type
= NLA_U32
},
1072 static int qrtr_addr_doit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1073 struct netlink_ext_ack
*extack
)
1075 struct nlattr
*tb
[IFA_MAX
+ 1];
1076 struct ifaddrmsg
*ifm
;
1079 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
1082 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
1087 rc
= nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFA_MAX
,
1088 qrtr_policy
, extack
);
1092 ifm
= nlmsg_data(nlh
);
1096 qrtr_local_nid
= nla_get_u32(tb
[IFA_LOCAL
]);
1100 static const struct net_proto_family qrtr_family
= {
1101 .owner
= THIS_MODULE
,
1102 .family
= AF_QIPCRTR
,
1103 .create
= qrtr_create
,
1106 static int __init
qrtr_proto_init(void)
1110 rc
= proto_register(&qrtr_proto
, 1);
1114 rc
= sock_register(&qrtr_family
);
1116 proto_unregister(&qrtr_proto
);
1120 rc
= rtnl_register_module(THIS_MODULE
, PF_QIPCRTR
, RTM_NEWADDR
, qrtr_addr_doit
, NULL
, 0);
1122 sock_unregister(qrtr_family
.family
);
1123 proto_unregister(&qrtr_proto
);
1128 postcore_initcall(qrtr_proto_init
);
1130 static void __exit
qrtr_proto_fini(void)
1132 rtnl_unregister(PF_QIPCRTR
, RTM_NEWADDR
);
1133 sock_unregister(qrtr_family
.family
);
1134 proto_unregister(&qrtr_proto
);
1136 module_exit(qrtr_proto_fini
);
1138 MODULE_DESCRIPTION("Qualcomm IPC-router driver");
1139 MODULE_LICENSE("GPL v2");
1140 MODULE_ALIAS_NETPROTO(PF_QIPCRTR
);