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