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