xref: /openbmc/linux/net/rxrpc/recvmsg.c (revision 9a36a6bc)
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)) {
45cb0fc0c9SDavid Howells 				rxrpc_get_call(call, rxrpc_call_get_notify_socket);
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;
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) {
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 
1765d7edbc9SDavid Howells 	trace_rxrpc_recvdata(call, rxrpc_recvmsg_terminal,
1775d7edbc9SDavid Howells 			     lower_32_bits(atomic64_read(&call->ackr_window)) - 1,
17884997905SDavid Howells 			     call->rx_pkt_offset, call->rx_pkt_len, ret);
179248f219cSDavid Howells 	return ret;
180248f219cSDavid Howells }
181248f219cSDavid Howells 
182248f219cSDavid Howells /*
183248f219cSDavid Howells  * End the packet reception phase.
184248f219cSDavid Howells  */
185b69d94d7SDavid Howells static void rxrpc_end_rx_phase(struct rxrpc_call *call, rxrpc_serial_t serial)
186248f219cSDavid Howells {
1875d7edbc9SDavid Howells 	rxrpc_seq_t whigh = READ_ONCE(call->rx_highest_seq);
1885d7edbc9SDavid Howells 
189248f219cSDavid Howells 	_enter("%d,%s", call->debug_id, rxrpc_call_states[call->state]);
190248f219cSDavid Howells 
1915d7edbc9SDavid Howells 	trace_rxrpc_receive(call, rxrpc_receive_end, 0, whigh);
192816c9fceSDavid Howells 
193530403d9SDavid Howells 	if (call->state == RXRPC_CALL_CLIENT_RECV_REPLY)
194530403d9SDavid Howells 		rxrpc_propose_delay_ACK(call, serial, rxrpc_propose_ack_terminal_ack);
195248f219cSDavid Howells 
196248f219cSDavid Howells 	write_lock_bh(&call->state_lock);
197248f219cSDavid Howells 
198248f219cSDavid Howells 	switch (call->state) {
199248f219cSDavid Howells 	case RXRPC_CALL_CLIENT_RECV_REPLY:
200248f219cSDavid Howells 		__rxrpc_call_completed(call);
2019749fd2bSDavid Howells 		write_unlock_bh(&call->state_lock);
202248f219cSDavid Howells 		break;
203248f219cSDavid Howells 
204248f219cSDavid Howells 	case RXRPC_CALL_SERVER_RECV_REQUEST:
205248f219cSDavid Howells 		call->state = RXRPC_CALL_SERVER_ACK_REQUEST;
206a158bdd3SDavid Howells 		call->expect_req_by = jiffies + MAX_JIFFY_OFFSET;
2079749fd2bSDavid Howells 		write_unlock_bh(&call->state_lock);
208530403d9SDavid Howells 		rxrpc_propose_delay_ACK(call, serial,
2099749fd2bSDavid Howells 					rxrpc_propose_ack_processing_op);
210248f219cSDavid Howells 		break;
211248f219cSDavid Howells 	default:
2129749fd2bSDavid Howells 		write_unlock_bh(&call->state_lock);
213248f219cSDavid Howells 		break;
214248f219cSDavid Howells 	}
215248f219cSDavid Howells }
216248f219cSDavid Howells 
217248f219cSDavid Howells /*
218248f219cSDavid Howells  * Discard a packet we've used up and advance the Rx window by one.
219248f219cSDavid Howells  */
220248f219cSDavid Howells static void rxrpc_rotate_rx_window(struct rxrpc_call *call)
221248f219cSDavid Howells {
222816c9fceSDavid Howells 	struct rxrpc_skb_priv *sp;
223248f219cSDavid Howells 	struct sk_buff *skb;
22458dc63c9SDavid Howells 	rxrpc_serial_t serial;
2255d7edbc9SDavid Howells 	rxrpc_seq_t old_consumed = call->rx_consumed, tseq;
2265d7edbc9SDavid Howells 	bool last;
2275d7edbc9SDavid Howells 	int acked;
228248f219cSDavid Howells 
229248f219cSDavid Howells 	_enter("%d", call->debug_id);
230248f219cSDavid Howells 
2315d7edbc9SDavid Howells 	skb = skb_dequeue(&call->recvmsg_queue);
232*9a36a6bcSDavid Howells 	rxrpc_see_skb(skb, rxrpc_skb_see_rotate);
2335d7edbc9SDavid Howells 
234816c9fceSDavid Howells 	sp = rxrpc_skb(skb);
2355d7edbc9SDavid Howells 	tseq   = sp->hdr.seq;
236d4d02d8bSDavid Howells 	serial = sp->hdr.serial;
2375d7edbc9SDavid Howells 	last   = sp->hdr.flags & RXRPC_LAST_PACKET;
238e2de6c40SDavid Howells 
239248f219cSDavid Howells 	/* Barrier against rxrpc_input_data(). */
2405d7edbc9SDavid Howells 	if (after(tseq, call->rx_consumed))
2415d7edbc9SDavid Howells 		smp_store_release(&call->rx_consumed, tseq);
242248f219cSDavid Howells 
243*9a36a6bcSDavid Howells 	rxrpc_free_skb(skb, rxrpc_skb_put_rotate);
244248f219cSDavid Howells 
2455d7edbc9SDavid Howells 	trace_rxrpc_receive(call, last ? rxrpc_receive_rotate_last : rxrpc_receive_rotate,
2465d7edbc9SDavid Howells 			    serial, call->rx_consumed);
247e2de6c40SDavid Howells 	if (last) {
248b69d94d7SDavid Howells 		rxrpc_end_rx_phase(call, serial);
2495d7edbc9SDavid Howells 		return;
2505d7edbc9SDavid Howells 	}
2515d7edbc9SDavid Howells 
252805b21b9SDavid Howells 	/* Check to see if there's an ACK that needs sending. */
2535d7edbc9SDavid Howells 	acked = atomic_add_return(call->rx_consumed - old_consumed,
2545d7edbc9SDavid Howells 				  &call->ackr_nr_consumed);
2555d7edbc9SDavid Howells 	if (acked > 2 &&
256530403d9SDavid Howells 	    !test_and_set_bit(RXRPC_CALL_IDLE_ACK_PENDING, &call->flags)) {
25772f0c6fbSDavid Howells 		rxrpc_send_ACK(call, RXRPC_ACK_IDLE, serial,
258805b21b9SDavid Howells 			       rxrpc_propose_ack_rotate_rx);
25972f0c6fbSDavid Howells 		rxrpc_transmit_ack_packets(call->peer->local);
26072f0c6fbSDavid Howells 	}
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) {
305*9a36a6bcSDavid 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 			}
3265d7edbc9SDavid Howells 			rxrpc_transmit_ack_packets(call->peer->local);
32784997905SDavid Howells 		} else {
328faf92e8dSDavid Howells 			trace_rxrpc_recvdata(call, rxrpc_recvmsg_cont, seq,
32984997905SDavid Howells 					     rx_pkt_offset, rx_pkt_len, 0);
330816c9fceSDavid Howells 		}
331248f219cSDavid Howells 
332248f219cSDavid Howells 		/* We have to handle short, empty and used-up DATA packets. */
333248f219cSDavid Howells 		remain = len - *_offset;
334248f219cSDavid Howells 		copy = rx_pkt_len;
335248f219cSDavid Howells 		if (copy > remain)
336248f219cSDavid Howells 			copy = remain;
337248f219cSDavid Howells 		if (copy > 0) {
338816c9fceSDavid Howells 			ret2 = skb_copy_datagram_iter(skb, rx_pkt_offset, iter,
339248f219cSDavid Howells 						      copy);
340816c9fceSDavid Howells 			if (ret2 < 0) {
341816c9fceSDavid Howells 				ret = ret2;
342248f219cSDavid Howells 				goto out;
343816c9fceSDavid Howells 			}
344248f219cSDavid Howells 
345248f219cSDavid Howells 			/* handle piecemeal consumption of data packets */
346248f219cSDavid Howells 			rx_pkt_offset += copy;
347248f219cSDavid Howells 			rx_pkt_len -= copy;
348248f219cSDavid Howells 			*_offset += copy;
349248f219cSDavid Howells 		}
350248f219cSDavid Howells 
351248f219cSDavid Howells 		if (rx_pkt_len > 0) {
352faf92e8dSDavid Howells 			trace_rxrpc_recvdata(call, rxrpc_recvmsg_full, seq,
35384997905SDavid Howells 					     rx_pkt_offset, rx_pkt_len, 0);
354248f219cSDavid Howells 			ASSERTCMP(*_offset, ==, len);
355816c9fceSDavid Howells 			ret = 0;
356248f219cSDavid Howells 			break;
357248f219cSDavid Howells 		}
358248f219cSDavid Howells 
359248f219cSDavid Howells 		/* The whole packet has been transferred. */
360d4d02d8bSDavid Howells 		if (sp->hdr.flags & RXRPC_LAST_PACKET)
361d4d02d8bSDavid Howells 			ret = 1;
362248f219cSDavid Howells 		rx_pkt_offset = 0;
363248f219cSDavid Howells 		rx_pkt_len = 0;
364248f219cSDavid Howells 
3655d7edbc9SDavid Howells 		skb = skb_peek_next(skb, &call->recvmsg_queue);
3665d7edbc9SDavid Howells 
367d4d02d8bSDavid Howells 		if (!(flags & MSG_PEEK))
368d4d02d8bSDavid Howells 			rxrpc_rotate_rx_window(call);
369248f219cSDavid Howells 	}
370248f219cSDavid Howells 
371248f219cSDavid Howells out:
372248f219cSDavid Howells 	if (!(flags & MSG_PEEK)) {
373248f219cSDavid Howells 		call->rx_pkt_offset = rx_pkt_offset;
374248f219cSDavid Howells 		call->rx_pkt_len = rx_pkt_len;
375248f219cSDavid Howells 	}
376816c9fceSDavid Howells done:
377faf92e8dSDavid Howells 	trace_rxrpc_recvdata(call, rxrpc_recvmsg_data_return, seq,
37884997905SDavid Howells 			     rx_pkt_offset, rx_pkt_len, ret);
379d0b35a42SDavid Howells 	if (ret == -EAGAIN)
380d0b35a42SDavid Howells 		set_bit(RXRPC_CALL_RX_UNDERRUN, &call->flags);
381248f219cSDavid Howells 	return ret;
382248f219cSDavid Howells }
383248f219cSDavid Howells 
384248f219cSDavid Howells /*
385248f219cSDavid Howells  * Receive a message from an RxRPC socket
3868c3e34a4SDavid Howells  * - we need to be careful about two or more threads calling recvmsg
3878c3e34a4SDavid Howells  *   simultaneously
3888c3e34a4SDavid Howells  */
3898c3e34a4SDavid Howells int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
3908c3e34a4SDavid Howells 		  int flags)
3918c3e34a4SDavid Howells {
392248f219cSDavid Howells 	struct rxrpc_call *call;
3938c3e34a4SDavid Howells 	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
394248f219cSDavid Howells 	struct list_head *l;
395248f219cSDavid Howells 	size_t copied = 0;
3968c3e34a4SDavid Howells 	long timeo;
397248f219cSDavid Howells 	int ret;
3988c3e34a4SDavid Howells 
3998c3e34a4SDavid Howells 	DEFINE_WAIT(wait);
4008c3e34a4SDavid Howells 
401faf92e8dSDavid Howells 	trace_rxrpc_recvmsg(NULL, 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;
438faf92e8dSDavid Howells 			trace_rxrpc_recvmsg(NULL, 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 	 */
448248f219cSDavid Howells 	write_lock_bh(&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);
455248f219cSDavid Howells 	write_unlock_bh(&rx->recvmsg_lock);
4568c3e34a4SDavid Howells 
457faf92e8dSDavid Howells 	trace_rxrpc_recvmsg(call, rxrpc_recvmsg_dequeue, 0);
458248f219cSDavid Howells 
459540b1c48SDavid Howells 	/* We're going to drop the socket lock, so we need to lock the call
460540b1c48SDavid Howells 	 * against interference by sendmsg.
461540b1c48SDavid Howells 	 */
462540b1c48SDavid Howells 	if (!mutex_trylock(&call->user_mutex)) {
463540b1c48SDavid Howells 		ret = -EWOULDBLOCK;
464540b1c48SDavid Howells 		if (flags & MSG_DONTWAIT)
465540b1c48SDavid Howells 			goto error_requeue_call;
466540b1c48SDavid Howells 		ret = -ERESTARTSYS;
467540b1c48SDavid Howells 		if (mutex_lock_interruptible(&call->user_mutex) < 0)
468540b1c48SDavid Howells 			goto error_requeue_call;
469540b1c48SDavid Howells 	}
470540b1c48SDavid Howells 
471540b1c48SDavid Howells 	release_sock(&rx->sk);
472540b1c48SDavid Howells 
473248f219cSDavid Howells 	if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
474248f219cSDavid Howells 		BUG();
475248f219cSDavid Howells 
476248f219cSDavid Howells 	if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
477248f219cSDavid Howells 		if (flags & MSG_CMSG_COMPAT) {
478248f219cSDavid Howells 			unsigned int id32 = call->user_call_ID;
479248f219cSDavid Howells 
480248f219cSDavid Howells 			ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
481248f219cSDavid Howells 				       sizeof(unsigned int), &id32);
482248f219cSDavid Howells 		} else {
483a16b8d0cSDavid Howells 			unsigned long idl = call->user_call_ID;
484a16b8d0cSDavid Howells 
485248f219cSDavid Howells 			ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
486a16b8d0cSDavid Howells 				       sizeof(unsigned long), &idl);
487248f219cSDavid Howells 		}
488248f219cSDavid Howells 		if (ret < 0)
489540b1c48SDavid Howells 			goto error_unlock_call;
490248f219cSDavid Howells 	}
491248f219cSDavid Howells 
49265550098SDavid Howells 	if (msg->msg_name && call->peer) {
49368d6d1aeSDavid Howells 		struct sockaddr_rxrpc *srx = msg->msg_name;
49468d6d1aeSDavid Howells 		size_t len = sizeof(call->peer->srx);
49568d6d1aeSDavid Howells 
49668d6d1aeSDavid Howells 		memcpy(msg->msg_name, &call->peer->srx, len);
49768d6d1aeSDavid Howells 		srx->srx_service = call->service_id;
4988c3e34a4SDavid Howells 		msg->msg_namelen = len;
4998c3e34a4SDavid Howells 	}
5008c3e34a4SDavid Howells 
501146d8fefSDavid Howells 	switch (READ_ONCE(call->state)) {
502248f219cSDavid Howells 	case RXRPC_CALL_CLIENT_RECV_REPLY:
503248f219cSDavid Howells 	case RXRPC_CALL_SERVER_RECV_REQUEST:
504248f219cSDavid Howells 	case RXRPC_CALL_SERVER_ACK_REQUEST:
505248f219cSDavid Howells 		ret = rxrpc_recvmsg_data(sock, call, msg, &msg->msg_iter, len,
506248f219cSDavid Howells 					 flags, &copied);
507248f219cSDavid Howells 		if (ret == -EAGAIN)
508248f219cSDavid Howells 			ret = 0;
50933b603fdSDavid Howells 
51072f0c6fbSDavid Howells 		rxrpc_transmit_ack_packets(call->peer->local);
5115d7edbc9SDavid Howells 		if (!skb_queue_empty(&call->recvmsg_queue))
51233b603fdSDavid Howells 			rxrpc_notify_socket(call);
5138c3e34a4SDavid Howells 		break;
5148c3e34a4SDavid Howells 	default:
515248f219cSDavid Howells 		ret = 0;
5168c3e34a4SDavid Howells 		break;
5178c3e34a4SDavid Howells 	}
5188c3e34a4SDavid Howells 
5198c3e34a4SDavid Howells 	if (ret < 0)
520540b1c48SDavid Howells 		goto error_unlock_call;
5218c3e34a4SDavid Howells 
522248f219cSDavid Howells 	if (call->state == RXRPC_CALL_COMPLETE) {
523248f219cSDavid Howells 		ret = rxrpc_recvmsg_term(call, msg);
524248f219cSDavid Howells 		if (ret < 0)
525540b1c48SDavid Howells 			goto error_unlock_call;
526248f219cSDavid Howells 		if (!(flags & MSG_PEEK))
5278d94aa38SDavid Howells 			rxrpc_release_call(rx, call);
528248f219cSDavid Howells 		msg->msg_flags |= MSG_EOR;
529248f219cSDavid Howells 		ret = 1;
5308c3e34a4SDavid Howells 	}
5318c3e34a4SDavid Howells 
532248f219cSDavid Howells 	if (ret == 0)
533248f219cSDavid Howells 		msg->msg_flags |= MSG_MORE;
534248f219cSDavid Howells 	else
535248f219cSDavid Howells 		msg->msg_flags &= ~MSG_MORE;
536248f219cSDavid Howells 	ret = copied;
5378c3e34a4SDavid Howells 
538540b1c48SDavid Howells error_unlock_call:
539540b1c48SDavid Howells 	mutex_unlock(&call->user_mutex);
540cb0fc0c9SDavid Howells 	rxrpc_put_call(call, rxrpc_call_put_recvmsg);
541faf92e8dSDavid Howells 	trace_rxrpc_recvmsg(call, rxrpc_recvmsg_return, ret);
542540b1c48SDavid Howells 	return ret;
543540b1c48SDavid Howells 
544540b1c48SDavid Howells error_requeue_call:
545540b1c48SDavid Howells 	if (!(flags & MSG_PEEK)) {
546540b1c48SDavid Howells 		write_lock_bh(&rx->recvmsg_lock);
547540b1c48SDavid Howells 		list_add(&call->recvmsg_link, &rx->recvmsg_q);
548540b1c48SDavid Howells 		write_unlock_bh(&rx->recvmsg_lock);
549faf92e8dSDavid Howells 		trace_rxrpc_recvmsg(call, rxrpc_recvmsg_requeue, 0);
550540b1c48SDavid Howells 	} else {
551cb0fc0c9SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_recvmsg);
552540b1c48SDavid Howells 	}
553248f219cSDavid Howells error_no_call:
554248f219cSDavid Howells 	release_sock(&rx->sk);
5556dce3c20SEric Dumazet error_trace:
556faf92e8dSDavid Howells 	trace_rxrpc_recvmsg(call, rxrpc_recvmsg_return, ret);
5578c3e34a4SDavid Howells 	return ret;
5588c3e34a4SDavid Howells 
5598c3e34a4SDavid Howells wait_interrupted:
5608c3e34a4SDavid Howells 	ret = sock_intr_errno(timeo);
5618c3e34a4SDavid Howells wait_error:
5628c3e34a4SDavid Howells 	finish_wait(sk_sleep(&rx->sk), &wait);
56384997905SDavid Howells 	call = NULL;
5646dce3c20SEric Dumazet 	goto error_trace;
565d001648eSDavid Howells }
5668c3e34a4SDavid Howells 
5678c3e34a4SDavid Howells /**
568d001648eSDavid Howells  * rxrpc_kernel_recv_data - Allow a kernel service to receive data/info
569d001648eSDavid Howells  * @sock: The socket that the call exists on
570d001648eSDavid Howells  * @call: The call to send data through
571eb9950ebSDavid Howells  * @iter: The buffer to receive into
572f105da1aSDavid Howells  * @_len: The amount of data we want to receive (decreased on return)
573d001648eSDavid Howells  * @want_more: True if more data is expected to be read
574d001648eSDavid Howells  * @_abort: Where the abort code is stored if -ECONNABORTED is returned
575a68f4a27SDavid Howells  * @_service: Where to store the actual service ID (may be upgraded)
5768c3e34a4SDavid Howells  *
577d001648eSDavid Howells  * Allow a kernel service to receive data and pick up information about the
578d001648eSDavid Howells  * state of a call.  Returns 0 if got what was asked for and there's more
579d001648eSDavid Howells  * available, 1 if we got what was asked for and we're at the end of the data
580d001648eSDavid Howells  * and -EAGAIN if we need more data.
581d001648eSDavid Howells  *
582d001648eSDavid Howells  * Note that we may return -EAGAIN to drain empty packets at the end of the
583d001648eSDavid Howells  * data, even if we've already copied over the requested data.
584d001648eSDavid Howells  *
585d001648eSDavid Howells  * *_abort should also be initialised to 0.
5868c3e34a4SDavid Howells  */
587d001648eSDavid Howells int rxrpc_kernel_recv_data(struct socket *sock, struct rxrpc_call *call,
588f105da1aSDavid Howells 			   struct iov_iter *iter, size_t *_len,
589a68f4a27SDavid Howells 			   bool want_more, u32 *_abort, u16 *_service)
5908c3e34a4SDavid Howells {
591eb9950ebSDavid Howells 	size_t offset = 0;
592d001648eSDavid Howells 	int ret;
5938c3e34a4SDavid Howells 
594eb9950ebSDavid Howells 	_enter("{%d,%s},%zu,%d",
595248f219cSDavid Howells 	       call->debug_id, rxrpc_call_states[call->state],
596f105da1aSDavid Howells 	       *_len, want_more);
597d001648eSDavid Howells 
5982d914c1bSDavid Howells 	ASSERTCMP(call->state, !=, RXRPC_CALL_SERVER_SECURING);
599d001648eSDavid Howells 
600540b1c48SDavid Howells 	mutex_lock(&call->user_mutex);
601d001648eSDavid Howells 
602146d8fefSDavid Howells 	switch (READ_ONCE(call->state)) {
603d001648eSDavid Howells 	case RXRPC_CALL_CLIENT_RECV_REPLY:
604d001648eSDavid Howells 	case RXRPC_CALL_SERVER_RECV_REQUEST:
605d001648eSDavid Howells 	case RXRPC_CALL_SERVER_ACK_REQUEST:
606eb9950ebSDavid Howells 		ret = rxrpc_recvmsg_data(sock, call, NULL, iter,
607f105da1aSDavid Howells 					 *_len, 0, &offset);
608f105da1aSDavid Howells 		*_len -= offset;
609d001648eSDavid Howells 		if (ret < 0)
610d001648eSDavid Howells 			goto out;
611d001648eSDavid Howells 
612d001648eSDavid Howells 		/* We can only reach here with a partially full buffer if we
613d001648eSDavid Howells 		 * have reached the end of the data.  We must otherwise have a
614d001648eSDavid Howells 		 * full buffer or have been given -EAGAIN.
615d001648eSDavid Howells 		 */
616d001648eSDavid Howells 		if (ret == 1) {
617eb9950ebSDavid Howells 			if (iov_iter_count(iter) > 0)
618d001648eSDavid Howells 				goto short_data;
619d001648eSDavid Howells 			if (!want_more)
620d001648eSDavid Howells 				goto read_phase_complete;
621d001648eSDavid Howells 			ret = 0;
622d001648eSDavid Howells 			goto out;
6238c3e34a4SDavid Howells 		}
6248c3e34a4SDavid Howells 
625d001648eSDavid Howells 		if (!want_more)
626d001648eSDavid Howells 			goto excess_data;
627d001648eSDavid Howells 		goto out;
628d001648eSDavid Howells 
629d001648eSDavid Howells 	case RXRPC_CALL_COMPLETE:
630d001648eSDavid Howells 		goto call_complete;
631d001648eSDavid Howells 
632d001648eSDavid Howells 	default:
633d001648eSDavid Howells 		ret = -EINPROGRESS;
634d001648eSDavid Howells 		goto out;
635d001648eSDavid Howells 	}
636d001648eSDavid Howells 
637d001648eSDavid Howells read_phase_complete:
638d001648eSDavid Howells 	ret = 1;
639d001648eSDavid Howells out:
64072f0c6fbSDavid Howells 	rxrpc_transmit_ack_packets(call->peer->local);
641a68f4a27SDavid Howells 	if (_service)
642a68f4a27SDavid Howells 		*_service = call->service_id;
643540b1c48SDavid Howells 	mutex_unlock(&call->user_mutex);
644eb9950ebSDavid Howells 	_leave(" = %d [%zu,%d]", ret, iov_iter_count(iter), *_abort);
645d001648eSDavid Howells 	return ret;
646d001648eSDavid Howells 
647d001648eSDavid Howells short_data:
648fb46f6eeSDavid Howells 	trace_rxrpc_rx_eproto(call, 0, tracepoint_string("short_data"));
649d001648eSDavid Howells 	ret = -EBADMSG;
650d001648eSDavid Howells 	goto out;
651d001648eSDavid Howells excess_data:
652fb46f6eeSDavid Howells 	trace_rxrpc_rx_eproto(call, 0, tracepoint_string("excess_data"));
653d001648eSDavid Howells 	ret = -EMSGSIZE;
654d001648eSDavid Howells 	goto out;
655d001648eSDavid Howells call_complete:
656d001648eSDavid Howells 	*_abort = call->abort_code;
6573a92789aSDavid Howells 	ret = call->error;
658d001648eSDavid Howells 	if (call->completion == RXRPC_CALL_SUCCEEDED) {
659d001648eSDavid Howells 		ret = 1;
660eb9950ebSDavid Howells 		if (iov_iter_count(iter) > 0)
661d001648eSDavid Howells 			ret = -ECONNRESET;
662d001648eSDavid Howells 	}
663d001648eSDavid Howells 	goto out;
664d001648eSDavid Howells }
665d001648eSDavid Howells EXPORT_SYMBOL(rxrpc_kernel_recv_data);
666