root/net/rxrpc/sendmsg.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. rxrpc_check_tx_space
  2. rxrpc_wait_for_tx_window_intr
  3. rxrpc_wait_for_tx_window_waitall
  4. rxrpc_wait_for_tx_window_nonintr
  5. rxrpc_wait_for_tx_window
  6. rxrpc_instant_resend
  7. rxrpc_notify_end_tx
  8. rxrpc_queue_packet
  9. rxrpc_send_data
  10. rxrpc_sendmsg_cmsg
  11. rxrpc_new_client_call_for_sendmsg
  12. rxrpc_do_sendmsg
  13. rxrpc_kernel_send_data
  14. rxrpc_kernel_abort_call
  15. rxrpc_kernel_set_tx_length

   1 // SPDX-License-Identifier: GPL-2.0-or-later
   2 /* AF_RXRPC sendmsg() implementation.
   3  *
   4  * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
   5  * Written by David Howells (dhowells@redhat.com)
   6  */
   7 
   8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
   9 
  10 #include <linux/net.h>
  11 #include <linux/gfp.h>
  12 #include <linux/skbuff.h>
  13 #include <linux/export.h>
  14 #include <linux/sched/signal.h>
  15 
  16 #include <net/sock.h>
  17 #include <net/af_rxrpc.h>
  18 #include "ar-internal.h"
  19 
  20 /*
  21  * Return true if there's sufficient Tx queue space.
  22  */
  23 static bool rxrpc_check_tx_space(struct rxrpc_call *call, rxrpc_seq_t *_tx_win)
  24 {
  25         unsigned int win_size =
  26                 min_t(unsigned int, call->tx_winsize,
  27                       call->cong_cwnd + call->cong_extra);
  28         rxrpc_seq_t tx_win = READ_ONCE(call->tx_hard_ack);
  29 
  30         if (_tx_win)
  31                 *_tx_win = tx_win;
  32         return call->tx_top - tx_win < win_size;
  33 }
  34 
  35 /*
  36  * Wait for space to appear in the Tx queue or a signal to occur.
  37  */
  38 static int rxrpc_wait_for_tx_window_intr(struct rxrpc_sock *rx,
  39                                          struct rxrpc_call *call,
  40                                          long *timeo)
  41 {
  42         for (;;) {
  43                 set_current_state(TASK_INTERRUPTIBLE);
  44                 if (rxrpc_check_tx_space(call, NULL))
  45                         return 0;
  46 
  47                 if (call->state >= RXRPC_CALL_COMPLETE)
  48                         return call->error;
  49 
  50                 if (signal_pending(current))
  51                         return sock_intr_errno(*timeo);
  52 
  53                 trace_rxrpc_transmit(call, rxrpc_transmit_wait);
  54                 mutex_unlock(&call->user_mutex);
  55                 *timeo = schedule_timeout(*timeo);
  56                 if (mutex_lock_interruptible(&call->user_mutex) < 0)
  57                         return sock_intr_errno(*timeo);
  58         }
  59 }
  60 
  61 /*
  62  * Wait for space to appear in the Tx queue uninterruptibly, but with
  63  * a timeout of 2*RTT if no progress was made and a signal occurred.
  64  */
  65 static int rxrpc_wait_for_tx_window_waitall(struct rxrpc_sock *rx,
  66                                             struct rxrpc_call *call)
  67 {
  68         rxrpc_seq_t tx_start, tx_win;
  69         signed long rtt, timeout;
  70 
  71         rtt = READ_ONCE(call->peer->srtt_us) >> 3;
  72         rtt = usecs_to_jiffies(rtt) * 2;
  73         if (rtt < 2)
  74                 rtt = 2;
  75 
  76         timeout = rtt;
  77         tx_start = READ_ONCE(call->tx_hard_ack);
  78 
  79         for (;;) {
  80                 set_current_state(TASK_UNINTERRUPTIBLE);
  81 
  82                 tx_win = READ_ONCE(call->tx_hard_ack);
  83                 if (rxrpc_check_tx_space(call, &tx_win))
  84                         return 0;
  85 
  86                 if (call->state >= RXRPC_CALL_COMPLETE)
  87                         return call->error;
  88 
  89                 if (timeout == 0 &&
  90                     tx_win == tx_start && signal_pending(current))
  91                         return -EINTR;
  92 
  93                 if (tx_win != tx_start) {
  94                         timeout = rtt;
  95                         tx_start = tx_win;
  96                 }
  97 
  98                 trace_rxrpc_transmit(call, rxrpc_transmit_wait);
  99                 timeout = schedule_timeout(timeout);
 100         }
 101 }
 102 
 103 /*
 104  * Wait for space to appear in the Tx queue uninterruptibly.
 105  */
 106 static int rxrpc_wait_for_tx_window_nonintr(struct rxrpc_sock *rx,
 107                                             struct rxrpc_call *call,
 108                                             long *timeo)
 109 {
 110         for (;;) {
 111                 set_current_state(TASK_UNINTERRUPTIBLE);
 112                 if (rxrpc_check_tx_space(call, NULL))
 113                         return 0;
 114 
 115                 if (call->state >= RXRPC_CALL_COMPLETE)
 116                         return call->error;
 117 
 118                 trace_rxrpc_transmit(call, rxrpc_transmit_wait);
 119                 *timeo = schedule_timeout(*timeo);
 120         }
 121 }
 122 
 123 /*
 124  * wait for space to appear in the transmit/ACK window
 125  * - caller holds the socket locked
 126  */
 127 static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
 128                                     struct rxrpc_call *call,
 129                                     long *timeo,
 130                                     bool waitall)
 131 {
 132         DECLARE_WAITQUEUE(myself, current);
 133         int ret;
 134 
 135         _enter(",{%u,%u,%u}",
 136                call->tx_hard_ack, call->tx_top, call->tx_winsize);
 137 
 138         add_wait_queue(&call->waitq, &myself);
 139 
 140         switch (call->interruptibility) {
 141         case RXRPC_INTERRUPTIBLE:
 142                 if (waitall)
 143                         ret = rxrpc_wait_for_tx_window_waitall(rx, call);
 144                 else
 145                         ret = rxrpc_wait_for_tx_window_intr(rx, call, timeo);
 146                 break;
 147         case RXRPC_PREINTERRUPTIBLE:
 148         case RXRPC_UNINTERRUPTIBLE:
 149         default:
 150                 ret = rxrpc_wait_for_tx_window_nonintr(rx, call, timeo);
 151                 break;
 152         }
 153 
 154         remove_wait_queue(&call->waitq, &myself);
 155         set_current_state(TASK_RUNNING);
 156         _leave(" = %d", ret);
 157         return ret;
 158 }
 159 
 160 /*
 161  * Schedule an instant Tx resend.
 162  */
 163 static inline void rxrpc_instant_resend(struct rxrpc_call *call, int ix)
 164 {
 165         spin_lock_bh(&call->lock);
 166 
 167         if (call->state < RXRPC_CALL_COMPLETE) {
 168                 call->rxtx_annotations[ix] =
 169                         (call->rxtx_annotations[ix] & RXRPC_TX_ANNO_LAST) |
 170                         RXRPC_TX_ANNO_RETRANS;
 171                 if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
 172                         rxrpc_queue_call(call);
 173         }
 174 
 175         spin_unlock_bh(&call->lock);
 176 }
 177 
 178 /*
 179  * Notify the owner of the call that the transmit phase is ended and the last
 180  * packet has been queued.
 181  */
 182 static void rxrpc_notify_end_tx(struct rxrpc_sock *rx, struct rxrpc_call *call,
 183                                 rxrpc_notify_end_tx_t notify_end_tx)
 184 {
 185         if (notify_end_tx)
 186                 notify_end_tx(&rx->sk, call, call->user_call_ID);
 187 }
 188 
 189 /*
 190  * Queue a DATA packet for transmission, set the resend timeout and send
 191  * the packet immediately.  Returns the error from rxrpc_send_data_packet()
 192  * in case the caller wants to do something with it.
 193  */
 194 static int rxrpc_queue_packet(struct rxrpc_sock *rx, struct rxrpc_call *call,
 195                               struct sk_buff *skb, bool last,
 196                               rxrpc_notify_end_tx_t notify_end_tx)
 197 {
 198         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
 199         unsigned long now;
 200         rxrpc_seq_t seq = sp->hdr.seq;
 201         int ret, ix;
 202         u8 annotation = RXRPC_TX_ANNO_UNACK;
 203 
 204         _net("queue skb %p [%d]", skb, seq);
 205 
 206         ASSERTCMP(seq, ==, call->tx_top + 1);
 207 
 208         if (last)
 209                 annotation |= RXRPC_TX_ANNO_LAST;
 210 
 211         /* We have to set the timestamp before queueing as the retransmit
 212          * algorithm can see the packet as soon as we queue it.
 213          */
 214         skb->tstamp = ktime_get_real();
 215 
 216         ix = seq & RXRPC_RXTX_BUFF_MASK;
 217         rxrpc_get_skb(skb, rxrpc_skb_got);
 218         call->rxtx_annotations[ix] = annotation;
 219         smp_wmb();
 220         call->rxtx_buffer[ix] = skb;
 221         call->tx_top = seq;
 222         if (last)
 223                 trace_rxrpc_transmit(call, rxrpc_transmit_queue_last);
 224         else
 225                 trace_rxrpc_transmit(call, rxrpc_transmit_queue);
 226 
 227         if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
 228                 _debug("________awaiting reply/ACK__________");
 229                 write_lock_bh(&call->state_lock);
 230                 switch (call->state) {
 231                 case RXRPC_CALL_CLIENT_SEND_REQUEST:
 232                         call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
 233                         rxrpc_notify_end_tx(rx, call, notify_end_tx);
 234                         break;
 235                 case RXRPC_CALL_SERVER_ACK_REQUEST:
 236                         call->state = RXRPC_CALL_SERVER_SEND_REPLY;
 237                         now = jiffies;
 238                         WRITE_ONCE(call->ack_at, now + MAX_JIFFY_OFFSET);
 239                         if (call->ackr_reason == RXRPC_ACK_DELAY)
 240                                 call->ackr_reason = 0;
 241                         trace_rxrpc_timer(call, rxrpc_timer_init_for_send_reply, now);
 242                         if (!last)
 243                                 break;
 244                         /* Fall through */
 245                 case RXRPC_CALL_SERVER_SEND_REPLY:
 246                         call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
 247                         rxrpc_notify_end_tx(rx, call, notify_end_tx);
 248                         break;
 249                 default:
 250                         break;
 251                 }
 252                 write_unlock_bh(&call->state_lock);
 253         }
 254 
 255         if (seq == 1 && rxrpc_is_client_call(call))
 256                 rxrpc_expose_client_call(call);
 257 
 258         ret = rxrpc_send_data_packet(call, skb, false);
 259         if (ret < 0) {
 260                 switch (ret) {
 261                 case -ENETUNREACH:
 262                 case -EHOSTUNREACH:
 263                 case -ECONNREFUSED:
 264                         rxrpc_set_call_completion(call,
 265                                                   RXRPC_CALL_LOCAL_ERROR,
 266                                                   0, ret);
 267                         rxrpc_notify_socket(call);
 268                         goto out;
 269                 }
 270                 _debug("need instant resend %d", ret);
 271                 rxrpc_instant_resend(call, ix);
 272         } else {
 273                 unsigned long now = jiffies;
 274                 unsigned long resend_at = now + call->peer->rto_j;
 275 
 276                 WRITE_ONCE(call->resend_at, resend_at);
 277                 rxrpc_reduce_call_timer(call, resend_at, now,
 278                                         rxrpc_timer_set_for_send);
 279         }
 280 
 281 out:
 282         rxrpc_free_skb(skb, rxrpc_skb_freed);
 283         _leave(" = %d", ret);
 284         return ret;
 285 }
 286 
 287 /*
 288  * send data through a socket
 289  * - must be called in process context
 290  * - The caller holds the call user access mutex, but not the socket lock.
 291  */
 292 static int rxrpc_send_data(struct rxrpc_sock *rx,
 293                            struct rxrpc_call *call,
 294                            struct msghdr *msg, size_t len,
 295                            rxrpc_notify_end_tx_t notify_end_tx)
 296 {
 297         struct rxrpc_skb_priv *sp;
 298         struct sk_buff *skb;
 299         struct sock *sk = &rx->sk;
 300         long timeo;
 301         bool more;
 302         int ret, copied;
 303 
 304         timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
 305 
 306         /* this should be in poll */
 307         sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
 308 
 309         if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
 310                 return -EPIPE;
 311 
 312         more = msg->msg_flags & MSG_MORE;
 313 
 314         if (call->tx_total_len != -1) {
 315                 if (len > call->tx_total_len)
 316                         return -EMSGSIZE;
 317                 if (!more && len != call->tx_total_len)
 318                         return -EMSGSIZE;
 319         }
 320 
 321         skb = call->tx_pending;
 322         call->tx_pending = NULL;
 323         rxrpc_see_skb(skb, rxrpc_skb_seen);
 324 
 325         copied = 0;
 326         do {
 327                 /* Check to see if there's a ping ACK to reply to. */
 328                 if (call->ackr_reason == RXRPC_ACK_PING_RESPONSE)
 329                         rxrpc_send_ack_packet(call, false, NULL);
 330 
 331                 if (!skb) {
 332                         size_t size, chunk, max, space;
 333 
 334                         _debug("alloc");
 335 
 336                         if (!rxrpc_check_tx_space(call, NULL)) {
 337                                 ret = -EAGAIN;
 338                                 if (msg->msg_flags & MSG_DONTWAIT)
 339                                         goto maybe_error;
 340                                 ret = rxrpc_wait_for_tx_window(rx, call,
 341                                                                &timeo,
 342                                                                msg->msg_flags & MSG_WAITALL);
 343                                 if (ret < 0)
 344                                         goto maybe_error;
 345                         }
 346 
 347                         max = RXRPC_JUMBO_DATALEN;
 348                         max -= call->conn->security_size;
 349                         max &= ~(call->conn->size_align - 1UL);
 350 
 351                         chunk = max;
 352                         if (chunk > msg_data_left(msg) && !more)
 353                                 chunk = msg_data_left(msg);
 354 
 355                         space = chunk + call->conn->size_align;
 356                         space &= ~(call->conn->size_align - 1UL);
 357 
 358                         size = space + call->conn->security_size;
 359 
 360                         _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
 361 
 362                         /* create a buffer that we can retain until it's ACK'd */
 363                         skb = sock_alloc_send_skb(
 364                                 sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
 365                         if (!skb)
 366                                 goto maybe_error;
 367 
 368                         sp = rxrpc_skb(skb);
 369                         sp->rx_flags |= RXRPC_SKB_TX_BUFFER;
 370                         rxrpc_new_skb(skb, rxrpc_skb_new);
 371 
 372                         _debug("ALLOC SEND %p", skb);
 373 
 374                         ASSERTCMP(skb->mark, ==, 0);
 375 
 376                         _debug("HS: %u", call->conn->security_size);
 377                         skb_reserve(skb, call->conn->security_size);
 378                         skb->len += call->conn->security_size;
 379 
 380                         sp->remain = chunk;
 381                         if (sp->remain > skb_tailroom(skb))
 382                                 sp->remain = skb_tailroom(skb);
 383 
 384                         _net("skb: hr %d, tr %d, hl %d, rm %d",
 385                                skb_headroom(skb),
 386                                skb_tailroom(skb),
 387                                skb_headlen(skb),
 388                                sp->remain);
 389 
 390                         skb->ip_summed = CHECKSUM_UNNECESSARY;
 391                 }
 392 
 393                 _debug("append");
 394                 sp = rxrpc_skb(skb);
 395 
 396                 /* append next segment of data to the current buffer */
 397                 if (msg_data_left(msg) > 0) {
 398                         int copy = skb_tailroom(skb);
 399                         ASSERTCMP(copy, >, 0);
 400                         if (copy > msg_data_left(msg))
 401                                 copy = msg_data_left(msg);
 402                         if (copy > sp->remain)
 403                                 copy = sp->remain;
 404 
 405                         _debug("add");
 406                         ret = skb_add_data(skb, &msg->msg_iter, copy);
 407                         _debug("added");
 408                         if (ret < 0)
 409                                 goto efault;
 410                         sp->remain -= copy;
 411                         skb->mark += copy;
 412                         copied += copy;
 413                         if (call->tx_total_len != -1)
 414                                 call->tx_total_len -= copy;
 415                 }
 416 
 417                 /* check for the far side aborting the call or a network error
 418                  * occurring */
 419                 if (call->state == RXRPC_CALL_COMPLETE)
 420                         goto call_terminated;
 421 
 422                 /* add the packet to the send queue if it's now full */
 423                 if (sp->remain <= 0 ||
 424                     (msg_data_left(msg) == 0 && !more)) {
 425                         struct rxrpc_connection *conn = call->conn;
 426                         uint32_t seq;
 427                         size_t pad;
 428 
 429                         /* pad out if we're using security */
 430                         if (conn->security_ix) {
 431                                 pad = conn->security_size + skb->mark;
 432                                 pad = conn->size_align - pad;
 433                                 pad &= conn->size_align - 1;
 434                                 _debug("pad %zu", pad);
 435                                 if (pad)
 436                                         skb_put_zero(skb, pad);
 437                         }
 438 
 439                         seq = call->tx_top + 1;
 440 
 441                         sp->hdr.seq     = seq;
 442                         sp->hdr._rsvd   = 0;
 443                         sp->hdr.flags   = conn->out_clientflag;
 444 
 445                         if (msg_data_left(msg) == 0 && !more)
 446                                 sp->hdr.flags |= RXRPC_LAST_PACKET;
 447                         else if (call->tx_top - call->tx_hard_ack <
 448                                  call->tx_winsize)
 449                                 sp->hdr.flags |= RXRPC_MORE_PACKETS;
 450 
 451                         ret = call->security->secure_packet(
 452                                 call, skb, skb->mark, skb->head);
 453                         if (ret < 0)
 454                                 goto out;
 455 
 456                         ret = rxrpc_queue_packet(rx, call, skb,
 457                                                  !msg_data_left(msg) && !more,
 458                                                  notify_end_tx);
 459                         /* Should check for failure here */
 460                         skb = NULL;
 461                 }
 462         } while (msg_data_left(msg) > 0);
 463 
 464 success:
 465         ret = copied;
 466 out:
 467         call->tx_pending = skb;
 468         _leave(" = %d", ret);
 469         return ret;
 470 
 471 call_terminated:
 472         rxrpc_free_skb(skb, rxrpc_skb_freed);
 473         _leave(" = %d", call->error);
 474         return call->error;
 475 
 476 maybe_error:
 477         if (copied)
 478                 goto success;
 479         goto out;
 480 
 481 efault:
 482         ret = -EFAULT;
 483         goto out;
 484 }
 485 
 486 /*
 487  * extract control messages from the sendmsg() control buffer
 488  */
 489 static int rxrpc_sendmsg_cmsg(struct msghdr *msg, struct rxrpc_send_params *p)
 490 {
 491         struct cmsghdr *cmsg;
 492         bool got_user_ID = false;
 493         int len;
 494 
 495         if (msg->msg_controllen == 0)
 496                 return -EINVAL;
 497 
 498         for_each_cmsghdr(cmsg, msg) {
 499                 if (!CMSG_OK(msg, cmsg))
 500                         return -EINVAL;
 501 
 502                 len = cmsg->cmsg_len - sizeof(struct cmsghdr);
 503                 _debug("CMSG %d, %d, %d",
 504                        cmsg->cmsg_level, cmsg->cmsg_type, len);
 505 
 506                 if (cmsg->cmsg_level != SOL_RXRPC)
 507                         continue;
 508 
 509                 switch (cmsg->cmsg_type) {
 510                 case RXRPC_USER_CALL_ID:
 511                         if (msg->msg_flags & MSG_CMSG_COMPAT) {
 512                                 if (len != sizeof(u32))
 513                                         return -EINVAL;
 514                                 p->call.user_call_ID = *(u32 *)CMSG_DATA(cmsg);
 515                         } else {
 516                                 if (len != sizeof(unsigned long))
 517                                         return -EINVAL;
 518                                 p->call.user_call_ID = *(unsigned long *)
 519                                         CMSG_DATA(cmsg);
 520                         }
 521                         got_user_ID = true;
 522                         break;
 523 
 524                 case RXRPC_ABORT:
 525                         if (p->command != RXRPC_CMD_SEND_DATA)
 526                                 return -EINVAL;
 527                         p->command = RXRPC_CMD_SEND_ABORT;
 528                         if (len != sizeof(p->abort_code))
 529                                 return -EINVAL;
 530                         p->abort_code = *(unsigned int *)CMSG_DATA(cmsg);
 531                         if (p->abort_code == 0)
 532                                 return -EINVAL;
 533                         break;
 534 
 535                 case RXRPC_ACCEPT:
 536                         if (p->command != RXRPC_CMD_SEND_DATA)
 537                                 return -EINVAL;
 538                         p->command = RXRPC_CMD_ACCEPT;
 539                         if (len != 0)
 540                                 return -EINVAL;
 541                         break;
 542 
 543                 case RXRPC_EXCLUSIVE_CALL:
 544                         p->exclusive = true;
 545                         if (len != 0)
 546                                 return -EINVAL;
 547                         break;
 548 
 549                 case RXRPC_UPGRADE_SERVICE:
 550                         p->upgrade = true;
 551                         if (len != 0)
 552                                 return -EINVAL;
 553                         break;
 554 
 555                 case RXRPC_TX_LENGTH:
 556                         if (p->call.tx_total_len != -1 || len != sizeof(__s64))
 557                                 return -EINVAL;
 558                         p->call.tx_total_len = *(__s64 *)CMSG_DATA(cmsg);
 559                         if (p->call.tx_total_len < 0)
 560                                 return -EINVAL;
 561                         break;
 562 
 563                 case RXRPC_SET_CALL_TIMEOUT:
 564                         if (len & 3 || len < 4 || len > 12)
 565                                 return -EINVAL;
 566                         memcpy(&p->call.timeouts, CMSG_DATA(cmsg), len);
 567                         p->call.nr_timeouts = len / 4;
 568                         if (p->call.timeouts.hard > INT_MAX / HZ)
 569                                 return -ERANGE;
 570                         if (p->call.nr_timeouts >= 2 && p->call.timeouts.idle > 60 * 60 * 1000)
 571                                 return -ERANGE;
 572                         if (p->call.nr_timeouts >= 3 && p->call.timeouts.normal > 60 * 60 * 1000)
 573                                 return -ERANGE;
 574                         break;
 575 
 576                 default:
 577                         return -EINVAL;
 578                 }
 579         }
 580 
 581         if (!got_user_ID)
 582                 return -EINVAL;
 583         if (p->call.tx_total_len != -1 && p->command != RXRPC_CMD_SEND_DATA)
 584                 return -EINVAL;
 585         _leave(" = 0");
 586         return 0;
 587 }
 588 
 589 /*
 590  * Create a new client call for sendmsg().
 591  * - Called with the socket lock held, which it must release.
 592  * - If it returns a call, the call's lock will need releasing by the caller.
 593  */
 594 static struct rxrpc_call *
 595 rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
 596                                   struct rxrpc_send_params *p)
 597         __releases(&rx->sk.sk_lock.slock)
 598         __acquires(&call->user_mutex)
 599 {
 600         struct rxrpc_conn_parameters cp;
 601         struct rxrpc_call *call;
 602         struct key *key;
 603 
 604         DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
 605 
 606         _enter("");
 607 
 608         if (!msg->msg_name) {
 609                 release_sock(&rx->sk);
 610                 return ERR_PTR(-EDESTADDRREQ);
 611         }
 612 
 613         key = rx->key;
 614         if (key && !rx->key->payload.data[0])
 615                 key = NULL;
 616 
 617         memset(&cp, 0, sizeof(cp));
 618         cp.local                = rx->local;
 619         cp.key                  = rx->key;
 620         cp.security_level       = rx->min_sec_level;
 621         cp.exclusive            = rx->exclusive | p->exclusive;
 622         cp.upgrade              = p->upgrade;
 623         cp.service_id           = srx->srx_service;
 624         call = rxrpc_new_client_call(rx, &cp, srx, &p->call, GFP_KERNEL,
 625                                      atomic_inc_return(&rxrpc_debug_id));
 626         /* The socket is now unlocked */
 627 
 628         rxrpc_put_peer(cp.peer);
 629         _leave(" = %p\n", call);
 630         return call;
 631 }
 632 
 633 /*
 634  * send a message forming part of a client call through an RxRPC socket
 635  * - caller holds the socket locked
 636  * - the socket may be either a client socket or a server socket
 637  */
 638 int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
 639         __releases(&rx->sk.sk_lock.slock)
 640         __releases(&call->user_mutex)
 641 {
 642         enum rxrpc_call_state state;
 643         struct rxrpc_call *call;
 644         unsigned long now, j;
 645         int ret;
 646 
 647         struct rxrpc_send_params p = {
 648                 .call.tx_total_len      = -1,
 649                 .call.user_call_ID      = 0,
 650                 .call.nr_timeouts       = 0,
 651                 .call.interruptibility  = RXRPC_INTERRUPTIBLE,
 652                 .abort_code             = 0,
 653                 .command                = RXRPC_CMD_SEND_DATA,
 654                 .exclusive              = false,
 655                 .upgrade                = false,
 656         };
 657 
 658         _enter("");
 659 
 660         ret = rxrpc_sendmsg_cmsg(msg, &p);
 661         if (ret < 0)
 662                 goto error_release_sock;
 663 
 664         if (p.command == RXRPC_CMD_ACCEPT) {
 665                 ret = -EINVAL;
 666                 if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
 667                         goto error_release_sock;
 668                 call = rxrpc_accept_call(rx, p.call.user_call_ID, NULL);
 669                 /* The socket is now unlocked. */
 670                 if (IS_ERR(call))
 671                         return PTR_ERR(call);
 672                 ret = 0;
 673                 goto out_put_unlock;
 674         }
 675 
 676         call = rxrpc_find_call_by_user_ID(rx, p.call.user_call_ID);
 677         if (!call) {
 678                 ret = -EBADSLT;
 679                 if (p.command != RXRPC_CMD_SEND_DATA)
 680                         goto error_release_sock;
 681                 call = rxrpc_new_client_call_for_sendmsg(rx, msg, &p);
 682                 /* The socket is now unlocked... */
 683                 if (IS_ERR(call))
 684                         return PTR_ERR(call);
 685                 /* ... and we have the call lock. */
 686         } else {
 687                 switch (READ_ONCE(call->state)) {
 688                 case RXRPC_CALL_UNINITIALISED:
 689                 case RXRPC_CALL_CLIENT_AWAIT_CONN:
 690                 case RXRPC_CALL_SERVER_PREALLOC:
 691                 case RXRPC_CALL_SERVER_SECURING:
 692                 case RXRPC_CALL_SERVER_ACCEPTING:
 693                         rxrpc_put_call(call, rxrpc_call_put);
 694                         ret = -EBUSY;
 695                         goto error_release_sock;
 696                 default:
 697                         break;
 698                 }
 699 
 700                 ret = mutex_lock_interruptible(&call->user_mutex);
 701                 release_sock(&rx->sk);
 702                 if (ret < 0) {
 703                         ret = -ERESTARTSYS;
 704                         goto error_put;
 705                 }
 706 
 707                 if (p.call.tx_total_len != -1) {
 708                         ret = -EINVAL;
 709                         if (call->tx_total_len != -1 ||
 710                             call->tx_pending ||
 711                             call->tx_top != 0)
 712                                 goto error_put;
 713                         call->tx_total_len = p.call.tx_total_len;
 714                 }
 715         }
 716 
 717         switch (p.call.nr_timeouts) {
 718         case 3:
 719                 j = msecs_to_jiffies(p.call.timeouts.normal);
 720                 if (p.call.timeouts.normal > 0 && j == 0)
 721                         j = 1;
 722                 WRITE_ONCE(call->next_rx_timo, j);
 723                 /* Fall through */
 724         case 2:
 725                 j = msecs_to_jiffies(p.call.timeouts.idle);
 726                 if (p.call.timeouts.idle > 0 && j == 0)
 727                         j = 1;
 728                 WRITE_ONCE(call->next_req_timo, j);
 729                 /* Fall through */
 730         case 1:
 731                 if (p.call.timeouts.hard > 0) {
 732                         j = msecs_to_jiffies(p.call.timeouts.hard);
 733                         now = jiffies;
 734                         j += now;
 735                         WRITE_ONCE(call->expect_term_by, j);
 736                         rxrpc_reduce_call_timer(call, j, now,
 737                                                 rxrpc_timer_set_for_hard);
 738                 }
 739                 break;
 740         }
 741 
 742         state = READ_ONCE(call->state);
 743         _debug("CALL %d USR %lx ST %d on CONN %p",
 744                call->debug_id, call->user_call_ID, state, call->conn);
 745 
 746         if (state >= RXRPC_CALL_COMPLETE) {
 747                 /* it's too late for this call */
 748                 ret = -ESHUTDOWN;
 749         } else if (p.command == RXRPC_CMD_SEND_ABORT) {
 750                 ret = 0;
 751                 if (rxrpc_abort_call("CMD", call, 0, p.abort_code, -ECONNABORTED))
 752                         ret = rxrpc_send_abort_packet(call);
 753         } else if (p.command != RXRPC_CMD_SEND_DATA) {
 754                 ret = -EINVAL;
 755         } else if (rxrpc_is_client_call(call) &&
 756                    state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
 757                 /* request phase complete for this client call */
 758                 ret = -EPROTO;
 759         } else if (rxrpc_is_service_call(call) &&
 760                    state != RXRPC_CALL_SERVER_ACK_REQUEST &&
 761                    state != RXRPC_CALL_SERVER_SEND_REPLY) {
 762                 /* Reply phase not begun or not complete for service call. */
 763                 ret = -EPROTO;
 764         } else {
 765                 ret = rxrpc_send_data(rx, call, msg, len, NULL);
 766         }
 767 
 768 out_put_unlock:
 769         mutex_unlock(&call->user_mutex);
 770 error_put:
 771         rxrpc_put_call(call, rxrpc_call_put);
 772         _leave(" = %d", ret);
 773         return ret;
 774 
 775 error_release_sock:
 776         release_sock(&rx->sk);
 777         return ret;
 778 }
 779 
 780 /**
 781  * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
 782  * @sock: The socket the call is on
 783  * @call: The call to send data through
 784  * @msg: The data to send
 785  * @len: The amount of data to send
 786  * @notify_end_tx: Notification that the last packet is queued.
 787  *
 788  * Allow a kernel service to send data on a call.  The call must be in an state
 789  * appropriate to sending data.  No control data should be supplied in @msg,
 790  * nor should an address be supplied.  MSG_MORE should be flagged if there's
 791  * more data to come, otherwise this data will end the transmission phase.
 792  */
 793 int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
 794                            struct msghdr *msg, size_t len,
 795                            rxrpc_notify_end_tx_t notify_end_tx)
 796 {
 797         int ret;
 798 
 799         _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
 800 
 801         ASSERTCMP(msg->msg_name, ==, NULL);
 802         ASSERTCMP(msg->msg_control, ==, NULL);
 803 
 804         mutex_lock(&call->user_mutex);
 805 
 806         _debug("CALL %d USR %lx ST %d on CONN %p",
 807                call->debug_id, call->user_call_ID, call->state, call->conn);
 808 
 809         switch (READ_ONCE(call->state)) {
 810         case RXRPC_CALL_CLIENT_SEND_REQUEST:
 811         case RXRPC_CALL_SERVER_ACK_REQUEST:
 812         case RXRPC_CALL_SERVER_SEND_REPLY:
 813                 ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len,
 814                                       notify_end_tx);
 815                 break;
 816         case RXRPC_CALL_COMPLETE:
 817                 read_lock_bh(&call->state_lock);
 818                 ret = call->error;
 819                 read_unlock_bh(&call->state_lock);
 820                 break;
 821         default:
 822                 /* Request phase complete for this client call */
 823                 trace_rxrpc_rx_eproto(call, 0, tracepoint_string("late_send"));
 824                 ret = -EPROTO;
 825                 break;
 826         }
 827 
 828         mutex_unlock(&call->user_mutex);
 829         _leave(" = %d", ret);
 830         return ret;
 831 }
 832 EXPORT_SYMBOL(rxrpc_kernel_send_data);
 833 
 834 /**
 835  * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
 836  * @sock: The socket the call is on
 837  * @call: The call to be aborted
 838  * @abort_code: The abort code to stick into the ABORT packet
 839  * @error: Local error value
 840  * @why: 3-char string indicating why.
 841  *
 842  * Allow a kernel service to abort a call, if it's still in an abortable state
 843  * and return true if the call was aborted, false if it was already complete.
 844  */
 845 bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
 846                              u32 abort_code, int error, const char *why)
 847 {
 848         bool aborted;
 849 
 850         _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why);
 851 
 852         mutex_lock(&call->user_mutex);
 853 
 854         aborted = rxrpc_abort_call(why, call, 0, abort_code, error);
 855         if (aborted)
 856                 rxrpc_send_abort_packet(call);
 857 
 858         mutex_unlock(&call->user_mutex);
 859         return aborted;
 860 }
 861 EXPORT_SYMBOL(rxrpc_kernel_abort_call);
 862 
 863 /**
 864  * rxrpc_kernel_set_tx_length - Set the total Tx length on a call
 865  * @sock: The socket the call is on
 866  * @call: The call to be informed
 867  * @tx_total_len: The amount of data to be transmitted for this call
 868  *
 869  * Allow a kernel service to set the total transmit length on a call.  This
 870  * allows buffer-to-packet encrypt-and-copy to be performed.
 871  *
 872  * This function is primarily for use for setting the reply length since the
 873  * request length can be set when beginning the call.
 874  */
 875 void rxrpc_kernel_set_tx_length(struct socket *sock, struct rxrpc_call *call,
 876                                 s64 tx_total_len)
 877 {
 878         WARN_ON(call->tx_total_len != -1);
 879         call->tx_total_len = tx_total_len;
 880 }
 881 EXPORT_SYMBOL(rxrpc_kernel_set_tx_length);

/* [<][>][^][v][top][bottom][index][help] */