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 92 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n", 93 stcb); 94 if (stcb == NULL) { 95 SCTP_INP_RLOCK(inp); 96 } 97 /* validate length */ 98 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) { 99 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 100 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 101 vrf_id, port); 102 if (stcb) 103 *abort_no_unlock = 1; 104 goto outnow; 105 } 106 /* validate parameters */ 107 init = &cp->init; 108 if (init->initiate_tag == 0) { 109 /* protocol error... send abort */ 110 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 111 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 112 vrf_id, port); 113 if (stcb) 114 *abort_no_unlock = 1; 115 goto outnow; 116 } 117 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) { 118 /* invalid parameter... send abort */ 119 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 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 (init->num_inbound_streams == 0) { 127 /* protocol error... send abort */ 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 if (init->num_outbound_streams == 0) { 136 /* protocol error... send abort */ 137 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 138 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 139 vrf_id, port); 140 if (stcb) 141 *abort_no_unlock = 1; 142 goto outnow; 143 } 144 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp), 145 offset + ntohs(cp->ch.chunk_length))) { 146 /* auth parameter(s) error... send abort */ 147 sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id, port); 148 if (stcb) 149 *abort_no_unlock = 1; 150 goto outnow; 151 } 152 /* 153 * We are only accepting if we have a socket with positive 154 * so_qlimit. 155 */ 156 if ((stcb == NULL) && 157 ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 158 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 159 (inp->sctp_socket == NULL) || 160 (inp->sctp_socket->so_qlimit == 0))) { 161 /* 162 * FIX ME ?? What about TCP model and we have a 163 * match/restart case? Actually no fix is needed. the lookup 164 * will always find the existing assoc so stcb would not be 165 * NULL. It may be questionable to do this since we COULD 166 * just send back the INIT-ACK and hope that the app did 167 * accept()'s by the time the COOKIE was sent. But there is 168 * a price to pay for COOKIE generation and I don't want to 169 * pay it on the chance that the app will actually do some 170 * accepts(). The App just looses and should NOT be in this 171 * state :-) 172 */ 173 if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) { 174 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port); 175 } 176 goto outnow; 177 } 178 if ((stcb != NULL) && 179 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) { 180 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n"); 181 sctp_send_shutdown_ack(stcb, NULL); 182 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 183 } else { 184 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n"); 185 sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id, port, 186 ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED)); 187 } 188 outnow: 189 if (stcb == NULL) { 190 SCTP_INP_RUNLOCK(inp); 191 } 192 } 193 194 /* 195 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error 196 */ 197 198 int 199 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked 200 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 201 SCTP_UNUSED 202 #endif 203 ) 204 { 205 int unsent_data = 0; 206 unsigned int i; 207 struct sctp_stream_queue_pending *sp; 208 struct sctp_association *asoc; 209 210 /* 211 * This function returns the number of streams that have true unsent 212 * data on them. Note that as it looks through it will clean up any 213 * places that have old data that has been sent but left at top of 214 * stream queue. 215 */ 216 asoc = &stcb->asoc; 217 SCTP_TCB_SEND_LOCK(stcb); 218 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 219 /* Check to see if some data queued */ 220 for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 221 /* sa_ignore FREED_MEMORY */ 222 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue); 223 if (sp == NULL) { 224 continue; 225 } 226 if ((sp->msg_is_complete) && 227 (sp->length == 0) && 228 (sp->sender_all_done)) { 229 /* 230 * We are doing differed cleanup. Last time 231 * through when we took all the data the 232 * sender_all_done was not set. 233 */ 234 if (sp->put_last_out == 0) { 235 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n"); 236 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n", 237 sp->sender_all_done, 238 sp->length, 239 sp->msg_is_complete, 240 sp->put_last_out); 241 } 242 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1); 243 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next); 244 if (sp->net) { 245 sctp_free_remote_addr(sp->net); 246 sp->net = NULL; 247 } 248 if (sp->data) { 249 sctp_m_freem(sp->data); 250 sp->data = NULL; 251 } 252 sctp_free_a_strmoq(stcb, sp, so_locked); 253 } else { 254 unsent_data++; 255 break; 256 } 257 } 258 } 259 SCTP_TCB_SEND_UNLOCK(stcb); 260 return (unsent_data); 261 } 262 263 static int 264 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb) 265 { 266 struct sctp_init *init; 267 struct sctp_association *asoc; 268 struct sctp_nets *lnet; 269 unsigned int i; 270 271 init = &cp->init; 272 asoc = &stcb->asoc; 273 /* save off parameters */ 274 asoc->peer_vtag = ntohl(init->initiate_tag); 275 asoc->peers_rwnd = ntohl(init->a_rwnd); 276 /* init tsn's */ 277 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1; 278 279 if (!TAILQ_EMPTY(&asoc->nets)) { 280 /* update any ssthresh's that may have a default */ 281 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { 282 lnet->ssthresh = asoc->peers_rwnd; 283 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) { 284 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION); 285 } 286 } 287 } 288 SCTP_TCB_SEND_LOCK(stcb); 289 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) { 290 unsigned int newcnt; 291 struct sctp_stream_out *outs; 292 struct sctp_stream_queue_pending *sp, *nsp; 293 struct sctp_tmit_chunk *chk, *nchk; 294 295 /* abandon the upper streams */ 296 newcnt = ntohs(init->num_inbound_streams); 297 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { 298 if (chk->rec.data.stream_number >= newcnt) { 299 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); 300 asoc->send_queue_cnt--; 301 if (chk->data != NULL) { 302 sctp_free_bufspace(stcb, asoc, chk, 1); 303 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, 304 SCTP_NOTIFY_DATAGRAM_UNSENT, chk, SCTP_SO_NOT_LOCKED); 305 if (chk->data) { 306 sctp_m_freem(chk->data); 307 chk->data = NULL; 308 } 309 } 310 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 311 /* sa_ignore FREED_MEMORY */ 312 } 313 } 314 if (asoc->strmout) { 315 for (i = newcnt; i < asoc->pre_open_streams; i++) { 316 outs = &asoc->strmout[i]; 317 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) { 318 TAILQ_REMOVE(&outs->outqueue, sp, next); 319 asoc->stream_queue_cnt--; 320 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, 321 stcb, SCTP_NOTIFY_DATAGRAM_UNSENT, 322 sp, SCTP_SO_NOT_LOCKED); 323 if (sp->data) { 324 sctp_m_freem(sp->data); 325 sp->data = NULL; 326 } 327 if (sp->net) { 328 sctp_free_remote_addr(sp->net); 329 sp->net = NULL; 330 } 331 /* Free the chunk */ 332 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED); 333 /* sa_ignore FREED_MEMORY */ 334 } 335 } 336 } 337 /* cut back the count */ 338 asoc->pre_open_streams = newcnt; 339 } 340 SCTP_TCB_SEND_UNLOCK(stcb); 341 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams; 342 343 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */ 344 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map; 345 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 346 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 347 } 348 /* This is the next one we expect */ 349 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1; 350 351 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn); 352 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in; 353 354 asoc->advanced_peer_ack_point = asoc->last_acked_seq; 355 /* open the requested streams */ 356 357 if (asoc->strmin != NULL) { 358 /* Free the old ones */ 359 struct sctp_queued_to_read *ctl, *nctl; 360 361 for (i = 0; i < asoc->streamincnt; i++) { 362 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) { 363 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); 364 sctp_free_remote_addr(ctl->whoFrom); 365 ctl->whoFrom = NULL; 366 sctp_m_freem(ctl->data); 367 ctl->data = NULL; 368 sctp_free_a_readq(stcb, ctl); 369 } 370 } 371 SCTP_FREE(asoc->strmin, SCTP_M_STRMI); 372 } 373 asoc->streamincnt = ntohs(init->num_outbound_streams); 374 if (asoc->streamincnt > MAX_SCTP_STREAMS) { 375 asoc->streamincnt = MAX_SCTP_STREAMS; 376 } 377 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt * 378 sizeof(struct sctp_stream_in), SCTP_M_STRMI); 379 if (asoc->strmin == NULL) { 380 /* we didn't get memory for the streams! */ 381 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n"); 382 return (-1); 383 } 384 for (i = 0; i < asoc->streamincnt; i++) { 385 asoc->strmin[i].stream_no = i; 386 asoc->strmin[i].last_sequence_delivered = 0xffff; 387 /* 388 * U-stream ranges will be set when the cookie is unpacked. 389 * Or for the INIT sender they are un set (if pr-sctp not 390 * supported) when the INIT-ACK arrives. 391 */ 392 TAILQ_INIT(&asoc->strmin[i].inqueue); 393 asoc->strmin[i].delivery_started = 0; 394 } 395 /* 396 * load_address_from_init will put the addresses into the 397 * association when the COOKIE is processed or the INIT-ACK is 398 * processed. Both types of COOKIE's existing and new call this 399 * routine. It will remove addresses that are no longer in the 400 * association (for the restarting case where addresses are 401 * removed). Up front when the INIT arrives we will discard it if it 402 * is a restart and new addresses have been added. 403 */ 404 /* sa_ignore MEMLEAK */ 405 return (0); 406 } 407 408 /* 409 * INIT-ACK message processing/consumption returns value < 0 on error 410 */ 411 static int 412 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset, 413 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 414 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 415 { 416 struct sctp_association *asoc; 417 struct mbuf *op_err; 418 int retval, abort_flag; 419 uint32_t initack_limit; 420 int nat_friendly = 0; 421 422 /* First verify that we have no illegal param's */ 423 abort_flag = 0; 424 op_err = NULL; 425 426 op_err = sctp_arethere_unrecognized_parameters(m, 427 (offset + sizeof(struct sctp_init_chunk)), 428 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly); 429 if (abort_flag) { 430 /* Send an abort and notify peer */ 431 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_CAUSE_PROTOCOL_VIOLATION, op_err, SCTP_SO_NOT_LOCKED); 432 *abort_no_unlock = 1; 433 return (-1); 434 } 435 asoc = &stcb->asoc; 436 asoc->peer_supports_nat = (uint8_t) nat_friendly; 437 /* process the peer's parameters in the INIT-ACK */ 438 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb); 439 if (retval < 0) { 440 return (retval); 441 } 442 initack_limit = offset + ntohs(cp->ch.chunk_length); 443 /* load all addresses */ 444 if ((retval = sctp_load_addresses_from_init(stcb, m, 445 (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh, 446 NULL))) { 447 /* Huh, we should abort */ 448 SCTPDBG(SCTP_DEBUG_INPUT1, 449 "Load addresses from INIT causes an abort %d\n", 450 retval); 451 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 452 NULL, 0, net->port); 453 *abort_no_unlock = 1; 454 return (-1); 455 } 456 /* if the peer doesn't support asconf, flush the asconf queue */ 457 if (asoc->peer_supports_asconf == 0) { 458 struct sctp_asconf_addr *param, *nparam; 459 460 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) { 461 TAILQ_REMOVE(&asoc->asconf_queue, param, next); 462 SCTP_FREE(param, SCTP_M_ASC_ADDR); 463 } 464 } 465 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 466 stcb->asoc.local_hmacs); 467 if (op_err) { 468 sctp_queue_op_err(stcb, op_err); 469 /* queuing will steal away the mbuf chain to the out queue */ 470 op_err = NULL; 471 } 472 /* extract the cookie and queue it to "echo" it back... */ 473 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 474 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 475 stcb->asoc.overall_error_count, 476 0, 477 SCTP_FROM_SCTP_INPUT, 478 __LINE__); 479 } 480 stcb->asoc.overall_error_count = 0; 481 net->error_count = 0; 482 483 /* 484 * Cancel the INIT timer, We do this first before queueing the 485 * cookie. We always cancel at the primary to assue that we are 486 * canceling the timer started by the INIT which always goes to the 487 * primary. 488 */ 489 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, 490 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4); 491 492 /* calculate the RTO */ 493 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy, 494 SCTP_RTT_FROM_NON_DATA); 495 496 retval = sctp_send_cookie_echo(m, offset, stcb, net); 497 if (retval < 0) { 498 /* 499 * No cookie, we probably should send a op error. But in any 500 * case if there is no cookie in the INIT-ACK, we can 501 * abandon the peer, its broke. 502 */ 503 if (retval == -3) { 504 /* We abort with an error of missing mandatory param */ 505 op_err = 506 sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM); 507 if (op_err) { 508 /* 509 * Expand beyond to include the mandatory 510 * param cookie 511 */ 512 struct sctp_inv_mandatory_param *mp; 513 514 SCTP_BUF_LEN(op_err) = 515 sizeof(struct sctp_inv_mandatory_param); 516 mp = mtod(op_err, 517 struct sctp_inv_mandatory_param *); 518 /* Subtract the reserved param */ 519 mp->length = 520 htons(sizeof(struct sctp_inv_mandatory_param) - 2); 521 mp->num_param = htonl(1); 522 mp->param = htons(SCTP_STATE_COOKIE); 523 mp->resv = 0; 524 } 525 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 526 sh, op_err, vrf_id, net->port); 527 *abort_no_unlock = 1; 528 } 529 return (retval); 530 } 531 return (0); 532 } 533 534 static void 535 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, 536 struct sctp_tcb *stcb, struct sctp_nets *net) 537 { 538 struct sockaddr_storage store; 539 struct sctp_nets *r_net, *f_net; 540 struct timeval tv; 541 int req_prim = 0; 542 uint16_t old_error_counter; 543 544 #ifdef INET 545 struct sockaddr_in *sin; 546 547 #endif 548 #ifdef INET6 549 struct sockaddr_in6 *sin6; 550 551 #endif 552 553 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { 554 /* Invalid length */ 555 return; 556 } 557 memset(&store, 0, sizeof(store)); 558 switch (cp->heartbeat.hb_info.addr_family) { 559 #ifdef INET 560 case AF_INET: 561 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { 562 sin = (struct sockaddr_in *)&store; 563 sin->sin_family = cp->heartbeat.hb_info.addr_family; 564 sin->sin_len = cp->heartbeat.hb_info.addr_len; 565 sin->sin_port = stcb->rport; 566 memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address, 567 sizeof(sin->sin_addr)); 568 } else { 569 return; 570 } 571 break; 572 #endif 573 #ifdef INET6 574 case AF_INET6: 575 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { 576 sin6 = (struct sockaddr_in6 *)&store; 577 sin6->sin6_family = cp->heartbeat.hb_info.addr_family; 578 sin6->sin6_len = cp->heartbeat.hb_info.addr_len; 579 sin6->sin6_port = stcb->rport; 580 memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address, 581 sizeof(sin6->sin6_addr)); 582 } else { 583 return; 584 } 585 break; 586 #endif 587 default: 588 return; 589 } 590 r_net = sctp_findnet(stcb, (struct sockaddr *)&store); 591 if (r_net == NULL) { 592 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n"); 593 return; 594 } 595 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && 596 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && 597 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { 598 /* 599 * If the its a HB and it's random value is correct when can 600 * confirm the destination. 601 */ 602 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 603 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { 604 stcb->asoc.primary_destination = r_net; 605 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 606 f_net = TAILQ_FIRST(&stcb->asoc.nets); 607 if (f_net != r_net) { 608 /* 609 * first one on the list is NOT the primary 610 * sctp_cmpaddr() is much more efficent if 611 * the primary is the first on the list, 612 * make it so. 613 */ 614 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next); 615 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next); 616 } 617 req_prim = 1; 618 } 619 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 620 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED); 621 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3); 622 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net); 623 } 624 old_error_counter = r_net->error_count; 625 r_net->error_count = 0; 626 r_net->hb_responded = 1; 627 tv.tv_sec = cp->heartbeat.hb_info.time_value_1; 628 tv.tv_usec = cp->heartbeat.hb_info.time_value_2; 629 /* Now lets do a RTO with this */ 630 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy, 631 SCTP_RTT_FROM_NON_DATA); 632 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) { 633 r_net->dest_state |= SCTP_ADDR_REACHABLE; 634 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 635 SCTP_HEARTBEAT_SUCCESS, (void *)r_net, SCTP_SO_NOT_LOCKED); 636 } 637 if (r_net->dest_state & SCTP_ADDR_PF) { 638 r_net->dest_state &= ~SCTP_ADDR_PF; 639 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net); 640 } 641 if (old_error_counter > 0) { 642 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3); 643 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net); 644 } 645 if (r_net == stcb->asoc.primary_destination) { 646 if (stcb->asoc.alternate) { 647 /* release the alternate, primary is good */ 648 sctp_free_remote_addr(stcb->asoc.alternate); 649 stcb->asoc.alternate = NULL; 650 } 651 } 652 /* Mobility adaptation */ 653 if (req_prim) { 654 if ((sctp_is_mobility_feature_on(stcb->sctp_ep, 655 SCTP_MOBILITY_BASE) || 656 sctp_is_mobility_feature_on(stcb->sctp_ep, 657 SCTP_MOBILITY_FASTHANDOFF)) && 658 sctp_is_mobility_feature_on(stcb->sctp_ep, 659 SCTP_MOBILITY_PRIM_DELETED)) { 660 661 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7); 662 if (sctp_is_mobility_feature_on(stcb->sctp_ep, 663 SCTP_MOBILITY_FASTHANDOFF)) { 664 sctp_assoc_immediate_retrans(stcb, 665 stcb->asoc.primary_destination); 666 } 667 if (sctp_is_mobility_feature_on(stcb->sctp_ep, 668 SCTP_MOBILITY_BASE)) { 669 sctp_move_chunks_from_net(stcb, 670 stcb->asoc.deleted_primary); 671 } 672 sctp_delete_prim_timer(stcb->sctp_ep, stcb, 673 stcb->asoc.deleted_primary); 674 } 675 } 676 } 677 678 static int 679 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb) 680 { 681 /* 682 * return 0 means we want you to proceed with the abort non-zero 683 * means no abort processing 684 */ 685 struct sctpasochead *head; 686 687 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) { 688 /* generate a new vtag and send init */ 689 LIST_REMOVE(stcb, sctp_asocs); 690 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 691 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 692 /* 693 * put it in the bucket in the vtag hash of assoc's for the 694 * system 695 */ 696 LIST_INSERT_HEAD(head, stcb, sctp_asocs); 697 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 698 return (1); 699 } 700 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) { 701 /* 702 * treat like a case where the cookie expired i.e.: - dump 703 * current cookie. - generate a new vtag. - resend init. 704 */ 705 /* generate a new vtag and send init */ 706 LIST_REMOVE(stcb, sctp_asocs); 707 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED; 708 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT; 709 sctp_stop_all_cookie_timers(stcb); 710 sctp_toss_old_cookies(stcb, &stcb->asoc); 711 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 712 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 713 /* 714 * put it in the bucket in the vtag hash of assoc's for the 715 * system 716 */ 717 LIST_INSERT_HEAD(head, stcb, sctp_asocs); 718 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 719 return (1); 720 } 721 return (0); 722 } 723 724 static int 725 sctp_handle_nat_missing_state(struct sctp_tcb *stcb, 726 struct sctp_nets *net) 727 { 728 /* 729 * return 0 means we want you to proceed with the abort non-zero 730 * means no abort processing 731 */ 732 if (stcb->asoc.peer_supports_auth == 0) { 733 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n"); 734 return (0); 735 } 736 sctp_asconf_send_nat_state_update(stcb, net); 737 return (1); 738 } 739 740 741 static void 742 sctp_handle_abort(struct sctp_abort_chunk *cp, 743 struct sctp_tcb *stcb, struct sctp_nets *net) 744 { 745 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 746 struct socket *so; 747 748 #endif 749 uint16_t len; 750 751 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n"); 752 if (stcb == NULL) 753 return; 754 755 len = ntohs(cp->ch.chunk_length); 756 if (len > sizeof(struct sctp_chunkhdr)) { 757 /* 758 * Need to check the cause codes for our two magic nat 759 * aborts which don't kill the assoc necessarily. 760 */ 761 struct sctp_abort_chunk *cpnext; 762 struct sctp_missing_nat_state *natc; 763 uint16_t cause; 764 765 cpnext = cp; 766 cpnext++; 767 natc = (struct sctp_missing_nat_state *)cpnext; 768 cause = ntohs(natc->cause); 769 if (cause == SCTP_CAUSE_NAT_COLLIDING_STATE) { 770 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", 771 cp->ch.chunk_flags); 772 if (sctp_handle_nat_colliding_state(stcb)) { 773 return; 774 } 775 } else if (cause == SCTP_CAUSE_NAT_MISSING_STATE) { 776 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", 777 cp->ch.chunk_flags); 778 if (sctp_handle_nat_missing_state(stcb, net)) { 779 return; 780 } 781 } 782 } 783 /* stop any receive timers */ 784 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 785 /* notify user of the abort and clean up... */ 786 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 787 /* free the tcb */ 788 #if defined(SCTP_PANIC_ON_ABORT) 789 printf("stcb:%p state:%d rport:%d net:%p\n", 790 stcb, stcb->asoc.state, stcb->rport, net); 791 if (!(stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) { 792 panic("Received an ABORT"); 793 } else { 794 printf("No panic its in state %x closed\n", stcb->asoc.state); 795 } 796 #endif 797 SCTP_STAT_INCR_COUNTER32(sctps_aborted); 798 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 799 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 800 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 801 } 802 #ifdef SCTP_ASOCLOG_OF_TSNS 803 sctp_print_out_track_log(stcb); 804 #endif 805 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 806 so = SCTP_INP_SO(stcb->sctp_ep); 807 atomic_add_int(&stcb->asoc.refcnt, 1); 808 SCTP_TCB_UNLOCK(stcb); 809 SCTP_SOCKET_LOCK(so, 1); 810 SCTP_TCB_LOCK(stcb); 811 atomic_subtract_int(&stcb->asoc.refcnt, 1); 812 #endif 813 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED; 814 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 815 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 816 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 817 SCTP_SOCKET_UNLOCK(so, 1); 818 #endif 819 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n"); 820 } 821 822 static void 823 sctp_start_net_timers(struct sctp_tcb *stcb) 824 { 825 uint32_t cnt_hb_sent; 826 struct sctp_nets *net; 827 828 cnt_hb_sent = 0; 829 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 830 /* 831 * For each network start: 1) A pmtu timer. 2) A HB timer 3) 832 * If the dest in unconfirmed send a hb as well if under 833 * max_hb_burst have been sent. 834 */ 835 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net); 836 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net); 837 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) && 838 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) { 839 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED); 840 cnt_hb_sent++; 841 } 842 } 843 if (cnt_hb_sent) { 844 sctp_chunk_output(stcb->sctp_ep, stcb, 845 SCTP_OUTPUT_FROM_COOKIE_ACK, 846 SCTP_SO_NOT_LOCKED); 847 } 848 } 849 850 851 static void 852 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp, 853 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) 854 { 855 struct sctp_association *asoc; 856 int some_on_streamwheel; 857 858 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 859 struct socket *so; 860 861 #endif 862 863 SCTPDBG(SCTP_DEBUG_INPUT2, 864 "sctp_handle_shutdown: handling SHUTDOWN\n"); 865 if (stcb == NULL) 866 return; 867 asoc = &stcb->asoc; 868 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 869 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 870 return; 871 } 872 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { 873 /* Shutdown NOT the expected size */ 874 return; 875 } else { 876 sctp_update_acked(stcb, cp, abort_flag); 877 if (*abort_flag) { 878 return; 879 } 880 } 881 if (asoc->control_pdapi) { 882 /* 883 * With a normal shutdown we assume the end of last record. 884 */ 885 SCTP_INP_READ_LOCK(stcb->sctp_ep); 886 asoc->control_pdapi->end_added = 1; 887 asoc->control_pdapi->pdapi_aborted = 1; 888 asoc->control_pdapi = NULL; 889 SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 890 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 891 so = SCTP_INP_SO(stcb->sctp_ep); 892 atomic_add_int(&stcb->asoc.refcnt, 1); 893 SCTP_TCB_UNLOCK(stcb); 894 SCTP_SOCKET_LOCK(so, 1); 895 SCTP_TCB_LOCK(stcb); 896 atomic_subtract_int(&stcb->asoc.refcnt, 1); 897 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 898 /* assoc was freed while we were unlocked */ 899 SCTP_SOCKET_UNLOCK(so, 1); 900 return; 901 } 902 #endif 903 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 904 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 905 SCTP_SOCKET_UNLOCK(so, 1); 906 #endif 907 } 908 /* goto SHUTDOWN_RECEIVED state to block new requests */ 909 if (stcb->sctp_socket) { 910 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 911 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) && 912 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 913 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED); 914 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 915 /* 916 * notify upper layer that peer has initiated a 917 * shutdown 918 */ 919 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 920 921 /* reset time */ 922 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 923 } 924 } 925 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 926 /* 927 * stop the shutdown timer, since we WILL move to 928 * SHUTDOWN-ACK-SENT. 929 */ 930 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8); 931 } 932 /* Now is there unsent data on a stream somewhere? */ 933 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED); 934 935 if (!TAILQ_EMPTY(&asoc->send_queue) || 936 !TAILQ_EMPTY(&asoc->sent_queue) || 937 some_on_streamwheel) { 938 /* By returning we will push more data out */ 939 return; 940 } else { 941 /* no outstanding data to send, so move on... */ 942 /* send SHUTDOWN-ACK */ 943 sctp_send_shutdown_ack(stcb, net); 944 /* move to SHUTDOWN-ACK-SENT state */ 945 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 946 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 947 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 948 } 949 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT); 950 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 951 sctp_stop_timers_for_shutdown(stcb); 952 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, 953 stcb, net); 954 } 955 } 956 957 static void 958 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED, 959 struct sctp_tcb *stcb, 960 struct sctp_nets *net) 961 { 962 struct sctp_association *asoc; 963 964 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 965 struct socket *so; 966 967 so = SCTP_INP_SO(stcb->sctp_ep); 968 #endif 969 SCTPDBG(SCTP_DEBUG_INPUT2, 970 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); 971 if (stcb == NULL) 972 return; 973 974 asoc = &stcb->asoc; 975 /* process according to association state */ 976 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 977 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 978 /* unexpected SHUTDOWN-ACK... do OOTB handling... */ 979 sctp_send_shutdown_complete(stcb, net, 1); 980 SCTP_TCB_UNLOCK(stcb); 981 return; 982 } 983 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 984 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 985 /* unexpected SHUTDOWN-ACK... so ignore... */ 986 SCTP_TCB_UNLOCK(stcb); 987 return; 988 } 989 if (asoc->control_pdapi) { 990 /* 991 * With a normal shutdown we assume the end of last record. 992 */ 993 SCTP_INP_READ_LOCK(stcb->sctp_ep); 994 asoc->control_pdapi->end_added = 1; 995 asoc->control_pdapi->pdapi_aborted = 1; 996 asoc->control_pdapi = NULL; 997 SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 998 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 999 atomic_add_int(&stcb->asoc.refcnt, 1); 1000 SCTP_TCB_UNLOCK(stcb); 1001 SCTP_SOCKET_LOCK(so, 1); 1002 SCTP_TCB_LOCK(stcb); 1003 atomic_subtract_int(&stcb->asoc.refcnt, 1); 1004 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1005 /* assoc was freed while we were unlocked */ 1006 SCTP_SOCKET_UNLOCK(so, 1); 1007 return; 1008 } 1009 #endif 1010 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 1011 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1012 SCTP_SOCKET_UNLOCK(so, 1); 1013 #endif 1014 } 1015 /* are the queues empty? */ 1016 if (!TAILQ_EMPTY(&asoc->send_queue) || 1017 !TAILQ_EMPTY(&asoc->sent_queue) || 1018 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 1019 sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 1020 } 1021 /* stop the timer */ 1022 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9); 1023 /* send SHUTDOWN-COMPLETE */ 1024 sctp_send_shutdown_complete(stcb, net, 0); 1025 /* notify upper layer protocol */ 1026 if (stcb->sctp_socket) { 1027 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 1028 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1029 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 1030 /* Set the connected flag to disconnected */ 1031 stcb->sctp_socket->so_snd.sb_cc = 0; 1032 } 1033 } 1034 SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 1035 /* free the TCB but first save off the ep */ 1036 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1037 atomic_add_int(&stcb->asoc.refcnt, 1); 1038 SCTP_TCB_UNLOCK(stcb); 1039 SCTP_SOCKET_LOCK(so, 1); 1040 SCTP_TCB_LOCK(stcb); 1041 atomic_subtract_int(&stcb->asoc.refcnt, 1); 1042 #endif 1043 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1044 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10); 1045 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1046 SCTP_SOCKET_UNLOCK(so, 1); 1047 #endif 1048 } 1049 1050 /* 1051 * Skip past the param header and then we will find the chunk that caused the 1052 * problem. There are two possiblities ASCONF or FWD-TSN other than that and 1053 * our peer must be broken. 1054 */ 1055 static void 1056 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr, 1057 struct sctp_nets *net) 1058 { 1059 struct sctp_chunkhdr *chk; 1060 1061 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr)); 1062 switch (chk->chunk_type) { 1063 case SCTP_ASCONF_ACK: 1064 case SCTP_ASCONF: 1065 sctp_asconf_cleanup(stcb, net); 1066 break; 1067 case SCTP_FORWARD_CUM_TSN: 1068 stcb->asoc.peer_supports_prsctp = 0; 1069 break; 1070 default: 1071 SCTPDBG(SCTP_DEBUG_INPUT2, 1072 "Peer does not support chunk type %d(%x)??\n", 1073 chk->chunk_type, (uint32_t) chk->chunk_type); 1074 break; 1075 } 1076 } 1077 1078 /* 1079 * Skip past the param header and then we will find the param that caused the 1080 * problem. There are a number of param's in a ASCONF OR the prsctp param 1081 * these will turn of specific features. 1082 */ 1083 static void 1084 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr) 1085 { 1086 struct sctp_paramhdr *pbad; 1087 1088 pbad = phdr + 1; 1089 switch (ntohs(pbad->param_type)) { 1090 /* pr-sctp draft */ 1091 case SCTP_PRSCTP_SUPPORTED: 1092 stcb->asoc.peer_supports_prsctp = 0; 1093 break; 1094 case SCTP_SUPPORTED_CHUNK_EXT: 1095 break; 1096 /* draft-ietf-tsvwg-addip-sctp */ 1097 case SCTP_HAS_NAT_SUPPORT: 1098 stcb->asoc.peer_supports_nat = 0; 1099 break; 1100 case SCTP_ADD_IP_ADDRESS: 1101 case SCTP_DEL_IP_ADDRESS: 1102 case SCTP_SET_PRIM_ADDR: 1103 stcb->asoc.peer_supports_asconf = 0; 1104 break; 1105 case SCTP_SUCCESS_REPORT: 1106 case SCTP_ERROR_CAUSE_IND: 1107 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n"); 1108 SCTPDBG(SCTP_DEBUG_INPUT2, 1109 "Turning off ASCONF to this strange peer\n"); 1110 stcb->asoc.peer_supports_asconf = 0; 1111 break; 1112 default: 1113 SCTPDBG(SCTP_DEBUG_INPUT2, 1114 "Peer does not support param type %d(%x)??\n", 1115 pbad->param_type, (uint32_t) pbad->param_type); 1116 break; 1117 } 1118 } 1119 1120 static int 1121 sctp_handle_error(struct sctp_chunkhdr *ch, 1122 struct sctp_tcb *stcb, struct sctp_nets *net) 1123 { 1124 int chklen; 1125 struct sctp_paramhdr *phdr; 1126 uint16_t error_type; 1127 uint16_t error_len; 1128 struct sctp_association *asoc; 1129 int adjust; 1130 1131 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1132 struct socket *so; 1133 1134 #endif 1135 1136 /* parse through all of the errors and process */ 1137 asoc = &stcb->asoc; 1138 phdr = (struct sctp_paramhdr *)((caddr_t)ch + 1139 sizeof(struct sctp_chunkhdr)); 1140 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr); 1141 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { 1142 /* Process an Error Cause */ 1143 error_type = ntohs(phdr->param_type); 1144 error_len = ntohs(phdr->param_length); 1145 if ((error_len > chklen) || (error_len == 0)) { 1146 /* invalid param length for this param */ 1147 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n", 1148 chklen, error_len); 1149 return (0); 1150 } 1151 switch (error_type) { 1152 case SCTP_CAUSE_INVALID_STREAM: 1153 case SCTP_CAUSE_MISSING_PARAM: 1154 case SCTP_CAUSE_INVALID_PARAM: 1155 case SCTP_CAUSE_NO_USER_DATA: 1156 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n", 1157 error_type); 1158 break; 1159 case SCTP_CAUSE_NAT_COLLIDING_STATE: 1160 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", 1161 ch->chunk_flags); 1162 if (sctp_handle_nat_colliding_state(stcb)) { 1163 return (0); 1164 } 1165 break; 1166 case SCTP_CAUSE_NAT_MISSING_STATE: 1167 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", 1168 ch->chunk_flags); 1169 if (sctp_handle_nat_missing_state(stcb, net)) { 1170 return (0); 1171 } 1172 break; 1173 case SCTP_CAUSE_STALE_COOKIE: 1174 /* 1175 * We only act if we have echoed a cookie and are 1176 * waiting. 1177 */ 1178 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 1179 int *p; 1180 1181 p = (int *)((caddr_t)phdr + sizeof(*phdr)); 1182 /* Save the time doubled */ 1183 asoc->cookie_preserve_req = ntohl(*p) << 1; 1184 asoc->stale_cookie_count++; 1185 if (asoc->stale_cookie_count > 1186 asoc->max_init_times) { 1187 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 1188 /* now free the asoc */ 1189 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1190 so = SCTP_INP_SO(stcb->sctp_ep); 1191 atomic_add_int(&stcb->asoc.refcnt, 1); 1192 SCTP_TCB_UNLOCK(stcb); 1193 SCTP_SOCKET_LOCK(so, 1); 1194 SCTP_TCB_LOCK(stcb); 1195 atomic_subtract_int(&stcb->asoc.refcnt, 1); 1196 #endif 1197 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1198 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 1199 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1200 SCTP_SOCKET_UNLOCK(so, 1); 1201 #endif 1202 return (-1); 1203 } 1204 /* blast back to INIT state */ 1205 sctp_toss_old_cookies(stcb, &stcb->asoc); 1206 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; 1207 asoc->state |= SCTP_STATE_COOKIE_WAIT; 1208 sctp_stop_all_cookie_timers(stcb); 1209 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 1210 } 1211 break; 1212 case SCTP_CAUSE_UNRESOLVABLE_ADDR: 1213 /* 1214 * Nothing we can do here, we don't do hostname 1215 * addresses so if the peer does not like my IPv6 1216 * (or IPv4 for that matter) it does not matter. If 1217 * they don't support that type of address, they can 1218 * NOT possibly get that packet type... i.e. with no 1219 * IPv6 you can't recieve a IPv6 packet. so we can 1220 * safely ignore this one. If we ever added support 1221 * for HOSTNAME Addresses, then we would need to do 1222 * something here. 1223 */ 1224 break; 1225 case SCTP_CAUSE_UNRECOG_CHUNK: 1226 sctp_process_unrecog_chunk(stcb, phdr, net); 1227 break; 1228 case SCTP_CAUSE_UNRECOG_PARAM: 1229 sctp_process_unrecog_param(stcb, phdr); 1230 break; 1231 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 1232 /* 1233 * We ignore this since the timer will drive out a 1234 * new cookie anyway and there timer will drive us 1235 * to send a SHUTDOWN_COMPLETE. We can't send one 1236 * here since we don't have their tag. 1237 */ 1238 break; 1239 case SCTP_CAUSE_DELETING_LAST_ADDR: 1240 case SCTP_CAUSE_RESOURCE_SHORTAGE: 1241 case SCTP_CAUSE_DELETING_SRC_ADDR: 1242 /* 1243 * We should NOT get these here, but in a 1244 * ASCONF-ACK. 1245 */ 1246 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n", 1247 error_type); 1248 break; 1249 case SCTP_CAUSE_OUT_OF_RESC: 1250 /* 1251 * And what, pray tell do we do with the fact that 1252 * the peer is out of resources? Not really sure we 1253 * could do anything but abort. I suspect this 1254 * should have came WITH an abort instead of in a 1255 * OP-ERROR. 1256 */ 1257 break; 1258 default: 1259 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n", 1260 error_type); 1261 break; 1262 } 1263 adjust = SCTP_SIZE32(error_len); 1264 chklen -= adjust; 1265 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); 1266 } 1267 return (0); 1268 } 1269 1270 static int 1271 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, 1272 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 1273 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 1274 { 1275 struct sctp_init_ack *init_ack; 1276 struct mbuf *op_err; 1277 1278 SCTPDBG(SCTP_DEBUG_INPUT2, 1279 "sctp_handle_init_ack: handling INIT-ACK\n"); 1280 1281 if (stcb == NULL) { 1282 SCTPDBG(SCTP_DEBUG_INPUT2, 1283 "sctp_handle_init_ack: TCB is null\n"); 1284 return (-1); 1285 } 1286 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { 1287 /* Invalid length */ 1288 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1289 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1290 op_err, 0, net->port); 1291 *abort_no_unlock = 1; 1292 return (-1); 1293 } 1294 init_ack = &cp->init; 1295 /* validate parameters */ 1296 if (init_ack->initiate_tag == 0) { 1297 /* protocol error... send an abort */ 1298 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1299 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1300 op_err, 0, net->port); 1301 *abort_no_unlock = 1; 1302 return (-1); 1303 } 1304 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { 1305 /* protocol error... send an abort */ 1306 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1307 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1308 op_err, 0, net->port); 1309 *abort_no_unlock = 1; 1310 return (-1); 1311 } 1312 if (init_ack->num_inbound_streams == 0) { 1313 /* protocol error... send an abort */ 1314 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1315 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1316 op_err, 0, net->port); 1317 *abort_no_unlock = 1; 1318 return (-1); 1319 } 1320 if (init_ack->num_outbound_streams == 0) { 1321 /* protocol error... send an abort */ 1322 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1323 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1324 op_err, 0, net->port); 1325 *abort_no_unlock = 1; 1326 return (-1); 1327 } 1328 /* process according to association state... */ 1329 switch (stcb->asoc.state & SCTP_STATE_MASK) { 1330 case SCTP_STATE_COOKIE_WAIT: 1331 /* this is the expected state for this chunk */ 1332 /* process the INIT-ACK parameters */ 1333 if (stcb->asoc.primary_destination->dest_state & 1334 SCTP_ADDR_UNCONFIRMED) { 1335 /* 1336 * The primary is where we sent the INIT, we can 1337 * always consider it confirmed when the INIT-ACK is 1338 * returned. Do this before we load addresses 1339 * though. 1340 */ 1341 stcb->asoc.primary_destination->dest_state &= 1342 ~SCTP_ADDR_UNCONFIRMED; 1343 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 1344 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED); 1345 } 1346 if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, 1347 net, abort_no_unlock, vrf_id) < 0) { 1348 /* error in parsing parameters */ 1349 return (-1); 1350 } 1351 /* update our state */ 1352 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n"); 1353 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED); 1354 1355 /* reset the RTO calc */ 1356 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 1357 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 1358 stcb->asoc.overall_error_count, 1359 0, 1360 SCTP_FROM_SCTP_INPUT, 1361 __LINE__); 1362 } 1363 stcb->asoc.overall_error_count = 0; 1364 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1365 /* 1366 * collapse the init timer back in case of a exponential 1367 * backoff 1368 */ 1369 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 1370 stcb, net); 1371 /* 1372 * the send at the end of the inbound data processing will 1373 * cause the cookie to be sent 1374 */ 1375 break; 1376 case SCTP_STATE_SHUTDOWN_SENT: 1377 /* incorrect state... discard */ 1378 break; 1379 case SCTP_STATE_COOKIE_ECHOED: 1380 /* incorrect state... discard */ 1381 break; 1382 case SCTP_STATE_OPEN: 1383 /* incorrect state... discard */ 1384 break; 1385 case SCTP_STATE_EMPTY: 1386 case SCTP_STATE_INUSE: 1387 default: 1388 /* incorrect state... discard */ 1389 return (-1); 1390 break; 1391 } 1392 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n"); 1393 return (0); 1394 } 1395 1396 static struct sctp_tcb * 1397 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1398 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1399 struct sctp_inpcb *inp, struct sctp_nets **netp, 1400 struct sockaddr *init_src, int *notification, 1401 int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1402 uint32_t vrf_id, uint16_t port); 1403 1404 1405 /* 1406 * handle a state cookie for an existing association m: input packet mbuf 1407 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 1408 * "split" mbuf and the cookie signature does not exist offset: offset into 1409 * mbuf to the cookie-echo chunk 1410 */ 1411 static struct sctp_tcb * 1412 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 1413 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1414 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp, 1415 struct sockaddr *init_src, int *notification, 1416 uint32_t vrf_id, int auth_skipped, uint32_t auth_offset, uint32_t auth_len, uint16_t port) 1417 { 1418 struct sctp_association *asoc; 1419 struct sctp_init_chunk *init_cp, init_buf; 1420 struct sctp_init_ack_chunk *initack_cp, initack_buf; 1421 struct sctp_nets *net; 1422 struct mbuf *op_err; 1423 struct sctp_paramhdr *ph; 1424 int init_offset, initack_offset, i; 1425 int retval; 1426 int spec_flag = 0; 1427 uint32_t how_indx; 1428 1429 net = *netp; 1430 /* I know that the TCB is non-NULL from the caller */ 1431 asoc = &stcb->asoc; 1432 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { 1433 if (asoc->cookie_how[how_indx] == 0) 1434 break; 1435 } 1436 if (how_indx < sizeof(asoc->cookie_how)) { 1437 asoc->cookie_how[how_indx] = 1; 1438 } 1439 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 1440 /* SHUTDOWN came in after sending INIT-ACK */ 1441 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 1442 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1443 0, M_DONTWAIT, 1, MT_DATA); 1444 if (op_err == NULL) { 1445 /* FOOBAR */ 1446 return (NULL); 1447 } 1448 /* Set the len */ 1449 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1450 ph = mtod(op_err, struct sctp_paramhdr *); 1451 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN); 1452 ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1453 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 1454 vrf_id, net->port); 1455 if (how_indx < sizeof(asoc->cookie_how)) 1456 asoc->cookie_how[how_indx] = 2; 1457 return (NULL); 1458 } 1459 /* 1460 * find and validate the INIT chunk in the cookie (peer's info) the 1461 * INIT should start after the cookie-echo header struct (chunk 1462 * header, state cookie header struct) 1463 */ 1464 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); 1465 1466 init_cp = (struct sctp_init_chunk *) 1467 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1468 (uint8_t *) & init_buf); 1469 if (init_cp == NULL) { 1470 /* could not pull a INIT chunk in cookie */ 1471 return (NULL); 1472 } 1473 if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1474 return (NULL); 1475 } 1476 /* 1477 * find and validate the INIT-ACK chunk in the cookie (my info) the 1478 * INIT-ACK follows the INIT chunk 1479 */ 1480 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length)); 1481 initack_cp = (struct sctp_init_ack_chunk *) 1482 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1483 (uint8_t *) & initack_buf); 1484 if (initack_cp == NULL) { 1485 /* could not pull INIT-ACK chunk in cookie */ 1486 return (NULL); 1487 } 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 init_offset, initack_offset, initack_limit; 1988 int retval; 1989 int error = 0; 1990 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 1991 1992 #ifdef INET 1993 struct sockaddr_in *sin; 1994 1995 #endif 1996 #ifdef INET6 1997 struct sockaddr_in6 *sin6; 1998 1999 #endif 2000 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2001 struct socket *so; 2002 2003 so = SCTP_INP_SO(inp); 2004 #endif 2005 2006 /* 2007 * find and validate the INIT chunk in the cookie (peer's info) the 2008 * INIT should start after the cookie-echo header struct (chunk 2009 * header, state cookie header struct) 2010 */ 2011 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 2012 init_cp = (struct sctp_init_chunk *) 2013 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 2014 (uint8_t *) & init_buf); 2015 if (init_cp == NULL) { 2016 /* could not pull a INIT chunk in cookie */ 2017 SCTPDBG(SCTP_DEBUG_INPUT1, 2018 "process_cookie_new: could not pull INIT chunk hdr\n"); 2019 return (NULL); 2020 } 2021 if (init_cp->ch.chunk_type != SCTP_INITIATION) { 2022 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); 2023 return (NULL); 2024 } 2025 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length)); 2026 /* 2027 * find and validate the INIT-ACK chunk in the cookie (my info) the 2028 * INIT-ACK follows the INIT chunk 2029 */ 2030 initack_cp = (struct sctp_init_ack_chunk *) 2031 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 2032 (uint8_t *) & initack_buf); 2033 if (initack_cp == NULL) { 2034 /* could not pull INIT-ACK chunk in cookie */ 2035 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 2036 return (NULL); 2037 } 2038 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 2039 return (NULL); 2040 } 2041 /* 2042 * NOTE: We can't use the INIT_ACK's chk_length to determine the 2043 * "initack_limit" value. This is because the chk_length field 2044 * includes the length of the cookie, but the cookie is omitted when 2045 * the INIT and INIT_ACK are tacked onto the cookie... 2046 */ 2047 initack_limit = offset + cookie_len; 2048 2049 /* 2050 * now that we know the INIT/INIT-ACK are in place, create a new TCB 2051 * and popluate 2052 */ 2053 2054 /* 2055 * Here we do a trick, we set in NULL for the proc/thread argument. 2056 * We do this since in effect we only use the p argument when the 2057 * socket is unbound and we must do an implicit bind. Since we are 2058 * getting a cookie, we cannot be unbound. 2059 */ 2060 stcb = sctp_aloc_assoc(inp, init_src, &error, 2061 ntohl(initack_cp->init.initiate_tag), vrf_id, 2062 (struct thread *)NULL 2063 ); 2064 if (stcb == NULL) { 2065 struct mbuf *op_err; 2066 2067 /* memory problem? */ 2068 SCTPDBG(SCTP_DEBUG_INPUT1, 2069 "process_cookie_new: no room for another TCB!\n"); 2070 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2071 2072 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2073 sh, op_err, vrf_id, port); 2074 return (NULL); 2075 } 2076 /* get the correct sctp_nets */ 2077 if (netp) 2078 *netp = sctp_findnet(stcb, init_src); 2079 2080 asoc = &stcb->asoc; 2081 /* get scope variables out of cookie */ 2082 asoc->ipv4_local_scope = cookie->ipv4_scope; 2083 asoc->site_scope = cookie->site_scope; 2084 asoc->local_scope = cookie->local_scope; 2085 asoc->loopback_scope = cookie->loopback_scope; 2086 2087 if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) || 2088 (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) { 2089 struct mbuf *op_err; 2090 2091 /* 2092 * Houston we have a problem. The EP changed while the 2093 * cookie was in flight. Only recourse is to abort the 2094 * association. 2095 */ 2096 atomic_add_int(&stcb->asoc.refcnt, 1); 2097 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2098 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2099 sh, op_err, vrf_id, port); 2100 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2101 SCTP_TCB_UNLOCK(stcb); 2102 SCTP_SOCKET_LOCK(so, 1); 2103 SCTP_TCB_LOCK(stcb); 2104 #endif 2105 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 2106 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2107 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2108 SCTP_SOCKET_UNLOCK(so, 1); 2109 #endif 2110 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2111 return (NULL); 2112 } 2113 /* process the INIT-ACK info (my info) */ 2114 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 2115 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 2116 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 2117 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 2118 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 2119 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 2120 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 2121 asoc->str_reset_seq_in = asoc->init_seq_number; 2122 2123 asoc->advanced_peer_ack_point = asoc->last_acked_seq; 2124 2125 /* process the INIT info (peer's info) */ 2126 if (netp) 2127 retval = sctp_process_init(init_cp, stcb); 2128 else 2129 retval = 0; 2130 if (retval < 0) { 2131 atomic_add_int(&stcb->asoc.refcnt, 1); 2132 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2133 SCTP_TCB_UNLOCK(stcb); 2134 SCTP_SOCKET_LOCK(so, 1); 2135 SCTP_TCB_LOCK(stcb); 2136 #endif 2137 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2138 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2139 SCTP_SOCKET_UNLOCK(so, 1); 2140 #endif 2141 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2142 return (NULL); 2143 } 2144 /* load all addresses */ 2145 if (sctp_load_addresses_from_init(stcb, m, 2146 init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh, 2147 init_src)) { 2148 atomic_add_int(&stcb->asoc.refcnt, 1); 2149 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2150 SCTP_TCB_UNLOCK(stcb); 2151 SCTP_SOCKET_LOCK(so, 1); 2152 SCTP_TCB_LOCK(stcb); 2153 #endif 2154 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 2155 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2156 SCTP_SOCKET_UNLOCK(so, 1); 2157 #endif 2158 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2159 return (NULL); 2160 } 2161 /* 2162 * verify any preceding AUTH chunk that was skipped 2163 */ 2164 /* pull the local authentication parameters from the cookie/init-ack */ 2165 sctp_auth_get_cookie_params(stcb, m, 2166 initack_offset + sizeof(struct sctp_init_ack_chunk), 2167 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 2168 if (auth_skipped) { 2169 struct sctp_auth_chunk *auth; 2170 2171 auth = (struct sctp_auth_chunk *) 2172 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 2173 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { 2174 /* auth HMAC failed, dump the assoc and packet */ 2175 SCTPDBG(SCTP_DEBUG_AUTH1, 2176 "COOKIE-ECHO: AUTH failed\n"); 2177 atomic_add_int(&stcb->asoc.refcnt, 1); 2178 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2179 SCTP_TCB_UNLOCK(stcb); 2180 SCTP_SOCKET_LOCK(so, 1); 2181 SCTP_TCB_LOCK(stcb); 2182 #endif 2183 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 2184 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2185 SCTP_SOCKET_UNLOCK(so, 1); 2186 #endif 2187 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2188 return (NULL); 2189 } else { 2190 /* remaining chunks checked... good to go */ 2191 stcb->asoc.authenticated = 1; 2192 } 2193 } 2194 /* update current state */ 2195 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2196 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2197 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2198 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2199 stcb->sctp_ep, stcb, asoc->primary_destination); 2200 } 2201 sctp_stop_all_cookie_timers(stcb); 2202 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 2203 SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2204 2205 /* 2206 * if we're doing ASCONFs, check to see if we have any new local 2207 * addresses that need to get added to the peer (eg. addresses 2208 * changed while cookie echo in flight). This needs to be done 2209 * after we go to the OPEN state to do the correct asconf 2210 * processing. else, make sure we have the correct addresses in our 2211 * lists 2212 */ 2213 2214 /* warning, we re-use sin, sin6, sa_store here! */ 2215 /* pull in local_address (our "from" address) */ 2216 switch (cookie->laddr_type) { 2217 #ifdef INET 2218 case SCTP_IPV4_ADDRESS: 2219 /* source addr is IPv4 */ 2220 sin = (struct sockaddr_in *)initack_src; 2221 memset(sin, 0, sizeof(*sin)); 2222 sin->sin_family = AF_INET; 2223 sin->sin_len = sizeof(struct sockaddr_in); 2224 sin->sin_addr.s_addr = cookie->laddress[0]; 2225 break; 2226 #endif 2227 #ifdef INET6 2228 case SCTP_IPV6_ADDRESS: 2229 /* source addr is IPv6 */ 2230 sin6 = (struct sockaddr_in6 *)initack_src; 2231 memset(sin6, 0, sizeof(*sin6)); 2232 sin6->sin6_family = AF_INET6; 2233 sin6->sin6_len = sizeof(struct sockaddr_in6); 2234 sin6->sin6_scope_id = cookie->scope_id; 2235 memcpy(&sin6->sin6_addr, cookie->laddress, 2236 sizeof(sin6->sin6_addr)); 2237 break; 2238 #endif 2239 default: 2240 atomic_add_int(&stcb->asoc.refcnt, 1); 2241 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2242 SCTP_TCB_UNLOCK(stcb); 2243 SCTP_SOCKET_LOCK(so, 1); 2244 SCTP_TCB_LOCK(stcb); 2245 #endif 2246 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 2247 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2248 SCTP_SOCKET_UNLOCK(so, 1); 2249 #endif 2250 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2251 return (NULL); 2252 } 2253 2254 /* set up to notify upper layer */ 2255 *notification = SCTP_NOTIFY_ASSOC_UP; 2256 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2257 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 2258 (inp->sctp_socket->so_qlimit == 0)) { 2259 /* 2260 * This is an endpoint that called connect() how it got a 2261 * cookie that is NEW is a bit of a mystery. It must be that 2262 * the INIT was sent, but before it got there.. a complete 2263 * INIT/INIT-ACK/COOKIE arrived. But of course then it 2264 * should have went to the other code.. not here.. oh well.. 2265 * a bit of protection is worth having.. 2266 */ 2267 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2268 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2269 atomic_add_int(&stcb->asoc.refcnt, 1); 2270 SCTP_TCB_UNLOCK(stcb); 2271 SCTP_SOCKET_LOCK(so, 1); 2272 SCTP_TCB_LOCK(stcb); 2273 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2274 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2275 SCTP_SOCKET_UNLOCK(so, 1); 2276 return (NULL); 2277 } 2278 #endif 2279 soisconnected(stcb->sctp_socket); 2280 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2281 SCTP_SOCKET_UNLOCK(so, 1); 2282 #endif 2283 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 2284 (inp->sctp_socket->so_qlimit)) { 2285 /* 2286 * We don't want to do anything with this one. Since it is 2287 * the listening guy. The timer will get started for 2288 * accepted connections in the caller. 2289 */ 2290 ; 2291 } 2292 /* since we did not send a HB make sure we don't double things */ 2293 if ((netp) && (*netp)) 2294 (*netp)->hb_responded = 1; 2295 2296 if (stcb->asoc.sctp_autoclose_ticks && 2297 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2298 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 2299 } 2300 /* calculate the RTT */ 2301 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 2302 if ((netp) && (*netp)) { 2303 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 2304 &cookie->time_entered, sctp_align_unsafe_makecopy, 2305 SCTP_RTT_FROM_NON_DATA); 2306 } 2307 /* respond with a COOKIE-ACK */ 2308 sctp_send_cookie_ack(stcb); 2309 2310 /* 2311 * check the address lists for any ASCONFs that need to be sent 2312 * AFTER the cookie-ack is sent 2313 */ 2314 sctp_check_address_list(stcb, m, 2315 initack_offset + sizeof(struct sctp_init_ack_chunk), 2316 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 2317 initack_src, cookie->local_scope, cookie->site_scope, 2318 cookie->ipv4_scope, cookie->loopback_scope); 2319 2320 2321 return (stcb); 2322 } 2323 2324 /* 2325 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e 2326 * we NEED to make sure we are not already using the vtag. If so we 2327 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit! 2328 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, 2329 SCTP_BASE_INFO(hashasocmark))]; 2330 LIST_FOREACH(stcb, head, sctp_asocs) { 2331 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) { 2332 -- SEND ABORT - TRY AGAIN -- 2333 } 2334 } 2335 */ 2336 2337 /* 2338 * handles a COOKIE-ECHO message stcb: modified to either a new or left as 2339 * existing (non-NULL) TCB 2340 */ 2341 static struct mbuf * 2342 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 2343 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 2344 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 2345 int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 2346 struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint16_t port) 2347 { 2348 struct sctp_state_cookie *cookie; 2349 struct sctp_tcb *l_stcb = *stcb; 2350 struct sctp_inpcb *l_inp; 2351 struct sockaddr *to; 2352 struct sctp_pcb *ep; 2353 struct mbuf *m_sig; 2354 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 2355 uint8_t *sig; 2356 uint8_t cookie_ok = 0; 2357 unsigned int size_of_pkt, sig_offset, cookie_offset; 2358 unsigned int cookie_len; 2359 struct timeval now; 2360 struct timeval time_expires; 2361 struct sockaddr_storage dest_store; 2362 struct sockaddr *localep_sa = (struct sockaddr *)&dest_store; 2363 struct ip *iph; 2364 int notification = 0; 2365 struct sctp_nets *netl; 2366 int had_a_existing_tcb = 0; 2367 int send_int_conf = 0; 2368 2369 #ifdef INET 2370 struct sockaddr_in sin; 2371 2372 #endif 2373 #ifdef INET6 2374 struct sockaddr_in6 sin6; 2375 2376 #endif 2377 2378 SCTPDBG(SCTP_DEBUG_INPUT2, 2379 "sctp_handle_cookie: handling COOKIE-ECHO\n"); 2380 2381 if (inp_p == NULL) { 2382 return (NULL); 2383 } 2384 /* First get the destination address setup too. */ 2385 iph = mtod(m, struct ip *); 2386 switch (iph->ip_v) { 2387 #ifdef INET 2388 case IPVERSION: 2389 { 2390 /* its IPv4 */ 2391 struct sockaddr_in *lsin; 2392 2393 lsin = (struct sockaddr_in *)(localep_sa); 2394 memset(lsin, 0, sizeof(*lsin)); 2395 lsin->sin_family = AF_INET; 2396 lsin->sin_len = sizeof(*lsin); 2397 lsin->sin_port = sh->dest_port; 2398 lsin->sin_addr.s_addr = iph->ip_dst.s_addr; 2399 size_of_pkt = SCTP_GET_IPV4_LENGTH(iph); 2400 break; 2401 } 2402 #endif 2403 #ifdef INET6 2404 case IPV6_VERSION >> 4: 2405 { 2406 /* its IPv6 */ 2407 struct ip6_hdr *ip6; 2408 struct sockaddr_in6 *lsin6; 2409 2410 lsin6 = (struct sockaddr_in6 *)(localep_sa); 2411 memset(lsin6, 0, sizeof(*lsin6)); 2412 lsin6->sin6_family = AF_INET6; 2413 lsin6->sin6_len = sizeof(struct sockaddr_in6); 2414 ip6 = mtod(m, struct ip6_hdr *); 2415 lsin6->sin6_port = sh->dest_port; 2416 lsin6->sin6_addr = ip6->ip6_dst; 2417 size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen; 2418 break; 2419 } 2420 #endif 2421 default: 2422 return (NULL); 2423 } 2424 2425 cookie = &cp->cookie; 2426 cookie_offset = offset + sizeof(struct sctp_chunkhdr); 2427 cookie_len = ntohs(cp->ch.chunk_length); 2428 2429 if ((cookie->peerport != sh->src_port) && 2430 (cookie->myport != sh->dest_port) && 2431 (cookie->my_vtag != sh->v_tag)) { 2432 /* 2433 * invalid ports or bad tag. Note that we always leave the 2434 * v_tag in the header in network order and when we stored 2435 * it in the my_vtag slot we also left it in network order. 2436 * This maintains the match even though it may be in the 2437 * opposite byte order of the machine :-> 2438 */ 2439 return (NULL); 2440 } 2441 if (cookie_len > size_of_pkt || 2442 cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 2443 sizeof(struct sctp_init_chunk) + 2444 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 2445 /* cookie too long! or too small */ 2446 return (NULL); 2447 } 2448 /* 2449 * split off the signature into its own mbuf (since it should not be 2450 * calculated in the sctp_hmac_m() call). 2451 */ 2452 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 2453 if (sig_offset > size_of_pkt) { 2454 /* packet not correct size! */ 2455 /* XXX this may already be accounted for earlier... */ 2456 return (NULL); 2457 } 2458 m_sig = m_split(m, sig_offset, M_DONTWAIT); 2459 if (m_sig == NULL) { 2460 /* out of memory or ?? */ 2461 return (NULL); 2462 } 2463 #ifdef SCTP_MBUF_LOGGING 2464 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 2465 struct mbuf *mat; 2466 2467 mat = m_sig; 2468 while (mat) { 2469 if (SCTP_BUF_IS_EXTENDED(mat)) { 2470 sctp_log_mb(mat, SCTP_MBUF_SPLIT); 2471 } 2472 mat = SCTP_BUF_NEXT(mat); 2473 } 2474 } 2475 #endif 2476 2477 /* 2478 * compute the signature/digest for the cookie 2479 */ 2480 ep = &(*inp_p)->sctp_ep; 2481 l_inp = *inp_p; 2482 if (l_stcb) { 2483 SCTP_TCB_UNLOCK(l_stcb); 2484 } 2485 SCTP_INP_RLOCK(l_inp); 2486 if (l_stcb) { 2487 SCTP_TCB_LOCK(l_stcb); 2488 } 2489 /* which cookie is it? */ 2490 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 2491 (ep->current_secret_number != ep->last_secret_number)) { 2492 /* it's the old cookie */ 2493 (void)sctp_hmac_m(SCTP_HMAC, 2494 (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2495 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2496 } else { 2497 /* it's the current cookie */ 2498 (void)sctp_hmac_m(SCTP_HMAC, 2499 (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 2500 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2501 } 2502 /* get the signature */ 2503 SCTP_INP_RUNLOCK(l_inp); 2504 sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 2505 if (sig == NULL) { 2506 /* couldn't find signature */ 2507 sctp_m_freem(m_sig); 2508 return (NULL); 2509 } 2510 /* compare the received digest with the computed digest */ 2511 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 2512 /* try the old cookie? */ 2513 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 2514 (ep->current_secret_number != ep->last_secret_number)) { 2515 /* compute digest with old */ 2516 (void)sctp_hmac_m(SCTP_HMAC, 2517 (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2518 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2519 /* compare */ 2520 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 2521 cookie_ok = 1; 2522 } 2523 } else { 2524 cookie_ok = 1; 2525 } 2526 2527 /* 2528 * Now before we continue we must reconstruct our mbuf so that 2529 * normal processing of any other chunks will work. 2530 */ 2531 { 2532 struct mbuf *m_at; 2533 2534 m_at = m; 2535 while (SCTP_BUF_NEXT(m_at) != NULL) { 2536 m_at = SCTP_BUF_NEXT(m_at); 2537 } 2538 SCTP_BUF_NEXT(m_at) = m_sig; 2539 } 2540 2541 if (cookie_ok == 0) { 2542 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); 2543 SCTPDBG(SCTP_DEBUG_INPUT2, 2544 "offset = %u, cookie_offset = %u, sig_offset = %u\n", 2545 (uint32_t) offset, cookie_offset, sig_offset); 2546 return (NULL); 2547 } 2548 /* 2549 * check the cookie timestamps to be sure it's not stale 2550 */ 2551 (void)SCTP_GETTIME_TIMEVAL(&now); 2552 /* Expire time is in Ticks, so we convert to seconds */ 2553 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life); 2554 time_expires.tv_usec = cookie->time_entered.tv_usec; 2555 /* 2556 * TODO sctp_constants.h needs alternative time macros when _KERNEL 2557 * is undefined. 2558 */ 2559 if (timevalcmp(&now, &time_expires, >)) { 2560 /* cookie is stale! */ 2561 struct mbuf *op_err; 2562 struct sctp_stale_cookie_msg *scm; 2563 uint32_t tim; 2564 2565 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 2566 0, M_DONTWAIT, 1, MT_DATA); 2567 if (op_err == NULL) { 2568 /* FOOBAR */ 2569 return (NULL); 2570 } 2571 /* Set the len */ 2572 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 2573 scm = mtod(op_err, struct sctp_stale_cookie_msg *); 2574 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 2575 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 2576 (sizeof(uint32_t)))); 2577 /* seconds to usec */ 2578 tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 2579 /* add in usec */ 2580 if (tim == 0) 2581 tim = now.tv_usec - cookie->time_entered.tv_usec; 2582 scm->time_usec = htonl(tim); 2583 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 2584 vrf_id, port); 2585 return (NULL); 2586 } 2587 /* 2588 * Now we must see with the lookup address if we have an existing 2589 * asoc. This will only happen if we were in the COOKIE-WAIT state 2590 * and a INIT collided with us and somewhere the peer sent the 2591 * cookie on another address besides the single address our assoc 2592 * had for him. In this case we will have one of the tie-tags set at 2593 * least AND the address field in the cookie can be used to look it 2594 * up. 2595 */ 2596 to = NULL; 2597 switch (cookie->addr_type) { 2598 #ifdef INET6 2599 case SCTP_IPV6_ADDRESS: 2600 memset(&sin6, 0, sizeof(sin6)); 2601 sin6.sin6_family = AF_INET6; 2602 sin6.sin6_len = sizeof(sin6); 2603 sin6.sin6_port = sh->src_port; 2604 sin6.sin6_scope_id = cookie->scope_id; 2605 memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 2606 sizeof(sin6.sin6_addr.s6_addr)); 2607 to = (struct sockaddr *)&sin6; 2608 break; 2609 #endif 2610 #ifdef INET 2611 case SCTP_IPV4_ADDRESS: 2612 memset(&sin, 0, sizeof(sin)); 2613 sin.sin_family = AF_INET; 2614 sin.sin_len = sizeof(sin); 2615 sin.sin_port = sh->src_port; 2616 sin.sin_addr.s_addr = cookie->address[0]; 2617 to = (struct sockaddr *)&sin; 2618 break; 2619 #endif 2620 default: 2621 /* This should not happen */ 2622 return (NULL); 2623 } 2624 if ((*stcb == NULL) && to) { 2625 /* Yep, lets check */ 2626 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL); 2627 if (*stcb == NULL) { 2628 /* 2629 * We should have only got back the same inp. If we 2630 * got back a different ep we have a problem. The 2631 * original findep got back l_inp and now 2632 */ 2633 if (l_inp != *inp_p) { 2634 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); 2635 } 2636 } else { 2637 if (*locked_tcb == NULL) { 2638 /* 2639 * In this case we found the assoc only 2640 * after we locked the create lock. This 2641 * means we are in a colliding case and we 2642 * must make sure that we unlock the tcb if 2643 * its one of the cases where we throw away 2644 * the incoming packets. 2645 */ 2646 *locked_tcb = *stcb; 2647 2648 /* 2649 * We must also increment the inp ref count 2650 * since the ref_count flags was set when we 2651 * did not find the TCB, now we found it 2652 * which reduces the refcount.. we must 2653 * raise it back out to balance it all :-) 2654 */ 2655 SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2656 if ((*stcb)->sctp_ep != l_inp) { 2657 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", 2658 (*stcb)->sctp_ep, l_inp); 2659 } 2660 } 2661 } 2662 } 2663 if (to == NULL) { 2664 return (NULL); 2665 } 2666 cookie_len -= SCTP_SIGNATURE_SIZE; 2667 if (*stcb == NULL) { 2668 /* this is the "normal" case... get a new TCB */ 2669 *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie, 2670 cookie_len, *inp_p, netp, to, ¬ification, 2671 auth_skipped, auth_offset, auth_len, vrf_id, port); 2672 } else { 2673 /* this is abnormal... cookie-echo on existing TCB */ 2674 had_a_existing_tcb = 1; 2675 *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh, 2676 cookie, cookie_len, *inp_p, *stcb, netp, to, 2677 ¬ification, vrf_id, auth_skipped, auth_offset, auth_len, port); 2678 } 2679 2680 if (*stcb == NULL) { 2681 /* still no TCB... must be bad cookie-echo */ 2682 return (NULL); 2683 } 2684 if ((*netp != NULL) && (m->m_flags & M_FLOWID)) { 2685 (*netp)->flowid = m->m_pkthdr.flowid; 2686 #ifdef INVARIANTS 2687 (*netp)->flowidset = 1; 2688 #endif 2689 } 2690 /* 2691 * Ok, we built an association so confirm the address we sent the 2692 * INIT-ACK to. 2693 */ 2694 netl = sctp_findnet(*stcb, to); 2695 /* 2696 * This code should in theory NOT run but 2697 */ 2698 if (netl == NULL) { 2699 /* TSNH! Huh, why do I need to add this address here? */ 2700 if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) { 2701 return (NULL); 2702 } 2703 netl = sctp_findnet(*stcb, to); 2704 } 2705 if (netl) { 2706 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2707 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2708 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2709 netl); 2710 send_int_conf = 1; 2711 } 2712 } 2713 sctp_start_net_timers(*stcb); 2714 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2715 if (!had_a_existing_tcb || 2716 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2717 /* 2718 * If we have a NEW cookie or the connect never 2719 * reached the connected state during collision we 2720 * must do the TCP accept thing. 2721 */ 2722 struct socket *so, *oso; 2723 struct sctp_inpcb *inp; 2724 2725 if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2726 /* 2727 * For a restart we will keep the same 2728 * socket, no need to do anything. I THINK!! 2729 */ 2730 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2731 if (send_int_conf) { 2732 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2733 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2734 } 2735 return (m); 2736 } 2737 oso = (*inp_p)->sctp_socket; 2738 atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2739 SCTP_TCB_UNLOCK((*stcb)); 2740 CURVNET_SET(oso->so_vnet); 2741 so = sonewconn(oso, 0 2742 ); 2743 CURVNET_RESTORE(); 2744 SCTP_TCB_LOCK((*stcb)); 2745 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2746 2747 if (so == NULL) { 2748 struct mbuf *op_err; 2749 2750 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2751 struct socket *pcb_so; 2752 2753 #endif 2754 /* Too many sockets */ 2755 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2756 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2757 sctp_abort_association(*inp_p, NULL, m, iphlen, 2758 sh, op_err, vrf_id, port); 2759 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2760 pcb_so = SCTP_INP_SO(*inp_p); 2761 atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2762 SCTP_TCB_UNLOCK((*stcb)); 2763 SCTP_SOCKET_LOCK(pcb_so, 1); 2764 SCTP_TCB_LOCK((*stcb)); 2765 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2766 #endif 2767 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2768 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2769 SCTP_SOCKET_UNLOCK(pcb_so, 1); 2770 #endif 2771 return (NULL); 2772 } 2773 inp = (struct sctp_inpcb *)so->so_pcb; 2774 SCTP_INP_INCR_REF(inp); 2775 /* 2776 * We add the unbound flag here so that if we get an 2777 * soabort() before we get the move_pcb done, we 2778 * will properly cleanup. 2779 */ 2780 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2781 SCTP_PCB_FLAGS_CONNECTED | 2782 SCTP_PCB_FLAGS_IN_TCPPOOL | 2783 SCTP_PCB_FLAGS_UNBOUND | 2784 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2785 SCTP_PCB_FLAGS_DONT_WAKE); 2786 inp->sctp_features = (*inp_p)->sctp_features; 2787 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features; 2788 inp->sctp_socket = so; 2789 inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2790 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off; 2791 inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable; 2792 inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2793 inp->sctp_context = (*inp_p)->sctp_context; 2794 inp->inp_starting_point_for_iterator = NULL; 2795 /* 2796 * copy in the authentication parameters from the 2797 * original endpoint 2798 */ 2799 if (inp->sctp_ep.local_hmacs) 2800 sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2801 inp->sctp_ep.local_hmacs = 2802 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2803 if (inp->sctp_ep.local_auth_chunks) 2804 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2805 inp->sctp_ep.local_auth_chunks = 2806 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2807 2808 /* 2809 * Now we must move it from one hash table to 2810 * another and get the tcb in the right place. 2811 */ 2812 2813 /* 2814 * This is where the one-2-one socket is put into 2815 * the accept state waiting for the accept! 2816 */ 2817 if (*stcb) { 2818 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE; 2819 } 2820 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2821 2822 atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2823 SCTP_TCB_UNLOCK((*stcb)); 2824 2825 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, 2826 0); 2827 SCTP_TCB_LOCK((*stcb)); 2828 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2829 2830 2831 /* 2832 * now we must check to see if we were aborted while 2833 * the move was going on and the lock/unlock 2834 * happened. 2835 */ 2836 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2837 /* 2838 * yep it was, we leave the assoc attached 2839 * to the socket since the sctp_inpcb_free() 2840 * call will send an abort for us. 2841 */ 2842 SCTP_INP_DECR_REF(inp); 2843 return (NULL); 2844 } 2845 SCTP_INP_DECR_REF(inp); 2846 /* Switch over to the new guy */ 2847 *inp_p = inp; 2848 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2849 if (send_int_conf) { 2850 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2851 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2852 } 2853 /* 2854 * Pull it from the incomplete queue and wake the 2855 * guy 2856 */ 2857 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2858 atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2859 SCTP_TCB_UNLOCK((*stcb)); 2860 SCTP_SOCKET_LOCK(so, 1); 2861 #endif 2862 soisconnected(so); 2863 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2864 SCTP_TCB_LOCK((*stcb)); 2865 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2866 SCTP_SOCKET_UNLOCK(so, 1); 2867 #endif 2868 return (m); 2869 } 2870 } 2871 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) { 2872 if (notification) { 2873 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2874 } 2875 if (send_int_conf) { 2876 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2877 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2878 } 2879 } 2880 return (m); 2881 } 2882 2883 static void 2884 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED, 2885 struct sctp_tcb *stcb, struct sctp_nets *net) 2886 { 2887 /* cp must not be used, others call this without a c-ack :-) */ 2888 struct sctp_association *asoc; 2889 2890 SCTPDBG(SCTP_DEBUG_INPUT2, 2891 "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2892 if (stcb == NULL) 2893 return; 2894 2895 asoc = &stcb->asoc; 2896 2897 sctp_stop_all_cookie_timers(stcb); 2898 /* process according to association state */ 2899 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2900 /* state change only needed when I am in right state */ 2901 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2902 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2903 sctp_start_net_timers(stcb); 2904 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2905 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2906 stcb->sctp_ep, stcb, asoc->primary_destination); 2907 2908 } 2909 /* update RTO */ 2910 SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2911 SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2912 if (asoc->overall_error_count == 0) { 2913 net->RTO = sctp_calculate_rto(stcb, asoc, net, 2914 &asoc->time_entered, sctp_align_safe_nocopy, 2915 SCTP_RTT_FROM_NON_DATA); 2916 } 2917 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2918 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2919 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2920 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2921 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2922 struct socket *so; 2923 2924 #endif 2925 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2926 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2927 so = SCTP_INP_SO(stcb->sctp_ep); 2928 atomic_add_int(&stcb->asoc.refcnt, 1); 2929 SCTP_TCB_UNLOCK(stcb); 2930 SCTP_SOCKET_LOCK(so, 1); 2931 SCTP_TCB_LOCK(stcb); 2932 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2933 #endif 2934 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) { 2935 soisconnected(stcb->sctp_socket); 2936 } 2937 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2938 SCTP_SOCKET_UNLOCK(so, 1); 2939 #endif 2940 } 2941 /* 2942 * since we did not send a HB make sure we don't double 2943 * things 2944 */ 2945 net->hb_responded = 1; 2946 2947 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2948 /* 2949 * We don't need to do the asconf thing, nor hb or 2950 * autoclose if the socket is closed. 2951 */ 2952 goto closed_socket; 2953 } 2954 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2955 stcb, net); 2956 2957 2958 if (stcb->asoc.sctp_autoclose_ticks && 2959 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2960 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2961 stcb->sctp_ep, stcb, NULL); 2962 } 2963 /* 2964 * send ASCONF if parameters are pending and ASCONFs are 2965 * allowed (eg. addresses changed when init/cookie echo were 2966 * in flight) 2967 */ 2968 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2969 (stcb->asoc.peer_supports_asconf) && 2970 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2971 #ifdef SCTP_TIMER_BASED_ASCONF 2972 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2973 stcb->sctp_ep, stcb, 2974 stcb->asoc.primary_destination); 2975 #else 2976 sctp_send_asconf(stcb, stcb->asoc.primary_destination, 2977 SCTP_ADDR_NOT_LOCKED); 2978 #endif 2979 } 2980 } 2981 closed_socket: 2982 /* Toss the cookie if I can */ 2983 sctp_toss_old_cookies(stcb, asoc); 2984 if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2985 /* Restart the timer if we have pending data */ 2986 struct sctp_tmit_chunk *chk; 2987 2988 chk = TAILQ_FIRST(&asoc->sent_queue); 2989 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 2990 } 2991 } 2992 2993 static void 2994 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2995 struct sctp_tcb *stcb) 2996 { 2997 struct sctp_nets *net; 2998 struct sctp_tmit_chunk *lchk; 2999 struct sctp_ecne_chunk bkup; 3000 uint8_t override_bit; 3001 uint32_t tsn, window_data_tsn; 3002 int len; 3003 unsigned int pkt_cnt; 3004 3005 len = ntohs(cp->ch.chunk_length); 3006 if ((len != sizeof(struct sctp_ecne_chunk)) && 3007 (len != sizeof(struct old_sctp_ecne_chunk))) { 3008 return; 3009 } 3010 if (len == sizeof(struct old_sctp_ecne_chunk)) { 3011 /* Its the old format */ 3012 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk)); 3013 bkup.num_pkts_since_cwr = htonl(1); 3014 cp = &bkup; 3015 } 3016 SCTP_STAT_INCR(sctps_recvecne); 3017 tsn = ntohl(cp->tsn); 3018 pkt_cnt = ntohl(cp->num_pkts_since_cwr); 3019 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead); 3020 if (lchk == NULL) { 3021 window_data_tsn = stcb->asoc.sending_seq - 1; 3022 } else { 3023 window_data_tsn = lchk->rec.data.TSN_seq; 3024 } 3025 3026 /* Find where it was sent to if possible. */ 3027 net = NULL; 3028 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) { 3029 if (lchk->rec.data.TSN_seq == tsn) { 3030 net = lchk->whoTo; 3031 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send; 3032 break; 3033 } 3034 if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) { 3035 break; 3036 } 3037 } 3038 if (net == NULL) { 3039 /* 3040 * What to do. A previous send of a CWR was possibly lost. 3041 * See how old it is, we may have it marked on the actual 3042 * net. 3043 */ 3044 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 3045 if (tsn == net->last_cwr_tsn) { 3046 /* Found him, send it off */ 3047 break; 3048 } 3049 } 3050 if (net == NULL) { 3051 /* 3052 * If we reach here, we need to send a special CWR 3053 * that says hey, we did this a long time ago and 3054 * you lost the response. 3055 */ 3056 net = TAILQ_FIRST(&stcb->asoc.nets); 3057 if (net == NULL) { 3058 /* TSNH */ 3059 return; 3060 } 3061 override_bit = SCTP_CWR_REDUCE_OVERRIDE; 3062 } else { 3063 override_bit = 0; 3064 } 3065 } else { 3066 override_bit = 0; 3067 } 3068 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) && 3069 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) { 3070 /* 3071 * JRS - Use the congestion control given in the pluggable 3072 * CC module 3073 */ 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 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) || 4506 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 4507 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 4508 /* implied cookie-ack.. we must have lost the ack */ 4509 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4510 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4511 stcb->asoc.overall_error_count, 4512 0, 4513 SCTP_FROM_SCTP_INPUT, 4514 __LINE__); 4515 } 4516 stcb->asoc.overall_error_count = 0; 4517 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 4518 *netp); 4519 } 4520 process_control_chunks: 4521 while (IS_SCTP_CONTROL(ch)) { 4522 /* validate chunk length */ 4523 chk_length = ntohs(ch->chunk_length); 4524 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 4525 ch->chunk_type, chk_length); 4526 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 4527 if (chk_length < sizeof(*ch) || 4528 (*offset + (int)chk_length) > length) { 4529 *offset = length; 4530 if (locked_tcb) { 4531 SCTP_TCB_UNLOCK(locked_tcb); 4532 } 4533 return (NULL); 4534 } 4535 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 4536 /* 4537 * INIT-ACK only gets the init ack "header" portion only 4538 * because we don't have to process the peer's COOKIE. All 4539 * others get a complete chunk. 4540 */ 4541 if ((ch->chunk_type == SCTP_INITIATION_ACK) || 4542 (ch->chunk_type == SCTP_INITIATION)) { 4543 /* get an init-ack chunk */ 4544 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4545 sizeof(struct sctp_init_ack_chunk), chunk_buf); 4546 if (ch == NULL) { 4547 *offset = length; 4548 if (locked_tcb) { 4549 SCTP_TCB_UNLOCK(locked_tcb); 4550 } 4551 return (NULL); 4552 } 4553 } else { 4554 /* For cookies and all other chunks. */ 4555 if (chk_length > sizeof(chunk_buf)) { 4556 /* 4557 * use just the size of the chunk buffer so 4558 * the front part of our chunks fit in 4559 * contiguous space up to the chunk buffer 4560 * size (508 bytes). For chunks that need to 4561 * get more than that they must use the 4562 * sctp_m_getptr() function or other means 4563 * (e.g. know how to parse mbuf chains). 4564 * Cookies do this already. 4565 */ 4566 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4567 (sizeof(chunk_buf) - 4), 4568 chunk_buf); 4569 if (ch == NULL) { 4570 *offset = length; 4571 if (locked_tcb) { 4572 SCTP_TCB_UNLOCK(locked_tcb); 4573 } 4574 return (NULL); 4575 } 4576 } else { 4577 /* We can fit it all */ 4578 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4579 chk_length, chunk_buf); 4580 if (ch == NULL) { 4581 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 4582 *offset = length; 4583 if (locked_tcb) { 4584 SCTP_TCB_UNLOCK(locked_tcb); 4585 } 4586 return (NULL); 4587 } 4588 } 4589 } 4590 num_chunks++; 4591 /* Save off the last place we got a control from */ 4592 if (stcb != NULL) { 4593 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 4594 /* 4595 * allow last_control to be NULL if 4596 * ASCONF... ASCONF processing will find the 4597 * right net later 4598 */ 4599 if ((netp != NULL) && (*netp != NULL)) 4600 stcb->asoc.last_control_chunk_from = *netp; 4601 } 4602 } 4603 #ifdef SCTP_AUDITING_ENABLED 4604 sctp_audit_log(0xB0, ch->chunk_type); 4605 #endif 4606 4607 /* check to see if this chunk required auth, but isn't */ 4608 if ((stcb != NULL) && 4609 !SCTP_BASE_SYSCTL(sctp_auth_disable) && 4610 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) && 4611 !stcb->asoc.authenticated) { 4612 /* "silently" ignore */ 4613 SCTP_STAT_INCR(sctps_recvauthmissing); 4614 goto next_chunk; 4615 } 4616 switch (ch->chunk_type) { 4617 case SCTP_INITIATION: 4618 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 4619 /* The INIT chunk must be the only chunk. */ 4620 if ((num_chunks > 1) || 4621 (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4622 sctp_abort_association(inp, stcb, m, 4623 iphlen, sh, NULL, vrf_id, port); 4624 *offset = length; 4625 return (NULL); 4626 } 4627 /* Honor our resource limit. */ 4628 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) { 4629 struct mbuf *op_err; 4630 4631 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 4632 sctp_abort_association(inp, stcb, m, 4633 iphlen, sh, op_err, vrf_id, port); 4634 *offset = length; 4635 return (NULL); 4636 } 4637 sctp_handle_init(m, iphlen, *offset, sh, 4638 (struct sctp_init_chunk *)ch, inp, 4639 stcb, &abort_no_unlock, vrf_id, port); 4640 *offset = length; 4641 if ((!abort_no_unlock) && (locked_tcb)) { 4642 SCTP_TCB_UNLOCK(locked_tcb); 4643 } 4644 return (NULL); 4645 break; 4646 case SCTP_PAD_CHUNK: 4647 break; 4648 case SCTP_INITIATION_ACK: 4649 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 4650 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4651 /* We are not interested anymore */ 4652 if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4653 ; 4654 } else { 4655 if (locked_tcb != stcb) { 4656 /* Very unlikely */ 4657 SCTP_TCB_UNLOCK(locked_tcb); 4658 } 4659 *offset = length; 4660 if (stcb) { 4661 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4662 so = SCTP_INP_SO(inp); 4663 atomic_add_int(&stcb->asoc.refcnt, 1); 4664 SCTP_TCB_UNLOCK(stcb); 4665 SCTP_SOCKET_LOCK(so, 1); 4666 SCTP_TCB_LOCK(stcb); 4667 atomic_subtract_int(&stcb->asoc.refcnt, 1); 4668 #endif 4669 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4670 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4671 SCTP_SOCKET_UNLOCK(so, 1); 4672 #endif 4673 } 4674 return (NULL); 4675 } 4676 } 4677 /* The INIT-ACK chunk must be the only chunk. */ 4678 if ((num_chunks > 1) || 4679 (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4680 *offset = length; 4681 if (locked_tcb) { 4682 SCTP_TCB_UNLOCK(locked_tcb); 4683 } 4684 return (NULL); 4685 } 4686 if ((netp) && (*netp)) { 4687 ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 4688 (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id); 4689 } else { 4690 ret = -1; 4691 } 4692 *offset = length; 4693 if (abort_no_unlock) { 4694 return (NULL); 4695 } 4696 /* 4697 * Special case, I must call the output routine to 4698 * get the cookie echoed 4699 */ 4700 if ((stcb != NULL) && (ret == 0)) { 4701 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4702 } 4703 if (locked_tcb) { 4704 SCTP_TCB_UNLOCK(locked_tcb); 4705 } 4706 return (NULL); 4707 break; 4708 case SCTP_SELECTIVE_ACK: 4709 { 4710 struct sctp_sack_chunk *sack; 4711 int abort_now = 0; 4712 uint32_t a_rwnd, cum_ack; 4713 uint16_t num_seg, num_dup; 4714 uint8_t flags; 4715 int offset_seg, offset_dup; 4716 4717 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 4718 SCTP_STAT_INCR(sctps_recvsacks); 4719 if (stcb == NULL) { 4720 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n"); 4721 break; 4722 } 4723 if (chk_length < sizeof(struct sctp_sack_chunk)) { 4724 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n"); 4725 break; 4726 } 4727 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4728 /*- 4729 * If we have sent a shutdown-ack, we will pay no 4730 * attention to a sack sent in to us since 4731 * we don't care anymore. 4732 */ 4733 break; 4734 } 4735 sack = (struct sctp_sack_chunk *)ch; 4736 flags = ch->chunk_flags; 4737 cum_ack = ntohl(sack->sack.cum_tsn_ack); 4738 num_seg = ntohs(sack->sack.num_gap_ack_blks); 4739 num_dup = ntohs(sack->sack.num_dup_tsns); 4740 a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 4741 if (sizeof(struct sctp_sack_chunk) + 4742 num_seg * sizeof(struct sctp_gap_ack_block) + 4743 num_dup * sizeof(uint32_t) != chk_length) { 4744 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n"); 4745 break; 4746 } 4747 offset_seg = *offset + sizeof(struct sctp_sack_chunk); 4748 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4749 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4750 cum_ack, num_seg, a_rwnd); 4751 stcb->asoc.seen_a_sack_this_pkt = 1; 4752 if ((stcb->asoc.pr_sctp_cnt == 0) && 4753 (num_seg == 0) && 4754 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && 4755 (stcb->asoc.saw_sack_with_frags == 0) && 4756 (stcb->asoc.saw_sack_with_nr_frags == 0) && 4757 (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 4758 ) { 4759 /* 4760 * We have a SIMPLE sack having no 4761 * prior segments and data on sent 4762 * queue to be acked.. Use the 4763 * faster path sack processing. We 4764 * also allow window update sacks 4765 * with no missing segments to go 4766 * this way too. 4767 */ 4768 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen); 4769 } else { 4770 if (netp && *netp) 4771 sctp_handle_sack(m, offset_seg, offset_dup, stcb, 4772 num_seg, 0, num_dup, &abort_now, flags, 4773 cum_ack, a_rwnd, ecne_seen); 4774 } 4775 if (abort_now) { 4776 /* ABORT signal from sack processing */ 4777 *offset = length; 4778 return (NULL); 4779 } 4780 if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4781 TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4782 (stcb->asoc.stream_queue_cnt == 0)) { 4783 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4784 } 4785 } 4786 break; 4787 /* 4788 * EY - nr_sack: If the received chunk is an 4789 * nr_sack chunk 4790 */ 4791 case SCTP_NR_SELECTIVE_ACK: 4792 { 4793 struct sctp_nr_sack_chunk *nr_sack; 4794 int abort_now = 0; 4795 uint32_t a_rwnd, cum_ack; 4796 uint16_t num_seg, num_nr_seg, num_dup; 4797 uint8_t flags; 4798 int offset_seg, offset_dup; 4799 4800 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n"); 4801 SCTP_STAT_INCR(sctps_recvsacks); 4802 if (stcb == NULL) { 4803 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n"); 4804 break; 4805 } 4806 if ((stcb->asoc.sctp_nr_sack_on_off == 0) || 4807 (stcb->asoc.peer_supports_nr_sack == 0)) { 4808 goto unknown_chunk; 4809 } 4810 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) { 4811 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n"); 4812 break; 4813 } 4814 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4815 /*- 4816 * If we have sent a shutdown-ack, we will pay no 4817 * attention to a sack sent in to us since 4818 * we don't care anymore. 4819 */ 4820 break; 4821 } 4822 nr_sack = (struct sctp_nr_sack_chunk *)ch; 4823 flags = ch->chunk_flags; 4824 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack); 4825 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks); 4826 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks); 4827 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns); 4828 a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd); 4829 if (sizeof(struct sctp_nr_sack_chunk) + 4830 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) + 4831 num_dup * sizeof(uint32_t) != chk_length) { 4832 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n"); 4833 break; 4834 } 4835 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk); 4836 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4837 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4838 cum_ack, num_seg, a_rwnd); 4839 stcb->asoc.seen_a_sack_this_pkt = 1; 4840 if ((stcb->asoc.pr_sctp_cnt == 0) && 4841 (num_seg == 0) && (num_nr_seg == 0) && 4842 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && 4843 (stcb->asoc.saw_sack_with_frags == 0) && 4844 (stcb->asoc.saw_sack_with_nr_frags == 0) && 4845 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) { 4846 /* 4847 * We have a SIMPLE sack having no 4848 * prior segments and data on sent 4849 * queue to be acked. Use the faster 4850 * path sack processing. We also 4851 * allow window update sacks with no 4852 * missing segments to go this way 4853 * too. 4854 */ 4855 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 4856 &abort_now, ecne_seen); 4857 } else { 4858 if (netp && *netp) 4859 sctp_handle_sack(m, offset_seg, offset_dup, stcb, 4860 num_seg, num_nr_seg, num_dup, &abort_now, flags, 4861 cum_ack, a_rwnd, ecne_seen); 4862 } 4863 if (abort_now) { 4864 /* ABORT signal from sack processing */ 4865 *offset = length; 4866 return (NULL); 4867 } 4868 if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4869 TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4870 (stcb->asoc.stream_queue_cnt == 0)) { 4871 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4872 } 4873 } 4874 break; 4875 4876 case SCTP_HEARTBEAT_REQUEST: 4877 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 4878 if ((stcb) && netp && *netp) { 4879 SCTP_STAT_INCR(sctps_recvheartbeat); 4880 sctp_send_heartbeat_ack(stcb, m, *offset, 4881 chk_length, *netp); 4882 4883 /* He's alive so give him credit */ 4884 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4885 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4886 stcb->asoc.overall_error_count, 4887 0, 4888 SCTP_FROM_SCTP_INPUT, 4889 __LINE__); 4890 } 4891 stcb->asoc.overall_error_count = 0; 4892 } 4893 break; 4894 case SCTP_HEARTBEAT_ACK: 4895 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 4896 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 4897 /* Its not ours */ 4898 *offset = length; 4899 if (locked_tcb) { 4900 SCTP_TCB_UNLOCK(locked_tcb); 4901 } 4902 return (NULL); 4903 } 4904 /* He's alive so give him credit */ 4905 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4906 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4907 stcb->asoc.overall_error_count, 4908 0, 4909 SCTP_FROM_SCTP_INPUT, 4910 __LINE__); 4911 } 4912 stcb->asoc.overall_error_count = 0; 4913 SCTP_STAT_INCR(sctps_recvheartbeatack); 4914 if (netp && *netp) 4915 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 4916 stcb, *netp); 4917 break; 4918 case SCTP_ABORT_ASSOCIATION: 4919 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 4920 stcb); 4921 if ((stcb) && netp && *netp) 4922 sctp_handle_abort((struct sctp_abort_chunk *)ch, 4923 stcb, *netp); 4924 *offset = length; 4925 return (NULL); 4926 break; 4927 case SCTP_SHUTDOWN: 4928 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 4929 stcb); 4930 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 4931 *offset = length; 4932 if (locked_tcb) { 4933 SCTP_TCB_UNLOCK(locked_tcb); 4934 } 4935 return (NULL); 4936 } 4937 if (netp && *netp) { 4938 int abort_flag = 0; 4939 4940 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 4941 stcb, *netp, &abort_flag); 4942 if (abort_flag) { 4943 *offset = length; 4944 return (NULL); 4945 } 4946 } 4947 break; 4948 case SCTP_SHUTDOWN_ACK: 4949 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb); 4950 if ((stcb) && (netp) && (*netp)) 4951 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 4952 *offset = length; 4953 return (NULL); 4954 break; 4955 4956 case SCTP_OPERATION_ERROR: 4957 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 4958 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 4959 *offset = length; 4960 return (NULL); 4961 } 4962 break; 4963 case SCTP_COOKIE_ECHO: 4964 SCTPDBG(SCTP_DEBUG_INPUT3, 4965 "SCTP_COOKIE-ECHO, stcb %p\n", stcb); 4966 if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4967 ; 4968 } else { 4969 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4970 /* We are not interested anymore */ 4971 abend: 4972 if (stcb) { 4973 SCTP_TCB_UNLOCK(stcb); 4974 } 4975 *offset = length; 4976 return (NULL); 4977 } 4978 } 4979 /* 4980 * First are we accepting? We do this again here 4981 * since it is possible that a previous endpoint WAS 4982 * listening responded to a INIT-ACK and then 4983 * closed. We opened and bound.. and are now no 4984 * longer listening. 4985 */ 4986 4987 if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) { 4988 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 4989 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) { 4990 struct mbuf *op_err; 4991 4992 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 4993 sctp_abort_association(inp, stcb, m, 4994 iphlen, sh, op_err, vrf_id, port); 4995 } 4996 *offset = length; 4997 return (NULL); 4998 } else { 4999 struct mbuf *ret_buf; 5000 struct sctp_inpcb *linp; 5001 5002 if (stcb) { 5003 linp = NULL; 5004 } else { 5005 linp = inp; 5006 } 5007 5008 if (linp) { 5009 SCTP_ASOC_CREATE_LOCK(linp); 5010 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 5011 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { 5012 SCTP_ASOC_CREATE_UNLOCK(linp); 5013 goto abend; 5014 } 5015 } 5016 if (netp) { 5017 ret_buf = 5018 sctp_handle_cookie_echo(m, iphlen, 5019 *offset, sh, 5020 (struct sctp_cookie_echo_chunk *)ch, 5021 &inp, &stcb, netp, 5022 auth_skipped, 5023 auth_offset, 5024 auth_len, 5025 &locked_tcb, 5026 vrf_id, 5027 port); 5028 } else { 5029 ret_buf = NULL; 5030 } 5031 if (linp) { 5032 SCTP_ASOC_CREATE_UNLOCK(linp); 5033 } 5034 if (ret_buf == NULL) { 5035 if (locked_tcb) { 5036 SCTP_TCB_UNLOCK(locked_tcb); 5037 } 5038 SCTPDBG(SCTP_DEBUG_INPUT3, 5039 "GAK, null buffer\n"); 5040 *offset = length; 5041 return (NULL); 5042 } 5043 /* if AUTH skipped, see if it verified... */ 5044 if (auth_skipped) { 5045 got_auth = 1; 5046 auth_skipped = 0; 5047 } 5048 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 5049 /* 5050 * Restart the timer if we have 5051 * pending data 5052 */ 5053 struct sctp_tmit_chunk *chk; 5054 5055 chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 5056 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 5057 } 5058 } 5059 break; 5060 case SCTP_COOKIE_ACK: 5061 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb); 5062 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 5063 if (locked_tcb) { 5064 SCTP_TCB_UNLOCK(locked_tcb); 5065 } 5066 return (NULL); 5067 } 5068 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5069 /* We are not interested anymore */ 5070 if ((stcb) && (stcb->asoc.total_output_queue_size)) { 5071 ; 5072 } else if (stcb) { 5073 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5074 so = SCTP_INP_SO(inp); 5075 atomic_add_int(&stcb->asoc.refcnt, 1); 5076 SCTP_TCB_UNLOCK(stcb); 5077 SCTP_SOCKET_LOCK(so, 1); 5078 SCTP_TCB_LOCK(stcb); 5079 atomic_subtract_int(&stcb->asoc.refcnt, 1); 5080 #endif 5081 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 5082 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5083 SCTP_SOCKET_UNLOCK(so, 1); 5084 #endif 5085 *offset = length; 5086 return (NULL); 5087 } 5088 } 5089 /* He's alive so give him credit */ 5090 if ((stcb) && netp && *netp) { 5091 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5092 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5093 stcb->asoc.overall_error_count, 5094 0, 5095 SCTP_FROM_SCTP_INPUT, 5096 __LINE__); 5097 } 5098 stcb->asoc.overall_error_count = 0; 5099 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 5100 } 5101 break; 5102 case SCTP_ECN_ECHO: 5103 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 5104 /* He's alive so give him credit */ 5105 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 5106 /* Its not ours */ 5107 if (locked_tcb) { 5108 SCTP_TCB_UNLOCK(locked_tcb); 5109 } 5110 *offset = length; 5111 return (NULL); 5112 } 5113 if (stcb) { 5114 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5115 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5116 stcb->asoc.overall_error_count, 5117 0, 5118 SCTP_FROM_SCTP_INPUT, 5119 __LINE__); 5120 } 5121 stcb->asoc.overall_error_count = 0; 5122 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 5123 stcb); 5124 ecne_seen = 1; 5125 } 5126 break; 5127 case SCTP_ECN_CWR: 5128 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 5129 /* He's alive so give him credit */ 5130 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 5131 /* Its not ours */ 5132 if (locked_tcb) { 5133 SCTP_TCB_UNLOCK(locked_tcb); 5134 } 5135 *offset = length; 5136 return (NULL); 5137 } 5138 if (stcb) { 5139 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5140 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5141 stcb->asoc.overall_error_count, 5142 0, 5143 SCTP_FROM_SCTP_INPUT, 5144 __LINE__); 5145 } 5146 stcb->asoc.overall_error_count = 0; 5147 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp); 5148 } 5149 break; 5150 case SCTP_SHUTDOWN_COMPLETE: 5151 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb); 5152 /* must be first and only chunk */ 5153 if ((num_chunks > 1) || 5154 (length - *offset > (int)SCTP_SIZE32(chk_length))) { 5155 *offset = length; 5156 if (locked_tcb) { 5157 SCTP_TCB_UNLOCK(locked_tcb); 5158 } 5159 return (NULL); 5160 } 5161 if ((stcb) && netp && *netp) { 5162 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 5163 stcb, *netp); 5164 } 5165 *offset = length; 5166 return (NULL); 5167 break; 5168 case SCTP_ASCONF: 5169 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 5170 /* He's alive so give him credit */ 5171 if (stcb) { 5172 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5173 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5174 stcb->asoc.overall_error_count, 5175 0, 5176 SCTP_FROM_SCTP_INPUT, 5177 __LINE__); 5178 } 5179 stcb->asoc.overall_error_count = 0; 5180 sctp_handle_asconf(m, *offset, 5181 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0); 5182 asconf_cnt++; 5183 } 5184 break; 5185 case SCTP_ASCONF_ACK: 5186 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 5187 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 5188 /* Its not ours */ 5189 if (locked_tcb) { 5190 SCTP_TCB_UNLOCK(locked_tcb); 5191 } 5192 *offset = length; 5193 return (NULL); 5194 } 5195 if ((stcb) && netp && *netp) { 5196 /* He's alive so give him credit */ 5197 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5198 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5199 stcb->asoc.overall_error_count, 5200 0, 5201 SCTP_FROM_SCTP_INPUT, 5202 __LINE__); 5203 } 5204 stcb->asoc.overall_error_count = 0; 5205 sctp_handle_asconf_ack(m, *offset, 5206 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock); 5207 if (abort_no_unlock) 5208 return (NULL); 5209 } 5210 break; 5211 case SCTP_FORWARD_CUM_TSN: 5212 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 5213 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 5214 /* Its not ours */ 5215 if (locked_tcb) { 5216 SCTP_TCB_UNLOCK(locked_tcb); 5217 } 5218 *offset = length; 5219 return (NULL); 5220 } 5221 /* He's alive so give him credit */ 5222 if (stcb) { 5223 int abort_flag = 0; 5224 5225 stcb->asoc.overall_error_count = 0; 5226 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5227 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5228 stcb->asoc.overall_error_count, 5229 0, 5230 SCTP_FROM_SCTP_INPUT, 5231 __LINE__); 5232 } 5233 *fwd_tsn_seen = 1; 5234 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5235 /* We are not interested anymore */ 5236 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5237 so = SCTP_INP_SO(inp); 5238 atomic_add_int(&stcb->asoc.refcnt, 1); 5239 SCTP_TCB_UNLOCK(stcb); 5240 SCTP_SOCKET_LOCK(so, 1); 5241 SCTP_TCB_LOCK(stcb); 5242 atomic_subtract_int(&stcb->asoc.refcnt, 1); 5243 #endif 5244 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 5245 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5246 SCTP_SOCKET_UNLOCK(so, 1); 5247 #endif 5248 *offset = length; 5249 return (NULL); 5250 } 5251 sctp_handle_forward_tsn(stcb, 5252 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); 5253 if (abort_flag) { 5254 *offset = length; 5255 return (NULL); 5256 } else { 5257 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5258 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5259 stcb->asoc.overall_error_count, 5260 0, 5261 SCTP_FROM_SCTP_INPUT, 5262 __LINE__); 5263 } 5264 stcb->asoc.overall_error_count = 0; 5265 } 5266 5267 } 5268 break; 5269 case SCTP_STREAM_RESET: 5270 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 5271 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 5272 /* Its not ours */ 5273 if (locked_tcb) { 5274 SCTP_TCB_UNLOCK(locked_tcb); 5275 } 5276 *offset = length; 5277 return (NULL); 5278 } 5279 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5280 /* We are not interested anymore */ 5281 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5282 so = SCTP_INP_SO(inp); 5283 atomic_add_int(&stcb->asoc.refcnt, 1); 5284 SCTP_TCB_UNLOCK(stcb); 5285 SCTP_SOCKET_LOCK(so, 1); 5286 SCTP_TCB_LOCK(stcb); 5287 atomic_subtract_int(&stcb->asoc.refcnt, 1); 5288 #endif 5289 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30); 5290 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5291 SCTP_SOCKET_UNLOCK(so, 1); 5292 #endif 5293 *offset = length; 5294 return (NULL); 5295 } 5296 if (stcb->asoc.peer_supports_strreset == 0) { 5297 /* 5298 * hmm, peer should have announced this, but 5299 * we will turn it on since he is sending us 5300 * a stream reset. 5301 */ 5302 stcb->asoc.peer_supports_strreset = 1; 5303 } 5304 if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) { 5305 /* stop processing */ 5306 *offset = length; 5307 return (NULL); 5308 } 5309 break; 5310 case SCTP_PACKET_DROPPED: 5311 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 5312 /* re-get it all please */ 5313 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 5314 /* Its not ours */ 5315 if (locked_tcb) { 5316 SCTP_TCB_UNLOCK(locked_tcb); 5317 } 5318 *offset = length; 5319 return (NULL); 5320 } 5321 if (ch && (stcb) && netp && (*netp)) { 5322 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 5323 stcb, *netp, 5324 min(chk_length, (sizeof(chunk_buf) - 4))); 5325 5326 } 5327 break; 5328 5329 case SCTP_AUTHENTICATION: 5330 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 5331 if (SCTP_BASE_SYSCTL(sctp_auth_disable)) 5332 goto unknown_chunk; 5333 5334 if (stcb == NULL) { 5335 /* save the first AUTH for later processing */ 5336 if (auth_skipped == 0) { 5337 auth_offset = *offset; 5338 auth_len = chk_length; 5339 auth_skipped = 1; 5340 } 5341 /* skip this chunk (temporarily) */ 5342 goto next_chunk; 5343 } 5344 if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 5345 (chk_length > (sizeof(struct sctp_auth_chunk) + 5346 SCTP_AUTH_DIGEST_LEN_MAX))) { 5347 /* Its not ours */ 5348 if (locked_tcb) { 5349 SCTP_TCB_UNLOCK(locked_tcb); 5350 } 5351 *offset = length; 5352 return (NULL); 5353 } 5354 if (got_auth == 1) { 5355 /* skip this chunk... it's already auth'd */ 5356 goto next_chunk; 5357 } 5358 got_auth = 1; 5359 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 5360 m, *offset)) { 5361 /* auth HMAC failed so dump the packet */ 5362 *offset = length; 5363 return (stcb); 5364 } else { 5365 /* remaining chunks are HMAC checked */ 5366 stcb->asoc.authenticated = 1; 5367 } 5368 break; 5369 5370 default: 5371 unknown_chunk: 5372 /* it's an unknown chunk! */ 5373 if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 5374 struct mbuf *mm; 5375 struct sctp_paramhdr *phd; 5376 5377 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 5378 0, M_DONTWAIT, 1, MT_DATA); 5379 if (mm) { 5380 phd = mtod(mm, struct sctp_paramhdr *); 5381 /* 5382 * We cheat and use param type since 5383 * we did not bother to define a 5384 * error cause struct. They are the 5385 * same basic format with different 5386 * names. 5387 */ 5388 phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 5389 phd->param_length = htons(chk_length + sizeof(*phd)); 5390 SCTP_BUF_LEN(mm) = sizeof(*phd); 5391 SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length), 5392 M_DONTWAIT); 5393 if (SCTP_BUF_NEXT(mm)) { 5394 #ifdef SCTP_MBUF_LOGGING 5395 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5396 struct mbuf *mat; 5397 5398 mat = SCTP_BUF_NEXT(mm); 5399 while (mat) { 5400 if (SCTP_BUF_IS_EXTENDED(mat)) { 5401 sctp_log_mb(mat, SCTP_MBUF_ICOPY); 5402 } 5403 mat = SCTP_BUF_NEXT(mat); 5404 } 5405 } 5406 #endif 5407 sctp_queue_op_err(stcb, mm); 5408 } else { 5409 sctp_m_freem(mm); 5410 } 5411 } 5412 } 5413 if ((ch->chunk_type & 0x80) == 0) { 5414 /* discard this packet */ 5415 *offset = length; 5416 return (stcb); 5417 } /* else skip this bad chunk and continue... */ 5418 break; 5419 } /* switch (ch->chunk_type) */ 5420 5421 5422 next_chunk: 5423 /* get the next chunk */ 5424 *offset += SCTP_SIZE32(chk_length); 5425 if (*offset >= length) { 5426 /* no more data left in the mbuf chain */ 5427 break; 5428 } 5429 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 5430 sizeof(struct sctp_chunkhdr), chunk_buf); 5431 if (ch == NULL) { 5432 if (locked_tcb) { 5433 SCTP_TCB_UNLOCK(locked_tcb); 5434 } 5435 *offset = length; 5436 return (NULL); 5437 } 5438 } /* while */ 5439 5440 if (asconf_cnt > 0 && stcb != NULL) { 5441 sctp_send_asconf_ack(stcb); 5442 } 5443 return (stcb); 5444 } 5445 5446 5447 #ifdef INVARIANTS 5448 #ifdef __GNUC__ 5449 __attribute__((noinline)) 5450 #endif 5451 void 5452 sctp_validate_no_locks(struct sctp_inpcb *inp) 5453 { 5454 struct sctp_tcb *lstcb; 5455 5456 LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) { 5457 if (mtx_owned(&lstcb->tcb_mtx)) { 5458 panic("Own lock on stcb at return from input"); 5459 } 5460 } 5461 if (mtx_owned(&inp->inp_create_mtx)) { 5462 panic("Own create lock on inp"); 5463 } 5464 if (mtx_owned(&inp->inp_mtx)) { 5465 panic("Own inp lock on inp"); 5466 } 5467 } 5468 5469 #endif 5470 5471 /* 5472 * common input chunk processing (v4 and v6) 5473 */ 5474 void 5475 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 5476 int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 5477 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 5478 uint8_t ecn_bits, uint32_t vrf_id, uint16_t port) 5479 { 5480 /* 5481 * Control chunk processing 5482 */ 5483 uint32_t high_tsn; 5484 int fwd_tsn_seen = 0, data_processed = 0; 5485 struct mbuf *m = *mm; 5486 int un_sent; 5487 int cnt_ctrl_ready = 0; 5488 5489 SCTP_STAT_INCR(sctps_recvdatagrams); 5490 #ifdef SCTP_AUDITING_ENABLED 5491 sctp_audit_log(0xE0, 1); 5492 sctp_auditing(0, inp, stcb, net); 5493 #endif 5494 5495 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n", 5496 m, iphlen, offset, length, stcb); 5497 if (stcb) { 5498 /* always clear this before beginning a packet */ 5499 stcb->asoc.authenticated = 0; 5500 stcb->asoc.seen_a_sack_this_pkt = 0; 5501 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n", 5502 stcb, stcb->asoc.state); 5503 5504 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) || 5505 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { 5506 /*- 5507 * If we hit here, we had a ref count 5508 * up when the assoc was aborted and the 5509 * timer is clearing out the assoc, we should 5510 * NOT respond to any packet.. its OOTB. 5511 */ 5512 SCTP_TCB_UNLOCK(stcb); 5513 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5514 vrf_id, port); 5515 goto out_now; 5516 } 5517 } 5518 if (IS_SCTP_CONTROL(ch)) { 5519 /* process the control portion of the SCTP packet */ 5520 /* sa_ignore NO_NULL_CHK */ 5521 stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 5522 inp, stcb, &net, &fwd_tsn_seen, vrf_id, port); 5523 if (stcb) { 5524 /* 5525 * This covers us if the cookie-echo was there and 5526 * it changes our INP. 5527 */ 5528 inp = stcb->sctp_ep; 5529 if ((net) && (port)) { 5530 if (net->port == 0) { 5531 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); 5532 } 5533 net->port = port; 5534 } 5535 } 5536 } else { 5537 /* 5538 * no control chunks, so pre-process DATA chunks (these 5539 * checks are taken care of by control processing) 5540 */ 5541 5542 /* 5543 * if DATA only packet, and auth is required, then punt... 5544 * can't have authenticated without any AUTH (control) 5545 * chunks 5546 */ 5547 if ((stcb != NULL) && 5548 !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5549 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) { 5550 /* "silently" ignore */ 5551 SCTP_STAT_INCR(sctps_recvauthmissing); 5552 SCTP_TCB_UNLOCK(stcb); 5553 goto out_now; 5554 } 5555 if (stcb == NULL) { 5556 /* out of the blue DATA chunk */ 5557 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5558 vrf_id, port); 5559 goto out_now; 5560 } 5561 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 5562 /* v_tag mismatch! */ 5563 SCTP_STAT_INCR(sctps_badvtag); 5564 SCTP_TCB_UNLOCK(stcb); 5565 goto out_now; 5566 } 5567 } 5568 5569 if (stcb == NULL) { 5570 /* 5571 * no valid TCB for this packet, or we found it's a bad 5572 * packet while processing control, or we're done with this 5573 * packet (done or skip rest of data), so we drop it... 5574 */ 5575 goto out_now; 5576 } 5577 /* 5578 * DATA chunk processing 5579 */ 5580 /* plow through the data chunks while length > offset */ 5581 5582 /* 5583 * Rest should be DATA only. Check authentication state if AUTH for 5584 * DATA is required. 5585 */ 5586 if ((length > offset) && 5587 (stcb != NULL) && 5588 !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5589 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) && 5590 !stcb->asoc.authenticated) { 5591 /* "silently" ignore */ 5592 SCTP_STAT_INCR(sctps_recvauthmissing); 5593 SCTPDBG(SCTP_DEBUG_AUTH1, 5594 "Data chunk requires AUTH, skipped\n"); 5595 goto trigger_send; 5596 } 5597 if (length > offset) { 5598 int retval; 5599 5600 /* 5601 * First check to make sure our state is correct. We would 5602 * not get here unless we really did have a tag, so we don't 5603 * abort if this happens, just dump the chunk silently. 5604 */ 5605 switch (SCTP_GET_STATE(&stcb->asoc)) { 5606 case SCTP_STATE_COOKIE_ECHOED: 5607 /* 5608 * we consider data with valid tags in this state 5609 * shows us the cookie-ack was lost. Imply it was 5610 * there. 5611 */ 5612 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5613 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5614 stcb->asoc.overall_error_count, 5615 0, 5616 SCTP_FROM_SCTP_INPUT, 5617 __LINE__); 5618 } 5619 stcb->asoc.overall_error_count = 0; 5620 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 5621 break; 5622 case SCTP_STATE_COOKIE_WAIT: 5623 /* 5624 * We consider OOTB any data sent during asoc setup. 5625 */ 5626 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5627 vrf_id, port); 5628 SCTP_TCB_UNLOCK(stcb); 5629 goto out_now; 5630 /* sa_ignore NOTREACHED */ 5631 break; 5632 case SCTP_STATE_EMPTY: /* should not happen */ 5633 case SCTP_STATE_INUSE: /* should not happen */ 5634 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 5635 case SCTP_STATE_SHUTDOWN_ACK_SENT: 5636 default: 5637 SCTP_TCB_UNLOCK(stcb); 5638 goto out_now; 5639 /* sa_ignore NOTREACHED */ 5640 break; 5641 case SCTP_STATE_OPEN: 5642 case SCTP_STATE_SHUTDOWN_SENT: 5643 break; 5644 } 5645 /* plow through the data chunks while length > offset */ 5646 retval = sctp_process_data(mm, iphlen, &offset, length, sh, 5647 inp, stcb, net, &high_tsn); 5648 if (retval == 2) { 5649 /* 5650 * The association aborted, NO UNLOCK needed since 5651 * the association is destroyed. 5652 */ 5653 goto out_now; 5654 } 5655 data_processed = 1; 5656 /* 5657 * Anything important needs to have been m_copy'ed in 5658 * process_data 5659 */ 5660 } 5661 /* take care of ecn */ 5662 if ((data_processed == 1) && 5663 (stcb->asoc.ecn_allowed == 1) && 5664 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) { 5665 /* Yep, we need to add a ECNE */ 5666 sctp_send_ecn_echo(stcb, net, high_tsn); 5667 } 5668 if ((data_processed == 0) && (fwd_tsn_seen)) { 5669 int was_a_gap; 5670 uint32_t highest_tsn; 5671 5672 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) { 5673 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map; 5674 } else { 5675 highest_tsn = stcb->asoc.highest_tsn_inside_map; 5676 } 5677 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn); 5678 stcb->asoc.send_sack = 1; 5679 sctp_sack_check(stcb, was_a_gap); 5680 } else if (fwd_tsn_seen) { 5681 stcb->asoc.send_sack = 1; 5682 } 5683 /* trigger send of any chunks in queue... */ 5684 trigger_send: 5685 #ifdef SCTP_AUDITING_ENABLED 5686 sctp_audit_log(0xE0, 2); 5687 sctp_auditing(1, inp, stcb, net); 5688 #endif 5689 SCTPDBG(SCTP_DEBUG_INPUT1, 5690 "Check for chunk output prw:%d tqe:%d tf=%d\n", 5691 stcb->asoc.peers_rwnd, 5692 TAILQ_EMPTY(&stcb->asoc.control_send_queue), 5693 stcb->asoc.total_flight); 5694 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 5695 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 5696 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq; 5697 } 5698 if (cnt_ctrl_ready || 5699 ((un_sent) && 5700 (stcb->asoc.peers_rwnd > 0 || 5701 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 5702 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 5703 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 5704 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 5705 } 5706 #ifdef SCTP_AUDITING_ENABLED 5707 sctp_audit_log(0xE0, 3); 5708 sctp_auditing(2, inp, stcb, net); 5709 #endif 5710 SCTP_TCB_UNLOCK(stcb); 5711 out_now: 5712 #ifdef INVARIANTS 5713 sctp_validate_no_locks(inp); 5714 #endif 5715 return; 5716 } 5717 5718 #if 0 5719 static void 5720 sctp_print_mbuf_chain(struct mbuf *m) 5721 { 5722 for (; m; m = SCTP_BUF_NEXT(m)) { 5723 printf("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m)); 5724 if (SCTP_BUF_IS_EXTENDED(m)) 5725 printf("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m)); 5726 } 5727 } 5728 5729 #endif 5730 5731 #ifdef INET 5732 void 5733 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port) 5734 { 5735 #ifdef SCTP_MBUF_LOGGING 5736 struct mbuf *mat; 5737 5738 #endif 5739 struct mbuf *m; 5740 int iphlen; 5741 uint32_t vrf_id = 0; 5742 uint8_t ecn_bits; 5743 struct ip *ip; 5744 struct sctphdr *sh; 5745 struct sctp_inpcb *inp = NULL; 5746 struct sctp_nets *net; 5747 struct sctp_tcb *stcb = NULL; 5748 struct sctp_chunkhdr *ch; 5749 int refcount_up = 0; 5750 int length, mlen, offset; 5751 5752 #if !defined(SCTP_WITH_NO_CSUM) 5753 uint32_t check, calc_check; 5754 5755 #endif 5756 5757 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 5758 SCTP_RELEASE_PKT(i_pak); 5759 return; 5760 } 5761 mlen = SCTP_HEADER_LEN(i_pak); 5762 iphlen = off; 5763 m = SCTP_HEADER_TO_CHAIN(i_pak); 5764 5765 net = NULL; 5766 SCTP_STAT_INCR(sctps_recvpackets); 5767 SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 5768 5769 5770 #ifdef SCTP_MBUF_LOGGING 5771 /* Log in any input mbufs */ 5772 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5773 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) { 5774 if (SCTP_BUF_IS_EXTENDED(mat)) { 5775 sctp_log_mb(mat, SCTP_MBUF_INPUT); 5776 } 5777 } 5778 } 5779 #endif 5780 #ifdef SCTP_PACKET_LOGGING 5781 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 5782 sctp_packet_log(m, mlen); 5783 #endif 5784 /* 5785 * Must take out the iphlen, since mlen expects this (only effect lb 5786 * case) 5787 */ 5788 mlen -= iphlen; 5789 5790 /* 5791 * Get IP, SCTP, and first chunk header together in first mbuf. 5792 */ 5793 ip = mtod(m, struct ip *); 5794 offset = iphlen + sizeof(*sh) + sizeof(*ch); 5795 if (SCTP_BUF_LEN(m) < offset) { 5796 if ((m = m_pullup(m, offset)) == 0) { 5797 SCTP_STAT_INCR(sctps_hdrops); 5798 return; 5799 } 5800 ip = mtod(m, struct ip *); 5801 } 5802 /* validate mbuf chain length with IP payload length */ 5803 if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) { 5804 SCTP_STAT_INCR(sctps_hdrops); 5805 goto bad; 5806 } 5807 sh = (struct sctphdr *)((caddr_t)ip + iphlen); 5808 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 5809 SCTPDBG(SCTP_DEBUG_INPUT1, 5810 "sctp_input() length:%d iphlen:%d\n", mlen, iphlen); 5811 5812 /* SCTP does not allow broadcasts or multicasts */ 5813 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 5814 goto bad; 5815 } 5816 if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 5817 /* 5818 * We only look at broadcast if its a front state, All 5819 * others we will not have a tcb for anyway. 5820 */ 5821 goto bad; 5822 } 5823 /* validate SCTP checksum */ 5824 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, 5825 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", 5826 m->m_pkthdr.len, 5827 if_name(m->m_pkthdr.rcvif), 5828 m->m_pkthdr.csum_flags); 5829 #if defined(SCTP_WITH_NO_CSUM) 5830 SCTP_STAT_INCR(sctps_recvnocrc); 5831 #else 5832 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { 5833 SCTP_STAT_INCR(sctps_recvhwcrc); 5834 goto sctp_skip_csum_4; 5835 } 5836 check = sh->checksum; /* save incoming checksum */ 5837 sh->checksum = 0; /* prepare for calc */ 5838 calc_check = sctp_calculate_cksum(m, iphlen); 5839 sh->checksum = check; 5840 SCTP_STAT_INCR(sctps_recvswcrc); 5841 if (calc_check != check) { 5842 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 5843 calc_check, check, m, mlen, iphlen); 5844 5845 stcb = sctp_findassociation_addr(m, 5846 offset - sizeof(*ch), 5847 sh, ch, &inp, &net, 5848 vrf_id); 5849 if ((net) && (port)) { 5850 if (net->port == 0) { 5851 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); 5852 } 5853 net->port = port; 5854 } 5855 if ((net != NULL) && (m->m_flags & M_FLOWID)) { 5856 net->flowid = m->m_pkthdr.flowid; 5857 #ifdef INVARIANTS 5858 net->flowidset = 1; 5859 #endif 5860 } 5861 if ((inp) && (stcb)) { 5862 sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 5863 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 5864 } else if ((inp != NULL) && (stcb == NULL)) { 5865 refcount_up = 1; 5866 } 5867 SCTP_STAT_INCR(sctps_badsum); 5868 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 5869 goto bad; 5870 } 5871 sctp_skip_csum_4: 5872 #endif 5873 /* destination port of 0 is illegal, based on RFC2960. */ 5874 if (sh->dest_port == 0) { 5875 SCTP_STAT_INCR(sctps_hdrops); 5876 goto bad; 5877 } 5878 /* 5879 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 5880 * IP/SCTP/first chunk header... 5881 */ 5882 stcb = sctp_findassociation_addr(m, offset - sizeof(*ch), 5883 sh, ch, &inp, &net, vrf_id); 5884 if ((net) && (port)) { 5885 if (net->port == 0) { 5886 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); 5887 } 5888 net->port = port; 5889 } 5890 if ((net != NULL) && (m->m_flags & M_FLOWID)) { 5891 net->flowid = m->m_pkthdr.flowid; 5892 #ifdef INVARIANTS 5893 net->flowidset = 1; 5894 #endif 5895 } 5896 /* inp's ref-count increased && stcb locked */ 5897 if (inp == NULL) { 5898 struct sctp_init_chunk *init_chk, chunk_buf; 5899 5900 SCTP_STAT_INCR(sctps_noport); 5901 #ifdef ICMP_BANDLIM 5902 /* 5903 * we use the bandwidth limiting to protect against sending 5904 * too many ABORTS all at once. In this case these count the 5905 * same as an ICMP message. 5906 */ 5907 if (badport_bandlim(0) < 0) 5908 goto bad; 5909 #endif /* ICMP_BANDLIM */ 5910 SCTPDBG(SCTP_DEBUG_INPUT1, 5911 "Sending a ABORT from packet entry!\n"); 5912 if (ch->chunk_type == SCTP_INITIATION) { 5913 /* 5914 * we do a trick here to get the INIT tag, dig in 5915 * and get the tag from the INIT and put it in the 5916 * common header. 5917 */ 5918 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 5919 iphlen + sizeof(*sh), sizeof(*init_chk), 5920 (uint8_t *) & chunk_buf); 5921 if (init_chk != NULL) 5922 sh->v_tag = init_chk->init.initiate_tag; 5923 } 5924 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 5925 sctp_send_shutdown_complete2(m, sh, vrf_id, port); 5926 goto bad; 5927 } 5928 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 5929 goto bad; 5930 } 5931 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) { 5932 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) || 5933 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) && 5934 (ch->chunk_type != SCTP_INIT))) { 5935 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port); 5936 } 5937 } 5938 goto bad; 5939 } else if (stcb == NULL) { 5940 refcount_up = 1; 5941 } 5942 #ifdef IPSEC 5943 /* 5944 * I very much doubt any of the IPSEC stuff will work but I have no 5945 * idea, so I will leave it in place. 5946 */ 5947 if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 5948 MODULE_GLOBAL(ipsec4stat).in_polvio++; 5949 SCTP_STAT_INCR(sctps_hdrops); 5950 goto bad; 5951 } 5952 #endif /* IPSEC */ 5953 5954 /* 5955 * common chunk processing 5956 */ 5957 length = ip->ip_len + iphlen; 5958 offset -= sizeof(struct sctp_chunkhdr); 5959 5960 ecn_bits = ip->ip_tos; 5961 5962 /* sa_ignore NO_NULL_CHK */ 5963 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 5964 inp, stcb, net, ecn_bits, vrf_id, port); 5965 /* inp's ref-count reduced && stcb unlocked */ 5966 if (m) { 5967 sctp_m_freem(m); 5968 } 5969 if ((inp) && (refcount_up)) { 5970 /* reduce ref-count */ 5971 SCTP_INP_DECR_REF(inp); 5972 } 5973 return; 5974 bad: 5975 if (stcb) { 5976 SCTP_TCB_UNLOCK(stcb); 5977 } 5978 if ((inp) && (refcount_up)) { 5979 /* reduce ref-count */ 5980 SCTP_INP_DECR_REF(inp); 5981 } 5982 if (m) { 5983 sctp_m_freem(m); 5984 } 5985 return; 5986 } 5987 5988 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) 5989 extern int *sctp_cpuarry; 5990 5991 #endif 5992 5993 void 5994 sctp_input(struct mbuf *m, int off) 5995 { 5996 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) 5997 struct ip *ip; 5998 struct sctphdr *sh; 5999 int offset; 6000 int cpu_to_use; 6001 uint32_t flowid, tag; 6002 6003 if (mp_ncpus > 1) { 6004 if (m->m_flags & M_FLOWID) { 6005 flowid = m->m_pkthdr.flowid; 6006 } else { 6007 /* 6008 * No flow id built by lower layers fix it so we 6009 * create one. 6010 */ 6011 ip = mtod(m, struct ip *); 6012 offset = off + sizeof(*sh); 6013 if (SCTP_BUF_LEN(m) < offset) { 6014 if ((m = m_pullup(m, offset)) == 0) { 6015 SCTP_STAT_INCR(sctps_hdrops); 6016 return; 6017 } 6018 ip = mtod(m, struct ip *); 6019 } 6020 sh = (struct sctphdr *)((caddr_t)ip + off); 6021 tag = htonl(sh->v_tag); 6022 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port); 6023 m->m_pkthdr.flowid = flowid; 6024 m->m_flags |= M_FLOWID; 6025 } 6026 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus]; 6027 sctp_queue_to_mcore(m, off, cpu_to_use); 6028 return; 6029 } 6030 #endif 6031 sctp_input_with_port(m, off, 0); 6032 } 6033 6034 #endif 6035