Commit | Line | Data |
---|---|---|
b4d0d230 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
0b58b8a1 DH |
2 | /* AF_RXRPC sendmsg() implementation. |
3 | * | |
4 | * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved. | |
5 | * Written by David Howells (dhowells@redhat.com) | |
0b58b8a1 DH |
6 | */ |
7 | ||
8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
9 | ||
10 | #include <linux/net.h> | |
11 | #include <linux/gfp.h> | |
12 | #include <linux/skbuff.h> | |
13 | #include <linux/export.h> | |
174cd4b1 IM |
14 | #include <linux/sched/signal.h> |
15 | ||
0b58b8a1 DH |
16 | #include <net/sock.h> |
17 | #include <net/af_rxrpc.h> | |
18 | #include "ar-internal.h" | |
19 | ||
158fe666 DH |
20 | /* |
21 | * Return true if there's sufficient Tx queue space. | |
22 | */ | |
23 | static bool rxrpc_check_tx_space(struct rxrpc_call *call, rxrpc_seq_t *_tx_win) | |
24 | { | |
25 | unsigned int win_size = | |
26 | min_t(unsigned int, call->tx_winsize, | |
27 | call->cong_cwnd + call->cong_extra); | |
28 | rxrpc_seq_t tx_win = READ_ONCE(call->tx_hard_ack); | |
29 | ||
30 | if (_tx_win) | |
31 | *_tx_win = tx_win; | |
32 | return call->tx_top - tx_win < win_size; | |
33 | } | |
34 | ||
bc5e3a54 DH |
35 | /* |
36 | * Wait for space to appear in the Tx queue or a signal to occur. | |
37 | */ | |
38 | static int rxrpc_wait_for_tx_window_intr(struct rxrpc_sock *rx, | |
39 | struct rxrpc_call *call, | |
40 | long *timeo) | |
41 | { | |
42 | for (;;) { | |
43 | set_current_state(TASK_INTERRUPTIBLE); | |
158fe666 | 44 | if (rxrpc_check_tx_space(call, NULL)) |
bc5e3a54 DH |
45 | return 0; |
46 | ||
47 | if (call->state >= RXRPC_CALL_COMPLETE) | |
48 | return call->error; | |
49 | ||
50 | if (signal_pending(current)) | |
51 | return sock_intr_errno(*timeo); | |
52 | ||
53 | trace_rxrpc_transmit(call, rxrpc_transmit_wait); | |
bc5e3a54 | 54 | *timeo = schedule_timeout(*timeo); |
bc5e3a54 DH |
55 | } |
56 | } | |
57 | ||
58 | /* | |
59 | * Wait for space to appear in the Tx queue uninterruptibly, but with | |
60 | * a timeout of 2*RTT if no progress was made and a signal occurred. | |
61 | */ | |
e138aa7d | 62 | static int rxrpc_wait_for_tx_window_waitall(struct rxrpc_sock *rx, |
bc5e3a54 DH |
63 | struct rxrpc_call *call) |
64 | { | |
65 | rxrpc_seq_t tx_start, tx_win; | |
c410bf01 | 66 | signed long rtt, timeout; |
bc5e3a54 | 67 | |
c410bf01 DH |
68 | rtt = READ_ONCE(call->peer->srtt_us) >> 3; |
69 | rtt = usecs_to_jiffies(rtt) * 2; | |
70 | if (rtt < 2) | |
71 | rtt = 2; | |
bc5e3a54 | 72 | |
c410bf01 | 73 | timeout = rtt; |
bc5e3a54 DH |
74 | tx_start = READ_ONCE(call->tx_hard_ack); |
75 | ||
76 | for (;;) { | |
77 | set_current_state(TASK_UNINTERRUPTIBLE); | |
78 | ||
79 | tx_win = READ_ONCE(call->tx_hard_ack); | |
158fe666 | 80 | if (rxrpc_check_tx_space(call, &tx_win)) |
bc5e3a54 DH |
81 | return 0; |
82 | ||
83 | if (call->state >= RXRPC_CALL_COMPLETE) | |
84 | return call->error; | |
85 | ||
e138aa7d | 86 | if (timeout == 0 && |
bc5e3a54 DH |
87 | tx_win == tx_start && signal_pending(current)) |
88 | return -EINTR; | |
89 | ||
90 | if (tx_win != tx_start) { | |
c410bf01 | 91 | timeout = rtt; |
bc5e3a54 DH |
92 | tx_start = tx_win; |
93 | } | |
94 | ||
95 | trace_rxrpc_transmit(call, rxrpc_transmit_wait); | |
96 | timeout = schedule_timeout(timeout); | |
97 | } | |
98 | } | |
99 | ||
e138aa7d DH |
100 | /* |
101 | * Wait for space to appear in the Tx queue uninterruptibly. | |
102 | */ | |
103 | static int rxrpc_wait_for_tx_window_nonintr(struct rxrpc_sock *rx, | |
104 | struct rxrpc_call *call, | |
105 | long *timeo) | |
106 | { | |
107 | for (;;) { | |
108 | set_current_state(TASK_UNINTERRUPTIBLE); | |
109 | if (rxrpc_check_tx_space(call, NULL)) | |
110 | return 0; | |
111 | ||
112 | if (call->state >= RXRPC_CALL_COMPLETE) | |
113 | return call->error; | |
114 | ||
115 | trace_rxrpc_transmit(call, rxrpc_transmit_wait); | |
116 | *timeo = schedule_timeout(*timeo); | |
117 | } | |
118 | } | |
119 | ||
0b58b8a1 | 120 | /* |
df423a4a DH |
121 | * wait for space to appear in the transmit/ACK window |
122 | * - caller holds the socket locked | |
0b58b8a1 | 123 | */ |
df423a4a DH |
124 | static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx, |
125 | struct rxrpc_call *call, | |
bc5e3a54 DH |
126 | long *timeo, |
127 | bool waitall) | |
0b58b8a1 | 128 | { |
df423a4a DH |
129 | DECLARE_WAITQUEUE(myself, current); |
130 | int ret; | |
0b58b8a1 | 131 | |
248f219c DH |
132 | _enter(",{%u,%u,%u}", |
133 | call->tx_hard_ack, call->tx_top, call->tx_winsize); | |
0b58b8a1 | 134 | |
df423a4a | 135 | add_wait_queue(&call->waitq, &myself); |
0b58b8a1 | 136 | |
e138aa7d DH |
137 | switch (call->interruptibility) { |
138 | case RXRPC_INTERRUPTIBLE: | |
139 | if (waitall) | |
140 | ret = rxrpc_wait_for_tx_window_waitall(rx, call); | |
141 | else | |
142 | ret = rxrpc_wait_for_tx_window_intr(rx, call, timeo); | |
143 | break; | |
144 | case RXRPC_PREINTERRUPTIBLE: | |
145 | case RXRPC_UNINTERRUPTIBLE: | |
146 | default: | |
147 | ret = rxrpc_wait_for_tx_window_nonintr(rx, call, timeo); | |
148 | break; | |
149 | } | |
0b58b8a1 | 150 | |
df423a4a DH |
151 | remove_wait_queue(&call->waitq, &myself); |
152 | set_current_state(TASK_RUNNING); | |
153 | _leave(" = %d", ret); | |
154 | return ret; | |
0b58b8a1 DH |
155 | } |
156 | ||
157 | /* | |
248f219c | 158 | * Schedule an instant Tx resend. |
0b58b8a1 | 159 | */ |
248f219c | 160 | static inline void rxrpc_instant_resend(struct rxrpc_call *call, int ix) |
0b58b8a1 | 161 | { |
248f219c DH |
162 | spin_lock_bh(&call->lock); |
163 | ||
164 | if (call->state < RXRPC_CALL_COMPLETE) { | |
03877bf6 DH |
165 | call->rxtx_annotations[ix] = |
166 | (call->rxtx_annotations[ix] & RXRPC_TX_ANNO_LAST) | | |
167 | RXRPC_TX_ANNO_RETRANS; | |
248f219c | 168 | if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events)) |
df423a4a | 169 | rxrpc_queue_call(call); |
0b58b8a1 | 170 | } |
248f219c DH |
171 | |
172 | spin_unlock_bh(&call->lock); | |
0b58b8a1 DH |
173 | } |
174 | ||
e833251a DH |
175 | /* |
176 | * Notify the owner of the call that the transmit phase is ended and the last | |
177 | * packet has been queued. | |
178 | */ | |
179 | static void rxrpc_notify_end_tx(struct rxrpc_sock *rx, struct rxrpc_call *call, | |
180 | rxrpc_notify_end_tx_t notify_end_tx) | |
181 | { | |
182 | if (notify_end_tx) | |
183 | notify_end_tx(&rx->sk, call, call->user_call_ID); | |
184 | } | |
185 | ||
0b58b8a1 | 186 | /* |
8e8715aa MD |
187 | * Queue a DATA packet for transmission, set the resend timeout and send |
188 | * the packet immediately. Returns the error from rxrpc_send_data_packet() | |
189 | * in case the caller wants to do something with it. | |
0b58b8a1 | 190 | */ |
8e8715aa MD |
191 | static int rxrpc_queue_packet(struct rxrpc_sock *rx, struct rxrpc_call *call, |
192 | struct sk_buff *skb, bool last, | |
193 | rxrpc_notify_end_tx_t notify_end_tx) | |
0b58b8a1 | 194 | { |
df423a4a | 195 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
a158bdd3 | 196 | unsigned long now; |
248f219c DH |
197 | rxrpc_seq_t seq = sp->hdr.seq; |
198 | int ret, ix; | |
70790dbe | 199 | u8 annotation = RXRPC_TX_ANNO_UNACK; |
248f219c DH |
200 | |
201 | _net("queue skb %p [%d]", skb, seq); | |
0b58b8a1 | 202 | |
248f219c | 203 | ASSERTCMP(seq, ==, call->tx_top + 1); |
0b58b8a1 | 204 | |
e122d845 | 205 | if (last) |
70790dbe DH |
206 | annotation |= RXRPC_TX_ANNO_LAST; |
207 | ||
b24d2891 DH |
208 | /* We have to set the timestamp before queueing as the retransmit |
209 | * algorithm can see the packet as soon as we queue it. | |
210 | */ | |
211 | skb->tstamp = ktime_get_real(); | |
212 | ||
248f219c | 213 | ix = seq & RXRPC_RXTX_BUFF_MASK; |
987db9f7 | 214 | rxrpc_get_skb(skb, rxrpc_skb_got); |
70790dbe | 215 | call->rxtx_annotations[ix] = annotation; |
df423a4a | 216 | smp_wmb(); |
248f219c DH |
217 | call->rxtx_buffer[ix] = skb; |
218 | call->tx_top = seq; | |
70790dbe | 219 | if (last) |
a124fe3e | 220 | trace_rxrpc_transmit(call, rxrpc_transmit_queue_last); |
70790dbe | 221 | else |
a124fe3e | 222 | trace_rxrpc_transmit(call, rxrpc_transmit_queue); |
0b58b8a1 | 223 | |
df423a4a DH |
224 | if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) { |
225 | _debug("________awaiting reply/ACK__________"); | |
226 | write_lock_bh(&call->state_lock); | |
227 | switch (call->state) { | |
228 | case RXRPC_CALL_CLIENT_SEND_REQUEST: | |
229 | call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY; | |
e833251a | 230 | rxrpc_notify_end_tx(rx, call, notify_end_tx); |
df423a4a DH |
231 | break; |
232 | case RXRPC_CALL_SERVER_ACK_REQUEST: | |
233 | call->state = RXRPC_CALL_SERVER_SEND_REPLY; | |
a158bdd3 DH |
234 | now = jiffies; |
235 | WRITE_ONCE(call->ack_at, now + MAX_JIFFY_OFFSET); | |
9749fd2b DH |
236 | if (call->ackr_reason == RXRPC_ACK_DELAY) |
237 | call->ackr_reason = 0; | |
a158bdd3 | 238 | trace_rxrpc_timer(call, rxrpc_timer_init_for_send_reply, now); |
df423a4a DH |
239 | if (!last) |
240 | break; | |
df561f66 | 241 | fallthrough; |
df423a4a DH |
242 | case RXRPC_CALL_SERVER_SEND_REPLY: |
243 | call->state = RXRPC_CALL_SERVER_AWAIT_ACK; | |
e833251a | 244 | rxrpc_notify_end_tx(rx, call, notify_end_tx); |
df423a4a DH |
245 | break; |
246 | default: | |
247 | break; | |
248 | } | |
249 | write_unlock_bh(&call->state_lock); | |
250 | } | |
0b58b8a1 | 251 | |
248f219c DH |
252 | if (seq == 1 && rxrpc_is_client_call(call)) |
253 | rxrpc_expose_client_call(call); | |
df423a4a | 254 | |
a1767077 | 255 | ret = rxrpc_send_data_packet(call, skb, false); |
df423a4a | 256 | if (ret < 0) { |
c54e43d7 DH |
257 | switch (ret) { |
258 | case -ENETUNREACH: | |
259 | case -EHOSTUNREACH: | |
260 | case -ECONNREFUSED: | |
5ac0d622 DH |
261 | rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, |
262 | 0, ret); | |
c54e43d7 DH |
263 | goto out; |
264 | } | |
df423a4a | 265 | _debug("need instant resend %d", ret); |
248f219c | 266 | rxrpc_instant_resend(call, ix); |
dfc3da44 | 267 | } else { |
c410bf01 DH |
268 | unsigned long now = jiffies; |
269 | unsigned long resend_at = now + call->peer->rto_j; | |
dfc3da44 | 270 | |
a158bdd3 DH |
271 | WRITE_ONCE(call->resend_at, resend_at); |
272 | rxrpc_reduce_call_timer(call, resend_at, now, | |
273 | rxrpc_timer_set_for_send); | |
df423a4a DH |
274 | } |
275 | ||
c54e43d7 | 276 | out: |
987db9f7 | 277 | rxrpc_free_skb(skb, rxrpc_skb_freed); |
8e8715aa MD |
278 | _leave(" = %d", ret); |
279 | return ret; | |
0b58b8a1 DH |
280 | } |
281 | ||
df423a4a DH |
282 | /* |
283 | * send data through a socket | |
284 | * - must be called in process context | |
540b1c48 | 285 | * - The caller holds the call user access mutex, but not the socket lock. |
0b58b8a1 | 286 | */ |
df423a4a DH |
287 | static int rxrpc_send_data(struct rxrpc_sock *rx, |
288 | struct rxrpc_call *call, | |
e833251a | 289 | struct msghdr *msg, size_t len, |
b0f571ec DH |
290 | rxrpc_notify_end_tx_t notify_end_tx, |
291 | bool *_dropped_lock) | |
0b58b8a1 | 292 | { |
df423a4a DH |
293 | struct rxrpc_skb_priv *sp; |
294 | struct sk_buff *skb; | |
295 | struct sock *sk = &rx->sk; | |
b0f571ec | 296 | enum rxrpc_call_state state; |
df423a4a | 297 | long timeo; |
b0f571ec DH |
298 | bool more = msg->msg_flags & MSG_MORE; |
299 | int ret, copied = 0; | |
0b58b8a1 | 300 | |
df423a4a | 301 | timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
0b58b8a1 | 302 | |
df423a4a DH |
303 | /* this should be in poll */ |
304 | sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); | |
0b58b8a1 | 305 | |
b0f571ec DH |
306 | reload: |
307 | ret = -EPIPE; | |
639f181f | 308 | if (sk->sk_shutdown & SEND_SHUTDOWN) |
b0f571ec DH |
309 | goto maybe_error; |
310 | state = READ_ONCE(call->state); | |
311 | ret = -ESHUTDOWN; | |
312 | if (state >= RXRPC_CALL_COMPLETE) | |
313 | goto maybe_error; | |
314 | ret = -EPROTO; | |
315 | if (state != RXRPC_CALL_CLIENT_SEND_REQUEST && | |
316 | state != RXRPC_CALL_SERVER_ACK_REQUEST && | |
317 | state != RXRPC_CALL_SERVER_SEND_REPLY) | |
318 | goto maybe_error; | |
319 | ||
320 | ret = -EMSGSIZE; | |
e754eba6 | 321 | if (call->tx_total_len != -1) { |
b0f571ec DH |
322 | if (len - copied > call->tx_total_len) |
323 | goto maybe_error; | |
324 | if (!more && len - copied != call->tx_total_len) | |
325 | goto maybe_error; | |
e754eba6 DH |
326 | } |
327 | ||
df423a4a DH |
328 | skb = call->tx_pending; |
329 | call->tx_pending = NULL; | |
987db9f7 | 330 | rxrpc_see_skb(skb, rxrpc_skb_seen); |
0b58b8a1 | 331 | |
df423a4a | 332 | do { |
7aa51da7 DH |
333 | /* Check to see if there's a ping ACK to reply to. */ |
334 | if (call->ackr_reason == RXRPC_ACK_PING_RESPONSE) | |
bd1fdf8c | 335 | rxrpc_send_ack_packet(call, false, NULL); |
7aa51da7 | 336 | |
df423a4a | 337 | if (!skb) { |
d7d775b1 | 338 | size_t remain, bufsize, chunk, offset; |
0b58b8a1 | 339 | |
df423a4a | 340 | _debug("alloc"); |
0b58b8a1 | 341 | |
b0f571ec DH |
342 | if (!rxrpc_check_tx_space(call, NULL)) |
343 | goto wait_for_space; | |
0b58b8a1 | 344 | |
d7d775b1 DH |
345 | /* Work out the maximum size of a packet. Assume that |
346 | * the security header is going to be in the padded | |
347 | * region (enc blocksize), but the trailer is not. | |
348 | */ | |
349 | remain = more ? INT_MAX : msg_data_left(msg); | |
350 | ret = call->conn->security->how_much_data(call, remain, | |
351 | &bufsize, &chunk, &offset); | |
352 | if (ret < 0) | |
353 | goto maybe_error; | |
0b58b8a1 | 354 | |
d7d775b1 | 355 | _debug("SIZE: %zu/%zu @%zu", chunk, bufsize, offset); |
0b58b8a1 | 356 | |
df423a4a DH |
357 | /* create a buffer that we can retain until it's ACK'd */ |
358 | skb = sock_alloc_send_skb( | |
d7d775b1 | 359 | sk, bufsize, msg->msg_flags & MSG_DONTWAIT, &ret); |
df423a4a DH |
360 | if (!skb) |
361 | goto maybe_error; | |
0b58b8a1 | 362 | |
b311e684 DH |
363 | sp = rxrpc_skb(skb); |
364 | sp->rx_flags |= RXRPC_SKB_TX_BUFFER; | |
987db9f7 | 365 | rxrpc_new_skb(skb, rxrpc_skb_new); |
0b58b8a1 | 366 | |
df423a4a | 367 | _debug("ALLOC SEND %p", skb); |
0b58b8a1 | 368 | |
df423a4a | 369 | ASSERTCMP(skb->mark, ==, 0); |
0b58b8a1 | 370 | |
d7d775b1 | 371 | __skb_put(skb, offset); |
0b58b8a1 | 372 | |
df423a4a DH |
373 | sp->remain = chunk; |
374 | if (sp->remain > skb_tailroom(skb)) | |
375 | sp->remain = skb_tailroom(skb); | |
0b58b8a1 | 376 | |
df423a4a DH |
377 | _net("skb: hr %d, tr %d, hl %d, rm %d", |
378 | skb_headroom(skb), | |
379 | skb_tailroom(skb), | |
380 | skb_headlen(skb), | |
381 | sp->remain); | |
0b58b8a1 | 382 | |
df423a4a DH |
383 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
384 | } | |
0b58b8a1 | 385 | |
df423a4a DH |
386 | _debug("append"); |
387 | sp = rxrpc_skb(skb); | |
0b58b8a1 | 388 | |
df423a4a DH |
389 | /* append next segment of data to the current buffer */ |
390 | if (msg_data_left(msg) > 0) { | |
391 | int copy = skb_tailroom(skb); | |
392 | ASSERTCMP(copy, >, 0); | |
393 | if (copy > msg_data_left(msg)) | |
394 | copy = msg_data_left(msg); | |
395 | if (copy > sp->remain) | |
396 | copy = sp->remain; | |
0b58b8a1 | 397 | |
df423a4a DH |
398 | _debug("add"); |
399 | ret = skb_add_data(skb, &msg->msg_iter, copy); | |
400 | _debug("added"); | |
401 | if (ret < 0) | |
402 | goto efault; | |
403 | sp->remain -= copy; | |
404 | skb->mark += copy; | |
405 | copied += copy; | |
e754eba6 DH |
406 | if (call->tx_total_len != -1) |
407 | call->tx_total_len -= copy; | |
0b58b8a1 DH |
408 | } |
409 | ||
e122d845 DH |
410 | /* check for the far side aborting the call or a network error |
411 | * occurring */ | |
412 | if (call->state == RXRPC_CALL_COMPLETE) | |
413 | goto call_terminated; | |
414 | ||
df423a4a DH |
415 | /* add the packet to the send queue if it's now full */ |
416 | if (sp->remain <= 0 || | |
417 | (msg_data_left(msg) == 0 && !more)) { | |
418 | struct rxrpc_connection *conn = call->conn; | |
419 | uint32_t seq; | |
0b58b8a1 | 420 | |
248f219c | 421 | seq = call->tx_top + 1; |
0b58b8a1 | 422 | |
df423a4a | 423 | sp->hdr.seq = seq; |
df423a4a | 424 | sp->hdr._rsvd = 0; |
5a924b89 | 425 | sp->hdr.flags = conn->out_clientflag; |
0b58b8a1 | 426 | |
df423a4a DH |
427 | if (msg_data_left(msg) == 0 && !more) |
428 | sp->hdr.flags |= RXRPC_LAST_PACKET; | |
248f219c DH |
429 | else if (call->tx_top - call->tx_hard_ack < |
430 | call->tx_winsize) | |
df423a4a | 431 | sp->hdr.flags |= RXRPC_MORE_PACKETS; |
0b58b8a1 | 432 | |
f4bdf3d6 | 433 | ret = call->security->secure_packet(call, skb, skb->mark); |
df423a4a DH |
434 | if (ret < 0) |
435 | goto out; | |
0b58b8a1 | 436 | |
8e8715aa MD |
437 | ret = rxrpc_queue_packet(rx, call, skb, |
438 | !msg_data_left(msg) && !more, | |
439 | notify_end_tx); | |
440 | /* Should check for failure here */ | |
df423a4a DH |
441 | skb = NULL; |
442 | } | |
443 | } while (msg_data_left(msg) > 0); | |
0b58b8a1 | 444 | |
df423a4a DH |
445 | success: |
446 | ret = copied; | |
4ba68c51 DH |
447 | if (READ_ONCE(call->state) == RXRPC_CALL_COMPLETE) { |
448 | read_lock_bh(&call->state_lock); | |
449 | if (call->error < 0) | |
450 | ret = call->error; | |
451 | read_unlock_bh(&call->state_lock); | |
452 | } | |
df423a4a DH |
453 | out: |
454 | call->tx_pending = skb; | |
455 | _leave(" = %d", ret); | |
456 | return ret; | |
0b58b8a1 | 457 | |
e122d845 | 458 | call_terminated: |
987db9f7 | 459 | rxrpc_free_skb(skb, rxrpc_skb_freed); |
e122d845 DH |
460 | _leave(" = %d", call->error); |
461 | return call->error; | |
462 | ||
df423a4a DH |
463 | maybe_error: |
464 | if (copied) | |
465 | goto success; | |
466 | goto out; | |
0b58b8a1 | 467 | |
df423a4a DH |
468 | efault: |
469 | ret = -EFAULT; | |
470 | goto out; | |
b0f571ec DH |
471 | |
472 | wait_for_space: | |
473 | ret = -EAGAIN; | |
474 | if (msg->msg_flags & MSG_DONTWAIT) | |
475 | goto maybe_error; | |
476 | mutex_unlock(&call->user_mutex); | |
477 | *_dropped_lock = true; | |
478 | ret = rxrpc_wait_for_tx_window(rx, call, &timeo, | |
479 | msg->msg_flags & MSG_WAITALL); | |
480 | if (ret < 0) | |
481 | goto maybe_error; | |
482 | if (call->interruptibility == RXRPC_INTERRUPTIBLE) { | |
483 | if (mutex_lock_interruptible(&call->user_mutex) < 0) { | |
484 | ret = sock_intr_errno(timeo); | |
485 | goto maybe_error; | |
486 | } | |
487 | } else { | |
488 | mutex_lock(&call->user_mutex); | |
489 | } | |
490 | *_dropped_lock = false; | |
491 | goto reload; | |
0b58b8a1 DH |
492 | } |
493 | ||
494 | /* | |
df423a4a | 495 | * extract control messages from the sendmsg() control buffer |
0b58b8a1 | 496 | */ |
3ab26a6f | 497 | static int rxrpc_sendmsg_cmsg(struct msghdr *msg, struct rxrpc_send_params *p) |
0b58b8a1 | 498 | { |
df423a4a DH |
499 | struct cmsghdr *cmsg; |
500 | bool got_user_ID = false; | |
501 | int len; | |
0b58b8a1 | 502 | |
df423a4a DH |
503 | if (msg->msg_controllen == 0) |
504 | return -EINVAL; | |
0b58b8a1 | 505 | |
df423a4a DH |
506 | for_each_cmsghdr(cmsg, msg) { |
507 | if (!CMSG_OK(msg, cmsg)) | |
508 | return -EINVAL; | |
0b58b8a1 | 509 | |
1ff8cebf | 510 | len = cmsg->cmsg_len - sizeof(struct cmsghdr); |
df423a4a DH |
511 | _debug("CMSG %d, %d, %d", |
512 | cmsg->cmsg_level, cmsg->cmsg_type, len); | |
0b58b8a1 | 513 | |
df423a4a DH |
514 | if (cmsg->cmsg_level != SOL_RXRPC) |
515 | continue; | |
0b58b8a1 | 516 | |
df423a4a DH |
517 | switch (cmsg->cmsg_type) { |
518 | case RXRPC_USER_CALL_ID: | |
519 | if (msg->msg_flags & MSG_CMSG_COMPAT) { | |
520 | if (len != sizeof(u32)) | |
521 | return -EINVAL; | |
48124178 | 522 | p->call.user_call_ID = *(u32 *)CMSG_DATA(cmsg); |
df423a4a DH |
523 | } else { |
524 | if (len != sizeof(unsigned long)) | |
525 | return -EINVAL; | |
48124178 | 526 | p->call.user_call_ID = *(unsigned long *) |
df423a4a DH |
527 | CMSG_DATA(cmsg); |
528 | } | |
df423a4a DH |
529 | got_user_ID = true; |
530 | break; | |
0b58b8a1 | 531 | |
df423a4a | 532 | case RXRPC_ABORT: |
3ab26a6f | 533 | if (p->command != RXRPC_CMD_SEND_DATA) |
df423a4a | 534 | return -EINVAL; |
3ab26a6f DH |
535 | p->command = RXRPC_CMD_SEND_ABORT; |
536 | if (len != sizeof(p->abort_code)) | |
df423a4a | 537 | return -EINVAL; |
3ab26a6f DH |
538 | p->abort_code = *(unsigned int *)CMSG_DATA(cmsg); |
539 | if (p->abort_code == 0) | |
df423a4a DH |
540 | return -EINVAL; |
541 | break; | |
0b58b8a1 | 542 | |
2d914c1b | 543 | case RXRPC_CHARGE_ACCEPT: |
3ab26a6f | 544 | if (p->command != RXRPC_CMD_SEND_DATA) |
df423a4a | 545 | return -EINVAL; |
2d914c1b | 546 | p->command = RXRPC_CMD_CHARGE_ACCEPT; |
df423a4a DH |
547 | if (len != 0) |
548 | return -EINVAL; | |
549 | break; | |
0b58b8a1 | 550 | |
df423a4a | 551 | case RXRPC_EXCLUSIVE_CALL: |
3ab26a6f | 552 | p->exclusive = true; |
df423a4a DH |
553 | if (len != 0) |
554 | return -EINVAL; | |
555 | break; | |
4e255721 DH |
556 | |
557 | case RXRPC_UPGRADE_SERVICE: | |
3ab26a6f | 558 | p->upgrade = true; |
4e255721 DH |
559 | if (len != 0) |
560 | return -EINVAL; | |
561 | break; | |
562 | ||
e754eba6 | 563 | case RXRPC_TX_LENGTH: |
48124178 | 564 | if (p->call.tx_total_len != -1 || len != sizeof(__s64)) |
e754eba6 | 565 | return -EINVAL; |
48124178 DH |
566 | p->call.tx_total_len = *(__s64 *)CMSG_DATA(cmsg); |
567 | if (p->call.tx_total_len < 0) | |
e754eba6 DH |
568 | return -EINVAL; |
569 | break; | |
570 | ||
a158bdd3 DH |
571 | case RXRPC_SET_CALL_TIMEOUT: |
572 | if (len & 3 || len < 4 || len > 12) | |
573 | return -EINVAL; | |
574 | memcpy(&p->call.timeouts, CMSG_DATA(cmsg), len); | |
575 | p->call.nr_timeouts = len / 4; | |
576 | if (p->call.timeouts.hard > INT_MAX / HZ) | |
577 | return -ERANGE; | |
578 | if (p->call.nr_timeouts >= 2 && p->call.timeouts.idle > 60 * 60 * 1000) | |
579 | return -ERANGE; | |
580 | if (p->call.nr_timeouts >= 3 && p->call.timeouts.normal > 60 * 60 * 1000) | |
581 | return -ERANGE; | |
582 | break; | |
583 | ||
df423a4a DH |
584 | default: |
585 | return -EINVAL; | |
586 | } | |
587 | } | |
0b58b8a1 | 588 | |
df423a4a DH |
589 | if (!got_user_ID) |
590 | return -EINVAL; | |
48124178 | 591 | if (p->call.tx_total_len != -1 && p->command != RXRPC_CMD_SEND_DATA) |
e754eba6 | 592 | return -EINVAL; |
df423a4a DH |
593 | _leave(" = 0"); |
594 | return 0; | |
595 | } | |
0b58b8a1 | 596 | |
df423a4a DH |
597 | /* |
598 | * Create a new client call for sendmsg(). | |
540b1c48 DH |
599 | * - Called with the socket lock held, which it must release. |
600 | * - If it returns a call, the call's lock will need releasing by the caller. | |
df423a4a DH |
601 | */ |
602 | static struct rxrpc_call * | |
603 | rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, | |
3ab26a6f | 604 | struct rxrpc_send_params *p) |
540b1c48 | 605 | __releases(&rx->sk.sk_lock.slock) |
88f2a825 | 606 | __acquires(&call->user_mutex) |
df423a4a DH |
607 | { |
608 | struct rxrpc_conn_parameters cp; | |
609 | struct rxrpc_call *call; | |
610 | struct key *key; | |
0b58b8a1 | 611 | |
df423a4a | 612 | DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name); |
0b58b8a1 | 613 | |
df423a4a | 614 | _enter(""); |
0b58b8a1 | 615 | |
540b1c48 DH |
616 | if (!msg->msg_name) { |
617 | release_sock(&rx->sk); | |
df423a4a | 618 | return ERR_PTR(-EDESTADDRREQ); |
540b1c48 | 619 | } |
0b58b8a1 | 620 | |
df423a4a DH |
621 | key = rx->key; |
622 | if (key && !rx->key->payload.data[0]) | |
623 | key = NULL; | |
0b58b8a1 | 624 | |
df423a4a DH |
625 | memset(&cp, 0, sizeof(cp)); |
626 | cp.local = rx->local; | |
627 | cp.key = rx->key; | |
628 | cp.security_level = rx->min_sec_level; | |
3ab26a6f DH |
629 | cp.exclusive = rx->exclusive | p->exclusive; |
630 | cp.upgrade = p->upgrade; | |
df423a4a | 631 | cp.service_id = srx->srx_service; |
a25e21f0 DH |
632 | call = rxrpc_new_client_call(rx, &cp, srx, &p->call, GFP_KERNEL, |
633 | atomic_inc_return(&rxrpc_debug_id)); | |
540b1c48 | 634 | /* The socket is now unlocked */ |
0b58b8a1 | 635 | |
17226f12 | 636 | rxrpc_put_peer(cp.peer); |
df423a4a DH |
637 | _leave(" = %p\n", call); |
638 | return call; | |
639 | } | |
0b58b8a1 | 640 | |
df423a4a DH |
641 | /* |
642 | * send a message forming part of a client call through an RxRPC socket | |
643 | * - caller holds the socket locked | |
644 | * - the socket may be either a client socket or a server socket | |
645 | */ | |
646 | int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len) | |
540b1c48 | 647 | __releases(&rx->sk.sk_lock.slock) |
88f2a825 | 648 | __releases(&call->user_mutex) |
df423a4a | 649 | { |
146d8fef | 650 | enum rxrpc_call_state state; |
df423a4a | 651 | struct rxrpc_call *call; |
a158bdd3 | 652 | unsigned long now, j; |
b0f571ec | 653 | bool dropped_lock = false; |
df423a4a | 654 | int ret; |
0b58b8a1 | 655 | |
3ab26a6f | 656 | struct rxrpc_send_params p = { |
48124178 DH |
657 | .call.tx_total_len = -1, |
658 | .call.user_call_ID = 0, | |
a158bdd3 | 659 | .call.nr_timeouts = 0, |
e138aa7d | 660 | .call.interruptibility = RXRPC_INTERRUPTIBLE, |
48124178 DH |
661 | .abort_code = 0, |
662 | .command = RXRPC_CMD_SEND_DATA, | |
663 | .exclusive = false, | |
664 | .upgrade = false, | |
3ab26a6f DH |
665 | }; |
666 | ||
df423a4a | 667 | _enter(""); |
0b58b8a1 | 668 | |
3ab26a6f | 669 | ret = rxrpc_sendmsg_cmsg(msg, &p); |
df423a4a | 670 | if (ret < 0) |
540b1c48 | 671 | goto error_release_sock; |
0b58b8a1 | 672 | |
2d914c1b | 673 | if (p.command == RXRPC_CMD_CHARGE_ACCEPT) { |
540b1c48 | 674 | ret = -EINVAL; |
df423a4a | 675 | if (rx->sk.sk_state != RXRPC_SERVER_LISTENING) |
540b1c48 | 676 | goto error_release_sock; |
2d914c1b DH |
677 | ret = rxrpc_user_charge_accept(rx, p.call.user_call_ID); |
678 | goto error_release_sock; | |
df423a4a | 679 | } |
0b58b8a1 | 680 | |
48124178 | 681 | call = rxrpc_find_call_by_user_ID(rx, p.call.user_call_ID); |
df423a4a | 682 | if (!call) { |
540b1c48 | 683 | ret = -EBADSLT; |
3ab26a6f | 684 | if (p.command != RXRPC_CMD_SEND_DATA) |
540b1c48 | 685 | goto error_release_sock; |
3ab26a6f | 686 | call = rxrpc_new_client_call_for_sendmsg(rx, msg, &p); |
540b1c48 | 687 | /* The socket is now unlocked... */ |
df423a4a DH |
688 | if (IS_ERR(call)) |
689 | return PTR_ERR(call); | |
540b1c48 | 690 | /* ... and we have the call lock. */ |
65550098 DH |
691 | ret = 0; |
692 | if (READ_ONCE(call->state) == RXRPC_CALL_COMPLETE) | |
693 | goto out_put_unlock; | |
540b1c48 | 694 | } else { |
146d8fef DH |
695 | switch (READ_ONCE(call->state)) { |
696 | case RXRPC_CALL_UNINITIALISED: | |
697 | case RXRPC_CALL_CLIENT_AWAIT_CONN: | |
698 | case RXRPC_CALL_SERVER_PREALLOC: | |
699 | case RXRPC_CALL_SERVER_SECURING: | |
c48fc11b | 700 | rxrpc_put_call(call, rxrpc_call_put); |
146d8fef | 701 | ret = -EBUSY; |
37411cad | 702 | goto error_release_sock; |
146d8fef DH |
703 | default: |
704 | break; | |
705 | } | |
37411cad | 706 | |
540b1c48 DH |
707 | ret = mutex_lock_interruptible(&call->user_mutex); |
708 | release_sock(&rx->sk); | |
709 | if (ret < 0) { | |
710 | ret = -ERESTARTSYS; | |
711 | goto error_put; | |
712 | } | |
e754eba6 | 713 | |
48124178 | 714 | if (p.call.tx_total_len != -1) { |
e754eba6 DH |
715 | ret = -EINVAL; |
716 | if (call->tx_total_len != -1 || | |
717 | call->tx_pending || | |
718 | call->tx_top != 0) | |
719 | goto error_put; | |
48124178 | 720 | call->tx_total_len = p.call.tx_total_len; |
e754eba6 | 721 | } |
df423a4a | 722 | } |
0b58b8a1 | 723 | |
a158bdd3 DH |
724 | switch (p.call.nr_timeouts) { |
725 | case 3: | |
726 | j = msecs_to_jiffies(p.call.timeouts.normal); | |
727 | if (p.call.timeouts.normal > 0 && j == 0) | |
728 | j = 1; | |
729 | WRITE_ONCE(call->next_rx_timo, j); | |
df561f66 | 730 | fallthrough; |
a158bdd3 DH |
731 | case 2: |
732 | j = msecs_to_jiffies(p.call.timeouts.idle); | |
733 | if (p.call.timeouts.idle > 0 && j == 0) | |
734 | j = 1; | |
735 | WRITE_ONCE(call->next_req_timo, j); | |
df561f66 | 736 | fallthrough; |
a158bdd3 DH |
737 | case 1: |
738 | if (p.call.timeouts.hard > 0) { | |
739 | j = msecs_to_jiffies(p.call.timeouts.hard); | |
740 | now = jiffies; | |
741 | j += now; | |
742 | WRITE_ONCE(call->expect_term_by, j); | |
743 | rxrpc_reduce_call_timer(call, j, now, | |
744 | rxrpc_timer_set_for_hard); | |
745 | } | |
746 | break; | |
747 | } | |
748 | ||
146d8fef | 749 | state = READ_ONCE(call->state); |
df423a4a | 750 | _debug("CALL %d USR %lx ST %d on CONN %p", |
146d8fef | 751 | call->debug_id, call->user_call_ID, state, call->conn); |
0b58b8a1 | 752 | |
146d8fef | 753 | if (state >= RXRPC_CALL_COMPLETE) { |
df423a4a DH |
754 | /* it's too late for this call */ |
755 | ret = -ESHUTDOWN; | |
3ab26a6f | 756 | } else if (p.command == RXRPC_CMD_SEND_ABORT) { |
df423a4a | 757 | ret = 0; |
3ab26a6f | 758 | if (rxrpc_abort_call("CMD", call, 0, p.abort_code, -ECONNABORTED)) |
26cb02aa | 759 | ret = rxrpc_send_abort_packet(call); |
3ab26a6f | 760 | } else if (p.command != RXRPC_CMD_SEND_DATA) { |
df423a4a | 761 | ret = -EINVAL; |
df423a4a | 762 | } else { |
b0f571ec | 763 | ret = rxrpc_send_data(rx, call, msg, len, NULL, &dropped_lock); |
df423a4a | 764 | } |
0b58b8a1 | 765 | |
03a6c822 | 766 | out_put_unlock: |
b0f571ec DH |
767 | if (!dropped_lock) |
768 | mutex_unlock(&call->user_mutex); | |
540b1c48 | 769 | error_put: |
fff72429 | 770 | rxrpc_put_call(call, rxrpc_call_put); |
df423a4a DH |
771 | _leave(" = %d", ret); |
772 | return ret; | |
540b1c48 DH |
773 | |
774 | error_release_sock: | |
775 | release_sock(&rx->sk); | |
776 | return ret; | |
df423a4a | 777 | } |
0b58b8a1 | 778 | |
df423a4a DH |
779 | /** |
780 | * rxrpc_kernel_send_data - Allow a kernel service to send data on a call | |
781 | * @sock: The socket the call is on | |
782 | * @call: The call to send data through | |
783 | * @msg: The data to send | |
784 | * @len: The amount of data to send | |
e833251a | 785 | * @notify_end_tx: Notification that the last packet is queued. |
df423a4a DH |
786 | * |
787 | * Allow a kernel service to send data on a call. The call must be in an state | |
788 | * appropriate to sending data. No control data should be supplied in @msg, | |
789 | * nor should an address be supplied. MSG_MORE should be flagged if there's | |
790 | * more data to come, otherwise this data will end the transmission phase. | |
791 | */ | |
792 | int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call, | |
e833251a DH |
793 | struct msghdr *msg, size_t len, |
794 | rxrpc_notify_end_tx_t notify_end_tx) | |
df423a4a | 795 | { |
b0f571ec | 796 | bool dropped_lock = false; |
df423a4a | 797 | int ret; |
0b58b8a1 | 798 | |
df423a4a | 799 | _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]); |
0b58b8a1 | 800 | |
df423a4a DH |
801 | ASSERTCMP(msg->msg_name, ==, NULL); |
802 | ASSERTCMP(msg->msg_control, ==, NULL); | |
0b58b8a1 | 803 | |
540b1c48 | 804 | mutex_lock(&call->user_mutex); |
0b58b8a1 | 805 | |
df423a4a DH |
806 | _debug("CALL %d USR %lx ST %d on CONN %p", |
807 | call->debug_id, call->user_call_ID, call->state, call->conn); | |
0b58b8a1 | 808 | |
146d8fef DH |
809 | switch (READ_ONCE(call->state)) { |
810 | case RXRPC_CALL_CLIENT_SEND_REQUEST: | |
811 | case RXRPC_CALL_SERVER_ACK_REQUEST: | |
812 | case RXRPC_CALL_SERVER_SEND_REPLY: | |
e833251a | 813 | ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len, |
b0f571ec | 814 | notify_end_tx, &dropped_lock); |
146d8fef DH |
815 | break; |
816 | case RXRPC_CALL_COMPLETE: | |
6fc166d6 | 817 | read_lock_bh(&call->state_lock); |
bd2db2d2 | 818 | ret = call->error; |
6fc166d6 | 819 | read_unlock_bh(&call->state_lock); |
146d8fef DH |
820 | break; |
821 | default: | |
fb46f6ee DH |
822 | /* Request phase complete for this client call */ |
823 | trace_rxrpc_rx_eproto(call, 0, tracepoint_string("late_send")); | |
146d8fef DH |
824 | ret = -EPROTO; |
825 | break; | |
df423a4a DH |
826 | } |
827 | ||
b0f571ec DH |
828 | if (!dropped_lock) |
829 | mutex_unlock(&call->user_mutex); | |
0b58b8a1 DH |
830 | _leave(" = %d", ret); |
831 | return ret; | |
df423a4a DH |
832 | } |
833 | EXPORT_SYMBOL(rxrpc_kernel_send_data); | |
0b58b8a1 | 834 | |
df423a4a DH |
835 | /** |
836 | * rxrpc_kernel_abort_call - Allow a kernel service to abort a call | |
837 | * @sock: The socket the call is on | |
838 | * @call: The call to be aborted | |
839 | * @abort_code: The abort code to stick into the ABORT packet | |
5a42976d DH |
840 | * @error: Local error value |
841 | * @why: 3-char string indicating why. | |
df423a4a | 842 | * |
84a4c09c DH |
843 | * Allow a kernel service to abort a call, if it's still in an abortable state |
844 | * and return true if the call was aborted, false if it was already complete. | |
df423a4a | 845 | */ |
84a4c09c | 846 | bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call, |
5a42976d | 847 | u32 abort_code, int error, const char *why) |
df423a4a | 848 | { |
84a4c09c DH |
849 | bool aborted; |
850 | ||
5a42976d | 851 | _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why); |
0b58b8a1 | 852 | |
540b1c48 | 853 | mutex_lock(&call->user_mutex); |
0b58b8a1 | 854 | |
84a4c09c DH |
855 | aborted = rxrpc_abort_call(why, call, 0, abort_code, error); |
856 | if (aborted) | |
26cb02aa | 857 | rxrpc_send_abort_packet(call); |
df423a4a | 858 | |
540b1c48 | 859 | mutex_unlock(&call->user_mutex); |
84a4c09c | 860 | return aborted; |
0b58b8a1 | 861 | } |
df423a4a | 862 | EXPORT_SYMBOL(rxrpc_kernel_abort_call); |
e754eba6 DH |
863 | |
864 | /** | |
865 | * rxrpc_kernel_set_tx_length - Set the total Tx length on a call | |
866 | * @sock: The socket the call is on | |
867 | * @call: The call to be informed | |
868 | * @tx_total_len: The amount of data to be transmitted for this call | |
869 | * | |
870 | * Allow a kernel service to set the total transmit length on a call. This | |
871 | * allows buffer-to-packet encrypt-and-copy to be performed. | |
872 | * | |
873 | * This function is primarily for use for setting the reply length since the | |
874 | * request length can be set when beginning the call. | |
875 | */ | |
876 | void rxrpc_kernel_set_tx_length(struct socket *sock, struct rxrpc_call *call, | |
877 | s64 tx_total_len) | |
878 | { | |
879 | WARN_ON(call->tx_total_len != -1); | |
880 | call->tx_total_len = tx_total_len; | |
881 | } | |
882 | EXPORT_SYMBOL(rxrpc_kernel_set_tx_length); |