David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 1 | /* 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> |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 18 | #include <net/sock.h> |
| 19 | #include <net/af_rxrpc.h> |
| 20 | #include "ar-internal.h" |
| 21 | |
David Howells | 3dc20f0 | 2016-09-04 13:25:21 +0100 | [diff] [blame] | 22 | enum rxrpc_command { |
| 23 | RXRPC_CMD_SEND_DATA, /* send data message */ |
| 24 | RXRPC_CMD_SEND_ABORT, /* request abort generation */ |
| 25 | RXRPC_CMD_ACCEPT, /* [server] accept incoming call */ |
| 26 | RXRPC_CMD_REJECT_BUSY, /* [server] reject a call as busy */ |
| 27 | }; |
| 28 | |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 29 | /* |
| 30 | * wait for space to appear in the transmit/ACK window |
| 31 | * - caller holds the socket locked |
| 32 | */ |
| 33 | static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx, |
| 34 | struct rxrpc_call *call, |
| 35 | long *timeo) |
| 36 | { |
| 37 | DECLARE_WAITQUEUE(myself, current); |
| 38 | int ret; |
| 39 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 40 | _enter(",{%u,%u,%u}", |
| 41 | call->tx_hard_ack, call->tx_top, call->tx_winsize); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 42 | |
| 43 | add_wait_queue(&call->waitq, &myself); |
| 44 | |
| 45 | for (;;) { |
| 46 | set_current_state(TASK_INTERRUPTIBLE); |
| 47 | ret = 0; |
David Howells | 5749434 | 2016-09-24 18:05:27 +0100 | [diff] [blame] | 48 | if (call->tx_top - call->tx_hard_ack < |
| 49 | min_t(unsigned int, call->tx_winsize, |
| 50 | call->cong_cwnd + call->cong_extra)) |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 51 | break; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 52 | if (call->state >= RXRPC_CALL_COMPLETE) { |
| 53 | ret = -call->error; |
| 54 | break; |
| 55 | } |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 56 | if (signal_pending(current)) { |
| 57 | ret = sock_intr_errno(*timeo); |
| 58 | break; |
| 59 | } |
| 60 | |
David Howells | a124fe3 | 2016-09-17 10:49:13 +0100 | [diff] [blame] | 61 | trace_rxrpc_transmit(call, rxrpc_transmit_wait); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 62 | release_sock(&rx->sk); |
| 63 | *timeo = schedule_timeout(*timeo); |
| 64 | lock_sock(&rx->sk); |
| 65 | } |
| 66 | |
| 67 | remove_wait_queue(&call->waitq, &myself); |
| 68 | set_current_state(TASK_RUNNING); |
| 69 | _leave(" = %d", ret); |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | /* |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 74 | * Schedule an instant Tx resend. |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 75 | */ |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 76 | static inline void rxrpc_instant_resend(struct rxrpc_call *call, int ix) |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 77 | { |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 78 | spin_lock_bh(&call->lock); |
| 79 | |
| 80 | if (call->state < RXRPC_CALL_COMPLETE) { |
David Howells | 9b7c95a | 2018-03-30 21:04:43 +0100 | [diff] [blame] | 81 | call->rxtx_annotations[ix] = |
| 82 | (call->rxtx_annotations[ix] & RXRPC_TX_ANNO_LAST) | |
| 83 | RXRPC_TX_ANNO_RETRANS; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 84 | if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events)) |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 85 | rxrpc_queue_call(call); |
| 86 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 87 | |
| 88 | spin_unlock_bh(&call->lock); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 92 | * Queue a DATA packet for transmission, set the resend timeout and send the |
| 93 | * packet immediately |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 94 | */ |
| 95 | static void rxrpc_queue_packet(struct rxrpc_call *call, struct sk_buff *skb, |
| 96 | bool last) |
| 97 | { |
| 98 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 99 | rxrpc_seq_t seq = sp->hdr.seq; |
| 100 | int ret, ix; |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 101 | u8 annotation = RXRPC_TX_ANNO_UNACK; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 102 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 103 | _net("queue skb %p [%d]", skb, seq); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 104 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 105 | ASSERTCMP(seq, ==, call->tx_top + 1); |
| 106 | |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 107 | if (last) |
| 108 | annotation |= RXRPC_TX_ANNO_LAST; |
| 109 | |
David Howells | b24d289 | 2016-09-23 13:17:33 +0100 | [diff] [blame] | 110 | /* We have to set the timestamp before queueing as the retransmit |
| 111 | * algorithm can see the packet as soon as we queue it. |
| 112 | */ |
| 113 | skb->tstamp = ktime_get_real(); |
| 114 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 115 | ix = seq & RXRPC_RXTX_BUFF_MASK; |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 116 | rxrpc_get_skb(skb, rxrpc_skb_tx_got); |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 117 | call->rxtx_annotations[ix] = annotation; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 118 | smp_wmb(); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 119 | call->rxtx_buffer[ix] = skb; |
| 120 | call->tx_top = seq; |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 121 | if (last) |
David Howells | a124fe3 | 2016-09-17 10:49:13 +0100 | [diff] [blame] | 122 | trace_rxrpc_transmit(call, rxrpc_transmit_queue_last); |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 123 | else |
David Howells | a124fe3 | 2016-09-17 10:49:13 +0100 | [diff] [blame] | 124 | trace_rxrpc_transmit(call, rxrpc_transmit_queue); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 125 | |
| 126 | if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) { |
| 127 | _debug("________awaiting reply/ACK__________"); |
| 128 | write_lock_bh(&call->state_lock); |
| 129 | switch (call->state) { |
| 130 | case RXRPC_CALL_CLIENT_SEND_REQUEST: |
| 131 | call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY; |
| 132 | break; |
| 133 | case RXRPC_CALL_SERVER_ACK_REQUEST: |
| 134 | call->state = RXRPC_CALL_SERVER_SEND_REPLY; |
David Howells | 9749fd2 | 2016-10-06 08:11:50 +0100 | [diff] [blame] | 135 | call->ack_at = call->expire_at; |
| 136 | if (call->ackr_reason == RXRPC_ACK_DELAY) |
| 137 | call->ackr_reason = 0; |
| 138 | __rxrpc_set_timer(call, rxrpc_timer_init_for_send_reply, |
| 139 | ktime_get_real()); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 140 | if (!last) |
| 141 | break; |
| 142 | case RXRPC_CALL_SERVER_SEND_REPLY: |
| 143 | call->state = RXRPC_CALL_SERVER_AWAIT_ACK; |
| 144 | break; |
| 145 | default: |
| 146 | break; |
| 147 | } |
| 148 | write_unlock_bh(&call->state_lock); |
| 149 | } |
| 150 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 151 | if (seq == 1 && rxrpc_is_client_call(call)) |
| 152 | rxrpc_expose_client_call(call); |
| 153 | |
David Howells | a176707 | 2016-09-29 22:37:15 +0100 | [diff] [blame] | 154 | ret = rxrpc_send_data_packet(call, skb, false); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 155 | if (ret < 0) { |
| 156 | _debug("need instant resend %d", ret); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 157 | rxrpc_instant_resend(call, ix); |
David Howells | dfc3da4 | 2016-09-23 12:39:23 +0100 | [diff] [blame] | 158 | } else { |
David Howells | df0adc7 | 2016-09-26 22:12:49 +0100 | [diff] [blame] | 159 | ktime_t now = ktime_get_real(), resend_at; |
David Howells | dfc3da4 | 2016-09-23 12:39:23 +0100 | [diff] [blame] | 160 | |
David Howells | df0adc7 | 2016-09-26 22:12:49 +0100 | [diff] [blame] | 161 | resend_at = ktime_add_ms(now, rxrpc_resend_timeout); |
David Howells | dfc3da4 | 2016-09-23 12:39:23 +0100 | [diff] [blame] | 162 | |
David Howells | df0adc7 | 2016-09-26 22:12:49 +0100 | [diff] [blame] | 163 | if (ktime_before(resend_at, call->resend_at)) { |
David Howells | dfc3da4 | 2016-09-23 12:39:23 +0100 | [diff] [blame] | 164 | call->resend_at = resend_at; |
David Howells | df0adc7 | 2016-09-26 22:12:49 +0100 | [diff] [blame] | 165 | rxrpc_set_timer(call, rxrpc_timer_set_for_send, now); |
David Howells | dfc3da4 | 2016-09-23 12:39:23 +0100 | [diff] [blame] | 166 | } |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 167 | } |
| 168 | |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 169 | rxrpc_free_skb(skb, rxrpc_skb_tx_freed); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 170 | _leave(""); |
| 171 | } |
| 172 | |
| 173 | /* |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 174 | * send data through a socket |
| 175 | * - must be called in process context |
| 176 | * - caller holds the socket locked |
| 177 | */ |
| 178 | static int rxrpc_send_data(struct rxrpc_sock *rx, |
| 179 | struct rxrpc_call *call, |
| 180 | struct msghdr *msg, size_t len) |
| 181 | { |
| 182 | struct rxrpc_skb_priv *sp; |
| 183 | struct sk_buff *skb; |
| 184 | struct sock *sk = &rx->sk; |
| 185 | long timeo; |
| 186 | bool more; |
| 187 | int ret, copied; |
| 188 | |
| 189 | timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
| 190 | |
| 191 | /* this should be in poll */ |
| 192 | sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
| 193 | |
| 194 | if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) |
| 195 | return -EPIPE; |
| 196 | |
| 197 | more = msg->msg_flags & MSG_MORE; |
| 198 | |
| 199 | skb = call->tx_pending; |
| 200 | call->tx_pending = NULL; |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 201 | rxrpc_see_skb(skb, rxrpc_skb_tx_seen); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 202 | |
| 203 | copied = 0; |
| 204 | do { |
David Howells | 7aa51da | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 205 | /* Check to see if there's a ping ACK to reply to. */ |
| 206 | if (call->ackr_reason == RXRPC_ACK_PING_RESPONSE) |
David Howells | a5af7e1 | 2016-10-06 08:11:49 +0100 | [diff] [blame] | 207 | rxrpc_send_ack_packet(call, false); |
David Howells | 7aa51da | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 208 | |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 209 | if (!skb) { |
| 210 | size_t size, chunk, max, space; |
| 211 | |
| 212 | _debug("alloc"); |
| 213 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 214 | if (call->tx_top - call->tx_hard_ack >= |
David Howells | 5749434 | 2016-09-24 18:05:27 +0100 | [diff] [blame] | 215 | min_t(unsigned int, call->tx_winsize, |
| 216 | call->cong_cwnd + call->cong_extra)) { |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 217 | ret = -EAGAIN; |
| 218 | if (msg->msg_flags & MSG_DONTWAIT) |
| 219 | goto maybe_error; |
| 220 | ret = rxrpc_wait_for_tx_window(rx, call, |
| 221 | &timeo); |
| 222 | if (ret < 0) |
| 223 | goto maybe_error; |
| 224 | } |
| 225 | |
David Howells | 182f505 | 2016-09-17 10:49:12 +0100 | [diff] [blame] | 226 | max = RXRPC_JUMBO_DATALEN; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 227 | max -= call->conn->security_size; |
| 228 | max &= ~(call->conn->size_align - 1UL); |
| 229 | |
| 230 | chunk = max; |
| 231 | if (chunk > msg_data_left(msg) && !more) |
| 232 | chunk = msg_data_left(msg); |
| 233 | |
| 234 | space = chunk + call->conn->size_align; |
| 235 | space &= ~(call->conn->size_align - 1UL); |
| 236 | |
David Howells | 5a924b8 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 237 | size = space + call->conn->security_size; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 238 | |
| 239 | _debug("SIZE: %zu/%zu/%zu", chunk, space, size); |
| 240 | |
| 241 | /* create a buffer that we can retain until it's ACK'd */ |
| 242 | skb = sock_alloc_send_skb( |
| 243 | sk, size, msg->msg_flags & MSG_DONTWAIT, &ret); |
| 244 | if (!skb) |
| 245 | goto maybe_error; |
| 246 | |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 247 | rxrpc_new_skb(skb, rxrpc_skb_tx_new); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 248 | |
| 249 | _debug("ALLOC SEND %p", skb); |
| 250 | |
| 251 | ASSERTCMP(skb->mark, ==, 0); |
| 252 | |
David Howells | 5a924b8 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 253 | _debug("HS: %u", call->conn->security_size); |
| 254 | skb_reserve(skb, call->conn->security_size); |
| 255 | skb->len += call->conn->security_size; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 256 | |
| 257 | sp = rxrpc_skb(skb); |
| 258 | sp->remain = chunk; |
| 259 | if (sp->remain > skb_tailroom(skb)) |
| 260 | sp->remain = skb_tailroom(skb); |
| 261 | |
| 262 | _net("skb: hr %d, tr %d, hl %d, rm %d", |
| 263 | skb_headroom(skb), |
| 264 | skb_tailroom(skb), |
| 265 | skb_headlen(skb), |
| 266 | sp->remain); |
| 267 | |
| 268 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 269 | } |
| 270 | |
| 271 | _debug("append"); |
| 272 | sp = rxrpc_skb(skb); |
| 273 | |
| 274 | /* append next segment of data to the current buffer */ |
| 275 | if (msg_data_left(msg) > 0) { |
| 276 | int copy = skb_tailroom(skb); |
| 277 | ASSERTCMP(copy, >, 0); |
| 278 | if (copy > msg_data_left(msg)) |
| 279 | copy = msg_data_left(msg); |
| 280 | if (copy > sp->remain) |
| 281 | copy = sp->remain; |
| 282 | |
| 283 | _debug("add"); |
| 284 | ret = skb_add_data(skb, &msg->msg_iter, copy); |
| 285 | _debug("added"); |
| 286 | if (ret < 0) |
| 287 | goto efault; |
| 288 | sp->remain -= copy; |
| 289 | skb->mark += copy; |
| 290 | copied += copy; |
| 291 | } |
| 292 | |
| 293 | /* check for the far side aborting the call or a network error |
| 294 | * occurring */ |
| 295 | if (call->state == RXRPC_CALL_COMPLETE) |
| 296 | goto call_terminated; |
| 297 | |
| 298 | /* add the packet to the send queue if it's now full */ |
| 299 | if (sp->remain <= 0 || |
| 300 | (msg_data_left(msg) == 0 && !more)) { |
| 301 | struct rxrpc_connection *conn = call->conn; |
| 302 | uint32_t seq; |
| 303 | size_t pad; |
| 304 | |
| 305 | /* pad out if we're using security */ |
| 306 | if (conn->security_ix) { |
| 307 | pad = conn->security_size + skb->mark; |
| 308 | pad = conn->size_align - pad; |
| 309 | pad &= conn->size_align - 1; |
| 310 | _debug("pad %zu", pad); |
| 311 | if (pad) |
| 312 | memset(skb_put(skb, pad), 0, pad); |
| 313 | } |
| 314 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 315 | seq = call->tx_top + 1; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 316 | |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 317 | sp->hdr.seq = seq; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 318 | sp->hdr._rsvd = 0; |
David Howells | 5a924b8 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 319 | sp->hdr.flags = conn->out_clientflag; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 320 | |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 321 | if (msg_data_left(msg) == 0 && !more) |
| 322 | sp->hdr.flags |= RXRPC_LAST_PACKET; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 323 | else if (call->tx_top - call->tx_hard_ack < |
| 324 | call->tx_winsize) |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 325 | sp->hdr.flags |= RXRPC_MORE_PACKETS; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 326 | |
| 327 | ret = conn->security->secure_packet( |
David Howells | 5a924b8 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 328 | call, skb, skb->mark, skb->head); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 329 | if (ret < 0) |
| 330 | goto out; |
| 331 | |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 332 | rxrpc_queue_packet(call, skb, !msg_data_left(msg) && !more); |
| 333 | skb = NULL; |
| 334 | } |
| 335 | } while (msg_data_left(msg) > 0); |
| 336 | |
| 337 | success: |
| 338 | ret = copied; |
| 339 | out: |
| 340 | call->tx_pending = skb; |
| 341 | _leave(" = %d", ret); |
| 342 | return ret; |
| 343 | |
| 344 | call_terminated: |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 345 | rxrpc_free_skb(skb, rxrpc_skb_tx_freed); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 346 | _leave(" = %d", -call->error); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 347 | return -call->error; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 348 | |
| 349 | maybe_error: |
| 350 | if (copied) |
| 351 | goto success; |
| 352 | goto out; |
| 353 | |
| 354 | efault: |
| 355 | ret = -EFAULT; |
| 356 | goto out; |
| 357 | } |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 358 | |
| 359 | /* |
| 360 | * extract control messages from the sendmsg() control buffer |
| 361 | */ |
| 362 | static int rxrpc_sendmsg_cmsg(struct msghdr *msg, |
| 363 | unsigned long *user_call_ID, |
| 364 | enum rxrpc_command *command, |
| 365 | u32 *abort_code, |
| 366 | bool *_exclusive) |
| 367 | { |
| 368 | struct cmsghdr *cmsg; |
| 369 | bool got_user_ID = false; |
| 370 | int len; |
| 371 | |
| 372 | *command = RXRPC_CMD_SEND_DATA; |
| 373 | |
| 374 | if (msg->msg_controllen == 0) |
| 375 | return -EINVAL; |
| 376 | |
| 377 | for_each_cmsghdr(cmsg, msg) { |
| 378 | if (!CMSG_OK(msg, cmsg)) |
| 379 | return -EINVAL; |
| 380 | |
| 381 | len = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)); |
| 382 | _debug("CMSG %d, %d, %d", |
| 383 | cmsg->cmsg_level, cmsg->cmsg_type, len); |
| 384 | |
| 385 | if (cmsg->cmsg_level != SOL_RXRPC) |
| 386 | continue; |
| 387 | |
| 388 | switch (cmsg->cmsg_type) { |
| 389 | case RXRPC_USER_CALL_ID: |
| 390 | if (msg->msg_flags & MSG_CMSG_COMPAT) { |
| 391 | if (len != sizeof(u32)) |
| 392 | return -EINVAL; |
| 393 | *user_call_ID = *(u32 *) CMSG_DATA(cmsg); |
| 394 | } else { |
| 395 | if (len != sizeof(unsigned long)) |
| 396 | return -EINVAL; |
| 397 | *user_call_ID = *(unsigned long *) |
| 398 | CMSG_DATA(cmsg); |
| 399 | } |
| 400 | _debug("User Call ID %lx", *user_call_ID); |
| 401 | got_user_ID = true; |
| 402 | break; |
| 403 | |
| 404 | case RXRPC_ABORT: |
| 405 | if (*command != RXRPC_CMD_SEND_DATA) |
| 406 | return -EINVAL; |
| 407 | *command = RXRPC_CMD_SEND_ABORT; |
| 408 | if (len != sizeof(*abort_code)) |
| 409 | return -EINVAL; |
| 410 | *abort_code = *(unsigned int *) CMSG_DATA(cmsg); |
| 411 | _debug("Abort %x", *abort_code); |
| 412 | if (*abort_code == 0) |
| 413 | return -EINVAL; |
| 414 | break; |
| 415 | |
| 416 | case RXRPC_ACCEPT: |
| 417 | if (*command != RXRPC_CMD_SEND_DATA) |
| 418 | return -EINVAL; |
| 419 | *command = RXRPC_CMD_ACCEPT; |
| 420 | if (len != 0) |
| 421 | return -EINVAL; |
| 422 | break; |
| 423 | |
| 424 | case RXRPC_EXCLUSIVE_CALL: |
| 425 | *_exclusive = true; |
| 426 | if (len != 0) |
| 427 | return -EINVAL; |
| 428 | break; |
| 429 | default: |
| 430 | return -EINVAL; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | if (!got_user_ID) |
| 435 | return -EINVAL; |
| 436 | _leave(" = 0"); |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | /* |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 441 | * Create a new client call for sendmsg(). |
| 442 | */ |
| 443 | static struct rxrpc_call * |
| 444 | rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, |
| 445 | unsigned long user_call_ID, bool exclusive) |
| 446 | { |
| 447 | struct rxrpc_conn_parameters cp; |
| 448 | struct rxrpc_call *call; |
| 449 | struct key *key; |
| 450 | |
| 451 | DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name); |
| 452 | |
| 453 | _enter(""); |
| 454 | |
| 455 | if (!msg->msg_name) |
| 456 | return ERR_PTR(-EDESTADDRREQ); |
| 457 | |
| 458 | key = rx->key; |
| 459 | if (key && !rx->key->payload.data[0]) |
| 460 | key = NULL; |
| 461 | |
| 462 | memset(&cp, 0, sizeof(cp)); |
| 463 | cp.local = rx->local; |
| 464 | cp.key = rx->key; |
| 465 | cp.security_level = rx->min_sec_level; |
| 466 | cp.exclusive = rx->exclusive | exclusive; |
| 467 | cp.service_id = srx->srx_service; |
| 468 | call = rxrpc_new_client_call(rx, &cp, srx, user_call_ID, GFP_KERNEL); |
| 469 | |
| 470 | _leave(" = %p\n", call); |
| 471 | return call; |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * send a message forming part of a client call through an RxRPC socket |
| 476 | * - caller holds the socket locked |
| 477 | * - the socket may be either a client socket or a server socket |
| 478 | */ |
| 479 | int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len) |
| 480 | { |
| 481 | enum rxrpc_command cmd; |
| 482 | struct rxrpc_call *call; |
| 483 | unsigned long user_call_ID = 0; |
| 484 | bool exclusive = false; |
| 485 | u32 abort_code = 0; |
| 486 | int ret; |
| 487 | |
| 488 | _enter(""); |
| 489 | |
| 490 | ret = rxrpc_sendmsg_cmsg(msg, &user_call_ID, &cmd, &abort_code, |
| 491 | &exclusive); |
| 492 | if (ret < 0) |
| 493 | return ret; |
| 494 | |
| 495 | if (cmd == RXRPC_CMD_ACCEPT) { |
| 496 | if (rx->sk.sk_state != RXRPC_SERVER_LISTENING) |
| 497 | return -EINVAL; |
| 498 | call = rxrpc_accept_call(rx, user_call_ID, NULL); |
| 499 | if (IS_ERR(call)) |
| 500 | return PTR_ERR(call); |
David Howells | fff7242 | 2016-09-07 14:34:21 +0100 | [diff] [blame] | 501 | rxrpc_put_call(call, rxrpc_call_put); |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | call = rxrpc_find_call_by_user_ID(rx, user_call_ID); |
| 506 | if (!call) { |
| 507 | if (cmd != RXRPC_CMD_SEND_DATA) |
| 508 | return -EBADSLT; |
| 509 | call = rxrpc_new_client_call_for_sendmsg(rx, msg, user_call_ID, |
| 510 | exclusive); |
| 511 | if (IS_ERR(call)) |
| 512 | return PTR_ERR(call); |
| 513 | } |
| 514 | |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 515 | _debug("CALL %d USR %lx ST %d on CONN %p", |
| 516 | call->debug_id, call->user_call_ID, call->state, call->conn); |
| 517 | |
| 518 | if (call->state >= RXRPC_CALL_COMPLETE) { |
| 519 | /* it's too late for this call */ |
| 520 | ret = -ESHUTDOWN; |
| 521 | } else if (cmd == RXRPC_CMD_SEND_ABORT) { |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 522 | ret = 0; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 523 | if (rxrpc_abort_call("CMD", call, 0, abort_code, ECONNABORTED)) |
David Howells | 26cb02a | 2016-10-06 08:11:49 +0100 | [diff] [blame] | 524 | ret = rxrpc_send_abort_packet(call); |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 525 | } else if (cmd != RXRPC_CMD_SEND_DATA) { |
| 526 | ret = -EINVAL; |
| 527 | } else if (rxrpc_is_client_call(call) && |
| 528 | call->state != RXRPC_CALL_CLIENT_SEND_REQUEST) { |
| 529 | /* request phase complete for this client call */ |
| 530 | ret = -EPROTO; |
| 531 | } else if (rxrpc_is_service_call(call) && |
| 532 | call->state != RXRPC_CALL_SERVER_ACK_REQUEST && |
| 533 | call->state != RXRPC_CALL_SERVER_SEND_REPLY) { |
| 534 | /* Reply phase not begun or not complete for service call. */ |
| 535 | ret = -EPROTO; |
| 536 | } else { |
| 537 | ret = rxrpc_send_data(rx, call, msg, len); |
| 538 | } |
| 539 | |
David Howells | fff7242 | 2016-09-07 14:34:21 +0100 | [diff] [blame] | 540 | rxrpc_put_call(call, rxrpc_call_put); |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 541 | _leave(" = %d", ret); |
| 542 | return ret; |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * rxrpc_kernel_send_data - Allow a kernel service to send data on a call |
| 547 | * @sock: The socket the call is on |
| 548 | * @call: The call to send data through |
| 549 | * @msg: The data to send |
| 550 | * @len: The amount of data to send |
| 551 | * |
| 552 | * Allow a kernel service to send data on a call. The call must be in an state |
| 553 | * appropriate to sending data. No control data should be supplied in @msg, |
| 554 | * nor should an address be supplied. MSG_MORE should be flagged if there's |
| 555 | * more data to come, otherwise this data will end the transmission phase. |
| 556 | */ |
| 557 | int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call, |
| 558 | struct msghdr *msg, size_t len) |
| 559 | { |
| 560 | int ret; |
| 561 | |
| 562 | _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]); |
| 563 | |
| 564 | ASSERTCMP(msg->msg_name, ==, NULL); |
| 565 | ASSERTCMP(msg->msg_control, ==, NULL); |
| 566 | |
| 567 | lock_sock(sock->sk); |
| 568 | |
| 569 | _debug("CALL %d USR %lx ST %d on CONN %p", |
| 570 | call->debug_id, call->user_call_ID, call->state, call->conn); |
| 571 | |
| 572 | if (call->state >= RXRPC_CALL_COMPLETE) { |
| 573 | ret = -ESHUTDOWN; /* it's too late for this call */ |
| 574 | } else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST && |
| 575 | call->state != RXRPC_CALL_SERVER_ACK_REQUEST && |
| 576 | call->state != RXRPC_CALL_SERVER_SEND_REPLY) { |
| 577 | ret = -EPROTO; /* request phase complete for this client call */ |
| 578 | } else { |
| 579 | ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len); |
| 580 | } |
| 581 | |
| 582 | release_sock(sock->sk); |
| 583 | _leave(" = %d", ret); |
| 584 | return ret; |
| 585 | } |
| 586 | EXPORT_SYMBOL(rxrpc_kernel_send_data); |
| 587 | |
| 588 | /** |
| 589 | * rxrpc_kernel_abort_call - Allow a kernel service to abort a call |
| 590 | * @sock: The socket the call is on |
| 591 | * @call: The call to be aborted |
| 592 | * @abort_code: The abort code to stick into the ABORT packet |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 593 | * @error: Local error value |
| 594 | * @why: 3-char string indicating why. |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 595 | * |
| 596 | * Allow a kernel service to abort a call, if it's still in an abortable state. |
| 597 | */ |
| 598 | void rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call, |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 599 | u32 abort_code, int error, const char *why) |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 600 | { |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 601 | _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why); |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 602 | |
| 603 | lock_sock(sock->sk); |
| 604 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 605 | if (rxrpc_abort_call(why, call, 0, abort_code, error)) |
David Howells | 26cb02a | 2016-10-06 08:11:49 +0100 | [diff] [blame] | 606 | rxrpc_send_abort_packet(call); |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 607 | |
| 608 | release_sock(sock->sk); |
| 609 | _leave(""); |
| 610 | } |
| 611 | |
| 612 | EXPORT_SYMBOL(rxrpc_kernel_abort_call); |