blob: 803078bea507e4559a0a998d015357070af997e7 [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>
18#include <linux/circ_buf.h>
19#include <net/sock.h>
20#include <net/af_rxrpc.h>
21#include "ar-internal.h"
22
David Howells3dc20f02016-09-04 13:25:21 +010023enum rxrpc_command {
24 RXRPC_CMD_SEND_DATA, /* send data message */
25 RXRPC_CMD_SEND_ABORT, /* request abort generation */
26 RXRPC_CMD_ACCEPT, /* [server] accept incoming call */
27 RXRPC_CMD_REJECT_BUSY, /* [server] reject a call as busy */
28};
29
David Howells0b58b8a2016-09-02 22:39:45 +010030/*
31 * wait for space to appear in the transmit/ACK window
32 * - caller holds the socket locked
33 */
34static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
35 struct rxrpc_call *call,
36 long *timeo)
37{
38 DECLARE_WAITQUEUE(myself, current);
39 int ret;
40
41 _enter(",{%d},%ld",
42 CIRC_SPACE(call->acks_head, ACCESS_ONCE(call->acks_tail),
43 call->acks_winsz),
44 *timeo);
45
46 add_wait_queue(&call->waitq, &myself);
47
48 for (;;) {
49 set_current_state(TASK_INTERRUPTIBLE);
50 ret = 0;
51 if (CIRC_SPACE(call->acks_head, ACCESS_ONCE(call->acks_tail),
52 call->acks_winsz) > 0)
53 break;
54 if (signal_pending(current)) {
55 ret = sock_intr_errno(*timeo);
56 break;
57 }
58
59 release_sock(&rx->sk);
60 *timeo = schedule_timeout(*timeo);
61 lock_sock(&rx->sk);
62 }
63
64 remove_wait_queue(&call->waitq, &myself);
65 set_current_state(TASK_RUNNING);
66 _leave(" = %d", ret);
67 return ret;
68}
69
70/*
71 * attempt to schedule an instant Tx resend
72 */
73static inline void rxrpc_instant_resend(struct rxrpc_call *call)
74{
75 read_lock_bh(&call->state_lock);
76 if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
77 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
78 if (call->state < RXRPC_CALL_COMPLETE &&
79 !test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
80 rxrpc_queue_call(call);
81 }
82 read_unlock_bh(&call->state_lock);
83}
84
85/*
86 * queue a packet for transmission, set the resend timer and attempt
87 * to send the packet immediately
88 */
89static void rxrpc_queue_packet(struct rxrpc_call *call, struct sk_buff *skb,
90 bool last)
91{
92 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
93 int ret;
94
95 _net("queue skb %p [%d]", skb, call->acks_head);
96
97 ASSERT(call->acks_window != NULL);
98 call->acks_window[call->acks_head] = (unsigned long) skb;
99 smp_wmb();
100 call->acks_head = (call->acks_head + 1) & (call->acks_winsz - 1);
101
102 if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
103 _debug("________awaiting reply/ACK__________");
104 write_lock_bh(&call->state_lock);
105 switch (call->state) {
106 case RXRPC_CALL_CLIENT_SEND_REQUEST:
107 call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
108 break;
109 case RXRPC_CALL_SERVER_ACK_REQUEST:
110 call->state = RXRPC_CALL_SERVER_SEND_REPLY;
111 if (!last)
112 break;
113 case RXRPC_CALL_SERVER_SEND_REPLY:
114 call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
115 break;
116 default:
117 break;
118 }
119 write_unlock_bh(&call->state_lock);
120 }
121
122 _proto("Tx DATA %%%u { #%u }", sp->hdr.serial, sp->hdr.seq);
123
124 sp->need_resend = false;
125 sp->resend_at = jiffies + rxrpc_resend_timeout;
126 if (!test_and_set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags)) {
127 _debug("run timer");
128 call->resend_timer.expires = sp->resend_at;
129 add_timer(&call->resend_timer);
130 }
131
132 /* attempt to cancel the rx-ACK timer, deferring reply transmission if
133 * we're ACK'ing the request phase of an incoming call */
134 ret = -EAGAIN;
135 if (try_to_del_timer_sync(&call->ack_timer) >= 0) {
136 /* the packet may be freed by rxrpc_process_call() before this
137 * returns */
138 if (rxrpc_is_client_call(call))
139 rxrpc_expose_client_call(call);
140 ret = rxrpc_send_data_packet(call->conn, skb);
141 _net("sent skb %p", skb);
142 } else {
143 _debug("failed to delete ACK timer");
144 }
145
146 if (ret < 0) {
147 _debug("need instant resend %d", ret);
148 sp->need_resend = true;
149 rxrpc_instant_resend(call);
150 }
151
152 _leave("");
153}
154
155/*
156 * Convert a host-endian header into a network-endian header.
157 */
158static void rxrpc_insert_header(struct sk_buff *skb)
159{
160 struct rxrpc_wire_header whdr;
161 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
162
163 whdr.epoch = htonl(sp->hdr.epoch);
164 whdr.cid = htonl(sp->hdr.cid);
165 whdr.callNumber = htonl(sp->hdr.callNumber);
166 whdr.seq = htonl(sp->hdr.seq);
167 whdr.serial = htonl(sp->hdr.serial);
168 whdr.type = sp->hdr.type;
169 whdr.flags = sp->hdr.flags;
170 whdr.userStatus = sp->hdr.userStatus;
171 whdr.securityIndex = sp->hdr.securityIndex;
172 whdr._rsvd = htons(sp->hdr._rsvd);
173 whdr.serviceId = htons(sp->hdr.serviceId);
174
175 memcpy(skb->head, &whdr, sizeof(whdr));
176}
177
178/*
179 * send data through a socket
180 * - must be called in process context
181 * - caller holds the socket locked
182 */
183static int rxrpc_send_data(struct rxrpc_sock *rx,
184 struct rxrpc_call *call,
185 struct msghdr *msg, size_t len)
186{
187 struct rxrpc_skb_priv *sp;
188 struct sk_buff *skb;
189 struct sock *sk = &rx->sk;
190 long timeo;
191 bool more;
192 int ret, copied;
193
194 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
195
196 /* this should be in poll */
197 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
198
199 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
200 return -EPIPE;
201
202 more = msg->msg_flags & MSG_MORE;
203
204 skb = call->tx_pending;
205 call->tx_pending = NULL;
206 rxrpc_see_skb(skb);
207
208 copied = 0;
209 do {
210 if (!skb) {
211 size_t size, chunk, max, space;
212
213 _debug("alloc");
214
215 if (CIRC_SPACE(call->acks_head,
216 ACCESS_ONCE(call->acks_tail),
217 call->acks_winsz) <= 0) {
218 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
227 max = call->conn->params.peer->maxdata;
228 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
238 size = space + call->conn->header_size;
239
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
248 rxrpc_new_skb(skb);
249
250 _debug("ALLOC SEND %p", skb);
251
252 ASSERTCMP(skb->mark, ==, 0);
253
254 _debug("HS: %u", call->conn->header_size);
255 skb_reserve(skb, call->conn->header_size);
256 skb->len += call->conn->header_size;
257
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
316 seq = atomic_inc_return(&call->sequence);
317
318 sp->hdr.epoch = conn->proto.epoch;
319 sp->hdr.cid = call->cid;
320 sp->hdr.callNumber = call->call_id;
321 sp->hdr.seq = seq;
322 sp->hdr.serial = atomic_inc_return(&conn->serial);
323 sp->hdr.type = RXRPC_PACKET_TYPE_DATA;
324 sp->hdr.userStatus = 0;
325 sp->hdr.securityIndex = conn->security_ix;
326 sp->hdr._rsvd = 0;
327 sp->hdr.serviceId = call->service_id;
328
329 sp->hdr.flags = conn->out_clientflag;
330 if (msg_data_left(msg) == 0 && !more)
331 sp->hdr.flags |= RXRPC_LAST_PACKET;
332 else if (CIRC_SPACE(call->acks_head,
333 ACCESS_ONCE(call->acks_tail),
334 call->acks_winsz) > 1)
335 sp->hdr.flags |= RXRPC_MORE_PACKETS;
336 if (more && seq & 1)
337 sp->hdr.flags |= RXRPC_REQUEST_ACK;
338
339 ret = conn->security->secure_packet(
340 call, skb, skb->mark,
341 skb->head + sizeof(struct rxrpc_wire_header));
342 if (ret < 0)
343 goto out;
344
345 rxrpc_insert_header(skb);
346 rxrpc_queue_packet(call, skb, !msg_data_left(msg) && !more);
347 skb = NULL;
348 }
349 } while (msg_data_left(msg) > 0);
350
351success:
352 ret = copied;
353out:
354 call->tx_pending = skb;
355 _leave(" = %d", ret);
356 return ret;
357
358call_terminated:
359 rxrpc_free_skb(skb);
360 _leave(" = %d", -call->error);
361 return ret;
362
363maybe_error:
364 if (copied)
365 goto success;
366 goto out;
367
368efault:
369 ret = -EFAULT;
370 goto out;
371}
David Howellsdf423a42016-09-02 22:39:45 +0100372
373/*
374 * extract control messages from the sendmsg() control buffer
375 */
376static int rxrpc_sendmsg_cmsg(struct msghdr *msg,
377 unsigned long *user_call_ID,
378 enum rxrpc_command *command,
379 u32 *abort_code,
380 bool *_exclusive)
381{
382 struct cmsghdr *cmsg;
383 bool got_user_ID = false;
384 int len;
385
386 *command = RXRPC_CMD_SEND_DATA;
387
388 if (msg->msg_controllen == 0)
389 return -EINVAL;
390
391 for_each_cmsghdr(cmsg, msg) {
392 if (!CMSG_OK(msg, cmsg))
393 return -EINVAL;
394
395 len = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
396 _debug("CMSG %d, %d, %d",
397 cmsg->cmsg_level, cmsg->cmsg_type, len);
398
399 if (cmsg->cmsg_level != SOL_RXRPC)
400 continue;
401
402 switch (cmsg->cmsg_type) {
403 case RXRPC_USER_CALL_ID:
404 if (msg->msg_flags & MSG_CMSG_COMPAT) {
405 if (len != sizeof(u32))
406 return -EINVAL;
407 *user_call_ID = *(u32 *) CMSG_DATA(cmsg);
408 } else {
409 if (len != sizeof(unsigned long))
410 return -EINVAL;
411 *user_call_ID = *(unsigned long *)
412 CMSG_DATA(cmsg);
413 }
414 _debug("User Call ID %lx", *user_call_ID);
415 got_user_ID = true;
416 break;
417
418 case RXRPC_ABORT:
419 if (*command != RXRPC_CMD_SEND_DATA)
420 return -EINVAL;
421 *command = RXRPC_CMD_SEND_ABORT;
422 if (len != sizeof(*abort_code))
423 return -EINVAL;
424 *abort_code = *(unsigned int *) CMSG_DATA(cmsg);
425 _debug("Abort %x", *abort_code);
426 if (*abort_code == 0)
427 return -EINVAL;
428 break;
429
430 case RXRPC_ACCEPT:
431 if (*command != RXRPC_CMD_SEND_DATA)
432 return -EINVAL;
433 *command = RXRPC_CMD_ACCEPT;
434 if (len != 0)
435 return -EINVAL;
436 break;
437
438 case RXRPC_EXCLUSIVE_CALL:
439 *_exclusive = true;
440 if (len != 0)
441 return -EINVAL;
442 break;
443 default:
444 return -EINVAL;
445 }
446 }
447
448 if (!got_user_ID)
449 return -EINVAL;
450 _leave(" = 0");
451 return 0;
452}
453
454/*
455 * abort a call, sending an ABORT packet to the peer
456 */
457static void rxrpc_send_abort(struct rxrpc_call *call, u32 abort_code)
458{
459 if (call->state >= RXRPC_CALL_COMPLETE)
460 return;
461
462 write_lock_bh(&call->state_lock);
463
464 if (__rxrpc_abort_call(call, abort_code, ECONNABORTED)) {
465 del_timer_sync(&call->resend_timer);
466 del_timer_sync(&call->ack_timer);
467 clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
468 clear_bit(RXRPC_CALL_EV_ACK, &call->events);
469 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
470 rxrpc_queue_call(call);
471 }
472
473 write_unlock_bh(&call->state_lock);
474}
475
476/*
477 * Create a new client call for sendmsg().
478 */
479static struct rxrpc_call *
480rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
481 unsigned long user_call_ID, bool exclusive)
482{
483 struct rxrpc_conn_parameters cp;
484 struct rxrpc_call *call;
485 struct key *key;
486
487 DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
488
489 _enter("");
490
491 if (!msg->msg_name)
492 return ERR_PTR(-EDESTADDRREQ);
493
494 key = rx->key;
495 if (key && !rx->key->payload.data[0])
496 key = NULL;
497
498 memset(&cp, 0, sizeof(cp));
499 cp.local = rx->local;
500 cp.key = rx->key;
501 cp.security_level = rx->min_sec_level;
502 cp.exclusive = rx->exclusive | exclusive;
503 cp.service_id = srx->srx_service;
504 call = rxrpc_new_client_call(rx, &cp, srx, user_call_ID, GFP_KERNEL);
505
506 _leave(" = %p\n", call);
507 return call;
508}
509
510/*
511 * send a message forming part of a client call through an RxRPC socket
512 * - caller holds the socket locked
513 * - the socket may be either a client socket or a server socket
514 */
515int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
516{
517 enum rxrpc_command cmd;
518 struct rxrpc_call *call;
519 unsigned long user_call_ID = 0;
520 bool exclusive = false;
521 u32 abort_code = 0;
522 int ret;
523
524 _enter("");
525
526 ret = rxrpc_sendmsg_cmsg(msg, &user_call_ID, &cmd, &abort_code,
527 &exclusive);
528 if (ret < 0)
529 return ret;
530
531 if (cmd == RXRPC_CMD_ACCEPT) {
532 if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
533 return -EINVAL;
534 call = rxrpc_accept_call(rx, user_call_ID, NULL);
535 if (IS_ERR(call))
536 return PTR_ERR(call);
David Howellsfff724292016-09-07 14:34:21 +0100537 rxrpc_put_call(call, rxrpc_call_put);
David Howellsdf423a42016-09-02 22:39:45 +0100538 return 0;
539 }
540
541 call = rxrpc_find_call_by_user_ID(rx, user_call_ID);
542 if (!call) {
543 if (cmd != RXRPC_CMD_SEND_DATA)
544 return -EBADSLT;
545 call = rxrpc_new_client_call_for_sendmsg(rx, msg, user_call_ID,
546 exclusive);
547 if (IS_ERR(call))
548 return PTR_ERR(call);
549 }
550
551 rxrpc_see_call(call);
552 _debug("CALL %d USR %lx ST %d on CONN %p",
553 call->debug_id, call->user_call_ID, call->state, call->conn);
554
555 if (call->state >= RXRPC_CALL_COMPLETE) {
556 /* it's too late for this call */
557 ret = -ESHUTDOWN;
558 } else if (cmd == RXRPC_CMD_SEND_ABORT) {
559 rxrpc_send_abort(call, abort_code);
560 ret = 0;
561 } else if (cmd != RXRPC_CMD_SEND_DATA) {
562 ret = -EINVAL;
563 } else if (rxrpc_is_client_call(call) &&
564 call->state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
565 /* request phase complete for this client call */
566 ret = -EPROTO;
567 } else if (rxrpc_is_service_call(call) &&
568 call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
569 call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
570 /* Reply phase not begun or not complete for service call. */
571 ret = -EPROTO;
572 } else {
573 ret = rxrpc_send_data(rx, call, msg, len);
574 }
575
David Howellsfff724292016-09-07 14:34:21 +0100576 rxrpc_put_call(call, rxrpc_call_put);
David Howellsdf423a42016-09-02 22:39:45 +0100577 _leave(" = %d", ret);
578 return ret;
579}
580
581/**
582 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
583 * @sock: The socket the call is on
584 * @call: The call to send data through
585 * @msg: The data to send
586 * @len: The amount of data to send
587 *
588 * Allow a kernel service to send data on a call. The call must be in an state
589 * appropriate to sending data. No control data should be supplied in @msg,
590 * nor should an address be supplied. MSG_MORE should be flagged if there's
591 * more data to come, otherwise this data will end the transmission phase.
592 */
593int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
594 struct msghdr *msg, size_t len)
595{
596 int ret;
597
598 _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
599
600 ASSERTCMP(msg->msg_name, ==, NULL);
601 ASSERTCMP(msg->msg_control, ==, NULL);
602
603 lock_sock(sock->sk);
604
605 _debug("CALL %d USR %lx ST %d on CONN %p",
606 call->debug_id, call->user_call_ID, call->state, call->conn);
607
608 if (call->state >= RXRPC_CALL_COMPLETE) {
609 ret = -ESHUTDOWN; /* it's too late for this call */
610 } else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
611 call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
612 call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
613 ret = -EPROTO; /* request phase complete for this client call */
614 } else {
615 ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len);
616 }
617
618 release_sock(sock->sk);
619 _leave(" = %d", ret);
620 return ret;
621}
622EXPORT_SYMBOL(rxrpc_kernel_send_data);
623
624/**
625 * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
626 * @sock: The socket the call is on
627 * @call: The call to be aborted
628 * @abort_code: The abort code to stick into the ABORT packet
629 *
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,
633 u32 abort_code)
634{
635 _enter("{%d},%d", call->debug_id, abort_code);
636
637 lock_sock(sock->sk);
638
639 _debug("CALL %d USR %lx ST %d on CONN %p",
640 call->debug_id, call->user_call_ID, call->state, call->conn);
641
642 rxrpc_send_abort(call, abort_code);
643
644 release_sock(sock->sk);
645 _leave("");
646}
647
648EXPORT_SYMBOL(rxrpc_kernel_abort_call);