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 listening socket. */
165 if ((stcb == NULL) &&
166 ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
167 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
168 (!SCTP_IS_LISTENING(inp)))) {
170 * FIX ME ?? What about TCP model and we have a
171 * match/restart case? Actually no fix is needed. the lookup
172 * will always find the existing assoc so stcb would not be
173 * NULL. It may be questionable to do this since we COULD
174 * just send back the INIT-ACK and hope that the app did
175 * accept()'s by the time the COOKIE was sent. But there is
176 * a price to pay for COOKIE generation and I don't want to
177 * pay it on the chance that the app will actually do some
178 * accepts(). The App just looses and should NOT be in this
181 if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
182 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
184 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
185 mflowtype, mflowid, inp->fibnum,
190 if ((stcb != NULL) &&
191 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
192 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
193 sctp_send_shutdown_ack(stcb, NULL);
194 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
196 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
197 sctp_send_initiate_ack(inp, stcb, net, m, iphlen, offset,
204 SCTP_INP_RUNLOCK(inp);
209 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
213 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
214 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
221 struct sctp_stream_queue_pending *sp;
222 struct sctp_association *asoc;
225 * This function returns if any stream has true unsent data on it.
226 * Note that as it looks through it will clean up any places that
227 * have old data that has been sent but left at top of stream queue.
231 SCTP_TCB_SEND_LOCK(stcb);
232 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
233 /* Check to see if some data queued */
234 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
235 /* sa_ignore FREED_MEMORY */
236 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
240 if ((sp->msg_is_complete) &&
242 (sp->sender_all_done)) {
244 * We are doing differed cleanup. Last time
245 * through when we took all the data the
246 * sender_all_done was not set.
248 if (sp->put_last_out == 0) {
249 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
250 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
256 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
257 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
258 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, &asoc->strmout[i], sp, 1);
260 sctp_free_remote_addr(sp->net);
264 sctp_m_freem(sp->data);
267 sctp_free_a_strmoq(stcb, sp, so_locked);
268 if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
274 if (unsent_data > 0) {
279 SCTP_TCB_SEND_UNLOCK(stcb);
280 return (unsent_data);
284 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
286 struct sctp_init *init;
287 struct sctp_association *asoc;
288 struct sctp_nets *lnet;
293 /* save off parameters */
294 asoc->peer_vtag = ntohl(init->initiate_tag);
295 asoc->peers_rwnd = ntohl(init->a_rwnd);
297 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
299 if (!TAILQ_EMPTY(&asoc->nets)) {
300 /* update any ssthresh's that may have a default */
301 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
302 lnet->ssthresh = asoc->peers_rwnd;
303 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
304 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
308 SCTP_TCB_SEND_LOCK(stcb);
309 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
311 struct sctp_stream_out *outs;
312 struct sctp_stream_queue_pending *sp, *nsp;
313 struct sctp_tmit_chunk *chk, *nchk;
315 /* abandon the upper streams */
316 newcnt = ntohs(init->num_inbound_streams);
317 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
318 if (chk->rec.data.sid >= newcnt) {
319 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
320 asoc->send_queue_cnt--;
321 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
322 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
325 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
328 if (chk->data != NULL) {
329 sctp_free_bufspace(stcb, asoc, chk, 1);
330 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
331 0, chk, SCTP_SO_NOT_LOCKED);
333 sctp_m_freem(chk->data);
337 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
338 /* sa_ignore FREED_MEMORY */
342 for (i = newcnt; i < asoc->pre_open_streams; i++) {
343 outs = &asoc->strmout[i];
344 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
345 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
346 TAILQ_REMOVE(&outs->outqueue, sp, next);
347 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
348 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
349 stcb, 0, sp, SCTP_SO_NOT_LOCKED);
351 sctp_m_freem(sp->data);
355 sctp_free_remote_addr(sp->net);
359 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
360 /* sa_ignore FREED_MEMORY */
362 outs->state = SCTP_STREAM_CLOSED;
365 /* cut back the count */
366 asoc->pre_open_streams = newcnt;
368 SCTP_TCB_SEND_UNLOCK(stcb);
369 asoc->streamoutcnt = asoc->pre_open_streams;
371 for (i = 0; i < asoc->streamoutcnt; i++) {
372 asoc->strmout[i].state = SCTP_STREAM_OPEN;
375 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
376 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
377 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
378 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
380 /* This is the next one we expect */
381 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
383 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
384 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
386 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
387 /* open the requested streams */
389 if (asoc->strmin != NULL) {
390 /* Free the old ones */
391 for (i = 0; i < asoc->streamincnt; i++) {
392 sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
393 sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
395 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
397 if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
398 asoc->streamincnt = ntohs(init->num_outbound_streams);
400 asoc->streamincnt = asoc->max_inbound_streams;
402 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
403 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
404 if (asoc->strmin == NULL) {
405 /* we didn't get memory for the streams! */
406 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
409 for (i = 0; i < asoc->streamincnt; i++) {
410 asoc->strmin[i].sid = i;
411 asoc->strmin[i].last_mid_delivered = 0xffffffff;
412 TAILQ_INIT(&asoc->strmin[i].inqueue);
413 TAILQ_INIT(&asoc->strmin[i].uno_inqueue);
414 asoc->strmin[i].pd_api_started = 0;
415 asoc->strmin[i].delivery_started = 0;
418 * load_address_from_init will put the addresses into the
419 * association when the COOKIE is processed or the INIT-ACK is
420 * processed. Both types of COOKIE's existing and new call this
421 * routine. It will remove addresses that are no longer in the
422 * association (for the restarting case where addresses are
423 * removed). Up front when the INIT arrives we will discard it if it
424 * is a restart and new addresses have been added.
426 /* sa_ignore MEMLEAK */
431 * INIT-ACK message processing/consumption returns value < 0 on error
434 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
435 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
436 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
437 struct sctp_nets *net, int *abort_no_unlock,
438 uint8_t mflowtype, uint32_t mflowid,
441 struct sctp_association *asoc;
443 int retval, abort_flag;
444 uint32_t initack_limit;
445 int nat_friendly = 0;
447 /* First verify that we have no illegal param's */
450 op_err = sctp_arethere_unrecognized_parameters(m,
451 (offset + sizeof(struct sctp_init_chunk)),
452 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
454 /* Send an abort and notify peer */
455 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
456 *abort_no_unlock = 1;
460 asoc->peer_supports_nat = (uint8_t)nat_friendly;
461 /* process the peer's parameters in the INIT-ACK */
462 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
466 initack_limit = offset + ntohs(cp->ch.chunk_length);
467 /* load all addresses */
468 if ((retval = sctp_load_addresses_from_init(stcb, m,
469 (offset + sizeof(struct sctp_init_chunk)), initack_limit,
470 src, dst, NULL, stcb->asoc.port))) {
471 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
472 "Problem with address parameters");
473 SCTPDBG(SCTP_DEBUG_INPUT1,
474 "Load addresses from INIT causes an abort %d\n",
476 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
477 src, dst, sh, op_err,
480 *abort_no_unlock = 1;
483 /* if the peer doesn't support asconf, flush the asconf queue */
484 if (asoc->asconf_supported == 0) {
485 struct sctp_asconf_addr *param, *nparam;
487 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
488 TAILQ_REMOVE(&asoc->asconf_queue, param, next);
489 SCTP_FREE(param, SCTP_M_ASC_ADDR);
492 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
493 stcb->asoc.local_hmacs);
495 sctp_queue_op_err(stcb, op_err);
496 /* queuing will steal away the mbuf chain to the out queue */
499 /* extract the cookie and queue it to "echo" it back... */
500 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
501 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
502 stcb->asoc.overall_error_count,
504 SCTP_FROM_SCTP_INPUT,
507 stcb->asoc.overall_error_count = 0;
508 net->error_count = 0;
511 * Cancel the INIT timer, We do this first before queueing the
512 * cookie. We always cancel at the primary to assue that we are
513 * canceling the timer started by the INIT which always goes to the
516 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
517 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
519 /* calculate the RTO */
520 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy,
521 SCTP_RTT_FROM_NON_DATA);
522 retval = sctp_send_cookie_echo(m, offset, stcb, net);
525 * No cookie, we probably should send a op error. But in any
526 * case if there is no cookie in the INIT-ACK, we can
527 * abandon the peer, its broke.
532 len = (uint16_t)(sizeof(struct sctp_error_missing_param) + sizeof(uint16_t));
533 /* We abort with an error of missing mandatory param */
534 op_err = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
535 if (op_err != NULL) {
536 struct sctp_error_missing_param *cause;
538 SCTP_BUF_LEN(op_err) = len;
539 cause = mtod(op_err, struct sctp_error_missing_param *);
540 /* Subtract the reserved param */
541 cause->cause.code = htons(SCTP_CAUSE_MISSING_PARAM);
542 cause->cause.length = htons(len);
543 cause->num_missing_params = htonl(1);
544 cause->type[0] = htons(SCTP_STATE_COOKIE);
546 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
547 src, dst, sh, op_err,
550 *abort_no_unlock = 1;
558 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
559 struct sctp_tcb *stcb, struct sctp_nets *net)
561 union sctp_sockstore store;
562 struct sctp_nets *r_net, *f_net;
565 uint16_t old_error_counter;
567 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
571 memset(&store, 0, sizeof(store));
572 switch (cp->heartbeat.hb_info.addr_family) {
575 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
576 store.sin.sin_family = cp->heartbeat.hb_info.addr_family;
577 store.sin.sin_len = cp->heartbeat.hb_info.addr_len;
578 store.sin.sin_port = stcb->rport;
579 memcpy(&store.sin.sin_addr, cp->heartbeat.hb_info.address,
580 sizeof(store.sin.sin_addr));
588 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
589 store.sin6.sin6_family = cp->heartbeat.hb_info.addr_family;
590 store.sin6.sin6_len = cp->heartbeat.hb_info.addr_len;
591 store.sin6.sin6_port = stcb->rport;
592 memcpy(&store.sin6.sin6_addr, cp->heartbeat.hb_info.address, sizeof(struct in6_addr));
601 r_net = sctp_findnet(stcb, &store.sa);
603 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
606 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
607 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
608 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
610 * If the its a HB and it's random value is correct when can
611 * confirm the destination.
613 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
614 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
615 stcb->asoc.primary_destination = r_net;
616 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
617 f_net = TAILQ_FIRST(&stcb->asoc.nets);
618 if (f_net != r_net) {
620 * first one on the list is NOT the primary
621 * sctp_cmpaddr() is much more efficient if
622 * the primary is the first on the list,
625 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
626 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
630 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
631 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
632 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
633 r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
634 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
636 old_error_counter = r_net->error_count;
637 r_net->error_count = 0;
638 r_net->hb_responded = 1;
639 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
640 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
641 /* Now lets do a RTO with this */
642 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy,
643 SCTP_RTT_FROM_NON_DATA);
644 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
645 r_net->dest_state |= SCTP_ADDR_REACHABLE;
646 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
647 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
649 if (r_net->dest_state & SCTP_ADDR_PF) {
650 r_net->dest_state &= ~SCTP_ADDR_PF;
651 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
653 if (old_error_counter > 0) {
654 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
655 stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
656 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
658 if (r_net == stcb->asoc.primary_destination) {
659 if (stcb->asoc.alternate) {
660 /* release the alternate, primary is good */
661 sctp_free_remote_addr(stcb->asoc.alternate);
662 stcb->asoc.alternate = NULL;
665 /* Mobility adaptation */
667 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
668 SCTP_MOBILITY_BASE) ||
669 sctp_is_mobility_feature_on(stcb->sctp_ep,
670 SCTP_MOBILITY_FASTHANDOFF)) &&
671 sctp_is_mobility_feature_on(stcb->sctp_ep,
672 SCTP_MOBILITY_PRIM_DELETED)) {
674 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
675 stcb->sctp_ep, stcb, NULL,
676 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
677 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
678 SCTP_MOBILITY_FASTHANDOFF)) {
679 sctp_assoc_immediate_retrans(stcb,
680 stcb->asoc.primary_destination);
682 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
683 SCTP_MOBILITY_BASE)) {
684 sctp_move_chunks_from_net(stcb,
685 stcb->asoc.deleted_primary);
687 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
688 stcb->asoc.deleted_primary);
694 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
697 * return 0 means we want you to proceed with the abort non-zero
698 * means no abort processing
700 struct sctpasochead *head;
702 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) ||
703 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
704 atomic_add_int(&stcb->asoc.refcnt, 1);
705 SCTP_TCB_UNLOCK(stcb);
706 SCTP_INP_INFO_WLOCK();
708 atomic_subtract_int(&stcb->asoc.refcnt, 1);
710 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
711 /* generate a new vtag and send init */
712 LIST_REMOVE(stcb, sctp_asocs);
713 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
714 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
716 * put it in the bucket in the vtag hash of assoc's for the
719 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
720 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
721 SCTP_INP_INFO_WUNLOCK();
724 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
726 * treat like a case where the cookie expired i.e.: - dump
727 * current cookie. - generate a new vtag. - resend init.
729 /* generate a new vtag and send init */
730 LIST_REMOVE(stcb, sctp_asocs);
731 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
732 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
733 sctp_stop_all_cookie_timers(stcb);
734 sctp_toss_old_cookies(stcb, &stcb->asoc);
735 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
736 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
738 * put it in the bucket in the vtag hash of assoc's for the
741 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
742 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
743 SCTP_INP_INFO_WUNLOCK();
750 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
751 struct sctp_nets *net)
754 * return 0 means we want you to proceed with the abort non-zero
755 * means no abort processing
757 if (stcb->asoc.auth_supported == 0) {
758 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
761 sctp_asconf_send_nat_state_update(stcb, net);
767 sctp_handle_abort(struct sctp_abort_chunk *abort,
768 struct sctp_tcb *stcb, struct sctp_nets *net)
770 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
776 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
780 len = ntohs(abort->ch.chunk_length);
781 if (len > sizeof(struct sctp_chunkhdr)) {
783 * Need to check the cause codes for our two magic nat
784 * aborts which don't kill the assoc necessarily.
786 struct sctp_gen_error_cause *cause;
788 cause = (struct sctp_gen_error_cause *)(abort + 1);
789 error = ntohs(cause->code);
790 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
791 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
792 abort->ch.chunk_flags);
793 if (sctp_handle_nat_colliding_state(stcb)) {
796 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
797 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
798 abort->ch.chunk_flags);
799 if (sctp_handle_nat_missing_state(stcb, net)) {
806 /* stop any receive timers */
807 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net,
808 SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
809 /* notify user of the abort and clean up... */
810 sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
812 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
813 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
814 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
815 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
817 #ifdef SCTP_ASOCLOG_OF_TSNS
818 sctp_print_out_track_log(stcb);
820 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
821 so = SCTP_INP_SO(stcb->sctp_ep);
822 atomic_add_int(&stcb->asoc.refcnt, 1);
823 SCTP_TCB_UNLOCK(stcb);
824 SCTP_SOCKET_LOCK(so, 1);
826 atomic_subtract_int(&stcb->asoc.refcnt, 1);
828 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
829 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
830 SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
831 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
832 SCTP_SOCKET_UNLOCK(so, 1);
834 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
838 sctp_start_net_timers(struct sctp_tcb *stcb)
840 uint32_t cnt_hb_sent;
841 struct sctp_nets *net;
844 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
846 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
847 * If the dest in unconfirmed send a hb as well if under
848 * max_hb_burst have been sent.
850 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
851 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
852 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
853 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
854 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
859 sctp_chunk_output(stcb->sctp_ep, stcb,
860 SCTP_OUTPUT_FROM_COOKIE_ACK,
867 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
868 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
870 struct sctp_association *asoc;
871 int some_on_streamwheel;
873 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
877 SCTPDBG(SCTP_DEBUG_INPUT2,
878 "sctp_handle_shutdown: handling SHUTDOWN\n");
882 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
883 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
886 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
887 /* Shutdown NOT the expected size */
890 old_state = SCTP_GET_STATE(asoc);
891 sctp_update_acked(stcb, cp, abort_flag);
895 if (asoc->control_pdapi) {
897 * With a normal shutdown we assume the end of last record.
899 SCTP_INP_READ_LOCK(stcb->sctp_ep);
900 if (asoc->control_pdapi->on_strm_q) {
901 struct sctp_stream_in *strm;
903 strm = &asoc->strmin[asoc->control_pdapi->sinfo_stream];
904 if (asoc->control_pdapi->on_strm_q == SCTP_ON_UNORDERED) {
906 TAILQ_REMOVE(&strm->uno_inqueue, asoc->control_pdapi, next_instrm);
907 asoc->control_pdapi->on_strm_q = 0;
908 } else if (asoc->control_pdapi->on_strm_q == SCTP_ON_ORDERED) {
910 TAILQ_REMOVE(&strm->inqueue, asoc->control_pdapi, next_instrm);
911 asoc->control_pdapi->on_strm_q = 0;
914 panic("Unknown state on ctrl:%p on_strm_q:%d",
916 asoc->control_pdapi->on_strm_q);
920 asoc->control_pdapi->end_added = 1;
921 asoc->control_pdapi->pdapi_aborted = 1;
922 asoc->control_pdapi = NULL;
923 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
924 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
925 so = SCTP_INP_SO(stcb->sctp_ep);
926 atomic_add_int(&stcb->asoc.refcnt, 1);
927 SCTP_TCB_UNLOCK(stcb);
928 SCTP_SOCKET_LOCK(so, 1);
930 atomic_subtract_int(&stcb->asoc.refcnt, 1);
931 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
932 /* assoc was freed while we were unlocked */
933 SCTP_SOCKET_UNLOCK(so, 1);
937 if (stcb->sctp_socket) {
938 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
940 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
941 SCTP_SOCKET_UNLOCK(so, 1);
944 /* goto SHUTDOWN_RECEIVED state to block new requests */
945 if (stcb->sctp_socket) {
946 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
947 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
948 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
949 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
950 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
952 * notify upper layer that peer has initiated a
955 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
958 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
961 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
963 * stop the shutdown timer, since we WILL move to
966 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
967 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
969 /* Now is there unsent data on a stream somewhere? */
970 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
972 if (!TAILQ_EMPTY(&asoc->send_queue) ||
973 !TAILQ_EMPTY(&asoc->sent_queue) ||
974 some_on_streamwheel) {
975 /* By returning we will push more data out */
978 /* no outstanding data to send, so move on... */
979 /* send SHUTDOWN-ACK */
980 /* move to SHUTDOWN-ACK-SENT state */
981 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
982 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
983 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
985 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
986 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
987 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
988 sctp_stop_timers_for_shutdown(stcb);
989 sctp_send_shutdown_ack(stcb, net);
990 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
991 stcb->sctp_ep, stcb, net);
992 } else if (old_state == SCTP_STATE_SHUTDOWN_ACK_SENT) {
993 sctp_send_shutdown_ack(stcb, net);
999 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
1000 struct sctp_tcb *stcb,
1001 struct sctp_nets *net)
1003 struct sctp_association *asoc;
1004 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1007 so = SCTP_INP_SO(stcb->sctp_ep);
1009 SCTPDBG(SCTP_DEBUG_INPUT2,
1010 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
1015 /* process according to association state */
1016 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
1017 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
1018 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
1019 sctp_send_shutdown_complete(stcb, net, 1);
1020 SCTP_TCB_UNLOCK(stcb);
1023 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
1024 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1025 /* unexpected SHUTDOWN-ACK... so ignore... */
1026 SCTP_TCB_UNLOCK(stcb);
1029 if (asoc->control_pdapi) {
1031 * With a normal shutdown we assume the end of last record.
1033 SCTP_INP_READ_LOCK(stcb->sctp_ep);
1034 asoc->control_pdapi->end_added = 1;
1035 asoc->control_pdapi->pdapi_aborted = 1;
1036 asoc->control_pdapi = NULL;
1037 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1038 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1039 atomic_add_int(&stcb->asoc.refcnt, 1);
1040 SCTP_TCB_UNLOCK(stcb);
1041 SCTP_SOCKET_LOCK(so, 1);
1042 SCTP_TCB_LOCK(stcb);
1043 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1044 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1045 /* assoc was freed while we were unlocked */
1046 SCTP_SOCKET_UNLOCK(so, 1);
1050 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1051 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1052 SCTP_SOCKET_UNLOCK(so, 1);
1056 if (!TAILQ_EMPTY(&asoc->send_queue) ||
1057 !TAILQ_EMPTY(&asoc->sent_queue) ||
1058 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
1059 panic("Queues are not empty when handling SHUTDOWN-ACK");
1062 /* stop the timer */
1063 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net,
1064 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1065 /* send SHUTDOWN-COMPLETE */
1066 sctp_send_shutdown_complete(stcb, net, 0);
1067 /* notify upper layer protocol */
1068 if (stcb->sctp_socket) {
1069 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1070 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1071 stcb->sctp_socket->so_snd.sb_cc = 0;
1073 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1075 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1076 /* free the TCB but first save off the ep */
1077 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1078 atomic_add_int(&stcb->asoc.refcnt, 1);
1079 SCTP_TCB_UNLOCK(stcb);
1080 SCTP_SOCKET_LOCK(so, 1);
1081 SCTP_TCB_LOCK(stcb);
1082 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1084 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1085 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1086 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1087 SCTP_SOCKET_UNLOCK(so, 1);
1092 * Skip past the param header and then we will find the chunk that caused the
1093 * problem. There are two possibilities ASCONF or FWD-TSN other than that and
1094 * our peer must be broken.
1097 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1098 struct sctp_nets *net)
1100 struct sctp_chunkhdr *chk;
1102 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1103 switch (chk->chunk_type) {
1104 case SCTP_ASCONF_ACK:
1106 sctp_asconf_cleanup(stcb, net);
1108 case SCTP_IFORWARD_CUM_TSN:
1109 case SCTP_FORWARD_CUM_TSN:
1110 stcb->asoc.prsctp_supported = 0;
1113 SCTPDBG(SCTP_DEBUG_INPUT2,
1114 "Peer does not support chunk type %d(%x)??\n",
1115 chk->chunk_type, (uint32_t)chk->chunk_type);
1121 * Skip past the param header and then we will find the param that caused the
1122 * problem. There are a number of param's in a ASCONF OR the prsctp param
1123 * these will turn of specific features.
1124 * XXX: Is this the right thing to do?
1127 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1129 struct sctp_paramhdr *pbad;
1132 switch (ntohs(pbad->param_type)) {
1134 case SCTP_PRSCTP_SUPPORTED:
1135 stcb->asoc.prsctp_supported = 0;
1137 case SCTP_SUPPORTED_CHUNK_EXT:
1139 /* draft-ietf-tsvwg-addip-sctp */
1140 case SCTP_HAS_NAT_SUPPORT:
1141 stcb->asoc.peer_supports_nat = 0;
1143 case SCTP_ADD_IP_ADDRESS:
1144 case SCTP_DEL_IP_ADDRESS:
1145 case SCTP_SET_PRIM_ADDR:
1146 stcb->asoc.asconf_supported = 0;
1148 case SCTP_SUCCESS_REPORT:
1149 case SCTP_ERROR_CAUSE_IND:
1150 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1151 SCTPDBG(SCTP_DEBUG_INPUT2,
1152 "Turning off ASCONF to this strange peer\n");
1153 stcb->asoc.asconf_supported = 0;
1156 SCTPDBG(SCTP_DEBUG_INPUT2,
1157 "Peer does not support param type %d(%x)??\n",
1158 pbad->param_type, (uint32_t)pbad->param_type);
1164 sctp_handle_error(struct sctp_chunkhdr *ch,
1165 struct sctp_tcb *stcb, struct sctp_nets *net)
1168 struct sctp_paramhdr *phdr;
1169 uint16_t error, error_type;
1171 struct sctp_association *asoc;
1173 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1177 /* parse through all of the errors and process */
1179 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1180 sizeof(struct sctp_chunkhdr));
1181 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1183 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1184 /* Process an Error Cause */
1185 error_type = ntohs(phdr->param_type);
1186 error_len = ntohs(phdr->param_length);
1187 if ((error_len > chklen) || (error_len == 0)) {
1188 /* invalid param length for this param */
1189 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1194 /* report the first error cause */
1197 switch (error_type) {
1198 case SCTP_CAUSE_INVALID_STREAM:
1199 case SCTP_CAUSE_MISSING_PARAM:
1200 case SCTP_CAUSE_INVALID_PARAM:
1201 case SCTP_CAUSE_NO_USER_DATA:
1202 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1205 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1206 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1208 if (sctp_handle_nat_colliding_state(stcb)) {
1212 case SCTP_CAUSE_NAT_MISSING_STATE:
1213 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1215 if (sctp_handle_nat_missing_state(stcb, net)) {
1219 case SCTP_CAUSE_STALE_COOKIE:
1221 * We only act if we have echoed a cookie and are
1224 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1227 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1228 /* Save the time doubled */
1229 asoc->cookie_preserve_req = ntohl(*p) << 1;
1230 asoc->stale_cookie_count++;
1231 if (asoc->stale_cookie_count >
1232 asoc->max_init_times) {
1233 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1234 /* now free the asoc */
1235 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1236 so = SCTP_INP_SO(stcb->sctp_ep);
1237 atomic_add_int(&stcb->asoc.refcnt, 1);
1238 SCTP_TCB_UNLOCK(stcb);
1239 SCTP_SOCKET_LOCK(so, 1);
1240 SCTP_TCB_LOCK(stcb);
1241 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1243 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1244 SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1245 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1246 SCTP_SOCKET_UNLOCK(so, 1);
1250 /* blast back to INIT state */
1251 sctp_toss_old_cookies(stcb, &stcb->asoc);
1252 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1253 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1254 sctp_stop_all_cookie_timers(stcb);
1255 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1258 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1260 * Nothing we can do here, we don't do hostname
1261 * addresses so if the peer does not like my IPv6
1262 * (or IPv4 for that matter) it does not matter. If
1263 * they don't support that type of address, they can
1264 * NOT possibly get that packet type... i.e. with no
1265 * IPv6 you can't receive a IPv6 packet. so we can
1266 * safely ignore this one. If we ever added support
1267 * for HOSTNAME Addresses, then we would need to do
1271 case SCTP_CAUSE_UNRECOG_CHUNK:
1272 sctp_process_unrecog_chunk(stcb, phdr, net);
1274 case SCTP_CAUSE_UNRECOG_PARAM:
1275 sctp_process_unrecog_param(stcb, phdr);
1277 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1279 * We ignore this since the timer will drive out a
1280 * new cookie anyway and there timer will drive us
1281 * to send a SHUTDOWN_COMPLETE. We can't send one
1282 * here since we don't have their tag.
1285 case SCTP_CAUSE_DELETING_LAST_ADDR:
1286 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1287 case SCTP_CAUSE_DELETING_SRC_ADDR:
1289 * We should NOT get these here, but in a
1292 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1295 case SCTP_CAUSE_OUT_OF_RESC:
1297 * And what, pray tell do we do with the fact that
1298 * the peer is out of resources? Not really sure we
1299 * could do anything but abort. I suspect this
1300 * should have came WITH an abort instead of in a
1305 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1309 adjust = SCTP_SIZE32(error_len);
1311 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1313 sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, error, ch, SCTP_SO_NOT_LOCKED);
1318 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1319 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1320 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1321 struct sctp_nets *net, int *abort_no_unlock,
1322 uint8_t mflowtype, uint32_t mflowid,
1325 struct sctp_init_ack *init_ack;
1326 struct mbuf *op_err;
1328 SCTPDBG(SCTP_DEBUG_INPUT2,
1329 "sctp_handle_init_ack: handling INIT-ACK\n");
1332 SCTPDBG(SCTP_DEBUG_INPUT2,
1333 "sctp_handle_init_ack: TCB is null\n");
1336 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1337 /* Invalid length */
1338 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1339 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1340 src, dst, sh, op_err,
1343 *abort_no_unlock = 1;
1346 init_ack = &cp->init;
1347 /* validate parameters */
1348 if (init_ack->initiate_tag == 0) {
1349 /* protocol error... send an abort */
1350 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1351 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1352 src, dst, sh, op_err,
1355 *abort_no_unlock = 1;
1358 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1359 /* protocol error... send an abort */
1360 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1361 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1362 src, dst, sh, op_err,
1365 *abort_no_unlock = 1;
1368 if (init_ack->num_inbound_streams == 0) {
1369 /* protocol error... send an abort */
1370 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1371 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1372 src, dst, sh, op_err,
1375 *abort_no_unlock = 1;
1378 if (init_ack->num_outbound_streams == 0) {
1379 /* protocol error... send an abort */
1380 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1381 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1382 src, dst, sh, op_err,
1385 *abort_no_unlock = 1;
1388 /* process according to association state... */
1389 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1390 case SCTP_STATE_COOKIE_WAIT:
1391 /* this is the expected state for this chunk */
1392 /* process the INIT-ACK parameters */
1393 if (stcb->asoc.primary_destination->dest_state &
1394 SCTP_ADDR_UNCONFIRMED) {
1396 * The primary is where we sent the INIT, we can
1397 * always consider it confirmed when the INIT-ACK is
1398 * returned. Do this before we load addresses
1401 stcb->asoc.primary_destination->dest_state &=
1402 ~SCTP_ADDR_UNCONFIRMED;
1403 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1404 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1406 if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1407 net, abort_no_unlock,
1410 /* error in parsing parameters */
1413 /* update our state */
1414 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1415 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1417 /* reset the RTO calc */
1418 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1419 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1420 stcb->asoc.overall_error_count,
1422 SCTP_FROM_SCTP_INPUT,
1425 stcb->asoc.overall_error_count = 0;
1426 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1428 * collapse the init timer back in case of a exponential
1431 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1434 * the send at the end of the inbound data processing will
1435 * cause the cookie to be sent
1438 case SCTP_STATE_SHUTDOWN_SENT:
1439 /* incorrect state... discard */
1441 case SCTP_STATE_COOKIE_ECHOED:
1442 /* incorrect state... discard */
1444 case SCTP_STATE_OPEN:
1445 /* incorrect state... discard */
1447 case SCTP_STATE_EMPTY:
1448 case SCTP_STATE_INUSE:
1450 /* incorrect state... discard */
1454 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1458 static struct sctp_tcb *
1459 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1460 struct sockaddr *src, struct sockaddr *dst,
1461 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1462 struct sctp_inpcb *inp, struct sctp_nets **netp,
1463 struct sockaddr *init_src, int *notification,
1464 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1465 uint8_t mflowtype, uint32_t mflowid,
1466 uint32_t vrf_id, uint16_t port);
1470 * handle a state cookie for an existing association m: input packet mbuf
1471 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1472 * "split" mbuf and the cookie signature does not exist offset: offset into
1473 * mbuf to the cookie-echo chunk
1475 static struct sctp_tcb *
1476 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1477 struct sockaddr *src, struct sockaddr *dst,
1478 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1479 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1480 struct sockaddr *init_src, int *notification,
1481 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1482 uint8_t mflowtype, uint32_t mflowid,
1483 uint32_t vrf_id, uint16_t port)
1485 struct sctp_association *asoc;
1486 struct sctp_init_chunk *init_cp, init_buf;
1487 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1488 struct sctp_nets *net;
1489 struct mbuf *op_err;
1490 int init_offset, initack_offset, i;
1494 #if defined(SCTP_DETAILED_STR_STATS)
1499 /* I know that the TCB is non-NULL from the caller */
1501 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1502 if (asoc->cookie_how[how_indx] == 0)
1505 if (how_indx < sizeof(asoc->cookie_how)) {
1506 asoc->cookie_how[how_indx] = 1;
1508 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1509 /* SHUTDOWN came in after sending INIT-ACK */
1510 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1511 op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
1512 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1513 mflowtype, mflowid, inp->fibnum,
1515 if (how_indx < sizeof(asoc->cookie_how))
1516 asoc->cookie_how[how_indx] = 2;
1520 * find and validate the INIT chunk in the cookie (peer's info) the
1521 * INIT should start after the cookie-echo header struct (chunk
1522 * header, state cookie header struct)
1524 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1526 init_cp = (struct sctp_init_chunk *)
1527 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1528 (uint8_t *)&init_buf);
1529 if (init_cp == NULL) {
1530 /* could not pull a INIT chunk in cookie */
1533 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1537 * find and validate the INIT-ACK chunk in the cookie (my info) the
1538 * INIT-ACK follows the INIT chunk
1540 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1541 initack_cp = (struct sctp_init_ack_chunk *)
1542 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1543 (uint8_t *)&initack_buf);
1544 if (initack_cp == NULL) {
1545 /* could not pull INIT-ACK chunk in cookie */
1548 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1551 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1552 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1554 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1555 * to get into the OPEN state
1557 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1559 * Opps, this means that we somehow generated two vtag's
1560 * the same. I.e. we did:
1562 * <---INIT(tag=a)------
1563 * ----INIT-ACK(tag=t)-->
1564 * ----INIT(tag=t)------> *1
1565 * <---INIT-ACK(tag=a)---
1566 * <----CE(tag=t)------------- *2
1568 * At point *1 we should be generating a different
1569 * tag t'. Which means we would throw away the CE and send
1570 * ours instead. Basically this is case C (throw away side).
1572 if (how_indx < sizeof(asoc->cookie_how))
1573 asoc->cookie_how[how_indx] = 17;
1577 switch (SCTP_GET_STATE(asoc)) {
1578 case SCTP_STATE_COOKIE_WAIT:
1579 case SCTP_STATE_COOKIE_ECHOED:
1581 * INIT was sent but got a COOKIE_ECHO with the
1582 * correct tags... just accept it...but we must
1583 * process the init so that we can make sure we have
1584 * the right seq no's.
1586 /* First we must process the INIT !! */
1587 retval = sctp_process_init(init_cp, stcb);
1589 if (how_indx < sizeof(asoc->cookie_how))
1590 asoc->cookie_how[how_indx] = 3;
1593 /* we have already processed the INIT so no problem */
1594 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp,
1596 SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1597 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp,
1599 SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1600 /* update current state */
1601 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1602 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1604 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1606 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1607 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1608 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1609 stcb->sctp_ep, stcb, asoc->primary_destination);
1611 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1612 sctp_stop_all_cookie_timers(stcb);
1613 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1614 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1615 (!SCTP_IS_LISTENING(inp))) {
1616 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1620 * Here is where collision would go if we
1621 * did a connect() and instead got a
1622 * init/init-ack/cookie done before the
1623 * init-ack came back..
1625 stcb->sctp_ep->sctp_flags |=
1626 SCTP_PCB_FLAGS_CONNECTED;
1627 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1628 so = SCTP_INP_SO(stcb->sctp_ep);
1629 atomic_add_int(&stcb->asoc.refcnt, 1);
1630 SCTP_TCB_UNLOCK(stcb);
1631 SCTP_SOCKET_LOCK(so, 1);
1632 SCTP_TCB_LOCK(stcb);
1633 atomic_add_int(&stcb->asoc.refcnt, -1);
1634 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1635 SCTP_SOCKET_UNLOCK(so, 1);
1639 soisconnected(stcb->sctp_socket);
1640 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1641 SCTP_SOCKET_UNLOCK(so, 1);
1644 /* notify upper layer */
1645 *notification = SCTP_NOTIFY_ASSOC_UP;
1647 * since we did not send a HB make sure we don't
1650 net->hb_responded = 1;
1651 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1652 &cookie->time_entered,
1653 sctp_align_unsafe_makecopy,
1654 SCTP_RTT_FROM_NON_DATA);
1656 if (stcb->asoc.sctp_autoclose_ticks &&
1657 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1658 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1664 * we're in the OPEN state (or beyond), so peer must
1665 * have simply lost the COOKIE-ACK
1669 sctp_stop_all_cookie_timers(stcb);
1671 * We ignore the return code here.. not sure if we should
1672 * somehow abort.. but we do have an existing asoc. This
1673 * really should not fail.
1675 if (sctp_load_addresses_from_init(stcb, m,
1676 init_offset + sizeof(struct sctp_init_chunk),
1677 initack_offset, src, dst, init_src, stcb->asoc.port)) {
1678 if (how_indx < sizeof(asoc->cookie_how))
1679 asoc->cookie_how[how_indx] = 4;
1682 /* respond with a COOKIE-ACK */
1683 sctp_toss_old_cookies(stcb, asoc);
1684 sctp_send_cookie_ack(stcb);
1685 if (how_indx < sizeof(asoc->cookie_how))
1686 asoc->cookie_how[how_indx] = 5;
1689 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1690 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1691 cookie->tie_tag_my_vtag == 0 &&
1692 cookie->tie_tag_peer_vtag == 0) {
1694 * case C in Section 5.2.4 Table 2: XMOO silently discard
1696 if (how_indx < sizeof(asoc->cookie_how))
1697 asoc->cookie_how[how_indx] = 6;
1701 * If nat support, and the below and stcb is established, send back
1702 * a ABORT(colliding state) if we are established.
1704 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1705 (asoc->peer_supports_nat) &&
1706 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1707 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1708 (asoc->peer_vtag == 0)))) {
1710 * Special case - Peer's support nat. We may have two init's
1711 * that we gave out the same tag on since one was not
1712 * established.. i.e. we get INIT from host-1 behind the nat
1713 * and we respond tag-a, we get a INIT from host-2 behind
1714 * the nat and we get tag-a again. Then we bring up host-1
1715 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1716 * Now we have colliding state. We must send an abort here
1717 * with colliding state indication.
1719 op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
1720 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1721 mflowtype, mflowid, inp->fibnum,
1725 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1726 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1727 (asoc->peer_vtag == 0))) {
1729 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1730 * should be ok, re-accept peer info
1732 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1734 * Extension of case C. If we hit this, then the
1735 * random number generator returned the same vtag
1736 * when we first sent our INIT-ACK and when we later
1737 * sent our INIT. The side with the seq numbers that
1738 * are different will be the one that normnally
1739 * would have hit case C. This in effect "extends"
1740 * our vtags in this collision case to be 64 bits.
1741 * The same collision could occur aka you get both
1742 * vtag and seq number the same twice in a row.. but
1743 * is much less likely. If it did happen then we
1744 * would proceed through and bring up the assoc.. we
1745 * may end up with the wrong stream setup however..
1746 * which would be bad.. but there is no way to
1747 * tell.. until we send on a stream that does not
1750 if (how_indx < sizeof(asoc->cookie_how))
1751 asoc->cookie_how[how_indx] = 7;
1755 if (how_indx < sizeof(asoc->cookie_how))
1756 asoc->cookie_how[how_indx] = 8;
1757 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1758 SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1759 sctp_stop_all_cookie_timers(stcb);
1761 * since we did not send a HB make sure we don't double
1764 net->hb_responded = 1;
1765 if (stcb->asoc.sctp_autoclose_ticks &&
1766 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1767 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1770 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1771 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1773 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1775 * Ok the peer probably discarded our data (if we
1776 * echoed a cookie+data). So anything on the
1777 * sent_queue should be marked for retransmit, we
1778 * may not get something to kick us so it COULD
1779 * still take a timeout to move these.. but it can't
1780 * hurt to mark them.
1782 struct sctp_tmit_chunk *chk;
1784 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1785 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1786 chk->sent = SCTP_DATAGRAM_RESEND;
1787 sctp_flight_size_decrease(chk);
1788 sctp_total_flight_decrease(stcb, chk);
1789 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1795 /* process the INIT info (peer's info) */
1796 retval = sctp_process_init(init_cp, stcb);
1798 if (how_indx < sizeof(asoc->cookie_how))
1799 asoc->cookie_how[how_indx] = 9;
1802 if (sctp_load_addresses_from_init(stcb, m,
1803 init_offset + sizeof(struct sctp_init_chunk),
1804 initack_offset, src, dst, init_src, stcb->asoc.port)) {
1805 if (how_indx < sizeof(asoc->cookie_how))
1806 asoc->cookie_how[how_indx] = 10;
1809 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1810 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1811 *notification = SCTP_NOTIFY_ASSOC_UP;
1813 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1814 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1815 (!SCTP_IS_LISTENING(inp))) {
1816 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1819 stcb->sctp_ep->sctp_flags |=
1820 SCTP_PCB_FLAGS_CONNECTED;
1821 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1822 so = SCTP_INP_SO(stcb->sctp_ep);
1823 atomic_add_int(&stcb->asoc.refcnt, 1);
1824 SCTP_TCB_UNLOCK(stcb);
1825 SCTP_SOCKET_LOCK(so, 1);
1826 SCTP_TCB_LOCK(stcb);
1827 atomic_add_int(&stcb->asoc.refcnt, -1);
1828 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1829 SCTP_SOCKET_UNLOCK(so, 1);
1833 soisconnected(stcb->sctp_socket);
1834 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1835 SCTP_SOCKET_UNLOCK(so, 1);
1838 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1839 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1841 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1842 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1843 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1844 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1846 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1848 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1849 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1850 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1851 stcb->sctp_ep, stcb, asoc->primary_destination);
1853 sctp_stop_all_cookie_timers(stcb);
1854 sctp_toss_old_cookies(stcb, asoc);
1855 sctp_send_cookie_ack(stcb);
1858 * only if we have retrans set do we do this. What
1859 * this call does is get only the COOKIE-ACK out and
1860 * then when we return the normal call to
1861 * sctp_chunk_output will get the retrans out behind
1864 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1866 if (how_indx < sizeof(asoc->cookie_how))
1867 asoc->cookie_how[how_indx] = 11;
1871 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1872 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1873 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1874 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1875 cookie->tie_tag_peer_vtag != 0) {
1876 struct sctpasochead *head;
1877 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1881 if (asoc->peer_supports_nat) {
1883 * This is a gross gross hack. Just call the
1884 * cookie_new code since we are allowing a duplicate
1885 * association. I hope this works...
1887 return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
1888 sh, cookie, cookie_len,
1889 inp, netp, init_src, notification,
1890 auth_skipped, auth_offset, auth_len,
1895 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1898 if (how_indx < sizeof(asoc->cookie_how))
1899 asoc->cookie_how[how_indx] = 12;
1900 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
1901 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1902 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1903 SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
1905 /* notify upper layer */
1906 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1907 atomic_add_int(&stcb->asoc.refcnt, 1);
1908 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1909 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1910 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1911 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1913 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1914 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1915 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1916 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1918 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1919 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1920 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1921 stcb->sctp_ep, stcb, asoc->primary_destination);
1923 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1924 /* move to OPEN state, if not in SHUTDOWN_SENT */
1925 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1927 asoc->pre_open_streams =
1928 ntohs(initack_cp->init.num_outbound_streams);
1929 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1930 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1931 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1933 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1935 asoc->str_reset_seq_in = asoc->init_seq_number;
1937 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1938 if (asoc->mapping_array) {
1939 memset(asoc->mapping_array, 0,
1940 asoc->mapping_array_size);
1942 if (asoc->nr_mapping_array) {
1943 memset(asoc->nr_mapping_array, 0,
1944 asoc->mapping_array_size);
1946 SCTP_TCB_UNLOCK(stcb);
1947 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1948 so = SCTP_INP_SO(stcb->sctp_ep);
1949 SCTP_SOCKET_LOCK(so, 1);
1951 SCTP_INP_INFO_WLOCK();
1952 SCTP_INP_WLOCK(stcb->sctp_ep);
1953 SCTP_TCB_LOCK(stcb);
1954 atomic_add_int(&stcb->asoc.refcnt, -1);
1955 /* send up all the data */
1956 SCTP_TCB_SEND_LOCK(stcb);
1958 sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_LOCKED);
1959 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1960 stcb->asoc.strmout[i].chunks_on_queues = 0;
1961 #if defined(SCTP_DETAILED_STR_STATS)
1962 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1963 asoc->strmout[i].abandoned_sent[j] = 0;
1964 asoc->strmout[i].abandoned_unsent[j] = 0;
1967 asoc->strmout[i].abandoned_sent[0] = 0;
1968 asoc->strmout[i].abandoned_unsent[0] = 0;
1970 stcb->asoc.strmout[i].sid = i;
1971 stcb->asoc.strmout[i].next_mid_ordered = 0;
1972 stcb->asoc.strmout[i].next_mid_unordered = 0;
1973 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1975 /* process the INIT-ACK info (my info) */
1976 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1977 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1979 /* pull from vtag hash */
1980 LIST_REMOVE(stcb, sctp_asocs);
1981 /* re-insert to new vtag position */
1982 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1983 SCTP_BASE_INFO(hashasocmark))];
1985 * put it in the bucket in the vtag hash of assoc's for the
1988 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1990 SCTP_TCB_SEND_UNLOCK(stcb);
1991 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1992 SCTP_INP_INFO_WUNLOCK();
1993 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1994 SCTP_SOCKET_UNLOCK(so, 1);
1996 asoc->total_flight = 0;
1997 asoc->total_flight_count = 0;
1998 /* process the INIT info (peer's info) */
1999 retval = sctp_process_init(init_cp, stcb);
2001 if (how_indx < sizeof(asoc->cookie_how))
2002 asoc->cookie_how[how_indx] = 13;
2007 * since we did not send a HB make sure we don't double
2010 net->hb_responded = 1;
2012 if (sctp_load_addresses_from_init(stcb, m,
2013 init_offset + sizeof(struct sctp_init_chunk),
2014 initack_offset, src, dst, init_src, stcb->asoc.port)) {
2015 if (how_indx < sizeof(asoc->cookie_how))
2016 asoc->cookie_how[how_indx] = 14;
2020 /* respond with a COOKIE-ACK */
2021 sctp_stop_all_cookie_timers(stcb);
2022 sctp_toss_old_cookies(stcb, asoc);
2023 sctp_send_cookie_ack(stcb);
2024 if (how_indx < sizeof(asoc->cookie_how))
2025 asoc->cookie_how[how_indx] = 15;
2029 if (how_indx < sizeof(asoc->cookie_how))
2030 asoc->cookie_how[how_indx] = 16;
2031 /* all other cases... */
2037 * handle a state cookie for a new association m: input packet mbuf chain--
2038 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
2039 * and the cookie signature does not exist offset: offset into mbuf to the
2040 * cookie-echo chunk length: length of the cookie chunk to: where the init
2041 * was from returns a new TCB
2043 static struct sctp_tcb *
2044 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
2045 struct sockaddr *src, struct sockaddr *dst,
2046 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
2047 struct sctp_inpcb *inp, struct sctp_nets **netp,
2048 struct sockaddr *init_src, int *notification,
2049 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2050 uint8_t mflowtype, uint32_t mflowid,
2051 uint32_t vrf_id, uint16_t port)
2053 struct sctp_tcb *stcb;
2054 struct sctp_init_chunk *init_cp, init_buf;
2055 struct sctp_init_ack_chunk *initack_cp, initack_buf;
2056 union sctp_sockstore store;
2057 struct sctp_association *asoc;
2058 int init_offset, initack_offset, initack_limit;
2061 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
2062 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2065 so = SCTP_INP_SO(inp);
2069 * find and validate the INIT chunk in the cookie (peer's info) the
2070 * INIT should start after the cookie-echo header struct (chunk
2071 * header, state cookie header struct)
2073 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2074 init_cp = (struct sctp_init_chunk *)
2075 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2076 (uint8_t *)&init_buf);
2077 if (init_cp == NULL) {
2078 /* could not pull a INIT chunk in cookie */
2079 SCTPDBG(SCTP_DEBUG_INPUT1,
2080 "process_cookie_new: could not pull INIT chunk hdr\n");
2083 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2084 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2087 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2089 * find and validate the INIT-ACK chunk in the cookie (my info) the
2090 * INIT-ACK follows the INIT chunk
2092 initack_cp = (struct sctp_init_ack_chunk *)
2093 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2094 (uint8_t *)&initack_buf);
2095 if (initack_cp == NULL) {
2096 /* could not pull INIT-ACK chunk in cookie */
2097 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2100 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2104 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2105 * "initack_limit" value. This is because the chk_length field
2106 * includes the length of the cookie, but the cookie is omitted when
2107 * the INIT and INIT_ACK are tacked onto the cookie...
2109 initack_limit = offset + cookie_len;
2112 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2117 * Here we do a trick, we set in NULL for the proc/thread argument.
2118 * We do this since in effect we only use the p argument when the
2119 * socket is unbound and we must do an implicit bind. Since we are
2120 * getting a cookie, we cannot be unbound.
2122 stcb = sctp_aloc_assoc(inp, init_src, &error,
2123 ntohl(initack_cp->init.initiate_tag), vrf_id,
2124 ntohs(initack_cp->init.num_outbound_streams),
2126 (struct thread *)NULL
2129 struct mbuf *op_err;
2131 /* memory problem? */
2132 SCTPDBG(SCTP_DEBUG_INPUT1,
2133 "process_cookie_new: no room for another TCB!\n");
2134 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2135 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2136 src, dst, sh, op_err,
2141 /* get the correct sctp_nets */
2143 *netp = sctp_findnet(stcb, init_src);
2146 /* get scope variables out of cookie */
2147 asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2148 asoc->scope.site_scope = cookie->site_scope;
2149 asoc->scope.local_scope = cookie->local_scope;
2150 asoc->scope.loopback_scope = cookie->loopback_scope;
2152 if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2153 (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2154 struct mbuf *op_err;
2157 * Houston we have a problem. The EP changed while the
2158 * cookie was in flight. Only recourse is to abort the
2161 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2162 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2163 src, dst, sh, op_err,
2166 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2167 atomic_add_int(&stcb->asoc.refcnt, 1);
2168 SCTP_TCB_UNLOCK(stcb);
2169 SCTP_SOCKET_LOCK(so, 1);
2170 SCTP_TCB_LOCK(stcb);
2171 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2173 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2174 SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2175 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2176 SCTP_SOCKET_UNLOCK(so, 1);
2180 /* process the INIT-ACK info (my info) */
2181 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2182 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2183 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2184 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2185 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2186 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2187 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2188 asoc->str_reset_seq_in = asoc->init_seq_number;
2190 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2192 /* process the INIT info (peer's info) */
2194 retval = sctp_process_init(init_cp, stcb);
2198 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2199 atomic_add_int(&stcb->asoc.refcnt, 1);
2200 SCTP_TCB_UNLOCK(stcb);
2201 SCTP_SOCKET_LOCK(so, 1);
2202 SCTP_TCB_LOCK(stcb);
2203 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2205 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2206 SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2207 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2208 SCTP_SOCKET_UNLOCK(so, 1);
2212 /* load all addresses */
2213 if (sctp_load_addresses_from_init(stcb, m,
2214 init_offset + sizeof(struct sctp_init_chunk), initack_offset,
2215 src, dst, init_src, port)) {
2216 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2217 atomic_add_int(&stcb->asoc.refcnt, 1);
2218 SCTP_TCB_UNLOCK(stcb);
2219 SCTP_SOCKET_LOCK(so, 1);
2220 SCTP_TCB_LOCK(stcb);
2221 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2223 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2224 SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2225 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2226 SCTP_SOCKET_UNLOCK(so, 1);
2231 * verify any preceding AUTH chunk that was skipped
2233 /* pull the local authentication parameters from the cookie/init-ack */
2234 sctp_auth_get_cookie_params(stcb, m,
2235 initack_offset + sizeof(struct sctp_init_ack_chunk),
2236 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2238 struct sctp_auth_chunk *auth;
2240 auth = (struct sctp_auth_chunk *)
2241 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2242 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2243 /* auth HMAC failed, dump the assoc and packet */
2244 SCTPDBG(SCTP_DEBUG_AUTH1,
2245 "COOKIE-ECHO: AUTH failed\n");
2246 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2247 atomic_add_int(&stcb->asoc.refcnt, 1);
2248 SCTP_TCB_UNLOCK(stcb);
2249 SCTP_SOCKET_LOCK(so, 1);
2250 SCTP_TCB_LOCK(stcb);
2251 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2253 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2254 SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2255 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2256 SCTP_SOCKET_UNLOCK(so, 1);
2260 /* remaining chunks checked... good to go */
2261 stcb->asoc.authenticated = 1;
2265 * if we're doing ASCONFs, check to see if we have any new local
2266 * addresses that need to get added to the peer (eg. addresses
2267 * changed while cookie echo in flight). This needs to be done
2268 * after we go to the OPEN state to do the correct asconf
2269 * processing. else, make sure we have the correct addresses in our
2273 /* warning, we re-use sin, sin6, sa_store here! */
2274 /* pull in local_address (our "from" address) */
2275 switch (cookie->laddr_type) {
2277 case SCTP_IPV4_ADDRESS:
2278 /* source addr is IPv4 */
2279 memset(&store.sin, 0, sizeof(struct sockaddr_in));
2280 store.sin.sin_family = AF_INET;
2281 store.sin.sin_len = sizeof(struct sockaddr_in);
2282 store.sin.sin_addr.s_addr = cookie->laddress[0];
2286 case SCTP_IPV6_ADDRESS:
2287 /* source addr is IPv6 */
2288 memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
2289 store.sin6.sin6_family = AF_INET6;
2290 store.sin6.sin6_len = sizeof(struct sockaddr_in6);
2291 store.sin6.sin6_scope_id = cookie->scope_id;
2292 memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2296 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2297 atomic_add_int(&stcb->asoc.refcnt, 1);
2298 SCTP_TCB_UNLOCK(stcb);
2299 SCTP_SOCKET_LOCK(so, 1);
2300 SCTP_TCB_LOCK(stcb);
2301 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2303 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2304 SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2305 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2306 SCTP_SOCKET_UNLOCK(so, 1);
2311 /* update current state */
2312 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2313 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2314 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2315 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2316 stcb->sctp_ep, stcb, asoc->primary_destination);
2318 sctp_stop_all_cookie_timers(stcb);
2319 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2320 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2322 /* set up to notify upper layer */
2323 *notification = SCTP_NOTIFY_ASSOC_UP;
2324 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2325 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2326 (!SCTP_IS_LISTENING(inp))) {
2328 * This is an endpoint that called connect() how it got a
2329 * cookie that is NEW is a bit of a mystery. It must be that
2330 * the INIT was sent, but before it got there.. a complete
2331 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2332 * should have went to the other code.. not here.. oh well..
2333 * a bit of protection is worth having..
2335 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2336 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2337 atomic_add_int(&stcb->asoc.refcnt, 1);
2338 SCTP_TCB_UNLOCK(stcb);
2339 SCTP_SOCKET_LOCK(so, 1);
2340 SCTP_TCB_LOCK(stcb);
2341 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2342 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2343 SCTP_SOCKET_UNLOCK(so, 1);
2347 soisconnected(stcb->sctp_socket);
2348 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2349 SCTP_SOCKET_UNLOCK(so, 1);
2351 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2352 (SCTP_IS_LISTENING(inp))) {
2354 * We don't want to do anything with this one. Since it is
2355 * the listening guy. The timer will get started for
2356 * accepted connections in the caller.
2360 /* since we did not send a HB make sure we don't double things */
2361 if ((netp) && (*netp))
2362 (*netp)->hb_responded = 1;
2364 if (stcb->asoc.sctp_autoclose_ticks &&
2365 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2366 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2368 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2369 if ((netp != NULL) && (*netp != NULL)) {
2370 /* calculate the RTT and set the encaps port */
2371 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2372 &cookie->time_entered, sctp_align_unsafe_makecopy,
2373 SCTP_RTT_FROM_NON_DATA);
2375 /* respond with a COOKIE-ACK */
2376 sctp_send_cookie_ack(stcb);
2379 * check the address lists for any ASCONFs that need to be sent
2380 * AFTER the cookie-ack is sent
2382 sctp_check_address_list(stcb, m,
2383 initack_offset + sizeof(struct sctp_init_ack_chunk),
2384 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2385 &store.sa, cookie->local_scope, cookie->site_scope,
2386 cookie->ipv4_scope, cookie->loopback_scope);
2393 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2394 * we NEED to make sure we are not already using the vtag. If so we
2395 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2396 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2397 SCTP_BASE_INFO(hashasocmark))];
2398 LIST_FOREACH(stcb, head, sctp_asocs) {
2399 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2400 -- SEND ABORT - TRY AGAIN --
2406 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2407 * existing (non-NULL) TCB
2409 static struct mbuf *
2410 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2411 struct sockaddr *src, struct sockaddr *dst,
2412 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2413 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2414 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2415 struct sctp_tcb **locked_tcb,
2416 uint8_t mflowtype, uint32_t mflowid,
2417 uint32_t vrf_id, uint16_t port)
2419 struct sctp_state_cookie *cookie;
2420 struct sctp_tcb *l_stcb = *stcb;
2421 struct sctp_inpcb *l_inp;
2422 struct sockaddr *to;
2423 struct sctp_pcb *ep;
2425 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2427 uint8_t cookie_ok = 0;
2428 unsigned int sig_offset, cookie_offset;
2429 unsigned int cookie_len;
2431 struct timeval time_expires;
2432 int notification = 0;
2433 struct sctp_nets *netl;
2434 int had_a_existing_tcb = 0;
2435 int send_int_conf = 0;
2437 struct sockaddr_in sin;
2440 struct sockaddr_in6 sin6;
2443 SCTPDBG(SCTP_DEBUG_INPUT2,
2444 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2446 if (inp_p == NULL) {
2449 cookie = &cp->cookie;
2450 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2451 cookie_len = ntohs(cp->ch.chunk_length);
2453 if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2454 sizeof(struct sctp_init_chunk) +
2455 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2456 /* cookie too small */
2459 if ((cookie->peerport != sh->src_port) ||
2460 (cookie->myport != sh->dest_port) ||
2461 (cookie->my_vtag != sh->v_tag)) {
2463 * invalid ports or bad tag. Note that we always leave the
2464 * v_tag in the header in network order and when we stored
2465 * it in the my_vtag slot we also left it in network order.
2466 * This maintains the match even though it may be in the
2467 * opposite byte order of the machine :->
2472 * split off the signature into its own mbuf (since it should not be
2473 * calculated in the sctp_hmac_m() call).
2475 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2476 m_sig = m_split(m, sig_offset, M_NOWAIT);
2477 if (m_sig == NULL) {
2478 /* out of memory or ?? */
2481 #ifdef SCTP_MBUF_LOGGING
2482 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2483 sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
2488 * compute the signature/digest for the cookie
2490 ep = &(*inp_p)->sctp_ep;
2493 SCTP_TCB_UNLOCK(l_stcb);
2495 SCTP_INP_RLOCK(l_inp);
2497 SCTP_TCB_LOCK(l_stcb);
2499 /* which cookie is it? */
2500 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2501 (ep->current_secret_number != ep->last_secret_number)) {
2502 /* it's the old cookie */
2503 (void)sctp_hmac_m(SCTP_HMAC,
2504 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2505 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2507 /* it's the current cookie */
2508 (void)sctp_hmac_m(SCTP_HMAC,
2509 (uint8_t *)ep->secret_key[(int)ep->current_secret_number],
2510 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2512 /* get the signature */
2513 SCTP_INP_RUNLOCK(l_inp);
2514 sig = (uint8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *)&tmp_sig);
2516 /* couldn't find signature */
2517 sctp_m_freem(m_sig);
2520 /* compare the received digest with the computed digest */
2521 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2522 /* try the old cookie? */
2523 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2524 (ep->current_secret_number != ep->last_secret_number)) {
2525 /* compute digest with old */
2526 (void)sctp_hmac_m(SCTP_HMAC,
2527 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2528 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2530 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2538 * Now before we continue we must reconstruct our mbuf so that
2539 * normal processing of any other chunks will work.
2545 while (SCTP_BUF_NEXT(m_at) != NULL) {
2546 m_at = SCTP_BUF_NEXT(m_at);
2548 SCTP_BUF_NEXT(m_at) = m_sig;
2551 if (cookie_ok == 0) {
2552 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2553 SCTPDBG(SCTP_DEBUG_INPUT2,
2554 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2555 (uint32_t)offset, cookie_offset, sig_offset);
2559 * check the cookie timestamps to be sure it's not stale
2561 (void)SCTP_GETTIME_TIMEVAL(&now);
2562 /* Expire time is in Ticks, so we convert to seconds */
2563 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2564 time_expires.tv_usec = cookie->time_entered.tv_usec;
2565 if (timevalcmp(&now, &time_expires, >)) {
2566 /* cookie is stale! */
2567 struct mbuf *op_err;
2568 struct sctp_error_stale_cookie *cause;
2569 struct timeval diff;
2572 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2573 0, M_NOWAIT, 1, MT_DATA);
2574 if (op_err == NULL) {
2579 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
2580 cause = mtod(op_err, struct sctp_error_stale_cookie *);
2581 cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
2582 cause->cause.length = htons((sizeof(struct sctp_paramhdr) +
2583 (sizeof(uint32_t))));
2585 timevalsub(&diff, &time_expires);
2586 if (diff.tv_sec > UINT32_MAX / 1000000) {
2587 staleness = UINT32_MAX;
2589 staleness = diff.tv_sec * 1000000;
2591 if (UINT32_MAX - staleness >= (uint32_t)diff.tv_usec) {
2592 staleness += diff.tv_usec;
2594 staleness = UINT32_MAX;
2596 cause->stale_time = htonl(staleness);
2597 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2598 mflowtype, mflowid, l_inp->fibnum,
2603 * Now we must see with the lookup address if we have an existing
2604 * asoc. This will only happen if we were in the COOKIE-WAIT state
2605 * and a INIT collided with us and somewhere the peer sent the
2606 * cookie on another address besides the single address our assoc
2607 * had for him. In this case we will have one of the tie-tags set at
2608 * least AND the address field in the cookie can be used to look it
2612 switch (cookie->addr_type) {
2614 case SCTP_IPV6_ADDRESS:
2615 memset(&sin6, 0, sizeof(sin6));
2616 sin6.sin6_family = AF_INET6;
2617 sin6.sin6_len = sizeof(sin6);
2618 sin6.sin6_port = sh->src_port;
2619 sin6.sin6_scope_id = cookie->scope_id;
2620 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2621 sizeof(sin6.sin6_addr.s6_addr));
2622 to = (struct sockaddr *)&sin6;
2626 case SCTP_IPV4_ADDRESS:
2627 memset(&sin, 0, sizeof(sin));
2628 sin.sin_family = AF_INET;
2629 sin.sin_len = sizeof(sin);
2630 sin.sin_port = sh->src_port;
2631 sin.sin_addr.s_addr = cookie->address[0];
2632 to = (struct sockaddr *)&sin;
2636 /* This should not happen */
2639 if (*stcb == NULL) {
2640 /* Yep, lets check */
2641 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2642 if (*stcb == NULL) {
2644 * We should have only got back the same inp. If we
2645 * got back a different ep we have a problem. The
2646 * original findep got back l_inp and now
2648 if (l_inp != *inp_p) {
2649 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2652 if (*locked_tcb == NULL) {
2654 * In this case we found the assoc only
2655 * after we locked the create lock. This
2656 * means we are in a colliding case and we
2657 * must make sure that we unlock the tcb if
2658 * its one of the cases where we throw away
2659 * the incoming packets.
2661 *locked_tcb = *stcb;
2664 * We must also increment the inp ref count
2665 * since the ref_count flags was set when we
2666 * did not find the TCB, now we found it
2667 * which reduces the refcount.. we must
2668 * raise it back out to balance it all :-)
2670 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2671 if ((*stcb)->sctp_ep != l_inp) {
2672 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2673 (void *)(*stcb)->sctp_ep, (void *)l_inp);
2678 cookie_len -= SCTP_SIGNATURE_SIZE;
2679 if (*stcb == NULL) {
2680 /* this is the "normal" case... get a new TCB */
2681 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2682 cookie, cookie_len, *inp_p,
2683 netp, to, ¬ification,
2684 auth_skipped, auth_offset, auth_len,
2688 /* this is abnormal... cookie-echo on existing TCB */
2689 had_a_existing_tcb = 1;
2690 *stcb = sctp_process_cookie_existing(m, iphlen, offset,
2692 cookie, cookie_len, *inp_p, *stcb, netp, to,
2693 ¬ification, auth_skipped, auth_offset, auth_len,
2698 if (*stcb == NULL) {
2699 /* still no TCB... must be bad cookie-echo */
2702 if (*netp != NULL) {
2703 (*netp)->flowtype = mflowtype;
2704 (*netp)->flowid = mflowid;
2707 * Ok, we built an association so confirm the address we sent the
2710 netl = sctp_findnet(*stcb, to);
2712 * This code should in theory NOT run but
2715 /* TSNH! Huh, why do I need to add this address here? */
2716 if (sctp_add_remote_addr(*stcb, to, NULL, port,
2717 SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2720 netl = sctp_findnet(*stcb, to);
2723 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2724 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2725 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2730 sctp_start_net_timers(*stcb);
2731 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2732 if (!had_a_existing_tcb ||
2733 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2735 * If we have a NEW cookie or the connect never
2736 * reached the connected state during collision we
2737 * must do the TCP accept thing.
2739 struct socket *so, *oso;
2740 struct sctp_inpcb *inp;
2742 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2744 * For a restart we will keep the same
2745 * socket, no need to do anything. I THINK!!
2747 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2748 if (send_int_conf) {
2749 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2750 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2754 oso = (*inp_p)->sctp_socket;
2755 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2756 SCTP_TCB_UNLOCK((*stcb));
2757 CURVNET_SET(oso->so_vnet);
2758 so = sonewconn(oso, 0
2761 SCTP_TCB_LOCK((*stcb));
2762 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2765 struct mbuf *op_err;
2766 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2767 struct socket *pcb_so;
2769 /* Too many sockets */
2770 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2771 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2772 sctp_abort_association(*inp_p, NULL, m, iphlen,
2773 src, dst, sh, op_err,
2776 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2777 pcb_so = SCTP_INP_SO(*inp_p);
2778 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2779 SCTP_TCB_UNLOCK((*stcb));
2780 SCTP_SOCKET_LOCK(pcb_so, 1);
2781 SCTP_TCB_LOCK((*stcb));
2782 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2784 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2785 SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2786 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2787 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2791 inp = (struct sctp_inpcb *)so->so_pcb;
2792 SCTP_INP_INCR_REF(inp);
2794 * We add the unbound flag here so that if we get an
2795 * soabort() before we get the move_pcb done, we
2796 * will properly cleanup.
2798 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2799 SCTP_PCB_FLAGS_CONNECTED |
2800 SCTP_PCB_FLAGS_IN_TCPPOOL |
2801 SCTP_PCB_FLAGS_UNBOUND |
2802 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2803 SCTP_PCB_FLAGS_DONT_WAKE);
2804 inp->sctp_features = (*inp_p)->sctp_features;
2805 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2806 inp->sctp_socket = so;
2807 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2808 inp->max_cwnd = (*inp_p)->max_cwnd;
2809 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2810 inp->ecn_supported = (*inp_p)->ecn_supported;
2811 inp->prsctp_supported = (*inp_p)->prsctp_supported;
2812 inp->auth_supported = (*inp_p)->auth_supported;
2813 inp->asconf_supported = (*inp_p)->asconf_supported;
2814 inp->reconfig_supported = (*inp_p)->reconfig_supported;
2815 inp->nrsack_supported = (*inp_p)->nrsack_supported;
2816 inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2817 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2818 inp->sctp_context = (*inp_p)->sctp_context;
2819 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2820 inp->fibnum = (*inp_p)->fibnum;
2821 inp->inp_starting_point_for_iterator = NULL;
2823 * copy in the authentication parameters from the
2826 if (inp->sctp_ep.local_hmacs)
2827 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2828 inp->sctp_ep.local_hmacs =
2829 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2830 if (inp->sctp_ep.local_auth_chunks)
2831 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2832 inp->sctp_ep.local_auth_chunks =
2833 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2836 * Now we must move it from one hash table to
2837 * another and get the tcb in the right place.
2841 * This is where the one-2-one socket is put into
2842 * the accept state waiting for the accept!
2845 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2847 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2849 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2850 SCTP_TCB_UNLOCK((*stcb));
2852 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2854 SCTP_TCB_LOCK((*stcb));
2855 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2859 * now we must check to see if we were aborted while
2860 * the move was going on and the lock/unlock
2863 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2865 * yep it was, we leave the assoc attached
2866 * to the socket since the sctp_inpcb_free()
2867 * call will send an abort for us.
2869 SCTP_INP_DECR_REF(inp);
2872 SCTP_INP_DECR_REF(inp);
2873 /* Switch over to the new guy */
2875 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2876 if (send_int_conf) {
2877 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2878 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2881 * Pull it from the incomplete queue and wake the
2884 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2885 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2886 SCTP_TCB_UNLOCK((*stcb));
2887 SCTP_SOCKET_LOCK(so, 1);
2890 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2891 SCTP_TCB_LOCK((*stcb));
2892 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2893 SCTP_SOCKET_UNLOCK(so, 1);
2899 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2901 if (send_int_conf) {
2902 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2903 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2909 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2910 struct sctp_tcb *stcb, struct sctp_nets *net)
2912 /* cp must not be used, others call this without a c-ack :-) */
2913 struct sctp_association *asoc;
2915 SCTPDBG(SCTP_DEBUG_INPUT2,
2916 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2917 if ((stcb == NULL) || (net == NULL)) {
2922 sctp_stop_all_cookie_timers(stcb);
2923 /* process according to association state */
2924 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2925 /* state change only needed when I am in right state */
2926 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2927 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2928 sctp_start_net_timers(stcb);
2929 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2930 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2931 stcb->sctp_ep, stcb, asoc->primary_destination);
2935 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2936 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2937 if (asoc->overall_error_count == 0) {
2938 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2939 &asoc->time_entered, sctp_align_safe_nocopy,
2940 SCTP_RTT_FROM_NON_DATA);
2942 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2943 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2944 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2945 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2946 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2950 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2951 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2952 so = SCTP_INP_SO(stcb->sctp_ep);
2953 atomic_add_int(&stcb->asoc.refcnt, 1);
2954 SCTP_TCB_UNLOCK(stcb);
2955 SCTP_SOCKET_LOCK(so, 1);
2956 SCTP_TCB_LOCK(stcb);
2957 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2959 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2960 soisconnected(stcb->sctp_socket);
2962 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2963 SCTP_SOCKET_UNLOCK(so, 1);
2967 * since we did not send a HB make sure we don't double
2970 net->hb_responded = 1;
2972 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2974 * We don't need to do the asconf thing, nor hb or
2975 * autoclose if the socket is closed.
2979 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2983 if (stcb->asoc.sctp_autoclose_ticks &&
2984 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2985 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2986 stcb->sctp_ep, stcb, NULL);
2989 * send ASCONF if parameters are pending and ASCONFs are
2990 * allowed (eg. addresses changed when init/cookie echo were
2993 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2994 (stcb->asoc.asconf_supported == 1) &&
2995 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2996 #ifdef SCTP_TIMER_BASED_ASCONF
2997 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2998 stcb->sctp_ep, stcb,
2999 stcb->asoc.primary_destination);
3001 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
3002 SCTP_ADDR_NOT_LOCKED);
3007 /* Toss the cookie if I can */
3008 sctp_toss_old_cookies(stcb, asoc);
3009 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3010 /* Restart the timer if we have pending data */
3011 struct sctp_tmit_chunk *chk;
3013 chk = TAILQ_FIRST(&asoc->sent_queue);
3014 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
3019 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
3020 struct sctp_tcb *stcb)
3022 struct sctp_nets *net;
3023 struct sctp_tmit_chunk *lchk;
3024 struct sctp_ecne_chunk bkup;
3025 uint8_t override_bit;
3026 uint32_t tsn, window_data_tsn;
3028 unsigned int pkt_cnt;
3030 len = ntohs(cp->ch.chunk_length);
3031 if ((len != sizeof(struct sctp_ecne_chunk)) &&
3032 (len != sizeof(struct old_sctp_ecne_chunk))) {
3035 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3036 /* Its the old format */
3037 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3038 bkup.num_pkts_since_cwr = htonl(1);
3041 SCTP_STAT_INCR(sctps_recvecne);
3042 tsn = ntohl(cp->tsn);
3043 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3044 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3046 window_data_tsn = stcb->asoc.sending_seq - 1;
3048 window_data_tsn = lchk->rec.data.tsn;
3051 /* Find where it was sent to if possible. */
3053 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3054 if (lchk->rec.data.tsn == tsn) {
3056 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3059 if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) {
3065 * What to do. A previous send of a CWR was possibly lost.
3066 * See how old it is, we may have it marked on the actual
3069 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3070 if (tsn == net->last_cwr_tsn) {
3071 /* Found him, send it off */
3077 * If we reach here, we need to send a special CWR
3078 * that says hey, we did this a long time ago and
3079 * you lost the response.
3081 net = TAILQ_FIRST(&stcb->asoc.nets);
3086 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3093 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3094 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3096 * JRS - Use the congestion control given in the pluggable
3099 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3101 * We reduce once every RTT. So we will only lower cwnd at
3102 * the next sending seq i.e. the window_data_tsn
3104 net->cwr_window_tsn = window_data_tsn;
3105 net->ecn_ce_pkt_cnt += pkt_cnt;
3106 net->lost_cnt = pkt_cnt;
3107 net->last_cwr_tsn = tsn;
3109 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3110 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3111 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3113 * Another loss in the same window update how many
3114 * marks/packets lost we have had.
3118 if (pkt_cnt > net->lost_cnt) {
3119 /* Should be the case */
3120 cnt = (pkt_cnt - net->lost_cnt);
3121 net->ecn_ce_pkt_cnt += cnt;
3123 net->lost_cnt = pkt_cnt;
3124 net->last_cwr_tsn = tsn;
3126 * Most CC functions will ignore this call, since we
3127 * are in-window yet of the initial CE the peer saw.
3129 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3133 * We always send a CWR this way if our previous one was lost our
3134 * peer will get an update, or if it is not time again to reduce we
3135 * still get the cwr to the peer. Note we set the override when we
3136 * could not find the TSN on the chunk or the destination network.
3138 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3142 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3145 * Here we get a CWR from the peer. We must look in the outqueue and
3146 * make sure that we have a covered ECNE in the control chunk part.
3149 struct sctp_tmit_chunk *chk, *nchk;
3150 struct sctp_ecne_chunk *ecne;
3154 cwr_tsn = ntohl(cp->tsn);
3155 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3156 TAILQ_FOREACH_SAFE(chk, &stcb->asoc.control_send_queue, sctp_next, nchk) {
3157 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3160 if ((override == 0) && (chk->whoTo != net)) {
3161 /* Must be from the right src unless override is set */
3164 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3165 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3166 /* this covers this ECNE, we can remove it */
3167 stcb->asoc.ecn_echo_cnt_onq--;
3168 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3170 sctp_m_freem(chk->data);
3172 stcb->asoc.ctrl_queue_cnt--;
3173 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3174 if (override == 0) {
3182 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3183 struct sctp_tcb *stcb, struct sctp_nets *net)
3185 struct sctp_association *asoc;
3186 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3190 SCTPDBG(SCTP_DEBUG_INPUT2,
3191 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3196 /* process according to association state */
3197 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3198 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3199 SCTPDBG(SCTP_DEBUG_INPUT2,
3200 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3201 SCTP_TCB_UNLOCK(stcb);
3204 /* notify upper layer protocol */
3205 if (stcb->sctp_socket) {
3206 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3209 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3210 !TAILQ_EMPTY(&asoc->sent_queue) ||
3211 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
3212 panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3215 /* stop the timer */
3216 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3217 SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3218 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3220 SCTPDBG(SCTP_DEBUG_INPUT2,
3221 "sctp_handle_shutdown_complete: calls free-asoc\n");
3222 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3223 so = SCTP_INP_SO(stcb->sctp_ep);
3224 atomic_add_int(&stcb->asoc.refcnt, 1);
3225 SCTP_TCB_UNLOCK(stcb);
3226 SCTP_SOCKET_LOCK(so, 1);
3227 SCTP_TCB_LOCK(stcb);
3228 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3230 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3231 SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3232 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3233 SCTP_SOCKET_UNLOCK(so, 1);
3239 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3240 struct sctp_nets *net, uint8_t flg)
3242 switch (desc->chunk_type) {
3244 /* find the tsn to resend (possibly */
3247 struct sctp_tmit_chunk *tp1;
3249 tsn = ntohl(desc->tsn_ifany);
3250 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3251 if (tp1->rec.data.tsn == tsn) {
3255 if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) {
3263 * Do it the other way , aka without paying
3264 * attention to queue seq order.
3266 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3267 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3268 if (tp1->rec.data.tsn == tsn) {
3275 SCTP_STAT_INCR(sctps_pdrptsnnf);
3277 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3280 if (((flg & SCTP_BADCRC) == 0) &&
3281 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3284 if ((stcb->asoc.peers_rwnd == 0) &&
3285 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3286 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3289 if (stcb->asoc.peers_rwnd == 0 &&
3290 (flg & SCTP_FROM_MIDDLE_BOX)) {
3291 SCTP_STAT_INCR(sctps_pdrpdizrw);
3294 ddp = (uint8_t *)(mtod(tp1->data, caddr_t)+
3295 sizeof(struct sctp_data_chunk));
3299 for (iii = 0; iii < sizeof(desc->data_bytes);
3301 if (ddp[iii] != desc->data_bytes[iii]) {
3302 SCTP_STAT_INCR(sctps_pdrpbadd);
3310 * this guy had a RTO calculation
3311 * pending on it, cancel it
3313 if (tp1->whoTo->rto_needed == 0) {
3314 tp1->whoTo->rto_needed = 1;
3318 SCTP_STAT_INCR(sctps_pdrpmark);
3319 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3320 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3322 * mark it as if we were doing a FR, since
3323 * we will be getting gap ack reports behind
3324 * the info from the router.
3326 tp1->rec.data.doing_fast_retransmit = 1;
3328 * mark the tsn with what sequences can
3331 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3332 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3334 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
3337 /* restart the timer */
3338 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3340 SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3341 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3344 /* fix counts and things */
3345 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3346 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3347 tp1->whoTo->flight_size,
3349 (uint32_t)(uintptr_t)stcb,
3352 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3353 sctp_flight_size_decrease(tp1);
3354 sctp_total_flight_decrease(stcb, tp1);
3356 tp1->sent = SCTP_DATAGRAM_RESEND;
3362 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3363 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3366 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3368 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3371 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3372 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3373 audit, stcb->asoc.sent_queue_retran_cnt);
3374 #ifndef SCTP_AUDITING_ENABLED
3375 stcb->asoc.sent_queue_retran_cnt = audit;
3383 struct sctp_tmit_chunk *asconf;
3385 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3387 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3392 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3393 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3394 asconf->sent = SCTP_DATAGRAM_RESEND;
3395 asconf->snd_count--;
3399 case SCTP_INITIATION:
3400 /* resend the INIT */
3401 stcb->asoc.dropped_special_cnt++;
3402 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3404 * If we can get it in, in a few attempts we do
3405 * this, otherwise we let the timer fire.
3407 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3409 SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3410 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3413 case SCTP_SELECTIVE_ACK:
3414 case SCTP_NR_SELECTIVE_ACK:
3415 /* resend the sack */
3416 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3418 case SCTP_HEARTBEAT_REQUEST:
3419 /* resend a demand HB */
3420 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3422 * Only retransmit if we KNOW we wont destroy the
3425 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3429 sctp_send_shutdown(stcb, net);
3431 case SCTP_SHUTDOWN_ACK:
3432 sctp_send_shutdown_ack(stcb, net);
3434 case SCTP_COOKIE_ECHO:
3436 struct sctp_tmit_chunk *cookie;
3439 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3441 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3446 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3447 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3448 cookie->sent = SCTP_DATAGRAM_RESEND;
3449 sctp_stop_all_cookie_timers(stcb);
3453 case SCTP_COOKIE_ACK:
3454 sctp_send_cookie_ack(stcb);
3456 case SCTP_ASCONF_ACK:
3457 /* resend last asconf ack */
3458 sctp_send_asconf_ack(stcb);
3460 case SCTP_IFORWARD_CUM_TSN:
3461 case SCTP_FORWARD_CUM_TSN:
3462 send_forward_tsn(stcb, &stcb->asoc);
3464 /* can't do anything with these */
3465 case SCTP_PACKET_DROPPED:
3466 case SCTP_INITIATION_ACK: /* this should not happen */
3467 case SCTP_HEARTBEAT_ACK:
3468 case SCTP_ABORT_ASSOCIATION:
3469 case SCTP_OPERATION_ERROR:
3470 case SCTP_SHUTDOWN_COMPLETE:
3480 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3486 * We set things to 0xffffffff since this is the last delivered
3487 * sequence and we will be sending in 0 after the reset.
3490 if (number_entries) {
3491 for (i = 0; i < number_entries; i++) {
3492 temp = ntohs(list[i]);
3493 if (temp >= stcb->asoc.streamincnt) {
3496 stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff;
3500 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3501 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
3504 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3508 sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3513 if (number_entries > 0) {
3514 for (i = 0; i < number_entries; i++) {
3515 temp = ntohs(list[i]);
3516 if (temp >= stcb->asoc.streamoutcnt) {
3517 /* no such stream */
3520 stcb->asoc.strmout[temp].next_mid_ordered = 0;
3521 stcb->asoc.strmout[temp].next_mid_unordered = 0;
3524 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3525 stcb->asoc.strmout[i].next_mid_ordered = 0;
3526 stcb->asoc.strmout[i].next_mid_unordered = 0;
3529 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3533 sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3538 if (number_entries > 0) {
3539 for (i = 0; i < number_entries; i++) {
3540 temp = ntohs(list[i]);
3541 if (temp >= stcb->asoc.streamoutcnt) {
3542 /* no such stream */
3545 stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
3548 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3549 stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
3555 struct sctp_stream_reset_request *
3556 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3558 struct sctp_association *asoc;
3559 struct sctp_chunkhdr *ch;
3560 struct sctp_stream_reset_request *r;
3561 struct sctp_tmit_chunk *chk;
3565 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3566 asoc->stream_reset_outstanding = 0;
3569 if (stcb->asoc.str_reset == NULL) {
3570 asoc->stream_reset_outstanding = 0;
3573 chk = stcb->asoc.str_reset;
3574 if (chk->data == NULL) {
3578 /* he wants a copy of the chk pointer */
3581 clen = chk->send_size;
3582 ch = mtod(chk->data, struct sctp_chunkhdr *);
3583 r = (struct sctp_stream_reset_request *)(ch + 1);
3584 if (ntohl(r->request_seq) == seq) {
3588 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3589 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3590 /* move to the next one, there can only be a max of two */
3591 r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3592 if (ntohl(r->request_seq) == seq) {
3596 /* that seq is not here */
3601 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3603 struct sctp_association *asoc;
3604 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3606 if (stcb->asoc.str_reset == NULL) {
3611 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
3612 chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3613 TAILQ_REMOVE(&asoc->control_send_queue,
3617 sctp_m_freem(chk->data);
3620 asoc->ctrl_queue_cnt--;
3621 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3622 /* sa_ignore NO_NULL_CHK */
3623 stcb->asoc.str_reset = NULL;
3628 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3629 uint32_t seq, uint32_t action,
3630 struct sctp_stream_reset_response *respin)
3634 struct sctp_association *asoc = &stcb->asoc;
3635 struct sctp_tmit_chunk *chk;
3636 struct sctp_stream_reset_request *req_param;
3637 struct sctp_stream_reset_out_request *req_out_param;
3638 struct sctp_stream_reset_in_request *req_in_param;
3639 uint32_t number_entries;
3641 if (asoc->stream_reset_outstanding == 0) {
3645 if (seq == stcb->asoc.str_reset_seq_out) {
3646 req_param = sctp_find_stream_reset(stcb, seq, &chk);
3647 if (req_param != NULL) {
3648 stcb->asoc.str_reset_seq_out++;
3649 type = ntohs(req_param->ph.param_type);
3650 lparam_len = ntohs(req_param->ph.param_length);
3651 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3654 req_out_param = (struct sctp_stream_reset_out_request *)req_param;
3655 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3656 asoc->stream_reset_out_is_outstanding = 0;
3657 if (asoc->stream_reset_outstanding)
3658 asoc->stream_reset_outstanding--;
3659 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3661 sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3662 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3663 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3664 } else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
3666 * Set it up so we don't stop
3669 asoc->stream_reset_outstanding++;
3670 stcb->asoc.str_reset_seq_out--;
3671 asoc->stream_reset_out_is_outstanding = 1;
3674 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3676 if (no_clear == 0) {
3677 sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
3679 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3680 req_in_param = (struct sctp_stream_reset_in_request *)req_param;
3681 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3682 if (asoc->stream_reset_outstanding)
3683 asoc->stream_reset_outstanding--;
3684 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3685 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3686 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3687 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3688 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3689 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3691 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3692 /* Ok we now may have more streams */
3695 num_stream = stcb->asoc.strm_pending_add_size;
3696 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3698 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3700 stcb->asoc.strm_pending_add_size = 0;
3701 if (asoc->stream_reset_outstanding)
3702 asoc->stream_reset_outstanding--;
3703 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3704 /* Put the new streams into effect */
3707 for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
3708 asoc->strmout[i].state = SCTP_STREAM_OPEN;
3710 asoc->streamoutcnt += num_stream;
3711 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3712 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3713 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3714 SCTP_STREAM_CHANGE_DENIED);
3716 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3717 SCTP_STREAM_CHANGE_FAILED);
3719 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3720 if (asoc->stream_reset_outstanding)
3721 asoc->stream_reset_outstanding--;
3722 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3723 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3724 SCTP_STREAM_CHANGE_DENIED);
3725 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3726 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3727 SCTP_STREAM_CHANGE_FAILED);
3729 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3731 * a) Adopt the new in tsn.
3733 * c) Adopt the new out-tsn
3735 struct sctp_stream_reset_response_tsn *resp;
3736 struct sctp_forward_tsn_chunk fwdtsn;
3739 if (respin == NULL) {
3743 if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
3746 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3747 resp = (struct sctp_stream_reset_response_tsn *)respin;
3748 asoc->stream_reset_outstanding--;
3749 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3750 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3751 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3752 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3756 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3757 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3758 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3760 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3761 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3762 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3764 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3765 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3767 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3768 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3770 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3771 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3772 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3773 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3774 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3775 SCTP_ASSOC_RESET_DENIED);
3777 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3778 SCTP_ASSOC_RESET_FAILED);
3781 /* get rid of the request and get the request flags */
3782 if (asoc->stream_reset_outstanding == 0) {
3783 sctp_clean_up_stream_reset(stcb);
3787 if (asoc->stream_reset_outstanding == 0) {
3788 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3794 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3795 struct sctp_tmit_chunk *chk,
3796 struct sctp_stream_reset_in_request *req, int trunc)
3804 * peer wants me to send a str-reset to him for my outgoing seq's if
3807 struct sctp_association *asoc = &stcb->asoc;
3809 seq = ntohl(req->request_seq);
3810 if (asoc->str_reset_seq_in == seq) {
3811 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3812 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3813 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3815 /* Can't do it, since they exceeded our buffer size */
3816 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3817 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3818 len = ntohs(req->ph.param_length);
3819 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3820 if (number_entries) {
3821 for (i = 0; i < number_entries; i++) {
3822 temp = ntohs(req->list_of_streams[i]);
3823 if (temp >= stcb->asoc.streamoutcnt) {
3824 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3827 req->list_of_streams[i] = temp;
3829 for (i = 0; i < number_entries; i++) {
3830 if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
3831 stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
3836 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3837 if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
3838 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
3841 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3843 /* Can't do it, since we have sent one out */
3844 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3847 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3848 asoc->str_reset_seq_in++;
3849 } else if (asoc->str_reset_seq_in - 1 == seq) {
3850 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3851 } else if (asoc->str_reset_seq_in - 2 == seq) {
3852 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3854 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3856 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3860 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3861 struct sctp_tmit_chunk *chk,
3862 struct sctp_stream_reset_tsn_request *req)
3864 /* reset all in and out and update the tsn */
3866 * A) reset my str-seq's on in and out. B) Select a receive next,
3867 * and set cum-ack to it. Also process this selected number as a
3868 * fwd-tsn as well. C) set in the response my next sending seq.
3870 struct sctp_forward_tsn_chunk fwdtsn;
3871 struct sctp_association *asoc = &stcb->asoc;
3875 seq = ntohl(req->request_seq);
3876 if (asoc->str_reset_seq_in == seq) {
3877 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3878 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3879 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3881 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3882 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3883 fwdtsn.ch.chunk_flags = 0;
3884 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3885 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3889 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3890 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3891 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3893 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3894 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3895 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3896 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3897 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3898 atomic_add_int(&asoc->sending_seq, 1);
3899 /* save off historical data for retrans */
3900 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3901 asoc->last_sending_seq[0] = asoc->sending_seq;
3902 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3903 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3904 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3905 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3906 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3907 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3909 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3910 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3911 asoc->str_reset_seq_in++;
3912 } else if (asoc->str_reset_seq_in - 1 == seq) {
3913 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3914 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3915 } else if (asoc->str_reset_seq_in - 2 == seq) {
3916 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3917 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3919 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3925 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3926 struct sctp_tmit_chunk *chk,
3927 struct sctp_stream_reset_out_request *req, int trunc)
3930 int number_entries, len;
3931 struct sctp_association *asoc = &stcb->asoc;
3933 seq = ntohl(req->request_seq);
3935 /* now if its not a duplicate we process it */
3936 if (asoc->str_reset_seq_in == seq) {
3937 len = ntohs(req->ph.param_length);
3938 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3940 * the sender is resetting, handle the list issue.. we must
3941 * a) verify if we can do the reset, if so no problem b) If
3942 * we can't do the reset we must copy the request. c) queue
3943 * it, and setup the data in processor to trigger it off
3944 * when needed and dequeue all the queued data.
3946 tsn = ntohl(req->send_reset_at_tsn);
3948 /* move the reset action back one */
3949 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3950 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3951 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3953 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3954 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3955 /* we can do it now */
3956 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3957 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3960 * we must queue it up and thus wait for the TSN's
3961 * to arrive that are at or before tsn
3963 struct sctp_stream_reset_list *liste;
3966 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3967 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3968 siz, SCTP_M_STRESET);
3969 if (liste == NULL) {
3970 /* gak out of memory */
3971 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3972 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3977 liste->number_entries = number_entries;
3978 memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
3979 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3980 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
3982 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3983 asoc->str_reset_seq_in++;
3984 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3986 * one seq back, just echo back last action since my
3987 * response was lost.
3989 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3990 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3992 * two seq back, just echo back last action since my
3993 * response was lost.
3995 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3997 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4002 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4003 struct sctp_stream_reset_add_strm *str_add)
4006 * Peer is requesting to add more streams. If its within our
4007 * max-streams we will allow it.
4009 uint32_t num_stream, i;
4011 struct sctp_association *asoc = &stcb->asoc;
4012 struct sctp_queued_to_read *ctl, *nctl;
4014 /* Get the number. */
4015 seq = ntohl(str_add->request_seq);
4016 num_stream = ntohs(str_add->number_of_streams);
4017 /* Now what would be the new total? */
4018 if (asoc->str_reset_seq_in == seq) {
4019 num_stream += stcb->asoc.streamincnt;
4020 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4021 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4022 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4023 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
4024 (num_stream > 0xffff)) {
4025 /* We must reject it they ask for to many */
4027 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4029 /* Ok, we can do that :-) */
4030 struct sctp_stream_in *oldstrm;
4032 /* save off the old */
4033 oldstrm = stcb->asoc.strmin;
4034 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
4035 (num_stream * sizeof(struct sctp_stream_in)),
4037 if (stcb->asoc.strmin == NULL) {
4038 stcb->asoc.strmin = oldstrm;
4041 /* copy off the old data */
4042 for (i = 0; i < stcb->asoc.streamincnt; i++) {
4043 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4044 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4045 stcb->asoc.strmin[i].sid = i;
4046 stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered;
4047 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
4048 stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
4049 /* now anything on those queues? */
4050 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
4051 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
4052 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
4054 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
4055 TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
4056 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
4059 /* Init the new streams */
4060 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
4061 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4062 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4063 stcb->asoc.strmin[i].sid = i;
4064 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
4065 stcb->asoc.strmin[i].pd_api_started = 0;
4066 stcb->asoc.strmin[i].delivery_started = 0;
4068 SCTP_FREE(oldstrm, SCTP_M_STRMI);
4069 /* update the size */
4070 stcb->asoc.streamincnt = num_stream;
4071 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4072 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
4074 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4075 asoc->str_reset_seq_in++;
4076 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4078 * one seq back, just echo back last action since my
4079 * response was lost.
4081 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4082 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4084 * two seq back, just echo back last action since my
4085 * response was lost.
4087 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4089 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4095 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4096 struct sctp_stream_reset_add_strm *str_add)
4099 * Peer is requesting to add more streams. If its within our
4100 * max-streams we will allow it.
4102 uint16_t num_stream;
4104 struct sctp_association *asoc = &stcb->asoc;
4106 /* Get the number. */
4107 seq = ntohl(str_add->request_seq);
4108 num_stream = ntohs(str_add->number_of_streams);
4109 /* Now what would be the new total? */
4110 if (asoc->str_reset_seq_in == seq) {
4111 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4112 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4113 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4114 } else if (stcb->asoc.stream_reset_outstanding) {
4115 /* We must reject it we have something pending */
4116 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4118 /* Ok, we can do that :-) */
4121 mychk = stcb->asoc.streamoutcnt;
4122 mychk += num_stream;
4123 if (mychk < 0x10000) {
4124 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4125 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
4126 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4129 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4132 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4133 asoc->str_reset_seq_in++;
4134 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4136 * one seq back, just echo back last action since my
4137 * response was lost.
4139 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4140 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4142 * two seq back, just echo back last action since my
4143 * response was lost.
4145 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4147 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4152 __attribute__((noinline))
4155 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4156 struct sctp_chunkhdr *ch_req)
4158 uint16_t remaining_length, param_len, ptype;
4159 struct sctp_paramhdr pstore;
4160 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4164 struct sctp_tmit_chunk *chk;
4165 struct sctp_chunkhdr *ch;
4166 struct sctp_paramhdr *ph;
4170 /* now it may be a reset or a reset-response */
4171 remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
4173 /* setup for adding the response */
4174 sctp_alloc_a_chunk(stcb, chk);
4178 chk->copy_by_ref = 0;
4179 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4180 chk->rec.chunk_id.can_take_data = 0;
4182 chk->asoc = &stcb->asoc;
4183 chk->no_fr_allowed = 0;
4184 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4185 chk->book_size_scale = 0;
4186 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4187 if (chk->data == NULL) {
4190 sctp_m_freem(chk->data);
4193 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4196 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4198 /* setup chunk parameters */
4199 chk->sent = SCTP_DATAGRAM_UNSENT;
4203 ch = mtod(chk->data, struct sctp_chunkhdr *);
4204 ch->chunk_type = SCTP_STREAM_RESET;
4205 ch->chunk_flags = 0;
4206 ch->chunk_length = htons(chk->send_size);
4207 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4208 offset += sizeof(struct sctp_chunkhdr);
4209 while (remaining_length >= sizeof(struct sctp_paramhdr)) {
4210 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
4215 param_len = ntohs(ph->param_length);
4216 if ((param_len > remaining_length) ||
4217 (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
4218 /* bad parameter length */
4221 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
4222 (uint8_t *)&cstore);
4227 ptype = ntohs(ph->param_type);
4229 if (param_len > sizeof(cstore)) {
4234 if (num_param > SCTP_MAX_RESET_PARAMS) {
4235 /* hit the max of parameters already sorry.. */
4238 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4239 struct sctp_stream_reset_out_request *req_out;
4241 if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
4244 req_out = (struct sctp_stream_reset_out_request *)ph;
4246 if (stcb->asoc.stream_reset_outstanding) {
4247 seq = ntohl(req_out->response_seq);
4248 if (seq == stcb->asoc.str_reset_seq_out) {
4250 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4253 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4254 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4255 struct sctp_stream_reset_add_strm *str_add;
4257 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4260 str_add = (struct sctp_stream_reset_add_strm *)ph;
4262 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4263 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4264 struct sctp_stream_reset_add_strm *str_add;
4266 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4269 str_add = (struct sctp_stream_reset_add_strm *)ph;
4271 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4272 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4273 struct sctp_stream_reset_in_request *req_in;
4276 req_in = (struct sctp_stream_reset_in_request *)ph;
4277 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4278 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4279 struct sctp_stream_reset_tsn_request *req_tsn;
4282 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4283 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4285 goto strres_nochunk;
4289 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4290 struct sctp_stream_reset_response *resp;
4293 if (param_len < sizeof(struct sctp_stream_reset_response)) {
4296 resp = (struct sctp_stream_reset_response *)ph;
4297 seq = ntohl(resp->response_seq);
4298 result = ntohl(resp->result);
4299 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4301 goto strres_nochunk;
4306 offset += SCTP_SIZE32(param_len);
4307 if (remaining_length >= SCTP_SIZE32(param_len)) {
4308 remaining_length -= SCTP_SIZE32(param_len);
4310 remaining_length = 0;
4314 /* we have no response free the stuff */
4315 goto strres_nochunk;
4317 /* ok we have a chunk to link in */
4318 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4321 stcb->asoc.ctrl_queue_cnt++;
4326 * Handle a router or endpoints report of a packet loss, there are two ways
4327 * to handle this, either we get the whole packet and must disect it
4328 * ourselves (possibly with truncation and or corruption) or it is a summary
4329 * from a middle box that did the disectting for us.
4332 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4333 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4335 uint32_t bottle_bw, on_queue;
4339 struct sctp_chunk_desc desc;
4340 struct sctp_chunkhdr *ch;
4342 chlen = ntohs(cp->ch.chunk_length);
4343 chlen -= sizeof(struct sctp_pktdrop_chunk);
4344 /* XXX possible chlen underflow */
4347 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4348 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4350 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4351 chlen -= sizeof(struct sctphdr);
4352 /* XXX possible chlen underflow */
4353 memset(&desc, 0, sizeof(desc));
4355 trunc_len = (uint16_t)ntohs(cp->trunc_len);
4356 if (trunc_len > limit) {
4359 /* now the chunks themselves */
4360 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4361 desc.chunk_type = ch->chunk_type;
4362 /* get amount we need to move */
4363 at = ntohs(ch->chunk_length);
4364 if (at < sizeof(struct sctp_chunkhdr)) {
4365 /* corrupt chunk, maybe at the end? */
4366 SCTP_STAT_INCR(sctps_pdrpcrupt);
4369 if (trunc_len == 0) {
4370 /* we are supposed to have all of it */
4372 /* corrupt skip it */
4373 SCTP_STAT_INCR(sctps_pdrpcrupt);
4377 /* is there enough of it left ? */
4378 if (desc.chunk_type == SCTP_DATA) {
4379 if (chlen < (sizeof(struct sctp_data_chunk) +
4380 sizeof(desc.data_bytes))) {
4384 if (chlen < sizeof(struct sctp_chunkhdr)) {
4389 if (desc.chunk_type == SCTP_DATA) {
4390 /* can we get out the tsn? */
4391 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4392 SCTP_STAT_INCR(sctps_pdrpmbda);
4394 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4396 struct sctp_data_chunk *dcp;
4400 dcp = (struct sctp_data_chunk *)ch;
4401 ddp = (uint8_t *)(dcp + 1);
4402 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4403 desc.data_bytes[iii] = ddp[iii];
4405 desc.tsn_ifany = dcp->dp.tsn;
4407 /* nope we are done. */
4408 SCTP_STAT_INCR(sctps_pdrpnedat);
4412 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4413 SCTP_STAT_INCR(sctps_pdrpmbct);
4416 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4417 SCTP_STAT_INCR(sctps_pdrppdbrk);
4420 if (SCTP_SIZE32(at) > chlen) {
4423 chlen -= SCTP_SIZE32(at);
4424 if (chlen < sizeof(struct sctp_chunkhdr)) {
4425 /* done, none left */
4428 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4430 /* Now update any rwnd --- possibly */
4431 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4432 /* From a peer, we get a rwnd report */
4435 SCTP_STAT_INCR(sctps_pdrpfehos);
4437 bottle_bw = ntohl(cp->bottle_bw);
4438 on_queue = ntohl(cp->current_onq);
4439 if (bottle_bw && on_queue) {
4440 /* a rwnd report is in here */
4441 if (bottle_bw > on_queue)
4442 a_rwnd = bottle_bw - on_queue;
4447 stcb->asoc.peers_rwnd = 0;
4449 if (a_rwnd > stcb->asoc.total_flight) {
4450 stcb->asoc.peers_rwnd =
4451 a_rwnd - stcb->asoc.total_flight;
4453 stcb->asoc.peers_rwnd = 0;
4455 if (stcb->asoc.peers_rwnd <
4456 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4457 /* SWS sender side engages */
4458 stcb->asoc.peers_rwnd = 0;
4463 SCTP_STAT_INCR(sctps_pdrpfmbox);
4466 /* now middle boxes in sat networks get a cwnd bump */
4467 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4468 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4469 (stcb->asoc.sat_network)) {
4471 * This is debatable but for sat networks it makes sense
4472 * Note if a T3 timer has went off, we will prohibit any
4473 * changes to cwnd until we exit the t3 loss recovery.
4475 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4476 net, cp, &bottle_bw, &on_queue);
4481 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4482 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4483 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4484 * length of the complete packet outputs: - length: modified to remaining
4485 * length after control processing - netp: modified to new sctp_nets after
4486 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4487 * bad packet,...) otherwise return the tcb for this packet
4490 __attribute__((noinline))
4492 static struct sctp_tcb *
4493 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4494 struct sockaddr *src, struct sockaddr *dst,
4495 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4496 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4497 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4498 uint32_t vrf_id, uint16_t port)
4500 struct sctp_association *asoc;
4501 struct mbuf *op_err;
4502 char msg[SCTP_DIAG_INFO_LEN];
4504 int num_chunks = 0; /* number of control chunks processed */
4505 uint32_t chk_length;
4507 int abort_no_unlock = 0;
4511 * How big should this be, and should it be alloc'd? Lets try the
4512 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4513 * until we get into jumbo grams and such..
4515 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4516 struct sctp_tcb *locked_tcb = stcb;
4518 uint32_t auth_offset = 0, auth_len = 0;
4519 int auth_skipped = 0;
4521 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4525 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4526 iphlen, *offset, length, (void *)stcb);
4528 /* validate chunk header length... */
4529 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4530 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4531 ntohs(ch->chunk_length));
4533 SCTP_TCB_UNLOCK(locked_tcb);
4538 * validate the verification tag
4540 vtag_in = ntohl(sh->v_tag);
4543 SCTP_TCB_LOCK_ASSERT(locked_tcb);
4545 if (ch->chunk_type == SCTP_INITIATION) {
4546 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4547 ntohs(ch->chunk_length), vtag_in);
4549 /* protocol error- silently discard... */
4550 SCTP_STAT_INCR(sctps_badvtag);
4552 SCTP_TCB_UNLOCK(locked_tcb);
4556 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4558 * If there is no stcb, skip the AUTH chunk and process
4559 * later after a stcb is found (to validate the lookup was
4562 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4564 (inp->auth_supported == 1)) {
4565 /* save this chunk for later processing */
4567 auth_offset = *offset;
4568 auth_len = ntohs(ch->chunk_length);
4570 /* (temporarily) move past this chunk */
4571 *offset += SCTP_SIZE32(auth_len);
4572 if (*offset >= length) {
4573 /* no more data left in the mbuf chain */
4576 SCTP_TCB_UNLOCK(locked_tcb);
4580 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4581 sizeof(struct sctp_chunkhdr), chunk_buf);
4587 SCTP_TCB_UNLOCK(locked_tcb);
4591 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4592 goto process_control_chunks;
4595 * first check if it's an ASCONF with an unknown src addr we
4596 * need to look inside to find the association
4598 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4599 struct sctp_chunkhdr *asconf_ch = ch;
4600 uint32_t asconf_offset = 0, asconf_len = 0;
4602 /* inp's refcount may be reduced */
4603 SCTP_INP_INCR_REF(inp);
4605 asconf_offset = *offset;
4607 asconf_len = ntohs(asconf_ch->chunk_length);
4608 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4610 stcb = sctp_findassociation_ep_asconf(m,
4613 sh, &inp, netp, vrf_id);
4616 asconf_offset += SCTP_SIZE32(asconf_len);
4617 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4618 sizeof(struct sctp_chunkhdr), chunk_buf);
4619 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4622 * reduce inp's refcount if not reduced in
4623 * sctp_findassociation_ep_asconf().
4625 SCTP_INP_DECR_REF(inp);
4630 /* now go back and verify any auth chunk to be sure */
4631 if (auth_skipped && (stcb != NULL)) {
4632 struct sctp_auth_chunk *auth;
4634 auth = (struct sctp_auth_chunk *)
4635 sctp_m_getptr(m, auth_offset,
4636 auth_len, chunk_buf);
4639 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4641 /* auth HMAC failed so dump it */
4644 SCTP_TCB_UNLOCK(locked_tcb);
4648 /* remaining chunks are HMAC checked */
4649 stcb->asoc.authenticated = 1;
4654 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4655 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4657 /* no association, so it's out of the blue... */
4658 sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4659 mflowtype, mflowid, inp->fibnum,
4663 SCTP_TCB_UNLOCK(locked_tcb);
4668 /* ABORT and SHUTDOWN can use either v_tag... */
4669 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4670 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4671 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4672 /* Take the T-bit always into account. */
4673 if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
4674 (vtag_in == asoc->my_vtag)) ||
4675 (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4676 (asoc->peer_vtag != htonl(0)) &&
4677 (vtag_in == asoc->peer_vtag))) {
4680 /* drop this packet... */
4681 SCTP_STAT_INCR(sctps_badvtag);
4683 SCTP_TCB_UNLOCK(locked_tcb);
4687 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4688 if (vtag_in != asoc->my_vtag) {
4690 * this could be a stale SHUTDOWN-ACK or the
4691 * peer never got the SHUTDOWN-COMPLETE and
4692 * is still hung; we have started a new asoc
4693 * but it won't complete until the shutdown
4697 SCTP_TCB_UNLOCK(locked_tcb);
4699 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4700 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4702 sctp_handle_ootb(m, iphlen, *offset, src, dst,
4704 mflowtype, mflowid, fibnum,
4709 /* for all other chunks, vtag must match */
4710 if (vtag_in != asoc->my_vtag) {
4711 /* invalid vtag... */
4712 SCTPDBG(SCTP_DEBUG_INPUT3,
4713 "invalid vtag: %xh, expect %xh\n",
4714 vtag_in, asoc->my_vtag);
4715 SCTP_STAT_INCR(sctps_badvtag);
4717 SCTP_TCB_UNLOCK(locked_tcb);
4723 } /* end if !SCTP_COOKIE_ECHO */
4725 * process all control chunks...
4727 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4728 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4729 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4730 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4731 /* implied cookie-ack.. we must have lost the ack */
4732 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4733 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4734 stcb->asoc.overall_error_count,
4736 SCTP_FROM_SCTP_INPUT,
4739 stcb->asoc.overall_error_count = 0;
4740 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4743 process_control_chunks:
4744 while (IS_SCTP_CONTROL(ch)) {
4745 /* validate chunk length */
4746 chk_length = ntohs(ch->chunk_length);
4747 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4748 ch->chunk_type, chk_length);
4749 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4750 if (chk_length < sizeof(*ch) ||
4751 (*offset + (int)chk_length) > length) {
4754 SCTP_TCB_UNLOCK(locked_tcb);
4758 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4760 * INIT-ACK only gets the init ack "header" portion only
4761 * because we don't have to process the peer's COOKIE. All
4762 * others get a complete chunk.
4764 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4765 (ch->chunk_type == SCTP_INITIATION)) {
4766 /* get an init-ack chunk */
4767 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4768 sizeof(struct sctp_init_ack_chunk), chunk_buf);
4772 SCTP_TCB_UNLOCK(locked_tcb);
4777 /* For cookies and all other chunks. */
4778 if (chk_length > sizeof(chunk_buf)) {
4780 * use just the size of the chunk buffer so
4781 * the front part of our chunks fit in
4782 * contiguous space up to the chunk buffer
4783 * size (508 bytes). For chunks that need to
4784 * get more than that they must use the
4785 * sctp_m_getptr() function or other means
4786 * (e.g. know how to parse mbuf chains).
4787 * Cookies do this already.
4789 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4790 (sizeof(chunk_buf) - 4),
4795 SCTP_TCB_UNLOCK(locked_tcb);
4800 /* We can fit it all */
4801 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4802 chk_length, chunk_buf);
4804 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4807 SCTP_TCB_UNLOCK(locked_tcb);
4814 /* Save off the last place we got a control from */
4816 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4818 * allow last_control to be NULL if
4819 * ASCONF... ASCONF processing will find the
4822 if ((netp != NULL) && (*netp != NULL))
4823 stcb->asoc.last_control_chunk_from = *netp;
4826 #ifdef SCTP_AUDITING_ENABLED
4827 sctp_audit_log(0xB0, ch->chunk_type);
4830 /* check to see if this chunk required auth, but isn't */
4831 if ((stcb != NULL) &&
4832 (stcb->asoc.auth_supported == 1) &&
4833 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4834 !stcb->asoc.authenticated) {
4835 /* "silently" ignore */
4836 SCTP_STAT_INCR(sctps_recvauthmissing);
4839 switch (ch->chunk_type) {
4840 case SCTP_INITIATION:
4841 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4842 /* The INIT chunk must be the only chunk. */
4843 if ((num_chunks > 1) ||
4844 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4845 /* RFC 4960 requires that no ABORT is sent */
4848 SCTP_TCB_UNLOCK(locked_tcb);
4852 /* Honor our resource limit. */
4853 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4854 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4855 sctp_abort_association(inp, stcb, m, iphlen,
4856 src, dst, sh, op_err,
4862 sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4863 (struct sctp_init_chunk *)ch, inp,
4864 stcb, *netp, &abort_no_unlock,
4868 if ((!abort_no_unlock) && (locked_tcb)) {
4869 SCTP_TCB_UNLOCK(locked_tcb);
4873 case SCTP_PAD_CHUNK:
4875 case SCTP_INITIATION_ACK:
4876 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4877 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4878 /* We are not interested anymore */
4879 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4882 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
4884 SCTP_TCB_UNLOCK(locked_tcb);
4888 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4889 so = SCTP_INP_SO(inp);
4890 atomic_add_int(&stcb->asoc.refcnt, 1);
4891 SCTP_TCB_UNLOCK(stcb);
4892 SCTP_SOCKET_LOCK(so, 1);
4893 SCTP_TCB_LOCK(stcb);
4894 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4896 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4897 SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4898 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4899 SCTP_SOCKET_UNLOCK(so, 1);
4905 /* The INIT-ACK chunk must be the only chunk. */
4906 if ((num_chunks > 1) ||
4907 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4910 SCTP_TCB_UNLOCK(locked_tcb);
4914 if ((netp) && (*netp)) {
4915 ret = sctp_handle_init_ack(m, iphlen, *offset,
4917 (struct sctp_init_ack_chunk *)ch,
4926 if (abort_no_unlock) {
4930 * Special case, I must call the output routine to
4931 * get the cookie echoed
4933 if ((stcb != NULL) && (ret == 0)) {
4934 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4937 SCTP_TCB_UNLOCK(locked_tcb);
4941 case SCTP_SELECTIVE_ACK:
4943 struct sctp_sack_chunk *sack;
4945 uint32_t a_rwnd, cum_ack;
4946 uint16_t num_seg, num_dup;
4948 int offset_seg, offset_dup;
4950 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4951 SCTP_STAT_INCR(sctps_recvsacks);
4953 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4956 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4957 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4960 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4962 * If we have sent a shutdown-ack, we will pay no
4963 * attention to a sack sent in to us since
4964 * we don't care anymore.
4968 sack = (struct sctp_sack_chunk *)ch;
4969 flags = ch->chunk_flags;
4970 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4971 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4972 num_dup = ntohs(sack->sack.num_dup_tsns);
4973 a_rwnd = (uint32_t)ntohl(sack->sack.a_rwnd);
4974 if (sizeof(struct sctp_sack_chunk) +
4975 num_seg * sizeof(struct sctp_gap_ack_block) +
4976 num_dup * sizeof(uint32_t) != chk_length) {
4977 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4980 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4981 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4982 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4983 cum_ack, num_seg, a_rwnd);
4984 stcb->asoc.seen_a_sack_this_pkt = 1;
4985 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4987 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4988 (stcb->asoc.saw_sack_with_frags == 0) &&
4989 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4990 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4993 * We have a SIMPLE sack having no
4994 * prior segments and data on sent
4995 * queue to be acked.. Use the
4996 * faster path sack processing. We
4997 * also allow window update sacks
4998 * with no missing segments to go
5001 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
5004 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
5005 num_seg, 0, num_dup, &abort_now, flags,
5006 cum_ack, a_rwnd, ecne_seen);
5009 /* ABORT signal from sack processing */
5013 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5014 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5015 (stcb->asoc.stream_queue_cnt == 0)) {
5016 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5021 * EY - nr_sack: If the received chunk is an
5024 case SCTP_NR_SELECTIVE_ACK:
5026 struct sctp_nr_sack_chunk *nr_sack;
5028 uint32_t a_rwnd, cum_ack;
5029 uint16_t num_seg, num_nr_seg, num_dup;
5031 int offset_seg, offset_dup;
5033 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
5034 SCTP_STAT_INCR(sctps_recvsacks);
5036 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
5039 if (stcb->asoc.nrsack_supported == 0) {
5042 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
5043 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
5046 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
5048 * If we have sent a shutdown-ack, we will pay no
5049 * attention to a sack sent in to us since
5050 * we don't care anymore.
5054 nr_sack = (struct sctp_nr_sack_chunk *)ch;
5055 flags = ch->chunk_flags;
5056 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
5057 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
5058 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
5059 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
5060 a_rwnd = (uint32_t)ntohl(nr_sack->nr_sack.a_rwnd);
5061 if (sizeof(struct sctp_nr_sack_chunk) +
5062 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
5063 num_dup * sizeof(uint32_t) != chk_length) {
5064 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
5067 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
5068 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
5069 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
5070 cum_ack, num_seg, a_rwnd);
5071 stcb->asoc.seen_a_sack_this_pkt = 1;
5072 if ((stcb->asoc.pr_sctp_cnt == 0) &&
5073 (num_seg == 0) && (num_nr_seg == 0) &&
5074 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
5075 (stcb->asoc.saw_sack_with_frags == 0) &&
5076 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
5077 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
5079 * We have a SIMPLE sack having no
5080 * prior segments and data on sent
5081 * queue to be acked. Use the faster
5082 * path sack processing. We also
5083 * allow window update sacks with no
5084 * missing segments to go this way
5087 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
5088 &abort_now, ecne_seen);
5091 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
5092 num_seg, num_nr_seg, num_dup, &abort_now, flags,
5093 cum_ack, a_rwnd, ecne_seen);
5096 /* ABORT signal from sack processing */
5100 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5101 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5102 (stcb->asoc.stream_queue_cnt == 0)) {
5103 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5108 case SCTP_HEARTBEAT_REQUEST:
5109 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
5110 if ((stcb) && netp && *netp) {
5111 SCTP_STAT_INCR(sctps_recvheartbeat);
5112 sctp_send_heartbeat_ack(stcb, m, *offset,
5115 /* He's alive so give him credit */
5116 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5117 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5118 stcb->asoc.overall_error_count,
5120 SCTP_FROM_SCTP_INPUT,
5123 stcb->asoc.overall_error_count = 0;
5126 case SCTP_HEARTBEAT_ACK:
5127 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
5128 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5132 SCTP_TCB_UNLOCK(locked_tcb);
5136 /* He's alive so give him credit */
5137 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5138 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5139 stcb->asoc.overall_error_count,
5141 SCTP_FROM_SCTP_INPUT,
5144 stcb->asoc.overall_error_count = 0;
5145 SCTP_STAT_INCR(sctps_recvheartbeatack);
5147 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5150 case SCTP_ABORT_ASSOCIATION:
5151 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5153 if ((stcb) && netp && *netp)
5154 sctp_handle_abort((struct sctp_abort_chunk *)ch,
5160 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5162 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5165 SCTP_TCB_UNLOCK(locked_tcb);
5169 if (netp && *netp) {
5172 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5173 stcb, *netp, &abort_flag);
5180 case SCTP_SHUTDOWN_ACK:
5181 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", (void *)stcb);
5182 if ((stcb) && (netp) && (*netp))
5183 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5188 case SCTP_OPERATION_ERROR:
5189 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
5190 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
5195 case SCTP_COOKIE_ECHO:
5196 SCTPDBG(SCTP_DEBUG_INPUT3,
5197 "SCTP_COOKIE-ECHO, stcb %p\n", (void *)stcb);
5198 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5201 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5202 /* We are not interested anymore */
5205 SCTP_TCB_UNLOCK(stcb);
5212 * First are we accepting? We do this again here
5213 * since it is possible that a previous endpoint WAS
5214 * listening responded to a INIT-ACK and then
5215 * closed. We opened and bound.. and are now no
5218 * XXXGL: notes on checking listen queue length.
5219 * 1) SCTP_IS_LISTENING() doesn't necessarily mean
5220 * SOLISTENING(), because a listening "UDP type"
5221 * socket isn't listening in terms of the socket
5222 * layer. It is a normal data flow socket, that
5223 * can fork off new connections. Thus, we should
5224 * look into sol_qlen only in case we are !UDP.
5225 * 2) Checking sol_qlen in general requires locking
5226 * the socket, and this code lacks that.
5228 if ((stcb == NULL) &&
5229 (!SCTP_IS_LISTENING(inp) ||
5230 (!(inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
5231 inp->sctp_socket->sol_qlen >= inp->sctp_socket->sol_qlimit))) {
5232 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5233 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5234 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
5235 sctp_abort_association(inp, stcb, m, iphlen,
5236 src, dst, sh, op_err,
5243 struct mbuf *ret_buf;
5244 struct sctp_inpcb *linp;
5253 SCTP_ASOC_CREATE_LOCK(linp);
5254 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5255 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5256 SCTP_ASOC_CREATE_UNLOCK(linp);
5262 sctp_handle_cookie_echo(m, iphlen,
5266 (struct sctp_cookie_echo_chunk *)ch,
5280 SCTP_ASOC_CREATE_UNLOCK(linp);
5282 if (ret_buf == NULL) {
5284 SCTP_TCB_UNLOCK(locked_tcb);
5286 SCTPDBG(SCTP_DEBUG_INPUT3,
5287 "GAK, null buffer\n");
5291 /* if AUTH skipped, see if it verified... */
5296 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5298 * Restart the timer if we have
5301 struct sctp_tmit_chunk *chk;
5303 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5304 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5308 case SCTP_COOKIE_ACK:
5309 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", (void *)stcb);
5310 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5312 SCTP_TCB_UNLOCK(locked_tcb);
5316 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5317 /* We are not interested anymore */
5318 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5321 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5322 so = SCTP_INP_SO(inp);
5323 atomic_add_int(&stcb->asoc.refcnt, 1);
5324 SCTP_TCB_UNLOCK(stcb);
5325 SCTP_SOCKET_LOCK(so, 1);
5326 SCTP_TCB_LOCK(stcb);
5327 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5329 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5330 SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5331 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5332 SCTP_SOCKET_UNLOCK(so, 1);
5338 /* He's alive so give him credit */
5339 if ((stcb) && netp && *netp) {
5340 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5341 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5342 stcb->asoc.overall_error_count,
5344 SCTP_FROM_SCTP_INPUT,
5347 stcb->asoc.overall_error_count = 0;
5348 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5352 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5353 /* He's alive so give him credit */
5354 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5357 SCTP_TCB_UNLOCK(locked_tcb);
5363 if (stcb->asoc.ecn_supported == 0) {
5366 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5367 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5368 stcb->asoc.overall_error_count,
5370 SCTP_FROM_SCTP_INPUT,
5373 stcb->asoc.overall_error_count = 0;
5374 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5380 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5381 /* He's alive so give him credit */
5382 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5385 SCTP_TCB_UNLOCK(locked_tcb);
5391 if (stcb->asoc.ecn_supported == 0) {
5394 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5395 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5396 stcb->asoc.overall_error_count,
5398 SCTP_FROM_SCTP_INPUT,
5401 stcb->asoc.overall_error_count = 0;
5402 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5405 case SCTP_SHUTDOWN_COMPLETE:
5406 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", (void *)stcb);
5407 /* must be first and only chunk */
5408 if ((num_chunks > 1) ||
5409 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5412 SCTP_TCB_UNLOCK(locked_tcb);
5416 if ((stcb) && netp && *netp) {
5417 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5424 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5425 /* He's alive so give him credit */
5427 if (stcb->asoc.asconf_supported == 0) {
5430 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5431 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5432 stcb->asoc.overall_error_count,
5434 SCTP_FROM_SCTP_INPUT,
5437 stcb->asoc.overall_error_count = 0;
5438 sctp_handle_asconf(m, *offset, src,
5439 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5443 case SCTP_ASCONF_ACK:
5444 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5445 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5448 SCTP_TCB_UNLOCK(locked_tcb);
5453 if ((stcb) && netp && *netp) {
5454 if (stcb->asoc.asconf_supported == 0) {
5457 /* He's alive so give him credit */
5458 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5459 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5460 stcb->asoc.overall_error_count,
5462 SCTP_FROM_SCTP_INPUT,
5465 stcb->asoc.overall_error_count = 0;
5466 sctp_handle_asconf_ack(m, *offset,
5467 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5468 if (abort_no_unlock)
5472 case SCTP_FORWARD_CUM_TSN:
5473 case SCTP_IFORWARD_CUM_TSN:
5474 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5475 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5478 SCTP_TCB_UNLOCK(locked_tcb);
5483 /* He's alive so give him credit */
5487 if (stcb->asoc.prsctp_supported == 0) {
5490 stcb->asoc.overall_error_count = 0;
5491 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5492 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5493 stcb->asoc.overall_error_count,
5495 SCTP_FROM_SCTP_INPUT,
5499 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5500 /* We are not interested anymore */
5501 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5502 so = SCTP_INP_SO(inp);
5503 atomic_add_int(&stcb->asoc.refcnt, 1);
5504 SCTP_TCB_UNLOCK(stcb);
5505 SCTP_SOCKET_LOCK(so, 1);
5506 SCTP_TCB_LOCK(stcb);
5507 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5509 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5510 SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5511 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5512 SCTP_SOCKET_UNLOCK(so, 1);
5518 * For sending a SACK this looks like DATA
5521 stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from;
5522 sctp_handle_forward_tsn(stcb,
5523 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5528 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5529 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5530 stcb->asoc.overall_error_count,
5532 SCTP_FROM_SCTP_INPUT,
5535 stcb->asoc.overall_error_count = 0;
5540 case SCTP_STREAM_RESET:
5541 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5542 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5545 SCTP_TCB_UNLOCK(locked_tcb);
5550 if (stcb->asoc.reconfig_supported == 0) {
5553 if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5554 /* stop processing */
5559 case SCTP_PACKET_DROPPED:
5560 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5561 /* re-get it all please */
5562 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5565 SCTP_TCB_UNLOCK(locked_tcb);
5570 if (ch && (stcb) && netp && (*netp)) {
5571 if (stcb->asoc.pktdrop_supported == 0) {
5574 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5576 min(chk_length, (sizeof(chunk_buf) - 4)));
5580 case SCTP_AUTHENTICATION:
5581 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5583 /* save the first AUTH for later processing */
5584 if (auth_skipped == 0) {
5585 auth_offset = *offset;
5586 auth_len = chk_length;
5589 /* skip this chunk (temporarily) */
5592 if (stcb->asoc.auth_supported == 0) {
5595 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5596 (chk_length > (sizeof(struct sctp_auth_chunk) +
5597 SCTP_AUTH_DIGEST_LEN_MAX))) {
5600 SCTP_TCB_UNLOCK(locked_tcb);
5605 if (got_auth == 1) {
5606 /* skip this chunk... it's already auth'd */
5610 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5612 /* auth HMAC failed so dump the packet */
5616 /* remaining chunks are HMAC checked */
5617 stcb->asoc.authenticated = 1;
5623 /* it's an unknown chunk! */
5624 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5625 struct sctp_gen_error_cause *cause;
5628 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5629 0, M_NOWAIT, 1, MT_DATA);
5630 if (op_err != NULL) {
5631 len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset));
5632 cause = mtod(op_err, struct sctp_gen_error_cause *);
5633 cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5634 cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause)));
5635 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
5636 SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
5637 if (SCTP_BUF_NEXT(op_err) != NULL) {
5638 #ifdef SCTP_MBUF_LOGGING
5639 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5640 sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5643 sctp_queue_op_err(stcb, op_err);
5645 sctp_m_freem(op_err);
5649 if ((ch->chunk_type & 0x80) == 0) {
5650 /* discard this packet */
5653 } /* else skip this bad chunk and continue... */
5655 } /* switch (ch->chunk_type) */
5659 /* get the next chunk */
5660 *offset += SCTP_SIZE32(chk_length);
5661 if (*offset >= length) {
5662 /* no more data left in the mbuf chain */
5665 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5666 sizeof(struct sctp_chunkhdr), chunk_buf);
5669 SCTP_TCB_UNLOCK(locked_tcb);
5676 if (asconf_cnt > 0 && stcb != NULL) {
5677 sctp_send_asconf_ack(stcb);
5684 * common input chunk processing (v4 and v6)
5687 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5688 struct sockaddr *src, struct sockaddr *dst,
5689 struct sctphdr *sh, struct sctp_chunkhdr *ch,
5690 #if !defined(SCTP_WITH_NO_CSUM)
5691 uint8_t compute_crc,
5694 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5695 uint32_t vrf_id, uint16_t port)
5698 int fwd_tsn_seen = 0, data_processed = 0;
5699 struct mbuf *m = *mm, *op_err;
5700 char msg[SCTP_DIAG_INFO_LEN];
5702 int cnt_ctrl_ready = 0;
5703 struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5704 struct sctp_tcb *stcb = NULL;
5705 struct sctp_nets *net = NULL;
5707 SCTP_STAT_INCR(sctps_recvdatagrams);
5708 #ifdef SCTP_AUDITING_ENABLED
5709 sctp_audit_log(0xE0, 1);
5710 sctp_auditing(0, inp, stcb, net);
5712 #if !defined(SCTP_WITH_NO_CSUM)
5713 if (compute_crc != 0) {
5714 uint32_t check, calc_check;
5716 check = sh->checksum;
5718 calc_check = sctp_calculate_cksum(m, iphlen);
5719 sh->checksum = check;
5720 if (calc_check != check) {
5721 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5722 calc_check, check, (void *)m, length, iphlen);
5723 stcb = sctp_findassociation_addr(m, offset, src, dst,
5724 sh, ch, &inp, &net, vrf_id);
5725 #if defined(INET) || defined(INET6)
5726 if ((ch->chunk_type != SCTP_INITIATION) &&
5727 (net != NULL) && (net->port != port)) {
5728 if (net->port == 0) {
5729 /* UDP encapsulation turned on. */
5730 net->mtu -= sizeof(struct udphdr);
5731 if (stcb->asoc.smallest_mtu > net->mtu) {
5732 sctp_pathmtu_adjustment(stcb, net->mtu);
5734 } else if (port == 0) {
5735 /* UDP encapsulation turned off. */
5736 net->mtu += sizeof(struct udphdr);
5737 /* XXX Update smallest_mtu */
5743 net->flowtype = mflowtype;
5744 net->flowid = mflowid;
5746 if ((inp != NULL) && (stcb != NULL)) {
5747 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5748 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5749 } else if ((inp != NULL) && (stcb == NULL)) {
5752 SCTP_STAT_INCR(sctps_badsum);
5753 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5758 /* Destination port of 0 is illegal, based on RFC4960. */
5759 if (sh->dest_port == 0) {
5760 SCTP_STAT_INCR(sctps_hdrops);
5763 stcb = sctp_findassociation_addr(m, offset, src, dst,
5764 sh, ch, &inp, &net, vrf_id);
5765 #if defined(INET) || defined(INET6)
5766 if ((ch->chunk_type != SCTP_INITIATION) &&
5767 (net != NULL) && (net->port != port)) {
5768 if (net->port == 0) {
5769 /* UDP encapsulation turned on. */
5770 net->mtu -= sizeof(struct udphdr);
5771 if (stcb->asoc.smallest_mtu > net->mtu) {
5772 sctp_pathmtu_adjustment(stcb, net->mtu);
5774 } else if (port == 0) {
5775 /* UDP encapsulation turned off. */
5776 net->mtu += sizeof(struct udphdr);
5777 /* XXX Update smallest_mtu */
5783 net->flowtype = mflowtype;
5784 net->flowid = mflowid;
5787 SCTP_STAT_INCR(sctps_noport);
5788 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5791 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5792 sctp_send_shutdown_complete2(src, dst, sh,
5793 mflowtype, mflowid, fibnum,
5797 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5800 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5801 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5802 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5803 (ch->chunk_type != SCTP_INIT))) {
5804 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5806 sctp_send_abort(m, iphlen, src, dst,
5808 mflowtype, mflowid, fibnum,
5813 } else if (stcb == NULL) {
5816 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5817 (void *)m, iphlen, offset, length, (void *)stcb);
5819 /* always clear this before beginning a packet */
5820 stcb->asoc.authenticated = 0;
5821 stcb->asoc.seen_a_sack_this_pkt = 0;
5822 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5823 (void *)stcb, stcb->asoc.state);
5825 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5826 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5828 * If we hit here, we had a ref count
5829 * up when the assoc was aborted and the
5830 * timer is clearing out the assoc, we should
5831 * NOT respond to any packet.. its OOTB.
5833 SCTP_TCB_UNLOCK(stcb);
5835 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5836 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5838 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5839 mflowtype, mflowid, inp->fibnum,
5844 if (IS_SCTP_CONTROL(ch)) {
5845 /* process the control portion of the SCTP packet */
5846 /* sa_ignore NO_NULL_CHK */
5847 stcb = sctp_process_control(m, iphlen, &offset, length,
5849 inp, stcb, &net, &fwd_tsn_seen,
5850 mflowtype, mflowid, fibnum,
5854 * This covers us if the cookie-echo was there and
5855 * it changes our INP.
5857 inp = stcb->sctp_ep;
5858 #if defined(INET) || defined(INET6)
5859 if ((ch->chunk_type != SCTP_INITIATION) &&
5860 (net != NULL) && (net->port != port)) {
5861 if (net->port == 0) {
5862 /* UDP encapsulation turned on. */
5863 net->mtu -= sizeof(struct udphdr);
5864 if (stcb->asoc.smallest_mtu > net->mtu) {
5865 sctp_pathmtu_adjustment(stcb, net->mtu);
5867 } else if (port == 0) {
5868 /* UDP encapsulation turned off. */
5869 net->mtu += sizeof(struct udphdr);
5870 /* XXX Update smallest_mtu */
5878 * no control chunks, so pre-process DATA chunks (these
5879 * checks are taken care of by control processing)
5883 * if DATA only packet, and auth is required, then punt...
5884 * can't have authenticated without any AUTH (control)
5887 if ((stcb != NULL) &&
5888 (stcb->asoc.auth_supported == 1) &&
5889 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5890 /* "silently" ignore */
5891 SCTP_STAT_INCR(sctps_recvauthmissing);
5895 /* out of the blue DATA chunk */
5896 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5897 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5899 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5900 mflowtype, mflowid, fibnum,
5904 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5905 /* v_tag mismatch! */
5906 SCTP_STAT_INCR(sctps_badvtag);
5913 * no valid TCB for this packet, or we found it's a bad
5914 * packet while processing control, or we're done with this
5915 * packet (done or skip rest of data), so we drop it...
5920 * DATA chunk processing
5922 /* plow through the data chunks while length > offset */
5925 * Rest should be DATA only. Check authentication state if AUTH for
5928 if ((length > offset) &&
5930 (stcb->asoc.auth_supported == 1) &&
5931 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5932 !stcb->asoc.authenticated) {
5933 /* "silently" ignore */
5934 SCTP_STAT_INCR(sctps_recvauthmissing);
5935 SCTPDBG(SCTP_DEBUG_AUTH1,
5936 "Data chunk requires AUTH, skipped\n");
5939 if (length > offset) {
5943 * First check to make sure our state is correct. We would
5944 * not get here unless we really did have a tag, so we don't
5945 * abort if this happens, just dump the chunk silently.
5947 switch (SCTP_GET_STATE(&stcb->asoc)) {
5948 case SCTP_STATE_COOKIE_ECHOED:
5950 * we consider data with valid tags in this state
5951 * shows us the cookie-ack was lost. Imply it was
5954 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5955 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5956 stcb->asoc.overall_error_count,
5958 SCTP_FROM_SCTP_INPUT,
5961 stcb->asoc.overall_error_count = 0;
5962 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5964 case SCTP_STATE_COOKIE_WAIT:
5966 * We consider OOTB any data sent during asoc setup.
5968 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5969 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5971 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5972 mflowtype, mflowid, inp->fibnum,
5975 /* sa_ignore NOTREACHED */
5977 case SCTP_STATE_EMPTY: /* should not happen */
5978 case SCTP_STATE_INUSE: /* should not happen */
5979 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5980 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5983 /* sa_ignore NOTREACHED */
5985 case SCTP_STATE_OPEN:
5986 case SCTP_STATE_SHUTDOWN_SENT:
5989 /* plow through the data chunks while length > offset */
5990 retval = sctp_process_data(mm, iphlen, &offset, length,
5991 inp, stcb, net, &high_tsn);
5994 * The association aborted, NO UNLOCK needed since
5995 * the association is destroyed.
6002 * Anything important needs to have been m_copy'ed in
6006 /* take care of ecn */
6007 if ((data_processed == 1) &&
6008 (stcb->asoc.ecn_supported == 1) &&
6009 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
6010 /* Yep, we need to add a ECNE */
6011 sctp_send_ecn_echo(stcb, net, high_tsn);
6013 if ((data_processed == 0) && (fwd_tsn_seen)) {
6015 uint32_t highest_tsn;
6017 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
6018 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
6020 highest_tsn = stcb->asoc.highest_tsn_inside_map;
6022 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
6023 stcb->asoc.send_sack = 1;
6024 sctp_sack_check(stcb, was_a_gap);
6025 } else if (fwd_tsn_seen) {
6026 stcb->asoc.send_sack = 1;
6028 /* trigger send of any chunks in queue... */
6030 #ifdef SCTP_AUDITING_ENABLED
6031 sctp_audit_log(0xE0, 2);
6032 sctp_auditing(1, inp, stcb, net);
6034 SCTPDBG(SCTP_DEBUG_INPUT1,
6035 "Check for chunk output prw:%d tqe:%d tf=%d\n",
6036 stcb->asoc.peers_rwnd,
6037 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
6038 stcb->asoc.total_flight);
6039 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
6040 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
6041 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
6043 if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
6045 stcb->asoc.trigger_reset ||
6047 (stcb->asoc.peers_rwnd > 0 ||
6048 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
6049 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
6050 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
6051 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
6053 #ifdef SCTP_AUDITING_ENABLED
6054 sctp_audit_log(0xE0, 3);
6055 sctp_auditing(2, inp, stcb, net);
6059 SCTP_TCB_UNLOCK(stcb);
6061 if (inp_decr != NULL) {
6062 /* reduce ref-count */
6063 SCTP_INP_WLOCK(inp_decr);
6064 SCTP_INP_DECR_REF(inp_decr);
6065 SCTP_INP_WUNLOCK(inp_decr);
6072 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
6076 uint32_t vrf_id = 0;
6078 struct sockaddr_in src, dst;
6081 struct sctp_chunkhdr *ch;
6083 #if !defined(SCTP_WITH_NO_CSUM)
6084 uint8_t compute_crc;
6091 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
6092 SCTP_RELEASE_PKT(i_pak);
6095 m = SCTP_HEADER_TO_CHAIN(i_pak);
6096 #ifdef SCTP_MBUF_LOGGING
6097 /* Log in any input mbufs */
6098 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6099 sctp_log_mbc(m, SCTP_MBUF_INPUT);
6102 #ifdef SCTP_PACKET_LOGGING
6103 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
6107 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6108 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
6110 if_name(m->m_pkthdr.rcvif),
6111 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
6112 mflowid = m->m_pkthdr.flowid;
6113 mflowtype = M_HASHTYPE_GET(m);
6114 fibnum = M_GETFIB(m);
6115 SCTP_STAT_INCR(sctps_recvpackets);
6116 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
6117 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
6118 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6119 if (SCTP_BUF_LEN(m) < offset) {
6120 if ((m = m_pullup(m, offset)) == NULL) {
6121 SCTP_STAT_INCR(sctps_hdrops);
6125 ip = mtod(m, struct ip *);
6126 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
6127 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
6128 offset -= sizeof(struct sctp_chunkhdr);
6129 memset(&src, 0, sizeof(struct sockaddr_in));
6130 src.sin_family = AF_INET;
6131 src.sin_len = sizeof(struct sockaddr_in);
6132 src.sin_port = sh->src_port;
6133 src.sin_addr = ip->ip_src;
6134 memset(&dst, 0, sizeof(struct sockaddr_in));
6135 dst.sin_family = AF_INET;
6136 dst.sin_len = sizeof(struct sockaddr_in);
6137 dst.sin_port = sh->dest_port;
6138 dst.sin_addr = ip->ip_dst;
6139 length = ntohs(ip->ip_len);
6140 /* Validate mbuf chain length with IP payload length. */
6141 if (SCTP_HEADER_LEN(m) != length) {
6142 SCTPDBG(SCTP_DEBUG_INPUT1,
6143 "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
6144 SCTP_STAT_INCR(sctps_hdrops);
6147 /* SCTP does not allow broadcasts or multicasts */
6148 if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
6151 if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
6154 ecn_bits = ip->ip_tos;
6155 #if defined(SCTP_WITH_NO_CSUM)
6156 SCTP_STAT_INCR(sctps_recvnocrc);
6158 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
6159 SCTP_STAT_INCR(sctps_recvhwcrc);
6162 SCTP_STAT_INCR(sctps_recvswcrc);
6166 sctp_common_input_processing(&m, iphlen, offset, length,
6167 (struct sockaddr *)&src,
6168 (struct sockaddr *)&dst,
6170 #if !defined(SCTP_WITH_NO_CSUM)
6174 mflowtype, mflowid, fibnum,
6183 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6184 extern int *sctp_cpuarry;
6188 sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
6195 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6201 uint32_t flowid, tag;
6203 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
6204 flowid = m->m_pkthdr.flowid;
6207 * No flow id built by lower layers fix it so we
6210 offset = off + sizeof(struct sctphdr);
6211 if (SCTP_BUF_LEN(m) < offset) {
6212 if ((m = m_pullup(m, offset)) == NULL) {
6213 SCTP_STAT_INCR(sctps_hdrops);
6214 return (IPPROTO_DONE);
6217 ip = mtod(m, struct ip *);
6218 sh = (struct sctphdr *)((caddr_t)ip + off);
6219 tag = htonl(sh->v_tag);
6220 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6221 m->m_pkthdr.flowid = flowid;
6222 M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH);
6224 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6225 sctp_queue_to_mcore(m, off, cpu_to_use);
6226 return (IPPROTO_DONE);
6229 sctp_input_with_port(m, off, 0);
6230 return (IPPROTO_DONE);