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