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