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 int *abort_no_unlock, uint32_t vrf_id, uint16_t port)
89 struct sctp_init *init;
92 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
98 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
99 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
100 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
103 *abort_no_unlock = 1;
106 /* validate parameters */
108 if (init->initiate_tag == 0) {
109 /* protocol error... send abort */
110 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
111 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
114 *abort_no_unlock = 1;
117 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
118 /* invalid parameter... send abort */
119 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
120 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
123 *abort_no_unlock = 1;
126 if (init->num_inbound_streams == 0) {
127 /* protocol error... send abort */
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 if (init->num_outbound_streams == 0) {
136 /* protocol error... send abort */
137 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
138 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
141 *abort_no_unlock = 1;
144 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
145 offset + ntohs(cp->ch.chunk_length))) {
146 /* auth parameter(s) error... send abort */
147 sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id, port);
149 *abort_no_unlock = 1;
153 * We are only accepting if we have a socket with positive
156 if ((stcb == NULL) &&
157 ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
158 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
159 (inp->sctp_socket == NULL) ||
160 (inp->sctp_socket->so_qlimit == 0))) {
162 * FIX ME ?? What about TCP model and we have a
163 * match/restart case? Actually no fix is needed. the lookup
164 * will always find the existing assoc so stcb would not be
165 * NULL. It may be questionable to do this since we COULD
166 * just send back the INIT-ACK and hope that the app did
167 * accept()'s by the time the COOKIE was sent. But there is
168 * a price to pay for COOKIE generation and I don't want to
169 * pay it on the chance that the app will actually do some
170 * accepts(). The App just looses and should NOT be in this
173 if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
174 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
178 if ((stcb != NULL) &&
179 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
180 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
181 sctp_send_shutdown_ack(stcb, NULL);
182 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
184 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
185 sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id, port,
186 ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED));
190 SCTP_INP_RUNLOCK(inp);
195 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
199 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
200 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
207 struct sctp_stream_queue_pending *sp;
208 struct sctp_association *asoc;
211 * This function returns the number of streams that have true unsent
212 * data on them. Note that as it looks through it will clean up any
213 * places that have old data that has been sent but left at top of
217 SCTP_TCB_SEND_LOCK(stcb);
218 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
219 /* Check to see if some data queued */
220 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
221 /* sa_ignore FREED_MEMORY */
222 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
226 if ((sp->msg_is_complete) &&
228 (sp->sender_all_done)) {
230 * We are doing differed cleanup. Last time
231 * through when we took all the data the
232 * sender_all_done was not set.
234 if (sp->put_last_out == 0) {
235 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
236 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
242 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
243 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
245 sctp_free_remote_addr(sp->net);
249 sctp_m_freem(sp->data);
252 sctp_free_a_strmoq(stcb, sp, so_locked);
259 SCTP_TCB_SEND_UNLOCK(stcb);
260 return (unsent_data);
264 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
266 struct sctp_init *init;
267 struct sctp_association *asoc;
268 struct sctp_nets *lnet;
273 /* save off parameters */
274 asoc->peer_vtag = ntohl(init->initiate_tag);
275 asoc->peers_rwnd = ntohl(init->a_rwnd);
277 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
279 if (!TAILQ_EMPTY(&asoc->nets)) {
280 /* update any ssthresh's that may have a default */
281 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
282 lnet->ssthresh = asoc->peers_rwnd;
283 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
284 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
288 SCTP_TCB_SEND_LOCK(stcb);
289 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
291 struct sctp_stream_out *outs;
292 struct sctp_stream_queue_pending *sp, *nsp;
293 struct sctp_tmit_chunk *chk, *nchk;
295 /* abandon the upper streams */
296 newcnt = ntohs(init->num_inbound_streams);
297 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
298 if (chk->rec.data.stream_number >= newcnt) {
299 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
300 asoc->send_queue_cnt--;
301 if (chk->data != NULL) {
302 sctp_free_bufspace(stcb, asoc, chk, 1);
303 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
304 SCTP_NOTIFY_DATAGRAM_UNSENT, chk, SCTP_SO_NOT_LOCKED);
306 sctp_m_freem(chk->data);
310 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
311 /* sa_ignore FREED_MEMORY */
315 for (i = newcnt; i < asoc->pre_open_streams; i++) {
316 outs = &asoc->strmout[i];
317 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
318 TAILQ_REMOVE(&outs->outqueue, sp, next);
319 asoc->stream_queue_cnt--;
320 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
321 stcb, SCTP_NOTIFY_DATAGRAM_UNSENT,
322 sp, SCTP_SO_NOT_LOCKED);
324 sctp_m_freem(sp->data);
328 sctp_free_remote_addr(sp->net);
332 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
333 /* sa_ignore FREED_MEMORY */
337 /* cut back the count */
338 asoc->pre_open_streams = newcnt;
340 SCTP_TCB_SEND_UNLOCK(stcb);
341 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
343 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
344 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
345 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
346 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
348 /* This is the next one we expect */
349 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
351 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
352 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
354 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
355 /* open the requested streams */
357 if (asoc->strmin != NULL) {
358 /* Free the old ones */
359 struct sctp_queued_to_read *ctl, *nctl;
361 for (i = 0; i < asoc->streamincnt; i++) {
362 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) {
363 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
364 sctp_free_remote_addr(ctl->whoFrom);
366 sctp_m_freem(ctl->data);
368 sctp_free_a_readq(stcb, ctl);
371 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
373 asoc->streamincnt = ntohs(init->num_outbound_streams);
374 if (asoc->streamincnt > MAX_SCTP_STREAMS) {
375 asoc->streamincnt = MAX_SCTP_STREAMS;
377 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
378 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
379 if (asoc->strmin == NULL) {
380 /* we didn't get memory for the streams! */
381 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
384 for (i = 0; i < asoc->streamincnt; i++) {
385 asoc->strmin[i].stream_no = i;
386 asoc->strmin[i].last_sequence_delivered = 0xffff;
388 * U-stream ranges will be set when the cookie is unpacked.
389 * Or for the INIT sender they are un set (if pr-sctp not
390 * supported) when the INIT-ACK arrives.
392 TAILQ_INIT(&asoc->strmin[i].inqueue);
393 asoc->strmin[i].delivery_started = 0;
396 * load_address_from_init will put the addresses into the
397 * association when the COOKIE is processed or the INIT-ACK is
398 * processed. Both types of COOKIE's existing and new call this
399 * routine. It will remove addresses that are no longer in the
400 * association (for the restarting case where addresses are
401 * removed). Up front when the INIT arrives we will discard it if it
402 * is a restart and new addresses have been added.
404 /* sa_ignore MEMLEAK */
409 * INIT-ACK message processing/consumption returns value < 0 on error
412 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
413 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
414 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id)
416 struct sctp_association *asoc;
418 int retval, abort_flag;
419 uint32_t initack_limit;
420 int nat_friendly = 0;
422 /* First verify that we have no illegal param's */
426 op_err = sctp_arethere_unrecognized_parameters(m,
427 (offset + sizeof(struct sctp_init_chunk)),
428 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
430 /* Send an abort and notify peer */
431 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_CAUSE_PROTOCOL_VIOLATION, op_err, SCTP_SO_NOT_LOCKED);
432 *abort_no_unlock = 1;
436 asoc->peer_supports_nat = (uint8_t) nat_friendly;
437 /* process the peer's parameters in the INIT-ACK */
438 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
442 initack_limit = offset + ntohs(cp->ch.chunk_length);
443 /* load all addresses */
444 if ((retval = sctp_load_addresses_from_init(stcb, m,
445 (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh,
447 /* Huh, we should abort */
448 SCTPDBG(SCTP_DEBUG_INPUT1,
449 "Load addresses from INIT causes an abort %d\n",
451 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
453 *abort_no_unlock = 1;
456 /* if the peer doesn't support asconf, flush the asconf queue */
457 if (asoc->peer_supports_asconf == 0) {
458 struct sctp_asconf_addr *param, *nparam;
460 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
461 TAILQ_REMOVE(&asoc->asconf_queue, param, next);
462 SCTP_FREE(param, SCTP_M_ASC_ADDR);
465 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
466 stcb->asoc.local_hmacs);
468 sctp_queue_op_err(stcb, op_err);
469 /* queuing will steal away the mbuf chain to the out queue */
472 /* extract the cookie and queue it to "echo" it back... */
473 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
474 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
475 stcb->asoc.overall_error_count,
477 SCTP_FROM_SCTP_INPUT,
480 stcb->asoc.overall_error_count = 0;
481 net->error_count = 0;
484 * Cancel the INIT timer, We do this first before queueing the
485 * cookie. We always cancel at the primary to assue that we are
486 * canceling the timer started by the INIT which always goes to the
489 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
490 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
492 /* calculate the RTO */
493 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy,
494 SCTP_RTT_FROM_NON_DATA);
496 retval = sctp_send_cookie_echo(m, offset, stcb, net);
499 * No cookie, we probably should send a op error. But in any
500 * case if there is no cookie in the INIT-ACK, we can
501 * abandon the peer, its broke.
504 /* We abort with an error of missing mandatory param */
506 sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM);
509 * Expand beyond to include the mandatory
512 struct sctp_inv_mandatory_param *mp;
514 SCTP_BUF_LEN(op_err) =
515 sizeof(struct sctp_inv_mandatory_param);
517 struct sctp_inv_mandatory_param *);
518 /* Subtract the reserved param */
520 htons(sizeof(struct sctp_inv_mandatory_param) - 2);
521 mp->num_param = htonl(1);
522 mp->param = htons(SCTP_STATE_COOKIE);
525 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
526 sh, op_err, vrf_id, net->port);
527 *abort_no_unlock = 1;
535 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
536 struct sctp_tcb *stcb, struct sctp_nets *net)
538 struct sockaddr_storage store;
539 struct sctp_nets *r_net, *f_net;
542 uint16_t old_error_counter;
545 struct sockaddr_in *sin;
549 struct sockaddr_in6 *sin6;
553 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
557 memset(&store, 0, sizeof(store));
558 switch (cp->heartbeat.hb_info.addr_family) {
561 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
562 sin = (struct sockaddr_in *)&store;
563 sin->sin_family = cp->heartbeat.hb_info.addr_family;
564 sin->sin_len = cp->heartbeat.hb_info.addr_len;
565 sin->sin_port = stcb->rport;
566 memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address,
567 sizeof(sin->sin_addr));
575 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
576 sin6 = (struct sockaddr_in6 *)&store;
577 sin6->sin6_family = cp->heartbeat.hb_info.addr_family;
578 sin6->sin6_len = cp->heartbeat.hb_info.addr_len;
579 sin6->sin6_port = stcb->rport;
580 memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address,
581 sizeof(sin6->sin6_addr));
590 r_net = sctp_findnet(stcb, (struct sockaddr *)&store);
592 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
595 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
596 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
597 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
599 * If the its a HB and it's random value is correct when can
600 * confirm the destination.
602 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
603 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
604 stcb->asoc.primary_destination = r_net;
605 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
606 f_net = TAILQ_FIRST(&stcb->asoc.nets);
607 if (f_net != r_net) {
609 * first one on the list is NOT the primary
610 * sctp_cmpaddr() is much more efficent if
611 * the primary is the first on the list,
614 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
615 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
619 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
620 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
621 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
622 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
624 old_error_counter = r_net->error_count;
625 r_net->error_count = 0;
626 r_net->hb_responded = 1;
627 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
628 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
629 /* Now lets do a RTO with this */
630 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy,
631 SCTP_RTT_FROM_NON_DATA);
632 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
633 r_net->dest_state |= SCTP_ADDR_REACHABLE;
634 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
635 SCTP_HEARTBEAT_SUCCESS, (void *)r_net, SCTP_SO_NOT_LOCKED);
637 if (r_net->dest_state & SCTP_ADDR_PF) {
638 r_net->dest_state &= ~SCTP_ADDR_PF;
639 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
641 if (old_error_counter > 0) {
642 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
643 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
645 if (r_net == stcb->asoc.primary_destination) {
646 if (stcb->asoc.alternate) {
647 /* release the alternate, primary is good */
648 sctp_free_remote_addr(stcb->asoc.alternate);
649 stcb->asoc.alternate = NULL;
652 /* Mobility adaptation */
654 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
655 SCTP_MOBILITY_BASE) ||
656 sctp_is_mobility_feature_on(stcb->sctp_ep,
657 SCTP_MOBILITY_FASTHANDOFF)) &&
658 sctp_is_mobility_feature_on(stcb->sctp_ep,
659 SCTP_MOBILITY_PRIM_DELETED)) {
661 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7);
662 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
663 SCTP_MOBILITY_FASTHANDOFF)) {
664 sctp_assoc_immediate_retrans(stcb,
665 stcb->asoc.primary_destination);
667 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
668 SCTP_MOBILITY_BASE)) {
669 sctp_move_chunks_from_net(stcb,
670 stcb->asoc.deleted_primary);
672 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
673 stcb->asoc.deleted_primary);
679 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
682 * return 0 means we want you to proceed with the abort non-zero
683 * means no abort processing
685 struct sctpasochead *head;
687 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
688 /* generate a new vtag and send init */
689 LIST_REMOVE(stcb, sctp_asocs);
690 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
691 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
693 * put it in the bucket in the vtag hash of assoc's for the
696 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
697 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
700 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
702 * treat like a case where the cookie expired i.e.: - dump
703 * current cookie. - generate a new vtag. - resend init.
705 /* generate a new vtag and send init */
706 LIST_REMOVE(stcb, sctp_asocs);
707 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
708 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
709 sctp_stop_all_cookie_timers(stcb);
710 sctp_toss_old_cookies(stcb, &stcb->asoc);
711 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
712 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
714 * put it in the bucket in the vtag hash of assoc's for the
717 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
718 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
725 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
726 struct sctp_nets *net)
729 * return 0 means we want you to proceed with the abort non-zero
730 * means no abort processing
732 if (stcb->asoc.peer_supports_auth == 0) {
733 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
736 sctp_asconf_send_nat_state_update(stcb, net);
742 sctp_handle_abort(struct sctp_abort_chunk *cp,
743 struct sctp_tcb *stcb, struct sctp_nets *net)
745 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
751 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
755 len = ntohs(cp->ch.chunk_length);
756 if (len > sizeof(struct sctp_chunkhdr)) {
758 * Need to check the cause codes for our two magic nat
759 * aborts which don't kill the assoc necessarily.
761 struct sctp_abort_chunk *cpnext;
762 struct sctp_missing_nat_state *natc;
767 natc = (struct sctp_missing_nat_state *)cpnext;
768 cause = ntohs(natc->cause);
769 if (cause == SCTP_CAUSE_NAT_COLLIDING_STATE) {
770 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
772 if (sctp_handle_nat_colliding_state(stcb)) {
775 } else if (cause == SCTP_CAUSE_NAT_MISSING_STATE) {
776 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
778 if (sctp_handle_nat_missing_state(stcb, net)) {
783 /* stop any receive timers */
784 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
785 /* notify user of the abort and clean up... */
786 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED);
788 #if defined(SCTP_PANIC_ON_ABORT)
789 printf("stcb:%p state:%d rport:%d net:%p\n",
790 stcb, stcb->asoc.state, stcb->rport, net);
791 if (!(stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
792 panic("Received an ABORT");
794 printf("No panic its in state %x closed\n", stcb->asoc.state);
797 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
798 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
799 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
800 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
802 #ifdef SCTP_ASOCLOG_OF_TSNS
803 sctp_print_out_track_log(stcb);
805 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
806 so = SCTP_INP_SO(stcb->sctp_ep);
807 atomic_add_int(&stcb->asoc.refcnt, 1);
808 SCTP_TCB_UNLOCK(stcb);
809 SCTP_SOCKET_LOCK(so, 1);
811 atomic_subtract_int(&stcb->asoc.refcnt, 1);
813 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
814 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
815 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
816 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
817 SCTP_SOCKET_UNLOCK(so, 1);
819 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
823 sctp_start_net_timers(struct sctp_tcb *stcb)
825 uint32_t cnt_hb_sent;
826 struct sctp_nets *net;
829 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
831 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
832 * If the dest in unconfirmed send a hb as well if under
833 * max_hb_burst have been sent.
835 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
836 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
837 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
838 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
839 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
844 sctp_chunk_output(stcb->sctp_ep, stcb,
845 SCTP_OUTPUT_FROM_COOKIE_ACK,
852 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
853 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
855 struct sctp_association *asoc;
856 int some_on_streamwheel;
858 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
863 SCTPDBG(SCTP_DEBUG_INPUT2,
864 "sctp_handle_shutdown: handling SHUTDOWN\n");
868 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
869 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
872 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
873 /* Shutdown NOT the expected size */
876 sctp_update_acked(stcb, cp, abort_flag);
881 if (asoc->control_pdapi) {
883 * With a normal shutdown we assume the end of last record.
885 SCTP_INP_READ_LOCK(stcb->sctp_ep);
886 asoc->control_pdapi->end_added = 1;
887 asoc->control_pdapi->pdapi_aborted = 1;
888 asoc->control_pdapi = NULL;
889 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
890 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
891 so = SCTP_INP_SO(stcb->sctp_ep);
892 atomic_add_int(&stcb->asoc.refcnt, 1);
893 SCTP_TCB_UNLOCK(stcb);
894 SCTP_SOCKET_LOCK(so, 1);
896 atomic_subtract_int(&stcb->asoc.refcnt, 1);
897 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
898 /* assoc was freed while we were unlocked */
899 SCTP_SOCKET_UNLOCK(so, 1);
903 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
904 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
905 SCTP_SOCKET_UNLOCK(so, 1);
908 /* goto SHUTDOWN_RECEIVED state to block new requests */
909 if (stcb->sctp_socket) {
910 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
911 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
912 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
913 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
914 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
916 * notify upper layer that peer has initiated a
919 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
922 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
925 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
927 * stop the shutdown timer, since we WILL move to
930 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
932 /* Now is there unsent data on a stream somewhere? */
933 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
935 if (!TAILQ_EMPTY(&asoc->send_queue) ||
936 !TAILQ_EMPTY(&asoc->sent_queue) ||
937 some_on_streamwheel) {
938 /* By returning we will push more data out */
941 /* no outstanding data to send, so move on... */
942 /* send SHUTDOWN-ACK */
943 sctp_send_shutdown_ack(stcb, net);
944 /* move to SHUTDOWN-ACK-SENT state */
945 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
946 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
947 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
949 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
950 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
951 sctp_stop_timers_for_shutdown(stcb);
952 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
958 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
959 struct sctp_tcb *stcb,
960 struct sctp_nets *net)
962 struct sctp_association *asoc;
964 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
967 so = SCTP_INP_SO(stcb->sctp_ep);
969 SCTPDBG(SCTP_DEBUG_INPUT2,
970 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
975 /* process according to association state */
976 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
977 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
978 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
979 sctp_send_shutdown_complete(stcb, net, 1);
980 SCTP_TCB_UNLOCK(stcb);
983 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
984 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
985 /* unexpected SHUTDOWN-ACK... so ignore... */
986 SCTP_TCB_UNLOCK(stcb);
989 if (asoc->control_pdapi) {
991 * With a normal shutdown we assume the end of last record.
993 SCTP_INP_READ_LOCK(stcb->sctp_ep);
994 asoc->control_pdapi->end_added = 1;
995 asoc->control_pdapi->pdapi_aborted = 1;
996 asoc->control_pdapi = NULL;
997 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
998 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
999 atomic_add_int(&stcb->asoc.refcnt, 1);
1000 SCTP_TCB_UNLOCK(stcb);
1001 SCTP_SOCKET_LOCK(so, 1);
1002 SCTP_TCB_LOCK(stcb);
1003 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1004 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1005 /* assoc was freed while we were unlocked */
1006 SCTP_SOCKET_UNLOCK(so, 1);
1010 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1011 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1012 SCTP_SOCKET_UNLOCK(so, 1);
1015 /* are the queues empty? */
1016 if (!TAILQ_EMPTY(&asoc->send_queue) ||
1017 !TAILQ_EMPTY(&asoc->sent_queue) ||
1018 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
1019 sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED);
1021 /* stop the timer */
1022 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
1023 /* send SHUTDOWN-COMPLETE */
1024 sctp_send_shutdown_complete(stcb, net, 0);
1025 /* notify upper layer protocol */
1026 if (stcb->sctp_socket) {
1027 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1028 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1029 stcb->sctp_socket->so_snd.sb_cc = 0;
1031 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1033 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1034 /* free the TCB but first save off the ep */
1035 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1036 atomic_add_int(&stcb->asoc.refcnt, 1);
1037 SCTP_TCB_UNLOCK(stcb);
1038 SCTP_SOCKET_LOCK(so, 1);
1039 SCTP_TCB_LOCK(stcb);
1040 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1042 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1043 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1044 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1045 SCTP_SOCKET_UNLOCK(so, 1);
1050 * Skip past the param header and then we will find the chunk that caused the
1051 * problem. There are two possiblities ASCONF or FWD-TSN other than that and
1052 * our peer must be broken.
1055 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1056 struct sctp_nets *net)
1058 struct sctp_chunkhdr *chk;
1060 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1061 switch (chk->chunk_type) {
1062 case SCTP_ASCONF_ACK:
1064 sctp_asconf_cleanup(stcb, net);
1066 case SCTP_FORWARD_CUM_TSN:
1067 stcb->asoc.peer_supports_prsctp = 0;
1070 SCTPDBG(SCTP_DEBUG_INPUT2,
1071 "Peer does not support chunk type %d(%x)??\n",
1072 chk->chunk_type, (uint32_t) chk->chunk_type);
1078 * Skip past the param header and then we will find the param that caused the
1079 * problem. There are a number of param's in a ASCONF OR the prsctp param
1080 * these will turn of specific features.
1083 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1085 struct sctp_paramhdr *pbad;
1088 switch (ntohs(pbad->param_type)) {
1090 case SCTP_PRSCTP_SUPPORTED:
1091 stcb->asoc.peer_supports_prsctp = 0;
1093 case SCTP_SUPPORTED_CHUNK_EXT:
1095 /* draft-ietf-tsvwg-addip-sctp */
1096 case SCTP_HAS_NAT_SUPPORT:
1097 stcb->asoc.peer_supports_nat = 0;
1099 case SCTP_ADD_IP_ADDRESS:
1100 case SCTP_DEL_IP_ADDRESS:
1101 case SCTP_SET_PRIM_ADDR:
1102 stcb->asoc.peer_supports_asconf = 0;
1104 case SCTP_SUCCESS_REPORT:
1105 case SCTP_ERROR_CAUSE_IND:
1106 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1107 SCTPDBG(SCTP_DEBUG_INPUT2,
1108 "Turning off ASCONF to this strange peer\n");
1109 stcb->asoc.peer_supports_asconf = 0;
1112 SCTPDBG(SCTP_DEBUG_INPUT2,
1113 "Peer does not support param type %d(%x)??\n",
1114 pbad->param_type, (uint32_t) pbad->param_type);
1120 sctp_handle_error(struct sctp_chunkhdr *ch,
1121 struct sctp_tcb *stcb, struct sctp_nets *net)
1124 struct sctp_paramhdr *phdr;
1125 uint16_t error_type;
1127 struct sctp_association *asoc;
1130 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1135 /* parse through all of the errors and process */
1137 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1138 sizeof(struct sctp_chunkhdr));
1139 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1140 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1141 /* Process an Error Cause */
1142 error_type = ntohs(phdr->param_type);
1143 error_len = ntohs(phdr->param_length);
1144 if ((error_len > chklen) || (error_len == 0)) {
1145 /* invalid param length for this param */
1146 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1150 switch (error_type) {
1151 case SCTP_CAUSE_INVALID_STREAM:
1152 case SCTP_CAUSE_MISSING_PARAM:
1153 case SCTP_CAUSE_INVALID_PARAM:
1154 case SCTP_CAUSE_NO_USER_DATA:
1155 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1158 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1159 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1161 if (sctp_handle_nat_colliding_state(stcb)) {
1165 case SCTP_CAUSE_NAT_MISSING_STATE:
1166 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1168 if (sctp_handle_nat_missing_state(stcb, net)) {
1172 case SCTP_CAUSE_STALE_COOKIE:
1174 * We only act if we have echoed a cookie and are
1177 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1180 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1181 /* Save the time doubled */
1182 asoc->cookie_preserve_req = ntohl(*p) << 1;
1183 asoc->stale_cookie_count++;
1184 if (asoc->stale_cookie_count >
1185 asoc->max_init_times) {
1186 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED);
1187 /* now free the asoc */
1188 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1189 so = SCTP_INP_SO(stcb->sctp_ep);
1190 atomic_add_int(&stcb->asoc.refcnt, 1);
1191 SCTP_TCB_UNLOCK(stcb);
1192 SCTP_SOCKET_LOCK(so, 1);
1193 SCTP_TCB_LOCK(stcb);
1194 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1196 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1197 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1198 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1199 SCTP_SOCKET_UNLOCK(so, 1);
1203 /* blast back to INIT state */
1204 sctp_toss_old_cookies(stcb, &stcb->asoc);
1205 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1206 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1207 sctp_stop_all_cookie_timers(stcb);
1208 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1211 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1213 * Nothing we can do here, we don't do hostname
1214 * addresses so if the peer does not like my IPv6
1215 * (or IPv4 for that matter) it does not matter. If
1216 * they don't support that type of address, they can
1217 * NOT possibly get that packet type... i.e. with no
1218 * IPv6 you can't recieve a IPv6 packet. so we can
1219 * safely ignore this one. If we ever added support
1220 * for HOSTNAME Addresses, then we would need to do
1224 case SCTP_CAUSE_UNRECOG_CHUNK:
1225 sctp_process_unrecog_chunk(stcb, phdr, net);
1227 case SCTP_CAUSE_UNRECOG_PARAM:
1228 sctp_process_unrecog_param(stcb, phdr);
1230 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1232 * We ignore this since the timer will drive out a
1233 * new cookie anyway and there timer will drive us
1234 * to send a SHUTDOWN_COMPLETE. We can't send one
1235 * here since we don't have their tag.
1238 case SCTP_CAUSE_DELETING_LAST_ADDR:
1239 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1240 case SCTP_CAUSE_DELETING_SRC_ADDR:
1242 * We should NOT get these here, but in a
1245 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1248 case SCTP_CAUSE_OUT_OF_RESC:
1250 * And what, pray tell do we do with the fact that
1251 * the peer is out of resources? Not really sure we
1252 * could do anything but abort. I suspect this
1253 * should have came WITH an abort instead of in a
1258 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1262 adjust = SCTP_SIZE32(error_len);
1264 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1270 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1271 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1272 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id)
1274 struct sctp_init_ack *init_ack;
1275 struct mbuf *op_err;
1277 SCTPDBG(SCTP_DEBUG_INPUT2,
1278 "sctp_handle_init_ack: handling INIT-ACK\n");
1281 SCTPDBG(SCTP_DEBUG_INPUT2,
1282 "sctp_handle_init_ack: TCB is null\n");
1285 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1286 /* Invalid length */
1287 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1288 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1289 op_err, 0, net->port);
1290 *abort_no_unlock = 1;
1293 init_ack = &cp->init;
1294 /* validate parameters */
1295 if (init_ack->initiate_tag == 0) {
1296 /* protocol error... send an abort */
1297 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1298 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1299 op_err, 0, net->port);
1300 *abort_no_unlock = 1;
1303 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1304 /* protocol error... send an abort */
1305 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1306 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1307 op_err, 0, net->port);
1308 *abort_no_unlock = 1;
1311 if (init_ack->num_inbound_streams == 0) {
1312 /* protocol error... send an abort */
1313 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1314 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1315 op_err, 0, net->port);
1316 *abort_no_unlock = 1;
1319 if (init_ack->num_outbound_streams == 0) {
1320 /* protocol error... send an abort */
1321 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1322 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1323 op_err, 0, net->port);
1324 *abort_no_unlock = 1;
1327 /* process according to association state... */
1328 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1329 case SCTP_STATE_COOKIE_WAIT:
1330 /* this is the expected state for this chunk */
1331 /* process the INIT-ACK parameters */
1332 if (stcb->asoc.primary_destination->dest_state &
1333 SCTP_ADDR_UNCONFIRMED) {
1335 * The primary is where we sent the INIT, we can
1336 * always consider it confirmed when the INIT-ACK is
1337 * returned. Do this before we load addresses
1340 stcb->asoc.primary_destination->dest_state &=
1341 ~SCTP_ADDR_UNCONFIRMED;
1342 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1343 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1345 if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb,
1346 net, abort_no_unlock, vrf_id) < 0) {
1347 /* error in parsing parameters */
1350 /* update our state */
1351 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1352 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1354 /* reset the RTO calc */
1355 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1356 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1357 stcb->asoc.overall_error_count,
1359 SCTP_FROM_SCTP_INPUT,
1362 stcb->asoc.overall_error_count = 0;
1363 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1365 * collapse the init timer back in case of a exponential
1368 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1371 * the send at the end of the inbound data processing will
1372 * cause the cookie to be sent
1375 case SCTP_STATE_SHUTDOWN_SENT:
1376 /* incorrect state... discard */
1378 case SCTP_STATE_COOKIE_ECHOED:
1379 /* incorrect state... discard */
1381 case SCTP_STATE_OPEN:
1382 /* incorrect state... discard */
1384 case SCTP_STATE_EMPTY:
1385 case SCTP_STATE_INUSE:
1387 /* incorrect state... discard */
1391 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1395 static struct sctp_tcb *
1396 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1397 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1398 struct sctp_inpcb *inp, struct sctp_nets **netp,
1399 struct sockaddr *init_src, int *notification,
1400 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1401 uint32_t vrf_id, uint16_t port);
1405 * handle a state cookie for an existing association m: input packet mbuf
1406 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1407 * "split" mbuf and the cookie signature does not exist offset: offset into
1408 * mbuf to the cookie-echo chunk
1410 static struct sctp_tcb *
1411 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1412 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1413 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1414 struct sockaddr *init_src, int *notification,
1415 uint32_t vrf_id, int auth_skipped, uint32_t auth_offset, uint32_t auth_len, uint16_t port)
1417 struct sctp_association *asoc;
1418 struct sctp_init_chunk *init_cp, init_buf;
1419 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1420 struct sctp_nets *net;
1421 struct mbuf *op_err;
1422 struct sctp_paramhdr *ph;
1423 int init_offset, initack_offset, i;
1429 /* I know that the TCB is non-NULL from the caller */
1431 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1432 if (asoc->cookie_how[how_indx] == 0)
1435 if (how_indx < sizeof(asoc->cookie_how)) {
1436 asoc->cookie_how[how_indx] = 1;
1438 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1439 /* SHUTDOWN came in after sending INIT-ACK */
1440 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1441 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1442 0, M_DONTWAIT, 1, MT_DATA);
1443 if (op_err == NULL) {
1448 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1449 ph = mtod(op_err, struct sctp_paramhdr *);
1450 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
1451 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1452 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag,
1454 if (how_indx < sizeof(asoc->cookie_how))
1455 asoc->cookie_how[how_indx] = 2;
1459 * find and validate the INIT chunk in the cookie (peer's info) the
1460 * INIT should start after the cookie-echo header struct (chunk
1461 * header, state cookie header struct)
1463 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1465 init_cp = (struct sctp_init_chunk *)
1466 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1467 (uint8_t *) & init_buf);
1468 if (init_cp == NULL) {
1469 /* could not pull a INIT chunk in cookie */
1472 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1476 * find and validate the INIT-ACK chunk in the cookie (my info) the
1477 * INIT-ACK follows the INIT chunk
1479 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1480 initack_cp = (struct sctp_init_ack_chunk *)
1481 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1482 (uint8_t *) & initack_buf);
1483 if (initack_cp == NULL) {
1484 /* could not pull INIT-ACK chunk in cookie */
1487 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1490 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1491 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1493 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1494 * to get into the OPEN state
1496 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1498 * Opps, this means that we somehow generated two vtag's
1499 * the same. I.e. we did:
1501 * <---INIT(tag=a)------
1502 * ----INIT-ACK(tag=t)-->
1503 * ----INIT(tag=t)------> *1
1504 * <---INIT-ACK(tag=a)---
1505 * <----CE(tag=t)------------- *2
1507 * At point *1 we should be generating a different
1508 * tag t'. Which means we would throw away the CE and send
1509 * ours instead. Basically this is case C (throw away side).
1511 if (how_indx < sizeof(asoc->cookie_how))
1512 asoc->cookie_how[how_indx] = 17;
1516 switch SCTP_GET_STATE
1518 case SCTP_STATE_COOKIE_WAIT:
1519 case SCTP_STATE_COOKIE_ECHOED:
1521 * INIT was sent but got a COOKIE_ECHO with the
1522 * correct tags... just accept it...but we must
1523 * process the init so that we can make sure we have
1524 * the right seq no's.
1526 /* First we must process the INIT !! */
1527 retval = sctp_process_init(init_cp, stcb);
1529 if (how_indx < sizeof(asoc->cookie_how))
1530 asoc->cookie_how[how_indx] = 3;
1533 /* we have already processed the INIT so no problem */
1534 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1535 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1536 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1537 /* update current state */
1538 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1539 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1541 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1543 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1544 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1545 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1546 stcb->sctp_ep, stcb, asoc->primary_destination);
1548 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1549 sctp_stop_all_cookie_timers(stcb);
1550 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1551 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1552 (inp->sctp_socket->so_qlimit == 0)
1554 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1559 * Here is where collision would go if we
1560 * did a connect() and instead got a
1561 * init/init-ack/cookie done before the
1562 * init-ack came back..
1564 stcb->sctp_ep->sctp_flags |=
1565 SCTP_PCB_FLAGS_CONNECTED;
1566 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1567 so = SCTP_INP_SO(stcb->sctp_ep);
1568 atomic_add_int(&stcb->asoc.refcnt, 1);
1569 SCTP_TCB_UNLOCK(stcb);
1570 SCTP_SOCKET_LOCK(so, 1);
1571 SCTP_TCB_LOCK(stcb);
1572 atomic_add_int(&stcb->asoc.refcnt, -1);
1573 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1574 SCTP_SOCKET_UNLOCK(so, 1);
1578 soisconnected(stcb->sctp_socket);
1579 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1580 SCTP_SOCKET_UNLOCK(so, 1);
1583 /* notify upper layer */
1584 *notification = SCTP_NOTIFY_ASSOC_UP;
1586 * since we did not send a HB make sure we don't
1589 net->hb_responded = 1;
1590 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1591 &cookie->time_entered,
1592 sctp_align_unsafe_makecopy,
1593 SCTP_RTT_FROM_NON_DATA);
1595 if (stcb->asoc.sctp_autoclose_ticks &&
1596 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1597 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1603 * we're in the OPEN state (or beyond), so peer must
1604 * have simply lost the COOKIE-ACK
1608 sctp_stop_all_cookie_timers(stcb);
1610 * We ignore the return code here.. not sure if we should
1611 * somehow abort.. but we do have an existing asoc. This
1612 * really should not fail.
1614 if (sctp_load_addresses_from_init(stcb, m,
1615 init_offset + sizeof(struct sctp_init_chunk),
1616 initack_offset, sh, init_src)) {
1617 if (how_indx < sizeof(asoc->cookie_how))
1618 asoc->cookie_how[how_indx] = 4;
1621 /* respond with a COOKIE-ACK */
1622 sctp_toss_old_cookies(stcb, asoc);
1623 sctp_send_cookie_ack(stcb);
1624 if (how_indx < sizeof(asoc->cookie_how))
1625 asoc->cookie_how[how_indx] = 5;
1628 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1629 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1630 cookie->tie_tag_my_vtag == 0 &&
1631 cookie->tie_tag_peer_vtag == 0) {
1633 * case C in Section 5.2.4 Table 2: XMOO silently discard
1635 if (how_indx < sizeof(asoc->cookie_how))
1636 asoc->cookie_how[how_indx] = 6;
1640 * If nat support, and the below and stcb is established, send back
1641 * a ABORT(colliding state) if we are established.
1643 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1644 (asoc->peer_supports_nat) &&
1645 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1646 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1647 (asoc->peer_vtag == 0)))) {
1649 * Special case - Peer's support nat. We may have two init's
1650 * that we gave out the same tag on since one was not
1651 * established.. i.e. we get INIT from host-1 behind the nat
1652 * and we respond tag-a, we get a INIT from host-2 behind
1653 * the nat and we get tag-a again. Then we bring up host-1
1654 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1655 * Now we have colliding state. We must send an abort here
1656 * with colliding state indication.
1658 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1659 0, M_DONTWAIT, 1, MT_DATA);
1660 if (op_err == NULL) {
1664 /* pre-reserve some space */
1666 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
1668 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
1670 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
1671 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1673 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1674 ph = mtod(op_err, struct sctp_paramhdr *);
1675 ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE);
1676 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1677 sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port);
1680 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1681 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1682 (asoc->peer_vtag == 0))) {
1684 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1685 * should be ok, re-accept peer info
1687 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1689 * Extension of case C. If we hit this, then the
1690 * random number generator returned the same vtag
1691 * when we first sent our INIT-ACK and when we later
1692 * sent our INIT. The side with the seq numbers that
1693 * are different will be the one that normnally
1694 * would have hit case C. This in effect "extends"
1695 * our vtags in this collision case to be 64 bits.
1696 * The same collision could occur aka you get both
1697 * vtag and seq number the same twice in a row.. but
1698 * is much less likely. If it did happen then we
1699 * would proceed through and bring up the assoc.. we
1700 * may end up with the wrong stream setup however..
1701 * which would be bad.. but there is no way to
1702 * tell.. until we send on a stream that does not
1705 if (how_indx < sizeof(asoc->cookie_how))
1706 asoc->cookie_how[how_indx] = 7;
1710 if (how_indx < sizeof(asoc->cookie_how))
1711 asoc->cookie_how[how_indx] = 8;
1712 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1713 sctp_stop_all_cookie_timers(stcb);
1715 * since we did not send a HB make sure we don't double
1718 net->hb_responded = 1;
1719 if (stcb->asoc.sctp_autoclose_ticks &&
1720 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1721 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1724 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1725 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1727 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1729 * Ok the peer probably discarded our data (if we
1730 * echoed a cookie+data). So anything on the
1731 * sent_queue should be marked for retransmit, we
1732 * may not get something to kick us so it COULD
1733 * still take a timeout to move these.. but it can't
1734 * hurt to mark them.
1736 struct sctp_tmit_chunk *chk;
1738 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1739 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1740 chk->sent = SCTP_DATAGRAM_RESEND;
1741 sctp_flight_size_decrease(chk);
1742 sctp_total_flight_decrease(stcb, chk);
1743 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1749 /* process the INIT info (peer's info) */
1750 retval = sctp_process_init(init_cp, stcb);
1752 if (how_indx < sizeof(asoc->cookie_how))
1753 asoc->cookie_how[how_indx] = 9;
1756 if (sctp_load_addresses_from_init(stcb, m,
1757 init_offset + sizeof(struct sctp_init_chunk),
1758 initack_offset, sh, init_src)) {
1759 if (how_indx < sizeof(asoc->cookie_how))
1760 asoc->cookie_how[how_indx] = 10;
1763 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1764 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1765 *notification = SCTP_NOTIFY_ASSOC_UP;
1767 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1768 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1769 (inp->sctp_socket->so_qlimit == 0)) {
1770 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1774 stcb->sctp_ep->sctp_flags |=
1775 SCTP_PCB_FLAGS_CONNECTED;
1776 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1777 so = SCTP_INP_SO(stcb->sctp_ep);
1778 atomic_add_int(&stcb->asoc.refcnt, 1);
1779 SCTP_TCB_UNLOCK(stcb);
1780 SCTP_SOCKET_LOCK(so, 1);
1781 SCTP_TCB_LOCK(stcb);
1782 atomic_add_int(&stcb->asoc.refcnt, -1);
1783 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1784 SCTP_SOCKET_UNLOCK(so, 1);
1788 soisconnected(stcb->sctp_socket);
1789 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1790 SCTP_SOCKET_UNLOCK(so, 1);
1793 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1794 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1796 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1797 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1798 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1799 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1801 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1803 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1804 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1805 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1806 stcb->sctp_ep, stcb, asoc->primary_destination);
1808 sctp_stop_all_cookie_timers(stcb);
1809 sctp_toss_old_cookies(stcb, asoc);
1810 sctp_send_cookie_ack(stcb);
1813 * only if we have retrans set do we do this. What
1814 * this call does is get only the COOKIE-ACK out and
1815 * then when we return the normal call to
1816 * sctp_chunk_output will get the retrans out behind
1819 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1821 if (how_indx < sizeof(asoc->cookie_how))
1822 asoc->cookie_how[how_indx] = 11;
1826 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1827 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1828 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1829 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1830 cookie->tie_tag_peer_vtag != 0) {
1831 struct sctpasochead *head;
1833 if (asoc->peer_supports_nat) {
1835 * This is a gross gross hack. just call the
1836 * cookie_new code since we are allowing a duplicate
1837 * association. I hope this works...
1839 return (sctp_process_cookie_new(m, iphlen, offset, sh, cookie, cookie_len,
1840 inp, netp, init_src, notification,
1841 auth_skipped, auth_offset, auth_len,
1845 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1848 if (how_indx < sizeof(asoc->cookie_how))
1849 asoc->cookie_how[how_indx] = 12;
1850 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1851 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1853 /* notify upper layer */
1854 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1855 atomic_add_int(&stcb->asoc.refcnt, 1);
1856 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1857 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1858 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1859 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1861 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1862 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1863 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1864 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1866 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1867 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1868 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1869 stcb->sctp_ep, stcb, asoc->primary_destination);
1871 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1872 /* move to OPEN state, if not in SHUTDOWN_SENT */
1873 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1875 asoc->pre_open_streams =
1876 ntohs(initack_cp->init.num_outbound_streams);
1877 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1878 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1879 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1881 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1883 asoc->str_reset_seq_in = asoc->init_seq_number;
1885 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1886 if (asoc->mapping_array) {
1887 memset(asoc->mapping_array, 0,
1888 asoc->mapping_array_size);
1890 if (asoc->nr_mapping_array) {
1891 memset(asoc->nr_mapping_array, 0,
1892 asoc->mapping_array_size);
1894 SCTP_TCB_UNLOCK(stcb);
1895 SCTP_INP_INFO_WLOCK();
1896 SCTP_INP_WLOCK(stcb->sctp_ep);
1897 SCTP_TCB_LOCK(stcb);
1898 atomic_add_int(&stcb->asoc.refcnt, -1);
1899 /* send up all the data */
1900 SCTP_TCB_SEND_LOCK(stcb);
1902 sctp_report_all_outbound(stcb, 1, SCTP_SO_NOT_LOCKED);
1903 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1904 stcb->asoc.strmout[i].stream_no = i;
1905 stcb->asoc.strmout[i].next_sequence_sent = 0;
1906 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1908 /* process the INIT-ACK info (my info) */
1909 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1910 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1912 /* pull from vtag hash */
1913 LIST_REMOVE(stcb, sctp_asocs);
1914 /* re-insert to new vtag position */
1915 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1916 SCTP_BASE_INFO(hashasocmark))];
1918 * put it in the bucket in the vtag hash of assoc's for the
1921 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1923 /* process the INIT info (peer's info) */
1924 SCTP_TCB_SEND_UNLOCK(stcb);
1925 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1926 SCTP_INP_INFO_WUNLOCK();
1928 retval = sctp_process_init(init_cp, stcb);
1930 if (how_indx < sizeof(asoc->cookie_how))
1931 asoc->cookie_how[how_indx] = 13;
1936 * since we did not send a HB make sure we don't double
1939 net->hb_responded = 1;
1941 if (sctp_load_addresses_from_init(stcb, m,
1942 init_offset + sizeof(struct sctp_init_chunk),
1943 initack_offset, sh, init_src)) {
1944 if (how_indx < sizeof(asoc->cookie_how))
1945 asoc->cookie_how[how_indx] = 14;
1949 /* respond with a COOKIE-ACK */
1950 sctp_stop_all_cookie_timers(stcb);
1951 sctp_toss_old_cookies(stcb, asoc);
1952 sctp_send_cookie_ack(stcb);
1953 if (how_indx < sizeof(asoc->cookie_how))
1954 asoc->cookie_how[how_indx] = 15;
1958 if (how_indx < sizeof(asoc->cookie_how))
1959 asoc->cookie_how[how_indx] = 16;
1960 /* all other cases... */
1966 * handle a state cookie for a new association m: input packet mbuf chain--
1967 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
1968 * and the cookie signature does not exist offset: offset into mbuf to the
1969 * cookie-echo chunk length: length of the cookie chunk to: where the init
1970 * was from returns a new TCB
1973 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1974 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1975 struct sctp_inpcb *inp, struct sctp_nets **netp,
1976 struct sockaddr *init_src, int *notification,
1977 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1978 uint32_t vrf_id, uint16_t port)
1980 struct sctp_tcb *stcb;
1981 struct sctp_init_chunk *init_cp, init_buf;
1982 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1983 struct sockaddr_storage sa_store;
1984 struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
1985 struct sctp_association *asoc;
1986 int init_offset, initack_offset, initack_limit;
1989 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
1992 struct sockaddr_in *sin;
1996 struct sockaddr_in6 *sin6;
1999 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2002 so = SCTP_INP_SO(inp);
2006 * find and validate the INIT chunk in the cookie (peer's info) the
2007 * INIT should start after the cookie-echo header struct (chunk
2008 * header, state cookie header struct)
2010 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2011 init_cp = (struct sctp_init_chunk *)
2012 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2013 (uint8_t *) & init_buf);
2014 if (init_cp == NULL) {
2015 /* could not pull a INIT chunk in cookie */
2016 SCTPDBG(SCTP_DEBUG_INPUT1,
2017 "process_cookie_new: could not pull INIT chunk hdr\n");
2020 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2021 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2024 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2026 * find and validate the INIT-ACK chunk in the cookie (my info) the
2027 * INIT-ACK follows the INIT chunk
2029 initack_cp = (struct sctp_init_ack_chunk *)
2030 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2031 (uint8_t *) & initack_buf);
2032 if (initack_cp == NULL) {
2033 /* could not pull INIT-ACK chunk in cookie */
2034 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2037 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2041 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2042 * "initack_limit" value. This is because the chk_length field
2043 * includes the length of the cookie, but the cookie is omitted when
2044 * the INIT and INIT_ACK are tacked onto the cookie...
2046 initack_limit = offset + cookie_len;
2049 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2054 * Here we do a trick, we set in NULL for the proc/thread argument.
2055 * We do this since in effect we only use the p argument when the
2056 * socket is unbound and we must do an implicit bind. Since we are
2057 * getting a cookie, we cannot be unbound.
2059 stcb = sctp_aloc_assoc(inp, init_src, &error,
2060 ntohl(initack_cp->init.initiate_tag), vrf_id,
2061 (struct thread *)NULL
2064 struct mbuf *op_err;
2066 /* memory problem? */
2067 SCTPDBG(SCTP_DEBUG_INPUT1,
2068 "process_cookie_new: no room for another TCB!\n");
2069 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2071 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2072 sh, op_err, vrf_id, port);
2075 /* get the correct sctp_nets */
2077 *netp = sctp_findnet(stcb, init_src);
2080 /* get scope variables out of cookie */
2081 asoc->ipv4_local_scope = cookie->ipv4_scope;
2082 asoc->site_scope = cookie->site_scope;
2083 asoc->local_scope = cookie->local_scope;
2084 asoc->loopback_scope = cookie->loopback_scope;
2086 if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2087 (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2088 struct mbuf *op_err;
2091 * Houston we have a problem. The EP changed while the
2092 * cookie was in flight. Only recourse is to abort the
2095 atomic_add_int(&stcb->asoc.refcnt, 1);
2096 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2097 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2098 sh, op_err, vrf_id, port);
2099 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2100 SCTP_TCB_UNLOCK(stcb);
2101 SCTP_SOCKET_LOCK(so, 1);
2102 SCTP_TCB_LOCK(stcb);
2104 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2105 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2106 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2107 SCTP_SOCKET_UNLOCK(so, 1);
2109 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2112 /* process the INIT-ACK info (my info) */
2113 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2114 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2115 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2116 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2117 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2118 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2119 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2120 asoc->str_reset_seq_in = asoc->init_seq_number;
2122 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2124 /* process the INIT info (peer's info) */
2126 retval = sctp_process_init(init_cp, stcb);
2130 atomic_add_int(&stcb->asoc.refcnt, 1);
2131 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2132 SCTP_TCB_UNLOCK(stcb);
2133 SCTP_SOCKET_LOCK(so, 1);
2134 SCTP_TCB_LOCK(stcb);
2136 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2137 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2138 SCTP_SOCKET_UNLOCK(so, 1);
2140 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2143 /* load all addresses */
2144 if (sctp_load_addresses_from_init(stcb, m,
2145 init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh,
2147 atomic_add_int(&stcb->asoc.refcnt, 1);
2148 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2149 SCTP_TCB_UNLOCK(stcb);
2150 SCTP_SOCKET_LOCK(so, 1);
2151 SCTP_TCB_LOCK(stcb);
2153 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
2154 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2155 SCTP_SOCKET_UNLOCK(so, 1);
2157 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2161 * verify any preceding AUTH chunk that was skipped
2163 /* pull the local authentication parameters from the cookie/init-ack */
2164 sctp_auth_get_cookie_params(stcb, m,
2165 initack_offset + sizeof(struct sctp_init_ack_chunk),
2166 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2168 struct sctp_auth_chunk *auth;
2170 auth = (struct sctp_auth_chunk *)
2171 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2172 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2173 /* auth HMAC failed, dump the assoc and packet */
2174 SCTPDBG(SCTP_DEBUG_AUTH1,
2175 "COOKIE-ECHO: AUTH failed\n");
2176 atomic_add_int(&stcb->asoc.refcnt, 1);
2177 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2178 SCTP_TCB_UNLOCK(stcb);
2179 SCTP_SOCKET_LOCK(so, 1);
2180 SCTP_TCB_LOCK(stcb);
2182 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2183 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2184 SCTP_SOCKET_UNLOCK(so, 1);
2186 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2189 /* remaining chunks checked... good to go */
2190 stcb->asoc.authenticated = 1;
2193 /* update current state */
2194 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2195 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2196 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2197 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2198 stcb->sctp_ep, stcb, asoc->primary_destination);
2200 sctp_stop_all_cookie_timers(stcb);
2201 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2202 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2205 * if we're doing ASCONFs, check to see if we have any new local
2206 * addresses that need to get added to the peer (eg. addresses
2207 * changed while cookie echo in flight). This needs to be done
2208 * after we go to the OPEN state to do the correct asconf
2209 * processing. else, make sure we have the correct addresses in our
2213 /* warning, we re-use sin, sin6, sa_store here! */
2214 /* pull in local_address (our "from" address) */
2215 switch (cookie->laddr_type) {
2217 case SCTP_IPV4_ADDRESS:
2218 /* source addr is IPv4 */
2219 sin = (struct sockaddr_in *)initack_src;
2220 memset(sin, 0, sizeof(*sin));
2221 sin->sin_family = AF_INET;
2222 sin->sin_len = sizeof(struct sockaddr_in);
2223 sin->sin_addr.s_addr = cookie->laddress[0];
2227 case SCTP_IPV6_ADDRESS:
2228 /* source addr is IPv6 */
2229 sin6 = (struct sockaddr_in6 *)initack_src;
2230 memset(sin6, 0, sizeof(*sin6));
2231 sin6->sin6_family = AF_INET6;
2232 sin6->sin6_len = sizeof(struct sockaddr_in6);
2233 sin6->sin6_scope_id = cookie->scope_id;
2234 memcpy(&sin6->sin6_addr, cookie->laddress,
2235 sizeof(sin6->sin6_addr));
2239 atomic_add_int(&stcb->asoc.refcnt, 1);
2240 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2241 SCTP_TCB_UNLOCK(stcb);
2242 SCTP_SOCKET_LOCK(so, 1);
2243 SCTP_TCB_LOCK(stcb);
2245 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2246 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2247 SCTP_SOCKET_UNLOCK(so, 1);
2249 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2253 /* set up to notify upper layer */
2254 *notification = SCTP_NOTIFY_ASSOC_UP;
2255 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2256 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2257 (inp->sctp_socket->so_qlimit == 0)) {
2259 * This is an endpoint that called connect() how it got a
2260 * cookie that is NEW is a bit of a mystery. It must be that
2261 * the INIT was sent, but before it got there.. a complete
2262 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2263 * should have went to the other code.. not here.. oh well..
2264 * a bit of protection is worth having..
2266 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2267 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2268 atomic_add_int(&stcb->asoc.refcnt, 1);
2269 SCTP_TCB_UNLOCK(stcb);
2270 SCTP_SOCKET_LOCK(so, 1);
2271 SCTP_TCB_LOCK(stcb);
2272 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2273 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2274 SCTP_SOCKET_UNLOCK(so, 1);
2278 soisconnected(stcb->sctp_socket);
2279 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2280 SCTP_SOCKET_UNLOCK(so, 1);
2282 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2283 (inp->sctp_socket->so_qlimit)) {
2285 * We don't want to do anything with this one. Since it is
2286 * the listening guy. The timer will get started for
2287 * accepted connections in the caller.
2291 /* since we did not send a HB make sure we don't double things */
2292 if ((netp) && (*netp))
2293 (*netp)->hb_responded = 1;
2295 if (stcb->asoc.sctp_autoclose_ticks &&
2296 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2297 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2299 /* calculate the RTT */
2300 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2301 if ((netp) && (*netp)) {
2302 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2303 &cookie->time_entered, sctp_align_unsafe_makecopy,
2304 SCTP_RTT_FROM_NON_DATA);
2306 /* respond with a COOKIE-ACK */
2307 sctp_send_cookie_ack(stcb);
2310 * check the address lists for any ASCONFs that need to be sent
2311 * AFTER the cookie-ack is sent
2313 sctp_check_address_list(stcb, m,
2314 initack_offset + sizeof(struct sctp_init_ack_chunk),
2315 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2316 initack_src, cookie->local_scope, cookie->site_scope,
2317 cookie->ipv4_scope, cookie->loopback_scope);
2324 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2325 * we NEED to make sure we are not already using the vtag. If so we
2326 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2327 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2328 SCTP_BASE_INFO(hashasocmark))];
2329 LIST_FOREACH(stcb, head, sctp_asocs) {
2330 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2331 -- SEND ABORT - TRY AGAIN --
2337 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2338 * existing (non-NULL) TCB
2340 static struct mbuf *
2341 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2342 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2343 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2344 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2345 struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint16_t port)
2347 struct sctp_state_cookie *cookie;
2348 struct sctp_tcb *l_stcb = *stcb;
2349 struct sctp_inpcb *l_inp;
2350 struct sockaddr *to;
2351 struct sctp_pcb *ep;
2353 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2355 uint8_t cookie_ok = 0;
2356 unsigned int size_of_pkt, sig_offset, cookie_offset;
2357 unsigned int cookie_len;
2359 struct timeval time_expires;
2360 struct sockaddr_storage dest_store;
2361 struct sockaddr *localep_sa = (struct sockaddr *)&dest_store;
2363 int notification = 0;
2364 struct sctp_nets *netl;
2365 int had_a_existing_tcb = 0;
2366 int send_int_conf = 0;
2369 struct sockaddr_in sin;
2373 struct sockaddr_in6 sin6;
2377 SCTPDBG(SCTP_DEBUG_INPUT2,
2378 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2380 if (inp_p == NULL) {
2383 /* First get the destination address setup too. */
2384 iph = mtod(m, struct ip *);
2385 switch (iph->ip_v) {
2390 struct sockaddr_in *lsin;
2392 lsin = (struct sockaddr_in *)(localep_sa);
2393 memset(lsin, 0, sizeof(*lsin));
2394 lsin->sin_family = AF_INET;
2395 lsin->sin_len = sizeof(*lsin);
2396 lsin->sin_port = sh->dest_port;
2397 lsin->sin_addr.s_addr = iph->ip_dst.s_addr;
2398 size_of_pkt = SCTP_GET_IPV4_LENGTH(iph);
2403 case IPV6_VERSION >> 4:
2406 struct ip6_hdr *ip6;
2407 struct sockaddr_in6 *lsin6;
2409 lsin6 = (struct sockaddr_in6 *)(localep_sa);
2410 memset(lsin6, 0, sizeof(*lsin6));
2411 lsin6->sin6_family = AF_INET6;
2412 lsin6->sin6_len = sizeof(struct sockaddr_in6);
2413 ip6 = mtod(m, struct ip6_hdr *);
2414 lsin6->sin6_port = sh->dest_port;
2415 lsin6->sin6_addr = ip6->ip6_dst;
2416 size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen;
2424 cookie = &cp->cookie;
2425 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2426 cookie_len = ntohs(cp->ch.chunk_length);
2428 if ((cookie->peerport != sh->src_port) &&
2429 (cookie->myport != sh->dest_port) &&
2430 (cookie->my_vtag != sh->v_tag)) {
2432 * invalid ports or bad tag. Note that we always leave the
2433 * v_tag in the header in network order and when we stored
2434 * it in the my_vtag slot we also left it in network order.
2435 * This maintains the match even though it may be in the
2436 * opposite byte order of the machine :->
2440 if (cookie_len > size_of_pkt ||
2441 cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2442 sizeof(struct sctp_init_chunk) +
2443 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2444 /* cookie too long! or too small */
2448 * split off the signature into its own mbuf (since it should not be
2449 * calculated in the sctp_hmac_m() call).
2451 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2452 if (sig_offset > size_of_pkt) {
2453 /* packet not correct size! */
2454 /* XXX this may already be accounted for earlier... */
2457 m_sig = m_split(m, sig_offset, M_DONTWAIT);
2458 if (m_sig == NULL) {
2459 /* out of memory or ?? */
2462 #ifdef SCTP_MBUF_LOGGING
2463 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2466 for (mat = m_sig; mat; mat = SCTP_BUF_NEXT(mat)) {
2467 if (SCTP_BUF_IS_EXTENDED(mat)) {
2468 sctp_log_mb(mat, SCTP_MBUF_SPLIT);
2475 * compute the signature/digest for the cookie
2477 ep = &(*inp_p)->sctp_ep;
2480 SCTP_TCB_UNLOCK(l_stcb);
2482 SCTP_INP_RLOCK(l_inp);
2484 SCTP_TCB_LOCK(l_stcb);
2486 /* which cookie is it? */
2487 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2488 (ep->current_secret_number != ep->last_secret_number)) {
2489 /* it's the old cookie */
2490 (void)sctp_hmac_m(SCTP_HMAC,
2491 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2492 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2494 /* it's the current cookie */
2495 (void)sctp_hmac_m(SCTP_HMAC,
2496 (uint8_t *) ep->secret_key[(int)ep->current_secret_number],
2497 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2499 /* get the signature */
2500 SCTP_INP_RUNLOCK(l_inp);
2501 sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig);
2503 /* couldn't find signature */
2504 sctp_m_freem(m_sig);
2507 /* compare the received digest with the computed digest */
2508 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2509 /* try the old cookie? */
2510 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2511 (ep->current_secret_number != ep->last_secret_number)) {
2512 /* compute digest with old */
2513 (void)sctp_hmac_m(SCTP_HMAC,
2514 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2515 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2517 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2525 * Now before we continue we must reconstruct our mbuf so that
2526 * normal processing of any other chunks will work.
2532 while (SCTP_BUF_NEXT(m_at) != NULL) {
2533 m_at = SCTP_BUF_NEXT(m_at);
2535 SCTP_BUF_NEXT(m_at) = m_sig;
2538 if (cookie_ok == 0) {
2539 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2540 SCTPDBG(SCTP_DEBUG_INPUT2,
2541 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2542 (uint32_t) offset, cookie_offset, sig_offset);
2546 * check the cookie timestamps to be sure it's not stale
2548 (void)SCTP_GETTIME_TIMEVAL(&now);
2549 /* Expire time is in Ticks, so we convert to seconds */
2550 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2551 time_expires.tv_usec = cookie->time_entered.tv_usec;
2553 * TODO sctp_constants.h needs alternative time macros when _KERNEL
2556 if (timevalcmp(&now, &time_expires, >)) {
2557 /* cookie is stale! */
2558 struct mbuf *op_err;
2559 struct sctp_stale_cookie_msg *scm;
2562 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg),
2563 0, M_DONTWAIT, 1, MT_DATA);
2564 if (op_err == NULL) {
2569 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg);
2570 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
2571 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
2572 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
2573 (sizeof(uint32_t))));
2574 /* seconds to usec */
2575 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2578 tim = now.tv_usec - cookie->time_entered.tv_usec;
2579 scm->time_usec = htonl(tim);
2580 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag,
2585 * Now we must see with the lookup address if we have an existing
2586 * asoc. This will only happen if we were in the COOKIE-WAIT state
2587 * and a INIT collided with us and somewhere the peer sent the
2588 * cookie on another address besides the single address our assoc
2589 * had for him. In this case we will have one of the tie-tags set at
2590 * least AND the address field in the cookie can be used to look it
2594 switch (cookie->addr_type) {
2596 case SCTP_IPV6_ADDRESS:
2597 memset(&sin6, 0, sizeof(sin6));
2598 sin6.sin6_family = AF_INET6;
2599 sin6.sin6_len = sizeof(sin6);
2600 sin6.sin6_port = sh->src_port;
2601 sin6.sin6_scope_id = cookie->scope_id;
2602 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2603 sizeof(sin6.sin6_addr.s6_addr));
2604 to = (struct sockaddr *)&sin6;
2608 case SCTP_IPV4_ADDRESS:
2609 memset(&sin, 0, sizeof(sin));
2610 sin.sin_family = AF_INET;
2611 sin.sin_len = sizeof(sin);
2612 sin.sin_port = sh->src_port;
2613 sin.sin_addr.s_addr = cookie->address[0];
2614 to = (struct sockaddr *)&sin;
2618 /* This should not happen */
2621 if ((*stcb == NULL) && to) {
2622 /* Yep, lets check */
2623 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL);
2624 if (*stcb == NULL) {
2626 * We should have only got back the same inp. If we
2627 * got back a different ep we have a problem. The
2628 * original findep got back l_inp and now
2630 if (l_inp != *inp_p) {
2631 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2634 if (*locked_tcb == NULL) {
2636 * In this case we found the assoc only
2637 * after we locked the create lock. This
2638 * means we are in a colliding case and we
2639 * must make sure that we unlock the tcb if
2640 * its one of the cases where we throw away
2641 * the incoming packets.
2643 *locked_tcb = *stcb;
2646 * We must also increment the inp ref count
2647 * since the ref_count flags was set when we
2648 * did not find the TCB, now we found it
2649 * which reduces the refcount.. we must
2650 * raise it back out to balance it all :-)
2652 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2653 if ((*stcb)->sctp_ep != l_inp) {
2654 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2655 (*stcb)->sctp_ep, l_inp);
2663 cookie_len -= SCTP_SIGNATURE_SIZE;
2664 if (*stcb == NULL) {
2665 /* this is the "normal" case... get a new TCB */
2666 *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie,
2667 cookie_len, *inp_p, netp, to, ¬ification,
2668 auth_skipped, auth_offset, auth_len, vrf_id, port);
2670 /* this is abnormal... cookie-echo on existing TCB */
2671 had_a_existing_tcb = 1;
2672 *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh,
2673 cookie, cookie_len, *inp_p, *stcb, netp, to,
2674 ¬ification, vrf_id, auth_skipped, auth_offset, auth_len, port);
2677 if (*stcb == NULL) {
2678 /* still no TCB... must be bad cookie-echo */
2681 if ((*netp != NULL) && (m->m_flags & M_FLOWID)) {
2682 (*netp)->flowid = m->m_pkthdr.flowid;
2684 (*netp)->flowidset = 1;
2688 * Ok, we built an association so confirm the address we sent the
2691 netl = sctp_findnet(*stcb, to);
2693 * This code should in theory NOT run but
2696 /* TSNH! Huh, why do I need to add this address here? */
2697 if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2700 netl = sctp_findnet(*stcb, to);
2703 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2704 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2705 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2710 sctp_start_net_timers(*stcb);
2711 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2712 if (!had_a_existing_tcb ||
2713 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2715 * If we have a NEW cookie or the connect never
2716 * reached the connected state during collision we
2717 * must do the TCP accept thing.
2719 struct socket *so, *oso;
2720 struct sctp_inpcb *inp;
2722 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2724 * For a restart we will keep the same
2725 * socket, no need to do anything. I THINK!!
2727 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2728 if (send_int_conf) {
2729 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2730 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2734 oso = (*inp_p)->sctp_socket;
2735 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2736 SCTP_TCB_UNLOCK((*stcb));
2737 CURVNET_SET(oso->so_vnet);
2738 so = sonewconn(oso, 0
2741 SCTP_TCB_LOCK((*stcb));
2742 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2745 struct mbuf *op_err;
2747 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2748 struct socket *pcb_so;
2751 /* Too many sockets */
2752 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2753 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2754 sctp_abort_association(*inp_p, NULL, m, iphlen,
2755 sh, op_err, vrf_id, port);
2756 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2757 pcb_so = SCTP_INP_SO(*inp_p);
2758 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2759 SCTP_TCB_UNLOCK((*stcb));
2760 SCTP_SOCKET_LOCK(pcb_so, 1);
2761 SCTP_TCB_LOCK((*stcb));
2762 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2764 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2765 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2766 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2770 inp = (struct sctp_inpcb *)so->so_pcb;
2771 SCTP_INP_INCR_REF(inp);
2773 * We add the unbound flag here so that if we get an
2774 * soabort() before we get the move_pcb done, we
2775 * will properly cleanup.
2777 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2778 SCTP_PCB_FLAGS_CONNECTED |
2779 SCTP_PCB_FLAGS_IN_TCPPOOL |
2780 SCTP_PCB_FLAGS_UNBOUND |
2781 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2782 SCTP_PCB_FLAGS_DONT_WAKE);
2783 inp->sctp_features = (*inp_p)->sctp_features;
2784 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2785 inp->sctp_socket = so;
2786 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2787 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2788 inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable;
2789 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2790 inp->sctp_context = (*inp_p)->sctp_context;
2791 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2792 inp->inp_starting_point_for_iterator = NULL;
2794 * copy in the authentication parameters from the
2797 if (inp->sctp_ep.local_hmacs)
2798 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2799 inp->sctp_ep.local_hmacs =
2800 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2801 if (inp->sctp_ep.local_auth_chunks)
2802 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2803 inp->sctp_ep.local_auth_chunks =
2804 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2807 * Now we must move it from one hash table to
2808 * another and get the tcb in the right place.
2812 * This is where the one-2-one socket is put into
2813 * the accept state waiting for the accept!
2816 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2818 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2820 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2821 SCTP_TCB_UNLOCK((*stcb));
2823 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2825 SCTP_TCB_LOCK((*stcb));
2826 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2830 * now we must check to see if we were aborted while
2831 * the move was going on and the lock/unlock
2834 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2836 * yep it was, we leave the assoc attached
2837 * to the socket since the sctp_inpcb_free()
2838 * call will send an abort for us.
2840 SCTP_INP_DECR_REF(inp);
2843 SCTP_INP_DECR_REF(inp);
2844 /* Switch over to the new guy */
2846 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2847 if (send_int_conf) {
2848 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2849 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2852 * Pull it from the incomplete queue and wake the
2855 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2856 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2857 SCTP_TCB_UNLOCK((*stcb));
2858 SCTP_SOCKET_LOCK(so, 1);
2861 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2862 SCTP_TCB_LOCK((*stcb));
2863 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2864 SCTP_SOCKET_UNLOCK(so, 1);
2869 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
2871 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2873 if (send_int_conf) {
2874 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2875 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2882 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2883 struct sctp_tcb *stcb, struct sctp_nets *net)
2885 /* cp must not be used, others call this without a c-ack :-) */
2886 struct sctp_association *asoc;
2888 SCTPDBG(SCTP_DEBUG_INPUT2,
2889 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2895 sctp_stop_all_cookie_timers(stcb);
2896 /* process according to association state */
2897 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2898 /* state change only needed when I am in right state */
2899 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2900 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2901 sctp_start_net_timers(stcb);
2902 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2903 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2904 stcb->sctp_ep, stcb, asoc->primary_destination);
2908 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2909 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2910 if (asoc->overall_error_count == 0) {
2911 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2912 &asoc->time_entered, sctp_align_safe_nocopy,
2913 SCTP_RTT_FROM_NON_DATA);
2915 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2916 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2917 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2918 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2919 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2923 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2924 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2925 so = SCTP_INP_SO(stcb->sctp_ep);
2926 atomic_add_int(&stcb->asoc.refcnt, 1);
2927 SCTP_TCB_UNLOCK(stcb);
2928 SCTP_SOCKET_LOCK(so, 1);
2929 SCTP_TCB_LOCK(stcb);
2930 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2932 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2933 soisconnected(stcb->sctp_socket);
2935 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2936 SCTP_SOCKET_UNLOCK(so, 1);
2940 * since we did not send a HB make sure we don't double
2943 net->hb_responded = 1;
2945 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2947 * We don't need to do the asconf thing, nor hb or
2948 * autoclose if the socket is closed.
2952 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2956 if (stcb->asoc.sctp_autoclose_ticks &&
2957 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2958 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2959 stcb->sctp_ep, stcb, NULL);
2962 * send ASCONF if parameters are pending and ASCONFs are
2963 * allowed (eg. addresses changed when init/cookie echo were
2966 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2967 (stcb->asoc.peer_supports_asconf) &&
2968 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2969 #ifdef SCTP_TIMER_BASED_ASCONF
2970 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2971 stcb->sctp_ep, stcb,
2972 stcb->asoc.primary_destination);
2974 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2975 SCTP_ADDR_NOT_LOCKED);
2980 /* Toss the cookie if I can */
2981 sctp_toss_old_cookies(stcb, asoc);
2982 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
2983 /* Restart the timer if we have pending data */
2984 struct sctp_tmit_chunk *chk;
2986 chk = TAILQ_FIRST(&asoc->sent_queue);
2987 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
2992 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2993 struct sctp_tcb *stcb)
2995 struct sctp_nets *net;
2996 struct sctp_tmit_chunk *lchk;
2997 struct sctp_ecne_chunk bkup;
2998 uint8_t override_bit;
2999 uint32_t tsn, window_data_tsn;
3001 unsigned int pkt_cnt;
3003 len = ntohs(cp->ch.chunk_length);
3004 if ((len != sizeof(struct sctp_ecne_chunk)) &&
3005 (len != sizeof(struct old_sctp_ecne_chunk))) {
3008 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3009 /* Its the old format */
3010 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3011 bkup.num_pkts_since_cwr = htonl(1);
3014 SCTP_STAT_INCR(sctps_recvecne);
3015 tsn = ntohl(cp->tsn);
3016 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3017 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3019 window_data_tsn = stcb->asoc.sending_seq - 1;
3021 window_data_tsn = lchk->rec.data.TSN_seq;
3024 /* Find where it was sent to if possible. */
3026 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3027 if (lchk->rec.data.TSN_seq == tsn) {
3029 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3032 if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) {
3038 * What to do. A previous send of a CWR was possibly lost.
3039 * See how old it is, we may have it marked on the actual
3042 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3043 if (tsn == net->last_cwr_tsn) {
3044 /* Found him, send it off */
3050 * If we reach here, we need to send a special CWR
3051 * that says hey, we did this a long time ago and
3052 * you lost the response.
3054 net = TAILQ_FIRST(&stcb->asoc.nets);
3059 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3066 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3067 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3069 * JRS - Use the congestion control given in the pluggable
3072 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3074 * We reduce once every RTT. So we will only lower cwnd at
3075 * the next sending seq i.e. the window_data_tsn
3077 net->cwr_window_tsn = window_data_tsn;
3078 net->ecn_ce_pkt_cnt += pkt_cnt;
3079 net->lost_cnt = pkt_cnt;
3080 net->last_cwr_tsn = tsn;
3082 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3083 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3084 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3086 * Another loss in the same window update how many
3087 * marks/packets lost we have had.
3091 if (pkt_cnt > net->lost_cnt) {
3092 /* Should be the case */
3093 cnt = (pkt_cnt - net->lost_cnt);
3094 net->ecn_ce_pkt_cnt += cnt;
3096 net->lost_cnt = pkt_cnt;
3097 net->last_cwr_tsn = tsn;
3099 * Most CC functions will ignore this call, since we
3100 * are in-window yet of the initial CE the peer saw.
3102 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3106 * We always send a CWR this way if our previous one was lost our
3107 * peer will get an update, or if it is not time again to reduce we
3108 * still get the cwr to the peer. Note we set the override when we
3109 * could not find the TSN on the chunk or the destination network.
3111 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3115 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3118 * Here we get a CWR from the peer. We must look in the outqueue and
3119 * make sure that we have a covered ECNE in teh control chunk part.
3122 struct sctp_tmit_chunk *chk;
3123 struct sctp_ecne_chunk *ecne;
3127 cwr_tsn = ntohl(cp->tsn);
3129 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3130 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
3131 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3134 if ((override == 0) && (chk->whoTo != net)) {
3135 /* Must be from the right src unless override is set */
3138 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3139 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3140 /* this covers this ECNE, we can remove it */
3141 stcb->asoc.ecn_echo_cnt_onq--;
3142 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3145 sctp_m_freem(chk->data);
3148 stcb->asoc.ctrl_queue_cnt--;
3149 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3150 if (override == 0) {
3158 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3159 struct sctp_tcb *stcb, struct sctp_nets *net)
3161 struct sctp_association *asoc;
3163 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3168 SCTPDBG(SCTP_DEBUG_INPUT2,
3169 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3174 /* process according to association state */
3175 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3176 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3177 SCTPDBG(SCTP_DEBUG_INPUT2,
3178 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3179 SCTP_TCB_UNLOCK(stcb);
3182 /* notify upper layer protocol */
3183 if (stcb->sctp_socket) {
3184 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3185 /* are the queues empty? they should be */
3186 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3187 !TAILQ_EMPTY(&asoc->sent_queue) ||
3188 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
3189 sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED);
3192 /* stop the timer */
3193 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
3194 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3196 SCTPDBG(SCTP_DEBUG_INPUT2,
3197 "sctp_handle_shutdown_complete: calls free-asoc\n");
3198 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3199 so = SCTP_INP_SO(stcb->sctp_ep);
3200 atomic_add_int(&stcb->asoc.refcnt, 1);
3201 SCTP_TCB_UNLOCK(stcb);
3202 SCTP_SOCKET_LOCK(so, 1);
3203 SCTP_TCB_LOCK(stcb);
3204 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3206 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
3207 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3208 SCTP_SOCKET_UNLOCK(so, 1);
3214 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3215 struct sctp_nets *net, uint8_t flg)
3217 switch (desc->chunk_type) {
3219 /* find the tsn to resend (possibly */
3222 struct sctp_tmit_chunk *tp1;
3224 tsn = ntohl(desc->tsn_ifany);
3225 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3226 if (tp1->rec.data.TSN_seq == tsn) {
3230 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) {
3238 * Do it the other way , aka without paying
3239 * attention to queue seq order.
3241 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3242 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3243 if (tp1->rec.data.TSN_seq == tsn) {
3250 SCTP_STAT_INCR(sctps_pdrptsnnf);
3252 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3255 if (((flg & SCTP_BADCRC) == 0) &&
3256 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3259 if ((stcb->asoc.peers_rwnd == 0) &&
3260 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3261 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3264 if (stcb->asoc.peers_rwnd == 0 &&
3265 (flg & SCTP_FROM_MIDDLE_BOX)) {
3266 SCTP_STAT_INCR(sctps_pdrpdizrw);
3269 ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+
3270 sizeof(struct sctp_data_chunk));
3274 for (iii = 0; iii < sizeof(desc->data_bytes);
3276 if (ddp[iii] != desc->data_bytes[iii]) {
3277 SCTP_STAT_INCR(sctps_pdrpbadd);
3285 * this guy had a RTO calculation
3286 * pending on it, cancel it
3288 if (tp1->whoTo->rto_needed == 0) {
3289 tp1->whoTo->rto_needed = 1;
3293 SCTP_STAT_INCR(sctps_pdrpmark);
3294 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3295 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3297 * mark it as if we were doing a FR, since
3298 * we will be getting gap ack reports behind
3299 * the info from the router.
3301 tp1->rec.data.doing_fast_retransmit = 1;
3303 * mark the tsn with what sequences can
3306 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3307 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3309 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
3312 /* restart the timer */
3313 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3314 stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3315 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3318 /* fix counts and things */
3319 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3320 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3321 tp1->whoTo->flight_size,
3324 tp1->rec.data.TSN_seq);
3326 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3327 sctp_flight_size_decrease(tp1);
3328 sctp_total_flight_decrease(stcb, tp1);
3330 tp1->sent = SCTP_DATAGRAM_RESEND;
3336 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3337 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3340 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3342 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3345 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3346 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3347 audit, stcb->asoc.sent_queue_retran_cnt);
3348 #ifndef SCTP_AUDITING_ENABLED
3349 stcb->asoc.sent_queue_retran_cnt = audit;
3357 struct sctp_tmit_chunk *asconf;
3359 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3361 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3366 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3367 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3368 asconf->sent = SCTP_DATAGRAM_RESEND;
3369 asconf->snd_count--;
3373 case SCTP_INITIATION:
3374 /* resend the INIT */
3375 stcb->asoc.dropped_special_cnt++;
3376 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3378 * If we can get it in, in a few attempts we do
3379 * this, otherwise we let the timer fire.
3381 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3382 stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3383 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3386 case SCTP_SELECTIVE_ACK:
3387 case SCTP_NR_SELECTIVE_ACK:
3388 /* resend the sack */
3389 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3391 case SCTP_HEARTBEAT_REQUEST:
3392 /* resend a demand HB */
3393 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3395 * Only retransmit if we KNOW we wont destroy the
3398 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3402 sctp_send_shutdown(stcb, net);
3404 case SCTP_SHUTDOWN_ACK:
3405 sctp_send_shutdown_ack(stcb, net);
3407 case SCTP_COOKIE_ECHO:
3409 struct sctp_tmit_chunk *cookie;
3412 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3414 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3419 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3420 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3421 cookie->sent = SCTP_DATAGRAM_RESEND;
3422 sctp_stop_all_cookie_timers(stcb);
3426 case SCTP_COOKIE_ACK:
3427 sctp_send_cookie_ack(stcb);
3429 case SCTP_ASCONF_ACK:
3430 /* resend last asconf ack */
3431 sctp_send_asconf_ack(stcb);
3433 case SCTP_FORWARD_CUM_TSN:
3434 send_forward_tsn(stcb, &stcb->asoc);
3436 /* can't do anything with these */
3437 case SCTP_PACKET_DROPPED:
3438 case SCTP_INITIATION_ACK: /* this should not happen */
3439 case SCTP_HEARTBEAT_ACK:
3440 case SCTP_ABORT_ASSOCIATION:
3441 case SCTP_OPERATION_ERROR:
3442 case SCTP_SHUTDOWN_COMPLETE:
3452 sctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3458 * We set things to 0xffff since this is the last delivered sequence
3459 * and we will be sending in 0 after the reset.
3462 if (number_entries) {
3463 for (i = 0; i < number_entries; i++) {
3464 temp = ntohs(list[i]);
3465 if (temp >= stcb->asoc.streamincnt) {
3468 stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff;
3472 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3473 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3476 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3480 sctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3484 if (number_entries == 0) {
3485 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3486 stcb->asoc.strmout[i].next_sequence_sent = 0;
3488 } else if (number_entries) {
3489 for (i = 0; i < number_entries; i++) {
3492 temp = ntohs(list[i]);
3493 if (temp >= stcb->asoc.streamoutcnt) {
3494 /* no such stream */
3497 stcb->asoc.strmout[temp].next_sequence_sent = 0;
3500 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3504 struct sctp_stream_reset_out_request *
3505 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3507 struct sctp_association *asoc;
3508 struct sctp_stream_reset_out_req *req;
3509 struct sctp_stream_reset_out_request *r;
3510 struct sctp_tmit_chunk *chk;
3514 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3515 asoc->stream_reset_outstanding = 0;
3518 if (stcb->asoc.str_reset == NULL) {
3519 asoc->stream_reset_outstanding = 0;
3522 chk = stcb->asoc.str_reset;
3523 if (chk->data == NULL) {
3527 /* he wants a copy of the chk pointer */
3530 clen = chk->send_size;
3531 req = mtod(chk->data, struct sctp_stream_reset_out_req *);
3533 if (ntohl(r->request_seq) == seq) {
3537 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3538 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3539 /* move to the next one, there can only be a max of two */
3540 r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len);
3541 if (ntohl(r->request_seq) == seq) {
3545 /* that seq is not here */
3550 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3552 struct sctp_association *asoc;
3553 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3555 if (stcb->asoc.str_reset == NULL) {
3560 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3561 TAILQ_REMOVE(&asoc->control_send_queue,
3565 sctp_m_freem(chk->data);
3568 asoc->ctrl_queue_cnt--;
3569 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3570 /* sa_ignore NO_NULL_CHK */
3571 stcb->asoc.str_reset = NULL;
3576 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3577 uint32_t seq, uint32_t action,
3578 struct sctp_stream_reset_response *respin)
3582 struct sctp_association *asoc = &stcb->asoc;
3583 struct sctp_tmit_chunk *chk;
3584 struct sctp_stream_reset_out_request *srparam;
3587 if (asoc->stream_reset_outstanding == 0) {
3591 if (seq == stcb->asoc.str_reset_seq_out) {
3592 srparam = sctp_find_stream_reset(stcb, seq, &chk);
3594 stcb->asoc.str_reset_seq_out++;
3595 type = ntohs(srparam->ph.param_type);
3596 lparm_len = ntohs(srparam->ph.param_length);
3597 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3598 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3599 asoc->stream_reset_out_is_outstanding = 0;
3600 if (asoc->stream_reset_outstanding)
3601 asoc->stream_reset_outstanding--;
3602 if (action == SCTP_STREAM_RESET_PERFORMED) {
3604 sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams);
3606 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3608 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3609 /* Answered my request */
3610 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3611 if (asoc->stream_reset_outstanding)
3612 asoc->stream_reset_outstanding--;
3613 if (action != SCTP_STREAM_RESET_PERFORMED) {
3614 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3615 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3617 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3618 /* Ok we now may have more streams */
3621 num_stream = stcb->asoc.strm_pending_add_size;
3622 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3624 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3626 stcb->asoc.strm_pending_add_size = 0;
3627 if (asoc->stream_reset_outstanding)
3628 asoc->stream_reset_outstanding--;
3629 if (action == SCTP_STREAM_RESET_PERFORMED) {
3630 /* Put the new streams into effect */
3631 stcb->asoc.streamoutcnt += num_stream;
3632 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3634 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3635 SCTP_STREAM_CHANGED_DENIED);
3637 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3638 if (asoc->stream_reset_outstanding)
3639 asoc->stream_reset_outstanding--;
3640 if (action != SCTP_STREAM_RESET_PERFORMED) {
3641 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3642 SCTP_STREAM_CHANGED_DENIED);
3644 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3646 * a) Adopt the new in tsn.
3648 * c) Adopt the new out-tsn
3650 struct sctp_stream_reset_response_tsn *resp;
3651 struct sctp_forward_tsn_chunk fwdtsn;
3654 if (respin == NULL) {
3658 if (action == SCTP_STREAM_RESET_PERFORMED) {
3659 resp = (struct sctp_stream_reset_response_tsn *)respin;
3660 asoc->stream_reset_outstanding--;
3661 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3662 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3663 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3664 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3668 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3669 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3670 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3672 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3673 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3674 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3676 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3677 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3679 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3680 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3682 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3683 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3684 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3686 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3687 SCTP_STREAM_RESET_FAILED);
3690 /* get rid of the request and get the request flags */
3691 if (asoc->stream_reset_outstanding == 0) {
3692 sctp_clean_up_stream_reset(stcb);
3700 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3701 struct sctp_tmit_chunk *chk,
3702 struct sctp_stream_reset_in_request *req, int trunc)
3710 * peer wants me to send a str-reset to him for my outgoing seq's if
3713 struct sctp_association *asoc = &stcb->asoc;
3715 seq = ntohl(req->request_seq);
3716 if (asoc->str_reset_seq_in == seq) {
3718 /* Can't do it, since they exceeded our buffer size */
3719 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3720 asoc->last_reset_action[0] = SCTP_STREAM_RESET_REJECT;
3721 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3722 len = ntohs(req->ph.param_length);
3723 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3724 for (i = 0; i < number_entries; i++) {
3725 temp = ntohs(req->list_of_streams[i]);
3726 req->list_of_streams[i] = temp;
3728 /* move the reset action back one */
3729 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3730 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3731 sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams,
3732 asoc->str_reset_seq_out,
3733 seq, (asoc->sending_seq - 1));
3734 asoc->stream_reset_out_is_outstanding = 1;
3735 asoc->str_reset = chk;
3736 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
3737 stcb->asoc.stream_reset_outstanding++;
3739 /* Can't do it, since we have sent one out */
3740 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3741 asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER;
3743 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3744 asoc->str_reset_seq_in++;
3745 } else if (asoc->str_reset_seq_in - 1 == seq) {
3746 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3747 } else if (asoc->str_reset_seq_in - 2 == seq) {
3748 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3750 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3755 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3756 struct sctp_tmit_chunk *chk,
3757 struct sctp_stream_reset_tsn_request *req)
3759 /* reset all in and out and update the tsn */
3761 * A) reset my str-seq's on in and out. B) Select a receive next,
3762 * and set cum-ack to it. Also process this selected number as a
3763 * fwd-tsn as well. C) set in the response my next sending seq.
3765 struct sctp_forward_tsn_chunk fwdtsn;
3766 struct sctp_association *asoc = &stcb->asoc;
3770 seq = ntohl(req->request_seq);
3771 if (asoc->str_reset_seq_in == seq) {
3772 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3773 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3774 fwdtsn.ch.chunk_flags = 0;
3775 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3776 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3780 stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3781 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3782 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3784 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3785 stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1;
3786 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3787 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3788 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3789 atomic_add_int(&stcb->asoc.sending_seq, 1);
3790 /* save off historical data for retrans */
3791 stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0];
3792 stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq;
3793 stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0];
3794 stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn;
3796 sctp_add_stream_reset_result_tsn(chk,
3797 ntohl(req->request_seq),
3798 SCTP_STREAM_RESET_PERFORMED,
3799 stcb->asoc.sending_seq,
3800 stcb->asoc.mapping_array_base_tsn);
3801 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3802 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3803 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3804 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3805 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3806 asoc->str_reset_seq_in++;
3807 } else if (asoc->str_reset_seq_in - 1 == seq) {
3808 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3809 stcb->asoc.last_sending_seq[0],
3810 stcb->asoc.last_base_tsnsent[0]
3812 } else if (asoc->str_reset_seq_in - 2 == seq) {
3813 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3814 stcb->asoc.last_sending_seq[1],
3815 stcb->asoc.last_base_tsnsent[1]
3818 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3824 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3825 struct sctp_tmit_chunk *chk,
3826 struct sctp_stream_reset_out_request *req, int trunc)
3829 int number_entries, len;
3830 struct sctp_association *asoc = &stcb->asoc;
3832 seq = ntohl(req->request_seq);
3834 /* now if its not a duplicate we process it */
3835 if (asoc->str_reset_seq_in == seq) {
3836 len = ntohs(req->ph.param_length);
3837 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3839 * the sender is resetting, handle the list issue.. we must
3840 * a) verify if we can do the reset, if so no problem b) If
3841 * we can't do the reset we must copy the request. c) queue
3842 * it, and setup the data in processor to trigger it off
3843 * when needed and dequeue all the queued data.
3845 tsn = ntohl(req->send_reset_at_tsn);
3847 /* move the reset action back one */
3848 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3850 asoc->last_reset_action[0] = SCTP_STREAM_RESET_REJECT;
3851 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3852 /* we can do it now */
3853 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3854 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3857 * we must queue it up and thus wait for the TSN's
3858 * to arrive that are at or before tsn
3860 struct sctp_stream_reset_list *liste;
3863 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3864 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3865 siz, SCTP_M_STRESET);
3866 if (liste == NULL) {
3867 /* gak out of memory */
3868 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_REJECT);
3869 asoc->last_reset_action[0] = SCTP_STREAM_RESET_REJECT;
3873 liste->number_entries = number_entries;
3874 memcpy(&liste->req, req,
3875 (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t))));
3876 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3877 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3879 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3880 asoc->str_reset_seq_in++;
3881 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3883 * one seq back, just echo back last action since my
3884 * response was lost.
3886 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3887 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3889 * two seq back, just echo back last action since my
3890 * response was lost.
3892 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3894 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3899 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3900 struct sctp_stream_reset_add_strm *str_add)
3903 * Peer is requesting to add more streams. If its within our
3904 * max-streams we will allow it.
3906 uint32_t num_stream, i;
3908 struct sctp_association *asoc = &stcb->asoc;
3909 struct sctp_queued_to_read *ctl, *nctl;
3911 /* Get the number. */
3912 seq = ntohl(str_add->request_seq);
3913 num_stream = ntohs(str_add->number_of_streams);
3914 /* Now what would be the new total? */
3915 if (asoc->str_reset_seq_in == seq) {
3916 num_stream += stcb->asoc.streamincnt;
3917 if ((num_stream > stcb->asoc.max_inbound_streams) ||
3918 (num_stream > 0xffff)) {
3919 /* We must reject it they ask for to many */
3921 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3922 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_REJECT;
3924 /* Ok, we can do that :-) */
3925 struct sctp_stream_in *oldstrm;
3927 /* save off the old */
3928 oldstrm = stcb->asoc.strmin;
3929 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
3930 (num_stream * sizeof(struct sctp_stream_in)),
3932 if (stcb->asoc.strmin == NULL) {
3933 stcb->asoc.strmin = oldstrm;
3936 /* copy off the old data */
3937 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3938 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3939 stcb->asoc.strmin[i].stream_no = i;
3940 stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered;
3941 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
3942 /* now anything on those queues? */
3943 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) {
3944 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next);
3945 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next);
3948 /* Init the new streams */
3949 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
3950 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3951 stcb->asoc.strmin[i].stream_no = i;
3952 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3953 stcb->asoc.strmin[i].delivery_started = 0;
3955 SCTP_FREE(oldstrm, SCTP_M_STRMI);
3956 /* update the size */
3957 stcb->asoc.streamincnt = num_stream;
3958 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3959 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3960 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3962 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3963 asoc->str_reset_seq_in++;
3964 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3966 * one seq back, just echo back last action since my
3967 * response was lost.
3969 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3970 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3972 * two seq back, just echo back last action since my
3973 * response was lost.
3975 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3977 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3983 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3984 struct sctp_stream_reset_add_strm *str_add)
3987 * Peer is requesting to add more streams. If its within our
3988 * max-streams we will allow it.
3990 uint16_t num_stream;
3992 struct sctp_association *asoc = &stcb->asoc;
3994 /* Get the number. */
3995 seq = ntohl(str_add->request_seq);
3996 num_stream = ntohs(str_add->number_of_streams);
3997 /* Now what would be the new total? */
3998 if (asoc->str_reset_seq_in == seq) {
3999 if (stcb->asoc.stream_reset_outstanding) {
4000 /* We must reject it we have something pending */
4001 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4002 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_REJECT;
4004 /* Ok, we can do that :-) */
4007 mychk = stcb->asoc.streamoutcnt;
4008 mychk += num_stream;
4009 if (mychk < 0x10000) {
4010 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4011 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
4012 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 0, 1, num_stream, 0, 1)) {
4013 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_REJECT;
4016 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4017 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_REJECT;
4020 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4021 asoc->str_reset_seq_in++;
4022 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4024 * one seq back, just echo back last action since my
4025 * response was lost.
4027 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4028 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4030 * two seq back, just echo back last action since my
4031 * response was lost.
4033 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4035 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
4040 __attribute__((noinline))
4043 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4044 struct sctp_stream_reset_out_req *sr_req)
4046 int chk_length, param_len, ptype;
4047 struct sctp_paramhdr pstore;
4048 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4053 struct sctp_tmit_chunk *chk;
4054 struct sctp_chunkhdr *ch;
4055 struct sctp_paramhdr *ph;
4059 /* now it may be a reset or a reset-response */
4060 chk_length = ntohs(sr_req->ch.chunk_length);
4062 /* setup for adding the response */
4063 sctp_alloc_a_chunk(stcb, chk);
4067 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4068 chk->rec.chunk_id.can_take_data = 0;
4069 chk->asoc = &stcb->asoc;
4070 chk->no_fr_allowed = 0;
4071 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4072 chk->book_size_scale = 0;
4073 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4074 if (chk->data == NULL) {
4077 sctp_m_freem(chk->data);
4080 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4083 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4085 /* setup chunk parameters */
4086 chk->sent = SCTP_DATAGRAM_UNSENT;
4090 ch = mtod(chk->data, struct sctp_chunkhdr *);
4091 ch->chunk_type = SCTP_STREAM_RESET;
4092 ch->chunk_flags = 0;
4093 ch->chunk_length = htons(chk->send_size);
4094 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4095 offset += sizeof(struct sctp_chunkhdr);
4096 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) {
4097 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore);
4100 param_len = ntohs(ph->param_length);
4101 if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) {
4105 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)),
4106 (uint8_t *) & cstore);
4107 ptype = ntohs(ph->param_type);
4109 if (param_len > (int)sizeof(cstore)) {
4114 if (num_param > SCTP_MAX_RESET_PARAMS) {
4115 /* hit the max of parameters already sorry.. */
4118 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4119 struct sctp_stream_reset_out_request *req_out;
4121 req_out = (struct sctp_stream_reset_out_request *)ph;
4123 if (stcb->asoc.stream_reset_outstanding) {
4124 seq = ntohl(req_out->response_seq);
4125 if (seq == stcb->asoc.str_reset_seq_out) {
4127 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL);
4130 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4131 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4132 struct sctp_stream_reset_add_strm *str_add;
4134 str_add = (struct sctp_stream_reset_add_strm *)ph;
4136 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4137 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4138 struct sctp_stream_reset_add_strm *str_add;
4140 str_add = (struct sctp_stream_reset_add_strm *)ph;
4142 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4143 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4144 struct sctp_stream_reset_in_request *req_in;
4147 req_in = (struct sctp_stream_reset_in_request *)ph;
4148 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4149 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4150 struct sctp_stream_reset_tsn_request *req_tsn;
4153 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4154 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4156 goto strres_nochunk;
4160 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4161 struct sctp_stream_reset_response *resp;
4164 resp = (struct sctp_stream_reset_response *)ph;
4165 seq = ntohl(resp->response_seq);
4166 result = ntohl(resp->result);
4167 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4169 goto strres_nochunk;
4174 offset += SCTP_SIZE32(param_len);
4175 chk_length -= SCTP_SIZE32(param_len);
4178 /* we have no response free the stuff */
4179 goto strres_nochunk;
4181 /* ok we have a chunk to link in */
4182 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4185 stcb->asoc.ctrl_queue_cnt++;
4190 * Handle a router or endpoints report of a packet loss, there are two ways
4191 * to handle this, either we get the whole packet and must disect it
4192 * ourselves (possibly with truncation and or corruption) or it is a summary
4193 * from a middle box that did the disectting for us.
4196 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4197 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4199 uint32_t bottle_bw, on_queue;
4203 struct sctp_chunk_desc desc;
4204 struct sctp_chunkhdr *ch;
4206 chlen = ntohs(cp->ch.chunk_length);
4207 chlen -= sizeof(struct sctp_pktdrop_chunk);
4208 /* XXX possible chlen underflow */
4211 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4212 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4214 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4215 chlen -= sizeof(struct sctphdr);
4216 /* XXX possible chlen underflow */
4217 memset(&desc, 0, sizeof(desc));
4219 trunc_len = (uint16_t) ntohs(cp->trunc_len);
4220 if (trunc_len > limit) {
4223 /* now the chunks themselves */
4224 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4225 desc.chunk_type = ch->chunk_type;
4226 /* get amount we need to move */
4227 at = ntohs(ch->chunk_length);
4228 if (at < sizeof(struct sctp_chunkhdr)) {
4229 /* corrupt chunk, maybe at the end? */
4230 SCTP_STAT_INCR(sctps_pdrpcrupt);
4233 if (trunc_len == 0) {
4234 /* we are supposed to have all of it */
4236 /* corrupt skip it */
4237 SCTP_STAT_INCR(sctps_pdrpcrupt);
4241 /* is there enough of it left ? */
4242 if (desc.chunk_type == SCTP_DATA) {
4243 if (chlen < (sizeof(struct sctp_data_chunk) +
4244 sizeof(desc.data_bytes))) {
4248 if (chlen < sizeof(struct sctp_chunkhdr)) {
4253 if (desc.chunk_type == SCTP_DATA) {
4254 /* can we get out the tsn? */
4255 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4256 SCTP_STAT_INCR(sctps_pdrpmbda);
4258 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4260 struct sctp_data_chunk *dcp;
4264 dcp = (struct sctp_data_chunk *)ch;
4265 ddp = (uint8_t *) (dcp + 1);
4266 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4267 desc.data_bytes[iii] = ddp[iii];
4269 desc.tsn_ifany = dcp->dp.tsn;
4271 /* nope we are done. */
4272 SCTP_STAT_INCR(sctps_pdrpnedat);
4276 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4277 SCTP_STAT_INCR(sctps_pdrpmbct);
4280 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4281 SCTP_STAT_INCR(sctps_pdrppdbrk);
4284 if (SCTP_SIZE32(at) > chlen) {
4287 chlen -= SCTP_SIZE32(at);
4288 if (chlen < sizeof(struct sctp_chunkhdr)) {
4289 /* done, none left */
4292 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4294 /* Now update any rwnd --- possibly */
4295 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4296 /* From a peer, we get a rwnd report */
4299 SCTP_STAT_INCR(sctps_pdrpfehos);
4301 bottle_bw = ntohl(cp->bottle_bw);
4302 on_queue = ntohl(cp->current_onq);
4303 if (bottle_bw && on_queue) {
4304 /* a rwnd report is in here */
4305 if (bottle_bw > on_queue)
4306 a_rwnd = bottle_bw - on_queue;
4311 stcb->asoc.peers_rwnd = 0;
4313 if (a_rwnd > stcb->asoc.total_flight) {
4314 stcb->asoc.peers_rwnd =
4315 a_rwnd - stcb->asoc.total_flight;
4317 stcb->asoc.peers_rwnd = 0;
4319 if (stcb->asoc.peers_rwnd <
4320 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4321 /* SWS sender side engages */
4322 stcb->asoc.peers_rwnd = 0;
4327 SCTP_STAT_INCR(sctps_pdrpfmbox);
4330 /* now middle boxes in sat networks get a cwnd bump */
4331 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4332 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4333 (stcb->asoc.sat_network)) {
4335 * This is debateable but for sat networks it makes sense
4336 * Note if a T3 timer has went off, we will prohibit any
4337 * changes to cwnd until we exit the t3 loss recovery.
4339 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4340 net, cp, &bottle_bw, &on_queue);
4345 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4346 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4347 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4348 * length of the complete packet outputs: - length: modified to remaining
4349 * length after control processing - netp: modified to new sctp_nets after
4350 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4351 * bad packet,...) otherwise return the tcb for this packet
4354 __attribute__((noinline))
4356 static struct sctp_tcb *
4357 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4358 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4359 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4360 uint32_t vrf_id, uint16_t port)
4362 struct sctp_association *asoc;
4364 int num_chunks = 0; /* number of control chunks processed */
4365 uint32_t chk_length;
4367 int abort_no_unlock = 0;
4371 * How big should this be, and should it be alloc'd? Lets try the
4372 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4373 * until we get into jumbo grams and such..
4375 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4376 struct sctp_tcb *locked_tcb = stcb;
4378 uint32_t auth_offset = 0, auth_len = 0;
4379 int auth_skipped = 0;
4382 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4387 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4388 iphlen, *offset, length, stcb);
4390 /* validate chunk header length... */
4391 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4392 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4393 ntohs(ch->chunk_length));
4395 SCTP_TCB_UNLOCK(locked_tcb);
4400 * validate the verification tag
4402 vtag_in = ntohl(sh->v_tag);
4405 SCTP_TCB_LOCK_ASSERT(locked_tcb);
4407 if (ch->chunk_type == SCTP_INITIATION) {
4408 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4409 ntohs(ch->chunk_length), vtag_in);
4411 /* protocol error- silently discard... */
4412 SCTP_STAT_INCR(sctps_badvtag);
4414 SCTP_TCB_UNLOCK(locked_tcb);
4418 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4420 * If there is no stcb, skip the AUTH chunk and process
4421 * later after a stcb is found (to validate the lookup was
4424 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4426 !SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4427 /* save this chunk for later processing */
4429 auth_offset = *offset;
4430 auth_len = ntohs(ch->chunk_length);
4432 /* (temporarily) move past this chunk */
4433 *offset += SCTP_SIZE32(auth_len);
4434 if (*offset >= length) {
4435 /* no more data left in the mbuf chain */
4438 SCTP_TCB_UNLOCK(locked_tcb);
4442 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4443 sizeof(struct sctp_chunkhdr), chunk_buf);
4449 SCTP_TCB_UNLOCK(locked_tcb);
4453 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4454 goto process_control_chunks;
4457 * first check if it's an ASCONF with an unknown src addr we
4458 * need to look inside to find the association
4460 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4461 struct sctp_chunkhdr *asconf_ch = ch;
4462 uint32_t asconf_offset = 0, asconf_len = 0;
4464 /* inp's refcount may be reduced */
4465 SCTP_INP_INCR_REF(inp);
4467 asconf_offset = *offset;
4469 asconf_len = ntohs(asconf_ch->chunk_length);
4470 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4472 stcb = sctp_findassociation_ep_asconf(m,
4473 *offset, sh, &inp, netp, vrf_id);
4476 asconf_offset += SCTP_SIZE32(asconf_len);
4477 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4478 sizeof(struct sctp_chunkhdr), chunk_buf);
4479 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4482 * reduce inp's refcount if not reduced in
4483 * sctp_findassociation_ep_asconf().
4485 SCTP_INP_DECR_REF(inp);
4490 /* now go back and verify any auth chunk to be sure */
4491 if (auth_skipped && (stcb != NULL)) {
4492 struct sctp_auth_chunk *auth;
4494 auth = (struct sctp_auth_chunk *)
4495 sctp_m_getptr(m, auth_offset,
4496 auth_len, chunk_buf);
4499 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4501 /* auth HMAC failed so dump it */
4504 SCTP_TCB_UNLOCK(locked_tcb);
4508 /* remaining chunks are HMAC checked */
4509 stcb->asoc.authenticated = 1;
4514 /* no association, so it's out of the blue... */
4515 sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL,
4519 SCTP_TCB_UNLOCK(locked_tcb);
4524 /* ABORT and SHUTDOWN can use either v_tag... */
4525 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4526 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4527 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4528 if ((vtag_in == asoc->my_vtag) ||
4529 ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
4530 (vtag_in == asoc->peer_vtag))) {
4533 /* drop this packet... */
4534 SCTP_STAT_INCR(sctps_badvtag);
4536 SCTP_TCB_UNLOCK(locked_tcb);
4540 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4541 if (vtag_in != asoc->my_vtag) {
4543 * this could be a stale SHUTDOWN-ACK or the
4544 * peer never got the SHUTDOWN-COMPLETE and
4545 * is still hung; we have started a new asoc
4546 * but it won't complete until the shutdown
4550 SCTP_TCB_UNLOCK(locked_tcb);
4552 sctp_handle_ootb(m, iphlen, *offset, sh, inp,
4553 NULL, vrf_id, port);
4557 /* for all other chunks, vtag must match */
4558 if (vtag_in != asoc->my_vtag) {
4559 /* invalid vtag... */
4560 SCTPDBG(SCTP_DEBUG_INPUT3,
4561 "invalid vtag: %xh, expect %xh\n",
4562 vtag_in, asoc->my_vtag);
4563 SCTP_STAT_INCR(sctps_badvtag);
4565 SCTP_TCB_UNLOCK(locked_tcb);
4571 } /* end if !SCTP_COOKIE_ECHO */
4573 * process all control chunks...
4575 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4576 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4577 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4578 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4579 /* implied cookie-ack.. we must have lost the ack */
4580 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4581 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4582 stcb->asoc.overall_error_count,
4584 SCTP_FROM_SCTP_INPUT,
4587 stcb->asoc.overall_error_count = 0;
4588 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4591 process_control_chunks:
4592 while (IS_SCTP_CONTROL(ch)) {
4593 /* validate chunk length */
4594 chk_length = ntohs(ch->chunk_length);
4595 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4596 ch->chunk_type, chk_length);
4597 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4598 if (chk_length < sizeof(*ch) ||
4599 (*offset + (int)chk_length) > length) {
4602 SCTP_TCB_UNLOCK(locked_tcb);
4606 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4608 * INIT-ACK only gets the init ack "header" portion only
4609 * because we don't have to process the peer's COOKIE. All
4610 * others get a complete chunk.
4612 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4613 (ch->chunk_type == SCTP_INITIATION)) {
4614 /* get an init-ack chunk */
4615 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4616 sizeof(struct sctp_init_ack_chunk), chunk_buf);
4620 SCTP_TCB_UNLOCK(locked_tcb);
4625 /* For cookies and all other chunks. */
4626 if (chk_length > sizeof(chunk_buf)) {
4628 * use just the size of the chunk buffer so
4629 * the front part of our chunks fit in
4630 * contiguous space up to the chunk buffer
4631 * size (508 bytes). For chunks that need to
4632 * get more than that they must use the
4633 * sctp_m_getptr() function or other means
4634 * (e.g. know how to parse mbuf chains).
4635 * Cookies do this already.
4637 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4638 (sizeof(chunk_buf) - 4),
4643 SCTP_TCB_UNLOCK(locked_tcb);
4648 /* We can fit it all */
4649 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4650 chk_length, chunk_buf);
4652 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4655 SCTP_TCB_UNLOCK(locked_tcb);
4662 /* Save off the last place we got a control from */
4664 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4666 * allow last_control to be NULL if
4667 * ASCONF... ASCONF processing will find the
4670 if ((netp != NULL) && (*netp != NULL))
4671 stcb->asoc.last_control_chunk_from = *netp;
4674 #ifdef SCTP_AUDITING_ENABLED
4675 sctp_audit_log(0xB0, ch->chunk_type);
4678 /* check to see if this chunk required auth, but isn't */
4679 if ((stcb != NULL) &&
4680 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
4681 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4682 !stcb->asoc.authenticated) {
4683 /* "silently" ignore */
4684 SCTP_STAT_INCR(sctps_recvauthmissing);
4687 switch (ch->chunk_type) {
4688 case SCTP_INITIATION:
4689 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4690 /* The INIT chunk must be the only chunk. */
4691 if ((num_chunks > 1) ||
4692 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4693 sctp_abort_association(inp, stcb, m,
4694 iphlen, sh, NULL, vrf_id, port);
4698 /* Honor our resource limit. */
4699 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4700 struct mbuf *op_err;
4702 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
4703 sctp_abort_association(inp, stcb, m,
4704 iphlen, sh, op_err, vrf_id, port);
4708 sctp_handle_init(m, iphlen, *offset, sh,
4709 (struct sctp_init_chunk *)ch, inp,
4710 stcb, &abort_no_unlock, vrf_id, port);
4712 if ((!abort_no_unlock) && (locked_tcb)) {
4713 SCTP_TCB_UNLOCK(locked_tcb);
4717 case SCTP_PAD_CHUNK:
4719 case SCTP_INITIATION_ACK:
4720 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4721 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4722 /* We are not interested anymore */
4723 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4726 if (locked_tcb != stcb) {
4728 SCTP_TCB_UNLOCK(locked_tcb);
4732 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4733 so = SCTP_INP_SO(inp);
4734 atomic_add_int(&stcb->asoc.refcnt, 1);
4735 SCTP_TCB_UNLOCK(stcb);
4736 SCTP_SOCKET_LOCK(so, 1);
4737 SCTP_TCB_LOCK(stcb);
4738 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4740 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4741 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4742 SCTP_SOCKET_UNLOCK(so, 1);
4748 /* The INIT-ACK chunk must be the only chunk. */
4749 if ((num_chunks > 1) ||
4750 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4753 SCTP_TCB_UNLOCK(locked_tcb);
4757 if ((netp) && (*netp)) {
4758 ret = sctp_handle_init_ack(m, iphlen, *offset, sh,
4759 (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id);
4764 if (abort_no_unlock) {
4768 * Special case, I must call the output routine to
4769 * get the cookie echoed
4771 if ((stcb != NULL) && (ret == 0)) {
4772 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4775 SCTP_TCB_UNLOCK(locked_tcb);
4779 case SCTP_SELECTIVE_ACK:
4781 struct sctp_sack_chunk *sack;
4783 uint32_t a_rwnd, cum_ack;
4784 uint16_t num_seg, num_dup;
4786 int offset_seg, offset_dup;
4788 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4789 SCTP_STAT_INCR(sctps_recvsacks);
4791 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4794 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4795 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4798 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4800 * If we have sent a shutdown-ack, we will pay no
4801 * attention to a sack sent in to us since
4802 * we don't care anymore.
4806 sack = (struct sctp_sack_chunk *)ch;
4807 flags = ch->chunk_flags;
4808 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4809 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4810 num_dup = ntohs(sack->sack.num_dup_tsns);
4811 a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
4812 if (sizeof(struct sctp_sack_chunk) +
4813 num_seg * sizeof(struct sctp_gap_ack_block) +
4814 num_dup * sizeof(uint32_t) != chk_length) {
4815 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4818 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4819 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4820 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4821 cum_ack, num_seg, a_rwnd);
4822 stcb->asoc.seen_a_sack_this_pkt = 1;
4823 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4825 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4826 (stcb->asoc.saw_sack_with_frags == 0) &&
4827 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4828 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4831 * We have a SIMPLE sack having no
4832 * prior segments and data on sent
4833 * queue to be acked.. Use the
4834 * faster path sack processing. We
4835 * also allow window update sacks
4836 * with no missing segments to go
4839 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
4842 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4843 num_seg, 0, num_dup, &abort_now, flags,
4844 cum_ack, a_rwnd, ecne_seen);
4847 /* ABORT signal from sack processing */
4851 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4852 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4853 (stcb->asoc.stream_queue_cnt == 0)) {
4854 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4859 * EY - nr_sack: If the received chunk is an
4862 case SCTP_NR_SELECTIVE_ACK:
4864 struct sctp_nr_sack_chunk *nr_sack;
4866 uint32_t a_rwnd, cum_ack;
4867 uint16_t num_seg, num_nr_seg, num_dup;
4869 int offset_seg, offset_dup;
4871 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
4872 SCTP_STAT_INCR(sctps_recvsacks);
4874 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
4877 if ((stcb->asoc.sctp_nr_sack_on_off == 0) ||
4878 (stcb->asoc.peer_supports_nr_sack == 0)) {
4881 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4882 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
4885 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4887 * If we have sent a shutdown-ack, we will pay no
4888 * attention to a sack sent in to us since
4889 * we don't care anymore.
4893 nr_sack = (struct sctp_nr_sack_chunk *)ch;
4894 flags = ch->chunk_flags;
4895 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4896 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4897 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4898 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4899 a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd);
4900 if (sizeof(struct sctp_nr_sack_chunk) +
4901 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4902 num_dup * sizeof(uint32_t) != chk_length) {
4903 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4906 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4907 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4908 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4909 cum_ack, num_seg, a_rwnd);
4910 stcb->asoc.seen_a_sack_this_pkt = 1;
4911 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4912 (num_seg == 0) && (num_nr_seg == 0) &&
4913 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4914 (stcb->asoc.saw_sack_with_frags == 0) &&
4915 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4916 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
4918 * We have a SIMPLE sack having no
4919 * prior segments and data on sent
4920 * queue to be acked. Use the faster
4921 * path sack processing. We also
4922 * allow window update sacks with no
4923 * missing segments to go this way
4926 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
4927 &abort_now, ecne_seen);
4930 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4931 num_seg, num_nr_seg, num_dup, &abort_now, flags,
4932 cum_ack, a_rwnd, ecne_seen);
4935 /* ABORT signal from sack processing */
4939 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4940 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4941 (stcb->asoc.stream_queue_cnt == 0)) {
4942 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4947 case SCTP_HEARTBEAT_REQUEST:
4948 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
4949 if ((stcb) && netp && *netp) {
4950 SCTP_STAT_INCR(sctps_recvheartbeat);
4951 sctp_send_heartbeat_ack(stcb, m, *offset,
4954 /* He's alive so give him credit */
4955 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4956 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4957 stcb->asoc.overall_error_count,
4959 SCTP_FROM_SCTP_INPUT,
4962 stcb->asoc.overall_error_count = 0;
4965 case SCTP_HEARTBEAT_ACK:
4966 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
4967 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
4971 SCTP_TCB_UNLOCK(locked_tcb);
4975 /* He's alive so give him credit */
4976 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4977 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4978 stcb->asoc.overall_error_count,
4980 SCTP_FROM_SCTP_INPUT,
4983 stcb->asoc.overall_error_count = 0;
4984 SCTP_STAT_INCR(sctps_recvheartbeatack);
4986 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
4989 case SCTP_ABORT_ASSOCIATION:
4990 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
4992 if ((stcb) && netp && *netp)
4993 sctp_handle_abort((struct sctp_abort_chunk *)ch,
4999 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5001 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5004 SCTP_TCB_UNLOCK(locked_tcb);
5008 if (netp && *netp) {
5011 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5012 stcb, *netp, &abort_flag);
5019 case SCTP_SHUTDOWN_ACK:
5020 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb);
5021 if ((stcb) && (netp) && (*netp))
5022 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5027 case SCTP_OPERATION_ERROR:
5028 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
5029 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
5034 case SCTP_COOKIE_ECHO:
5035 SCTPDBG(SCTP_DEBUG_INPUT3,
5036 "SCTP_COOKIE-ECHO, stcb %p\n", stcb);
5037 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5040 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5041 /* We are not interested anymore */
5044 SCTP_TCB_UNLOCK(stcb);
5051 * First are we accepting? We do this again here
5052 * since it is possible that a previous endpoint WAS
5053 * listening responded to a INIT-ACK and then
5054 * closed. We opened and bound.. and are now no
5058 if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
5059 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5060 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5061 struct mbuf *op_err;
5063 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
5064 sctp_abort_association(inp, stcb, m,
5065 iphlen, sh, op_err, vrf_id, port);
5070 struct mbuf *ret_buf;
5071 struct sctp_inpcb *linp;
5080 SCTP_ASOC_CREATE_LOCK(linp);
5081 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5082 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5083 SCTP_ASOC_CREATE_UNLOCK(linp);
5089 sctp_handle_cookie_echo(m, iphlen,
5091 (struct sctp_cookie_echo_chunk *)ch,
5103 SCTP_ASOC_CREATE_UNLOCK(linp);
5105 if (ret_buf == NULL) {
5107 SCTP_TCB_UNLOCK(locked_tcb);
5109 SCTPDBG(SCTP_DEBUG_INPUT3,
5110 "GAK, null buffer\n");
5114 /* if AUTH skipped, see if it verified... */
5119 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5121 * Restart the timer if we have
5124 struct sctp_tmit_chunk *chk;
5126 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5127 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5131 case SCTP_COOKIE_ACK:
5132 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb);
5133 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5135 SCTP_TCB_UNLOCK(locked_tcb);
5139 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5140 /* We are not interested anymore */
5141 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5144 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5145 so = SCTP_INP_SO(inp);
5146 atomic_add_int(&stcb->asoc.refcnt, 1);
5147 SCTP_TCB_UNLOCK(stcb);
5148 SCTP_SOCKET_LOCK(so, 1);
5149 SCTP_TCB_LOCK(stcb);
5150 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5152 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
5153 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5154 SCTP_SOCKET_UNLOCK(so, 1);
5160 /* He's alive so give him credit */
5161 if ((stcb) && netp && *netp) {
5162 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5163 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5164 stcb->asoc.overall_error_count,
5166 SCTP_FROM_SCTP_INPUT,
5169 stcb->asoc.overall_error_count = 0;
5170 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5174 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5175 /* He's alive so give him credit */
5176 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5179 SCTP_TCB_UNLOCK(locked_tcb);
5185 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5186 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5187 stcb->asoc.overall_error_count,
5189 SCTP_FROM_SCTP_INPUT,
5192 stcb->asoc.overall_error_count = 0;
5193 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5199 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5200 /* He's alive so give him credit */
5201 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5204 SCTP_TCB_UNLOCK(locked_tcb);
5210 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5211 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5212 stcb->asoc.overall_error_count,
5214 SCTP_FROM_SCTP_INPUT,
5217 stcb->asoc.overall_error_count = 0;
5218 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5221 case SCTP_SHUTDOWN_COMPLETE:
5222 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb);
5223 /* must be first and only chunk */
5224 if ((num_chunks > 1) ||
5225 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5228 SCTP_TCB_UNLOCK(locked_tcb);
5232 if ((stcb) && netp && *netp) {
5233 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5240 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5241 /* He's alive so give him credit */
5243 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5244 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5245 stcb->asoc.overall_error_count,
5247 SCTP_FROM_SCTP_INPUT,
5250 stcb->asoc.overall_error_count = 0;
5251 sctp_handle_asconf(m, *offset,
5252 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5256 case SCTP_ASCONF_ACK:
5257 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5258 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5261 SCTP_TCB_UNLOCK(locked_tcb);
5266 if ((stcb) && netp && *netp) {
5267 /* He's alive so give him credit */
5268 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5269 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5270 stcb->asoc.overall_error_count,
5272 SCTP_FROM_SCTP_INPUT,
5275 stcb->asoc.overall_error_count = 0;
5276 sctp_handle_asconf_ack(m, *offset,
5277 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5278 if (abort_no_unlock)
5282 case SCTP_FORWARD_CUM_TSN:
5283 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5284 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5287 SCTP_TCB_UNLOCK(locked_tcb);
5292 /* He's alive so give him credit */
5296 stcb->asoc.overall_error_count = 0;
5297 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5298 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5299 stcb->asoc.overall_error_count,
5301 SCTP_FROM_SCTP_INPUT,
5305 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5306 /* We are not interested anymore */
5307 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5308 so = SCTP_INP_SO(inp);
5309 atomic_add_int(&stcb->asoc.refcnt, 1);
5310 SCTP_TCB_UNLOCK(stcb);
5311 SCTP_SOCKET_LOCK(so, 1);
5312 SCTP_TCB_LOCK(stcb);
5313 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5315 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
5316 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5317 SCTP_SOCKET_UNLOCK(so, 1);
5322 sctp_handle_forward_tsn(stcb,
5323 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5328 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5329 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5330 stcb->asoc.overall_error_count,
5332 SCTP_FROM_SCTP_INPUT,
5335 stcb->asoc.overall_error_count = 0;
5340 case SCTP_STREAM_RESET:
5341 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5342 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5345 SCTP_TCB_UNLOCK(locked_tcb);
5350 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5351 /* We are not interested anymore */
5352 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5353 so = SCTP_INP_SO(inp);
5354 atomic_add_int(&stcb->asoc.refcnt, 1);
5355 SCTP_TCB_UNLOCK(stcb);
5356 SCTP_SOCKET_LOCK(so, 1);
5357 SCTP_TCB_LOCK(stcb);
5358 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5360 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5361 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5362 SCTP_SOCKET_UNLOCK(so, 1);
5367 if (stcb->asoc.peer_supports_strreset == 0) {
5369 * hmm, peer should have announced this, but
5370 * we will turn it on since he is sending us
5373 stcb->asoc.peer_supports_strreset = 1;
5375 if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) {
5376 /* stop processing */
5381 case SCTP_PACKET_DROPPED:
5382 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5383 /* re-get it all please */
5384 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5387 SCTP_TCB_UNLOCK(locked_tcb);
5392 if (ch && (stcb) && netp && (*netp)) {
5393 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5395 min(chk_length, (sizeof(chunk_buf) - 4)));
5400 case SCTP_AUTHENTICATION:
5401 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5402 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
5406 /* save the first AUTH for later processing */
5407 if (auth_skipped == 0) {
5408 auth_offset = *offset;
5409 auth_len = chk_length;
5412 /* skip this chunk (temporarily) */
5415 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5416 (chk_length > (sizeof(struct sctp_auth_chunk) +
5417 SCTP_AUTH_DIGEST_LEN_MAX))) {
5420 SCTP_TCB_UNLOCK(locked_tcb);
5425 if (got_auth == 1) {
5426 /* skip this chunk... it's already auth'd */
5430 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5432 /* auth HMAC failed so dump the packet */
5436 /* remaining chunks are HMAC checked */
5437 stcb->asoc.authenticated = 1;
5443 /* it's an unknown chunk! */
5444 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5446 struct sctp_paramhdr *phd;
5448 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
5449 0, M_DONTWAIT, 1, MT_DATA);
5451 phd = mtod(mm, struct sctp_paramhdr *);
5453 * We cheat and use param type since
5454 * we did not bother to define a
5455 * error cause struct. They are the
5456 * same basic format with different
5459 phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5460 phd->param_length = htons(chk_length + sizeof(*phd));
5461 SCTP_BUF_LEN(mm) = sizeof(*phd);
5462 SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, chk_length, M_DONTWAIT);
5463 if (SCTP_BUF_NEXT(mm)) {
5464 if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(mm), SCTP_SIZE32(chk_length) - chk_length, NULL)) {
5467 #ifdef SCTP_MBUF_LOGGING
5468 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5471 for (mat = SCTP_BUF_NEXT(mm); mat; mat = SCTP_BUF_NEXT(mat)) {
5472 if (SCTP_BUF_IS_EXTENDED(mat)) {
5473 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5478 sctp_queue_op_err(stcb, mm);
5485 if ((ch->chunk_type & 0x80) == 0) {
5486 /* discard this packet */
5489 } /* else skip this bad chunk and continue... */
5491 } /* switch (ch->chunk_type) */
5495 /* get the next chunk */
5496 *offset += SCTP_SIZE32(chk_length);
5497 if (*offset >= length) {
5498 /* no more data left in the mbuf chain */
5501 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5502 sizeof(struct sctp_chunkhdr), chunk_buf);
5505 SCTP_TCB_UNLOCK(locked_tcb);
5512 if (asconf_cnt > 0 && stcb != NULL) {
5513 sctp_send_asconf_ack(stcb);
5521 __attribute__((noinline))
5524 sctp_validate_no_locks(struct sctp_inpcb *inp)
5526 struct sctp_tcb *lstcb;
5528 LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) {
5529 if (mtx_owned(&lstcb->tcb_mtx)) {
5530 panic("Own lock on stcb at return from input");
5533 if (mtx_owned(&inp->inp_create_mtx)) {
5534 panic("Own create lock on inp");
5536 if (mtx_owned(&inp->inp_mtx)) {
5537 panic("Own inp lock on inp");
5544 * common input chunk processing (v4 and v6)
5547 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset,
5548 int length, struct sctphdr *sh, struct sctp_chunkhdr *ch,
5549 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
5550 uint8_t ecn_bits, uint32_t vrf_id, uint16_t port)
5553 * Control chunk processing
5556 int fwd_tsn_seen = 0, data_processed = 0;
5557 struct mbuf *m = *mm;
5559 int cnt_ctrl_ready = 0;
5561 SCTP_STAT_INCR(sctps_recvdatagrams);
5562 #ifdef SCTP_AUDITING_ENABLED
5563 sctp_audit_log(0xE0, 1);
5564 sctp_auditing(0, inp, stcb, net);
5567 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5568 m, iphlen, offset, length, stcb);
5570 /* always clear this before beginning a packet */
5571 stcb->asoc.authenticated = 0;
5572 stcb->asoc.seen_a_sack_this_pkt = 0;
5573 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5574 stcb, stcb->asoc.state);
5576 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5577 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5579 * If we hit here, we had a ref count
5580 * up when the assoc was aborted and the
5581 * timer is clearing out the assoc, we should
5582 * NOT respond to any packet.. its OOTB.
5584 SCTP_TCB_UNLOCK(stcb);
5585 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5590 if (IS_SCTP_CONTROL(ch)) {
5591 /* process the control portion of the SCTP packet */
5592 /* sa_ignore NO_NULL_CHK */
5593 stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch,
5594 inp, stcb, &net, &fwd_tsn_seen, vrf_id, port);
5597 * This covers us if the cookie-echo was there and
5598 * it changes our INP.
5600 inp = stcb->sctp_ep;
5601 if ((net) && (port)) {
5602 if (net->port == 0) {
5603 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5610 * no control chunks, so pre-process DATA chunks (these
5611 * checks are taken care of by control processing)
5615 * if DATA only packet, and auth is required, then punt...
5616 * can't have authenticated without any AUTH (control)
5619 if ((stcb != NULL) &&
5620 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5621 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5622 /* "silently" ignore */
5623 SCTP_STAT_INCR(sctps_recvauthmissing);
5624 SCTP_TCB_UNLOCK(stcb);
5628 /* out of the blue DATA chunk */
5629 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5633 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5634 /* v_tag mismatch! */
5635 SCTP_STAT_INCR(sctps_badvtag);
5636 SCTP_TCB_UNLOCK(stcb);
5643 * no valid TCB for this packet, or we found it's a bad
5644 * packet while processing control, or we're done with this
5645 * packet (done or skip rest of data), so we drop it...
5650 * DATA chunk processing
5652 /* plow through the data chunks while length > offset */
5655 * Rest should be DATA only. Check authentication state if AUTH for
5658 if ((length > offset) &&
5660 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5661 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5662 !stcb->asoc.authenticated) {
5663 /* "silently" ignore */
5664 SCTP_STAT_INCR(sctps_recvauthmissing);
5665 SCTPDBG(SCTP_DEBUG_AUTH1,
5666 "Data chunk requires AUTH, skipped\n");
5669 if (length > offset) {
5673 * First check to make sure our state is correct. We would
5674 * not get here unless we really did have a tag, so we don't
5675 * abort if this happens, just dump the chunk silently.
5677 switch (SCTP_GET_STATE(&stcb->asoc)) {
5678 case SCTP_STATE_COOKIE_ECHOED:
5680 * we consider data with valid tags in this state
5681 * shows us the cookie-ack was lost. Imply it was
5684 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5685 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5686 stcb->asoc.overall_error_count,
5688 SCTP_FROM_SCTP_INPUT,
5691 stcb->asoc.overall_error_count = 0;
5692 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5694 case SCTP_STATE_COOKIE_WAIT:
5696 * We consider OOTB any data sent during asoc setup.
5698 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5700 SCTP_TCB_UNLOCK(stcb);
5702 /* sa_ignore NOTREACHED */
5704 case SCTP_STATE_EMPTY: /* should not happen */
5705 case SCTP_STATE_INUSE: /* should not happen */
5706 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5707 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5709 SCTP_TCB_UNLOCK(stcb);
5711 /* sa_ignore NOTREACHED */
5713 case SCTP_STATE_OPEN:
5714 case SCTP_STATE_SHUTDOWN_SENT:
5717 /* plow through the data chunks while length > offset */
5718 retval = sctp_process_data(mm, iphlen, &offset, length, sh,
5719 inp, stcb, net, &high_tsn);
5722 * The association aborted, NO UNLOCK needed since
5723 * the association is destroyed.
5729 * Anything important needs to have been m_copy'ed in
5733 /* take care of ecn */
5734 if ((data_processed == 1) &&
5735 (stcb->asoc.ecn_allowed == 1) &&
5736 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5737 /* Yep, we need to add a ECNE */
5738 sctp_send_ecn_echo(stcb, net, high_tsn);
5740 if ((data_processed == 0) && (fwd_tsn_seen)) {
5742 uint32_t highest_tsn;
5744 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5745 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5747 highest_tsn = stcb->asoc.highest_tsn_inside_map;
5749 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
5750 stcb->asoc.send_sack = 1;
5751 sctp_sack_check(stcb, was_a_gap);
5752 } else if (fwd_tsn_seen) {
5753 stcb->asoc.send_sack = 1;
5755 /* trigger send of any chunks in queue... */
5757 #ifdef SCTP_AUDITING_ENABLED
5758 sctp_audit_log(0xE0, 2);
5759 sctp_auditing(1, inp, stcb, net);
5761 SCTPDBG(SCTP_DEBUG_INPUT1,
5762 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5763 stcb->asoc.peers_rwnd,
5764 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5765 stcb->asoc.total_flight);
5766 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5767 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5768 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5770 if (cnt_ctrl_ready ||
5772 (stcb->asoc.peers_rwnd > 0 ||
5773 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5774 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5775 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5776 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5778 #ifdef SCTP_AUDITING_ENABLED
5779 sctp_audit_log(0xE0, 3);
5780 sctp_auditing(2, inp, stcb, net);
5782 SCTP_TCB_UNLOCK(stcb);
5785 sctp_validate_no_locks(inp);
5792 sctp_print_mbuf_chain(struct mbuf *m)
5794 for (; m; m = SCTP_BUF_NEXT(m)) {
5795 printf("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m));
5796 if (SCTP_BUF_IS_EXTENDED(m))
5797 printf("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m));
5805 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5809 uint32_t vrf_id = 0;
5813 struct sctp_inpcb *inp = NULL;
5814 struct sctp_nets *net;
5815 struct sctp_tcb *stcb = NULL;
5816 struct sctp_chunkhdr *ch;
5817 int refcount_up = 0;
5818 int length, mlen, offset;
5820 #if !defined(SCTP_WITH_NO_CSUM)
5821 uint32_t check, calc_check;
5825 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
5826 SCTP_RELEASE_PKT(i_pak);
5829 mlen = SCTP_HEADER_LEN(i_pak);
5831 m = SCTP_HEADER_TO_CHAIN(i_pak);
5834 SCTP_STAT_INCR(sctps_recvpackets);
5835 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
5838 #ifdef SCTP_MBUF_LOGGING
5839 /* Log in any input mbufs */
5840 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5843 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
5844 if (SCTP_BUF_IS_EXTENDED(mat)) {
5845 sctp_log_mb(mat, SCTP_MBUF_INPUT);
5850 #ifdef SCTP_PACKET_LOGGING
5851 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
5852 sctp_packet_log(m, mlen);
5855 * Must take out the iphlen, since mlen expects this (only effect lb
5861 * Get IP, SCTP, and first chunk header together in first mbuf.
5863 ip = mtod(m, struct ip *);
5864 offset = iphlen + sizeof(*sh) + sizeof(*ch);
5865 if (SCTP_BUF_LEN(m) < offset) {
5866 if ((m = m_pullup(m, offset)) == 0) {
5867 SCTP_STAT_INCR(sctps_hdrops);
5870 ip = mtod(m, struct ip *);
5872 /* validate mbuf chain length with IP payload length */
5873 if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) {
5874 SCTP_STAT_INCR(sctps_hdrops);
5877 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
5878 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh));
5879 SCTPDBG(SCTP_DEBUG_INPUT1,
5880 "sctp_input() length:%d iphlen:%d\n", mlen, iphlen);
5882 /* SCTP does not allow broadcasts or multicasts */
5883 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
5886 if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) {
5888 * We only look at broadcast if its a front state, All
5889 * others we will not have a tcb for anyway.
5893 /* validate SCTP checksum */
5894 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
5895 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
5897 if_name(m->m_pkthdr.rcvif),
5898 m->m_pkthdr.csum_flags);
5899 #if defined(SCTP_WITH_NO_CSUM)
5900 SCTP_STAT_INCR(sctps_recvnocrc);
5902 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5903 SCTP_STAT_INCR(sctps_recvhwcrc);
5904 goto sctp_skip_csum_4;
5906 check = sh->checksum; /* save incoming checksum */
5907 sh->checksum = 0; /* prepare for calc */
5908 calc_check = sctp_calculate_cksum(m, iphlen);
5909 sh->checksum = check;
5910 SCTP_STAT_INCR(sctps_recvswcrc);
5911 if (calc_check != check) {
5912 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5913 calc_check, check, m, mlen, iphlen);
5915 stcb = sctp_findassociation_addr(m,
5916 offset - sizeof(*ch),
5919 if ((net) && (port)) {
5920 if (net->port == 0) {
5921 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5925 if ((net != NULL) && (m->m_flags & M_FLOWID)) {
5926 net->flowid = m->m_pkthdr.flowid;
5931 if ((inp) && (stcb)) {
5932 sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
5933 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5934 } else if ((inp != NULL) && (stcb == NULL)) {
5937 SCTP_STAT_INCR(sctps_badsum);
5938 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5943 /* destination port of 0 is illegal, based on RFC2960. */
5944 if (sh->dest_port == 0) {
5945 SCTP_STAT_INCR(sctps_hdrops);
5949 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
5950 * IP/SCTP/first chunk header...
5952 stcb = sctp_findassociation_addr(m, offset - sizeof(*ch),
5953 sh, ch, &inp, &net, vrf_id);
5954 if ((net) && (port)) {
5955 if (net->port == 0) {
5956 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5960 if ((net != NULL) && (m->m_flags & M_FLOWID)) {
5961 net->flowid = m->m_pkthdr.flowid;
5966 /* inp's ref-count increased && stcb locked */
5968 struct sctp_init_chunk *init_chk, chunk_buf;
5970 SCTP_STAT_INCR(sctps_noport);
5973 * we use the bandwidth limiting to protect against sending
5974 * too many ABORTS all at once. In this case these count the
5975 * same as an ICMP message.
5977 if (badport_bandlim(0) < 0)
5979 #endif /* ICMP_BANDLIM */
5980 SCTPDBG(SCTP_DEBUG_INPUT1,
5981 "Sending a ABORT from packet entry!\n");
5982 if (ch->chunk_type == SCTP_INITIATION) {
5984 * we do a trick here to get the INIT tag, dig in
5985 * and get the tag from the INIT and put it in the
5988 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
5989 iphlen + sizeof(*sh), sizeof(*init_chk),
5990 (uint8_t *) & chunk_buf);
5991 if (init_chk != NULL)
5992 sh->v_tag = init_chk->init.initiate_tag;
5994 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5995 sctp_send_shutdown_complete2(m, sh, vrf_id, port);
5998 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
6001 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
6002 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
6003 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
6004 (ch->chunk_type != SCTP_INIT))) {
6005 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
6009 } else if (stcb == NULL) {
6014 * I very much doubt any of the IPSEC stuff will work but I have no
6015 * idea, so I will leave it in place.
6017 if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) {
6018 MODULE_GLOBAL(ipsec4stat).in_polvio++;
6019 SCTP_STAT_INCR(sctps_hdrops);
6025 * common chunk processing
6027 length = ip->ip_len + iphlen;
6028 offset -= sizeof(struct sctp_chunkhdr);
6030 ecn_bits = ip->ip_tos;
6032 /* sa_ignore NO_NULL_CHK */
6033 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
6034 inp, stcb, net, ecn_bits, vrf_id, port);
6035 /* inp's ref-count reduced && stcb unlocked */
6039 if ((inp) && (refcount_up)) {
6040 /* reduce ref-count */
6041 SCTP_INP_DECR_REF(inp);
6046 SCTP_TCB_UNLOCK(stcb);
6048 if ((inp) && (refcount_up)) {
6049 /* reduce ref-count */
6050 SCTP_INP_DECR_REF(inp);
6058 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6059 extern int *sctp_cpuarry;
6064 sctp_input(struct mbuf *m, int off)
6066 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6071 uint32_t flowid, tag;
6074 if (m->m_flags & M_FLOWID) {
6075 flowid = m->m_pkthdr.flowid;
6078 * No flow id built by lower layers fix it so we
6081 ip = mtod(m, struct ip *);
6082 offset = off + sizeof(*sh);
6083 if (SCTP_BUF_LEN(m) < offset) {
6084 if ((m = m_pullup(m, offset)) == 0) {
6085 SCTP_STAT_INCR(sctps_hdrops);
6088 ip = mtod(m, struct ip *);
6090 sh = (struct sctphdr *)((caddr_t)ip + off);
6091 tag = htonl(sh->v_tag);
6092 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6093 m->m_pkthdr.flowid = flowid;
6094 m->m_flags |= M_FLOWID;
6096 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6097 sctp_queue_to_mcore(m, off, cpu_to_use);
6101 sctp_input_with_port(m, off, 0);