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