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