blob: 22447dbcc380211f1bd1eb84ad9fc2b48137ff0c [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* RxRPC recvmsg() implementation
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells ([email protected])
5 *
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.
10 */
11
Joe Perches9b6d5392016-06-02 12:08:52 -070012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
David Howells17926a72007-04-26 15:48:28 -070014#include <linux/net.h>
15#include <linux/skbuff.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040016#include <linux/export.h>
David Howells17926a72007-04-26 15:48:28 -070017#include <net/sock.h>
18#include <net/af_rxrpc.h>
19#include "ar-internal.h"
20
21/*
David Howells248f2192016-09-08 11:10:12 +010022 * Post a call for attention by the socket or kernel service. Further
23 * notifications are suppressed by putting recvmsg_link on a dummy queue.
24 */
25void rxrpc_notify_socket(struct rxrpc_call *call)
26{
27 struct rxrpc_sock *rx;
28 struct sock *sk;
29
30 _enter("%d", call->debug_id);
31
32 if (!list_empty(&call->recvmsg_link))
33 return;
34
35 rcu_read_lock();
36
37 rx = rcu_dereference(call->socket);
38 sk = &rx->sk;
39 if (rx && sk->sk_state < RXRPC_CLOSE) {
40 if (call->notify_rx) {
41 call->notify_rx(sk, call, call->user_call_ID);
42 } else {
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);
47 }
48 write_unlock_bh(&rx->recvmsg_lock);
49
50 if (!sock_flag(sk, SOCK_DEAD)) {
51 _debug("call %ps", sk->sk_data_ready);
52 sk->sk_data_ready(sk);
53 }
54 }
55 }
56
57 rcu_read_unlock();
58 _leave("");
59}
60
61/*
62 * Pass a call terminating message to userspace.
63 */
64static int rxrpc_recvmsg_term(struct rxrpc_call *call, struct msghdr *msg)
65{
66 u32 tmp = 0;
67 int ret;
68
69 switch (call->completion) {
70 case RXRPC_CALL_SUCCEEDED:
71 ret = 0;
72 if (rxrpc_is_service_call(call))
73 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ACK, 0, &tmp);
74 break;
75 case RXRPC_CALL_REMOTELY_ABORTED:
76 tmp = call->abort_code;
77 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &tmp);
78 break;
79 case RXRPC_CALL_LOCALLY_ABORTED:
80 tmp = call->abort_code;
81 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &tmp);
82 break;
83 case RXRPC_CALL_NETWORK_ERROR:
84 tmp = call->error;
85 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NET_ERROR, 4, &tmp);
86 break;
87 case RXRPC_CALL_LOCAL_ERROR:
88 tmp = call->error;
89 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_LOCAL_ERROR, 4, &tmp);
90 break;
91 default:
92 pr_err("Invalid terminal call state %u\n", call->state);
93 BUG();
94 break;
95 }
96
David Howells84997902016-09-17 11:13:31 +010097 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_terminal, call->rx_hard_ack,
98 call->rx_pkt_offset, call->rx_pkt_len, ret);
David Howells248f2192016-09-08 11:10:12 +010099 return ret;
100}
101
102/*
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.
108 */
109static int rxrpc_recvmsg_new_call(struct rxrpc_sock *rx,
110 struct rxrpc_call *call,
111 struct msghdr *msg, int flags)
112{
113 int tmp = 0, ret;
114
115 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NEW_CALL, 0, &tmp);
116
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);
122
David Howells3432a752016-09-13 09:05:14 +0100123 rxrpc_get_call(call, rxrpc_call_got);
David Howells248f2192016-09-08 11:10:12 +0100124 write_lock(&rx->call_lock);
125 list_add_tail(&call->accept_link, &rx->to_be_accepted);
126 write_unlock(&rx->call_lock);
127 }
128
David Howells84997902016-09-17 11:13:31 +0100129 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_to_be_accepted, 1, 0, 0, ret);
David Howells248f2192016-09-08 11:10:12 +0100130 return ret;
131}
132
133/*
134 * End the packet reception phase.
135 */
David Howellsb69d94d2016-09-24 18:05:27 +0100136static void rxrpc_end_rx_phase(struct rxrpc_call *call, rxrpc_serial_t serial)
David Howells248f2192016-09-08 11:10:12 +0100137{
138 _enter("%d,%s", call->debug_id, rxrpc_call_states[call->state]);
139
David Howells58dc63c2016-09-17 10:49:13 +0100140 trace_rxrpc_receive(call, rxrpc_receive_end, 0, call->rx_top);
David Howells816c9fc2016-09-17 10:49:11 +0100141 ASSERTCMP(call->rx_hard_ack, ==, call->rx_top);
142
David Howells248f2192016-09-08 11:10:12 +0100143 if (call->state == RXRPC_CALL_CLIENT_RECV_REPLY) {
David Howellsb69d94d2016-09-24 18:05:27 +0100144 rxrpc_propose_ACK(call, RXRPC_ACK_IDLE, 0, serial, true, false,
David Howells9c7ad432016-09-23 13:50:40 +0100145 rxrpc_propose_ack_terminal_ack);
David Howellsa5af7e12016-10-06 08:11:49 +0100146 rxrpc_send_ack_packet(call, false);
David Howells248f2192016-09-08 11:10:12 +0100147 }
148
149 write_lock_bh(&call->state_lock);
150
151 switch (call->state) {
152 case RXRPC_CALL_CLIENT_RECV_REPLY:
153 __rxrpc_call_completed(call);
David Howells9749fd22016-10-06 08:11:50 +0100154 write_unlock_bh(&call->state_lock);
David Howells248f2192016-09-08 11:10:12 +0100155 break;
156
157 case RXRPC_CALL_SERVER_RECV_REQUEST:
David Howells71f3ca42016-09-17 10:49:14 +0100158 call->tx_phase = true;
David Howells248f2192016-09-08 11:10:12 +0100159 call->state = RXRPC_CALL_SERVER_ACK_REQUEST;
David Howells9749fd22016-10-06 08:11:50 +0100160 call->ack_at = call->expire_at;
161 write_unlock_bh(&call->state_lock);
162 rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, 0, serial, false, true,
163 rxrpc_propose_ack_processing_op);
David Howells248f2192016-09-08 11:10:12 +0100164 break;
165 default:
David Howells9749fd22016-10-06 08:11:50 +0100166 write_unlock_bh(&call->state_lock);
David Howells248f2192016-09-08 11:10:12 +0100167 break;
168 }
David Howells248f2192016-09-08 11:10:12 +0100169}
170
171/*
172 * Discard a packet we've used up and advance the Rx window by one.
173 */
174static void rxrpc_rotate_rx_window(struct rxrpc_call *call)
175{
David Howells816c9fc2016-09-17 10:49:11 +0100176 struct rxrpc_skb_priv *sp;
David Howells248f2192016-09-08 11:10:12 +0100177 struct sk_buff *skb;
David Howells58dc63c2016-09-17 10:49:13 +0100178 rxrpc_serial_t serial;
David Howells248f2192016-09-08 11:10:12 +0100179 rxrpc_seq_t hard_ack, top;
David Howells816c9fc2016-09-17 10:49:11 +0100180 u8 flags;
David Howells248f2192016-09-08 11:10:12 +0100181 int ix;
182
183 _enter("%d", call->debug_id);
184
185 hard_ack = call->rx_hard_ack;
186 top = smp_load_acquire(&call->rx_top);
187 ASSERT(before(hard_ack, top));
188
189 hard_ack++;
190 ix = hard_ack & RXRPC_RXTX_BUFF_MASK;
191 skb = call->rxtx_buffer[ix];
David Howells71f3ca42016-09-17 10:49:14 +0100192 rxrpc_see_skb(skb, rxrpc_skb_rx_rotated);
David Howells816c9fc2016-09-17 10:49:11 +0100193 sp = rxrpc_skb(skb);
194 flags = sp->hdr.flags;
David Howells58dc63c2016-09-17 10:49:13 +0100195 serial = sp->hdr.serial;
196 if (call->rxtx_annotations[ix] & RXRPC_RX_ANNO_JUMBO)
197 serial += (call->rxtx_annotations[ix] & RXRPC_RX_ANNO_JUMBO) - 1;
198
David Howells248f2192016-09-08 11:10:12 +0100199 call->rxtx_buffer[ix] = NULL;
200 call->rxtx_annotations[ix] = 0;
201 /* Barrier against rxrpc_input_data(). */
202 smp_store_release(&call->rx_hard_ack, hard_ack);
203
David Howells71f3ca42016-09-17 10:49:14 +0100204 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
David Howells248f2192016-09-08 11:10:12 +0100205
David Howells816c9fc2016-09-17 10:49:11 +0100206 _debug("%u,%u,%02x", hard_ack, top, flags);
David Howells58dc63c2016-09-17 10:49:13 +0100207 trace_rxrpc_receive(call, rxrpc_receive_rotate, serial, hard_ack);
David Howells805b21b92016-09-24 18:05:26 +0100208 if (flags & RXRPC_LAST_PACKET) {
David Howellsb69d94d2016-09-24 18:05:27 +0100209 rxrpc_end_rx_phase(call, serial);
David Howells805b21b92016-09-24 18:05:26 +0100210 } else {
211 /* Check to see if there's an ACK that needs sending. */
212 if (after_eq(hard_ack, call->ackr_consumed + 2) ||
213 after_eq(top, call->ackr_seen + 2) ||
214 (hard_ack == top && after(hard_ack, call->ackr_consumed)))
215 rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, 0, serial,
216 true, false,
217 rxrpc_propose_ack_rotate_rx);
218 if (call->ackr_reason)
David Howellsa5af7e12016-10-06 08:11:49 +0100219 rxrpc_send_ack_packet(call, false);
David Howells805b21b92016-09-24 18:05:26 +0100220 }
David Howells248f2192016-09-08 11:10:12 +0100221}
222
223/*
224 * Decrypt and verify a (sub)packet. The packet's length may be changed due to
225 * padding, but if this is the case, the packet length will be resident in the
226 * socket buffer. Note that we can't modify the master skb info as the skb may
227 * be the home to multiple subpackets.
228 */
229static int rxrpc_verify_packet(struct rxrpc_call *call, struct sk_buff *skb,
230 u8 annotation,
231 unsigned int offset, unsigned int len)
232{
233 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
234 rxrpc_seq_t seq = sp->hdr.seq;
235 u16 cksum = sp->hdr.cksum;
236
237 _enter("");
238
239 /* For all but the head jumbo subpacket, the security checksum is in a
240 * jumbo header immediately prior to the data.
241 */
242 if ((annotation & RXRPC_RX_ANNO_JUMBO) > 1) {
243 __be16 tmp;
244 if (skb_copy_bits(skb, offset - 2, &tmp, 2) < 0)
245 BUG();
246 cksum = ntohs(tmp);
247 seq += (annotation & RXRPC_RX_ANNO_JUMBO) - 1;
248 }
249
250 return call->conn->security->verify_packet(call, skb, offset, len,
251 seq, cksum);
252}
253
254/*
255 * Locate the data within a packet. This is complicated by:
256 *
257 * (1) An skb may contain a jumbo packet - so we have to find the appropriate
258 * subpacket.
259 *
260 * (2) The (sub)packets may be encrypted and, if so, the encrypted portion
261 * contains an extra header which includes the true length of the data,
262 * excluding any encrypted padding.
263 */
264static int rxrpc_locate_data(struct rxrpc_call *call, struct sk_buff *skb,
265 u8 *_annotation,
266 unsigned int *_offset, unsigned int *_len)
267{
David Howells775e5b72016-09-30 13:26:03 +0100268 unsigned int offset = sizeof(struct rxrpc_wire_header);
David Howells248f2192016-09-08 11:10:12 +0100269 unsigned int len = *_len;
270 int ret;
271 u8 annotation = *_annotation;
272
David Howells248f2192016-09-08 11:10:12 +0100273 /* Locate the subpacket */
David Howells775e5b72016-09-30 13:26:03 +0100274 len = skb->len - offset;
David Howells248f2192016-09-08 11:10:12 +0100275 if ((annotation & RXRPC_RX_ANNO_JUMBO) > 0) {
276 offset += (((annotation & RXRPC_RX_ANNO_JUMBO) - 1) *
277 RXRPC_JUMBO_SUBPKTLEN);
278 len = (annotation & RXRPC_RX_ANNO_JLAST) ?
279 skb->len - offset : RXRPC_JUMBO_SUBPKTLEN;
280 }
281
282 if (!(annotation & RXRPC_RX_ANNO_VERIFIED)) {
283 ret = rxrpc_verify_packet(call, skb, annotation, offset, len);
284 if (ret < 0)
285 return ret;
286 *_annotation |= RXRPC_RX_ANNO_VERIFIED;
287 }
288
289 *_offset = offset;
290 *_len = len;
291 call->conn->security->locate_data(call, skb, _offset, _len);
292 return 0;
293}
294
295/*
296 * Deliver messages to a call. This keeps processing packets until the buffer
297 * is filled and we find either more DATA (returns 0) or the end of the DATA
298 * (returns 1). If more packets are required, it returns -EAGAIN.
299 */
300static int rxrpc_recvmsg_data(struct socket *sock, struct rxrpc_call *call,
301 struct msghdr *msg, struct iov_iter *iter,
302 size_t len, int flags, size_t *_offset)
303{
304 struct rxrpc_skb_priv *sp;
305 struct sk_buff *skb;
306 rxrpc_seq_t hard_ack, top, seq;
307 size_t remain;
308 bool last;
309 unsigned int rx_pkt_offset, rx_pkt_len;
David Howells816c9fc2016-09-17 10:49:11 +0100310 int ix, copy, ret = -EAGAIN, ret2;
David Howells248f2192016-09-08 11:10:12 +0100311
David Howells248f2192016-09-08 11:10:12 +0100312 rx_pkt_offset = call->rx_pkt_offset;
313 rx_pkt_len = call->rx_pkt_len;
314
David Howells816c9fc2016-09-17 10:49:11 +0100315 if (call->state >= RXRPC_CALL_SERVER_ACK_REQUEST) {
316 seq = call->rx_hard_ack;
317 ret = 1;
318 goto done;
319 }
320
David Howells248f2192016-09-08 11:10:12 +0100321 /* Barriers against rxrpc_input_data(). */
322 hard_ack = call->rx_hard_ack;
David Howellsd7e15832017-02-24 21:57:13 +0000323 seq = hard_ack + 1;
324 while (top = smp_load_acquire(&call->rx_top),
325 before_eq(seq, top)
326 ) {
David Howells248f2192016-09-08 11:10:12 +0100327 ix = seq & RXRPC_RXTX_BUFF_MASK;
328 skb = call->rxtx_buffer[ix];
David Howells84997902016-09-17 11:13:31 +0100329 if (!skb) {
330 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_hole, seq,
331 rx_pkt_offset, rx_pkt_len, 0);
David Howells248f2192016-09-08 11:10:12 +0100332 break;
David Howells84997902016-09-17 11:13:31 +0100333 }
David Howells248f2192016-09-08 11:10:12 +0100334 smp_rmb();
David Howells71f3ca42016-09-17 10:49:14 +0100335 rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
David Howells248f2192016-09-08 11:10:12 +0100336 sp = rxrpc_skb(skb);
337
David Howells58dc63c2016-09-17 10:49:13 +0100338 if (!(flags & MSG_PEEK))
339 trace_rxrpc_receive(call, rxrpc_receive_front,
340 sp->hdr.serial, seq);
341
David Howells248f2192016-09-08 11:10:12 +0100342 if (msg)
343 sock_recv_timestamp(msg, sock->sk, skb);
344
David Howells2e2ea512016-09-17 10:49:11 +0100345 if (rx_pkt_offset == 0) {
David Howells816c9fc2016-09-17 10:49:11 +0100346 ret2 = rxrpc_locate_data(call, skb,
347 &call->rxtx_annotations[ix],
348 &rx_pkt_offset, &rx_pkt_len);
David Howells84997902016-09-17 11:13:31 +0100349 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_next, seq,
350 rx_pkt_offset, rx_pkt_len, ret2);
David Howells816c9fc2016-09-17 10:49:11 +0100351 if (ret2 < 0) {
352 ret = ret2;
David Howells2e2ea512016-09-17 10:49:11 +0100353 goto out;
David Howells816c9fc2016-09-17 10:49:11 +0100354 }
David Howells84997902016-09-17 11:13:31 +0100355 } else {
356 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_cont, seq,
357 rx_pkt_offset, rx_pkt_len, 0);
David Howells2e2ea512016-09-17 10:49:11 +0100358 }
David Howells248f2192016-09-08 11:10:12 +0100359
360 /* We have to handle short, empty and used-up DATA packets. */
361 remain = len - *_offset;
362 copy = rx_pkt_len;
363 if (copy > remain)
364 copy = remain;
365 if (copy > 0) {
David Howells816c9fc2016-09-17 10:49:11 +0100366 ret2 = skb_copy_datagram_iter(skb, rx_pkt_offset, iter,
367 copy);
368 if (ret2 < 0) {
369 ret = ret2;
David Howells248f2192016-09-08 11:10:12 +0100370 goto out;
David Howells816c9fc2016-09-17 10:49:11 +0100371 }
David Howells248f2192016-09-08 11:10:12 +0100372
373 /* handle piecemeal consumption of data packets */
David Howells248f2192016-09-08 11:10:12 +0100374 rx_pkt_offset += copy;
375 rx_pkt_len -= copy;
376 *_offset += copy;
377 }
378
379 if (rx_pkt_len > 0) {
David Howells84997902016-09-17 11:13:31 +0100380 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_full, seq,
381 rx_pkt_offset, rx_pkt_len, 0);
David Howells248f2192016-09-08 11:10:12 +0100382 ASSERTCMP(*_offset, ==, len);
David Howells816c9fc2016-09-17 10:49:11 +0100383 ret = 0;
David Howells248f2192016-09-08 11:10:12 +0100384 break;
385 }
386
387 /* The whole packet has been transferred. */
388 last = sp->hdr.flags & RXRPC_LAST_PACKET;
389 if (!(flags & MSG_PEEK))
390 rxrpc_rotate_rx_window(call);
391 rx_pkt_offset = 0;
392 rx_pkt_len = 0;
393
David Howells816c9fc2016-09-17 10:49:11 +0100394 if (last) {
395 ASSERTCMP(seq, ==, READ_ONCE(call->rx_top));
396 ret = 1;
397 goto out;
398 }
David Howellsd7e15832017-02-24 21:57:13 +0000399
400 seq++;
David Howells248f2192016-09-08 11:10:12 +0100401 }
402
David Howells248f2192016-09-08 11:10:12 +0100403out:
404 if (!(flags & MSG_PEEK)) {
405 call->rx_pkt_offset = rx_pkt_offset;
406 call->rx_pkt_len = rx_pkt_len;
407 }
David Howells816c9fc2016-09-17 10:49:11 +0100408done:
David Howells84997902016-09-17 11:13:31 +0100409 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_data_return, seq,
410 rx_pkt_offset, rx_pkt_len, ret);
David Howells248f2192016-09-08 11:10:12 +0100411 return ret;
412}
413
414/*
415 * Receive a message from an RxRPC socket
David Howells17926a72007-04-26 15:48:28 -0700416 * - we need to be careful about two or more threads calling recvmsg
417 * simultaneously
418 */
Ying Xue1b784142015-03-02 15:37:48 +0800419int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
420 int flags)
David Howells17926a72007-04-26 15:48:28 -0700421{
David Howells248f2192016-09-08 11:10:12 +0100422 struct rxrpc_call *call;
David Howells17926a72007-04-26 15:48:28 -0700423 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
David Howells248f2192016-09-08 11:10:12 +0100424 struct list_head *l;
425 size_t copied = 0;
David Howells17926a72007-04-26 15:48:28 -0700426 long timeo;
David Howells248f2192016-09-08 11:10:12 +0100427 int ret;
David Howells17926a72007-04-26 15:48:28 -0700428
429 DEFINE_WAIT(wait);
430
David Howells84997902016-09-17 11:13:31 +0100431 trace_rxrpc_recvmsg(NULL, rxrpc_recvmsg_enter, 0, 0, 0, 0);
David Howells17926a72007-04-26 15:48:28 -0700432
433 if (flags & (MSG_OOB | MSG_TRUNC))
434 return -EOPNOTSUPP;
435
David Howells17926a72007-04-26 15:48:28 -0700436 timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT);
David Howells17926a72007-04-26 15:48:28 -0700437
David Howells248f2192016-09-08 11:10:12 +0100438try_again:
David Howells17926a72007-04-26 15:48:28 -0700439 lock_sock(&rx->sk);
440
David Howells248f2192016-09-08 11:10:12 +0100441 /* Return immediately if a client socket has no outstanding calls */
442 if (RB_EMPTY_ROOT(&rx->calls) &&
443 list_empty(&rx->recvmsg_q) &&
444 rx->sk.sk_state != RXRPC_SERVER_LISTENING) {
445 release_sock(&rx->sk);
446 return -ENODATA;
447 }
448
449 if (list_empty(&rx->recvmsg_q)) {
450 ret = -EWOULDBLOCK;
David Howells84997902016-09-17 11:13:31 +0100451 if (timeo == 0) {
452 call = NULL;
David Howells248f2192016-09-08 11:10:12 +0100453 goto error_no_call;
David Howells84997902016-09-17 11:13:31 +0100454 }
David Howells248f2192016-09-08 11:10:12 +0100455
456 release_sock(&rx->sk);
457
458 /* Wait for something to happen */
459 prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait,
460 TASK_INTERRUPTIBLE);
461 ret = sock_error(&rx->sk);
462 if (ret)
463 goto wait_error;
464
465 if (list_empty(&rx->recvmsg_q)) {
466 if (signal_pending(current))
467 goto wait_interrupted;
David Howells84997902016-09-17 11:13:31 +0100468 trace_rxrpc_recvmsg(NULL, rxrpc_recvmsg_wait,
469 0, 0, 0, 0);
David Howells248f2192016-09-08 11:10:12 +0100470 timeo = schedule_timeout(timeo);
David Howells17926a72007-04-26 15:48:28 -0700471 }
David Howells248f2192016-09-08 11:10:12 +0100472 finish_wait(sk_sleep(&rx->sk), &wait);
473 goto try_again;
474 }
David Howells17926a72007-04-26 15:48:28 -0700475
David Howells248f2192016-09-08 11:10:12 +0100476 /* Find the next call and dequeue it if we're not just peeking. If we
477 * do dequeue it, that comes with a ref that we will need to release.
478 */
479 write_lock_bh(&rx->recvmsg_lock);
480 l = rx->recvmsg_q.next;
481 call = list_entry(l, struct rxrpc_call, recvmsg_link);
482 if (!(flags & MSG_PEEK))
483 list_del_init(&call->recvmsg_link);
484 else
David Howellsfff724292016-09-07 14:34:21 +0100485 rxrpc_get_call(call, rxrpc_call_got);
David Howells248f2192016-09-08 11:10:12 +0100486 write_unlock_bh(&rx->recvmsg_lock);
David Howells17926a72007-04-26 15:48:28 -0700487
David Howells84997902016-09-17 11:13:31 +0100488 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_dequeue, 0, 0, 0, 0);
David Howells17926a72007-04-26 15:48:28 -0700489
David Howells540b1c42017-02-27 15:43:06 +0000490 /* We're going to drop the socket lock, so we need to lock the call
491 * against interference by sendmsg.
492 */
493 if (!mutex_trylock(&call->user_mutex)) {
494 ret = -EWOULDBLOCK;
495 if (flags & MSG_DONTWAIT)
496 goto error_requeue_call;
497 ret = -ERESTARTSYS;
498 if (mutex_lock_interruptible(&call->user_mutex) < 0)
499 goto error_requeue_call;
500 }
501
502 release_sock(&rx->sk);
503
David Howells248f2192016-09-08 11:10:12 +0100504 if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
David Howells17926a72007-04-26 15:48:28 -0700505 BUG();
David Howells248f2192016-09-08 11:10:12 +0100506
507 if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
508 if (flags & MSG_CMSG_COMPAT) {
509 unsigned int id32 = call->user_call_ID;
510
511 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
512 sizeof(unsigned int), &id32);
513 } else {
514 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
515 sizeof(unsigned long),
516 &call->user_call_ID);
David Howellsf5c17aa2016-08-30 09:49:28 +0100517 }
David Howells248f2192016-09-08 11:10:12 +0100518 if (ret < 0)
David Howells540b1c42017-02-27 15:43:06 +0000519 goto error_unlock_call;
David Howells248f2192016-09-08 11:10:12 +0100520 }
521
522 if (msg->msg_name) {
523 size_t len = sizeof(call->conn->params.peer->srx);
524 memcpy(msg->msg_name, &call->conn->params.peer->srx, len);
525 msg->msg_namelen = len;
526 }
527
528 switch (call->state) {
529 case RXRPC_CALL_SERVER_ACCEPTING:
530 ret = rxrpc_recvmsg_new_call(rx, call, msg, flags);
David Howells17926a72007-04-26 15:48:28 -0700531 break;
David Howells248f2192016-09-08 11:10:12 +0100532 case RXRPC_CALL_CLIENT_RECV_REPLY:
533 case RXRPC_CALL_SERVER_RECV_REQUEST:
534 case RXRPC_CALL_SERVER_ACK_REQUEST:
535 ret = rxrpc_recvmsg_data(sock, call, msg, &msg->msg_iter, len,
536 flags, &copied);
537 if (ret == -EAGAIN)
538 ret = 0;
David Howells33b603f2016-09-13 22:36:21 +0100539
540 if (after(call->rx_top, call->rx_hard_ack) &&
541 call->rxtx_buffer[(call->rx_hard_ack + 1) & RXRPC_RXTX_BUFF_MASK])
542 rxrpc_notify_socket(call);
David Howells17926a72007-04-26 15:48:28 -0700543 break;
544 default:
David Howells248f2192016-09-08 11:10:12 +0100545 ret = 0;
David Howells17926a72007-04-26 15:48:28 -0700546 break;
547 }
548
549 if (ret < 0)
David Howells540b1c42017-02-27 15:43:06 +0000550 goto error_unlock_call;
David Howells17926a72007-04-26 15:48:28 -0700551
David Howells248f2192016-09-08 11:10:12 +0100552 if (call->state == RXRPC_CALL_COMPLETE) {
553 ret = rxrpc_recvmsg_term(call, msg);
554 if (ret < 0)
David Howells540b1c42017-02-27 15:43:06 +0000555 goto error_unlock_call;
David Howells248f2192016-09-08 11:10:12 +0100556 if (!(flags & MSG_PEEK))
557 rxrpc_release_call(rx, call);
558 msg->msg_flags |= MSG_EOR;
559 ret = 1;
David Howells17926a72007-04-26 15:48:28 -0700560 }
561
David Howells248f2192016-09-08 11:10:12 +0100562 if (ret == 0)
563 msg->msg_flags |= MSG_MORE;
564 else
565 msg->msg_flags &= ~MSG_MORE;
566 ret = copied;
David Howells17926a72007-04-26 15:48:28 -0700567
David Howells540b1c42017-02-27 15:43:06 +0000568error_unlock_call:
569 mutex_unlock(&call->user_mutex);
David Howellsfff724292016-09-07 14:34:21 +0100570 rxrpc_put_call(call, rxrpc_call_put);
David Howells540b1c42017-02-27 15:43:06 +0000571 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_return, 0, 0, 0, ret);
572 return ret;
573
574error_requeue_call:
575 if (!(flags & MSG_PEEK)) {
576 write_lock_bh(&rx->recvmsg_lock);
577 list_add(&call->recvmsg_link, &rx->recvmsg_q);
578 write_unlock_bh(&rx->recvmsg_lock);
579 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_requeue, 0, 0, 0, 0);
580 } else {
581 rxrpc_put_call(call, rxrpc_call_put);
582 }
David Howells248f2192016-09-08 11:10:12 +0100583error_no_call:
584 release_sock(&rx->sk);
David Howells84997902016-09-17 11:13:31 +0100585 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_return, 0, 0, 0, ret);
David Howells17926a72007-04-26 15:48:28 -0700586 return ret;
587
David Howells17926a72007-04-26 15:48:28 -0700588wait_interrupted:
589 ret = sock_intr_errno(timeo);
590wait_error:
Eric Dumazet4a4771a2010-04-25 22:20:06 +0000591 finish_wait(sk_sleep(&rx->sk), &wait);
David Howells84997902016-09-17 11:13:31 +0100592 call = NULL;
593 goto error_no_call;
David Howellsd0016482016-08-30 20:42:14 +0100594}
David Howells651350d2007-04-26 15:50:17 -0700595
596/**
David Howellsd0016482016-08-30 20:42:14 +0100597 * rxrpc_kernel_recv_data - Allow a kernel service to receive data/info
598 * @sock: The socket that the call exists on
599 * @call: The call to send data through
600 * @buf: The buffer to receive into
601 * @size: The size of the buffer, including data already read
602 * @_offset: The running offset into the buffer.
603 * @want_more: True if more data is expected to be read
604 * @_abort: Where the abort code is stored if -ECONNABORTED is returned
David Howells651350d2007-04-26 15:50:17 -0700605 *
David Howellsd0016482016-08-30 20:42:14 +0100606 * Allow a kernel service to receive data and pick up information about the
607 * state of a call. Returns 0 if got what was asked for and there's more
608 * available, 1 if we got what was asked for and we're at the end of the data
609 * and -EAGAIN if we need more data.
610 *
611 * Note that we may return -EAGAIN to drain empty packets at the end of the
612 * data, even if we've already copied over the requested data.
613 *
614 * This function adds the amount it transfers to *_offset, so this should be
615 * precleared as appropriate. Note that the amount remaining in the buffer is
616 * taken to be size - *_offset.
617 *
618 * *_abort should also be initialised to 0.
David Howells651350d2007-04-26 15:50:17 -0700619 */
David Howellsd0016482016-08-30 20:42:14 +0100620int rxrpc_kernel_recv_data(struct socket *sock, struct rxrpc_call *call,
621 void *buf, size_t size, size_t *_offset,
622 bool want_more, u32 *_abort)
David Howells651350d2007-04-26 15:50:17 -0700623{
David Howellsd0016482016-08-30 20:42:14 +0100624 struct iov_iter iter;
625 struct kvec iov;
626 int ret;
David Howells651350d2007-04-26 15:50:17 -0700627
David Howells248f2192016-09-08 11:10:12 +0100628 _enter("{%d,%s},%zu/%zu,%d",
629 call->debug_id, rxrpc_call_states[call->state],
630 *_offset, size, want_more);
David Howellsd0016482016-08-30 20:42:14 +0100631
632 ASSERTCMP(*_offset, <=, size);
633 ASSERTCMP(call->state, !=, RXRPC_CALL_SERVER_ACCEPTING);
634
635 iov.iov_base = buf + *_offset;
636 iov.iov_len = size - *_offset;
637 iov_iter_kvec(&iter, ITER_KVEC | READ, &iov, 1, size - *_offset);
638
David Howells540b1c42017-02-27 15:43:06 +0000639 mutex_lock(&call->user_mutex);
David Howellsd0016482016-08-30 20:42:14 +0100640
641 switch (call->state) {
642 case RXRPC_CALL_CLIENT_RECV_REPLY:
643 case RXRPC_CALL_SERVER_RECV_REQUEST:
644 case RXRPC_CALL_SERVER_ACK_REQUEST:
David Howells248f2192016-09-08 11:10:12 +0100645 ret = rxrpc_recvmsg_data(sock, call, NULL, &iter, size, 0,
646 _offset);
David Howellsd0016482016-08-30 20:42:14 +0100647 if (ret < 0)
648 goto out;
649
650 /* We can only reach here with a partially full buffer if we
651 * have reached the end of the data. We must otherwise have a
652 * full buffer or have been given -EAGAIN.
653 */
654 if (ret == 1) {
655 if (*_offset < size)
656 goto short_data;
657 if (!want_more)
658 goto read_phase_complete;
659 ret = 0;
660 goto out;
661 }
662
663 if (!want_more)
664 goto excess_data;
665 goto out;
666
667 case RXRPC_CALL_COMPLETE:
668 goto call_complete;
669
670 default:
David Howellsd0016482016-08-30 20:42:14 +0100671 ret = -EINPROGRESS;
672 goto out;
673 }
674
675read_phase_complete:
676 ret = 1;
677out:
David Howells540b1c42017-02-27 15:43:06 +0000678 mutex_unlock(&call->user_mutex);
David Howellsd0016482016-08-30 20:42:14 +0100679 _leave(" = %d [%zu,%d]", ret, *_offset, *_abort);
680 return ret;
681
682short_data:
683 ret = -EBADMSG;
684 goto out;
685excess_data:
686 ret = -EMSGSIZE;
687 goto out;
688call_complete:
689 *_abort = call->abort_code;
David Howellscf692072016-10-06 08:11:50 +0100690 ret = -call->error;
David Howellsd0016482016-08-30 20:42:14 +0100691 if (call->completion == RXRPC_CALL_SUCCEEDED) {
692 ret = 1;
693 if (size > 0)
694 ret = -ECONNRESET;
695 }
696 goto out;
David Howells651350d2007-04-26 15:50:17 -0700697}
David Howellsd0016482016-08-30 20:42:14 +0100698EXPORT_SYMBOL(rxrpc_kernel_recv_data);