xref: /openbmc/linux/net/rxrpc/recvmsg.c (revision 93368b6b)
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  */
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 {
433dd9c8b5SDavid Howells 			write_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 			}
483dd9c8b5SDavid Howells 			write_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  */
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,
985d7edbc9SDavid Howells 			     lower_32_bits(atomic64_read(&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  */
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);
133*93368b6bSDavid Howells 
134*93368b6bSDavid Howells 	if (last)
135*93368b6bSDavid 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);
1405d7edbc9SDavid Howells 	if (acked > 2 &&
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  */
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
160*93368b6bSDavid Howells  * (returns 1).  If more packets are required, it returns -EAGAIN and if the
161*93368b6bSDavid Howells  * call has failed it returns -EIO.
162248f219cSDavid Howells  */
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 
177*93368b6bSDavid Howells 	if (rxrpc_call_has_failed(call)) {
178*93368b6bSDavid Howells 		seq = lower_32_bits(atomic64_read(&call->ackr_window)) - 1;
179*93368b6bSDavid Howells 		ret = -EIO;
180*93368b6bSDavid Howells 		goto done;
181*93368b6bSDavid Howells 	}
182*93368b6bSDavid Howells 
183*93368b6bSDavid Howells 	if (test_bit(RXRPC_CALL_RECVMSG_READ_ALL, &call->flags)) {
1845d7edbc9SDavid Howells 		seq = lower_32_bits(atomic64_read(&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,
208*93368b6bSDavid Howells 					     sp->offset, sp->len, ret2);
209816c9fceSDavid Howells 			if (ret2 < 0) {
210*93368b6bSDavid Howells 				kdebug("verify = %d", ret2);
211816c9fceSDavid Howells 				ret = ret2;
2122e2ea51dSDavid Howells 				goto out;
2132e2ea51dSDavid Howells 			}
214*93368b6bSDavid Howells 			rx_pkt_offset = sp->offset;
215*93368b6bSDavid 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  */
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.
337248f219cSDavid Howells 	 */
3383dd9c8b5SDavid Howells 	write_lock(&rx->recvmsg_lock);
339248f219cSDavid Howells 	l = rx->recvmsg_q.next;
340248f219cSDavid Howells 	call = list_entry(l, struct rxrpc_call, recvmsg_link);
341248f219cSDavid Howells 	if (!(flags & MSG_PEEK))
342248f219cSDavid Howells 		list_del_init(&call->recvmsg_link);
343248f219cSDavid Howells 	else
344cb0fc0c9SDavid Howells 		rxrpc_get_call(call, rxrpc_call_get_recvmsg);
3453dd9c8b5SDavid Howells 	write_unlock(&rx->recvmsg_lock);
3468c3e34a4SDavid Howells 
3470e50d999SDavid Howells 	call_debug_id = call->debug_id;
3480e50d999SDavid Howells 	trace_rxrpc_recvmsg(call_debug_id, rxrpc_recvmsg_dequeue, 0);
349248f219cSDavid Howells 
350540b1c48SDavid Howells 	/* We're going to drop the socket lock, so we need to lock the call
351540b1c48SDavid Howells 	 * against interference by sendmsg.
352540b1c48SDavid Howells 	 */
353540b1c48SDavid Howells 	if (!mutex_trylock(&call->user_mutex)) {
354540b1c48SDavid Howells 		ret = -EWOULDBLOCK;
355540b1c48SDavid Howells 		if (flags & MSG_DONTWAIT)
356540b1c48SDavid Howells 			goto error_requeue_call;
357540b1c48SDavid Howells 		ret = -ERESTARTSYS;
358540b1c48SDavid Howells 		if (mutex_lock_interruptible(&call->user_mutex) < 0)
359540b1c48SDavid Howells 			goto error_requeue_call;
360540b1c48SDavid Howells 	}
361540b1c48SDavid Howells 
362540b1c48SDavid Howells 	release_sock(&rx->sk);
363540b1c48SDavid Howells 
364248f219cSDavid Howells 	if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
365248f219cSDavid Howells 		BUG();
366248f219cSDavid Howells 
367248f219cSDavid Howells 	if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
368248f219cSDavid Howells 		if (flags & MSG_CMSG_COMPAT) {
369248f219cSDavid Howells 			unsigned int id32 = call->user_call_ID;
370248f219cSDavid Howells 
371248f219cSDavid Howells 			ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
372248f219cSDavid Howells 				       sizeof(unsigned int), &id32);
373248f219cSDavid Howells 		} else {
374a16b8d0cSDavid Howells 			unsigned long idl = call->user_call_ID;
375a16b8d0cSDavid Howells 
376248f219cSDavid Howells 			ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
377a16b8d0cSDavid Howells 				       sizeof(unsigned long), &idl);
378248f219cSDavid Howells 		}
379248f219cSDavid Howells 		if (ret < 0)
380540b1c48SDavid Howells 			goto error_unlock_call;
381248f219cSDavid Howells 	}
382248f219cSDavid Howells 
38365550098SDavid Howells 	if (msg->msg_name && call->peer) {
384f3441d41SDavid Howells 		size_t len = sizeof(call->dest_srx);
38568d6d1aeSDavid Howells 
386f3441d41SDavid Howells 		memcpy(msg->msg_name, &call->dest_srx, len);
3878c3e34a4SDavid Howells 		msg->msg_namelen = len;
3888c3e34a4SDavid Howells 	}
3898c3e34a4SDavid Howells 
390248f219cSDavid Howells 	ret = rxrpc_recvmsg_data(sock, call, msg, &msg->msg_iter, len,
391248f219cSDavid Howells 				 flags, &copied);
392248f219cSDavid Howells 	if (ret == -EAGAIN)
393248f219cSDavid Howells 		ret = 0;
394*93368b6bSDavid Howells 	if (ret == -EIO)
395*93368b6bSDavid Howells 		goto call_failed;
3968c3e34a4SDavid Howells 	if (ret < 0)
397540b1c48SDavid Howells 		goto error_unlock_call;
3988c3e34a4SDavid Howells 
399*93368b6bSDavid Howells 	if (rxrpc_call_is_complete(call) &&
400*93368b6bSDavid Howells 	    skb_queue_empty(&call->recvmsg_queue))
401*93368b6bSDavid Howells 		goto call_complete;
402*93368b6bSDavid Howells 	if (rxrpc_call_has_failed(call))
403*93368b6bSDavid Howells 		goto call_failed;
404*93368b6bSDavid Howells 
405*93368b6bSDavid Howells 	rxrpc_notify_socket(call);
406*93368b6bSDavid Howells 	goto not_yet_complete;
407*93368b6bSDavid Howells 
408*93368b6bSDavid Howells call_failed:
409*93368b6bSDavid Howells 	rxrpc_purge_queue(&call->recvmsg_queue);
410*93368b6bSDavid Howells call_complete:
411248f219cSDavid Howells 	ret = rxrpc_recvmsg_term(call, msg);
412248f219cSDavid Howells 	if (ret < 0)
413540b1c48SDavid Howells 		goto error_unlock_call;
414248f219cSDavid Howells 	if (!(flags & MSG_PEEK))
4158d94aa38SDavid Howells 		rxrpc_release_call(rx, call);
416248f219cSDavid Howells 	msg->msg_flags |= MSG_EOR;
417248f219cSDavid Howells 	ret = 1;
4188c3e34a4SDavid Howells 
419*93368b6bSDavid Howells not_yet_complete:
420248f219cSDavid Howells 	if (ret == 0)
421248f219cSDavid Howells 		msg->msg_flags |= MSG_MORE;
422248f219cSDavid Howells 	else
423248f219cSDavid Howells 		msg->msg_flags &= ~MSG_MORE;
424248f219cSDavid Howells 	ret = copied;
4258c3e34a4SDavid Howells 
426540b1c48SDavid Howells error_unlock_call:
427540b1c48SDavid Howells 	mutex_unlock(&call->user_mutex);
428cb0fc0c9SDavid Howells 	rxrpc_put_call(call, rxrpc_call_put_recvmsg);
4290e50d999SDavid Howells 	trace_rxrpc_recvmsg(call_debug_id, rxrpc_recvmsg_return, ret);
430540b1c48SDavid Howells 	return ret;
431540b1c48SDavid Howells 
432540b1c48SDavid Howells error_requeue_call:
433540b1c48SDavid Howells 	if (!(flags & MSG_PEEK)) {
4343dd9c8b5SDavid Howells 		write_lock(&rx->recvmsg_lock);
435540b1c48SDavid Howells 		list_add(&call->recvmsg_link, &rx->recvmsg_q);
4363dd9c8b5SDavid Howells 		write_unlock(&rx->recvmsg_lock);
4370e50d999SDavid Howells 		trace_rxrpc_recvmsg(call_debug_id, rxrpc_recvmsg_requeue, 0);
438540b1c48SDavid Howells 	} else {
439cb0fc0c9SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_recvmsg);
440540b1c48SDavid Howells 	}
441248f219cSDavid Howells error_no_call:
442248f219cSDavid Howells 	release_sock(&rx->sk);
4436dce3c20SEric Dumazet error_trace:
4440e50d999SDavid Howells 	trace_rxrpc_recvmsg(call_debug_id, rxrpc_recvmsg_return, ret);
4458c3e34a4SDavid Howells 	return ret;
4468c3e34a4SDavid Howells 
4478c3e34a4SDavid Howells wait_interrupted:
4488c3e34a4SDavid Howells 	ret = sock_intr_errno(timeo);
4498c3e34a4SDavid Howells wait_error:
4508c3e34a4SDavid Howells 	finish_wait(sk_sleep(&rx->sk), &wait);
45184997905SDavid Howells 	call = NULL;
4526dce3c20SEric Dumazet 	goto error_trace;
453d001648eSDavid Howells }
4548c3e34a4SDavid Howells 
4558c3e34a4SDavid Howells /**
456d001648eSDavid Howells  * rxrpc_kernel_recv_data - Allow a kernel service to receive data/info
457d001648eSDavid Howells  * @sock: The socket that the call exists on
458d001648eSDavid Howells  * @call: The call to send data through
459eb9950ebSDavid Howells  * @iter: The buffer to receive into
460f105da1aSDavid Howells  * @_len: The amount of data we want to receive (decreased on return)
461d001648eSDavid Howells  * @want_more: True if more data is expected to be read
462d001648eSDavid Howells  * @_abort: Where the abort code is stored if -ECONNABORTED is returned
463a68f4a27SDavid Howells  * @_service: Where to store the actual service ID (may be upgraded)
4648c3e34a4SDavid Howells  *
465d001648eSDavid Howells  * Allow a kernel service to receive data and pick up information about the
466d001648eSDavid Howells  * state of a call.  Returns 0 if got what was asked for and there's more
467d001648eSDavid Howells  * available, 1 if we got what was asked for and we're at the end of the data
468d001648eSDavid Howells  * and -EAGAIN if we need more data.
469d001648eSDavid Howells  *
470d001648eSDavid Howells  * Note that we may return -EAGAIN to drain empty packets at the end of the
471d001648eSDavid Howells  * data, even if we've already copied over the requested data.
472d001648eSDavid Howells  *
473d001648eSDavid Howells  * *_abort should also be initialised to 0.
4748c3e34a4SDavid Howells  */
475d001648eSDavid Howells int rxrpc_kernel_recv_data(struct socket *sock, struct rxrpc_call *call,
476f105da1aSDavid Howells 			   struct iov_iter *iter, size_t *_len,
477a68f4a27SDavid Howells 			   bool want_more, u32 *_abort, u16 *_service)
4788c3e34a4SDavid Howells {
479eb9950ebSDavid Howells 	size_t offset = 0;
480d001648eSDavid Howells 	int ret;
4818c3e34a4SDavid Howells 
482*93368b6bSDavid Howells 	_enter("{%d},%zu,%d", call->debug_id, *_len, want_more);
483d001648eSDavid Howells 
484540b1c48SDavid Howells 	mutex_lock(&call->user_mutex);
485d001648eSDavid Howells 
486*93368b6bSDavid Howells 	ret = rxrpc_recvmsg_data(sock, call, NULL, iter, *_len, 0, &offset);
487f105da1aSDavid Howells 	*_len -= offset;
488*93368b6bSDavid Howells 	if (ret == -EIO)
489*93368b6bSDavid Howells 		goto call_failed;
490d001648eSDavid Howells 	if (ret < 0)
491d001648eSDavid Howells 		goto out;
492d001648eSDavid Howells 
493*93368b6bSDavid Howells 	/* We can only reach here with a partially full buffer if we have
494*93368b6bSDavid Howells 	 * reached the end of the data.  We must otherwise have a full buffer
495*93368b6bSDavid Howells 	 * or have been given -EAGAIN.
496d001648eSDavid Howells 	 */
497d001648eSDavid Howells 	if (ret == 1) {
498eb9950ebSDavid Howells 		if (iov_iter_count(iter) > 0)
499d001648eSDavid Howells 			goto short_data;
500d001648eSDavid Howells 		if (!want_more)
501d001648eSDavid Howells 			goto read_phase_complete;
502d001648eSDavid Howells 		ret = 0;
503d001648eSDavid Howells 		goto out;
5048c3e34a4SDavid Howells 	}
5058c3e34a4SDavid Howells 
506d001648eSDavid Howells 	if (!want_more)
507d001648eSDavid Howells 		goto excess_data;
508d001648eSDavid Howells 	goto out;
509d001648eSDavid Howells 
510d001648eSDavid Howells read_phase_complete:
511d001648eSDavid Howells 	ret = 1;
512d001648eSDavid Howells out:
513a68f4a27SDavid Howells 	if (_service)
514f3441d41SDavid Howells 		*_service = call->dest_srx.srx_service;
515540b1c48SDavid Howells 	mutex_unlock(&call->user_mutex);
516eb9950ebSDavid Howells 	_leave(" = %d [%zu,%d]", ret, iov_iter_count(iter), *_abort);
517d001648eSDavid Howells 	return ret;
518d001648eSDavid Howells 
519d001648eSDavid Howells short_data:
52057af281eSDavid Howells 	trace_rxrpc_abort(call->debug_id, rxrpc_recvmsg_short_data,
52157af281eSDavid Howells 			  call->cid, call->call_id, call->rx_consumed,
52257af281eSDavid Howells 			  0, -EBADMSG);
523d001648eSDavid Howells 	ret = -EBADMSG;
524d001648eSDavid Howells 	goto out;
525d001648eSDavid Howells excess_data:
52657af281eSDavid Howells 	trace_rxrpc_abort(call->debug_id, rxrpc_recvmsg_excess_data,
52757af281eSDavid Howells 			  call->cid, call->call_id, call->rx_consumed,
52857af281eSDavid Howells 			  0, -EMSGSIZE);
529d001648eSDavid Howells 	ret = -EMSGSIZE;
530d001648eSDavid Howells 	goto out;
531*93368b6bSDavid Howells call_failed:
532d001648eSDavid Howells 	*_abort = call->abort_code;
5333a92789aSDavid Howells 	ret = call->error;
534d001648eSDavid Howells 	if (call->completion == RXRPC_CALL_SUCCEEDED) {
535d001648eSDavid Howells 		ret = 1;
536eb9950ebSDavid Howells 		if (iov_iter_count(iter) > 0)
537d001648eSDavid Howells 			ret = -ECONNRESET;
538d001648eSDavid Howells 	}
539d001648eSDavid Howells 	goto out;
540d001648eSDavid Howells }
541d001648eSDavid Howells EXPORT_SYMBOL(rxrpc_kernel_recv_data);
542