2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * a) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
14 * b) Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the distribution.
18 * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <netinet/sctp_os.h>
39 #include <netinet/sctp_var.h>
40 #include <netinet/sctp_sysctl.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctp_header.h>
43 #include <netinet/sctputil.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_input.h>
46 #include <netinet/sctp_auth.h>
47 #include <netinet/sctp_indata.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctp_bsd_addr.h>
50 #include <netinet/sctp_timer.h>
51 #include <netinet/sctp_crc32.h>
52 #if defined(INET) || defined(INET6)
53 #include <netinet/udp.h>
60 sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
62 struct sctp_nets *net;
65 * This now not only stops all cookie timers it also stops any INIT
66 * timers as well. This will make sure that the timers are stopped
67 * in all collision cases.
69 SCTP_TCB_LOCK_ASSERT(stcb);
70 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
71 if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
72 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
75 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
76 } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
77 sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
80 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
87 sctp_handle_init(struct mbuf *m, int iphlen, int offset,
88 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
89 struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
90 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_no_unlock,
91 uint8_t mflowtype, uint32_t mflowid,
92 uint32_t vrf_id, uint16_t port)
94 struct sctp_init *init;
97 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
102 /* validate length */
103 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
104 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
105 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
109 *abort_no_unlock = 1;
112 /* validate parameters */
114 if (init->initiate_tag == 0) {
115 /* protocol error... send abort */
116 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
117 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
121 *abort_no_unlock = 1;
124 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
125 /* invalid parameter... send abort */
126 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
127 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
131 *abort_no_unlock = 1;
134 if (init->num_inbound_streams == 0) {
135 /* protocol error... send abort */
136 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
137 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
141 *abort_no_unlock = 1;
144 if (init->num_outbound_streams == 0) {
145 /* protocol error... send abort */
146 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
147 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
151 *abort_no_unlock = 1;
154 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
155 offset + ntohs(cp->ch.chunk_length))) {
156 /* auth parameter(s) error... send abort */
157 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
158 "Problem with AUTH parameters");
159 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
163 *abort_no_unlock = 1;
166 /* We are only accepting if we have a listening socket. */
167 if ((stcb == NULL) &&
168 ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
169 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
170 (!SCTP_IS_LISTENING(inp)))) {
172 * FIX ME ?? What about TCP model and we have a
173 * match/restart case? Actually no fix is needed. the lookup
174 * will always find the existing assoc so stcb would not be
175 * NULL. It may be questionable to do this since we COULD
176 * just send back the INIT-ACK and hope that the app did
177 * accept()'s by the time the COOKIE was sent. But there is
178 * a price to pay for COOKIE generation and I don't want to
179 * pay it on the chance that the app will actually do some
180 * accepts(). The App just looses and should NOT be in this
183 if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
184 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
186 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
187 mflowtype, mflowid, inp->fibnum,
192 if ((stcb != NULL) &&
193 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
194 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
195 sctp_send_shutdown_ack(stcb, NULL);
196 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
198 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
199 sctp_send_initiate_ack(inp, stcb, net, m, iphlen, offset,
206 SCTP_INP_RUNLOCK(inp);
211 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
215 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
216 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
223 struct sctp_stream_queue_pending *sp;
224 struct sctp_association *asoc;
227 * This function returns if any stream has true unsent data on it.
228 * Note that as it looks through it will clean up any places that
229 * have old data that has been sent but left at top of stream queue.
233 SCTP_TCB_SEND_LOCK(stcb);
234 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
235 /* Check to see if some data queued */
236 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
237 /* sa_ignore FREED_MEMORY */
238 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
242 if ((sp->msg_is_complete) &&
244 (sp->sender_all_done)) {
246 * We are doing differed cleanup. Last time
247 * through when we took all the data the
248 * sender_all_done was not set.
250 if (sp->put_last_out == 0) {
251 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
252 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
258 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
259 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
260 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, &asoc->strmout[i], sp, 1);
262 sctp_free_remote_addr(sp->net);
266 sctp_m_freem(sp->data);
269 sctp_free_a_strmoq(stcb, sp, so_locked);
270 if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
276 if (unsent_data > 0) {
281 SCTP_TCB_SEND_UNLOCK(stcb);
282 return (unsent_data);
286 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
288 struct sctp_init *init;
289 struct sctp_association *asoc;
290 struct sctp_nets *lnet;
295 /* save off parameters */
296 asoc->peer_vtag = ntohl(init->initiate_tag);
297 asoc->peers_rwnd = ntohl(init->a_rwnd);
299 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
301 if (!TAILQ_EMPTY(&asoc->nets)) {
302 /* update any ssthresh's that may have a default */
303 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
304 lnet->ssthresh = asoc->peers_rwnd;
305 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
306 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
311 SCTP_TCB_SEND_LOCK(stcb);
312 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
314 struct sctp_stream_out *outs;
315 struct sctp_stream_queue_pending *sp, *nsp;
316 struct sctp_tmit_chunk *chk, *nchk;
318 /* abandon the upper streams */
319 newcnt = ntohs(init->num_inbound_streams);
320 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
321 if (chk->rec.data.sid >= newcnt) {
322 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
323 asoc->send_queue_cnt--;
324 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
325 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
328 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
331 if (chk->data != NULL) {
332 sctp_free_bufspace(stcb, asoc, chk, 1);
333 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
334 0, chk, SCTP_SO_NOT_LOCKED);
336 sctp_m_freem(chk->data);
340 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
341 /* sa_ignore FREED_MEMORY */
345 for (i = newcnt; i < asoc->pre_open_streams; i++) {
346 outs = &asoc->strmout[i];
347 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
348 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
349 TAILQ_REMOVE(&outs->outqueue, sp, next);
350 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
351 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
352 stcb, 0, sp, SCTP_SO_NOT_LOCKED);
354 sctp_m_freem(sp->data);
358 sctp_free_remote_addr(sp->net);
362 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
363 /* sa_ignore FREED_MEMORY */
365 outs->state = SCTP_STREAM_CLOSED;
368 /* cut back the count */
369 asoc->pre_open_streams = newcnt;
371 SCTP_TCB_SEND_UNLOCK(stcb);
372 asoc->streamoutcnt = asoc->pre_open_streams;
374 for (i = 0; i < asoc->streamoutcnt; i++) {
375 asoc->strmout[i].state = SCTP_STREAM_OPEN;
378 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
379 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
380 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
381 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
383 /* This is the next one we expect */
384 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
386 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
387 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
389 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
390 /* open the requested streams */
392 if (asoc->strmin != NULL) {
393 /* Free the old ones */
394 for (i = 0; i < asoc->streamincnt; i++) {
395 sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
396 sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
398 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
400 if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
401 asoc->streamincnt = ntohs(init->num_outbound_streams);
403 asoc->streamincnt = asoc->max_inbound_streams;
405 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
406 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
407 if (asoc->strmin == NULL) {
408 /* we didn't get memory for the streams! */
409 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
412 for (i = 0; i < asoc->streamincnt; i++) {
413 asoc->strmin[i].sid = i;
414 asoc->strmin[i].last_mid_delivered = 0xffffffff;
415 TAILQ_INIT(&asoc->strmin[i].inqueue);
416 TAILQ_INIT(&asoc->strmin[i].uno_inqueue);
417 asoc->strmin[i].pd_api_started = 0;
418 asoc->strmin[i].delivery_started = 0;
421 * load_address_from_init will put the addresses into the
422 * association when the COOKIE is processed or the INIT-ACK is
423 * processed. Both types of COOKIE's existing and new call this
424 * routine. It will remove addresses that are no longer in the
425 * association (for the restarting case where addresses are
426 * removed). Up front when the INIT arrives we will discard it if it
427 * is a restart and new addresses have been added.
429 /* sa_ignore MEMLEAK */
434 * INIT-ACK message processing/consumption returns value < 0 on error
437 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
438 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
439 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
440 struct sctp_nets *net, int *abort_no_unlock,
441 uint8_t mflowtype, uint32_t mflowid,
444 struct sctp_association *asoc;
446 int retval, abort_flag;
447 uint32_t initack_limit;
448 int nat_friendly = 0;
450 /* First verify that we have no illegal param's */
453 op_err = sctp_arethere_unrecognized_parameters(m,
454 (offset + sizeof(struct sctp_init_chunk)),
455 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
457 /* Send an abort and notify peer */
458 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
459 *abort_no_unlock = 1;
463 asoc->peer_supports_nat = (uint8_t)nat_friendly;
464 /* process the peer's parameters in the INIT-ACK */
465 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
469 initack_limit = offset + ntohs(cp->ch.chunk_length);
470 /* load all addresses */
471 if ((retval = sctp_load_addresses_from_init(stcb, m,
472 (offset + sizeof(struct sctp_init_chunk)), initack_limit,
473 src, dst, NULL, stcb->asoc.port))) {
474 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
475 "Problem with address parameters");
476 SCTPDBG(SCTP_DEBUG_INPUT1,
477 "Load addresses from INIT causes an abort %d\n",
479 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
480 src, dst, sh, op_err,
483 *abort_no_unlock = 1;
486 /* if the peer doesn't support asconf, flush the asconf queue */
487 if (asoc->asconf_supported == 0) {
488 struct sctp_asconf_addr *param, *nparam;
490 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
491 TAILQ_REMOVE(&asoc->asconf_queue, param, next);
492 SCTP_FREE(param, SCTP_M_ASC_ADDR);
496 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
497 stcb->asoc.local_hmacs);
499 sctp_queue_op_err(stcb, op_err);
500 /* queuing will steal away the mbuf chain to the out queue */
503 /* extract the cookie and queue it to "echo" it back... */
504 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
505 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
506 stcb->asoc.overall_error_count,
508 SCTP_FROM_SCTP_INPUT,
511 stcb->asoc.overall_error_count = 0;
512 net->error_count = 0;
515 * Cancel the INIT timer, We do this first before queueing the
516 * cookie. We always cancel at the primary to assue that we are
517 * canceling the timer started by the INIT which always goes to the
520 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
521 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
523 /* calculate the RTO */
524 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
525 SCTP_RTT_FROM_NON_DATA);
526 retval = sctp_send_cookie_echo(m, offset, stcb, net);
529 * No cookie, we probably should send a op error. But in any
530 * case if there is no cookie in the INIT-ACK, we can
531 * abandon the peer, its broke.
536 len = (uint16_t)(sizeof(struct sctp_error_missing_param) + sizeof(uint16_t));
537 /* We abort with an error of missing mandatory param */
538 op_err = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
539 if (op_err != NULL) {
540 struct sctp_error_missing_param *cause;
542 SCTP_BUF_LEN(op_err) = len;
543 cause = mtod(op_err, struct sctp_error_missing_param *);
544 /* Subtract the reserved param */
545 cause->cause.code = htons(SCTP_CAUSE_MISSING_PARAM);
546 cause->cause.length = htons(len);
547 cause->num_missing_params = htonl(1);
548 cause->type[0] = htons(SCTP_STATE_COOKIE);
550 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
551 src, dst, sh, op_err,
554 *abort_no_unlock = 1;
563 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
564 struct sctp_tcb *stcb, struct sctp_nets *net)
566 union sctp_sockstore store;
567 struct sctp_nets *r_net, *f_net;
570 uint16_t old_error_counter;
572 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
577 memset(&store, 0, sizeof(store));
578 switch (cp->heartbeat.hb_info.addr_family) {
581 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
582 store.sin.sin_family = cp->heartbeat.hb_info.addr_family;
583 store.sin.sin_len = cp->heartbeat.hb_info.addr_len;
584 store.sin.sin_port = stcb->rport;
585 memcpy(&store.sin.sin_addr, cp->heartbeat.hb_info.address,
586 sizeof(store.sin.sin_addr));
594 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
595 store.sin6.sin6_family = cp->heartbeat.hb_info.addr_family;
596 store.sin6.sin6_len = cp->heartbeat.hb_info.addr_len;
597 store.sin6.sin6_port = stcb->rport;
598 memcpy(&store.sin6.sin6_addr, cp->heartbeat.hb_info.address, sizeof(struct in6_addr));
607 r_net = sctp_findnet(stcb, &store.sa);
609 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
612 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
613 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
614 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
616 * If the its a HB and it's random value is correct when can
617 * confirm the destination.
619 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
620 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
621 stcb->asoc.primary_destination = r_net;
622 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
623 f_net = TAILQ_FIRST(&stcb->asoc.nets);
624 if (f_net != r_net) {
626 * first one on the list is NOT the primary
627 * sctp_cmpaddr() is much more efficient if
628 * the primary is the first on the list,
631 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
632 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
636 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
637 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
638 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
639 r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
640 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
642 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
643 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
644 stcb->asoc.overall_error_count,
646 SCTP_FROM_SCTP_INPUT,
649 stcb->asoc.overall_error_count = 0;
650 old_error_counter = r_net->error_count;
651 r_net->error_count = 0;
652 r_net->hb_responded = 1;
653 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
654 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
655 /* Now lets do a RTO with this */
656 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv,
657 SCTP_RTT_FROM_NON_DATA);
658 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
659 r_net->dest_state |= SCTP_ADDR_REACHABLE;
660 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
661 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
663 if (r_net->dest_state & SCTP_ADDR_PF) {
664 r_net->dest_state &= ~SCTP_ADDR_PF;
665 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
667 if (old_error_counter > 0) {
668 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
669 stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
670 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
672 if (r_net == stcb->asoc.primary_destination) {
673 if (stcb->asoc.alternate) {
674 /* release the alternate, primary is good */
675 sctp_free_remote_addr(stcb->asoc.alternate);
676 stcb->asoc.alternate = NULL;
679 /* Mobility adaptation */
681 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
682 SCTP_MOBILITY_BASE) ||
683 sctp_is_mobility_feature_on(stcb->sctp_ep,
684 SCTP_MOBILITY_FASTHANDOFF)) &&
685 sctp_is_mobility_feature_on(stcb->sctp_ep,
686 SCTP_MOBILITY_PRIM_DELETED)) {
688 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
689 stcb->sctp_ep, stcb, NULL,
690 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
691 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
692 SCTP_MOBILITY_FASTHANDOFF)) {
693 sctp_assoc_immediate_retrans(stcb,
694 stcb->asoc.primary_destination);
696 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
697 SCTP_MOBILITY_BASE)) {
698 sctp_move_chunks_from_net(stcb,
699 stcb->asoc.deleted_primary);
701 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
702 stcb->asoc.deleted_primary);
708 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
711 * return 0 means we want you to proceed with the abort non-zero
712 * means no abort processing
714 struct sctpasochead *head;
716 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) ||
717 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
718 atomic_add_int(&stcb->asoc.refcnt, 1);
719 SCTP_TCB_UNLOCK(stcb);
720 SCTP_INP_INFO_WLOCK();
722 atomic_subtract_int(&stcb->asoc.refcnt, 1);
724 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
725 /* generate a new vtag and send init */
726 LIST_REMOVE(stcb, sctp_asocs);
727 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
728 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
730 * put it in the bucket in the vtag hash of assoc's for the
733 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
734 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
735 SCTP_INP_INFO_WUNLOCK();
738 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
740 * treat like a case where the cookie expired i.e.: - dump
741 * current cookie. - generate a new vtag. - resend init.
743 /* generate a new vtag and send init */
744 LIST_REMOVE(stcb, sctp_asocs);
745 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
746 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
747 sctp_stop_all_cookie_timers(stcb);
748 sctp_toss_old_cookies(stcb, &stcb->asoc);
749 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
750 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
752 * put it in the bucket in the vtag hash of assoc's for the
755 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
756 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
757 SCTP_INP_INFO_WUNLOCK();
764 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
765 struct sctp_nets *net)
768 * return 0 means we want you to proceed with the abort non-zero
769 * means no abort processing
771 if (stcb->asoc.auth_supported == 0) {
772 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
775 sctp_asconf_send_nat_state_update(stcb, net);
780 /* Returns 1 if the stcb was aborted, 0 otherwise */
782 sctp_handle_abort(struct sctp_abort_chunk *abort,
783 struct sctp_tcb *stcb, struct sctp_nets *net)
785 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
791 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
795 len = ntohs(abort->ch.chunk_length);
796 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_error_cause)) {
798 * Need to check the cause codes for our two magic nat
799 * aborts which don't kill the assoc necessarily.
801 struct sctp_error_cause *cause;
803 cause = (struct sctp_error_cause *)(abort + 1);
804 error = ntohs(cause->code);
805 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
806 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
807 abort->ch.chunk_flags);
808 if (sctp_handle_nat_colliding_state(stcb)) {
811 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
812 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
813 abort->ch.chunk_flags);
814 if (sctp_handle_nat_missing_state(stcb, net)) {
821 /* stop any receive timers */
822 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net,
823 SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
824 /* notify user of the abort and clean up... */
825 sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
827 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
828 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
829 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
830 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
832 #ifdef SCTP_ASOCLOG_OF_TSNS
833 sctp_print_out_track_log(stcb);
835 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
836 so = SCTP_INP_SO(stcb->sctp_ep);
837 atomic_add_int(&stcb->asoc.refcnt, 1);
838 SCTP_TCB_UNLOCK(stcb);
839 SCTP_SOCKET_LOCK(so, 1);
841 atomic_subtract_int(&stcb->asoc.refcnt, 1);
843 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
844 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
845 SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
846 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
847 SCTP_SOCKET_UNLOCK(so, 1);
849 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
854 sctp_start_net_timers(struct sctp_tcb *stcb)
856 uint32_t cnt_hb_sent;
857 struct sctp_nets *net;
860 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
862 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
863 * If the dest in unconfirmed send a hb as well if under
864 * max_hb_burst have been sent.
866 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
867 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
868 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
869 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
870 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
875 sctp_chunk_output(stcb->sctp_ep, stcb,
876 SCTP_OUTPUT_FROM_COOKIE_ACK,
883 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
884 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
886 struct sctp_association *asoc;
887 int some_on_streamwheel;
889 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
893 SCTPDBG(SCTP_DEBUG_INPUT2,
894 "sctp_handle_shutdown: handling SHUTDOWN\n");
898 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
899 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
902 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
903 /* Shutdown NOT the expected size */
906 old_state = SCTP_GET_STATE(asoc);
907 sctp_update_acked(stcb, cp, abort_flag);
911 if (asoc->control_pdapi) {
913 * With a normal shutdown we assume the end of last record.
915 SCTP_INP_READ_LOCK(stcb->sctp_ep);
916 if (asoc->control_pdapi->on_strm_q) {
917 struct sctp_stream_in *strm;
919 strm = &asoc->strmin[asoc->control_pdapi->sinfo_stream];
920 if (asoc->control_pdapi->on_strm_q == SCTP_ON_UNORDERED) {
922 TAILQ_REMOVE(&strm->uno_inqueue, asoc->control_pdapi, next_instrm);
923 asoc->control_pdapi->on_strm_q = 0;
924 } else if (asoc->control_pdapi->on_strm_q == SCTP_ON_ORDERED) {
926 TAILQ_REMOVE(&strm->inqueue, asoc->control_pdapi, next_instrm);
927 asoc->control_pdapi->on_strm_q = 0;
930 panic("Unknown state on ctrl:%p on_strm_q:%d",
932 asoc->control_pdapi->on_strm_q);
936 asoc->control_pdapi->end_added = 1;
937 asoc->control_pdapi->pdapi_aborted = 1;
938 asoc->control_pdapi = NULL;
939 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
940 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
941 so = SCTP_INP_SO(stcb->sctp_ep);
942 atomic_add_int(&stcb->asoc.refcnt, 1);
943 SCTP_TCB_UNLOCK(stcb);
944 SCTP_SOCKET_LOCK(so, 1);
946 atomic_subtract_int(&stcb->asoc.refcnt, 1);
947 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
948 /* assoc was freed while we were unlocked */
949 SCTP_SOCKET_UNLOCK(so, 1);
953 if (stcb->sctp_socket) {
954 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
956 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
957 SCTP_SOCKET_UNLOCK(so, 1);
960 /* goto SHUTDOWN_RECEIVED state to block new requests */
961 if (stcb->sctp_socket) {
962 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
963 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
964 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
965 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
966 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
968 * notify upper layer that peer has initiated a
971 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
974 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
977 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
979 * stop the shutdown timer, since we WILL move to
982 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
983 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
985 /* Now is there unsent data on a stream somewhere? */
986 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
988 if (!TAILQ_EMPTY(&asoc->send_queue) ||
989 !TAILQ_EMPTY(&asoc->sent_queue) ||
990 some_on_streamwheel) {
991 /* By returning we will push more data out */
994 /* no outstanding data to send, so move on... */
995 /* send SHUTDOWN-ACK */
996 /* move to SHUTDOWN-ACK-SENT state */
997 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
998 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
999 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1001 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1002 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
1003 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
1004 sctp_stop_timers_for_shutdown(stcb);
1005 sctp_send_shutdown_ack(stcb, net);
1006 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
1007 stcb->sctp_ep, stcb, net);
1008 } else if (old_state == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1009 sctp_send_shutdown_ack(stcb, net);
1015 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
1016 struct sctp_tcb *stcb,
1017 struct sctp_nets *net)
1019 struct sctp_association *asoc;
1020 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1023 so = SCTP_INP_SO(stcb->sctp_ep);
1025 SCTPDBG(SCTP_DEBUG_INPUT2,
1026 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
1031 /* process according to association state */
1032 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
1033 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
1034 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
1035 sctp_send_shutdown_complete(stcb, net, 1);
1036 SCTP_TCB_UNLOCK(stcb);
1039 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
1040 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1041 /* unexpected SHUTDOWN-ACK... so ignore... */
1042 SCTP_TCB_UNLOCK(stcb);
1045 if (asoc->control_pdapi) {
1047 * With a normal shutdown we assume the end of last record.
1049 SCTP_INP_READ_LOCK(stcb->sctp_ep);
1050 asoc->control_pdapi->end_added = 1;
1051 asoc->control_pdapi->pdapi_aborted = 1;
1052 asoc->control_pdapi = NULL;
1053 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1054 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1055 atomic_add_int(&stcb->asoc.refcnt, 1);
1056 SCTP_TCB_UNLOCK(stcb);
1057 SCTP_SOCKET_LOCK(so, 1);
1058 SCTP_TCB_LOCK(stcb);
1059 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1060 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1061 /* assoc was freed while we were unlocked */
1062 SCTP_SOCKET_UNLOCK(so, 1);
1066 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1067 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1068 SCTP_SOCKET_UNLOCK(so, 1);
1072 if (!TAILQ_EMPTY(&asoc->send_queue) ||
1073 !TAILQ_EMPTY(&asoc->sent_queue) ||
1074 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
1075 panic("Queues are not empty when handling SHUTDOWN-ACK");
1078 /* stop the timer */
1079 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net,
1080 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1081 /* send SHUTDOWN-COMPLETE */
1082 sctp_send_shutdown_complete(stcb, net, 0);
1083 /* notify upper layer protocol */
1084 if (stcb->sctp_socket) {
1085 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1086 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1087 stcb->sctp_socket->so_snd.sb_cc = 0;
1089 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1091 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1092 /* free the TCB but first save off the ep */
1093 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1094 atomic_add_int(&stcb->asoc.refcnt, 1);
1095 SCTP_TCB_UNLOCK(stcb);
1096 SCTP_SOCKET_LOCK(so, 1);
1097 SCTP_TCB_LOCK(stcb);
1098 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1100 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1101 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1102 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1103 SCTP_SOCKET_UNLOCK(so, 1);
1108 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, uint8_t chunk_type,
1109 struct sctp_nets *net)
1111 switch (chunk_type) {
1112 case SCTP_ASCONF_ACK:
1114 sctp_asconf_cleanup(stcb, net);
1116 case SCTP_IFORWARD_CUM_TSN:
1117 case SCTP_FORWARD_CUM_TSN:
1118 stcb->asoc.prsctp_supported = 0;
1121 SCTPDBG(SCTP_DEBUG_INPUT2,
1122 "Peer does not support chunk type %d (0x%x).\n",
1123 chunk_type, chunk_type);
1129 * Skip past the param header and then we will find the param that caused the
1130 * problem. There are a number of param's in a ASCONF OR the prsctp param
1131 * these will turn of specific features.
1132 * XXX: Is this the right thing to do?
1135 sctp_process_unrecog_param(struct sctp_tcb *stcb, uint16_t parameter_type)
1137 switch (parameter_type) {
1139 case SCTP_PRSCTP_SUPPORTED:
1140 stcb->asoc.prsctp_supported = 0;
1142 case SCTP_SUPPORTED_CHUNK_EXT:
1144 /* draft-ietf-tsvwg-addip-sctp */
1145 case SCTP_HAS_NAT_SUPPORT:
1146 stcb->asoc.peer_supports_nat = 0;
1148 case SCTP_ADD_IP_ADDRESS:
1149 case SCTP_DEL_IP_ADDRESS:
1150 case SCTP_SET_PRIM_ADDR:
1151 stcb->asoc.asconf_supported = 0;
1153 case SCTP_SUCCESS_REPORT:
1154 case SCTP_ERROR_CAUSE_IND:
1155 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1156 SCTPDBG(SCTP_DEBUG_INPUT2,
1157 "Turning off ASCONF to this strange peer\n");
1158 stcb->asoc.asconf_supported = 0;
1161 SCTPDBG(SCTP_DEBUG_INPUT2,
1162 "Peer does not support param type %d (0x%x)??\n",
1163 parameter_type, parameter_type);
1169 sctp_handle_error(struct sctp_chunkhdr *ch,
1170 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
1172 struct sctp_error_cause *cause;
1173 struct sctp_association *asoc;
1174 uint32_t remaining_length, adjust;
1175 uint16_t code, cause_code, cause_length;
1176 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1180 /* parse through all of the errors and process */
1182 cause = (struct sctp_error_cause *)((caddr_t)ch +
1183 sizeof(struct sctp_chunkhdr));
1184 remaining_length = ntohs(ch->chunk_length);
1185 if (remaining_length > limit) {
1186 remaining_length = limit;
1188 if (remaining_length >= sizeof(struct sctp_chunkhdr)) {
1189 remaining_length -= sizeof(struct sctp_chunkhdr);
1191 remaining_length = 0;
1194 while (remaining_length >= sizeof(struct sctp_error_cause)) {
1195 /* Process an Error Cause */
1196 cause_code = ntohs(cause->code);
1197 cause_length = ntohs(cause->length);
1198 if ((cause_length > remaining_length) || (cause_length == 0)) {
1199 /* Invalid cause length, possibly due to truncation. */
1200 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in cause - bytes left: %u cause length: %u\n",
1201 remaining_length, cause_length);
1205 /* report the first error cause */
1208 switch (cause_code) {
1209 case SCTP_CAUSE_INVALID_STREAM:
1210 case SCTP_CAUSE_MISSING_PARAM:
1211 case SCTP_CAUSE_INVALID_PARAM:
1212 case SCTP_CAUSE_NO_USER_DATA:
1213 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %u back? We have a bug :/ (or do they?)\n",
1216 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1217 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags: %x\n",
1219 if (sctp_handle_nat_colliding_state(stcb)) {
1223 case SCTP_CAUSE_NAT_MISSING_STATE:
1224 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags: %x\n",
1226 if (sctp_handle_nat_missing_state(stcb, net)) {
1230 case SCTP_CAUSE_STALE_COOKIE:
1232 * We only act if we have echoed a cookie and are
1235 if ((cause_length >= sizeof(struct sctp_error_stale_cookie)) &&
1236 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
1237 struct sctp_error_stale_cookie *stale_cookie;
1239 stale_cookie = (struct sctp_error_stale_cookie *)cause;
1240 asoc->cookie_preserve_req = ntohl(stale_cookie->stale_time);
1241 /* Double it to be more robust on RTX */
1242 if (asoc->cookie_preserve_req <= UINT32_MAX / 2) {
1243 asoc->cookie_preserve_req *= 2;
1245 asoc->cookie_preserve_req = UINT32_MAX;
1247 asoc->stale_cookie_count++;
1248 if (asoc->stale_cookie_count >
1249 asoc->max_init_times) {
1250 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1251 /* now free the asoc */
1252 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1253 so = SCTP_INP_SO(stcb->sctp_ep);
1254 atomic_add_int(&stcb->asoc.refcnt, 1);
1255 SCTP_TCB_UNLOCK(stcb);
1256 SCTP_SOCKET_LOCK(so, 1);
1257 SCTP_TCB_LOCK(stcb);
1258 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1260 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1261 SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1262 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1263 SCTP_SOCKET_UNLOCK(so, 1);
1267 /* blast back to INIT state */
1268 sctp_toss_old_cookies(stcb, &stcb->asoc);
1269 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1270 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1271 sctp_stop_all_cookie_timers(stcb);
1272 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1275 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1277 * Nothing we can do here, we don't do hostname
1278 * addresses so if the peer does not like my IPv6
1279 * (or IPv4 for that matter) it does not matter. If
1280 * they don't support that type of address, they can
1281 * NOT possibly get that packet type... i.e. with no
1282 * IPv6 you can't receive a IPv6 packet. so we can
1283 * safely ignore this one. If we ever added support
1284 * for HOSTNAME Addresses, then we would need to do
1288 case SCTP_CAUSE_UNRECOG_CHUNK:
1289 if (cause_length >= sizeof(struct sctp_error_unrecognized_chunk)) {
1290 struct sctp_error_unrecognized_chunk *unrec_chunk;
1292 unrec_chunk = (struct sctp_error_unrecognized_chunk *)cause;
1293 sctp_process_unrecog_chunk(stcb, unrec_chunk->ch.chunk_type, net);
1296 case SCTP_CAUSE_UNRECOG_PARAM:
1297 /* XXX: We only consider the first parameter */
1298 if (cause_length >= sizeof(struct sctp_error_cause) + sizeof(struct sctp_paramhdr)) {
1299 struct sctp_paramhdr *unrec_parameter;
1301 unrec_parameter = (struct sctp_paramhdr *)(cause + 1);
1302 sctp_process_unrecog_param(stcb, ntohs(unrec_parameter->param_type));
1305 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1307 * We ignore this since the timer will drive out a
1308 * new cookie anyway and there timer will drive us
1309 * to send a SHUTDOWN_COMPLETE. We can't send one
1310 * here since we don't have their tag.
1313 case SCTP_CAUSE_DELETING_LAST_ADDR:
1314 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1315 case SCTP_CAUSE_DELETING_SRC_ADDR:
1317 * We should NOT get these here, but in a
1320 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a error cause with code %u.\n",
1323 case SCTP_CAUSE_OUT_OF_RESC:
1325 * And what, pray tell do we do with the fact that
1326 * the peer is out of resources? Not really sure we
1327 * could do anything but abort. I suspect this
1328 * should have came WITH an abort instead of in a
1333 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown code 0x%x\n",
1337 adjust = SCTP_SIZE32(cause_length);
1338 if (remaining_length >= adjust) {
1339 remaining_length -= adjust;
1341 remaining_length = 0;
1343 cause = (struct sctp_error_cause *)((caddr_t)cause + adjust);
1345 sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, code, ch, SCTP_SO_NOT_LOCKED);
1350 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1351 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1352 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1353 struct sctp_nets *net, int *abort_no_unlock,
1354 uint8_t mflowtype, uint32_t mflowid,
1357 struct sctp_init_ack *init_ack;
1358 struct mbuf *op_err;
1360 SCTPDBG(SCTP_DEBUG_INPUT2,
1361 "sctp_handle_init_ack: handling INIT-ACK\n");
1364 SCTPDBG(SCTP_DEBUG_INPUT2,
1365 "sctp_handle_init_ack: TCB is null\n");
1368 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1369 /* Invalid length */
1370 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1371 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1372 src, dst, sh, op_err,
1375 *abort_no_unlock = 1;
1378 init_ack = &cp->init;
1379 /* validate parameters */
1380 if (init_ack->initiate_tag == 0) {
1381 /* protocol error... send an abort */
1382 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1383 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1384 src, dst, sh, op_err,
1387 *abort_no_unlock = 1;
1390 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1391 /* protocol error... send an abort */
1392 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1393 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1394 src, dst, sh, op_err,
1397 *abort_no_unlock = 1;
1400 if (init_ack->num_inbound_streams == 0) {
1401 /* protocol error... send an abort */
1402 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1403 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1404 src, dst, sh, op_err,
1407 *abort_no_unlock = 1;
1410 if (init_ack->num_outbound_streams == 0) {
1411 /* protocol error... send an abort */
1412 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1413 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1414 src, dst, sh, op_err,
1417 *abort_no_unlock = 1;
1420 /* process according to association state... */
1421 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1422 case SCTP_STATE_COOKIE_WAIT:
1423 /* this is the expected state for this chunk */
1424 /* process the INIT-ACK parameters */
1425 if (stcb->asoc.primary_destination->dest_state &
1426 SCTP_ADDR_UNCONFIRMED) {
1428 * The primary is where we sent the INIT, we can
1429 * always consider it confirmed when the INIT-ACK is
1430 * returned. Do this before we load addresses
1433 stcb->asoc.primary_destination->dest_state &=
1434 ~SCTP_ADDR_UNCONFIRMED;
1435 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1436 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1438 if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1439 net, abort_no_unlock,
1442 /* error in parsing parameters */
1445 /* update our state */
1446 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1447 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1449 /* reset the RTO calc */
1450 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1451 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1452 stcb->asoc.overall_error_count,
1454 SCTP_FROM_SCTP_INPUT,
1457 stcb->asoc.overall_error_count = 0;
1458 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1460 * collapse the init timer back in case of a exponential
1463 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1466 * the send at the end of the inbound data processing will
1467 * cause the cookie to be sent
1470 case SCTP_STATE_SHUTDOWN_SENT:
1471 /* incorrect state... discard */
1473 case SCTP_STATE_COOKIE_ECHOED:
1474 /* incorrect state... discard */
1476 case SCTP_STATE_OPEN:
1477 /* incorrect state... discard */
1479 case SCTP_STATE_EMPTY:
1480 case SCTP_STATE_INUSE:
1482 /* incorrect state... discard */
1486 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1490 static struct sctp_tcb *
1491 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1492 struct sockaddr *src, struct sockaddr *dst,
1493 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1494 struct sctp_inpcb *inp, struct sctp_nets **netp,
1495 struct sockaddr *init_src, int *notification,
1496 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1497 uint8_t mflowtype, uint32_t mflowid,
1498 uint32_t vrf_id, uint16_t port);
1502 * handle a state cookie for an existing association m: input packet mbuf
1503 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1504 * "split" mbuf and the cookie signature does not exist offset: offset into
1505 * mbuf to the cookie-echo chunk
1507 static struct sctp_tcb *
1508 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1509 struct sockaddr *src, struct sockaddr *dst,
1510 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1511 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1512 struct sockaddr *init_src, int *notification,
1513 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1514 uint8_t mflowtype, uint32_t mflowid,
1515 uint32_t vrf_id, uint16_t port)
1517 struct sctp_association *asoc;
1518 struct sctp_init_chunk *init_cp, init_buf;
1519 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1520 struct sctp_nets *net;
1521 struct mbuf *op_err;
1523 int init_offset, initack_offset, i;
1527 #if defined(SCTP_DETAILED_STR_STATS)
1532 /* I know that the TCB is non-NULL from the caller */
1534 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1535 if (asoc->cookie_how[how_indx] == 0)
1538 if (how_indx < sizeof(asoc->cookie_how)) {
1539 asoc->cookie_how[how_indx] = 1;
1541 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1542 /* SHUTDOWN came in after sending INIT-ACK */
1543 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1544 op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
1545 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1546 mflowtype, mflowid, inp->fibnum,
1548 if (how_indx < sizeof(asoc->cookie_how))
1549 asoc->cookie_how[how_indx] = 2;
1553 * find and validate the INIT chunk in the cookie (peer's info) the
1554 * INIT should start after the cookie-echo header struct (chunk
1555 * header, state cookie header struct)
1557 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1559 init_cp = (struct sctp_init_chunk *)
1560 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1561 (uint8_t *)&init_buf);
1562 if (init_cp == NULL) {
1563 /* could not pull a INIT chunk in cookie */
1566 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1570 * find and validate the INIT-ACK chunk in the cookie (my info) the
1571 * INIT-ACK follows the INIT chunk
1573 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1574 initack_cp = (struct sctp_init_ack_chunk *)
1575 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1576 (uint8_t *)&initack_buf);
1577 if (initack_cp == NULL) {
1578 /* could not pull INIT-ACK chunk in cookie */
1581 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1584 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1585 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1587 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1588 * to get into the OPEN state
1590 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1592 * Opps, this means that we somehow generated two vtag's
1593 * the same. I.e. we did:
1595 * <---INIT(tag=a)------
1596 * ----INIT-ACK(tag=t)-->
1597 * ----INIT(tag=t)------> *1
1598 * <---INIT-ACK(tag=a)---
1599 * <----CE(tag=t)------------- *2
1601 * At point *1 we should be generating a different
1602 * tag t'. Which means we would throw away the CE and send
1603 * ours instead. Basically this is case C (throw away side).
1605 if (how_indx < sizeof(asoc->cookie_how))
1606 asoc->cookie_how[how_indx] = 17;
1610 switch (SCTP_GET_STATE(asoc)) {
1611 case SCTP_STATE_COOKIE_WAIT:
1612 case SCTP_STATE_COOKIE_ECHOED:
1614 * INIT was sent but got a COOKIE_ECHO with the
1615 * correct tags... just accept it...but we must
1616 * process the init so that we can make sure we have
1617 * the right seq no's.
1619 /* First we must process the INIT !! */
1620 retval = sctp_process_init(init_cp, stcb);
1622 if (how_indx < sizeof(asoc->cookie_how))
1623 asoc->cookie_how[how_indx] = 3;
1626 /* we have already processed the INIT so no problem */
1627 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp,
1629 SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1630 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp,
1632 SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1633 /* update current state */
1634 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1635 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1637 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1639 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1640 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1641 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1642 stcb->sctp_ep, stcb, asoc->primary_destination);
1644 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1645 sctp_stop_all_cookie_timers(stcb);
1646 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1647 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1648 (!SCTP_IS_LISTENING(inp))) {
1649 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1653 * Here is where collision would go if we
1654 * did a connect() and instead got a
1655 * init/init-ack/cookie done before the
1656 * init-ack came back..
1658 stcb->sctp_ep->sctp_flags |=
1659 SCTP_PCB_FLAGS_CONNECTED;
1660 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1661 so = SCTP_INP_SO(stcb->sctp_ep);
1662 atomic_add_int(&stcb->asoc.refcnt, 1);
1663 SCTP_TCB_UNLOCK(stcb);
1664 SCTP_SOCKET_LOCK(so, 1);
1665 SCTP_TCB_LOCK(stcb);
1666 atomic_add_int(&stcb->asoc.refcnt, -1);
1667 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1668 SCTP_SOCKET_UNLOCK(so, 1);
1672 soisconnected(stcb->sctp_socket);
1673 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1674 SCTP_SOCKET_UNLOCK(so, 1);
1677 /* notify upper layer */
1678 *notification = SCTP_NOTIFY_ASSOC_UP;
1680 * since we did not send a HB make sure we don't
1683 old.tv_sec = cookie->time_entered.tv_sec;
1684 old.tv_usec = cookie->time_entered.tv_usec;
1685 net->hb_responded = 1;
1686 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1688 SCTP_RTT_FROM_NON_DATA);
1690 if (stcb->asoc.sctp_autoclose_ticks &&
1691 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1692 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1698 * we're in the OPEN state (or beyond), so peer must
1699 * have simply lost the COOKIE-ACK
1703 sctp_stop_all_cookie_timers(stcb);
1705 * We ignore the return code here.. not sure if we should
1706 * somehow abort.. but we do have an existing asoc. This
1707 * really should not fail.
1709 if (sctp_load_addresses_from_init(stcb, m,
1710 init_offset + sizeof(struct sctp_init_chunk),
1711 initack_offset, src, dst, init_src, stcb->asoc.port)) {
1712 if (how_indx < sizeof(asoc->cookie_how))
1713 asoc->cookie_how[how_indx] = 4;
1716 /* respond with a COOKIE-ACK */
1717 sctp_toss_old_cookies(stcb, asoc);
1718 sctp_send_cookie_ack(stcb);
1719 if (how_indx < sizeof(asoc->cookie_how))
1720 asoc->cookie_how[how_indx] = 5;
1724 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1725 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1726 cookie->tie_tag_my_vtag == 0 &&
1727 cookie->tie_tag_peer_vtag == 0) {
1729 * case C in Section 5.2.4 Table 2: XMOO silently discard
1731 if (how_indx < sizeof(asoc->cookie_how))
1732 asoc->cookie_how[how_indx] = 6;
1736 * If nat support, and the below and stcb is established, send back
1737 * a ABORT(colliding state) if we are established.
1739 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1740 (asoc->peer_supports_nat) &&
1741 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1742 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1743 (asoc->peer_vtag == 0)))) {
1745 * Special case - Peer's support nat. We may have two init's
1746 * that we gave out the same tag on since one was not
1747 * established.. i.e. we get INIT from host-1 behind the nat
1748 * and we respond tag-a, we get a INIT from host-2 behind
1749 * the nat and we get tag-a again. Then we bring up host-1
1750 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1751 * Now we have colliding state. We must send an abort here
1752 * with colliding state indication.
1754 op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
1755 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1756 mflowtype, mflowid, inp->fibnum,
1760 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1761 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1762 (asoc->peer_vtag == 0))) {
1764 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1765 * should be ok, re-accept peer info
1767 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1769 * Extension of case C. If we hit this, then the
1770 * random number generator returned the same vtag
1771 * when we first sent our INIT-ACK and when we later
1772 * sent our INIT. The side with the seq numbers that
1773 * are different will be the one that normnally
1774 * would have hit case C. This in effect "extends"
1775 * our vtags in this collision case to be 64 bits.
1776 * The same collision could occur aka you get both
1777 * vtag and seq number the same twice in a row.. but
1778 * is much less likely. If it did happen then we
1779 * would proceed through and bring up the assoc.. we
1780 * may end up with the wrong stream setup however..
1781 * which would be bad.. but there is no way to
1782 * tell.. until we send on a stream that does not
1785 if (how_indx < sizeof(asoc->cookie_how))
1786 asoc->cookie_how[how_indx] = 7;
1790 if (how_indx < sizeof(asoc->cookie_how))
1791 asoc->cookie_how[how_indx] = 8;
1792 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1793 SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1794 sctp_stop_all_cookie_timers(stcb);
1796 * since we did not send a HB make sure we don't double
1799 net->hb_responded = 1;
1800 if (stcb->asoc.sctp_autoclose_ticks &&
1801 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1802 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1805 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1806 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1808 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1810 * Ok the peer probably discarded our data (if we
1811 * echoed a cookie+data). So anything on the
1812 * sent_queue should be marked for retransmit, we
1813 * may not get something to kick us so it COULD
1814 * still take a timeout to move these.. but it can't
1815 * hurt to mark them.
1817 struct sctp_tmit_chunk *chk;
1819 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1820 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1821 chk->sent = SCTP_DATAGRAM_RESEND;
1822 sctp_flight_size_decrease(chk);
1823 sctp_total_flight_decrease(stcb, chk);
1824 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1830 /* process the INIT info (peer's info) */
1831 retval = sctp_process_init(init_cp, stcb);
1833 if (how_indx < sizeof(asoc->cookie_how))
1834 asoc->cookie_how[how_indx] = 9;
1837 if (sctp_load_addresses_from_init(stcb, m,
1838 init_offset + sizeof(struct sctp_init_chunk),
1839 initack_offset, src, dst, init_src, stcb->asoc.port)) {
1840 if (how_indx < sizeof(asoc->cookie_how))
1841 asoc->cookie_how[how_indx] = 10;
1844 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1845 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1846 *notification = SCTP_NOTIFY_ASSOC_UP;
1848 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1849 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1850 (!SCTP_IS_LISTENING(inp))) {
1851 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1854 stcb->sctp_ep->sctp_flags |=
1855 SCTP_PCB_FLAGS_CONNECTED;
1856 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1857 so = SCTP_INP_SO(stcb->sctp_ep);
1858 atomic_add_int(&stcb->asoc.refcnt, 1);
1859 SCTP_TCB_UNLOCK(stcb);
1860 SCTP_SOCKET_LOCK(so, 1);
1861 SCTP_TCB_LOCK(stcb);
1862 atomic_add_int(&stcb->asoc.refcnt, -1);
1863 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1864 SCTP_SOCKET_UNLOCK(so, 1);
1868 soisconnected(stcb->sctp_socket);
1869 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1870 SCTP_SOCKET_UNLOCK(so, 1);
1873 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1874 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1876 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1877 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1878 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1879 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1881 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1883 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1884 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1885 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1886 stcb->sctp_ep, stcb, asoc->primary_destination);
1888 sctp_stop_all_cookie_timers(stcb);
1889 sctp_toss_old_cookies(stcb, asoc);
1890 sctp_send_cookie_ack(stcb);
1893 * only if we have retrans set do we do this. What
1894 * this call does is get only the COOKIE-ACK out and
1895 * then when we return the normal call to
1896 * sctp_chunk_output will get the retrans out behind
1899 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1901 if (how_indx < sizeof(asoc->cookie_how))
1902 asoc->cookie_how[how_indx] = 11;
1906 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1907 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1908 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1909 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1910 cookie->tie_tag_peer_vtag != 0) {
1911 struct sctpasochead *head;
1912 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1916 if (asoc->peer_supports_nat) {
1918 * This is a gross gross hack. Just call the
1919 * cookie_new code since we are allowing a duplicate
1920 * association. I hope this works...
1922 return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
1923 sh, cookie, cookie_len,
1924 inp, netp, init_src, notification,
1925 auth_skipped, auth_offset, auth_len,
1930 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1933 if (how_indx < sizeof(asoc->cookie_how))
1934 asoc->cookie_how[how_indx] = 12;
1935 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
1936 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1937 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1938 SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
1940 /* notify upper layer */
1941 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1942 atomic_add_int(&stcb->asoc.refcnt, 1);
1943 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1944 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1945 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1946 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1948 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1949 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1950 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1951 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1953 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1954 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1955 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1956 stcb->sctp_ep, stcb, asoc->primary_destination);
1958 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1959 /* move to OPEN state, if not in SHUTDOWN_SENT */
1960 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1962 asoc->pre_open_streams =
1963 ntohs(initack_cp->init.num_outbound_streams);
1964 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1965 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1966 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1968 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1970 asoc->str_reset_seq_in = asoc->init_seq_number;
1972 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1973 if (asoc->mapping_array) {
1974 memset(asoc->mapping_array, 0,
1975 asoc->mapping_array_size);
1977 if (asoc->nr_mapping_array) {
1978 memset(asoc->nr_mapping_array, 0,
1979 asoc->mapping_array_size);
1981 SCTP_TCB_UNLOCK(stcb);
1982 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1983 so = SCTP_INP_SO(stcb->sctp_ep);
1984 SCTP_SOCKET_LOCK(so, 1);
1986 SCTP_INP_INFO_WLOCK();
1987 SCTP_INP_WLOCK(stcb->sctp_ep);
1988 SCTP_TCB_LOCK(stcb);
1989 atomic_add_int(&stcb->asoc.refcnt, -1);
1990 /* send up all the data */
1991 SCTP_TCB_SEND_LOCK(stcb);
1993 sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_LOCKED);
1994 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1995 stcb->asoc.strmout[i].chunks_on_queues = 0;
1996 #if defined(SCTP_DETAILED_STR_STATS)
1997 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1998 asoc->strmout[i].abandoned_sent[j] = 0;
1999 asoc->strmout[i].abandoned_unsent[j] = 0;
2002 asoc->strmout[i].abandoned_sent[0] = 0;
2003 asoc->strmout[i].abandoned_unsent[0] = 0;
2005 stcb->asoc.strmout[i].sid = i;
2006 stcb->asoc.strmout[i].next_mid_ordered = 0;
2007 stcb->asoc.strmout[i].next_mid_unordered = 0;
2008 stcb->asoc.strmout[i].last_msg_incomplete = 0;
2010 /* process the INIT-ACK info (my info) */
2011 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2012 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2014 /* pull from vtag hash */
2015 LIST_REMOVE(stcb, sctp_asocs);
2016 /* re-insert to new vtag position */
2017 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
2018 SCTP_BASE_INFO(hashasocmark))];
2020 * put it in the bucket in the vtag hash of assoc's for the
2023 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
2025 SCTP_TCB_SEND_UNLOCK(stcb);
2026 SCTP_INP_WUNLOCK(stcb->sctp_ep);
2027 SCTP_INP_INFO_WUNLOCK();
2028 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2029 SCTP_SOCKET_UNLOCK(so, 1);
2031 asoc->total_flight = 0;
2032 asoc->total_flight_count = 0;
2033 /* process the INIT info (peer's info) */
2034 retval = sctp_process_init(init_cp, stcb);
2036 if (how_indx < sizeof(asoc->cookie_how))
2037 asoc->cookie_how[how_indx] = 13;
2042 * since we did not send a HB make sure we don't double
2045 net->hb_responded = 1;
2047 if (sctp_load_addresses_from_init(stcb, m,
2048 init_offset + sizeof(struct sctp_init_chunk),
2049 initack_offset, src, dst, init_src, stcb->asoc.port)) {
2050 if (how_indx < sizeof(asoc->cookie_how))
2051 asoc->cookie_how[how_indx] = 14;
2055 /* respond with a COOKIE-ACK */
2056 sctp_stop_all_cookie_timers(stcb);
2057 sctp_toss_old_cookies(stcb, asoc);
2058 sctp_send_cookie_ack(stcb);
2059 if (how_indx < sizeof(asoc->cookie_how))
2060 asoc->cookie_how[how_indx] = 15;
2064 if (how_indx < sizeof(asoc->cookie_how))
2065 asoc->cookie_how[how_indx] = 16;
2066 /* all other cases... */
2072 * handle a state cookie for a new association m: input packet mbuf chain--
2073 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
2074 * and the cookie signature does not exist offset: offset into mbuf to the
2075 * cookie-echo chunk length: length of the cookie chunk to: where the init
2076 * was from returns a new TCB
2078 static struct sctp_tcb *
2079 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
2080 struct sockaddr *src, struct sockaddr *dst,
2081 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
2082 struct sctp_inpcb *inp, struct sctp_nets **netp,
2083 struct sockaddr *init_src, int *notification,
2084 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2085 uint8_t mflowtype, uint32_t mflowid,
2086 uint32_t vrf_id, uint16_t port)
2088 struct sctp_tcb *stcb;
2089 struct sctp_init_chunk *init_cp, init_buf;
2090 struct sctp_init_ack_chunk *initack_cp, initack_buf;
2091 union sctp_sockstore store;
2092 struct sctp_association *asoc;
2093 int init_offset, initack_offset, initack_limit;
2096 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
2097 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2100 so = SCTP_INP_SO(inp);
2104 * find and validate the INIT chunk in the cookie (peer's info) the
2105 * INIT should start after the cookie-echo header struct (chunk
2106 * header, state cookie header struct)
2108 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2109 init_cp = (struct sctp_init_chunk *)
2110 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2111 (uint8_t *)&init_buf);
2112 if (init_cp == NULL) {
2113 /* could not pull a INIT chunk in cookie */
2114 SCTPDBG(SCTP_DEBUG_INPUT1,
2115 "process_cookie_new: could not pull INIT chunk hdr\n");
2118 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2119 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2122 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2124 * find and validate the INIT-ACK chunk in the cookie (my info) the
2125 * INIT-ACK follows the INIT chunk
2127 initack_cp = (struct sctp_init_ack_chunk *)
2128 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2129 (uint8_t *)&initack_buf);
2130 if (initack_cp == NULL) {
2131 /* could not pull INIT-ACK chunk in cookie */
2132 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2135 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2139 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2140 * "initack_limit" value. This is because the chk_length field
2141 * includes the length of the cookie, but the cookie is omitted when
2142 * the INIT and INIT_ACK are tacked onto the cookie...
2144 initack_limit = offset + cookie_len;
2147 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2152 * Here we do a trick, we set in NULL for the proc/thread argument.
2153 * We do this since in effect we only use the p argument when the
2154 * socket is unbound and we must do an implicit bind. Since we are
2155 * getting a cookie, we cannot be unbound.
2157 stcb = sctp_aloc_assoc(inp, init_src, &error,
2158 ntohl(initack_cp->init.initiate_tag), vrf_id,
2159 ntohs(initack_cp->init.num_outbound_streams),
2161 (struct thread *)NULL
2164 struct mbuf *op_err;
2166 /* memory problem? */
2167 SCTPDBG(SCTP_DEBUG_INPUT1,
2168 "process_cookie_new: no room for another TCB!\n");
2169 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2170 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2171 src, dst, sh, op_err,
2176 /* get the correct sctp_nets */
2178 *netp = sctp_findnet(stcb, init_src);
2181 /* get scope variables out of cookie */
2182 asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2183 asoc->scope.site_scope = cookie->site_scope;
2184 asoc->scope.local_scope = cookie->local_scope;
2185 asoc->scope.loopback_scope = cookie->loopback_scope;
2187 if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2188 (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2189 struct mbuf *op_err;
2192 * Houston we have a problem. The EP changed while the
2193 * cookie was in flight. Only recourse is to abort the
2196 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2197 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2198 src, dst, sh, op_err,
2201 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2202 atomic_add_int(&stcb->asoc.refcnt, 1);
2203 SCTP_TCB_UNLOCK(stcb);
2204 SCTP_SOCKET_LOCK(so, 1);
2205 SCTP_TCB_LOCK(stcb);
2206 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2208 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2209 SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2210 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2211 SCTP_SOCKET_UNLOCK(so, 1);
2215 /* process the INIT-ACK info (my info) */
2216 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2217 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2218 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2219 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2220 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2221 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2222 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2223 asoc->str_reset_seq_in = asoc->init_seq_number;
2225 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2227 /* process the INIT info (peer's info) */
2229 retval = sctp_process_init(init_cp, stcb);
2233 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2234 atomic_add_int(&stcb->asoc.refcnt, 1);
2235 SCTP_TCB_UNLOCK(stcb);
2236 SCTP_SOCKET_LOCK(so, 1);
2237 SCTP_TCB_LOCK(stcb);
2238 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2240 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2241 SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2242 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2243 SCTP_SOCKET_UNLOCK(so, 1);
2247 /* load all addresses */
2248 if (sctp_load_addresses_from_init(stcb, m,
2249 init_offset + sizeof(struct sctp_init_chunk), initack_offset,
2250 src, dst, init_src, port)) {
2251 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2252 atomic_add_int(&stcb->asoc.refcnt, 1);
2253 SCTP_TCB_UNLOCK(stcb);
2254 SCTP_SOCKET_LOCK(so, 1);
2255 SCTP_TCB_LOCK(stcb);
2256 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2258 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2259 SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2260 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2261 SCTP_SOCKET_UNLOCK(so, 1);
2266 * verify any preceding AUTH chunk that was skipped
2268 /* pull the local authentication parameters from the cookie/init-ack */
2269 sctp_auth_get_cookie_params(stcb, m,
2270 initack_offset + sizeof(struct sctp_init_ack_chunk),
2271 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2273 struct sctp_auth_chunk *auth;
2275 auth = (struct sctp_auth_chunk *)
2276 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2277 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2278 /* auth HMAC failed, dump the assoc and packet */
2279 SCTPDBG(SCTP_DEBUG_AUTH1,
2280 "COOKIE-ECHO: AUTH failed\n");
2281 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2282 atomic_add_int(&stcb->asoc.refcnt, 1);
2283 SCTP_TCB_UNLOCK(stcb);
2284 SCTP_SOCKET_LOCK(so, 1);
2285 SCTP_TCB_LOCK(stcb);
2286 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2288 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2289 SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2290 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2291 SCTP_SOCKET_UNLOCK(so, 1);
2295 /* remaining chunks checked... good to go */
2296 stcb->asoc.authenticated = 1;
2301 * if we're doing ASCONFs, check to see if we have any new local
2302 * addresses that need to get added to the peer (eg. addresses
2303 * changed while cookie echo in flight). This needs to be done
2304 * after we go to the OPEN state to do the correct asconf
2305 * processing. else, make sure we have the correct addresses in our
2309 /* warning, we re-use sin, sin6, sa_store here! */
2310 /* pull in local_address (our "from" address) */
2311 switch (cookie->laddr_type) {
2313 case SCTP_IPV4_ADDRESS:
2314 /* source addr is IPv4 */
2315 memset(&store.sin, 0, sizeof(struct sockaddr_in));
2316 store.sin.sin_family = AF_INET;
2317 store.sin.sin_len = sizeof(struct sockaddr_in);
2318 store.sin.sin_addr.s_addr = cookie->laddress[0];
2322 case SCTP_IPV6_ADDRESS:
2323 /* source addr is IPv6 */
2324 memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
2325 store.sin6.sin6_family = AF_INET6;
2326 store.sin6.sin6_len = sizeof(struct sockaddr_in6);
2327 store.sin6.sin6_scope_id = cookie->scope_id;
2328 memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2332 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2333 atomic_add_int(&stcb->asoc.refcnt, 1);
2334 SCTP_TCB_UNLOCK(stcb);
2335 SCTP_SOCKET_LOCK(so, 1);
2336 SCTP_TCB_LOCK(stcb);
2337 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2339 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2340 SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2341 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2342 SCTP_SOCKET_UNLOCK(so, 1);
2347 /* update current state */
2348 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2349 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2350 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2351 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2352 stcb->sctp_ep, stcb, asoc->primary_destination);
2354 sctp_stop_all_cookie_timers(stcb);
2355 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2356 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2358 /* set up to notify upper layer */
2359 *notification = SCTP_NOTIFY_ASSOC_UP;
2360 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2361 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2362 (!SCTP_IS_LISTENING(inp))) {
2364 * This is an endpoint that called connect() how it got a
2365 * cookie that is NEW is a bit of a mystery. It must be that
2366 * the INIT was sent, but before it got there.. a complete
2367 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2368 * should have went to the other code.. not here.. oh well..
2369 * a bit of protection is worth having..
2371 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2372 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2373 atomic_add_int(&stcb->asoc.refcnt, 1);
2374 SCTP_TCB_UNLOCK(stcb);
2375 SCTP_SOCKET_LOCK(so, 1);
2376 SCTP_TCB_LOCK(stcb);
2377 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2378 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2379 SCTP_SOCKET_UNLOCK(so, 1);
2383 soisconnected(stcb->sctp_socket);
2384 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2385 SCTP_SOCKET_UNLOCK(so, 1);
2387 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2388 (SCTP_IS_LISTENING(inp))) {
2390 * We don't want to do anything with this one. Since it is
2391 * the listening guy. The timer will get started for
2392 * accepted connections in the caller.
2396 /* since we did not send a HB make sure we don't double things */
2397 if ((netp) && (*netp))
2398 (*netp)->hb_responded = 1;
2400 if (stcb->asoc.sctp_autoclose_ticks &&
2401 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2402 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2404 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2405 if ((netp != NULL) && (*netp != NULL)) {
2408 /* calculate the RTT and set the encaps port */
2409 old.tv_sec = cookie->time_entered.tv_sec;
2410 old.tv_usec = cookie->time_entered.tv_usec;
2411 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2412 &old, SCTP_RTT_FROM_NON_DATA);
2414 /* respond with a COOKIE-ACK */
2415 sctp_send_cookie_ack(stcb);
2418 * check the address lists for any ASCONFs that need to be sent
2419 * AFTER the cookie-ack is sent
2421 sctp_check_address_list(stcb, m,
2422 initack_offset + sizeof(struct sctp_init_ack_chunk),
2423 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2424 &store.sa, cookie->local_scope, cookie->site_scope,
2425 cookie->ipv4_scope, cookie->loopback_scope);
2432 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2433 * we NEED to make sure we are not already using the vtag. If so we
2434 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2435 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2436 SCTP_BASE_INFO(hashasocmark))];
2437 LIST_FOREACH(stcb, head, sctp_asocs) {
2438 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2439 -- SEND ABORT - TRY AGAIN --
2445 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2446 * existing (non-NULL) TCB
2448 static struct mbuf *
2449 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2450 struct sockaddr *src, struct sockaddr *dst,
2451 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2452 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2453 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2454 struct sctp_tcb **locked_tcb,
2455 uint8_t mflowtype, uint32_t mflowid,
2456 uint32_t vrf_id, uint16_t port)
2458 struct sctp_state_cookie *cookie;
2459 struct sctp_tcb *l_stcb = *stcb;
2460 struct sctp_inpcb *l_inp;
2461 struct sockaddr *to;
2462 struct sctp_pcb *ep;
2464 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2466 uint8_t cookie_ok = 0;
2467 unsigned int sig_offset, cookie_offset;
2468 unsigned int cookie_len;
2470 struct timeval time_expires;
2471 int notification = 0;
2472 struct sctp_nets *netl;
2473 int had_a_existing_tcb = 0;
2474 int send_int_conf = 0;
2476 struct sockaddr_in sin;
2479 struct sockaddr_in6 sin6;
2482 SCTPDBG(SCTP_DEBUG_INPUT2,
2483 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2485 if (inp_p == NULL) {
2488 cookie = &cp->cookie;
2489 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2490 cookie_len = ntohs(cp->ch.chunk_length);
2492 if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2493 sizeof(struct sctp_init_chunk) +
2494 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2495 /* cookie too small */
2498 if ((cookie->peerport != sh->src_port) ||
2499 (cookie->myport != sh->dest_port) ||
2500 (cookie->my_vtag != sh->v_tag)) {
2502 * invalid ports or bad tag. Note that we always leave the
2503 * v_tag in the header in network order and when we stored
2504 * it in the my_vtag slot we also left it in network order.
2505 * This maintains the match even though it may be in the
2506 * opposite byte order of the machine :->
2511 * split off the signature into its own mbuf (since it should not be
2512 * calculated in the sctp_hmac_m() call).
2514 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2515 m_sig = m_split(m, sig_offset, M_NOWAIT);
2516 if (m_sig == NULL) {
2517 /* out of memory or ?? */
2520 #ifdef SCTP_MBUF_LOGGING
2521 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2522 sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
2527 * compute the signature/digest for the cookie
2529 ep = &(*inp_p)->sctp_ep;
2532 SCTP_TCB_UNLOCK(l_stcb);
2534 SCTP_INP_RLOCK(l_inp);
2536 SCTP_TCB_LOCK(l_stcb);
2538 /* which cookie is it? */
2539 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2540 (ep->current_secret_number != ep->last_secret_number)) {
2541 /* it's the old cookie */
2542 (void)sctp_hmac_m(SCTP_HMAC,
2543 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2544 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2546 /* it's the current cookie */
2547 (void)sctp_hmac_m(SCTP_HMAC,
2548 (uint8_t *)ep->secret_key[(int)ep->current_secret_number],
2549 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2551 /* get the signature */
2552 SCTP_INP_RUNLOCK(l_inp);
2553 sig = (uint8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *)&tmp_sig);
2555 /* couldn't find signature */
2556 sctp_m_freem(m_sig);
2559 /* compare the received digest with the computed digest */
2560 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2561 /* try the old cookie? */
2562 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2563 (ep->current_secret_number != ep->last_secret_number)) {
2564 /* compute digest with old */
2565 (void)sctp_hmac_m(SCTP_HMAC,
2566 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2567 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2569 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2577 * Now before we continue we must reconstruct our mbuf so that
2578 * normal processing of any other chunks will work.
2584 while (SCTP_BUF_NEXT(m_at) != NULL) {
2585 m_at = SCTP_BUF_NEXT(m_at);
2587 SCTP_BUF_NEXT(m_at) = m_sig;
2590 if (cookie_ok == 0) {
2591 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2592 SCTPDBG(SCTP_DEBUG_INPUT2,
2593 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2594 (uint32_t)offset, cookie_offset, sig_offset);
2599 * check the cookie timestamps to be sure it's not stale
2601 (void)SCTP_GETTIME_TIMEVAL(&now);
2602 /* Expire time is in Ticks, so we convert to seconds */
2603 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2604 time_expires.tv_usec = cookie->time_entered.tv_usec;
2605 if (timevalcmp(&now, &time_expires, >)) {
2606 /* cookie is stale! */
2607 struct mbuf *op_err;
2608 struct sctp_error_stale_cookie *cause;
2609 struct timeval diff;
2612 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2613 0, M_NOWAIT, 1, MT_DATA);
2614 if (op_err == NULL) {
2619 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
2620 cause = mtod(op_err, struct sctp_error_stale_cookie *);
2621 cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
2622 cause->cause.length = htons((sizeof(struct sctp_paramhdr) +
2623 (sizeof(uint32_t))));
2625 timevalsub(&diff, &time_expires);
2626 if ((uint32_t)diff.tv_sec > UINT32_MAX / 1000000) {
2627 staleness = UINT32_MAX;
2629 staleness = diff.tv_sec * 1000000;
2631 if (UINT32_MAX - staleness >= (uint32_t)diff.tv_usec) {
2632 staleness += diff.tv_usec;
2634 staleness = UINT32_MAX;
2636 cause->stale_time = htonl(staleness);
2637 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2638 mflowtype, mflowid, l_inp->fibnum,
2643 * Now we must see with the lookup address if we have an existing
2644 * asoc. This will only happen if we were in the COOKIE-WAIT state
2645 * and a INIT collided with us and somewhere the peer sent the
2646 * cookie on another address besides the single address our assoc
2647 * had for him. In this case we will have one of the tie-tags set at
2648 * least AND the address field in the cookie can be used to look it
2652 switch (cookie->addr_type) {
2654 case SCTP_IPV6_ADDRESS:
2655 memset(&sin6, 0, sizeof(sin6));
2656 sin6.sin6_family = AF_INET6;
2657 sin6.sin6_len = sizeof(sin6);
2658 sin6.sin6_port = sh->src_port;
2659 sin6.sin6_scope_id = cookie->scope_id;
2660 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2661 sizeof(sin6.sin6_addr.s6_addr));
2662 to = (struct sockaddr *)&sin6;
2666 case SCTP_IPV4_ADDRESS:
2667 memset(&sin, 0, sizeof(sin));
2668 sin.sin_family = AF_INET;
2669 sin.sin_len = sizeof(sin);
2670 sin.sin_port = sh->src_port;
2671 sin.sin_addr.s_addr = cookie->address[0];
2672 to = (struct sockaddr *)&sin;
2676 /* This should not happen */
2679 if (*stcb == NULL) {
2680 /* Yep, lets check */
2681 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2682 if (*stcb == NULL) {
2684 * We should have only got back the same inp. If we
2685 * got back a different ep we have a problem. The
2686 * original findep got back l_inp and now
2688 if (l_inp != *inp_p) {
2689 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2692 if (*locked_tcb == NULL) {
2694 * In this case we found the assoc only
2695 * after we locked the create lock. This
2696 * means we are in a colliding case and we
2697 * must make sure that we unlock the tcb if
2698 * its one of the cases where we throw away
2699 * the incoming packets.
2701 *locked_tcb = *stcb;
2704 * We must also increment the inp ref count
2705 * since the ref_count flags was set when we
2706 * did not find the TCB, now we found it
2707 * which reduces the refcount.. we must
2708 * raise it back out to balance it all :-)
2710 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2711 if ((*stcb)->sctp_ep != l_inp) {
2712 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2713 (void *)(*stcb)->sctp_ep, (void *)l_inp);
2719 cookie_len -= SCTP_SIGNATURE_SIZE;
2720 if (*stcb == NULL) {
2721 /* this is the "normal" case... get a new TCB */
2722 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2723 cookie, cookie_len, *inp_p,
2724 netp, to, ¬ification,
2725 auth_skipped, auth_offset, auth_len,
2729 /* this is abnormal... cookie-echo on existing TCB */
2730 had_a_existing_tcb = 1;
2731 *stcb = sctp_process_cookie_existing(m, iphlen, offset,
2733 cookie, cookie_len, *inp_p, *stcb, netp, to,
2734 ¬ification, auth_skipped, auth_offset, auth_len,
2739 if (*stcb == NULL) {
2740 /* still no TCB... must be bad cookie-echo */
2743 if (*netp != NULL) {
2744 (*netp)->flowtype = mflowtype;
2745 (*netp)->flowid = mflowid;
2748 * Ok, we built an association so confirm the address we sent the
2751 netl = sctp_findnet(*stcb, to);
2753 * This code should in theory NOT run but
2756 /* TSNH! Huh, why do I need to add this address here? */
2757 if (sctp_add_remote_addr(*stcb, to, NULL, port,
2758 SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2761 netl = sctp_findnet(*stcb, to);
2764 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2765 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2766 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2771 sctp_start_net_timers(*stcb);
2772 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2773 if (!had_a_existing_tcb ||
2774 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2776 * If we have a NEW cookie or the connect never
2777 * reached the connected state during collision we
2778 * must do the TCP accept thing.
2780 struct socket *so, *oso;
2781 struct sctp_inpcb *inp;
2783 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2785 * For a restart we will keep the same
2786 * socket, no need to do anything. I THINK!!
2788 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2789 if (send_int_conf) {
2790 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2791 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2795 oso = (*inp_p)->sctp_socket;
2796 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2797 SCTP_TCB_UNLOCK((*stcb));
2798 CURVNET_SET(oso->so_vnet);
2799 so = sonewconn(oso, 0
2802 SCTP_TCB_LOCK((*stcb));
2803 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2806 struct mbuf *op_err;
2807 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2808 struct socket *pcb_so;
2810 /* Too many sockets */
2811 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2812 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2813 sctp_abort_association(*inp_p, NULL, m, iphlen,
2814 src, dst, sh, op_err,
2817 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2818 pcb_so = SCTP_INP_SO(*inp_p);
2819 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2820 SCTP_TCB_UNLOCK((*stcb));
2821 SCTP_SOCKET_LOCK(pcb_so, 1);
2822 SCTP_TCB_LOCK((*stcb));
2823 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2825 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2826 SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2827 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2828 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2832 inp = (struct sctp_inpcb *)so->so_pcb;
2833 SCTP_INP_INCR_REF(inp);
2835 * We add the unbound flag here so that if we get an
2836 * soabort() before we get the move_pcb done, we
2837 * will properly cleanup.
2839 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2840 SCTP_PCB_FLAGS_CONNECTED |
2841 SCTP_PCB_FLAGS_IN_TCPPOOL |
2842 SCTP_PCB_FLAGS_UNBOUND |
2843 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2844 SCTP_PCB_FLAGS_DONT_WAKE);
2845 inp->sctp_features = (*inp_p)->sctp_features;
2846 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2847 inp->sctp_socket = so;
2848 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2849 inp->max_cwnd = (*inp_p)->max_cwnd;
2850 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2851 inp->ecn_supported = (*inp_p)->ecn_supported;
2852 inp->prsctp_supported = (*inp_p)->prsctp_supported;
2853 inp->auth_supported = (*inp_p)->auth_supported;
2854 inp->asconf_supported = (*inp_p)->asconf_supported;
2855 inp->reconfig_supported = (*inp_p)->reconfig_supported;
2856 inp->nrsack_supported = (*inp_p)->nrsack_supported;
2857 inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2858 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2859 inp->sctp_context = (*inp_p)->sctp_context;
2860 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2861 inp->fibnum = (*inp_p)->fibnum;
2862 inp->inp_starting_point_for_iterator = NULL;
2864 * copy in the authentication parameters from the
2867 if (inp->sctp_ep.local_hmacs)
2868 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2869 inp->sctp_ep.local_hmacs =
2870 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2871 if (inp->sctp_ep.local_auth_chunks)
2872 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2873 inp->sctp_ep.local_auth_chunks =
2874 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2877 * Now we must move it from one hash table to
2878 * another and get the tcb in the right place.
2882 * This is where the one-2-one socket is put into
2883 * the accept state waiting for the accept!
2886 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2888 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2890 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2891 SCTP_TCB_UNLOCK((*stcb));
2893 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2895 SCTP_TCB_LOCK((*stcb));
2896 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2900 * now we must check to see if we were aborted while
2901 * the move was going on and the lock/unlock
2904 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2906 * yep it was, we leave the assoc attached
2907 * to the socket since the sctp_inpcb_free()
2908 * call will send an abort for us.
2910 SCTP_INP_DECR_REF(inp);
2913 SCTP_INP_DECR_REF(inp);
2914 /* Switch over to the new guy */
2916 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2917 if (send_int_conf) {
2918 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2919 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2923 * Pull it from the incomplete queue and wake the
2926 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2927 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2928 SCTP_TCB_UNLOCK((*stcb));
2929 SCTP_SOCKET_LOCK(so, 1);
2932 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2933 SCTP_TCB_LOCK((*stcb));
2934 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2935 SCTP_SOCKET_UNLOCK(so, 1);
2941 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2943 if (send_int_conf) {
2944 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2945 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2951 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2952 struct sctp_tcb *stcb, struct sctp_nets *net)
2954 /* cp must not be used, others call this without a c-ack :-) */
2955 struct sctp_association *asoc;
2957 SCTPDBG(SCTP_DEBUG_INPUT2,
2958 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2959 if ((stcb == NULL) || (net == NULL)) {
2964 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2965 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2966 asoc->overall_error_count,
2968 SCTP_FROM_SCTP_INPUT,
2971 asoc->overall_error_count = 0;
2972 sctp_stop_all_cookie_timers(stcb);
2973 /* process according to association state */
2974 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2975 /* state change only needed when I am in right state */
2976 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2977 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2978 sctp_start_net_timers(stcb);
2979 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2980 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2981 stcb->sctp_ep, stcb, asoc->primary_destination);
2985 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2986 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2987 if (asoc->overall_error_count == 0) {
2988 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2989 &asoc->time_entered,
2990 SCTP_RTT_FROM_NON_DATA);
2992 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2993 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2994 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2995 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2996 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3000 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
3001 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3002 so = SCTP_INP_SO(stcb->sctp_ep);
3003 atomic_add_int(&stcb->asoc.refcnt, 1);
3004 SCTP_TCB_UNLOCK(stcb);
3005 SCTP_SOCKET_LOCK(so, 1);
3006 SCTP_TCB_LOCK(stcb);
3007 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3009 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
3010 soisconnected(stcb->sctp_socket);
3012 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3013 SCTP_SOCKET_UNLOCK(so, 1);
3017 * since we did not send a HB make sure we don't double
3020 net->hb_responded = 1;
3022 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3024 * We don't need to do the asconf thing, nor hb or
3025 * autoclose if the socket is closed.
3030 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
3034 if (stcb->asoc.sctp_autoclose_ticks &&
3035 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
3036 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
3037 stcb->sctp_ep, stcb, NULL);
3040 * send ASCONF if parameters are pending and ASCONFs are
3041 * allowed (eg. addresses changed when init/cookie echo were
3044 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
3045 (stcb->asoc.asconf_supported == 1) &&
3046 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
3047 #ifdef SCTP_TIMER_BASED_ASCONF
3048 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
3049 stcb->sctp_ep, stcb,
3050 stcb->asoc.primary_destination);
3052 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
3053 SCTP_ADDR_NOT_LOCKED);
3058 /* Toss the cookie if I can */
3059 sctp_toss_old_cookies(stcb, asoc);
3060 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3061 /* Restart the timer if we have pending data */
3062 struct sctp_tmit_chunk *chk;
3064 chk = TAILQ_FIRST(&asoc->sent_queue);
3065 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
3070 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
3071 struct sctp_tcb *stcb)
3073 struct sctp_nets *net;
3074 struct sctp_tmit_chunk *lchk;
3075 struct sctp_ecne_chunk bkup;
3076 uint8_t override_bit;
3077 uint32_t tsn, window_data_tsn;
3079 unsigned int pkt_cnt;
3081 len = ntohs(cp->ch.chunk_length);
3082 if ((len != sizeof(struct sctp_ecne_chunk)) &&
3083 (len != sizeof(struct old_sctp_ecne_chunk))) {
3086 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3087 /* Its the old format */
3088 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3089 bkup.num_pkts_since_cwr = htonl(1);
3092 SCTP_STAT_INCR(sctps_recvecne);
3093 tsn = ntohl(cp->tsn);
3094 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3095 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3097 window_data_tsn = stcb->asoc.sending_seq - 1;
3099 window_data_tsn = lchk->rec.data.tsn;
3102 /* Find where it was sent to if possible. */
3104 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3105 if (lchk->rec.data.tsn == tsn) {
3107 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3110 if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) {
3116 * What to do. A previous send of a CWR was possibly lost.
3117 * See how old it is, we may have it marked on the actual
3120 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3121 if (tsn == net->last_cwr_tsn) {
3122 /* Found him, send it off */
3128 * If we reach here, we need to send a special CWR
3129 * that says hey, we did this a long time ago and
3130 * you lost the response.
3132 net = TAILQ_FIRST(&stcb->asoc.nets);
3137 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3144 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3145 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3147 * JRS - Use the congestion control given in the pluggable
3150 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3152 * We reduce once every RTT. So we will only lower cwnd at
3153 * the next sending seq i.e. the window_data_tsn
3155 net->cwr_window_tsn = window_data_tsn;
3156 net->ecn_ce_pkt_cnt += pkt_cnt;
3157 net->lost_cnt = pkt_cnt;
3158 net->last_cwr_tsn = tsn;
3160 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3161 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3162 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3164 * Another loss in the same window update how many
3165 * marks/packets lost we have had.
3169 if (pkt_cnt > net->lost_cnt) {
3170 /* Should be the case */
3171 cnt = (pkt_cnt - net->lost_cnt);
3172 net->ecn_ce_pkt_cnt += cnt;
3174 net->lost_cnt = pkt_cnt;
3175 net->last_cwr_tsn = tsn;
3177 * Most CC functions will ignore this call, since we
3178 * are in-window yet of the initial CE the peer saw.
3180 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3184 * We always send a CWR this way if our previous one was lost our
3185 * peer will get an update, or if it is not time again to reduce we
3186 * still get the cwr to the peer. Note we set the override when we
3187 * could not find the TSN on the chunk or the destination network.
3189 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3193 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3196 * Here we get a CWR from the peer. We must look in the outqueue and
3197 * make sure that we have a covered ECNE in the control chunk part.
3200 struct sctp_tmit_chunk *chk, *nchk;
3201 struct sctp_ecne_chunk *ecne;
3205 cwr_tsn = ntohl(cp->tsn);
3206 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3207 TAILQ_FOREACH_SAFE(chk, &stcb->asoc.control_send_queue, sctp_next, nchk) {
3208 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3211 if ((override == 0) && (chk->whoTo != net)) {
3212 /* Must be from the right src unless override is set */
3215 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3216 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3217 /* this covers this ECNE, we can remove it */
3218 stcb->asoc.ecn_echo_cnt_onq--;
3219 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3221 stcb->asoc.ctrl_queue_cnt--;
3222 sctp_m_freem(chk->data);
3224 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3225 if (override == 0) {
3233 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3234 struct sctp_tcb *stcb, struct sctp_nets *net)
3236 struct sctp_association *asoc;
3237 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3241 SCTPDBG(SCTP_DEBUG_INPUT2,
3242 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3247 /* process according to association state */
3248 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3249 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3250 SCTPDBG(SCTP_DEBUG_INPUT2,
3251 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3252 SCTP_TCB_UNLOCK(stcb);
3255 /* notify upper layer protocol */
3256 if (stcb->sctp_socket) {
3257 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3260 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3261 !TAILQ_EMPTY(&asoc->sent_queue) ||
3262 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
3263 panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3266 /* stop the timer */
3267 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3268 SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3269 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3271 SCTPDBG(SCTP_DEBUG_INPUT2,
3272 "sctp_handle_shutdown_complete: calls free-asoc\n");
3273 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3274 so = SCTP_INP_SO(stcb->sctp_ep);
3275 atomic_add_int(&stcb->asoc.refcnt, 1);
3276 SCTP_TCB_UNLOCK(stcb);
3277 SCTP_SOCKET_LOCK(so, 1);
3278 SCTP_TCB_LOCK(stcb);
3279 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3281 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3282 SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3283 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3284 SCTP_SOCKET_UNLOCK(so, 1);
3290 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3291 struct sctp_nets *net, uint8_t flg)
3293 switch (desc->chunk_type) {
3295 /* find the tsn to resend (possibly */
3298 struct sctp_tmit_chunk *tp1;
3300 tsn = ntohl(desc->tsn_ifany);
3301 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3302 if (tp1->rec.data.tsn == tsn) {
3306 if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) {
3314 * Do it the other way , aka without paying
3315 * attention to queue seq order.
3317 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3318 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3319 if (tp1->rec.data.tsn == tsn) {
3326 SCTP_STAT_INCR(sctps_pdrptsnnf);
3328 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3331 if (((flg & SCTP_BADCRC) == 0) &&
3332 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3335 if ((stcb->asoc.peers_rwnd == 0) &&
3336 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3337 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3340 if (stcb->asoc.peers_rwnd == 0 &&
3341 (flg & SCTP_FROM_MIDDLE_BOX)) {
3342 SCTP_STAT_INCR(sctps_pdrpdizrw);
3345 ddp = (uint8_t *)(mtod(tp1->data, caddr_t)+
3346 sizeof(struct sctp_data_chunk));
3350 for (iii = 0; iii < sizeof(desc->data_bytes);
3352 if (ddp[iii] != desc->data_bytes[iii]) {
3353 SCTP_STAT_INCR(sctps_pdrpbadd);
3361 * this guy had a RTO calculation
3362 * pending on it, cancel it
3364 if (tp1->whoTo->rto_needed == 0) {
3365 tp1->whoTo->rto_needed = 1;
3369 SCTP_STAT_INCR(sctps_pdrpmark);
3370 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3371 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3373 * mark it as if we were doing a FR, since
3374 * we will be getting gap ack reports behind
3375 * the info from the router.
3377 tp1->rec.data.doing_fast_retransmit = 1;
3379 * mark the tsn with what sequences can
3382 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3383 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3385 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
3388 /* restart the timer */
3389 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3391 SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3392 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3395 /* fix counts and things */
3396 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3397 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3398 tp1->whoTo->flight_size,
3400 (uint32_t)(uintptr_t)stcb,
3403 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3404 sctp_flight_size_decrease(tp1);
3405 sctp_total_flight_decrease(stcb, tp1);
3407 tp1->sent = SCTP_DATAGRAM_RESEND;
3413 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3414 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3417 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3419 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3422 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3423 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3424 audit, stcb->asoc.sent_queue_retran_cnt);
3425 #ifndef SCTP_AUDITING_ENABLED
3426 stcb->asoc.sent_queue_retran_cnt = audit;
3434 struct sctp_tmit_chunk *asconf;
3436 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3438 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3443 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3444 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3445 asconf->sent = SCTP_DATAGRAM_RESEND;
3446 asconf->snd_count--;
3450 case SCTP_INITIATION:
3451 /* resend the INIT */
3452 stcb->asoc.dropped_special_cnt++;
3453 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3455 * If we can get it in, in a few attempts we do
3456 * this, otherwise we let the timer fire.
3458 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3460 SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3461 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3464 case SCTP_SELECTIVE_ACK:
3465 case SCTP_NR_SELECTIVE_ACK:
3466 /* resend the sack */
3467 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3469 case SCTP_HEARTBEAT_REQUEST:
3470 /* resend a demand HB */
3471 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3473 * Only retransmit if we KNOW we wont destroy the
3476 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3480 sctp_send_shutdown(stcb, net);
3482 case SCTP_SHUTDOWN_ACK:
3483 sctp_send_shutdown_ack(stcb, net);
3485 case SCTP_COOKIE_ECHO:
3487 struct sctp_tmit_chunk *cookie;
3490 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3492 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3497 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3498 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3499 cookie->sent = SCTP_DATAGRAM_RESEND;
3500 sctp_stop_all_cookie_timers(stcb);
3504 case SCTP_COOKIE_ACK:
3505 sctp_send_cookie_ack(stcb);
3507 case SCTP_ASCONF_ACK:
3508 /* resend last asconf ack */
3509 sctp_send_asconf_ack(stcb);
3511 case SCTP_IFORWARD_CUM_TSN:
3512 case SCTP_FORWARD_CUM_TSN:
3513 send_forward_tsn(stcb, &stcb->asoc);
3515 /* can't do anything with these */
3516 case SCTP_PACKET_DROPPED:
3517 case SCTP_INITIATION_ACK: /* this should not happen */
3518 case SCTP_HEARTBEAT_ACK:
3519 case SCTP_ABORT_ASSOCIATION:
3520 case SCTP_OPERATION_ERROR:
3521 case SCTP_SHUTDOWN_COMPLETE:
3531 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3537 * We set things to 0xffffffff since this is the last delivered
3538 * sequence and we will be sending in 0 after the reset.
3541 if (number_entries) {
3542 for (i = 0; i < number_entries; i++) {
3543 temp = ntohs(list[i]);
3544 if (temp >= stcb->asoc.streamincnt) {
3547 stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff;
3551 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3552 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
3555 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3559 sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3564 if (number_entries > 0) {
3565 for (i = 0; i < number_entries; i++) {
3566 temp = ntohs(list[i]);
3567 if (temp >= stcb->asoc.streamoutcnt) {
3568 /* no such stream */
3571 stcb->asoc.strmout[temp].next_mid_ordered = 0;
3572 stcb->asoc.strmout[temp].next_mid_unordered = 0;
3575 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3576 stcb->asoc.strmout[i].next_mid_ordered = 0;
3577 stcb->asoc.strmout[i].next_mid_unordered = 0;
3580 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3584 sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3589 if (number_entries > 0) {
3590 for (i = 0; i < number_entries; i++) {
3591 temp = ntohs(list[i]);
3592 if (temp >= stcb->asoc.streamoutcnt) {
3593 /* no such stream */
3596 stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
3599 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3600 stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
3606 struct sctp_stream_reset_request *
3607 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3609 struct sctp_association *asoc;
3610 struct sctp_chunkhdr *ch;
3611 struct sctp_stream_reset_request *r;
3612 struct sctp_tmit_chunk *chk;
3616 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3617 asoc->stream_reset_outstanding = 0;
3620 if (stcb->asoc.str_reset == NULL) {
3621 asoc->stream_reset_outstanding = 0;
3624 chk = stcb->asoc.str_reset;
3625 if (chk->data == NULL) {
3629 /* he wants a copy of the chk pointer */
3632 clen = chk->send_size;
3633 ch = mtod(chk->data, struct sctp_chunkhdr *);
3634 r = (struct sctp_stream_reset_request *)(ch + 1);
3635 if (ntohl(r->request_seq) == seq) {
3639 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3640 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3641 /* move to the next one, there can only be a max of two */
3642 r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3643 if (ntohl(r->request_seq) == seq) {
3647 /* that seq is not here */
3652 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3654 struct sctp_association *asoc;
3655 struct sctp_tmit_chunk *chk;
3658 chk = asoc->str_reset;
3662 asoc->str_reset = NULL;
3663 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
3664 chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3665 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3666 asoc->ctrl_queue_cnt--;
3668 sctp_m_freem(chk->data);
3671 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3676 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3677 uint32_t seq, uint32_t action,
3678 struct sctp_stream_reset_response *respin)
3682 struct sctp_association *asoc = &stcb->asoc;
3683 struct sctp_tmit_chunk *chk;
3684 struct sctp_stream_reset_request *req_param;
3685 struct sctp_stream_reset_out_request *req_out_param;
3686 struct sctp_stream_reset_in_request *req_in_param;
3687 uint32_t number_entries;
3689 if (asoc->stream_reset_outstanding == 0) {
3693 if (seq == stcb->asoc.str_reset_seq_out) {
3694 req_param = sctp_find_stream_reset(stcb, seq, &chk);
3695 if (req_param != NULL) {
3696 stcb->asoc.str_reset_seq_out++;
3697 type = ntohs(req_param->ph.param_type);
3698 lparam_len = ntohs(req_param->ph.param_length);
3699 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3702 req_out_param = (struct sctp_stream_reset_out_request *)req_param;
3703 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3704 asoc->stream_reset_out_is_outstanding = 0;
3705 if (asoc->stream_reset_outstanding)
3706 asoc->stream_reset_outstanding--;
3707 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3709 sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3710 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3711 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3712 } else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
3714 * Set it up so we don't stop
3717 asoc->stream_reset_outstanding++;
3718 stcb->asoc.str_reset_seq_out--;
3719 asoc->stream_reset_out_is_outstanding = 1;
3722 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3724 if (no_clear == 0) {
3725 sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
3727 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3728 req_in_param = (struct sctp_stream_reset_in_request *)req_param;
3729 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3730 if (asoc->stream_reset_outstanding)
3731 asoc->stream_reset_outstanding--;
3732 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3733 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3734 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3735 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3736 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3737 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3739 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3740 /* Ok we now may have more streams */
3743 num_stream = stcb->asoc.strm_pending_add_size;
3744 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3746 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3748 stcb->asoc.strm_pending_add_size = 0;
3749 if (asoc->stream_reset_outstanding)
3750 asoc->stream_reset_outstanding--;
3751 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3752 /* Put the new streams into effect */
3755 for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
3756 asoc->strmout[i].state = SCTP_STREAM_OPEN;
3758 asoc->streamoutcnt += num_stream;
3759 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3760 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3761 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3762 SCTP_STREAM_CHANGE_DENIED);
3764 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3765 SCTP_STREAM_CHANGE_FAILED);
3767 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3768 if (asoc->stream_reset_outstanding)
3769 asoc->stream_reset_outstanding--;
3770 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3771 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3772 SCTP_STREAM_CHANGE_DENIED);
3773 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3774 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3775 SCTP_STREAM_CHANGE_FAILED);
3777 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3779 * a) Adopt the new in tsn.
3781 * c) Adopt the new out-tsn
3783 struct sctp_stream_reset_response_tsn *resp;
3784 struct sctp_forward_tsn_chunk fwdtsn;
3787 if (respin == NULL) {
3791 if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
3794 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3795 resp = (struct sctp_stream_reset_response_tsn *)respin;
3796 asoc->stream_reset_outstanding--;
3797 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3798 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3799 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3800 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3804 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3805 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3806 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3809 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3810 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3811 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3813 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3814 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3816 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3817 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3819 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3820 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3821 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3822 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3823 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3824 SCTP_ASSOC_RESET_DENIED);
3826 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3827 SCTP_ASSOC_RESET_FAILED);
3830 /* get rid of the request and get the request flags */
3831 if (asoc->stream_reset_outstanding == 0) {
3832 sctp_clean_up_stream_reset(stcb);
3836 if (asoc->stream_reset_outstanding == 0) {
3837 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3843 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3844 struct sctp_tmit_chunk *chk,
3845 struct sctp_stream_reset_in_request *req, int trunc)
3853 * peer wants me to send a str-reset to him for my outgoing seq's if
3856 struct sctp_association *asoc = &stcb->asoc;
3858 seq = ntohl(req->request_seq);
3859 if (asoc->str_reset_seq_in == seq) {
3860 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3861 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3862 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3864 /* Can't do it, since they exceeded our buffer size */
3865 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3866 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3867 len = ntohs(req->ph.param_length);
3868 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3869 if (number_entries) {
3870 for (i = 0; i < number_entries; i++) {
3871 temp = ntohs(req->list_of_streams[i]);
3872 if (temp >= stcb->asoc.streamoutcnt) {
3873 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3876 req->list_of_streams[i] = temp;
3878 for (i = 0; i < number_entries; i++) {
3879 if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
3880 stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
3885 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3886 if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
3887 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
3890 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3892 /* Can't do it, since we have sent one out */
3893 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3896 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3897 asoc->str_reset_seq_in++;
3898 } else if (asoc->str_reset_seq_in - 1 == seq) {
3899 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3900 } else if (asoc->str_reset_seq_in - 2 == seq) {
3901 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3903 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3905 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3909 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3910 struct sctp_tmit_chunk *chk,
3911 struct sctp_stream_reset_tsn_request *req)
3913 /* reset all in and out and update the tsn */
3915 * A) reset my str-seq's on in and out. B) Select a receive next,
3916 * and set cum-ack to it. Also process this selected number as a
3917 * fwd-tsn as well. C) set in the response my next sending seq.
3919 struct sctp_forward_tsn_chunk fwdtsn;
3920 struct sctp_association *asoc = &stcb->asoc;
3924 seq = ntohl(req->request_seq);
3925 if (asoc->str_reset_seq_in == seq) {
3926 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3927 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3928 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3930 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3931 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3932 fwdtsn.ch.chunk_flags = 0;
3933 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3934 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3938 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3939 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3940 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3942 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3943 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3944 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3945 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3946 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3947 atomic_add_int(&asoc->sending_seq, 1);
3948 /* save off historical data for retrans */
3949 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3950 asoc->last_sending_seq[0] = asoc->sending_seq;
3951 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3952 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3953 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3954 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3955 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3956 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3958 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3959 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3960 asoc->str_reset_seq_in++;
3961 } else if (asoc->str_reset_seq_in - 1 == seq) {
3962 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3963 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3964 } else if (asoc->str_reset_seq_in - 2 == seq) {
3965 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3966 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3968 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3974 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3975 struct sctp_tmit_chunk *chk,
3976 struct sctp_stream_reset_out_request *req, int trunc)
3979 int number_entries, len;
3980 struct sctp_association *asoc = &stcb->asoc;
3982 seq = ntohl(req->request_seq);
3984 /* now if its not a duplicate we process it */
3985 if (asoc->str_reset_seq_in == seq) {
3986 len = ntohs(req->ph.param_length);
3987 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3989 * the sender is resetting, handle the list issue.. we must
3990 * a) verify if we can do the reset, if so no problem b) If
3991 * we can't do the reset we must copy the request. c) queue
3992 * it, and setup the data in processor to trigger it off
3993 * when needed and dequeue all the queued data.
3995 tsn = ntohl(req->send_reset_at_tsn);
3997 /* move the reset action back one */
3998 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3999 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
4000 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4002 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4003 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
4004 /* we can do it now */
4005 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
4006 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4009 * we must queue it up and thus wait for the TSN's
4010 * to arrive that are at or before tsn
4012 struct sctp_stream_reset_list *liste;
4015 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
4016 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
4017 siz, SCTP_M_STRESET);
4018 if (liste == NULL) {
4019 /* gak out of memory */
4020 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4021 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4026 liste->number_entries = number_entries;
4027 memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
4028 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
4029 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
4031 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4032 asoc->str_reset_seq_in++;
4033 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4035 * one seq back, just echo back last action since my
4036 * response was lost.
4038 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4039 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4041 * two seq back, just echo back last action since my
4042 * response was lost.
4044 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4046 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4051 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4052 struct sctp_stream_reset_add_strm *str_add)
4055 * Peer is requesting to add more streams. If its within our
4056 * max-streams we will allow it.
4058 uint32_t num_stream, i;
4060 struct sctp_association *asoc = &stcb->asoc;
4061 struct sctp_queued_to_read *ctl, *nctl;
4063 /* Get the number. */
4064 seq = ntohl(str_add->request_seq);
4065 num_stream = ntohs(str_add->number_of_streams);
4066 /* Now what would be the new total? */
4067 if (asoc->str_reset_seq_in == seq) {
4068 num_stream += stcb->asoc.streamincnt;
4069 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4070 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4071 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4072 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
4073 (num_stream > 0xffff)) {
4074 /* We must reject it they ask for to many */
4076 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4078 /* Ok, we can do that :-) */
4079 struct sctp_stream_in *oldstrm;
4081 /* save off the old */
4082 oldstrm = stcb->asoc.strmin;
4083 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
4084 (num_stream * sizeof(struct sctp_stream_in)),
4086 if (stcb->asoc.strmin == NULL) {
4087 stcb->asoc.strmin = oldstrm;
4090 /* copy off the old data */
4091 for (i = 0; i < stcb->asoc.streamincnt; i++) {
4092 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4093 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4094 stcb->asoc.strmin[i].sid = i;
4095 stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered;
4096 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
4097 stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
4098 /* now anything on those queues? */
4099 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
4100 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
4101 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
4103 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
4104 TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
4105 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
4108 /* Init the new streams */
4109 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
4110 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4111 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4112 stcb->asoc.strmin[i].sid = i;
4113 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
4114 stcb->asoc.strmin[i].pd_api_started = 0;
4115 stcb->asoc.strmin[i].delivery_started = 0;
4117 SCTP_FREE(oldstrm, SCTP_M_STRMI);
4118 /* update the size */
4119 stcb->asoc.streamincnt = num_stream;
4120 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4121 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
4123 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4124 asoc->str_reset_seq_in++;
4125 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4127 * one seq back, just echo back last action since my
4128 * response was lost.
4130 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4131 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4133 * two seq back, just echo back last action since my
4134 * response was lost.
4136 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4138 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4144 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4145 struct sctp_stream_reset_add_strm *str_add)
4148 * Peer is requesting to add more streams. If its within our
4149 * max-streams we will allow it.
4151 uint16_t num_stream;
4153 struct sctp_association *asoc = &stcb->asoc;
4155 /* Get the number. */
4156 seq = ntohl(str_add->request_seq);
4157 num_stream = ntohs(str_add->number_of_streams);
4158 /* Now what would be the new total? */
4159 if (asoc->str_reset_seq_in == seq) {
4160 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4161 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4162 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4163 } else if (stcb->asoc.stream_reset_outstanding) {
4164 /* We must reject it we have something pending */
4165 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4167 /* Ok, we can do that :-) */
4170 mychk = stcb->asoc.streamoutcnt;
4171 mychk += num_stream;
4172 if (mychk < 0x10000) {
4173 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4174 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
4175 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4178 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4181 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4182 asoc->str_reset_seq_in++;
4183 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4185 * one seq back, just echo back last action since my
4186 * response was lost.
4188 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4189 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4191 * two seq back, just echo back last action since my
4192 * response was lost.
4194 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4196 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4201 __attribute__((noinline))
4204 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4205 struct sctp_chunkhdr *ch_req)
4207 uint16_t remaining_length, param_len, ptype;
4208 struct sctp_paramhdr pstore;
4209 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4213 struct sctp_tmit_chunk *chk;
4214 struct sctp_chunkhdr *ch;
4215 struct sctp_paramhdr *ph;
4219 /* now it may be a reset or a reset-response */
4220 remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
4222 /* setup for adding the response */
4223 sctp_alloc_a_chunk(stcb, chk);
4227 chk->copy_by_ref = 0;
4228 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4229 chk->rec.chunk_id.can_take_data = 0;
4231 chk->asoc = &stcb->asoc;
4232 chk->no_fr_allowed = 0;
4233 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4234 chk->book_size_scale = 0;
4235 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4236 if (chk->data == NULL) {
4239 sctp_m_freem(chk->data);
4242 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4245 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4247 /* setup chunk parameters */
4248 chk->sent = SCTP_DATAGRAM_UNSENT;
4252 ch = mtod(chk->data, struct sctp_chunkhdr *);
4253 ch->chunk_type = SCTP_STREAM_RESET;
4254 ch->chunk_flags = 0;
4255 ch->chunk_length = htons(chk->send_size);
4256 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4257 offset += sizeof(struct sctp_chunkhdr);
4258 while (remaining_length >= sizeof(struct sctp_paramhdr)) {
4259 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
4264 param_len = ntohs(ph->param_length);
4265 if ((param_len > remaining_length) ||
4266 (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
4267 /* bad parameter length */
4270 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
4271 (uint8_t *)&cstore);
4276 ptype = ntohs(ph->param_type);
4278 if (param_len > sizeof(cstore)) {
4283 if (num_param > SCTP_MAX_RESET_PARAMS) {
4284 /* hit the max of parameters already sorry.. */
4287 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4288 struct sctp_stream_reset_out_request *req_out;
4290 if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
4293 req_out = (struct sctp_stream_reset_out_request *)ph;
4295 if (stcb->asoc.stream_reset_outstanding) {
4296 seq = ntohl(req_out->response_seq);
4297 if (seq == stcb->asoc.str_reset_seq_out) {
4299 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4302 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4303 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4304 struct sctp_stream_reset_add_strm *str_add;
4306 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4309 str_add = (struct sctp_stream_reset_add_strm *)ph;
4311 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4312 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4313 struct sctp_stream_reset_add_strm *str_add;
4315 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4318 str_add = (struct sctp_stream_reset_add_strm *)ph;
4320 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4321 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4322 struct sctp_stream_reset_in_request *req_in;
4325 req_in = (struct sctp_stream_reset_in_request *)ph;
4326 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4327 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4328 struct sctp_stream_reset_tsn_request *req_tsn;
4331 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4332 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4334 goto strres_nochunk;
4338 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4339 struct sctp_stream_reset_response *resp;
4342 if (param_len < sizeof(struct sctp_stream_reset_response)) {
4345 resp = (struct sctp_stream_reset_response *)ph;
4346 seq = ntohl(resp->response_seq);
4347 result = ntohl(resp->result);
4348 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4350 goto strres_nochunk;
4355 offset += SCTP_SIZE32(param_len);
4356 if (remaining_length >= SCTP_SIZE32(param_len)) {
4357 remaining_length -= SCTP_SIZE32(param_len);
4359 remaining_length = 0;
4363 /* we have no response free the stuff */
4364 goto strres_nochunk;
4366 /* ok we have a chunk to link in */
4367 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4370 stcb->asoc.ctrl_queue_cnt++;
4375 * Handle a router or endpoints report of a packet loss, there are two ways
4376 * to handle this, either we get the whole packet and must disect it
4377 * ourselves (possibly with truncation and or corruption) or it is a summary
4378 * from a middle box that did the disectting for us.
4381 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4382 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4384 uint32_t bottle_bw, on_queue;
4388 struct sctp_chunk_desc desc;
4389 struct sctp_chunkhdr *ch;
4391 chlen = ntohs(cp->ch.chunk_length);
4392 chlen -= sizeof(struct sctp_pktdrop_chunk);
4393 /* XXX possible chlen underflow */
4396 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4397 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4399 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4400 chlen -= sizeof(struct sctphdr);
4401 /* XXX possible chlen underflow */
4402 memset(&desc, 0, sizeof(desc));
4404 trunc_len = (uint16_t)ntohs(cp->trunc_len);
4405 if (trunc_len > limit) {
4409 /* now the chunks themselves */
4410 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4411 desc.chunk_type = ch->chunk_type;
4412 /* get amount we need to move */
4413 at = ntohs(ch->chunk_length);
4414 if (at < sizeof(struct sctp_chunkhdr)) {
4415 /* corrupt chunk, maybe at the end? */
4416 SCTP_STAT_INCR(sctps_pdrpcrupt);
4419 if (trunc_len == 0) {
4420 /* we are supposed to have all of it */
4422 /* corrupt skip it */
4423 SCTP_STAT_INCR(sctps_pdrpcrupt);
4427 /* is there enough of it left ? */
4428 if (desc.chunk_type == SCTP_DATA) {
4429 if (chlen < (sizeof(struct sctp_data_chunk) +
4430 sizeof(desc.data_bytes))) {
4434 if (chlen < sizeof(struct sctp_chunkhdr)) {
4439 if (desc.chunk_type == SCTP_DATA) {
4440 /* can we get out the tsn? */
4441 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4442 SCTP_STAT_INCR(sctps_pdrpmbda);
4444 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4446 struct sctp_data_chunk *dcp;
4450 dcp = (struct sctp_data_chunk *)ch;
4451 ddp = (uint8_t *)(dcp + 1);
4452 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4453 desc.data_bytes[iii] = ddp[iii];
4455 desc.tsn_ifany = dcp->dp.tsn;
4457 /* nope we are done. */
4458 SCTP_STAT_INCR(sctps_pdrpnedat);
4462 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4463 SCTP_STAT_INCR(sctps_pdrpmbct);
4466 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4467 SCTP_STAT_INCR(sctps_pdrppdbrk);
4470 if (SCTP_SIZE32(at) > chlen) {
4473 chlen -= SCTP_SIZE32(at);
4474 if (chlen < sizeof(struct sctp_chunkhdr)) {
4475 /* done, none left */
4478 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4480 /* Now update any rwnd --- possibly */
4481 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4482 /* From a peer, we get a rwnd report */
4485 SCTP_STAT_INCR(sctps_pdrpfehos);
4487 bottle_bw = ntohl(cp->bottle_bw);
4488 on_queue = ntohl(cp->current_onq);
4489 if (bottle_bw && on_queue) {
4490 /* a rwnd report is in here */
4491 if (bottle_bw > on_queue)
4492 a_rwnd = bottle_bw - on_queue;
4497 stcb->asoc.peers_rwnd = 0;
4499 if (a_rwnd > stcb->asoc.total_flight) {
4500 stcb->asoc.peers_rwnd =
4501 a_rwnd - stcb->asoc.total_flight;
4503 stcb->asoc.peers_rwnd = 0;
4505 if (stcb->asoc.peers_rwnd <
4506 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4507 /* SWS sender side engages */
4508 stcb->asoc.peers_rwnd = 0;
4513 SCTP_STAT_INCR(sctps_pdrpfmbox);
4516 /* now middle boxes in sat networks get a cwnd bump */
4517 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4518 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4519 (stcb->asoc.sat_network)) {
4521 * This is debatable but for sat networks it makes sense
4522 * Note if a T3 timer has went off, we will prohibit any
4523 * changes to cwnd until we exit the t3 loss recovery.
4525 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4526 net, cp, &bottle_bw, &on_queue);
4531 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4532 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4533 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4534 * length of the complete packet outputs: - length: modified to remaining
4535 * length after control processing - netp: modified to new sctp_nets after
4536 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4537 * bad packet,...) otherwise return the tcb for this packet
4540 __attribute__((noinline))
4542 static struct sctp_tcb *
4543 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4544 struct sockaddr *src, struct sockaddr *dst,
4545 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4546 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4547 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4548 uint32_t vrf_id, uint16_t port)
4550 struct sctp_association *asoc;
4551 struct mbuf *op_err;
4552 char msg[SCTP_DIAG_INFO_LEN];
4554 int num_chunks = 0; /* number of control chunks processed */
4555 uint32_t chk_length, contiguous;
4557 int abort_no_unlock = 0;
4561 * How big should this be, and should it be alloc'd? Lets try the
4562 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4563 * until we get into jumbo grams and such..
4565 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4567 uint32_t auth_offset = 0, auth_len = 0;
4568 int auth_skipped = 0;
4570 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4574 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4575 iphlen, *offset, length, (void *)stcb);
4578 SCTP_TCB_LOCK_ASSERT(stcb);
4580 /* validate chunk header length... */
4581 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4582 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4583 ntohs(ch->chunk_length));
4588 * validate the verification tag
4590 vtag_in = ntohl(sh->v_tag);
4592 if (ch->chunk_type == SCTP_INITIATION) {
4593 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4594 ntohs(ch->chunk_length), vtag_in);
4596 /* protocol error- silently discard... */
4597 SCTP_STAT_INCR(sctps_badvtag);
4599 SCTP_TCB_UNLOCK(stcb);
4603 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4605 * If there is no stcb, skip the AUTH chunk and process
4606 * later after a stcb is found (to validate the lookup was
4609 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4611 (inp->auth_supported == 1)) {
4612 /* save this chunk for later processing */
4614 auth_offset = *offset;
4615 auth_len = ntohs(ch->chunk_length);
4617 /* (temporarily) move past this chunk */
4618 *offset += SCTP_SIZE32(auth_len);
4619 if (*offset >= length) {
4620 /* no more data left in the mbuf chain */
4624 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4625 sizeof(struct sctp_chunkhdr), chunk_buf);
4632 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4633 goto process_control_chunks;
4636 * first check if it's an ASCONF with an unknown src addr we
4637 * need to look inside to find the association
4639 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4640 struct sctp_chunkhdr *asconf_ch = ch;
4641 uint32_t asconf_offset = 0, asconf_len = 0;
4643 /* inp's refcount may be reduced */
4644 SCTP_INP_INCR_REF(inp);
4646 asconf_offset = *offset;
4648 asconf_len = ntohs(asconf_ch->chunk_length);
4649 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4651 stcb = sctp_findassociation_ep_asconf(m,
4654 sh, &inp, netp, vrf_id);
4657 asconf_offset += SCTP_SIZE32(asconf_len);
4658 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4659 sizeof(struct sctp_chunkhdr), chunk_buf);
4660 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4663 * reduce inp's refcount if not reduced in
4664 * sctp_findassociation_ep_asconf().
4666 SCTP_INP_DECR_REF(inp);
4669 /* now go back and verify any auth chunk to be sure */
4670 if (auth_skipped && (stcb != NULL)) {
4671 struct sctp_auth_chunk *auth;
4673 auth = (struct sctp_auth_chunk *)
4674 sctp_m_getptr(m, auth_offset,
4675 auth_len, chunk_buf);
4678 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4680 /* auth HMAC failed so dump it */
4684 /* remaining chunks are HMAC checked */
4685 stcb->asoc.authenticated = 1;
4690 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4691 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4693 /* no association, so it's out of the blue... */
4694 sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4695 mflowtype, mflowid, inp->fibnum,
4701 /* ABORT and SHUTDOWN can use either v_tag... */
4702 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4703 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4704 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4705 /* Take the T-bit always into account. */
4706 if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
4707 (vtag_in == asoc->my_vtag)) ||
4708 (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4709 (asoc->peer_vtag != htonl(0)) &&
4710 (vtag_in == asoc->peer_vtag))) {
4713 /* drop this packet... */
4714 SCTP_STAT_INCR(sctps_badvtag);
4716 SCTP_TCB_UNLOCK(stcb);
4720 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4721 if (vtag_in != asoc->my_vtag) {
4723 * this could be a stale SHUTDOWN-ACK or the
4724 * peer never got the SHUTDOWN-COMPLETE and
4725 * is still hung; we have started a new asoc
4726 * but it won't complete until the shutdown
4730 SCTP_TCB_UNLOCK(stcb);
4732 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4733 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4735 sctp_handle_ootb(m, iphlen, *offset, src, dst,
4737 mflowtype, mflowid, fibnum,
4742 /* for all other chunks, vtag must match */
4743 if (vtag_in != asoc->my_vtag) {
4744 /* invalid vtag... */
4745 SCTPDBG(SCTP_DEBUG_INPUT3,
4746 "invalid vtag: %xh, expect %xh\n",
4747 vtag_in, asoc->my_vtag);
4748 SCTP_STAT_INCR(sctps_badvtag);
4750 SCTP_TCB_UNLOCK(stcb);
4756 } /* end if !SCTP_COOKIE_ECHO */
4758 * process all control chunks...
4760 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4761 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4762 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4763 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4764 /* implied cookie-ack.. we must have lost the ack */
4765 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4769 process_control_chunks:
4770 while (IS_SCTP_CONTROL(ch)) {
4771 /* validate chunk length */
4772 chk_length = ntohs(ch->chunk_length);
4773 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4774 ch->chunk_type, chk_length);
4775 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4776 if (chk_length < sizeof(*ch) ||
4777 (*offset + (int)chk_length) > length) {
4781 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4783 * INIT and INIT-ACK only gets the init ack "header" portion
4784 * only because we don't have to process the peer's COOKIE.
4785 * All others get a complete chunk.
4787 switch (ch->chunk_type) {
4788 case SCTP_INITIATION:
4789 contiguous = sizeof(struct sctp_init_chunk);
4791 case SCTP_INITIATION_ACK:
4792 contiguous = sizeof(struct sctp_init_ack_chunk);
4795 contiguous = min(chk_length, sizeof(chunk_buf));
4798 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4804 SCTP_TCB_UNLOCK(stcb);
4810 /* Save off the last place we got a control from */
4812 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4814 * allow last_control to be NULL if
4815 * ASCONF... ASCONF processing will find the
4818 if ((netp != NULL) && (*netp != NULL))
4819 stcb->asoc.last_control_chunk_from = *netp;
4822 #ifdef SCTP_AUDITING_ENABLED
4823 sctp_audit_log(0xB0, ch->chunk_type);
4826 /* check to see if this chunk required auth, but isn't */
4827 if ((stcb != NULL) &&
4828 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4829 !stcb->asoc.authenticated) {
4830 /* "silently" ignore */
4831 SCTP_STAT_INCR(sctps_recvauthmissing);
4834 switch (ch->chunk_type) {
4835 case SCTP_INITIATION:
4836 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4837 /* The INIT chunk must be the only chunk. */
4838 if ((num_chunks > 1) ||
4839 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4840 /* RFC 4960 requires that no ABORT is sent */
4843 SCTP_TCB_UNLOCK(stcb);
4847 /* Honor our resource limit. */
4848 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4849 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4850 sctp_abort_association(inp, stcb, m, iphlen,
4851 src, dst, sh, op_err,
4857 sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4858 (struct sctp_init_chunk *)ch, inp,
4859 stcb, *netp, &abort_no_unlock,
4863 if ((!abort_no_unlock) && (stcb != NULL)) {
4864 SCTP_TCB_UNLOCK(stcb);
4868 case SCTP_PAD_CHUNK:
4870 case SCTP_INITIATION_ACK:
4871 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT_ACK\n");
4872 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4873 /* We are not interested anymore */
4874 if ((stcb != NULL) && (stcb->asoc.total_output_queue_size)) {
4879 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4880 so = SCTP_INP_SO(inp);
4881 atomic_add_int(&stcb->asoc.refcnt, 1);
4882 SCTP_TCB_UNLOCK(stcb);
4883 SCTP_SOCKET_LOCK(so, 1);
4884 SCTP_TCB_LOCK(stcb);
4885 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4887 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4888 SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4889 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4890 SCTP_SOCKET_UNLOCK(so, 1);
4896 /* The INIT-ACK chunk must be the only chunk. */
4897 if ((num_chunks > 1) ||
4898 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4902 if ((netp != NULL) && (*netp != NULL)) {
4903 ret = sctp_handle_init_ack(m, iphlen, *offset,
4905 (struct sctp_init_ack_chunk *)ch,
4914 if (abort_no_unlock) {
4918 * Special case, I must call the output routine to
4919 * get the cookie echoed
4921 if ((stcb != NULL) && (ret == 0)) {
4922 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4926 case SCTP_SELECTIVE_ACK:
4927 case SCTP_NR_SELECTIVE_ACK:
4930 uint32_t a_rwnd, cum_ack;
4931 uint16_t num_seg, num_nr_seg, num_dup;
4933 int offset_seg, offset_dup;
4935 SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n",
4936 ch->chunk_type == SCTP_SELECTIVE_ACK ? "SCTP_SACK" : "SCTP_NR_SACK");
4937 SCTP_STAT_INCR(sctps_recvsacks);
4939 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing %s chunk\n",
4940 (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK");
4943 if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4944 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4945 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4949 if (stcb->asoc.nrsack_supported == 0) {
4952 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4953 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR_SACK chunk, too small\n");
4957 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4959 * If we have sent a shutdown-ack, we will pay no
4960 * attention to a sack sent in to us since
4961 * we don't care anymore.
4965 flags = ch->chunk_flags;
4966 if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4967 struct sctp_sack_chunk *sack;
4969 sack = (struct sctp_sack_chunk *)ch;
4970 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4971 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4973 num_dup = ntohs(sack->sack.num_dup_tsns);
4974 a_rwnd = ntohl(sack->sack.a_rwnd);
4975 if (sizeof(struct sctp_sack_chunk) +
4976 num_seg * sizeof(struct sctp_gap_ack_block) +
4977 num_dup * sizeof(uint32_t) != chk_length) {
4978 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4981 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4982 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4984 struct sctp_nr_sack_chunk *nr_sack;
4986 nr_sack = (struct sctp_nr_sack_chunk *)ch;
4987 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4988 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4989 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4990 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4991 a_rwnd = ntohl(nr_sack->nr_sack.a_rwnd);
4992 if (sizeof(struct sctp_nr_sack_chunk) +
4993 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4994 num_dup * sizeof(uint32_t) != chk_length) {
4995 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4998 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4999 offset_dup = offset_seg + (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block);
5001 SCTPDBG(SCTP_DEBUG_INPUT3, "%s process cum_ack:%x num_seg:%d a_rwnd:%d\n",
5002 (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK",
5003 cum_ack, num_seg, a_rwnd);
5004 stcb->asoc.seen_a_sack_this_pkt = 1;
5005 if ((stcb->asoc.pr_sctp_cnt == 0) &&
5006 (num_seg == 0) && (num_nr_seg == 0) &&
5007 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
5008 (stcb->asoc.saw_sack_with_frags == 0) &&
5009 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
5010 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
5012 * We have a SIMPLE sack having no
5013 * prior segments and data on sent
5014 * queue to be acked. Use the faster
5015 * path sack processing. We also
5016 * allow window update sacks with no
5017 * missing segments to go this way
5020 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
5021 &abort_now, ecne_seen);
5023 if ((netp != NULL) && (*netp != NULL)) {
5024 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
5025 num_seg, num_nr_seg, num_dup, &abort_now, flags,
5026 cum_ack, a_rwnd, ecne_seen);
5030 /* ABORT signal from sack processing */
5034 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5035 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5036 (stcb->asoc.stream_queue_cnt == 0)) {
5037 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5041 case SCTP_HEARTBEAT_REQUEST:
5042 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
5043 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5044 SCTP_STAT_INCR(sctps_recvheartbeat);
5045 sctp_send_heartbeat_ack(stcb, m, *offset,
5049 case SCTP_HEARTBEAT_ACK:
5050 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT_ACK\n");
5051 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5056 SCTP_STAT_INCR(sctps_recvheartbeatack);
5057 if ((netp != NULL) && (*netp != NULL)) {
5058 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5062 case SCTP_ABORT_ASSOCIATION:
5063 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5066 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5067 if (sctp_handle_abort((struct sctp_abort_chunk *)ch, stcb, *netp)) {
5077 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5079 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5083 if ((netp != NULL) && (*netp != NULL)) {
5086 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5087 stcb, *netp, &abort_flag);
5094 case SCTP_SHUTDOWN_ACK:
5095 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_ACK, stcb %p\n", (void *)stcb);
5096 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5097 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5102 case SCTP_OPERATION_ERROR:
5103 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP_ERR\n");
5104 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL) &&
5105 sctp_handle_error(ch, stcb, *netp, contiguous) < 0) {
5110 case SCTP_COOKIE_ECHO:
5111 SCTPDBG(SCTP_DEBUG_INPUT3,
5112 "SCTP_COOKIE_ECHO, stcb %p\n", (void *)stcb);
5113 if ((stcb != NULL) && (stcb->asoc.total_output_queue_size > 0)) {
5116 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5117 /* We are not interested anymore */
5120 SCTP_TCB_UNLOCK(stcb);
5127 * First are we accepting? We do this again here
5128 * since it is possible that a previous endpoint WAS
5129 * listening responded to a INIT-ACK and then
5130 * closed. We opened and bound.. and are now no
5133 * XXXGL: notes on checking listen queue length.
5134 * 1) SCTP_IS_LISTENING() doesn't necessarily mean
5135 * SOLISTENING(), because a listening "UDP type"
5136 * socket isn't listening in terms of the socket
5137 * layer. It is a normal data flow socket, that
5138 * can fork off new connections. Thus, we should
5139 * look into sol_qlen only in case we are !UDP.
5140 * 2) Checking sol_qlen in general requires locking
5141 * the socket, and this code lacks that.
5143 if ((stcb == NULL) &&
5144 (!SCTP_IS_LISTENING(inp) ||
5145 (!(inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
5146 inp->sctp_socket->sol_qlen >= inp->sctp_socket->sol_qlimit))) {
5147 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5148 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5149 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
5150 sctp_abort_association(inp, stcb, m, iphlen,
5151 src, dst, sh, op_err,
5158 struct mbuf *ret_buf;
5159 struct sctp_inpcb *linp;
5168 SCTP_ASOC_CREATE_LOCK(linp);
5169 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5170 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5171 SCTP_ASOC_CREATE_UNLOCK(linp);
5177 struct sctp_tcb *locked_stcb;
5181 sctp_handle_cookie_echo(m, iphlen,
5185 (struct sctp_cookie_echo_chunk *)ch,
5195 if ((locked_stcb != NULL) && (locked_stcb != stcb)) {
5196 SCTP_TCB_UNLOCK(locked_stcb);
5199 SCTP_TCB_LOCK_ASSERT(stcb);
5205 SCTP_ASOC_CREATE_UNLOCK(linp);
5207 if (ret_buf == NULL) {
5209 SCTP_TCB_UNLOCK(stcb);
5211 SCTPDBG(SCTP_DEBUG_INPUT3,
5212 "GAK, null buffer\n");
5216 /* if AUTH skipped, see if it verified... */
5221 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5223 * Restart the timer if we have
5226 struct sctp_tmit_chunk *chk;
5228 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5229 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5233 case SCTP_COOKIE_ACK:
5234 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE_ACK, stcb %p\n", (void *)stcb);
5235 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5238 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5239 /* We are not interested anymore */
5240 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5243 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5244 so = SCTP_INP_SO(inp);
5245 atomic_add_int(&stcb->asoc.refcnt, 1);
5246 SCTP_TCB_UNLOCK(stcb);
5247 SCTP_SOCKET_LOCK(so, 1);
5248 SCTP_TCB_LOCK(stcb);
5249 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5251 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5252 SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5253 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5254 SCTP_SOCKET_UNLOCK(so, 1);
5260 if ((netp != NULL) && (*netp != NULL)) {
5261 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5265 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_ECHO\n");
5266 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5271 if (stcb->asoc.ecn_supported == 0) {
5274 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, stcb);
5278 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_CWR\n");
5279 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5283 if (stcb->asoc.ecn_supported == 0) {
5286 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5288 case SCTP_SHUTDOWN_COMPLETE:
5289 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_COMPLETE, stcb %p\n", (void *)stcb);
5290 /* must be first and only chunk */
5291 if ((num_chunks > 1) ||
5292 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5296 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5297 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5304 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5306 if (stcb->asoc.asconf_supported == 0) {
5309 sctp_handle_asconf(m, *offset, src,
5310 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5314 case SCTP_ASCONF_ACK:
5315 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF_ACK\n");
5316 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5321 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5322 if (stcb->asoc.asconf_supported == 0) {
5325 /* He's alive so give him credit */
5326 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5327 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5328 stcb->asoc.overall_error_count,
5330 SCTP_FROM_SCTP_INPUT,
5333 stcb->asoc.overall_error_count = 0;
5334 sctp_handle_asconf_ack(m, *offset,
5335 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5336 if (abort_no_unlock)
5340 case SCTP_FORWARD_CUM_TSN:
5341 case SCTP_IFORWARD_CUM_TSN:
5342 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD_TSN\n");
5343 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5352 if (stcb->asoc.prsctp_supported == 0) {
5356 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5357 /* We are not interested anymore */
5358 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5359 so = SCTP_INP_SO(inp);
5360 atomic_add_int(&stcb->asoc.refcnt, 1);
5361 SCTP_TCB_UNLOCK(stcb);
5362 SCTP_SOCKET_LOCK(so, 1);
5363 SCTP_TCB_LOCK(stcb);
5364 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5366 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5367 SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5368 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5369 SCTP_SOCKET_UNLOCK(so, 1);
5375 * For sending a SACK this looks like DATA
5378 stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from;
5379 sctp_handle_forward_tsn(stcb,
5380 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5387 case SCTP_STREAM_RESET:
5388 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5389 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5394 if (stcb->asoc.reconfig_supported == 0) {
5397 if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5398 /* stop processing */
5403 case SCTP_PACKET_DROPPED:
5404 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5405 /* re-get it all please */
5406 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5412 if ((ch != NULL) && (stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5413 if (stcb->asoc.pktdrop_supported == 0) {
5416 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5418 min(chk_length, contiguous));
5421 case SCTP_AUTHENTICATION:
5422 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5424 /* save the first AUTH for later processing */
5425 if (auth_skipped == 0) {
5426 auth_offset = *offset;
5427 auth_len = chk_length;
5430 /* skip this chunk (temporarily) */
5433 if (stcb->asoc.auth_supported == 0) {
5436 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5437 (chk_length > (sizeof(struct sctp_auth_chunk) +
5438 SCTP_AUTH_DIGEST_LEN_MAX))) {
5443 if (got_auth == 1) {
5444 /* skip this chunk... it's already auth'd */
5448 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5450 /* auth HMAC failed so dump the packet */
5454 /* remaining chunks are HMAC checked */
5455 stcb->asoc.authenticated = 1;
5461 /* it's an unknown chunk! */
5462 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5463 struct sctp_gen_error_cause *cause;
5466 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5467 0, M_NOWAIT, 1, MT_DATA);
5468 if (op_err != NULL) {
5469 len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset));
5470 cause = mtod(op_err, struct sctp_gen_error_cause *);
5471 cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5472 cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause)));
5473 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
5474 SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
5475 if (SCTP_BUF_NEXT(op_err) != NULL) {
5476 #ifdef SCTP_MBUF_LOGGING
5477 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5478 sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5481 sctp_queue_op_err(stcb, op_err);
5483 sctp_m_freem(op_err);
5487 if ((ch->chunk_type & 0x80) == 0) {
5488 /* discard this packet */
5491 } /* else skip this bad chunk and continue... */
5493 } /* switch (ch->chunk_type) */
5497 /* get the next chunk */
5498 *offset += SCTP_SIZE32(chk_length);
5499 if (*offset >= length) {
5500 /* no more data left in the mbuf chain */
5503 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5504 sizeof(struct sctp_chunkhdr), chunk_buf);
5511 if ((asconf_cnt > 0) && (stcb != NULL)) {
5512 sctp_send_asconf_ack(stcb);
5519 * common input chunk processing (v4 and v6)
5522 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5523 struct sockaddr *src, struct sockaddr *dst,
5524 struct sctphdr *sh, struct sctp_chunkhdr *ch,
5525 uint8_t compute_crc,
5527 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5528 uint32_t vrf_id, uint16_t port)
5531 int fwd_tsn_seen = 0, data_processed = 0;
5532 struct mbuf *m = *mm, *op_err;
5533 char msg[SCTP_DIAG_INFO_LEN];
5535 int cnt_ctrl_ready = 0;
5536 struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5537 struct sctp_tcb *stcb = NULL;
5538 struct sctp_nets *net = NULL;
5540 SCTP_STAT_INCR(sctps_recvdatagrams);
5541 #ifdef SCTP_AUDITING_ENABLED
5542 sctp_audit_log(0xE0, 1);
5543 sctp_auditing(0, inp, stcb, net);
5545 if (compute_crc != 0) {
5546 uint32_t check, calc_check;
5548 check = sh->checksum;
5550 calc_check = sctp_calculate_cksum(m, iphlen);
5551 sh->checksum = check;
5552 if (calc_check != check) {
5553 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5554 calc_check, check, (void *)m, length, iphlen);
5555 stcb = sctp_findassociation_addr(m, offset, src, dst,
5556 sh, ch, &inp, &net, vrf_id);
5557 #if defined(INET) || defined(INET6)
5558 if ((ch->chunk_type != SCTP_INITIATION) &&
5559 (net != NULL) && (net->port != port)) {
5560 if (net->port == 0) {
5561 /* UDP encapsulation turned on. */
5562 net->mtu -= sizeof(struct udphdr);
5563 if (stcb->asoc.smallest_mtu > net->mtu) {
5564 sctp_pathmtu_adjustment(stcb, net->mtu);
5566 } else if (port == 0) {
5567 /* UDP encapsulation turned off. */
5568 net->mtu += sizeof(struct udphdr);
5569 /* XXX Update smallest_mtu */
5575 net->flowtype = mflowtype;
5576 net->flowid = mflowid;
5578 if ((inp != NULL) && (stcb != NULL)) {
5579 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5580 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5581 } else if ((inp != NULL) && (stcb == NULL)) {
5584 SCTP_STAT_INCR(sctps_badsum);
5585 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5589 /* Destination port of 0 is illegal, based on RFC4960. */
5590 if (sh->dest_port == 0) {
5591 SCTP_STAT_INCR(sctps_hdrops);
5594 stcb = sctp_findassociation_addr(m, offset, src, dst,
5595 sh, ch, &inp, &net, vrf_id);
5596 #if defined(INET) || defined(INET6)
5597 if ((ch->chunk_type != SCTP_INITIATION) &&
5598 (net != NULL) && (net->port != port)) {
5599 if (net->port == 0) {
5600 /* UDP encapsulation turned on. */
5601 net->mtu -= sizeof(struct udphdr);
5602 if (stcb->asoc.smallest_mtu > net->mtu) {
5603 sctp_pathmtu_adjustment(stcb, net->mtu);
5605 } else if (port == 0) {
5606 /* UDP encapsulation turned off. */
5607 net->mtu += sizeof(struct udphdr);
5608 /* XXX Update smallest_mtu */
5614 net->flowtype = mflowtype;
5615 net->flowid = mflowid;
5618 SCTP_STAT_INCR(sctps_noport);
5619 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5622 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5623 sctp_send_shutdown_complete2(src, dst, sh,
5624 mflowtype, mflowid, fibnum,
5628 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5631 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5632 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5633 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5634 (ch->chunk_type != SCTP_INIT))) {
5635 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5637 sctp_send_abort(m, iphlen, src, dst,
5639 mflowtype, mflowid, fibnum,
5644 } else if (stcb == NULL) {
5647 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5648 (void *)m, iphlen, offset, length, (void *)stcb);
5650 /* always clear this before beginning a packet */
5651 stcb->asoc.authenticated = 0;
5652 stcb->asoc.seen_a_sack_this_pkt = 0;
5653 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5654 (void *)stcb, stcb->asoc.state);
5656 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5657 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5659 * If we hit here, we had a ref count
5660 * up when the assoc was aborted and the
5661 * timer is clearing out the assoc, we should
5662 * NOT respond to any packet.. its OOTB.
5664 SCTP_TCB_UNLOCK(stcb);
5666 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5667 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5669 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5670 mflowtype, mflowid, inp->fibnum,
5676 if (IS_SCTP_CONTROL(ch)) {
5677 /* process the control portion of the SCTP packet */
5678 /* sa_ignore NO_NULL_CHK */
5679 stcb = sctp_process_control(m, iphlen, &offset, length,
5681 inp, stcb, &net, &fwd_tsn_seen,
5682 mflowtype, mflowid, fibnum,
5686 * This covers us if the cookie-echo was there and
5687 * it changes our INP.
5689 inp = stcb->sctp_ep;
5690 #if defined(INET) || defined(INET6)
5691 if ((ch->chunk_type != SCTP_INITIATION) &&
5692 (net != NULL) && (net->port != port)) {
5693 if (net->port == 0) {
5694 /* UDP encapsulation turned on. */
5695 net->mtu -= sizeof(struct udphdr);
5696 if (stcb->asoc.smallest_mtu > net->mtu) {
5697 sctp_pathmtu_adjustment(stcb, net->mtu);
5699 } else if (port == 0) {
5700 /* UDP encapsulation turned off. */
5701 net->mtu += sizeof(struct udphdr);
5702 /* XXX Update smallest_mtu */
5710 * no control chunks, so pre-process DATA chunks (these
5711 * checks are taken care of by control processing)
5715 * if DATA only packet, and auth is required, then punt...
5716 * can't have authenticated without any AUTH (control)
5719 if ((stcb != NULL) &&
5720 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5721 /* "silently" ignore */
5722 SCTP_STAT_INCR(sctps_recvauthmissing);
5726 /* out of the blue DATA chunk */
5727 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5728 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5730 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5731 mflowtype, mflowid, fibnum,
5735 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5736 /* v_tag mismatch! */
5737 SCTP_STAT_INCR(sctps_badvtag);
5744 * no valid TCB for this packet, or we found it's a bad
5745 * packet while processing control, or we're done with this
5746 * packet (done or skip rest of data), so we drop it...
5752 * DATA chunk processing
5754 /* plow through the data chunks while length > offset */
5757 * Rest should be DATA only. Check authentication state if AUTH for
5760 if ((length > offset) &&
5762 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5763 !stcb->asoc.authenticated) {
5764 /* "silently" ignore */
5765 SCTP_STAT_INCR(sctps_recvauthmissing);
5766 SCTPDBG(SCTP_DEBUG_AUTH1,
5767 "Data chunk requires AUTH, skipped\n");
5770 if (length > offset) {
5774 * First check to make sure our state is correct. We would
5775 * not get here unless we really did have a tag, so we don't
5776 * abort if this happens, just dump the chunk silently.
5778 switch (SCTP_GET_STATE(&stcb->asoc)) {
5779 case SCTP_STATE_COOKIE_ECHOED:
5781 * we consider data with valid tags in this state
5782 * shows us the cookie-ack was lost. Imply it was
5785 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5787 case SCTP_STATE_COOKIE_WAIT:
5789 * We consider OOTB any data sent during asoc setup.
5791 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5792 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5794 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5795 mflowtype, mflowid, inp->fibnum,
5798 /* sa_ignore NOTREACHED */
5800 case SCTP_STATE_EMPTY: /* should not happen */
5801 case SCTP_STATE_INUSE: /* should not happen */
5802 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5803 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5806 /* sa_ignore NOTREACHED */
5808 case SCTP_STATE_OPEN:
5809 case SCTP_STATE_SHUTDOWN_SENT:
5812 /* plow through the data chunks while length > offset */
5813 retval = sctp_process_data(mm, iphlen, &offset, length,
5814 inp, stcb, net, &high_tsn);
5817 * The association aborted, NO UNLOCK needed since
5818 * the association is destroyed.
5825 * Anything important needs to have been m_copy'ed in
5830 /* take care of ecn */
5831 if ((data_processed == 1) &&
5832 (stcb->asoc.ecn_supported == 1) &&
5833 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5834 /* Yep, we need to add a ECNE */
5835 sctp_send_ecn_echo(stcb, net, high_tsn);
5838 if ((data_processed == 0) && (fwd_tsn_seen)) {
5840 uint32_t highest_tsn;
5842 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5843 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5845 highest_tsn = stcb->asoc.highest_tsn_inside_map;
5847 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
5848 stcb->asoc.send_sack = 1;
5849 sctp_sack_check(stcb, was_a_gap);
5850 } else if (fwd_tsn_seen) {
5851 stcb->asoc.send_sack = 1;
5853 /* trigger send of any chunks in queue... */
5855 #ifdef SCTP_AUDITING_ENABLED
5856 sctp_audit_log(0xE0, 2);
5857 sctp_auditing(1, inp, stcb, net);
5859 SCTPDBG(SCTP_DEBUG_INPUT1,
5860 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5861 stcb->asoc.peers_rwnd,
5862 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5863 stcb->asoc.total_flight);
5864 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5865 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5866 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5868 if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
5870 stcb->asoc.trigger_reset ||
5872 (stcb->asoc.peers_rwnd > 0 ||
5873 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5874 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5875 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5876 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5878 #ifdef SCTP_AUDITING_ENABLED
5879 sctp_audit_log(0xE0, 3);
5880 sctp_auditing(2, inp, stcb, net);
5884 SCTP_TCB_UNLOCK(stcb);
5886 if (inp_decr != NULL) {
5887 /* reduce ref-count */
5888 SCTP_INP_WLOCK(inp_decr);
5889 SCTP_INP_DECR_REF(inp_decr);
5890 SCTP_INP_WUNLOCK(inp_decr);
5897 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5901 uint32_t vrf_id = 0;
5903 struct sockaddr_in src, dst;
5906 struct sctp_chunkhdr *ch;
5908 uint8_t compute_crc;
5914 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
5915 SCTP_RELEASE_PKT(i_pak);
5918 m = SCTP_HEADER_TO_CHAIN(i_pak);
5919 #ifdef SCTP_MBUF_LOGGING
5920 /* Log in any input mbufs */
5921 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5922 sctp_log_mbc(m, SCTP_MBUF_INPUT);
5925 #ifdef SCTP_PACKET_LOGGING
5926 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
5930 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
5931 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
5933 if_name(m->m_pkthdr.rcvif),
5934 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
5935 mflowid = m->m_pkthdr.flowid;
5936 mflowtype = M_HASHTYPE_GET(m);
5937 fibnum = M_GETFIB(m);
5938 SCTP_STAT_INCR(sctps_recvpackets);
5939 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
5940 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
5941 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5942 if (SCTP_BUF_LEN(m) < offset) {
5943 if ((m = m_pullup(m, offset)) == NULL) {
5944 SCTP_STAT_INCR(sctps_hdrops);
5948 ip = mtod(m, struct ip *);
5949 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
5950 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
5951 offset -= sizeof(struct sctp_chunkhdr);
5952 memset(&src, 0, sizeof(struct sockaddr_in));
5953 src.sin_family = AF_INET;
5954 src.sin_len = sizeof(struct sockaddr_in);
5955 src.sin_port = sh->src_port;
5956 src.sin_addr = ip->ip_src;
5957 memset(&dst, 0, sizeof(struct sockaddr_in));
5958 dst.sin_family = AF_INET;
5959 dst.sin_len = sizeof(struct sockaddr_in);
5960 dst.sin_port = sh->dest_port;
5961 dst.sin_addr = ip->ip_dst;
5962 length = ntohs(ip->ip_len);
5963 /* Validate mbuf chain length with IP payload length. */
5964 if (SCTP_HEADER_LEN(m) != length) {
5965 SCTPDBG(SCTP_DEBUG_INPUT1,
5966 "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
5967 SCTP_STAT_INCR(sctps_hdrops);
5970 /* SCTP does not allow broadcasts or multicasts */
5971 if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
5974 if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
5977 ecn_bits = ip->ip_tos;
5978 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5979 SCTP_STAT_INCR(sctps_recvhwcrc);
5982 SCTP_STAT_INCR(sctps_recvswcrc);
5985 sctp_common_input_processing(&m, iphlen, offset, length,
5986 (struct sockaddr *)&src,
5987 (struct sockaddr *)&dst,
5991 mflowtype, mflowid, fibnum,
6000 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6001 extern int *sctp_cpuarry;
6005 sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
6012 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6018 uint32_t flowid, tag;
6020 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
6021 flowid = m->m_pkthdr.flowid;
6024 * No flow id built by lower layers fix it so we
6027 offset = off + sizeof(struct sctphdr);
6028 if (SCTP_BUF_LEN(m) < offset) {
6029 if ((m = m_pullup(m, offset)) == NULL) {
6030 SCTP_STAT_INCR(sctps_hdrops);
6031 return (IPPROTO_DONE);
6034 ip = mtod(m, struct ip *);
6035 sh = (struct sctphdr *)((caddr_t)ip + off);
6036 tag = htonl(sh->v_tag);
6037 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6038 m->m_pkthdr.flowid = flowid;
6039 M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH);
6041 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6042 sctp_queue_to_mcore(m, off, cpu_to_use);
6043 return (IPPROTO_DONE);
6046 sctp_input_with_port(m, off, 0);
6047 return (IPPROTO_DONE);