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