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