344fdce89823f668a8fc3e21b4b45be2dd14f918
[platform/kernel/linux-exynos.git] / net / rxrpc / sendmsg.c
1 /* AF_RXRPC sendmsg() implementation.
2  *
3  * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public Licence
8  * as published by the Free Software Foundation; either version
9  * 2 of the Licence, or (at your option) any later version.
10  */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include <linux/net.h>
15 #include <linux/gfp.h>
16 #include <linux/skbuff.h>
17 #include <linux/export.h>
18 #include <linux/sched/signal.h>
19
20 #include <net/sock.h>
21 #include <net/af_rxrpc.h>
22 #include "ar-internal.h"
23
24 enum rxrpc_command {
25         RXRPC_CMD_SEND_DATA,            /* send data message */
26         RXRPC_CMD_SEND_ABORT,           /* request abort generation */
27         RXRPC_CMD_ACCEPT,               /* [server] accept incoming call */
28         RXRPC_CMD_REJECT_BUSY,          /* [server] reject a call as busy */
29 };
30
31 struct rxrpc_send_params {
32         s64                     tx_total_len;   /* Total Tx data length (if send data) */
33         unsigned long           user_call_ID;   /* User's call ID */
34         u32                     abort_code;     /* Abort code to Tx (if abort) */
35         enum rxrpc_command      command : 8;    /* The command to implement */
36         bool                    exclusive;      /* Shared or exclusive call */
37         bool                    upgrade;        /* If the connection is upgradeable */
38 };
39
40 /*
41  * wait for space to appear in the transmit/ACK window
42  * - caller holds the socket locked
43  */
44 static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
45                                     struct rxrpc_call *call,
46                                     long *timeo)
47 {
48         DECLARE_WAITQUEUE(myself, current);
49         int ret;
50
51         _enter(",{%u,%u,%u}",
52                call->tx_hard_ack, call->tx_top, call->tx_winsize);
53
54         add_wait_queue(&call->waitq, &myself);
55
56         for (;;) {
57                 set_current_state(TASK_INTERRUPTIBLE);
58                 ret = 0;
59                 if (call->tx_top - call->tx_hard_ack <
60                     min_t(unsigned int, call->tx_winsize,
61                           call->cong_cwnd + call->cong_extra))
62                         break;
63                 if (call->state >= RXRPC_CALL_COMPLETE) {
64                         ret = call->error;
65                         break;
66                 }
67                 if (signal_pending(current)) {
68                         ret = sock_intr_errno(*timeo);
69                         break;
70                 }
71
72                 trace_rxrpc_transmit(call, rxrpc_transmit_wait);
73                 mutex_unlock(&call->user_mutex);
74                 *timeo = schedule_timeout(*timeo);
75                 if (mutex_lock_interruptible(&call->user_mutex) < 0) {
76                         ret = sock_intr_errno(*timeo);
77                         break;
78                 }
79         }
80
81         remove_wait_queue(&call->waitq, &myself);
82         set_current_state(TASK_RUNNING);
83         _leave(" = %d", ret);
84         return ret;
85 }
86
87 /*
88  * Schedule an instant Tx resend.
89  */
90 static inline void rxrpc_instant_resend(struct rxrpc_call *call, int ix)
91 {
92         spin_lock_bh(&call->lock);
93
94         if (call->state < RXRPC_CALL_COMPLETE) {
95                 call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS;
96                 if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
97                         rxrpc_queue_call(call);
98         }
99
100         spin_unlock_bh(&call->lock);
101 }
102
103 /*
104  * Notify the owner of the call that the transmit phase is ended and the last
105  * packet has been queued.
106  */
107 static void rxrpc_notify_end_tx(struct rxrpc_sock *rx, struct rxrpc_call *call,
108                                 rxrpc_notify_end_tx_t notify_end_tx)
109 {
110         if (notify_end_tx)
111                 notify_end_tx(&rx->sk, call, call->user_call_ID);
112 }
113
114 /*
115  * Queue a DATA packet for transmission, set the resend timeout and send the
116  * packet immediately
117  */
118 static void rxrpc_queue_packet(struct rxrpc_sock *rx, struct rxrpc_call *call,
119                                struct sk_buff *skb, bool last,
120                                rxrpc_notify_end_tx_t notify_end_tx)
121 {
122         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
123         rxrpc_seq_t seq = sp->hdr.seq;
124         int ret, ix;
125         u8 annotation = RXRPC_TX_ANNO_UNACK;
126
127         _net("queue skb %p [%d]", skb, seq);
128
129         ASSERTCMP(seq, ==, call->tx_top + 1);
130
131         if (last)
132                 annotation |= RXRPC_TX_ANNO_LAST;
133
134         /* We have to set the timestamp before queueing as the retransmit
135          * algorithm can see the packet as soon as we queue it.
136          */
137         skb->tstamp = ktime_get_real();
138
139         ix = seq & RXRPC_RXTX_BUFF_MASK;
140         rxrpc_get_skb(skb, rxrpc_skb_tx_got);
141         call->rxtx_annotations[ix] = annotation;
142         smp_wmb();
143         call->rxtx_buffer[ix] = skb;
144         call->tx_top = seq;
145         if (last)
146                 trace_rxrpc_transmit(call, rxrpc_transmit_queue_last);
147         else
148                 trace_rxrpc_transmit(call, rxrpc_transmit_queue);
149
150         if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
151                 _debug("________awaiting reply/ACK__________");
152                 write_lock_bh(&call->state_lock);
153                 switch (call->state) {
154                 case RXRPC_CALL_CLIENT_SEND_REQUEST:
155                         call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
156                         rxrpc_notify_end_tx(rx, call, notify_end_tx);
157                         break;
158                 case RXRPC_CALL_SERVER_ACK_REQUEST:
159                         call->state = RXRPC_CALL_SERVER_SEND_REPLY;
160                         call->ack_at = call->expire_at;
161                         if (call->ackr_reason == RXRPC_ACK_DELAY)
162                                 call->ackr_reason = 0;
163                         __rxrpc_set_timer(call, rxrpc_timer_init_for_send_reply,
164                                           ktime_get_real());
165                         if (!last)
166                                 break;
167                 case RXRPC_CALL_SERVER_SEND_REPLY:
168                         call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
169                         rxrpc_notify_end_tx(rx, call, notify_end_tx);
170                         break;
171                 default:
172                         break;
173                 }
174                 write_unlock_bh(&call->state_lock);
175         }
176
177         if (seq == 1 && rxrpc_is_client_call(call))
178                 rxrpc_expose_client_call(call);
179
180         ret = rxrpc_send_data_packet(call, skb, false);
181         if (ret < 0) {
182                 _debug("need instant resend %d", ret);
183                 rxrpc_instant_resend(call, ix);
184         } else {
185                 ktime_t now = ktime_get_real(), resend_at;
186
187                 resend_at = ktime_add_ms(now, rxrpc_resend_timeout);
188
189                 if (ktime_before(resend_at, call->resend_at)) {
190                         call->resend_at = resend_at;
191                         rxrpc_set_timer(call, rxrpc_timer_set_for_send, now);
192                 }
193         }
194
195         rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
196         _leave("");
197 }
198
199 /*
200  * send data through a socket
201  * - must be called in process context
202  * - The caller holds the call user access mutex, but not the socket lock.
203  */
204 static int rxrpc_send_data(struct rxrpc_sock *rx,
205                            struct rxrpc_call *call,
206                            struct msghdr *msg, size_t len,
207                            rxrpc_notify_end_tx_t notify_end_tx)
208 {
209         struct rxrpc_skb_priv *sp;
210         struct sk_buff *skb;
211         struct sock *sk = &rx->sk;
212         long timeo;
213         bool more;
214         int ret, copied;
215
216         timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
217
218         /* this should be in poll */
219         sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
220
221         if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
222                 return -EPIPE;
223
224         more = msg->msg_flags & MSG_MORE;
225
226         if (call->tx_total_len != -1) {
227                 if (len > call->tx_total_len)
228                         return -EMSGSIZE;
229                 if (!more && len != call->tx_total_len)
230                         return -EMSGSIZE;
231         }
232
233         skb = call->tx_pending;
234         call->tx_pending = NULL;
235         rxrpc_see_skb(skb, rxrpc_skb_tx_seen);
236
237         copied = 0;
238         do {
239                 /* Check to see if there's a ping ACK to reply to. */
240                 if (call->ackr_reason == RXRPC_ACK_PING_RESPONSE)
241                         rxrpc_send_ack_packet(call, false);
242
243                 if (!skb) {
244                         size_t size, chunk, max, space;
245
246                         _debug("alloc");
247
248                         if (call->tx_top - call->tx_hard_ack >=
249                             min_t(unsigned int, call->tx_winsize,
250                                   call->cong_cwnd + call->cong_extra)) {
251                                 ret = -EAGAIN;
252                                 if (msg->msg_flags & MSG_DONTWAIT)
253                                         goto maybe_error;
254                                 ret = rxrpc_wait_for_tx_window(rx, call,
255                                                                &timeo);
256                                 if (ret < 0)
257                                         goto maybe_error;
258                         }
259
260                         max = RXRPC_JUMBO_DATALEN;
261                         max -= call->conn->security_size;
262                         max &= ~(call->conn->size_align - 1UL);
263
264                         chunk = max;
265                         if (chunk > msg_data_left(msg) && !more)
266                                 chunk = msg_data_left(msg);
267
268                         space = chunk + call->conn->size_align;
269                         space &= ~(call->conn->size_align - 1UL);
270
271                         size = space + call->conn->security_size;
272
273                         _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
274
275                         /* create a buffer that we can retain until it's ACK'd */
276                         skb = sock_alloc_send_skb(
277                                 sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
278                         if (!skb)
279                                 goto maybe_error;
280
281                         rxrpc_new_skb(skb, rxrpc_skb_tx_new);
282
283                         _debug("ALLOC SEND %p", skb);
284
285                         ASSERTCMP(skb->mark, ==, 0);
286
287                         _debug("HS: %u", call->conn->security_size);
288                         skb_reserve(skb, call->conn->security_size);
289                         skb->len += call->conn->security_size;
290
291                         sp = rxrpc_skb(skb);
292                         sp->remain = chunk;
293                         if (sp->remain > skb_tailroom(skb))
294                                 sp->remain = skb_tailroom(skb);
295
296                         _net("skb: hr %d, tr %d, hl %d, rm %d",
297                                skb_headroom(skb),
298                                skb_tailroom(skb),
299                                skb_headlen(skb),
300                                sp->remain);
301
302                         skb->ip_summed = CHECKSUM_UNNECESSARY;
303                 }
304
305                 _debug("append");
306                 sp = rxrpc_skb(skb);
307
308                 /* append next segment of data to the current buffer */
309                 if (msg_data_left(msg) > 0) {
310                         int copy = skb_tailroom(skb);
311                         ASSERTCMP(copy, >, 0);
312                         if (copy > msg_data_left(msg))
313                                 copy = msg_data_left(msg);
314                         if (copy > sp->remain)
315                                 copy = sp->remain;
316
317                         _debug("add");
318                         ret = skb_add_data(skb, &msg->msg_iter, copy);
319                         _debug("added");
320                         if (ret < 0)
321                                 goto efault;
322                         sp->remain -= copy;
323                         skb->mark += copy;
324                         copied += copy;
325                         if (call->tx_total_len != -1)
326                                 call->tx_total_len -= copy;
327                 }
328
329                 /* check for the far side aborting the call or a network error
330                  * occurring */
331                 if (call->state == RXRPC_CALL_COMPLETE)
332                         goto call_terminated;
333
334                 /* add the packet to the send queue if it's now full */
335                 if (sp->remain <= 0 ||
336                     (msg_data_left(msg) == 0 && !more)) {
337                         struct rxrpc_connection *conn = call->conn;
338                         uint32_t seq;
339                         size_t pad;
340
341                         /* pad out if we're using security */
342                         if (conn->security_ix) {
343                                 pad = conn->security_size + skb->mark;
344                                 pad = conn->size_align - pad;
345                                 pad &= conn->size_align - 1;
346                                 _debug("pad %zu", pad);
347                                 if (pad)
348                                         skb_put_zero(skb, pad);
349                         }
350
351                         seq = call->tx_top + 1;
352
353                         sp->hdr.seq     = seq;
354                         sp->hdr._rsvd   = 0;
355                         sp->hdr.flags   = conn->out_clientflag;
356
357                         if (msg_data_left(msg) == 0 && !more)
358                                 sp->hdr.flags |= RXRPC_LAST_PACKET;
359                         else if (call->tx_top - call->tx_hard_ack <
360                                  call->tx_winsize)
361                                 sp->hdr.flags |= RXRPC_MORE_PACKETS;
362
363                         ret = conn->security->secure_packet(
364                                 call, skb, skb->mark, skb->head);
365                         if (ret < 0)
366                                 goto out;
367
368                         rxrpc_queue_packet(rx, call, skb,
369                                            !msg_data_left(msg) && !more,
370                                            notify_end_tx);
371                         skb = NULL;
372                 }
373         } while (msg_data_left(msg) > 0);
374
375 success:
376         ret = copied;
377 out:
378         call->tx_pending = skb;
379         _leave(" = %d", ret);
380         return ret;
381
382 call_terminated:
383         rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
384         _leave(" = %d", call->error);
385         return call->error;
386
387 maybe_error:
388         if (copied)
389                 goto success;
390         goto out;
391
392 efault:
393         ret = -EFAULT;
394         goto out;
395 }
396
397 /*
398  * extract control messages from the sendmsg() control buffer
399  */
400 static int rxrpc_sendmsg_cmsg(struct msghdr *msg, struct rxrpc_send_params *p)
401 {
402         struct cmsghdr *cmsg;
403         bool got_user_ID = false;
404         int len;
405
406         if (msg->msg_controllen == 0)
407                 return -EINVAL;
408
409         for_each_cmsghdr(cmsg, msg) {
410                 if (!CMSG_OK(msg, cmsg))
411                         return -EINVAL;
412
413                 len = cmsg->cmsg_len - sizeof(struct cmsghdr);
414                 _debug("CMSG %d, %d, %d",
415                        cmsg->cmsg_level, cmsg->cmsg_type, len);
416
417                 if (cmsg->cmsg_level != SOL_RXRPC)
418                         continue;
419
420                 switch (cmsg->cmsg_type) {
421                 case RXRPC_USER_CALL_ID:
422                         if (msg->msg_flags & MSG_CMSG_COMPAT) {
423                                 if (len != sizeof(u32))
424                                         return -EINVAL;
425                                 p->user_call_ID = *(u32 *)CMSG_DATA(cmsg);
426                         } else {
427                                 if (len != sizeof(unsigned long))
428                                         return -EINVAL;
429                                 p->user_call_ID = *(unsigned long *)
430                                         CMSG_DATA(cmsg);
431                         }
432                         got_user_ID = true;
433                         break;
434
435                 case RXRPC_ABORT:
436                         if (p->command != RXRPC_CMD_SEND_DATA)
437                                 return -EINVAL;
438                         p->command = RXRPC_CMD_SEND_ABORT;
439                         if (len != sizeof(p->abort_code))
440                                 return -EINVAL;
441                         p->abort_code = *(unsigned int *)CMSG_DATA(cmsg);
442                         if (p->abort_code == 0)
443                                 return -EINVAL;
444                         break;
445
446                 case RXRPC_ACCEPT:
447                         if (p->command != RXRPC_CMD_SEND_DATA)
448                                 return -EINVAL;
449                         p->command = RXRPC_CMD_ACCEPT;
450                         if (len != 0)
451                                 return -EINVAL;
452                         break;
453
454                 case RXRPC_EXCLUSIVE_CALL:
455                         p->exclusive = true;
456                         if (len != 0)
457                                 return -EINVAL;
458                         break;
459
460                 case RXRPC_UPGRADE_SERVICE:
461                         p->upgrade = true;
462                         if (len != 0)
463                                 return -EINVAL;
464                         break;
465
466                 case RXRPC_TX_LENGTH:
467                         if (p->tx_total_len != -1 || len != sizeof(__s64))
468                                 return -EINVAL;
469                         p->tx_total_len = *(__s64 *)CMSG_DATA(cmsg);
470                         if (p->tx_total_len < 0)
471                                 return -EINVAL;
472                         break;
473
474                 default:
475                         return -EINVAL;
476                 }
477         }
478
479         if (!got_user_ID)
480                 return -EINVAL;
481         if (p->tx_total_len != -1 && p->command != RXRPC_CMD_SEND_DATA)
482                 return -EINVAL;
483         _leave(" = 0");
484         return 0;
485 }
486
487 /*
488  * Create a new client call for sendmsg().
489  * - Called with the socket lock held, which it must release.
490  * - If it returns a call, the call's lock will need releasing by the caller.
491  */
492 static struct rxrpc_call *
493 rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
494                                   struct rxrpc_send_params *p)
495         __releases(&rx->sk.sk_lock.slock)
496 {
497         struct rxrpc_conn_parameters cp;
498         struct rxrpc_call *call;
499         struct key *key;
500
501         DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
502
503         _enter("");
504
505         if (!msg->msg_name) {
506                 release_sock(&rx->sk);
507                 return ERR_PTR(-EDESTADDRREQ);
508         }
509
510         key = rx->key;
511         if (key && !rx->key->payload.data[0])
512                 key = NULL;
513
514         memset(&cp, 0, sizeof(cp));
515         cp.local                = rx->local;
516         cp.key                  = rx->key;
517         cp.security_level       = rx->min_sec_level;
518         cp.exclusive            = rx->exclusive | p->exclusive;
519         cp.upgrade              = p->upgrade;
520         cp.service_id           = srx->srx_service;
521         call = rxrpc_new_client_call(rx, &cp, srx, p->user_call_ID,
522                                      p->tx_total_len, GFP_KERNEL);
523         /* The socket is now unlocked */
524
525         _leave(" = %p\n", call);
526         return call;
527 }
528
529 /*
530  * send a message forming part of a client call through an RxRPC socket
531  * - caller holds the socket locked
532  * - the socket may be either a client socket or a server socket
533  */
534 int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
535         __releases(&rx->sk.sk_lock.slock)
536 {
537         enum rxrpc_call_state state;
538         struct rxrpc_call *call;
539         int ret;
540
541         struct rxrpc_send_params p = {
542                 .tx_total_len   = -1,
543                 .user_call_ID   = 0,
544                 .abort_code     = 0,
545                 .command        = RXRPC_CMD_SEND_DATA,
546                 .exclusive      = false,
547                 .upgrade        = true,
548         };
549
550         _enter("");
551
552         ret = rxrpc_sendmsg_cmsg(msg, &p);
553         if (ret < 0)
554                 goto error_release_sock;
555
556         if (p.command == RXRPC_CMD_ACCEPT) {
557                 ret = -EINVAL;
558                 if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
559                         goto error_release_sock;
560                 call = rxrpc_accept_call(rx, p.user_call_ID, NULL);
561                 /* The socket is now unlocked. */
562                 if (IS_ERR(call))
563                         return PTR_ERR(call);
564                 rxrpc_put_call(call, rxrpc_call_put);
565                 return 0;
566         }
567
568         call = rxrpc_find_call_by_user_ID(rx, p.user_call_ID);
569         if (!call) {
570                 ret = -EBADSLT;
571                 if (p.command != RXRPC_CMD_SEND_DATA)
572                         goto error_release_sock;
573                 call = rxrpc_new_client_call_for_sendmsg(rx, msg, &p);
574                 /* The socket is now unlocked... */
575                 if (IS_ERR(call))
576                         return PTR_ERR(call);
577                 /* ... and we have the call lock. */
578         } else {
579                 switch (READ_ONCE(call->state)) {
580                 case RXRPC_CALL_UNINITIALISED:
581                 case RXRPC_CALL_CLIENT_AWAIT_CONN:
582                 case RXRPC_CALL_SERVER_PREALLOC:
583                 case RXRPC_CALL_SERVER_SECURING:
584                 case RXRPC_CALL_SERVER_ACCEPTING:
585                         ret = -EBUSY;
586                         goto error_release_sock;
587                 default:
588                         break;
589                 }
590
591                 ret = mutex_lock_interruptible(&call->user_mutex);
592                 release_sock(&rx->sk);
593                 if (ret < 0) {
594                         ret = -ERESTARTSYS;
595                         goto error_put;
596                 }
597
598                 if (p.tx_total_len != -1) {
599                         ret = -EINVAL;
600                         if (call->tx_total_len != -1 ||
601                             call->tx_pending ||
602                             call->tx_top != 0)
603                                 goto error_put;
604                         call->tx_total_len = p.tx_total_len;
605                 }
606         }
607
608         state = READ_ONCE(call->state);
609         _debug("CALL %d USR %lx ST %d on CONN %p",
610                call->debug_id, call->user_call_ID, state, call->conn);
611
612         if (state >= RXRPC_CALL_COMPLETE) {
613                 /* it's too late for this call */
614                 ret = -ESHUTDOWN;
615         } else if (p.command == RXRPC_CMD_SEND_ABORT) {
616                 ret = 0;
617                 if (rxrpc_abort_call("CMD", call, 0, p.abort_code, -ECONNABORTED))
618                         ret = rxrpc_send_abort_packet(call);
619         } else if (p.command != RXRPC_CMD_SEND_DATA) {
620                 ret = -EINVAL;
621         } else if (rxrpc_is_client_call(call) &&
622                    state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
623                 /* request phase complete for this client call */
624                 ret = -EPROTO;
625         } else if (rxrpc_is_service_call(call) &&
626                    state != RXRPC_CALL_SERVER_ACK_REQUEST &&
627                    state != RXRPC_CALL_SERVER_SEND_REPLY) {
628                 /* Reply phase not begun or not complete for service call. */
629                 ret = -EPROTO;
630         } else {
631                 ret = rxrpc_send_data(rx, call, msg, len, NULL);
632         }
633
634         mutex_unlock(&call->user_mutex);
635 error_put:
636         rxrpc_put_call(call, rxrpc_call_put);
637         _leave(" = %d", ret);
638         return ret;
639
640 error_release_sock:
641         release_sock(&rx->sk);
642         return ret;
643 }
644
645 /**
646  * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
647  * @sock: The socket the call is on
648  * @call: The call to send data through
649  * @msg: The data to send
650  * @len: The amount of data to send
651  * @notify_end_tx: Notification that the last packet is queued.
652  *
653  * Allow a kernel service to send data on a call.  The call must be in an state
654  * appropriate to sending data.  No control data should be supplied in @msg,
655  * nor should an address be supplied.  MSG_MORE should be flagged if there's
656  * more data to come, otherwise this data will end the transmission phase.
657  */
658 int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
659                            struct msghdr *msg, size_t len,
660                            rxrpc_notify_end_tx_t notify_end_tx)
661 {
662         int ret;
663
664         _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
665
666         ASSERTCMP(msg->msg_name, ==, NULL);
667         ASSERTCMP(msg->msg_control, ==, NULL);
668
669         mutex_lock(&call->user_mutex);
670
671         _debug("CALL %d USR %lx ST %d on CONN %p",
672                call->debug_id, call->user_call_ID, call->state, call->conn);
673
674         switch (READ_ONCE(call->state)) {
675         case RXRPC_CALL_CLIENT_SEND_REQUEST:
676         case RXRPC_CALL_SERVER_ACK_REQUEST:
677         case RXRPC_CALL_SERVER_SEND_REPLY:
678                 ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len,
679                                       notify_end_tx);
680                 break;
681         case RXRPC_CALL_COMPLETE:
682                 read_lock_bh(&call->state_lock);
683                 ret = call->error;
684                 read_unlock_bh(&call->state_lock);
685                 break;
686         default:
687                 /* Request phase complete for this client call */
688                 trace_rxrpc_rx_eproto(call, 0, tracepoint_string("late_send"));
689                 ret = -EPROTO;
690                 break;
691         }
692
693         mutex_unlock(&call->user_mutex);
694         _leave(" = %d", ret);
695         return ret;
696 }
697 EXPORT_SYMBOL(rxrpc_kernel_send_data);
698
699 /**
700  * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
701  * @sock: The socket the call is on
702  * @call: The call to be aborted
703  * @abort_code: The abort code to stick into the ABORT packet
704  * @error: Local error value
705  * @why: 3-char string indicating why.
706  *
707  * Allow a kernel service to abort a call, if it's still in an abortable state
708  * and return true if the call was aborted, false if it was already complete.
709  */
710 bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
711                              u32 abort_code, int error, const char *why)
712 {
713         bool aborted;
714
715         _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why);
716
717         mutex_lock(&call->user_mutex);
718
719         aborted = rxrpc_abort_call(why, call, 0, abort_code, error);
720         if (aborted)
721                 rxrpc_send_abort_packet(call);
722
723         mutex_unlock(&call->user_mutex);
724         return aborted;
725 }
726 EXPORT_SYMBOL(rxrpc_kernel_abort_call);
727
728 /**
729  * rxrpc_kernel_set_tx_length - Set the total Tx length on a call
730  * @sock: The socket the call is on
731  * @call: The call to be informed
732  * @tx_total_len: The amount of data to be transmitted for this call
733  *
734  * Allow a kernel service to set the total transmit length on a call.  This
735  * allows buffer-to-packet encrypt-and-copy to be performed.
736  *
737  * This function is primarily for use for setting the reply length since the
738  * request length can be set when beginning the call.
739  */
740 void rxrpc_kernel_set_tx_length(struct socket *sock, struct rxrpc_call *call,
741                                 s64 tx_total_len)
742 {
743         WARN_ON(call->tx_total_len != -1);
744         call->tx_total_len = tx_total_len;
745 }
746 EXPORT_SYMBOL(rxrpc_kernel_set_tx_length);