blob: 27685d8cba1aefe1696712f03ad2ae513c9e3765 [file] [log] [blame]
David Howells0b58b8a2016-09-02 22:39:45 +01001/* 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>
David Howells0b58b8a2016-09-02 22:39:45 +010018#include <net/sock.h>
19#include <net/af_rxrpc.h>
20#include "ar-internal.h"
21
David Howells3dc20f02016-09-04 13:25:21 +010022enum rxrpc_command {
23 RXRPC_CMD_SEND_DATA, /* send data message */
24 RXRPC_CMD_SEND_ABORT, /* request abort generation */
25 RXRPC_CMD_ACCEPT, /* [server] accept incoming call */
26 RXRPC_CMD_REJECT_BUSY, /* [server] reject a call as busy */
27};
28
David Howells0b58b8a2016-09-02 22:39:45 +010029/*
30 * wait for space to appear in the transmit/ACK window
31 * - caller holds the socket locked
32 */
33static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
34 struct rxrpc_call *call,
35 long *timeo)
36{
37 DECLARE_WAITQUEUE(myself, current);
38 int ret;
39
David Howells248f2192016-09-08 11:10:12 +010040 _enter(",{%u,%u,%u}",
41 call->tx_hard_ack, call->tx_top, call->tx_winsize);
David Howells0b58b8a2016-09-02 22:39:45 +010042
43 add_wait_queue(&call->waitq, &myself);
44
45 for (;;) {
46 set_current_state(TASK_INTERRUPTIBLE);
47 ret = 0;
David Howells57494342016-09-24 18:05:27 +010048 if (call->tx_top - call->tx_hard_ack <
49 min_t(unsigned int, call->tx_winsize,
50 call->cong_cwnd + call->cong_extra))
David Howells0b58b8a2016-09-02 22:39:45 +010051 break;
David Howells248f2192016-09-08 11:10:12 +010052 if (call->state >= RXRPC_CALL_COMPLETE) {
53 ret = -call->error;
54 break;
55 }
David Howells0b58b8a2016-09-02 22:39:45 +010056 if (signal_pending(current)) {
57 ret = sock_intr_errno(*timeo);
58 break;
59 }
60
David Howellsa124fe32016-09-17 10:49:13 +010061 trace_rxrpc_transmit(call, rxrpc_transmit_wait);
David Howells540b1c42017-02-27 15:43:06 +000062 mutex_unlock(&call->user_mutex);
David Howells0b58b8a2016-09-02 22:39:45 +010063 *timeo = schedule_timeout(*timeo);
David Howells540b1c42017-02-27 15:43:06 +000064 if (mutex_lock_interruptible(&call->user_mutex) < 0) {
65 ret = sock_intr_errno(*timeo);
66 break;
67 }
David Howells0b58b8a2016-09-02 22:39:45 +010068 }
69
70 remove_wait_queue(&call->waitq, &myself);
71 set_current_state(TASK_RUNNING);
72 _leave(" = %d", ret);
73 return ret;
74}
75
76/*
David Howells248f2192016-09-08 11:10:12 +010077 * Schedule an instant Tx resend.
David Howells0b58b8a2016-09-02 22:39:45 +010078 */
David Howells248f2192016-09-08 11:10:12 +010079static inline void rxrpc_instant_resend(struct rxrpc_call *call, int ix)
David Howells0b58b8a2016-09-02 22:39:45 +010080{
David Howells248f2192016-09-08 11:10:12 +010081 spin_lock_bh(&call->lock);
82
83 if (call->state < RXRPC_CALL_COMPLETE) {
84 call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS;
85 if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
David Howells0b58b8a2016-09-02 22:39:45 +010086 rxrpc_queue_call(call);
87 }
David Howells248f2192016-09-08 11:10:12 +010088
89 spin_unlock_bh(&call->lock);
David Howells0b58b8a2016-09-02 22:39:45 +010090}
91
92/*
David Howells248f2192016-09-08 11:10:12 +010093 * Queue a DATA packet for transmission, set the resend timeout and send the
94 * packet immediately
David Howells0b58b8a2016-09-02 22:39:45 +010095 */
96static void rxrpc_queue_packet(struct rxrpc_call *call, struct sk_buff *skb,
97 bool last)
98{
99 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells248f2192016-09-08 11:10:12 +0100100 rxrpc_seq_t seq = sp->hdr.seq;
101 int ret, ix;
David Howells70790db2016-09-23 12:39:22 +0100102 u8 annotation = RXRPC_TX_ANNO_UNACK;
David Howells0b58b8a2016-09-02 22:39:45 +0100103
David Howells248f2192016-09-08 11:10:12 +0100104 _net("queue skb %p [%d]", skb, seq);
David Howells0b58b8a2016-09-02 22:39:45 +0100105
David Howells248f2192016-09-08 11:10:12 +0100106 ASSERTCMP(seq, ==, call->tx_top + 1);
107
David Howells70790db2016-09-23 12:39:22 +0100108 if (last)
109 annotation |= RXRPC_TX_ANNO_LAST;
110
David Howellsb24d2892016-09-23 13:17:33 +0100111 /* We have to set the timestamp before queueing as the retransmit
112 * algorithm can see the packet as soon as we queue it.
113 */
114 skb->tstamp = ktime_get_real();
115
David Howells248f2192016-09-08 11:10:12 +0100116 ix = seq & RXRPC_RXTX_BUFF_MASK;
David Howells71f3ca42016-09-17 10:49:14 +0100117 rxrpc_get_skb(skb, rxrpc_skb_tx_got);
David Howells70790db2016-09-23 12:39:22 +0100118 call->rxtx_annotations[ix] = annotation;
David Howells0b58b8a2016-09-02 22:39:45 +0100119 smp_wmb();
David Howells248f2192016-09-08 11:10:12 +0100120 call->rxtx_buffer[ix] = skb;
121 call->tx_top = seq;
David Howells70790db2016-09-23 12:39:22 +0100122 if (last)
David Howellsa124fe32016-09-17 10:49:13 +0100123 trace_rxrpc_transmit(call, rxrpc_transmit_queue_last);
David Howells70790db2016-09-23 12:39:22 +0100124 else
David Howellsa124fe32016-09-17 10:49:13 +0100125 trace_rxrpc_transmit(call, rxrpc_transmit_queue);
David Howells0b58b8a2016-09-02 22:39:45 +0100126
127 if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
128 _debug("________awaiting reply/ACK__________");
129 write_lock_bh(&call->state_lock);
130 switch (call->state) {
131 case RXRPC_CALL_CLIENT_SEND_REQUEST:
132 call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
133 break;
134 case RXRPC_CALL_SERVER_ACK_REQUEST:
135 call->state = RXRPC_CALL_SERVER_SEND_REPLY;
David Howells9749fd22016-10-06 08:11:50 +0100136 call->ack_at = call->expire_at;
137 if (call->ackr_reason == RXRPC_ACK_DELAY)
138 call->ackr_reason = 0;
139 __rxrpc_set_timer(call, rxrpc_timer_init_for_send_reply,
140 ktime_get_real());
David Howells0b58b8a2016-09-02 22:39:45 +0100141 if (!last)
142 break;
143 case RXRPC_CALL_SERVER_SEND_REPLY:
144 call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
145 break;
146 default:
147 break;
148 }
149 write_unlock_bh(&call->state_lock);
150 }
151
David Howells248f2192016-09-08 11:10:12 +0100152 if (seq == 1 && rxrpc_is_client_call(call))
153 rxrpc_expose_client_call(call);
154
David Howellsa1767072016-09-29 22:37:15 +0100155 ret = rxrpc_send_data_packet(call, skb, false);
David Howells0b58b8a2016-09-02 22:39:45 +0100156 if (ret < 0) {
157 _debug("need instant resend %d", ret);
David Howells248f2192016-09-08 11:10:12 +0100158 rxrpc_instant_resend(call, ix);
David Howellsdfc3da42016-09-23 12:39:23 +0100159 } else {
David Howellsdf0adc72016-09-26 22:12:49 +0100160 ktime_t now = ktime_get_real(), resend_at;
David Howellsdfc3da42016-09-23 12:39:23 +0100161
David Howellsdf0adc72016-09-26 22:12:49 +0100162 resend_at = ktime_add_ms(now, rxrpc_resend_timeout);
David Howellsdfc3da42016-09-23 12:39:23 +0100163
David Howellsdf0adc72016-09-26 22:12:49 +0100164 if (ktime_before(resend_at, call->resend_at)) {
David Howellsdfc3da42016-09-23 12:39:23 +0100165 call->resend_at = resend_at;
David Howellsdf0adc72016-09-26 22:12:49 +0100166 rxrpc_set_timer(call, rxrpc_timer_set_for_send, now);
David Howellsdfc3da42016-09-23 12:39:23 +0100167 }
David Howells0b58b8a2016-09-02 22:39:45 +0100168 }
169
David Howells71f3ca42016-09-17 10:49:14 +0100170 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
David Howells0b58b8a2016-09-02 22:39:45 +0100171 _leave("");
172}
173
174/*
David Howells0b58b8a2016-09-02 22:39:45 +0100175 * send data through a socket
176 * - must be called in process context
David Howells540b1c42017-02-27 15:43:06 +0000177 * - The caller holds the call user access mutex, but not the socket lock.
David Howells0b58b8a2016-09-02 22:39:45 +0100178 */
179static int rxrpc_send_data(struct rxrpc_sock *rx,
180 struct rxrpc_call *call,
181 struct msghdr *msg, size_t len)
182{
183 struct rxrpc_skb_priv *sp;
184 struct sk_buff *skb;
185 struct sock *sk = &rx->sk;
186 long timeo;
187 bool more;
188 int ret, copied;
189
190 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
191
192 /* this should be in poll */
193 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
194
195 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
196 return -EPIPE;
197
198 more = msg->msg_flags & MSG_MORE;
199
200 skb = call->tx_pending;
201 call->tx_pending = NULL;
David Howells71f3ca42016-09-17 10:49:14 +0100202 rxrpc_see_skb(skb, rxrpc_skb_tx_seen);
David Howells0b58b8a2016-09-02 22:39:45 +0100203
204 copied = 0;
205 do {
David Howells7aa51da2016-09-22 00:29:31 +0100206 /* Check to see if there's a ping ACK to reply to. */
207 if (call->ackr_reason == RXRPC_ACK_PING_RESPONSE)
David Howellsa5af7e12016-10-06 08:11:49 +0100208 rxrpc_send_ack_packet(call, false);
David Howells7aa51da2016-09-22 00:29:31 +0100209
David Howells0b58b8a2016-09-02 22:39:45 +0100210 if (!skb) {
211 size_t size, chunk, max, space;
212
213 _debug("alloc");
214
David Howells248f2192016-09-08 11:10:12 +0100215 if (call->tx_top - call->tx_hard_ack >=
David Howells57494342016-09-24 18:05:27 +0100216 min_t(unsigned int, call->tx_winsize,
217 call->cong_cwnd + call->cong_extra)) {
David Howells0b58b8a2016-09-02 22:39:45 +0100218 ret = -EAGAIN;
219 if (msg->msg_flags & MSG_DONTWAIT)
220 goto maybe_error;
221 ret = rxrpc_wait_for_tx_window(rx, call,
222 &timeo);
223 if (ret < 0)
224 goto maybe_error;
225 }
226
David Howells182f5052016-09-17 10:49:12 +0100227 max = RXRPC_JUMBO_DATALEN;
David Howells0b58b8a2016-09-02 22:39:45 +0100228 max -= call->conn->security_size;
229 max &= ~(call->conn->size_align - 1UL);
230
231 chunk = max;
232 if (chunk > msg_data_left(msg) && !more)
233 chunk = msg_data_left(msg);
234
235 space = chunk + call->conn->size_align;
236 space &= ~(call->conn->size_align - 1UL);
237
David Howells5a924b82016-09-22 00:29:31 +0100238 size = space + call->conn->security_size;
David Howells0b58b8a2016-09-02 22:39:45 +0100239
240 _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
241
242 /* create a buffer that we can retain until it's ACK'd */
243 skb = sock_alloc_send_skb(
244 sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
245 if (!skb)
246 goto maybe_error;
247
David Howells71f3ca42016-09-17 10:49:14 +0100248 rxrpc_new_skb(skb, rxrpc_skb_tx_new);
David Howells0b58b8a2016-09-02 22:39:45 +0100249
250 _debug("ALLOC SEND %p", skb);
251
252 ASSERTCMP(skb->mark, ==, 0);
253
David Howells5a924b82016-09-22 00:29:31 +0100254 _debug("HS: %u", call->conn->security_size);
255 skb_reserve(skb, call->conn->security_size);
256 skb->len += call->conn->security_size;
David Howells0b58b8a2016-09-02 22:39:45 +0100257
258 sp = rxrpc_skb(skb);
259 sp->remain = chunk;
260 if (sp->remain > skb_tailroom(skb))
261 sp->remain = skb_tailroom(skb);
262
263 _net("skb: hr %d, tr %d, hl %d, rm %d",
264 skb_headroom(skb),
265 skb_tailroom(skb),
266 skb_headlen(skb),
267 sp->remain);
268
269 skb->ip_summed = CHECKSUM_UNNECESSARY;
270 }
271
272 _debug("append");
273 sp = rxrpc_skb(skb);
274
275 /* append next segment of data to the current buffer */
276 if (msg_data_left(msg) > 0) {
277 int copy = skb_tailroom(skb);
278 ASSERTCMP(copy, >, 0);
279 if (copy > msg_data_left(msg))
280 copy = msg_data_left(msg);
281 if (copy > sp->remain)
282 copy = sp->remain;
283
284 _debug("add");
285 ret = skb_add_data(skb, &msg->msg_iter, copy);
286 _debug("added");
287 if (ret < 0)
288 goto efault;
289 sp->remain -= copy;
290 skb->mark += copy;
291 copied += copy;
292 }
293
294 /* check for the far side aborting the call or a network error
295 * occurring */
296 if (call->state == RXRPC_CALL_COMPLETE)
297 goto call_terminated;
298
299 /* add the packet to the send queue if it's now full */
300 if (sp->remain <= 0 ||
301 (msg_data_left(msg) == 0 && !more)) {
302 struct rxrpc_connection *conn = call->conn;
303 uint32_t seq;
304 size_t pad;
305
306 /* pad out if we're using security */
307 if (conn->security_ix) {
308 pad = conn->security_size + skb->mark;
309 pad = conn->size_align - pad;
310 pad &= conn->size_align - 1;
311 _debug("pad %zu", pad);
312 if (pad)
313 memset(skb_put(skb, pad), 0, pad);
314 }
315
David Howells248f2192016-09-08 11:10:12 +0100316 seq = call->tx_top + 1;
David Howells0b58b8a2016-09-02 22:39:45 +0100317
David Howells0b58b8a2016-09-02 22:39:45 +0100318 sp->hdr.seq = seq;
David Howells0b58b8a2016-09-02 22:39:45 +0100319 sp->hdr._rsvd = 0;
David Howells5a924b82016-09-22 00:29:31 +0100320 sp->hdr.flags = conn->out_clientflag;
David Howells0b58b8a2016-09-02 22:39:45 +0100321
David Howells0b58b8a2016-09-02 22:39:45 +0100322 if (msg_data_left(msg) == 0 && !more)
323 sp->hdr.flags |= RXRPC_LAST_PACKET;
David Howells248f2192016-09-08 11:10:12 +0100324 else if (call->tx_top - call->tx_hard_ack <
325 call->tx_winsize)
David Howells0b58b8a2016-09-02 22:39:45 +0100326 sp->hdr.flags |= RXRPC_MORE_PACKETS;
David Howells0b58b8a2016-09-02 22:39:45 +0100327
328 ret = conn->security->secure_packet(
David Howells5a924b82016-09-22 00:29:31 +0100329 call, skb, skb->mark, skb->head);
David Howells0b58b8a2016-09-02 22:39:45 +0100330 if (ret < 0)
331 goto out;
332
David Howells0b58b8a2016-09-02 22:39:45 +0100333 rxrpc_queue_packet(call, skb, !msg_data_left(msg) && !more);
334 skb = NULL;
335 }
336 } while (msg_data_left(msg) > 0);
337
338success:
339 ret = copied;
340out:
341 call->tx_pending = skb;
342 _leave(" = %d", ret);
343 return ret;
344
345call_terminated:
David Howells71f3ca42016-09-17 10:49:14 +0100346 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
David Howells0b58b8a2016-09-02 22:39:45 +0100347 _leave(" = %d", -call->error);
David Howells248f2192016-09-08 11:10:12 +0100348 return -call->error;
David Howells0b58b8a2016-09-02 22:39:45 +0100349
350maybe_error:
351 if (copied)
352 goto success;
353 goto out;
354
355efault:
356 ret = -EFAULT;
357 goto out;
358}
David Howellsdf423a42016-09-02 22:39:45 +0100359
360/*
361 * extract control messages from the sendmsg() control buffer
362 */
363static int rxrpc_sendmsg_cmsg(struct msghdr *msg,
364 unsigned long *user_call_ID,
365 enum rxrpc_command *command,
366 u32 *abort_code,
367 bool *_exclusive)
368{
369 struct cmsghdr *cmsg;
370 bool got_user_ID = false;
371 int len;
372
373 *command = RXRPC_CMD_SEND_DATA;
374
375 if (msg->msg_controllen == 0)
376 return -EINVAL;
377
378 for_each_cmsghdr(cmsg, msg) {
379 if (!CMSG_OK(msg, cmsg))
380 return -EINVAL;
381
yuan linyu1ff8ceb2017-01-03 20:42:17 +0800382 len = cmsg->cmsg_len - sizeof(struct cmsghdr);
David Howellsdf423a42016-09-02 22:39:45 +0100383 _debug("CMSG %d, %d, %d",
384 cmsg->cmsg_level, cmsg->cmsg_type, len);
385
386 if (cmsg->cmsg_level != SOL_RXRPC)
387 continue;
388
389 switch (cmsg->cmsg_type) {
390 case RXRPC_USER_CALL_ID:
391 if (msg->msg_flags & MSG_CMSG_COMPAT) {
392 if (len != sizeof(u32))
393 return -EINVAL;
394 *user_call_ID = *(u32 *) CMSG_DATA(cmsg);
395 } else {
396 if (len != sizeof(unsigned long))
397 return -EINVAL;
398 *user_call_ID = *(unsigned long *)
399 CMSG_DATA(cmsg);
400 }
401 _debug("User Call ID %lx", *user_call_ID);
402 got_user_ID = true;
403 break;
404
405 case RXRPC_ABORT:
406 if (*command != RXRPC_CMD_SEND_DATA)
407 return -EINVAL;
408 *command = RXRPC_CMD_SEND_ABORT;
409 if (len != sizeof(*abort_code))
410 return -EINVAL;
411 *abort_code = *(unsigned int *) CMSG_DATA(cmsg);
412 _debug("Abort %x", *abort_code);
413 if (*abort_code == 0)
414 return -EINVAL;
415 break;
416
417 case RXRPC_ACCEPT:
418 if (*command != RXRPC_CMD_SEND_DATA)
419 return -EINVAL;
420 *command = RXRPC_CMD_ACCEPT;
421 if (len != 0)
422 return -EINVAL;
423 break;
424
425 case RXRPC_EXCLUSIVE_CALL:
426 *_exclusive = true;
427 if (len != 0)
428 return -EINVAL;
429 break;
430 default:
431 return -EINVAL;
432 }
433 }
434
435 if (!got_user_ID)
436 return -EINVAL;
437 _leave(" = 0");
438 return 0;
439}
440
441/*
David Howellsdf423a42016-09-02 22:39:45 +0100442 * Create a new client call for sendmsg().
David Howells540b1c42017-02-27 15:43:06 +0000443 * - Called with the socket lock held, which it must release.
444 * - If it returns a call, the call's lock will need releasing by the caller.
David Howellsdf423a42016-09-02 22:39:45 +0100445 */
446static struct rxrpc_call *
447rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
448 unsigned long user_call_ID, bool exclusive)
David Howells540b1c42017-02-27 15:43:06 +0000449 __releases(&rx->sk.sk_lock.slock)
David Howellsdf423a42016-09-02 22:39:45 +0100450{
451 struct rxrpc_conn_parameters cp;
452 struct rxrpc_call *call;
453 struct key *key;
454
455 DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
456
457 _enter("");
458
David Howells540b1c42017-02-27 15:43:06 +0000459 if (!msg->msg_name) {
460 release_sock(&rx->sk);
David Howellsdf423a42016-09-02 22:39:45 +0100461 return ERR_PTR(-EDESTADDRREQ);
David Howells540b1c42017-02-27 15:43:06 +0000462 }
David Howellsdf423a42016-09-02 22:39:45 +0100463
464 key = rx->key;
465 if (key && !rx->key->payload.data[0])
466 key = NULL;
467
468 memset(&cp, 0, sizeof(cp));
469 cp.local = rx->local;
470 cp.key = rx->key;
471 cp.security_level = rx->min_sec_level;
472 cp.exclusive = rx->exclusive | exclusive;
473 cp.service_id = srx->srx_service;
474 call = rxrpc_new_client_call(rx, &cp, srx, user_call_ID, GFP_KERNEL);
David Howells540b1c42017-02-27 15:43:06 +0000475 /* The socket is now unlocked */
David Howellsdf423a42016-09-02 22:39:45 +0100476
477 _leave(" = %p\n", call);
478 return call;
479}
480
481/*
482 * send a message forming part of a client call through an RxRPC socket
483 * - caller holds the socket locked
484 * - the socket may be either a client socket or a server socket
485 */
486int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
David Howells540b1c42017-02-27 15:43:06 +0000487 __releases(&rx->sk.sk_lock.slock)
David Howellsdf423a42016-09-02 22:39:45 +0100488{
489 enum rxrpc_command cmd;
490 struct rxrpc_call *call;
491 unsigned long user_call_ID = 0;
492 bool exclusive = false;
493 u32 abort_code = 0;
494 int ret;
495
496 _enter("");
497
498 ret = rxrpc_sendmsg_cmsg(msg, &user_call_ID, &cmd, &abort_code,
499 &exclusive);
500 if (ret < 0)
David Howells540b1c42017-02-27 15:43:06 +0000501 goto error_release_sock;
David Howellsdf423a42016-09-02 22:39:45 +0100502
503 if (cmd == RXRPC_CMD_ACCEPT) {
David Howells540b1c42017-02-27 15:43:06 +0000504 ret = -EINVAL;
David Howellsdf423a42016-09-02 22:39:45 +0100505 if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
David Howells540b1c42017-02-27 15:43:06 +0000506 goto error_release_sock;
David Howellsdf423a42016-09-02 22:39:45 +0100507 call = rxrpc_accept_call(rx, user_call_ID, NULL);
David Howells540b1c42017-02-27 15:43:06 +0000508 /* The socket is now unlocked. */
David Howellsdf423a42016-09-02 22:39:45 +0100509 if (IS_ERR(call))
510 return PTR_ERR(call);
David Howellsfff72422016-09-07 14:34:21 +0100511 rxrpc_put_call(call, rxrpc_call_put);
David Howellsdf423a42016-09-02 22:39:45 +0100512 return 0;
513 }
514
515 call = rxrpc_find_call_by_user_ID(rx, user_call_ID);
516 if (!call) {
David Howells540b1c42017-02-27 15:43:06 +0000517 ret = -EBADSLT;
David Howellsdf423a42016-09-02 22:39:45 +0100518 if (cmd != RXRPC_CMD_SEND_DATA)
David Howells540b1c42017-02-27 15:43:06 +0000519 goto error_release_sock;
David Howellsdf423a42016-09-02 22:39:45 +0100520 call = rxrpc_new_client_call_for_sendmsg(rx, msg, user_call_ID,
521 exclusive);
David Howells540b1c42017-02-27 15:43:06 +0000522 /* The socket is now unlocked... */
David Howellsdf423a42016-09-02 22:39:45 +0100523 if (IS_ERR(call))
524 return PTR_ERR(call);
David Howells540b1c42017-02-27 15:43:06 +0000525 /* ... and we have the call lock. */
526 } else {
David Howells37411ca2017-03-02 23:48:52 +0000527 ret = -EBUSY;
528 if (call->state == RXRPC_CALL_UNINITIALISED ||
529 call->state == RXRPC_CALL_CLIENT_AWAIT_CONN ||
530 call->state == RXRPC_CALL_SERVER_PREALLOC ||
531 call->state == RXRPC_CALL_SERVER_SECURING ||
532 call->state == RXRPC_CALL_SERVER_ACCEPTING)
533 goto error_release_sock;
534
David Howells540b1c42017-02-27 15:43:06 +0000535 ret = mutex_lock_interruptible(&call->user_mutex);
536 release_sock(&rx->sk);
537 if (ret < 0) {
538 ret = -ERESTARTSYS;
539 goto error_put;
540 }
David Howellsdf423a42016-09-02 22:39:45 +0100541 }
542
David Howellsdf423a42016-09-02 22:39:45 +0100543 _debug("CALL %d USR %lx ST %d on CONN %p",
544 call->debug_id, call->user_call_ID, call->state, call->conn);
545
546 if (call->state >= RXRPC_CALL_COMPLETE) {
547 /* it's too late for this call */
548 ret = -ESHUTDOWN;
549 } else if (cmd == RXRPC_CMD_SEND_ABORT) {
David Howellsdf423a42016-09-02 22:39:45 +0100550 ret = 0;
David Howells248f2192016-09-08 11:10:12 +0100551 if (rxrpc_abort_call("CMD", call, 0, abort_code, ECONNABORTED))
David Howells26cb02a2016-10-06 08:11:49 +0100552 ret = rxrpc_send_abort_packet(call);
David Howellsdf423a42016-09-02 22:39:45 +0100553 } else if (cmd != RXRPC_CMD_SEND_DATA) {
554 ret = -EINVAL;
555 } else if (rxrpc_is_client_call(call) &&
556 call->state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
557 /* request phase complete for this client call */
558 ret = -EPROTO;
559 } else if (rxrpc_is_service_call(call) &&
560 call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
561 call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
562 /* Reply phase not begun or not complete for service call. */
563 ret = -EPROTO;
564 } else {
565 ret = rxrpc_send_data(rx, call, msg, len);
566 }
567
David Howells540b1c42017-02-27 15:43:06 +0000568 mutex_unlock(&call->user_mutex);
569error_put:
David Howellsfff72422016-09-07 14:34:21 +0100570 rxrpc_put_call(call, rxrpc_call_put);
David Howellsdf423a42016-09-02 22:39:45 +0100571 _leave(" = %d", ret);
572 return ret;
David Howells540b1c42017-02-27 15:43:06 +0000573
574error_release_sock:
575 release_sock(&rx->sk);
576 return ret;
David Howellsdf423a42016-09-02 22:39:45 +0100577}
578
579/**
580 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
581 * @sock: The socket the call is on
582 * @call: The call to send data through
583 * @msg: The data to send
584 * @len: The amount of data to send
585 *
586 * Allow a kernel service to send data on a call. The call must be in an state
587 * appropriate to sending data. No control data should be supplied in @msg,
588 * nor should an address be supplied. MSG_MORE should be flagged if there's
589 * more data to come, otherwise this data will end the transmission phase.
590 */
591int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
592 struct msghdr *msg, size_t len)
593{
594 int ret;
595
596 _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
597
598 ASSERTCMP(msg->msg_name, ==, NULL);
599 ASSERTCMP(msg->msg_control, ==, NULL);
600
David Howells540b1c42017-02-27 15:43:06 +0000601 mutex_lock(&call->user_mutex);
David Howellsdf423a42016-09-02 22:39:45 +0100602
603 _debug("CALL %d USR %lx ST %d on CONN %p",
604 call->debug_id, call->user_call_ID, call->state, call->conn);
605
606 if (call->state >= RXRPC_CALL_COMPLETE) {
607 ret = -ESHUTDOWN; /* it's too late for this call */
608 } else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
609 call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
610 call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
611 ret = -EPROTO; /* request phase complete for this client call */
612 } else {
613 ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len);
614 }
615
David Howells540b1c42017-02-27 15:43:06 +0000616 mutex_unlock(&call->user_mutex);
David Howellsdf423a42016-09-02 22:39:45 +0100617 _leave(" = %d", ret);
618 return ret;
619}
620EXPORT_SYMBOL(rxrpc_kernel_send_data);
621
622/**
623 * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
624 * @sock: The socket the call is on
625 * @call: The call to be aborted
626 * @abort_code: The abort code to stick into the ABORT packet
David Howells5a429762016-09-06 22:19:51 +0100627 * @error: Local error value
628 * @why: 3-char string indicating why.
David Howellsdf423a42016-09-02 22:39:45 +0100629 *
630 * Allow a kernel service to abort a call, if it's still in an abortable state.
631 */
632void rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
David Howells5a429762016-09-06 22:19:51 +0100633 u32 abort_code, int error, const char *why)
David Howellsdf423a42016-09-02 22:39:45 +0100634{
David Howells5a429762016-09-06 22:19:51 +0100635 _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why);
David Howellsdf423a42016-09-02 22:39:45 +0100636
David Howells540b1c42017-02-27 15:43:06 +0000637 mutex_lock(&call->user_mutex);
David Howellsdf423a42016-09-02 22:39:45 +0100638
David Howells248f2192016-09-08 11:10:12 +0100639 if (rxrpc_abort_call(why, call, 0, abort_code, error))
David Howells26cb02a2016-10-06 08:11:49 +0100640 rxrpc_send_abort_packet(call);
David Howellsdf423a42016-09-02 22:39:45 +0100641
David Howells540b1c42017-02-27 15:43:06 +0000642 mutex_unlock(&call->user_mutex);
David Howellsdf423a42016-09-02 22:39:45 +0100643 _leave("");
644}
645
646EXPORT_SYMBOL(rxrpc_kernel_abort_call);