Linux 4.8-rc8
[linux/fpc-iii.git] / net / rxrpc / recvmsg.c
blob9ed66d533002df33c8f0167a6eb43d94d8d4132c
1 /* RxRPC recvmsg() implementation
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/net.h>
15 #include <linux/skbuff.h>
16 #include <linux/export.h>
17 #include <net/sock.h>
18 #include <net/af_rxrpc.h>
19 #include "ar-internal.h"
22 * removal a call's user ID from the socket tree to make the user ID available
23 * again and so that it won't be seen again in association with that call
25 void rxrpc_remove_user_ID(struct rxrpc_sock *rx, struct rxrpc_call *call)
27 _debug("RELEASE CALL %d", call->debug_id);
29 if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
30 write_lock_bh(&rx->call_lock);
31 rb_erase(&call->sock_node, &call->socket->calls);
32 clear_bit(RXRPC_CALL_HAS_USERID, &call->flags);
33 write_unlock_bh(&rx->call_lock);
36 read_lock_bh(&call->state_lock);
37 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
38 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
39 rxrpc_queue_call(call);
40 read_unlock_bh(&call->state_lock);
44 * receive a message from an RxRPC socket
45 * - we need to be careful about two or more threads calling recvmsg
46 * simultaneously
48 int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
49 int flags)
51 struct rxrpc_skb_priv *sp;
52 struct rxrpc_call *call = NULL, *continue_call = NULL;
53 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
54 struct sk_buff *skb;
55 long timeo;
56 int copy, ret, ullen, offset, copied = 0;
57 u32 abort_code;
59 DEFINE_WAIT(wait);
61 _enter(",,,%zu,%d", len, flags);
63 if (flags & (MSG_OOB | MSG_TRUNC))
64 return -EOPNOTSUPP;
66 ullen = msg->msg_flags & MSG_CMSG_COMPAT ? 4 : sizeof(unsigned long);
68 timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT);
69 msg->msg_flags |= MSG_MORE;
71 lock_sock(&rx->sk);
73 for (;;) {
74 /* return immediately if a client socket has no outstanding
75 * calls */
76 if (RB_EMPTY_ROOT(&rx->calls)) {
77 if (copied)
78 goto out;
79 if (rx->sk.sk_state != RXRPC_SERVER_LISTENING) {
80 release_sock(&rx->sk);
81 if (continue_call)
82 rxrpc_put_call(continue_call);
83 return -ENODATA;
87 /* get the next message on the Rx queue */
88 skb = skb_peek(&rx->sk.sk_receive_queue);
89 if (!skb) {
90 /* nothing remains on the queue */
91 if (copied &&
92 (flags & MSG_PEEK || timeo == 0))
93 goto out;
95 /* wait for a message to turn up */
96 release_sock(&rx->sk);
97 prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait,
98 TASK_INTERRUPTIBLE);
99 ret = sock_error(&rx->sk);
100 if (ret)
101 goto wait_error;
103 if (skb_queue_empty(&rx->sk.sk_receive_queue)) {
104 if (signal_pending(current))
105 goto wait_interrupted;
106 timeo = schedule_timeout(timeo);
108 finish_wait(sk_sleep(&rx->sk), &wait);
109 lock_sock(&rx->sk);
110 continue;
113 peek_next_packet:
114 sp = rxrpc_skb(skb);
115 call = sp->call;
116 ASSERT(call != NULL);
118 _debug("next pkt %s", rxrpc_pkts[sp->hdr.type]);
120 /* make sure we wait for the state to be updated in this call */
121 spin_lock_bh(&call->lock);
122 spin_unlock_bh(&call->lock);
124 if (test_bit(RXRPC_CALL_RELEASED, &call->flags)) {
125 _debug("packet from released call");
126 if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
127 BUG();
128 rxrpc_free_skb(skb);
129 continue;
132 /* determine whether to continue last data receive */
133 if (continue_call) {
134 _debug("maybe cont");
135 if (call != continue_call ||
136 skb->mark != RXRPC_SKB_MARK_DATA) {
137 release_sock(&rx->sk);
138 rxrpc_put_call(continue_call);
139 _leave(" = %d [noncont]", copied);
140 return copied;
144 rxrpc_get_call(call);
146 /* copy the peer address and timestamp */
147 if (!continue_call) {
148 if (msg->msg_name) {
149 size_t len =
150 sizeof(call->conn->params.peer->srx);
151 memcpy(msg->msg_name,
152 &call->conn->params.peer->srx, len);
153 msg->msg_namelen = len;
155 sock_recv_timestamp(msg, &rx->sk, skb);
158 /* receive the message */
159 if (skb->mark != RXRPC_SKB_MARK_DATA)
160 goto receive_non_data_message;
162 _debug("recvmsg DATA #%u { %d, %d }",
163 sp->hdr.seq, skb->len, sp->offset);
165 if (!continue_call) {
166 /* only set the control data once per recvmsg() */
167 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
168 ullen, &call->user_call_ID);
169 if (ret < 0)
170 goto copy_error;
171 ASSERT(test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
174 ASSERTCMP(sp->hdr.seq, >=, call->rx_data_recv);
175 ASSERTCMP(sp->hdr.seq, <=, call->rx_data_recv + 1);
176 call->rx_data_recv = sp->hdr.seq;
178 ASSERTCMP(sp->hdr.seq, >, call->rx_data_eaten);
180 offset = sp->offset;
181 copy = skb->len - offset;
182 if (copy > len - copied)
183 copy = len - copied;
185 ret = skb_copy_datagram_msg(skb, offset, msg, copy);
187 if (ret < 0)
188 goto copy_error;
190 /* handle piecemeal consumption of data packets */
191 _debug("copied %d+%d", copy, copied);
193 offset += copy;
194 copied += copy;
196 if (!(flags & MSG_PEEK))
197 sp->offset = offset;
199 if (sp->offset < skb->len) {
200 _debug("buffer full");
201 ASSERTCMP(copied, ==, len);
202 break;
205 /* we transferred the whole data packet */
206 if (!(flags & MSG_PEEK))
207 rxrpc_kernel_data_consumed(call, skb);
209 if (sp->hdr.flags & RXRPC_LAST_PACKET) {
210 _debug("last");
211 if (rxrpc_conn_is_client(call->conn)) {
212 /* last byte of reply received */
213 ret = copied;
214 goto terminal_message;
217 /* last bit of request received */
218 if (!(flags & MSG_PEEK)) {
219 _debug("eat packet");
220 if (skb_dequeue(&rx->sk.sk_receive_queue) !=
221 skb)
222 BUG();
223 rxrpc_free_skb(skb);
225 msg->msg_flags &= ~MSG_MORE;
226 break;
229 /* move on to the next data message */
230 _debug("next");
231 if (!continue_call)
232 continue_call = sp->call;
233 else
234 rxrpc_put_call(call);
235 call = NULL;
237 if (flags & MSG_PEEK) {
238 _debug("peek next");
239 skb = skb->next;
240 if (skb == (struct sk_buff *) &rx->sk.sk_receive_queue)
241 break;
242 goto peek_next_packet;
245 _debug("eat packet");
246 if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
247 BUG();
248 rxrpc_free_skb(skb);
251 /* end of non-terminal data packet reception for the moment */
252 _debug("end rcv data");
253 out:
254 release_sock(&rx->sk);
255 if (call)
256 rxrpc_put_call(call);
257 if (continue_call)
258 rxrpc_put_call(continue_call);
259 _leave(" = %d [data]", copied);
260 return copied;
262 /* handle non-DATA messages such as aborts, incoming connections and
263 * final ACKs */
264 receive_non_data_message:
265 _debug("non-data");
267 if (skb->mark == RXRPC_SKB_MARK_NEW_CALL) {
268 _debug("RECV NEW CALL");
269 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NEW_CALL, 0, &abort_code);
270 if (ret < 0)
271 goto copy_error;
272 if (!(flags & MSG_PEEK)) {
273 if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
274 BUG();
275 rxrpc_free_skb(skb);
277 goto out;
280 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
281 ullen, &call->user_call_ID);
282 if (ret < 0)
283 goto copy_error;
284 ASSERT(test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
286 switch (skb->mark) {
287 case RXRPC_SKB_MARK_DATA:
288 BUG();
289 case RXRPC_SKB_MARK_FINAL_ACK:
290 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ACK, 0, &abort_code);
291 break;
292 case RXRPC_SKB_MARK_BUSY:
293 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_BUSY, 0, &abort_code);
294 break;
295 case RXRPC_SKB_MARK_REMOTE_ABORT:
296 abort_code = call->remote_abort;
297 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &abort_code);
298 break;
299 case RXRPC_SKB_MARK_LOCAL_ABORT:
300 abort_code = call->local_abort;
301 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &abort_code);
302 break;
303 case RXRPC_SKB_MARK_NET_ERROR:
304 _debug("RECV NET ERROR %d", sp->error);
305 abort_code = sp->error;
306 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NET_ERROR, 4, &abort_code);
307 break;
308 case RXRPC_SKB_MARK_LOCAL_ERROR:
309 _debug("RECV LOCAL ERROR %d", sp->error);
310 abort_code = sp->error;
311 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_LOCAL_ERROR, 4,
312 &abort_code);
313 break;
314 default:
315 pr_err("Unknown packet mark %u\n", skb->mark);
316 BUG();
317 break;
320 if (ret < 0)
321 goto copy_error;
323 terminal_message:
324 _debug("terminal");
325 msg->msg_flags &= ~MSG_MORE;
326 msg->msg_flags |= MSG_EOR;
328 if (!(flags & MSG_PEEK)) {
329 _net("free terminal skb %p", skb);
330 if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
331 BUG();
332 rxrpc_free_skb(skb);
333 rxrpc_remove_user_ID(rx, call);
336 release_sock(&rx->sk);
337 rxrpc_put_call(call);
338 if (continue_call)
339 rxrpc_put_call(continue_call);
340 _leave(" = %d", ret);
341 return ret;
343 copy_error:
344 _debug("copy error");
345 release_sock(&rx->sk);
346 rxrpc_put_call(call);
347 if (continue_call)
348 rxrpc_put_call(continue_call);
349 _leave(" = %d", ret);
350 return ret;
352 wait_interrupted:
353 ret = sock_intr_errno(timeo);
354 wait_error:
355 finish_wait(sk_sleep(&rx->sk), &wait);
356 if (continue_call)
357 rxrpc_put_call(continue_call);
358 if (copied)
359 copied = ret;
360 _leave(" = %d [waitfail %d]", copied, ret);
361 return copied;
366 * rxrpc_kernel_is_data_last - Determine if data message is last one
367 * @skb: Message holding data
369 * Determine if data message is last one for the parent call.
371 bool rxrpc_kernel_is_data_last(struct sk_buff *skb)
373 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
375 ASSERTCMP(skb->mark, ==, RXRPC_SKB_MARK_DATA);
377 return sp->hdr.flags & RXRPC_LAST_PACKET;
380 EXPORT_SYMBOL(rxrpc_kernel_is_data_last);
383 * rxrpc_kernel_get_abort_code - Get the abort code from an RxRPC abort message
384 * @skb: Message indicating an abort
386 * Get the abort code from an RxRPC abort message.
388 u32 rxrpc_kernel_get_abort_code(struct sk_buff *skb)
390 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
392 switch (skb->mark) {
393 case RXRPC_SKB_MARK_REMOTE_ABORT:
394 return sp->call->remote_abort;
395 case RXRPC_SKB_MARK_LOCAL_ABORT:
396 return sp->call->local_abort;
397 default:
398 BUG();
402 EXPORT_SYMBOL(rxrpc_kernel_get_abort_code);
405 * rxrpc_kernel_get_error - Get the error number from an RxRPC error message
406 * @skb: Message indicating an error
408 * Get the error number from an RxRPC error message.
410 int rxrpc_kernel_get_error_number(struct sk_buff *skb)
412 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
414 return sp->error;
417 EXPORT_SYMBOL(rxrpc_kernel_get_error_number);