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->local_abort); 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 u32 abort_code, int error) 158 { 159 struct rxrpc_call *call; 160 int i; 161 162 _enter("{%d},%x", conn->debug_id, 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 abort_code, error); 176 if (rxrpc_set_call_completion(call, compl, 177 abort_code, error)) 178 rxrpc_notify_socket(call); 179 } 180 } 181 182 spin_unlock(&conn->channel_lock); 183 _leave(""); 184 } 185 186 /* 187 * generate a connection-level abort 188 */ 189 static int rxrpc_abort_connection(struct rxrpc_connection *conn, 190 int error, u32 abort_code) 191 { 192 struct rxrpc_wire_header whdr; 193 struct msghdr msg; 194 struct kvec iov[2]; 195 __be32 word; 196 size_t len; 197 u32 serial; 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); 204 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) { 205 spin_unlock_bh(&conn->state_lock); 206 _leave(" = 0 [already dead]"); 207 return 0; 208 } 209 210 conn->state = RXRPC_CONN_LOCALLY_ABORTED; 211 spin_unlock_bh(&conn->state_lock); 212 213 rxrpc_abort_calls(conn, RXRPC_CALL_LOCALLY_ABORTED, abort_code, error); 214 215 msg.msg_name = &conn->params.peer->srx.transport; 216 msg.msg_namelen = conn->params.peer->srx.transport_len; 217 msg.msg_control = NULL; 218 msg.msg_controllen = 0; 219 msg.msg_flags = 0; 220 221 whdr.epoch = htonl(conn->proto.epoch); 222 whdr.cid = htonl(conn->proto.cid); 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; 230 whdr.serviceId = htons(conn->service_id); 231 232 word = htonl(conn->local_abort); 233 234 iov[0].iov_base = &whdr; 235 iov[0].iov_len = sizeof(whdr); 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 241 serial = atomic_inc_return(&conn->serial); 242 whdr.serial = htonl(serial); 243 _proto("Tx CONN ABORT %%%u { %d }", serial, conn->local_abort); 244 245 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len); 246 if (ret < 0) { 247 trace_rxrpc_tx_fail(conn->debug_id, serial, ret, 248 rxrpc_tx_point_conn_abort); 249 _debug("sendmsg failed: %d", ret); 250 return -EAGAIN; 251 } 252 253 trace_rxrpc_tx_packet(conn->debug_id, &whdr, rxrpc_tx_point_conn_abort); 254 255 conn->params.peer->last_tx_at = ktime_get_seconds(); 256 257 _leave(" = 0"); 258 return 0; 259 } 260 261 /* 262 * mark a call as being on a now-secured channel 263 * - must be called with BH's disabled. 264 */ 265 static void rxrpc_call_is_secure(struct rxrpc_call *call) 266 { 267 _enter("%p", call); 268 if (call) { 269 write_lock_bh(&call->state_lock); 270 if (call->state == RXRPC_CALL_SERVER_SECURING) { 271 call->state = RXRPC_CALL_SERVER_ACCEPTING; 272 rxrpc_notify_socket(call); 273 } 274 write_unlock_bh(&call->state_lock); 275 } 276 } 277 278 /* 279 * connection-level Rx packet processor 280 */ 281 static int rxrpc_process_event(struct rxrpc_connection *conn, 282 struct sk_buff *skb, 283 u32 *_abort_code) 284 { 285 struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 286 __be32 wtmp; 287 u32 abort_code; 288 int loop, ret; 289 290 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) { 291 _leave(" = -ECONNABORTED [%u]", conn->state); 292 return -ECONNABORTED; 293 } 294 295 _enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, sp->hdr.serial); 296 297 switch (sp->hdr.type) { 298 case RXRPC_PACKET_TYPE_DATA: 299 case RXRPC_PACKET_TYPE_ACK: 300 rxrpc_conn_retransmit_call(conn, skb, 301 sp->hdr.cid & RXRPC_CHANNELMASK); 302 return 0; 303 304 case RXRPC_PACKET_TYPE_BUSY: 305 /* Just ignore BUSY packets for now. */ 306 return 0; 307 308 case RXRPC_PACKET_TYPE_ABORT: 309 if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header), 310 &wtmp, sizeof(wtmp)) < 0) { 311 trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, 312 tracepoint_string("bad_abort")); 313 return -EPROTO; 314 } 315 abort_code = ntohl(wtmp); 316 _proto("Rx ABORT %%%u { ac=%d }", sp->hdr.serial, abort_code); 317 318 conn->state = RXRPC_CONN_REMOTELY_ABORTED; 319 rxrpc_abort_calls(conn, RXRPC_CALL_REMOTELY_ABORTED, 320 abort_code, -ECONNABORTED); 321 return -ECONNABORTED; 322 323 case RXRPC_PACKET_TYPE_CHALLENGE: 324 return conn->security->respond_to_challenge(conn, skb, 325 _abort_code); 326 327 case RXRPC_PACKET_TYPE_RESPONSE: 328 ret = conn->security->verify_response(conn, skb, _abort_code); 329 if (ret < 0) 330 return ret; 331 332 ret = conn->security->init_connection_security(conn); 333 if (ret < 0) 334 return ret; 335 336 ret = conn->security->prime_packet_security(conn); 337 if (ret < 0) 338 return ret; 339 340 spin_lock(&conn->channel_lock); 341 spin_lock(&conn->state_lock); 342 343 if (conn->state == RXRPC_CONN_SERVICE_CHALLENGING) { 344 conn->state = RXRPC_CONN_SERVICE; 345 spin_unlock(&conn->state_lock); 346 for (loop = 0; loop < RXRPC_MAXCALLS; loop++) 347 rxrpc_call_is_secure( 348 rcu_dereference_protected( 349 conn->channels[loop].call, 350 lockdep_is_held(&conn->channel_lock))); 351 } else { 352 spin_unlock(&conn->state_lock); 353 } 354 355 spin_unlock(&conn->channel_lock); 356 return 0; 357 358 default: 359 trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, 360 tracepoint_string("bad_conn_pkt")); 361 return -EPROTO; 362 } 363 } 364 365 /* 366 * set up security and issue a challenge 367 */ 368 static void rxrpc_secure_connection(struct rxrpc_connection *conn) 369 { 370 u32 abort_code; 371 int ret; 372 373 _enter("{%d}", conn->debug_id); 374 375 ASSERT(conn->security_ix != 0); 376 377 if (!conn->params.key) { 378 _debug("set up security"); 379 ret = rxrpc_init_server_conn_security(conn); 380 switch (ret) { 381 case 0: 382 break; 383 case -ENOENT: 384 abort_code = RX_CALL_DEAD; 385 goto abort; 386 default: 387 abort_code = RXKADNOAUTH; 388 goto abort; 389 } 390 } 391 392 if (conn->security->issue_challenge(conn) < 0) { 393 abort_code = RX_CALL_DEAD; 394 ret = -ENOMEM; 395 goto abort; 396 } 397 398 _leave(""); 399 return; 400 401 abort: 402 _debug("abort %d, %d", ret, abort_code); 403 rxrpc_abort_connection(conn, ret, abort_code); 404 _leave(" [aborted]"); 405 } 406 407 /* 408 * Process delayed final ACKs that we haven't subsumed into a subsequent call. 409 */ 410 static void rxrpc_process_delayed_final_acks(struct rxrpc_connection *conn) 411 { 412 unsigned long j = jiffies, next_j; 413 unsigned int channel; 414 bool set; 415 416 again: 417 next_j = j + LONG_MAX; 418 set = false; 419 for (channel = 0; channel < RXRPC_MAXCALLS; channel++) { 420 struct rxrpc_channel *chan = &conn->channels[channel]; 421 unsigned long ack_at; 422 423 if (!test_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags)) 424 continue; 425 426 smp_rmb(); /* vs rxrpc_disconnect_client_call */ 427 ack_at = READ_ONCE(chan->final_ack_at); 428 429 if (time_before(j, ack_at)) { 430 if (time_before(ack_at, next_j)) { 431 next_j = ack_at; 432 set = true; 433 } 434 continue; 435 } 436 437 if (test_and_clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel, 438 &conn->flags)) 439 rxrpc_conn_retransmit_call(conn, NULL, channel); 440 } 441 442 j = jiffies; 443 if (time_before_eq(next_j, j)) 444 goto again; 445 if (set) 446 rxrpc_reduce_conn_timer(conn, next_j); 447 } 448 449 /* 450 * connection-level event processor 451 */ 452 void rxrpc_process_connection(struct work_struct *work) 453 { 454 struct rxrpc_connection *conn = 455 container_of(work, struct rxrpc_connection, processor); 456 struct sk_buff *skb; 457 u32 abort_code = RX_PROTOCOL_ERROR; 458 int ret; 459 460 rxrpc_see_connection(conn); 461 462 if (test_and_clear_bit(RXRPC_CONN_EV_CHALLENGE, &conn->events)) 463 rxrpc_secure_connection(conn); 464 465 /* Process delayed ACKs whose time has come. */ 466 if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK) 467 rxrpc_process_delayed_final_acks(conn); 468 469 /* go through the conn-level event packets, releasing the ref on this 470 * connection that each one has when we've finished with it */ 471 while ((skb = skb_dequeue(&conn->rx_queue))) { 472 rxrpc_see_skb(skb, rxrpc_skb_rx_seen); 473 ret = rxrpc_process_event(conn, skb, &abort_code); 474 switch (ret) { 475 case -EPROTO: 476 case -EKEYEXPIRED: 477 case -EKEYREJECTED: 478 goto protocol_error; 479 case -ENOMEM: 480 case -EAGAIN: 481 goto requeue_and_leave; 482 case -ECONNABORTED: 483 default: 484 rxrpc_free_skb(skb, rxrpc_skb_rx_freed); 485 break; 486 } 487 } 488 489 out: 490 rxrpc_put_connection(conn); 491 _leave(""); 492 return; 493 494 requeue_and_leave: 495 skb_queue_head(&conn->rx_queue, skb); 496 goto out; 497 498 protocol_error: 499 if (rxrpc_abort_connection(conn, ret, abort_code) < 0) 500 goto requeue_and_leave; 501 rxrpc_free_skb(skb, rxrpc_skb_rx_freed); 502 goto out; 503 } 504