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