xref: /openbmc/linux/net/rxrpc/recvmsg.c (revision 57af281e)
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 /*
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;
72a319aeddSZheng Yongjun 		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) {
903dd9c8b5SDavid Howells 		write_lock(&call->state_lock);
913067bf8cSDavid Howells 		ret = __rxrpc_set_call_completion(call, compl, abort_code, error);
923dd9c8b5SDavid Howells 		write_unlock(&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) {
1103dd9c8b5SDavid Howells 		write_lock(&call->state_lock);
1113067bf8cSDavid Howells 		ret = __rxrpc_call_completed(call);
1123dd9c8b5SDavid Howells 		write_unlock(&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  */
120*57af281eSDavid Howells bool __rxrpc_abort_call(struct rxrpc_call *call, rxrpc_seq_t seq,
121*57af281eSDavid Howells 			u32 abort_code, int error, enum rxrpc_abort_reason why)
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 
129*57af281eSDavid Howells bool rxrpc_abort_call(struct rxrpc_call *call, rxrpc_seq_t seq,
130*57af281eSDavid Howells 		      u32 abort_code, int error, enum rxrpc_abort_reason why)
1313067bf8cSDavid Howells {
1323067bf8cSDavid Howells 	bool ret;
1333067bf8cSDavid Howells 
1343dd9c8b5SDavid Howells 	write_lock(&call->state_lock);
135*57af281eSDavid Howells 	ret = __rxrpc_abort_call(call, seq, abort_code, error, why);
1363dd9c8b5SDavid Howells 	write_unlock(&call->state_lock);
137a343b174SDavid Howells 	if (ret && test_bit(RXRPC_CALL_EXPOSED, &call->flags))
138a343b174SDavid Howells 		rxrpc_send_abort_packet(call);
1393067bf8cSDavid Howells 	return ret;
1403067bf8cSDavid Howells }
1413067bf8cSDavid Howells 
1423067bf8cSDavid Howells /*
143248f219cSDavid Howells  * Pass a call terminating message to userspace.
144248f219cSDavid Howells  */
145248f219cSDavid Howells static int rxrpc_recvmsg_term(struct rxrpc_call *call, struct msghdr *msg)
146248f219cSDavid Howells {
147248f219cSDavid Howells 	u32 tmp = 0;
148248f219cSDavid Howells 	int ret;
149248f219cSDavid Howells 
150248f219cSDavid Howells 	switch (call->completion) {
151248f219cSDavid Howells 	case RXRPC_CALL_SUCCEEDED:
152248f219cSDavid Howells 		ret = 0;
153248f219cSDavid Howells 		if (rxrpc_is_service_call(call))
154248f219cSDavid Howells 			ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ACK, 0, &tmp);
155248f219cSDavid Howells 		break;
156248f219cSDavid Howells 	case RXRPC_CALL_REMOTELY_ABORTED:
157248f219cSDavid Howells 		tmp = call->abort_code;
158248f219cSDavid Howells 		ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &tmp);
159248f219cSDavid Howells 		break;
160248f219cSDavid Howells 	case RXRPC_CALL_LOCALLY_ABORTED:
161248f219cSDavid Howells 		tmp = call->abort_code;
162248f219cSDavid Howells 		ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &tmp);
163248f219cSDavid Howells 		break;
164248f219cSDavid Howells 	case RXRPC_CALL_NETWORK_ERROR:
1653a92789aSDavid Howells 		tmp = -call->error;
166248f219cSDavid Howells 		ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NET_ERROR, 4, &tmp);
167248f219cSDavid Howells 		break;
168248f219cSDavid Howells 	case RXRPC_CALL_LOCAL_ERROR:
1693a92789aSDavid Howells 		tmp = -call->error;
170248f219cSDavid Howells 		ret = put_cmsg(msg, SOL_RXRPC, RXRPC_LOCAL_ERROR, 4, &tmp);
171248f219cSDavid Howells 		break;
172248f219cSDavid Howells 	default:
173248f219cSDavid Howells 		pr_err("Invalid terminal call state %u\n", call->state);
174248f219cSDavid Howells 		BUG();
175248f219cSDavid Howells 		break;
176248f219cSDavid Howells 	}
177248f219cSDavid Howells 
1785d7edbc9SDavid Howells 	trace_rxrpc_recvdata(call, rxrpc_recvmsg_terminal,
1795d7edbc9SDavid Howells 			     lower_32_bits(atomic64_read(&call->ackr_window)) - 1,
18084997905SDavid Howells 			     call->rx_pkt_offset, call->rx_pkt_len, ret);
181248f219cSDavid Howells 	return ret;
182248f219cSDavid Howells }
183248f219cSDavid Howells 
184248f219cSDavid Howells /*
185248f219cSDavid Howells  * End the packet reception phase.
186248f219cSDavid Howells  */
187b69d94d7SDavid Howells static void rxrpc_end_rx_phase(struct rxrpc_call *call, rxrpc_serial_t serial)
188248f219cSDavid Howells {
1895d7edbc9SDavid Howells 	rxrpc_seq_t whigh = READ_ONCE(call->rx_highest_seq);
1905d7edbc9SDavid Howells 
191248f219cSDavid Howells 	_enter("%d,%s", call->debug_id, rxrpc_call_states[call->state]);
192248f219cSDavid Howells 
1935d7edbc9SDavid Howells 	trace_rxrpc_receive(call, rxrpc_receive_end, 0, whigh);
194816c9fceSDavid Howells 
195530403d9SDavid Howells 	if (call->state == RXRPC_CALL_CLIENT_RECV_REPLY)
196530403d9SDavid Howells 		rxrpc_propose_delay_ACK(call, serial, rxrpc_propose_ack_terminal_ack);
197248f219cSDavid Howells 
1983dd9c8b5SDavid Howells 	write_lock(&call->state_lock);
199248f219cSDavid Howells 
200248f219cSDavid Howells 	switch (call->state) {
201248f219cSDavid Howells 	case RXRPC_CALL_CLIENT_RECV_REPLY:
202248f219cSDavid Howells 		__rxrpc_call_completed(call);
2033dd9c8b5SDavid Howells 		write_unlock(&call->state_lock);
20403fc55adSDavid Howells 		rxrpc_poke_call(call, rxrpc_call_poke_complete);
205248f219cSDavid Howells 		break;
206248f219cSDavid Howells 
207248f219cSDavid Howells 	case RXRPC_CALL_SERVER_RECV_REQUEST:
208248f219cSDavid Howells 		call->state = RXRPC_CALL_SERVER_ACK_REQUEST;
209a158bdd3SDavid Howells 		call->expect_req_by = jiffies + MAX_JIFFY_OFFSET;
2103dd9c8b5SDavid Howells 		write_unlock(&call->state_lock);
211530403d9SDavid Howells 		rxrpc_propose_delay_ACK(call, serial,
2129749fd2bSDavid Howells 					rxrpc_propose_ack_processing_op);
213248f219cSDavid Howells 		break;
214248f219cSDavid Howells 	default:
2153dd9c8b5SDavid Howells 		write_unlock(&call->state_lock);
216248f219cSDavid Howells 		break;
217248f219cSDavid Howells 	}
218248f219cSDavid Howells }
219248f219cSDavid Howells 
220248f219cSDavid Howells /*
221248f219cSDavid Howells  * Discard a packet we've used up and advance the Rx window by one.
222248f219cSDavid Howells  */
223248f219cSDavid Howells static void rxrpc_rotate_rx_window(struct rxrpc_call *call)
224248f219cSDavid Howells {
225816c9fceSDavid Howells 	struct rxrpc_skb_priv *sp;
226248f219cSDavid Howells 	struct sk_buff *skb;
22758dc63c9SDavid Howells 	rxrpc_serial_t serial;
2285d7edbc9SDavid Howells 	rxrpc_seq_t old_consumed = call->rx_consumed, tseq;
2295d7edbc9SDavid Howells 	bool last;
2305d7edbc9SDavid Howells 	int acked;
231248f219cSDavid Howells 
232248f219cSDavid Howells 	_enter("%d", call->debug_id);
233248f219cSDavid Howells 
2345d7edbc9SDavid Howells 	skb = skb_dequeue(&call->recvmsg_queue);
2359a36a6bcSDavid Howells 	rxrpc_see_skb(skb, rxrpc_skb_see_rotate);
2365d7edbc9SDavid Howells 
237816c9fceSDavid Howells 	sp = rxrpc_skb(skb);
2385d7edbc9SDavid Howells 	tseq   = sp->hdr.seq;
239d4d02d8bSDavid Howells 	serial = sp->hdr.serial;
2405d7edbc9SDavid Howells 	last   = sp->hdr.flags & RXRPC_LAST_PACKET;
241e2de6c40SDavid Howells 
242248f219cSDavid Howells 	/* Barrier against rxrpc_input_data(). */
2435d7edbc9SDavid Howells 	if (after(tseq, call->rx_consumed))
2445d7edbc9SDavid Howells 		smp_store_release(&call->rx_consumed, tseq);
245248f219cSDavid Howells 
2469a36a6bcSDavid Howells 	rxrpc_free_skb(skb, rxrpc_skb_put_rotate);
247248f219cSDavid Howells 
2485d7edbc9SDavid Howells 	trace_rxrpc_receive(call, last ? rxrpc_receive_rotate_last : rxrpc_receive_rotate,
2495d7edbc9SDavid Howells 			    serial, call->rx_consumed);
250e2de6c40SDavid Howells 	if (last) {
251b69d94d7SDavid Howells 		rxrpc_end_rx_phase(call, serial);
2525d7edbc9SDavid Howells 		return;
2535d7edbc9SDavid Howells 	}
2545d7edbc9SDavid Howells 
255805b21b9SDavid Howells 	/* Check to see if there's an ACK that needs sending. */
2565d7edbc9SDavid Howells 	acked = atomic_add_return(call->rx_consumed - old_consumed,
2575d7edbc9SDavid Howells 				  &call->ackr_nr_consumed);
2585d7edbc9SDavid Howells 	if (acked > 2 &&
2595e6ef4f1SDavid Howells 	    !test_and_set_bit(RXRPC_CALL_RX_IS_IDLE, &call->flags))
2605e6ef4f1SDavid Howells 		rxrpc_poke_call(call, rxrpc_call_poke_idle);
261805b21b9SDavid Howells }
262248f219cSDavid Howells 
263248f219cSDavid Howells /*
264d4d02d8bSDavid Howells  * Decrypt and verify a DATA packet.
265248f219cSDavid Howells  */
266d4d02d8bSDavid Howells static int rxrpc_verify_data(struct rxrpc_call *call, struct sk_buff *skb)
267248f219cSDavid Howells {
268248f219cSDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
269248f219cSDavid Howells 
270d4d02d8bSDavid Howells 	if (sp->flags & RXRPC_RX_VERIFIED)
271248f219cSDavid Howells 		return 0;
272d4d02d8bSDavid Howells 	return call->security->verify_packet(call, skb);
273248f219cSDavid Howells }
274248f219cSDavid Howells 
275248f219cSDavid Howells /*
276248f219cSDavid Howells  * Deliver messages to a call.  This keeps processing packets until the buffer
277248f219cSDavid Howells  * is filled and we find either more DATA (returns 0) or the end of the DATA
278248f219cSDavid Howells  * (returns 1).  If more packets are required, it returns -EAGAIN.
279248f219cSDavid Howells  */
280248f219cSDavid Howells static int rxrpc_recvmsg_data(struct socket *sock, struct rxrpc_call *call,
281248f219cSDavid Howells 			      struct msghdr *msg, struct iov_iter *iter,
282248f219cSDavid Howells 			      size_t len, int flags, size_t *_offset)
283248f219cSDavid Howells {
284248f219cSDavid Howells 	struct rxrpc_skb_priv *sp;
285248f219cSDavid Howells 	struct sk_buff *skb;
2865d7edbc9SDavid Howells 	rxrpc_seq_t seq = 0;
287248f219cSDavid Howells 	size_t remain;
288248f219cSDavid Howells 	unsigned int rx_pkt_offset, rx_pkt_len;
2895d7edbc9SDavid Howells 	int copy, ret = -EAGAIN, ret2;
290248f219cSDavid Howells 
291248f219cSDavid Howells 	rx_pkt_offset = call->rx_pkt_offset;
292248f219cSDavid Howells 	rx_pkt_len = call->rx_pkt_len;
293248f219cSDavid Howells 
294816c9fceSDavid Howells 	if (call->state >= RXRPC_CALL_SERVER_ACK_REQUEST) {
2955d7edbc9SDavid Howells 		seq = lower_32_bits(atomic64_read(&call->ackr_window)) - 1;
296816c9fceSDavid Howells 		ret = 1;
297816c9fceSDavid Howells 		goto done;
298816c9fceSDavid Howells 	}
299816c9fceSDavid Howells 
3005d7edbc9SDavid Howells 	/* No one else can be removing stuff from the queue, so we shouldn't
3015d7edbc9SDavid Howells 	 * need the Rx lock to walk it.
3025d7edbc9SDavid Howells 	 */
3035d7edbc9SDavid Howells 	skb = skb_peek(&call->recvmsg_queue);
3045d7edbc9SDavid Howells 	while (skb) {
3059a36a6bcSDavid Howells 		rxrpc_see_skb(skb, rxrpc_skb_see_recvmsg);
306248f219cSDavid Howells 		sp = rxrpc_skb(skb);
3075d7edbc9SDavid Howells 		seq = sp->hdr.seq;
308248f219cSDavid Howells 
3095d7edbc9SDavid Howells 		if (!(flags & MSG_PEEK))
3105d7edbc9SDavid Howells 			trace_rxrpc_receive(call, rxrpc_receive_front,
3115d7edbc9SDavid Howells 					    sp->hdr.serial, seq);
3125d7edbc9SDavid Howells 
313248f219cSDavid Howells 		if (msg)
314248f219cSDavid Howells 			sock_recv_timestamp(msg, sock->sk, skb);
315248f219cSDavid Howells 
3162e2ea51dSDavid Howells 		if (rx_pkt_offset == 0) {
317d4d02d8bSDavid Howells 			ret2 = rxrpc_verify_data(call, skb);
318d4d02d8bSDavid Howells 			rx_pkt_offset = sp->offset;
319d4d02d8bSDavid Howells 			rx_pkt_len = sp->len;
320faf92e8dSDavid Howells 			trace_rxrpc_recvdata(call, rxrpc_recvmsg_next, seq,
32184997905SDavid Howells 					     rx_pkt_offset, rx_pkt_len, ret2);
322816c9fceSDavid Howells 			if (ret2 < 0) {
323816c9fceSDavid Howells 				ret = ret2;
3242e2ea51dSDavid Howells 				goto out;
3252e2ea51dSDavid Howells 			}
32684997905SDavid Howells 		} else {
327faf92e8dSDavid Howells 			trace_rxrpc_recvdata(call, rxrpc_recvmsg_cont, seq,
32884997905SDavid Howells 					     rx_pkt_offset, rx_pkt_len, 0);
329816c9fceSDavid Howells 		}
330248f219cSDavid Howells 
331248f219cSDavid Howells 		/* We have to handle short, empty and used-up DATA packets. */
332248f219cSDavid Howells 		remain = len - *_offset;
333248f219cSDavid Howells 		copy = rx_pkt_len;
334248f219cSDavid Howells 		if (copy > remain)
335248f219cSDavid Howells 			copy = remain;
336248f219cSDavid Howells 		if (copy > 0) {
337816c9fceSDavid Howells 			ret2 = skb_copy_datagram_iter(skb, rx_pkt_offset, iter,
338248f219cSDavid Howells 						      copy);
339816c9fceSDavid Howells 			if (ret2 < 0) {
340816c9fceSDavid Howells 				ret = ret2;
341248f219cSDavid Howells 				goto out;
342816c9fceSDavid Howells 			}
343248f219cSDavid Howells 
344248f219cSDavid Howells 			/* handle piecemeal consumption of data packets */
345248f219cSDavid Howells 			rx_pkt_offset += copy;
346248f219cSDavid Howells 			rx_pkt_len -= copy;
347248f219cSDavid Howells 			*_offset += copy;
348248f219cSDavid Howells 		}
349248f219cSDavid Howells 
350248f219cSDavid Howells 		if (rx_pkt_len > 0) {
351faf92e8dSDavid Howells 			trace_rxrpc_recvdata(call, rxrpc_recvmsg_full, seq,
35284997905SDavid Howells 					     rx_pkt_offset, rx_pkt_len, 0);
353248f219cSDavid Howells 			ASSERTCMP(*_offset, ==, len);
354816c9fceSDavid Howells 			ret = 0;
355248f219cSDavid Howells 			break;
356248f219cSDavid Howells 		}
357248f219cSDavid Howells 
358248f219cSDavid Howells 		/* The whole packet has been transferred. */
359d4d02d8bSDavid Howells 		if (sp->hdr.flags & RXRPC_LAST_PACKET)
360d4d02d8bSDavid Howells 			ret = 1;
361248f219cSDavid Howells 		rx_pkt_offset = 0;
362248f219cSDavid Howells 		rx_pkt_len = 0;
363248f219cSDavid Howells 
3645d7edbc9SDavid Howells 		skb = skb_peek_next(skb, &call->recvmsg_queue);
3655d7edbc9SDavid Howells 
366d4d02d8bSDavid Howells 		if (!(flags & MSG_PEEK))
367d4d02d8bSDavid Howells 			rxrpc_rotate_rx_window(call);
368248f219cSDavid Howells 	}
369248f219cSDavid Howells 
370248f219cSDavid Howells out:
371248f219cSDavid Howells 	if (!(flags & MSG_PEEK)) {
372248f219cSDavid Howells 		call->rx_pkt_offset = rx_pkt_offset;
373248f219cSDavid Howells 		call->rx_pkt_len = rx_pkt_len;
374248f219cSDavid Howells 	}
375816c9fceSDavid Howells done:
376faf92e8dSDavid Howells 	trace_rxrpc_recvdata(call, rxrpc_recvmsg_data_return, seq,
37784997905SDavid Howells 			     rx_pkt_offset, rx_pkt_len, ret);
378d0b35a42SDavid Howells 	if (ret == -EAGAIN)
3795e6ef4f1SDavid Howells 		set_bit(RXRPC_CALL_RX_IS_IDLE, &call->flags);
380248f219cSDavid Howells 	return ret;
381248f219cSDavid Howells }
382248f219cSDavid Howells 
383248f219cSDavid Howells /*
384248f219cSDavid Howells  * Receive a message from an RxRPC socket
3858c3e34a4SDavid Howells  * - we need to be careful about two or more threads calling recvmsg
3868c3e34a4SDavid Howells  *   simultaneously
3878c3e34a4SDavid Howells  */
3888c3e34a4SDavid Howells int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
3898c3e34a4SDavid Howells 		  int flags)
3908c3e34a4SDavid Howells {
391248f219cSDavid Howells 	struct rxrpc_call *call;
3928c3e34a4SDavid Howells 	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
393248f219cSDavid Howells 	struct list_head *l;
3940e50d999SDavid Howells 	unsigned int call_debug_id = 0;
395248f219cSDavid Howells 	size_t copied = 0;
3968c3e34a4SDavid Howells 	long timeo;
397248f219cSDavid Howells 	int ret;
3988c3e34a4SDavid Howells 
3998c3e34a4SDavid Howells 	DEFINE_WAIT(wait);
4008c3e34a4SDavid Howells 
4010e50d999SDavid Howells 	trace_rxrpc_recvmsg(0, rxrpc_recvmsg_enter, 0);
4028c3e34a4SDavid Howells 
4038c3e34a4SDavid Howells 	if (flags & (MSG_OOB | MSG_TRUNC))
4048c3e34a4SDavid Howells 		return -EOPNOTSUPP;
4058c3e34a4SDavid Howells 
4068c3e34a4SDavid Howells 	timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT);
4078c3e34a4SDavid Howells 
408248f219cSDavid Howells try_again:
4098c3e34a4SDavid Howells 	lock_sock(&rx->sk);
4108c3e34a4SDavid Howells 
411248f219cSDavid Howells 	/* Return immediately if a client socket has no outstanding calls */
412248f219cSDavid Howells 	if (RB_EMPTY_ROOT(&rx->calls) &&
413248f219cSDavid Howells 	    list_empty(&rx->recvmsg_q) &&
414248f219cSDavid Howells 	    rx->sk.sk_state != RXRPC_SERVER_LISTENING) {
4158c3e34a4SDavid Howells 		release_sock(&rx->sk);
416639f181fSDavid Howells 		return -EAGAIN;
4178c3e34a4SDavid Howells 	}
4188c3e34a4SDavid Howells 
419248f219cSDavid Howells 	if (list_empty(&rx->recvmsg_q)) {
420248f219cSDavid Howells 		ret = -EWOULDBLOCK;
42184997905SDavid Howells 		if (timeo == 0) {
42284997905SDavid Howells 			call = NULL;
423248f219cSDavid Howells 			goto error_no_call;
42484997905SDavid Howells 		}
4258c3e34a4SDavid Howells 
4268c3e34a4SDavid Howells 		release_sock(&rx->sk);
427248f219cSDavid Howells 
428248f219cSDavid Howells 		/* Wait for something to happen */
4298c3e34a4SDavid Howells 		prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait,
4308c3e34a4SDavid Howells 					  TASK_INTERRUPTIBLE);
4318c3e34a4SDavid Howells 		ret = sock_error(&rx->sk);
4328c3e34a4SDavid Howells 		if (ret)
4338c3e34a4SDavid Howells 			goto wait_error;
4348c3e34a4SDavid Howells 
435248f219cSDavid Howells 		if (list_empty(&rx->recvmsg_q)) {
4368c3e34a4SDavid Howells 			if (signal_pending(current))
4378c3e34a4SDavid Howells 				goto wait_interrupted;
4380e50d999SDavid Howells 			trace_rxrpc_recvmsg(0, rxrpc_recvmsg_wait, 0);
4398c3e34a4SDavid Howells 			timeo = schedule_timeout(timeo);
4408c3e34a4SDavid Howells 		}
4418c3e34a4SDavid Howells 		finish_wait(sk_sleep(&rx->sk), &wait);
442248f219cSDavid Howells 		goto try_again;
4438c3e34a4SDavid Howells 	}
4448c3e34a4SDavid Howells 
445248f219cSDavid Howells 	/* Find the next call and dequeue it if we're not just peeking.  If we
446248f219cSDavid Howells 	 * do dequeue it, that comes with a ref that we will need to release.
447248f219cSDavid Howells 	 */
4483dd9c8b5SDavid Howells 	write_lock(&rx->recvmsg_lock);
449248f219cSDavid Howells 	l = rx->recvmsg_q.next;
450248f219cSDavid Howells 	call = list_entry(l, struct rxrpc_call, recvmsg_link);
451248f219cSDavid Howells 	if (!(flags & MSG_PEEK))
452248f219cSDavid Howells 		list_del_init(&call->recvmsg_link);
453248f219cSDavid Howells 	else
454cb0fc0c9SDavid Howells 		rxrpc_get_call(call, rxrpc_call_get_recvmsg);
4553dd9c8b5SDavid Howells 	write_unlock(&rx->recvmsg_lock);
4568c3e34a4SDavid Howells 
4570e50d999SDavid Howells 	call_debug_id = call->debug_id;
4580e50d999SDavid Howells 	trace_rxrpc_recvmsg(call_debug_id, rxrpc_recvmsg_dequeue, 0);
459248f219cSDavid Howells 
460540b1c48SDavid Howells 	/* We're going to drop the socket lock, so we need to lock the call
461540b1c48SDavid Howells 	 * against interference by sendmsg.
462540b1c48SDavid Howells 	 */
463540b1c48SDavid Howells 	if (!mutex_trylock(&call->user_mutex)) {
464540b1c48SDavid Howells 		ret = -EWOULDBLOCK;
465540b1c48SDavid Howells 		if (flags & MSG_DONTWAIT)
466540b1c48SDavid Howells 			goto error_requeue_call;
467540b1c48SDavid Howells 		ret = -ERESTARTSYS;
468540b1c48SDavid Howells 		if (mutex_lock_interruptible(&call->user_mutex) < 0)
469540b1c48SDavid Howells 			goto error_requeue_call;
470540b1c48SDavid Howells 	}
471540b1c48SDavid Howells 
472540b1c48SDavid Howells 	release_sock(&rx->sk);
473540b1c48SDavid Howells 
474248f219cSDavid Howells 	if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
475248f219cSDavid Howells 		BUG();
476248f219cSDavid Howells 
477248f219cSDavid Howells 	if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
478248f219cSDavid Howells 		if (flags & MSG_CMSG_COMPAT) {
479248f219cSDavid Howells 			unsigned int id32 = call->user_call_ID;
480248f219cSDavid Howells 
481248f219cSDavid Howells 			ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
482248f219cSDavid Howells 				       sizeof(unsigned int), &id32);
483248f219cSDavid Howells 		} else {
484a16b8d0cSDavid Howells 			unsigned long idl = call->user_call_ID;
485a16b8d0cSDavid Howells 
486248f219cSDavid Howells 			ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
487a16b8d0cSDavid Howells 				       sizeof(unsigned long), &idl);
488248f219cSDavid Howells 		}
489248f219cSDavid Howells 		if (ret < 0)
490540b1c48SDavid Howells 			goto error_unlock_call;
491248f219cSDavid Howells 	}
492248f219cSDavid Howells 
49365550098SDavid Howells 	if (msg->msg_name && call->peer) {
494f3441d41SDavid Howells 		size_t len = sizeof(call->dest_srx);
49568d6d1aeSDavid Howells 
496f3441d41SDavid Howells 		memcpy(msg->msg_name, &call->dest_srx, len);
4978c3e34a4SDavid Howells 		msg->msg_namelen = len;
4988c3e34a4SDavid Howells 	}
4998c3e34a4SDavid Howells 
500146d8fefSDavid Howells 	switch (READ_ONCE(call->state)) {
501248f219cSDavid Howells 	case RXRPC_CALL_CLIENT_RECV_REPLY:
502248f219cSDavid Howells 	case RXRPC_CALL_SERVER_RECV_REQUEST:
503248f219cSDavid Howells 	case RXRPC_CALL_SERVER_ACK_REQUEST:
504248f219cSDavid Howells 		ret = rxrpc_recvmsg_data(sock, call, msg, &msg->msg_iter, len,
505248f219cSDavid Howells 					 flags, &copied);
506248f219cSDavid Howells 		if (ret == -EAGAIN)
507248f219cSDavid Howells 			ret = 0;
50833b603fdSDavid Howells 
5095d7edbc9SDavid Howells 		if (!skb_queue_empty(&call->recvmsg_queue))
51033b603fdSDavid Howells 			rxrpc_notify_socket(call);
5118c3e34a4SDavid Howells 		break;
5128c3e34a4SDavid Howells 	default:
513248f219cSDavid Howells 		ret = 0;
5148c3e34a4SDavid Howells 		break;
5158c3e34a4SDavid Howells 	}
5168c3e34a4SDavid Howells 
5178c3e34a4SDavid Howells 	if (ret < 0)
518540b1c48SDavid Howells 		goto error_unlock_call;
5198c3e34a4SDavid Howells 
520248f219cSDavid Howells 	if (call->state == RXRPC_CALL_COMPLETE) {
521248f219cSDavid Howells 		ret = rxrpc_recvmsg_term(call, msg);
522248f219cSDavid Howells 		if (ret < 0)
523540b1c48SDavid Howells 			goto error_unlock_call;
524248f219cSDavid Howells 		if (!(flags & MSG_PEEK))
5258d94aa38SDavid Howells 			rxrpc_release_call(rx, call);
526248f219cSDavid Howells 		msg->msg_flags |= MSG_EOR;
527248f219cSDavid Howells 		ret = 1;
5288c3e34a4SDavid Howells 	}
5298c3e34a4SDavid Howells 
530248f219cSDavid Howells 	if (ret == 0)
531248f219cSDavid Howells 		msg->msg_flags |= MSG_MORE;
532248f219cSDavid Howells 	else
533248f219cSDavid Howells 		msg->msg_flags &= ~MSG_MORE;
534248f219cSDavid Howells 	ret = copied;
5358c3e34a4SDavid Howells 
536540b1c48SDavid Howells error_unlock_call:
537540b1c48SDavid Howells 	mutex_unlock(&call->user_mutex);
538cb0fc0c9SDavid Howells 	rxrpc_put_call(call, rxrpc_call_put_recvmsg);
5390e50d999SDavid Howells 	trace_rxrpc_recvmsg(call_debug_id, rxrpc_recvmsg_return, ret);
540540b1c48SDavid Howells 	return ret;
541540b1c48SDavid Howells 
542540b1c48SDavid Howells error_requeue_call:
543540b1c48SDavid Howells 	if (!(flags & MSG_PEEK)) {
5443dd9c8b5SDavid Howells 		write_lock(&rx->recvmsg_lock);
545540b1c48SDavid Howells 		list_add(&call->recvmsg_link, &rx->recvmsg_q);
5463dd9c8b5SDavid Howells 		write_unlock(&rx->recvmsg_lock);
5470e50d999SDavid Howells 		trace_rxrpc_recvmsg(call_debug_id, rxrpc_recvmsg_requeue, 0);
548540b1c48SDavid Howells 	} else {
549cb0fc0c9SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_recvmsg);
550540b1c48SDavid Howells 	}
551248f219cSDavid Howells error_no_call:
552248f219cSDavid Howells 	release_sock(&rx->sk);
5536dce3c20SEric Dumazet error_trace:
5540e50d999SDavid Howells 	trace_rxrpc_recvmsg(call_debug_id, rxrpc_recvmsg_return, ret);
5558c3e34a4SDavid Howells 	return ret;
5568c3e34a4SDavid Howells 
5578c3e34a4SDavid Howells wait_interrupted:
5588c3e34a4SDavid Howells 	ret = sock_intr_errno(timeo);
5598c3e34a4SDavid Howells wait_error:
5608c3e34a4SDavid Howells 	finish_wait(sk_sleep(&rx->sk), &wait);
56184997905SDavid Howells 	call = NULL;
5626dce3c20SEric Dumazet 	goto error_trace;
563d001648eSDavid Howells }
5648c3e34a4SDavid Howells 
5658c3e34a4SDavid Howells /**
566d001648eSDavid Howells  * rxrpc_kernel_recv_data - Allow a kernel service to receive data/info
567d001648eSDavid Howells  * @sock: The socket that the call exists on
568d001648eSDavid Howells  * @call: The call to send data through
569eb9950ebSDavid Howells  * @iter: The buffer to receive into
570f105da1aSDavid Howells  * @_len: The amount of data we want to receive (decreased on return)
571d001648eSDavid Howells  * @want_more: True if more data is expected to be read
572d001648eSDavid Howells  * @_abort: Where the abort code is stored if -ECONNABORTED is returned
573a68f4a27SDavid Howells  * @_service: Where to store the actual service ID (may be upgraded)
5748c3e34a4SDavid Howells  *
575d001648eSDavid Howells  * Allow a kernel service to receive data and pick up information about the
576d001648eSDavid Howells  * state of a call.  Returns 0 if got what was asked for and there's more
577d001648eSDavid Howells  * available, 1 if we got what was asked for and we're at the end of the data
578d001648eSDavid Howells  * and -EAGAIN if we need more data.
579d001648eSDavid Howells  *
580d001648eSDavid Howells  * Note that we may return -EAGAIN to drain empty packets at the end of the
581d001648eSDavid Howells  * data, even if we've already copied over the requested data.
582d001648eSDavid Howells  *
583d001648eSDavid Howells  * *_abort should also be initialised to 0.
5848c3e34a4SDavid Howells  */
585d001648eSDavid Howells int rxrpc_kernel_recv_data(struct socket *sock, struct rxrpc_call *call,
586f105da1aSDavid Howells 			   struct iov_iter *iter, size_t *_len,
587a68f4a27SDavid Howells 			   bool want_more, u32 *_abort, u16 *_service)
5888c3e34a4SDavid Howells {
589eb9950ebSDavid Howells 	size_t offset = 0;
590d001648eSDavid Howells 	int ret;
5918c3e34a4SDavid Howells 
592eb9950ebSDavid Howells 	_enter("{%d,%s},%zu,%d",
593248f219cSDavid Howells 	       call->debug_id, rxrpc_call_states[call->state],
594f105da1aSDavid Howells 	       *_len, want_more);
595d001648eSDavid Howells 
5962d914c1bSDavid Howells 	ASSERTCMP(call->state, !=, RXRPC_CALL_SERVER_SECURING);
597d001648eSDavid Howells 
598540b1c48SDavid Howells 	mutex_lock(&call->user_mutex);
599d001648eSDavid Howells 
600146d8fefSDavid Howells 	switch (READ_ONCE(call->state)) {
601d001648eSDavid Howells 	case RXRPC_CALL_CLIENT_RECV_REPLY:
602d001648eSDavid Howells 	case RXRPC_CALL_SERVER_RECV_REQUEST:
603d001648eSDavid Howells 	case RXRPC_CALL_SERVER_ACK_REQUEST:
604eb9950ebSDavid Howells 		ret = rxrpc_recvmsg_data(sock, call, NULL, iter,
605f105da1aSDavid Howells 					 *_len, 0, &offset);
606f105da1aSDavid Howells 		*_len -= offset;
607d001648eSDavid Howells 		if (ret < 0)
608d001648eSDavid Howells 			goto out;
609d001648eSDavid Howells 
610d001648eSDavid Howells 		/* We can only reach here with a partially full buffer if we
611d001648eSDavid Howells 		 * have reached the end of the data.  We must otherwise have a
612d001648eSDavid Howells 		 * full buffer or have been given -EAGAIN.
613d001648eSDavid Howells 		 */
614d001648eSDavid Howells 		if (ret == 1) {
615eb9950ebSDavid Howells 			if (iov_iter_count(iter) > 0)
616d001648eSDavid Howells 				goto short_data;
617d001648eSDavid Howells 			if (!want_more)
618d001648eSDavid Howells 				goto read_phase_complete;
619d001648eSDavid Howells 			ret = 0;
620d001648eSDavid Howells 			goto out;
6218c3e34a4SDavid Howells 		}
6228c3e34a4SDavid Howells 
623d001648eSDavid Howells 		if (!want_more)
624d001648eSDavid Howells 			goto excess_data;
625d001648eSDavid Howells 		goto out;
626d001648eSDavid Howells 
627d001648eSDavid Howells 	case RXRPC_CALL_COMPLETE:
628d001648eSDavid Howells 		goto call_complete;
629d001648eSDavid Howells 
630d001648eSDavid Howells 	default:
631d001648eSDavid Howells 		ret = -EINPROGRESS;
632d001648eSDavid Howells 		goto out;
633d001648eSDavid Howells 	}
634d001648eSDavid Howells 
635d001648eSDavid Howells read_phase_complete:
636d001648eSDavid Howells 	ret = 1;
637d001648eSDavid Howells out:
638a68f4a27SDavid Howells 	if (_service)
639f3441d41SDavid Howells 		*_service = call->dest_srx.srx_service;
640540b1c48SDavid Howells 	mutex_unlock(&call->user_mutex);
641eb9950ebSDavid Howells 	_leave(" = %d [%zu,%d]", ret, iov_iter_count(iter), *_abort);
642d001648eSDavid Howells 	return ret;
643d001648eSDavid Howells 
644d001648eSDavid Howells short_data:
645*57af281eSDavid Howells 	trace_rxrpc_abort(call->debug_id, rxrpc_recvmsg_short_data,
646*57af281eSDavid Howells 			  call->cid, call->call_id, call->rx_consumed,
647*57af281eSDavid Howells 			  0, -EBADMSG);
648d001648eSDavid Howells 	ret = -EBADMSG;
649d001648eSDavid Howells 	goto out;
650d001648eSDavid Howells excess_data:
651*57af281eSDavid Howells 	trace_rxrpc_abort(call->debug_id, rxrpc_recvmsg_excess_data,
652*57af281eSDavid Howells 			  call->cid, call->call_id, call->rx_consumed,
653*57af281eSDavid Howells 			  0, -EMSGSIZE);
654d001648eSDavid Howells 	ret = -EMSGSIZE;
655d001648eSDavid Howells 	goto out;
656d001648eSDavid Howells call_complete:
657d001648eSDavid Howells 	*_abort = call->abort_code;
6583a92789aSDavid Howells 	ret = call->error;
659d001648eSDavid Howells 	if (call->completion == RXRPC_CALL_SUCCEEDED) {
660d001648eSDavid Howells 		ret = 1;
661eb9950ebSDavid Howells 		if (iov_iter_count(iter) > 0)
662d001648eSDavid Howells 			ret = -ECONNRESET;
663d001648eSDavid Howells 	}
664d001648eSDavid Howells 	goto out;
665d001648eSDavid Howells }
666d001648eSDavid Howells EXPORT_SYMBOL(rxrpc_kernel_recv_data);
667