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