1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* connection-level event handling 3 * 4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9 10 #include <linux/module.h> 11 #include <linux/net.h> 12 #include <linux/skbuff.h> 13 #include <linux/errqueue.h> 14 #include <net/sock.h> 15 #include <net/af_rxrpc.h> 16 #include <net/ip.h> 17 #include "ar-internal.h" 18 19 /* 20 * Set the completion state on an aborted connection. 21 */ 22 static bool rxrpc_set_conn_aborted(struct rxrpc_connection *conn, struct sk_buff *skb, 23 s32 abort_code, int err, 24 enum rxrpc_call_completion compl) 25 { 26 bool aborted = false; 27 28 if (conn->state != RXRPC_CONN_ABORTED) { 29 spin_lock(&conn->state_lock); 30 if (conn->state != RXRPC_CONN_ABORTED) { 31 conn->abort_code = abort_code; 32 conn->error = err; 33 conn->completion = compl; 34 /* Order the abort info before the state change. */ 35 smp_store_release(&conn->state, RXRPC_CONN_ABORTED); 36 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags); 37 set_bit(RXRPC_CONN_EV_ABORT_CALLS, &conn->events); 38 aborted = true; 39 } 40 spin_unlock(&conn->state_lock); 41 } 42 43 return aborted; 44 } 45 46 /* 47 * Mark a socket buffer to indicate that the connection it's on should be aborted. 48 */ 49 int rxrpc_abort_conn(struct rxrpc_connection *conn, struct sk_buff *skb, 50 s32 abort_code, int err, enum rxrpc_abort_reason why) 51 { 52 struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 53 54 if (rxrpc_set_conn_aborted(conn, skb, abort_code, err, 55 RXRPC_CALL_LOCALLY_ABORTED)) { 56 trace_rxrpc_abort(0, why, sp->hdr.cid, sp->hdr.callNumber, 57 sp->hdr.seq, abort_code, err); 58 rxrpc_poke_conn(conn, rxrpc_conn_get_poke_abort); 59 } 60 return -EPROTO; 61 } 62 63 /* 64 * Mark a connection as being remotely aborted. 65 */ 66 static bool rxrpc_input_conn_abort(struct rxrpc_connection *conn, 67 struct sk_buff *skb) 68 { 69 return rxrpc_set_conn_aborted(conn, skb, skb->priority, -ECONNABORTED, 70 RXRPC_CALL_REMOTELY_ABORTED); 71 } 72 73 /* 74 * Retransmit terminal ACK or ABORT of the previous call. 75 */ 76 void rxrpc_conn_retransmit_call(struct rxrpc_connection *conn, 77 struct sk_buff *skb, 78 unsigned int channel) 79 { 80 struct rxrpc_skb_priv *sp = skb ? rxrpc_skb(skb) : NULL; 81 struct rxrpc_channel *chan; 82 struct msghdr msg; 83 struct kvec iov[3]; 84 struct { 85 struct rxrpc_wire_header whdr; 86 union { 87 __be32 abort_code; 88 struct rxrpc_ackpacket ack; 89 }; 90 } __attribute__((packed)) pkt; 91 struct rxrpc_ackinfo ack_info; 92 size_t len; 93 int ret, ioc; 94 u32 serial, mtu, call_id, padding; 95 96 _enter("%d", conn->debug_id); 97 98 chan = &conn->channels[channel]; 99 100 /* If the last call got moved on whilst we were waiting to run, just 101 * ignore this packet. 102 */ 103 call_id = chan->last_call; 104 if (skb && call_id != sp->hdr.callNumber) 105 return; 106 107 msg.msg_name = &conn->peer->srx.transport; 108 msg.msg_namelen = conn->peer->srx.transport_len; 109 msg.msg_control = NULL; 110 msg.msg_controllen = 0; 111 msg.msg_flags = 0; 112 113 iov[0].iov_base = &pkt; 114 iov[0].iov_len = sizeof(pkt.whdr); 115 iov[1].iov_base = &padding; 116 iov[1].iov_len = 3; 117 iov[2].iov_base = &ack_info; 118 iov[2].iov_len = sizeof(ack_info); 119 120 serial = atomic_inc_return(&conn->serial); 121 122 pkt.whdr.epoch = htonl(conn->proto.epoch); 123 pkt.whdr.cid = htonl(conn->proto.cid | channel); 124 pkt.whdr.callNumber = htonl(call_id); 125 pkt.whdr.serial = htonl(serial); 126 pkt.whdr.seq = 0; 127 pkt.whdr.type = chan->last_type; 128 pkt.whdr.flags = conn->out_clientflag; 129 pkt.whdr.userStatus = 0; 130 pkt.whdr.securityIndex = conn->security_ix; 131 pkt.whdr._rsvd = 0; 132 pkt.whdr.serviceId = htons(conn->service_id); 133 134 len = sizeof(pkt.whdr); 135 switch (chan->last_type) { 136 case RXRPC_PACKET_TYPE_ABORT: 137 pkt.abort_code = htonl(chan->last_abort); 138 iov[0].iov_len += sizeof(pkt.abort_code); 139 len += sizeof(pkt.abort_code); 140 ioc = 1; 141 break; 142 143 case RXRPC_PACKET_TYPE_ACK: 144 mtu = conn->peer->if_mtu; 145 mtu -= conn->peer->hdrsize; 146 pkt.ack.bufferSpace = 0; 147 pkt.ack.maxSkew = htons(skb ? skb->priority : 0); 148 pkt.ack.firstPacket = htonl(chan->last_seq + 1); 149 pkt.ack.previousPacket = htonl(chan->last_seq); 150 pkt.ack.serial = htonl(skb ? sp->hdr.serial : 0); 151 pkt.ack.reason = skb ? RXRPC_ACK_DUPLICATE : RXRPC_ACK_IDLE; 152 pkt.ack.nAcks = 0; 153 ack_info.rxMTU = htonl(rxrpc_rx_mtu); 154 ack_info.maxMTU = htonl(mtu); 155 ack_info.rwind = htonl(rxrpc_rx_window_size); 156 ack_info.jumbo_max = htonl(rxrpc_rx_jumbo_max); 157 pkt.whdr.flags |= RXRPC_SLOW_START_OK; 158 padding = 0; 159 iov[0].iov_len += sizeof(pkt.ack); 160 len += sizeof(pkt.ack) + 3 + sizeof(ack_info); 161 ioc = 3; 162 163 trace_rxrpc_tx_ack(chan->call_debug_id, serial, 164 ntohl(pkt.ack.firstPacket), 165 ntohl(pkt.ack.serial), 166 pkt.ack.reason, 0, rxrpc_rx_window_size); 167 break; 168 169 default: 170 return; 171 } 172 173 ret = kernel_sendmsg(conn->local->socket, &msg, iov, ioc, len); 174 conn->peer->last_tx_at = ktime_get_seconds(); 175 if (ret < 0) 176 trace_rxrpc_tx_fail(chan->call_debug_id, serial, ret, 177 rxrpc_tx_point_call_final_resend); 178 else 179 trace_rxrpc_tx_packet(chan->call_debug_id, &pkt.whdr, 180 rxrpc_tx_point_call_final_resend); 181 182 _leave(""); 183 } 184 185 /* 186 * pass a connection-level abort onto all calls on that connection 187 */ 188 static void rxrpc_abort_calls(struct rxrpc_connection *conn) 189 { 190 struct rxrpc_call *call; 191 int i; 192 193 _enter("{%d},%x", conn->debug_id, conn->abort_code); 194 195 for (i = 0; i < RXRPC_MAXCALLS; i++) { 196 call = conn->channels[i].call; 197 if (call) 198 rxrpc_set_call_completion(call, 199 conn->completion, 200 conn->abort_code, 201 conn->error); 202 } 203 204 _leave(""); 205 } 206 207 /* 208 * mark a call as being on a now-secured channel 209 * - must be called with BH's disabled. 210 */ 211 static void rxrpc_call_is_secure(struct rxrpc_call *call) 212 { 213 if (call && __rxrpc_call_state(call) == RXRPC_CALL_SERVER_SECURING) { 214 rxrpc_set_call_state(call, RXRPC_CALL_SERVER_RECV_REQUEST); 215 rxrpc_notify_socket(call); 216 } 217 } 218 219 /* 220 * connection-level Rx packet processor 221 */ 222 static int rxrpc_process_event(struct rxrpc_connection *conn, 223 struct sk_buff *skb) 224 { 225 struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 226 int ret; 227 228 if (conn->state == RXRPC_CONN_ABORTED) 229 return -ECONNABORTED; 230 231 _enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, sp->hdr.serial); 232 233 switch (sp->hdr.type) { 234 case RXRPC_PACKET_TYPE_CHALLENGE: 235 return conn->security->respond_to_challenge(conn, skb); 236 237 case RXRPC_PACKET_TYPE_RESPONSE: 238 ret = conn->security->verify_response(conn, skb); 239 if (ret < 0) 240 return ret; 241 242 ret = conn->security->init_connection_security( 243 conn, conn->key->payload.data[0]); 244 if (ret < 0) 245 return ret; 246 247 spin_lock(&conn->state_lock); 248 if (conn->state == RXRPC_CONN_SERVICE_CHALLENGING) 249 conn->state = RXRPC_CONN_SERVICE; 250 spin_unlock(&conn->state_lock); 251 252 if (conn->state == RXRPC_CONN_SERVICE) { 253 /* Offload call state flipping to the I/O thread. As 254 * we've already received the packet, put it on the 255 * front of the queue. 256 */ 257 skb->mark = RXRPC_SKB_MARK_SERVICE_CONN_SECURED; 258 rxrpc_get_skb(skb, rxrpc_skb_get_conn_secured); 259 skb_queue_head(&conn->local->rx_queue, skb); 260 rxrpc_wake_up_io_thread(conn->local); 261 } 262 return 0; 263 264 default: 265 WARN_ON_ONCE(1); 266 return -EPROTO; 267 } 268 } 269 270 /* 271 * set up security and issue a challenge 272 */ 273 static void rxrpc_secure_connection(struct rxrpc_connection *conn) 274 { 275 if (conn->security->issue_challenge(conn) < 0) 276 rxrpc_abort_conn(conn, NULL, RX_CALL_DEAD, -ENOMEM, 277 rxrpc_abort_nomem); 278 } 279 280 /* 281 * Process delayed final ACKs that we haven't subsumed into a subsequent call. 282 */ 283 void rxrpc_process_delayed_final_acks(struct rxrpc_connection *conn, bool force) 284 { 285 unsigned long j = jiffies, next_j; 286 unsigned int channel; 287 bool set; 288 289 again: 290 next_j = j + LONG_MAX; 291 set = false; 292 for (channel = 0; channel < RXRPC_MAXCALLS; channel++) { 293 struct rxrpc_channel *chan = &conn->channels[channel]; 294 unsigned long ack_at; 295 296 if (!test_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags)) 297 continue; 298 299 ack_at = chan->final_ack_at; 300 if (time_before(j, ack_at) && !force) { 301 if (time_before(ack_at, next_j)) { 302 next_j = ack_at; 303 set = true; 304 } 305 continue; 306 } 307 308 if (test_and_clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel, 309 &conn->flags)) 310 rxrpc_conn_retransmit_call(conn, NULL, channel); 311 } 312 313 j = jiffies; 314 if (time_before_eq(next_j, j)) 315 goto again; 316 if (set) 317 rxrpc_reduce_conn_timer(conn, next_j); 318 } 319 320 /* 321 * connection-level event processor 322 */ 323 static void rxrpc_do_process_connection(struct rxrpc_connection *conn) 324 { 325 struct sk_buff *skb; 326 int ret; 327 328 if (test_and_clear_bit(RXRPC_CONN_EV_CHALLENGE, &conn->events)) 329 rxrpc_secure_connection(conn); 330 331 /* go through the conn-level event packets, releasing the ref on this 332 * connection that each one has when we've finished with it */ 333 while ((skb = skb_dequeue(&conn->rx_queue))) { 334 rxrpc_see_skb(skb, rxrpc_skb_see_conn_work); 335 ret = rxrpc_process_event(conn, skb); 336 switch (ret) { 337 case -ENOMEM: 338 case -EAGAIN: 339 skb_queue_head(&conn->rx_queue, skb); 340 rxrpc_queue_conn(conn, rxrpc_conn_queue_retry_work); 341 break; 342 default: 343 rxrpc_free_skb(skb, rxrpc_skb_put_conn_work); 344 break; 345 } 346 } 347 } 348 349 void rxrpc_process_connection(struct work_struct *work) 350 { 351 struct rxrpc_connection *conn = 352 container_of(work, struct rxrpc_connection, processor); 353 354 rxrpc_see_connection(conn, rxrpc_conn_see_work); 355 356 if (__rxrpc_use_local(conn->local, rxrpc_local_use_conn_work)) { 357 rxrpc_do_process_connection(conn); 358 rxrpc_unuse_local(conn->local, rxrpc_local_unuse_conn_work); 359 } 360 } 361 362 /* 363 * post connection-level events to the connection 364 * - this includes challenges, responses, some aborts and call terminal packet 365 * retransmission. 366 */ 367 static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn, 368 struct sk_buff *skb) 369 { 370 _enter("%p,%p", conn, skb); 371 372 rxrpc_get_skb(skb, rxrpc_skb_get_conn_work); 373 skb_queue_tail(&conn->rx_queue, skb); 374 rxrpc_queue_conn(conn, rxrpc_conn_queue_rx_work); 375 } 376 377 /* 378 * Input a connection-level packet. 379 */ 380 bool rxrpc_input_conn_packet(struct rxrpc_connection *conn, struct sk_buff *skb) 381 { 382 struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 383 384 switch (sp->hdr.type) { 385 case RXRPC_PACKET_TYPE_BUSY: 386 /* Just ignore BUSY packets for now. */ 387 return true; 388 389 case RXRPC_PACKET_TYPE_ABORT: 390 if (rxrpc_is_conn_aborted(conn)) 391 return true; 392 rxrpc_input_conn_abort(conn, skb); 393 rxrpc_abort_calls(conn); 394 return true; 395 396 case RXRPC_PACKET_TYPE_CHALLENGE: 397 case RXRPC_PACKET_TYPE_RESPONSE: 398 if (rxrpc_is_conn_aborted(conn)) { 399 if (conn->completion == RXRPC_CALL_LOCALLY_ABORTED) 400 rxrpc_send_conn_abort(conn); 401 return true; 402 } 403 rxrpc_post_packet_to_conn(conn, skb); 404 return true; 405 406 default: 407 WARN_ON_ONCE(1); 408 return true; 409 } 410 } 411 412 /* 413 * Input a connection event. 414 */ 415 void rxrpc_input_conn_event(struct rxrpc_connection *conn, struct sk_buff *skb) 416 { 417 unsigned int loop; 418 419 if (test_and_clear_bit(RXRPC_CONN_EV_ABORT_CALLS, &conn->events)) 420 rxrpc_abort_calls(conn); 421 422 switch (skb->mark) { 423 case RXRPC_SKB_MARK_SERVICE_CONN_SECURED: 424 if (conn->state != RXRPC_CONN_SERVICE) 425 break; 426 427 for (loop = 0; loop < RXRPC_MAXCALLS; loop++) 428 rxrpc_call_is_secure(conn->channels[loop].call); 429 break; 430 } 431 432 /* Process delayed ACKs whose time has come. */ 433 if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK) 434 rxrpc_process_delayed_final_acks(conn, false); 435 } 436