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