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 | |
David Howells | 8499790 | 2016-09-17 11:13:31 +0100 | [diff] [blame] | 97 | trace_rxrpc_recvmsg(call, rxrpc_recvmsg_terminal, call->rx_hard_ack, |
| 98 | call->rx_pkt_offset, call->rx_pkt_len, ret); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 99 | 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 | */ |
| 109 | static 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 Howells | 3432a75 | 2016-09-13 09:05:14 +0100 | [diff] [blame] | 123 | rxrpc_get_call(call, rxrpc_call_got); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 124 | 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 Howells | 8499790 | 2016-09-17 11:13:31 +0100 | [diff] [blame] | 129 | trace_rxrpc_recvmsg(call, rxrpc_recvmsg_to_be_accepted, 1, 0, 0, ret); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * End the packet reception phase. |
| 135 | */ |
David Howells | b69d94d | 2016-09-24 18:05:27 +0100 | [diff] [blame] | 136 | static void rxrpc_end_rx_phase(struct rxrpc_call *call, rxrpc_serial_t serial) |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 137 | { |
| 138 | _enter("%d,%s", call->debug_id, rxrpc_call_states[call->state]); |
| 139 | |
David Howells | 58dc63c | 2016-09-17 10:49:13 +0100 | [diff] [blame] | 140 | trace_rxrpc_receive(call, rxrpc_receive_end, 0, call->rx_top); |
David Howells | 816c9fc | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 141 | ASSERTCMP(call->rx_hard_ack, ==, call->rx_top); |
| 142 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 143 | if (call->state == RXRPC_CALL_CLIENT_RECV_REPLY) { |
David Howells | b69d94d | 2016-09-24 18:05:27 +0100 | [diff] [blame] | 144 | rxrpc_propose_ACK(call, RXRPC_ACK_IDLE, 0, serial, true, false, |
David Howells | 9c7ad43 | 2016-09-23 13:50:40 +0100 | [diff] [blame] | 145 | rxrpc_propose_ack_terminal_ack); |
David Howells | a5af7e1 | 2016-10-06 08:11:49 +0100 | [diff] [blame] | 146 | rxrpc_send_ack_packet(call, false); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | write_lock_bh(&call->state_lock); |
| 150 | |
| 151 | switch (call->state) { |
| 152 | case RXRPC_CALL_CLIENT_RECV_REPLY: |
| 153 | __rxrpc_call_completed(call); |
David Howells | 9749fd2 | 2016-10-06 08:11:50 +0100 | [diff] [blame] | 154 | write_unlock_bh(&call->state_lock); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 155 | break; |
| 156 | |
| 157 | case RXRPC_CALL_SERVER_RECV_REQUEST: |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 158 | call->tx_phase = true; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 159 | call->state = RXRPC_CALL_SERVER_ACK_REQUEST; |
David Howells | 9749fd2 | 2016-10-06 08:11:50 +0100 | [diff] [blame] | 160 | call->ack_at = call->expire_at; |
| 161 | write_unlock_bh(&call->state_lock); |
| 162 | rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, 0, serial, false, true, |
| 163 | rxrpc_propose_ack_processing_op); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 164 | break; |
| 165 | default: |
David Howells | 9749fd2 | 2016-10-06 08:11:50 +0100 | [diff] [blame] | 166 | write_unlock_bh(&call->state_lock); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 167 | break; |
| 168 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Discard a packet we've used up and advance the Rx window by one. |
| 173 | */ |
| 174 | static void rxrpc_rotate_rx_window(struct rxrpc_call *call) |
| 175 | { |
David Howells | 816c9fc | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 176 | struct rxrpc_skb_priv *sp; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 177 | struct sk_buff *skb; |
David Howells | 58dc63c | 2016-09-17 10:49:13 +0100 | [diff] [blame] | 178 | rxrpc_serial_t serial; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 179 | rxrpc_seq_t hard_ack, top; |
David Howells | 816c9fc | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 180 | u8 flags; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 181 | int ix; |
| 182 | |
| 183 | _enter("%d", call->debug_id); |
| 184 | |
| 185 | hard_ack = call->rx_hard_ack; |
| 186 | top = smp_load_acquire(&call->rx_top); |
| 187 | ASSERT(before(hard_ack, top)); |
| 188 | |
| 189 | hard_ack++; |
| 190 | ix = hard_ack & RXRPC_RXTX_BUFF_MASK; |
| 191 | skb = call->rxtx_buffer[ix]; |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 192 | rxrpc_see_skb(skb, rxrpc_skb_rx_rotated); |
David Howells | 816c9fc | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 193 | sp = rxrpc_skb(skb); |
| 194 | flags = sp->hdr.flags; |
David Howells | 58dc63c | 2016-09-17 10:49:13 +0100 | [diff] [blame] | 195 | serial = sp->hdr.serial; |
| 196 | if (call->rxtx_annotations[ix] & RXRPC_RX_ANNO_JUMBO) |
| 197 | serial += (call->rxtx_annotations[ix] & RXRPC_RX_ANNO_JUMBO) - 1; |
| 198 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 199 | call->rxtx_buffer[ix] = NULL; |
| 200 | call->rxtx_annotations[ix] = 0; |
| 201 | /* Barrier against rxrpc_input_data(). */ |
| 202 | smp_store_release(&call->rx_hard_ack, hard_ack); |
| 203 | |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 204 | rxrpc_free_skb(skb, rxrpc_skb_rx_freed); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 205 | |
David Howells | 816c9fc | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 206 | _debug("%u,%u,%02x", hard_ack, top, flags); |
David Howells | 58dc63c | 2016-09-17 10:49:13 +0100 | [diff] [blame] | 207 | trace_rxrpc_receive(call, rxrpc_receive_rotate, serial, hard_ack); |
David Howells | 805b21b | 2016-09-24 18:05:26 +0100 | [diff] [blame] | 208 | if (flags & RXRPC_LAST_PACKET) { |
David Howells | b69d94d | 2016-09-24 18:05:27 +0100 | [diff] [blame] | 209 | rxrpc_end_rx_phase(call, serial); |
David Howells | 805b21b | 2016-09-24 18:05:26 +0100 | [diff] [blame] | 210 | } else { |
| 211 | /* Check to see if there's an ACK that needs sending. */ |
| 212 | if (after_eq(hard_ack, call->ackr_consumed + 2) || |
| 213 | after_eq(top, call->ackr_seen + 2) || |
| 214 | (hard_ack == top && after(hard_ack, call->ackr_consumed))) |
| 215 | rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, 0, serial, |
| 216 | true, false, |
| 217 | rxrpc_propose_ack_rotate_rx); |
| 218 | if (call->ackr_reason) |
David Howells | a5af7e1 | 2016-10-06 08:11:49 +0100 | [diff] [blame] | 219 | rxrpc_send_ack_packet(call, false); |
David Howells | 805b21b | 2016-09-24 18:05:26 +0100 | [diff] [blame] | 220 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Decrypt and verify a (sub)packet. The packet's length may be changed due to |
| 225 | * padding, but if this is the case, the packet length will be resident in the |
| 226 | * socket buffer. Note that we can't modify the master skb info as the skb may |
| 227 | * be the home to multiple subpackets. |
| 228 | */ |
| 229 | static int rxrpc_verify_packet(struct rxrpc_call *call, struct sk_buff *skb, |
| 230 | u8 annotation, |
| 231 | unsigned int offset, unsigned int len) |
| 232 | { |
| 233 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
| 234 | rxrpc_seq_t seq = sp->hdr.seq; |
| 235 | u16 cksum = sp->hdr.cksum; |
| 236 | |
| 237 | _enter(""); |
| 238 | |
| 239 | /* For all but the head jumbo subpacket, the security checksum is in a |
| 240 | * jumbo header immediately prior to the data. |
| 241 | */ |
| 242 | if ((annotation & RXRPC_RX_ANNO_JUMBO) > 1) { |
| 243 | __be16 tmp; |
| 244 | if (skb_copy_bits(skb, offset - 2, &tmp, 2) < 0) |
| 245 | BUG(); |
| 246 | cksum = ntohs(tmp); |
| 247 | seq += (annotation & RXRPC_RX_ANNO_JUMBO) - 1; |
| 248 | } |
| 249 | |
| 250 | return call->conn->security->verify_packet(call, skb, offset, len, |
| 251 | seq, cksum); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Locate the data within a packet. This is complicated by: |
| 256 | * |
| 257 | * (1) An skb may contain a jumbo packet - so we have to find the appropriate |
| 258 | * subpacket. |
| 259 | * |
| 260 | * (2) The (sub)packets may be encrypted and, if so, the encrypted portion |
| 261 | * contains an extra header which includes the true length of the data, |
| 262 | * excluding any encrypted padding. |
| 263 | */ |
| 264 | static int rxrpc_locate_data(struct rxrpc_call *call, struct sk_buff *skb, |
| 265 | u8 *_annotation, |
| 266 | unsigned int *_offset, unsigned int *_len) |
| 267 | { |
David Howells | 775e5b7 | 2016-09-30 13:26:03 +0100 | [diff] [blame] | 268 | unsigned int offset = sizeof(struct rxrpc_wire_header); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 269 | unsigned int len = *_len; |
| 270 | int ret; |
| 271 | u8 annotation = *_annotation; |
| 272 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 273 | /* Locate the subpacket */ |
David Howells | 775e5b7 | 2016-09-30 13:26:03 +0100 | [diff] [blame] | 274 | len = skb->len - offset; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 275 | if ((annotation & RXRPC_RX_ANNO_JUMBO) > 0) { |
| 276 | offset += (((annotation & RXRPC_RX_ANNO_JUMBO) - 1) * |
| 277 | RXRPC_JUMBO_SUBPKTLEN); |
| 278 | len = (annotation & RXRPC_RX_ANNO_JLAST) ? |
| 279 | skb->len - offset : RXRPC_JUMBO_SUBPKTLEN; |
| 280 | } |
| 281 | |
| 282 | if (!(annotation & RXRPC_RX_ANNO_VERIFIED)) { |
| 283 | ret = rxrpc_verify_packet(call, skb, annotation, offset, len); |
| 284 | if (ret < 0) |
| 285 | return ret; |
| 286 | *_annotation |= RXRPC_RX_ANNO_VERIFIED; |
| 287 | } |
| 288 | |
| 289 | *_offset = offset; |
| 290 | *_len = len; |
| 291 | call->conn->security->locate_data(call, skb, _offset, _len); |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * Deliver messages to a call. This keeps processing packets until the buffer |
| 297 | * is filled and we find either more DATA (returns 0) or the end of the DATA |
| 298 | * (returns 1). If more packets are required, it returns -EAGAIN. |
| 299 | */ |
| 300 | static int rxrpc_recvmsg_data(struct socket *sock, struct rxrpc_call *call, |
| 301 | struct msghdr *msg, struct iov_iter *iter, |
| 302 | size_t len, int flags, size_t *_offset) |
| 303 | { |
| 304 | struct rxrpc_skb_priv *sp; |
| 305 | struct sk_buff *skb; |
| 306 | rxrpc_seq_t hard_ack, top, seq; |
| 307 | size_t remain; |
| 308 | bool last; |
| 309 | unsigned int rx_pkt_offset, rx_pkt_len; |
David Howells | 816c9fc | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 310 | int ix, copy, ret = -EAGAIN, ret2; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 311 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 312 | rx_pkt_offset = call->rx_pkt_offset; |
| 313 | rx_pkt_len = call->rx_pkt_len; |
| 314 | |
David Howells | 816c9fc | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 315 | if (call->state >= RXRPC_CALL_SERVER_ACK_REQUEST) { |
| 316 | seq = call->rx_hard_ack; |
| 317 | ret = 1; |
| 318 | goto done; |
| 319 | } |
| 320 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 321 | /* Barriers against rxrpc_input_data(). */ |
| 322 | hard_ack = call->rx_hard_ack; |
| 323 | top = smp_load_acquire(&call->rx_top); |
| 324 | for (seq = hard_ack + 1; before_eq(seq, top); seq++) { |
| 325 | ix = seq & RXRPC_RXTX_BUFF_MASK; |
| 326 | skb = call->rxtx_buffer[ix]; |
David Howells | 8499790 | 2016-09-17 11:13:31 +0100 | [diff] [blame] | 327 | if (!skb) { |
| 328 | trace_rxrpc_recvmsg(call, rxrpc_recvmsg_hole, seq, |
| 329 | rx_pkt_offset, rx_pkt_len, 0); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 330 | break; |
David Howells | 8499790 | 2016-09-17 11:13:31 +0100 | [diff] [blame] | 331 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 332 | smp_rmb(); |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 333 | rxrpc_see_skb(skb, rxrpc_skb_rx_seen); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 334 | sp = rxrpc_skb(skb); |
| 335 | |
David Howells | 58dc63c | 2016-09-17 10:49:13 +0100 | [diff] [blame] | 336 | if (!(flags & MSG_PEEK)) |
| 337 | trace_rxrpc_receive(call, rxrpc_receive_front, |
| 338 | sp->hdr.serial, seq); |
| 339 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 340 | if (msg) |
| 341 | sock_recv_timestamp(msg, sock->sk, skb); |
| 342 | |
David Howells | 2e2ea51 | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 343 | if (rx_pkt_offset == 0) { |
David Howells | 816c9fc | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 344 | ret2 = rxrpc_locate_data(call, skb, |
| 345 | &call->rxtx_annotations[ix], |
| 346 | &rx_pkt_offset, &rx_pkt_len); |
David Howells | 8499790 | 2016-09-17 11:13:31 +0100 | [diff] [blame] | 347 | trace_rxrpc_recvmsg(call, rxrpc_recvmsg_next, seq, |
| 348 | rx_pkt_offset, rx_pkt_len, ret2); |
David Howells | 816c9fc | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 349 | if (ret2 < 0) { |
| 350 | ret = ret2; |
David Howells | 2e2ea51 | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 351 | goto out; |
David Howells | 816c9fc | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 352 | } |
David Howells | 8499790 | 2016-09-17 11:13:31 +0100 | [diff] [blame] | 353 | } else { |
| 354 | trace_rxrpc_recvmsg(call, rxrpc_recvmsg_cont, seq, |
| 355 | rx_pkt_offset, rx_pkt_len, 0); |
David Howells | 2e2ea51 | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 356 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 357 | |
| 358 | /* We have to handle short, empty and used-up DATA packets. */ |
| 359 | remain = len - *_offset; |
| 360 | copy = rx_pkt_len; |
| 361 | if (copy > remain) |
| 362 | copy = remain; |
| 363 | if (copy > 0) { |
David Howells | 816c9fc | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 364 | ret2 = skb_copy_datagram_iter(skb, rx_pkt_offset, iter, |
| 365 | copy); |
| 366 | if (ret2 < 0) { |
| 367 | ret = ret2; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 368 | goto out; |
David Howells | 816c9fc | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 369 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 370 | |
| 371 | /* handle piecemeal consumption of data packets */ |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 372 | rx_pkt_offset += copy; |
| 373 | rx_pkt_len -= copy; |
| 374 | *_offset += copy; |
| 375 | } |
| 376 | |
| 377 | if (rx_pkt_len > 0) { |
David Howells | 8499790 | 2016-09-17 11:13:31 +0100 | [diff] [blame] | 378 | trace_rxrpc_recvmsg(call, rxrpc_recvmsg_full, seq, |
| 379 | rx_pkt_offset, rx_pkt_len, 0); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 380 | ASSERTCMP(*_offset, ==, len); |
David Howells | 816c9fc | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 381 | ret = 0; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 382 | break; |
| 383 | } |
| 384 | |
| 385 | /* The whole packet has been transferred. */ |
| 386 | last = sp->hdr.flags & RXRPC_LAST_PACKET; |
| 387 | if (!(flags & MSG_PEEK)) |
| 388 | rxrpc_rotate_rx_window(call); |
| 389 | rx_pkt_offset = 0; |
| 390 | rx_pkt_len = 0; |
| 391 | |
David Howells | 816c9fc | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 392 | if (last) { |
| 393 | ASSERTCMP(seq, ==, READ_ONCE(call->rx_top)); |
| 394 | ret = 1; |
| 395 | goto out; |
| 396 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 397 | } |
| 398 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 399 | out: |
| 400 | if (!(flags & MSG_PEEK)) { |
| 401 | call->rx_pkt_offset = rx_pkt_offset; |
| 402 | call->rx_pkt_len = rx_pkt_len; |
| 403 | } |
David Howells | 816c9fc | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 404 | done: |
David Howells | 8499790 | 2016-09-17 11:13:31 +0100 | [diff] [blame] | 405 | trace_rxrpc_recvmsg(call, rxrpc_recvmsg_data_return, seq, |
| 406 | rx_pkt_offset, rx_pkt_len, ret); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 407 | return ret; |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * Receive a message from an RxRPC socket |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 412 | * - we need to be careful about two or more threads calling recvmsg |
| 413 | * simultaneously |
| 414 | */ |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 415 | int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, |
| 416 | int flags) |
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 | struct rxrpc_call *call; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 419 | struct rxrpc_sock *rx = rxrpc_sk(sock->sk); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 420 | struct list_head *l; |
| 421 | size_t copied = 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 422 | long timeo; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 423 | int ret; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 424 | |
| 425 | DEFINE_WAIT(wait); |
| 426 | |
David Howells | 8499790 | 2016-09-17 11:13:31 +0100 | [diff] [blame] | 427 | trace_rxrpc_recvmsg(NULL, rxrpc_recvmsg_enter, 0, 0, 0, 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 428 | |
| 429 | if (flags & (MSG_OOB | MSG_TRUNC)) |
| 430 | return -EOPNOTSUPP; |
| 431 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 432 | timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 433 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 434 | try_again: |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 435 | lock_sock(&rx->sk); |
| 436 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 437 | /* Return immediately if a client socket has no outstanding calls */ |
| 438 | if (RB_EMPTY_ROOT(&rx->calls) && |
| 439 | list_empty(&rx->recvmsg_q) && |
| 440 | rx->sk.sk_state != RXRPC_SERVER_LISTENING) { |
| 441 | release_sock(&rx->sk); |
| 442 | return -ENODATA; |
| 443 | } |
| 444 | |
| 445 | if (list_empty(&rx->recvmsg_q)) { |
| 446 | ret = -EWOULDBLOCK; |
David Howells | 8499790 | 2016-09-17 11:13:31 +0100 | [diff] [blame] | 447 | if (timeo == 0) { |
| 448 | call = NULL; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 449 | goto error_no_call; |
David Howells | 8499790 | 2016-09-17 11:13:31 +0100 | [diff] [blame] | 450 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 451 | |
| 452 | release_sock(&rx->sk); |
| 453 | |
| 454 | /* Wait for something to happen */ |
| 455 | prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait, |
| 456 | TASK_INTERRUPTIBLE); |
| 457 | ret = sock_error(&rx->sk); |
| 458 | if (ret) |
| 459 | goto wait_error; |
| 460 | |
| 461 | if (list_empty(&rx->recvmsg_q)) { |
| 462 | if (signal_pending(current)) |
| 463 | goto wait_interrupted; |
David Howells | 8499790 | 2016-09-17 11:13:31 +0100 | [diff] [blame] | 464 | trace_rxrpc_recvmsg(NULL, rxrpc_recvmsg_wait, |
| 465 | 0, 0, 0, 0); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 466 | timeo = schedule_timeout(timeo); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 467 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 468 | finish_wait(sk_sleep(&rx->sk), &wait); |
| 469 | goto try_again; |
| 470 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 471 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 472 | /* Find the next call and dequeue it if we're not just peeking. If we |
| 473 | * do dequeue it, that comes with a ref that we will need to release. |
| 474 | */ |
| 475 | write_lock_bh(&rx->recvmsg_lock); |
| 476 | l = rx->recvmsg_q.next; |
| 477 | call = list_entry(l, struct rxrpc_call, recvmsg_link); |
| 478 | if (!(flags & MSG_PEEK)) |
| 479 | list_del_init(&call->recvmsg_link); |
| 480 | else |
David Howells | fff72429 | 2016-09-07 14:34:21 +0100 | [diff] [blame] | 481 | rxrpc_get_call(call, rxrpc_call_got); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 482 | write_unlock_bh(&rx->recvmsg_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 483 | |
David Howells | 8499790 | 2016-09-17 11:13:31 +0100 | [diff] [blame] | 484 | trace_rxrpc_recvmsg(call, rxrpc_recvmsg_dequeue, 0, 0, 0, 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 485 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 486 | if (test_bit(RXRPC_CALL_RELEASED, &call->flags)) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 487 | BUG(); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 488 | |
| 489 | if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) { |
| 490 | if (flags & MSG_CMSG_COMPAT) { |
| 491 | unsigned int id32 = call->user_call_ID; |
| 492 | |
| 493 | ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID, |
| 494 | sizeof(unsigned int), &id32); |
| 495 | } else { |
| 496 | ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID, |
| 497 | sizeof(unsigned long), |
| 498 | &call->user_call_ID); |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 499 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 500 | if (ret < 0) |
| 501 | goto error; |
| 502 | } |
| 503 | |
| 504 | if (msg->msg_name) { |
| 505 | size_t len = sizeof(call->conn->params.peer->srx); |
| 506 | memcpy(msg->msg_name, &call->conn->params.peer->srx, len); |
| 507 | msg->msg_namelen = len; |
| 508 | } |
| 509 | |
| 510 | switch (call->state) { |
| 511 | case RXRPC_CALL_SERVER_ACCEPTING: |
| 512 | ret = rxrpc_recvmsg_new_call(rx, call, msg, flags); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 513 | break; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 514 | case RXRPC_CALL_CLIENT_RECV_REPLY: |
| 515 | case RXRPC_CALL_SERVER_RECV_REQUEST: |
| 516 | case RXRPC_CALL_SERVER_ACK_REQUEST: |
| 517 | ret = rxrpc_recvmsg_data(sock, call, msg, &msg->msg_iter, len, |
| 518 | flags, &copied); |
| 519 | if (ret == -EAGAIN) |
| 520 | ret = 0; |
David Howells | 33b603f | 2016-09-13 22:36:21 +0100 | [diff] [blame] | 521 | |
| 522 | if (after(call->rx_top, call->rx_hard_ack) && |
| 523 | call->rxtx_buffer[(call->rx_hard_ack + 1) & RXRPC_RXTX_BUFF_MASK]) |
| 524 | rxrpc_notify_socket(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 525 | break; |
| 526 | default: |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 527 | ret = 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 528 | break; |
| 529 | } |
| 530 | |
| 531 | if (ret < 0) |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 532 | goto error; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 533 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 534 | if (call->state == RXRPC_CALL_COMPLETE) { |
| 535 | ret = rxrpc_recvmsg_term(call, msg); |
| 536 | if (ret < 0) |
| 537 | goto error; |
| 538 | if (!(flags & MSG_PEEK)) |
| 539 | rxrpc_release_call(rx, call); |
| 540 | msg->msg_flags |= MSG_EOR; |
| 541 | ret = 1; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 542 | } |
| 543 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 544 | if (ret == 0) |
| 545 | msg->msg_flags |= MSG_MORE; |
| 546 | else |
| 547 | msg->msg_flags &= ~MSG_MORE; |
| 548 | ret = copied; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 549 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 550 | error: |
David Howells | fff72429 | 2016-09-07 14:34:21 +0100 | [diff] [blame] | 551 | rxrpc_put_call(call, rxrpc_call_put); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 552 | error_no_call: |
| 553 | release_sock(&rx->sk); |
David Howells | 8499790 | 2016-09-17 11:13:31 +0100 | [diff] [blame] | 554 | trace_rxrpc_recvmsg(call, rxrpc_recvmsg_return, 0, 0, 0, ret); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 555 | return ret; |
| 556 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 557 | wait_interrupted: |
| 558 | ret = sock_intr_errno(timeo); |
| 559 | wait_error: |
Eric Dumazet | 4a4771a | 2010-04-25 22:20:06 +0000 | [diff] [blame] | 560 | finish_wait(sk_sleep(&rx->sk), &wait); |
David Howells | 8499790 | 2016-09-17 11:13:31 +0100 | [diff] [blame] | 561 | call = NULL; |
| 562 | goto error_no_call; |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 563 | } |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 564 | |
| 565 | /** |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 566 | * rxrpc_kernel_recv_data - Allow a kernel service to receive data/info |
| 567 | * @sock: The socket that the call exists on |
| 568 | * @call: The call to send data through |
| 569 | * @buf: The buffer to receive into |
| 570 | * @size: The size of the buffer, including data already read |
| 571 | * @_offset: The running offset into the buffer. |
| 572 | * @want_more: True if more data is expected to be read |
| 573 | * @_abort: Where the abort code is stored if -ECONNABORTED is returned |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 574 | * |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 575 | * Allow a kernel service to receive data and pick up information about the |
| 576 | * state of a call. Returns 0 if got what was asked for and there's more |
| 577 | * available, 1 if we got what was asked for and we're at the end of the data |
| 578 | * and -EAGAIN if we need more data. |
| 579 | * |
| 580 | * Note that we may return -EAGAIN to drain empty packets at the end of the |
| 581 | * data, even if we've already copied over the requested data. |
| 582 | * |
| 583 | * This function adds the amount it transfers to *_offset, so this should be |
| 584 | * precleared as appropriate. Note that the amount remaining in the buffer is |
| 585 | * taken to be size - *_offset. |
| 586 | * |
| 587 | * *_abort should also be initialised to 0. |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 588 | */ |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 589 | int rxrpc_kernel_recv_data(struct socket *sock, struct rxrpc_call *call, |
| 590 | void *buf, size_t size, size_t *_offset, |
| 591 | bool want_more, u32 *_abort) |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 592 | { |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 593 | struct iov_iter iter; |
| 594 | struct kvec iov; |
| 595 | int ret; |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 596 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 597 | _enter("{%d,%s},%zu/%zu,%d", |
| 598 | call->debug_id, rxrpc_call_states[call->state], |
| 599 | *_offset, size, want_more); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 600 | |
| 601 | ASSERTCMP(*_offset, <=, size); |
| 602 | ASSERTCMP(call->state, !=, RXRPC_CALL_SERVER_ACCEPTING); |
| 603 | |
| 604 | iov.iov_base = buf + *_offset; |
| 605 | iov.iov_len = size - *_offset; |
| 606 | iov_iter_kvec(&iter, ITER_KVEC | READ, &iov, 1, size - *_offset); |
| 607 | |
| 608 | lock_sock(sock->sk); |
| 609 | |
| 610 | switch (call->state) { |
| 611 | case RXRPC_CALL_CLIENT_RECV_REPLY: |
| 612 | case RXRPC_CALL_SERVER_RECV_REQUEST: |
| 613 | case RXRPC_CALL_SERVER_ACK_REQUEST: |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 614 | ret = rxrpc_recvmsg_data(sock, call, NULL, &iter, size, 0, |
| 615 | _offset); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 616 | if (ret < 0) |
| 617 | goto out; |
| 618 | |
| 619 | /* We can only reach here with a partially full buffer if we |
| 620 | * have reached the end of the data. We must otherwise have a |
| 621 | * full buffer or have been given -EAGAIN. |
| 622 | */ |
| 623 | if (ret == 1) { |
| 624 | if (*_offset < size) |
| 625 | goto short_data; |
| 626 | if (!want_more) |
| 627 | goto read_phase_complete; |
| 628 | ret = 0; |
| 629 | goto out; |
| 630 | } |
| 631 | |
| 632 | if (!want_more) |
| 633 | goto excess_data; |
| 634 | goto out; |
| 635 | |
| 636 | case RXRPC_CALL_COMPLETE: |
| 637 | goto call_complete; |
| 638 | |
| 639 | default: |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 640 | ret = -EINPROGRESS; |
| 641 | goto out; |
| 642 | } |
| 643 | |
| 644 | read_phase_complete: |
| 645 | ret = 1; |
| 646 | out: |
| 647 | release_sock(sock->sk); |
| 648 | _leave(" = %d [%zu,%d]", ret, *_offset, *_abort); |
| 649 | return ret; |
| 650 | |
| 651 | short_data: |
| 652 | ret = -EBADMSG; |
| 653 | goto out; |
| 654 | excess_data: |
| 655 | ret = -EMSGSIZE; |
| 656 | goto out; |
| 657 | call_complete: |
| 658 | *_abort = call->abort_code; |
David Howells | cf69207 | 2016-10-06 08:11:50 +0100 | [diff] [blame] | 659 | ret = -call->error; |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 660 | if (call->completion == RXRPC_CALL_SUCCEEDED) { |
| 661 | ret = 1; |
| 662 | if (size > 0) |
| 663 | ret = -ECONNRESET; |
| 664 | } |
| 665 | goto out; |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 666 | } |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 667 | EXPORT_SYMBOL(rxrpc_kernel_recv_data); |