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