David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1 | /* RxRPC packet transmission |
| 2 | * |
| 3 | * Copyright (C) 2007 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 License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
Joe Perches | 9b6d539 | 2016-06-02 12:08:52 -0700 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 14 | #include <linux/net.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/gfp.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 16 | #include <linux/skbuff.h> |
| 17 | #include <linux/circ_buf.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 18 | #include <linux/export.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 19 | #include <net/sock.h> |
| 20 | #include <net/af_rxrpc.h> |
| 21 | #include "ar-internal.h" |
| 22 | |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 23 | /* |
| 24 | * Time till packet resend (in jiffies). |
| 25 | */ |
David Howells | dad8aff | 2016-03-09 23:22:56 +0000 | [diff] [blame] | 26 | unsigned int rxrpc_resend_timeout = 4 * HZ; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 27 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 28 | static int rxrpc_send_data(struct rxrpc_sock *rx, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 29 | struct rxrpc_call *call, |
| 30 | struct msghdr *msg, size_t len); |
| 31 | |
| 32 | /* |
| 33 | * extract control messages from the sendmsg() control buffer |
| 34 | */ |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 35 | static int rxrpc_sendmsg_cmsg(struct msghdr *msg, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 36 | unsigned long *user_call_ID, |
| 37 | enum rxrpc_command *command, |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 38 | u32 *abort_code) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 39 | { |
| 40 | struct cmsghdr *cmsg; |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 41 | bool got_user_ID = false; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 42 | int len; |
| 43 | |
| 44 | *command = RXRPC_CMD_SEND_DATA; |
| 45 | |
| 46 | if (msg->msg_controllen == 0) |
| 47 | return -EINVAL; |
| 48 | |
Gu Zheng | f95b414 | 2014-12-11 11:22:04 +0800 | [diff] [blame] | 49 | for_each_cmsghdr(cmsg, msg) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 50 | if (!CMSG_OK(msg, cmsg)) |
| 51 | return -EINVAL; |
| 52 | |
| 53 | len = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)); |
| 54 | _debug("CMSG %d, %d, %d", |
| 55 | cmsg->cmsg_level, cmsg->cmsg_type, len); |
| 56 | |
| 57 | if (cmsg->cmsg_level != SOL_RXRPC) |
| 58 | continue; |
| 59 | |
| 60 | switch (cmsg->cmsg_type) { |
| 61 | case RXRPC_USER_CALL_ID: |
| 62 | if (msg->msg_flags & MSG_CMSG_COMPAT) { |
| 63 | if (len != sizeof(u32)) |
| 64 | return -EINVAL; |
| 65 | *user_call_ID = *(u32 *) CMSG_DATA(cmsg); |
| 66 | } else { |
| 67 | if (len != sizeof(unsigned long)) |
| 68 | return -EINVAL; |
| 69 | *user_call_ID = *(unsigned long *) |
| 70 | CMSG_DATA(cmsg); |
| 71 | } |
| 72 | _debug("User Call ID %lx", *user_call_ID); |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 73 | got_user_ID = true; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 74 | break; |
| 75 | |
| 76 | case RXRPC_ABORT: |
| 77 | if (*command != RXRPC_CMD_SEND_DATA) |
| 78 | return -EINVAL; |
| 79 | *command = RXRPC_CMD_SEND_ABORT; |
| 80 | if (len != sizeof(*abort_code)) |
| 81 | return -EINVAL; |
| 82 | *abort_code = *(unsigned int *) CMSG_DATA(cmsg); |
| 83 | _debug("Abort %x", *abort_code); |
| 84 | if (*abort_code == 0) |
| 85 | return -EINVAL; |
| 86 | break; |
| 87 | |
| 88 | case RXRPC_ACCEPT: |
| 89 | if (*command != RXRPC_CMD_SEND_DATA) |
| 90 | return -EINVAL; |
| 91 | *command = RXRPC_CMD_ACCEPT; |
| 92 | if (len != 0) |
| 93 | return -EINVAL; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 94 | break; |
| 95 | |
| 96 | default: |
| 97 | return -EINVAL; |
| 98 | } |
| 99 | } |
| 100 | |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 101 | if (!got_user_ID) |
| 102 | return -EINVAL; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 103 | _leave(" = 0"); |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * abort a call, sending an ABORT packet to the peer |
| 109 | */ |
| 110 | static void rxrpc_send_abort(struct rxrpc_call *call, u32 abort_code) |
| 111 | { |
| 112 | write_lock_bh(&call->state_lock); |
| 113 | |
| 114 | if (call->state <= RXRPC_CALL_COMPLETE) { |
| 115 | call->state = RXRPC_CALL_LOCALLY_ABORTED; |
David Howells | dc44b3a | 2016-04-07 17:23:30 +0100 | [diff] [blame] | 116 | call->local_abort = abort_code; |
David Howells | 4c198ad | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 117 | set_bit(RXRPC_CALL_EV_ABORT, &call->events); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 118 | del_timer_sync(&call->resend_timer); |
| 119 | del_timer_sync(&call->ack_timer); |
David Howells | 4c198ad | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 120 | clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events); |
| 121 | clear_bit(RXRPC_CALL_EV_ACK, &call->events); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 122 | clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 123 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | write_unlock_bh(&call->state_lock); |
| 127 | } |
| 128 | |
| 129 | /* |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 130 | * Create a new client call for sendmsg(). |
| 131 | */ |
| 132 | static struct rxrpc_call * |
| 133 | rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, |
| 134 | unsigned long user_call_ID) |
| 135 | { |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame^] | 136 | struct rxrpc_conn_parameters cp; |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 137 | struct rxrpc_conn_bundle *bundle; |
| 138 | struct rxrpc_transport *trans; |
| 139 | struct rxrpc_call *call; |
| 140 | struct key *key; |
| 141 | long ret; |
| 142 | |
| 143 | DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name); |
| 144 | |
| 145 | _enter(""); |
| 146 | |
| 147 | if (!msg->msg_name) |
| 148 | return ERR_PTR(-EDESTADDRREQ); |
| 149 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame^] | 150 | key = rx->key; |
| 151 | if (key && !rx->key->payload.data[0]) |
| 152 | key = NULL; |
| 153 | |
| 154 | memset(&cp, 0, sizeof(cp)); |
| 155 | cp.local = rx->local; |
| 156 | cp.key = rx->key; |
| 157 | cp.security_level = rx->min_sec_level; |
| 158 | cp.exclusive = test_bit(RXRPC_SOCK_EXCLUSIVE_CONN, &rx->flags); |
| 159 | cp.service_id = srx->srx_service; |
| 160 | trans = rxrpc_name_to_transport(&cp, msg->msg_name, msg->msg_namelen, |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 161 | GFP_KERNEL); |
| 162 | if (IS_ERR(trans)) { |
| 163 | ret = PTR_ERR(trans); |
| 164 | goto out; |
| 165 | } |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame^] | 166 | cp.peer = trans->peer; |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 167 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame^] | 168 | bundle = rxrpc_get_bundle(rx, trans, cp.key, srx->srx_service, |
| 169 | GFP_KERNEL); |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 170 | if (IS_ERR(bundle)) { |
| 171 | ret = PTR_ERR(bundle); |
| 172 | goto out_trans; |
| 173 | } |
| 174 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame^] | 175 | call = rxrpc_new_client_call(rx, &cp, trans, bundle, user_call_ID, |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 176 | GFP_KERNEL); |
| 177 | rxrpc_put_bundle(trans, bundle); |
| 178 | rxrpc_put_transport(trans); |
| 179 | if (IS_ERR(call)) { |
| 180 | ret = PTR_ERR(call); |
| 181 | goto out_trans; |
| 182 | } |
| 183 | |
| 184 | _leave(" = %p\n", call); |
| 185 | return call; |
| 186 | |
| 187 | out_trans: |
| 188 | rxrpc_put_transport(trans); |
| 189 | out: |
| 190 | _leave(" = %ld", ret); |
| 191 | return ERR_PTR(ret); |
| 192 | } |
| 193 | |
| 194 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 195 | * send a message forming part of a client call through an RxRPC socket |
| 196 | * - caller holds the socket locked |
| 197 | * - the socket may be either a client socket or a server socket |
| 198 | */ |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 199 | int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 200 | { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 201 | enum rxrpc_command cmd; |
| 202 | struct rxrpc_call *call; |
| 203 | unsigned long user_call_ID = 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 204 | u32 abort_code = 0; |
| 205 | int ret; |
| 206 | |
| 207 | _enter(""); |
| 208 | |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 209 | ret = rxrpc_sendmsg_cmsg(msg, &user_call_ID, &cmd, &abort_code); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 210 | if (ret < 0) |
| 211 | return ret; |
| 212 | |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 213 | if (cmd == RXRPC_CMD_ACCEPT) { |
| 214 | if (rx->sk.sk_state != RXRPC_SERVER_LISTENING) |
| 215 | return -EINVAL; |
| 216 | call = rxrpc_accept_call(rx, user_call_ID); |
| 217 | if (IS_ERR(call)) |
| 218 | return PTR_ERR(call); |
| 219 | rxrpc_put_call(call); |
| 220 | return 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 221 | } |
| 222 | |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 223 | call = rxrpc_find_call_by_user_ID(rx, user_call_ID); |
| 224 | if (!call) { |
| 225 | if (cmd != RXRPC_CMD_SEND_DATA) |
| 226 | return -EBADSLT; |
| 227 | call = rxrpc_new_client_call_for_sendmsg(rx, msg, user_call_ID); |
| 228 | if (IS_ERR(call)) |
| 229 | return PTR_ERR(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | _debug("CALL %d USR %lx ST %d on CONN %p", |
| 233 | call->debug_id, call->user_call_ID, call->state, call->conn); |
| 234 | |
| 235 | if (call->state >= RXRPC_CALL_COMPLETE) { |
| 236 | /* it's too late for this call */ |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 237 | ret = -ECONNRESET; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 238 | } else if (cmd == RXRPC_CMD_SEND_ABORT) { |
| 239 | rxrpc_send_abort(call, abort_code); |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 240 | ret = 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 241 | } else if (cmd != RXRPC_CMD_SEND_DATA) { |
| 242 | ret = -EINVAL; |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 243 | } else if (!call->in_clientflag && |
| 244 | call->state != RXRPC_CALL_CLIENT_SEND_REQUEST) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 245 | /* request phase complete for this client call */ |
| 246 | ret = -EPROTO; |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 247 | } else if (call->in_clientflag && |
| 248 | call->state != RXRPC_CALL_SERVER_ACK_REQUEST && |
| 249 | call->state != RXRPC_CALL_SERVER_SEND_REPLY) { |
| 250 | /* Reply phase not begun or not complete for service call. */ |
| 251 | ret = -EPROTO; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 252 | } else { |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 253 | ret = rxrpc_send_data(rx, call, msg, len); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | rxrpc_put_call(call); |
| 257 | _leave(" = %d", ret); |
| 258 | return ret; |
| 259 | } |
| 260 | |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 261 | /** |
| 262 | * rxrpc_kernel_send_data - Allow a kernel service to send data on a call |
| 263 | * @call: The call to send data through |
| 264 | * @msg: The data to send |
| 265 | * @len: The amount of data to send |
| 266 | * |
| 267 | * Allow a kernel service to send data on a call. The call must be in an state |
| 268 | * appropriate to sending data. No control data should be supplied in @msg, |
| 269 | * nor should an address be supplied. MSG_MORE should be flagged if there's |
| 270 | * more data to come, otherwise this data will end the transmission phase. |
| 271 | */ |
| 272 | int rxrpc_kernel_send_data(struct rxrpc_call *call, struct msghdr *msg, |
| 273 | size_t len) |
| 274 | { |
| 275 | int ret; |
| 276 | |
| 277 | _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]); |
| 278 | |
| 279 | ASSERTCMP(msg->msg_name, ==, NULL); |
| 280 | ASSERTCMP(msg->msg_control, ==, NULL); |
| 281 | |
| 282 | lock_sock(&call->socket->sk); |
| 283 | |
| 284 | _debug("CALL %d USR %lx ST %d on CONN %p", |
| 285 | call->debug_id, call->user_call_ID, call->state, call->conn); |
| 286 | |
| 287 | if (call->state >= RXRPC_CALL_COMPLETE) { |
| 288 | ret = -ESHUTDOWN; /* it's too late for this call */ |
| 289 | } else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST && |
| 290 | call->state != RXRPC_CALL_SERVER_ACK_REQUEST && |
| 291 | call->state != RXRPC_CALL_SERVER_SEND_REPLY) { |
| 292 | ret = -EPROTO; /* request phase complete for this client call */ |
| 293 | } else { |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 294 | ret = rxrpc_send_data(call->socket, call, msg, len); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | release_sock(&call->socket->sk); |
| 298 | _leave(" = %d", ret); |
| 299 | return ret; |
| 300 | } |
| 301 | |
| 302 | EXPORT_SYMBOL(rxrpc_kernel_send_data); |
| 303 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 304 | /** |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 305 | * rxrpc_kernel_abort_call - Allow a kernel service to abort a call |
| 306 | * @call: The call to be aborted |
| 307 | * @abort_code: The abort code to stick into the ABORT packet |
| 308 | * |
| 309 | * Allow a kernel service to abort a call, if it's still in an abortable state. |
| 310 | */ |
| 311 | void rxrpc_kernel_abort_call(struct rxrpc_call *call, u32 abort_code) |
| 312 | { |
| 313 | _enter("{%d},%d", call->debug_id, abort_code); |
| 314 | |
| 315 | lock_sock(&call->socket->sk); |
| 316 | |
| 317 | _debug("CALL %d USR %lx ST %d on CONN %p", |
| 318 | call->debug_id, call->user_call_ID, call->state, call->conn); |
| 319 | |
| 320 | if (call->state < RXRPC_CALL_COMPLETE) |
| 321 | rxrpc_send_abort(call, abort_code); |
| 322 | |
| 323 | release_sock(&call->socket->sk); |
| 324 | _leave(""); |
| 325 | } |
| 326 | |
| 327 | EXPORT_SYMBOL(rxrpc_kernel_abort_call); |
| 328 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 329 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 330 | * send a packet through the transport endpoint |
| 331 | */ |
| 332 | int rxrpc_send_packet(struct rxrpc_transport *trans, struct sk_buff *skb) |
| 333 | { |
| 334 | struct kvec iov[1]; |
| 335 | struct msghdr msg; |
| 336 | int ret, opt; |
| 337 | |
| 338 | _enter(",{%d}", skb->len); |
| 339 | |
| 340 | iov[0].iov_base = skb->head; |
| 341 | iov[0].iov_len = skb->len; |
| 342 | |
| 343 | msg.msg_name = &trans->peer->srx.transport.sin; |
| 344 | msg.msg_namelen = sizeof(trans->peer->srx.transport.sin); |
| 345 | msg.msg_control = NULL; |
| 346 | msg.msg_controllen = 0; |
| 347 | msg.msg_flags = 0; |
| 348 | |
| 349 | /* send the packet with the don't fragment bit set if we currently |
| 350 | * think it's small enough */ |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 351 | if (skb->len - sizeof(struct rxrpc_wire_header) < trans->peer->maxdata) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 352 | down_read(&trans->local->defrag_sem); |
| 353 | /* send the packet by UDP |
| 354 | * - returns -EMSGSIZE if UDP would have to fragment the packet |
| 355 | * to go out of the interface |
| 356 | * - in which case, we'll have processed the ICMP error |
| 357 | * message and update the peer record |
| 358 | */ |
| 359 | ret = kernel_sendmsg(trans->local->socket, &msg, iov, 1, |
| 360 | iov[0].iov_len); |
| 361 | |
| 362 | up_read(&trans->local->defrag_sem); |
| 363 | if (ret == -EMSGSIZE) |
| 364 | goto send_fragmentable; |
| 365 | |
| 366 | _leave(" = %d [%u]", ret, trans->peer->maxdata); |
| 367 | return ret; |
| 368 | } |
| 369 | |
| 370 | send_fragmentable: |
| 371 | /* attempt to send this message with fragmentation enabled */ |
| 372 | _debug("send fragment"); |
| 373 | |
| 374 | down_write(&trans->local->defrag_sem); |
| 375 | opt = IP_PMTUDISC_DONT; |
| 376 | ret = kernel_setsockopt(trans->local->socket, SOL_IP, IP_MTU_DISCOVER, |
| 377 | (char *) &opt, sizeof(opt)); |
| 378 | if (ret == 0) { |
| 379 | ret = kernel_sendmsg(trans->local->socket, &msg, iov, 1, |
| 380 | iov[0].iov_len); |
| 381 | |
| 382 | opt = IP_PMTUDISC_DO; |
| 383 | kernel_setsockopt(trans->local->socket, SOL_IP, |
| 384 | IP_MTU_DISCOVER, (char *) &opt, sizeof(opt)); |
| 385 | } |
| 386 | |
| 387 | up_write(&trans->local->defrag_sem); |
| 388 | _leave(" = %d [frag %u]", ret, trans->peer->maxdata); |
| 389 | return ret; |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * wait for space to appear in the transmit/ACK window |
| 394 | * - caller holds the socket locked |
| 395 | */ |
| 396 | static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx, |
| 397 | struct rxrpc_call *call, |
| 398 | long *timeo) |
| 399 | { |
| 400 | DECLARE_WAITQUEUE(myself, current); |
| 401 | int ret; |
| 402 | |
| 403 | _enter(",{%d},%ld", |
David Howells | ee72b9f | 2016-03-04 15:58:06 +0000 | [diff] [blame] | 404 | CIRC_SPACE(call->acks_head, ACCESS_ONCE(call->acks_tail), |
| 405 | call->acks_winsz), |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 406 | *timeo); |
| 407 | |
| 408 | add_wait_queue(&call->tx_waitq, &myself); |
| 409 | |
| 410 | for (;;) { |
| 411 | set_current_state(TASK_INTERRUPTIBLE); |
| 412 | ret = 0; |
David Howells | ee72b9f | 2016-03-04 15:58:06 +0000 | [diff] [blame] | 413 | if (CIRC_SPACE(call->acks_head, ACCESS_ONCE(call->acks_tail), |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 414 | call->acks_winsz) > 0) |
| 415 | break; |
| 416 | if (signal_pending(current)) { |
| 417 | ret = sock_intr_errno(*timeo); |
| 418 | break; |
| 419 | } |
| 420 | |
| 421 | release_sock(&rx->sk); |
| 422 | *timeo = schedule_timeout(*timeo); |
| 423 | lock_sock(&rx->sk); |
| 424 | } |
| 425 | |
| 426 | remove_wait_queue(&call->tx_waitq, &myself); |
| 427 | set_current_state(TASK_RUNNING); |
| 428 | _leave(" = %d", ret); |
| 429 | return ret; |
| 430 | } |
| 431 | |
| 432 | /* |
| 433 | * attempt to schedule an instant Tx resend |
| 434 | */ |
| 435 | static inline void rxrpc_instant_resend(struct rxrpc_call *call) |
| 436 | { |
| 437 | read_lock_bh(&call->state_lock); |
| 438 | if (try_to_del_timer_sync(&call->resend_timer) >= 0) { |
| 439 | clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags); |
| 440 | if (call->state < RXRPC_CALL_COMPLETE && |
David Howells | 4c198ad | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 441 | !test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events)) |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 442 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 443 | } |
| 444 | read_unlock_bh(&call->state_lock); |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * queue a packet for transmission, set the resend timer and attempt |
| 449 | * to send the packet immediately |
| 450 | */ |
| 451 | static void rxrpc_queue_packet(struct rxrpc_call *call, struct sk_buff *skb, |
| 452 | bool last) |
| 453 | { |
| 454 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
| 455 | int ret; |
| 456 | |
| 457 | _net("queue skb %p [%d]", skb, call->acks_head); |
| 458 | |
| 459 | ASSERT(call->acks_window != NULL); |
| 460 | call->acks_window[call->acks_head] = (unsigned long) skb; |
| 461 | smp_wmb(); |
| 462 | call->acks_head = (call->acks_head + 1) & (call->acks_winsz - 1); |
| 463 | |
| 464 | if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) { |
| 465 | _debug("________awaiting reply/ACK__________"); |
| 466 | write_lock_bh(&call->state_lock); |
| 467 | switch (call->state) { |
| 468 | case RXRPC_CALL_CLIENT_SEND_REQUEST: |
| 469 | call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY; |
| 470 | break; |
| 471 | case RXRPC_CALL_SERVER_ACK_REQUEST: |
| 472 | call->state = RXRPC_CALL_SERVER_SEND_REPLY; |
| 473 | if (!last) |
| 474 | break; |
| 475 | case RXRPC_CALL_SERVER_SEND_REPLY: |
| 476 | call->state = RXRPC_CALL_SERVER_AWAIT_ACK; |
| 477 | break; |
| 478 | default: |
| 479 | break; |
| 480 | } |
| 481 | write_unlock_bh(&call->state_lock); |
| 482 | } |
| 483 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 484 | _proto("Tx DATA %%%u { #%u }", sp->hdr.serial, sp->hdr.seq); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 485 | |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 486 | sp->need_resend = false; |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 487 | sp->resend_at = jiffies + rxrpc_resend_timeout; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 488 | if (!test_and_set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags)) { |
| 489 | _debug("run timer"); |
| 490 | call->resend_timer.expires = sp->resend_at; |
| 491 | add_timer(&call->resend_timer); |
| 492 | } |
| 493 | |
| 494 | /* attempt to cancel the rx-ACK timer, deferring reply transmission if |
| 495 | * we're ACK'ing the request phase of an incoming call */ |
| 496 | ret = -EAGAIN; |
| 497 | if (try_to_del_timer_sync(&call->ack_timer) >= 0) { |
| 498 | /* the packet may be freed by rxrpc_process_call() before this |
| 499 | * returns */ |
| 500 | ret = rxrpc_send_packet(call->conn->trans, skb); |
| 501 | _net("sent skb %p", skb); |
| 502 | } else { |
| 503 | _debug("failed to delete ACK timer"); |
| 504 | } |
| 505 | |
| 506 | if (ret < 0) { |
| 507 | _debug("need instant resend %d", ret); |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 508 | sp->need_resend = true; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 509 | rxrpc_instant_resend(call); |
| 510 | } |
| 511 | |
| 512 | _leave(""); |
| 513 | } |
| 514 | |
| 515 | /* |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 516 | * Convert a host-endian header into a network-endian header. |
| 517 | */ |
| 518 | static void rxrpc_insert_header(struct sk_buff *skb) |
| 519 | { |
| 520 | struct rxrpc_wire_header whdr; |
| 521 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
| 522 | |
| 523 | whdr.epoch = htonl(sp->hdr.epoch); |
| 524 | whdr.cid = htonl(sp->hdr.cid); |
| 525 | whdr.callNumber = htonl(sp->hdr.callNumber); |
| 526 | whdr.seq = htonl(sp->hdr.seq); |
| 527 | whdr.serial = htonl(sp->hdr.serial); |
| 528 | whdr.type = sp->hdr.type; |
| 529 | whdr.flags = sp->hdr.flags; |
| 530 | whdr.userStatus = sp->hdr.userStatus; |
| 531 | whdr.securityIndex = sp->hdr.securityIndex; |
| 532 | whdr._rsvd = htons(sp->hdr._rsvd); |
| 533 | whdr.serviceId = htons(sp->hdr.serviceId); |
| 534 | |
| 535 | memcpy(skb->head, &whdr, sizeof(whdr)); |
| 536 | } |
| 537 | |
| 538 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 539 | * send data through a socket |
| 540 | * - must be called in process context |
| 541 | * - caller holds the socket locked |
| 542 | */ |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 543 | static int rxrpc_send_data(struct rxrpc_sock *rx, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 544 | struct rxrpc_call *call, |
| 545 | struct msghdr *msg, size_t len) |
| 546 | { |
| 547 | struct rxrpc_skb_priv *sp; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 548 | struct sk_buff *skb; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 549 | struct sock *sk = &rx->sk; |
| 550 | long timeo; |
| 551 | bool more; |
Al Viro | af2b040 | 2014-11-27 21:44:24 -0500 | [diff] [blame] | 552 | int ret, copied; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 553 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 554 | timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
| 555 | |
| 556 | /* this should be in poll */ |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 557 | sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 558 | |
| 559 | if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) |
| 560 | return -EPIPE; |
| 561 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 562 | more = msg->msg_flags & MSG_MORE; |
| 563 | |
| 564 | skb = call->tx_pending; |
| 565 | call->tx_pending = NULL; |
| 566 | |
| 567 | copied = 0; |
David Howells | 3af6878 | 2015-04-01 14:06:00 +0100 | [diff] [blame] | 568 | do { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 569 | if (!skb) { |
| 570 | size_t size, chunk, max, space; |
| 571 | |
| 572 | _debug("alloc"); |
| 573 | |
David Howells | ee72b9f | 2016-03-04 15:58:06 +0000 | [diff] [blame] | 574 | if (CIRC_SPACE(call->acks_head, |
| 575 | ACCESS_ONCE(call->acks_tail), |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 576 | call->acks_winsz) <= 0) { |
| 577 | ret = -EAGAIN; |
| 578 | if (msg->msg_flags & MSG_DONTWAIT) |
| 579 | goto maybe_error; |
| 580 | ret = rxrpc_wait_for_tx_window(rx, call, |
| 581 | &timeo); |
| 582 | if (ret < 0) |
| 583 | goto maybe_error; |
| 584 | } |
| 585 | |
| 586 | max = call->conn->trans->peer->maxdata; |
| 587 | max -= call->conn->security_size; |
| 588 | max &= ~(call->conn->size_align - 1UL); |
| 589 | |
| 590 | chunk = max; |
Al Viro | 01e97e6 | 2014-12-15 21:39:31 -0500 | [diff] [blame] | 591 | if (chunk > msg_data_left(msg) && !more) |
| 592 | chunk = msg_data_left(msg); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 593 | |
| 594 | space = chunk + call->conn->size_align; |
| 595 | space &= ~(call->conn->size_align - 1UL); |
| 596 | |
| 597 | size = space + call->conn->header_size; |
| 598 | |
| 599 | _debug("SIZE: %zu/%zu/%zu", chunk, space, size); |
| 600 | |
| 601 | /* create a buffer that we can retain until it's ACK'd */ |
| 602 | skb = sock_alloc_send_skb( |
| 603 | sk, size, msg->msg_flags & MSG_DONTWAIT, &ret); |
| 604 | if (!skb) |
| 605 | goto maybe_error; |
| 606 | |
| 607 | rxrpc_new_skb(skb); |
| 608 | |
| 609 | _debug("ALLOC SEND %p", skb); |
| 610 | |
| 611 | ASSERTCMP(skb->mark, ==, 0); |
| 612 | |
| 613 | _debug("HS: %u", call->conn->header_size); |
| 614 | skb_reserve(skb, call->conn->header_size); |
| 615 | skb->len += call->conn->header_size; |
| 616 | |
| 617 | sp = rxrpc_skb(skb); |
| 618 | sp->remain = chunk; |
| 619 | if (sp->remain > skb_tailroom(skb)) |
| 620 | sp->remain = skb_tailroom(skb); |
| 621 | |
| 622 | _net("skb: hr %d, tr %d, hl %d, rm %d", |
| 623 | skb_headroom(skb), |
| 624 | skb_tailroom(skb), |
| 625 | skb_headlen(skb), |
| 626 | sp->remain); |
| 627 | |
| 628 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 629 | } |
| 630 | |
| 631 | _debug("append"); |
| 632 | sp = rxrpc_skb(skb); |
| 633 | |
| 634 | /* append next segment of data to the current buffer */ |
Al Viro | 01e97e6 | 2014-12-15 21:39:31 -0500 | [diff] [blame] | 635 | if (msg_data_left(msg) > 0) { |
David Howells | aab9483 | 2015-04-01 15:48:00 +0100 | [diff] [blame] | 636 | int copy = skb_tailroom(skb); |
| 637 | ASSERTCMP(copy, >, 0); |
Al Viro | 01e97e6 | 2014-12-15 21:39:31 -0500 | [diff] [blame] | 638 | if (copy > msg_data_left(msg)) |
| 639 | copy = msg_data_left(msg); |
David Howells | aab9483 | 2015-04-01 15:48:00 +0100 | [diff] [blame] | 640 | if (copy > sp->remain) |
| 641 | copy = sp->remain; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 642 | |
David Howells | aab9483 | 2015-04-01 15:48:00 +0100 | [diff] [blame] | 643 | _debug("add"); |
| 644 | ret = skb_add_data(skb, &msg->msg_iter, copy); |
| 645 | _debug("added"); |
| 646 | if (ret < 0) |
| 647 | goto efault; |
| 648 | sp->remain -= copy; |
| 649 | skb->mark += copy; |
| 650 | copied += copy; |
David Howells | aab9483 | 2015-04-01 15:48:00 +0100 | [diff] [blame] | 651 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 652 | |
| 653 | /* check for the far side aborting the call or a network error |
| 654 | * occurring */ |
| 655 | if (call->state > RXRPC_CALL_COMPLETE) |
| 656 | goto call_aborted; |
| 657 | |
| 658 | /* add the packet to the send queue if it's now full */ |
David Howells | 382d797 | 2015-04-01 15:43:26 +0100 | [diff] [blame] | 659 | if (sp->remain <= 0 || |
Al Viro | 01e97e6 | 2014-12-15 21:39:31 -0500 | [diff] [blame] | 660 | (msg_data_left(msg) == 0 && !more)) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 661 | struct rxrpc_connection *conn = call->conn; |
David Howells | e8388eb | 2014-02-14 20:05:32 +0000 | [diff] [blame] | 662 | uint32_t seq; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 663 | size_t pad; |
| 664 | |
| 665 | /* pad out if we're using security */ |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 666 | if (conn->security_ix) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 667 | pad = conn->security_size + skb->mark; |
| 668 | pad = conn->size_align - pad; |
| 669 | pad &= conn->size_align - 1; |
| 670 | _debug("pad %zu", pad); |
| 671 | if (pad) |
| 672 | memset(skb_put(skb, pad), 0, pad); |
| 673 | } |
| 674 | |
David Howells | e8388eb | 2014-02-14 20:05:32 +0000 | [diff] [blame] | 675 | seq = atomic_inc_return(&call->sequence); |
| 676 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame^] | 677 | sp->hdr.epoch = conn->proto.epoch; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 678 | sp->hdr.cid = call->cid; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 679 | sp->hdr.callNumber = call->call_id; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 680 | sp->hdr.seq = seq; |
| 681 | sp->hdr.serial = atomic_inc_return(&conn->serial); |
| 682 | sp->hdr.type = RXRPC_PACKET_TYPE_DATA; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 683 | sp->hdr.userStatus = 0; |
| 684 | sp->hdr.securityIndex = conn->security_ix; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 685 | sp->hdr._rsvd = 0; |
| 686 | sp->hdr.serviceId = call->service_id; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 687 | |
| 688 | sp->hdr.flags = conn->out_clientflag; |
Al Viro | 01e97e6 | 2014-12-15 21:39:31 -0500 | [diff] [blame] | 689 | if (msg_data_left(msg) == 0 && !more) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 690 | sp->hdr.flags |= RXRPC_LAST_PACKET; |
David Howells | ee72b9f | 2016-03-04 15:58:06 +0000 | [diff] [blame] | 691 | else if (CIRC_SPACE(call->acks_head, |
| 692 | ACCESS_ONCE(call->acks_tail), |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 693 | call->acks_winsz) > 1) |
| 694 | sp->hdr.flags |= RXRPC_MORE_PACKETS; |
David Howells | e8388eb | 2014-02-14 20:05:32 +0000 | [diff] [blame] | 695 | if (more && seq & 1) |
| 696 | sp->hdr.flags |= RXRPC_REQUEST_ACK; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 697 | |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 698 | ret = conn->security->secure_packet( |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 699 | call, skb, skb->mark, |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 700 | skb->head + sizeof(struct rxrpc_wire_header)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 701 | if (ret < 0) |
| 702 | goto out; |
| 703 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 704 | rxrpc_insert_header(skb); |
Al Viro | 01e97e6 | 2014-12-15 21:39:31 -0500 | [diff] [blame] | 705 | rxrpc_queue_packet(call, skb, !msg_data_left(msg) && !more); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 706 | skb = NULL; |
| 707 | } |
Al Viro | 01e97e6 | 2014-12-15 21:39:31 -0500 | [diff] [blame] | 708 | } while (msg_data_left(msg) > 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 709 | |
David Howells | 19e6454 | 2007-06-18 23:30:41 -0700 | [diff] [blame] | 710 | success: |
| 711 | ret = copied; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 712 | out: |
| 713 | call->tx_pending = skb; |
| 714 | _leave(" = %d", ret); |
| 715 | return ret; |
| 716 | |
| 717 | call_aborted: |
| 718 | rxrpc_free_skb(skb); |
| 719 | if (call->state == RXRPC_CALL_NETWORK_ERROR) |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 720 | ret = call->error_report < RXRPC_LOCAL_ERROR_OFFSET ? |
| 721 | call->error_report : |
| 722 | call->error_report - RXRPC_LOCAL_ERROR_OFFSET; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 723 | else |
| 724 | ret = -ECONNABORTED; |
| 725 | _leave(" = %d", ret); |
| 726 | return ret; |
| 727 | |
| 728 | maybe_error: |
| 729 | if (copied) |
David Howells | 19e6454 | 2007-06-18 23:30:41 -0700 | [diff] [blame] | 730 | goto success; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 731 | goto out; |
| 732 | |
| 733 | efault: |
| 734 | ret = -EFAULT; |
| 735 | goto out; |
| 736 | } |