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, 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 *abort,
743 struct sctp_tcb *stcb, struct sctp_nets *net)
745 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
752 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
756 len = ntohs(abort->ch.chunk_length);
757 if (len > sizeof(struct sctp_chunkhdr)) {
759 * Need to check the cause codes for our two magic nat
760 * aborts which don't kill the assoc necessarily.
762 struct sctp_missing_nat_state *natc;
764 natc = (struct sctp_missing_nat_state *)(abort + 1);
765 error = ntohs(natc->cause);
766 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
767 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
768 abort->ch.chunk_flags);
769 if (sctp_handle_nat_colliding_state(stcb)) {
772 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
773 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
774 abort->ch.chunk_flags);
775 if (sctp_handle_nat_missing_state(stcb, net)) {
782 /* stop any receive timers */
783 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
784 /* notify user of the abort and clean up... */
785 sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
787 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
788 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
789 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
790 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
792 #ifdef SCTP_ASOCLOG_OF_TSNS
793 sctp_print_out_track_log(stcb);
795 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
796 so = SCTP_INP_SO(stcb->sctp_ep);
797 atomic_add_int(&stcb->asoc.refcnt, 1);
798 SCTP_TCB_UNLOCK(stcb);
799 SCTP_SOCKET_LOCK(so, 1);
801 atomic_subtract_int(&stcb->asoc.refcnt, 1);
803 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
804 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
805 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
806 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
807 SCTP_SOCKET_UNLOCK(so, 1);
809 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
813 sctp_start_net_timers(struct sctp_tcb *stcb)
815 uint32_t cnt_hb_sent;
816 struct sctp_nets *net;
819 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
821 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
822 * If the dest in unconfirmed send a hb as well if under
823 * max_hb_burst have been sent.
825 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
826 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
827 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
828 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
829 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
834 sctp_chunk_output(stcb->sctp_ep, stcb,
835 SCTP_OUTPUT_FROM_COOKIE_ACK,
842 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
843 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
845 struct sctp_association *asoc;
846 int some_on_streamwheel;
848 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
853 SCTPDBG(SCTP_DEBUG_INPUT2,
854 "sctp_handle_shutdown: handling SHUTDOWN\n");
858 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
859 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
862 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
863 /* Shutdown NOT the expected size */
866 sctp_update_acked(stcb, cp, abort_flag);
871 if (asoc->control_pdapi) {
873 * With a normal shutdown we assume the end of last record.
875 SCTP_INP_READ_LOCK(stcb->sctp_ep);
876 asoc->control_pdapi->end_added = 1;
877 asoc->control_pdapi->pdapi_aborted = 1;
878 asoc->control_pdapi = NULL;
879 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
880 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
881 so = SCTP_INP_SO(stcb->sctp_ep);
882 atomic_add_int(&stcb->asoc.refcnt, 1);
883 SCTP_TCB_UNLOCK(stcb);
884 SCTP_SOCKET_LOCK(so, 1);
886 atomic_subtract_int(&stcb->asoc.refcnt, 1);
887 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
888 /* assoc was freed while we were unlocked */
889 SCTP_SOCKET_UNLOCK(so, 1);
893 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
894 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
895 SCTP_SOCKET_UNLOCK(so, 1);
898 /* goto SHUTDOWN_RECEIVED state to block new requests */
899 if (stcb->sctp_socket) {
900 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
901 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
902 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
903 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
904 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
906 * notify upper layer that peer has initiated a
909 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
912 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
915 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
917 * stop the shutdown timer, since we WILL move to
920 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
922 /* Now is there unsent data on a stream somewhere? */
923 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
925 if (!TAILQ_EMPTY(&asoc->send_queue) ||
926 !TAILQ_EMPTY(&asoc->sent_queue) ||
927 some_on_streamwheel) {
928 /* By returning we will push more data out */
931 /* no outstanding data to send, so move on... */
932 /* send SHUTDOWN-ACK */
933 sctp_send_shutdown_ack(stcb, net);
934 /* move to SHUTDOWN-ACK-SENT state */
935 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
936 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
937 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
939 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
940 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
941 sctp_stop_timers_for_shutdown(stcb);
942 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
948 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
949 struct sctp_tcb *stcb,
950 struct sctp_nets *net)
952 struct sctp_association *asoc;
954 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
957 so = SCTP_INP_SO(stcb->sctp_ep);
959 SCTPDBG(SCTP_DEBUG_INPUT2,
960 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
965 /* process according to association state */
966 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
967 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
968 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
969 sctp_send_shutdown_complete(stcb, net, 1);
970 SCTP_TCB_UNLOCK(stcb);
973 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
974 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
975 /* unexpected SHUTDOWN-ACK... so ignore... */
976 SCTP_TCB_UNLOCK(stcb);
979 if (asoc->control_pdapi) {
981 * With a normal shutdown we assume the end of last record.
983 SCTP_INP_READ_LOCK(stcb->sctp_ep);
984 asoc->control_pdapi->end_added = 1;
985 asoc->control_pdapi->pdapi_aborted = 1;
986 asoc->control_pdapi = NULL;
987 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
988 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
989 atomic_add_int(&stcb->asoc.refcnt, 1);
990 SCTP_TCB_UNLOCK(stcb);
991 SCTP_SOCKET_LOCK(so, 1);
993 atomic_subtract_int(&stcb->asoc.refcnt, 1);
994 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
995 /* assoc was freed while we were unlocked */
996 SCTP_SOCKET_UNLOCK(so, 1);
1000 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1001 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1002 SCTP_SOCKET_UNLOCK(so, 1);
1005 /* are the queues empty? */
1006 if (!TAILQ_EMPTY(&asoc->send_queue) ||
1007 !TAILQ_EMPTY(&asoc->sent_queue) ||
1008 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
1009 sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED);
1011 /* stop the timer */
1012 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
1013 /* send SHUTDOWN-COMPLETE */
1014 sctp_send_shutdown_complete(stcb, net, 0);
1015 /* notify upper layer protocol */
1016 if (stcb->sctp_socket) {
1017 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1018 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1019 stcb->sctp_socket->so_snd.sb_cc = 0;
1021 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1023 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1024 /* free the TCB but first save off the ep */
1025 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1026 atomic_add_int(&stcb->asoc.refcnt, 1);
1027 SCTP_TCB_UNLOCK(stcb);
1028 SCTP_SOCKET_LOCK(so, 1);
1029 SCTP_TCB_LOCK(stcb);
1030 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1032 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1033 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1034 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1035 SCTP_SOCKET_UNLOCK(so, 1);
1040 * Skip past the param header and then we will find the chunk that caused the
1041 * problem. There are two possiblities ASCONF or FWD-TSN other than that and
1042 * our peer must be broken.
1045 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1046 struct sctp_nets *net)
1048 struct sctp_chunkhdr *chk;
1050 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1051 switch (chk->chunk_type) {
1052 case SCTP_ASCONF_ACK:
1054 sctp_asconf_cleanup(stcb, net);
1056 case SCTP_FORWARD_CUM_TSN:
1057 stcb->asoc.peer_supports_prsctp = 0;
1060 SCTPDBG(SCTP_DEBUG_INPUT2,
1061 "Peer does not support chunk type %d(%x)??\n",
1062 chk->chunk_type, (uint32_t) chk->chunk_type);
1068 * Skip past the param header and then we will find the param that caused the
1069 * problem. There are a number of param's in a ASCONF OR the prsctp param
1070 * these will turn of specific features.
1073 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1075 struct sctp_paramhdr *pbad;
1078 switch (ntohs(pbad->param_type)) {
1080 case SCTP_PRSCTP_SUPPORTED:
1081 stcb->asoc.peer_supports_prsctp = 0;
1083 case SCTP_SUPPORTED_CHUNK_EXT:
1085 /* draft-ietf-tsvwg-addip-sctp */
1086 case SCTP_HAS_NAT_SUPPORT:
1087 stcb->asoc.peer_supports_nat = 0;
1089 case SCTP_ADD_IP_ADDRESS:
1090 case SCTP_DEL_IP_ADDRESS:
1091 case SCTP_SET_PRIM_ADDR:
1092 stcb->asoc.peer_supports_asconf = 0;
1094 case SCTP_SUCCESS_REPORT:
1095 case SCTP_ERROR_CAUSE_IND:
1096 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1097 SCTPDBG(SCTP_DEBUG_INPUT2,
1098 "Turning off ASCONF to this strange peer\n");
1099 stcb->asoc.peer_supports_asconf = 0;
1102 SCTPDBG(SCTP_DEBUG_INPUT2,
1103 "Peer does not support param type %d(%x)??\n",
1104 pbad->param_type, (uint32_t) pbad->param_type);
1110 sctp_handle_error(struct sctp_chunkhdr *ch,
1111 struct sctp_tcb *stcb, struct sctp_nets *net)
1114 struct sctp_paramhdr *phdr;
1115 uint16_t error_type;
1117 struct sctp_association *asoc;
1120 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1125 /* parse through all of the errors and process */
1127 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1128 sizeof(struct sctp_chunkhdr));
1129 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1130 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1131 /* Process an Error Cause */
1132 error_type = ntohs(phdr->param_type);
1133 error_len = ntohs(phdr->param_length);
1134 if ((error_len > chklen) || (error_len == 0)) {
1135 /* invalid param length for this param */
1136 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1140 switch (error_type) {
1141 case SCTP_CAUSE_INVALID_STREAM:
1142 case SCTP_CAUSE_MISSING_PARAM:
1143 case SCTP_CAUSE_INVALID_PARAM:
1144 case SCTP_CAUSE_NO_USER_DATA:
1145 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1148 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1149 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1151 if (sctp_handle_nat_colliding_state(stcb)) {
1155 case SCTP_CAUSE_NAT_MISSING_STATE:
1156 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1158 if (sctp_handle_nat_missing_state(stcb, net)) {
1162 case SCTP_CAUSE_STALE_COOKIE:
1164 * We only act if we have echoed a cookie and are
1167 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1170 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1171 /* Save the time doubled */
1172 asoc->cookie_preserve_req = ntohl(*p) << 1;
1173 asoc->stale_cookie_count++;
1174 if (asoc->stale_cookie_count >
1175 asoc->max_init_times) {
1176 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1177 /* now free the asoc */
1178 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1179 so = SCTP_INP_SO(stcb->sctp_ep);
1180 atomic_add_int(&stcb->asoc.refcnt, 1);
1181 SCTP_TCB_UNLOCK(stcb);
1182 SCTP_SOCKET_LOCK(so, 1);
1183 SCTP_TCB_LOCK(stcb);
1184 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1186 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1187 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1188 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1189 SCTP_SOCKET_UNLOCK(so, 1);
1193 /* blast back to INIT state */
1194 sctp_toss_old_cookies(stcb, &stcb->asoc);
1195 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1196 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1197 sctp_stop_all_cookie_timers(stcb);
1198 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1201 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1203 * Nothing we can do here, we don't do hostname
1204 * addresses so if the peer does not like my IPv6
1205 * (or IPv4 for that matter) it does not matter. If
1206 * they don't support that type of address, they can
1207 * NOT possibly get that packet type... i.e. with no
1208 * IPv6 you can't recieve a IPv6 packet. so we can
1209 * safely ignore this one. If we ever added support
1210 * for HOSTNAME Addresses, then we would need to do
1214 case SCTP_CAUSE_UNRECOG_CHUNK:
1215 sctp_process_unrecog_chunk(stcb, phdr, net);
1217 case SCTP_CAUSE_UNRECOG_PARAM:
1218 sctp_process_unrecog_param(stcb, phdr);
1220 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1222 * We ignore this since the timer will drive out a
1223 * new cookie anyway and there timer will drive us
1224 * to send a SHUTDOWN_COMPLETE. We can't send one
1225 * here since we don't have their tag.
1228 case SCTP_CAUSE_DELETING_LAST_ADDR:
1229 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1230 case SCTP_CAUSE_DELETING_SRC_ADDR:
1232 * We should NOT get these here, but in a
1235 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1238 case SCTP_CAUSE_OUT_OF_RESC:
1240 * And what, pray tell do we do with the fact that
1241 * the peer is out of resources? Not really sure we
1242 * could do anything but abort. I suspect this
1243 * should have came WITH an abort instead of in a
1248 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1252 adjust = SCTP_SIZE32(error_len);
1254 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1260 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1261 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1262 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id)
1264 struct sctp_init_ack *init_ack;
1265 struct mbuf *op_err;
1267 SCTPDBG(SCTP_DEBUG_INPUT2,
1268 "sctp_handle_init_ack: handling INIT-ACK\n");
1271 SCTPDBG(SCTP_DEBUG_INPUT2,
1272 "sctp_handle_init_ack: TCB is null\n");
1275 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1276 /* Invalid length */
1277 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1278 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1279 op_err, 0, net->port);
1280 *abort_no_unlock = 1;
1283 init_ack = &cp->init;
1284 /* validate parameters */
1285 if (init_ack->initiate_tag == 0) {
1286 /* protocol error... send an abort */
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 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1294 /* protocol error... send an abort */
1295 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1296 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1297 op_err, 0, net->port);
1298 *abort_no_unlock = 1;
1301 if (init_ack->num_inbound_streams == 0) {
1302 /* protocol error... send an abort */
1303 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1304 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1305 op_err, 0, net->port);
1306 *abort_no_unlock = 1;
1309 if (init_ack->num_outbound_streams == 0) {
1310 /* protocol error... send an abort */
1311 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1312 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1313 op_err, 0, net->port);
1314 *abort_no_unlock = 1;
1317 /* process according to association state... */
1318 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1319 case SCTP_STATE_COOKIE_WAIT:
1320 /* this is the expected state for this chunk */
1321 /* process the INIT-ACK parameters */
1322 if (stcb->asoc.primary_destination->dest_state &
1323 SCTP_ADDR_UNCONFIRMED) {
1325 * The primary is where we sent the INIT, we can
1326 * always consider it confirmed when the INIT-ACK is
1327 * returned. Do this before we load addresses
1330 stcb->asoc.primary_destination->dest_state &=
1331 ~SCTP_ADDR_UNCONFIRMED;
1332 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1333 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1335 if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb,
1336 net, abort_no_unlock, vrf_id) < 0) {
1337 /* error in parsing parameters */
1340 /* update our state */
1341 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1342 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1344 /* reset the RTO calc */
1345 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1346 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1347 stcb->asoc.overall_error_count,
1349 SCTP_FROM_SCTP_INPUT,
1352 stcb->asoc.overall_error_count = 0;
1353 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1355 * collapse the init timer back in case of a exponential
1358 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1361 * the send at the end of the inbound data processing will
1362 * cause the cookie to be sent
1365 case SCTP_STATE_SHUTDOWN_SENT:
1366 /* incorrect state... discard */
1368 case SCTP_STATE_COOKIE_ECHOED:
1369 /* incorrect state... discard */
1371 case SCTP_STATE_OPEN:
1372 /* incorrect state... discard */
1374 case SCTP_STATE_EMPTY:
1375 case SCTP_STATE_INUSE:
1377 /* incorrect state... discard */
1381 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1385 static struct sctp_tcb *
1386 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1387 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1388 struct sctp_inpcb *inp, struct sctp_nets **netp,
1389 struct sockaddr *init_src, int *notification,
1390 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1391 uint32_t vrf_id, uint16_t port);
1395 * handle a state cookie for an existing association m: input packet mbuf
1396 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1397 * "split" mbuf and the cookie signature does not exist offset: offset into
1398 * mbuf to the cookie-echo chunk
1400 static struct sctp_tcb *
1401 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1402 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1403 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1404 struct sockaddr *init_src, int *notification,
1405 uint32_t vrf_id, int auth_skipped, uint32_t auth_offset, uint32_t auth_len, uint16_t port)
1407 struct sctp_association *asoc;
1408 struct sctp_init_chunk *init_cp, init_buf;
1409 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1410 struct sctp_nets *net;
1411 struct mbuf *op_err;
1412 struct sctp_paramhdr *ph;
1413 int init_offset, initack_offset, i;
1419 /* I know that the TCB is non-NULL from the caller */
1421 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1422 if (asoc->cookie_how[how_indx] == 0)
1425 if (how_indx < sizeof(asoc->cookie_how)) {
1426 asoc->cookie_how[how_indx] = 1;
1428 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1429 /* SHUTDOWN came in after sending INIT-ACK */
1430 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1431 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1432 0, M_DONTWAIT, 1, MT_DATA);
1433 if (op_err == NULL) {
1438 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1439 ph = mtod(op_err, struct sctp_paramhdr *);
1440 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
1441 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1442 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag,
1444 if (how_indx < sizeof(asoc->cookie_how))
1445 asoc->cookie_how[how_indx] = 2;
1449 * find and validate the INIT chunk in the cookie (peer's info) the
1450 * INIT should start after the cookie-echo header struct (chunk
1451 * header, state cookie header struct)
1453 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1455 init_cp = (struct sctp_init_chunk *)
1456 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1457 (uint8_t *) & init_buf);
1458 if (init_cp == NULL) {
1459 /* could not pull a INIT chunk in cookie */
1462 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1466 * find and validate the INIT-ACK chunk in the cookie (my info) the
1467 * INIT-ACK follows the INIT chunk
1469 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1470 initack_cp = (struct sctp_init_ack_chunk *)
1471 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1472 (uint8_t *) & initack_buf);
1473 if (initack_cp == NULL) {
1474 /* could not pull INIT-ACK chunk in cookie */
1477 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1480 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1481 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1483 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1484 * to get into the OPEN state
1486 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1488 * Opps, this means that we somehow generated two vtag's
1489 * the same. I.e. we did:
1491 * <---INIT(tag=a)------
1492 * ----INIT-ACK(tag=t)-->
1493 * ----INIT(tag=t)------> *1
1494 * <---INIT-ACK(tag=a)---
1495 * <----CE(tag=t)------------- *2
1497 * At point *1 we should be generating a different
1498 * tag t'. Which means we would throw away the CE and send
1499 * ours instead. Basically this is case C (throw away side).
1501 if (how_indx < sizeof(asoc->cookie_how))
1502 asoc->cookie_how[how_indx] = 17;
1506 switch SCTP_GET_STATE
1508 case SCTP_STATE_COOKIE_WAIT:
1509 case SCTP_STATE_COOKIE_ECHOED:
1511 * INIT was sent but got a COOKIE_ECHO with the
1512 * correct tags... just accept it...but we must
1513 * process the init so that we can make sure we have
1514 * the right seq no's.
1516 /* First we must process the INIT !! */
1517 retval = sctp_process_init(init_cp, stcb);
1519 if (how_indx < sizeof(asoc->cookie_how))
1520 asoc->cookie_how[how_indx] = 3;
1523 /* we have already processed the INIT so no problem */
1524 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1525 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1526 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1527 /* update current state */
1528 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1529 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1531 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1533 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1534 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1535 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1536 stcb->sctp_ep, stcb, asoc->primary_destination);
1538 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1539 sctp_stop_all_cookie_timers(stcb);
1540 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1541 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1542 (inp->sctp_socket->so_qlimit == 0)
1544 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1549 * Here is where collision would go if we
1550 * did a connect() and instead got a
1551 * init/init-ack/cookie done before the
1552 * init-ack came back..
1554 stcb->sctp_ep->sctp_flags |=
1555 SCTP_PCB_FLAGS_CONNECTED;
1556 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1557 so = SCTP_INP_SO(stcb->sctp_ep);
1558 atomic_add_int(&stcb->asoc.refcnt, 1);
1559 SCTP_TCB_UNLOCK(stcb);
1560 SCTP_SOCKET_LOCK(so, 1);
1561 SCTP_TCB_LOCK(stcb);
1562 atomic_add_int(&stcb->asoc.refcnt, -1);
1563 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1564 SCTP_SOCKET_UNLOCK(so, 1);
1568 soisconnected(stcb->sctp_socket);
1569 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1570 SCTP_SOCKET_UNLOCK(so, 1);
1573 /* notify upper layer */
1574 *notification = SCTP_NOTIFY_ASSOC_UP;
1576 * since we did not send a HB make sure we don't
1579 net->hb_responded = 1;
1580 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1581 &cookie->time_entered,
1582 sctp_align_unsafe_makecopy,
1583 SCTP_RTT_FROM_NON_DATA);
1585 if (stcb->asoc.sctp_autoclose_ticks &&
1586 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1587 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1593 * we're in the OPEN state (or beyond), so peer must
1594 * have simply lost the COOKIE-ACK
1598 sctp_stop_all_cookie_timers(stcb);
1600 * We ignore the return code here.. not sure if we should
1601 * somehow abort.. but we do have an existing asoc. This
1602 * really should not fail.
1604 if (sctp_load_addresses_from_init(stcb, m,
1605 init_offset + sizeof(struct sctp_init_chunk),
1606 initack_offset, sh, init_src)) {
1607 if (how_indx < sizeof(asoc->cookie_how))
1608 asoc->cookie_how[how_indx] = 4;
1611 /* respond with a COOKIE-ACK */
1612 sctp_toss_old_cookies(stcb, asoc);
1613 sctp_send_cookie_ack(stcb);
1614 if (how_indx < sizeof(asoc->cookie_how))
1615 asoc->cookie_how[how_indx] = 5;
1618 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1619 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1620 cookie->tie_tag_my_vtag == 0 &&
1621 cookie->tie_tag_peer_vtag == 0) {
1623 * case C in Section 5.2.4 Table 2: XMOO silently discard
1625 if (how_indx < sizeof(asoc->cookie_how))
1626 asoc->cookie_how[how_indx] = 6;
1630 * If nat support, and the below and stcb is established, send back
1631 * a ABORT(colliding state) if we are established.
1633 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1634 (asoc->peer_supports_nat) &&
1635 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1636 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1637 (asoc->peer_vtag == 0)))) {
1639 * Special case - Peer's support nat. We may have two init's
1640 * that we gave out the same tag on since one was not
1641 * established.. i.e. we get INIT from host-1 behind the nat
1642 * and we respond tag-a, we get a INIT from host-2 behind
1643 * the nat and we get tag-a again. Then we bring up host-1
1644 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1645 * Now we have colliding state. We must send an abort here
1646 * with colliding state indication.
1648 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1649 0, M_DONTWAIT, 1, MT_DATA);
1650 if (op_err == NULL) {
1654 /* pre-reserve some space */
1656 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
1658 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
1660 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
1661 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1663 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1664 ph = mtod(op_err, struct sctp_paramhdr *);
1665 ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE);
1666 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1667 sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port);
1670 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1671 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1672 (asoc->peer_vtag == 0))) {
1674 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1675 * should be ok, re-accept peer info
1677 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1679 * Extension of case C. If we hit this, then the
1680 * random number generator returned the same vtag
1681 * when we first sent our INIT-ACK and when we later
1682 * sent our INIT. The side with the seq numbers that
1683 * are different will be the one that normnally
1684 * would have hit case C. This in effect "extends"
1685 * our vtags in this collision case to be 64 bits.
1686 * The same collision could occur aka you get both
1687 * vtag and seq number the same twice in a row.. but
1688 * is much less likely. If it did happen then we
1689 * would proceed through and bring up the assoc.. we
1690 * may end up with the wrong stream setup however..
1691 * which would be bad.. but there is no way to
1692 * tell.. until we send on a stream that does not
1695 if (how_indx < sizeof(asoc->cookie_how))
1696 asoc->cookie_how[how_indx] = 7;
1700 if (how_indx < sizeof(asoc->cookie_how))
1701 asoc->cookie_how[how_indx] = 8;
1702 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1703 sctp_stop_all_cookie_timers(stcb);
1705 * since we did not send a HB make sure we don't double
1708 net->hb_responded = 1;
1709 if (stcb->asoc.sctp_autoclose_ticks &&
1710 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1711 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1714 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1715 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1717 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1719 * Ok the peer probably discarded our data (if we
1720 * echoed a cookie+data). So anything on the
1721 * sent_queue should be marked for retransmit, we
1722 * may not get something to kick us so it COULD
1723 * still take a timeout to move these.. but it can't
1724 * hurt to mark them.
1726 struct sctp_tmit_chunk *chk;
1728 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1729 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1730 chk->sent = SCTP_DATAGRAM_RESEND;
1731 sctp_flight_size_decrease(chk);
1732 sctp_total_flight_decrease(stcb, chk);
1733 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1739 /* process the INIT info (peer's info) */
1740 retval = sctp_process_init(init_cp, stcb);
1742 if (how_indx < sizeof(asoc->cookie_how))
1743 asoc->cookie_how[how_indx] = 9;
1746 if (sctp_load_addresses_from_init(stcb, m,
1747 init_offset + sizeof(struct sctp_init_chunk),
1748 initack_offset, sh, init_src)) {
1749 if (how_indx < sizeof(asoc->cookie_how))
1750 asoc->cookie_how[how_indx] = 10;
1753 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1754 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1755 *notification = SCTP_NOTIFY_ASSOC_UP;
1757 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1758 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1759 (inp->sctp_socket->so_qlimit == 0)) {
1760 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1764 stcb->sctp_ep->sctp_flags |=
1765 SCTP_PCB_FLAGS_CONNECTED;
1766 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1767 so = SCTP_INP_SO(stcb->sctp_ep);
1768 atomic_add_int(&stcb->asoc.refcnt, 1);
1769 SCTP_TCB_UNLOCK(stcb);
1770 SCTP_SOCKET_LOCK(so, 1);
1771 SCTP_TCB_LOCK(stcb);
1772 atomic_add_int(&stcb->asoc.refcnt, -1);
1773 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1774 SCTP_SOCKET_UNLOCK(so, 1);
1778 soisconnected(stcb->sctp_socket);
1779 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1780 SCTP_SOCKET_UNLOCK(so, 1);
1783 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1784 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1786 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1787 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1788 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1789 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1791 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1793 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1794 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1795 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1796 stcb->sctp_ep, stcb, asoc->primary_destination);
1798 sctp_stop_all_cookie_timers(stcb);
1799 sctp_toss_old_cookies(stcb, asoc);
1800 sctp_send_cookie_ack(stcb);
1803 * only if we have retrans set do we do this. What
1804 * this call does is get only the COOKIE-ACK out and
1805 * then when we return the normal call to
1806 * sctp_chunk_output will get the retrans out behind
1809 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1811 if (how_indx < sizeof(asoc->cookie_how))
1812 asoc->cookie_how[how_indx] = 11;
1816 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1817 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1818 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1819 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1820 cookie->tie_tag_peer_vtag != 0) {
1821 struct sctpasochead *head;
1823 if (asoc->peer_supports_nat) {
1825 * This is a gross gross hack. just call the
1826 * cookie_new code since we are allowing a duplicate
1827 * association. I hope this works...
1829 return (sctp_process_cookie_new(m, iphlen, offset, sh, cookie, cookie_len,
1830 inp, netp, init_src, notification,
1831 auth_skipped, auth_offset, auth_len,
1835 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1838 if (how_indx < sizeof(asoc->cookie_how))
1839 asoc->cookie_how[how_indx] = 12;
1840 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1841 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1843 /* notify upper layer */
1844 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1845 atomic_add_int(&stcb->asoc.refcnt, 1);
1846 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1847 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1848 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1849 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1851 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1852 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1853 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1854 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1856 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1857 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1858 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1859 stcb->sctp_ep, stcb, asoc->primary_destination);
1861 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1862 /* move to OPEN state, if not in SHUTDOWN_SENT */
1863 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1865 asoc->pre_open_streams =
1866 ntohs(initack_cp->init.num_outbound_streams);
1867 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1868 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1869 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1871 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1873 asoc->str_reset_seq_in = asoc->init_seq_number;
1875 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1876 if (asoc->mapping_array) {
1877 memset(asoc->mapping_array, 0,
1878 asoc->mapping_array_size);
1880 if (asoc->nr_mapping_array) {
1881 memset(asoc->nr_mapping_array, 0,
1882 asoc->mapping_array_size);
1884 SCTP_TCB_UNLOCK(stcb);
1885 SCTP_INP_INFO_WLOCK();
1886 SCTP_INP_WLOCK(stcb->sctp_ep);
1887 SCTP_TCB_LOCK(stcb);
1888 atomic_add_int(&stcb->asoc.refcnt, -1);
1889 /* send up all the data */
1890 SCTP_TCB_SEND_LOCK(stcb);
1892 sctp_report_all_outbound(stcb, 1, SCTP_SO_NOT_LOCKED);
1893 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1894 stcb->asoc.strmout[i].stream_no = i;
1895 stcb->asoc.strmout[i].next_sequence_sent = 0;
1896 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1898 /* process the INIT-ACK info (my info) */
1899 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1900 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1902 /* pull from vtag hash */
1903 LIST_REMOVE(stcb, sctp_asocs);
1904 /* re-insert to new vtag position */
1905 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1906 SCTP_BASE_INFO(hashasocmark))];
1908 * put it in the bucket in the vtag hash of assoc's for the
1911 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1913 /* process the INIT info (peer's info) */
1914 SCTP_TCB_SEND_UNLOCK(stcb);
1915 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1916 SCTP_INP_INFO_WUNLOCK();
1918 retval = sctp_process_init(init_cp, stcb);
1920 if (how_indx < sizeof(asoc->cookie_how))
1921 asoc->cookie_how[how_indx] = 13;
1926 * since we did not send a HB make sure we don't double
1929 net->hb_responded = 1;
1931 if (sctp_load_addresses_from_init(stcb, m,
1932 init_offset + sizeof(struct sctp_init_chunk),
1933 initack_offset, sh, init_src)) {
1934 if (how_indx < sizeof(asoc->cookie_how))
1935 asoc->cookie_how[how_indx] = 14;
1939 /* respond with a COOKIE-ACK */
1940 sctp_stop_all_cookie_timers(stcb);
1941 sctp_toss_old_cookies(stcb, asoc);
1942 sctp_send_cookie_ack(stcb);
1943 if (how_indx < sizeof(asoc->cookie_how))
1944 asoc->cookie_how[how_indx] = 15;
1948 if (how_indx < sizeof(asoc->cookie_how))
1949 asoc->cookie_how[how_indx] = 16;
1950 /* all other cases... */
1956 * handle a state cookie for a new association m: input packet mbuf chain--
1957 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
1958 * and the cookie signature does not exist offset: offset into mbuf to the
1959 * cookie-echo chunk length: length of the cookie chunk to: where the init
1960 * was from returns a new TCB
1963 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1964 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1965 struct sctp_inpcb *inp, struct sctp_nets **netp,
1966 struct sockaddr *init_src, int *notification,
1967 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1968 uint32_t vrf_id, uint16_t port)
1970 struct sctp_tcb *stcb;
1971 struct sctp_init_chunk *init_cp, init_buf;
1972 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1973 struct sockaddr_storage sa_store;
1974 struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
1975 struct sctp_association *asoc;
1976 int init_offset, initack_offset, initack_limit;
1979 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
1982 struct sockaddr_in *sin;
1986 struct sockaddr_in6 *sin6;
1989 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1992 so = SCTP_INP_SO(inp);
1996 * find and validate the INIT chunk in the cookie (peer's info) the
1997 * INIT should start after the cookie-echo header struct (chunk
1998 * header, state cookie header struct)
2000 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2001 init_cp = (struct sctp_init_chunk *)
2002 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2003 (uint8_t *) & init_buf);
2004 if (init_cp == NULL) {
2005 /* could not pull a INIT chunk in cookie */
2006 SCTPDBG(SCTP_DEBUG_INPUT1,
2007 "process_cookie_new: could not pull INIT chunk hdr\n");
2010 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2011 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2014 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2016 * find and validate the INIT-ACK chunk in the cookie (my info) the
2017 * INIT-ACK follows the INIT chunk
2019 initack_cp = (struct sctp_init_ack_chunk *)
2020 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2021 (uint8_t *) & initack_buf);
2022 if (initack_cp == NULL) {
2023 /* could not pull INIT-ACK chunk in cookie */
2024 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2027 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2031 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2032 * "initack_limit" value. This is because the chk_length field
2033 * includes the length of the cookie, but the cookie is omitted when
2034 * the INIT and INIT_ACK are tacked onto the cookie...
2036 initack_limit = offset + cookie_len;
2039 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2044 * Here we do a trick, we set in NULL for the proc/thread argument.
2045 * We do this since in effect we only use the p argument when the
2046 * socket is unbound and we must do an implicit bind. Since we are
2047 * getting a cookie, we cannot be unbound.
2049 stcb = sctp_aloc_assoc(inp, init_src, &error,
2050 ntohl(initack_cp->init.initiate_tag), vrf_id,
2051 (struct thread *)NULL
2054 struct mbuf *op_err;
2056 /* memory problem? */
2057 SCTPDBG(SCTP_DEBUG_INPUT1,
2058 "process_cookie_new: no room for another TCB!\n");
2059 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2061 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2062 sh, op_err, vrf_id, port);
2065 /* get the correct sctp_nets */
2067 *netp = sctp_findnet(stcb, init_src);
2070 /* get scope variables out of cookie */
2071 asoc->ipv4_local_scope = cookie->ipv4_scope;
2072 asoc->site_scope = cookie->site_scope;
2073 asoc->local_scope = cookie->local_scope;
2074 asoc->loopback_scope = cookie->loopback_scope;
2076 if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2077 (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2078 struct mbuf *op_err;
2081 * Houston we have a problem. The EP changed while the
2082 * cookie was in flight. Only recourse is to abort the
2085 atomic_add_int(&stcb->asoc.refcnt, 1);
2086 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2087 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2088 sh, op_err, vrf_id, port);
2089 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2090 SCTP_TCB_UNLOCK(stcb);
2091 SCTP_SOCKET_LOCK(so, 1);
2092 SCTP_TCB_LOCK(stcb);
2094 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2095 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2096 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2097 SCTP_SOCKET_UNLOCK(so, 1);
2099 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2102 /* process the INIT-ACK info (my info) */
2103 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2104 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2105 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2106 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2107 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2108 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2109 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2110 asoc->str_reset_seq_in = asoc->init_seq_number;
2112 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2114 /* process the INIT info (peer's info) */
2116 retval = sctp_process_init(init_cp, stcb);
2120 atomic_add_int(&stcb->asoc.refcnt, 1);
2121 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2122 SCTP_TCB_UNLOCK(stcb);
2123 SCTP_SOCKET_LOCK(so, 1);
2124 SCTP_TCB_LOCK(stcb);
2126 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2127 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2128 SCTP_SOCKET_UNLOCK(so, 1);
2130 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2133 /* load all addresses */
2134 if (sctp_load_addresses_from_init(stcb, m,
2135 init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh,
2137 atomic_add_int(&stcb->asoc.refcnt, 1);
2138 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2139 SCTP_TCB_UNLOCK(stcb);
2140 SCTP_SOCKET_LOCK(so, 1);
2141 SCTP_TCB_LOCK(stcb);
2143 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
2144 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2145 SCTP_SOCKET_UNLOCK(so, 1);
2147 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2151 * verify any preceding AUTH chunk that was skipped
2153 /* pull the local authentication parameters from the cookie/init-ack */
2154 sctp_auth_get_cookie_params(stcb, m,
2155 initack_offset + sizeof(struct sctp_init_ack_chunk),
2156 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2158 struct sctp_auth_chunk *auth;
2160 auth = (struct sctp_auth_chunk *)
2161 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2162 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2163 /* auth HMAC failed, dump the assoc and packet */
2164 SCTPDBG(SCTP_DEBUG_AUTH1,
2165 "COOKIE-ECHO: AUTH failed\n");
2166 atomic_add_int(&stcb->asoc.refcnt, 1);
2167 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2168 SCTP_TCB_UNLOCK(stcb);
2169 SCTP_SOCKET_LOCK(so, 1);
2170 SCTP_TCB_LOCK(stcb);
2172 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2173 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2174 SCTP_SOCKET_UNLOCK(so, 1);
2176 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2179 /* remaining chunks checked... good to go */
2180 stcb->asoc.authenticated = 1;
2183 /* update current state */
2184 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2185 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2186 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2187 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2188 stcb->sctp_ep, stcb, asoc->primary_destination);
2190 sctp_stop_all_cookie_timers(stcb);
2191 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2192 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2195 * if we're doing ASCONFs, check to see if we have any new local
2196 * addresses that need to get added to the peer (eg. addresses
2197 * changed while cookie echo in flight). This needs to be done
2198 * after we go to the OPEN state to do the correct asconf
2199 * processing. else, make sure we have the correct addresses in our
2203 /* warning, we re-use sin, sin6, sa_store here! */
2204 /* pull in local_address (our "from" address) */
2205 switch (cookie->laddr_type) {
2207 case SCTP_IPV4_ADDRESS:
2208 /* source addr is IPv4 */
2209 sin = (struct sockaddr_in *)initack_src;
2210 memset(sin, 0, sizeof(*sin));
2211 sin->sin_family = AF_INET;
2212 sin->sin_len = sizeof(struct sockaddr_in);
2213 sin->sin_addr.s_addr = cookie->laddress[0];
2217 case SCTP_IPV6_ADDRESS:
2218 /* source addr is IPv6 */
2219 sin6 = (struct sockaddr_in6 *)initack_src;
2220 memset(sin6, 0, sizeof(*sin6));
2221 sin6->sin6_family = AF_INET6;
2222 sin6->sin6_len = sizeof(struct sockaddr_in6);
2223 sin6->sin6_scope_id = cookie->scope_id;
2224 memcpy(&sin6->sin6_addr, cookie->laddress,
2225 sizeof(sin6->sin6_addr));
2229 atomic_add_int(&stcb->asoc.refcnt, 1);
2230 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2231 SCTP_TCB_UNLOCK(stcb);
2232 SCTP_SOCKET_LOCK(so, 1);
2233 SCTP_TCB_LOCK(stcb);
2235 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2236 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2237 SCTP_SOCKET_UNLOCK(so, 1);
2239 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2243 /* set up to notify upper layer */
2244 *notification = SCTP_NOTIFY_ASSOC_UP;
2245 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2246 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2247 (inp->sctp_socket->so_qlimit == 0)) {
2249 * This is an endpoint that called connect() how it got a
2250 * cookie that is NEW is a bit of a mystery. It must be that
2251 * the INIT was sent, but before it got there.. a complete
2252 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2253 * should have went to the other code.. not here.. oh well..
2254 * a bit of protection is worth having..
2256 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2257 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2258 atomic_add_int(&stcb->asoc.refcnt, 1);
2259 SCTP_TCB_UNLOCK(stcb);
2260 SCTP_SOCKET_LOCK(so, 1);
2261 SCTP_TCB_LOCK(stcb);
2262 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2263 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2264 SCTP_SOCKET_UNLOCK(so, 1);
2268 soisconnected(stcb->sctp_socket);
2269 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2270 SCTP_SOCKET_UNLOCK(so, 1);
2272 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2273 (inp->sctp_socket->so_qlimit)) {
2275 * We don't want to do anything with this one. Since it is
2276 * the listening guy. The timer will get started for
2277 * accepted connections in the caller.
2281 /* since we did not send a HB make sure we don't double things */
2282 if ((netp) && (*netp))
2283 (*netp)->hb_responded = 1;
2285 if (stcb->asoc.sctp_autoclose_ticks &&
2286 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2287 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2289 /* calculate the RTT */
2290 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2291 if ((netp) && (*netp)) {
2292 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2293 &cookie->time_entered, sctp_align_unsafe_makecopy,
2294 SCTP_RTT_FROM_NON_DATA);
2296 /* respond with a COOKIE-ACK */
2297 sctp_send_cookie_ack(stcb);
2300 * check the address lists for any ASCONFs that need to be sent
2301 * AFTER the cookie-ack is sent
2303 sctp_check_address_list(stcb, m,
2304 initack_offset + sizeof(struct sctp_init_ack_chunk),
2305 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2306 initack_src, cookie->local_scope, cookie->site_scope,
2307 cookie->ipv4_scope, cookie->loopback_scope);
2314 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2315 * we NEED to make sure we are not already using the vtag. If so we
2316 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2317 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2318 SCTP_BASE_INFO(hashasocmark))];
2319 LIST_FOREACH(stcb, head, sctp_asocs) {
2320 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2321 -- SEND ABORT - TRY AGAIN --
2327 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2328 * existing (non-NULL) TCB
2330 static struct mbuf *
2331 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2332 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2333 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2334 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2335 struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint16_t port)
2337 struct sctp_state_cookie *cookie;
2338 struct sctp_tcb *l_stcb = *stcb;
2339 struct sctp_inpcb *l_inp;
2340 struct sockaddr *to;
2341 struct sctp_pcb *ep;
2343 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2345 uint8_t cookie_ok = 0;
2346 unsigned int size_of_pkt, sig_offset, cookie_offset;
2347 unsigned int cookie_len;
2349 struct timeval time_expires;
2350 struct sockaddr_storage dest_store;
2351 struct sockaddr *localep_sa = (struct sockaddr *)&dest_store;
2353 int notification = 0;
2354 struct sctp_nets *netl;
2355 int had_a_existing_tcb = 0;
2356 int send_int_conf = 0;
2359 struct sockaddr_in sin;
2363 struct sockaddr_in6 sin6;
2367 SCTPDBG(SCTP_DEBUG_INPUT2,
2368 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2370 if (inp_p == NULL) {
2373 /* First get the destination address setup too. */
2374 iph = mtod(m, struct ip *);
2375 switch (iph->ip_v) {
2380 struct sockaddr_in *lsin;
2382 lsin = (struct sockaddr_in *)(localep_sa);
2383 memset(lsin, 0, sizeof(*lsin));
2384 lsin->sin_family = AF_INET;
2385 lsin->sin_len = sizeof(*lsin);
2386 lsin->sin_port = sh->dest_port;
2387 lsin->sin_addr.s_addr = iph->ip_dst.s_addr;
2388 size_of_pkt = SCTP_GET_IPV4_LENGTH(iph);
2393 case IPV6_VERSION >> 4:
2396 struct ip6_hdr *ip6;
2397 struct sockaddr_in6 *lsin6;
2399 lsin6 = (struct sockaddr_in6 *)(localep_sa);
2400 memset(lsin6, 0, sizeof(*lsin6));
2401 lsin6->sin6_family = AF_INET6;
2402 lsin6->sin6_len = sizeof(struct sockaddr_in6);
2403 ip6 = mtod(m, struct ip6_hdr *);
2404 lsin6->sin6_port = sh->dest_port;
2405 lsin6->sin6_addr = ip6->ip6_dst;
2406 size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen;
2414 cookie = &cp->cookie;
2415 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2416 cookie_len = ntohs(cp->ch.chunk_length);
2418 if ((cookie->peerport != sh->src_port) &&
2419 (cookie->myport != sh->dest_port) &&
2420 (cookie->my_vtag != sh->v_tag)) {
2422 * invalid ports or bad tag. Note that we always leave the
2423 * v_tag in the header in network order and when we stored
2424 * it in the my_vtag slot we also left it in network order.
2425 * This maintains the match even though it may be in the
2426 * opposite byte order of the machine :->
2430 if (cookie_len > size_of_pkt ||
2431 cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2432 sizeof(struct sctp_init_chunk) +
2433 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2434 /* cookie too long! or too small */
2438 * split off the signature into its own mbuf (since it should not be
2439 * calculated in the sctp_hmac_m() call).
2441 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2442 if (sig_offset > size_of_pkt) {
2443 /* packet not correct size! */
2444 /* XXX this may already be accounted for earlier... */
2447 m_sig = m_split(m, sig_offset, M_DONTWAIT);
2448 if (m_sig == NULL) {
2449 /* out of memory or ?? */
2452 #ifdef SCTP_MBUF_LOGGING
2453 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2456 for (mat = m_sig; mat; mat = SCTP_BUF_NEXT(mat)) {
2457 if (SCTP_BUF_IS_EXTENDED(mat)) {
2458 sctp_log_mb(mat, SCTP_MBUF_SPLIT);
2465 * compute the signature/digest for the cookie
2467 ep = &(*inp_p)->sctp_ep;
2470 SCTP_TCB_UNLOCK(l_stcb);
2472 SCTP_INP_RLOCK(l_inp);
2474 SCTP_TCB_LOCK(l_stcb);
2476 /* which cookie is it? */
2477 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2478 (ep->current_secret_number != ep->last_secret_number)) {
2479 /* it's the old cookie */
2480 (void)sctp_hmac_m(SCTP_HMAC,
2481 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2482 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2484 /* it's the current cookie */
2485 (void)sctp_hmac_m(SCTP_HMAC,
2486 (uint8_t *) ep->secret_key[(int)ep->current_secret_number],
2487 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2489 /* get the signature */
2490 SCTP_INP_RUNLOCK(l_inp);
2491 sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig);
2493 /* couldn't find signature */
2494 sctp_m_freem(m_sig);
2497 /* compare the received digest with the computed digest */
2498 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2499 /* try the old cookie? */
2500 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2501 (ep->current_secret_number != ep->last_secret_number)) {
2502 /* compute digest with old */
2503 (void)sctp_hmac_m(SCTP_HMAC,
2504 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2505 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2507 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2515 * Now before we continue we must reconstruct our mbuf so that
2516 * normal processing of any other chunks will work.
2522 while (SCTP_BUF_NEXT(m_at) != NULL) {
2523 m_at = SCTP_BUF_NEXT(m_at);
2525 SCTP_BUF_NEXT(m_at) = m_sig;
2528 if (cookie_ok == 0) {
2529 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2530 SCTPDBG(SCTP_DEBUG_INPUT2,
2531 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2532 (uint32_t) offset, cookie_offset, sig_offset);
2536 * check the cookie timestamps to be sure it's not stale
2538 (void)SCTP_GETTIME_TIMEVAL(&now);
2539 /* Expire time is in Ticks, so we convert to seconds */
2540 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2541 time_expires.tv_usec = cookie->time_entered.tv_usec;
2543 * TODO sctp_constants.h needs alternative time macros when _KERNEL
2546 if (timevalcmp(&now, &time_expires, >)) {
2547 /* cookie is stale! */
2548 struct mbuf *op_err;
2549 struct sctp_stale_cookie_msg *scm;
2552 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg),
2553 0, M_DONTWAIT, 1, MT_DATA);
2554 if (op_err == NULL) {
2559 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg);
2560 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
2561 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
2562 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
2563 (sizeof(uint32_t))));
2564 /* seconds to usec */
2565 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2568 tim = now.tv_usec - cookie->time_entered.tv_usec;
2569 scm->time_usec = htonl(tim);
2570 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag,
2575 * Now we must see with the lookup address if we have an existing
2576 * asoc. This will only happen if we were in the COOKIE-WAIT state
2577 * and a INIT collided with us and somewhere the peer sent the
2578 * cookie on another address besides the single address our assoc
2579 * had for him. In this case we will have one of the tie-tags set at
2580 * least AND the address field in the cookie can be used to look it
2584 switch (cookie->addr_type) {
2586 case SCTP_IPV6_ADDRESS:
2587 memset(&sin6, 0, sizeof(sin6));
2588 sin6.sin6_family = AF_INET6;
2589 sin6.sin6_len = sizeof(sin6);
2590 sin6.sin6_port = sh->src_port;
2591 sin6.sin6_scope_id = cookie->scope_id;
2592 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2593 sizeof(sin6.sin6_addr.s6_addr));
2594 to = (struct sockaddr *)&sin6;
2598 case SCTP_IPV4_ADDRESS:
2599 memset(&sin, 0, sizeof(sin));
2600 sin.sin_family = AF_INET;
2601 sin.sin_len = sizeof(sin);
2602 sin.sin_port = sh->src_port;
2603 sin.sin_addr.s_addr = cookie->address[0];
2604 to = (struct sockaddr *)&sin;
2608 /* This should not happen */
2611 if ((*stcb == NULL) && to) {
2612 /* Yep, lets check */
2613 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL);
2614 if (*stcb == NULL) {
2616 * We should have only got back the same inp. If we
2617 * got back a different ep we have a problem. The
2618 * original findep got back l_inp and now
2620 if (l_inp != *inp_p) {
2621 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2624 if (*locked_tcb == NULL) {
2626 * In this case we found the assoc only
2627 * after we locked the create lock. This
2628 * means we are in a colliding case and we
2629 * must make sure that we unlock the tcb if
2630 * its one of the cases where we throw away
2631 * the incoming packets.
2633 *locked_tcb = *stcb;
2636 * We must also increment the inp ref count
2637 * since the ref_count flags was set when we
2638 * did not find the TCB, now we found it
2639 * which reduces the refcount.. we must
2640 * raise it back out to balance it all :-)
2642 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2643 if ((*stcb)->sctp_ep != l_inp) {
2644 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2645 (*stcb)->sctp_ep, l_inp);
2653 cookie_len -= SCTP_SIGNATURE_SIZE;
2654 if (*stcb == NULL) {
2655 /* this is the "normal" case... get a new TCB */
2656 *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie,
2657 cookie_len, *inp_p, netp, to, ¬ification,
2658 auth_skipped, auth_offset, auth_len, vrf_id, port);
2660 /* this is abnormal... cookie-echo on existing TCB */
2661 had_a_existing_tcb = 1;
2662 *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh,
2663 cookie, cookie_len, *inp_p, *stcb, netp, to,
2664 ¬ification, vrf_id, auth_skipped, auth_offset, auth_len, port);
2667 if (*stcb == NULL) {
2668 /* still no TCB... must be bad cookie-echo */
2671 if ((*netp != NULL) && (m->m_flags & M_FLOWID)) {
2672 (*netp)->flowid = m->m_pkthdr.flowid;
2674 (*netp)->flowidset = 1;
2678 * Ok, we built an association so confirm the address we sent the
2681 netl = sctp_findnet(*stcb, to);
2683 * This code should in theory NOT run but
2686 /* TSNH! Huh, why do I need to add this address here? */
2687 if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2690 netl = sctp_findnet(*stcb, to);
2693 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2694 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2695 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2700 sctp_start_net_timers(*stcb);
2701 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2702 if (!had_a_existing_tcb ||
2703 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2705 * If we have a NEW cookie or the connect never
2706 * reached the connected state during collision we
2707 * must do the TCP accept thing.
2709 struct socket *so, *oso;
2710 struct sctp_inpcb *inp;
2712 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2714 * For a restart we will keep the same
2715 * socket, no need to do anything. I THINK!!
2717 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2718 if (send_int_conf) {
2719 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2720 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2724 oso = (*inp_p)->sctp_socket;
2725 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2726 SCTP_TCB_UNLOCK((*stcb));
2727 CURVNET_SET(oso->so_vnet);
2728 so = sonewconn(oso, 0
2731 SCTP_TCB_LOCK((*stcb));
2732 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2735 struct mbuf *op_err;
2737 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2738 struct socket *pcb_so;
2741 /* Too many sockets */
2742 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2743 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2744 sctp_abort_association(*inp_p, NULL, m, iphlen,
2745 sh, op_err, vrf_id, port);
2746 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2747 pcb_so = SCTP_INP_SO(*inp_p);
2748 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2749 SCTP_TCB_UNLOCK((*stcb));
2750 SCTP_SOCKET_LOCK(pcb_so, 1);
2751 SCTP_TCB_LOCK((*stcb));
2752 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2754 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2755 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2756 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2760 inp = (struct sctp_inpcb *)so->so_pcb;
2761 SCTP_INP_INCR_REF(inp);
2763 * We add the unbound flag here so that if we get an
2764 * soabort() before we get the move_pcb done, we
2765 * will properly cleanup.
2767 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2768 SCTP_PCB_FLAGS_CONNECTED |
2769 SCTP_PCB_FLAGS_IN_TCPPOOL |
2770 SCTP_PCB_FLAGS_UNBOUND |
2771 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2772 SCTP_PCB_FLAGS_DONT_WAKE);
2773 inp->sctp_features = (*inp_p)->sctp_features;
2774 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2775 inp->sctp_socket = so;
2776 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2777 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2778 inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable;
2779 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2780 inp->sctp_context = (*inp_p)->sctp_context;
2781 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2782 inp->inp_starting_point_for_iterator = NULL;
2784 * copy in the authentication parameters from the
2787 if (inp->sctp_ep.local_hmacs)
2788 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2789 inp->sctp_ep.local_hmacs =
2790 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2791 if (inp->sctp_ep.local_auth_chunks)
2792 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2793 inp->sctp_ep.local_auth_chunks =
2794 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2797 * Now we must move it from one hash table to
2798 * another and get the tcb in the right place.
2802 * This is where the one-2-one socket is put into
2803 * the accept state waiting for the accept!
2806 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2808 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2810 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2811 SCTP_TCB_UNLOCK((*stcb));
2813 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2815 SCTP_TCB_LOCK((*stcb));
2816 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2820 * now we must check to see if we were aborted while
2821 * the move was going on and the lock/unlock
2824 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2826 * yep it was, we leave the assoc attached
2827 * to the socket since the sctp_inpcb_free()
2828 * call will send an abort for us.
2830 SCTP_INP_DECR_REF(inp);
2833 SCTP_INP_DECR_REF(inp);
2834 /* Switch over to the new guy */
2836 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2837 if (send_int_conf) {
2838 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2839 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2842 * Pull it from the incomplete queue and wake the
2845 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2846 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2847 SCTP_TCB_UNLOCK((*stcb));
2848 SCTP_SOCKET_LOCK(so, 1);
2851 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2852 SCTP_TCB_LOCK((*stcb));
2853 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2854 SCTP_SOCKET_UNLOCK(so, 1);
2859 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
2861 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2863 if (send_int_conf) {
2864 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2865 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2872 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2873 struct sctp_tcb *stcb, struct sctp_nets *net)
2875 /* cp must not be used, others call this without a c-ack :-) */
2876 struct sctp_association *asoc;
2878 SCTPDBG(SCTP_DEBUG_INPUT2,
2879 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2885 sctp_stop_all_cookie_timers(stcb);
2886 /* process according to association state */
2887 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2888 /* state change only needed when I am in right state */
2889 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2890 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2891 sctp_start_net_timers(stcb);
2892 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2893 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2894 stcb->sctp_ep, stcb, asoc->primary_destination);
2898 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2899 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2900 if (asoc->overall_error_count == 0) {
2901 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2902 &asoc->time_entered, sctp_align_safe_nocopy,
2903 SCTP_RTT_FROM_NON_DATA);
2905 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2906 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2907 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2908 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2909 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2913 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2914 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2915 so = SCTP_INP_SO(stcb->sctp_ep);
2916 atomic_add_int(&stcb->asoc.refcnt, 1);
2917 SCTP_TCB_UNLOCK(stcb);
2918 SCTP_SOCKET_LOCK(so, 1);
2919 SCTP_TCB_LOCK(stcb);
2920 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2922 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2923 soisconnected(stcb->sctp_socket);
2925 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2926 SCTP_SOCKET_UNLOCK(so, 1);
2930 * since we did not send a HB make sure we don't double
2933 net->hb_responded = 1;
2935 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2937 * We don't need to do the asconf thing, nor hb or
2938 * autoclose if the socket is closed.
2942 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2946 if (stcb->asoc.sctp_autoclose_ticks &&
2947 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2948 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2949 stcb->sctp_ep, stcb, NULL);
2952 * send ASCONF if parameters are pending and ASCONFs are
2953 * allowed (eg. addresses changed when init/cookie echo were
2956 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2957 (stcb->asoc.peer_supports_asconf) &&
2958 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2959 #ifdef SCTP_TIMER_BASED_ASCONF
2960 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2961 stcb->sctp_ep, stcb,
2962 stcb->asoc.primary_destination);
2964 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2965 SCTP_ADDR_NOT_LOCKED);
2970 /* Toss the cookie if I can */
2971 sctp_toss_old_cookies(stcb, asoc);
2972 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
2973 /* Restart the timer if we have pending data */
2974 struct sctp_tmit_chunk *chk;
2976 chk = TAILQ_FIRST(&asoc->sent_queue);
2977 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
2982 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2983 struct sctp_tcb *stcb)
2985 struct sctp_nets *net;
2986 struct sctp_tmit_chunk *lchk;
2987 struct sctp_ecne_chunk bkup;
2988 uint8_t override_bit;
2989 uint32_t tsn, window_data_tsn;
2991 unsigned int pkt_cnt;
2993 len = ntohs(cp->ch.chunk_length);
2994 if ((len != sizeof(struct sctp_ecne_chunk)) &&
2995 (len != sizeof(struct old_sctp_ecne_chunk))) {
2998 if (len == sizeof(struct old_sctp_ecne_chunk)) {
2999 /* Its the old format */
3000 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3001 bkup.num_pkts_since_cwr = htonl(1);
3004 SCTP_STAT_INCR(sctps_recvecne);
3005 tsn = ntohl(cp->tsn);
3006 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3007 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3009 window_data_tsn = stcb->asoc.sending_seq - 1;
3011 window_data_tsn = lchk->rec.data.TSN_seq;
3014 /* Find where it was sent to if possible. */
3016 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3017 if (lchk->rec.data.TSN_seq == tsn) {
3019 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3022 if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) {
3028 * What to do. A previous send of a CWR was possibly lost.
3029 * See how old it is, we may have it marked on the actual
3032 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3033 if (tsn == net->last_cwr_tsn) {
3034 /* Found him, send it off */
3040 * If we reach here, we need to send a special CWR
3041 * that says hey, we did this a long time ago and
3042 * you lost the response.
3044 net = TAILQ_FIRST(&stcb->asoc.nets);
3049 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3056 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3057 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3059 * JRS - Use the congestion control given in the pluggable
3062 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3064 * We reduce once every RTT. So we will only lower cwnd at
3065 * the next sending seq i.e. the window_data_tsn
3067 net->cwr_window_tsn = window_data_tsn;
3068 net->ecn_ce_pkt_cnt += pkt_cnt;
3069 net->lost_cnt = pkt_cnt;
3070 net->last_cwr_tsn = tsn;
3072 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3073 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3074 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3076 * Another loss in the same window update how many
3077 * marks/packets lost we have had.
3081 if (pkt_cnt > net->lost_cnt) {
3082 /* Should be the case */
3083 cnt = (pkt_cnt - net->lost_cnt);
3084 net->ecn_ce_pkt_cnt += cnt;
3086 net->lost_cnt = pkt_cnt;
3087 net->last_cwr_tsn = tsn;
3089 * Most CC functions will ignore this call, since we
3090 * are in-window yet of the initial CE the peer saw.
3092 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3096 * We always send a CWR this way if our previous one was lost our
3097 * peer will get an update, or if it is not time again to reduce we
3098 * still get the cwr to the peer. Note we set the override when we
3099 * could not find the TSN on the chunk or the destination network.
3101 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3105 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3108 * Here we get a CWR from the peer. We must look in the outqueue and
3109 * make sure that we have a covered ECNE in the control chunk part.
3112 struct sctp_tmit_chunk *chk;
3113 struct sctp_ecne_chunk *ecne;
3117 cwr_tsn = ntohl(cp->tsn);
3119 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3120 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
3121 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3124 if ((override == 0) && (chk->whoTo != net)) {
3125 /* Must be from the right src unless override is set */
3128 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3129 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3130 /* this covers this ECNE, we can remove it */
3131 stcb->asoc.ecn_echo_cnt_onq--;
3132 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3135 sctp_m_freem(chk->data);
3138 stcb->asoc.ctrl_queue_cnt--;
3139 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3140 if (override == 0) {
3148 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3149 struct sctp_tcb *stcb, struct sctp_nets *net)
3151 struct sctp_association *asoc;
3153 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3158 SCTPDBG(SCTP_DEBUG_INPUT2,
3159 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3164 /* process according to association state */
3165 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3166 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3167 SCTPDBG(SCTP_DEBUG_INPUT2,
3168 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3169 SCTP_TCB_UNLOCK(stcb);
3172 /* notify upper layer protocol */
3173 if (stcb->sctp_socket) {
3174 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3175 /* are the queues empty? they should be */
3176 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3177 !TAILQ_EMPTY(&asoc->sent_queue) ||
3178 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
3179 sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED);
3182 /* stop the timer */
3183 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
3184 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3186 SCTPDBG(SCTP_DEBUG_INPUT2,
3187 "sctp_handle_shutdown_complete: calls free-asoc\n");
3188 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3189 so = SCTP_INP_SO(stcb->sctp_ep);
3190 atomic_add_int(&stcb->asoc.refcnt, 1);
3191 SCTP_TCB_UNLOCK(stcb);
3192 SCTP_SOCKET_LOCK(so, 1);
3193 SCTP_TCB_LOCK(stcb);
3194 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3196 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
3197 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3198 SCTP_SOCKET_UNLOCK(so, 1);
3204 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3205 struct sctp_nets *net, uint8_t flg)
3207 switch (desc->chunk_type) {
3209 /* find the tsn to resend (possibly */
3212 struct sctp_tmit_chunk *tp1;
3214 tsn = ntohl(desc->tsn_ifany);
3215 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3216 if (tp1->rec.data.TSN_seq == tsn) {
3220 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) {
3228 * Do it the other way , aka without paying
3229 * attention to queue seq order.
3231 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3232 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3233 if (tp1->rec.data.TSN_seq == tsn) {
3240 SCTP_STAT_INCR(sctps_pdrptsnnf);
3242 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3245 if (((flg & SCTP_BADCRC) == 0) &&
3246 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3249 if ((stcb->asoc.peers_rwnd == 0) &&
3250 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3251 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3254 if (stcb->asoc.peers_rwnd == 0 &&
3255 (flg & SCTP_FROM_MIDDLE_BOX)) {
3256 SCTP_STAT_INCR(sctps_pdrpdizrw);
3259 ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+
3260 sizeof(struct sctp_data_chunk));
3264 for (iii = 0; iii < sizeof(desc->data_bytes);
3266 if (ddp[iii] != desc->data_bytes[iii]) {
3267 SCTP_STAT_INCR(sctps_pdrpbadd);
3275 * this guy had a RTO calculation
3276 * pending on it, cancel it
3278 if (tp1->whoTo->rto_needed == 0) {
3279 tp1->whoTo->rto_needed = 1;
3283 SCTP_STAT_INCR(sctps_pdrpmark);
3284 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3285 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3287 * mark it as if we were doing a FR, since
3288 * we will be getting gap ack reports behind
3289 * the info from the router.
3291 tp1->rec.data.doing_fast_retransmit = 1;
3293 * mark the tsn with what sequences can
3296 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3297 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3299 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
3302 /* restart the timer */
3303 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3304 stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3305 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3308 /* fix counts and things */
3309 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3310 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3311 tp1->whoTo->flight_size,
3314 tp1->rec.data.TSN_seq);
3316 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3317 sctp_flight_size_decrease(tp1);
3318 sctp_total_flight_decrease(stcb, tp1);
3320 tp1->sent = SCTP_DATAGRAM_RESEND;
3326 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3327 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3330 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3332 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3335 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3336 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3337 audit, stcb->asoc.sent_queue_retran_cnt);
3338 #ifndef SCTP_AUDITING_ENABLED
3339 stcb->asoc.sent_queue_retran_cnt = audit;
3347 struct sctp_tmit_chunk *asconf;
3349 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3351 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3356 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3357 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3358 asconf->sent = SCTP_DATAGRAM_RESEND;
3359 asconf->snd_count--;
3363 case SCTP_INITIATION:
3364 /* resend the INIT */
3365 stcb->asoc.dropped_special_cnt++;
3366 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3368 * If we can get it in, in a few attempts we do
3369 * this, otherwise we let the timer fire.
3371 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3372 stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3373 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3376 case SCTP_SELECTIVE_ACK:
3377 case SCTP_NR_SELECTIVE_ACK:
3378 /* resend the sack */
3379 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3381 case SCTP_HEARTBEAT_REQUEST:
3382 /* resend a demand HB */
3383 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3385 * Only retransmit if we KNOW we wont destroy the
3388 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3392 sctp_send_shutdown(stcb, net);
3394 case SCTP_SHUTDOWN_ACK:
3395 sctp_send_shutdown_ack(stcb, net);
3397 case SCTP_COOKIE_ECHO:
3399 struct sctp_tmit_chunk *cookie;
3402 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3404 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3409 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3410 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3411 cookie->sent = SCTP_DATAGRAM_RESEND;
3412 sctp_stop_all_cookie_timers(stcb);
3416 case SCTP_COOKIE_ACK:
3417 sctp_send_cookie_ack(stcb);
3419 case SCTP_ASCONF_ACK:
3420 /* resend last asconf ack */
3421 sctp_send_asconf_ack(stcb);
3423 case SCTP_FORWARD_CUM_TSN:
3424 send_forward_tsn(stcb, &stcb->asoc);
3426 /* can't do anything with these */
3427 case SCTP_PACKET_DROPPED:
3428 case SCTP_INITIATION_ACK: /* this should not happen */
3429 case SCTP_HEARTBEAT_ACK:
3430 case SCTP_ABORT_ASSOCIATION:
3431 case SCTP_OPERATION_ERROR:
3432 case SCTP_SHUTDOWN_COMPLETE:
3442 sctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3448 * We set things to 0xffff since this is the last delivered sequence
3449 * and we will be sending in 0 after the reset.
3452 if (number_entries) {
3453 for (i = 0; i < number_entries; i++) {
3454 temp = ntohs(list[i]);
3455 if (temp >= stcb->asoc.streamincnt) {
3458 stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff;
3462 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3463 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3466 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3470 sctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3474 if (number_entries == 0) {
3475 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3476 stcb->asoc.strmout[i].next_sequence_sent = 0;
3478 } else if (number_entries) {
3479 for (i = 0; i < number_entries; i++) {
3482 temp = ntohs(list[i]);
3483 if (temp >= stcb->asoc.streamoutcnt) {
3484 /* no such stream */
3487 stcb->asoc.strmout[temp].next_sequence_sent = 0;
3490 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3494 struct sctp_stream_reset_out_request *
3495 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3497 struct sctp_association *asoc;
3498 struct sctp_stream_reset_out_req *req;
3499 struct sctp_stream_reset_out_request *r;
3500 struct sctp_tmit_chunk *chk;
3504 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3505 asoc->stream_reset_outstanding = 0;
3508 if (stcb->asoc.str_reset == NULL) {
3509 asoc->stream_reset_outstanding = 0;
3512 chk = stcb->asoc.str_reset;
3513 if (chk->data == NULL) {
3517 /* he wants a copy of the chk pointer */
3520 clen = chk->send_size;
3521 req = mtod(chk->data, struct sctp_stream_reset_out_req *);
3523 if (ntohl(r->request_seq) == seq) {
3527 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3528 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3529 /* move to the next one, there can only be a max of two */
3530 r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len);
3531 if (ntohl(r->request_seq) == seq) {
3535 /* that seq is not here */
3540 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3542 struct sctp_association *asoc;
3543 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3545 if (stcb->asoc.str_reset == NULL) {
3550 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3551 TAILQ_REMOVE(&asoc->control_send_queue,
3555 sctp_m_freem(chk->data);
3558 asoc->ctrl_queue_cnt--;
3559 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3560 /* sa_ignore NO_NULL_CHK */
3561 stcb->asoc.str_reset = NULL;
3566 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3567 uint32_t seq, uint32_t action,
3568 struct sctp_stream_reset_response *respin)
3572 struct sctp_association *asoc = &stcb->asoc;
3573 struct sctp_tmit_chunk *chk;
3574 struct sctp_stream_reset_out_request *srparam;
3577 if (asoc->stream_reset_outstanding == 0) {
3581 if (seq == stcb->asoc.str_reset_seq_out) {
3582 srparam = sctp_find_stream_reset(stcb, seq, &chk);
3584 stcb->asoc.str_reset_seq_out++;
3585 type = ntohs(srparam->ph.param_type);
3586 lparm_len = ntohs(srparam->ph.param_length);
3587 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3588 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3589 asoc->stream_reset_out_is_outstanding = 0;
3590 if (asoc->stream_reset_outstanding)
3591 asoc->stream_reset_outstanding--;
3592 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3594 sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams);
3595 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3596 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3598 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3600 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3601 /* Answered my request */
3602 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3603 if (asoc->stream_reset_outstanding)
3604 asoc->stream_reset_outstanding--;
3605 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3606 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3607 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3608 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3609 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3610 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3612 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3613 /* Ok we now may have more streams */
3616 num_stream = stcb->asoc.strm_pending_add_size;
3617 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3619 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3621 stcb->asoc.strm_pending_add_size = 0;
3622 if (asoc->stream_reset_outstanding)
3623 asoc->stream_reset_outstanding--;
3624 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3625 /* Put the new streams into effect */
3626 stcb->asoc.streamoutcnt += num_stream;
3627 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3628 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3629 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3630 SCTP_STREAM_CHANGE_DENIED);
3632 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3633 SCTP_STREAM_CHANGE_FAILED);
3635 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3636 if (asoc->stream_reset_outstanding)
3637 asoc->stream_reset_outstanding--;
3638 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3639 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3640 SCTP_STREAM_CHANGE_DENIED);
3641 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3642 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3643 SCTP_STREAM_CHANGE_FAILED);
3645 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3647 * a) Adopt the new in tsn.
3649 * c) Adopt the new out-tsn
3651 struct sctp_stream_reset_response_tsn *resp;
3652 struct sctp_forward_tsn_chunk fwdtsn;
3655 if (respin == NULL) {
3659 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3660 resp = (struct sctp_stream_reset_response_tsn *)respin;
3661 asoc->stream_reset_outstanding--;
3662 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3663 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3664 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3665 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3669 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3670 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3671 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3673 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3674 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3675 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3677 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3678 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3680 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3681 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3683 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3684 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3685 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3686 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3687 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3688 SCTP_ASSOC_RESET_DENIED);
3690 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3691 SCTP_ASSOC_RESET_FAILED);
3694 /* get rid of the request and get the request flags */
3695 if (asoc->stream_reset_outstanding == 0) {
3696 sctp_clean_up_stream_reset(stcb);
3704 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3705 struct sctp_tmit_chunk *chk,
3706 struct sctp_stream_reset_in_request *req, int trunc)
3714 * peer wants me to send a str-reset to him for my outgoing seq's if
3717 struct sctp_association *asoc = &stcb->asoc;
3719 seq = ntohl(req->request_seq);
3720 if (asoc->str_reset_seq_in == seq) {
3721 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3722 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3723 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3725 /* Can't do it, since they exceeded our buffer size */
3726 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3727 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3728 len = ntohs(req->ph.param_length);
3729 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3730 for (i = 0; i < number_entries; i++) {
3731 temp = ntohs(req->list_of_streams[i]);
3732 req->list_of_streams[i] = temp;
3734 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3735 sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams,
3736 asoc->str_reset_seq_out,
3737 seq, (asoc->sending_seq - 1));
3738 asoc->stream_reset_out_is_outstanding = 1;
3739 asoc->str_reset = chk;
3740 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
3741 stcb->asoc.stream_reset_outstanding++;
3743 /* Can't do it, since we have sent one out */
3744 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3746 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3747 asoc->str_reset_seq_in++;
3748 } else if (asoc->str_reset_seq_in - 1 == seq) {
3749 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3750 } else if (asoc->str_reset_seq_in - 2 == seq) {
3751 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3753 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3758 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3759 struct sctp_tmit_chunk *chk,
3760 struct sctp_stream_reset_tsn_request *req)
3762 /* reset all in and out and update the tsn */
3764 * A) reset my str-seq's on in and out. B) Select a receive next,
3765 * and set cum-ack to it. Also process this selected number as a
3766 * fwd-tsn as well. C) set in the response my next sending seq.
3768 struct sctp_forward_tsn_chunk fwdtsn;
3769 struct sctp_association *asoc = &stcb->asoc;
3773 seq = ntohl(req->request_seq);
3774 if (asoc->str_reset_seq_in == seq) {
3775 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3776 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3777 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3779 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3780 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3781 fwdtsn.ch.chunk_flags = 0;
3782 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3783 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3787 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3788 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3789 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3791 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3792 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3793 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3794 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3795 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3796 atomic_add_int(&asoc->sending_seq, 1);
3797 /* save off historical data for retrans */
3798 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3799 asoc->last_sending_seq[0] = asoc->sending_seq;
3800 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3801 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3802 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3803 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3804 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3805 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3807 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3808 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3809 asoc->str_reset_seq_in++;
3810 } else if (asoc->str_reset_seq_in - 1 == seq) {
3811 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3812 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3813 } else if (asoc->str_reset_seq_in - 2 == seq) {
3814 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3815 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3817 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3823 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3824 struct sctp_tmit_chunk *chk,
3825 struct sctp_stream_reset_out_request *req, int trunc)
3828 int number_entries, len;
3829 struct sctp_association *asoc = &stcb->asoc;
3831 seq = ntohl(req->request_seq);
3833 /* now if its not a duplicate we process it */
3834 if (asoc->str_reset_seq_in == seq) {
3835 len = ntohs(req->ph.param_length);
3836 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3838 * the sender is resetting, handle the list issue.. we must
3839 * a) verify if we can do the reset, if so no problem b) If
3840 * we can't do the reset we must copy the request. c) queue
3841 * it, and setup the data in processor to trigger it off
3842 * when needed and dequeue all the queued data.
3844 tsn = ntohl(req->send_reset_at_tsn);
3846 /* move the reset action back one */
3847 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3848 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3849 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3851 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3852 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3853 /* we can do it now */
3854 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3855 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3858 * we must queue it up and thus wait for the TSN's
3859 * to arrive that are at or before tsn
3861 struct sctp_stream_reset_list *liste;
3864 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3865 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3866 siz, SCTP_M_STRESET);
3867 if (liste == NULL) {
3868 /* gak out of memory */
3869 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3870 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3874 liste->number_entries = number_entries;
3875 memcpy(&liste->req, req,
3876 (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t))));
3877 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3878 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3880 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3881 asoc->str_reset_seq_in++;
3882 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3884 * one seq back, just echo back last action since my
3885 * response was lost.
3887 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3888 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3890 * two seq back, just echo back last action since my
3891 * response was lost.
3893 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3895 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3900 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3901 struct sctp_stream_reset_add_strm *str_add)
3904 * Peer is requesting to add more streams. If its within our
3905 * max-streams we will allow it.
3907 uint32_t num_stream, i;
3909 struct sctp_association *asoc = &stcb->asoc;
3910 struct sctp_queued_to_read *ctl, *nctl;
3912 /* Get the number. */
3913 seq = ntohl(str_add->request_seq);
3914 num_stream = ntohs(str_add->number_of_streams);
3915 /* Now what would be the new total? */
3916 if (asoc->str_reset_seq_in == seq) {
3917 num_stream += stcb->asoc.streamincnt;
3918 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3919 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3920 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3921 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
3922 (num_stream > 0xffff)) {
3923 /* We must reject it they ask for to many */
3925 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3927 /* Ok, we can do that :-) */
3928 struct sctp_stream_in *oldstrm;
3930 /* save off the old */
3931 oldstrm = stcb->asoc.strmin;
3932 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
3933 (num_stream * sizeof(struct sctp_stream_in)),
3935 if (stcb->asoc.strmin == NULL) {
3936 stcb->asoc.strmin = oldstrm;
3939 /* copy off the old data */
3940 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3941 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3942 stcb->asoc.strmin[i].stream_no = i;
3943 stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered;
3944 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
3945 /* now anything on those queues? */
3946 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) {
3947 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next);
3948 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next);
3951 /* Init the new streams */
3952 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
3953 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3954 stcb->asoc.strmin[i].stream_no = i;
3955 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3956 stcb->asoc.strmin[i].delivery_started = 0;
3958 SCTP_FREE(oldstrm, SCTP_M_STRMI);
3959 /* update the size */
3960 stcb->asoc.streamincnt = num_stream;
3961 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3962 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3964 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3965 asoc->str_reset_seq_in++;
3966 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3968 * one seq back, just echo back last action since my
3969 * response was lost.
3971 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3972 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3974 * two seq back, just echo back last action since my
3975 * response was lost.
3977 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3979 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3985 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3986 struct sctp_stream_reset_add_strm *str_add)
3989 * Peer is requesting to add more streams. If its within our
3990 * max-streams we will allow it.
3992 uint16_t num_stream;
3994 struct sctp_association *asoc = &stcb->asoc;
3996 /* Get the number. */
3997 seq = ntohl(str_add->request_seq);
3998 num_stream = ntohs(str_add->number_of_streams);
3999 /* Now what would be the new total? */
4000 if (asoc->str_reset_seq_in == seq) {
4001 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4002 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4003 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4004 } else if (stcb->asoc.stream_reset_outstanding) {
4005 /* We must reject it we have something pending */
4006 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4008 /* Ok, we can do that :-) */
4011 mychk = stcb->asoc.streamoutcnt;
4012 mychk += num_stream;
4013 if (mychk < 0x10000) {
4014 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4015 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 0, 1, num_stream, 0, 1)) {
4016 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4019 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4022 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4023 asoc->str_reset_seq_in++;
4024 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4026 * one seq back, just echo back last action since my
4027 * response was lost.
4029 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4030 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4032 * two seq back, just echo back last action since my
4033 * response was lost.
4035 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4037 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4042 __attribute__((noinline))
4045 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4046 struct sctp_stream_reset_out_req *sr_req)
4048 int chk_length, param_len, ptype;
4049 struct sctp_paramhdr pstore;
4050 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4054 struct sctp_tmit_chunk *chk;
4055 struct sctp_chunkhdr *ch;
4056 struct sctp_paramhdr *ph;
4060 /* now it may be a reset or a reset-response */
4061 chk_length = ntohs(sr_req->ch.chunk_length);
4063 /* setup for adding the response */
4064 sctp_alloc_a_chunk(stcb, chk);
4068 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4069 chk->rec.chunk_id.can_take_data = 0;
4070 chk->asoc = &stcb->asoc;
4071 chk->no_fr_allowed = 0;
4072 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4073 chk->book_size_scale = 0;
4074 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4075 if (chk->data == NULL) {
4078 sctp_m_freem(chk->data);
4081 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4084 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4086 /* setup chunk parameters */
4087 chk->sent = SCTP_DATAGRAM_UNSENT;
4091 ch = mtod(chk->data, struct sctp_chunkhdr *);
4092 ch->chunk_type = SCTP_STREAM_RESET;
4093 ch->chunk_flags = 0;
4094 ch->chunk_length = htons(chk->send_size);
4095 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4096 offset += sizeof(struct sctp_chunkhdr);
4097 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) {
4098 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore);
4101 param_len = ntohs(ph->param_length);
4102 if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) {
4106 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)),
4107 (uint8_t *) & cstore);
4108 ptype = ntohs(ph->param_type);
4110 if (param_len > (int)sizeof(cstore)) {
4115 if (num_param > SCTP_MAX_RESET_PARAMS) {
4116 /* hit the max of parameters already sorry.. */
4119 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4120 struct sctp_stream_reset_out_request *req_out;
4122 req_out = (struct sctp_stream_reset_out_request *)ph;
4124 if (stcb->asoc.stream_reset_outstanding) {
4125 seq = ntohl(req_out->response_seq);
4126 if (seq == stcb->asoc.str_reset_seq_out) {
4128 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4131 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4132 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4133 struct sctp_stream_reset_add_strm *str_add;
4135 str_add = (struct sctp_stream_reset_add_strm *)ph;
4137 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4138 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4139 struct sctp_stream_reset_add_strm *str_add;
4141 str_add = (struct sctp_stream_reset_add_strm *)ph;
4143 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4144 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4145 struct sctp_stream_reset_in_request *req_in;
4148 req_in = (struct sctp_stream_reset_in_request *)ph;
4149 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4150 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4151 struct sctp_stream_reset_tsn_request *req_tsn;
4154 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4155 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4157 goto strres_nochunk;
4161 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4162 struct sctp_stream_reset_response *resp;
4165 resp = (struct sctp_stream_reset_response *)ph;
4166 seq = ntohl(resp->response_seq);
4167 result = ntohl(resp->result);
4168 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4170 goto strres_nochunk;
4175 offset += SCTP_SIZE32(param_len);
4176 chk_length -= SCTP_SIZE32(param_len);
4179 /* we have no response free the stuff */
4180 goto strres_nochunk;
4182 /* ok we have a chunk to link in */
4183 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4186 stcb->asoc.ctrl_queue_cnt++;
4191 * Handle a router or endpoints report of a packet loss, there are two ways
4192 * to handle this, either we get the whole packet and must disect it
4193 * ourselves (possibly with truncation and or corruption) or it is a summary
4194 * from a middle box that did the disectting for us.
4197 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4198 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4200 uint32_t bottle_bw, on_queue;
4204 struct sctp_chunk_desc desc;
4205 struct sctp_chunkhdr *ch;
4207 chlen = ntohs(cp->ch.chunk_length);
4208 chlen -= sizeof(struct sctp_pktdrop_chunk);
4209 /* XXX possible chlen underflow */
4212 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4213 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4215 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4216 chlen -= sizeof(struct sctphdr);
4217 /* XXX possible chlen underflow */
4218 memset(&desc, 0, sizeof(desc));
4220 trunc_len = (uint16_t) ntohs(cp->trunc_len);
4221 if (trunc_len > limit) {
4224 /* now the chunks themselves */
4225 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4226 desc.chunk_type = ch->chunk_type;
4227 /* get amount we need to move */
4228 at = ntohs(ch->chunk_length);
4229 if (at < sizeof(struct sctp_chunkhdr)) {
4230 /* corrupt chunk, maybe at the end? */
4231 SCTP_STAT_INCR(sctps_pdrpcrupt);
4234 if (trunc_len == 0) {
4235 /* we are supposed to have all of it */
4237 /* corrupt skip it */
4238 SCTP_STAT_INCR(sctps_pdrpcrupt);
4242 /* is there enough of it left ? */
4243 if (desc.chunk_type == SCTP_DATA) {
4244 if (chlen < (sizeof(struct sctp_data_chunk) +
4245 sizeof(desc.data_bytes))) {
4249 if (chlen < sizeof(struct sctp_chunkhdr)) {
4254 if (desc.chunk_type == SCTP_DATA) {
4255 /* can we get out the tsn? */
4256 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4257 SCTP_STAT_INCR(sctps_pdrpmbda);
4259 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4261 struct sctp_data_chunk *dcp;
4265 dcp = (struct sctp_data_chunk *)ch;
4266 ddp = (uint8_t *) (dcp + 1);
4267 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4268 desc.data_bytes[iii] = ddp[iii];
4270 desc.tsn_ifany = dcp->dp.tsn;
4272 /* nope we are done. */
4273 SCTP_STAT_INCR(sctps_pdrpnedat);
4277 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4278 SCTP_STAT_INCR(sctps_pdrpmbct);
4281 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4282 SCTP_STAT_INCR(sctps_pdrppdbrk);
4285 if (SCTP_SIZE32(at) > chlen) {
4288 chlen -= SCTP_SIZE32(at);
4289 if (chlen < sizeof(struct sctp_chunkhdr)) {
4290 /* done, none left */
4293 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4295 /* Now update any rwnd --- possibly */
4296 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4297 /* From a peer, we get a rwnd report */
4300 SCTP_STAT_INCR(sctps_pdrpfehos);
4302 bottle_bw = ntohl(cp->bottle_bw);
4303 on_queue = ntohl(cp->current_onq);
4304 if (bottle_bw && on_queue) {
4305 /* a rwnd report is in here */
4306 if (bottle_bw > on_queue)
4307 a_rwnd = bottle_bw - on_queue;
4312 stcb->asoc.peers_rwnd = 0;
4314 if (a_rwnd > stcb->asoc.total_flight) {
4315 stcb->asoc.peers_rwnd =
4316 a_rwnd - stcb->asoc.total_flight;
4318 stcb->asoc.peers_rwnd = 0;
4320 if (stcb->asoc.peers_rwnd <
4321 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4322 /* SWS sender side engages */
4323 stcb->asoc.peers_rwnd = 0;
4328 SCTP_STAT_INCR(sctps_pdrpfmbox);
4331 /* now middle boxes in sat networks get a cwnd bump */
4332 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4333 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4334 (stcb->asoc.sat_network)) {
4336 * This is debateable but for sat networks it makes sense
4337 * Note if a T3 timer has went off, we will prohibit any
4338 * changes to cwnd until we exit the t3 loss recovery.
4340 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4341 net, cp, &bottle_bw, &on_queue);
4346 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4347 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4348 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4349 * length of the complete packet outputs: - length: modified to remaining
4350 * length after control processing - netp: modified to new sctp_nets after
4351 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4352 * bad packet,...) otherwise return the tcb for this packet
4355 __attribute__((noinline))
4357 static struct sctp_tcb *
4358 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4359 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4360 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4361 uint32_t vrf_id, uint16_t port)
4363 struct sctp_association *asoc;
4365 int num_chunks = 0; /* number of control chunks processed */
4366 uint32_t chk_length;
4368 int abort_no_unlock = 0;
4372 * How big should this be, and should it be alloc'd? Lets try the
4373 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4374 * until we get into jumbo grams and such..
4376 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4377 struct sctp_tcb *locked_tcb = stcb;
4379 uint32_t auth_offset = 0, auth_len = 0;
4380 int auth_skipped = 0;
4383 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4388 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4389 iphlen, *offset, length, stcb);
4391 /* validate chunk header length... */
4392 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4393 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4394 ntohs(ch->chunk_length));
4396 SCTP_TCB_UNLOCK(locked_tcb);
4401 * validate the verification tag
4403 vtag_in = ntohl(sh->v_tag);
4406 SCTP_TCB_LOCK_ASSERT(locked_tcb);
4408 if (ch->chunk_type == SCTP_INITIATION) {
4409 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4410 ntohs(ch->chunk_length), vtag_in);
4412 /* protocol error- silently discard... */
4413 SCTP_STAT_INCR(sctps_badvtag);
4415 SCTP_TCB_UNLOCK(locked_tcb);
4419 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4421 * If there is no stcb, skip the AUTH chunk and process
4422 * later after a stcb is found (to validate the lookup was
4425 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4427 !SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4428 /* save this chunk for later processing */
4430 auth_offset = *offset;
4431 auth_len = ntohs(ch->chunk_length);
4433 /* (temporarily) move past this chunk */
4434 *offset += SCTP_SIZE32(auth_len);
4435 if (*offset >= length) {
4436 /* no more data left in the mbuf chain */
4439 SCTP_TCB_UNLOCK(locked_tcb);
4443 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4444 sizeof(struct sctp_chunkhdr), chunk_buf);
4450 SCTP_TCB_UNLOCK(locked_tcb);
4454 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4455 goto process_control_chunks;
4458 * first check if it's an ASCONF with an unknown src addr we
4459 * need to look inside to find the association
4461 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4462 struct sctp_chunkhdr *asconf_ch = ch;
4463 uint32_t asconf_offset = 0, asconf_len = 0;
4465 /* inp's refcount may be reduced */
4466 SCTP_INP_INCR_REF(inp);
4468 asconf_offset = *offset;
4470 asconf_len = ntohs(asconf_ch->chunk_length);
4471 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4473 stcb = sctp_findassociation_ep_asconf(m,
4474 *offset, sh, &inp, netp, vrf_id);
4477 asconf_offset += SCTP_SIZE32(asconf_len);
4478 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4479 sizeof(struct sctp_chunkhdr), chunk_buf);
4480 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4483 * reduce inp's refcount if not reduced in
4484 * sctp_findassociation_ep_asconf().
4486 SCTP_INP_DECR_REF(inp);
4491 /* now go back and verify any auth chunk to be sure */
4492 if (auth_skipped && (stcb != NULL)) {
4493 struct sctp_auth_chunk *auth;
4495 auth = (struct sctp_auth_chunk *)
4496 sctp_m_getptr(m, auth_offset,
4497 auth_len, chunk_buf);
4500 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4502 /* auth HMAC failed so dump it */
4505 SCTP_TCB_UNLOCK(locked_tcb);
4509 /* remaining chunks are HMAC checked */
4510 stcb->asoc.authenticated = 1;
4515 /* no association, so it's out of the blue... */
4516 sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL,
4520 SCTP_TCB_UNLOCK(locked_tcb);
4525 /* ABORT and SHUTDOWN can use either v_tag... */
4526 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4527 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4528 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4529 if ((vtag_in == asoc->my_vtag) ||
4530 ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
4531 (vtag_in == asoc->peer_vtag))) {
4534 /* drop this packet... */
4535 SCTP_STAT_INCR(sctps_badvtag);
4537 SCTP_TCB_UNLOCK(locked_tcb);
4541 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4542 if (vtag_in != asoc->my_vtag) {
4544 * this could be a stale SHUTDOWN-ACK or the
4545 * peer never got the SHUTDOWN-COMPLETE and
4546 * is still hung; we have started a new asoc
4547 * but it won't complete until the shutdown
4551 SCTP_TCB_UNLOCK(locked_tcb);
4553 sctp_handle_ootb(m, iphlen, *offset, sh, inp,
4554 NULL, vrf_id, port);
4558 /* for all other chunks, vtag must match */
4559 if (vtag_in != asoc->my_vtag) {
4560 /* invalid vtag... */
4561 SCTPDBG(SCTP_DEBUG_INPUT3,
4562 "invalid vtag: %xh, expect %xh\n",
4563 vtag_in, asoc->my_vtag);
4564 SCTP_STAT_INCR(sctps_badvtag);
4566 SCTP_TCB_UNLOCK(locked_tcb);
4572 } /* end if !SCTP_COOKIE_ECHO */
4574 * process all control chunks...
4576 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4577 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4578 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4579 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4580 /* implied cookie-ack.. we must have lost the ack */
4581 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4582 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4583 stcb->asoc.overall_error_count,
4585 SCTP_FROM_SCTP_INPUT,
4588 stcb->asoc.overall_error_count = 0;
4589 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4592 process_control_chunks:
4593 while (IS_SCTP_CONTROL(ch)) {
4594 /* validate chunk length */
4595 chk_length = ntohs(ch->chunk_length);
4596 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4597 ch->chunk_type, chk_length);
4598 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4599 if (chk_length < sizeof(*ch) ||
4600 (*offset + (int)chk_length) > length) {
4603 SCTP_TCB_UNLOCK(locked_tcb);
4607 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4609 * INIT-ACK only gets the init ack "header" portion only
4610 * because we don't have to process the peer's COOKIE. All
4611 * others get a complete chunk.
4613 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4614 (ch->chunk_type == SCTP_INITIATION)) {
4615 /* get an init-ack chunk */
4616 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4617 sizeof(struct sctp_init_ack_chunk), chunk_buf);
4621 SCTP_TCB_UNLOCK(locked_tcb);
4626 /* For cookies and all other chunks. */
4627 if (chk_length > sizeof(chunk_buf)) {
4629 * use just the size of the chunk buffer so
4630 * the front part of our chunks fit in
4631 * contiguous space up to the chunk buffer
4632 * size (508 bytes). For chunks that need to
4633 * get more than that they must use the
4634 * sctp_m_getptr() function or other means
4635 * (e.g. know how to parse mbuf chains).
4636 * Cookies do this already.
4638 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4639 (sizeof(chunk_buf) - 4),
4644 SCTP_TCB_UNLOCK(locked_tcb);
4649 /* We can fit it all */
4650 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4651 chk_length, chunk_buf);
4653 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4656 SCTP_TCB_UNLOCK(locked_tcb);
4663 /* Save off the last place we got a control from */
4665 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4667 * allow last_control to be NULL if
4668 * ASCONF... ASCONF processing will find the
4671 if ((netp != NULL) && (*netp != NULL))
4672 stcb->asoc.last_control_chunk_from = *netp;
4675 #ifdef SCTP_AUDITING_ENABLED
4676 sctp_audit_log(0xB0, ch->chunk_type);
4679 /* check to see if this chunk required auth, but isn't */
4680 if ((stcb != NULL) &&
4681 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
4682 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4683 !stcb->asoc.authenticated) {
4684 /* "silently" ignore */
4685 SCTP_STAT_INCR(sctps_recvauthmissing);
4688 switch (ch->chunk_type) {
4689 case SCTP_INITIATION:
4690 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4691 /* The INIT chunk must be the only chunk. */
4692 if ((num_chunks > 1) ||
4693 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4694 sctp_abort_association(inp, stcb, m,
4695 iphlen, sh, NULL, vrf_id, port);
4699 /* Honor our resource limit. */
4700 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4701 struct mbuf *op_err;
4703 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
4704 sctp_abort_association(inp, stcb, m,
4705 iphlen, sh, op_err, vrf_id, port);
4709 sctp_handle_init(m, iphlen, *offset, sh,
4710 (struct sctp_init_chunk *)ch, inp,
4711 stcb, &abort_no_unlock, vrf_id, port);
4713 if ((!abort_no_unlock) && (locked_tcb)) {
4714 SCTP_TCB_UNLOCK(locked_tcb);
4718 case SCTP_PAD_CHUNK:
4720 case SCTP_INITIATION_ACK:
4721 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4722 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4723 /* We are not interested anymore */
4724 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4727 if (locked_tcb != stcb) {
4729 SCTP_TCB_UNLOCK(locked_tcb);
4733 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4734 so = SCTP_INP_SO(inp);
4735 atomic_add_int(&stcb->asoc.refcnt, 1);
4736 SCTP_TCB_UNLOCK(stcb);
4737 SCTP_SOCKET_LOCK(so, 1);
4738 SCTP_TCB_LOCK(stcb);
4739 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4741 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4742 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4743 SCTP_SOCKET_UNLOCK(so, 1);
4749 /* The INIT-ACK chunk must be the only chunk. */
4750 if ((num_chunks > 1) ||
4751 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4754 SCTP_TCB_UNLOCK(locked_tcb);
4758 if ((netp) && (*netp)) {
4759 ret = sctp_handle_init_ack(m, iphlen, *offset, sh,
4760 (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id);
4765 if (abort_no_unlock) {
4769 * Special case, I must call the output routine to
4770 * get the cookie echoed
4772 if ((stcb != NULL) && (ret == 0)) {
4773 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4776 SCTP_TCB_UNLOCK(locked_tcb);
4780 case SCTP_SELECTIVE_ACK:
4782 struct sctp_sack_chunk *sack;
4784 uint32_t a_rwnd, cum_ack;
4785 uint16_t num_seg, num_dup;
4787 int offset_seg, offset_dup;
4789 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4790 SCTP_STAT_INCR(sctps_recvsacks);
4792 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4795 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4796 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4799 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4801 * If we have sent a shutdown-ack, we will pay no
4802 * attention to a sack sent in to us since
4803 * we don't care anymore.
4807 sack = (struct sctp_sack_chunk *)ch;
4808 flags = ch->chunk_flags;
4809 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4810 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4811 num_dup = ntohs(sack->sack.num_dup_tsns);
4812 a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
4813 if (sizeof(struct sctp_sack_chunk) +
4814 num_seg * sizeof(struct sctp_gap_ack_block) +
4815 num_dup * sizeof(uint32_t) != chk_length) {
4816 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4819 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4820 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4821 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4822 cum_ack, num_seg, a_rwnd);
4823 stcb->asoc.seen_a_sack_this_pkt = 1;
4824 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4826 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4827 (stcb->asoc.saw_sack_with_frags == 0) &&
4828 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4829 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4832 * We have a SIMPLE sack having no
4833 * prior segments and data on sent
4834 * queue to be acked.. Use the
4835 * faster path sack processing. We
4836 * also allow window update sacks
4837 * with no missing segments to go
4840 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
4843 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4844 num_seg, 0, num_dup, &abort_now, flags,
4845 cum_ack, a_rwnd, ecne_seen);
4848 /* ABORT signal from sack processing */
4852 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4853 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4854 (stcb->asoc.stream_queue_cnt == 0)) {
4855 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4860 * EY - nr_sack: If the received chunk is an
4863 case SCTP_NR_SELECTIVE_ACK:
4865 struct sctp_nr_sack_chunk *nr_sack;
4867 uint32_t a_rwnd, cum_ack;
4868 uint16_t num_seg, num_nr_seg, num_dup;
4870 int offset_seg, offset_dup;
4872 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
4873 SCTP_STAT_INCR(sctps_recvsacks);
4875 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
4878 if ((stcb->asoc.sctp_nr_sack_on_off == 0) ||
4879 (stcb->asoc.peer_supports_nr_sack == 0)) {
4882 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4883 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
4886 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4888 * If we have sent a shutdown-ack, we will pay no
4889 * attention to a sack sent in to us since
4890 * we don't care anymore.
4894 nr_sack = (struct sctp_nr_sack_chunk *)ch;
4895 flags = ch->chunk_flags;
4896 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4897 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4898 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4899 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4900 a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd);
4901 if (sizeof(struct sctp_nr_sack_chunk) +
4902 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4903 num_dup * sizeof(uint32_t) != chk_length) {
4904 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4907 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4908 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4909 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4910 cum_ack, num_seg, a_rwnd);
4911 stcb->asoc.seen_a_sack_this_pkt = 1;
4912 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4913 (num_seg == 0) && (num_nr_seg == 0) &&
4914 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4915 (stcb->asoc.saw_sack_with_frags == 0) &&
4916 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4917 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
4919 * We have a SIMPLE sack having no
4920 * prior segments and data on sent
4921 * queue to be acked. Use the faster
4922 * path sack processing. We also
4923 * allow window update sacks with no
4924 * missing segments to go this way
4927 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
4928 &abort_now, ecne_seen);
4931 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4932 num_seg, num_nr_seg, num_dup, &abort_now, flags,
4933 cum_ack, a_rwnd, ecne_seen);
4936 /* ABORT signal from sack processing */
4940 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4941 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4942 (stcb->asoc.stream_queue_cnt == 0)) {
4943 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4948 case SCTP_HEARTBEAT_REQUEST:
4949 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
4950 if ((stcb) && netp && *netp) {
4951 SCTP_STAT_INCR(sctps_recvheartbeat);
4952 sctp_send_heartbeat_ack(stcb, m, *offset,
4955 /* He's alive so give him credit */
4956 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4957 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4958 stcb->asoc.overall_error_count,
4960 SCTP_FROM_SCTP_INPUT,
4963 stcb->asoc.overall_error_count = 0;
4966 case SCTP_HEARTBEAT_ACK:
4967 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
4968 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
4972 SCTP_TCB_UNLOCK(locked_tcb);
4976 /* He's alive so give him credit */
4977 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4978 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4979 stcb->asoc.overall_error_count,
4981 SCTP_FROM_SCTP_INPUT,
4984 stcb->asoc.overall_error_count = 0;
4985 SCTP_STAT_INCR(sctps_recvheartbeatack);
4987 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
4990 case SCTP_ABORT_ASSOCIATION:
4991 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
4993 if ((stcb) && netp && *netp)
4994 sctp_handle_abort((struct sctp_abort_chunk *)ch,
5000 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5002 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5005 SCTP_TCB_UNLOCK(locked_tcb);
5009 if (netp && *netp) {
5012 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5013 stcb, *netp, &abort_flag);
5020 case SCTP_SHUTDOWN_ACK:
5021 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb);
5022 if ((stcb) && (netp) && (*netp))
5023 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5028 case SCTP_OPERATION_ERROR:
5029 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
5030 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
5035 case SCTP_COOKIE_ECHO:
5036 SCTPDBG(SCTP_DEBUG_INPUT3,
5037 "SCTP_COOKIE-ECHO, stcb %p\n", stcb);
5038 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5041 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5042 /* We are not interested anymore */
5045 SCTP_TCB_UNLOCK(stcb);
5052 * First are we accepting? We do this again here
5053 * since it is possible that a previous endpoint WAS
5054 * listening responded to a INIT-ACK and then
5055 * closed. We opened and bound.. and are now no
5059 if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
5060 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5061 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5062 struct mbuf *op_err;
5064 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
5065 sctp_abort_association(inp, stcb, m,
5066 iphlen, sh, op_err, vrf_id, port);
5071 struct mbuf *ret_buf;
5072 struct sctp_inpcb *linp;
5081 SCTP_ASOC_CREATE_LOCK(linp);
5082 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5083 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5084 SCTP_ASOC_CREATE_UNLOCK(linp);
5090 sctp_handle_cookie_echo(m, iphlen,
5092 (struct sctp_cookie_echo_chunk *)ch,
5104 SCTP_ASOC_CREATE_UNLOCK(linp);
5106 if (ret_buf == NULL) {
5108 SCTP_TCB_UNLOCK(locked_tcb);
5110 SCTPDBG(SCTP_DEBUG_INPUT3,
5111 "GAK, null buffer\n");
5115 /* if AUTH skipped, see if it verified... */
5120 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5122 * Restart the timer if we have
5125 struct sctp_tmit_chunk *chk;
5127 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5128 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5132 case SCTP_COOKIE_ACK:
5133 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb);
5134 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5136 SCTP_TCB_UNLOCK(locked_tcb);
5140 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5141 /* We are not interested anymore */
5142 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5145 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5146 so = SCTP_INP_SO(inp);
5147 atomic_add_int(&stcb->asoc.refcnt, 1);
5148 SCTP_TCB_UNLOCK(stcb);
5149 SCTP_SOCKET_LOCK(so, 1);
5150 SCTP_TCB_LOCK(stcb);
5151 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5153 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
5154 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5155 SCTP_SOCKET_UNLOCK(so, 1);
5161 /* He's alive so give him credit */
5162 if ((stcb) && netp && *netp) {
5163 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5164 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5165 stcb->asoc.overall_error_count,
5167 SCTP_FROM_SCTP_INPUT,
5170 stcb->asoc.overall_error_count = 0;
5171 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5175 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5176 /* He's alive so give him credit */
5177 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5180 SCTP_TCB_UNLOCK(locked_tcb);
5186 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5187 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5188 stcb->asoc.overall_error_count,
5190 SCTP_FROM_SCTP_INPUT,
5193 stcb->asoc.overall_error_count = 0;
5194 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5200 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5201 /* He's alive so give him credit */
5202 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5205 SCTP_TCB_UNLOCK(locked_tcb);
5211 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5212 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5213 stcb->asoc.overall_error_count,
5215 SCTP_FROM_SCTP_INPUT,
5218 stcb->asoc.overall_error_count = 0;
5219 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5222 case SCTP_SHUTDOWN_COMPLETE:
5223 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb);
5224 /* must be first and only chunk */
5225 if ((num_chunks > 1) ||
5226 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5229 SCTP_TCB_UNLOCK(locked_tcb);
5233 if ((stcb) && netp && *netp) {
5234 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5241 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5242 /* He's alive so give him credit */
5244 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5245 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5246 stcb->asoc.overall_error_count,
5248 SCTP_FROM_SCTP_INPUT,
5251 stcb->asoc.overall_error_count = 0;
5252 sctp_handle_asconf(m, *offset,
5253 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5257 case SCTP_ASCONF_ACK:
5258 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5259 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5262 SCTP_TCB_UNLOCK(locked_tcb);
5267 if ((stcb) && netp && *netp) {
5268 /* He's alive so give him credit */
5269 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5270 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5271 stcb->asoc.overall_error_count,
5273 SCTP_FROM_SCTP_INPUT,
5276 stcb->asoc.overall_error_count = 0;
5277 sctp_handle_asconf_ack(m, *offset,
5278 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5279 if (abort_no_unlock)
5283 case SCTP_FORWARD_CUM_TSN:
5284 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5285 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5288 SCTP_TCB_UNLOCK(locked_tcb);
5293 /* He's alive so give him credit */
5297 stcb->asoc.overall_error_count = 0;
5298 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5299 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5300 stcb->asoc.overall_error_count,
5302 SCTP_FROM_SCTP_INPUT,
5306 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5307 /* We are not interested anymore */
5308 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5309 so = SCTP_INP_SO(inp);
5310 atomic_add_int(&stcb->asoc.refcnt, 1);
5311 SCTP_TCB_UNLOCK(stcb);
5312 SCTP_SOCKET_LOCK(so, 1);
5313 SCTP_TCB_LOCK(stcb);
5314 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5316 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
5317 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5318 SCTP_SOCKET_UNLOCK(so, 1);
5323 sctp_handle_forward_tsn(stcb,
5324 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5329 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5330 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5331 stcb->asoc.overall_error_count,
5333 SCTP_FROM_SCTP_INPUT,
5336 stcb->asoc.overall_error_count = 0;
5341 case SCTP_STREAM_RESET:
5342 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5343 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5346 SCTP_TCB_UNLOCK(locked_tcb);
5351 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5352 /* We are not interested anymore */
5353 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5354 so = SCTP_INP_SO(inp);
5355 atomic_add_int(&stcb->asoc.refcnt, 1);
5356 SCTP_TCB_UNLOCK(stcb);
5357 SCTP_SOCKET_LOCK(so, 1);
5358 SCTP_TCB_LOCK(stcb);
5359 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5361 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5362 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5363 SCTP_SOCKET_UNLOCK(so, 1);
5368 if (stcb->asoc.peer_supports_strreset == 0) {
5370 * hmm, peer should have announced this, but
5371 * we will turn it on since he is sending us
5374 stcb->asoc.peer_supports_strreset = 1;
5376 if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) {
5377 /* stop processing */
5382 case SCTP_PACKET_DROPPED:
5383 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5384 /* re-get it all please */
5385 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5388 SCTP_TCB_UNLOCK(locked_tcb);
5393 if (ch && (stcb) && netp && (*netp)) {
5394 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5396 min(chk_length, (sizeof(chunk_buf) - 4)));
5401 case SCTP_AUTHENTICATION:
5402 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5403 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
5407 /* save the first AUTH for later processing */
5408 if (auth_skipped == 0) {
5409 auth_offset = *offset;
5410 auth_len = chk_length;
5413 /* skip this chunk (temporarily) */
5416 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5417 (chk_length > (sizeof(struct sctp_auth_chunk) +
5418 SCTP_AUTH_DIGEST_LEN_MAX))) {
5421 SCTP_TCB_UNLOCK(locked_tcb);
5426 if (got_auth == 1) {
5427 /* skip this chunk... it's already auth'd */
5431 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5433 /* auth HMAC failed so dump the packet */
5437 /* remaining chunks are HMAC checked */
5438 stcb->asoc.authenticated = 1;
5444 /* it's an unknown chunk! */
5445 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5447 struct sctp_paramhdr *phd;
5449 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
5450 0, M_DONTWAIT, 1, MT_DATA);
5452 phd = mtod(mm, struct sctp_paramhdr *);
5454 * We cheat and use param type since
5455 * we did not bother to define a
5456 * error cause struct. They are the
5457 * same basic format with different
5460 phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5461 phd->param_length = htons(chk_length + sizeof(*phd));
5462 SCTP_BUF_LEN(mm) = sizeof(*phd);
5463 SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, chk_length, M_DONTWAIT);
5464 if (SCTP_BUF_NEXT(mm)) {
5465 if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(mm), SCTP_SIZE32(chk_length) - chk_length, NULL)) {
5468 #ifdef SCTP_MBUF_LOGGING
5469 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5472 for (mat = SCTP_BUF_NEXT(mm); mat; mat = SCTP_BUF_NEXT(mat)) {
5473 if (SCTP_BUF_IS_EXTENDED(mat)) {
5474 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5479 sctp_queue_op_err(stcb, mm);
5486 if ((ch->chunk_type & 0x80) == 0) {
5487 /* discard this packet */
5490 } /* else skip this bad chunk and continue... */
5492 } /* switch (ch->chunk_type) */
5496 /* get the next chunk */
5497 *offset += SCTP_SIZE32(chk_length);
5498 if (*offset >= length) {
5499 /* no more data left in the mbuf chain */
5502 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5503 sizeof(struct sctp_chunkhdr), chunk_buf);
5506 SCTP_TCB_UNLOCK(locked_tcb);
5513 if (asconf_cnt > 0 && stcb != NULL) {
5514 sctp_send_asconf_ack(stcb);
5522 __attribute__((noinline))
5525 sctp_validate_no_locks(struct sctp_inpcb *inp)
5527 struct sctp_tcb *lstcb;
5529 LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) {
5530 if (mtx_owned(&lstcb->tcb_mtx)) {
5531 panic("Own lock on stcb at return from input");
5534 if (mtx_owned(&inp->inp_create_mtx)) {
5535 panic("Own create lock on inp");
5537 if (mtx_owned(&inp->inp_mtx)) {
5538 panic("Own inp lock on inp");
5545 * common input chunk processing (v4 and v6)
5548 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset,
5549 int length, struct sctphdr *sh, struct sctp_chunkhdr *ch,
5550 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
5551 uint8_t ecn_bits, uint32_t vrf_id, uint16_t port)
5554 * Control chunk processing
5557 int fwd_tsn_seen = 0, data_processed = 0;
5558 struct mbuf *m = *mm;
5560 int cnt_ctrl_ready = 0;
5562 SCTP_STAT_INCR(sctps_recvdatagrams);
5563 #ifdef SCTP_AUDITING_ENABLED
5564 sctp_audit_log(0xE0, 1);
5565 sctp_auditing(0, inp, stcb, net);
5568 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5569 m, iphlen, offset, length, stcb);
5571 /* always clear this before beginning a packet */
5572 stcb->asoc.authenticated = 0;
5573 stcb->asoc.seen_a_sack_this_pkt = 0;
5574 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5575 stcb, stcb->asoc.state);
5577 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5578 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5580 * If we hit here, we had a ref count
5581 * up when the assoc was aborted and the
5582 * timer is clearing out the assoc, we should
5583 * NOT respond to any packet.. its OOTB.
5585 SCTP_TCB_UNLOCK(stcb);
5586 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5591 if (IS_SCTP_CONTROL(ch)) {
5592 /* process the control portion of the SCTP packet */
5593 /* sa_ignore NO_NULL_CHK */
5594 stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch,
5595 inp, stcb, &net, &fwd_tsn_seen, vrf_id, port);
5598 * This covers us if the cookie-echo was there and
5599 * it changes our INP.
5601 inp = stcb->sctp_ep;
5602 if ((net) && (port)) {
5603 if (net->port == 0) {
5604 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5611 * no control chunks, so pre-process DATA chunks (these
5612 * checks are taken care of by control processing)
5616 * if DATA only packet, and auth is required, then punt...
5617 * can't have authenticated without any AUTH (control)
5620 if ((stcb != NULL) &&
5621 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5622 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5623 /* "silently" ignore */
5624 SCTP_STAT_INCR(sctps_recvauthmissing);
5625 SCTP_TCB_UNLOCK(stcb);
5629 /* out of the blue DATA chunk */
5630 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5634 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5635 /* v_tag mismatch! */
5636 SCTP_STAT_INCR(sctps_badvtag);
5637 SCTP_TCB_UNLOCK(stcb);
5644 * no valid TCB for this packet, or we found it's a bad
5645 * packet while processing control, or we're done with this
5646 * packet (done or skip rest of data), so we drop it...
5651 * DATA chunk processing
5653 /* plow through the data chunks while length > offset */
5656 * Rest should be DATA only. Check authentication state if AUTH for
5659 if ((length > offset) &&
5661 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5662 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5663 !stcb->asoc.authenticated) {
5664 /* "silently" ignore */
5665 SCTP_STAT_INCR(sctps_recvauthmissing);
5666 SCTPDBG(SCTP_DEBUG_AUTH1,
5667 "Data chunk requires AUTH, skipped\n");
5670 if (length > offset) {
5674 * First check to make sure our state is correct. We would
5675 * not get here unless we really did have a tag, so we don't
5676 * abort if this happens, just dump the chunk silently.
5678 switch (SCTP_GET_STATE(&stcb->asoc)) {
5679 case SCTP_STATE_COOKIE_ECHOED:
5681 * we consider data with valid tags in this state
5682 * shows us the cookie-ack was lost. Imply it was
5685 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5686 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5687 stcb->asoc.overall_error_count,
5689 SCTP_FROM_SCTP_INPUT,
5692 stcb->asoc.overall_error_count = 0;
5693 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5695 case SCTP_STATE_COOKIE_WAIT:
5697 * We consider OOTB any data sent during asoc setup.
5699 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5701 SCTP_TCB_UNLOCK(stcb);
5703 /* sa_ignore NOTREACHED */
5705 case SCTP_STATE_EMPTY: /* should not happen */
5706 case SCTP_STATE_INUSE: /* should not happen */
5707 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5708 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5710 SCTP_TCB_UNLOCK(stcb);
5712 /* sa_ignore NOTREACHED */
5714 case SCTP_STATE_OPEN:
5715 case SCTP_STATE_SHUTDOWN_SENT:
5718 /* plow through the data chunks while length > offset */
5719 retval = sctp_process_data(mm, iphlen, &offset, length, sh,
5720 inp, stcb, net, &high_tsn);
5723 * The association aborted, NO UNLOCK needed since
5724 * the association is destroyed.
5730 * Anything important needs to have been m_copy'ed in
5734 /* take care of ecn */
5735 if ((data_processed == 1) &&
5736 (stcb->asoc.ecn_allowed == 1) &&
5737 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5738 /* Yep, we need to add a ECNE */
5739 sctp_send_ecn_echo(stcb, net, high_tsn);
5741 if ((data_processed == 0) && (fwd_tsn_seen)) {
5743 uint32_t highest_tsn;
5745 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5746 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5748 highest_tsn = stcb->asoc.highest_tsn_inside_map;
5750 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
5751 stcb->asoc.send_sack = 1;
5752 sctp_sack_check(stcb, was_a_gap);
5753 } else if (fwd_tsn_seen) {
5754 stcb->asoc.send_sack = 1;
5756 /* trigger send of any chunks in queue... */
5758 #ifdef SCTP_AUDITING_ENABLED
5759 sctp_audit_log(0xE0, 2);
5760 sctp_auditing(1, inp, stcb, net);
5762 SCTPDBG(SCTP_DEBUG_INPUT1,
5763 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5764 stcb->asoc.peers_rwnd,
5765 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5766 stcb->asoc.total_flight);
5767 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5768 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5769 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5771 if (cnt_ctrl_ready ||
5773 (stcb->asoc.peers_rwnd > 0 ||
5774 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5775 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5776 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5777 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5779 #ifdef SCTP_AUDITING_ENABLED
5780 sctp_audit_log(0xE0, 3);
5781 sctp_auditing(2, inp, stcb, net);
5783 SCTP_TCB_UNLOCK(stcb);
5786 sctp_validate_no_locks(inp);
5793 sctp_print_mbuf_chain(struct mbuf *m)
5795 for (; m; m = SCTP_BUF_NEXT(m)) {
5796 SCTP_PRINTF("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m));
5797 if (SCTP_BUF_IS_EXTENDED(m))
5798 SCTP_PRINTF("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m));
5806 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5810 uint32_t vrf_id = 0;
5814 struct sctp_inpcb *inp = NULL;
5815 struct sctp_nets *net;
5816 struct sctp_tcb *stcb = NULL;
5817 struct sctp_chunkhdr *ch;
5818 int refcount_up = 0;
5819 int length, mlen, offset;
5821 #if !defined(SCTP_WITH_NO_CSUM)
5822 uint32_t check, calc_check;
5826 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
5827 SCTP_RELEASE_PKT(i_pak);
5830 mlen = SCTP_HEADER_LEN(i_pak);
5832 m = SCTP_HEADER_TO_CHAIN(i_pak);
5835 SCTP_STAT_INCR(sctps_recvpackets);
5836 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
5839 #ifdef SCTP_MBUF_LOGGING
5840 /* Log in any input mbufs */
5841 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5844 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
5845 if (SCTP_BUF_IS_EXTENDED(mat)) {
5846 sctp_log_mb(mat, SCTP_MBUF_INPUT);
5851 #ifdef SCTP_PACKET_LOGGING
5852 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
5853 sctp_packet_log(m, mlen);
5856 * Must take out the iphlen, since mlen expects this (only effect lb
5862 * Get IP, SCTP, and first chunk header together in first mbuf.
5864 ip = mtod(m, struct ip *);
5865 offset = iphlen + sizeof(*sh) + sizeof(*ch);
5866 if (SCTP_BUF_LEN(m) < offset) {
5867 if ((m = m_pullup(m, offset)) == 0) {
5868 SCTP_STAT_INCR(sctps_hdrops);
5871 ip = mtod(m, struct ip *);
5873 /* validate mbuf chain length with IP payload length */
5874 if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) {
5875 SCTP_STAT_INCR(sctps_hdrops);
5878 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
5879 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh));
5880 SCTPDBG(SCTP_DEBUG_INPUT1,
5881 "sctp_input() length:%d iphlen:%d\n", mlen, iphlen);
5883 /* SCTP does not allow broadcasts or multicasts */
5884 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
5887 if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) {
5889 * We only look at broadcast if its a front state, All
5890 * others we will not have a tcb for anyway.
5894 /* validate SCTP checksum */
5895 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
5896 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
5898 if_name(m->m_pkthdr.rcvif),
5899 m->m_pkthdr.csum_flags);
5900 #if defined(SCTP_WITH_NO_CSUM)
5901 SCTP_STAT_INCR(sctps_recvnocrc);
5903 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5904 SCTP_STAT_INCR(sctps_recvhwcrc);
5905 goto sctp_skip_csum_4;
5907 check = sh->checksum; /* save incoming checksum */
5908 sh->checksum = 0; /* prepare for calc */
5909 calc_check = sctp_calculate_cksum(m, iphlen);
5910 sh->checksum = check;
5911 SCTP_STAT_INCR(sctps_recvswcrc);
5912 if (calc_check != check) {
5913 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5914 calc_check, check, m, mlen, iphlen);
5916 stcb = sctp_findassociation_addr(m,
5917 offset - sizeof(*ch),
5920 if ((net) && (port)) {
5921 if (net->port == 0) {
5922 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5926 if ((net != NULL) && (m->m_flags & M_FLOWID)) {
5927 net->flowid = m->m_pkthdr.flowid;
5932 if ((inp) && (stcb)) {
5933 sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
5934 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5935 } else if ((inp != NULL) && (stcb == NULL)) {
5938 SCTP_STAT_INCR(sctps_badsum);
5939 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5944 /* destination port of 0 is illegal, based on RFC2960. */
5945 if (sh->dest_port == 0) {
5946 SCTP_STAT_INCR(sctps_hdrops);
5950 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
5951 * IP/SCTP/first chunk header...
5953 stcb = sctp_findassociation_addr(m, offset - sizeof(*ch),
5954 sh, ch, &inp, &net, vrf_id);
5955 if ((net) && (port)) {
5956 if (net->port == 0) {
5957 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5961 if ((net != NULL) && (m->m_flags & M_FLOWID)) {
5962 net->flowid = m->m_pkthdr.flowid;
5967 /* inp's ref-count increased && stcb locked */
5969 struct sctp_init_chunk *init_chk, chunk_buf;
5971 SCTP_STAT_INCR(sctps_noport);
5974 * we use the bandwidth limiting to protect against sending
5975 * too many ABORTS all at once. In this case these count the
5976 * same as an ICMP message.
5978 if (badport_bandlim(0) < 0)
5980 #endif /* ICMP_BANDLIM */
5981 SCTPDBG(SCTP_DEBUG_INPUT1,
5982 "Sending a ABORT from packet entry!\n");
5983 if (ch->chunk_type == SCTP_INITIATION) {
5985 * we do a trick here to get the INIT tag, dig in
5986 * and get the tag from the INIT and put it in the
5989 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
5990 iphlen + sizeof(*sh), sizeof(*init_chk),
5991 (uint8_t *) & chunk_buf);
5992 if (init_chk != NULL)
5993 sh->v_tag = init_chk->init.initiate_tag;
5995 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5996 sctp_send_shutdown_complete2(m, sh, vrf_id, port);
5999 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
6002 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
6003 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
6004 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
6005 (ch->chunk_type != SCTP_INIT))) {
6006 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
6010 } else if (stcb == NULL) {
6015 * I very much doubt any of the IPSEC stuff will work but I have no
6016 * idea, so I will leave it in place.
6018 if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) {
6019 MODULE_GLOBAL(ipsec4stat).in_polvio++;
6020 SCTP_STAT_INCR(sctps_hdrops);
6026 * common chunk processing
6028 length = ip->ip_len + iphlen;
6029 offset -= sizeof(struct sctp_chunkhdr);
6031 ecn_bits = ip->ip_tos;
6033 /* sa_ignore NO_NULL_CHK */
6034 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
6035 inp, stcb, net, ecn_bits, vrf_id, port);
6036 /* inp's ref-count reduced && stcb unlocked */
6040 if ((inp) && (refcount_up)) {
6041 /* reduce ref-count */
6042 SCTP_INP_DECR_REF(inp);
6047 SCTP_TCB_UNLOCK(stcb);
6049 if ((inp) && (refcount_up)) {
6050 /* reduce ref-count */
6051 SCTP_INP_DECR_REF(inp);
6059 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6060 extern int *sctp_cpuarry;
6065 sctp_input(struct mbuf *m, int off)
6067 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6072 uint32_t flowid, tag;
6075 if (m->m_flags & M_FLOWID) {
6076 flowid = m->m_pkthdr.flowid;
6079 * No flow id built by lower layers fix it so we
6082 ip = mtod(m, struct ip *);
6083 offset = off + sizeof(*sh);
6084 if (SCTP_BUF_LEN(m) < offset) {
6085 if ((m = m_pullup(m, offset)) == 0) {
6086 SCTP_STAT_INCR(sctps_hdrops);
6089 ip = mtod(m, struct ip *);
6091 sh = (struct sctphdr *)((caddr_t)ip + off);
6092 tag = htonl(sh->v_tag);
6093 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6094 m->m_pkthdr.flowid = flowid;
6095 m->m_flags |= M_FLOWID;
6097 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6098 sctp_queue_to_mcore(m, off, cpu_to_use);
6102 sctp_input_with_port(m, off, 0);