Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 2 | /* connection-level event handling |
| 3 | * |
| 4 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 5 | * Written by David Howells (dhowells@redhat.com) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Joe Perches | 9b6d539 | 2016-06-02 12:08:52 -0700 | [diff] [blame] | 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <linux/net.h> |
| 12 | #include <linux/skbuff.h> |
| 13 | #include <linux/errqueue.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 14 | #include <net/sock.h> |
| 15 | #include <net/af_rxrpc.h> |
| 16 | #include <net/ip.h> |
| 17 | #include "ar-internal.h" |
| 18 | |
| 19 | /* |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 20 | * Retransmit terminal ACK or ABORT of the previous call. |
| 21 | */ |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 22 | static void rxrpc_conn_retransmit_call(struct rxrpc_connection *conn, |
David Howells | 3136ef4 | 2017-11-24 10:18:41 +0000 | [diff] [blame] | 23 | struct sk_buff *skb, |
| 24 | unsigned int channel) |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 25 | { |
David Howells | 3136ef4 | 2017-11-24 10:18:41 +0000 | [diff] [blame] | 26 | struct rxrpc_skb_priv *sp = skb ? rxrpc_skb(skb) : NULL; |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 27 | struct rxrpc_channel *chan; |
| 28 | struct msghdr msg; |
David Howells | 5fc62f6 | 2017-11-29 14:40:41 +0000 | [diff] [blame] | 29 | struct kvec iov[3]; |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 30 | struct { |
| 31 | struct rxrpc_wire_header whdr; |
| 32 | union { |
David Howells | 5fc62f6 | 2017-11-29 14:40:41 +0000 | [diff] [blame] | 33 | __be32 abort_code; |
| 34 | struct rxrpc_ackpacket ack; |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 35 | }; |
| 36 | } __attribute__((packed)) pkt; |
David Howells | 5fc62f6 | 2017-11-29 14:40:41 +0000 | [diff] [blame] | 37 | struct rxrpc_ackinfo ack_info; |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 38 | size_t len; |
David Howells | 6b47fe1 | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 39 | int ret, ioc; |
David Howells | 5fc62f6 | 2017-11-29 14:40:41 +0000 | [diff] [blame] | 40 | u32 serial, mtu, call_id, padding; |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 41 | |
| 42 | _enter("%d", conn->debug_id); |
| 43 | |
David Howells | 3136ef4 | 2017-11-24 10:18:41 +0000 | [diff] [blame] | 44 | chan = &conn->channels[channel]; |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 45 | |
| 46 | /* If the last call got moved on whilst we were waiting to run, just |
| 47 | * ignore this packet. |
| 48 | */ |
| 49 | call_id = READ_ONCE(chan->last_call); |
| 50 | /* Sync with __rxrpc_disconnect_call() */ |
| 51 | smp_rmb(); |
David Howells | 3136ef4 | 2017-11-24 10:18:41 +0000 | [diff] [blame] | 52 | if (skb && call_id != sp->hdr.callNumber) |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 53 | return; |
| 54 | |
| 55 | msg.msg_name = &conn->params.peer->srx.transport; |
| 56 | msg.msg_namelen = conn->params.peer->srx.transport_len; |
| 57 | msg.msg_control = NULL; |
| 58 | msg.msg_controllen = 0; |
| 59 | msg.msg_flags = 0; |
| 60 | |
David Howells | 5fc62f6 | 2017-11-29 14:40:41 +0000 | [diff] [blame] | 61 | iov[0].iov_base = &pkt; |
| 62 | iov[0].iov_len = sizeof(pkt.whdr); |
| 63 | iov[1].iov_base = &padding; |
| 64 | iov[1].iov_len = 3; |
| 65 | iov[2].iov_base = &ack_info; |
| 66 | iov[2].iov_len = sizeof(ack_info); |
| 67 | |
David Howells | 3136ef4 | 2017-11-24 10:18:41 +0000 | [diff] [blame] | 68 | pkt.whdr.epoch = htonl(conn->proto.epoch); |
David Howells | fb1967a | 2018-06-06 14:59:14 +0100 | [diff] [blame] | 69 | pkt.whdr.cid = htonl(conn->proto.cid | channel); |
David Howells | 3136ef4 | 2017-11-24 10:18:41 +0000 | [diff] [blame] | 70 | pkt.whdr.callNumber = htonl(call_id); |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 71 | pkt.whdr.seq = 0; |
| 72 | pkt.whdr.type = chan->last_type; |
| 73 | pkt.whdr.flags = conn->out_clientflag; |
| 74 | pkt.whdr.userStatus = 0; |
| 75 | pkt.whdr.securityIndex = conn->security_ix; |
| 76 | pkt.whdr._rsvd = 0; |
David Howells | 68d6d1a | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 77 | pkt.whdr.serviceId = htons(conn->service_id); |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 78 | |
| 79 | len = sizeof(pkt.whdr); |
| 80 | switch (chan->last_type) { |
| 81 | case RXRPC_PACKET_TYPE_ABORT: |
David Howells | 5fc62f6 | 2017-11-29 14:40:41 +0000 | [diff] [blame] | 82 | pkt.abort_code = htonl(chan->last_abort); |
| 83 | iov[0].iov_len += sizeof(pkt.abort_code); |
| 84 | len += sizeof(pkt.abort_code); |
| 85 | ioc = 1; |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 86 | break; |
| 87 | |
| 88 | case RXRPC_PACKET_TYPE_ACK: |
| 89 | mtu = conn->params.peer->if_mtu; |
| 90 | mtu -= conn->params.peer->hdrsize; |
| 91 | pkt.ack.bufferSpace = 0; |
David Howells | 3136ef4 | 2017-11-24 10:18:41 +0000 | [diff] [blame] | 92 | pkt.ack.maxSkew = htons(skb ? skb->priority : 0); |
| 93 | pkt.ack.firstPacket = htonl(chan->last_seq + 1); |
| 94 | pkt.ack.previousPacket = htonl(chan->last_seq); |
| 95 | pkt.ack.serial = htonl(skb ? sp->hdr.serial : 0); |
| 96 | pkt.ack.reason = skb ? RXRPC_ACK_DUPLICATE : RXRPC_ACK_IDLE; |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 97 | pkt.ack.nAcks = 0; |
David Howells | 5fc62f6 | 2017-11-29 14:40:41 +0000 | [diff] [blame] | 98 | ack_info.rxMTU = htonl(rxrpc_rx_mtu); |
| 99 | ack_info.maxMTU = htonl(mtu); |
| 100 | ack_info.rwind = htonl(rxrpc_rx_window_size); |
| 101 | ack_info.jumbo_max = htonl(rxrpc_rx_jumbo_max); |
David Howells | 5749434 | 2016-09-24 18:05:27 +0100 | [diff] [blame] | 102 | pkt.whdr.flags |= RXRPC_SLOW_START_OK; |
David Howells | 5fc62f6 | 2017-11-29 14:40:41 +0000 | [diff] [blame] | 103 | padding = 0; |
| 104 | iov[0].iov_len += sizeof(pkt.ack); |
| 105 | len += sizeof(pkt.ack) + 3 + sizeof(ack_info); |
| 106 | ioc = 3; |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 107 | break; |
David Howells | 5fc62f6 | 2017-11-29 14:40:41 +0000 | [diff] [blame] | 108 | |
| 109 | default: |
| 110 | return; |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* Resync with __rxrpc_disconnect_call() and check that the last call |
| 114 | * didn't get advanced whilst we were filling out the packets. |
| 115 | */ |
| 116 | smp_rmb(); |
| 117 | if (READ_ONCE(chan->last_call) != call_id) |
| 118 | return; |
| 119 | |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 120 | serial = atomic_inc_return(&conn->serial); |
| 121 | pkt.whdr.serial = htonl(serial); |
| 122 | |
| 123 | switch (chan->last_type) { |
| 124 | case RXRPC_PACKET_TYPE_ABORT: |
David Howells | 6475309 | 2018-10-08 15:46:17 +0100 | [diff] [blame] | 125 | _proto("Tx ABORT %%%u { %d } [re]", serial, conn->abort_code); |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 126 | break; |
| 127 | case RXRPC_PACKET_TYPE_ACK: |
David Howells | 4764c0d | 2018-07-23 17:18:37 +0100 | [diff] [blame] | 128 | trace_rxrpc_tx_ack(chan->call_debug_id, serial, |
David Howells | f3f8337 | 2018-07-23 17:18:36 +0100 | [diff] [blame] | 129 | ntohl(pkt.ack.firstPacket), |
| 130 | ntohl(pkt.ack.serial), |
| 131 | pkt.ack.reason, 0); |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 132 | _proto("Tx ACK %%%u [re]", serial); |
| 133 | break; |
| 134 | } |
| 135 | |
David Howells | 6b47fe1 | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 136 | ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, ioc, len); |
David Howells | 330bdcf | 2018-08-08 11:30:02 +0100 | [diff] [blame] | 137 | conn->params.peer->last_tx_at = ktime_get_seconds(); |
David Howells | 6b47fe1 | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 138 | if (ret < 0) |
David Howells | 4764c0d | 2018-07-23 17:18:37 +0100 | [diff] [blame] | 139 | trace_rxrpc_tx_fail(chan->call_debug_id, serial, ret, |
| 140 | rxrpc_tx_point_call_final_resend); |
| 141 | else |
| 142 | trace_rxrpc_tx_packet(chan->call_debug_id, &pkt.whdr, |
| 143 | rxrpc_tx_point_call_final_resend); |
David Howells | 6b47fe1 | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 144 | |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 145 | _leave(""); |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 149 | * pass a connection-level abort onto all calls on that connection |
| 150 | */ |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 151 | static void rxrpc_abort_calls(struct rxrpc_connection *conn, |
David Howells | 39ce675 | 2019-04-12 16:34:09 +0100 | [diff] [blame] | 152 | enum rxrpc_call_completion compl, |
| 153 | rxrpc_serial_t serial) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 154 | { |
| 155 | struct rxrpc_call *call; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 156 | int i; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 157 | |
David Howells | 6475309 | 2018-10-08 15:46:17 +0100 | [diff] [blame] | 158 | _enter("{%d},%x", conn->debug_id, conn->abort_code); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 159 | |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 160 | spin_lock(&conn->channel_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 161 | |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 162 | for (i = 0; i < RXRPC_MAXCALLS; i++) { |
| 163 | call = rcu_dereference_protected( |
| 164 | conn->channels[i].call, |
| 165 | lockdep_is_held(&conn->channel_lock)); |
David Howells | ccbd3db | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 166 | if (call) { |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 167 | if (compl == RXRPC_CALL_LOCALLY_ABORTED) |
David Howells | a25e21f | 2018-03-27 23:03:00 +0100 | [diff] [blame] | 168 | trace_rxrpc_abort(call->debug_id, |
| 169 | "CON", call->cid, |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 170 | call->call_id, 0, |
David Howells | 6475309 | 2018-10-08 15:46:17 +0100 | [diff] [blame] | 171 | conn->abort_code, |
| 172 | conn->error); |
David Howells | 39ce675 | 2019-04-12 16:34:09 +0100 | [diff] [blame] | 173 | else |
| 174 | trace_rxrpc_rx_abort(call, serial, |
| 175 | conn->abort_code); |
David Howells | 5ac0d62 | 2020-06-03 22:21:16 +0100 | [diff] [blame] | 176 | rxrpc_set_call_completion(call, compl, |
| 177 | conn->abort_code, |
| 178 | conn->error); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 179 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 180 | } |
| 181 | |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 182 | spin_unlock(&conn->channel_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 183 | _leave(""); |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * generate a connection-level abort |
| 188 | */ |
| 189 | static int rxrpc_abort_connection(struct rxrpc_connection *conn, |
David Howells | 3a92789 | 2017-04-06 10:11:56 +0100 | [diff] [blame] | 190 | int error, u32 abort_code) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 191 | { |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 192 | struct rxrpc_wire_header whdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 193 | struct msghdr msg; |
| 194 | struct kvec iov[2]; |
| 195 | __be32 word; |
| 196 | size_t len; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 197 | u32 serial; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 198 | int ret; |
| 199 | |
| 200 | _enter("%d,,%u,%u", conn->debug_id, error, abort_code); |
| 201 | |
| 202 | /* generate a connection-level abort */ |
| 203 | spin_lock_bh(&conn->state_lock); |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 204 | if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 205 | spin_unlock_bh(&conn->state_lock); |
| 206 | _leave(" = 0 [already dead]"); |
| 207 | return 0; |
| 208 | } |
| 209 | |
David Howells | 6475309 | 2018-10-08 15:46:17 +0100 | [diff] [blame] | 210 | conn->error = error; |
| 211 | conn->abort_code = abort_code; |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 212 | conn->state = RXRPC_CONN_LOCALLY_ABORTED; |
| 213 | spin_unlock_bh(&conn->state_lock); |
| 214 | |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 215 | msg.msg_name = &conn->params.peer->srx.transport; |
| 216 | msg.msg_namelen = conn->params.peer->srx.transport_len; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 217 | msg.msg_control = NULL; |
| 218 | msg.msg_controllen = 0; |
| 219 | msg.msg_flags = 0; |
| 220 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 221 | whdr.epoch = htonl(conn->proto.epoch); |
| 222 | whdr.cid = htonl(conn->proto.cid); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 223 | whdr.callNumber = 0; |
| 224 | whdr.seq = 0; |
| 225 | whdr.type = RXRPC_PACKET_TYPE_ABORT; |
| 226 | whdr.flags = conn->out_clientflag; |
| 227 | whdr.userStatus = 0; |
| 228 | whdr.securityIndex = conn->security_ix; |
| 229 | whdr._rsvd = 0; |
David Howells | 68d6d1a | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 230 | whdr.serviceId = htons(conn->service_id); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 231 | |
David Howells | 6475309 | 2018-10-08 15:46:17 +0100 | [diff] [blame] | 232 | word = htonl(conn->abort_code); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 233 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 234 | iov[0].iov_base = &whdr; |
| 235 | iov[0].iov_len = sizeof(whdr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 236 | iov[1].iov_base = &word; |
| 237 | iov[1].iov_len = sizeof(word); |
| 238 | |
| 239 | len = iov[0].iov_len + iov[1].iov_len; |
| 240 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 241 | serial = atomic_inc_return(&conn->serial); |
David Howells | 39ce675 | 2019-04-12 16:34:09 +0100 | [diff] [blame] | 242 | rxrpc_abort_calls(conn, RXRPC_CALL_LOCALLY_ABORTED, serial); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 243 | whdr.serial = htonl(serial); |
David Howells | 6475309 | 2018-10-08 15:46:17 +0100 | [diff] [blame] | 244 | _proto("Tx CONN ABORT %%%u { %d }", serial, conn->abort_code); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 245 | |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 246 | ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 247 | if (ret < 0) { |
David Howells | 6b47fe1 | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 248 | trace_rxrpc_tx_fail(conn->debug_id, serial, ret, |
David Howells | 4764c0d | 2018-07-23 17:18:37 +0100 | [diff] [blame] | 249 | rxrpc_tx_point_conn_abort); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 250 | _debug("sendmsg failed: %d", ret); |
| 251 | return -EAGAIN; |
| 252 | } |
| 253 | |
David Howells | 4764c0d | 2018-07-23 17:18:37 +0100 | [diff] [blame] | 254 | trace_rxrpc_tx_packet(conn->debug_id, &whdr, rxrpc_tx_point_conn_abort); |
| 255 | |
David Howells | 330bdcf | 2018-08-08 11:30:02 +0100 | [diff] [blame] | 256 | conn->params.peer->last_tx_at = ktime_get_seconds(); |
David Howells | ace45be | 2018-03-30 21:04:43 +0100 | [diff] [blame] | 257 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 258 | _leave(" = 0"); |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * mark a call as being on a now-secured channel |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 264 | * - must be called with BH's disabled. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 265 | */ |
Roel Kluin | 5eaa65b | 2008-12-10 15:18:31 -0800 | [diff] [blame] | 266 | static void rxrpc_call_is_secure(struct rxrpc_call *call) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 267 | { |
| 268 | _enter("%p", call); |
| 269 | if (call) { |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 270 | write_lock_bh(&call->state_lock); |
| 271 | if (call->state == RXRPC_CALL_SERVER_SECURING) { |
| 272 | call->state = RXRPC_CALL_SERVER_ACCEPTING; |
| 273 | rxrpc_notify_socket(call); |
| 274 | } |
| 275 | write_unlock_bh(&call->state_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * connection-level Rx packet processor |
| 281 | */ |
| 282 | static int rxrpc_process_event(struct rxrpc_connection *conn, |
| 283 | struct sk_buff *skb, |
| 284 | u32 *_abort_code) |
| 285 | { |
| 286 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 287 | __be32 wtmp; |
| 288 | u32 abort_code; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 289 | int loop, ret; |
| 290 | |
David Howells | 519d256 | 2009-06-16 21:36:44 +0100 | [diff] [blame] | 291 | if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) { |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 292 | _leave(" = -ECONNABORTED [%u]", conn->state); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 293 | return -ECONNABORTED; |
David Howells | 519d256 | 2009-06-16 21:36:44 +0100 | [diff] [blame] | 294 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 295 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 296 | _enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, sp->hdr.serial); |
David Howells | 519d256 | 2009-06-16 21:36:44 +0100 | [diff] [blame] | 297 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 298 | switch (sp->hdr.type) { |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 299 | case RXRPC_PACKET_TYPE_DATA: |
| 300 | case RXRPC_PACKET_TYPE_ACK: |
David Howells | 3136ef4 | 2017-11-24 10:18:41 +0000 | [diff] [blame] | 301 | rxrpc_conn_retransmit_call(conn, skb, |
| 302 | sp->hdr.cid & RXRPC_CHANNELMASK); |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 303 | return 0; |
| 304 | |
David Howells | 4d4a6ac | 2017-03-16 16:27:10 +0000 | [diff] [blame] | 305 | case RXRPC_PACKET_TYPE_BUSY: |
| 306 | /* Just ignore BUSY packets for now. */ |
| 307 | return 0; |
| 308 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 309 | case RXRPC_PACKET_TYPE_ABORT: |
David Howells | 775e5b7 | 2016-09-30 13:26:03 +0100 | [diff] [blame] | 310 | if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header), |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 311 | &wtmp, sizeof(wtmp)) < 0) { |
| 312 | trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, |
| 313 | tracepoint_string("bad_abort")); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 314 | return -EPROTO; |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 315 | } |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 316 | abort_code = ntohl(wtmp); |
| 317 | _proto("Rx ABORT %%%u { ac=%d }", sp->hdr.serial, abort_code); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 318 | |
David Howells | 6475309 | 2018-10-08 15:46:17 +0100 | [diff] [blame] | 319 | conn->error = -ECONNABORTED; |
| 320 | conn->abort_code = abort_code; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 321 | conn->state = RXRPC_CONN_REMOTELY_ABORTED; |
David Howells | 39ce675 | 2019-04-12 16:34:09 +0100 | [diff] [blame] | 322 | rxrpc_abort_calls(conn, RXRPC_CALL_REMOTELY_ABORTED, sp->hdr.serial); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 323 | return -ECONNABORTED; |
| 324 | |
| 325 | case RXRPC_PACKET_TYPE_CHALLENGE: |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 326 | return conn->security->respond_to_challenge(conn, skb, |
| 327 | _abort_code); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 328 | |
| 329 | case RXRPC_PACKET_TYPE_RESPONSE: |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 330 | ret = conn->security->verify_response(conn, skb, _abort_code); |
| 331 | if (ret < 0) |
| 332 | return ret; |
| 333 | |
| 334 | ret = conn->security->init_connection_security(conn); |
| 335 | if (ret < 0) |
| 336 | return ret; |
| 337 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 338 | ret = conn->security->prime_packet_security(conn); |
| 339 | if (ret < 0) |
| 340 | return ret; |
| 341 | |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 342 | spin_lock(&conn->channel_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 343 | spin_lock(&conn->state_lock); |
| 344 | |
David Howells | bba304d | 2016-06-27 10:32:02 +0100 | [diff] [blame] | 345 | if (conn->state == RXRPC_CONN_SERVICE_CHALLENGING) { |
| 346 | conn->state = RXRPC_CONN_SERVICE; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 347 | spin_unlock(&conn->state_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 348 | for (loop = 0; loop < RXRPC_MAXCALLS; loop++) |
David Howells | dee4636 | 2016-06-27 17:11:19 +0100 | [diff] [blame] | 349 | rxrpc_call_is_secure( |
| 350 | rcu_dereference_protected( |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 351 | conn->channels[loop].call, |
| 352 | lockdep_is_held(&conn->channel_lock))); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 353 | } else { |
| 354 | spin_unlock(&conn->state_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 355 | } |
| 356 | |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 357 | spin_unlock(&conn->channel_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 358 | return 0; |
| 359 | |
| 360 | default: |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 361 | trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, |
| 362 | tracepoint_string("bad_conn_pkt")); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 363 | return -EPROTO; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * set up security and issue a challenge |
| 369 | */ |
| 370 | static void rxrpc_secure_connection(struct rxrpc_connection *conn) |
| 371 | { |
| 372 | u32 abort_code; |
| 373 | int ret; |
| 374 | |
| 375 | _enter("{%d}", conn->debug_id); |
| 376 | |
| 377 | ASSERT(conn->security_ix != 0); |
David Howells | 063c60d | 2019-12-20 16:17:16 +0000 | [diff] [blame] | 378 | ASSERT(conn->server_key); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 379 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 380 | if (conn->security->issue_challenge(conn) < 0) { |
| 381 | abort_code = RX_CALL_DEAD; |
| 382 | ret = -ENOMEM; |
| 383 | goto abort; |
| 384 | } |
| 385 | |
| 386 | _leave(""); |
| 387 | return; |
| 388 | |
| 389 | abort: |
| 390 | _debug("abort %d, %d", ret, abort_code); |
David Howells | 3a92789 | 2017-04-06 10:11:56 +0100 | [diff] [blame] | 391 | rxrpc_abort_connection(conn, ret, abort_code); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 392 | _leave(" [aborted]"); |
| 393 | } |
| 394 | |
| 395 | /* |
David Howells | 3136ef4 | 2017-11-24 10:18:41 +0000 | [diff] [blame] | 396 | * Process delayed final ACKs that we haven't subsumed into a subsequent call. |
| 397 | */ |
| 398 | static void rxrpc_process_delayed_final_acks(struct rxrpc_connection *conn) |
| 399 | { |
| 400 | unsigned long j = jiffies, next_j; |
| 401 | unsigned int channel; |
| 402 | bool set; |
| 403 | |
| 404 | again: |
| 405 | next_j = j + LONG_MAX; |
| 406 | set = false; |
| 407 | for (channel = 0; channel < RXRPC_MAXCALLS; channel++) { |
| 408 | struct rxrpc_channel *chan = &conn->channels[channel]; |
| 409 | unsigned long ack_at; |
| 410 | |
| 411 | if (!test_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags)) |
| 412 | continue; |
| 413 | |
| 414 | smp_rmb(); /* vs rxrpc_disconnect_client_call */ |
| 415 | ack_at = READ_ONCE(chan->final_ack_at); |
| 416 | |
| 417 | if (time_before(j, ack_at)) { |
| 418 | if (time_before(ack_at, next_j)) { |
| 419 | next_j = ack_at; |
| 420 | set = true; |
| 421 | } |
| 422 | continue; |
| 423 | } |
| 424 | |
| 425 | if (test_and_clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel, |
| 426 | &conn->flags)) |
| 427 | rxrpc_conn_retransmit_call(conn, NULL, channel); |
| 428 | } |
| 429 | |
| 430 | j = jiffies; |
| 431 | if (time_before_eq(next_j, j)) |
| 432 | goto again; |
| 433 | if (set) |
| 434 | rxrpc_reduce_conn_timer(conn, next_j); |
| 435 | } |
| 436 | |
| 437 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 438 | * connection-level event processor |
| 439 | */ |
David Howells | 04d36d7 | 2020-01-30 21:50:36 +0000 | [diff] [blame] | 440 | static void rxrpc_do_process_connection(struct rxrpc_connection *conn) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 441 | { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 442 | struct sk_buff *skb; |
| 443 | u32 abort_code = RX_PROTOCOL_ERROR; |
| 444 | int ret; |
| 445 | |
David Howells | 2c4579e | 2016-06-27 10:32:03 +0100 | [diff] [blame] | 446 | if (test_and_clear_bit(RXRPC_CONN_EV_CHALLENGE, &conn->events)) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 447 | rxrpc_secure_connection(conn); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 448 | |
David Howells | 3136ef4 | 2017-11-24 10:18:41 +0000 | [diff] [blame] | 449 | /* Process delayed ACKs whose time has come. */ |
| 450 | if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK) |
| 451 | rxrpc_process_delayed_final_acks(conn); |
| 452 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 453 | /* go through the conn-level event packets, releasing the ref on this |
| 454 | * connection that each one has when we've finished with it */ |
| 455 | while ((skb = skb_dequeue(&conn->rx_queue))) { |
David Howells | 987db9f | 2019-08-19 09:25:38 +0100 | [diff] [blame] | 456 | rxrpc_see_skb(skb, rxrpc_skb_seen); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 457 | ret = rxrpc_process_event(conn, skb, &abort_code); |
| 458 | switch (ret) { |
| 459 | case -EPROTO: |
| 460 | case -EKEYEXPIRED: |
| 461 | case -EKEYREJECTED: |
| 462 | goto protocol_error; |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 463 | case -ENOMEM: |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 464 | case -EAGAIN: |
| 465 | goto requeue_and_leave; |
| 466 | case -ECONNABORTED: |
| 467 | default: |
David Howells | 987db9f | 2019-08-19 09:25:38 +0100 | [diff] [blame] | 468 | rxrpc_free_skb(skb, rxrpc_skb_freed); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 469 | break; |
| 470 | } |
| 471 | } |
| 472 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 473 | return; |
| 474 | |
| 475 | requeue_and_leave: |
| 476 | skb_queue_head(&conn->rx_queue, skb); |
David Howells | 04d36d7 | 2020-01-30 21:50:36 +0000 | [diff] [blame] | 477 | return; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 478 | |
| 479 | protocol_error: |
David Howells | 3a92789 | 2017-04-06 10:11:56 +0100 | [diff] [blame] | 480 | if (rxrpc_abort_connection(conn, ret, abort_code) < 0) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 481 | goto requeue_and_leave; |
David Howells | 987db9f | 2019-08-19 09:25:38 +0100 | [diff] [blame] | 482 | rxrpc_free_skb(skb, rxrpc_skb_freed); |
David Howells | 04d36d7 | 2020-01-30 21:50:36 +0000 | [diff] [blame] | 483 | return; |
| 484 | } |
| 485 | |
| 486 | void rxrpc_process_connection(struct work_struct *work) |
| 487 | { |
| 488 | struct rxrpc_connection *conn = |
| 489 | container_of(work, struct rxrpc_connection, processor); |
| 490 | |
| 491 | rxrpc_see_connection(conn); |
| 492 | |
| 493 | if (__rxrpc_use_local(conn->params.local)) { |
| 494 | rxrpc_do_process_connection(conn); |
| 495 | rxrpc_unuse_local(conn->params.local); |
| 496 | } |
| 497 | |
| 498 | rxrpc_put_connection(conn); |
| 499 | _leave(""); |
| 500 | return; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 501 | } |