]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/rxrpc/sendmsg.c
Merge tag '9p-for-5.7-2' of git://github.com/martinetd/linux
[mirror_ubuntu-jammy-kernel.git] / net / rxrpc / sendmsg.c
CommitLineData
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 */
23static 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 */
38static 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);
54 mutex_unlock(&call->user_mutex);
55 *timeo = schedule_timeout(*timeo);
56 if (mutex_lock_interruptible(&call->user_mutex) < 0)
57 return sock_intr_errno(*timeo);
58 }
59}
60
61/*
62 * Wait for space to appear in the Tx queue uninterruptibly, but with
63 * a timeout of 2*RTT if no progress was made and a signal occurred.
64 */
e138aa7d 65static int rxrpc_wait_for_tx_window_waitall(struct rxrpc_sock *rx,
bc5e3a54
DH
66 struct rxrpc_call *call)
67{
68 rxrpc_seq_t tx_start, tx_win;
69 signed long rtt2, timeout;
70 u64 rtt;
71
72 rtt = READ_ONCE(call->peer->rtt);
73 rtt2 = nsecs_to_jiffies64(rtt) * 2;
498b5776
DH
74 if (rtt2 < 2)
75 rtt2 = 2;
bc5e3a54
DH
76
77 timeout = rtt2;
78 tx_start = READ_ONCE(call->tx_hard_ack);
79
80 for (;;) {
81 set_current_state(TASK_UNINTERRUPTIBLE);
82
83 tx_win = READ_ONCE(call->tx_hard_ack);
158fe666 84 if (rxrpc_check_tx_space(call, &tx_win))
bc5e3a54
DH
85 return 0;
86
87 if (call->state >= RXRPC_CALL_COMPLETE)
88 return call->error;
89
e138aa7d 90 if (timeout == 0 &&
bc5e3a54
DH
91 tx_win == tx_start && signal_pending(current))
92 return -EINTR;
93
94 if (tx_win != tx_start) {
95 timeout = rtt2;
96 tx_start = tx_win;
97 }
98
99 trace_rxrpc_transmit(call, rxrpc_transmit_wait);
100 timeout = schedule_timeout(timeout);
101 }
102}
103
e138aa7d
DH
104/*
105 * Wait for space to appear in the Tx queue uninterruptibly.
106 */
107static int rxrpc_wait_for_tx_window_nonintr(struct rxrpc_sock *rx,
108 struct rxrpc_call *call,
109 long *timeo)
110{
111 for (;;) {
112 set_current_state(TASK_UNINTERRUPTIBLE);
113 if (rxrpc_check_tx_space(call, NULL))
114 return 0;
115
116 if (call->state >= RXRPC_CALL_COMPLETE)
117 return call->error;
118
119 trace_rxrpc_transmit(call, rxrpc_transmit_wait);
120 *timeo = schedule_timeout(*timeo);
121 }
122}
123
0b58b8a1 124/*
df423a4a
DH
125 * wait for space to appear in the transmit/ACK window
126 * - caller holds the socket locked
0b58b8a1 127 */
df423a4a
DH
128static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
129 struct rxrpc_call *call,
bc5e3a54
DH
130 long *timeo,
131 bool waitall)
0b58b8a1 132{
df423a4a
DH
133 DECLARE_WAITQUEUE(myself, current);
134 int ret;
0b58b8a1 135
248f219c
DH
136 _enter(",{%u,%u,%u}",
137 call->tx_hard_ack, call->tx_top, call->tx_winsize);
0b58b8a1 138
df423a4a 139 add_wait_queue(&call->waitq, &myself);
0b58b8a1 140
e138aa7d
DH
141 switch (call->interruptibility) {
142 case RXRPC_INTERRUPTIBLE:
143 if (waitall)
144 ret = rxrpc_wait_for_tx_window_waitall(rx, call);
145 else
146 ret = rxrpc_wait_for_tx_window_intr(rx, call, timeo);
147 break;
148 case RXRPC_PREINTERRUPTIBLE:
149 case RXRPC_UNINTERRUPTIBLE:
150 default:
151 ret = rxrpc_wait_for_tx_window_nonintr(rx, call, timeo);
152 break;
153 }
0b58b8a1 154
df423a4a
DH
155 remove_wait_queue(&call->waitq, &myself);
156 set_current_state(TASK_RUNNING);
157 _leave(" = %d", ret);
158 return ret;
0b58b8a1
DH
159}
160
161/*
248f219c 162 * Schedule an instant Tx resend.
0b58b8a1 163 */
248f219c 164static inline void rxrpc_instant_resend(struct rxrpc_call *call, int ix)
0b58b8a1 165{
248f219c
DH
166 spin_lock_bh(&call->lock);
167
168 if (call->state < RXRPC_CALL_COMPLETE) {
03877bf6
DH
169 call->rxtx_annotations[ix] =
170 (call->rxtx_annotations[ix] & RXRPC_TX_ANNO_LAST) |
171 RXRPC_TX_ANNO_RETRANS;
248f219c 172 if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
df423a4a 173 rxrpc_queue_call(call);
0b58b8a1 174 }
248f219c
DH
175
176 spin_unlock_bh(&call->lock);
0b58b8a1
DH
177}
178
e833251a
DH
179/*
180 * Notify the owner of the call that the transmit phase is ended and the last
181 * packet has been queued.
182 */
183static void rxrpc_notify_end_tx(struct rxrpc_sock *rx, struct rxrpc_call *call,
184 rxrpc_notify_end_tx_t notify_end_tx)
185{
186 if (notify_end_tx)
187 notify_end_tx(&rx->sk, call, call->user_call_ID);
188}
189
0b58b8a1 190/*
8e8715aa
MD
191 * Queue a DATA packet for transmission, set the resend timeout and send
192 * the packet immediately. Returns the error from rxrpc_send_data_packet()
193 * in case the caller wants to do something with it.
0b58b8a1 194 */
8e8715aa
MD
195static int rxrpc_queue_packet(struct rxrpc_sock *rx, struct rxrpc_call *call,
196 struct sk_buff *skb, bool last,
197 rxrpc_notify_end_tx_t notify_end_tx)
0b58b8a1 198{
df423a4a 199 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
a158bdd3 200 unsigned long now;
248f219c
DH
201 rxrpc_seq_t seq = sp->hdr.seq;
202 int ret, ix;
70790dbe 203 u8 annotation = RXRPC_TX_ANNO_UNACK;
248f219c
DH
204
205 _net("queue skb %p [%d]", skb, seq);
0b58b8a1 206
248f219c 207 ASSERTCMP(seq, ==, call->tx_top + 1);
0b58b8a1 208
e122d845 209 if (last)
70790dbe
DH
210 annotation |= RXRPC_TX_ANNO_LAST;
211
b24d2891
DH
212 /* We have to set the timestamp before queueing as the retransmit
213 * algorithm can see the packet as soon as we queue it.
214 */
215 skb->tstamp = ktime_get_real();
216
248f219c 217 ix = seq & RXRPC_RXTX_BUFF_MASK;
987db9f7 218 rxrpc_get_skb(skb, rxrpc_skb_got);
70790dbe 219 call->rxtx_annotations[ix] = annotation;
df423a4a 220 smp_wmb();
248f219c
DH
221 call->rxtx_buffer[ix] = skb;
222 call->tx_top = seq;
70790dbe 223 if (last)
a124fe3e 224 trace_rxrpc_transmit(call, rxrpc_transmit_queue_last);
70790dbe 225 else
a124fe3e 226 trace_rxrpc_transmit(call, rxrpc_transmit_queue);
0b58b8a1 227
df423a4a
DH
228 if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
229 _debug("________awaiting reply/ACK__________");
230 write_lock_bh(&call->state_lock);
231 switch (call->state) {
232 case RXRPC_CALL_CLIENT_SEND_REQUEST:
233 call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
e833251a 234 rxrpc_notify_end_tx(rx, call, notify_end_tx);
df423a4a
DH
235 break;
236 case RXRPC_CALL_SERVER_ACK_REQUEST:
237 call->state = RXRPC_CALL_SERVER_SEND_REPLY;
a158bdd3
DH
238 now = jiffies;
239 WRITE_ONCE(call->ack_at, now + MAX_JIFFY_OFFSET);
9749fd2b
DH
240 if (call->ackr_reason == RXRPC_ACK_DELAY)
241 call->ackr_reason = 0;
a158bdd3 242 trace_rxrpc_timer(call, rxrpc_timer_init_for_send_reply, now);
df423a4a
DH
243 if (!last)
244 break;
e3cf3970 245 /* Fall through */
df423a4a
DH
246 case RXRPC_CALL_SERVER_SEND_REPLY:
247 call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
e833251a 248 rxrpc_notify_end_tx(rx, call, notify_end_tx);
df423a4a
DH
249 break;
250 default:
251 break;
252 }
253 write_unlock_bh(&call->state_lock);
254 }
0b58b8a1 255
248f219c
DH
256 if (seq == 1 && rxrpc_is_client_call(call))
257 rxrpc_expose_client_call(call);
df423a4a 258
a1767077 259 ret = rxrpc_send_data_packet(call, skb, false);
df423a4a 260 if (ret < 0) {
c54e43d7
DH
261 switch (ret) {
262 case -ENETUNREACH:
263 case -EHOSTUNREACH:
264 case -ECONNREFUSED:
265 rxrpc_set_call_completion(call,
266 RXRPC_CALL_LOCAL_ERROR,
267 0, ret);
c69565ee 268 rxrpc_notify_socket(call);
c54e43d7
DH
269 goto out;
270 }
df423a4a 271 _debug("need instant resend %d", ret);
248f219c 272 rxrpc_instant_resend(call, ix);
dfc3da44 273 } else {
a158bdd3 274 unsigned long now = jiffies, resend_at;
dfc3da44 275
beb8e5e4
DH
276 if (call->peer->rtt_usage > 1)
277 resend_at = nsecs_to_jiffies(call->peer->rtt * 3 / 2);
278 else
279 resend_at = rxrpc_resend_timeout;
280 if (resend_at < 1)
281 resend_at = 1;
282
282ef472 283 resend_at += now;
a158bdd3
DH
284 WRITE_ONCE(call->resend_at, resend_at);
285 rxrpc_reduce_call_timer(call, resend_at, now,
286 rxrpc_timer_set_for_send);
df423a4a
DH
287 }
288
c54e43d7 289out:
987db9f7 290 rxrpc_free_skb(skb, rxrpc_skb_freed);
8e8715aa
MD
291 _leave(" = %d", ret);
292 return ret;
0b58b8a1
DH
293}
294
df423a4a
DH
295/*
296 * send data through a socket
297 * - must be called in process context
540b1c48 298 * - The caller holds the call user access mutex, but not the socket lock.
0b58b8a1 299 */
df423a4a
DH
300static int rxrpc_send_data(struct rxrpc_sock *rx,
301 struct rxrpc_call *call,
e833251a
DH
302 struct msghdr *msg, size_t len,
303 rxrpc_notify_end_tx_t notify_end_tx)
0b58b8a1 304{
df423a4a
DH
305 struct rxrpc_skb_priv *sp;
306 struct sk_buff *skb;
307 struct sock *sk = &rx->sk;
308 long timeo;
309 bool more;
310 int ret, copied;
0b58b8a1 311
df423a4a 312 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
0b58b8a1 313
df423a4a
DH
314 /* this should be in poll */
315 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
0b58b8a1 316
df423a4a
DH
317 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
318 return -EPIPE;
0b58b8a1 319
df423a4a 320 more = msg->msg_flags & MSG_MORE;
0b58b8a1 321
e754eba6
DH
322 if (call->tx_total_len != -1) {
323 if (len > call->tx_total_len)
324 return -EMSGSIZE;
325 if (!more && len != call->tx_total_len)
326 return -EMSGSIZE;
327 }
328
df423a4a
DH
329 skb = call->tx_pending;
330 call->tx_pending = NULL;
987db9f7 331 rxrpc_see_skb(skb, rxrpc_skb_seen);
0b58b8a1 332
df423a4a
DH
333 copied = 0;
334 do {
7aa51da7
DH
335 /* Check to see if there's a ping ACK to reply to. */
336 if (call->ackr_reason == RXRPC_ACK_PING_RESPONSE)
bd1fdf8c 337 rxrpc_send_ack_packet(call, false, NULL);
7aa51da7 338
df423a4a
DH
339 if (!skb) {
340 size_t size, chunk, max, space;
0b58b8a1 341
df423a4a 342 _debug("alloc");
0b58b8a1 343
158fe666 344 if (!rxrpc_check_tx_space(call, NULL)) {
df423a4a
DH
345 ret = -EAGAIN;
346 if (msg->msg_flags & MSG_DONTWAIT)
347 goto maybe_error;
348 ret = rxrpc_wait_for_tx_window(rx, call,
bc5e3a54
DH
349 &timeo,
350 msg->msg_flags & MSG_WAITALL);
df423a4a
DH
351 if (ret < 0)
352 goto maybe_error;
353 }
0b58b8a1 354
182f5056 355 max = RXRPC_JUMBO_DATALEN;
df423a4a
DH
356 max -= call->conn->security_size;
357 max &= ~(call->conn->size_align - 1UL);
0b58b8a1 358
df423a4a
DH
359 chunk = max;
360 if (chunk > msg_data_left(msg) && !more)
361 chunk = msg_data_left(msg);
0b58b8a1 362
df423a4a
DH
363 space = chunk + call->conn->size_align;
364 space &= ~(call->conn->size_align - 1UL);
0b58b8a1 365
5a924b89 366 size = space + call->conn->security_size;
0b58b8a1 367
df423a4a 368 _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
0b58b8a1 369
df423a4a
DH
370 /* create a buffer that we can retain until it's ACK'd */
371 skb = sock_alloc_send_skb(
372 sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
373 if (!skb)
374 goto maybe_error;
0b58b8a1 375
b311e684
DH
376 sp = rxrpc_skb(skb);
377 sp->rx_flags |= RXRPC_SKB_TX_BUFFER;
987db9f7 378 rxrpc_new_skb(skb, rxrpc_skb_new);
0b58b8a1 379
df423a4a 380 _debug("ALLOC SEND %p", skb);
0b58b8a1 381
df423a4a 382 ASSERTCMP(skb->mark, ==, 0);
0b58b8a1 383
5a924b89
DH
384 _debug("HS: %u", call->conn->security_size);
385 skb_reserve(skb, call->conn->security_size);
386 skb->len += call->conn->security_size;
0b58b8a1 387
df423a4a
DH
388 sp->remain = chunk;
389 if (sp->remain > skb_tailroom(skb))
390 sp->remain = skb_tailroom(skb);
0b58b8a1 391
df423a4a
DH
392 _net("skb: hr %d, tr %d, hl %d, rm %d",
393 skb_headroom(skb),
394 skb_tailroom(skb),
395 skb_headlen(skb),
396 sp->remain);
0b58b8a1 397
df423a4a
DH
398 skb->ip_summed = CHECKSUM_UNNECESSARY;
399 }
0b58b8a1 400
df423a4a
DH
401 _debug("append");
402 sp = rxrpc_skb(skb);
0b58b8a1 403
df423a4a
DH
404 /* append next segment of data to the current buffer */
405 if (msg_data_left(msg) > 0) {
406 int copy = skb_tailroom(skb);
407 ASSERTCMP(copy, >, 0);
408 if (copy > msg_data_left(msg))
409 copy = msg_data_left(msg);
410 if (copy > sp->remain)
411 copy = sp->remain;
0b58b8a1 412
df423a4a
DH
413 _debug("add");
414 ret = skb_add_data(skb, &msg->msg_iter, copy);
415 _debug("added");
416 if (ret < 0)
417 goto efault;
418 sp->remain -= copy;
419 skb->mark += copy;
420 copied += copy;
e754eba6
DH
421 if (call->tx_total_len != -1)
422 call->tx_total_len -= copy;
0b58b8a1
DH
423 }
424
e122d845
DH
425 /* check for the far side aborting the call or a network error
426 * occurring */
427 if (call->state == RXRPC_CALL_COMPLETE)
428 goto call_terminated;
429
df423a4a
DH
430 /* add the packet to the send queue if it's now full */
431 if (sp->remain <= 0 ||
432 (msg_data_left(msg) == 0 && !more)) {
433 struct rxrpc_connection *conn = call->conn;
434 uint32_t seq;
435 size_t pad;
0b58b8a1 436
df423a4a
DH
437 /* pad out if we're using security */
438 if (conn->security_ix) {
439 pad = conn->security_size + skb->mark;
440 pad = conn->size_align - pad;
441 pad &= conn->size_align - 1;
442 _debug("pad %zu", pad);
443 if (pad)
b080db58 444 skb_put_zero(skb, pad);
df423a4a 445 }
0b58b8a1 446
248f219c 447 seq = call->tx_top + 1;
0b58b8a1 448
df423a4a 449 sp->hdr.seq = seq;
df423a4a 450 sp->hdr._rsvd = 0;
5a924b89 451 sp->hdr.flags = conn->out_clientflag;
0b58b8a1 452
df423a4a
DH
453 if (msg_data_left(msg) == 0 && !more)
454 sp->hdr.flags |= RXRPC_LAST_PACKET;
248f219c
DH
455 else if (call->tx_top - call->tx_hard_ack <
456 call->tx_winsize)
df423a4a 457 sp->hdr.flags |= RXRPC_MORE_PACKETS;
0b58b8a1 458
91fcfbe8 459 ret = call->security->secure_packet(
5a924b89 460 call, skb, skb->mark, skb->head);
df423a4a
DH
461 if (ret < 0)
462 goto out;
0b58b8a1 463
8e8715aa
MD
464 ret = rxrpc_queue_packet(rx, call, skb,
465 !msg_data_left(msg) && !more,
466 notify_end_tx);
467 /* Should check for failure here */
df423a4a
DH
468 skb = NULL;
469 }
470 } while (msg_data_left(msg) > 0);
0b58b8a1 471
df423a4a
DH
472success:
473 ret = copied;
474out:
475 call->tx_pending = skb;
476 _leave(" = %d", ret);
477 return ret;
0b58b8a1 478
e122d845 479call_terminated:
987db9f7 480 rxrpc_free_skb(skb, rxrpc_skb_freed);
e122d845
DH
481 _leave(" = %d", call->error);
482 return call->error;
483
df423a4a
DH
484maybe_error:
485 if (copied)
486 goto success;
487 goto out;
0b58b8a1 488
df423a4a
DH
489efault:
490 ret = -EFAULT;
491 goto out;
0b58b8a1
DH
492}
493
494/*
df423a4a 495 * extract control messages from the sendmsg() control buffer
0b58b8a1 496 */
3ab26a6f 497static 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
df423a4a 543 case RXRPC_ACCEPT:
3ab26a6f 544 if (p->command != RXRPC_CMD_SEND_DATA)
df423a4a 545 return -EINVAL;
3ab26a6f 546 p->command = RXRPC_CMD_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 */
602static struct rxrpc_call *
603rxrpc_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 */
646int 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;
df423a4a 653 int ret;
0b58b8a1 654
3ab26a6f 655 struct rxrpc_send_params p = {
48124178
DH
656 .call.tx_total_len = -1,
657 .call.user_call_ID = 0,
a158bdd3 658 .call.nr_timeouts = 0,
e138aa7d 659 .call.interruptibility = RXRPC_INTERRUPTIBLE,
48124178
DH
660 .abort_code = 0,
661 .command = RXRPC_CMD_SEND_DATA,
662 .exclusive = false,
663 .upgrade = false,
3ab26a6f
DH
664 };
665
df423a4a 666 _enter("");
0b58b8a1 667
3ab26a6f 668 ret = rxrpc_sendmsg_cmsg(msg, &p);
df423a4a 669 if (ret < 0)
540b1c48 670 goto error_release_sock;
0b58b8a1 671
3ab26a6f 672 if (p.command == RXRPC_CMD_ACCEPT) {
540b1c48 673 ret = -EINVAL;
df423a4a 674 if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
540b1c48 675 goto error_release_sock;
48124178 676 call = rxrpc_accept_call(rx, p.call.user_call_ID, NULL);
540b1c48 677 /* The socket is now unlocked. */
df423a4a
DH
678 if (IS_ERR(call))
679 return PTR_ERR(call);
03a6c822
DH
680 ret = 0;
681 goto out_put_unlock;
df423a4a 682 }
0b58b8a1 683
48124178 684 call = rxrpc_find_call_by_user_ID(rx, p.call.user_call_ID);
df423a4a 685 if (!call) {
540b1c48 686 ret = -EBADSLT;
3ab26a6f 687 if (p.command != RXRPC_CMD_SEND_DATA)
540b1c48 688 goto error_release_sock;
3ab26a6f 689 call = rxrpc_new_client_call_for_sendmsg(rx, msg, &p);
540b1c48 690 /* The socket is now unlocked... */
df423a4a
DH
691 if (IS_ERR(call))
692 return PTR_ERR(call);
540b1c48
DH
693 /* ... and we have the call lock. */
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:
700 case RXRPC_CALL_SERVER_ACCEPTING:
c48fc11b 701 rxrpc_put_call(call, rxrpc_call_put);
146d8fef 702 ret = -EBUSY;
37411cad 703 goto error_release_sock;
146d8fef
DH
704 default:
705 break;
706 }
37411cad 707
540b1c48
DH
708 ret = mutex_lock_interruptible(&call->user_mutex);
709 release_sock(&rx->sk);
710 if (ret < 0) {
711 ret = -ERESTARTSYS;
712 goto error_put;
713 }
e754eba6 714
48124178 715 if (p.call.tx_total_len != -1) {
e754eba6
DH
716 ret = -EINVAL;
717 if (call->tx_total_len != -1 ||
718 call->tx_pending ||
719 call->tx_top != 0)
720 goto error_put;
48124178 721 call->tx_total_len = p.call.tx_total_len;
e754eba6 722 }
df423a4a 723 }
0b58b8a1 724
a158bdd3
DH
725 switch (p.call.nr_timeouts) {
726 case 3:
727 j = msecs_to_jiffies(p.call.timeouts.normal);
728 if (p.call.timeouts.normal > 0 && j == 0)
729 j = 1;
730 WRITE_ONCE(call->next_rx_timo, j);
731 /* Fall through */
732 case 2:
733 j = msecs_to_jiffies(p.call.timeouts.idle);
734 if (p.call.timeouts.idle > 0 && j == 0)
735 j = 1;
736 WRITE_ONCE(call->next_req_timo, j);
737 /* Fall through */
738 case 1:
739 if (p.call.timeouts.hard > 0) {
740 j = msecs_to_jiffies(p.call.timeouts.hard);
741 now = jiffies;
742 j += now;
743 WRITE_ONCE(call->expect_term_by, j);
744 rxrpc_reduce_call_timer(call, j, now,
745 rxrpc_timer_set_for_hard);
746 }
747 break;
748 }
749
146d8fef 750 state = READ_ONCE(call->state);
df423a4a 751 _debug("CALL %d USR %lx ST %d on CONN %p",
146d8fef 752 call->debug_id, call->user_call_ID, state, call->conn);
0b58b8a1 753
146d8fef 754 if (state >= RXRPC_CALL_COMPLETE) {
df423a4a
DH
755 /* it's too late for this call */
756 ret = -ESHUTDOWN;
3ab26a6f 757 } else if (p.command == RXRPC_CMD_SEND_ABORT) {
df423a4a 758 ret = 0;
3ab26a6f 759 if (rxrpc_abort_call("CMD", call, 0, p.abort_code, -ECONNABORTED))
26cb02aa 760 ret = rxrpc_send_abort_packet(call);
3ab26a6f 761 } else if (p.command != RXRPC_CMD_SEND_DATA) {
df423a4a
DH
762 ret = -EINVAL;
763 } else if (rxrpc_is_client_call(call) &&
146d8fef 764 state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
df423a4a
DH
765 /* request phase complete for this client call */
766 ret = -EPROTO;
767 } else if (rxrpc_is_service_call(call) &&
146d8fef
DH
768 state != RXRPC_CALL_SERVER_ACK_REQUEST &&
769 state != RXRPC_CALL_SERVER_SEND_REPLY) {
df423a4a
DH
770 /* Reply phase not begun or not complete for service call. */
771 ret = -EPROTO;
772 } else {
e833251a 773 ret = rxrpc_send_data(rx, call, msg, len, NULL);
df423a4a 774 }
0b58b8a1 775
03a6c822 776out_put_unlock:
540b1c48
DH
777 mutex_unlock(&call->user_mutex);
778error_put:
fff72429 779 rxrpc_put_call(call, rxrpc_call_put);
df423a4a
DH
780 _leave(" = %d", ret);
781 return ret;
540b1c48
DH
782
783error_release_sock:
784 release_sock(&rx->sk);
785 return ret;
df423a4a 786}
0b58b8a1 787
df423a4a
DH
788/**
789 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
790 * @sock: The socket the call is on
791 * @call: The call to send data through
792 * @msg: The data to send
793 * @len: The amount of data to send
e833251a 794 * @notify_end_tx: Notification that the last packet is queued.
df423a4a
DH
795 *
796 * Allow a kernel service to send data on a call. The call must be in an state
797 * appropriate to sending data. No control data should be supplied in @msg,
798 * nor should an address be supplied. MSG_MORE should be flagged if there's
799 * more data to come, otherwise this data will end the transmission phase.
800 */
801int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
e833251a
DH
802 struct msghdr *msg, size_t len,
803 rxrpc_notify_end_tx_t notify_end_tx)
df423a4a
DH
804{
805 int ret;
0b58b8a1 806
df423a4a 807 _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
0b58b8a1 808
df423a4a
DH
809 ASSERTCMP(msg->msg_name, ==, NULL);
810 ASSERTCMP(msg->msg_control, ==, NULL);
0b58b8a1 811
540b1c48 812 mutex_lock(&call->user_mutex);
0b58b8a1 813
df423a4a
DH
814 _debug("CALL %d USR %lx ST %d on CONN %p",
815 call->debug_id, call->user_call_ID, call->state, call->conn);
0b58b8a1 816
146d8fef
DH
817 switch (READ_ONCE(call->state)) {
818 case RXRPC_CALL_CLIENT_SEND_REQUEST:
819 case RXRPC_CALL_SERVER_ACK_REQUEST:
820 case RXRPC_CALL_SERVER_SEND_REPLY:
e833251a
DH
821 ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len,
822 notify_end_tx);
146d8fef
DH
823 break;
824 case RXRPC_CALL_COMPLETE:
6fc166d6 825 read_lock_bh(&call->state_lock);
bd2db2d2 826 ret = call->error;
6fc166d6 827 read_unlock_bh(&call->state_lock);
146d8fef
DH
828 break;
829 default:
fb46f6ee
DH
830 /* Request phase complete for this client call */
831 trace_rxrpc_rx_eproto(call, 0, tracepoint_string("late_send"));
146d8fef
DH
832 ret = -EPROTO;
833 break;
df423a4a
DH
834 }
835
540b1c48 836 mutex_unlock(&call->user_mutex);
0b58b8a1
DH
837 _leave(" = %d", ret);
838 return ret;
df423a4a
DH
839}
840EXPORT_SYMBOL(rxrpc_kernel_send_data);
0b58b8a1 841
df423a4a
DH
842/**
843 * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
844 * @sock: The socket the call is on
845 * @call: The call to be aborted
846 * @abort_code: The abort code to stick into the ABORT packet
5a42976d
DH
847 * @error: Local error value
848 * @why: 3-char string indicating why.
df423a4a 849 *
84a4c09c
DH
850 * Allow a kernel service to abort a call, if it's still in an abortable state
851 * and return true if the call was aborted, false if it was already complete.
df423a4a 852 */
84a4c09c 853bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
5a42976d 854 u32 abort_code, int error, const char *why)
df423a4a 855{
84a4c09c
DH
856 bool aborted;
857
5a42976d 858 _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why);
0b58b8a1 859
540b1c48 860 mutex_lock(&call->user_mutex);
0b58b8a1 861
84a4c09c
DH
862 aborted = rxrpc_abort_call(why, call, 0, abort_code, error);
863 if (aborted)
26cb02aa 864 rxrpc_send_abort_packet(call);
df423a4a 865
540b1c48 866 mutex_unlock(&call->user_mutex);
84a4c09c 867 return aborted;
0b58b8a1 868}
df423a4a 869EXPORT_SYMBOL(rxrpc_kernel_abort_call);
e754eba6
DH
870
871/**
872 * rxrpc_kernel_set_tx_length - Set the total Tx length on a call
873 * @sock: The socket the call is on
874 * @call: The call to be informed
875 * @tx_total_len: The amount of data to be transmitted for this call
876 *
877 * Allow a kernel service to set the total transmit length on a call. This
878 * allows buffer-to-packet encrypt-and-copy to be performed.
879 *
880 * This function is primarily for use for setting the reply length since the
881 * request length can be set when beginning the call.
882 */
883void rxrpc_kernel_set_tx_length(struct socket *sock, struct rxrpc_call *call,
884 s64 tx_total_len)
885{
886 WARN_ON(call->tx_total_len != -1);
887 call->tx_total_len = tx_total_len;
888}
889EXPORT_SYMBOL(rxrpc_kernel_set_tx_length);