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