blob: 22d51087c5805114d8fbeaea762eb26f269efcef [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 (dhowells@redhat.com)
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
97 return ret;
98}
99
100/*
101 * Pass back notification of a new call. The call is added to the
102 * to-be-accepted list. This means that the next call to be accepted might not
103 * be the last call seen awaiting acceptance, but unless we leave this on the
104 * front of the queue and block all other messages until someone gives us a
105 * user_ID for it, there's not a lot we can do.
106 */
107static int rxrpc_recvmsg_new_call(struct rxrpc_sock *rx,
108 struct rxrpc_call *call,
109 struct msghdr *msg, int flags)
110{
111 int tmp = 0, ret;
112
113 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NEW_CALL, 0, &tmp);
114
115 if (ret == 0 && !(flags & MSG_PEEK)) {
116 _debug("to be accepted");
117 write_lock_bh(&rx->recvmsg_lock);
118 list_del_init(&call->recvmsg_link);
119 write_unlock_bh(&rx->recvmsg_lock);
120
David Howells3432a752016-09-13 09:05:14 +0100121 rxrpc_get_call(call, rxrpc_call_got);
David Howells248f2192016-09-08 11:10:12 +0100122 write_lock(&rx->call_lock);
123 list_add_tail(&call->accept_link, &rx->to_be_accepted);
124 write_unlock(&rx->call_lock);
125 }
126
127 return ret;
128}
129
130/*
131 * End the packet reception phase.
132 */
133static void rxrpc_end_rx_phase(struct rxrpc_call *call)
134{
135 _enter("%d,%s", call->debug_id, rxrpc_call_states[call->state]);
136
David Howells58dc63c2016-09-17 10:49:13 +0100137 trace_rxrpc_receive(call, rxrpc_receive_end, 0, call->rx_top);
David Howells816c9fc2016-09-17 10:49:11 +0100138 ASSERTCMP(call->rx_hard_ack, ==, call->rx_top);
139
David Howells248f2192016-09-08 11:10:12 +0100140 if (call->state == RXRPC_CALL_CLIENT_RECV_REPLY) {
141 rxrpc_propose_ACK(call, RXRPC_ACK_IDLE, 0, 0, true, false);
142 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ACK);
143 } else {
144 rxrpc_propose_ACK(call, RXRPC_ACK_IDLE, 0, 0, false, false);
145 }
146
147 write_lock_bh(&call->state_lock);
148
149 switch (call->state) {
150 case RXRPC_CALL_CLIENT_RECV_REPLY:
151 __rxrpc_call_completed(call);
152 break;
153
154 case RXRPC_CALL_SERVER_RECV_REQUEST:
155 call->state = RXRPC_CALL_SERVER_ACK_REQUEST;
156 break;
157 default:
158 break;
159 }
160
161 write_unlock_bh(&call->state_lock);
162}
163
164/*
165 * Discard a packet we've used up and advance the Rx window by one.
166 */
167static void rxrpc_rotate_rx_window(struct rxrpc_call *call)
168{
David Howells816c9fc2016-09-17 10:49:11 +0100169 struct rxrpc_skb_priv *sp;
David Howells248f2192016-09-08 11:10:12 +0100170 struct sk_buff *skb;
David Howells58dc63c2016-09-17 10:49:13 +0100171 rxrpc_serial_t serial;
David Howells248f2192016-09-08 11:10:12 +0100172 rxrpc_seq_t hard_ack, top;
David Howells816c9fc2016-09-17 10:49:11 +0100173 u8 flags;
David Howells248f2192016-09-08 11:10:12 +0100174 int ix;
175
176 _enter("%d", call->debug_id);
177
178 hard_ack = call->rx_hard_ack;
179 top = smp_load_acquire(&call->rx_top);
180 ASSERT(before(hard_ack, top));
181
182 hard_ack++;
183 ix = hard_ack & RXRPC_RXTX_BUFF_MASK;
184 skb = call->rxtx_buffer[ix];
185 rxrpc_see_skb(skb);
David Howells816c9fc2016-09-17 10:49:11 +0100186 sp = rxrpc_skb(skb);
187 flags = sp->hdr.flags;
David Howells58dc63c2016-09-17 10:49:13 +0100188 serial = sp->hdr.serial;
189 if (call->rxtx_annotations[ix] & RXRPC_RX_ANNO_JUMBO)
190 serial += (call->rxtx_annotations[ix] & RXRPC_RX_ANNO_JUMBO) - 1;
191
David Howells248f2192016-09-08 11:10:12 +0100192 call->rxtx_buffer[ix] = NULL;
193 call->rxtx_annotations[ix] = 0;
194 /* Barrier against rxrpc_input_data(). */
195 smp_store_release(&call->rx_hard_ack, hard_ack);
196
197 rxrpc_free_skb(skb);
198
David Howells816c9fc2016-09-17 10:49:11 +0100199 _debug("%u,%u,%02x", hard_ack, top, flags);
David Howells58dc63c2016-09-17 10:49:13 +0100200 trace_rxrpc_receive(call, rxrpc_receive_rotate, serial, hard_ack);
David Howells816c9fc2016-09-17 10:49:11 +0100201 if (flags & RXRPC_LAST_PACKET)
David Howells248f2192016-09-08 11:10:12 +0100202 rxrpc_end_rx_phase(call);
203}
204
205/*
206 * Decrypt and verify a (sub)packet. The packet's length may be changed due to
207 * padding, but if this is the case, the packet length will be resident in the
208 * socket buffer. Note that we can't modify the master skb info as the skb may
209 * be the home to multiple subpackets.
210 */
211static int rxrpc_verify_packet(struct rxrpc_call *call, struct sk_buff *skb,
212 u8 annotation,
213 unsigned int offset, unsigned int len)
214{
215 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
216 rxrpc_seq_t seq = sp->hdr.seq;
217 u16 cksum = sp->hdr.cksum;
218
219 _enter("");
220
221 /* For all but the head jumbo subpacket, the security checksum is in a
222 * jumbo header immediately prior to the data.
223 */
224 if ((annotation & RXRPC_RX_ANNO_JUMBO) > 1) {
225 __be16 tmp;
226 if (skb_copy_bits(skb, offset - 2, &tmp, 2) < 0)
227 BUG();
228 cksum = ntohs(tmp);
229 seq += (annotation & RXRPC_RX_ANNO_JUMBO) - 1;
230 }
231
232 return call->conn->security->verify_packet(call, skb, offset, len,
233 seq, cksum);
234}
235
236/*
237 * Locate the data within a packet. This is complicated by:
238 *
239 * (1) An skb may contain a jumbo packet - so we have to find the appropriate
240 * subpacket.
241 *
242 * (2) The (sub)packets may be encrypted and, if so, the encrypted portion
243 * contains an extra header which includes the true length of the data,
244 * excluding any encrypted padding.
245 */
246static int rxrpc_locate_data(struct rxrpc_call *call, struct sk_buff *skb,
247 u8 *_annotation,
248 unsigned int *_offset, unsigned int *_len)
249{
250 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
251 unsigned int offset = *_offset;
252 unsigned int len = *_len;
253 int ret;
254 u8 annotation = *_annotation;
255
David Howells248f2192016-09-08 11:10:12 +0100256 /* Locate the subpacket */
257 offset = sp->offset;
258 len = skb->len - sp->offset;
259 if ((annotation & RXRPC_RX_ANNO_JUMBO) > 0) {
260 offset += (((annotation & RXRPC_RX_ANNO_JUMBO) - 1) *
261 RXRPC_JUMBO_SUBPKTLEN);
262 len = (annotation & RXRPC_RX_ANNO_JLAST) ?
263 skb->len - offset : RXRPC_JUMBO_SUBPKTLEN;
264 }
265
266 if (!(annotation & RXRPC_RX_ANNO_VERIFIED)) {
267 ret = rxrpc_verify_packet(call, skb, annotation, offset, len);
268 if (ret < 0)
269 return ret;
270 *_annotation |= RXRPC_RX_ANNO_VERIFIED;
271 }
272
273 *_offset = offset;
274 *_len = len;
275 call->conn->security->locate_data(call, skb, _offset, _len);
276 return 0;
277}
278
279/*
280 * Deliver messages to a call. This keeps processing packets until the buffer
281 * is filled and we find either more DATA (returns 0) or the end of the DATA
282 * (returns 1). If more packets are required, it returns -EAGAIN.
283 */
284static int rxrpc_recvmsg_data(struct socket *sock, struct rxrpc_call *call,
285 struct msghdr *msg, struct iov_iter *iter,
286 size_t len, int flags, size_t *_offset)
287{
288 struct rxrpc_skb_priv *sp;
289 struct sk_buff *skb;
290 rxrpc_seq_t hard_ack, top, seq;
291 size_t remain;
292 bool last;
293 unsigned int rx_pkt_offset, rx_pkt_len;
David Howells816c9fc2016-09-17 10:49:11 +0100294 int ix, copy, ret = -EAGAIN, ret2;
David Howells248f2192016-09-08 11:10:12 +0100295
296 _enter("");
297
298 rx_pkt_offset = call->rx_pkt_offset;
299 rx_pkt_len = call->rx_pkt_len;
300
David Howells816c9fc2016-09-17 10:49:11 +0100301 if (call->state >= RXRPC_CALL_SERVER_ACK_REQUEST) {
302 seq = call->rx_hard_ack;
303 ret = 1;
304 goto done;
305 }
306
David Howells248f2192016-09-08 11:10:12 +0100307 /* 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];
313 if (!skb)
314 break;
315 smp_rmb();
316 rxrpc_see_skb(skb);
317 sp = rxrpc_skb(skb);
318
David Howells58dc63c2016-09-17 10:49:13 +0100319 if (!(flags & MSG_PEEK))
320 trace_rxrpc_receive(call, rxrpc_receive_front,
321 sp->hdr.serial, seq);
322
David Howells248f2192016-09-08 11:10:12 +0100323 if (msg)
324 sock_recv_timestamp(msg, sock->sk, skb);
325
David Howells2e2ea512016-09-17 10:49:11 +0100326 if (rx_pkt_offset == 0) {
David Howells816c9fc2016-09-17 10:49:11 +0100327 ret2 = rxrpc_locate_data(call, skb,
328 &call->rxtx_annotations[ix],
329 &rx_pkt_offset, &rx_pkt_len);
330 if (ret2 < 0) {
331 ret = ret2;
David Howells2e2ea512016-09-17 10:49:11 +0100332 goto out;
David Howells816c9fc2016-09-17 10:49:11 +0100333 }
David Howells2e2ea512016-09-17 10:49:11 +0100334 }
David Howells248f2192016-09-08 11:10:12 +0100335 _debug("recvmsg %x DATA #%u { %d, %d }",
336 sp->hdr.callNumber, seq, rx_pkt_offset, rx_pkt_len);
337
338 /* We have to handle short, empty and used-up DATA packets. */
339 remain = len - *_offset;
340 copy = rx_pkt_len;
341 if (copy > remain)
342 copy = remain;
343 if (copy > 0) {
David Howells816c9fc2016-09-17 10:49:11 +0100344 ret2 = skb_copy_datagram_iter(skb, rx_pkt_offset, iter,
345 copy);
346 if (ret2 < 0) {
347 ret = ret2;
David Howells248f2192016-09-08 11:10:12 +0100348 goto out;
David Howells816c9fc2016-09-17 10:49:11 +0100349 }
David Howells248f2192016-09-08 11:10:12 +0100350
351 /* handle piecemeal consumption of data packets */
352 _debug("copied %d @%zu", copy, *_offset);
353
354 rx_pkt_offset += copy;
355 rx_pkt_len -= copy;
356 *_offset += copy;
357 }
358
359 if (rx_pkt_len > 0) {
360 _debug("buffer full");
361 ASSERTCMP(*_offset, ==, len);
David Howells816c9fc2016-09-17 10:49:11 +0100362 ret = 0;
David Howells248f2192016-09-08 11:10:12 +0100363 break;
364 }
365
366 /* The whole packet has been transferred. */
367 last = sp->hdr.flags & RXRPC_LAST_PACKET;
368 if (!(flags & MSG_PEEK))
369 rxrpc_rotate_rx_window(call);
370 rx_pkt_offset = 0;
371 rx_pkt_len = 0;
372
David Howells816c9fc2016-09-17 10:49:11 +0100373 if (last) {
374 ASSERTCMP(seq, ==, READ_ONCE(call->rx_top));
375 ret = 1;
376 goto out;
377 }
David Howells248f2192016-09-08 11:10:12 +0100378 }
379
David Howells248f2192016-09-08 11:10:12 +0100380out:
381 if (!(flags & MSG_PEEK)) {
382 call->rx_pkt_offset = rx_pkt_offset;
383 call->rx_pkt_len = rx_pkt_len;
384 }
David Howells816c9fc2016-09-17 10:49:11 +0100385done:
David Howells248f2192016-09-08 11:10:12 +0100386 _leave(" = %d [%u/%u]", ret, seq, top);
387 return ret;
388}
389
390/*
391 * Receive a message from an RxRPC socket
David Howells17926a72007-04-26 15:48:28 -0700392 * - we need to be careful about two or more threads calling recvmsg
393 * simultaneously
394 */
Ying Xue1b784142015-03-02 15:37:48 +0800395int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
396 int flags)
David Howells17926a72007-04-26 15:48:28 -0700397{
David Howells248f2192016-09-08 11:10:12 +0100398 struct rxrpc_call *call;
David Howells17926a72007-04-26 15:48:28 -0700399 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
David Howells248f2192016-09-08 11:10:12 +0100400 struct list_head *l;
401 size_t copied = 0;
David Howells17926a72007-04-26 15:48:28 -0700402 long timeo;
David Howells248f2192016-09-08 11:10:12 +0100403 int ret;
David Howells17926a72007-04-26 15:48:28 -0700404
405 DEFINE_WAIT(wait);
406
407 _enter(",,,%zu,%d", len, flags);
408
409 if (flags & (MSG_OOB | MSG_TRUNC))
410 return -EOPNOTSUPP;
411
David Howells17926a72007-04-26 15:48:28 -0700412 timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT);
David Howells17926a72007-04-26 15:48:28 -0700413
David Howells248f2192016-09-08 11:10:12 +0100414try_again:
David Howells17926a72007-04-26 15:48:28 -0700415 lock_sock(&rx->sk);
416
David Howells248f2192016-09-08 11:10:12 +0100417 /* Return immediately if a client socket has no outstanding calls */
418 if (RB_EMPTY_ROOT(&rx->calls) &&
419 list_empty(&rx->recvmsg_q) &&
420 rx->sk.sk_state != RXRPC_SERVER_LISTENING) {
421 release_sock(&rx->sk);
422 return -ENODATA;
423 }
424
425 if (list_empty(&rx->recvmsg_q)) {
426 ret = -EWOULDBLOCK;
427 if (timeo == 0)
428 goto error_no_call;
429
430 release_sock(&rx->sk);
431
432 /* Wait for something to happen */
433 prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait,
434 TASK_INTERRUPTIBLE);
435 ret = sock_error(&rx->sk);
436 if (ret)
437 goto wait_error;
438
439 if (list_empty(&rx->recvmsg_q)) {
440 if (signal_pending(current))
441 goto wait_interrupted;
442 timeo = schedule_timeout(timeo);
David Howells17926a72007-04-26 15:48:28 -0700443 }
David Howells248f2192016-09-08 11:10:12 +0100444 finish_wait(sk_sleep(&rx->sk), &wait);
445 goto try_again;
446 }
David Howells17926a72007-04-26 15:48:28 -0700447
David Howells248f2192016-09-08 11:10:12 +0100448 /* Find the next call and dequeue it if we're not just peeking. If we
449 * do dequeue it, that comes with a ref that we will need to release.
450 */
451 write_lock_bh(&rx->recvmsg_lock);
452 l = rx->recvmsg_q.next;
453 call = list_entry(l, struct rxrpc_call, recvmsg_link);
454 if (!(flags & MSG_PEEK))
455 list_del_init(&call->recvmsg_link);
456 else
David Howellsfff724292016-09-07 14:34:21 +0100457 rxrpc_get_call(call, rxrpc_call_got);
David Howells248f2192016-09-08 11:10:12 +0100458 write_unlock_bh(&rx->recvmsg_lock);
David Howells17926a72007-04-26 15:48:28 -0700459
David Howells248f2192016-09-08 11:10:12 +0100460 _debug("recvmsg call %p", call);
David Howells17926a72007-04-26 15:48:28 -0700461
David Howells248f2192016-09-08 11:10:12 +0100462 if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
David Howells17926a72007-04-26 15:48:28 -0700463 BUG();
David Howells248f2192016-09-08 11:10:12 +0100464
465 if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
466 if (flags & MSG_CMSG_COMPAT) {
467 unsigned int id32 = call->user_call_ID;
468
469 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
470 sizeof(unsigned int), &id32);
471 } else {
472 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
473 sizeof(unsigned long),
474 &call->user_call_ID);
David Howellsf5c17aa2016-08-30 09:49:28 +0100475 }
David Howells248f2192016-09-08 11:10:12 +0100476 if (ret < 0)
477 goto error;
478 }
479
480 if (msg->msg_name) {
481 size_t len = sizeof(call->conn->params.peer->srx);
482 memcpy(msg->msg_name, &call->conn->params.peer->srx, len);
483 msg->msg_namelen = len;
484 }
485
486 switch (call->state) {
487 case RXRPC_CALL_SERVER_ACCEPTING:
488 ret = rxrpc_recvmsg_new_call(rx, call, msg, flags);
David Howells17926a72007-04-26 15:48:28 -0700489 break;
David Howells248f2192016-09-08 11:10:12 +0100490 case RXRPC_CALL_CLIENT_RECV_REPLY:
491 case RXRPC_CALL_SERVER_RECV_REQUEST:
492 case RXRPC_CALL_SERVER_ACK_REQUEST:
493 ret = rxrpc_recvmsg_data(sock, call, msg, &msg->msg_iter, len,
494 flags, &copied);
495 if (ret == -EAGAIN)
496 ret = 0;
David Howells33b603f2016-09-13 22:36:21 +0100497
498 if (after(call->rx_top, call->rx_hard_ack) &&
499 call->rxtx_buffer[(call->rx_hard_ack + 1) & RXRPC_RXTX_BUFF_MASK])
500 rxrpc_notify_socket(call);
David Howells17926a72007-04-26 15:48:28 -0700501 break;
502 default:
David Howells248f2192016-09-08 11:10:12 +0100503 ret = 0;
David Howells17926a72007-04-26 15:48:28 -0700504 break;
505 }
506
507 if (ret < 0)
David Howells248f2192016-09-08 11:10:12 +0100508 goto error;
David Howells17926a72007-04-26 15:48:28 -0700509
David Howells248f2192016-09-08 11:10:12 +0100510 if (call->state == RXRPC_CALL_COMPLETE) {
511 ret = rxrpc_recvmsg_term(call, msg);
512 if (ret < 0)
513 goto error;
514 if (!(flags & MSG_PEEK))
515 rxrpc_release_call(rx, call);
516 msg->msg_flags |= MSG_EOR;
517 ret = 1;
David Howells17926a72007-04-26 15:48:28 -0700518 }
519
David Howells248f2192016-09-08 11:10:12 +0100520 if (ret == 0)
521 msg->msg_flags |= MSG_MORE;
522 else
523 msg->msg_flags &= ~MSG_MORE;
524 ret = copied;
David Howells17926a72007-04-26 15:48:28 -0700525
David Howells248f2192016-09-08 11:10:12 +0100526error:
David Howellsfff724292016-09-07 14:34:21 +0100527 rxrpc_put_call(call, rxrpc_call_put);
David Howells248f2192016-09-08 11:10:12 +0100528error_no_call:
529 release_sock(&rx->sk);
David Howells17926a72007-04-26 15:48:28 -0700530 _leave(" = %d", ret);
531 return ret;
532
David Howells17926a72007-04-26 15:48:28 -0700533wait_interrupted:
534 ret = sock_intr_errno(timeo);
535wait_error:
Eric Dumazet4a4771a2010-04-25 22:20:06 +0000536 finish_wait(sk_sleep(&rx->sk), &wait);
David Howells248f2192016-09-08 11:10:12 +0100537 release_sock(&rx->sk);
538 _leave(" = %d [wait]", ret);
David Howellsd0016482016-08-30 20:42:14 +0100539 return ret;
540}
David Howells651350d2007-04-26 15:50:17 -0700541
542/**
David Howellsd0016482016-08-30 20:42:14 +0100543 * rxrpc_kernel_recv_data - Allow a kernel service to receive data/info
544 * @sock: The socket that the call exists on
545 * @call: The call to send data through
546 * @buf: The buffer to receive into
547 * @size: The size of the buffer, including data already read
548 * @_offset: The running offset into the buffer.
549 * @want_more: True if more data is expected to be read
550 * @_abort: Where the abort code is stored if -ECONNABORTED is returned
David Howells651350d2007-04-26 15:50:17 -0700551 *
David Howellsd0016482016-08-30 20:42:14 +0100552 * Allow a kernel service to receive data and pick up information about the
553 * state of a call. Returns 0 if got what was asked for and there's more
554 * available, 1 if we got what was asked for and we're at the end of the data
555 * and -EAGAIN if we need more data.
556 *
557 * Note that we may return -EAGAIN to drain empty packets at the end of the
558 * data, even if we've already copied over the requested data.
559 *
560 * This function adds the amount it transfers to *_offset, so this should be
561 * precleared as appropriate. Note that the amount remaining in the buffer is
562 * taken to be size - *_offset.
563 *
564 * *_abort should also be initialised to 0.
David Howells651350d2007-04-26 15:50:17 -0700565 */
David Howellsd0016482016-08-30 20:42:14 +0100566int rxrpc_kernel_recv_data(struct socket *sock, struct rxrpc_call *call,
567 void *buf, size_t size, size_t *_offset,
568 bool want_more, u32 *_abort)
David Howells651350d2007-04-26 15:50:17 -0700569{
David Howellsd0016482016-08-30 20:42:14 +0100570 struct iov_iter iter;
571 struct kvec iov;
572 int ret;
David Howells651350d2007-04-26 15:50:17 -0700573
David Howells248f2192016-09-08 11:10:12 +0100574 _enter("{%d,%s},%zu/%zu,%d",
575 call->debug_id, rxrpc_call_states[call->state],
576 *_offset, size, want_more);
David Howellsd0016482016-08-30 20:42:14 +0100577
578 ASSERTCMP(*_offset, <=, size);
579 ASSERTCMP(call->state, !=, RXRPC_CALL_SERVER_ACCEPTING);
580
581 iov.iov_base = buf + *_offset;
582 iov.iov_len = size - *_offset;
583 iov_iter_kvec(&iter, ITER_KVEC | READ, &iov, 1, size - *_offset);
584
585 lock_sock(sock->sk);
586
587 switch (call->state) {
588 case RXRPC_CALL_CLIENT_RECV_REPLY:
589 case RXRPC_CALL_SERVER_RECV_REQUEST:
590 case RXRPC_CALL_SERVER_ACK_REQUEST:
David Howells248f2192016-09-08 11:10:12 +0100591 ret = rxrpc_recvmsg_data(sock, call, NULL, &iter, size, 0,
592 _offset);
David Howellsd0016482016-08-30 20:42:14 +0100593 if (ret < 0)
594 goto out;
595
596 /* We can only reach here with a partially full buffer if we
597 * have reached the end of the data. We must otherwise have a
598 * full buffer or have been given -EAGAIN.
599 */
600 if (ret == 1) {
601 if (*_offset < size)
602 goto short_data;
603 if (!want_more)
604 goto read_phase_complete;
605 ret = 0;
606 goto out;
607 }
608
609 if (!want_more)
610 goto excess_data;
611 goto out;
612
613 case RXRPC_CALL_COMPLETE:
614 goto call_complete;
615
616 default:
David Howellsd0016482016-08-30 20:42:14 +0100617 ret = -EINPROGRESS;
618 goto out;
619 }
620
621read_phase_complete:
622 ret = 1;
623out:
624 release_sock(sock->sk);
625 _leave(" = %d [%zu,%d]", ret, *_offset, *_abort);
626 return ret;
627
628short_data:
629 ret = -EBADMSG;
630 goto out;
631excess_data:
632 ret = -EMSGSIZE;
633 goto out;
634call_complete:
635 *_abort = call->abort_code;
636 ret = call->error;
637 if (call->completion == RXRPC_CALL_SUCCEEDED) {
638 ret = 1;
639 if (size > 0)
640 ret = -ECONNRESET;
641 }
642 goto out;
David Howells651350d2007-04-26 15:50:17 -0700643}
David Howellsd0016482016-08-30 20:42:14 +0100644EXPORT_SYMBOL(rxrpc_kernel_recv_data);