1 /*- 2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved. 3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. 4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * a) Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * b) Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the distribution. 15 * 16 * c) Neither the name of Cisco Systems, Inc. nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <netinet/sctp_os.h> 37 #include <netinet/sctp_var.h> 38 #include <netinet/sctp_sysctl.h> 39 #include <netinet/sctp_pcb.h> 40 #include <netinet/sctp_header.h> 41 #include <netinet/sctputil.h> 42 #include <netinet/sctp_output.h> 43 #include <netinet/sctp_input.h> 44 #include <netinet/sctp_auth.h> 45 #include <netinet/sctp_indata.h> 46 #include <netinet/sctp_asconf.h> 47 #include <netinet/sctp_bsd_addr.h> 48 #include <netinet/sctp_timer.h> 49 #include <netinet/sctp_crc32.h> 50 #include <netinet/udp.h> 51 #include <sys/smp.h> 52 53 54 55 static void 56 sctp_stop_all_cookie_timers(struct sctp_tcb *stcb) 57 { 58 struct sctp_nets *net; 59 60 /* 61 * This now not only stops all cookie timers it also stops any INIT 62 * timers as well. This will make sure that the timers are stopped 63 * in all collision cases. 64 */ 65 SCTP_TCB_LOCK_ASSERT(stcb); 66 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 67 if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) { 68 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, 69 stcb->sctp_ep, 70 stcb, 71 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1); 72 } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) { 73 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, 74 stcb->sctp_ep, 75 stcb, 76 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2); 77 } 78 } 79 } 80 81 /* INIT handler */ 82 static void 83 sctp_handle_init(struct mbuf *m, int iphlen, int offset, 84 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh, 85 struct sctp_init_chunk *cp, struct sctp_inpcb *inp, 86 struct sctp_tcb *stcb, int *abort_no_unlock, 87 uint8_t use_mflowid, uint32_t mflowid, 88 uint32_t vrf_id, uint16_t port) 89 { 90 struct sctp_init *init; 91 struct mbuf *op_err; 92 93 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n", 94 (void *)stcb); 95 if (stcb == NULL) { 96 SCTP_INP_RLOCK(inp); 97 } 98 /* validate length */ 99 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) { 100 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 101 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err, 102 use_mflowid, mflowid, 103 vrf_id, port); 104 if (stcb) 105 *abort_no_unlock = 1; 106 goto outnow; 107 } 108 /* validate parameters */ 109 init = &cp->init; 110 if (init->initiate_tag == 0) { 111 /* protocol error... send abort */ 112 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 113 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err, 114 use_mflowid, mflowid, 115 vrf_id, port); 116 if (stcb) 117 *abort_no_unlock = 1; 118 goto outnow; 119 } 120 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) { 121 /* invalid parameter... send abort */ 122 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 123 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err, 124 use_mflowid, mflowid, 125 vrf_id, port); 126 if (stcb) 127 *abort_no_unlock = 1; 128 goto outnow; 129 } 130 if (init->num_inbound_streams == 0) { 131 /* protocol error... send abort */ 132 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 133 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err, 134 use_mflowid, mflowid, 135 vrf_id, port); 136 if (stcb) 137 *abort_no_unlock = 1; 138 goto outnow; 139 } 140 if (init->num_outbound_streams == 0) { 141 /* protocol error... send abort */ 142 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 143 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err, 144 use_mflowid, mflowid, 145 vrf_id, port); 146 if (stcb) 147 *abort_no_unlock = 1; 148 goto outnow; 149 } 150 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp), 151 offset + ntohs(cp->ch.chunk_length))) { 152 /* auth parameter(s) error... send abort */ 153 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, NULL, 154 use_mflowid, mflowid, 155 vrf_id, port); 156 if (stcb) 157 *abort_no_unlock = 1; 158 goto outnow; 159 } 160 /* 161 * We are only accepting if we have a socket with positive 162 * so_qlimit. 163 */ 164 if ((stcb == NULL) && 165 ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 166 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 167 (inp->sctp_socket == NULL) || 168 (inp->sctp_socket->so_qlimit == 0))) { 169 /* 170 * FIX ME ?? What about TCP model and we have a 171 * match/restart case? Actually no fix is needed. the lookup 172 * will always find the existing assoc so stcb would not be 173 * NULL. It may be questionable to do this since we COULD 174 * just send back the INIT-ACK and hope that the app did 175 * accept()'s by the time the COOKIE was sent. But there is 176 * a price to pay for COOKIE generation and I don't want to 177 * pay it on the chance that the app will actually do some 178 * accepts(). The App just looses and should NOT be in this 179 * state :-) 180 */ 181 if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) { 182 sctp_send_abort(m, iphlen, src, dst, sh, 0, NULL, 183 use_mflowid, mflowid, 184 vrf_id, port); 185 } 186 goto outnow; 187 } 188 if ((stcb != NULL) && 189 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) { 190 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n"); 191 sctp_send_shutdown_ack(stcb, NULL); 192 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 193 } else { 194 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n"); 195 sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, src, dst, 196 sh, cp, 197 use_mflowid, mflowid, 198 vrf_id, port, 199 ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED)); 200 } 201 outnow: 202 if (stcb == NULL) { 203 SCTP_INP_RUNLOCK(inp); 204 } 205 } 206 207 /* 208 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error 209 */ 210 211 int 212 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked 213 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 214 SCTP_UNUSED 215 #endif 216 ) 217 { 218 int unsent_data = 0; 219 unsigned int i; 220 struct sctp_stream_queue_pending *sp; 221 struct sctp_association *asoc; 222 223 /* 224 * This function returns the number of streams that have true unsent 225 * data on them. Note that as it looks through it will clean up any 226 * places that have old data that has been sent but left at top of 227 * stream queue. 228 */ 229 asoc = &stcb->asoc; 230 SCTP_TCB_SEND_LOCK(stcb); 231 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 232 /* Check to see if some data queued */ 233 for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 234 /* sa_ignore FREED_MEMORY */ 235 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue); 236 if (sp == NULL) { 237 continue; 238 } 239 if ((sp->msg_is_complete) && 240 (sp->length == 0) && 241 (sp->sender_all_done)) { 242 /* 243 * We are doing differed cleanup. Last time 244 * through when we took all the data the 245 * sender_all_done was not set. 246 */ 247 if (sp->put_last_out == 0) { 248 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n"); 249 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n", 250 sp->sender_all_done, 251 sp->length, 252 sp->msg_is_complete, 253 sp->put_last_out); 254 } 255 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1); 256 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next); 257 if (sp->net) { 258 sctp_free_remote_addr(sp->net); 259 sp->net = NULL; 260 } 261 if (sp->data) { 262 sctp_m_freem(sp->data); 263 sp->data = NULL; 264 } 265 sctp_free_a_strmoq(stcb, sp, so_locked); 266 } else { 267 unsent_data++; 268 break; 269 } 270 } 271 } 272 SCTP_TCB_SEND_UNLOCK(stcb); 273 return (unsent_data); 274 } 275 276 static int 277 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb) 278 { 279 struct sctp_init *init; 280 struct sctp_association *asoc; 281 struct sctp_nets *lnet; 282 unsigned int i; 283 284 init = &cp->init; 285 asoc = &stcb->asoc; 286 /* save off parameters */ 287 asoc->peer_vtag = ntohl(init->initiate_tag); 288 asoc->peers_rwnd = ntohl(init->a_rwnd); 289 /* init tsn's */ 290 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1; 291 292 if (!TAILQ_EMPTY(&asoc->nets)) { 293 /* update any ssthresh's that may have a default */ 294 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { 295 lnet->ssthresh = asoc->peers_rwnd; 296 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) { 297 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION); 298 } 299 } 300 } 301 SCTP_TCB_SEND_LOCK(stcb); 302 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) { 303 unsigned int newcnt; 304 struct sctp_stream_out *outs; 305 struct sctp_stream_queue_pending *sp, *nsp; 306 struct sctp_tmit_chunk *chk, *nchk; 307 308 /* abandon the upper streams */ 309 newcnt = ntohs(init->num_inbound_streams); 310 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { 311 if (chk->rec.data.stream_number >= newcnt) { 312 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); 313 asoc->send_queue_cnt--; 314 if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) { 315 asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--; 316 #ifdef INVARIANTS 317 } else { 318 panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number); 319 #endif 320 } 321 if (chk->data != NULL) { 322 sctp_free_bufspace(stcb, asoc, chk, 1); 323 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 324 0, chk, SCTP_SO_NOT_LOCKED); 325 if (chk->data) { 326 sctp_m_freem(chk->data); 327 chk->data = NULL; 328 } 329 } 330 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 331 /* sa_ignore FREED_MEMORY */ 332 } 333 } 334 if (asoc->strmout) { 335 for (i = newcnt; i < asoc->pre_open_streams; i++) { 336 outs = &asoc->strmout[i]; 337 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) { 338 TAILQ_REMOVE(&outs->outqueue, sp, next); 339 asoc->stream_queue_cnt--; 340 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, 341 stcb, 0, sp, SCTP_SO_NOT_LOCKED); 342 if (sp->data) { 343 sctp_m_freem(sp->data); 344 sp->data = NULL; 345 } 346 if (sp->net) { 347 sctp_free_remote_addr(sp->net); 348 sp->net = NULL; 349 } 350 /* Free the chunk */ 351 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED); 352 /* sa_ignore FREED_MEMORY */ 353 } 354 } 355 } 356 /* cut back the count */ 357 asoc->pre_open_streams = newcnt; 358 } 359 SCTP_TCB_SEND_UNLOCK(stcb); 360 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams; 361 362 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */ 363 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map; 364 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 365 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 366 } 367 /* This is the next one we expect */ 368 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1; 369 370 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn); 371 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in; 372 373 asoc->advanced_peer_ack_point = asoc->last_acked_seq; 374 /* open the requested streams */ 375 376 if (asoc->strmin != NULL) { 377 /* Free the old ones */ 378 struct sctp_queued_to_read *ctl, *nctl; 379 380 for (i = 0; i < asoc->streamincnt; i++) { 381 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) { 382 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); 383 sctp_free_remote_addr(ctl->whoFrom); 384 ctl->whoFrom = NULL; 385 sctp_m_freem(ctl->data); 386 ctl->data = NULL; 387 sctp_free_a_readq(stcb, ctl); 388 } 389 } 390 SCTP_FREE(asoc->strmin, SCTP_M_STRMI); 391 } 392 if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) { 393 asoc->streamincnt = ntohs(init->num_outbound_streams); 394 } else { 395 asoc->streamincnt = asoc->max_inbound_streams; 396 } 397 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt * 398 sizeof(struct sctp_stream_in), SCTP_M_STRMI); 399 if (asoc->strmin == NULL) { 400 /* we didn't get memory for the streams! */ 401 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n"); 402 return (-1); 403 } 404 for (i = 0; i < asoc->streamincnt; i++) { 405 asoc->strmin[i].stream_no = i; 406 asoc->strmin[i].last_sequence_delivered = 0xffff; 407 TAILQ_INIT(&asoc->strmin[i].inqueue); 408 asoc->strmin[i].delivery_started = 0; 409 } 410 /* 411 * load_address_from_init will put the addresses into the 412 * association when the COOKIE is processed or the INIT-ACK is 413 * processed. Both types of COOKIE's existing and new call this 414 * routine. It will remove addresses that are no longer in the 415 * association (for the restarting case where addresses are 416 * removed). Up front when the INIT arrives we will discard it if it 417 * is a restart and new addresses have been added. 418 */ 419 /* sa_ignore MEMLEAK */ 420 return (0); 421 } 422 423 /* 424 * INIT-ACK message processing/consumption returns value < 0 on error 425 */ 426 static int 427 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset, 428 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh, 429 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 430 struct sctp_nets *net, int *abort_no_unlock, 431 uint8_t use_mflowid, uint32_t mflowid, 432 uint32_t vrf_id) 433 { 434 struct sctp_association *asoc; 435 struct mbuf *op_err; 436 int retval, abort_flag; 437 uint32_t initack_limit; 438 int nat_friendly = 0; 439 440 /* First verify that we have no illegal param's */ 441 abort_flag = 0; 442 op_err = NULL; 443 444 op_err = sctp_arethere_unrecognized_parameters(m, 445 (offset + sizeof(struct sctp_init_chunk)), 446 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly); 447 if (abort_flag) { 448 /* Send an abort and notify peer */ 449 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 450 *abort_no_unlock = 1; 451 return (-1); 452 } 453 asoc = &stcb->asoc; 454 asoc->peer_supports_nat = (uint8_t) nat_friendly; 455 /* process the peer's parameters in the INIT-ACK */ 456 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb); 457 if (retval < 0) { 458 return (retval); 459 } 460 initack_limit = offset + ntohs(cp->ch.chunk_length); 461 /* load all addresses */ 462 if ((retval = sctp_load_addresses_from_init(stcb, m, 463 (offset + sizeof(struct sctp_init_chunk)), initack_limit, 464 src, dst, NULL))) { 465 /* Huh, we should abort */ 466 SCTPDBG(SCTP_DEBUG_INPUT1, 467 "Load addresses from INIT causes an abort %d\n", 468 retval); 469 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 470 src, dst, sh, NULL, 471 use_mflowid, mflowid, 472 vrf_id, net->port); 473 *abort_no_unlock = 1; 474 return (-1); 475 } 476 /* if the peer doesn't support asconf, flush the asconf queue */ 477 if (asoc->peer_supports_asconf == 0) { 478 struct sctp_asconf_addr *param, *nparam; 479 480 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) { 481 TAILQ_REMOVE(&asoc->asconf_queue, param, next); 482 SCTP_FREE(param, SCTP_M_ASC_ADDR); 483 } 484 } 485 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 486 stcb->asoc.local_hmacs); 487 if (op_err) { 488 sctp_queue_op_err(stcb, op_err); 489 /* queuing will steal away the mbuf chain to the out queue */ 490 op_err = NULL; 491 } 492 /* extract the cookie and queue it to "echo" it back... */ 493 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 494 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 495 stcb->asoc.overall_error_count, 496 0, 497 SCTP_FROM_SCTP_INPUT, 498 __LINE__); 499 } 500 stcb->asoc.overall_error_count = 0; 501 net->error_count = 0; 502 503 /* 504 * Cancel the INIT timer, We do this first before queueing the 505 * cookie. We always cancel at the primary to assue that we are 506 * canceling the timer started by the INIT which always goes to the 507 * primary. 508 */ 509 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, 510 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4); 511 512 /* calculate the RTO */ 513 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy, 514 SCTP_RTT_FROM_NON_DATA); 515 516 retval = sctp_send_cookie_echo(m, offset, stcb, net); 517 if (retval < 0) { 518 /* 519 * No cookie, we probably should send a op error. But in any 520 * case if there is no cookie in the INIT-ACK, we can 521 * abandon the peer, its broke. 522 */ 523 if (retval == -3) { 524 /* We abort with an error of missing mandatory param */ 525 op_err = 526 sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM); 527 if (op_err) { 528 /* 529 * Expand beyond to include the mandatory 530 * param cookie 531 */ 532 struct sctp_inv_mandatory_param *mp; 533 534 SCTP_BUF_LEN(op_err) = 535 sizeof(struct sctp_inv_mandatory_param); 536 mp = mtod(op_err, 537 struct sctp_inv_mandatory_param *); 538 /* Subtract the reserved param */ 539 mp->length = 540 htons(sizeof(struct sctp_inv_mandatory_param) - 2); 541 mp->num_param = htonl(1); 542 mp->param = htons(SCTP_STATE_COOKIE); 543 mp->resv = 0; 544 } 545 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 546 src, dst, sh, op_err, 547 use_mflowid, mflowid, 548 vrf_id, net->port); 549 *abort_no_unlock = 1; 550 } 551 return (retval); 552 } 553 return (0); 554 } 555 556 static void 557 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, 558 struct sctp_tcb *stcb, struct sctp_nets *net) 559 { 560 struct sockaddr_storage store; 561 struct sctp_nets *r_net, *f_net; 562 struct timeval tv; 563 int req_prim = 0; 564 uint16_t old_error_counter; 565 566 #ifdef INET 567 struct sockaddr_in *sin; 568 569 #endif 570 #ifdef INET6 571 struct sockaddr_in6 *sin6; 572 573 #endif 574 575 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { 576 /* Invalid length */ 577 return; 578 } 579 memset(&store, 0, sizeof(store)); 580 switch (cp->heartbeat.hb_info.addr_family) { 581 #ifdef INET 582 case AF_INET: 583 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { 584 sin = (struct sockaddr_in *)&store; 585 sin->sin_family = cp->heartbeat.hb_info.addr_family; 586 sin->sin_len = cp->heartbeat.hb_info.addr_len; 587 sin->sin_port = stcb->rport; 588 memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address, 589 sizeof(sin->sin_addr)); 590 } else { 591 return; 592 } 593 break; 594 #endif 595 #ifdef INET6 596 case AF_INET6: 597 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { 598 sin6 = (struct sockaddr_in6 *)&store; 599 sin6->sin6_family = cp->heartbeat.hb_info.addr_family; 600 sin6->sin6_len = cp->heartbeat.hb_info.addr_len; 601 sin6->sin6_port = stcb->rport; 602 memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address, 603 sizeof(sin6->sin6_addr)); 604 } else { 605 return; 606 } 607 break; 608 #endif 609 default: 610 return; 611 } 612 r_net = sctp_findnet(stcb, (struct sockaddr *)&store); 613 if (r_net == NULL) { 614 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n"); 615 return; 616 } 617 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && 618 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && 619 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { 620 /* 621 * If the its a HB and it's random value is correct when can 622 * confirm the destination. 623 */ 624 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 625 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { 626 stcb->asoc.primary_destination = r_net; 627 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 628 f_net = TAILQ_FIRST(&stcb->asoc.nets); 629 if (f_net != r_net) { 630 /* 631 * first one on the list is NOT the primary 632 * sctp_cmpaddr() is much more efficent if 633 * the primary is the first on the list, 634 * make it so. 635 */ 636 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next); 637 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next); 638 } 639 req_prim = 1; 640 } 641 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 642 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED); 643 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3); 644 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net); 645 } 646 old_error_counter = r_net->error_count; 647 r_net->error_count = 0; 648 r_net->hb_responded = 1; 649 tv.tv_sec = cp->heartbeat.hb_info.time_value_1; 650 tv.tv_usec = cp->heartbeat.hb_info.time_value_2; 651 /* Now lets do a RTO with this */ 652 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy, 653 SCTP_RTT_FROM_NON_DATA); 654 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) { 655 r_net->dest_state |= SCTP_ADDR_REACHABLE; 656 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 657 0, (void *)r_net, SCTP_SO_NOT_LOCKED); 658 } 659 if (r_net->dest_state & SCTP_ADDR_PF) { 660 r_net->dest_state &= ~SCTP_ADDR_PF; 661 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net); 662 } 663 if (old_error_counter > 0) { 664 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3); 665 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net); 666 } 667 if (r_net == stcb->asoc.primary_destination) { 668 if (stcb->asoc.alternate) { 669 /* release the alternate, primary is good */ 670 sctp_free_remote_addr(stcb->asoc.alternate); 671 stcb->asoc.alternate = NULL; 672 } 673 } 674 /* Mobility adaptation */ 675 if (req_prim) { 676 if ((sctp_is_mobility_feature_on(stcb->sctp_ep, 677 SCTP_MOBILITY_BASE) || 678 sctp_is_mobility_feature_on(stcb->sctp_ep, 679 SCTP_MOBILITY_FASTHANDOFF)) && 680 sctp_is_mobility_feature_on(stcb->sctp_ep, 681 SCTP_MOBILITY_PRIM_DELETED)) { 682 683 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7); 684 if (sctp_is_mobility_feature_on(stcb->sctp_ep, 685 SCTP_MOBILITY_FASTHANDOFF)) { 686 sctp_assoc_immediate_retrans(stcb, 687 stcb->asoc.primary_destination); 688 } 689 if (sctp_is_mobility_feature_on(stcb->sctp_ep, 690 SCTP_MOBILITY_BASE)) { 691 sctp_move_chunks_from_net(stcb, 692 stcb->asoc.deleted_primary); 693 } 694 sctp_delete_prim_timer(stcb->sctp_ep, stcb, 695 stcb->asoc.deleted_primary); 696 } 697 } 698 } 699 700 static int 701 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb) 702 { 703 /* 704 * return 0 means we want you to proceed with the abort non-zero 705 * means no abort processing 706 */ 707 struct sctpasochead *head; 708 709 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) { 710 /* generate a new vtag and send init */ 711 LIST_REMOVE(stcb, sctp_asocs); 712 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 713 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 714 /* 715 * put it in the bucket in the vtag hash of assoc's for the 716 * system 717 */ 718 LIST_INSERT_HEAD(head, stcb, sctp_asocs); 719 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 720 return (1); 721 } 722 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) { 723 /* 724 * treat like a case where the cookie expired i.e.: - dump 725 * current cookie. - generate a new vtag. - resend init. 726 */ 727 /* generate a new vtag and send init */ 728 LIST_REMOVE(stcb, sctp_asocs); 729 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED; 730 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT; 731 sctp_stop_all_cookie_timers(stcb); 732 sctp_toss_old_cookies(stcb, &stcb->asoc); 733 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 734 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 735 /* 736 * put it in the bucket in the vtag hash of assoc's for the 737 * system 738 */ 739 LIST_INSERT_HEAD(head, stcb, sctp_asocs); 740 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 741 return (1); 742 } 743 return (0); 744 } 745 746 static int 747 sctp_handle_nat_missing_state(struct sctp_tcb *stcb, 748 struct sctp_nets *net) 749 { 750 /* 751 * return 0 means we want you to proceed with the abort non-zero 752 * means no abort processing 753 */ 754 if (stcb->asoc.peer_supports_auth == 0) { 755 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n"); 756 return (0); 757 } 758 sctp_asconf_send_nat_state_update(stcb, net); 759 return (1); 760 } 761 762 763 static void 764 sctp_handle_abort(struct sctp_abort_chunk *abort, 765 struct sctp_tcb *stcb, struct sctp_nets *net) 766 { 767 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 768 struct socket *so; 769 770 #endif 771 uint16_t len; 772 uint16_t error; 773 774 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n"); 775 if (stcb == NULL) 776 return; 777 778 len = ntohs(abort->ch.chunk_length); 779 if (len > sizeof(struct sctp_chunkhdr)) { 780 /* 781 * Need to check the cause codes for our two magic nat 782 * aborts which don't kill the assoc necessarily. 783 */ 784 struct sctp_missing_nat_state *natc; 785 786 natc = (struct sctp_missing_nat_state *)(abort + 1); 787 error = ntohs(natc->cause); 788 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) { 789 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", 790 abort->ch.chunk_flags); 791 if (sctp_handle_nat_colliding_state(stcb)) { 792 return; 793 } 794 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) { 795 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", 796 abort->ch.chunk_flags); 797 if (sctp_handle_nat_missing_state(stcb, net)) { 798 return; 799 } 800 } 801 } else { 802 error = 0; 803 } 804 /* stop any receive timers */ 805 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 806 /* notify user of the abort and clean up... */ 807 sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED); 808 /* free the tcb */ 809 SCTP_STAT_INCR_COUNTER32(sctps_aborted); 810 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 811 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 812 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 813 } 814 #ifdef SCTP_ASOCLOG_OF_TSNS 815 sctp_print_out_track_log(stcb); 816 #endif 817 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 818 so = SCTP_INP_SO(stcb->sctp_ep); 819 atomic_add_int(&stcb->asoc.refcnt, 1); 820 SCTP_TCB_UNLOCK(stcb); 821 SCTP_SOCKET_LOCK(so, 1); 822 SCTP_TCB_LOCK(stcb); 823 atomic_subtract_int(&stcb->asoc.refcnt, 1); 824 #endif 825 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED; 826 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 827 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 828 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 829 SCTP_SOCKET_UNLOCK(so, 1); 830 #endif 831 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n"); 832 } 833 834 static void 835 sctp_start_net_timers(struct sctp_tcb *stcb) 836 { 837 uint32_t cnt_hb_sent; 838 struct sctp_nets *net; 839 840 cnt_hb_sent = 0; 841 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 842 /* 843 * For each network start: 1) A pmtu timer. 2) A HB timer 3) 844 * If the dest in unconfirmed send a hb as well if under 845 * max_hb_burst have been sent. 846 */ 847 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net); 848 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net); 849 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) && 850 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) { 851 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED); 852 cnt_hb_sent++; 853 } 854 } 855 if (cnt_hb_sent) { 856 sctp_chunk_output(stcb->sctp_ep, stcb, 857 SCTP_OUTPUT_FROM_COOKIE_ACK, 858 SCTP_SO_NOT_LOCKED); 859 } 860 } 861 862 863 static void 864 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp, 865 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) 866 { 867 struct sctp_association *asoc; 868 int some_on_streamwheel; 869 870 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 871 struct socket *so; 872 873 #endif 874 875 SCTPDBG(SCTP_DEBUG_INPUT2, 876 "sctp_handle_shutdown: handling SHUTDOWN\n"); 877 if (stcb == NULL) 878 return; 879 asoc = &stcb->asoc; 880 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 881 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 882 return; 883 } 884 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { 885 /* Shutdown NOT the expected size */ 886 return; 887 } else { 888 sctp_update_acked(stcb, cp, abort_flag); 889 if (*abort_flag) { 890 return; 891 } 892 } 893 if (asoc->control_pdapi) { 894 /* 895 * With a normal shutdown we assume the end of last record. 896 */ 897 SCTP_INP_READ_LOCK(stcb->sctp_ep); 898 asoc->control_pdapi->end_added = 1; 899 asoc->control_pdapi->pdapi_aborted = 1; 900 asoc->control_pdapi = NULL; 901 SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 902 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 903 so = SCTP_INP_SO(stcb->sctp_ep); 904 atomic_add_int(&stcb->asoc.refcnt, 1); 905 SCTP_TCB_UNLOCK(stcb); 906 SCTP_SOCKET_LOCK(so, 1); 907 SCTP_TCB_LOCK(stcb); 908 atomic_subtract_int(&stcb->asoc.refcnt, 1); 909 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 910 /* assoc was freed while we were unlocked */ 911 SCTP_SOCKET_UNLOCK(so, 1); 912 return; 913 } 914 #endif 915 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 916 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 917 SCTP_SOCKET_UNLOCK(so, 1); 918 #endif 919 } 920 /* goto SHUTDOWN_RECEIVED state to block new requests */ 921 if (stcb->sctp_socket) { 922 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 923 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) && 924 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 925 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED); 926 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 927 /* 928 * notify upper layer that peer has initiated a 929 * shutdown 930 */ 931 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 932 933 /* reset time */ 934 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 935 } 936 } 937 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 938 /* 939 * stop the shutdown timer, since we WILL move to 940 * SHUTDOWN-ACK-SENT. 941 */ 942 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8); 943 } 944 /* Now is there unsent data on a stream somewhere? */ 945 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED); 946 947 if (!TAILQ_EMPTY(&asoc->send_queue) || 948 !TAILQ_EMPTY(&asoc->sent_queue) || 949 some_on_streamwheel) { 950 /* By returning we will push more data out */ 951 return; 952 } else { 953 /* no outstanding data to send, so move on... */ 954 /* send SHUTDOWN-ACK */ 955 /* move to SHUTDOWN-ACK-SENT state */ 956 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 957 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 958 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 959 } 960 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT); 961 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 962 sctp_stop_timers_for_shutdown(stcb); 963 sctp_send_shutdown_ack(stcb, net); 964 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, 965 stcb, net); 966 } 967 } 968 969 static void 970 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED, 971 struct sctp_tcb *stcb, 972 struct sctp_nets *net) 973 { 974 struct sctp_association *asoc; 975 976 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 977 struct socket *so; 978 979 so = SCTP_INP_SO(stcb->sctp_ep); 980 #endif 981 SCTPDBG(SCTP_DEBUG_INPUT2, 982 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); 983 if (stcb == NULL) 984 return; 985 986 asoc = &stcb->asoc; 987 /* process according to association state */ 988 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 989 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 990 /* unexpected SHUTDOWN-ACK... do OOTB handling... */ 991 sctp_send_shutdown_complete(stcb, net, 1); 992 SCTP_TCB_UNLOCK(stcb); 993 return; 994 } 995 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 996 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 997 /* unexpected SHUTDOWN-ACK... so ignore... */ 998 SCTP_TCB_UNLOCK(stcb); 999 return; 1000 } 1001 if (asoc->control_pdapi) { 1002 /* 1003 * With a normal shutdown we assume the end of last record. 1004 */ 1005 SCTP_INP_READ_LOCK(stcb->sctp_ep); 1006 asoc->control_pdapi->end_added = 1; 1007 asoc->control_pdapi->pdapi_aborted = 1; 1008 asoc->control_pdapi = NULL; 1009 SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 1010 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1011 atomic_add_int(&stcb->asoc.refcnt, 1); 1012 SCTP_TCB_UNLOCK(stcb); 1013 SCTP_SOCKET_LOCK(so, 1); 1014 SCTP_TCB_LOCK(stcb); 1015 atomic_subtract_int(&stcb->asoc.refcnt, 1); 1016 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1017 /* assoc was freed while we were unlocked */ 1018 SCTP_SOCKET_UNLOCK(so, 1); 1019 return; 1020 } 1021 #endif 1022 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 1023 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1024 SCTP_SOCKET_UNLOCK(so, 1); 1025 #endif 1026 } 1027 #ifdef INVARIANTS 1028 if (!TAILQ_EMPTY(&asoc->send_queue) || 1029 !TAILQ_EMPTY(&asoc->sent_queue) || 1030 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 1031 panic("Queues are not empty when handling SHUTDOWN-ACK"); 1032 } 1033 #endif 1034 /* stop the timer */ 1035 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9); 1036 /* send SHUTDOWN-COMPLETE */ 1037 sctp_send_shutdown_complete(stcb, net, 0); 1038 /* notify upper layer protocol */ 1039 if (stcb->sctp_socket) { 1040 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1041 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 1042 stcb->sctp_socket->so_snd.sb_cc = 0; 1043 } 1044 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 1045 } 1046 SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 1047 /* free the TCB but first save off the ep */ 1048 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1049 atomic_add_int(&stcb->asoc.refcnt, 1); 1050 SCTP_TCB_UNLOCK(stcb); 1051 SCTP_SOCKET_LOCK(so, 1); 1052 SCTP_TCB_LOCK(stcb); 1053 atomic_subtract_int(&stcb->asoc.refcnt, 1); 1054 #endif 1055 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1056 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10); 1057 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1058 SCTP_SOCKET_UNLOCK(so, 1); 1059 #endif 1060 } 1061 1062 /* 1063 * Skip past the param header and then we will find the chunk that caused the 1064 * problem. There are two possiblities ASCONF or FWD-TSN other than that and 1065 * our peer must be broken. 1066 */ 1067 static void 1068 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr, 1069 struct sctp_nets *net) 1070 { 1071 struct sctp_chunkhdr *chk; 1072 1073 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr)); 1074 switch (chk->chunk_type) { 1075 case SCTP_ASCONF_ACK: 1076 case SCTP_ASCONF: 1077 sctp_asconf_cleanup(stcb, net); 1078 break; 1079 case SCTP_FORWARD_CUM_TSN: 1080 stcb->asoc.peer_supports_prsctp = 0; 1081 break; 1082 default: 1083 SCTPDBG(SCTP_DEBUG_INPUT2, 1084 "Peer does not support chunk type %d(%x)??\n", 1085 chk->chunk_type, (uint32_t) chk->chunk_type); 1086 break; 1087 } 1088 } 1089 1090 /* 1091 * Skip past the param header and then we will find the param that caused the 1092 * problem. There are a number of param's in a ASCONF OR the prsctp param 1093 * these will turn of specific features. 1094 */ 1095 static void 1096 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr) 1097 { 1098 struct sctp_paramhdr *pbad; 1099 1100 pbad = phdr + 1; 1101 switch (ntohs(pbad->param_type)) { 1102 /* pr-sctp draft */ 1103 case SCTP_PRSCTP_SUPPORTED: 1104 stcb->asoc.peer_supports_prsctp = 0; 1105 break; 1106 case SCTP_SUPPORTED_CHUNK_EXT: 1107 break; 1108 /* draft-ietf-tsvwg-addip-sctp */ 1109 case SCTP_HAS_NAT_SUPPORT: 1110 stcb->asoc.peer_supports_nat = 0; 1111 break; 1112 case SCTP_ADD_IP_ADDRESS: 1113 case SCTP_DEL_IP_ADDRESS: 1114 case SCTP_SET_PRIM_ADDR: 1115 stcb->asoc.peer_supports_asconf = 0; 1116 break; 1117 case SCTP_SUCCESS_REPORT: 1118 case SCTP_ERROR_CAUSE_IND: 1119 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n"); 1120 SCTPDBG(SCTP_DEBUG_INPUT2, 1121 "Turning off ASCONF to this strange peer\n"); 1122 stcb->asoc.peer_supports_asconf = 0; 1123 break; 1124 default: 1125 SCTPDBG(SCTP_DEBUG_INPUT2, 1126 "Peer does not support param type %d(%x)??\n", 1127 pbad->param_type, (uint32_t) pbad->param_type); 1128 break; 1129 } 1130 } 1131 1132 static int 1133 sctp_handle_error(struct sctp_chunkhdr *ch, 1134 struct sctp_tcb *stcb, struct sctp_nets *net) 1135 { 1136 int chklen; 1137 struct sctp_paramhdr *phdr; 1138 uint16_t error, error_type; 1139 uint16_t error_len; 1140 struct sctp_association *asoc; 1141 int adjust; 1142 1143 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1144 struct socket *so; 1145 1146 #endif 1147 1148 /* parse through all of the errors and process */ 1149 asoc = &stcb->asoc; 1150 phdr = (struct sctp_paramhdr *)((caddr_t)ch + 1151 sizeof(struct sctp_chunkhdr)); 1152 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr); 1153 error = 0; 1154 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { 1155 /* Process an Error Cause */ 1156 error_type = ntohs(phdr->param_type); 1157 error_len = ntohs(phdr->param_length); 1158 if ((error_len > chklen) || (error_len == 0)) { 1159 /* invalid param length for this param */ 1160 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n", 1161 chklen, error_len); 1162 return (0); 1163 } 1164 if (error == 0) { 1165 /* report the first error cause */ 1166 error = error_type; 1167 } 1168 switch (error_type) { 1169 case SCTP_CAUSE_INVALID_STREAM: 1170 case SCTP_CAUSE_MISSING_PARAM: 1171 case SCTP_CAUSE_INVALID_PARAM: 1172 case SCTP_CAUSE_NO_USER_DATA: 1173 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n", 1174 error_type); 1175 break; 1176 case SCTP_CAUSE_NAT_COLLIDING_STATE: 1177 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", 1178 ch->chunk_flags); 1179 if (sctp_handle_nat_colliding_state(stcb)) { 1180 return (0); 1181 } 1182 break; 1183 case SCTP_CAUSE_NAT_MISSING_STATE: 1184 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", 1185 ch->chunk_flags); 1186 if (sctp_handle_nat_missing_state(stcb, net)) { 1187 return (0); 1188 } 1189 break; 1190 case SCTP_CAUSE_STALE_COOKIE: 1191 /* 1192 * We only act if we have echoed a cookie and are 1193 * waiting. 1194 */ 1195 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 1196 int *p; 1197 1198 p = (int *)((caddr_t)phdr + sizeof(*phdr)); 1199 /* Save the time doubled */ 1200 asoc->cookie_preserve_req = ntohl(*p) << 1; 1201 asoc->stale_cookie_count++; 1202 if (asoc->stale_cookie_count > 1203 asoc->max_init_times) { 1204 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED); 1205 /* now free the asoc */ 1206 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1207 so = SCTP_INP_SO(stcb->sctp_ep); 1208 atomic_add_int(&stcb->asoc.refcnt, 1); 1209 SCTP_TCB_UNLOCK(stcb); 1210 SCTP_SOCKET_LOCK(so, 1); 1211 SCTP_TCB_LOCK(stcb); 1212 atomic_subtract_int(&stcb->asoc.refcnt, 1); 1213 #endif 1214 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1215 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 1216 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1217 SCTP_SOCKET_UNLOCK(so, 1); 1218 #endif 1219 return (-1); 1220 } 1221 /* blast back to INIT state */ 1222 sctp_toss_old_cookies(stcb, &stcb->asoc); 1223 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; 1224 asoc->state |= SCTP_STATE_COOKIE_WAIT; 1225 sctp_stop_all_cookie_timers(stcb); 1226 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 1227 } 1228 break; 1229 case SCTP_CAUSE_UNRESOLVABLE_ADDR: 1230 /* 1231 * Nothing we can do here, we don't do hostname 1232 * addresses so if the peer does not like my IPv6 1233 * (or IPv4 for that matter) it does not matter. If 1234 * they don't support that type of address, they can 1235 * NOT possibly get that packet type... i.e. with no 1236 * IPv6 you can't recieve a IPv6 packet. so we can 1237 * safely ignore this one. If we ever added support 1238 * for HOSTNAME Addresses, then we would need to do 1239 * something here. 1240 */ 1241 break; 1242 case SCTP_CAUSE_UNRECOG_CHUNK: 1243 sctp_process_unrecog_chunk(stcb, phdr, net); 1244 break; 1245 case SCTP_CAUSE_UNRECOG_PARAM: 1246 sctp_process_unrecog_param(stcb, phdr); 1247 break; 1248 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 1249 /* 1250 * We ignore this since the timer will drive out a 1251 * new cookie anyway and there timer will drive us 1252 * to send a SHUTDOWN_COMPLETE. We can't send one 1253 * here since we don't have their tag. 1254 */ 1255 break; 1256 case SCTP_CAUSE_DELETING_LAST_ADDR: 1257 case SCTP_CAUSE_RESOURCE_SHORTAGE: 1258 case SCTP_CAUSE_DELETING_SRC_ADDR: 1259 /* 1260 * We should NOT get these here, but in a 1261 * ASCONF-ACK. 1262 */ 1263 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n", 1264 error_type); 1265 break; 1266 case SCTP_CAUSE_OUT_OF_RESC: 1267 /* 1268 * And what, pray tell do we do with the fact that 1269 * the peer is out of resources? Not really sure we 1270 * could do anything but abort. I suspect this 1271 * should have came WITH an abort instead of in a 1272 * OP-ERROR. 1273 */ 1274 break; 1275 default: 1276 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n", 1277 error_type); 1278 break; 1279 } 1280 adjust = SCTP_SIZE32(error_len); 1281 chklen -= adjust; 1282 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); 1283 } 1284 sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, error, ch, SCTP_SO_NOT_LOCKED); 1285 return (0); 1286 } 1287 1288 static int 1289 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, 1290 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh, 1291 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 1292 struct sctp_nets *net, int *abort_no_unlock, 1293 uint8_t use_mflowid, uint32_t mflowid, 1294 uint32_t vrf_id) 1295 { 1296 struct sctp_init_ack *init_ack; 1297 struct mbuf *op_err; 1298 1299 SCTPDBG(SCTP_DEBUG_INPUT2, 1300 "sctp_handle_init_ack: handling INIT-ACK\n"); 1301 1302 if (stcb == NULL) { 1303 SCTPDBG(SCTP_DEBUG_INPUT2, 1304 "sctp_handle_init_ack: TCB is null\n"); 1305 return (-1); 1306 } 1307 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { 1308 /* Invalid length */ 1309 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1310 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 1311 src, dst, sh, op_err, 1312 use_mflowid, mflowid, 1313 vrf_id, net->port); 1314 *abort_no_unlock = 1; 1315 return (-1); 1316 } 1317 init_ack = &cp->init; 1318 /* validate parameters */ 1319 if (init_ack->initiate_tag == 0) { 1320 /* protocol error... send an abort */ 1321 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1322 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 1323 src, dst, sh, op_err, 1324 use_mflowid, mflowid, 1325 vrf_id, net->port); 1326 *abort_no_unlock = 1; 1327 return (-1); 1328 } 1329 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { 1330 /* protocol error... send an abort */ 1331 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1332 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 1333 src, dst, sh, op_err, 1334 use_mflowid, mflowid, 1335 vrf_id, net->port); 1336 *abort_no_unlock = 1; 1337 return (-1); 1338 } 1339 if (init_ack->num_inbound_streams == 0) { 1340 /* protocol error... send an abort */ 1341 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1342 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 1343 src, dst, sh, op_err, 1344 use_mflowid, mflowid, 1345 vrf_id, net->port); 1346 *abort_no_unlock = 1; 1347 return (-1); 1348 } 1349 if (init_ack->num_outbound_streams == 0) { 1350 /* protocol error... send an abort */ 1351 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1352 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 1353 src, dst, sh, op_err, 1354 use_mflowid, mflowid, 1355 vrf_id, net->port); 1356 *abort_no_unlock = 1; 1357 return (-1); 1358 } 1359 /* process according to association state... */ 1360 switch (stcb->asoc.state & SCTP_STATE_MASK) { 1361 case SCTP_STATE_COOKIE_WAIT: 1362 /* this is the expected state for this chunk */ 1363 /* process the INIT-ACK parameters */ 1364 if (stcb->asoc.primary_destination->dest_state & 1365 SCTP_ADDR_UNCONFIRMED) { 1366 /* 1367 * The primary is where we sent the INIT, we can 1368 * always consider it confirmed when the INIT-ACK is 1369 * returned. Do this before we load addresses 1370 * though. 1371 */ 1372 stcb->asoc.primary_destination->dest_state &= 1373 ~SCTP_ADDR_UNCONFIRMED; 1374 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 1375 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED); 1376 } 1377 if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb, 1378 net, abort_no_unlock, 1379 use_mflowid, mflowid, 1380 vrf_id) < 0) { 1381 /* error in parsing parameters */ 1382 return (-1); 1383 } 1384 /* update our state */ 1385 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n"); 1386 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED); 1387 1388 /* reset the RTO calc */ 1389 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 1390 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 1391 stcb->asoc.overall_error_count, 1392 0, 1393 SCTP_FROM_SCTP_INPUT, 1394 __LINE__); 1395 } 1396 stcb->asoc.overall_error_count = 0; 1397 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1398 /* 1399 * collapse the init timer back in case of a exponential 1400 * backoff 1401 */ 1402 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 1403 stcb, net); 1404 /* 1405 * the send at the end of the inbound data processing will 1406 * cause the cookie to be sent 1407 */ 1408 break; 1409 case SCTP_STATE_SHUTDOWN_SENT: 1410 /* incorrect state... discard */ 1411 break; 1412 case SCTP_STATE_COOKIE_ECHOED: 1413 /* incorrect state... discard */ 1414 break; 1415 case SCTP_STATE_OPEN: 1416 /* incorrect state... discard */ 1417 break; 1418 case SCTP_STATE_EMPTY: 1419 case SCTP_STATE_INUSE: 1420 default: 1421 /* incorrect state... discard */ 1422 return (-1); 1423 break; 1424 } 1425 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n"); 1426 return (0); 1427 } 1428 1429 static struct sctp_tcb * 1430 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1431 struct sockaddr *src, struct sockaddr *dst, 1432 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1433 struct sctp_inpcb *inp, struct sctp_nets **netp, 1434 struct sockaddr *init_src, int *notification, 1435 int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1436 uint8_t use_mflowid, uint32_t mflowid, 1437 uint32_t vrf_id, uint16_t port); 1438 1439 1440 /* 1441 * handle a state cookie for an existing association m: input packet mbuf 1442 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 1443 * "split" mbuf and the cookie signature does not exist offset: offset into 1444 * mbuf to the cookie-echo chunk 1445 */ 1446 static struct sctp_tcb * 1447 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 1448 struct sockaddr *src, struct sockaddr *dst, 1449 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1450 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp, 1451 struct sockaddr *init_src, int *notification, 1452 int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1453 uint8_t use_mflowid, uint32_t mflowid, 1454 uint32_t vrf_id, uint16_t port) 1455 { 1456 struct sctp_association *asoc; 1457 struct sctp_init_chunk *init_cp, init_buf; 1458 struct sctp_init_ack_chunk *initack_cp, initack_buf; 1459 struct sctp_nets *net; 1460 struct mbuf *op_err; 1461 struct sctp_paramhdr *ph; 1462 int init_offset, initack_offset, i; 1463 int retval; 1464 int spec_flag = 0; 1465 uint32_t how_indx; 1466 1467 net = *netp; 1468 /* I know that the TCB is non-NULL from the caller */ 1469 asoc = &stcb->asoc; 1470 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { 1471 if (asoc->cookie_how[how_indx] == 0) 1472 break; 1473 } 1474 if (how_indx < sizeof(asoc->cookie_how)) { 1475 asoc->cookie_how[how_indx] = 1; 1476 } 1477 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 1478 /* SHUTDOWN came in after sending INIT-ACK */ 1479 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 1480 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1481 0, M_NOWAIT, 1, MT_DATA); 1482 if (op_err == NULL) { 1483 /* FOOBAR */ 1484 return (NULL); 1485 } 1486 /* Set the len */ 1487 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1488 ph = mtod(op_err, struct sctp_paramhdr *); 1489 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN); 1490 ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1491 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err, 1492 use_mflowid, mflowid, 1493 vrf_id, net->port); 1494 if (how_indx < sizeof(asoc->cookie_how)) 1495 asoc->cookie_how[how_indx] = 2; 1496 return (NULL); 1497 } 1498 /* 1499 * find and validate the INIT chunk in the cookie (peer's info) the 1500 * INIT should start after the cookie-echo header struct (chunk 1501 * header, state cookie header struct) 1502 */ 1503 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); 1504 1505 init_cp = (struct sctp_init_chunk *) 1506 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1507 (uint8_t *) & init_buf); 1508 if (init_cp == NULL) { 1509 /* could not pull a INIT chunk in cookie */ 1510 return (NULL); 1511 } 1512 if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1513 return (NULL); 1514 } 1515 /* 1516 * find and validate the INIT-ACK chunk in the cookie (my info) the 1517 * INIT-ACK follows the INIT chunk 1518 */ 1519 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length)); 1520 initack_cp = (struct sctp_init_ack_chunk *) 1521 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1522 (uint8_t *) & initack_buf); 1523 if (initack_cp == NULL) { 1524 /* could not pull INIT-ACK chunk in cookie */ 1525 return (NULL); 1526 } 1527 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1528 return (NULL); 1529 } 1530 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1531 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { 1532 /* 1533 * case D in Section 5.2.4 Table 2: MMAA process accordingly 1534 * to get into the OPEN state 1535 */ 1536 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1537 /*- 1538 * Opps, this means that we somehow generated two vtag's 1539 * the same. I.e. we did: 1540 * Us Peer 1541 * <---INIT(tag=a)------ 1542 * ----INIT-ACK(tag=t)--> 1543 * ----INIT(tag=t)------> *1 1544 * <---INIT-ACK(tag=a)--- 1545 * <----CE(tag=t)------------- *2 1546 * 1547 * At point *1 we should be generating a different 1548 * tag t'. Which means we would throw away the CE and send 1549 * ours instead. Basically this is case C (throw away side). 1550 */ 1551 if (how_indx < sizeof(asoc->cookie_how)) 1552 asoc->cookie_how[how_indx] = 17; 1553 return (NULL); 1554 1555 } 1556 switch SCTP_GET_STATE 1557 (asoc) { 1558 case SCTP_STATE_COOKIE_WAIT: 1559 case SCTP_STATE_COOKIE_ECHOED: 1560 /* 1561 * INIT was sent but got a COOKIE_ECHO with the 1562 * correct tags... just accept it...but we must 1563 * process the init so that we can make sure we have 1564 * the right seq no's. 1565 */ 1566 /* First we must process the INIT !! */ 1567 retval = sctp_process_init(init_cp, stcb); 1568 if (retval < 0) { 1569 if (how_indx < sizeof(asoc->cookie_how)) 1570 asoc->cookie_how[how_indx] = 3; 1571 return (NULL); 1572 } 1573 /* we have already processed the INIT so no problem */ 1574 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, 1575 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12); 1576 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13); 1577 /* update current state */ 1578 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1579 SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1580 else 1581 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1582 1583 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1584 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1585 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1586 stcb->sctp_ep, stcb, asoc->primary_destination); 1587 } 1588 SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1589 sctp_stop_all_cookie_timers(stcb); 1590 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1591 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1592 (inp->sctp_socket->so_qlimit == 0) 1593 ) { 1594 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1595 struct socket *so; 1596 1597 #endif 1598 /* 1599 * Here is where collision would go if we 1600 * did a connect() and instead got a 1601 * init/init-ack/cookie done before the 1602 * init-ack came back.. 1603 */ 1604 stcb->sctp_ep->sctp_flags |= 1605 SCTP_PCB_FLAGS_CONNECTED; 1606 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1607 so = SCTP_INP_SO(stcb->sctp_ep); 1608 atomic_add_int(&stcb->asoc.refcnt, 1); 1609 SCTP_TCB_UNLOCK(stcb); 1610 SCTP_SOCKET_LOCK(so, 1); 1611 SCTP_TCB_LOCK(stcb); 1612 atomic_add_int(&stcb->asoc.refcnt, -1); 1613 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1614 SCTP_SOCKET_UNLOCK(so, 1); 1615 return (NULL); 1616 } 1617 #endif 1618 soisconnected(stcb->sctp_socket); 1619 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1620 SCTP_SOCKET_UNLOCK(so, 1); 1621 #endif 1622 } 1623 /* notify upper layer */ 1624 *notification = SCTP_NOTIFY_ASSOC_UP; 1625 /* 1626 * since we did not send a HB make sure we don't 1627 * double things 1628 */ 1629 net->hb_responded = 1; 1630 net->RTO = sctp_calculate_rto(stcb, asoc, net, 1631 &cookie->time_entered, 1632 sctp_align_unsafe_makecopy, 1633 SCTP_RTT_FROM_NON_DATA); 1634 1635 if (stcb->asoc.sctp_autoclose_ticks && 1636 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { 1637 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 1638 inp, stcb, NULL); 1639 } 1640 break; 1641 default: 1642 /* 1643 * we're in the OPEN state (or beyond), so peer must 1644 * have simply lost the COOKIE-ACK 1645 */ 1646 break; 1647 } /* end switch */ 1648 sctp_stop_all_cookie_timers(stcb); 1649 /* 1650 * We ignore the return code here.. not sure if we should 1651 * somehow abort.. but we do have an existing asoc. This 1652 * really should not fail. 1653 */ 1654 if (sctp_load_addresses_from_init(stcb, m, 1655 init_offset + sizeof(struct sctp_init_chunk), 1656 initack_offset, src, dst, init_src)) { 1657 if (how_indx < sizeof(asoc->cookie_how)) 1658 asoc->cookie_how[how_indx] = 4; 1659 return (NULL); 1660 } 1661 /* respond with a COOKIE-ACK */ 1662 sctp_toss_old_cookies(stcb, asoc); 1663 sctp_send_cookie_ack(stcb); 1664 if (how_indx < sizeof(asoc->cookie_how)) 1665 asoc->cookie_how[how_indx] = 5; 1666 return (stcb); 1667 } 1668 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1669 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && 1670 cookie->tie_tag_my_vtag == 0 && 1671 cookie->tie_tag_peer_vtag == 0) { 1672 /* 1673 * case C in Section 5.2.4 Table 2: XMOO silently discard 1674 */ 1675 if (how_indx < sizeof(asoc->cookie_how)) 1676 asoc->cookie_how[how_indx] = 6; 1677 return (NULL); 1678 } 1679 /* 1680 * If nat support, and the below and stcb is established, send back 1681 * a ABORT(colliding state) if we are established. 1682 */ 1683 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) && 1684 (asoc->peer_supports_nat) && 1685 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1686 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1687 (asoc->peer_vtag == 0)))) { 1688 /* 1689 * Special case - Peer's support nat. We may have two init's 1690 * that we gave out the same tag on since one was not 1691 * established.. i.e. we get INIT from host-1 behind the nat 1692 * and we respond tag-a, we get a INIT from host-2 behind 1693 * the nat and we get tag-a again. Then we bring up host-1 1694 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1). 1695 * Now we have colliding state. We must send an abort here 1696 * with colliding state indication. 1697 */ 1698 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1699 0, M_NOWAIT, 1, MT_DATA); 1700 if (op_err == NULL) { 1701 /* FOOBAR */ 1702 return (NULL); 1703 } 1704 /* pre-reserve some space */ 1705 #ifdef INET6 1706 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1707 #else 1708 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); 1709 #endif 1710 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1711 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1712 /* Set the len */ 1713 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1714 ph = mtod(op_err, struct sctp_paramhdr *); 1715 ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE); 1716 ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1717 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err, 1718 use_mflowid, mflowid, 1719 vrf_id, port); 1720 return (NULL); 1721 } 1722 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1723 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1724 (asoc->peer_vtag == 0))) { 1725 /* 1726 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info 1727 * should be ok, re-accept peer info 1728 */ 1729 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1730 /* 1731 * Extension of case C. If we hit this, then the 1732 * random number generator returned the same vtag 1733 * when we first sent our INIT-ACK and when we later 1734 * sent our INIT. The side with the seq numbers that 1735 * are different will be the one that normnally 1736 * would have hit case C. This in effect "extends" 1737 * our vtags in this collision case to be 64 bits. 1738 * The same collision could occur aka you get both 1739 * vtag and seq number the same twice in a row.. but 1740 * is much less likely. If it did happen then we 1741 * would proceed through and bring up the assoc.. we 1742 * may end up with the wrong stream setup however.. 1743 * which would be bad.. but there is no way to 1744 * tell.. until we send on a stream that does not 1745 * exist :-) 1746 */ 1747 if (how_indx < sizeof(asoc->cookie_how)) 1748 asoc->cookie_how[how_indx] = 7; 1749 1750 return (NULL); 1751 } 1752 if (how_indx < sizeof(asoc->cookie_how)) 1753 asoc->cookie_how[how_indx] = 8; 1754 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14); 1755 sctp_stop_all_cookie_timers(stcb); 1756 /* 1757 * since we did not send a HB make sure we don't double 1758 * things 1759 */ 1760 net->hb_responded = 1; 1761 if (stcb->asoc.sctp_autoclose_ticks && 1762 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1763 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, 1764 NULL); 1765 } 1766 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1767 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1768 1769 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { 1770 /* 1771 * Ok the peer probably discarded our data (if we 1772 * echoed a cookie+data). So anything on the 1773 * sent_queue should be marked for retransmit, we 1774 * may not get something to kick us so it COULD 1775 * still take a timeout to move these.. but it can't 1776 * hurt to mark them. 1777 */ 1778 struct sctp_tmit_chunk *chk; 1779 1780 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 1781 if (chk->sent < SCTP_DATAGRAM_RESEND) { 1782 chk->sent = SCTP_DATAGRAM_RESEND; 1783 sctp_flight_size_decrease(chk); 1784 sctp_total_flight_decrease(stcb, chk); 1785 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1786 spec_flag++; 1787 } 1788 } 1789 1790 } 1791 /* process the INIT info (peer's info) */ 1792 retval = sctp_process_init(init_cp, stcb); 1793 if (retval < 0) { 1794 if (how_indx < sizeof(asoc->cookie_how)) 1795 asoc->cookie_how[how_indx] = 9; 1796 return (NULL); 1797 } 1798 if (sctp_load_addresses_from_init(stcb, m, 1799 init_offset + sizeof(struct sctp_init_chunk), 1800 initack_offset, src, dst, init_src)) { 1801 if (how_indx < sizeof(asoc->cookie_how)) 1802 asoc->cookie_how[how_indx] = 10; 1803 return (NULL); 1804 } 1805 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) || 1806 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) { 1807 *notification = SCTP_NOTIFY_ASSOC_UP; 1808 1809 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1810 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1811 (inp->sctp_socket->so_qlimit == 0)) { 1812 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1813 struct socket *so; 1814 1815 #endif 1816 stcb->sctp_ep->sctp_flags |= 1817 SCTP_PCB_FLAGS_CONNECTED; 1818 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1819 so = SCTP_INP_SO(stcb->sctp_ep); 1820 atomic_add_int(&stcb->asoc.refcnt, 1); 1821 SCTP_TCB_UNLOCK(stcb); 1822 SCTP_SOCKET_LOCK(so, 1); 1823 SCTP_TCB_LOCK(stcb); 1824 atomic_add_int(&stcb->asoc.refcnt, -1); 1825 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1826 SCTP_SOCKET_UNLOCK(so, 1); 1827 return (NULL); 1828 } 1829 #endif 1830 soisconnected(stcb->sctp_socket); 1831 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1832 SCTP_SOCKET_UNLOCK(so, 1); 1833 #endif 1834 } 1835 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1836 SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1837 else 1838 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1839 SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1840 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1841 SCTP_STAT_INCR_COUNTER32(sctps_restartestab); 1842 } else { 1843 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1844 } 1845 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1846 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1847 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1848 stcb->sctp_ep, stcb, asoc->primary_destination); 1849 } 1850 sctp_stop_all_cookie_timers(stcb); 1851 sctp_toss_old_cookies(stcb, asoc); 1852 sctp_send_cookie_ack(stcb); 1853 if (spec_flag) { 1854 /* 1855 * only if we have retrans set do we do this. What 1856 * this call does is get only the COOKIE-ACK out and 1857 * then when we return the normal call to 1858 * sctp_chunk_output will get the retrans out behind 1859 * this. 1860 */ 1861 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED); 1862 } 1863 if (how_indx < sizeof(asoc->cookie_how)) 1864 asoc->cookie_how[how_indx] = 11; 1865 1866 return (stcb); 1867 } 1868 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1869 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && 1870 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && 1871 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && 1872 cookie->tie_tag_peer_vtag != 0) { 1873 struct sctpasochead *head; 1874 1875 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1876 struct socket *so; 1877 1878 #endif 1879 1880 if (asoc->peer_supports_nat) { 1881 /* 1882 * This is a gross gross hack. Just call the 1883 * cookie_new code since we are allowing a duplicate 1884 * association. I hope this works... 1885 */ 1886 return (sctp_process_cookie_new(m, iphlen, offset, src, dst, 1887 sh, cookie, cookie_len, 1888 inp, netp, init_src, notification, 1889 auth_skipped, auth_offset, auth_len, 1890 use_mflowid, mflowid, 1891 vrf_id, port)); 1892 } 1893 /* 1894 * case A in Section 5.2.4 Table 2: XXMM (peer restarted) 1895 */ 1896 /* temp code */ 1897 if (how_indx < sizeof(asoc->cookie_how)) 1898 asoc->cookie_how[how_indx] = 12; 1899 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15); 1900 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1901 1902 /* notify upper layer */ 1903 *notification = SCTP_NOTIFY_ASSOC_RESTART; 1904 atomic_add_int(&stcb->asoc.refcnt, 1); 1905 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) && 1906 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 1907 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 1908 SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1909 } 1910 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1911 SCTP_STAT_INCR_GAUGE32(sctps_restartestab); 1912 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 1913 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); 1914 } 1915 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1916 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1917 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1918 stcb->sctp_ep, stcb, asoc->primary_destination); 1919 1920 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) { 1921 /* move to OPEN state, if not in SHUTDOWN_SENT */ 1922 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1923 } 1924 asoc->pre_open_streams = 1925 ntohs(initack_cp->init.num_outbound_streams); 1926 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1927 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1928 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 1929 1930 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1931 1932 asoc->str_reset_seq_in = asoc->init_seq_number; 1933 1934 asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1935 if (asoc->mapping_array) { 1936 memset(asoc->mapping_array, 0, 1937 asoc->mapping_array_size); 1938 } 1939 if (asoc->nr_mapping_array) { 1940 memset(asoc->nr_mapping_array, 0, 1941 asoc->mapping_array_size); 1942 } 1943 SCTP_TCB_UNLOCK(stcb); 1944 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1945 so = SCTP_INP_SO(stcb->sctp_ep); 1946 SCTP_SOCKET_LOCK(so, 1); 1947 #endif 1948 SCTP_INP_INFO_WLOCK(); 1949 SCTP_INP_WLOCK(stcb->sctp_ep); 1950 SCTP_TCB_LOCK(stcb); 1951 atomic_add_int(&stcb->asoc.refcnt, -1); 1952 /* send up all the data */ 1953 SCTP_TCB_SEND_LOCK(stcb); 1954 1955 sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_LOCKED); 1956 for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1957 stcb->asoc.strmout[i].chunks_on_queues = 0; 1958 stcb->asoc.strmout[i].stream_no = i; 1959 stcb->asoc.strmout[i].next_sequence_send = 0; 1960 stcb->asoc.strmout[i].last_msg_incomplete = 0; 1961 } 1962 /* process the INIT-ACK info (my info) */ 1963 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1964 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1965 1966 /* pull from vtag hash */ 1967 LIST_REMOVE(stcb, sctp_asocs); 1968 /* re-insert to new vtag position */ 1969 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1970 SCTP_BASE_INFO(hashasocmark))]; 1971 /* 1972 * put it in the bucket in the vtag hash of assoc's for the 1973 * system 1974 */ 1975 LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1976 1977 SCTP_TCB_SEND_UNLOCK(stcb); 1978 SCTP_INP_WUNLOCK(stcb->sctp_ep); 1979 SCTP_INP_INFO_WUNLOCK(); 1980 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1981 SCTP_SOCKET_UNLOCK(so, 1); 1982 #endif 1983 asoc->total_flight = 0; 1984 asoc->total_flight_count = 0; 1985 /* process the INIT info (peer's info) */ 1986 retval = sctp_process_init(init_cp, stcb); 1987 if (retval < 0) { 1988 if (how_indx < sizeof(asoc->cookie_how)) 1989 asoc->cookie_how[how_indx] = 13; 1990 1991 return (NULL); 1992 } 1993 /* 1994 * since we did not send a HB make sure we don't double 1995 * things 1996 */ 1997 net->hb_responded = 1; 1998 1999 if (sctp_load_addresses_from_init(stcb, m, 2000 init_offset + sizeof(struct sctp_init_chunk), 2001 initack_offset, src, dst, init_src)) { 2002 if (how_indx < sizeof(asoc->cookie_how)) 2003 asoc->cookie_how[how_indx] = 14; 2004 2005 return (NULL); 2006 } 2007 /* respond with a COOKIE-ACK */ 2008 sctp_stop_all_cookie_timers(stcb); 2009 sctp_toss_old_cookies(stcb, asoc); 2010 sctp_send_cookie_ack(stcb); 2011 if (how_indx < sizeof(asoc->cookie_how)) 2012 asoc->cookie_how[how_indx] = 15; 2013 2014 return (stcb); 2015 } 2016 if (how_indx < sizeof(asoc->cookie_how)) 2017 asoc->cookie_how[how_indx] = 16; 2018 /* all other cases... */ 2019 return (NULL); 2020 } 2021 2022 2023 /* 2024 * handle a state cookie for a new association m: input packet mbuf chain-- 2025 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 2026 * and the cookie signature does not exist offset: offset into mbuf to the 2027 * cookie-echo chunk length: length of the cookie chunk to: where the init 2028 * was from returns a new TCB 2029 */ 2030 static struct sctp_tcb * 2031 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 2032 struct sockaddr *src, struct sockaddr *dst, 2033 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 2034 struct sctp_inpcb *inp, struct sctp_nets **netp, 2035 struct sockaddr *init_src, int *notification, 2036 int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 2037 uint8_t use_mflowid, uint32_t mflowid, 2038 uint32_t vrf_id, uint16_t port) 2039 { 2040 struct sctp_tcb *stcb; 2041 struct sctp_init_chunk *init_cp, init_buf; 2042 struct sctp_init_ack_chunk *initack_cp, initack_buf; 2043 struct sockaddr_storage sa_store; 2044 struct sockaddr *initack_src = (struct sockaddr *)&sa_store; 2045 struct sctp_association *asoc; 2046 int init_offset, initack_offset, initack_limit; 2047 int retval; 2048 int error = 0; 2049 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 2050 2051 #ifdef INET 2052 struct sockaddr_in *sin; 2053 2054 #endif 2055 #ifdef INET6 2056 struct sockaddr_in6 *sin6; 2057 2058 #endif 2059 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2060 struct socket *so; 2061 2062 so = SCTP_INP_SO(inp); 2063 #endif 2064 2065 /* 2066 * find and validate the INIT chunk in the cookie (peer's info) the 2067 * INIT should start after the cookie-echo header struct (chunk 2068 * header, state cookie header struct) 2069 */ 2070 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 2071 init_cp = (struct sctp_init_chunk *) 2072 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 2073 (uint8_t *) & init_buf); 2074 if (init_cp == NULL) { 2075 /* could not pull a INIT chunk in cookie */ 2076 SCTPDBG(SCTP_DEBUG_INPUT1, 2077 "process_cookie_new: could not pull INIT chunk hdr\n"); 2078 return (NULL); 2079 } 2080 if (init_cp->ch.chunk_type != SCTP_INITIATION) { 2081 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); 2082 return (NULL); 2083 } 2084 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length)); 2085 /* 2086 * find and validate the INIT-ACK chunk in the cookie (my info) the 2087 * INIT-ACK follows the INIT chunk 2088 */ 2089 initack_cp = (struct sctp_init_ack_chunk *) 2090 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 2091 (uint8_t *) & initack_buf); 2092 if (initack_cp == NULL) { 2093 /* could not pull INIT-ACK chunk in cookie */ 2094 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 2095 return (NULL); 2096 } 2097 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 2098 return (NULL); 2099 } 2100 /* 2101 * NOTE: We can't use the INIT_ACK's chk_length to determine the 2102 * "initack_limit" value. This is because the chk_length field 2103 * includes the length of the cookie, but the cookie is omitted when 2104 * the INIT and INIT_ACK are tacked onto the cookie... 2105 */ 2106 initack_limit = offset + cookie_len; 2107 2108 /* 2109 * now that we know the INIT/INIT-ACK are in place, create a new TCB 2110 * and popluate 2111 */ 2112 2113 /* 2114 * Here we do a trick, we set in NULL for the proc/thread argument. 2115 * We do this since in effect we only use the p argument when the 2116 * socket is unbound and we must do an implicit bind. Since we are 2117 * getting a cookie, we cannot be unbound. 2118 */ 2119 stcb = sctp_aloc_assoc(inp, init_src, &error, 2120 ntohl(initack_cp->init.initiate_tag), vrf_id, 2121 (struct thread *)NULL 2122 ); 2123 if (stcb == NULL) { 2124 struct mbuf *op_err; 2125 2126 /* memory problem? */ 2127 SCTPDBG(SCTP_DEBUG_INPUT1, 2128 "process_cookie_new: no room for another TCB!\n"); 2129 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2130 2131 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2132 src, dst, sh, op_err, 2133 use_mflowid, mflowid, 2134 vrf_id, port); 2135 return (NULL); 2136 } 2137 /* get the correct sctp_nets */ 2138 if (netp) 2139 *netp = sctp_findnet(stcb, init_src); 2140 2141 asoc = &stcb->asoc; 2142 /* get scope variables out of cookie */ 2143 asoc->scope.ipv4_local_scope = cookie->ipv4_scope; 2144 asoc->scope.site_scope = cookie->site_scope; 2145 asoc->scope.local_scope = cookie->local_scope; 2146 asoc->scope.loopback_scope = cookie->loopback_scope; 2147 2148 if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) || 2149 (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) { 2150 struct mbuf *op_err; 2151 2152 /* 2153 * Houston we have a problem. The EP changed while the 2154 * cookie was in flight. Only recourse is to abort the 2155 * association. 2156 */ 2157 atomic_add_int(&stcb->asoc.refcnt, 1); 2158 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2159 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2160 src, dst, sh, op_err, 2161 use_mflowid, mflowid, 2162 vrf_id, port); 2163 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2164 SCTP_TCB_UNLOCK(stcb); 2165 SCTP_SOCKET_LOCK(so, 1); 2166 SCTP_TCB_LOCK(stcb); 2167 #endif 2168 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 2169 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2170 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2171 SCTP_SOCKET_UNLOCK(so, 1); 2172 #endif 2173 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2174 return (NULL); 2175 } 2176 /* process the INIT-ACK info (my info) */ 2177 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 2178 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 2179 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 2180 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 2181 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 2182 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 2183 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 2184 asoc->str_reset_seq_in = asoc->init_seq_number; 2185 2186 asoc->advanced_peer_ack_point = asoc->last_acked_seq; 2187 2188 /* process the INIT info (peer's info) */ 2189 if (netp) 2190 retval = sctp_process_init(init_cp, stcb); 2191 else 2192 retval = 0; 2193 if (retval < 0) { 2194 atomic_add_int(&stcb->asoc.refcnt, 1); 2195 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2196 SCTP_TCB_UNLOCK(stcb); 2197 SCTP_SOCKET_LOCK(so, 1); 2198 SCTP_TCB_LOCK(stcb); 2199 #endif 2200 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2201 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2202 SCTP_SOCKET_UNLOCK(so, 1); 2203 #endif 2204 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2205 return (NULL); 2206 } 2207 /* load all addresses */ 2208 if (sctp_load_addresses_from_init(stcb, m, 2209 init_offset + sizeof(struct sctp_init_chunk), initack_offset, 2210 src, dst, init_src)) { 2211 atomic_add_int(&stcb->asoc.refcnt, 1); 2212 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2213 SCTP_TCB_UNLOCK(stcb); 2214 SCTP_SOCKET_LOCK(so, 1); 2215 SCTP_TCB_LOCK(stcb); 2216 #endif 2217 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 2218 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2219 SCTP_SOCKET_UNLOCK(so, 1); 2220 #endif 2221 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2222 return (NULL); 2223 } 2224 /* 2225 * verify any preceding AUTH chunk that was skipped 2226 */ 2227 /* pull the local authentication parameters from the cookie/init-ack */ 2228 sctp_auth_get_cookie_params(stcb, m, 2229 initack_offset + sizeof(struct sctp_init_ack_chunk), 2230 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 2231 if (auth_skipped) { 2232 struct sctp_auth_chunk *auth; 2233 2234 auth = (struct sctp_auth_chunk *) 2235 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 2236 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { 2237 /* auth HMAC failed, dump the assoc and packet */ 2238 SCTPDBG(SCTP_DEBUG_AUTH1, 2239 "COOKIE-ECHO: AUTH failed\n"); 2240 atomic_add_int(&stcb->asoc.refcnt, 1); 2241 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2242 SCTP_TCB_UNLOCK(stcb); 2243 SCTP_SOCKET_LOCK(so, 1); 2244 SCTP_TCB_LOCK(stcb); 2245 #endif 2246 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 2247 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2248 SCTP_SOCKET_UNLOCK(so, 1); 2249 #endif 2250 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2251 return (NULL); 2252 } else { 2253 /* remaining chunks checked... good to go */ 2254 stcb->asoc.authenticated = 1; 2255 } 2256 } 2257 /* update current state */ 2258 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2259 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2260 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2261 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2262 stcb->sctp_ep, stcb, asoc->primary_destination); 2263 } 2264 sctp_stop_all_cookie_timers(stcb); 2265 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 2266 SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2267 2268 /* 2269 * if we're doing ASCONFs, check to see if we have any new local 2270 * addresses that need to get added to the peer (eg. addresses 2271 * changed while cookie echo in flight). This needs to be done 2272 * after we go to the OPEN state to do the correct asconf 2273 * processing. else, make sure we have the correct addresses in our 2274 * lists 2275 */ 2276 2277 /* warning, we re-use sin, sin6, sa_store here! */ 2278 /* pull in local_address (our "from" address) */ 2279 switch (cookie->laddr_type) { 2280 #ifdef INET 2281 case SCTP_IPV4_ADDRESS: 2282 /* source addr is IPv4 */ 2283 sin = (struct sockaddr_in *)initack_src; 2284 memset(sin, 0, sizeof(*sin)); 2285 sin->sin_family = AF_INET; 2286 sin->sin_len = sizeof(struct sockaddr_in); 2287 sin->sin_addr.s_addr = cookie->laddress[0]; 2288 break; 2289 #endif 2290 #ifdef INET6 2291 case SCTP_IPV6_ADDRESS: 2292 /* source addr is IPv6 */ 2293 sin6 = (struct sockaddr_in6 *)initack_src; 2294 memset(sin6, 0, sizeof(*sin6)); 2295 sin6->sin6_family = AF_INET6; 2296 sin6->sin6_len = sizeof(struct sockaddr_in6); 2297 sin6->sin6_scope_id = cookie->scope_id; 2298 memcpy(&sin6->sin6_addr, cookie->laddress, 2299 sizeof(sin6->sin6_addr)); 2300 break; 2301 #endif 2302 default: 2303 atomic_add_int(&stcb->asoc.refcnt, 1); 2304 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2305 SCTP_TCB_UNLOCK(stcb); 2306 SCTP_SOCKET_LOCK(so, 1); 2307 SCTP_TCB_LOCK(stcb); 2308 #endif 2309 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 2310 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2311 SCTP_SOCKET_UNLOCK(so, 1); 2312 #endif 2313 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2314 return (NULL); 2315 } 2316 2317 /* set up to notify upper layer */ 2318 *notification = SCTP_NOTIFY_ASSOC_UP; 2319 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2320 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 2321 (inp->sctp_socket->so_qlimit == 0)) { 2322 /* 2323 * This is an endpoint that called connect() how it got a 2324 * cookie that is NEW is a bit of a mystery. It must be that 2325 * the INIT was sent, but before it got there.. a complete 2326 * INIT/INIT-ACK/COOKIE arrived. But of course then it 2327 * should have went to the other code.. not here.. oh well.. 2328 * a bit of protection is worth having.. 2329 */ 2330 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2331 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2332 atomic_add_int(&stcb->asoc.refcnt, 1); 2333 SCTP_TCB_UNLOCK(stcb); 2334 SCTP_SOCKET_LOCK(so, 1); 2335 SCTP_TCB_LOCK(stcb); 2336 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2337 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2338 SCTP_SOCKET_UNLOCK(so, 1); 2339 return (NULL); 2340 } 2341 #endif 2342 soisconnected(stcb->sctp_socket); 2343 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2344 SCTP_SOCKET_UNLOCK(so, 1); 2345 #endif 2346 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 2347 (inp->sctp_socket->so_qlimit)) { 2348 /* 2349 * We don't want to do anything with this one. Since it is 2350 * the listening guy. The timer will get started for 2351 * accepted connections in the caller. 2352 */ 2353 ; 2354 } 2355 /* since we did not send a HB make sure we don't double things */ 2356 if ((netp) && (*netp)) 2357 (*netp)->hb_responded = 1; 2358 2359 if (stcb->asoc.sctp_autoclose_ticks && 2360 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2361 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 2362 } 2363 /* calculate the RTT */ 2364 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 2365 if ((netp) && (*netp)) { 2366 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 2367 &cookie->time_entered, sctp_align_unsafe_makecopy, 2368 SCTP_RTT_FROM_NON_DATA); 2369 } 2370 /* respond with a COOKIE-ACK */ 2371 sctp_send_cookie_ack(stcb); 2372 2373 /* 2374 * check the address lists for any ASCONFs that need to be sent 2375 * AFTER the cookie-ack is sent 2376 */ 2377 sctp_check_address_list(stcb, m, 2378 initack_offset + sizeof(struct sctp_init_ack_chunk), 2379 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 2380 initack_src, cookie->local_scope, cookie->site_scope, 2381 cookie->ipv4_scope, cookie->loopback_scope); 2382 2383 2384 return (stcb); 2385 } 2386 2387 /* 2388 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e 2389 * we NEED to make sure we are not already using the vtag. If so we 2390 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit! 2391 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, 2392 SCTP_BASE_INFO(hashasocmark))]; 2393 LIST_FOREACH(stcb, head, sctp_asocs) { 2394 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) { 2395 -- SEND ABORT - TRY AGAIN -- 2396 } 2397 } 2398 */ 2399 2400 /* 2401 * handles a COOKIE-ECHO message stcb: modified to either a new or left as 2402 * existing (non-NULL) TCB 2403 */ 2404 static struct mbuf * 2405 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 2406 struct sockaddr *src, struct sockaddr *dst, 2407 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 2408 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 2409 int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 2410 struct sctp_tcb **locked_tcb, 2411 uint8_t use_mflowid, uint32_t mflowid, 2412 uint32_t vrf_id, uint16_t port) 2413 { 2414 struct sctp_state_cookie *cookie; 2415 struct sctp_tcb *l_stcb = *stcb; 2416 struct sctp_inpcb *l_inp; 2417 struct sockaddr *to; 2418 struct sctp_pcb *ep; 2419 struct mbuf *m_sig; 2420 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 2421 uint8_t *sig; 2422 uint8_t cookie_ok = 0; 2423 unsigned int sig_offset, cookie_offset; 2424 unsigned int cookie_len; 2425 struct timeval now; 2426 struct timeval time_expires; 2427 int notification = 0; 2428 struct sctp_nets *netl; 2429 int had_a_existing_tcb = 0; 2430 int send_int_conf = 0; 2431 2432 #ifdef INET 2433 struct sockaddr_in sin; 2434 2435 #endif 2436 #ifdef INET6 2437 struct sockaddr_in6 sin6; 2438 2439 #endif 2440 2441 SCTPDBG(SCTP_DEBUG_INPUT2, 2442 "sctp_handle_cookie: handling COOKIE-ECHO\n"); 2443 2444 if (inp_p == NULL) { 2445 return (NULL); 2446 } 2447 cookie = &cp->cookie; 2448 cookie_offset = offset + sizeof(struct sctp_chunkhdr); 2449 cookie_len = ntohs(cp->ch.chunk_length); 2450 2451 if ((cookie->peerport != sh->src_port) && 2452 (cookie->myport != sh->dest_port) && 2453 (cookie->my_vtag != sh->v_tag)) { 2454 /* 2455 * invalid ports or bad tag. Note that we always leave the 2456 * v_tag in the header in network order and when we stored 2457 * it in the my_vtag slot we also left it in network order. 2458 * This maintains the match even though it may be in the 2459 * opposite byte order of the machine :-> 2460 */ 2461 return (NULL); 2462 } 2463 if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 2464 sizeof(struct sctp_init_chunk) + 2465 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 2466 /* cookie too small */ 2467 return (NULL); 2468 } 2469 /* 2470 * split off the signature into its own mbuf (since it should not be 2471 * calculated in the sctp_hmac_m() call). 2472 */ 2473 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 2474 m_sig = m_split(m, sig_offset, M_NOWAIT); 2475 if (m_sig == NULL) { 2476 /* out of memory or ?? */ 2477 return (NULL); 2478 } 2479 #ifdef SCTP_MBUF_LOGGING 2480 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 2481 struct mbuf *mat; 2482 2483 for (mat = m_sig; mat; mat = SCTP_BUF_NEXT(mat)) { 2484 if (SCTP_BUF_IS_EXTENDED(mat)) { 2485 sctp_log_mb(mat, SCTP_MBUF_SPLIT); 2486 } 2487 } 2488 } 2489 #endif 2490 2491 /* 2492 * compute the signature/digest for the cookie 2493 */ 2494 ep = &(*inp_p)->sctp_ep; 2495 l_inp = *inp_p; 2496 if (l_stcb) { 2497 SCTP_TCB_UNLOCK(l_stcb); 2498 } 2499 SCTP_INP_RLOCK(l_inp); 2500 if (l_stcb) { 2501 SCTP_TCB_LOCK(l_stcb); 2502 } 2503 /* which cookie is it? */ 2504 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 2505 (ep->current_secret_number != ep->last_secret_number)) { 2506 /* it's the old cookie */ 2507 (void)sctp_hmac_m(SCTP_HMAC, 2508 (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2509 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2510 } else { 2511 /* it's the current cookie */ 2512 (void)sctp_hmac_m(SCTP_HMAC, 2513 (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 2514 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2515 } 2516 /* get the signature */ 2517 SCTP_INP_RUNLOCK(l_inp); 2518 sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 2519 if (sig == NULL) { 2520 /* couldn't find signature */ 2521 sctp_m_freem(m_sig); 2522 return (NULL); 2523 } 2524 /* compare the received digest with the computed digest */ 2525 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 2526 /* try the old cookie? */ 2527 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 2528 (ep->current_secret_number != ep->last_secret_number)) { 2529 /* compute digest with old */ 2530 (void)sctp_hmac_m(SCTP_HMAC, 2531 (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2532 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2533 /* compare */ 2534 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 2535 cookie_ok = 1; 2536 } 2537 } else { 2538 cookie_ok = 1; 2539 } 2540 2541 /* 2542 * Now before we continue we must reconstruct our mbuf so that 2543 * normal processing of any other chunks will work. 2544 */ 2545 { 2546 struct mbuf *m_at; 2547 2548 m_at = m; 2549 while (SCTP_BUF_NEXT(m_at) != NULL) { 2550 m_at = SCTP_BUF_NEXT(m_at); 2551 } 2552 SCTP_BUF_NEXT(m_at) = m_sig; 2553 } 2554 2555 if (cookie_ok == 0) { 2556 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); 2557 SCTPDBG(SCTP_DEBUG_INPUT2, 2558 "offset = %u, cookie_offset = %u, sig_offset = %u\n", 2559 (uint32_t) offset, cookie_offset, sig_offset); 2560 return (NULL); 2561 } 2562 /* 2563 * check the cookie timestamps to be sure it's not stale 2564 */ 2565 (void)SCTP_GETTIME_TIMEVAL(&now); 2566 /* Expire time is in Ticks, so we convert to seconds */ 2567 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life); 2568 time_expires.tv_usec = cookie->time_entered.tv_usec; 2569 /* 2570 * TODO sctp_constants.h needs alternative time macros when _KERNEL 2571 * is undefined. 2572 */ 2573 if (timevalcmp(&now, &time_expires, >)) { 2574 /* cookie is stale! */ 2575 struct mbuf *op_err; 2576 struct sctp_stale_cookie_msg *scm; 2577 uint32_t tim; 2578 2579 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 2580 0, M_NOWAIT, 1, MT_DATA); 2581 if (op_err == NULL) { 2582 /* FOOBAR */ 2583 return (NULL); 2584 } 2585 /* Set the len */ 2586 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 2587 scm = mtod(op_err, struct sctp_stale_cookie_msg *); 2588 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 2589 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 2590 (sizeof(uint32_t)))); 2591 /* seconds to usec */ 2592 tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 2593 /* add in usec */ 2594 if (tim == 0) 2595 tim = now.tv_usec - cookie->time_entered.tv_usec; 2596 scm->time_usec = htonl(tim); 2597 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err, 2598 use_mflowid, mflowid, 2599 vrf_id, port); 2600 return (NULL); 2601 } 2602 /* 2603 * Now we must see with the lookup address if we have an existing 2604 * asoc. This will only happen if we were in the COOKIE-WAIT state 2605 * and a INIT collided with us and somewhere the peer sent the 2606 * cookie on another address besides the single address our assoc 2607 * had for him. In this case we will have one of the tie-tags set at 2608 * least AND the address field in the cookie can be used to look it 2609 * up. 2610 */ 2611 to = NULL; 2612 switch (cookie->addr_type) { 2613 #ifdef INET6 2614 case SCTP_IPV6_ADDRESS: 2615 memset(&sin6, 0, sizeof(sin6)); 2616 sin6.sin6_family = AF_INET6; 2617 sin6.sin6_len = sizeof(sin6); 2618 sin6.sin6_port = sh->src_port; 2619 sin6.sin6_scope_id = cookie->scope_id; 2620 memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 2621 sizeof(sin6.sin6_addr.s6_addr)); 2622 to = (struct sockaddr *)&sin6; 2623 break; 2624 #endif 2625 #ifdef INET 2626 case SCTP_IPV4_ADDRESS: 2627 memset(&sin, 0, sizeof(sin)); 2628 sin.sin_family = AF_INET; 2629 sin.sin_len = sizeof(sin); 2630 sin.sin_port = sh->src_port; 2631 sin.sin_addr.s_addr = cookie->address[0]; 2632 to = (struct sockaddr *)&sin; 2633 break; 2634 #endif 2635 default: 2636 /* This should not happen */ 2637 return (NULL); 2638 } 2639 if ((*stcb == NULL) && to) { 2640 /* Yep, lets check */ 2641 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL); 2642 if (*stcb == NULL) { 2643 /* 2644 * We should have only got back the same inp. If we 2645 * got back a different ep we have a problem. The 2646 * original findep got back l_inp and now 2647 */ 2648 if (l_inp != *inp_p) { 2649 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); 2650 } 2651 } else { 2652 if (*locked_tcb == NULL) { 2653 /* 2654 * In this case we found the assoc only 2655 * after we locked the create lock. This 2656 * means we are in a colliding case and we 2657 * must make sure that we unlock the tcb if 2658 * its one of the cases where we throw away 2659 * the incoming packets. 2660 */ 2661 *locked_tcb = *stcb; 2662 2663 /* 2664 * We must also increment the inp ref count 2665 * since the ref_count flags was set when we 2666 * did not find the TCB, now we found it 2667 * which reduces the refcount.. we must 2668 * raise it back out to balance it all :-) 2669 */ 2670 SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2671 if ((*stcb)->sctp_ep != l_inp) { 2672 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", 2673 (void *)(*stcb)->sctp_ep, (void *)l_inp); 2674 } 2675 } 2676 } 2677 } 2678 if (to == NULL) { 2679 return (NULL); 2680 } 2681 cookie_len -= SCTP_SIGNATURE_SIZE; 2682 if (*stcb == NULL) { 2683 /* this is the "normal" case... get a new TCB */ 2684 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh, 2685 cookie, cookie_len, *inp_p, 2686 netp, to, ¬ification, 2687 auth_skipped, auth_offset, auth_len, 2688 use_mflowid, mflowid, 2689 vrf_id, port); 2690 } else { 2691 /* this is abnormal... cookie-echo on existing TCB */ 2692 had_a_existing_tcb = 1; 2693 *stcb = sctp_process_cookie_existing(m, iphlen, offset, 2694 src, dst, sh, 2695 cookie, cookie_len, *inp_p, *stcb, netp, to, 2696 ¬ification, auth_skipped, auth_offset, auth_len, 2697 use_mflowid, mflowid, 2698 vrf_id, port); 2699 } 2700 2701 if (*stcb == NULL) { 2702 /* still no TCB... must be bad cookie-echo */ 2703 return (NULL); 2704 } 2705 if ((*netp != NULL) && (use_mflowid != 0)) { 2706 (*netp)->flowid = mflowid; 2707 #ifdef INVARIANTS 2708 (*netp)->flowidset = 1; 2709 #endif 2710 } 2711 /* 2712 * Ok, we built an association so confirm the address we sent the 2713 * INIT-ACK to. 2714 */ 2715 netl = sctp_findnet(*stcb, to); 2716 /* 2717 * This code should in theory NOT run but 2718 */ 2719 if (netl == NULL) { 2720 /* TSNH! Huh, why do I need to add this address here? */ 2721 if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) { 2722 return (NULL); 2723 } 2724 netl = sctp_findnet(*stcb, to); 2725 } 2726 if (netl) { 2727 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2728 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2729 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2730 netl); 2731 send_int_conf = 1; 2732 } 2733 } 2734 sctp_start_net_timers(*stcb); 2735 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2736 if (!had_a_existing_tcb || 2737 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2738 /* 2739 * If we have a NEW cookie or the connect never 2740 * reached the connected state during collision we 2741 * must do the TCP accept thing. 2742 */ 2743 struct socket *so, *oso; 2744 struct sctp_inpcb *inp; 2745 2746 if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2747 /* 2748 * For a restart we will keep the same 2749 * socket, no need to do anything. I THINK!! 2750 */ 2751 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2752 if (send_int_conf) { 2753 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2754 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2755 } 2756 return (m); 2757 } 2758 oso = (*inp_p)->sctp_socket; 2759 atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2760 SCTP_TCB_UNLOCK((*stcb)); 2761 CURVNET_SET(oso->so_vnet); 2762 so = sonewconn(oso, 0 2763 ); 2764 CURVNET_RESTORE(); 2765 SCTP_TCB_LOCK((*stcb)); 2766 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2767 2768 if (so == NULL) { 2769 struct mbuf *op_err; 2770 2771 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2772 struct socket *pcb_so; 2773 2774 #endif 2775 /* Too many sockets */ 2776 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2777 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2778 sctp_abort_association(*inp_p, NULL, m, iphlen, 2779 src, dst, sh, op_err, 2780 use_mflowid, mflowid, 2781 vrf_id, port); 2782 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2783 pcb_so = SCTP_INP_SO(*inp_p); 2784 atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2785 SCTP_TCB_UNLOCK((*stcb)); 2786 SCTP_SOCKET_LOCK(pcb_so, 1); 2787 SCTP_TCB_LOCK((*stcb)); 2788 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2789 #endif 2790 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2791 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2792 SCTP_SOCKET_UNLOCK(pcb_so, 1); 2793 #endif 2794 return (NULL); 2795 } 2796 inp = (struct sctp_inpcb *)so->so_pcb; 2797 SCTP_INP_INCR_REF(inp); 2798 /* 2799 * We add the unbound flag here so that if we get an 2800 * soabort() before we get the move_pcb done, we 2801 * will properly cleanup. 2802 */ 2803 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2804 SCTP_PCB_FLAGS_CONNECTED | 2805 SCTP_PCB_FLAGS_IN_TCPPOOL | 2806 SCTP_PCB_FLAGS_UNBOUND | 2807 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2808 SCTP_PCB_FLAGS_DONT_WAKE); 2809 inp->sctp_features = (*inp_p)->sctp_features; 2810 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features; 2811 inp->sctp_socket = so; 2812 inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2813 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off; 2814 inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable; 2815 inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2816 inp->sctp_context = (*inp_p)->sctp_context; 2817 inp->local_strreset_support = (*inp_p)->local_strreset_support; 2818 inp->inp_starting_point_for_iterator = NULL; 2819 /* 2820 * copy in the authentication parameters from the 2821 * original endpoint 2822 */ 2823 if (inp->sctp_ep.local_hmacs) 2824 sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2825 inp->sctp_ep.local_hmacs = 2826 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2827 if (inp->sctp_ep.local_auth_chunks) 2828 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2829 inp->sctp_ep.local_auth_chunks = 2830 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2831 2832 /* 2833 * Now we must move it from one hash table to 2834 * another and get the tcb in the right place. 2835 */ 2836 2837 /* 2838 * This is where the one-2-one socket is put into 2839 * the accept state waiting for the accept! 2840 */ 2841 if (*stcb) { 2842 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE; 2843 } 2844 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2845 2846 atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2847 SCTP_TCB_UNLOCK((*stcb)); 2848 2849 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, 2850 0); 2851 SCTP_TCB_LOCK((*stcb)); 2852 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2853 2854 2855 /* 2856 * now we must check to see if we were aborted while 2857 * the move was going on and the lock/unlock 2858 * happened. 2859 */ 2860 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2861 /* 2862 * yep it was, we leave the assoc attached 2863 * to the socket since the sctp_inpcb_free() 2864 * call will send an abort for us. 2865 */ 2866 SCTP_INP_DECR_REF(inp); 2867 return (NULL); 2868 } 2869 SCTP_INP_DECR_REF(inp); 2870 /* Switch over to the new guy */ 2871 *inp_p = inp; 2872 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2873 if (send_int_conf) { 2874 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2875 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2876 } 2877 /* 2878 * Pull it from the incomplete queue and wake the 2879 * guy 2880 */ 2881 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2882 atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2883 SCTP_TCB_UNLOCK((*stcb)); 2884 SCTP_SOCKET_LOCK(so, 1); 2885 #endif 2886 soisconnected(so); 2887 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2888 SCTP_TCB_LOCK((*stcb)); 2889 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2890 SCTP_SOCKET_UNLOCK(so, 1); 2891 #endif 2892 return (m); 2893 } 2894 } 2895 if (notification) { 2896 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2897 } 2898 if (send_int_conf) { 2899 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2900 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2901 } 2902 return (m); 2903 } 2904 2905 static void 2906 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED, 2907 struct sctp_tcb *stcb, struct sctp_nets *net) 2908 { 2909 /* cp must not be used, others call this without a c-ack :-) */ 2910 struct sctp_association *asoc; 2911 2912 SCTPDBG(SCTP_DEBUG_INPUT2, 2913 "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2914 if (stcb == NULL) 2915 return; 2916 2917 asoc = &stcb->asoc; 2918 2919 sctp_stop_all_cookie_timers(stcb); 2920 /* process according to association state */ 2921 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2922 /* state change only needed when I am in right state */ 2923 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2924 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2925 sctp_start_net_timers(stcb); 2926 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2927 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2928 stcb->sctp_ep, stcb, asoc->primary_destination); 2929 2930 } 2931 /* update RTO */ 2932 SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2933 SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2934 if (asoc->overall_error_count == 0) { 2935 net->RTO = sctp_calculate_rto(stcb, asoc, net, 2936 &asoc->time_entered, sctp_align_safe_nocopy, 2937 SCTP_RTT_FROM_NON_DATA); 2938 } 2939 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2940 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2941 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2942 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2943 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2944 struct socket *so; 2945 2946 #endif 2947 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2948 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2949 so = SCTP_INP_SO(stcb->sctp_ep); 2950 atomic_add_int(&stcb->asoc.refcnt, 1); 2951 SCTP_TCB_UNLOCK(stcb); 2952 SCTP_SOCKET_LOCK(so, 1); 2953 SCTP_TCB_LOCK(stcb); 2954 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2955 #endif 2956 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) { 2957 soisconnected(stcb->sctp_socket); 2958 } 2959 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2960 SCTP_SOCKET_UNLOCK(so, 1); 2961 #endif 2962 } 2963 /* 2964 * since we did not send a HB make sure we don't double 2965 * things 2966 */ 2967 net->hb_responded = 1; 2968 2969 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2970 /* 2971 * We don't need to do the asconf thing, nor hb or 2972 * autoclose if the socket is closed. 2973 */ 2974 goto closed_socket; 2975 } 2976 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2977 stcb, net); 2978 2979 2980 if (stcb->asoc.sctp_autoclose_ticks && 2981 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2982 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2983 stcb->sctp_ep, stcb, NULL); 2984 } 2985 /* 2986 * send ASCONF if parameters are pending and ASCONFs are 2987 * allowed (eg. addresses changed when init/cookie echo were 2988 * in flight) 2989 */ 2990 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2991 (stcb->asoc.peer_supports_asconf) && 2992 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2993 #ifdef SCTP_TIMER_BASED_ASCONF 2994 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2995 stcb->sctp_ep, stcb, 2996 stcb->asoc.primary_destination); 2997 #else 2998 sctp_send_asconf(stcb, stcb->asoc.primary_destination, 2999 SCTP_ADDR_NOT_LOCKED); 3000 #endif 3001 } 3002 } 3003 closed_socket: 3004 /* Toss the cookie if I can */ 3005 sctp_toss_old_cookies(stcb, asoc); 3006 if (!TAILQ_EMPTY(&asoc->sent_queue)) { 3007 /* Restart the timer if we have pending data */ 3008 struct sctp_tmit_chunk *chk; 3009 3010 chk = TAILQ_FIRST(&asoc->sent_queue); 3011 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 3012 } 3013 } 3014 3015 static void 3016 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 3017 struct sctp_tcb *stcb) 3018 { 3019 struct sctp_nets *net; 3020 struct sctp_tmit_chunk *lchk; 3021 struct sctp_ecne_chunk bkup; 3022 uint8_t override_bit; 3023 uint32_t tsn, window_data_tsn; 3024 int len; 3025 unsigned int pkt_cnt; 3026 3027 len = ntohs(cp->ch.chunk_length); 3028 if ((len != sizeof(struct sctp_ecne_chunk)) && 3029 (len != sizeof(struct old_sctp_ecne_chunk))) { 3030 return; 3031 } 3032 if (len == sizeof(struct old_sctp_ecne_chunk)) { 3033 /* Its the old format */ 3034 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk)); 3035 bkup.num_pkts_since_cwr = htonl(1); 3036 cp = &bkup; 3037 } 3038 SCTP_STAT_INCR(sctps_recvecne); 3039 tsn = ntohl(cp->tsn); 3040 pkt_cnt = ntohl(cp->num_pkts_since_cwr); 3041 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead); 3042 if (lchk == NULL) { 3043 window_data_tsn = stcb->asoc.sending_seq - 1; 3044 } else { 3045 window_data_tsn = lchk->rec.data.TSN_seq; 3046 } 3047 3048 /* Find where it was sent to if possible. */ 3049 net = NULL; 3050 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) { 3051 if (lchk->rec.data.TSN_seq == tsn) { 3052 net = lchk->whoTo; 3053 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send; 3054 break; 3055 } 3056 if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) { 3057 break; 3058 } 3059 } 3060 if (net == NULL) { 3061 /* 3062 * What to do. A previous send of a CWR was possibly lost. 3063 * See how old it is, we may have it marked on the actual 3064 * net. 3065 */ 3066 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 3067 if (tsn == net->last_cwr_tsn) { 3068 /* Found him, send it off */ 3069 break; 3070 } 3071 } 3072 if (net == NULL) { 3073 /* 3074 * If we reach here, we need to send a special CWR 3075 * that says hey, we did this a long time ago and 3076 * you lost the response. 3077 */ 3078 net = TAILQ_FIRST(&stcb->asoc.nets); 3079 if (net == NULL) { 3080 /* TSNH */ 3081 return; 3082 } 3083 override_bit = SCTP_CWR_REDUCE_OVERRIDE; 3084 } else { 3085 override_bit = 0; 3086 } 3087 } else { 3088 override_bit = 0; 3089 } 3090 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) && 3091 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) { 3092 /* 3093 * JRS - Use the congestion control given in the pluggable 3094 * CC module 3095 */ 3096 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt); 3097 /* 3098 * We reduce once every RTT. So we will only lower cwnd at 3099 * the next sending seq i.e. the window_data_tsn 3100 */ 3101 net->cwr_window_tsn = window_data_tsn; 3102 net->ecn_ce_pkt_cnt += pkt_cnt; 3103 net->lost_cnt = pkt_cnt; 3104 net->last_cwr_tsn = tsn; 3105 } else { 3106 override_bit |= SCTP_CWR_IN_SAME_WINDOW; 3107 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) && 3108 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) { 3109 /* 3110 * Another loss in the same window update how many 3111 * marks/packets lost we have had. 3112 */ 3113 int cnt = 1; 3114 3115 if (pkt_cnt > net->lost_cnt) { 3116 /* Should be the case */ 3117 cnt = (pkt_cnt - net->lost_cnt); 3118 net->ecn_ce_pkt_cnt += cnt; 3119 } 3120 net->lost_cnt = pkt_cnt; 3121 net->last_cwr_tsn = tsn; 3122 /* 3123 * Most CC functions will ignore this call, since we 3124 * are in-window yet of the initial CE the peer saw. 3125 */ 3126 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt); 3127 } 3128 } 3129 /* 3130 * We always send a CWR this way if our previous one was lost our 3131 * peer will get an update, or if it is not time again to reduce we 3132 * still get the cwr to the peer. Note we set the override when we 3133 * could not find the TSN on the chunk or the destination network. 3134 */ 3135 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit); 3136 } 3137 3138 static void 3139 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net) 3140 { 3141 /* 3142 * Here we get a CWR from the peer. We must look in the outqueue and 3143 * make sure that we have a covered ECNE in the control chunk part. 3144 * If so remove it. 3145 */ 3146 struct sctp_tmit_chunk *chk; 3147 struct sctp_ecne_chunk *ecne; 3148 int override; 3149 uint32_t cwr_tsn; 3150 3151 cwr_tsn = ntohl(cp->tsn); 3152 3153 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE; 3154 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 3155 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 3156 continue; 3157 } 3158 if ((override == 0) && (chk->whoTo != net)) { 3159 /* Must be from the right src unless override is set */ 3160 continue; 3161 } 3162 ecne = mtod(chk->data, struct sctp_ecne_chunk *); 3163 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) { 3164 /* this covers this ECNE, we can remove it */ 3165 stcb->asoc.ecn_echo_cnt_onq--; 3166 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 3167 sctp_next); 3168 if (chk->data) { 3169 sctp_m_freem(chk->data); 3170 chk->data = NULL; 3171 } 3172 stcb->asoc.ctrl_queue_cnt--; 3173 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 3174 if (override == 0) { 3175 break; 3176 } 3177 } 3178 } 3179 } 3180 3181 static void 3182 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED, 3183 struct sctp_tcb *stcb, struct sctp_nets *net) 3184 { 3185 struct sctp_association *asoc; 3186 3187 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3188 struct socket *so; 3189 3190 #endif 3191 3192 SCTPDBG(SCTP_DEBUG_INPUT2, 3193 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 3194 if (stcb == NULL) 3195 return; 3196 3197 asoc = &stcb->asoc; 3198 /* process according to association state */ 3199 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 3200 /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 3201 SCTPDBG(SCTP_DEBUG_INPUT2, 3202 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n"); 3203 SCTP_TCB_UNLOCK(stcb); 3204 return; 3205 } 3206 /* notify upper layer protocol */ 3207 if (stcb->sctp_socket) { 3208 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 3209 } 3210 #ifdef INVARIANTS 3211 if (!TAILQ_EMPTY(&asoc->send_queue) || 3212 !TAILQ_EMPTY(&asoc->sent_queue) || 3213 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 3214 panic("Queues are not empty when handling SHUTDOWN-COMPLETE"); 3215 } 3216 #endif 3217 /* stop the timer */ 3218 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 3219 SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 3220 /* free the TCB */ 3221 SCTPDBG(SCTP_DEBUG_INPUT2, 3222 "sctp_handle_shutdown_complete: calls free-asoc\n"); 3223 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3224 so = SCTP_INP_SO(stcb->sctp_ep); 3225 atomic_add_int(&stcb->asoc.refcnt, 1); 3226 SCTP_TCB_UNLOCK(stcb); 3227 SCTP_SOCKET_LOCK(so, 1); 3228 SCTP_TCB_LOCK(stcb); 3229 atomic_subtract_int(&stcb->asoc.refcnt, 1); 3230 #endif 3231 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 3232 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3233 SCTP_SOCKET_UNLOCK(so, 1); 3234 #endif 3235 return; 3236 } 3237 3238 static int 3239 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 3240 struct sctp_nets *net, uint8_t flg) 3241 { 3242 switch (desc->chunk_type) { 3243 case SCTP_DATA: 3244 /* find the tsn to resend (possibly */ 3245 { 3246 uint32_t tsn; 3247 struct sctp_tmit_chunk *tp1; 3248 3249 tsn = ntohl(desc->tsn_ifany); 3250 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3251 if (tp1->rec.data.TSN_seq == tsn) { 3252 /* found it */ 3253 break; 3254 } 3255 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) { 3256 /* not found */ 3257 tp1 = NULL; 3258 break; 3259 } 3260 } 3261 if (tp1 == NULL) { 3262 /* 3263 * Do it the other way , aka without paying 3264 * attention to queue seq order. 3265 */ 3266 SCTP_STAT_INCR(sctps_pdrpdnfnd); 3267 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3268 if (tp1->rec.data.TSN_seq == tsn) { 3269 /* found it */ 3270 break; 3271 } 3272 } 3273 } 3274 if (tp1 == NULL) { 3275 SCTP_STAT_INCR(sctps_pdrptsnnf); 3276 } 3277 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 3278 uint8_t *ddp; 3279 3280 if (((flg & SCTP_BADCRC) == 0) && 3281 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3282 return (0); 3283 } 3284 if ((stcb->asoc.peers_rwnd == 0) && 3285 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3286 SCTP_STAT_INCR(sctps_pdrpdiwnp); 3287 return (0); 3288 } 3289 if (stcb->asoc.peers_rwnd == 0 && 3290 (flg & SCTP_FROM_MIDDLE_BOX)) { 3291 SCTP_STAT_INCR(sctps_pdrpdizrw); 3292 return (0); 3293 } 3294 ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 3295 sizeof(struct sctp_data_chunk)); 3296 { 3297 unsigned int iii; 3298 3299 for (iii = 0; iii < sizeof(desc->data_bytes); 3300 iii++) { 3301 if (ddp[iii] != desc->data_bytes[iii]) { 3302 SCTP_STAT_INCR(sctps_pdrpbadd); 3303 return (-1); 3304 } 3305 } 3306 } 3307 3308 if (tp1->do_rtt) { 3309 /* 3310 * this guy had a RTO calculation 3311 * pending on it, cancel it 3312 */ 3313 if (tp1->whoTo->rto_needed == 0) { 3314 tp1->whoTo->rto_needed = 1; 3315 } 3316 tp1->do_rtt = 0; 3317 } 3318 SCTP_STAT_INCR(sctps_pdrpmark); 3319 if (tp1->sent != SCTP_DATAGRAM_RESEND) 3320 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3321 /* 3322 * mark it as if we were doing a FR, since 3323 * we will be getting gap ack reports behind 3324 * the info from the router. 3325 */ 3326 tp1->rec.data.doing_fast_retransmit = 1; 3327 /* 3328 * mark the tsn with what sequences can 3329 * cause a new FR. 3330 */ 3331 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 3332 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 3333 } else { 3334 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 3335 } 3336 3337 /* restart the timer */ 3338 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3339 stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 3340 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3341 stcb, tp1->whoTo); 3342 3343 /* fix counts and things */ 3344 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 3345 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 3346 tp1->whoTo->flight_size, 3347 tp1->book_size, 3348 (uintptr_t) stcb, 3349 tp1->rec.data.TSN_seq); 3350 } 3351 if (tp1->sent < SCTP_DATAGRAM_RESEND) { 3352 sctp_flight_size_decrease(tp1); 3353 sctp_total_flight_decrease(stcb, tp1); 3354 } 3355 tp1->sent = SCTP_DATAGRAM_RESEND; 3356 } { 3357 /* audit code */ 3358 unsigned int audit; 3359 3360 audit = 0; 3361 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3362 if (tp1->sent == SCTP_DATAGRAM_RESEND) 3363 audit++; 3364 } 3365 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 3366 sctp_next) { 3367 if (tp1->sent == SCTP_DATAGRAM_RESEND) 3368 audit++; 3369 } 3370 if (audit != stcb->asoc.sent_queue_retran_cnt) { 3371 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 3372 audit, stcb->asoc.sent_queue_retran_cnt); 3373 #ifndef SCTP_AUDITING_ENABLED 3374 stcb->asoc.sent_queue_retran_cnt = audit; 3375 #endif 3376 } 3377 } 3378 } 3379 break; 3380 case SCTP_ASCONF: 3381 { 3382 struct sctp_tmit_chunk *asconf; 3383 3384 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 3385 sctp_next) { 3386 if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 3387 break; 3388 } 3389 } 3390 if (asconf) { 3391 if (asconf->sent != SCTP_DATAGRAM_RESEND) 3392 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3393 asconf->sent = SCTP_DATAGRAM_RESEND; 3394 asconf->snd_count--; 3395 } 3396 } 3397 break; 3398 case SCTP_INITIATION: 3399 /* resend the INIT */ 3400 stcb->asoc.dropped_special_cnt++; 3401 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 3402 /* 3403 * If we can get it in, in a few attempts we do 3404 * this, otherwise we let the timer fire. 3405 */ 3406 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 3407 stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 3408 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 3409 } 3410 break; 3411 case SCTP_SELECTIVE_ACK: 3412 case SCTP_NR_SELECTIVE_ACK: 3413 /* resend the sack */ 3414 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED); 3415 break; 3416 case SCTP_HEARTBEAT_REQUEST: 3417 /* resend a demand HB */ 3418 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) { 3419 /* 3420 * Only retransmit if we KNOW we wont destroy the 3421 * tcb 3422 */ 3423 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED); 3424 } 3425 break; 3426 case SCTP_SHUTDOWN: 3427 sctp_send_shutdown(stcb, net); 3428 break; 3429 case SCTP_SHUTDOWN_ACK: 3430 sctp_send_shutdown_ack(stcb, net); 3431 break; 3432 case SCTP_COOKIE_ECHO: 3433 { 3434 struct sctp_tmit_chunk *cookie; 3435 3436 cookie = NULL; 3437 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 3438 sctp_next) { 3439 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 3440 break; 3441 } 3442 } 3443 if (cookie) { 3444 if (cookie->sent != SCTP_DATAGRAM_RESEND) 3445 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3446 cookie->sent = SCTP_DATAGRAM_RESEND; 3447 sctp_stop_all_cookie_timers(stcb); 3448 } 3449 } 3450 break; 3451 case SCTP_COOKIE_ACK: 3452 sctp_send_cookie_ack(stcb); 3453 break; 3454 case SCTP_ASCONF_ACK: 3455 /* resend last asconf ack */ 3456 sctp_send_asconf_ack(stcb); 3457 break; 3458 case SCTP_FORWARD_CUM_TSN: 3459 send_forward_tsn(stcb, &stcb->asoc); 3460 break; 3461 /* can't do anything with these */ 3462 case SCTP_PACKET_DROPPED: 3463 case SCTP_INITIATION_ACK: /* this should not happen */ 3464 case SCTP_HEARTBEAT_ACK: 3465 case SCTP_ABORT_ASSOCIATION: 3466 case SCTP_OPERATION_ERROR: 3467 case SCTP_SHUTDOWN_COMPLETE: 3468 case SCTP_ECN_ECHO: 3469 case SCTP_ECN_CWR: 3470 default: 3471 break; 3472 } 3473 return (0); 3474 } 3475 3476 void 3477 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t * list) 3478 { 3479 uint32_t i; 3480 uint16_t temp; 3481 3482 /* 3483 * We set things to 0xffff since this is the last delivered sequence 3484 * and we will be sending in 0 after the reset. 3485 */ 3486 3487 if (number_entries) { 3488 for (i = 0; i < number_entries; i++) { 3489 temp = ntohs(list[i]); 3490 if (temp >= stcb->asoc.streamincnt) { 3491 continue; 3492 } 3493 stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 3494 } 3495 } else { 3496 list = NULL; 3497 for (i = 0; i < stcb->asoc.streamincnt; i++) { 3498 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3499 } 3500 } 3501 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3502 } 3503 3504 static void 3505 sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t * list) 3506 { 3507 uint32_t i; 3508 uint16_t temp; 3509 3510 if (number_entries > 0) { 3511 for (i = 0; i < number_entries; i++) { 3512 temp = ntohs(list[i]); 3513 if (temp >= stcb->asoc.streamoutcnt) { 3514 /* no such stream */ 3515 continue; 3516 } 3517 stcb->asoc.strmout[temp].next_sequence_send = 0; 3518 } 3519 } else { 3520 for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 3521 stcb->asoc.strmout[i].next_sequence_send = 0; 3522 } 3523 } 3524 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3525 } 3526 3527 3528 struct sctp_stream_reset_out_request * 3529 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 3530 { 3531 struct sctp_association *asoc; 3532 struct sctp_chunkhdr *ch; 3533 struct sctp_stream_reset_out_request *r; 3534 struct sctp_tmit_chunk *chk; 3535 int len, clen; 3536 3537 asoc = &stcb->asoc; 3538 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 3539 asoc->stream_reset_outstanding = 0; 3540 return (NULL); 3541 } 3542 if (stcb->asoc.str_reset == NULL) { 3543 asoc->stream_reset_outstanding = 0; 3544 return (NULL); 3545 } 3546 chk = stcb->asoc.str_reset; 3547 if (chk->data == NULL) { 3548 return (NULL); 3549 } 3550 if (bchk) { 3551 /* he wants a copy of the chk pointer */ 3552 *bchk = chk; 3553 } 3554 clen = chk->send_size; 3555 ch = mtod(chk->data, struct sctp_chunkhdr *); 3556 r = (struct sctp_stream_reset_out_request *)(ch + 1); 3557 if (ntohl(r->request_seq) == seq) { 3558 /* found it */ 3559 return (r); 3560 } 3561 len = SCTP_SIZE32(ntohs(r->ph.param_length)); 3562 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 3563 /* move to the next one, there can only be a max of two */ 3564 r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 3565 if (ntohl(r->request_seq) == seq) { 3566 return (r); 3567 } 3568 } 3569 /* that seq is not here */ 3570 return (NULL); 3571 } 3572 3573 static void 3574 sctp_clean_up_stream_reset(struct sctp_tcb *stcb) 3575 { 3576 struct sctp_association *asoc; 3577 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 3578 3579 if (stcb->asoc.str_reset == NULL) { 3580 return; 3581 } 3582 asoc = &stcb->asoc; 3583 3584 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3585 TAILQ_REMOVE(&asoc->control_send_queue, 3586 chk, 3587 sctp_next); 3588 if (chk->data) { 3589 sctp_m_freem(chk->data); 3590 chk->data = NULL; 3591 } 3592 asoc->ctrl_queue_cnt--; 3593 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 3594 /* sa_ignore NO_NULL_CHK */ 3595 stcb->asoc.str_reset = NULL; 3596 } 3597 3598 3599 static int 3600 sctp_handle_stream_reset_response(struct sctp_tcb *stcb, 3601 uint32_t seq, uint32_t action, 3602 struct sctp_stream_reset_response *respin) 3603 { 3604 uint16_t type; 3605 int lparm_len; 3606 struct sctp_association *asoc = &stcb->asoc; 3607 struct sctp_tmit_chunk *chk; 3608 struct sctp_stream_reset_out_request *srparam; 3609 uint32_t number_entries; 3610 3611 if (asoc->stream_reset_outstanding == 0) { 3612 /* duplicate */ 3613 return (0); 3614 } 3615 if (seq == stcb->asoc.str_reset_seq_out) { 3616 srparam = sctp_find_stream_reset(stcb, seq, &chk); 3617 if (srparam) { 3618 stcb->asoc.str_reset_seq_out++; 3619 type = ntohs(srparam->ph.param_type); 3620 lparm_len = ntohs(srparam->ph.param_length); 3621 if (type == SCTP_STR_RESET_OUT_REQUEST) { 3622 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 3623 asoc->stream_reset_out_is_outstanding = 0; 3624 if (asoc->stream_reset_outstanding) 3625 asoc->stream_reset_outstanding--; 3626 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) { 3627 /* do it */ 3628 sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 3629 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3630 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3631 } else { 3632 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3633 } 3634 } else if (type == SCTP_STR_RESET_IN_REQUEST) { 3635 /* Answered my request */ 3636 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 3637 if (asoc->stream_reset_outstanding) 3638 asoc->stream_reset_outstanding--; 3639 if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3640 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb, 3641 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3642 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) { 3643 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, 3644 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3645 } 3646 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) { 3647 /* Ok we now may have more streams */ 3648 int num_stream; 3649 3650 num_stream = stcb->asoc.strm_pending_add_size; 3651 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) { 3652 /* TSNH */ 3653 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt; 3654 } 3655 stcb->asoc.strm_pending_add_size = 0; 3656 if (asoc->stream_reset_outstanding) 3657 asoc->stream_reset_outstanding--; 3658 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) { 3659 /* Put the new streams into effect */ 3660 stcb->asoc.streamoutcnt += num_stream; 3661 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0); 3662 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3663 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3664 SCTP_STREAM_CHANGE_DENIED); 3665 } else { 3666 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3667 SCTP_STREAM_CHANGE_FAILED); 3668 } 3669 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) { 3670 if (asoc->stream_reset_outstanding) 3671 asoc->stream_reset_outstanding--; 3672 if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3673 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3674 SCTP_STREAM_CHANGE_DENIED); 3675 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) { 3676 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3677 SCTP_STREAM_CHANGE_FAILED); 3678 } 3679 } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 3680 /** 3681 * a) Adopt the new in tsn. 3682 * b) reset the map 3683 * c) Adopt the new out-tsn 3684 */ 3685 struct sctp_stream_reset_response_tsn *resp; 3686 struct sctp_forward_tsn_chunk fwdtsn; 3687 int abort_flag = 0; 3688 3689 if (respin == NULL) { 3690 /* huh ? */ 3691 return (0); 3692 } 3693 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) { 3694 resp = (struct sctp_stream_reset_response_tsn *)respin; 3695 asoc->stream_reset_outstanding--; 3696 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3697 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3698 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 3699 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3700 if (abort_flag) { 3701 return (1); 3702 } 3703 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 3704 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3705 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3706 } 3707 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3708 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 3709 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3710 3711 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3712 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size); 3713 3714 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 3715 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 3716 3717 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3718 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3719 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0); 3720 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3721 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 3722 SCTP_ASSOC_RESET_DENIED); 3723 } else { 3724 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 3725 SCTP_ASSOC_RESET_FAILED); 3726 } 3727 } 3728 /* get rid of the request and get the request flags */ 3729 if (asoc->stream_reset_outstanding == 0) { 3730 sctp_clean_up_stream_reset(stcb); 3731 } 3732 } 3733 } 3734 return (0); 3735 } 3736 3737 static void 3738 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 3739 struct sctp_tmit_chunk *chk, 3740 struct sctp_stream_reset_in_request *req, int trunc) 3741 { 3742 uint32_t seq; 3743 int len, i; 3744 int number_entries; 3745 uint16_t temp; 3746 3747 /* 3748 * peer wants me to send a str-reset to him for my outgoing seq's if 3749 * seq_in is right. 3750 */ 3751 struct sctp_association *asoc = &stcb->asoc; 3752 3753 seq = ntohl(req->request_seq); 3754 if (asoc->str_reset_seq_in == seq) { 3755 asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3756 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) { 3757 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3758 } else if (trunc) { 3759 /* Can't do it, since they exceeded our buffer size */ 3760 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3761 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 3762 len = ntohs(req->ph.param_length); 3763 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 3764 for (i = 0; i < number_entries; i++) { 3765 temp = ntohs(req->list_of_streams[i]); 3766 req->list_of_streams[i] = temp; 3767 } 3768 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3769 sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 3770 asoc->str_reset_seq_out, 3771 seq, (asoc->sending_seq - 1)); 3772 asoc->stream_reset_out_is_outstanding = 1; 3773 asoc->str_reset = chk; 3774 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 3775 stcb->asoc.stream_reset_outstanding++; 3776 } else { 3777 /* Can't do it, since we have sent one out */ 3778 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS; 3779 } 3780 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3781 asoc->str_reset_seq_in++; 3782 } else if (asoc->str_reset_seq_in - 1 == seq) { 3783 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3784 } else if (asoc->str_reset_seq_in - 2 == seq) { 3785 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3786 } else { 3787 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 3788 } 3789 } 3790 3791 static int 3792 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 3793 struct sctp_tmit_chunk *chk, 3794 struct sctp_stream_reset_tsn_request *req) 3795 { 3796 /* reset all in and out and update the tsn */ 3797 /* 3798 * A) reset my str-seq's on in and out. B) Select a receive next, 3799 * and set cum-ack to it. Also process this selected number as a 3800 * fwd-tsn as well. C) set in the response my next sending seq. 3801 */ 3802 struct sctp_forward_tsn_chunk fwdtsn; 3803 struct sctp_association *asoc = &stcb->asoc; 3804 int abort_flag = 0; 3805 uint32_t seq; 3806 3807 seq = ntohl(req->request_seq); 3808 if (asoc->str_reset_seq_in == seq) { 3809 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3810 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) { 3811 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3812 } else { 3813 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3814 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3815 fwdtsn.ch.chunk_flags = 0; 3816 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3817 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3818 if (abort_flag) { 3819 return (1); 3820 } 3821 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3822 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3823 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3824 } 3825 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map; 3826 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1; 3827 memset(asoc->mapping_array, 0, asoc->mapping_array_size); 3828 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map; 3829 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size); 3830 atomic_add_int(&asoc->sending_seq, 1); 3831 /* save off historical data for retrans */ 3832 asoc->last_sending_seq[1] = asoc->last_sending_seq[0]; 3833 asoc->last_sending_seq[0] = asoc->sending_seq; 3834 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0]; 3835 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn; 3836 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3837 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3838 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3839 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0); 3840 } 3841 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3842 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]); 3843 asoc->str_reset_seq_in++; 3844 } else if (asoc->str_reset_seq_in - 1 == seq) { 3845 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3846 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]); 3847 } else if (asoc->str_reset_seq_in - 2 == seq) { 3848 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3849 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]); 3850 } else { 3851 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 3852 } 3853 return (0); 3854 } 3855 3856 static void 3857 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3858 struct sctp_tmit_chunk *chk, 3859 struct sctp_stream_reset_out_request *req, int trunc) 3860 { 3861 uint32_t seq, tsn; 3862 int number_entries, len; 3863 struct sctp_association *asoc = &stcb->asoc; 3864 3865 seq = ntohl(req->request_seq); 3866 3867 /* now if its not a duplicate we process it */ 3868 if (asoc->str_reset_seq_in == seq) { 3869 len = ntohs(req->ph.param_length); 3870 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3871 /* 3872 * the sender is resetting, handle the list issue.. we must 3873 * a) verify if we can do the reset, if so no problem b) If 3874 * we can't do the reset we must copy the request. c) queue 3875 * it, and setup the data in processor to trigger it off 3876 * when needed and dequeue all the queued data. 3877 */ 3878 tsn = ntohl(req->send_reset_at_tsn); 3879 3880 /* move the reset action back one */ 3881 asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3882 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) { 3883 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3884 } else if (trunc) { 3885 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3886 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) { 3887 /* we can do it now */ 3888 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3889 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3890 } else { 3891 /* 3892 * we must queue it up and thus wait for the TSN's 3893 * to arrive that are at or before tsn 3894 */ 3895 struct sctp_stream_reset_list *liste; 3896 int siz; 3897 3898 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3899 SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3900 siz, SCTP_M_STRESET); 3901 if (liste == NULL) { 3902 /* gak out of memory */ 3903 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3904 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3905 return; 3906 } 3907 liste->tsn = tsn; 3908 liste->number_entries = number_entries; 3909 memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t)); 3910 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3911 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3912 } 3913 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3914 asoc->str_reset_seq_in++; 3915 } else if ((asoc->str_reset_seq_in - 1) == seq) { 3916 /* 3917 * one seq back, just echo back last action since my 3918 * response was lost. 3919 */ 3920 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3921 } else if ((asoc->str_reset_seq_in - 2) == seq) { 3922 /* 3923 * two seq back, just echo back last action since my 3924 * response was lost. 3925 */ 3926 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3927 } else { 3928 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 3929 } 3930 } 3931 3932 static void 3933 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, 3934 struct sctp_stream_reset_add_strm *str_add) 3935 { 3936 /* 3937 * Peer is requesting to add more streams. If its within our 3938 * max-streams we will allow it. 3939 */ 3940 uint32_t num_stream, i; 3941 uint32_t seq; 3942 struct sctp_association *asoc = &stcb->asoc; 3943 struct sctp_queued_to_read *ctl, *nctl; 3944 3945 /* Get the number. */ 3946 seq = ntohl(str_add->request_seq); 3947 num_stream = ntohs(str_add->number_of_streams); 3948 /* Now what would be the new total? */ 3949 if (asoc->str_reset_seq_in == seq) { 3950 num_stream += stcb->asoc.streamincnt; 3951 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3952 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) { 3953 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3954 } else if ((num_stream > stcb->asoc.max_inbound_streams) || 3955 (num_stream > 0xffff)) { 3956 /* We must reject it they ask for to many */ 3957 denied: 3958 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3959 } else { 3960 /* Ok, we can do that :-) */ 3961 struct sctp_stream_in *oldstrm; 3962 3963 /* save off the old */ 3964 oldstrm = stcb->asoc.strmin; 3965 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *, 3966 (num_stream * sizeof(struct sctp_stream_in)), 3967 SCTP_M_STRMI); 3968 if (stcb->asoc.strmin == NULL) { 3969 stcb->asoc.strmin = oldstrm; 3970 goto denied; 3971 } 3972 /* copy off the old data */ 3973 for (i = 0; i < stcb->asoc.streamincnt; i++) { 3974 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3975 stcb->asoc.strmin[i].stream_no = i; 3976 stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered; 3977 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started; 3978 /* now anything on those queues? */ 3979 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) { 3980 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next); 3981 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next); 3982 } 3983 } 3984 /* Init the new streams */ 3985 for (i = stcb->asoc.streamincnt; i < num_stream; i++) { 3986 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3987 stcb->asoc.strmin[i].stream_no = i; 3988 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3989 stcb->asoc.strmin[i].delivery_started = 0; 3990 } 3991 SCTP_FREE(oldstrm, SCTP_M_STRMI); 3992 /* update the size */ 3993 stcb->asoc.streamincnt = num_stream; 3994 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3995 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0); 3996 } 3997 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3998 asoc->str_reset_seq_in++; 3999 } else if ((asoc->str_reset_seq_in - 1) == seq) { 4000 /* 4001 * one seq back, just echo back last action since my 4002 * response was lost. 4003 */ 4004 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 4005 } else if ((asoc->str_reset_seq_in - 2) == seq) { 4006 /* 4007 * two seq back, just echo back last action since my 4008 * response was lost. 4009 */ 4010 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 4011 } else { 4012 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 4013 4014 } 4015 } 4016 4017 static void 4018 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, 4019 struct sctp_stream_reset_add_strm *str_add) 4020 { 4021 /* 4022 * Peer is requesting to add more streams. If its within our 4023 * max-streams we will allow it. 4024 */ 4025 uint16_t num_stream; 4026 uint32_t seq; 4027 struct sctp_association *asoc = &stcb->asoc; 4028 4029 /* Get the number. */ 4030 seq = ntohl(str_add->request_seq); 4031 num_stream = ntohs(str_add->number_of_streams); 4032 /* Now what would be the new total? */ 4033 if (asoc->str_reset_seq_in == seq) { 4034 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 4035 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) { 4036 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 4037 } else if (stcb->asoc.stream_reset_outstanding) { 4038 /* We must reject it we have something pending */ 4039 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS; 4040 } else { 4041 /* Ok, we can do that :-) */ 4042 int mychk; 4043 4044 mychk = stcb->asoc.streamoutcnt; 4045 mychk += num_stream; 4046 if (mychk < 0x10000) { 4047 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 4048 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 0, 1, num_stream, 0, 1)) { 4049 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 4050 } 4051 } else { 4052 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 4053 } 4054 } 4055 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]); 4056 asoc->str_reset_seq_in++; 4057 } else if ((asoc->str_reset_seq_in - 1) == seq) { 4058 /* 4059 * one seq back, just echo back last action since my 4060 * response was lost. 4061 */ 4062 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 4063 } else if ((asoc->str_reset_seq_in - 2) == seq) { 4064 /* 4065 * two seq back, just echo back last action since my 4066 * response was lost. 4067 */ 4068 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 4069 } else { 4070 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 4071 } 4072 } 4073 4074 #ifdef __GNUC__ 4075 __attribute__((noinline)) 4076 #endif 4077 static int 4078 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset, 4079 struct sctp_chunkhdr *ch_req) 4080 { 4081 int chk_length, param_len, ptype; 4082 struct sctp_paramhdr pstore; 4083 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE]; 4084 uint32_t seq = 0; 4085 int num_req = 0; 4086 int trunc = 0; 4087 struct sctp_tmit_chunk *chk; 4088 struct sctp_chunkhdr *ch; 4089 struct sctp_paramhdr *ph; 4090 int ret_code = 0; 4091 int num_param = 0; 4092 4093 /* now it may be a reset or a reset-response */ 4094 chk_length = ntohs(ch_req->chunk_length); 4095 4096 /* setup for adding the response */ 4097 sctp_alloc_a_chunk(stcb, chk); 4098 if (chk == NULL) { 4099 return (ret_code); 4100 } 4101 chk->rec.chunk_id.id = SCTP_STREAM_RESET; 4102 chk->rec.chunk_id.can_take_data = 0; 4103 chk->asoc = &stcb->asoc; 4104 chk->no_fr_allowed = 0; 4105 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 4106 chk->book_size_scale = 0; 4107 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); 4108 if (chk->data == NULL) { 4109 strres_nochunk: 4110 if (chk->data) { 4111 sctp_m_freem(chk->data); 4112 chk->data = NULL; 4113 } 4114 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 4115 return (ret_code); 4116 } 4117 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 4118 4119 /* setup chunk parameters */ 4120 chk->sent = SCTP_DATAGRAM_UNSENT; 4121 chk->snd_count = 0; 4122 chk->whoTo = NULL; 4123 4124 ch = mtod(chk->data, struct sctp_chunkhdr *); 4125 ch->chunk_type = SCTP_STREAM_RESET; 4126 ch->chunk_flags = 0; 4127 ch->chunk_length = htons(chk->send_size); 4128 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 4129 offset += sizeof(struct sctp_chunkhdr); 4130 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 4131 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore); 4132 if (ph == NULL) 4133 break; 4134 param_len = ntohs(ph->param_length); 4135 if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 4136 /* bad param */ 4137 break; 4138 } 4139 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)), 4140 (uint8_t *) & cstore); 4141 ptype = ntohs(ph->param_type); 4142 num_param++; 4143 if (param_len > (int)sizeof(cstore)) { 4144 trunc = 1; 4145 } else { 4146 trunc = 0; 4147 } 4148 if (num_param > SCTP_MAX_RESET_PARAMS) { 4149 /* hit the max of parameters already sorry.. */ 4150 break; 4151 } 4152 if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 4153 struct sctp_stream_reset_out_request *req_out; 4154 4155 req_out = (struct sctp_stream_reset_out_request *)ph; 4156 num_req++; 4157 if (stcb->asoc.stream_reset_outstanding) { 4158 seq = ntohl(req_out->response_seq); 4159 if (seq == stcb->asoc.str_reset_seq_out) { 4160 /* implicit ack */ 4161 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL); 4162 } 4163 } 4164 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc); 4165 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) { 4166 struct sctp_stream_reset_add_strm *str_add; 4167 4168 str_add = (struct sctp_stream_reset_add_strm *)ph; 4169 num_req++; 4170 sctp_handle_str_reset_add_strm(stcb, chk, str_add); 4171 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) { 4172 struct sctp_stream_reset_add_strm *str_add; 4173 4174 str_add = (struct sctp_stream_reset_add_strm *)ph; 4175 num_req++; 4176 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add); 4177 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 4178 struct sctp_stream_reset_in_request *req_in; 4179 4180 num_req++; 4181 req_in = (struct sctp_stream_reset_in_request *)ph; 4182 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc); 4183 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 4184 struct sctp_stream_reset_tsn_request *req_tsn; 4185 4186 num_req++; 4187 req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 4188 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 4189 ret_code = 1; 4190 goto strres_nochunk; 4191 } 4192 /* no more */ 4193 break; 4194 } else if (ptype == SCTP_STR_RESET_RESPONSE) { 4195 struct sctp_stream_reset_response *resp; 4196 uint32_t result; 4197 4198 resp = (struct sctp_stream_reset_response *)ph; 4199 seq = ntohl(resp->response_seq); 4200 result = ntohl(resp->result); 4201 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 4202 ret_code = 1; 4203 goto strres_nochunk; 4204 } 4205 } else { 4206 break; 4207 } 4208 offset += SCTP_SIZE32(param_len); 4209 chk_length -= SCTP_SIZE32(param_len); 4210 } 4211 if (num_req == 0) { 4212 /* we have no response free the stuff */ 4213 goto strres_nochunk; 4214 } 4215 /* ok we have a chunk to link in */ 4216 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 4217 chk, 4218 sctp_next); 4219 stcb->asoc.ctrl_queue_cnt++; 4220 return (ret_code); 4221 } 4222 4223 /* 4224 * Handle a router or endpoints report of a packet loss, there are two ways 4225 * to handle this, either we get the whole packet and must disect it 4226 * ourselves (possibly with truncation and or corruption) or it is a summary 4227 * from a middle box that did the disectting for us. 4228 */ 4229 static void 4230 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 4231 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 4232 { 4233 uint32_t bottle_bw, on_queue; 4234 uint16_t trunc_len; 4235 unsigned int chlen; 4236 unsigned int at; 4237 struct sctp_chunk_desc desc; 4238 struct sctp_chunkhdr *ch; 4239 4240 chlen = ntohs(cp->ch.chunk_length); 4241 chlen -= sizeof(struct sctp_pktdrop_chunk); 4242 /* XXX possible chlen underflow */ 4243 if (chlen == 0) { 4244 ch = NULL; 4245 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 4246 SCTP_STAT_INCR(sctps_pdrpbwrpt); 4247 } else { 4248 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 4249 chlen -= sizeof(struct sctphdr); 4250 /* XXX possible chlen underflow */ 4251 memset(&desc, 0, sizeof(desc)); 4252 } 4253 trunc_len = (uint16_t) ntohs(cp->trunc_len); 4254 if (trunc_len > limit) { 4255 trunc_len = limit; 4256 } 4257 /* now the chunks themselves */ 4258 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 4259 desc.chunk_type = ch->chunk_type; 4260 /* get amount we need to move */ 4261 at = ntohs(ch->chunk_length); 4262 if (at < sizeof(struct sctp_chunkhdr)) { 4263 /* corrupt chunk, maybe at the end? */ 4264 SCTP_STAT_INCR(sctps_pdrpcrupt); 4265 break; 4266 } 4267 if (trunc_len == 0) { 4268 /* we are supposed to have all of it */ 4269 if (at > chlen) { 4270 /* corrupt skip it */ 4271 SCTP_STAT_INCR(sctps_pdrpcrupt); 4272 break; 4273 } 4274 } else { 4275 /* is there enough of it left ? */ 4276 if (desc.chunk_type == SCTP_DATA) { 4277 if (chlen < (sizeof(struct sctp_data_chunk) + 4278 sizeof(desc.data_bytes))) { 4279 break; 4280 } 4281 } else { 4282 if (chlen < sizeof(struct sctp_chunkhdr)) { 4283 break; 4284 } 4285 } 4286 } 4287 if (desc.chunk_type == SCTP_DATA) { 4288 /* can we get out the tsn? */ 4289 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4290 SCTP_STAT_INCR(sctps_pdrpmbda); 4291 4292 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 4293 /* yep */ 4294 struct sctp_data_chunk *dcp; 4295 uint8_t *ddp; 4296 unsigned int iii; 4297 4298 dcp = (struct sctp_data_chunk *)ch; 4299 ddp = (uint8_t *) (dcp + 1); 4300 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 4301 desc.data_bytes[iii] = ddp[iii]; 4302 } 4303 desc.tsn_ifany = dcp->dp.tsn; 4304 } else { 4305 /* nope we are done. */ 4306 SCTP_STAT_INCR(sctps_pdrpnedat); 4307 break; 4308 } 4309 } else { 4310 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4311 SCTP_STAT_INCR(sctps_pdrpmbct); 4312 } 4313 4314 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 4315 SCTP_STAT_INCR(sctps_pdrppdbrk); 4316 break; 4317 } 4318 if (SCTP_SIZE32(at) > chlen) { 4319 break; 4320 } 4321 chlen -= SCTP_SIZE32(at); 4322 if (chlen < sizeof(struct sctp_chunkhdr)) { 4323 /* done, none left */ 4324 break; 4325 } 4326 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 4327 } 4328 /* Now update any rwnd --- possibly */ 4329 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 4330 /* From a peer, we get a rwnd report */ 4331 uint32_t a_rwnd; 4332 4333 SCTP_STAT_INCR(sctps_pdrpfehos); 4334 4335 bottle_bw = ntohl(cp->bottle_bw); 4336 on_queue = ntohl(cp->current_onq); 4337 if (bottle_bw && on_queue) { 4338 /* a rwnd report is in here */ 4339 if (bottle_bw > on_queue) 4340 a_rwnd = bottle_bw - on_queue; 4341 else 4342 a_rwnd = 0; 4343 4344 if (a_rwnd == 0) 4345 stcb->asoc.peers_rwnd = 0; 4346 else { 4347 if (a_rwnd > stcb->asoc.total_flight) { 4348 stcb->asoc.peers_rwnd = 4349 a_rwnd - stcb->asoc.total_flight; 4350 } else { 4351 stcb->asoc.peers_rwnd = 0; 4352 } 4353 if (stcb->asoc.peers_rwnd < 4354 stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 4355 /* SWS sender side engages */ 4356 stcb->asoc.peers_rwnd = 0; 4357 } 4358 } 4359 } 4360 } else { 4361 SCTP_STAT_INCR(sctps_pdrpfmbox); 4362 } 4363 4364 /* now middle boxes in sat networks get a cwnd bump */ 4365 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 4366 (stcb->asoc.sat_t3_loss_recovery == 0) && 4367 (stcb->asoc.sat_network)) { 4368 /* 4369 * This is debateable but for sat networks it makes sense 4370 * Note if a T3 timer has went off, we will prohibit any 4371 * changes to cwnd until we exit the t3 loss recovery. 4372 */ 4373 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb, 4374 net, cp, &bottle_bw, &on_queue); 4375 } 4376 } 4377 4378 /* 4379 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 4380 * still contain IP/SCTP header - stcb: is the tcb found for this packet - 4381 * offset: offset into the mbuf chain to first chunkhdr - length: is the 4382 * length of the complete packet outputs: - length: modified to remaining 4383 * length after control processing - netp: modified to new sctp_nets after 4384 * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 4385 * bad packet,...) otherwise return the tcb for this packet 4386 */ 4387 #ifdef __GNUC__ 4388 __attribute__((noinline)) 4389 #endif 4390 static struct sctp_tcb * 4391 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 4392 struct sockaddr *src, struct sockaddr *dst, 4393 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 4394 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 4395 uint8_t use_mflowid, uint32_t mflowid, 4396 uint32_t vrf_id, uint16_t port) 4397 { 4398 struct sctp_association *asoc; 4399 uint32_t vtag_in; 4400 int num_chunks = 0; /* number of control chunks processed */ 4401 uint32_t chk_length; 4402 int ret; 4403 int abort_no_unlock = 0; 4404 int ecne_seen = 0; 4405 4406 /* 4407 * How big should this be, and should it be alloc'd? Lets try the 4408 * d-mtu-ceiling for now (2k) and that should hopefully work ... 4409 * until we get into jumbo grams and such.. 4410 */ 4411 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 4412 struct sctp_tcb *locked_tcb = stcb; 4413 int got_auth = 0; 4414 uint32_t auth_offset = 0, auth_len = 0; 4415 int auth_skipped = 0; 4416 int asconf_cnt = 0; 4417 4418 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4419 struct socket *so; 4420 4421 #endif 4422 4423 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 4424 iphlen, *offset, length, (void *)stcb); 4425 4426 /* validate chunk header length... */ 4427 if (ntohs(ch->chunk_length) < sizeof(*ch)) { 4428 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 4429 ntohs(ch->chunk_length)); 4430 if (locked_tcb) { 4431 SCTP_TCB_UNLOCK(locked_tcb); 4432 } 4433 return (NULL); 4434 } 4435 /* 4436 * validate the verification tag 4437 */ 4438 vtag_in = ntohl(sh->v_tag); 4439 4440 if (locked_tcb) { 4441 SCTP_TCB_LOCK_ASSERT(locked_tcb); 4442 } 4443 if (ch->chunk_type == SCTP_INITIATION) { 4444 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 4445 ntohs(ch->chunk_length), vtag_in); 4446 if (vtag_in != 0) { 4447 /* protocol error- silently discard... */ 4448 SCTP_STAT_INCR(sctps_badvtag); 4449 if (locked_tcb) { 4450 SCTP_TCB_UNLOCK(locked_tcb); 4451 } 4452 return (NULL); 4453 } 4454 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 4455 /* 4456 * If there is no stcb, skip the AUTH chunk and process 4457 * later after a stcb is found (to validate the lookup was 4458 * valid. 4459 */ 4460 if ((ch->chunk_type == SCTP_AUTHENTICATION) && 4461 (stcb == NULL) && 4462 !SCTP_BASE_SYSCTL(sctp_auth_disable)) { 4463 /* save this chunk for later processing */ 4464 auth_skipped = 1; 4465 auth_offset = *offset; 4466 auth_len = ntohs(ch->chunk_length); 4467 4468 /* (temporarily) move past this chunk */ 4469 *offset += SCTP_SIZE32(auth_len); 4470 if (*offset >= length) { 4471 /* no more data left in the mbuf chain */ 4472 *offset = length; 4473 if (locked_tcb) { 4474 SCTP_TCB_UNLOCK(locked_tcb); 4475 } 4476 return (NULL); 4477 } 4478 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4479 sizeof(struct sctp_chunkhdr), chunk_buf); 4480 } 4481 if (ch == NULL) { 4482 /* Help */ 4483 *offset = length; 4484 if (locked_tcb) { 4485 SCTP_TCB_UNLOCK(locked_tcb); 4486 } 4487 return (NULL); 4488 } 4489 if (ch->chunk_type == SCTP_COOKIE_ECHO) { 4490 goto process_control_chunks; 4491 } 4492 /* 4493 * first check if it's an ASCONF with an unknown src addr we 4494 * need to look inside to find the association 4495 */ 4496 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 4497 struct sctp_chunkhdr *asconf_ch = ch; 4498 uint32_t asconf_offset = 0, asconf_len = 0; 4499 4500 /* inp's refcount may be reduced */ 4501 SCTP_INP_INCR_REF(inp); 4502 4503 asconf_offset = *offset; 4504 do { 4505 asconf_len = ntohs(asconf_ch->chunk_length); 4506 if (asconf_len < sizeof(struct sctp_asconf_paramhdr)) 4507 break; 4508 stcb = sctp_findassociation_ep_asconf(m, 4509 *offset, 4510 dst, 4511 sh, &inp, netp, vrf_id); 4512 if (stcb != NULL) 4513 break; 4514 asconf_offset += SCTP_SIZE32(asconf_len); 4515 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset, 4516 sizeof(struct sctp_chunkhdr), chunk_buf); 4517 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF); 4518 if (stcb == NULL) { 4519 /* 4520 * reduce inp's refcount if not reduced in 4521 * sctp_findassociation_ep_asconf(). 4522 */ 4523 SCTP_INP_DECR_REF(inp); 4524 } else { 4525 locked_tcb = stcb; 4526 } 4527 4528 /* now go back and verify any auth chunk to be sure */ 4529 if (auth_skipped && (stcb != NULL)) { 4530 struct sctp_auth_chunk *auth; 4531 4532 auth = (struct sctp_auth_chunk *) 4533 sctp_m_getptr(m, auth_offset, 4534 auth_len, chunk_buf); 4535 got_auth = 1; 4536 auth_skipped = 0; 4537 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 4538 auth_offset)) { 4539 /* auth HMAC failed so dump it */ 4540 *offset = length; 4541 if (locked_tcb) { 4542 SCTP_TCB_UNLOCK(locked_tcb); 4543 } 4544 return (NULL); 4545 } else { 4546 /* remaining chunks are HMAC checked */ 4547 stcb->asoc.authenticated = 1; 4548 } 4549 } 4550 } 4551 if (stcb == NULL) { 4552 /* no association, so it's out of the blue... */ 4553 sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, 4554 use_mflowid, mflowid, 4555 vrf_id, port); 4556 *offset = length; 4557 if (locked_tcb) { 4558 SCTP_TCB_UNLOCK(locked_tcb); 4559 } 4560 return (NULL); 4561 } 4562 asoc = &stcb->asoc; 4563 /* ABORT and SHUTDOWN can use either v_tag... */ 4564 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 4565 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 4566 (ch->chunk_type == SCTP_PACKET_DROPPED)) { 4567 /* Take the T-bit always into account. */ 4568 if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) && 4569 (vtag_in == asoc->my_vtag)) || 4570 (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) && 4571 (vtag_in == asoc->peer_vtag))) { 4572 /* this is valid */ 4573 } else { 4574 /* drop this packet... */ 4575 SCTP_STAT_INCR(sctps_badvtag); 4576 if (locked_tcb) { 4577 SCTP_TCB_UNLOCK(locked_tcb); 4578 } 4579 return (NULL); 4580 } 4581 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 4582 if (vtag_in != asoc->my_vtag) { 4583 /* 4584 * this could be a stale SHUTDOWN-ACK or the 4585 * peer never got the SHUTDOWN-COMPLETE and 4586 * is still hung; we have started a new asoc 4587 * but it won't complete until the shutdown 4588 * is completed 4589 */ 4590 if (locked_tcb) { 4591 SCTP_TCB_UNLOCK(locked_tcb); 4592 } 4593 sctp_handle_ootb(m, iphlen, *offset, src, dst, 4594 sh, inp, 4595 use_mflowid, mflowid, 4596 vrf_id, port); 4597 return (NULL); 4598 } 4599 } else { 4600 /* for all other chunks, vtag must match */ 4601 if (vtag_in != asoc->my_vtag) { 4602 /* invalid vtag... */ 4603 SCTPDBG(SCTP_DEBUG_INPUT3, 4604 "invalid vtag: %xh, expect %xh\n", 4605 vtag_in, asoc->my_vtag); 4606 SCTP_STAT_INCR(sctps_badvtag); 4607 if (locked_tcb) { 4608 SCTP_TCB_UNLOCK(locked_tcb); 4609 } 4610 *offset = length; 4611 return (NULL); 4612 } 4613 } 4614 } /* end if !SCTP_COOKIE_ECHO */ 4615 /* 4616 * process all control chunks... 4617 */ 4618 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 4619 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) || 4620 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 4621 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 4622 /* implied cookie-ack.. we must have lost the ack */ 4623 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4624 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4625 stcb->asoc.overall_error_count, 4626 0, 4627 SCTP_FROM_SCTP_INPUT, 4628 __LINE__); 4629 } 4630 stcb->asoc.overall_error_count = 0; 4631 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 4632 *netp); 4633 } 4634 process_control_chunks: 4635 while (IS_SCTP_CONTROL(ch)) { 4636 /* validate chunk length */ 4637 chk_length = ntohs(ch->chunk_length); 4638 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 4639 ch->chunk_type, chk_length); 4640 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 4641 if (chk_length < sizeof(*ch) || 4642 (*offset + (int)chk_length) > length) { 4643 *offset = length; 4644 if (locked_tcb) { 4645 SCTP_TCB_UNLOCK(locked_tcb); 4646 } 4647 return (NULL); 4648 } 4649 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 4650 /* 4651 * INIT-ACK only gets the init ack "header" portion only 4652 * because we don't have to process the peer's COOKIE. All 4653 * others get a complete chunk. 4654 */ 4655 if ((ch->chunk_type == SCTP_INITIATION_ACK) || 4656 (ch->chunk_type == SCTP_INITIATION)) { 4657 /* get an init-ack chunk */ 4658 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4659 sizeof(struct sctp_init_ack_chunk), chunk_buf); 4660 if (ch == NULL) { 4661 *offset = length; 4662 if (locked_tcb) { 4663 SCTP_TCB_UNLOCK(locked_tcb); 4664 } 4665 return (NULL); 4666 } 4667 } else { 4668 /* For cookies and all other chunks. */ 4669 if (chk_length > sizeof(chunk_buf)) { 4670 /* 4671 * use just the size of the chunk buffer so 4672 * the front part of our chunks fit in 4673 * contiguous space up to the chunk buffer 4674 * size (508 bytes). For chunks that need to 4675 * get more than that they must use the 4676 * sctp_m_getptr() function or other means 4677 * (e.g. know how to parse mbuf chains). 4678 * Cookies do this already. 4679 */ 4680 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4681 (sizeof(chunk_buf) - 4), 4682 chunk_buf); 4683 if (ch == NULL) { 4684 *offset = length; 4685 if (locked_tcb) { 4686 SCTP_TCB_UNLOCK(locked_tcb); 4687 } 4688 return (NULL); 4689 } 4690 } else { 4691 /* We can fit it all */ 4692 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4693 chk_length, chunk_buf); 4694 if (ch == NULL) { 4695 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 4696 *offset = length; 4697 if (locked_tcb) { 4698 SCTP_TCB_UNLOCK(locked_tcb); 4699 } 4700 return (NULL); 4701 } 4702 } 4703 } 4704 num_chunks++; 4705 /* Save off the last place we got a control from */ 4706 if (stcb != NULL) { 4707 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 4708 /* 4709 * allow last_control to be NULL if 4710 * ASCONF... ASCONF processing will find the 4711 * right net later 4712 */ 4713 if ((netp != NULL) && (*netp != NULL)) 4714 stcb->asoc.last_control_chunk_from = *netp; 4715 } 4716 } 4717 #ifdef SCTP_AUDITING_ENABLED 4718 sctp_audit_log(0xB0, ch->chunk_type); 4719 #endif 4720 4721 /* check to see if this chunk required auth, but isn't */ 4722 if ((stcb != NULL) && 4723 !SCTP_BASE_SYSCTL(sctp_auth_disable) && 4724 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) && 4725 !stcb->asoc.authenticated) { 4726 /* "silently" ignore */ 4727 SCTP_STAT_INCR(sctps_recvauthmissing); 4728 goto next_chunk; 4729 } 4730 switch (ch->chunk_type) { 4731 case SCTP_INITIATION: 4732 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 4733 /* The INIT chunk must be the only chunk. */ 4734 if ((num_chunks > 1) || 4735 (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4736 sctp_abort_association(inp, stcb, m, iphlen, 4737 src, dst, sh, NULL, 4738 use_mflowid, mflowid, 4739 vrf_id, port); 4740 *offset = length; 4741 return (NULL); 4742 } 4743 /* Honor our resource limit. */ 4744 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) { 4745 struct mbuf *op_err; 4746 4747 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 4748 sctp_abort_association(inp, stcb, m, iphlen, 4749 src, dst, sh, op_err, 4750 use_mflowid, mflowid, 4751 vrf_id, port); 4752 *offset = length; 4753 return (NULL); 4754 } 4755 sctp_handle_init(m, iphlen, *offset, src, dst, sh, 4756 (struct sctp_init_chunk *)ch, inp, 4757 stcb, &abort_no_unlock, 4758 use_mflowid, mflowid, 4759 vrf_id, port); 4760 *offset = length; 4761 if ((!abort_no_unlock) && (locked_tcb)) { 4762 SCTP_TCB_UNLOCK(locked_tcb); 4763 } 4764 return (NULL); 4765 break; 4766 case SCTP_PAD_CHUNK: 4767 break; 4768 case SCTP_INITIATION_ACK: 4769 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 4770 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4771 /* We are not interested anymore */ 4772 if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4773 ; 4774 } else { 4775 if (locked_tcb != stcb) { 4776 /* Very unlikely */ 4777 SCTP_TCB_UNLOCK(locked_tcb); 4778 } 4779 *offset = length; 4780 if (stcb) { 4781 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4782 so = SCTP_INP_SO(inp); 4783 atomic_add_int(&stcb->asoc.refcnt, 1); 4784 SCTP_TCB_UNLOCK(stcb); 4785 SCTP_SOCKET_LOCK(so, 1); 4786 SCTP_TCB_LOCK(stcb); 4787 atomic_subtract_int(&stcb->asoc.refcnt, 1); 4788 #endif 4789 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4790 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4791 SCTP_SOCKET_UNLOCK(so, 1); 4792 #endif 4793 } 4794 return (NULL); 4795 } 4796 } 4797 /* The INIT-ACK chunk must be the only chunk. */ 4798 if ((num_chunks > 1) || 4799 (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4800 *offset = length; 4801 if (locked_tcb) { 4802 SCTP_TCB_UNLOCK(locked_tcb); 4803 } 4804 return (NULL); 4805 } 4806 if ((netp) && (*netp)) { 4807 ret = sctp_handle_init_ack(m, iphlen, *offset, 4808 src, dst, sh, 4809 (struct sctp_init_ack_chunk *)ch, 4810 stcb, *netp, 4811 &abort_no_unlock, 4812 use_mflowid, mflowid, 4813 vrf_id); 4814 } else { 4815 ret = -1; 4816 } 4817 *offset = length; 4818 if (abort_no_unlock) { 4819 return (NULL); 4820 } 4821 /* 4822 * Special case, I must call the output routine to 4823 * get the cookie echoed 4824 */ 4825 if ((stcb != NULL) && (ret == 0)) { 4826 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4827 } 4828 if (locked_tcb) { 4829 SCTP_TCB_UNLOCK(locked_tcb); 4830 } 4831 return (NULL); 4832 break; 4833 case SCTP_SELECTIVE_ACK: 4834 { 4835 struct sctp_sack_chunk *sack; 4836 int abort_now = 0; 4837 uint32_t a_rwnd, cum_ack; 4838 uint16_t num_seg, num_dup; 4839 uint8_t flags; 4840 int offset_seg, offset_dup; 4841 4842 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 4843 SCTP_STAT_INCR(sctps_recvsacks); 4844 if (stcb == NULL) { 4845 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n"); 4846 break; 4847 } 4848 if (chk_length < sizeof(struct sctp_sack_chunk)) { 4849 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n"); 4850 break; 4851 } 4852 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4853 /*- 4854 * If we have sent a shutdown-ack, we will pay no 4855 * attention to a sack sent in to us since 4856 * we don't care anymore. 4857 */ 4858 break; 4859 } 4860 sack = (struct sctp_sack_chunk *)ch; 4861 flags = ch->chunk_flags; 4862 cum_ack = ntohl(sack->sack.cum_tsn_ack); 4863 num_seg = ntohs(sack->sack.num_gap_ack_blks); 4864 num_dup = ntohs(sack->sack.num_dup_tsns); 4865 a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 4866 if (sizeof(struct sctp_sack_chunk) + 4867 num_seg * sizeof(struct sctp_gap_ack_block) + 4868 num_dup * sizeof(uint32_t) != chk_length) { 4869 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n"); 4870 break; 4871 } 4872 offset_seg = *offset + sizeof(struct sctp_sack_chunk); 4873 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4874 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4875 cum_ack, num_seg, a_rwnd); 4876 stcb->asoc.seen_a_sack_this_pkt = 1; 4877 if ((stcb->asoc.pr_sctp_cnt == 0) && 4878 (num_seg == 0) && 4879 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && 4880 (stcb->asoc.saw_sack_with_frags == 0) && 4881 (stcb->asoc.saw_sack_with_nr_frags == 0) && 4882 (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 4883 ) { 4884 /* 4885 * We have a SIMPLE sack having no 4886 * prior segments and data on sent 4887 * queue to be acked.. Use the 4888 * faster path sack processing. We 4889 * also allow window update sacks 4890 * with no missing segments to go 4891 * this way too. 4892 */ 4893 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen); 4894 } else { 4895 if (netp && *netp) 4896 sctp_handle_sack(m, offset_seg, offset_dup, stcb, 4897 num_seg, 0, num_dup, &abort_now, flags, 4898 cum_ack, a_rwnd, ecne_seen); 4899 } 4900 if (abort_now) { 4901 /* ABORT signal from sack processing */ 4902 *offset = length; 4903 return (NULL); 4904 } 4905 if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4906 TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4907 (stcb->asoc.stream_queue_cnt == 0)) { 4908 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4909 } 4910 } 4911 break; 4912 /* 4913 * EY - nr_sack: If the received chunk is an 4914 * nr_sack chunk 4915 */ 4916 case SCTP_NR_SELECTIVE_ACK: 4917 { 4918 struct sctp_nr_sack_chunk *nr_sack; 4919 int abort_now = 0; 4920 uint32_t a_rwnd, cum_ack; 4921 uint16_t num_seg, num_nr_seg, num_dup; 4922 uint8_t flags; 4923 int offset_seg, offset_dup; 4924 4925 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n"); 4926 SCTP_STAT_INCR(sctps_recvsacks); 4927 if (stcb == NULL) { 4928 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n"); 4929 break; 4930 } 4931 if ((stcb->asoc.sctp_nr_sack_on_off == 0) || 4932 (stcb->asoc.peer_supports_nr_sack == 0)) { 4933 goto unknown_chunk; 4934 } 4935 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) { 4936 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n"); 4937 break; 4938 } 4939 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4940 /*- 4941 * If we have sent a shutdown-ack, we will pay no 4942 * attention to a sack sent in to us since 4943 * we don't care anymore. 4944 */ 4945 break; 4946 } 4947 nr_sack = (struct sctp_nr_sack_chunk *)ch; 4948 flags = ch->chunk_flags; 4949 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack); 4950 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks); 4951 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks); 4952 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns); 4953 a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd); 4954 if (sizeof(struct sctp_nr_sack_chunk) + 4955 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) + 4956 num_dup * sizeof(uint32_t) != chk_length) { 4957 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n"); 4958 break; 4959 } 4960 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk); 4961 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4962 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4963 cum_ack, num_seg, a_rwnd); 4964 stcb->asoc.seen_a_sack_this_pkt = 1; 4965 if ((stcb->asoc.pr_sctp_cnt == 0) && 4966 (num_seg == 0) && (num_nr_seg == 0) && 4967 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && 4968 (stcb->asoc.saw_sack_with_frags == 0) && 4969 (stcb->asoc.saw_sack_with_nr_frags == 0) && 4970 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) { 4971 /* 4972 * We have a SIMPLE sack having no 4973 * prior segments and data on sent 4974 * queue to be acked. Use the faster 4975 * path sack processing. We also 4976 * allow window update sacks with no 4977 * missing segments to go this way 4978 * too. 4979 */ 4980 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 4981 &abort_now, ecne_seen); 4982 } else { 4983 if (netp && *netp) 4984 sctp_handle_sack(m, offset_seg, offset_dup, stcb, 4985 num_seg, num_nr_seg, num_dup, &abort_now, flags, 4986 cum_ack, a_rwnd, ecne_seen); 4987 } 4988 if (abort_now) { 4989 /* ABORT signal from sack processing */ 4990 *offset = length; 4991 return (NULL); 4992 } 4993 if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4994 TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4995 (stcb->asoc.stream_queue_cnt == 0)) { 4996 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4997 } 4998 } 4999 break; 5000 5001 case SCTP_HEARTBEAT_REQUEST: 5002 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 5003 if ((stcb) && netp && *netp) { 5004 SCTP_STAT_INCR(sctps_recvheartbeat); 5005 sctp_send_heartbeat_ack(stcb, m, *offset, 5006 chk_length, *netp); 5007 5008 /* He's alive so give him credit */ 5009 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5010 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5011 stcb->asoc.overall_error_count, 5012 0, 5013 SCTP_FROM_SCTP_INPUT, 5014 __LINE__); 5015 } 5016 stcb->asoc.overall_error_count = 0; 5017 } 5018 break; 5019 case SCTP_HEARTBEAT_ACK: 5020 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 5021 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 5022 /* Its not ours */ 5023 *offset = length; 5024 if (locked_tcb) { 5025 SCTP_TCB_UNLOCK(locked_tcb); 5026 } 5027 return (NULL); 5028 } 5029 /* He's alive so give him credit */ 5030 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5031 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5032 stcb->asoc.overall_error_count, 5033 0, 5034 SCTP_FROM_SCTP_INPUT, 5035 __LINE__); 5036 } 5037 stcb->asoc.overall_error_count = 0; 5038 SCTP_STAT_INCR(sctps_recvheartbeatack); 5039 if (netp && *netp) 5040 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 5041 stcb, *netp); 5042 break; 5043 case SCTP_ABORT_ASSOCIATION: 5044 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 5045 (void *)stcb); 5046 if ((stcb) && netp && *netp) 5047 sctp_handle_abort((struct sctp_abort_chunk *)ch, 5048 stcb, *netp); 5049 *offset = length; 5050 return (NULL); 5051 break; 5052 case SCTP_SHUTDOWN: 5053 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 5054 (void *)stcb); 5055 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 5056 *offset = length; 5057 if (locked_tcb) { 5058 SCTP_TCB_UNLOCK(locked_tcb); 5059 } 5060 return (NULL); 5061 } 5062 if (netp && *netp) { 5063 int abort_flag = 0; 5064 5065 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 5066 stcb, *netp, &abort_flag); 5067 if (abort_flag) { 5068 *offset = length; 5069 return (NULL); 5070 } 5071 } 5072 break; 5073 case SCTP_SHUTDOWN_ACK: 5074 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", (void *)stcb); 5075 if ((stcb) && (netp) && (*netp)) 5076 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 5077 *offset = length; 5078 return (NULL); 5079 break; 5080 5081 case SCTP_OPERATION_ERROR: 5082 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 5083 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 5084 *offset = length; 5085 return (NULL); 5086 } 5087 break; 5088 case SCTP_COOKIE_ECHO: 5089 SCTPDBG(SCTP_DEBUG_INPUT3, 5090 "SCTP_COOKIE-ECHO, stcb %p\n", (void *)stcb); 5091 if ((stcb) && (stcb->asoc.total_output_queue_size)) { 5092 ; 5093 } else { 5094 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5095 /* We are not interested anymore */ 5096 abend: 5097 if (stcb) { 5098 SCTP_TCB_UNLOCK(stcb); 5099 } 5100 *offset = length; 5101 return (NULL); 5102 } 5103 } 5104 /* 5105 * First are we accepting? We do this again here 5106 * since it is possible that a previous endpoint WAS 5107 * listening responded to a INIT-ACK and then 5108 * closed. We opened and bound.. and are now no 5109 * longer listening. 5110 */ 5111 5112 if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) { 5113 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 5114 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) { 5115 struct mbuf *op_err; 5116 5117 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 5118 sctp_abort_association(inp, stcb, m, iphlen, 5119 src, dst, sh, op_err, 5120 use_mflowid, mflowid, 5121 vrf_id, port); 5122 } 5123 *offset = length; 5124 return (NULL); 5125 } else { 5126 struct mbuf *ret_buf; 5127 struct sctp_inpcb *linp; 5128 5129 if (stcb) { 5130 linp = NULL; 5131 } else { 5132 linp = inp; 5133 } 5134 5135 if (linp) { 5136 SCTP_ASOC_CREATE_LOCK(linp); 5137 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 5138 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { 5139 SCTP_ASOC_CREATE_UNLOCK(linp); 5140 goto abend; 5141 } 5142 } 5143 if (netp) { 5144 ret_buf = 5145 sctp_handle_cookie_echo(m, iphlen, 5146 *offset, 5147 src, dst, 5148 sh, 5149 (struct sctp_cookie_echo_chunk *)ch, 5150 &inp, &stcb, netp, 5151 auth_skipped, 5152 auth_offset, 5153 auth_len, 5154 &locked_tcb, 5155 use_mflowid, 5156 mflowid, 5157 vrf_id, 5158 port); 5159 } else { 5160 ret_buf = NULL; 5161 } 5162 if (linp) { 5163 SCTP_ASOC_CREATE_UNLOCK(linp); 5164 } 5165 if (ret_buf == NULL) { 5166 if (locked_tcb) { 5167 SCTP_TCB_UNLOCK(locked_tcb); 5168 } 5169 SCTPDBG(SCTP_DEBUG_INPUT3, 5170 "GAK, null buffer\n"); 5171 *offset = length; 5172 return (NULL); 5173 } 5174 /* if AUTH skipped, see if it verified... */ 5175 if (auth_skipped) { 5176 got_auth = 1; 5177 auth_skipped = 0; 5178 } 5179 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 5180 /* 5181 * Restart the timer if we have 5182 * pending data 5183 */ 5184 struct sctp_tmit_chunk *chk; 5185 5186 chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 5187 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 5188 } 5189 } 5190 break; 5191 case SCTP_COOKIE_ACK: 5192 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", (void *)stcb); 5193 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 5194 if (locked_tcb) { 5195 SCTP_TCB_UNLOCK(locked_tcb); 5196 } 5197 return (NULL); 5198 } 5199 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5200 /* We are not interested anymore */ 5201 if ((stcb) && (stcb->asoc.total_output_queue_size)) { 5202 ; 5203 } else if (stcb) { 5204 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5205 so = SCTP_INP_SO(inp); 5206 atomic_add_int(&stcb->asoc.refcnt, 1); 5207 SCTP_TCB_UNLOCK(stcb); 5208 SCTP_SOCKET_LOCK(so, 1); 5209 SCTP_TCB_LOCK(stcb); 5210 atomic_subtract_int(&stcb->asoc.refcnt, 1); 5211 #endif 5212 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 5213 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5214 SCTP_SOCKET_UNLOCK(so, 1); 5215 #endif 5216 *offset = length; 5217 return (NULL); 5218 } 5219 } 5220 /* He's alive so give him credit */ 5221 if ((stcb) && netp && *netp) { 5222 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5223 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5224 stcb->asoc.overall_error_count, 5225 0, 5226 SCTP_FROM_SCTP_INPUT, 5227 __LINE__); 5228 } 5229 stcb->asoc.overall_error_count = 0; 5230 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 5231 } 5232 break; 5233 case SCTP_ECN_ECHO: 5234 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 5235 /* He's alive so give him credit */ 5236 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 5237 /* Its not ours */ 5238 if (locked_tcb) { 5239 SCTP_TCB_UNLOCK(locked_tcb); 5240 } 5241 *offset = length; 5242 return (NULL); 5243 } 5244 if (stcb) { 5245 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5246 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5247 stcb->asoc.overall_error_count, 5248 0, 5249 SCTP_FROM_SCTP_INPUT, 5250 __LINE__); 5251 } 5252 stcb->asoc.overall_error_count = 0; 5253 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 5254 stcb); 5255 ecne_seen = 1; 5256 } 5257 break; 5258 case SCTP_ECN_CWR: 5259 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 5260 /* He's alive so give him credit */ 5261 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 5262 /* Its not ours */ 5263 if (locked_tcb) { 5264 SCTP_TCB_UNLOCK(locked_tcb); 5265 } 5266 *offset = length; 5267 return (NULL); 5268 } 5269 if (stcb) { 5270 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5271 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5272 stcb->asoc.overall_error_count, 5273 0, 5274 SCTP_FROM_SCTP_INPUT, 5275 __LINE__); 5276 } 5277 stcb->asoc.overall_error_count = 0; 5278 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp); 5279 } 5280 break; 5281 case SCTP_SHUTDOWN_COMPLETE: 5282 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", (void *)stcb); 5283 /* must be first and only chunk */ 5284 if ((num_chunks > 1) || 5285 (length - *offset > (int)SCTP_SIZE32(chk_length))) { 5286 *offset = length; 5287 if (locked_tcb) { 5288 SCTP_TCB_UNLOCK(locked_tcb); 5289 } 5290 return (NULL); 5291 } 5292 if ((stcb) && netp && *netp) { 5293 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 5294 stcb, *netp); 5295 } 5296 *offset = length; 5297 return (NULL); 5298 break; 5299 case SCTP_ASCONF: 5300 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 5301 /* He's alive so give him credit */ 5302 if (stcb) { 5303 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5304 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5305 stcb->asoc.overall_error_count, 5306 0, 5307 SCTP_FROM_SCTP_INPUT, 5308 __LINE__); 5309 } 5310 stcb->asoc.overall_error_count = 0; 5311 sctp_handle_asconf(m, *offset, src, 5312 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0); 5313 asconf_cnt++; 5314 } 5315 break; 5316 case SCTP_ASCONF_ACK: 5317 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 5318 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 5319 /* Its not ours */ 5320 if (locked_tcb) { 5321 SCTP_TCB_UNLOCK(locked_tcb); 5322 } 5323 *offset = length; 5324 return (NULL); 5325 } 5326 if ((stcb) && netp && *netp) { 5327 /* He's alive so give him credit */ 5328 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5329 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5330 stcb->asoc.overall_error_count, 5331 0, 5332 SCTP_FROM_SCTP_INPUT, 5333 __LINE__); 5334 } 5335 stcb->asoc.overall_error_count = 0; 5336 sctp_handle_asconf_ack(m, *offset, 5337 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock); 5338 if (abort_no_unlock) 5339 return (NULL); 5340 } 5341 break; 5342 case SCTP_FORWARD_CUM_TSN: 5343 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 5344 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 5345 /* Its not ours */ 5346 if (locked_tcb) { 5347 SCTP_TCB_UNLOCK(locked_tcb); 5348 } 5349 *offset = length; 5350 return (NULL); 5351 } 5352 /* He's alive so give him credit */ 5353 if (stcb) { 5354 int abort_flag = 0; 5355 5356 stcb->asoc.overall_error_count = 0; 5357 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5358 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5359 stcb->asoc.overall_error_count, 5360 0, 5361 SCTP_FROM_SCTP_INPUT, 5362 __LINE__); 5363 } 5364 *fwd_tsn_seen = 1; 5365 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5366 /* We are not interested anymore */ 5367 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5368 so = SCTP_INP_SO(inp); 5369 atomic_add_int(&stcb->asoc.refcnt, 1); 5370 SCTP_TCB_UNLOCK(stcb); 5371 SCTP_SOCKET_LOCK(so, 1); 5372 SCTP_TCB_LOCK(stcb); 5373 atomic_subtract_int(&stcb->asoc.refcnt, 1); 5374 #endif 5375 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 5376 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5377 SCTP_SOCKET_UNLOCK(so, 1); 5378 #endif 5379 *offset = length; 5380 return (NULL); 5381 } 5382 sctp_handle_forward_tsn(stcb, 5383 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); 5384 if (abort_flag) { 5385 *offset = length; 5386 return (NULL); 5387 } else { 5388 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5389 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5390 stcb->asoc.overall_error_count, 5391 0, 5392 SCTP_FROM_SCTP_INPUT, 5393 __LINE__); 5394 } 5395 stcb->asoc.overall_error_count = 0; 5396 } 5397 5398 } 5399 break; 5400 case SCTP_STREAM_RESET: 5401 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 5402 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 5403 /* Its not ours */ 5404 if (locked_tcb) { 5405 SCTP_TCB_UNLOCK(locked_tcb); 5406 } 5407 *offset = length; 5408 return (NULL); 5409 } 5410 if (stcb->asoc.peer_supports_strreset == 0) { 5411 /* 5412 * hmm, peer should have announced this, but 5413 * we will turn it on since he is sending us 5414 * a stream reset. 5415 */ 5416 stcb->asoc.peer_supports_strreset = 1; 5417 } 5418 if (sctp_handle_stream_reset(stcb, m, *offset, ch)) { 5419 /* stop processing */ 5420 *offset = length; 5421 return (NULL); 5422 } 5423 break; 5424 case SCTP_PACKET_DROPPED: 5425 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 5426 /* re-get it all please */ 5427 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 5428 /* Its not ours */ 5429 if (locked_tcb) { 5430 SCTP_TCB_UNLOCK(locked_tcb); 5431 } 5432 *offset = length; 5433 return (NULL); 5434 } 5435 if (ch && (stcb) && netp && (*netp)) { 5436 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 5437 stcb, *netp, 5438 min(chk_length, (sizeof(chunk_buf) - 4))); 5439 5440 } 5441 break; 5442 5443 case SCTP_AUTHENTICATION: 5444 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 5445 if (SCTP_BASE_SYSCTL(sctp_auth_disable)) 5446 goto unknown_chunk; 5447 5448 if (stcb == NULL) { 5449 /* save the first AUTH for later processing */ 5450 if (auth_skipped == 0) { 5451 auth_offset = *offset; 5452 auth_len = chk_length; 5453 auth_skipped = 1; 5454 } 5455 /* skip this chunk (temporarily) */ 5456 goto next_chunk; 5457 } 5458 if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 5459 (chk_length > (sizeof(struct sctp_auth_chunk) + 5460 SCTP_AUTH_DIGEST_LEN_MAX))) { 5461 /* Its not ours */ 5462 if (locked_tcb) { 5463 SCTP_TCB_UNLOCK(locked_tcb); 5464 } 5465 *offset = length; 5466 return (NULL); 5467 } 5468 if (got_auth == 1) { 5469 /* skip this chunk... it's already auth'd */ 5470 goto next_chunk; 5471 } 5472 got_auth = 1; 5473 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 5474 m, *offset)) { 5475 /* auth HMAC failed so dump the packet */ 5476 *offset = length; 5477 return (stcb); 5478 } else { 5479 /* remaining chunks are HMAC checked */ 5480 stcb->asoc.authenticated = 1; 5481 } 5482 break; 5483 5484 default: 5485 unknown_chunk: 5486 /* it's an unknown chunk! */ 5487 if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 5488 struct mbuf *mm; 5489 struct sctp_paramhdr *phd; 5490 5491 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 5492 0, M_NOWAIT, 1, MT_DATA); 5493 if (mm) { 5494 phd = mtod(mm, struct sctp_paramhdr *); 5495 /* 5496 * We cheat and use param type since 5497 * we did not bother to define a 5498 * error cause struct. They are the 5499 * same basic format with different 5500 * names. 5501 */ 5502 phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 5503 phd->param_length = htons(chk_length + sizeof(*phd)); 5504 SCTP_BUF_LEN(mm) = sizeof(*phd); 5505 SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, chk_length, M_NOWAIT); 5506 if (SCTP_BUF_NEXT(mm)) { 5507 if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(mm), SCTP_SIZE32(chk_length) - chk_length, NULL)) { 5508 sctp_m_freem(mm); 5509 } else { 5510 #ifdef SCTP_MBUF_LOGGING 5511 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5512 struct mbuf *mat; 5513 5514 for (mat = SCTP_BUF_NEXT(mm); mat; mat = SCTP_BUF_NEXT(mat)) { 5515 if (SCTP_BUF_IS_EXTENDED(mat)) { 5516 sctp_log_mb(mat, SCTP_MBUF_ICOPY); 5517 } 5518 } 5519 } 5520 #endif 5521 sctp_queue_op_err(stcb, mm); 5522 } 5523 } else { 5524 sctp_m_freem(mm); 5525 } 5526 } 5527 } 5528 if ((ch->chunk_type & 0x80) == 0) { 5529 /* discard this packet */ 5530 *offset = length; 5531 return (stcb); 5532 } /* else skip this bad chunk and continue... */ 5533 break; 5534 } /* switch (ch->chunk_type) */ 5535 5536 5537 next_chunk: 5538 /* get the next chunk */ 5539 *offset += SCTP_SIZE32(chk_length); 5540 if (*offset >= length) { 5541 /* no more data left in the mbuf chain */ 5542 break; 5543 } 5544 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 5545 sizeof(struct sctp_chunkhdr), chunk_buf); 5546 if (ch == NULL) { 5547 if (locked_tcb) { 5548 SCTP_TCB_UNLOCK(locked_tcb); 5549 } 5550 *offset = length; 5551 return (NULL); 5552 } 5553 } /* while */ 5554 5555 if (asconf_cnt > 0 && stcb != NULL) { 5556 sctp_send_asconf_ack(stcb); 5557 } 5558 return (stcb); 5559 } 5560 5561 5562 #ifdef INVARIANTS 5563 #ifdef __GNUC__ 5564 __attribute__((noinline)) 5565 #endif 5566 void 5567 sctp_validate_no_locks(struct sctp_inpcb *inp) 5568 { 5569 struct sctp_tcb *lstcb; 5570 5571 LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) { 5572 if (mtx_owned(&lstcb->tcb_mtx)) { 5573 panic("Own lock on stcb at return from input"); 5574 } 5575 } 5576 if (mtx_owned(&inp->inp_create_mtx)) { 5577 panic("Own create lock on inp"); 5578 } 5579 if (mtx_owned(&inp->inp_mtx)) { 5580 panic("Own inp lock on inp"); 5581 } 5582 } 5583 5584 #endif 5585 5586 /* 5587 * common input chunk processing (v4 and v6) 5588 */ 5589 void 5590 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length, 5591 struct sockaddr *src, struct sockaddr *dst, 5592 struct sctphdr *sh, struct sctp_chunkhdr *ch, 5593 #if !defined(SCTP_WITH_NO_CSUM) 5594 uint8_t compute_crc, 5595 #endif 5596 uint8_t ecn_bits, 5597 uint8_t use_mflowid, uint32_t mflowid, 5598 uint32_t vrf_id, uint16_t port) 5599 { 5600 uint32_t high_tsn; 5601 int fwd_tsn_seen = 0, data_processed = 0; 5602 struct mbuf *m = *mm; 5603 int un_sent; 5604 int cnt_ctrl_ready = 0; 5605 struct sctp_inpcb *inp = NULL, *inp_decr = NULL; 5606 struct sctp_tcb *stcb = NULL; 5607 struct sctp_nets *net = NULL; 5608 5609 SCTP_STAT_INCR(sctps_recvdatagrams); 5610 #ifdef SCTP_AUDITING_ENABLED 5611 sctp_audit_log(0xE0, 1); 5612 sctp_auditing(0, inp, stcb, net); 5613 #endif 5614 #if !defined(SCTP_WITH_NO_CSUM) 5615 if (compute_crc != 0) { 5616 uint32_t check, calc_check; 5617 5618 check = sh->checksum; 5619 sh->checksum = 0; 5620 calc_check = sctp_calculate_cksum(m, iphlen); 5621 sh->checksum = check; 5622 if (calc_check != check) { 5623 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 5624 calc_check, check, (void *)m, length, iphlen); 5625 stcb = sctp_findassociation_addr(m, offset, src, dst, 5626 sh, ch, &inp, &net, vrf_id); 5627 if ((net != NULL) && (port != 0)) { 5628 if (net->port == 0) { 5629 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); 5630 } 5631 net->port = port; 5632 } 5633 if ((net != NULL) && (use_mflowid != 0)) { 5634 net->flowid = mflowid; 5635 #ifdef INVARIANTS 5636 net->flowidset = 1; 5637 #endif 5638 } 5639 if ((inp != NULL) && (stcb != NULL)) { 5640 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1); 5641 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 5642 } else if ((inp != NULL) && (stcb == NULL)) { 5643 inp_decr = inp; 5644 } 5645 SCTP_STAT_INCR(sctps_badsum); 5646 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 5647 goto out; 5648 } 5649 } 5650 #endif 5651 /* Destination port of 0 is illegal, based on RFC4960. */ 5652 if (sh->dest_port == 0) { 5653 SCTP_STAT_INCR(sctps_hdrops); 5654 goto out; 5655 } 5656 stcb = sctp_findassociation_addr(m, offset, src, dst, 5657 sh, ch, &inp, &net, vrf_id); 5658 if ((net != NULL) && (port != 0)) { 5659 if (net->port == 0) { 5660 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); 5661 } 5662 net->port = port; 5663 } 5664 if ((net != NULL) && (use_mflowid != 0)) { 5665 net->flowid = mflowid; 5666 #ifdef INVARIANTS 5667 net->flowidset = 1; 5668 #endif 5669 } 5670 if (inp == NULL) { 5671 SCTP_STAT_INCR(sctps_noport); 5672 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) { 5673 goto out; 5674 } 5675 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 5676 sctp_send_shutdown_complete2(src, dst, sh, 5677 use_mflowid, mflowid, 5678 vrf_id, port); 5679 goto out; 5680 } 5681 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 5682 goto out; 5683 } 5684 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) { 5685 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) || 5686 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) && 5687 (ch->chunk_type != SCTP_INIT))) { 5688 sctp_send_abort(m, iphlen, src, dst, 5689 sh, 0, NULL, 5690 use_mflowid, mflowid, 5691 vrf_id, port); 5692 } 5693 } 5694 goto out; 5695 } else if (stcb == NULL) { 5696 inp_decr = inp; 5697 } 5698 #ifdef IPSEC 5699 /*- 5700 * I very much doubt any of the IPSEC stuff will work but I have no 5701 * idea, so I will leave it in place. 5702 */ 5703 if (inp != NULL) { 5704 switch (dst->sa_family) { 5705 #ifdef INET 5706 case AF_INET: 5707 if (ipsec4_in_reject(m, &inp->ip_inp.inp)) { 5708 IPSECSTAT_INC(ips_in_polvio); 5709 SCTP_STAT_INCR(sctps_hdrops); 5710 goto out; 5711 } 5712 break; 5713 #endif 5714 #ifdef INET6 5715 case AF_INET6: 5716 if (ipsec6_in_reject(m, &inp->ip_inp.inp)) { 5717 IPSEC6STAT_INC(ips_in_polvio); 5718 SCTP_STAT_INCR(sctps_hdrops); 5719 goto out; 5720 } 5721 break; 5722 #endif 5723 default: 5724 break; 5725 } 5726 } 5727 #endif 5728 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n", 5729 (void *)m, iphlen, offset, length, (void *)stcb); 5730 if (stcb) { 5731 /* always clear this before beginning a packet */ 5732 stcb->asoc.authenticated = 0; 5733 stcb->asoc.seen_a_sack_this_pkt = 0; 5734 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n", 5735 (void *)stcb, stcb->asoc.state); 5736 5737 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) || 5738 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { 5739 /*- 5740 * If we hit here, we had a ref count 5741 * up when the assoc was aborted and the 5742 * timer is clearing out the assoc, we should 5743 * NOT respond to any packet.. its OOTB. 5744 */ 5745 SCTP_TCB_UNLOCK(stcb); 5746 stcb = NULL; 5747 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, 5748 use_mflowid, mflowid, 5749 vrf_id, port); 5750 goto out; 5751 } 5752 } 5753 if (IS_SCTP_CONTROL(ch)) { 5754 /* process the control portion of the SCTP packet */ 5755 /* sa_ignore NO_NULL_CHK */ 5756 stcb = sctp_process_control(m, iphlen, &offset, length, 5757 src, dst, sh, ch, 5758 inp, stcb, &net, &fwd_tsn_seen, 5759 use_mflowid, mflowid, 5760 vrf_id, port); 5761 if (stcb) { 5762 /* 5763 * This covers us if the cookie-echo was there and 5764 * it changes our INP. 5765 */ 5766 inp = stcb->sctp_ep; 5767 if ((net) && (port)) { 5768 if (net->port == 0) { 5769 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); 5770 } 5771 net->port = port; 5772 } 5773 } 5774 } else { 5775 /* 5776 * no control chunks, so pre-process DATA chunks (these 5777 * checks are taken care of by control processing) 5778 */ 5779 5780 /* 5781 * if DATA only packet, and auth is required, then punt... 5782 * can't have authenticated without any AUTH (control) 5783 * chunks 5784 */ 5785 if ((stcb != NULL) && 5786 !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5787 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) { 5788 /* "silently" ignore */ 5789 SCTP_STAT_INCR(sctps_recvauthmissing); 5790 goto out; 5791 } 5792 if (stcb == NULL) { 5793 /* out of the blue DATA chunk */ 5794 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, 5795 use_mflowid, mflowid, 5796 vrf_id, port); 5797 goto out; 5798 } 5799 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 5800 /* v_tag mismatch! */ 5801 SCTP_STAT_INCR(sctps_badvtag); 5802 goto out; 5803 } 5804 } 5805 5806 if (stcb == NULL) { 5807 /* 5808 * no valid TCB for this packet, or we found it's a bad 5809 * packet while processing control, or we're done with this 5810 * packet (done or skip rest of data), so we drop it... 5811 */ 5812 goto out; 5813 } 5814 /* 5815 * DATA chunk processing 5816 */ 5817 /* plow through the data chunks while length > offset */ 5818 5819 /* 5820 * Rest should be DATA only. Check authentication state if AUTH for 5821 * DATA is required. 5822 */ 5823 if ((length > offset) && 5824 (stcb != NULL) && 5825 !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5826 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) && 5827 !stcb->asoc.authenticated) { 5828 /* "silently" ignore */ 5829 SCTP_STAT_INCR(sctps_recvauthmissing); 5830 SCTPDBG(SCTP_DEBUG_AUTH1, 5831 "Data chunk requires AUTH, skipped\n"); 5832 goto trigger_send; 5833 } 5834 if (length > offset) { 5835 int retval; 5836 5837 /* 5838 * First check to make sure our state is correct. We would 5839 * not get here unless we really did have a tag, so we don't 5840 * abort if this happens, just dump the chunk silently. 5841 */ 5842 switch (SCTP_GET_STATE(&stcb->asoc)) { 5843 case SCTP_STATE_COOKIE_ECHOED: 5844 /* 5845 * we consider data with valid tags in this state 5846 * shows us the cookie-ack was lost. Imply it was 5847 * there. 5848 */ 5849 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5850 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5851 stcb->asoc.overall_error_count, 5852 0, 5853 SCTP_FROM_SCTP_INPUT, 5854 __LINE__); 5855 } 5856 stcb->asoc.overall_error_count = 0; 5857 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 5858 break; 5859 case SCTP_STATE_COOKIE_WAIT: 5860 /* 5861 * We consider OOTB any data sent during asoc setup. 5862 */ 5863 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, 5864 use_mflowid, mflowid, 5865 vrf_id, port); 5866 goto out; 5867 /* sa_ignore NOTREACHED */ 5868 break; 5869 case SCTP_STATE_EMPTY: /* should not happen */ 5870 case SCTP_STATE_INUSE: /* should not happen */ 5871 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 5872 case SCTP_STATE_SHUTDOWN_ACK_SENT: 5873 default: 5874 goto out; 5875 /* sa_ignore NOTREACHED */ 5876 break; 5877 case SCTP_STATE_OPEN: 5878 case SCTP_STATE_SHUTDOWN_SENT: 5879 break; 5880 } 5881 /* plow through the data chunks while length > offset */ 5882 retval = sctp_process_data(mm, iphlen, &offset, length, 5883 src, dst, sh, 5884 inp, stcb, net, &high_tsn, 5885 use_mflowid, mflowid, 5886 vrf_id, port); 5887 if (retval == 2) { 5888 /* 5889 * The association aborted, NO UNLOCK needed since 5890 * the association is destroyed. 5891 */ 5892 stcb = NULL; 5893 goto out; 5894 } 5895 data_processed = 1; 5896 /* 5897 * Anything important needs to have been m_copy'ed in 5898 * process_data 5899 */ 5900 } 5901 /* take care of ecn */ 5902 if ((data_processed == 1) && 5903 (stcb->asoc.ecn_allowed == 1) && 5904 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) { 5905 /* Yep, we need to add a ECNE */ 5906 sctp_send_ecn_echo(stcb, net, high_tsn); 5907 } 5908 if ((data_processed == 0) && (fwd_tsn_seen)) { 5909 int was_a_gap; 5910 uint32_t highest_tsn; 5911 5912 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) { 5913 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map; 5914 } else { 5915 highest_tsn = stcb->asoc.highest_tsn_inside_map; 5916 } 5917 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn); 5918 stcb->asoc.send_sack = 1; 5919 sctp_sack_check(stcb, was_a_gap); 5920 } else if (fwd_tsn_seen) { 5921 stcb->asoc.send_sack = 1; 5922 } 5923 /* trigger send of any chunks in queue... */ 5924 trigger_send: 5925 #ifdef SCTP_AUDITING_ENABLED 5926 sctp_audit_log(0xE0, 2); 5927 sctp_auditing(1, inp, stcb, net); 5928 #endif 5929 SCTPDBG(SCTP_DEBUG_INPUT1, 5930 "Check for chunk output prw:%d tqe:%d tf=%d\n", 5931 stcb->asoc.peers_rwnd, 5932 TAILQ_EMPTY(&stcb->asoc.control_send_queue), 5933 stcb->asoc.total_flight); 5934 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 5935 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 5936 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq; 5937 } 5938 if (cnt_ctrl_ready || 5939 ((un_sent) && 5940 (stcb->asoc.peers_rwnd > 0 || 5941 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 5942 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 5943 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 5944 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 5945 } 5946 #ifdef SCTP_AUDITING_ENABLED 5947 sctp_audit_log(0xE0, 3); 5948 sctp_auditing(2, inp, stcb, net); 5949 #endif 5950 out: 5951 if (stcb != NULL) { 5952 SCTP_TCB_UNLOCK(stcb); 5953 } 5954 if (inp_decr != NULL) { 5955 /* reduce ref-count */ 5956 SCTP_INP_WLOCK(inp_decr); 5957 SCTP_INP_DECR_REF(inp_decr); 5958 SCTP_INP_WUNLOCK(inp_decr); 5959 } 5960 #ifdef INVARIANTS 5961 if (inp != NULL) { 5962 sctp_validate_no_locks(inp); 5963 } 5964 #endif 5965 return; 5966 } 5967 5968 #if 0 5969 static void 5970 sctp_print_mbuf_chain(struct mbuf *m) 5971 { 5972 for (; m; m = SCTP_BUF_NEXT(m)) { 5973 SCTP_PRINTF("%p: m_len = %ld\n", (void *)m, SCTP_BUF_LEN(m)); 5974 if (SCTP_BUF_IS_EXTENDED(m)) 5975 SCTP_PRINTF("%p: extend_size = %d\n", (void *)m, SCTP_BUF_EXTEND_SIZE(m)); 5976 } 5977 } 5978 5979 #endif 5980 5981 #ifdef INET 5982 void 5983 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port) 5984 { 5985 struct mbuf *m; 5986 int iphlen; 5987 uint32_t vrf_id = 0; 5988 uint8_t ecn_bits; 5989 struct sockaddr_in src, dst; 5990 struct ip *ip; 5991 struct sctphdr *sh; 5992 struct sctp_chunkhdr *ch; 5993 int length, offset; 5994 5995 #if !defined(SCTP_WITH_NO_CSUM) 5996 uint8_t compute_crc; 5997 5998 #endif 5999 uint32_t mflowid; 6000 uint8_t use_mflowid; 6001 6002 iphlen = off; 6003 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 6004 SCTP_RELEASE_PKT(i_pak); 6005 return; 6006 } 6007 m = SCTP_HEADER_TO_CHAIN(i_pak); 6008 #ifdef SCTP_MBUF_LOGGING 6009 /* Log in any input mbufs */ 6010 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 6011 struct mbuf *mat; 6012 6013 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) { 6014 if (SCTP_BUF_IS_EXTENDED(mat)) { 6015 sctp_log_mb(mat, SCTP_MBUF_INPUT); 6016 } 6017 } 6018 } 6019 #endif 6020 #ifdef SCTP_PACKET_LOGGING 6021 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) { 6022 sctp_packet_log(m); 6023 } 6024 #endif 6025 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, 6026 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n", 6027 m->m_pkthdr.len, 6028 if_name(m->m_pkthdr.rcvif), 6029 (int)m->m_pkthdr.csum_flags, CSUM_BITS); 6030 if (m->m_flags & M_FLOWID) { 6031 mflowid = m->m_pkthdr.flowid; 6032 use_mflowid = 1; 6033 } else { 6034 mflowid = 0; 6035 use_mflowid = 0; 6036 } 6037 SCTP_STAT_INCR(sctps_recvpackets); 6038 SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 6039 /* Get IP, SCTP, and first chunk header together in the first mbuf. */ 6040 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); 6041 if (SCTP_BUF_LEN(m) < offset) { 6042 if ((m = m_pullup(m, offset)) == NULL) { 6043 SCTP_STAT_INCR(sctps_hdrops); 6044 return; 6045 } 6046 } 6047 ip = mtod(m, struct ip *); 6048 sh = (struct sctphdr *)((caddr_t)ip + iphlen); 6049 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr)); 6050 offset -= sizeof(struct sctp_chunkhdr); 6051 memset(&src, 0, sizeof(struct sockaddr_in)); 6052 src.sin_family = AF_INET; 6053 src.sin_len = sizeof(struct sockaddr_in); 6054 src.sin_port = sh->src_port; 6055 src.sin_addr = ip->ip_src; 6056 memset(&dst, 0, sizeof(struct sockaddr_in)); 6057 dst.sin_family = AF_INET; 6058 dst.sin_len = sizeof(struct sockaddr_in); 6059 dst.sin_port = sh->dest_port; 6060 dst.sin_addr = ip->ip_dst; 6061 length = ntohs(ip->ip_len); 6062 /* Validate mbuf chain length with IP payload length. */ 6063 if (SCTP_HEADER_LEN(m) != length) { 6064 SCTPDBG(SCTP_DEBUG_INPUT1, 6065 "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m)); 6066 SCTP_STAT_INCR(sctps_hdrops); 6067 goto out; 6068 } 6069 /* SCTP does not allow broadcasts or multicasts */ 6070 if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) { 6071 goto out; 6072 } 6073 if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) { 6074 goto out; 6075 } 6076 ecn_bits = ip->ip_tos; 6077 #if defined(SCTP_WITH_NO_CSUM) 6078 SCTP_STAT_INCR(sctps_recvnocrc); 6079 #else 6080 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { 6081 SCTP_STAT_INCR(sctps_recvhwcrc); 6082 compute_crc = 0; 6083 } else { 6084 SCTP_STAT_INCR(sctps_recvswcrc); 6085 compute_crc = 1; 6086 } 6087 #endif 6088 sctp_common_input_processing(&m, iphlen, offset, length, 6089 (struct sockaddr *)&src, 6090 (struct sockaddr *)&dst, 6091 sh, ch, 6092 #if !defined(SCTP_WITH_NO_CSUM) 6093 compute_crc, 6094 #endif 6095 ecn_bits, 6096 use_mflowid, mflowid, 6097 vrf_id, port); 6098 out: 6099 if (m) { 6100 sctp_m_freem(m); 6101 } 6102 return; 6103 } 6104 6105 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) 6106 extern int *sctp_cpuarry; 6107 6108 #endif 6109 6110 void 6111 sctp_input(struct mbuf *m, int off) 6112 { 6113 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) 6114 struct ip *ip; 6115 struct sctphdr *sh; 6116 int offset; 6117 int cpu_to_use; 6118 uint32_t flowid, tag; 6119 6120 if (mp_ncpus > 1) { 6121 if (m->m_flags & M_FLOWID) { 6122 flowid = m->m_pkthdr.flowid; 6123 } else { 6124 /* 6125 * No flow id built by lower layers fix it so we 6126 * create one. 6127 */ 6128 offset = off + sizeof(struct sctphdr); 6129 if (SCTP_BUF_LEN(m) < offset) { 6130 if ((m = m_pullup(m, offset)) == NULL) { 6131 SCTP_STAT_INCR(sctps_hdrops); 6132 return; 6133 } 6134 } 6135 ip = mtod(m, struct ip *); 6136 sh = (struct sctphdr *)((caddr_t)ip + off); 6137 tag = htonl(sh->v_tag); 6138 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port); 6139 m->m_pkthdr.flowid = flowid; 6140 m->m_flags |= M_FLOWID; 6141 } 6142 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus]; 6143 sctp_queue_to_mcore(m, off, cpu_to_use); 6144 return; 6145 } 6146 #endif 6147 sctp_input_with_port(m, off, 0); 6148 } 6149 6150 #endif 6151