58e0a36f6aa9ead2ce1d04f0ba8bbe49feb949ca
[platform/kernel/linux-rpi.git] / net / rxrpc / sendmsg.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
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)
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>
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 /*
21  * Return true if there's sufficient Tx queue space.
22  */
23 static bool rxrpc_check_tx_space(struct rxrpc_call *call, rxrpc_seq_t *_tx_win)
24 {
25         if (_tx_win)
26                 *_tx_win = call->tx_bottom;
27         return call->tx_prepared - call->tx_bottom < 256;
28 }
29
30 /*
31  * Wait for space to appear in the Tx queue or a signal to occur.
32  */
33 static int rxrpc_wait_for_tx_window_intr(struct rxrpc_sock *rx,
34                                          struct rxrpc_call *call,
35                                          long *timeo)
36 {
37         for (;;) {
38                 set_current_state(TASK_INTERRUPTIBLE);
39                 if (rxrpc_check_tx_space(call, NULL))
40                         return 0;
41
42                 if (call->state >= RXRPC_CALL_COMPLETE)
43                         return call->error;
44
45                 if (signal_pending(current))
46                         return sock_intr_errno(*timeo);
47
48                 trace_rxrpc_txqueue(call, rxrpc_txqueue_wait);
49                 *timeo = schedule_timeout(*timeo);
50         }
51 }
52
53 /*
54  * Wait for space to appear in the Tx queue uninterruptibly, but with
55  * a timeout of 2*RTT if no progress was made and a signal occurred.
56  */
57 static int rxrpc_wait_for_tx_window_waitall(struct rxrpc_sock *rx,
58                                             struct rxrpc_call *call)
59 {
60         rxrpc_seq_t tx_start, tx_win;
61         signed long rtt, timeout;
62
63         rtt = READ_ONCE(call->peer->srtt_us) >> 3;
64         rtt = usecs_to_jiffies(rtt) * 2;
65         if (rtt < 2)
66                 rtt = 2;
67
68         timeout = rtt;
69         tx_start = smp_load_acquire(&call->acks_hard_ack);
70
71         for (;;) {
72                 set_current_state(TASK_UNINTERRUPTIBLE);
73
74                 if (rxrpc_check_tx_space(call, &tx_win))
75                         return 0;
76
77                 if (call->state >= RXRPC_CALL_COMPLETE)
78                         return call->error;
79
80                 if (timeout == 0 &&
81                     tx_win == tx_start && signal_pending(current))
82                         return -EINTR;
83
84                 if (tx_win != tx_start) {
85                         timeout = rtt;
86                         tx_start = tx_win;
87                 }
88
89                 trace_rxrpc_txqueue(call, rxrpc_txqueue_wait);
90                 timeout = schedule_timeout(timeout);
91         }
92 }
93
94 /*
95  * Wait for space to appear in the Tx queue uninterruptibly.
96  */
97 static int rxrpc_wait_for_tx_window_nonintr(struct rxrpc_sock *rx,
98                                             struct rxrpc_call *call,
99                                             long *timeo)
100 {
101         for (;;) {
102                 set_current_state(TASK_UNINTERRUPTIBLE);
103                 if (rxrpc_check_tx_space(call, NULL))
104                         return 0;
105
106                 if (call->state >= RXRPC_CALL_COMPLETE)
107                         return call->error;
108
109                 trace_rxrpc_txqueue(call, rxrpc_txqueue_wait);
110                 *timeo = schedule_timeout(*timeo);
111         }
112 }
113
114 /*
115  * wait for space to appear in the transmit/ACK window
116  * - caller holds the socket locked
117  */
118 static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
119                                     struct rxrpc_call *call,
120                                     long *timeo,
121                                     bool waitall)
122 {
123         DECLARE_WAITQUEUE(myself, current);
124         int ret;
125
126         _enter(",{%u,%u,%u,%u}",
127                call->tx_bottom, call->acks_hard_ack, call->tx_top, call->tx_winsize);
128
129         add_wait_queue(&call->waitq, &myself);
130
131         switch (call->interruptibility) {
132         case RXRPC_INTERRUPTIBLE:
133                 if (waitall)
134                         ret = rxrpc_wait_for_tx_window_waitall(rx, call);
135                 else
136                         ret = rxrpc_wait_for_tx_window_intr(rx, call, timeo);
137                 break;
138         case RXRPC_PREINTERRUPTIBLE:
139         case RXRPC_UNINTERRUPTIBLE:
140         default:
141                 ret = rxrpc_wait_for_tx_window_nonintr(rx, call, timeo);
142                 break;
143         }
144
145         remove_wait_queue(&call->waitq, &myself);
146         set_current_state(TASK_RUNNING);
147         _leave(" = %d", ret);
148         return ret;
149 }
150
151 /*
152  * Notify the owner of the call that the transmit phase is ended and the last
153  * packet has been queued.
154  */
155 static void rxrpc_notify_end_tx(struct rxrpc_sock *rx, struct rxrpc_call *call,
156                                 rxrpc_notify_end_tx_t notify_end_tx)
157 {
158         if (notify_end_tx)
159                 notify_end_tx(&rx->sk, call, call->user_call_ID);
160 }
161
162 /*
163  * Queue a DATA packet for transmission, set the resend timeout and send
164  * the packet immediately.  Returns the error from rxrpc_send_data_packet()
165  * in case the caller wants to do something with it.
166  */
167 static void rxrpc_queue_packet(struct rxrpc_sock *rx, struct rxrpc_call *call,
168                                struct rxrpc_txbuf *txb,
169                                rxrpc_notify_end_tx_t notify_end_tx)
170 {
171         unsigned long now;
172         rxrpc_seq_t seq = txb->seq;
173         bool last = test_bit(RXRPC_TXBUF_LAST, &txb->flags), poke;
174
175         rxrpc_inc_stat(call->rxnet, stat_tx_data);
176
177         ASSERTCMP(txb->seq, ==, call->tx_prepared + 1);
178
179         /* We have to set the timestamp before queueing as the retransmit
180          * algorithm can see the packet as soon as we queue it.
181          */
182         txb->last_sent = ktime_get_real();
183
184         if (last)
185                 trace_rxrpc_txqueue(call, rxrpc_txqueue_queue_last);
186         else
187                 trace_rxrpc_txqueue(call, rxrpc_txqueue_queue);
188
189         /* Add the packet to the call's output buffer */
190         spin_lock(&call->tx_lock);
191         poke = list_empty(&call->tx_sendmsg);
192         list_add_tail(&txb->call_link, &call->tx_sendmsg);
193         call->tx_prepared = seq;
194         spin_unlock(&call->tx_lock);
195
196         if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
197                 _debug("________awaiting reply/ACK__________");
198                 write_lock_bh(&call->state_lock);
199                 switch (call->state) {
200                 case RXRPC_CALL_CLIENT_SEND_REQUEST:
201                         call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
202                         rxrpc_notify_end_tx(rx, call, notify_end_tx);
203                         break;
204                 case RXRPC_CALL_SERVER_ACK_REQUEST:
205                         call->state = RXRPC_CALL_SERVER_SEND_REPLY;
206                         now = jiffies;
207                         WRITE_ONCE(call->delay_ack_at, now + MAX_JIFFY_OFFSET);
208                         if (call->ackr_reason == RXRPC_ACK_DELAY)
209                                 call->ackr_reason = 0;
210                         trace_rxrpc_timer(call, rxrpc_timer_init_for_send_reply, now);
211                         if (!last)
212                                 break;
213                         fallthrough;
214                 case RXRPC_CALL_SERVER_SEND_REPLY:
215                         call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
216                         rxrpc_notify_end_tx(rx, call, notify_end_tx);
217                         break;
218                 default:
219                         break;
220                 }
221                 write_unlock_bh(&call->state_lock);
222         }
223
224         if (poke)
225                 rxrpc_poke_call(call, rxrpc_call_poke_start);
226 }
227
228 /*
229  * send data through a socket
230  * - must be called in process context
231  * - The caller holds the call user access mutex, but not the socket lock.
232  */
233 static int rxrpc_send_data(struct rxrpc_sock *rx,
234                            struct rxrpc_call *call,
235                            struct msghdr *msg, size_t len,
236                            rxrpc_notify_end_tx_t notify_end_tx,
237                            bool *_dropped_lock)
238 {
239         struct rxrpc_txbuf *txb;
240         struct sock *sk = &rx->sk;
241         enum rxrpc_call_state state;
242         long timeo;
243         bool more = msg->msg_flags & MSG_MORE;
244         int ret, copied = 0;
245
246         timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
247
248         /* this should be in poll */
249         sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
250
251 reload:
252         ret = -EPIPE;
253         if (sk->sk_shutdown & SEND_SHUTDOWN)
254                 goto maybe_error;
255         state = READ_ONCE(call->state);
256         ret = -ESHUTDOWN;
257         if (state >= RXRPC_CALL_COMPLETE)
258                 goto maybe_error;
259         ret = -EPROTO;
260         if (state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
261             state != RXRPC_CALL_SERVER_ACK_REQUEST &&
262             state != RXRPC_CALL_SERVER_SEND_REPLY)
263                 goto maybe_error;
264
265         ret = -EMSGSIZE;
266         if (call->tx_total_len != -1) {
267                 if (len - copied > call->tx_total_len)
268                         goto maybe_error;
269                 if (!more && len - copied != call->tx_total_len)
270                         goto maybe_error;
271         }
272
273         txb = call->tx_pending;
274         call->tx_pending = NULL;
275         if (txb)
276                 rxrpc_see_txbuf(txb, rxrpc_txbuf_see_send_more);
277
278         do {
279                 rxrpc_transmit_ack_packets(call->peer->local);
280
281                 if (!txb) {
282                         size_t remain, bufsize, chunk, offset;
283
284                         _debug("alloc");
285
286                         if (!rxrpc_check_tx_space(call, NULL))
287                                 goto wait_for_space;
288
289                         /* Work out the maximum size of a packet.  Assume that
290                          * the security header is going to be in the padded
291                          * region (enc blocksize), but the trailer is not.
292                          */
293                         remain = more ? INT_MAX : msg_data_left(msg);
294                         ret = call->conn->security->how_much_data(call, remain,
295                                                                   &bufsize, &chunk, &offset);
296                         if (ret < 0)
297                                 goto maybe_error;
298
299                         _debug("SIZE: %zu/%zu @%zu", chunk, bufsize, offset);
300
301                         /* create a buffer that we can retain until it's ACK'd */
302                         ret = -ENOMEM;
303                         txb = rxrpc_alloc_txbuf(call, RXRPC_PACKET_TYPE_DATA,
304                                                 GFP_KERNEL);
305                         if (!txb)
306                                 goto maybe_error;
307
308                         txb->offset = offset;
309                         txb->space -= offset;
310                         txb->space = min_t(size_t, chunk, txb->space);
311                 }
312
313                 _debug("append");
314
315                 /* append next segment of data to the current buffer */
316                 if (msg_data_left(msg) > 0) {
317                         size_t copy = min_t(size_t, txb->space, msg_data_left(msg));
318
319                         _debug("add %zu", copy);
320                         if (!copy_from_iter_full(txb->data + txb->offset, copy,
321                                                  &msg->msg_iter))
322                                 goto efault;
323                         _debug("added");
324                         txb->space -= copy;
325                         txb->len += copy;
326                         txb->offset += copy;
327                         copied += copy;
328                         if (call->tx_total_len != -1)
329                                 call->tx_total_len -= copy;
330                 }
331
332                 /* check for the far side aborting the call or a network error
333                  * occurring */
334                 if (call->state == RXRPC_CALL_COMPLETE)
335                         goto call_terminated;
336
337                 /* add the packet to the send queue if it's now full */
338                 if (!txb->space ||
339                     (msg_data_left(msg) == 0 && !more)) {
340                         if (msg_data_left(msg) == 0 && !more) {
341                                 txb->wire.flags |= RXRPC_LAST_PACKET;
342                                 __set_bit(RXRPC_TXBUF_LAST, &txb->flags);
343                         }
344                         else if (call->tx_top - call->acks_hard_ack <
345                                  call->tx_winsize)
346                                 txb->wire.flags |= RXRPC_MORE_PACKETS;
347
348                         ret = call->security->secure_packet(call, txb);
349                         if (ret < 0)
350                                 goto out;
351
352                         rxrpc_queue_packet(rx, call, txb, notify_end_tx);
353                         txb = NULL;
354                 }
355         } while (msg_data_left(msg) > 0);
356
357 success:
358         ret = copied;
359         if (READ_ONCE(call->state) == RXRPC_CALL_COMPLETE) {
360                 read_lock_bh(&call->state_lock);
361                 if (call->error < 0)
362                         ret = call->error;
363                 read_unlock_bh(&call->state_lock);
364         }
365 out:
366         call->tx_pending = txb;
367         _leave(" = %d", ret);
368         return ret;
369
370 call_terminated:
371         rxrpc_put_txbuf(txb, rxrpc_txbuf_put_send_aborted);
372         _leave(" = %d", call->error);
373         return call->error;
374
375 maybe_error:
376         if (copied)
377                 goto success;
378         goto out;
379
380 efault:
381         ret = -EFAULT;
382         goto out;
383
384 wait_for_space:
385         ret = -EAGAIN;
386         if (msg->msg_flags & MSG_DONTWAIT)
387                 goto maybe_error;
388         mutex_unlock(&call->user_mutex);
389         *_dropped_lock = true;
390         ret = rxrpc_wait_for_tx_window(rx, call, &timeo,
391                                        msg->msg_flags & MSG_WAITALL);
392         if (ret < 0)
393                 goto maybe_error;
394         if (call->interruptibility == RXRPC_INTERRUPTIBLE) {
395                 if (mutex_lock_interruptible(&call->user_mutex) < 0) {
396                         ret = sock_intr_errno(timeo);
397                         goto maybe_error;
398                 }
399         } else {
400                 mutex_lock(&call->user_mutex);
401         }
402         *_dropped_lock = false;
403         goto reload;
404 }
405
406 /*
407  * extract control messages from the sendmsg() control buffer
408  */
409 static int rxrpc_sendmsg_cmsg(struct msghdr *msg, struct rxrpc_send_params *p)
410 {
411         struct cmsghdr *cmsg;
412         bool got_user_ID = false;
413         int len;
414
415         if (msg->msg_controllen == 0)
416                 return -EINVAL;
417
418         for_each_cmsghdr(cmsg, msg) {
419                 if (!CMSG_OK(msg, cmsg))
420                         return -EINVAL;
421
422                 len = cmsg->cmsg_len - sizeof(struct cmsghdr);
423                 _debug("CMSG %d, %d, %d",
424                        cmsg->cmsg_level, cmsg->cmsg_type, len);
425
426                 if (cmsg->cmsg_level != SOL_RXRPC)
427                         continue;
428
429                 switch (cmsg->cmsg_type) {
430                 case RXRPC_USER_CALL_ID:
431                         if (msg->msg_flags & MSG_CMSG_COMPAT) {
432                                 if (len != sizeof(u32))
433                                         return -EINVAL;
434                                 p->call.user_call_ID = *(u32 *)CMSG_DATA(cmsg);
435                         } else {
436                                 if (len != sizeof(unsigned long))
437                                         return -EINVAL;
438                                 p->call.user_call_ID = *(unsigned long *)
439                                         CMSG_DATA(cmsg);
440                         }
441                         got_user_ID = true;
442                         break;
443
444                 case RXRPC_ABORT:
445                         if (p->command != RXRPC_CMD_SEND_DATA)
446                                 return -EINVAL;
447                         p->command = RXRPC_CMD_SEND_ABORT;
448                         if (len != sizeof(p->abort_code))
449                                 return -EINVAL;
450                         p->abort_code = *(unsigned int *)CMSG_DATA(cmsg);
451                         if (p->abort_code == 0)
452                                 return -EINVAL;
453                         break;
454
455                 case RXRPC_CHARGE_ACCEPT:
456                         if (p->command != RXRPC_CMD_SEND_DATA)
457                                 return -EINVAL;
458                         p->command = RXRPC_CMD_CHARGE_ACCEPT;
459                         if (len != 0)
460                                 return -EINVAL;
461                         break;
462
463                 case RXRPC_EXCLUSIVE_CALL:
464                         p->exclusive = true;
465                         if (len != 0)
466                                 return -EINVAL;
467                         break;
468
469                 case RXRPC_UPGRADE_SERVICE:
470                         p->upgrade = true;
471                         if (len != 0)
472                                 return -EINVAL;
473                         break;
474
475                 case RXRPC_TX_LENGTH:
476                         if (p->call.tx_total_len != -1 || len != sizeof(__s64))
477                                 return -EINVAL;
478                         p->call.tx_total_len = *(__s64 *)CMSG_DATA(cmsg);
479                         if (p->call.tx_total_len < 0)
480                                 return -EINVAL;
481                         break;
482
483                 case RXRPC_SET_CALL_TIMEOUT:
484                         if (len & 3 || len < 4 || len > 12)
485                                 return -EINVAL;
486                         memcpy(&p->call.timeouts, CMSG_DATA(cmsg), len);
487                         p->call.nr_timeouts = len / 4;
488                         if (p->call.timeouts.hard > INT_MAX / HZ)
489                                 return -ERANGE;
490                         if (p->call.nr_timeouts >= 2 && p->call.timeouts.idle > 60 * 60 * 1000)
491                                 return -ERANGE;
492                         if (p->call.nr_timeouts >= 3 && p->call.timeouts.normal > 60 * 60 * 1000)
493                                 return -ERANGE;
494                         break;
495
496                 default:
497                         return -EINVAL;
498                 }
499         }
500
501         if (!got_user_ID)
502                 return -EINVAL;
503         if (p->call.tx_total_len != -1 && p->command != RXRPC_CMD_SEND_DATA)
504                 return -EINVAL;
505         _leave(" = 0");
506         return 0;
507 }
508
509 /*
510  * Create a new client call for sendmsg().
511  * - Called with the socket lock held, which it must release.
512  * - If it returns a call, the call's lock will need releasing by the caller.
513  */
514 static struct rxrpc_call *
515 rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
516                                   struct rxrpc_send_params *p)
517         __releases(&rx->sk.sk_lock.slock)
518         __acquires(&call->user_mutex)
519 {
520         struct rxrpc_conn_parameters cp;
521         struct rxrpc_call *call;
522         struct key *key;
523
524         DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
525
526         _enter("");
527
528         if (!msg->msg_name) {
529                 release_sock(&rx->sk);
530                 return ERR_PTR(-EDESTADDRREQ);
531         }
532
533         key = rx->key;
534         if (key && !rx->key->payload.data[0])
535                 key = NULL;
536
537         memset(&cp, 0, sizeof(cp));
538         cp.local                = rx->local;
539         cp.key                  = rx->key;
540         cp.security_level       = rx->min_sec_level;
541         cp.exclusive            = rx->exclusive | p->exclusive;
542         cp.upgrade              = p->upgrade;
543         cp.service_id           = srx->srx_service;
544         call = rxrpc_new_client_call(rx, &cp, srx, &p->call, GFP_KERNEL,
545                                      atomic_inc_return(&rxrpc_debug_id));
546         /* The socket is now unlocked */
547
548         rxrpc_put_peer(cp.peer, rxrpc_peer_put_discard_tmp);
549         _leave(" = %p\n", call);
550         return call;
551 }
552
553 /*
554  * send a message forming part of a client call through an RxRPC socket
555  * - caller holds the socket locked
556  * - the socket may be either a client socket or a server socket
557  */
558 int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
559         __releases(&rx->sk.sk_lock.slock)
560 {
561         enum rxrpc_call_state state;
562         struct rxrpc_call *call;
563         unsigned long now, j;
564         bool dropped_lock = false;
565         int ret;
566
567         struct rxrpc_send_params p = {
568                 .call.tx_total_len      = -1,
569                 .call.user_call_ID      = 0,
570                 .call.nr_timeouts       = 0,
571                 .call.interruptibility  = RXRPC_INTERRUPTIBLE,
572                 .abort_code             = 0,
573                 .command                = RXRPC_CMD_SEND_DATA,
574                 .exclusive              = false,
575                 .upgrade                = false,
576         };
577
578         _enter("");
579
580         ret = rxrpc_sendmsg_cmsg(msg, &p);
581         if (ret < 0)
582                 goto error_release_sock;
583
584         if (p.command == RXRPC_CMD_CHARGE_ACCEPT) {
585                 ret = -EINVAL;
586                 if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
587                         goto error_release_sock;
588                 ret = rxrpc_user_charge_accept(rx, p.call.user_call_ID);
589                 goto error_release_sock;
590         }
591
592         call = rxrpc_find_call_by_user_ID(rx, p.call.user_call_ID);
593         if (!call) {
594                 ret = -EBADSLT;
595                 if (p.command != RXRPC_CMD_SEND_DATA)
596                         goto error_release_sock;
597                 call = rxrpc_new_client_call_for_sendmsg(rx, msg, &p);
598                 /* The socket is now unlocked... */
599                 if (IS_ERR(call))
600                         return PTR_ERR(call);
601                 /* ... and we have the call lock. */
602                 ret = 0;
603                 if (READ_ONCE(call->state) == RXRPC_CALL_COMPLETE)
604                         goto out_put_unlock;
605         } else {
606                 switch (READ_ONCE(call->state)) {
607                 case RXRPC_CALL_UNINITIALISED:
608                 case RXRPC_CALL_CLIENT_AWAIT_CONN:
609                 case RXRPC_CALL_SERVER_PREALLOC:
610                 case RXRPC_CALL_SERVER_SECURING:
611                         rxrpc_put_call(call, rxrpc_call_put_sendmsg);
612                         ret = -EBUSY;
613                         goto error_release_sock;
614                 default:
615                         break;
616                 }
617
618                 ret = mutex_lock_interruptible(&call->user_mutex);
619                 release_sock(&rx->sk);
620                 if (ret < 0) {
621                         ret = -ERESTARTSYS;
622                         goto error_put;
623                 }
624
625                 if (p.call.tx_total_len != -1) {
626                         ret = -EINVAL;
627                         if (call->tx_total_len != -1 ||
628                             call->tx_pending ||
629                             call->tx_top != 0)
630                                 goto error_put;
631                         call->tx_total_len = p.call.tx_total_len;
632                 }
633         }
634
635         switch (p.call.nr_timeouts) {
636         case 3:
637                 j = msecs_to_jiffies(p.call.timeouts.normal);
638                 if (p.call.timeouts.normal > 0 && j == 0)
639                         j = 1;
640                 WRITE_ONCE(call->next_rx_timo, j);
641                 fallthrough;
642         case 2:
643                 j = msecs_to_jiffies(p.call.timeouts.idle);
644                 if (p.call.timeouts.idle > 0 && j == 0)
645                         j = 1;
646                 WRITE_ONCE(call->next_req_timo, j);
647                 fallthrough;
648         case 1:
649                 if (p.call.timeouts.hard > 0) {
650                         j = msecs_to_jiffies(p.call.timeouts.hard);
651                         now = jiffies;
652                         j += now;
653                         WRITE_ONCE(call->expect_term_by, j);
654                         rxrpc_reduce_call_timer(call, j, now,
655                                                 rxrpc_timer_set_for_hard);
656                 }
657                 break;
658         }
659
660         state = READ_ONCE(call->state);
661         _debug("CALL %d USR %lx ST %d on CONN %p",
662                call->debug_id, call->user_call_ID, state, call->conn);
663
664         if (state >= RXRPC_CALL_COMPLETE) {
665                 /* it's too late for this call */
666                 ret = -ESHUTDOWN;
667         } else if (p.command == RXRPC_CMD_SEND_ABORT) {
668                 ret = 0;
669                 if (rxrpc_abort_call("CMD", call, 0, p.abort_code, -ECONNABORTED))
670                         ret = rxrpc_send_abort_packet(call);
671         } else if (p.command != RXRPC_CMD_SEND_DATA) {
672                 ret = -EINVAL;
673         } else {
674                 ret = rxrpc_send_data(rx, call, msg, len, NULL, &dropped_lock);
675         }
676
677 out_put_unlock:
678         if (!dropped_lock)
679                 mutex_unlock(&call->user_mutex);
680 error_put:
681         rxrpc_put_call(call, rxrpc_call_put_sendmsg);
682         _leave(" = %d", ret);
683         return ret;
684
685 error_release_sock:
686         release_sock(&rx->sk);
687         return ret;
688 }
689
690 /**
691  * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
692  * @sock: The socket the call is on
693  * @call: The call to send data through
694  * @msg: The data to send
695  * @len: The amount of data to send
696  * @notify_end_tx: Notification that the last packet is queued.
697  *
698  * Allow a kernel service to send data on a call.  The call must be in an state
699  * appropriate to sending data.  No control data should be supplied in @msg,
700  * nor should an address be supplied.  MSG_MORE should be flagged if there's
701  * more data to come, otherwise this data will end the transmission phase.
702  */
703 int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
704                            struct msghdr *msg, size_t len,
705                            rxrpc_notify_end_tx_t notify_end_tx)
706 {
707         bool dropped_lock = false;
708         int ret;
709
710         _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
711
712         ASSERTCMP(msg->msg_name, ==, NULL);
713         ASSERTCMP(msg->msg_control, ==, NULL);
714
715         mutex_lock(&call->user_mutex);
716
717         _debug("CALL %d USR %lx ST %d on CONN %p",
718                call->debug_id, call->user_call_ID, call->state, call->conn);
719
720         switch (READ_ONCE(call->state)) {
721         case RXRPC_CALL_CLIENT_SEND_REQUEST:
722         case RXRPC_CALL_SERVER_ACK_REQUEST:
723         case RXRPC_CALL_SERVER_SEND_REPLY:
724                 ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len,
725                                       notify_end_tx, &dropped_lock);
726                 break;
727         case RXRPC_CALL_COMPLETE:
728                 read_lock_bh(&call->state_lock);
729                 ret = call->error;
730                 read_unlock_bh(&call->state_lock);
731                 break;
732         default:
733                 /* Request phase complete for this client call */
734                 trace_rxrpc_rx_eproto(call, 0, tracepoint_string("late_send"));
735                 ret = -EPROTO;
736                 break;
737         }
738
739         if (!dropped_lock)
740                 mutex_unlock(&call->user_mutex);
741         _leave(" = %d", ret);
742         return ret;
743 }
744 EXPORT_SYMBOL(rxrpc_kernel_send_data);
745
746 /**
747  * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
748  * @sock: The socket the call is on
749  * @call: The call to be aborted
750  * @abort_code: The abort code to stick into the ABORT packet
751  * @error: Local error value
752  * @why: 3-char string indicating why.
753  *
754  * Allow a kernel service to abort a call, if it's still in an abortable state
755  * and return true if the call was aborted, false if it was already complete.
756  */
757 bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
758                              u32 abort_code, int error, const char *why)
759 {
760         bool aborted;
761
762         _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why);
763
764         mutex_lock(&call->user_mutex);
765
766         aborted = rxrpc_abort_call(why, call, 0, abort_code, error);
767         if (aborted)
768                 rxrpc_send_abort_packet(call);
769
770         mutex_unlock(&call->user_mutex);
771         return aborted;
772 }
773 EXPORT_SYMBOL(rxrpc_kernel_abort_call);
774
775 /**
776  * rxrpc_kernel_set_tx_length - Set the total Tx length on a call
777  * @sock: The socket the call is on
778  * @call: The call to be informed
779  * @tx_total_len: The amount of data to be transmitted for this call
780  *
781  * Allow a kernel service to set the total transmit length on a call.  This
782  * allows buffer-to-packet encrypt-and-copy to be performed.
783  *
784  * This function is primarily for use for setting the reply length since the
785  * request length can be set when beginning the call.
786  */
787 void rxrpc_kernel_set_tx_length(struct socket *sock, struct rxrpc_call *call,
788                                 s64 tx_total_len)
789 {
790         WARN_ON(call->tx_total_len != -1);
791         call->tx_total_len = tx_total_len;
792 }
793 EXPORT_SYMBOL(rxrpc_kernel_set_tx_length);