]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/rxrpc/recvmsg.c
rxrpc: Move the check of rx_pkt_offset from rxrpc_locate_data() to caller
[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
4b22457c
DH
303 if (rx_pkt_offset == 0)
304 ret = rxrpc_locate_data(call, skb,
305 &call->rxtx_annotations[ix],
306 &rx_pkt_offset, &rx_pkt_len);
248f219c
DH
307 _debug("recvmsg %x DATA #%u { %d, %d }",
308 sp->hdr.callNumber, seq, rx_pkt_offset, rx_pkt_len);
309
310 /* We have to handle short, empty and used-up DATA packets. */
311 remain = len - *_offset;
312 copy = rx_pkt_len;
313 if (copy > remain)
314 copy = remain;
315 if (copy > 0) {
316 ret = skb_copy_datagram_iter(skb, rx_pkt_offset, iter,
317 copy);
318 if (ret < 0)
319 goto out;
320
321 /* handle piecemeal consumption of data packets */
322 _debug("copied %d @%zu", copy, *_offset);
323
324 rx_pkt_offset += copy;
325 rx_pkt_len -= copy;
326 *_offset += copy;
327 }
17926a79 328
248f219c 329 if (rx_pkt_len > 0) {
17926a79 330 _debug("buffer full");
248f219c 331 ASSERTCMP(*_offset, ==, len);
17926a79
DH
332 break;
333 }
334
248f219c
DH
335 /* The whole packet has been transferred. */
336 last = sp->hdr.flags & RXRPC_LAST_PACKET;
372ee163 337 if (!(flags & MSG_PEEK))
248f219c
DH
338 rxrpc_rotate_rx_window(call);
339 rx_pkt_offset = 0;
340 rx_pkt_len = 0;
17926a79 341
248f219c
DH
342 ASSERTIFCMP(last, seq, ==, top);
343 }
17926a79 344
248f219c
DH
345 if (after(seq, top)) {
346 ret = -EAGAIN;
347 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags))
348 ret = 1;
349 }
350out:
351 if (!(flags & MSG_PEEK)) {
352 call->rx_pkt_offset = rx_pkt_offset;
353 call->rx_pkt_len = rx_pkt_len;
354 }
355 _leave(" = %d [%u/%u]", ret, seq, top);
356 return ret;
357}
17926a79 358
248f219c
DH
359/*
360 * Receive a message from an RxRPC socket
361 * - we need to be careful about two or more threads calling recvmsg
362 * simultaneously
363 */
364int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
365 int flags)
366{
367 struct rxrpc_call *call;
368 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
369 struct list_head *l;
370 size_t copied = 0;
371 long timeo;
372 int ret;
373
374 DEFINE_WAIT(wait);
375
376 _enter(",,,%zu,%d", len, flags);
377
378 if (flags & (MSG_OOB | MSG_TRUNC))
379 return -EOPNOTSUPP;
380
381 timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT);
382
383try_again:
384 lock_sock(&rx->sk);
385
386 /* Return immediately if a client socket has no outstanding calls */
387 if (RB_EMPTY_ROOT(&rx->calls) &&
388 list_empty(&rx->recvmsg_q) &&
389 rx->sk.sk_state != RXRPC_SERVER_LISTENING) {
390 release_sock(&rx->sk);
391 return -ENODATA;
17926a79
DH
392 }
393
248f219c
DH
394 if (list_empty(&rx->recvmsg_q)) {
395 ret = -EWOULDBLOCK;
396 if (timeo == 0)
397 goto error_no_call;
398
399 release_sock(&rx->sk);
400
401 /* Wait for something to happen */
402 prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait,
403 TASK_INTERRUPTIBLE);
404 ret = sock_error(&rx->sk);
405 if (ret)
406 goto wait_error;
407
408 if (list_empty(&rx->recvmsg_q)) {
409 if (signal_pending(current))
410 goto wait_interrupted;
411 timeo = schedule_timeout(timeo);
17926a79 412 }
248f219c
DH
413 finish_wait(sk_sleep(&rx->sk), &wait);
414 goto try_again;
17926a79
DH
415 }
416
248f219c
DH
417 /* Find the next call and dequeue it if we're not just peeking. If we
418 * do dequeue it, that comes with a ref that we will need to release.
419 */
420 write_lock_bh(&rx->recvmsg_lock);
421 l = rx->recvmsg_q.next;
422 call = list_entry(l, struct rxrpc_call, recvmsg_link);
423 if (!(flags & MSG_PEEK))
424 list_del_init(&call->recvmsg_link);
425 else
426 rxrpc_get_call(call, rxrpc_call_got);
427 write_unlock_bh(&rx->recvmsg_lock);
17926a79 428
248f219c
DH
429 _debug("recvmsg call %p", call);
430
431 if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
17926a79 432 BUG();
248f219c
DH
433
434 if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
435 if (flags & MSG_CMSG_COMPAT) {
436 unsigned int id32 = call->user_call_ID;
437
438 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
439 sizeof(unsigned int), &id32);
440 } else {
441 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
442 sizeof(unsigned long),
443 &call->user_call_ID);
f5c17aae 444 }
248f219c
DH
445 if (ret < 0)
446 goto error;
447 }
448
449 if (msg->msg_name) {
450 size_t len = sizeof(call->conn->params.peer->srx);
451 memcpy(msg->msg_name, &call->conn->params.peer->srx, len);
452 msg->msg_namelen = len;
453 }
454
455 switch (call->state) {
456 case RXRPC_CALL_SERVER_ACCEPTING:
457 ret = rxrpc_recvmsg_new_call(rx, call, msg, flags);
17926a79 458 break;
248f219c
DH
459 case RXRPC_CALL_CLIENT_RECV_REPLY:
460 case RXRPC_CALL_SERVER_RECV_REQUEST:
461 case RXRPC_CALL_SERVER_ACK_REQUEST:
462 ret = rxrpc_recvmsg_data(sock, call, msg, &msg->msg_iter, len,
463 flags, &copied);
464 if (ret == -EAGAIN)
465 ret = 0;
33b603fd
DH
466
467 if (after(call->rx_top, call->rx_hard_ack) &&
468 call->rxtx_buffer[(call->rx_hard_ack + 1) & RXRPC_RXTX_BUFF_MASK])
469 rxrpc_notify_socket(call);
17926a79
DH
470 break;
471 default:
248f219c 472 ret = 0;
17926a79
DH
473 break;
474 }
475
476 if (ret < 0)
248f219c 477 goto error;
17926a79 478
248f219c
DH
479 if (call->state == RXRPC_CALL_COMPLETE) {
480 ret = rxrpc_recvmsg_term(call, msg);
481 if (ret < 0)
482 goto error;
483 if (!(flags & MSG_PEEK))
484 rxrpc_release_call(rx, call);
485 msg->msg_flags |= MSG_EOR;
486 ret = 1;
17926a79
DH
487 }
488
248f219c
DH
489 if (ret == 0)
490 msg->msg_flags |= MSG_MORE;
491 else
492 msg->msg_flags &= ~MSG_MORE;
493 ret = copied;
17926a79 494
248f219c 495error:
fff72429 496 rxrpc_put_call(call, rxrpc_call_put);
248f219c
DH
497error_no_call:
498 release_sock(&rx->sk);
17926a79
DH
499 _leave(" = %d", ret);
500 return ret;
501
17926a79
DH
502wait_interrupted:
503 ret = sock_intr_errno(timeo);
504wait_error:
4a4771a5 505 finish_wait(sk_sleep(&rx->sk), &wait);
248f219c
DH
506 release_sock(&rx->sk);
507 _leave(" = %d [wait]", ret);
d001648e
DH
508 return ret;
509}
651350d1
DH
510
511/**
d001648e
DH
512 * rxrpc_kernel_recv_data - Allow a kernel service to receive data/info
513 * @sock: The socket that the call exists on
514 * @call: The call to send data through
515 * @buf: The buffer to receive into
516 * @size: The size of the buffer, including data already read
517 * @_offset: The running offset into the buffer.
518 * @want_more: True if more data is expected to be read
519 * @_abort: Where the abort code is stored if -ECONNABORTED is returned
520 *
521 * Allow a kernel service to receive data and pick up information about the
522 * state of a call. Returns 0 if got what was asked for and there's more
523 * available, 1 if we got what was asked for and we're at the end of the data
524 * and -EAGAIN if we need more data.
525 *
526 * Note that we may return -EAGAIN to drain empty packets at the end of the
527 * data, even if we've already copied over the requested data.
651350d1 528 *
d001648e
DH
529 * This function adds the amount it transfers to *_offset, so this should be
530 * precleared as appropriate. Note that the amount remaining in the buffer is
531 * taken to be size - *_offset.
532 *
533 * *_abort should also be initialised to 0.
651350d1 534 */
d001648e
DH
535int rxrpc_kernel_recv_data(struct socket *sock, struct rxrpc_call *call,
536 void *buf, size_t size, size_t *_offset,
537 bool want_more, u32 *_abort)
651350d1 538{
d001648e
DH
539 struct iov_iter iter;
540 struct kvec iov;
541 int ret;
651350d1 542
248f219c
DH
543 _enter("{%d,%s},%zu/%zu,%d",
544 call->debug_id, rxrpc_call_states[call->state],
545 *_offset, size, want_more);
d001648e
DH
546
547 ASSERTCMP(*_offset, <=, size);
548 ASSERTCMP(call->state, !=, RXRPC_CALL_SERVER_ACCEPTING);
651350d1 549
d001648e
DH
550 iov.iov_base = buf + *_offset;
551 iov.iov_len = size - *_offset;
552 iov_iter_kvec(&iter, ITER_KVEC | READ, &iov, 1, size - *_offset);
553
554 lock_sock(sock->sk);
555
556 switch (call->state) {
557 case RXRPC_CALL_CLIENT_RECV_REPLY:
558 case RXRPC_CALL_SERVER_RECV_REQUEST:
559 case RXRPC_CALL_SERVER_ACK_REQUEST:
248f219c
DH
560 ret = rxrpc_recvmsg_data(sock, call, NULL, &iter, size, 0,
561 _offset);
d001648e
DH
562 if (ret < 0)
563 goto out;
564
565 /* We can only reach here with a partially full buffer if we
566 * have reached the end of the data. We must otherwise have a
567 * full buffer or have been given -EAGAIN.
568 */
569 if (ret == 1) {
570 if (*_offset < size)
571 goto short_data;
572 if (!want_more)
573 goto read_phase_complete;
574 ret = 0;
575 goto out;
576 }
577
578 if (!want_more)
579 goto excess_data;
580 goto out;
581
582 case RXRPC_CALL_COMPLETE:
583 goto call_complete;
584
585 default:
d001648e
DH
586 ret = -EINPROGRESS;
587 goto out;
588 }
589
590read_phase_complete:
591 ret = 1;
592out:
593 release_sock(sock->sk);
594 _leave(" = %d [%zu,%d]", ret, *_offset, *_abort);
595 return ret;
596
597short_data:
598 ret = -EBADMSG;
599 goto out;
600excess_data:
601 ret = -EMSGSIZE;
602 goto out;
603call_complete:
604 *_abort = call->abort_code;
605 ret = call->error;
606 if (call->completion == RXRPC_CALL_SUCCEEDED) {
607 ret = 1;
608 if (size > 0)
609 ret = -ECONNRESET;
610 }
611 goto out;
612}
613EXPORT_SYMBOL(rxrpc_kernel_recv_data);