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