xref: /openbmc/linux/net/rxrpc/recvmsg.c (revision c0783818)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
28c3e34a4SDavid Howells /* RxRPC recvmsg() implementation
38c3e34a4SDavid Howells  *
48c3e34a4SDavid Howells  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
58c3e34a4SDavid Howells  * Written by David Howells (dhowells@redhat.com)
68c3e34a4SDavid Howells  */
78c3e34a4SDavid Howells 
88c3e34a4SDavid Howells #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
98c3e34a4SDavid Howells 
108c3e34a4SDavid Howells #include <linux/net.h>
118c3e34a4SDavid Howells #include <linux/skbuff.h>
128c3e34a4SDavid Howells #include <linux/export.h>
13174cd4b1SIngo Molnar #include <linux/sched/signal.h>
14174cd4b1SIngo Molnar 
158c3e34a4SDavid Howells #include <net/sock.h>
168c3e34a4SDavid Howells #include <net/af_rxrpc.h>
178c3e34a4SDavid Howells #include "ar-internal.h"
188c3e34a4SDavid Howells 
198c3e34a4SDavid Howells /*
20248f219cSDavid Howells  * Post a call for attention by the socket or kernel service.  Further
21248f219cSDavid Howells  * notifications are suppressed by putting recvmsg_link on a dummy queue.
22248f219cSDavid Howells  */
rxrpc_notify_socket(struct rxrpc_call * call)23248f219cSDavid Howells void rxrpc_notify_socket(struct rxrpc_call *call)
24248f219cSDavid Howells {
25248f219cSDavid Howells 	struct rxrpc_sock *rx;
26248f219cSDavid Howells 	struct sock *sk;
27248f219cSDavid Howells 
28248f219cSDavid Howells 	_enter("%d", call->debug_id);
29248f219cSDavid Howells 
30248f219cSDavid Howells 	if (!list_empty(&call->recvmsg_link))
31248f219cSDavid Howells 		return;
32248f219cSDavid Howells 
33248f219cSDavid Howells 	rcu_read_lock();
34248f219cSDavid Howells 
35248f219cSDavid Howells 	rx = rcu_dereference(call->socket);
36248f219cSDavid Howells 	sk = &rx->sk;
37248f219cSDavid Howells 	if (rx && sk->sk_state < RXRPC_CLOSE) {
38248f219cSDavid Howells 		if (call->notify_rx) {
393dd9c8b5SDavid Howells 			spin_lock(&call->notify_lock);
40248f219cSDavid Howells 			call->notify_rx(sk, call, call->user_call_ID);
413dd9c8b5SDavid Howells 			spin_unlock(&call->notify_lock);
42248f219cSDavid Howells 		} else {
43223f5901SDavid Howells 			spin_lock(&rx->recvmsg_lock);
44248f219cSDavid Howells 			if (list_empty(&call->recvmsg_link)) {
45cb0fc0c9SDavid Howells 				rxrpc_get_call(call, rxrpc_call_get_notify_socket);
46248f219cSDavid Howells 				list_add_tail(&call->recvmsg_link, &rx->recvmsg_q);
47248f219cSDavid Howells 			}
48223f5901SDavid Howells 			spin_unlock(&rx->recvmsg_lock);
49248f219cSDavid Howells 
50248f219cSDavid Howells 			if (!sock_flag(sk, SOCK_DEAD)) {
51248f219cSDavid Howells 				_debug("call %ps", sk->sk_data_ready);
52248f219cSDavid Howells 				sk->sk_data_ready(sk);
53248f219cSDavid Howells 			}
54248f219cSDavid Howells 		}
55248f219cSDavid Howells 	}
56248f219cSDavid Howells 
57248f219cSDavid Howells 	rcu_read_unlock();
58248f219cSDavid Howells 	_leave("");
59248f219cSDavid Howells }
60248f219cSDavid Howells 
61248f219cSDavid Howells /*
62248f219cSDavid Howells  * Pass a call terminating message to userspace.
63248f219cSDavid Howells  */
rxrpc_recvmsg_term(struct rxrpc_call * call,struct msghdr * msg)64248f219cSDavid Howells static int rxrpc_recvmsg_term(struct rxrpc_call *call, struct msghdr *msg)
65248f219cSDavid Howells {
66248f219cSDavid Howells 	u32 tmp = 0;
67248f219cSDavid Howells 	int ret;
68248f219cSDavid Howells 
69248f219cSDavid Howells 	switch (call->completion) {
70248f219cSDavid Howells 	case RXRPC_CALL_SUCCEEDED:
71248f219cSDavid Howells 		ret = 0;
72248f219cSDavid Howells 		if (rxrpc_is_service_call(call))
73248f219cSDavid Howells 			ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ACK, 0, &tmp);
74248f219cSDavid Howells 		break;
75248f219cSDavid Howells 	case RXRPC_CALL_REMOTELY_ABORTED:
76248f219cSDavid Howells 		tmp = call->abort_code;
77248f219cSDavid Howells 		ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &tmp);
78248f219cSDavid Howells 		break;
79248f219cSDavid Howells 	case RXRPC_CALL_LOCALLY_ABORTED:
80248f219cSDavid Howells 		tmp = call->abort_code;
81248f219cSDavid Howells 		ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &tmp);
82248f219cSDavid Howells 		break;
83248f219cSDavid Howells 	case RXRPC_CALL_NETWORK_ERROR:
843a92789aSDavid Howells 		tmp = -call->error;
85248f219cSDavid Howells 		ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NET_ERROR, 4, &tmp);
86248f219cSDavid Howells 		break;
87248f219cSDavid Howells 	case RXRPC_CALL_LOCAL_ERROR:
883a92789aSDavid Howells 		tmp = -call->error;
89248f219cSDavid Howells 		ret = put_cmsg(msg, SOL_RXRPC, RXRPC_LOCAL_ERROR, 4, &tmp);
90248f219cSDavid Howells 		break;
91248f219cSDavid Howells 	default:
92d41b3f5bSDavid Howells 		pr_err("Invalid terminal call state %u\n", call->completion);
93248f219cSDavid Howells 		BUG();
94248f219cSDavid Howells 		break;
95248f219cSDavid Howells 	}
96248f219cSDavid Howells 
975d7edbc9SDavid Howells 	trace_rxrpc_recvdata(call, rxrpc_recvmsg_terminal,
985bbf9533SDavid Howells 			     call->ackr_window - 1,
9984997905SDavid Howells 			     call->rx_pkt_offset, call->rx_pkt_len, ret);
100248f219cSDavid Howells 	return ret;
101248f219cSDavid Howells }
102248f219cSDavid Howells 
103248f219cSDavid Howells /*
104248f219cSDavid Howells  * Discard a packet we've used up and advance the Rx window by one.
105248f219cSDavid Howells  */
rxrpc_rotate_rx_window(struct rxrpc_call * call)106248f219cSDavid Howells static void rxrpc_rotate_rx_window(struct rxrpc_call *call)
107248f219cSDavid Howells {
108816c9fceSDavid Howells 	struct rxrpc_skb_priv *sp;
109248f219cSDavid Howells 	struct sk_buff *skb;
11058dc63c9SDavid Howells 	rxrpc_serial_t serial;
1115d7edbc9SDavid Howells 	rxrpc_seq_t old_consumed = call->rx_consumed, tseq;
1125d7edbc9SDavid Howells 	bool last;
1135d7edbc9SDavid Howells 	int acked;
114248f219cSDavid Howells 
115248f219cSDavid Howells 	_enter("%d", call->debug_id);
116248f219cSDavid Howells 
1175d7edbc9SDavid Howells 	skb = skb_dequeue(&call->recvmsg_queue);
1189a36a6bcSDavid Howells 	rxrpc_see_skb(skb, rxrpc_skb_see_rotate);
1195d7edbc9SDavid Howells 
120816c9fceSDavid Howells 	sp = rxrpc_skb(skb);
1215d7edbc9SDavid Howells 	tseq   = sp->hdr.seq;
122d4d02d8bSDavid Howells 	serial = sp->hdr.serial;
1235d7edbc9SDavid Howells 	last   = sp->hdr.flags & RXRPC_LAST_PACKET;
124e2de6c40SDavid Howells 
125248f219cSDavid Howells 	/* Barrier against rxrpc_input_data(). */
1265d7edbc9SDavid Howells 	if (after(tseq, call->rx_consumed))
1275d7edbc9SDavid Howells 		smp_store_release(&call->rx_consumed, tseq);
128248f219cSDavid Howells 
1299a36a6bcSDavid Howells 	rxrpc_free_skb(skb, rxrpc_skb_put_rotate);
130248f219cSDavid Howells 
1315d7edbc9SDavid Howells 	trace_rxrpc_receive(call, last ? rxrpc_receive_rotate_last : rxrpc_receive_rotate,
1325d7edbc9SDavid Howells 			    serial, call->rx_consumed);
13393368b6bSDavid Howells 
13493368b6bSDavid Howells 	if (last)
13593368b6bSDavid Howells 		set_bit(RXRPC_CALL_RECVMSG_READ_ALL, &call->flags);
1365d7edbc9SDavid Howells 
137805b21b9SDavid Howells 	/* Check to see if there's an ACK that needs sending. */
1385d7edbc9SDavid Howells 	acked = atomic_add_return(call->rx_consumed - old_consumed,
1395d7edbc9SDavid Howells 				  &call->ackr_nr_consumed);
1405a2c5a5bSDavid Howells 	if (acked > 8 &&
1415e6ef4f1SDavid Howells 	    !test_and_set_bit(RXRPC_CALL_RX_IS_IDLE, &call->flags))
1425e6ef4f1SDavid Howells 		rxrpc_poke_call(call, rxrpc_call_poke_idle);
143805b21b9SDavid Howells }
144248f219cSDavid Howells 
145248f219cSDavid Howells /*
146d4d02d8bSDavid Howells  * Decrypt and verify a DATA packet.
147248f219cSDavid Howells  */
rxrpc_verify_data(struct rxrpc_call * call,struct sk_buff * skb)148d4d02d8bSDavid Howells static int rxrpc_verify_data(struct rxrpc_call *call, struct sk_buff *skb)
149248f219cSDavid Howells {
150248f219cSDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
151248f219cSDavid Howells 
152d4d02d8bSDavid Howells 	if (sp->flags & RXRPC_RX_VERIFIED)
153248f219cSDavid Howells 		return 0;
154d4d02d8bSDavid Howells 	return call->security->verify_packet(call, skb);
155248f219cSDavid Howells }
156248f219cSDavid Howells 
157248f219cSDavid Howells /*
158248f219cSDavid Howells  * Deliver messages to a call.  This keeps processing packets until the buffer
159248f219cSDavid Howells  * is filled and we find either more DATA (returns 0) or the end of the DATA
16093368b6bSDavid Howells  * (returns 1).  If more packets are required, it returns -EAGAIN and if the
16193368b6bSDavid Howells  * call has failed it returns -EIO.
162248f219cSDavid Howells  */
rxrpc_recvmsg_data(struct socket * sock,struct rxrpc_call * call,struct msghdr * msg,struct iov_iter * iter,size_t len,int flags,size_t * _offset)163248f219cSDavid Howells static int rxrpc_recvmsg_data(struct socket *sock, struct rxrpc_call *call,
164248f219cSDavid Howells 			      struct msghdr *msg, struct iov_iter *iter,
165248f219cSDavid Howells 			      size_t len, int flags, size_t *_offset)
166248f219cSDavid Howells {
167248f219cSDavid Howells 	struct rxrpc_skb_priv *sp;
168248f219cSDavid Howells 	struct sk_buff *skb;
1695d7edbc9SDavid Howells 	rxrpc_seq_t seq = 0;
170248f219cSDavid Howells 	size_t remain;
171248f219cSDavid Howells 	unsigned int rx_pkt_offset, rx_pkt_len;
1725d7edbc9SDavid Howells 	int copy, ret = -EAGAIN, ret2;
173248f219cSDavid Howells 
174248f219cSDavid Howells 	rx_pkt_offset = call->rx_pkt_offset;
175248f219cSDavid Howells 	rx_pkt_len = call->rx_pkt_len;
176248f219cSDavid Howells 
17793368b6bSDavid Howells 	if (rxrpc_call_has_failed(call)) {
1785bbf9533SDavid Howells 		seq = call->ackr_window - 1;
17993368b6bSDavid Howells 		ret = -EIO;
18093368b6bSDavid Howells 		goto done;
18193368b6bSDavid Howells 	}
18293368b6bSDavid Howells 
18393368b6bSDavid Howells 	if (test_bit(RXRPC_CALL_RECVMSG_READ_ALL, &call->flags)) {
1845bbf9533SDavid Howells 		seq = call->ackr_window - 1;
185816c9fceSDavid Howells 		ret = 1;
186816c9fceSDavid Howells 		goto done;
187816c9fceSDavid Howells 	}
188816c9fceSDavid Howells 
1895d7edbc9SDavid Howells 	/* No one else can be removing stuff from the queue, so we shouldn't
1905d7edbc9SDavid Howells 	 * need the Rx lock to walk it.
1915d7edbc9SDavid Howells 	 */
1925d7edbc9SDavid Howells 	skb = skb_peek(&call->recvmsg_queue);
1935d7edbc9SDavid Howells 	while (skb) {
1949a36a6bcSDavid Howells 		rxrpc_see_skb(skb, rxrpc_skb_see_recvmsg);
195248f219cSDavid Howells 		sp = rxrpc_skb(skb);
1965d7edbc9SDavid Howells 		seq = sp->hdr.seq;
197248f219cSDavid Howells 
1985d7edbc9SDavid Howells 		if (!(flags & MSG_PEEK))
1995d7edbc9SDavid Howells 			trace_rxrpc_receive(call, rxrpc_receive_front,
2005d7edbc9SDavid Howells 					    sp->hdr.serial, seq);
2015d7edbc9SDavid Howells 
202248f219cSDavid Howells 		if (msg)
203248f219cSDavid Howells 			sock_recv_timestamp(msg, sock->sk, skb);
204248f219cSDavid Howells 
2052e2ea51dSDavid Howells 		if (rx_pkt_offset == 0) {
206d4d02d8bSDavid Howells 			ret2 = rxrpc_verify_data(call, skb);
207faf92e8dSDavid Howells 			trace_rxrpc_recvdata(call, rxrpc_recvmsg_next, seq,
20893368b6bSDavid Howells 					     sp->offset, sp->len, ret2);
209816c9fceSDavid Howells 			if (ret2 < 0) {
21093368b6bSDavid Howells 				kdebug("verify = %d", ret2);
211816c9fceSDavid Howells 				ret = ret2;
2122e2ea51dSDavid Howells 				goto out;
2132e2ea51dSDavid Howells 			}
21493368b6bSDavid Howells 			rx_pkt_offset = sp->offset;
21593368b6bSDavid Howells 			rx_pkt_len = sp->len;
21684997905SDavid Howells 		} else {
217faf92e8dSDavid Howells 			trace_rxrpc_recvdata(call, rxrpc_recvmsg_cont, seq,
21884997905SDavid Howells 					     rx_pkt_offset, rx_pkt_len, 0);
219816c9fceSDavid Howells 		}
220248f219cSDavid Howells 
221248f219cSDavid Howells 		/* We have to handle short, empty and used-up DATA packets. */
222248f219cSDavid Howells 		remain = len - *_offset;
223248f219cSDavid Howells 		copy = rx_pkt_len;
224248f219cSDavid Howells 		if (copy > remain)
225248f219cSDavid Howells 			copy = remain;
226248f219cSDavid Howells 		if (copy > 0) {
227816c9fceSDavid Howells 			ret2 = skb_copy_datagram_iter(skb, rx_pkt_offset, iter,
228248f219cSDavid Howells 						      copy);
229816c9fceSDavid Howells 			if (ret2 < 0) {
230816c9fceSDavid Howells 				ret = ret2;
231248f219cSDavid Howells 				goto out;
232816c9fceSDavid Howells 			}
233248f219cSDavid Howells 
234248f219cSDavid Howells 			/* handle piecemeal consumption of data packets */
235248f219cSDavid Howells 			rx_pkt_offset += copy;
236248f219cSDavid Howells 			rx_pkt_len -= copy;
237248f219cSDavid Howells 			*_offset += copy;
238248f219cSDavid Howells 		}
239248f219cSDavid Howells 
240248f219cSDavid Howells 		if (rx_pkt_len > 0) {
241faf92e8dSDavid Howells 			trace_rxrpc_recvdata(call, rxrpc_recvmsg_full, seq,
24284997905SDavid Howells 					     rx_pkt_offset, rx_pkt_len, 0);
243248f219cSDavid Howells 			ASSERTCMP(*_offset, ==, len);
244816c9fceSDavid Howells 			ret = 0;
245248f219cSDavid Howells 			break;
246248f219cSDavid Howells 		}
247248f219cSDavid Howells 
248248f219cSDavid Howells 		/* The whole packet has been transferred. */
249d4d02d8bSDavid Howells 		if (sp->hdr.flags & RXRPC_LAST_PACKET)
250d4d02d8bSDavid Howells 			ret = 1;
251248f219cSDavid Howells 		rx_pkt_offset = 0;
252248f219cSDavid Howells 		rx_pkt_len = 0;
253248f219cSDavid Howells 
2545d7edbc9SDavid Howells 		skb = skb_peek_next(skb, &call->recvmsg_queue);
2555d7edbc9SDavid Howells 
256d4d02d8bSDavid Howells 		if (!(flags & MSG_PEEK))
257d4d02d8bSDavid Howells 			rxrpc_rotate_rx_window(call);
258248f219cSDavid Howells 	}
259248f219cSDavid Howells 
260248f219cSDavid Howells out:
261248f219cSDavid Howells 	if (!(flags & MSG_PEEK)) {
262248f219cSDavid Howells 		call->rx_pkt_offset = rx_pkt_offset;
263248f219cSDavid Howells 		call->rx_pkt_len = rx_pkt_len;
264248f219cSDavid Howells 	}
265816c9fceSDavid Howells done:
266faf92e8dSDavid Howells 	trace_rxrpc_recvdata(call, rxrpc_recvmsg_data_return, seq,
26784997905SDavid Howells 			     rx_pkt_offset, rx_pkt_len, ret);
268d0b35a42SDavid Howells 	if (ret == -EAGAIN)
2695e6ef4f1SDavid Howells 		set_bit(RXRPC_CALL_RX_IS_IDLE, &call->flags);
270248f219cSDavid Howells 	return ret;
271248f219cSDavid Howells }
272248f219cSDavid Howells 
273248f219cSDavid Howells /*
274248f219cSDavid Howells  * Receive a message from an RxRPC socket
2758c3e34a4SDavid Howells  * - we need to be careful about two or more threads calling recvmsg
2768c3e34a4SDavid Howells  *   simultaneously
2778c3e34a4SDavid Howells  */
rxrpc_recvmsg(struct socket * sock,struct msghdr * msg,size_t len,int flags)2788c3e34a4SDavid Howells int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
2798c3e34a4SDavid Howells 		  int flags)
2808c3e34a4SDavid Howells {
281248f219cSDavid Howells 	struct rxrpc_call *call;
2828c3e34a4SDavid Howells 	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
283248f219cSDavid Howells 	struct list_head *l;
2840e50d999SDavid Howells 	unsigned int call_debug_id = 0;
285248f219cSDavid Howells 	size_t copied = 0;
2868c3e34a4SDavid Howells 	long timeo;
287248f219cSDavid Howells 	int ret;
2888c3e34a4SDavid Howells 
2898c3e34a4SDavid Howells 	DEFINE_WAIT(wait);
2908c3e34a4SDavid Howells 
2910e50d999SDavid Howells 	trace_rxrpc_recvmsg(0, rxrpc_recvmsg_enter, 0);
2928c3e34a4SDavid Howells 
2938c3e34a4SDavid Howells 	if (flags & (MSG_OOB | MSG_TRUNC))
2948c3e34a4SDavid Howells 		return -EOPNOTSUPP;
2958c3e34a4SDavid Howells 
2968c3e34a4SDavid Howells 	timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT);
2978c3e34a4SDavid Howells 
298248f219cSDavid Howells try_again:
2998c3e34a4SDavid Howells 	lock_sock(&rx->sk);
3008c3e34a4SDavid Howells 
301248f219cSDavid Howells 	/* Return immediately if a client socket has no outstanding calls */
302248f219cSDavid Howells 	if (RB_EMPTY_ROOT(&rx->calls) &&
303248f219cSDavid Howells 	    list_empty(&rx->recvmsg_q) &&
304248f219cSDavid Howells 	    rx->sk.sk_state != RXRPC_SERVER_LISTENING) {
3058c3e34a4SDavid Howells 		release_sock(&rx->sk);
306639f181fSDavid Howells 		return -EAGAIN;
3078c3e34a4SDavid Howells 	}
3088c3e34a4SDavid Howells 
309248f219cSDavid Howells 	if (list_empty(&rx->recvmsg_q)) {
310248f219cSDavid Howells 		ret = -EWOULDBLOCK;
31184997905SDavid Howells 		if (timeo == 0) {
31284997905SDavid Howells 			call = NULL;
313248f219cSDavid Howells 			goto error_no_call;
31484997905SDavid Howells 		}
3158c3e34a4SDavid Howells 
3168c3e34a4SDavid Howells 		release_sock(&rx->sk);
317248f219cSDavid Howells 
318248f219cSDavid Howells 		/* Wait for something to happen */
3198c3e34a4SDavid Howells 		prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait,
3208c3e34a4SDavid Howells 					  TASK_INTERRUPTIBLE);
3218c3e34a4SDavid Howells 		ret = sock_error(&rx->sk);
3228c3e34a4SDavid Howells 		if (ret)
3238c3e34a4SDavid Howells 			goto wait_error;
3248c3e34a4SDavid Howells 
325248f219cSDavid Howells 		if (list_empty(&rx->recvmsg_q)) {
3268c3e34a4SDavid Howells 			if (signal_pending(current))
3278c3e34a4SDavid Howells 				goto wait_interrupted;
3280e50d999SDavid Howells 			trace_rxrpc_recvmsg(0, rxrpc_recvmsg_wait, 0);
3298c3e34a4SDavid Howells 			timeo = schedule_timeout(timeo);
3308c3e34a4SDavid Howells 		}
3318c3e34a4SDavid Howells 		finish_wait(sk_sleep(&rx->sk), &wait);
332248f219cSDavid Howells 		goto try_again;
3338c3e34a4SDavid Howells 	}
3348c3e34a4SDavid Howells 
335248f219cSDavid Howells 	/* Find the next call and dequeue it if we're not just peeking.  If we
336248f219cSDavid Howells 	 * do dequeue it, that comes with a ref that we will need to release.
337*c0783818SDavid Howells 	 * We also want to weed out calls that got requeued whilst we were
338*c0783818SDavid Howells 	 * shovelling data out.
339248f219cSDavid Howells 	 */
340223f5901SDavid Howells 	spin_lock(&rx->recvmsg_lock);
341248f219cSDavid Howells 	l = rx->recvmsg_q.next;
342248f219cSDavid Howells 	call = list_entry(l, struct rxrpc_call, recvmsg_link);
343*c0783818SDavid Howells 
344*c0783818SDavid Howells 	if (!rxrpc_call_is_complete(call) &&
345*c0783818SDavid Howells 	    skb_queue_empty(&call->recvmsg_queue)) {
346*c0783818SDavid Howells 		list_del_init(&call->recvmsg_link);
347*c0783818SDavid Howells 		spin_unlock(&rx->recvmsg_lock);
348*c0783818SDavid Howells 		release_sock(&rx->sk);
349*c0783818SDavid Howells 		trace_rxrpc_recvmsg(call->debug_id, rxrpc_recvmsg_unqueue, 0);
350*c0783818SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_recvmsg);
351*c0783818SDavid Howells 		goto try_again;
352*c0783818SDavid Howells 	}
353*c0783818SDavid Howells 
354248f219cSDavid Howells 	if (!(flags & MSG_PEEK))
355248f219cSDavid Howells 		list_del_init(&call->recvmsg_link);
356248f219cSDavid Howells 	else
357cb0fc0c9SDavid Howells 		rxrpc_get_call(call, rxrpc_call_get_recvmsg);
358223f5901SDavid Howells 	spin_unlock(&rx->recvmsg_lock);
3598c3e34a4SDavid Howells 
3600e50d999SDavid Howells 	call_debug_id = call->debug_id;
3610e50d999SDavid Howells 	trace_rxrpc_recvmsg(call_debug_id, rxrpc_recvmsg_dequeue, 0);
362248f219cSDavid Howells 
363540b1c48SDavid Howells 	/* We're going to drop the socket lock, so we need to lock the call
364540b1c48SDavid Howells 	 * against interference by sendmsg.
365540b1c48SDavid Howells 	 */
366540b1c48SDavid Howells 	if (!mutex_trylock(&call->user_mutex)) {
367540b1c48SDavid Howells 		ret = -EWOULDBLOCK;
368540b1c48SDavid Howells 		if (flags & MSG_DONTWAIT)
369540b1c48SDavid Howells 			goto error_requeue_call;
370540b1c48SDavid Howells 		ret = -ERESTARTSYS;
371540b1c48SDavid Howells 		if (mutex_lock_interruptible(&call->user_mutex) < 0)
372540b1c48SDavid Howells 			goto error_requeue_call;
373540b1c48SDavid Howells 	}
374540b1c48SDavid Howells 
375540b1c48SDavid Howells 	release_sock(&rx->sk);
376540b1c48SDavid Howells 
377248f219cSDavid Howells 	if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
378248f219cSDavid Howells 		BUG();
379248f219cSDavid Howells 
380248f219cSDavid Howells 	if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
381248f219cSDavid Howells 		if (flags & MSG_CMSG_COMPAT) {
382248f219cSDavid Howells 			unsigned int id32 = call->user_call_ID;
383248f219cSDavid Howells 
384248f219cSDavid Howells 			ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
385248f219cSDavid Howells 				       sizeof(unsigned int), &id32);
386248f219cSDavid Howells 		} else {
387a16b8d0cSDavid Howells 			unsigned long idl = call->user_call_ID;
388a16b8d0cSDavid Howells 
389248f219cSDavid Howells 			ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
390a16b8d0cSDavid Howells 				       sizeof(unsigned long), &idl);
391248f219cSDavid Howells 		}
392248f219cSDavid Howells 		if (ret < 0)
393540b1c48SDavid Howells 			goto error_unlock_call;
394248f219cSDavid Howells 	}
395248f219cSDavid Howells 
39665550098SDavid Howells 	if (msg->msg_name && call->peer) {
397f3441d41SDavid Howells 		size_t len = sizeof(call->dest_srx);
39868d6d1aeSDavid Howells 
399f3441d41SDavid Howells 		memcpy(msg->msg_name, &call->dest_srx, len);
4008c3e34a4SDavid Howells 		msg->msg_namelen = len;
4018c3e34a4SDavid Howells 	}
4028c3e34a4SDavid Howells 
403248f219cSDavid Howells 	ret = rxrpc_recvmsg_data(sock, call, msg, &msg->msg_iter, len,
404248f219cSDavid Howells 				 flags, &copied);
405248f219cSDavid Howells 	if (ret == -EAGAIN)
406248f219cSDavid Howells 		ret = 0;
40793368b6bSDavid Howells 	if (ret == -EIO)
40893368b6bSDavid Howells 		goto call_failed;
4098c3e34a4SDavid Howells 	if (ret < 0)
410540b1c48SDavid Howells 		goto error_unlock_call;
4118c3e34a4SDavid Howells 
41293368b6bSDavid Howells 	if (rxrpc_call_is_complete(call) &&
41393368b6bSDavid Howells 	    skb_queue_empty(&call->recvmsg_queue))
41493368b6bSDavid Howells 		goto call_complete;
41593368b6bSDavid Howells 	if (rxrpc_call_has_failed(call))
41693368b6bSDavid Howells 		goto call_failed;
41793368b6bSDavid Howells 
418*c0783818SDavid Howells 	if (!skb_queue_empty(&call->recvmsg_queue))
41993368b6bSDavid Howells 		rxrpc_notify_socket(call);
42093368b6bSDavid Howells 	goto not_yet_complete;
42193368b6bSDavid Howells 
42293368b6bSDavid Howells call_failed:
42393368b6bSDavid Howells 	rxrpc_purge_queue(&call->recvmsg_queue);
42493368b6bSDavid Howells call_complete:
425248f219cSDavid Howells 	ret = rxrpc_recvmsg_term(call, msg);
426248f219cSDavid Howells 	if (ret < 0)
427540b1c48SDavid Howells 		goto error_unlock_call;
428248f219cSDavid Howells 	if (!(flags & MSG_PEEK))
4298d94aa38SDavid Howells 		rxrpc_release_call(rx, call);
430248f219cSDavid Howells 	msg->msg_flags |= MSG_EOR;
431248f219cSDavid Howells 	ret = 1;
4328c3e34a4SDavid Howells 
43393368b6bSDavid Howells not_yet_complete:
434248f219cSDavid Howells 	if (ret == 0)
435248f219cSDavid Howells 		msg->msg_flags |= MSG_MORE;
436248f219cSDavid Howells 	else
437248f219cSDavid Howells 		msg->msg_flags &= ~MSG_MORE;
438248f219cSDavid Howells 	ret = copied;
4398c3e34a4SDavid Howells 
440540b1c48SDavid Howells error_unlock_call:
441540b1c48SDavid Howells 	mutex_unlock(&call->user_mutex);
442cb0fc0c9SDavid Howells 	rxrpc_put_call(call, rxrpc_call_put_recvmsg);
4430e50d999SDavid Howells 	trace_rxrpc_recvmsg(call_debug_id, rxrpc_recvmsg_return, ret);
444540b1c48SDavid Howells 	return ret;
445540b1c48SDavid Howells 
446540b1c48SDavid Howells error_requeue_call:
447540b1c48SDavid Howells 	if (!(flags & MSG_PEEK)) {
448223f5901SDavid Howells 		spin_lock(&rx->recvmsg_lock);
449540b1c48SDavid Howells 		list_add(&call->recvmsg_link, &rx->recvmsg_q);
450223f5901SDavid Howells 		spin_unlock(&rx->recvmsg_lock);
4510e50d999SDavid Howells 		trace_rxrpc_recvmsg(call_debug_id, rxrpc_recvmsg_requeue, 0);
452540b1c48SDavid Howells 	} else {
453cb0fc0c9SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_recvmsg);
454540b1c48SDavid Howells 	}
455248f219cSDavid Howells error_no_call:
456248f219cSDavid Howells 	release_sock(&rx->sk);
4576dce3c20SEric Dumazet error_trace:
4580e50d999SDavid Howells 	trace_rxrpc_recvmsg(call_debug_id, rxrpc_recvmsg_return, ret);
4598c3e34a4SDavid Howells 	return ret;
4608c3e34a4SDavid Howells 
4618c3e34a4SDavid Howells wait_interrupted:
4628c3e34a4SDavid Howells 	ret = sock_intr_errno(timeo);
4638c3e34a4SDavid Howells wait_error:
4648c3e34a4SDavid Howells 	finish_wait(sk_sleep(&rx->sk), &wait);
46584997905SDavid Howells 	call = NULL;
4666dce3c20SEric Dumazet 	goto error_trace;
467d001648eSDavid Howells }
4688c3e34a4SDavid Howells 
4698c3e34a4SDavid Howells /**
470d001648eSDavid Howells  * rxrpc_kernel_recv_data - Allow a kernel service to receive data/info
471d001648eSDavid Howells  * @sock: The socket that the call exists on
472d001648eSDavid Howells  * @call: The call to send data through
473eb9950ebSDavid Howells  * @iter: The buffer to receive into
474f105da1aSDavid Howells  * @_len: The amount of data we want to receive (decreased on return)
475d001648eSDavid Howells  * @want_more: True if more data is expected to be read
476d001648eSDavid Howells  * @_abort: Where the abort code is stored if -ECONNABORTED is returned
477a68f4a27SDavid Howells  * @_service: Where to store the actual service ID (may be upgraded)
4788c3e34a4SDavid Howells  *
479d001648eSDavid Howells  * Allow a kernel service to receive data and pick up information about the
480d001648eSDavid Howells  * state of a call.  Returns 0 if got what was asked for and there's more
481d001648eSDavid Howells  * available, 1 if we got what was asked for and we're at the end of the data
482d001648eSDavid Howells  * and -EAGAIN if we need more data.
483d001648eSDavid Howells  *
484d001648eSDavid Howells  * Note that we may return -EAGAIN to drain empty packets at the end of the
485d001648eSDavid Howells  * data, even if we've already copied over the requested data.
486d001648eSDavid Howells  *
487d001648eSDavid Howells  * *_abort should also be initialised to 0.
4888c3e34a4SDavid Howells  */
rxrpc_kernel_recv_data(struct socket * sock,struct rxrpc_call * call,struct iov_iter * iter,size_t * _len,bool want_more,u32 * _abort,u16 * _service)489d001648eSDavid Howells int rxrpc_kernel_recv_data(struct socket *sock, struct rxrpc_call *call,
490f105da1aSDavid Howells 			   struct iov_iter *iter, size_t *_len,
491a68f4a27SDavid Howells 			   bool want_more, u32 *_abort, u16 *_service)
4928c3e34a4SDavid Howells {
493eb9950ebSDavid Howells 	size_t offset = 0;
494d001648eSDavid Howells 	int ret;
4958c3e34a4SDavid Howells 
49693368b6bSDavid Howells 	_enter("{%d},%zu,%d", call->debug_id, *_len, want_more);
497d001648eSDavid Howells 
498540b1c48SDavid Howells 	mutex_lock(&call->user_mutex);
499d001648eSDavid Howells 
50093368b6bSDavid Howells 	ret = rxrpc_recvmsg_data(sock, call, NULL, iter, *_len, 0, &offset);
501f105da1aSDavid Howells 	*_len -= offset;
50293368b6bSDavid Howells 	if (ret == -EIO)
50393368b6bSDavid Howells 		goto call_failed;
504d001648eSDavid Howells 	if (ret < 0)
505d001648eSDavid Howells 		goto out;
506d001648eSDavid Howells 
50793368b6bSDavid Howells 	/* We can only reach here with a partially full buffer if we have
50893368b6bSDavid Howells 	 * reached the end of the data.  We must otherwise have a full buffer
50993368b6bSDavid Howells 	 * or have been given -EAGAIN.
510d001648eSDavid Howells 	 */
511d001648eSDavid Howells 	if (ret == 1) {
512eb9950ebSDavid Howells 		if (iov_iter_count(iter) > 0)
513d001648eSDavid Howells 			goto short_data;
514d001648eSDavid Howells 		if (!want_more)
515d001648eSDavid Howells 			goto read_phase_complete;
516d001648eSDavid Howells 		ret = 0;
517d001648eSDavid Howells 		goto out;
5188c3e34a4SDavid Howells 	}
5198c3e34a4SDavid Howells 
520d001648eSDavid Howells 	if (!want_more)
521d001648eSDavid Howells 		goto excess_data;
522d001648eSDavid Howells 	goto out;
523d001648eSDavid Howells 
524d001648eSDavid Howells read_phase_complete:
525d001648eSDavid Howells 	ret = 1;
526d001648eSDavid Howells out:
527a68f4a27SDavid Howells 	if (_service)
528f3441d41SDavid Howells 		*_service = call->dest_srx.srx_service;
529540b1c48SDavid Howells 	mutex_unlock(&call->user_mutex);
530eb9950ebSDavid Howells 	_leave(" = %d [%zu,%d]", ret, iov_iter_count(iter), *_abort);
531d001648eSDavid Howells 	return ret;
532d001648eSDavid Howells 
533d001648eSDavid Howells short_data:
53457af281eSDavid Howells 	trace_rxrpc_abort(call->debug_id, rxrpc_recvmsg_short_data,
53557af281eSDavid Howells 			  call->cid, call->call_id, call->rx_consumed,
53657af281eSDavid Howells 			  0, -EBADMSG);
537d001648eSDavid Howells 	ret = -EBADMSG;
538d001648eSDavid Howells 	goto out;
539d001648eSDavid Howells excess_data:
54057af281eSDavid Howells 	trace_rxrpc_abort(call->debug_id, rxrpc_recvmsg_excess_data,
54157af281eSDavid Howells 			  call->cid, call->call_id, call->rx_consumed,
54257af281eSDavid Howells 			  0, -EMSGSIZE);
543d001648eSDavid Howells 	ret = -EMSGSIZE;
544d001648eSDavid Howells 	goto out;
54593368b6bSDavid Howells call_failed:
546d001648eSDavid Howells 	*_abort = call->abort_code;
5473a92789aSDavid Howells 	ret = call->error;
548d001648eSDavid Howells 	if (call->completion == RXRPC_CALL_SUCCEEDED) {
549d001648eSDavid Howells 		ret = 1;
550eb9950ebSDavid Howells 		if (iov_iter_count(iter) > 0)
551d001648eSDavid Howells 			ret = -ECONNRESET;
552d001648eSDavid Howells 	}
553d001648eSDavid Howells 	goto out;
554d001648eSDavid Howells }
555d001648eSDavid Howells EXPORT_SYMBOL(rxrpc_kernel_recv_data);
556