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;
448 uint32_t initack_limit;
449 int nat_friendly = 0;
451 /* First verify that we have no illegal param's */
454 op_err = sctp_arethere_unrecognized_parameters(m,
455 (offset + sizeof(struct sctp_init_chunk)),
456 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
458 /* Send an abort and notify peer */
459 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
460 *abort_no_unlock = 1;
464 asoc->peer_supports_nat = (uint8_t)nat_friendly;
465 /* process the peer's parameters in the INIT-ACK */
466 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
470 initack_limit = offset + ntohs(cp->ch.chunk_length);
471 /* load all addresses */
472 if ((retval = sctp_load_addresses_from_init(stcb, m,
473 (offset + sizeof(struct sctp_init_chunk)), initack_limit,
474 src, dst, NULL, stcb->asoc.port))) {
475 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
476 "Problem with address parameters");
477 SCTPDBG(SCTP_DEBUG_INPUT1,
478 "Load addresses from INIT causes an abort %d\n",
480 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
481 src, dst, sh, op_err,
484 *abort_no_unlock = 1;
487 /* if the peer doesn't support asconf, flush the asconf queue */
488 if (asoc->asconf_supported == 0) {
489 struct sctp_asconf_addr *param, *nparam;
491 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
492 TAILQ_REMOVE(&asoc->asconf_queue, param, next);
493 SCTP_FREE(param, SCTP_M_ASC_ADDR);
497 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
498 stcb->asoc.local_hmacs);
500 sctp_queue_op_err(stcb, op_err);
501 /* queuing will steal away the mbuf chain to the out queue */
504 /* extract the cookie and queue it to "echo" it back... */
505 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
506 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
507 stcb->asoc.overall_error_count,
509 SCTP_FROM_SCTP_INPUT,
512 stcb->asoc.overall_error_count = 0;
513 net->error_count = 0;
516 * Cancel the INIT timer, We do this first before queueing the
517 * cookie. We always cancel at the primary to assue that we are
518 * canceling the timer started by the INIT which always goes to the
521 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
522 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
524 /* calculate the RTO */
525 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
526 SCTP_RTT_FROM_NON_DATA);
527 retval = sctp_send_cookie_echo(m, offset, stcb, net);
530 * No cookie, we probably should send a op error. But in any
531 * case if there is no cookie in the INIT-ACK, we can
532 * abandon the peer, its broke.
537 len = (uint16_t)(sizeof(struct sctp_error_missing_param) + sizeof(uint16_t));
538 /* We abort with an error of missing mandatory param */
539 op_err = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
540 if (op_err != NULL) {
541 struct sctp_error_missing_param *cause;
543 SCTP_BUF_LEN(op_err) = len;
544 cause = mtod(op_err, struct sctp_error_missing_param *);
545 /* Subtract the reserved param */
546 cause->cause.code = htons(SCTP_CAUSE_MISSING_PARAM);
547 cause->cause.length = htons(len);
548 cause->num_missing_params = htonl(1);
549 cause->type[0] = htons(SCTP_STATE_COOKIE);
551 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
552 src, dst, sh, op_err,
555 *abort_no_unlock = 1;
564 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
565 struct sctp_tcb *stcb, struct sctp_nets *net)
567 union sctp_sockstore store;
568 struct sctp_nets *r_net, *f_net;
571 uint16_t old_error_counter;
573 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
578 memset(&store, 0, sizeof(store));
579 switch (cp->heartbeat.hb_info.addr_family) {
582 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
583 store.sin.sin_family = cp->heartbeat.hb_info.addr_family;
584 store.sin.sin_len = cp->heartbeat.hb_info.addr_len;
585 store.sin.sin_port = stcb->rport;
586 memcpy(&store.sin.sin_addr, cp->heartbeat.hb_info.address,
587 sizeof(store.sin.sin_addr));
595 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
596 store.sin6.sin6_family = cp->heartbeat.hb_info.addr_family;
597 store.sin6.sin6_len = cp->heartbeat.hb_info.addr_len;
598 store.sin6.sin6_port = stcb->rport;
599 memcpy(&store.sin6.sin6_addr, cp->heartbeat.hb_info.address, sizeof(struct in6_addr));
608 r_net = sctp_findnet(stcb, &store.sa);
610 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
613 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
614 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
615 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
617 * If the its a HB and it's random value is correct when can
618 * confirm the destination.
620 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
621 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
622 stcb->asoc.primary_destination = r_net;
623 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
624 f_net = TAILQ_FIRST(&stcb->asoc.nets);
625 if (f_net != r_net) {
627 * first one on the list is NOT the primary
628 * sctp_cmpaddr() is much more efficient if
629 * the primary is the first on the list,
632 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
633 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
637 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
638 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
639 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
640 r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
641 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
643 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
644 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
645 stcb->asoc.overall_error_count,
647 SCTP_FROM_SCTP_INPUT,
650 stcb->asoc.overall_error_count = 0;
651 old_error_counter = r_net->error_count;
652 r_net->error_count = 0;
653 r_net->hb_responded = 1;
654 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
655 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
656 /* Now lets do a RTO with this */
657 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv,
658 SCTP_RTT_FROM_NON_DATA);
659 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
660 r_net->dest_state |= SCTP_ADDR_REACHABLE;
661 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
662 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
664 if (r_net->dest_state & SCTP_ADDR_PF) {
665 r_net->dest_state &= ~SCTP_ADDR_PF;
666 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
668 if (old_error_counter > 0) {
669 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
670 stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
671 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
673 if (r_net == stcb->asoc.primary_destination) {
674 if (stcb->asoc.alternate) {
675 /* release the alternate, primary is good */
676 sctp_free_remote_addr(stcb->asoc.alternate);
677 stcb->asoc.alternate = NULL;
680 /* Mobility adaptation */
682 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
683 SCTP_MOBILITY_BASE) ||
684 sctp_is_mobility_feature_on(stcb->sctp_ep,
685 SCTP_MOBILITY_FASTHANDOFF)) &&
686 sctp_is_mobility_feature_on(stcb->sctp_ep,
687 SCTP_MOBILITY_PRIM_DELETED)) {
689 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
690 stcb->sctp_ep, stcb, NULL,
691 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
692 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
693 SCTP_MOBILITY_FASTHANDOFF)) {
694 sctp_assoc_immediate_retrans(stcb,
695 stcb->asoc.primary_destination);
697 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
698 SCTP_MOBILITY_BASE)) {
699 sctp_move_chunks_from_net(stcb,
700 stcb->asoc.deleted_primary);
702 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
703 stcb->asoc.deleted_primary);
709 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
712 * return 0 means we want you to proceed with the abort non-zero
713 * means no abort processing
715 struct sctpasochead *head;
717 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
718 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
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);
725 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
726 /* generate a new vtag and send init */
727 LIST_REMOVE(stcb, sctp_asocs);
728 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
729 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
731 * put it in the bucket in the vtag hash of assoc's for the
734 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
735 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
736 SCTP_INP_INFO_WUNLOCK();
739 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) {
741 * treat like a case where the cookie expired i.e.: - dump
742 * current cookie. - generate a new vtag. - resend init.
744 /* generate a new vtag and send init */
745 LIST_REMOVE(stcb, sctp_asocs);
746 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
747 sctp_stop_all_cookie_timers(stcb);
748 sctp_toss_old_cookies(stcb, &stcb->asoc);
749 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
750 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
752 * put it in the bucket in the vtag hash of assoc's for the
755 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
756 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
757 SCTP_INP_INFO_WUNLOCK();
764 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
765 struct sctp_nets *net)
768 * return 0 means we want you to proceed with the abort non-zero
769 * means no abort processing
771 if (stcb->asoc.auth_supported == 0) {
772 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
775 sctp_asconf_send_nat_state_update(stcb, net);
780 /* Returns 1 if the stcb was aborted, 0 otherwise */
782 sctp_handle_abort(struct sctp_abort_chunk *abort,
783 struct sctp_tcb *stcb, struct sctp_nets *net)
785 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
791 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
795 len = ntohs(abort->ch.chunk_length);
796 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_error_cause)) {
798 * Need to check the cause codes for our two magic nat
799 * aborts which don't kill the assoc necessarily.
801 struct sctp_error_cause *cause;
803 cause = (struct sctp_error_cause *)(abort + 1);
804 error = ntohs(cause->code);
805 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
806 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
807 abort->ch.chunk_flags);
808 if (sctp_handle_nat_colliding_state(stcb)) {
811 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
812 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
813 abort->ch.chunk_flags);
814 if (sctp_handle_nat_missing_state(stcb, net)) {
821 /* stop any receive timers */
822 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net,
823 SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
824 /* notify user of the abort and clean up... */
825 sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
827 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
828 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
829 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
830 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
832 #ifdef SCTP_ASOCLOG_OF_TSNS
833 sctp_print_out_track_log(stcb);
835 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
836 so = SCTP_INP_SO(stcb->sctp_ep);
837 atomic_add_int(&stcb->asoc.refcnt, 1);
838 SCTP_TCB_UNLOCK(stcb);
839 SCTP_SOCKET_LOCK(so, 1);
841 atomic_subtract_int(&stcb->asoc.refcnt, 1);
843 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
844 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
845 SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
846 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
847 SCTP_SOCKET_UNLOCK(so, 1);
849 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
854 sctp_start_net_timers(struct sctp_tcb *stcb)
856 uint32_t cnt_hb_sent;
857 struct sctp_nets *net;
860 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
862 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
863 * If the dest in unconfirmed send a hb as well if under
864 * max_hb_burst have been sent.
866 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
867 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
868 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
869 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
870 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
875 sctp_chunk_output(stcb->sctp_ep, stcb,
876 SCTP_OUTPUT_FROM_COOKIE_ACK,
883 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
884 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
886 struct sctp_association *asoc;
887 int some_on_streamwheel;
889 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
893 SCTPDBG(SCTP_DEBUG_INPUT2,
894 "sctp_handle_shutdown: handling SHUTDOWN\n");
898 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
899 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
902 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
903 /* Shutdown NOT the expected size */
906 old_state = SCTP_GET_STATE(stcb);
907 sctp_update_acked(stcb, cp, abort_flag);
911 if (asoc->control_pdapi) {
913 * With a normal shutdown we assume the end of last record.
915 SCTP_INP_READ_LOCK(stcb->sctp_ep);
916 if (asoc->control_pdapi->on_strm_q) {
917 struct sctp_stream_in *strm;
919 strm = &asoc->strmin[asoc->control_pdapi->sinfo_stream];
920 if (asoc->control_pdapi->on_strm_q == SCTP_ON_UNORDERED) {
922 TAILQ_REMOVE(&strm->uno_inqueue, asoc->control_pdapi, next_instrm);
923 asoc->control_pdapi->on_strm_q = 0;
924 } else if (asoc->control_pdapi->on_strm_q == SCTP_ON_ORDERED) {
926 TAILQ_REMOVE(&strm->inqueue, asoc->control_pdapi, next_instrm);
927 asoc->control_pdapi->on_strm_q = 0;
930 panic("Unknown state on ctrl:%p on_strm_q:%d",
932 asoc->control_pdapi->on_strm_q);
936 asoc->control_pdapi->end_added = 1;
937 asoc->control_pdapi->pdapi_aborted = 1;
938 asoc->control_pdapi = NULL;
939 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
940 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
941 so = SCTP_INP_SO(stcb->sctp_ep);
942 atomic_add_int(&stcb->asoc.refcnt, 1);
943 SCTP_TCB_UNLOCK(stcb);
944 SCTP_SOCKET_LOCK(so, 1);
946 atomic_subtract_int(&stcb->asoc.refcnt, 1);
947 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
948 /* assoc was freed while we were unlocked */
949 SCTP_SOCKET_UNLOCK(so, 1);
953 if (stcb->sctp_socket) {
954 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
956 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
957 SCTP_SOCKET_UNLOCK(so, 1);
960 /* goto SHUTDOWN_RECEIVED state to block new requests */
961 if (stcb->sctp_socket) {
962 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
963 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
964 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) {
965 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_RECEIVED);
967 * notify upper layer that peer has initiated a
970 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
973 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
976 if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) {
978 * stop the shutdown timer, since we WILL move to
981 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
982 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
984 /* Now is there unsent data on a stream somewhere? */
985 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
987 if (!TAILQ_EMPTY(&asoc->send_queue) ||
988 !TAILQ_EMPTY(&asoc->sent_queue) ||
989 some_on_streamwheel) {
990 /* By returning we will push more data out */
993 /* no outstanding data to send, so move on... */
994 /* send SHUTDOWN-ACK */
995 /* move to SHUTDOWN-ACK-SENT state */
996 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
997 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
998 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1000 if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
1001 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_ACK_SENT);
1002 sctp_stop_timers_for_shutdown(stcb);
1003 sctp_send_shutdown_ack(stcb, net);
1004 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
1005 stcb->sctp_ep, stcb, net);
1006 } else if (old_state == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1007 sctp_send_shutdown_ack(stcb, net);
1013 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
1014 struct sctp_tcb *stcb,
1015 struct sctp_nets *net)
1017 struct sctp_association *asoc;
1018 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1021 so = SCTP_INP_SO(stcb->sctp_ep);
1023 SCTPDBG(SCTP_DEBUG_INPUT2,
1024 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
1029 /* process according to association state */
1030 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
1031 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
1032 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
1033 sctp_send_shutdown_complete(stcb, net, 1);
1034 SCTP_TCB_UNLOCK(stcb);
1037 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
1038 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1039 /* unexpected SHUTDOWN-ACK... so ignore... */
1040 SCTP_TCB_UNLOCK(stcb);
1043 if (asoc->control_pdapi) {
1045 * With a normal shutdown we assume the end of last record.
1047 SCTP_INP_READ_LOCK(stcb->sctp_ep);
1048 asoc->control_pdapi->end_added = 1;
1049 asoc->control_pdapi->pdapi_aborted = 1;
1050 asoc->control_pdapi = NULL;
1051 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1052 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1053 atomic_add_int(&stcb->asoc.refcnt, 1);
1054 SCTP_TCB_UNLOCK(stcb);
1055 SCTP_SOCKET_LOCK(so, 1);
1056 SCTP_TCB_LOCK(stcb);
1057 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1058 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1059 /* assoc was freed while we were unlocked */
1060 SCTP_SOCKET_UNLOCK(so, 1);
1064 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1065 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1066 SCTP_SOCKET_UNLOCK(so, 1);
1070 if (!TAILQ_EMPTY(&asoc->send_queue) ||
1071 !TAILQ_EMPTY(&asoc->sent_queue) ||
1072 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
1073 panic("Queues are not empty when handling SHUTDOWN-ACK");
1076 /* stop the timer */
1077 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net,
1078 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1079 /* send SHUTDOWN-COMPLETE */
1080 sctp_send_shutdown_complete(stcb, net, 0);
1081 /* notify upper layer protocol */
1082 if (stcb->sctp_socket) {
1083 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1084 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1085 stcb->sctp_socket->so_snd.sb_cc = 0;
1087 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1089 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1090 /* free the TCB but first save off the ep */
1091 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1092 atomic_add_int(&stcb->asoc.refcnt, 1);
1093 SCTP_TCB_UNLOCK(stcb);
1094 SCTP_SOCKET_LOCK(so, 1);
1095 SCTP_TCB_LOCK(stcb);
1096 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1098 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1099 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1100 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1101 SCTP_SOCKET_UNLOCK(so, 1);
1106 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, uint8_t chunk_type,
1107 struct sctp_nets *net)
1109 switch (chunk_type) {
1110 case SCTP_ASCONF_ACK:
1112 sctp_asconf_cleanup(stcb, net);
1114 case SCTP_IFORWARD_CUM_TSN:
1115 case SCTP_FORWARD_CUM_TSN:
1116 stcb->asoc.prsctp_supported = 0;
1119 SCTPDBG(SCTP_DEBUG_INPUT2,
1120 "Peer does not support chunk type %d (0x%x).\n",
1121 chunk_type, chunk_type);
1127 * Skip past the param header and then we will find the param that caused the
1128 * problem. There are a number of param's in a ASCONF OR the prsctp param
1129 * these will turn of specific features.
1130 * XXX: Is this the right thing to do?
1133 sctp_process_unrecog_param(struct sctp_tcb *stcb, uint16_t parameter_type)
1135 switch (parameter_type) {
1137 case SCTP_PRSCTP_SUPPORTED:
1138 stcb->asoc.prsctp_supported = 0;
1140 case SCTP_SUPPORTED_CHUNK_EXT:
1142 /* draft-ietf-tsvwg-addip-sctp */
1143 case SCTP_HAS_NAT_SUPPORT:
1144 stcb->asoc.peer_supports_nat = 0;
1146 case SCTP_ADD_IP_ADDRESS:
1147 case SCTP_DEL_IP_ADDRESS:
1148 case SCTP_SET_PRIM_ADDR:
1149 stcb->asoc.asconf_supported = 0;
1151 case SCTP_SUCCESS_REPORT:
1152 case SCTP_ERROR_CAUSE_IND:
1153 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1154 SCTPDBG(SCTP_DEBUG_INPUT2,
1155 "Turning off ASCONF to this strange peer\n");
1156 stcb->asoc.asconf_supported = 0;
1159 SCTPDBG(SCTP_DEBUG_INPUT2,
1160 "Peer does not support param type %d (0x%x)??\n",
1161 parameter_type, parameter_type);
1167 sctp_handle_error(struct sctp_chunkhdr *ch,
1168 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
1170 struct sctp_error_cause *cause;
1171 struct sctp_association *asoc;
1172 uint32_t remaining_length, adjust;
1173 uint16_t code, cause_code, cause_length;
1174 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1178 /* parse through all of the errors and process */
1180 cause = (struct sctp_error_cause *)((caddr_t)ch +
1181 sizeof(struct sctp_chunkhdr));
1182 remaining_length = ntohs(ch->chunk_length);
1183 if (remaining_length > limit) {
1184 remaining_length = limit;
1186 if (remaining_length >= sizeof(struct sctp_chunkhdr)) {
1187 remaining_length -= sizeof(struct sctp_chunkhdr);
1189 remaining_length = 0;
1192 while (remaining_length >= sizeof(struct sctp_error_cause)) {
1193 /* Process an Error Cause */
1194 cause_code = ntohs(cause->code);
1195 cause_length = ntohs(cause->length);
1196 if ((cause_length > remaining_length) || (cause_length == 0)) {
1197 /* Invalid cause length, possibly due to truncation. */
1198 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in cause - bytes left: %u cause length: %u\n",
1199 remaining_length, cause_length);
1203 /* report the first error cause */
1206 switch (cause_code) {
1207 case SCTP_CAUSE_INVALID_STREAM:
1208 case SCTP_CAUSE_MISSING_PARAM:
1209 case SCTP_CAUSE_INVALID_PARAM:
1210 case SCTP_CAUSE_NO_USER_DATA:
1211 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %u back? We have a bug :/ (or do they?)\n",
1214 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1215 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags: %x\n",
1217 if (sctp_handle_nat_colliding_state(stcb)) {
1221 case SCTP_CAUSE_NAT_MISSING_STATE:
1222 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags: %x\n",
1224 if (sctp_handle_nat_missing_state(stcb, net)) {
1228 case SCTP_CAUSE_STALE_COOKIE:
1230 * We only act if we have echoed a cookie and are
1233 if ((cause_length >= sizeof(struct sctp_error_stale_cookie)) &&
1234 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
1235 struct sctp_error_stale_cookie *stale_cookie;
1237 stale_cookie = (struct sctp_error_stale_cookie *)cause;
1238 asoc->cookie_preserve_req = ntohl(stale_cookie->stale_time);
1239 /* Double it to be more robust on RTX */
1240 if (asoc->cookie_preserve_req <= UINT32_MAX / 2) {
1241 asoc->cookie_preserve_req *= 2;
1243 asoc->cookie_preserve_req = UINT32_MAX;
1245 asoc->stale_cookie_count++;
1246 if (asoc->stale_cookie_count >
1247 asoc->max_init_times) {
1248 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1249 /* now free the asoc */
1250 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1251 so = SCTP_INP_SO(stcb->sctp_ep);
1252 atomic_add_int(&stcb->asoc.refcnt, 1);
1253 SCTP_TCB_UNLOCK(stcb);
1254 SCTP_SOCKET_LOCK(so, 1);
1255 SCTP_TCB_LOCK(stcb);
1256 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1258 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1259 SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1260 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1261 SCTP_SOCKET_UNLOCK(so, 1);
1265 /* blast back to INIT state */
1266 sctp_toss_old_cookies(stcb, &stcb->asoc);
1267 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
1268 sctp_stop_all_cookie_timers(stcb);
1269 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1272 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1274 * Nothing we can do here, we don't do hostname
1275 * addresses so if the peer does not like my IPv6
1276 * (or IPv4 for that matter) it does not matter. If
1277 * they don't support that type of address, they can
1278 * NOT possibly get that packet type... i.e. with no
1279 * IPv6 you can't receive a IPv6 packet. so we can
1280 * safely ignore this one. If we ever added support
1281 * for HOSTNAME Addresses, then we would need to do
1285 case SCTP_CAUSE_UNRECOG_CHUNK:
1286 if (cause_length >= sizeof(struct sctp_error_unrecognized_chunk)) {
1287 struct sctp_error_unrecognized_chunk *unrec_chunk;
1289 unrec_chunk = (struct sctp_error_unrecognized_chunk *)cause;
1290 sctp_process_unrecog_chunk(stcb, unrec_chunk->ch.chunk_type, net);
1293 case SCTP_CAUSE_UNRECOG_PARAM:
1294 /* XXX: We only consider the first parameter */
1295 if (cause_length >= sizeof(struct sctp_error_cause) + sizeof(struct sctp_paramhdr)) {
1296 struct sctp_paramhdr *unrec_parameter;
1298 unrec_parameter = (struct sctp_paramhdr *)(cause + 1);
1299 sctp_process_unrecog_param(stcb, ntohs(unrec_parameter->param_type));
1302 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1304 * We ignore this since the timer will drive out a
1305 * new cookie anyway and there timer will drive us
1306 * to send a SHUTDOWN_COMPLETE. We can't send one
1307 * here since we don't have their tag.
1310 case SCTP_CAUSE_DELETING_LAST_ADDR:
1311 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1312 case SCTP_CAUSE_DELETING_SRC_ADDR:
1314 * We should NOT get these here, but in a
1317 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a error cause with code %u.\n",
1320 case SCTP_CAUSE_OUT_OF_RESC:
1322 * And what, pray tell do we do with the fact that
1323 * the peer is out of resources? Not really sure we
1324 * could do anything but abort. I suspect this
1325 * should have came WITH an abort instead of in a
1330 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown code 0x%x\n",
1334 adjust = SCTP_SIZE32(cause_length);
1335 if (remaining_length >= adjust) {
1336 remaining_length -= adjust;
1338 remaining_length = 0;
1340 cause = (struct sctp_error_cause *)((caddr_t)cause + adjust);
1342 sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, code, ch, SCTP_SO_NOT_LOCKED);
1347 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1348 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1349 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1350 struct sctp_nets *net, int *abort_no_unlock,
1351 uint8_t mflowtype, uint32_t mflowid,
1354 struct sctp_init_ack *init_ack;
1355 struct mbuf *op_err;
1357 SCTPDBG(SCTP_DEBUG_INPUT2,
1358 "sctp_handle_init_ack: handling INIT-ACK\n");
1361 SCTPDBG(SCTP_DEBUG_INPUT2,
1362 "sctp_handle_init_ack: TCB is null\n");
1365 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1366 /* Invalid length */
1367 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1368 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1369 src, dst, sh, op_err,
1372 *abort_no_unlock = 1;
1375 init_ack = &cp->init;
1376 /* validate parameters */
1377 if (init_ack->initiate_tag == 0) {
1378 /* protocol error... send an abort */
1379 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1380 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1381 src, dst, sh, op_err,
1384 *abort_no_unlock = 1;
1387 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1388 /* protocol error... send an abort */
1389 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1390 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1391 src, dst, sh, op_err,
1394 *abort_no_unlock = 1;
1397 if (init_ack->num_inbound_streams == 0) {
1398 /* protocol error... send an abort */
1399 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1400 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1401 src, dst, sh, op_err,
1404 *abort_no_unlock = 1;
1407 if (init_ack->num_outbound_streams == 0) {
1408 /* protocol error... send an abort */
1409 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1410 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1411 src, dst, sh, op_err,
1414 *abort_no_unlock = 1;
1417 /* process according to association state... */
1418 switch (SCTP_GET_STATE(stcb)) {
1419 case SCTP_STATE_COOKIE_WAIT:
1420 /* this is the expected state for this chunk */
1421 /* process the INIT-ACK parameters */
1422 if (stcb->asoc.primary_destination->dest_state &
1423 SCTP_ADDR_UNCONFIRMED) {
1425 * The primary is where we sent the INIT, we can
1426 * always consider it confirmed when the INIT-ACK is
1427 * returned. Do this before we load addresses
1430 stcb->asoc.primary_destination->dest_state &=
1431 ~SCTP_ADDR_UNCONFIRMED;
1432 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1433 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1435 if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1436 net, abort_no_unlock,
1439 /* error in parsing parameters */
1442 /* update our state */
1443 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1444 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_ECHOED);
1446 /* reset the RTO calc */
1447 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1448 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1449 stcb->asoc.overall_error_count,
1451 SCTP_FROM_SCTP_INPUT,
1454 stcb->asoc.overall_error_count = 0;
1455 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1457 * collapse the init timer back in case of a exponential
1460 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1463 * the send at the end of the inbound data processing will
1464 * cause the cookie to be sent
1467 case SCTP_STATE_SHUTDOWN_SENT:
1468 /* incorrect state... discard */
1470 case SCTP_STATE_COOKIE_ECHOED:
1471 /* incorrect state... discard */
1473 case SCTP_STATE_OPEN:
1474 /* incorrect state... discard */
1476 case SCTP_STATE_EMPTY:
1477 case SCTP_STATE_INUSE:
1479 /* incorrect state... discard */
1483 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1487 static struct sctp_tcb *
1488 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1489 struct sockaddr *src, struct sockaddr *dst,
1490 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1491 struct sctp_inpcb *inp, struct sctp_nets **netp,
1492 struct sockaddr *init_src, int *notification,
1493 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1494 uint8_t mflowtype, uint32_t mflowid,
1495 uint32_t vrf_id, uint16_t port);
1499 * handle a state cookie for an existing association m: input packet mbuf
1500 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1501 * "split" mbuf and the cookie signature does not exist offset: offset into
1502 * mbuf to the cookie-echo chunk
1504 static struct sctp_tcb *
1505 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1506 struct sockaddr *src, struct sockaddr *dst,
1507 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1508 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1509 struct sockaddr *init_src, int *notification,
1510 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1511 uint8_t mflowtype, uint32_t mflowid,
1512 uint32_t vrf_id, uint16_t port)
1514 struct sctp_association *asoc;
1515 struct sctp_init_chunk *init_cp, init_buf;
1516 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1517 struct sctp_nets *net;
1518 struct mbuf *op_err;
1520 int init_offset, initack_offset, i;
1524 #if defined(SCTP_DETAILED_STR_STATS)
1529 /* I know that the TCB is non-NULL from the caller */
1531 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1532 if (asoc->cookie_how[how_indx] == 0)
1535 if (how_indx < sizeof(asoc->cookie_how)) {
1536 asoc->cookie_how[how_indx] = 1;
1538 if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1539 /* SHUTDOWN came in after sending INIT-ACK */
1540 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1541 op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
1542 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1543 mflowtype, mflowid, inp->fibnum,
1545 if (how_indx < sizeof(asoc->cookie_how))
1546 asoc->cookie_how[how_indx] = 2;
1550 * find and validate the INIT chunk in the cookie (peer's info) the
1551 * INIT should start after the cookie-echo header struct (chunk
1552 * header, state cookie header struct)
1554 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1556 init_cp = (struct sctp_init_chunk *)
1557 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1558 (uint8_t *)&init_buf);
1559 if (init_cp == NULL) {
1560 /* could not pull a INIT chunk in cookie */
1563 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1567 * find and validate the INIT-ACK chunk in the cookie (my info) the
1568 * INIT-ACK follows the INIT chunk
1570 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1571 initack_cp = (struct sctp_init_ack_chunk *)
1572 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1573 (uint8_t *)&initack_buf);
1574 if (initack_cp == NULL) {
1575 /* could not pull INIT-ACK chunk in cookie */
1578 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1581 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1582 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1584 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1585 * to get into the OPEN state
1587 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1589 * Opps, this means that we somehow generated two vtag's
1590 * the same. I.e. we did:
1592 * <---INIT(tag=a)------
1593 * ----INIT-ACK(tag=t)-->
1594 * ----INIT(tag=t)------> *1
1595 * <---INIT-ACK(tag=a)---
1596 * <----CE(tag=t)------------- *2
1598 * At point *1 we should be generating a different
1599 * tag t'. Which means we would throw away the CE and send
1600 * ours instead. Basically this is case C (throw away side).
1602 if (how_indx < sizeof(asoc->cookie_how))
1603 asoc->cookie_how[how_indx] = 17;
1607 switch (SCTP_GET_STATE(stcb)) {
1608 case SCTP_STATE_COOKIE_WAIT:
1609 case SCTP_STATE_COOKIE_ECHOED:
1611 * INIT was sent but got a COOKIE_ECHO with the
1612 * correct tags... just accept it...but we must
1613 * process the init so that we can make sure we have
1614 * the right seq no's.
1616 /* First we must process the INIT !! */
1617 retval = sctp_process_init(init_cp, stcb);
1619 if (how_indx < sizeof(asoc->cookie_how))
1620 asoc->cookie_how[how_indx] = 3;
1623 /* we have already processed the INIT so no problem */
1624 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp,
1626 SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1627 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp,
1629 SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1630 /* update current state */
1631 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)
1632 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1634 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1636 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1637 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1638 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1639 stcb->sctp_ep, stcb, asoc->primary_destination);
1641 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1642 sctp_stop_all_cookie_timers(stcb);
1643 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1644 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1645 (!SCTP_IS_LISTENING(inp))) {
1646 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1650 * Here is where collision would go if we
1651 * did a connect() and instead got a
1652 * init/init-ack/cookie done before the
1653 * init-ack came back..
1655 stcb->sctp_ep->sctp_flags |=
1656 SCTP_PCB_FLAGS_CONNECTED;
1657 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1658 so = SCTP_INP_SO(stcb->sctp_ep);
1659 atomic_add_int(&stcb->asoc.refcnt, 1);
1660 SCTP_TCB_UNLOCK(stcb);
1661 SCTP_SOCKET_LOCK(so, 1);
1662 SCTP_TCB_LOCK(stcb);
1663 atomic_add_int(&stcb->asoc.refcnt, -1);
1664 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1665 SCTP_SOCKET_UNLOCK(so, 1);
1669 soisconnected(stcb->sctp_socket);
1670 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1671 SCTP_SOCKET_UNLOCK(so, 1);
1674 /* notify upper layer */
1675 *notification = SCTP_NOTIFY_ASSOC_UP;
1677 * since we did not send a HB make sure we don't
1680 old.tv_sec = cookie->time_entered.tv_sec;
1681 old.tv_usec = cookie->time_entered.tv_usec;
1682 net->hb_responded = 1;
1683 net->RTO = sctp_calculate_rto(stcb, asoc, net,
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 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2409 &old, SCTP_RTT_FROM_NON_DATA);
2411 /* respond with a COOKIE-ACK */
2412 sctp_send_cookie_ack(stcb);
2415 * check the address lists for any ASCONFs that need to be sent
2416 * AFTER the cookie-ack is sent
2418 sctp_check_address_list(stcb, m,
2419 initack_offset + sizeof(struct sctp_init_ack_chunk),
2420 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2421 &store.sa, cookie->local_scope, cookie->site_scope,
2422 cookie->ipv4_scope, cookie->loopback_scope);
2429 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2430 * we NEED to make sure we are not already using the vtag. If so we
2431 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2432 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2433 SCTP_BASE_INFO(hashasocmark))];
2434 LIST_FOREACH(stcb, head, sctp_asocs) {
2435 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2436 -- SEND ABORT - TRY AGAIN --
2442 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2443 * existing (non-NULL) TCB
2445 static struct mbuf *
2446 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2447 struct sockaddr *src, struct sockaddr *dst,
2448 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2449 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2450 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2451 struct sctp_tcb **locked_tcb,
2452 uint8_t mflowtype, uint32_t mflowid,
2453 uint32_t vrf_id, uint16_t port)
2455 struct sctp_state_cookie *cookie;
2456 struct sctp_tcb *l_stcb = *stcb;
2457 struct sctp_inpcb *l_inp;
2458 struct sockaddr *to;
2459 struct sctp_pcb *ep;
2461 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2463 uint8_t cookie_ok = 0;
2464 unsigned int sig_offset, cookie_offset;
2465 unsigned int cookie_len;
2467 struct timeval time_expires;
2468 int notification = 0;
2469 struct sctp_nets *netl;
2470 int had_a_existing_tcb = 0;
2471 int send_int_conf = 0;
2473 struct sockaddr_in sin;
2476 struct sockaddr_in6 sin6;
2479 SCTPDBG(SCTP_DEBUG_INPUT2,
2480 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2482 if (inp_p == NULL) {
2485 cookie = &cp->cookie;
2486 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2487 cookie_len = ntohs(cp->ch.chunk_length);
2489 if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2490 sizeof(struct sctp_init_chunk) +
2491 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2492 /* cookie too small */
2495 if ((cookie->peerport != sh->src_port) ||
2496 (cookie->myport != sh->dest_port) ||
2497 (cookie->my_vtag != sh->v_tag)) {
2499 * invalid ports or bad tag. Note that we always leave the
2500 * v_tag in the header in network order and when we stored
2501 * it in the my_vtag slot we also left it in network order.
2502 * This maintains the match even though it may be in the
2503 * opposite byte order of the machine :->
2508 * split off the signature into its own mbuf (since it should not be
2509 * calculated in the sctp_hmac_m() call).
2511 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2512 m_sig = m_split(m, sig_offset, M_NOWAIT);
2513 if (m_sig == NULL) {
2514 /* out of memory or ?? */
2517 #ifdef SCTP_MBUF_LOGGING
2518 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2519 sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
2524 * compute the signature/digest for the cookie
2526 ep = &(*inp_p)->sctp_ep;
2529 SCTP_TCB_UNLOCK(l_stcb);
2531 SCTP_INP_RLOCK(l_inp);
2533 SCTP_TCB_LOCK(l_stcb);
2535 /* which cookie is it? */
2536 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2537 (ep->current_secret_number != ep->last_secret_number)) {
2538 /* it's the old cookie */
2539 (void)sctp_hmac_m(SCTP_HMAC,
2540 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2541 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2543 /* it's the current cookie */
2544 (void)sctp_hmac_m(SCTP_HMAC,
2545 (uint8_t *)ep->secret_key[(int)ep->current_secret_number],
2546 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2548 /* get the signature */
2549 SCTP_INP_RUNLOCK(l_inp);
2550 sig = (uint8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *)&tmp_sig);
2552 /* couldn't find signature */
2553 sctp_m_freem(m_sig);
2556 /* compare the received digest with the computed digest */
2557 if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2558 /* try the old cookie? */
2559 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2560 (ep->current_secret_number != ep->last_secret_number)) {
2561 /* compute digest with old */
2562 (void)sctp_hmac_m(SCTP_HMAC,
2563 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2564 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2566 if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2574 * Now before we continue we must reconstruct our mbuf so that
2575 * normal processing of any other chunks will work.
2581 while (SCTP_BUF_NEXT(m_at) != NULL) {
2582 m_at = SCTP_BUF_NEXT(m_at);
2584 SCTP_BUF_NEXT(m_at) = m_sig;
2587 if (cookie_ok == 0) {
2588 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2589 SCTPDBG(SCTP_DEBUG_INPUT2,
2590 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2591 (uint32_t)offset, cookie_offset, sig_offset);
2596 * check the cookie timestamps to be sure it's not stale
2598 (void)SCTP_GETTIME_TIMEVAL(&now);
2599 /* Expire time is in Ticks, so we convert to seconds */
2600 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2601 time_expires.tv_usec = cookie->time_entered.tv_usec;
2602 if (timevalcmp(&now, &time_expires, >)) {
2603 /* cookie is stale! */
2604 struct mbuf *op_err;
2605 struct sctp_error_stale_cookie *cause;
2606 struct timeval diff;
2609 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2610 0, M_NOWAIT, 1, MT_DATA);
2611 if (op_err == NULL) {
2616 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
2617 cause = mtod(op_err, struct sctp_error_stale_cookie *);
2618 cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
2619 cause->cause.length = htons((sizeof(struct sctp_paramhdr) +
2620 (sizeof(uint32_t))));
2622 timevalsub(&diff, &time_expires);
2623 if ((uint32_t)diff.tv_sec > UINT32_MAX / 1000000) {
2624 staleness = UINT32_MAX;
2626 staleness = diff.tv_sec * 1000000;
2628 if (UINT32_MAX - staleness >= (uint32_t)diff.tv_usec) {
2629 staleness += diff.tv_usec;
2631 staleness = UINT32_MAX;
2633 cause->stale_time = htonl(staleness);
2634 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2635 mflowtype, mflowid, l_inp->fibnum,
2640 * Now we must see with the lookup address if we have an existing
2641 * asoc. This will only happen if we were in the COOKIE-WAIT state
2642 * and a INIT collided with us and somewhere the peer sent the
2643 * cookie on another address besides the single address our assoc
2644 * had for him. In this case we will have one of the tie-tags set at
2645 * least AND the address field in the cookie can be used to look it
2649 switch (cookie->addr_type) {
2651 case SCTP_IPV6_ADDRESS:
2652 memset(&sin6, 0, sizeof(sin6));
2653 sin6.sin6_family = AF_INET6;
2654 sin6.sin6_len = sizeof(sin6);
2655 sin6.sin6_port = sh->src_port;
2656 sin6.sin6_scope_id = cookie->scope_id;
2657 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2658 sizeof(sin6.sin6_addr.s6_addr));
2659 to = (struct sockaddr *)&sin6;
2663 case SCTP_IPV4_ADDRESS:
2664 memset(&sin, 0, sizeof(sin));
2665 sin.sin_family = AF_INET;
2666 sin.sin_len = sizeof(sin);
2667 sin.sin_port = sh->src_port;
2668 sin.sin_addr.s_addr = cookie->address[0];
2669 to = (struct sockaddr *)&sin;
2673 /* This should not happen */
2676 if (*stcb == NULL) {
2677 /* Yep, lets check */
2678 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2679 if (*stcb == NULL) {
2681 * We should have only got back the same inp. If we
2682 * got back a different ep we have a problem. The
2683 * original findep got back l_inp and now
2685 if (l_inp != *inp_p) {
2686 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2689 if (*locked_tcb == NULL) {
2691 * In this case we found the assoc only
2692 * after we locked the create lock. This
2693 * means we are in a colliding case and we
2694 * must make sure that we unlock the tcb if
2695 * its one of the cases where we throw away
2696 * the incoming packets.
2698 *locked_tcb = *stcb;
2701 * We must also increment the inp ref count
2702 * since the ref_count flags was set when we
2703 * did not find the TCB, now we found it
2704 * which reduces the refcount.. we must
2705 * raise it back out to balance it all :-)
2707 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2708 if ((*stcb)->sctp_ep != l_inp) {
2709 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2710 (void *)(*stcb)->sctp_ep, (void *)l_inp);
2716 cookie_len -= SCTP_SIGNATURE_SIZE;
2717 if (*stcb == NULL) {
2718 /* this is the "normal" case... get a new TCB */
2719 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2720 cookie, cookie_len, *inp_p,
2721 netp, to, ¬ification,
2722 auth_skipped, auth_offset, auth_len,
2726 /* this is abnormal... cookie-echo on existing TCB */
2727 had_a_existing_tcb = 1;
2728 *stcb = sctp_process_cookie_existing(m, iphlen, offset,
2730 cookie, cookie_len, *inp_p, *stcb, netp, to,
2731 ¬ification, auth_skipped, auth_offset, auth_len,
2736 if (*stcb == NULL) {
2737 /* still no TCB... must be bad cookie-echo */
2740 if (*netp != NULL) {
2741 (*netp)->flowtype = mflowtype;
2742 (*netp)->flowid = mflowid;
2745 * Ok, we built an association so confirm the address we sent the
2748 netl = sctp_findnet(*stcb, to);
2750 * This code should in theory NOT run but
2753 /* TSNH! Huh, why do I need to add this address here? */
2754 if (sctp_add_remote_addr(*stcb, to, NULL, port,
2755 SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2758 netl = sctp_findnet(*stcb, to);
2761 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2762 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2763 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2768 sctp_start_net_timers(*stcb);
2769 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2770 if (!had_a_existing_tcb ||
2771 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2773 * If we have a NEW cookie or the connect never
2774 * reached the connected state during collision we
2775 * must do the TCP accept thing.
2777 struct socket *so, *oso;
2778 struct sctp_inpcb *inp;
2780 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2782 * For a restart we will keep the same
2783 * socket, no need to do anything. I THINK!!
2785 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2786 if (send_int_conf) {
2787 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2788 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2792 oso = (*inp_p)->sctp_socket;
2793 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2794 SCTP_TCB_UNLOCK((*stcb));
2795 CURVNET_SET(oso->so_vnet);
2796 so = sonewconn(oso, 0
2799 SCTP_TCB_LOCK((*stcb));
2800 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2803 struct mbuf *op_err;
2804 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2805 struct socket *pcb_so;
2807 /* Too many sockets */
2808 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2809 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2810 sctp_abort_association(*inp_p, NULL, m, iphlen,
2811 src, dst, sh, op_err,
2814 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2815 pcb_so = SCTP_INP_SO(*inp_p);
2816 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2817 SCTP_TCB_UNLOCK((*stcb));
2818 SCTP_SOCKET_LOCK(pcb_so, 1);
2819 SCTP_TCB_LOCK((*stcb));
2820 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2822 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2823 SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2824 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2825 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2829 inp = (struct sctp_inpcb *)so->so_pcb;
2830 SCTP_INP_INCR_REF(inp);
2832 * We add the unbound flag here so that if we get an
2833 * soabort() before we get the move_pcb done, we
2834 * will properly cleanup.
2836 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2837 SCTP_PCB_FLAGS_CONNECTED |
2838 SCTP_PCB_FLAGS_IN_TCPPOOL |
2839 SCTP_PCB_FLAGS_UNBOUND |
2840 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2841 SCTP_PCB_FLAGS_DONT_WAKE);
2842 inp->sctp_features = (*inp_p)->sctp_features;
2843 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2844 inp->sctp_socket = so;
2845 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2846 inp->max_cwnd = (*inp_p)->max_cwnd;
2847 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2848 inp->ecn_supported = (*inp_p)->ecn_supported;
2849 inp->prsctp_supported = (*inp_p)->prsctp_supported;
2850 inp->auth_supported = (*inp_p)->auth_supported;
2851 inp->asconf_supported = (*inp_p)->asconf_supported;
2852 inp->reconfig_supported = (*inp_p)->reconfig_supported;
2853 inp->nrsack_supported = (*inp_p)->nrsack_supported;
2854 inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2855 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2856 inp->sctp_context = (*inp_p)->sctp_context;
2857 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2858 inp->fibnum = (*inp_p)->fibnum;
2859 inp->inp_starting_point_for_iterator = NULL;
2861 * copy in the authentication parameters from the
2864 if (inp->sctp_ep.local_hmacs)
2865 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2866 inp->sctp_ep.local_hmacs =
2867 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2868 if (inp->sctp_ep.local_auth_chunks)
2869 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2870 inp->sctp_ep.local_auth_chunks =
2871 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2874 * Now we must move it from one hash table to
2875 * another and get the tcb in the right place.
2879 * This is where the one-2-one socket is put into
2880 * the accept state waiting for the accept!
2883 SCTP_ADD_SUBSTATE(*stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
2885 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2887 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2888 SCTP_TCB_UNLOCK((*stcb));
2890 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2892 SCTP_TCB_LOCK((*stcb));
2893 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2897 * now we must check to see if we were aborted while
2898 * the move was going on and the lock/unlock
2901 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2903 * yep it was, we leave the assoc attached
2904 * to the socket since the sctp_inpcb_free()
2905 * call will send an abort for us.
2907 SCTP_INP_DECR_REF(inp);
2910 SCTP_INP_DECR_REF(inp);
2911 /* Switch over to the new guy */
2913 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2914 if (send_int_conf) {
2915 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2916 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2920 * Pull it from the incomplete queue and wake the
2923 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2924 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2925 SCTP_TCB_UNLOCK((*stcb));
2926 SCTP_SOCKET_LOCK(so, 1);
2929 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2930 SCTP_TCB_LOCK((*stcb));
2931 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2932 SCTP_SOCKET_UNLOCK(so, 1);
2938 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2940 if (send_int_conf) {
2941 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2942 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2948 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2949 struct sctp_tcb *stcb, struct sctp_nets *net)
2951 /* cp must not be used, others call this without a c-ack :-) */
2952 struct sctp_association *asoc;
2954 SCTPDBG(SCTP_DEBUG_INPUT2,
2955 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2956 if ((stcb == NULL) || (net == NULL)) {
2961 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2962 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2963 asoc->overall_error_count,
2965 SCTP_FROM_SCTP_INPUT,
2968 asoc->overall_error_count = 0;
2969 sctp_stop_all_cookie_timers(stcb);
2970 /* process according to association state */
2971 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) {
2972 /* state change only needed when I am in right state */
2973 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2974 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
2975 sctp_start_net_timers(stcb);
2976 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2977 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2978 stcb->sctp_ep, stcb, asoc->primary_destination);
2982 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2983 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2984 if (asoc->overall_error_count == 0) {
2985 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2986 &asoc->time_entered,
2987 SCTP_RTT_FROM_NON_DATA);
2989 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2990 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2991 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2992 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2993 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2997 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2998 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2999 so = SCTP_INP_SO(stcb->sctp_ep);
3000 atomic_add_int(&stcb->asoc.refcnt, 1);
3001 SCTP_TCB_UNLOCK(stcb);
3002 SCTP_SOCKET_LOCK(so, 1);
3003 SCTP_TCB_LOCK(stcb);
3004 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3006 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
3007 soisconnected(stcb->sctp_socket);
3009 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3010 SCTP_SOCKET_UNLOCK(so, 1);
3014 * since we did not send a HB make sure we don't double
3017 net->hb_responded = 1;
3019 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3021 * We don't need to do the asconf thing, nor hb or
3022 * autoclose if the socket is closed.
3027 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
3031 if (stcb->asoc.sctp_autoclose_ticks &&
3032 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
3033 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
3034 stcb->sctp_ep, stcb, NULL);
3037 * send ASCONF if parameters are pending and ASCONFs are
3038 * allowed (eg. addresses changed when init/cookie echo were
3041 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
3042 (stcb->asoc.asconf_supported == 1) &&
3043 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
3044 #ifdef SCTP_TIMER_BASED_ASCONF
3045 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
3046 stcb->sctp_ep, stcb,
3047 stcb->asoc.primary_destination);
3049 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
3050 SCTP_ADDR_NOT_LOCKED);
3055 /* Toss the cookie if I can */
3056 sctp_toss_old_cookies(stcb, asoc);
3057 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3058 /* Restart the timer if we have pending data */
3059 struct sctp_tmit_chunk *chk;
3061 chk = TAILQ_FIRST(&asoc->sent_queue);
3062 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
3067 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
3068 struct sctp_tcb *stcb)
3070 struct sctp_nets *net;
3071 struct sctp_tmit_chunk *lchk;
3072 struct sctp_ecne_chunk bkup;
3073 uint8_t override_bit;
3074 uint32_t tsn, window_data_tsn;
3076 unsigned int pkt_cnt;
3078 len = ntohs(cp->ch.chunk_length);
3079 if ((len != sizeof(struct sctp_ecne_chunk)) &&
3080 (len != sizeof(struct old_sctp_ecne_chunk))) {
3083 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3084 /* Its the old format */
3085 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3086 bkup.num_pkts_since_cwr = htonl(1);
3089 SCTP_STAT_INCR(sctps_recvecne);
3090 tsn = ntohl(cp->tsn);
3091 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3092 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3094 window_data_tsn = stcb->asoc.sending_seq - 1;
3096 window_data_tsn = lchk->rec.data.tsn;
3099 /* Find where it was sent to if possible. */
3101 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3102 if (lchk->rec.data.tsn == tsn) {
3104 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3107 if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) {
3113 * What to do. A previous send of a CWR was possibly lost.
3114 * See how old it is, we may have it marked on the actual
3117 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3118 if (tsn == net->last_cwr_tsn) {
3119 /* Found him, send it off */
3125 * If we reach here, we need to send a special CWR
3126 * that says hey, we did this a long time ago and
3127 * you lost the response.
3129 net = TAILQ_FIRST(&stcb->asoc.nets);
3134 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3141 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3142 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3144 * JRS - Use the congestion control given in the pluggable
3147 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3149 * We reduce once every RTT. So we will only lower cwnd at
3150 * the next sending seq i.e. the window_data_tsn
3152 net->cwr_window_tsn = window_data_tsn;
3153 net->ecn_ce_pkt_cnt += pkt_cnt;
3154 net->lost_cnt = pkt_cnt;
3155 net->last_cwr_tsn = tsn;
3157 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3158 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3159 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3161 * Another loss in the same window update how many
3162 * marks/packets lost we have had.
3166 if (pkt_cnt > net->lost_cnt) {
3167 /* Should be the case */
3168 cnt = (pkt_cnt - net->lost_cnt);
3169 net->ecn_ce_pkt_cnt += cnt;
3171 net->lost_cnt = pkt_cnt;
3172 net->last_cwr_tsn = tsn;
3174 * Most CC functions will ignore this call, since we
3175 * are in-window yet of the initial CE the peer saw.
3177 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3181 * We always send a CWR this way if our previous one was lost our
3182 * peer will get an update, or if it is not time again to reduce we
3183 * still get the cwr to the peer. Note we set the override when we
3184 * could not find the TSN on the chunk or the destination network.
3186 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3190 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3193 * Here we get a CWR from the peer. We must look in the outqueue and
3194 * make sure that we have a covered ECNE in the control chunk part.
3197 struct sctp_tmit_chunk *chk, *nchk;
3198 struct sctp_ecne_chunk *ecne;
3202 cwr_tsn = ntohl(cp->tsn);
3203 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3204 TAILQ_FOREACH_SAFE(chk, &stcb->asoc.control_send_queue, sctp_next, nchk) {
3205 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3208 if ((override == 0) && (chk->whoTo != net)) {
3209 /* Must be from the right src unless override is set */
3212 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3213 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3214 /* this covers this ECNE, we can remove it */
3215 stcb->asoc.ecn_echo_cnt_onq--;
3216 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3218 stcb->asoc.ctrl_queue_cnt--;
3219 sctp_m_freem(chk->data);
3221 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3222 if (override == 0) {
3230 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3231 struct sctp_tcb *stcb, struct sctp_nets *net)
3233 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3237 SCTPDBG(SCTP_DEBUG_INPUT2,
3238 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3242 /* process according to association state */
3243 if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3244 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3245 SCTPDBG(SCTP_DEBUG_INPUT2,
3246 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3247 SCTP_TCB_UNLOCK(stcb);
3250 /* notify upper layer protocol */
3251 if (stcb->sctp_socket) {
3252 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3255 if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
3256 !TAILQ_EMPTY(&stcb->asoc.sent_queue) ||
3257 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
3258 panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3261 /* stop the timer */
3262 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3263 SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3264 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3266 SCTPDBG(SCTP_DEBUG_INPUT2,
3267 "sctp_handle_shutdown_complete: calls free-asoc\n");
3268 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3269 so = SCTP_INP_SO(stcb->sctp_ep);
3270 atomic_add_int(&stcb->asoc.refcnt, 1);
3271 SCTP_TCB_UNLOCK(stcb);
3272 SCTP_SOCKET_LOCK(so, 1);
3273 SCTP_TCB_LOCK(stcb);
3274 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3276 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3277 SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3278 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3279 SCTP_SOCKET_UNLOCK(so, 1);
3285 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3286 struct sctp_nets *net, uint8_t flg)
3288 switch (desc->chunk_type) {
3290 /* find the tsn to resend (possibly */
3293 struct sctp_tmit_chunk *tp1;
3295 tsn = ntohl(desc->tsn_ifany);
3296 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3297 if (tp1->rec.data.tsn == tsn) {
3301 if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) {
3309 * Do it the other way , aka without paying
3310 * attention to queue seq order.
3312 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3313 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3314 if (tp1->rec.data.tsn == tsn) {
3321 SCTP_STAT_INCR(sctps_pdrptsnnf);
3323 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3326 if (((flg & SCTP_BADCRC) == 0) &&
3327 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3330 if ((stcb->asoc.peers_rwnd == 0) &&
3331 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3332 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3335 if (stcb->asoc.peers_rwnd == 0 &&
3336 (flg & SCTP_FROM_MIDDLE_BOX)) {
3337 SCTP_STAT_INCR(sctps_pdrpdizrw);
3340 ddp = (uint8_t *)(mtod(tp1->data, caddr_t)+
3341 sizeof(struct sctp_data_chunk));
3345 for (iii = 0; iii < sizeof(desc->data_bytes);
3347 if (ddp[iii] != desc->data_bytes[iii]) {
3348 SCTP_STAT_INCR(sctps_pdrpbadd);
3356 * this guy had a RTO calculation
3357 * pending on it, cancel it
3359 if (tp1->whoTo->rto_needed == 0) {
3360 tp1->whoTo->rto_needed = 1;
3364 SCTP_STAT_INCR(sctps_pdrpmark);
3365 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3366 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3368 * mark it as if we were doing a FR, since
3369 * we will be getting gap ack reports behind
3370 * the info from the router.
3372 tp1->rec.data.doing_fast_retransmit = 1;
3374 * mark the tsn with what sequences can
3377 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3378 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3380 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
3383 /* restart the timer */
3384 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3386 SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3387 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3390 /* fix counts and things */
3391 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3392 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3393 tp1->whoTo->flight_size,
3395 (uint32_t)(uintptr_t)stcb,
3398 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3399 sctp_flight_size_decrease(tp1);
3400 sctp_total_flight_decrease(stcb, tp1);
3402 tp1->sent = SCTP_DATAGRAM_RESEND;
3408 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3409 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3412 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3414 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3417 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3418 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3419 audit, stcb->asoc.sent_queue_retran_cnt);
3420 #ifndef SCTP_AUDITING_ENABLED
3421 stcb->asoc.sent_queue_retran_cnt = audit;
3429 struct sctp_tmit_chunk *asconf;
3431 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3433 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3438 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3439 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3440 asconf->sent = SCTP_DATAGRAM_RESEND;
3441 asconf->snd_count--;
3445 case SCTP_INITIATION:
3446 /* resend the INIT */
3447 stcb->asoc.dropped_special_cnt++;
3448 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3450 * If we can get it in, in a few attempts we do
3451 * this, otherwise we let the timer fire.
3453 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3455 SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3456 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3459 case SCTP_SELECTIVE_ACK:
3460 case SCTP_NR_SELECTIVE_ACK:
3461 /* resend the sack */
3462 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3464 case SCTP_HEARTBEAT_REQUEST:
3465 /* resend a demand HB */
3466 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3468 * Only retransmit if we KNOW we wont destroy the
3471 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3475 sctp_send_shutdown(stcb, net);
3477 case SCTP_SHUTDOWN_ACK:
3478 sctp_send_shutdown_ack(stcb, net);
3480 case SCTP_COOKIE_ECHO:
3482 struct sctp_tmit_chunk *cookie;
3485 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3487 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3492 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3493 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3494 cookie->sent = SCTP_DATAGRAM_RESEND;
3495 sctp_stop_all_cookie_timers(stcb);
3499 case SCTP_COOKIE_ACK:
3500 sctp_send_cookie_ack(stcb);
3502 case SCTP_ASCONF_ACK:
3503 /* resend last asconf ack */
3504 sctp_send_asconf_ack(stcb);
3506 case SCTP_IFORWARD_CUM_TSN:
3507 case SCTP_FORWARD_CUM_TSN:
3508 send_forward_tsn(stcb, &stcb->asoc);
3510 /* can't do anything with these */
3511 case SCTP_PACKET_DROPPED:
3512 case SCTP_INITIATION_ACK: /* this should not happen */
3513 case SCTP_HEARTBEAT_ACK:
3514 case SCTP_ABORT_ASSOCIATION:
3515 case SCTP_OPERATION_ERROR:
3516 case SCTP_SHUTDOWN_COMPLETE:
3526 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3532 * We set things to 0xffffffff since this is the last delivered
3533 * sequence and we will be sending in 0 after the reset.
3536 if (number_entries) {
3537 for (i = 0; i < number_entries; i++) {
3538 temp = ntohs(list[i]);
3539 if (temp >= stcb->asoc.streamincnt) {
3542 stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff;
3546 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3547 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
3550 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3554 sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3559 if (number_entries > 0) {
3560 for (i = 0; i < number_entries; i++) {
3561 temp = ntohs(list[i]);
3562 if (temp >= stcb->asoc.streamoutcnt) {
3563 /* no such stream */
3566 stcb->asoc.strmout[temp].next_mid_ordered = 0;
3567 stcb->asoc.strmout[temp].next_mid_unordered = 0;
3570 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3571 stcb->asoc.strmout[i].next_mid_ordered = 0;
3572 stcb->asoc.strmout[i].next_mid_unordered = 0;
3575 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3579 sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3584 if (number_entries > 0) {
3585 for (i = 0; i < number_entries; i++) {
3586 temp = ntohs(list[i]);
3587 if (temp >= stcb->asoc.streamoutcnt) {
3588 /* no such stream */
3591 stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
3594 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3595 stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
3601 struct sctp_stream_reset_request *
3602 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3604 struct sctp_association *asoc;
3605 struct sctp_chunkhdr *ch;
3606 struct sctp_stream_reset_request *r;
3607 struct sctp_tmit_chunk *chk;
3611 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3612 asoc->stream_reset_outstanding = 0;
3615 if (stcb->asoc.str_reset == NULL) {
3616 asoc->stream_reset_outstanding = 0;
3619 chk = stcb->asoc.str_reset;
3620 if (chk->data == NULL) {
3624 /* he wants a copy of the chk pointer */
3627 clen = chk->send_size;
3628 ch = mtod(chk->data, struct sctp_chunkhdr *);
3629 r = (struct sctp_stream_reset_request *)(ch + 1);
3630 if (ntohl(r->request_seq) == seq) {
3634 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3635 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3636 /* move to the next one, there can only be a max of two */
3637 r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3638 if (ntohl(r->request_seq) == seq) {
3642 /* that seq is not here */
3647 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3649 struct sctp_association *asoc;
3650 struct sctp_tmit_chunk *chk;
3653 chk = asoc->str_reset;
3657 asoc->str_reset = NULL;
3658 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
3659 chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3660 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3661 asoc->ctrl_queue_cnt--;
3663 sctp_m_freem(chk->data);
3666 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3671 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3672 uint32_t seq, uint32_t action,
3673 struct sctp_stream_reset_response *respin)
3677 struct sctp_association *asoc = &stcb->asoc;
3678 struct sctp_tmit_chunk *chk;
3679 struct sctp_stream_reset_request *req_param;
3680 struct sctp_stream_reset_out_request *req_out_param;
3681 struct sctp_stream_reset_in_request *req_in_param;
3682 uint32_t number_entries;
3684 if (asoc->stream_reset_outstanding == 0) {
3688 if (seq == stcb->asoc.str_reset_seq_out) {
3689 req_param = sctp_find_stream_reset(stcb, seq, &chk);
3690 if (req_param != NULL) {
3691 stcb->asoc.str_reset_seq_out++;
3692 type = ntohs(req_param->ph.param_type);
3693 lparam_len = ntohs(req_param->ph.param_length);
3694 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3697 req_out_param = (struct sctp_stream_reset_out_request *)req_param;
3698 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3699 asoc->stream_reset_out_is_outstanding = 0;
3700 if (asoc->stream_reset_outstanding)
3701 asoc->stream_reset_outstanding--;
3702 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3704 sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3705 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3706 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3707 } else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
3709 * Set it up so we don't stop
3712 asoc->stream_reset_outstanding++;
3713 stcb->asoc.str_reset_seq_out--;
3714 asoc->stream_reset_out_is_outstanding = 1;
3717 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3719 if (no_clear == 0) {
3720 sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
3722 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3723 req_in_param = (struct sctp_stream_reset_in_request *)req_param;
3724 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3725 if (asoc->stream_reset_outstanding)
3726 asoc->stream_reset_outstanding--;
3727 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3728 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3729 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3730 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3731 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3732 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3734 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3735 /* Ok we now may have more streams */
3738 num_stream = stcb->asoc.strm_pending_add_size;
3739 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3741 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3743 stcb->asoc.strm_pending_add_size = 0;
3744 if (asoc->stream_reset_outstanding)
3745 asoc->stream_reset_outstanding--;
3746 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3747 /* Put the new streams into effect */
3750 for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
3751 asoc->strmout[i].state = SCTP_STREAM_OPEN;
3753 asoc->streamoutcnt += num_stream;
3754 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3755 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3756 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3757 SCTP_STREAM_CHANGE_DENIED);
3759 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3760 SCTP_STREAM_CHANGE_FAILED);
3762 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3763 if (asoc->stream_reset_outstanding)
3764 asoc->stream_reset_outstanding--;
3765 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3766 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3767 SCTP_STREAM_CHANGE_DENIED);
3768 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3769 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3770 SCTP_STREAM_CHANGE_FAILED);
3772 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3774 * a) Adopt the new in tsn.
3776 * c) Adopt the new out-tsn
3778 struct sctp_stream_reset_response_tsn *resp;
3779 struct sctp_forward_tsn_chunk fwdtsn;
3782 if (respin == NULL) {
3786 if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
3789 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3790 resp = (struct sctp_stream_reset_response_tsn *)respin;
3791 asoc->stream_reset_outstanding--;
3792 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3793 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3794 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3795 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3799 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3800 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3801 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3804 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3805 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3806 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3808 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3809 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3811 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3812 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3814 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3815 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3816 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3817 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3818 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3819 SCTP_ASSOC_RESET_DENIED);
3821 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3822 SCTP_ASSOC_RESET_FAILED);
3825 /* get rid of the request and get the request flags */
3826 if (asoc->stream_reset_outstanding == 0) {
3827 sctp_clean_up_stream_reset(stcb);
3831 if (asoc->stream_reset_outstanding == 0) {
3832 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3838 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3839 struct sctp_tmit_chunk *chk,
3840 struct sctp_stream_reset_in_request *req, int trunc)
3848 * peer wants me to send a str-reset to him for my outgoing seq's if
3851 struct sctp_association *asoc = &stcb->asoc;
3853 seq = ntohl(req->request_seq);
3854 if (asoc->str_reset_seq_in == seq) {
3855 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3856 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3857 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3859 /* Can't do it, since they exceeded our buffer size */
3860 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3861 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3862 len = ntohs(req->ph.param_length);
3863 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3864 if (number_entries) {
3865 for (i = 0; i < number_entries; i++) {
3866 temp = ntohs(req->list_of_streams[i]);
3867 if (temp >= stcb->asoc.streamoutcnt) {
3868 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3871 req->list_of_streams[i] = temp;
3873 for (i = 0; i < number_entries; i++) {
3874 if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
3875 stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
3880 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3881 if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
3882 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
3885 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3887 /* Can't do it, since we have sent one out */
3888 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3891 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3892 asoc->str_reset_seq_in++;
3893 } else if (asoc->str_reset_seq_in - 1 == seq) {
3894 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3895 } else if (asoc->str_reset_seq_in - 2 == seq) {
3896 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3898 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3900 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3904 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3905 struct sctp_tmit_chunk *chk,
3906 struct sctp_stream_reset_tsn_request *req)
3908 /* reset all in and out and update the tsn */
3910 * A) reset my str-seq's on in and out. B) Select a receive next,
3911 * and set cum-ack to it. Also process this selected number as a
3912 * fwd-tsn as well. C) set in the response my next sending seq.
3914 struct sctp_forward_tsn_chunk fwdtsn;
3915 struct sctp_association *asoc = &stcb->asoc;
3919 seq = ntohl(req->request_seq);
3920 if (asoc->str_reset_seq_in == seq) {
3921 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3922 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3923 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3925 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3926 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3927 fwdtsn.ch.chunk_flags = 0;
3928 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3929 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3933 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3934 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3935 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3937 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3938 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3939 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3940 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3941 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3942 atomic_add_int(&asoc->sending_seq, 1);
3943 /* save off historical data for retrans */
3944 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3945 asoc->last_sending_seq[0] = asoc->sending_seq;
3946 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3947 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3948 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3949 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3950 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3951 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3953 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3954 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3955 asoc->str_reset_seq_in++;
3956 } else if (asoc->str_reset_seq_in - 1 == seq) {
3957 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3958 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3959 } else if (asoc->str_reset_seq_in - 2 == seq) {
3960 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3961 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3963 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3969 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3970 struct sctp_tmit_chunk *chk,
3971 struct sctp_stream_reset_out_request *req, int trunc)
3974 int number_entries, len;
3975 struct sctp_association *asoc = &stcb->asoc;
3977 seq = ntohl(req->request_seq);
3979 /* now if its not a duplicate we process it */
3980 if (asoc->str_reset_seq_in == seq) {
3981 len = ntohs(req->ph.param_length);
3982 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3984 * the sender is resetting, handle the list issue.. we must
3985 * a) verify if we can do the reset, if so no problem b) If
3986 * we can't do the reset we must copy the request. c) queue
3987 * it, and setup the data in processor to trigger it off
3988 * when needed and dequeue all the queued data.
3990 tsn = ntohl(req->send_reset_at_tsn);
3992 /* move the reset action back one */
3993 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3994 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3995 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3997 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3998 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3999 /* we can do it now */
4000 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
4001 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4004 * we must queue it up and thus wait for the TSN's
4005 * to arrive that are at or before tsn
4007 struct sctp_stream_reset_list *liste;
4010 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
4011 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
4012 siz, SCTP_M_STRESET);
4013 if (liste == NULL) {
4014 /* gak out of memory */
4015 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4016 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4021 liste->number_entries = number_entries;
4022 memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
4023 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
4024 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
4026 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4027 asoc->str_reset_seq_in++;
4028 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4030 * one seq back, just echo back last action since my
4031 * response was lost.
4033 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4034 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4036 * two seq back, just echo back last action since my
4037 * response was lost.
4039 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4041 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4046 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4047 struct sctp_stream_reset_add_strm *str_add)
4050 * Peer is requesting to add more streams. If its within our
4051 * max-streams we will allow it.
4053 uint32_t num_stream, i;
4055 struct sctp_association *asoc = &stcb->asoc;
4056 struct sctp_queued_to_read *ctl, *nctl;
4058 /* Get the number. */
4059 seq = ntohl(str_add->request_seq);
4060 num_stream = ntohs(str_add->number_of_streams);
4061 /* Now what would be the new total? */
4062 if (asoc->str_reset_seq_in == seq) {
4063 num_stream += stcb->asoc.streamincnt;
4064 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4065 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4066 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4067 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
4068 (num_stream > 0xffff)) {
4069 /* We must reject it they ask for to many */
4071 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4073 /* Ok, we can do that :-) */
4074 struct sctp_stream_in *oldstrm;
4076 /* save off the old */
4077 oldstrm = stcb->asoc.strmin;
4078 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
4079 (num_stream * sizeof(struct sctp_stream_in)),
4081 if (stcb->asoc.strmin == NULL) {
4082 stcb->asoc.strmin = oldstrm;
4085 /* copy off the old data */
4086 for (i = 0; i < stcb->asoc.streamincnt; i++) {
4087 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4088 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4089 stcb->asoc.strmin[i].sid = i;
4090 stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered;
4091 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
4092 stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
4093 /* now anything on those queues? */
4094 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
4095 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
4096 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
4098 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
4099 TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
4100 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
4103 /* Init the new streams */
4104 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
4105 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4106 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4107 stcb->asoc.strmin[i].sid = i;
4108 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
4109 stcb->asoc.strmin[i].pd_api_started = 0;
4110 stcb->asoc.strmin[i].delivery_started = 0;
4112 SCTP_FREE(oldstrm, SCTP_M_STRMI);
4113 /* update the size */
4114 stcb->asoc.streamincnt = num_stream;
4115 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4116 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
4118 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4119 asoc->str_reset_seq_in++;
4120 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4122 * one seq back, just echo back last action since my
4123 * response was lost.
4125 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4126 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4128 * two seq back, just echo back last action since my
4129 * response was lost.
4131 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4133 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4139 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4140 struct sctp_stream_reset_add_strm *str_add)
4143 * Peer is requesting to add more streams. If its within our
4144 * max-streams we will allow it.
4146 uint16_t num_stream;
4148 struct sctp_association *asoc = &stcb->asoc;
4150 /* Get the number. */
4151 seq = ntohl(str_add->request_seq);
4152 num_stream = ntohs(str_add->number_of_streams);
4153 /* Now what would be the new total? */
4154 if (asoc->str_reset_seq_in == seq) {
4155 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4156 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4157 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4158 } else if (stcb->asoc.stream_reset_outstanding) {
4159 /* We must reject it we have something pending */
4160 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4162 /* Ok, we can do that :-) */
4165 mychk = stcb->asoc.streamoutcnt;
4166 mychk += num_stream;
4167 if (mychk < 0x10000) {
4168 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4169 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
4170 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4173 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4176 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4177 asoc->str_reset_seq_in++;
4178 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4180 * one seq back, just echo back last action since my
4181 * response was lost.
4183 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4184 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4186 * two seq back, just echo back last action since my
4187 * response was lost.
4189 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4191 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4196 __attribute__((noinline))
4199 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4200 struct sctp_chunkhdr *ch_req)
4202 uint16_t remaining_length, param_len, ptype;
4203 struct sctp_paramhdr pstore;
4204 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4208 struct sctp_tmit_chunk *chk;
4209 struct sctp_chunkhdr *ch;
4210 struct sctp_paramhdr *ph;
4214 /* now it may be a reset or a reset-response */
4215 remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
4217 /* setup for adding the response */
4218 sctp_alloc_a_chunk(stcb, chk);
4222 chk->copy_by_ref = 0;
4223 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4224 chk->rec.chunk_id.can_take_data = 0;
4226 chk->asoc = &stcb->asoc;
4227 chk->no_fr_allowed = 0;
4228 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4229 chk->book_size_scale = 0;
4230 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4231 if (chk->data == NULL) {
4234 sctp_m_freem(chk->data);
4237 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4240 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4242 /* setup chunk parameters */
4243 chk->sent = SCTP_DATAGRAM_UNSENT;
4247 ch = mtod(chk->data, struct sctp_chunkhdr *);
4248 ch->chunk_type = SCTP_STREAM_RESET;
4249 ch->chunk_flags = 0;
4250 ch->chunk_length = htons(chk->send_size);
4251 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4252 offset += sizeof(struct sctp_chunkhdr);
4253 while (remaining_length >= sizeof(struct sctp_paramhdr)) {
4254 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
4259 param_len = ntohs(ph->param_length);
4260 if ((param_len > remaining_length) ||
4261 (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
4262 /* bad parameter length */
4265 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
4266 (uint8_t *)&cstore);
4271 ptype = ntohs(ph->param_type);
4273 if (param_len > sizeof(cstore)) {
4278 if (num_param > SCTP_MAX_RESET_PARAMS) {
4279 /* hit the max of parameters already sorry.. */
4282 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4283 struct sctp_stream_reset_out_request *req_out;
4285 if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
4288 req_out = (struct sctp_stream_reset_out_request *)ph;
4290 if (stcb->asoc.stream_reset_outstanding) {
4291 seq = ntohl(req_out->response_seq);
4292 if (seq == stcb->asoc.str_reset_seq_out) {
4294 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4297 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4298 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4299 struct sctp_stream_reset_add_strm *str_add;
4301 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4304 str_add = (struct sctp_stream_reset_add_strm *)ph;
4306 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4307 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4308 struct sctp_stream_reset_add_strm *str_add;
4310 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4313 str_add = (struct sctp_stream_reset_add_strm *)ph;
4315 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4316 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4317 struct sctp_stream_reset_in_request *req_in;
4320 req_in = (struct sctp_stream_reset_in_request *)ph;
4321 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4322 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4323 struct sctp_stream_reset_tsn_request *req_tsn;
4326 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4327 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4329 goto strres_nochunk;
4333 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4334 struct sctp_stream_reset_response *resp;
4337 if (param_len < sizeof(struct sctp_stream_reset_response)) {
4340 resp = (struct sctp_stream_reset_response *)ph;
4341 seq = ntohl(resp->response_seq);
4342 result = ntohl(resp->result);
4343 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4345 goto strres_nochunk;
4350 offset += SCTP_SIZE32(param_len);
4351 if (remaining_length >= SCTP_SIZE32(param_len)) {
4352 remaining_length -= SCTP_SIZE32(param_len);
4354 remaining_length = 0;
4358 /* we have no response free the stuff */
4359 goto strres_nochunk;
4361 /* ok we have a chunk to link in */
4362 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4365 stcb->asoc.ctrl_queue_cnt++;
4370 * Handle a router or endpoints report of a packet loss, there are two ways
4371 * to handle this, either we get the whole packet and must disect it
4372 * ourselves (possibly with truncation and or corruption) or it is a summary
4373 * from a middle box that did the disectting for us.
4376 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4377 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4379 uint32_t bottle_bw, on_queue;
4383 struct sctp_chunk_desc desc;
4384 struct sctp_chunkhdr *ch;
4386 chlen = ntohs(cp->ch.chunk_length);
4387 chlen -= sizeof(struct sctp_pktdrop_chunk);
4388 /* XXX possible chlen underflow */
4391 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4392 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4394 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4395 chlen -= sizeof(struct sctphdr);
4396 /* XXX possible chlen underflow */
4397 memset(&desc, 0, sizeof(desc));
4399 trunc_len = (uint16_t)ntohs(cp->trunc_len);
4400 if (trunc_len > limit) {
4404 /* now the chunks themselves */
4405 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4406 desc.chunk_type = ch->chunk_type;
4407 /* get amount we need to move */
4408 at = ntohs(ch->chunk_length);
4409 if (at < sizeof(struct sctp_chunkhdr)) {
4410 /* corrupt chunk, maybe at the end? */
4411 SCTP_STAT_INCR(sctps_pdrpcrupt);
4414 if (trunc_len == 0) {
4415 /* we are supposed to have all of it */
4417 /* corrupt skip it */
4418 SCTP_STAT_INCR(sctps_pdrpcrupt);
4422 /* is there enough of it left ? */
4423 if (desc.chunk_type == SCTP_DATA) {
4424 if (chlen < (sizeof(struct sctp_data_chunk) +
4425 sizeof(desc.data_bytes))) {
4429 if (chlen < sizeof(struct sctp_chunkhdr)) {
4434 if (desc.chunk_type == SCTP_DATA) {
4435 /* can we get out the tsn? */
4436 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4437 SCTP_STAT_INCR(sctps_pdrpmbda);
4439 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4441 struct sctp_data_chunk *dcp;
4445 dcp = (struct sctp_data_chunk *)ch;
4446 ddp = (uint8_t *)(dcp + 1);
4447 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4448 desc.data_bytes[iii] = ddp[iii];
4450 desc.tsn_ifany = dcp->dp.tsn;
4452 /* nope we are done. */
4453 SCTP_STAT_INCR(sctps_pdrpnedat);
4457 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4458 SCTP_STAT_INCR(sctps_pdrpmbct);
4461 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4462 SCTP_STAT_INCR(sctps_pdrppdbrk);
4465 if (SCTP_SIZE32(at) > chlen) {
4468 chlen -= SCTP_SIZE32(at);
4469 if (chlen < sizeof(struct sctp_chunkhdr)) {
4470 /* done, none left */
4473 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4475 /* Now update any rwnd --- possibly */
4476 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4477 /* From a peer, we get a rwnd report */
4480 SCTP_STAT_INCR(sctps_pdrpfehos);
4482 bottle_bw = ntohl(cp->bottle_bw);
4483 on_queue = ntohl(cp->current_onq);
4484 if (bottle_bw && on_queue) {
4485 /* a rwnd report is in here */
4486 if (bottle_bw > on_queue)
4487 a_rwnd = bottle_bw - on_queue;
4492 stcb->asoc.peers_rwnd = 0;
4494 if (a_rwnd > stcb->asoc.total_flight) {
4495 stcb->asoc.peers_rwnd =
4496 a_rwnd - stcb->asoc.total_flight;
4498 stcb->asoc.peers_rwnd = 0;
4500 if (stcb->asoc.peers_rwnd <
4501 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4502 /* SWS sender side engages */
4503 stcb->asoc.peers_rwnd = 0;
4508 SCTP_STAT_INCR(sctps_pdrpfmbox);
4511 /* now middle boxes in sat networks get a cwnd bump */
4512 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4513 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4514 (stcb->asoc.sat_network)) {
4516 * This is debatable but for sat networks it makes sense
4517 * Note if a T3 timer has went off, we will prohibit any
4518 * changes to cwnd until we exit the t3 loss recovery.
4520 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4521 net, cp, &bottle_bw, &on_queue);
4526 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4527 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4528 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4529 * length of the complete packet outputs: - length: modified to remaining
4530 * length after control processing - netp: modified to new sctp_nets after
4531 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4532 * bad packet,...) otherwise return the tcb for this packet
4535 __attribute__((noinline))
4537 static struct sctp_tcb *
4538 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4539 struct sockaddr *src, struct sockaddr *dst,
4540 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4541 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4542 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4543 uint32_t vrf_id, uint16_t port)
4545 struct sctp_association *asoc;
4546 struct mbuf *op_err;
4547 char msg[SCTP_DIAG_INFO_LEN];
4549 int num_chunks = 0; /* number of control chunks processed */
4550 uint32_t chk_length, contiguous;
4552 int abort_no_unlock = 0;
4556 * How big should this be, and should it be alloc'd? Lets try the
4557 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4558 * until we get into jumbo grams and such..
4560 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4562 uint32_t auth_offset = 0, auth_len = 0;
4563 int auth_skipped = 0;
4565 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4569 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4570 iphlen, *offset, length, (void *)stcb);
4573 SCTP_TCB_LOCK_ASSERT(stcb);
4575 /* validate chunk header length... */
4576 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4577 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4578 ntohs(ch->chunk_length));
4583 * validate the verification tag
4585 vtag_in = ntohl(sh->v_tag);
4587 if (ch->chunk_type == SCTP_INITIATION) {
4588 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4589 ntohs(ch->chunk_length), vtag_in);
4591 /* protocol error- silently discard... */
4592 SCTP_STAT_INCR(sctps_badvtag);
4594 SCTP_TCB_UNLOCK(stcb);
4598 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4600 * If there is no stcb, skip the AUTH chunk and process
4601 * later after a stcb is found (to validate the lookup was
4604 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4606 (inp->auth_supported == 1)) {
4607 /* save this chunk for later processing */
4609 auth_offset = *offset;
4610 auth_len = ntohs(ch->chunk_length);
4612 /* (temporarily) move past this chunk */
4613 *offset += SCTP_SIZE32(auth_len);
4614 if (*offset >= length) {
4615 /* no more data left in the mbuf chain */
4619 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4620 sizeof(struct sctp_chunkhdr), chunk_buf);
4627 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4628 goto process_control_chunks;
4631 * first check if it's an ASCONF with an unknown src addr we
4632 * need to look inside to find the association
4634 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4635 struct sctp_chunkhdr *asconf_ch = ch;
4636 uint32_t asconf_offset = 0, asconf_len = 0;
4638 /* inp's refcount may be reduced */
4639 SCTP_INP_INCR_REF(inp);
4641 asconf_offset = *offset;
4643 asconf_len = ntohs(asconf_ch->chunk_length);
4644 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4646 stcb = sctp_findassociation_ep_asconf(m,
4649 sh, &inp, netp, vrf_id);
4652 asconf_offset += SCTP_SIZE32(asconf_len);
4653 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4654 sizeof(struct sctp_chunkhdr), chunk_buf);
4655 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4658 * reduce inp's refcount if not reduced in
4659 * sctp_findassociation_ep_asconf().
4661 SCTP_INP_DECR_REF(inp);
4664 /* now go back and verify any auth chunk to be sure */
4665 if (auth_skipped && (stcb != NULL)) {
4666 struct sctp_auth_chunk *auth;
4668 auth = (struct sctp_auth_chunk *)
4669 sctp_m_getptr(m, auth_offset,
4670 auth_len, chunk_buf);
4673 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4675 /* auth HMAC failed so dump it */
4679 /* remaining chunks are HMAC checked */
4680 stcb->asoc.authenticated = 1;
4685 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4686 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4688 /* no association, so it's out of the blue... */
4689 sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4690 mflowtype, mflowid, inp->fibnum,
4696 /* ABORT and SHUTDOWN can use either v_tag... */
4697 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4698 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4699 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4700 /* Take the T-bit always into account. */
4701 if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
4702 (vtag_in == asoc->my_vtag)) ||
4703 (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4704 (asoc->peer_vtag != htonl(0)) &&
4705 (vtag_in == asoc->peer_vtag))) {
4708 /* drop this packet... */
4709 SCTP_STAT_INCR(sctps_badvtag);
4711 SCTP_TCB_UNLOCK(stcb);
4715 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4716 if (vtag_in != asoc->my_vtag) {
4718 * this could be a stale SHUTDOWN-ACK or the
4719 * peer never got the SHUTDOWN-COMPLETE and
4720 * is still hung; we have started a new asoc
4721 * but it won't complete until the shutdown
4725 SCTP_TCB_UNLOCK(stcb);
4727 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4728 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4730 sctp_handle_ootb(m, iphlen, *offset, src, dst,
4732 mflowtype, mflowid, fibnum,
4737 /* for all other chunks, vtag must match */
4738 if (vtag_in != asoc->my_vtag) {
4739 /* invalid vtag... */
4740 SCTPDBG(SCTP_DEBUG_INPUT3,
4741 "invalid vtag: %xh, expect %xh\n",
4742 vtag_in, asoc->my_vtag);
4743 SCTP_STAT_INCR(sctps_badvtag);
4745 SCTP_TCB_UNLOCK(stcb);
4751 } /* end if !SCTP_COOKIE_ECHO */
4753 * process all control chunks...
4755 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4756 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4757 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4758 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4759 /* implied cookie-ack.. we must have lost the ack */
4760 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4764 process_control_chunks:
4765 while (IS_SCTP_CONTROL(ch)) {
4766 /* validate chunk length */
4767 chk_length = ntohs(ch->chunk_length);
4768 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4769 ch->chunk_type, chk_length);
4770 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4771 if (chk_length < sizeof(*ch) ||
4772 (*offset + (int)chk_length) > length) {
4776 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4778 * INIT and INIT-ACK only gets the init ack "header" portion
4779 * only because we don't have to process the peer's COOKIE.
4780 * All others get a complete chunk.
4782 switch (ch->chunk_type) {
4783 case SCTP_INITIATION:
4784 contiguous = sizeof(struct sctp_init_chunk);
4786 case SCTP_INITIATION_ACK:
4787 contiguous = sizeof(struct sctp_init_ack_chunk);
4790 contiguous = min(chk_length, sizeof(chunk_buf));
4793 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4799 SCTP_TCB_UNLOCK(stcb);
4805 /* Save off the last place we got a control from */
4807 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4809 * allow last_control to be NULL if
4810 * ASCONF... ASCONF processing will find the
4813 if ((netp != NULL) && (*netp != NULL))
4814 stcb->asoc.last_control_chunk_from = *netp;
4817 #ifdef SCTP_AUDITING_ENABLED
4818 sctp_audit_log(0xB0, ch->chunk_type);
4821 /* check to see if this chunk required auth, but isn't */
4822 if ((stcb != NULL) &&
4823 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4824 !stcb->asoc.authenticated) {
4825 /* "silently" ignore */
4826 SCTP_STAT_INCR(sctps_recvauthmissing);
4829 switch (ch->chunk_type) {
4830 case SCTP_INITIATION:
4831 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4832 /* The INIT chunk must be the only chunk. */
4833 if ((num_chunks > 1) ||
4834 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4835 /* RFC 4960 requires that no ABORT is sent */
4838 SCTP_TCB_UNLOCK(stcb);
4842 /* Honor our resource limit. */
4843 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4844 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4845 sctp_abort_association(inp, stcb, m, iphlen,
4846 src, dst, sh, op_err,
4852 sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4853 (struct sctp_init_chunk *)ch, inp,
4854 stcb, *netp, &abort_no_unlock,
4858 if ((!abort_no_unlock) && (stcb != NULL)) {
4859 SCTP_TCB_UNLOCK(stcb);
4863 case SCTP_PAD_CHUNK:
4865 case SCTP_INITIATION_ACK:
4866 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT_ACK\n");
4867 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4868 /* We are not interested anymore */
4869 if ((stcb != NULL) && (stcb->asoc.total_output_queue_size)) {
4874 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4875 so = SCTP_INP_SO(inp);
4876 atomic_add_int(&stcb->asoc.refcnt, 1);
4877 SCTP_TCB_UNLOCK(stcb);
4878 SCTP_SOCKET_LOCK(so, 1);
4879 SCTP_TCB_LOCK(stcb);
4880 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4882 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4883 SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4884 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4885 SCTP_SOCKET_UNLOCK(so, 1);
4891 /* The INIT-ACK chunk must be the only chunk. */
4892 if ((num_chunks > 1) ||
4893 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4897 if ((netp != NULL) && (*netp != NULL)) {
4898 ret = sctp_handle_init_ack(m, iphlen, *offset,
4900 (struct sctp_init_ack_chunk *)ch,
4909 if (abort_no_unlock) {
4913 * Special case, I must call the output routine to
4914 * get the cookie echoed
4916 if ((stcb != NULL) && (ret == 0)) {
4917 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4921 case SCTP_SELECTIVE_ACK:
4922 case SCTP_NR_SELECTIVE_ACK:
4925 uint32_t a_rwnd, cum_ack;
4926 uint16_t num_seg, num_nr_seg, num_dup;
4928 int offset_seg, offset_dup;
4930 SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n",
4931 ch->chunk_type == SCTP_SELECTIVE_ACK ? "SCTP_SACK" : "SCTP_NR_SACK");
4932 SCTP_STAT_INCR(sctps_recvsacks);
4934 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing %s chunk\n",
4935 (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK");
4938 if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4939 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4940 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4944 if (stcb->asoc.nrsack_supported == 0) {
4947 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4948 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR_SACK chunk, too small\n");
4952 if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4954 * If we have sent a shutdown-ack, we will pay no
4955 * attention to a sack sent in to us since
4956 * we don't care anymore.
4960 flags = ch->chunk_flags;
4961 if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4962 struct sctp_sack_chunk *sack;
4964 sack = (struct sctp_sack_chunk *)ch;
4965 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4966 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4968 num_dup = ntohs(sack->sack.num_dup_tsns);
4969 a_rwnd = ntohl(sack->sack.a_rwnd);
4970 if (sizeof(struct sctp_sack_chunk) +
4971 num_seg * sizeof(struct sctp_gap_ack_block) +
4972 num_dup * sizeof(uint32_t) != chk_length) {
4973 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4976 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4977 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4979 struct sctp_nr_sack_chunk *nr_sack;
4981 nr_sack = (struct sctp_nr_sack_chunk *)ch;
4982 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4983 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4984 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4985 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4986 a_rwnd = ntohl(nr_sack->nr_sack.a_rwnd);
4987 if (sizeof(struct sctp_nr_sack_chunk) +
4988 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4989 num_dup * sizeof(uint32_t) != chk_length) {
4990 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4993 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4994 offset_dup = offset_seg + (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block);
4996 SCTPDBG(SCTP_DEBUG_INPUT3, "%s process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4997 (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK",
4998 cum_ack, num_seg, a_rwnd);
4999 stcb->asoc.seen_a_sack_this_pkt = 1;
5000 if ((stcb->asoc.pr_sctp_cnt == 0) &&
5001 (num_seg == 0) && (num_nr_seg == 0) &&
5002 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
5003 (stcb->asoc.saw_sack_with_frags == 0) &&
5004 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
5005 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
5007 * We have a SIMPLE sack having no
5008 * prior segments and data on sent
5009 * queue to be acked. Use the faster
5010 * path sack processing. We also
5011 * allow window update sacks with no
5012 * missing segments to go this way
5015 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
5016 &abort_now, ecne_seen);
5018 if ((netp != NULL) && (*netp != NULL)) {
5019 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
5020 num_seg, num_nr_seg, num_dup, &abort_now, flags,
5021 cum_ack, a_rwnd, ecne_seen);
5025 /* ABORT signal from sack processing */
5029 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5030 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5031 (stcb->asoc.stream_queue_cnt == 0)) {
5032 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5036 case SCTP_HEARTBEAT_REQUEST:
5037 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
5038 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5039 SCTP_STAT_INCR(sctps_recvheartbeat);
5040 sctp_send_heartbeat_ack(stcb, m, *offset,
5044 case SCTP_HEARTBEAT_ACK:
5045 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT_ACK\n");
5046 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5051 SCTP_STAT_INCR(sctps_recvheartbeatack);
5052 if ((netp != NULL) && (*netp != NULL)) {
5053 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5057 case SCTP_ABORT_ASSOCIATION:
5058 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5061 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5062 if (sctp_handle_abort((struct sctp_abort_chunk *)ch, stcb, *netp)) {
5072 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5074 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5078 if ((netp != NULL) && (*netp != NULL)) {
5081 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5082 stcb, *netp, &abort_flag);
5089 case SCTP_SHUTDOWN_ACK:
5090 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_ACK, stcb %p\n", (void *)stcb);
5091 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5092 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5097 case SCTP_OPERATION_ERROR:
5098 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP_ERR\n");
5099 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL) &&
5100 sctp_handle_error(ch, stcb, *netp, contiguous) < 0) {
5105 case SCTP_COOKIE_ECHO:
5106 SCTPDBG(SCTP_DEBUG_INPUT3,
5107 "SCTP_COOKIE_ECHO, stcb %p\n", (void *)stcb);
5108 if ((stcb != NULL) && (stcb->asoc.total_output_queue_size > 0)) {
5111 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5112 /* We are not interested anymore */
5115 SCTP_TCB_UNLOCK(stcb);
5122 * First are we accepting? We do this again here
5123 * since it is possible that a previous endpoint WAS
5124 * listening responded to a INIT-ACK and then
5125 * closed. We opened and bound.. and are now no
5128 * XXXGL: notes on checking listen queue length.
5129 * 1) SCTP_IS_LISTENING() doesn't necessarily mean
5130 * SOLISTENING(), because a listening "UDP type"
5131 * socket isn't listening in terms of the socket
5132 * layer. It is a normal data flow socket, that
5133 * can fork off new connections. Thus, we should
5134 * look into sol_qlen only in case we are !UDP.
5135 * 2) Checking sol_qlen in general requires locking
5136 * the socket, and this code lacks that.
5138 if ((stcb == NULL) &&
5139 (!SCTP_IS_LISTENING(inp) ||
5140 (!(inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
5141 inp->sctp_socket->sol_qlen >= inp->sctp_socket->sol_qlimit))) {
5142 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5143 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5144 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
5145 sctp_abort_association(inp, stcb, m, iphlen,
5146 src, dst, sh, op_err,
5153 struct mbuf *ret_buf;
5154 struct sctp_inpcb *linp;
5163 SCTP_ASOC_CREATE_LOCK(linp);
5164 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5165 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5166 SCTP_ASOC_CREATE_UNLOCK(linp);
5172 struct sctp_tcb *locked_stcb;
5176 sctp_handle_cookie_echo(m, iphlen,
5180 (struct sctp_cookie_echo_chunk *)ch,
5190 if ((locked_stcb != NULL) && (locked_stcb != stcb)) {
5191 SCTP_TCB_UNLOCK(locked_stcb);
5194 SCTP_TCB_LOCK_ASSERT(stcb);
5200 SCTP_ASOC_CREATE_UNLOCK(linp);
5202 if (ret_buf == NULL) {
5204 SCTP_TCB_UNLOCK(stcb);
5206 SCTPDBG(SCTP_DEBUG_INPUT3,
5207 "GAK, null buffer\n");
5211 /* if AUTH skipped, see if it verified... */
5216 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5218 * Restart the timer if we have
5221 struct sctp_tmit_chunk *chk;
5223 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5224 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5228 case SCTP_COOKIE_ACK:
5229 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE_ACK, stcb %p\n", (void *)stcb);
5230 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5233 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5234 /* We are not interested anymore */
5235 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5238 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5239 so = SCTP_INP_SO(inp);
5240 atomic_add_int(&stcb->asoc.refcnt, 1);
5241 SCTP_TCB_UNLOCK(stcb);
5242 SCTP_SOCKET_LOCK(so, 1);
5243 SCTP_TCB_LOCK(stcb);
5244 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5246 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5247 SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5248 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5249 SCTP_SOCKET_UNLOCK(so, 1);
5255 if ((netp != NULL) && (*netp != NULL)) {
5256 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5260 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_ECHO\n");
5261 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5266 if (stcb->asoc.ecn_supported == 0) {
5269 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, stcb);
5273 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_CWR\n");
5274 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5278 if (stcb->asoc.ecn_supported == 0) {
5281 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5283 case SCTP_SHUTDOWN_COMPLETE:
5284 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_COMPLETE, stcb %p\n", (void *)stcb);
5285 /* must be first and only chunk */
5286 if ((num_chunks > 1) ||
5287 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5291 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5292 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5299 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5301 if (stcb->asoc.asconf_supported == 0) {
5304 sctp_handle_asconf(m, *offset, src,
5305 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5309 case SCTP_ASCONF_ACK:
5310 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF_ACK\n");
5311 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5316 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5317 if (stcb->asoc.asconf_supported == 0) {
5320 /* He's alive so give him credit */
5321 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5322 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5323 stcb->asoc.overall_error_count,
5325 SCTP_FROM_SCTP_INPUT,
5328 stcb->asoc.overall_error_count = 0;
5329 sctp_handle_asconf_ack(m, *offset,
5330 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5331 if (abort_no_unlock)
5335 case SCTP_FORWARD_CUM_TSN:
5336 case SCTP_IFORWARD_CUM_TSN:
5337 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD_TSN\n");
5338 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5347 if (stcb->asoc.prsctp_supported == 0) {
5351 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5352 /* We are not interested anymore */
5353 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5354 so = SCTP_INP_SO(inp);
5355 atomic_add_int(&stcb->asoc.refcnt, 1);
5356 SCTP_TCB_UNLOCK(stcb);
5357 SCTP_SOCKET_LOCK(so, 1);
5358 SCTP_TCB_LOCK(stcb);
5359 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5361 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5362 SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5363 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5364 SCTP_SOCKET_UNLOCK(so, 1);
5370 * For sending a SACK this looks like DATA
5373 stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from;
5374 sctp_handle_forward_tsn(stcb,
5375 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5382 case SCTP_STREAM_RESET:
5383 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5384 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5389 if (stcb->asoc.reconfig_supported == 0) {
5392 if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5393 /* stop processing */
5398 case SCTP_PACKET_DROPPED:
5399 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5400 /* re-get it all please */
5401 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5407 if ((ch != NULL) && (stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5408 if (stcb->asoc.pktdrop_supported == 0) {
5411 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5413 min(chk_length, contiguous));
5416 case SCTP_AUTHENTICATION:
5417 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5419 /* save the first AUTH for later processing */
5420 if (auth_skipped == 0) {
5421 auth_offset = *offset;
5422 auth_len = chk_length;
5425 /* skip this chunk (temporarily) */
5428 if (stcb->asoc.auth_supported == 0) {
5431 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5432 (chk_length > (sizeof(struct sctp_auth_chunk) +
5433 SCTP_AUTH_DIGEST_LEN_MAX))) {
5438 if (got_auth == 1) {
5439 /* skip this chunk... it's already auth'd */
5443 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5445 /* auth HMAC failed so dump the packet */
5449 /* remaining chunks are HMAC checked */
5450 stcb->asoc.authenticated = 1;
5456 /* it's an unknown chunk! */
5457 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5458 struct sctp_gen_error_cause *cause;
5461 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5462 0, M_NOWAIT, 1, MT_DATA);
5463 if (op_err != NULL) {
5464 len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset));
5465 cause = mtod(op_err, struct sctp_gen_error_cause *);
5466 cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5467 cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause)));
5468 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
5469 SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
5470 if (SCTP_BUF_NEXT(op_err) != NULL) {
5471 #ifdef SCTP_MBUF_LOGGING
5472 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5473 sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5476 sctp_queue_op_err(stcb, op_err);
5478 sctp_m_freem(op_err);
5482 if ((ch->chunk_type & 0x80) == 0) {
5483 /* discard this packet */
5486 } /* else skip this bad chunk and continue... */
5488 } /* switch (ch->chunk_type) */
5492 /* get the next chunk */
5493 *offset += SCTP_SIZE32(chk_length);
5494 if (*offset >= length) {
5495 /* no more data left in the mbuf chain */
5498 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5499 sizeof(struct sctp_chunkhdr), chunk_buf);
5506 if ((asconf_cnt > 0) && (stcb != NULL)) {
5507 sctp_send_asconf_ack(stcb);
5514 * common input chunk processing (v4 and v6)
5517 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5518 struct sockaddr *src, struct sockaddr *dst,
5519 struct sctphdr *sh, struct sctp_chunkhdr *ch,
5520 uint8_t compute_crc,
5522 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5523 uint32_t vrf_id, uint16_t port)
5526 int fwd_tsn_seen = 0, data_processed = 0;
5527 struct mbuf *m = *mm, *op_err;
5528 char msg[SCTP_DIAG_INFO_LEN];
5530 int cnt_ctrl_ready = 0;
5531 struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5532 struct sctp_tcb *stcb = NULL;
5533 struct sctp_nets *net = NULL;
5535 SCTP_STAT_INCR(sctps_recvdatagrams);
5536 #ifdef SCTP_AUDITING_ENABLED
5537 sctp_audit_log(0xE0, 1);
5538 sctp_auditing(0, inp, stcb, net);
5540 if (compute_crc != 0) {
5541 uint32_t check, calc_check;
5543 check = sh->checksum;
5545 calc_check = sctp_calculate_cksum(m, iphlen);
5546 sh->checksum = check;
5547 if (calc_check != check) {
5548 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5549 calc_check, check, (void *)m, length, iphlen);
5550 stcb = sctp_findassociation_addr(m, offset, src, dst,
5551 sh, ch, &inp, &net, vrf_id);
5552 #if defined(INET) || defined(INET6)
5553 if ((ch->chunk_type != SCTP_INITIATION) &&
5554 (net != NULL) && (net->port != port)) {
5555 if (net->port == 0) {
5556 /* UDP encapsulation turned on. */
5557 net->mtu -= sizeof(struct udphdr);
5558 if (stcb->asoc.smallest_mtu > net->mtu) {
5559 sctp_pathmtu_adjustment(stcb, net->mtu);
5561 } else if (port == 0) {
5562 /* UDP encapsulation turned off. */
5563 net->mtu += sizeof(struct udphdr);
5564 /* XXX Update smallest_mtu */
5570 net->flowtype = mflowtype;
5571 net->flowid = mflowid;
5573 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5574 if ((inp != NULL) && (stcb != NULL)) {
5575 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5576 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5577 } else if ((inp != NULL) && (stcb == NULL)) {
5580 SCTP_STAT_INCR(sctps_badsum);
5581 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5585 /* Destination port of 0 is illegal, based on RFC4960. */
5586 if (sh->dest_port == 0) {
5587 SCTP_STAT_INCR(sctps_hdrops);
5590 stcb = sctp_findassociation_addr(m, offset, src, dst,
5591 sh, ch, &inp, &net, vrf_id);
5592 #if defined(INET) || defined(INET6)
5593 if ((ch->chunk_type != SCTP_INITIATION) &&
5594 (net != NULL) && (net->port != port)) {
5595 if (net->port == 0) {
5596 /* UDP encapsulation turned on. */
5597 net->mtu -= sizeof(struct udphdr);
5598 if (stcb->asoc.smallest_mtu > net->mtu) {
5599 sctp_pathmtu_adjustment(stcb, net->mtu);
5601 } else if (port == 0) {
5602 /* UDP encapsulation turned off. */
5603 net->mtu += sizeof(struct udphdr);
5604 /* XXX Update smallest_mtu */
5610 net->flowtype = mflowtype;
5611 net->flowid = mflowid;
5614 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5615 SCTP_STAT_INCR(sctps_noport);
5616 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5619 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5620 sctp_send_shutdown_complete2(src, dst, sh,
5621 mflowtype, mflowid, fibnum,
5625 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5628 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5629 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5630 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5631 (ch->chunk_type != SCTP_INIT))) {
5632 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5634 sctp_send_abort(m, iphlen, src, dst,
5636 mflowtype, mflowid, fibnum,
5641 } else if (stcb == NULL) {
5644 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5645 (void *)m, iphlen, offset, length, (void *)stcb);
5647 /* always clear this before beginning a packet */
5648 stcb->asoc.authenticated = 0;
5649 stcb->asoc.seen_a_sack_this_pkt = 0;
5650 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5651 (void *)stcb, stcb->asoc.state);
5653 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5654 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5656 * If we hit here, we had a ref count
5657 * up when the assoc was aborted and the
5658 * timer is clearing out the assoc, we should
5659 * NOT respond to any packet.. its OOTB.
5661 SCTP_TCB_UNLOCK(stcb);
5663 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5664 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5665 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5667 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5668 mflowtype, mflowid, inp->fibnum,
5673 if (IS_SCTP_CONTROL(ch)) {
5674 /* process the control portion of the SCTP packet */
5675 /* sa_ignore NO_NULL_CHK */
5676 stcb = sctp_process_control(m, iphlen, &offset, length,
5678 inp, stcb, &net, &fwd_tsn_seen,
5679 mflowtype, mflowid, fibnum,
5683 * This covers us if the cookie-echo was there and
5684 * it changes our INP.
5686 inp = stcb->sctp_ep;
5687 #if defined(INET) || defined(INET6)
5688 if ((ch->chunk_type != SCTP_INITIATION) &&
5689 (net != NULL) && (net->port != port)) {
5690 if (net->port == 0) {
5691 /* UDP encapsulation turned on. */
5692 net->mtu -= sizeof(struct udphdr);
5693 if (stcb->asoc.smallest_mtu > net->mtu) {
5694 sctp_pathmtu_adjustment(stcb, net->mtu);
5696 } else if (port == 0) {
5697 /* UDP encapsulation turned off. */
5698 net->mtu += sizeof(struct udphdr);
5699 /* XXX Update smallest_mtu */
5707 * no control chunks, so pre-process DATA chunks (these
5708 * checks are taken care of by control processing)
5712 * if DATA only packet, and auth is required, then punt...
5713 * can't have authenticated without any AUTH (control)
5716 if ((stcb != NULL) &&
5717 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5718 /* "silently" ignore */
5719 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5720 SCTP_STAT_INCR(sctps_recvauthmissing);
5724 /* out of the blue DATA chunk */
5725 SCTP_PROBE5(receive, NULL, NULL, m, NULL, sh);
5726 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5727 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5729 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5730 mflowtype, mflowid, fibnum,
5734 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5735 /* v_tag mismatch! */
5736 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5737 SCTP_STAT_INCR(sctps_badvtag);
5742 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5745 * no valid TCB for this packet, or we found it's a bad
5746 * packet while processing control, or we're done with this
5747 * packet (done or skip rest of data), so we drop it...
5753 * DATA chunk processing
5755 /* plow through the data chunks while length > offset */
5758 * Rest should be DATA only. Check authentication state if AUTH for
5761 if ((length > offset) &&
5763 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5764 !stcb->asoc.authenticated) {
5765 /* "silently" ignore */
5766 SCTP_STAT_INCR(sctps_recvauthmissing);
5767 SCTPDBG(SCTP_DEBUG_AUTH1,
5768 "Data chunk requires AUTH, skipped\n");
5771 if (length > offset) {
5775 * First check to make sure our state is correct. We would
5776 * not get here unless we really did have a tag, so we don't
5777 * abort if this happens, just dump the chunk silently.
5779 switch (SCTP_GET_STATE(stcb)) {
5780 case SCTP_STATE_COOKIE_ECHOED:
5782 * we consider data with valid tags in this state
5783 * shows us the cookie-ack was lost. Imply it was
5786 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5788 case SCTP_STATE_COOKIE_WAIT:
5790 * We consider OOTB any data sent during asoc setup.
5792 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5793 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5795 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5796 mflowtype, mflowid, inp->fibnum,
5799 /* sa_ignore NOTREACHED */
5801 case SCTP_STATE_EMPTY: /* should not happen */
5802 case SCTP_STATE_INUSE: /* should not happen */
5803 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5804 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5807 /* sa_ignore NOTREACHED */
5809 case SCTP_STATE_OPEN:
5810 case SCTP_STATE_SHUTDOWN_SENT:
5813 /* plow through the data chunks while length > offset */
5814 retval = sctp_process_data(mm, iphlen, &offset, length,
5815 inp, stcb, net, &high_tsn);
5818 * The association aborted, NO UNLOCK needed since
5819 * the association is destroyed.
5826 * Anything important needs to have been m_copy'ed in
5831 /* take care of ecn */
5832 if ((data_processed == 1) &&
5833 (stcb->asoc.ecn_supported == 1) &&
5834 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5835 /* Yep, we need to add a ECNE */
5836 sctp_send_ecn_echo(stcb, net, high_tsn);
5839 if ((data_processed == 0) && (fwd_tsn_seen)) {
5841 uint32_t highest_tsn;
5843 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5844 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5846 highest_tsn = stcb->asoc.highest_tsn_inside_map;
5848 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
5849 stcb->asoc.send_sack = 1;
5850 sctp_sack_check(stcb, was_a_gap);
5851 } else if (fwd_tsn_seen) {
5852 stcb->asoc.send_sack = 1;
5854 /* trigger send of any chunks in queue... */
5856 #ifdef SCTP_AUDITING_ENABLED
5857 sctp_audit_log(0xE0, 2);
5858 sctp_auditing(1, inp, stcb, net);
5860 SCTPDBG(SCTP_DEBUG_INPUT1,
5861 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5862 stcb->asoc.peers_rwnd,
5863 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5864 stcb->asoc.total_flight);
5865 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5866 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5867 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5869 if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
5871 stcb->asoc.trigger_reset ||
5873 (stcb->asoc.peers_rwnd > 0 ||
5874 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5875 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5876 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5877 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5879 #ifdef SCTP_AUDITING_ENABLED
5880 sctp_audit_log(0xE0, 3);
5881 sctp_auditing(2, inp, stcb, net);
5885 SCTP_TCB_UNLOCK(stcb);
5887 if (inp_decr != NULL) {
5888 /* reduce ref-count */
5889 SCTP_INP_WLOCK(inp_decr);
5890 SCTP_INP_DECR_REF(inp_decr);
5891 SCTP_INP_WUNLOCK(inp_decr);
5898 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5902 uint32_t vrf_id = 0;
5904 struct sockaddr_in src, dst;
5907 struct sctp_chunkhdr *ch;
5909 uint8_t compute_crc;
5915 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
5916 SCTP_RELEASE_PKT(i_pak);
5919 m = SCTP_HEADER_TO_CHAIN(i_pak);
5920 #ifdef SCTP_MBUF_LOGGING
5921 /* Log in any input mbufs */
5922 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5923 sctp_log_mbc(m, SCTP_MBUF_INPUT);
5926 #ifdef SCTP_PACKET_LOGGING
5927 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
5931 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
5932 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
5934 if_name(m->m_pkthdr.rcvif),
5935 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
5936 mflowid = m->m_pkthdr.flowid;
5937 mflowtype = M_HASHTYPE_GET(m);
5938 fibnum = M_GETFIB(m);
5939 SCTP_STAT_INCR(sctps_recvpackets);
5940 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
5941 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
5942 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5943 if (SCTP_BUF_LEN(m) < offset) {
5944 if ((m = m_pullup(m, offset)) == NULL) {
5945 SCTP_STAT_INCR(sctps_hdrops);
5949 ip = mtod(m, struct ip *);
5950 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
5951 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
5952 offset -= sizeof(struct sctp_chunkhdr);
5953 memset(&src, 0, sizeof(struct sockaddr_in));
5954 src.sin_family = AF_INET;
5955 src.sin_len = sizeof(struct sockaddr_in);
5956 src.sin_port = sh->src_port;
5957 src.sin_addr = ip->ip_src;
5958 memset(&dst, 0, sizeof(struct sockaddr_in));
5959 dst.sin_family = AF_INET;
5960 dst.sin_len = sizeof(struct sockaddr_in);
5961 dst.sin_port = sh->dest_port;
5962 dst.sin_addr = ip->ip_dst;
5963 length = ntohs(ip->ip_len);
5964 /* Validate mbuf chain length with IP payload length. */
5965 if (SCTP_HEADER_LEN(m) != length) {
5966 SCTPDBG(SCTP_DEBUG_INPUT1,
5967 "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
5968 SCTP_STAT_INCR(sctps_hdrops);
5971 /* SCTP does not allow broadcasts or multicasts */
5972 if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
5975 if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
5978 ecn_bits = ip->ip_tos;
5979 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5980 SCTP_STAT_INCR(sctps_recvhwcrc);
5983 SCTP_STAT_INCR(sctps_recvswcrc);
5986 sctp_common_input_processing(&m, iphlen, offset, length,
5987 (struct sockaddr *)&src,
5988 (struct sockaddr *)&dst,
5992 mflowtype, mflowid, fibnum,
6001 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6002 extern int *sctp_cpuarry;
6006 sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
6013 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6019 uint32_t flowid, tag;
6021 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
6022 flowid = m->m_pkthdr.flowid;
6025 * No flow id built by lower layers fix it so we
6028 offset = off + sizeof(struct sctphdr);
6029 if (SCTP_BUF_LEN(m) < offset) {
6030 if ((m = m_pullup(m, offset)) == NULL) {
6031 SCTP_STAT_INCR(sctps_hdrops);
6032 return (IPPROTO_DONE);
6035 ip = mtod(m, struct ip *);
6036 sh = (struct sctphdr *)((caddr_t)ip + off);
6037 tag = htonl(sh->v_tag);
6038 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6039 m->m_pkthdr.flowid = flowid;
6040 M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH);
6042 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6043 sctp_queue_to_mcore(m, off, cpu_to_use);
6044 return (IPPROTO_DONE);
6047 sctp_input_with_port(m, off, 0);
6048 return (IPPROTO_DONE);