2 * Copyright (c) 2001-2007, Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * b) Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution.
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
31 /* $KAME: sctp_input.c,v 1.27 2005/03/06 16:04:17 itojun Exp $ */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.h>
37 #include <netinet/sctp_var.h>
38 #include <netinet/sctp_sysctl.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctputil.h>
42 #include <netinet/sctp_output.h>
43 #include <netinet/sctp_input.h>
44 #include <netinet/sctp_auth.h>
45 #include <netinet/sctp_indata.h>
46 #include <netinet/sctp_asconf.h>
52 sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
54 struct sctp_nets *net;
57 * This now not only stops all cookie timers it also stops any INIT
58 * timers as well. This will make sure that the timers are stopped
59 * in all collision cases.
61 SCTP_TCB_LOCK_ASSERT(stcb);
62 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
63 if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
64 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
67 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
68 } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
69 sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
72 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
79 sctp_handle_init(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
80 struct sctp_init_chunk *cp, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
81 struct sctp_nets *net, int *abort_no_unlock)
83 struct sctp_init *init;
88 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
89 printf("sctp_handle_init: handling INIT tcb:%p\n", stcb);
94 /* First are we accepting? */
95 if ((inp->sctp_socket->so_qlimit == 0) && (stcb == NULL)) {
97 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
98 printf("sctp_handle_init: Abort, so_qlimit:%d\n", inp->sctp_socket->so_qlimit);
102 * FIX ME ?? What about TCP model and we have a
103 * match/restart case?
105 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
107 *abort_no_unlock = 1;
110 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
112 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
113 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
115 *abort_no_unlock = 1;
118 /* validate parameters */
119 if (init->initiate_tag == 0) {
120 /* protocol error... send abort */
121 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
122 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
124 *abort_no_unlock = 1;
127 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
128 /* invalid parameter... send abort */
129 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
130 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
133 if (init->num_inbound_streams == 0) {
134 /* protocol error... send abort */
135 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
136 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
138 *abort_no_unlock = 1;
141 if (init->num_outbound_streams == 0) {
142 /* protocol error... send abort */
143 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
144 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
146 *abort_no_unlock = 1;
149 init_limit = offset + ntohs(cp->ch.chunk_length);
150 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
152 /* auth parameter(s) error... send abort */
153 sctp_abort_association(inp, stcb, m, iphlen, sh, NULL);
155 *abort_no_unlock = 1;
158 /* send an INIT-ACK w/cookie */
160 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
161 printf("sctp_handle_init: sending INIT-ACK\n");
164 sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp);
168 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
171 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb,
172 struct sctp_nets *net)
174 struct sctp_init *init;
175 struct sctp_association *asoc;
176 struct sctp_nets *lnet;
181 /* save off parameters */
182 asoc->peer_vtag = ntohl(init->initiate_tag);
183 asoc->peers_rwnd = ntohl(init->a_rwnd);
184 if (TAILQ_FIRST(&asoc->nets)) {
185 /* update any ssthresh's that may have a default */
186 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
187 lnet->ssthresh = asoc->peers_rwnd;
189 #if defined(SCTP_CWND_MONITOR) || defined(SCTP_CWND_LOGGING)
190 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
195 SCTP_TCB_SEND_LOCK(stcb);
196 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
198 struct sctp_stream_out *outs;
199 struct sctp_stream_queue_pending *sp;
201 /* cut back on number of streams */
202 newcnt = ntohs(init->num_inbound_streams);
203 /* This if is probably not needed but I am cautious */
205 /* First make sure no data chunks are trapped */
206 for (i = newcnt; i < asoc->pre_open_streams; i++) {
207 outs = &asoc->strmout[i];
208 sp = TAILQ_FIRST(&outs->outqueue);
210 TAILQ_REMOVE(&outs->outqueue, sp,
212 asoc->stream_queue_cnt--;
213 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
214 stcb, SCTP_NOTIFY_DATAGRAM_UNSENT,
217 sctp_m_freem(sp->data);
220 sctp_free_remote_addr(sp->net);
223 printf("sp:%p tcb:%p weird free case\n",
226 sctp_free_a_strmoq(stcb, sp);
227 sp = TAILQ_FIRST(&outs->outqueue);
231 /* cut back the count and abandon the upper streams */
232 asoc->pre_open_streams = newcnt;
234 SCTP_TCB_SEND_UNLOCK(stcb);
235 asoc->streamoutcnt = asoc->pre_open_streams;
237 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
238 #ifdef SCTP_MAP_LOGGING
239 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
241 /* This is the next one we expect */
242 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
244 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
245 asoc->cumulative_tsn = asoc->asconf_seq_in;
246 asoc->last_echo_tsn = asoc->asconf_seq_in;
247 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
248 /* open the requested streams */
249 if (asoc->strmin != NULL) {
250 /* Free the old ones */
251 struct sctp_queued_to_read *ctl;
253 for (i = 0; i < asoc->streamincnt; i++) {
254 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
256 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
257 sctp_free_remote_addr(ctl->whoFrom);
258 sctp_m_freem(ctl->data);
260 sctp_free_a_readq(stcb, ctl);
261 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
264 SCTP_FREE(asoc->strmin);
266 asoc->streamincnt = ntohs(init->num_outbound_streams);
267 if (asoc->streamincnt > MAX_SCTP_STREAMS) {
268 asoc->streamincnt = MAX_SCTP_STREAMS;
270 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
271 sizeof(struct sctp_stream_in), "StreamsIn");
272 if (asoc->strmin == NULL) {
273 /* we didn't get memory for the streams! */
275 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
276 printf("process_init: couldn't get memory for the streams!\n");
281 for (i = 0; i < asoc->streamincnt; i++) {
282 asoc->strmin[i].stream_no = i;
283 asoc->strmin[i].last_sequence_delivered = 0xffff;
285 * U-stream ranges will be set when the cookie is unpacked.
286 * Or for the INIT sender they are un set (if pr-sctp not
287 * supported) when the INIT-ACK arrives.
289 TAILQ_INIT(&asoc->strmin[i].inqueue);
290 asoc->strmin[i].delivery_started = 0;
293 * load_address_from_init will put the addresses into the
294 * association when the COOKIE is processed or the INIT-ACK is
295 * processed. Both types of COOKIE's existing and new call this
296 * routine. It will remove addresses that are no longer in the
297 * association (for the restarting case where addresses are
298 * removed). Up front when the INIT arrives we will discard it if it
299 * is a restart and new addresses have been added.
305 * INIT-ACK message processing/consumption returns value < 0 on error
308 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
309 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
310 struct sctp_nets *net, int *abort_no_unlock)
312 struct sctp_association *asoc;
314 int retval, abort_flag;
315 uint32_t initack_limit;
317 /* First verify that we have no illegal param's */
321 op_err = sctp_arethere_unrecognized_parameters(m,
322 (offset + sizeof(struct sctp_init_chunk)),
323 &abort_flag, (struct sctp_chunkhdr *)cp);
325 /* Send an abort and notify peer */
326 if (op_err != NULL) {
327 sctp_send_operr_to(m, iphlen, op_err, cp->init.initiate_tag);
330 * Just notify (abort_assoc does this if we send an
333 sctp_abort_notification(stcb, 0);
335 * No sense in further INIT's since we will get the
338 sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
339 *abort_no_unlock = 1;
344 /* process the peer's parameters in the INIT-ACK */
345 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net);
349 initack_limit = offset + ntohs(cp->ch.chunk_length);
350 /* load all addresses */
351 if ((retval = sctp_load_addresses_from_init(stcb, m, iphlen,
352 (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh,
354 /* Huh, we should abort */
356 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
357 printf("Load addresses from INIT causes an abort %d\n", retval);
360 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
362 *abort_no_unlock = 1;
365 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
366 stcb->asoc.local_hmacs);
368 sctp_queue_op_err(stcb, op_err);
369 /* queuing will steal away the mbuf chain to the out queue */
372 /* extract the cookie and queue it to "echo" it back... */
373 stcb->asoc.overall_error_count = 0;
374 net->error_count = 0;
377 * Cancel the INIT timer, We do this first before queueing the
378 * cookie. We always cancel at the primary to assue that we are
379 * canceling the timer started by the INIT which always goes to the
382 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
383 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
385 /* calculate the RTO */
386 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered);
388 retval = sctp_send_cookie_echo(m, offset, stcb, net);
391 * No cookie, we probably should send a op error. But in any
392 * case if there is no cookie in the INIT-ACK, we can
393 * abandon the peer, its broke.
396 /* We abort with an error of missing mandatory param */
400 sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM);
403 * Expand beyond to include the mandatory
406 struct sctp_inv_mandatory_param *mp;
408 SCTP_BUF_LEN(op_err) =
409 sizeof(struct sctp_inv_mandatory_param);
411 struct sctp_inv_mandatory_param *);
412 /* Subtract the reserved param */
414 htons(sizeof(struct sctp_inv_mandatory_param) - 2);
415 mp->num_param = htonl(1);
416 mp->param = htons(SCTP_STATE_COOKIE);
419 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
421 *abort_no_unlock = 1;
429 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
430 struct sctp_tcb *stcb, struct sctp_nets *net)
432 struct sockaddr_storage store;
433 struct sockaddr_in *sin;
434 struct sockaddr_in6 *sin6;
435 struct sctp_nets *r_net;
438 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
442 sin = (struct sockaddr_in *)&store;
443 sin6 = (struct sockaddr_in6 *)&store;
445 memset(&store, 0, sizeof(store));
446 if (cp->heartbeat.hb_info.addr_family == AF_INET &&
447 cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
448 sin->sin_family = cp->heartbeat.hb_info.addr_family;
449 sin->sin_len = cp->heartbeat.hb_info.addr_len;
450 sin->sin_port = stcb->rport;
451 memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address,
452 sizeof(sin->sin_addr));
453 } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 &&
454 cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
455 sin6->sin6_family = cp->heartbeat.hb_info.addr_family;
456 sin6->sin6_len = cp->heartbeat.hb_info.addr_len;
457 sin6->sin6_port = stcb->rport;
458 memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address,
459 sizeof(sin6->sin6_addr));
463 r_net = sctp_findnet(stcb, (struct sockaddr *)sin);
466 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
467 printf("Huh? I can't find the address I sent it to, discard\n");
472 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
473 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
474 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
476 * If the its a HB and it's random value is correct when can
477 * confirm the destination.
479 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
480 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
481 stcb->asoc.primary_destination = r_net;
482 r_net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
483 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
484 r_net = TAILQ_FIRST(&stcb->asoc.nets);
485 if (r_net != stcb->asoc.primary_destination) {
487 * first one on the list is NOT the primary
488 * sctp_cmpaddr() is much more efficent if
489 * the primary is the first on the list,
492 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
493 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
496 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
497 stcb, 0, (void *)r_net);
499 r_net->error_count = 0;
500 r_net->hb_responded = 1;
501 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
502 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
503 if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
504 r_net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE;
505 r_net->dest_state |= SCTP_ADDR_REACHABLE;
506 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
507 SCTP_HEARTBEAT_SUCCESS, (void *)r_net);
508 /* now was it the primary? if so restore */
509 if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) {
510 sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net);
513 /* Now lets do a RTO with this */
514 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv);
518 sctp_handle_abort(struct sctp_abort_chunk *cp,
519 struct sctp_tcb *stcb, struct sctp_nets *net)
523 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
524 printf("sctp_handle_abort: handling ABORT\n");
529 /* verify that the destination addr is in the association */
530 /* ignore abort for addresses being deleted */
532 /* stop any receive timers */
533 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
534 /* notify user of the abort and clean up... */
535 sctp_abort_notification(stcb, 0);
537 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
538 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
539 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
540 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
542 #ifdef SCTP_ASOCLOG_OF_TSNS
543 sctp_print_out_track_log(stcb);
545 sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
547 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
548 printf("sctp_handle_abort: finished\n");
554 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
555 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
557 struct sctp_association *asoc;
558 int some_on_streamwheel;
561 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
562 printf("sctp_handle_shutdown: handling SHUTDOWN\n");
568 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
569 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
572 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
573 /* Shutdown NOT the expected size */
576 sctp_update_acked(stcb, cp, net, abort_flag);
578 if (asoc->control_pdapi) {
580 * With a normal shutdown we assume the end of last record.
582 SCTP_INP_READ_LOCK(stcb->sctp_ep);
583 asoc->control_pdapi->end_added = 1;
584 asoc->control_pdapi->pdapi_aborted = 1;
585 asoc->control_pdapi = NULL;
586 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
587 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
589 /* goto SHUTDOWN_RECEIVED state to block new requests */
590 if (stcb->sctp_socket) {
591 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
592 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
593 asoc->state = SCTP_STATE_SHUTDOWN_RECEIVED;
595 * notify upper layer that peer has initiated a
598 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL);
601 SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
604 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
606 * stop the shutdown timer, since we WILL move to
609 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
611 /* Now are we there yet? */
612 some_on_streamwheel = 0;
613 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
614 /* Check to see if some data queued */
615 struct sctp_stream_out *outs;
617 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
618 if (!TAILQ_EMPTY(&outs->outqueue)) {
619 some_on_streamwheel = 1;
624 if (!TAILQ_EMPTY(&asoc->send_queue) ||
625 !TAILQ_EMPTY(&asoc->sent_queue) ||
626 some_on_streamwheel) {
627 /* By returning we will push more data out */
630 /* no outstanding data to send, so move on... */
631 /* send SHUTDOWN-ACK */
632 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
633 /* move to SHUTDOWN-ACK-SENT state */
634 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
635 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
636 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
638 asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
640 /* start SHUTDOWN timer */
641 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
647 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp,
648 struct sctp_tcb *stcb, struct sctp_nets *net)
650 struct sctp_association *asoc;
653 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
654 printf("sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
661 /* process according to association state */
662 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
663 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
664 /* unexpected SHUTDOWN-ACK... so ignore... */
665 SCTP_TCB_UNLOCK(stcb);
668 if (asoc->control_pdapi) {
670 * With a normal shutdown we assume the end of last record.
672 SCTP_INP_READ_LOCK(stcb->sctp_ep);
673 asoc->control_pdapi->end_added = 1;
674 asoc->control_pdapi->pdapi_aborted = 1;
675 asoc->control_pdapi = NULL;
676 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
677 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
679 /* are the queues empty? */
680 if (!TAILQ_EMPTY(&asoc->send_queue) ||
681 !TAILQ_EMPTY(&asoc->sent_queue) ||
682 !TAILQ_EMPTY(&asoc->out_wheel)) {
683 sctp_report_all_outbound(stcb, 0);
686 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
687 /* send SHUTDOWN-COMPLETE */
688 sctp_send_shutdown_complete(stcb, net);
689 /* notify upper layer protocol */
690 if (stcb->sctp_socket) {
691 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL);
692 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
693 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
694 /* Set the connected flag to disconnected */
695 stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
698 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
699 /* free the TCB but first save off the ep */
700 sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
701 SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
705 * Skip past the param header and then we will find the chunk that caused the
706 * problem. There are two possiblities ASCONF or FWD-TSN other than that and
707 * our peer must be broken.
710 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
711 struct sctp_nets *net)
713 struct sctp_chunkhdr *chk;
715 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
716 switch (chk->chunk_type) {
717 case SCTP_ASCONF_ACK:
719 sctp_asconf_cleanup(stcb, net);
721 case SCTP_FORWARD_CUM_TSN:
722 stcb->asoc.peer_supports_prsctp = 0;
726 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
727 printf("Peer does not support chunk type %d(%x)??\n",
728 chk->chunk_type, (uint32_t) chk->chunk_type);
736 * Skip past the param header and then we will find the param that caused the
737 * problem. There are a number of param's in a ASCONF OR the prsctp param
738 * these will turn of specific features.
741 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
743 struct sctp_paramhdr *pbad;
746 switch (ntohs(pbad->param_type)) {
748 case SCTP_PRSCTP_SUPPORTED:
749 stcb->asoc.peer_supports_prsctp = 0;
751 case SCTP_SUPPORTED_CHUNK_EXT:
753 /* draft-ietf-tsvwg-addip-sctp */
754 case SCTP_ECN_NONCE_SUPPORTED:
755 stcb->asoc.peer_supports_ecn_nonce = 0;
756 stcb->asoc.ecn_nonce_allowed = 0;
757 stcb->asoc.ecn_allowed = 0;
759 case SCTP_ADD_IP_ADDRESS:
760 case SCTP_DEL_IP_ADDRESS:
761 case SCTP_SET_PRIM_ADDR:
762 stcb->asoc.peer_supports_asconf = 0;
764 case SCTP_SUCCESS_REPORT:
765 case SCTP_ERROR_CAUSE_IND:
767 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
768 printf("Huh, the peer does not support success? or error cause?\n");
769 printf("Turning off ASCONF to this strange peer\n");
772 stcb->asoc.peer_supports_asconf = 0;
776 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
777 printf("Peer does not support param type %d(%x)??\n",
778 pbad->param_type, (uint32_t) pbad->param_type);
786 sctp_handle_error(struct sctp_chunkhdr *ch,
787 struct sctp_tcb *stcb, struct sctp_nets *net)
790 struct sctp_paramhdr *phdr;
793 struct sctp_association *asoc;
797 /* parse through all of the errors and process */
799 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
800 sizeof(struct sctp_chunkhdr));
801 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
802 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
803 /* Process an Error Cause */
804 error_type = ntohs(phdr->param_type);
805 error_len = ntohs(phdr->param_length);
806 if ((error_len > chklen) || (error_len == 0)) {
807 /* invalid param length for this param */
809 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
810 printf("Bogus length in error param- chunk left:%d errorlen:%d\n",
813 #endif /* SCTP_DEBUG */
816 switch (error_type) {
817 case SCTP_CAUSE_INVALID_STREAM:
818 case SCTP_CAUSE_MISSING_PARAM:
819 case SCTP_CAUSE_INVALID_PARAM:
820 case SCTP_CAUSE_NO_USER_DATA:
822 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
823 printf("Software error we got a %d back? We have a bug :/ (or do they?)\n",
828 case SCTP_CAUSE_STALE_COOKIE:
830 * We only act if we have echoed a cookie and are
833 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
836 p = (int *)((caddr_t)phdr + sizeof(*phdr));
837 /* Save the time doubled */
838 asoc->cookie_preserve_req = ntohl(*p) << 1;
839 asoc->stale_cookie_count++;
840 if (asoc->stale_cookie_count >
841 asoc->max_init_times) {
842 sctp_abort_notification(stcb, 0);
843 /* now free the asoc */
844 sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
847 /* blast back to INIT state */
848 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
849 asoc->state |= SCTP_STATE_COOKIE_WAIT;
851 sctp_stop_all_cookie_timers(stcb);
852 sctp_send_initiate(stcb->sctp_ep, stcb);
855 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
857 * Nothing we can do here, we don't do hostname
858 * addresses so if the peer does not like my IPv6
859 * (or IPv4 for that matter) it does not matter. If
860 * they don't support that type of address, they can
861 * NOT possibly get that packet type... i.e. with no
862 * IPv6 you can't recieve a IPv6 packet. so we can
863 * safely ignore this one. If we ever added support
864 * for HOSTNAME Addresses, then we would need to do
868 case SCTP_CAUSE_UNRECOG_CHUNK:
869 sctp_process_unrecog_chunk(stcb, phdr, net);
871 case SCTP_CAUSE_UNRECOG_PARAM:
872 sctp_process_unrecog_param(stcb, phdr);
874 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
876 * We ignore this since the timer will drive out a
877 * new cookie anyway and there timer will drive us
878 * to send a SHUTDOWN_COMPLETE. We can't send one
879 * here since we don't have their tag.
882 case SCTP_CAUSE_DELETING_LAST_ADDR:
883 case SCTP_CAUSE_RESOURCE_SHORTAGE:
884 case SCTP_CAUSE_DELETING_SRC_ADDR:
886 * We should NOT get these here, but in a
890 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
891 printf("Peer sends ASCONF errors in a Operational Error?<%d>?\n",
896 case SCTP_CAUSE_OUT_OF_RESC:
898 * And what, pray tell do we do with the fact that
899 * the peer is out of resources? Not really sure we
900 * could do anything but abort. I suspect this
901 * should have came WITH an abort instead of in a
907 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
908 /* don't know what this error cause is... */
909 printf("sctp_handle_error: unknown error type = 0x%xh\n",
912 #endif /* SCTP_DEBUG */
915 adjust = SCTP_SIZE32(error_len);
917 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
923 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
924 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
925 struct sctp_nets *net, int *abort_no_unlock)
927 struct sctp_init_ack *init_ack;
932 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
933 printf("sctp_handle_init_ack: handling INIT-ACK\n");
938 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
939 printf("sctp_handle_init_ack: TCB is null\n");
944 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
946 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
947 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
949 *abort_no_unlock = 1;
952 init_ack = &cp->init;
953 /* validate parameters */
954 if (init_ack->initiate_tag == 0) {
955 /* protocol error... send an abort */
956 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
957 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
959 *abort_no_unlock = 1;
962 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
963 /* protocol error... send an abort */
964 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
965 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
967 *abort_no_unlock = 1;
970 if (init_ack->num_inbound_streams == 0) {
971 /* protocol error... send an abort */
972 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
973 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
975 *abort_no_unlock = 1;
978 if (init_ack->num_outbound_streams == 0) {
979 /* protocol error... send an abort */
980 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
981 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
983 *abort_no_unlock = 1;
986 /* process according to association state... */
987 state = &stcb->asoc.state;
988 switch (*state & SCTP_STATE_MASK) {
989 case SCTP_STATE_COOKIE_WAIT:
990 /* this is the expected state for this chunk */
991 /* process the INIT-ACK parameters */
992 if (stcb->asoc.primary_destination->dest_state &
993 SCTP_ADDR_UNCONFIRMED) {
995 * The primary is where we sent the INIT, we can
996 * always consider it confirmed when the INIT-ACK is
997 * returned. Do this before we load addresses
1000 stcb->asoc.primary_destination->dest_state &=
1001 ~SCTP_ADDR_UNCONFIRMED;
1002 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1003 stcb, 0, (void *)stcb->asoc.primary_destination);
1005 if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, net,
1006 abort_no_unlock) < 0) {
1007 /* error in parsing parameters */
1010 /* update our state */
1012 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1013 printf("moving to COOKIE-ECHOED state\n");
1016 if (*state & SCTP_STATE_SHUTDOWN_PENDING) {
1017 *state = SCTP_STATE_COOKIE_ECHOED |
1018 SCTP_STATE_SHUTDOWN_PENDING;
1020 *state = SCTP_STATE_COOKIE_ECHOED;
1023 /* reset the RTO calc */
1024 stcb->asoc.overall_error_count = 0;
1025 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1027 * collapse the init timer back in case of a exponential
1030 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1033 * the send at the end of the inbound data processing will
1034 * cause the cookie to be sent
1037 case SCTP_STATE_SHUTDOWN_SENT:
1038 /* incorrect state... discard */
1040 case SCTP_STATE_COOKIE_ECHOED:
1041 /* incorrect state... discard */
1043 case SCTP_STATE_OPEN:
1044 /* incorrect state... discard */
1046 case SCTP_STATE_EMPTY:
1047 case SCTP_STATE_INUSE:
1049 /* incorrect state... discard */
1052 } /* end switch asoc state */
1054 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1055 printf("Leaving handle-init-ack end\n");
1063 * handle a state cookie for an existing association m: input packet mbuf
1064 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1065 * "split" mbuf and the cookie signature does not exist offset: offset into
1066 * mbuf to the cookie-echo chunk
1068 static struct sctp_tcb *
1069 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1070 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1071 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
1072 struct sockaddr *init_src, int *notification, sctp_assoc_t * sac_assoc_id)
1074 struct sctp_association *asoc;
1075 struct sctp_init_chunk *init_cp, init_buf;
1076 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1078 int init_offset, initack_offset, i;
1083 /* I know that the TCB is non-NULL from the caller */
1085 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1086 if (asoc->cookie_how[how_indx] == 0)
1089 if (how_indx < sizeof(asoc->cookie_how)) {
1090 asoc->cookie_how[how_indx] = 1;
1092 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1093 /* SHUTDOWN came in after sending INIT-ACK */
1094 struct mbuf *op_err;
1095 struct sctp_paramhdr *ph;
1097 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1098 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1099 0, M_DONTWAIT, 1, MT_DATA);
1100 if (op_err == NULL) {
1104 /* pre-reserve some space */
1105 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
1106 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
1107 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1109 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1110 ph = mtod(op_err, struct sctp_paramhdr *);
1111 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
1112 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1113 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag);
1114 if (how_indx < sizeof(asoc->cookie_how))
1115 asoc->cookie_how[how_indx] = 2;
1119 * find and validate the INIT chunk in the cookie (peer's info) the
1120 * INIT should start after the cookie-echo header struct (chunk
1121 * header, state cookie header struct)
1123 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1125 init_cp = (struct sctp_init_chunk *)
1126 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1127 (uint8_t *) & init_buf);
1128 if (init_cp == NULL) {
1129 /* could not pull a INIT chunk in cookie */
1132 chk_length = ntohs(init_cp->ch.chunk_length);
1133 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1137 * find and validate the INIT-ACK chunk in the cookie (my info) the
1138 * INIT-ACK follows the INIT chunk
1140 initack_offset = init_offset + SCTP_SIZE32(chk_length);
1141 initack_cp = (struct sctp_init_ack_chunk *)
1142 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1143 (uint8_t *) & initack_buf);
1144 if (initack_cp == NULL) {
1145 /* could not pull INIT-ACK chunk in cookie */
1148 chk_length = ntohs(initack_cp->ch.chunk_length);
1149 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1152 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1153 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1155 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1156 * to get into the OPEN state
1158 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1160 panic("Case D and non-match seq?");
1162 printf("Case D, seq non-match %x vs %x?\n",
1163 ntohl(initack_cp->init.initial_tsn), asoc->init_seq_number);
1166 switch SCTP_GET_STATE
1168 case SCTP_STATE_COOKIE_WAIT:
1169 case SCTP_STATE_COOKIE_ECHOED:
1171 * INIT was sent, but got got a COOKIE_ECHO with the
1172 * correct tags... just accept it...but we must
1173 * process the init so that we can make sure we have
1174 * the right seq no's.
1176 /* First we must process the INIT !! */
1177 retval = sctp_process_init(init_cp, stcb, net);
1179 if (how_indx < sizeof(asoc->cookie_how))
1180 asoc->cookie_how[how_indx] = 3;
1183 /* we have already processed the INIT so no problem */
1184 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1185 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1186 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1187 /* update current state */
1188 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1189 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1191 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1192 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1193 asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING;
1194 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1195 stcb->sctp_ep, stcb, asoc->primary_destination);
1198 /* if ok, move to OPEN state */
1199 asoc->state = SCTP_STATE_OPEN;
1201 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1202 sctp_stop_all_cookie_timers(stcb);
1203 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1204 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1205 (inp->sctp_socket->so_qlimit == 0)
1208 * Here is where collision would go if we
1209 * did a connect() and instead got a
1210 * init/init-ack/cookie done before the
1211 * init-ack came back..
1213 stcb->sctp_ep->sctp_flags |=
1214 SCTP_PCB_FLAGS_CONNECTED;
1215 soisconnected(stcb->sctp_ep->sctp_socket);
1217 /* notify upper layer */
1218 *notification = SCTP_NOTIFY_ASSOC_UP;
1220 * since we did not send a HB make sure we don't
1223 net->hb_responded = 1;
1225 if (stcb->asoc.sctp_autoclose_ticks &&
1226 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1227 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1233 * we're in the OPEN state (or beyond), so peer must
1234 * have simply lost the COOKIE-ACK
1239 sctp_stop_all_cookie_timers(stcb);
1241 * We ignore the return code here.. not sure if we should
1242 * somehow abort.. but we do have an existing asoc. This
1243 * really should not fail.
1245 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1246 init_offset + sizeof(struct sctp_init_chunk),
1247 initack_offset, sh, init_src)) {
1248 if (how_indx < sizeof(asoc->cookie_how))
1249 asoc->cookie_how[how_indx] = 4;
1252 /* respond with a COOKIE-ACK */
1253 sctp_toss_old_cookies(stcb, asoc);
1254 sctp_send_cookie_ack(stcb);
1255 if (how_indx < sizeof(asoc->cookie_how))
1256 asoc->cookie_how[how_indx] = 5;
1259 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1260 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1261 cookie->tie_tag_my_vtag == 0 &&
1262 cookie->tie_tag_peer_vtag == 0) {
1264 * case C in Section 5.2.4 Table 2: XMOO silently discard
1266 if (how_indx < sizeof(asoc->cookie_how))
1267 asoc->cookie_how[how_indx] = 6;
1270 if (ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag &&
1271 (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag ||
1272 init_cp->init.initiate_tag == 0)) {
1274 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1275 * should be ok, re-accept peer info
1277 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1279 * Extension of case C. If we hit this, then the
1280 * random number generator returned the same vtag
1281 * when we first sent our INIT-ACK and when we later
1282 * sent our INIT. The side with the seq numbers that
1283 * are different will be the one that normnally
1284 * would have hit case C. This in effect "extends"
1285 * our vtags in this collision case to be 64 bits.
1286 * The same collision could occur aka you get both
1287 * vtag and seq number the same twice in a row.. but
1288 * is much less likely. If it did happen then we
1289 * would proceed through and bring up the assoc.. we
1290 * may end up with the wrong stream setup however..
1291 * which would be bad.. but there is no way to
1292 * tell.. until we send on a stream that does not
1295 if (how_indx < sizeof(asoc->cookie_how))
1296 asoc->cookie_how[how_indx] = 7;
1300 if (how_indx < sizeof(asoc->cookie_how))
1301 asoc->cookie_how[how_indx] = 8;
1302 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1303 sctp_stop_all_cookie_timers(stcb);
1305 * since we did not send a HB make sure we don't double
1308 net->hb_responded = 1;
1309 if (stcb->asoc.sctp_autoclose_ticks &&
1310 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1311 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1314 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1315 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1317 /* Note last_cwr_tsn? where is this used? */
1318 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1319 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1321 * Ok the peer probably discarded our data (if we
1322 * echoed a cookie+data). So anything on the
1323 * sent_queue should be marked for retransmit, we
1324 * may not get something to kick us so it COULD
1325 * still take a timeout to move these.. but it can't
1326 * hurt to mark them.
1328 struct sctp_tmit_chunk *chk;
1330 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1331 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1332 chk->sent = SCTP_DATAGRAM_RESEND;
1333 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1339 /* process the INIT info (peer's info) */
1340 retval = sctp_process_init(init_cp, stcb, net);
1342 if (how_indx < sizeof(asoc->cookie_how))
1343 asoc->cookie_how[how_indx] = 9;
1346 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1347 init_offset + sizeof(struct sctp_init_chunk),
1348 initack_offset, sh, init_src)) {
1349 if (how_indx < sizeof(asoc->cookie_how))
1350 asoc->cookie_how[how_indx] = 10;
1353 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1354 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1355 *notification = SCTP_NOTIFY_ASSOC_UP;
1357 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1358 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1359 (inp->sctp_socket->so_qlimit == 0)) {
1360 stcb->sctp_ep->sctp_flags |=
1361 SCTP_PCB_FLAGS_CONNECTED;
1362 soisconnected(stcb->sctp_ep->sctp_socket);
1364 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1365 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1367 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1368 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1369 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1370 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1371 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1373 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1375 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1376 asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING;
1377 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1378 stcb->sctp_ep, stcb, asoc->primary_destination);
1381 asoc->state = SCTP_STATE_OPEN;
1383 sctp_stop_all_cookie_timers(stcb);
1384 sctp_toss_old_cookies(stcb, asoc);
1385 sctp_send_cookie_ack(stcb);
1388 * only if we have retrans set do we do this. What
1389 * this call does is get only the COOKIE-ACK out and
1390 * then when we return the normal call to
1391 * sctp_chunk_output will get the retrans out behind
1394 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK);
1396 if (how_indx < sizeof(asoc->cookie_how))
1397 asoc->cookie_how[how_indx] = 11;
1401 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1402 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1403 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1404 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1405 cookie->tie_tag_peer_vtag != 0) {
1406 struct sctpasochead *head;
1409 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1412 if (how_indx < sizeof(asoc->cookie_how))
1413 asoc->cookie_how[how_indx] = 12;
1414 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1415 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1417 *sac_assoc_id = sctp_get_associd(stcb);
1418 /* notify upper layer */
1419 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1420 atomic_add_int(&stcb->asoc.refcnt, 1);
1421 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1422 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1423 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1424 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1426 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1427 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1428 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1429 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1431 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1432 asoc->state = SCTP_STATE_OPEN |
1433 SCTP_STATE_SHUTDOWN_PENDING;
1434 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1435 stcb->sctp_ep, stcb, asoc->primary_destination);
1437 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1438 /* move to OPEN state, if not in SHUTDOWN_SENT */
1439 asoc->state = SCTP_STATE_OPEN;
1441 asoc->pre_open_streams =
1442 ntohs(initack_cp->init.num_outbound_streams);
1443 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1444 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1446 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1447 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1449 asoc->str_reset_seq_in = asoc->init_seq_number;
1451 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1452 if (asoc->mapping_array)
1453 memset(asoc->mapping_array, 0,
1454 asoc->mapping_array_size);
1455 SCTP_TCB_UNLOCK(stcb);
1456 SCTP_INP_INFO_WLOCK();
1457 SCTP_INP_WLOCK(stcb->sctp_ep);
1458 SCTP_TCB_LOCK(stcb);
1459 atomic_add_int(&stcb->asoc.refcnt, -1);
1460 /* send up all the data */
1461 SCTP_TCB_SEND_LOCK(stcb);
1463 sctp_report_all_outbound(stcb, 1);
1464 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1465 stcb->asoc.strmout[i].stream_no = i;
1466 stcb->asoc.strmout[i].next_sequence_sent = 0;
1467 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1469 /* process the INIT-ACK info (my info) */
1470 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1471 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1473 /* pull from vtag hash */
1474 LIST_REMOVE(stcb, sctp_asocs);
1475 /* re-insert to new vtag position */
1476 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1477 sctppcbinfo.hashasocmark)];
1479 * put it in the bucket in the vtag hash of assoc's for the
1482 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1484 /* Is this the first restart? */
1485 if (stcb->asoc.in_restart_hash == 0) {
1486 /* Ok add it to assoc_id vtag hash */
1487 head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id,
1488 sctppcbinfo.hashrestartmark)];
1489 LIST_INSERT_HEAD(head, stcb, sctp_tcbrestarhash);
1490 stcb->asoc.in_restart_hash = 1;
1492 /* process the INIT info (peer's info) */
1493 SCTP_TCB_SEND_UNLOCK(stcb);
1494 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1495 SCTP_INP_INFO_WUNLOCK();
1497 retval = sctp_process_init(init_cp, stcb, net);
1499 if (how_indx < sizeof(asoc->cookie_how))
1500 asoc->cookie_how[how_indx] = 13;
1505 * since we did not send a HB make sure we don't double
1508 net->hb_responded = 1;
1510 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1511 init_offset + sizeof(struct sctp_init_chunk),
1512 initack_offset, sh, init_src)) {
1513 if (how_indx < sizeof(asoc->cookie_how))
1514 asoc->cookie_how[how_indx] = 14;
1518 /* respond with a COOKIE-ACK */
1519 sctp_stop_all_cookie_timers(stcb);
1520 sctp_toss_old_cookies(stcb, asoc);
1521 sctp_send_cookie_ack(stcb);
1522 if (how_indx < sizeof(asoc->cookie_how))
1523 asoc->cookie_how[how_indx] = 15;
1527 if (how_indx < sizeof(asoc->cookie_how))
1528 asoc->cookie_how[how_indx] = 16;
1529 /* all other cases... */
1535 * handle a state cookie for a new association m: input packet mbuf chain--
1536 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
1537 * and the cookie signature does not exist offset: offset into mbuf to the
1538 * cookie-echo chunk length: length of the cookie chunk to: where the init
1539 * was from returns a new TCB
1541 static struct sctp_tcb *
1542 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1543 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1544 struct sctp_inpcb *inp, struct sctp_nets **netp,
1545 struct sockaddr *init_src, int *notification,
1546 int auth_skipped, uint32_t auth_offset, uint32_t auth_len)
1548 struct sctp_tcb *stcb;
1549 struct sctp_init_chunk *init_cp, init_buf;
1550 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1551 struct sockaddr_storage sa_store;
1552 struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
1553 struct sockaddr_in *sin;
1554 struct sockaddr_in6 *sin6;
1555 struct sctp_association *asoc;
1558 int init_offset, initack_offset, initack_limit;
1562 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
1564 vrf_id = inp->def_vrf_id;
1567 * find and validate the INIT chunk in the cookie (peer's info) the
1568 * INIT should start after the cookie-echo header struct (chunk
1569 * header, state cookie header struct)
1571 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
1572 init_cp = (struct sctp_init_chunk *)
1573 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1574 (uint8_t *) & init_buf);
1575 if (init_cp == NULL) {
1576 /* could not pull a INIT chunk in cookie */
1578 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1579 printf("process_cookie_new: could not pull INIT chunk hdr\n");
1581 #endif /* SCTP_DEBUG */
1584 chk_length = ntohs(init_cp->ch.chunk_length);
1585 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1587 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1588 printf("HUH? process_cookie_new: could not find INIT chunk!\n");
1590 #endif /* SCTP_DEBUG */
1593 initack_offset = init_offset + SCTP_SIZE32(chk_length);
1595 * find and validate the INIT-ACK chunk in the cookie (my info) the
1596 * INIT-ACK follows the INIT chunk
1598 initack_cp = (struct sctp_init_ack_chunk *)
1599 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1600 (uint8_t *) & initack_buf);
1601 if (initack_cp == NULL) {
1602 /* could not pull INIT-ACK chunk in cookie */
1604 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1605 printf("process_cookie_new: could not pull INIT-ACK chunk hdr\n");
1607 #endif /* SCTP_DEBUG */
1610 chk_length = ntohs(initack_cp->ch.chunk_length);
1611 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1615 * NOTE: We can't use the INIT_ACK's chk_length to determine the
1616 * "initack_limit" value. This is because the chk_length field
1617 * includes the length of the cookie, but the cookie is omitted when
1618 * the INIT and INIT_ACK are tacked onto the cookie...
1620 initack_limit = offset + cookie_len;
1623 * now that we know the INIT/INIT-ACK are in place, create a new TCB
1626 stcb = sctp_aloc_assoc(inp, init_src, 0, &error,
1627 ntohl(initack_cp->init.initiate_tag), vrf_id);
1629 struct mbuf *op_err;
1631 /* memory problem? */
1633 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1634 printf("process_cookie_new: no room for another TCB!\n");
1636 #endif /* SCTP_DEBUG */
1637 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1638 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
1642 /* get the correct sctp_nets */
1643 *netp = sctp_findnet(stcb, init_src);
1645 /* get scope variables out of cookie */
1646 asoc->ipv4_local_scope = cookie->ipv4_scope;
1647 asoc->site_scope = cookie->site_scope;
1648 asoc->local_scope = cookie->local_scope;
1649 asoc->loopback_scope = cookie->loopback_scope;
1651 if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) ||
1652 (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) {
1653 struct mbuf *op_err;
1656 * Houston we have a problem. The EP changed while the
1657 * cookie was in flight. Only recourse is to abort the
1660 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1661 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
1665 /* process the INIT-ACK info (my info) */
1666 old_tag = asoc->my_vtag;
1667 asoc->assoc_id = asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1668 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1669 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1670 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1671 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1672 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1673 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1674 asoc->str_reset_seq_in = asoc->init_seq_number;
1676 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1678 /* process the INIT info (peer's info) */
1679 retval = sctp_process_init(init_cp, stcb, *netp);
1681 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1684 /* load all addresses */
1685 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1686 init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh,
1688 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
1692 * verify any preceding AUTH chunk that was skipped
1694 /* pull the local authentication parameters from the cookie/init-ack */
1695 sctp_auth_get_cookie_params(stcb, m,
1696 initack_offset + sizeof(struct sctp_init_ack_chunk),
1697 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
1699 struct sctp_auth_chunk *auth;
1701 auth = (struct sctp_auth_chunk *)
1702 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
1703 if (sctp_handle_auth(stcb, auth, m, auth_offset)) {
1704 /* auth HMAC failed, dump the assoc and packet */
1706 if (sctp_debug_on & SCTP_DEBUG_AUTH1)
1707 printf("COOKIE-ECHO: AUTH failed\n");
1708 #endif /* SCTP_DEBUG */
1709 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
1712 /* remaining chunks checked... good to go */
1713 stcb->asoc.authenticated = 1;
1716 /* update current state */
1718 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1719 printf("moving to OPEN state\n");
1722 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1723 asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING;
1724 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1725 stcb->sctp_ep, stcb, asoc->primary_destination);
1727 asoc->state = SCTP_STATE_OPEN;
1729 sctp_stop_all_cookie_timers(stcb);
1730 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
1731 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1734 * if we're doing ASCONFs, check to see if we have any new local
1735 * addresses that need to get added to the peer (eg. addresses
1736 * changed while cookie echo in flight). This needs to be done
1737 * after we go to the OPEN state to do the correct asconf
1738 * processing. else, make sure we have the correct addresses in our
1742 /* warning, we re-use sin, sin6, sa_store here! */
1743 /* pull in local_address (our "from" address) */
1744 if (cookie->laddr_type == SCTP_IPV4_ADDRESS) {
1745 /* source addr is IPv4 */
1746 sin = (struct sockaddr_in *)initack_src;
1747 memset(sin, 0, sizeof(*sin));
1748 sin->sin_family = AF_INET;
1749 sin->sin_len = sizeof(struct sockaddr_in);
1750 sin->sin_addr.s_addr = cookie->laddress[0];
1751 } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) {
1752 /* source addr is IPv6 */
1753 sin6 = (struct sockaddr_in6 *)initack_src;
1754 memset(sin6, 0, sizeof(*sin6));
1755 sin6->sin6_family = AF_INET6;
1756 sin6->sin6_len = sizeof(struct sockaddr_in6);
1757 sin6->sin6_scope_id = cookie->scope_id;
1758 memcpy(&sin6->sin6_addr, cookie->laddress,
1759 sizeof(sin6->sin6_addr));
1761 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
1765 sctp_check_address_list(stcb, m,
1766 initack_offset + sizeof(struct sctp_init_ack_chunk),
1767 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
1768 initack_src, cookie->local_scope, cookie->site_scope,
1769 cookie->ipv4_scope, cookie->loopback_scope);
1772 /* set up to notify upper layer */
1773 *notification = SCTP_NOTIFY_ASSOC_UP;
1774 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1775 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1776 (inp->sctp_socket->so_qlimit == 0)) {
1778 * This is an endpoint that called connect() how it got a
1779 * cookie that is NEW is a bit of a mystery. It must be that
1780 * the INIT was sent, but before it got there.. a complete
1781 * INIT/INIT-ACK/COOKIE arrived. But of course then it
1782 * should have went to the other code.. not here.. oh well..
1783 * a bit of protection is worth having..
1785 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1786 soisconnected(stcb->sctp_ep->sctp_socket);
1787 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1788 (inp->sctp_socket->so_qlimit)) {
1790 * We don't want to do anything with this one. Since it is
1791 * the listening guy. The timer will get started for
1792 * accepted connections in the caller.
1796 /* since we did not send a HB make sure we don't double things */
1797 (*netp)->hb_responded = 1;
1799 if (stcb->asoc.sctp_autoclose_ticks &&
1800 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1801 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
1803 /* respond with a COOKIE-ACK */
1804 /* calculate the RTT */
1805 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
1806 &cookie->time_entered);
1807 sctp_send_cookie_ack(stcb);
1813 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
1814 * existing (non-NULL) TCB
1816 static struct mbuf *
1817 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
1818 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
1819 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
1820 int auth_skipped, uint32_t auth_offset, uint32_t auth_len, struct sctp_tcb **locked_tcb)
1822 struct sctp_state_cookie *cookie;
1823 struct sockaddr_in6 sin6;
1824 struct sockaddr_in sin;
1825 struct sctp_tcb *l_stcb = *stcb;
1826 struct sctp_inpcb *l_inp;
1827 struct sockaddr *to;
1828 sctp_assoc_t sac_restart_id;
1829 struct sctp_pcb *ep;
1831 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
1833 uint8_t cookie_ok = 0;
1834 unsigned int size_of_pkt, sig_offset, cookie_offset;
1835 unsigned int cookie_len;
1837 struct timeval time_expires;
1838 struct sockaddr_storage dest_store;
1839 struct sockaddr *localep_sa = (struct sockaddr *)&dest_store;
1841 int notification = 0;
1842 struct sctp_nets *netl;
1843 int had_a_existing_tcb = 0;
1846 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1847 printf("sctp_handle_cookie: handling COOKIE-ECHO\n");
1851 if (inp_p == NULL) {
1854 /* First get the destination address setup too. */
1855 iph = mtod(m, struct ip *);
1856 if (iph->ip_v == IPVERSION) {
1858 struct sockaddr_in *sin;
1860 sin = (struct sockaddr_in *)(localep_sa);
1861 memset(sin, 0, sizeof(*sin));
1862 sin->sin_family = AF_INET;
1863 sin->sin_len = sizeof(*sin);
1864 sin->sin_port = sh->dest_port;
1865 sin->sin_addr.s_addr = iph->ip_dst.s_addr;
1866 size_of_pkt = SCTP_GET_IPV4_LENGTH(iph);
1867 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1869 struct ip6_hdr *ip6;
1870 struct sockaddr_in6 *sin6;
1872 sin6 = (struct sockaddr_in6 *)(localep_sa);
1873 memset(sin6, 0, sizeof(*sin6));
1874 sin6->sin6_family = AF_INET6;
1875 sin6->sin6_len = sizeof(struct sockaddr_in6);
1876 ip6 = mtod(m, struct ip6_hdr *);
1877 sin6->sin6_port = sh->dest_port;
1878 sin6->sin6_addr = ip6->ip6_dst;
1879 size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen;
1884 cookie = &cp->cookie;
1885 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
1886 cookie_len = ntohs(cp->ch.chunk_length);
1888 if ((cookie->peerport != sh->src_port) &&
1889 (cookie->myport != sh->dest_port) &&
1890 (cookie->my_vtag != sh->v_tag)) {
1892 * invalid ports or bad tag. Note that we always leave the
1893 * v_tag in the header in network order and when we stored
1894 * it in the my_vtag slot we also left it in network order.
1895 * This maintians the match even though it may be in the
1896 * opposite byte order of the machine :->
1900 if (cookie_len > size_of_pkt ||
1901 cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
1902 sizeof(struct sctp_init_chunk) +
1903 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
1904 /* cookie too long! or too small */
1908 * split off the signature into its own mbuf (since it should not be
1909 * calculated in the sctp_hmac_m() call).
1911 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
1912 if (sig_offset > size_of_pkt) {
1913 /* packet not correct size! */
1914 /* XXX this may already be accounted for earlier... */
1917 m_sig = m_split(m, sig_offset, M_DONTWAIT);
1918 if (m_sig == NULL) {
1919 /* out of memory or ?? */
1923 * compute the signature/digest for the cookie
1925 ep = &(*inp_p)->sctp_ep;
1928 SCTP_TCB_UNLOCK(l_stcb);
1930 SCTP_INP_RLOCK(l_inp);
1932 SCTP_TCB_LOCK(l_stcb);
1934 /* which cookie is it? */
1935 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
1936 (ep->current_secret_number != ep->last_secret_number)) {
1937 /* it's the old cookie */
1938 sctp_hmac_m(SCTP_HMAC,
1939 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
1940 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
1942 /* it's the current cookie */
1943 sctp_hmac_m(SCTP_HMAC,
1944 (uint8_t *) ep->secret_key[(int)ep->current_secret_number],
1945 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
1947 /* get the signature */
1948 SCTP_INP_RUNLOCK(l_inp);
1949 sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig);
1951 /* couldn't find signature */
1952 sctp_m_freem(m_sig);
1955 /* compare the received digest with the computed digest */
1956 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
1957 /* try the old cookie? */
1958 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
1959 (ep->current_secret_number != ep->last_secret_number)) {
1960 /* compute digest with old */
1961 sctp_hmac_m(SCTP_HMAC,
1962 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
1963 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
1965 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
1973 * Now before we continue we must reconstruct our mbuf so that
1974 * normal processing of any other chunks will work.
1980 while (SCTP_BUF_NEXT(m_at) != NULL) {
1981 m_at = SCTP_BUF_NEXT(m_at);
1983 SCTP_BUF_NEXT(m_at) = m_sig;
1986 if (cookie_ok == 0) {
1988 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1989 printf("handle_cookie_echo: cookie signature validation failed!\n");
1990 printf("offset = %u, cookie_offset = %u, sig_offset = %u\n",
1991 (uint32_t) offset, cookie_offset, sig_offset);
1997 * check the cookie timestamps to be sure it's not stale
1999 SCTP_GETTIME_TIMEVAL(&now);
2000 /* Expire time is in Ticks, so we convert to seconds */
2001 time_expires.tv_sec = cookie->time_entered.tv_sec + cookie->cookie_life;
2002 time_expires.tv_usec = cookie->time_entered.tv_usec;
2003 if (timevalcmp(&now, &time_expires, >)) {
2004 /* cookie is stale! */
2005 struct mbuf *op_err;
2006 struct sctp_stale_cookie_msg *scm;
2009 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg),
2010 0, M_DONTWAIT, 1, MT_DATA);
2011 if (op_err == NULL) {
2015 /* pre-reserve some space */
2016 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
2017 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
2018 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
2021 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg);
2022 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
2023 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
2024 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
2025 (sizeof(uint32_t))));
2026 /* seconds to usec */
2027 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2030 tim = now.tv_usec - cookie->time_entered.tv_usec;
2031 scm->time_usec = htonl(tim);
2032 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag);
2036 * Now we must see with the lookup address if we have an existing
2037 * asoc. This will only happen if we were in the COOKIE-WAIT state
2038 * and a INIT collided with us and somewhere the peer sent the
2039 * cookie on another address besides the single address our assoc
2040 * had for him. In this case we will have one of the tie-tags set at
2041 * least AND the address field in the cookie can be used to look it
2045 if (cookie->addr_type == SCTP_IPV6_ADDRESS) {
2046 memset(&sin6, 0, sizeof(sin6));
2047 sin6.sin6_family = AF_INET6;
2048 sin6.sin6_len = sizeof(sin6);
2049 sin6.sin6_port = sh->src_port;
2050 sin6.sin6_scope_id = cookie->scope_id;
2051 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2052 sizeof(sin6.sin6_addr.s6_addr));
2053 to = (struct sockaddr *)&sin6;
2054 } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) {
2055 memset(&sin, 0, sizeof(sin));
2056 sin.sin_family = AF_INET;
2057 sin.sin_len = sizeof(sin);
2058 sin.sin_port = sh->src_port;
2059 sin.sin_addr.s_addr = cookie->address[0];
2060 to = (struct sockaddr *)&sin;
2062 if ((*stcb == NULL) && to) {
2063 /* Yep, lets check */
2064 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL);
2065 if (*stcb == NULL) {
2067 * We should have only got back the same inp. If we
2068 * got back a different ep we have a problem. The
2069 * original findep got back l_inp and now
2071 if (l_inp != *inp_p) {
2072 printf("Bad problem find_ep got a diff inp then special_locate?\n");
2075 if (*locked_tcb == NULL) {
2077 * In this case we found the assoc only
2078 * after we locked the create lock. This
2079 * means we are in a colliding case and we
2080 * must make sure that we unlock the tcb if
2081 * its one of the cases where we throw away
2082 * the incoming packets.
2084 *locked_tcb = *stcb;
2087 * We must also increment the inp ref count
2088 * since the ref_count flags was set when we
2089 * did not find the TCB, now we found it
2090 * which reduces the refcount.. we must
2091 * raise it back out to balance it all :-)
2093 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2094 if ((*stcb)->sctp_ep != l_inp) {
2095 printf("Huh? ep:%p diff then l_inp:%p?\n",
2096 (*stcb)->sctp_ep, l_inp);
2101 cookie_len -= SCTP_SIGNATURE_SIZE;
2102 if (*stcb == NULL) {
2103 /* this is the "normal" case... get a new TCB */
2104 *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie,
2105 cookie_len, *inp_p, netp, to, ¬ification,
2106 auth_skipped, auth_offset, auth_len);
2108 /* this is abnormal... cookie-echo on existing TCB */
2109 had_a_existing_tcb = 1;
2110 *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh,
2111 cookie, cookie_len, *inp_p, *stcb, *netp, to, ¬ification,
2115 if (*stcb == NULL) {
2116 /* still no TCB... must be bad cookie-echo */
2120 * Ok, we built an association so confirm the address we sent the
2123 netl = sctp_findnet(*stcb, to);
2125 * This code should in theory NOT run but
2128 /* TSNH! Huh, why do I need to add this address here? */
2131 ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE,
2132 SCTP_IN_COOKIE_PROC);
2133 netl = sctp_findnet(*stcb, to);
2136 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2137 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2138 sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2140 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2141 (*stcb), 0, (void *)netl);
2145 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p,
2148 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2149 if (!had_a_existing_tcb ||
2150 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2152 * If we have a NEW cookie or the connect never
2153 * reached the connected state during collision we
2154 * must do the TCP accept thing.
2156 struct socket *so, *oso;
2157 struct sctp_inpcb *inp;
2159 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2161 * For a restart we will keep the same
2162 * socket, no need to do anything. I THINK!!
2164 sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id);
2167 oso = (*inp_p)->sctp_socket;
2169 * We do this to keep the sockets side happy durin
2170 * the sonewcon ONLY.
2173 SCTP_TCB_UNLOCK((*stcb));
2174 so = sonewconn(oso, 0
2177 SCTP_INP_WLOCK((*stcb)->sctp_ep);
2178 SCTP_TCB_LOCK((*stcb));
2179 SCTP_INP_WUNLOCK((*stcb)->sctp_ep);
2181 struct mbuf *op_err;
2183 /* Too many sockets */
2185 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
2186 printf("process_cookie_new: no room for another socket!\n");
2188 #endif /* SCTP_DEBUG */
2189 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2190 sctp_abort_association(*inp_p, NULL, m, iphlen,
2192 sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2195 inp = (struct sctp_inpcb *)so->so_pcb;
2196 SCTP_INP_INCR_REF(inp);
2198 * We add the unbound flag here so that if we get an
2199 * soabort() before we get the move_pcb done, we
2200 * will properly cleanup.
2202 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2203 SCTP_PCB_FLAGS_CONNECTED |
2204 SCTP_PCB_FLAGS_IN_TCPPOOL |
2205 SCTP_PCB_FLAGS_UNBOUND |
2206 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2207 SCTP_PCB_FLAGS_DONT_WAKE);
2208 inp->sctp_features = (*inp_p)->sctp_features;
2209 inp->sctp_socket = so;
2210 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2211 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2212 inp->sctp_context = (*inp_p)->sctp_context;
2213 inp->inp_starting_point_for_iterator = NULL;
2215 * copy in the authentication parameters from the
2218 if (inp->sctp_ep.local_hmacs)
2219 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2220 inp->sctp_ep.local_hmacs =
2221 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2222 if (inp->sctp_ep.local_auth_chunks)
2223 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2224 inp->sctp_ep.local_auth_chunks =
2225 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2226 (void)sctp_copy_skeylist(&(*inp_p)->sctp_ep.shared_keys,
2227 &inp->sctp_ep.shared_keys);
2230 * Now we must move it from one hash table to
2231 * another and get the tcb in the right place.
2233 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2234 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb);
2237 * now we must check to see if we were aborted while
2238 * the move was going on and the lock/unlock
2241 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2243 * yep it was, we leave the assoc attached
2244 * to the socket since the sctp_inpcb_free()
2245 * call will send an abort for us.
2247 SCTP_INP_DECR_REF(inp);
2250 SCTP_INP_DECR_REF(inp);
2251 /* Switch over to the new guy */
2253 sctp_ulp_notify(notification, *stcb, 0, NULL);
2256 * Pull it from the incomplete queue and wake the
2263 if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2264 sctp_ulp_notify(notification, *stcb, 0, NULL);
2270 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp,
2271 struct sctp_tcb *stcb, struct sctp_nets *net)
2273 /* cp must not be used, others call this without a c-ack :-) */
2274 struct sctp_association *asoc;
2277 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2278 printf("sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2286 sctp_stop_all_cookie_timers(stcb);
2287 /* process according to association state */
2288 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2289 /* state change only needed when I am in right state */
2291 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2292 printf("moving to OPEN state\n");
2295 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2296 asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING;
2297 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2298 stcb->sctp_ep, stcb, asoc->primary_destination);
2301 asoc->state = SCTP_STATE_OPEN;
2304 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2305 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2306 if (asoc->overall_error_count == 0) {
2307 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2308 &asoc->time_entered);
2310 SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2311 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL);
2312 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2313 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2314 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2315 soisconnected(stcb->sctp_ep->sctp_socket);
2317 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2320 * since we did not send a HB make sure we don't double
2323 net->hb_responded = 1;
2325 if (stcb->asoc.sctp_autoclose_ticks &&
2326 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2327 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2328 stcb->sctp_ep, stcb, NULL);
2331 * set ASCONF timer if ASCONFs are pending and allowed (eg.
2332 * addresses changed when init/cookie echo in flight)
2334 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2335 (stcb->asoc.peer_supports_asconf) &&
2336 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2337 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2338 stcb->sctp_ep, stcb,
2339 stcb->asoc.primary_destination);
2342 /* Toss the cookie if I can */
2343 sctp_toss_old_cookies(stcb, asoc);
2344 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
2345 /* Restart the timer if we have pending data */
2346 struct sctp_tmit_chunk *chk;
2348 chk = TAILQ_FIRST(&asoc->sent_queue);
2350 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2357 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2358 struct sctp_tcb *stcb)
2360 struct sctp_nets *net;
2361 struct sctp_tmit_chunk *lchk;
2364 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) {
2367 SCTP_STAT_INCR(sctps_recvecne);
2368 tsn = ntohl(cp->tsn);
2369 /* ECN Nonce stuff: need a resync and disable the nonce sum check */
2370 /* Also we make sure we disable the nonce_wait */
2371 lchk = TAILQ_FIRST(&stcb->asoc.send_queue);
2373 stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq;
2375 stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq;
2377 stcb->asoc.nonce_wait_for_ecne = 0;
2378 stcb->asoc.nonce_sum_check = 0;
2380 /* Find where it was sent, if possible */
2382 lchk = TAILQ_FIRST(&stcb->asoc.sent_queue);
2384 if (lchk->rec.data.TSN_seq == tsn) {
2388 if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ))
2390 lchk = TAILQ_NEXT(lchk, sctp_next);
2393 /* default is we use the primary */
2394 net = stcb->asoc.primary_destination;
2396 if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) {
2397 #ifdef SCTP_CWND_MONITOR
2400 old_cwnd = net->cwnd;
2402 SCTP_STAT_INCR(sctps_ecnereducedcwnd);
2403 net->ssthresh = net->cwnd / 2;
2404 if (net->ssthresh < net->mtu) {
2405 net->ssthresh = net->mtu;
2406 /* here back off the timer as well, to slow us down */
2409 net->cwnd = net->ssthresh;
2410 #ifdef SCTP_CWND_MONITOR
2411 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_SAT);
2414 * we reduce once every RTT. So we will only lower cwnd at
2415 * the next sending seq i.e. the resync_tsn.
2417 stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn;
2420 * We always send a CWR this way if our previous one was lost our
2421 * peer will get an update, or if it is not time again to reduce we
2422 * still get the cwr to the peer.
2424 sctp_send_cwr(stcb, net, tsn);
2428 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb)
2431 * Here we get a CWR from the peer. We must look in the outqueue and
2432 * make sure that we have a covered ECNE in teh control chunk part.
2435 struct sctp_tmit_chunk *chk;
2436 struct sctp_ecne_chunk *ecne;
2438 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
2439 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
2443 * Look for and remove if it is the right TSN. Since there
2444 * is only ONE ECNE on the control queue at any one time we
2445 * don't need to worry about more than one!
2447 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
2448 if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn),
2449 MAX_TSN) || (cp->tsn == ecne->tsn)) {
2450 /* this covers this ECNE, we can remove it */
2451 stcb->asoc.ecn_echo_cnt_onq--;
2452 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
2455 sctp_m_freem(chk->data);
2458 stcb->asoc.ctrl_queue_cnt--;
2459 sctp_free_remote_addr(chk->whoTo);
2460 sctp_free_a_chunk(stcb, chk);
2467 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp,
2468 struct sctp_tcb *stcb, struct sctp_nets *net)
2470 struct sctp_association *asoc;
2473 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2474 printf("sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
2481 /* process according to association state */
2482 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
2483 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
2484 SCTP_TCB_UNLOCK(stcb);
2487 /* notify upper layer protocol */
2488 if (stcb->sctp_socket) {
2489 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL);
2490 /* are the queues empty? they should be */
2491 if (!TAILQ_EMPTY(&asoc->send_queue) ||
2492 !TAILQ_EMPTY(&asoc->sent_queue) ||
2493 !TAILQ_EMPTY(&asoc->out_wheel)) {
2494 sctp_report_all_outbound(stcb, 0);
2497 /* stop the timer */
2498 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2499 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
2501 sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2506 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
2507 struct sctp_nets *net, uint8_t flg)
2509 switch (desc->chunk_type) {
2511 /* find the tsn to resend (possibly */
2514 struct sctp_tmit_chunk *tp1;
2516 tsn = ntohl(desc->tsn_ifany);
2517 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2519 if (tp1->rec.data.TSN_seq == tsn) {
2523 if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn,
2529 tp1 = TAILQ_NEXT(tp1, sctp_next);
2533 * Do it the other way , aka without paying
2534 * attention to queue seq order.
2536 SCTP_STAT_INCR(sctps_pdrpdnfnd);
2537 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2539 if (tp1->rec.data.TSN_seq == tsn) {
2543 tp1 = TAILQ_NEXT(tp1, sctp_next);
2547 SCTP_STAT_INCR(sctps_pdrptsnnf);
2549 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
2552 if ((stcb->asoc.peers_rwnd == 0) &&
2553 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
2554 SCTP_STAT_INCR(sctps_pdrpdiwnp);
2557 if (stcb->asoc.peers_rwnd == 0 &&
2558 (flg & SCTP_FROM_MIDDLE_BOX)) {
2559 SCTP_STAT_INCR(sctps_pdrpdizrw);
2562 ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+
2563 sizeof(struct sctp_data_chunk));
2567 for (iii = 0; iii < sizeof(desc->data_bytes);
2569 if (ddp[iii] != desc->data_bytes[iii]) {
2570 SCTP_STAT_INCR(sctps_pdrpbadd);
2576 * We zero out the nonce so resync not
2579 tp1->rec.data.ect_nonce = 0;
2583 * this guy had a RTO calculation
2584 * pending on it, cancel it
2588 SCTP_STAT_INCR(sctps_pdrpmark);
2589 if (tp1->sent != SCTP_DATAGRAM_RESEND)
2590 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
2591 tp1->sent = SCTP_DATAGRAM_RESEND;
2593 * mark it as if we were doing a FR, since
2594 * we will be getting gap ack reports behind
2595 * the info from the router.
2597 tp1->rec.data.doing_fast_retransmit = 1;
2599 * mark the tsn with what sequences can
2602 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
2603 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
2605 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
2608 /* restart the timer */
2609 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2610 stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2611 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2614 /* fix counts and things */
2615 #ifdef SCTP_FLIGHT_LOGGING
2616 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
2617 tp1->whoTo->flight_size,
2620 tp1->rec.data.TSN_seq);
2622 sctp_flight_size_decrease(tp1);
2623 sctp_total_flight_decrease(stcb, tp1);
2629 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
2630 if (tp1->sent == SCTP_DATAGRAM_RESEND)
2633 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
2635 if (tp1->sent == SCTP_DATAGRAM_RESEND)
2638 if (audit != stcb->asoc.sent_queue_retran_cnt) {
2639 printf("**Local Audit finds cnt:%d asoc cnt:%d\n",
2640 audit, stcb->asoc.sent_queue_retran_cnt);
2641 #ifndef SCTP_AUDITING_ENABLED
2642 stcb->asoc.sent_queue_retran_cnt = audit;
2650 struct sctp_tmit_chunk *asconf;
2652 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
2654 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
2659 if (asconf->sent != SCTP_DATAGRAM_RESEND)
2660 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
2661 asconf->sent = SCTP_DATAGRAM_RESEND;
2662 asconf->snd_count--;
2666 case SCTP_INITIATION:
2667 /* resend the INIT */
2668 stcb->asoc.dropped_special_cnt++;
2669 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
2671 * If we can get it in, in a few attempts we do
2672 * this, otherwise we let the timer fire.
2674 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
2675 stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
2676 sctp_send_initiate(stcb->sctp_ep, stcb);
2679 case SCTP_SELECTIVE_ACK:
2680 /* resend the sack */
2681 sctp_send_sack(stcb);
2683 case SCTP_HEARTBEAT_REQUEST:
2684 /* resend a demand HB */
2685 sctp_send_hb(stcb, 1, net);
2688 sctp_send_shutdown(stcb, net);
2690 case SCTP_SHUTDOWN_ACK:
2691 sctp_send_shutdown_ack(stcb, net);
2693 case SCTP_COOKIE_ECHO:
2695 struct sctp_tmit_chunk *cookie;
2698 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
2700 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
2705 if (cookie->sent != SCTP_DATAGRAM_RESEND)
2706 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
2707 cookie->sent = SCTP_DATAGRAM_RESEND;
2708 sctp_stop_all_cookie_timers(stcb);
2712 case SCTP_COOKIE_ACK:
2713 sctp_send_cookie_ack(stcb);
2715 case SCTP_ASCONF_ACK:
2716 /* resend last asconf ack */
2717 sctp_send_asconf_ack(stcb, 1);
2719 case SCTP_FORWARD_CUM_TSN:
2720 send_forward_tsn(stcb, &stcb->asoc);
2722 /* can't do anything with these */
2723 case SCTP_PACKET_DROPPED:
2724 case SCTP_INITIATION_ACK: /* this should not happen */
2725 case SCTP_HEARTBEAT_ACK:
2726 case SCTP_ABORT_ASSOCIATION:
2727 case SCTP_OPERATION_ERROR:
2728 case SCTP_SHUTDOWN_COMPLETE:
2738 sctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
2744 * We set things to 0xffff since this is the last delivered sequence
2745 * and we will be sending in 0 after the reset.
2748 if (number_entries) {
2749 for (i = 0; i < number_entries; i++) {
2750 temp = ntohs(list[i]);
2751 if (temp >= stcb->asoc.streamincnt) {
2754 stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff;
2758 for (i = 0; i < stcb->asoc.streamincnt; i++) {
2759 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
2762 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list);
2766 sctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
2770 if (number_entries == 0) {
2771 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
2772 stcb->asoc.strmout[i].next_sequence_sent = 0;
2774 } else if (number_entries) {
2775 for (i = 0; i < number_entries; i++) {
2778 temp = ntohs(list[i]);
2779 if (temp >= stcb->asoc.streamoutcnt) {
2780 /* no such stream */
2783 stcb->asoc.strmout[temp].next_sequence_sent = 0;
2786 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list);
2790 struct sctp_stream_reset_out_request *
2791 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
2793 struct sctp_association *asoc;
2794 struct sctp_stream_reset_out_req *req;
2795 struct sctp_stream_reset_out_request *r;
2796 struct sctp_tmit_chunk *chk;
2800 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
2803 if (stcb->asoc.str_reset == NULL) {
2806 chk = stcb->asoc.str_reset;
2807 if (chk->data == NULL) {
2811 /* he wants a copy of the chk pointer */
2814 clen = chk->send_size;
2815 req = mtod(chk->data, struct sctp_stream_reset_out_req *);
2817 if (ntohl(r->request_seq) == seq) {
2821 len = SCTP_SIZE32(ntohs(r->ph.param_length));
2822 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
2823 /* move to the next one, there can only be a max of two */
2824 r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len);
2825 if (ntohl(r->request_seq) == seq) {
2829 /* that seq is not here */
2834 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
2836 struct sctp_association *asoc;
2837 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
2839 if (stcb->asoc.str_reset == NULL) {
2844 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
2845 TAILQ_REMOVE(&asoc->control_send_queue,
2849 sctp_m_freem(chk->data);
2852 asoc->ctrl_queue_cnt--;
2853 sctp_free_remote_addr(chk->whoTo);
2855 sctp_free_a_chunk(stcb, chk);
2856 stcb->asoc.str_reset = NULL;
2861 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
2862 uint32_t seq, uint32_t action,
2863 struct sctp_stream_reset_response *respin)
2867 struct sctp_association *asoc = &stcb->asoc;
2868 struct sctp_tmit_chunk *chk;
2869 struct sctp_stream_reset_out_request *srparam;
2872 if (asoc->stream_reset_outstanding == 0) {
2876 if (seq == stcb->asoc.str_reset_seq_out) {
2877 srparam = sctp_find_stream_reset(stcb, seq, &chk);
2879 stcb->asoc.str_reset_seq_out++;
2880 type = ntohs(srparam->ph.param_type);
2881 lparm_len = ntohs(srparam->ph.param_length);
2882 if (type == SCTP_STR_RESET_OUT_REQUEST) {
2883 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
2884 asoc->stream_reset_out_is_outstanding = 0;
2885 if (asoc->stream_reset_outstanding)
2886 asoc->stream_reset_outstanding--;
2887 if (action == SCTP_STREAM_RESET_PERFORMED) {
2889 sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams);
2891 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams);
2893 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
2894 /* Answered my request */
2895 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
2896 if (asoc->stream_reset_outstanding)
2897 asoc->stream_reset_outstanding--;
2898 if (action != SCTP_STREAM_RESET_PERFORMED) {
2899 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams);
2901 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
2903 * a) Adopt the new in tsn.
2905 * c) Adopt the new out-tsn
2907 struct sctp_stream_reset_response_tsn *resp;
2908 struct sctp_forward_tsn_chunk fwdtsn;
2911 if (respin == NULL) {
2915 if (action == SCTP_STREAM_RESET_PERFORMED) {
2916 resp = (struct sctp_stream_reset_response_tsn *)respin;
2917 asoc->stream_reset_outstanding--;
2918 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
2919 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
2920 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
2921 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag);
2925 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
2926 stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
2927 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
2928 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
2929 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
2930 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
2932 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
2933 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
2937 /* get rid of the request and get the request flags */
2938 if (asoc->stream_reset_outstanding == 0) {
2939 sctp_clean_up_stream_reset(stcb);
2947 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
2948 struct sctp_tmit_chunk *chk,
2949 struct sctp_stream_reset_in_request *req)
2957 * peer wants me to send a str-reset to him for my outgoing seq's if
2960 struct sctp_association *asoc = &stcb->asoc;
2962 seq = ntohl(req->request_seq);
2963 if (asoc->str_reset_seq_in == seq) {
2964 if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
2965 len = ntohs(req->ph.param_length);
2966 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
2967 for (i = 0; i < number_entries; i++) {
2968 temp = ntohs(req->list_of_streams[i]);
2969 req->list_of_streams[i] = temp;
2971 /* move the reset action back one */
2972 asoc->last_reset_action[1] = asoc->last_reset_action[0];
2973 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
2974 sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams,
2975 asoc->str_reset_seq_out,
2976 seq, (asoc->sending_seq - 1));
2977 asoc->stream_reset_out_is_outstanding = 1;
2978 asoc->str_reset = chk;
2979 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
2980 stcb->asoc.stream_reset_outstanding++;
2982 /* Can't do it, since we have sent one out */
2983 asoc->last_reset_action[1] = asoc->last_reset_action[0];
2984 asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER;
2985 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
2987 asoc->str_reset_seq_in++;
2988 } else if (asoc->str_reset_seq_in - 1 == seq) {
2989 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
2990 } else if (asoc->str_reset_seq_in - 2 == seq) {
2991 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
2993 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
2998 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
2999 struct sctp_tmit_chunk *chk,
3000 struct sctp_stream_reset_tsn_request *req)
3002 /* reset all in and out and update the tsn */
3004 * A) reset my str-seq's on in and out. B) Select a receive next,
3005 * and set cum-ack to it. Also process this selected number as a
3006 * fwd-tsn as well. C) set in the response my next sending seq.
3008 struct sctp_forward_tsn_chunk fwdtsn;
3009 struct sctp_association *asoc = &stcb->asoc;
3013 seq = ntohl(req->request_seq);
3014 if (asoc->str_reset_seq_in == seq) {
3015 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3016 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3017 fwdtsn.ch.chunk_flags = 0;
3018 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3019 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag);
3023 stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3024 stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3025 stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1;
3026 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3027 atomic_add_int(&stcb->asoc.sending_seq, 1);
3028 /* save off historical data for retrans */
3029 stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0];
3030 stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq;
3031 stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0];
3032 stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn;
3034 sctp_add_stream_reset_result_tsn(chk,
3035 ntohl(req->request_seq),
3036 SCTP_STREAM_RESET_PERFORMED,
3037 stcb->asoc.sending_seq,
3038 stcb->asoc.mapping_array_base_tsn);
3039 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3040 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3041 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3042 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3044 asoc->str_reset_seq_in++;
3045 } else if (asoc->str_reset_seq_in - 1 == seq) {
3046 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3047 stcb->asoc.last_sending_seq[0],
3048 stcb->asoc.last_base_tsnsent[0]
3050 } else if (asoc->str_reset_seq_in - 2 == seq) {
3051 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3052 stcb->asoc.last_sending_seq[1],
3053 stcb->asoc.last_base_tsnsent[1]
3056 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3062 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3063 struct sctp_tmit_chunk *chk,
3064 struct sctp_stream_reset_out_request *req)
3067 int number_entries, len;
3068 struct sctp_association *asoc = &stcb->asoc;
3070 seq = ntohl(req->request_seq);
3072 /* now if its not a duplicate we process it */
3073 if (asoc->str_reset_seq_in == seq) {
3074 len = ntohs(req->ph.param_length);
3075 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3077 * the sender is resetting, handle the list issue.. we must
3078 * a) verify if we can do the reset, if so no problem b) If
3079 * we can't do the reset we must copy the request. c) queue
3080 * it, and setup the data in processor to trigger it off
3081 * when needed and dequeue all the queued data.
3083 tsn = ntohl(req->send_reset_at_tsn);
3085 /* move the reset action back one */
3086 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3087 if ((tsn == asoc->cumulative_tsn) ||
3088 (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN))) {
3089 /* we can do it now */
3090 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3091 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3092 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3095 * we must queue it up and thus wait for the TSN's
3096 * to arrive that are at or before tsn
3098 struct sctp_stream_reset_list *liste;
3101 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3102 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3104 if (liste == NULL) {
3105 /* gak out of memory */
3106 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED);
3107 asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3111 liste->number_entries = number_entries;
3112 memcpy(&liste->req, req,
3113 (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t))));
3114 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3115 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3116 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3118 asoc->str_reset_seq_in++;
3119 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3121 * one seq back, just echo back last action since my
3122 * response was lost.
3124 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3125 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3127 * two seq back, just echo back last action since my
3128 * response was lost.
3130 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3132 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3137 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct sctp_stream_reset_out_req *sr_req)
3139 int chk_length, param_len, ptype;
3142 struct sctp_tmit_chunk *chk;
3143 struct sctp_chunkhdr *ch;
3144 struct sctp_paramhdr *ph;
3148 /* now it may be a reset or a reset-response */
3149 chk_length = ntohs(sr_req->ch.chunk_length);
3151 /* setup for adding the response */
3152 sctp_alloc_a_chunk(stcb, chk);
3156 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
3157 chk->asoc = &stcb->asoc;
3158 chk->no_fr_allowed = 0;
3159 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
3160 chk->book_size_scale = 0;
3161 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
3162 if (chk->data == NULL) {
3165 sctp_m_freem(chk->data);
3168 sctp_free_a_chunk(stcb, chk);
3171 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
3173 /* setup chunk parameters */
3174 chk->sent = SCTP_DATAGRAM_UNSENT;
3176 chk->whoTo = stcb->asoc.primary_destination;
3177 atomic_add_int(&chk->whoTo->ref_count, 1);
3179 ch = mtod(chk->data, struct sctp_chunkhdr *);
3180 ch->chunk_type = SCTP_STREAM_RESET;
3181 ch->chunk_flags = 0;
3182 ch->chunk_length = htons(chk->send_size);
3183 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
3185 ph = (struct sctp_paramhdr *)&sr_req->sr_req;
3186 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) {
3187 param_len = ntohs(ph->param_length);
3188 if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) {
3192 ptype = ntohs(ph->param_type);
3194 if (num_param > SCTP_MAX_RESET_PARAMS) {
3195 /* hit the max of parameters already sorry.. */
3198 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
3199 struct sctp_stream_reset_out_request *req_out;
3201 req_out = (struct sctp_stream_reset_out_request *)ph;
3203 if (stcb->asoc.stream_reset_outstanding) {
3204 seq = ntohl(req_out->response_seq);
3205 if (seq == stcb->asoc.str_reset_seq_out) {
3207 sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL);
3210 sctp_handle_str_reset_request_out(stcb, chk, req_out);
3211 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
3212 struct sctp_stream_reset_in_request *req_in;
3215 req_in = (struct sctp_stream_reset_in_request *)ph;
3216 sctp_handle_str_reset_request_in(stcb, chk, req_in);
3217 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
3218 struct sctp_stream_reset_tsn_request *req_tsn;
3221 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
3222 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
3224 goto strres_nochunk;
3228 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
3229 struct sctp_stream_reset_response *resp;
3232 resp = (struct sctp_stream_reset_response *)ph;
3233 seq = ntohl(resp->response_seq);
3234 result = ntohl(resp->result);
3235 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
3237 goto strres_nochunk;
3243 ph = (struct sctp_paramhdr *)((caddr_t)ph + SCTP_SIZE32(param_len));
3244 chk_length -= SCTP_SIZE32(param_len);
3247 /* we have no response free the stuff */
3248 goto strres_nochunk;
3250 /* ok we have a chunk to link in */
3251 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
3254 stcb->asoc.ctrl_queue_cnt++;
3259 * Handle a router or endpoints report of a packet loss, there are two ways
3260 * to handle this, either we get the whole packet and must disect it
3261 * ourselves (possibly with truncation and or corruption) or it is a summary
3262 * from a middle box that did the disectting for us.
3265 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
3266 struct sctp_tcb *stcb, struct sctp_nets *net)
3268 uint32_t bottle_bw, on_queue;
3272 struct sctp_chunk_desc desc;
3273 struct sctp_chunkhdr *ch;
3275 chlen = ntohs(cp->ch.chunk_length);
3276 chlen -= sizeof(struct sctp_pktdrop_chunk);
3277 /* XXX possible chlen underflow */
3280 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
3281 SCTP_STAT_INCR(sctps_pdrpbwrpt);
3283 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
3284 chlen -= sizeof(struct sctphdr);
3285 /* XXX possible chlen underflow */
3286 memset(&desc, 0, sizeof(desc));
3288 trunc_len = (uint16_t) ntohs(cp->trunc_len);
3289 /* now the chunks themselves */
3290 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
3291 desc.chunk_type = ch->chunk_type;
3292 /* get amount we need to move */
3293 at = ntohs(ch->chunk_length);
3294 if (at < sizeof(struct sctp_chunkhdr)) {
3295 /* corrupt chunk, maybe at the end? */
3296 SCTP_STAT_INCR(sctps_pdrpcrupt);
3299 if (trunc_len == 0) {
3300 /* we are supposed to have all of it */
3302 /* corrupt skip it */
3303 SCTP_STAT_INCR(sctps_pdrpcrupt);
3307 /* is there enough of it left ? */
3308 if (desc.chunk_type == SCTP_DATA) {
3309 if (chlen < (sizeof(struct sctp_data_chunk) +
3310 sizeof(desc.data_bytes))) {
3314 if (chlen < sizeof(struct sctp_chunkhdr)) {
3319 if (desc.chunk_type == SCTP_DATA) {
3320 /* can we get out the tsn? */
3321 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
3322 SCTP_STAT_INCR(sctps_pdrpmbda);
3324 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
3326 struct sctp_data_chunk *dcp;
3330 dcp = (struct sctp_data_chunk *)ch;
3331 ddp = (uint8_t *) (dcp + 1);
3332 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
3333 desc.data_bytes[iii] = ddp[iii];
3335 desc.tsn_ifany = dcp->dp.tsn;
3337 /* nope we are done. */
3338 SCTP_STAT_INCR(sctps_pdrpnedat);
3342 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
3343 SCTP_STAT_INCR(sctps_pdrpmbct);
3346 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
3347 SCTP_STAT_INCR(sctps_pdrppdbrk);
3350 if (SCTP_SIZE32(at) > chlen) {
3353 chlen -= SCTP_SIZE32(at);
3354 if (chlen < sizeof(struct sctp_chunkhdr)) {
3355 /* done, none left */
3358 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
3360 /* Now update any rwnd --- possibly */
3361 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
3362 /* From a peer, we get a rwnd report */
3365 SCTP_STAT_INCR(sctps_pdrpfehos);
3367 bottle_bw = ntohl(cp->bottle_bw);
3368 on_queue = ntohl(cp->current_onq);
3369 if (bottle_bw && on_queue) {
3370 /* a rwnd report is in here */
3371 if (bottle_bw > on_queue)
3372 a_rwnd = bottle_bw - on_queue;
3377 stcb->asoc.peers_rwnd = 0;
3379 if (a_rwnd > stcb->asoc.total_flight) {
3380 stcb->asoc.peers_rwnd =
3381 a_rwnd - stcb->asoc.total_flight;
3383 stcb->asoc.peers_rwnd = 0;
3385 if (stcb->asoc.peers_rwnd <
3386 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3387 /* SWS sender side engages */
3388 stcb->asoc.peers_rwnd = 0;
3393 SCTP_STAT_INCR(sctps_pdrpfmbox);
3396 /* now middle boxes in sat networks get a cwnd bump */
3397 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
3398 (stcb->asoc.sat_t3_loss_recovery == 0) &&
3399 (stcb->asoc.sat_network)) {
3401 * This is debateable but for sat networks it makes sense
3402 * Note if a T3 timer has went off, we will prohibit any
3403 * changes to cwnd until we exit the t3 loss recovery.
3408 #ifdef SCTP_CWND_MONITOR
3409 int old_cwnd = net->cwnd;
3412 /* need real RTT for this calc */
3413 rtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
3414 /* get bottle neck bw */
3415 bottle_bw = ntohl(cp->bottle_bw);
3416 /* and whats on queue */
3417 on_queue = ntohl(cp->current_onq);
3419 * adjust the on-queue if our flight is more it could be
3420 * that the router has not yet gotten data "in-flight" to it
3422 if (on_queue < net->flight_size)
3423 on_queue = net->flight_size;
3425 /* calculate the available space */
3426 bw_avail = (bottle_bw * rtt) / 1000;
3427 if (bw_avail > bottle_bw) {
3429 * Cap the growth to no more than the bottle neck.
3430 * This can happen as RTT slides up due to queues.
3431 * It also means if you have more than a 1 second
3432 * RTT with a empty queue you will be limited to the
3433 * bottle_bw per second no matter if other points
3434 * have 1/2 the RTT and you could get more out...
3436 bw_avail = bottle_bw;
3438 if (on_queue > bw_avail) {
3440 * No room for anything else don't allow anything
3441 * else to be "added to the fire".
3443 int seg_inflight, seg_onqueue, my_portion;
3445 net->partial_bytes_acked = 0;
3447 /* how much are we over queue size? */
3448 incr = on_queue - bw_avail;
3449 if (stcb->asoc.seen_a_sack_this_pkt) {
3451 * undo any cwnd adjustment that the sack
3454 net->cwnd = net->prev_cwnd;
3456 /* Now how much of that is mine? */
3457 seg_inflight = net->flight_size / net->mtu;
3458 seg_onqueue = on_queue / net->mtu;
3459 my_portion = (incr * seg_inflight) / seg_onqueue;
3461 /* Have I made an adjustment already */
3462 if (net->cwnd > net->flight_size) {
3464 * for this flight I made an adjustment we
3465 * need to decrease the portion by a share
3466 * our previous adjustment.
3470 diff_adj = net->cwnd - net->flight_size;
3471 if (diff_adj > my_portion)
3474 my_portion -= diff_adj;
3477 * back down to the previous cwnd (assume we have
3478 * had a sack before this packet). minus what ever
3479 * portion of the overage is my fault.
3481 net->cwnd -= my_portion;
3483 /* we will NOT back down more than 1 MTU */
3484 if (net->cwnd <= net->mtu) {
3485 net->cwnd = net->mtu;
3488 net->ssthresh = net->cwnd - 1;
3491 * Take 1/4 of the space left or max burst up ..
3492 * whichever is less.
3494 incr = min((bw_avail - on_queue) >> 2,
3495 (int)stcb->asoc.max_burst * (int)net->mtu);
3498 if (net->cwnd > bw_avail) {
3499 /* We can't exceed the pipe size */
3500 net->cwnd = bw_avail;
3502 if (net->cwnd < net->mtu) {
3503 /* We always have 1 MTU */
3504 net->cwnd = net->mtu;
3506 #ifdef SCTP_CWND_MONITOR
3507 if (net->cwnd - old_cwnd != 0) {
3508 /* log only changes */
3509 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd),
3510 SCTP_CWND_LOG_FROM_SAT);
3517 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
3518 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
3519 * offset: offset into the mbuf chain to first chunkhdr - length: is the
3520 * length of the complete packet outputs: - length: modified to remaining
3521 * length after control processing - netp: modified to new sctp_nets after
3522 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
3523 * bad packet,...) otherwise return the tcb for this packet
3525 static struct sctp_tcb *
3526 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
3527 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
3528 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen)
3530 struct sctp_association *asoc;
3532 int num_chunks = 0; /* number of control chunks processed */
3535 int abort_no_unlock = 0;
3538 * How big should this be, and should it be alloc'd? Lets try the
3539 * d-mtu-ceiling for now (2k) and that should hopefully work ...
3540 * until we get into jumbo grams and such..
3542 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
3543 struct sctp_tcb *locked_tcb = stcb;
3545 uint32_t auth_offset = 0, auth_len = 0;
3546 int auth_skipped = 0;
3549 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3550 printf("sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
3551 iphlen, *offset, length, stcb);
3553 #endif /* SCTP_DEBUG */
3555 /* validate chunk header length... */
3556 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
3560 * validate the verification tag
3562 vtag_in = ntohl(sh->v_tag);
3565 SCTP_TCB_LOCK_ASSERT(locked_tcb);
3567 if (ch->chunk_type == SCTP_INITIATION) {
3569 /* protocol error- silently discard... */
3570 SCTP_STAT_INCR(sctps_badvtag);
3572 SCTP_TCB_UNLOCK(locked_tcb);
3575 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
3577 * If there is no stcb, skip the AUTH chunk and process
3578 * later after a stcb is found (to validate the lookup was
3581 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
3582 (stcb == NULL) && !sctp_auth_disable) {
3583 /* save this chunk for later processing */
3585 auth_offset = *offset;
3586 auth_len = ntohs(ch->chunk_length);
3588 /* (temporarily) move past this chunk */
3589 *offset += SCTP_SIZE32(auth_len);
3590 if (*offset >= length) {
3591 /* no more data left in the mbuf chain */
3595 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3596 sizeof(struct sctp_chunkhdr), chunk_buf);
3598 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
3599 goto process_control_chunks;
3602 * first check if it's an ASCONF with an unknown src addr we
3603 * need to look inside to find the association
3605 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
3606 /* inp's refcount may be reduced */
3607 SCTP_INP_INCR_REF(inp);
3609 stcb = sctp_findassociation_ep_asconf(m, iphlen,
3610 *offset, sh, &inp, netp);
3613 * reduce inp's refcount if not reduced in
3614 * sctp_findassociation_ep_asconf().
3616 SCTP_INP_DECR_REF(inp);
3618 /* now go back and verify any auth chunk to be sure */
3619 if (auth_skipped && (stcb != NULL)) {
3620 struct sctp_auth_chunk *auth;
3622 auth = (struct sctp_auth_chunk *)
3623 sctp_m_getptr(m, auth_offset,
3624 auth_len, chunk_buf);
3627 if (sctp_handle_auth(stcb, auth, m,
3629 /* auth HMAC failed so dump it */
3633 /* remaining chunks are HMAC checked */
3634 stcb->asoc.authenticated = 1;
3639 /* no association, so it's out of the blue... */
3640 sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL);
3643 SCTP_TCB_UNLOCK(locked_tcb);
3647 /* ABORT and SHUTDOWN can use either v_tag... */
3648 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
3649 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
3650 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
3651 if ((vtag_in == asoc->my_vtag) ||
3652 ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
3653 (vtag_in == asoc->peer_vtag))) {
3656 /* drop this packet... */
3657 SCTP_STAT_INCR(sctps_badvtag);
3659 SCTP_TCB_UNLOCK(locked_tcb);
3662 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
3663 if (vtag_in != asoc->my_vtag) {
3665 * this could be a stale SHUTDOWN-ACK or the
3666 * peer never got the SHUTDOWN-COMPLETE and
3667 * is still hung; we have started a new asoc
3668 * but it won't complete until the shutdown
3672 SCTP_TCB_UNLOCK(locked_tcb);
3673 sctp_handle_ootb(m, iphlen, *offset, sh, inp,
3678 /* for all other chunks, vtag must match */
3679 if (vtag_in != asoc->my_vtag) {
3680 /* invalid vtag... */
3682 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3683 printf("invalid vtag: %xh, expect %xh\n", vtag_in, asoc->my_vtag);
3685 #endif /* SCTP_DEBUG */
3686 SCTP_STAT_INCR(sctps_badvtag);
3688 SCTP_TCB_UNLOCK(locked_tcb);
3693 } /* end if !SCTP_COOKIE_ECHO */
3695 * process all control chunks...
3697 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
3698 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
3699 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
3700 /* implied cookie-ack.. we must have lost the ack */
3701 stcb->asoc.overall_error_count = 0;
3702 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
3705 process_control_chunks:
3707 while (IS_SCTP_CONTROL(ch)) {
3708 /* validate chunk length */
3709 chk_length = ntohs(ch->chunk_length);
3711 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
3712 printf("sctp_process_control: processing a chunk type=%u, len=%u\n",
3713 ch->chunk_type, chk_length);
3715 #endif /* SCTP_DEBUG */
3716 if ((size_t)chk_length < sizeof(*ch) ||
3717 (*offset + chk_length) > length) {
3720 SCTP_TCB_UNLOCK(locked_tcb);
3723 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
3725 * INIT-ACK only gets the init ack "header" portion only
3726 * because we don't have to process the peer's COOKIE. All
3727 * others get a complete chunk.
3729 if (ch->chunk_type == SCTP_INITIATION_ACK) {
3730 /* get an init-ack chunk */
3731 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3732 sizeof(struct sctp_init_ack_chunk), chunk_buf);
3736 SCTP_TCB_UNLOCK(locked_tcb);
3740 /* get a complete chunk... */
3741 if ((size_t)chk_length > sizeof(chunk_buf)) {
3743 struct sctp_paramhdr *phdr;
3746 oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
3747 0, M_DONTWAIT, 1, MT_DATA);
3749 /* pre-reserve some space */
3750 SCTP_BUF_RESV_UF(oper, sizeof(struct sctp_chunkhdr));
3751 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr);
3752 phdr = mtod(oper, struct sctp_paramhdr *);
3753 phdr->param_type = htons(SCTP_CAUSE_OUT_OF_RESC);
3754 phdr->param_length = htons(sizeof(struct sctp_paramhdr));
3755 sctp_queue_op_err(stcb, oper);
3758 SCTP_TCB_UNLOCK(locked_tcb);
3761 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3762 chk_length, chunk_buf);
3764 printf("sctp_process_control: Can't get the all data....\n");
3767 SCTP_TCB_UNLOCK(locked_tcb);
3772 /* Save off the last place we got a control from */
3774 if ((*netp != NULL) || (ch->chunk_type == SCTP_ASCONF)) {
3776 * allow last_control to be NULL if
3777 * ASCONF... ASCONF processing will find the
3780 stcb->asoc.last_control_chunk_from = *netp;
3783 #ifdef SCTP_AUDITING_ENABLED
3784 sctp_audit_log(0xB0, ch->chunk_type);
3787 /* check to see if this chunk required auth, but isn't */
3788 if ((stcb != NULL) && !sctp_auth_disable &&
3789 sctp_auth_is_required_chunk(ch->chunk_type,
3790 stcb->asoc.local_auth_chunks) &&
3791 !stcb->asoc.authenticated) {
3792 /* "silently" ignore */
3793 SCTP_STAT_INCR(sctps_recvauthmissing);
3796 switch (ch->chunk_type) {
3797 case SCTP_INITIATION:
3798 /* must be first and only chunk */
3800 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3801 printf("SCTP_INIT\n");
3803 #endif /* SCTP_DEBUG */
3804 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3805 /* We are not interested anymore? */
3806 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
3808 * collision case where we are
3809 * sending to them too
3814 SCTP_TCB_UNLOCK(locked_tcb);
3819 if ((num_chunks > 1) ||
3820 (sctp_strict_init && (length - *offset > SCTP_SIZE32(chk_length)))) {
3823 SCTP_TCB_UNLOCK(locked_tcb);
3826 if ((stcb != NULL) &&
3827 (SCTP_GET_STATE(&stcb->asoc) ==
3828 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
3829 sctp_send_shutdown_ack(stcb,
3830 stcb->asoc.primary_destination);
3832 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC);
3834 SCTP_TCB_UNLOCK(locked_tcb);
3837 sctp_handle_init(m, iphlen, *offset, sh,
3838 (struct sctp_init_chunk *)ch, inp, stcb, *netp, &abort_no_unlock);
3839 if (abort_no_unlock)
3844 SCTP_TCB_UNLOCK(locked_tcb);
3847 case SCTP_INITIATION_ACK:
3848 /* must be first and only chunk */
3850 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3851 printf("SCTP_INIT-ACK\n");
3853 #endif /* SCTP_DEBUG */
3854 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3855 /* We are not interested anymore */
3856 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
3860 SCTP_TCB_UNLOCK(locked_tcb);
3863 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3868 if ((num_chunks > 1) ||
3869 (sctp_strict_init && (length - *offset > SCTP_SIZE32(chk_length)))) {
3872 SCTP_TCB_UNLOCK(locked_tcb);
3875 ret = sctp_handle_init_ack(m, iphlen, *offset, sh,
3876 (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
3878 * Special case, I must call the output routine to
3879 * get the cookie echoed
3881 if (abort_no_unlock)
3884 if ((stcb) && ret == 0)
3885 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC);
3888 SCTP_TCB_UNLOCK(locked_tcb);
3891 case SCTP_SELECTIVE_ACK:
3893 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3894 printf("SCTP_SACK\n");
3896 #endif /* SCTP_DEBUG */
3897 SCTP_STAT_INCR(sctps_recvsacks);
3899 struct sctp_sack_chunk *sack;
3901 uint32_t a_rwnd, cum_ack;
3905 sack = (struct sctp_sack_chunk *)ch;
3907 nonce_sum_flag = ch->chunk_flags & SCTP_SACK_NONCE_SUM;
3908 cum_ack = ntohl(sack->sack.cum_tsn_ack);
3909 num_seg = ntohs(sack->sack.num_gap_ack_blks);
3910 a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
3911 stcb->asoc.seen_a_sack_this_pkt = 1;
3912 if ((stcb->asoc.pr_sctp_cnt == 0) &&
3914 ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) ||
3915 (cum_ack == stcb->asoc.last_acked_seq)) &&
3916 (stcb->asoc.saw_sack_with_frags == 0) &&
3917 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
3920 * We have a SIMPLE sack having no
3921 * prior segments and data on sent
3922 * queue to be acked.. Use the
3923 * faster path sack processing. We
3924 * also allow window update sacks
3925 * with no missing segments to go
3928 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag, &abort_now);
3930 sctp_handle_sack(sack, stcb, *netp, &abort_now);
3933 /* ABORT signal from sack processing */
3939 case SCTP_HEARTBEAT_REQUEST:
3941 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3942 printf("SCTP_HEARTBEAT\n");
3944 #endif /* SCTP_DEBUG */
3945 SCTP_STAT_INCR(sctps_recvheartbeat);
3946 sctp_send_heartbeat_ack(stcb, m, *offset, chk_length,
3949 /* He's alive so give him credit */
3950 stcb->asoc.overall_error_count = 0;
3952 case SCTP_HEARTBEAT_ACK:
3954 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3955 printf("SCTP_HEARTBEAT-ACK\n");
3957 #endif /* SCTP_DEBUG */
3959 /* He's alive so give him credit */
3960 stcb->asoc.overall_error_count = 0;
3961 SCTP_STAT_INCR(sctps_recvheartbeatack);
3962 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
3965 case SCTP_ABORT_ASSOCIATION:
3967 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3968 printf("SCTP_ABORT\n");
3970 #endif /* SCTP_DEBUG */
3971 sctp_handle_abort((struct sctp_abort_chunk *)ch,
3978 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3979 printf("SCTP_SHUTDOWN\n");
3981 #endif /* SCTP_DEBUG */
3985 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
3986 stcb, *netp, &abort_flag);
3993 case SCTP_SHUTDOWN_ACK:
3995 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3996 printf("SCTP_SHUTDOWN-ACK\n");
3998 #endif /* SCTP_DEBUG */
3999 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
4003 case SCTP_OPERATION_ERROR:
4005 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4006 printf("SCTP_OP-ERR\n");
4008 #endif /* SCTP_DEBUG */
4009 if (sctp_handle_error(ch, stcb, *netp) < 0) {
4014 case SCTP_COOKIE_ECHO:
4016 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4017 printf("SCTP_COOKIE-ECHO stcb is %p\n", stcb);
4019 #endif /* SCTP_DEBUG */
4020 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4023 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) &&
4025 /* We are not interested anymore */
4031 * First are we accepting? We do this again here
4032 * since it is possible that a previous endpoint WAS
4033 * listening responded to a INIT-ACK and then
4034 * closed. We opened and bound.. and are now no
4037 if (inp->sctp_socket->so_qlimit == 0) {
4038 if ((stcb) && (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4040 * special case, is this a retran'd
4041 * COOKIE-ECHO or a restarting assoc
4042 * that is a peeled off or
4043 * one-to-one style socket.
4045 goto process_cookie_anyway;
4047 sctp_abort_association(inp, stcb, m, iphlen, sh,
4051 } else if (inp->sctp_socket->so_qlimit) {
4052 /* we are accepting so check limits like TCP */
4053 if (inp->sctp_socket->so_qlen >
4054 inp->sctp_socket->so_qlimit) {
4057 struct sctp_paramhdr *phdr;
4059 if (sctp_abort_if_one_2_one_hits_limit) {
4061 oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
4062 0, M_DONTWAIT, 1, MT_DATA);
4064 SCTP_BUF_LEN(oper) =
4065 sizeof(struct sctp_paramhdr);
4067 struct sctp_paramhdr *);
4069 htons(SCTP_CAUSE_OUT_OF_RESC);
4070 phdr->param_length =
4071 htons(sizeof(struct sctp_paramhdr));
4073 sctp_abort_association(inp, stcb, m,
4080 process_cookie_anyway:
4082 struct mbuf *ret_buf;
4083 struct sctp_inpcb *linp;
4091 SCTP_ASOC_CREATE_LOCK(linp);
4093 sctp_handle_cookie_echo(m, iphlen,
4095 (struct sctp_cookie_echo_chunk *)ch,
4102 SCTP_ASOC_CREATE_UNLOCK(linp);
4103 if (ret_buf == NULL) {
4105 SCTP_TCB_UNLOCK(locked_tcb);
4108 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4109 printf("GAK, null buffer\n");
4111 #endif /* SCTP_DEBUG */
4116 /* if AUTH skipped, see if it verified... */
4121 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
4123 * Restart the timer if we have
4126 struct sctp_tmit_chunk *chk;
4128 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
4130 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4131 stcb->sctp_ep, stcb,
4137 case SCTP_COOKIE_ACK:
4139 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4140 printf("SCTP_COOKIE-ACK\n");
4142 #endif /* SCTP_DEBUG */
4144 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4145 /* We are not interested anymore */
4146 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4149 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4154 /* He's alive so give him credit */
4155 stcb->asoc.overall_error_count = 0;
4156 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
4160 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4161 printf("SCTP_ECN-ECHO\n");
4163 #endif /* SCTP_DEBUG */
4164 /* He's alive so give him credit */
4165 stcb->asoc.overall_error_count = 0;
4166 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
4171 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4172 printf("SCTP_ECN-CWR\n");
4174 #endif /* SCTP_DEBUG */
4175 /* He's alive so give him credit */
4176 stcb->asoc.overall_error_count = 0;
4178 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb);
4180 case SCTP_SHUTDOWN_COMPLETE:
4182 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4183 printf("SCTP_SHUTDOWN-COMPLETE\n");
4185 #endif /* SCTP_DEBUG */
4186 /* must be first and only chunk */
4187 if ((num_chunks > 1) ||
4188 (length - *offset > SCTP_SIZE32(chk_length))) {
4191 SCTP_TCB_UNLOCK(locked_tcb);
4195 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
4202 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4203 printf("SCTP_ASCONF\n");
4205 #endif /* SCTP_DEBUG */
4206 /* He's alive so give him credit */
4207 stcb->asoc.overall_error_count = 0;
4209 sctp_handle_asconf(m, *offset,
4210 (struct sctp_asconf_chunk *)ch, stcb);
4212 case SCTP_ASCONF_ACK:
4214 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4215 printf("SCTP_ASCONF-ACK\n");
4217 #endif /* SCTP_DEBUG */
4218 /* He's alive so give him credit */
4219 stcb->asoc.overall_error_count = 0;
4221 sctp_handle_asconf_ack(m, *offset,
4222 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp);
4224 case SCTP_FORWARD_CUM_TSN:
4226 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4227 printf("SCTP_FWD-TSN\n");
4229 #endif /* SCTP_DEBUG */
4230 /* He's alive so give him credit */
4234 stcb->asoc.overall_error_count = 0;
4236 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4237 /* We are not interested anymore */
4238 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
4242 sctp_handle_forward_tsn(stcb,
4243 (struct sctp_forward_tsn_chunk *)ch, &abort_flag);
4248 stcb->asoc.overall_error_count = 0;
4253 case SCTP_STREAM_RESET:
4255 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4256 printf("SCTP_STREAM_RESET\n");
4258 #endif /* SCTP_DEBUG */
4259 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4260 chk_length, chunk_buf);
4261 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4262 /* We are not interested anymore */
4263 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4267 if (stcb->asoc.peer_supports_strreset == 0) {
4269 * hmm, peer should have announced this, but
4270 * we will turn it on since he is sending us
4273 stcb->asoc.peer_supports_strreset = 1;
4275 if (sctp_handle_stream_reset(stcb, (struct sctp_stream_reset_out_req *)ch)) {
4276 /* stop processing */
4281 case SCTP_PACKET_DROPPED:
4283 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4284 printf("SCTP_PACKET_DROPPED\n");
4286 #endif /* SCTP_DEBUG */
4287 /* re-get it all please */
4288 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4289 chk_length, chunk_buf);
4291 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
4296 case SCTP_AUTHENTICATION:
4298 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4299 printf("SCTP_AUTHENTICATION\n");
4301 #endif /* SCTP_DEBUG */
4302 if (sctp_auth_disable)
4306 /* save the first AUTH for later processing */
4307 if (auth_skipped == 0) {
4308 auth_offset = *offset;
4309 auth_len = chk_length;
4312 /* skip this chunk (temporarily) */
4315 if (got_auth == 1) {
4316 /* skip this chunk... it's already auth'd */
4319 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4320 chk_length, chunk_buf);
4322 if (sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
4324 /* auth HMAC failed so dump the packet */
4328 /* remaining chunks are HMAC checked */
4329 stcb->asoc.authenticated = 1;
4335 /* it's an unknown chunk! */
4336 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
4338 struct sctp_paramhdr *phd;
4340 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
4341 0, M_DONTWAIT, 1, MT_DATA);
4343 phd = mtod(mm, struct sctp_paramhdr *);
4345 * We cheat and use param type since
4346 * we did not bother to define a
4347 * error cause struct. They are the
4348 * same basic format with different
4351 phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK);
4352 phd->param_length = htons(chk_length + sizeof(*phd));
4353 SCTP_BUF_LEN(mm) = sizeof(*phd);
4354 SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length),
4356 if (SCTP_BUF_NEXT(mm)) {
4357 sctp_queue_op_err(stcb, mm);
4363 if ((ch->chunk_type & 0x80) == 0) {
4364 /* discard this packet */
4367 } /* else skip this bad chunk and continue... */
4369 } /* switch (ch->chunk_type) */
4373 /* get the next chunk */
4374 *offset += SCTP_SIZE32(chk_length);
4375 if (*offset >= length) {
4376 /* no more data left in the mbuf chain */
4379 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4380 sizeof(struct sctp_chunkhdr), chunk_buf);
4383 SCTP_TCB_UNLOCK(locked_tcb);
4393 * Process the ECN bits we have something set so we must look to see if it is
4394 * ECN(0) or ECN(1) or CE
4396 static __inline void
4397 sctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net,
4400 if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
4402 } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) {
4404 * we only add to the nonce sum for ECT1, ECT0 does not
4405 * change the NS bit (that we have yet to find a way to send
4409 /* ECN Nonce stuff */
4410 stcb->asoc.receiver_nonce_sum++;
4411 stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM;
4414 * Drag up the last_echo point if cumack is larger since we
4415 * don't want the point falling way behind by more than
4416 * 2^^31 and then having it be incorrect.
4418 if (compare_with_wrap(stcb->asoc.cumulative_tsn,
4419 stcb->asoc.last_echo_tsn, MAX_TSN)) {
4420 stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
4422 } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) {
4424 * Drag up the last_echo point if cumack is larger since we
4425 * don't want the point falling way behind by more than
4426 * 2^^31 and then having it be incorrect.
4428 if (compare_with_wrap(stcb->asoc.cumulative_tsn,
4429 stcb->asoc.last_echo_tsn, MAX_TSN)) {
4430 stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
4435 static __inline void
4436 sctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net,
4437 uint32_t high_tsn, uint8_t ecn_bits)
4439 if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
4441 * we possibly must notify the sender that a congestion
4442 * window reduction is in order. We do this by adding a ECNE
4443 * chunk to the output chunk queue. The incoming CWR will
4444 * remove this chunk.
4446 if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn,
4448 /* Yep, we need to add a ECNE */
4449 sctp_send_ecn_echo(stcb, net, high_tsn);
4450 stcb->asoc.last_echo_tsn = high_tsn;
4456 * common input chunk processing (v4 and v6)
4459 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset,
4460 int length, struct sctphdr *sh, struct sctp_chunkhdr *ch,
4461 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
4465 * Control chunk processing
4468 int fwd_tsn_seen = 0, data_processed = 0;
4469 struct mbuf *m = *mm;
4473 SCTP_STAT_INCR(sctps_recvdatagrams);
4474 #ifdef SCTP_AUDITING_ENABLED
4475 sctp_audit_log(0xE0, 1);
4476 sctp_auditing(0, inp, stcb, net);
4480 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4481 printf("Ok, Common input processing called, m:%p iphlen:%d offset:%d\n",
4484 #endif /* SCTP_DEBUG */
4487 /* always clear this before beginning a packet */
4488 stcb->asoc.authenticated = 0;
4489 stcb->asoc.seen_a_sack_this_pkt = 0;
4491 if (IS_SCTP_CONTROL(ch)) {
4492 /* process the control portion of the SCTP packet */
4493 stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch,
4494 inp, stcb, &net, &fwd_tsn_seen);
4497 * This covers us if the cookie-echo was there and
4498 * it changes our INP.
4500 inp = stcb->sctp_ep;
4504 * no control chunks, so pre-process DATA chunks (these
4505 * checks are taken care of by control processing)
4509 * if DATA only packet, and auth is required, then punt...
4510 * can't have authenticated without any AUTH (control)
4513 if ((stcb != NULL) && !sctp_auth_disable &&
4514 sctp_auth_is_required_chunk(SCTP_DATA,
4515 stcb->asoc.local_auth_chunks)) {
4516 /* "silently" ignore */
4517 SCTP_STAT_INCR(sctps_recvauthmissing);
4518 SCTP_TCB_UNLOCK(stcb);
4522 /* out of the blue DATA chunk */
4523 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL);
4526 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
4527 /* v_tag mismatch! */
4528 SCTP_STAT_INCR(sctps_badvtag);
4529 SCTP_TCB_UNLOCK(stcb);
4536 * no valid TCB for this packet, or we found it's a bad
4537 * packet while processing control, or we're done with this
4538 * packet (done or skip rest of data), so we drop it...
4543 * DATA chunk processing
4545 /* plow through the data chunks while length > offset */
4548 * Rest should be DATA only. Check authentication state if AUTH for
4551 if ((length > offset) && (stcb != NULL) && !sctp_auth_disable &&
4552 sctp_auth_is_required_chunk(SCTP_DATA,
4553 stcb->asoc.local_auth_chunks) &&
4554 !stcb->asoc.authenticated) {
4555 /* "silently" ignore */
4556 SCTP_STAT_INCR(sctps_recvauthmissing);
4558 if (sctp_debug_on & SCTP_DEBUG_AUTH1)
4559 printf("Data chunk requires AUTH, skipped\n");
4563 if (length > offset) {
4567 * First check to make sure our state is correct. We would
4568 * not get here unless we really did have a tag, so we don't
4569 * abort if this happens, just dump the chunk silently.
4571 switch (SCTP_GET_STATE(&stcb->asoc)) {
4572 case SCTP_STATE_COOKIE_ECHOED:
4574 * we consider data with valid tags in this state
4575 * shows us the cookie-ack was lost. Imply it was
4578 stcb->asoc.overall_error_count = 0;
4579 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
4581 case SCTP_STATE_COOKIE_WAIT:
4583 * We consider OOTB any data sent during asoc setup.
4585 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL);
4586 SCTP_TCB_UNLOCK(stcb);
4589 case SCTP_STATE_EMPTY: /* should not happen */
4590 case SCTP_STATE_INUSE: /* should not happen */
4591 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
4592 case SCTP_STATE_SHUTDOWN_ACK_SENT:
4594 SCTP_TCB_UNLOCK(stcb);
4597 case SCTP_STATE_OPEN:
4598 case SCTP_STATE_SHUTDOWN_SENT:
4601 /* take care of ECN, part 1. */
4602 if (stcb->asoc.ecn_allowed &&
4603 (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) {
4604 sctp_process_ecn_marked_a(stcb, net, ecn_bits);
4606 /* plow through the data chunks while length > offset */
4607 retval = sctp_process_data(mm, iphlen, &offset, length, sh,
4608 inp, stcb, net, &high_tsn);
4611 * The association aborted, NO UNLOCK needed since
4612 * the association is destroyed.
4618 /* take care of ecn part 2. */
4619 if (stcb->asoc.ecn_allowed &&
4620 (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) {
4621 sctp_process_ecn_marked_b(stcb, net, high_tsn,
4626 * Anything important needs to have been m_copy'ed in
4630 if ((data_processed == 0) && (fwd_tsn_seen)) {
4633 if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
4634 stcb->asoc.cumulative_tsn, MAX_TSN)) {
4635 /* there was a gap before this data was processed */
4638 sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
4640 /* Again, we aborted so NO UNLOCK needed */
4644 /* trigger send of any chunks in queue... */
4646 #ifdef SCTP_AUDITING_ENABLED
4647 sctp_audit_log(0xE0, 2);
4648 sctp_auditing(1, inp, stcb, net);
4651 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4652 printf("Check for chunk output prw:%d tqe:%d tf=%d\n",
4653 stcb->asoc.peers_rwnd,
4654 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
4655 stcb->asoc.total_flight);
4658 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
4660 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue) ||
4662 (stcb->asoc.peers_rwnd > 0 ||
4663 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
4665 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4666 printf("Calling chunk OUTPUT\n");
4669 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC);
4671 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4672 printf("chunk OUTPUT returns\n");
4676 #ifdef SCTP_AUDITING_ENABLED
4677 sctp_audit_log(0xE0, 3);
4678 sctp_auditing(2, inp, stcb, net);
4680 SCTP_TCB_UNLOCK(stcb);
4687 sctp_input(i_pak, off)
4692 #ifdef SCTP_MBUF_LOGGING
4702 struct sctp_inpcb *inp = NULL;
4704 uint32_t check, calc_check;
4705 struct sctp_nets *net;
4706 struct sctp_tcb *stcb = NULL;
4707 struct sctp_chunkhdr *ch;
4708 int refcount_up = 0;
4709 int length, mlen, offset;
4711 vrf_id = SCTP_DEFAULT_VRFID;
4712 mlen = SCTP_HEADER_LEN(i_pak);
4714 m = SCTP_HEADER_TO_CHAIN(i_pak);
4716 SCTP_STAT_INCR(sctps_recvpackets);
4717 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
4719 #ifdef SCTP_MBUF_LOGGING
4720 /* Log in any input mbufs */
4723 if (SCTP_BUF_IS_EXTENDED(mat)) {
4724 sctp_log_mb(mat, SCTP_MBUF_INPUT);
4726 mat = SCTP_BUF_NEXT(mat);
4731 * Get IP, SCTP, and first chunk header together in first mbuf.
4733 ip = mtod(m, struct ip *);
4734 offset = iphlen + sizeof(*sh) + sizeof(*ch);
4735 if (SCTP_BUF_LEN(m) < offset) {
4736 if ((m = m_pullup(m, offset)) == 0) {
4737 SCTP_STAT_INCR(sctps_hdrops);
4740 ip = mtod(m, struct ip *);
4742 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
4743 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh));
4745 /* SCTP does not allow broadcasts or multicasts */
4746 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
4749 if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) {
4751 * We only look at broadcast if its a front state, All
4752 * others we will not have a tcb for anyway.
4756 /* validate SCTP checksum */
4757 if ((sctp_no_csum_on_loopback == 0) || !SCTP_IS_IT_LOOPBACK(m)) {
4759 * we do NOT validate things from the loopback if the sysctl
4762 check = sh->checksum; /* save incoming checksum */
4763 if ((check == 0) && (sctp_no_csum_on_loopback)) {
4765 * special hook for where we got a local address
4766 * somehow routed across a non IFT_LOOP type
4769 if (ip->ip_src.s_addr == ip->ip_dst.s_addr)
4770 goto sctp_skip_csum_4;
4772 sh->checksum = 0; /* prepare for calc */
4773 calc_check = sctp_calculate_sum(m, &mlen, iphlen);
4774 if (calc_check != check) {
4776 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4777 printf("Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
4778 calc_check, check, m, mlen, iphlen);
4782 stcb = sctp_findassociation_addr(m, iphlen,
4783 offset - sizeof(*ch),
4784 sh, ch, &inp, &net, vrf_id);
4785 if ((inp) && (stcb)) {
4786 sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
4787 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR);
4788 } else if ((inp != NULL) && (stcb == NULL)) {
4791 SCTP_STAT_INCR(sctps_badsum);
4792 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
4795 sh->checksum = calc_check;
4798 /* destination port of 0 is illegal, based on RFC2960. */
4799 if (sh->dest_port == 0) {
4800 SCTP_STAT_INCR(sctps_hdrops);
4803 /* validate mbuf chain length with IP payload length */
4804 if (mlen < (ip->ip_len - iphlen)) {
4805 SCTP_STAT_INCR(sctps_hdrops);
4809 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
4810 * IP/SCTP/first chunk header...
4812 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
4813 sh, ch, &inp, &net, vrf_id);
4814 /* inp's ref-count increased && stcb locked */
4816 struct sctp_init_chunk *init_chk, chunk_buf;
4818 SCTP_STAT_INCR(sctps_noport);
4821 * we use the bandwidth limiting to protect against sending
4822 * too many ABORTS all at once. In this case these count the
4823 * same as an ICMP message.
4825 if (badport_bandlim(0) < 0)
4827 #endif /* ICMP_BANDLIM */
4829 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4830 printf("Sending a ABORT from packet entry!\n");
4833 if (ch->chunk_type == SCTP_INITIATION) {
4835 * we do a trick here to get the INIT tag, dig in
4836 * and get the tag from the INIT and put it in the
4839 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4840 iphlen + sizeof(*sh), sizeof(*init_chk),
4841 (uint8_t *) & chunk_buf);
4842 if (init_chk != NULL)
4843 sh->v_tag = init_chk->init.initiate_tag;
4845 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4846 sctp_send_shutdown_complete2(m, iphlen, sh);
4849 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
4852 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
4853 sctp_send_abort(m, iphlen, sh, 0, NULL);
4855 } else if (stcb == NULL) {
4860 * I very much doubt any of the IPSEC stuff will work but I have no
4861 * idea, so I will leave it in place.
4864 if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) {
4865 ipsecstat.in_polvio++;
4866 SCTP_STAT_INCR(sctps_hdrops);
4872 * common chunk processing
4874 length = ip->ip_len + iphlen;
4875 offset -= sizeof(struct sctp_chunkhdr);
4877 ecn_bits = ip->ip_tos;
4879 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
4880 inp, stcb, net, ecn_bits);
4881 /* inp's ref-count reduced && stcb unlocked */
4885 if ((inp) && (refcount_up)) {
4886 /* reduce ref-count */
4887 SCTP_INP_WLOCK(inp);
4888 SCTP_INP_DECR_REF(inp);
4889 SCTP_INP_WUNLOCK(inp);
4894 SCTP_TCB_UNLOCK(stcb);
4896 if ((inp) && (refcount_up)) {
4897 /* reduce ref-count */
4898 SCTP_INP_WLOCK(inp);
4899 SCTP_INP_DECR_REF(inp);
4900 SCTP_INP_WUNLOCK(inp);