1 /* RxRPC recvmsg() implementation
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/net.h>
15 #include <linux/skbuff.h>
16 #include <linux/export.h>
18 #include <net/af_rxrpc.h>
19 #include "ar-internal.h"
22 * Post a call for attention by the socket or kernel service. Further
23 * notifications are suppressed by putting recvmsg_link on a dummy queue.
25 void rxrpc_notify_socket(struct rxrpc_call *call)
27 struct rxrpc_sock *rx;
30 _enter("%d", call->debug_id);
32 if (!list_empty(&call->recvmsg_link))
37 rx = rcu_dereference(call->socket);
39 if (rx && sk->sk_state < RXRPC_CLOSE) {
40 if (call->notify_rx) {
41 call->notify_rx(sk, call, call->user_call_ID);
43 write_lock_bh(&rx->recvmsg_lock);
44 if (list_empty(&call->recvmsg_link)) {
45 rxrpc_get_call(call, rxrpc_call_got);
46 list_add_tail(&call->recvmsg_link, &rx->recvmsg_q);
48 write_unlock_bh(&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_recvmsg(call, rxrpc_recvmsg_terminal, call->rx_hard_ack,
98 call->rx_pkt_offset, call->rx_pkt_len, ret);
103 * Pass back notification of a new call. The call is added to the
104 * to-be-accepted list. This means that the next call to be accepted might not
105 * be the last call seen awaiting acceptance, but unless we leave this on the
106 * front of the queue and block all other messages until someone gives us a
107 * user_ID for it, there's not a lot we can do.
109 static int rxrpc_recvmsg_new_call(struct rxrpc_sock *rx,
110 struct rxrpc_call *call,
111 struct msghdr *msg, int flags)
115 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NEW_CALL, 0, &tmp);
117 if (ret == 0 && !(flags & MSG_PEEK)) {
118 _debug("to be accepted");
119 write_lock_bh(&rx->recvmsg_lock);
120 list_del_init(&call->recvmsg_link);
121 write_unlock_bh(&rx->recvmsg_lock);
123 rxrpc_get_call(call, rxrpc_call_got);
124 write_lock(&rx->call_lock);
125 list_add_tail(&call->accept_link, &rx->to_be_accepted);
126 write_unlock(&rx->call_lock);
129 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_to_be_accepted, 1, 0, 0, ret);
134 * End the packet reception phase.
136 static void rxrpc_end_rx_phase(struct rxrpc_call *call)
138 _enter("%d,%s", call->debug_id, rxrpc_call_states[call->state]);
140 trace_rxrpc_receive(call, rxrpc_receive_end, 0, call->rx_top);
141 ASSERTCMP(call->rx_hard_ack, ==, call->rx_top);
143 if (call->state == RXRPC_CALL_CLIENT_RECV_REPLY) {
144 rxrpc_propose_ACK(call, RXRPC_ACK_IDLE, 0, 0, true, false);
145 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ACK);
148 write_lock_bh(&call->state_lock);
150 switch (call->state) {
151 case RXRPC_CALL_CLIENT_RECV_REPLY:
152 __rxrpc_call_completed(call);
155 case RXRPC_CALL_SERVER_RECV_REQUEST:
156 call->tx_phase = true;
157 call->state = RXRPC_CALL_SERVER_ACK_REQUEST;
163 write_unlock_bh(&call->state_lock);
167 * Discard a packet we've used up and advance the Rx window by one.
169 static void rxrpc_rotate_rx_window(struct rxrpc_call *call)
171 struct rxrpc_skb_priv *sp;
173 rxrpc_serial_t serial;
174 rxrpc_seq_t hard_ack, top;
178 _enter("%d", call->debug_id);
180 hard_ack = call->rx_hard_ack;
181 top = smp_load_acquire(&call->rx_top);
182 ASSERT(before(hard_ack, top));
185 ix = hard_ack & RXRPC_RXTX_BUFF_MASK;
186 skb = call->rxtx_buffer[ix];
187 rxrpc_see_skb(skb, rxrpc_skb_rx_rotated);
189 flags = sp->hdr.flags;
190 serial = sp->hdr.serial;
191 if (call->rxtx_annotations[ix] & RXRPC_RX_ANNO_JUMBO)
192 serial += (call->rxtx_annotations[ix] & RXRPC_RX_ANNO_JUMBO) - 1;
194 call->rxtx_buffer[ix] = NULL;
195 call->rxtx_annotations[ix] = 0;
196 /* Barrier against rxrpc_input_data(). */
197 smp_store_release(&call->rx_hard_ack, hard_ack);
199 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
201 _debug("%u,%u,%02x", hard_ack, top, flags);
202 trace_rxrpc_receive(call, rxrpc_receive_rotate, serial, hard_ack);
203 if (flags & RXRPC_LAST_PACKET)
204 rxrpc_end_rx_phase(call);
208 * Decrypt and verify a (sub)packet. The packet's length may be changed due to
209 * padding, but if this is the case, the packet length will be resident in the
210 * socket buffer. Note that we can't modify the master skb info as the skb may
211 * be the home to multiple subpackets.
213 static int rxrpc_verify_packet(struct rxrpc_call *call, struct sk_buff *skb,
215 unsigned int offset, unsigned int len)
217 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
218 rxrpc_seq_t seq = sp->hdr.seq;
219 u16 cksum = sp->hdr.cksum;
223 /* For all but the head jumbo subpacket, the security checksum is in a
224 * jumbo header immediately prior to the data.
226 if ((annotation & RXRPC_RX_ANNO_JUMBO) > 1) {
228 if (skb_copy_bits(skb, offset - 2, &tmp, 2) < 0)
231 seq += (annotation & RXRPC_RX_ANNO_JUMBO) - 1;
234 return call->conn->security->verify_packet(call, skb, offset, len,
239 * Locate the data within a packet. This is complicated by:
241 * (1) An skb may contain a jumbo packet - so we have to find the appropriate
244 * (2) The (sub)packets may be encrypted and, if so, the encrypted portion
245 * contains an extra header which includes the true length of the data,
246 * excluding any encrypted padding.
248 static int rxrpc_locate_data(struct rxrpc_call *call, struct sk_buff *skb,
250 unsigned int *_offset, unsigned int *_len)
252 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
253 unsigned int offset = *_offset;
254 unsigned int len = *_len;
256 u8 annotation = *_annotation;
258 /* Locate the subpacket */
260 len = skb->len - sp->offset;
261 if ((annotation & RXRPC_RX_ANNO_JUMBO) > 0) {
262 offset += (((annotation & RXRPC_RX_ANNO_JUMBO) - 1) *
263 RXRPC_JUMBO_SUBPKTLEN);
264 len = (annotation & RXRPC_RX_ANNO_JLAST) ?
265 skb->len - offset : RXRPC_JUMBO_SUBPKTLEN;
268 if (!(annotation & RXRPC_RX_ANNO_VERIFIED)) {
269 ret = rxrpc_verify_packet(call, skb, annotation, offset, len);
272 *_annotation |= RXRPC_RX_ANNO_VERIFIED;
277 call->conn->security->locate_data(call, skb, _offset, _len);
282 * Deliver messages to a call. This keeps processing packets until the buffer
283 * is filled and we find either more DATA (returns 0) or the end of the DATA
284 * (returns 1). If more packets are required, it returns -EAGAIN.
286 static int rxrpc_recvmsg_data(struct socket *sock, struct rxrpc_call *call,
287 struct msghdr *msg, struct iov_iter *iter,
288 size_t len, int flags, size_t *_offset)
290 struct rxrpc_skb_priv *sp;
292 rxrpc_seq_t hard_ack, top, seq;
295 unsigned int rx_pkt_offset, rx_pkt_len;
296 int ix, copy, ret = -EAGAIN, ret2;
298 rx_pkt_offset = call->rx_pkt_offset;
299 rx_pkt_len = call->rx_pkt_len;
301 if (call->state >= RXRPC_CALL_SERVER_ACK_REQUEST) {
302 seq = call->rx_hard_ack;
307 /* Barriers against rxrpc_input_data(). */
308 hard_ack = call->rx_hard_ack;
309 top = smp_load_acquire(&call->rx_top);
310 for (seq = hard_ack + 1; before_eq(seq, top); seq++) {
311 ix = seq & RXRPC_RXTX_BUFF_MASK;
312 skb = call->rxtx_buffer[ix];
314 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_hole, seq,
315 rx_pkt_offset, rx_pkt_len, 0);
319 rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
322 if (!(flags & MSG_PEEK))
323 trace_rxrpc_receive(call, rxrpc_receive_front,
324 sp->hdr.serial, seq);
327 sock_recv_timestamp(msg, sock->sk, skb);
329 if (rx_pkt_offset == 0) {
330 ret2 = rxrpc_locate_data(call, skb,
331 &call->rxtx_annotations[ix],
332 &rx_pkt_offset, &rx_pkt_len);
333 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_next, seq,
334 rx_pkt_offset, rx_pkt_len, ret2);
340 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_cont, seq,
341 rx_pkt_offset, rx_pkt_len, 0);
344 /* We have to handle short, empty and used-up DATA packets. */
345 remain = len - *_offset;
350 ret2 = skb_copy_datagram_iter(skb, rx_pkt_offset, iter,
357 /* handle piecemeal consumption of data packets */
358 rx_pkt_offset += copy;
363 if (rx_pkt_len > 0) {
364 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_full, seq,
365 rx_pkt_offset, rx_pkt_len, 0);
366 ASSERTCMP(*_offset, ==, len);
371 /* The whole packet has been transferred. */
372 last = sp->hdr.flags & RXRPC_LAST_PACKET;
373 if (!(flags & MSG_PEEK))
374 rxrpc_rotate_rx_window(call);
379 ASSERTCMP(seq, ==, READ_ONCE(call->rx_top));
386 if (!(flags & MSG_PEEK)) {
387 call->rx_pkt_offset = rx_pkt_offset;
388 call->rx_pkt_len = rx_pkt_len;
391 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_data_return, seq,
392 rx_pkt_offset, rx_pkt_len, ret);
397 * Receive a message from an RxRPC socket
398 * - we need to be careful about two or more threads calling recvmsg
401 int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
404 struct rxrpc_call *call;
405 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
413 trace_rxrpc_recvmsg(NULL, rxrpc_recvmsg_enter, 0, 0, 0, 0);
415 if (flags & (MSG_OOB | MSG_TRUNC))
418 timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT);
423 /* Return immediately if a client socket has no outstanding calls */
424 if (RB_EMPTY_ROOT(&rx->calls) &&
425 list_empty(&rx->recvmsg_q) &&
426 rx->sk.sk_state != RXRPC_SERVER_LISTENING) {
427 release_sock(&rx->sk);
431 if (list_empty(&rx->recvmsg_q)) {
438 release_sock(&rx->sk);
440 /* Wait for something to happen */
441 prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait,
443 ret = sock_error(&rx->sk);
447 if (list_empty(&rx->recvmsg_q)) {
448 if (signal_pending(current))
449 goto wait_interrupted;
450 trace_rxrpc_recvmsg(NULL, rxrpc_recvmsg_wait,
452 timeo = schedule_timeout(timeo);
454 finish_wait(sk_sleep(&rx->sk), &wait);
458 /* Find the next call and dequeue it if we're not just peeking. If we
459 * do dequeue it, that comes with a ref that we will need to release.
461 write_lock_bh(&rx->recvmsg_lock);
462 l = rx->recvmsg_q.next;
463 call = list_entry(l, struct rxrpc_call, recvmsg_link);
464 if (!(flags & MSG_PEEK))
465 list_del_init(&call->recvmsg_link);
467 rxrpc_get_call(call, rxrpc_call_got);
468 write_unlock_bh(&rx->recvmsg_lock);
470 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_dequeue, 0, 0, 0, 0);
472 if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
475 if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
476 if (flags & MSG_CMSG_COMPAT) {
477 unsigned int id32 = call->user_call_ID;
479 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
480 sizeof(unsigned int), &id32);
482 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
483 sizeof(unsigned long),
484 &call->user_call_ID);
491 size_t len = sizeof(call->conn->params.peer->srx);
492 memcpy(msg->msg_name, &call->conn->params.peer->srx, len);
493 msg->msg_namelen = len;
496 switch (call->state) {
497 case RXRPC_CALL_SERVER_ACCEPTING:
498 ret = rxrpc_recvmsg_new_call(rx, call, msg, flags);
500 case RXRPC_CALL_CLIENT_RECV_REPLY:
501 case RXRPC_CALL_SERVER_RECV_REQUEST:
502 case RXRPC_CALL_SERVER_ACK_REQUEST:
503 ret = rxrpc_recvmsg_data(sock, call, msg, &msg->msg_iter, len,
508 if (after(call->rx_top, call->rx_hard_ack) &&
509 call->rxtx_buffer[(call->rx_hard_ack + 1) & RXRPC_RXTX_BUFF_MASK])
510 rxrpc_notify_socket(call);
520 if (call->state == RXRPC_CALL_COMPLETE) {
521 ret = rxrpc_recvmsg_term(call, msg);
524 if (!(flags & MSG_PEEK))
525 rxrpc_release_call(rx, call);
526 msg->msg_flags |= MSG_EOR;
531 msg->msg_flags |= MSG_MORE;
533 msg->msg_flags &= ~MSG_MORE;
537 rxrpc_put_call(call, rxrpc_call_put);
539 release_sock(&rx->sk);
540 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_return, 0, 0, 0, ret);
544 ret = sock_intr_errno(timeo);
546 finish_wait(sk_sleep(&rx->sk), &wait);
552 * rxrpc_kernel_recv_data - Allow a kernel service to receive data/info
553 * @sock: The socket that the call exists on
554 * @call: The call to send data through
555 * @buf: The buffer to receive into
556 * @size: The size of the buffer, including data already read
557 * @_offset: The running offset into the buffer.
558 * @want_more: True if more data is expected to be read
559 * @_abort: Where the abort code is stored if -ECONNABORTED is returned
561 * Allow a kernel service to receive data and pick up information about the
562 * state of a call. Returns 0 if got what was asked for and there's more
563 * available, 1 if we got what was asked for and we're at the end of the data
564 * and -EAGAIN if we need more data.
566 * Note that we may return -EAGAIN to drain empty packets at the end of the
567 * data, even if we've already copied over the requested data.
569 * This function adds the amount it transfers to *_offset, so this should be
570 * precleared as appropriate. Note that the amount remaining in the buffer is
571 * taken to be size - *_offset.
573 * *_abort should also be initialised to 0.
575 int rxrpc_kernel_recv_data(struct socket *sock, struct rxrpc_call *call,
576 void *buf, size_t size, size_t *_offset,
577 bool want_more, u32 *_abort)
579 struct iov_iter iter;
583 _enter("{%d,%s},%zu/%zu,%d",
584 call->debug_id, rxrpc_call_states[call->state],
585 *_offset, size, want_more);
587 ASSERTCMP(*_offset, <=, size);
588 ASSERTCMP(call->state, !=, RXRPC_CALL_SERVER_ACCEPTING);
590 iov.iov_base = buf + *_offset;
591 iov.iov_len = size - *_offset;
592 iov_iter_kvec(&iter, ITER_KVEC | READ, &iov, 1, size - *_offset);
596 switch (call->state) {
597 case RXRPC_CALL_CLIENT_RECV_REPLY:
598 case RXRPC_CALL_SERVER_RECV_REQUEST:
599 case RXRPC_CALL_SERVER_ACK_REQUEST:
600 ret = rxrpc_recvmsg_data(sock, call, NULL, &iter, size, 0,
605 /* We can only reach here with a partially full buffer if we
606 * have reached the end of the data. We must otherwise have a
607 * full buffer or have been given -EAGAIN.
613 goto read_phase_complete;
622 case RXRPC_CALL_COMPLETE:
633 release_sock(sock->sk);
634 _leave(" = %d [%zu,%d]", ret, *_offset, *_abort);
644 *_abort = call->abort_code;
646 if (call->completion == RXRPC_CALL_SUCCEEDED) {
653 EXPORT_SYMBOL(rxrpc_kernel_recv_data);