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 #include <netinet/sctp_kdtrace.h>
53 #if defined(INET) || defined(INET6)
54 #include <netinet/udp.h>
59 sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
61 struct sctp_nets *net;
64 * This now not only stops all cookie timers it also stops any INIT
65 * timers as well. This will make sure that the timers are stopped
66 * in all collision cases.
68 SCTP_TCB_LOCK_ASSERT(stcb);
69 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
70 if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
71 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
74 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
75 } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
76 sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
79 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
86 sctp_handle_init(struct mbuf *m, int iphlen, int offset,
87 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
88 struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
89 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_no_unlock,
90 uint8_t mflowtype, uint32_t mflowid,
91 uint32_t vrf_id, uint16_t port)
93 struct sctp_init *init;
96 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
101 /* validate length */
102 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
103 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
104 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
108 *abort_no_unlock = 1;
111 /* validate parameters */
113 if (init->initiate_tag == 0) {
114 /* protocol error... send abort */
115 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
116 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
120 *abort_no_unlock = 1;
123 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
124 /* invalid parameter... send abort */
125 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
126 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
130 *abort_no_unlock = 1;
133 if (init->num_inbound_streams == 0) {
134 /* protocol error... send abort */
135 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
136 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
140 *abort_no_unlock = 1;
143 if (init->num_outbound_streams == 0) {
144 /* protocol error... send abort */
145 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
146 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
150 *abort_no_unlock = 1;
153 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
154 offset + ntohs(cp->ch.chunk_length))) {
155 /* auth parameter(s) error... send abort */
156 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
157 "Problem with AUTH parameters");
158 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
162 *abort_no_unlock = 1;
165 /* We are only accepting if we have a listening socket. */
166 if ((stcb == NULL) &&
167 ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
168 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
169 (!SCTP_IS_LISTENING(inp)))) {
171 * FIX ME ?? What about TCP model and we have a
172 * match/restart case? Actually no fix is needed. the lookup
173 * will always find the existing assoc so stcb would not be
174 * NULL. It may be questionable to do this since we COULD
175 * just send back the INIT-ACK and hope that the app did
176 * accept()'s by the time the COOKIE was sent. But there is
177 * a price to pay for COOKIE generation and I don't want to
178 * pay it on the chance that the app will actually do some
179 * accepts(). The App just looses and should NOT be in this
182 if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
183 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
185 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
186 mflowtype, mflowid, inp->fibnum,
191 if ((stcb != NULL) &&
192 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
193 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
194 sctp_send_shutdown_ack(stcb, NULL);
195 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
197 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
198 sctp_send_initiate_ack(inp, stcb, net, m, iphlen, offset,
205 SCTP_INP_RUNLOCK(inp);
210 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
214 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked)
218 struct sctp_stream_queue_pending *sp;
219 struct sctp_association *asoc;
222 * This function returns if any stream has true unsent data on it.
223 * Note that as it looks through it will clean up any places that
224 * have old data that has been sent but left at top of stream queue.
228 SCTP_TCB_SEND_LOCK(stcb);
229 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
230 /* Check to see if some data queued */
231 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
232 /* sa_ignore FREED_MEMORY */
233 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
237 if ((sp->msg_is_complete) &&
239 (sp->sender_all_done)) {
241 * We are doing differed cleanup. Last time
242 * through when we took all the data the
243 * sender_all_done was not set.
245 if (sp->put_last_out == 0) {
246 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
247 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
253 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
254 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
255 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, &asoc->strmout[i], sp, 1);
257 sctp_free_remote_addr(sp->net);
261 sctp_m_freem(sp->data);
264 sctp_free_a_strmoq(stcb, sp, so_locked);
265 if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
271 if (unsent_data > 0) {
276 SCTP_TCB_SEND_UNLOCK(stcb);
277 return (unsent_data);
281 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
283 struct sctp_init *init;
284 struct sctp_association *asoc;
285 struct sctp_nets *lnet;
290 /* save off parameters */
291 asoc->peer_vtag = ntohl(init->initiate_tag);
292 asoc->peers_rwnd = ntohl(init->a_rwnd);
294 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
296 if (!TAILQ_EMPTY(&asoc->nets)) {
297 /* update any ssthresh's that may have a default */
298 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
299 lnet->ssthresh = asoc->peers_rwnd;
300 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
301 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
306 SCTP_TCB_SEND_LOCK(stcb);
307 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
309 struct sctp_stream_out *outs;
310 struct sctp_stream_queue_pending *sp, *nsp;
311 struct sctp_tmit_chunk *chk, *nchk;
313 /* abandon the upper streams */
314 newcnt = ntohs(init->num_inbound_streams);
315 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
316 if (chk->rec.data.sid >= newcnt) {
317 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
318 asoc->send_queue_cnt--;
319 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
320 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
323 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
326 if (chk->data != NULL) {
327 sctp_free_bufspace(stcb, asoc, chk, 1);
328 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
329 0, chk, SCTP_SO_NOT_LOCKED);
331 sctp_m_freem(chk->data);
335 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
336 /* sa_ignore FREED_MEMORY */
340 for (i = newcnt; i < asoc->pre_open_streams; i++) {
341 outs = &asoc->strmout[i];
342 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
343 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
344 TAILQ_REMOVE(&outs->outqueue, sp, next);
345 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
346 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
347 stcb, 0, sp, SCTP_SO_NOT_LOCKED);
349 sctp_m_freem(sp->data);
353 sctp_free_remote_addr(sp->net);
357 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
358 /* sa_ignore FREED_MEMORY */
360 outs->state = SCTP_STREAM_CLOSED;
363 /* cut back the count */
364 asoc->pre_open_streams = newcnt;
366 SCTP_TCB_SEND_UNLOCK(stcb);
367 asoc->streamoutcnt = asoc->pre_open_streams;
369 for (i = 0; i < asoc->streamoutcnt; i++) {
370 asoc->strmout[i].state = SCTP_STREAM_OPEN;
373 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
374 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
375 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
376 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
378 /* This is the next one we expect */
379 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
381 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
382 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
384 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
385 /* open the requested streams */
387 if (asoc->strmin != NULL) {
388 /* Free the old ones */
389 for (i = 0; i < asoc->streamincnt; i++) {
390 sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
391 sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
393 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
395 if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
396 asoc->streamincnt = ntohs(init->num_outbound_streams);
398 asoc->streamincnt = asoc->max_inbound_streams;
400 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
401 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
402 if (asoc->strmin == NULL) {
403 /* we didn't get memory for the streams! */
404 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
407 for (i = 0; i < asoc->streamincnt; i++) {
408 asoc->strmin[i].sid = i;
409 asoc->strmin[i].last_mid_delivered = 0xffffffff;
410 TAILQ_INIT(&asoc->strmin[i].inqueue);
411 TAILQ_INIT(&asoc->strmin[i].uno_inqueue);
412 asoc->strmin[i].pd_api_started = 0;
413 asoc->strmin[i].delivery_started = 0;
416 * load_address_from_init will put the addresses into the
417 * association when the COOKIE is processed or the INIT-ACK is
418 * processed. Both types of COOKIE's existing and new call this
419 * routine. It will remove addresses that are no longer in the
420 * association (for the restarting case where addresses are
421 * removed). Up front when the INIT arrives we will discard it if it
422 * is a restart and new addresses have been added.
424 /* sa_ignore MEMLEAK */
429 * INIT-ACK message processing/consumption returns value < 0 on error
432 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
433 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
434 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
435 struct sctp_nets *net, int *abort_no_unlock,
436 uint8_t mflowtype, uint32_t mflowid,
439 struct sctp_association *asoc;
441 int retval, abort_flag, cookie_found;
443 int nat_friendly = 0;
445 /* First verify that we have no illegal param's */
449 op_err = sctp_arethere_unrecognized_parameters(m,
450 (offset + sizeof(struct sctp_init_chunk)),
451 &abort_flag, (struct sctp_chunkhdr *)cp,
452 &nat_friendly, &cookie_found);
454 /* Send an abort and notify peer */
455 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
456 *abort_no_unlock = 1;
462 /* Only report the missing cookie parameter */
463 if (op_err != NULL) {
464 sctp_m_freem(op_err);
466 len = (uint16_t)(sizeof(struct sctp_error_missing_param) + sizeof(uint16_t));
467 /* We abort with an error of missing mandatory param */
468 op_err = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
469 if (op_err != NULL) {
470 struct sctp_error_missing_param *cause;
472 SCTP_BUF_LEN(op_err) = len;
473 cause = mtod(op_err, struct sctp_error_missing_param *);
474 /* Subtract the reserved param */
475 cause->cause.code = htons(SCTP_CAUSE_MISSING_PARAM);
476 cause->cause.length = htons(len);
477 cause->num_missing_params = htonl(1);
478 cause->type[0] = htons(SCTP_STATE_COOKIE);
480 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
481 src, dst, sh, op_err,
484 *abort_no_unlock = 1;
488 asoc->peer_supports_nat = (uint8_t)nat_friendly;
489 /* process the peer's parameters in the INIT-ACK */
490 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
492 if (op_err != NULL) {
493 sctp_m_freem(op_err);
497 initack_limit = offset + ntohs(cp->ch.chunk_length);
498 /* load all addresses */
499 if ((retval = sctp_load_addresses_from_init(stcb, m,
500 (offset + sizeof(struct sctp_init_chunk)), initack_limit,
501 src, dst, NULL, stcb->asoc.port))) {
502 if (op_err != NULL) {
503 sctp_m_freem(op_err);
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);
706 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
709 * Return 0 means we want you to proceed with the abort non-zero
710 * means no abort processing.
713 struct sctpasochead *head;
715 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
716 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
717 new_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
718 atomic_add_int(&stcb->asoc.refcnt, 1);
719 SCTP_TCB_UNLOCK(stcb);
720 SCTP_INP_INFO_WLOCK();
722 atomic_subtract_int(&stcb->asoc.refcnt, 1);
726 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
727 /* generate a new vtag and send init */
728 LIST_REMOVE(stcb, sctp_asocs);
729 stcb->asoc.my_vtag = new_vtag;
730 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
732 * put it in the bucket in the vtag hash of assoc's for the
735 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
736 SCTP_INP_INFO_WUNLOCK();
737 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
741 * treat like a case where the cookie expired i.e.: - dump
742 * current cookie. - generate a new vtag. - resend init.
744 /* generate a new vtag and send init */
745 LIST_REMOVE(stcb, sctp_asocs);
746 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
747 sctp_stop_all_cookie_timers(stcb);
748 sctp_toss_old_cookies(stcb, &stcb->asoc);
749 stcb->asoc.my_vtag = new_vtag;
750 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
752 * put it in the bucket in the vtag hash of assoc's for the
755 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
756 SCTP_INP_INFO_WUNLOCK();
757 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
764 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
765 struct sctp_nets *net)
768 * return 0 means we want you to proceed with the abort non-zero
769 * means no abort processing
771 if (stcb->asoc.auth_supported == 0) {
772 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
775 sctp_asconf_send_nat_state_update(stcb, net);
780 /* Returns 1 if the stcb was aborted, 0 otherwise */
782 sctp_handle_abort(struct sctp_abort_chunk *abort,
783 struct sctp_tcb *stcb, struct sctp_nets *net)
788 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
792 len = ntohs(abort->ch.chunk_length);
793 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_error_cause)) {
795 * Need to check the cause codes for our two magic nat
796 * aborts which don't kill the assoc necessarily.
798 struct sctp_error_cause *cause;
800 cause = (struct sctp_error_cause *)(abort + 1);
801 error = ntohs(cause->code);
802 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
803 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
804 abort->ch.chunk_flags);
805 if (sctp_handle_nat_colliding_state(stcb)) {
808 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
809 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
810 abort->ch.chunk_flags);
811 if (sctp_handle_nat_missing_state(stcb, net)) {
818 /* stop any receive timers */
819 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
820 SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
821 /* notify user of the abort and clean up... */
822 sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
824 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
825 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
826 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
827 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
829 #ifdef SCTP_ASOCLOG_OF_TSNS
830 sctp_print_out_track_log(stcb);
832 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
833 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
834 SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
835 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
840 sctp_start_net_timers(struct sctp_tcb *stcb)
842 uint32_t cnt_hb_sent;
843 struct sctp_nets *net;
846 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
848 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
849 * If the dest in unconfirmed send a hb as well if under
850 * max_hb_burst have been sent.
852 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
853 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
854 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
855 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
856 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
861 sctp_chunk_output(stcb->sctp_ep, stcb,
862 SCTP_OUTPUT_FROM_COOKIE_ACK,
869 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
870 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
872 struct sctp_association *asoc;
873 int some_on_streamwheel;
876 SCTPDBG(SCTP_DEBUG_INPUT2,
877 "sctp_handle_shutdown: handling SHUTDOWN\n");
881 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
882 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
885 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
886 /* Shutdown NOT the expected size */
889 old_state = SCTP_GET_STATE(stcb);
890 sctp_update_acked(stcb, cp, abort_flag);
894 if (asoc->control_pdapi) {
896 * With a normal shutdown we assume the end of last record.
898 SCTP_INP_READ_LOCK(stcb->sctp_ep);
899 if (asoc->control_pdapi->on_strm_q) {
900 struct sctp_stream_in *strm;
902 strm = &asoc->strmin[asoc->control_pdapi->sinfo_stream];
903 if (asoc->control_pdapi->on_strm_q == SCTP_ON_UNORDERED) {
905 TAILQ_REMOVE(&strm->uno_inqueue, asoc->control_pdapi, next_instrm);
906 asoc->control_pdapi->on_strm_q = 0;
907 } else if (asoc->control_pdapi->on_strm_q == SCTP_ON_ORDERED) {
909 TAILQ_REMOVE(&strm->inqueue, asoc->control_pdapi, next_instrm);
910 asoc->control_pdapi->on_strm_q = 0;
913 panic("Unknown state on ctrl:%p on_strm_q:%d",
915 asoc->control_pdapi->on_strm_q);
919 asoc->control_pdapi->end_added = 1;
920 asoc->control_pdapi->pdapi_aborted = 1;
921 asoc->control_pdapi = NULL;
922 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
923 if (stcb->sctp_socket) {
924 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
927 /* goto SHUTDOWN_RECEIVED state to block new requests */
928 if (stcb->sctp_socket) {
929 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
930 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
931 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) {
932 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_RECEIVED);
934 * notify upper layer that peer has initiated a
937 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
940 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
943 if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) {
945 * stop the shutdown timer, since we WILL move to
948 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
949 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
951 /* Now is there unsent data on a stream somewhere? */
952 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
954 if (!TAILQ_EMPTY(&asoc->send_queue) ||
955 !TAILQ_EMPTY(&asoc->sent_queue) ||
956 some_on_streamwheel) {
957 /* By returning we will push more data out */
960 /* no outstanding data to send, so move on... */
961 /* send SHUTDOWN-ACK */
962 /* move to SHUTDOWN-ACK-SENT state */
963 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
964 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
965 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
967 if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
968 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_ACK_SENT);
969 sctp_stop_timers_for_shutdown(stcb);
970 sctp_send_shutdown_ack(stcb, net);
971 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
972 stcb->sctp_ep, stcb, net);
973 } else if (old_state == SCTP_STATE_SHUTDOWN_ACK_SENT) {
974 sctp_send_shutdown_ack(stcb, net);
980 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
981 struct sctp_tcb *stcb,
982 struct sctp_nets *net)
984 struct sctp_association *asoc;
986 SCTPDBG(SCTP_DEBUG_INPUT2,
987 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
992 /* process according to association state */
993 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
994 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
995 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
996 sctp_send_shutdown_complete(stcb, net, 1);
997 SCTP_TCB_UNLOCK(stcb);
1000 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
1001 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1002 /* unexpected SHUTDOWN-ACK... so ignore... */
1003 SCTP_TCB_UNLOCK(stcb);
1006 if (asoc->control_pdapi) {
1008 * With a normal shutdown we assume the end of last record.
1010 SCTP_INP_READ_LOCK(stcb->sctp_ep);
1011 asoc->control_pdapi->end_added = 1;
1012 asoc->control_pdapi->pdapi_aborted = 1;
1013 asoc->control_pdapi = NULL;
1014 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1015 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1018 if (!TAILQ_EMPTY(&asoc->send_queue) ||
1019 !TAILQ_EMPTY(&asoc->sent_queue) ||
1020 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
1021 panic("Queues are not empty when handling SHUTDOWN-ACK");
1024 /* stop the timer */
1025 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net,
1026 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1027 /* send SHUTDOWN-COMPLETE */
1028 sctp_send_shutdown_complete(stcb, net, 0);
1029 /* notify upper layer protocol */
1030 if (stcb->sctp_socket) {
1031 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1032 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1033 stcb->sctp_socket->so_snd.sb_cc = 0;
1035 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1037 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1038 /* free the TCB but first save off the ep */
1039 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1040 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1044 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, uint8_t chunk_type)
1046 switch (chunk_type) {
1047 case SCTP_ASCONF_ACK:
1049 sctp_asconf_cleanup(stcb);
1051 case SCTP_IFORWARD_CUM_TSN:
1052 case SCTP_FORWARD_CUM_TSN:
1053 stcb->asoc.prsctp_supported = 0;
1056 SCTPDBG(SCTP_DEBUG_INPUT2,
1057 "Peer does not support chunk type %d (0x%x).\n",
1058 chunk_type, chunk_type);
1064 * Skip past the param header and then we will find the param that caused the
1065 * problem. There are a number of param's in a ASCONF OR the prsctp param
1066 * these will turn of specific features.
1067 * XXX: Is this the right thing to do?
1070 sctp_process_unrecog_param(struct sctp_tcb *stcb, uint16_t parameter_type)
1072 switch (parameter_type) {
1074 case SCTP_PRSCTP_SUPPORTED:
1075 stcb->asoc.prsctp_supported = 0;
1077 case SCTP_SUPPORTED_CHUNK_EXT:
1079 /* draft-ietf-tsvwg-addip-sctp */
1080 case SCTP_HAS_NAT_SUPPORT:
1081 stcb->asoc.peer_supports_nat = 0;
1083 case SCTP_ADD_IP_ADDRESS:
1084 case SCTP_DEL_IP_ADDRESS:
1085 case SCTP_SET_PRIM_ADDR:
1086 stcb->asoc.asconf_supported = 0;
1088 case SCTP_SUCCESS_REPORT:
1089 case SCTP_ERROR_CAUSE_IND:
1090 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1091 SCTPDBG(SCTP_DEBUG_INPUT2,
1092 "Turning off ASCONF to this strange peer\n");
1093 stcb->asoc.asconf_supported = 0;
1096 SCTPDBG(SCTP_DEBUG_INPUT2,
1097 "Peer does not support param type %d (0x%x)??\n",
1098 parameter_type, parameter_type);
1104 sctp_handle_error(struct sctp_chunkhdr *ch,
1105 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
1107 struct sctp_error_cause *cause;
1108 struct sctp_association *asoc;
1109 uint32_t remaining_length, adjust;
1110 uint16_t code, cause_code, cause_length;
1112 /* parse through all of the errors and process */
1114 cause = (struct sctp_error_cause *)((caddr_t)ch +
1115 sizeof(struct sctp_chunkhdr));
1116 remaining_length = ntohs(ch->chunk_length);
1117 if (remaining_length > limit) {
1118 remaining_length = limit;
1120 if (remaining_length >= sizeof(struct sctp_chunkhdr)) {
1121 remaining_length -= sizeof(struct sctp_chunkhdr);
1123 remaining_length = 0;
1126 while (remaining_length >= sizeof(struct sctp_error_cause)) {
1127 /* Process an Error Cause */
1128 cause_code = ntohs(cause->code);
1129 cause_length = ntohs(cause->length);
1130 if ((cause_length > remaining_length) || (cause_length == 0)) {
1131 /* Invalid cause length, possibly due to truncation. */
1132 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in cause - bytes left: %u cause length: %u\n",
1133 remaining_length, cause_length);
1137 /* report the first error cause */
1140 switch (cause_code) {
1141 case SCTP_CAUSE_INVALID_STREAM:
1142 case SCTP_CAUSE_MISSING_PARAM:
1143 case SCTP_CAUSE_INVALID_PARAM:
1144 case SCTP_CAUSE_NO_USER_DATA:
1145 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %u back? We have a bug :/ (or do they?)\n",
1148 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1149 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags: %x\n",
1151 if (sctp_handle_nat_colliding_state(stcb)) {
1155 case SCTP_CAUSE_NAT_MISSING_STATE:
1156 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags: %x\n",
1158 if (sctp_handle_nat_missing_state(stcb, net)) {
1162 case SCTP_CAUSE_STALE_COOKIE:
1164 * We only act if we have echoed a cookie and are
1167 if ((cause_length >= sizeof(struct sctp_error_stale_cookie)) &&
1168 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
1169 struct sctp_error_stale_cookie *stale_cookie;
1171 stale_cookie = (struct sctp_error_stale_cookie *)cause;
1172 asoc->cookie_preserve_req = ntohl(stale_cookie->stale_time);
1173 /* Double it to be more robust on RTX */
1174 if (asoc->cookie_preserve_req <= UINT32_MAX / 2) {
1175 asoc->cookie_preserve_req *= 2;
1177 asoc->cookie_preserve_req = UINT32_MAX;
1179 asoc->stale_cookie_count++;
1180 if (asoc->stale_cookie_count >
1181 asoc->max_init_times) {
1182 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1183 /* now free the asoc */
1184 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1185 SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1188 /* blast back to INIT state */
1189 sctp_toss_old_cookies(stcb, &stcb->asoc);
1190 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
1191 sctp_stop_all_cookie_timers(stcb);
1192 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1195 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1197 * Nothing we can do here, we don't do hostname
1198 * addresses so if the peer does not like my IPv6
1199 * (or IPv4 for that matter) it does not matter. If
1200 * they don't support that type of address, they can
1201 * NOT possibly get that packet type... i.e. with no
1202 * IPv6 you can't receive a IPv6 packet. so we can
1203 * safely ignore this one. If we ever added support
1204 * for HOSTNAME Addresses, then we would need to do
1208 case SCTP_CAUSE_UNRECOG_CHUNK:
1209 if (cause_length >= sizeof(struct sctp_error_unrecognized_chunk)) {
1210 struct sctp_error_unrecognized_chunk *unrec_chunk;
1212 unrec_chunk = (struct sctp_error_unrecognized_chunk *)cause;
1213 sctp_process_unrecog_chunk(stcb, unrec_chunk->ch.chunk_type);
1216 case SCTP_CAUSE_UNRECOG_PARAM:
1217 /* XXX: We only consider the first parameter */
1218 if (cause_length >= sizeof(struct sctp_error_cause) + sizeof(struct sctp_paramhdr)) {
1219 struct sctp_paramhdr *unrec_parameter;
1221 unrec_parameter = (struct sctp_paramhdr *)(cause + 1);
1222 sctp_process_unrecog_param(stcb, ntohs(unrec_parameter->param_type));
1225 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1227 * We ignore this since the timer will drive out a
1228 * new cookie anyway and there timer will drive us
1229 * to send a SHUTDOWN_COMPLETE. We can't send one
1230 * here since we don't have their tag.
1233 case SCTP_CAUSE_DELETING_LAST_ADDR:
1234 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1235 case SCTP_CAUSE_DELETING_SRC_ADDR:
1237 * We should NOT get these here, but in a
1240 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a error cause with code %u.\n",
1243 case SCTP_CAUSE_OUT_OF_RESC:
1245 * And what, pray tell do we do with the fact that
1246 * the peer is out of resources? Not really sure we
1247 * could do anything but abort. I suspect this
1248 * should have came WITH an abort instead of in a
1253 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown code 0x%x\n",
1257 adjust = SCTP_SIZE32(cause_length);
1258 if (remaining_length >= adjust) {
1259 remaining_length -= adjust;
1261 remaining_length = 0;
1263 cause = (struct sctp_error_cause *)((caddr_t)cause + adjust);
1265 sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, code, ch, SCTP_SO_NOT_LOCKED);
1270 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1271 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1272 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1273 struct sctp_nets *net, int *abort_no_unlock,
1274 uint8_t mflowtype, uint32_t mflowid,
1277 struct sctp_init_ack *init_ack;
1278 struct mbuf *op_err;
1280 SCTPDBG(SCTP_DEBUG_INPUT2,
1281 "sctp_handle_init_ack: handling INIT-ACK\n");
1284 SCTPDBG(SCTP_DEBUG_INPUT2,
1285 "sctp_handle_init_ack: TCB is null\n");
1288 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1289 /* Invalid length */
1290 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1291 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1292 src, dst, sh, op_err,
1295 *abort_no_unlock = 1;
1298 init_ack = &cp->init;
1299 /* validate parameters */
1300 if (init_ack->initiate_tag == 0) {
1301 /* protocol error... send an abort */
1302 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1303 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1304 src, dst, sh, op_err,
1307 *abort_no_unlock = 1;
1310 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1311 /* protocol error... send an abort */
1312 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1313 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1314 src, dst, sh, op_err,
1317 *abort_no_unlock = 1;
1320 if (init_ack->num_inbound_streams == 0) {
1321 /* protocol error... send an abort */
1322 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1323 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1324 src, dst, sh, op_err,
1327 *abort_no_unlock = 1;
1330 if (init_ack->num_outbound_streams == 0) {
1331 /* protocol error... send an abort */
1332 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1333 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1334 src, dst, sh, op_err,
1337 *abort_no_unlock = 1;
1340 /* process according to association state... */
1341 switch (SCTP_GET_STATE(stcb)) {
1342 case SCTP_STATE_COOKIE_WAIT:
1343 /* this is the expected state for this chunk */
1344 /* process the INIT-ACK parameters */
1345 if (stcb->asoc.primary_destination->dest_state &
1346 SCTP_ADDR_UNCONFIRMED) {
1348 * The primary is where we sent the INIT, we can
1349 * always consider it confirmed when the INIT-ACK is
1350 * returned. Do this before we load addresses
1353 stcb->asoc.primary_destination->dest_state &=
1354 ~SCTP_ADDR_UNCONFIRMED;
1355 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1356 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1358 if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1359 net, abort_no_unlock,
1362 /* error in parsing parameters */
1365 /* update our state */
1366 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1367 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_ECHOED);
1369 /* reset the RTO calc */
1370 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1371 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1372 stcb->asoc.overall_error_count,
1374 SCTP_FROM_SCTP_INPUT,
1377 stcb->asoc.overall_error_count = 0;
1378 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1380 * collapse the init timer back in case of a exponential
1383 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1386 * the send at the end of the inbound data processing will
1387 * cause the cookie to be sent
1390 case SCTP_STATE_SHUTDOWN_SENT:
1391 /* incorrect state... discard */
1393 case SCTP_STATE_COOKIE_ECHOED:
1394 /* incorrect state... discard */
1396 case SCTP_STATE_OPEN:
1397 /* incorrect state... discard */
1399 case SCTP_STATE_EMPTY:
1400 case SCTP_STATE_INUSE:
1402 /* incorrect state... discard */
1406 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1410 static struct sctp_tcb *
1411 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1412 struct sockaddr *src, struct sockaddr *dst,
1413 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1414 struct sctp_inpcb *inp, struct sctp_nets **netp,
1415 struct sockaddr *init_src, int *notification,
1416 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1417 uint8_t mflowtype, uint32_t mflowid,
1418 uint32_t vrf_id, uint16_t port);
1422 * handle a state cookie for an existing association m: input packet mbuf
1423 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1424 * "split" mbuf and the cookie signature does not exist offset: offset into
1425 * mbuf to the cookie-echo chunk
1427 static struct sctp_tcb *
1428 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1429 struct sockaddr *src, struct sockaddr *dst,
1430 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1431 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1432 struct sockaddr *init_src, int *notification,
1433 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1434 uint8_t mflowtype, uint32_t mflowid,
1435 uint32_t vrf_id, uint16_t port)
1437 struct sctp_association *asoc;
1438 struct sctp_init_chunk *init_cp, init_buf;
1439 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1440 struct sctp_nets *net;
1441 struct mbuf *op_err;
1443 int init_offset, initack_offset, i;
1447 #if defined(SCTP_DETAILED_STR_STATS)
1452 /* I know that the TCB is non-NULL from the caller */
1454 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1455 if (asoc->cookie_how[how_indx] == 0)
1458 if (how_indx < sizeof(asoc->cookie_how)) {
1459 asoc->cookie_how[how_indx] = 1;
1461 if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1462 /* SHUTDOWN came in after sending INIT-ACK */
1463 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1464 op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
1465 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1466 mflowtype, mflowid, inp->fibnum,
1468 if (how_indx < sizeof(asoc->cookie_how))
1469 asoc->cookie_how[how_indx] = 2;
1473 * find and validate the INIT chunk in the cookie (peer's info) the
1474 * INIT should start after the cookie-echo header struct (chunk
1475 * header, state cookie header struct)
1477 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1479 init_cp = (struct sctp_init_chunk *)
1480 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1481 (uint8_t *)&init_buf);
1482 if (init_cp == NULL) {
1483 /* could not pull a INIT chunk in cookie */
1486 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1490 * find and validate the INIT-ACK chunk in the cookie (my info) the
1491 * INIT-ACK follows the INIT chunk
1493 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1494 initack_cp = (struct sctp_init_ack_chunk *)
1495 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1496 (uint8_t *)&initack_buf);
1497 if (initack_cp == NULL) {
1498 /* could not pull INIT-ACK chunk in cookie */
1501 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1504 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1505 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1507 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1508 * to get into the OPEN state
1510 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1512 * Opps, this means that we somehow generated two vtag's
1513 * the same. I.e. we did:
1515 * <---INIT(tag=a)------
1516 * ----INIT-ACK(tag=t)-->
1517 * ----INIT(tag=t)------> *1
1518 * <---INIT-ACK(tag=a)---
1519 * <----CE(tag=t)------------- *2
1521 * At point *1 we should be generating a different
1522 * tag t'. Which means we would throw away the CE and send
1523 * ours instead. Basically this is case C (throw away side).
1525 if (how_indx < sizeof(asoc->cookie_how))
1526 asoc->cookie_how[how_indx] = 17;
1530 switch (SCTP_GET_STATE(stcb)) {
1531 case SCTP_STATE_COOKIE_WAIT:
1532 case SCTP_STATE_COOKIE_ECHOED:
1534 * INIT was sent but got a COOKIE_ECHO with the
1535 * correct tags... just accept it...but we must
1536 * process the init so that we can make sure we have
1537 * the right seq no's.
1539 /* First we must process the INIT !! */
1540 retval = sctp_process_init(init_cp, stcb);
1542 if (how_indx < sizeof(asoc->cookie_how))
1543 asoc->cookie_how[how_indx] = 3;
1546 /* we have already processed the INIT so no problem */
1547 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp,
1549 SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1550 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp,
1552 SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1553 /* update current state */
1554 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)
1555 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1557 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1559 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1560 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1561 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1562 stcb->sctp_ep, stcb, NULL);
1564 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1565 sctp_stop_all_cookie_timers(stcb);
1566 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1567 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1568 (!SCTP_IS_LISTENING(inp))) {
1570 * Here is where collision would go if we
1571 * did a connect() and instead got a
1572 * init/init-ack/cookie done before the
1573 * init-ack came back..
1575 stcb->sctp_ep->sctp_flags |=
1576 SCTP_PCB_FLAGS_CONNECTED;
1577 soisconnected(stcb->sctp_socket);
1579 /* notify upper layer */
1580 *notification = SCTP_NOTIFY_ASSOC_UP;
1582 * since we did not send a HB make sure we don't
1585 old.tv_sec = cookie->time_entered.tv_sec;
1586 old.tv_usec = cookie->time_entered.tv_usec;
1587 net->hb_responded = 1;
1588 sctp_calculate_rto(stcb, asoc, net, &old,
1589 SCTP_RTT_FROM_NON_DATA);
1591 if (stcb->asoc.sctp_autoclose_ticks &&
1592 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1593 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1599 * we're in the OPEN state (or beyond), so peer must
1600 * have simply lost the COOKIE-ACK
1604 sctp_stop_all_cookie_timers(stcb);
1606 * We ignore the return code here.. not sure if we should
1607 * somehow abort.. but we do have an existing asoc. This
1608 * really should not fail.
1610 if (sctp_load_addresses_from_init(stcb, m,
1611 init_offset + sizeof(struct sctp_init_chunk),
1612 initack_offset, src, dst, init_src, stcb->asoc.port)) {
1613 if (how_indx < sizeof(asoc->cookie_how))
1614 asoc->cookie_how[how_indx] = 4;
1617 /* respond with a COOKIE-ACK */
1618 sctp_toss_old_cookies(stcb, asoc);
1619 sctp_send_cookie_ack(stcb);
1620 if (how_indx < sizeof(asoc->cookie_how))
1621 asoc->cookie_how[how_indx] = 5;
1625 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1626 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1627 cookie->tie_tag_my_vtag == 0 &&
1628 cookie->tie_tag_peer_vtag == 0) {
1630 * case C in Section 5.2.4 Table 2: XMOO silently discard
1632 if (how_indx < sizeof(asoc->cookie_how))
1633 asoc->cookie_how[how_indx] = 6;
1637 * If nat support, and the below and stcb is established, send back
1638 * a ABORT(colliding state) if we are established.
1640 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) &&
1641 (asoc->peer_supports_nat) &&
1642 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1643 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1644 (asoc->peer_vtag == 0)))) {
1646 * Special case - Peer's support nat. We may have two init's
1647 * that we gave out the same tag on since one was not
1648 * established.. i.e. we get INIT from host-1 behind the nat
1649 * and we respond tag-a, we get a INIT from host-2 behind
1650 * the nat and we get tag-a again. Then we bring up host-1
1651 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1652 * Now we have colliding state. We must send an abort here
1653 * with colliding state indication.
1655 op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
1656 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1657 mflowtype, mflowid, inp->fibnum,
1661 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1662 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1663 (asoc->peer_vtag == 0))) {
1665 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1666 * should be ok, re-accept peer info
1668 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1670 * Extension of case C. If we hit this, then the
1671 * random number generator returned the same vtag
1672 * when we first sent our INIT-ACK and when we later
1673 * sent our INIT. The side with the seq numbers that
1674 * are different will be the one that normnally
1675 * would have hit case C. This in effect "extends"
1676 * our vtags in this collision case to be 64 bits.
1677 * The same collision could occur aka you get both
1678 * vtag and seq number the same twice in a row.. but
1679 * is much less likely. If it did happen then we
1680 * would proceed through and bring up the assoc.. we
1681 * may end up with the wrong stream setup however..
1682 * which would be bad.. but there is no way to
1683 * tell.. until we send on a stream that does not
1686 if (how_indx < sizeof(asoc->cookie_how))
1687 asoc->cookie_how[how_indx] = 7;
1691 if (how_indx < sizeof(asoc->cookie_how))
1692 asoc->cookie_how[how_indx] = 8;
1693 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1694 SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1695 sctp_stop_all_cookie_timers(stcb);
1697 * since we did not send a HB make sure we don't double
1700 net->hb_responded = 1;
1701 if (stcb->asoc.sctp_autoclose_ticks &&
1702 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1703 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1706 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1707 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1709 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1711 * Ok the peer probably discarded our data (if we
1712 * echoed a cookie+data). So anything on the
1713 * sent_queue should be marked for retransmit, we
1714 * may not get something to kick us so it COULD
1715 * still take a timeout to move these.. but it can't
1716 * hurt to mark them.
1718 struct sctp_tmit_chunk *chk;
1720 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1721 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1722 chk->sent = SCTP_DATAGRAM_RESEND;
1723 sctp_flight_size_decrease(chk);
1724 sctp_total_flight_decrease(stcb, chk);
1725 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1731 /* process the INIT info (peer's info) */
1732 retval = sctp_process_init(init_cp, stcb);
1734 if (how_indx < sizeof(asoc->cookie_how))
1735 asoc->cookie_how[how_indx] = 9;
1738 if (sctp_load_addresses_from_init(stcb, m,
1739 init_offset + sizeof(struct sctp_init_chunk),
1740 initack_offset, src, dst, init_src, stcb->asoc.port)) {
1741 if (how_indx < sizeof(asoc->cookie_how))
1742 asoc->cookie_how[how_indx] = 10;
1745 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
1746 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
1747 *notification = SCTP_NOTIFY_ASSOC_UP;
1749 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1750 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1751 (!SCTP_IS_LISTENING(inp))) {
1752 stcb->sctp_ep->sctp_flags |=
1753 SCTP_PCB_FLAGS_CONNECTED;
1754 soisconnected(stcb->sctp_socket);
1756 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)
1757 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1759 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1760 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1761 } else if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
1762 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1764 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1766 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1767 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1768 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1769 stcb->sctp_ep, stcb, NULL);
1771 sctp_stop_all_cookie_timers(stcb);
1772 sctp_toss_old_cookies(stcb, asoc);
1773 sctp_send_cookie_ack(stcb);
1776 * only if we have retrans set do we do this. What
1777 * this call does is get only the COOKIE-ACK out and
1778 * then when we return the normal call to
1779 * sctp_chunk_output will get the retrans out behind
1782 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1784 if (how_indx < sizeof(asoc->cookie_how))
1785 asoc->cookie_how[how_indx] = 11;
1789 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1790 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1791 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1792 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1793 cookie->tie_tag_peer_vtag != 0) {
1794 struct sctpasochead *head;
1796 if (asoc->peer_supports_nat) {
1798 * This is a gross gross hack. Just call the
1799 * cookie_new code since we are allowing a duplicate
1800 * association. I hope this works...
1802 return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
1803 sh, cookie, cookie_len,
1804 inp, netp, init_src, notification,
1805 auth_skipped, auth_offset, auth_len,
1810 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1813 if (how_indx < sizeof(asoc->cookie_how))
1814 asoc->cookie_how[how_indx] = 12;
1815 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
1816 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1817 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1818 SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
1820 /* notify upper layer */
1821 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1822 atomic_add_int(&stcb->asoc.refcnt, 1);
1823 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_OPEN) &&
1824 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1825 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) {
1826 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1828 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
1829 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1830 } else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
1831 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1833 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1834 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1835 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1836 stcb->sctp_ep, stcb, NULL);
1838 } else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
1839 /* move to OPEN state, if not in SHUTDOWN_SENT */
1840 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1842 asoc->pre_open_streams =
1843 ntohs(initack_cp->init.num_outbound_streams);
1844 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1845 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1846 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1848 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1850 asoc->str_reset_seq_in = asoc->init_seq_number;
1852 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1853 if (asoc->mapping_array) {
1854 memset(asoc->mapping_array, 0,
1855 asoc->mapping_array_size);
1857 if (asoc->nr_mapping_array) {
1858 memset(asoc->nr_mapping_array, 0,
1859 asoc->mapping_array_size);
1861 SCTP_TCB_UNLOCK(stcb);
1862 SCTP_INP_INFO_WLOCK();
1863 SCTP_INP_WLOCK(stcb->sctp_ep);
1864 SCTP_TCB_LOCK(stcb);
1865 atomic_add_int(&stcb->asoc.refcnt, -1);
1866 /* send up all the data */
1867 SCTP_TCB_SEND_LOCK(stcb);
1869 sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_LOCKED);
1870 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1871 stcb->asoc.strmout[i].chunks_on_queues = 0;
1872 #if defined(SCTP_DETAILED_STR_STATS)
1873 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1874 asoc->strmout[i].abandoned_sent[j] = 0;
1875 asoc->strmout[i].abandoned_unsent[j] = 0;
1878 asoc->strmout[i].abandoned_sent[0] = 0;
1879 asoc->strmout[i].abandoned_unsent[0] = 0;
1881 stcb->asoc.strmout[i].sid = i;
1882 stcb->asoc.strmout[i].next_mid_ordered = 0;
1883 stcb->asoc.strmout[i].next_mid_unordered = 0;
1884 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1886 /* process the INIT-ACK info (my info) */
1887 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1888 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1890 /* pull from vtag hash */
1891 LIST_REMOVE(stcb, sctp_asocs);
1892 /* re-insert to new vtag position */
1893 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1894 SCTP_BASE_INFO(hashasocmark))];
1896 * put it in the bucket in the vtag hash of assoc's for the
1899 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1901 SCTP_TCB_SEND_UNLOCK(stcb);
1902 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1903 SCTP_INP_INFO_WUNLOCK();
1904 asoc->total_flight = 0;
1905 asoc->total_flight_count = 0;
1906 /* process the INIT info (peer's info) */
1907 retval = sctp_process_init(init_cp, stcb);
1909 if (how_indx < sizeof(asoc->cookie_how))
1910 asoc->cookie_how[how_indx] = 13;
1915 * since we did not send a HB make sure we don't double
1918 net->hb_responded = 1;
1920 if (sctp_load_addresses_from_init(stcb, m,
1921 init_offset + sizeof(struct sctp_init_chunk),
1922 initack_offset, src, dst, init_src, stcb->asoc.port)) {
1923 if (how_indx < sizeof(asoc->cookie_how))
1924 asoc->cookie_how[how_indx] = 14;
1928 /* respond with a COOKIE-ACK */
1929 sctp_stop_all_cookie_timers(stcb);
1930 sctp_toss_old_cookies(stcb, asoc);
1931 sctp_send_cookie_ack(stcb);
1932 if (how_indx < sizeof(asoc->cookie_how))
1933 asoc->cookie_how[how_indx] = 15;
1937 if (how_indx < sizeof(asoc->cookie_how))
1938 asoc->cookie_how[how_indx] = 16;
1939 /* all other cases... */
1945 * handle a state cookie for a new association m: input packet mbuf chain--
1946 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
1947 * and the cookie signature does not exist offset: offset into mbuf to the
1948 * cookie-echo chunk length: length of the cookie chunk to: where the init
1949 * was from returns a new TCB
1951 static struct sctp_tcb *
1952 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1953 struct sockaddr *src, struct sockaddr *dst,
1954 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1955 struct sctp_inpcb *inp, struct sctp_nets **netp,
1956 struct sockaddr *init_src, int *notification,
1957 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1958 uint8_t mflowtype, uint32_t mflowid,
1959 uint32_t vrf_id, uint16_t port)
1961 struct sctp_tcb *stcb;
1962 struct sctp_init_chunk *init_cp, init_buf;
1963 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1964 union sctp_sockstore store;
1965 struct sctp_association *asoc;
1966 int init_offset, initack_offset, initack_limit;
1969 uint8_t auth_chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
1972 * find and validate the INIT chunk in the cookie (peer's info) the
1973 * INIT should start after the cookie-echo header struct (chunk
1974 * header, state cookie header struct)
1976 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
1977 init_cp = (struct sctp_init_chunk *)
1978 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1979 (uint8_t *)&init_buf);
1980 if (init_cp == NULL) {
1981 /* could not pull a INIT chunk in cookie */
1982 SCTPDBG(SCTP_DEBUG_INPUT1,
1983 "process_cookie_new: could not pull INIT chunk hdr\n");
1986 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1987 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
1990 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1992 * find and validate the INIT-ACK chunk in the cookie (my info) the
1993 * INIT-ACK follows the INIT chunk
1995 initack_cp = (struct sctp_init_ack_chunk *)
1996 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1997 (uint8_t *)&initack_buf);
1998 if (initack_cp == NULL) {
1999 /* could not pull INIT-ACK chunk in cookie */
2000 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2003 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2007 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2008 * "initack_limit" value. This is because the chk_length field
2009 * includes the length of the cookie, but the cookie is omitted when
2010 * the INIT and INIT_ACK are tacked onto the cookie...
2012 initack_limit = offset + cookie_len;
2015 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2020 * Here we do a trick, we set in NULL for the proc/thread argument.
2021 * We do this since in effect we only use the p argument when the
2022 * socket is unbound and we must do an implicit bind. Since we are
2023 * getting a cookie, we cannot be unbound.
2025 stcb = sctp_aloc_assoc(inp, init_src, &error,
2026 ntohl(initack_cp->init.initiate_tag), vrf_id,
2027 ntohs(initack_cp->init.num_outbound_streams),
2029 (struct thread *)NULL,
2030 SCTP_DONT_INITIALIZE_AUTH_PARAMS);
2032 struct mbuf *op_err;
2034 /* memory problem? */
2035 SCTPDBG(SCTP_DEBUG_INPUT1,
2036 "process_cookie_new: no room for another TCB!\n");
2037 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2038 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2039 src, dst, sh, op_err,
2044 /* get the correct sctp_nets */
2046 *netp = sctp_findnet(stcb, init_src);
2049 /* get scope variables out of cookie */
2050 asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2051 asoc->scope.site_scope = cookie->site_scope;
2052 asoc->scope.local_scope = cookie->local_scope;
2053 asoc->scope.loopback_scope = cookie->loopback_scope;
2055 if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2056 (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2057 struct mbuf *op_err;
2060 * Houston we have a problem. The EP changed while the
2061 * cookie was in flight. Only recourse is to abort the
2064 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2065 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2066 src, dst, sh, op_err,
2069 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2070 SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2073 /* process the INIT-ACK info (my info) */
2074 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2075 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2076 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2077 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2078 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2079 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2080 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2081 asoc->str_reset_seq_in = asoc->init_seq_number;
2083 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2085 /* process the INIT info (peer's info) */
2087 retval = sctp_process_init(init_cp, stcb);
2091 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2092 SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2095 /* load all addresses */
2096 if (sctp_load_addresses_from_init(stcb, m,
2097 init_offset + sizeof(struct sctp_init_chunk), initack_offset,
2098 src, dst, init_src, port)) {
2099 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2100 SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2104 * verify any preceding AUTH chunk that was skipped
2106 /* pull the local authentication parameters from the cookie/init-ack */
2107 sctp_auth_get_cookie_params(stcb, m,
2108 initack_offset + sizeof(struct sctp_init_ack_chunk),
2109 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2111 struct sctp_auth_chunk *auth;
2113 if (auth_len <= SCTP_CHUNK_BUFFER_SIZE) {
2114 auth = (struct sctp_auth_chunk *)sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2118 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2119 /* auth HMAC failed, dump the assoc and packet */
2120 SCTPDBG(SCTP_DEBUG_AUTH1,
2121 "COOKIE-ECHO: AUTH failed\n");
2122 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2123 SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2126 /* remaining chunks checked... good to go */
2127 stcb->asoc.authenticated = 1;
2132 * if we're doing ASCONFs, check to see if we have any new local
2133 * addresses that need to get added to the peer (eg. addresses
2134 * changed while cookie echo in flight). This needs to be done
2135 * after we go to the OPEN state to do the correct asconf
2136 * processing. else, make sure we have the correct addresses in our
2140 /* warning, we re-use sin, sin6, sa_store here! */
2141 /* pull in local_address (our "from" address) */
2142 switch (cookie->laddr_type) {
2144 case SCTP_IPV4_ADDRESS:
2145 /* source addr is IPv4 */
2146 memset(&store.sin, 0, sizeof(struct sockaddr_in));
2147 store.sin.sin_family = AF_INET;
2148 store.sin.sin_len = sizeof(struct sockaddr_in);
2149 store.sin.sin_addr.s_addr = cookie->laddress[0];
2153 case SCTP_IPV6_ADDRESS:
2154 /* source addr is IPv6 */
2155 memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
2156 store.sin6.sin6_family = AF_INET6;
2157 store.sin6.sin6_len = sizeof(struct sockaddr_in6);
2158 store.sin6.sin6_scope_id = cookie->scope_id;
2159 memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2163 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2164 SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2168 /* update current state */
2169 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2170 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
2171 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2172 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2173 stcb->sctp_ep, stcb, NULL);
2175 sctp_stop_all_cookie_timers(stcb);
2176 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2177 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2179 /* set up to notify upper layer */
2180 *notification = SCTP_NOTIFY_ASSOC_UP;
2181 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2182 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2183 (!SCTP_IS_LISTENING(inp))) {
2185 * This is an endpoint that called connect() how it got a
2186 * cookie that is NEW is a bit of a mystery. It must be that
2187 * the INIT was sent, but before it got there.. a complete
2188 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2189 * should have went to the other code.. not here.. oh well..
2190 * a bit of protection is worth having..
2192 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2193 soisconnected(stcb->sctp_socket);
2194 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2195 (SCTP_IS_LISTENING(inp))) {
2197 * We don't want to do anything with this one. Since it is
2198 * the listening guy. The timer will get started for
2199 * accepted connections in the caller.
2203 /* since we did not send a HB make sure we don't double things */
2204 if ((netp) && (*netp))
2205 (*netp)->hb_responded = 1;
2207 if (stcb->asoc.sctp_autoclose_ticks &&
2208 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2209 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2211 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2212 if ((netp != NULL) && (*netp != NULL)) {
2215 /* calculate the RTT and set the encaps port */
2216 old.tv_sec = cookie->time_entered.tv_sec;
2217 old.tv_usec = cookie->time_entered.tv_usec;
2218 sctp_calculate_rto(stcb, asoc, *netp, &old, SCTP_RTT_FROM_NON_DATA);
2220 /* respond with a COOKIE-ACK */
2221 sctp_send_cookie_ack(stcb);
2224 * check the address lists for any ASCONFs that need to be sent
2225 * AFTER the cookie-ack is sent
2227 sctp_check_address_list(stcb, m,
2228 initack_offset + sizeof(struct sctp_init_ack_chunk),
2229 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2230 &store.sa, cookie->local_scope, cookie->site_scope,
2231 cookie->ipv4_scope, cookie->loopback_scope);
2238 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2239 * we NEED to make sure we are not already using the vtag. If so we
2240 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2241 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2242 SCTP_BASE_INFO(hashasocmark))];
2243 LIST_FOREACH(stcb, head, sctp_asocs) {
2244 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2245 -- SEND ABORT - TRY AGAIN --
2251 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2252 * existing (non-NULL) TCB
2254 static struct mbuf *
2255 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2256 struct sockaddr *src, struct sockaddr *dst,
2257 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2258 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2259 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2260 struct sctp_tcb **locked_tcb,
2261 uint8_t mflowtype, uint32_t mflowid,
2262 uint32_t vrf_id, uint16_t port)
2264 struct sctp_state_cookie *cookie;
2265 struct sctp_tcb *l_stcb = *stcb;
2266 struct sctp_inpcb *l_inp;
2267 struct sockaddr *to;
2268 struct sctp_pcb *ep;
2270 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2272 uint8_t cookie_ok = 0;
2273 unsigned int sig_offset, cookie_offset;
2274 unsigned int cookie_len;
2276 struct timeval time_expires;
2277 int notification = 0;
2278 struct sctp_nets *netl;
2279 int had_a_existing_tcb = 0;
2280 int send_int_conf = 0;
2282 struct sockaddr_in sin;
2285 struct sockaddr_in6 sin6;
2288 SCTPDBG(SCTP_DEBUG_INPUT2,
2289 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2291 if (inp_p == NULL) {
2294 cookie = &cp->cookie;
2295 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2296 cookie_len = ntohs(cp->ch.chunk_length);
2298 if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2299 sizeof(struct sctp_init_chunk) +
2300 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2301 /* cookie too small */
2304 if ((cookie->peerport != sh->src_port) ||
2305 (cookie->myport != sh->dest_port) ||
2306 (cookie->my_vtag != sh->v_tag)) {
2308 * invalid ports or bad tag. Note that we always leave the
2309 * v_tag in the header in network order and when we stored
2310 * it in the my_vtag slot we also left it in network order.
2311 * This maintains the match even though it may be in the
2312 * opposite byte order of the machine :->
2317 * split off the signature into its own mbuf (since it should not be
2318 * calculated in the sctp_hmac_m() call).
2320 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2321 m_sig = m_split(m, sig_offset, M_NOWAIT);
2322 if (m_sig == NULL) {
2323 /* out of memory or ?? */
2326 #ifdef SCTP_MBUF_LOGGING
2327 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2328 sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
2333 * compute the signature/digest for the cookie
2335 ep = &(*inp_p)->sctp_ep;
2338 SCTP_TCB_UNLOCK(l_stcb);
2340 SCTP_INP_RLOCK(l_inp);
2342 SCTP_TCB_LOCK(l_stcb);
2344 /* which cookie is it? */
2345 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2346 (ep->current_secret_number != ep->last_secret_number)) {
2347 /* it's the old cookie */
2348 (void)sctp_hmac_m(SCTP_HMAC,
2349 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2350 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2352 /* it's the current cookie */
2353 (void)sctp_hmac_m(SCTP_HMAC,
2354 (uint8_t *)ep->secret_key[(int)ep->current_secret_number],
2355 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2357 /* get the signature */
2358 SCTP_INP_RUNLOCK(l_inp);
2359 sig = (uint8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *)&tmp_sig);
2361 /* couldn't find signature */
2362 sctp_m_freem(m_sig);
2365 /* compare the received digest with the computed digest */
2366 if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2367 /* try the old cookie? */
2368 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2369 (ep->current_secret_number != ep->last_secret_number)) {
2370 /* compute digest with old */
2371 (void)sctp_hmac_m(SCTP_HMAC,
2372 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2373 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2375 if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2383 * Now before we continue we must reconstruct our mbuf so that
2384 * normal processing of any other chunks will work.
2390 while (SCTP_BUF_NEXT(m_at) != NULL) {
2391 m_at = SCTP_BUF_NEXT(m_at);
2393 SCTP_BUF_NEXT(m_at) = m_sig;
2396 if (cookie_ok == 0) {
2397 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2398 SCTPDBG(SCTP_DEBUG_INPUT2,
2399 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2400 (uint32_t)offset, cookie_offset, sig_offset);
2405 * check the cookie timestamps to be sure it's not stale
2407 (void)SCTP_GETTIME_TIMEVAL(&now);
2408 /* Expire time is in Ticks, so we convert to seconds */
2409 time_expires.tv_sec = cookie->time_entered.tv_sec + sctp_ticks_to_secs(cookie->cookie_life);
2410 time_expires.tv_usec = cookie->time_entered.tv_usec;
2411 if (timevalcmp(&now, &time_expires, >)) {
2412 /* cookie is stale! */
2413 struct mbuf *op_err;
2414 struct sctp_error_stale_cookie *cause;
2415 struct timeval diff;
2418 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2419 0, M_NOWAIT, 1, MT_DATA);
2420 if (op_err == NULL) {
2425 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
2426 cause = mtod(op_err, struct sctp_error_stale_cookie *);
2427 cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
2428 cause->cause.length = htons((sizeof(struct sctp_paramhdr) +
2429 (sizeof(uint32_t))));
2431 timevalsub(&diff, &time_expires);
2432 if ((uint32_t)diff.tv_sec > UINT32_MAX / 1000000) {
2433 staleness = UINT32_MAX;
2435 staleness = diff.tv_sec * 1000000;
2437 if (UINT32_MAX - staleness >= (uint32_t)diff.tv_usec) {
2438 staleness += diff.tv_usec;
2440 staleness = UINT32_MAX;
2442 cause->stale_time = htonl(staleness);
2443 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2444 mflowtype, mflowid, l_inp->fibnum,
2449 * Now we must see with the lookup address if we have an existing
2450 * asoc. This will only happen if we were in the COOKIE-WAIT state
2451 * and a INIT collided with us and somewhere the peer sent the
2452 * cookie on another address besides the single address our assoc
2453 * had for him. In this case we will have one of the tie-tags set at
2454 * least AND the address field in the cookie can be used to look it
2458 switch (cookie->addr_type) {
2460 case SCTP_IPV6_ADDRESS:
2461 memset(&sin6, 0, sizeof(sin6));
2462 sin6.sin6_family = AF_INET6;
2463 sin6.sin6_len = sizeof(sin6);
2464 sin6.sin6_port = sh->src_port;
2465 sin6.sin6_scope_id = cookie->scope_id;
2466 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2467 sizeof(sin6.sin6_addr.s6_addr));
2468 to = (struct sockaddr *)&sin6;
2472 case SCTP_IPV4_ADDRESS:
2473 memset(&sin, 0, sizeof(sin));
2474 sin.sin_family = AF_INET;
2475 sin.sin_len = sizeof(sin);
2476 sin.sin_port = sh->src_port;
2477 sin.sin_addr.s_addr = cookie->address[0];
2478 to = (struct sockaddr *)&sin;
2482 /* This should not happen */
2485 if (*stcb == NULL) {
2486 /* Yep, lets check */
2487 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2488 if (*stcb == NULL) {
2490 * We should have only got back the same inp. If we
2491 * got back a different ep we have a problem. The
2492 * original findep got back l_inp and now
2494 if (l_inp != *inp_p) {
2495 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2498 if (*locked_tcb == NULL) {
2500 * In this case we found the assoc only
2501 * after we locked the create lock. This
2502 * means we are in a colliding case and we
2503 * must make sure that we unlock the tcb if
2504 * its one of the cases where we throw away
2505 * the incoming packets.
2507 *locked_tcb = *stcb;
2510 * We must also increment the inp ref count
2511 * since the ref_count flags was set when we
2512 * did not find the TCB, now we found it
2513 * which reduces the refcount.. we must
2514 * raise it back out to balance it all :-)
2516 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2517 if ((*stcb)->sctp_ep != l_inp) {
2518 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2519 (void *)(*stcb)->sctp_ep, (void *)l_inp);
2525 cookie_len -= SCTP_SIGNATURE_SIZE;
2526 if (*stcb == NULL) {
2527 /* this is the "normal" case... get a new TCB */
2528 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2529 cookie, cookie_len, *inp_p,
2530 netp, to, ¬ification,
2531 auth_skipped, auth_offset, auth_len,
2535 /* this is abnormal... cookie-echo on existing TCB */
2536 had_a_existing_tcb = 1;
2537 *stcb = sctp_process_cookie_existing(m, iphlen, offset,
2539 cookie, cookie_len, *inp_p, *stcb, netp, to,
2540 ¬ification, auth_skipped, auth_offset, auth_len,
2545 if (*stcb == NULL) {
2546 /* still no TCB... must be bad cookie-echo */
2549 if (*netp != NULL) {
2550 (*netp)->flowtype = mflowtype;
2551 (*netp)->flowid = mflowid;
2554 * Ok, we built an association so confirm the address we sent the
2557 netl = sctp_findnet(*stcb, to);
2559 * This code should in theory NOT run but
2562 /* TSNH! Huh, why do I need to add this address here? */
2563 if (sctp_add_remote_addr(*stcb, to, NULL, port,
2564 SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2567 netl = sctp_findnet(*stcb, to);
2570 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2571 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2572 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2577 sctp_start_net_timers(*stcb);
2578 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2579 if (!had_a_existing_tcb ||
2580 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2582 * If we have a NEW cookie or the connect never
2583 * reached the connected state during collision we
2584 * must do the TCP accept thing.
2586 struct socket *so, *oso;
2587 struct sctp_inpcb *inp;
2589 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2591 * For a restart we will keep the same
2592 * socket, no need to do anything. I THINK!!
2594 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2595 if (send_int_conf) {
2596 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2597 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2601 oso = (*inp_p)->sctp_socket;
2602 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2603 SCTP_TCB_UNLOCK((*stcb));
2604 CURVNET_SET(oso->so_vnet);
2605 so = sonewconn(oso, 0
2608 SCTP_TCB_LOCK((*stcb));
2609 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2612 struct mbuf *op_err;
2614 /* Too many sockets */
2615 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2616 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2617 sctp_abort_association(*inp_p, NULL, m, iphlen,
2618 src, dst, sh, op_err,
2621 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2622 SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2625 inp = (struct sctp_inpcb *)so->so_pcb;
2626 SCTP_INP_INCR_REF(inp);
2628 * We add the unbound flag here so that if we get an
2629 * soabort() before we get the move_pcb done, we
2630 * will properly cleanup.
2632 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2633 SCTP_PCB_FLAGS_CONNECTED |
2634 SCTP_PCB_FLAGS_IN_TCPPOOL |
2635 SCTP_PCB_FLAGS_UNBOUND |
2636 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2637 SCTP_PCB_FLAGS_DONT_WAKE);
2638 inp->sctp_features = (*inp_p)->sctp_features;
2639 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2640 inp->sctp_socket = so;
2641 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2642 inp->max_cwnd = (*inp_p)->max_cwnd;
2643 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2644 inp->ecn_supported = (*inp_p)->ecn_supported;
2645 inp->prsctp_supported = (*inp_p)->prsctp_supported;
2646 inp->auth_supported = (*inp_p)->auth_supported;
2647 inp->asconf_supported = (*inp_p)->asconf_supported;
2648 inp->reconfig_supported = (*inp_p)->reconfig_supported;
2649 inp->nrsack_supported = (*inp_p)->nrsack_supported;
2650 inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2651 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2652 inp->sctp_context = (*inp_p)->sctp_context;
2653 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2654 inp->fibnum = (*inp_p)->fibnum;
2655 inp->inp_starting_point_for_iterator = NULL;
2657 * copy in the authentication parameters from the
2660 if (inp->sctp_ep.local_hmacs)
2661 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2662 inp->sctp_ep.local_hmacs =
2663 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2664 if (inp->sctp_ep.local_auth_chunks)
2665 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2666 inp->sctp_ep.local_auth_chunks =
2667 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2670 * Now we must move it from one hash table to
2671 * another and get the tcb in the right place.
2675 * This is where the one-2-one socket is put into
2676 * the accept state waiting for the accept!
2679 SCTP_ADD_SUBSTATE(*stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
2681 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2683 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2684 SCTP_TCB_UNLOCK((*stcb));
2686 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2688 SCTP_TCB_LOCK((*stcb));
2689 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2693 * now we must check to see if we were aborted while
2694 * the move was going on and the lock/unlock
2697 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2699 * yep it was, we leave the assoc attached
2700 * to the socket since the sctp_inpcb_free()
2701 * call will send an abort for us.
2703 SCTP_INP_DECR_REF(inp);
2706 SCTP_INP_DECR_REF(inp);
2707 /* Switch over to the new guy */
2709 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2710 if (send_int_conf) {
2711 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2712 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2716 * Pull it from the incomplete queue and wake the
2724 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2726 if (send_int_conf) {
2727 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2728 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2734 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2735 struct sctp_tcb *stcb, struct sctp_nets *net)
2737 /* cp must not be used, others call this without a c-ack :-) */
2738 struct sctp_association *asoc;
2739 struct sctp_tmit_chunk *chk;
2741 SCTPDBG(SCTP_DEBUG_INPUT2,
2742 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2743 if ((stcb == NULL) || (net == NULL)) {
2748 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2749 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2750 asoc->overall_error_count,
2752 SCTP_FROM_SCTP_INPUT,
2755 asoc->overall_error_count = 0;
2756 sctp_stop_all_cookie_timers(stcb);
2757 /* process according to association state */
2758 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) {
2759 /* state change only needed when I am in right state */
2760 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2761 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
2762 sctp_start_net_timers(stcb);
2763 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2764 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2765 stcb->sctp_ep, stcb, NULL);
2769 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2770 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2771 if (asoc->overall_error_count == 0) {
2772 sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
2773 SCTP_RTT_FROM_NON_DATA);
2775 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2776 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2777 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2778 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2779 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2780 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2781 soisconnected(stcb->sctp_socket);
2785 * since we did not send a HB make sure we don't double
2788 net->hb_responded = 1;
2790 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2792 * We don't need to do the asconf thing, nor hb or
2793 * autoclose if the socket is closed.
2798 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2802 if (stcb->asoc.sctp_autoclose_ticks &&
2803 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2804 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2805 stcb->sctp_ep, stcb, NULL);
2808 * send ASCONF if parameters are pending and ASCONFs are
2809 * allowed (eg. addresses changed when init/cookie echo were
2812 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2813 (stcb->asoc.asconf_supported == 1) &&
2814 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2815 #ifdef SCTP_TIMER_BASED_ASCONF
2816 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2817 stcb->sctp_ep, stcb,
2818 stcb->asoc.primary_destination);
2820 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2821 SCTP_ADDR_NOT_LOCKED);
2826 /* Toss the cookie if I can */
2827 sctp_toss_old_cookies(stcb, asoc);
2828 /* Restart the timer if we have pending data */
2829 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2830 if (chk->whoTo != NULL) {
2835 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
2840 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2841 struct sctp_tcb *stcb)
2843 struct sctp_nets *net;
2844 struct sctp_tmit_chunk *lchk;
2845 struct sctp_ecne_chunk bkup;
2846 uint8_t override_bit;
2847 uint32_t tsn, window_data_tsn;
2849 unsigned int pkt_cnt;
2851 len = ntohs(cp->ch.chunk_length);
2852 if ((len != sizeof(struct sctp_ecne_chunk)) &&
2853 (len != sizeof(struct old_sctp_ecne_chunk))) {
2856 if (len == sizeof(struct old_sctp_ecne_chunk)) {
2857 /* Its the old format */
2858 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
2859 bkup.num_pkts_since_cwr = htonl(1);
2862 SCTP_STAT_INCR(sctps_recvecne);
2863 tsn = ntohl(cp->tsn);
2864 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
2865 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
2867 window_data_tsn = stcb->asoc.sending_seq - 1;
2869 window_data_tsn = lchk->rec.data.tsn;
2872 /* Find where it was sent to if possible. */
2874 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
2875 if (lchk->rec.data.tsn == tsn) {
2877 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
2880 if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) {
2886 * What to do. A previous send of a CWR was possibly lost.
2887 * See how old it is, we may have it marked on the actual
2890 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2891 if (tsn == net->last_cwr_tsn) {
2892 /* Found him, send it off */
2898 * If we reach here, we need to send a special CWR
2899 * that says hey, we did this a long time ago and
2900 * you lost the response.
2902 net = TAILQ_FIRST(&stcb->asoc.nets);
2907 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
2914 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
2915 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
2917 * JRS - Use the congestion control given in the pluggable
2920 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
2922 * We reduce once every RTT. So we will only lower cwnd at
2923 * the next sending seq i.e. the window_data_tsn
2925 net->cwr_window_tsn = window_data_tsn;
2926 net->ecn_ce_pkt_cnt += pkt_cnt;
2927 net->lost_cnt = pkt_cnt;
2928 net->last_cwr_tsn = tsn;
2930 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
2931 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
2932 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
2934 * Another loss in the same window update how many
2935 * marks/packets lost we have had.
2939 if (pkt_cnt > net->lost_cnt) {
2940 /* Should be the case */
2941 cnt = (pkt_cnt - net->lost_cnt);
2942 net->ecn_ce_pkt_cnt += cnt;
2944 net->lost_cnt = pkt_cnt;
2945 net->last_cwr_tsn = tsn;
2947 * Most CC functions will ignore this call, since we
2948 * are in-window yet of the initial CE the peer saw.
2950 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
2954 * We always send a CWR this way if our previous one was lost our
2955 * peer will get an update, or if it is not time again to reduce we
2956 * still get the cwr to the peer. Note we set the override when we
2957 * could not find the TSN on the chunk or the destination network.
2959 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
2963 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
2966 * Here we get a CWR from the peer. We must look in the outqueue and
2967 * make sure that we have a covered ECNE in the control chunk part.
2970 struct sctp_tmit_chunk *chk, *nchk;
2971 struct sctp_ecne_chunk *ecne;
2975 cwr_tsn = ntohl(cp->tsn);
2976 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
2977 TAILQ_FOREACH_SAFE(chk, &stcb->asoc.control_send_queue, sctp_next, nchk) {
2978 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
2981 if ((override == 0) && (chk->whoTo != net)) {
2982 /* Must be from the right src unless override is set */
2985 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
2986 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
2987 /* this covers this ECNE, we can remove it */
2988 stcb->asoc.ecn_echo_cnt_onq--;
2989 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
2991 stcb->asoc.ctrl_queue_cnt--;
2992 sctp_m_freem(chk->data);
2994 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
2995 if (override == 0) {
3003 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3004 struct sctp_tcb *stcb, struct sctp_nets *net)
3007 SCTPDBG(SCTP_DEBUG_INPUT2,
3008 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3012 /* process according to association state */
3013 if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3014 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3015 SCTPDBG(SCTP_DEBUG_INPUT2,
3016 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3017 SCTP_TCB_UNLOCK(stcb);
3020 /* notify upper layer protocol */
3021 if (stcb->sctp_socket) {
3022 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3025 if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
3026 !TAILQ_EMPTY(&stcb->asoc.sent_queue) ||
3027 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
3028 panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3031 /* stop the timer */
3032 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3033 SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3034 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3036 SCTPDBG(SCTP_DEBUG_INPUT2,
3037 "sctp_handle_shutdown_complete: calls free-asoc\n");
3038 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3039 SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3044 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3045 struct sctp_nets *net, uint8_t flg)
3047 switch (desc->chunk_type) {
3049 /* find the tsn to resend (possibly */
3052 struct sctp_tmit_chunk *tp1;
3054 tsn = ntohl(desc->tsn_ifany);
3055 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3056 if (tp1->rec.data.tsn == tsn) {
3060 if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) {
3068 * Do it the other way , aka without paying
3069 * attention to queue seq order.
3071 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3072 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3073 if (tp1->rec.data.tsn == tsn) {
3080 SCTP_STAT_INCR(sctps_pdrptsnnf);
3082 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3085 if (((flg & SCTP_BADCRC) == 0) &&
3086 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3089 if ((stcb->asoc.peers_rwnd == 0) &&
3090 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3091 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3094 if (stcb->asoc.peers_rwnd == 0 &&
3095 (flg & SCTP_FROM_MIDDLE_BOX)) {
3096 SCTP_STAT_INCR(sctps_pdrpdizrw);
3099 ddp = (uint8_t *)(mtod(tp1->data, caddr_t)+
3100 sizeof(struct sctp_data_chunk));
3104 for (iii = 0; iii < sizeof(desc->data_bytes);
3106 if (ddp[iii] != desc->data_bytes[iii]) {
3107 SCTP_STAT_INCR(sctps_pdrpbadd);
3115 * this guy had a RTO calculation
3116 * pending on it, cancel it
3118 if (tp1->whoTo->rto_needed == 0) {
3119 tp1->whoTo->rto_needed = 1;
3123 SCTP_STAT_INCR(sctps_pdrpmark);
3124 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3125 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3127 * mark it as if we were doing a FR, since
3128 * we will be getting gap ack reports behind
3129 * the info from the router.
3131 tp1->rec.data.doing_fast_retransmit = 1;
3133 * mark the tsn with what sequences can
3136 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3137 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3139 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
3142 /* restart the timer */
3143 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3145 SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3146 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3149 /* fix counts and things */
3150 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3151 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3152 tp1->whoTo->flight_size,
3154 (uint32_t)(uintptr_t)stcb,
3157 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3158 sctp_flight_size_decrease(tp1);
3159 sctp_total_flight_decrease(stcb, tp1);
3161 tp1->sent = SCTP_DATAGRAM_RESEND;
3167 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3168 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3171 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3173 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3176 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3177 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3178 audit, stcb->asoc.sent_queue_retran_cnt);
3179 #ifndef SCTP_AUDITING_ENABLED
3180 stcb->asoc.sent_queue_retran_cnt = audit;
3188 struct sctp_tmit_chunk *asconf;
3190 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3192 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3197 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3198 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3199 asconf->sent = SCTP_DATAGRAM_RESEND;
3200 asconf->snd_count--;
3204 case SCTP_INITIATION:
3205 /* resend the INIT */
3206 stcb->asoc.dropped_special_cnt++;
3207 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3209 * If we can get it in, in a few attempts we do
3210 * this, otherwise we let the timer fire.
3212 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3214 SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3215 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3218 case SCTP_SELECTIVE_ACK:
3219 case SCTP_NR_SELECTIVE_ACK:
3220 /* resend the sack */
3221 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3223 case SCTP_HEARTBEAT_REQUEST:
3224 /* resend a demand HB */
3225 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3227 * Only retransmit if we KNOW we wont destroy the
3230 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3234 sctp_send_shutdown(stcb, net);
3236 case SCTP_SHUTDOWN_ACK:
3237 sctp_send_shutdown_ack(stcb, net);
3239 case SCTP_COOKIE_ECHO:
3241 struct sctp_tmit_chunk *cookie;
3244 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3246 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3251 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3252 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3253 cookie->sent = SCTP_DATAGRAM_RESEND;
3254 sctp_stop_all_cookie_timers(stcb);
3258 case SCTP_COOKIE_ACK:
3259 sctp_send_cookie_ack(stcb);
3261 case SCTP_ASCONF_ACK:
3262 /* resend last asconf ack */
3263 sctp_send_asconf_ack(stcb);
3265 case SCTP_IFORWARD_CUM_TSN:
3266 case SCTP_FORWARD_CUM_TSN:
3267 send_forward_tsn(stcb, &stcb->asoc);
3269 /* can't do anything with these */
3270 case SCTP_PACKET_DROPPED:
3271 case SCTP_INITIATION_ACK: /* this should not happen */
3272 case SCTP_HEARTBEAT_ACK:
3273 case SCTP_ABORT_ASSOCIATION:
3274 case SCTP_OPERATION_ERROR:
3275 case SCTP_SHUTDOWN_COMPLETE:
3285 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3291 * We set things to 0xffffffff since this is the last delivered
3292 * sequence and we will be sending in 0 after the reset.
3295 if (number_entries) {
3296 for (i = 0; i < number_entries; i++) {
3297 temp = ntohs(list[i]);
3298 if (temp >= stcb->asoc.streamincnt) {
3301 stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff;
3305 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3306 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
3309 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3313 sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3318 if (number_entries > 0) {
3319 for (i = 0; i < number_entries; i++) {
3320 temp = ntohs(list[i]);
3321 if (temp >= stcb->asoc.streamoutcnt) {
3322 /* no such stream */
3325 stcb->asoc.strmout[temp].next_mid_ordered = 0;
3326 stcb->asoc.strmout[temp].next_mid_unordered = 0;
3329 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3330 stcb->asoc.strmout[i].next_mid_ordered = 0;
3331 stcb->asoc.strmout[i].next_mid_unordered = 0;
3334 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3338 sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3343 if (number_entries > 0) {
3344 for (i = 0; i < number_entries; i++) {
3345 temp = ntohs(list[i]);
3346 if (temp >= stcb->asoc.streamoutcnt) {
3347 /* no such stream */
3350 stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
3353 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3354 stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
3360 struct sctp_stream_reset_request *
3361 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3363 struct sctp_association *asoc;
3364 struct sctp_chunkhdr *ch;
3365 struct sctp_stream_reset_request *r;
3366 struct sctp_tmit_chunk *chk;
3370 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3371 asoc->stream_reset_outstanding = 0;
3374 if (stcb->asoc.str_reset == NULL) {
3375 asoc->stream_reset_outstanding = 0;
3378 chk = stcb->asoc.str_reset;
3379 if (chk->data == NULL) {
3383 /* he wants a copy of the chk pointer */
3386 clen = chk->send_size;
3387 ch = mtod(chk->data, struct sctp_chunkhdr *);
3388 r = (struct sctp_stream_reset_request *)(ch + 1);
3389 if (ntohl(r->request_seq) == seq) {
3393 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3394 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3395 /* move to the next one, there can only be a max of two */
3396 r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3397 if (ntohl(r->request_seq) == seq) {
3401 /* that seq is not here */
3406 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3408 struct sctp_association *asoc;
3409 struct sctp_tmit_chunk *chk;
3412 chk = asoc->str_reset;
3416 asoc->str_reset = NULL;
3417 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
3418 NULL, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3419 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3420 asoc->ctrl_queue_cnt--;
3422 sctp_m_freem(chk->data);
3425 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3430 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3431 uint32_t seq, uint32_t action,
3432 struct sctp_stream_reset_response *respin)
3436 struct sctp_association *asoc = &stcb->asoc;
3437 struct sctp_tmit_chunk *chk;
3438 struct sctp_stream_reset_request *req_param;
3439 struct sctp_stream_reset_out_request *req_out_param;
3440 struct sctp_stream_reset_in_request *req_in_param;
3441 uint32_t number_entries;
3443 if (asoc->stream_reset_outstanding == 0) {
3447 if (seq == stcb->asoc.str_reset_seq_out) {
3448 req_param = sctp_find_stream_reset(stcb, seq, &chk);
3449 if (req_param != NULL) {
3450 stcb->asoc.str_reset_seq_out++;
3451 type = ntohs(req_param->ph.param_type);
3452 lparam_len = ntohs(req_param->ph.param_length);
3453 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3456 req_out_param = (struct sctp_stream_reset_out_request *)req_param;
3457 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3458 asoc->stream_reset_out_is_outstanding = 0;
3459 if (asoc->stream_reset_outstanding)
3460 asoc->stream_reset_outstanding--;
3461 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3463 sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3464 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3465 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3466 } else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
3468 * Set it up so we don't stop
3471 asoc->stream_reset_outstanding++;
3472 stcb->asoc.str_reset_seq_out--;
3473 asoc->stream_reset_out_is_outstanding = 1;
3476 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3478 if (no_clear == 0) {
3479 sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
3481 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3482 req_in_param = (struct sctp_stream_reset_in_request *)req_param;
3483 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3484 if (asoc->stream_reset_outstanding)
3485 asoc->stream_reset_outstanding--;
3486 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3487 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3488 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3489 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3490 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3491 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3493 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3494 /* Ok we now may have more streams */
3497 num_stream = stcb->asoc.strm_pending_add_size;
3498 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3500 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3502 stcb->asoc.strm_pending_add_size = 0;
3503 if (asoc->stream_reset_outstanding)
3504 asoc->stream_reset_outstanding--;
3505 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3506 /* Put the new streams into effect */
3509 for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
3510 asoc->strmout[i].state = SCTP_STREAM_OPEN;
3512 asoc->streamoutcnt += num_stream;
3513 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3514 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3515 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3516 SCTP_STREAM_CHANGE_DENIED);
3518 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3519 SCTP_STREAM_CHANGE_FAILED);
3521 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3522 if (asoc->stream_reset_outstanding)
3523 asoc->stream_reset_outstanding--;
3524 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3525 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3526 SCTP_STREAM_CHANGE_DENIED);
3527 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3528 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3529 SCTP_STREAM_CHANGE_FAILED);
3531 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3533 * a) Adopt the new in tsn.
3535 * c) Adopt the new out-tsn
3537 struct sctp_stream_reset_response_tsn *resp;
3538 struct sctp_forward_tsn_chunk fwdtsn;
3541 if (respin == NULL) {
3545 if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
3548 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3549 resp = (struct sctp_stream_reset_response_tsn *)respin;
3550 asoc->stream_reset_outstanding--;
3551 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3552 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3553 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3554 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3558 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3559 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3560 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3563 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3564 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3565 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3567 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3568 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3570 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3571 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3573 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3574 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3575 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3576 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3577 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3578 SCTP_ASSOC_RESET_DENIED);
3580 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3581 SCTP_ASSOC_RESET_FAILED);
3584 /* get rid of the request and get the request flags */
3585 if (asoc->stream_reset_outstanding == 0) {
3586 sctp_clean_up_stream_reset(stcb);
3590 if (asoc->stream_reset_outstanding == 0) {
3591 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3597 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3598 struct sctp_tmit_chunk *chk,
3599 struct sctp_stream_reset_in_request *req, int trunc)
3607 * peer wants me to send a str-reset to him for my outgoing seq's if
3610 struct sctp_association *asoc = &stcb->asoc;
3612 seq = ntohl(req->request_seq);
3613 if (asoc->str_reset_seq_in == seq) {
3614 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3615 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3616 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3618 /* Can't do it, since they exceeded our buffer size */
3619 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3620 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3621 len = ntohs(req->ph.param_length);
3622 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3623 if (number_entries) {
3624 for (i = 0; i < number_entries; i++) {
3625 temp = ntohs(req->list_of_streams[i]);
3626 if (temp >= stcb->asoc.streamoutcnt) {
3627 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3630 req->list_of_streams[i] = temp;
3632 for (i = 0; i < number_entries; i++) {
3633 if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
3634 stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
3639 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3640 if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
3641 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
3644 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3646 /* Can't do it, since we have sent one out */
3647 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3650 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3651 asoc->str_reset_seq_in++;
3652 } else if (asoc->str_reset_seq_in - 1 == seq) {
3653 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3654 } else if (asoc->str_reset_seq_in - 2 == seq) {
3655 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3657 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3659 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3663 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3664 struct sctp_tmit_chunk *chk,
3665 struct sctp_stream_reset_tsn_request *req)
3667 /* reset all in and out and update the tsn */
3669 * A) reset my str-seq's on in and out. B) Select a receive next,
3670 * and set cum-ack to it. Also process this selected number as a
3671 * fwd-tsn as well. C) set in the response my next sending seq.
3673 struct sctp_forward_tsn_chunk fwdtsn;
3674 struct sctp_association *asoc = &stcb->asoc;
3678 seq = ntohl(req->request_seq);
3679 if (asoc->str_reset_seq_in == seq) {
3680 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3681 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3682 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3684 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3685 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3686 fwdtsn.ch.chunk_flags = 0;
3687 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3688 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3692 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3693 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3694 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3696 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3697 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3698 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3699 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3700 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3701 atomic_add_int(&asoc->sending_seq, 1);
3702 /* save off historical data for retrans */
3703 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3704 asoc->last_sending_seq[0] = asoc->sending_seq;
3705 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3706 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3707 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3708 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3709 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3710 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3712 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3713 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3714 asoc->str_reset_seq_in++;
3715 } else if (asoc->str_reset_seq_in - 1 == seq) {
3716 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3717 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3718 } else if (asoc->str_reset_seq_in - 2 == seq) {
3719 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3720 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3722 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3728 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3729 struct sctp_tmit_chunk *chk,
3730 struct sctp_stream_reset_out_request *req, int trunc)
3733 int number_entries, len;
3734 struct sctp_association *asoc = &stcb->asoc;
3736 seq = ntohl(req->request_seq);
3738 /* now if its not a duplicate we process it */
3739 if (asoc->str_reset_seq_in == seq) {
3740 len = ntohs(req->ph.param_length);
3741 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3743 * the sender is resetting, handle the list issue.. we must
3744 * a) verify if we can do the reset, if so no problem b) If
3745 * we can't do the reset we must copy the request. c) queue
3746 * it, and setup the data in processor to trigger it off
3747 * when needed and dequeue all the queued data.
3749 tsn = ntohl(req->send_reset_at_tsn);
3751 /* move the reset action back one */
3752 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3753 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3754 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3756 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3757 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3758 /* we can do it now */
3759 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3760 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3763 * we must queue it up and thus wait for the TSN's
3764 * to arrive that are at or before tsn
3766 struct sctp_stream_reset_list *liste;
3769 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3770 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3771 siz, SCTP_M_STRESET);
3772 if (liste == NULL) {
3773 /* gak out of memory */
3774 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3775 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3780 liste->number_entries = number_entries;
3781 memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
3782 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3783 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
3785 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3786 asoc->str_reset_seq_in++;
3787 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3789 * one seq back, just echo back last action since my
3790 * response was lost.
3792 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3793 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3795 * two seq back, just echo back last action since my
3796 * response was lost.
3798 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3800 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3805 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3806 struct sctp_stream_reset_add_strm *str_add)
3809 * Peer is requesting to add more streams. If its within our
3810 * max-streams we will allow it.
3812 uint32_t num_stream, i;
3814 struct sctp_association *asoc = &stcb->asoc;
3815 struct sctp_queued_to_read *ctl, *nctl;
3817 /* Get the number. */
3818 seq = ntohl(str_add->request_seq);
3819 num_stream = ntohs(str_add->number_of_streams);
3820 /* Now what would be the new total? */
3821 if (asoc->str_reset_seq_in == seq) {
3822 num_stream += stcb->asoc.streamincnt;
3823 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3824 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3825 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3826 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
3827 (num_stream > 0xffff)) {
3828 /* We must reject it they ask for to many */
3830 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3832 /* Ok, we can do that :-) */
3833 struct sctp_stream_in *oldstrm;
3835 /* save off the old */
3836 oldstrm = stcb->asoc.strmin;
3837 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
3838 (num_stream * sizeof(struct sctp_stream_in)),
3840 if (stcb->asoc.strmin == NULL) {
3841 stcb->asoc.strmin = oldstrm;
3844 /* copy off the old data */
3845 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3846 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3847 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
3848 stcb->asoc.strmin[i].sid = i;
3849 stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered;
3850 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
3851 stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
3852 /* now anything on those queues? */
3853 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
3854 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
3855 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
3857 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
3858 TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
3859 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
3862 /* Init the new streams */
3863 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
3864 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3865 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
3866 stcb->asoc.strmin[i].sid = i;
3867 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
3868 stcb->asoc.strmin[i].pd_api_started = 0;
3869 stcb->asoc.strmin[i].delivery_started = 0;
3871 SCTP_FREE(oldstrm, SCTP_M_STRMI);
3872 /* update the size */
3873 stcb->asoc.streamincnt = num_stream;
3874 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3875 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3877 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3878 asoc->str_reset_seq_in++;
3879 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3881 * one seq back, just echo back last action since my
3882 * response was lost.
3884 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3885 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3887 * two seq back, just echo back last action since my
3888 * response was lost.
3890 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3892 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3898 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3899 struct sctp_stream_reset_add_strm *str_add)
3902 * Peer is requesting to add more streams. If its within our
3903 * max-streams we will allow it.
3905 uint16_t num_stream;
3907 struct sctp_association *asoc = &stcb->asoc;
3909 /* Get the number. */
3910 seq = ntohl(str_add->request_seq);
3911 num_stream = ntohs(str_add->number_of_streams);
3912 /* Now what would be the new total? */
3913 if (asoc->str_reset_seq_in == seq) {
3914 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3915 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3916 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3917 } else if (stcb->asoc.stream_reset_outstanding) {
3918 /* We must reject it we have something pending */
3919 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3921 /* Ok, we can do that :-) */
3924 mychk = stcb->asoc.streamoutcnt;
3925 mychk += num_stream;
3926 if (mychk < 0x10000) {
3927 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3928 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
3929 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3932 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3935 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
3936 asoc->str_reset_seq_in++;
3937 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3939 * one seq back, just echo back last action since my
3940 * response was lost.
3942 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3943 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3945 * two seq back, just echo back last action since my
3946 * response was lost.
3948 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3950 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3955 __attribute__((noinline))
3958 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3959 struct sctp_chunkhdr *ch_req)
3961 uint16_t remaining_length, param_len, ptype;
3962 struct sctp_paramhdr pstore;
3963 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
3967 struct sctp_tmit_chunk *chk;
3968 struct sctp_chunkhdr *ch;
3969 struct sctp_paramhdr *ph;
3973 /* now it may be a reset or a reset-response */
3974 remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
3976 /* setup for adding the response */
3977 sctp_alloc_a_chunk(stcb, chk);
3981 chk->copy_by_ref = 0;
3982 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
3983 chk->rec.chunk_id.can_take_data = 0;
3985 chk->asoc = &stcb->asoc;
3986 chk->no_fr_allowed = 0;
3987 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
3988 chk->book_size_scale = 0;
3989 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
3990 if (chk->data == NULL) {
3993 sctp_m_freem(chk->data);
3996 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3999 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4001 /* setup chunk parameters */
4002 chk->sent = SCTP_DATAGRAM_UNSENT;
4006 ch = mtod(chk->data, struct sctp_chunkhdr *);
4007 ch->chunk_type = SCTP_STREAM_RESET;
4008 ch->chunk_flags = 0;
4009 ch->chunk_length = htons(chk->send_size);
4010 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4011 offset += sizeof(struct sctp_chunkhdr);
4012 while (remaining_length >= sizeof(struct sctp_paramhdr)) {
4013 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
4018 param_len = ntohs(ph->param_length);
4019 if ((param_len > remaining_length) ||
4020 (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
4021 /* bad parameter length */
4024 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
4025 (uint8_t *)&cstore);
4030 ptype = ntohs(ph->param_type);
4032 if (param_len > sizeof(cstore)) {
4037 if (num_param > SCTP_MAX_RESET_PARAMS) {
4038 /* hit the max of parameters already sorry.. */
4041 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4042 struct sctp_stream_reset_out_request *req_out;
4044 if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
4047 req_out = (struct sctp_stream_reset_out_request *)ph;
4049 if (stcb->asoc.stream_reset_outstanding) {
4050 seq = ntohl(req_out->response_seq);
4051 if (seq == stcb->asoc.str_reset_seq_out) {
4053 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4056 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4057 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4058 struct sctp_stream_reset_add_strm *str_add;
4060 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4063 str_add = (struct sctp_stream_reset_add_strm *)ph;
4065 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4066 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4067 struct sctp_stream_reset_add_strm *str_add;
4069 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4072 str_add = (struct sctp_stream_reset_add_strm *)ph;
4074 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4075 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4076 struct sctp_stream_reset_in_request *req_in;
4079 req_in = (struct sctp_stream_reset_in_request *)ph;
4080 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4081 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4082 struct sctp_stream_reset_tsn_request *req_tsn;
4085 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4086 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4088 goto strres_nochunk;
4092 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4093 struct sctp_stream_reset_response *resp;
4096 if (param_len < sizeof(struct sctp_stream_reset_response)) {
4099 resp = (struct sctp_stream_reset_response *)ph;
4100 seq = ntohl(resp->response_seq);
4101 result = ntohl(resp->result);
4102 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4104 goto strres_nochunk;
4109 offset += SCTP_SIZE32(param_len);
4110 if (remaining_length >= SCTP_SIZE32(param_len)) {
4111 remaining_length -= SCTP_SIZE32(param_len);
4113 remaining_length = 0;
4117 /* we have no response free the stuff */
4118 goto strres_nochunk;
4120 /* ok we have a chunk to link in */
4121 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4124 stcb->asoc.ctrl_queue_cnt++;
4129 * Handle a router or endpoints report of a packet loss, there are two ways
4130 * to handle this, either we get the whole packet and must disect it
4131 * ourselves (possibly with truncation and or corruption) or it is a summary
4132 * from a middle box that did the disectting for us.
4135 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4136 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4138 uint32_t bottle_bw, on_queue;
4142 struct sctp_chunk_desc desc;
4143 struct sctp_chunkhdr *ch;
4145 chlen = ntohs(cp->ch.chunk_length);
4146 chlen -= sizeof(struct sctp_pktdrop_chunk);
4147 /* XXX possible chlen underflow */
4150 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4151 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4153 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4154 chlen -= sizeof(struct sctphdr);
4155 /* XXX possible chlen underflow */
4156 memset(&desc, 0, sizeof(desc));
4158 trunc_len = (uint16_t)ntohs(cp->trunc_len);
4159 if (trunc_len > limit) {
4163 /* now the chunks themselves */
4164 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4165 desc.chunk_type = ch->chunk_type;
4166 /* get amount we need to move */
4167 at = ntohs(ch->chunk_length);
4168 if (at < sizeof(struct sctp_chunkhdr)) {
4169 /* corrupt chunk, maybe at the end? */
4170 SCTP_STAT_INCR(sctps_pdrpcrupt);
4173 if (trunc_len == 0) {
4174 /* we are supposed to have all of it */
4176 /* corrupt skip it */
4177 SCTP_STAT_INCR(sctps_pdrpcrupt);
4181 /* is there enough of it left ? */
4182 if (desc.chunk_type == SCTP_DATA) {
4183 if (chlen < (sizeof(struct sctp_data_chunk) +
4184 sizeof(desc.data_bytes))) {
4188 if (chlen < sizeof(struct sctp_chunkhdr)) {
4193 if (desc.chunk_type == SCTP_DATA) {
4194 /* can we get out the tsn? */
4195 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4196 SCTP_STAT_INCR(sctps_pdrpmbda);
4198 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4200 struct sctp_data_chunk *dcp;
4204 dcp = (struct sctp_data_chunk *)ch;
4205 ddp = (uint8_t *)(dcp + 1);
4206 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4207 desc.data_bytes[iii] = ddp[iii];
4209 desc.tsn_ifany = dcp->dp.tsn;
4211 /* nope we are done. */
4212 SCTP_STAT_INCR(sctps_pdrpnedat);
4216 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4217 SCTP_STAT_INCR(sctps_pdrpmbct);
4220 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4221 SCTP_STAT_INCR(sctps_pdrppdbrk);
4224 if (SCTP_SIZE32(at) > chlen) {
4227 chlen -= SCTP_SIZE32(at);
4228 if (chlen < sizeof(struct sctp_chunkhdr)) {
4229 /* done, none left */
4232 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4234 /* Now update any rwnd --- possibly */
4235 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4236 /* From a peer, we get a rwnd report */
4239 SCTP_STAT_INCR(sctps_pdrpfehos);
4241 bottle_bw = ntohl(cp->bottle_bw);
4242 on_queue = ntohl(cp->current_onq);
4243 if (bottle_bw && on_queue) {
4244 /* a rwnd report is in here */
4245 if (bottle_bw > on_queue)
4246 a_rwnd = bottle_bw - on_queue;
4251 stcb->asoc.peers_rwnd = 0;
4253 if (a_rwnd > stcb->asoc.total_flight) {
4254 stcb->asoc.peers_rwnd =
4255 a_rwnd - stcb->asoc.total_flight;
4257 stcb->asoc.peers_rwnd = 0;
4259 if (stcb->asoc.peers_rwnd <
4260 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4261 /* SWS sender side engages */
4262 stcb->asoc.peers_rwnd = 0;
4267 SCTP_STAT_INCR(sctps_pdrpfmbox);
4270 /* now middle boxes in sat networks get a cwnd bump */
4271 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4272 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4273 (stcb->asoc.sat_network)) {
4275 * This is debatable but for sat networks it makes sense
4276 * Note if a T3 timer has went off, we will prohibit any
4277 * changes to cwnd until we exit the t3 loss recovery.
4279 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4280 net, cp, &bottle_bw, &on_queue);
4285 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4286 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4287 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4288 * length of the complete packet outputs: - length: modified to remaining
4289 * length after control processing - netp: modified to new sctp_nets after
4290 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4291 * bad packet,...) otherwise return the tcb for this packet
4294 __attribute__((noinline))
4296 static struct sctp_tcb *
4297 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4298 struct sockaddr *src, struct sockaddr *dst,
4299 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4300 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4301 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4302 uint32_t vrf_id, uint16_t port)
4304 struct sctp_association *asoc;
4305 struct mbuf *op_err;
4306 char msg[SCTP_DIAG_INFO_LEN];
4308 int num_chunks = 0; /* number of control chunks processed */
4309 uint32_t chk_length, contiguous;
4311 int abort_no_unlock = 0;
4315 * How big should this be, and should it be alloc'd? Lets try the
4316 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4317 * until we get into jumbo grams and such..
4319 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4321 uint32_t auth_offset = 0, auth_len = 0;
4322 int auth_skipped = 0;
4325 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4326 iphlen, *offset, length, (void *)stcb);
4329 SCTP_TCB_LOCK_ASSERT(stcb);
4331 /* validate chunk header length... */
4332 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4333 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4334 ntohs(ch->chunk_length));
4339 * validate the verification tag
4341 vtag_in = ntohl(sh->v_tag);
4343 if (ch->chunk_type == SCTP_INITIATION) {
4344 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4345 ntohs(ch->chunk_length), vtag_in);
4347 /* protocol error- silently discard... */
4348 SCTP_STAT_INCR(sctps_badvtag);
4350 SCTP_TCB_UNLOCK(stcb);
4354 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4356 * If there is no stcb, skip the AUTH chunk and process
4357 * later after a stcb is found (to validate the lookup was
4360 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4362 (inp->auth_supported == 1)) {
4363 /* save this chunk for later processing */
4365 auth_offset = *offset;
4366 auth_len = ntohs(ch->chunk_length);
4368 /* (temporarily) move past this chunk */
4369 *offset += SCTP_SIZE32(auth_len);
4370 if (*offset >= length) {
4371 /* no more data left in the mbuf chain */
4375 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4376 sizeof(struct sctp_chunkhdr), chunk_buf);
4383 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4384 goto process_control_chunks;
4387 * first check if it's an ASCONF with an unknown src addr we
4388 * need to look inside to find the association
4390 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4391 struct sctp_chunkhdr *asconf_ch = ch;
4392 uint32_t asconf_offset = 0, asconf_len = 0;
4394 /* inp's refcount may be reduced */
4395 SCTP_INP_INCR_REF(inp);
4397 asconf_offset = *offset;
4399 asconf_len = ntohs(asconf_ch->chunk_length);
4400 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4402 stcb = sctp_findassociation_ep_asconf(m,
4405 sh, &inp, netp, vrf_id);
4408 asconf_offset += SCTP_SIZE32(asconf_len);
4409 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4410 sizeof(struct sctp_chunkhdr), chunk_buf);
4411 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4414 * reduce inp's refcount if not reduced in
4415 * sctp_findassociation_ep_asconf().
4417 SCTP_INP_DECR_REF(inp);
4420 /* now go back and verify any auth chunk to be sure */
4421 if (auth_skipped && (stcb != NULL)) {
4422 struct sctp_auth_chunk *auth;
4424 if (auth_len <= SCTP_CHUNK_BUFFER_SIZE) {
4425 auth = (struct sctp_auth_chunk *)sctp_m_getptr(m, auth_offset, auth_len, chunk_buf);
4431 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4433 /* auth HMAC failed so dump it */
4437 /* remaining chunks are HMAC checked */
4438 stcb->asoc.authenticated = 1;
4443 SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4444 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4446 /* no association, so it's out of the blue... */
4447 sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4448 mflowtype, mflowid, inp->fibnum,
4454 /* ABORT and SHUTDOWN can use either v_tag... */
4455 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4456 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4457 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4458 /* Take the T-bit always into account. */
4459 if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
4460 (vtag_in == asoc->my_vtag)) ||
4461 (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4462 (asoc->peer_vtag != htonl(0)) &&
4463 (vtag_in == asoc->peer_vtag))) {
4466 /* drop this packet... */
4467 SCTP_STAT_INCR(sctps_badvtag);
4469 SCTP_TCB_UNLOCK(stcb);
4473 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4474 if (vtag_in != asoc->my_vtag) {
4476 * this could be a stale SHUTDOWN-ACK or the
4477 * peer never got the SHUTDOWN-COMPLETE and
4478 * is still hung; we have started a new asoc
4479 * but it won't complete until the shutdown
4483 SCTP_TCB_UNLOCK(stcb);
4485 SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4486 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4488 sctp_handle_ootb(m, iphlen, *offset, src, dst,
4490 mflowtype, mflowid, fibnum,
4495 /* for all other chunks, vtag must match */
4496 if (vtag_in != asoc->my_vtag) {
4497 /* invalid vtag... */
4498 SCTPDBG(SCTP_DEBUG_INPUT3,
4499 "invalid vtag: %xh, expect %xh\n",
4500 vtag_in, asoc->my_vtag);
4501 SCTP_STAT_INCR(sctps_badvtag);
4503 SCTP_TCB_UNLOCK(stcb);
4509 } /* end if !SCTP_COOKIE_ECHO */
4511 * process all control chunks...
4513 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4514 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4515 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4516 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4517 /* implied cookie-ack.. we must have lost the ack */
4518 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4522 process_control_chunks:
4523 while (IS_SCTP_CONTROL(ch)) {
4524 /* validate chunk length */
4525 chk_length = ntohs(ch->chunk_length);
4526 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4527 ch->chunk_type, chk_length);
4528 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4529 if (chk_length < sizeof(*ch) ||
4530 (*offset + (int)chk_length) > length) {
4534 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4536 * INIT and INIT-ACK only gets the init ack "header" portion
4537 * only because we don't have to process the peer's COOKIE.
4538 * All others get a complete chunk.
4540 switch (ch->chunk_type) {
4541 case SCTP_INITIATION:
4542 contiguous = sizeof(struct sctp_init_chunk);
4544 case SCTP_INITIATION_ACK:
4545 contiguous = sizeof(struct sctp_init_ack_chunk);
4548 contiguous = min(chk_length, sizeof(chunk_buf));
4551 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4557 SCTP_TCB_UNLOCK(stcb);
4563 /* Save off the last place we got a control from */
4565 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4567 * allow last_control to be NULL if
4568 * ASCONF... ASCONF processing will find the
4571 if ((netp != NULL) && (*netp != NULL))
4572 stcb->asoc.last_control_chunk_from = *netp;
4575 #ifdef SCTP_AUDITING_ENABLED
4576 sctp_audit_log(0xB0, ch->chunk_type);
4579 /* check to see if this chunk required auth, but isn't */
4580 if ((stcb != NULL) &&
4581 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4582 !stcb->asoc.authenticated) {
4583 /* "silently" ignore */
4584 SCTP_STAT_INCR(sctps_recvauthmissing);
4587 switch (ch->chunk_type) {
4588 case SCTP_INITIATION:
4589 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4590 /* The INIT chunk must be the only chunk. */
4591 if ((num_chunks > 1) ||
4592 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4593 /* RFC 4960 requires that no ABORT is sent */
4596 SCTP_TCB_UNLOCK(stcb);
4600 /* Honor our resource limit. */
4601 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4602 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4603 sctp_abort_association(inp, stcb, m, iphlen,
4604 src, dst, sh, op_err,
4610 sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4611 (struct sctp_init_chunk *)ch, inp,
4612 stcb, *netp, &abort_no_unlock,
4616 if ((!abort_no_unlock) && (stcb != NULL)) {
4617 SCTP_TCB_UNLOCK(stcb);
4621 case SCTP_PAD_CHUNK:
4623 case SCTP_INITIATION_ACK:
4624 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT_ACK\n");
4625 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4626 /* We are not interested anymore */
4627 if ((stcb != NULL) && (stcb->asoc.total_output_queue_size)) {
4632 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4633 SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4638 /* The INIT-ACK chunk must be the only chunk. */
4639 if ((num_chunks > 1) ||
4640 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4644 if ((netp != NULL) && (*netp != NULL)) {
4645 ret = sctp_handle_init_ack(m, iphlen, *offset,
4647 (struct sctp_init_ack_chunk *)ch,
4656 if (abort_no_unlock) {
4660 * Special case, I must call the output routine to
4661 * get the cookie echoed
4663 if ((stcb != NULL) && (ret == 0)) {
4664 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4668 case SCTP_SELECTIVE_ACK:
4669 case SCTP_NR_SELECTIVE_ACK:
4672 uint32_t a_rwnd, cum_ack;
4673 uint16_t num_seg, num_nr_seg, num_dup;
4675 int offset_seg, offset_dup;
4677 SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n",
4678 ch->chunk_type == SCTP_SELECTIVE_ACK ? "SCTP_SACK" : "SCTP_NR_SACK");
4679 SCTP_STAT_INCR(sctps_recvsacks);
4681 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing %s chunk\n",
4682 (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK");
4685 if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4686 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4687 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4691 if (stcb->asoc.nrsack_supported == 0) {
4694 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4695 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR_SACK chunk, too small\n");
4699 if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4701 * If we have sent a shutdown-ack, we will pay no
4702 * attention to a sack sent in to us since
4703 * we don't care anymore.
4707 flags = ch->chunk_flags;
4708 if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4709 struct sctp_sack_chunk *sack;
4711 sack = (struct sctp_sack_chunk *)ch;
4712 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4713 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4715 num_dup = ntohs(sack->sack.num_dup_tsns);
4716 a_rwnd = ntohl(sack->sack.a_rwnd);
4717 if (sizeof(struct sctp_sack_chunk) +
4718 num_seg * sizeof(struct sctp_gap_ack_block) +
4719 num_dup * sizeof(uint32_t) != chk_length) {
4720 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4723 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4724 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4726 struct sctp_nr_sack_chunk *nr_sack;
4728 nr_sack = (struct sctp_nr_sack_chunk *)ch;
4729 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4730 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4731 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4732 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4733 a_rwnd = ntohl(nr_sack->nr_sack.a_rwnd);
4734 if (sizeof(struct sctp_nr_sack_chunk) +
4735 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4736 num_dup * sizeof(uint32_t) != chk_length) {
4737 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4740 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4741 offset_dup = offset_seg + (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block);
4743 SCTPDBG(SCTP_DEBUG_INPUT3, "%s process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4744 (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK",
4745 cum_ack, num_seg, a_rwnd);
4746 stcb->asoc.seen_a_sack_this_pkt = 1;
4747 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4748 (num_seg == 0) && (num_nr_seg == 0) &&
4749 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4750 (stcb->asoc.saw_sack_with_frags == 0) &&
4751 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4752 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
4754 * We have a SIMPLE sack having no
4755 * prior segments and data on sent
4756 * queue to be acked. Use the faster
4757 * path sack processing. We also
4758 * allow window update sacks with no
4759 * missing segments to go this way
4762 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
4763 &abort_now, ecne_seen);
4765 if ((netp != NULL) && (*netp != NULL)) {
4766 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4767 num_seg, num_nr_seg, num_dup, &abort_now, flags,
4768 cum_ack, a_rwnd, ecne_seen);
4772 /* ABORT signal from sack processing */
4776 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4777 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4778 (stcb->asoc.stream_queue_cnt == 0)) {
4779 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4783 case SCTP_HEARTBEAT_REQUEST:
4784 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
4785 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
4786 SCTP_STAT_INCR(sctps_recvheartbeat);
4787 sctp_send_heartbeat_ack(stcb, m, *offset,
4791 case SCTP_HEARTBEAT_ACK:
4792 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT_ACK\n");
4793 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
4798 SCTP_STAT_INCR(sctps_recvheartbeatack);
4799 if ((netp != NULL) && (*netp != NULL)) {
4800 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
4804 case SCTP_ABORT_ASSOCIATION:
4805 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
4808 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
4809 if (sctp_handle_abort((struct sctp_abort_chunk *)ch, stcb, *netp)) {
4819 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
4821 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
4825 if ((netp != NULL) && (*netp != NULL)) {
4828 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
4829 stcb, *netp, &abort_flag);
4836 case SCTP_SHUTDOWN_ACK:
4837 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_ACK, stcb %p\n", (void *)stcb);
4838 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
4839 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
4844 case SCTP_OPERATION_ERROR:
4845 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP_ERR\n");
4846 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL) &&
4847 sctp_handle_error(ch, stcb, *netp, contiguous) < 0) {
4852 case SCTP_COOKIE_ECHO:
4853 SCTPDBG(SCTP_DEBUG_INPUT3,
4854 "SCTP_COOKIE_ECHO, stcb %p\n", (void *)stcb);
4855 if ((stcb != NULL) && (stcb->asoc.total_output_queue_size > 0)) {
4858 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4859 /* We are not interested anymore */
4862 SCTP_TCB_UNLOCK(stcb);
4869 * First are we accepting? We do this again here
4870 * since it is possible that a previous endpoint WAS
4871 * listening responded to a INIT-ACK and then
4872 * closed. We opened and bound.. and are now no
4875 * XXXGL: notes on checking listen queue length.
4876 * 1) SCTP_IS_LISTENING() doesn't necessarily mean
4877 * SOLISTENING(), because a listening "UDP type"
4878 * socket isn't listening in terms of the socket
4879 * layer. It is a normal data flow socket, that
4880 * can fork off new connections. Thus, we should
4881 * look into sol_qlen only in case we are !UDP.
4882 * 2) Checking sol_qlen in general requires locking
4883 * the socket, and this code lacks that.
4885 if ((stcb == NULL) &&
4886 (!SCTP_IS_LISTENING(inp) ||
4887 (!(inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
4888 inp->sctp_socket->sol_qlen >= inp->sctp_socket->sol_qlimit))) {
4889 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4890 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
4891 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4892 sctp_abort_association(inp, stcb, m, iphlen,
4893 src, dst, sh, op_err,
4900 struct mbuf *ret_buf;
4901 struct sctp_inpcb *linp;
4902 struct sctp_tmit_chunk *chk;
4911 SCTP_ASOC_CREATE_LOCK(linp);
4912 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4913 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4914 SCTP_ASOC_CREATE_UNLOCK(linp);
4920 struct sctp_tcb *locked_stcb;
4924 sctp_handle_cookie_echo(m, iphlen,
4928 (struct sctp_cookie_echo_chunk *)ch,
4938 if ((locked_stcb != NULL) && (locked_stcb != stcb)) {
4939 SCTP_TCB_UNLOCK(locked_stcb);
4942 SCTP_TCB_LOCK_ASSERT(stcb);
4948 SCTP_ASOC_CREATE_UNLOCK(linp);
4950 if (ret_buf == NULL) {
4952 SCTP_TCB_UNLOCK(stcb);
4954 SCTPDBG(SCTP_DEBUG_INPUT3,
4955 "GAK, null buffer\n");
4959 /* if AUTH skipped, see if it verified... */
4964 /* Restart the timer if we have pending data */
4965 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
4966 if (chk->whoTo != NULL) {
4971 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
4975 case SCTP_COOKIE_ACK:
4976 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE_ACK, stcb %p\n", (void *)stcb);
4977 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
4980 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4981 /* We are not interested anymore */
4982 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4985 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4986 SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
4991 if ((netp != NULL) && (*netp != NULL)) {
4992 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
4996 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_ECHO\n");
4997 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5002 if (stcb->asoc.ecn_supported == 0) {
5005 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, stcb);
5009 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_CWR\n");
5010 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5014 if (stcb->asoc.ecn_supported == 0) {
5017 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5019 case SCTP_SHUTDOWN_COMPLETE:
5020 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_COMPLETE, stcb %p\n", (void *)stcb);
5021 /* must be first and only chunk */
5022 if ((num_chunks > 1) ||
5023 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5027 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5028 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5035 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5037 if (stcb->asoc.asconf_supported == 0) {
5040 sctp_handle_asconf(m, *offset, src,
5041 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5045 case SCTP_ASCONF_ACK:
5046 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF_ACK\n");
5047 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5052 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5053 if (stcb->asoc.asconf_supported == 0) {
5056 /* He's alive so give him credit */
5057 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5058 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5059 stcb->asoc.overall_error_count,
5061 SCTP_FROM_SCTP_INPUT,
5064 stcb->asoc.overall_error_count = 0;
5065 sctp_handle_asconf_ack(m, *offset,
5066 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5067 if (abort_no_unlock)
5071 case SCTP_FORWARD_CUM_TSN:
5072 case SCTP_IFORWARD_CUM_TSN:
5073 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD_TSN\n");
5074 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5083 if (stcb->asoc.prsctp_supported == 0) {
5087 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5088 /* We are not interested anymore */
5089 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5090 SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5095 * For sending a SACK this looks like DATA
5098 stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from;
5099 sctp_handle_forward_tsn(stcb,
5100 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5107 case SCTP_STREAM_RESET:
5108 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5109 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5114 if (stcb->asoc.reconfig_supported == 0) {
5117 if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5118 /* stop processing */
5123 case SCTP_PACKET_DROPPED:
5124 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5125 /* re-get it all please */
5126 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5132 if ((ch != NULL) && (stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5133 if (stcb->asoc.pktdrop_supported == 0) {
5136 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5138 min(chk_length, contiguous));
5141 case SCTP_AUTHENTICATION:
5142 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5144 /* save the first AUTH for later processing */
5145 if (auth_skipped == 0) {
5146 auth_offset = *offset;
5147 auth_len = chk_length;
5150 /* skip this chunk (temporarily) */
5153 if (stcb->asoc.auth_supported == 0) {
5156 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5157 (chk_length > (sizeof(struct sctp_auth_chunk) +
5158 SCTP_AUTH_DIGEST_LEN_MAX))) {
5163 if (got_auth == 1) {
5164 /* skip this chunk... it's already auth'd */
5168 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5170 /* auth HMAC failed so dump the packet */
5174 /* remaining chunks are HMAC checked */
5175 stcb->asoc.authenticated = 1;
5181 /* it's an unknown chunk! */
5182 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5183 struct sctp_gen_error_cause *cause;
5186 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5187 0, M_NOWAIT, 1, MT_DATA);
5188 if (op_err != NULL) {
5189 len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset));
5190 cause = mtod(op_err, struct sctp_gen_error_cause *);
5191 cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5192 cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause)));
5193 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
5194 SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
5195 if (SCTP_BUF_NEXT(op_err) != NULL) {
5196 #ifdef SCTP_MBUF_LOGGING
5197 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5198 sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5201 sctp_queue_op_err(stcb, op_err);
5203 sctp_m_freem(op_err);
5207 if ((ch->chunk_type & 0x80) == 0) {
5208 /* discard this packet */
5211 } /* else skip this bad chunk and continue... */
5213 } /* switch (ch->chunk_type) */
5217 /* get the next chunk */
5218 *offset += SCTP_SIZE32(chk_length);
5219 if (*offset >= length) {
5220 /* no more data left in the mbuf chain */
5223 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5224 sizeof(struct sctp_chunkhdr), chunk_buf);
5231 if ((asconf_cnt > 0) && (stcb != NULL)) {
5232 sctp_send_asconf_ack(stcb);
5239 * common input chunk processing (v4 and v6)
5242 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5243 struct sockaddr *src, struct sockaddr *dst,
5244 struct sctphdr *sh, struct sctp_chunkhdr *ch,
5245 uint8_t compute_crc,
5247 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5248 uint32_t vrf_id, uint16_t port)
5251 int fwd_tsn_seen = 0, data_processed = 0;
5252 struct mbuf *m = *mm, *op_err;
5253 char msg[SCTP_DIAG_INFO_LEN];
5255 int cnt_ctrl_ready = 0;
5256 struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5257 struct sctp_tcb *stcb = NULL;
5258 struct sctp_nets *net = NULL;
5260 SCTP_STAT_INCR(sctps_recvdatagrams);
5261 #ifdef SCTP_AUDITING_ENABLED
5262 sctp_audit_log(0xE0, 1);
5263 sctp_auditing(0, inp, stcb, net);
5265 if (compute_crc != 0) {
5266 uint32_t check, calc_check;
5268 check = sh->checksum;
5270 calc_check = sctp_calculate_cksum(m, iphlen);
5271 sh->checksum = check;
5272 if (calc_check != check) {
5273 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5274 calc_check, check, (void *)m, length, iphlen);
5275 stcb = sctp_findassociation_addr(m, offset, src, dst,
5276 sh, ch, &inp, &net, vrf_id);
5277 #if defined(INET) || defined(INET6)
5278 if ((ch->chunk_type != SCTP_INITIATION) &&
5279 (net != NULL) && (net->port != port)) {
5280 if (net->port == 0) {
5281 /* UDP encapsulation turned on. */
5282 net->mtu -= sizeof(struct udphdr);
5283 if (stcb->asoc.smallest_mtu > net->mtu) {
5284 sctp_pathmtu_adjustment(stcb, net->mtu);
5286 } else if (port == 0) {
5287 /* UDP encapsulation turned off. */
5288 net->mtu += sizeof(struct udphdr);
5289 /* XXX Update smallest_mtu */
5295 net->flowtype = mflowtype;
5296 net->flowid = mflowid;
5298 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5299 if ((inp != NULL) && (stcb != NULL)) {
5300 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5301 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5302 } else if ((inp != NULL) && (stcb == NULL)) {
5305 SCTP_STAT_INCR(sctps_badsum);
5306 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5310 /* Destination port of 0 is illegal, based on RFC4960. */
5311 if (sh->dest_port == 0) {
5312 SCTP_STAT_INCR(sctps_hdrops);
5315 stcb = sctp_findassociation_addr(m, offset, src, dst,
5316 sh, ch, &inp, &net, vrf_id);
5317 #if defined(INET) || defined(INET6)
5318 if ((ch->chunk_type != SCTP_INITIATION) &&
5319 (net != NULL) && (net->port != port)) {
5320 if (net->port == 0) {
5321 /* UDP encapsulation turned on. */
5322 net->mtu -= sizeof(struct udphdr);
5323 if (stcb->asoc.smallest_mtu > net->mtu) {
5324 sctp_pathmtu_adjustment(stcb, net->mtu);
5326 } else if (port == 0) {
5327 /* UDP encapsulation turned off. */
5328 net->mtu += sizeof(struct udphdr);
5329 /* XXX Update smallest_mtu */
5335 net->flowtype = mflowtype;
5336 net->flowid = mflowid;
5339 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5340 SCTP_STAT_INCR(sctps_noport);
5341 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5344 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5345 sctp_send_shutdown_complete2(src, dst, sh,
5346 mflowtype, mflowid, fibnum,
5350 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5353 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5354 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5355 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5356 (ch->chunk_type != SCTP_INIT))) {
5357 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5359 sctp_send_abort(m, iphlen, src, dst,
5361 mflowtype, mflowid, fibnum,
5366 } else if (stcb == NULL) {
5369 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5370 (void *)m, iphlen, offset, length, (void *)stcb);
5372 /* always clear this before beginning a packet */
5373 stcb->asoc.authenticated = 0;
5374 stcb->asoc.seen_a_sack_this_pkt = 0;
5375 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5376 (void *)stcb, stcb->asoc.state);
5378 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5379 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5381 * If we hit here, we had a ref count
5382 * up when the assoc was aborted and the
5383 * timer is clearing out the assoc, we should
5384 * NOT respond to any packet.. its OOTB.
5386 SCTP_TCB_UNLOCK(stcb);
5388 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5389 SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5390 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5392 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5393 mflowtype, mflowid, inp->fibnum,
5398 if (IS_SCTP_CONTROL(ch)) {
5399 /* process the control portion of the SCTP packet */
5400 /* sa_ignore NO_NULL_CHK */
5401 stcb = sctp_process_control(m, iphlen, &offset, length,
5403 inp, stcb, &net, &fwd_tsn_seen,
5404 mflowtype, mflowid, fibnum,
5408 * This covers us if the cookie-echo was there and
5409 * it changes our INP.
5411 inp = stcb->sctp_ep;
5412 #if defined(INET) || defined(INET6)
5413 if ((ch->chunk_type != SCTP_INITIATION) &&
5414 (net != NULL) && (net->port != port)) {
5415 if (net->port == 0) {
5416 /* UDP encapsulation turned on. */
5417 net->mtu -= sizeof(struct udphdr);
5418 if (stcb->asoc.smallest_mtu > net->mtu) {
5419 sctp_pathmtu_adjustment(stcb, net->mtu);
5421 } else if (port == 0) {
5422 /* UDP encapsulation turned off. */
5423 net->mtu += sizeof(struct udphdr);
5424 /* XXX Update smallest_mtu */
5432 * no control chunks, so pre-process DATA chunks (these
5433 * checks are taken care of by control processing)
5437 * if DATA only packet, and auth is required, then punt...
5438 * can't have authenticated without any AUTH (control)
5441 if ((stcb != NULL) &&
5442 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5443 /* "silently" ignore */
5444 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5445 SCTP_STAT_INCR(sctps_recvauthmissing);
5449 /* out of the blue DATA chunk */
5450 SCTP_PROBE5(receive, NULL, NULL, m, NULL, sh);
5451 SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5452 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5454 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5455 mflowtype, mflowid, fibnum,
5459 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5460 /* v_tag mismatch! */
5461 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5462 SCTP_STAT_INCR(sctps_badvtag);
5467 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5470 * no valid TCB for this packet, or we found it's a bad
5471 * packet while processing control, or we're done with this
5472 * packet (done or skip rest of data), so we drop it...
5478 * DATA chunk processing
5480 /* plow through the data chunks while length > offset */
5483 * Rest should be DATA only. Check authentication state if AUTH for
5486 if ((length > offset) &&
5488 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5489 !stcb->asoc.authenticated) {
5490 /* "silently" ignore */
5491 SCTP_STAT_INCR(sctps_recvauthmissing);
5492 SCTPDBG(SCTP_DEBUG_AUTH1,
5493 "Data chunk requires AUTH, skipped\n");
5496 if (length > offset) {
5500 * First check to make sure our state is correct. We would
5501 * not get here unless we really did have a tag, so we don't
5502 * abort if this happens, just dump the chunk silently.
5504 switch (SCTP_GET_STATE(stcb)) {
5505 case SCTP_STATE_COOKIE_ECHOED:
5507 * we consider data with valid tags in this state
5508 * shows us the cookie-ack was lost. Imply it was
5511 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5513 case SCTP_STATE_COOKIE_WAIT:
5515 * We consider OOTB any data sent during asoc setup.
5517 SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5518 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5520 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5521 mflowtype, mflowid, inp->fibnum,
5524 /* sa_ignore NOTREACHED */
5526 case SCTP_STATE_EMPTY: /* should not happen */
5527 case SCTP_STATE_INUSE: /* should not happen */
5528 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5529 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5532 /* sa_ignore NOTREACHED */
5534 case SCTP_STATE_OPEN:
5535 case SCTP_STATE_SHUTDOWN_SENT:
5538 /* plow through the data chunks while length > offset */
5539 retval = sctp_process_data(mm, iphlen, &offset, length,
5540 inp, stcb, net, &high_tsn);
5543 * The association aborted, NO UNLOCK needed since
5544 * the association is destroyed.
5551 * Anything important needs to have been m_copy'ed in
5556 /* take care of ecn */
5557 if ((data_processed == 1) &&
5558 (stcb->asoc.ecn_supported == 1) &&
5559 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5560 /* Yep, we need to add a ECNE */
5561 sctp_send_ecn_echo(stcb, net, high_tsn);
5564 if ((data_processed == 0) && (fwd_tsn_seen)) {
5566 uint32_t highest_tsn;
5568 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5569 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5571 highest_tsn = stcb->asoc.highest_tsn_inside_map;
5573 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
5574 stcb->asoc.send_sack = 1;
5575 sctp_sack_check(stcb, was_a_gap);
5576 } else if (fwd_tsn_seen) {
5577 stcb->asoc.send_sack = 1;
5579 /* trigger send of any chunks in queue... */
5581 #ifdef SCTP_AUDITING_ENABLED
5582 sctp_audit_log(0xE0, 2);
5583 sctp_auditing(1, inp, stcb, net);
5585 SCTPDBG(SCTP_DEBUG_INPUT1,
5586 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5587 stcb->asoc.peers_rwnd,
5588 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5589 stcb->asoc.total_flight);
5590 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5591 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5592 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5594 if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
5596 stcb->asoc.trigger_reset ||
5598 (stcb->asoc.peers_rwnd > 0 ||
5599 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5600 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5601 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5602 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5604 #ifdef SCTP_AUDITING_ENABLED
5605 sctp_audit_log(0xE0, 3);
5606 sctp_auditing(2, inp, stcb, net);
5610 SCTP_TCB_UNLOCK(stcb);
5612 if (inp_decr != NULL) {
5613 /* reduce ref-count */
5614 SCTP_INP_WLOCK(inp_decr);
5615 SCTP_INP_DECR_REF(inp_decr);
5616 SCTP_INP_WUNLOCK(inp_decr);
5623 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5627 uint32_t vrf_id = 0;
5629 struct sockaddr_in src, dst;
5632 struct sctp_chunkhdr *ch;
5634 uint8_t compute_crc;
5640 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
5641 SCTP_RELEASE_PKT(i_pak);
5644 m = SCTP_HEADER_TO_CHAIN(i_pak);
5645 #ifdef SCTP_MBUF_LOGGING
5646 /* Log in any input mbufs */
5647 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5648 sctp_log_mbc(m, SCTP_MBUF_INPUT);
5651 #ifdef SCTP_PACKET_LOGGING
5652 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
5656 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
5657 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
5659 if_name(m->m_pkthdr.rcvif),
5660 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
5661 mflowid = m->m_pkthdr.flowid;
5662 mflowtype = M_HASHTYPE_GET(m);
5663 fibnum = M_GETFIB(m);
5664 SCTP_STAT_INCR(sctps_recvpackets);
5665 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
5666 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
5667 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5668 if (SCTP_BUF_LEN(m) < offset) {
5669 if ((m = m_pullup(m, offset)) == NULL) {
5670 SCTP_STAT_INCR(sctps_hdrops);
5674 ip = mtod(m, struct ip *);
5675 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
5676 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
5677 offset -= sizeof(struct sctp_chunkhdr);
5678 memset(&src, 0, sizeof(struct sockaddr_in));
5679 src.sin_family = AF_INET;
5680 src.sin_len = sizeof(struct sockaddr_in);
5681 src.sin_port = sh->src_port;
5682 src.sin_addr = ip->ip_src;
5683 memset(&dst, 0, sizeof(struct sockaddr_in));
5684 dst.sin_family = AF_INET;
5685 dst.sin_len = sizeof(struct sockaddr_in);
5686 dst.sin_port = sh->dest_port;
5687 dst.sin_addr = ip->ip_dst;
5688 length = ntohs(ip->ip_len);
5689 /* Validate mbuf chain length with IP payload length. */
5690 if (SCTP_HEADER_LEN(m) != length) {
5691 SCTPDBG(SCTP_DEBUG_INPUT1,
5692 "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
5693 SCTP_STAT_INCR(sctps_hdrops);
5696 /* SCTP does not allow broadcasts or multicasts */
5697 if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
5700 if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
5703 ecn_bits = ip->ip_tos;
5704 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5705 SCTP_STAT_INCR(sctps_recvhwcrc);
5708 SCTP_STAT_INCR(sctps_recvswcrc);
5711 sctp_common_input_processing(&m, iphlen, offset, length,
5712 (struct sockaddr *)&src,
5713 (struct sockaddr *)&dst,
5717 mflowtype, mflowid, fibnum,
5726 #if defined(SCTP_MCORE_INPUT) && defined(SMP)
5727 extern int *sctp_cpuarry;
5731 sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
5738 #if defined(SCTP_MCORE_INPUT) && defined(SMP)
5744 uint32_t flowid, tag;
5746 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
5747 flowid = m->m_pkthdr.flowid;
5750 * No flow id built by lower layers fix it so we
5753 offset = off + sizeof(struct sctphdr);
5754 if (SCTP_BUF_LEN(m) < offset) {
5755 if ((m = m_pullup(m, offset)) == NULL) {
5756 SCTP_STAT_INCR(sctps_hdrops);
5757 return (IPPROTO_DONE);
5760 ip = mtod(m, struct ip *);
5761 sh = (struct sctphdr *)((caddr_t)ip + off);
5762 tag = htonl(sh->v_tag);
5763 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
5764 m->m_pkthdr.flowid = flowid;
5765 M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH);
5767 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
5768 sctp_queue_to_mcore(m, off, cpu_to_use);
5769 return (IPPROTO_DONE);
5772 sctp_input_with_port(m, off, 0);
5773 return (IPPROTO_DONE);