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>
55 #include <netinet/in_kdtrace.h>
61 sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
63 struct sctp_nets *net;
66 * This now not only stops all cookie timers it also stops any INIT
67 * timers as well. This will make sure that the timers are stopped
68 * in all collision cases.
70 SCTP_TCB_LOCK_ASSERT(stcb);
71 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
72 if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
73 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
76 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
77 } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
78 sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
81 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
88 sctp_handle_init(struct mbuf *m, int iphlen, int offset,
89 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
90 struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
91 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_no_unlock,
92 uint8_t mflowtype, uint32_t mflowid,
93 uint32_t vrf_id, uint16_t port)
95 struct sctp_init *init;
98 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
103 /* validate length */
104 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
105 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
106 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
110 *abort_no_unlock = 1;
113 /* validate parameters */
115 if (init->initiate_tag == 0) {
116 /* protocol error... send abort */
117 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
118 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
122 *abort_no_unlock = 1;
125 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
126 /* invalid parameter... send abort */
127 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
128 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
132 *abort_no_unlock = 1;
135 if (init->num_inbound_streams == 0) {
136 /* protocol error... send abort */
137 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
138 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
142 *abort_no_unlock = 1;
145 if (init->num_outbound_streams == 0) {
146 /* protocol error... send abort */
147 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
148 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
152 *abort_no_unlock = 1;
155 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
156 offset + ntohs(cp->ch.chunk_length))) {
157 /* auth parameter(s) error... send abort */
158 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
159 "Problem with AUTH parameters");
160 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
164 *abort_no_unlock = 1;
167 /* We are only accepting if we have a listening socket. */
168 if ((stcb == NULL) &&
169 ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
170 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
171 (!SCTP_IS_LISTENING(inp)))) {
173 * FIX ME ?? What about TCP model and we have a
174 * match/restart case? Actually no fix is needed. the lookup
175 * will always find the existing assoc so stcb would not be
176 * NULL. It may be questionable to do this since we COULD
177 * just send back the INIT-ACK and hope that the app did
178 * accept()'s by the time the COOKIE was sent. But there is
179 * a price to pay for COOKIE generation and I don't want to
180 * pay it on the chance that the app will actually do some
181 * accepts(). The App just looses and should NOT be in this
184 if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
185 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
187 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
188 mflowtype, mflowid, inp->fibnum,
193 if ((stcb != NULL) &&
194 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
195 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
196 sctp_send_shutdown_ack(stcb, NULL);
197 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
199 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
200 sctp_send_initiate_ack(inp, stcb, net, m, iphlen, offset,
207 SCTP_INP_RUNLOCK(inp);
212 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
216 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
217 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
224 struct sctp_stream_queue_pending *sp;
225 struct sctp_association *asoc;
228 * This function returns if any stream has true unsent data on it.
229 * Note that as it looks through it will clean up any places that
230 * have old data that has been sent but left at top of stream queue.
234 SCTP_TCB_SEND_LOCK(stcb);
235 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
236 /* Check to see if some data queued */
237 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
238 /* sa_ignore FREED_MEMORY */
239 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
243 if ((sp->msg_is_complete) &&
245 (sp->sender_all_done)) {
247 * We are doing differed cleanup. Last time
248 * through when we took all the data the
249 * sender_all_done was not set.
251 if (sp->put_last_out == 0) {
252 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
253 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
259 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
260 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
261 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, &asoc->strmout[i], sp, 1);
263 sctp_free_remote_addr(sp->net);
267 sctp_m_freem(sp->data);
270 sctp_free_a_strmoq(stcb, sp, so_locked);
271 if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
277 if (unsent_data > 0) {
282 SCTP_TCB_SEND_UNLOCK(stcb);
283 return (unsent_data);
287 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
289 struct sctp_init *init;
290 struct sctp_association *asoc;
291 struct sctp_nets *lnet;
296 /* save off parameters */
297 asoc->peer_vtag = ntohl(init->initiate_tag);
298 asoc->peers_rwnd = ntohl(init->a_rwnd);
300 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
302 if (!TAILQ_EMPTY(&asoc->nets)) {
303 /* update any ssthresh's that may have a default */
304 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
305 lnet->ssthresh = asoc->peers_rwnd;
306 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
307 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
312 SCTP_TCB_SEND_LOCK(stcb);
313 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
315 struct sctp_stream_out *outs;
316 struct sctp_stream_queue_pending *sp, *nsp;
317 struct sctp_tmit_chunk *chk, *nchk;
319 /* abandon the upper streams */
320 newcnt = ntohs(init->num_inbound_streams);
321 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
322 if (chk->rec.data.sid >= newcnt) {
323 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
324 asoc->send_queue_cnt--;
325 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
326 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
329 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
332 if (chk->data != NULL) {
333 sctp_free_bufspace(stcb, asoc, chk, 1);
334 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
335 0, chk, SCTP_SO_NOT_LOCKED);
337 sctp_m_freem(chk->data);
341 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
342 /* sa_ignore FREED_MEMORY */
346 for (i = newcnt; i < asoc->pre_open_streams; i++) {
347 outs = &asoc->strmout[i];
348 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
349 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
350 TAILQ_REMOVE(&outs->outqueue, sp, next);
351 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
352 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
353 stcb, 0, sp, SCTP_SO_NOT_LOCKED);
355 sctp_m_freem(sp->data);
359 sctp_free_remote_addr(sp->net);
363 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
364 /* sa_ignore FREED_MEMORY */
366 outs->state = SCTP_STREAM_CLOSED;
369 /* cut back the count */
370 asoc->pre_open_streams = newcnt;
372 SCTP_TCB_SEND_UNLOCK(stcb);
373 asoc->streamoutcnt = asoc->pre_open_streams;
375 for (i = 0; i < asoc->streamoutcnt; i++) {
376 asoc->strmout[i].state = SCTP_STREAM_OPEN;
379 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
380 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
381 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
382 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
384 /* This is the next one we expect */
385 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
387 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
388 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
390 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
391 /* open the requested streams */
393 if (asoc->strmin != NULL) {
394 /* Free the old ones */
395 for (i = 0; i < asoc->streamincnt; i++) {
396 sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
397 sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
399 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
401 if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
402 asoc->streamincnt = ntohs(init->num_outbound_streams);
404 asoc->streamincnt = asoc->max_inbound_streams;
406 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
407 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
408 if (asoc->strmin == NULL) {
409 /* we didn't get memory for the streams! */
410 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
413 for (i = 0; i < asoc->streamincnt; i++) {
414 asoc->strmin[i].sid = i;
415 asoc->strmin[i].last_mid_delivered = 0xffffffff;
416 TAILQ_INIT(&asoc->strmin[i].inqueue);
417 TAILQ_INIT(&asoc->strmin[i].uno_inqueue);
418 asoc->strmin[i].pd_api_started = 0;
419 asoc->strmin[i].delivery_started = 0;
422 * load_address_from_init will put the addresses into the
423 * association when the COOKIE is processed or the INIT-ACK is
424 * processed. Both types of COOKIE's existing and new call this
425 * routine. It will remove addresses that are no longer in the
426 * association (for the restarting case where addresses are
427 * removed). Up front when the INIT arrives we will discard it if it
428 * is a restart and new addresses have been added.
430 /* sa_ignore MEMLEAK */
435 * INIT-ACK message processing/consumption returns value < 0 on error
438 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
439 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
440 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
441 struct sctp_nets *net, int *abort_no_unlock,
442 uint8_t mflowtype, uint32_t mflowid,
445 struct sctp_association *asoc;
447 int retval, abort_flag, cookie_found;
449 int nat_friendly = 0;
451 /* First verify that we have no illegal param's */
455 op_err = sctp_arethere_unrecognized_parameters(m,
456 (offset + sizeof(struct sctp_init_chunk)),
457 &abort_flag, (struct sctp_chunkhdr *)cp,
458 &nat_friendly, &cookie_found);
460 /* Send an abort and notify peer */
461 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
462 *abort_no_unlock = 1;
468 /* Only report the missing cookie parameter */
469 if (op_err != NULL) {
470 sctp_m_freem(op_err);
472 len = (uint16_t)(sizeof(struct sctp_error_missing_param) + sizeof(uint16_t));
473 /* We abort with an error of missing mandatory param */
474 op_err = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
475 if (op_err != NULL) {
476 struct sctp_error_missing_param *cause;
478 SCTP_BUF_LEN(op_err) = len;
479 cause = mtod(op_err, struct sctp_error_missing_param *);
480 /* Subtract the reserved param */
481 cause->cause.code = htons(SCTP_CAUSE_MISSING_PARAM);
482 cause->cause.length = htons(len);
483 cause->num_missing_params = htonl(1);
484 cause->type[0] = htons(SCTP_STATE_COOKIE);
486 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
487 src, dst, sh, op_err,
490 *abort_no_unlock = 1;
494 asoc->peer_supports_nat = (uint8_t)nat_friendly;
495 /* process the peer's parameters in the INIT-ACK */
496 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
500 initack_limit = offset + ntohs(cp->ch.chunk_length);
501 /* load all addresses */
502 if ((retval = sctp_load_addresses_from_init(stcb, m,
503 (offset + sizeof(struct sctp_init_chunk)), initack_limit,
504 src, dst, NULL, stcb->asoc.port))) {
505 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
506 "Problem with address parameters");
507 SCTPDBG(SCTP_DEBUG_INPUT1,
508 "Load addresses from INIT causes an abort %d\n",
510 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
511 src, dst, sh, op_err,
514 *abort_no_unlock = 1;
517 /* if the peer doesn't support asconf, flush the asconf queue */
518 if (asoc->asconf_supported == 0) {
519 struct sctp_asconf_addr *param, *nparam;
521 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
522 TAILQ_REMOVE(&asoc->asconf_queue, param, next);
523 SCTP_FREE(param, SCTP_M_ASC_ADDR);
527 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
528 stcb->asoc.local_hmacs);
530 sctp_queue_op_err(stcb, op_err);
531 /* queuing will steal away the mbuf chain to the out queue */
534 /* extract the cookie and queue it to "echo" it back... */
535 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
536 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
537 stcb->asoc.overall_error_count,
539 SCTP_FROM_SCTP_INPUT,
542 stcb->asoc.overall_error_count = 0;
543 net->error_count = 0;
546 * Cancel the INIT timer, We do this first before queueing the
547 * cookie. We always cancel at the primary to assue that we are
548 * canceling the timer started by the INIT which always goes to the
551 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
552 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
554 /* calculate the RTO */
555 sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
556 SCTP_RTT_FROM_NON_DATA);
557 retval = sctp_send_cookie_echo(m, offset, initack_limit, stcb, net);
562 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
563 struct sctp_tcb *stcb, struct sctp_nets *net)
565 union sctp_sockstore store;
566 struct sctp_nets *r_net, *f_net;
569 uint16_t old_error_counter;
571 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
576 memset(&store, 0, sizeof(store));
577 switch (cp->heartbeat.hb_info.addr_family) {
580 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
581 store.sin.sin_family = cp->heartbeat.hb_info.addr_family;
582 store.sin.sin_len = cp->heartbeat.hb_info.addr_len;
583 store.sin.sin_port = stcb->rport;
584 memcpy(&store.sin.sin_addr, cp->heartbeat.hb_info.address,
585 sizeof(store.sin.sin_addr));
593 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
594 store.sin6.sin6_family = cp->heartbeat.hb_info.addr_family;
595 store.sin6.sin6_len = cp->heartbeat.hb_info.addr_len;
596 store.sin6.sin6_port = stcb->rport;
597 memcpy(&store.sin6.sin6_addr, cp->heartbeat.hb_info.address, sizeof(struct in6_addr));
606 r_net = sctp_findnet(stcb, &store.sa);
608 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
611 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
612 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
613 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
615 * If the its a HB and it's random value is correct when can
616 * confirm the destination.
618 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
619 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
620 stcb->asoc.primary_destination = r_net;
621 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
622 f_net = TAILQ_FIRST(&stcb->asoc.nets);
623 if (f_net != r_net) {
625 * first one on the list is NOT the primary
626 * sctp_cmpaddr() is much more efficient if
627 * the primary is the first on the list,
630 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
631 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
635 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
636 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
637 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
638 r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
639 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
641 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
642 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
643 stcb->asoc.overall_error_count,
645 SCTP_FROM_SCTP_INPUT,
648 stcb->asoc.overall_error_count = 0;
649 old_error_counter = r_net->error_count;
650 r_net->error_count = 0;
651 r_net->hb_responded = 1;
652 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
653 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
654 /* Now lets do a RTO with this */
655 sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv,
656 SCTP_RTT_FROM_NON_DATA);
657 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
658 r_net->dest_state |= SCTP_ADDR_REACHABLE;
659 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
660 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
662 if (r_net->dest_state & SCTP_ADDR_PF) {
663 r_net->dest_state &= ~SCTP_ADDR_PF;
664 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
666 if (old_error_counter > 0) {
667 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
668 stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
669 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
671 if (r_net == stcb->asoc.primary_destination) {
672 if (stcb->asoc.alternate) {
673 /* release the alternate, primary is good */
674 sctp_free_remote_addr(stcb->asoc.alternate);
675 stcb->asoc.alternate = NULL;
678 /* Mobility adaptation */
680 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
681 SCTP_MOBILITY_BASE) ||
682 sctp_is_mobility_feature_on(stcb->sctp_ep,
683 SCTP_MOBILITY_FASTHANDOFF)) &&
684 sctp_is_mobility_feature_on(stcb->sctp_ep,
685 SCTP_MOBILITY_PRIM_DELETED)) {
687 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
688 stcb->sctp_ep, stcb, NULL,
689 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
690 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
691 SCTP_MOBILITY_FASTHANDOFF)) {
692 sctp_assoc_immediate_retrans(stcb,
693 stcb->asoc.primary_destination);
695 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
696 SCTP_MOBILITY_BASE)) {
697 sctp_move_chunks_from_net(stcb,
698 stcb->asoc.deleted_primary);
700 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
701 stcb->asoc.deleted_primary);
707 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
710 * Return 0 means we want you to proceed with the abort non-zero
711 * means no abort processing.
714 struct sctpasochead *head;
716 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
717 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
718 new_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
719 atomic_add_int(&stcb->asoc.refcnt, 1);
720 SCTP_TCB_UNLOCK(stcb);
721 SCTP_INP_INFO_WLOCK();
723 atomic_subtract_int(&stcb->asoc.refcnt, 1);
727 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
728 /* generate a new vtag and send init */
729 LIST_REMOVE(stcb, sctp_asocs);
730 stcb->asoc.my_vtag = new_vtag;
731 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
733 * put it in the bucket in the vtag hash of assoc's for the
736 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
737 SCTP_INP_INFO_WUNLOCK();
738 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
742 * treat like a case where the cookie expired i.e.: - dump
743 * current cookie. - generate a new vtag. - resend init.
745 /* generate a new vtag and send init */
746 LIST_REMOVE(stcb, sctp_asocs);
747 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
748 sctp_stop_all_cookie_timers(stcb);
749 sctp_toss_old_cookies(stcb, &stcb->asoc);
750 stcb->asoc.my_vtag = new_vtag;
751 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
753 * put it in the bucket in the vtag hash of assoc's for the
756 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
757 SCTP_INP_INFO_WUNLOCK();
758 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
765 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
766 struct sctp_nets *net)
769 * return 0 means we want you to proceed with the abort non-zero
770 * means no abort processing
772 if (stcb->asoc.auth_supported == 0) {
773 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
776 sctp_asconf_send_nat_state_update(stcb, net);
781 /* Returns 1 if the stcb was aborted, 0 otherwise */
783 sctp_handle_abort(struct sctp_abort_chunk *abort,
784 struct sctp_tcb *stcb, struct sctp_nets *net)
786 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
792 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
796 len = ntohs(abort->ch.chunk_length);
797 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_error_cause)) {
799 * Need to check the cause codes for our two magic nat
800 * aborts which don't kill the assoc necessarily.
802 struct sctp_error_cause *cause;
804 cause = (struct sctp_error_cause *)(abort + 1);
805 error = ntohs(cause->code);
806 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
807 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
808 abort->ch.chunk_flags);
809 if (sctp_handle_nat_colliding_state(stcb)) {
812 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
813 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
814 abort->ch.chunk_flags);
815 if (sctp_handle_nat_missing_state(stcb, net)) {
822 /* stop any receive timers */
823 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net,
824 SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
825 /* notify user of the abort and clean up... */
826 sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
828 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
829 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
830 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
831 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
833 #ifdef SCTP_ASOCLOG_OF_TSNS
834 sctp_print_out_track_log(stcb);
836 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
837 so = SCTP_INP_SO(stcb->sctp_ep);
838 atomic_add_int(&stcb->asoc.refcnt, 1);
839 SCTP_TCB_UNLOCK(stcb);
840 SCTP_SOCKET_LOCK(so, 1);
842 atomic_subtract_int(&stcb->asoc.refcnt, 1);
844 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
845 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
846 SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
847 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
848 SCTP_SOCKET_UNLOCK(so, 1);
850 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
855 sctp_start_net_timers(struct sctp_tcb *stcb)
857 uint32_t cnt_hb_sent;
858 struct sctp_nets *net;
861 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
863 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
864 * If the dest in unconfirmed send a hb as well if under
865 * max_hb_burst have been sent.
867 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
868 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
869 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
870 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
871 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
876 sctp_chunk_output(stcb->sctp_ep, stcb,
877 SCTP_OUTPUT_FROM_COOKIE_ACK,
884 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
885 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
887 struct sctp_association *asoc;
888 int some_on_streamwheel;
890 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
894 SCTPDBG(SCTP_DEBUG_INPUT2,
895 "sctp_handle_shutdown: handling SHUTDOWN\n");
899 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
900 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
903 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
904 /* Shutdown NOT the expected size */
907 old_state = SCTP_GET_STATE(stcb);
908 sctp_update_acked(stcb, cp, abort_flag);
912 if (asoc->control_pdapi) {
914 * With a normal shutdown we assume the end of last record.
916 SCTP_INP_READ_LOCK(stcb->sctp_ep);
917 if (asoc->control_pdapi->on_strm_q) {
918 struct sctp_stream_in *strm;
920 strm = &asoc->strmin[asoc->control_pdapi->sinfo_stream];
921 if (asoc->control_pdapi->on_strm_q == SCTP_ON_UNORDERED) {
923 TAILQ_REMOVE(&strm->uno_inqueue, asoc->control_pdapi, next_instrm);
924 asoc->control_pdapi->on_strm_q = 0;
925 } else if (asoc->control_pdapi->on_strm_q == SCTP_ON_ORDERED) {
927 TAILQ_REMOVE(&strm->inqueue, asoc->control_pdapi, next_instrm);
928 asoc->control_pdapi->on_strm_q = 0;
931 panic("Unknown state on ctrl:%p on_strm_q:%d",
933 asoc->control_pdapi->on_strm_q);
937 asoc->control_pdapi->end_added = 1;
938 asoc->control_pdapi->pdapi_aborted = 1;
939 asoc->control_pdapi = NULL;
940 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
941 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
942 so = SCTP_INP_SO(stcb->sctp_ep);
943 atomic_add_int(&stcb->asoc.refcnt, 1);
944 SCTP_TCB_UNLOCK(stcb);
945 SCTP_SOCKET_LOCK(so, 1);
947 atomic_subtract_int(&stcb->asoc.refcnt, 1);
948 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
949 /* assoc was freed while we were unlocked */
950 SCTP_SOCKET_UNLOCK(so, 1);
954 if (stcb->sctp_socket) {
955 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
957 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
958 SCTP_SOCKET_UNLOCK(so, 1);
961 /* goto SHUTDOWN_RECEIVED state to block new requests */
962 if (stcb->sctp_socket) {
963 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
964 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
965 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) {
966 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_RECEIVED);
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(stcb) == 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(stcb) == SCTP_STATE_OPEN) ||
998 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
999 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1001 if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
1002 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_ACK_SENT);
1003 sctp_stop_timers_for_shutdown(stcb);
1004 sctp_send_shutdown_ack(stcb, net);
1005 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
1006 stcb->sctp_ep, stcb, net);
1007 } else if (old_state == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1008 sctp_send_shutdown_ack(stcb, net);
1014 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
1015 struct sctp_tcb *stcb,
1016 struct sctp_nets *net)
1018 struct sctp_association *asoc;
1019 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1022 so = SCTP_INP_SO(stcb->sctp_ep);
1024 SCTPDBG(SCTP_DEBUG_INPUT2,
1025 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
1030 /* process according to association state */
1031 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
1032 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
1033 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
1034 sctp_send_shutdown_complete(stcb, net, 1);
1035 SCTP_TCB_UNLOCK(stcb);
1038 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
1039 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1040 /* unexpected SHUTDOWN-ACK... so ignore... */
1041 SCTP_TCB_UNLOCK(stcb);
1044 if (asoc->control_pdapi) {
1046 * With a normal shutdown we assume the end of last record.
1048 SCTP_INP_READ_LOCK(stcb->sctp_ep);
1049 asoc->control_pdapi->end_added = 1;
1050 asoc->control_pdapi->pdapi_aborted = 1;
1051 asoc->control_pdapi = NULL;
1052 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1053 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1054 atomic_add_int(&stcb->asoc.refcnt, 1);
1055 SCTP_TCB_UNLOCK(stcb);
1056 SCTP_SOCKET_LOCK(so, 1);
1057 SCTP_TCB_LOCK(stcb);
1058 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1059 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1060 /* assoc was freed while we were unlocked */
1061 SCTP_SOCKET_UNLOCK(so, 1);
1065 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1066 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1067 SCTP_SOCKET_UNLOCK(so, 1);
1071 if (!TAILQ_EMPTY(&asoc->send_queue) ||
1072 !TAILQ_EMPTY(&asoc->sent_queue) ||
1073 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
1074 panic("Queues are not empty when handling SHUTDOWN-ACK");
1077 /* stop the timer */
1078 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net,
1079 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1080 /* send SHUTDOWN-COMPLETE */
1081 sctp_send_shutdown_complete(stcb, net, 0);
1082 /* notify upper layer protocol */
1083 if (stcb->sctp_socket) {
1084 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1085 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1086 stcb->sctp_socket->so_snd.sb_cc = 0;
1088 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1090 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1091 /* free the TCB but first save off the ep */
1092 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1093 atomic_add_int(&stcb->asoc.refcnt, 1);
1094 SCTP_TCB_UNLOCK(stcb);
1095 SCTP_SOCKET_LOCK(so, 1);
1096 SCTP_TCB_LOCK(stcb);
1097 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1099 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1100 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1101 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1102 SCTP_SOCKET_UNLOCK(so, 1);
1107 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, uint8_t chunk_type,
1108 struct sctp_nets *net)
1110 switch (chunk_type) {
1111 case SCTP_ASCONF_ACK:
1113 sctp_asconf_cleanup(stcb, net);
1115 case SCTP_IFORWARD_CUM_TSN:
1116 case SCTP_FORWARD_CUM_TSN:
1117 stcb->asoc.prsctp_supported = 0;
1120 SCTPDBG(SCTP_DEBUG_INPUT2,
1121 "Peer does not support chunk type %d (0x%x).\n",
1122 chunk_type, chunk_type);
1128 * Skip past the param header and then we will find the param that caused the
1129 * problem. There are a number of param's in a ASCONF OR the prsctp param
1130 * these will turn of specific features.
1131 * XXX: Is this the right thing to do?
1134 sctp_process_unrecog_param(struct sctp_tcb *stcb, uint16_t parameter_type)
1136 switch (parameter_type) {
1138 case SCTP_PRSCTP_SUPPORTED:
1139 stcb->asoc.prsctp_supported = 0;
1141 case SCTP_SUPPORTED_CHUNK_EXT:
1143 /* draft-ietf-tsvwg-addip-sctp */
1144 case SCTP_HAS_NAT_SUPPORT:
1145 stcb->asoc.peer_supports_nat = 0;
1147 case SCTP_ADD_IP_ADDRESS:
1148 case SCTP_DEL_IP_ADDRESS:
1149 case SCTP_SET_PRIM_ADDR:
1150 stcb->asoc.asconf_supported = 0;
1152 case SCTP_SUCCESS_REPORT:
1153 case SCTP_ERROR_CAUSE_IND:
1154 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1155 SCTPDBG(SCTP_DEBUG_INPUT2,
1156 "Turning off ASCONF to this strange peer\n");
1157 stcb->asoc.asconf_supported = 0;
1160 SCTPDBG(SCTP_DEBUG_INPUT2,
1161 "Peer does not support param type %d (0x%x)??\n",
1162 parameter_type, parameter_type);
1168 sctp_handle_error(struct sctp_chunkhdr *ch,
1169 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
1171 struct sctp_error_cause *cause;
1172 struct sctp_association *asoc;
1173 uint32_t remaining_length, adjust;
1174 uint16_t code, cause_code, cause_length;
1175 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1179 /* parse through all of the errors and process */
1181 cause = (struct sctp_error_cause *)((caddr_t)ch +
1182 sizeof(struct sctp_chunkhdr));
1183 remaining_length = ntohs(ch->chunk_length);
1184 if (remaining_length > limit) {
1185 remaining_length = limit;
1187 if (remaining_length >= sizeof(struct sctp_chunkhdr)) {
1188 remaining_length -= sizeof(struct sctp_chunkhdr);
1190 remaining_length = 0;
1193 while (remaining_length >= sizeof(struct sctp_error_cause)) {
1194 /* Process an Error Cause */
1195 cause_code = ntohs(cause->code);
1196 cause_length = ntohs(cause->length);
1197 if ((cause_length > remaining_length) || (cause_length == 0)) {
1198 /* Invalid cause length, possibly due to truncation. */
1199 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in cause - bytes left: %u cause length: %u\n",
1200 remaining_length, cause_length);
1204 /* report the first error cause */
1207 switch (cause_code) {
1208 case SCTP_CAUSE_INVALID_STREAM:
1209 case SCTP_CAUSE_MISSING_PARAM:
1210 case SCTP_CAUSE_INVALID_PARAM:
1211 case SCTP_CAUSE_NO_USER_DATA:
1212 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %u back? We have a bug :/ (or do they?)\n",
1215 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1216 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags: %x\n",
1218 if (sctp_handle_nat_colliding_state(stcb)) {
1222 case SCTP_CAUSE_NAT_MISSING_STATE:
1223 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags: %x\n",
1225 if (sctp_handle_nat_missing_state(stcb, net)) {
1229 case SCTP_CAUSE_STALE_COOKIE:
1231 * We only act if we have echoed a cookie and are
1234 if ((cause_length >= sizeof(struct sctp_error_stale_cookie)) &&
1235 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
1236 struct sctp_error_stale_cookie *stale_cookie;
1238 stale_cookie = (struct sctp_error_stale_cookie *)cause;
1239 asoc->cookie_preserve_req = ntohl(stale_cookie->stale_time);
1240 /* Double it to be more robust on RTX */
1241 if (asoc->cookie_preserve_req <= UINT32_MAX / 2) {
1242 asoc->cookie_preserve_req *= 2;
1244 asoc->cookie_preserve_req = UINT32_MAX;
1246 asoc->stale_cookie_count++;
1247 if (asoc->stale_cookie_count >
1248 asoc->max_init_times) {
1249 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1250 /* now free the asoc */
1251 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1252 so = SCTP_INP_SO(stcb->sctp_ep);
1253 atomic_add_int(&stcb->asoc.refcnt, 1);
1254 SCTP_TCB_UNLOCK(stcb);
1255 SCTP_SOCKET_LOCK(so, 1);
1256 SCTP_TCB_LOCK(stcb);
1257 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1259 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1260 SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1261 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1262 SCTP_SOCKET_UNLOCK(so, 1);
1266 /* blast back to INIT state */
1267 sctp_toss_old_cookies(stcb, &stcb->asoc);
1268 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
1269 sctp_stop_all_cookie_timers(stcb);
1270 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1273 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1275 * Nothing we can do here, we don't do hostname
1276 * addresses so if the peer does not like my IPv6
1277 * (or IPv4 for that matter) it does not matter. If
1278 * they don't support that type of address, they can
1279 * NOT possibly get that packet type... i.e. with no
1280 * IPv6 you can't receive a IPv6 packet. so we can
1281 * safely ignore this one. If we ever added support
1282 * for HOSTNAME Addresses, then we would need to do
1286 case SCTP_CAUSE_UNRECOG_CHUNK:
1287 if (cause_length >= sizeof(struct sctp_error_unrecognized_chunk)) {
1288 struct sctp_error_unrecognized_chunk *unrec_chunk;
1290 unrec_chunk = (struct sctp_error_unrecognized_chunk *)cause;
1291 sctp_process_unrecog_chunk(stcb, unrec_chunk->ch.chunk_type, net);
1294 case SCTP_CAUSE_UNRECOG_PARAM:
1295 /* XXX: We only consider the first parameter */
1296 if (cause_length >= sizeof(struct sctp_error_cause) + sizeof(struct sctp_paramhdr)) {
1297 struct sctp_paramhdr *unrec_parameter;
1299 unrec_parameter = (struct sctp_paramhdr *)(cause + 1);
1300 sctp_process_unrecog_param(stcb, ntohs(unrec_parameter->param_type));
1303 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1305 * We ignore this since the timer will drive out a
1306 * new cookie anyway and there timer will drive us
1307 * to send a SHUTDOWN_COMPLETE. We can't send one
1308 * here since we don't have their tag.
1311 case SCTP_CAUSE_DELETING_LAST_ADDR:
1312 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1313 case SCTP_CAUSE_DELETING_SRC_ADDR:
1315 * We should NOT get these here, but in a
1318 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a error cause with code %u.\n",
1321 case SCTP_CAUSE_OUT_OF_RESC:
1323 * And what, pray tell do we do with the fact that
1324 * the peer is out of resources? Not really sure we
1325 * could do anything but abort. I suspect this
1326 * should have came WITH an abort instead of in a
1331 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown code 0x%x\n",
1335 adjust = SCTP_SIZE32(cause_length);
1336 if (remaining_length >= adjust) {
1337 remaining_length -= adjust;
1339 remaining_length = 0;
1341 cause = (struct sctp_error_cause *)((caddr_t)cause + adjust);
1343 sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, code, ch, SCTP_SO_NOT_LOCKED);
1348 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1349 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1350 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1351 struct sctp_nets *net, int *abort_no_unlock,
1352 uint8_t mflowtype, uint32_t mflowid,
1355 struct sctp_init_ack *init_ack;
1356 struct mbuf *op_err;
1358 SCTPDBG(SCTP_DEBUG_INPUT2,
1359 "sctp_handle_init_ack: handling INIT-ACK\n");
1362 SCTPDBG(SCTP_DEBUG_INPUT2,
1363 "sctp_handle_init_ack: TCB is null\n");
1366 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1367 /* Invalid length */
1368 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1369 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1370 src, dst, sh, op_err,
1373 *abort_no_unlock = 1;
1376 init_ack = &cp->init;
1377 /* validate parameters */
1378 if (init_ack->initiate_tag == 0) {
1379 /* protocol error... send an abort */
1380 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1381 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1382 src, dst, sh, op_err,
1385 *abort_no_unlock = 1;
1388 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1389 /* protocol error... send an abort */
1390 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1391 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1392 src, dst, sh, op_err,
1395 *abort_no_unlock = 1;
1398 if (init_ack->num_inbound_streams == 0) {
1399 /* protocol error... send an abort */
1400 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1401 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1402 src, dst, sh, op_err,
1405 *abort_no_unlock = 1;
1408 if (init_ack->num_outbound_streams == 0) {
1409 /* protocol error... send an abort */
1410 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1411 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1412 src, dst, sh, op_err,
1415 *abort_no_unlock = 1;
1418 /* process according to association state... */
1419 switch (SCTP_GET_STATE(stcb)) {
1420 case SCTP_STATE_COOKIE_WAIT:
1421 /* this is the expected state for this chunk */
1422 /* process the INIT-ACK parameters */
1423 if (stcb->asoc.primary_destination->dest_state &
1424 SCTP_ADDR_UNCONFIRMED) {
1426 * The primary is where we sent the INIT, we can
1427 * always consider it confirmed when the INIT-ACK is
1428 * returned. Do this before we load addresses
1431 stcb->asoc.primary_destination->dest_state &=
1432 ~SCTP_ADDR_UNCONFIRMED;
1433 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1434 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1436 if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1437 net, abort_no_unlock,
1440 /* error in parsing parameters */
1443 /* update our state */
1444 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1445 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_ECHOED);
1447 /* reset the RTO calc */
1448 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1449 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1450 stcb->asoc.overall_error_count,
1452 SCTP_FROM_SCTP_INPUT,
1455 stcb->asoc.overall_error_count = 0;
1456 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1458 * collapse the init timer back in case of a exponential
1461 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1464 * the send at the end of the inbound data processing will
1465 * cause the cookie to be sent
1468 case SCTP_STATE_SHUTDOWN_SENT:
1469 /* incorrect state... discard */
1471 case SCTP_STATE_COOKIE_ECHOED:
1472 /* incorrect state... discard */
1474 case SCTP_STATE_OPEN:
1475 /* incorrect state... discard */
1477 case SCTP_STATE_EMPTY:
1478 case SCTP_STATE_INUSE:
1480 /* incorrect state... discard */
1484 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1488 static struct sctp_tcb *
1489 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1490 struct sockaddr *src, struct sockaddr *dst,
1491 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1492 struct sctp_inpcb *inp, struct sctp_nets **netp,
1493 struct sockaddr *init_src, int *notification,
1494 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1495 uint8_t mflowtype, uint32_t mflowid,
1496 uint32_t vrf_id, uint16_t port);
1500 * handle a state cookie for an existing association m: input packet mbuf
1501 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1502 * "split" mbuf and the cookie signature does not exist offset: offset into
1503 * mbuf to the cookie-echo chunk
1505 static struct sctp_tcb *
1506 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1507 struct sockaddr *src, struct sockaddr *dst,
1508 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1509 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1510 struct sockaddr *init_src, int *notification,
1511 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1512 uint8_t mflowtype, uint32_t mflowid,
1513 uint32_t vrf_id, uint16_t port)
1515 struct sctp_association *asoc;
1516 struct sctp_init_chunk *init_cp, init_buf;
1517 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1518 struct sctp_nets *net;
1519 struct mbuf *op_err;
1521 int init_offset, initack_offset, i;
1525 #if defined(SCTP_DETAILED_STR_STATS)
1530 /* I know that the TCB is non-NULL from the caller */
1532 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1533 if (asoc->cookie_how[how_indx] == 0)
1536 if (how_indx < sizeof(asoc->cookie_how)) {
1537 asoc->cookie_how[how_indx] = 1;
1539 if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1540 /* SHUTDOWN came in after sending INIT-ACK */
1541 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1542 op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
1543 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1544 mflowtype, mflowid, inp->fibnum,
1546 if (how_indx < sizeof(asoc->cookie_how))
1547 asoc->cookie_how[how_indx] = 2;
1551 * find and validate the INIT chunk in the cookie (peer's info) the
1552 * INIT should start after the cookie-echo header struct (chunk
1553 * header, state cookie header struct)
1555 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1557 init_cp = (struct sctp_init_chunk *)
1558 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1559 (uint8_t *)&init_buf);
1560 if (init_cp == NULL) {
1561 /* could not pull a INIT chunk in cookie */
1564 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1568 * find and validate the INIT-ACK chunk in the cookie (my info) the
1569 * INIT-ACK follows the INIT chunk
1571 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1572 initack_cp = (struct sctp_init_ack_chunk *)
1573 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1574 (uint8_t *)&initack_buf);
1575 if (initack_cp == NULL) {
1576 /* could not pull INIT-ACK chunk in cookie */
1579 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1582 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1583 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1585 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1586 * to get into the OPEN state
1588 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1590 * Opps, this means that we somehow generated two vtag's
1591 * the same. I.e. we did:
1593 * <---INIT(tag=a)------
1594 * ----INIT-ACK(tag=t)-->
1595 * ----INIT(tag=t)------> *1
1596 * <---INIT-ACK(tag=a)---
1597 * <----CE(tag=t)------------- *2
1599 * At point *1 we should be generating a different
1600 * tag t'. Which means we would throw away the CE and send
1601 * ours instead. Basically this is case C (throw away side).
1603 if (how_indx < sizeof(asoc->cookie_how))
1604 asoc->cookie_how[how_indx] = 17;
1608 switch (SCTP_GET_STATE(stcb)) {
1609 case SCTP_STATE_COOKIE_WAIT:
1610 case SCTP_STATE_COOKIE_ECHOED:
1612 * INIT was sent but got a COOKIE_ECHO with the
1613 * correct tags... just accept it...but we must
1614 * process the init so that we can make sure we have
1615 * the right seq no's.
1617 /* First we must process the INIT !! */
1618 retval = sctp_process_init(init_cp, stcb);
1620 if (how_indx < sizeof(asoc->cookie_how))
1621 asoc->cookie_how[how_indx] = 3;
1624 /* we have already processed the INIT so no problem */
1625 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp,
1627 SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1628 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp,
1630 SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1631 /* update current state */
1632 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)
1633 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1635 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1637 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1638 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1639 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1640 stcb->sctp_ep, stcb, asoc->primary_destination);
1642 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1643 sctp_stop_all_cookie_timers(stcb);
1644 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1645 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1646 (!SCTP_IS_LISTENING(inp))) {
1647 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1651 * Here is where collision would go if we
1652 * did a connect() and instead got a
1653 * init/init-ack/cookie done before the
1654 * init-ack came back..
1656 stcb->sctp_ep->sctp_flags |=
1657 SCTP_PCB_FLAGS_CONNECTED;
1658 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1659 so = SCTP_INP_SO(stcb->sctp_ep);
1660 atomic_add_int(&stcb->asoc.refcnt, 1);
1661 SCTP_TCB_UNLOCK(stcb);
1662 SCTP_SOCKET_LOCK(so, 1);
1663 SCTP_TCB_LOCK(stcb);
1664 atomic_add_int(&stcb->asoc.refcnt, -1);
1665 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1666 SCTP_SOCKET_UNLOCK(so, 1);
1670 soisconnected(stcb->sctp_socket);
1671 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1672 SCTP_SOCKET_UNLOCK(so, 1);
1675 /* notify upper layer */
1676 *notification = SCTP_NOTIFY_ASSOC_UP;
1678 * since we did not send a HB make sure we don't
1681 old.tv_sec = cookie->time_entered.tv_sec;
1682 old.tv_usec = cookie->time_entered.tv_usec;
1683 net->hb_responded = 1;
1684 sctp_calculate_rto(stcb, asoc, net, &old,
1685 SCTP_RTT_FROM_NON_DATA);
1687 if (stcb->asoc.sctp_autoclose_ticks &&
1688 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1689 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1695 * we're in the OPEN state (or beyond), so peer must
1696 * have simply lost the COOKIE-ACK
1700 sctp_stop_all_cookie_timers(stcb);
1702 * We ignore the return code here.. not sure if we should
1703 * somehow abort.. but we do have an existing asoc. This
1704 * really should not fail.
1706 if (sctp_load_addresses_from_init(stcb, m,
1707 init_offset + sizeof(struct sctp_init_chunk),
1708 initack_offset, src, dst, init_src, stcb->asoc.port)) {
1709 if (how_indx < sizeof(asoc->cookie_how))
1710 asoc->cookie_how[how_indx] = 4;
1713 /* respond with a COOKIE-ACK */
1714 sctp_toss_old_cookies(stcb, asoc);
1715 sctp_send_cookie_ack(stcb);
1716 if (how_indx < sizeof(asoc->cookie_how))
1717 asoc->cookie_how[how_indx] = 5;
1721 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1722 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1723 cookie->tie_tag_my_vtag == 0 &&
1724 cookie->tie_tag_peer_vtag == 0) {
1726 * case C in Section 5.2.4 Table 2: XMOO silently discard
1728 if (how_indx < sizeof(asoc->cookie_how))
1729 asoc->cookie_how[how_indx] = 6;
1733 * If nat support, and the below and stcb is established, send back
1734 * a ABORT(colliding state) if we are established.
1736 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) &&
1737 (asoc->peer_supports_nat) &&
1738 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1739 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1740 (asoc->peer_vtag == 0)))) {
1742 * Special case - Peer's support nat. We may have two init's
1743 * that we gave out the same tag on since one was not
1744 * established.. i.e. we get INIT from host-1 behind the nat
1745 * and we respond tag-a, we get a INIT from host-2 behind
1746 * the nat and we get tag-a again. Then we bring up host-1
1747 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1748 * Now we have colliding state. We must send an abort here
1749 * with colliding state indication.
1751 op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
1752 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1753 mflowtype, mflowid, inp->fibnum,
1757 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1758 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1759 (asoc->peer_vtag == 0))) {
1761 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1762 * should be ok, re-accept peer info
1764 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1766 * Extension of case C. If we hit this, then the
1767 * random number generator returned the same vtag
1768 * when we first sent our INIT-ACK and when we later
1769 * sent our INIT. The side with the seq numbers that
1770 * are different will be the one that normnally
1771 * would have hit case C. This in effect "extends"
1772 * our vtags in this collision case to be 64 bits.
1773 * The same collision could occur aka you get both
1774 * vtag and seq number the same twice in a row.. but
1775 * is much less likely. If it did happen then we
1776 * would proceed through and bring up the assoc.. we
1777 * may end up with the wrong stream setup however..
1778 * which would be bad.. but there is no way to
1779 * tell.. until we send on a stream that does not
1782 if (how_indx < sizeof(asoc->cookie_how))
1783 asoc->cookie_how[how_indx] = 7;
1787 if (how_indx < sizeof(asoc->cookie_how))
1788 asoc->cookie_how[how_indx] = 8;
1789 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1790 SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1791 sctp_stop_all_cookie_timers(stcb);
1793 * since we did not send a HB make sure we don't double
1796 net->hb_responded = 1;
1797 if (stcb->asoc.sctp_autoclose_ticks &&
1798 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1799 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1802 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1803 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1805 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1807 * Ok the peer probably discarded our data (if we
1808 * echoed a cookie+data). So anything on the
1809 * sent_queue should be marked for retransmit, we
1810 * may not get something to kick us so it COULD
1811 * still take a timeout to move these.. but it can't
1812 * hurt to mark them.
1814 struct sctp_tmit_chunk *chk;
1816 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1817 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1818 chk->sent = SCTP_DATAGRAM_RESEND;
1819 sctp_flight_size_decrease(chk);
1820 sctp_total_flight_decrease(stcb, chk);
1821 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1827 /* process the INIT info (peer's info) */
1828 retval = sctp_process_init(init_cp, stcb);
1830 if (how_indx < sizeof(asoc->cookie_how))
1831 asoc->cookie_how[how_indx] = 9;
1834 if (sctp_load_addresses_from_init(stcb, m,
1835 init_offset + sizeof(struct sctp_init_chunk),
1836 initack_offset, src, dst, init_src, stcb->asoc.port)) {
1837 if (how_indx < sizeof(asoc->cookie_how))
1838 asoc->cookie_how[how_indx] = 10;
1841 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
1842 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
1843 *notification = SCTP_NOTIFY_ASSOC_UP;
1845 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1846 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1847 (!SCTP_IS_LISTENING(inp))) {
1848 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1851 stcb->sctp_ep->sctp_flags |=
1852 SCTP_PCB_FLAGS_CONNECTED;
1853 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1854 so = SCTP_INP_SO(stcb->sctp_ep);
1855 atomic_add_int(&stcb->asoc.refcnt, 1);
1856 SCTP_TCB_UNLOCK(stcb);
1857 SCTP_SOCKET_LOCK(so, 1);
1858 SCTP_TCB_LOCK(stcb);
1859 atomic_add_int(&stcb->asoc.refcnt, -1);
1860 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1861 SCTP_SOCKET_UNLOCK(so, 1);
1865 soisconnected(stcb->sctp_socket);
1866 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1867 SCTP_SOCKET_UNLOCK(so, 1);
1870 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)
1871 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1873 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1874 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1875 } else if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
1876 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1878 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1880 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1881 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1882 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1883 stcb->sctp_ep, stcb, asoc->primary_destination);
1885 sctp_stop_all_cookie_timers(stcb);
1886 sctp_toss_old_cookies(stcb, asoc);
1887 sctp_send_cookie_ack(stcb);
1890 * only if we have retrans set do we do this. What
1891 * this call does is get only the COOKIE-ACK out and
1892 * then when we return the normal call to
1893 * sctp_chunk_output will get the retrans out behind
1896 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1898 if (how_indx < sizeof(asoc->cookie_how))
1899 asoc->cookie_how[how_indx] = 11;
1903 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1904 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1905 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1906 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1907 cookie->tie_tag_peer_vtag != 0) {
1908 struct sctpasochead *head;
1909 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1913 if (asoc->peer_supports_nat) {
1915 * This is a gross gross hack. Just call the
1916 * cookie_new code since we are allowing a duplicate
1917 * association. I hope this works...
1919 return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
1920 sh, cookie, cookie_len,
1921 inp, netp, init_src, notification,
1922 auth_skipped, auth_offset, auth_len,
1927 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1930 if (how_indx < sizeof(asoc->cookie_how))
1931 asoc->cookie_how[how_indx] = 12;
1932 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
1933 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1934 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1935 SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
1937 /* notify upper layer */
1938 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1939 atomic_add_int(&stcb->asoc.refcnt, 1);
1940 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_OPEN) &&
1941 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1942 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) {
1943 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1945 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
1946 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1947 } else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
1948 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1950 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1951 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1952 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1953 stcb->sctp_ep, stcb, asoc->primary_destination);
1955 } else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
1956 /* move to OPEN state, if not in SHUTDOWN_SENT */
1957 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1959 asoc->pre_open_streams =
1960 ntohs(initack_cp->init.num_outbound_streams);
1961 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1962 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1963 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1965 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1967 asoc->str_reset_seq_in = asoc->init_seq_number;
1969 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1970 if (asoc->mapping_array) {
1971 memset(asoc->mapping_array, 0,
1972 asoc->mapping_array_size);
1974 if (asoc->nr_mapping_array) {
1975 memset(asoc->nr_mapping_array, 0,
1976 asoc->mapping_array_size);
1978 SCTP_TCB_UNLOCK(stcb);
1979 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1980 so = SCTP_INP_SO(stcb->sctp_ep);
1981 SCTP_SOCKET_LOCK(so, 1);
1983 SCTP_INP_INFO_WLOCK();
1984 SCTP_INP_WLOCK(stcb->sctp_ep);
1985 SCTP_TCB_LOCK(stcb);
1986 atomic_add_int(&stcb->asoc.refcnt, -1);
1987 /* send up all the data */
1988 SCTP_TCB_SEND_LOCK(stcb);
1990 sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_LOCKED);
1991 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1992 stcb->asoc.strmout[i].chunks_on_queues = 0;
1993 #if defined(SCTP_DETAILED_STR_STATS)
1994 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1995 asoc->strmout[i].abandoned_sent[j] = 0;
1996 asoc->strmout[i].abandoned_unsent[j] = 0;
1999 asoc->strmout[i].abandoned_sent[0] = 0;
2000 asoc->strmout[i].abandoned_unsent[0] = 0;
2002 stcb->asoc.strmout[i].sid = i;
2003 stcb->asoc.strmout[i].next_mid_ordered = 0;
2004 stcb->asoc.strmout[i].next_mid_unordered = 0;
2005 stcb->asoc.strmout[i].last_msg_incomplete = 0;
2007 /* process the INIT-ACK info (my info) */
2008 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2009 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2011 /* pull from vtag hash */
2012 LIST_REMOVE(stcb, sctp_asocs);
2013 /* re-insert to new vtag position */
2014 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
2015 SCTP_BASE_INFO(hashasocmark))];
2017 * put it in the bucket in the vtag hash of assoc's for the
2020 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
2022 SCTP_TCB_SEND_UNLOCK(stcb);
2023 SCTP_INP_WUNLOCK(stcb->sctp_ep);
2024 SCTP_INP_INFO_WUNLOCK();
2025 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2026 SCTP_SOCKET_UNLOCK(so, 1);
2028 asoc->total_flight = 0;
2029 asoc->total_flight_count = 0;
2030 /* process the INIT info (peer's info) */
2031 retval = sctp_process_init(init_cp, stcb);
2033 if (how_indx < sizeof(asoc->cookie_how))
2034 asoc->cookie_how[how_indx] = 13;
2039 * since we did not send a HB make sure we don't double
2042 net->hb_responded = 1;
2044 if (sctp_load_addresses_from_init(stcb, m,
2045 init_offset + sizeof(struct sctp_init_chunk),
2046 initack_offset, src, dst, init_src, stcb->asoc.port)) {
2047 if (how_indx < sizeof(asoc->cookie_how))
2048 asoc->cookie_how[how_indx] = 14;
2052 /* respond with a COOKIE-ACK */
2053 sctp_stop_all_cookie_timers(stcb);
2054 sctp_toss_old_cookies(stcb, asoc);
2055 sctp_send_cookie_ack(stcb);
2056 if (how_indx < sizeof(asoc->cookie_how))
2057 asoc->cookie_how[how_indx] = 15;
2061 if (how_indx < sizeof(asoc->cookie_how))
2062 asoc->cookie_how[how_indx] = 16;
2063 /* all other cases... */
2069 * handle a state cookie for a new association m: input packet mbuf chain--
2070 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
2071 * and the cookie signature does not exist offset: offset into mbuf to the
2072 * cookie-echo chunk length: length of the cookie chunk to: where the init
2073 * was from returns a new TCB
2075 static struct sctp_tcb *
2076 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
2077 struct sockaddr *src, struct sockaddr *dst,
2078 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
2079 struct sctp_inpcb *inp, struct sctp_nets **netp,
2080 struct sockaddr *init_src, int *notification,
2081 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2082 uint8_t mflowtype, uint32_t mflowid,
2083 uint32_t vrf_id, uint16_t port)
2085 struct sctp_tcb *stcb;
2086 struct sctp_init_chunk *init_cp, init_buf;
2087 struct sctp_init_ack_chunk *initack_cp, initack_buf;
2088 union sctp_sockstore store;
2089 struct sctp_association *asoc;
2090 int init_offset, initack_offset, initack_limit;
2093 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
2094 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2097 so = SCTP_INP_SO(inp);
2101 * find and validate the INIT chunk in the cookie (peer's info) the
2102 * INIT should start after the cookie-echo header struct (chunk
2103 * header, state cookie header struct)
2105 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2106 init_cp = (struct sctp_init_chunk *)
2107 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2108 (uint8_t *)&init_buf);
2109 if (init_cp == NULL) {
2110 /* could not pull a INIT chunk in cookie */
2111 SCTPDBG(SCTP_DEBUG_INPUT1,
2112 "process_cookie_new: could not pull INIT chunk hdr\n");
2115 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2116 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2119 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2121 * find and validate the INIT-ACK chunk in the cookie (my info) the
2122 * INIT-ACK follows the INIT chunk
2124 initack_cp = (struct sctp_init_ack_chunk *)
2125 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2126 (uint8_t *)&initack_buf);
2127 if (initack_cp == NULL) {
2128 /* could not pull INIT-ACK chunk in cookie */
2129 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2132 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2136 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2137 * "initack_limit" value. This is because the chk_length field
2138 * includes the length of the cookie, but the cookie is omitted when
2139 * the INIT and INIT_ACK are tacked onto the cookie...
2141 initack_limit = offset + cookie_len;
2144 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2149 * Here we do a trick, we set in NULL for the proc/thread argument.
2150 * We do this since in effect we only use the p argument when the
2151 * socket is unbound and we must do an implicit bind. Since we are
2152 * getting a cookie, we cannot be unbound.
2154 stcb = sctp_aloc_assoc(inp, init_src, &error,
2155 ntohl(initack_cp->init.initiate_tag), vrf_id,
2156 ntohs(initack_cp->init.num_outbound_streams),
2158 (struct thread *)NULL,
2159 SCTP_DONT_INITIALIZE_AUTH_PARAMS);
2161 struct mbuf *op_err;
2163 /* memory problem? */
2164 SCTPDBG(SCTP_DEBUG_INPUT1,
2165 "process_cookie_new: no room for another TCB!\n");
2166 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2167 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2168 src, dst, sh, op_err,
2173 /* get the correct sctp_nets */
2175 *netp = sctp_findnet(stcb, init_src);
2178 /* get scope variables out of cookie */
2179 asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2180 asoc->scope.site_scope = cookie->site_scope;
2181 asoc->scope.local_scope = cookie->local_scope;
2182 asoc->scope.loopback_scope = cookie->loopback_scope;
2184 if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2185 (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2186 struct mbuf *op_err;
2189 * Houston we have a problem. The EP changed while the
2190 * cookie was in flight. Only recourse is to abort the
2193 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2194 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2195 src, dst, sh, op_err,
2198 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2199 atomic_add_int(&stcb->asoc.refcnt, 1);
2200 SCTP_TCB_UNLOCK(stcb);
2201 SCTP_SOCKET_LOCK(so, 1);
2202 SCTP_TCB_LOCK(stcb);
2203 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2205 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2206 SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2207 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2208 SCTP_SOCKET_UNLOCK(so, 1);
2212 /* process the INIT-ACK info (my info) */
2213 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2214 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2215 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2216 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2217 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2218 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2219 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2220 asoc->str_reset_seq_in = asoc->init_seq_number;
2222 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2224 /* process the INIT info (peer's info) */
2226 retval = sctp_process_init(init_cp, stcb);
2230 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2231 atomic_add_int(&stcb->asoc.refcnt, 1);
2232 SCTP_TCB_UNLOCK(stcb);
2233 SCTP_SOCKET_LOCK(so, 1);
2234 SCTP_TCB_LOCK(stcb);
2235 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2237 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2238 SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2239 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2240 SCTP_SOCKET_UNLOCK(so, 1);
2244 /* load all addresses */
2245 if (sctp_load_addresses_from_init(stcb, m,
2246 init_offset + sizeof(struct sctp_init_chunk), initack_offset,
2247 src, dst, init_src, port)) {
2248 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2249 atomic_add_int(&stcb->asoc.refcnt, 1);
2250 SCTP_TCB_UNLOCK(stcb);
2251 SCTP_SOCKET_LOCK(so, 1);
2252 SCTP_TCB_LOCK(stcb);
2253 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2255 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2256 SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2257 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2258 SCTP_SOCKET_UNLOCK(so, 1);
2263 * verify any preceding AUTH chunk that was skipped
2265 /* pull the local authentication parameters from the cookie/init-ack */
2266 sctp_auth_get_cookie_params(stcb, m,
2267 initack_offset + sizeof(struct sctp_init_ack_chunk),
2268 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2270 struct sctp_auth_chunk *auth;
2272 auth = (struct sctp_auth_chunk *)
2273 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2274 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2275 /* auth HMAC failed, dump the assoc and packet */
2276 SCTPDBG(SCTP_DEBUG_AUTH1,
2277 "COOKIE-ECHO: AUTH failed\n");
2278 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2279 atomic_add_int(&stcb->asoc.refcnt, 1);
2280 SCTP_TCB_UNLOCK(stcb);
2281 SCTP_SOCKET_LOCK(so, 1);
2282 SCTP_TCB_LOCK(stcb);
2283 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2285 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2286 SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2287 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2288 SCTP_SOCKET_UNLOCK(so, 1);
2292 /* remaining chunks checked... good to go */
2293 stcb->asoc.authenticated = 1;
2298 * if we're doing ASCONFs, check to see if we have any new local
2299 * addresses that need to get added to the peer (eg. addresses
2300 * changed while cookie echo in flight). This needs to be done
2301 * after we go to the OPEN state to do the correct asconf
2302 * processing. else, make sure we have the correct addresses in our
2306 /* warning, we re-use sin, sin6, sa_store here! */
2307 /* pull in local_address (our "from" address) */
2308 switch (cookie->laddr_type) {
2310 case SCTP_IPV4_ADDRESS:
2311 /* source addr is IPv4 */
2312 memset(&store.sin, 0, sizeof(struct sockaddr_in));
2313 store.sin.sin_family = AF_INET;
2314 store.sin.sin_len = sizeof(struct sockaddr_in);
2315 store.sin.sin_addr.s_addr = cookie->laddress[0];
2319 case SCTP_IPV6_ADDRESS:
2320 /* source addr is IPv6 */
2321 memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
2322 store.sin6.sin6_family = AF_INET6;
2323 store.sin6.sin6_len = sizeof(struct sockaddr_in6);
2324 store.sin6.sin6_scope_id = cookie->scope_id;
2325 memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2329 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2330 atomic_add_int(&stcb->asoc.refcnt, 1);
2331 SCTP_TCB_UNLOCK(stcb);
2332 SCTP_SOCKET_LOCK(so, 1);
2333 SCTP_TCB_LOCK(stcb);
2334 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2336 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2337 SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2338 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2339 SCTP_SOCKET_UNLOCK(so, 1);
2344 /* update current state */
2345 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2346 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
2347 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2348 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2349 stcb->sctp_ep, stcb, asoc->primary_destination);
2351 sctp_stop_all_cookie_timers(stcb);
2352 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2353 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2355 /* set up to notify upper layer */
2356 *notification = SCTP_NOTIFY_ASSOC_UP;
2357 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2358 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2359 (!SCTP_IS_LISTENING(inp))) {
2361 * This is an endpoint that called connect() how it got a
2362 * cookie that is NEW is a bit of a mystery. It must be that
2363 * the INIT was sent, but before it got there.. a complete
2364 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2365 * should have went to the other code.. not here.. oh well..
2366 * a bit of protection is worth having..
2368 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2369 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2370 atomic_add_int(&stcb->asoc.refcnt, 1);
2371 SCTP_TCB_UNLOCK(stcb);
2372 SCTP_SOCKET_LOCK(so, 1);
2373 SCTP_TCB_LOCK(stcb);
2374 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2375 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2376 SCTP_SOCKET_UNLOCK(so, 1);
2380 soisconnected(stcb->sctp_socket);
2381 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2382 SCTP_SOCKET_UNLOCK(so, 1);
2384 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2385 (SCTP_IS_LISTENING(inp))) {
2387 * We don't want to do anything with this one. Since it is
2388 * the listening guy. The timer will get started for
2389 * accepted connections in the caller.
2393 /* since we did not send a HB make sure we don't double things */
2394 if ((netp) && (*netp))
2395 (*netp)->hb_responded = 1;
2397 if (stcb->asoc.sctp_autoclose_ticks &&
2398 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2399 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2401 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2402 if ((netp != NULL) && (*netp != NULL)) {
2405 /* calculate the RTT and set the encaps port */
2406 old.tv_sec = cookie->time_entered.tv_sec;
2407 old.tv_usec = cookie->time_entered.tv_usec;
2408 sctp_calculate_rto(stcb, asoc, *netp, &old, SCTP_RTT_FROM_NON_DATA);
2410 /* respond with a COOKIE-ACK */
2411 sctp_send_cookie_ack(stcb);
2414 * check the address lists for any ASCONFs that need to be sent
2415 * AFTER the cookie-ack is sent
2417 sctp_check_address_list(stcb, m,
2418 initack_offset + sizeof(struct sctp_init_ack_chunk),
2419 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2420 &store.sa, cookie->local_scope, cookie->site_scope,
2421 cookie->ipv4_scope, cookie->loopback_scope);
2428 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2429 * we NEED to make sure we are not already using the vtag. If so we
2430 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2431 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2432 SCTP_BASE_INFO(hashasocmark))];
2433 LIST_FOREACH(stcb, head, sctp_asocs) {
2434 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2435 -- SEND ABORT - TRY AGAIN --
2441 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2442 * existing (non-NULL) TCB
2444 static struct mbuf *
2445 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2446 struct sockaddr *src, struct sockaddr *dst,
2447 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2448 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2449 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2450 struct sctp_tcb **locked_tcb,
2451 uint8_t mflowtype, uint32_t mflowid,
2452 uint32_t vrf_id, uint16_t port)
2454 struct sctp_state_cookie *cookie;
2455 struct sctp_tcb *l_stcb = *stcb;
2456 struct sctp_inpcb *l_inp;
2457 struct sockaddr *to;
2458 struct sctp_pcb *ep;
2460 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2462 uint8_t cookie_ok = 0;
2463 unsigned int sig_offset, cookie_offset;
2464 unsigned int cookie_len;
2466 struct timeval time_expires;
2467 int notification = 0;
2468 struct sctp_nets *netl;
2469 int had_a_existing_tcb = 0;
2470 int send_int_conf = 0;
2472 struct sockaddr_in sin;
2475 struct sockaddr_in6 sin6;
2478 SCTPDBG(SCTP_DEBUG_INPUT2,
2479 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2481 if (inp_p == NULL) {
2484 cookie = &cp->cookie;
2485 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2486 cookie_len = ntohs(cp->ch.chunk_length);
2488 if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2489 sizeof(struct sctp_init_chunk) +
2490 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2491 /* cookie too small */
2494 if ((cookie->peerport != sh->src_port) ||
2495 (cookie->myport != sh->dest_port) ||
2496 (cookie->my_vtag != sh->v_tag)) {
2498 * invalid ports or bad tag. Note that we always leave the
2499 * v_tag in the header in network order and when we stored
2500 * it in the my_vtag slot we also left it in network order.
2501 * This maintains the match even though it may be in the
2502 * opposite byte order of the machine :->
2507 * split off the signature into its own mbuf (since it should not be
2508 * calculated in the sctp_hmac_m() call).
2510 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2511 m_sig = m_split(m, sig_offset, M_NOWAIT);
2512 if (m_sig == NULL) {
2513 /* out of memory or ?? */
2516 #ifdef SCTP_MBUF_LOGGING
2517 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2518 sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
2523 * compute the signature/digest for the cookie
2525 ep = &(*inp_p)->sctp_ep;
2528 SCTP_TCB_UNLOCK(l_stcb);
2530 SCTP_INP_RLOCK(l_inp);
2532 SCTP_TCB_LOCK(l_stcb);
2534 /* which cookie is it? */
2535 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2536 (ep->current_secret_number != ep->last_secret_number)) {
2537 /* it's the old cookie */
2538 (void)sctp_hmac_m(SCTP_HMAC,
2539 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2540 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2542 /* it's the current cookie */
2543 (void)sctp_hmac_m(SCTP_HMAC,
2544 (uint8_t *)ep->secret_key[(int)ep->current_secret_number],
2545 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2547 /* get the signature */
2548 SCTP_INP_RUNLOCK(l_inp);
2549 sig = (uint8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *)&tmp_sig);
2551 /* couldn't find signature */
2552 sctp_m_freem(m_sig);
2555 /* compare the received digest with the computed digest */
2556 if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2557 /* try the old cookie? */
2558 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2559 (ep->current_secret_number != ep->last_secret_number)) {
2560 /* compute digest with old */
2561 (void)sctp_hmac_m(SCTP_HMAC,
2562 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2563 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2565 if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2573 * Now before we continue we must reconstruct our mbuf so that
2574 * normal processing of any other chunks will work.
2580 while (SCTP_BUF_NEXT(m_at) != NULL) {
2581 m_at = SCTP_BUF_NEXT(m_at);
2583 SCTP_BUF_NEXT(m_at) = m_sig;
2586 if (cookie_ok == 0) {
2587 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2588 SCTPDBG(SCTP_DEBUG_INPUT2,
2589 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2590 (uint32_t)offset, cookie_offset, sig_offset);
2595 * check the cookie timestamps to be sure it's not stale
2597 (void)SCTP_GETTIME_TIMEVAL(&now);
2598 /* Expire time is in Ticks, so we convert to seconds */
2599 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2600 time_expires.tv_usec = cookie->time_entered.tv_usec;
2601 if (timevalcmp(&now, &time_expires, >)) {
2602 /* cookie is stale! */
2603 struct mbuf *op_err;
2604 struct sctp_error_stale_cookie *cause;
2605 struct timeval diff;
2608 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2609 0, M_NOWAIT, 1, MT_DATA);
2610 if (op_err == NULL) {
2615 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
2616 cause = mtod(op_err, struct sctp_error_stale_cookie *);
2617 cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
2618 cause->cause.length = htons((sizeof(struct sctp_paramhdr) +
2619 (sizeof(uint32_t))));
2621 timevalsub(&diff, &time_expires);
2622 if ((uint32_t)diff.tv_sec > UINT32_MAX / 1000000) {
2623 staleness = UINT32_MAX;
2625 staleness = diff.tv_sec * 1000000;
2627 if (UINT32_MAX - staleness >= (uint32_t)diff.tv_usec) {
2628 staleness += diff.tv_usec;
2630 staleness = UINT32_MAX;
2632 cause->stale_time = htonl(staleness);
2633 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2634 mflowtype, mflowid, l_inp->fibnum,
2639 * Now we must see with the lookup address if we have an existing
2640 * asoc. This will only happen if we were in the COOKIE-WAIT state
2641 * and a INIT collided with us and somewhere the peer sent the
2642 * cookie on another address besides the single address our assoc
2643 * had for him. In this case we will have one of the tie-tags set at
2644 * least AND the address field in the cookie can be used to look it
2648 switch (cookie->addr_type) {
2650 case SCTP_IPV6_ADDRESS:
2651 memset(&sin6, 0, sizeof(sin6));
2652 sin6.sin6_family = AF_INET6;
2653 sin6.sin6_len = sizeof(sin6);
2654 sin6.sin6_port = sh->src_port;
2655 sin6.sin6_scope_id = cookie->scope_id;
2656 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2657 sizeof(sin6.sin6_addr.s6_addr));
2658 to = (struct sockaddr *)&sin6;
2662 case SCTP_IPV4_ADDRESS:
2663 memset(&sin, 0, sizeof(sin));
2664 sin.sin_family = AF_INET;
2665 sin.sin_len = sizeof(sin);
2666 sin.sin_port = sh->src_port;
2667 sin.sin_addr.s_addr = cookie->address[0];
2668 to = (struct sockaddr *)&sin;
2672 /* This should not happen */
2675 if (*stcb == NULL) {
2676 /* Yep, lets check */
2677 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2678 if (*stcb == NULL) {
2680 * We should have only got back the same inp. If we
2681 * got back a different ep we have a problem. The
2682 * original findep got back l_inp and now
2684 if (l_inp != *inp_p) {
2685 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2688 if (*locked_tcb == NULL) {
2690 * In this case we found the assoc only
2691 * after we locked the create lock. This
2692 * means we are in a colliding case and we
2693 * must make sure that we unlock the tcb if
2694 * its one of the cases where we throw away
2695 * the incoming packets.
2697 *locked_tcb = *stcb;
2700 * We must also increment the inp ref count
2701 * since the ref_count flags was set when we
2702 * did not find the TCB, now we found it
2703 * which reduces the refcount.. we must
2704 * raise it back out to balance it all :-)
2706 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2707 if ((*stcb)->sctp_ep != l_inp) {
2708 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2709 (void *)(*stcb)->sctp_ep, (void *)l_inp);
2715 cookie_len -= SCTP_SIGNATURE_SIZE;
2716 if (*stcb == NULL) {
2717 /* this is the "normal" case... get a new TCB */
2718 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2719 cookie, cookie_len, *inp_p,
2720 netp, to, ¬ification,
2721 auth_skipped, auth_offset, auth_len,
2725 /* this is abnormal... cookie-echo on existing TCB */
2726 had_a_existing_tcb = 1;
2727 *stcb = sctp_process_cookie_existing(m, iphlen, offset,
2729 cookie, cookie_len, *inp_p, *stcb, netp, to,
2730 ¬ification, auth_skipped, auth_offset, auth_len,
2735 if (*stcb == NULL) {
2736 /* still no TCB... must be bad cookie-echo */
2739 if (*netp != NULL) {
2740 (*netp)->flowtype = mflowtype;
2741 (*netp)->flowid = mflowid;
2744 * Ok, we built an association so confirm the address we sent the
2747 netl = sctp_findnet(*stcb, to);
2749 * This code should in theory NOT run but
2752 /* TSNH! Huh, why do I need to add this address here? */
2753 if (sctp_add_remote_addr(*stcb, to, NULL, port,
2754 SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2757 netl = sctp_findnet(*stcb, to);
2760 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2761 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2762 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2767 sctp_start_net_timers(*stcb);
2768 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2769 if (!had_a_existing_tcb ||
2770 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2772 * If we have a NEW cookie or the connect never
2773 * reached the connected state during collision we
2774 * must do the TCP accept thing.
2776 struct socket *so, *oso;
2777 struct sctp_inpcb *inp;
2779 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2781 * For a restart we will keep the same
2782 * socket, no need to do anything. I THINK!!
2784 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2785 if (send_int_conf) {
2786 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2787 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2791 oso = (*inp_p)->sctp_socket;
2792 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2793 SCTP_TCB_UNLOCK((*stcb));
2794 CURVNET_SET(oso->so_vnet);
2795 so = sonewconn(oso, 0
2798 SCTP_TCB_LOCK((*stcb));
2799 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2802 struct mbuf *op_err;
2803 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2804 struct socket *pcb_so;
2806 /* Too many sockets */
2807 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2808 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2809 sctp_abort_association(*inp_p, NULL, m, iphlen,
2810 src, dst, sh, op_err,
2813 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2814 pcb_so = SCTP_INP_SO(*inp_p);
2815 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2816 SCTP_TCB_UNLOCK((*stcb));
2817 SCTP_SOCKET_LOCK(pcb_so, 1);
2818 SCTP_TCB_LOCK((*stcb));
2819 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2821 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2822 SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2823 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2824 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2828 inp = (struct sctp_inpcb *)so->so_pcb;
2829 SCTP_INP_INCR_REF(inp);
2831 * We add the unbound flag here so that if we get an
2832 * soabort() before we get the move_pcb done, we
2833 * will properly cleanup.
2835 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2836 SCTP_PCB_FLAGS_CONNECTED |
2837 SCTP_PCB_FLAGS_IN_TCPPOOL |
2838 SCTP_PCB_FLAGS_UNBOUND |
2839 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2840 SCTP_PCB_FLAGS_DONT_WAKE);
2841 inp->sctp_features = (*inp_p)->sctp_features;
2842 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2843 inp->sctp_socket = so;
2844 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2845 inp->max_cwnd = (*inp_p)->max_cwnd;
2846 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2847 inp->ecn_supported = (*inp_p)->ecn_supported;
2848 inp->prsctp_supported = (*inp_p)->prsctp_supported;
2849 inp->auth_supported = (*inp_p)->auth_supported;
2850 inp->asconf_supported = (*inp_p)->asconf_supported;
2851 inp->reconfig_supported = (*inp_p)->reconfig_supported;
2852 inp->nrsack_supported = (*inp_p)->nrsack_supported;
2853 inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2854 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2855 inp->sctp_context = (*inp_p)->sctp_context;
2856 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2857 inp->fibnum = (*inp_p)->fibnum;
2858 inp->inp_starting_point_for_iterator = NULL;
2860 * copy in the authentication parameters from the
2863 if (inp->sctp_ep.local_hmacs)
2864 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2865 inp->sctp_ep.local_hmacs =
2866 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2867 if (inp->sctp_ep.local_auth_chunks)
2868 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2869 inp->sctp_ep.local_auth_chunks =
2870 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2873 * Now we must move it from one hash table to
2874 * another and get the tcb in the right place.
2878 * This is where the one-2-one socket is put into
2879 * the accept state waiting for the accept!
2882 SCTP_ADD_SUBSTATE(*stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
2884 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2886 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2887 SCTP_TCB_UNLOCK((*stcb));
2889 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2891 SCTP_TCB_LOCK((*stcb));
2892 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2896 * now we must check to see if we were aborted while
2897 * the move was going on and the lock/unlock
2900 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2902 * yep it was, we leave the assoc attached
2903 * to the socket since the sctp_inpcb_free()
2904 * call will send an abort for us.
2906 SCTP_INP_DECR_REF(inp);
2909 SCTP_INP_DECR_REF(inp);
2910 /* Switch over to the new guy */
2912 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2913 if (send_int_conf) {
2914 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2915 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2919 * Pull it from the incomplete queue and wake the
2922 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2923 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2924 SCTP_TCB_UNLOCK((*stcb));
2925 SCTP_SOCKET_LOCK(so, 1);
2928 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2929 SCTP_TCB_LOCK((*stcb));
2930 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2931 SCTP_SOCKET_UNLOCK(so, 1);
2937 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2939 if (send_int_conf) {
2940 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2941 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2947 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2948 struct sctp_tcb *stcb, struct sctp_nets *net)
2950 /* cp must not be used, others call this without a c-ack :-) */
2951 struct sctp_association *asoc;
2953 SCTPDBG(SCTP_DEBUG_INPUT2,
2954 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2955 if ((stcb == NULL) || (net == NULL)) {
2960 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2961 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2962 asoc->overall_error_count,
2964 SCTP_FROM_SCTP_INPUT,
2967 asoc->overall_error_count = 0;
2968 sctp_stop_all_cookie_timers(stcb);
2969 /* process according to association state */
2970 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) {
2971 /* state change only needed when I am in right state */
2972 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2973 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
2974 sctp_start_net_timers(stcb);
2975 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2976 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2977 stcb->sctp_ep, stcb, asoc->primary_destination);
2981 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2982 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2983 if (asoc->overall_error_count == 0) {
2984 sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
2985 SCTP_RTT_FROM_NON_DATA);
2987 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2988 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2989 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2990 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2991 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2995 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2996 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2997 so = SCTP_INP_SO(stcb->sctp_ep);
2998 atomic_add_int(&stcb->asoc.refcnt, 1);
2999 SCTP_TCB_UNLOCK(stcb);
3000 SCTP_SOCKET_LOCK(so, 1);
3001 SCTP_TCB_LOCK(stcb);
3002 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3004 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
3005 soisconnected(stcb->sctp_socket);
3007 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3008 SCTP_SOCKET_UNLOCK(so, 1);
3012 * since we did not send a HB make sure we don't double
3015 net->hb_responded = 1;
3017 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3019 * We don't need to do the asconf thing, nor hb or
3020 * autoclose if the socket is closed.
3025 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
3029 if (stcb->asoc.sctp_autoclose_ticks &&
3030 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
3031 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
3032 stcb->sctp_ep, stcb, NULL);
3035 * send ASCONF if parameters are pending and ASCONFs are
3036 * allowed (eg. addresses changed when init/cookie echo were
3039 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
3040 (stcb->asoc.asconf_supported == 1) &&
3041 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
3042 #ifdef SCTP_TIMER_BASED_ASCONF
3043 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
3044 stcb->sctp_ep, stcb,
3045 stcb->asoc.primary_destination);
3047 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
3048 SCTP_ADDR_NOT_LOCKED);
3053 /* Toss the cookie if I can */
3054 sctp_toss_old_cookies(stcb, asoc);
3055 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3056 /* Restart the timer if we have pending data */
3057 struct sctp_tmit_chunk *chk;
3059 chk = TAILQ_FIRST(&asoc->sent_queue);
3060 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
3065 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
3066 struct sctp_tcb *stcb)
3068 struct sctp_nets *net;
3069 struct sctp_tmit_chunk *lchk;
3070 struct sctp_ecne_chunk bkup;
3071 uint8_t override_bit;
3072 uint32_t tsn, window_data_tsn;
3074 unsigned int pkt_cnt;
3076 len = ntohs(cp->ch.chunk_length);
3077 if ((len != sizeof(struct sctp_ecne_chunk)) &&
3078 (len != sizeof(struct old_sctp_ecne_chunk))) {
3081 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3082 /* Its the old format */
3083 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3084 bkup.num_pkts_since_cwr = htonl(1);
3087 SCTP_STAT_INCR(sctps_recvecne);
3088 tsn = ntohl(cp->tsn);
3089 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3090 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3092 window_data_tsn = stcb->asoc.sending_seq - 1;
3094 window_data_tsn = lchk->rec.data.tsn;
3097 /* Find where it was sent to if possible. */
3099 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3100 if (lchk->rec.data.tsn == tsn) {
3102 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3105 if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) {
3111 * What to do. A previous send of a CWR was possibly lost.
3112 * See how old it is, we may have it marked on the actual
3115 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3116 if (tsn == net->last_cwr_tsn) {
3117 /* Found him, send it off */
3123 * If we reach here, we need to send a special CWR
3124 * that says hey, we did this a long time ago and
3125 * you lost the response.
3127 net = TAILQ_FIRST(&stcb->asoc.nets);
3132 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3139 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3140 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3142 * JRS - Use the congestion control given in the pluggable
3145 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3147 * We reduce once every RTT. So we will only lower cwnd at
3148 * the next sending seq i.e. the window_data_tsn
3150 net->cwr_window_tsn = window_data_tsn;
3151 net->ecn_ce_pkt_cnt += pkt_cnt;
3152 net->lost_cnt = pkt_cnt;
3153 net->last_cwr_tsn = tsn;
3155 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3156 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3157 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3159 * Another loss in the same window update how many
3160 * marks/packets lost we have had.
3164 if (pkt_cnt > net->lost_cnt) {
3165 /* Should be the case */
3166 cnt = (pkt_cnt - net->lost_cnt);
3167 net->ecn_ce_pkt_cnt += cnt;
3169 net->lost_cnt = pkt_cnt;
3170 net->last_cwr_tsn = tsn;
3172 * Most CC functions will ignore this call, since we
3173 * are in-window yet of the initial CE the peer saw.
3175 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3179 * We always send a CWR this way if our previous one was lost our
3180 * peer will get an update, or if it is not time again to reduce we
3181 * still get the cwr to the peer. Note we set the override when we
3182 * could not find the TSN on the chunk or the destination network.
3184 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3188 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3191 * Here we get a CWR from the peer. We must look in the outqueue and
3192 * make sure that we have a covered ECNE in the control chunk part.
3195 struct sctp_tmit_chunk *chk, *nchk;
3196 struct sctp_ecne_chunk *ecne;
3200 cwr_tsn = ntohl(cp->tsn);
3201 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3202 TAILQ_FOREACH_SAFE(chk, &stcb->asoc.control_send_queue, sctp_next, nchk) {
3203 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3206 if ((override == 0) && (chk->whoTo != net)) {
3207 /* Must be from the right src unless override is set */
3210 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3211 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3212 /* this covers this ECNE, we can remove it */
3213 stcb->asoc.ecn_echo_cnt_onq--;
3214 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3216 stcb->asoc.ctrl_queue_cnt--;
3217 sctp_m_freem(chk->data);
3219 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3220 if (override == 0) {
3228 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3229 struct sctp_tcb *stcb, struct sctp_nets *net)
3231 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3235 SCTPDBG(SCTP_DEBUG_INPUT2,
3236 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3240 /* process according to association state */
3241 if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3242 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3243 SCTPDBG(SCTP_DEBUG_INPUT2,
3244 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3245 SCTP_TCB_UNLOCK(stcb);
3248 /* notify upper layer protocol */
3249 if (stcb->sctp_socket) {
3250 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3253 if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
3254 !TAILQ_EMPTY(&stcb->asoc.sent_queue) ||
3255 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
3256 panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3259 /* stop the timer */
3260 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3261 SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3262 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3264 SCTPDBG(SCTP_DEBUG_INPUT2,
3265 "sctp_handle_shutdown_complete: calls free-asoc\n");
3266 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3267 so = SCTP_INP_SO(stcb->sctp_ep);
3268 atomic_add_int(&stcb->asoc.refcnt, 1);
3269 SCTP_TCB_UNLOCK(stcb);
3270 SCTP_SOCKET_LOCK(so, 1);
3271 SCTP_TCB_LOCK(stcb);
3272 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3274 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3275 SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3276 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3277 SCTP_SOCKET_UNLOCK(so, 1);
3283 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3284 struct sctp_nets *net, uint8_t flg)
3286 switch (desc->chunk_type) {
3288 /* find the tsn to resend (possibly */
3291 struct sctp_tmit_chunk *tp1;
3293 tsn = ntohl(desc->tsn_ifany);
3294 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3295 if (tp1->rec.data.tsn == tsn) {
3299 if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) {
3307 * Do it the other way , aka without paying
3308 * attention to queue seq order.
3310 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3311 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3312 if (tp1->rec.data.tsn == tsn) {
3319 SCTP_STAT_INCR(sctps_pdrptsnnf);
3321 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3324 if (((flg & SCTP_BADCRC) == 0) &&
3325 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3328 if ((stcb->asoc.peers_rwnd == 0) &&
3329 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3330 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3333 if (stcb->asoc.peers_rwnd == 0 &&
3334 (flg & SCTP_FROM_MIDDLE_BOX)) {
3335 SCTP_STAT_INCR(sctps_pdrpdizrw);
3338 ddp = (uint8_t *)(mtod(tp1->data, caddr_t)+
3339 sizeof(struct sctp_data_chunk));
3343 for (iii = 0; iii < sizeof(desc->data_bytes);
3345 if (ddp[iii] != desc->data_bytes[iii]) {
3346 SCTP_STAT_INCR(sctps_pdrpbadd);
3354 * this guy had a RTO calculation
3355 * pending on it, cancel it
3357 if (tp1->whoTo->rto_needed == 0) {
3358 tp1->whoTo->rto_needed = 1;
3362 SCTP_STAT_INCR(sctps_pdrpmark);
3363 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3364 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3366 * mark it as if we were doing a FR, since
3367 * we will be getting gap ack reports behind
3368 * the info from the router.
3370 tp1->rec.data.doing_fast_retransmit = 1;
3372 * mark the tsn with what sequences can
3375 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3376 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3378 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
3381 /* restart the timer */
3382 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3384 SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3385 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3388 /* fix counts and things */
3389 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3390 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3391 tp1->whoTo->flight_size,
3393 (uint32_t)(uintptr_t)stcb,
3396 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3397 sctp_flight_size_decrease(tp1);
3398 sctp_total_flight_decrease(stcb, tp1);
3400 tp1->sent = SCTP_DATAGRAM_RESEND;
3406 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3407 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3410 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3412 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3415 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3416 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3417 audit, stcb->asoc.sent_queue_retran_cnt);
3418 #ifndef SCTP_AUDITING_ENABLED
3419 stcb->asoc.sent_queue_retran_cnt = audit;
3427 struct sctp_tmit_chunk *asconf;
3429 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3431 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3436 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3437 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3438 asconf->sent = SCTP_DATAGRAM_RESEND;
3439 asconf->snd_count--;
3443 case SCTP_INITIATION:
3444 /* resend the INIT */
3445 stcb->asoc.dropped_special_cnt++;
3446 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3448 * If we can get it in, in a few attempts we do
3449 * this, otherwise we let the timer fire.
3451 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3453 SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3454 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3457 case SCTP_SELECTIVE_ACK:
3458 case SCTP_NR_SELECTIVE_ACK:
3459 /* resend the sack */
3460 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3462 case SCTP_HEARTBEAT_REQUEST:
3463 /* resend a demand HB */
3464 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3466 * Only retransmit if we KNOW we wont destroy the
3469 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3473 sctp_send_shutdown(stcb, net);
3475 case SCTP_SHUTDOWN_ACK:
3476 sctp_send_shutdown_ack(stcb, net);
3478 case SCTP_COOKIE_ECHO:
3480 struct sctp_tmit_chunk *cookie;
3483 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3485 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3490 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3491 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3492 cookie->sent = SCTP_DATAGRAM_RESEND;
3493 sctp_stop_all_cookie_timers(stcb);
3497 case SCTP_COOKIE_ACK:
3498 sctp_send_cookie_ack(stcb);
3500 case SCTP_ASCONF_ACK:
3501 /* resend last asconf ack */
3502 sctp_send_asconf_ack(stcb);
3504 case SCTP_IFORWARD_CUM_TSN:
3505 case SCTP_FORWARD_CUM_TSN:
3506 send_forward_tsn(stcb, &stcb->asoc);
3508 /* can't do anything with these */
3509 case SCTP_PACKET_DROPPED:
3510 case SCTP_INITIATION_ACK: /* this should not happen */
3511 case SCTP_HEARTBEAT_ACK:
3512 case SCTP_ABORT_ASSOCIATION:
3513 case SCTP_OPERATION_ERROR:
3514 case SCTP_SHUTDOWN_COMPLETE:
3524 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3530 * We set things to 0xffffffff since this is the last delivered
3531 * sequence and we will be sending in 0 after the reset.
3534 if (number_entries) {
3535 for (i = 0; i < number_entries; i++) {
3536 temp = ntohs(list[i]);
3537 if (temp >= stcb->asoc.streamincnt) {
3540 stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff;
3544 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3545 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
3548 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3552 sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3557 if (number_entries > 0) {
3558 for (i = 0; i < number_entries; i++) {
3559 temp = ntohs(list[i]);
3560 if (temp >= stcb->asoc.streamoutcnt) {
3561 /* no such stream */
3564 stcb->asoc.strmout[temp].next_mid_ordered = 0;
3565 stcb->asoc.strmout[temp].next_mid_unordered = 0;
3568 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3569 stcb->asoc.strmout[i].next_mid_ordered = 0;
3570 stcb->asoc.strmout[i].next_mid_unordered = 0;
3573 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3577 sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3582 if (number_entries > 0) {
3583 for (i = 0; i < number_entries; i++) {
3584 temp = ntohs(list[i]);
3585 if (temp >= stcb->asoc.streamoutcnt) {
3586 /* no such stream */
3589 stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
3592 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3593 stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
3599 struct sctp_stream_reset_request *
3600 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3602 struct sctp_association *asoc;
3603 struct sctp_chunkhdr *ch;
3604 struct sctp_stream_reset_request *r;
3605 struct sctp_tmit_chunk *chk;
3609 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3610 asoc->stream_reset_outstanding = 0;
3613 if (stcb->asoc.str_reset == NULL) {
3614 asoc->stream_reset_outstanding = 0;
3617 chk = stcb->asoc.str_reset;
3618 if (chk->data == NULL) {
3622 /* he wants a copy of the chk pointer */
3625 clen = chk->send_size;
3626 ch = mtod(chk->data, struct sctp_chunkhdr *);
3627 r = (struct sctp_stream_reset_request *)(ch + 1);
3628 if (ntohl(r->request_seq) == seq) {
3632 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3633 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3634 /* move to the next one, there can only be a max of two */
3635 r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3636 if (ntohl(r->request_seq) == seq) {
3640 /* that seq is not here */
3645 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3647 struct sctp_association *asoc;
3648 struct sctp_tmit_chunk *chk;
3651 chk = asoc->str_reset;
3655 asoc->str_reset = NULL;
3656 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
3657 chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3658 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3659 asoc->ctrl_queue_cnt--;
3661 sctp_m_freem(chk->data);
3664 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3669 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3670 uint32_t seq, uint32_t action,
3671 struct sctp_stream_reset_response *respin)
3675 struct sctp_association *asoc = &stcb->asoc;
3676 struct sctp_tmit_chunk *chk;
3677 struct sctp_stream_reset_request *req_param;
3678 struct sctp_stream_reset_out_request *req_out_param;
3679 struct sctp_stream_reset_in_request *req_in_param;
3680 uint32_t number_entries;
3682 if (asoc->stream_reset_outstanding == 0) {
3686 if (seq == stcb->asoc.str_reset_seq_out) {
3687 req_param = sctp_find_stream_reset(stcb, seq, &chk);
3688 if (req_param != NULL) {
3689 stcb->asoc.str_reset_seq_out++;
3690 type = ntohs(req_param->ph.param_type);
3691 lparam_len = ntohs(req_param->ph.param_length);
3692 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3695 req_out_param = (struct sctp_stream_reset_out_request *)req_param;
3696 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3697 asoc->stream_reset_out_is_outstanding = 0;
3698 if (asoc->stream_reset_outstanding)
3699 asoc->stream_reset_outstanding--;
3700 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3702 sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3703 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3704 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3705 } else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
3707 * Set it up so we don't stop
3710 asoc->stream_reset_outstanding++;
3711 stcb->asoc.str_reset_seq_out--;
3712 asoc->stream_reset_out_is_outstanding = 1;
3715 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3717 if (no_clear == 0) {
3718 sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
3720 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3721 req_in_param = (struct sctp_stream_reset_in_request *)req_param;
3722 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3723 if (asoc->stream_reset_outstanding)
3724 asoc->stream_reset_outstanding--;
3725 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3726 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3727 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3728 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3729 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3730 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3732 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3733 /* Ok we now may have more streams */
3736 num_stream = stcb->asoc.strm_pending_add_size;
3737 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3739 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3741 stcb->asoc.strm_pending_add_size = 0;
3742 if (asoc->stream_reset_outstanding)
3743 asoc->stream_reset_outstanding--;
3744 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3745 /* Put the new streams into effect */
3748 for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
3749 asoc->strmout[i].state = SCTP_STREAM_OPEN;
3751 asoc->streamoutcnt += num_stream;
3752 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3753 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3754 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3755 SCTP_STREAM_CHANGE_DENIED);
3757 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3758 SCTP_STREAM_CHANGE_FAILED);
3760 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3761 if (asoc->stream_reset_outstanding)
3762 asoc->stream_reset_outstanding--;
3763 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3764 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3765 SCTP_STREAM_CHANGE_DENIED);
3766 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3767 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3768 SCTP_STREAM_CHANGE_FAILED);
3770 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3772 * a) Adopt the new in tsn.
3774 * c) Adopt the new out-tsn
3776 struct sctp_stream_reset_response_tsn *resp;
3777 struct sctp_forward_tsn_chunk fwdtsn;
3780 if (respin == NULL) {
3784 if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
3787 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3788 resp = (struct sctp_stream_reset_response_tsn *)respin;
3789 asoc->stream_reset_outstanding--;
3790 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3791 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3792 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3793 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3797 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3798 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3799 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3802 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3803 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3804 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3806 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3807 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3809 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3810 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3812 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3813 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3814 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3815 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3816 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3817 SCTP_ASSOC_RESET_DENIED);
3819 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3820 SCTP_ASSOC_RESET_FAILED);
3823 /* get rid of the request and get the request flags */
3824 if (asoc->stream_reset_outstanding == 0) {
3825 sctp_clean_up_stream_reset(stcb);
3829 if (asoc->stream_reset_outstanding == 0) {
3830 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3836 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3837 struct sctp_tmit_chunk *chk,
3838 struct sctp_stream_reset_in_request *req, int trunc)
3846 * peer wants me to send a str-reset to him for my outgoing seq's if
3849 struct sctp_association *asoc = &stcb->asoc;
3851 seq = ntohl(req->request_seq);
3852 if (asoc->str_reset_seq_in == seq) {
3853 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3854 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3855 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3857 /* Can't do it, since they exceeded our buffer size */
3858 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3859 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3860 len = ntohs(req->ph.param_length);
3861 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3862 if (number_entries) {
3863 for (i = 0; i < number_entries; i++) {
3864 temp = ntohs(req->list_of_streams[i]);
3865 if (temp >= stcb->asoc.streamoutcnt) {
3866 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3869 req->list_of_streams[i] = temp;
3871 for (i = 0; i < number_entries; i++) {
3872 if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
3873 stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
3878 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3879 if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
3880 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
3883 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3885 /* Can't do it, since we have sent one out */
3886 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3889 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3890 asoc->str_reset_seq_in++;
3891 } else if (asoc->str_reset_seq_in - 1 == seq) {
3892 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3893 } else if (asoc->str_reset_seq_in - 2 == seq) {
3894 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3896 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3898 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3902 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3903 struct sctp_tmit_chunk *chk,
3904 struct sctp_stream_reset_tsn_request *req)
3906 /* reset all in and out and update the tsn */
3908 * A) reset my str-seq's on in and out. B) Select a receive next,
3909 * and set cum-ack to it. Also process this selected number as a
3910 * fwd-tsn as well. C) set in the response my next sending seq.
3912 struct sctp_forward_tsn_chunk fwdtsn;
3913 struct sctp_association *asoc = &stcb->asoc;
3917 seq = ntohl(req->request_seq);
3918 if (asoc->str_reset_seq_in == seq) {
3919 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3920 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3921 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3923 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3924 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3925 fwdtsn.ch.chunk_flags = 0;
3926 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3927 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3931 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3932 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3933 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3935 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3936 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3937 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3938 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3939 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3940 atomic_add_int(&asoc->sending_seq, 1);
3941 /* save off historical data for retrans */
3942 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3943 asoc->last_sending_seq[0] = asoc->sending_seq;
3944 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3945 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3946 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3947 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3948 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3949 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3951 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3952 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3953 asoc->str_reset_seq_in++;
3954 } else if (asoc->str_reset_seq_in - 1 == seq) {
3955 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3956 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3957 } else if (asoc->str_reset_seq_in - 2 == seq) {
3958 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3959 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3961 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3967 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3968 struct sctp_tmit_chunk *chk,
3969 struct sctp_stream_reset_out_request *req, int trunc)
3972 int number_entries, len;
3973 struct sctp_association *asoc = &stcb->asoc;
3975 seq = ntohl(req->request_seq);
3977 /* now if its not a duplicate we process it */
3978 if (asoc->str_reset_seq_in == seq) {
3979 len = ntohs(req->ph.param_length);
3980 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3982 * the sender is resetting, handle the list issue.. we must
3983 * a) verify if we can do the reset, if so no problem b) If
3984 * we can't do the reset we must copy the request. c) queue
3985 * it, and setup the data in processor to trigger it off
3986 * when needed and dequeue all the queued data.
3988 tsn = ntohl(req->send_reset_at_tsn);
3990 /* move the reset action back one */
3991 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3992 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3993 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3995 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3996 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3997 /* we can do it now */
3998 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3999 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4002 * we must queue it up and thus wait for the TSN's
4003 * to arrive that are at or before tsn
4005 struct sctp_stream_reset_list *liste;
4008 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
4009 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
4010 siz, SCTP_M_STRESET);
4011 if (liste == NULL) {
4012 /* gak out of memory */
4013 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4014 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4019 liste->number_entries = number_entries;
4020 memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
4021 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
4022 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
4024 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4025 asoc->str_reset_seq_in++;
4026 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4028 * one seq back, just echo back last action since my
4029 * response was lost.
4031 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4032 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4034 * two seq back, just echo back last action since my
4035 * response was lost.
4037 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4039 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4044 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4045 struct sctp_stream_reset_add_strm *str_add)
4048 * Peer is requesting to add more streams. If its within our
4049 * max-streams we will allow it.
4051 uint32_t num_stream, i;
4053 struct sctp_association *asoc = &stcb->asoc;
4054 struct sctp_queued_to_read *ctl, *nctl;
4056 /* Get the number. */
4057 seq = ntohl(str_add->request_seq);
4058 num_stream = ntohs(str_add->number_of_streams);
4059 /* Now what would be the new total? */
4060 if (asoc->str_reset_seq_in == seq) {
4061 num_stream += stcb->asoc.streamincnt;
4062 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4063 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4064 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4065 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
4066 (num_stream > 0xffff)) {
4067 /* We must reject it they ask for to many */
4069 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4071 /* Ok, we can do that :-) */
4072 struct sctp_stream_in *oldstrm;
4074 /* save off the old */
4075 oldstrm = stcb->asoc.strmin;
4076 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
4077 (num_stream * sizeof(struct sctp_stream_in)),
4079 if (stcb->asoc.strmin == NULL) {
4080 stcb->asoc.strmin = oldstrm;
4083 /* copy off the old data */
4084 for (i = 0; i < stcb->asoc.streamincnt; i++) {
4085 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4086 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4087 stcb->asoc.strmin[i].sid = i;
4088 stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered;
4089 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
4090 stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
4091 /* now anything on those queues? */
4092 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
4093 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
4094 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
4096 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
4097 TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
4098 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
4101 /* Init the new streams */
4102 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
4103 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4104 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4105 stcb->asoc.strmin[i].sid = i;
4106 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
4107 stcb->asoc.strmin[i].pd_api_started = 0;
4108 stcb->asoc.strmin[i].delivery_started = 0;
4110 SCTP_FREE(oldstrm, SCTP_M_STRMI);
4111 /* update the size */
4112 stcb->asoc.streamincnt = num_stream;
4113 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4114 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
4116 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4117 asoc->str_reset_seq_in++;
4118 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4120 * one seq back, just echo back last action since my
4121 * response was lost.
4123 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4124 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4126 * two seq back, just echo back last action since my
4127 * response was lost.
4129 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4131 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4137 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4138 struct sctp_stream_reset_add_strm *str_add)
4141 * Peer is requesting to add more streams. If its within our
4142 * max-streams we will allow it.
4144 uint16_t num_stream;
4146 struct sctp_association *asoc = &stcb->asoc;
4148 /* Get the number. */
4149 seq = ntohl(str_add->request_seq);
4150 num_stream = ntohs(str_add->number_of_streams);
4151 /* Now what would be the new total? */
4152 if (asoc->str_reset_seq_in == seq) {
4153 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4154 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4155 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4156 } else if (stcb->asoc.stream_reset_outstanding) {
4157 /* We must reject it we have something pending */
4158 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4160 /* Ok, we can do that :-) */
4163 mychk = stcb->asoc.streamoutcnt;
4164 mychk += num_stream;
4165 if (mychk < 0x10000) {
4166 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4167 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
4168 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4171 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4174 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4175 asoc->str_reset_seq_in++;
4176 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4178 * one seq back, just echo back last action since my
4179 * response was lost.
4181 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4182 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4184 * two seq back, just echo back last action since my
4185 * response was lost.
4187 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4189 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4194 __attribute__((noinline))
4197 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4198 struct sctp_chunkhdr *ch_req)
4200 uint16_t remaining_length, param_len, ptype;
4201 struct sctp_paramhdr pstore;
4202 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4206 struct sctp_tmit_chunk *chk;
4207 struct sctp_chunkhdr *ch;
4208 struct sctp_paramhdr *ph;
4212 /* now it may be a reset or a reset-response */
4213 remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
4215 /* setup for adding the response */
4216 sctp_alloc_a_chunk(stcb, chk);
4220 chk->copy_by_ref = 0;
4221 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4222 chk->rec.chunk_id.can_take_data = 0;
4224 chk->asoc = &stcb->asoc;
4225 chk->no_fr_allowed = 0;
4226 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4227 chk->book_size_scale = 0;
4228 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4229 if (chk->data == NULL) {
4232 sctp_m_freem(chk->data);
4235 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4238 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4240 /* setup chunk parameters */
4241 chk->sent = SCTP_DATAGRAM_UNSENT;
4245 ch = mtod(chk->data, struct sctp_chunkhdr *);
4246 ch->chunk_type = SCTP_STREAM_RESET;
4247 ch->chunk_flags = 0;
4248 ch->chunk_length = htons(chk->send_size);
4249 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4250 offset += sizeof(struct sctp_chunkhdr);
4251 while (remaining_length >= sizeof(struct sctp_paramhdr)) {
4252 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
4257 param_len = ntohs(ph->param_length);
4258 if ((param_len > remaining_length) ||
4259 (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
4260 /* bad parameter length */
4263 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
4264 (uint8_t *)&cstore);
4269 ptype = ntohs(ph->param_type);
4271 if (param_len > sizeof(cstore)) {
4276 if (num_param > SCTP_MAX_RESET_PARAMS) {
4277 /* hit the max of parameters already sorry.. */
4280 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4281 struct sctp_stream_reset_out_request *req_out;
4283 if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
4286 req_out = (struct sctp_stream_reset_out_request *)ph;
4288 if (stcb->asoc.stream_reset_outstanding) {
4289 seq = ntohl(req_out->response_seq);
4290 if (seq == stcb->asoc.str_reset_seq_out) {
4292 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4295 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4296 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4297 struct sctp_stream_reset_add_strm *str_add;
4299 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4302 str_add = (struct sctp_stream_reset_add_strm *)ph;
4304 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4305 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4306 struct sctp_stream_reset_add_strm *str_add;
4308 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4311 str_add = (struct sctp_stream_reset_add_strm *)ph;
4313 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4314 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4315 struct sctp_stream_reset_in_request *req_in;
4318 req_in = (struct sctp_stream_reset_in_request *)ph;
4319 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4320 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4321 struct sctp_stream_reset_tsn_request *req_tsn;
4324 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4325 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4327 goto strres_nochunk;
4331 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4332 struct sctp_stream_reset_response *resp;
4335 if (param_len < sizeof(struct sctp_stream_reset_response)) {
4338 resp = (struct sctp_stream_reset_response *)ph;
4339 seq = ntohl(resp->response_seq);
4340 result = ntohl(resp->result);
4341 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4343 goto strres_nochunk;
4348 offset += SCTP_SIZE32(param_len);
4349 if (remaining_length >= SCTP_SIZE32(param_len)) {
4350 remaining_length -= SCTP_SIZE32(param_len);
4352 remaining_length = 0;
4356 /* we have no response free the stuff */
4357 goto strres_nochunk;
4359 /* ok we have a chunk to link in */
4360 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4363 stcb->asoc.ctrl_queue_cnt++;
4368 * Handle a router or endpoints report of a packet loss, there are two ways
4369 * to handle this, either we get the whole packet and must disect it
4370 * ourselves (possibly with truncation and or corruption) or it is a summary
4371 * from a middle box that did the disectting for us.
4374 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4375 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4377 uint32_t bottle_bw, on_queue;
4381 struct sctp_chunk_desc desc;
4382 struct sctp_chunkhdr *ch;
4384 chlen = ntohs(cp->ch.chunk_length);
4385 chlen -= sizeof(struct sctp_pktdrop_chunk);
4386 /* XXX possible chlen underflow */
4389 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4390 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4392 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4393 chlen -= sizeof(struct sctphdr);
4394 /* XXX possible chlen underflow */
4395 memset(&desc, 0, sizeof(desc));
4397 trunc_len = (uint16_t)ntohs(cp->trunc_len);
4398 if (trunc_len > limit) {
4402 /* now the chunks themselves */
4403 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4404 desc.chunk_type = ch->chunk_type;
4405 /* get amount we need to move */
4406 at = ntohs(ch->chunk_length);
4407 if (at < sizeof(struct sctp_chunkhdr)) {
4408 /* corrupt chunk, maybe at the end? */
4409 SCTP_STAT_INCR(sctps_pdrpcrupt);
4412 if (trunc_len == 0) {
4413 /* we are supposed to have all of it */
4415 /* corrupt skip it */
4416 SCTP_STAT_INCR(sctps_pdrpcrupt);
4420 /* is there enough of it left ? */
4421 if (desc.chunk_type == SCTP_DATA) {
4422 if (chlen < (sizeof(struct sctp_data_chunk) +
4423 sizeof(desc.data_bytes))) {
4427 if (chlen < sizeof(struct sctp_chunkhdr)) {
4432 if (desc.chunk_type == SCTP_DATA) {
4433 /* can we get out the tsn? */
4434 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4435 SCTP_STAT_INCR(sctps_pdrpmbda);
4437 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4439 struct sctp_data_chunk *dcp;
4443 dcp = (struct sctp_data_chunk *)ch;
4444 ddp = (uint8_t *)(dcp + 1);
4445 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4446 desc.data_bytes[iii] = ddp[iii];
4448 desc.tsn_ifany = dcp->dp.tsn;
4450 /* nope we are done. */
4451 SCTP_STAT_INCR(sctps_pdrpnedat);
4455 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4456 SCTP_STAT_INCR(sctps_pdrpmbct);
4459 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4460 SCTP_STAT_INCR(sctps_pdrppdbrk);
4463 if (SCTP_SIZE32(at) > chlen) {
4466 chlen -= SCTP_SIZE32(at);
4467 if (chlen < sizeof(struct sctp_chunkhdr)) {
4468 /* done, none left */
4471 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4473 /* Now update any rwnd --- possibly */
4474 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4475 /* From a peer, we get a rwnd report */
4478 SCTP_STAT_INCR(sctps_pdrpfehos);
4480 bottle_bw = ntohl(cp->bottle_bw);
4481 on_queue = ntohl(cp->current_onq);
4482 if (bottle_bw && on_queue) {
4483 /* a rwnd report is in here */
4484 if (bottle_bw > on_queue)
4485 a_rwnd = bottle_bw - on_queue;
4490 stcb->asoc.peers_rwnd = 0;
4492 if (a_rwnd > stcb->asoc.total_flight) {
4493 stcb->asoc.peers_rwnd =
4494 a_rwnd - stcb->asoc.total_flight;
4496 stcb->asoc.peers_rwnd = 0;
4498 if (stcb->asoc.peers_rwnd <
4499 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4500 /* SWS sender side engages */
4501 stcb->asoc.peers_rwnd = 0;
4506 SCTP_STAT_INCR(sctps_pdrpfmbox);
4509 /* now middle boxes in sat networks get a cwnd bump */
4510 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4511 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4512 (stcb->asoc.sat_network)) {
4514 * This is debatable but for sat networks it makes sense
4515 * Note if a T3 timer has went off, we will prohibit any
4516 * changes to cwnd until we exit the t3 loss recovery.
4518 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4519 net, cp, &bottle_bw, &on_queue);
4524 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4525 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4526 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4527 * length of the complete packet outputs: - length: modified to remaining
4528 * length after control processing - netp: modified to new sctp_nets after
4529 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4530 * bad packet,...) otherwise return the tcb for this packet
4533 __attribute__((noinline))
4535 static struct sctp_tcb *
4536 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4537 struct sockaddr *src, struct sockaddr *dst,
4538 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4539 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4540 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4541 uint32_t vrf_id, uint16_t port)
4543 struct sctp_association *asoc;
4544 struct mbuf *op_err;
4545 char msg[SCTP_DIAG_INFO_LEN];
4547 int num_chunks = 0; /* number of control chunks processed */
4548 uint32_t chk_length, contiguous;
4550 int abort_no_unlock = 0;
4554 * How big should this be, and should it be alloc'd? Lets try the
4555 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4556 * until we get into jumbo grams and such..
4558 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4560 uint32_t auth_offset = 0, auth_len = 0;
4561 int auth_skipped = 0;
4563 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4567 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4568 iphlen, *offset, length, (void *)stcb);
4571 SCTP_TCB_LOCK_ASSERT(stcb);
4573 /* validate chunk header length... */
4574 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4575 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4576 ntohs(ch->chunk_length));
4581 * validate the verification tag
4583 vtag_in = ntohl(sh->v_tag);
4585 if (ch->chunk_type == SCTP_INITIATION) {
4586 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4587 ntohs(ch->chunk_length), vtag_in);
4589 /* protocol error- silently discard... */
4590 SCTP_STAT_INCR(sctps_badvtag);
4592 SCTP_TCB_UNLOCK(stcb);
4596 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4598 * If there is no stcb, skip the AUTH chunk and process
4599 * later after a stcb is found (to validate the lookup was
4602 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4604 (inp->auth_supported == 1)) {
4605 /* save this chunk for later processing */
4607 auth_offset = *offset;
4608 auth_len = ntohs(ch->chunk_length);
4610 /* (temporarily) move past this chunk */
4611 *offset += SCTP_SIZE32(auth_len);
4612 if (*offset >= length) {
4613 /* no more data left in the mbuf chain */
4617 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4618 sizeof(struct sctp_chunkhdr), chunk_buf);
4625 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4626 goto process_control_chunks;
4629 * first check if it's an ASCONF with an unknown src addr we
4630 * need to look inside to find the association
4632 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4633 struct sctp_chunkhdr *asconf_ch = ch;
4634 uint32_t asconf_offset = 0, asconf_len = 0;
4636 /* inp's refcount may be reduced */
4637 SCTP_INP_INCR_REF(inp);
4639 asconf_offset = *offset;
4641 asconf_len = ntohs(asconf_ch->chunk_length);
4642 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4644 stcb = sctp_findassociation_ep_asconf(m,
4647 sh, &inp, netp, vrf_id);
4650 asconf_offset += SCTP_SIZE32(asconf_len);
4651 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4652 sizeof(struct sctp_chunkhdr), chunk_buf);
4653 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4656 * reduce inp's refcount if not reduced in
4657 * sctp_findassociation_ep_asconf().
4659 SCTP_INP_DECR_REF(inp);
4662 /* now go back and verify any auth chunk to be sure */
4663 if (auth_skipped && (stcb != NULL)) {
4664 struct sctp_auth_chunk *auth;
4666 auth = (struct sctp_auth_chunk *)
4667 sctp_m_getptr(m, auth_offset,
4668 auth_len, chunk_buf);
4671 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4673 /* auth HMAC failed so dump it */
4677 /* remaining chunks are HMAC checked */
4678 stcb->asoc.authenticated = 1;
4683 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4684 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4686 /* no association, so it's out of the blue... */
4687 sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4688 mflowtype, mflowid, inp->fibnum,
4694 /* ABORT and SHUTDOWN can use either v_tag... */
4695 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4696 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4697 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4698 /* Take the T-bit always into account. */
4699 if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
4700 (vtag_in == asoc->my_vtag)) ||
4701 (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4702 (asoc->peer_vtag != htonl(0)) &&
4703 (vtag_in == asoc->peer_vtag))) {
4706 /* drop this packet... */
4707 SCTP_STAT_INCR(sctps_badvtag);
4709 SCTP_TCB_UNLOCK(stcb);
4713 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4714 if (vtag_in != asoc->my_vtag) {
4716 * this could be a stale SHUTDOWN-ACK or the
4717 * peer never got the SHUTDOWN-COMPLETE and
4718 * is still hung; we have started a new asoc
4719 * but it won't complete until the shutdown
4723 SCTP_TCB_UNLOCK(stcb);
4725 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4726 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4728 sctp_handle_ootb(m, iphlen, *offset, src, dst,
4730 mflowtype, mflowid, fibnum,
4735 /* for all other chunks, vtag must match */
4736 if (vtag_in != asoc->my_vtag) {
4737 /* invalid vtag... */
4738 SCTPDBG(SCTP_DEBUG_INPUT3,
4739 "invalid vtag: %xh, expect %xh\n",
4740 vtag_in, asoc->my_vtag);
4741 SCTP_STAT_INCR(sctps_badvtag);
4743 SCTP_TCB_UNLOCK(stcb);
4749 } /* end if !SCTP_COOKIE_ECHO */
4751 * process all control chunks...
4753 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4754 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4755 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4756 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4757 /* implied cookie-ack.. we must have lost the ack */
4758 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4762 process_control_chunks:
4763 while (IS_SCTP_CONTROL(ch)) {
4764 /* validate chunk length */
4765 chk_length = ntohs(ch->chunk_length);
4766 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4767 ch->chunk_type, chk_length);
4768 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4769 if (chk_length < sizeof(*ch) ||
4770 (*offset + (int)chk_length) > length) {
4774 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4776 * INIT and INIT-ACK only gets the init ack "header" portion
4777 * only because we don't have to process the peer's COOKIE.
4778 * All others get a complete chunk.
4780 switch (ch->chunk_type) {
4781 case SCTP_INITIATION:
4782 contiguous = sizeof(struct sctp_init_chunk);
4784 case SCTP_INITIATION_ACK:
4785 contiguous = sizeof(struct sctp_init_ack_chunk);
4788 contiguous = min(chk_length, sizeof(chunk_buf));
4791 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4797 SCTP_TCB_UNLOCK(stcb);
4803 /* Save off the last place we got a control from */
4805 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4807 * allow last_control to be NULL if
4808 * ASCONF... ASCONF processing will find the
4811 if ((netp != NULL) && (*netp != NULL))
4812 stcb->asoc.last_control_chunk_from = *netp;
4815 #ifdef SCTP_AUDITING_ENABLED
4816 sctp_audit_log(0xB0, ch->chunk_type);
4819 /* check to see if this chunk required auth, but isn't */
4820 if ((stcb != NULL) &&
4821 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4822 !stcb->asoc.authenticated) {
4823 /* "silently" ignore */
4824 SCTP_STAT_INCR(sctps_recvauthmissing);
4827 switch (ch->chunk_type) {
4828 case SCTP_INITIATION:
4829 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4830 /* The INIT chunk must be the only chunk. */
4831 if ((num_chunks > 1) ||
4832 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4833 /* RFC 4960 requires that no ABORT is sent */
4836 SCTP_TCB_UNLOCK(stcb);
4840 /* Honor our resource limit. */
4841 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4842 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4843 sctp_abort_association(inp, stcb, m, iphlen,
4844 src, dst, sh, op_err,
4850 sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4851 (struct sctp_init_chunk *)ch, inp,
4852 stcb, *netp, &abort_no_unlock,
4856 if ((!abort_no_unlock) && (stcb != NULL)) {
4857 SCTP_TCB_UNLOCK(stcb);
4861 case SCTP_PAD_CHUNK:
4863 case SCTP_INITIATION_ACK:
4864 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT_ACK\n");
4865 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4866 /* We are not interested anymore */
4867 if ((stcb != NULL) && (stcb->asoc.total_output_queue_size)) {
4872 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4873 so = SCTP_INP_SO(inp);
4874 atomic_add_int(&stcb->asoc.refcnt, 1);
4875 SCTP_TCB_UNLOCK(stcb);
4876 SCTP_SOCKET_LOCK(so, 1);
4877 SCTP_TCB_LOCK(stcb);
4878 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4880 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4881 SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4882 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4883 SCTP_SOCKET_UNLOCK(so, 1);
4889 /* The INIT-ACK chunk must be the only chunk. */
4890 if ((num_chunks > 1) ||
4891 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4895 if ((netp != NULL) && (*netp != NULL)) {
4896 ret = sctp_handle_init_ack(m, iphlen, *offset,
4898 (struct sctp_init_ack_chunk *)ch,
4907 if (abort_no_unlock) {
4911 * Special case, I must call the output routine to
4912 * get the cookie echoed
4914 if ((stcb != NULL) && (ret == 0)) {
4915 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4919 case SCTP_SELECTIVE_ACK:
4920 case SCTP_NR_SELECTIVE_ACK:
4923 uint32_t a_rwnd, cum_ack;
4924 uint16_t num_seg, num_nr_seg, num_dup;
4926 int offset_seg, offset_dup;
4928 SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n",
4929 ch->chunk_type == SCTP_SELECTIVE_ACK ? "SCTP_SACK" : "SCTP_NR_SACK");
4930 SCTP_STAT_INCR(sctps_recvsacks);
4932 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing %s chunk\n",
4933 (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK");
4936 if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4937 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4938 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4942 if (stcb->asoc.nrsack_supported == 0) {
4945 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4946 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR_SACK chunk, too small\n");
4950 if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4952 * If we have sent a shutdown-ack, we will pay no
4953 * attention to a sack sent in to us since
4954 * we don't care anymore.
4958 flags = ch->chunk_flags;
4959 if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4960 struct sctp_sack_chunk *sack;
4962 sack = (struct sctp_sack_chunk *)ch;
4963 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4964 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4966 num_dup = ntohs(sack->sack.num_dup_tsns);
4967 a_rwnd = ntohl(sack->sack.a_rwnd);
4968 if (sizeof(struct sctp_sack_chunk) +
4969 num_seg * sizeof(struct sctp_gap_ack_block) +
4970 num_dup * sizeof(uint32_t) != chk_length) {
4971 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4974 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4975 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4977 struct sctp_nr_sack_chunk *nr_sack;
4979 nr_sack = (struct sctp_nr_sack_chunk *)ch;
4980 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4981 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4982 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4983 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4984 a_rwnd = ntohl(nr_sack->nr_sack.a_rwnd);
4985 if (sizeof(struct sctp_nr_sack_chunk) +
4986 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4987 num_dup * sizeof(uint32_t) != chk_length) {
4988 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4991 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4992 offset_dup = offset_seg + (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block);
4994 SCTPDBG(SCTP_DEBUG_INPUT3, "%s process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4995 (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK",
4996 cum_ack, num_seg, a_rwnd);
4997 stcb->asoc.seen_a_sack_this_pkt = 1;
4998 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4999 (num_seg == 0) && (num_nr_seg == 0) &&
5000 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
5001 (stcb->asoc.saw_sack_with_frags == 0) &&
5002 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
5003 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
5005 * We have a SIMPLE sack having no
5006 * prior segments and data on sent
5007 * queue to be acked. Use the faster
5008 * path sack processing. We also
5009 * allow window update sacks with no
5010 * missing segments to go this way
5013 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
5014 &abort_now, ecne_seen);
5016 if ((netp != NULL) && (*netp != NULL)) {
5017 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
5018 num_seg, num_nr_seg, num_dup, &abort_now, flags,
5019 cum_ack, a_rwnd, ecne_seen);
5023 /* ABORT signal from sack processing */
5027 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5028 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5029 (stcb->asoc.stream_queue_cnt == 0)) {
5030 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5034 case SCTP_HEARTBEAT_REQUEST:
5035 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
5036 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5037 SCTP_STAT_INCR(sctps_recvheartbeat);
5038 sctp_send_heartbeat_ack(stcb, m, *offset,
5042 case SCTP_HEARTBEAT_ACK:
5043 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT_ACK\n");
5044 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5049 SCTP_STAT_INCR(sctps_recvheartbeatack);
5050 if ((netp != NULL) && (*netp != NULL)) {
5051 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5055 case SCTP_ABORT_ASSOCIATION:
5056 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5059 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5060 if (sctp_handle_abort((struct sctp_abort_chunk *)ch, stcb, *netp)) {
5070 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5072 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5076 if ((netp != NULL) && (*netp != NULL)) {
5079 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5080 stcb, *netp, &abort_flag);
5087 case SCTP_SHUTDOWN_ACK:
5088 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_ACK, stcb %p\n", (void *)stcb);
5089 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5090 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5095 case SCTP_OPERATION_ERROR:
5096 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP_ERR\n");
5097 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL) &&
5098 sctp_handle_error(ch, stcb, *netp, contiguous) < 0) {
5103 case SCTP_COOKIE_ECHO:
5104 SCTPDBG(SCTP_DEBUG_INPUT3,
5105 "SCTP_COOKIE_ECHO, stcb %p\n", (void *)stcb);
5106 if ((stcb != NULL) && (stcb->asoc.total_output_queue_size > 0)) {
5109 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5110 /* We are not interested anymore */
5113 SCTP_TCB_UNLOCK(stcb);
5120 * First are we accepting? We do this again here
5121 * since it is possible that a previous endpoint WAS
5122 * listening responded to a INIT-ACK and then
5123 * closed. We opened and bound.. and are now no
5126 * XXXGL: notes on checking listen queue length.
5127 * 1) SCTP_IS_LISTENING() doesn't necessarily mean
5128 * SOLISTENING(), because a listening "UDP type"
5129 * socket isn't listening in terms of the socket
5130 * layer. It is a normal data flow socket, that
5131 * can fork off new connections. Thus, we should
5132 * look into sol_qlen only in case we are !UDP.
5133 * 2) Checking sol_qlen in general requires locking
5134 * the socket, and this code lacks that.
5136 if ((stcb == NULL) &&
5137 (!SCTP_IS_LISTENING(inp) ||
5138 (!(inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
5139 inp->sctp_socket->sol_qlen >= inp->sctp_socket->sol_qlimit))) {
5140 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5141 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5142 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
5143 sctp_abort_association(inp, stcb, m, iphlen,
5144 src, dst, sh, op_err,
5151 struct mbuf *ret_buf;
5152 struct sctp_inpcb *linp;
5161 SCTP_ASOC_CREATE_LOCK(linp);
5162 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5163 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5164 SCTP_ASOC_CREATE_UNLOCK(linp);
5170 struct sctp_tcb *locked_stcb;
5174 sctp_handle_cookie_echo(m, iphlen,
5178 (struct sctp_cookie_echo_chunk *)ch,
5188 if ((locked_stcb != NULL) && (locked_stcb != stcb)) {
5189 SCTP_TCB_UNLOCK(locked_stcb);
5192 SCTP_TCB_LOCK_ASSERT(stcb);
5198 SCTP_ASOC_CREATE_UNLOCK(linp);
5200 if (ret_buf == NULL) {
5202 SCTP_TCB_UNLOCK(stcb);
5204 SCTPDBG(SCTP_DEBUG_INPUT3,
5205 "GAK, null buffer\n");
5209 /* if AUTH skipped, see if it verified... */
5214 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5216 * Restart the timer if we have
5219 struct sctp_tmit_chunk *chk;
5221 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5222 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5226 case SCTP_COOKIE_ACK:
5227 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE_ACK, stcb %p\n", (void *)stcb);
5228 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5231 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5232 /* We are not interested anymore */
5233 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5236 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5237 so = SCTP_INP_SO(inp);
5238 atomic_add_int(&stcb->asoc.refcnt, 1);
5239 SCTP_TCB_UNLOCK(stcb);
5240 SCTP_SOCKET_LOCK(so, 1);
5241 SCTP_TCB_LOCK(stcb);
5242 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5244 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5245 SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5246 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5247 SCTP_SOCKET_UNLOCK(so, 1);
5253 if ((netp != NULL) && (*netp != NULL)) {
5254 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5258 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_ECHO\n");
5259 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5264 if (stcb->asoc.ecn_supported == 0) {
5267 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, stcb);
5271 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_CWR\n");
5272 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5276 if (stcb->asoc.ecn_supported == 0) {
5279 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5281 case SCTP_SHUTDOWN_COMPLETE:
5282 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_COMPLETE, stcb %p\n", (void *)stcb);
5283 /* must be first and only chunk */
5284 if ((num_chunks > 1) ||
5285 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5289 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5290 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5297 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5299 if (stcb->asoc.asconf_supported == 0) {
5302 sctp_handle_asconf(m, *offset, src,
5303 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5307 case SCTP_ASCONF_ACK:
5308 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF_ACK\n");
5309 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5314 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5315 if (stcb->asoc.asconf_supported == 0) {
5318 /* He's alive so give him credit */
5319 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5320 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5321 stcb->asoc.overall_error_count,
5323 SCTP_FROM_SCTP_INPUT,
5326 stcb->asoc.overall_error_count = 0;
5327 sctp_handle_asconf_ack(m, *offset,
5328 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5329 if (abort_no_unlock)
5333 case SCTP_FORWARD_CUM_TSN:
5334 case SCTP_IFORWARD_CUM_TSN:
5335 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD_TSN\n");
5336 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5345 if (stcb->asoc.prsctp_supported == 0) {
5349 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5350 /* We are not interested anymore */
5351 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5352 so = SCTP_INP_SO(inp);
5353 atomic_add_int(&stcb->asoc.refcnt, 1);
5354 SCTP_TCB_UNLOCK(stcb);
5355 SCTP_SOCKET_LOCK(so, 1);
5356 SCTP_TCB_LOCK(stcb);
5357 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5359 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5360 SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5361 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5362 SCTP_SOCKET_UNLOCK(so, 1);
5368 * For sending a SACK this looks like DATA
5371 stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from;
5372 sctp_handle_forward_tsn(stcb,
5373 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5380 case SCTP_STREAM_RESET:
5381 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5382 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5387 if (stcb->asoc.reconfig_supported == 0) {
5390 if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5391 /* stop processing */
5396 case SCTP_PACKET_DROPPED:
5397 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5398 /* re-get it all please */
5399 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5405 if ((ch != NULL) && (stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5406 if (stcb->asoc.pktdrop_supported == 0) {
5409 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5411 min(chk_length, contiguous));
5414 case SCTP_AUTHENTICATION:
5415 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5417 /* save the first AUTH for later processing */
5418 if (auth_skipped == 0) {
5419 auth_offset = *offset;
5420 auth_len = chk_length;
5423 /* skip this chunk (temporarily) */
5426 if (stcb->asoc.auth_supported == 0) {
5429 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5430 (chk_length > (sizeof(struct sctp_auth_chunk) +
5431 SCTP_AUTH_DIGEST_LEN_MAX))) {
5436 if (got_auth == 1) {
5437 /* skip this chunk... it's already auth'd */
5441 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5443 /* auth HMAC failed so dump the packet */
5447 /* remaining chunks are HMAC checked */
5448 stcb->asoc.authenticated = 1;
5454 /* it's an unknown chunk! */
5455 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5456 struct sctp_gen_error_cause *cause;
5459 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5460 0, M_NOWAIT, 1, MT_DATA);
5461 if (op_err != NULL) {
5462 len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset));
5463 cause = mtod(op_err, struct sctp_gen_error_cause *);
5464 cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5465 cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause)));
5466 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
5467 SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
5468 if (SCTP_BUF_NEXT(op_err) != NULL) {
5469 #ifdef SCTP_MBUF_LOGGING
5470 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5471 sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5474 sctp_queue_op_err(stcb, op_err);
5476 sctp_m_freem(op_err);
5480 if ((ch->chunk_type & 0x80) == 0) {
5481 /* discard this packet */
5484 } /* else skip this bad chunk and continue... */
5486 } /* switch (ch->chunk_type) */
5490 /* get the next chunk */
5491 *offset += SCTP_SIZE32(chk_length);
5492 if (*offset >= length) {
5493 /* no more data left in the mbuf chain */
5496 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5497 sizeof(struct sctp_chunkhdr), chunk_buf);
5504 if ((asconf_cnt > 0) && (stcb != NULL)) {
5505 sctp_send_asconf_ack(stcb);
5512 * common input chunk processing (v4 and v6)
5515 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5516 struct sockaddr *src, struct sockaddr *dst,
5517 struct sctphdr *sh, struct sctp_chunkhdr *ch,
5518 uint8_t compute_crc,
5520 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5521 uint32_t vrf_id, uint16_t port)
5524 int fwd_tsn_seen = 0, data_processed = 0;
5525 struct mbuf *m = *mm, *op_err;
5526 char msg[SCTP_DIAG_INFO_LEN];
5528 int cnt_ctrl_ready = 0;
5529 struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5530 struct sctp_tcb *stcb = NULL;
5531 struct sctp_nets *net = NULL;
5533 SCTP_STAT_INCR(sctps_recvdatagrams);
5534 #ifdef SCTP_AUDITING_ENABLED
5535 sctp_audit_log(0xE0, 1);
5536 sctp_auditing(0, inp, stcb, net);
5538 if (compute_crc != 0) {
5539 uint32_t check, calc_check;
5541 check = sh->checksum;
5543 calc_check = sctp_calculate_cksum(m, iphlen);
5544 sh->checksum = check;
5545 if (calc_check != check) {
5546 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5547 calc_check, check, (void *)m, length, iphlen);
5548 stcb = sctp_findassociation_addr(m, offset, src, dst,
5549 sh, ch, &inp, &net, vrf_id);
5550 #if defined(INET) || defined(INET6)
5551 if ((ch->chunk_type != SCTP_INITIATION) &&
5552 (net != NULL) && (net->port != port)) {
5553 if (net->port == 0) {
5554 /* UDP encapsulation turned on. */
5555 net->mtu -= sizeof(struct udphdr);
5556 if (stcb->asoc.smallest_mtu > net->mtu) {
5557 sctp_pathmtu_adjustment(stcb, net->mtu);
5559 } else if (port == 0) {
5560 /* UDP encapsulation turned off. */
5561 net->mtu += sizeof(struct udphdr);
5562 /* XXX Update smallest_mtu */
5568 net->flowtype = mflowtype;
5569 net->flowid = mflowid;
5571 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5572 if ((inp != NULL) && (stcb != NULL)) {
5573 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5574 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5575 } else if ((inp != NULL) && (stcb == NULL)) {
5578 SCTP_STAT_INCR(sctps_badsum);
5579 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5583 /* Destination port of 0 is illegal, based on RFC4960. */
5584 if (sh->dest_port == 0) {
5585 SCTP_STAT_INCR(sctps_hdrops);
5588 stcb = sctp_findassociation_addr(m, offset, src, dst,
5589 sh, ch, &inp, &net, vrf_id);
5590 #if defined(INET) || defined(INET6)
5591 if ((ch->chunk_type != SCTP_INITIATION) &&
5592 (net != NULL) && (net->port != port)) {
5593 if (net->port == 0) {
5594 /* UDP encapsulation turned on. */
5595 net->mtu -= sizeof(struct udphdr);
5596 if (stcb->asoc.smallest_mtu > net->mtu) {
5597 sctp_pathmtu_adjustment(stcb, net->mtu);
5599 } else if (port == 0) {
5600 /* UDP encapsulation turned off. */
5601 net->mtu += sizeof(struct udphdr);
5602 /* XXX Update smallest_mtu */
5608 net->flowtype = mflowtype;
5609 net->flowid = mflowid;
5612 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5613 SCTP_STAT_INCR(sctps_noport);
5614 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5617 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5618 sctp_send_shutdown_complete2(src, dst, sh,
5619 mflowtype, mflowid, fibnum,
5623 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5626 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5627 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5628 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5629 (ch->chunk_type != SCTP_INIT))) {
5630 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5632 sctp_send_abort(m, iphlen, src, dst,
5634 mflowtype, mflowid, fibnum,
5639 } else if (stcb == NULL) {
5642 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5643 (void *)m, iphlen, offset, length, (void *)stcb);
5645 /* always clear this before beginning a packet */
5646 stcb->asoc.authenticated = 0;
5647 stcb->asoc.seen_a_sack_this_pkt = 0;
5648 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5649 (void *)stcb, stcb->asoc.state);
5651 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5652 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5654 * If we hit here, we had a ref count
5655 * up when the assoc was aborted and the
5656 * timer is clearing out the assoc, we should
5657 * NOT respond to any packet.. its OOTB.
5659 SCTP_TCB_UNLOCK(stcb);
5661 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5662 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5663 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5665 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5666 mflowtype, mflowid, inp->fibnum,
5671 if (IS_SCTP_CONTROL(ch)) {
5672 /* process the control portion of the SCTP packet */
5673 /* sa_ignore NO_NULL_CHK */
5674 stcb = sctp_process_control(m, iphlen, &offset, length,
5676 inp, stcb, &net, &fwd_tsn_seen,
5677 mflowtype, mflowid, fibnum,
5681 * This covers us if the cookie-echo was there and
5682 * it changes our INP.
5684 inp = stcb->sctp_ep;
5685 #if defined(INET) || defined(INET6)
5686 if ((ch->chunk_type != SCTP_INITIATION) &&
5687 (net != NULL) && (net->port != port)) {
5688 if (net->port == 0) {
5689 /* UDP encapsulation turned on. */
5690 net->mtu -= sizeof(struct udphdr);
5691 if (stcb->asoc.smallest_mtu > net->mtu) {
5692 sctp_pathmtu_adjustment(stcb, net->mtu);
5694 } else if (port == 0) {
5695 /* UDP encapsulation turned off. */
5696 net->mtu += sizeof(struct udphdr);
5697 /* XXX Update smallest_mtu */
5705 * no control chunks, so pre-process DATA chunks (these
5706 * checks are taken care of by control processing)
5710 * if DATA only packet, and auth is required, then punt...
5711 * can't have authenticated without any AUTH (control)
5714 if ((stcb != NULL) &&
5715 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5716 /* "silently" ignore */
5717 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5718 SCTP_STAT_INCR(sctps_recvauthmissing);
5722 /* out of the blue DATA chunk */
5723 SCTP_PROBE5(receive, NULL, NULL, m, NULL, sh);
5724 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5725 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5727 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5728 mflowtype, mflowid, fibnum,
5732 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5733 /* v_tag mismatch! */
5734 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5735 SCTP_STAT_INCR(sctps_badvtag);
5740 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5743 * no valid TCB for this packet, or we found it's a bad
5744 * packet while processing control, or we're done with this
5745 * packet (done or skip rest of data), so we drop it...
5751 * DATA chunk processing
5753 /* plow through the data chunks while length > offset */
5756 * Rest should be DATA only. Check authentication state if AUTH for
5759 if ((length > offset) &&
5761 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5762 !stcb->asoc.authenticated) {
5763 /* "silently" ignore */
5764 SCTP_STAT_INCR(sctps_recvauthmissing);
5765 SCTPDBG(SCTP_DEBUG_AUTH1,
5766 "Data chunk requires AUTH, skipped\n");
5769 if (length > offset) {
5773 * First check to make sure our state is correct. We would
5774 * not get here unless we really did have a tag, so we don't
5775 * abort if this happens, just dump the chunk silently.
5777 switch (SCTP_GET_STATE(stcb)) {
5778 case SCTP_STATE_COOKIE_ECHOED:
5780 * we consider data with valid tags in this state
5781 * shows us the cookie-ack was lost. Imply it was
5784 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5786 case SCTP_STATE_COOKIE_WAIT:
5788 * We consider OOTB any data sent during asoc setup.
5790 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5791 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5793 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5794 mflowtype, mflowid, inp->fibnum,
5797 /* sa_ignore NOTREACHED */
5799 case SCTP_STATE_EMPTY: /* should not happen */
5800 case SCTP_STATE_INUSE: /* should not happen */
5801 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5802 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5805 /* sa_ignore NOTREACHED */
5807 case SCTP_STATE_OPEN:
5808 case SCTP_STATE_SHUTDOWN_SENT:
5811 /* plow through the data chunks while length > offset */
5812 retval = sctp_process_data(mm, iphlen, &offset, length,
5813 inp, stcb, net, &high_tsn);
5816 * The association aborted, NO UNLOCK needed since
5817 * the association is destroyed.
5824 * Anything important needs to have been m_copy'ed in
5829 /* take care of ecn */
5830 if ((data_processed == 1) &&
5831 (stcb->asoc.ecn_supported == 1) &&
5832 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5833 /* Yep, we need to add a ECNE */
5834 sctp_send_ecn_echo(stcb, net, high_tsn);
5837 if ((data_processed == 0) && (fwd_tsn_seen)) {
5839 uint32_t highest_tsn;
5841 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5842 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5844 highest_tsn = stcb->asoc.highest_tsn_inside_map;
5846 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
5847 stcb->asoc.send_sack = 1;
5848 sctp_sack_check(stcb, was_a_gap);
5849 } else if (fwd_tsn_seen) {
5850 stcb->asoc.send_sack = 1;
5852 /* trigger send of any chunks in queue... */
5854 #ifdef SCTP_AUDITING_ENABLED
5855 sctp_audit_log(0xE0, 2);
5856 sctp_auditing(1, inp, stcb, net);
5858 SCTPDBG(SCTP_DEBUG_INPUT1,
5859 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5860 stcb->asoc.peers_rwnd,
5861 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5862 stcb->asoc.total_flight);
5863 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5864 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5865 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5867 if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
5869 stcb->asoc.trigger_reset ||
5871 (stcb->asoc.peers_rwnd > 0 ||
5872 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5873 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5874 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5875 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5877 #ifdef SCTP_AUDITING_ENABLED
5878 sctp_audit_log(0xE0, 3);
5879 sctp_auditing(2, inp, stcb, net);
5883 SCTP_TCB_UNLOCK(stcb);
5885 if (inp_decr != NULL) {
5886 /* reduce ref-count */
5887 SCTP_INP_WLOCK(inp_decr);
5888 SCTP_INP_DECR_REF(inp_decr);
5889 SCTP_INP_WUNLOCK(inp_decr);
5896 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5900 uint32_t vrf_id = 0;
5902 struct sockaddr_in src, dst;
5905 struct sctp_chunkhdr *ch;
5907 uint8_t compute_crc;
5913 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
5914 SCTP_RELEASE_PKT(i_pak);
5917 m = SCTP_HEADER_TO_CHAIN(i_pak);
5918 #ifdef SCTP_MBUF_LOGGING
5919 /* Log in any input mbufs */
5920 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5921 sctp_log_mbc(m, SCTP_MBUF_INPUT);
5924 #ifdef SCTP_PACKET_LOGGING
5925 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
5929 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
5930 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
5932 if_name(m->m_pkthdr.rcvif),
5933 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
5934 mflowid = m->m_pkthdr.flowid;
5935 mflowtype = M_HASHTYPE_GET(m);
5936 fibnum = M_GETFIB(m);
5937 SCTP_STAT_INCR(sctps_recvpackets);
5938 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
5939 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
5940 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5941 if (SCTP_BUF_LEN(m) < offset) {
5942 if ((m = m_pullup(m, offset)) == NULL) {
5943 SCTP_STAT_INCR(sctps_hdrops);
5947 ip = mtod(m, struct ip *);
5948 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
5949 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
5950 offset -= sizeof(struct sctp_chunkhdr);
5951 memset(&src, 0, sizeof(struct sockaddr_in));
5952 src.sin_family = AF_INET;
5953 src.sin_len = sizeof(struct sockaddr_in);
5954 src.sin_port = sh->src_port;
5955 src.sin_addr = ip->ip_src;
5956 memset(&dst, 0, sizeof(struct sockaddr_in));
5957 dst.sin_family = AF_INET;
5958 dst.sin_len = sizeof(struct sockaddr_in);
5959 dst.sin_port = sh->dest_port;
5960 dst.sin_addr = ip->ip_dst;
5961 length = ntohs(ip->ip_len);
5962 /* Validate mbuf chain length with IP payload length. */
5963 if (SCTP_HEADER_LEN(m) != length) {
5964 SCTPDBG(SCTP_DEBUG_INPUT1,
5965 "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
5966 SCTP_STAT_INCR(sctps_hdrops);
5969 /* SCTP does not allow broadcasts or multicasts */
5970 if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
5973 if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
5976 ecn_bits = ip->ip_tos;
5977 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5978 SCTP_STAT_INCR(sctps_recvhwcrc);
5981 SCTP_STAT_INCR(sctps_recvswcrc);
5984 sctp_common_input_processing(&m, iphlen, offset, length,
5985 (struct sockaddr *)&src,
5986 (struct sockaddr *)&dst,
5990 mflowtype, mflowid, fibnum,
5999 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6000 extern int *sctp_cpuarry;
6004 sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
6011 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6017 uint32_t flowid, tag;
6019 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
6020 flowid = m->m_pkthdr.flowid;
6023 * No flow id built by lower layers fix it so we
6026 offset = off + sizeof(struct sctphdr);
6027 if (SCTP_BUF_LEN(m) < offset) {
6028 if ((m = m_pullup(m, offset)) == NULL) {
6029 SCTP_STAT_INCR(sctps_hdrops);
6030 return (IPPROTO_DONE);
6033 ip = mtod(m, struct ip *);
6034 sh = (struct sctphdr *)((caddr_t)ip + off);
6035 tag = htonl(sh->v_tag);
6036 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6037 m->m_pkthdr.flowid = flowid;
6038 M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH);
6040 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6041 sctp_queue_to_mcore(m, off, cpu_to_use);
6042 return (IPPROTO_DONE);
6045 sctp_input_with_port(m, off, 0);
6046 return (IPPROTO_DONE);