1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RxRPC recvmsg() implementation
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/net.h>
11 #include <linux/skbuff.h>
12 #include <linux/export.h>
13 #include <linux/sched/signal.h>
16 #include <net/af_rxrpc.h>
17 #include "ar-internal.h"
20 * Post a call for attention by the socket or kernel service. Further
21 * notifications are suppressed by putting recvmsg_link on a dummy queue.
23 void rxrpc_notify_socket(struct rxrpc_call *call)
25 struct rxrpc_sock *rx;
28 _enter("%d", call->debug_id);
30 if (!list_empty(&call->recvmsg_link))
35 rx = rcu_dereference(call->socket);
37 if (rx && sk->sk_state < RXRPC_CLOSE) {
38 if (call->notify_rx) {
39 spin_lock(&call->notify_lock);
40 call->notify_rx(sk, call, call->user_call_ID);
41 spin_unlock(&call->notify_lock);
43 write_lock(&rx->recvmsg_lock);
44 if (list_empty(&call->recvmsg_link)) {
45 rxrpc_get_call(call, rxrpc_call_get_notify_socket);
46 list_add_tail(&call->recvmsg_link, &rx->recvmsg_q);
48 write_unlock(&rx->recvmsg_lock);
50 if (!sock_flag(sk, SOCK_DEAD)) {
51 _debug("call %ps", sk->sk_data_ready);
52 sk->sk_data_ready(sk);
62 * Pass a call terminating message to userspace.
64 static int rxrpc_recvmsg_term(struct rxrpc_call *call, struct msghdr *msg)
69 switch (call->completion) {
70 case RXRPC_CALL_SUCCEEDED:
72 if (rxrpc_is_service_call(call))
73 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ACK, 0, &tmp);
75 case RXRPC_CALL_REMOTELY_ABORTED:
76 tmp = call->abort_code;
77 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &tmp);
79 case RXRPC_CALL_LOCALLY_ABORTED:
80 tmp = call->abort_code;
81 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &tmp);
83 case RXRPC_CALL_NETWORK_ERROR:
85 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NET_ERROR, 4, &tmp);
87 case RXRPC_CALL_LOCAL_ERROR:
89 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_LOCAL_ERROR, 4, &tmp);
92 pr_err("Invalid terminal call state %u\n", call->state);
97 trace_rxrpc_recvdata(call, rxrpc_recvmsg_terminal,
98 lower_32_bits(atomic64_read(&call->ackr_window)) - 1,
99 call->rx_pkt_offset, call->rx_pkt_len, ret);
104 * End the packet reception phase.
106 static void rxrpc_end_rx_phase(struct rxrpc_call *call, rxrpc_serial_t serial)
108 rxrpc_seq_t whigh = READ_ONCE(call->rx_highest_seq);
110 _enter("%d,%s", call->debug_id, rxrpc_call_states[call->state]);
112 trace_rxrpc_receive(call, rxrpc_receive_end, 0, whigh);
114 if (call->state == RXRPC_CALL_CLIENT_RECV_REPLY)
115 rxrpc_propose_delay_ACK(call, serial, rxrpc_propose_ack_terminal_ack);
117 write_lock(&call->state_lock);
119 switch (call->state) {
120 case RXRPC_CALL_CLIENT_RECV_REPLY:
121 __rxrpc_call_completed(call);
122 write_unlock(&call->state_lock);
123 rxrpc_poke_call(call, rxrpc_call_poke_complete);
126 case RXRPC_CALL_SERVER_RECV_REQUEST:
127 call->state = RXRPC_CALL_SERVER_ACK_REQUEST;
128 call->expect_req_by = jiffies + MAX_JIFFY_OFFSET;
129 write_unlock(&call->state_lock);
130 rxrpc_propose_delay_ACK(call, serial,
131 rxrpc_propose_ack_processing_op);
134 write_unlock(&call->state_lock);
140 * Discard a packet we've used up and advance the Rx window by one.
142 static void rxrpc_rotate_rx_window(struct rxrpc_call *call)
144 struct rxrpc_skb_priv *sp;
146 rxrpc_serial_t serial;
147 rxrpc_seq_t old_consumed = call->rx_consumed, tseq;
151 _enter("%d", call->debug_id);
153 skb = skb_dequeue(&call->recvmsg_queue);
154 rxrpc_see_skb(skb, rxrpc_skb_see_rotate);
158 serial = sp->hdr.serial;
159 last = sp->hdr.flags & RXRPC_LAST_PACKET;
161 /* Barrier against rxrpc_input_data(). */
162 if (after(tseq, call->rx_consumed))
163 smp_store_release(&call->rx_consumed, tseq);
165 rxrpc_free_skb(skb, rxrpc_skb_put_rotate);
167 trace_rxrpc_receive(call, last ? rxrpc_receive_rotate_last : rxrpc_receive_rotate,
168 serial, call->rx_consumed);
170 rxrpc_end_rx_phase(call, serial);
174 /* Check to see if there's an ACK that needs sending. */
175 acked = atomic_add_return(call->rx_consumed - old_consumed,
176 &call->ackr_nr_consumed);
178 !test_and_set_bit(RXRPC_CALL_RX_IS_IDLE, &call->flags))
179 rxrpc_poke_call(call, rxrpc_call_poke_idle);
183 * Decrypt and verify a DATA packet.
185 static int rxrpc_verify_data(struct rxrpc_call *call, struct sk_buff *skb)
187 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
189 if (sp->flags & RXRPC_RX_VERIFIED)
191 return call->security->verify_packet(call, skb);
195 * Deliver messages to a call. This keeps processing packets until the buffer
196 * is filled and we find either more DATA (returns 0) or the end of the DATA
197 * (returns 1). If more packets are required, it returns -EAGAIN.
199 static int rxrpc_recvmsg_data(struct socket *sock, struct rxrpc_call *call,
200 struct msghdr *msg, struct iov_iter *iter,
201 size_t len, int flags, size_t *_offset)
203 struct rxrpc_skb_priv *sp;
207 unsigned int rx_pkt_offset, rx_pkt_len;
208 int copy, ret = -EAGAIN, ret2;
210 rx_pkt_offset = call->rx_pkt_offset;
211 rx_pkt_len = call->rx_pkt_len;
213 if (call->state >= RXRPC_CALL_SERVER_ACK_REQUEST) {
214 seq = lower_32_bits(atomic64_read(&call->ackr_window)) - 1;
219 /* No one else can be removing stuff from the queue, so we shouldn't
220 * need the Rx lock to walk it.
222 skb = skb_peek(&call->recvmsg_queue);
224 rxrpc_see_skb(skb, rxrpc_skb_see_recvmsg);
228 if (!(flags & MSG_PEEK))
229 trace_rxrpc_receive(call, rxrpc_receive_front,
230 sp->hdr.serial, seq);
233 sock_recv_timestamp(msg, sock->sk, skb);
235 if (rx_pkt_offset == 0) {
236 ret2 = rxrpc_verify_data(call, skb);
237 rx_pkt_offset = sp->offset;
238 rx_pkt_len = sp->len;
239 trace_rxrpc_recvdata(call, rxrpc_recvmsg_next, seq,
240 rx_pkt_offset, rx_pkt_len, ret2);
246 trace_rxrpc_recvdata(call, rxrpc_recvmsg_cont, seq,
247 rx_pkt_offset, rx_pkt_len, 0);
250 /* We have to handle short, empty and used-up DATA packets. */
251 remain = len - *_offset;
256 ret2 = skb_copy_datagram_iter(skb, rx_pkt_offset, iter,
263 /* handle piecemeal consumption of data packets */
264 rx_pkt_offset += copy;
269 if (rx_pkt_len > 0) {
270 trace_rxrpc_recvdata(call, rxrpc_recvmsg_full, seq,
271 rx_pkt_offset, rx_pkt_len, 0);
272 ASSERTCMP(*_offset, ==, len);
277 /* The whole packet has been transferred. */
278 if (sp->hdr.flags & RXRPC_LAST_PACKET)
283 skb = skb_peek_next(skb, &call->recvmsg_queue);
285 if (!(flags & MSG_PEEK))
286 rxrpc_rotate_rx_window(call);
290 if (!(flags & MSG_PEEK)) {
291 call->rx_pkt_offset = rx_pkt_offset;
292 call->rx_pkt_len = rx_pkt_len;
295 trace_rxrpc_recvdata(call, rxrpc_recvmsg_data_return, seq,
296 rx_pkt_offset, rx_pkt_len, ret);
298 set_bit(RXRPC_CALL_RX_IS_IDLE, &call->flags);
303 * Receive a message from an RxRPC socket
304 * - we need to be careful about two or more threads calling recvmsg
307 int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
310 struct rxrpc_call *call;
311 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
313 unsigned int call_debug_id = 0;
320 trace_rxrpc_recvmsg(0, rxrpc_recvmsg_enter, 0);
322 if (flags & (MSG_OOB | MSG_TRUNC))
325 timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT);
330 /* Return immediately if a client socket has no outstanding calls */
331 if (RB_EMPTY_ROOT(&rx->calls) &&
332 list_empty(&rx->recvmsg_q) &&
333 rx->sk.sk_state != RXRPC_SERVER_LISTENING) {
334 release_sock(&rx->sk);
338 if (list_empty(&rx->recvmsg_q)) {
345 release_sock(&rx->sk);
347 /* Wait for something to happen */
348 prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait,
350 ret = sock_error(&rx->sk);
354 if (list_empty(&rx->recvmsg_q)) {
355 if (signal_pending(current))
356 goto wait_interrupted;
357 trace_rxrpc_recvmsg(0, rxrpc_recvmsg_wait, 0);
358 timeo = schedule_timeout(timeo);
360 finish_wait(sk_sleep(&rx->sk), &wait);
364 /* Find the next call and dequeue it if we're not just peeking. If we
365 * do dequeue it, that comes with a ref that we will need to release.
367 write_lock(&rx->recvmsg_lock);
368 l = rx->recvmsg_q.next;
369 call = list_entry(l, struct rxrpc_call, recvmsg_link);
370 if (!(flags & MSG_PEEK))
371 list_del_init(&call->recvmsg_link);
373 rxrpc_get_call(call, rxrpc_call_get_recvmsg);
374 write_unlock(&rx->recvmsg_lock);
376 call_debug_id = call->debug_id;
377 trace_rxrpc_recvmsg(call_debug_id, rxrpc_recvmsg_dequeue, 0);
379 /* We're going to drop the socket lock, so we need to lock the call
380 * against interference by sendmsg.
382 if (!mutex_trylock(&call->user_mutex)) {
384 if (flags & MSG_DONTWAIT)
385 goto error_requeue_call;
387 if (mutex_lock_interruptible(&call->user_mutex) < 0)
388 goto error_requeue_call;
391 release_sock(&rx->sk);
393 if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
396 if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
397 if (flags & MSG_CMSG_COMPAT) {
398 unsigned int id32 = call->user_call_ID;
400 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
401 sizeof(unsigned int), &id32);
403 unsigned long idl = call->user_call_ID;
405 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
406 sizeof(unsigned long), &idl);
409 goto error_unlock_call;
412 if (msg->msg_name && call->peer) {
413 size_t len = sizeof(call->dest_srx);
415 memcpy(msg->msg_name, &call->dest_srx, len);
416 msg->msg_namelen = len;
419 switch (READ_ONCE(call->state)) {
420 case RXRPC_CALL_CLIENT_RECV_REPLY:
421 case RXRPC_CALL_SERVER_RECV_REQUEST:
422 case RXRPC_CALL_SERVER_ACK_REQUEST:
423 ret = rxrpc_recvmsg_data(sock, call, msg, &msg->msg_iter, len,
428 if (!skb_queue_empty(&call->recvmsg_queue))
429 rxrpc_notify_socket(call);
437 goto error_unlock_call;
439 if (call->state == RXRPC_CALL_COMPLETE) {
440 ret = rxrpc_recvmsg_term(call, msg);
442 goto error_unlock_call;
443 if (!(flags & MSG_PEEK))
444 rxrpc_release_call(rx, call);
445 msg->msg_flags |= MSG_EOR;
450 msg->msg_flags |= MSG_MORE;
452 msg->msg_flags &= ~MSG_MORE;
456 mutex_unlock(&call->user_mutex);
457 rxrpc_put_call(call, rxrpc_call_put_recvmsg);
458 trace_rxrpc_recvmsg(call_debug_id, rxrpc_recvmsg_return, ret);
462 if (!(flags & MSG_PEEK)) {
463 write_lock(&rx->recvmsg_lock);
464 list_add(&call->recvmsg_link, &rx->recvmsg_q);
465 write_unlock(&rx->recvmsg_lock);
466 trace_rxrpc_recvmsg(call_debug_id, rxrpc_recvmsg_requeue, 0);
468 rxrpc_put_call(call, rxrpc_call_put_recvmsg);
471 release_sock(&rx->sk);
473 trace_rxrpc_recvmsg(call_debug_id, rxrpc_recvmsg_return, ret);
477 ret = sock_intr_errno(timeo);
479 finish_wait(sk_sleep(&rx->sk), &wait);
485 * rxrpc_kernel_recv_data - Allow a kernel service to receive data/info
486 * @sock: The socket that the call exists on
487 * @call: The call to send data through
488 * @iter: The buffer to receive into
489 * @_len: The amount of data we want to receive (decreased on return)
490 * @want_more: True if more data is expected to be read
491 * @_abort: Where the abort code is stored if -ECONNABORTED is returned
492 * @_service: Where to store the actual service ID (may be upgraded)
494 * Allow a kernel service to receive data and pick up information about the
495 * state of a call. Returns 0 if got what was asked for and there's more
496 * available, 1 if we got what was asked for and we're at the end of the data
497 * and -EAGAIN if we need more data.
499 * Note that we may return -EAGAIN to drain empty packets at the end of the
500 * data, even if we've already copied over the requested data.
502 * *_abort should also be initialised to 0.
504 int rxrpc_kernel_recv_data(struct socket *sock, struct rxrpc_call *call,
505 struct iov_iter *iter, size_t *_len,
506 bool want_more, u32 *_abort, u16 *_service)
511 _enter("{%d,%s},%zu,%d",
512 call->debug_id, rxrpc_call_states[call->state],
515 ASSERTCMP(call->state, !=, RXRPC_CALL_SERVER_SECURING);
517 mutex_lock(&call->user_mutex);
519 switch (READ_ONCE(call->state)) {
520 case RXRPC_CALL_CLIENT_RECV_REPLY:
521 case RXRPC_CALL_SERVER_RECV_REQUEST:
522 case RXRPC_CALL_SERVER_ACK_REQUEST:
523 ret = rxrpc_recvmsg_data(sock, call, NULL, iter,
529 /* We can only reach here with a partially full buffer if we
530 * have reached the end of the data. We must otherwise have a
531 * full buffer or have been given -EAGAIN.
534 if (iov_iter_count(iter) > 0)
537 goto read_phase_complete;
546 case RXRPC_CALL_COMPLETE:
558 *_service = call->dest_srx.srx_service;
559 mutex_unlock(&call->user_mutex);
560 _leave(" = %d [%zu,%d]", ret, iov_iter_count(iter), *_abort);
564 trace_rxrpc_abort(call->debug_id, rxrpc_recvmsg_short_data,
565 call->cid, call->call_id, call->rx_consumed,
570 trace_rxrpc_abort(call->debug_id, rxrpc_recvmsg_excess_data,
571 call->cid, call->call_id, call->rx_consumed,
576 *_abort = call->abort_code;
578 if (call->completion == RXRPC_CALL_SUCCEEDED) {
580 if (iov_iter_count(iter) > 0)
585 EXPORT_SYMBOL(rxrpc_kernel_recv_data);