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