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