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 #include <linux/net.h>
13 #include <linux/skbuff.h>
14 #include <linux/export.h>
16 #include <net/af_rxrpc.h>
17 #include "ar-internal.h"
20 * removal a call's user ID from the socket tree to make the user ID available
21 * again and so that it won't be seen again in association with that call
23 void rxrpc_remove_user_ID(struct rxrpc_sock
*rx
, struct rxrpc_call
*call
)
25 _debug("RELEASE CALL %d", call
->debug_id
);
27 if (test_bit(RXRPC_CALL_HAS_USERID
, &call
->flags
)) {
28 write_lock_bh(&rx
->call_lock
);
29 rb_erase(&call
->sock_node
, &call
->socket
->calls
);
30 clear_bit(RXRPC_CALL_HAS_USERID
, &call
->flags
);
31 write_unlock_bh(&rx
->call_lock
);
34 read_lock_bh(&call
->state_lock
);
35 if (!test_bit(RXRPC_CALL_RELEASED
, &call
->flags
) &&
36 !test_and_set_bit(RXRPC_CALL_RELEASE
, &call
->events
))
37 rxrpc_queue_call(call
);
38 read_unlock_bh(&call
->state_lock
);
42 * receive a message from an RxRPC socket
43 * - we need to be careful about two or more threads calling recvmsg
46 int rxrpc_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
,
49 struct rxrpc_skb_priv
*sp
;
50 struct rxrpc_call
*call
= NULL
, *continue_call
= NULL
;
51 struct rxrpc_sock
*rx
= rxrpc_sk(sock
->sk
);
54 int copy
, ret
, ullen
, offset
, copied
= 0;
59 _enter(",,,%zu,%d", len
, flags
);
61 if (flags
& (MSG_OOB
| MSG_TRUNC
))
64 ullen
= msg
->msg_flags
& MSG_CMSG_COMPAT
? 4 : sizeof(unsigned long);
66 timeo
= sock_rcvtimeo(&rx
->sk
, flags
& MSG_DONTWAIT
);
67 msg
->msg_flags
|= MSG_MORE
;
72 /* return immediately if a client socket has no outstanding
74 if (RB_EMPTY_ROOT(&rx
->calls
)) {
77 if (rx
->sk
.sk_state
!= RXRPC_SERVER_LISTENING
) {
78 release_sock(&rx
->sk
);
80 rxrpc_put_call(continue_call
);
85 /* get the next message on the Rx queue */
86 skb
= skb_peek(&rx
->sk
.sk_receive_queue
);
88 /* nothing remains on the queue */
90 (flags
& MSG_PEEK
|| timeo
== 0))
93 /* wait for a message to turn up */
94 release_sock(&rx
->sk
);
95 prepare_to_wait_exclusive(sk_sleep(&rx
->sk
), &wait
,
97 ret
= sock_error(&rx
->sk
);
101 if (skb_queue_empty(&rx
->sk
.sk_receive_queue
)) {
102 if (signal_pending(current
))
103 goto wait_interrupted
;
104 timeo
= schedule_timeout(timeo
);
106 finish_wait(sk_sleep(&rx
->sk
), &wait
);
114 ASSERT(call
!= NULL
);
116 _debug("next pkt %s", rxrpc_pkts
[sp
->hdr
.type
]);
118 /* make sure we wait for the state to be updated in this call */
119 spin_lock_bh(&call
->lock
);
120 spin_unlock_bh(&call
->lock
);
122 if (test_bit(RXRPC_CALL_RELEASED
, &call
->flags
)) {
123 _debug("packet from released call");
124 if (skb_dequeue(&rx
->sk
.sk_receive_queue
) != skb
)
130 /* determine whether to continue last data receive */
132 _debug("maybe cont");
133 if (call
!= continue_call
||
134 skb
->mark
!= RXRPC_SKB_MARK_DATA
) {
135 release_sock(&rx
->sk
);
136 rxrpc_put_call(continue_call
);
137 _leave(" = %d [noncont]", copied
);
142 rxrpc_get_call(call
);
144 /* copy the peer address and timestamp */
145 if (!continue_call
) {
148 sizeof(call
->conn
->trans
->peer
->srx
);
149 memcpy(msg
->msg_name
,
150 &call
->conn
->trans
->peer
->srx
, len
);
151 msg
->msg_namelen
= len
;
153 sock_recv_timestamp(msg
, &rx
->sk
, skb
);
156 /* receive the message */
157 if (skb
->mark
!= RXRPC_SKB_MARK_DATA
)
158 goto receive_non_data_message
;
160 _debug("recvmsg DATA #%u { %d, %d }",
161 ntohl(sp
->hdr
.seq
), skb
->len
, sp
->offset
);
163 if (!continue_call
) {
164 /* only set the control data once per recvmsg() */
165 ret
= put_cmsg(msg
, SOL_RXRPC
, RXRPC_USER_CALL_ID
,
166 ullen
, &call
->user_call_ID
);
169 ASSERT(test_bit(RXRPC_CALL_HAS_USERID
, &call
->flags
));
172 ASSERTCMP(ntohl(sp
->hdr
.seq
), >=, call
->rx_data_recv
);
173 ASSERTCMP(ntohl(sp
->hdr
.seq
), <=, call
->rx_data_recv
+ 1);
174 call
->rx_data_recv
= ntohl(sp
->hdr
.seq
);
176 ASSERTCMP(ntohl(sp
->hdr
.seq
), >, call
->rx_data_eaten
);
179 copy
= skb
->len
- offset
;
180 if (copy
> len
- copied
)
183 ret
= skb_copy_datagram_msg(skb
, offset
, msg
, copy
);
188 /* handle piecemeal consumption of data packets */
189 _debug("copied %d+%d", copy
, copied
);
194 if (!(flags
& MSG_PEEK
))
197 if (sp
->offset
< skb
->len
) {
198 _debug("buffer full");
199 ASSERTCMP(copied
, ==, len
);
203 /* we transferred the whole data packet */
204 if (sp
->hdr
.flags
& RXRPC_LAST_PACKET
) {
206 if (call
->conn
->out_clientflag
) {
207 /* last byte of reply received */
209 goto terminal_message
;
212 /* last bit of request received */
213 if (!(flags
& MSG_PEEK
)) {
214 _debug("eat packet");
215 if (skb_dequeue(&rx
->sk
.sk_receive_queue
) !=
220 msg
->msg_flags
&= ~MSG_MORE
;
224 /* move on to the next data message */
227 continue_call
= sp
->call
;
229 rxrpc_put_call(call
);
232 if (flags
& MSG_PEEK
) {
235 if (skb
== (struct sk_buff
*) &rx
->sk
.sk_receive_queue
)
237 goto peek_next_packet
;
240 _debug("eat packet");
241 if (skb_dequeue(&rx
->sk
.sk_receive_queue
) != skb
)
246 /* end of non-terminal data packet reception for the moment */
247 _debug("end rcv data");
249 release_sock(&rx
->sk
);
251 rxrpc_put_call(call
);
253 rxrpc_put_call(continue_call
);
254 _leave(" = %d [data]", copied
);
257 /* handle non-DATA messages such as aborts, incoming connections and
259 receive_non_data_message
:
262 if (skb
->mark
== RXRPC_SKB_MARK_NEW_CALL
) {
263 _debug("RECV NEW CALL");
264 ret
= put_cmsg(msg
, SOL_RXRPC
, RXRPC_NEW_CALL
, 0, &abort_code
);
267 if (!(flags
& MSG_PEEK
)) {
268 if (skb_dequeue(&rx
->sk
.sk_receive_queue
) != skb
)
275 ret
= put_cmsg(msg
, SOL_RXRPC
, RXRPC_USER_CALL_ID
,
276 ullen
, &call
->user_call_ID
);
279 ASSERT(test_bit(RXRPC_CALL_HAS_USERID
, &call
->flags
));
282 case RXRPC_SKB_MARK_DATA
:
284 case RXRPC_SKB_MARK_FINAL_ACK
:
285 ret
= put_cmsg(msg
, SOL_RXRPC
, RXRPC_ACK
, 0, &abort_code
);
287 case RXRPC_SKB_MARK_BUSY
:
288 ret
= put_cmsg(msg
, SOL_RXRPC
, RXRPC_BUSY
, 0, &abort_code
);
290 case RXRPC_SKB_MARK_REMOTE_ABORT
:
291 abort_code
= call
->abort_code
;
292 ret
= put_cmsg(msg
, SOL_RXRPC
, RXRPC_ABORT
, 4, &abort_code
);
294 case RXRPC_SKB_MARK_NET_ERROR
:
295 _debug("RECV NET ERROR %d", sp
->error
);
296 abort_code
= sp
->error
;
297 ret
= put_cmsg(msg
, SOL_RXRPC
, RXRPC_NET_ERROR
, 4, &abort_code
);
299 case RXRPC_SKB_MARK_LOCAL_ERROR
:
300 _debug("RECV LOCAL ERROR %d", sp
->error
);
301 abort_code
= sp
->error
;
302 ret
= put_cmsg(msg
, SOL_RXRPC
, RXRPC_LOCAL_ERROR
, 4,
315 msg
->msg_flags
&= ~MSG_MORE
;
316 msg
->msg_flags
|= MSG_EOR
;
318 if (!(flags
& MSG_PEEK
)) {
319 _net("free terminal skb %p", skb
);
320 if (skb_dequeue(&rx
->sk
.sk_receive_queue
) != skb
)
323 rxrpc_remove_user_ID(rx
, call
);
326 release_sock(&rx
->sk
);
327 rxrpc_put_call(call
);
329 rxrpc_put_call(continue_call
);
330 _leave(" = %d", ret
);
334 _debug("copy error");
335 release_sock(&rx
->sk
);
336 rxrpc_put_call(call
);
338 rxrpc_put_call(continue_call
);
339 _leave(" = %d", ret
);
343 ret
= sock_intr_errno(timeo
);
345 finish_wait(sk_sleep(&rx
->sk
), &wait
);
347 rxrpc_put_call(continue_call
);
350 _leave(" = %d [waitfail %d]", copied
, ret
);
356 * rxrpc_kernel_data_delivered - Record delivery of data message
357 * @skb: Message holding data
359 * Record the delivery of a data message. This permits RxRPC to keep its
360 * tracking correct. The socket buffer will be deleted.
362 void rxrpc_kernel_data_delivered(struct sk_buff
*skb
)
364 struct rxrpc_skb_priv
*sp
= rxrpc_skb(skb
);
365 struct rxrpc_call
*call
= sp
->call
;
367 ASSERTCMP(ntohl(sp
->hdr
.seq
), >=, call
->rx_data_recv
);
368 ASSERTCMP(ntohl(sp
->hdr
.seq
), <=, call
->rx_data_recv
+ 1);
369 call
->rx_data_recv
= ntohl(sp
->hdr
.seq
);
371 ASSERTCMP(ntohl(sp
->hdr
.seq
), >, call
->rx_data_eaten
);
375 EXPORT_SYMBOL(rxrpc_kernel_data_delivered
);
378 * rxrpc_kernel_is_data_last - Determine if data message is last one
379 * @skb: Message holding data
381 * Determine if data message is last one for the parent call.
383 bool rxrpc_kernel_is_data_last(struct sk_buff
*skb
)
385 struct rxrpc_skb_priv
*sp
= rxrpc_skb(skb
);
387 ASSERTCMP(skb
->mark
, ==, RXRPC_SKB_MARK_DATA
);
389 return sp
->hdr
.flags
& RXRPC_LAST_PACKET
;
392 EXPORT_SYMBOL(rxrpc_kernel_is_data_last
);
395 * rxrpc_kernel_get_abort_code - Get the abort code from an RxRPC abort message
396 * @skb: Message indicating an abort
398 * Get the abort code from an RxRPC abort message.
400 u32
rxrpc_kernel_get_abort_code(struct sk_buff
*skb
)
402 struct rxrpc_skb_priv
*sp
= rxrpc_skb(skb
);
404 ASSERTCMP(skb
->mark
, ==, RXRPC_SKB_MARK_REMOTE_ABORT
);
406 return sp
->call
->abort_code
;
409 EXPORT_SYMBOL(rxrpc_kernel_get_abort_code
);
412 * rxrpc_kernel_get_error - Get the error number from an RxRPC error message
413 * @skb: Message indicating an error
415 * Get the error number from an RxRPC error message.
417 int rxrpc_kernel_get_error_number(struct sk_buff
*skb
)
419 struct rxrpc_skb_priv
*sp
= rxrpc_skb(skb
);
424 EXPORT_SYMBOL(rxrpc_kernel_get_error_number
);