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