blob: 1882fea71903525723eb64e916e76b3bec7fbec8 [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells0b58b8a2016-09-02 22:39:45 +01002/* AF_RXRPC sendmsg() implementation.
3 *
4 * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howells0b58b8a2016-09-02 22:39:45 +01006 */
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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010014#include <linux/sched/signal.h>
15
David Howells0b58b8a2016-09-02 22:39:45 +010016#include <net/sock.h>
17#include <net/af_rxrpc.h>
18#include "ar-internal.h"
19
David Howells0b58b8a2016-09-02 22:39:45 +010020/*
David Howells158fe662020-03-13 09:05:38 +000021 * 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/*
David Howellsbc5e3a52017-10-18 11:07:31 +010036 * 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);
David Howells158fe662020-03-13 09:05:38 +000044 if (rxrpc_check_tx_space(call, NULL))
David Howellsbc5e3a52017-10-18 11:07:31 +010045 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);
David Howellsbc5e3a52017-10-18 11:07:31 +010054 *timeo = schedule_timeout(*timeo);
David Howellsbc5e3a52017-10-18 11:07:31 +010055 }
56}
57
58/*
59 * Wait for space to appear in the Tx queue uninterruptibly, but with
60 * a timeout of 2*RTT if no progress was made and a signal occurred.
61 */
David Howellse138aa72020-03-13 09:22:09 +000062static int rxrpc_wait_for_tx_window_waitall(struct rxrpc_sock *rx,
David Howellsbc5e3a52017-10-18 11:07:31 +010063 struct rxrpc_call *call)
64{
65 rxrpc_seq_t tx_start, tx_win;
David Howellsc410bf012020-05-11 14:54:34 +010066 signed long rtt, timeout;
David Howellsbc5e3a52017-10-18 11:07:31 +010067
David Howellsc410bf012020-05-11 14:54:34 +010068 rtt = READ_ONCE(call->peer->srtt_us) >> 3;
69 rtt = usecs_to_jiffies(rtt) * 2;
70 if (rtt < 2)
71 rtt = 2;
David Howellsbc5e3a52017-10-18 11:07:31 +010072
David Howellsc410bf012020-05-11 14:54:34 +010073 timeout = rtt;
David Howellsbc5e3a52017-10-18 11:07:31 +010074 tx_start = READ_ONCE(call->tx_hard_ack);
75
76 for (;;) {
77 set_current_state(TASK_UNINTERRUPTIBLE);
78
79 tx_win = READ_ONCE(call->tx_hard_ack);
David Howells158fe662020-03-13 09:05:38 +000080 if (rxrpc_check_tx_space(call, &tx_win))
David Howellsbc5e3a52017-10-18 11:07:31 +010081 return 0;
82
83 if (call->state >= RXRPC_CALL_COMPLETE)
84 return call->error;
85
David Howellse138aa72020-03-13 09:22:09 +000086 if (timeout == 0 &&
David Howellsbc5e3a52017-10-18 11:07:31 +010087 tx_win == tx_start && signal_pending(current))
88 return -EINTR;
89
90 if (tx_win != tx_start) {
David Howellsc410bf012020-05-11 14:54:34 +010091 timeout = rtt;
David Howellsbc5e3a52017-10-18 11:07:31 +010092 tx_start = tx_win;
93 }
94
95 trace_rxrpc_transmit(call, rxrpc_transmit_wait);
96 timeout = schedule_timeout(timeout);
97 }
98}
99
100/*
David Howellse138aa72020-03-13 09:22:09 +0000101 * Wait for space to appear in the Tx queue uninterruptibly.
102 */
103static int rxrpc_wait_for_tx_window_nonintr(struct rxrpc_sock *rx,
104 struct rxrpc_call *call,
105 long *timeo)
106{
107 for (;;) {
108 set_current_state(TASK_UNINTERRUPTIBLE);
109 if (rxrpc_check_tx_space(call, NULL))
110 return 0;
111
112 if (call->state >= RXRPC_CALL_COMPLETE)
113 return call->error;
114
115 trace_rxrpc_transmit(call, rxrpc_transmit_wait);
116 *timeo = schedule_timeout(*timeo);
117 }
118}
119
120/*
David Howells0b58b8a2016-09-02 22:39:45 +0100121 * wait for space to appear in the transmit/ACK window
122 * - caller holds the socket locked
123 */
124static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
125 struct rxrpc_call *call,
David Howellsbc5e3a52017-10-18 11:07:31 +0100126 long *timeo,
127 bool waitall)
David Howells0b58b8a2016-09-02 22:39:45 +0100128{
129 DECLARE_WAITQUEUE(myself, current);
130 int ret;
131
David Howells248f2192016-09-08 11:10:12 +0100132 _enter(",{%u,%u,%u}",
133 call->tx_hard_ack, call->tx_top, call->tx_winsize);
David Howells0b58b8a2016-09-02 22:39:45 +0100134
135 add_wait_queue(&call->waitq, &myself);
136
David Howellse138aa72020-03-13 09:22:09 +0000137 switch (call->interruptibility) {
138 case RXRPC_INTERRUPTIBLE:
139 if (waitall)
140 ret = rxrpc_wait_for_tx_window_waitall(rx, call);
141 else
142 ret = rxrpc_wait_for_tx_window_intr(rx, call, timeo);
143 break;
144 case RXRPC_PREINTERRUPTIBLE:
145 case RXRPC_UNINTERRUPTIBLE:
146 default:
147 ret = rxrpc_wait_for_tx_window_nonintr(rx, call, timeo);
148 break;
149 }
David Howells0b58b8a2016-09-02 22:39:45 +0100150
151 remove_wait_queue(&call->waitq, &myself);
152 set_current_state(TASK_RUNNING);
153 _leave(" = %d", ret);
154 return ret;
155}
156
157/*
David Howells248f2192016-09-08 11:10:12 +0100158 * Schedule an instant Tx resend.
David Howells0b58b8a2016-09-02 22:39:45 +0100159 */
David Howells248f2192016-09-08 11:10:12 +0100160static inline void rxrpc_instant_resend(struct rxrpc_call *call, int ix)
David Howells0b58b8a2016-09-02 22:39:45 +0100161{
David Howells248f2192016-09-08 11:10:12 +0100162 spin_lock_bh(&call->lock);
163
164 if (call->state < RXRPC_CALL_COMPLETE) {
David Howells03877bf2018-03-30 21:04:43 +0100165 call->rxtx_annotations[ix] =
166 (call->rxtx_annotations[ix] & RXRPC_TX_ANNO_LAST) |
167 RXRPC_TX_ANNO_RETRANS;
David Howells248f2192016-09-08 11:10:12 +0100168 if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
David Howells0b58b8a2016-09-02 22:39:45 +0100169 rxrpc_queue_call(call);
170 }
David Howells248f2192016-09-08 11:10:12 +0100171
172 spin_unlock_bh(&call->lock);
David Howells0b58b8a2016-09-02 22:39:45 +0100173}
174
175/*
David Howellse8332512017-08-29 10:18:56 +0100176 * Notify the owner of the call that the transmit phase is ended and the last
177 * packet has been queued.
178 */
179static void rxrpc_notify_end_tx(struct rxrpc_sock *rx, struct rxrpc_call *call,
180 rxrpc_notify_end_tx_t notify_end_tx)
181{
182 if (notify_end_tx)
183 notify_end_tx(&rx->sk, call, call->user_call_ID);
184}
185
186/*
Marc Dionne8e8715a2019-04-12 16:33:54 +0100187 * Queue a DATA packet for transmission, set the resend timeout and send
188 * the packet immediately. Returns the error from rxrpc_send_data_packet()
189 * in case the caller wants to do something with it.
David Howells0b58b8a2016-09-02 22:39:45 +0100190 */
Marc Dionne8e8715a2019-04-12 16:33:54 +0100191static int rxrpc_queue_packet(struct rxrpc_sock *rx, struct rxrpc_call *call,
192 struct sk_buff *skb, bool last,
193 rxrpc_notify_end_tx_t notify_end_tx)
David Howells0b58b8a2016-09-02 22:39:45 +0100194{
195 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howellsa158bdd2017-11-24 10:18:41 +0000196 unsigned long now;
David Howells248f2192016-09-08 11:10:12 +0100197 rxrpc_seq_t seq = sp->hdr.seq;
198 int ret, ix;
David Howells70790dbe2016-09-23 12:39:22 +0100199 u8 annotation = RXRPC_TX_ANNO_UNACK;
David Howells0b58b8a2016-09-02 22:39:45 +0100200
David Howells248f2192016-09-08 11:10:12 +0100201 _net("queue skb %p [%d]", skb, seq);
David Howells0b58b8a2016-09-02 22:39:45 +0100202
David Howells248f2192016-09-08 11:10:12 +0100203 ASSERTCMP(seq, ==, call->tx_top + 1);
204
David Howellse122d842019-01-10 16:59:13 +0000205 if (last)
David Howells70790dbe2016-09-23 12:39:22 +0100206 annotation |= RXRPC_TX_ANNO_LAST;
207
David Howellsb24d2892016-09-23 13:17:33 +0100208 /* We have to set the timestamp before queueing as the retransmit
209 * algorithm can see the packet as soon as we queue it.
210 */
211 skb->tstamp = ktime_get_real();
212
David Howells248f2192016-09-08 11:10:12 +0100213 ix = seq & RXRPC_RXTX_BUFF_MASK;
David Howells987db9f2019-08-19 09:25:38 +0100214 rxrpc_get_skb(skb, rxrpc_skb_got);
David Howells70790dbe2016-09-23 12:39:22 +0100215 call->rxtx_annotations[ix] = annotation;
David Howells0b58b8a2016-09-02 22:39:45 +0100216 smp_wmb();
David Howells248f2192016-09-08 11:10:12 +0100217 call->rxtx_buffer[ix] = skb;
218 call->tx_top = seq;
David Howells70790dbe2016-09-23 12:39:22 +0100219 if (last)
David Howellsa124fe32016-09-17 10:49:13 +0100220 trace_rxrpc_transmit(call, rxrpc_transmit_queue_last);
David Howells70790dbe2016-09-23 12:39:22 +0100221 else
David Howellsa124fe32016-09-17 10:49:13 +0100222 trace_rxrpc_transmit(call, rxrpc_transmit_queue);
David Howells0b58b8a2016-09-02 22:39:45 +0100223
224 if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
225 _debug("________awaiting reply/ACK__________");
226 write_lock_bh(&call->state_lock);
227 switch (call->state) {
228 case RXRPC_CALL_CLIENT_SEND_REQUEST:
229 call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
David Howellse8332512017-08-29 10:18:56 +0100230 rxrpc_notify_end_tx(rx, call, notify_end_tx);
David Howells0b58b8a2016-09-02 22:39:45 +0100231 break;
232 case RXRPC_CALL_SERVER_ACK_REQUEST:
233 call->state = RXRPC_CALL_SERVER_SEND_REPLY;
David Howellsa158bdd2017-11-24 10:18:41 +0000234 now = jiffies;
235 WRITE_ONCE(call->ack_at, now + MAX_JIFFY_OFFSET);
David Howells9749fd22016-10-06 08:11:50 +0100236 if (call->ackr_reason == RXRPC_ACK_DELAY)
237 call->ackr_reason = 0;
David Howellsa158bdd2017-11-24 10:18:41 +0000238 trace_rxrpc_timer(call, rxrpc_timer_init_for_send_reply, now);
David Howells0b58b8a2016-09-02 22:39:45 +0100239 if (!last)
240 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500241 fallthrough;
David Howells0b58b8a2016-09-02 22:39:45 +0100242 case RXRPC_CALL_SERVER_SEND_REPLY:
243 call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
David Howellse8332512017-08-29 10:18:56 +0100244 rxrpc_notify_end_tx(rx, call, notify_end_tx);
David Howells0b58b8a2016-09-02 22:39:45 +0100245 break;
246 default:
247 break;
248 }
249 write_unlock_bh(&call->state_lock);
250 }
251
David Howells248f2192016-09-08 11:10:12 +0100252 if (seq == 1 && rxrpc_is_client_call(call))
253 rxrpc_expose_client_call(call);
254
David Howellsa1767072016-09-29 22:37:15 +0100255 ret = rxrpc_send_data_packet(call, skb, false);
David Howells0b58b8a2016-09-02 22:39:45 +0100256 if (ret < 0) {
David Howellsc54e43d2018-05-10 23:26:00 +0100257 switch (ret) {
258 case -ENETUNREACH:
259 case -EHOSTUNREACH:
260 case -ECONNREFUSED:
David Howells5ac0d622020-06-03 22:21:16 +0100261 rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
262 0, ret);
David Howellsc54e43d2018-05-10 23:26:00 +0100263 goto out;
264 }
David Howells0b58b8a2016-09-02 22:39:45 +0100265 _debug("need instant resend %d", ret);
David Howells248f2192016-09-08 11:10:12 +0100266 rxrpc_instant_resend(call, ix);
David Howellsdfc3da42016-09-23 12:39:23 +0100267 } else {
David Howellsc410bf012020-05-11 14:54:34 +0100268 unsigned long now = jiffies;
269 unsigned long resend_at = now + call->peer->rto_j;
David Howellsdfc3da42016-09-23 12:39:23 +0100270
David Howellsa158bdd2017-11-24 10:18:41 +0000271 WRITE_ONCE(call->resend_at, resend_at);
272 rxrpc_reduce_call_timer(call, resend_at, now,
273 rxrpc_timer_set_for_send);
David Howells0b58b8a2016-09-02 22:39:45 +0100274 }
275
David Howellsc54e43d2018-05-10 23:26:00 +0100276out:
David Howells987db9f2019-08-19 09:25:38 +0100277 rxrpc_free_skb(skb, rxrpc_skb_freed);
Marc Dionne8e8715a2019-04-12 16:33:54 +0100278 _leave(" = %d", ret);
279 return ret;
David Howells0b58b8a2016-09-02 22:39:45 +0100280}
281
282/*
David Howells0b58b8a2016-09-02 22:39:45 +0100283 * send data through a socket
284 * - must be called in process context
David Howells540b1c42017-02-27 15:43:06 +0000285 * - The caller holds the call user access mutex, but not the socket lock.
David Howells0b58b8a2016-09-02 22:39:45 +0100286 */
287static int rxrpc_send_data(struct rxrpc_sock *rx,
288 struct rxrpc_call *call,
David Howellse8332512017-08-29 10:18:56 +0100289 struct msghdr *msg, size_t len,
David Howells79e2ca7a2022-08-24 17:35:45 +0100290 rxrpc_notify_end_tx_t notify_end_tx,
291 bool *_dropped_lock)
David Howells0b58b8a2016-09-02 22:39:45 +0100292{
293 struct rxrpc_skb_priv *sp;
294 struct sk_buff *skb;
295 struct sock *sk = &rx->sk;
David Howells79e2ca7a2022-08-24 17:35:45 +0100296 enum rxrpc_call_state state;
David Howells0b58b8a2016-09-02 22:39:45 +0100297 long timeo;
David Howells79e2ca7a2022-08-24 17:35:45 +0100298 bool more = msg->msg_flags & MSG_MORE;
299 int ret, copied = 0;
David Howells0b58b8a2016-09-02 22:39:45 +0100300
301 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
302
303 /* this should be in poll */
304 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
305
David Howells79e2ca7a2022-08-24 17:35:45 +0100306reload:
307 ret = -EPIPE;
David Howells639f1812020-07-20 12:41:46 +0100308 if (sk->sk_shutdown & SEND_SHUTDOWN)
David Howells79e2ca7a2022-08-24 17:35:45 +0100309 goto maybe_error;
310 state = READ_ONCE(call->state);
311 ret = -ESHUTDOWN;
312 if (state >= RXRPC_CALL_COMPLETE)
313 goto maybe_error;
314 ret = -EPROTO;
315 if (state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
316 state != RXRPC_CALL_SERVER_ACK_REQUEST &&
317 state != RXRPC_CALL_SERVER_SEND_REPLY)
318 goto maybe_error;
David Howells0b58b8a2016-09-02 22:39:45 +0100319
David Howells79e2ca7a2022-08-24 17:35:45 +0100320 ret = -EMSGSIZE;
David Howellse754eba2017-06-07 12:40:03 +0100321 if (call->tx_total_len != -1) {
David Howells79e2ca7a2022-08-24 17:35:45 +0100322 if (len - copied > call->tx_total_len)
323 goto maybe_error;
324 if (!more && len - copied != call->tx_total_len)
325 goto maybe_error;
David Howellse754eba2017-06-07 12:40:03 +0100326 }
327
David Howells0b58b8a2016-09-02 22:39:45 +0100328 skb = call->tx_pending;
329 call->tx_pending = NULL;
David Howells987db9f2019-08-19 09:25:38 +0100330 rxrpc_see_skb(skb, rxrpc_skb_seen);
David Howells0b58b8a2016-09-02 22:39:45 +0100331
David Howells0b58b8a2016-09-02 22:39:45 +0100332 do {
David Howells7aa51da2016-09-22 00:29:31 +0100333 /* Check to see if there's a ping ACK to reply to. */
334 if (call->ackr_reason == RXRPC_ACK_PING_RESPONSE)
David Howellsbd1fdf82017-11-24 10:18:42 +0000335 rxrpc_send_ack_packet(call, false, NULL);
David Howells7aa51da2016-09-22 00:29:31 +0100336
David Howells0b58b8a2016-09-02 22:39:45 +0100337 if (!skb) {
338 size_t size, chunk, max, space;
339
340 _debug("alloc");
341
David Howells79e2ca7a2022-08-24 17:35:45 +0100342 if (!rxrpc_check_tx_space(call, NULL))
343 goto wait_for_space;
David Howells0b58b8a2016-09-02 22:39:45 +0100344
David Howells182f5052016-09-17 10:49:12 +0100345 max = RXRPC_JUMBO_DATALEN;
David Howells0b58b8a2016-09-02 22:39:45 +0100346 max -= call->conn->security_size;
347 max &= ~(call->conn->size_align - 1UL);
348
349 chunk = max;
350 if (chunk > msg_data_left(msg) && !more)
351 chunk = msg_data_left(msg);
352
353 space = chunk + call->conn->size_align;
354 space &= ~(call->conn->size_align - 1UL);
355
David Howells5a924b82016-09-22 00:29:31 +0100356 size = space + call->conn->security_size;
David Howells0b58b8a2016-09-02 22:39:45 +0100357
358 _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
359
360 /* create a buffer that we can retain until it's ACK'd */
361 skb = sock_alloc_send_skb(
362 sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
363 if (!skb)
364 goto maybe_error;
365
David Howellsb311e682019-08-19 09:25:37 +0100366 sp = rxrpc_skb(skb);
367 sp->rx_flags |= RXRPC_SKB_TX_BUFFER;
David Howells987db9f2019-08-19 09:25:38 +0100368 rxrpc_new_skb(skb, rxrpc_skb_new);
David Howells0b58b8a2016-09-02 22:39:45 +0100369
370 _debug("ALLOC SEND %p", skb);
371
372 ASSERTCMP(skb->mark, ==, 0);
373
David Howells5a924b82016-09-22 00:29:31 +0100374 _debug("HS: %u", call->conn->security_size);
375 skb_reserve(skb, call->conn->security_size);
376 skb->len += call->conn->security_size;
David Howells0b58b8a2016-09-02 22:39:45 +0100377
David Howells0b58b8a2016-09-02 22:39:45 +0100378 sp->remain = chunk;
379 if (sp->remain > skb_tailroom(skb))
380 sp->remain = skb_tailroom(skb);
381
382 _net("skb: hr %d, tr %d, hl %d, rm %d",
383 skb_headroom(skb),
384 skb_tailroom(skb),
385 skb_headlen(skb),
386 sp->remain);
387
388 skb->ip_summed = CHECKSUM_UNNECESSARY;
389 }
390
391 _debug("append");
392 sp = rxrpc_skb(skb);
393
394 /* append next segment of data to the current buffer */
395 if (msg_data_left(msg) > 0) {
396 int copy = skb_tailroom(skb);
397 ASSERTCMP(copy, >, 0);
398 if (copy > msg_data_left(msg))
399 copy = msg_data_left(msg);
400 if (copy > sp->remain)
401 copy = sp->remain;
402
403 _debug("add");
404 ret = skb_add_data(skb, &msg->msg_iter, copy);
405 _debug("added");
406 if (ret < 0)
407 goto efault;
408 sp->remain -= copy;
409 skb->mark += copy;
410 copied += copy;
David Howellse754eba2017-06-07 12:40:03 +0100411 if (call->tx_total_len != -1)
412 call->tx_total_len -= copy;
David Howells0b58b8a2016-09-02 22:39:45 +0100413 }
414
David Howellse122d842019-01-10 16:59:13 +0000415 /* check for the far side aborting the call or a network error
416 * occurring */
417 if (call->state == RXRPC_CALL_COMPLETE)
418 goto call_terminated;
419
David Howells0b58b8a2016-09-02 22:39:45 +0100420 /* add the packet to the send queue if it's now full */
421 if (sp->remain <= 0 ||
422 (msg_data_left(msg) == 0 && !more)) {
423 struct rxrpc_connection *conn = call->conn;
424 uint32_t seq;
425 size_t pad;
426
427 /* pad out if we're using security */
428 if (conn->security_ix) {
429 pad = conn->security_size + skb->mark;
430 pad = conn->size_align - pad;
431 pad &= conn->size_align - 1;
432 _debug("pad %zu", pad);
433 if (pad)
Johannes Bergb080db52017-06-16 14:29:19 +0200434 skb_put_zero(skb, pad);
David Howells0b58b8a2016-09-02 22:39:45 +0100435 }
436
David Howells248f2192016-09-08 11:10:12 +0100437 seq = call->tx_top + 1;
David Howells0b58b8a2016-09-02 22:39:45 +0100438
David Howells0b58b8a2016-09-02 22:39:45 +0100439 sp->hdr.seq = seq;
David Howells0b58b8a2016-09-02 22:39:45 +0100440 sp->hdr._rsvd = 0;
David Howells5a924b82016-09-22 00:29:31 +0100441 sp->hdr.flags = conn->out_clientflag;
David Howells0b58b8a2016-09-02 22:39:45 +0100442
David Howells0b58b8a2016-09-02 22:39:45 +0100443 if (msg_data_left(msg) == 0 && !more)
444 sp->hdr.flags |= RXRPC_LAST_PACKET;
David Howells248f2192016-09-08 11:10:12 +0100445 else if (call->tx_top - call->tx_hard_ack <
446 call->tx_winsize)
David Howells0b58b8a2016-09-02 22:39:45 +0100447 sp->hdr.flags |= RXRPC_MORE_PACKETS;
David Howells0b58b8a2016-09-02 22:39:45 +0100448
David Howells91fcfbe2019-10-07 10:58:29 +0100449 ret = call->security->secure_packet(
David Howells5a924b82016-09-22 00:29:31 +0100450 call, skb, skb->mark, skb->head);
David Howells0b58b8a2016-09-02 22:39:45 +0100451 if (ret < 0)
452 goto out;
453
Marc Dionne8e8715a2019-04-12 16:33:54 +0100454 ret = rxrpc_queue_packet(rx, call, skb,
455 !msg_data_left(msg) && !more,
456 notify_end_tx);
457 /* Should check for failure here */
David Howells0b58b8a2016-09-02 22:39:45 +0100458 skb = NULL;
459 }
460 } while (msg_data_left(msg) > 0);
461
462success:
463 ret = copied;
David Howells4a4e2e92022-05-21 08:45:41 +0100464 if (READ_ONCE(call->state) == RXRPC_CALL_COMPLETE) {
465 read_lock_bh(&call->state_lock);
466 if (call->error < 0)
467 ret = call->error;
468 read_unlock_bh(&call->state_lock);
469 }
David Howells0b58b8a2016-09-02 22:39:45 +0100470out:
471 call->tx_pending = skb;
472 _leave(" = %d", ret);
473 return ret;
474
David Howellse122d842019-01-10 16:59:13 +0000475call_terminated:
David Howells987db9f2019-08-19 09:25:38 +0100476 rxrpc_free_skb(skb, rxrpc_skb_freed);
David Howellse122d842019-01-10 16:59:13 +0000477 _leave(" = %d", call->error);
478 return call->error;
479
David Howells0b58b8a2016-09-02 22:39:45 +0100480maybe_error:
481 if (copied)
482 goto success;
483 goto out;
484
485efault:
486 ret = -EFAULT;
487 goto out;
David Howells79e2ca7a2022-08-24 17:35:45 +0100488
489wait_for_space:
490 ret = -EAGAIN;
491 if (msg->msg_flags & MSG_DONTWAIT)
492 goto maybe_error;
493 mutex_unlock(&call->user_mutex);
494 *_dropped_lock = true;
495 ret = rxrpc_wait_for_tx_window(rx, call, &timeo,
496 msg->msg_flags & MSG_WAITALL);
497 if (ret < 0)
498 goto maybe_error;
499 if (call->interruptibility == RXRPC_INTERRUPTIBLE) {
500 if (mutex_lock_interruptible(&call->user_mutex) < 0) {
501 ret = sock_intr_errno(timeo);
502 goto maybe_error;
503 }
504 } else {
505 mutex_lock(&call->user_mutex);
506 }
507 *_dropped_lock = false;
508 goto reload;
David Howells0b58b8a2016-09-02 22:39:45 +0100509}
David Howellsdf423a42016-09-02 22:39:45 +0100510
511/*
512 * extract control messages from the sendmsg() control buffer
513 */
David Howells3ab26a62017-06-07 14:41:52 +0100514static int rxrpc_sendmsg_cmsg(struct msghdr *msg, struct rxrpc_send_params *p)
David Howellsdf423a42016-09-02 22:39:45 +0100515{
516 struct cmsghdr *cmsg;
517 bool got_user_ID = false;
518 int len;
519
David Howellsdf423a42016-09-02 22:39:45 +0100520 if (msg->msg_controllen == 0)
521 return -EINVAL;
522
523 for_each_cmsghdr(cmsg, msg) {
524 if (!CMSG_OK(msg, cmsg))
525 return -EINVAL;
526
yuan linyu1ff8cebf42017-01-03 20:42:17 +0800527 len = cmsg->cmsg_len - sizeof(struct cmsghdr);
David Howellsdf423a42016-09-02 22:39:45 +0100528 _debug("CMSG %d, %d, %d",
529 cmsg->cmsg_level, cmsg->cmsg_type, len);
530
531 if (cmsg->cmsg_level != SOL_RXRPC)
532 continue;
533
534 switch (cmsg->cmsg_type) {
535 case RXRPC_USER_CALL_ID:
536 if (msg->msg_flags & MSG_CMSG_COMPAT) {
537 if (len != sizeof(u32))
538 return -EINVAL;
David Howells48124172017-11-24 10:18:41 +0000539 p->call.user_call_ID = *(u32 *)CMSG_DATA(cmsg);
David Howellsdf423a42016-09-02 22:39:45 +0100540 } else {
541 if (len != sizeof(unsigned long))
542 return -EINVAL;
David Howells48124172017-11-24 10:18:41 +0000543 p->call.user_call_ID = *(unsigned long *)
David Howellsdf423a42016-09-02 22:39:45 +0100544 CMSG_DATA(cmsg);
545 }
David Howellsdf423a42016-09-02 22:39:45 +0100546 got_user_ID = true;
547 break;
548
549 case RXRPC_ABORT:
David Howells3ab26a62017-06-07 14:41:52 +0100550 if (p->command != RXRPC_CMD_SEND_DATA)
David Howellsdf423a42016-09-02 22:39:45 +0100551 return -EINVAL;
David Howells3ab26a62017-06-07 14:41:52 +0100552 p->command = RXRPC_CMD_SEND_ABORT;
553 if (len != sizeof(p->abort_code))
David Howellsdf423a42016-09-02 22:39:45 +0100554 return -EINVAL;
David Howells3ab26a62017-06-07 14:41:52 +0100555 p->abort_code = *(unsigned int *)CMSG_DATA(cmsg);
556 if (p->abort_code == 0)
David Howellsdf423a42016-09-02 22:39:45 +0100557 return -EINVAL;
558 break;
559
David Howells2d914c12020-09-30 21:27:18 +0100560 case RXRPC_CHARGE_ACCEPT:
David Howells3ab26a62017-06-07 14:41:52 +0100561 if (p->command != RXRPC_CMD_SEND_DATA)
David Howellsdf423a42016-09-02 22:39:45 +0100562 return -EINVAL;
David Howells2d914c12020-09-30 21:27:18 +0100563 p->command = RXRPC_CMD_CHARGE_ACCEPT;
David Howellsdf423a42016-09-02 22:39:45 +0100564 if (len != 0)
565 return -EINVAL;
566 break;
567
568 case RXRPC_EXCLUSIVE_CALL:
David Howells3ab26a62017-06-07 14:41:52 +0100569 p->exclusive = true;
David Howellsdf423a42016-09-02 22:39:45 +0100570 if (len != 0)
571 return -EINVAL;
572 break;
David Howells4e255722017-06-05 14:30:49 +0100573
574 case RXRPC_UPGRADE_SERVICE:
David Howells3ab26a62017-06-07 14:41:52 +0100575 p->upgrade = true;
David Howells4e255722017-06-05 14:30:49 +0100576 if (len != 0)
577 return -EINVAL;
578 break;
579
David Howellse754eba2017-06-07 12:40:03 +0100580 case RXRPC_TX_LENGTH:
David Howells48124172017-11-24 10:18:41 +0000581 if (p->call.tx_total_len != -1 || len != sizeof(__s64))
David Howellse754eba2017-06-07 12:40:03 +0100582 return -EINVAL;
David Howells48124172017-11-24 10:18:41 +0000583 p->call.tx_total_len = *(__s64 *)CMSG_DATA(cmsg);
584 if (p->call.tx_total_len < 0)
David Howellse754eba2017-06-07 12:40:03 +0100585 return -EINVAL;
586 break;
587
David Howellsa158bdd2017-11-24 10:18:41 +0000588 case RXRPC_SET_CALL_TIMEOUT:
589 if (len & 3 || len < 4 || len > 12)
590 return -EINVAL;
591 memcpy(&p->call.timeouts, CMSG_DATA(cmsg), len);
592 p->call.nr_timeouts = len / 4;
593 if (p->call.timeouts.hard > INT_MAX / HZ)
594 return -ERANGE;
595 if (p->call.nr_timeouts >= 2 && p->call.timeouts.idle > 60 * 60 * 1000)
596 return -ERANGE;
597 if (p->call.nr_timeouts >= 3 && p->call.timeouts.normal > 60 * 60 * 1000)
598 return -ERANGE;
599 break;
600
David Howellsdf423a42016-09-02 22:39:45 +0100601 default:
602 return -EINVAL;
603 }
604 }
605
606 if (!got_user_ID)
607 return -EINVAL;
David Howells48124172017-11-24 10:18:41 +0000608 if (p->call.tx_total_len != -1 && p->command != RXRPC_CMD_SEND_DATA)
David Howellse754eba2017-06-07 12:40:03 +0100609 return -EINVAL;
David Howellsdf423a42016-09-02 22:39:45 +0100610 _leave(" = 0");
611 return 0;
612}
613
614/*
David Howellsdf423a42016-09-02 22:39:45 +0100615 * Create a new client call for sendmsg().
David Howells540b1c42017-02-27 15:43:06 +0000616 * - Called with the socket lock held, which it must release.
617 * - If it returns a call, the call's lock will need releasing by the caller.
David Howellsdf423a42016-09-02 22:39:45 +0100618 */
619static struct rxrpc_call *
620rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
David Howells3ab26a62017-06-07 14:41:52 +0100621 struct rxrpc_send_params *p)
David Howells540b1c42017-02-27 15:43:06 +0000622 __releases(&rx->sk.sk_lock.slock)
David Howells88f2a8252018-03-30 21:05:17 +0100623 __acquires(&call->user_mutex)
David Howellsdf423a42016-09-02 22:39:45 +0100624{
625 struct rxrpc_conn_parameters cp;
626 struct rxrpc_call *call;
627 struct key *key;
628
629 DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
630
631 _enter("");
632
David Howells540b1c42017-02-27 15:43:06 +0000633 if (!msg->msg_name) {
634 release_sock(&rx->sk);
David Howellsdf423a42016-09-02 22:39:45 +0100635 return ERR_PTR(-EDESTADDRREQ);
David Howells540b1c42017-02-27 15:43:06 +0000636 }
David Howellsdf423a42016-09-02 22:39:45 +0100637
638 key = rx->key;
639 if (key && !rx->key->payload.data[0])
640 key = NULL;
641
642 memset(&cp, 0, sizeof(cp));
643 cp.local = rx->local;
644 cp.key = rx->key;
645 cp.security_level = rx->min_sec_level;
David Howells3ab26a62017-06-07 14:41:52 +0100646 cp.exclusive = rx->exclusive | p->exclusive;
647 cp.upgrade = p->upgrade;
David Howellsdf423a42016-09-02 22:39:45 +0100648 cp.service_id = srx->srx_service;
David Howellsa25e21f2018-03-27 23:03:00 +0100649 call = rxrpc_new_client_call(rx, &cp, srx, &p->call, GFP_KERNEL,
650 atomic_inc_return(&rxrpc_debug_id));
David Howells540b1c42017-02-27 15:43:06 +0000651 /* The socket is now unlocked */
David Howellsdf423a42016-09-02 22:39:45 +0100652
David Howells17226f12018-03-30 21:05:44 +0100653 rxrpc_put_peer(cp.peer);
David Howellsdf423a42016-09-02 22:39:45 +0100654 _leave(" = %p\n", call);
655 return call;
656}
657
658/*
659 * send a message forming part of a client call through an RxRPC socket
660 * - caller holds the socket locked
661 * - the socket may be either a client socket or a server socket
662 */
663int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
David Howells540b1c42017-02-27 15:43:06 +0000664 __releases(&rx->sk.sk_lock.slock)
David Howells88f2a8252018-03-30 21:05:17 +0100665 __releases(&call->user_mutex)
David Howellsdf423a42016-09-02 22:39:45 +0100666{
David Howells146d8fe2017-03-04 00:01:41 +0000667 enum rxrpc_call_state state;
David Howellsdf423a42016-09-02 22:39:45 +0100668 struct rxrpc_call *call;
David Howellsa158bdd2017-11-24 10:18:41 +0000669 unsigned long now, j;
David Howells79e2ca7a2022-08-24 17:35:45 +0100670 bool dropped_lock = false;
David Howellsdf423a42016-09-02 22:39:45 +0100671 int ret;
672
David Howells3ab26a62017-06-07 14:41:52 +0100673 struct rxrpc_send_params p = {
David Howells48124172017-11-24 10:18:41 +0000674 .call.tx_total_len = -1,
675 .call.user_call_ID = 0,
David Howellsa158bdd2017-11-24 10:18:41 +0000676 .call.nr_timeouts = 0,
David Howellse138aa72020-03-13 09:22:09 +0000677 .call.interruptibility = RXRPC_INTERRUPTIBLE,
David Howells48124172017-11-24 10:18:41 +0000678 .abort_code = 0,
679 .command = RXRPC_CMD_SEND_DATA,
680 .exclusive = false,
681 .upgrade = false,
David Howells3ab26a62017-06-07 14:41:52 +0100682 };
683
David Howellsdf423a42016-09-02 22:39:45 +0100684 _enter("");
685
David Howells3ab26a62017-06-07 14:41:52 +0100686 ret = rxrpc_sendmsg_cmsg(msg, &p);
David Howellsdf423a42016-09-02 22:39:45 +0100687 if (ret < 0)
David Howells540b1c42017-02-27 15:43:06 +0000688 goto error_release_sock;
David Howellsdf423a42016-09-02 22:39:45 +0100689
David Howells2d914c12020-09-30 21:27:18 +0100690 if (p.command == RXRPC_CMD_CHARGE_ACCEPT) {
David Howells540b1c42017-02-27 15:43:06 +0000691 ret = -EINVAL;
David Howellsdf423a42016-09-02 22:39:45 +0100692 if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
David Howells540b1c42017-02-27 15:43:06 +0000693 goto error_release_sock;
David Howells2d914c12020-09-30 21:27:18 +0100694 ret = rxrpc_user_charge_accept(rx, p.call.user_call_ID);
695 goto error_release_sock;
David Howellsdf423a42016-09-02 22:39:45 +0100696 }
697
David Howells48124172017-11-24 10:18:41 +0000698 call = rxrpc_find_call_by_user_ID(rx, p.call.user_call_ID);
David Howellsdf423a42016-09-02 22:39:45 +0100699 if (!call) {
David Howells540b1c42017-02-27 15:43:06 +0000700 ret = -EBADSLT;
David Howells3ab26a62017-06-07 14:41:52 +0100701 if (p.command != RXRPC_CMD_SEND_DATA)
David Howells540b1c42017-02-27 15:43:06 +0000702 goto error_release_sock;
David Howells3ab26a62017-06-07 14:41:52 +0100703 call = rxrpc_new_client_call_for_sendmsg(rx, msg, &p);
David Howells540b1c42017-02-27 15:43:06 +0000704 /* The socket is now unlocked... */
David Howellsdf423a42016-09-02 22:39:45 +0100705 if (IS_ERR(call))
706 return PTR_ERR(call);
David Howells540b1c42017-02-27 15:43:06 +0000707 /* ... and we have the call lock. */
David Howells655500982020-07-29 00:03:56 +0100708 ret = 0;
709 if (READ_ONCE(call->state) == RXRPC_CALL_COMPLETE)
710 goto out_put_unlock;
David Howells540b1c42017-02-27 15:43:06 +0000711 } else {
David Howells146d8fe2017-03-04 00:01:41 +0000712 switch (READ_ONCE(call->state)) {
713 case RXRPC_CALL_UNINITIALISED:
714 case RXRPC_CALL_CLIENT_AWAIT_CONN:
715 case RXRPC_CALL_SERVER_PREALLOC:
716 case RXRPC_CALL_SERVER_SECURING:
David Howellsc48fc112019-10-07 10:58:28 +0100717 rxrpc_put_call(call, rxrpc_call_put);
David Howells146d8fe2017-03-04 00:01:41 +0000718 ret = -EBUSY;
David Howells37411cad2017-03-02 23:48:52 +0000719 goto error_release_sock;
David Howells146d8fe2017-03-04 00:01:41 +0000720 default:
721 break;
722 }
David Howells37411cad2017-03-02 23:48:52 +0000723
David Howells540b1c42017-02-27 15:43:06 +0000724 ret = mutex_lock_interruptible(&call->user_mutex);
725 release_sock(&rx->sk);
726 if (ret < 0) {
727 ret = -ERESTARTSYS;
728 goto error_put;
729 }
David Howellse754eba2017-06-07 12:40:03 +0100730
David Howells48124172017-11-24 10:18:41 +0000731 if (p.call.tx_total_len != -1) {
David Howellse754eba2017-06-07 12:40:03 +0100732 ret = -EINVAL;
733 if (call->tx_total_len != -1 ||
734 call->tx_pending ||
735 call->tx_top != 0)
David Howells3c973732022-12-15 16:19:47 +0000736 goto out_put_unlock;
David Howells48124172017-11-24 10:18:41 +0000737 call->tx_total_len = p.call.tx_total_len;
David Howellse754eba2017-06-07 12:40:03 +0100738 }
David Howellsdf423a42016-09-02 22:39:45 +0100739 }
740
David Howellsa158bdd2017-11-24 10:18:41 +0000741 switch (p.call.nr_timeouts) {
742 case 3:
743 j = msecs_to_jiffies(p.call.timeouts.normal);
744 if (p.call.timeouts.normal > 0 && j == 0)
745 j = 1;
746 WRITE_ONCE(call->next_rx_timo, j);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500747 fallthrough;
David Howellsa158bdd2017-11-24 10:18:41 +0000748 case 2:
749 j = msecs_to_jiffies(p.call.timeouts.idle);
750 if (p.call.timeouts.idle > 0 && j == 0)
751 j = 1;
752 WRITE_ONCE(call->next_req_timo, j);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500753 fallthrough;
David Howellsa158bdd2017-11-24 10:18:41 +0000754 case 1:
755 if (p.call.timeouts.hard > 0) {
David Howells15152b82023-04-28 21:27:54 +0100756 j = p.call.timeouts.hard * HZ;
David Howellsa158bdd2017-11-24 10:18:41 +0000757 now = jiffies;
758 j += now;
759 WRITE_ONCE(call->expect_term_by, j);
760 rxrpc_reduce_call_timer(call, j, now,
761 rxrpc_timer_set_for_hard);
762 }
763 break;
764 }
765
David Howells146d8fe2017-03-04 00:01:41 +0000766 state = READ_ONCE(call->state);
David Howellsdf423a42016-09-02 22:39:45 +0100767 _debug("CALL %d USR %lx ST %d on CONN %p",
David Howells146d8fe2017-03-04 00:01:41 +0000768 call->debug_id, call->user_call_ID, state, call->conn);
David Howellsdf423a42016-09-02 22:39:45 +0100769
David Howells146d8fe2017-03-04 00:01:41 +0000770 if (state >= RXRPC_CALL_COMPLETE) {
David Howellsdf423a42016-09-02 22:39:45 +0100771 /* it's too late for this call */
772 ret = -ESHUTDOWN;
David Howells3ab26a62017-06-07 14:41:52 +0100773 } else if (p.command == RXRPC_CMD_SEND_ABORT) {
David Howellsdf423a42016-09-02 22:39:45 +0100774 ret = 0;
David Howells3ab26a62017-06-07 14:41:52 +0100775 if (rxrpc_abort_call("CMD", call, 0, p.abort_code, -ECONNABORTED))
David Howells26cb02a2016-10-06 08:11:49 +0100776 ret = rxrpc_send_abort_packet(call);
David Howells3ab26a62017-06-07 14:41:52 +0100777 } else if (p.command != RXRPC_CMD_SEND_DATA) {
David Howellsdf423a42016-09-02 22:39:45 +0100778 ret = -EINVAL;
David Howellsdf423a42016-09-02 22:39:45 +0100779 } else {
David Howells79e2ca7a2022-08-24 17:35:45 +0100780 ret = rxrpc_send_data(rx, call, msg, len, NULL, &dropped_lock);
David Howellsdf423a42016-09-02 22:39:45 +0100781 }
782
David Howells03a6c822017-11-24 10:18:40 +0000783out_put_unlock:
David Howells79e2ca7a2022-08-24 17:35:45 +0100784 if (!dropped_lock)
785 mutex_unlock(&call->user_mutex);
David Howells540b1c42017-02-27 15:43:06 +0000786error_put:
David Howellsfff724292016-09-07 14:34:21 +0100787 rxrpc_put_call(call, rxrpc_call_put);
David Howellsdf423a42016-09-02 22:39:45 +0100788 _leave(" = %d", ret);
789 return ret;
David Howells540b1c42017-02-27 15:43:06 +0000790
791error_release_sock:
792 release_sock(&rx->sk);
793 return ret;
David Howellsdf423a42016-09-02 22:39:45 +0100794}
795
796/**
797 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
798 * @sock: The socket the call is on
799 * @call: The call to send data through
800 * @msg: The data to send
801 * @len: The amount of data to send
David Howellse8332512017-08-29 10:18:56 +0100802 * @notify_end_tx: Notification that the last packet is queued.
David Howellsdf423a42016-09-02 22:39:45 +0100803 *
804 * Allow a kernel service to send data on a call. The call must be in an state
805 * appropriate to sending data. No control data should be supplied in @msg,
806 * nor should an address be supplied. MSG_MORE should be flagged if there's
807 * more data to come, otherwise this data will end the transmission phase.
808 */
809int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
David Howellse8332512017-08-29 10:18:56 +0100810 struct msghdr *msg, size_t len,
811 rxrpc_notify_end_tx_t notify_end_tx)
David Howellsdf423a42016-09-02 22:39:45 +0100812{
David Howells79e2ca7a2022-08-24 17:35:45 +0100813 bool dropped_lock = false;
David Howellsdf423a42016-09-02 22:39:45 +0100814 int ret;
815
816 _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
817
818 ASSERTCMP(msg->msg_name, ==, NULL);
819 ASSERTCMP(msg->msg_control, ==, NULL);
820
David Howells540b1c42017-02-27 15:43:06 +0000821 mutex_lock(&call->user_mutex);
David Howellsdf423a42016-09-02 22:39:45 +0100822
823 _debug("CALL %d USR %lx ST %d on CONN %p",
824 call->debug_id, call->user_call_ID, call->state, call->conn);
825
David Howells146d8fe2017-03-04 00:01:41 +0000826 switch (READ_ONCE(call->state)) {
827 case RXRPC_CALL_CLIENT_SEND_REQUEST:
828 case RXRPC_CALL_SERVER_ACK_REQUEST:
829 case RXRPC_CALL_SERVER_SEND_REPLY:
David Howellse8332512017-08-29 10:18:56 +0100830 ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len,
David Howells79e2ca7a2022-08-24 17:35:45 +0100831 notify_end_tx, &dropped_lock);
David Howells146d8fe2017-03-04 00:01:41 +0000832 break;
833 case RXRPC_CALL_COMPLETE:
David Howells6fc166d2017-03-09 08:10:32 +0000834 read_lock_bh(&call->state_lock);
David Howellsbd2db2d2017-08-29 10:18:43 +0100835 ret = call->error;
David Howells6fc166d2017-03-09 08:10:32 +0000836 read_unlock_bh(&call->state_lock);
David Howells146d8fe2017-03-04 00:01:41 +0000837 break;
838 default:
David Howellsfb46f6e2017-04-06 10:12:00 +0100839 /* Request phase complete for this client call */
840 trace_rxrpc_rx_eproto(call, 0, tracepoint_string("late_send"));
David Howells146d8fe2017-03-04 00:01:41 +0000841 ret = -EPROTO;
842 break;
David Howellsdf423a42016-09-02 22:39:45 +0100843 }
844
David Howells79e2ca7a2022-08-24 17:35:45 +0100845 if (!dropped_lock)
846 mutex_unlock(&call->user_mutex);
David Howellsdf423a42016-09-02 22:39:45 +0100847 _leave(" = %d", ret);
848 return ret;
849}
850EXPORT_SYMBOL(rxrpc_kernel_send_data);
851
852/**
853 * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
854 * @sock: The socket the call is on
855 * @call: The call to be aborted
856 * @abort_code: The abort code to stick into the ABORT packet
David Howells5a429762016-09-06 22:19:51 +0100857 * @error: Local error value
858 * @why: 3-char string indicating why.
David Howellsdf423a42016-09-02 22:39:45 +0100859 *
David Howells84a4c092017-04-06 10:11:59 +0100860 * Allow a kernel service to abort a call, if it's still in an abortable state
861 * and return true if the call was aborted, false if it was already complete.
David Howellsdf423a42016-09-02 22:39:45 +0100862 */
David Howells84a4c092017-04-06 10:11:59 +0100863bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
David Howells5a429762016-09-06 22:19:51 +0100864 u32 abort_code, int error, const char *why)
David Howellsdf423a42016-09-02 22:39:45 +0100865{
David Howells84a4c092017-04-06 10:11:59 +0100866 bool aborted;
867
David Howells5a429762016-09-06 22:19:51 +0100868 _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why);
David Howellsdf423a42016-09-02 22:39:45 +0100869
David Howells540b1c42017-02-27 15:43:06 +0000870 mutex_lock(&call->user_mutex);
David Howellsdf423a42016-09-02 22:39:45 +0100871
David Howells84a4c092017-04-06 10:11:59 +0100872 aborted = rxrpc_abort_call(why, call, 0, abort_code, error);
873 if (aborted)
David Howells26cb02a2016-10-06 08:11:49 +0100874 rxrpc_send_abort_packet(call);
David Howellsdf423a42016-09-02 22:39:45 +0100875
David Howells540b1c42017-02-27 15:43:06 +0000876 mutex_unlock(&call->user_mutex);
David Howells84a4c092017-04-06 10:11:59 +0100877 return aborted;
David Howellsdf423a42016-09-02 22:39:45 +0100878}
David Howellsdf423a42016-09-02 22:39:45 +0100879EXPORT_SYMBOL(rxrpc_kernel_abort_call);
David Howellse754eba2017-06-07 12:40:03 +0100880
881/**
882 * rxrpc_kernel_set_tx_length - Set the total Tx length on a call
883 * @sock: The socket the call is on
884 * @call: The call to be informed
885 * @tx_total_len: The amount of data to be transmitted for this call
886 *
887 * Allow a kernel service to set the total transmit length on a call. This
888 * allows buffer-to-packet encrypt-and-copy to be performed.
889 *
890 * This function is primarily for use for setting the reply length since the
891 * request length can be set when beginning the call.
892 */
893void rxrpc_kernel_set_tx_length(struct socket *sock, struct rxrpc_call *call,
894 s64 tx_total_len)
895{
896 WARN_ON(call->tx_total_len != -1);
897 call->tx_total_len = tx_total_len;
898}
899EXPORT_SYMBOL(rxrpc_kernel_set_tx_length);