blob: 6ba4af5a8d95cc6a7d558df5dd76140d076897b4 [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
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 */
136static void rxrpc_end_rx_phase(struct rxrpc_call *call)
137{
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) {
144 rxrpc_propose_ACK(call, RXRPC_ACK_IDLE, 0, 0, true, false);
145 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ACK);
146 } else {
147 rxrpc_propose_ACK(call, RXRPC_ACK_IDLE, 0, 0, false, false);
148 }
149
150 write_lock_bh(&call->state_lock);
151
152 switch (call->state) {
153 case RXRPC_CALL_CLIENT_RECV_REPLY:
154 __rxrpc_call_completed(call);
155 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;
160 break;
161 default:
162 break;
163 }
164
165 write_unlock_bh(&call->state_lock);
166}
167
168/*
169 * Discard a packet we've used up and advance the Rx window by one.
170 */
171static void rxrpc_rotate_rx_window(struct rxrpc_call *call)
172{
David Howells816c9fc2016-09-17 10:49:11 +0100173 struct rxrpc_skb_priv *sp;
David Howells248f2192016-09-08 11:10:12 +0100174 struct sk_buff *skb;
David Howells58dc63c2016-09-17 10:49:13 +0100175 rxrpc_serial_t serial;
David Howells248f2192016-09-08 11:10:12 +0100176 rxrpc_seq_t hard_ack, top;
David Howells816c9fc2016-09-17 10:49:11 +0100177 u8 flags;
David Howells248f2192016-09-08 11:10:12 +0100178 int ix;
179
180 _enter("%d", call->debug_id);
181
182 hard_ack = call->rx_hard_ack;
183 top = smp_load_acquire(&call->rx_top);
184 ASSERT(before(hard_ack, top));
185
186 hard_ack++;
187 ix = hard_ack & RXRPC_RXTX_BUFF_MASK;
188 skb = call->rxtx_buffer[ix];
David Howells71f3ca42016-09-17 10:49:14 +0100189 rxrpc_see_skb(skb, rxrpc_skb_rx_rotated);
David Howells816c9fc2016-09-17 10:49:11 +0100190 sp = rxrpc_skb(skb);
191 flags = sp->hdr.flags;
David Howells58dc63c2016-09-17 10:49:13 +0100192 serial = sp->hdr.serial;
193 if (call->rxtx_annotations[ix] & RXRPC_RX_ANNO_JUMBO)
194 serial += (call->rxtx_annotations[ix] & RXRPC_RX_ANNO_JUMBO) - 1;
195
David Howells248f2192016-09-08 11:10:12 +0100196 call->rxtx_buffer[ix] = NULL;
197 call->rxtx_annotations[ix] = 0;
198 /* Barrier against rxrpc_input_data(). */
199 smp_store_release(&call->rx_hard_ack, hard_ack);
200
David Howells71f3ca42016-09-17 10:49:14 +0100201 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
David Howells248f2192016-09-08 11:10:12 +0100202
David Howells816c9fc2016-09-17 10:49:11 +0100203 _debug("%u,%u,%02x", hard_ack, top, flags);
David Howells58dc63c2016-09-17 10:49:13 +0100204 trace_rxrpc_receive(call, rxrpc_receive_rotate, serial, hard_ack);
David Howells816c9fc2016-09-17 10:49:11 +0100205 if (flags & RXRPC_LAST_PACKET)
David Howells248f2192016-09-08 11:10:12 +0100206 rxrpc_end_rx_phase(call);
207}
208
209/*
210 * Decrypt and verify a (sub)packet. The packet's length may be changed due to
211 * padding, but if this is the case, the packet length will be resident in the
212 * socket buffer. Note that we can't modify the master skb info as the skb may
213 * be the home to multiple subpackets.
214 */
215static int rxrpc_verify_packet(struct rxrpc_call *call, struct sk_buff *skb,
216 u8 annotation,
217 unsigned int offset, unsigned int len)
218{
219 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
220 rxrpc_seq_t seq = sp->hdr.seq;
221 u16 cksum = sp->hdr.cksum;
222
223 _enter("");
224
225 /* For all but the head jumbo subpacket, the security checksum is in a
226 * jumbo header immediately prior to the data.
227 */
228 if ((annotation & RXRPC_RX_ANNO_JUMBO) > 1) {
229 __be16 tmp;
230 if (skb_copy_bits(skb, offset - 2, &tmp, 2) < 0)
231 BUG();
232 cksum = ntohs(tmp);
233 seq += (annotation & RXRPC_RX_ANNO_JUMBO) - 1;
234 }
235
236 return call->conn->security->verify_packet(call, skb, offset, len,
237 seq, cksum);
238}
239
240/*
241 * Locate the data within a packet. This is complicated by:
242 *
243 * (1) An skb may contain a jumbo packet - so we have to find the appropriate
244 * subpacket.
245 *
246 * (2) The (sub)packets may be encrypted and, if so, the encrypted portion
247 * contains an extra header which includes the true length of the data,
248 * excluding any encrypted padding.
249 */
250static int rxrpc_locate_data(struct rxrpc_call *call, struct sk_buff *skb,
251 u8 *_annotation,
252 unsigned int *_offset, unsigned int *_len)
253{
254 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
255 unsigned int offset = *_offset;
256 unsigned int len = *_len;
257 int ret;
258 u8 annotation = *_annotation;
259
David Howells248f2192016-09-08 11:10:12 +0100260 /* Locate the subpacket */
261 offset = sp->offset;
262 len = skb->len - sp->offset;
263 if ((annotation & RXRPC_RX_ANNO_JUMBO) > 0) {
264 offset += (((annotation & RXRPC_RX_ANNO_JUMBO) - 1) *
265 RXRPC_JUMBO_SUBPKTLEN);
266 len = (annotation & RXRPC_RX_ANNO_JLAST) ?
267 skb->len - offset : RXRPC_JUMBO_SUBPKTLEN;
268 }
269
270 if (!(annotation & RXRPC_RX_ANNO_VERIFIED)) {
271 ret = rxrpc_verify_packet(call, skb, annotation, offset, len);
272 if (ret < 0)
273 return ret;
274 *_annotation |= RXRPC_RX_ANNO_VERIFIED;
275 }
276
277 *_offset = offset;
278 *_len = len;
279 call->conn->security->locate_data(call, skb, _offset, _len);
280 return 0;
281}
282
283/*
284 * Deliver messages to a call. This keeps processing packets until the buffer
285 * is filled and we find either more DATA (returns 0) or the end of the DATA
286 * (returns 1). If more packets are required, it returns -EAGAIN.
287 */
288static int rxrpc_recvmsg_data(struct socket *sock, struct rxrpc_call *call,
289 struct msghdr *msg, struct iov_iter *iter,
290 size_t len, int flags, size_t *_offset)
291{
292 struct rxrpc_skb_priv *sp;
293 struct sk_buff *skb;
294 rxrpc_seq_t hard_ack, top, seq;
295 size_t remain;
296 bool last;
297 unsigned int rx_pkt_offset, rx_pkt_len;
David Howells816c9fc2016-09-17 10:49:11 +0100298 int ix, copy, ret = -EAGAIN, ret2;
David Howells248f2192016-09-08 11:10:12 +0100299
David Howells248f2192016-09-08 11:10:12 +0100300 rx_pkt_offset = call->rx_pkt_offset;
301 rx_pkt_len = call->rx_pkt_len;
302
David Howells816c9fc2016-09-17 10:49:11 +0100303 if (call->state >= RXRPC_CALL_SERVER_ACK_REQUEST) {
304 seq = call->rx_hard_ack;
305 ret = 1;
306 goto done;
307 }
308
David Howells248f2192016-09-08 11:10:12 +0100309 /* Barriers against rxrpc_input_data(). */
310 hard_ack = call->rx_hard_ack;
311 top = smp_load_acquire(&call->rx_top);
312 for (seq = hard_ack + 1; before_eq(seq, top); seq++) {
313 ix = seq & RXRPC_RXTX_BUFF_MASK;
314 skb = call->rxtx_buffer[ix];
David Howells84997902016-09-17 11:13:31 +0100315 if (!skb) {
316 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_hole, seq,
317 rx_pkt_offset, rx_pkt_len, 0);
David Howells248f2192016-09-08 11:10:12 +0100318 break;
David Howells84997902016-09-17 11:13:31 +0100319 }
David Howells248f2192016-09-08 11:10:12 +0100320 smp_rmb();
David Howells71f3ca42016-09-17 10:49:14 +0100321 rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
David Howells248f2192016-09-08 11:10:12 +0100322 sp = rxrpc_skb(skb);
323
David Howells58dc63c2016-09-17 10:49:13 +0100324 if (!(flags & MSG_PEEK))
325 trace_rxrpc_receive(call, rxrpc_receive_front,
326 sp->hdr.serial, seq);
327
David Howells248f2192016-09-08 11:10:12 +0100328 if (msg)
329 sock_recv_timestamp(msg, sock->sk, skb);
330
David Howells2e2ea512016-09-17 10:49:11 +0100331 if (rx_pkt_offset == 0) {
David Howells816c9fc2016-09-17 10:49:11 +0100332 ret2 = rxrpc_locate_data(call, skb,
333 &call->rxtx_annotations[ix],
334 &rx_pkt_offset, &rx_pkt_len);
David Howells84997902016-09-17 11:13:31 +0100335 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_next, seq,
336 rx_pkt_offset, rx_pkt_len, ret2);
David Howells816c9fc2016-09-17 10:49:11 +0100337 if (ret2 < 0) {
338 ret = ret2;
David Howells2e2ea512016-09-17 10:49:11 +0100339 goto out;
David Howells816c9fc2016-09-17 10:49:11 +0100340 }
David Howells84997902016-09-17 11:13:31 +0100341 } else {
342 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_cont, seq,
343 rx_pkt_offset, rx_pkt_len, 0);
David Howells2e2ea512016-09-17 10:49:11 +0100344 }
David Howells248f2192016-09-08 11:10:12 +0100345
346 /* We have to handle short, empty and used-up DATA packets. */
347 remain = len - *_offset;
348 copy = rx_pkt_len;
349 if (copy > remain)
350 copy = remain;
351 if (copy > 0) {
David Howells816c9fc2016-09-17 10:49:11 +0100352 ret2 = skb_copy_datagram_iter(skb, rx_pkt_offset, iter,
353 copy);
354 if (ret2 < 0) {
355 ret = ret2;
David Howells248f2192016-09-08 11:10:12 +0100356 goto out;
David Howells816c9fc2016-09-17 10:49:11 +0100357 }
David Howells248f2192016-09-08 11:10:12 +0100358
359 /* handle piecemeal consumption of data packets */
David Howells248f2192016-09-08 11:10:12 +0100360 rx_pkt_offset += copy;
361 rx_pkt_len -= copy;
362 *_offset += copy;
363 }
364
365 if (rx_pkt_len > 0) {
David Howells84997902016-09-17 11:13:31 +0100366 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_full, seq,
367 rx_pkt_offset, rx_pkt_len, 0);
David Howells248f2192016-09-08 11:10:12 +0100368 ASSERTCMP(*_offset, ==, len);
David Howells816c9fc2016-09-17 10:49:11 +0100369 ret = 0;
David Howells248f2192016-09-08 11:10:12 +0100370 break;
371 }
372
373 /* The whole packet has been transferred. */
374 last = sp->hdr.flags & RXRPC_LAST_PACKET;
375 if (!(flags & MSG_PEEK))
376 rxrpc_rotate_rx_window(call);
377 rx_pkt_offset = 0;
378 rx_pkt_len = 0;
379
David Howells816c9fc2016-09-17 10:49:11 +0100380 if (last) {
381 ASSERTCMP(seq, ==, READ_ONCE(call->rx_top));
382 ret = 1;
383 goto out;
384 }
David Howells248f2192016-09-08 11:10:12 +0100385 }
386
David Howells248f2192016-09-08 11:10:12 +0100387out:
388 if (!(flags & MSG_PEEK)) {
389 call->rx_pkt_offset = rx_pkt_offset;
390 call->rx_pkt_len = rx_pkt_len;
391 }
David Howells816c9fc2016-09-17 10:49:11 +0100392done:
David Howells84997902016-09-17 11:13:31 +0100393 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_data_return, seq,
394 rx_pkt_offset, rx_pkt_len, ret);
David Howells248f2192016-09-08 11:10:12 +0100395 return ret;
396}
397
398/*
399 * Receive a message from an RxRPC socket
David Howells17926a72007-04-26 15:48:28 -0700400 * - we need to be careful about two or more threads calling recvmsg
401 * simultaneously
402 */
Ying Xue1b784142015-03-02 15:37:48 +0800403int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
404 int flags)
David Howells17926a72007-04-26 15:48:28 -0700405{
David Howells248f2192016-09-08 11:10:12 +0100406 struct rxrpc_call *call;
David Howells17926a72007-04-26 15:48:28 -0700407 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
David Howells248f2192016-09-08 11:10:12 +0100408 struct list_head *l;
409 size_t copied = 0;
David Howells17926a72007-04-26 15:48:28 -0700410 long timeo;
David Howells248f2192016-09-08 11:10:12 +0100411 int ret;
David Howells17926a72007-04-26 15:48:28 -0700412
413 DEFINE_WAIT(wait);
414
David Howells84997902016-09-17 11:13:31 +0100415 trace_rxrpc_recvmsg(NULL, rxrpc_recvmsg_enter, 0, 0, 0, 0);
David Howells17926a72007-04-26 15:48:28 -0700416
417 if (flags & (MSG_OOB | MSG_TRUNC))
418 return -EOPNOTSUPP;
419
David Howells17926a72007-04-26 15:48:28 -0700420 timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT);
David Howells17926a72007-04-26 15:48:28 -0700421
David Howells248f2192016-09-08 11:10:12 +0100422try_again:
David Howells17926a72007-04-26 15:48:28 -0700423 lock_sock(&rx->sk);
424
David Howells248f2192016-09-08 11:10:12 +0100425 /* Return immediately if a client socket has no outstanding calls */
426 if (RB_EMPTY_ROOT(&rx->calls) &&
427 list_empty(&rx->recvmsg_q) &&
428 rx->sk.sk_state != RXRPC_SERVER_LISTENING) {
429 release_sock(&rx->sk);
430 return -ENODATA;
431 }
432
433 if (list_empty(&rx->recvmsg_q)) {
434 ret = -EWOULDBLOCK;
David Howells84997902016-09-17 11:13:31 +0100435 if (timeo == 0) {
436 call = NULL;
David Howells248f2192016-09-08 11:10:12 +0100437 goto error_no_call;
David Howells84997902016-09-17 11:13:31 +0100438 }
David Howells248f2192016-09-08 11:10:12 +0100439
440 release_sock(&rx->sk);
441
442 /* Wait for something to happen */
443 prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait,
444 TASK_INTERRUPTIBLE);
445 ret = sock_error(&rx->sk);
446 if (ret)
447 goto wait_error;
448
449 if (list_empty(&rx->recvmsg_q)) {
450 if (signal_pending(current))
451 goto wait_interrupted;
David Howells84997902016-09-17 11:13:31 +0100452 trace_rxrpc_recvmsg(NULL, rxrpc_recvmsg_wait,
453 0, 0, 0, 0);
David Howells248f2192016-09-08 11:10:12 +0100454 timeo = schedule_timeout(timeo);
David Howells17926a72007-04-26 15:48:28 -0700455 }
David Howells248f2192016-09-08 11:10:12 +0100456 finish_wait(sk_sleep(&rx->sk), &wait);
457 goto try_again;
458 }
David Howells17926a72007-04-26 15:48:28 -0700459
David Howells248f2192016-09-08 11:10:12 +0100460 /* Find the next call and dequeue it if we're not just peeking. If we
461 * do dequeue it, that comes with a ref that we will need to release.
462 */
463 write_lock_bh(&rx->recvmsg_lock);
464 l = rx->recvmsg_q.next;
465 call = list_entry(l, struct rxrpc_call, recvmsg_link);
466 if (!(flags & MSG_PEEK))
467 list_del_init(&call->recvmsg_link);
468 else
David Howellsfff72422016-09-07 14:34:21 +0100469 rxrpc_get_call(call, rxrpc_call_got);
David Howells248f2192016-09-08 11:10:12 +0100470 write_unlock_bh(&rx->recvmsg_lock);
David Howells17926a72007-04-26 15:48:28 -0700471
David Howells84997902016-09-17 11:13:31 +0100472 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_dequeue, 0, 0, 0, 0);
David Howells17926a72007-04-26 15:48:28 -0700473
David Howells248f2192016-09-08 11:10:12 +0100474 if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
David Howells17926a72007-04-26 15:48:28 -0700475 BUG();
David Howells248f2192016-09-08 11:10:12 +0100476
477 if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
478 if (flags & MSG_CMSG_COMPAT) {
479 unsigned int id32 = call->user_call_ID;
480
481 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
482 sizeof(unsigned int), &id32);
483 } else {
484 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
485 sizeof(unsigned long),
486 &call->user_call_ID);
David Howellsf5c17aa2016-08-30 09:49:28 +0100487 }
David Howells248f2192016-09-08 11:10:12 +0100488 if (ret < 0)
489 goto error;
490 }
491
492 if (msg->msg_name) {
493 size_t len = sizeof(call->conn->params.peer->srx);
494 memcpy(msg->msg_name, &call->conn->params.peer->srx, len);
495 msg->msg_namelen = len;
496 }
497
498 switch (call->state) {
499 case RXRPC_CALL_SERVER_ACCEPTING:
500 ret = rxrpc_recvmsg_new_call(rx, call, msg, flags);
David Howells17926a72007-04-26 15:48:28 -0700501 break;
David Howells248f2192016-09-08 11:10:12 +0100502 case RXRPC_CALL_CLIENT_RECV_REPLY:
503 case RXRPC_CALL_SERVER_RECV_REQUEST:
504 case RXRPC_CALL_SERVER_ACK_REQUEST:
505 ret = rxrpc_recvmsg_data(sock, call, msg, &msg->msg_iter, len,
506 flags, &copied);
507 if (ret == -EAGAIN)
508 ret = 0;
David Howells33b603f2016-09-13 22:36:21 +0100509
510 if (after(call->rx_top, call->rx_hard_ack) &&
511 call->rxtx_buffer[(call->rx_hard_ack + 1) & RXRPC_RXTX_BUFF_MASK])
512 rxrpc_notify_socket(call);
David Howells17926a72007-04-26 15:48:28 -0700513 break;
514 default:
David Howells248f2192016-09-08 11:10:12 +0100515 ret = 0;
David Howells17926a72007-04-26 15:48:28 -0700516 break;
517 }
518
519 if (ret < 0)
David Howells248f2192016-09-08 11:10:12 +0100520 goto error;
David Howells17926a72007-04-26 15:48:28 -0700521
David Howells248f2192016-09-08 11:10:12 +0100522 if (call->state == RXRPC_CALL_COMPLETE) {
523 ret = rxrpc_recvmsg_term(call, msg);
524 if (ret < 0)
525 goto error;
526 if (!(flags & MSG_PEEK))
527 rxrpc_release_call(rx, call);
528 msg->msg_flags |= MSG_EOR;
529 ret = 1;
David Howells17926a72007-04-26 15:48:28 -0700530 }
531
David Howells248f2192016-09-08 11:10:12 +0100532 if (ret == 0)
533 msg->msg_flags |= MSG_MORE;
534 else
535 msg->msg_flags &= ~MSG_MORE;
536 ret = copied;
David Howells17926a72007-04-26 15:48:28 -0700537
David Howells248f2192016-09-08 11:10:12 +0100538error:
David Howellsfff72422016-09-07 14:34:21 +0100539 rxrpc_put_call(call, rxrpc_call_put);
David Howells248f2192016-09-08 11:10:12 +0100540error_no_call:
541 release_sock(&rx->sk);
David Howells84997902016-09-17 11:13:31 +0100542 trace_rxrpc_recvmsg(call, rxrpc_recvmsg_return, 0, 0, 0, ret);
David Howells17926a72007-04-26 15:48:28 -0700543 return ret;
544
David Howells17926a72007-04-26 15:48:28 -0700545wait_interrupted:
546 ret = sock_intr_errno(timeo);
547wait_error:
Eric Dumazet4a4771a2010-04-25 22:20:06 +0000548 finish_wait(sk_sleep(&rx->sk), &wait);
David Howells84997902016-09-17 11:13:31 +0100549 call = NULL;
550 goto error_no_call;
David Howellsd0016482016-08-30 20:42:14 +0100551}
David Howells651350d2007-04-26 15:50:17 -0700552
553/**
David Howellsd0016482016-08-30 20:42:14 +0100554 * rxrpc_kernel_recv_data - Allow a kernel service to receive data/info
555 * @sock: The socket that the call exists on
556 * @call: The call to send data through
557 * @buf: The buffer to receive into
558 * @size: The size of the buffer, including data already read
559 * @_offset: The running offset into the buffer.
560 * @want_more: True if more data is expected to be read
561 * @_abort: Where the abort code is stored if -ECONNABORTED is returned
David Howells651350d2007-04-26 15:50:17 -0700562 *
David Howellsd0016482016-08-30 20:42:14 +0100563 * Allow a kernel service to receive data and pick up information about the
564 * state of a call. Returns 0 if got what was asked for and there's more
565 * available, 1 if we got what was asked for and we're at the end of the data
566 * and -EAGAIN if we need more data.
567 *
568 * Note that we may return -EAGAIN to drain empty packets at the end of the
569 * data, even if we've already copied over the requested data.
570 *
571 * This function adds the amount it transfers to *_offset, so this should be
572 * precleared as appropriate. Note that the amount remaining in the buffer is
573 * taken to be size - *_offset.
574 *
575 * *_abort should also be initialised to 0.
David Howells651350d2007-04-26 15:50:17 -0700576 */
David Howellsd0016482016-08-30 20:42:14 +0100577int rxrpc_kernel_recv_data(struct socket *sock, struct rxrpc_call *call,
578 void *buf, size_t size, size_t *_offset,
579 bool want_more, u32 *_abort)
David Howells651350d2007-04-26 15:50:17 -0700580{
David Howellsd0016482016-08-30 20:42:14 +0100581 struct iov_iter iter;
582 struct kvec iov;
583 int ret;
David Howells651350d2007-04-26 15:50:17 -0700584
David Howells248f2192016-09-08 11:10:12 +0100585 _enter("{%d,%s},%zu/%zu,%d",
586 call->debug_id, rxrpc_call_states[call->state],
587 *_offset, size, want_more);
David Howellsd0016482016-08-30 20:42:14 +0100588
589 ASSERTCMP(*_offset, <=, size);
590 ASSERTCMP(call->state, !=, RXRPC_CALL_SERVER_ACCEPTING);
591
592 iov.iov_base = buf + *_offset;
593 iov.iov_len = size - *_offset;
594 iov_iter_kvec(&iter, ITER_KVEC | READ, &iov, 1, size - *_offset);
595
596 lock_sock(sock->sk);
597
598 switch (call->state) {
599 case RXRPC_CALL_CLIENT_RECV_REPLY:
600 case RXRPC_CALL_SERVER_RECV_REQUEST:
601 case RXRPC_CALL_SERVER_ACK_REQUEST:
David Howells248f2192016-09-08 11:10:12 +0100602 ret = rxrpc_recvmsg_data(sock, call, NULL, &iter, size, 0,
603 _offset);
David Howellsd0016482016-08-30 20:42:14 +0100604 if (ret < 0)
605 goto out;
606
607 /* We can only reach here with a partially full buffer if we
608 * have reached the end of the data. We must otherwise have a
609 * full buffer or have been given -EAGAIN.
610 */
611 if (ret == 1) {
612 if (*_offset < size)
613 goto short_data;
614 if (!want_more)
615 goto read_phase_complete;
616 ret = 0;
617 goto out;
618 }
619
620 if (!want_more)
621 goto excess_data;
622 goto out;
623
624 case RXRPC_CALL_COMPLETE:
625 goto call_complete;
626
627 default:
David Howellsd0016482016-08-30 20:42:14 +0100628 ret = -EINPROGRESS;
629 goto out;
630 }
631
632read_phase_complete:
633 ret = 1;
634out:
635 release_sock(sock->sk);
636 _leave(" = %d [%zu,%d]", ret, *_offset, *_abort);
637 return ret;
638
639short_data:
640 ret = -EBADMSG;
641 goto out;
642excess_data:
643 ret = -EMSGSIZE;
644 goto out;
645call_complete:
646 *_abort = call->abort_code;
647 ret = call->error;
648 if (call->completion == RXRPC_CALL_SUCCEEDED) {
649 ret = 1;
650 if (size > 0)
651 ret = -ECONNRESET;
652 }
653 goto out;
David Howells651350d2007-04-26 15:50:17 -0700654}
David Howellsd0016482016-08-30 20:42:14 +0100655EXPORT_SYMBOL(rxrpc_kernel_recv_data);