2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2011, 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 /* $KAME: sctp_input.c,v 1.27 2005/03/06 16:04:17 itojun Exp $ */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <netinet/sctp_os.h>
39 #include <netinet/sctp_var.h>
40 #include <netinet/sctp_sysctl.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctp_header.h>
43 #include <netinet/sctputil.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_input.h>
46 #include <netinet/sctp_auth.h>
47 #include <netinet/sctp_indata.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctp_bsd_addr.h>
50 #include <netinet/sctp_timer.h>
51 #include <netinet/sctp_crc32.h>
52 #include <netinet/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, struct sctphdr *sh,
86 struct sctp_init_chunk *cp, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
87 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id, uint16_t port)
89 struct sctp_init *init;
93 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
97 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
103 /* First are we accepting? */
104 if ((inp->sctp_socket->so_qlimit == 0) && (stcb == NULL)) {
105 SCTPDBG(SCTP_DEBUG_INPUT2,
106 "sctp_handle_init: Abort, so_qlimit:%d\n",
107 inp->sctp_socket->so_qlimit);
109 * FIX ME ?? What about TCP model and we have a
110 * match/restart case? Actually no fix is needed. the lookup
111 * will always find the existing assoc so stcb would not be
112 * NULL. It may be questionable to do this since we COULD
113 * just send back the INIT-ACK and hope that the app did
114 * accept()'s by the time the COOKIE was sent. But there is
115 * a price to pay for COOKIE generation and I don't want to
116 * pay it on the chance that the app will actually do some
117 * accepts(). The App just looses and should NOT be in this
120 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
123 *abort_no_unlock = 1;
126 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
128 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
129 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
132 *abort_no_unlock = 1;
135 /* validate parameters */
136 if (init->initiate_tag == 0) {
137 /* protocol error... send abort */
138 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
139 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
142 *abort_no_unlock = 1;
145 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
146 /* invalid parameter... send abort */
147 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
148 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
151 *abort_no_unlock = 1;
154 if (init->num_inbound_streams == 0) {
155 /* protocol error... send abort */
156 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
157 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
160 *abort_no_unlock = 1;
163 if (init->num_outbound_streams == 0) {
164 /* protocol error... send abort */
165 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
166 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
169 *abort_no_unlock = 1;
172 init_limit = offset + ntohs(cp->ch.chunk_length);
173 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
175 /* auth parameter(s) error... send abort */
176 sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id, port);
178 *abort_no_unlock = 1;
181 /* send an INIT-ACK w/cookie */
182 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
183 sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id, port,
184 ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED));
187 SCTP_INP_RUNLOCK(inp);
192 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
196 sctp_is_there_unsent_data(struct sctp_tcb *stcb)
200 struct sctp_stream_queue_pending *sp;
201 struct sctp_association *asoc;
204 * This function returns the number of streams that have true unsent
205 * data on them. Note that as it looks through it will clean up any
206 * places that have old data that has been sent but left at top of
210 SCTP_TCB_SEND_LOCK(stcb);
211 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
212 /* Check to see if some data queued */
213 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
214 /* sa_ignore FREED_MEMORY */
215 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
219 if ((sp->msg_is_complete) &&
221 (sp->sender_all_done)) {
223 * We are doing differed cleanup. Last time
224 * through when we took all the data the
225 * sender_all_done was not set.
227 if (sp->put_last_out == 0) {
228 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
229 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
235 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
236 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
238 sctp_free_remote_addr(sp->net);
242 sctp_m_freem(sp->data);
245 sctp_free_a_strmoq(stcb, sp);
252 SCTP_TCB_SEND_UNLOCK(stcb);
253 return (unsent_data);
257 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb,
258 struct sctp_nets *net)
260 struct sctp_init *init;
261 struct sctp_association *asoc;
262 struct sctp_nets *lnet;
267 /* save off parameters */
268 asoc->peer_vtag = ntohl(init->initiate_tag);
269 asoc->peers_rwnd = ntohl(init->a_rwnd);
271 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
273 if (!TAILQ_EMPTY(&asoc->nets)) {
274 /* update any ssthresh's that may have a default */
275 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
276 lnet->ssthresh = asoc->peers_rwnd;
277 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
278 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
282 SCTP_TCB_SEND_LOCK(stcb);
283 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
285 struct sctp_stream_out *outs;
286 struct sctp_stream_queue_pending *sp, *nsp;
287 struct sctp_tmit_chunk *chk, *nchk;
289 /* abandon the upper streams */
290 newcnt = ntohs(init->num_inbound_streams);
291 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
292 if (chk->rec.data.stream_number >= newcnt) {
293 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
294 asoc->send_queue_cnt--;
295 if (chk->data != NULL) {
296 sctp_free_bufspace(stcb, asoc, chk, 1);
297 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
298 SCTP_NOTIFY_DATAGRAM_UNSENT, chk, SCTP_SO_NOT_LOCKED);
300 sctp_m_freem(chk->data);
304 sctp_free_a_chunk(stcb, chk);
305 /* sa_ignore FREED_MEMORY */
309 for (i = newcnt; i < asoc->pre_open_streams; i++) {
310 outs = &asoc->strmout[i];
311 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
312 TAILQ_REMOVE(&outs->outqueue, sp, next);
313 asoc->stream_queue_cnt--;
314 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
315 stcb, SCTP_NOTIFY_DATAGRAM_UNSENT,
316 sp, SCTP_SO_NOT_LOCKED);
318 sctp_m_freem(sp->data);
322 sctp_free_remote_addr(sp->net);
326 sctp_free_a_strmoq(stcb, sp);
327 /* sa_ignore FREED_MEMORY */
331 /* cut back the count */
332 asoc->pre_open_streams = newcnt;
334 SCTP_TCB_SEND_UNLOCK(stcb);
335 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
337 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
338 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
339 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
340 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
342 /* This is the next one we expect */
343 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
345 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
346 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
348 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
349 /* open the requested streams */
351 if (asoc->strmin != NULL) {
352 /* Free the old ones */
353 struct sctp_queued_to_read *ctl, *nctl;
355 for (i = 0; i < asoc->streamincnt; i++) {
356 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) {
357 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
358 sctp_free_remote_addr(ctl->whoFrom);
360 sctp_m_freem(ctl->data);
362 sctp_free_a_readq(stcb, ctl);
365 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
367 asoc->streamincnt = ntohs(init->num_outbound_streams);
368 if (asoc->streamincnt > MAX_SCTP_STREAMS) {
369 asoc->streamincnt = MAX_SCTP_STREAMS;
371 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
372 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
373 if (asoc->strmin == NULL) {
374 /* we didn't get memory for the streams! */
375 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
378 for (i = 0; i < asoc->streamincnt; i++) {
379 asoc->strmin[i].stream_no = i;
380 asoc->strmin[i].last_sequence_delivered = 0xffff;
382 * U-stream ranges will be set when the cookie is unpacked.
383 * Or for the INIT sender they are un set (if pr-sctp not
384 * supported) when the INIT-ACK arrives.
386 TAILQ_INIT(&asoc->strmin[i].inqueue);
387 asoc->strmin[i].delivery_started = 0;
390 * load_address_from_init will put the addresses into the
391 * association when the COOKIE is processed or the INIT-ACK is
392 * processed. Both types of COOKIE's existing and new call this
393 * routine. It will remove addresses that are no longer in the
394 * association (for the restarting case where addresses are
395 * removed). Up front when the INIT arrives we will discard it if it
396 * is a restart and new addresses have been added.
398 /* sa_ignore MEMLEAK */
403 * INIT-ACK message processing/consumption returns value < 0 on error
406 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
407 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
408 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id)
410 struct sctp_association *asoc;
412 int retval, abort_flag;
413 uint32_t initack_limit;
414 int nat_friendly = 0;
416 /* First verify that we have no illegal param's */
420 op_err = sctp_arethere_unrecognized_parameters(m,
421 (offset + sizeof(struct sctp_init_chunk)),
422 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
424 /* Send an abort and notify peer */
425 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_CAUSE_PROTOCOL_VIOLATION, op_err, SCTP_SO_NOT_LOCKED);
426 *abort_no_unlock = 1;
430 asoc->peer_supports_nat = (uint8_t) nat_friendly;
431 /* process the peer's parameters in the INIT-ACK */
432 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net);
436 initack_limit = offset + ntohs(cp->ch.chunk_length);
437 /* load all addresses */
438 if ((retval = sctp_load_addresses_from_init(stcb, m, iphlen,
439 (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh,
441 /* Huh, we should abort */
442 SCTPDBG(SCTP_DEBUG_INPUT1,
443 "Load addresses from INIT causes an abort %d\n",
445 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
447 *abort_no_unlock = 1;
450 /* if the peer doesn't support asconf, flush the asconf queue */
451 if (asoc->peer_supports_asconf == 0) {
452 struct sctp_asconf_addr *param, *nparam;
454 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
455 TAILQ_REMOVE(&asoc->asconf_queue, param, next);
456 SCTP_FREE(param, SCTP_M_ASC_ADDR);
459 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
460 stcb->asoc.local_hmacs);
462 sctp_queue_op_err(stcb, op_err);
463 /* queuing will steal away the mbuf chain to the out queue */
466 /* extract the cookie and queue it to "echo" it back... */
467 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
468 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
469 stcb->asoc.overall_error_count,
471 SCTP_FROM_SCTP_INPUT,
474 stcb->asoc.overall_error_count = 0;
475 net->error_count = 0;
478 * Cancel the INIT timer, We do this first before queueing the
479 * cookie. We always cancel at the primary to assue that we are
480 * canceling the timer started by the INIT which always goes to the
483 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
484 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
486 /* calculate the RTO */
487 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy,
488 SCTP_DETERMINE_LL_NOTOK);
490 retval = sctp_send_cookie_echo(m, offset, stcb, net);
493 * No cookie, we probably should send a op error. But in any
494 * case if there is no cookie in the INIT-ACK, we can
495 * abandon the peer, its broke.
498 /* We abort with an error of missing mandatory param */
500 sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM);
503 * Expand beyond to include the mandatory
506 struct sctp_inv_mandatory_param *mp;
508 SCTP_BUF_LEN(op_err) =
509 sizeof(struct sctp_inv_mandatory_param);
511 struct sctp_inv_mandatory_param *);
512 /* Subtract the reserved param */
514 htons(sizeof(struct sctp_inv_mandatory_param) - 2);
515 mp->num_param = htonl(1);
516 mp->param = htons(SCTP_STATE_COOKIE);
519 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
520 sh, op_err, 0, net->port);
521 *abort_no_unlock = 1;
529 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
530 struct sctp_tcb *stcb, struct sctp_nets *net)
532 struct sockaddr_storage store;
533 struct sockaddr_in *sin;
534 struct sockaddr_in6 *sin6;
535 struct sctp_nets *r_net, *f_net;
539 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
543 sin = (struct sockaddr_in *)&store;
544 sin6 = (struct sockaddr_in6 *)&store;
546 memset(&store, 0, sizeof(store));
547 if (cp->heartbeat.hb_info.addr_family == AF_INET &&
548 cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
549 sin->sin_family = cp->heartbeat.hb_info.addr_family;
550 sin->sin_len = cp->heartbeat.hb_info.addr_len;
551 sin->sin_port = stcb->rport;
552 memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address,
553 sizeof(sin->sin_addr));
554 } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 &&
555 cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
556 sin6->sin6_family = cp->heartbeat.hb_info.addr_family;
557 sin6->sin6_len = cp->heartbeat.hb_info.addr_len;
558 sin6->sin6_port = stcb->rport;
559 memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address,
560 sizeof(sin6->sin6_addr));
564 r_net = sctp_findnet(stcb, (struct sockaddr *)sin);
566 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
569 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
570 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
571 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
573 * If the its a HB and it's random value is correct when can
574 * confirm the destination.
576 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
577 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
578 stcb->asoc.primary_destination = r_net;
579 r_net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
580 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
581 f_net = TAILQ_FIRST(&stcb->asoc.nets);
582 if (f_net != r_net) {
584 * first one on the list is NOT the primary
585 * sctp_cmpaddr() is much more efficent if
586 * the primary is the first on the list,
589 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
590 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
594 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
595 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
597 r_net->error_count = 0;
598 r_net->hb_responded = 1;
599 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
600 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
601 if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
602 r_net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE;
603 r_net->dest_state |= SCTP_ADDR_REACHABLE;
604 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
605 SCTP_HEARTBEAT_SUCCESS, (void *)r_net, SCTP_SO_NOT_LOCKED);
606 /* now was it the primary? if so restore */
607 if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) {
608 (void)sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net);
612 * JRS 5/14/07 - If CMT PF is on and the destination is in PF state,
613 * set the destination to active state and set the cwnd to one or
614 * two MTU's based on whether PF1 or PF2 is being used. If a T3
615 * timer is running, for the destination, stop the timer because a
616 * PF-heartbeat was received.
618 if ((stcb->asoc.sctp_cmt_on_off > 0) &&
619 (stcb->asoc.sctp_cmt_pf > 0) &&
620 ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
621 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
622 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
624 SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
626 net->dest_state &= ~SCTP_ADDR_PF;
627 net->cwnd = net->mtu * stcb->asoc.sctp_cmt_pf;
628 SCTPDBG(SCTP_DEBUG_INPUT1, "Destination %p moved from PF to reachable with cwnd %d.\n",
631 /* Now lets do a RTO with this */
632 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy,
633 SCTP_DETERMINE_LL_OK);
634 /* Mobility adaptation */
636 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
637 SCTP_MOBILITY_BASE) ||
638 sctp_is_mobility_feature_on(stcb->sctp_ep,
639 SCTP_MOBILITY_FASTHANDOFF)) &&
640 sctp_is_mobility_feature_on(stcb->sctp_ep,
641 SCTP_MOBILITY_PRIM_DELETED)) {
643 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7);
644 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
645 SCTP_MOBILITY_FASTHANDOFF)) {
646 sctp_assoc_immediate_retrans(stcb,
647 stcb->asoc.primary_destination);
649 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
650 SCTP_MOBILITY_BASE)) {
651 sctp_move_chunks_from_net(stcb,
652 stcb->asoc.deleted_primary);
654 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
655 stcb->asoc.deleted_primary);
661 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
664 * return 0 means we want you to proceed with the abort non-zero
665 * means no abort processing
667 struct sctpasochead *head;
669 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
670 /* generate a new vtag and send init */
671 LIST_REMOVE(stcb, sctp_asocs);
672 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
673 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
675 * put it in the bucket in the vtag hash of assoc's for the
678 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
679 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
682 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
684 * treat like a case where the cookie expired i.e.: - dump
685 * current cookie. - generate a new vtag. - resend init.
687 /* generate a new vtag and send init */
688 LIST_REMOVE(stcb, sctp_asocs);
689 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
690 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
691 sctp_stop_all_cookie_timers(stcb);
692 sctp_toss_old_cookies(stcb, &stcb->asoc);
693 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
694 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
696 * put it in the bucket in the vtag hash of assoc's for the
699 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
700 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
707 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
708 struct sctp_nets *net)
711 * return 0 means we want you to proceed with the abort non-zero
712 * means no abort processing
714 if (stcb->asoc.peer_supports_auth == 0) {
715 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
718 sctp_asconf_send_nat_state_update(stcb, net);
724 sctp_handle_abort(struct sctp_abort_chunk *cp,
725 struct sctp_tcb *stcb, struct sctp_nets *net)
727 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
733 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
737 len = ntohs(cp->ch.chunk_length);
738 if (len > sizeof(struct sctp_chunkhdr)) {
740 * Need to check the cause codes for our two magic nat
741 * aborts which don't kill the assoc necessarily.
743 struct sctp_abort_chunk *cpnext;
744 struct sctp_missing_nat_state *natc;
749 natc = (struct sctp_missing_nat_state *)cpnext;
750 cause = ntohs(natc->cause);
751 if (cause == SCTP_CAUSE_NAT_COLLIDING_STATE) {
752 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
754 if (sctp_handle_nat_colliding_state(stcb)) {
757 } else if (cause == SCTP_CAUSE_NAT_MISSING_STATE) {
758 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
760 if (sctp_handle_nat_missing_state(stcb, net)) {
765 /* stop any receive timers */
766 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
767 /* notify user of the abort and clean up... */
768 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED);
770 #if defined(SCTP_PANIC_ON_ABORT)
771 printf("stcb:%p state:%d rport:%d net:%p\n",
772 stcb, stcb->asoc.state, stcb->rport, net);
773 if (!(stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
774 panic("Received an ABORT");
776 printf("No panic its in state %x closed\n", stcb->asoc.state);
779 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
780 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
781 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
782 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
784 #ifdef SCTP_ASOCLOG_OF_TSNS
785 sctp_print_out_track_log(stcb);
787 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
788 so = SCTP_INP_SO(stcb->sctp_ep);
789 atomic_add_int(&stcb->asoc.refcnt, 1);
790 SCTP_TCB_UNLOCK(stcb);
791 SCTP_SOCKET_LOCK(so, 1);
793 atomic_subtract_int(&stcb->asoc.refcnt, 1);
795 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
796 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
797 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
798 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
799 SCTP_SOCKET_UNLOCK(so, 1);
801 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
805 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
806 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
808 struct sctp_association *asoc;
809 int some_on_streamwheel;
811 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
816 SCTPDBG(SCTP_DEBUG_INPUT2,
817 "sctp_handle_shutdown: handling SHUTDOWN\n");
821 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
822 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
825 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
826 /* Shutdown NOT the expected size */
829 sctp_update_acked(stcb, cp, net, abort_flag);
834 if (asoc->control_pdapi) {
836 * With a normal shutdown we assume the end of last record.
838 SCTP_INP_READ_LOCK(stcb->sctp_ep);
839 asoc->control_pdapi->end_added = 1;
840 asoc->control_pdapi->pdapi_aborted = 1;
841 asoc->control_pdapi = NULL;
842 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
843 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
844 so = SCTP_INP_SO(stcb->sctp_ep);
845 atomic_add_int(&stcb->asoc.refcnt, 1);
846 SCTP_TCB_UNLOCK(stcb);
847 SCTP_SOCKET_LOCK(so, 1);
849 atomic_subtract_int(&stcb->asoc.refcnt, 1);
850 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
851 /* assoc was freed while we were unlocked */
852 SCTP_SOCKET_UNLOCK(so, 1);
856 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
857 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
858 SCTP_SOCKET_UNLOCK(so, 1);
861 /* goto SHUTDOWN_RECEIVED state to block new requests */
862 if (stcb->sctp_socket) {
863 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
864 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
865 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
866 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
867 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
869 * notify upper layer that peer has initiated a
872 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
875 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
878 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
880 * stop the shutdown timer, since we WILL move to
883 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
885 /* Now is there unsent data on a stream somewhere? */
886 some_on_streamwheel = sctp_is_there_unsent_data(stcb);
888 if (!TAILQ_EMPTY(&asoc->send_queue) ||
889 !TAILQ_EMPTY(&asoc->sent_queue) ||
890 some_on_streamwheel) {
891 /* By returning we will push more data out */
894 /* no outstanding data to send, so move on... */
895 /* send SHUTDOWN-ACK */
896 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
897 /* move to SHUTDOWN-ACK-SENT state */
898 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
899 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
900 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
902 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
903 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
904 sctp_stop_timers_for_shutdown(stcb);
905 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
911 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp,
912 struct sctp_tcb *stcb,
913 struct sctp_nets *net)
915 struct sctp_association *asoc;
917 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
920 so = SCTP_INP_SO(stcb->sctp_ep);
922 SCTPDBG(SCTP_DEBUG_INPUT2,
923 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
928 /* process according to association state */
929 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
930 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
931 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
932 sctp_send_shutdown_complete(stcb, net, 1);
933 SCTP_TCB_UNLOCK(stcb);
936 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
937 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
938 /* unexpected SHUTDOWN-ACK... so ignore... */
939 SCTP_TCB_UNLOCK(stcb);
942 if (asoc->control_pdapi) {
944 * With a normal shutdown we assume the end of last record.
946 SCTP_INP_READ_LOCK(stcb->sctp_ep);
947 asoc->control_pdapi->end_added = 1;
948 asoc->control_pdapi->pdapi_aborted = 1;
949 asoc->control_pdapi = NULL;
950 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
951 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
952 atomic_add_int(&stcb->asoc.refcnt, 1);
953 SCTP_TCB_UNLOCK(stcb);
954 SCTP_SOCKET_LOCK(so, 1);
956 atomic_subtract_int(&stcb->asoc.refcnt, 1);
957 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
958 /* assoc was freed while we were unlocked */
959 SCTP_SOCKET_UNLOCK(so, 1);
963 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
964 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
965 SCTP_SOCKET_UNLOCK(so, 1);
968 /* are the queues empty? */
969 if (!TAILQ_EMPTY(&asoc->send_queue) ||
970 !TAILQ_EMPTY(&asoc->sent_queue) ||
971 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
972 sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED);
975 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
976 /* send SHUTDOWN-COMPLETE */
977 sctp_send_shutdown_complete(stcb, net, 0);
978 /* notify upper layer protocol */
979 if (stcb->sctp_socket) {
980 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
981 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
982 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
983 /* Set the connected flag to disconnected */
984 stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
987 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
988 /* free the TCB but first save off the ep */
989 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
990 atomic_add_int(&stcb->asoc.refcnt, 1);
991 SCTP_TCB_UNLOCK(stcb);
992 SCTP_SOCKET_LOCK(so, 1);
994 atomic_subtract_int(&stcb->asoc.refcnt, 1);
996 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
997 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
998 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
999 SCTP_SOCKET_UNLOCK(so, 1);
1004 * Skip past the param header and then we will find the chunk that caused the
1005 * problem. There are two possiblities ASCONF or FWD-TSN other than that and
1006 * our peer must be broken.
1009 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1010 struct sctp_nets *net)
1012 struct sctp_chunkhdr *chk;
1014 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1015 switch (chk->chunk_type) {
1016 case SCTP_ASCONF_ACK:
1018 sctp_asconf_cleanup(stcb, net);
1020 case SCTP_FORWARD_CUM_TSN:
1021 stcb->asoc.peer_supports_prsctp = 0;
1024 SCTPDBG(SCTP_DEBUG_INPUT2,
1025 "Peer does not support chunk type %d(%x)??\n",
1026 chk->chunk_type, (uint32_t) chk->chunk_type);
1032 * Skip past the param header and then we will find the param that caused the
1033 * problem. There are a number of param's in a ASCONF OR the prsctp param
1034 * these will turn of specific features.
1037 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1039 struct sctp_paramhdr *pbad;
1042 switch (ntohs(pbad->param_type)) {
1044 case SCTP_PRSCTP_SUPPORTED:
1045 stcb->asoc.peer_supports_prsctp = 0;
1047 case SCTP_SUPPORTED_CHUNK_EXT:
1049 /* draft-ietf-tsvwg-addip-sctp */
1050 case SCTP_HAS_NAT_SUPPORT:
1051 stcb->asoc.peer_supports_nat = 0;
1053 case SCTP_ADD_IP_ADDRESS:
1054 case SCTP_DEL_IP_ADDRESS:
1055 case SCTP_SET_PRIM_ADDR:
1056 stcb->asoc.peer_supports_asconf = 0;
1058 case SCTP_SUCCESS_REPORT:
1059 case SCTP_ERROR_CAUSE_IND:
1060 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1061 SCTPDBG(SCTP_DEBUG_INPUT2,
1062 "Turning off ASCONF to this strange peer\n");
1063 stcb->asoc.peer_supports_asconf = 0;
1066 SCTPDBG(SCTP_DEBUG_INPUT2,
1067 "Peer does not support param type %d(%x)??\n",
1068 pbad->param_type, (uint32_t) pbad->param_type);
1074 sctp_handle_error(struct sctp_chunkhdr *ch,
1075 struct sctp_tcb *stcb, struct sctp_nets *net)
1078 struct sctp_paramhdr *phdr;
1079 uint16_t error_type;
1081 struct sctp_association *asoc;
1084 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1089 /* parse through all of the errors and process */
1091 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1092 sizeof(struct sctp_chunkhdr));
1093 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1094 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1095 /* Process an Error Cause */
1096 error_type = ntohs(phdr->param_type);
1097 error_len = ntohs(phdr->param_length);
1098 if ((error_len > chklen) || (error_len == 0)) {
1099 /* invalid param length for this param */
1100 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1104 switch (error_type) {
1105 case SCTP_CAUSE_INVALID_STREAM:
1106 case SCTP_CAUSE_MISSING_PARAM:
1107 case SCTP_CAUSE_INVALID_PARAM:
1108 case SCTP_CAUSE_NO_USER_DATA:
1109 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1112 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1113 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1115 if (sctp_handle_nat_colliding_state(stcb)) {
1119 case SCTP_CAUSE_NAT_MISSING_STATE:
1120 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1122 if (sctp_handle_nat_missing_state(stcb, net)) {
1126 case SCTP_CAUSE_STALE_COOKIE:
1128 * We only act if we have echoed a cookie and are
1131 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1134 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1135 /* Save the time doubled */
1136 asoc->cookie_preserve_req = ntohl(*p) << 1;
1137 asoc->stale_cookie_count++;
1138 if (asoc->stale_cookie_count >
1139 asoc->max_init_times) {
1140 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED);
1141 /* now free the asoc */
1142 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1143 so = SCTP_INP_SO(stcb->sctp_ep);
1144 atomic_add_int(&stcb->asoc.refcnt, 1);
1145 SCTP_TCB_UNLOCK(stcb);
1146 SCTP_SOCKET_LOCK(so, 1);
1147 SCTP_TCB_LOCK(stcb);
1148 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1150 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1151 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1152 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1153 SCTP_SOCKET_UNLOCK(so, 1);
1157 /* blast back to INIT state */
1158 sctp_toss_old_cookies(stcb, &stcb->asoc);
1159 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1160 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1161 sctp_stop_all_cookie_timers(stcb);
1162 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1165 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1167 * Nothing we can do here, we don't do hostname
1168 * addresses so if the peer does not like my IPv6
1169 * (or IPv4 for that matter) it does not matter. If
1170 * they don't support that type of address, they can
1171 * NOT possibly get that packet type... i.e. with no
1172 * IPv6 you can't recieve a IPv6 packet. so we can
1173 * safely ignore this one. If we ever added support
1174 * for HOSTNAME Addresses, then we would need to do
1178 case SCTP_CAUSE_UNRECOG_CHUNK:
1179 sctp_process_unrecog_chunk(stcb, phdr, net);
1181 case SCTP_CAUSE_UNRECOG_PARAM:
1182 sctp_process_unrecog_param(stcb, phdr);
1184 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1186 * We ignore this since the timer will drive out a
1187 * new cookie anyway and there timer will drive us
1188 * to send a SHUTDOWN_COMPLETE. We can't send one
1189 * here since we don't have their tag.
1192 case SCTP_CAUSE_DELETING_LAST_ADDR:
1193 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1194 case SCTP_CAUSE_DELETING_SRC_ADDR:
1196 * We should NOT get these here, but in a
1199 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1202 case SCTP_CAUSE_OUT_OF_RESC:
1204 * And what, pray tell do we do with the fact that
1205 * the peer is out of resources? Not really sure we
1206 * could do anything but abort. I suspect this
1207 * should have came WITH an abort instead of in a
1212 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1216 adjust = SCTP_SIZE32(error_len);
1218 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1224 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1225 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1226 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id)
1228 struct sctp_init_ack *init_ack;
1229 struct mbuf *op_err;
1231 SCTPDBG(SCTP_DEBUG_INPUT2,
1232 "sctp_handle_init_ack: handling INIT-ACK\n");
1235 SCTPDBG(SCTP_DEBUG_INPUT2,
1236 "sctp_handle_init_ack: TCB is null\n");
1239 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1240 /* Invalid length */
1241 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1242 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1243 op_err, 0, net->port);
1244 *abort_no_unlock = 1;
1247 init_ack = &cp->init;
1248 /* validate parameters */
1249 if (init_ack->initiate_tag == 0) {
1250 /* protocol error... send an abort */
1251 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1252 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1253 op_err, 0, net->port);
1254 *abort_no_unlock = 1;
1257 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1258 /* protocol error... send an abort */
1259 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1260 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1261 op_err, 0, net->port);
1262 *abort_no_unlock = 1;
1265 if (init_ack->num_inbound_streams == 0) {
1266 /* protocol error... send an abort */
1267 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1268 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1269 op_err, 0, net->port);
1270 *abort_no_unlock = 1;
1273 if (init_ack->num_outbound_streams == 0) {
1274 /* protocol error... send an abort */
1275 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1276 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1277 op_err, 0, net->port);
1278 *abort_no_unlock = 1;
1281 /* process according to association state... */
1282 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1283 case SCTP_STATE_COOKIE_WAIT:
1284 /* this is the expected state for this chunk */
1285 /* process the INIT-ACK parameters */
1286 if (stcb->asoc.primary_destination->dest_state &
1287 SCTP_ADDR_UNCONFIRMED) {
1289 * The primary is where we sent the INIT, we can
1290 * always consider it confirmed when the INIT-ACK is
1291 * returned. Do this before we load addresses
1294 stcb->asoc.primary_destination->dest_state &=
1295 ~SCTP_ADDR_UNCONFIRMED;
1296 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1297 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1299 if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb,
1300 net, abort_no_unlock, vrf_id) < 0) {
1301 /* error in parsing parameters */
1304 /* update our state */
1305 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1306 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1308 /* reset the RTO calc */
1309 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1310 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1311 stcb->asoc.overall_error_count,
1313 SCTP_FROM_SCTP_INPUT,
1316 stcb->asoc.overall_error_count = 0;
1317 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1319 * collapse the init timer back in case of a exponential
1322 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1325 * the send at the end of the inbound data processing will
1326 * cause the cookie to be sent
1329 case SCTP_STATE_SHUTDOWN_SENT:
1330 /* incorrect state... discard */
1332 case SCTP_STATE_COOKIE_ECHOED:
1333 /* incorrect state... discard */
1335 case SCTP_STATE_OPEN:
1336 /* incorrect state... discard */
1338 case SCTP_STATE_EMPTY:
1339 case SCTP_STATE_INUSE:
1341 /* incorrect state... discard */
1345 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1349 static struct sctp_tcb *
1350 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1351 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1352 struct sctp_inpcb *inp, struct sctp_nets **netp,
1353 struct sockaddr *init_src, int *notification,
1354 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1355 uint32_t vrf_id, uint16_t port);
1359 * handle a state cookie for an existing association m: input packet mbuf
1360 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1361 * "split" mbuf and the cookie signature does not exist offset: offset into
1362 * mbuf to the cookie-echo chunk
1364 static struct sctp_tcb *
1365 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1366 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1367 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1368 struct sockaddr *init_src, int *notification, sctp_assoc_t * sac_assoc_id,
1369 uint32_t vrf_id, int auth_skipped, uint32_t auth_offset, uint32_t auth_len, uint16_t port)
1371 struct sctp_association *asoc;
1372 struct sctp_init_chunk *init_cp, init_buf;
1373 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1374 struct sctp_nets *net;
1375 struct mbuf *op_err;
1376 struct sctp_paramhdr *ph;
1378 int init_offset, initack_offset, i;
1384 /* I know that the TCB is non-NULL from the caller */
1386 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1387 if (asoc->cookie_how[how_indx] == 0)
1390 if (how_indx < sizeof(asoc->cookie_how)) {
1391 asoc->cookie_how[how_indx] = 1;
1393 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1394 /* SHUTDOWN came in after sending INIT-ACK */
1395 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1396 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1397 0, M_DONTWAIT, 1, MT_DATA);
1398 if (op_err == NULL) {
1403 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1404 ph = mtod(op_err, struct sctp_paramhdr *);
1405 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
1406 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1407 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag,
1409 if (how_indx < sizeof(asoc->cookie_how))
1410 asoc->cookie_how[how_indx] = 2;
1414 * find and validate the INIT chunk in the cookie (peer's info) the
1415 * INIT should start after the cookie-echo header struct (chunk
1416 * header, state cookie header struct)
1418 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1420 init_cp = (struct sctp_init_chunk *)
1421 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1422 (uint8_t *) & init_buf);
1423 if (init_cp == NULL) {
1424 /* could not pull a INIT chunk in cookie */
1427 chk_length = ntohs(init_cp->ch.chunk_length);
1428 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1432 * find and validate the INIT-ACK chunk in the cookie (my info) the
1433 * INIT-ACK follows the INIT chunk
1435 initack_offset = init_offset + SCTP_SIZE32(chk_length);
1436 initack_cp = (struct sctp_init_ack_chunk *)
1437 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1438 (uint8_t *) & initack_buf);
1439 if (initack_cp == NULL) {
1440 /* could not pull INIT-ACK chunk in cookie */
1443 chk_length = ntohs(initack_cp->ch.chunk_length);
1444 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1447 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1448 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1450 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1451 * to get into the OPEN state
1453 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1455 * Opps, this means that we somehow generated two vtag's
1456 * the same. I.e. we did:
1458 * <---INIT(tag=a)------
1459 * ----INIT-ACK(tag=t)-->
1460 * ----INIT(tag=t)------> *1
1461 * <---INIT-ACK(tag=a)---
1462 * <----CE(tag=t)------------- *2
1464 * At point *1 we should be generating a different
1465 * tag t'. Which means we would throw away the CE and send
1466 * ours instead. Basically this is case C (throw away side).
1468 if (how_indx < sizeof(asoc->cookie_how))
1469 asoc->cookie_how[how_indx] = 17;
1473 switch SCTP_GET_STATE
1475 case SCTP_STATE_COOKIE_WAIT:
1476 case SCTP_STATE_COOKIE_ECHOED:
1478 * INIT was sent but got a COOKIE_ECHO with the
1479 * correct tags... just accept it...but we must
1480 * process the init so that we can make sure we have
1481 * the right seq no's.
1483 /* First we must process the INIT !! */
1484 retval = sctp_process_init(init_cp, stcb, net);
1486 if (how_indx < sizeof(asoc->cookie_how))
1487 asoc->cookie_how[how_indx] = 3;
1490 /* we have already processed the INIT so no problem */
1491 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1492 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1493 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1494 /* update current state */
1495 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1496 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1498 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1500 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1501 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1502 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1503 stcb->sctp_ep, stcb, asoc->primary_destination);
1505 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1506 sctp_stop_all_cookie_timers(stcb);
1507 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1508 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1509 (inp->sctp_socket->so_qlimit == 0)
1511 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1516 * Here is where collision would go if we
1517 * did a connect() and instead got a
1518 * init/init-ack/cookie done before the
1519 * init-ack came back..
1521 stcb->sctp_ep->sctp_flags |=
1522 SCTP_PCB_FLAGS_CONNECTED;
1523 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1524 so = SCTP_INP_SO(stcb->sctp_ep);
1525 atomic_add_int(&stcb->asoc.refcnt, 1);
1526 SCTP_TCB_UNLOCK(stcb);
1527 SCTP_SOCKET_LOCK(so, 1);
1528 SCTP_TCB_LOCK(stcb);
1529 atomic_add_int(&stcb->asoc.refcnt, -1);
1530 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1531 SCTP_SOCKET_UNLOCK(so, 1);
1535 soisconnected(stcb->sctp_socket);
1536 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1537 SCTP_SOCKET_UNLOCK(so, 1);
1540 /* notify upper layer */
1541 *notification = SCTP_NOTIFY_ASSOC_UP;
1543 * since we did not send a HB make sure we don't
1546 net->hb_responded = 1;
1547 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1548 &cookie->time_entered,
1549 sctp_align_unsafe_makecopy,
1550 SCTP_DETERMINE_LL_NOTOK);
1552 if (stcb->asoc.sctp_autoclose_ticks &&
1553 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1554 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1560 * we're in the OPEN state (or beyond), so peer must
1561 * have simply lost the COOKIE-ACK
1565 sctp_stop_all_cookie_timers(stcb);
1567 * We ignore the return code here.. not sure if we should
1568 * somehow abort.. but we do have an existing asoc. This
1569 * really should not fail.
1571 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1572 init_offset + sizeof(struct sctp_init_chunk),
1573 initack_offset, sh, init_src)) {
1574 if (how_indx < sizeof(asoc->cookie_how))
1575 asoc->cookie_how[how_indx] = 4;
1578 /* respond with a COOKIE-ACK */
1579 sctp_toss_old_cookies(stcb, asoc);
1580 sctp_send_cookie_ack(stcb);
1581 if (how_indx < sizeof(asoc->cookie_how))
1582 asoc->cookie_how[how_indx] = 5;
1585 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1586 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1587 cookie->tie_tag_my_vtag == 0 &&
1588 cookie->tie_tag_peer_vtag == 0) {
1590 * case C in Section 5.2.4 Table 2: XMOO silently discard
1592 if (how_indx < sizeof(asoc->cookie_how))
1593 asoc->cookie_how[how_indx] = 6;
1597 * If nat support, and the below and stcb is established, send back
1598 * a ABORT(colliding state) if we are established.
1600 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1601 (asoc->peer_supports_nat) &&
1602 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1603 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1604 (asoc->peer_vtag == 0)))) {
1606 * Special case - Peer's support nat. We may have two init's
1607 * that we gave out the same tag on since one was not
1608 * established.. i.e. we get INIT from host-1 behind the nat
1609 * and we respond tag-a, we get a INIT from host-2 behind
1610 * the nat and we get tag-a again. Then we bring up host-1
1611 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1612 * Now we have colliding state. We must send an abort here
1613 * with colliding state indication.
1615 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1616 0, M_DONTWAIT, 1, MT_DATA);
1617 if (op_err == NULL) {
1621 /* pre-reserve some space */
1623 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
1625 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
1627 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
1628 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1630 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1631 ph = mtod(op_err, struct sctp_paramhdr *);
1632 ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE);
1633 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1634 sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port);
1637 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1638 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1639 (asoc->peer_vtag == 0))) {
1641 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1642 * should be ok, re-accept peer info
1644 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1646 * Extension of case C. If we hit this, then the
1647 * random number generator returned the same vtag
1648 * when we first sent our INIT-ACK and when we later
1649 * sent our INIT. The side with the seq numbers that
1650 * are different will be the one that normnally
1651 * would have hit case C. This in effect "extends"
1652 * our vtags in this collision case to be 64 bits.
1653 * The same collision could occur aka you get both
1654 * vtag and seq number the same twice in a row.. but
1655 * is much less likely. If it did happen then we
1656 * would proceed through and bring up the assoc.. we
1657 * may end up with the wrong stream setup however..
1658 * which would be bad.. but there is no way to
1659 * tell.. until we send on a stream that does not
1662 if (how_indx < sizeof(asoc->cookie_how))
1663 asoc->cookie_how[how_indx] = 7;
1667 if (how_indx < sizeof(asoc->cookie_how))
1668 asoc->cookie_how[how_indx] = 8;
1669 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1670 sctp_stop_all_cookie_timers(stcb);
1672 * since we did not send a HB make sure we don't double
1675 net->hb_responded = 1;
1676 if (stcb->asoc.sctp_autoclose_ticks &&
1677 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1678 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1681 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1682 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1684 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1686 * Ok the peer probably discarded our data (if we
1687 * echoed a cookie+data). So anything on the
1688 * sent_queue should be marked for retransmit, we
1689 * may not get something to kick us so it COULD
1690 * still take a timeout to move these.. but it can't
1691 * hurt to mark them.
1693 struct sctp_tmit_chunk *chk;
1695 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1696 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1697 chk->sent = SCTP_DATAGRAM_RESEND;
1698 sctp_flight_size_decrease(chk);
1699 sctp_total_flight_decrease(stcb, chk);
1700 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1706 /* process the INIT info (peer's info) */
1707 retval = sctp_process_init(init_cp, stcb, net);
1709 if (how_indx < sizeof(asoc->cookie_how))
1710 asoc->cookie_how[how_indx] = 9;
1713 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1714 init_offset + sizeof(struct sctp_init_chunk),
1715 initack_offset, sh, init_src)) {
1716 if (how_indx < sizeof(asoc->cookie_how))
1717 asoc->cookie_how[how_indx] = 10;
1720 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1721 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1722 *notification = SCTP_NOTIFY_ASSOC_UP;
1724 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1725 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1726 (inp->sctp_socket->so_qlimit == 0)) {
1727 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1731 stcb->sctp_ep->sctp_flags |=
1732 SCTP_PCB_FLAGS_CONNECTED;
1733 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1734 so = SCTP_INP_SO(stcb->sctp_ep);
1735 atomic_add_int(&stcb->asoc.refcnt, 1);
1736 SCTP_TCB_UNLOCK(stcb);
1737 SCTP_SOCKET_LOCK(so, 1);
1738 SCTP_TCB_LOCK(stcb);
1739 atomic_add_int(&stcb->asoc.refcnt, -1);
1740 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1741 SCTP_SOCKET_UNLOCK(so, 1);
1745 soisconnected(stcb->sctp_socket);
1746 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1747 SCTP_SOCKET_UNLOCK(so, 1);
1750 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1751 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1753 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1754 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1755 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1756 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1758 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1760 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1761 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1762 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1763 stcb->sctp_ep, stcb, asoc->primary_destination);
1765 sctp_stop_all_cookie_timers(stcb);
1766 sctp_toss_old_cookies(stcb, asoc);
1767 sctp_send_cookie_ack(stcb);
1770 * only if we have retrans set do we do this. What
1771 * this call does is get only the COOKIE-ACK out and
1772 * then when we return the normal call to
1773 * sctp_chunk_output will get the retrans out behind
1776 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1778 if (how_indx < sizeof(asoc->cookie_how))
1779 asoc->cookie_how[how_indx] = 11;
1783 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1784 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1785 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1786 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1787 cookie->tie_tag_peer_vtag != 0) {
1788 struct sctpasochead *head;
1790 if (asoc->peer_supports_nat) {
1792 * This is a gross gross hack. just call the
1793 * cookie_new code since we are allowing a duplicate
1794 * association. I hope this works...
1796 return (sctp_process_cookie_new(m, iphlen, offset, sh, cookie, cookie_len,
1797 inp, netp, init_src, notification,
1798 auth_skipped, auth_offset, auth_len,
1802 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1805 if (how_indx < sizeof(asoc->cookie_how))
1806 asoc->cookie_how[how_indx] = 12;
1807 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1808 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1810 *sac_assoc_id = sctp_get_associd(stcb);
1811 /* notify upper layer */
1812 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1813 atomic_add_int(&stcb->asoc.refcnt, 1);
1814 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1815 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1816 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1817 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1819 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1820 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1821 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1822 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1824 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1825 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1826 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1827 stcb->sctp_ep, stcb, asoc->primary_destination);
1829 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1830 /* move to OPEN state, if not in SHUTDOWN_SENT */
1831 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1833 asoc->pre_open_streams =
1834 ntohs(initack_cp->init.num_outbound_streams);
1835 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1836 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1837 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1839 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1841 asoc->str_reset_seq_in = asoc->init_seq_number;
1843 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1844 if (asoc->mapping_array) {
1845 memset(asoc->mapping_array, 0,
1846 asoc->mapping_array_size);
1848 if (asoc->nr_mapping_array) {
1849 memset(asoc->nr_mapping_array, 0,
1850 asoc->mapping_array_size);
1852 SCTP_TCB_UNLOCK(stcb);
1853 SCTP_INP_INFO_WLOCK();
1854 SCTP_INP_WLOCK(stcb->sctp_ep);
1855 SCTP_TCB_LOCK(stcb);
1856 atomic_add_int(&stcb->asoc.refcnt, -1);
1857 /* send up all the data */
1858 SCTP_TCB_SEND_LOCK(stcb);
1860 sctp_report_all_outbound(stcb, 1, SCTP_SO_NOT_LOCKED);
1861 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1862 stcb->asoc.strmout[i].stream_no = i;
1863 stcb->asoc.strmout[i].next_sequence_sent = 0;
1864 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1866 /* process the INIT-ACK info (my info) */
1867 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1868 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1870 /* pull from vtag hash */
1871 LIST_REMOVE(stcb, sctp_asocs);
1872 /* re-insert to new vtag position */
1873 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1874 SCTP_BASE_INFO(hashasocmark))];
1876 * put it in the bucket in the vtag hash of assoc's for the
1879 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1881 /* process the INIT info (peer's info) */
1882 SCTP_TCB_SEND_UNLOCK(stcb);
1883 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1884 SCTP_INP_INFO_WUNLOCK();
1886 retval = sctp_process_init(init_cp, stcb, net);
1888 if (how_indx < sizeof(asoc->cookie_how))
1889 asoc->cookie_how[how_indx] = 13;
1894 * since we did not send a HB make sure we don't double
1897 net->hb_responded = 1;
1899 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1900 init_offset + sizeof(struct sctp_init_chunk),
1901 initack_offset, sh, init_src)) {
1902 if (how_indx < sizeof(asoc->cookie_how))
1903 asoc->cookie_how[how_indx] = 14;
1907 /* respond with a COOKIE-ACK */
1908 sctp_stop_all_cookie_timers(stcb);
1909 sctp_toss_old_cookies(stcb, asoc);
1910 sctp_send_cookie_ack(stcb);
1911 if (how_indx < sizeof(asoc->cookie_how))
1912 asoc->cookie_how[how_indx] = 15;
1916 if (how_indx < sizeof(asoc->cookie_how))
1917 asoc->cookie_how[how_indx] = 16;
1918 /* all other cases... */
1924 * handle a state cookie for a new association m: input packet mbuf chain--
1925 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
1926 * and the cookie signature does not exist offset: offset into mbuf to the
1927 * cookie-echo chunk length: length of the cookie chunk to: where the init
1928 * was from returns a new TCB
1931 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1932 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1933 struct sctp_inpcb *inp, struct sctp_nets **netp,
1934 struct sockaddr *init_src, int *notification,
1935 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1936 uint32_t vrf_id, uint16_t port)
1938 struct sctp_tcb *stcb;
1939 struct sctp_init_chunk *init_cp, init_buf;
1940 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1941 struct sockaddr_storage sa_store;
1942 struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
1943 struct sockaddr_in *sin;
1944 struct sockaddr_in6 *sin6;
1945 struct sctp_association *asoc;
1947 int init_offset, initack_offset, initack_limit;
1951 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
1953 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1956 so = SCTP_INP_SO(inp);
1960 * find and validate the INIT chunk in the cookie (peer's info) the
1961 * INIT should start after the cookie-echo header struct (chunk
1962 * header, state cookie header struct)
1964 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
1965 init_cp = (struct sctp_init_chunk *)
1966 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1967 (uint8_t *) & init_buf);
1968 if (init_cp == NULL) {
1969 /* could not pull a INIT chunk in cookie */
1970 SCTPDBG(SCTP_DEBUG_INPUT1,
1971 "process_cookie_new: could not pull INIT chunk hdr\n");
1974 chk_length = ntohs(init_cp->ch.chunk_length);
1975 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1976 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
1979 initack_offset = init_offset + SCTP_SIZE32(chk_length);
1981 * find and validate the INIT-ACK chunk in the cookie (my info) the
1982 * INIT-ACK follows the INIT chunk
1984 initack_cp = (struct sctp_init_ack_chunk *)
1985 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1986 (uint8_t *) & initack_buf);
1987 if (initack_cp == NULL) {
1988 /* could not pull INIT-ACK chunk in cookie */
1989 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
1992 chk_length = ntohs(initack_cp->ch.chunk_length);
1993 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1997 * NOTE: We can't use the INIT_ACK's chk_length to determine the
1998 * "initack_limit" value. This is because the chk_length field
1999 * includes the length of the cookie, but the cookie is omitted when
2000 * the INIT and INIT_ACK are tacked onto the cookie...
2002 initack_limit = offset + cookie_len;
2005 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2010 * Here we do a trick, we set in NULL for the proc/thread argument.
2011 * We do this since in effect we only use the p argument when the
2012 * socket is unbound and we must do an implicit bind. Since we are
2013 * getting a cookie, we cannot be unbound.
2015 stcb = sctp_aloc_assoc(inp, init_src, &error,
2016 ntohl(initack_cp->init.initiate_tag), vrf_id,
2017 (struct thread *)NULL
2020 struct mbuf *op_err;
2022 /* memory problem? */
2023 SCTPDBG(SCTP_DEBUG_INPUT1,
2024 "process_cookie_new: no room for another TCB!\n");
2025 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2027 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2028 sh, op_err, vrf_id, port);
2031 /* get the correct sctp_nets */
2033 *netp = sctp_findnet(stcb, init_src);
2036 /* get scope variables out of cookie */
2037 asoc->ipv4_local_scope = cookie->ipv4_scope;
2038 asoc->site_scope = cookie->site_scope;
2039 asoc->local_scope = cookie->local_scope;
2040 asoc->loopback_scope = cookie->loopback_scope;
2042 if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2043 (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2044 struct mbuf *op_err;
2047 * Houston we have a problem. The EP changed while the
2048 * cookie was in flight. Only recourse is to abort the
2051 atomic_add_int(&stcb->asoc.refcnt, 1);
2052 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2053 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2054 sh, op_err, vrf_id, port);
2055 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2056 SCTP_TCB_UNLOCK(stcb);
2057 SCTP_SOCKET_LOCK(so, 1);
2058 SCTP_TCB_LOCK(stcb);
2060 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2061 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2062 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2063 SCTP_SOCKET_UNLOCK(so, 1);
2065 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2068 /* process the INIT-ACK info (my info) */
2069 old_tag = asoc->my_vtag;
2070 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2071 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2072 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2073 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2074 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2075 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2076 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2077 asoc->str_reset_seq_in = asoc->init_seq_number;
2079 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2081 /* process the INIT info (peer's info) */
2083 retval = sctp_process_init(init_cp, stcb, *netp);
2087 atomic_add_int(&stcb->asoc.refcnt, 1);
2088 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2089 SCTP_TCB_UNLOCK(stcb);
2090 SCTP_SOCKET_LOCK(so, 1);
2091 SCTP_TCB_LOCK(stcb);
2093 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2094 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2095 SCTP_SOCKET_UNLOCK(so, 1);
2097 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2100 /* load all addresses */
2101 if (sctp_load_addresses_from_init(stcb, m, iphlen,
2102 init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh,
2104 atomic_add_int(&stcb->asoc.refcnt, 1);
2105 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2106 SCTP_TCB_UNLOCK(stcb);
2107 SCTP_SOCKET_LOCK(so, 1);
2108 SCTP_TCB_LOCK(stcb);
2110 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
2111 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2112 SCTP_SOCKET_UNLOCK(so, 1);
2114 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2118 * verify any preceding AUTH chunk that was skipped
2120 /* pull the local authentication parameters from the cookie/init-ack */
2121 sctp_auth_get_cookie_params(stcb, m,
2122 initack_offset + sizeof(struct sctp_init_ack_chunk),
2123 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2125 struct sctp_auth_chunk *auth;
2127 auth = (struct sctp_auth_chunk *)
2128 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2129 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2130 /* auth HMAC failed, dump the assoc and packet */
2131 SCTPDBG(SCTP_DEBUG_AUTH1,
2132 "COOKIE-ECHO: AUTH failed\n");
2133 atomic_add_int(&stcb->asoc.refcnt, 1);
2134 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2135 SCTP_TCB_UNLOCK(stcb);
2136 SCTP_SOCKET_LOCK(so, 1);
2137 SCTP_TCB_LOCK(stcb);
2139 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2140 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2141 SCTP_SOCKET_UNLOCK(so, 1);
2143 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2146 /* remaining chunks checked... good to go */
2147 stcb->asoc.authenticated = 1;
2150 /* update current state */
2151 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2152 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2153 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2154 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2155 stcb->sctp_ep, stcb, asoc->primary_destination);
2157 sctp_stop_all_cookie_timers(stcb);
2158 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2159 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2162 * if we're doing ASCONFs, check to see if we have any new local
2163 * addresses that need to get added to the peer (eg. addresses
2164 * changed while cookie echo in flight). This needs to be done
2165 * after we go to the OPEN state to do the correct asconf
2166 * processing. else, make sure we have the correct addresses in our
2170 /* warning, we re-use sin, sin6, sa_store here! */
2171 /* pull in local_address (our "from" address) */
2172 if (cookie->laddr_type == SCTP_IPV4_ADDRESS) {
2173 /* source addr is IPv4 */
2174 sin = (struct sockaddr_in *)initack_src;
2175 memset(sin, 0, sizeof(*sin));
2176 sin->sin_family = AF_INET;
2177 sin->sin_len = sizeof(struct sockaddr_in);
2178 sin->sin_addr.s_addr = cookie->laddress[0];
2179 } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) {
2180 /* source addr is IPv6 */
2181 sin6 = (struct sockaddr_in6 *)initack_src;
2182 memset(sin6, 0, sizeof(*sin6));
2183 sin6->sin6_family = AF_INET6;
2184 sin6->sin6_len = sizeof(struct sockaddr_in6);
2185 sin6->sin6_scope_id = cookie->scope_id;
2186 memcpy(&sin6->sin6_addr, cookie->laddress,
2187 sizeof(sin6->sin6_addr));
2189 atomic_add_int(&stcb->asoc.refcnt, 1);
2190 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2191 SCTP_TCB_UNLOCK(stcb);
2192 SCTP_SOCKET_LOCK(so, 1);
2193 SCTP_TCB_LOCK(stcb);
2195 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2196 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2197 SCTP_SOCKET_UNLOCK(so, 1);
2199 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2203 /* set up to notify upper layer */
2204 *notification = SCTP_NOTIFY_ASSOC_UP;
2205 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2206 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2207 (inp->sctp_socket->so_qlimit == 0)) {
2209 * This is an endpoint that called connect() how it got a
2210 * cookie that is NEW is a bit of a mystery. It must be that
2211 * the INIT was sent, but before it got there.. a complete
2212 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2213 * should have went to the other code.. not here.. oh well..
2214 * a bit of protection is worth having..
2216 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2217 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2218 atomic_add_int(&stcb->asoc.refcnt, 1);
2219 SCTP_TCB_UNLOCK(stcb);
2220 SCTP_SOCKET_LOCK(so, 1);
2221 SCTP_TCB_LOCK(stcb);
2222 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2223 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2224 SCTP_SOCKET_UNLOCK(so, 1);
2228 soisconnected(stcb->sctp_socket);
2229 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2230 SCTP_SOCKET_UNLOCK(so, 1);
2232 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2233 (inp->sctp_socket->so_qlimit)) {
2235 * We don't want to do anything with this one. Since it is
2236 * the listening guy. The timer will get started for
2237 * accepted connections in the caller.
2241 /* since we did not send a HB make sure we don't double things */
2242 if ((netp) && (*netp))
2243 (*netp)->hb_responded = 1;
2245 if (stcb->asoc.sctp_autoclose_ticks &&
2246 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2247 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2249 /* calculate the RTT */
2250 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2251 if ((netp) && (*netp)) {
2252 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2253 &cookie->time_entered, sctp_align_unsafe_makecopy,
2254 SCTP_DETERMINE_LL_NOTOK);
2256 /* respond with a COOKIE-ACK */
2257 sctp_send_cookie_ack(stcb);
2260 * check the address lists for any ASCONFs that need to be sent
2261 * AFTER the cookie-ack is sent
2263 sctp_check_address_list(stcb, m,
2264 initack_offset + sizeof(struct sctp_init_ack_chunk),
2265 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2266 initack_src, cookie->local_scope, cookie->site_scope,
2267 cookie->ipv4_scope, cookie->loopback_scope);
2274 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2275 * we NEED to make sure we are not already using the vtag. If so we
2276 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2277 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2278 SCTP_BASE_INFO(hashasocmark))];
2279 LIST_FOREACH(stcb, head, sctp_asocs) {
2280 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2281 -- SEND ABORT - TRY AGAIN --
2287 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2288 * existing (non-NULL) TCB
2290 static struct mbuf *
2291 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2292 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2293 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2294 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2295 struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint16_t port)
2297 struct sctp_state_cookie *cookie;
2298 struct sockaddr_in6 sin6;
2299 struct sockaddr_in sin;
2300 struct sctp_tcb *l_stcb = *stcb;
2301 struct sctp_inpcb *l_inp;
2302 struct sockaddr *to;
2303 sctp_assoc_t sac_restart_id;
2304 struct sctp_pcb *ep;
2306 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2308 uint8_t cookie_ok = 0;
2309 unsigned int size_of_pkt, sig_offset, cookie_offset;
2310 unsigned int cookie_len;
2312 struct timeval time_expires;
2313 struct sockaddr_storage dest_store;
2314 struct sockaddr *localep_sa = (struct sockaddr *)&dest_store;
2316 int notification = 0;
2317 struct sctp_nets *netl;
2318 int had_a_existing_tcb = 0;
2319 int send_int_conf = 0;
2321 SCTPDBG(SCTP_DEBUG_INPUT2,
2322 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2324 if (inp_p == NULL) {
2327 /* First get the destination address setup too. */
2328 iph = mtod(m, struct ip *);
2329 switch (iph->ip_v) {
2333 struct sockaddr_in *lsin;
2335 lsin = (struct sockaddr_in *)(localep_sa);
2336 memset(lsin, 0, sizeof(*lsin));
2337 lsin->sin_family = AF_INET;
2338 lsin->sin_len = sizeof(*lsin);
2339 lsin->sin_port = sh->dest_port;
2340 lsin->sin_addr.s_addr = iph->ip_dst.s_addr;
2341 size_of_pkt = SCTP_GET_IPV4_LENGTH(iph);
2345 case IPV6_VERSION >> 4:
2348 struct ip6_hdr *ip6;
2349 struct sockaddr_in6 *lsin6;
2351 lsin6 = (struct sockaddr_in6 *)(localep_sa);
2352 memset(lsin6, 0, sizeof(*lsin6));
2353 lsin6->sin6_family = AF_INET6;
2354 lsin6->sin6_len = sizeof(struct sockaddr_in6);
2355 ip6 = mtod(m, struct ip6_hdr *);
2356 lsin6->sin6_port = sh->dest_port;
2357 lsin6->sin6_addr = ip6->ip6_dst;
2358 size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen;
2366 cookie = &cp->cookie;
2367 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2368 cookie_len = ntohs(cp->ch.chunk_length);
2370 if ((cookie->peerport != sh->src_port) &&
2371 (cookie->myport != sh->dest_port) &&
2372 (cookie->my_vtag != sh->v_tag)) {
2374 * invalid ports or bad tag. Note that we always leave the
2375 * v_tag in the header in network order and when we stored
2376 * it in the my_vtag slot we also left it in network order.
2377 * This maintains the match even though it may be in the
2378 * opposite byte order of the machine :->
2382 if (cookie_len > size_of_pkt ||
2383 cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2384 sizeof(struct sctp_init_chunk) +
2385 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2386 /* cookie too long! or too small */
2390 * split off the signature into its own mbuf (since it should not be
2391 * calculated in the sctp_hmac_m() call).
2393 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2394 if (sig_offset > size_of_pkt) {
2395 /* packet not correct size! */
2396 /* XXX this may already be accounted for earlier... */
2399 m_sig = m_split(m, sig_offset, M_DONTWAIT);
2400 if (m_sig == NULL) {
2401 /* out of memory or ?? */
2404 #ifdef SCTP_MBUF_LOGGING
2405 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2410 if (SCTP_BUF_IS_EXTENDED(mat)) {
2411 sctp_log_mb(mat, SCTP_MBUF_SPLIT);
2413 mat = SCTP_BUF_NEXT(mat);
2419 * compute the signature/digest for the cookie
2421 ep = &(*inp_p)->sctp_ep;
2424 SCTP_TCB_UNLOCK(l_stcb);
2426 SCTP_INP_RLOCK(l_inp);
2428 SCTP_TCB_LOCK(l_stcb);
2430 /* which cookie is it? */
2431 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2432 (ep->current_secret_number != ep->last_secret_number)) {
2433 /* it's the old cookie */
2434 (void)sctp_hmac_m(SCTP_HMAC,
2435 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2436 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2438 /* it's the current cookie */
2439 (void)sctp_hmac_m(SCTP_HMAC,
2440 (uint8_t *) ep->secret_key[(int)ep->current_secret_number],
2441 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2443 /* get the signature */
2444 SCTP_INP_RUNLOCK(l_inp);
2445 sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig);
2447 /* couldn't find signature */
2448 sctp_m_freem(m_sig);
2451 /* compare the received digest with the computed digest */
2452 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2453 /* try the old cookie? */
2454 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2455 (ep->current_secret_number != ep->last_secret_number)) {
2456 /* compute digest with old */
2457 (void)sctp_hmac_m(SCTP_HMAC,
2458 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2459 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2461 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2469 * Now before we continue we must reconstruct our mbuf so that
2470 * normal processing of any other chunks will work.
2476 while (SCTP_BUF_NEXT(m_at) != NULL) {
2477 m_at = SCTP_BUF_NEXT(m_at);
2479 SCTP_BUF_NEXT(m_at) = m_sig;
2482 if (cookie_ok == 0) {
2483 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2484 SCTPDBG(SCTP_DEBUG_INPUT2,
2485 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2486 (uint32_t) offset, cookie_offset, sig_offset);
2490 * check the cookie timestamps to be sure it's not stale
2492 (void)SCTP_GETTIME_TIMEVAL(&now);
2493 /* Expire time is in Ticks, so we convert to seconds */
2494 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2495 time_expires.tv_usec = cookie->time_entered.tv_usec;
2497 * TODO sctp_constants.h needs alternative time macros when _KERNEL
2500 if (timevalcmp(&now, &time_expires, >)) {
2501 /* cookie is stale! */
2502 struct mbuf *op_err;
2503 struct sctp_stale_cookie_msg *scm;
2506 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg),
2507 0, M_DONTWAIT, 1, MT_DATA);
2508 if (op_err == NULL) {
2513 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg);
2514 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
2515 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
2516 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
2517 (sizeof(uint32_t))));
2518 /* seconds to usec */
2519 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2522 tim = now.tv_usec - cookie->time_entered.tv_usec;
2523 scm->time_usec = htonl(tim);
2524 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag,
2529 * Now we must see with the lookup address if we have an existing
2530 * asoc. This will only happen if we were in the COOKIE-WAIT state
2531 * and a INIT collided with us and somewhere the peer sent the
2532 * cookie on another address besides the single address our assoc
2533 * had for him. In this case we will have one of the tie-tags set at
2534 * least AND the address field in the cookie can be used to look it
2538 if (cookie->addr_type == SCTP_IPV6_ADDRESS) {
2539 memset(&sin6, 0, sizeof(sin6));
2540 sin6.sin6_family = AF_INET6;
2541 sin6.sin6_len = sizeof(sin6);
2542 sin6.sin6_port = sh->src_port;
2543 sin6.sin6_scope_id = cookie->scope_id;
2544 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2545 sizeof(sin6.sin6_addr.s6_addr));
2546 to = (struct sockaddr *)&sin6;
2547 } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) {
2548 memset(&sin, 0, sizeof(sin));
2549 sin.sin_family = AF_INET;
2550 sin.sin_len = sizeof(sin);
2551 sin.sin_port = sh->src_port;
2552 sin.sin_addr.s_addr = cookie->address[0];
2553 to = (struct sockaddr *)&sin;
2555 /* This should not happen */
2558 if ((*stcb == NULL) && to) {
2559 /* Yep, lets check */
2560 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL);
2561 if (*stcb == NULL) {
2563 * We should have only got back the same inp. If we
2564 * got back a different ep we have a problem. The
2565 * original findep got back l_inp and now
2567 if (l_inp != *inp_p) {
2568 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2571 if (*locked_tcb == NULL) {
2573 * In this case we found the assoc only
2574 * after we locked the create lock. This
2575 * means we are in a colliding case and we
2576 * must make sure that we unlock the tcb if
2577 * its one of the cases where we throw away
2578 * the incoming packets.
2580 *locked_tcb = *stcb;
2583 * We must also increment the inp ref count
2584 * since the ref_count flags was set when we
2585 * did not find the TCB, now we found it
2586 * which reduces the refcount.. we must
2587 * raise it back out to balance it all :-)
2589 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2590 if ((*stcb)->sctp_ep != l_inp) {
2591 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2592 (*stcb)->sctp_ep, l_inp);
2600 cookie_len -= SCTP_SIGNATURE_SIZE;
2601 if (*stcb == NULL) {
2602 /* this is the "normal" case... get a new TCB */
2603 *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie,
2604 cookie_len, *inp_p, netp, to, ¬ification,
2605 auth_skipped, auth_offset, auth_len, vrf_id, port);
2607 /* this is abnormal... cookie-echo on existing TCB */
2608 had_a_existing_tcb = 1;
2609 *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh,
2610 cookie, cookie_len, *inp_p, *stcb, netp, to,
2611 ¬ification, &sac_restart_id, vrf_id, auth_skipped, auth_offset, auth_len, port);
2614 if (*stcb == NULL) {
2615 /* still no TCB... must be bad cookie-echo */
2618 if ((*netp != NULL) && (m->m_flags & M_FLOWID)) {
2619 (*netp)->flowid = m->m_pkthdr.flowid;
2621 (*netp)->flowidset = 1;
2625 * Ok, we built an association so confirm the address we sent the
2628 netl = sctp_findnet(*stcb, to);
2630 * This code should in theory NOT run but
2633 /* TSNH! Huh, why do I need to add this address here? */
2636 ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE,
2637 SCTP_IN_COOKIE_PROC);
2638 netl = sctp_findnet(*stcb, to);
2641 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2642 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2643 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2649 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p,
2652 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2653 if (!had_a_existing_tcb ||
2654 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2656 * If we have a NEW cookie or the connect never
2657 * reached the connected state during collision we
2658 * must do the TCP accept thing.
2660 struct socket *so, *oso;
2661 struct sctp_inpcb *inp;
2663 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2665 * For a restart we will keep the same
2666 * socket, no need to do anything. I THINK!!
2668 sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id, SCTP_SO_NOT_LOCKED);
2669 if (send_int_conf) {
2670 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2671 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2675 oso = (*inp_p)->sctp_socket;
2676 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2677 SCTP_TCB_UNLOCK((*stcb));
2678 CURVNET_SET(oso->so_vnet);
2679 so = sonewconn(oso, 0
2682 SCTP_TCB_LOCK((*stcb));
2683 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2686 struct mbuf *op_err;
2688 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2689 struct socket *pcb_so;
2692 /* Too many sockets */
2693 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2694 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2695 sctp_abort_association(*inp_p, NULL, m, iphlen,
2696 sh, op_err, vrf_id, port);
2697 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2698 pcb_so = SCTP_INP_SO(*inp_p);
2699 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2700 SCTP_TCB_UNLOCK((*stcb));
2701 SCTP_SOCKET_LOCK(pcb_so, 1);
2702 SCTP_TCB_LOCK((*stcb));
2703 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2705 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2706 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2707 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2711 inp = (struct sctp_inpcb *)so->so_pcb;
2712 SCTP_INP_INCR_REF(inp);
2714 * We add the unbound flag here so that if we get an
2715 * soabort() before we get the move_pcb done, we
2716 * will properly cleanup.
2718 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2719 SCTP_PCB_FLAGS_CONNECTED |
2720 SCTP_PCB_FLAGS_IN_TCPPOOL |
2721 SCTP_PCB_FLAGS_UNBOUND |
2722 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2723 SCTP_PCB_FLAGS_DONT_WAKE);
2724 inp->sctp_features = (*inp_p)->sctp_features;
2725 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2726 inp->sctp_socket = so;
2727 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2728 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2729 inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable;
2730 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2731 inp->sctp_context = (*inp_p)->sctp_context;
2732 inp->inp_starting_point_for_iterator = NULL;
2734 * copy in the authentication parameters from the
2737 if (inp->sctp_ep.local_hmacs)
2738 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2739 inp->sctp_ep.local_hmacs =
2740 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2741 if (inp->sctp_ep.local_auth_chunks)
2742 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2743 inp->sctp_ep.local_auth_chunks =
2744 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2747 * Now we must move it from one hash table to
2748 * another and get the tcb in the right place.
2752 * This is where the one-2-one socket is put into
2753 * the accept state waiting for the accept!
2756 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2758 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2760 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2761 SCTP_TCB_UNLOCK((*stcb));
2763 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2765 SCTP_TCB_LOCK((*stcb));
2766 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2770 * now we must check to see if we were aborted while
2771 * the move was going on and the lock/unlock
2774 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2776 * yep it was, we leave the assoc attached
2777 * to the socket since the sctp_inpcb_free()
2778 * call will send an abort for us.
2780 SCTP_INP_DECR_REF(inp);
2783 SCTP_INP_DECR_REF(inp);
2784 /* Switch over to the new guy */
2786 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2787 if (send_int_conf) {
2788 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2789 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2792 * Pull it from the incomplete queue and wake the
2795 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2796 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2797 SCTP_TCB_UNLOCK((*stcb));
2798 SCTP_SOCKET_LOCK(so, 1);
2801 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2802 SCTP_TCB_LOCK((*stcb));
2803 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2804 SCTP_SOCKET_UNLOCK(so, 1);
2809 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
2811 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2813 if (send_int_conf) {
2814 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2815 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2822 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp,
2823 struct sctp_tcb *stcb, struct sctp_nets *net)
2825 /* cp must not be used, others call this without a c-ack :-) */
2826 struct sctp_association *asoc;
2828 SCTPDBG(SCTP_DEBUG_INPUT2,
2829 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2835 sctp_stop_all_cookie_timers(stcb);
2836 /* process according to association state */
2837 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2838 /* state change only needed when I am in right state */
2839 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2840 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2841 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2842 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2843 stcb->sctp_ep, stcb, asoc->primary_destination);
2847 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2848 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2849 if (asoc->overall_error_count == 0) {
2850 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2851 &asoc->time_entered, sctp_align_safe_nocopy,
2852 SCTP_DETERMINE_LL_NOTOK);
2854 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2855 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2856 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2857 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2858 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2862 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2863 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2864 so = SCTP_INP_SO(stcb->sctp_ep);
2865 atomic_add_int(&stcb->asoc.refcnt, 1);
2866 SCTP_TCB_UNLOCK(stcb);
2867 SCTP_SOCKET_LOCK(so, 1);
2868 SCTP_TCB_LOCK(stcb);
2869 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2871 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2872 soisconnected(stcb->sctp_socket);
2874 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2875 SCTP_SOCKET_UNLOCK(so, 1);
2879 * since we did not send a HB make sure we don't double
2882 net->hb_responded = 1;
2884 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2886 * We don't need to do the asconf thing, nor hb or
2887 * autoclose if the socket is closed.
2891 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2895 if (stcb->asoc.sctp_autoclose_ticks &&
2896 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2897 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2898 stcb->sctp_ep, stcb, NULL);
2901 * send ASCONF if parameters are pending and ASCONFs are
2902 * allowed (eg. addresses changed when init/cookie echo were
2905 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2906 (stcb->asoc.peer_supports_asconf) &&
2907 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2908 #ifdef SCTP_TIMER_BASED_ASCONF
2909 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2910 stcb->sctp_ep, stcb,
2911 stcb->asoc.primary_destination);
2913 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2914 SCTP_ADDR_NOT_LOCKED);
2919 /* Toss the cookie if I can */
2920 sctp_toss_old_cookies(stcb, asoc);
2921 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
2922 /* Restart the timer if we have pending data */
2923 struct sctp_tmit_chunk *chk;
2925 chk = TAILQ_FIRST(&asoc->sent_queue);
2926 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
2931 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2932 struct sctp_tcb *stcb)
2934 struct sctp_nets *net;
2935 struct sctp_tmit_chunk *lchk;
2936 struct sctp_ecne_chunk bkup;
2937 uint8_t override_bit = 0;
2938 uint32_t tsn, window_data_tsn;
2940 unsigned int pkt_cnt;
2942 len = ntohs(cp->ch.chunk_length);
2943 if ((len != sizeof(struct sctp_ecne_chunk)) &&
2944 (len != sizeof(struct old_sctp_ecne_chunk))) {
2947 if (len == sizeof(struct old_sctp_ecne_chunk)) {
2948 /* Its the old format */
2949 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
2950 bkup.num_pkts_since_cwr = htonl(1);
2953 SCTP_STAT_INCR(sctps_recvecne);
2954 tsn = ntohl(cp->tsn);
2955 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
2956 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
2958 window_data_tsn = stcb->asoc.sending_seq - 1;
2960 window_data_tsn = lchk->rec.data.TSN_seq;
2963 /* Find where it was sent to if possible. */
2965 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
2966 if (lchk->rec.data.TSN_seq == tsn) {
2968 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
2971 if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) {
2977 * What to do. A previous send of a CWR was possibly lost.
2978 * See how old it is, we may have it marked on the actual
2981 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2982 if (tsn == net->last_cwr_tsn) {
2983 /* Found him, send it off */
2988 * If we reach here, we need to send a special CWR that says
2989 * hey, we did this a long time ago and you lost the
2992 net = TAILQ_FIRST(&stcb->asoc.nets);
2993 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
2996 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
2997 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
2999 * JRS - Use the congestion control given in the pluggable
3005 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3007 * We reduce once every RTT. So we will only lower cwnd at
3008 * the next sending seq i.e. the window_data_tsn
3010 net->cwr_window_tsn = window_data_tsn;
3011 net->ecn_ce_pkt_cnt += pkt_cnt;
3012 net->lost_cnt = pkt_cnt;
3013 net->last_cwr_tsn = tsn;
3015 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3016 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3017 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3019 * Another loss in the same window update how many
3020 * marks/packets lost we have had.
3024 if (pkt_cnt > net->lost_cnt) {
3025 /* Should be the case */
3026 cnt = (pkt_cnt - net->lost_cnt);
3027 net->ecn_ce_pkt_cnt += cnt;
3029 net->lost_cnt = pkt_cnt;
3030 net->last_cwr_tsn = tsn;
3032 * Most CC functions will ignore this call, since we
3033 * are in-window yet of the initial CE the peer saw.
3035 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3039 * We always send a CWR this way if our previous one was lost our
3040 * peer will get an update, or if it is not time again to reduce we
3041 * still get the cwr to the peer. Note we set the override when we
3042 * could not find the TSN on the chunk or the destination network.
3044 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3048 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3051 * Here we get a CWR from the peer. We must look in the outqueue and
3052 * make sure that we have a covered ECNE in teh control chunk part.
3055 struct sctp_tmit_chunk *chk;
3056 struct sctp_ecne_chunk *ecne;
3060 cwr_tsn = ntohl(cp->tsn);
3062 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3063 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
3064 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3067 if ((override == 0) && (chk->whoTo != net)) {
3068 /* Must be from the right src unless override is set */
3071 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3072 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3073 /* this covers this ECNE, we can remove it */
3074 stcb->asoc.ecn_echo_cnt_onq--;
3075 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3078 sctp_m_freem(chk->data);
3081 stcb->asoc.ctrl_queue_cnt--;
3082 sctp_free_a_chunk(stcb, chk);
3083 if (override == 0) {
3091 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp,
3092 struct sctp_tcb *stcb, struct sctp_nets *net)
3094 struct sctp_association *asoc;
3096 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3101 SCTPDBG(SCTP_DEBUG_INPUT2,
3102 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3107 /* process according to association state */
3108 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3109 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3110 SCTPDBG(SCTP_DEBUG_INPUT2,
3111 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3112 SCTP_TCB_UNLOCK(stcb);
3115 /* notify upper layer protocol */
3116 if (stcb->sctp_socket) {
3117 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3118 /* are the queues empty? they should be */
3119 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3120 !TAILQ_EMPTY(&asoc->sent_queue) ||
3121 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
3122 sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED);
3125 /* stop the timer */
3126 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
3127 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3129 SCTPDBG(SCTP_DEBUG_INPUT2,
3130 "sctp_handle_shutdown_complete: calls free-asoc\n");
3131 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3132 so = SCTP_INP_SO(stcb->sctp_ep);
3133 atomic_add_int(&stcb->asoc.refcnt, 1);
3134 SCTP_TCB_UNLOCK(stcb);
3135 SCTP_SOCKET_LOCK(so, 1);
3136 SCTP_TCB_LOCK(stcb);
3137 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3139 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
3140 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3141 SCTP_SOCKET_UNLOCK(so, 1);
3147 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3148 struct sctp_nets *net, uint8_t flg)
3150 switch (desc->chunk_type) {
3152 /* find the tsn to resend (possibly */
3155 struct sctp_tmit_chunk *tp1;
3157 tsn = ntohl(desc->tsn_ifany);
3158 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3159 if (tp1->rec.data.TSN_seq == tsn) {
3163 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) {
3171 * Do it the other way , aka without paying
3172 * attention to queue seq order.
3174 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3175 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3176 if (tp1->rec.data.TSN_seq == tsn) {
3183 SCTP_STAT_INCR(sctps_pdrptsnnf);
3185 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3188 if (((flg & SCTP_BADCRC) == 0) &&
3189 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3192 if ((stcb->asoc.peers_rwnd == 0) &&
3193 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3194 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3197 if (stcb->asoc.peers_rwnd == 0 &&
3198 (flg & SCTP_FROM_MIDDLE_BOX)) {
3199 SCTP_STAT_INCR(sctps_pdrpdizrw);
3202 ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+
3203 sizeof(struct sctp_data_chunk));
3207 for (iii = 0; iii < sizeof(desc->data_bytes);
3209 if (ddp[iii] != desc->data_bytes[iii]) {
3210 SCTP_STAT_INCR(sctps_pdrpbadd);
3218 * this guy had a RTO calculation
3219 * pending on it, cancel it
3223 SCTP_STAT_INCR(sctps_pdrpmark);
3224 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3225 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3227 * mark it as if we were doing a FR, since
3228 * we will be getting gap ack reports behind
3229 * the info from the router.
3231 tp1->rec.data.doing_fast_retransmit = 1;
3233 * mark the tsn with what sequences can
3236 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3237 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3239 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
3242 /* restart the timer */
3243 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3244 stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3245 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3248 /* fix counts and things */
3249 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3250 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3251 tp1->whoTo->flight_size,
3254 tp1->rec.data.TSN_seq);
3256 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3257 sctp_flight_size_decrease(tp1);
3258 sctp_total_flight_decrease(stcb, tp1);
3260 tp1->sent = SCTP_DATAGRAM_RESEND;
3266 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3267 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3270 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3272 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3275 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3276 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3277 audit, stcb->asoc.sent_queue_retran_cnt);
3278 #ifndef SCTP_AUDITING_ENABLED
3279 stcb->asoc.sent_queue_retran_cnt = audit;
3287 struct sctp_tmit_chunk *asconf;
3289 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3291 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3296 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3297 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3298 asconf->sent = SCTP_DATAGRAM_RESEND;
3299 asconf->snd_count--;
3303 case SCTP_INITIATION:
3304 /* resend the INIT */
3305 stcb->asoc.dropped_special_cnt++;
3306 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3308 * If we can get it in, in a few attempts we do
3309 * this, otherwise we let the timer fire.
3311 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3312 stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3313 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3316 case SCTP_SELECTIVE_ACK:
3317 case SCTP_NR_SELECTIVE_ACK:
3318 /* resend the sack */
3319 sctp_send_sack(stcb);
3321 case SCTP_HEARTBEAT_REQUEST:
3322 /* resend a demand HB */
3323 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3325 * Only retransmit if we KNOW we wont destroy the
3328 (void)sctp_send_hb(stcb, 1, net);
3332 sctp_send_shutdown(stcb, net);
3334 case SCTP_SHUTDOWN_ACK:
3335 sctp_send_shutdown_ack(stcb, net);
3337 case SCTP_COOKIE_ECHO:
3339 struct sctp_tmit_chunk *cookie;
3342 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3344 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3349 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3350 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3351 cookie->sent = SCTP_DATAGRAM_RESEND;
3352 sctp_stop_all_cookie_timers(stcb);
3356 case SCTP_COOKIE_ACK:
3357 sctp_send_cookie_ack(stcb);
3359 case SCTP_ASCONF_ACK:
3360 /* resend last asconf ack */
3361 sctp_send_asconf_ack(stcb);
3363 case SCTP_FORWARD_CUM_TSN:
3364 send_forward_tsn(stcb, &stcb->asoc);
3366 /* can't do anything with these */
3367 case SCTP_PACKET_DROPPED:
3368 case SCTP_INITIATION_ACK: /* this should not happen */
3369 case SCTP_HEARTBEAT_ACK:
3370 case SCTP_ABORT_ASSOCIATION:
3371 case SCTP_OPERATION_ERROR:
3372 case SCTP_SHUTDOWN_COMPLETE:
3382 sctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3388 * We set things to 0xffff since this is the last delivered sequence
3389 * and we will be sending in 0 after the reset.
3392 if (number_entries) {
3393 for (i = 0; i < number_entries; i++) {
3394 temp = ntohs(list[i]);
3395 if (temp >= stcb->asoc.streamincnt) {
3398 stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff;
3402 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3403 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3406 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3410 sctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3414 if (number_entries == 0) {
3415 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3416 stcb->asoc.strmout[i].next_sequence_sent = 0;
3418 } else if (number_entries) {
3419 for (i = 0; i < number_entries; i++) {
3422 temp = ntohs(list[i]);
3423 if (temp >= stcb->asoc.streamoutcnt) {
3424 /* no such stream */
3427 stcb->asoc.strmout[temp].next_sequence_sent = 0;
3430 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3434 struct sctp_stream_reset_out_request *
3435 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3437 struct sctp_association *asoc;
3438 struct sctp_stream_reset_out_req *req;
3439 struct sctp_stream_reset_out_request *r;
3440 struct sctp_tmit_chunk *chk;
3444 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3445 asoc->stream_reset_outstanding = 0;
3448 if (stcb->asoc.str_reset == NULL) {
3449 asoc->stream_reset_outstanding = 0;
3452 chk = stcb->asoc.str_reset;
3453 if (chk->data == NULL) {
3457 /* he wants a copy of the chk pointer */
3460 clen = chk->send_size;
3461 req = mtod(chk->data, struct sctp_stream_reset_out_req *);
3463 if (ntohl(r->request_seq) == seq) {
3467 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3468 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3469 /* move to the next one, there can only be a max of two */
3470 r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len);
3471 if (ntohl(r->request_seq) == seq) {
3475 /* that seq is not here */
3480 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3482 struct sctp_association *asoc;
3483 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3485 if (stcb->asoc.str_reset == NULL) {
3490 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3491 TAILQ_REMOVE(&asoc->control_send_queue,
3495 sctp_m_freem(chk->data);
3498 asoc->ctrl_queue_cnt--;
3499 sctp_free_a_chunk(stcb, chk);
3500 /* sa_ignore NO_NULL_CHK */
3501 stcb->asoc.str_reset = NULL;
3506 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3507 uint32_t seq, uint32_t action,
3508 struct sctp_stream_reset_response *respin)
3512 struct sctp_association *asoc = &stcb->asoc;
3513 struct sctp_tmit_chunk *chk;
3514 struct sctp_stream_reset_out_request *srparam;
3517 if (asoc->stream_reset_outstanding == 0) {
3521 if (seq == stcb->asoc.str_reset_seq_out) {
3522 srparam = sctp_find_stream_reset(stcb, seq, &chk);
3524 stcb->asoc.str_reset_seq_out++;
3525 type = ntohs(srparam->ph.param_type);
3526 lparm_len = ntohs(srparam->ph.param_length);
3527 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3528 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3529 asoc->stream_reset_out_is_outstanding = 0;
3530 if (asoc->stream_reset_outstanding)
3531 asoc->stream_reset_outstanding--;
3532 if (action == SCTP_STREAM_RESET_PERFORMED) {
3534 sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams);
3536 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3538 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3539 /* Answered my request */
3540 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3541 if (asoc->stream_reset_outstanding)
3542 asoc->stream_reset_outstanding--;
3543 if (action != SCTP_STREAM_RESET_PERFORMED) {
3544 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3546 } else if (type == SCTP_STR_RESET_ADD_STREAMS) {
3547 /* Ok we now may have more streams */
3548 if (asoc->stream_reset_outstanding)
3549 asoc->stream_reset_outstanding--;
3550 if (action == SCTP_STREAM_RESET_PERFORMED) {
3551 /* Put the new streams into effect */
3552 stcb->asoc.streamoutcnt = stcb->asoc.strm_realoutsize;
3553 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_OK, stcb,
3554 (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED);
3556 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_FAIL, stcb,
3557 (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED);
3559 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3561 * a) Adopt the new in tsn.
3563 * c) Adopt the new out-tsn
3565 struct sctp_stream_reset_response_tsn *resp;
3566 struct sctp_forward_tsn_chunk fwdtsn;
3569 if (respin == NULL) {
3573 if (action == SCTP_STREAM_RESET_PERFORMED) {
3574 resp = (struct sctp_stream_reset_response_tsn *)respin;
3575 asoc->stream_reset_outstanding--;
3576 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3577 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3578 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3579 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3583 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3584 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3585 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3587 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3588 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3589 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3591 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3592 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3594 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3595 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3597 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3598 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3602 /* get rid of the request and get the request flags */
3603 if (asoc->stream_reset_outstanding == 0) {
3604 sctp_clean_up_stream_reset(stcb);
3612 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3613 struct sctp_tmit_chunk *chk,
3614 struct sctp_stream_reset_in_request *req, int trunc)
3622 * peer wants me to send a str-reset to him for my outgoing seq's if
3625 struct sctp_association *asoc = &stcb->asoc;
3627 seq = ntohl(req->request_seq);
3628 if (asoc->str_reset_seq_in == seq) {
3630 /* Can't do it, since they exceeded our buffer size */
3631 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3632 asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3633 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3634 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3635 len = ntohs(req->ph.param_length);
3636 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3637 for (i = 0; i < number_entries; i++) {
3638 temp = ntohs(req->list_of_streams[i]);
3639 req->list_of_streams[i] = temp;
3641 /* move the reset action back one */
3642 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3643 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3644 sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams,
3645 asoc->str_reset_seq_out,
3646 seq, (asoc->sending_seq - 1));
3647 asoc->stream_reset_out_is_outstanding = 1;
3648 asoc->str_reset = chk;
3649 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
3650 stcb->asoc.stream_reset_outstanding++;
3652 /* Can't do it, since we have sent one out */
3653 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3654 asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER;
3655 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3657 asoc->str_reset_seq_in++;
3658 } else if (asoc->str_reset_seq_in - 1 == seq) {
3659 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3660 } else if (asoc->str_reset_seq_in - 2 == seq) {
3661 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3663 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3668 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3669 struct sctp_tmit_chunk *chk,
3670 struct sctp_stream_reset_tsn_request *req)
3672 /* reset all in and out and update the tsn */
3674 * A) reset my str-seq's on in and out. B) Select a receive next,
3675 * and set cum-ack to it. Also process this selected number as a
3676 * fwd-tsn as well. C) set in the response my next sending seq.
3678 struct sctp_forward_tsn_chunk fwdtsn;
3679 struct sctp_association *asoc = &stcb->asoc;
3683 seq = ntohl(req->request_seq);
3684 if (asoc->str_reset_seq_in == seq) {
3685 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3686 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3687 fwdtsn.ch.chunk_flags = 0;
3688 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3689 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3693 stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3694 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3695 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3697 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3698 stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1;
3699 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3700 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3701 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3702 atomic_add_int(&stcb->asoc.sending_seq, 1);
3703 /* save off historical data for retrans */
3704 stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0];
3705 stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq;
3706 stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0];
3707 stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn;
3709 sctp_add_stream_reset_result_tsn(chk,
3710 ntohl(req->request_seq),
3711 SCTP_STREAM_RESET_PERFORMED,
3712 stcb->asoc.sending_seq,
3713 stcb->asoc.mapping_array_base_tsn);
3714 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3715 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3716 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3717 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3719 asoc->str_reset_seq_in++;
3720 } else if (asoc->str_reset_seq_in - 1 == seq) {
3721 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3722 stcb->asoc.last_sending_seq[0],
3723 stcb->asoc.last_base_tsnsent[0]
3725 } else if (asoc->str_reset_seq_in - 2 == seq) {
3726 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3727 stcb->asoc.last_sending_seq[1],
3728 stcb->asoc.last_base_tsnsent[1]
3731 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3737 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3738 struct sctp_tmit_chunk *chk,
3739 struct sctp_stream_reset_out_request *req, int trunc)
3742 int number_entries, len;
3743 struct sctp_association *asoc = &stcb->asoc;
3745 seq = ntohl(req->request_seq);
3747 /* now if its not a duplicate we process it */
3748 if (asoc->str_reset_seq_in == seq) {
3749 len = ntohs(req->ph.param_length);
3750 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3752 * the sender is resetting, handle the list issue.. we must
3753 * a) verify if we can do the reset, if so no problem b) If
3754 * we can't do the reset we must copy the request. c) queue
3755 * it, and setup the data in processor to trigger it off
3756 * when needed and dequeue all the queued data.
3758 tsn = ntohl(req->send_reset_at_tsn);
3760 /* move the reset action back one */
3761 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3763 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED);
3764 asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3765 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3766 /* we can do it now */
3767 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3768 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3769 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3772 * we must queue it up and thus wait for the TSN's
3773 * to arrive that are at or before tsn
3775 struct sctp_stream_reset_list *liste;
3778 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3779 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3780 siz, SCTP_M_STRESET);
3781 if (liste == NULL) {
3782 /* gak out of memory */
3783 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED);
3784 asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3788 liste->number_entries = number_entries;
3789 memcpy(&liste->req, req,
3790 (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t))));
3791 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3792 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3793 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3795 asoc->str_reset_seq_in++;
3796 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3798 * one seq back, just echo back last action since my
3799 * response was lost.
3801 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3802 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3804 * two seq back, just echo back last action since my
3805 * response was lost.
3807 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3809 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3814 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3815 struct sctp_stream_reset_add_strm *str_add)
3818 * Peer is requesting to add more streams. If its within our
3819 * max-streams we will allow it.
3821 uint16_t num_stream, i;
3823 struct sctp_association *asoc = &stcb->asoc;
3824 struct sctp_queued_to_read *ctl, *nctl;
3826 /* Get the number. */
3827 seq = ntohl(str_add->request_seq);
3828 num_stream = ntohs(str_add->number_of_streams);
3829 /* Now what would be the new total? */
3830 if (asoc->str_reset_seq_in == seq) {
3831 num_stream += stcb->asoc.streamincnt;
3832 if (num_stream > stcb->asoc.max_inbound_streams) {
3833 /* We must reject it they ask for to many */
3835 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED);
3836 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3837 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3839 /* Ok, we can do that :-) */
3840 struct sctp_stream_in *oldstrm;
3842 /* save off the old */
3843 oldstrm = stcb->asoc.strmin;
3844 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
3845 (num_stream * sizeof(struct sctp_stream_in)),
3847 if (stcb->asoc.strmin == NULL) {
3848 stcb->asoc.strmin = oldstrm;
3851 /* copy off the old data */
3852 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3853 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3854 stcb->asoc.strmin[i].stream_no = i;
3855 stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered;
3856 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
3857 /* now anything on those queues? */
3858 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) {
3859 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next);
3860 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next);
3863 /* Init the new streams */
3864 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
3865 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3866 stcb->asoc.strmin[i].stream_no = i;
3867 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3868 stcb->asoc.strmin[i].delivery_started = 0;
3870 SCTP_FREE(oldstrm, SCTP_M_STRMI);
3871 /* update the size */
3872 stcb->asoc.streamincnt = num_stream;
3874 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3875 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3876 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3877 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_INSTREAM_ADD_OK, stcb,
3878 (uint32_t) stcb->asoc.streamincnt, NULL, SCTP_SO_NOT_LOCKED);
3880 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3882 * one seq back, just echo back last action since my
3883 * response was lost.
3885 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3886 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3888 * two seq back, just echo back last action since my
3889 * response was lost.
3891 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3893 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3899 __attribute__((noinline))
3902 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3903 struct sctp_stream_reset_out_req *sr_req)
3905 int chk_length, param_len, ptype;
3906 struct sctp_paramhdr pstore;
3907 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
3912 struct sctp_tmit_chunk *chk;
3913 struct sctp_chunkhdr *ch;
3914 struct sctp_paramhdr *ph;
3918 /* now it may be a reset or a reset-response */
3919 chk_length = ntohs(sr_req->ch.chunk_length);
3921 /* setup for adding the response */
3922 sctp_alloc_a_chunk(stcb, chk);
3926 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
3927 chk->rec.chunk_id.can_take_data = 0;
3928 chk->asoc = &stcb->asoc;
3929 chk->no_fr_allowed = 0;
3930 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
3931 chk->book_size_scale = 0;
3932 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
3933 if (chk->data == NULL) {
3936 sctp_m_freem(chk->data);
3939 sctp_free_a_chunk(stcb, chk);
3942 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
3944 /* setup chunk parameters */
3945 chk->sent = SCTP_DATAGRAM_UNSENT;
3947 chk->whoTo = stcb->asoc.primary_destination;
3948 atomic_add_int(&chk->whoTo->ref_count, 1);
3950 ch = mtod(chk->data, struct sctp_chunkhdr *);
3951 ch->chunk_type = SCTP_STREAM_RESET;
3952 ch->chunk_flags = 0;
3953 ch->chunk_length = htons(chk->send_size);
3954 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
3955 offset += sizeof(struct sctp_chunkhdr);
3956 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) {
3957 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore);
3960 param_len = ntohs(ph->param_length);
3961 if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) {
3965 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)),
3966 (uint8_t *) & cstore);
3967 ptype = ntohs(ph->param_type);
3969 if (param_len > (int)sizeof(cstore)) {
3975 if (num_param > SCTP_MAX_RESET_PARAMS) {
3976 /* hit the max of parameters already sorry.. */
3979 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
3980 struct sctp_stream_reset_out_request *req_out;
3982 req_out = (struct sctp_stream_reset_out_request *)ph;
3984 if (stcb->asoc.stream_reset_outstanding) {
3985 seq = ntohl(req_out->response_seq);
3986 if (seq == stcb->asoc.str_reset_seq_out) {
3988 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL);
3991 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
3992 } else if (ptype == SCTP_STR_RESET_ADD_STREAMS) {
3993 struct sctp_stream_reset_add_strm *str_add;
3995 str_add = (struct sctp_stream_reset_add_strm *)ph;
3997 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
3998 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
3999 struct sctp_stream_reset_in_request *req_in;
4003 req_in = (struct sctp_stream_reset_in_request *)ph;
4005 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4006 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4007 struct sctp_stream_reset_tsn_request *req_tsn;
4010 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4012 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4014 goto strres_nochunk;
4018 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4019 struct sctp_stream_reset_response *resp;
4022 resp = (struct sctp_stream_reset_response *)ph;
4023 seq = ntohl(resp->response_seq);
4024 result = ntohl(resp->result);
4025 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4027 goto strres_nochunk;
4032 offset += SCTP_SIZE32(param_len);
4033 chk_length -= SCTP_SIZE32(param_len);
4036 /* we have no response free the stuff */
4037 goto strres_nochunk;
4039 /* ok we have a chunk to link in */
4040 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4043 stcb->asoc.ctrl_queue_cnt++;
4048 * Handle a router or endpoints report of a packet loss, there are two ways
4049 * to handle this, either we get the whole packet and must disect it
4050 * ourselves (possibly with truncation and or corruption) or it is a summary
4051 * from a middle box that did the disectting for us.
4054 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4055 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4057 uint32_t bottle_bw, on_queue;
4061 struct sctp_chunk_desc desc;
4062 struct sctp_chunkhdr *ch;
4064 chlen = ntohs(cp->ch.chunk_length);
4065 chlen -= sizeof(struct sctp_pktdrop_chunk);
4066 /* XXX possible chlen underflow */
4069 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4070 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4072 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4073 chlen -= sizeof(struct sctphdr);
4074 /* XXX possible chlen underflow */
4075 memset(&desc, 0, sizeof(desc));
4077 trunc_len = (uint16_t) ntohs(cp->trunc_len);
4078 if (trunc_len > limit) {
4081 /* now the chunks themselves */
4082 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4083 desc.chunk_type = ch->chunk_type;
4084 /* get amount we need to move */
4085 at = ntohs(ch->chunk_length);
4086 if (at < sizeof(struct sctp_chunkhdr)) {
4087 /* corrupt chunk, maybe at the end? */
4088 SCTP_STAT_INCR(sctps_pdrpcrupt);
4091 if (trunc_len == 0) {
4092 /* we are supposed to have all of it */
4094 /* corrupt skip it */
4095 SCTP_STAT_INCR(sctps_pdrpcrupt);
4099 /* is there enough of it left ? */
4100 if (desc.chunk_type == SCTP_DATA) {
4101 if (chlen < (sizeof(struct sctp_data_chunk) +
4102 sizeof(desc.data_bytes))) {
4106 if (chlen < sizeof(struct sctp_chunkhdr)) {
4111 if (desc.chunk_type == SCTP_DATA) {
4112 /* can we get out the tsn? */
4113 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4114 SCTP_STAT_INCR(sctps_pdrpmbda);
4116 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4118 struct sctp_data_chunk *dcp;
4122 dcp = (struct sctp_data_chunk *)ch;
4123 ddp = (uint8_t *) (dcp + 1);
4124 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4125 desc.data_bytes[iii] = ddp[iii];
4127 desc.tsn_ifany = dcp->dp.tsn;
4129 /* nope we are done. */
4130 SCTP_STAT_INCR(sctps_pdrpnedat);
4134 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4135 SCTP_STAT_INCR(sctps_pdrpmbct);
4138 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4139 SCTP_STAT_INCR(sctps_pdrppdbrk);
4142 if (SCTP_SIZE32(at) > chlen) {
4145 chlen -= SCTP_SIZE32(at);
4146 if (chlen < sizeof(struct sctp_chunkhdr)) {
4147 /* done, none left */
4150 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4152 /* Now update any rwnd --- possibly */
4153 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4154 /* From a peer, we get a rwnd report */
4157 SCTP_STAT_INCR(sctps_pdrpfehos);
4159 bottle_bw = ntohl(cp->bottle_bw);
4160 on_queue = ntohl(cp->current_onq);
4161 if (bottle_bw && on_queue) {
4162 /* a rwnd report is in here */
4163 if (bottle_bw > on_queue)
4164 a_rwnd = bottle_bw - on_queue;
4169 stcb->asoc.peers_rwnd = 0;
4171 if (a_rwnd > stcb->asoc.total_flight) {
4172 stcb->asoc.peers_rwnd =
4173 a_rwnd - stcb->asoc.total_flight;
4175 stcb->asoc.peers_rwnd = 0;
4177 if (stcb->asoc.peers_rwnd <
4178 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4179 /* SWS sender side engages */
4180 stcb->asoc.peers_rwnd = 0;
4185 SCTP_STAT_INCR(sctps_pdrpfmbox);
4188 /* now middle boxes in sat networks get a cwnd bump */
4189 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4190 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4191 (stcb->asoc.sat_network)) {
4193 * This is debateable but for sat networks it makes sense
4194 * Note if a T3 timer has went off, we will prohibit any
4195 * changes to cwnd until we exit the t3 loss recovery.
4197 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4198 net, cp, &bottle_bw, &on_queue);
4203 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4204 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4205 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4206 * length of the complete packet outputs: - length: modified to remaining
4207 * length after control processing - netp: modified to new sctp_nets after
4208 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4209 * bad packet,...) otherwise return the tcb for this packet
4212 __attribute__((noinline))
4214 static struct sctp_tcb *
4215 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4216 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4217 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4218 uint32_t vrf_id, uint16_t port)
4220 struct sctp_association *asoc;
4222 int num_chunks = 0; /* number of control chunks processed */
4223 uint32_t chk_length;
4225 int abort_no_unlock = 0;
4229 * How big should this be, and should it be alloc'd? Lets try the
4230 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4231 * until we get into jumbo grams and such..
4233 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4234 struct sctp_tcb *locked_tcb = stcb;
4236 uint32_t auth_offset = 0, auth_len = 0;
4237 int auth_skipped = 0;
4240 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4245 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4246 iphlen, *offset, length, stcb);
4248 /* validate chunk header length... */
4249 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4250 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4251 ntohs(ch->chunk_length));
4253 SCTP_TCB_UNLOCK(locked_tcb);
4258 * validate the verification tag
4260 vtag_in = ntohl(sh->v_tag);
4263 SCTP_TCB_LOCK_ASSERT(locked_tcb);
4265 if (ch->chunk_type == SCTP_INITIATION) {
4266 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4267 ntohs(ch->chunk_length), vtag_in);
4269 /* protocol error- silently discard... */
4270 SCTP_STAT_INCR(sctps_badvtag);
4272 SCTP_TCB_UNLOCK(locked_tcb);
4276 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4278 * If there is no stcb, skip the AUTH chunk and process
4279 * later after a stcb is found (to validate the lookup was
4282 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4284 !SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4285 /* save this chunk for later processing */
4287 auth_offset = *offset;
4288 auth_len = ntohs(ch->chunk_length);
4290 /* (temporarily) move past this chunk */
4291 *offset += SCTP_SIZE32(auth_len);
4292 if (*offset >= length) {
4293 /* no more data left in the mbuf chain */
4296 SCTP_TCB_UNLOCK(locked_tcb);
4300 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4301 sizeof(struct sctp_chunkhdr), chunk_buf);
4307 SCTP_TCB_UNLOCK(locked_tcb);
4311 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4312 goto process_control_chunks;
4315 * first check if it's an ASCONF with an unknown src addr we
4316 * need to look inside to find the association
4318 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4319 struct sctp_chunkhdr *asconf_ch = ch;
4320 uint32_t asconf_offset = 0, asconf_len = 0;
4322 /* inp's refcount may be reduced */
4323 SCTP_INP_INCR_REF(inp);
4325 asconf_offset = *offset;
4327 asconf_len = ntohs(asconf_ch->chunk_length);
4328 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4330 stcb = sctp_findassociation_ep_asconf(m, iphlen,
4331 *offset, sh, &inp, netp, vrf_id);
4334 asconf_offset += SCTP_SIZE32(asconf_len);
4335 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4336 sizeof(struct sctp_chunkhdr), chunk_buf);
4337 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4340 * reduce inp's refcount if not reduced in
4341 * sctp_findassociation_ep_asconf().
4343 SCTP_INP_DECR_REF(inp);
4348 /* now go back and verify any auth chunk to be sure */
4349 if (auth_skipped && (stcb != NULL)) {
4350 struct sctp_auth_chunk *auth;
4352 auth = (struct sctp_auth_chunk *)
4353 sctp_m_getptr(m, auth_offset,
4354 auth_len, chunk_buf);
4357 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4359 /* auth HMAC failed so dump it */
4362 SCTP_TCB_UNLOCK(locked_tcb);
4366 /* remaining chunks are HMAC checked */
4367 stcb->asoc.authenticated = 1;
4372 /* no association, so it's out of the blue... */
4373 sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL,
4377 SCTP_TCB_UNLOCK(locked_tcb);
4382 /* ABORT and SHUTDOWN can use either v_tag... */
4383 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4384 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4385 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4386 if ((vtag_in == asoc->my_vtag) ||
4387 ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
4388 (vtag_in == asoc->peer_vtag))) {
4391 /* drop this packet... */
4392 SCTP_STAT_INCR(sctps_badvtag);
4394 SCTP_TCB_UNLOCK(locked_tcb);
4398 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4399 if (vtag_in != asoc->my_vtag) {
4401 * this could be a stale SHUTDOWN-ACK or the
4402 * peer never got the SHUTDOWN-COMPLETE and
4403 * is still hung; we have started a new asoc
4404 * but it won't complete until the shutdown
4408 SCTP_TCB_UNLOCK(locked_tcb);
4410 sctp_handle_ootb(m, iphlen, *offset, sh, inp,
4411 NULL, vrf_id, port);
4415 /* for all other chunks, vtag must match */
4416 if (vtag_in != asoc->my_vtag) {
4417 /* invalid vtag... */
4418 SCTPDBG(SCTP_DEBUG_INPUT3,
4419 "invalid vtag: %xh, expect %xh\n",
4420 vtag_in, asoc->my_vtag);
4421 SCTP_STAT_INCR(sctps_badvtag);
4423 SCTP_TCB_UNLOCK(locked_tcb);
4429 } /* end if !SCTP_COOKIE_ECHO */
4431 * process all control chunks...
4433 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4435 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4436 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4437 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4438 /* implied cookie-ack.. we must have lost the ack */
4439 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4440 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4441 stcb->asoc.overall_error_count,
4443 SCTP_FROM_SCTP_INPUT,
4446 stcb->asoc.overall_error_count = 0;
4447 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4450 process_control_chunks:
4451 while (IS_SCTP_CONTROL(ch)) {
4452 /* validate chunk length */
4453 chk_length = ntohs(ch->chunk_length);
4454 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4455 ch->chunk_type, chk_length);
4456 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4457 if (chk_length < sizeof(*ch) ||
4458 (*offset + (int)chk_length) > length) {
4461 SCTP_TCB_UNLOCK(locked_tcb);
4465 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4467 * INIT-ACK only gets the init ack "header" portion only
4468 * because we don't have to process the peer's COOKIE. All
4469 * others get a complete chunk.
4471 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4472 (ch->chunk_type == SCTP_INITIATION)) {
4473 /* get an init-ack chunk */
4474 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4475 sizeof(struct sctp_init_ack_chunk), chunk_buf);
4479 SCTP_TCB_UNLOCK(locked_tcb);
4484 /* For cookies and all other chunks. */
4485 if (chk_length > sizeof(chunk_buf)) {
4487 * use just the size of the chunk buffer so
4488 * the front part of our chunks fit in
4489 * contiguous space up to the chunk buffer
4490 * size (508 bytes). For chunks that need to
4491 * get more than that they must use the
4492 * sctp_m_getptr() function or other means
4493 * (e.g. know how to parse mbuf chains).
4494 * Cookies do this already.
4496 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4497 (sizeof(chunk_buf) - 4),
4502 SCTP_TCB_UNLOCK(locked_tcb);
4507 /* We can fit it all */
4508 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4509 chk_length, chunk_buf);
4511 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4514 SCTP_TCB_UNLOCK(locked_tcb);
4521 /* Save off the last place we got a control from */
4523 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4525 * allow last_control to be NULL if
4526 * ASCONF... ASCONF processing will find the
4529 if ((netp != NULL) && (*netp != NULL))
4530 stcb->asoc.last_control_chunk_from = *netp;
4533 #ifdef SCTP_AUDITING_ENABLED
4534 sctp_audit_log(0xB0, ch->chunk_type);
4537 /* check to see if this chunk required auth, but isn't */
4538 if ((stcb != NULL) &&
4539 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
4540 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4541 !stcb->asoc.authenticated) {
4542 /* "silently" ignore */
4543 SCTP_STAT_INCR(sctps_recvauthmissing);
4546 switch (ch->chunk_type) {
4547 case SCTP_INITIATION:
4548 /* must be first and only chunk */
4549 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4550 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4551 /* We are not interested anymore? */
4552 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4554 * collision case where we are
4555 * sending to them too
4560 SCTP_TCB_UNLOCK(locked_tcb);
4566 if ((chk_length > SCTP_LARGEST_INIT_ACCEPTED) ||
4568 (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) {
4571 SCTP_TCB_UNLOCK(locked_tcb);
4575 if ((stcb != NULL) &&
4576 (SCTP_GET_STATE(&stcb->asoc) ==
4577 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
4578 sctp_send_shutdown_ack(stcb,
4579 stcb->asoc.primary_destination);
4581 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4583 SCTP_TCB_UNLOCK(locked_tcb);
4588 sctp_handle_init(m, iphlen, *offset, sh,
4589 (struct sctp_init_chunk *)ch, inp,
4590 stcb, *netp, &abort_no_unlock, vrf_id, port);
4592 if (abort_no_unlock)
4597 SCTP_TCB_UNLOCK(locked_tcb);
4601 case SCTP_PAD_CHUNK:
4603 case SCTP_INITIATION_ACK:
4604 /* must be first and only chunk */
4605 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4606 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4607 /* We are not interested anymore */
4608 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4611 if (locked_tcb != stcb) {
4613 SCTP_TCB_UNLOCK(locked_tcb);
4617 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4618 so = SCTP_INP_SO(inp);
4619 atomic_add_int(&stcb->asoc.refcnt, 1);
4620 SCTP_TCB_UNLOCK(stcb);
4621 SCTP_SOCKET_LOCK(so, 1);
4622 SCTP_TCB_LOCK(stcb);
4623 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4625 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4626 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4627 SCTP_SOCKET_UNLOCK(so, 1);
4633 if ((num_chunks > 1) ||
4634 (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) {
4637 SCTP_TCB_UNLOCK(locked_tcb);
4641 if ((netp) && (*netp)) {
4642 ret = sctp_handle_init_ack(m, iphlen, *offset, sh,
4643 (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id);
4648 * Special case, I must call the output routine to
4649 * get the cookie echoed
4651 if (abort_no_unlock)
4654 if ((stcb) && ret == 0)
4655 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4658 SCTP_TCB_UNLOCK(locked_tcb);
4662 case SCTP_SELECTIVE_ACK:
4664 struct sctp_sack_chunk *sack;
4666 uint32_t a_rwnd, cum_ack;
4667 uint16_t num_seg, num_dup;
4669 int offset_seg, offset_dup;
4671 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4672 SCTP_STAT_INCR(sctps_recvsacks);
4674 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4677 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4678 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4681 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4683 * If we have sent a shutdown-ack, we will pay no
4684 * attention to a sack sent in to us since
4685 * we don't care anymore.
4689 sack = (struct sctp_sack_chunk *)ch;
4690 flags = ch->chunk_flags;
4691 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4692 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4693 num_dup = ntohs(sack->sack.num_dup_tsns);
4694 a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
4695 if (sizeof(struct sctp_sack_chunk) +
4696 num_seg * sizeof(struct sctp_gap_ack_block) +
4697 num_dup * sizeof(uint32_t) != chk_length) {
4698 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4701 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4702 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4703 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4704 cum_ack, num_seg, a_rwnd);
4705 stcb->asoc.seen_a_sack_this_pkt = 1;
4706 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4708 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4709 (stcb->asoc.saw_sack_with_frags == 0) &&
4710 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4711 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4714 * We have a SIMPLE sack having no
4715 * prior segments and data on sent
4716 * queue to be acked.. Use the
4717 * faster path sack processing. We
4718 * also allow window update sacks
4719 * with no missing segments to go
4722 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
4725 sctp_handle_sack(m, offset_seg, offset_dup,
4727 num_seg, 0, num_dup, &abort_now, flags,
4728 cum_ack, a_rwnd, ecne_seen);
4731 /* ABORT signal from sack processing */
4735 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4736 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4737 (stcb->asoc.stream_queue_cnt == 0)) {
4738 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4743 * EY - nr_sack: If the received chunk is an
4746 case SCTP_NR_SELECTIVE_ACK:
4748 struct sctp_nr_sack_chunk *nr_sack;
4750 uint32_t a_rwnd, cum_ack;
4751 uint16_t num_seg, num_nr_seg, num_dup;
4753 int offset_seg, offset_dup;
4755 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
4756 SCTP_STAT_INCR(sctps_recvsacks);
4758 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
4761 if ((stcb->asoc.sctp_nr_sack_on_off == 0) ||
4762 (stcb->asoc.peer_supports_nr_sack == 0)) {
4765 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4766 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
4769 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4771 * If we have sent a shutdown-ack, we will pay no
4772 * attention to a sack sent in to us since
4773 * we don't care anymore.
4777 nr_sack = (struct sctp_nr_sack_chunk *)ch;
4778 flags = ch->chunk_flags;
4779 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4780 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4781 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4782 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4783 a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd);
4784 if (sizeof(struct sctp_nr_sack_chunk) +
4785 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4786 num_dup * sizeof(uint32_t) != chk_length) {
4787 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4790 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4791 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4792 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4793 cum_ack, num_seg, a_rwnd);
4794 stcb->asoc.seen_a_sack_this_pkt = 1;
4795 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4796 (num_seg == 0) && (num_nr_seg == 0) &&
4797 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4798 (stcb->asoc.saw_sack_with_frags == 0) &&
4799 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4800 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
4802 * We have a SIMPLE sack having no
4803 * prior segments and data on sent
4804 * queue to be acked. Use the faster
4805 * path sack processing. We also
4806 * allow window update sacks with no
4807 * missing segments to go this way
4810 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
4811 &abort_now, ecne_seen);
4814 sctp_handle_sack(m, offset_seg, offset_dup,
4816 num_seg, num_nr_seg, num_dup, &abort_now, flags,
4817 cum_ack, a_rwnd, ecne_seen);
4820 /* ABORT signal from sack processing */
4824 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4825 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4826 (stcb->asoc.stream_queue_cnt == 0)) {
4827 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4832 case SCTP_HEARTBEAT_REQUEST:
4833 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
4834 if ((stcb) && netp && *netp) {
4835 SCTP_STAT_INCR(sctps_recvheartbeat);
4836 sctp_send_heartbeat_ack(stcb, m, *offset,
4839 /* He's alive so give him credit */
4840 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4841 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4842 stcb->asoc.overall_error_count,
4844 SCTP_FROM_SCTP_INPUT,
4847 stcb->asoc.overall_error_count = 0;
4850 case SCTP_HEARTBEAT_ACK:
4851 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
4852 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
4856 SCTP_TCB_UNLOCK(locked_tcb);
4860 /* He's alive so give him credit */
4861 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4862 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4863 stcb->asoc.overall_error_count,
4865 SCTP_FROM_SCTP_INPUT,
4868 stcb->asoc.overall_error_count = 0;
4869 SCTP_STAT_INCR(sctps_recvheartbeatack);
4871 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
4874 case SCTP_ABORT_ASSOCIATION:
4875 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
4877 if ((stcb) && netp && *netp)
4878 sctp_handle_abort((struct sctp_abort_chunk *)ch,
4884 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
4886 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
4889 SCTP_TCB_UNLOCK(locked_tcb);
4893 if (netp && *netp) {
4896 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
4897 stcb, *netp, &abort_flag);
4904 case SCTP_SHUTDOWN_ACK:
4905 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb);
4906 if ((stcb) && (netp) && (*netp))
4907 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
4912 case SCTP_OPERATION_ERROR:
4913 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
4914 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
4920 case SCTP_COOKIE_ECHO:
4921 SCTPDBG(SCTP_DEBUG_INPUT3,
4922 "SCTP_COOKIE-ECHO, stcb %p\n", stcb);
4923 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4926 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4927 /* We are not interested anymore */
4930 SCTP_TCB_UNLOCK(stcb);
4937 * First are we accepting? We do this again here
4938 * since it is possible that a previous endpoint WAS
4939 * listening responded to a INIT-ACK and then
4940 * closed. We opened and bound.. and are now no
4944 if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
4945 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4946 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
4948 struct sctp_paramhdr *phdr;
4950 oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
4951 0, M_DONTWAIT, 1, MT_DATA);
4953 SCTP_BUF_LEN(oper) =
4954 sizeof(struct sctp_paramhdr);
4956 struct sctp_paramhdr *);
4958 htons(SCTP_CAUSE_OUT_OF_RESC);
4959 phdr->param_length =
4960 htons(sizeof(struct sctp_paramhdr));
4962 sctp_abort_association(inp, stcb, m,
4963 iphlen, sh, oper, vrf_id, port);
4968 struct mbuf *ret_buf;
4969 struct sctp_inpcb *linp;
4978 SCTP_ASOC_CREATE_LOCK(linp);
4979 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4980 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4981 SCTP_ASOC_CREATE_UNLOCK(linp);
4987 sctp_handle_cookie_echo(m, iphlen,
4989 (struct sctp_cookie_echo_chunk *)ch,
5001 SCTP_ASOC_CREATE_UNLOCK(linp);
5003 if (ret_buf == NULL) {
5005 SCTP_TCB_UNLOCK(locked_tcb);
5007 SCTPDBG(SCTP_DEBUG_INPUT3,
5008 "GAK, null buffer\n");
5013 /* if AUTH skipped, see if it verified... */
5018 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5020 * Restart the timer if we have
5023 struct sctp_tmit_chunk *chk;
5025 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5026 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5030 case SCTP_COOKIE_ACK:
5031 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb);
5032 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5034 SCTP_TCB_UNLOCK(locked_tcb);
5038 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5039 /* We are not interested anymore */
5040 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5043 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5044 so = SCTP_INP_SO(inp);
5045 atomic_add_int(&stcb->asoc.refcnt, 1);
5046 SCTP_TCB_UNLOCK(stcb);
5047 SCTP_SOCKET_LOCK(so, 1);
5048 SCTP_TCB_LOCK(stcb);
5049 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5051 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
5052 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5053 SCTP_SOCKET_UNLOCK(so, 1);
5059 /* He's alive so give him credit */
5060 if ((stcb) && netp && *netp) {
5061 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5062 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5063 stcb->asoc.overall_error_count,
5065 SCTP_FROM_SCTP_INPUT,
5068 stcb->asoc.overall_error_count = 0;
5069 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5073 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5074 /* He's alive so give him credit */
5075 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5078 SCTP_TCB_UNLOCK(locked_tcb);
5084 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5085 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5086 stcb->asoc.overall_error_count,
5088 SCTP_FROM_SCTP_INPUT,
5091 stcb->asoc.overall_error_count = 0;
5092 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5098 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5099 /* He's alive so give him credit */
5100 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5103 SCTP_TCB_UNLOCK(locked_tcb);
5109 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5110 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5111 stcb->asoc.overall_error_count,
5113 SCTP_FROM_SCTP_INPUT,
5116 stcb->asoc.overall_error_count = 0;
5117 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5120 case SCTP_SHUTDOWN_COMPLETE:
5121 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb);
5122 /* must be first and only chunk */
5123 if ((num_chunks > 1) ||
5124 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5127 SCTP_TCB_UNLOCK(locked_tcb);
5131 if ((stcb) && netp && *netp) {
5132 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5139 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5140 /* He's alive so give him credit */
5142 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5143 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5144 stcb->asoc.overall_error_count,
5146 SCTP_FROM_SCTP_INPUT,
5149 stcb->asoc.overall_error_count = 0;
5150 sctp_handle_asconf(m, *offset,
5151 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5155 case SCTP_ASCONF_ACK:
5156 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5157 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5160 SCTP_TCB_UNLOCK(locked_tcb);
5165 if ((stcb) && netp && *netp) {
5166 /* He's alive so give him credit */
5167 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5168 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5169 stcb->asoc.overall_error_count,
5171 SCTP_FROM_SCTP_INPUT,
5174 stcb->asoc.overall_error_count = 0;
5175 sctp_handle_asconf_ack(m, *offset,
5176 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5177 if (abort_no_unlock)
5181 case SCTP_FORWARD_CUM_TSN:
5182 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5183 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5186 SCTP_TCB_UNLOCK(locked_tcb);
5191 /* He's alive so give him credit */
5195 stcb->asoc.overall_error_count = 0;
5196 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5197 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5198 stcb->asoc.overall_error_count,
5200 SCTP_FROM_SCTP_INPUT,
5204 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5205 /* We are not interested anymore */
5206 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5207 so = SCTP_INP_SO(inp);
5208 atomic_add_int(&stcb->asoc.refcnt, 1);
5209 SCTP_TCB_UNLOCK(stcb);
5210 SCTP_SOCKET_LOCK(so, 1);
5211 SCTP_TCB_LOCK(stcb);
5212 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5214 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
5215 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5216 SCTP_SOCKET_UNLOCK(so, 1);
5221 sctp_handle_forward_tsn(stcb,
5222 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5227 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5228 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5229 stcb->asoc.overall_error_count,
5231 SCTP_FROM_SCTP_INPUT,
5234 stcb->asoc.overall_error_count = 0;
5239 case SCTP_STREAM_RESET:
5240 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5241 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5244 SCTP_TCB_UNLOCK(locked_tcb);
5249 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5250 /* We are not interested anymore */
5251 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5252 so = SCTP_INP_SO(inp);
5253 atomic_add_int(&stcb->asoc.refcnt, 1);
5254 SCTP_TCB_UNLOCK(stcb);
5255 SCTP_SOCKET_LOCK(so, 1);
5256 SCTP_TCB_LOCK(stcb);
5257 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5259 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5260 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5261 SCTP_SOCKET_UNLOCK(so, 1);
5266 if (stcb->asoc.peer_supports_strreset == 0) {
5268 * hmm, peer should have announced this, but
5269 * we will turn it on since he is sending us
5272 stcb->asoc.peer_supports_strreset = 1;
5274 if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) {
5275 /* stop processing */
5280 case SCTP_PACKET_DROPPED:
5281 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5282 /* re-get it all please */
5283 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5286 SCTP_TCB_UNLOCK(locked_tcb);
5291 if (ch && (stcb) && netp && (*netp)) {
5292 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5294 min(chk_length, (sizeof(chunk_buf) - 4)));
5299 case SCTP_AUTHENTICATION:
5300 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5301 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
5305 /* save the first AUTH for later processing */
5306 if (auth_skipped == 0) {
5307 auth_offset = *offset;
5308 auth_len = chk_length;
5311 /* skip this chunk (temporarily) */
5314 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5315 (chk_length > (sizeof(struct sctp_auth_chunk) +
5316 SCTP_AUTH_DIGEST_LEN_MAX))) {
5319 SCTP_TCB_UNLOCK(locked_tcb);
5324 if (got_auth == 1) {
5325 /* skip this chunk... it's already auth'd */
5329 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5331 /* auth HMAC failed so dump the packet */
5335 /* remaining chunks are HMAC checked */
5336 stcb->asoc.authenticated = 1;
5342 /* it's an unknown chunk! */
5343 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5345 struct sctp_paramhdr *phd;
5347 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
5348 0, M_DONTWAIT, 1, MT_DATA);
5350 phd = mtod(mm, struct sctp_paramhdr *);
5352 * We cheat and use param type since
5353 * we did not bother to define a
5354 * error cause struct. They are the
5355 * same basic format with different
5358 phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5359 phd->param_length = htons(chk_length + sizeof(*phd));
5360 SCTP_BUF_LEN(mm) = sizeof(*phd);
5361 SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length),
5363 if (SCTP_BUF_NEXT(mm)) {
5364 #ifdef SCTP_MBUF_LOGGING
5365 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5368 mat = SCTP_BUF_NEXT(mm);
5370 if (SCTP_BUF_IS_EXTENDED(mat)) {
5371 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5373 mat = SCTP_BUF_NEXT(mat);
5377 sctp_queue_op_err(stcb, mm);
5383 if ((ch->chunk_type & 0x80) == 0) {
5384 /* discard this packet */
5387 } /* else skip this bad chunk and continue... */
5389 } /* switch (ch->chunk_type) */
5393 /* get the next chunk */
5394 *offset += SCTP_SIZE32(chk_length);
5395 if (*offset >= length) {
5396 /* no more data left in the mbuf chain */
5399 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5400 sizeof(struct sctp_chunkhdr), chunk_buf);
5403 SCTP_TCB_UNLOCK(locked_tcb);
5410 if (asconf_cnt > 0 && stcb != NULL) {
5411 sctp_send_asconf_ack(stcb);
5419 __attribute__((noinline))
5422 sctp_validate_no_locks(struct sctp_inpcb *inp)
5424 struct sctp_tcb *lstcb;
5426 LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) {
5427 if (mtx_owned(&lstcb->tcb_mtx)) {
5428 panic("Own lock on stcb at return from input");
5431 if (mtx_owned(&inp->inp_create_mtx)) {
5432 panic("Own create lock on inp");
5434 if (mtx_owned(&inp->inp_mtx)) {
5435 panic("Own inp lock on inp");
5442 * common input chunk processing (v4 and v6)
5445 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset,
5446 int length, struct sctphdr *sh, struct sctp_chunkhdr *ch,
5447 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
5448 uint8_t ecn_bits, uint32_t vrf_id, uint16_t port)
5451 * Control chunk processing
5454 int fwd_tsn_seen = 0, data_processed = 0;
5455 struct mbuf *m = *mm;
5458 int cnt_ctrl_ready = 0;
5460 SCTP_STAT_INCR(sctps_recvdatagrams);
5461 #ifdef SCTP_AUDITING_ENABLED
5462 sctp_audit_log(0xE0, 1);
5463 sctp_auditing(0, inp, stcb, net);
5466 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5467 m, iphlen, offset, length, stcb);
5469 /* always clear this before beginning a packet */
5470 stcb->asoc.authenticated = 0;
5471 stcb->asoc.seen_a_sack_this_pkt = 0;
5472 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5473 stcb, stcb->asoc.state);
5475 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5476 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5478 * If we hit here, we had a ref count
5479 * up when the assoc was aborted and the
5480 * timer is clearing out the assoc, we should
5481 * NOT respond to any packet.. its OOTB.
5483 SCTP_TCB_UNLOCK(stcb);
5484 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5489 if (IS_SCTP_CONTROL(ch)) {
5490 /* process the control portion of the SCTP packet */
5491 /* sa_ignore NO_NULL_CHK */
5492 stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch,
5493 inp, stcb, &net, &fwd_tsn_seen, vrf_id, port);
5496 * This covers us if the cookie-echo was there and
5497 * it changes our INP.
5499 inp = stcb->sctp_ep;
5500 if ((net) && (port)) {
5501 if (net->port == 0) {
5502 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr));
5509 * no control chunks, so pre-process DATA chunks (these
5510 * checks are taken care of by control processing)
5514 * if DATA only packet, and auth is required, then punt...
5515 * can't have authenticated without any AUTH (control)
5518 if ((stcb != NULL) &&
5519 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5520 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5521 /* "silently" ignore */
5522 SCTP_STAT_INCR(sctps_recvauthmissing);
5523 SCTP_TCB_UNLOCK(stcb);
5527 /* out of the blue DATA chunk */
5528 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5532 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5533 /* v_tag mismatch! */
5534 SCTP_STAT_INCR(sctps_badvtag);
5535 SCTP_TCB_UNLOCK(stcb);
5542 * no valid TCB for this packet, or we found it's a bad
5543 * packet while processing control, or we're done with this
5544 * packet (done or skip rest of data), so we drop it...
5549 * DATA chunk processing
5551 /* plow through the data chunks while length > offset */
5554 * Rest should be DATA only. Check authentication state if AUTH for
5557 if ((length > offset) &&
5559 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5560 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5561 !stcb->asoc.authenticated) {
5562 /* "silently" ignore */
5563 SCTP_STAT_INCR(sctps_recvauthmissing);
5564 SCTPDBG(SCTP_DEBUG_AUTH1,
5565 "Data chunk requires AUTH, skipped\n");
5568 if (length > offset) {
5572 * First check to make sure our state is correct. We would
5573 * not get here unless we really did have a tag, so we don't
5574 * abort if this happens, just dump the chunk silently.
5576 switch (SCTP_GET_STATE(&stcb->asoc)) {
5577 case SCTP_STATE_COOKIE_ECHOED:
5579 * we consider data with valid tags in this state
5580 * shows us the cookie-ack was lost. Imply it was
5583 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5584 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5585 stcb->asoc.overall_error_count,
5587 SCTP_FROM_SCTP_INPUT,
5590 stcb->asoc.overall_error_count = 0;
5591 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5593 case SCTP_STATE_COOKIE_WAIT:
5595 * We consider OOTB any data sent during asoc setup.
5597 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5599 SCTP_TCB_UNLOCK(stcb);
5601 /* sa_ignore NOTREACHED */
5603 case SCTP_STATE_EMPTY: /* should not happen */
5604 case SCTP_STATE_INUSE: /* should not happen */
5605 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5606 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5608 SCTP_TCB_UNLOCK(stcb);
5610 /* sa_ignore NOTREACHED */
5612 case SCTP_STATE_OPEN:
5613 case SCTP_STATE_SHUTDOWN_SENT:
5616 /* plow through the data chunks while length > offset */
5617 retval = sctp_process_data(mm, iphlen, &offset, length, sh,
5618 inp, stcb, net, &high_tsn);
5621 * The association aborted, NO UNLOCK needed since
5622 * the association is destroyed.
5628 * Anything important needs to have been m_copy'ed in
5632 /* take care of ecn */
5633 if ((stcb->asoc.ecn_allowed == 1) &&
5634 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5635 /* Yep, we need to add a ECNE */
5636 sctp_send_ecn_echo(stcb, net, high_tsn);
5638 if ((data_processed == 0) && (fwd_tsn_seen)) {
5640 uint32_t highest_tsn;
5642 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5643 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5645 highest_tsn = stcb->asoc.highest_tsn_inside_map;
5647 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
5648 stcb->asoc.send_sack = 1;
5649 sctp_sack_check(stcb, was_a_gap, &abort_flag);
5651 /* Again, we aborted so NO UNLOCK needed */
5654 } else if (fwd_tsn_seen) {
5655 stcb->asoc.send_sack = 1;
5657 /* trigger send of any chunks in queue... */
5659 #ifdef SCTP_AUDITING_ENABLED
5660 sctp_audit_log(0xE0, 2);
5661 sctp_auditing(1, inp, stcb, net);
5663 SCTPDBG(SCTP_DEBUG_INPUT1,
5664 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5665 stcb->asoc.peers_rwnd,
5666 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5667 stcb->asoc.total_flight);
5668 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5669 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5670 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5672 if (cnt_ctrl_ready ||
5674 (stcb->asoc.peers_rwnd > 0 ||
5675 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5676 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5677 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5678 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5680 #ifdef SCTP_AUDITING_ENABLED
5681 sctp_audit_log(0xE0, 3);
5682 sctp_auditing(2, inp, stcb, net);
5684 SCTP_TCB_UNLOCK(stcb);
5687 sctp_validate_no_locks(inp);
5694 sctp_print_mbuf_chain(struct mbuf *m)
5696 for (; m; m = SCTP_BUF_NEXT(m)) {
5697 printf("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m));
5698 if (SCTP_BUF_IS_EXTENDED(m))
5699 printf("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m));
5706 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5708 #ifdef SCTP_MBUF_LOGGING
5714 uint32_t vrf_id = 0;
5718 struct sctp_inpcb *inp = NULL;
5719 struct sctp_nets *net;
5720 struct sctp_tcb *stcb = NULL;
5721 struct sctp_chunkhdr *ch;
5722 int refcount_up = 0;
5723 int length, mlen, offset;
5725 #if !defined(SCTP_WITH_NO_CSUM)
5726 uint32_t check, calc_check;
5730 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
5731 SCTP_RELEASE_PKT(i_pak);
5734 mlen = SCTP_HEADER_LEN(i_pak);
5736 m = SCTP_HEADER_TO_CHAIN(i_pak);
5739 SCTP_STAT_INCR(sctps_recvpackets);
5740 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
5743 #ifdef SCTP_MBUF_LOGGING
5744 /* Log in any input mbufs */
5745 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5748 if (SCTP_BUF_IS_EXTENDED(mat)) {
5749 sctp_log_mb(mat, SCTP_MBUF_INPUT);
5751 mat = SCTP_BUF_NEXT(mat);
5755 #ifdef SCTP_PACKET_LOGGING
5756 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
5757 sctp_packet_log(m, mlen);
5760 * Must take out the iphlen, since mlen expects this (only effect lb
5766 * Get IP, SCTP, and first chunk header together in first mbuf.
5768 ip = mtod(m, struct ip *);
5769 offset = iphlen + sizeof(*sh) + sizeof(*ch);
5770 if (SCTP_BUF_LEN(m) < offset) {
5771 if ((m = m_pullup(m, offset)) == 0) {
5772 SCTP_STAT_INCR(sctps_hdrops);
5775 ip = mtod(m, struct ip *);
5777 /* validate mbuf chain length with IP payload length */
5778 if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) {
5779 SCTP_STAT_INCR(sctps_hdrops);
5782 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
5783 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh));
5784 SCTPDBG(SCTP_DEBUG_INPUT1,
5785 "sctp_input() length:%d iphlen:%d\n", mlen, iphlen);
5787 /* SCTP does not allow broadcasts or multicasts */
5788 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
5791 if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) {
5793 * We only look at broadcast if its a front state, All
5794 * others we will not have a tcb for anyway.
5798 /* validate SCTP checksum */
5799 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
5800 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
5802 if_name(m->m_pkthdr.rcvif),
5803 m->m_pkthdr.csum_flags);
5804 #if defined(SCTP_WITH_NO_CSUM)
5805 SCTP_STAT_INCR(sctps_recvnocrc);
5807 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5808 SCTP_STAT_INCR(sctps_recvhwcrc);
5809 goto sctp_skip_csum_4;
5811 check = sh->checksum; /* save incoming checksum */
5812 sh->checksum = 0; /* prepare for calc */
5813 calc_check = sctp_calculate_cksum(m, iphlen);
5814 sh->checksum = check;
5815 SCTP_STAT_INCR(sctps_recvswcrc);
5816 if (calc_check != check) {
5817 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5818 calc_check, check, m, mlen, iphlen);
5820 stcb = sctp_findassociation_addr(m, iphlen,
5821 offset - sizeof(*ch),
5824 if ((net) && (port)) {
5825 if (net->port == 0) {
5826 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr));
5830 if ((net != NULL) && (m->m_flags & M_FLOWID)) {
5831 net->flowid = m->m_pkthdr.flowid;
5836 if ((inp) && (stcb)) {
5837 sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
5838 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5839 } else if ((inp != NULL) && (stcb == NULL)) {
5842 SCTP_STAT_INCR(sctps_badsum);
5843 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5848 /* destination port of 0 is illegal, based on RFC2960. */
5849 if (sh->dest_port == 0) {
5850 SCTP_STAT_INCR(sctps_hdrops);
5854 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
5855 * IP/SCTP/first chunk header...
5857 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
5858 sh, ch, &inp, &net, vrf_id);
5859 if ((net) && (port)) {
5860 if (net->port == 0) {
5861 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr));
5865 if ((net != NULL) && (m->m_flags & M_FLOWID)) {
5866 net->flowid = m->m_pkthdr.flowid;
5871 /* inp's ref-count increased && stcb locked */
5873 struct sctp_init_chunk *init_chk, chunk_buf;
5875 SCTP_STAT_INCR(sctps_noport);
5878 * we use the bandwidth limiting to protect against sending
5879 * too many ABORTS all at once. In this case these count the
5880 * same as an ICMP message.
5882 if (badport_bandlim(0) < 0)
5884 #endif /* ICMP_BANDLIM */
5885 SCTPDBG(SCTP_DEBUG_INPUT1,
5886 "Sending a ABORT from packet entry!\n");
5887 if (ch->chunk_type == SCTP_INITIATION) {
5889 * we do a trick here to get the INIT tag, dig in
5890 * and get the tag from the INIT and put it in the
5893 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
5894 iphlen + sizeof(*sh), sizeof(*init_chk),
5895 (uint8_t *) & chunk_buf);
5896 if (init_chk != NULL)
5897 sh->v_tag = init_chk->init.initiate_tag;
5899 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5900 sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port);
5903 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5906 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
5907 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
5909 } else if (stcb == NULL) {
5914 * I very much doubt any of the IPSEC stuff will work but I have no
5915 * idea, so I will leave it in place.
5917 if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) {
5918 MODULE_GLOBAL(ipsec4stat).in_polvio++;
5919 SCTP_STAT_INCR(sctps_hdrops);
5925 * common chunk processing
5927 length = ip->ip_len + iphlen;
5928 offset -= sizeof(struct sctp_chunkhdr);
5930 ecn_bits = ip->ip_tos;
5932 /* sa_ignore NO_NULL_CHK */
5933 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
5934 inp, stcb, net, ecn_bits, vrf_id, port);
5935 /* inp's ref-count reduced && stcb unlocked */
5939 if ((inp) && (refcount_up)) {
5940 /* reduce ref-count */
5941 SCTP_INP_DECR_REF(inp);
5946 SCTP_TCB_UNLOCK(stcb);
5948 if ((inp) && (refcount_up)) {
5949 /* reduce ref-count */
5950 SCTP_INP_DECR_REF(inp);
5958 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
5959 extern int *sctp_cpuarry;
5964 sctp_input(struct mbuf *m, int off)
5966 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
5971 uint32_t flowid, tag;
5974 if (m->m_flags & M_FLOWID) {
5975 flowid = m->m_pkthdr.flowid;
5978 * No flow id built by lower layers fix it so we
5981 ip = mtod(m, struct ip *);
5982 offset = off + sizeof(*sh);
5983 if (SCTP_BUF_LEN(m) < offset) {
5984 if ((m = m_pullup(m, offset)) == 0) {
5985 SCTP_STAT_INCR(sctps_hdrops);
5988 ip = mtod(m, struct ip *);
5990 sh = (struct sctphdr *)((caddr_t)ip + off);
5991 tag = htonl(sh->v_tag);
5992 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
5993 m->m_pkthdr.flowid = flowid;
5994 m->m_flags |= M_FLOWID;
5996 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
5997 sctp_queue_to_mcore(m, off, cpu_to_use);
6001 sctp_input_with_port(m, off, 0);