2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.h>
37 #include <netinet/sctp_var.h>
38 #include <netinet/sctp_sysctl.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctputil.h>
42 #include <netinet/sctp_output.h>
43 #include <netinet/sctp_input.h>
44 #include <netinet/sctp_auth.h>
45 #include <netinet/sctp_indata.h>
46 #include <netinet/sctp_asconf.h>
47 #include <netinet/sctp_bsd_addr.h>
48 #include <netinet/sctp_timer.h>
49 #include <netinet/sctp_crc32.h>
50 #if defined(INET) || defined(INET6)
51 #include <netinet/udp.h>
58 sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
60 struct sctp_nets *net;
63 * This now not only stops all cookie timers it also stops any INIT
64 * timers as well. This will make sure that the timers are stopped
65 * in all collision cases.
67 SCTP_TCB_LOCK_ASSERT(stcb);
68 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
69 if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
70 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
73 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
74 } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
75 sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
78 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
85 sctp_handle_init(struct mbuf *m, int iphlen, int offset,
86 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
87 struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
88 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_no_unlock,
89 uint8_t mflowtype, uint32_t mflowid,
90 uint32_t vrf_id, uint16_t port)
92 struct sctp_init *init;
95 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
100 /* validate length */
101 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
102 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
103 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
107 *abort_no_unlock = 1;
110 /* validate parameters */
112 if (init->initiate_tag == 0) {
113 /* protocol error... send abort */
114 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
115 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
119 *abort_no_unlock = 1;
122 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
123 /* invalid parameter... send abort */
124 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
125 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
129 *abort_no_unlock = 1;
132 if (init->num_inbound_streams == 0) {
133 /* protocol error... send abort */
134 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
135 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
139 *abort_no_unlock = 1;
142 if (init->num_outbound_streams == 0) {
143 /* protocol error... send abort */
144 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
145 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
149 *abort_no_unlock = 1;
152 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
153 offset + ntohs(cp->ch.chunk_length))) {
154 /* auth parameter(s) error... send abort */
155 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
156 "Problem with AUTH parameters");
157 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
161 *abort_no_unlock = 1;
164 /* We are only accepting if we have a socket with positive
166 if ((stcb == NULL) &&
167 ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
168 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
169 (inp->sctp_socket == NULL) ||
170 (inp->sctp_socket->so_qlimit == 0))) {
172 * FIX ME ?? What about TCP model and we have a
173 * match/restart case? Actually no fix is needed. the lookup
174 * will always find the existing assoc so stcb would not be
175 * NULL. It may be questionable to do this since we COULD
176 * just send back the INIT-ACK and hope that the app did
177 * accept()'s by the time the COOKIE was sent. But there is
178 * a price to pay for COOKIE generation and I don't want to
179 * pay it on the chance that the app will actually do some
180 * accepts(). The App just looses and should NOT be in this
183 if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
184 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
186 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
187 mflowtype, mflowid, inp->fibnum,
192 if ((stcb != NULL) &&
193 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
194 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
195 sctp_send_shutdown_ack(stcb, NULL);
196 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
198 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
199 sctp_send_initiate_ack(inp, stcb, net, m, iphlen, offset,
206 SCTP_INP_RUNLOCK(inp);
211 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
215 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
216 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
223 struct sctp_stream_queue_pending *sp;
224 struct sctp_association *asoc;
227 * This function returns if any stream has true unsent data on it.
228 * Note that as it looks through it will clean up any places that
229 * have old data that has been sent but left at top of stream queue.
233 SCTP_TCB_SEND_LOCK(stcb);
234 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
235 /* Check to see if some data queued */
236 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
237 /* sa_ignore FREED_MEMORY */
238 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
242 if ((sp->msg_is_complete) &&
244 (sp->sender_all_done)) {
246 * We are doing differed cleanup. Last time
247 * through when we took all the data the
248 * sender_all_done was not set.
250 if (sp->put_last_out == 0) {
251 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
252 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
258 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
259 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
260 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, &asoc->strmout[i], sp, 1);
262 sctp_free_remote_addr(sp->net);
266 sctp_m_freem(sp->data);
269 sctp_free_a_strmoq(stcb, sp, so_locked);
270 if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
276 if (unsent_data > 0) {
281 SCTP_TCB_SEND_UNLOCK(stcb);
282 return (unsent_data);
286 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
288 struct sctp_init *init;
289 struct sctp_association *asoc;
290 struct sctp_nets *lnet;
295 /* save off parameters */
296 asoc->peer_vtag = ntohl(init->initiate_tag);
297 asoc->peers_rwnd = ntohl(init->a_rwnd);
299 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
301 if (!TAILQ_EMPTY(&asoc->nets)) {
302 /* update any ssthresh's that may have a default */
303 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
304 lnet->ssthresh = asoc->peers_rwnd;
305 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
306 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
310 SCTP_TCB_SEND_LOCK(stcb);
311 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
313 struct sctp_stream_out *outs;
314 struct sctp_stream_queue_pending *sp, *nsp;
315 struct sctp_tmit_chunk *chk, *nchk;
317 /* abandon the upper streams */
318 newcnt = ntohs(init->num_inbound_streams);
319 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
320 if (chk->rec.data.sid >= newcnt) {
321 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
322 asoc->send_queue_cnt--;
323 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
324 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
327 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
330 if (chk->data != NULL) {
331 sctp_free_bufspace(stcb, asoc, chk, 1);
332 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
333 0, chk, SCTP_SO_NOT_LOCKED);
335 sctp_m_freem(chk->data);
339 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
340 /* sa_ignore FREED_MEMORY */
344 for (i = newcnt; i < asoc->pre_open_streams; i++) {
345 outs = &asoc->strmout[i];
346 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
347 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
348 TAILQ_REMOVE(&outs->outqueue, sp, next);
349 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
350 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
351 stcb, 0, sp, SCTP_SO_NOT_LOCKED);
353 sctp_m_freem(sp->data);
357 sctp_free_remote_addr(sp->net);
361 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
362 /* sa_ignore FREED_MEMORY */
364 outs->state = SCTP_STREAM_CLOSED;
367 /* cut back the count */
368 asoc->pre_open_streams = newcnt;
370 SCTP_TCB_SEND_UNLOCK(stcb);
371 asoc->streamoutcnt = asoc->pre_open_streams;
373 for (i = 0; i < asoc->streamoutcnt; i++) {
374 asoc->strmout[i].state = SCTP_STREAM_OPEN;
377 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
378 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
379 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
380 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
382 /* This is the next one we expect */
383 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
385 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
386 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
388 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
389 /* open the requested streams */
391 if (asoc->strmin != NULL) {
392 /* Free the old ones */
393 for (i = 0; i < asoc->streamincnt; i++) {
394 sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
395 sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
397 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
399 if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
400 asoc->streamincnt = ntohs(init->num_outbound_streams);
402 asoc->streamincnt = asoc->max_inbound_streams;
404 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
405 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
406 if (asoc->strmin == NULL) {
407 /* we didn't get memory for the streams! */
408 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
411 for (i = 0; i < asoc->streamincnt; i++) {
412 asoc->strmin[i].sid = i;
413 asoc->strmin[i].last_mid_delivered = 0xffffffff;
414 TAILQ_INIT(&asoc->strmin[i].inqueue);
415 TAILQ_INIT(&asoc->strmin[i].uno_inqueue);
416 asoc->strmin[i].pd_api_started = 0;
417 asoc->strmin[i].delivery_started = 0;
420 * load_address_from_init will put the addresses into the
421 * association when the COOKIE is processed or the INIT-ACK is
422 * processed. Both types of COOKIE's existing and new call this
423 * routine. It will remove addresses that are no longer in the
424 * association (for the restarting case where addresses are
425 * removed). Up front when the INIT arrives we will discard it if it
426 * is a restart and new addresses have been added.
428 /* sa_ignore MEMLEAK */
433 * INIT-ACK message processing/consumption returns value < 0 on error
436 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
437 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
438 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
439 struct sctp_nets *net, int *abort_no_unlock,
440 uint8_t mflowtype, uint32_t mflowid,
443 struct sctp_association *asoc;
445 int retval, abort_flag;
446 uint32_t initack_limit;
447 int nat_friendly = 0;
449 /* First verify that we have no illegal param's */
452 op_err = sctp_arethere_unrecognized_parameters(m,
453 (offset + sizeof(struct sctp_init_chunk)),
454 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
456 /* Send an abort and notify peer */
457 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
458 *abort_no_unlock = 1;
462 asoc->peer_supports_nat = (uint8_t)nat_friendly;
463 /* process the peer's parameters in the INIT-ACK */
464 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
468 initack_limit = offset + ntohs(cp->ch.chunk_length);
469 /* load all addresses */
470 if ((retval = sctp_load_addresses_from_init(stcb, m,
471 (offset + sizeof(struct sctp_init_chunk)), initack_limit,
472 src, dst, NULL, stcb->asoc.port))) {
473 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
474 "Problem with address parameters");
475 SCTPDBG(SCTP_DEBUG_INPUT1,
476 "Load addresses from INIT causes an abort %d\n",
478 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
479 src, dst, sh, op_err,
482 *abort_no_unlock = 1;
485 /* if the peer doesn't support asconf, flush the asconf queue */
486 if (asoc->asconf_supported == 0) {
487 struct sctp_asconf_addr *param, *nparam;
489 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
490 TAILQ_REMOVE(&asoc->asconf_queue, param, next);
491 SCTP_FREE(param, SCTP_M_ASC_ADDR);
494 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
495 stcb->asoc.local_hmacs);
497 sctp_queue_op_err(stcb, op_err);
498 /* queuing will steal away the mbuf chain to the out queue */
501 /* extract the cookie and queue it to "echo" it back... */
502 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
503 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
504 stcb->asoc.overall_error_count,
506 SCTP_FROM_SCTP_INPUT,
509 stcb->asoc.overall_error_count = 0;
510 net->error_count = 0;
513 * Cancel the INIT timer, We do this first before queueing the
514 * cookie. We always cancel at the primary to assue that we are
515 * canceling the timer started by the INIT which always goes to the
518 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
519 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
521 /* calculate the RTO */
522 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy,
523 SCTP_RTT_FROM_NON_DATA);
524 retval = sctp_send_cookie_echo(m, offset, stcb, net);
527 * No cookie, we probably should send a op error. But in any
528 * case if there is no cookie in the INIT-ACK, we can
529 * abandon the peer, its broke.
534 len = (uint16_t)(sizeof(struct sctp_error_missing_param) + sizeof(uint16_t));
535 /* We abort with an error of missing mandatory param */
536 op_err = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
537 if (op_err != NULL) {
538 struct sctp_error_missing_param *cause;
540 SCTP_BUF_LEN(op_err) = len;
541 cause = mtod(op_err, struct sctp_error_missing_param *);
542 /* Subtract the reserved param */
543 cause->cause.code = htons(SCTP_CAUSE_MISSING_PARAM);
544 cause->cause.length = htons(len);
545 cause->num_missing_params = htonl(1);
546 cause->type[0] = htons(SCTP_STATE_COOKIE);
548 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
549 src, dst, sh, op_err,
552 *abort_no_unlock = 1;
560 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
561 struct sctp_tcb *stcb, struct sctp_nets *net)
563 union sctp_sockstore store;
564 struct sctp_nets *r_net, *f_net;
567 uint16_t old_error_counter;
569 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
573 memset(&store, 0, sizeof(store));
574 switch (cp->heartbeat.hb_info.addr_family) {
577 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
578 store.sin.sin_family = cp->heartbeat.hb_info.addr_family;
579 store.sin.sin_len = cp->heartbeat.hb_info.addr_len;
580 store.sin.sin_port = stcb->rport;
581 memcpy(&store.sin.sin_addr, cp->heartbeat.hb_info.address,
582 sizeof(store.sin.sin_addr));
590 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
591 store.sin6.sin6_family = cp->heartbeat.hb_info.addr_family;
592 store.sin6.sin6_len = cp->heartbeat.hb_info.addr_len;
593 store.sin6.sin6_port = stcb->rport;
594 memcpy(&store.sin6.sin6_addr, cp->heartbeat.hb_info.address, sizeof(struct in6_addr));
603 r_net = sctp_findnet(stcb, &store.sa);
605 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
608 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
609 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
610 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
612 * If the its a HB and it's random value is correct when can
613 * confirm the destination.
615 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
616 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
617 stcb->asoc.primary_destination = r_net;
618 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
619 f_net = TAILQ_FIRST(&stcb->asoc.nets);
620 if (f_net != r_net) {
622 * first one on the list is NOT the primary
623 * sctp_cmpaddr() is much more efficient if
624 * the primary is the first on the list,
627 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
628 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
632 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
633 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
634 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
635 r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
636 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
638 old_error_counter = r_net->error_count;
639 r_net->error_count = 0;
640 r_net->hb_responded = 1;
641 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
642 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
643 /* Now lets do a RTO with this */
644 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy,
645 SCTP_RTT_FROM_NON_DATA);
646 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
647 r_net->dest_state |= SCTP_ADDR_REACHABLE;
648 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
649 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
651 if (r_net->dest_state & SCTP_ADDR_PF) {
652 r_net->dest_state &= ~SCTP_ADDR_PF;
653 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
655 if (old_error_counter > 0) {
656 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
657 stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
658 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
660 if (r_net == stcb->asoc.primary_destination) {
661 if (stcb->asoc.alternate) {
662 /* release the alternate, primary is good */
663 sctp_free_remote_addr(stcb->asoc.alternate);
664 stcb->asoc.alternate = NULL;
667 /* Mobility adaptation */
669 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
670 SCTP_MOBILITY_BASE) ||
671 sctp_is_mobility_feature_on(stcb->sctp_ep,
672 SCTP_MOBILITY_FASTHANDOFF)) &&
673 sctp_is_mobility_feature_on(stcb->sctp_ep,
674 SCTP_MOBILITY_PRIM_DELETED)) {
676 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
677 stcb->sctp_ep, stcb, NULL,
678 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
679 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
680 SCTP_MOBILITY_FASTHANDOFF)) {
681 sctp_assoc_immediate_retrans(stcb,
682 stcb->asoc.primary_destination);
684 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
685 SCTP_MOBILITY_BASE)) {
686 sctp_move_chunks_from_net(stcb,
687 stcb->asoc.deleted_primary);
689 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
690 stcb->asoc.deleted_primary);
696 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
699 * return 0 means we want you to proceed with the abort non-zero
700 * means no abort processing
702 struct sctpasochead *head;
704 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
705 /* generate a new vtag and send init */
706 LIST_REMOVE(stcb, sctp_asocs);
707 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
708 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
710 * put it in the bucket in the vtag hash of assoc's for the
713 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
714 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
717 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
719 * treat like a case where the cookie expired i.e.: - dump
720 * current cookie. - generate a new vtag. - resend init.
722 /* generate a new vtag and send init */
723 LIST_REMOVE(stcb, sctp_asocs);
724 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
725 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
726 sctp_stop_all_cookie_timers(stcb);
727 sctp_toss_old_cookies(stcb, &stcb->asoc);
728 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
729 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
731 * put it in the bucket in the vtag hash of assoc's for the
734 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
735 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
742 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
743 struct sctp_nets *net)
746 * return 0 means we want you to proceed with the abort non-zero
747 * means no abort processing
749 if (stcb->asoc.auth_supported == 0) {
750 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
753 sctp_asconf_send_nat_state_update(stcb, net);
759 sctp_handle_abort(struct sctp_abort_chunk *abort,
760 struct sctp_tcb *stcb, struct sctp_nets *net)
762 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
768 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
772 len = ntohs(abort->ch.chunk_length);
773 if (len > sizeof(struct sctp_chunkhdr)) {
775 * Need to check the cause codes for our two magic nat
776 * aborts which don't kill the assoc necessarily.
778 struct sctp_gen_error_cause *cause;
780 cause = (struct sctp_gen_error_cause *)(abort + 1);
781 error = ntohs(cause->code);
782 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
783 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
784 abort->ch.chunk_flags);
785 if (sctp_handle_nat_colliding_state(stcb)) {
788 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
789 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
790 abort->ch.chunk_flags);
791 if (sctp_handle_nat_missing_state(stcb, net)) {
798 /* stop any receive timers */
799 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net,
800 SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
801 /* notify user of the abort and clean up... */
802 sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
804 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
805 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
806 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
807 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
809 #ifdef SCTP_ASOCLOG_OF_TSNS
810 sctp_print_out_track_log(stcb);
812 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
813 so = SCTP_INP_SO(stcb->sctp_ep);
814 atomic_add_int(&stcb->asoc.refcnt, 1);
815 SCTP_TCB_UNLOCK(stcb);
816 SCTP_SOCKET_LOCK(so, 1);
818 atomic_subtract_int(&stcb->asoc.refcnt, 1);
820 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
821 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
822 SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
823 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
824 SCTP_SOCKET_UNLOCK(so, 1);
826 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
830 sctp_start_net_timers(struct sctp_tcb *stcb)
832 uint32_t cnt_hb_sent;
833 struct sctp_nets *net;
836 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
838 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
839 * If the dest in unconfirmed send a hb as well if under
840 * max_hb_burst have been sent.
842 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
843 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
844 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
845 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
846 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
851 sctp_chunk_output(stcb->sctp_ep, stcb,
852 SCTP_OUTPUT_FROM_COOKIE_ACK,
859 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
860 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
862 struct sctp_association *asoc;
863 int some_on_streamwheel;
865 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
869 SCTPDBG(SCTP_DEBUG_INPUT2,
870 "sctp_handle_shutdown: handling SHUTDOWN\n");
874 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
875 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
878 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
879 /* Shutdown NOT the expected size */
882 old_state = SCTP_GET_STATE(asoc);
883 sctp_update_acked(stcb, cp, abort_flag);
887 if (asoc->control_pdapi) {
889 * With a normal shutdown we assume the end of last record.
891 SCTP_INP_READ_LOCK(stcb->sctp_ep);
892 if (asoc->control_pdapi->on_strm_q) {
893 struct sctp_stream_in *strm;
895 strm = &asoc->strmin[asoc->control_pdapi->sinfo_stream];
896 if (asoc->control_pdapi->on_strm_q == SCTP_ON_UNORDERED) {
898 TAILQ_REMOVE(&strm->uno_inqueue, asoc->control_pdapi, next_instrm);
899 asoc->control_pdapi->on_strm_q = 0;
900 } else if (asoc->control_pdapi->on_strm_q == SCTP_ON_ORDERED) {
902 TAILQ_REMOVE(&strm->inqueue, asoc->control_pdapi, next_instrm);
903 asoc->control_pdapi->on_strm_q = 0;
906 panic("Unknown state on ctrl:%p on_strm_q:%d",
908 asoc->control_pdapi->on_strm_q);
912 asoc->control_pdapi->end_added = 1;
913 asoc->control_pdapi->pdapi_aborted = 1;
914 asoc->control_pdapi = NULL;
915 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
916 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
917 so = SCTP_INP_SO(stcb->sctp_ep);
918 atomic_add_int(&stcb->asoc.refcnt, 1);
919 SCTP_TCB_UNLOCK(stcb);
920 SCTP_SOCKET_LOCK(so, 1);
922 atomic_subtract_int(&stcb->asoc.refcnt, 1);
923 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
924 /* assoc was freed while we were unlocked */
925 SCTP_SOCKET_UNLOCK(so, 1);
929 if (stcb->sctp_socket) {
930 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
932 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
933 SCTP_SOCKET_UNLOCK(so, 1);
936 /* goto SHUTDOWN_RECEIVED state to block new requests */
937 if (stcb->sctp_socket) {
938 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
939 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
940 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
941 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
942 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
944 * notify upper layer that peer has initiated a
947 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
950 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
953 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
955 * stop the shutdown timer, since we WILL move to
958 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
959 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
961 /* Now is there unsent data on a stream somewhere? */
962 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
964 if (!TAILQ_EMPTY(&asoc->send_queue) ||
965 !TAILQ_EMPTY(&asoc->sent_queue) ||
966 some_on_streamwheel) {
967 /* By returning we will push more data out */
970 /* no outstanding data to send, so move on... */
971 /* send SHUTDOWN-ACK */
972 /* move to SHUTDOWN-ACK-SENT state */
973 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
974 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
975 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
977 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
978 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
979 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
980 sctp_stop_timers_for_shutdown(stcb);
981 sctp_send_shutdown_ack(stcb, net);
982 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
983 stcb->sctp_ep, stcb, net);
984 } else if (old_state == SCTP_STATE_SHUTDOWN_ACK_SENT) {
985 sctp_send_shutdown_ack(stcb, net);
991 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
992 struct sctp_tcb *stcb,
993 struct sctp_nets *net)
995 struct sctp_association *asoc;
996 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
999 so = SCTP_INP_SO(stcb->sctp_ep);
1001 SCTPDBG(SCTP_DEBUG_INPUT2,
1002 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
1007 /* process according to association state */
1008 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
1009 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
1010 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
1011 sctp_send_shutdown_complete(stcb, net, 1);
1012 SCTP_TCB_UNLOCK(stcb);
1015 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
1016 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1017 /* unexpected SHUTDOWN-ACK... so ignore... */
1018 SCTP_TCB_UNLOCK(stcb);
1021 if (asoc->control_pdapi) {
1023 * With a normal shutdown we assume the end of last record.
1025 SCTP_INP_READ_LOCK(stcb->sctp_ep);
1026 asoc->control_pdapi->end_added = 1;
1027 asoc->control_pdapi->pdapi_aborted = 1;
1028 asoc->control_pdapi = NULL;
1029 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1030 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1031 atomic_add_int(&stcb->asoc.refcnt, 1);
1032 SCTP_TCB_UNLOCK(stcb);
1033 SCTP_SOCKET_LOCK(so, 1);
1034 SCTP_TCB_LOCK(stcb);
1035 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1036 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1037 /* assoc was freed while we were unlocked */
1038 SCTP_SOCKET_UNLOCK(so, 1);
1042 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1043 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1044 SCTP_SOCKET_UNLOCK(so, 1);
1048 if (!TAILQ_EMPTY(&asoc->send_queue) ||
1049 !TAILQ_EMPTY(&asoc->sent_queue) ||
1050 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
1051 panic("Queues are not empty when handling SHUTDOWN-ACK");
1054 /* stop the timer */
1055 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net,
1056 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1057 /* send SHUTDOWN-COMPLETE */
1058 sctp_send_shutdown_complete(stcb, net, 0);
1059 /* notify upper layer protocol */
1060 if (stcb->sctp_socket) {
1061 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1062 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1063 stcb->sctp_socket->so_snd.sb_cc = 0;
1065 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1067 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1068 /* free the TCB but first save off the ep */
1069 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1070 atomic_add_int(&stcb->asoc.refcnt, 1);
1071 SCTP_TCB_UNLOCK(stcb);
1072 SCTP_SOCKET_LOCK(so, 1);
1073 SCTP_TCB_LOCK(stcb);
1074 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1076 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1077 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1078 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1079 SCTP_SOCKET_UNLOCK(so, 1);
1084 * Skip past the param header and then we will find the chunk that caused the
1085 * problem. There are two possibilities ASCONF or FWD-TSN other than that and
1086 * our peer must be broken.
1089 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1090 struct sctp_nets *net)
1092 struct sctp_chunkhdr *chk;
1094 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1095 switch (chk->chunk_type) {
1096 case SCTP_ASCONF_ACK:
1098 sctp_asconf_cleanup(stcb, net);
1100 case SCTP_IFORWARD_CUM_TSN:
1101 case SCTP_FORWARD_CUM_TSN:
1102 stcb->asoc.prsctp_supported = 0;
1105 SCTPDBG(SCTP_DEBUG_INPUT2,
1106 "Peer does not support chunk type %d(%x)??\n",
1107 chk->chunk_type, (uint32_t)chk->chunk_type);
1113 * Skip past the param header and then we will find the param that caused the
1114 * problem. There are a number of param's in a ASCONF OR the prsctp param
1115 * these will turn of specific features.
1116 * XXX: Is this the right thing to do?
1119 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1121 struct sctp_paramhdr *pbad;
1124 switch (ntohs(pbad->param_type)) {
1126 case SCTP_PRSCTP_SUPPORTED:
1127 stcb->asoc.prsctp_supported = 0;
1129 case SCTP_SUPPORTED_CHUNK_EXT:
1131 /* draft-ietf-tsvwg-addip-sctp */
1132 case SCTP_HAS_NAT_SUPPORT:
1133 stcb->asoc.peer_supports_nat = 0;
1135 case SCTP_ADD_IP_ADDRESS:
1136 case SCTP_DEL_IP_ADDRESS:
1137 case SCTP_SET_PRIM_ADDR:
1138 stcb->asoc.asconf_supported = 0;
1140 case SCTP_SUCCESS_REPORT:
1141 case SCTP_ERROR_CAUSE_IND:
1142 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1143 SCTPDBG(SCTP_DEBUG_INPUT2,
1144 "Turning off ASCONF to this strange peer\n");
1145 stcb->asoc.asconf_supported = 0;
1148 SCTPDBG(SCTP_DEBUG_INPUT2,
1149 "Peer does not support param type %d(%x)??\n",
1150 pbad->param_type, (uint32_t)pbad->param_type);
1156 sctp_handle_error(struct sctp_chunkhdr *ch,
1157 struct sctp_tcb *stcb, struct sctp_nets *net)
1160 struct sctp_paramhdr *phdr;
1161 uint16_t error, error_type;
1163 struct sctp_association *asoc;
1165 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1169 /* parse through all of the errors and process */
1171 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1172 sizeof(struct sctp_chunkhdr));
1173 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1175 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1176 /* Process an Error Cause */
1177 error_type = ntohs(phdr->param_type);
1178 error_len = ntohs(phdr->param_length);
1179 if ((error_len > chklen) || (error_len == 0)) {
1180 /* invalid param length for this param */
1181 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1186 /* report the first error cause */
1189 switch (error_type) {
1190 case SCTP_CAUSE_INVALID_STREAM:
1191 case SCTP_CAUSE_MISSING_PARAM:
1192 case SCTP_CAUSE_INVALID_PARAM:
1193 case SCTP_CAUSE_NO_USER_DATA:
1194 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1197 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1198 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1200 if (sctp_handle_nat_colliding_state(stcb)) {
1204 case SCTP_CAUSE_NAT_MISSING_STATE:
1205 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1207 if (sctp_handle_nat_missing_state(stcb, net)) {
1211 case SCTP_CAUSE_STALE_COOKIE:
1213 * We only act if we have echoed a cookie and are
1216 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1219 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1220 /* Save the time doubled */
1221 asoc->cookie_preserve_req = ntohl(*p) << 1;
1222 asoc->stale_cookie_count++;
1223 if (asoc->stale_cookie_count >
1224 asoc->max_init_times) {
1225 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1226 /* now free the asoc */
1227 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1228 so = SCTP_INP_SO(stcb->sctp_ep);
1229 atomic_add_int(&stcb->asoc.refcnt, 1);
1230 SCTP_TCB_UNLOCK(stcb);
1231 SCTP_SOCKET_LOCK(so, 1);
1232 SCTP_TCB_LOCK(stcb);
1233 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1235 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1236 SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1237 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1238 SCTP_SOCKET_UNLOCK(so, 1);
1242 /* blast back to INIT state */
1243 sctp_toss_old_cookies(stcb, &stcb->asoc);
1244 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1245 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1246 sctp_stop_all_cookie_timers(stcb);
1247 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1250 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1252 * Nothing we can do here, we don't do hostname
1253 * addresses so if the peer does not like my IPv6
1254 * (or IPv4 for that matter) it does not matter. If
1255 * they don't support that type of address, they can
1256 * NOT possibly get that packet type... i.e. with no
1257 * IPv6 you can't receive a IPv6 packet. so we can
1258 * safely ignore this one. If we ever added support
1259 * for HOSTNAME Addresses, then we would need to do
1263 case SCTP_CAUSE_UNRECOG_CHUNK:
1264 sctp_process_unrecog_chunk(stcb, phdr, net);
1266 case SCTP_CAUSE_UNRECOG_PARAM:
1267 sctp_process_unrecog_param(stcb, phdr);
1269 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1271 * We ignore this since the timer will drive out a
1272 * new cookie anyway and there timer will drive us
1273 * to send a SHUTDOWN_COMPLETE. We can't send one
1274 * here since we don't have their tag.
1277 case SCTP_CAUSE_DELETING_LAST_ADDR:
1278 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1279 case SCTP_CAUSE_DELETING_SRC_ADDR:
1281 * We should NOT get these here, but in a
1284 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1287 case SCTP_CAUSE_OUT_OF_RESC:
1289 * And what, pray tell do we do with the fact that
1290 * the peer is out of resources? Not really sure we
1291 * could do anything but abort. I suspect this
1292 * should have came WITH an abort instead of in a
1297 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1301 adjust = SCTP_SIZE32(error_len);
1303 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1305 sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, error, ch, SCTP_SO_NOT_LOCKED);
1310 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1311 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1312 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1313 struct sctp_nets *net, int *abort_no_unlock,
1314 uint8_t mflowtype, uint32_t mflowid,
1317 struct sctp_init_ack *init_ack;
1318 struct mbuf *op_err;
1320 SCTPDBG(SCTP_DEBUG_INPUT2,
1321 "sctp_handle_init_ack: handling INIT-ACK\n");
1324 SCTPDBG(SCTP_DEBUG_INPUT2,
1325 "sctp_handle_init_ack: TCB is null\n");
1328 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1329 /* Invalid length */
1330 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1331 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1332 src, dst, sh, op_err,
1335 *abort_no_unlock = 1;
1338 init_ack = &cp->init;
1339 /* validate parameters */
1340 if (init_ack->initiate_tag == 0) {
1341 /* protocol error... send an abort */
1342 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1343 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1344 src, dst, sh, op_err,
1347 *abort_no_unlock = 1;
1350 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1351 /* protocol error... send an abort */
1352 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1353 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1354 src, dst, sh, op_err,
1357 *abort_no_unlock = 1;
1360 if (init_ack->num_inbound_streams == 0) {
1361 /* protocol error... send an abort */
1362 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1363 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1364 src, dst, sh, op_err,
1367 *abort_no_unlock = 1;
1370 if (init_ack->num_outbound_streams == 0) {
1371 /* protocol error... send an abort */
1372 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1373 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1374 src, dst, sh, op_err,
1377 *abort_no_unlock = 1;
1380 /* process according to association state... */
1381 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1382 case SCTP_STATE_COOKIE_WAIT:
1383 /* this is the expected state for this chunk */
1384 /* process the INIT-ACK parameters */
1385 if (stcb->asoc.primary_destination->dest_state &
1386 SCTP_ADDR_UNCONFIRMED) {
1388 * The primary is where we sent the INIT, we can
1389 * always consider it confirmed when the INIT-ACK is
1390 * returned. Do this before we load addresses
1393 stcb->asoc.primary_destination->dest_state &=
1394 ~SCTP_ADDR_UNCONFIRMED;
1395 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1396 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1398 if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1399 net, abort_no_unlock,
1402 /* error in parsing parameters */
1405 /* update our state */
1406 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1407 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1409 /* reset the RTO calc */
1410 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1411 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1412 stcb->asoc.overall_error_count,
1414 SCTP_FROM_SCTP_INPUT,
1417 stcb->asoc.overall_error_count = 0;
1418 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1420 * collapse the init timer back in case of a exponential
1423 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1426 * the send at the end of the inbound data processing will
1427 * cause the cookie to be sent
1430 case SCTP_STATE_SHUTDOWN_SENT:
1431 /* incorrect state... discard */
1433 case SCTP_STATE_COOKIE_ECHOED:
1434 /* incorrect state... discard */
1436 case SCTP_STATE_OPEN:
1437 /* incorrect state... discard */
1439 case SCTP_STATE_EMPTY:
1440 case SCTP_STATE_INUSE:
1442 /* incorrect state... discard */
1446 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1450 static struct sctp_tcb *
1451 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1452 struct sockaddr *src, struct sockaddr *dst,
1453 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1454 struct sctp_inpcb *inp, struct sctp_nets **netp,
1455 struct sockaddr *init_src, int *notification,
1456 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1457 uint8_t mflowtype, uint32_t mflowid,
1458 uint32_t vrf_id, uint16_t port);
1462 * handle a state cookie for an existing association m: input packet mbuf
1463 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1464 * "split" mbuf and the cookie signature does not exist offset: offset into
1465 * mbuf to the cookie-echo chunk
1467 static struct sctp_tcb *
1468 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1469 struct sockaddr *src, struct sockaddr *dst,
1470 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1471 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1472 struct sockaddr *init_src, int *notification,
1473 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1474 uint8_t mflowtype, uint32_t mflowid,
1475 uint32_t vrf_id, uint16_t port)
1477 struct sctp_association *asoc;
1478 struct sctp_init_chunk *init_cp, init_buf;
1479 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1480 struct sctp_nets *net;
1481 struct mbuf *op_err;
1482 int init_offset, initack_offset, i;
1486 #if defined(SCTP_DETAILED_STR_STATS)
1491 /* I know that the TCB is non-NULL from the caller */
1493 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1494 if (asoc->cookie_how[how_indx] == 0)
1497 if (how_indx < sizeof(asoc->cookie_how)) {
1498 asoc->cookie_how[how_indx] = 1;
1500 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1501 /* SHUTDOWN came in after sending INIT-ACK */
1502 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1503 op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
1504 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1505 mflowtype, mflowid, inp->fibnum,
1507 if (how_indx < sizeof(asoc->cookie_how))
1508 asoc->cookie_how[how_indx] = 2;
1512 * find and validate the INIT chunk in the cookie (peer's info) the
1513 * INIT should start after the cookie-echo header struct (chunk
1514 * header, state cookie header struct)
1516 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1518 init_cp = (struct sctp_init_chunk *)
1519 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1520 (uint8_t *)&init_buf);
1521 if (init_cp == NULL) {
1522 /* could not pull a INIT chunk in cookie */
1525 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1529 * find and validate the INIT-ACK chunk in the cookie (my info) the
1530 * INIT-ACK follows the INIT chunk
1532 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1533 initack_cp = (struct sctp_init_ack_chunk *)
1534 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1535 (uint8_t *)&initack_buf);
1536 if (initack_cp == NULL) {
1537 /* could not pull INIT-ACK chunk in cookie */
1540 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1543 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1544 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1546 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1547 * to get into the OPEN state
1549 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1551 * Opps, this means that we somehow generated two vtag's
1552 * the same. I.e. we did:
1554 * <---INIT(tag=a)------
1555 * ----INIT-ACK(tag=t)-->
1556 * ----INIT(tag=t)------> *1
1557 * <---INIT-ACK(tag=a)---
1558 * <----CE(tag=t)------------- *2
1560 * At point *1 we should be generating a different
1561 * tag t'. Which means we would throw away the CE and send
1562 * ours instead. Basically this is case C (throw away side).
1564 if (how_indx < sizeof(asoc->cookie_how))
1565 asoc->cookie_how[how_indx] = 17;
1569 switch (SCTP_GET_STATE(asoc)) {
1570 case SCTP_STATE_COOKIE_WAIT:
1571 case SCTP_STATE_COOKIE_ECHOED:
1573 * INIT was sent but got a COOKIE_ECHO with the
1574 * correct tags... just accept it...but we must
1575 * process the init so that we can make sure we have
1576 * the right seq no's.
1578 /* First we must process the INIT !! */
1579 retval = sctp_process_init(init_cp, stcb);
1581 if (how_indx < sizeof(asoc->cookie_how))
1582 asoc->cookie_how[how_indx] = 3;
1585 /* we have already processed the INIT so no problem */
1586 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp,
1588 SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1589 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp,
1591 SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1592 /* update current state */
1593 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1594 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1596 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1598 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1599 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1600 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1601 stcb->sctp_ep, stcb, asoc->primary_destination);
1603 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1604 sctp_stop_all_cookie_timers(stcb);
1605 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1606 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1607 (inp->sctp_socket->so_qlimit == 0)
1609 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1613 * Here is where collision would go if we
1614 * did a connect() and instead got a
1615 * init/init-ack/cookie done before the
1616 * init-ack came back..
1618 stcb->sctp_ep->sctp_flags |=
1619 SCTP_PCB_FLAGS_CONNECTED;
1620 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1621 so = SCTP_INP_SO(stcb->sctp_ep);
1622 atomic_add_int(&stcb->asoc.refcnt, 1);
1623 SCTP_TCB_UNLOCK(stcb);
1624 SCTP_SOCKET_LOCK(so, 1);
1625 SCTP_TCB_LOCK(stcb);
1626 atomic_add_int(&stcb->asoc.refcnt, -1);
1627 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1628 SCTP_SOCKET_UNLOCK(so, 1);
1632 soisconnected(stcb->sctp_socket);
1633 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1634 SCTP_SOCKET_UNLOCK(so, 1);
1637 /* notify upper layer */
1638 *notification = SCTP_NOTIFY_ASSOC_UP;
1640 * since we did not send a HB make sure we don't
1643 net->hb_responded = 1;
1644 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1645 &cookie->time_entered,
1646 sctp_align_unsafe_makecopy,
1647 SCTP_RTT_FROM_NON_DATA);
1649 if (stcb->asoc.sctp_autoclose_ticks &&
1650 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1651 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1657 * we're in the OPEN state (or beyond), so peer must
1658 * have simply lost the COOKIE-ACK
1662 sctp_stop_all_cookie_timers(stcb);
1664 * We ignore the return code here.. not sure if we should
1665 * somehow abort.. but we do have an existing asoc. This
1666 * really should not fail.
1668 if (sctp_load_addresses_from_init(stcb, m,
1669 init_offset + sizeof(struct sctp_init_chunk),
1670 initack_offset, src, dst, init_src, stcb->asoc.port)) {
1671 if (how_indx < sizeof(asoc->cookie_how))
1672 asoc->cookie_how[how_indx] = 4;
1675 /* respond with a COOKIE-ACK */
1676 sctp_toss_old_cookies(stcb, asoc);
1677 sctp_send_cookie_ack(stcb);
1678 if (how_indx < sizeof(asoc->cookie_how))
1679 asoc->cookie_how[how_indx] = 5;
1682 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1683 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1684 cookie->tie_tag_my_vtag == 0 &&
1685 cookie->tie_tag_peer_vtag == 0) {
1687 * case C in Section 5.2.4 Table 2: XMOO silently discard
1689 if (how_indx < sizeof(asoc->cookie_how))
1690 asoc->cookie_how[how_indx] = 6;
1694 * If nat support, and the below and stcb is established, send back
1695 * a ABORT(colliding state) if we are established.
1697 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1698 (asoc->peer_supports_nat) &&
1699 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1700 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1701 (asoc->peer_vtag == 0)))) {
1703 * Special case - Peer's support nat. We may have two init's
1704 * that we gave out the same tag on since one was not
1705 * established.. i.e. we get INIT from host-1 behind the nat
1706 * and we respond tag-a, we get a INIT from host-2 behind
1707 * the nat and we get tag-a again. Then we bring up host-1
1708 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1709 * Now we have colliding state. We must send an abort here
1710 * with colliding state indication.
1712 op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
1713 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1714 mflowtype, mflowid, inp->fibnum,
1718 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1719 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1720 (asoc->peer_vtag == 0))) {
1722 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1723 * should be ok, re-accept peer info
1725 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1727 * Extension of case C. If we hit this, then the
1728 * random number generator returned the same vtag
1729 * when we first sent our INIT-ACK and when we later
1730 * sent our INIT. The side with the seq numbers that
1731 * are different will be the one that normnally
1732 * would have hit case C. This in effect "extends"
1733 * our vtags in this collision case to be 64 bits.
1734 * The same collision could occur aka you get both
1735 * vtag and seq number the same twice in a row.. but
1736 * is much less likely. If it did happen then we
1737 * would proceed through and bring up the assoc.. we
1738 * may end up with the wrong stream setup however..
1739 * which would be bad.. but there is no way to
1740 * tell.. until we send on a stream that does not
1743 if (how_indx < sizeof(asoc->cookie_how))
1744 asoc->cookie_how[how_indx] = 7;
1748 if (how_indx < sizeof(asoc->cookie_how))
1749 asoc->cookie_how[how_indx] = 8;
1750 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1751 SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1752 sctp_stop_all_cookie_timers(stcb);
1754 * since we did not send a HB make sure we don't double
1757 net->hb_responded = 1;
1758 if (stcb->asoc.sctp_autoclose_ticks &&
1759 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1760 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1763 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1764 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1766 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1768 * Ok the peer probably discarded our data (if we
1769 * echoed a cookie+data). So anything on the
1770 * sent_queue should be marked for retransmit, we
1771 * may not get something to kick us so it COULD
1772 * still take a timeout to move these.. but it can't
1773 * hurt to mark them.
1775 struct sctp_tmit_chunk *chk;
1777 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1778 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1779 chk->sent = SCTP_DATAGRAM_RESEND;
1780 sctp_flight_size_decrease(chk);
1781 sctp_total_flight_decrease(stcb, chk);
1782 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1788 /* process the INIT info (peer's info) */
1789 retval = sctp_process_init(init_cp, stcb);
1791 if (how_indx < sizeof(asoc->cookie_how))
1792 asoc->cookie_how[how_indx] = 9;
1795 if (sctp_load_addresses_from_init(stcb, m,
1796 init_offset + sizeof(struct sctp_init_chunk),
1797 initack_offset, src, dst, init_src, stcb->asoc.port)) {
1798 if (how_indx < sizeof(asoc->cookie_how))
1799 asoc->cookie_how[how_indx] = 10;
1802 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1803 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1804 *notification = SCTP_NOTIFY_ASSOC_UP;
1806 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1807 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1808 (inp->sctp_socket->so_qlimit == 0)) {
1809 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1812 stcb->sctp_ep->sctp_flags |=
1813 SCTP_PCB_FLAGS_CONNECTED;
1814 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1815 so = SCTP_INP_SO(stcb->sctp_ep);
1816 atomic_add_int(&stcb->asoc.refcnt, 1);
1817 SCTP_TCB_UNLOCK(stcb);
1818 SCTP_SOCKET_LOCK(so, 1);
1819 SCTP_TCB_LOCK(stcb);
1820 atomic_add_int(&stcb->asoc.refcnt, -1);
1821 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1822 SCTP_SOCKET_UNLOCK(so, 1);
1826 soisconnected(stcb->sctp_socket);
1827 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1828 SCTP_SOCKET_UNLOCK(so, 1);
1831 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1832 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1834 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1835 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1836 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1837 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1839 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1841 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1842 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1843 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1844 stcb->sctp_ep, stcb, asoc->primary_destination);
1846 sctp_stop_all_cookie_timers(stcb);
1847 sctp_toss_old_cookies(stcb, asoc);
1848 sctp_send_cookie_ack(stcb);
1851 * only if we have retrans set do we do this. What
1852 * this call does is get only the COOKIE-ACK out and
1853 * then when we return the normal call to
1854 * sctp_chunk_output will get the retrans out behind
1857 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1859 if (how_indx < sizeof(asoc->cookie_how))
1860 asoc->cookie_how[how_indx] = 11;
1864 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1865 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1866 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1867 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1868 cookie->tie_tag_peer_vtag != 0) {
1869 struct sctpasochead *head;
1870 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1874 if (asoc->peer_supports_nat) {
1876 * This is a gross gross hack. Just call the
1877 * cookie_new code since we are allowing a duplicate
1878 * association. I hope this works...
1880 return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
1881 sh, cookie, cookie_len,
1882 inp, netp, init_src, notification,
1883 auth_skipped, auth_offset, auth_len,
1888 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1891 if (how_indx < sizeof(asoc->cookie_how))
1892 asoc->cookie_how[how_indx] = 12;
1893 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
1894 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1895 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1896 SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
1898 /* notify upper layer */
1899 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1900 atomic_add_int(&stcb->asoc.refcnt, 1);
1901 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1902 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1903 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1904 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1906 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1907 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1908 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1909 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1911 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1912 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1913 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1914 stcb->sctp_ep, stcb, asoc->primary_destination);
1916 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1917 /* move to OPEN state, if not in SHUTDOWN_SENT */
1918 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1920 asoc->pre_open_streams =
1921 ntohs(initack_cp->init.num_outbound_streams);
1922 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1923 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1924 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1926 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1928 asoc->str_reset_seq_in = asoc->init_seq_number;
1930 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1931 if (asoc->mapping_array) {
1932 memset(asoc->mapping_array, 0,
1933 asoc->mapping_array_size);
1935 if (asoc->nr_mapping_array) {
1936 memset(asoc->nr_mapping_array, 0,
1937 asoc->mapping_array_size);
1939 SCTP_TCB_UNLOCK(stcb);
1940 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1941 so = SCTP_INP_SO(stcb->sctp_ep);
1942 SCTP_SOCKET_LOCK(so, 1);
1944 SCTP_INP_INFO_WLOCK();
1945 SCTP_INP_WLOCK(stcb->sctp_ep);
1946 SCTP_TCB_LOCK(stcb);
1947 atomic_add_int(&stcb->asoc.refcnt, -1);
1948 /* send up all the data */
1949 SCTP_TCB_SEND_LOCK(stcb);
1951 sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_LOCKED);
1952 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1953 stcb->asoc.strmout[i].chunks_on_queues = 0;
1954 #if defined(SCTP_DETAILED_STR_STATS)
1955 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1956 asoc->strmout[i].abandoned_sent[j] = 0;
1957 asoc->strmout[i].abandoned_unsent[j] = 0;
1960 asoc->strmout[i].abandoned_sent[0] = 0;
1961 asoc->strmout[i].abandoned_unsent[0] = 0;
1963 stcb->asoc.strmout[i].sid = i;
1964 stcb->asoc.strmout[i].next_mid_ordered = 0;
1965 stcb->asoc.strmout[i].next_mid_unordered = 0;
1966 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1968 /* process the INIT-ACK info (my info) */
1969 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1970 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1972 /* pull from vtag hash */
1973 LIST_REMOVE(stcb, sctp_asocs);
1974 /* re-insert to new vtag position */
1975 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1976 SCTP_BASE_INFO(hashasocmark))];
1978 * put it in the bucket in the vtag hash of assoc's for the
1981 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1983 SCTP_TCB_SEND_UNLOCK(stcb);
1984 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1985 SCTP_INP_INFO_WUNLOCK();
1986 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1987 SCTP_SOCKET_UNLOCK(so, 1);
1989 asoc->total_flight = 0;
1990 asoc->total_flight_count = 0;
1991 /* process the INIT info (peer's info) */
1992 retval = sctp_process_init(init_cp, stcb);
1994 if (how_indx < sizeof(asoc->cookie_how))
1995 asoc->cookie_how[how_indx] = 13;
2000 * since we did not send a HB make sure we don't double
2003 net->hb_responded = 1;
2005 if (sctp_load_addresses_from_init(stcb, m,
2006 init_offset + sizeof(struct sctp_init_chunk),
2007 initack_offset, src, dst, init_src, stcb->asoc.port)) {
2008 if (how_indx < sizeof(asoc->cookie_how))
2009 asoc->cookie_how[how_indx] = 14;
2013 /* respond with a COOKIE-ACK */
2014 sctp_stop_all_cookie_timers(stcb);
2015 sctp_toss_old_cookies(stcb, asoc);
2016 sctp_send_cookie_ack(stcb);
2017 if (how_indx < sizeof(asoc->cookie_how))
2018 asoc->cookie_how[how_indx] = 15;
2022 if (how_indx < sizeof(asoc->cookie_how))
2023 asoc->cookie_how[how_indx] = 16;
2024 /* all other cases... */
2030 * handle a state cookie for a new association m: input packet mbuf chain--
2031 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
2032 * and the cookie signature does not exist offset: offset into mbuf to the
2033 * cookie-echo chunk length: length of the cookie chunk to: where the init
2034 * was from returns a new TCB
2036 static struct sctp_tcb *
2037 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
2038 struct sockaddr *src, struct sockaddr *dst,
2039 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
2040 struct sctp_inpcb *inp, struct sctp_nets **netp,
2041 struct sockaddr *init_src, int *notification,
2042 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2043 uint8_t mflowtype, uint32_t mflowid,
2044 uint32_t vrf_id, uint16_t port)
2046 struct sctp_tcb *stcb;
2047 struct sctp_init_chunk *init_cp, init_buf;
2048 struct sctp_init_ack_chunk *initack_cp, initack_buf;
2049 union sctp_sockstore store;
2050 struct sctp_association *asoc;
2051 int init_offset, initack_offset, initack_limit;
2054 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
2055 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2058 so = SCTP_INP_SO(inp);
2062 * find and validate the INIT chunk in the cookie (peer's info) the
2063 * INIT should start after the cookie-echo header struct (chunk
2064 * header, state cookie header struct)
2066 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2067 init_cp = (struct sctp_init_chunk *)
2068 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2069 (uint8_t *)&init_buf);
2070 if (init_cp == NULL) {
2071 /* could not pull a INIT chunk in cookie */
2072 SCTPDBG(SCTP_DEBUG_INPUT1,
2073 "process_cookie_new: could not pull INIT chunk hdr\n");
2076 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2077 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2080 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2082 * find and validate the INIT-ACK chunk in the cookie (my info) the
2083 * INIT-ACK follows the INIT chunk
2085 initack_cp = (struct sctp_init_ack_chunk *)
2086 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2087 (uint8_t *)&initack_buf);
2088 if (initack_cp == NULL) {
2089 /* could not pull INIT-ACK chunk in cookie */
2090 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2093 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2097 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2098 * "initack_limit" value. This is because the chk_length field
2099 * includes the length of the cookie, but the cookie is omitted when
2100 * the INIT and INIT_ACK are tacked onto the cookie...
2102 initack_limit = offset + cookie_len;
2105 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2110 * Here we do a trick, we set in NULL for the proc/thread argument.
2111 * We do this since in effect we only use the p argument when the
2112 * socket is unbound and we must do an implicit bind. Since we are
2113 * getting a cookie, we cannot be unbound.
2115 stcb = sctp_aloc_assoc(inp, init_src, &error,
2116 ntohl(initack_cp->init.initiate_tag), vrf_id,
2117 ntohs(initack_cp->init.num_outbound_streams),
2119 (struct thread *)NULL
2122 struct mbuf *op_err;
2124 /* memory problem? */
2125 SCTPDBG(SCTP_DEBUG_INPUT1,
2126 "process_cookie_new: no room for another TCB!\n");
2127 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2128 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2129 src, dst, sh, op_err,
2134 /* get the correct sctp_nets */
2136 *netp = sctp_findnet(stcb, init_src);
2139 /* get scope variables out of cookie */
2140 asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2141 asoc->scope.site_scope = cookie->site_scope;
2142 asoc->scope.local_scope = cookie->local_scope;
2143 asoc->scope.loopback_scope = cookie->loopback_scope;
2145 if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2146 (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2147 struct mbuf *op_err;
2150 * Houston we have a problem. The EP changed while the
2151 * cookie was in flight. Only recourse is to abort the
2154 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2155 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2156 src, dst, sh, op_err,
2159 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2160 atomic_add_int(&stcb->asoc.refcnt, 1);
2161 SCTP_TCB_UNLOCK(stcb);
2162 SCTP_SOCKET_LOCK(so, 1);
2163 SCTP_TCB_LOCK(stcb);
2164 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2166 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2167 SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2168 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2169 SCTP_SOCKET_UNLOCK(so, 1);
2173 /* process the INIT-ACK info (my info) */
2174 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2175 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2176 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2177 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2178 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2179 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2180 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2181 asoc->str_reset_seq_in = asoc->init_seq_number;
2183 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2185 /* process the INIT info (peer's info) */
2187 retval = sctp_process_init(init_cp, stcb);
2191 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2192 atomic_add_int(&stcb->asoc.refcnt, 1);
2193 SCTP_TCB_UNLOCK(stcb);
2194 SCTP_SOCKET_LOCK(so, 1);
2195 SCTP_TCB_LOCK(stcb);
2196 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2198 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2199 SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2200 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2201 SCTP_SOCKET_UNLOCK(so, 1);
2205 /* load all addresses */
2206 if (sctp_load_addresses_from_init(stcb, m,
2207 init_offset + sizeof(struct sctp_init_chunk), initack_offset,
2208 src, dst, init_src, port)) {
2209 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2210 atomic_add_int(&stcb->asoc.refcnt, 1);
2211 SCTP_TCB_UNLOCK(stcb);
2212 SCTP_SOCKET_LOCK(so, 1);
2213 SCTP_TCB_LOCK(stcb);
2214 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2216 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2217 SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2218 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2219 SCTP_SOCKET_UNLOCK(so, 1);
2224 * verify any preceding AUTH chunk that was skipped
2226 /* pull the local authentication parameters from the cookie/init-ack */
2227 sctp_auth_get_cookie_params(stcb, m,
2228 initack_offset + sizeof(struct sctp_init_ack_chunk),
2229 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2231 struct sctp_auth_chunk *auth;
2233 auth = (struct sctp_auth_chunk *)
2234 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2235 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2236 /* auth HMAC failed, dump the assoc and packet */
2237 SCTPDBG(SCTP_DEBUG_AUTH1,
2238 "COOKIE-ECHO: AUTH failed\n");
2239 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2240 atomic_add_int(&stcb->asoc.refcnt, 1);
2241 SCTP_TCB_UNLOCK(stcb);
2242 SCTP_SOCKET_LOCK(so, 1);
2243 SCTP_TCB_LOCK(stcb);
2244 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2246 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2247 SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2248 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2249 SCTP_SOCKET_UNLOCK(so, 1);
2253 /* remaining chunks checked... good to go */
2254 stcb->asoc.authenticated = 1;
2258 * if we're doing ASCONFs, check to see if we have any new local
2259 * addresses that need to get added to the peer (eg. addresses
2260 * changed while cookie echo in flight). This needs to be done
2261 * after we go to the OPEN state to do the correct asconf
2262 * processing. else, make sure we have the correct addresses in our
2266 /* warning, we re-use sin, sin6, sa_store here! */
2267 /* pull in local_address (our "from" address) */
2268 switch (cookie->laddr_type) {
2270 case SCTP_IPV4_ADDRESS:
2271 /* source addr is IPv4 */
2272 memset(&store.sin, 0, sizeof(struct sockaddr_in));
2273 store.sin.sin_family = AF_INET;
2274 store.sin.sin_len = sizeof(struct sockaddr_in);
2275 store.sin.sin_addr.s_addr = cookie->laddress[0];
2279 case SCTP_IPV6_ADDRESS:
2280 /* source addr is IPv6 */
2281 memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
2282 store.sin6.sin6_family = AF_INET6;
2283 store.sin6.sin6_len = sizeof(struct sockaddr_in6);
2284 store.sin6.sin6_scope_id = cookie->scope_id;
2285 memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2289 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2290 atomic_add_int(&stcb->asoc.refcnt, 1);
2291 SCTP_TCB_UNLOCK(stcb);
2292 SCTP_SOCKET_LOCK(so, 1);
2293 SCTP_TCB_LOCK(stcb);
2294 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2296 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2297 SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2298 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2299 SCTP_SOCKET_UNLOCK(so, 1);
2304 /* update current state */
2305 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2306 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2307 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2308 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2309 stcb->sctp_ep, stcb, asoc->primary_destination);
2311 sctp_stop_all_cookie_timers(stcb);
2312 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2313 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2315 /* set up to notify upper layer */
2316 *notification = SCTP_NOTIFY_ASSOC_UP;
2317 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2318 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2319 (inp->sctp_socket->so_qlimit == 0)) {
2321 * This is an endpoint that called connect() how it got a
2322 * cookie that is NEW is a bit of a mystery. It must be that
2323 * the INIT was sent, but before it got there.. a complete
2324 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2325 * should have went to the other code.. not here.. oh well..
2326 * a bit of protection is worth having..
2328 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2329 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2330 atomic_add_int(&stcb->asoc.refcnt, 1);
2331 SCTP_TCB_UNLOCK(stcb);
2332 SCTP_SOCKET_LOCK(so, 1);
2333 SCTP_TCB_LOCK(stcb);
2334 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2335 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2336 SCTP_SOCKET_UNLOCK(so, 1);
2340 soisconnected(stcb->sctp_socket);
2341 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2342 SCTP_SOCKET_UNLOCK(so, 1);
2344 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2345 (inp->sctp_socket->so_qlimit)) {
2347 * We don't want to do anything with this one. Since it is
2348 * the listening guy. The timer will get started for
2349 * accepted connections in the caller.
2353 /* since we did not send a HB make sure we don't double things */
2354 if ((netp) && (*netp))
2355 (*netp)->hb_responded = 1;
2357 if (stcb->asoc.sctp_autoclose_ticks &&
2358 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2359 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2361 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2362 if ((netp != NULL) && (*netp != NULL)) {
2363 /* calculate the RTT and set the encaps port */
2364 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2365 &cookie->time_entered, sctp_align_unsafe_makecopy,
2366 SCTP_RTT_FROM_NON_DATA);
2368 /* respond with a COOKIE-ACK */
2369 sctp_send_cookie_ack(stcb);
2372 * check the address lists for any ASCONFs that need to be sent
2373 * AFTER the cookie-ack is sent
2375 sctp_check_address_list(stcb, m,
2376 initack_offset + sizeof(struct sctp_init_ack_chunk),
2377 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2378 &store.sa, cookie->local_scope, cookie->site_scope,
2379 cookie->ipv4_scope, cookie->loopback_scope);
2386 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2387 * we NEED to make sure we are not already using the vtag. If so we
2388 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2389 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2390 SCTP_BASE_INFO(hashasocmark))];
2391 LIST_FOREACH(stcb, head, sctp_asocs) {
2392 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2393 -- SEND ABORT - TRY AGAIN --
2399 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2400 * existing (non-NULL) TCB
2402 static struct mbuf *
2403 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2404 struct sockaddr *src, struct sockaddr *dst,
2405 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2406 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2407 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2408 struct sctp_tcb **locked_tcb,
2409 uint8_t mflowtype, uint32_t mflowid,
2410 uint32_t vrf_id, uint16_t port)
2412 struct sctp_state_cookie *cookie;
2413 struct sctp_tcb *l_stcb = *stcb;
2414 struct sctp_inpcb *l_inp;
2415 struct sockaddr *to;
2416 struct sctp_pcb *ep;
2418 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2420 uint8_t cookie_ok = 0;
2421 unsigned int sig_offset, cookie_offset;
2422 unsigned int cookie_len;
2424 struct timeval time_expires;
2425 int notification = 0;
2426 struct sctp_nets *netl;
2427 int had_a_existing_tcb = 0;
2428 int send_int_conf = 0;
2430 struct sockaddr_in sin;
2433 struct sockaddr_in6 sin6;
2436 SCTPDBG(SCTP_DEBUG_INPUT2,
2437 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2439 if (inp_p == NULL) {
2442 cookie = &cp->cookie;
2443 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2444 cookie_len = ntohs(cp->ch.chunk_length);
2446 if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2447 sizeof(struct sctp_init_chunk) +
2448 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2449 /* cookie too small */
2452 if ((cookie->peerport != sh->src_port) ||
2453 (cookie->myport != sh->dest_port) ||
2454 (cookie->my_vtag != sh->v_tag)) {
2456 * invalid ports or bad tag. Note that we always leave the
2457 * v_tag in the header in network order and when we stored
2458 * it in the my_vtag slot we also left it in network order.
2459 * This maintains the match even though it may be in the
2460 * opposite byte order of the machine :->
2465 * split off the signature into its own mbuf (since it should not be
2466 * calculated in the sctp_hmac_m() call).
2468 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2469 m_sig = m_split(m, sig_offset, M_NOWAIT);
2470 if (m_sig == NULL) {
2471 /* out of memory or ?? */
2474 #ifdef SCTP_MBUF_LOGGING
2475 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2476 sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
2481 * compute the signature/digest for the cookie
2483 ep = &(*inp_p)->sctp_ep;
2486 SCTP_TCB_UNLOCK(l_stcb);
2488 SCTP_INP_RLOCK(l_inp);
2490 SCTP_TCB_LOCK(l_stcb);
2492 /* which cookie is it? */
2493 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2494 (ep->current_secret_number != ep->last_secret_number)) {
2495 /* it's the old cookie */
2496 (void)sctp_hmac_m(SCTP_HMAC,
2497 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2498 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2500 /* it's the current cookie */
2501 (void)sctp_hmac_m(SCTP_HMAC,
2502 (uint8_t *)ep->secret_key[(int)ep->current_secret_number],
2503 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2505 /* get the signature */
2506 SCTP_INP_RUNLOCK(l_inp);
2507 sig = (uint8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *)&tmp_sig);
2509 /* couldn't find signature */
2510 sctp_m_freem(m_sig);
2513 /* compare the received digest with the computed digest */
2514 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2515 /* try the old cookie? */
2516 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2517 (ep->current_secret_number != ep->last_secret_number)) {
2518 /* compute digest with old */
2519 (void)sctp_hmac_m(SCTP_HMAC,
2520 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2521 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2523 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2531 * Now before we continue we must reconstruct our mbuf so that
2532 * normal processing of any other chunks will work.
2538 while (SCTP_BUF_NEXT(m_at) != NULL) {
2539 m_at = SCTP_BUF_NEXT(m_at);
2541 SCTP_BUF_NEXT(m_at) = m_sig;
2544 if (cookie_ok == 0) {
2545 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2546 SCTPDBG(SCTP_DEBUG_INPUT2,
2547 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2548 (uint32_t)offset, cookie_offset, sig_offset);
2552 * check the cookie timestamps to be sure it's not stale
2554 (void)SCTP_GETTIME_TIMEVAL(&now);
2555 /* Expire time is in Ticks, so we convert to seconds */
2556 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2557 time_expires.tv_usec = cookie->time_entered.tv_usec;
2558 if (timevalcmp(&now, &time_expires, >)) {
2559 /* cookie is stale! */
2560 struct mbuf *op_err;
2561 struct sctp_error_stale_cookie *cause;
2562 struct timeval diff;
2565 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2566 0, M_NOWAIT, 1, MT_DATA);
2567 if (op_err == NULL) {
2572 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
2573 cause = mtod(op_err, struct sctp_error_stale_cookie *);
2574 cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
2575 cause->cause.length = htons((sizeof(struct sctp_paramhdr) +
2576 (sizeof(uint32_t))));
2578 timevalsub(&diff, &time_expires);
2579 if (diff.tv_sec > UINT32_MAX / 1000000) {
2580 staleness = UINT32_MAX;
2582 staleness = diff.tv_sec * 1000000;
2584 if (UINT32_MAX - staleness >= (uint32_t)diff.tv_usec) {
2585 staleness += diff.tv_usec;
2587 staleness = UINT32_MAX;
2589 cause->stale_time = htonl(staleness);
2590 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2591 mflowtype, mflowid, l_inp->fibnum,
2596 * Now we must see with the lookup address if we have an existing
2597 * asoc. This will only happen if we were in the COOKIE-WAIT state
2598 * and a INIT collided with us and somewhere the peer sent the
2599 * cookie on another address besides the single address our assoc
2600 * had for him. In this case we will have one of the tie-tags set at
2601 * least AND the address field in the cookie can be used to look it
2605 switch (cookie->addr_type) {
2607 case SCTP_IPV6_ADDRESS:
2608 memset(&sin6, 0, sizeof(sin6));
2609 sin6.sin6_family = AF_INET6;
2610 sin6.sin6_len = sizeof(sin6);
2611 sin6.sin6_port = sh->src_port;
2612 sin6.sin6_scope_id = cookie->scope_id;
2613 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2614 sizeof(sin6.sin6_addr.s6_addr));
2615 to = (struct sockaddr *)&sin6;
2619 case SCTP_IPV4_ADDRESS:
2620 memset(&sin, 0, sizeof(sin));
2621 sin.sin_family = AF_INET;
2622 sin.sin_len = sizeof(sin);
2623 sin.sin_port = sh->src_port;
2624 sin.sin_addr.s_addr = cookie->address[0];
2625 to = (struct sockaddr *)&sin;
2629 /* This should not happen */
2632 if (*stcb == NULL) {
2633 /* Yep, lets check */
2634 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2635 if (*stcb == NULL) {
2637 * We should have only got back the same inp. If we
2638 * got back a different ep we have a problem. The
2639 * original findep got back l_inp and now
2641 if (l_inp != *inp_p) {
2642 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2645 if (*locked_tcb == NULL) {
2647 * In this case we found the assoc only
2648 * after we locked the create lock. This
2649 * means we are in a colliding case and we
2650 * must make sure that we unlock the tcb if
2651 * its one of the cases where we throw away
2652 * the incoming packets.
2654 *locked_tcb = *stcb;
2657 * We must also increment the inp ref count
2658 * since the ref_count flags was set when we
2659 * did not find the TCB, now we found it
2660 * which reduces the refcount.. we must
2661 * raise it back out to balance it all :-)
2663 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2664 if ((*stcb)->sctp_ep != l_inp) {
2665 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2666 (void *)(*stcb)->sctp_ep, (void *)l_inp);
2671 cookie_len -= SCTP_SIGNATURE_SIZE;
2672 if (*stcb == NULL) {
2673 /* this is the "normal" case... get a new TCB */
2674 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2675 cookie, cookie_len, *inp_p,
2676 netp, to, ¬ification,
2677 auth_skipped, auth_offset, auth_len,
2681 /* this is abnormal... cookie-echo on existing TCB */
2682 had_a_existing_tcb = 1;
2683 *stcb = sctp_process_cookie_existing(m, iphlen, offset,
2685 cookie, cookie_len, *inp_p, *stcb, netp, to,
2686 ¬ification, auth_skipped, auth_offset, auth_len,
2691 if (*stcb == NULL) {
2692 /* still no TCB... must be bad cookie-echo */
2695 if (*netp != NULL) {
2696 (*netp)->flowtype = mflowtype;
2697 (*netp)->flowid = mflowid;
2700 * Ok, we built an association so confirm the address we sent the
2703 netl = sctp_findnet(*stcb, to);
2705 * This code should in theory NOT run but
2708 /* TSNH! Huh, why do I need to add this address here? */
2709 if (sctp_add_remote_addr(*stcb, to, NULL, port,
2710 SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2713 netl = sctp_findnet(*stcb, to);
2716 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2717 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2718 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2723 sctp_start_net_timers(*stcb);
2724 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2725 if (!had_a_existing_tcb ||
2726 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2728 * If we have a NEW cookie or the connect never
2729 * reached the connected state during collision we
2730 * must do the TCP accept thing.
2732 struct socket *so, *oso;
2733 struct sctp_inpcb *inp;
2735 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2737 * For a restart we will keep the same
2738 * socket, no need to do anything. I THINK!!
2740 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2741 if (send_int_conf) {
2742 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2743 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2747 oso = (*inp_p)->sctp_socket;
2748 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2749 SCTP_TCB_UNLOCK((*stcb));
2750 CURVNET_SET(oso->so_vnet);
2751 so = sonewconn(oso, 0
2754 SCTP_TCB_LOCK((*stcb));
2755 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2758 struct mbuf *op_err;
2759 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2760 struct socket *pcb_so;
2762 /* Too many sockets */
2763 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2764 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2765 sctp_abort_association(*inp_p, NULL, m, iphlen,
2766 src, dst, sh, op_err,
2769 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2770 pcb_so = SCTP_INP_SO(*inp_p);
2771 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2772 SCTP_TCB_UNLOCK((*stcb));
2773 SCTP_SOCKET_LOCK(pcb_so, 1);
2774 SCTP_TCB_LOCK((*stcb));
2775 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2777 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2778 SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2779 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2780 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2784 inp = (struct sctp_inpcb *)so->so_pcb;
2785 SCTP_INP_INCR_REF(inp);
2787 * We add the unbound flag here so that if we get an
2788 * soabort() before we get the move_pcb done, we
2789 * will properly cleanup.
2791 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2792 SCTP_PCB_FLAGS_CONNECTED |
2793 SCTP_PCB_FLAGS_IN_TCPPOOL |
2794 SCTP_PCB_FLAGS_UNBOUND |
2795 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2796 SCTP_PCB_FLAGS_DONT_WAKE);
2797 inp->sctp_features = (*inp_p)->sctp_features;
2798 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2799 inp->sctp_socket = so;
2800 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2801 inp->max_cwnd = (*inp_p)->max_cwnd;
2802 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2803 inp->ecn_supported = (*inp_p)->ecn_supported;
2804 inp->prsctp_supported = (*inp_p)->prsctp_supported;
2805 inp->auth_supported = (*inp_p)->auth_supported;
2806 inp->asconf_supported = (*inp_p)->asconf_supported;
2807 inp->reconfig_supported = (*inp_p)->reconfig_supported;
2808 inp->nrsack_supported = (*inp_p)->nrsack_supported;
2809 inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2810 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2811 inp->sctp_context = (*inp_p)->sctp_context;
2812 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2813 inp->fibnum = (*inp_p)->fibnum;
2814 inp->inp_starting_point_for_iterator = NULL;
2816 * copy in the authentication parameters from the
2819 if (inp->sctp_ep.local_hmacs)
2820 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2821 inp->sctp_ep.local_hmacs =
2822 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2823 if (inp->sctp_ep.local_auth_chunks)
2824 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2825 inp->sctp_ep.local_auth_chunks =
2826 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2829 * Now we must move it from one hash table to
2830 * another and get the tcb in the right place.
2834 * This is where the one-2-one socket is put into
2835 * the accept state waiting for the accept!
2838 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2840 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2842 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2843 SCTP_TCB_UNLOCK((*stcb));
2845 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2847 SCTP_TCB_LOCK((*stcb));
2848 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2852 * now we must check to see if we were aborted while
2853 * the move was going on and the lock/unlock
2856 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2858 * yep it was, we leave the assoc attached
2859 * to the socket since the sctp_inpcb_free()
2860 * call will send an abort for us.
2862 SCTP_INP_DECR_REF(inp);
2865 SCTP_INP_DECR_REF(inp);
2866 /* Switch over to the new guy */
2868 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2869 if (send_int_conf) {
2870 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2871 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2874 * Pull it from the incomplete queue and wake the
2877 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2878 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2879 SCTP_TCB_UNLOCK((*stcb));
2880 SCTP_SOCKET_LOCK(so, 1);
2883 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2884 SCTP_TCB_LOCK((*stcb));
2885 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2886 SCTP_SOCKET_UNLOCK(so, 1);
2892 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2894 if (send_int_conf) {
2895 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2896 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2902 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2903 struct sctp_tcb *stcb, struct sctp_nets *net)
2905 /* cp must not be used, others call this without a c-ack :-) */
2906 struct sctp_association *asoc;
2908 SCTPDBG(SCTP_DEBUG_INPUT2,
2909 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2910 if ((stcb == NULL) || (net == NULL)) {
2915 sctp_stop_all_cookie_timers(stcb);
2916 /* process according to association state */
2917 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2918 /* state change only needed when I am in right state */
2919 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2920 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2921 sctp_start_net_timers(stcb);
2922 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2923 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2924 stcb->sctp_ep, stcb, asoc->primary_destination);
2928 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2929 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2930 if (asoc->overall_error_count == 0) {
2931 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2932 &asoc->time_entered, sctp_align_safe_nocopy,
2933 SCTP_RTT_FROM_NON_DATA);
2935 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2936 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2937 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2938 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2939 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2943 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2944 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2945 so = SCTP_INP_SO(stcb->sctp_ep);
2946 atomic_add_int(&stcb->asoc.refcnt, 1);
2947 SCTP_TCB_UNLOCK(stcb);
2948 SCTP_SOCKET_LOCK(so, 1);
2949 SCTP_TCB_LOCK(stcb);
2950 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2952 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2953 soisconnected(stcb->sctp_socket);
2955 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2956 SCTP_SOCKET_UNLOCK(so, 1);
2960 * since we did not send a HB make sure we don't double
2963 net->hb_responded = 1;
2965 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2967 * We don't need to do the asconf thing, nor hb or
2968 * autoclose if the socket is closed.
2972 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2976 if (stcb->asoc.sctp_autoclose_ticks &&
2977 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2978 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2979 stcb->sctp_ep, stcb, NULL);
2982 * send ASCONF if parameters are pending and ASCONFs are
2983 * allowed (eg. addresses changed when init/cookie echo were
2986 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2987 (stcb->asoc.asconf_supported == 1) &&
2988 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2989 #ifdef SCTP_TIMER_BASED_ASCONF
2990 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2991 stcb->sctp_ep, stcb,
2992 stcb->asoc.primary_destination);
2994 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2995 SCTP_ADDR_NOT_LOCKED);
3000 /* Toss the cookie if I can */
3001 sctp_toss_old_cookies(stcb, asoc);
3002 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3003 /* Restart the timer if we have pending data */
3004 struct sctp_tmit_chunk *chk;
3006 chk = TAILQ_FIRST(&asoc->sent_queue);
3007 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
3012 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
3013 struct sctp_tcb *stcb)
3015 struct sctp_nets *net;
3016 struct sctp_tmit_chunk *lchk;
3017 struct sctp_ecne_chunk bkup;
3018 uint8_t override_bit;
3019 uint32_t tsn, window_data_tsn;
3021 unsigned int pkt_cnt;
3023 len = ntohs(cp->ch.chunk_length);
3024 if ((len != sizeof(struct sctp_ecne_chunk)) &&
3025 (len != sizeof(struct old_sctp_ecne_chunk))) {
3028 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3029 /* Its the old format */
3030 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3031 bkup.num_pkts_since_cwr = htonl(1);
3034 SCTP_STAT_INCR(sctps_recvecne);
3035 tsn = ntohl(cp->tsn);
3036 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3037 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3039 window_data_tsn = stcb->asoc.sending_seq - 1;
3041 window_data_tsn = lchk->rec.data.tsn;
3044 /* Find where it was sent to if possible. */
3046 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3047 if (lchk->rec.data.tsn == tsn) {
3049 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3052 if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) {
3058 * What to do. A previous send of a CWR was possibly lost.
3059 * See how old it is, we may have it marked on the actual
3062 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3063 if (tsn == net->last_cwr_tsn) {
3064 /* Found him, send it off */
3070 * If we reach here, we need to send a special CWR
3071 * that says hey, we did this a long time ago and
3072 * you lost the response.
3074 net = TAILQ_FIRST(&stcb->asoc.nets);
3079 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3086 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3087 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3089 * JRS - Use the congestion control given in the pluggable
3092 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3094 * We reduce once every RTT. So we will only lower cwnd at
3095 * the next sending seq i.e. the window_data_tsn
3097 net->cwr_window_tsn = window_data_tsn;
3098 net->ecn_ce_pkt_cnt += pkt_cnt;
3099 net->lost_cnt = pkt_cnt;
3100 net->last_cwr_tsn = tsn;
3102 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3103 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3104 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3106 * Another loss in the same window update how many
3107 * marks/packets lost we have had.
3111 if (pkt_cnt > net->lost_cnt) {
3112 /* Should be the case */
3113 cnt = (pkt_cnt - net->lost_cnt);
3114 net->ecn_ce_pkt_cnt += cnt;
3116 net->lost_cnt = pkt_cnt;
3117 net->last_cwr_tsn = tsn;
3119 * Most CC functions will ignore this call, since we
3120 * are in-window yet of the initial CE the peer saw.
3122 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3126 * We always send a CWR this way if our previous one was lost our
3127 * peer will get an update, or if it is not time again to reduce we
3128 * still get the cwr to the peer. Note we set the override when we
3129 * could not find the TSN on the chunk or the destination network.
3131 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3135 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3138 * Here we get a CWR from the peer. We must look in the outqueue and
3139 * make sure that we have a covered ECNE in the control chunk part.
3142 struct sctp_tmit_chunk *chk, *nchk;
3143 struct sctp_ecne_chunk *ecne;
3147 cwr_tsn = ntohl(cp->tsn);
3148 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3149 TAILQ_FOREACH_SAFE(chk, &stcb->asoc.control_send_queue, sctp_next, nchk) {
3150 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3153 if ((override == 0) && (chk->whoTo != net)) {
3154 /* Must be from the right src unless override is set */
3157 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3158 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3159 /* this covers this ECNE, we can remove it */
3160 stcb->asoc.ecn_echo_cnt_onq--;
3161 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3163 sctp_m_freem(chk->data);
3165 stcb->asoc.ctrl_queue_cnt--;
3166 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3167 if (override == 0) {
3175 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3176 struct sctp_tcb *stcb, struct sctp_nets *net)
3178 struct sctp_association *asoc;
3179 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3183 SCTPDBG(SCTP_DEBUG_INPUT2,
3184 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3189 /* process according to association state */
3190 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3191 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3192 SCTPDBG(SCTP_DEBUG_INPUT2,
3193 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3194 SCTP_TCB_UNLOCK(stcb);
3197 /* notify upper layer protocol */
3198 if (stcb->sctp_socket) {
3199 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3202 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3203 !TAILQ_EMPTY(&asoc->sent_queue) ||
3204 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
3205 panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3208 /* stop the timer */
3209 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3210 SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3211 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3213 SCTPDBG(SCTP_DEBUG_INPUT2,
3214 "sctp_handle_shutdown_complete: calls free-asoc\n");
3215 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3216 so = SCTP_INP_SO(stcb->sctp_ep);
3217 atomic_add_int(&stcb->asoc.refcnt, 1);
3218 SCTP_TCB_UNLOCK(stcb);
3219 SCTP_SOCKET_LOCK(so, 1);
3220 SCTP_TCB_LOCK(stcb);
3221 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3223 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3224 SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3225 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3226 SCTP_SOCKET_UNLOCK(so, 1);
3232 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3233 struct sctp_nets *net, uint8_t flg)
3235 switch (desc->chunk_type) {
3237 /* find the tsn to resend (possibly */
3240 struct sctp_tmit_chunk *tp1;
3242 tsn = ntohl(desc->tsn_ifany);
3243 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3244 if (tp1->rec.data.tsn == tsn) {
3248 if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) {
3256 * Do it the other way , aka without paying
3257 * attention to queue seq order.
3259 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3260 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3261 if (tp1->rec.data.tsn == tsn) {
3268 SCTP_STAT_INCR(sctps_pdrptsnnf);
3270 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3273 if (((flg & SCTP_BADCRC) == 0) &&
3274 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3277 if ((stcb->asoc.peers_rwnd == 0) &&
3278 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3279 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3282 if (stcb->asoc.peers_rwnd == 0 &&
3283 (flg & SCTP_FROM_MIDDLE_BOX)) {
3284 SCTP_STAT_INCR(sctps_pdrpdizrw);
3287 ddp = (uint8_t *)(mtod(tp1->data, caddr_t)+
3288 sizeof(struct sctp_data_chunk));
3292 for (iii = 0; iii < sizeof(desc->data_bytes);
3294 if (ddp[iii] != desc->data_bytes[iii]) {
3295 SCTP_STAT_INCR(sctps_pdrpbadd);
3303 * this guy had a RTO calculation
3304 * pending on it, cancel it
3306 if (tp1->whoTo->rto_needed == 0) {
3307 tp1->whoTo->rto_needed = 1;
3311 SCTP_STAT_INCR(sctps_pdrpmark);
3312 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3313 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3315 * mark it as if we were doing a FR, since
3316 * we will be getting gap ack reports behind
3317 * the info from the router.
3319 tp1->rec.data.doing_fast_retransmit = 1;
3321 * mark the tsn with what sequences can
3324 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3325 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3327 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
3330 /* restart the timer */
3331 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3333 SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3334 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3337 /* fix counts and things */
3338 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3339 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3340 tp1->whoTo->flight_size,
3342 (uint32_t)(uintptr_t)stcb,
3345 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3346 sctp_flight_size_decrease(tp1);
3347 sctp_total_flight_decrease(stcb, tp1);
3349 tp1->sent = SCTP_DATAGRAM_RESEND;
3355 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3356 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3359 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3361 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3364 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3365 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3366 audit, stcb->asoc.sent_queue_retran_cnt);
3367 #ifndef SCTP_AUDITING_ENABLED
3368 stcb->asoc.sent_queue_retran_cnt = audit;
3376 struct sctp_tmit_chunk *asconf;
3378 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3380 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3385 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3386 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3387 asconf->sent = SCTP_DATAGRAM_RESEND;
3388 asconf->snd_count--;
3392 case SCTP_INITIATION:
3393 /* resend the INIT */
3394 stcb->asoc.dropped_special_cnt++;
3395 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3397 * If we can get it in, in a few attempts we do
3398 * this, otherwise we let the timer fire.
3400 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3402 SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3403 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3406 case SCTP_SELECTIVE_ACK:
3407 case SCTP_NR_SELECTIVE_ACK:
3408 /* resend the sack */
3409 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3411 case SCTP_HEARTBEAT_REQUEST:
3412 /* resend a demand HB */
3413 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3415 * Only retransmit if we KNOW we wont destroy the
3418 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3422 sctp_send_shutdown(stcb, net);
3424 case SCTP_SHUTDOWN_ACK:
3425 sctp_send_shutdown_ack(stcb, net);
3427 case SCTP_COOKIE_ECHO:
3429 struct sctp_tmit_chunk *cookie;
3432 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3434 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3439 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3440 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3441 cookie->sent = SCTP_DATAGRAM_RESEND;
3442 sctp_stop_all_cookie_timers(stcb);
3446 case SCTP_COOKIE_ACK:
3447 sctp_send_cookie_ack(stcb);
3449 case SCTP_ASCONF_ACK:
3450 /* resend last asconf ack */
3451 sctp_send_asconf_ack(stcb);
3453 case SCTP_IFORWARD_CUM_TSN:
3454 case SCTP_FORWARD_CUM_TSN:
3455 send_forward_tsn(stcb, &stcb->asoc);
3457 /* can't do anything with these */
3458 case SCTP_PACKET_DROPPED:
3459 case SCTP_INITIATION_ACK: /* this should not happen */
3460 case SCTP_HEARTBEAT_ACK:
3461 case SCTP_ABORT_ASSOCIATION:
3462 case SCTP_OPERATION_ERROR:
3463 case SCTP_SHUTDOWN_COMPLETE:
3473 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3479 * We set things to 0xffffffff since this is the last delivered
3480 * sequence and we will be sending in 0 after the reset.
3483 if (number_entries) {
3484 for (i = 0; i < number_entries; i++) {
3485 temp = ntohs(list[i]);
3486 if (temp >= stcb->asoc.streamincnt) {
3489 stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff;
3493 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3494 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
3497 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3501 sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3506 if (number_entries > 0) {
3507 for (i = 0; i < number_entries; i++) {
3508 temp = ntohs(list[i]);
3509 if (temp >= stcb->asoc.streamoutcnt) {
3510 /* no such stream */
3513 stcb->asoc.strmout[temp].next_mid_ordered = 0;
3514 stcb->asoc.strmout[temp].next_mid_unordered = 0;
3517 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3518 stcb->asoc.strmout[i].next_mid_ordered = 0;
3519 stcb->asoc.strmout[i].next_mid_unordered = 0;
3522 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3526 sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3531 if (number_entries > 0) {
3532 for (i = 0; i < number_entries; i++) {
3533 temp = ntohs(list[i]);
3534 if (temp >= stcb->asoc.streamoutcnt) {
3535 /* no such stream */
3538 stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
3541 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3542 stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
3548 struct sctp_stream_reset_request *
3549 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3551 struct sctp_association *asoc;
3552 struct sctp_chunkhdr *ch;
3553 struct sctp_stream_reset_request *r;
3554 struct sctp_tmit_chunk *chk;
3558 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3559 asoc->stream_reset_outstanding = 0;
3562 if (stcb->asoc.str_reset == NULL) {
3563 asoc->stream_reset_outstanding = 0;
3566 chk = stcb->asoc.str_reset;
3567 if (chk->data == NULL) {
3571 /* he wants a copy of the chk pointer */
3574 clen = chk->send_size;
3575 ch = mtod(chk->data, struct sctp_chunkhdr *);
3576 r = (struct sctp_stream_reset_request *)(ch + 1);
3577 if (ntohl(r->request_seq) == seq) {
3581 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3582 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3583 /* move to the next one, there can only be a max of two */
3584 r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3585 if (ntohl(r->request_seq) == seq) {
3589 /* that seq is not here */
3594 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3596 struct sctp_association *asoc;
3597 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3599 if (stcb->asoc.str_reset == NULL) {
3604 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
3605 chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3606 TAILQ_REMOVE(&asoc->control_send_queue,
3610 sctp_m_freem(chk->data);
3613 asoc->ctrl_queue_cnt--;
3614 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3615 /* sa_ignore NO_NULL_CHK */
3616 stcb->asoc.str_reset = NULL;
3621 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3622 uint32_t seq, uint32_t action,
3623 struct sctp_stream_reset_response *respin)
3627 struct sctp_association *asoc = &stcb->asoc;
3628 struct sctp_tmit_chunk *chk;
3629 struct sctp_stream_reset_request *req_param;
3630 struct sctp_stream_reset_out_request *req_out_param;
3631 struct sctp_stream_reset_in_request *req_in_param;
3632 uint32_t number_entries;
3634 if (asoc->stream_reset_outstanding == 0) {
3638 if (seq == stcb->asoc.str_reset_seq_out) {
3639 req_param = sctp_find_stream_reset(stcb, seq, &chk);
3640 if (req_param != NULL) {
3641 stcb->asoc.str_reset_seq_out++;
3642 type = ntohs(req_param->ph.param_type);
3643 lparam_len = ntohs(req_param->ph.param_length);
3644 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3647 req_out_param = (struct sctp_stream_reset_out_request *)req_param;
3648 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3649 asoc->stream_reset_out_is_outstanding = 0;
3650 if (asoc->stream_reset_outstanding)
3651 asoc->stream_reset_outstanding--;
3652 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3654 sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3655 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3656 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3657 } else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
3659 * Set it up so we don't stop
3662 asoc->stream_reset_outstanding++;
3663 stcb->asoc.str_reset_seq_out--;
3664 asoc->stream_reset_out_is_outstanding = 1;
3667 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3669 if (no_clear == 0) {
3670 sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
3672 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3673 req_in_param = (struct sctp_stream_reset_in_request *)req_param;
3674 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3675 if (asoc->stream_reset_outstanding)
3676 asoc->stream_reset_outstanding--;
3677 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3678 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3679 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3680 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3681 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3682 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3684 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3685 /* Ok we now may have more streams */
3688 num_stream = stcb->asoc.strm_pending_add_size;
3689 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3691 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3693 stcb->asoc.strm_pending_add_size = 0;
3694 if (asoc->stream_reset_outstanding)
3695 asoc->stream_reset_outstanding--;
3696 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3697 /* Put the new streams into effect */
3700 for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
3701 asoc->strmout[i].state = SCTP_STREAM_OPEN;
3703 asoc->streamoutcnt += num_stream;
3704 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3705 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3706 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3707 SCTP_STREAM_CHANGE_DENIED);
3709 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3710 SCTP_STREAM_CHANGE_FAILED);
3712 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3713 if (asoc->stream_reset_outstanding)
3714 asoc->stream_reset_outstanding--;
3715 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3716 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3717 SCTP_STREAM_CHANGE_DENIED);
3718 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3719 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3720 SCTP_STREAM_CHANGE_FAILED);
3722 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3724 * a) Adopt the new in tsn.
3726 * c) Adopt the new out-tsn
3728 struct sctp_stream_reset_response_tsn *resp;
3729 struct sctp_forward_tsn_chunk fwdtsn;
3732 if (respin == NULL) {
3736 if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
3739 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3740 resp = (struct sctp_stream_reset_response_tsn *)respin;
3741 asoc->stream_reset_outstanding--;
3742 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3743 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3744 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3745 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3749 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3750 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3751 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3753 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3754 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3755 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3757 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3758 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3760 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3761 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3763 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3764 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3765 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3766 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3767 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3768 SCTP_ASSOC_RESET_DENIED);
3770 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3771 SCTP_ASSOC_RESET_FAILED);
3774 /* get rid of the request and get the request flags */
3775 if (asoc->stream_reset_outstanding == 0) {
3776 sctp_clean_up_stream_reset(stcb);
3780 if (asoc->stream_reset_outstanding == 0) {
3781 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3787 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3788 struct sctp_tmit_chunk *chk,
3789 struct sctp_stream_reset_in_request *req, int trunc)
3797 * peer wants me to send a str-reset to him for my outgoing seq's if
3800 struct sctp_association *asoc = &stcb->asoc;
3802 seq = ntohl(req->request_seq);
3803 if (asoc->str_reset_seq_in == seq) {
3804 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3805 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3806 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3808 /* Can't do it, since they exceeded our buffer size */
3809 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3810 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3811 len = ntohs(req->ph.param_length);
3812 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3813 if (number_entries) {
3814 for (i = 0; i < number_entries; i++) {
3815 temp = ntohs(req->list_of_streams[i]);
3816 if (temp >= stcb->asoc.streamoutcnt) {
3817 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3820 req->list_of_streams[i] = temp;
3822 for (i = 0; i < number_entries; i++) {
3823 if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
3824 stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
3829 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3830 if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
3831 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
3834 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3836 /* Can't do it, since we have sent one out */
3837 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3840 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3841 asoc->str_reset_seq_in++;
3842 } else if (asoc->str_reset_seq_in - 1 == seq) {
3843 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3844 } else if (asoc->str_reset_seq_in - 2 == seq) {
3845 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3847 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3849 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3853 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3854 struct sctp_tmit_chunk *chk,
3855 struct sctp_stream_reset_tsn_request *req)
3857 /* reset all in and out and update the tsn */
3859 * A) reset my str-seq's on in and out. B) Select a receive next,
3860 * and set cum-ack to it. Also process this selected number as a
3861 * fwd-tsn as well. C) set in the response my next sending seq.
3863 struct sctp_forward_tsn_chunk fwdtsn;
3864 struct sctp_association *asoc = &stcb->asoc;
3868 seq = ntohl(req->request_seq);
3869 if (asoc->str_reset_seq_in == seq) {
3870 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3871 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3872 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3874 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3875 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3876 fwdtsn.ch.chunk_flags = 0;
3877 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3878 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3882 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3883 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3884 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3886 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3887 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3888 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3889 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3890 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3891 atomic_add_int(&asoc->sending_seq, 1);
3892 /* save off historical data for retrans */
3893 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3894 asoc->last_sending_seq[0] = asoc->sending_seq;
3895 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3896 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3897 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3898 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3899 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3900 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3902 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3903 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3904 asoc->str_reset_seq_in++;
3905 } else if (asoc->str_reset_seq_in - 1 == seq) {
3906 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3907 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3908 } else if (asoc->str_reset_seq_in - 2 == seq) {
3909 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3910 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3912 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3918 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3919 struct sctp_tmit_chunk *chk,
3920 struct sctp_stream_reset_out_request *req, int trunc)
3923 int number_entries, len;
3924 struct sctp_association *asoc = &stcb->asoc;
3926 seq = ntohl(req->request_seq);
3928 /* now if its not a duplicate we process it */
3929 if (asoc->str_reset_seq_in == seq) {
3930 len = ntohs(req->ph.param_length);
3931 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3933 * the sender is resetting, handle the list issue.. we must
3934 * a) verify if we can do the reset, if so no problem b) If
3935 * we can't do the reset we must copy the request. c) queue
3936 * it, and setup the data in processor to trigger it off
3937 * when needed and dequeue all the queued data.
3939 tsn = ntohl(req->send_reset_at_tsn);
3941 /* move the reset action back one */
3942 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3943 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3944 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3946 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3947 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3948 /* we can do it now */
3949 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3950 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3953 * we must queue it up and thus wait for the TSN's
3954 * to arrive that are at or before tsn
3956 struct sctp_stream_reset_list *liste;
3959 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3960 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3961 siz, SCTP_M_STRESET);
3962 if (liste == NULL) {
3963 /* gak out of memory */
3964 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3965 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3970 liste->number_entries = number_entries;
3971 memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
3972 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3973 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
3975 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3976 asoc->str_reset_seq_in++;
3977 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3979 * one seq back, just echo back last action since my
3980 * response was lost.
3982 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3983 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3985 * two seq back, just echo back last action since my
3986 * response was lost.
3988 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3990 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3995 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3996 struct sctp_stream_reset_add_strm *str_add)
3999 * Peer is requesting to add more streams. If its within our
4000 * max-streams we will allow it.
4002 uint32_t num_stream, i;
4004 struct sctp_association *asoc = &stcb->asoc;
4005 struct sctp_queued_to_read *ctl, *nctl;
4007 /* Get the number. */
4008 seq = ntohl(str_add->request_seq);
4009 num_stream = ntohs(str_add->number_of_streams);
4010 /* Now what would be the new total? */
4011 if (asoc->str_reset_seq_in == seq) {
4012 num_stream += stcb->asoc.streamincnt;
4013 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4014 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4015 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4016 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
4017 (num_stream > 0xffff)) {
4018 /* We must reject it they ask for to many */
4020 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4022 /* Ok, we can do that :-) */
4023 struct sctp_stream_in *oldstrm;
4025 /* save off the old */
4026 oldstrm = stcb->asoc.strmin;
4027 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
4028 (num_stream * sizeof(struct sctp_stream_in)),
4030 if (stcb->asoc.strmin == NULL) {
4031 stcb->asoc.strmin = oldstrm;
4034 /* copy off the old data */
4035 for (i = 0; i < stcb->asoc.streamincnt; i++) {
4036 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4037 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4038 stcb->asoc.strmin[i].sid = i;
4039 stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered;
4040 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
4041 stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
4042 /* now anything on those queues? */
4043 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
4044 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
4045 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
4047 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
4048 TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
4049 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
4052 /* Init the new streams */
4053 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
4054 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4055 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4056 stcb->asoc.strmin[i].sid = i;
4057 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
4058 stcb->asoc.strmin[i].pd_api_started = 0;
4059 stcb->asoc.strmin[i].delivery_started = 0;
4061 SCTP_FREE(oldstrm, SCTP_M_STRMI);
4062 /* update the size */
4063 stcb->asoc.streamincnt = num_stream;
4064 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4065 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
4067 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4068 asoc->str_reset_seq_in++;
4069 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4071 * one seq back, just echo back last action since my
4072 * response was lost.
4074 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4075 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4077 * two seq back, just echo back last action since my
4078 * response was lost.
4080 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4082 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4088 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4089 struct sctp_stream_reset_add_strm *str_add)
4092 * Peer is requesting to add more streams. If its within our
4093 * max-streams we will allow it.
4095 uint16_t num_stream;
4097 struct sctp_association *asoc = &stcb->asoc;
4099 /* Get the number. */
4100 seq = ntohl(str_add->request_seq);
4101 num_stream = ntohs(str_add->number_of_streams);
4102 /* Now what would be the new total? */
4103 if (asoc->str_reset_seq_in == seq) {
4104 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4105 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4106 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4107 } else if (stcb->asoc.stream_reset_outstanding) {
4108 /* We must reject it we have something pending */
4109 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4111 /* Ok, we can do that :-) */
4114 mychk = stcb->asoc.streamoutcnt;
4115 mychk += num_stream;
4116 if (mychk < 0x10000) {
4117 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4118 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
4119 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4122 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4125 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4126 asoc->str_reset_seq_in++;
4127 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4129 * one seq back, just echo back last action since my
4130 * response was lost.
4132 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4133 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4135 * two seq back, just echo back last action since my
4136 * response was lost.
4138 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4140 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4145 __attribute__((noinline))
4148 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4149 struct sctp_chunkhdr *ch_req)
4151 uint16_t remaining_length, param_len, ptype;
4152 struct sctp_paramhdr pstore;
4153 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4157 struct sctp_tmit_chunk *chk;
4158 struct sctp_chunkhdr *ch;
4159 struct sctp_paramhdr *ph;
4163 /* now it may be a reset or a reset-response */
4164 remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
4166 /* setup for adding the response */
4167 sctp_alloc_a_chunk(stcb, chk);
4171 chk->copy_by_ref = 0;
4172 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4173 chk->rec.chunk_id.can_take_data = 0;
4175 chk->asoc = &stcb->asoc;
4176 chk->no_fr_allowed = 0;
4177 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4178 chk->book_size_scale = 0;
4179 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4180 if (chk->data == NULL) {
4183 sctp_m_freem(chk->data);
4186 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4189 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4191 /* setup chunk parameters */
4192 chk->sent = SCTP_DATAGRAM_UNSENT;
4196 ch = mtod(chk->data, struct sctp_chunkhdr *);
4197 ch->chunk_type = SCTP_STREAM_RESET;
4198 ch->chunk_flags = 0;
4199 ch->chunk_length = htons(chk->send_size);
4200 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4201 offset += sizeof(struct sctp_chunkhdr);
4202 while (remaining_length >= sizeof(struct sctp_paramhdr)) {
4203 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
4208 param_len = ntohs(ph->param_length);
4209 if ((param_len > remaining_length) ||
4210 (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
4211 /* bad parameter length */
4214 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
4215 (uint8_t *)&cstore);
4220 ptype = ntohs(ph->param_type);
4222 if (param_len > sizeof(cstore)) {
4227 if (num_param > SCTP_MAX_RESET_PARAMS) {
4228 /* hit the max of parameters already sorry.. */
4231 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4232 struct sctp_stream_reset_out_request *req_out;
4234 if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
4237 req_out = (struct sctp_stream_reset_out_request *)ph;
4239 if (stcb->asoc.stream_reset_outstanding) {
4240 seq = ntohl(req_out->response_seq);
4241 if (seq == stcb->asoc.str_reset_seq_out) {
4243 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4246 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4247 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4248 struct sctp_stream_reset_add_strm *str_add;
4250 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4253 str_add = (struct sctp_stream_reset_add_strm *)ph;
4255 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4256 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4257 struct sctp_stream_reset_add_strm *str_add;
4259 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4262 str_add = (struct sctp_stream_reset_add_strm *)ph;
4264 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4265 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4266 struct sctp_stream_reset_in_request *req_in;
4269 req_in = (struct sctp_stream_reset_in_request *)ph;
4270 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4271 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4272 struct sctp_stream_reset_tsn_request *req_tsn;
4275 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4276 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4278 goto strres_nochunk;
4282 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4283 struct sctp_stream_reset_response *resp;
4286 if (param_len < sizeof(struct sctp_stream_reset_response)) {
4289 resp = (struct sctp_stream_reset_response *)ph;
4290 seq = ntohl(resp->response_seq);
4291 result = ntohl(resp->result);
4292 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4294 goto strres_nochunk;
4299 offset += SCTP_SIZE32(param_len);
4300 if (remaining_length >= SCTP_SIZE32(param_len)) {
4301 remaining_length -= SCTP_SIZE32(param_len);
4303 remaining_length = 0;
4307 /* we have no response free the stuff */
4308 goto strres_nochunk;
4310 /* ok we have a chunk to link in */
4311 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4314 stcb->asoc.ctrl_queue_cnt++;
4319 * Handle a router or endpoints report of a packet loss, there are two ways
4320 * to handle this, either we get the whole packet and must disect it
4321 * ourselves (possibly with truncation and or corruption) or it is a summary
4322 * from a middle box that did the disectting for us.
4325 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4326 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4328 uint32_t bottle_bw, on_queue;
4332 struct sctp_chunk_desc desc;
4333 struct sctp_chunkhdr *ch;
4335 chlen = ntohs(cp->ch.chunk_length);
4336 chlen -= sizeof(struct sctp_pktdrop_chunk);
4337 /* XXX possible chlen underflow */
4340 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4341 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4343 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4344 chlen -= sizeof(struct sctphdr);
4345 /* XXX possible chlen underflow */
4346 memset(&desc, 0, sizeof(desc));
4348 trunc_len = (uint16_t)ntohs(cp->trunc_len);
4349 if (trunc_len > limit) {
4352 /* now the chunks themselves */
4353 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4354 desc.chunk_type = ch->chunk_type;
4355 /* get amount we need to move */
4356 at = ntohs(ch->chunk_length);
4357 if (at < sizeof(struct sctp_chunkhdr)) {
4358 /* corrupt chunk, maybe at the end? */
4359 SCTP_STAT_INCR(sctps_pdrpcrupt);
4362 if (trunc_len == 0) {
4363 /* we are supposed to have all of it */
4365 /* corrupt skip it */
4366 SCTP_STAT_INCR(sctps_pdrpcrupt);
4370 /* is there enough of it left ? */
4371 if (desc.chunk_type == SCTP_DATA) {
4372 if (chlen < (sizeof(struct sctp_data_chunk) +
4373 sizeof(desc.data_bytes))) {
4377 if (chlen < sizeof(struct sctp_chunkhdr)) {
4382 if (desc.chunk_type == SCTP_DATA) {
4383 /* can we get out the tsn? */
4384 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4385 SCTP_STAT_INCR(sctps_pdrpmbda);
4387 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4389 struct sctp_data_chunk *dcp;
4393 dcp = (struct sctp_data_chunk *)ch;
4394 ddp = (uint8_t *)(dcp + 1);
4395 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4396 desc.data_bytes[iii] = ddp[iii];
4398 desc.tsn_ifany = dcp->dp.tsn;
4400 /* nope we are done. */
4401 SCTP_STAT_INCR(sctps_pdrpnedat);
4405 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4406 SCTP_STAT_INCR(sctps_pdrpmbct);
4409 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4410 SCTP_STAT_INCR(sctps_pdrppdbrk);
4413 if (SCTP_SIZE32(at) > chlen) {
4416 chlen -= SCTP_SIZE32(at);
4417 if (chlen < sizeof(struct sctp_chunkhdr)) {
4418 /* done, none left */
4421 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4423 /* Now update any rwnd --- possibly */
4424 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4425 /* From a peer, we get a rwnd report */
4428 SCTP_STAT_INCR(sctps_pdrpfehos);
4430 bottle_bw = ntohl(cp->bottle_bw);
4431 on_queue = ntohl(cp->current_onq);
4432 if (bottle_bw && on_queue) {
4433 /* a rwnd report is in here */
4434 if (bottle_bw > on_queue)
4435 a_rwnd = bottle_bw - on_queue;
4440 stcb->asoc.peers_rwnd = 0;
4442 if (a_rwnd > stcb->asoc.total_flight) {
4443 stcb->asoc.peers_rwnd =
4444 a_rwnd - stcb->asoc.total_flight;
4446 stcb->asoc.peers_rwnd = 0;
4448 if (stcb->asoc.peers_rwnd <
4449 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4450 /* SWS sender side engages */
4451 stcb->asoc.peers_rwnd = 0;
4456 SCTP_STAT_INCR(sctps_pdrpfmbox);
4459 /* now middle boxes in sat networks get a cwnd bump */
4460 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4461 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4462 (stcb->asoc.sat_network)) {
4464 * This is debatable but for sat networks it makes sense
4465 * Note if a T3 timer has went off, we will prohibit any
4466 * changes to cwnd until we exit the t3 loss recovery.
4468 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4469 net, cp, &bottle_bw, &on_queue);
4474 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4475 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4476 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4477 * length of the complete packet outputs: - length: modified to remaining
4478 * length after control processing - netp: modified to new sctp_nets after
4479 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4480 * bad packet,...) otherwise return the tcb for this packet
4483 __attribute__((noinline))
4485 static struct sctp_tcb *
4486 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4487 struct sockaddr *src, struct sockaddr *dst,
4488 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4489 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4490 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4491 uint32_t vrf_id, uint16_t port)
4493 struct sctp_association *asoc;
4494 struct mbuf *op_err;
4495 char msg[SCTP_DIAG_INFO_LEN];
4497 int num_chunks = 0; /* number of control chunks processed */
4498 uint32_t chk_length;
4500 int abort_no_unlock = 0;
4504 * How big should this be, and should it be alloc'd? Lets try the
4505 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4506 * until we get into jumbo grams and such..
4508 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4509 struct sctp_tcb *locked_tcb = stcb;
4511 uint32_t auth_offset = 0, auth_len = 0;
4512 int auth_skipped = 0;
4514 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4518 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4519 iphlen, *offset, length, (void *)stcb);
4521 /* validate chunk header length... */
4522 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4523 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4524 ntohs(ch->chunk_length));
4526 SCTP_TCB_UNLOCK(locked_tcb);
4531 * validate the verification tag
4533 vtag_in = ntohl(sh->v_tag);
4536 SCTP_TCB_LOCK_ASSERT(locked_tcb);
4538 if (ch->chunk_type == SCTP_INITIATION) {
4539 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4540 ntohs(ch->chunk_length), vtag_in);
4542 /* protocol error- silently discard... */
4543 SCTP_STAT_INCR(sctps_badvtag);
4545 SCTP_TCB_UNLOCK(locked_tcb);
4549 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4551 * If there is no stcb, skip the AUTH chunk and process
4552 * later after a stcb is found (to validate the lookup was
4555 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4557 (inp->auth_supported == 1)) {
4558 /* save this chunk for later processing */
4560 auth_offset = *offset;
4561 auth_len = ntohs(ch->chunk_length);
4563 /* (temporarily) move past this chunk */
4564 *offset += SCTP_SIZE32(auth_len);
4565 if (*offset >= length) {
4566 /* no more data left in the mbuf chain */
4569 SCTP_TCB_UNLOCK(locked_tcb);
4573 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4574 sizeof(struct sctp_chunkhdr), chunk_buf);
4580 SCTP_TCB_UNLOCK(locked_tcb);
4584 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4585 goto process_control_chunks;
4588 * first check if it's an ASCONF with an unknown src addr we
4589 * need to look inside to find the association
4591 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4592 struct sctp_chunkhdr *asconf_ch = ch;
4593 uint32_t asconf_offset = 0, asconf_len = 0;
4595 /* inp's refcount may be reduced */
4596 SCTP_INP_INCR_REF(inp);
4598 asconf_offset = *offset;
4600 asconf_len = ntohs(asconf_ch->chunk_length);
4601 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4603 stcb = sctp_findassociation_ep_asconf(m,
4606 sh, &inp, netp, vrf_id);
4609 asconf_offset += SCTP_SIZE32(asconf_len);
4610 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4611 sizeof(struct sctp_chunkhdr), chunk_buf);
4612 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4615 * reduce inp's refcount if not reduced in
4616 * sctp_findassociation_ep_asconf().
4618 SCTP_INP_DECR_REF(inp);
4623 /* now go back and verify any auth chunk to be sure */
4624 if (auth_skipped && (stcb != NULL)) {
4625 struct sctp_auth_chunk *auth;
4627 auth = (struct sctp_auth_chunk *)
4628 sctp_m_getptr(m, auth_offset,
4629 auth_len, chunk_buf);
4632 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4634 /* auth HMAC failed so dump it */
4637 SCTP_TCB_UNLOCK(locked_tcb);
4641 /* remaining chunks are HMAC checked */
4642 stcb->asoc.authenticated = 1;
4647 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4648 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4650 /* no association, so it's out of the blue... */
4651 sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4652 mflowtype, mflowid, inp->fibnum,
4656 SCTP_TCB_UNLOCK(locked_tcb);
4661 /* ABORT and SHUTDOWN can use either v_tag... */
4662 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4663 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4664 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4665 /* Take the T-bit always into account. */
4666 if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
4667 (vtag_in == asoc->my_vtag)) ||
4668 (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4669 (vtag_in == asoc->peer_vtag))) {
4672 /* drop this packet... */
4673 SCTP_STAT_INCR(sctps_badvtag);
4675 SCTP_TCB_UNLOCK(locked_tcb);
4679 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4680 if (vtag_in != asoc->my_vtag) {
4682 * this could be a stale SHUTDOWN-ACK or the
4683 * peer never got the SHUTDOWN-COMPLETE and
4684 * is still hung; we have started a new asoc
4685 * but it won't complete until the shutdown
4689 SCTP_TCB_UNLOCK(locked_tcb);
4691 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4692 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4694 sctp_handle_ootb(m, iphlen, *offset, src, dst,
4696 mflowtype, mflowid, fibnum,
4701 /* for all other chunks, vtag must match */
4702 if (vtag_in != asoc->my_vtag) {
4703 /* invalid vtag... */
4704 SCTPDBG(SCTP_DEBUG_INPUT3,
4705 "invalid vtag: %xh, expect %xh\n",
4706 vtag_in, asoc->my_vtag);
4707 SCTP_STAT_INCR(sctps_badvtag);
4709 SCTP_TCB_UNLOCK(locked_tcb);
4715 } /* end if !SCTP_COOKIE_ECHO */
4717 * process all control chunks...
4719 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4720 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4721 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4722 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4723 /* implied cookie-ack.. we must have lost the ack */
4724 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4725 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4726 stcb->asoc.overall_error_count,
4728 SCTP_FROM_SCTP_INPUT,
4731 stcb->asoc.overall_error_count = 0;
4732 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4735 process_control_chunks:
4736 while (IS_SCTP_CONTROL(ch)) {
4737 /* validate chunk length */
4738 chk_length = ntohs(ch->chunk_length);
4739 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4740 ch->chunk_type, chk_length);
4741 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4742 if (chk_length < sizeof(*ch) ||
4743 (*offset + (int)chk_length) > length) {
4746 SCTP_TCB_UNLOCK(locked_tcb);
4750 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4752 * INIT-ACK only gets the init ack "header" portion only
4753 * because we don't have to process the peer's COOKIE. All
4754 * others get a complete chunk.
4756 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4757 (ch->chunk_type == SCTP_INITIATION)) {
4758 /* get an init-ack chunk */
4759 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4760 sizeof(struct sctp_init_ack_chunk), chunk_buf);
4764 SCTP_TCB_UNLOCK(locked_tcb);
4769 /* For cookies and all other chunks. */
4770 if (chk_length > sizeof(chunk_buf)) {
4772 * use just the size of the chunk buffer so
4773 * the front part of our chunks fit in
4774 * contiguous space up to the chunk buffer
4775 * size (508 bytes). For chunks that need to
4776 * get more than that they must use the
4777 * sctp_m_getptr() function or other means
4778 * (e.g. know how to parse mbuf chains).
4779 * Cookies do this already.
4781 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4782 (sizeof(chunk_buf) - 4),
4787 SCTP_TCB_UNLOCK(locked_tcb);
4792 /* We can fit it all */
4793 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4794 chk_length, chunk_buf);
4796 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4799 SCTP_TCB_UNLOCK(locked_tcb);
4806 /* Save off the last place we got a control from */
4808 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4810 * allow last_control to be NULL if
4811 * ASCONF... ASCONF processing will find the
4814 if ((netp != NULL) && (*netp != NULL))
4815 stcb->asoc.last_control_chunk_from = *netp;
4818 #ifdef SCTP_AUDITING_ENABLED
4819 sctp_audit_log(0xB0, ch->chunk_type);
4822 /* check to see if this chunk required auth, but isn't */
4823 if ((stcb != NULL) &&
4824 (stcb->asoc.auth_supported == 1) &&
4825 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4826 !stcb->asoc.authenticated) {
4827 /* "silently" ignore */
4828 SCTP_STAT_INCR(sctps_recvauthmissing);
4831 switch (ch->chunk_type) {
4832 case SCTP_INITIATION:
4833 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4834 /* The INIT chunk must be the only chunk. */
4835 if ((num_chunks > 1) ||
4836 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4837 /* RFC 4960 requires that no ABORT is sent */
4840 SCTP_TCB_UNLOCK(locked_tcb);
4844 /* Honor our resource limit. */
4845 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4846 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4847 sctp_abort_association(inp, stcb, m, iphlen,
4848 src, dst, sh, op_err,
4854 sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4855 (struct sctp_init_chunk *)ch, inp,
4856 stcb, *netp, &abort_no_unlock,
4860 if ((!abort_no_unlock) && (locked_tcb)) {
4861 SCTP_TCB_UNLOCK(locked_tcb);
4865 case SCTP_PAD_CHUNK:
4867 case SCTP_INITIATION_ACK:
4868 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4869 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4870 /* We are not interested anymore */
4871 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4874 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
4876 SCTP_TCB_UNLOCK(locked_tcb);
4880 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4881 so = SCTP_INP_SO(inp);
4882 atomic_add_int(&stcb->asoc.refcnt, 1);
4883 SCTP_TCB_UNLOCK(stcb);
4884 SCTP_SOCKET_LOCK(so, 1);
4885 SCTP_TCB_LOCK(stcb);
4886 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4888 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4889 SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4890 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4891 SCTP_SOCKET_UNLOCK(so, 1);
4897 /* The INIT-ACK chunk must be the only chunk. */
4898 if ((num_chunks > 1) ||
4899 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4902 SCTP_TCB_UNLOCK(locked_tcb);
4906 if ((netp) && (*netp)) {
4907 ret = sctp_handle_init_ack(m, iphlen, *offset,
4909 (struct sctp_init_ack_chunk *)ch,
4918 if (abort_no_unlock) {
4922 * Special case, I must call the output routine to
4923 * get the cookie echoed
4925 if ((stcb != NULL) && (ret == 0)) {
4926 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4929 SCTP_TCB_UNLOCK(locked_tcb);
4933 case SCTP_SELECTIVE_ACK:
4935 struct sctp_sack_chunk *sack;
4937 uint32_t a_rwnd, cum_ack;
4938 uint16_t num_seg, num_dup;
4940 int offset_seg, offset_dup;
4942 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4943 SCTP_STAT_INCR(sctps_recvsacks);
4945 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4948 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4949 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4952 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4954 * If we have sent a shutdown-ack, we will pay no
4955 * attention to a sack sent in to us since
4956 * we don't care anymore.
4960 sack = (struct sctp_sack_chunk *)ch;
4961 flags = ch->chunk_flags;
4962 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4963 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4964 num_dup = ntohs(sack->sack.num_dup_tsns);
4965 a_rwnd = (uint32_t)ntohl(sack->sack.a_rwnd);
4966 if (sizeof(struct sctp_sack_chunk) +
4967 num_seg * sizeof(struct sctp_gap_ack_block) +
4968 num_dup * sizeof(uint32_t) != chk_length) {
4969 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4972 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4973 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4974 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4975 cum_ack, num_seg, a_rwnd);
4976 stcb->asoc.seen_a_sack_this_pkt = 1;
4977 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4979 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4980 (stcb->asoc.saw_sack_with_frags == 0) &&
4981 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4982 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4985 * We have a SIMPLE sack having no
4986 * prior segments and data on sent
4987 * queue to be acked.. Use the
4988 * faster path sack processing. We
4989 * also allow window update sacks
4990 * with no missing segments to go
4993 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
4996 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4997 num_seg, 0, num_dup, &abort_now, flags,
4998 cum_ack, a_rwnd, ecne_seen);
5001 /* ABORT signal from sack processing */
5005 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5006 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5007 (stcb->asoc.stream_queue_cnt == 0)) {
5008 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5013 * EY - nr_sack: If the received chunk is an
5016 case SCTP_NR_SELECTIVE_ACK:
5018 struct sctp_nr_sack_chunk *nr_sack;
5020 uint32_t a_rwnd, cum_ack;
5021 uint16_t num_seg, num_nr_seg, num_dup;
5023 int offset_seg, offset_dup;
5025 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
5026 SCTP_STAT_INCR(sctps_recvsacks);
5028 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
5031 if (stcb->asoc.nrsack_supported == 0) {
5034 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
5035 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
5038 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
5040 * If we have sent a shutdown-ack, we will pay no
5041 * attention to a sack sent in to us since
5042 * we don't care anymore.
5046 nr_sack = (struct sctp_nr_sack_chunk *)ch;
5047 flags = ch->chunk_flags;
5048 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
5049 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
5050 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
5051 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
5052 a_rwnd = (uint32_t)ntohl(nr_sack->nr_sack.a_rwnd);
5053 if (sizeof(struct sctp_nr_sack_chunk) +
5054 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
5055 num_dup * sizeof(uint32_t) != chk_length) {
5056 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
5059 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
5060 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
5061 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
5062 cum_ack, num_seg, a_rwnd);
5063 stcb->asoc.seen_a_sack_this_pkt = 1;
5064 if ((stcb->asoc.pr_sctp_cnt == 0) &&
5065 (num_seg == 0) && (num_nr_seg == 0) &&
5066 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
5067 (stcb->asoc.saw_sack_with_frags == 0) &&
5068 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
5069 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
5071 * We have a SIMPLE sack having no
5072 * prior segments and data on sent
5073 * queue to be acked. Use the faster
5074 * path sack processing. We also
5075 * allow window update sacks with no
5076 * missing segments to go this way
5079 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
5080 &abort_now, ecne_seen);
5083 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
5084 num_seg, num_nr_seg, num_dup, &abort_now, flags,
5085 cum_ack, a_rwnd, ecne_seen);
5088 /* ABORT signal from sack processing */
5092 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5093 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5094 (stcb->asoc.stream_queue_cnt == 0)) {
5095 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5100 case SCTP_HEARTBEAT_REQUEST:
5101 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
5102 if ((stcb) && netp && *netp) {
5103 SCTP_STAT_INCR(sctps_recvheartbeat);
5104 sctp_send_heartbeat_ack(stcb, m, *offset,
5107 /* He's alive so give him credit */
5108 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5109 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5110 stcb->asoc.overall_error_count,
5112 SCTP_FROM_SCTP_INPUT,
5115 stcb->asoc.overall_error_count = 0;
5118 case SCTP_HEARTBEAT_ACK:
5119 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
5120 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5124 SCTP_TCB_UNLOCK(locked_tcb);
5128 /* He's alive so give him credit */
5129 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5130 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5131 stcb->asoc.overall_error_count,
5133 SCTP_FROM_SCTP_INPUT,
5136 stcb->asoc.overall_error_count = 0;
5137 SCTP_STAT_INCR(sctps_recvheartbeatack);
5139 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5142 case SCTP_ABORT_ASSOCIATION:
5143 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5145 if ((stcb) && netp && *netp)
5146 sctp_handle_abort((struct sctp_abort_chunk *)ch,
5152 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5154 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5157 SCTP_TCB_UNLOCK(locked_tcb);
5161 if (netp && *netp) {
5164 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5165 stcb, *netp, &abort_flag);
5172 case SCTP_SHUTDOWN_ACK:
5173 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", (void *)stcb);
5174 if ((stcb) && (netp) && (*netp))
5175 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5180 case SCTP_OPERATION_ERROR:
5181 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
5182 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
5187 case SCTP_COOKIE_ECHO:
5188 SCTPDBG(SCTP_DEBUG_INPUT3,
5189 "SCTP_COOKIE-ECHO, stcb %p\n", (void *)stcb);
5190 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5193 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5194 /* We are not interested anymore */
5197 SCTP_TCB_UNLOCK(stcb);
5204 * First are we accepting? We do this again here
5205 * since it is possible that a previous endpoint WAS
5206 * listening responded to a INIT-ACK and then
5207 * closed. We opened and bound.. and are now no
5211 if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
5212 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5213 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5214 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
5215 sctp_abort_association(inp, stcb, m, iphlen,
5216 src, dst, sh, op_err,
5223 struct mbuf *ret_buf;
5224 struct sctp_inpcb *linp;
5233 SCTP_ASOC_CREATE_LOCK(linp);
5234 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5235 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5236 SCTP_ASOC_CREATE_UNLOCK(linp);
5242 sctp_handle_cookie_echo(m, iphlen,
5246 (struct sctp_cookie_echo_chunk *)ch,
5260 SCTP_ASOC_CREATE_UNLOCK(linp);
5262 if (ret_buf == NULL) {
5264 SCTP_TCB_UNLOCK(locked_tcb);
5266 SCTPDBG(SCTP_DEBUG_INPUT3,
5267 "GAK, null buffer\n");
5271 /* if AUTH skipped, see if it verified... */
5276 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5278 * Restart the timer if we have
5281 struct sctp_tmit_chunk *chk;
5283 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5284 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5288 case SCTP_COOKIE_ACK:
5289 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", (void *)stcb);
5290 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5292 SCTP_TCB_UNLOCK(locked_tcb);
5296 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5297 /* We are not interested anymore */
5298 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5301 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5302 so = SCTP_INP_SO(inp);
5303 atomic_add_int(&stcb->asoc.refcnt, 1);
5304 SCTP_TCB_UNLOCK(stcb);
5305 SCTP_SOCKET_LOCK(so, 1);
5306 SCTP_TCB_LOCK(stcb);
5307 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5309 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5310 SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5311 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5312 SCTP_SOCKET_UNLOCK(so, 1);
5318 /* He's alive so give him credit */
5319 if ((stcb) && netp && *netp) {
5320 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5321 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5322 stcb->asoc.overall_error_count,
5324 SCTP_FROM_SCTP_INPUT,
5327 stcb->asoc.overall_error_count = 0;
5328 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5332 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5333 /* He's alive so give him credit */
5334 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5337 SCTP_TCB_UNLOCK(locked_tcb);
5343 if (stcb->asoc.ecn_supported == 0) {
5346 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5347 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5348 stcb->asoc.overall_error_count,
5350 SCTP_FROM_SCTP_INPUT,
5353 stcb->asoc.overall_error_count = 0;
5354 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5360 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5361 /* He's alive so give him credit */
5362 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5365 SCTP_TCB_UNLOCK(locked_tcb);
5371 if (stcb->asoc.ecn_supported == 0) {
5374 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5375 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5376 stcb->asoc.overall_error_count,
5378 SCTP_FROM_SCTP_INPUT,
5381 stcb->asoc.overall_error_count = 0;
5382 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5385 case SCTP_SHUTDOWN_COMPLETE:
5386 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", (void *)stcb);
5387 /* must be first and only chunk */
5388 if ((num_chunks > 1) ||
5389 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5392 SCTP_TCB_UNLOCK(locked_tcb);
5396 if ((stcb) && netp && *netp) {
5397 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5404 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5405 /* He's alive so give him credit */
5407 if (stcb->asoc.asconf_supported == 0) {
5410 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5411 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5412 stcb->asoc.overall_error_count,
5414 SCTP_FROM_SCTP_INPUT,
5417 stcb->asoc.overall_error_count = 0;
5418 sctp_handle_asconf(m, *offset, src,
5419 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5423 case SCTP_ASCONF_ACK:
5424 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5425 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5428 SCTP_TCB_UNLOCK(locked_tcb);
5433 if ((stcb) && netp && *netp) {
5434 if (stcb->asoc.asconf_supported == 0) {
5437 /* He's alive so give him credit */
5438 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5439 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5440 stcb->asoc.overall_error_count,
5442 SCTP_FROM_SCTP_INPUT,
5445 stcb->asoc.overall_error_count = 0;
5446 sctp_handle_asconf_ack(m, *offset,
5447 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5448 if (abort_no_unlock)
5452 case SCTP_FORWARD_CUM_TSN:
5453 case SCTP_IFORWARD_CUM_TSN:
5454 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5455 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5458 SCTP_TCB_UNLOCK(locked_tcb);
5463 /* He's alive so give him credit */
5467 if (stcb->asoc.prsctp_supported == 0) {
5470 stcb->asoc.overall_error_count = 0;
5471 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5472 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5473 stcb->asoc.overall_error_count,
5475 SCTP_FROM_SCTP_INPUT,
5479 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5480 /* We are not interested anymore */
5481 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5482 so = SCTP_INP_SO(inp);
5483 atomic_add_int(&stcb->asoc.refcnt, 1);
5484 SCTP_TCB_UNLOCK(stcb);
5485 SCTP_SOCKET_LOCK(so, 1);
5486 SCTP_TCB_LOCK(stcb);
5487 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5489 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5490 SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5491 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5492 SCTP_SOCKET_UNLOCK(so, 1);
5498 * For sending a SACK this looks like DATA
5501 stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from;
5502 sctp_handle_forward_tsn(stcb,
5503 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5508 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5509 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5510 stcb->asoc.overall_error_count,
5512 SCTP_FROM_SCTP_INPUT,
5515 stcb->asoc.overall_error_count = 0;
5520 case SCTP_STREAM_RESET:
5521 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5522 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5525 SCTP_TCB_UNLOCK(locked_tcb);
5530 if (stcb->asoc.reconfig_supported == 0) {
5533 if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5534 /* stop processing */
5539 case SCTP_PACKET_DROPPED:
5540 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5541 /* re-get it all please */
5542 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5545 SCTP_TCB_UNLOCK(locked_tcb);
5550 if (ch && (stcb) && netp && (*netp)) {
5551 if (stcb->asoc.pktdrop_supported == 0) {
5554 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5556 min(chk_length, (sizeof(chunk_buf) - 4)));
5560 case SCTP_AUTHENTICATION:
5561 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5563 /* save the first AUTH for later processing */
5564 if (auth_skipped == 0) {
5565 auth_offset = *offset;
5566 auth_len = chk_length;
5569 /* skip this chunk (temporarily) */
5572 if (stcb->asoc.auth_supported == 0) {
5575 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5576 (chk_length > (sizeof(struct sctp_auth_chunk) +
5577 SCTP_AUTH_DIGEST_LEN_MAX))) {
5580 SCTP_TCB_UNLOCK(locked_tcb);
5585 if (got_auth == 1) {
5586 /* skip this chunk... it's already auth'd */
5590 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5592 /* auth HMAC failed so dump the packet */
5596 /* remaining chunks are HMAC checked */
5597 stcb->asoc.authenticated = 1;
5603 /* it's an unknown chunk! */
5604 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5605 struct sctp_gen_error_cause *cause;
5608 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5609 0, M_NOWAIT, 1, MT_DATA);
5610 if (op_err != NULL) {
5611 len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset));
5612 cause = mtod(op_err, struct sctp_gen_error_cause *);
5613 cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5614 cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause)));
5615 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
5616 SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
5617 if (SCTP_BUF_NEXT(op_err) != NULL) {
5618 #ifdef SCTP_MBUF_LOGGING
5619 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5620 sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5623 sctp_queue_op_err(stcb, op_err);
5625 sctp_m_freem(op_err);
5629 if ((ch->chunk_type & 0x80) == 0) {
5630 /* discard this packet */
5633 } /* else skip this bad chunk and continue... */
5635 } /* switch (ch->chunk_type) */
5639 /* get the next chunk */
5640 *offset += SCTP_SIZE32(chk_length);
5641 if (*offset >= length) {
5642 /* no more data left in the mbuf chain */
5645 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5646 sizeof(struct sctp_chunkhdr), chunk_buf);
5649 SCTP_TCB_UNLOCK(locked_tcb);
5656 if (asconf_cnt > 0 && stcb != NULL) {
5657 sctp_send_asconf_ack(stcb);
5664 * common input chunk processing (v4 and v6)
5667 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5668 struct sockaddr *src, struct sockaddr *dst,
5669 struct sctphdr *sh, struct sctp_chunkhdr *ch,
5670 #if !defined(SCTP_WITH_NO_CSUM)
5671 uint8_t compute_crc,
5674 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5675 uint32_t vrf_id, uint16_t port)
5678 int fwd_tsn_seen = 0, data_processed = 0;
5679 struct mbuf *m = *mm, *op_err;
5680 char msg[SCTP_DIAG_INFO_LEN];
5682 int cnt_ctrl_ready = 0;
5683 struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5684 struct sctp_tcb *stcb = NULL;
5685 struct sctp_nets *net = NULL;
5687 SCTP_STAT_INCR(sctps_recvdatagrams);
5688 #ifdef SCTP_AUDITING_ENABLED
5689 sctp_audit_log(0xE0, 1);
5690 sctp_auditing(0, inp, stcb, net);
5692 #if !defined(SCTP_WITH_NO_CSUM)
5693 if (compute_crc != 0) {
5694 uint32_t check, calc_check;
5696 check = sh->checksum;
5698 calc_check = sctp_calculate_cksum(m, iphlen);
5699 sh->checksum = check;
5700 if (calc_check != check) {
5701 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5702 calc_check, check, (void *)m, length, iphlen);
5703 stcb = sctp_findassociation_addr(m, offset, src, dst,
5704 sh, ch, &inp, &net, vrf_id);
5705 #if defined(INET) || defined(INET6)
5706 if ((ch->chunk_type != SCTP_INITIATION) &&
5707 (net != NULL) && (net->port != port)) {
5708 if (net->port == 0) {
5709 /* UDP encapsulation turned on. */
5710 net->mtu -= sizeof(struct udphdr);
5711 if (stcb->asoc.smallest_mtu > net->mtu) {
5712 sctp_pathmtu_adjustment(stcb, net->mtu);
5714 } else if (port == 0) {
5715 /* UDP encapsulation turned off. */
5716 net->mtu += sizeof(struct udphdr);
5717 /* XXX Update smallest_mtu */
5723 net->flowtype = mflowtype;
5724 net->flowid = mflowid;
5726 if ((inp != NULL) && (stcb != NULL)) {
5727 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5728 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5729 } else if ((inp != NULL) && (stcb == NULL)) {
5732 SCTP_STAT_INCR(sctps_badsum);
5733 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5738 /* Destination port of 0 is illegal, based on RFC4960. */
5739 if (sh->dest_port == 0) {
5740 SCTP_STAT_INCR(sctps_hdrops);
5743 stcb = sctp_findassociation_addr(m, offset, src, dst,
5744 sh, ch, &inp, &net, vrf_id);
5745 #if defined(INET) || defined(INET6)
5746 if ((ch->chunk_type != SCTP_INITIATION) &&
5747 (net != NULL) && (net->port != port)) {
5748 if (net->port == 0) {
5749 /* UDP encapsulation turned on. */
5750 net->mtu -= sizeof(struct udphdr);
5751 if (stcb->asoc.smallest_mtu > net->mtu) {
5752 sctp_pathmtu_adjustment(stcb, net->mtu);
5754 } else if (port == 0) {
5755 /* UDP encapsulation turned off. */
5756 net->mtu += sizeof(struct udphdr);
5757 /* XXX Update smallest_mtu */
5763 net->flowtype = mflowtype;
5764 net->flowid = mflowid;
5767 SCTP_STAT_INCR(sctps_noport);
5768 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5771 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5772 sctp_send_shutdown_complete2(src, dst, sh,
5773 mflowtype, mflowid, fibnum,
5777 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5780 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5781 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5782 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5783 (ch->chunk_type != SCTP_INIT))) {
5784 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5786 sctp_send_abort(m, iphlen, src, dst,
5788 mflowtype, mflowid, fibnum,
5793 } else if (stcb == NULL) {
5796 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5797 (void *)m, iphlen, offset, length, (void *)stcb);
5799 /* always clear this before beginning a packet */
5800 stcb->asoc.authenticated = 0;
5801 stcb->asoc.seen_a_sack_this_pkt = 0;
5802 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5803 (void *)stcb, stcb->asoc.state);
5805 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5806 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5808 * If we hit here, we had a ref count
5809 * up when the assoc was aborted and the
5810 * timer is clearing out the assoc, we should
5811 * NOT respond to any packet.. its OOTB.
5813 SCTP_TCB_UNLOCK(stcb);
5815 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5816 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5818 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5819 mflowtype, mflowid, inp->fibnum,
5824 if (IS_SCTP_CONTROL(ch)) {
5825 /* process the control portion of the SCTP packet */
5826 /* sa_ignore NO_NULL_CHK */
5827 stcb = sctp_process_control(m, iphlen, &offset, length,
5829 inp, stcb, &net, &fwd_tsn_seen,
5830 mflowtype, mflowid, fibnum,
5834 * This covers us if the cookie-echo was there and
5835 * it changes our INP.
5837 inp = stcb->sctp_ep;
5838 #if defined(INET) || defined(INET6)
5839 if ((ch->chunk_type != SCTP_INITIATION) &&
5840 (net != NULL) && (net->port != port)) {
5841 if (net->port == 0) {
5842 /* UDP encapsulation turned on. */
5843 net->mtu -= sizeof(struct udphdr);
5844 if (stcb->asoc.smallest_mtu > net->mtu) {
5845 sctp_pathmtu_adjustment(stcb, net->mtu);
5847 } else if (port == 0) {
5848 /* UDP encapsulation turned off. */
5849 net->mtu += sizeof(struct udphdr);
5850 /* XXX Update smallest_mtu */
5858 * no control chunks, so pre-process DATA chunks (these
5859 * checks are taken care of by control processing)
5863 * if DATA only packet, and auth is required, then punt...
5864 * can't have authenticated without any AUTH (control)
5867 if ((stcb != NULL) &&
5868 (stcb->asoc.auth_supported == 1) &&
5869 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5870 /* "silently" ignore */
5871 SCTP_STAT_INCR(sctps_recvauthmissing);
5875 /* out of the blue DATA chunk */
5876 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5877 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5879 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5880 mflowtype, mflowid, fibnum,
5884 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5885 /* v_tag mismatch! */
5886 SCTP_STAT_INCR(sctps_badvtag);
5893 * no valid TCB for this packet, or we found it's a bad
5894 * packet while processing control, or we're done with this
5895 * packet (done or skip rest of data), so we drop it...
5900 * DATA chunk processing
5902 /* plow through the data chunks while length > offset */
5905 * Rest should be DATA only. Check authentication state if AUTH for
5908 if ((length > offset) &&
5910 (stcb->asoc.auth_supported == 1) &&
5911 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5912 !stcb->asoc.authenticated) {
5913 /* "silently" ignore */
5914 SCTP_STAT_INCR(sctps_recvauthmissing);
5915 SCTPDBG(SCTP_DEBUG_AUTH1,
5916 "Data chunk requires AUTH, skipped\n");
5919 if (length > offset) {
5923 * First check to make sure our state is correct. We would
5924 * not get here unless we really did have a tag, so we don't
5925 * abort if this happens, just dump the chunk silently.
5927 switch (SCTP_GET_STATE(&stcb->asoc)) {
5928 case SCTP_STATE_COOKIE_ECHOED:
5930 * we consider data with valid tags in this state
5931 * shows us the cookie-ack was lost. Imply it was
5934 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5935 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5936 stcb->asoc.overall_error_count,
5938 SCTP_FROM_SCTP_INPUT,
5941 stcb->asoc.overall_error_count = 0;
5942 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5944 case SCTP_STATE_COOKIE_WAIT:
5946 * We consider OOTB any data sent during asoc setup.
5948 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5949 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5951 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5952 mflowtype, mflowid, inp->fibnum,
5955 /* sa_ignore NOTREACHED */
5957 case SCTP_STATE_EMPTY: /* should not happen */
5958 case SCTP_STATE_INUSE: /* should not happen */
5959 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5960 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5963 /* sa_ignore NOTREACHED */
5965 case SCTP_STATE_OPEN:
5966 case SCTP_STATE_SHUTDOWN_SENT:
5969 /* plow through the data chunks while length > offset */
5970 retval = sctp_process_data(mm, iphlen, &offset, length,
5971 inp, stcb, net, &high_tsn);
5974 * The association aborted, NO UNLOCK needed since
5975 * the association is destroyed.
5982 * Anything important needs to have been m_copy'ed in
5986 /* take care of ecn */
5987 if ((data_processed == 1) &&
5988 (stcb->asoc.ecn_supported == 1) &&
5989 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5990 /* Yep, we need to add a ECNE */
5991 sctp_send_ecn_echo(stcb, net, high_tsn);
5993 if ((data_processed == 0) && (fwd_tsn_seen)) {
5995 uint32_t highest_tsn;
5997 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5998 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
6000 highest_tsn = stcb->asoc.highest_tsn_inside_map;
6002 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
6003 stcb->asoc.send_sack = 1;
6004 sctp_sack_check(stcb, was_a_gap);
6005 } else if (fwd_tsn_seen) {
6006 stcb->asoc.send_sack = 1;
6008 /* trigger send of any chunks in queue... */
6010 #ifdef SCTP_AUDITING_ENABLED
6011 sctp_audit_log(0xE0, 2);
6012 sctp_auditing(1, inp, stcb, net);
6014 SCTPDBG(SCTP_DEBUG_INPUT1,
6015 "Check for chunk output prw:%d tqe:%d tf=%d\n",
6016 stcb->asoc.peers_rwnd,
6017 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
6018 stcb->asoc.total_flight);
6019 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
6020 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
6021 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
6023 if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
6025 stcb->asoc.trigger_reset ||
6027 (stcb->asoc.peers_rwnd > 0 ||
6028 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
6029 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
6030 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
6031 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
6033 #ifdef SCTP_AUDITING_ENABLED
6034 sctp_audit_log(0xE0, 3);
6035 sctp_auditing(2, inp, stcb, net);
6039 SCTP_TCB_UNLOCK(stcb);
6041 if (inp_decr != NULL) {
6042 /* reduce ref-count */
6043 SCTP_INP_WLOCK(inp_decr);
6044 SCTP_INP_DECR_REF(inp_decr);
6045 SCTP_INP_WUNLOCK(inp_decr);
6052 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
6056 uint32_t vrf_id = 0;
6058 struct sockaddr_in src, dst;
6061 struct sctp_chunkhdr *ch;
6063 #if !defined(SCTP_WITH_NO_CSUM)
6064 uint8_t compute_crc;
6071 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
6072 SCTP_RELEASE_PKT(i_pak);
6075 m = SCTP_HEADER_TO_CHAIN(i_pak);
6076 #ifdef SCTP_MBUF_LOGGING
6077 /* Log in any input mbufs */
6078 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6079 sctp_log_mbc(m, SCTP_MBUF_INPUT);
6082 #ifdef SCTP_PACKET_LOGGING
6083 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
6087 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6088 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
6090 if_name(m->m_pkthdr.rcvif),
6091 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
6092 mflowid = m->m_pkthdr.flowid;
6093 mflowtype = M_HASHTYPE_GET(m);
6094 fibnum = M_GETFIB(m);
6095 SCTP_STAT_INCR(sctps_recvpackets);
6096 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
6097 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
6098 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6099 if (SCTP_BUF_LEN(m) < offset) {
6100 if ((m = m_pullup(m, offset)) == NULL) {
6101 SCTP_STAT_INCR(sctps_hdrops);
6105 ip = mtod(m, struct ip *);
6106 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
6107 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
6108 offset -= sizeof(struct sctp_chunkhdr);
6109 memset(&src, 0, sizeof(struct sockaddr_in));
6110 src.sin_family = AF_INET;
6111 src.sin_len = sizeof(struct sockaddr_in);
6112 src.sin_port = sh->src_port;
6113 src.sin_addr = ip->ip_src;
6114 memset(&dst, 0, sizeof(struct sockaddr_in));
6115 dst.sin_family = AF_INET;
6116 dst.sin_len = sizeof(struct sockaddr_in);
6117 dst.sin_port = sh->dest_port;
6118 dst.sin_addr = ip->ip_dst;
6119 length = ntohs(ip->ip_len);
6120 /* Validate mbuf chain length with IP payload length. */
6121 if (SCTP_HEADER_LEN(m) != length) {
6122 SCTPDBG(SCTP_DEBUG_INPUT1,
6123 "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
6124 SCTP_STAT_INCR(sctps_hdrops);
6127 /* SCTP does not allow broadcasts or multicasts */
6128 if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
6131 if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
6134 ecn_bits = ip->ip_tos;
6135 #if defined(SCTP_WITH_NO_CSUM)
6136 SCTP_STAT_INCR(sctps_recvnocrc);
6138 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
6139 SCTP_STAT_INCR(sctps_recvhwcrc);
6142 SCTP_STAT_INCR(sctps_recvswcrc);
6146 sctp_common_input_processing(&m, iphlen, offset, length,
6147 (struct sockaddr *)&src,
6148 (struct sockaddr *)&dst,
6150 #if !defined(SCTP_WITH_NO_CSUM)
6154 mflowtype, mflowid, fibnum,
6163 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6164 extern int *sctp_cpuarry;
6168 sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
6175 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6181 uint32_t flowid, tag;
6183 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
6184 flowid = m->m_pkthdr.flowid;
6187 * No flow id built by lower layers fix it so we
6190 offset = off + sizeof(struct sctphdr);
6191 if (SCTP_BUF_LEN(m) < offset) {
6192 if ((m = m_pullup(m, offset)) == NULL) {
6193 SCTP_STAT_INCR(sctps_hdrops);
6194 return (IPPROTO_DONE);
6197 ip = mtod(m, struct ip *);
6198 sh = (struct sctphdr *)((caddr_t)ip + off);
6199 tag = htonl(sh->v_tag);
6200 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6201 m->m_pkthdr.flowid = flowid;
6202 M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH);
6204 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6205 sctp_queue_to_mcore(m, off, cpu_to_use);
6206 return (IPPROTO_DONE);
6209 sctp_input_with_port(m, off, 0);
6210 return (IPPROTO_DONE);