18c3e34a4SDavid Howells /* RxRPC recvmsg() implementation 28c3e34a4SDavid Howells * 38c3e34a4SDavid Howells * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. 48c3e34a4SDavid Howells * Written by David Howells (dhowells@redhat.com) 58c3e34a4SDavid Howells * 68c3e34a4SDavid Howells * This program is free software; you can redistribute it and/or 78c3e34a4SDavid Howells * modify it under the terms of the GNU General Public License 88c3e34a4SDavid Howells * as published by the Free Software Foundation; either version 98c3e34a4SDavid Howells * 2 of the License, or (at your option) any later version. 108c3e34a4SDavid Howells */ 118c3e34a4SDavid Howells 128c3e34a4SDavid Howells #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 138c3e34a4SDavid Howells 148c3e34a4SDavid Howells #include <linux/net.h> 158c3e34a4SDavid Howells #include <linux/skbuff.h> 168c3e34a4SDavid Howells #include <linux/export.h> 178c3e34a4SDavid Howells #include <net/sock.h> 188c3e34a4SDavid Howells #include <net/af_rxrpc.h> 198c3e34a4SDavid Howells #include "ar-internal.h" 208c3e34a4SDavid Howells 218c3e34a4SDavid Howells /* 22248f219cSDavid Howells * Post a call for attention by the socket or kernel service. Further 23248f219cSDavid Howells * notifications are suppressed by putting recvmsg_link on a dummy queue. 24248f219cSDavid Howells */ 25248f219cSDavid Howells void rxrpc_notify_socket(struct rxrpc_call *call) 26248f219cSDavid Howells { 27248f219cSDavid Howells struct rxrpc_sock *rx; 28248f219cSDavid Howells struct sock *sk; 29248f219cSDavid Howells 30248f219cSDavid Howells _enter("%d", call->debug_id); 31248f219cSDavid Howells 32248f219cSDavid Howells if (!list_empty(&call->recvmsg_link)) 33248f219cSDavid Howells return; 34248f219cSDavid Howells 35248f219cSDavid Howells rcu_read_lock(); 36248f219cSDavid Howells 37248f219cSDavid Howells rx = rcu_dereference(call->socket); 38248f219cSDavid Howells sk = &rx->sk; 39248f219cSDavid Howells if (rx && sk->sk_state < RXRPC_CLOSE) { 40248f219cSDavid Howells if (call->notify_rx) { 41248f219cSDavid Howells call->notify_rx(sk, call, call->user_call_ID); 42248f219cSDavid Howells } else { 43248f219cSDavid Howells write_lock_bh(&rx->recvmsg_lock); 44248f219cSDavid Howells if (list_empty(&call->recvmsg_link)) { 45248f219cSDavid Howells rxrpc_get_call(call, rxrpc_call_got); 46248f219cSDavid Howells list_add_tail(&call->recvmsg_link, &rx->recvmsg_q); 47248f219cSDavid Howells } 48248f219cSDavid Howells write_unlock_bh(&rx->recvmsg_lock); 49248f219cSDavid Howells 50248f219cSDavid Howells if (!sock_flag(sk, SOCK_DEAD)) { 51248f219cSDavid Howells _debug("call %ps", sk->sk_data_ready); 52248f219cSDavid Howells sk->sk_data_ready(sk); 53248f219cSDavid Howells } 54248f219cSDavid Howells } 55248f219cSDavid Howells } 56248f219cSDavid Howells 57248f219cSDavid Howells rcu_read_unlock(); 58248f219cSDavid Howells _leave(""); 59248f219cSDavid Howells } 60248f219cSDavid Howells 61248f219cSDavid Howells /* 62248f219cSDavid Howells * Pass a call terminating message to userspace. 63248f219cSDavid Howells */ 64248f219cSDavid Howells static int rxrpc_recvmsg_term(struct rxrpc_call *call, struct msghdr *msg) 65248f219cSDavid Howells { 66248f219cSDavid Howells u32 tmp = 0; 67248f219cSDavid Howells int ret; 68248f219cSDavid Howells 69248f219cSDavid Howells switch (call->completion) { 70248f219cSDavid Howells case RXRPC_CALL_SUCCEEDED: 71248f219cSDavid Howells ret = 0; 72248f219cSDavid Howells if (rxrpc_is_service_call(call)) 73248f219cSDavid Howells ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ACK, 0, &tmp); 74248f219cSDavid Howells break; 75248f219cSDavid Howells case RXRPC_CALL_REMOTELY_ABORTED: 76248f219cSDavid Howells tmp = call->abort_code; 77248f219cSDavid Howells ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &tmp); 78248f219cSDavid Howells break; 79248f219cSDavid Howells case RXRPC_CALL_LOCALLY_ABORTED: 80248f219cSDavid Howells tmp = call->abort_code; 81248f219cSDavid Howells ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &tmp); 82248f219cSDavid Howells break; 83248f219cSDavid Howells case RXRPC_CALL_NETWORK_ERROR: 84248f219cSDavid Howells tmp = call->error; 85248f219cSDavid Howells ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NET_ERROR, 4, &tmp); 86248f219cSDavid Howells break; 87248f219cSDavid Howells case RXRPC_CALL_LOCAL_ERROR: 88248f219cSDavid Howells tmp = call->error; 89248f219cSDavid Howells ret = put_cmsg(msg, SOL_RXRPC, RXRPC_LOCAL_ERROR, 4, &tmp); 90248f219cSDavid Howells break; 91248f219cSDavid Howells default: 92248f219cSDavid Howells pr_err("Invalid terminal call state %u\n", call->state); 93248f219cSDavid Howells BUG(); 94248f219cSDavid Howells break; 95248f219cSDavid Howells } 96248f219cSDavid Howells 97248f219cSDavid Howells return ret; 98248f219cSDavid Howells } 99248f219cSDavid Howells 100248f219cSDavid Howells /* 101248f219cSDavid Howells * Pass back notification of a new call. The call is added to the 102248f219cSDavid Howells * to-be-accepted list. This means that the next call to be accepted might not 103248f219cSDavid Howells * be the last call seen awaiting acceptance, but unless we leave this on the 104248f219cSDavid Howells * front of the queue and block all other messages until someone gives us a 105248f219cSDavid Howells * user_ID for it, there's not a lot we can do. 106248f219cSDavid Howells */ 107248f219cSDavid Howells static int rxrpc_recvmsg_new_call(struct rxrpc_sock *rx, 108248f219cSDavid Howells struct rxrpc_call *call, 109248f219cSDavid Howells struct msghdr *msg, int flags) 110248f219cSDavid Howells { 111248f219cSDavid Howells int tmp = 0, ret; 112248f219cSDavid Howells 113248f219cSDavid Howells ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NEW_CALL, 0, &tmp); 114248f219cSDavid Howells 115248f219cSDavid Howells if (ret == 0 && !(flags & MSG_PEEK)) { 116248f219cSDavid Howells _debug("to be accepted"); 117248f219cSDavid Howells write_lock_bh(&rx->recvmsg_lock); 118248f219cSDavid Howells list_del_init(&call->recvmsg_link); 119248f219cSDavid Howells write_unlock_bh(&rx->recvmsg_lock); 120248f219cSDavid Howells 1213432a757SDavid Howells rxrpc_get_call(call, rxrpc_call_got); 122248f219cSDavid Howells write_lock(&rx->call_lock); 123248f219cSDavid Howells list_add_tail(&call->accept_link, &rx->to_be_accepted); 124248f219cSDavid Howells write_unlock(&rx->call_lock); 125248f219cSDavid Howells } 126248f219cSDavid Howells 127248f219cSDavid Howells return ret; 128248f219cSDavid Howells } 129248f219cSDavid Howells 130248f219cSDavid Howells /* 131248f219cSDavid Howells * End the packet reception phase. 132248f219cSDavid Howells */ 133248f219cSDavid Howells static void rxrpc_end_rx_phase(struct rxrpc_call *call) 134248f219cSDavid Howells { 135248f219cSDavid Howells _enter("%d,%s", call->debug_id, rxrpc_call_states[call->state]); 136248f219cSDavid Howells 137248f219cSDavid Howells if (call->state == RXRPC_CALL_CLIENT_RECV_REPLY) { 138248f219cSDavid Howells rxrpc_propose_ACK(call, RXRPC_ACK_IDLE, 0, 0, true, false); 139248f219cSDavid Howells rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ACK); 140248f219cSDavid Howells } else { 141248f219cSDavid Howells rxrpc_propose_ACK(call, RXRPC_ACK_IDLE, 0, 0, false, false); 142248f219cSDavid Howells } 143248f219cSDavid Howells 144248f219cSDavid Howells write_lock_bh(&call->state_lock); 145248f219cSDavid Howells 146248f219cSDavid Howells switch (call->state) { 147248f219cSDavid Howells case RXRPC_CALL_CLIENT_RECV_REPLY: 148248f219cSDavid Howells __rxrpc_call_completed(call); 149248f219cSDavid Howells break; 150248f219cSDavid Howells 151248f219cSDavid Howells case RXRPC_CALL_SERVER_RECV_REQUEST: 152248f219cSDavid Howells call->state = RXRPC_CALL_SERVER_ACK_REQUEST; 153248f219cSDavid Howells break; 154248f219cSDavid Howells default: 155248f219cSDavid Howells break; 156248f219cSDavid Howells } 157248f219cSDavid Howells 158248f219cSDavid Howells write_unlock_bh(&call->state_lock); 159248f219cSDavid Howells } 160248f219cSDavid Howells 161248f219cSDavid Howells /* 162248f219cSDavid Howells * Discard a packet we've used up and advance the Rx window by one. 163248f219cSDavid Howells */ 164248f219cSDavid Howells static void rxrpc_rotate_rx_window(struct rxrpc_call *call) 165248f219cSDavid Howells { 166248f219cSDavid Howells struct sk_buff *skb; 167248f219cSDavid Howells rxrpc_seq_t hard_ack, top; 168248f219cSDavid Howells int ix; 169248f219cSDavid Howells 170248f219cSDavid Howells _enter("%d", call->debug_id); 171248f219cSDavid Howells 172248f219cSDavid Howells hard_ack = call->rx_hard_ack; 173248f219cSDavid Howells top = smp_load_acquire(&call->rx_top); 174248f219cSDavid Howells ASSERT(before(hard_ack, top)); 175248f219cSDavid Howells 176248f219cSDavid Howells hard_ack++; 177248f219cSDavid Howells ix = hard_ack & RXRPC_RXTX_BUFF_MASK; 178248f219cSDavid Howells skb = call->rxtx_buffer[ix]; 179248f219cSDavid Howells rxrpc_see_skb(skb); 180248f219cSDavid Howells call->rxtx_buffer[ix] = NULL; 181248f219cSDavid Howells call->rxtx_annotations[ix] = 0; 182248f219cSDavid Howells /* Barrier against rxrpc_input_data(). */ 183248f219cSDavid Howells smp_store_release(&call->rx_hard_ack, hard_ack); 184248f219cSDavid Howells 185248f219cSDavid Howells rxrpc_free_skb(skb); 186248f219cSDavid Howells 187248f219cSDavid Howells _debug("%u,%u,%lx", hard_ack, top, call->flags); 188248f219cSDavid Howells if (hard_ack == top && test_bit(RXRPC_CALL_RX_LAST, &call->flags)) 189248f219cSDavid Howells rxrpc_end_rx_phase(call); 190248f219cSDavid Howells } 191248f219cSDavid Howells 192248f219cSDavid Howells /* 193248f219cSDavid Howells * Decrypt and verify a (sub)packet. The packet's length may be changed due to 194248f219cSDavid Howells * padding, but if this is the case, the packet length will be resident in the 195248f219cSDavid Howells * socket buffer. Note that we can't modify the master skb info as the skb may 196248f219cSDavid Howells * be the home to multiple subpackets. 197248f219cSDavid Howells */ 198248f219cSDavid Howells static int rxrpc_verify_packet(struct rxrpc_call *call, struct sk_buff *skb, 199248f219cSDavid Howells u8 annotation, 200248f219cSDavid Howells unsigned int offset, unsigned int len) 201248f219cSDavid Howells { 202248f219cSDavid Howells struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 203248f219cSDavid Howells rxrpc_seq_t seq = sp->hdr.seq; 204248f219cSDavid Howells u16 cksum = sp->hdr.cksum; 205248f219cSDavid Howells 206248f219cSDavid Howells _enter(""); 207248f219cSDavid Howells 208248f219cSDavid Howells /* For all but the head jumbo subpacket, the security checksum is in a 209248f219cSDavid Howells * jumbo header immediately prior to the data. 210248f219cSDavid Howells */ 211248f219cSDavid Howells if ((annotation & RXRPC_RX_ANNO_JUMBO) > 1) { 212248f219cSDavid Howells __be16 tmp; 213248f219cSDavid Howells if (skb_copy_bits(skb, offset - 2, &tmp, 2) < 0) 214248f219cSDavid Howells BUG(); 215248f219cSDavid Howells cksum = ntohs(tmp); 216248f219cSDavid Howells seq += (annotation & RXRPC_RX_ANNO_JUMBO) - 1; 217248f219cSDavid Howells } 218248f219cSDavid Howells 219248f219cSDavid Howells return call->conn->security->verify_packet(call, skb, offset, len, 220248f219cSDavid Howells seq, cksum); 221248f219cSDavid Howells } 222248f219cSDavid Howells 223248f219cSDavid Howells /* 224248f219cSDavid Howells * Locate the data within a packet. This is complicated by: 225248f219cSDavid Howells * 226248f219cSDavid Howells * (1) An skb may contain a jumbo packet - so we have to find the appropriate 227248f219cSDavid Howells * subpacket. 228248f219cSDavid Howells * 229248f219cSDavid Howells * (2) The (sub)packets may be encrypted and, if so, the encrypted portion 230248f219cSDavid Howells * contains an extra header which includes the true length of the data, 231248f219cSDavid Howells * excluding any encrypted padding. 232248f219cSDavid Howells */ 233248f219cSDavid Howells static int rxrpc_locate_data(struct rxrpc_call *call, struct sk_buff *skb, 234248f219cSDavid Howells u8 *_annotation, 235248f219cSDavid Howells unsigned int *_offset, unsigned int *_len) 236248f219cSDavid Howells { 237248f219cSDavid Howells struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 238248f219cSDavid Howells unsigned int offset = *_offset; 239248f219cSDavid Howells unsigned int len = *_len; 240248f219cSDavid Howells int ret; 241248f219cSDavid Howells u8 annotation = *_annotation; 242248f219cSDavid Howells 243248f219cSDavid Howells /* Locate the subpacket */ 244248f219cSDavid Howells offset = sp->offset; 245248f219cSDavid Howells len = skb->len - sp->offset; 246248f219cSDavid Howells if ((annotation & RXRPC_RX_ANNO_JUMBO) > 0) { 247248f219cSDavid Howells offset += (((annotation & RXRPC_RX_ANNO_JUMBO) - 1) * 248248f219cSDavid Howells RXRPC_JUMBO_SUBPKTLEN); 249248f219cSDavid Howells len = (annotation & RXRPC_RX_ANNO_JLAST) ? 250248f219cSDavid Howells skb->len - offset : RXRPC_JUMBO_SUBPKTLEN; 251248f219cSDavid Howells } 252248f219cSDavid Howells 253248f219cSDavid Howells if (!(annotation & RXRPC_RX_ANNO_VERIFIED)) { 254248f219cSDavid Howells ret = rxrpc_verify_packet(call, skb, annotation, offset, len); 255248f219cSDavid Howells if (ret < 0) 256248f219cSDavid Howells return ret; 257248f219cSDavid Howells *_annotation |= RXRPC_RX_ANNO_VERIFIED; 258248f219cSDavid Howells } 259248f219cSDavid Howells 260248f219cSDavid Howells *_offset = offset; 261248f219cSDavid Howells *_len = len; 262248f219cSDavid Howells call->conn->security->locate_data(call, skb, _offset, _len); 263248f219cSDavid Howells return 0; 264248f219cSDavid Howells } 265248f219cSDavid Howells 266248f219cSDavid Howells /* 267248f219cSDavid Howells * Deliver messages to a call. This keeps processing packets until the buffer 268248f219cSDavid Howells * is filled and we find either more DATA (returns 0) or the end of the DATA 269248f219cSDavid Howells * (returns 1). If more packets are required, it returns -EAGAIN. 270248f219cSDavid Howells */ 271248f219cSDavid Howells static int rxrpc_recvmsg_data(struct socket *sock, struct rxrpc_call *call, 272248f219cSDavid Howells struct msghdr *msg, struct iov_iter *iter, 273248f219cSDavid Howells size_t len, int flags, size_t *_offset) 274248f219cSDavid Howells { 275248f219cSDavid Howells struct rxrpc_skb_priv *sp; 276248f219cSDavid Howells struct sk_buff *skb; 277248f219cSDavid Howells rxrpc_seq_t hard_ack, top, seq; 278248f219cSDavid Howells size_t remain; 279248f219cSDavid Howells bool last; 280248f219cSDavid Howells unsigned int rx_pkt_offset, rx_pkt_len; 281248f219cSDavid Howells int ix, copy, ret = 0; 282248f219cSDavid Howells 283248f219cSDavid Howells _enter(""); 284248f219cSDavid Howells 285248f219cSDavid Howells rx_pkt_offset = call->rx_pkt_offset; 286248f219cSDavid Howells rx_pkt_len = call->rx_pkt_len; 287248f219cSDavid Howells 288248f219cSDavid Howells /* Barriers against rxrpc_input_data(). */ 289248f219cSDavid Howells hard_ack = call->rx_hard_ack; 290248f219cSDavid Howells top = smp_load_acquire(&call->rx_top); 291248f219cSDavid Howells for (seq = hard_ack + 1; before_eq(seq, top); seq++) { 292248f219cSDavid Howells ix = seq & RXRPC_RXTX_BUFF_MASK; 293248f219cSDavid Howells skb = call->rxtx_buffer[ix]; 294248f219cSDavid Howells if (!skb) 295248f219cSDavid Howells break; 296248f219cSDavid Howells smp_rmb(); 297248f219cSDavid Howells rxrpc_see_skb(skb); 298248f219cSDavid Howells sp = rxrpc_skb(skb); 299248f219cSDavid Howells 300248f219cSDavid Howells if (msg) 301248f219cSDavid Howells sock_recv_timestamp(msg, sock->sk, skb); 302248f219cSDavid Howells 3032e2ea51dSDavid Howells if (rx_pkt_offset == 0) { 3044b22457cSDavid Howells ret = rxrpc_locate_data(call, skb, 3054b22457cSDavid Howells &call->rxtx_annotations[ix], 306248f219cSDavid Howells &rx_pkt_offset, &rx_pkt_len); 3072e2ea51dSDavid Howells if (ret < 0) 3082e2ea51dSDavid Howells goto out; 3092e2ea51dSDavid Howells } 310248f219cSDavid Howells _debug("recvmsg %x DATA #%u { %d, %d }", 311248f219cSDavid Howells sp->hdr.callNumber, seq, rx_pkt_offset, rx_pkt_len); 312248f219cSDavid Howells 313248f219cSDavid Howells /* We have to handle short, empty and used-up DATA packets. */ 314248f219cSDavid Howells remain = len - *_offset; 315248f219cSDavid Howells copy = rx_pkt_len; 316248f219cSDavid Howells if (copy > remain) 317248f219cSDavid Howells copy = remain; 318248f219cSDavid Howells if (copy > 0) { 319248f219cSDavid Howells ret = skb_copy_datagram_iter(skb, rx_pkt_offset, iter, 320248f219cSDavid Howells copy); 321248f219cSDavid Howells if (ret < 0) 322248f219cSDavid Howells goto out; 323248f219cSDavid Howells 324248f219cSDavid Howells /* handle piecemeal consumption of data packets */ 325248f219cSDavid Howells _debug("copied %d @%zu", copy, *_offset); 326248f219cSDavid Howells 327248f219cSDavid Howells rx_pkt_offset += copy; 328248f219cSDavid Howells rx_pkt_len -= copy; 329248f219cSDavid Howells *_offset += copy; 330248f219cSDavid Howells } 331248f219cSDavid Howells 332248f219cSDavid Howells if (rx_pkt_len > 0) { 333248f219cSDavid Howells _debug("buffer full"); 334248f219cSDavid Howells ASSERTCMP(*_offset, ==, len); 335248f219cSDavid Howells break; 336248f219cSDavid Howells } 337248f219cSDavid Howells 338248f219cSDavid Howells /* The whole packet has been transferred. */ 339248f219cSDavid Howells last = sp->hdr.flags & RXRPC_LAST_PACKET; 340248f219cSDavid Howells if (!(flags & MSG_PEEK)) 341248f219cSDavid Howells rxrpc_rotate_rx_window(call); 342248f219cSDavid Howells rx_pkt_offset = 0; 343248f219cSDavid Howells rx_pkt_len = 0; 344248f219cSDavid Howells 345248f219cSDavid Howells ASSERTIFCMP(last, seq, ==, top); 346248f219cSDavid Howells } 347248f219cSDavid Howells 348248f219cSDavid Howells if (after(seq, top)) { 349248f219cSDavid Howells ret = -EAGAIN; 350248f219cSDavid Howells if (test_bit(RXRPC_CALL_RX_LAST, &call->flags)) 351248f219cSDavid Howells ret = 1; 352248f219cSDavid Howells } 353248f219cSDavid Howells out: 354248f219cSDavid Howells if (!(flags & MSG_PEEK)) { 355248f219cSDavid Howells call->rx_pkt_offset = rx_pkt_offset; 356248f219cSDavid Howells call->rx_pkt_len = rx_pkt_len; 357248f219cSDavid Howells } 358248f219cSDavid Howells _leave(" = %d [%u/%u]", ret, seq, top); 359248f219cSDavid Howells return ret; 360248f219cSDavid Howells } 361248f219cSDavid Howells 362248f219cSDavid Howells /* 363248f219cSDavid Howells * Receive a message from an RxRPC socket 3648c3e34a4SDavid Howells * - we need to be careful about two or more threads calling recvmsg 3658c3e34a4SDavid Howells * simultaneously 3668c3e34a4SDavid Howells */ 3678c3e34a4SDavid Howells int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, 3688c3e34a4SDavid Howells int flags) 3698c3e34a4SDavid Howells { 370248f219cSDavid Howells struct rxrpc_call *call; 3718c3e34a4SDavid Howells struct rxrpc_sock *rx = rxrpc_sk(sock->sk); 372248f219cSDavid Howells struct list_head *l; 373248f219cSDavid Howells size_t copied = 0; 3748c3e34a4SDavid Howells long timeo; 375248f219cSDavid Howells int ret; 3768c3e34a4SDavid Howells 3778c3e34a4SDavid Howells DEFINE_WAIT(wait); 3788c3e34a4SDavid Howells 3798c3e34a4SDavid Howells _enter(",,,%zu,%d", len, flags); 3808c3e34a4SDavid Howells 3818c3e34a4SDavid Howells if (flags & (MSG_OOB | MSG_TRUNC)) 3828c3e34a4SDavid Howells return -EOPNOTSUPP; 3838c3e34a4SDavid Howells 3848c3e34a4SDavid Howells timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT); 3858c3e34a4SDavid Howells 386248f219cSDavid Howells try_again: 3878c3e34a4SDavid Howells lock_sock(&rx->sk); 3888c3e34a4SDavid Howells 389248f219cSDavid Howells /* Return immediately if a client socket has no outstanding calls */ 390248f219cSDavid Howells if (RB_EMPTY_ROOT(&rx->calls) && 391248f219cSDavid Howells list_empty(&rx->recvmsg_q) && 392248f219cSDavid Howells rx->sk.sk_state != RXRPC_SERVER_LISTENING) { 3938c3e34a4SDavid Howells release_sock(&rx->sk); 3948c3e34a4SDavid Howells return -ENODATA; 3958c3e34a4SDavid Howells } 3968c3e34a4SDavid Howells 397248f219cSDavid Howells if (list_empty(&rx->recvmsg_q)) { 398248f219cSDavid Howells ret = -EWOULDBLOCK; 399248f219cSDavid Howells if (timeo == 0) 400248f219cSDavid Howells goto error_no_call; 4018c3e34a4SDavid Howells 4028c3e34a4SDavid Howells release_sock(&rx->sk); 403248f219cSDavid Howells 404248f219cSDavid Howells /* Wait for something to happen */ 4058c3e34a4SDavid Howells prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait, 4068c3e34a4SDavid Howells TASK_INTERRUPTIBLE); 4078c3e34a4SDavid Howells ret = sock_error(&rx->sk); 4088c3e34a4SDavid Howells if (ret) 4098c3e34a4SDavid Howells goto wait_error; 4108c3e34a4SDavid Howells 411248f219cSDavid Howells if (list_empty(&rx->recvmsg_q)) { 4128c3e34a4SDavid Howells if (signal_pending(current)) 4138c3e34a4SDavid Howells goto wait_interrupted; 4148c3e34a4SDavid Howells timeo = schedule_timeout(timeo); 4158c3e34a4SDavid Howells } 4168c3e34a4SDavid Howells finish_wait(sk_sleep(&rx->sk), &wait); 417248f219cSDavid Howells goto try_again; 4188c3e34a4SDavid Howells } 4198c3e34a4SDavid Howells 420248f219cSDavid Howells /* Find the next call and dequeue it if we're not just peeking. If we 421248f219cSDavid Howells * do dequeue it, that comes with a ref that we will need to release. 422248f219cSDavid Howells */ 423248f219cSDavid Howells write_lock_bh(&rx->recvmsg_lock); 424248f219cSDavid Howells l = rx->recvmsg_q.next; 425248f219cSDavid Howells call = list_entry(l, struct rxrpc_call, recvmsg_link); 426248f219cSDavid Howells if (!(flags & MSG_PEEK)) 427248f219cSDavid Howells list_del_init(&call->recvmsg_link); 428248f219cSDavid Howells else 429fff72429SDavid Howells rxrpc_get_call(call, rxrpc_call_got); 430248f219cSDavid Howells write_unlock_bh(&rx->recvmsg_lock); 4318c3e34a4SDavid Howells 432248f219cSDavid Howells _debug("recvmsg call %p", call); 433248f219cSDavid Howells 434248f219cSDavid Howells if (test_bit(RXRPC_CALL_RELEASED, &call->flags)) 435248f219cSDavid Howells BUG(); 436248f219cSDavid Howells 437248f219cSDavid Howells if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) { 438248f219cSDavid Howells if (flags & MSG_CMSG_COMPAT) { 439248f219cSDavid Howells unsigned int id32 = call->user_call_ID; 440248f219cSDavid Howells 441248f219cSDavid Howells ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID, 442248f219cSDavid Howells sizeof(unsigned int), &id32); 443248f219cSDavid Howells } else { 444248f219cSDavid Howells ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID, 445248f219cSDavid Howells sizeof(unsigned long), 446248f219cSDavid Howells &call->user_call_ID); 447248f219cSDavid Howells } 448248f219cSDavid Howells if (ret < 0) 449248f219cSDavid Howells goto error; 450248f219cSDavid Howells } 451248f219cSDavid Howells 4528c3e34a4SDavid Howells if (msg->msg_name) { 453248f219cSDavid Howells size_t len = sizeof(call->conn->params.peer->srx); 454248f219cSDavid Howells memcpy(msg->msg_name, &call->conn->params.peer->srx, len); 4558c3e34a4SDavid Howells msg->msg_namelen = len; 4568c3e34a4SDavid Howells } 4578c3e34a4SDavid Howells 458248f219cSDavid Howells switch (call->state) { 459248f219cSDavid Howells case RXRPC_CALL_SERVER_ACCEPTING: 460248f219cSDavid Howells ret = rxrpc_recvmsg_new_call(rx, call, msg, flags); 4618c3e34a4SDavid Howells break; 462248f219cSDavid Howells case RXRPC_CALL_CLIENT_RECV_REPLY: 463248f219cSDavid Howells case RXRPC_CALL_SERVER_RECV_REQUEST: 464248f219cSDavid Howells case RXRPC_CALL_SERVER_ACK_REQUEST: 465248f219cSDavid Howells ret = rxrpc_recvmsg_data(sock, call, msg, &msg->msg_iter, len, 466248f219cSDavid Howells flags, &copied); 467248f219cSDavid Howells if (ret == -EAGAIN) 468248f219cSDavid Howells ret = 0; 46933b603fdSDavid Howells 47033b603fdSDavid Howells if (after(call->rx_top, call->rx_hard_ack) && 47133b603fdSDavid Howells call->rxtx_buffer[(call->rx_hard_ack + 1) & RXRPC_RXTX_BUFF_MASK]) 47233b603fdSDavid Howells rxrpc_notify_socket(call); 4738c3e34a4SDavid Howells break; 4748c3e34a4SDavid Howells default: 475248f219cSDavid Howells ret = 0; 4768c3e34a4SDavid Howells break; 4778c3e34a4SDavid Howells } 4788c3e34a4SDavid Howells 4798c3e34a4SDavid Howells if (ret < 0) 480248f219cSDavid Howells goto error; 4818c3e34a4SDavid Howells 482248f219cSDavid Howells if (call->state == RXRPC_CALL_COMPLETE) { 483248f219cSDavid Howells ret = rxrpc_recvmsg_term(call, msg); 484248f219cSDavid Howells if (ret < 0) 485248f219cSDavid Howells goto error; 486248f219cSDavid Howells if (!(flags & MSG_PEEK)) 4878d94aa38SDavid Howells rxrpc_release_call(rx, call); 488248f219cSDavid Howells msg->msg_flags |= MSG_EOR; 489248f219cSDavid Howells ret = 1; 4908c3e34a4SDavid Howells } 4918c3e34a4SDavid Howells 492248f219cSDavid Howells if (ret == 0) 493248f219cSDavid Howells msg->msg_flags |= MSG_MORE; 494248f219cSDavid Howells else 495248f219cSDavid Howells msg->msg_flags &= ~MSG_MORE; 496248f219cSDavid Howells ret = copied; 4978c3e34a4SDavid Howells 498248f219cSDavid Howells error: 499fff72429SDavid Howells rxrpc_put_call(call, rxrpc_call_put); 500248f219cSDavid Howells error_no_call: 501248f219cSDavid Howells release_sock(&rx->sk); 5028c3e34a4SDavid Howells _leave(" = %d", ret); 5038c3e34a4SDavid Howells return ret; 5048c3e34a4SDavid Howells 5058c3e34a4SDavid Howells wait_interrupted: 5068c3e34a4SDavid Howells ret = sock_intr_errno(timeo); 5078c3e34a4SDavid Howells wait_error: 5088c3e34a4SDavid Howells finish_wait(sk_sleep(&rx->sk), &wait); 509248f219cSDavid Howells release_sock(&rx->sk); 510248f219cSDavid Howells _leave(" = %d [wait]", ret); 511d001648eSDavid Howells return ret; 512d001648eSDavid Howells } 5138c3e34a4SDavid Howells 5148c3e34a4SDavid Howells /** 515d001648eSDavid Howells * rxrpc_kernel_recv_data - Allow a kernel service to receive data/info 516d001648eSDavid Howells * @sock: The socket that the call exists on 517d001648eSDavid Howells * @call: The call to send data through 518d001648eSDavid Howells * @buf: The buffer to receive into 519d001648eSDavid Howells * @size: The size of the buffer, including data already read 520d001648eSDavid Howells * @_offset: The running offset into the buffer. 521d001648eSDavid Howells * @want_more: True if more data is expected to be read 522d001648eSDavid Howells * @_abort: Where the abort code is stored if -ECONNABORTED is returned 5238c3e34a4SDavid Howells * 524d001648eSDavid Howells * Allow a kernel service to receive data and pick up information about the 525d001648eSDavid Howells * state of a call. Returns 0 if got what was asked for and there's more 526d001648eSDavid Howells * available, 1 if we got what was asked for and we're at the end of the data 527d001648eSDavid Howells * and -EAGAIN if we need more data. 528d001648eSDavid Howells * 529d001648eSDavid Howells * Note that we may return -EAGAIN to drain empty packets at the end of the 530d001648eSDavid Howells * data, even if we've already copied over the requested data. 531d001648eSDavid Howells * 532d001648eSDavid Howells * This function adds the amount it transfers to *_offset, so this should be 533d001648eSDavid Howells * precleared as appropriate. Note that the amount remaining in the buffer is 534d001648eSDavid Howells * taken to be size - *_offset. 535d001648eSDavid Howells * 536d001648eSDavid Howells * *_abort should also be initialised to 0. 5378c3e34a4SDavid Howells */ 538d001648eSDavid Howells int rxrpc_kernel_recv_data(struct socket *sock, struct rxrpc_call *call, 539d001648eSDavid Howells void *buf, size_t size, size_t *_offset, 540d001648eSDavid Howells bool want_more, u32 *_abort) 5418c3e34a4SDavid Howells { 542d001648eSDavid Howells struct iov_iter iter; 543d001648eSDavid Howells struct kvec iov; 544d001648eSDavid Howells int ret; 5458c3e34a4SDavid Howells 546248f219cSDavid Howells _enter("{%d,%s},%zu/%zu,%d", 547248f219cSDavid Howells call->debug_id, rxrpc_call_states[call->state], 548248f219cSDavid Howells *_offset, size, want_more); 549d001648eSDavid Howells 550d001648eSDavid Howells ASSERTCMP(*_offset, <=, size); 551d001648eSDavid Howells ASSERTCMP(call->state, !=, RXRPC_CALL_SERVER_ACCEPTING); 552d001648eSDavid Howells 553d001648eSDavid Howells iov.iov_base = buf + *_offset; 554d001648eSDavid Howells iov.iov_len = size - *_offset; 555d001648eSDavid Howells iov_iter_kvec(&iter, ITER_KVEC | READ, &iov, 1, size - *_offset); 556d001648eSDavid Howells 557d001648eSDavid Howells lock_sock(sock->sk); 558d001648eSDavid Howells 559d001648eSDavid Howells switch (call->state) { 560d001648eSDavid Howells case RXRPC_CALL_CLIENT_RECV_REPLY: 561d001648eSDavid Howells case RXRPC_CALL_SERVER_RECV_REQUEST: 562d001648eSDavid Howells case RXRPC_CALL_SERVER_ACK_REQUEST: 563248f219cSDavid Howells ret = rxrpc_recvmsg_data(sock, call, NULL, &iter, size, 0, 564248f219cSDavid Howells _offset); 565d001648eSDavid Howells if (ret < 0) 566d001648eSDavid Howells goto out; 567d001648eSDavid Howells 568d001648eSDavid Howells /* We can only reach here with a partially full buffer if we 569d001648eSDavid Howells * have reached the end of the data. We must otherwise have a 570d001648eSDavid Howells * full buffer or have been given -EAGAIN. 571d001648eSDavid Howells */ 572d001648eSDavid Howells if (ret == 1) { 573d001648eSDavid Howells if (*_offset < size) 574d001648eSDavid Howells goto short_data; 575d001648eSDavid Howells if (!want_more) 576d001648eSDavid Howells goto read_phase_complete; 577d001648eSDavid Howells ret = 0; 578d001648eSDavid Howells goto out; 5798c3e34a4SDavid Howells } 5808c3e34a4SDavid Howells 581d001648eSDavid Howells if (!want_more) 582d001648eSDavid Howells goto excess_data; 583d001648eSDavid Howells goto out; 584d001648eSDavid Howells 585d001648eSDavid Howells case RXRPC_CALL_COMPLETE: 586d001648eSDavid Howells goto call_complete; 587d001648eSDavid Howells 588d001648eSDavid Howells default: 589d001648eSDavid Howells ret = -EINPROGRESS; 590d001648eSDavid Howells goto out; 591d001648eSDavid Howells } 592d001648eSDavid Howells 593d001648eSDavid Howells read_phase_complete: 594d001648eSDavid Howells ret = 1; 595d001648eSDavid Howells out: 596d001648eSDavid Howells release_sock(sock->sk); 597d001648eSDavid Howells _leave(" = %d [%zu,%d]", ret, *_offset, *_abort); 598d001648eSDavid Howells return ret; 599d001648eSDavid Howells 600d001648eSDavid Howells short_data: 601d001648eSDavid Howells ret = -EBADMSG; 602d001648eSDavid Howells goto out; 603d001648eSDavid Howells excess_data: 604d001648eSDavid Howells ret = -EMSGSIZE; 605d001648eSDavid Howells goto out; 606d001648eSDavid Howells call_complete: 607d001648eSDavid Howells *_abort = call->abort_code; 608d001648eSDavid Howells ret = call->error; 609d001648eSDavid Howells if (call->completion == RXRPC_CALL_SUCCEEDED) { 610d001648eSDavid Howells ret = 1; 611d001648eSDavid Howells if (size > 0) 612d001648eSDavid Howells ret = -ECONNRESET; 613d001648eSDavid Howells } 614d001648eSDavid Howells goto out; 615d001648eSDavid Howells } 616d001648eSDavid Howells EXPORT_SYMBOL(rxrpc_kernel_recv_data); 617