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