blob: d939a5b1abc3d23ec5c062db40562752829d09db [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010018#include <linux/sched/signal.h>
19
David Howells0b58b8a2016-09-02 22:39:45 +010020#include <net/sock.h>
21#include <net/af_rxrpc.h>
22#include "ar-internal.h"
23
David Howells3dc20f02016-09-04 13:25:21 +010024enum 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
David Howells3ab26a62017-06-07 14:41:52 +010031struct rxrpc_send_params {
32 unsigned long user_call_ID; /* User's call ID */
33 u32 abort_code; /* Abort code to Tx (if abort) */
34 enum rxrpc_command command : 8; /* The command to implement */
35 bool exclusive; /* Shared or exclusive call */
36 bool upgrade; /* If the connection is upgradeable */
37};
38
David Howells0b58b8a2016-09-02 22:39:45 +010039/*
40 * wait for space to appear in the transmit/ACK window
41 * - caller holds the socket locked
42 */
43static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
44 struct rxrpc_call *call,
45 long *timeo)
46{
47 DECLARE_WAITQUEUE(myself, current);
48 int ret;
49
David Howells248f2192016-09-08 11:10:12 +010050 _enter(",{%u,%u,%u}",
51 call->tx_hard_ack, call->tx_top, call->tx_winsize);
David Howells0b58b8a2016-09-02 22:39:45 +010052
53 add_wait_queue(&call->waitq, &myself);
54
55 for (;;) {
56 set_current_state(TASK_INTERRUPTIBLE);
57 ret = 0;
David Howells57494342016-09-24 18:05:27 +010058 if (call->tx_top - call->tx_hard_ack <
59 min_t(unsigned int, call->tx_winsize,
60 call->cong_cwnd + call->cong_extra))
David Howells0b58b8a2016-09-02 22:39:45 +010061 break;
David Howells248f2192016-09-08 11:10:12 +010062 if (call->state >= RXRPC_CALL_COMPLETE) {
63 ret = -call->error;
64 break;
65 }
David Howells0b58b8a2016-09-02 22:39:45 +010066 if (signal_pending(current)) {
67 ret = sock_intr_errno(*timeo);
68 break;
69 }
70
David Howellsa124fe32016-09-17 10:49:13 +010071 trace_rxrpc_transmit(call, rxrpc_transmit_wait);
David Howells540b1c42017-02-27 15:43:06 +000072 mutex_unlock(&call->user_mutex);
David Howells0b58b8a2016-09-02 22:39:45 +010073 *timeo = schedule_timeout(*timeo);
David Howells540b1c42017-02-27 15:43:06 +000074 if (mutex_lock_interruptible(&call->user_mutex) < 0) {
75 ret = sock_intr_errno(*timeo);
76 break;
77 }
David Howells0b58b8a2016-09-02 22:39:45 +010078 }
79
80 remove_wait_queue(&call->waitq, &myself);
81 set_current_state(TASK_RUNNING);
82 _leave(" = %d", ret);
83 return ret;
84}
85
86/*
David Howells248f2192016-09-08 11:10:12 +010087 * Schedule an instant Tx resend.
David Howells0b58b8a2016-09-02 22:39:45 +010088 */
David Howells248f2192016-09-08 11:10:12 +010089static inline void rxrpc_instant_resend(struct rxrpc_call *call, int ix)
David Howells0b58b8a2016-09-02 22:39:45 +010090{
David Howells248f2192016-09-08 11:10:12 +010091 spin_lock_bh(&call->lock);
92
93 if (call->state < RXRPC_CALL_COMPLETE) {
94 call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS;
95 if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
David Howells0b58b8a2016-09-02 22:39:45 +010096 rxrpc_queue_call(call);
97 }
David Howells248f2192016-09-08 11:10:12 +010098
99 spin_unlock_bh(&call->lock);
David Howells0b58b8a2016-09-02 22:39:45 +0100100}
101
102/*
David Howells248f2192016-09-08 11:10:12 +0100103 * Queue a DATA packet for transmission, set the resend timeout and send the
104 * packet immediately
David Howells0b58b8a2016-09-02 22:39:45 +0100105 */
106static void rxrpc_queue_packet(struct rxrpc_call *call, struct sk_buff *skb,
107 bool last)
108{
109 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells248f2192016-09-08 11:10:12 +0100110 rxrpc_seq_t seq = sp->hdr.seq;
111 int ret, ix;
David Howells70790db2016-09-23 12:39:22 +0100112 u8 annotation = RXRPC_TX_ANNO_UNACK;
David Howells0b58b8a2016-09-02 22:39:45 +0100113
David Howells248f2192016-09-08 11:10:12 +0100114 _net("queue skb %p [%d]", skb, seq);
David Howells0b58b8a2016-09-02 22:39:45 +0100115
David Howells248f2192016-09-08 11:10:12 +0100116 ASSERTCMP(seq, ==, call->tx_top + 1);
117
David Howells70790db2016-09-23 12:39:22 +0100118 if (last)
119 annotation |= RXRPC_TX_ANNO_LAST;
120
David Howellsb24d2892016-09-23 13:17:33 +0100121 /* We have to set the timestamp before queueing as the retransmit
122 * algorithm can see the packet as soon as we queue it.
123 */
124 skb->tstamp = ktime_get_real();
125
David Howells248f2192016-09-08 11:10:12 +0100126 ix = seq & RXRPC_RXTX_BUFF_MASK;
David Howells71f3ca42016-09-17 10:49:14 +0100127 rxrpc_get_skb(skb, rxrpc_skb_tx_got);
David Howells70790db2016-09-23 12:39:22 +0100128 call->rxtx_annotations[ix] = annotation;
David Howells0b58b8a2016-09-02 22:39:45 +0100129 smp_wmb();
David Howells248f2192016-09-08 11:10:12 +0100130 call->rxtx_buffer[ix] = skb;
131 call->tx_top = seq;
David Howells70790db2016-09-23 12:39:22 +0100132 if (last)
David Howellsa124fe32016-09-17 10:49:13 +0100133 trace_rxrpc_transmit(call, rxrpc_transmit_queue_last);
David Howells70790db2016-09-23 12:39:22 +0100134 else
David Howellsa124fe32016-09-17 10:49:13 +0100135 trace_rxrpc_transmit(call, rxrpc_transmit_queue);
David Howells0b58b8a2016-09-02 22:39:45 +0100136
137 if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
138 _debug("________awaiting reply/ACK__________");
139 write_lock_bh(&call->state_lock);
140 switch (call->state) {
141 case RXRPC_CALL_CLIENT_SEND_REQUEST:
142 call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
143 break;
144 case RXRPC_CALL_SERVER_ACK_REQUEST:
145 call->state = RXRPC_CALL_SERVER_SEND_REPLY;
David Howells9749fd22016-10-06 08:11:50 +0100146 call->ack_at = call->expire_at;
147 if (call->ackr_reason == RXRPC_ACK_DELAY)
148 call->ackr_reason = 0;
149 __rxrpc_set_timer(call, rxrpc_timer_init_for_send_reply,
150 ktime_get_real());
David Howells0b58b8a2016-09-02 22:39:45 +0100151 if (!last)
152 break;
153 case RXRPC_CALL_SERVER_SEND_REPLY:
154 call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
155 break;
156 default:
157 break;
158 }
159 write_unlock_bh(&call->state_lock);
160 }
161
David Howells248f2192016-09-08 11:10:12 +0100162 if (seq == 1 && rxrpc_is_client_call(call))
163 rxrpc_expose_client_call(call);
164
David Howellsa1767072016-09-29 22:37:15 +0100165 ret = rxrpc_send_data_packet(call, skb, false);
David Howells0b58b8a2016-09-02 22:39:45 +0100166 if (ret < 0) {
167 _debug("need instant resend %d", ret);
David Howells248f2192016-09-08 11:10:12 +0100168 rxrpc_instant_resend(call, ix);
David Howellsdfc3da42016-09-23 12:39:23 +0100169 } else {
David Howellsdf0adc72016-09-26 22:12:49 +0100170 ktime_t now = ktime_get_real(), resend_at;
David Howellsdfc3da42016-09-23 12:39:23 +0100171
David Howellsdf0adc72016-09-26 22:12:49 +0100172 resend_at = ktime_add_ms(now, rxrpc_resend_timeout);
David Howellsdfc3da42016-09-23 12:39:23 +0100173
David Howellsdf0adc72016-09-26 22:12:49 +0100174 if (ktime_before(resend_at, call->resend_at)) {
David Howellsdfc3da42016-09-23 12:39:23 +0100175 call->resend_at = resend_at;
David Howellsdf0adc72016-09-26 22:12:49 +0100176 rxrpc_set_timer(call, rxrpc_timer_set_for_send, now);
David Howellsdfc3da42016-09-23 12:39:23 +0100177 }
David Howells0b58b8a2016-09-02 22:39:45 +0100178 }
179
David Howells71f3ca42016-09-17 10:49:14 +0100180 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
David Howells0b58b8a2016-09-02 22:39:45 +0100181 _leave("");
182}
183
184/*
David Howells0b58b8a2016-09-02 22:39:45 +0100185 * send data through a socket
186 * - must be called in process context
David Howells540b1c42017-02-27 15:43:06 +0000187 * - The caller holds the call user access mutex, but not the socket lock.
David Howells0b58b8a2016-09-02 22:39:45 +0100188 */
189static int rxrpc_send_data(struct rxrpc_sock *rx,
190 struct rxrpc_call *call,
191 struct msghdr *msg, size_t len)
192{
193 struct rxrpc_skb_priv *sp;
194 struct sk_buff *skb;
195 struct sock *sk = &rx->sk;
196 long timeo;
197 bool more;
198 int ret, copied;
199
200 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
201
202 /* this should be in poll */
203 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
204
205 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
206 return -EPIPE;
207
208 more = msg->msg_flags & MSG_MORE;
209
210 skb = call->tx_pending;
211 call->tx_pending = NULL;
David Howells71f3ca42016-09-17 10:49:14 +0100212 rxrpc_see_skb(skb, rxrpc_skb_tx_seen);
David Howells0b58b8a2016-09-02 22:39:45 +0100213
214 copied = 0;
215 do {
David Howells7aa51da2016-09-22 00:29:31 +0100216 /* Check to see if there's a ping ACK to reply to. */
217 if (call->ackr_reason == RXRPC_ACK_PING_RESPONSE)
David Howellsa5af7e12016-10-06 08:11:49 +0100218 rxrpc_send_ack_packet(call, false);
David Howells7aa51da2016-09-22 00:29:31 +0100219
David Howells0b58b8a2016-09-02 22:39:45 +0100220 if (!skb) {
221 size_t size, chunk, max, space;
222
223 _debug("alloc");
224
David Howells248f2192016-09-08 11:10:12 +0100225 if (call->tx_top - call->tx_hard_ack >=
David Howells57494342016-09-24 18:05:27 +0100226 min_t(unsigned int, call->tx_winsize,
227 call->cong_cwnd + call->cong_extra)) {
David Howells0b58b8a2016-09-02 22:39:45 +0100228 ret = -EAGAIN;
229 if (msg->msg_flags & MSG_DONTWAIT)
230 goto maybe_error;
231 ret = rxrpc_wait_for_tx_window(rx, call,
232 &timeo);
233 if (ret < 0)
234 goto maybe_error;
235 }
236
David Howells182f5052016-09-17 10:49:12 +0100237 max = RXRPC_JUMBO_DATALEN;
David Howells0b58b8a2016-09-02 22:39:45 +0100238 max -= call->conn->security_size;
239 max &= ~(call->conn->size_align - 1UL);
240
241 chunk = max;
242 if (chunk > msg_data_left(msg) && !more)
243 chunk = msg_data_left(msg);
244
245 space = chunk + call->conn->size_align;
246 space &= ~(call->conn->size_align - 1UL);
247
David Howells5a924b82016-09-22 00:29:31 +0100248 size = space + call->conn->security_size;
David Howells0b58b8a2016-09-02 22:39:45 +0100249
250 _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
251
252 /* create a buffer that we can retain until it's ACK'd */
253 skb = sock_alloc_send_skb(
254 sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
255 if (!skb)
256 goto maybe_error;
257
David Howells71f3ca42016-09-17 10:49:14 +0100258 rxrpc_new_skb(skb, rxrpc_skb_tx_new);
David Howells0b58b8a2016-09-02 22:39:45 +0100259
260 _debug("ALLOC SEND %p", skb);
261
262 ASSERTCMP(skb->mark, ==, 0);
263
David Howells5a924b82016-09-22 00:29:31 +0100264 _debug("HS: %u", call->conn->security_size);
265 skb_reserve(skb, call->conn->security_size);
266 skb->len += call->conn->security_size;
David Howells0b58b8a2016-09-02 22:39:45 +0100267
268 sp = rxrpc_skb(skb);
269 sp->remain = chunk;
270 if (sp->remain > skb_tailroom(skb))
271 sp->remain = skb_tailroom(skb);
272
273 _net("skb: hr %d, tr %d, hl %d, rm %d",
274 skb_headroom(skb),
275 skb_tailroom(skb),
276 skb_headlen(skb),
277 sp->remain);
278
279 skb->ip_summed = CHECKSUM_UNNECESSARY;
280 }
281
282 _debug("append");
283 sp = rxrpc_skb(skb);
284
285 /* append next segment of data to the current buffer */
286 if (msg_data_left(msg) > 0) {
287 int copy = skb_tailroom(skb);
288 ASSERTCMP(copy, >, 0);
289 if (copy > msg_data_left(msg))
290 copy = msg_data_left(msg);
291 if (copy > sp->remain)
292 copy = sp->remain;
293
294 _debug("add");
295 ret = skb_add_data(skb, &msg->msg_iter, copy);
296 _debug("added");
297 if (ret < 0)
298 goto efault;
299 sp->remain -= copy;
300 skb->mark += copy;
301 copied += copy;
302 }
303
304 /* check for the far side aborting the call or a network error
305 * occurring */
306 if (call->state == RXRPC_CALL_COMPLETE)
307 goto call_terminated;
308
309 /* add the packet to the send queue if it's now full */
310 if (sp->remain <= 0 ||
311 (msg_data_left(msg) == 0 && !more)) {
312 struct rxrpc_connection *conn = call->conn;
313 uint32_t seq;
314 size_t pad;
315
316 /* pad out if we're using security */
317 if (conn->security_ix) {
318 pad = conn->security_size + skb->mark;
319 pad = conn->size_align - pad;
320 pad &= conn->size_align - 1;
321 _debug("pad %zu", pad);
322 if (pad)
323 memset(skb_put(skb, pad), 0, pad);
324 }
325
David Howells248f2192016-09-08 11:10:12 +0100326 seq = call->tx_top + 1;
David Howells0b58b8a2016-09-02 22:39:45 +0100327
David Howells0b58b8a2016-09-02 22:39:45 +0100328 sp->hdr.seq = seq;
David Howells0b58b8a2016-09-02 22:39:45 +0100329 sp->hdr._rsvd = 0;
David Howells5a924b82016-09-22 00:29:31 +0100330 sp->hdr.flags = conn->out_clientflag;
David Howells0b58b8a2016-09-02 22:39:45 +0100331
David Howells0b58b8a2016-09-02 22:39:45 +0100332 if (msg_data_left(msg) == 0 && !more)
333 sp->hdr.flags |= RXRPC_LAST_PACKET;
David Howells248f2192016-09-08 11:10:12 +0100334 else if (call->tx_top - call->tx_hard_ack <
335 call->tx_winsize)
David Howells0b58b8a2016-09-02 22:39:45 +0100336 sp->hdr.flags |= RXRPC_MORE_PACKETS;
David Howells0b58b8a2016-09-02 22:39:45 +0100337
338 ret = conn->security->secure_packet(
David Howells5a924b82016-09-22 00:29:31 +0100339 call, skb, skb->mark, skb->head);
David Howells0b58b8a2016-09-02 22:39:45 +0100340 if (ret < 0)
341 goto out;
342
David Howells0b58b8a2016-09-02 22:39:45 +0100343 rxrpc_queue_packet(call, skb, !msg_data_left(msg) && !more);
344 skb = NULL;
345 }
346 } while (msg_data_left(msg) > 0);
347
348success:
349 ret = copied;
350out:
351 call->tx_pending = skb;
352 _leave(" = %d", ret);
353 return ret;
354
355call_terminated:
David Howells71f3ca42016-09-17 10:49:14 +0100356 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
David Howells0b58b8a2016-09-02 22:39:45 +0100357 _leave(" = %d", -call->error);
David Howells248f2192016-09-08 11:10:12 +0100358 return -call->error;
David Howells0b58b8a2016-09-02 22:39:45 +0100359
360maybe_error:
361 if (copied)
362 goto success;
363 goto out;
364
365efault:
366 ret = -EFAULT;
367 goto out;
368}
David Howellsdf423a42016-09-02 22:39:45 +0100369
370/*
371 * extract control messages from the sendmsg() control buffer
372 */
David Howells3ab26a62017-06-07 14:41:52 +0100373static int rxrpc_sendmsg_cmsg(struct msghdr *msg, struct rxrpc_send_params *p)
David Howellsdf423a42016-09-02 22:39:45 +0100374{
375 struct cmsghdr *cmsg;
376 bool got_user_ID = false;
377 int len;
378
David Howellsdf423a42016-09-02 22:39:45 +0100379 if (msg->msg_controllen == 0)
380 return -EINVAL;
381
382 for_each_cmsghdr(cmsg, msg) {
383 if (!CMSG_OK(msg, cmsg))
384 return -EINVAL;
385
yuan linyu1ff8ceb2017-01-03 20:42:17 +0800386 len = cmsg->cmsg_len - sizeof(struct cmsghdr);
David Howellsdf423a42016-09-02 22:39:45 +0100387 _debug("CMSG %d, %d, %d",
388 cmsg->cmsg_level, cmsg->cmsg_type, len);
389
390 if (cmsg->cmsg_level != SOL_RXRPC)
391 continue;
392
393 switch (cmsg->cmsg_type) {
394 case RXRPC_USER_CALL_ID:
395 if (msg->msg_flags & MSG_CMSG_COMPAT) {
396 if (len != sizeof(u32))
397 return -EINVAL;
David Howells3ab26a62017-06-07 14:41:52 +0100398 p->user_call_ID = *(u32 *)CMSG_DATA(cmsg);
David Howellsdf423a42016-09-02 22:39:45 +0100399 } else {
400 if (len != sizeof(unsigned long))
401 return -EINVAL;
David Howells3ab26a62017-06-07 14:41:52 +0100402 p->user_call_ID = *(unsigned long *)
David Howellsdf423a42016-09-02 22:39:45 +0100403 CMSG_DATA(cmsg);
404 }
David Howellsdf423a42016-09-02 22:39:45 +0100405 got_user_ID = true;
406 break;
407
408 case RXRPC_ABORT:
David Howells3ab26a62017-06-07 14:41:52 +0100409 if (p->command != RXRPC_CMD_SEND_DATA)
David Howellsdf423a42016-09-02 22:39:45 +0100410 return -EINVAL;
David Howells3ab26a62017-06-07 14:41:52 +0100411 p->command = RXRPC_CMD_SEND_ABORT;
412 if (len != sizeof(p->abort_code))
David Howellsdf423a42016-09-02 22:39:45 +0100413 return -EINVAL;
David Howells3ab26a62017-06-07 14:41:52 +0100414 p->abort_code = *(unsigned int *)CMSG_DATA(cmsg);
415 if (p->abort_code == 0)
David Howellsdf423a42016-09-02 22:39:45 +0100416 return -EINVAL;
417 break;
418
419 case RXRPC_ACCEPT:
David Howells3ab26a62017-06-07 14:41:52 +0100420 if (p->command != RXRPC_CMD_SEND_DATA)
David Howellsdf423a42016-09-02 22:39:45 +0100421 return -EINVAL;
David Howells3ab26a62017-06-07 14:41:52 +0100422 p->command = RXRPC_CMD_ACCEPT;
David Howellsdf423a42016-09-02 22:39:45 +0100423 if (len != 0)
424 return -EINVAL;
425 break;
426
427 case RXRPC_EXCLUSIVE_CALL:
David Howells3ab26a62017-06-07 14:41:52 +0100428 p->exclusive = true;
David Howellsdf423a42016-09-02 22:39:45 +0100429 if (len != 0)
430 return -EINVAL;
431 break;
David Howells4e255722017-06-05 14:30:49 +0100432
433 case RXRPC_UPGRADE_SERVICE:
David Howells3ab26a62017-06-07 14:41:52 +0100434 p->upgrade = true;
David Howells4e255722017-06-05 14:30:49 +0100435 if (len != 0)
436 return -EINVAL;
437 break;
438
David Howellsdf423a42016-09-02 22:39:45 +0100439 default:
440 return -EINVAL;
441 }
442 }
443
444 if (!got_user_ID)
445 return -EINVAL;
446 _leave(" = 0");
447 return 0;
448}
449
450/*
David Howellsdf423a42016-09-02 22:39:45 +0100451 * Create a new client call for sendmsg().
David Howells540b1c42017-02-27 15:43:06 +0000452 * - Called with the socket lock held, which it must release.
453 * - If it returns a call, the call's lock will need releasing by the caller.
David Howellsdf423a42016-09-02 22:39:45 +0100454 */
455static struct rxrpc_call *
456rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
David Howells3ab26a62017-06-07 14:41:52 +0100457 struct rxrpc_send_params *p)
David Howells540b1c42017-02-27 15:43:06 +0000458 __releases(&rx->sk.sk_lock.slock)
David Howellsdf423a42016-09-02 22:39:45 +0100459{
460 struct rxrpc_conn_parameters cp;
461 struct rxrpc_call *call;
462 struct key *key;
463
464 DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
465
466 _enter("");
467
David Howells540b1c42017-02-27 15:43:06 +0000468 if (!msg->msg_name) {
469 release_sock(&rx->sk);
David Howellsdf423a42016-09-02 22:39:45 +0100470 return ERR_PTR(-EDESTADDRREQ);
David Howells540b1c42017-02-27 15:43:06 +0000471 }
David Howellsdf423a42016-09-02 22:39:45 +0100472
473 key = rx->key;
474 if (key && !rx->key->payload.data[0])
475 key = NULL;
476
477 memset(&cp, 0, sizeof(cp));
478 cp.local = rx->local;
479 cp.key = rx->key;
480 cp.security_level = rx->min_sec_level;
David Howells3ab26a62017-06-07 14:41:52 +0100481 cp.exclusive = rx->exclusive | p->exclusive;
482 cp.upgrade = p->upgrade;
David Howellsdf423a42016-09-02 22:39:45 +0100483 cp.service_id = srx->srx_service;
David Howells3ab26a62017-06-07 14:41:52 +0100484 call = rxrpc_new_client_call(rx, &cp, srx, p->user_call_ID, GFP_KERNEL);
David Howells540b1c42017-02-27 15:43:06 +0000485 /* The socket is now unlocked */
David Howellsdf423a42016-09-02 22:39:45 +0100486
487 _leave(" = %p\n", call);
488 return call;
489}
490
491/*
492 * send a message forming part of a client call through an RxRPC socket
493 * - caller holds the socket locked
494 * - the socket may be either a client socket or a server socket
495 */
496int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
David Howells540b1c42017-02-27 15:43:06 +0000497 __releases(&rx->sk.sk_lock.slock)
David Howellsdf423a42016-09-02 22:39:45 +0100498{
David Howells146d8fe2017-03-04 00:01:41 +0000499 enum rxrpc_call_state state;
David Howellsdf423a42016-09-02 22:39:45 +0100500 struct rxrpc_call *call;
David Howellsdf423a42016-09-02 22:39:45 +0100501 int ret;
502
David Howells3ab26a62017-06-07 14:41:52 +0100503 struct rxrpc_send_params p = {
504 .user_call_ID = 0,
505 .abort_code = 0,
506 .command = RXRPC_CMD_SEND_DATA,
507 .exclusive = false,
508 .upgrade = true,
509 };
510
David Howellsdf423a42016-09-02 22:39:45 +0100511 _enter("");
512
David Howells3ab26a62017-06-07 14:41:52 +0100513 ret = rxrpc_sendmsg_cmsg(msg, &p);
David Howellsdf423a42016-09-02 22:39:45 +0100514 if (ret < 0)
David Howells540b1c42017-02-27 15:43:06 +0000515 goto error_release_sock;
David Howellsdf423a42016-09-02 22:39:45 +0100516
David Howells3ab26a62017-06-07 14:41:52 +0100517 if (p.command == RXRPC_CMD_ACCEPT) {
David Howells540b1c42017-02-27 15:43:06 +0000518 ret = -EINVAL;
David Howellsdf423a42016-09-02 22:39:45 +0100519 if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
David Howells540b1c42017-02-27 15:43:06 +0000520 goto error_release_sock;
David Howells3ab26a62017-06-07 14:41:52 +0100521 call = rxrpc_accept_call(rx, p.user_call_ID, NULL);
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 Howellsfff724292016-09-07 14:34:21 +0100525 rxrpc_put_call(call, rxrpc_call_put);
David Howellsdf423a42016-09-02 22:39:45 +0100526 return 0;
527 }
528
David Howells3ab26a62017-06-07 14:41:52 +0100529 call = rxrpc_find_call_by_user_ID(rx, p.user_call_ID);
David Howellsdf423a42016-09-02 22:39:45 +0100530 if (!call) {
David Howells540b1c42017-02-27 15:43:06 +0000531 ret = -EBADSLT;
David Howells3ab26a62017-06-07 14:41:52 +0100532 if (p.command != RXRPC_CMD_SEND_DATA)
David Howells540b1c42017-02-27 15:43:06 +0000533 goto error_release_sock;
David Howells3ab26a62017-06-07 14:41:52 +0100534 call = rxrpc_new_client_call_for_sendmsg(rx, msg, &p);
David Howells540b1c42017-02-27 15:43:06 +0000535 /* The socket is now unlocked... */
David Howellsdf423a42016-09-02 22:39:45 +0100536 if (IS_ERR(call))
537 return PTR_ERR(call);
David Howells540b1c42017-02-27 15:43:06 +0000538 /* ... and we have the call lock. */
539 } else {
David Howells146d8fe2017-03-04 00:01:41 +0000540 switch (READ_ONCE(call->state)) {
541 case RXRPC_CALL_UNINITIALISED:
542 case RXRPC_CALL_CLIENT_AWAIT_CONN:
543 case RXRPC_CALL_SERVER_PREALLOC:
544 case RXRPC_CALL_SERVER_SECURING:
545 case RXRPC_CALL_SERVER_ACCEPTING:
546 ret = -EBUSY;
David Howells37411ca2017-03-02 23:48:52 +0000547 goto error_release_sock;
David Howells146d8fe2017-03-04 00:01:41 +0000548 default:
549 break;
550 }
David Howells37411ca2017-03-02 23:48:52 +0000551
David Howells540b1c42017-02-27 15:43:06 +0000552 ret = mutex_lock_interruptible(&call->user_mutex);
553 release_sock(&rx->sk);
554 if (ret < 0) {
555 ret = -ERESTARTSYS;
556 goto error_put;
557 }
David Howellsdf423a42016-09-02 22:39:45 +0100558 }
559
David Howells146d8fe2017-03-04 00:01:41 +0000560 state = READ_ONCE(call->state);
David Howellsdf423a42016-09-02 22:39:45 +0100561 _debug("CALL %d USR %lx ST %d on CONN %p",
David Howells146d8fe2017-03-04 00:01:41 +0000562 call->debug_id, call->user_call_ID, state, call->conn);
David Howellsdf423a42016-09-02 22:39:45 +0100563
David Howells146d8fe2017-03-04 00:01:41 +0000564 if (state >= RXRPC_CALL_COMPLETE) {
David Howellsdf423a42016-09-02 22:39:45 +0100565 /* it's too late for this call */
566 ret = -ESHUTDOWN;
David Howells3ab26a62017-06-07 14:41:52 +0100567 } else if (p.command == RXRPC_CMD_SEND_ABORT) {
David Howellsdf423a42016-09-02 22:39:45 +0100568 ret = 0;
David Howells3ab26a62017-06-07 14:41:52 +0100569 if (rxrpc_abort_call("CMD", call, 0, p.abort_code, -ECONNABORTED))
David Howells26cb02a2016-10-06 08:11:49 +0100570 ret = rxrpc_send_abort_packet(call);
David Howells3ab26a62017-06-07 14:41:52 +0100571 } else if (p.command != RXRPC_CMD_SEND_DATA) {
David Howellsdf423a42016-09-02 22:39:45 +0100572 ret = -EINVAL;
573 } else if (rxrpc_is_client_call(call) &&
David Howells146d8fe2017-03-04 00:01:41 +0000574 state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
David Howellsdf423a42016-09-02 22:39:45 +0100575 /* request phase complete for this client call */
576 ret = -EPROTO;
577 } else if (rxrpc_is_service_call(call) &&
David Howells146d8fe2017-03-04 00:01:41 +0000578 state != RXRPC_CALL_SERVER_ACK_REQUEST &&
579 state != RXRPC_CALL_SERVER_SEND_REPLY) {
David Howellsdf423a42016-09-02 22:39:45 +0100580 /* Reply phase not begun or not complete for service call. */
581 ret = -EPROTO;
582 } else {
583 ret = rxrpc_send_data(rx, call, msg, len);
584 }
585
David Howells540b1c42017-02-27 15:43:06 +0000586 mutex_unlock(&call->user_mutex);
587error_put:
David Howellsfff724292016-09-07 14:34:21 +0100588 rxrpc_put_call(call, rxrpc_call_put);
David Howellsdf423a42016-09-02 22:39:45 +0100589 _leave(" = %d", ret);
590 return ret;
David Howells540b1c42017-02-27 15:43:06 +0000591
592error_release_sock:
593 release_sock(&rx->sk);
594 return ret;
David Howellsdf423a42016-09-02 22:39:45 +0100595}
596
597/**
598 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
599 * @sock: The socket the call is on
600 * @call: The call to send data through
601 * @msg: The data to send
602 * @len: The amount of data to send
603 *
604 * Allow a kernel service to send data on a call. The call must be in an state
605 * appropriate to sending data. No control data should be supplied in @msg,
606 * nor should an address be supplied. MSG_MORE should be flagged if there's
607 * more data to come, otherwise this data will end the transmission phase.
608 */
609int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
610 struct msghdr *msg, size_t len)
611{
612 int ret;
613
614 _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
615
616 ASSERTCMP(msg->msg_name, ==, NULL);
617 ASSERTCMP(msg->msg_control, ==, NULL);
618
David Howells540b1c42017-02-27 15:43:06 +0000619 mutex_lock(&call->user_mutex);
David Howellsdf423a42016-09-02 22:39:45 +0100620
621 _debug("CALL %d USR %lx ST %d on CONN %p",
622 call->debug_id, call->user_call_ID, call->state, call->conn);
623
David Howells146d8fe2017-03-04 00:01:41 +0000624 switch (READ_ONCE(call->state)) {
625 case RXRPC_CALL_CLIENT_SEND_REQUEST:
626 case RXRPC_CALL_SERVER_ACK_REQUEST:
627 case RXRPC_CALL_SERVER_SEND_REPLY:
David Howellsdf423a42016-09-02 22:39:45 +0100628 ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len);
David Howells146d8fe2017-03-04 00:01:41 +0000629 break;
630 case RXRPC_CALL_COMPLETE:
David Howells6fc166d2017-03-09 08:10:32 +0000631 read_lock_bh(&call->state_lock);
632 ret = -call->error;
633 read_unlock_bh(&call->state_lock);
David Howells146d8fe2017-03-04 00:01:41 +0000634 break;
635 default:
David Howellsfb46f6e2017-04-06 10:12:00 +0100636 /* Request phase complete for this client call */
637 trace_rxrpc_rx_eproto(call, 0, tracepoint_string("late_send"));
David Howells146d8fe2017-03-04 00:01:41 +0000638 ret = -EPROTO;
639 break;
David Howellsdf423a42016-09-02 22:39:45 +0100640 }
641
David Howells540b1c42017-02-27 15:43:06 +0000642 mutex_unlock(&call->user_mutex);
David Howellsdf423a42016-09-02 22:39:45 +0100643 _leave(" = %d", ret);
644 return ret;
645}
646EXPORT_SYMBOL(rxrpc_kernel_send_data);
647
648/**
649 * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
650 * @sock: The socket the call is on
651 * @call: The call to be aborted
652 * @abort_code: The abort code to stick into the ABORT packet
David Howells5a429762016-09-06 22:19:51 +0100653 * @error: Local error value
654 * @why: 3-char string indicating why.
David Howellsdf423a42016-09-02 22:39:45 +0100655 *
David Howells84a4c092017-04-06 10:11:59 +0100656 * Allow a kernel service to abort a call, if it's still in an abortable state
657 * and return true if the call was aborted, false if it was already complete.
David Howellsdf423a42016-09-02 22:39:45 +0100658 */
David Howells84a4c092017-04-06 10:11:59 +0100659bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
David Howells5a429762016-09-06 22:19:51 +0100660 u32 abort_code, int error, const char *why)
David Howellsdf423a42016-09-02 22:39:45 +0100661{
David Howells84a4c092017-04-06 10:11:59 +0100662 bool aborted;
663
David Howells5a429762016-09-06 22:19:51 +0100664 _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why);
David Howellsdf423a42016-09-02 22:39:45 +0100665
David Howells540b1c42017-02-27 15:43:06 +0000666 mutex_lock(&call->user_mutex);
David Howellsdf423a42016-09-02 22:39:45 +0100667
David Howells84a4c092017-04-06 10:11:59 +0100668 aborted = rxrpc_abort_call(why, call, 0, abort_code, error);
669 if (aborted)
David Howells26cb02a2016-10-06 08:11:49 +0100670 rxrpc_send_abort_packet(call);
David Howellsdf423a42016-09-02 22:39:45 +0100671
David Howells540b1c42017-02-27 15:43:06 +0000672 mutex_unlock(&call->user_mutex);
David Howells84a4c092017-04-06 10:11:59 +0100673 return aborted;
David Howellsdf423a42016-09-02 22:39:45 +0100674}
675
676EXPORT_SYMBOL(rxrpc_kernel_abort_call);