xref: /openbmc/linux/net/rxrpc/recvmsg.c (revision 639f181f)
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) {
3920acbd9aSDavid Howells 			spin_lock_bh(&call->notify_lock);
40248f219cSDavid Howells 			call->notify_rx(sk, call, call->user_call_ID);
4120acbd9aSDavid Howells 			spin_unlock_bh(&call->notify_lock);
42248f219cSDavid Howells 		} else {
43248f219cSDavid Howells 			write_lock_bh(&rx->recvmsg_lock);
44248f219cSDavid Howells 			if (list_empty(&call->recvmsg_link)) {
45248f219cSDavid Howells 				rxrpc_get_call(call, rxrpc_call_got);
46248f219cSDavid Howells 				list_add_tail(&call->recvmsg_link, &rx->recvmsg_q);
47248f219cSDavid Howells 			}
48248f219cSDavid Howells 			write_unlock_bh(&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 /*
623067bf8cSDavid Howells  * Transition a call to the complete state.
633067bf8cSDavid Howells  */
643067bf8cSDavid Howells bool __rxrpc_set_call_completion(struct rxrpc_call *call,
653067bf8cSDavid Howells 				 enum rxrpc_call_completion compl,
663067bf8cSDavid Howells 				 u32 abort_code,
673067bf8cSDavid Howells 				 int error)
683067bf8cSDavid Howells {
693067bf8cSDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE) {
703067bf8cSDavid Howells 		call->abort_code = abort_code;
713067bf8cSDavid Howells 		call->error = error;
723067bf8cSDavid Howells 		call->completion = compl,
733067bf8cSDavid Howells 		call->state = RXRPC_CALL_COMPLETE;
743067bf8cSDavid Howells 		trace_rxrpc_call_complete(call);
753067bf8cSDavid Howells 		wake_up(&call->waitq);
765ac0d622SDavid Howells 		rxrpc_notify_socket(call);
773067bf8cSDavid Howells 		return true;
783067bf8cSDavid Howells 	}
793067bf8cSDavid Howells 	return false;
803067bf8cSDavid Howells }
813067bf8cSDavid Howells 
823067bf8cSDavid Howells bool rxrpc_set_call_completion(struct rxrpc_call *call,
833067bf8cSDavid Howells 			       enum rxrpc_call_completion compl,
843067bf8cSDavid Howells 			       u32 abort_code,
853067bf8cSDavid Howells 			       int error)
863067bf8cSDavid Howells {
875ac0d622SDavid Howells 	bool ret = false;
883067bf8cSDavid Howells 
895ac0d622SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE) {
903067bf8cSDavid Howells 		write_lock_bh(&call->state_lock);
913067bf8cSDavid Howells 		ret = __rxrpc_set_call_completion(call, compl, abort_code, error);
923067bf8cSDavid Howells 		write_unlock_bh(&call->state_lock);
935ac0d622SDavid Howells 	}
943067bf8cSDavid Howells 	return ret;
953067bf8cSDavid Howells }
963067bf8cSDavid Howells 
973067bf8cSDavid Howells /*
983067bf8cSDavid Howells  * Record that a call successfully completed.
993067bf8cSDavid Howells  */
1003067bf8cSDavid Howells bool __rxrpc_call_completed(struct rxrpc_call *call)
1013067bf8cSDavid Howells {
1023067bf8cSDavid Howells 	return __rxrpc_set_call_completion(call, RXRPC_CALL_SUCCEEDED, 0, 0);
1033067bf8cSDavid Howells }
1043067bf8cSDavid Howells 
1053067bf8cSDavid Howells bool rxrpc_call_completed(struct rxrpc_call *call)
1063067bf8cSDavid Howells {
1075ac0d622SDavid Howells 	bool ret = false;
1083067bf8cSDavid Howells 
1095ac0d622SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE) {
1103067bf8cSDavid Howells 		write_lock_bh(&call->state_lock);
1113067bf8cSDavid Howells 		ret = __rxrpc_call_completed(call);
1123067bf8cSDavid Howells 		write_unlock_bh(&call->state_lock);
1135ac0d622SDavid Howells 	}
1143067bf8cSDavid Howells 	return ret;
1153067bf8cSDavid Howells }
1163067bf8cSDavid Howells 
1173067bf8cSDavid Howells /*
1183067bf8cSDavid Howells  * Record that a call is locally aborted.
1193067bf8cSDavid Howells  */
1203067bf8cSDavid Howells bool __rxrpc_abort_call(const char *why, struct rxrpc_call *call,
1213067bf8cSDavid Howells 			rxrpc_seq_t seq, u32 abort_code, int error)
1223067bf8cSDavid Howells {
1233067bf8cSDavid Howells 	trace_rxrpc_abort(call->debug_id, why, call->cid, call->call_id, seq,
1243067bf8cSDavid Howells 			  abort_code, error);
1253067bf8cSDavid Howells 	return __rxrpc_set_call_completion(call, RXRPC_CALL_LOCALLY_ABORTED,
1263067bf8cSDavid Howells 					   abort_code, error);
1273067bf8cSDavid Howells }
1283067bf8cSDavid Howells 
1293067bf8cSDavid Howells bool rxrpc_abort_call(const char *why, struct rxrpc_call *call,
1303067bf8cSDavid Howells 		      rxrpc_seq_t seq, u32 abort_code, int error)
1313067bf8cSDavid Howells {
1323067bf8cSDavid Howells 	bool ret;
1333067bf8cSDavid Howells 
1343067bf8cSDavid Howells 	write_lock_bh(&call->state_lock);
1353067bf8cSDavid Howells 	ret = __rxrpc_abort_call(why, call, seq, abort_code, error);
1363067bf8cSDavid Howells 	write_unlock_bh(&call->state_lock);
1373067bf8cSDavid Howells 	return ret;
1383067bf8cSDavid Howells }
1393067bf8cSDavid Howells 
1403067bf8cSDavid Howells /*
141248f219cSDavid Howells  * Pass a call terminating message to userspace.
142248f219cSDavid Howells  */
143248f219cSDavid Howells static int rxrpc_recvmsg_term(struct rxrpc_call *call, struct msghdr *msg)
144248f219cSDavid Howells {
145248f219cSDavid Howells 	u32 tmp = 0;
146248f219cSDavid Howells 	int ret;
147248f219cSDavid Howells 
148248f219cSDavid Howells 	switch (call->completion) {
149248f219cSDavid Howells 	case RXRPC_CALL_SUCCEEDED:
150248f219cSDavid Howells 		ret = 0;
151248f219cSDavid Howells 		if (rxrpc_is_service_call(call))
152248f219cSDavid Howells 			ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ACK, 0, &tmp);
153248f219cSDavid Howells 		break;
154248f219cSDavid Howells 	case RXRPC_CALL_REMOTELY_ABORTED:
155248f219cSDavid Howells 		tmp = call->abort_code;
156248f219cSDavid Howells 		ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &tmp);
157248f219cSDavid Howells 		break;
158248f219cSDavid Howells 	case RXRPC_CALL_LOCALLY_ABORTED:
159248f219cSDavid Howells 		tmp = call->abort_code;
160248f219cSDavid Howells 		ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &tmp);
161248f219cSDavid Howells 		break;
162248f219cSDavid Howells 	case RXRPC_CALL_NETWORK_ERROR:
1633a92789aSDavid Howells 		tmp = -call->error;
164248f219cSDavid Howells 		ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NET_ERROR, 4, &tmp);
165248f219cSDavid Howells 		break;
166248f219cSDavid Howells 	case RXRPC_CALL_LOCAL_ERROR:
1673a92789aSDavid Howells 		tmp = -call->error;
168248f219cSDavid Howells 		ret = put_cmsg(msg, SOL_RXRPC, RXRPC_LOCAL_ERROR, 4, &tmp);
169248f219cSDavid Howells 		break;
170248f219cSDavid Howells 	default:
171248f219cSDavid Howells 		pr_err("Invalid terminal call state %u\n", call->state);
172248f219cSDavid Howells 		BUG();
173248f219cSDavid Howells 		break;
174248f219cSDavid Howells 	}
175248f219cSDavid Howells 
17684997905SDavid Howells 	trace_rxrpc_recvmsg(call, rxrpc_recvmsg_terminal, call->rx_hard_ack,
17784997905SDavid Howells 			    call->rx_pkt_offset, call->rx_pkt_len, ret);
178248f219cSDavid Howells 	return ret;
179248f219cSDavid Howells }
180248f219cSDavid Howells 
181248f219cSDavid Howells /*
182248f219cSDavid Howells  * Pass back notification of a new call.  The call is added to the
183248f219cSDavid Howells  * to-be-accepted list.  This means that the next call to be accepted might not
184248f219cSDavid Howells  * be the last call seen awaiting acceptance, but unless we leave this on the
185248f219cSDavid Howells  * front of the queue and block all other messages until someone gives us a
186248f219cSDavid Howells  * user_ID for it, there's not a lot we can do.
187248f219cSDavid Howells  */
188248f219cSDavid Howells static int rxrpc_recvmsg_new_call(struct rxrpc_sock *rx,
189248f219cSDavid Howells 				  struct rxrpc_call *call,
190248f219cSDavid Howells 				  struct msghdr *msg, int flags)
191248f219cSDavid Howells {
192248f219cSDavid Howells 	int tmp = 0, ret;
193248f219cSDavid Howells 
194248f219cSDavid Howells 	ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NEW_CALL, 0, &tmp);
195248f219cSDavid Howells 
196248f219cSDavid Howells 	if (ret == 0 && !(flags & MSG_PEEK)) {
197248f219cSDavid Howells 		_debug("to be accepted");
198248f219cSDavid Howells 		write_lock_bh(&rx->recvmsg_lock);
199248f219cSDavid Howells 		list_del_init(&call->recvmsg_link);
200248f219cSDavid Howells 		write_unlock_bh(&rx->recvmsg_lock);
201248f219cSDavid Howells 
2023432a757SDavid Howells 		rxrpc_get_call(call, rxrpc_call_got);
203248f219cSDavid Howells 		write_lock(&rx->call_lock);
204248f219cSDavid Howells 		list_add_tail(&call->accept_link, &rx->to_be_accepted);
205248f219cSDavid Howells 		write_unlock(&rx->call_lock);
206248f219cSDavid Howells 	}
207248f219cSDavid Howells 
20884997905SDavid Howells 	trace_rxrpc_recvmsg(call, rxrpc_recvmsg_to_be_accepted, 1, 0, 0, ret);
209248f219cSDavid Howells 	return ret;
210248f219cSDavid Howells }
211248f219cSDavid Howells 
212248f219cSDavid Howells /*
213248f219cSDavid Howells  * End the packet reception phase.
214248f219cSDavid Howells  */
215b69d94d7SDavid Howells static void rxrpc_end_rx_phase(struct rxrpc_call *call, rxrpc_serial_t serial)
216248f219cSDavid Howells {
217248f219cSDavid Howells 	_enter("%d,%s", call->debug_id, rxrpc_call_states[call->state]);
218248f219cSDavid Howells 
21958dc63c9SDavid Howells 	trace_rxrpc_receive(call, rxrpc_receive_end, 0, call->rx_top);
220816c9fceSDavid Howells 	ASSERTCMP(call->rx_hard_ack, ==, call->rx_top);
221816c9fceSDavid Howells 
222248f219cSDavid Howells 	if (call->state == RXRPC_CALL_CLIENT_RECV_REPLY) {
223e8c3af6bSDavid Howells 		rxrpc_propose_ACK(call, RXRPC_ACK_IDLE, serial, false, true,
2249c7ad434SDavid Howells 				  rxrpc_propose_ack_terminal_ack);
225a71a2651SDavid Howells 		//rxrpc_send_ack_packet(call, false, NULL);
226248f219cSDavid Howells 	}
227248f219cSDavid Howells 
228248f219cSDavid Howells 	write_lock_bh(&call->state_lock);
229248f219cSDavid Howells 
230248f219cSDavid Howells 	switch (call->state) {
231248f219cSDavid Howells 	case RXRPC_CALL_CLIENT_RECV_REPLY:
232248f219cSDavid Howells 		__rxrpc_call_completed(call);
2339749fd2bSDavid Howells 		write_unlock_bh(&call->state_lock);
234248f219cSDavid Howells 		break;
235248f219cSDavid Howells 
236248f219cSDavid Howells 	case RXRPC_CALL_SERVER_RECV_REQUEST:
23771f3ca40SDavid Howells 		call->tx_phase = true;
238248f219cSDavid Howells 		call->state = RXRPC_CALL_SERVER_ACK_REQUEST;
239a158bdd3SDavid Howells 		call->expect_req_by = jiffies + MAX_JIFFY_OFFSET;
2409749fd2bSDavid Howells 		write_unlock_bh(&call->state_lock);
241e8c3af6bSDavid Howells 		rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, serial, false, true,
2429749fd2bSDavid Howells 				  rxrpc_propose_ack_processing_op);
243248f219cSDavid Howells 		break;
244248f219cSDavid Howells 	default:
2459749fd2bSDavid Howells 		write_unlock_bh(&call->state_lock);
246248f219cSDavid Howells 		break;
247248f219cSDavid Howells 	}
248248f219cSDavid Howells }
249248f219cSDavid Howells 
250248f219cSDavid Howells /*
251248f219cSDavid Howells  * Discard a packet we've used up and advance the Rx window by one.
252248f219cSDavid Howells  */
253248f219cSDavid Howells static void rxrpc_rotate_rx_window(struct rxrpc_call *call)
254248f219cSDavid Howells {
255816c9fceSDavid Howells 	struct rxrpc_skb_priv *sp;
256248f219cSDavid Howells 	struct sk_buff *skb;
25758dc63c9SDavid Howells 	rxrpc_serial_t serial;
258248f219cSDavid Howells 	rxrpc_seq_t hard_ack, top;
259e2de6c40SDavid Howells 	bool last = false;
260e2de6c40SDavid Howells 	u8 subpacket;
261248f219cSDavid Howells 	int ix;
262248f219cSDavid Howells 
263248f219cSDavid Howells 	_enter("%d", call->debug_id);
264248f219cSDavid Howells 
265248f219cSDavid Howells 	hard_ack = call->rx_hard_ack;
266248f219cSDavid Howells 	top = smp_load_acquire(&call->rx_top);
267248f219cSDavid Howells 	ASSERT(before(hard_ack, top));
268248f219cSDavid Howells 
269248f219cSDavid Howells 	hard_ack++;
270248f219cSDavid Howells 	ix = hard_ack & RXRPC_RXTX_BUFF_MASK;
271248f219cSDavid Howells 	skb = call->rxtx_buffer[ix];
272987db9f7SDavid Howells 	rxrpc_see_skb(skb, rxrpc_skb_rotated);
273816c9fceSDavid Howells 	sp = rxrpc_skb(skb);
274e2de6c40SDavid Howells 
275e2de6c40SDavid Howells 	subpacket = call->rxtx_annotations[ix] & RXRPC_RX_ANNO_SUBPACKET;
276e2de6c40SDavid Howells 	serial = sp->hdr.serial + subpacket;
277e2de6c40SDavid Howells 
278e2de6c40SDavid Howells 	if (subpacket == sp->nr_subpackets - 1 &&
279e2de6c40SDavid Howells 	    sp->rx_flags & RXRPC_SKB_INCL_LAST)
280e2de6c40SDavid Howells 		last = true;
28158dc63c9SDavid Howells 
282248f219cSDavid Howells 	call->rxtx_buffer[ix] = NULL;
283248f219cSDavid Howells 	call->rxtx_annotations[ix] = 0;
284248f219cSDavid Howells 	/* Barrier against rxrpc_input_data(). */
285248f219cSDavid Howells 	smp_store_release(&call->rx_hard_ack, hard_ack);
286248f219cSDavid Howells 
287987db9f7SDavid Howells 	rxrpc_free_skb(skb, rxrpc_skb_freed);
288248f219cSDavid Howells 
28958dc63c9SDavid Howells 	trace_rxrpc_receive(call, rxrpc_receive_rotate, serial, hard_ack);
290e2de6c40SDavid Howells 	if (last) {
291b69d94d7SDavid Howells 		rxrpc_end_rx_phase(call, serial);
292805b21b9SDavid Howells 	} else {
293805b21b9SDavid Howells 		/* Check to see if there's an ACK that needs sending. */
294805b21b9SDavid Howells 		if (after_eq(hard_ack, call->ackr_consumed + 2) ||
295805b21b9SDavid Howells 		    after_eq(top, call->ackr_seen + 2) ||
296805b21b9SDavid Howells 		    (hard_ack == top && after(hard_ack, call->ackr_consumed)))
297e8c3af6bSDavid Howells 			rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, serial,
2988637abaaSDavid Howells 					  true, true,
299805b21b9SDavid Howells 					  rxrpc_propose_ack_rotate_rx);
3008637abaaSDavid Howells 		if (call->ackr_reason && call->ackr_reason != RXRPC_ACK_DELAY)
301bd1fdf8cSDavid Howells 			rxrpc_send_ack_packet(call, false, NULL);
302805b21b9SDavid Howells 	}
303248f219cSDavid Howells }
304248f219cSDavid Howells 
305248f219cSDavid Howells /*
306248f219cSDavid Howells  * Decrypt and verify a (sub)packet.  The packet's length may be changed due to
307248f219cSDavid Howells  * padding, but if this is the case, the packet length will be resident in the
308248f219cSDavid Howells  * socket buffer.  Note that we can't modify the master skb info as the skb may
309248f219cSDavid Howells  * be the home to multiple subpackets.
310248f219cSDavid Howells  */
311248f219cSDavid Howells static int rxrpc_verify_packet(struct rxrpc_call *call, struct sk_buff *skb,
312248f219cSDavid Howells 			       u8 annotation,
313248f219cSDavid Howells 			       unsigned int offset, unsigned int len)
314248f219cSDavid Howells {
315248f219cSDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
316248f219cSDavid Howells 	rxrpc_seq_t seq = sp->hdr.seq;
317248f219cSDavid Howells 	u16 cksum = sp->hdr.cksum;
318e2de6c40SDavid Howells 	u8 subpacket = annotation & RXRPC_RX_ANNO_SUBPACKET;
319248f219cSDavid Howells 
320248f219cSDavid Howells 	_enter("");
321248f219cSDavid Howells 
322248f219cSDavid Howells 	/* For all but the head jumbo subpacket, the security checksum is in a
323248f219cSDavid Howells 	 * jumbo header immediately prior to the data.
324248f219cSDavid Howells 	 */
325e2de6c40SDavid Howells 	if (subpacket > 0) {
326248f219cSDavid Howells 		__be16 tmp;
327248f219cSDavid Howells 		if (skb_copy_bits(skb, offset - 2, &tmp, 2) < 0)
328248f219cSDavid Howells 			BUG();
329248f219cSDavid Howells 		cksum = ntohs(tmp);
330e2de6c40SDavid Howells 		seq += subpacket;
331248f219cSDavid Howells 	}
332248f219cSDavid Howells 
33391fcfbe8SDavid Howells 	return call->security->verify_packet(call, skb, offset, len,
334248f219cSDavid Howells 					     seq, cksum);
335248f219cSDavid Howells }
336248f219cSDavid Howells 
337248f219cSDavid Howells /*
338248f219cSDavid Howells  * Locate the data within a packet.  This is complicated by:
339248f219cSDavid Howells  *
340248f219cSDavid Howells  * (1) An skb may contain a jumbo packet - so we have to find the appropriate
341248f219cSDavid Howells  *     subpacket.
342248f219cSDavid Howells  *
343248f219cSDavid Howells  * (2) The (sub)packets may be encrypted and, if so, the encrypted portion
344248f219cSDavid Howells  *     contains an extra header which includes the true length of the data,
345248f219cSDavid Howells  *     excluding any encrypted padding.
346248f219cSDavid Howells  */
347248f219cSDavid Howells static int rxrpc_locate_data(struct rxrpc_call *call, struct sk_buff *skb,
348248f219cSDavid Howells 			     u8 *_annotation,
349f9c32435SDavid Howells 			     unsigned int *_offset, unsigned int *_len,
350f9c32435SDavid Howells 			     bool *_last)
351248f219cSDavid Howells {
352e2de6c40SDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
353775e5b71SDavid Howells 	unsigned int offset = sizeof(struct rxrpc_wire_header);
354650b4ecaSColin Ian King 	unsigned int len;
355f9c32435SDavid Howells 	bool last = false;
356248f219cSDavid Howells 	int ret;
357248f219cSDavid Howells 	u8 annotation = *_annotation;
358e2de6c40SDavid Howells 	u8 subpacket = annotation & RXRPC_RX_ANNO_SUBPACKET;
359248f219cSDavid Howells 
360248f219cSDavid Howells 	/* Locate the subpacket */
361e2de6c40SDavid Howells 	offset += subpacket * RXRPC_JUMBO_SUBPKTLEN;
362775e5b71SDavid Howells 	len = skb->len - offset;
363e2de6c40SDavid Howells 	if (subpacket < sp->nr_subpackets - 1)
364e2de6c40SDavid Howells 		len = RXRPC_JUMBO_DATALEN;
365f9c32435SDavid Howells 	else if (sp->rx_flags & RXRPC_SKB_INCL_LAST)
366f9c32435SDavid Howells 		last = true;
367248f219cSDavid Howells 
368248f219cSDavid Howells 	if (!(annotation & RXRPC_RX_ANNO_VERIFIED)) {
369248f219cSDavid Howells 		ret = rxrpc_verify_packet(call, skb, annotation, offset, len);
370248f219cSDavid Howells 		if (ret < 0)
371248f219cSDavid Howells 			return ret;
372248f219cSDavid Howells 		*_annotation |= RXRPC_RX_ANNO_VERIFIED;
373248f219cSDavid Howells 	}
374248f219cSDavid Howells 
375248f219cSDavid Howells 	*_offset = offset;
376248f219cSDavid Howells 	*_len = len;
377f9c32435SDavid Howells 	*_last = last;
37891fcfbe8SDavid Howells 	call->security->locate_data(call, skb, _offset, _len);
379248f219cSDavid Howells 	return 0;
380248f219cSDavid Howells }
381248f219cSDavid Howells 
382248f219cSDavid Howells /*
383248f219cSDavid Howells  * Deliver messages to a call.  This keeps processing packets until the buffer
384248f219cSDavid Howells  * is filled and we find either more DATA (returns 0) or the end of the DATA
385248f219cSDavid Howells  * (returns 1).  If more packets are required, it returns -EAGAIN.
386248f219cSDavid Howells  */
387248f219cSDavid Howells static int rxrpc_recvmsg_data(struct socket *sock, struct rxrpc_call *call,
388248f219cSDavid Howells 			      struct msghdr *msg, struct iov_iter *iter,
389248f219cSDavid Howells 			      size_t len, int flags, size_t *_offset)
390248f219cSDavid Howells {
391248f219cSDavid Howells 	struct rxrpc_skb_priv *sp;
392248f219cSDavid Howells 	struct sk_buff *skb;
393e2de6c40SDavid Howells 	rxrpc_serial_t serial;
394248f219cSDavid Howells 	rxrpc_seq_t hard_ack, top, seq;
395248f219cSDavid Howells 	size_t remain;
396f9c32435SDavid Howells 	bool rx_pkt_last;
397248f219cSDavid Howells 	unsigned int rx_pkt_offset, rx_pkt_len;
398816c9fceSDavid Howells 	int ix, copy, ret = -EAGAIN, ret2;
399248f219cSDavid Howells 
400d0b35a42SDavid Howells 	if (test_and_clear_bit(RXRPC_CALL_RX_UNDERRUN, &call->flags) &&
401d0b35a42SDavid Howells 	    call->ackr_reason)
402d0b35a42SDavid Howells 		rxrpc_send_ack_packet(call, false, NULL);
403d0b35a42SDavid Howells 
404248f219cSDavid Howells 	rx_pkt_offset = call->rx_pkt_offset;
405248f219cSDavid Howells 	rx_pkt_len = call->rx_pkt_len;
406f9c32435SDavid Howells 	rx_pkt_last = call->rx_pkt_last;
407248f219cSDavid Howells 
408816c9fceSDavid Howells 	if (call->state >= RXRPC_CALL_SERVER_ACK_REQUEST) {
409816c9fceSDavid Howells 		seq = call->rx_hard_ack;
410816c9fceSDavid Howells 		ret = 1;
411816c9fceSDavid Howells 		goto done;
412816c9fceSDavid Howells 	}
413816c9fceSDavid Howells 
414248f219cSDavid Howells 	/* Barriers against rxrpc_input_data(). */
415248f219cSDavid Howells 	hard_ack = call->rx_hard_ack;
416d7e15835SDavid Howells 	seq = hard_ack + 1;
417f9c32435SDavid Howells 
418d7e15835SDavid Howells 	while (top = smp_load_acquire(&call->rx_top),
419d7e15835SDavid Howells 	       before_eq(seq, top)
420d7e15835SDavid Howells 	       ) {
421248f219cSDavid Howells 		ix = seq & RXRPC_RXTX_BUFF_MASK;
422248f219cSDavid Howells 		skb = call->rxtx_buffer[ix];
42384997905SDavid Howells 		if (!skb) {
42484997905SDavid Howells 			trace_rxrpc_recvmsg(call, rxrpc_recvmsg_hole, seq,
42584997905SDavid Howells 					    rx_pkt_offset, rx_pkt_len, 0);
426248f219cSDavid Howells 			break;
42784997905SDavid Howells 		}
428248f219cSDavid Howells 		smp_rmb();
429987db9f7SDavid Howells 		rxrpc_see_skb(skb, rxrpc_skb_seen);
430248f219cSDavid Howells 		sp = rxrpc_skb(skb);
431248f219cSDavid Howells 
432e2de6c40SDavid Howells 		if (!(flags & MSG_PEEK)) {
433e2de6c40SDavid Howells 			serial = sp->hdr.serial;
434e2de6c40SDavid Howells 			serial += call->rxtx_annotations[ix] & RXRPC_RX_ANNO_SUBPACKET;
43558dc63c9SDavid Howells 			trace_rxrpc_receive(call, rxrpc_receive_front,
436e2de6c40SDavid Howells 					    serial, seq);
437e2de6c40SDavid Howells 		}
43858dc63c9SDavid Howells 
439248f219cSDavid Howells 		if (msg)
440248f219cSDavid Howells 			sock_recv_timestamp(msg, sock->sk, skb);
441248f219cSDavid Howells 
4422e2ea51dSDavid Howells 		if (rx_pkt_offset == 0) {
443816c9fceSDavid Howells 			ret2 = rxrpc_locate_data(call, skb,
4444b22457cSDavid Howells 						 &call->rxtx_annotations[ix],
445f9c32435SDavid Howells 						 &rx_pkt_offset, &rx_pkt_len,
446f9c32435SDavid Howells 						 &rx_pkt_last);
44784997905SDavid Howells 			trace_rxrpc_recvmsg(call, rxrpc_recvmsg_next, seq,
44884997905SDavid Howells 					    rx_pkt_offset, rx_pkt_len, ret2);
449816c9fceSDavid Howells 			if (ret2 < 0) {
450816c9fceSDavid Howells 				ret = ret2;
4512e2ea51dSDavid Howells 				goto out;
4522e2ea51dSDavid Howells 			}
45384997905SDavid Howells 		} else {
45484997905SDavid Howells 			trace_rxrpc_recvmsg(call, rxrpc_recvmsg_cont, seq,
45584997905SDavid Howells 					    rx_pkt_offset, rx_pkt_len, 0);
456816c9fceSDavid Howells 		}
457248f219cSDavid Howells 
458248f219cSDavid Howells 		/* We have to handle short, empty and used-up DATA packets. */
459248f219cSDavid Howells 		remain = len - *_offset;
460248f219cSDavid Howells 		copy = rx_pkt_len;
461248f219cSDavid Howells 		if (copy > remain)
462248f219cSDavid Howells 			copy = remain;
463248f219cSDavid Howells 		if (copy > 0) {
464816c9fceSDavid Howells 			ret2 = skb_copy_datagram_iter(skb, rx_pkt_offset, iter,
465248f219cSDavid Howells 						      copy);
466816c9fceSDavid Howells 			if (ret2 < 0) {
467816c9fceSDavid Howells 				ret = ret2;
468248f219cSDavid Howells 				goto out;
469816c9fceSDavid Howells 			}
470248f219cSDavid Howells 
471248f219cSDavid Howells 			/* handle piecemeal consumption of data packets */
472248f219cSDavid Howells 			rx_pkt_offset += copy;
473248f219cSDavid Howells 			rx_pkt_len -= copy;
474248f219cSDavid Howells 			*_offset += copy;
475248f219cSDavid Howells 		}
476248f219cSDavid Howells 
477248f219cSDavid Howells 		if (rx_pkt_len > 0) {
47884997905SDavid Howells 			trace_rxrpc_recvmsg(call, rxrpc_recvmsg_full, seq,
47984997905SDavid Howells 					    rx_pkt_offset, rx_pkt_len, 0);
480248f219cSDavid Howells 			ASSERTCMP(*_offset, ==, len);
481816c9fceSDavid Howells 			ret = 0;
482248f219cSDavid Howells 			break;
483248f219cSDavid Howells 		}
484248f219cSDavid Howells 
485248f219cSDavid Howells 		/* The whole packet has been transferred. */
486248f219cSDavid Howells 		if (!(flags & MSG_PEEK))
487248f219cSDavid Howells 			rxrpc_rotate_rx_window(call);
488248f219cSDavid Howells 		rx_pkt_offset = 0;
489248f219cSDavid Howells 		rx_pkt_len = 0;
490248f219cSDavid Howells 
491f9c32435SDavid Howells 		if (rx_pkt_last) {
492816c9fceSDavid Howells 			ASSERTCMP(seq, ==, READ_ONCE(call->rx_top));
493816c9fceSDavid Howells 			ret = 1;
494816c9fceSDavid Howells 			goto out;
495816c9fceSDavid Howells 		}
496d7e15835SDavid Howells 
497d7e15835SDavid Howells 		seq++;
498248f219cSDavid Howells 	}
499248f219cSDavid Howells 
500248f219cSDavid Howells out:
501248f219cSDavid Howells 	if (!(flags & MSG_PEEK)) {
502248f219cSDavid Howells 		call->rx_pkt_offset = rx_pkt_offset;
503248f219cSDavid Howells 		call->rx_pkt_len = rx_pkt_len;
504f9c32435SDavid Howells 		call->rx_pkt_last = rx_pkt_last;
505248f219cSDavid Howells 	}
506816c9fceSDavid Howells done:
50784997905SDavid Howells 	trace_rxrpc_recvmsg(call, rxrpc_recvmsg_data_return, seq,
50884997905SDavid Howells 			    rx_pkt_offset, rx_pkt_len, ret);
509d0b35a42SDavid Howells 	if (ret == -EAGAIN)
510d0b35a42SDavid Howells 		set_bit(RXRPC_CALL_RX_UNDERRUN, &call->flags);
511248f219cSDavid Howells 	return ret;
512248f219cSDavid Howells }
513248f219cSDavid Howells 
514248f219cSDavid Howells /*
515248f219cSDavid Howells  * Receive a message from an RxRPC socket
5168c3e34a4SDavid Howells  * - we need to be careful about two or more threads calling recvmsg
5178c3e34a4SDavid Howells  *   simultaneously
5188c3e34a4SDavid Howells  */
5198c3e34a4SDavid Howells int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
5208c3e34a4SDavid Howells 		  int flags)
5218c3e34a4SDavid Howells {
522248f219cSDavid Howells 	struct rxrpc_call *call;
5238c3e34a4SDavid Howells 	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
524248f219cSDavid Howells 	struct list_head *l;
525248f219cSDavid Howells 	size_t copied = 0;
5268c3e34a4SDavid Howells 	long timeo;
527248f219cSDavid Howells 	int ret;
5288c3e34a4SDavid Howells 
5298c3e34a4SDavid Howells 	DEFINE_WAIT(wait);
5308c3e34a4SDavid Howells 
53184997905SDavid Howells 	trace_rxrpc_recvmsg(NULL, rxrpc_recvmsg_enter, 0, 0, 0, 0);
5328c3e34a4SDavid Howells 
5338c3e34a4SDavid Howells 	if (flags & (MSG_OOB | MSG_TRUNC))
5348c3e34a4SDavid Howells 		return -EOPNOTSUPP;
5358c3e34a4SDavid Howells 
5368c3e34a4SDavid Howells 	timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT);
5378c3e34a4SDavid Howells 
538248f219cSDavid Howells try_again:
5398c3e34a4SDavid Howells 	lock_sock(&rx->sk);
5408c3e34a4SDavid Howells 
541248f219cSDavid Howells 	/* Return immediately if a client socket has no outstanding calls */
542248f219cSDavid Howells 	if (RB_EMPTY_ROOT(&rx->calls) &&
543248f219cSDavid Howells 	    list_empty(&rx->recvmsg_q) &&
544248f219cSDavid Howells 	    rx->sk.sk_state != RXRPC_SERVER_LISTENING) {
5458c3e34a4SDavid Howells 		release_sock(&rx->sk);
546639f181fSDavid Howells 		return -EAGAIN;
5478c3e34a4SDavid Howells 	}
5488c3e34a4SDavid Howells 
549248f219cSDavid Howells 	if (list_empty(&rx->recvmsg_q)) {
550248f219cSDavid Howells 		ret = -EWOULDBLOCK;
55184997905SDavid Howells 		if (timeo == 0) {
55284997905SDavid Howells 			call = NULL;
553248f219cSDavid Howells 			goto error_no_call;
55484997905SDavid Howells 		}
5558c3e34a4SDavid Howells 
5568c3e34a4SDavid Howells 		release_sock(&rx->sk);
557248f219cSDavid Howells 
558248f219cSDavid Howells 		/* Wait for something to happen */
5598c3e34a4SDavid Howells 		prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait,
5608c3e34a4SDavid Howells 					  TASK_INTERRUPTIBLE);
5618c3e34a4SDavid Howells 		ret = sock_error(&rx->sk);
5628c3e34a4SDavid Howells 		if (ret)
5638c3e34a4SDavid Howells 			goto wait_error;
5648c3e34a4SDavid Howells 
565248f219cSDavid Howells 		if (list_empty(&rx->recvmsg_q)) {
5668c3e34a4SDavid Howells 			if (signal_pending(current))
5678c3e34a4SDavid Howells 				goto wait_interrupted;
56884997905SDavid Howells 			trace_rxrpc_recvmsg(NULL, rxrpc_recvmsg_wait,
56984997905SDavid Howells 					    0, 0, 0, 0);
5708c3e34a4SDavid Howells 			timeo = schedule_timeout(timeo);
5718c3e34a4SDavid Howells 		}
5728c3e34a4SDavid Howells 		finish_wait(sk_sleep(&rx->sk), &wait);
573248f219cSDavid Howells 		goto try_again;
5748c3e34a4SDavid Howells 	}
5758c3e34a4SDavid Howells 
576248f219cSDavid Howells 	/* Find the next call and dequeue it if we're not just peeking.  If we
577248f219cSDavid Howells 	 * do dequeue it, that comes with a ref that we will need to release.
578248f219cSDavid Howells 	 */
579248f219cSDavid Howells 	write_lock_bh(&rx->recvmsg_lock);
580248f219cSDavid Howells 	l = rx->recvmsg_q.next;
581248f219cSDavid Howells 	call = list_entry(l, struct rxrpc_call, recvmsg_link);
582248f219cSDavid Howells 	if (!(flags & MSG_PEEK))
583248f219cSDavid Howells 		list_del_init(&call->recvmsg_link);
584248f219cSDavid Howells 	else
585fff72429SDavid Howells 		rxrpc_get_call(call, rxrpc_call_got);
586248f219cSDavid Howells 	write_unlock_bh(&rx->recvmsg_lock);
5878c3e34a4SDavid Howells 
58884997905SDavid Howells 	trace_rxrpc_recvmsg(call, rxrpc_recvmsg_dequeue, 0, 0, 0, 0);
589248f219cSDavid Howells 
590540b1c48SDavid Howells 	/* We're going to drop the socket lock, so we need to lock the call
591540b1c48SDavid Howells 	 * against interference by sendmsg.
592540b1c48SDavid Howells 	 */
593540b1c48SDavid Howells 	if (!mutex_trylock(&call->user_mutex)) {
594540b1c48SDavid Howells 		ret = -EWOULDBLOCK;
595540b1c48SDavid Howells 		if (flags & MSG_DONTWAIT)
596540b1c48SDavid Howells 			goto error_requeue_call;
597540b1c48SDavid Howells 		ret = -ERESTARTSYS;
598540b1c48SDavid Howells 		if (mutex_lock_interruptible(&call->user_mutex) < 0)
599540b1c48SDavid Howells 			goto error_requeue_call;
600540b1c48SDavid Howells 	}
601540b1c48SDavid Howells 
602540b1c48SDavid Howells 	release_sock(&rx->sk);
603540b1c48SDavid Howells 
604248f219cSDavid Howells 	if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
605248f219cSDavid Howells 		BUG();
606248f219cSDavid Howells 
607248f219cSDavid Howells 	if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
608248f219cSDavid Howells 		if (flags & MSG_CMSG_COMPAT) {
609248f219cSDavid Howells 			unsigned int id32 = call->user_call_ID;
610248f219cSDavid Howells 
611248f219cSDavid Howells 			ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
612248f219cSDavid Howells 				       sizeof(unsigned int), &id32);
613248f219cSDavid Howells 		} else {
614a16b8d0cSDavid Howells 			unsigned long idl = call->user_call_ID;
615a16b8d0cSDavid Howells 
616248f219cSDavid Howells 			ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
617a16b8d0cSDavid Howells 				       sizeof(unsigned long), &idl);
618248f219cSDavid Howells 		}
619248f219cSDavid Howells 		if (ret < 0)
620540b1c48SDavid Howells 			goto error_unlock_call;
621248f219cSDavid Howells 	}
622248f219cSDavid Howells 
6238c3e34a4SDavid Howells 	if (msg->msg_name) {
62468d6d1aeSDavid Howells 		struct sockaddr_rxrpc *srx = msg->msg_name;
62568d6d1aeSDavid Howells 		size_t len = sizeof(call->peer->srx);
62668d6d1aeSDavid Howells 
62768d6d1aeSDavid Howells 		memcpy(msg->msg_name, &call->peer->srx, len);
62868d6d1aeSDavid Howells 		srx->srx_service = call->service_id;
6298c3e34a4SDavid Howells 		msg->msg_namelen = len;
6308c3e34a4SDavid Howells 	}
6318c3e34a4SDavid Howells 
632146d8fefSDavid Howells 	switch (READ_ONCE(call->state)) {
633248f219cSDavid Howells 	case RXRPC_CALL_SERVER_ACCEPTING:
634248f219cSDavid Howells 		ret = rxrpc_recvmsg_new_call(rx, call, msg, flags);
6358c3e34a4SDavid Howells 		break;
636248f219cSDavid Howells 	case RXRPC_CALL_CLIENT_RECV_REPLY:
637248f219cSDavid Howells 	case RXRPC_CALL_SERVER_RECV_REQUEST:
638248f219cSDavid Howells 	case RXRPC_CALL_SERVER_ACK_REQUEST:
639248f219cSDavid Howells 		ret = rxrpc_recvmsg_data(sock, call, msg, &msg->msg_iter, len,
640248f219cSDavid Howells 					 flags, &copied);
641248f219cSDavid Howells 		if (ret == -EAGAIN)
642248f219cSDavid Howells 			ret = 0;
64333b603fdSDavid Howells 
64433b603fdSDavid Howells 		if (after(call->rx_top, call->rx_hard_ack) &&
64533b603fdSDavid Howells 		    call->rxtx_buffer[(call->rx_hard_ack + 1) & RXRPC_RXTX_BUFF_MASK])
64633b603fdSDavid Howells 			rxrpc_notify_socket(call);
6478c3e34a4SDavid Howells 		break;
6488c3e34a4SDavid Howells 	default:
649248f219cSDavid Howells 		ret = 0;
6508c3e34a4SDavid Howells 		break;
6518c3e34a4SDavid Howells 	}
6528c3e34a4SDavid Howells 
6538c3e34a4SDavid Howells 	if (ret < 0)
654540b1c48SDavid Howells 		goto error_unlock_call;
6558c3e34a4SDavid Howells 
656248f219cSDavid Howells 	if (call->state == RXRPC_CALL_COMPLETE) {
657248f219cSDavid Howells 		ret = rxrpc_recvmsg_term(call, msg);
658248f219cSDavid Howells 		if (ret < 0)
659540b1c48SDavid Howells 			goto error_unlock_call;
660248f219cSDavid Howells 		if (!(flags & MSG_PEEK))
6618d94aa38SDavid Howells 			rxrpc_release_call(rx, call);
662248f219cSDavid Howells 		msg->msg_flags |= MSG_EOR;
663248f219cSDavid Howells 		ret = 1;
6648c3e34a4SDavid Howells 	}
6658c3e34a4SDavid Howells 
666248f219cSDavid Howells 	if (ret == 0)
667248f219cSDavid Howells 		msg->msg_flags |= MSG_MORE;
668248f219cSDavid Howells 	else
669248f219cSDavid Howells 		msg->msg_flags &= ~MSG_MORE;
670248f219cSDavid Howells 	ret = copied;
6718c3e34a4SDavid Howells 
672540b1c48SDavid Howells error_unlock_call:
673540b1c48SDavid Howells 	mutex_unlock(&call->user_mutex);
674fff72429SDavid Howells 	rxrpc_put_call(call, rxrpc_call_put);
675540b1c48SDavid Howells 	trace_rxrpc_recvmsg(call, rxrpc_recvmsg_return, 0, 0, 0, ret);
676540b1c48SDavid Howells 	return ret;
677540b1c48SDavid Howells 
678540b1c48SDavid Howells error_requeue_call:
679540b1c48SDavid Howells 	if (!(flags & MSG_PEEK)) {
680540b1c48SDavid Howells 		write_lock_bh(&rx->recvmsg_lock);
681540b1c48SDavid Howells 		list_add(&call->recvmsg_link, &rx->recvmsg_q);
682540b1c48SDavid Howells 		write_unlock_bh(&rx->recvmsg_lock);
683540b1c48SDavid Howells 		trace_rxrpc_recvmsg(call, rxrpc_recvmsg_requeue, 0, 0, 0, 0);
684540b1c48SDavid Howells 	} else {
685540b1c48SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put);
686540b1c48SDavid Howells 	}
687248f219cSDavid Howells error_no_call:
688248f219cSDavid Howells 	release_sock(&rx->sk);
6896dce3c20SEric Dumazet error_trace:
69084997905SDavid Howells 	trace_rxrpc_recvmsg(call, rxrpc_recvmsg_return, 0, 0, 0, ret);
6918c3e34a4SDavid Howells 	return ret;
6928c3e34a4SDavid Howells 
6938c3e34a4SDavid Howells wait_interrupted:
6948c3e34a4SDavid Howells 	ret = sock_intr_errno(timeo);
6958c3e34a4SDavid Howells wait_error:
6968c3e34a4SDavid Howells 	finish_wait(sk_sleep(&rx->sk), &wait);
69784997905SDavid Howells 	call = NULL;
6986dce3c20SEric Dumazet 	goto error_trace;
699d001648eSDavid Howells }
7008c3e34a4SDavid Howells 
7018c3e34a4SDavid Howells /**
702d001648eSDavid Howells  * rxrpc_kernel_recv_data - Allow a kernel service to receive data/info
703d001648eSDavid Howells  * @sock: The socket that the call exists on
704d001648eSDavid Howells  * @call: The call to send data through
705eb9950ebSDavid Howells  * @iter: The buffer to receive into
706d001648eSDavid Howells  * @want_more: True if more data is expected to be read
707d001648eSDavid Howells  * @_abort: Where the abort code is stored if -ECONNABORTED is returned
708a68f4a27SDavid Howells  * @_service: Where to store the actual service ID (may be upgraded)
7098c3e34a4SDavid Howells  *
710d001648eSDavid Howells  * Allow a kernel service to receive data and pick up information about the
711d001648eSDavid Howells  * state of a call.  Returns 0 if got what was asked for and there's more
712d001648eSDavid Howells  * available, 1 if we got what was asked for and we're at the end of the data
713d001648eSDavid Howells  * and -EAGAIN if we need more data.
714d001648eSDavid Howells  *
715d001648eSDavid Howells  * Note that we may return -EAGAIN to drain empty packets at the end of the
716d001648eSDavid Howells  * data, even if we've already copied over the requested data.
717d001648eSDavid Howells  *
718d001648eSDavid Howells  * *_abort should also be initialised to 0.
7198c3e34a4SDavid Howells  */
720d001648eSDavid Howells int rxrpc_kernel_recv_data(struct socket *sock, struct rxrpc_call *call,
721eb9950ebSDavid Howells 			   struct iov_iter *iter,
722a68f4a27SDavid Howells 			   bool want_more, u32 *_abort, u16 *_service)
7238c3e34a4SDavid Howells {
724eb9950ebSDavid Howells 	size_t offset = 0;
725d001648eSDavid Howells 	int ret;
7268c3e34a4SDavid Howells 
727eb9950ebSDavid Howells 	_enter("{%d,%s},%zu,%d",
728248f219cSDavid Howells 	       call->debug_id, rxrpc_call_states[call->state],
729eb9950ebSDavid Howells 	       iov_iter_count(iter), want_more);
730d001648eSDavid Howells 
731d001648eSDavid Howells 	ASSERTCMP(call->state, !=, RXRPC_CALL_SERVER_ACCEPTING);
732d001648eSDavid Howells 
733540b1c48SDavid Howells 	mutex_lock(&call->user_mutex);
734d001648eSDavid Howells 
735146d8fefSDavid Howells 	switch (READ_ONCE(call->state)) {
736d001648eSDavid Howells 	case RXRPC_CALL_CLIENT_RECV_REPLY:
737d001648eSDavid Howells 	case RXRPC_CALL_SERVER_RECV_REQUEST:
738d001648eSDavid Howells 	case RXRPC_CALL_SERVER_ACK_REQUEST:
739eb9950ebSDavid Howells 		ret = rxrpc_recvmsg_data(sock, call, NULL, iter,
740eb9950ebSDavid Howells 					 iov_iter_count(iter), 0,
741eb9950ebSDavid Howells 					 &offset);
742d001648eSDavid Howells 		if (ret < 0)
743d001648eSDavid Howells 			goto out;
744d001648eSDavid Howells 
745d001648eSDavid Howells 		/* We can only reach here with a partially full buffer if we
746d001648eSDavid Howells 		 * have reached the end of the data.  We must otherwise have a
747d001648eSDavid Howells 		 * full buffer or have been given -EAGAIN.
748d001648eSDavid Howells 		 */
749d001648eSDavid Howells 		if (ret == 1) {
750eb9950ebSDavid Howells 			if (iov_iter_count(iter) > 0)
751d001648eSDavid Howells 				goto short_data;
752d001648eSDavid Howells 			if (!want_more)
753d001648eSDavid Howells 				goto read_phase_complete;
754d001648eSDavid Howells 			ret = 0;
755d001648eSDavid Howells 			goto out;
7568c3e34a4SDavid Howells 		}
7578c3e34a4SDavid Howells 
758d001648eSDavid Howells 		if (!want_more)
759d001648eSDavid Howells 			goto excess_data;
760d001648eSDavid Howells 		goto out;
761d001648eSDavid Howells 
762d001648eSDavid Howells 	case RXRPC_CALL_COMPLETE:
763d001648eSDavid Howells 		goto call_complete;
764d001648eSDavid Howells 
765d001648eSDavid Howells 	default:
766d001648eSDavid Howells 		ret = -EINPROGRESS;
767d001648eSDavid Howells 		goto out;
768d001648eSDavid Howells 	}
769d001648eSDavid Howells 
770d001648eSDavid Howells read_phase_complete:
771d001648eSDavid Howells 	ret = 1;
772d001648eSDavid Howells out:
773d0b35a42SDavid Howells 	switch (call->ackr_reason) {
774d0b35a42SDavid Howells 	case RXRPC_ACK_IDLE:
775d0b35a42SDavid Howells 		break;
776d0b35a42SDavid Howells 	case RXRPC_ACK_DELAY:
777d0b35a42SDavid Howells 		if (ret != -EAGAIN)
778d0b35a42SDavid Howells 			break;
779d0b35a42SDavid Howells 		/* Fall through */
780d0b35a42SDavid Howells 	default:
781d0b35a42SDavid Howells 		rxrpc_send_ack_packet(call, false, NULL);
782d0b35a42SDavid Howells 	}
783d0b35a42SDavid Howells 
784a68f4a27SDavid Howells 	if (_service)
785a68f4a27SDavid Howells 		*_service = call->service_id;
786540b1c48SDavid Howells 	mutex_unlock(&call->user_mutex);
787eb9950ebSDavid Howells 	_leave(" = %d [%zu,%d]", ret, iov_iter_count(iter), *_abort);
788d001648eSDavid Howells 	return ret;
789d001648eSDavid Howells 
790d001648eSDavid Howells short_data:
791fb46f6eeSDavid Howells 	trace_rxrpc_rx_eproto(call, 0, tracepoint_string("short_data"));
792d001648eSDavid Howells 	ret = -EBADMSG;
793d001648eSDavid Howells 	goto out;
794d001648eSDavid Howells excess_data:
795fb46f6eeSDavid Howells 	trace_rxrpc_rx_eproto(call, 0, tracepoint_string("excess_data"));
796d001648eSDavid Howells 	ret = -EMSGSIZE;
797d001648eSDavid Howells 	goto out;
798d001648eSDavid Howells call_complete:
799d001648eSDavid Howells 	*_abort = call->abort_code;
8003a92789aSDavid Howells 	ret = call->error;
801d001648eSDavid Howells 	if (call->completion == RXRPC_CALL_SUCCEEDED) {
802d001648eSDavid Howells 		ret = 1;
803eb9950ebSDavid Howells 		if (iov_iter_count(iter) > 0)
804d001648eSDavid Howells 			ret = -ECONNRESET;
805d001648eSDavid Howells 	}
806d001648eSDavid Howells 	goto out;
807d001648eSDavid Howells }
808d001648eSDavid Howells EXPORT_SYMBOL(rxrpc_kernel_recv_data);
8092070a3e4SDavid Howells 
8102070a3e4SDavid Howells /**
8112070a3e4SDavid Howells  * rxrpc_kernel_get_reply_time - Get timestamp on first reply packet
8122070a3e4SDavid Howells  * @sock: The socket that the call exists on
8132070a3e4SDavid Howells  * @call: The call to query
8142070a3e4SDavid Howells  * @_ts: Where to put the timestamp
8152070a3e4SDavid Howells  *
8162070a3e4SDavid Howells  * Retrieve the timestamp from the first DATA packet of the reply if it is
8172070a3e4SDavid Howells  * in the ring.  Returns true if successful, false if not.
8182070a3e4SDavid Howells  */
8192070a3e4SDavid Howells bool rxrpc_kernel_get_reply_time(struct socket *sock, struct rxrpc_call *call,
8202070a3e4SDavid Howells 				 ktime_t *_ts)
8212070a3e4SDavid Howells {
8222070a3e4SDavid Howells 	struct sk_buff *skb;
8232070a3e4SDavid Howells 	rxrpc_seq_t hard_ack, top, seq;
8242070a3e4SDavid Howells 	bool success = false;
8252070a3e4SDavid Howells 
8262070a3e4SDavid Howells 	mutex_lock(&call->user_mutex);
8272070a3e4SDavid Howells 
8282070a3e4SDavid Howells 	if (READ_ONCE(call->state) != RXRPC_CALL_CLIENT_RECV_REPLY)
8292070a3e4SDavid Howells 		goto out;
8302070a3e4SDavid Howells 
8312070a3e4SDavid Howells 	hard_ack = call->rx_hard_ack;
8322070a3e4SDavid Howells 	if (hard_ack != 0)
8332070a3e4SDavid Howells 		goto out;
8342070a3e4SDavid Howells 
8352070a3e4SDavid Howells 	seq = hard_ack + 1;
8362070a3e4SDavid Howells 	top = smp_load_acquire(&call->rx_top);
8372070a3e4SDavid Howells 	if (after(seq, top))
8382070a3e4SDavid Howells 		goto out;
8392070a3e4SDavid Howells 
8402070a3e4SDavid Howells 	skb = call->rxtx_buffer[seq & RXRPC_RXTX_BUFF_MASK];
8412070a3e4SDavid Howells 	if (!skb)
8422070a3e4SDavid Howells 		goto out;
8432070a3e4SDavid Howells 
8442070a3e4SDavid Howells 	*_ts = skb_get_ktime(skb);
8452070a3e4SDavid Howells 	success = true;
8462070a3e4SDavid Howells 
8472070a3e4SDavid Howells out:
8482070a3e4SDavid Howells 	mutex_unlock(&call->user_mutex);
8492070a3e4SDavid Howells 	return success;
8502070a3e4SDavid Howells }
8512070a3e4SDavid Howells EXPORT_SYMBOL(rxrpc_kernel_get_reply_time);
852