David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 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 | |
Joe Perches | 9b6d539 | 2016-06-02 12:08:52 -0700 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 14 | #include <linux/net.h> |
| 15 | #include <linux/skbuff.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 16 | #include <linux/export.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 17 | #include <net/sock.h> |
| 18 | #include <net/af_rxrpc.h> |
| 19 | #include "ar-internal.h" |
| 20 | |
| 21 | /* |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 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. |
| 24 | */ |
| 25 | void 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 | */ |
| 64 | static 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 | */ |
| 107 | static 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 Howells | 3432a75 | 2016-09-13 09:05:14 +0100 | [diff] [blame^] | 121 | rxrpc_get_call(call, rxrpc_call_got); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 122 | 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 | */ |
| 133 | static void rxrpc_end_rx_phase(struct rxrpc_call *call) |
| 134 | { |
| 135 | _enter("%d,%s", call->debug_id, rxrpc_call_states[call->state]); |
| 136 | |
| 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 | } |
| 143 | |
| 144 | write_lock_bh(&call->state_lock); |
| 145 | |
| 146 | switch (call->state) { |
| 147 | case RXRPC_CALL_CLIENT_RECV_REPLY: |
| 148 | __rxrpc_call_completed(call); |
| 149 | break; |
| 150 | |
| 151 | case RXRPC_CALL_SERVER_RECV_REQUEST: |
| 152 | call->state = RXRPC_CALL_SERVER_ACK_REQUEST; |
| 153 | break; |
| 154 | default: |
| 155 | break; |
| 156 | } |
| 157 | |
| 158 | write_unlock_bh(&call->state_lock); |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Discard a packet we've used up and advance the Rx window by one. |
| 163 | */ |
| 164 | static 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); |
| 184 | |
| 185 | rxrpc_free_skb(skb); |
| 186 | |
| 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 | */ |
| 198 | static 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 | */ |
| 233 | static 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 | |
| 243 | if (offset > 0) |
| 244 | return 0; |
| 245 | |
| 246 | /* Locate the subpacket */ |
| 247 | offset = sp->offset; |
| 248 | len = skb->len - sp->offset; |
| 249 | if ((annotation & RXRPC_RX_ANNO_JUMBO) > 0) { |
| 250 | offset += (((annotation & RXRPC_RX_ANNO_JUMBO) - 1) * |
| 251 | RXRPC_JUMBO_SUBPKTLEN); |
| 252 | len = (annotation & RXRPC_RX_ANNO_JLAST) ? |
| 253 | skb->len - offset : RXRPC_JUMBO_SUBPKTLEN; |
| 254 | } |
| 255 | |
| 256 | if (!(annotation & RXRPC_RX_ANNO_VERIFIED)) { |
| 257 | ret = rxrpc_verify_packet(call, skb, annotation, offset, len); |
| 258 | if (ret < 0) |
| 259 | return ret; |
| 260 | *_annotation |= RXRPC_RX_ANNO_VERIFIED; |
| 261 | } |
| 262 | |
| 263 | *_offset = offset; |
| 264 | *_len = len; |
| 265 | call->conn->security->locate_data(call, skb, _offset, _len); |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Deliver messages to a call. This keeps processing packets until the buffer |
| 271 | * is filled and we find either more DATA (returns 0) or the end of the DATA |
| 272 | * (returns 1). If more packets are required, it returns -EAGAIN. |
| 273 | */ |
| 274 | static int rxrpc_recvmsg_data(struct socket *sock, struct rxrpc_call *call, |
| 275 | struct msghdr *msg, struct iov_iter *iter, |
| 276 | size_t len, int flags, size_t *_offset) |
| 277 | { |
| 278 | struct rxrpc_skb_priv *sp; |
| 279 | struct sk_buff *skb; |
| 280 | rxrpc_seq_t hard_ack, top, seq; |
| 281 | size_t remain; |
| 282 | bool last; |
| 283 | unsigned int rx_pkt_offset, rx_pkt_len; |
| 284 | int ix, copy, ret = 0; |
| 285 | |
| 286 | _enter(""); |
| 287 | |
| 288 | rx_pkt_offset = call->rx_pkt_offset; |
| 289 | rx_pkt_len = call->rx_pkt_len; |
| 290 | |
| 291 | /* Barriers against rxrpc_input_data(). */ |
| 292 | hard_ack = call->rx_hard_ack; |
| 293 | top = smp_load_acquire(&call->rx_top); |
| 294 | for (seq = hard_ack + 1; before_eq(seq, top); seq++) { |
| 295 | ix = seq & RXRPC_RXTX_BUFF_MASK; |
| 296 | skb = call->rxtx_buffer[ix]; |
| 297 | if (!skb) |
| 298 | break; |
| 299 | smp_rmb(); |
| 300 | rxrpc_see_skb(skb); |
| 301 | sp = rxrpc_skb(skb); |
| 302 | |
| 303 | if (msg) |
| 304 | sock_recv_timestamp(msg, sock->sk, skb); |
| 305 | |
| 306 | ret = rxrpc_locate_data(call, skb, &call->rxtx_annotations[ix], |
| 307 | &rx_pkt_offset, &rx_pkt_len); |
| 308 | _debug("recvmsg %x DATA #%u { %d, %d }", |
| 309 | sp->hdr.callNumber, seq, rx_pkt_offset, rx_pkt_len); |
| 310 | |
| 311 | /* We have to handle short, empty and used-up DATA packets. */ |
| 312 | remain = len - *_offset; |
| 313 | copy = rx_pkt_len; |
| 314 | if (copy > remain) |
| 315 | copy = remain; |
| 316 | if (copy > 0) { |
| 317 | ret = skb_copy_datagram_iter(skb, rx_pkt_offset, iter, |
| 318 | copy); |
| 319 | if (ret < 0) |
| 320 | goto out; |
| 321 | |
| 322 | /* handle piecemeal consumption of data packets */ |
| 323 | _debug("copied %d @%zu", copy, *_offset); |
| 324 | |
| 325 | rx_pkt_offset += copy; |
| 326 | rx_pkt_len -= copy; |
| 327 | *_offset += copy; |
| 328 | } |
| 329 | |
| 330 | if (rx_pkt_len > 0) { |
| 331 | _debug("buffer full"); |
| 332 | ASSERTCMP(*_offset, ==, len); |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | /* The whole packet has been transferred. */ |
| 337 | last = sp->hdr.flags & RXRPC_LAST_PACKET; |
| 338 | if (!(flags & MSG_PEEK)) |
| 339 | rxrpc_rotate_rx_window(call); |
| 340 | rx_pkt_offset = 0; |
| 341 | rx_pkt_len = 0; |
| 342 | |
| 343 | ASSERTIFCMP(last, seq, ==, top); |
| 344 | } |
| 345 | |
| 346 | if (after(seq, top)) { |
| 347 | ret = -EAGAIN; |
| 348 | if (test_bit(RXRPC_CALL_RX_LAST, &call->flags)) |
| 349 | ret = 1; |
| 350 | } |
| 351 | out: |
| 352 | if (!(flags & MSG_PEEK)) { |
| 353 | call->rx_pkt_offset = rx_pkt_offset; |
| 354 | call->rx_pkt_len = rx_pkt_len; |
| 355 | } |
| 356 | _leave(" = %d [%u/%u]", ret, seq, top); |
| 357 | return ret; |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * Receive a message from an RxRPC socket |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 362 | * - we need to be careful about two or more threads calling recvmsg |
| 363 | * simultaneously |
| 364 | */ |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 365 | int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, |
| 366 | int flags) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 367 | { |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 368 | struct rxrpc_call *call; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 369 | struct rxrpc_sock *rx = rxrpc_sk(sock->sk); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 370 | struct list_head *l; |
| 371 | size_t copied = 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 372 | long timeo; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 373 | int ret; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 374 | |
| 375 | DEFINE_WAIT(wait); |
| 376 | |
| 377 | _enter(",,,%zu,%d", len, flags); |
| 378 | |
| 379 | if (flags & (MSG_OOB | MSG_TRUNC)) |
| 380 | return -EOPNOTSUPP; |
| 381 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 382 | timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 383 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 384 | try_again: |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 385 | lock_sock(&rx->sk); |
| 386 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 387 | /* Return immediately if a client socket has no outstanding calls */ |
| 388 | if (RB_EMPTY_ROOT(&rx->calls) && |
| 389 | list_empty(&rx->recvmsg_q) && |
| 390 | rx->sk.sk_state != RXRPC_SERVER_LISTENING) { |
| 391 | release_sock(&rx->sk); |
| 392 | return -ENODATA; |
| 393 | } |
| 394 | |
| 395 | if (list_empty(&rx->recvmsg_q)) { |
| 396 | ret = -EWOULDBLOCK; |
| 397 | if (timeo == 0) |
| 398 | goto error_no_call; |
| 399 | |
| 400 | release_sock(&rx->sk); |
| 401 | |
| 402 | /* Wait for something to happen */ |
| 403 | prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait, |
| 404 | TASK_INTERRUPTIBLE); |
| 405 | ret = sock_error(&rx->sk); |
| 406 | if (ret) |
| 407 | goto wait_error; |
| 408 | |
| 409 | if (list_empty(&rx->recvmsg_q)) { |
| 410 | if (signal_pending(current)) |
| 411 | goto wait_interrupted; |
| 412 | timeo = schedule_timeout(timeo); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 413 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 414 | finish_wait(sk_sleep(&rx->sk), &wait); |
| 415 | goto try_again; |
| 416 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 417 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 418 | /* Find the next call and dequeue it if we're not just peeking. If we |
| 419 | * do dequeue it, that comes with a ref that we will need to release. |
| 420 | */ |
| 421 | write_lock_bh(&rx->recvmsg_lock); |
| 422 | l = rx->recvmsg_q.next; |
| 423 | call = list_entry(l, struct rxrpc_call, recvmsg_link); |
| 424 | if (!(flags & MSG_PEEK)) |
| 425 | list_del_init(&call->recvmsg_link); |
| 426 | else |
David Howells | fff72429 | 2016-09-07 14:34:21 +0100 | [diff] [blame] | 427 | rxrpc_get_call(call, rxrpc_call_got); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 428 | write_unlock_bh(&rx->recvmsg_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 429 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 430 | _debug("recvmsg call %p", call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 431 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 432 | if (test_bit(RXRPC_CALL_RELEASED, &call->flags)) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 433 | BUG(); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 434 | |
| 435 | if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) { |
| 436 | if (flags & MSG_CMSG_COMPAT) { |
| 437 | unsigned int id32 = call->user_call_ID; |
| 438 | |
| 439 | ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID, |
| 440 | sizeof(unsigned int), &id32); |
| 441 | } else { |
| 442 | ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID, |
| 443 | sizeof(unsigned long), |
| 444 | &call->user_call_ID); |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 445 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 446 | if (ret < 0) |
| 447 | goto error; |
| 448 | } |
| 449 | |
| 450 | if (msg->msg_name) { |
| 451 | size_t len = sizeof(call->conn->params.peer->srx); |
| 452 | memcpy(msg->msg_name, &call->conn->params.peer->srx, len); |
| 453 | msg->msg_namelen = len; |
| 454 | } |
| 455 | |
| 456 | switch (call->state) { |
| 457 | case RXRPC_CALL_SERVER_ACCEPTING: |
| 458 | ret = rxrpc_recvmsg_new_call(rx, call, msg, flags); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 459 | break; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 460 | case RXRPC_CALL_CLIENT_RECV_REPLY: |
| 461 | case RXRPC_CALL_SERVER_RECV_REQUEST: |
| 462 | case RXRPC_CALL_SERVER_ACK_REQUEST: |
| 463 | ret = rxrpc_recvmsg_data(sock, call, msg, &msg->msg_iter, len, |
| 464 | flags, &copied); |
| 465 | if (ret == -EAGAIN) |
| 466 | ret = 0; |
David Howells | 33b603f | 2016-09-13 22:36:21 +0100 | [diff] [blame] | 467 | |
| 468 | if (after(call->rx_top, call->rx_hard_ack) && |
| 469 | call->rxtx_buffer[(call->rx_hard_ack + 1) & RXRPC_RXTX_BUFF_MASK]) |
| 470 | rxrpc_notify_socket(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 471 | break; |
| 472 | default: |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 473 | ret = 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 474 | break; |
| 475 | } |
| 476 | |
| 477 | if (ret < 0) |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 478 | goto error; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 479 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 480 | if (call->state == RXRPC_CALL_COMPLETE) { |
| 481 | ret = rxrpc_recvmsg_term(call, msg); |
| 482 | if (ret < 0) |
| 483 | goto error; |
| 484 | if (!(flags & MSG_PEEK)) |
| 485 | rxrpc_release_call(rx, call); |
| 486 | msg->msg_flags |= MSG_EOR; |
| 487 | ret = 1; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 488 | } |
| 489 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 490 | if (ret == 0) |
| 491 | msg->msg_flags |= MSG_MORE; |
| 492 | else |
| 493 | msg->msg_flags &= ~MSG_MORE; |
| 494 | ret = copied; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 495 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 496 | error: |
David Howells | fff72429 | 2016-09-07 14:34:21 +0100 | [diff] [blame] | 497 | rxrpc_put_call(call, rxrpc_call_put); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 498 | error_no_call: |
| 499 | release_sock(&rx->sk); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 500 | _leave(" = %d", ret); |
| 501 | return ret; |
| 502 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 503 | wait_interrupted: |
| 504 | ret = sock_intr_errno(timeo); |
| 505 | wait_error: |
Eric Dumazet | 4a4771a | 2010-04-25 22:20:06 +0000 | [diff] [blame] | 506 | finish_wait(sk_sleep(&rx->sk), &wait); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 507 | release_sock(&rx->sk); |
| 508 | _leave(" = %d [wait]", ret); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 509 | return ret; |
| 510 | } |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 511 | |
| 512 | /** |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 513 | * rxrpc_kernel_recv_data - Allow a kernel service to receive data/info |
| 514 | * @sock: The socket that the call exists on |
| 515 | * @call: The call to send data through |
| 516 | * @buf: The buffer to receive into |
| 517 | * @size: The size of the buffer, including data already read |
| 518 | * @_offset: The running offset into the buffer. |
| 519 | * @want_more: True if more data is expected to be read |
| 520 | * @_abort: Where the abort code is stored if -ECONNABORTED is returned |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 521 | * |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 522 | * Allow a kernel service to receive data and pick up information about the |
| 523 | * state of a call. Returns 0 if got what was asked for and there's more |
| 524 | * available, 1 if we got what was asked for and we're at the end of the data |
| 525 | * and -EAGAIN if we need more data. |
| 526 | * |
| 527 | * Note that we may return -EAGAIN to drain empty packets at the end of the |
| 528 | * data, even if we've already copied over the requested data. |
| 529 | * |
| 530 | * This function adds the amount it transfers to *_offset, so this should be |
| 531 | * precleared as appropriate. Note that the amount remaining in the buffer is |
| 532 | * taken to be size - *_offset. |
| 533 | * |
| 534 | * *_abort should also be initialised to 0. |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 535 | */ |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 536 | int rxrpc_kernel_recv_data(struct socket *sock, struct rxrpc_call *call, |
| 537 | void *buf, size_t size, size_t *_offset, |
| 538 | bool want_more, u32 *_abort) |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 539 | { |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 540 | struct iov_iter iter; |
| 541 | struct kvec iov; |
| 542 | int ret; |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 543 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 544 | _enter("{%d,%s},%zu/%zu,%d", |
| 545 | call->debug_id, rxrpc_call_states[call->state], |
| 546 | *_offset, size, want_more); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 547 | |
| 548 | ASSERTCMP(*_offset, <=, size); |
| 549 | ASSERTCMP(call->state, !=, RXRPC_CALL_SERVER_ACCEPTING); |
| 550 | |
| 551 | iov.iov_base = buf + *_offset; |
| 552 | iov.iov_len = size - *_offset; |
| 553 | iov_iter_kvec(&iter, ITER_KVEC | READ, &iov, 1, size - *_offset); |
| 554 | |
| 555 | lock_sock(sock->sk); |
| 556 | |
| 557 | switch (call->state) { |
| 558 | case RXRPC_CALL_CLIENT_RECV_REPLY: |
| 559 | case RXRPC_CALL_SERVER_RECV_REQUEST: |
| 560 | case RXRPC_CALL_SERVER_ACK_REQUEST: |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 561 | ret = rxrpc_recvmsg_data(sock, call, NULL, &iter, size, 0, |
| 562 | _offset); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 563 | if (ret < 0) |
| 564 | goto out; |
| 565 | |
| 566 | /* We can only reach here with a partially full buffer if we |
| 567 | * have reached the end of the data. We must otherwise have a |
| 568 | * full buffer or have been given -EAGAIN. |
| 569 | */ |
| 570 | if (ret == 1) { |
| 571 | if (*_offset < size) |
| 572 | goto short_data; |
| 573 | if (!want_more) |
| 574 | goto read_phase_complete; |
| 575 | ret = 0; |
| 576 | goto out; |
| 577 | } |
| 578 | |
| 579 | if (!want_more) |
| 580 | goto excess_data; |
| 581 | goto out; |
| 582 | |
| 583 | case RXRPC_CALL_COMPLETE: |
| 584 | goto call_complete; |
| 585 | |
| 586 | default: |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 587 | ret = -EINPROGRESS; |
| 588 | goto out; |
| 589 | } |
| 590 | |
| 591 | read_phase_complete: |
| 592 | ret = 1; |
| 593 | out: |
| 594 | release_sock(sock->sk); |
| 595 | _leave(" = %d [%zu,%d]", ret, *_offset, *_abort); |
| 596 | return ret; |
| 597 | |
| 598 | short_data: |
| 599 | ret = -EBADMSG; |
| 600 | goto out; |
| 601 | excess_data: |
| 602 | ret = -EMSGSIZE; |
| 603 | goto out; |
| 604 | call_complete: |
| 605 | *_abort = call->abort_code; |
| 606 | ret = call->error; |
| 607 | if (call->completion == RXRPC_CALL_SUCCEEDED) { |
| 608 | ret = 1; |
| 609 | if (size > 0) |
| 610 | ret = -ECONNRESET; |
| 611 | } |
| 612 | goto out; |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 613 | } |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 614 | EXPORT_SYMBOL(rxrpc_kernel_recv_data); |