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