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