1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* SCTP kernel implementation 3 * (C) Copyright IBM Corp. 2001, 2004 4 * Copyright (c) 1999 Cisco, Inc. 5 * Copyright (c) 1999-2001 Motorola, Inc. 6 * 7 * This file is part of the SCTP kernel implementation 8 * 9 * These functions work with the state functions in sctp_sm_statefuns.c 10 * to implement that state operations. These functions implement the 11 * steps which require modifying existing data structures. 12 * 13 * Please send any bug reports or fixes you make to the 14 * email address(es): 15 * lksctp developers <linux-sctp@vger.kernel.org> 16 * 17 * Written or modified by: 18 * La Monte H.P. Yarroll <piggy@acm.org> 19 * Karl Knutson <karl@athena.chicago.il.us> 20 * Jon Grimm <jgrimm@austin.ibm.com> 21 * Hui Huang <hui.huang@nokia.com> 22 * Dajiang Zhang <dajiang.zhang@nokia.com> 23 * Daisy Chang <daisyc@us.ibm.com> 24 * Sridhar Samudrala <sri@us.ibm.com> 25 * Ardelle Fan <ardelle.fan@intel.com> 26 */ 27 28 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 29 30 #include <linux/skbuff.h> 31 #include <linux/types.h> 32 #include <linux/socket.h> 33 #include <linux/ip.h> 34 #include <linux/gfp.h> 35 #include <net/sock.h> 36 #include <net/sctp/sctp.h> 37 #include <net/sctp/sm.h> 38 #include <net/sctp/stream_sched.h> 39 40 static int sctp_cmd_interpreter(enum sctp_event_type event_type, 41 union sctp_subtype subtype, 42 enum sctp_state state, 43 struct sctp_endpoint *ep, 44 struct sctp_association *asoc, 45 void *event_arg, 46 enum sctp_disposition status, 47 struct sctp_cmd_seq *commands, 48 gfp_t gfp); 49 static int sctp_side_effects(enum sctp_event_type event_type, 50 union sctp_subtype subtype, 51 enum sctp_state state, 52 struct sctp_endpoint *ep, 53 struct sctp_association **asoc, 54 void *event_arg, 55 enum sctp_disposition status, 56 struct sctp_cmd_seq *commands, 57 gfp_t gfp); 58 59 /******************************************************************** 60 * Helper functions 61 ********************************************************************/ 62 63 /* A helper function for delayed processing of INET ECN CE bit. */ 64 static void sctp_do_ecn_ce_work(struct sctp_association *asoc, 65 __u32 lowest_tsn) 66 { 67 /* Save the TSN away for comparison when we receive CWR */ 68 69 asoc->last_ecne_tsn = lowest_tsn; 70 asoc->need_ecne = 1; 71 } 72 73 /* Helper function for delayed processing of SCTP ECNE chunk. */ 74 /* RFC 2960 Appendix A 75 * 76 * RFC 2481 details a specific bit for a sender to send in 77 * the header of its next outbound TCP segment to indicate to 78 * its peer that it has reduced its congestion window. This 79 * is termed the CWR bit. For SCTP the same indication is made 80 * by including the CWR chunk. This chunk contains one data 81 * element, i.e. the TSN number that was sent in the ECNE chunk. 82 * This element represents the lowest TSN number in the datagram 83 * that was originally marked with the CE bit. 84 */ 85 static struct sctp_chunk *sctp_do_ecn_ecne_work(struct sctp_association *asoc, 86 __u32 lowest_tsn, 87 struct sctp_chunk *chunk) 88 { 89 struct sctp_chunk *repl; 90 91 /* Our previously transmitted packet ran into some congestion 92 * so we should take action by reducing cwnd and ssthresh 93 * and then ACK our peer that we we've done so by 94 * sending a CWR. 95 */ 96 97 /* First, try to determine if we want to actually lower 98 * our cwnd variables. Only lower them if the ECNE looks more 99 * recent than the last response. 100 */ 101 if (TSN_lt(asoc->last_cwr_tsn, lowest_tsn)) { 102 struct sctp_transport *transport; 103 104 /* Find which transport's congestion variables 105 * need to be adjusted. 106 */ 107 transport = sctp_assoc_lookup_tsn(asoc, lowest_tsn); 108 109 /* Update the congestion variables. */ 110 if (transport) 111 sctp_transport_lower_cwnd(transport, 112 SCTP_LOWER_CWND_ECNE); 113 asoc->last_cwr_tsn = lowest_tsn; 114 } 115 116 /* Always try to quiet the other end. In case of lost CWR, 117 * resend last_cwr_tsn. 118 */ 119 repl = sctp_make_cwr(asoc, asoc->last_cwr_tsn, chunk); 120 121 /* If we run out of memory, it will look like a lost CWR. We'll 122 * get back in sync eventually. 123 */ 124 return repl; 125 } 126 127 /* Helper function to do delayed processing of ECN CWR chunk. */ 128 static void sctp_do_ecn_cwr_work(struct sctp_association *asoc, 129 __u32 lowest_tsn) 130 { 131 /* Turn off ECNE getting auto-prepended to every outgoing 132 * packet 133 */ 134 asoc->need_ecne = 0; 135 } 136 137 /* Generate SACK if necessary. We call this at the end of a packet. */ 138 static int sctp_gen_sack(struct sctp_association *asoc, int force, 139 struct sctp_cmd_seq *commands) 140 { 141 struct sctp_transport *trans = asoc->peer.last_data_from; 142 __u32 ctsn, max_tsn_seen; 143 struct sctp_chunk *sack; 144 int error = 0; 145 146 if (force || 147 (!trans && (asoc->param_flags & SPP_SACKDELAY_DISABLE)) || 148 (trans && (trans->param_flags & SPP_SACKDELAY_DISABLE))) 149 asoc->peer.sack_needed = 1; 150 151 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map); 152 max_tsn_seen = sctp_tsnmap_get_max_tsn_seen(&asoc->peer.tsn_map); 153 154 /* From 12.2 Parameters necessary per association (i.e. the TCB): 155 * 156 * Ack State : This flag indicates if the next received packet 157 * : is to be responded to with a SACK. ... 158 * : When DATA chunks are out of order, SACK's 159 * : are not delayed (see Section 6). 160 * 161 * [This is actually not mentioned in Section 6, but we 162 * implement it here anyway. --piggy] 163 */ 164 if (max_tsn_seen != ctsn) 165 asoc->peer.sack_needed = 1; 166 167 /* From 6.2 Acknowledgement on Reception of DATA Chunks: 168 * 169 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, 170 * an acknowledgement SHOULD be generated for at least every 171 * second packet (not every second DATA chunk) received, and 172 * SHOULD be generated within 200 ms of the arrival of any 173 * unacknowledged DATA chunk. ... 174 */ 175 if (!asoc->peer.sack_needed) { 176 asoc->peer.sack_cnt++; 177 178 /* Set the SACK delay timeout based on the 179 * SACK delay for the last transport 180 * data was received from, or the default 181 * for the association. 182 */ 183 if (trans) { 184 /* We will need a SACK for the next packet. */ 185 if (asoc->peer.sack_cnt >= trans->sackfreq - 1) 186 asoc->peer.sack_needed = 1; 187 188 asoc->timeouts[SCTP_EVENT_TIMEOUT_SACK] = 189 trans->sackdelay; 190 } else { 191 /* We will need a SACK for the next packet. */ 192 if (asoc->peer.sack_cnt >= asoc->sackfreq - 1) 193 asoc->peer.sack_needed = 1; 194 195 asoc->timeouts[SCTP_EVENT_TIMEOUT_SACK] = 196 asoc->sackdelay; 197 } 198 199 /* Restart the SACK timer. */ 200 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART, 201 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK)); 202 } else { 203 __u32 old_a_rwnd = asoc->a_rwnd; 204 205 asoc->a_rwnd = asoc->rwnd; 206 sack = sctp_make_sack(asoc); 207 if (!sack) { 208 asoc->a_rwnd = old_a_rwnd; 209 goto nomem; 210 } 211 212 asoc->peer.sack_needed = 0; 213 asoc->peer.sack_cnt = 0; 214 215 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(sack)); 216 217 /* Stop the SACK timer. */ 218 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP, 219 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK)); 220 } 221 222 return error; 223 nomem: 224 error = -ENOMEM; 225 return error; 226 } 227 228 /* When the T3-RTX timer expires, it calls this function to create the 229 * relevant state machine event. 230 */ 231 void sctp_generate_t3_rtx_event(struct timer_list *t) 232 { 233 struct sctp_transport *transport = 234 timer_container_of(transport, t, T3_rtx_timer); 235 struct sctp_association *asoc = transport->asoc; 236 struct sock *sk = asoc->base.sk; 237 struct net *net = sock_net(sk); 238 int error; 239 240 /* Check whether a task is in the sock. */ 241 242 bh_lock_sock(sk); 243 if (sock_owned_by_user(sk)) { 244 pr_debug("%s: sock is busy\n", __func__); 245 246 /* Try again later. */ 247 sctp_transport_hold(transport); 248 if (mod_timer(&transport->T3_rtx_timer, jiffies + (HZ / 20))) 249 sctp_transport_put(transport); 250 goto out_unlock; 251 } 252 253 /* Run through the state machine. */ 254 error = sctp_do_sm(net, SCTP_EVENT_T_TIMEOUT, 255 SCTP_ST_TIMEOUT(SCTP_EVENT_TIMEOUT_T3_RTX), 256 asoc->state, 257 asoc->ep, asoc, 258 transport, GFP_ATOMIC); 259 260 if (error) 261 sk->sk_err = -error; 262 263 out_unlock: 264 bh_unlock_sock(sk); 265 sctp_transport_put(transport); 266 } 267 268 /* This is a sa interface for producing timeout events. It works 269 * for timeouts which use the association as their parameter. 270 */ 271 static void sctp_generate_timeout_event(struct sctp_association *asoc, 272 enum sctp_event_timeout timeout_type) 273 { 274 struct sock *sk = asoc->base.sk; 275 struct net *net = sock_net(sk); 276 int error = 0; 277 278 bh_lock_sock(sk); 279 if (sock_owned_by_user(sk)) { 280 pr_debug("%s: sock is busy: timer %d\n", __func__, 281 timeout_type); 282 283 /* Try again later. */ 284 sctp_association_hold(asoc); 285 if (mod_timer(&asoc->timers[timeout_type], jiffies + (HZ / 20))) 286 sctp_association_put(asoc); 287 goto out_unlock; 288 } 289 290 /* Is this association really dead and just waiting around for 291 * the timer to let go of the reference? 292 */ 293 if (asoc->base.dead) 294 goto out_unlock; 295 296 /* Run through the state machine. */ 297 error = sctp_do_sm(net, SCTP_EVENT_T_TIMEOUT, 298 SCTP_ST_TIMEOUT(timeout_type), 299 asoc->state, asoc->ep, asoc, 300 (void *)timeout_type, GFP_ATOMIC); 301 302 if (error) 303 sk->sk_err = -error; 304 305 out_unlock: 306 bh_unlock_sock(sk); 307 sctp_association_put(asoc); 308 } 309 310 static void sctp_generate_t1_cookie_event(struct timer_list *t) 311 { 312 struct sctp_association *asoc = 313 timer_container_of(asoc, t, 314 timers[SCTP_EVENT_TIMEOUT_T1_COOKIE]); 315 316 sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T1_COOKIE); 317 } 318 319 static void sctp_generate_t1_init_event(struct timer_list *t) 320 { 321 struct sctp_association *asoc = 322 timer_container_of(asoc, t, 323 timers[SCTP_EVENT_TIMEOUT_T1_INIT]); 324 325 sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T1_INIT); 326 } 327 328 static void sctp_generate_t2_shutdown_event(struct timer_list *t) 329 { 330 struct sctp_association *asoc = 331 timer_container_of(asoc, t, 332 timers[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN]); 333 334 sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T2_SHUTDOWN); 335 } 336 337 static void sctp_generate_t4_rto_event(struct timer_list *t) 338 { 339 struct sctp_association *asoc = 340 timer_container_of(asoc, t, timers[SCTP_EVENT_TIMEOUT_T4_RTO]); 341 342 sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T4_RTO); 343 } 344 345 static void sctp_generate_t5_shutdown_guard_event(struct timer_list *t) 346 { 347 struct sctp_association *asoc = 348 timer_container_of(asoc, t, 349 timers[SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD]); 350 351 sctp_generate_timeout_event(asoc, 352 SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD); 353 354 } /* sctp_generate_t5_shutdown_guard_event() */ 355 356 static void sctp_generate_autoclose_event(struct timer_list *t) 357 { 358 struct sctp_association *asoc = 359 timer_container_of(asoc, t, 360 timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE]); 361 362 sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_AUTOCLOSE); 363 } 364 365 /* Generate a heart beat event. If the sock is busy, reschedule. Make 366 * sure that the transport is still valid. 367 */ 368 void sctp_generate_heartbeat_event(struct timer_list *t) 369 { 370 struct sctp_transport *transport = timer_container_of(transport, t, 371 hb_timer); 372 struct sctp_association *asoc = transport->asoc; 373 struct sock *sk = asoc->base.sk; 374 struct net *net = sock_net(sk); 375 u32 elapsed, timeout; 376 int error = 0; 377 378 bh_lock_sock(sk); 379 if (sock_owned_by_user(sk)) { 380 pr_debug("%s: sock is busy\n", __func__); 381 382 /* Try again later. */ 383 sctp_transport_hold(transport); 384 if (mod_timer(&transport->hb_timer, jiffies + (HZ / 20))) 385 sctp_transport_put(transport); 386 goto out_unlock; 387 } 388 389 /* Check if we should still send the heartbeat or reschedule */ 390 elapsed = jiffies - transport->last_time_sent; 391 timeout = sctp_transport_timeout(transport); 392 if (elapsed < timeout) { 393 elapsed = timeout - elapsed; 394 sctp_transport_hold(transport); 395 if (mod_timer(&transport->hb_timer, jiffies + elapsed)) 396 sctp_transport_put(transport); 397 goto out_unlock; 398 } 399 400 error = sctp_do_sm(net, SCTP_EVENT_T_TIMEOUT, 401 SCTP_ST_TIMEOUT(SCTP_EVENT_TIMEOUT_HEARTBEAT), 402 asoc->state, asoc->ep, asoc, 403 transport, GFP_ATOMIC); 404 405 if (error) 406 sk->sk_err = -error; 407 408 out_unlock: 409 bh_unlock_sock(sk); 410 sctp_transport_put(transport); 411 } 412 413 /* Handle the timeout of the ICMP protocol unreachable timer. Trigger 414 * the correct state machine transition that will close the association. 415 */ 416 void sctp_generate_proto_unreach_event(struct timer_list *t) 417 { 418 struct sctp_transport *transport = 419 timer_container_of(transport, t, proto_unreach_timer); 420 struct sctp_association *asoc = transport->asoc; 421 struct sock *sk = asoc->base.sk; 422 struct net *net = sock_net(sk); 423 424 bh_lock_sock(sk); 425 if (sock_owned_by_user(sk)) { 426 pr_debug("%s: sock is busy\n", __func__); 427 428 /* Try again later. */ 429 sctp_transport_hold(transport); 430 if (mod_timer(&transport->proto_unreach_timer, 431 jiffies + (HZ / 20))) 432 sctp_transport_put(transport); 433 goto out_unlock; 434 } 435 436 /* Is this structure just waiting around for us to actually 437 * get destroyed? 438 */ 439 if (asoc->base.dead) 440 goto out_unlock; 441 442 sctp_do_sm(net, SCTP_EVENT_T_OTHER, 443 SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH), 444 asoc->state, asoc->ep, asoc, transport, GFP_ATOMIC); 445 446 out_unlock: 447 bh_unlock_sock(sk); 448 sctp_transport_put(transport); 449 } 450 451 /* Handle the timeout of the RE-CONFIG timer. */ 452 void sctp_generate_reconf_event(struct timer_list *t) 453 { 454 struct sctp_transport *transport = 455 timer_container_of(transport, t, reconf_timer); 456 struct sctp_association *asoc = transport->asoc; 457 struct sock *sk = asoc->base.sk; 458 struct net *net = sock_net(sk); 459 int error = 0; 460 461 bh_lock_sock(sk); 462 if (sock_owned_by_user(sk)) { 463 pr_debug("%s: sock is busy\n", __func__); 464 465 /* Try again later. */ 466 sctp_transport_hold(transport); 467 if (mod_timer(&transport->reconf_timer, jiffies + (HZ / 20))) 468 sctp_transport_put(transport); 469 goto out_unlock; 470 } 471 472 /* This happens when the response arrives after the timer is triggered. */ 473 if (!asoc->strreset_chunk) 474 goto out_unlock; 475 476 error = sctp_do_sm(net, SCTP_EVENT_T_TIMEOUT, 477 SCTP_ST_TIMEOUT(SCTP_EVENT_TIMEOUT_RECONF), 478 asoc->state, asoc->ep, asoc, 479 transport, GFP_ATOMIC); 480 481 if (error) 482 sk->sk_err = -error; 483 484 out_unlock: 485 bh_unlock_sock(sk); 486 sctp_transport_put(transport); 487 } 488 489 /* Handle the timeout of the probe timer. */ 490 void sctp_generate_probe_event(struct timer_list *t) 491 { 492 struct sctp_transport *transport = timer_container_of(transport, t, 493 probe_timer); 494 struct sctp_association *asoc = transport->asoc; 495 struct sock *sk = asoc->base.sk; 496 struct net *net = sock_net(sk); 497 int error = 0; 498 499 bh_lock_sock(sk); 500 if (sock_owned_by_user(sk)) { 501 pr_debug("%s: sock is busy\n", __func__); 502 503 /* Try again later. */ 504 sctp_transport_hold(transport); 505 if (mod_timer(&transport->probe_timer, jiffies + (HZ / 20))) 506 sctp_transport_put(transport); 507 goto out_unlock; 508 } 509 510 error = sctp_do_sm(net, SCTP_EVENT_T_TIMEOUT, 511 SCTP_ST_TIMEOUT(SCTP_EVENT_TIMEOUT_PROBE), 512 asoc->state, asoc->ep, asoc, 513 transport, GFP_ATOMIC); 514 515 if (error) 516 sk->sk_err = -error; 517 518 out_unlock: 519 bh_unlock_sock(sk); 520 sctp_transport_put(transport); 521 } 522 523 /* Inject a SACK Timeout event into the state machine. */ 524 static void sctp_generate_sack_event(struct timer_list *t) 525 { 526 struct sctp_association *asoc = 527 timer_container_of(asoc, t, timers[SCTP_EVENT_TIMEOUT_SACK]); 528 529 sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_SACK); 530 } 531 532 sctp_timer_event_t *sctp_timer_events[SCTP_NUM_TIMEOUT_TYPES] = { 533 [SCTP_EVENT_TIMEOUT_NONE] = NULL, 534 [SCTP_EVENT_TIMEOUT_T1_COOKIE] = sctp_generate_t1_cookie_event, 535 [SCTP_EVENT_TIMEOUT_T1_INIT] = sctp_generate_t1_init_event, 536 [SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] = sctp_generate_t2_shutdown_event, 537 [SCTP_EVENT_TIMEOUT_T3_RTX] = NULL, 538 [SCTP_EVENT_TIMEOUT_T4_RTO] = sctp_generate_t4_rto_event, 539 [SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD] = 540 sctp_generate_t5_shutdown_guard_event, 541 [SCTP_EVENT_TIMEOUT_HEARTBEAT] = NULL, 542 [SCTP_EVENT_TIMEOUT_RECONF] = NULL, 543 [SCTP_EVENT_TIMEOUT_SACK] = sctp_generate_sack_event, 544 [SCTP_EVENT_TIMEOUT_AUTOCLOSE] = sctp_generate_autoclose_event, 545 }; 546 547 548 /* RFC 2960 8.2 Path Failure Detection 549 * 550 * When its peer endpoint is multi-homed, an endpoint should keep a 551 * error counter for each of the destination transport addresses of the 552 * peer endpoint. 553 * 554 * Each time the T3-rtx timer expires on any address, or when a 555 * HEARTBEAT sent to an idle address is not acknowledged within a RTO, 556 * the error counter of that destination address will be incremented. 557 * When the value in the error counter exceeds the protocol parameter 558 * 'Path.Max.Retrans' of that destination address, the endpoint should 559 * mark the destination transport address as inactive, and a 560 * notification SHOULD be sent to the upper layer. 561 * 562 */ 563 static void sctp_do_8_2_transport_strike(struct sctp_cmd_seq *commands, 564 struct sctp_association *asoc, 565 struct sctp_transport *transport, 566 int is_hb) 567 { 568 /* The check for association's overall error counter exceeding the 569 * threshold is done in the state function. 570 */ 571 /* We are here due to a timer expiration. If the timer was 572 * not a HEARTBEAT, then normal error tracking is done. 573 * If the timer was a heartbeat, we only increment error counts 574 * when we already have an outstanding HEARTBEAT that has not 575 * been acknowledged. 576 * Additionally, some tranport states inhibit error increments. 577 */ 578 if (!is_hb) { 579 asoc->overall_error_count++; 580 if (transport->state != SCTP_INACTIVE) 581 transport->error_count++; 582 } else if (transport->hb_sent) { 583 if (transport->state != SCTP_UNCONFIRMED) 584 asoc->overall_error_count++; 585 if (transport->state != SCTP_INACTIVE) 586 transport->error_count++; 587 } 588 589 /* If the transport error count is greater than the pf_retrans 590 * threshold, and less than pathmaxrtx, and if the current state 591 * is SCTP_ACTIVE, then mark this transport as Partially Failed, 592 * see SCTP Quick Failover Draft, section 5.1 593 */ 594 if (asoc->base.net->sctp.pf_enable && 595 transport->state == SCTP_ACTIVE && 596 transport->error_count < transport->pathmaxrxt && 597 transport->error_count > transport->pf_retrans) { 598 599 sctp_assoc_control_transport(asoc, transport, 600 SCTP_TRANSPORT_PF, 601 0); 602 603 /* Update the hb timer to resend a heartbeat every rto */ 604 sctp_transport_reset_hb_timer(transport); 605 } 606 607 if (transport->state != SCTP_INACTIVE && 608 (transport->error_count > transport->pathmaxrxt)) { 609 pr_debug("%s: association:%p transport addr:%pISpc failed\n", 610 __func__, asoc, &transport->ipaddr.sa); 611 612 sctp_assoc_control_transport(asoc, transport, 613 SCTP_TRANSPORT_DOWN, 614 SCTP_FAILED_THRESHOLD); 615 } 616 617 if (transport->error_count > transport->ps_retrans && 618 asoc->peer.primary_path == transport && 619 asoc->peer.active_path != transport) 620 sctp_assoc_set_primary(asoc, asoc->peer.active_path); 621 622 /* E2) For the destination address for which the timer 623 * expires, set RTO <- RTO * 2 ("back off the timer"). The 624 * maximum value discussed in rule C7 above (RTO.max) may be 625 * used to provide an upper bound to this doubling operation. 626 * 627 * Special Case: the first HB doesn't trigger exponential backoff. 628 * The first unacknowledged HB triggers it. We do this with a flag 629 * that indicates that we have an outstanding HB. 630 */ 631 if (!is_hb || transport->hb_sent) { 632 transport->rto = min((transport->rto * 2), transport->asoc->rto_max); 633 sctp_max_rto(asoc, transport); 634 } 635 } 636 637 /* Worker routine to handle INIT command failure. */ 638 static void sctp_cmd_init_failed(struct sctp_cmd_seq *commands, 639 struct sctp_association *asoc, 640 unsigned int error) 641 { 642 struct sctp_ulpevent *event; 643 644 event = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_CANT_STR_ASSOC, 645 (__u16)error, 0, 0, NULL, 646 GFP_ATOMIC); 647 648 if (event) 649 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, 650 SCTP_ULPEVENT(event)); 651 652 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE, 653 SCTP_STATE(SCTP_STATE_CLOSED)); 654 655 /* SEND_FAILED sent later when cleaning up the association. */ 656 asoc->outqueue.error = error; 657 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL()); 658 } 659 660 /* Worker routine to handle SCTP_CMD_ASSOC_FAILED. */ 661 static void sctp_cmd_assoc_failed(struct sctp_cmd_seq *commands, 662 struct sctp_association *asoc, 663 enum sctp_event_type event_type, 664 union sctp_subtype subtype, 665 struct sctp_chunk *chunk, 666 unsigned int error) 667 { 668 struct sctp_ulpevent *event; 669 struct sctp_chunk *abort; 670 671 /* Cancel any partial delivery in progress. */ 672 asoc->stream.si->abort_pd(&asoc->ulpq, GFP_ATOMIC); 673 674 if (event_type == SCTP_EVENT_T_CHUNK && subtype.chunk == SCTP_CID_ABORT) 675 event = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_LOST, 676 (__u16)error, 0, 0, chunk, 677 GFP_ATOMIC); 678 else 679 event = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_LOST, 680 (__u16)error, 0, 0, NULL, 681 GFP_ATOMIC); 682 if (event) 683 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, 684 SCTP_ULPEVENT(event)); 685 686 if (asoc->overall_error_count >= asoc->max_retrans) { 687 abort = sctp_make_violation_max_retrans(asoc, chunk); 688 if (abort) 689 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, 690 SCTP_CHUNK(abort)); 691 } 692 693 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE, 694 SCTP_STATE(SCTP_STATE_CLOSED)); 695 696 /* SEND_FAILED sent later when cleaning up the association. */ 697 asoc->outqueue.error = error; 698 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL()); 699 } 700 701 /* Process an init chunk (may be real INIT/INIT-ACK or an embedded INIT 702 * inside the cookie. In reality, this is only used for INIT-ACK processing 703 * since all other cases use "temporary" associations and can do all 704 * their work in statefuns directly. 705 */ 706 static int sctp_cmd_process_init(struct sctp_cmd_seq *commands, 707 struct sctp_association *asoc, 708 struct sctp_chunk *chunk, 709 struct sctp_init_chunk *peer_init, 710 gfp_t gfp) 711 { 712 int error; 713 714 /* We only process the init as a sideeffect in a single 715 * case. This is when we process the INIT-ACK. If we 716 * fail during INIT processing (due to malloc problems), 717 * just return the error and stop processing the stack. 718 */ 719 if (!sctp_process_init(asoc, chunk, sctp_source(chunk), peer_init, gfp)) 720 error = -ENOMEM; 721 else 722 error = 0; 723 724 return error; 725 } 726 727 /* Helper function to break out starting up of heartbeat timers. */ 728 static void sctp_cmd_hb_timers_start(struct sctp_cmd_seq *cmds, 729 struct sctp_association *asoc) 730 { 731 struct sctp_transport *t; 732 733 /* Start a heartbeat timer for each transport on the association. 734 * hold a reference on the transport to make sure none of 735 * the needed data structures go away. 736 */ 737 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports) 738 sctp_transport_reset_hb_timer(t); 739 } 740 741 static void sctp_cmd_hb_timers_stop(struct sctp_cmd_seq *cmds, 742 struct sctp_association *asoc) 743 { 744 struct sctp_transport *t; 745 746 /* Stop all heartbeat timers. */ 747 748 list_for_each_entry(t, &asoc->peer.transport_addr_list, 749 transports) { 750 if (timer_delete(&t->hb_timer)) 751 sctp_transport_put(t); 752 } 753 } 754 755 /* Helper function to stop any pending T3-RTX timers */ 756 static void sctp_cmd_t3_rtx_timers_stop(struct sctp_cmd_seq *cmds, 757 struct sctp_association *asoc) 758 { 759 struct sctp_transport *t; 760 761 list_for_each_entry(t, &asoc->peer.transport_addr_list, 762 transports) { 763 if (timer_delete(&t->T3_rtx_timer)) 764 sctp_transport_put(t); 765 } 766 } 767 768 769 /* Helper function to handle the reception of an HEARTBEAT ACK. */ 770 static void sctp_cmd_transport_on(struct sctp_cmd_seq *cmds, 771 struct sctp_association *asoc, 772 struct sctp_transport *t, 773 struct sctp_chunk *chunk) 774 { 775 struct sctp_sender_hb_info *hbinfo; 776 int was_unconfirmed = 0; 777 778 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of the 779 * HEARTBEAT should clear the error counter of the destination 780 * transport address to which the HEARTBEAT was sent. 781 */ 782 t->error_count = 0; 783 784 /* 785 * Although RFC4960 specifies that the overall error count must 786 * be cleared when a HEARTBEAT ACK is received, we make an 787 * exception while in SHUTDOWN PENDING. If the peer keeps its 788 * window shut forever, we may never be able to transmit our 789 * outstanding data and rely on the retransmission limit be reached 790 * to shutdown the association. 791 */ 792 if (t->asoc->state < SCTP_STATE_SHUTDOWN_PENDING) 793 t->asoc->overall_error_count = 0; 794 795 /* Clear the hb_sent flag to signal that we had a good 796 * acknowledgement. 797 */ 798 t->hb_sent = 0; 799 800 /* Mark the destination transport address as active if it is not so 801 * marked. 802 */ 803 if ((t->state == SCTP_INACTIVE) || (t->state == SCTP_UNCONFIRMED)) { 804 was_unconfirmed = 1; 805 sctp_assoc_control_transport(asoc, t, SCTP_TRANSPORT_UP, 806 SCTP_HEARTBEAT_SUCCESS); 807 } 808 809 if (t->state == SCTP_PF) 810 sctp_assoc_control_transport(asoc, t, SCTP_TRANSPORT_UP, 811 SCTP_HEARTBEAT_SUCCESS); 812 813 /* HB-ACK was received for a the proper HB. Consider this 814 * forward progress. 815 */ 816 if (t->dst) 817 sctp_transport_dst_confirm(t); 818 819 /* The receiver of the HEARTBEAT ACK should also perform an 820 * RTT measurement for that destination transport address 821 * using the time value carried in the HEARTBEAT ACK chunk. 822 * If the transport's rto_pending variable has been cleared, 823 * it was most likely due to a retransmit. However, we want 824 * to re-enable it to properly update the rto. 825 */ 826 if (t->rto_pending == 0) 827 t->rto_pending = 1; 828 829 hbinfo = (struct sctp_sender_hb_info *)chunk->skb->data; 830 sctp_transport_update_rto(t, (jiffies - hbinfo->sent_at)); 831 832 /* Update the heartbeat timer. */ 833 sctp_transport_reset_hb_timer(t); 834 835 if (was_unconfirmed && asoc->peer.transport_count == 1) 836 sctp_transport_immediate_rtx(t); 837 } 838 839 840 /* Helper function to process the process SACK command. */ 841 static int sctp_cmd_process_sack(struct sctp_cmd_seq *cmds, 842 struct sctp_association *asoc, 843 struct sctp_chunk *chunk) 844 { 845 int err = 0; 846 847 if (sctp_outq_sack(&asoc->outqueue, chunk)) { 848 /* There are no more TSNs awaiting SACK. */ 849 err = sctp_do_sm(asoc->base.net, SCTP_EVENT_T_OTHER, 850 SCTP_ST_OTHER(SCTP_EVENT_NO_PENDING_TSN), 851 asoc->state, asoc->ep, asoc, NULL, 852 GFP_ATOMIC); 853 } 854 855 return err; 856 } 857 858 /* Helper function to set the timeout value for T2-SHUTDOWN timer and to set 859 * the transport for a shutdown chunk. 860 */ 861 static void sctp_cmd_setup_t2(struct sctp_cmd_seq *cmds, 862 struct sctp_association *asoc, 863 struct sctp_chunk *chunk) 864 { 865 struct sctp_transport *t; 866 867 if (chunk->transport) 868 t = chunk->transport; 869 else { 870 t = sctp_assoc_choose_alter_transport(asoc, 871 asoc->shutdown_last_sent_to); 872 chunk->transport = t; 873 } 874 asoc->shutdown_last_sent_to = t; 875 asoc->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] = t->rto; 876 } 877 878 /* Helper function to change the state of an association. */ 879 static void sctp_cmd_new_state(struct sctp_cmd_seq *cmds, 880 struct sctp_association *asoc, 881 enum sctp_state state) 882 { 883 struct sock *sk = asoc->base.sk; 884 885 asoc->state = state; 886 887 pr_debug("%s: asoc:%p[%s]\n", __func__, asoc, sctp_state_tbl[state]); 888 889 if (sctp_style(sk, TCP)) { 890 /* Change the sk->sk_state of a TCP-style socket that has 891 * successfully completed a connect() call. 892 */ 893 if (sctp_state(asoc, ESTABLISHED) && sctp_sstate(sk, CLOSED)) 894 inet_sk_set_state(sk, SCTP_SS_ESTABLISHED); 895 896 /* Set the RCV_SHUTDOWN flag when a SHUTDOWN is received. */ 897 if (sctp_state(asoc, SHUTDOWN_RECEIVED) && 898 sctp_sstate(sk, ESTABLISHED)) { 899 inet_sk_set_state(sk, SCTP_SS_CLOSING); 900 sk->sk_shutdown |= RCV_SHUTDOWN; 901 } 902 } 903 904 if (sctp_state(asoc, COOKIE_WAIT)) { 905 /* Reset init timeouts since they may have been 906 * increased due to timer expirations. 907 */ 908 asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] = 909 asoc->rto_initial; 910 asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] = 911 asoc->rto_initial; 912 } 913 914 if (sctp_state(asoc, ESTABLISHED)) { 915 kfree(asoc->peer.cookie); 916 asoc->peer.cookie = NULL; 917 } 918 919 if (sctp_state(asoc, ESTABLISHED) || 920 sctp_state(asoc, CLOSED) || 921 sctp_state(asoc, SHUTDOWN_RECEIVED)) { 922 /* Wake up any processes waiting in the asoc's wait queue in 923 * sctp_wait_for_connect() or sctp_wait_for_sndbuf(). 924 */ 925 if (waitqueue_active(&asoc->wait)) 926 wake_up_interruptible(&asoc->wait); 927 928 /* Wake up any processes waiting in the sk's sleep queue of 929 * a TCP-style or UDP-style peeled-off socket in 930 * sctp_wait_for_accept() or sctp_wait_for_packet(). 931 * For a UDP-style socket, the waiters are woken up by the 932 * notifications. 933 */ 934 if (!sctp_style(sk, UDP)) 935 sk->sk_state_change(sk); 936 } 937 938 if (sctp_state(asoc, SHUTDOWN_PENDING) && 939 !sctp_outq_is_empty(&asoc->outqueue)) 940 sctp_outq_uncork(&asoc->outqueue, GFP_ATOMIC); 941 } 942 943 /* Helper function to delete an association. */ 944 static void sctp_cmd_delete_tcb(struct sctp_cmd_seq *cmds, 945 struct sctp_association *asoc) 946 { 947 struct sock *sk = asoc->base.sk; 948 949 /* If it is a non-temporary association belonging to a TCP-style 950 * listening socket that is not closed, do not free it so that accept() 951 * can pick it up later. 952 */ 953 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING) && 954 (!asoc->temp) && (sk->sk_shutdown != SHUTDOWN_MASK)) 955 return; 956 957 sctp_association_free(asoc); 958 } 959 960 /* 961 * ADDIP Section 4.1 ASCONF Chunk Procedures 962 * A4) Start a T-4 RTO timer, using the RTO value of the selected 963 * destination address (we use active path instead of primary path just 964 * because primary path may be inactive. 965 */ 966 static void sctp_cmd_setup_t4(struct sctp_cmd_seq *cmds, 967 struct sctp_association *asoc, 968 struct sctp_chunk *chunk) 969 { 970 struct sctp_transport *t; 971 972 t = sctp_assoc_choose_alter_transport(asoc, chunk->transport); 973 asoc->timeouts[SCTP_EVENT_TIMEOUT_T4_RTO] = t->rto; 974 chunk->transport = t; 975 } 976 977 /* Process an incoming Operation Error Chunk. */ 978 static void sctp_cmd_process_operr(struct sctp_cmd_seq *cmds, 979 struct sctp_association *asoc, 980 struct sctp_chunk *chunk) 981 { 982 struct sctp_errhdr *err_hdr; 983 struct sctp_ulpevent *ev; 984 985 while (chunk->chunk_end > chunk->skb->data) { 986 err_hdr = (struct sctp_errhdr *)(chunk->skb->data); 987 988 ev = sctp_ulpevent_make_remote_error(asoc, chunk, 0, 989 GFP_ATOMIC); 990 if (!ev) 991 return; 992 993 asoc->stream.si->enqueue_event(&asoc->ulpq, ev); 994 995 switch (err_hdr->cause) { 996 case SCTP_ERROR_UNKNOWN_CHUNK: 997 { 998 struct sctp_chunkhdr *unk_chunk_hdr; 999 1000 unk_chunk_hdr = (struct sctp_chunkhdr *)(err_hdr + 1); 1001 switch (unk_chunk_hdr->type) { 1002 /* ADDIP 4.1 A9) If the peer responds to an ASCONF with 1003 * an ERROR chunk reporting that it did not recognized 1004 * the ASCONF chunk type, the sender of the ASCONF MUST 1005 * NOT send any further ASCONF chunks and MUST stop its 1006 * T-4 timer. 1007 */ 1008 case SCTP_CID_ASCONF: 1009 if (asoc->peer.asconf_capable == 0) 1010 break; 1011 1012 asoc->peer.asconf_capable = 0; 1013 sctp_add_cmd_sf(cmds, SCTP_CMD_TIMER_STOP, 1014 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO)); 1015 break; 1016 default: 1017 break; 1018 } 1019 break; 1020 } 1021 default: 1022 break; 1023 } 1024 } 1025 } 1026 1027 /* Helper function to remove the association non-primary peer 1028 * transports. 1029 */ 1030 static void sctp_cmd_del_non_primary(struct sctp_association *asoc) 1031 { 1032 struct sctp_transport *t; 1033 struct list_head *temp; 1034 struct list_head *pos; 1035 1036 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) { 1037 t = list_entry(pos, struct sctp_transport, transports); 1038 if (!sctp_cmp_addr_exact(&t->ipaddr, 1039 &asoc->peer.primary_addr)) { 1040 sctp_assoc_rm_peer(asoc, t); 1041 } 1042 } 1043 } 1044 1045 /* Helper function to set sk_err on a 1-1 style socket. */ 1046 static void sctp_cmd_set_sk_err(struct sctp_association *asoc, int error) 1047 { 1048 struct sock *sk = asoc->base.sk; 1049 1050 if (!sctp_style(sk, UDP)) 1051 sk->sk_err = error; 1052 } 1053 1054 /* Helper function to generate an association change event */ 1055 static void sctp_cmd_assoc_change(struct sctp_cmd_seq *commands, 1056 struct sctp_association *asoc, 1057 u8 state) 1058 { 1059 struct sctp_ulpevent *ev; 1060 1061 ev = sctp_ulpevent_make_assoc_change(asoc, 0, state, 0, 1062 asoc->c.sinit_num_ostreams, 1063 asoc->c.sinit_max_instreams, 1064 NULL, GFP_ATOMIC); 1065 if (ev) 1066 asoc->stream.si->enqueue_event(&asoc->ulpq, ev); 1067 } 1068 1069 static void sctp_cmd_peer_no_auth(struct sctp_cmd_seq *commands, 1070 struct sctp_association *asoc) 1071 { 1072 struct sctp_ulpevent *ev; 1073 1074 ev = sctp_ulpevent_make_authkey(asoc, 0, SCTP_AUTH_NO_AUTH, GFP_ATOMIC); 1075 if (ev) 1076 asoc->stream.si->enqueue_event(&asoc->ulpq, ev); 1077 } 1078 1079 /* Helper function to generate an adaptation indication event */ 1080 static void sctp_cmd_adaptation_ind(struct sctp_cmd_seq *commands, 1081 struct sctp_association *asoc) 1082 { 1083 struct sctp_ulpevent *ev; 1084 1085 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC); 1086 1087 if (ev) 1088 asoc->stream.si->enqueue_event(&asoc->ulpq, ev); 1089 } 1090 1091 1092 static void sctp_cmd_t1_timer_update(struct sctp_association *asoc, 1093 enum sctp_event_timeout timer, 1094 char *name) 1095 { 1096 struct sctp_transport *t; 1097 1098 t = asoc->init_last_sent_to; 1099 asoc->init_err_counter++; 1100 1101 if (t->init_sent_count > (asoc->init_cycle + 1)) { 1102 asoc->timeouts[timer] *= 2; 1103 if (asoc->timeouts[timer] > asoc->max_init_timeo) { 1104 asoc->timeouts[timer] = asoc->max_init_timeo; 1105 } 1106 asoc->init_cycle++; 1107 1108 pr_debug("%s: T1[%s] timeout adjustment init_err_counter:%d" 1109 " cycle:%d timeout:%ld\n", __func__, name, 1110 asoc->init_err_counter, asoc->init_cycle, 1111 asoc->timeouts[timer]); 1112 } 1113 1114 } 1115 1116 /* Send the whole message, chunk by chunk, to the outqueue. 1117 * This way the whole message is queued up and bundling if 1118 * encouraged for small fragments. 1119 */ 1120 static void sctp_cmd_send_msg(struct sctp_association *asoc, 1121 struct sctp_datamsg *msg, gfp_t gfp) 1122 { 1123 struct sctp_chunk *chunk; 1124 1125 list_for_each_entry(chunk, &msg->chunks, frag_list) 1126 sctp_outq_tail(&asoc->outqueue, chunk, gfp); 1127 1128 asoc->outqueue.sched->enqueue(&asoc->outqueue, msg); 1129 } 1130 1131 1132 /* These three macros allow us to pull the debugging code out of the 1133 * main flow of sctp_do_sm() to keep attention focused on the real 1134 * functionality there. 1135 */ 1136 #define debug_pre_sfn() \ 1137 pr_debug("%s[pre-fn]: ep:%p, %s, %s, asoc:%p[%s], %s\n", __func__, \ 1138 ep, sctp_evttype_tbl[event_type], (*debug_fn)(subtype), \ 1139 asoc, sctp_state_tbl[state], state_fn->name) 1140 1141 #define debug_post_sfn() \ 1142 pr_debug("%s[post-fn]: asoc:%p, status:%s\n", __func__, asoc, \ 1143 sctp_status_tbl[status]) 1144 1145 #define debug_post_sfx() \ 1146 pr_debug("%s[post-sfx]: error:%d, asoc:%p[%s]\n", __func__, error, \ 1147 asoc, sctp_state_tbl[(asoc && sctp_id2assoc(ep->base.sk, \ 1148 sctp_assoc2id(asoc))) ? asoc->state : SCTP_STATE_CLOSED]) 1149 1150 /* 1151 * This is the master state machine processing function. 1152 * 1153 * If you want to understand all of lksctp, this is a 1154 * good place to start. 1155 */ 1156 int sctp_do_sm(struct net *net, enum sctp_event_type event_type, 1157 union sctp_subtype subtype, enum sctp_state state, 1158 struct sctp_endpoint *ep, struct sctp_association *asoc, 1159 void *event_arg, gfp_t gfp) 1160 { 1161 typedef const char *(printfn_t)(union sctp_subtype); 1162 static printfn_t *table[] = { 1163 NULL, sctp_cname, sctp_tname, sctp_oname, sctp_pname, 1164 }; 1165 printfn_t *debug_fn __attribute__ ((unused)) = table[event_type]; 1166 const struct sctp_sm_table_entry *state_fn; 1167 struct sctp_cmd_seq commands; 1168 enum sctp_disposition status; 1169 int error = 0; 1170 1171 /* Look up the state function, run it, and then process the 1172 * side effects. These three steps are the heart of lksctp. 1173 */ 1174 state_fn = sctp_sm_lookup_event(net, event_type, state, subtype); 1175 1176 sctp_init_cmd_seq(&commands); 1177 1178 debug_pre_sfn(); 1179 status = state_fn->fn(net, ep, asoc, subtype, event_arg, &commands); 1180 debug_post_sfn(); 1181 1182 error = sctp_side_effects(event_type, subtype, state, 1183 ep, &asoc, event_arg, status, 1184 &commands, gfp); 1185 debug_post_sfx(); 1186 1187 return error; 1188 } 1189 1190 /***************************************************************** 1191 * This the master state function side effect processing function. 1192 *****************************************************************/ 1193 static int sctp_side_effects(enum sctp_event_type event_type, 1194 union sctp_subtype subtype, 1195 enum sctp_state state, 1196 struct sctp_endpoint *ep, 1197 struct sctp_association **asoc, 1198 void *event_arg, 1199 enum sctp_disposition status, 1200 struct sctp_cmd_seq *commands, 1201 gfp_t gfp) 1202 { 1203 int error; 1204 1205 /* FIXME - Most of the dispositions left today would be categorized 1206 * as "exceptional" dispositions. For those dispositions, it 1207 * may not be proper to run through any of the commands at all. 1208 * For example, the command interpreter might be run only with 1209 * disposition SCTP_DISPOSITION_CONSUME. 1210 */ 1211 if (0 != (error = sctp_cmd_interpreter(event_type, subtype, state, 1212 ep, *asoc, 1213 event_arg, status, 1214 commands, gfp))) 1215 goto bail; 1216 1217 switch (status) { 1218 case SCTP_DISPOSITION_DISCARD: 1219 pr_debug("%s: ignored sctp protocol event - state:%d, " 1220 "event_type:%d, event_id:%d\n", __func__, state, 1221 event_type, subtype.chunk); 1222 break; 1223 1224 case SCTP_DISPOSITION_NOMEM: 1225 /* We ran out of memory, so we need to discard this 1226 * packet. 1227 */ 1228 /* BUG--we should now recover some memory, probably by 1229 * reneging... 1230 */ 1231 error = -ENOMEM; 1232 break; 1233 1234 case SCTP_DISPOSITION_DELETE_TCB: 1235 case SCTP_DISPOSITION_ABORT: 1236 /* This should now be a command. */ 1237 *asoc = NULL; 1238 break; 1239 1240 case SCTP_DISPOSITION_CONSUME: 1241 /* 1242 * We should no longer have much work to do here as the 1243 * real work has been done as explicit commands above. 1244 */ 1245 break; 1246 1247 case SCTP_DISPOSITION_VIOLATION: 1248 net_err_ratelimited("protocol violation state %d chunkid %d\n", 1249 state, subtype.chunk); 1250 break; 1251 1252 case SCTP_DISPOSITION_NOT_IMPL: 1253 pr_warn("unimplemented feature in state %d, event_type %d, event_id %d\n", 1254 state, event_type, subtype.chunk); 1255 break; 1256 1257 case SCTP_DISPOSITION_BUG: 1258 pr_err("bug in state %d, event_type %d, event_id %d\n", 1259 state, event_type, subtype.chunk); 1260 BUG(); 1261 break; 1262 1263 default: 1264 pr_err("impossible disposition %d in state %d, event_type %d, event_id %d\n", 1265 status, state, event_type, subtype.chunk); 1266 error = status; 1267 if (error >= 0) 1268 error = -EINVAL; 1269 WARN_ON_ONCE(1); 1270 break; 1271 } 1272 1273 bail: 1274 return error; 1275 } 1276 1277 /******************************************************************** 1278 * 2nd Level Abstractions 1279 ********************************************************************/ 1280 1281 /* This is the side-effect interpreter. */ 1282 static int sctp_cmd_interpreter(enum sctp_event_type event_type, 1283 union sctp_subtype subtype, 1284 enum sctp_state state, 1285 struct sctp_endpoint *ep, 1286 struct sctp_association *asoc, 1287 void *event_arg, 1288 enum sctp_disposition status, 1289 struct sctp_cmd_seq *commands, 1290 gfp_t gfp) 1291 { 1292 struct sctp_sock *sp = sctp_sk(ep->base.sk); 1293 struct sctp_chunk *chunk = NULL, *new_obj; 1294 struct sctp_packet *packet; 1295 struct sctp_sackhdr sackh; 1296 struct timer_list *timer; 1297 struct sctp_transport *t; 1298 unsigned long timeout; 1299 struct sctp_cmd *cmd; 1300 int local_cork = 0; 1301 int error = 0; 1302 int force; 1303 1304 if (SCTP_EVENT_T_TIMEOUT != event_type) 1305 chunk = event_arg; 1306 1307 /* Note: This whole file is a huge candidate for rework. 1308 * For example, each command could either have its own handler, so 1309 * the loop would look like: 1310 * while (cmds) 1311 * cmd->handle(x, y, z) 1312 * --jgrimm 1313 */ 1314 while (NULL != (cmd = sctp_next_cmd(commands))) { 1315 switch (cmd->verb) { 1316 case SCTP_CMD_NOP: 1317 /* Do nothing. */ 1318 break; 1319 1320 case SCTP_CMD_NEW_ASOC: 1321 /* Register a new association. */ 1322 if (local_cork) { 1323 sctp_outq_uncork(&asoc->outqueue, gfp); 1324 local_cork = 0; 1325 } 1326 1327 /* Register with the endpoint. */ 1328 asoc = cmd->obj.asoc; 1329 BUG_ON(asoc->peer.primary_path == NULL); 1330 sctp_endpoint_add_asoc(ep, asoc); 1331 break; 1332 1333 case SCTP_CMD_PURGE_OUTQUEUE: 1334 sctp_outq_teardown(&asoc->outqueue); 1335 break; 1336 1337 case SCTP_CMD_DELETE_TCB: 1338 if (local_cork) { 1339 sctp_outq_uncork(&asoc->outqueue, gfp); 1340 local_cork = 0; 1341 } 1342 /* No chunk left in this packet may use this asoc. */ 1343 if (event_type == SCTP_EVENT_T_CHUNK && 1344 chunk->asoc == asoc) 1345 chunk->pdiscard = 1; 1346 /* Delete the current association. */ 1347 sctp_cmd_delete_tcb(commands, asoc); 1348 asoc = NULL; 1349 break; 1350 1351 case SCTP_CMD_NEW_STATE: 1352 /* Enter a new state. */ 1353 sctp_cmd_new_state(commands, asoc, cmd->obj.state); 1354 break; 1355 1356 case SCTP_CMD_REPORT_TSN: 1357 /* Record the arrival of a TSN. */ 1358 error = sctp_tsnmap_mark(&asoc->peer.tsn_map, 1359 cmd->obj.u32, NULL); 1360 break; 1361 1362 case SCTP_CMD_REPORT_FWDTSN: 1363 asoc->stream.si->report_ftsn(&asoc->ulpq, cmd->obj.u32); 1364 break; 1365 1366 case SCTP_CMD_PROCESS_FWDTSN: 1367 asoc->stream.si->handle_ftsn(&asoc->ulpq, 1368 cmd->obj.chunk); 1369 break; 1370 1371 case SCTP_CMD_GEN_SACK: 1372 /* Generate a Selective ACK. 1373 * The argument tells us whether to just count 1374 * the packet and MAYBE generate a SACK, or 1375 * force a SACK out. 1376 */ 1377 force = cmd->obj.i32; 1378 error = sctp_gen_sack(asoc, force, commands); 1379 break; 1380 1381 case SCTP_CMD_PROCESS_SACK: 1382 /* Process an inbound SACK. */ 1383 error = sctp_cmd_process_sack(commands, asoc, 1384 cmd->obj.chunk); 1385 break; 1386 1387 case SCTP_CMD_GEN_INIT_ACK: 1388 /* Generate an INIT ACK chunk. */ 1389 new_obj = sctp_make_init_ack(asoc, chunk, GFP_ATOMIC, 1390 0); 1391 if (!new_obj) { 1392 error = -ENOMEM; 1393 break; 1394 } 1395 1396 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, 1397 SCTP_CHUNK(new_obj)); 1398 break; 1399 1400 case SCTP_CMD_PEER_INIT: 1401 /* Process a unified INIT from the peer. 1402 * Note: Only used during INIT-ACK processing. If 1403 * there is an error just return to the outter 1404 * layer which will bail. 1405 */ 1406 error = sctp_cmd_process_init(commands, asoc, chunk, 1407 cmd->obj.init, gfp); 1408 break; 1409 1410 case SCTP_CMD_GEN_COOKIE_ECHO: 1411 /* Generate a COOKIE ECHO chunk. */ 1412 new_obj = sctp_make_cookie_echo(asoc, chunk); 1413 if (!new_obj) { 1414 if (cmd->obj.chunk) 1415 sctp_chunk_free(cmd->obj.chunk); 1416 error = -ENOMEM; 1417 break; 1418 } 1419 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, 1420 SCTP_CHUNK(new_obj)); 1421 1422 /* If there is an ERROR chunk to be sent along with 1423 * the COOKIE_ECHO, send it, too. 1424 */ 1425 if (cmd->obj.chunk) 1426 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, 1427 SCTP_CHUNK(cmd->obj.chunk)); 1428 1429 if (new_obj->transport) { 1430 new_obj->transport->init_sent_count++; 1431 asoc->init_last_sent_to = new_obj->transport; 1432 } 1433 1434 /* FIXME - Eventually come up with a cleaner way to 1435 * enabling COOKIE-ECHO + DATA bundling during 1436 * multihoming stale cookie scenarios, the following 1437 * command plays with asoc->peer.retran_path to 1438 * avoid the problem of sending the COOKIE-ECHO and 1439 * DATA in different paths, which could result 1440 * in the association being ABORTed if the DATA chunk 1441 * is processed first by the server. Checking the 1442 * init error counter simply causes this command 1443 * to be executed only during failed attempts of 1444 * association establishment. 1445 */ 1446 if ((asoc->peer.retran_path != 1447 asoc->peer.primary_path) && 1448 (asoc->init_err_counter > 0)) { 1449 sctp_add_cmd_sf(commands, 1450 SCTP_CMD_FORCE_PRIM_RETRAN, 1451 SCTP_NULL()); 1452 } 1453 1454 break; 1455 1456 case SCTP_CMD_GEN_SHUTDOWN: 1457 /* Generate SHUTDOWN when in SHUTDOWN_SENT state. 1458 * Reset error counts. 1459 */ 1460 asoc->overall_error_count = 0; 1461 1462 /* Generate a SHUTDOWN chunk. */ 1463 new_obj = sctp_make_shutdown(asoc, chunk); 1464 if (!new_obj) { 1465 error = -ENOMEM; 1466 break; 1467 } 1468 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, 1469 SCTP_CHUNK(new_obj)); 1470 break; 1471 1472 case SCTP_CMD_CHUNK_ULP: 1473 /* Send a chunk to the sockets layer. */ 1474 pr_debug("%s: sm_sideff: chunk_up:%p, ulpq:%p\n", 1475 __func__, cmd->obj.chunk, &asoc->ulpq); 1476 1477 asoc->stream.si->ulpevent_data(&asoc->ulpq, 1478 cmd->obj.chunk, 1479 GFP_ATOMIC); 1480 break; 1481 1482 case SCTP_CMD_EVENT_ULP: 1483 /* Send a notification to the sockets layer. */ 1484 pr_debug("%s: sm_sideff: event_up:%p, ulpq:%p\n", 1485 __func__, cmd->obj.ulpevent, &asoc->ulpq); 1486 1487 asoc->stream.si->enqueue_event(&asoc->ulpq, 1488 cmd->obj.ulpevent); 1489 break; 1490 1491 case SCTP_CMD_REPLY: 1492 /* If an caller has not already corked, do cork. */ 1493 if (!asoc->outqueue.cork) { 1494 sctp_outq_cork(&asoc->outqueue); 1495 local_cork = 1; 1496 } 1497 /* Send a chunk to our peer. */ 1498 sctp_outq_tail(&asoc->outqueue, cmd->obj.chunk, gfp); 1499 break; 1500 1501 case SCTP_CMD_SEND_PKT: 1502 /* Send a full packet to our peer. */ 1503 packet = cmd->obj.packet; 1504 sctp_packet_transmit(packet, gfp); 1505 sctp_ootb_pkt_free(packet); 1506 break; 1507 1508 case SCTP_CMD_T1_RETRAN: 1509 /* Mark a transport for retransmission. */ 1510 sctp_retransmit(&asoc->outqueue, cmd->obj.transport, 1511 SCTP_RTXR_T1_RTX); 1512 break; 1513 1514 case SCTP_CMD_RETRAN: 1515 /* Mark a transport for retransmission. */ 1516 sctp_retransmit(&asoc->outqueue, cmd->obj.transport, 1517 SCTP_RTXR_T3_RTX); 1518 break; 1519 1520 case SCTP_CMD_ECN_CE: 1521 /* Do delayed CE processing. */ 1522 sctp_do_ecn_ce_work(asoc, cmd->obj.u32); 1523 break; 1524 1525 case SCTP_CMD_ECN_ECNE: 1526 /* Do delayed ECNE processing. */ 1527 new_obj = sctp_do_ecn_ecne_work(asoc, cmd->obj.u32, 1528 chunk); 1529 if (new_obj) 1530 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, 1531 SCTP_CHUNK(new_obj)); 1532 break; 1533 1534 case SCTP_CMD_ECN_CWR: 1535 /* Do delayed CWR processing. */ 1536 sctp_do_ecn_cwr_work(asoc, cmd->obj.u32); 1537 break; 1538 1539 case SCTP_CMD_SETUP_T2: 1540 sctp_cmd_setup_t2(commands, asoc, cmd->obj.chunk); 1541 break; 1542 1543 case SCTP_CMD_TIMER_START_ONCE: 1544 timer = &asoc->timers[cmd->obj.to]; 1545 1546 if (timer_pending(timer)) 1547 break; 1548 fallthrough; 1549 1550 case SCTP_CMD_TIMER_START: 1551 timer = &asoc->timers[cmd->obj.to]; 1552 timeout = asoc->timeouts[cmd->obj.to]; 1553 BUG_ON(!timeout); 1554 1555 if (!timer_reduce(timer, jiffies + timeout)) 1556 sctp_association_hold(asoc); 1557 break; 1558 1559 case SCTP_CMD_TIMER_RESTART: 1560 timer = &asoc->timers[cmd->obj.to]; 1561 timeout = asoc->timeouts[cmd->obj.to]; 1562 if (!mod_timer(timer, jiffies + timeout)) 1563 sctp_association_hold(asoc); 1564 break; 1565 1566 case SCTP_CMD_TIMER_STOP: 1567 timer = &asoc->timers[cmd->obj.to]; 1568 if (timer_delete(timer)) 1569 sctp_association_put(asoc); 1570 break; 1571 1572 case SCTP_CMD_INIT_CHOOSE_TRANSPORT: 1573 chunk = cmd->obj.chunk; 1574 t = sctp_assoc_choose_alter_transport(asoc, 1575 asoc->init_last_sent_to); 1576 asoc->init_last_sent_to = t; 1577 chunk->transport = t; 1578 t->init_sent_count++; 1579 /* Set the new transport as primary */ 1580 sctp_assoc_set_primary(asoc, t); 1581 break; 1582 1583 case SCTP_CMD_INIT_RESTART: 1584 /* Do the needed accounting and updates 1585 * associated with restarting an initialization 1586 * timer. Only multiply the timeout by two if 1587 * all transports have been tried at the current 1588 * timeout. 1589 */ 1590 sctp_cmd_t1_timer_update(asoc, 1591 SCTP_EVENT_TIMEOUT_T1_INIT, 1592 "INIT"); 1593 1594 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART, 1595 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT)); 1596 break; 1597 1598 case SCTP_CMD_COOKIEECHO_RESTART: 1599 /* Do the needed accounting and updates 1600 * associated with restarting an initialization 1601 * timer. Only multiply the timeout by two if 1602 * all transports have been tried at the current 1603 * timeout. 1604 */ 1605 sctp_cmd_t1_timer_update(asoc, 1606 SCTP_EVENT_TIMEOUT_T1_COOKIE, 1607 "COOKIE"); 1608 1609 /* If we've sent any data bundled with 1610 * COOKIE-ECHO we need to resend. 1611 */ 1612 list_for_each_entry(t, &asoc->peer.transport_addr_list, 1613 transports) { 1614 sctp_retransmit_mark(&asoc->outqueue, t, 1615 SCTP_RTXR_T1_RTX); 1616 } 1617 1618 sctp_add_cmd_sf(commands, 1619 SCTP_CMD_TIMER_RESTART, 1620 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE)); 1621 break; 1622 1623 case SCTP_CMD_INIT_FAILED: 1624 sctp_cmd_init_failed(commands, asoc, cmd->obj.u16); 1625 break; 1626 1627 case SCTP_CMD_ASSOC_FAILED: 1628 sctp_cmd_assoc_failed(commands, asoc, event_type, 1629 subtype, chunk, cmd->obj.u16); 1630 break; 1631 1632 case SCTP_CMD_INIT_COUNTER_INC: 1633 asoc->init_err_counter++; 1634 break; 1635 1636 case SCTP_CMD_INIT_COUNTER_RESET: 1637 asoc->init_err_counter = 0; 1638 asoc->init_cycle = 0; 1639 list_for_each_entry(t, &asoc->peer.transport_addr_list, 1640 transports) { 1641 t->init_sent_count = 0; 1642 } 1643 break; 1644 1645 case SCTP_CMD_REPORT_DUP: 1646 sctp_tsnmap_mark_dup(&asoc->peer.tsn_map, 1647 cmd->obj.u32); 1648 break; 1649 1650 case SCTP_CMD_REPORT_BAD_TAG: 1651 pr_debug("%s: vtag mismatch!\n", __func__); 1652 break; 1653 1654 case SCTP_CMD_STRIKE: 1655 /* Mark one strike against a transport. */ 1656 sctp_do_8_2_transport_strike(commands, asoc, 1657 cmd->obj.transport, 0); 1658 break; 1659 1660 case SCTP_CMD_TRANSPORT_IDLE: 1661 t = cmd->obj.transport; 1662 sctp_transport_lower_cwnd(t, SCTP_LOWER_CWND_INACTIVE); 1663 break; 1664 1665 case SCTP_CMD_TRANSPORT_HB_SENT: 1666 t = cmd->obj.transport; 1667 sctp_do_8_2_transport_strike(commands, asoc, 1668 t, 1); 1669 t->hb_sent = 1; 1670 break; 1671 1672 case SCTP_CMD_TRANSPORT_ON: 1673 t = cmd->obj.transport; 1674 sctp_cmd_transport_on(commands, asoc, t, chunk); 1675 break; 1676 1677 case SCTP_CMD_HB_TIMERS_START: 1678 sctp_cmd_hb_timers_start(commands, asoc); 1679 break; 1680 1681 case SCTP_CMD_HB_TIMER_UPDATE: 1682 t = cmd->obj.transport; 1683 sctp_transport_reset_hb_timer(t); 1684 break; 1685 1686 case SCTP_CMD_HB_TIMERS_STOP: 1687 sctp_cmd_hb_timers_stop(commands, asoc); 1688 break; 1689 1690 case SCTP_CMD_PROBE_TIMER_UPDATE: 1691 t = cmd->obj.transport; 1692 sctp_transport_reset_probe_timer(t); 1693 break; 1694 1695 case SCTP_CMD_REPORT_ERROR: 1696 error = cmd->obj.error; 1697 break; 1698 1699 case SCTP_CMD_PROCESS_CTSN: 1700 /* Dummy up a SACK for processing. */ 1701 sackh.cum_tsn_ack = cmd->obj.be32; 1702 sackh.a_rwnd = htonl(asoc->peer.rwnd + 1703 asoc->outqueue.outstanding_bytes); 1704 sackh.num_gap_ack_blocks = 0; 1705 sackh.num_dup_tsns = 0; 1706 chunk->subh.sack_hdr = &sackh; 1707 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, 1708 SCTP_CHUNK(chunk)); 1709 break; 1710 1711 case SCTP_CMD_DISCARD_PACKET: 1712 /* We need to discard the whole packet. 1713 * Uncork the queue since there might be 1714 * responses pending 1715 */ 1716 chunk->pdiscard = 1; 1717 if (asoc) { 1718 sctp_outq_uncork(&asoc->outqueue, gfp); 1719 local_cork = 0; 1720 } 1721 break; 1722 1723 case SCTP_CMD_RTO_PENDING: 1724 t = cmd->obj.transport; 1725 t->rto_pending = 1; 1726 break; 1727 1728 case SCTP_CMD_PART_DELIVER: 1729 asoc->stream.si->start_pd(&asoc->ulpq, GFP_ATOMIC); 1730 break; 1731 1732 case SCTP_CMD_RENEGE: 1733 asoc->stream.si->renege_events(&asoc->ulpq, 1734 cmd->obj.chunk, 1735 GFP_ATOMIC); 1736 break; 1737 1738 case SCTP_CMD_SETUP_T4: 1739 sctp_cmd_setup_t4(commands, asoc, cmd->obj.chunk); 1740 break; 1741 1742 case SCTP_CMD_PROCESS_OPERR: 1743 sctp_cmd_process_operr(commands, asoc, chunk); 1744 break; 1745 case SCTP_CMD_CLEAR_INIT_TAG: 1746 asoc->peer.i.init_tag = 0; 1747 break; 1748 case SCTP_CMD_DEL_NON_PRIMARY: 1749 sctp_cmd_del_non_primary(asoc); 1750 break; 1751 case SCTP_CMD_T3_RTX_TIMERS_STOP: 1752 sctp_cmd_t3_rtx_timers_stop(commands, asoc); 1753 break; 1754 case SCTP_CMD_FORCE_PRIM_RETRAN: 1755 t = asoc->peer.retran_path; 1756 asoc->peer.retran_path = asoc->peer.primary_path; 1757 sctp_outq_uncork(&asoc->outqueue, gfp); 1758 local_cork = 0; 1759 asoc->peer.retran_path = t; 1760 break; 1761 case SCTP_CMD_SET_SK_ERR: 1762 sctp_cmd_set_sk_err(asoc, cmd->obj.error); 1763 break; 1764 case SCTP_CMD_ASSOC_CHANGE: 1765 sctp_cmd_assoc_change(commands, asoc, 1766 cmd->obj.u8); 1767 break; 1768 case SCTP_CMD_ADAPTATION_IND: 1769 sctp_cmd_adaptation_ind(commands, asoc); 1770 break; 1771 case SCTP_CMD_PEER_NO_AUTH: 1772 sctp_cmd_peer_no_auth(commands, asoc); 1773 break; 1774 1775 case SCTP_CMD_ASSOC_SHKEY: 1776 error = sctp_auth_asoc_init_active_key(asoc, 1777 GFP_ATOMIC); 1778 break; 1779 case SCTP_CMD_UPDATE_INITTAG: 1780 asoc->peer.i.init_tag = cmd->obj.u32; 1781 break; 1782 case SCTP_CMD_SEND_MSG: 1783 if (!asoc->outqueue.cork) { 1784 sctp_outq_cork(&asoc->outqueue); 1785 local_cork = 1; 1786 } 1787 sctp_cmd_send_msg(asoc, cmd->obj.msg, gfp); 1788 break; 1789 case SCTP_CMD_PURGE_ASCONF_QUEUE: 1790 sctp_asconf_queue_teardown(asoc); 1791 break; 1792 1793 case SCTP_CMD_SET_ASOC: 1794 if (asoc && local_cork) { 1795 sctp_outq_uncork(&asoc->outqueue, gfp); 1796 local_cork = 0; 1797 } 1798 asoc = cmd->obj.asoc; 1799 break; 1800 1801 default: 1802 pr_warn("Impossible command: %u\n", 1803 cmd->verb); 1804 break; 1805 } 1806 1807 if (error) { 1808 cmd = sctp_next_cmd(commands); 1809 while (cmd) { 1810 if (cmd->verb == SCTP_CMD_REPLY) 1811 sctp_chunk_free(cmd->obj.chunk); 1812 cmd = sctp_next_cmd(commands); 1813 } 1814 break; 1815 } 1816 } 1817 1818 /* If this is in response to a received chunk, wait until 1819 * we are done with the packet to open the queue so that we don't 1820 * send multiple packets in response to a single request. 1821 */ 1822 if (asoc && SCTP_EVENT_T_CHUNK == event_type && chunk) { 1823 if (chunk->end_of_packet || chunk->singleton) 1824 sctp_outq_uncork(&asoc->outqueue, gfp); 1825 } else if (local_cork) 1826 sctp_outq_uncork(&asoc->outqueue, gfp); 1827 1828 if (sp->data_ready_signalled) 1829 sp->data_ready_signalled = 0; 1830 1831 return error; 1832 } 1833