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