2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * a) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
14 * b) Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the distribution.
18 * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <netinet/sctp_os.h>
39 #include <netinet/sctp_var.h>
40 #include <netinet/sctp_sysctl.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctp_header.h>
43 #include <netinet/sctputil.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_input.h>
46 #include <netinet/sctp_auth.h>
47 #include <netinet/sctp_indata.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctp_bsd_addr.h>
50 #include <netinet/sctp_timer.h>
51 #include <netinet/sctp_crc32.h>
52 #if defined(INET) || defined(INET6)
53 #include <netinet/udp.h>
55 #include <netinet/in_kdtrace.h>
61 sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
63 struct sctp_nets *net;
66 * This now not only stops all cookie timers it also stops any INIT
67 * timers as well. This will make sure that the timers are stopped
68 * in all collision cases.
70 SCTP_TCB_LOCK_ASSERT(stcb);
71 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
72 if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
73 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
76 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
77 } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
78 sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
81 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
88 sctp_handle_init(struct mbuf *m, int iphlen, int offset,
89 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
90 struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
91 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_no_unlock,
92 uint8_t mflowtype, uint32_t mflowid,
93 uint32_t vrf_id, uint16_t port)
95 struct sctp_init *init;
98 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
103 /* validate length */
104 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
105 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
106 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
110 *abort_no_unlock = 1;
113 /* validate parameters */
115 if (init->initiate_tag == 0) {
116 /* protocol error... send abort */
117 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
118 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
122 *abort_no_unlock = 1;
125 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
126 /* invalid parameter... send abort */
127 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
128 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
132 *abort_no_unlock = 1;
135 if (init->num_inbound_streams == 0) {
136 /* protocol error... send abort */
137 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
138 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
142 *abort_no_unlock = 1;
145 if (init->num_outbound_streams == 0) {
146 /* protocol error... send abort */
147 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
148 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
152 *abort_no_unlock = 1;
155 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
156 offset + ntohs(cp->ch.chunk_length))) {
157 /* auth parameter(s) error... send abort */
158 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
159 "Problem with AUTH parameters");
160 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
164 *abort_no_unlock = 1;
167 /* We are only accepting if we have a listening socket. */
168 if ((stcb == NULL) &&
169 ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
170 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
171 (!SCTP_IS_LISTENING(inp)))) {
173 * FIX ME ?? What about TCP model and we have a
174 * match/restart case? Actually no fix is needed. the lookup
175 * will always find the existing assoc so stcb would not be
176 * NULL. It may be questionable to do this since we COULD
177 * just send back the INIT-ACK and hope that the app did
178 * accept()'s by the time the COOKIE was sent. But there is
179 * a price to pay for COOKIE generation and I don't want to
180 * pay it on the chance that the app will actually do some
181 * accepts(). The App just looses and should NOT be in this
184 if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
185 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
187 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
188 mflowtype, mflowid, inp->fibnum,
193 if ((stcb != NULL) &&
194 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
195 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
196 sctp_send_shutdown_ack(stcb, NULL);
197 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
199 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
200 sctp_send_initiate_ack(inp, stcb, net, m, iphlen, offset,
207 SCTP_INP_RUNLOCK(inp);
212 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
216 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
217 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
224 struct sctp_stream_queue_pending *sp;
225 struct sctp_association *asoc;
228 * This function returns if any stream has true unsent data on it.
229 * Note that as it looks through it will clean up any places that
230 * have old data that has been sent but left at top of stream queue.
234 SCTP_TCB_SEND_LOCK(stcb);
235 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
236 /* Check to see if some data queued */
237 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
238 /* sa_ignore FREED_MEMORY */
239 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
243 if ((sp->msg_is_complete) &&
245 (sp->sender_all_done)) {
247 * We are doing differed cleanup. Last time
248 * through when we took all the data the
249 * sender_all_done was not set.
251 if (sp->put_last_out == 0) {
252 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
253 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
259 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
260 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
261 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, &asoc->strmout[i], sp, 1);
263 sctp_free_remote_addr(sp->net);
267 sctp_m_freem(sp->data);
270 sctp_free_a_strmoq(stcb, sp, so_locked);
271 if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
277 if (unsent_data > 0) {
282 SCTP_TCB_SEND_UNLOCK(stcb);
283 return (unsent_data);
287 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
289 struct sctp_init *init;
290 struct sctp_association *asoc;
291 struct sctp_nets *lnet;
296 /* save off parameters */
297 asoc->peer_vtag = ntohl(init->initiate_tag);
298 asoc->peers_rwnd = ntohl(init->a_rwnd);
300 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
302 if (!TAILQ_EMPTY(&asoc->nets)) {
303 /* update any ssthresh's that may have a default */
304 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
305 lnet->ssthresh = asoc->peers_rwnd;
306 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
307 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
312 SCTP_TCB_SEND_LOCK(stcb);
313 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
315 struct sctp_stream_out *outs;
316 struct sctp_stream_queue_pending *sp, *nsp;
317 struct sctp_tmit_chunk *chk, *nchk;
319 /* abandon the upper streams */
320 newcnt = ntohs(init->num_inbound_streams);
321 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
322 if (chk->rec.data.sid >= newcnt) {
323 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
324 asoc->send_queue_cnt--;
325 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
326 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
329 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
332 if (chk->data != NULL) {
333 sctp_free_bufspace(stcb, asoc, chk, 1);
334 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
335 0, chk, SCTP_SO_NOT_LOCKED);
337 sctp_m_freem(chk->data);
341 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
342 /* sa_ignore FREED_MEMORY */
346 for (i = newcnt; i < asoc->pre_open_streams; i++) {
347 outs = &asoc->strmout[i];
348 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
349 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
350 TAILQ_REMOVE(&outs->outqueue, sp, next);
351 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
352 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
353 stcb, 0, sp, SCTP_SO_NOT_LOCKED);
355 sctp_m_freem(sp->data);
359 sctp_free_remote_addr(sp->net);
363 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
364 /* sa_ignore FREED_MEMORY */
366 outs->state = SCTP_STREAM_CLOSED;
369 /* cut back the count */
370 asoc->pre_open_streams = newcnt;
372 SCTP_TCB_SEND_UNLOCK(stcb);
373 asoc->streamoutcnt = asoc->pre_open_streams;
375 for (i = 0; i < asoc->streamoutcnt; i++) {
376 asoc->strmout[i].state = SCTP_STREAM_OPEN;
379 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
380 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
381 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
382 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
384 /* This is the next one we expect */
385 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
387 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
388 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
390 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
391 /* open the requested streams */
393 if (asoc->strmin != NULL) {
394 /* Free the old ones */
395 for (i = 0; i < asoc->streamincnt; i++) {
396 sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
397 sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
399 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
401 if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
402 asoc->streamincnt = ntohs(init->num_outbound_streams);
404 asoc->streamincnt = asoc->max_inbound_streams;
406 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
407 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
408 if (asoc->strmin == NULL) {
409 /* we didn't get memory for the streams! */
410 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
413 for (i = 0; i < asoc->streamincnt; i++) {
414 asoc->strmin[i].sid = i;
415 asoc->strmin[i].last_mid_delivered = 0xffffffff;
416 TAILQ_INIT(&asoc->strmin[i].inqueue);
417 TAILQ_INIT(&asoc->strmin[i].uno_inqueue);
418 asoc->strmin[i].pd_api_started = 0;
419 asoc->strmin[i].delivery_started = 0;
422 * load_address_from_init will put the addresses into the
423 * association when the COOKIE is processed or the INIT-ACK is
424 * processed. Both types of COOKIE's existing and new call this
425 * routine. It will remove addresses that are no longer in the
426 * association (for the restarting case where addresses are
427 * removed). Up front when the INIT arrives we will discard it if it
428 * is a restart and new addresses have been added.
430 /* sa_ignore MEMLEAK */
435 * INIT-ACK message processing/consumption returns value < 0 on error
438 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
439 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
440 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
441 struct sctp_nets *net, int *abort_no_unlock,
442 uint8_t mflowtype, uint32_t mflowid,
445 struct sctp_association *asoc;
447 int retval, abort_flag, cookie_found;
449 int nat_friendly = 0;
451 /* First verify that we have no illegal param's */
455 op_err = sctp_arethere_unrecognized_parameters(m,
456 (offset + sizeof(struct sctp_init_chunk)),
457 &abort_flag, (struct sctp_chunkhdr *)cp,
458 &nat_friendly, &cookie_found);
460 /* Send an abort and notify peer */
461 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
462 *abort_no_unlock = 1;
468 /* Only report the missing cookie parameter */
469 if (op_err != NULL) {
470 sctp_m_freem(op_err);
472 len = (uint16_t)(sizeof(struct sctp_error_missing_param) + sizeof(uint16_t));
473 /* We abort with an error of missing mandatory param */
474 op_err = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
475 if (op_err != NULL) {
476 struct sctp_error_missing_param *cause;
478 SCTP_BUF_LEN(op_err) = len;
479 cause = mtod(op_err, struct sctp_error_missing_param *);
480 /* Subtract the reserved param */
481 cause->cause.code = htons(SCTP_CAUSE_MISSING_PARAM);
482 cause->cause.length = htons(len);
483 cause->num_missing_params = htonl(1);
484 cause->type[0] = htons(SCTP_STATE_COOKIE);
486 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
487 src, dst, sh, op_err,
490 *abort_no_unlock = 1;
494 asoc->peer_supports_nat = (uint8_t)nat_friendly;
495 /* process the peer's parameters in the INIT-ACK */
496 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
500 initack_limit = offset + ntohs(cp->ch.chunk_length);
501 /* load all addresses */
502 if ((retval = sctp_load_addresses_from_init(stcb, m,
503 (offset + sizeof(struct sctp_init_chunk)), initack_limit,
504 src, dst, NULL, stcb->asoc.port))) {
505 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
506 "Problem with address parameters");
507 SCTPDBG(SCTP_DEBUG_INPUT1,
508 "Load addresses from INIT causes an abort %d\n",
510 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
511 src, dst, sh, op_err,
514 *abort_no_unlock = 1;
517 /* if the peer doesn't support asconf, flush the asconf queue */
518 if (asoc->asconf_supported == 0) {
519 struct sctp_asconf_addr *param, *nparam;
521 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
522 TAILQ_REMOVE(&asoc->asconf_queue, param, next);
523 SCTP_FREE(param, SCTP_M_ASC_ADDR);
527 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
528 stcb->asoc.local_hmacs);
530 sctp_queue_op_err(stcb, op_err);
531 /* queuing will steal away the mbuf chain to the out queue */
534 /* extract the cookie and queue it to "echo" it back... */
535 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
536 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
537 stcb->asoc.overall_error_count,
539 SCTP_FROM_SCTP_INPUT,
542 stcb->asoc.overall_error_count = 0;
543 net->error_count = 0;
546 * Cancel the INIT timer, We do this first before queueing the
547 * cookie. We always cancel at the primary to assue that we are
548 * canceling the timer started by the INIT which always goes to the
551 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
552 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
554 /* calculate the RTO */
555 sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
556 SCTP_RTT_FROM_NON_DATA);
557 retval = sctp_send_cookie_echo(m, offset, initack_limit, stcb, net);
562 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
563 struct sctp_tcb *stcb, struct sctp_nets *net)
565 union sctp_sockstore store;
566 struct sctp_nets *r_net, *f_net;
569 uint16_t old_error_counter;
571 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
576 memset(&store, 0, sizeof(store));
577 switch (cp->heartbeat.hb_info.addr_family) {
580 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
581 store.sin.sin_family = cp->heartbeat.hb_info.addr_family;
582 store.sin.sin_len = cp->heartbeat.hb_info.addr_len;
583 store.sin.sin_port = stcb->rport;
584 memcpy(&store.sin.sin_addr, cp->heartbeat.hb_info.address,
585 sizeof(store.sin.sin_addr));
593 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
594 store.sin6.sin6_family = cp->heartbeat.hb_info.addr_family;
595 store.sin6.sin6_len = cp->heartbeat.hb_info.addr_len;
596 store.sin6.sin6_port = stcb->rport;
597 memcpy(&store.sin6.sin6_addr, cp->heartbeat.hb_info.address, sizeof(struct in6_addr));
606 r_net = sctp_findnet(stcb, &store.sa);
608 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
611 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
612 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
613 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
615 * If the its a HB and it's random value is correct when can
616 * confirm the destination.
618 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
619 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
620 stcb->asoc.primary_destination = r_net;
621 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
622 f_net = TAILQ_FIRST(&stcb->asoc.nets);
623 if (f_net != r_net) {
625 * first one on the list is NOT the primary
626 * sctp_cmpaddr() is much more efficient if
627 * the primary is the first on the list,
630 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
631 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
635 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
636 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
637 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
638 r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
639 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
641 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
642 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
643 stcb->asoc.overall_error_count,
645 SCTP_FROM_SCTP_INPUT,
648 stcb->asoc.overall_error_count = 0;
649 old_error_counter = r_net->error_count;
650 r_net->error_count = 0;
651 r_net->hb_responded = 1;
652 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
653 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
654 /* Now lets do a RTO with this */
655 sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv,
656 SCTP_RTT_FROM_NON_DATA);
657 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
658 r_net->dest_state |= SCTP_ADDR_REACHABLE;
659 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
660 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
662 if (r_net->dest_state & SCTP_ADDR_PF) {
663 r_net->dest_state &= ~SCTP_ADDR_PF;
664 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
666 if (old_error_counter > 0) {
667 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
668 stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
669 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
671 if (r_net == stcb->asoc.primary_destination) {
672 if (stcb->asoc.alternate) {
673 /* release the alternate, primary is good */
674 sctp_free_remote_addr(stcb->asoc.alternate);
675 stcb->asoc.alternate = NULL;
678 /* Mobility adaptation */
680 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
681 SCTP_MOBILITY_BASE) ||
682 sctp_is_mobility_feature_on(stcb->sctp_ep,
683 SCTP_MOBILITY_FASTHANDOFF)) &&
684 sctp_is_mobility_feature_on(stcb->sctp_ep,
685 SCTP_MOBILITY_PRIM_DELETED)) {
687 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
688 stcb->sctp_ep, stcb, NULL,
689 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
690 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
691 SCTP_MOBILITY_FASTHANDOFF)) {
692 sctp_assoc_immediate_retrans(stcb,
693 stcb->asoc.primary_destination);
695 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
696 SCTP_MOBILITY_BASE)) {
697 sctp_move_chunks_from_net(stcb,
698 stcb->asoc.deleted_primary);
700 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
701 stcb->asoc.deleted_primary);
707 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
710 * Return 0 means we want you to proceed with the abort non-zero
711 * means no abort processing.
714 struct sctpasochead *head;
716 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
717 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
718 new_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
719 atomic_add_int(&stcb->asoc.refcnt, 1);
720 SCTP_TCB_UNLOCK(stcb);
721 SCTP_INP_INFO_WLOCK();
723 atomic_subtract_int(&stcb->asoc.refcnt, 1);
727 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
728 /* generate a new vtag and send init */
729 LIST_REMOVE(stcb, sctp_asocs);
730 stcb->asoc.my_vtag = new_vtag;
731 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
733 * put it in the bucket in the vtag hash of assoc's for the
736 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
737 SCTP_INP_INFO_WUNLOCK();
738 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
742 * treat like a case where the cookie expired i.e.: - dump
743 * current cookie. - generate a new vtag. - resend init.
745 /* generate a new vtag and send init */
746 LIST_REMOVE(stcb, sctp_asocs);
747 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
748 sctp_stop_all_cookie_timers(stcb);
749 sctp_toss_old_cookies(stcb, &stcb->asoc);
750 stcb->asoc.my_vtag = new_vtag;
751 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
753 * put it in the bucket in the vtag hash of assoc's for the
756 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
757 SCTP_INP_INFO_WUNLOCK();
758 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
765 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
766 struct sctp_nets *net)
769 * return 0 means we want you to proceed with the abort non-zero
770 * means no abort processing
772 if (stcb->asoc.auth_supported == 0) {
773 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
776 sctp_asconf_send_nat_state_update(stcb, net);
781 /* Returns 1 if the stcb was aborted, 0 otherwise */
783 sctp_handle_abort(struct sctp_abort_chunk *abort,
784 struct sctp_tcb *stcb, struct sctp_nets *net)
786 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
792 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
796 len = ntohs(abort->ch.chunk_length);
797 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_error_cause)) {
799 * Need to check the cause codes for our two magic nat
800 * aborts which don't kill the assoc necessarily.
802 struct sctp_error_cause *cause;
804 cause = (struct sctp_error_cause *)(abort + 1);
805 error = ntohs(cause->code);
806 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
807 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
808 abort->ch.chunk_flags);
809 if (sctp_handle_nat_colliding_state(stcb)) {
812 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
813 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
814 abort->ch.chunk_flags);
815 if (sctp_handle_nat_missing_state(stcb, net)) {
822 /* stop any receive timers */
823 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net,
824 SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
825 /* notify user of the abort and clean up... */
826 sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
828 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
829 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
830 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
831 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
833 #ifdef SCTP_ASOCLOG_OF_TSNS
834 sctp_print_out_track_log(stcb);
836 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
837 so = SCTP_INP_SO(stcb->sctp_ep);
838 atomic_add_int(&stcb->asoc.refcnt, 1);
839 SCTP_TCB_UNLOCK(stcb);
840 SCTP_SOCKET_LOCK(so, 1);
842 atomic_subtract_int(&stcb->asoc.refcnt, 1);
844 (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 sctp_calculate_rto(stcb, asoc, net, &old,
1684 SCTP_RTT_FROM_NON_DATA);
1686 if (stcb->asoc.sctp_autoclose_ticks &&
1687 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1688 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1694 * we're in the OPEN state (or beyond), so peer must
1695 * have simply lost the COOKIE-ACK
1699 sctp_stop_all_cookie_timers(stcb);
1701 * We ignore the return code here.. not sure if we should
1702 * somehow abort.. but we do have an existing asoc. This
1703 * really should not fail.
1705 if (sctp_load_addresses_from_init(stcb, m,
1706 init_offset + sizeof(struct sctp_init_chunk),
1707 initack_offset, src, dst, init_src, stcb->asoc.port)) {
1708 if (how_indx < sizeof(asoc->cookie_how))
1709 asoc->cookie_how[how_indx] = 4;
1712 /* respond with a COOKIE-ACK */
1713 sctp_toss_old_cookies(stcb, asoc);
1714 sctp_send_cookie_ack(stcb);
1715 if (how_indx < sizeof(asoc->cookie_how))
1716 asoc->cookie_how[how_indx] = 5;
1720 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1721 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1722 cookie->tie_tag_my_vtag == 0 &&
1723 cookie->tie_tag_peer_vtag == 0) {
1725 * case C in Section 5.2.4 Table 2: XMOO silently discard
1727 if (how_indx < sizeof(asoc->cookie_how))
1728 asoc->cookie_how[how_indx] = 6;
1732 * If nat support, and the below and stcb is established, send back
1733 * a ABORT(colliding state) if we are established.
1735 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) &&
1736 (asoc->peer_supports_nat) &&
1737 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1738 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1739 (asoc->peer_vtag == 0)))) {
1741 * Special case - Peer's support nat. We may have two init's
1742 * that we gave out the same tag on since one was not
1743 * established.. i.e. we get INIT from host-1 behind the nat
1744 * and we respond tag-a, we get a INIT from host-2 behind
1745 * the nat and we get tag-a again. Then we bring up host-1
1746 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1747 * Now we have colliding state. We must send an abort here
1748 * with colliding state indication.
1750 op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
1751 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1752 mflowtype, mflowid, inp->fibnum,
1756 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1757 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1758 (asoc->peer_vtag == 0))) {
1760 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1761 * should be ok, re-accept peer info
1763 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1765 * Extension of case C. If we hit this, then the
1766 * random number generator returned the same vtag
1767 * when we first sent our INIT-ACK and when we later
1768 * sent our INIT. The side with the seq numbers that
1769 * are different will be the one that normnally
1770 * would have hit case C. This in effect "extends"
1771 * our vtags in this collision case to be 64 bits.
1772 * The same collision could occur aka you get both
1773 * vtag and seq number the same twice in a row.. but
1774 * is much less likely. If it did happen then we
1775 * would proceed through and bring up the assoc.. we
1776 * may end up with the wrong stream setup however..
1777 * which would be bad.. but there is no way to
1778 * tell.. until we send on a stream that does not
1781 if (how_indx < sizeof(asoc->cookie_how))
1782 asoc->cookie_how[how_indx] = 7;
1786 if (how_indx < sizeof(asoc->cookie_how))
1787 asoc->cookie_how[how_indx] = 8;
1788 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1789 SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1790 sctp_stop_all_cookie_timers(stcb);
1792 * since we did not send a HB make sure we don't double
1795 net->hb_responded = 1;
1796 if (stcb->asoc.sctp_autoclose_ticks &&
1797 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1798 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1801 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1802 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1804 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1806 * Ok the peer probably discarded our data (if we
1807 * echoed a cookie+data). So anything on the
1808 * sent_queue should be marked for retransmit, we
1809 * may not get something to kick us so it COULD
1810 * still take a timeout to move these.. but it can't
1811 * hurt to mark them.
1813 struct sctp_tmit_chunk *chk;
1815 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1816 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1817 chk->sent = SCTP_DATAGRAM_RESEND;
1818 sctp_flight_size_decrease(chk);
1819 sctp_total_flight_decrease(stcb, chk);
1820 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1826 /* process the INIT info (peer's info) */
1827 retval = sctp_process_init(init_cp, stcb);
1829 if (how_indx < sizeof(asoc->cookie_how))
1830 asoc->cookie_how[how_indx] = 9;
1833 if (sctp_load_addresses_from_init(stcb, m,
1834 init_offset + sizeof(struct sctp_init_chunk),
1835 initack_offset, src, dst, init_src, stcb->asoc.port)) {
1836 if (how_indx < sizeof(asoc->cookie_how))
1837 asoc->cookie_how[how_indx] = 10;
1840 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
1841 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
1842 *notification = SCTP_NOTIFY_ASSOC_UP;
1844 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1845 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1846 (!SCTP_IS_LISTENING(inp))) {
1847 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1850 stcb->sctp_ep->sctp_flags |=
1851 SCTP_PCB_FLAGS_CONNECTED;
1852 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1853 so = SCTP_INP_SO(stcb->sctp_ep);
1854 atomic_add_int(&stcb->asoc.refcnt, 1);
1855 SCTP_TCB_UNLOCK(stcb);
1856 SCTP_SOCKET_LOCK(so, 1);
1857 SCTP_TCB_LOCK(stcb);
1858 atomic_add_int(&stcb->asoc.refcnt, -1);
1859 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1860 SCTP_SOCKET_UNLOCK(so, 1);
1864 soisconnected(stcb->sctp_socket);
1865 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1866 SCTP_SOCKET_UNLOCK(so, 1);
1869 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)
1870 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1872 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1873 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1874 } else if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
1875 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1877 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1879 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1880 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1881 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1882 stcb->sctp_ep, stcb, asoc->primary_destination);
1884 sctp_stop_all_cookie_timers(stcb);
1885 sctp_toss_old_cookies(stcb, asoc);
1886 sctp_send_cookie_ack(stcb);
1889 * only if we have retrans set do we do this. What
1890 * this call does is get only the COOKIE-ACK out and
1891 * then when we return the normal call to
1892 * sctp_chunk_output will get the retrans out behind
1895 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1897 if (how_indx < sizeof(asoc->cookie_how))
1898 asoc->cookie_how[how_indx] = 11;
1902 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1903 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1904 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1905 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1906 cookie->tie_tag_peer_vtag != 0) {
1907 struct sctpasochead *head;
1908 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1912 if (asoc->peer_supports_nat) {
1914 * This is a gross gross hack. Just call the
1915 * cookie_new code since we are allowing a duplicate
1916 * association. I hope this works...
1918 return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
1919 sh, cookie, cookie_len,
1920 inp, netp, init_src, notification,
1921 auth_skipped, auth_offset, auth_len,
1926 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1929 if (how_indx < sizeof(asoc->cookie_how))
1930 asoc->cookie_how[how_indx] = 12;
1931 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
1932 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1933 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1934 SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
1936 /* notify upper layer */
1937 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1938 atomic_add_int(&stcb->asoc.refcnt, 1);
1939 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_OPEN) &&
1940 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1941 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) {
1942 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1944 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
1945 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1946 } else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
1947 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1949 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1950 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1951 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1952 stcb->sctp_ep, stcb, asoc->primary_destination);
1954 } else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
1955 /* move to OPEN state, if not in SHUTDOWN_SENT */
1956 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1958 asoc->pre_open_streams =
1959 ntohs(initack_cp->init.num_outbound_streams);
1960 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1961 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1962 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1964 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1966 asoc->str_reset_seq_in = asoc->init_seq_number;
1968 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1969 if (asoc->mapping_array) {
1970 memset(asoc->mapping_array, 0,
1971 asoc->mapping_array_size);
1973 if (asoc->nr_mapping_array) {
1974 memset(asoc->nr_mapping_array, 0,
1975 asoc->mapping_array_size);
1977 SCTP_TCB_UNLOCK(stcb);
1978 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1979 so = SCTP_INP_SO(stcb->sctp_ep);
1980 SCTP_SOCKET_LOCK(so, 1);
1982 SCTP_INP_INFO_WLOCK();
1983 SCTP_INP_WLOCK(stcb->sctp_ep);
1984 SCTP_TCB_LOCK(stcb);
1985 atomic_add_int(&stcb->asoc.refcnt, -1);
1986 /* send up all the data */
1987 SCTP_TCB_SEND_LOCK(stcb);
1989 sctp_report_all_outbound(stcb, 0, SCTP_SO_LOCKED);
1990 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1991 stcb->asoc.strmout[i].chunks_on_queues = 0;
1992 #if defined(SCTP_DETAILED_STR_STATS)
1993 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1994 asoc->strmout[i].abandoned_sent[j] = 0;
1995 asoc->strmout[i].abandoned_unsent[j] = 0;
1998 asoc->strmout[i].abandoned_sent[0] = 0;
1999 asoc->strmout[i].abandoned_unsent[0] = 0;
2001 stcb->asoc.strmout[i].sid = i;
2002 stcb->asoc.strmout[i].next_mid_ordered = 0;
2003 stcb->asoc.strmout[i].next_mid_unordered = 0;
2004 stcb->asoc.strmout[i].last_msg_incomplete = 0;
2006 /* process the INIT-ACK info (my info) */
2007 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2008 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2010 /* pull from vtag hash */
2011 LIST_REMOVE(stcb, sctp_asocs);
2012 /* re-insert to new vtag position */
2013 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
2014 SCTP_BASE_INFO(hashasocmark))];
2016 * put it in the bucket in the vtag hash of assoc's for the
2019 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
2021 SCTP_TCB_SEND_UNLOCK(stcb);
2022 SCTP_INP_WUNLOCK(stcb->sctp_ep);
2023 SCTP_INP_INFO_WUNLOCK();
2024 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2025 SCTP_SOCKET_UNLOCK(so, 1);
2027 asoc->total_flight = 0;
2028 asoc->total_flight_count = 0;
2029 /* process the INIT info (peer's info) */
2030 retval = sctp_process_init(init_cp, stcb);
2032 if (how_indx < sizeof(asoc->cookie_how))
2033 asoc->cookie_how[how_indx] = 13;
2038 * since we did not send a HB make sure we don't double
2041 net->hb_responded = 1;
2043 if (sctp_load_addresses_from_init(stcb, m,
2044 init_offset + sizeof(struct sctp_init_chunk),
2045 initack_offset, src, dst, init_src, stcb->asoc.port)) {
2046 if (how_indx < sizeof(asoc->cookie_how))
2047 asoc->cookie_how[how_indx] = 14;
2051 /* respond with a COOKIE-ACK */
2052 sctp_stop_all_cookie_timers(stcb);
2053 sctp_toss_old_cookies(stcb, asoc);
2054 sctp_send_cookie_ack(stcb);
2055 if (how_indx < sizeof(asoc->cookie_how))
2056 asoc->cookie_how[how_indx] = 15;
2060 if (how_indx < sizeof(asoc->cookie_how))
2061 asoc->cookie_how[how_indx] = 16;
2062 /* all other cases... */
2068 * handle a state cookie for a new association m: input packet mbuf chain--
2069 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
2070 * and the cookie signature does not exist offset: offset into mbuf to the
2071 * cookie-echo chunk length: length of the cookie chunk to: where the init
2072 * was from returns a new TCB
2074 static struct sctp_tcb *
2075 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
2076 struct sockaddr *src, struct sockaddr *dst,
2077 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
2078 struct sctp_inpcb *inp, struct sctp_nets **netp,
2079 struct sockaddr *init_src, int *notification,
2080 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2081 uint8_t mflowtype, uint32_t mflowid,
2082 uint32_t vrf_id, uint16_t port)
2084 struct sctp_tcb *stcb;
2085 struct sctp_init_chunk *init_cp, init_buf;
2086 struct sctp_init_ack_chunk *initack_cp, initack_buf;
2087 union sctp_sockstore store;
2088 struct sctp_association *asoc;
2089 int init_offset, initack_offset, initack_limit;
2092 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
2093 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2096 so = SCTP_INP_SO(inp);
2100 * find and validate the INIT chunk in the cookie (peer's info) the
2101 * INIT should start after the cookie-echo header struct (chunk
2102 * header, state cookie header struct)
2104 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2105 init_cp = (struct sctp_init_chunk *)
2106 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2107 (uint8_t *)&init_buf);
2108 if (init_cp == NULL) {
2109 /* could not pull a INIT chunk in cookie */
2110 SCTPDBG(SCTP_DEBUG_INPUT1,
2111 "process_cookie_new: could not pull INIT chunk hdr\n");
2114 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2115 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2118 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2120 * find and validate the INIT-ACK chunk in the cookie (my info) the
2121 * INIT-ACK follows the INIT chunk
2123 initack_cp = (struct sctp_init_ack_chunk *)
2124 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2125 (uint8_t *)&initack_buf);
2126 if (initack_cp == NULL) {
2127 /* could not pull INIT-ACK chunk in cookie */
2128 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2131 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2135 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2136 * "initack_limit" value. This is because the chk_length field
2137 * includes the length of the cookie, but the cookie is omitted when
2138 * the INIT and INIT_ACK are tacked onto the cookie...
2140 initack_limit = offset + cookie_len;
2143 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2148 * Here we do a trick, we set in NULL for the proc/thread argument.
2149 * We do this since in effect we only use the p argument when the
2150 * socket is unbound and we must do an implicit bind. Since we are
2151 * getting a cookie, we cannot be unbound.
2153 stcb = sctp_aloc_assoc(inp, init_src, &error,
2154 ntohl(initack_cp->init.initiate_tag), vrf_id,
2155 ntohs(initack_cp->init.num_outbound_streams),
2157 (struct thread *)NULL,
2158 SCTP_DONT_INITIALIZE_AUTH_PARAMS);
2160 struct mbuf *op_err;
2162 /* memory problem? */
2163 SCTPDBG(SCTP_DEBUG_INPUT1,
2164 "process_cookie_new: no room for another TCB!\n");
2165 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2166 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2167 src, dst, sh, op_err,
2172 /* get the correct sctp_nets */
2174 *netp = sctp_findnet(stcb, init_src);
2177 /* get scope variables out of cookie */
2178 asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2179 asoc->scope.site_scope = cookie->site_scope;
2180 asoc->scope.local_scope = cookie->local_scope;
2181 asoc->scope.loopback_scope = cookie->loopback_scope;
2183 if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2184 (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2185 struct mbuf *op_err;
2188 * Houston we have a problem. The EP changed while the
2189 * cookie was in flight. Only recourse is to abort the
2192 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2193 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2194 src, dst, sh, op_err,
2197 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2198 atomic_add_int(&stcb->asoc.refcnt, 1);
2199 SCTP_TCB_UNLOCK(stcb);
2200 SCTP_SOCKET_LOCK(so, 1);
2201 SCTP_TCB_LOCK(stcb);
2202 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2204 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2205 SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2206 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2207 SCTP_SOCKET_UNLOCK(so, 1);
2211 /* process the INIT-ACK info (my info) */
2212 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2213 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2214 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2215 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2216 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2217 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2218 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2219 asoc->str_reset_seq_in = asoc->init_seq_number;
2221 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2223 /* process the INIT info (peer's info) */
2225 retval = sctp_process_init(init_cp, stcb);
2229 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2230 atomic_add_int(&stcb->asoc.refcnt, 1);
2231 SCTP_TCB_UNLOCK(stcb);
2232 SCTP_SOCKET_LOCK(so, 1);
2233 SCTP_TCB_LOCK(stcb);
2234 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2236 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2237 SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2238 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2239 SCTP_SOCKET_UNLOCK(so, 1);
2243 /* load all addresses */
2244 if (sctp_load_addresses_from_init(stcb, m,
2245 init_offset + sizeof(struct sctp_init_chunk), initack_offset,
2246 src, dst, init_src, port)) {
2247 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2248 atomic_add_int(&stcb->asoc.refcnt, 1);
2249 SCTP_TCB_UNLOCK(stcb);
2250 SCTP_SOCKET_LOCK(so, 1);
2251 SCTP_TCB_LOCK(stcb);
2252 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2254 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2255 SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2256 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2257 SCTP_SOCKET_UNLOCK(so, 1);
2262 * verify any preceding AUTH chunk that was skipped
2264 /* pull the local authentication parameters from the cookie/init-ack */
2265 sctp_auth_get_cookie_params(stcb, m,
2266 initack_offset + sizeof(struct sctp_init_ack_chunk),
2267 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2269 struct sctp_auth_chunk *auth;
2271 auth = (struct sctp_auth_chunk *)
2272 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2273 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2274 /* auth HMAC failed, dump the assoc and packet */
2275 SCTPDBG(SCTP_DEBUG_AUTH1,
2276 "COOKIE-ECHO: AUTH failed\n");
2277 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2278 atomic_add_int(&stcb->asoc.refcnt, 1);
2279 SCTP_TCB_UNLOCK(stcb);
2280 SCTP_SOCKET_LOCK(so, 1);
2281 SCTP_TCB_LOCK(stcb);
2282 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2284 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2285 SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2286 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2287 SCTP_SOCKET_UNLOCK(so, 1);
2291 /* remaining chunks checked... good to go */
2292 stcb->asoc.authenticated = 1;
2297 * if we're doing ASCONFs, check to see if we have any new local
2298 * addresses that need to get added to the peer (eg. addresses
2299 * changed while cookie echo in flight). This needs to be done
2300 * after we go to the OPEN state to do the correct asconf
2301 * processing. else, make sure we have the correct addresses in our
2305 /* warning, we re-use sin, sin6, sa_store here! */
2306 /* pull in local_address (our "from" address) */
2307 switch (cookie->laddr_type) {
2309 case SCTP_IPV4_ADDRESS:
2310 /* source addr is IPv4 */
2311 memset(&store.sin, 0, sizeof(struct sockaddr_in));
2312 store.sin.sin_family = AF_INET;
2313 store.sin.sin_len = sizeof(struct sockaddr_in);
2314 store.sin.sin_addr.s_addr = cookie->laddress[0];
2318 case SCTP_IPV6_ADDRESS:
2319 /* source addr is IPv6 */
2320 memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
2321 store.sin6.sin6_family = AF_INET6;
2322 store.sin6.sin6_len = sizeof(struct sockaddr_in6);
2323 store.sin6.sin6_scope_id = cookie->scope_id;
2324 memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2328 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2329 atomic_add_int(&stcb->asoc.refcnt, 1);
2330 SCTP_TCB_UNLOCK(stcb);
2331 SCTP_SOCKET_LOCK(so, 1);
2332 SCTP_TCB_LOCK(stcb);
2333 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2335 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2336 SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2337 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2338 SCTP_SOCKET_UNLOCK(so, 1);
2343 /* update current state */
2344 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2345 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
2346 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2347 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2348 stcb->sctp_ep, stcb, asoc->primary_destination);
2350 sctp_stop_all_cookie_timers(stcb);
2351 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2352 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2354 /* set up to notify upper layer */
2355 *notification = SCTP_NOTIFY_ASSOC_UP;
2356 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2357 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2358 (!SCTP_IS_LISTENING(inp))) {
2360 * This is an endpoint that called connect() how it got a
2361 * cookie that is NEW is a bit of a mystery. It must be that
2362 * the INIT was sent, but before it got there.. a complete
2363 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2364 * should have went to the other code.. not here.. oh well..
2365 * a bit of protection is worth having..
2367 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2368 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2369 atomic_add_int(&stcb->asoc.refcnt, 1);
2370 SCTP_TCB_UNLOCK(stcb);
2371 SCTP_SOCKET_LOCK(so, 1);
2372 SCTP_TCB_LOCK(stcb);
2373 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2374 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2375 SCTP_SOCKET_UNLOCK(so, 1);
2379 soisconnected(stcb->sctp_socket);
2380 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2381 SCTP_SOCKET_UNLOCK(so, 1);
2383 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2384 (SCTP_IS_LISTENING(inp))) {
2386 * We don't want to do anything with this one. Since it is
2387 * the listening guy. The timer will get started for
2388 * accepted connections in the caller.
2392 /* since we did not send a HB make sure we don't double things */
2393 if ((netp) && (*netp))
2394 (*netp)->hb_responded = 1;
2396 if (stcb->asoc.sctp_autoclose_ticks &&
2397 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2398 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2400 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2401 if ((netp != NULL) && (*netp != NULL)) {
2404 /* calculate the RTT and set the encaps port */
2405 old.tv_sec = cookie->time_entered.tv_sec;
2406 old.tv_usec = cookie->time_entered.tv_usec;
2407 sctp_calculate_rto(stcb, asoc, *netp, &old, SCTP_RTT_FROM_NON_DATA);
2409 /* respond with a COOKIE-ACK */
2410 sctp_send_cookie_ack(stcb);
2413 * check the address lists for any ASCONFs that need to be sent
2414 * AFTER the cookie-ack is sent
2416 sctp_check_address_list(stcb, m,
2417 initack_offset + sizeof(struct sctp_init_ack_chunk),
2418 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2419 &store.sa, cookie->local_scope, cookie->site_scope,
2420 cookie->ipv4_scope, cookie->loopback_scope);
2427 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2428 * we NEED to make sure we are not already using the vtag. If so we
2429 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2430 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2431 SCTP_BASE_INFO(hashasocmark))];
2432 LIST_FOREACH(stcb, head, sctp_asocs) {
2433 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2434 -- SEND ABORT - TRY AGAIN --
2440 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2441 * existing (non-NULL) TCB
2443 static struct mbuf *
2444 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2445 struct sockaddr *src, struct sockaddr *dst,
2446 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2447 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2448 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2449 struct sctp_tcb **locked_tcb,
2450 uint8_t mflowtype, uint32_t mflowid,
2451 uint32_t vrf_id, uint16_t port)
2453 struct sctp_state_cookie *cookie;
2454 struct sctp_tcb *l_stcb = *stcb;
2455 struct sctp_inpcb *l_inp;
2456 struct sockaddr *to;
2457 struct sctp_pcb *ep;
2459 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2461 uint8_t cookie_ok = 0;
2462 unsigned int sig_offset, cookie_offset;
2463 unsigned int cookie_len;
2465 struct timeval time_expires;
2466 int notification = 0;
2467 struct sctp_nets *netl;
2468 int had_a_existing_tcb = 0;
2469 int send_int_conf = 0;
2471 struct sockaddr_in sin;
2474 struct sockaddr_in6 sin6;
2477 SCTPDBG(SCTP_DEBUG_INPUT2,
2478 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2480 if (inp_p == NULL) {
2483 cookie = &cp->cookie;
2484 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2485 cookie_len = ntohs(cp->ch.chunk_length);
2487 if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2488 sizeof(struct sctp_init_chunk) +
2489 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2490 /* cookie too small */
2493 if ((cookie->peerport != sh->src_port) ||
2494 (cookie->myport != sh->dest_port) ||
2495 (cookie->my_vtag != sh->v_tag)) {
2497 * invalid ports or bad tag. Note that we always leave the
2498 * v_tag in the header in network order and when we stored
2499 * it in the my_vtag slot we also left it in network order.
2500 * This maintains the match even though it may be in the
2501 * opposite byte order of the machine :->
2506 * split off the signature into its own mbuf (since it should not be
2507 * calculated in the sctp_hmac_m() call).
2509 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2510 m_sig = m_split(m, sig_offset, M_NOWAIT);
2511 if (m_sig == NULL) {
2512 /* out of memory or ?? */
2515 #ifdef SCTP_MBUF_LOGGING
2516 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2517 sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
2522 * compute the signature/digest for the cookie
2524 ep = &(*inp_p)->sctp_ep;
2527 SCTP_TCB_UNLOCK(l_stcb);
2529 SCTP_INP_RLOCK(l_inp);
2531 SCTP_TCB_LOCK(l_stcb);
2533 /* which cookie is it? */
2534 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2535 (ep->current_secret_number != ep->last_secret_number)) {
2536 /* it's the old cookie */
2537 (void)sctp_hmac_m(SCTP_HMAC,
2538 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2539 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2541 /* it's the current cookie */
2542 (void)sctp_hmac_m(SCTP_HMAC,
2543 (uint8_t *)ep->secret_key[(int)ep->current_secret_number],
2544 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2546 /* get the signature */
2547 SCTP_INP_RUNLOCK(l_inp);
2548 sig = (uint8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *)&tmp_sig);
2550 /* couldn't find signature */
2551 sctp_m_freem(m_sig);
2554 /* compare the received digest with the computed digest */
2555 if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2556 /* try the old cookie? */
2557 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2558 (ep->current_secret_number != ep->last_secret_number)) {
2559 /* compute digest with old */
2560 (void)sctp_hmac_m(SCTP_HMAC,
2561 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2562 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2564 if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2572 * Now before we continue we must reconstruct our mbuf so that
2573 * normal processing of any other chunks will work.
2579 while (SCTP_BUF_NEXT(m_at) != NULL) {
2580 m_at = SCTP_BUF_NEXT(m_at);
2582 SCTP_BUF_NEXT(m_at) = m_sig;
2585 if (cookie_ok == 0) {
2586 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2587 SCTPDBG(SCTP_DEBUG_INPUT2,
2588 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2589 (uint32_t)offset, cookie_offset, sig_offset);
2594 * check the cookie timestamps to be sure it's not stale
2596 (void)SCTP_GETTIME_TIMEVAL(&now);
2597 /* Expire time is in Ticks, so we convert to seconds */
2598 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2599 time_expires.tv_usec = cookie->time_entered.tv_usec;
2600 if (timevalcmp(&now, &time_expires, >)) {
2601 /* cookie is stale! */
2602 struct mbuf *op_err;
2603 struct sctp_error_stale_cookie *cause;
2604 struct timeval diff;
2607 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2608 0, M_NOWAIT, 1, MT_DATA);
2609 if (op_err == NULL) {
2614 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
2615 cause = mtod(op_err, struct sctp_error_stale_cookie *);
2616 cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
2617 cause->cause.length = htons((sizeof(struct sctp_paramhdr) +
2618 (sizeof(uint32_t))));
2620 timevalsub(&diff, &time_expires);
2621 if ((uint32_t)diff.tv_sec > UINT32_MAX / 1000000) {
2622 staleness = UINT32_MAX;
2624 staleness = diff.tv_sec * 1000000;
2626 if (UINT32_MAX - staleness >= (uint32_t)diff.tv_usec) {
2627 staleness += diff.tv_usec;
2629 staleness = UINT32_MAX;
2631 cause->stale_time = htonl(staleness);
2632 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2633 mflowtype, mflowid, l_inp->fibnum,
2638 * Now we must see with the lookup address if we have an existing
2639 * asoc. This will only happen if we were in the COOKIE-WAIT state
2640 * and a INIT collided with us and somewhere the peer sent the
2641 * cookie on another address besides the single address our assoc
2642 * had for him. In this case we will have one of the tie-tags set at
2643 * least AND the address field in the cookie can be used to look it
2647 switch (cookie->addr_type) {
2649 case SCTP_IPV6_ADDRESS:
2650 memset(&sin6, 0, sizeof(sin6));
2651 sin6.sin6_family = AF_INET6;
2652 sin6.sin6_len = sizeof(sin6);
2653 sin6.sin6_port = sh->src_port;
2654 sin6.sin6_scope_id = cookie->scope_id;
2655 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2656 sizeof(sin6.sin6_addr.s6_addr));
2657 to = (struct sockaddr *)&sin6;
2661 case SCTP_IPV4_ADDRESS:
2662 memset(&sin, 0, sizeof(sin));
2663 sin.sin_family = AF_INET;
2664 sin.sin_len = sizeof(sin);
2665 sin.sin_port = sh->src_port;
2666 sin.sin_addr.s_addr = cookie->address[0];
2667 to = (struct sockaddr *)&sin;
2671 /* This should not happen */
2674 if (*stcb == NULL) {
2675 /* Yep, lets check */
2676 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2677 if (*stcb == NULL) {
2679 * We should have only got back the same inp. If we
2680 * got back a different ep we have a problem. The
2681 * original findep got back l_inp and now
2683 if (l_inp != *inp_p) {
2684 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2687 if (*locked_tcb == NULL) {
2689 * In this case we found the assoc only
2690 * after we locked the create lock. This
2691 * means we are in a colliding case and we
2692 * must make sure that we unlock the tcb if
2693 * its one of the cases where we throw away
2694 * the incoming packets.
2696 *locked_tcb = *stcb;
2699 * We must also increment the inp ref count
2700 * since the ref_count flags was set when we
2701 * did not find the TCB, now we found it
2702 * which reduces the refcount.. we must
2703 * raise it back out to balance it all :-)
2705 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2706 if ((*stcb)->sctp_ep != l_inp) {
2707 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2708 (void *)(*stcb)->sctp_ep, (void *)l_inp);
2714 cookie_len -= SCTP_SIGNATURE_SIZE;
2715 if (*stcb == NULL) {
2716 /* this is the "normal" case... get a new TCB */
2717 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2718 cookie, cookie_len, *inp_p,
2719 netp, to, ¬ification,
2720 auth_skipped, auth_offset, auth_len,
2724 /* this is abnormal... cookie-echo on existing TCB */
2725 had_a_existing_tcb = 1;
2726 *stcb = sctp_process_cookie_existing(m, iphlen, offset,
2728 cookie, cookie_len, *inp_p, *stcb, netp, to,
2729 ¬ification, auth_skipped, auth_offset, auth_len,
2734 if (*stcb == NULL) {
2735 /* still no TCB... must be bad cookie-echo */
2738 if (*netp != NULL) {
2739 (*netp)->flowtype = mflowtype;
2740 (*netp)->flowid = mflowid;
2743 * Ok, we built an association so confirm the address we sent the
2746 netl = sctp_findnet(*stcb, to);
2748 * This code should in theory NOT run but
2751 /* TSNH! Huh, why do I need to add this address here? */
2752 if (sctp_add_remote_addr(*stcb, to, NULL, port,
2753 SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2756 netl = sctp_findnet(*stcb, to);
2759 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2760 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2761 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2766 sctp_start_net_timers(*stcb);
2767 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2768 if (!had_a_existing_tcb ||
2769 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2771 * If we have a NEW cookie or the connect never
2772 * reached the connected state during collision we
2773 * must do the TCP accept thing.
2775 struct socket *so, *oso;
2776 struct sctp_inpcb *inp;
2778 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2780 * For a restart we will keep the same
2781 * socket, no need to do anything. I THINK!!
2783 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2784 if (send_int_conf) {
2785 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2786 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2790 oso = (*inp_p)->sctp_socket;
2791 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2792 SCTP_TCB_UNLOCK((*stcb));
2793 CURVNET_SET(oso->so_vnet);
2794 so = sonewconn(oso, 0
2797 SCTP_TCB_LOCK((*stcb));
2798 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2801 struct mbuf *op_err;
2802 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2803 struct socket *pcb_so;
2805 /* Too many sockets */
2806 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2807 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2808 sctp_abort_association(*inp_p, NULL, m, iphlen,
2809 src, dst, sh, op_err,
2812 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2813 pcb_so = SCTP_INP_SO(*inp_p);
2814 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2815 SCTP_TCB_UNLOCK((*stcb));
2816 SCTP_SOCKET_LOCK(pcb_so, 1);
2817 SCTP_TCB_LOCK((*stcb));
2818 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2820 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2821 SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2822 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2823 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2827 inp = (struct sctp_inpcb *)so->so_pcb;
2828 SCTP_INP_INCR_REF(inp);
2830 * We add the unbound flag here so that if we get an
2831 * soabort() before we get the move_pcb done, we
2832 * will properly cleanup.
2834 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2835 SCTP_PCB_FLAGS_CONNECTED |
2836 SCTP_PCB_FLAGS_IN_TCPPOOL |
2837 SCTP_PCB_FLAGS_UNBOUND |
2838 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2839 SCTP_PCB_FLAGS_DONT_WAKE);
2840 inp->sctp_features = (*inp_p)->sctp_features;
2841 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2842 inp->sctp_socket = so;
2843 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2844 inp->max_cwnd = (*inp_p)->max_cwnd;
2845 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2846 inp->ecn_supported = (*inp_p)->ecn_supported;
2847 inp->prsctp_supported = (*inp_p)->prsctp_supported;
2848 inp->auth_supported = (*inp_p)->auth_supported;
2849 inp->asconf_supported = (*inp_p)->asconf_supported;
2850 inp->reconfig_supported = (*inp_p)->reconfig_supported;
2851 inp->nrsack_supported = (*inp_p)->nrsack_supported;
2852 inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2853 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2854 inp->sctp_context = (*inp_p)->sctp_context;
2855 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2856 inp->fibnum = (*inp_p)->fibnum;
2857 inp->inp_starting_point_for_iterator = NULL;
2859 * copy in the authentication parameters from the
2862 if (inp->sctp_ep.local_hmacs)
2863 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2864 inp->sctp_ep.local_hmacs =
2865 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2866 if (inp->sctp_ep.local_auth_chunks)
2867 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2868 inp->sctp_ep.local_auth_chunks =
2869 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2872 * Now we must move it from one hash table to
2873 * another and get the tcb in the right place.
2877 * This is where the one-2-one socket is put into
2878 * the accept state waiting for the accept!
2881 SCTP_ADD_SUBSTATE(*stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
2883 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2885 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2886 SCTP_TCB_UNLOCK((*stcb));
2888 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2890 SCTP_TCB_LOCK((*stcb));
2891 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2895 * now we must check to see if we were aborted while
2896 * the move was going on and the lock/unlock
2899 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2901 * yep it was, we leave the assoc attached
2902 * to the socket since the sctp_inpcb_free()
2903 * call will send an abort for us.
2905 SCTP_INP_DECR_REF(inp);
2908 SCTP_INP_DECR_REF(inp);
2909 /* Switch over to the new guy */
2911 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2912 if (send_int_conf) {
2913 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2914 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2918 * Pull it from the incomplete queue and wake the
2921 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2922 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2923 SCTP_TCB_UNLOCK((*stcb));
2924 SCTP_SOCKET_LOCK(so, 1);
2927 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2928 SCTP_TCB_LOCK((*stcb));
2929 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2930 SCTP_SOCKET_UNLOCK(so, 1);
2936 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2938 if (send_int_conf) {
2939 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2940 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2946 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2947 struct sctp_tcb *stcb, struct sctp_nets *net)
2949 /* cp must not be used, others call this without a c-ack :-) */
2950 struct sctp_association *asoc;
2952 SCTPDBG(SCTP_DEBUG_INPUT2,
2953 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2954 if ((stcb == NULL) || (net == NULL)) {
2959 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2960 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2961 asoc->overall_error_count,
2963 SCTP_FROM_SCTP_INPUT,
2966 asoc->overall_error_count = 0;
2967 sctp_stop_all_cookie_timers(stcb);
2968 /* process according to association state */
2969 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) {
2970 /* state change only needed when I am in right state */
2971 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2972 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
2973 sctp_start_net_timers(stcb);
2974 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2975 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2976 stcb->sctp_ep, stcb, asoc->primary_destination);
2980 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2981 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2982 if (asoc->overall_error_count == 0) {
2983 sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
2984 SCTP_RTT_FROM_NON_DATA);
2986 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2987 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2988 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2989 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2990 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2994 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2995 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2996 so = SCTP_INP_SO(stcb->sctp_ep);
2997 atomic_add_int(&stcb->asoc.refcnt, 1);
2998 SCTP_TCB_UNLOCK(stcb);
2999 SCTP_SOCKET_LOCK(so, 1);
3000 SCTP_TCB_LOCK(stcb);
3001 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3003 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
3004 soisconnected(stcb->sctp_socket);
3006 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3007 SCTP_SOCKET_UNLOCK(so, 1);
3011 * since we did not send a HB make sure we don't double
3014 net->hb_responded = 1;
3016 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3018 * We don't need to do the asconf thing, nor hb or
3019 * autoclose if the socket is closed.
3024 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
3028 if (stcb->asoc.sctp_autoclose_ticks &&
3029 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
3030 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
3031 stcb->sctp_ep, stcb, NULL);
3034 * send ASCONF if parameters are pending and ASCONFs are
3035 * allowed (eg. addresses changed when init/cookie echo were
3038 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
3039 (stcb->asoc.asconf_supported == 1) &&
3040 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
3041 #ifdef SCTP_TIMER_BASED_ASCONF
3042 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
3043 stcb->sctp_ep, stcb,
3044 stcb->asoc.primary_destination);
3046 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
3047 SCTP_ADDR_NOT_LOCKED);
3052 /* Toss the cookie if I can */
3053 sctp_toss_old_cookies(stcb, asoc);
3054 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3055 /* Restart the timer if we have pending data */
3056 struct sctp_tmit_chunk *chk;
3058 chk = TAILQ_FIRST(&asoc->sent_queue);
3059 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
3064 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
3065 struct sctp_tcb *stcb)
3067 struct sctp_nets *net;
3068 struct sctp_tmit_chunk *lchk;
3069 struct sctp_ecne_chunk bkup;
3070 uint8_t override_bit;
3071 uint32_t tsn, window_data_tsn;
3073 unsigned int pkt_cnt;
3075 len = ntohs(cp->ch.chunk_length);
3076 if ((len != sizeof(struct sctp_ecne_chunk)) &&
3077 (len != sizeof(struct old_sctp_ecne_chunk))) {
3080 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3081 /* Its the old format */
3082 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3083 bkup.num_pkts_since_cwr = htonl(1);
3086 SCTP_STAT_INCR(sctps_recvecne);
3087 tsn = ntohl(cp->tsn);
3088 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3089 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3091 window_data_tsn = stcb->asoc.sending_seq - 1;
3093 window_data_tsn = lchk->rec.data.tsn;
3096 /* Find where it was sent to if possible. */
3098 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3099 if (lchk->rec.data.tsn == tsn) {
3101 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3104 if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) {
3110 * What to do. A previous send of a CWR was possibly lost.
3111 * See how old it is, we may have it marked on the actual
3114 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3115 if (tsn == net->last_cwr_tsn) {
3116 /* Found him, send it off */
3122 * If we reach here, we need to send a special CWR
3123 * that says hey, we did this a long time ago and
3124 * you lost the response.
3126 net = TAILQ_FIRST(&stcb->asoc.nets);
3131 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3138 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3139 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3141 * JRS - Use the congestion control given in the pluggable
3144 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3146 * We reduce once every RTT. So we will only lower cwnd at
3147 * the next sending seq i.e. the window_data_tsn
3149 net->cwr_window_tsn = window_data_tsn;
3150 net->ecn_ce_pkt_cnt += pkt_cnt;
3151 net->lost_cnt = pkt_cnt;
3152 net->last_cwr_tsn = tsn;
3154 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3155 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3156 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3158 * Another loss in the same window update how many
3159 * marks/packets lost we have had.
3163 if (pkt_cnt > net->lost_cnt) {
3164 /* Should be the case */
3165 cnt = (pkt_cnt - net->lost_cnt);
3166 net->ecn_ce_pkt_cnt += cnt;
3168 net->lost_cnt = pkt_cnt;
3169 net->last_cwr_tsn = tsn;
3171 * Most CC functions will ignore this call, since we
3172 * are in-window yet of the initial CE the peer saw.
3174 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3178 * We always send a CWR this way if our previous one was lost our
3179 * peer will get an update, or if it is not time again to reduce we
3180 * still get the cwr to the peer. Note we set the override when we
3181 * could not find the TSN on the chunk or the destination network.
3183 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3187 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3190 * Here we get a CWR from the peer. We must look in the outqueue and
3191 * make sure that we have a covered ECNE in the control chunk part.
3194 struct sctp_tmit_chunk *chk, *nchk;
3195 struct sctp_ecne_chunk *ecne;
3199 cwr_tsn = ntohl(cp->tsn);
3200 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3201 TAILQ_FOREACH_SAFE(chk, &stcb->asoc.control_send_queue, sctp_next, nchk) {
3202 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3205 if ((override == 0) && (chk->whoTo != net)) {
3206 /* Must be from the right src unless override is set */
3209 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3210 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3211 /* this covers this ECNE, we can remove it */
3212 stcb->asoc.ecn_echo_cnt_onq--;
3213 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3215 stcb->asoc.ctrl_queue_cnt--;
3216 sctp_m_freem(chk->data);
3218 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3219 if (override == 0) {
3227 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3228 struct sctp_tcb *stcb, struct sctp_nets *net)
3230 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3234 SCTPDBG(SCTP_DEBUG_INPUT2,
3235 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3239 /* process according to association state */
3240 if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3241 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3242 SCTPDBG(SCTP_DEBUG_INPUT2,
3243 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3244 SCTP_TCB_UNLOCK(stcb);
3247 /* notify upper layer protocol */
3248 if (stcb->sctp_socket) {
3249 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3252 if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
3253 !TAILQ_EMPTY(&stcb->asoc.sent_queue) ||
3254 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
3255 panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3258 /* stop the timer */
3259 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3260 SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3261 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3263 SCTPDBG(SCTP_DEBUG_INPUT2,
3264 "sctp_handle_shutdown_complete: calls free-asoc\n");
3265 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3266 so = SCTP_INP_SO(stcb->sctp_ep);
3267 atomic_add_int(&stcb->asoc.refcnt, 1);
3268 SCTP_TCB_UNLOCK(stcb);
3269 SCTP_SOCKET_LOCK(so, 1);
3270 SCTP_TCB_LOCK(stcb);
3271 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3273 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3274 SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3275 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3276 SCTP_SOCKET_UNLOCK(so, 1);
3282 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3283 struct sctp_nets *net, uint8_t flg)
3285 switch (desc->chunk_type) {
3287 /* find the tsn to resend (possibly */
3290 struct sctp_tmit_chunk *tp1;
3292 tsn = ntohl(desc->tsn_ifany);
3293 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3294 if (tp1->rec.data.tsn == tsn) {
3298 if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) {
3306 * Do it the other way , aka without paying
3307 * attention to queue seq order.
3309 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3310 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3311 if (tp1->rec.data.tsn == tsn) {
3318 SCTP_STAT_INCR(sctps_pdrptsnnf);
3320 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3323 if (((flg & SCTP_BADCRC) == 0) &&
3324 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3327 if ((stcb->asoc.peers_rwnd == 0) &&
3328 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3329 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3332 if (stcb->asoc.peers_rwnd == 0 &&
3333 (flg & SCTP_FROM_MIDDLE_BOX)) {
3334 SCTP_STAT_INCR(sctps_pdrpdizrw);
3337 ddp = (uint8_t *)(mtod(tp1->data, caddr_t)+
3338 sizeof(struct sctp_data_chunk));
3342 for (iii = 0; iii < sizeof(desc->data_bytes);
3344 if (ddp[iii] != desc->data_bytes[iii]) {
3345 SCTP_STAT_INCR(sctps_pdrpbadd);
3353 * this guy had a RTO calculation
3354 * pending on it, cancel it
3356 if (tp1->whoTo->rto_needed == 0) {
3357 tp1->whoTo->rto_needed = 1;
3361 SCTP_STAT_INCR(sctps_pdrpmark);
3362 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3363 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3365 * mark it as if we were doing a FR, since
3366 * we will be getting gap ack reports behind
3367 * the info from the router.
3369 tp1->rec.data.doing_fast_retransmit = 1;
3371 * mark the tsn with what sequences can
3374 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3375 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3377 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
3380 /* restart the timer */
3381 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3383 SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3384 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3387 /* fix counts and things */
3388 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3389 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3390 tp1->whoTo->flight_size,
3392 (uint32_t)(uintptr_t)stcb,
3395 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3396 sctp_flight_size_decrease(tp1);
3397 sctp_total_flight_decrease(stcb, tp1);
3399 tp1->sent = SCTP_DATAGRAM_RESEND;
3405 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3406 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3409 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3411 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3414 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3415 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3416 audit, stcb->asoc.sent_queue_retran_cnt);
3417 #ifndef SCTP_AUDITING_ENABLED
3418 stcb->asoc.sent_queue_retran_cnt = audit;
3426 struct sctp_tmit_chunk *asconf;
3428 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3430 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3435 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3436 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3437 asconf->sent = SCTP_DATAGRAM_RESEND;
3438 asconf->snd_count--;
3442 case SCTP_INITIATION:
3443 /* resend the INIT */
3444 stcb->asoc.dropped_special_cnt++;
3445 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3447 * If we can get it in, in a few attempts we do
3448 * this, otherwise we let the timer fire.
3450 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3452 SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3453 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3456 case SCTP_SELECTIVE_ACK:
3457 case SCTP_NR_SELECTIVE_ACK:
3458 /* resend the sack */
3459 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3461 case SCTP_HEARTBEAT_REQUEST:
3462 /* resend a demand HB */
3463 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3465 * Only retransmit if we KNOW we wont destroy the
3468 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3472 sctp_send_shutdown(stcb, net);
3474 case SCTP_SHUTDOWN_ACK:
3475 sctp_send_shutdown_ack(stcb, net);
3477 case SCTP_COOKIE_ECHO:
3479 struct sctp_tmit_chunk *cookie;
3482 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3484 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3489 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3490 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3491 cookie->sent = SCTP_DATAGRAM_RESEND;
3492 sctp_stop_all_cookie_timers(stcb);
3496 case SCTP_COOKIE_ACK:
3497 sctp_send_cookie_ack(stcb);
3499 case SCTP_ASCONF_ACK:
3500 /* resend last asconf ack */
3501 sctp_send_asconf_ack(stcb);
3503 case SCTP_IFORWARD_CUM_TSN:
3504 case SCTP_FORWARD_CUM_TSN:
3505 send_forward_tsn(stcb, &stcb->asoc);
3507 /* can't do anything with these */
3508 case SCTP_PACKET_DROPPED:
3509 case SCTP_INITIATION_ACK: /* this should not happen */
3510 case SCTP_HEARTBEAT_ACK:
3511 case SCTP_ABORT_ASSOCIATION:
3512 case SCTP_OPERATION_ERROR:
3513 case SCTP_SHUTDOWN_COMPLETE:
3523 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3529 * We set things to 0xffffffff since this is the last delivered
3530 * sequence and we will be sending in 0 after the reset.
3533 if (number_entries) {
3534 for (i = 0; i < number_entries; i++) {
3535 temp = ntohs(list[i]);
3536 if (temp >= stcb->asoc.streamincnt) {
3539 stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff;
3543 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3544 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
3547 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3551 sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3556 if (number_entries > 0) {
3557 for (i = 0; i < number_entries; i++) {
3558 temp = ntohs(list[i]);
3559 if (temp >= stcb->asoc.streamoutcnt) {
3560 /* no such stream */
3563 stcb->asoc.strmout[temp].next_mid_ordered = 0;
3564 stcb->asoc.strmout[temp].next_mid_unordered = 0;
3567 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3568 stcb->asoc.strmout[i].next_mid_ordered = 0;
3569 stcb->asoc.strmout[i].next_mid_unordered = 0;
3572 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3576 sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3581 if (number_entries > 0) {
3582 for (i = 0; i < number_entries; i++) {
3583 temp = ntohs(list[i]);
3584 if (temp >= stcb->asoc.streamoutcnt) {
3585 /* no such stream */
3588 stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
3591 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3592 stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
3598 struct sctp_stream_reset_request *
3599 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3601 struct sctp_association *asoc;
3602 struct sctp_chunkhdr *ch;
3603 struct sctp_stream_reset_request *r;
3604 struct sctp_tmit_chunk *chk;
3608 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3609 asoc->stream_reset_outstanding = 0;
3612 if (stcb->asoc.str_reset == NULL) {
3613 asoc->stream_reset_outstanding = 0;
3616 chk = stcb->asoc.str_reset;
3617 if (chk->data == NULL) {
3621 /* he wants a copy of the chk pointer */
3624 clen = chk->send_size;
3625 ch = mtod(chk->data, struct sctp_chunkhdr *);
3626 r = (struct sctp_stream_reset_request *)(ch + 1);
3627 if (ntohl(r->request_seq) == seq) {
3631 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3632 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3633 /* move to the next one, there can only be a max of two */
3634 r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3635 if (ntohl(r->request_seq) == seq) {
3639 /* that seq is not here */
3644 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3646 struct sctp_association *asoc;
3647 struct sctp_tmit_chunk *chk;
3650 chk = asoc->str_reset;
3654 asoc->str_reset = NULL;
3655 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
3656 chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3657 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3658 asoc->ctrl_queue_cnt--;
3660 sctp_m_freem(chk->data);
3663 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3668 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3669 uint32_t seq, uint32_t action,
3670 struct sctp_stream_reset_response *respin)
3674 struct sctp_association *asoc = &stcb->asoc;
3675 struct sctp_tmit_chunk *chk;
3676 struct sctp_stream_reset_request *req_param;
3677 struct sctp_stream_reset_out_request *req_out_param;
3678 struct sctp_stream_reset_in_request *req_in_param;
3679 uint32_t number_entries;
3681 if (asoc->stream_reset_outstanding == 0) {
3685 if (seq == stcb->asoc.str_reset_seq_out) {
3686 req_param = sctp_find_stream_reset(stcb, seq, &chk);
3687 if (req_param != NULL) {
3688 stcb->asoc.str_reset_seq_out++;
3689 type = ntohs(req_param->ph.param_type);
3690 lparam_len = ntohs(req_param->ph.param_length);
3691 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3694 req_out_param = (struct sctp_stream_reset_out_request *)req_param;
3695 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3696 asoc->stream_reset_out_is_outstanding = 0;
3697 if (asoc->stream_reset_outstanding)
3698 asoc->stream_reset_outstanding--;
3699 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3701 sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3702 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3703 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3704 } else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
3706 * Set it up so we don't stop
3709 asoc->stream_reset_outstanding++;
3710 stcb->asoc.str_reset_seq_out--;
3711 asoc->stream_reset_out_is_outstanding = 1;
3714 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3716 if (no_clear == 0) {
3717 sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
3719 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3720 req_in_param = (struct sctp_stream_reset_in_request *)req_param;
3721 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3722 if (asoc->stream_reset_outstanding)
3723 asoc->stream_reset_outstanding--;
3724 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3725 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3726 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3727 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3728 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3729 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3731 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3732 /* Ok we now may have more streams */
3735 num_stream = stcb->asoc.strm_pending_add_size;
3736 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3738 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3740 stcb->asoc.strm_pending_add_size = 0;
3741 if (asoc->stream_reset_outstanding)
3742 asoc->stream_reset_outstanding--;
3743 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3744 /* Put the new streams into effect */
3747 for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
3748 asoc->strmout[i].state = SCTP_STREAM_OPEN;
3750 asoc->streamoutcnt += num_stream;
3751 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3752 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3753 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3754 SCTP_STREAM_CHANGE_DENIED);
3756 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3757 SCTP_STREAM_CHANGE_FAILED);
3759 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3760 if (asoc->stream_reset_outstanding)
3761 asoc->stream_reset_outstanding--;
3762 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3763 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3764 SCTP_STREAM_CHANGE_DENIED);
3765 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3766 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3767 SCTP_STREAM_CHANGE_FAILED);
3769 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3771 * a) Adopt the new in tsn.
3773 * c) Adopt the new out-tsn
3775 struct sctp_stream_reset_response_tsn *resp;
3776 struct sctp_forward_tsn_chunk fwdtsn;
3779 if (respin == NULL) {
3783 if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
3786 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3787 resp = (struct sctp_stream_reset_response_tsn *)respin;
3788 asoc->stream_reset_outstanding--;
3789 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3790 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3791 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3792 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3796 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3797 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3798 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3801 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3802 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3803 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3805 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3806 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3808 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3809 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3811 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3812 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3813 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3814 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3815 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3816 SCTP_ASSOC_RESET_DENIED);
3818 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3819 SCTP_ASSOC_RESET_FAILED);
3822 /* get rid of the request and get the request flags */
3823 if (asoc->stream_reset_outstanding == 0) {
3824 sctp_clean_up_stream_reset(stcb);
3828 if (asoc->stream_reset_outstanding == 0) {
3829 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3835 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3836 struct sctp_tmit_chunk *chk,
3837 struct sctp_stream_reset_in_request *req, int trunc)
3845 * peer wants me to send a str-reset to him for my outgoing seq's if
3848 struct sctp_association *asoc = &stcb->asoc;
3850 seq = ntohl(req->request_seq);
3851 if (asoc->str_reset_seq_in == seq) {
3852 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3853 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3854 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3856 /* Can't do it, since they exceeded our buffer size */
3857 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3858 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3859 len = ntohs(req->ph.param_length);
3860 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3861 if (number_entries) {
3862 for (i = 0; i < number_entries; i++) {
3863 temp = ntohs(req->list_of_streams[i]);
3864 if (temp >= stcb->asoc.streamoutcnt) {
3865 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3868 req->list_of_streams[i] = temp;
3870 for (i = 0; i < number_entries; i++) {
3871 if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
3872 stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
3877 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3878 if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
3879 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
3882 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3884 /* Can't do it, since we have sent one out */
3885 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3888 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3889 asoc->str_reset_seq_in++;
3890 } else if (asoc->str_reset_seq_in - 1 == seq) {
3891 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3892 } else if (asoc->str_reset_seq_in - 2 == seq) {
3893 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3895 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3897 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3901 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3902 struct sctp_tmit_chunk *chk,
3903 struct sctp_stream_reset_tsn_request *req)
3905 /* reset all in and out and update the tsn */
3907 * A) reset my str-seq's on in and out. B) Select a receive next,
3908 * and set cum-ack to it. Also process this selected number as a
3909 * fwd-tsn as well. C) set in the response my next sending seq.
3911 struct sctp_forward_tsn_chunk fwdtsn;
3912 struct sctp_association *asoc = &stcb->asoc;
3916 seq = ntohl(req->request_seq);
3917 if (asoc->str_reset_seq_in == seq) {
3918 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3919 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3920 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3922 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3923 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3924 fwdtsn.ch.chunk_flags = 0;
3925 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3926 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3930 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3931 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3932 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3934 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3935 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3936 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3937 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3938 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3939 atomic_add_int(&asoc->sending_seq, 1);
3940 /* save off historical data for retrans */
3941 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3942 asoc->last_sending_seq[0] = asoc->sending_seq;
3943 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3944 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3945 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3946 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3947 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3948 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3950 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3951 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3952 asoc->str_reset_seq_in++;
3953 } else if (asoc->str_reset_seq_in - 1 == seq) {
3954 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3955 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3956 } else if (asoc->str_reset_seq_in - 2 == seq) {
3957 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3958 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3960 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3966 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3967 struct sctp_tmit_chunk *chk,
3968 struct sctp_stream_reset_out_request *req, int trunc)
3971 int number_entries, len;
3972 struct sctp_association *asoc = &stcb->asoc;
3974 seq = ntohl(req->request_seq);
3976 /* now if its not a duplicate we process it */
3977 if (asoc->str_reset_seq_in == seq) {
3978 len = ntohs(req->ph.param_length);
3979 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3981 * the sender is resetting, handle the list issue.. we must
3982 * a) verify if we can do the reset, if so no problem b) If
3983 * we can't do the reset we must copy the request. c) queue
3984 * it, and setup the data in processor to trigger it off
3985 * when needed and dequeue all the queued data.
3987 tsn = ntohl(req->send_reset_at_tsn);
3989 /* move the reset action back one */
3990 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3991 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3992 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3994 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3995 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3996 /* we can do it now */
3997 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3998 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4001 * we must queue it up and thus wait for the TSN's
4002 * to arrive that are at or before tsn
4004 struct sctp_stream_reset_list *liste;
4007 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
4008 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
4009 siz, SCTP_M_STRESET);
4010 if (liste == NULL) {
4011 /* gak out of memory */
4012 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4013 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4018 liste->number_entries = number_entries;
4019 memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
4020 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
4021 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
4023 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4024 asoc->str_reset_seq_in++;
4025 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4027 * one seq back, just echo back last action since my
4028 * response was lost.
4030 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4031 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4033 * two seq back, just echo back last action since my
4034 * response was lost.
4036 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4038 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4043 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4044 struct sctp_stream_reset_add_strm *str_add)
4047 * Peer is requesting to add more streams. If its within our
4048 * max-streams we will allow it.
4050 uint32_t num_stream, i;
4052 struct sctp_association *asoc = &stcb->asoc;
4053 struct sctp_queued_to_read *ctl, *nctl;
4055 /* Get the number. */
4056 seq = ntohl(str_add->request_seq);
4057 num_stream = ntohs(str_add->number_of_streams);
4058 /* Now what would be the new total? */
4059 if (asoc->str_reset_seq_in == seq) {
4060 num_stream += stcb->asoc.streamincnt;
4061 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4062 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4063 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4064 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
4065 (num_stream > 0xffff)) {
4066 /* We must reject it they ask for to many */
4068 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4070 /* Ok, we can do that :-) */
4071 struct sctp_stream_in *oldstrm;
4073 /* save off the old */
4074 oldstrm = stcb->asoc.strmin;
4075 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
4076 (num_stream * sizeof(struct sctp_stream_in)),
4078 if (stcb->asoc.strmin == NULL) {
4079 stcb->asoc.strmin = oldstrm;
4082 /* copy off the old data */
4083 for (i = 0; i < stcb->asoc.streamincnt; i++) {
4084 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4085 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4086 stcb->asoc.strmin[i].sid = i;
4087 stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered;
4088 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
4089 stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
4090 /* now anything on those queues? */
4091 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
4092 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
4093 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
4095 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
4096 TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
4097 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
4100 /* Init the new streams */
4101 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
4102 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4103 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4104 stcb->asoc.strmin[i].sid = i;
4105 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
4106 stcb->asoc.strmin[i].pd_api_started = 0;
4107 stcb->asoc.strmin[i].delivery_started = 0;
4109 SCTP_FREE(oldstrm, SCTP_M_STRMI);
4110 /* update the size */
4111 stcb->asoc.streamincnt = num_stream;
4112 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4113 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
4115 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4116 asoc->str_reset_seq_in++;
4117 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4119 * one seq back, just echo back last action since my
4120 * response was lost.
4122 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4123 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4125 * two seq back, just echo back last action since my
4126 * response was lost.
4128 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4130 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4136 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4137 struct sctp_stream_reset_add_strm *str_add)
4140 * Peer is requesting to add more streams. If its within our
4141 * max-streams we will allow it.
4143 uint16_t num_stream;
4145 struct sctp_association *asoc = &stcb->asoc;
4147 /* Get the number. */
4148 seq = ntohl(str_add->request_seq);
4149 num_stream = ntohs(str_add->number_of_streams);
4150 /* Now what would be the new total? */
4151 if (asoc->str_reset_seq_in == seq) {
4152 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4153 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4154 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4155 } else if (stcb->asoc.stream_reset_outstanding) {
4156 /* We must reject it we have something pending */
4157 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4159 /* Ok, we can do that :-) */
4162 mychk = stcb->asoc.streamoutcnt;
4163 mychk += num_stream;
4164 if (mychk < 0x10000) {
4165 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4166 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
4167 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4170 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4173 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4174 asoc->str_reset_seq_in++;
4175 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4177 * one seq back, just echo back last action since my
4178 * response was lost.
4180 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4181 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4183 * two seq back, just echo back last action since my
4184 * response was lost.
4186 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4188 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4193 __attribute__((noinline))
4196 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4197 struct sctp_chunkhdr *ch_req)
4199 uint16_t remaining_length, param_len, ptype;
4200 struct sctp_paramhdr pstore;
4201 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4205 struct sctp_tmit_chunk *chk;
4206 struct sctp_chunkhdr *ch;
4207 struct sctp_paramhdr *ph;
4211 /* now it may be a reset or a reset-response */
4212 remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
4214 /* setup for adding the response */
4215 sctp_alloc_a_chunk(stcb, chk);
4219 chk->copy_by_ref = 0;
4220 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4221 chk->rec.chunk_id.can_take_data = 0;
4223 chk->asoc = &stcb->asoc;
4224 chk->no_fr_allowed = 0;
4225 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4226 chk->book_size_scale = 0;
4227 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4228 if (chk->data == NULL) {
4231 sctp_m_freem(chk->data);
4234 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4237 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4239 /* setup chunk parameters */
4240 chk->sent = SCTP_DATAGRAM_UNSENT;
4244 ch = mtod(chk->data, struct sctp_chunkhdr *);
4245 ch->chunk_type = SCTP_STREAM_RESET;
4246 ch->chunk_flags = 0;
4247 ch->chunk_length = htons(chk->send_size);
4248 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4249 offset += sizeof(struct sctp_chunkhdr);
4250 while (remaining_length >= sizeof(struct sctp_paramhdr)) {
4251 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
4256 param_len = ntohs(ph->param_length);
4257 if ((param_len > remaining_length) ||
4258 (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
4259 /* bad parameter length */
4262 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
4263 (uint8_t *)&cstore);
4268 ptype = ntohs(ph->param_type);
4270 if (param_len > sizeof(cstore)) {
4275 if (num_param > SCTP_MAX_RESET_PARAMS) {
4276 /* hit the max of parameters already sorry.. */
4279 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4280 struct sctp_stream_reset_out_request *req_out;
4282 if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
4285 req_out = (struct sctp_stream_reset_out_request *)ph;
4287 if (stcb->asoc.stream_reset_outstanding) {
4288 seq = ntohl(req_out->response_seq);
4289 if (seq == stcb->asoc.str_reset_seq_out) {
4291 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4294 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4295 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4296 struct sctp_stream_reset_add_strm *str_add;
4298 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4301 str_add = (struct sctp_stream_reset_add_strm *)ph;
4303 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4304 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4305 struct sctp_stream_reset_add_strm *str_add;
4307 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4310 str_add = (struct sctp_stream_reset_add_strm *)ph;
4312 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4313 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4314 struct sctp_stream_reset_in_request *req_in;
4317 req_in = (struct sctp_stream_reset_in_request *)ph;
4318 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4319 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4320 struct sctp_stream_reset_tsn_request *req_tsn;
4323 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4324 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4326 goto strres_nochunk;
4330 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4331 struct sctp_stream_reset_response *resp;
4334 if (param_len < sizeof(struct sctp_stream_reset_response)) {
4337 resp = (struct sctp_stream_reset_response *)ph;
4338 seq = ntohl(resp->response_seq);
4339 result = ntohl(resp->result);
4340 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4342 goto strres_nochunk;
4347 offset += SCTP_SIZE32(param_len);
4348 if (remaining_length >= SCTP_SIZE32(param_len)) {
4349 remaining_length -= SCTP_SIZE32(param_len);
4351 remaining_length = 0;
4355 /* we have no response free the stuff */
4356 goto strres_nochunk;
4358 /* ok we have a chunk to link in */
4359 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4362 stcb->asoc.ctrl_queue_cnt++;
4367 * Handle a router or endpoints report of a packet loss, there are two ways
4368 * to handle this, either we get the whole packet and must disect it
4369 * ourselves (possibly with truncation and or corruption) or it is a summary
4370 * from a middle box that did the disectting for us.
4373 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4374 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4376 uint32_t bottle_bw, on_queue;
4380 struct sctp_chunk_desc desc;
4381 struct sctp_chunkhdr *ch;
4383 chlen = ntohs(cp->ch.chunk_length);
4384 chlen -= sizeof(struct sctp_pktdrop_chunk);
4385 /* XXX possible chlen underflow */
4388 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4389 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4391 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4392 chlen -= sizeof(struct sctphdr);
4393 /* XXX possible chlen underflow */
4394 memset(&desc, 0, sizeof(desc));
4396 trunc_len = (uint16_t)ntohs(cp->trunc_len);
4397 if (trunc_len > limit) {
4401 /* now the chunks themselves */
4402 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4403 desc.chunk_type = ch->chunk_type;
4404 /* get amount we need to move */
4405 at = ntohs(ch->chunk_length);
4406 if (at < sizeof(struct sctp_chunkhdr)) {
4407 /* corrupt chunk, maybe at the end? */
4408 SCTP_STAT_INCR(sctps_pdrpcrupt);
4411 if (trunc_len == 0) {
4412 /* we are supposed to have all of it */
4414 /* corrupt skip it */
4415 SCTP_STAT_INCR(sctps_pdrpcrupt);
4419 /* is there enough of it left ? */
4420 if (desc.chunk_type == SCTP_DATA) {
4421 if (chlen < (sizeof(struct sctp_data_chunk) +
4422 sizeof(desc.data_bytes))) {
4426 if (chlen < sizeof(struct sctp_chunkhdr)) {
4431 if (desc.chunk_type == SCTP_DATA) {
4432 /* can we get out the tsn? */
4433 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4434 SCTP_STAT_INCR(sctps_pdrpmbda);
4436 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4438 struct sctp_data_chunk *dcp;
4442 dcp = (struct sctp_data_chunk *)ch;
4443 ddp = (uint8_t *)(dcp + 1);
4444 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4445 desc.data_bytes[iii] = ddp[iii];
4447 desc.tsn_ifany = dcp->dp.tsn;
4449 /* nope we are done. */
4450 SCTP_STAT_INCR(sctps_pdrpnedat);
4454 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4455 SCTP_STAT_INCR(sctps_pdrpmbct);
4458 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4459 SCTP_STAT_INCR(sctps_pdrppdbrk);
4462 if (SCTP_SIZE32(at) > chlen) {
4465 chlen -= SCTP_SIZE32(at);
4466 if (chlen < sizeof(struct sctp_chunkhdr)) {
4467 /* done, none left */
4470 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4472 /* Now update any rwnd --- possibly */
4473 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4474 /* From a peer, we get a rwnd report */
4477 SCTP_STAT_INCR(sctps_pdrpfehos);
4479 bottle_bw = ntohl(cp->bottle_bw);
4480 on_queue = ntohl(cp->current_onq);
4481 if (bottle_bw && on_queue) {
4482 /* a rwnd report is in here */
4483 if (bottle_bw > on_queue)
4484 a_rwnd = bottle_bw - on_queue;
4489 stcb->asoc.peers_rwnd = 0;
4491 if (a_rwnd > stcb->asoc.total_flight) {
4492 stcb->asoc.peers_rwnd =
4493 a_rwnd - stcb->asoc.total_flight;
4495 stcb->asoc.peers_rwnd = 0;
4497 if (stcb->asoc.peers_rwnd <
4498 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4499 /* SWS sender side engages */
4500 stcb->asoc.peers_rwnd = 0;
4505 SCTP_STAT_INCR(sctps_pdrpfmbox);
4508 /* now middle boxes in sat networks get a cwnd bump */
4509 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4510 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4511 (stcb->asoc.sat_network)) {
4513 * This is debatable but for sat networks it makes sense
4514 * Note if a T3 timer has went off, we will prohibit any
4515 * changes to cwnd until we exit the t3 loss recovery.
4517 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4518 net, cp, &bottle_bw, &on_queue);
4523 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4524 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4525 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4526 * length of the complete packet outputs: - length: modified to remaining
4527 * length after control processing - netp: modified to new sctp_nets after
4528 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4529 * bad packet,...) otherwise return the tcb for this packet
4532 __attribute__((noinline))
4534 static struct sctp_tcb *
4535 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4536 struct sockaddr *src, struct sockaddr *dst,
4537 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4538 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4539 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4540 uint32_t vrf_id, uint16_t port)
4542 struct sctp_association *asoc;
4543 struct mbuf *op_err;
4544 char msg[SCTP_DIAG_INFO_LEN];
4546 int num_chunks = 0; /* number of control chunks processed */
4547 uint32_t chk_length, contiguous;
4549 int abort_no_unlock = 0;
4553 * How big should this be, and should it be alloc'd? Lets try the
4554 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4555 * until we get into jumbo grams and such..
4557 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4559 uint32_t auth_offset = 0, auth_len = 0;
4560 int auth_skipped = 0;
4562 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4566 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4567 iphlen, *offset, length, (void *)stcb);
4570 SCTP_TCB_LOCK_ASSERT(stcb);
4572 /* validate chunk header length... */
4573 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4574 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4575 ntohs(ch->chunk_length));
4580 * validate the verification tag
4582 vtag_in = ntohl(sh->v_tag);
4584 if (ch->chunk_type == SCTP_INITIATION) {
4585 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4586 ntohs(ch->chunk_length), vtag_in);
4588 /* protocol error- silently discard... */
4589 SCTP_STAT_INCR(sctps_badvtag);
4591 SCTP_TCB_UNLOCK(stcb);
4595 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4597 * If there is no stcb, skip the AUTH chunk and process
4598 * later after a stcb is found (to validate the lookup was
4601 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4603 (inp->auth_supported == 1)) {
4604 /* save this chunk for later processing */
4606 auth_offset = *offset;
4607 auth_len = ntohs(ch->chunk_length);
4609 /* (temporarily) move past this chunk */
4610 *offset += SCTP_SIZE32(auth_len);
4611 if (*offset >= length) {
4612 /* no more data left in the mbuf chain */
4616 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4617 sizeof(struct sctp_chunkhdr), chunk_buf);
4624 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4625 goto process_control_chunks;
4628 * first check if it's an ASCONF with an unknown src addr we
4629 * need to look inside to find the association
4631 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4632 struct sctp_chunkhdr *asconf_ch = ch;
4633 uint32_t asconf_offset = 0, asconf_len = 0;
4635 /* inp's refcount may be reduced */
4636 SCTP_INP_INCR_REF(inp);
4638 asconf_offset = *offset;
4640 asconf_len = ntohs(asconf_ch->chunk_length);
4641 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4643 stcb = sctp_findassociation_ep_asconf(m,
4646 sh, &inp, netp, vrf_id);
4649 asconf_offset += SCTP_SIZE32(asconf_len);
4650 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4651 sizeof(struct sctp_chunkhdr), chunk_buf);
4652 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4655 * reduce inp's refcount if not reduced in
4656 * sctp_findassociation_ep_asconf().
4658 SCTP_INP_DECR_REF(inp);
4661 /* now go back and verify any auth chunk to be sure */
4662 if (auth_skipped && (stcb != NULL)) {
4663 struct sctp_auth_chunk *auth;
4665 auth = (struct sctp_auth_chunk *)
4666 sctp_m_getptr(m, auth_offset,
4667 auth_len, chunk_buf);
4670 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4672 /* auth HMAC failed so dump it */
4676 /* remaining chunks are HMAC checked */
4677 stcb->asoc.authenticated = 1;
4682 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4683 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4685 /* no association, so it's out of the blue... */
4686 sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4687 mflowtype, mflowid, inp->fibnum,
4693 /* ABORT and SHUTDOWN can use either v_tag... */
4694 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4695 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4696 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4697 /* Take the T-bit always into account. */
4698 if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
4699 (vtag_in == asoc->my_vtag)) ||
4700 (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4701 (asoc->peer_vtag != htonl(0)) &&
4702 (vtag_in == asoc->peer_vtag))) {
4705 /* drop this packet... */
4706 SCTP_STAT_INCR(sctps_badvtag);
4708 SCTP_TCB_UNLOCK(stcb);
4712 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4713 if (vtag_in != asoc->my_vtag) {
4715 * this could be a stale SHUTDOWN-ACK or the
4716 * peer never got the SHUTDOWN-COMPLETE and
4717 * is still hung; we have started a new asoc
4718 * but it won't complete until the shutdown
4722 SCTP_TCB_UNLOCK(stcb);
4724 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4725 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4727 sctp_handle_ootb(m, iphlen, *offset, src, dst,
4729 mflowtype, mflowid, fibnum,
4734 /* for all other chunks, vtag must match */
4735 if (vtag_in != asoc->my_vtag) {
4736 /* invalid vtag... */
4737 SCTPDBG(SCTP_DEBUG_INPUT3,
4738 "invalid vtag: %xh, expect %xh\n",
4739 vtag_in, asoc->my_vtag);
4740 SCTP_STAT_INCR(sctps_badvtag);
4742 SCTP_TCB_UNLOCK(stcb);
4748 } /* end if !SCTP_COOKIE_ECHO */
4750 * process all control chunks...
4752 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4753 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4754 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4755 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4756 /* implied cookie-ack.. we must have lost the ack */
4757 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4761 process_control_chunks:
4762 while (IS_SCTP_CONTROL(ch)) {
4763 /* validate chunk length */
4764 chk_length = ntohs(ch->chunk_length);
4765 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4766 ch->chunk_type, chk_length);
4767 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4768 if (chk_length < sizeof(*ch) ||
4769 (*offset + (int)chk_length) > length) {
4773 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4775 * INIT and INIT-ACK only gets the init ack "header" portion
4776 * only because we don't have to process the peer's COOKIE.
4777 * All others get a complete chunk.
4779 switch (ch->chunk_type) {
4780 case SCTP_INITIATION:
4781 contiguous = sizeof(struct sctp_init_chunk);
4783 case SCTP_INITIATION_ACK:
4784 contiguous = sizeof(struct sctp_init_ack_chunk);
4787 contiguous = min(chk_length, sizeof(chunk_buf));
4790 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4796 SCTP_TCB_UNLOCK(stcb);
4802 /* Save off the last place we got a control from */
4804 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4806 * allow last_control to be NULL if
4807 * ASCONF... ASCONF processing will find the
4810 if ((netp != NULL) && (*netp != NULL))
4811 stcb->asoc.last_control_chunk_from = *netp;
4814 #ifdef SCTP_AUDITING_ENABLED
4815 sctp_audit_log(0xB0, ch->chunk_type);
4818 /* check to see if this chunk required auth, but isn't */
4819 if ((stcb != NULL) &&
4820 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4821 !stcb->asoc.authenticated) {
4822 /* "silently" ignore */
4823 SCTP_STAT_INCR(sctps_recvauthmissing);
4826 switch (ch->chunk_type) {
4827 case SCTP_INITIATION:
4828 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4829 /* The INIT chunk must be the only chunk. */
4830 if ((num_chunks > 1) ||
4831 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4832 /* RFC 4960 requires that no ABORT is sent */
4835 SCTP_TCB_UNLOCK(stcb);
4839 /* Honor our resource limit. */
4840 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4841 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4842 sctp_abort_association(inp, stcb, m, iphlen,
4843 src, dst, sh, op_err,
4849 sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4850 (struct sctp_init_chunk *)ch, inp,
4851 stcb, *netp, &abort_no_unlock,
4855 if ((!abort_no_unlock) && (stcb != NULL)) {
4856 SCTP_TCB_UNLOCK(stcb);
4860 case SCTP_PAD_CHUNK:
4862 case SCTP_INITIATION_ACK:
4863 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT_ACK\n");
4864 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4865 /* We are not interested anymore */
4866 if ((stcb != NULL) && (stcb->asoc.total_output_queue_size)) {
4871 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4872 so = SCTP_INP_SO(inp);
4873 atomic_add_int(&stcb->asoc.refcnt, 1);
4874 SCTP_TCB_UNLOCK(stcb);
4875 SCTP_SOCKET_LOCK(so, 1);
4876 SCTP_TCB_LOCK(stcb);
4877 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4879 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4880 SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4881 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4882 SCTP_SOCKET_UNLOCK(so, 1);
4888 /* The INIT-ACK chunk must be the only chunk. */
4889 if ((num_chunks > 1) ||
4890 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4894 if ((netp != NULL) && (*netp != NULL)) {
4895 ret = sctp_handle_init_ack(m, iphlen, *offset,
4897 (struct sctp_init_ack_chunk *)ch,
4906 if (abort_no_unlock) {
4910 * Special case, I must call the output routine to
4911 * get the cookie echoed
4913 if ((stcb != NULL) && (ret == 0)) {
4914 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4918 case SCTP_SELECTIVE_ACK:
4919 case SCTP_NR_SELECTIVE_ACK:
4922 uint32_t a_rwnd, cum_ack;
4923 uint16_t num_seg, num_nr_seg, num_dup;
4925 int offset_seg, offset_dup;
4927 SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n",
4928 ch->chunk_type == SCTP_SELECTIVE_ACK ? "SCTP_SACK" : "SCTP_NR_SACK");
4929 SCTP_STAT_INCR(sctps_recvsacks);
4931 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing %s chunk\n",
4932 (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK");
4935 if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4936 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4937 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4941 if (stcb->asoc.nrsack_supported == 0) {
4944 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4945 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR_SACK chunk, too small\n");
4949 if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4951 * If we have sent a shutdown-ack, we will pay no
4952 * attention to a sack sent in to us since
4953 * we don't care anymore.
4957 flags = ch->chunk_flags;
4958 if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4959 struct sctp_sack_chunk *sack;
4961 sack = (struct sctp_sack_chunk *)ch;
4962 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4963 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4965 num_dup = ntohs(sack->sack.num_dup_tsns);
4966 a_rwnd = ntohl(sack->sack.a_rwnd);
4967 if (sizeof(struct sctp_sack_chunk) +
4968 num_seg * sizeof(struct sctp_gap_ack_block) +
4969 num_dup * sizeof(uint32_t) != chk_length) {
4970 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4973 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4974 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4976 struct sctp_nr_sack_chunk *nr_sack;
4978 nr_sack = (struct sctp_nr_sack_chunk *)ch;
4979 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4980 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4981 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4982 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4983 a_rwnd = ntohl(nr_sack->nr_sack.a_rwnd);
4984 if (sizeof(struct sctp_nr_sack_chunk) +
4985 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4986 num_dup * sizeof(uint32_t) != chk_length) {
4987 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4990 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4991 offset_dup = offset_seg + (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block);
4993 SCTPDBG(SCTP_DEBUG_INPUT3, "%s process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4994 (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK",
4995 cum_ack, num_seg, a_rwnd);
4996 stcb->asoc.seen_a_sack_this_pkt = 1;
4997 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4998 (num_seg == 0) && (num_nr_seg == 0) &&
4999 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
5000 (stcb->asoc.saw_sack_with_frags == 0) &&
5001 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
5002 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
5004 * We have a SIMPLE sack having no
5005 * prior segments and data on sent
5006 * queue to be acked. Use the faster
5007 * path sack processing. We also
5008 * allow window update sacks with no
5009 * missing segments to go this way
5012 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
5013 &abort_now, ecne_seen);
5015 if ((netp != NULL) && (*netp != NULL)) {
5016 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
5017 num_seg, num_nr_seg, num_dup, &abort_now, flags,
5018 cum_ack, a_rwnd, ecne_seen);
5022 /* ABORT signal from sack processing */
5026 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5027 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5028 (stcb->asoc.stream_queue_cnt == 0)) {
5029 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5033 case SCTP_HEARTBEAT_REQUEST:
5034 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
5035 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5036 SCTP_STAT_INCR(sctps_recvheartbeat);
5037 sctp_send_heartbeat_ack(stcb, m, *offset,
5041 case SCTP_HEARTBEAT_ACK:
5042 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT_ACK\n");
5043 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5048 SCTP_STAT_INCR(sctps_recvheartbeatack);
5049 if ((netp != NULL) && (*netp != NULL)) {
5050 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5054 case SCTP_ABORT_ASSOCIATION:
5055 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5058 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5059 if (sctp_handle_abort((struct sctp_abort_chunk *)ch, stcb, *netp)) {
5069 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5071 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5075 if ((netp != NULL) && (*netp != NULL)) {
5078 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5079 stcb, *netp, &abort_flag);
5086 case SCTP_SHUTDOWN_ACK:
5087 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_ACK, stcb %p\n", (void *)stcb);
5088 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5089 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5094 case SCTP_OPERATION_ERROR:
5095 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP_ERR\n");
5096 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL) &&
5097 sctp_handle_error(ch, stcb, *netp, contiguous) < 0) {
5102 case SCTP_COOKIE_ECHO:
5103 SCTPDBG(SCTP_DEBUG_INPUT3,
5104 "SCTP_COOKIE_ECHO, stcb %p\n", (void *)stcb);
5105 if ((stcb != NULL) && (stcb->asoc.total_output_queue_size > 0)) {
5108 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5109 /* We are not interested anymore */
5112 SCTP_TCB_UNLOCK(stcb);
5119 * First are we accepting? We do this again here
5120 * since it is possible that a previous endpoint WAS
5121 * listening responded to a INIT-ACK and then
5122 * closed. We opened and bound.. and are now no
5125 * XXXGL: notes on checking listen queue length.
5126 * 1) SCTP_IS_LISTENING() doesn't necessarily mean
5127 * SOLISTENING(), because a listening "UDP type"
5128 * socket isn't listening in terms of the socket
5129 * layer. It is a normal data flow socket, that
5130 * can fork off new connections. Thus, we should
5131 * look into sol_qlen only in case we are !UDP.
5132 * 2) Checking sol_qlen in general requires locking
5133 * the socket, and this code lacks that.
5135 if ((stcb == NULL) &&
5136 (!SCTP_IS_LISTENING(inp) ||
5137 (!(inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
5138 inp->sctp_socket->sol_qlen >= inp->sctp_socket->sol_qlimit))) {
5139 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5140 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5141 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
5142 sctp_abort_association(inp, stcb, m, iphlen,
5143 src, dst, sh, op_err,
5150 struct mbuf *ret_buf;
5151 struct sctp_inpcb *linp;
5160 SCTP_ASOC_CREATE_LOCK(linp);
5161 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5162 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5163 SCTP_ASOC_CREATE_UNLOCK(linp);
5169 struct sctp_tcb *locked_stcb;
5173 sctp_handle_cookie_echo(m, iphlen,
5177 (struct sctp_cookie_echo_chunk *)ch,
5187 if ((locked_stcb != NULL) && (locked_stcb != stcb)) {
5188 SCTP_TCB_UNLOCK(locked_stcb);
5191 SCTP_TCB_LOCK_ASSERT(stcb);
5197 SCTP_ASOC_CREATE_UNLOCK(linp);
5199 if (ret_buf == NULL) {
5201 SCTP_TCB_UNLOCK(stcb);
5203 SCTPDBG(SCTP_DEBUG_INPUT3,
5204 "GAK, null buffer\n");
5208 /* if AUTH skipped, see if it verified... */
5213 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5215 * Restart the timer if we have
5218 struct sctp_tmit_chunk *chk;
5220 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5221 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5225 case SCTP_COOKIE_ACK:
5226 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE_ACK, stcb %p\n", (void *)stcb);
5227 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5230 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5231 /* We are not interested anymore */
5232 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5235 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5236 so = SCTP_INP_SO(inp);
5237 atomic_add_int(&stcb->asoc.refcnt, 1);
5238 SCTP_TCB_UNLOCK(stcb);
5239 SCTP_SOCKET_LOCK(so, 1);
5240 SCTP_TCB_LOCK(stcb);
5241 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5243 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5244 SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5245 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5246 SCTP_SOCKET_UNLOCK(so, 1);
5252 if ((netp != NULL) && (*netp != NULL)) {
5253 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5257 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_ECHO\n");
5258 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5263 if (stcb->asoc.ecn_supported == 0) {
5266 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, stcb);
5270 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_CWR\n");
5271 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5275 if (stcb->asoc.ecn_supported == 0) {
5278 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5280 case SCTP_SHUTDOWN_COMPLETE:
5281 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_COMPLETE, stcb %p\n", (void *)stcb);
5282 /* must be first and only chunk */
5283 if ((num_chunks > 1) ||
5284 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5288 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5289 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5296 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5298 if (stcb->asoc.asconf_supported == 0) {
5301 sctp_handle_asconf(m, *offset, src,
5302 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5306 case SCTP_ASCONF_ACK:
5307 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF_ACK\n");
5308 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5313 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5314 if (stcb->asoc.asconf_supported == 0) {
5317 /* He's alive so give him credit */
5318 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5319 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5320 stcb->asoc.overall_error_count,
5322 SCTP_FROM_SCTP_INPUT,
5325 stcb->asoc.overall_error_count = 0;
5326 sctp_handle_asconf_ack(m, *offset,
5327 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5328 if (abort_no_unlock)
5332 case SCTP_FORWARD_CUM_TSN:
5333 case SCTP_IFORWARD_CUM_TSN:
5334 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD_TSN\n");
5335 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5344 if (stcb->asoc.prsctp_supported == 0) {
5348 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5349 /* We are not interested anymore */
5350 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5351 so = SCTP_INP_SO(inp);
5352 atomic_add_int(&stcb->asoc.refcnt, 1);
5353 SCTP_TCB_UNLOCK(stcb);
5354 SCTP_SOCKET_LOCK(so, 1);
5355 SCTP_TCB_LOCK(stcb);
5356 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5358 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5359 SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5360 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5361 SCTP_SOCKET_UNLOCK(so, 1);
5367 * For sending a SACK this looks like DATA
5370 stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from;
5371 sctp_handle_forward_tsn(stcb,
5372 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5379 case SCTP_STREAM_RESET:
5380 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5381 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5386 if (stcb->asoc.reconfig_supported == 0) {
5389 if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5390 /* stop processing */
5395 case SCTP_PACKET_DROPPED:
5396 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5397 /* re-get it all please */
5398 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5404 if ((ch != NULL) && (stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5405 if (stcb->asoc.pktdrop_supported == 0) {
5408 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5410 min(chk_length, contiguous));
5413 case SCTP_AUTHENTICATION:
5414 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5416 /* save the first AUTH for later processing */
5417 if (auth_skipped == 0) {
5418 auth_offset = *offset;
5419 auth_len = chk_length;
5422 /* skip this chunk (temporarily) */
5425 if (stcb->asoc.auth_supported == 0) {
5428 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5429 (chk_length > (sizeof(struct sctp_auth_chunk) +
5430 SCTP_AUTH_DIGEST_LEN_MAX))) {
5435 if (got_auth == 1) {
5436 /* skip this chunk... it's already auth'd */
5440 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5442 /* auth HMAC failed so dump the packet */
5446 /* remaining chunks are HMAC checked */
5447 stcb->asoc.authenticated = 1;
5453 /* it's an unknown chunk! */
5454 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5455 struct sctp_gen_error_cause *cause;
5458 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5459 0, M_NOWAIT, 1, MT_DATA);
5460 if (op_err != NULL) {
5461 len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset));
5462 cause = mtod(op_err, struct sctp_gen_error_cause *);
5463 cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5464 cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause)));
5465 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
5466 SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
5467 if (SCTP_BUF_NEXT(op_err) != NULL) {
5468 #ifdef SCTP_MBUF_LOGGING
5469 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5470 sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5473 sctp_queue_op_err(stcb, op_err);
5475 sctp_m_freem(op_err);
5479 if ((ch->chunk_type & 0x80) == 0) {
5480 /* discard this packet */
5483 } /* else skip this bad chunk and continue... */
5485 } /* switch (ch->chunk_type) */
5489 /* get the next chunk */
5490 *offset += SCTP_SIZE32(chk_length);
5491 if (*offset >= length) {
5492 /* no more data left in the mbuf chain */
5495 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5496 sizeof(struct sctp_chunkhdr), chunk_buf);
5503 if ((asconf_cnt > 0) && (stcb != NULL)) {
5504 sctp_send_asconf_ack(stcb);
5511 * common input chunk processing (v4 and v6)
5514 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5515 struct sockaddr *src, struct sockaddr *dst,
5516 struct sctphdr *sh, struct sctp_chunkhdr *ch,
5517 uint8_t compute_crc,
5519 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5520 uint32_t vrf_id, uint16_t port)
5523 int fwd_tsn_seen = 0, data_processed = 0;
5524 struct mbuf *m = *mm, *op_err;
5525 char msg[SCTP_DIAG_INFO_LEN];
5527 int cnt_ctrl_ready = 0;
5528 struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5529 struct sctp_tcb *stcb = NULL;
5530 struct sctp_nets *net = NULL;
5532 SCTP_STAT_INCR(sctps_recvdatagrams);
5533 #ifdef SCTP_AUDITING_ENABLED
5534 sctp_audit_log(0xE0, 1);
5535 sctp_auditing(0, inp, stcb, net);
5537 if (compute_crc != 0) {
5538 uint32_t check, calc_check;
5540 check = sh->checksum;
5542 calc_check = sctp_calculate_cksum(m, iphlen);
5543 sh->checksum = check;
5544 if (calc_check != check) {
5545 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5546 calc_check, check, (void *)m, length, iphlen);
5547 stcb = sctp_findassociation_addr(m, offset, src, dst,
5548 sh, ch, &inp, &net, vrf_id);
5549 #if defined(INET) || defined(INET6)
5550 if ((ch->chunk_type != SCTP_INITIATION) &&
5551 (net != NULL) && (net->port != port)) {
5552 if (net->port == 0) {
5553 /* UDP encapsulation turned on. */
5554 net->mtu -= sizeof(struct udphdr);
5555 if (stcb->asoc.smallest_mtu > net->mtu) {
5556 sctp_pathmtu_adjustment(stcb, net->mtu);
5558 } else if (port == 0) {
5559 /* UDP encapsulation turned off. */
5560 net->mtu += sizeof(struct udphdr);
5561 /* XXX Update smallest_mtu */
5567 net->flowtype = mflowtype;
5568 net->flowid = mflowid;
5570 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5571 if ((inp != NULL) && (stcb != NULL)) {
5572 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5573 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5574 } else if ((inp != NULL) && (stcb == NULL)) {
5577 SCTP_STAT_INCR(sctps_badsum);
5578 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5582 /* Destination port of 0 is illegal, based on RFC4960. */
5583 if (sh->dest_port == 0) {
5584 SCTP_STAT_INCR(sctps_hdrops);
5587 stcb = sctp_findassociation_addr(m, offset, src, dst,
5588 sh, ch, &inp, &net, vrf_id);
5589 #if defined(INET) || defined(INET6)
5590 if ((ch->chunk_type != SCTP_INITIATION) &&
5591 (net != NULL) && (net->port != port)) {
5592 if (net->port == 0) {
5593 /* UDP encapsulation turned on. */
5594 net->mtu -= sizeof(struct udphdr);
5595 if (stcb->asoc.smallest_mtu > net->mtu) {
5596 sctp_pathmtu_adjustment(stcb, net->mtu);
5598 } else if (port == 0) {
5599 /* UDP encapsulation turned off. */
5600 net->mtu += sizeof(struct udphdr);
5601 /* XXX Update smallest_mtu */
5607 net->flowtype = mflowtype;
5608 net->flowid = mflowid;
5611 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5612 SCTP_STAT_INCR(sctps_noport);
5613 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5616 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5617 sctp_send_shutdown_complete2(src, dst, sh,
5618 mflowtype, mflowid, fibnum,
5622 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5625 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5626 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5627 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5628 (ch->chunk_type != SCTP_INIT))) {
5629 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5631 sctp_send_abort(m, iphlen, src, dst,
5633 mflowtype, mflowid, fibnum,
5638 } else if (stcb == NULL) {
5641 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5642 (void *)m, iphlen, offset, length, (void *)stcb);
5644 /* always clear this before beginning a packet */
5645 stcb->asoc.authenticated = 0;
5646 stcb->asoc.seen_a_sack_this_pkt = 0;
5647 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5648 (void *)stcb, stcb->asoc.state);
5650 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5651 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5653 * If we hit here, we had a ref count
5654 * up when the assoc was aborted and the
5655 * timer is clearing out the assoc, we should
5656 * NOT respond to any packet.. its OOTB.
5658 SCTP_TCB_UNLOCK(stcb);
5660 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5661 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5662 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5664 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5665 mflowtype, mflowid, inp->fibnum,
5670 if (IS_SCTP_CONTROL(ch)) {
5671 /* process the control portion of the SCTP packet */
5672 /* sa_ignore NO_NULL_CHK */
5673 stcb = sctp_process_control(m, iphlen, &offset, length,
5675 inp, stcb, &net, &fwd_tsn_seen,
5676 mflowtype, mflowid, fibnum,
5680 * This covers us if the cookie-echo was there and
5681 * it changes our INP.
5683 inp = stcb->sctp_ep;
5684 #if defined(INET) || defined(INET6)
5685 if ((ch->chunk_type != SCTP_INITIATION) &&
5686 (net != NULL) && (net->port != port)) {
5687 if (net->port == 0) {
5688 /* UDP encapsulation turned on. */
5689 net->mtu -= sizeof(struct udphdr);
5690 if (stcb->asoc.smallest_mtu > net->mtu) {
5691 sctp_pathmtu_adjustment(stcb, net->mtu);
5693 } else if (port == 0) {
5694 /* UDP encapsulation turned off. */
5695 net->mtu += sizeof(struct udphdr);
5696 /* XXX Update smallest_mtu */
5704 * no control chunks, so pre-process DATA chunks (these
5705 * checks are taken care of by control processing)
5709 * if DATA only packet, and auth is required, then punt...
5710 * can't have authenticated without any AUTH (control)
5713 if ((stcb != NULL) &&
5714 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5715 /* "silently" ignore */
5716 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5717 SCTP_STAT_INCR(sctps_recvauthmissing);
5721 /* out of the blue DATA chunk */
5722 SCTP_PROBE5(receive, NULL, NULL, m, NULL, sh);
5723 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5724 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5726 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5727 mflowtype, mflowid, fibnum,
5731 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5732 /* v_tag mismatch! */
5733 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5734 SCTP_STAT_INCR(sctps_badvtag);
5739 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5742 * no valid TCB for this packet, or we found it's a bad
5743 * packet while processing control, or we're done with this
5744 * packet (done or skip rest of data), so we drop it...
5750 * DATA chunk processing
5752 /* plow through the data chunks while length > offset */
5755 * Rest should be DATA only. Check authentication state if AUTH for
5758 if ((length > offset) &&
5760 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5761 !stcb->asoc.authenticated) {
5762 /* "silently" ignore */
5763 SCTP_STAT_INCR(sctps_recvauthmissing);
5764 SCTPDBG(SCTP_DEBUG_AUTH1,
5765 "Data chunk requires AUTH, skipped\n");
5768 if (length > offset) {
5772 * First check to make sure our state is correct. We would
5773 * not get here unless we really did have a tag, so we don't
5774 * abort if this happens, just dump the chunk silently.
5776 switch (SCTP_GET_STATE(stcb)) {
5777 case SCTP_STATE_COOKIE_ECHOED:
5779 * we consider data with valid tags in this state
5780 * shows us the cookie-ack was lost. Imply it was
5783 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5785 case SCTP_STATE_COOKIE_WAIT:
5787 * We consider OOTB any data sent during asoc setup.
5789 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5790 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5792 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5793 mflowtype, mflowid, inp->fibnum,
5796 /* sa_ignore NOTREACHED */
5798 case SCTP_STATE_EMPTY: /* should not happen */
5799 case SCTP_STATE_INUSE: /* should not happen */
5800 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5801 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5804 /* sa_ignore NOTREACHED */
5806 case SCTP_STATE_OPEN:
5807 case SCTP_STATE_SHUTDOWN_SENT:
5810 /* plow through the data chunks while length > offset */
5811 retval = sctp_process_data(mm, iphlen, &offset, length,
5812 inp, stcb, net, &high_tsn);
5815 * The association aborted, NO UNLOCK needed since
5816 * the association is destroyed.
5823 * Anything important needs to have been m_copy'ed in
5828 /* take care of ecn */
5829 if ((data_processed == 1) &&
5830 (stcb->asoc.ecn_supported == 1) &&
5831 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5832 /* Yep, we need to add a ECNE */
5833 sctp_send_ecn_echo(stcb, net, high_tsn);
5836 if ((data_processed == 0) && (fwd_tsn_seen)) {
5838 uint32_t highest_tsn;
5840 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5841 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5843 highest_tsn = stcb->asoc.highest_tsn_inside_map;
5845 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
5846 stcb->asoc.send_sack = 1;
5847 sctp_sack_check(stcb, was_a_gap);
5848 } else if (fwd_tsn_seen) {
5849 stcb->asoc.send_sack = 1;
5851 /* trigger send of any chunks in queue... */
5853 #ifdef SCTP_AUDITING_ENABLED
5854 sctp_audit_log(0xE0, 2);
5855 sctp_auditing(1, inp, stcb, net);
5857 SCTPDBG(SCTP_DEBUG_INPUT1,
5858 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5859 stcb->asoc.peers_rwnd,
5860 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5861 stcb->asoc.total_flight);
5862 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5863 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5864 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5866 if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
5868 stcb->asoc.trigger_reset ||
5870 (stcb->asoc.peers_rwnd > 0 ||
5871 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5872 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5873 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5874 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5876 #ifdef SCTP_AUDITING_ENABLED
5877 sctp_audit_log(0xE0, 3);
5878 sctp_auditing(2, inp, stcb, net);
5882 SCTP_TCB_UNLOCK(stcb);
5884 if (inp_decr != NULL) {
5885 /* reduce ref-count */
5886 SCTP_INP_WLOCK(inp_decr);
5887 SCTP_INP_DECR_REF(inp_decr);
5888 SCTP_INP_WUNLOCK(inp_decr);
5895 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5899 uint32_t vrf_id = 0;
5901 struct sockaddr_in src, dst;
5904 struct sctp_chunkhdr *ch;
5906 uint8_t compute_crc;
5912 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
5913 SCTP_RELEASE_PKT(i_pak);
5916 m = SCTP_HEADER_TO_CHAIN(i_pak);
5917 #ifdef SCTP_MBUF_LOGGING
5918 /* Log in any input mbufs */
5919 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5920 sctp_log_mbc(m, SCTP_MBUF_INPUT);
5923 #ifdef SCTP_PACKET_LOGGING
5924 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
5928 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
5929 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
5931 if_name(m->m_pkthdr.rcvif),
5932 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
5933 mflowid = m->m_pkthdr.flowid;
5934 mflowtype = M_HASHTYPE_GET(m);
5935 fibnum = M_GETFIB(m);
5936 SCTP_STAT_INCR(sctps_recvpackets);
5937 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
5938 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
5939 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5940 if (SCTP_BUF_LEN(m) < offset) {
5941 if ((m = m_pullup(m, offset)) == NULL) {
5942 SCTP_STAT_INCR(sctps_hdrops);
5946 ip = mtod(m, struct ip *);
5947 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
5948 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
5949 offset -= sizeof(struct sctp_chunkhdr);
5950 memset(&src, 0, sizeof(struct sockaddr_in));
5951 src.sin_family = AF_INET;
5952 src.sin_len = sizeof(struct sockaddr_in);
5953 src.sin_port = sh->src_port;
5954 src.sin_addr = ip->ip_src;
5955 memset(&dst, 0, sizeof(struct sockaddr_in));
5956 dst.sin_family = AF_INET;
5957 dst.sin_len = sizeof(struct sockaddr_in);
5958 dst.sin_port = sh->dest_port;
5959 dst.sin_addr = ip->ip_dst;
5960 length = ntohs(ip->ip_len);
5961 /* Validate mbuf chain length with IP payload length. */
5962 if (SCTP_HEADER_LEN(m) != length) {
5963 SCTPDBG(SCTP_DEBUG_INPUT1,
5964 "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
5965 SCTP_STAT_INCR(sctps_hdrops);
5968 /* SCTP does not allow broadcasts or multicasts */
5969 if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
5972 if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
5975 ecn_bits = ip->ip_tos;
5976 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5977 SCTP_STAT_INCR(sctps_recvhwcrc);
5980 SCTP_STAT_INCR(sctps_recvswcrc);
5983 sctp_common_input_processing(&m, iphlen, offset, length,
5984 (struct sockaddr *)&src,
5985 (struct sockaddr *)&dst,
5989 mflowtype, mflowid, fibnum,
5998 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
5999 extern int *sctp_cpuarry;
6003 sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
6010 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6016 uint32_t flowid, tag;
6018 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
6019 flowid = m->m_pkthdr.flowid;
6022 * No flow id built by lower layers fix it so we
6025 offset = off + sizeof(struct sctphdr);
6026 if (SCTP_BUF_LEN(m) < offset) {
6027 if ((m = m_pullup(m, offset)) == NULL) {
6028 SCTP_STAT_INCR(sctps_hdrops);
6029 return (IPPROTO_DONE);
6032 ip = mtod(m, struct ip *);
6033 sh = (struct sctphdr *)((caddr_t)ip + off);
6034 tag = htonl(sh->v_tag);
6035 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6036 m->m_pkthdr.flowid = flowid;
6037 M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH);
6039 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6040 sctp_queue_to_mcore(m, off, cpu_to_use);
6041 return (IPPROTO_DONE);
6044 sctp_input_with_port(m, off, 0);
6045 return (IPPROTO_DONE);