blob: 03e0676db28c11a0a2220cc97801f3e4d3e8b301 [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 Howells0b58b8a2016-09-02 22:39:45 +010024/*
David Howellsbc5e3a52017-10-18 11:07:31 +010025 * Wait for space to appear in the Tx queue or a signal to occur.
26 */
27static int rxrpc_wait_for_tx_window_intr(struct rxrpc_sock *rx,
28 struct rxrpc_call *call,
29 long *timeo)
30{
31 for (;;) {
32 set_current_state(TASK_INTERRUPTIBLE);
33 if (call->tx_top - call->tx_hard_ack <
34 min_t(unsigned int, call->tx_winsize,
35 call->cong_cwnd + call->cong_extra))
36 return 0;
37
38 if (call->state >= RXRPC_CALL_COMPLETE)
39 return call->error;
40
41 if (signal_pending(current))
42 return sock_intr_errno(*timeo);
43
44 trace_rxrpc_transmit(call, rxrpc_transmit_wait);
45 mutex_unlock(&call->user_mutex);
46 *timeo = schedule_timeout(*timeo);
47 if (mutex_lock_interruptible(&call->user_mutex) < 0)
48 return sock_intr_errno(*timeo);
49 }
50}
51
52/*
53 * Wait for space to appear in the Tx queue uninterruptibly, but with
54 * a timeout of 2*RTT if no progress was made and a signal occurred.
55 */
56static int rxrpc_wait_for_tx_window_nonintr(struct rxrpc_sock *rx,
57 struct rxrpc_call *call)
58{
59 rxrpc_seq_t tx_start, tx_win;
60 signed long rtt2, timeout;
61 u64 rtt;
62
63 rtt = READ_ONCE(call->peer->rtt);
64 rtt2 = nsecs_to_jiffies64(rtt) * 2;
65 if (rtt2 < 1)
66 rtt2 = 1;
67
68 timeout = rtt2;
69 tx_start = READ_ONCE(call->tx_hard_ack);
70
71 for (;;) {
72 set_current_state(TASK_UNINTERRUPTIBLE);
73
74 tx_win = READ_ONCE(call->tx_hard_ack);
75 if (call->tx_top - tx_win <
76 min_t(unsigned int, call->tx_winsize,
77 call->cong_cwnd + call->cong_extra))
78 return 0;
79
80 if (call->state >= RXRPC_CALL_COMPLETE)
81 return call->error;
82
83 if (timeout == 0 &&
84 tx_win == tx_start && signal_pending(current))
85 return -EINTR;
86
87 if (tx_win != tx_start) {
88 timeout = rtt2;
89 tx_start = tx_win;
90 }
91
92 trace_rxrpc_transmit(call, rxrpc_transmit_wait);
93 timeout = schedule_timeout(timeout);
94 }
95}
96
97/*
David Howells0b58b8a2016-09-02 22:39:45 +010098 * wait for space to appear in the transmit/ACK window
99 * - caller holds the socket locked
100 */
101static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
102 struct rxrpc_call *call,
David Howellsbc5e3a52017-10-18 11:07:31 +0100103 long *timeo,
104 bool waitall)
David Howells0b58b8a2016-09-02 22:39:45 +0100105{
106 DECLARE_WAITQUEUE(myself, current);
107 int ret;
108
David Howells248f2192016-09-08 11:10:12 +0100109 _enter(",{%u,%u,%u}",
110 call->tx_hard_ack, call->tx_top, call->tx_winsize);
David Howells0b58b8a2016-09-02 22:39:45 +0100111
112 add_wait_queue(&call->waitq, &myself);
113
David Howellsbc5e3a52017-10-18 11:07:31 +0100114 if (waitall)
115 ret = rxrpc_wait_for_tx_window_nonintr(rx, call);
116 else
117 ret = rxrpc_wait_for_tx_window_intr(rx, call, timeo);
David Howells0b58b8a2016-09-02 22:39:45 +0100118
119 remove_wait_queue(&call->waitq, &myself);
120 set_current_state(TASK_RUNNING);
121 _leave(" = %d", ret);
122 return ret;
123}
124
125/*
David Howells248f2192016-09-08 11:10:12 +0100126 * Schedule an instant Tx resend.
David Howells0b58b8a2016-09-02 22:39:45 +0100127 */
David Howells248f2192016-09-08 11:10:12 +0100128static inline void rxrpc_instant_resend(struct rxrpc_call *call, int ix)
David Howells0b58b8a2016-09-02 22:39:45 +0100129{
David Howells248f2192016-09-08 11:10:12 +0100130 spin_lock_bh(&call->lock);
131
132 if (call->state < RXRPC_CALL_COMPLETE) {
133 call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS;
134 if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
David Howells0b58b8a2016-09-02 22:39:45 +0100135 rxrpc_queue_call(call);
136 }
David Howells248f2192016-09-08 11:10:12 +0100137
138 spin_unlock_bh(&call->lock);
David Howells0b58b8a2016-09-02 22:39:45 +0100139}
140
141/*
David Howellse8332512017-08-29 10:18:56 +0100142 * Notify the owner of the call that the transmit phase is ended and the last
143 * packet has been queued.
144 */
145static void rxrpc_notify_end_tx(struct rxrpc_sock *rx, struct rxrpc_call *call,
146 rxrpc_notify_end_tx_t notify_end_tx)
147{
148 if (notify_end_tx)
149 notify_end_tx(&rx->sk, call, call->user_call_ID);
150}
151
152/*
David Howells248f2192016-09-08 11:10:12 +0100153 * Queue a DATA packet for transmission, set the resend timeout and send the
154 * packet immediately
David Howells0b58b8a2016-09-02 22:39:45 +0100155 */
David Howellse8332512017-08-29 10:18:56 +0100156static void rxrpc_queue_packet(struct rxrpc_sock *rx, struct rxrpc_call *call,
157 struct sk_buff *skb, bool last,
158 rxrpc_notify_end_tx_t notify_end_tx)
David Howells0b58b8a2016-09-02 22:39:45 +0100159{
160 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howellsa158bdd2017-11-24 10:18:41 +0000161 unsigned long now;
David Howells248f2192016-09-08 11:10:12 +0100162 rxrpc_seq_t seq = sp->hdr.seq;
163 int ret, ix;
David Howells70790dbe2016-09-23 12:39:22 +0100164 u8 annotation = RXRPC_TX_ANNO_UNACK;
David Howells0b58b8a2016-09-02 22:39:45 +0100165
David Howells248f2192016-09-08 11:10:12 +0100166 _net("queue skb %p [%d]", skb, seq);
David Howells0b58b8a2016-09-02 22:39:45 +0100167
David Howells248f2192016-09-08 11:10:12 +0100168 ASSERTCMP(seq, ==, call->tx_top + 1);
169
David Howellsc038a582017-08-29 10:19:01 +0100170 if (last) {
David Howells70790dbe2016-09-23 12:39:22 +0100171 annotation |= RXRPC_TX_ANNO_LAST;
David Howellsc038a582017-08-29 10:19:01 +0100172 set_bit(RXRPC_CALL_TX_LASTQ, &call->flags);
173 }
David Howells70790dbe2016-09-23 12:39:22 +0100174
David Howellsb24d2892016-09-23 13:17:33 +0100175 /* We have to set the timestamp before queueing as the retransmit
176 * algorithm can see the packet as soon as we queue it.
177 */
178 skb->tstamp = ktime_get_real();
179
David Howells248f2192016-09-08 11:10:12 +0100180 ix = seq & RXRPC_RXTX_BUFF_MASK;
David Howells71f3ca42016-09-17 10:49:14 +0100181 rxrpc_get_skb(skb, rxrpc_skb_tx_got);
David Howells70790dbe2016-09-23 12:39:22 +0100182 call->rxtx_annotations[ix] = annotation;
David Howells0b58b8a2016-09-02 22:39:45 +0100183 smp_wmb();
David Howells248f2192016-09-08 11:10:12 +0100184 call->rxtx_buffer[ix] = skb;
185 call->tx_top = seq;
David Howells70790dbe2016-09-23 12:39:22 +0100186 if (last)
David Howellsa124fe32016-09-17 10:49:13 +0100187 trace_rxrpc_transmit(call, rxrpc_transmit_queue_last);
David Howells70790dbe2016-09-23 12:39:22 +0100188 else
David Howellsa124fe32016-09-17 10:49:13 +0100189 trace_rxrpc_transmit(call, rxrpc_transmit_queue);
David Howells0b58b8a2016-09-02 22:39:45 +0100190
191 if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
192 _debug("________awaiting reply/ACK__________");
193 write_lock_bh(&call->state_lock);
194 switch (call->state) {
195 case RXRPC_CALL_CLIENT_SEND_REQUEST:
196 call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
David Howellse8332512017-08-29 10:18:56 +0100197 rxrpc_notify_end_tx(rx, call, notify_end_tx);
David Howells0b58b8a2016-09-02 22:39:45 +0100198 break;
199 case RXRPC_CALL_SERVER_ACK_REQUEST:
200 call->state = RXRPC_CALL_SERVER_SEND_REPLY;
David Howellsa158bdd2017-11-24 10:18:41 +0000201 now = jiffies;
202 WRITE_ONCE(call->ack_at, now + MAX_JIFFY_OFFSET);
David Howells9749fd22016-10-06 08:11:50 +0100203 if (call->ackr_reason == RXRPC_ACK_DELAY)
204 call->ackr_reason = 0;
David Howellsa158bdd2017-11-24 10:18:41 +0000205 trace_rxrpc_timer(call, rxrpc_timer_init_for_send_reply, now);
David Howells0b58b8a2016-09-02 22:39:45 +0100206 if (!last)
207 break;
Gustavo A. R. Silvae3cf39702017-10-19 16:54:48 -0500208 /* Fall through */
David Howells0b58b8a2016-09-02 22:39:45 +0100209 case RXRPC_CALL_SERVER_SEND_REPLY:
210 call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
David Howellse8332512017-08-29 10:18:56 +0100211 rxrpc_notify_end_tx(rx, call, notify_end_tx);
David Howells0b58b8a2016-09-02 22:39:45 +0100212 break;
213 default:
214 break;
215 }
216 write_unlock_bh(&call->state_lock);
217 }
218
David Howells248f2192016-09-08 11:10:12 +0100219 if (seq == 1 && rxrpc_is_client_call(call))
220 rxrpc_expose_client_call(call);
221
David Howellsa1767072016-09-29 22:37:15 +0100222 ret = rxrpc_send_data_packet(call, skb, false);
David Howells0b58b8a2016-09-02 22:39:45 +0100223 if (ret < 0) {
224 _debug("need instant resend %d", ret);
David Howells248f2192016-09-08 11:10:12 +0100225 rxrpc_instant_resend(call, ix);
David Howellsdfc3da42016-09-23 12:39:23 +0100226 } else {
David Howellsa158bdd2017-11-24 10:18:41 +0000227 unsigned long now = jiffies, resend_at;
David Howellsdfc3da42016-09-23 12:39:23 +0100228
David Howellsa158bdd2017-11-24 10:18:41 +0000229 resend_at = now + rxrpc_resend_timeout;
230 WRITE_ONCE(call->resend_at, resend_at);
231 rxrpc_reduce_call_timer(call, resend_at, now,
232 rxrpc_timer_set_for_send);
David Howells0b58b8a2016-09-02 22:39:45 +0100233 }
234
David Howells71f3ca42016-09-17 10:49:14 +0100235 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
David Howells0b58b8a2016-09-02 22:39:45 +0100236 _leave("");
237}
238
239/*
David Howells0b58b8a2016-09-02 22:39:45 +0100240 * send data through a socket
241 * - must be called in process context
David Howells540b1c42017-02-27 15:43:06 +0000242 * - The caller holds the call user access mutex, but not the socket lock.
David Howells0b58b8a2016-09-02 22:39:45 +0100243 */
244static int rxrpc_send_data(struct rxrpc_sock *rx,
245 struct rxrpc_call *call,
David Howellse8332512017-08-29 10:18:56 +0100246 struct msghdr *msg, size_t len,
247 rxrpc_notify_end_tx_t notify_end_tx)
David Howells0b58b8a2016-09-02 22:39:45 +0100248{
249 struct rxrpc_skb_priv *sp;
250 struct sk_buff *skb;
251 struct sock *sk = &rx->sk;
252 long timeo;
253 bool more;
254 int ret, copied;
255
256 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
257
258 /* this should be in poll */
259 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
260
261 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
262 return -EPIPE;
263
264 more = msg->msg_flags & MSG_MORE;
265
David Howellse754eba2017-06-07 12:40:03 +0100266 if (call->tx_total_len != -1) {
267 if (len > call->tx_total_len)
268 return -EMSGSIZE;
269 if (!more && len != call->tx_total_len)
270 return -EMSGSIZE;
271 }
272
David Howells0b58b8a2016-09-02 22:39:45 +0100273 skb = call->tx_pending;
274 call->tx_pending = NULL;
David Howells71f3ca42016-09-17 10:49:14 +0100275 rxrpc_see_skb(skb, rxrpc_skb_tx_seen);
David Howells0b58b8a2016-09-02 22:39:45 +0100276
277 copied = 0;
278 do {
David Howells7aa51da2016-09-22 00:29:31 +0100279 /* Check to see if there's a ping ACK to reply to. */
280 if (call->ackr_reason == RXRPC_ACK_PING_RESPONSE)
David Howellsa5af7e12016-10-06 08:11:49 +0100281 rxrpc_send_ack_packet(call, false);
David Howells7aa51da2016-09-22 00:29:31 +0100282
David Howells0b58b8a2016-09-02 22:39:45 +0100283 if (!skb) {
284 size_t size, chunk, max, space;
285
286 _debug("alloc");
287
David Howells248f2192016-09-08 11:10:12 +0100288 if (call->tx_top - call->tx_hard_ack >=
David Howells57494342016-09-24 18:05:27 +0100289 min_t(unsigned int, call->tx_winsize,
290 call->cong_cwnd + call->cong_extra)) {
David Howells0b58b8a2016-09-02 22:39:45 +0100291 ret = -EAGAIN;
292 if (msg->msg_flags & MSG_DONTWAIT)
293 goto maybe_error;
294 ret = rxrpc_wait_for_tx_window(rx, call,
David Howellsbc5e3a52017-10-18 11:07:31 +0100295 &timeo,
296 msg->msg_flags & MSG_WAITALL);
David Howells0b58b8a2016-09-02 22:39:45 +0100297 if (ret < 0)
298 goto maybe_error;
299 }
300
David Howells182f5052016-09-17 10:49:12 +0100301 max = RXRPC_JUMBO_DATALEN;
David Howells0b58b8a2016-09-02 22:39:45 +0100302 max -= call->conn->security_size;
303 max &= ~(call->conn->size_align - 1UL);
304
305 chunk = max;
306 if (chunk > msg_data_left(msg) && !more)
307 chunk = msg_data_left(msg);
308
309 space = chunk + call->conn->size_align;
310 space &= ~(call->conn->size_align - 1UL);
311
David Howells5a924b82016-09-22 00:29:31 +0100312 size = space + call->conn->security_size;
David Howells0b58b8a2016-09-02 22:39:45 +0100313
314 _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
315
316 /* create a buffer that we can retain until it's ACK'd */
317 skb = sock_alloc_send_skb(
318 sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
319 if (!skb)
320 goto maybe_error;
321
David Howells71f3ca42016-09-17 10:49:14 +0100322 rxrpc_new_skb(skb, rxrpc_skb_tx_new);
David Howells0b58b8a2016-09-02 22:39:45 +0100323
324 _debug("ALLOC SEND %p", skb);
325
326 ASSERTCMP(skb->mark, ==, 0);
327
David Howells5a924b82016-09-22 00:29:31 +0100328 _debug("HS: %u", call->conn->security_size);
329 skb_reserve(skb, call->conn->security_size);
330 skb->len += call->conn->security_size;
David Howells0b58b8a2016-09-02 22:39:45 +0100331
332 sp = rxrpc_skb(skb);
333 sp->remain = chunk;
334 if (sp->remain > skb_tailroom(skb))
335 sp->remain = skb_tailroom(skb);
336
337 _net("skb: hr %d, tr %d, hl %d, rm %d",
338 skb_headroom(skb),
339 skb_tailroom(skb),
340 skb_headlen(skb),
341 sp->remain);
342
343 skb->ip_summed = CHECKSUM_UNNECESSARY;
344 }
345
346 _debug("append");
347 sp = rxrpc_skb(skb);
348
349 /* append next segment of data to the current buffer */
350 if (msg_data_left(msg) > 0) {
351 int copy = skb_tailroom(skb);
352 ASSERTCMP(copy, >, 0);
353 if (copy > msg_data_left(msg))
354 copy = msg_data_left(msg);
355 if (copy > sp->remain)
356 copy = sp->remain;
357
358 _debug("add");
359 ret = skb_add_data(skb, &msg->msg_iter, copy);
360 _debug("added");
361 if (ret < 0)
362 goto efault;
363 sp->remain -= copy;
364 skb->mark += copy;
365 copied += copy;
David Howellse754eba2017-06-07 12:40:03 +0100366 if (call->tx_total_len != -1)
367 call->tx_total_len -= copy;
David Howells0b58b8a2016-09-02 22:39:45 +0100368 }
369
David Howells0b58b8a2016-09-02 22:39:45 +0100370 /* add the packet to the send queue if it's now full */
371 if (sp->remain <= 0 ||
372 (msg_data_left(msg) == 0 && !more)) {
373 struct rxrpc_connection *conn = call->conn;
374 uint32_t seq;
375 size_t pad;
376
377 /* pad out if we're using security */
378 if (conn->security_ix) {
379 pad = conn->security_size + skb->mark;
380 pad = conn->size_align - pad;
381 pad &= conn->size_align - 1;
382 _debug("pad %zu", pad);
383 if (pad)
Johannes Bergb080db52017-06-16 14:29:19 +0200384 skb_put_zero(skb, pad);
David Howells0b58b8a2016-09-02 22:39:45 +0100385 }
386
David Howells248f2192016-09-08 11:10:12 +0100387 seq = call->tx_top + 1;
David Howells0b58b8a2016-09-02 22:39:45 +0100388
David Howells0b58b8a2016-09-02 22:39:45 +0100389 sp->hdr.seq = seq;
David Howells0b58b8a2016-09-02 22:39:45 +0100390 sp->hdr._rsvd = 0;
David Howells5a924b82016-09-22 00:29:31 +0100391 sp->hdr.flags = conn->out_clientflag;
David Howells0b58b8a2016-09-02 22:39:45 +0100392
David Howells0b58b8a2016-09-02 22:39:45 +0100393 if (msg_data_left(msg) == 0 && !more)
394 sp->hdr.flags |= RXRPC_LAST_PACKET;
David Howells248f2192016-09-08 11:10:12 +0100395 else if (call->tx_top - call->tx_hard_ack <
396 call->tx_winsize)
David Howells0b58b8a2016-09-02 22:39:45 +0100397 sp->hdr.flags |= RXRPC_MORE_PACKETS;
David Howells0b58b8a2016-09-02 22:39:45 +0100398
399 ret = conn->security->secure_packet(
David Howells5a924b82016-09-22 00:29:31 +0100400 call, skb, skb->mark, skb->head);
David Howells0b58b8a2016-09-02 22:39:45 +0100401 if (ret < 0)
402 goto out;
403
David Howellse8332512017-08-29 10:18:56 +0100404 rxrpc_queue_packet(rx, call, skb,
405 !msg_data_left(msg) && !more,
406 notify_end_tx);
David Howells0b58b8a2016-09-02 22:39:45 +0100407 skb = NULL;
408 }
David Howellsc038a582017-08-29 10:19:01 +0100409
410 /* Check for the far side aborting the call or a network error
411 * occurring. If this happens, save any packet that was under
412 * construction so that in the case of a network error, the
413 * call can be retried or redirected.
414 */
415 if (call->state == RXRPC_CALL_COMPLETE) {
416 ret = call->error;
417 goto out;
418 }
David Howells0b58b8a2016-09-02 22:39:45 +0100419 } while (msg_data_left(msg) > 0);
420
421success:
422 ret = copied;
423out:
424 call->tx_pending = skb;
425 _leave(" = %d", ret);
426 return ret;
427
David Howells0b58b8a2016-09-02 22:39:45 +0100428maybe_error:
429 if (copied)
430 goto success;
431 goto out;
432
433efault:
434 ret = -EFAULT;
435 goto out;
436}
David Howellsdf423a42016-09-02 22:39:45 +0100437
438/*
439 * extract control messages from the sendmsg() control buffer
440 */
David Howells3ab26a62017-06-07 14:41:52 +0100441static int rxrpc_sendmsg_cmsg(struct msghdr *msg, struct rxrpc_send_params *p)
David Howellsdf423a42016-09-02 22:39:45 +0100442{
443 struct cmsghdr *cmsg;
444 bool got_user_ID = false;
445 int len;
446
David Howellsdf423a42016-09-02 22:39:45 +0100447 if (msg->msg_controllen == 0)
448 return -EINVAL;
449
450 for_each_cmsghdr(cmsg, msg) {
451 if (!CMSG_OK(msg, cmsg))
452 return -EINVAL;
453
yuan linyu1ff8ceb2017-01-03 20:42:17 +0800454 len = cmsg->cmsg_len - sizeof(struct cmsghdr);
David Howellsdf423a42016-09-02 22:39:45 +0100455 _debug("CMSG %d, %d, %d",
456 cmsg->cmsg_level, cmsg->cmsg_type, len);
457
458 if (cmsg->cmsg_level != SOL_RXRPC)
459 continue;
460
461 switch (cmsg->cmsg_type) {
462 case RXRPC_USER_CALL_ID:
463 if (msg->msg_flags & MSG_CMSG_COMPAT) {
464 if (len != sizeof(u32))
465 return -EINVAL;
David Howells48124172017-11-24 10:18:41 +0000466 p->call.user_call_ID = *(u32 *)CMSG_DATA(cmsg);
David Howellsdf423a42016-09-02 22:39:45 +0100467 } else {
468 if (len != sizeof(unsigned long))
469 return -EINVAL;
David Howells48124172017-11-24 10:18:41 +0000470 p->call.user_call_ID = *(unsigned long *)
David Howellsdf423a42016-09-02 22:39:45 +0100471 CMSG_DATA(cmsg);
472 }
David Howellsdf423a42016-09-02 22:39:45 +0100473 got_user_ID = true;
474 break;
475
476 case RXRPC_ABORT:
David Howells3ab26a62017-06-07 14:41:52 +0100477 if (p->command != RXRPC_CMD_SEND_DATA)
David Howellsdf423a42016-09-02 22:39:45 +0100478 return -EINVAL;
David Howells3ab26a62017-06-07 14:41:52 +0100479 p->command = RXRPC_CMD_SEND_ABORT;
480 if (len != sizeof(p->abort_code))
David Howellsdf423a42016-09-02 22:39:45 +0100481 return -EINVAL;
David Howells3ab26a62017-06-07 14:41:52 +0100482 p->abort_code = *(unsigned int *)CMSG_DATA(cmsg);
483 if (p->abort_code == 0)
David Howellsdf423a42016-09-02 22:39:45 +0100484 return -EINVAL;
485 break;
486
487 case RXRPC_ACCEPT:
David Howells3ab26a62017-06-07 14:41:52 +0100488 if (p->command != RXRPC_CMD_SEND_DATA)
David Howellsdf423a42016-09-02 22:39:45 +0100489 return -EINVAL;
David Howells3ab26a62017-06-07 14:41:52 +0100490 p->command = RXRPC_CMD_ACCEPT;
David Howellsdf423a42016-09-02 22:39:45 +0100491 if (len != 0)
492 return -EINVAL;
493 break;
494
495 case RXRPC_EXCLUSIVE_CALL:
David Howells3ab26a62017-06-07 14:41:52 +0100496 p->exclusive = true;
David Howellsdf423a42016-09-02 22:39:45 +0100497 if (len != 0)
498 return -EINVAL;
499 break;
David Howells4e255722017-06-05 14:30:49 +0100500
501 case RXRPC_UPGRADE_SERVICE:
David Howells3ab26a62017-06-07 14:41:52 +0100502 p->upgrade = true;
David Howells4e255722017-06-05 14:30:49 +0100503 if (len != 0)
504 return -EINVAL;
505 break;
506
David Howellse754eba2017-06-07 12:40:03 +0100507 case RXRPC_TX_LENGTH:
David Howells48124172017-11-24 10:18:41 +0000508 if (p->call.tx_total_len != -1 || len != sizeof(__s64))
David Howellse754eba2017-06-07 12:40:03 +0100509 return -EINVAL;
David Howells48124172017-11-24 10:18:41 +0000510 p->call.tx_total_len = *(__s64 *)CMSG_DATA(cmsg);
511 if (p->call.tx_total_len < 0)
David Howellse754eba2017-06-07 12:40:03 +0100512 return -EINVAL;
513 break;
514
David Howellsa158bdd2017-11-24 10:18:41 +0000515 case RXRPC_SET_CALL_TIMEOUT:
516 if (len & 3 || len < 4 || len > 12)
517 return -EINVAL;
518 memcpy(&p->call.timeouts, CMSG_DATA(cmsg), len);
519 p->call.nr_timeouts = len / 4;
520 if (p->call.timeouts.hard > INT_MAX / HZ)
521 return -ERANGE;
522 if (p->call.nr_timeouts >= 2 && p->call.timeouts.idle > 60 * 60 * 1000)
523 return -ERANGE;
524 if (p->call.nr_timeouts >= 3 && p->call.timeouts.normal > 60 * 60 * 1000)
525 return -ERANGE;
526 break;
527
David Howellsdf423a42016-09-02 22:39:45 +0100528 default:
529 return -EINVAL;
530 }
531 }
532
533 if (!got_user_ID)
534 return -EINVAL;
David Howells48124172017-11-24 10:18:41 +0000535 if (p->call.tx_total_len != -1 && p->command != RXRPC_CMD_SEND_DATA)
David Howellse754eba2017-06-07 12:40:03 +0100536 return -EINVAL;
David Howellsdf423a42016-09-02 22:39:45 +0100537 _leave(" = 0");
538 return 0;
539}
540
541/*
David Howellsdf423a42016-09-02 22:39:45 +0100542 * Create a new client call for sendmsg().
David Howells540b1c42017-02-27 15:43:06 +0000543 * - Called with the socket lock held, which it must release.
544 * - If it returns a call, the call's lock will need releasing by the caller.
David Howellsdf423a42016-09-02 22:39:45 +0100545 */
546static struct rxrpc_call *
547rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
David Howells3ab26a62017-06-07 14:41:52 +0100548 struct rxrpc_send_params *p)
David Howells540b1c42017-02-27 15:43:06 +0000549 __releases(&rx->sk.sk_lock.slock)
David Howellsdf423a42016-09-02 22:39:45 +0100550{
551 struct rxrpc_conn_parameters cp;
552 struct rxrpc_call *call;
553 struct key *key;
554
555 DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
556
557 _enter("");
558
David Howells540b1c42017-02-27 15:43:06 +0000559 if (!msg->msg_name) {
560 release_sock(&rx->sk);
David Howellsdf423a42016-09-02 22:39:45 +0100561 return ERR_PTR(-EDESTADDRREQ);
David Howells540b1c42017-02-27 15:43:06 +0000562 }
David Howellsdf423a42016-09-02 22:39:45 +0100563
564 key = rx->key;
565 if (key && !rx->key->payload.data[0])
566 key = NULL;
567
568 memset(&cp, 0, sizeof(cp));
569 cp.local = rx->local;
570 cp.key = rx->key;
571 cp.security_level = rx->min_sec_level;
David Howells3ab26a62017-06-07 14:41:52 +0100572 cp.exclusive = rx->exclusive | p->exclusive;
573 cp.upgrade = p->upgrade;
David Howellsdf423a42016-09-02 22:39:45 +0100574 cp.service_id = srx->srx_service;
David Howells48124172017-11-24 10:18:41 +0000575 call = rxrpc_new_client_call(rx, &cp, srx, &p->call, GFP_KERNEL);
David Howells540b1c42017-02-27 15:43:06 +0000576 /* The socket is now unlocked */
David Howellsdf423a42016-09-02 22:39:45 +0100577
578 _leave(" = %p\n", call);
579 return call;
580}
581
582/*
583 * send a message forming part of a client call through an RxRPC socket
584 * - caller holds the socket locked
585 * - the socket may be either a client socket or a server socket
586 */
587int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
David Howells540b1c42017-02-27 15:43:06 +0000588 __releases(&rx->sk.sk_lock.slock)
David Howellsdf423a42016-09-02 22:39:45 +0100589{
David Howells146d8fe2017-03-04 00:01:41 +0000590 enum rxrpc_call_state state;
David Howellsdf423a42016-09-02 22:39:45 +0100591 struct rxrpc_call *call;
David Howellsa158bdd2017-11-24 10:18:41 +0000592 unsigned long now, j;
David Howellsdf423a42016-09-02 22:39:45 +0100593 int ret;
594
David Howells3ab26a62017-06-07 14:41:52 +0100595 struct rxrpc_send_params p = {
David Howells48124172017-11-24 10:18:41 +0000596 .call.tx_total_len = -1,
597 .call.user_call_ID = 0,
David Howellsa158bdd2017-11-24 10:18:41 +0000598 .call.nr_timeouts = 0,
David Howells48124172017-11-24 10:18:41 +0000599 .abort_code = 0,
600 .command = RXRPC_CMD_SEND_DATA,
601 .exclusive = false,
602 .upgrade = false,
David Howells3ab26a62017-06-07 14:41:52 +0100603 };
604
David Howellsdf423a42016-09-02 22:39:45 +0100605 _enter("");
606
David Howells3ab26a62017-06-07 14:41:52 +0100607 ret = rxrpc_sendmsg_cmsg(msg, &p);
David Howellsdf423a42016-09-02 22:39:45 +0100608 if (ret < 0)
David Howells540b1c42017-02-27 15:43:06 +0000609 goto error_release_sock;
David Howellsdf423a42016-09-02 22:39:45 +0100610
David Howells3ab26a62017-06-07 14:41:52 +0100611 if (p.command == RXRPC_CMD_ACCEPT) {
David Howells540b1c42017-02-27 15:43:06 +0000612 ret = -EINVAL;
David Howellsdf423a42016-09-02 22:39:45 +0100613 if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
David Howells540b1c42017-02-27 15:43:06 +0000614 goto error_release_sock;
David Howells48124172017-11-24 10:18:41 +0000615 call = rxrpc_accept_call(rx, p.call.user_call_ID, NULL);
David Howells540b1c42017-02-27 15:43:06 +0000616 /* The socket is now unlocked. */
David Howellsdf423a42016-09-02 22:39:45 +0100617 if (IS_ERR(call))
618 return PTR_ERR(call);
David Howells03a6c822017-11-24 10:18:40 +0000619 ret = 0;
620 goto out_put_unlock;
David Howellsdf423a42016-09-02 22:39:45 +0100621 }
622
David Howells48124172017-11-24 10:18:41 +0000623 call = rxrpc_find_call_by_user_ID(rx, p.call.user_call_ID);
David Howellsdf423a42016-09-02 22:39:45 +0100624 if (!call) {
David Howells540b1c42017-02-27 15:43:06 +0000625 ret = -EBADSLT;
David Howells3ab26a62017-06-07 14:41:52 +0100626 if (p.command != RXRPC_CMD_SEND_DATA)
David Howells540b1c42017-02-27 15:43:06 +0000627 goto error_release_sock;
David Howells3ab26a62017-06-07 14:41:52 +0100628 call = rxrpc_new_client_call_for_sendmsg(rx, msg, &p);
David Howells540b1c42017-02-27 15:43:06 +0000629 /* The socket is now unlocked... */
David Howellsdf423a42016-09-02 22:39:45 +0100630 if (IS_ERR(call))
631 return PTR_ERR(call);
David Howells540b1c42017-02-27 15:43:06 +0000632 /* ... and we have the call lock. */
633 } else {
David Howells146d8fe2017-03-04 00:01:41 +0000634 switch (READ_ONCE(call->state)) {
635 case RXRPC_CALL_UNINITIALISED:
636 case RXRPC_CALL_CLIENT_AWAIT_CONN:
637 case RXRPC_CALL_SERVER_PREALLOC:
638 case RXRPC_CALL_SERVER_SECURING:
639 case RXRPC_CALL_SERVER_ACCEPTING:
640 ret = -EBUSY;
David Howells37411ca2017-03-02 23:48:52 +0000641 goto error_release_sock;
David Howells146d8fe2017-03-04 00:01:41 +0000642 default:
643 break;
644 }
David Howells37411ca2017-03-02 23:48:52 +0000645
David Howells540b1c42017-02-27 15:43:06 +0000646 ret = mutex_lock_interruptible(&call->user_mutex);
647 release_sock(&rx->sk);
648 if (ret < 0) {
649 ret = -ERESTARTSYS;
650 goto error_put;
651 }
David Howellse754eba2017-06-07 12:40:03 +0100652
David Howells48124172017-11-24 10:18:41 +0000653 if (p.call.tx_total_len != -1) {
David Howellse754eba2017-06-07 12:40:03 +0100654 ret = -EINVAL;
655 if (call->tx_total_len != -1 ||
656 call->tx_pending ||
657 call->tx_top != 0)
658 goto error_put;
David Howells48124172017-11-24 10:18:41 +0000659 call->tx_total_len = p.call.tx_total_len;
David Howellse754eba2017-06-07 12:40:03 +0100660 }
David Howellsdf423a42016-09-02 22:39:45 +0100661 }
662
David Howellsa158bdd2017-11-24 10:18:41 +0000663 switch (p.call.nr_timeouts) {
664 case 3:
665 j = msecs_to_jiffies(p.call.timeouts.normal);
666 if (p.call.timeouts.normal > 0 && j == 0)
667 j = 1;
668 WRITE_ONCE(call->next_rx_timo, j);
669 /* Fall through */
670 case 2:
671 j = msecs_to_jiffies(p.call.timeouts.idle);
672 if (p.call.timeouts.idle > 0 && j == 0)
673 j = 1;
674 WRITE_ONCE(call->next_req_timo, j);
675 /* Fall through */
676 case 1:
677 if (p.call.timeouts.hard > 0) {
678 j = msecs_to_jiffies(p.call.timeouts.hard);
679 now = jiffies;
680 j += now;
681 WRITE_ONCE(call->expect_term_by, j);
682 rxrpc_reduce_call_timer(call, j, now,
683 rxrpc_timer_set_for_hard);
684 }
685 break;
686 }
687
David Howells146d8fe2017-03-04 00:01:41 +0000688 state = READ_ONCE(call->state);
David Howellsdf423a42016-09-02 22:39:45 +0100689 _debug("CALL %d USR %lx ST %d on CONN %p",
David Howells146d8fe2017-03-04 00:01:41 +0000690 call->debug_id, call->user_call_ID, state, call->conn);
David Howellsdf423a42016-09-02 22:39:45 +0100691
David Howells146d8fe2017-03-04 00:01:41 +0000692 if (state >= RXRPC_CALL_COMPLETE) {
David Howellsdf423a42016-09-02 22:39:45 +0100693 /* it's too late for this call */
694 ret = -ESHUTDOWN;
David Howells3ab26a62017-06-07 14:41:52 +0100695 } else if (p.command == RXRPC_CMD_SEND_ABORT) {
David Howellsdf423a42016-09-02 22:39:45 +0100696 ret = 0;
David Howells3ab26a62017-06-07 14:41:52 +0100697 if (rxrpc_abort_call("CMD", call, 0, p.abort_code, -ECONNABORTED))
David Howells26cb02a2016-10-06 08:11:49 +0100698 ret = rxrpc_send_abort_packet(call);
David Howells3ab26a62017-06-07 14:41:52 +0100699 } else if (p.command != RXRPC_CMD_SEND_DATA) {
David Howellsdf423a42016-09-02 22:39:45 +0100700 ret = -EINVAL;
701 } else if (rxrpc_is_client_call(call) &&
David Howells146d8fe2017-03-04 00:01:41 +0000702 state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
David Howellsdf423a42016-09-02 22:39:45 +0100703 /* request phase complete for this client call */
704 ret = -EPROTO;
705 } else if (rxrpc_is_service_call(call) &&
David Howells146d8fe2017-03-04 00:01:41 +0000706 state != RXRPC_CALL_SERVER_ACK_REQUEST &&
707 state != RXRPC_CALL_SERVER_SEND_REPLY) {
David Howellsdf423a42016-09-02 22:39:45 +0100708 /* Reply phase not begun or not complete for service call. */
709 ret = -EPROTO;
710 } else {
David Howellse8332512017-08-29 10:18:56 +0100711 ret = rxrpc_send_data(rx, call, msg, len, NULL);
David Howellsdf423a42016-09-02 22:39:45 +0100712 }
713
David Howells03a6c822017-11-24 10:18:40 +0000714out_put_unlock:
David Howells540b1c42017-02-27 15:43:06 +0000715 mutex_unlock(&call->user_mutex);
716error_put:
David Howellsfff724292016-09-07 14:34:21 +0100717 rxrpc_put_call(call, rxrpc_call_put);
David Howellsdf423a42016-09-02 22:39:45 +0100718 _leave(" = %d", ret);
719 return ret;
David Howells540b1c42017-02-27 15:43:06 +0000720
721error_release_sock:
722 release_sock(&rx->sk);
723 return ret;
David Howellsdf423a42016-09-02 22:39:45 +0100724}
725
726/**
727 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
728 * @sock: The socket the call is on
729 * @call: The call to send data through
730 * @msg: The data to send
731 * @len: The amount of data to send
David Howellse8332512017-08-29 10:18:56 +0100732 * @notify_end_tx: Notification that the last packet is queued.
David Howellsdf423a42016-09-02 22:39:45 +0100733 *
734 * Allow a kernel service to send data on a call. The call must be in an state
735 * appropriate to sending data. No control data should be supplied in @msg,
736 * nor should an address be supplied. MSG_MORE should be flagged if there's
737 * more data to come, otherwise this data will end the transmission phase.
738 */
739int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
David Howellse8332512017-08-29 10:18:56 +0100740 struct msghdr *msg, size_t len,
741 rxrpc_notify_end_tx_t notify_end_tx)
David Howellsdf423a42016-09-02 22:39:45 +0100742{
743 int ret;
744
745 _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
746
747 ASSERTCMP(msg->msg_name, ==, NULL);
748 ASSERTCMP(msg->msg_control, ==, NULL);
749
David Howells540b1c42017-02-27 15:43:06 +0000750 mutex_lock(&call->user_mutex);
David Howellsdf423a42016-09-02 22:39:45 +0100751
752 _debug("CALL %d USR %lx ST %d on CONN %p",
753 call->debug_id, call->user_call_ID, call->state, call->conn);
754
David Howells146d8fe2017-03-04 00:01:41 +0000755 switch (READ_ONCE(call->state)) {
756 case RXRPC_CALL_CLIENT_SEND_REQUEST:
757 case RXRPC_CALL_SERVER_ACK_REQUEST:
758 case RXRPC_CALL_SERVER_SEND_REPLY:
David Howellse8332512017-08-29 10:18:56 +0100759 ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len,
760 notify_end_tx);
David Howells146d8fe2017-03-04 00:01:41 +0000761 break;
762 case RXRPC_CALL_COMPLETE:
David Howells6fc166d2017-03-09 08:10:32 +0000763 read_lock_bh(&call->state_lock);
David Howellsbd2db2d2017-08-29 10:18:43 +0100764 ret = call->error;
David Howells6fc166d2017-03-09 08:10:32 +0000765 read_unlock_bh(&call->state_lock);
David Howells146d8fe2017-03-04 00:01:41 +0000766 break;
767 default:
David Howellsfb46f6e2017-04-06 10:12:00 +0100768 /* Request phase complete for this client call */
769 trace_rxrpc_rx_eproto(call, 0, tracepoint_string("late_send"));
David Howells146d8fe2017-03-04 00:01:41 +0000770 ret = -EPROTO;
771 break;
David Howellsdf423a42016-09-02 22:39:45 +0100772 }
773
David Howells540b1c42017-02-27 15:43:06 +0000774 mutex_unlock(&call->user_mutex);
David Howellsdf423a42016-09-02 22:39:45 +0100775 _leave(" = %d", ret);
776 return ret;
777}
778EXPORT_SYMBOL(rxrpc_kernel_send_data);
779
780/**
781 * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
782 * @sock: The socket the call is on
783 * @call: The call to be aborted
784 * @abort_code: The abort code to stick into the ABORT packet
David Howells5a429762016-09-06 22:19:51 +0100785 * @error: Local error value
786 * @why: 3-char string indicating why.
David Howellsdf423a42016-09-02 22:39:45 +0100787 *
David Howells84a4c092017-04-06 10:11:59 +0100788 * Allow a kernel service to abort a call, if it's still in an abortable state
789 * and return true if the call was aborted, false if it was already complete.
David Howellsdf423a42016-09-02 22:39:45 +0100790 */
David Howells84a4c092017-04-06 10:11:59 +0100791bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
David Howells5a429762016-09-06 22:19:51 +0100792 u32 abort_code, int error, const char *why)
David Howellsdf423a42016-09-02 22:39:45 +0100793{
David Howells84a4c092017-04-06 10:11:59 +0100794 bool aborted;
795
David Howells5a429762016-09-06 22:19:51 +0100796 _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why);
David Howellsdf423a42016-09-02 22:39:45 +0100797
David Howells540b1c42017-02-27 15:43:06 +0000798 mutex_lock(&call->user_mutex);
David Howellsdf423a42016-09-02 22:39:45 +0100799
David Howells84a4c092017-04-06 10:11:59 +0100800 aborted = rxrpc_abort_call(why, call, 0, abort_code, error);
801 if (aborted)
David Howells26cb02a2016-10-06 08:11:49 +0100802 rxrpc_send_abort_packet(call);
David Howellsdf423a42016-09-02 22:39:45 +0100803
David Howells540b1c42017-02-27 15:43:06 +0000804 mutex_unlock(&call->user_mutex);
David Howells84a4c092017-04-06 10:11:59 +0100805 return aborted;
David Howellsdf423a42016-09-02 22:39:45 +0100806}
David Howellsdf423a42016-09-02 22:39:45 +0100807EXPORT_SYMBOL(rxrpc_kernel_abort_call);
David Howellse754eba2017-06-07 12:40:03 +0100808
809/**
810 * rxrpc_kernel_set_tx_length - Set the total Tx length on a call
811 * @sock: The socket the call is on
812 * @call: The call to be informed
813 * @tx_total_len: The amount of data to be transmitted for this call
814 *
815 * Allow a kernel service to set the total transmit length on a call. This
816 * allows buffer-to-packet encrypt-and-copy to be performed.
817 *
818 * This function is primarily for use for setting the reply length since the
819 * request length can be set when beginning the call.
820 */
821void rxrpc_kernel_set_tx_length(struct socket *sock, struct rxrpc_call *call,
822 s64 tx_total_len)
823{
824 WARN_ON(call->tx_total_len != -1);
825 call->tx_total_len = tx_total_len;
826}
827EXPORT_SYMBOL(rxrpc_kernel_set_tx_length);