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