2 * Copyright (c) 2015, Sony Mobile Communications Inc.
3 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 #include <linux/module.h>
15 #include <linux/netlink.h>
16 #include <linux/qrtr.h>
17 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
23 #define QRTR_PROTO_VER 1
26 #define QRTR_MIN_EPH_SOCKET 0x4000
27 #define QRTR_MAX_EPH_SOCKET 0x7fff
33 QRTR_TYPE_NEW_SERVER
= 4,
34 QRTR_TYPE_DEL_SERVER
= 5,
35 QRTR_TYPE_DEL_CLIENT
= 6,
36 QRTR_TYPE_RESUME_TX
= 7,
42 * struct qrtr_hdr - (I|R)PCrouter packet header
43 * @version: protocol version
44 * @type: packet type; one of QRTR_TYPE_*
45 * @src_node_id: source node
46 * @src_port_id: source port
47 * @confirm_rx: boolean; whether a resume-tx packet should be send in reply
48 * @size: length of packet, excluding this header
49 * @dst_node_id: destination node
50 * @dst_port_id: destination port
63 #define QRTR_HDR_SIZE sizeof(struct qrtr_hdr)
64 #define QRTR_NODE_BCAST ((unsigned int)-1)
65 #define QRTR_PORT_CTRL ((unsigned int)-2)
68 /* WARNING: sk must be the first member */
70 struct sockaddr_qrtr us
;
71 struct sockaddr_qrtr peer
;
74 static inline struct qrtr_sock
*qrtr_sk(struct sock
*sk
)
76 BUILD_BUG_ON(offsetof(struct qrtr_sock
, sk
) != 0);
77 return container_of(sk
, struct qrtr_sock
, sk
);
80 static unsigned int qrtr_local_nid
= -1;
83 static RADIX_TREE(qrtr_nodes
, GFP_KERNEL
);
85 static LIST_HEAD(qrtr_all_nodes
);
86 /* lock for qrtr_nodes, qrtr_all_nodes and node reference */
87 static DEFINE_MUTEX(qrtr_node_lock
);
89 /* local port allocation management */
90 static DEFINE_IDR(qrtr_ports
);
91 static DEFINE_MUTEX(qrtr_port_lock
);
94 * struct qrtr_node - endpoint node
95 * @ep_lock: lock for endpoint management and callbacks
97 * @ref: reference count for node
99 * @rx_queue: receive queue
100 * @work: scheduled work struct for recv work
101 * @item: list item for broadcast list
104 struct mutex ep_lock
;
105 struct qrtr_endpoint
*ep
;
109 struct sk_buff_head rx_queue
;
110 struct work_struct work
;
111 struct list_head item
;
114 static int qrtr_local_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
);
115 static int qrtr_bcast_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
);
117 /* Release node resources and free the node.
119 * Do not call directly, use qrtr_node_release. To be used with
120 * kref_put_mutex. As such, the node mutex is expected to be locked on call.
122 static void __qrtr_node_release(struct kref
*kref
)
124 struct qrtr_node
*node
= container_of(kref
, struct qrtr_node
, ref
);
126 if (node
->nid
!= QRTR_EP_NID_AUTO
)
127 radix_tree_delete(&qrtr_nodes
, node
->nid
);
129 list_del(&node
->item
);
130 mutex_unlock(&qrtr_node_lock
);
132 skb_queue_purge(&node
->rx_queue
);
136 /* Increment reference to node. */
137 static struct qrtr_node
*qrtr_node_acquire(struct qrtr_node
*node
)
140 kref_get(&node
->ref
);
144 /* Decrement reference to node and release as necessary. */
145 static void qrtr_node_release(struct qrtr_node
*node
)
149 kref_put_mutex(&node
->ref
, __qrtr_node_release
, &qrtr_node_lock
);
152 /* Pass an outgoing packet socket buffer to the endpoint driver. */
153 static int qrtr_node_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
)
157 mutex_lock(&node
->ep_lock
);
159 rc
= node
->ep
->xmit(node
->ep
, skb
);
162 mutex_unlock(&node
->ep_lock
);
167 /* Lookup node by id.
169 * callers must release with qrtr_node_release()
171 static struct qrtr_node
*qrtr_node_lookup(unsigned int nid
)
173 struct qrtr_node
*node
;
175 mutex_lock(&qrtr_node_lock
);
176 node
= radix_tree_lookup(&qrtr_nodes
, nid
);
177 node
= qrtr_node_acquire(node
);
178 mutex_unlock(&qrtr_node_lock
);
183 /* Assign node id to node.
185 * This is mostly useful for automatic node id assignment, based on
186 * the source id in the incoming packet.
188 static void qrtr_node_assign(struct qrtr_node
*node
, unsigned int nid
)
190 if (node
->nid
!= QRTR_EP_NID_AUTO
|| nid
== QRTR_EP_NID_AUTO
)
193 mutex_lock(&qrtr_node_lock
);
194 radix_tree_insert(&qrtr_nodes
, nid
, node
);
196 mutex_unlock(&qrtr_node_lock
);
200 * qrtr_endpoint_post() - post incoming data
201 * @ep: endpoint handle
202 * @data: data pointer
203 * @len: size of data in bytes
205 * Return: 0 on success; negative error code on failure
207 int qrtr_endpoint_post(struct qrtr_endpoint
*ep
, const void *data
, size_t len
)
209 struct qrtr_node
*node
= ep
->node
;
210 const struct qrtr_hdr
*phdr
= data
;
218 if (len
< QRTR_HDR_SIZE
|| len
& 3)
221 ver
= le32_to_cpu(phdr
->version
);
222 size
= le32_to_cpu(phdr
->size
);
223 type
= le32_to_cpu(phdr
->type
);
224 dst
= le32_to_cpu(phdr
->dst_port_id
);
226 psize
= (size
+ 3) & ~3;
228 if (ver
!= QRTR_PROTO_VER
)
231 if (len
!= psize
+ QRTR_HDR_SIZE
)
234 if (dst
!= QRTR_PORT_CTRL
&& type
!= QRTR_TYPE_DATA
)
237 skb
= netdev_alloc_skb(NULL
, len
);
241 skb_reset_transport_header(skb
);
242 skb_put_data(skb
, data
, len
);
244 skb_queue_tail(&node
->rx_queue
, skb
);
245 schedule_work(&node
->work
);
249 EXPORT_SYMBOL_GPL(qrtr_endpoint_post
);
251 static struct sk_buff
*qrtr_alloc_ctrl_packet(u32 type
, size_t pkt_len
,
252 u32 src_node
, u32 dst_node
)
254 struct qrtr_hdr
*hdr
;
257 skb
= alloc_skb(QRTR_HDR_SIZE
+ pkt_len
, GFP_KERNEL
);
260 skb_reset_transport_header(skb
);
262 hdr
= skb_put(skb
, QRTR_HDR_SIZE
);
263 hdr
->version
= cpu_to_le32(QRTR_PROTO_VER
);
264 hdr
->type
= cpu_to_le32(type
);
265 hdr
->src_node_id
= cpu_to_le32(src_node
);
266 hdr
->src_port_id
= cpu_to_le32(QRTR_PORT_CTRL
);
267 hdr
->confirm_rx
= cpu_to_le32(0);
268 hdr
->size
= cpu_to_le32(pkt_len
);
269 hdr
->dst_node_id
= cpu_to_le32(dst_node
);
270 hdr
->dst_port_id
= cpu_to_le32(QRTR_PORT_CTRL
);
275 /* Allocate and construct a resume-tx packet. */
276 static struct sk_buff
*qrtr_alloc_resume_tx(u32 src_node
,
277 u32 dst_node
, u32 port
)
279 const int pkt_len
= 20;
283 skb
= qrtr_alloc_ctrl_packet(QRTR_TYPE_RESUME_TX
, pkt_len
,
288 buf
= skb_put_zero(skb
, pkt_len
);
289 buf
[0] = cpu_to_le32(QRTR_TYPE_RESUME_TX
);
290 buf
[1] = cpu_to_le32(src_node
);
291 buf
[2] = cpu_to_le32(port
);
296 /* Allocate and construct a BYE message to signal remote termination */
297 static struct sk_buff
*qrtr_alloc_local_bye(u32 src_node
)
299 const int pkt_len
= 20;
303 skb
= qrtr_alloc_ctrl_packet(QRTR_TYPE_BYE
, pkt_len
,
304 src_node
, qrtr_local_nid
);
308 buf
= skb_put_zero(skb
, pkt_len
);
309 buf
[0] = cpu_to_le32(QRTR_TYPE_BYE
);
314 static struct sk_buff
*qrtr_alloc_del_client(struct sockaddr_qrtr
*sq
)
316 const int pkt_len
= 20;
320 skb
= qrtr_alloc_ctrl_packet(QRTR_TYPE_DEL_CLIENT
, pkt_len
,
321 sq
->sq_node
, QRTR_NODE_BCAST
);
325 buf
= skb_put_zero(skb
, pkt_len
);
326 buf
[0] = cpu_to_le32(QRTR_TYPE_DEL_CLIENT
);
327 buf
[1] = cpu_to_le32(sq
->sq_node
);
328 buf
[2] = cpu_to_le32(sq
->sq_port
);
333 static struct qrtr_sock
*qrtr_port_lookup(int port
);
334 static void qrtr_port_put(struct qrtr_sock
*ipc
);
336 /* Handle and route a received packet.
338 * This will auto-reply with resume-tx packet as necessary.
340 static void qrtr_node_rx_work(struct work_struct
*work
)
342 struct qrtr_node
*node
= container_of(work
, struct qrtr_node
, work
);
345 while ((skb
= skb_dequeue(&node
->rx_queue
)) != NULL
) {
346 const struct qrtr_hdr
*phdr
;
347 u32 dst_node
, dst_port
;
348 struct qrtr_sock
*ipc
;
352 phdr
= (const struct qrtr_hdr
*)skb_transport_header(skb
);
353 src_node
= le32_to_cpu(phdr
->src_node_id
);
354 dst_node
= le32_to_cpu(phdr
->dst_node_id
);
355 dst_port
= le32_to_cpu(phdr
->dst_port_id
);
356 confirm
= !!phdr
->confirm_rx
;
358 qrtr_node_assign(node
, src_node
);
360 ipc
= qrtr_port_lookup(dst_port
);
364 if (sock_queue_rcv_skb(&ipc
->sk
, skb
))
371 skb
= qrtr_alloc_resume_tx(dst_node
, node
->nid
, dst_port
);
374 if (qrtr_node_enqueue(node
, skb
))
381 * qrtr_endpoint_register() - register a new endpoint
382 * @ep: endpoint to register
383 * @nid: desired node id; may be QRTR_EP_NID_AUTO for auto-assignment
384 * Return: 0 on success; negative error code on failure
386 * The specified endpoint must have the xmit function pointer set on call.
388 int qrtr_endpoint_register(struct qrtr_endpoint
*ep
, unsigned int nid
)
390 struct qrtr_node
*node
;
392 if (!ep
|| !ep
->xmit
)
395 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
399 INIT_WORK(&node
->work
, qrtr_node_rx_work
);
400 kref_init(&node
->ref
);
401 mutex_init(&node
->ep_lock
);
402 skb_queue_head_init(&node
->rx_queue
);
403 node
->nid
= QRTR_EP_NID_AUTO
;
406 qrtr_node_assign(node
, nid
);
408 mutex_lock(&qrtr_node_lock
);
409 list_add(&node
->item
, &qrtr_all_nodes
);
410 mutex_unlock(&qrtr_node_lock
);
415 EXPORT_SYMBOL_GPL(qrtr_endpoint_register
);
418 * qrtr_endpoint_unregister - unregister endpoint
419 * @ep: endpoint to unregister
421 void qrtr_endpoint_unregister(struct qrtr_endpoint
*ep
)
423 struct qrtr_node
*node
= ep
->node
;
426 mutex_lock(&node
->ep_lock
);
428 mutex_unlock(&node
->ep_lock
);
430 /* Notify the local controller about the event */
431 skb
= qrtr_alloc_local_bye(node
->nid
);
433 qrtr_local_enqueue(NULL
, skb
);
435 qrtr_node_release(node
);
438 EXPORT_SYMBOL_GPL(qrtr_endpoint_unregister
);
440 /* Lookup socket by port.
442 * Callers must release with qrtr_port_put()
444 static struct qrtr_sock
*qrtr_port_lookup(int port
)
446 struct qrtr_sock
*ipc
;
448 if (port
== QRTR_PORT_CTRL
)
451 mutex_lock(&qrtr_port_lock
);
452 ipc
= idr_find(&qrtr_ports
, port
);
455 mutex_unlock(&qrtr_port_lock
);
460 /* Release acquired socket. */
461 static void qrtr_port_put(struct qrtr_sock
*ipc
)
466 /* Remove port assignment. */
467 static void qrtr_port_remove(struct qrtr_sock
*ipc
)
470 int port
= ipc
->us
.sq_port
;
472 skb
= qrtr_alloc_del_client(&ipc
->us
);
474 skb_set_owner_w(skb
, &ipc
->sk
);
475 qrtr_bcast_enqueue(NULL
, skb
);
478 if (port
== QRTR_PORT_CTRL
)
481 __sock_put(&ipc
->sk
);
483 mutex_lock(&qrtr_port_lock
);
484 idr_remove(&qrtr_ports
, port
);
485 mutex_unlock(&qrtr_port_lock
);
488 /* Assign port number to socket.
490 * Specify port in the integer pointed to by port, and it will be adjusted
491 * on return as necesssary.
494 * 0: Assign ephemeral port in [QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET]
495 * <QRTR_MIN_EPH_SOCKET: Specified; requires CAP_NET_ADMIN
496 * >QRTR_MIN_EPH_SOCKET: Specified; available to all
498 static int qrtr_port_assign(struct qrtr_sock
*ipc
, int *port
)
502 mutex_lock(&qrtr_port_lock
);
504 rc
= idr_alloc(&qrtr_ports
, ipc
,
505 QRTR_MIN_EPH_SOCKET
, QRTR_MAX_EPH_SOCKET
+ 1,
509 } else if (*port
< QRTR_MIN_EPH_SOCKET
&& !capable(CAP_NET_ADMIN
)) {
511 } else if (*port
== QRTR_PORT_CTRL
) {
512 rc
= idr_alloc(&qrtr_ports
, ipc
, 0, 1, GFP_ATOMIC
);
514 rc
= idr_alloc(&qrtr_ports
, ipc
, *port
, *port
+ 1, GFP_ATOMIC
);
518 mutex_unlock(&qrtr_port_lock
);
530 /* Reset all non-control ports */
531 static void qrtr_reset_ports(void)
533 struct qrtr_sock
*ipc
;
536 mutex_lock(&qrtr_port_lock
);
537 idr_for_each_entry(&qrtr_ports
, ipc
, id
) {
538 /* Don't reset control port */
543 ipc
->sk
.sk_err
= ENETRESET
;
544 wake_up_interruptible(sk_sleep(&ipc
->sk
));
547 mutex_unlock(&qrtr_port_lock
);
550 /* Bind socket to address.
552 * Socket should be locked upon call.
554 static int __qrtr_bind(struct socket
*sock
,
555 const struct sockaddr_qrtr
*addr
, int zapped
)
557 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
558 struct sock
*sk
= sock
->sk
;
563 if (!zapped
&& addr
->sq_port
== ipc
->us
.sq_port
)
566 port
= addr
->sq_port
;
567 rc
= qrtr_port_assign(ipc
, &port
);
571 /* unbind previous, if any */
573 qrtr_port_remove(ipc
);
574 ipc
->us
.sq_port
= port
;
576 sock_reset_flag(sk
, SOCK_ZAPPED
);
578 /* Notify all open ports about the new controller */
579 if (port
== QRTR_PORT_CTRL
)
585 /* Auto bind to an ephemeral port. */
586 static int qrtr_autobind(struct socket
*sock
)
588 struct sock
*sk
= sock
->sk
;
589 struct sockaddr_qrtr addr
;
591 if (!sock_flag(sk
, SOCK_ZAPPED
))
594 addr
.sq_family
= AF_QIPCRTR
;
595 addr
.sq_node
= qrtr_local_nid
;
598 return __qrtr_bind(sock
, &addr
, 1);
601 /* Bind socket to specified sockaddr. */
602 static int qrtr_bind(struct socket
*sock
, struct sockaddr
*saddr
, int len
)
604 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, saddr
);
605 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
606 struct sock
*sk
= sock
->sk
;
609 if (len
< sizeof(*addr
) || addr
->sq_family
!= AF_QIPCRTR
)
612 if (addr
->sq_node
!= ipc
->us
.sq_node
)
616 rc
= __qrtr_bind(sock
, addr
, sock_flag(sk
, SOCK_ZAPPED
));
622 /* Queue packet to local peer socket. */
623 static int qrtr_local_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
)
625 const struct qrtr_hdr
*phdr
;
626 struct qrtr_sock
*ipc
;
628 phdr
= (const struct qrtr_hdr
*)skb_transport_header(skb
);
630 ipc
= qrtr_port_lookup(le32_to_cpu(phdr
->dst_port_id
));
631 if (!ipc
|| &ipc
->sk
== skb
->sk
) { /* do not send to self */
636 if (sock_queue_rcv_skb(&ipc
->sk
, skb
)) {
647 /* Queue packet for broadcast. */
648 static int qrtr_bcast_enqueue(struct qrtr_node
*node
, struct sk_buff
*skb
)
650 struct sk_buff
*skbn
;
652 mutex_lock(&qrtr_node_lock
);
653 list_for_each_entry(node
, &qrtr_all_nodes
, item
) {
654 skbn
= skb_clone(skb
, GFP_KERNEL
);
657 skb_set_owner_w(skbn
, skb
->sk
);
658 qrtr_node_enqueue(node
, skbn
);
660 mutex_unlock(&qrtr_node_lock
);
662 qrtr_local_enqueue(node
, skb
);
667 static int qrtr_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
)
669 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, msg
->msg_name
);
670 int (*enqueue_fn
)(struct qrtr_node
*, struct sk_buff
*);
671 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
672 struct sock
*sk
= sock
->sk
;
673 struct qrtr_node
*node
;
674 struct qrtr_hdr
*hdr
;
679 if (msg
->msg_flags
& ~(MSG_DONTWAIT
))
688 if (msg
->msg_namelen
< sizeof(*addr
)) {
693 if (addr
->sq_family
!= AF_QIPCRTR
) {
698 rc
= qrtr_autobind(sock
);
703 } else if (sk
->sk_state
== TCP_ESTABLISHED
) {
711 if (addr
->sq_node
== QRTR_NODE_BCAST
) {
712 enqueue_fn
= qrtr_bcast_enqueue
;
713 } else if (addr
->sq_node
== ipc
->us
.sq_node
) {
714 enqueue_fn
= qrtr_local_enqueue
;
716 enqueue_fn
= qrtr_node_enqueue
;
717 node
= qrtr_node_lookup(addr
->sq_node
);
724 plen
= (len
+ 3) & ~3;
725 skb
= sock_alloc_send_skb(sk
, plen
+ QRTR_HDR_SIZE
,
726 msg
->msg_flags
& MSG_DONTWAIT
, &rc
);
730 skb_reset_transport_header(skb
);
731 skb_put(skb
, len
+ QRTR_HDR_SIZE
);
733 hdr
= (struct qrtr_hdr
*)skb_transport_header(skb
);
734 hdr
->version
= cpu_to_le32(QRTR_PROTO_VER
);
735 hdr
->src_node_id
= cpu_to_le32(ipc
->us
.sq_node
);
736 hdr
->src_port_id
= cpu_to_le32(ipc
->us
.sq_port
);
737 hdr
->confirm_rx
= cpu_to_le32(0);
738 hdr
->size
= cpu_to_le32(len
);
739 hdr
->dst_node_id
= cpu_to_le32(addr
->sq_node
);
740 hdr
->dst_port_id
= cpu_to_le32(addr
->sq_port
);
742 rc
= skb_copy_datagram_from_iter(skb
, QRTR_HDR_SIZE
,
743 &msg
->msg_iter
, len
);
750 rc
= skb_pad(skb
, plen
- len
);
753 skb_put(skb
, plen
- len
);
756 if (ipc
->us
.sq_port
== QRTR_PORT_CTRL
) {
763 /* control messages already require the type as 'command' */
764 skb_copy_bits(skb
, QRTR_HDR_SIZE
, &hdr
->type
, 4);
766 hdr
->type
= cpu_to_le32(QRTR_TYPE_DATA
);
769 rc
= enqueue_fn(node
, skb
);
774 qrtr_node_release(node
);
780 static int qrtr_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
781 size_t size
, int flags
)
783 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, msg
->msg_name
);
784 const struct qrtr_hdr
*phdr
;
785 struct sock
*sk
= sock
->sk
;
791 if (sock_flag(sk
, SOCK_ZAPPED
)) {
793 return -EADDRNOTAVAIL
;
796 skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
,
797 flags
& MSG_DONTWAIT
, &rc
);
803 phdr
= (const struct qrtr_hdr
*)skb_transport_header(skb
);
804 copied
= le32_to_cpu(phdr
->size
);
807 msg
->msg_flags
|= MSG_TRUNC
;
810 rc
= skb_copy_datagram_msg(skb
, QRTR_HDR_SIZE
, msg
, copied
);
816 addr
->sq_family
= AF_QIPCRTR
;
817 addr
->sq_node
= le32_to_cpu(phdr
->src_node_id
);
818 addr
->sq_port
= le32_to_cpu(phdr
->src_port_id
);
819 msg
->msg_namelen
= sizeof(*addr
);
823 skb_free_datagram(sk
, skb
);
829 static int qrtr_connect(struct socket
*sock
, struct sockaddr
*saddr
,
832 DECLARE_SOCKADDR(struct sockaddr_qrtr
*, addr
, saddr
);
833 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
834 struct sock
*sk
= sock
->sk
;
837 if (len
< sizeof(*addr
) || addr
->sq_family
!= AF_QIPCRTR
)
842 sk
->sk_state
= TCP_CLOSE
;
843 sock
->state
= SS_UNCONNECTED
;
845 rc
= qrtr_autobind(sock
);
852 sock
->state
= SS_CONNECTED
;
853 sk
->sk_state
= TCP_ESTABLISHED
;
860 static int qrtr_getname(struct socket
*sock
, struct sockaddr
*saddr
,
863 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
864 struct sockaddr_qrtr qaddr
;
865 struct sock
*sk
= sock
->sk
;
869 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
880 *len
= sizeof(qaddr
);
881 qaddr
.sq_family
= AF_QIPCRTR
;
883 memcpy(saddr
, &qaddr
, sizeof(qaddr
));
888 static int qrtr_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
890 void __user
*argp
= (void __user
*)arg
;
891 struct qrtr_sock
*ipc
= qrtr_sk(sock
->sk
);
892 struct sock
*sk
= sock
->sk
;
893 struct sockaddr_qrtr
*sq
;
903 len
= sk
->sk_sndbuf
- sk_wmem_alloc_get(sk
);
906 rc
= put_user(len
, (int __user
*)argp
);
909 skb
= skb_peek(&sk
->sk_receive_queue
);
911 len
= skb
->len
- QRTR_HDR_SIZE
;
912 rc
= put_user(len
, (int __user
*)argp
);
915 if (copy_from_user(&ifr
, argp
, sizeof(ifr
))) {
920 sq
= (struct sockaddr_qrtr
*)&ifr
.ifr_addr
;
922 if (copy_to_user(argp
, &ifr
, sizeof(ifr
))) {
928 rc
= sock_get_timestamp(sk
, argp
);
951 static int qrtr_release(struct socket
*sock
)
953 struct sock
*sk
= sock
->sk
;
954 struct qrtr_sock
*ipc
;
962 sk
->sk_shutdown
= SHUTDOWN_MASK
;
963 if (!sock_flag(sk
, SOCK_DEAD
))
964 sk
->sk_state_change(sk
);
966 sock_set_flag(sk
, SOCK_DEAD
);
969 if (!sock_flag(sk
, SOCK_ZAPPED
))
970 qrtr_port_remove(ipc
);
972 skb_queue_purge(&sk
->sk_receive_queue
);
980 static const struct proto_ops qrtr_proto_ops
= {
981 .owner
= THIS_MODULE
,
982 .family
= AF_QIPCRTR
,
984 .connect
= qrtr_connect
,
985 .socketpair
= sock_no_socketpair
,
986 .accept
= sock_no_accept
,
987 .listen
= sock_no_listen
,
988 .sendmsg
= qrtr_sendmsg
,
989 .recvmsg
= qrtr_recvmsg
,
990 .getname
= qrtr_getname
,
992 .poll
= datagram_poll
,
993 .shutdown
= sock_no_shutdown
,
994 .setsockopt
= sock_no_setsockopt
,
995 .getsockopt
= sock_no_getsockopt
,
996 .release
= qrtr_release
,
997 .mmap
= sock_no_mmap
,
998 .sendpage
= sock_no_sendpage
,
1001 static struct proto qrtr_proto
= {
1003 .owner
= THIS_MODULE
,
1004 .obj_size
= sizeof(struct qrtr_sock
),
1007 static int qrtr_create(struct net
*net
, struct socket
*sock
,
1008 int protocol
, int kern
)
1010 struct qrtr_sock
*ipc
;
1013 if (sock
->type
!= SOCK_DGRAM
)
1016 sk
= sk_alloc(net
, AF_QIPCRTR
, GFP_KERNEL
, &qrtr_proto
, kern
);
1020 sock_set_flag(sk
, SOCK_ZAPPED
);
1022 sock_init_data(sock
, sk
);
1023 sock
->ops
= &qrtr_proto_ops
;
1026 ipc
->us
.sq_family
= AF_QIPCRTR
;
1027 ipc
->us
.sq_node
= qrtr_local_nid
;
1028 ipc
->us
.sq_port
= 0;
1033 static const struct nla_policy qrtr_policy
[IFA_MAX
+ 1] = {
1034 [IFA_LOCAL
] = { .type
= NLA_U32
},
1037 static int qrtr_addr_doit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1038 struct netlink_ext_ack
*extack
)
1040 struct nlattr
*tb
[IFA_MAX
+ 1];
1041 struct ifaddrmsg
*ifm
;
1044 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
1047 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
1052 rc
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, qrtr_policy
, extack
);
1056 ifm
= nlmsg_data(nlh
);
1060 qrtr_local_nid
= nla_get_u32(tb
[IFA_LOCAL
]);
1064 static const struct net_proto_family qrtr_family
= {
1065 .owner
= THIS_MODULE
,
1066 .family
= AF_QIPCRTR
,
1067 .create
= qrtr_create
,
1070 static int __init
qrtr_proto_init(void)
1074 rc
= proto_register(&qrtr_proto
, 1);
1078 rc
= sock_register(&qrtr_family
);
1080 proto_unregister(&qrtr_proto
);
1084 rtnl_register(PF_QIPCRTR
, RTM_NEWADDR
, qrtr_addr_doit
, NULL
, NULL
);
1088 module_init(qrtr_proto_init
);
1090 static void __exit
qrtr_proto_fini(void)
1092 rtnl_unregister(PF_QIPCRTR
, RTM_NEWADDR
);
1093 sock_unregister(qrtr_family
.family
);
1094 proto_unregister(&qrtr_proto
);
1096 module_exit(qrtr_proto_fini
);
1098 MODULE_DESCRIPTION("Qualcomm IPC-router driver");
1099 MODULE_LICENSE("GPL v2");