]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/rxrpc/recvmsg.c
rxrpc: Check the return value of rxrpc_locate_data()
[mirror_ubuntu-bionic-kernel.git] / net / rxrpc / recvmsg.c
CommitLineData
17926a79
DH
1/* 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
9b6d5398
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
17926a79
DH
14#include <linux/net.h>
15#include <linux/skbuff.h>
bc3b2d7f 16#include <linux/export.h>
17926a79
DH
17#include <net/sock.h>
18#include <net/af_rxrpc.h>
19#include "ar-internal.h"
20
17926a79 21/*
248f219c
DH
22 * Post a call for attention by the socket or kernel service. Further
23 * notifications are suppressed by putting recvmsg_link on a dummy queue.
17926a79 24 */
248f219c 25void rxrpc_notify_socket(struct rxrpc_call *call)
17926a79 26{
248f219c
DH
27 struct rxrpc_sock *rx;
28 struct sock *sk;
17926a79 29
248f219c 30 _enter("%d", call->debug_id);
17926a79 31
248f219c
DH
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);
17926a79 49
248f219c
DH
50 if (!sock_flag(sk, SOCK_DEAD)) {
51 _debug("call %ps", sk->sk_data_ready);
52 sk->sk_data_ready(sk);
53 }
54 }
55 }
17926a79 56
248f219c
DH
57 rcu_read_unlock();
58 _leave("");
59}
17926a79 60
248f219c
DH
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;
17926a79 68
248f219c
DH
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 }
17926a79 96
248f219c
DH
97 return ret;
98}
17926a79 99
248f219c
DH
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;
17926a79 112
248f219c 113 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NEW_CALL, 0, &tmp);
17926a79 114
248f219c
DH
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);
17926a79 120
3432a757 121 rxrpc_get_call(call, rxrpc_call_got);
248f219c
DH
122 write_lock(&rx->call_lock);
123 list_add_tail(&call->accept_link, &rx->to_be_accepted);
124 write_unlock(&rx->call_lock);
125 }
17926a79 126
248f219c
DH
127 return ret;
128}
17926a79 129
248f219c
DH
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]);
17926a79 136
248f219c
DH
137 if (call->state == RXRPC_CALL_CLIENT_RECV_REPLY) {
138 rxrpc_propose_ACK(call, RXRPC_ACK_IDLE, 0, 0, true, false);
139 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ACK);
140 } else {
141 rxrpc_propose_ACK(call, RXRPC_ACK_IDLE, 0, 0, false, false);
142 }
17926a79 143
248f219c 144 write_lock_bh(&call->state_lock);
17926a79 145
248f219c
DH
146 switch (call->state) {
147 case RXRPC_CALL_CLIENT_RECV_REPLY:
148 __rxrpc_call_completed(call);
149 break;
17926a79 150
248f219c
DH
151 case RXRPC_CALL_SERVER_RECV_REQUEST:
152 call->state = RXRPC_CALL_SERVER_ACK_REQUEST;
153 break;
154 default:
155 break;
156 }
17926a79 157
248f219c
DH
158 write_unlock_bh(&call->state_lock);
159}
17926a79 160
248f219c
DH
161/*
162 * Discard a packet we've used up and advance the Rx window by one.
163 */
164static void rxrpc_rotate_rx_window(struct rxrpc_call *call)
165{
166 struct sk_buff *skb;
167 rxrpc_seq_t hard_ack, top;
168 int ix;
169
170 _enter("%d", call->debug_id);
171
172 hard_ack = call->rx_hard_ack;
173 top = smp_load_acquire(&call->rx_top);
174 ASSERT(before(hard_ack, top));
175
176 hard_ack++;
177 ix = hard_ack & RXRPC_RXTX_BUFF_MASK;
178 skb = call->rxtx_buffer[ix];
179 rxrpc_see_skb(skb);
180 call->rxtx_buffer[ix] = NULL;
181 call->rxtx_annotations[ix] = 0;
182 /* Barrier against rxrpc_input_data(). */
183 smp_store_release(&call->rx_hard_ack, hard_ack);
17926a79 184
248f219c 185 rxrpc_free_skb(skb);
17926a79 186
248f219c
DH
187 _debug("%u,%u,%lx", hard_ack, top, call->flags);
188 if (hard_ack == top && test_bit(RXRPC_CALL_RX_LAST, &call->flags))
189 rxrpc_end_rx_phase(call);
190}
191
192/*
193 * Decrypt and verify a (sub)packet. The packet's length may be changed due to
194 * padding, but if this is the case, the packet length will be resident in the
195 * socket buffer. Note that we can't modify the master skb info as the skb may
196 * be the home to multiple subpackets.
197 */
198static int rxrpc_verify_packet(struct rxrpc_call *call, struct sk_buff *skb,
199 u8 annotation,
200 unsigned int offset, unsigned int len)
201{
202 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
203 rxrpc_seq_t seq = sp->hdr.seq;
204 u16 cksum = sp->hdr.cksum;
205
206 _enter("");
207
208 /* For all but the head jumbo subpacket, the security checksum is in a
209 * jumbo header immediately prior to the data.
210 */
211 if ((annotation & RXRPC_RX_ANNO_JUMBO) > 1) {
212 __be16 tmp;
213 if (skb_copy_bits(skb, offset - 2, &tmp, 2) < 0)
214 BUG();
215 cksum = ntohs(tmp);
216 seq += (annotation & RXRPC_RX_ANNO_JUMBO) - 1;
217 }
218
219 return call->conn->security->verify_packet(call, skb, offset, len,
220 seq, cksum);
221}
222
223/*
224 * Locate the data within a packet. This is complicated by:
225 *
226 * (1) An skb may contain a jumbo packet - so we have to find the appropriate
227 * subpacket.
228 *
229 * (2) The (sub)packets may be encrypted and, if so, the encrypted portion
230 * contains an extra header which includes the true length of the data,
231 * excluding any encrypted padding.
232 */
233static int rxrpc_locate_data(struct rxrpc_call *call, struct sk_buff *skb,
234 u8 *_annotation,
235 unsigned int *_offset, unsigned int *_len)
236{
237 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
238 unsigned int offset = *_offset;
239 unsigned int len = *_len;
240 int ret;
241 u8 annotation = *_annotation;
242
248f219c
DH
243 /* Locate the subpacket */
244 offset = sp->offset;
245 len = skb->len - sp->offset;
246 if ((annotation & RXRPC_RX_ANNO_JUMBO) > 0) {
247 offset += (((annotation & RXRPC_RX_ANNO_JUMBO) - 1) *
248 RXRPC_JUMBO_SUBPKTLEN);
249 len = (annotation & RXRPC_RX_ANNO_JLAST) ?
250 skb->len - offset : RXRPC_JUMBO_SUBPKTLEN;
251 }
252
253 if (!(annotation & RXRPC_RX_ANNO_VERIFIED)) {
254 ret = rxrpc_verify_packet(call, skb, annotation, offset, len);
17926a79 255 if (ret < 0)
248f219c
DH
256 return ret;
257 *_annotation |= RXRPC_RX_ANNO_VERIFIED;
258 }
17926a79 259
248f219c
DH
260 *_offset = offset;
261 *_len = len;
262 call->conn->security->locate_data(call, skb, _offset, _len);
263 return 0;
264}
17926a79 265
248f219c
DH
266/*
267 * Deliver messages to a call. This keeps processing packets until the buffer
268 * is filled and we find either more DATA (returns 0) or the end of the DATA
269 * (returns 1). If more packets are required, it returns -EAGAIN.
270 */
271static int rxrpc_recvmsg_data(struct socket *sock, struct rxrpc_call *call,
272 struct msghdr *msg, struct iov_iter *iter,
273 size_t len, int flags, size_t *_offset)
274{
275 struct rxrpc_skb_priv *sp;
276 struct sk_buff *skb;
277 rxrpc_seq_t hard_ack, top, seq;
278 size_t remain;
279 bool last;
280 unsigned int rx_pkt_offset, rx_pkt_len;
281 int ix, copy, ret = 0;
282
283 _enter("");
284
285 rx_pkt_offset = call->rx_pkt_offset;
286 rx_pkt_len = call->rx_pkt_len;
287
288 /* Barriers against rxrpc_input_data(). */
289 hard_ack = call->rx_hard_ack;
290 top = smp_load_acquire(&call->rx_top);
291 for (seq = hard_ack + 1; before_eq(seq, top); seq++) {
292 ix = seq & RXRPC_RXTX_BUFF_MASK;
293 skb = call->rxtx_buffer[ix];
294 if (!skb)
295 break;
296 smp_rmb();
297 rxrpc_see_skb(skb);
298 sp = rxrpc_skb(skb);
17926a79 299
248f219c
DH
300 if (msg)
301 sock_recv_timestamp(msg, sock->sk, skb);
302
2e2ea51d 303 if (rx_pkt_offset == 0) {
4b22457c
DH
304 ret = rxrpc_locate_data(call, skb,
305 &call->rxtx_annotations[ix],
306 &rx_pkt_offset, &rx_pkt_len);
2e2ea51d
DH
307 if (ret < 0)
308 goto out;
309 }
248f219c
DH
310 _debug("recvmsg %x DATA #%u { %d, %d }",
311 sp->hdr.callNumber, seq, rx_pkt_offset, rx_pkt_len);
312
313 /* We have to handle short, empty and used-up DATA packets. */
314 remain = len - *_offset;
315 copy = rx_pkt_len;
316 if (copy > remain)
317 copy = remain;
318 if (copy > 0) {
319 ret = skb_copy_datagram_iter(skb, rx_pkt_offset, iter,
320 copy);
321 if (ret < 0)
322 goto out;
323
324 /* handle piecemeal consumption of data packets */
325 _debug("copied %d @%zu", copy, *_offset);
326
327 rx_pkt_offset += copy;
328 rx_pkt_len -= copy;
329 *_offset += copy;
330 }
17926a79 331
248f219c 332 if (rx_pkt_len > 0) {
17926a79 333 _debug("buffer full");
248f219c 334 ASSERTCMP(*_offset, ==, len);
17926a79
DH
335 break;
336 }
337
248f219c
DH
338 /* The whole packet has been transferred. */
339 last = sp->hdr.flags & RXRPC_LAST_PACKET;
372ee163 340 if (!(flags & MSG_PEEK))
248f219c
DH
341 rxrpc_rotate_rx_window(call);
342 rx_pkt_offset = 0;
343 rx_pkt_len = 0;
17926a79 344
248f219c
DH
345 ASSERTIFCMP(last, seq, ==, top);
346 }
17926a79 347
248f219c
DH
348 if (after(seq, top)) {
349 ret = -EAGAIN;
350 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags))
351 ret = 1;
352 }
353out:
354 if (!(flags & MSG_PEEK)) {
355 call->rx_pkt_offset = rx_pkt_offset;
356 call->rx_pkt_len = rx_pkt_len;
357 }
358 _leave(" = %d [%u/%u]", ret, seq, top);
359 return ret;
360}
17926a79 361
248f219c
DH
362/*
363 * Receive a message from an RxRPC socket
364 * - we need to be careful about two or more threads calling recvmsg
365 * simultaneously
366 */
367int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
368 int flags)
369{
370 struct rxrpc_call *call;
371 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
372 struct list_head *l;
373 size_t copied = 0;
374 long timeo;
375 int ret;
376
377 DEFINE_WAIT(wait);
378
379 _enter(",,,%zu,%d", len, flags);
380
381 if (flags & (MSG_OOB | MSG_TRUNC))
382 return -EOPNOTSUPP;
383
384 timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT);
385
386try_again:
387 lock_sock(&rx->sk);
388
389 /* Return immediately if a client socket has no outstanding calls */
390 if (RB_EMPTY_ROOT(&rx->calls) &&
391 list_empty(&rx->recvmsg_q) &&
392 rx->sk.sk_state != RXRPC_SERVER_LISTENING) {
393 release_sock(&rx->sk);
394 return -ENODATA;
17926a79
DH
395 }
396
248f219c
DH
397 if (list_empty(&rx->recvmsg_q)) {
398 ret = -EWOULDBLOCK;
399 if (timeo == 0)
400 goto error_no_call;
401
402 release_sock(&rx->sk);
403
404 /* Wait for something to happen */
405 prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait,
406 TASK_INTERRUPTIBLE);
407 ret = sock_error(&rx->sk);
408 if (ret)
409 goto wait_error;
410
411 if (list_empty(&rx->recvmsg_q)) {
412 if (signal_pending(current))
413 goto wait_interrupted;
414 timeo = schedule_timeout(timeo);
17926a79 415 }
248f219c
DH
416 finish_wait(sk_sleep(&rx->sk), &wait);
417 goto try_again;
17926a79
DH
418 }
419
248f219c
DH
420 /* Find the next call and dequeue it if we're not just peeking. If we
421 * do dequeue it, that comes with a ref that we will need to release.
422 */
423 write_lock_bh(&rx->recvmsg_lock);
424 l = rx->recvmsg_q.next;
425 call = list_entry(l, struct rxrpc_call, recvmsg_link);
426 if (!(flags & MSG_PEEK))
427 list_del_init(&call->recvmsg_link);
428 else
429 rxrpc_get_call(call, rxrpc_call_got);
430 write_unlock_bh(&rx->recvmsg_lock);
17926a79 431
248f219c
DH
432 _debug("recvmsg call %p", call);
433
434 if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
17926a79 435 BUG();
248f219c
DH
436
437 if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
438 if (flags & MSG_CMSG_COMPAT) {
439 unsigned int id32 = call->user_call_ID;
440
441 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
442 sizeof(unsigned int), &id32);
443 } else {
444 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
445 sizeof(unsigned long),
446 &call->user_call_ID);
f5c17aae 447 }
248f219c
DH
448 if (ret < 0)
449 goto error;
450 }
451
452 if (msg->msg_name) {
453 size_t len = sizeof(call->conn->params.peer->srx);
454 memcpy(msg->msg_name, &call->conn->params.peer->srx, len);
455 msg->msg_namelen = len;
456 }
457
458 switch (call->state) {
459 case RXRPC_CALL_SERVER_ACCEPTING:
460 ret = rxrpc_recvmsg_new_call(rx, call, msg, flags);
17926a79 461 break;
248f219c
DH
462 case RXRPC_CALL_CLIENT_RECV_REPLY:
463 case RXRPC_CALL_SERVER_RECV_REQUEST:
464 case RXRPC_CALL_SERVER_ACK_REQUEST:
465 ret = rxrpc_recvmsg_data(sock, call, msg, &msg->msg_iter, len,
466 flags, &copied);
467 if (ret == -EAGAIN)
468 ret = 0;
33b603fd
DH
469
470 if (after(call->rx_top, call->rx_hard_ack) &&
471 call->rxtx_buffer[(call->rx_hard_ack + 1) & RXRPC_RXTX_BUFF_MASK])
472 rxrpc_notify_socket(call);
17926a79
DH
473 break;
474 default:
248f219c 475 ret = 0;
17926a79
DH
476 break;
477 }
478
479 if (ret < 0)
248f219c 480 goto error;
17926a79 481
248f219c
DH
482 if (call->state == RXRPC_CALL_COMPLETE) {
483 ret = rxrpc_recvmsg_term(call, msg);
484 if (ret < 0)
485 goto error;
486 if (!(flags & MSG_PEEK))
487 rxrpc_release_call(rx, call);
488 msg->msg_flags |= MSG_EOR;
489 ret = 1;
17926a79
DH
490 }
491
248f219c
DH
492 if (ret == 0)
493 msg->msg_flags |= MSG_MORE;
494 else
495 msg->msg_flags &= ~MSG_MORE;
496 ret = copied;
17926a79 497
248f219c 498error:
fff72429 499 rxrpc_put_call(call, rxrpc_call_put);
248f219c
DH
500error_no_call:
501 release_sock(&rx->sk);
17926a79
DH
502 _leave(" = %d", ret);
503 return ret;
504
17926a79
DH
505wait_interrupted:
506 ret = sock_intr_errno(timeo);
507wait_error:
4a4771a5 508 finish_wait(sk_sleep(&rx->sk), &wait);
248f219c
DH
509 release_sock(&rx->sk);
510 _leave(" = %d [wait]", ret);
d001648e
DH
511 return ret;
512}
651350d1
DH
513
514/**
d001648e
DH
515 * rxrpc_kernel_recv_data - Allow a kernel service to receive data/info
516 * @sock: The socket that the call exists on
517 * @call: The call to send data through
518 * @buf: The buffer to receive into
519 * @size: The size of the buffer, including data already read
520 * @_offset: The running offset into the buffer.
521 * @want_more: True if more data is expected to be read
522 * @_abort: Where the abort code is stored if -ECONNABORTED is returned
523 *
524 * Allow a kernel service to receive data and pick up information about the
525 * state of a call. Returns 0 if got what was asked for and there's more
526 * available, 1 if we got what was asked for and we're at the end of the data
527 * and -EAGAIN if we need more data.
528 *
529 * Note that we may return -EAGAIN to drain empty packets at the end of the
530 * data, even if we've already copied over the requested data.
651350d1 531 *
d001648e
DH
532 * This function adds the amount it transfers to *_offset, so this should be
533 * precleared as appropriate. Note that the amount remaining in the buffer is
534 * taken to be size - *_offset.
535 *
536 * *_abort should also be initialised to 0.
651350d1 537 */
d001648e
DH
538int rxrpc_kernel_recv_data(struct socket *sock, struct rxrpc_call *call,
539 void *buf, size_t size, size_t *_offset,
540 bool want_more, u32 *_abort)
651350d1 541{
d001648e
DH
542 struct iov_iter iter;
543 struct kvec iov;
544 int ret;
651350d1 545
248f219c
DH
546 _enter("{%d,%s},%zu/%zu,%d",
547 call->debug_id, rxrpc_call_states[call->state],
548 *_offset, size, want_more);
d001648e
DH
549
550 ASSERTCMP(*_offset, <=, size);
551 ASSERTCMP(call->state, !=, RXRPC_CALL_SERVER_ACCEPTING);
651350d1 552
d001648e
DH
553 iov.iov_base = buf + *_offset;
554 iov.iov_len = size - *_offset;
555 iov_iter_kvec(&iter, ITER_KVEC | READ, &iov, 1, size - *_offset);
556
557 lock_sock(sock->sk);
558
559 switch (call->state) {
560 case RXRPC_CALL_CLIENT_RECV_REPLY:
561 case RXRPC_CALL_SERVER_RECV_REQUEST:
562 case RXRPC_CALL_SERVER_ACK_REQUEST:
248f219c
DH
563 ret = rxrpc_recvmsg_data(sock, call, NULL, &iter, size, 0,
564 _offset);
d001648e
DH
565 if (ret < 0)
566 goto out;
567
568 /* We can only reach here with a partially full buffer if we
569 * have reached the end of the data. We must otherwise have a
570 * full buffer or have been given -EAGAIN.
571 */
572 if (ret == 1) {
573 if (*_offset < size)
574 goto short_data;
575 if (!want_more)
576 goto read_phase_complete;
577 ret = 0;
578 goto out;
579 }
580
581 if (!want_more)
582 goto excess_data;
583 goto out;
584
585 case RXRPC_CALL_COMPLETE:
586 goto call_complete;
587
588 default:
d001648e
DH
589 ret = -EINPROGRESS;
590 goto out;
591 }
592
593read_phase_complete:
594 ret = 1;
595out:
596 release_sock(sock->sk);
597 _leave(" = %d [%zu,%d]", ret, *_offset, *_abort);
598 return ret;
599
600short_data:
601 ret = -EBADMSG;
602 goto out;
603excess_data:
604 ret = -EMSGSIZE;
605 goto out;
606call_complete:
607 *_abort = call->abort_code;
608 ret = call->error;
609 if (call->completion == RXRPC_CALL_SUCCEEDED) {
610 ret = 1;
611 if (size > 0)
612 ret = -ECONNRESET;
613 }
614 goto out;
615}
616EXPORT_SYMBOL(rxrpc_kernel_recv_data);