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