blob: 8b8d7e14f8004655e94c28be0e71bd69cad3040b [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 Howells816c9fc2016-09-17 10:49:11 +0100137 ASSERTCMP(call->rx_hard_ack, ==, call->rx_top);
138
David Howells248f2192016-09-08 11:10:12 +0100139 if (call->state == RXRPC_CALL_CLIENT_RECV_REPLY) {
140 rxrpc_propose_ACK(call, RXRPC_ACK_IDLE, 0, 0, true, false);
141 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ACK);
142 } else {
143 rxrpc_propose_ACK(call, RXRPC_ACK_IDLE, 0, 0, false, false);
144 }
145
146 write_lock_bh(&call->state_lock);
147
148 switch (call->state) {
149 case RXRPC_CALL_CLIENT_RECV_REPLY:
150 __rxrpc_call_completed(call);
151 break;
152
153 case RXRPC_CALL_SERVER_RECV_REQUEST:
154 call->state = RXRPC_CALL_SERVER_ACK_REQUEST;
155 break;
156 default:
157 break;
158 }
159
160 write_unlock_bh(&call->state_lock);
161}
162
163/*
164 * Discard a packet we've used up and advance the Rx window by one.
165 */
166static void rxrpc_rotate_rx_window(struct rxrpc_call *call)
167{
David Howells816c9fc2016-09-17 10:49:11 +0100168 struct rxrpc_skb_priv *sp;
David Howells248f2192016-09-08 11:10:12 +0100169 struct sk_buff *skb;
170 rxrpc_seq_t hard_ack, top;
David Howells816c9fc2016-09-17 10:49:11 +0100171 u8 flags;
David Howells248f2192016-09-08 11:10:12 +0100172 int ix;
173
174 _enter("%d", call->debug_id);
175
176 hard_ack = call->rx_hard_ack;
177 top = smp_load_acquire(&call->rx_top);
178 ASSERT(before(hard_ack, top));
179
180 hard_ack++;
181 ix = hard_ack & RXRPC_RXTX_BUFF_MASK;
182 skb = call->rxtx_buffer[ix];
183 rxrpc_see_skb(skb);
David Howells816c9fc2016-09-17 10:49:11 +0100184 sp = rxrpc_skb(skb);
185 flags = sp->hdr.flags;
David Howells248f2192016-09-08 11:10:12 +0100186 call->rxtx_buffer[ix] = NULL;
187 call->rxtx_annotations[ix] = 0;
188 /* Barrier against rxrpc_input_data(). */
189 smp_store_release(&call->rx_hard_ack, hard_ack);
190
191 rxrpc_free_skb(skb);
192
David Howells816c9fc2016-09-17 10:49:11 +0100193 _debug("%u,%u,%02x", hard_ack, top, flags);
194 if (flags & RXRPC_LAST_PACKET)
David Howells248f2192016-09-08 11:10:12 +0100195 rxrpc_end_rx_phase(call);
196}
197
198/*
199 * Decrypt and verify a (sub)packet. The packet's length may be changed due to
200 * padding, but if this is the case, the packet length will be resident in the
201 * socket buffer. Note that we can't modify the master skb info as the skb may
202 * be the home to multiple subpackets.
203 */
204static int rxrpc_verify_packet(struct rxrpc_call *call, struct sk_buff *skb,
205 u8 annotation,
206 unsigned int offset, unsigned int len)
207{
208 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
209 rxrpc_seq_t seq = sp->hdr.seq;
210 u16 cksum = sp->hdr.cksum;
211
212 _enter("");
213
214 /* For all but the head jumbo subpacket, the security checksum is in a
215 * jumbo header immediately prior to the data.
216 */
217 if ((annotation & RXRPC_RX_ANNO_JUMBO) > 1) {
218 __be16 tmp;
219 if (skb_copy_bits(skb, offset - 2, &tmp, 2) < 0)
220 BUG();
221 cksum = ntohs(tmp);
222 seq += (annotation & RXRPC_RX_ANNO_JUMBO) - 1;
223 }
224
225 return call->conn->security->verify_packet(call, skb, offset, len,
226 seq, cksum);
227}
228
229/*
230 * Locate the data within a packet. This is complicated by:
231 *
232 * (1) An skb may contain a jumbo packet - so we have to find the appropriate
233 * subpacket.
234 *
235 * (2) The (sub)packets may be encrypted and, if so, the encrypted portion
236 * contains an extra header which includes the true length of the data,
237 * excluding any encrypted padding.
238 */
239static int rxrpc_locate_data(struct rxrpc_call *call, struct sk_buff *skb,
240 u8 *_annotation,
241 unsigned int *_offset, unsigned int *_len)
242{
243 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
244 unsigned int offset = *_offset;
245 unsigned int len = *_len;
246 int ret;
247 u8 annotation = *_annotation;
248
David Howells248f2192016-09-08 11:10:12 +0100249 /* Locate the subpacket */
250 offset = sp->offset;
251 len = skb->len - sp->offset;
252 if ((annotation & RXRPC_RX_ANNO_JUMBO) > 0) {
253 offset += (((annotation & RXRPC_RX_ANNO_JUMBO) - 1) *
254 RXRPC_JUMBO_SUBPKTLEN);
255 len = (annotation & RXRPC_RX_ANNO_JLAST) ?
256 skb->len - offset : RXRPC_JUMBO_SUBPKTLEN;
257 }
258
259 if (!(annotation & RXRPC_RX_ANNO_VERIFIED)) {
260 ret = rxrpc_verify_packet(call, skb, annotation, offset, len);
261 if (ret < 0)
262 return ret;
263 *_annotation |= RXRPC_RX_ANNO_VERIFIED;
264 }
265
266 *_offset = offset;
267 *_len = len;
268 call->conn->security->locate_data(call, skb, _offset, _len);
269 return 0;
270}
271
272/*
273 * Deliver messages to a call. This keeps processing packets until the buffer
274 * is filled and we find either more DATA (returns 0) or the end of the DATA
275 * (returns 1). If more packets are required, it returns -EAGAIN.
276 */
277static int rxrpc_recvmsg_data(struct socket *sock, struct rxrpc_call *call,
278 struct msghdr *msg, struct iov_iter *iter,
279 size_t len, int flags, size_t *_offset)
280{
281 struct rxrpc_skb_priv *sp;
282 struct sk_buff *skb;
283 rxrpc_seq_t hard_ack, top, seq;
284 size_t remain;
285 bool last;
286 unsigned int rx_pkt_offset, rx_pkt_len;
David Howells816c9fc2016-09-17 10:49:11 +0100287 int ix, copy, ret = -EAGAIN, ret2;
David Howells248f2192016-09-08 11:10:12 +0100288
289 _enter("");
290
291 rx_pkt_offset = call->rx_pkt_offset;
292 rx_pkt_len = call->rx_pkt_len;
293
David Howells816c9fc2016-09-17 10:49:11 +0100294 if (call->state >= RXRPC_CALL_SERVER_ACK_REQUEST) {
295 seq = call->rx_hard_ack;
296 ret = 1;
297 goto done;
298 }
299
David Howells248f2192016-09-08 11:10:12 +0100300 /* Barriers against rxrpc_input_data(). */
301 hard_ack = call->rx_hard_ack;
302 top = smp_load_acquire(&call->rx_top);
303 for (seq = hard_ack + 1; before_eq(seq, top); seq++) {
304 ix = seq & RXRPC_RXTX_BUFF_MASK;
305 skb = call->rxtx_buffer[ix];
306 if (!skb)
307 break;
308 smp_rmb();
309 rxrpc_see_skb(skb);
310 sp = rxrpc_skb(skb);
311
312 if (msg)
313 sock_recv_timestamp(msg, sock->sk, skb);
314
David Howells2e2ea512016-09-17 10:49:11 +0100315 if (rx_pkt_offset == 0) {
David Howells816c9fc2016-09-17 10:49:11 +0100316 ret2 = rxrpc_locate_data(call, skb,
317 &call->rxtx_annotations[ix],
318 &rx_pkt_offset, &rx_pkt_len);
319 if (ret2 < 0) {
320 ret = ret2;
David Howells2e2ea512016-09-17 10:49:11 +0100321 goto out;
David Howells816c9fc2016-09-17 10:49:11 +0100322 }
David Howells2e2ea512016-09-17 10:49:11 +0100323 }
David Howells248f2192016-09-08 11:10:12 +0100324 _debug("recvmsg %x DATA #%u { %d, %d }",
325 sp->hdr.callNumber, seq, rx_pkt_offset, rx_pkt_len);
326
327 /* We have to handle short, empty and used-up DATA packets. */
328 remain = len - *_offset;
329 copy = rx_pkt_len;
330 if (copy > remain)
331 copy = remain;
332 if (copy > 0) {
David Howells816c9fc2016-09-17 10:49:11 +0100333 ret2 = skb_copy_datagram_iter(skb, rx_pkt_offset, iter,
334 copy);
335 if (ret2 < 0) {
336 ret = ret2;
David Howells248f2192016-09-08 11:10:12 +0100337 goto out;
David Howells816c9fc2016-09-17 10:49:11 +0100338 }
David Howells248f2192016-09-08 11:10:12 +0100339
340 /* handle piecemeal consumption of data packets */
341 _debug("copied %d @%zu", copy, *_offset);
342
343 rx_pkt_offset += copy;
344 rx_pkt_len -= copy;
345 *_offset += copy;
346 }
347
348 if (rx_pkt_len > 0) {
349 _debug("buffer full");
350 ASSERTCMP(*_offset, ==, len);
David Howells816c9fc2016-09-17 10:49:11 +0100351 ret = 0;
David Howells248f2192016-09-08 11:10:12 +0100352 break;
353 }
354
355 /* The whole packet has been transferred. */
356 last = sp->hdr.flags & RXRPC_LAST_PACKET;
357 if (!(flags & MSG_PEEK))
358 rxrpc_rotate_rx_window(call);
359 rx_pkt_offset = 0;
360 rx_pkt_len = 0;
361
David Howells816c9fc2016-09-17 10:49:11 +0100362 if (last) {
363 ASSERTCMP(seq, ==, READ_ONCE(call->rx_top));
364 ret = 1;
365 goto out;
366 }
David Howells248f2192016-09-08 11:10:12 +0100367 }
368
David Howells248f2192016-09-08 11:10:12 +0100369out:
370 if (!(flags & MSG_PEEK)) {
371 call->rx_pkt_offset = rx_pkt_offset;
372 call->rx_pkt_len = rx_pkt_len;
373 }
David Howells816c9fc2016-09-17 10:49:11 +0100374done:
David Howells248f2192016-09-08 11:10:12 +0100375 _leave(" = %d [%u/%u]", ret, seq, top);
376 return ret;
377}
378
379/*
380 * Receive a message from an RxRPC socket
David Howells17926a72007-04-26 15:48:28 -0700381 * - we need to be careful about two or more threads calling recvmsg
382 * simultaneously
383 */
Ying Xue1b784142015-03-02 15:37:48 +0800384int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
385 int flags)
David Howells17926a72007-04-26 15:48:28 -0700386{
David Howells248f2192016-09-08 11:10:12 +0100387 struct rxrpc_call *call;
David Howells17926a72007-04-26 15:48:28 -0700388 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
David Howells248f2192016-09-08 11:10:12 +0100389 struct list_head *l;
390 size_t copied = 0;
David Howells17926a72007-04-26 15:48:28 -0700391 long timeo;
David Howells248f2192016-09-08 11:10:12 +0100392 int ret;
David Howells17926a72007-04-26 15:48:28 -0700393
394 DEFINE_WAIT(wait);
395
396 _enter(",,,%zu,%d", len, flags);
397
398 if (flags & (MSG_OOB | MSG_TRUNC))
399 return -EOPNOTSUPP;
400
David Howells17926a72007-04-26 15:48:28 -0700401 timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT);
David Howells17926a72007-04-26 15:48:28 -0700402
David Howells248f2192016-09-08 11:10:12 +0100403try_again:
David Howells17926a72007-04-26 15:48:28 -0700404 lock_sock(&rx->sk);
405
David Howells248f2192016-09-08 11:10:12 +0100406 /* Return immediately if a client socket has no outstanding calls */
407 if (RB_EMPTY_ROOT(&rx->calls) &&
408 list_empty(&rx->recvmsg_q) &&
409 rx->sk.sk_state != RXRPC_SERVER_LISTENING) {
410 release_sock(&rx->sk);
411 return -ENODATA;
412 }
413
414 if (list_empty(&rx->recvmsg_q)) {
415 ret = -EWOULDBLOCK;
416 if (timeo == 0)
417 goto error_no_call;
418
419 release_sock(&rx->sk);
420
421 /* Wait for something to happen */
422 prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait,
423 TASK_INTERRUPTIBLE);
424 ret = sock_error(&rx->sk);
425 if (ret)
426 goto wait_error;
427
428 if (list_empty(&rx->recvmsg_q)) {
429 if (signal_pending(current))
430 goto wait_interrupted;
431 timeo = schedule_timeout(timeo);
David Howells17926a72007-04-26 15:48:28 -0700432 }
David Howells248f2192016-09-08 11:10:12 +0100433 finish_wait(sk_sleep(&rx->sk), &wait);
434 goto try_again;
435 }
David Howells17926a72007-04-26 15:48:28 -0700436
David Howells248f2192016-09-08 11:10:12 +0100437 /* Find the next call and dequeue it if we're not just peeking. If we
438 * do dequeue it, that comes with a ref that we will need to release.
439 */
440 write_lock_bh(&rx->recvmsg_lock);
441 l = rx->recvmsg_q.next;
442 call = list_entry(l, struct rxrpc_call, recvmsg_link);
443 if (!(flags & MSG_PEEK))
444 list_del_init(&call->recvmsg_link);
445 else
David Howellsfff724292016-09-07 14:34:21 +0100446 rxrpc_get_call(call, rxrpc_call_got);
David Howells248f2192016-09-08 11:10:12 +0100447 write_unlock_bh(&rx->recvmsg_lock);
David Howells17926a72007-04-26 15:48:28 -0700448
David Howells248f2192016-09-08 11:10:12 +0100449 _debug("recvmsg call %p", call);
David Howells17926a72007-04-26 15:48:28 -0700450
David Howells248f2192016-09-08 11:10:12 +0100451 if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
David Howells17926a72007-04-26 15:48:28 -0700452 BUG();
David Howells248f2192016-09-08 11:10:12 +0100453
454 if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
455 if (flags & MSG_CMSG_COMPAT) {
456 unsigned int id32 = call->user_call_ID;
457
458 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
459 sizeof(unsigned int), &id32);
460 } else {
461 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
462 sizeof(unsigned long),
463 &call->user_call_ID);
David Howellsf5c17aa2016-08-30 09:49:28 +0100464 }
David Howells248f2192016-09-08 11:10:12 +0100465 if (ret < 0)
466 goto error;
467 }
468
469 if (msg->msg_name) {
470 size_t len = sizeof(call->conn->params.peer->srx);
471 memcpy(msg->msg_name, &call->conn->params.peer->srx, len);
472 msg->msg_namelen = len;
473 }
474
475 switch (call->state) {
476 case RXRPC_CALL_SERVER_ACCEPTING:
477 ret = rxrpc_recvmsg_new_call(rx, call, msg, flags);
David Howells17926a72007-04-26 15:48:28 -0700478 break;
David Howells248f2192016-09-08 11:10:12 +0100479 case RXRPC_CALL_CLIENT_RECV_REPLY:
480 case RXRPC_CALL_SERVER_RECV_REQUEST:
481 case RXRPC_CALL_SERVER_ACK_REQUEST:
482 ret = rxrpc_recvmsg_data(sock, call, msg, &msg->msg_iter, len,
483 flags, &copied);
484 if (ret == -EAGAIN)
485 ret = 0;
David Howells33b603f2016-09-13 22:36:21 +0100486
487 if (after(call->rx_top, call->rx_hard_ack) &&
488 call->rxtx_buffer[(call->rx_hard_ack + 1) & RXRPC_RXTX_BUFF_MASK])
489 rxrpc_notify_socket(call);
David Howells17926a72007-04-26 15:48:28 -0700490 break;
491 default:
David Howells248f2192016-09-08 11:10:12 +0100492 ret = 0;
David Howells17926a72007-04-26 15:48:28 -0700493 break;
494 }
495
496 if (ret < 0)
David Howells248f2192016-09-08 11:10:12 +0100497 goto error;
David Howells17926a72007-04-26 15:48:28 -0700498
David Howells248f2192016-09-08 11:10:12 +0100499 if (call->state == RXRPC_CALL_COMPLETE) {
500 ret = rxrpc_recvmsg_term(call, msg);
501 if (ret < 0)
502 goto error;
503 if (!(flags & MSG_PEEK))
504 rxrpc_release_call(rx, call);
505 msg->msg_flags |= MSG_EOR;
506 ret = 1;
David Howells17926a72007-04-26 15:48:28 -0700507 }
508
David Howells248f2192016-09-08 11:10:12 +0100509 if (ret == 0)
510 msg->msg_flags |= MSG_MORE;
511 else
512 msg->msg_flags &= ~MSG_MORE;
513 ret = copied;
David Howells17926a72007-04-26 15:48:28 -0700514
David Howells248f2192016-09-08 11:10:12 +0100515error:
David Howellsfff724292016-09-07 14:34:21 +0100516 rxrpc_put_call(call, rxrpc_call_put);
David Howells248f2192016-09-08 11:10:12 +0100517error_no_call:
518 release_sock(&rx->sk);
David Howells17926a72007-04-26 15:48:28 -0700519 _leave(" = %d", ret);
520 return ret;
521
David Howells17926a72007-04-26 15:48:28 -0700522wait_interrupted:
523 ret = sock_intr_errno(timeo);
524wait_error:
Eric Dumazet4a4771a2010-04-25 22:20:06 +0000525 finish_wait(sk_sleep(&rx->sk), &wait);
David Howells248f2192016-09-08 11:10:12 +0100526 release_sock(&rx->sk);
527 _leave(" = %d [wait]", ret);
David Howellsd0016482016-08-30 20:42:14 +0100528 return ret;
529}
David Howells651350d2007-04-26 15:50:17 -0700530
531/**
David Howellsd0016482016-08-30 20:42:14 +0100532 * rxrpc_kernel_recv_data - Allow a kernel service to receive data/info
533 * @sock: The socket that the call exists on
534 * @call: The call to send data through
535 * @buf: The buffer to receive into
536 * @size: The size of the buffer, including data already read
537 * @_offset: The running offset into the buffer.
538 * @want_more: True if more data is expected to be read
539 * @_abort: Where the abort code is stored if -ECONNABORTED is returned
David Howells651350d2007-04-26 15:50:17 -0700540 *
David Howellsd0016482016-08-30 20:42:14 +0100541 * Allow a kernel service to receive data and pick up information about the
542 * state of a call. Returns 0 if got what was asked for and there's more
543 * available, 1 if we got what was asked for and we're at the end of the data
544 * and -EAGAIN if we need more data.
545 *
546 * Note that we may return -EAGAIN to drain empty packets at the end of the
547 * data, even if we've already copied over the requested data.
548 *
549 * This function adds the amount it transfers to *_offset, so this should be
550 * precleared as appropriate. Note that the amount remaining in the buffer is
551 * taken to be size - *_offset.
552 *
553 * *_abort should also be initialised to 0.
David Howells651350d2007-04-26 15:50:17 -0700554 */
David Howellsd0016482016-08-30 20:42:14 +0100555int rxrpc_kernel_recv_data(struct socket *sock, struct rxrpc_call *call,
556 void *buf, size_t size, size_t *_offset,
557 bool want_more, u32 *_abort)
David Howells651350d2007-04-26 15:50:17 -0700558{
David Howellsd0016482016-08-30 20:42:14 +0100559 struct iov_iter iter;
560 struct kvec iov;
561 int ret;
David Howells651350d2007-04-26 15:50:17 -0700562
David Howells248f2192016-09-08 11:10:12 +0100563 _enter("{%d,%s},%zu/%zu,%d",
564 call->debug_id, rxrpc_call_states[call->state],
565 *_offset, size, want_more);
David Howellsd0016482016-08-30 20:42:14 +0100566
567 ASSERTCMP(*_offset, <=, size);
568 ASSERTCMP(call->state, !=, RXRPC_CALL_SERVER_ACCEPTING);
569
570 iov.iov_base = buf + *_offset;
571 iov.iov_len = size - *_offset;
572 iov_iter_kvec(&iter, ITER_KVEC | READ, &iov, 1, size - *_offset);
573
574 lock_sock(sock->sk);
575
576 switch (call->state) {
577 case RXRPC_CALL_CLIENT_RECV_REPLY:
578 case RXRPC_CALL_SERVER_RECV_REQUEST:
579 case RXRPC_CALL_SERVER_ACK_REQUEST:
David Howells248f2192016-09-08 11:10:12 +0100580 ret = rxrpc_recvmsg_data(sock, call, NULL, &iter, size, 0,
581 _offset);
David Howellsd0016482016-08-30 20:42:14 +0100582 if (ret < 0)
583 goto out;
584
585 /* We can only reach here with a partially full buffer if we
586 * have reached the end of the data. We must otherwise have a
587 * full buffer or have been given -EAGAIN.
588 */
589 if (ret == 1) {
590 if (*_offset < size)
591 goto short_data;
592 if (!want_more)
593 goto read_phase_complete;
594 ret = 0;
595 goto out;
596 }
597
598 if (!want_more)
599 goto excess_data;
600 goto out;
601
602 case RXRPC_CALL_COMPLETE:
603 goto call_complete;
604
605 default:
David Howellsd0016482016-08-30 20:42:14 +0100606 ret = -EINPROGRESS;
607 goto out;
608 }
609
610read_phase_complete:
611 ret = 1;
612out:
613 release_sock(sock->sk);
614 _leave(" = %d [%zu,%d]", ret, *_offset, *_abort);
615 return ret;
616
617short_data:
618 ret = -EBADMSG;
619 goto out;
620excess_data:
621 ret = -EMSGSIZE;
622 goto out;
623call_complete:
624 *_abort = call->abort_code;
625 ret = call->error;
626 if (call->completion == RXRPC_CALL_SUCCEEDED) {
627 ret = 1;
628 if (size > 0)
629 ret = -ECONNRESET;
630 }
631 goto out;
David Howells651350d2007-04-26 15:50:17 -0700632}
David Howellsd0016482016-08-30 20:42:14 +0100633EXPORT_SYMBOL(rxrpc_kernel_recv_data);