iwlwifi: mvm: use LIST_HEAD() macro
[linux-2.6/btrfs-unstable.git] / net / rxrpc / recvmsg.c
bloba284205b8ecf9b1fc5ae57cd9428786d57a31393
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 * Post a call for attention by the socket or kernel service. Further
23 * notifications are suppressed by putting recvmsg_link on a dummy queue.
25 void rxrpc_notify_socket(struct rxrpc_call *call)
27 struct rxrpc_sock *rx;
28 struct sock *sk;
30 _enter("%d", call->debug_id);
32 if (!list_empty(&call->recvmsg_link))
33 return;
35 rcu_read_lock();
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);
48 write_unlock_bh(&rx->recvmsg_lock);
50 if (!sock_flag(sk, SOCK_DEAD)) {
51 _debug("call %ps", sk->sk_data_ready);
52 sk->sk_data_ready(sk);
57 rcu_read_unlock();
58 _leave("");
62 * Pass a call terminating message to userspace.
64 static int rxrpc_recvmsg_term(struct rxrpc_call *call, struct msghdr *msg)
66 u32 tmp = 0;
67 int ret;
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;
97 return ret;
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.
107 static int rxrpc_recvmsg_new_call(struct rxrpc_sock *rx,
108 struct rxrpc_call *call,
109 struct msghdr *msg, int flags)
111 int tmp = 0, ret;
113 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NEW_CALL, 0, &tmp);
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);
121 rxrpc_get_call(call, rxrpc_call_got);
122 write_lock(&rx->call_lock);
123 list_add_tail(&call->accept_link, &rx->to_be_accepted);
124 write_unlock(&rx->call_lock);
127 return ret;
131 * End the packet reception phase.
133 static void rxrpc_end_rx_phase(struct rxrpc_call *call)
135 _enter("%d,%s", call->debug_id, rxrpc_call_states[call->state]);
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);
144 write_lock_bh(&call->state_lock);
146 switch (call->state) {
147 case RXRPC_CALL_CLIENT_RECV_REPLY:
148 __rxrpc_call_completed(call);
149 break;
151 case RXRPC_CALL_SERVER_RECV_REQUEST:
152 call->state = RXRPC_CALL_SERVER_ACK_REQUEST;
153 break;
154 default:
155 break;
158 write_unlock_bh(&call->state_lock);
162 * Discard a packet we've used up and advance the Rx window by one.
164 static void rxrpc_rotate_rx_window(struct rxrpc_call *call)
166 struct sk_buff *skb;
167 rxrpc_seq_t hard_ack, top;
168 int ix;
170 _enter("%d", call->debug_id);
172 hard_ack = call->rx_hard_ack;
173 top = smp_load_acquire(&call->rx_top);
174 ASSERT(before(hard_ack, top));
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);
185 rxrpc_free_skb(skb);
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);
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.
198 static int rxrpc_verify_packet(struct rxrpc_call *call, struct sk_buff *skb,
199 u8 annotation,
200 unsigned int offset, unsigned int len)
202 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
203 rxrpc_seq_t seq = sp->hdr.seq;
204 u16 cksum = sp->hdr.cksum;
206 _enter("");
208 /* For all but the head jumbo subpacket, the security checksum is in a
209 * jumbo header immediately prior to the data.
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;
219 return call->conn->security->verify_packet(call, skb, offset, len,
220 seq, cksum);
224 * Locate the data within a packet. This is complicated by:
226 * (1) An skb may contain a jumbo packet - so we have to find the appropriate
227 * subpacket.
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.
233 static int rxrpc_locate_data(struct rxrpc_call *call, struct sk_buff *skb,
234 u8 *_annotation,
235 unsigned int *_offset, unsigned int *_len)
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;
243 if (offset > 0)
244 return 0;
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;
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;
263 *_offset = offset;
264 *_len = len;
265 call->conn->security->locate_data(call, skb, _offset, _len);
266 return 0;
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.
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)
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;
286 _enter("");
288 rx_pkt_offset = call->rx_pkt_offset;
289 rx_pkt_len = call->rx_pkt_len;
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);
303 if (msg)
304 sock_recv_timestamp(msg, sock->sk, skb);
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);
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;
322 /* handle piecemeal consumption of data packets */
323 _debug("copied %d @%zu", copy, *_offset);
325 rx_pkt_offset += copy;
326 rx_pkt_len -= copy;
327 *_offset += copy;
330 if (rx_pkt_len > 0) {
331 _debug("buffer full");
332 ASSERTCMP(*_offset, ==, len);
333 break;
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;
343 ASSERTIFCMP(last, seq, ==, top);
346 if (after(seq, top)) {
347 ret = -EAGAIN;
348 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags))
349 ret = 1;
351 out:
352 if (!(flags & MSG_PEEK)) {
353 call->rx_pkt_offset = rx_pkt_offset;
354 call->rx_pkt_len = rx_pkt_len;
356 _leave(" = %d [%u/%u]", ret, seq, top);
357 return ret;
361 * Receive a message from an RxRPC socket
362 * - we need to be careful about two or more threads calling recvmsg
363 * simultaneously
365 int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
366 int flags)
368 struct rxrpc_call *call;
369 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
370 struct list_head *l;
371 size_t copied = 0;
372 long timeo;
373 int ret;
375 DEFINE_WAIT(wait);
377 _enter(",,,%zu,%d", len, flags);
379 if (flags & (MSG_OOB | MSG_TRUNC))
380 return -EOPNOTSUPP;
382 timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT);
384 try_again:
385 lock_sock(&rx->sk);
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;
395 if (list_empty(&rx->recvmsg_q)) {
396 ret = -EWOULDBLOCK;
397 if (timeo == 0)
398 goto error_no_call;
400 release_sock(&rx->sk);
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;
409 if (list_empty(&rx->recvmsg_q)) {
410 if (signal_pending(current))
411 goto wait_interrupted;
412 timeo = schedule_timeout(timeo);
414 finish_wait(sk_sleep(&rx->sk), &wait);
415 goto try_again;
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.
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
427 rxrpc_get_call(call, rxrpc_call_got);
428 write_unlock_bh(&rx->recvmsg_lock);
430 _debug("recvmsg call %p", call);
432 if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
433 BUG();
435 if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
436 if (flags & MSG_CMSG_COMPAT) {
437 unsigned int id32 = call->user_call_ID;
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);
446 if (ret < 0)
447 goto error;
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;
456 switch (call->state) {
457 case RXRPC_CALL_SERVER_ACCEPTING:
458 ret = rxrpc_recvmsg_new_call(rx, call, msg, flags);
459 break;
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;
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);
471 break;
472 default:
473 ret = 0;
474 break;
477 if (ret < 0)
478 goto error;
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;
490 if (ret == 0)
491 msg->msg_flags |= MSG_MORE;
492 else
493 msg->msg_flags &= ~MSG_MORE;
494 ret = copied;
496 error:
497 rxrpc_put_call(call, rxrpc_call_put);
498 error_no_call:
499 release_sock(&rx->sk);
500 _leave(" = %d", ret);
501 return ret;
503 wait_interrupted:
504 ret = sock_intr_errno(timeo);
505 wait_error:
506 finish_wait(sk_sleep(&rx->sk), &wait);
507 release_sock(&rx->sk);
508 _leave(" = %d [wait]", ret);
509 return ret;
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
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.
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.
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.
534 * *_abort should also be initialised to 0.
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)
540 struct iov_iter iter;
541 struct kvec iov;
542 int ret;
544 _enter("{%d,%s},%zu/%zu,%d",
545 call->debug_id, rxrpc_call_states[call->state],
546 *_offset, size, want_more);
548 ASSERTCMP(*_offset, <=, size);
549 ASSERTCMP(call->state, !=, RXRPC_CALL_SERVER_ACCEPTING);
551 iov.iov_base = buf + *_offset;
552 iov.iov_len = size - *_offset;
553 iov_iter_kvec(&iter, ITER_KVEC | READ, &iov, 1, size - *_offset);
555 lock_sock(sock->sk);
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:
561 ret = rxrpc_recvmsg_data(sock, call, NULL, &iter, size, 0,
562 _offset);
563 if (ret < 0)
564 goto out;
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.
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;
579 if (!want_more)
580 goto excess_data;
581 goto out;
583 case RXRPC_CALL_COMPLETE:
584 goto call_complete;
586 default:
587 ret = -EINPROGRESS;
588 goto out;
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;
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;
612 goto out;
614 EXPORT_SYMBOL(rxrpc_kernel_recv_data);