]>
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 | 225 | if (ret < 0) { |
7d8b6b66 DH |
226 | switch (ret) { |
227 | case -ENETUNREACH: | |
228 | case -EHOSTUNREACH: | |
229 | case -ECONNREFUSED: | |
230 | rxrpc_set_call_completion(call, | |
231 | RXRPC_CALL_LOCAL_ERROR, | |
232 | 0, ret); | |
afac2b79 | 233 | rxrpc_notify_socket(call); |
7d8b6b66 DH |
234 | goto out; |
235 | } | |
df423a4a | 236 | _debug("need instant resend %d", ret); |
248f219c | 237 | rxrpc_instant_resend(call, ix); |
dfc3da44 | 238 | } else { |
a158bdd3 | 239 | unsigned long now = jiffies, resend_at; |
dfc3da44 | 240 | |
beb8e5e4 DH |
241 | if (call->peer->rtt_usage > 1) |
242 | resend_at = nsecs_to_jiffies(call->peer->rtt * 3 / 2); | |
243 | else | |
244 | resend_at = rxrpc_resend_timeout; | |
245 | if (resend_at < 1) | |
246 | resend_at = 1; | |
247 | ||
282ef472 | 248 | resend_at += now; |
a158bdd3 DH |
249 | WRITE_ONCE(call->resend_at, resend_at); |
250 | rxrpc_reduce_call_timer(call, resend_at, now, | |
251 | rxrpc_timer_set_for_send); | |
df423a4a DH |
252 | } |
253 | ||
7d8b6b66 | 254 | out: |
71f3ca40 | 255 | rxrpc_free_skb(skb, rxrpc_skb_tx_freed); |
df423a4a | 256 | _leave(""); |
0b58b8a1 DH |
257 | } |
258 | ||
df423a4a DH |
259 | /* |
260 | * send data through a socket | |
261 | * - must be called in process context | |
540b1c48 | 262 | * - The caller holds the call user access mutex, but not the socket lock. |
0b58b8a1 | 263 | */ |
df423a4a DH |
264 | static int rxrpc_send_data(struct rxrpc_sock *rx, |
265 | struct rxrpc_call *call, | |
e833251a DH |
266 | struct msghdr *msg, size_t len, |
267 | rxrpc_notify_end_tx_t notify_end_tx) | |
0b58b8a1 | 268 | { |
df423a4a DH |
269 | struct rxrpc_skb_priv *sp; |
270 | struct sk_buff *skb; | |
271 | struct sock *sk = &rx->sk; | |
272 | long timeo; | |
273 | bool more; | |
274 | int ret, copied; | |
0b58b8a1 | 275 | |
df423a4a | 276 | timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
0b58b8a1 | 277 | |
df423a4a DH |
278 | /* this should be in poll */ |
279 | sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); | |
0b58b8a1 | 280 | |
df423a4a DH |
281 | if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) |
282 | return -EPIPE; | |
0b58b8a1 | 283 | |
df423a4a | 284 | more = msg->msg_flags & MSG_MORE; |
0b58b8a1 | 285 | |
e754eba6 DH |
286 | if (call->tx_total_len != -1) { |
287 | if (len > call->tx_total_len) | |
288 | return -EMSGSIZE; | |
289 | if (!more && len != call->tx_total_len) | |
290 | return -EMSGSIZE; | |
291 | } | |
292 | ||
df423a4a DH |
293 | skb = call->tx_pending; |
294 | call->tx_pending = NULL; | |
71f3ca40 | 295 | rxrpc_see_skb(skb, rxrpc_skb_tx_seen); |
0b58b8a1 | 296 | |
df423a4a DH |
297 | copied = 0; |
298 | do { | |
7aa51da7 DH |
299 | /* Check to see if there's a ping ACK to reply to. */ |
300 | if (call->ackr_reason == RXRPC_ACK_PING_RESPONSE) | |
bd1fdf8c | 301 | rxrpc_send_ack_packet(call, false, NULL); |
7aa51da7 | 302 | |
df423a4a DH |
303 | if (!skb) { |
304 | size_t size, chunk, max, space; | |
0b58b8a1 | 305 | |
df423a4a | 306 | _debug("alloc"); |
0b58b8a1 | 307 | |
248f219c | 308 | if (call->tx_top - call->tx_hard_ack >= |
57494343 DH |
309 | min_t(unsigned int, call->tx_winsize, |
310 | call->cong_cwnd + call->cong_extra)) { | |
df423a4a DH |
311 | ret = -EAGAIN; |
312 | if (msg->msg_flags & MSG_DONTWAIT) | |
313 | goto maybe_error; | |
314 | ret = rxrpc_wait_for_tx_window(rx, call, | |
bc5e3a54 DH |
315 | &timeo, |
316 | msg->msg_flags & MSG_WAITALL); | |
df423a4a DH |
317 | if (ret < 0) |
318 | goto maybe_error; | |
319 | } | |
0b58b8a1 | 320 | |
182f5056 | 321 | max = RXRPC_JUMBO_DATALEN; |
df423a4a DH |
322 | max -= call->conn->security_size; |
323 | max &= ~(call->conn->size_align - 1UL); | |
0b58b8a1 | 324 | |
df423a4a DH |
325 | chunk = max; |
326 | if (chunk > msg_data_left(msg) && !more) | |
327 | chunk = msg_data_left(msg); | |
0b58b8a1 | 328 | |
df423a4a DH |
329 | space = chunk + call->conn->size_align; |
330 | space &= ~(call->conn->size_align - 1UL); | |
0b58b8a1 | 331 | |
5a924b89 | 332 | size = space + call->conn->security_size; |
0b58b8a1 | 333 | |
df423a4a | 334 | _debug("SIZE: %zu/%zu/%zu", chunk, space, size); |
0b58b8a1 | 335 | |
df423a4a DH |
336 | /* create a buffer that we can retain until it's ACK'd */ |
337 | skb = sock_alloc_send_skb( | |
338 | sk, size, msg->msg_flags & MSG_DONTWAIT, &ret); | |
339 | if (!skb) | |
340 | goto maybe_error; | |
0b58b8a1 | 341 | |
71f3ca40 | 342 | rxrpc_new_skb(skb, rxrpc_skb_tx_new); |
0b58b8a1 | 343 | |
df423a4a | 344 | _debug("ALLOC SEND %p", skb); |
0b58b8a1 | 345 | |
df423a4a | 346 | ASSERTCMP(skb->mark, ==, 0); |
0b58b8a1 | 347 | |
5a924b89 DH |
348 | _debug("HS: %u", call->conn->security_size); |
349 | skb_reserve(skb, call->conn->security_size); | |
350 | skb->len += call->conn->security_size; | |
0b58b8a1 | 351 | |
df423a4a DH |
352 | sp = rxrpc_skb(skb); |
353 | sp->remain = chunk; | |
354 | if (sp->remain > skb_tailroom(skb)) | |
355 | sp->remain = skb_tailroom(skb); | |
0b58b8a1 | 356 | |
df423a4a DH |
357 | _net("skb: hr %d, tr %d, hl %d, rm %d", |
358 | skb_headroom(skb), | |
359 | skb_tailroom(skb), | |
360 | skb_headlen(skb), | |
361 | sp->remain); | |
0b58b8a1 | 362 | |
df423a4a DH |
363 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
364 | } | |
0b58b8a1 | 365 | |
df423a4a DH |
366 | _debug("append"); |
367 | sp = rxrpc_skb(skb); | |
0b58b8a1 | 368 | |
df423a4a DH |
369 | /* append next segment of data to the current buffer */ |
370 | if (msg_data_left(msg) > 0) { | |
371 | int copy = skb_tailroom(skb); | |
372 | ASSERTCMP(copy, >, 0); | |
373 | if (copy > msg_data_left(msg)) | |
374 | copy = msg_data_left(msg); | |
375 | if (copy > sp->remain) | |
376 | copy = sp->remain; | |
0b58b8a1 | 377 | |
df423a4a DH |
378 | _debug("add"); |
379 | ret = skb_add_data(skb, &msg->msg_iter, copy); | |
380 | _debug("added"); | |
381 | if (ret < 0) | |
382 | goto efault; | |
383 | sp->remain -= copy; | |
384 | skb->mark += copy; | |
385 | copied += copy; | |
e754eba6 DH |
386 | if (call->tx_total_len != -1) |
387 | call->tx_total_len -= copy; | |
0b58b8a1 DH |
388 | } |
389 | ||
df423a4a DH |
390 | /* add the packet to the send queue if it's now full */ |
391 | if (sp->remain <= 0 || | |
392 | (msg_data_left(msg) == 0 && !more)) { | |
393 | struct rxrpc_connection *conn = call->conn; | |
394 | uint32_t seq; | |
395 | size_t pad; | |
0b58b8a1 | 396 | |
df423a4a DH |
397 | /* pad out if we're using security */ |
398 | if (conn->security_ix) { | |
399 | pad = conn->security_size + skb->mark; | |
400 | pad = conn->size_align - pad; | |
401 | pad &= conn->size_align - 1; | |
402 | _debug("pad %zu", pad); | |
403 | if (pad) | |
b080db58 | 404 | skb_put_zero(skb, pad); |
df423a4a | 405 | } |
0b58b8a1 | 406 | |
248f219c | 407 | seq = call->tx_top + 1; |
0b58b8a1 | 408 | |
df423a4a | 409 | sp->hdr.seq = seq; |
df423a4a | 410 | sp->hdr._rsvd = 0; |
5a924b89 | 411 | sp->hdr.flags = conn->out_clientflag; |
0b58b8a1 | 412 | |
df423a4a DH |
413 | if (msg_data_left(msg) == 0 && !more) |
414 | sp->hdr.flags |= RXRPC_LAST_PACKET; | |
248f219c DH |
415 | else if (call->tx_top - call->tx_hard_ack < |
416 | call->tx_winsize) | |
df423a4a | 417 | sp->hdr.flags |= RXRPC_MORE_PACKETS; |
0b58b8a1 | 418 | |
df423a4a | 419 | ret = conn->security->secure_packet( |
5a924b89 | 420 | call, skb, skb->mark, skb->head); |
df423a4a DH |
421 | if (ret < 0) |
422 | goto out; | |
0b58b8a1 | 423 | |
e833251a DH |
424 | rxrpc_queue_packet(rx, call, skb, |
425 | !msg_data_left(msg) && !more, | |
426 | notify_end_tx); | |
df423a4a DH |
427 | skb = NULL; |
428 | } | |
c038a58c DH |
429 | |
430 | /* Check for the far side aborting the call or a network error | |
431 | * occurring. If this happens, save any packet that was under | |
432 | * construction so that in the case of a network error, the | |
433 | * call can be retried or redirected. | |
434 | */ | |
435 | if (call->state == RXRPC_CALL_COMPLETE) { | |
436 | ret = call->error; | |
437 | goto out; | |
438 | } | |
df423a4a | 439 | } while (msg_data_left(msg) > 0); |
0b58b8a1 | 440 | |
df423a4a DH |
441 | success: |
442 | ret = copied; | |
443 | out: | |
444 | call->tx_pending = skb; | |
445 | _leave(" = %d", ret); | |
446 | return ret; | |
0b58b8a1 | 447 | |
df423a4a DH |
448 | maybe_error: |
449 | if (copied) | |
450 | goto success; | |
451 | goto out; | |
0b58b8a1 | 452 | |
df423a4a DH |
453 | efault: |
454 | ret = -EFAULT; | |
455 | goto out; | |
0b58b8a1 DH |
456 | } |
457 | ||
458 | /* | |
df423a4a | 459 | * extract control messages from the sendmsg() control buffer |
0b58b8a1 | 460 | */ |
3ab26a6f | 461 | static int rxrpc_sendmsg_cmsg(struct msghdr *msg, struct rxrpc_send_params *p) |
0b58b8a1 | 462 | { |
df423a4a DH |
463 | struct cmsghdr *cmsg; |
464 | bool got_user_ID = false; | |
465 | int len; | |
0b58b8a1 | 466 | |
df423a4a DH |
467 | if (msg->msg_controllen == 0) |
468 | return -EINVAL; | |
0b58b8a1 | 469 | |
df423a4a DH |
470 | for_each_cmsghdr(cmsg, msg) { |
471 | if (!CMSG_OK(msg, cmsg)) | |
472 | return -EINVAL; | |
0b58b8a1 | 473 | |
1ff8cebf | 474 | len = cmsg->cmsg_len - sizeof(struct cmsghdr); |
df423a4a DH |
475 | _debug("CMSG %d, %d, %d", |
476 | cmsg->cmsg_level, cmsg->cmsg_type, len); | |
0b58b8a1 | 477 | |
df423a4a DH |
478 | if (cmsg->cmsg_level != SOL_RXRPC) |
479 | continue; | |
0b58b8a1 | 480 | |
df423a4a DH |
481 | switch (cmsg->cmsg_type) { |
482 | case RXRPC_USER_CALL_ID: | |
483 | if (msg->msg_flags & MSG_CMSG_COMPAT) { | |
484 | if (len != sizeof(u32)) | |
485 | return -EINVAL; | |
48124178 | 486 | p->call.user_call_ID = *(u32 *)CMSG_DATA(cmsg); |
df423a4a DH |
487 | } else { |
488 | if (len != sizeof(unsigned long)) | |
489 | return -EINVAL; | |
48124178 | 490 | p->call.user_call_ID = *(unsigned long *) |
df423a4a DH |
491 | CMSG_DATA(cmsg); |
492 | } | |
df423a4a DH |
493 | got_user_ID = true; |
494 | break; | |
0b58b8a1 | 495 | |
df423a4a | 496 | case RXRPC_ABORT: |
3ab26a6f | 497 | if (p->command != RXRPC_CMD_SEND_DATA) |
df423a4a | 498 | return -EINVAL; |
3ab26a6f DH |
499 | p->command = RXRPC_CMD_SEND_ABORT; |
500 | if (len != sizeof(p->abort_code)) | |
df423a4a | 501 | return -EINVAL; |
3ab26a6f DH |
502 | p->abort_code = *(unsigned int *)CMSG_DATA(cmsg); |
503 | if (p->abort_code == 0) | |
df423a4a DH |
504 | return -EINVAL; |
505 | break; | |
0b58b8a1 | 506 | |
df423a4a | 507 | case RXRPC_ACCEPT: |
3ab26a6f | 508 | if (p->command != RXRPC_CMD_SEND_DATA) |
df423a4a | 509 | return -EINVAL; |
3ab26a6f | 510 | p->command = RXRPC_CMD_ACCEPT; |
df423a4a DH |
511 | if (len != 0) |
512 | return -EINVAL; | |
513 | break; | |
0b58b8a1 | 514 | |
df423a4a | 515 | case RXRPC_EXCLUSIVE_CALL: |
3ab26a6f | 516 | p->exclusive = true; |
df423a4a DH |
517 | if (len != 0) |
518 | return -EINVAL; | |
519 | break; | |
4e255721 DH |
520 | |
521 | case RXRPC_UPGRADE_SERVICE: | |
3ab26a6f | 522 | p->upgrade = true; |
4e255721 DH |
523 | if (len != 0) |
524 | return -EINVAL; | |
525 | break; | |
526 | ||
e754eba6 | 527 | case RXRPC_TX_LENGTH: |
48124178 | 528 | if (p->call.tx_total_len != -1 || len != sizeof(__s64)) |
e754eba6 | 529 | return -EINVAL; |
48124178 DH |
530 | p->call.tx_total_len = *(__s64 *)CMSG_DATA(cmsg); |
531 | if (p->call.tx_total_len < 0) | |
e754eba6 DH |
532 | return -EINVAL; |
533 | break; | |
534 | ||
a158bdd3 DH |
535 | case RXRPC_SET_CALL_TIMEOUT: |
536 | if (len & 3 || len < 4 || len > 12) | |
537 | return -EINVAL; | |
538 | memcpy(&p->call.timeouts, CMSG_DATA(cmsg), len); | |
539 | p->call.nr_timeouts = len / 4; | |
540 | if (p->call.timeouts.hard > INT_MAX / HZ) | |
541 | return -ERANGE; | |
542 | if (p->call.nr_timeouts >= 2 && p->call.timeouts.idle > 60 * 60 * 1000) | |
543 | return -ERANGE; | |
544 | if (p->call.nr_timeouts >= 3 && p->call.timeouts.normal > 60 * 60 * 1000) | |
545 | return -ERANGE; | |
546 | break; | |
547 | ||
df423a4a DH |
548 | default: |
549 | return -EINVAL; | |
550 | } | |
551 | } | |
0b58b8a1 | 552 | |
df423a4a DH |
553 | if (!got_user_ID) |
554 | return -EINVAL; | |
48124178 | 555 | if (p->call.tx_total_len != -1 && p->command != RXRPC_CMD_SEND_DATA) |
e754eba6 | 556 | return -EINVAL; |
df423a4a DH |
557 | _leave(" = 0"); |
558 | return 0; | |
559 | } | |
0b58b8a1 | 560 | |
df423a4a DH |
561 | /* |
562 | * Create a new client call for sendmsg(). | |
540b1c48 DH |
563 | * - Called with the socket lock held, which it must release. |
564 | * - If it returns a call, the call's lock will need releasing by the caller. | |
df423a4a DH |
565 | */ |
566 | static struct rxrpc_call * | |
567 | rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, | |
3ab26a6f | 568 | struct rxrpc_send_params *p) |
540b1c48 | 569 | __releases(&rx->sk.sk_lock.slock) |
df423a4a DH |
570 | { |
571 | struct rxrpc_conn_parameters cp; | |
572 | struct rxrpc_call *call; | |
573 | struct key *key; | |
0b58b8a1 | 574 | |
df423a4a | 575 | DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name); |
0b58b8a1 | 576 | |
df423a4a | 577 | _enter(""); |
0b58b8a1 | 578 | |
540b1c48 DH |
579 | if (!msg->msg_name) { |
580 | release_sock(&rx->sk); | |
df423a4a | 581 | return ERR_PTR(-EDESTADDRREQ); |
540b1c48 | 582 | } |
0b58b8a1 | 583 | |
df423a4a DH |
584 | key = rx->key; |
585 | if (key && !rx->key->payload.data[0]) | |
586 | key = NULL; | |
0b58b8a1 | 587 | |
df423a4a DH |
588 | memset(&cp, 0, sizeof(cp)); |
589 | cp.local = rx->local; | |
590 | cp.key = rx->key; | |
591 | cp.security_level = rx->min_sec_level; | |
3ab26a6f DH |
592 | cp.exclusive = rx->exclusive | p->exclusive; |
593 | cp.upgrade = p->upgrade; | |
df423a4a | 594 | cp.service_id = srx->srx_service; |
48124178 | 595 | call = rxrpc_new_client_call(rx, &cp, srx, &p->call, GFP_KERNEL); |
540b1c48 | 596 | /* The socket is now unlocked */ |
0b58b8a1 | 597 | |
df423a4a DH |
598 | _leave(" = %p\n", call); |
599 | return call; | |
600 | } | |
0b58b8a1 | 601 | |
df423a4a DH |
602 | /* |
603 | * send a message forming part of a client call through an RxRPC socket | |
604 | * - caller holds the socket locked | |
605 | * - the socket may be either a client socket or a server socket | |
606 | */ | |
607 | int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len) | |
540b1c48 | 608 | __releases(&rx->sk.sk_lock.slock) |
df423a4a | 609 | { |
146d8fef | 610 | enum rxrpc_call_state state; |
df423a4a | 611 | struct rxrpc_call *call; |
a158bdd3 | 612 | unsigned long now, j; |
df423a4a | 613 | int ret; |
0b58b8a1 | 614 | |
3ab26a6f | 615 | struct rxrpc_send_params p = { |
48124178 DH |
616 | .call.tx_total_len = -1, |
617 | .call.user_call_ID = 0, | |
a158bdd3 | 618 | .call.nr_timeouts = 0, |
48124178 DH |
619 | .abort_code = 0, |
620 | .command = RXRPC_CMD_SEND_DATA, | |
621 | .exclusive = false, | |
622 | .upgrade = false, | |
3ab26a6f DH |
623 | }; |
624 | ||
df423a4a | 625 | _enter(""); |
0b58b8a1 | 626 | |
3ab26a6f | 627 | ret = rxrpc_sendmsg_cmsg(msg, &p); |
df423a4a | 628 | if (ret < 0) |
540b1c48 | 629 | goto error_release_sock; |
0b58b8a1 | 630 | |
3ab26a6f | 631 | if (p.command == RXRPC_CMD_ACCEPT) { |
540b1c48 | 632 | ret = -EINVAL; |
df423a4a | 633 | if (rx->sk.sk_state != RXRPC_SERVER_LISTENING) |
540b1c48 | 634 | goto error_release_sock; |
48124178 | 635 | call = rxrpc_accept_call(rx, p.call.user_call_ID, NULL); |
540b1c48 | 636 | /* The socket is now unlocked. */ |
df423a4a DH |
637 | if (IS_ERR(call)) |
638 | return PTR_ERR(call); | |
03a6c822 DH |
639 | ret = 0; |
640 | goto out_put_unlock; | |
df423a4a | 641 | } |
0b58b8a1 | 642 | |
48124178 | 643 | call = rxrpc_find_call_by_user_ID(rx, p.call.user_call_ID); |
df423a4a | 644 | if (!call) { |
540b1c48 | 645 | ret = -EBADSLT; |
3ab26a6f | 646 | if (p.command != RXRPC_CMD_SEND_DATA) |
540b1c48 | 647 | goto error_release_sock; |
3ab26a6f | 648 | call = rxrpc_new_client_call_for_sendmsg(rx, msg, &p); |
540b1c48 | 649 | /* The socket is now unlocked... */ |
df423a4a DH |
650 | if (IS_ERR(call)) |
651 | return PTR_ERR(call); | |
540b1c48 DH |
652 | /* ... and we have the call lock. */ |
653 | } else { | |
146d8fef DH |
654 | switch (READ_ONCE(call->state)) { |
655 | case RXRPC_CALL_UNINITIALISED: | |
656 | case RXRPC_CALL_CLIENT_AWAIT_CONN: | |
657 | case RXRPC_CALL_SERVER_PREALLOC: | |
658 | case RXRPC_CALL_SERVER_SECURING: | |
659 | case RXRPC_CALL_SERVER_ACCEPTING: | |
d7c957e3 | 660 | rxrpc_put_call(call, rxrpc_call_put); |
146d8fef | 661 | ret = -EBUSY; |
37411cad | 662 | goto error_release_sock; |
146d8fef DH |
663 | default: |
664 | break; | |
665 | } | |
37411cad | 666 | |
540b1c48 DH |
667 | ret = mutex_lock_interruptible(&call->user_mutex); |
668 | release_sock(&rx->sk); | |
669 | if (ret < 0) { | |
670 | ret = -ERESTARTSYS; | |
671 | goto error_put; | |
672 | } | |
e754eba6 | 673 | |
48124178 | 674 | if (p.call.tx_total_len != -1) { |
e754eba6 DH |
675 | ret = -EINVAL; |
676 | if (call->tx_total_len != -1 || | |
677 | call->tx_pending || | |
678 | call->tx_top != 0) | |
679 | goto error_put; | |
48124178 | 680 | call->tx_total_len = p.call.tx_total_len; |
e754eba6 | 681 | } |
df423a4a | 682 | } |
0b58b8a1 | 683 | |
a158bdd3 DH |
684 | switch (p.call.nr_timeouts) { |
685 | case 3: | |
686 | j = msecs_to_jiffies(p.call.timeouts.normal); | |
687 | if (p.call.timeouts.normal > 0 && j == 0) | |
688 | j = 1; | |
689 | WRITE_ONCE(call->next_rx_timo, j); | |
690 | /* Fall through */ | |
691 | case 2: | |
692 | j = msecs_to_jiffies(p.call.timeouts.idle); | |
693 | if (p.call.timeouts.idle > 0 && j == 0) | |
694 | j = 1; | |
695 | WRITE_ONCE(call->next_req_timo, j); | |
696 | /* Fall through */ | |
697 | case 1: | |
698 | if (p.call.timeouts.hard > 0) { | |
699 | j = msecs_to_jiffies(p.call.timeouts.hard); | |
700 | now = jiffies; | |
701 | j += now; | |
702 | WRITE_ONCE(call->expect_term_by, j); | |
703 | rxrpc_reduce_call_timer(call, j, now, | |
704 | rxrpc_timer_set_for_hard); | |
705 | } | |
706 | break; | |
707 | } | |
708 | ||
146d8fef | 709 | state = READ_ONCE(call->state); |
df423a4a | 710 | _debug("CALL %d USR %lx ST %d on CONN %p", |
146d8fef | 711 | call->debug_id, call->user_call_ID, state, call->conn); |
0b58b8a1 | 712 | |
146d8fef | 713 | if (state >= RXRPC_CALL_COMPLETE) { |
df423a4a DH |
714 | /* it's too late for this call */ |
715 | ret = -ESHUTDOWN; | |
3ab26a6f | 716 | } else if (p.command == RXRPC_CMD_SEND_ABORT) { |
df423a4a | 717 | ret = 0; |
3ab26a6f | 718 | if (rxrpc_abort_call("CMD", call, 0, p.abort_code, -ECONNABORTED)) |
26cb02aa | 719 | ret = rxrpc_send_abort_packet(call); |
3ab26a6f | 720 | } else if (p.command != RXRPC_CMD_SEND_DATA) { |
df423a4a DH |
721 | ret = -EINVAL; |
722 | } else if (rxrpc_is_client_call(call) && | |
146d8fef | 723 | state != RXRPC_CALL_CLIENT_SEND_REQUEST) { |
df423a4a DH |
724 | /* request phase complete for this client call */ |
725 | ret = -EPROTO; | |
726 | } else if (rxrpc_is_service_call(call) && | |
146d8fef DH |
727 | state != RXRPC_CALL_SERVER_ACK_REQUEST && |
728 | state != RXRPC_CALL_SERVER_SEND_REPLY) { | |
df423a4a DH |
729 | /* Reply phase not begun or not complete for service call. */ |
730 | ret = -EPROTO; | |
731 | } else { | |
e833251a | 732 | ret = rxrpc_send_data(rx, call, msg, len, NULL); |
df423a4a | 733 | } |
0b58b8a1 | 734 | |
03a6c822 | 735 | out_put_unlock: |
540b1c48 DH |
736 | mutex_unlock(&call->user_mutex); |
737 | error_put: | |
fff72429 | 738 | rxrpc_put_call(call, rxrpc_call_put); |
df423a4a DH |
739 | _leave(" = %d", ret); |
740 | return ret; | |
540b1c48 DH |
741 | |
742 | error_release_sock: | |
743 | release_sock(&rx->sk); | |
744 | return ret; | |
df423a4a | 745 | } |
0b58b8a1 | 746 | |
df423a4a DH |
747 | /** |
748 | * rxrpc_kernel_send_data - Allow a kernel service to send data on a call | |
749 | * @sock: The socket the call is on | |
750 | * @call: The call to send data through | |
751 | * @msg: The data to send | |
752 | * @len: The amount of data to send | |
e833251a | 753 | * @notify_end_tx: Notification that the last packet is queued. |
df423a4a DH |
754 | * |
755 | * Allow a kernel service to send data on a call. The call must be in an state | |
756 | * appropriate to sending data. No control data should be supplied in @msg, | |
757 | * nor should an address be supplied. MSG_MORE should be flagged if there's | |
758 | * more data to come, otherwise this data will end the transmission phase. | |
759 | */ | |
760 | int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call, | |
e833251a DH |
761 | struct msghdr *msg, size_t len, |
762 | rxrpc_notify_end_tx_t notify_end_tx) | |
df423a4a DH |
763 | { |
764 | int ret; | |
0b58b8a1 | 765 | |
df423a4a | 766 | _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]); |
0b58b8a1 | 767 | |
df423a4a DH |
768 | ASSERTCMP(msg->msg_name, ==, NULL); |
769 | ASSERTCMP(msg->msg_control, ==, NULL); | |
0b58b8a1 | 770 | |
540b1c48 | 771 | mutex_lock(&call->user_mutex); |
0b58b8a1 | 772 | |
df423a4a DH |
773 | _debug("CALL %d USR %lx ST %d on CONN %p", |
774 | call->debug_id, call->user_call_ID, call->state, call->conn); | |
0b58b8a1 | 775 | |
146d8fef DH |
776 | switch (READ_ONCE(call->state)) { |
777 | case RXRPC_CALL_CLIENT_SEND_REQUEST: | |
778 | case RXRPC_CALL_SERVER_ACK_REQUEST: | |
779 | case RXRPC_CALL_SERVER_SEND_REPLY: | |
e833251a DH |
780 | ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len, |
781 | notify_end_tx); | |
146d8fef DH |
782 | break; |
783 | case RXRPC_CALL_COMPLETE: | |
6fc166d6 | 784 | read_lock_bh(&call->state_lock); |
bd2db2d2 | 785 | ret = call->error; |
6fc166d6 | 786 | read_unlock_bh(&call->state_lock); |
146d8fef DH |
787 | break; |
788 | default: | |
fb46f6ee DH |
789 | /* Request phase complete for this client call */ |
790 | trace_rxrpc_rx_eproto(call, 0, tracepoint_string("late_send")); | |
146d8fef DH |
791 | ret = -EPROTO; |
792 | break; | |
df423a4a DH |
793 | } |
794 | ||
540b1c48 | 795 | mutex_unlock(&call->user_mutex); |
0b58b8a1 DH |
796 | _leave(" = %d", ret); |
797 | return ret; | |
df423a4a DH |
798 | } |
799 | EXPORT_SYMBOL(rxrpc_kernel_send_data); | |
0b58b8a1 | 800 | |
df423a4a DH |
801 | /** |
802 | * rxrpc_kernel_abort_call - Allow a kernel service to abort a call | |
803 | * @sock: The socket the call is on | |
804 | * @call: The call to be aborted | |
805 | * @abort_code: The abort code to stick into the ABORT packet | |
5a42976d DH |
806 | * @error: Local error value |
807 | * @why: 3-char string indicating why. | |
df423a4a | 808 | * |
84a4c09c DH |
809 | * Allow a kernel service to abort a call, if it's still in an abortable state |
810 | * and return true if the call was aborted, false if it was already complete. | |
df423a4a | 811 | */ |
84a4c09c | 812 | bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call, |
5a42976d | 813 | u32 abort_code, int error, const char *why) |
df423a4a | 814 | { |
84a4c09c DH |
815 | bool aborted; |
816 | ||
5a42976d | 817 | _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why); |
0b58b8a1 | 818 | |
540b1c48 | 819 | mutex_lock(&call->user_mutex); |
0b58b8a1 | 820 | |
84a4c09c DH |
821 | aborted = rxrpc_abort_call(why, call, 0, abort_code, error); |
822 | if (aborted) | |
26cb02aa | 823 | rxrpc_send_abort_packet(call); |
df423a4a | 824 | |
540b1c48 | 825 | mutex_unlock(&call->user_mutex); |
84a4c09c | 826 | return aborted; |
0b58b8a1 | 827 | } |
df423a4a | 828 | EXPORT_SYMBOL(rxrpc_kernel_abort_call); |
e754eba6 DH |
829 | |
830 | /** | |
831 | * rxrpc_kernel_set_tx_length - Set the total Tx length on a call | |
832 | * @sock: The socket the call is on | |
833 | * @call: The call to be informed | |
834 | * @tx_total_len: The amount of data to be transmitted for this call | |
835 | * | |
836 | * Allow a kernel service to set the total transmit length on a call. This | |
837 | * allows buffer-to-packet encrypt-and-copy to be performed. | |
838 | * | |
839 | * This function is primarily for use for setting the reply length since the | |
840 | * request length can be set when beginning the call. | |
841 | */ | |
842 | void rxrpc_kernel_set_tx_length(struct socket *sock, struct rxrpc_call *call, | |
843 | s64 tx_total_len) | |
844 | { | |
845 | WARN_ON(call->tx_total_len != -1); | |
846 | call->tx_total_len = tx_total_len; | |
847 | } | |
848 | EXPORT_SYMBOL(rxrpc_kernel_set_tx_length); |