2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 /* $KAME: sctp_input.c,v 1.27 2005/03/06 16:04:17 itojun Exp $ */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <netinet/sctp_os.h>
39 #include <netinet/sctp_var.h>
40 #include <netinet/sctp_sysctl.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctp_header.h>
43 #include <netinet/sctputil.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_input.h>
46 #include <netinet/sctp_auth.h>
47 #include <netinet/sctp_indata.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctp_bsd_addr.h>
50 #include <netinet/sctp_timer.h>
51 #include <netinet/sctp_crc32.h>
52 #include <netinet/udp.h>
58 sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
60 struct sctp_nets *net;
63 * This now not only stops all cookie timers it also stops any INIT
64 * timers as well. This will make sure that the timers are stopped
65 * in all collision cases.
67 SCTP_TCB_LOCK_ASSERT(stcb);
68 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
69 if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
70 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
73 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
74 } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
75 sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
78 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
85 sctp_handle_init(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
86 struct sctp_init_chunk *cp, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
87 int *abort_no_unlock, uint32_t vrf_id, uint16_t port)
89 struct sctp_init *init;
93 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
97 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
103 /* First are we accepting? */
104 if ((inp->sctp_socket->so_qlimit == 0) && (stcb == NULL)) {
105 SCTPDBG(SCTP_DEBUG_INPUT2,
106 "sctp_handle_init: Abort, so_qlimit:%d\n",
107 inp->sctp_socket->so_qlimit);
109 * FIX ME ?? What about TCP model and we have a
110 * match/restart case? Actually no fix is needed. the lookup
111 * will always find the existing assoc so stcb would not be
112 * NULL. It may be questionable to do this since we COULD
113 * just send back the INIT-ACK and hope that the app did
114 * accept()'s by the time the COOKIE was sent. But there is
115 * a price to pay for COOKIE generation and I don't want to
116 * pay it on the chance that the app will actually do some
117 * accepts(). The App just looses and should NOT be in this
120 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
123 *abort_no_unlock = 1;
126 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
128 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
129 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
132 *abort_no_unlock = 1;
135 /* validate parameters */
136 if (init->initiate_tag == 0) {
137 /* protocol error... send abort */
138 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
139 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
142 *abort_no_unlock = 1;
145 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
146 /* invalid parameter... send abort */
147 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
148 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
151 *abort_no_unlock = 1;
154 if (init->num_inbound_streams == 0) {
155 /* protocol error... send abort */
156 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
157 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
160 *abort_no_unlock = 1;
163 if (init->num_outbound_streams == 0) {
164 /* protocol error... send abort */
165 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
166 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
169 *abort_no_unlock = 1;
172 init_limit = offset + ntohs(cp->ch.chunk_length);
173 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
175 /* auth parameter(s) error... send abort */
176 sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id, port);
178 *abort_no_unlock = 1;
181 /* send an INIT-ACK w/cookie */
182 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
183 sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id, port,
184 ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED));
187 SCTP_INP_RUNLOCK(inp);
192 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
196 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
197 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
204 struct sctp_stream_queue_pending *sp;
205 struct sctp_association *asoc;
208 * This function returns the number of streams that have true unsent
209 * data on them. Note that as it looks through it will clean up any
210 * places that have old data that has been sent but left at top of
214 SCTP_TCB_SEND_LOCK(stcb);
215 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
216 /* Check to see if some data queued */
217 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
218 /* sa_ignore FREED_MEMORY */
219 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
223 if ((sp->msg_is_complete) &&
225 (sp->sender_all_done)) {
227 * We are doing differed cleanup. Last time
228 * through when we took all the data the
229 * sender_all_done was not set.
231 if (sp->put_last_out == 0) {
232 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
233 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
239 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
240 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
242 sctp_free_remote_addr(sp->net);
246 sctp_m_freem(sp->data);
249 sctp_free_a_strmoq(stcb, sp, so_locked);
256 SCTP_TCB_SEND_UNLOCK(stcb);
257 return (unsent_data);
261 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
263 struct sctp_init *init;
264 struct sctp_association *asoc;
265 struct sctp_nets *lnet;
270 /* save off parameters */
271 asoc->peer_vtag = ntohl(init->initiate_tag);
272 asoc->peers_rwnd = ntohl(init->a_rwnd);
274 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
276 if (!TAILQ_EMPTY(&asoc->nets)) {
277 /* update any ssthresh's that may have a default */
278 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
279 lnet->ssthresh = asoc->peers_rwnd;
280 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
281 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
285 SCTP_TCB_SEND_LOCK(stcb);
286 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
288 struct sctp_stream_out *outs;
289 struct sctp_stream_queue_pending *sp, *nsp;
290 struct sctp_tmit_chunk *chk, *nchk;
292 /* abandon the upper streams */
293 newcnt = ntohs(init->num_inbound_streams);
294 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
295 if (chk->rec.data.stream_number >= newcnt) {
296 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
297 asoc->send_queue_cnt--;
298 if (chk->data != NULL) {
299 sctp_free_bufspace(stcb, asoc, chk, 1);
300 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
301 SCTP_NOTIFY_DATAGRAM_UNSENT, chk, SCTP_SO_NOT_LOCKED);
303 sctp_m_freem(chk->data);
307 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
308 /* sa_ignore FREED_MEMORY */
312 for (i = newcnt; i < asoc->pre_open_streams; i++) {
313 outs = &asoc->strmout[i];
314 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
315 TAILQ_REMOVE(&outs->outqueue, sp, next);
316 asoc->stream_queue_cnt--;
317 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
318 stcb, SCTP_NOTIFY_DATAGRAM_UNSENT,
319 sp, SCTP_SO_NOT_LOCKED);
321 sctp_m_freem(sp->data);
325 sctp_free_remote_addr(sp->net);
329 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
330 /* sa_ignore FREED_MEMORY */
334 /* cut back the count */
335 asoc->pre_open_streams = newcnt;
337 SCTP_TCB_SEND_UNLOCK(stcb);
338 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
340 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
341 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
342 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
343 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
345 /* This is the next one we expect */
346 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
348 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
349 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
351 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
352 /* open the requested streams */
354 if (asoc->strmin != NULL) {
355 /* Free the old ones */
356 struct sctp_queued_to_read *ctl, *nctl;
358 for (i = 0; i < asoc->streamincnt; i++) {
359 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) {
360 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
361 sctp_free_remote_addr(ctl->whoFrom);
363 sctp_m_freem(ctl->data);
365 sctp_free_a_readq(stcb, ctl);
368 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
370 asoc->streamincnt = ntohs(init->num_outbound_streams);
371 if (asoc->streamincnt > MAX_SCTP_STREAMS) {
372 asoc->streamincnt = MAX_SCTP_STREAMS;
374 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
375 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
376 if (asoc->strmin == NULL) {
377 /* we didn't get memory for the streams! */
378 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
381 for (i = 0; i < asoc->streamincnt; i++) {
382 asoc->strmin[i].stream_no = i;
383 asoc->strmin[i].last_sequence_delivered = 0xffff;
385 * U-stream ranges will be set when the cookie is unpacked.
386 * Or for the INIT sender they are un set (if pr-sctp not
387 * supported) when the INIT-ACK arrives.
389 TAILQ_INIT(&asoc->strmin[i].inqueue);
390 asoc->strmin[i].delivery_started = 0;
393 * load_address_from_init will put the addresses into the
394 * association when the COOKIE is processed or the INIT-ACK is
395 * processed. Both types of COOKIE's existing and new call this
396 * routine. It will remove addresses that are no longer in the
397 * association (for the restarting case where addresses are
398 * removed). Up front when the INIT arrives we will discard it if it
399 * is a restart and new addresses have been added.
401 /* sa_ignore MEMLEAK */
406 * INIT-ACK message processing/consumption returns value < 0 on error
409 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
410 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
411 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id)
413 struct sctp_association *asoc;
415 int retval, abort_flag;
416 uint32_t initack_limit;
417 int nat_friendly = 0;
419 /* First verify that we have no illegal param's */
423 op_err = sctp_arethere_unrecognized_parameters(m,
424 (offset + sizeof(struct sctp_init_chunk)),
425 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
427 /* Send an abort and notify peer */
428 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_CAUSE_PROTOCOL_VIOLATION, op_err, SCTP_SO_NOT_LOCKED);
429 *abort_no_unlock = 1;
433 asoc->peer_supports_nat = (uint8_t) nat_friendly;
434 /* process the peer's parameters in the INIT-ACK */
435 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
439 initack_limit = offset + ntohs(cp->ch.chunk_length);
440 /* load all addresses */
441 if ((retval = sctp_load_addresses_from_init(stcb, m,
442 (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh,
444 /* Huh, we should abort */
445 SCTPDBG(SCTP_DEBUG_INPUT1,
446 "Load addresses from INIT causes an abort %d\n",
448 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
450 *abort_no_unlock = 1;
453 /* if the peer doesn't support asconf, flush the asconf queue */
454 if (asoc->peer_supports_asconf == 0) {
455 struct sctp_asconf_addr *param, *nparam;
457 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
458 TAILQ_REMOVE(&asoc->asconf_queue, param, next);
459 SCTP_FREE(param, SCTP_M_ASC_ADDR);
462 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
463 stcb->asoc.local_hmacs);
465 sctp_queue_op_err(stcb, op_err);
466 /* queuing will steal away the mbuf chain to the out queue */
469 /* extract the cookie and queue it to "echo" it back... */
470 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
471 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
472 stcb->asoc.overall_error_count,
474 SCTP_FROM_SCTP_INPUT,
477 stcb->asoc.overall_error_count = 0;
478 net->error_count = 0;
481 * Cancel the INIT timer, We do this first before queueing the
482 * cookie. We always cancel at the primary to assue that we are
483 * canceling the timer started by the INIT which always goes to the
486 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
487 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
489 /* calculate the RTO */
490 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy,
491 SCTP_RTT_FROM_NON_DATA);
493 retval = sctp_send_cookie_echo(m, offset, stcb, net);
496 * No cookie, we probably should send a op error. But in any
497 * case if there is no cookie in the INIT-ACK, we can
498 * abandon the peer, its broke.
501 /* We abort with an error of missing mandatory param */
503 sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM);
506 * Expand beyond to include the mandatory
509 struct sctp_inv_mandatory_param *mp;
511 SCTP_BUF_LEN(op_err) =
512 sizeof(struct sctp_inv_mandatory_param);
514 struct sctp_inv_mandatory_param *);
515 /* Subtract the reserved param */
517 htons(sizeof(struct sctp_inv_mandatory_param) - 2);
518 mp->num_param = htonl(1);
519 mp->param = htons(SCTP_STATE_COOKIE);
522 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
523 sh, op_err, vrf_id, net->port);
524 *abort_no_unlock = 1;
532 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
533 struct sctp_tcb *stcb, struct sctp_nets *net)
535 struct sockaddr_storage store;
536 struct sctp_nets *r_net, *f_net;
539 uint16_t old_error_counter;
542 struct sockaddr_in *sin;
546 struct sockaddr_in6 *sin6;
550 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
554 memset(&store, 0, sizeof(store));
555 switch (cp->heartbeat.hb_info.addr_family) {
558 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
559 sin = (struct sockaddr_in *)&store;
560 sin->sin_family = cp->heartbeat.hb_info.addr_family;
561 sin->sin_len = cp->heartbeat.hb_info.addr_len;
562 sin->sin_port = stcb->rport;
563 memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address,
564 sizeof(sin->sin_addr));
572 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
573 sin6 = (struct sockaddr_in6 *)&store;
574 sin6->sin6_family = cp->heartbeat.hb_info.addr_family;
575 sin6->sin6_len = cp->heartbeat.hb_info.addr_len;
576 sin6->sin6_port = stcb->rport;
577 memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address,
578 sizeof(sin6->sin6_addr));
587 r_net = sctp_findnet(stcb, (struct sockaddr *)&store);
589 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
592 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
593 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
594 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
596 * If the its a HB and it's random value is correct when can
597 * confirm the destination.
599 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
600 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
601 stcb->asoc.primary_destination = r_net;
602 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
603 f_net = TAILQ_FIRST(&stcb->asoc.nets);
604 if (f_net != r_net) {
606 * first one on the list is NOT the primary
607 * sctp_cmpaddr() is much more efficent if
608 * the primary is the first on the list,
611 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
612 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
616 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
617 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
618 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
619 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
621 old_error_counter = r_net->error_count;
622 r_net->error_count = 0;
623 r_net->hb_responded = 1;
624 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
625 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
626 /* Now lets do a RTO with this */
627 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy,
628 SCTP_RTT_FROM_NON_DATA);
629 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
630 r_net->dest_state |= SCTP_ADDR_REACHABLE;
631 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
632 SCTP_HEARTBEAT_SUCCESS, (void *)r_net, SCTP_SO_NOT_LOCKED);
634 if (r_net->dest_state & SCTP_ADDR_PF) {
635 r_net->dest_state &= ~SCTP_ADDR_PF;
636 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
638 if (old_error_counter > 0) {
639 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
640 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
642 if (r_net == stcb->asoc.primary_destination) {
643 if (stcb->asoc.alternate) {
644 /* release the alternate, primary is good */
645 sctp_free_remote_addr(stcb->asoc.alternate);
646 stcb->asoc.alternate = NULL;
649 /* Mobility adaptation */
651 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
652 SCTP_MOBILITY_BASE) ||
653 sctp_is_mobility_feature_on(stcb->sctp_ep,
654 SCTP_MOBILITY_FASTHANDOFF)) &&
655 sctp_is_mobility_feature_on(stcb->sctp_ep,
656 SCTP_MOBILITY_PRIM_DELETED)) {
658 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7);
659 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
660 SCTP_MOBILITY_FASTHANDOFF)) {
661 sctp_assoc_immediate_retrans(stcb,
662 stcb->asoc.primary_destination);
664 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
665 SCTP_MOBILITY_BASE)) {
666 sctp_move_chunks_from_net(stcb,
667 stcb->asoc.deleted_primary);
669 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
670 stcb->asoc.deleted_primary);
676 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
679 * return 0 means we want you to proceed with the abort non-zero
680 * means no abort processing
682 struct sctpasochead *head;
684 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
685 /* generate a new vtag and send init */
686 LIST_REMOVE(stcb, sctp_asocs);
687 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
688 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
690 * put it in the bucket in the vtag hash of assoc's for the
693 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
694 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
697 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
699 * treat like a case where the cookie expired i.e.: - dump
700 * current cookie. - generate a new vtag. - resend init.
702 /* generate a new vtag and send init */
703 LIST_REMOVE(stcb, sctp_asocs);
704 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
705 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
706 sctp_stop_all_cookie_timers(stcb);
707 sctp_toss_old_cookies(stcb, &stcb->asoc);
708 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
709 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
711 * put it in the bucket in the vtag hash of assoc's for the
714 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
715 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
722 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
723 struct sctp_nets *net)
726 * return 0 means we want you to proceed with the abort non-zero
727 * means no abort processing
729 if (stcb->asoc.peer_supports_auth == 0) {
730 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
733 sctp_asconf_send_nat_state_update(stcb, net);
739 sctp_handle_abort(struct sctp_abort_chunk *cp,
740 struct sctp_tcb *stcb, struct sctp_nets *net)
742 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
748 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
752 len = ntohs(cp->ch.chunk_length);
753 if (len > sizeof(struct sctp_chunkhdr)) {
755 * Need to check the cause codes for our two magic nat
756 * aborts which don't kill the assoc necessarily.
758 struct sctp_abort_chunk *cpnext;
759 struct sctp_missing_nat_state *natc;
764 natc = (struct sctp_missing_nat_state *)cpnext;
765 cause = ntohs(natc->cause);
766 if (cause == SCTP_CAUSE_NAT_COLLIDING_STATE) {
767 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
769 if (sctp_handle_nat_colliding_state(stcb)) {
772 } else if (cause == SCTP_CAUSE_NAT_MISSING_STATE) {
773 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
775 if (sctp_handle_nat_missing_state(stcb, net)) {
780 /* stop any receive timers */
781 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
782 /* notify user of the abort and clean up... */
783 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED);
785 #if defined(SCTP_PANIC_ON_ABORT)
786 printf("stcb:%p state:%d rport:%d net:%p\n",
787 stcb, stcb->asoc.state, stcb->rport, net);
788 if (!(stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
789 panic("Received an ABORT");
791 printf("No panic its in state %x closed\n", stcb->asoc.state);
794 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
795 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
796 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
797 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
799 #ifdef SCTP_ASOCLOG_OF_TSNS
800 sctp_print_out_track_log(stcb);
802 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
803 so = SCTP_INP_SO(stcb->sctp_ep);
804 atomic_add_int(&stcb->asoc.refcnt, 1);
805 SCTP_TCB_UNLOCK(stcb);
806 SCTP_SOCKET_LOCK(so, 1);
808 atomic_subtract_int(&stcb->asoc.refcnt, 1);
810 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
811 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
812 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
813 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
814 SCTP_SOCKET_UNLOCK(so, 1);
816 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
820 sctp_start_net_timers(struct sctp_tcb *stcb)
822 uint32_t cnt_hb_sent;
823 struct sctp_nets *net;
826 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
828 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
829 * If the dest in unconfirmed send a hb as well if under
830 * max_hb_burst have been sent.
832 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
833 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
834 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
835 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
836 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
841 sctp_chunk_output(stcb->sctp_ep, stcb,
842 SCTP_OUTPUT_FROM_COOKIE_ACK,
849 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
850 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
852 struct sctp_association *asoc;
853 int some_on_streamwheel;
855 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
860 SCTPDBG(SCTP_DEBUG_INPUT2,
861 "sctp_handle_shutdown: handling SHUTDOWN\n");
865 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
866 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
869 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
870 /* Shutdown NOT the expected size */
873 sctp_update_acked(stcb, cp, abort_flag);
878 if (asoc->control_pdapi) {
880 * With a normal shutdown we assume the end of last record.
882 SCTP_INP_READ_LOCK(stcb->sctp_ep);
883 asoc->control_pdapi->end_added = 1;
884 asoc->control_pdapi->pdapi_aborted = 1;
885 asoc->control_pdapi = NULL;
886 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
887 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
888 so = SCTP_INP_SO(stcb->sctp_ep);
889 atomic_add_int(&stcb->asoc.refcnt, 1);
890 SCTP_TCB_UNLOCK(stcb);
891 SCTP_SOCKET_LOCK(so, 1);
893 atomic_subtract_int(&stcb->asoc.refcnt, 1);
894 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
895 /* assoc was freed while we were unlocked */
896 SCTP_SOCKET_UNLOCK(so, 1);
900 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
901 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
902 SCTP_SOCKET_UNLOCK(so, 1);
905 /* goto SHUTDOWN_RECEIVED state to block new requests */
906 if (stcb->sctp_socket) {
907 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
908 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
909 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
910 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
911 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
913 * notify upper layer that peer has initiated a
916 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
919 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
922 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
924 * stop the shutdown timer, since we WILL move to
927 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
929 /* Now is there unsent data on a stream somewhere? */
930 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
932 if (!TAILQ_EMPTY(&asoc->send_queue) ||
933 !TAILQ_EMPTY(&asoc->sent_queue) ||
934 some_on_streamwheel) {
935 /* By returning we will push more data out */
938 /* no outstanding data to send, so move on... */
939 /* send SHUTDOWN-ACK */
940 sctp_send_shutdown_ack(stcb, net);
941 /* move to SHUTDOWN-ACK-SENT state */
942 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
943 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
944 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
946 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
947 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
948 sctp_stop_timers_for_shutdown(stcb);
949 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
955 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
956 struct sctp_tcb *stcb,
957 struct sctp_nets *net)
959 struct sctp_association *asoc;
961 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
964 so = SCTP_INP_SO(stcb->sctp_ep);
966 SCTPDBG(SCTP_DEBUG_INPUT2,
967 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
972 /* process according to association state */
973 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
974 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
975 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
976 sctp_send_shutdown_complete(stcb, net, 1);
977 SCTP_TCB_UNLOCK(stcb);
980 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
981 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
982 /* unexpected SHUTDOWN-ACK... so ignore... */
983 SCTP_TCB_UNLOCK(stcb);
986 if (asoc->control_pdapi) {
988 * With a normal shutdown we assume the end of last record.
990 SCTP_INP_READ_LOCK(stcb->sctp_ep);
991 asoc->control_pdapi->end_added = 1;
992 asoc->control_pdapi->pdapi_aborted = 1;
993 asoc->control_pdapi = NULL;
994 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
995 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
996 atomic_add_int(&stcb->asoc.refcnt, 1);
997 SCTP_TCB_UNLOCK(stcb);
998 SCTP_SOCKET_LOCK(so, 1);
1000 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1001 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1002 /* assoc was freed while we were unlocked */
1003 SCTP_SOCKET_UNLOCK(so, 1);
1007 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1008 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1009 SCTP_SOCKET_UNLOCK(so, 1);
1012 /* are the queues empty? */
1013 if (!TAILQ_EMPTY(&asoc->send_queue) ||
1014 !TAILQ_EMPTY(&asoc->sent_queue) ||
1015 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
1016 sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED);
1018 /* stop the timer */
1019 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
1020 /* send SHUTDOWN-COMPLETE */
1021 sctp_send_shutdown_complete(stcb, net, 0);
1022 /* notify upper layer protocol */
1023 if (stcb->sctp_socket) {
1024 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1025 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1026 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1027 /* Set the connected flag to disconnected */
1028 stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
1031 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1032 /* free the TCB but first save off the ep */
1033 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1034 atomic_add_int(&stcb->asoc.refcnt, 1);
1035 SCTP_TCB_UNLOCK(stcb);
1036 SCTP_SOCKET_LOCK(so, 1);
1037 SCTP_TCB_LOCK(stcb);
1038 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1040 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1041 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1042 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1043 SCTP_SOCKET_UNLOCK(so, 1);
1048 * Skip past the param header and then we will find the chunk that caused the
1049 * problem. There are two possiblities ASCONF or FWD-TSN other than that and
1050 * our peer must be broken.
1053 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1054 struct sctp_nets *net)
1056 struct sctp_chunkhdr *chk;
1058 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1059 switch (chk->chunk_type) {
1060 case SCTP_ASCONF_ACK:
1062 sctp_asconf_cleanup(stcb, net);
1064 case SCTP_FORWARD_CUM_TSN:
1065 stcb->asoc.peer_supports_prsctp = 0;
1068 SCTPDBG(SCTP_DEBUG_INPUT2,
1069 "Peer does not support chunk type %d(%x)??\n",
1070 chk->chunk_type, (uint32_t) chk->chunk_type);
1076 * Skip past the param header and then we will find the param that caused the
1077 * problem. There are a number of param's in a ASCONF OR the prsctp param
1078 * these will turn of specific features.
1081 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1083 struct sctp_paramhdr *pbad;
1086 switch (ntohs(pbad->param_type)) {
1088 case SCTP_PRSCTP_SUPPORTED:
1089 stcb->asoc.peer_supports_prsctp = 0;
1091 case SCTP_SUPPORTED_CHUNK_EXT:
1093 /* draft-ietf-tsvwg-addip-sctp */
1094 case SCTP_HAS_NAT_SUPPORT:
1095 stcb->asoc.peer_supports_nat = 0;
1097 case SCTP_ADD_IP_ADDRESS:
1098 case SCTP_DEL_IP_ADDRESS:
1099 case SCTP_SET_PRIM_ADDR:
1100 stcb->asoc.peer_supports_asconf = 0;
1102 case SCTP_SUCCESS_REPORT:
1103 case SCTP_ERROR_CAUSE_IND:
1104 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1105 SCTPDBG(SCTP_DEBUG_INPUT2,
1106 "Turning off ASCONF to this strange peer\n");
1107 stcb->asoc.peer_supports_asconf = 0;
1110 SCTPDBG(SCTP_DEBUG_INPUT2,
1111 "Peer does not support param type %d(%x)??\n",
1112 pbad->param_type, (uint32_t) pbad->param_type);
1118 sctp_handle_error(struct sctp_chunkhdr *ch,
1119 struct sctp_tcb *stcb, struct sctp_nets *net)
1122 struct sctp_paramhdr *phdr;
1123 uint16_t error_type;
1125 struct sctp_association *asoc;
1128 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1133 /* parse through all of the errors and process */
1135 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1136 sizeof(struct sctp_chunkhdr));
1137 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1138 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1139 /* Process an Error Cause */
1140 error_type = ntohs(phdr->param_type);
1141 error_len = ntohs(phdr->param_length);
1142 if ((error_len > chklen) || (error_len == 0)) {
1143 /* invalid param length for this param */
1144 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1148 switch (error_type) {
1149 case SCTP_CAUSE_INVALID_STREAM:
1150 case SCTP_CAUSE_MISSING_PARAM:
1151 case SCTP_CAUSE_INVALID_PARAM:
1152 case SCTP_CAUSE_NO_USER_DATA:
1153 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1156 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1157 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1159 if (sctp_handle_nat_colliding_state(stcb)) {
1163 case SCTP_CAUSE_NAT_MISSING_STATE:
1164 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1166 if (sctp_handle_nat_missing_state(stcb, net)) {
1170 case SCTP_CAUSE_STALE_COOKIE:
1172 * We only act if we have echoed a cookie and are
1175 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1178 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1179 /* Save the time doubled */
1180 asoc->cookie_preserve_req = ntohl(*p) << 1;
1181 asoc->stale_cookie_count++;
1182 if (asoc->stale_cookie_count >
1183 asoc->max_init_times) {
1184 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED);
1185 /* now free the asoc */
1186 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1187 so = SCTP_INP_SO(stcb->sctp_ep);
1188 atomic_add_int(&stcb->asoc.refcnt, 1);
1189 SCTP_TCB_UNLOCK(stcb);
1190 SCTP_SOCKET_LOCK(so, 1);
1191 SCTP_TCB_LOCK(stcb);
1192 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1194 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1195 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1196 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1197 SCTP_SOCKET_UNLOCK(so, 1);
1201 /* blast back to INIT state */
1202 sctp_toss_old_cookies(stcb, &stcb->asoc);
1203 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1204 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1205 sctp_stop_all_cookie_timers(stcb);
1206 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1209 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1211 * Nothing we can do here, we don't do hostname
1212 * addresses so if the peer does not like my IPv6
1213 * (or IPv4 for that matter) it does not matter. If
1214 * they don't support that type of address, they can
1215 * NOT possibly get that packet type... i.e. with no
1216 * IPv6 you can't recieve a IPv6 packet. so we can
1217 * safely ignore this one. If we ever added support
1218 * for HOSTNAME Addresses, then we would need to do
1222 case SCTP_CAUSE_UNRECOG_CHUNK:
1223 sctp_process_unrecog_chunk(stcb, phdr, net);
1225 case SCTP_CAUSE_UNRECOG_PARAM:
1226 sctp_process_unrecog_param(stcb, phdr);
1228 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1230 * We ignore this since the timer will drive out a
1231 * new cookie anyway and there timer will drive us
1232 * to send a SHUTDOWN_COMPLETE. We can't send one
1233 * here since we don't have their tag.
1236 case SCTP_CAUSE_DELETING_LAST_ADDR:
1237 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1238 case SCTP_CAUSE_DELETING_SRC_ADDR:
1240 * We should NOT get these here, but in a
1243 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1246 case SCTP_CAUSE_OUT_OF_RESC:
1248 * And what, pray tell do we do with the fact that
1249 * the peer is out of resources? Not really sure we
1250 * could do anything but abort. I suspect this
1251 * should have came WITH an abort instead of in a
1256 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1260 adjust = SCTP_SIZE32(error_len);
1262 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1268 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1269 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1270 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id)
1272 struct sctp_init_ack *init_ack;
1273 struct mbuf *op_err;
1275 SCTPDBG(SCTP_DEBUG_INPUT2,
1276 "sctp_handle_init_ack: handling INIT-ACK\n");
1279 SCTPDBG(SCTP_DEBUG_INPUT2,
1280 "sctp_handle_init_ack: TCB is null\n");
1283 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1284 /* Invalid length */
1285 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1286 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1287 op_err, 0, net->port);
1288 *abort_no_unlock = 1;
1291 init_ack = &cp->init;
1292 /* validate parameters */
1293 if (init_ack->initiate_tag == 0) {
1294 /* protocol error... send an abort */
1295 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1296 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1297 op_err, 0, net->port);
1298 *abort_no_unlock = 1;
1301 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1302 /* protocol error... send an abort */
1303 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1304 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1305 op_err, 0, net->port);
1306 *abort_no_unlock = 1;
1309 if (init_ack->num_inbound_streams == 0) {
1310 /* protocol error... send an abort */
1311 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1312 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1313 op_err, 0, net->port);
1314 *abort_no_unlock = 1;
1317 if (init_ack->num_outbound_streams == 0) {
1318 /* protocol error... send an abort */
1319 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1320 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1321 op_err, 0, net->port);
1322 *abort_no_unlock = 1;
1325 /* process according to association state... */
1326 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1327 case SCTP_STATE_COOKIE_WAIT:
1328 /* this is the expected state for this chunk */
1329 /* process the INIT-ACK parameters */
1330 if (stcb->asoc.primary_destination->dest_state &
1331 SCTP_ADDR_UNCONFIRMED) {
1333 * The primary is where we sent the INIT, we can
1334 * always consider it confirmed when the INIT-ACK is
1335 * returned. Do this before we load addresses
1338 stcb->asoc.primary_destination->dest_state &=
1339 ~SCTP_ADDR_UNCONFIRMED;
1340 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1341 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1343 if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb,
1344 net, abort_no_unlock, vrf_id) < 0) {
1345 /* error in parsing parameters */
1348 /* update our state */
1349 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1350 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1352 /* reset the RTO calc */
1353 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1354 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1355 stcb->asoc.overall_error_count,
1357 SCTP_FROM_SCTP_INPUT,
1360 stcb->asoc.overall_error_count = 0;
1361 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1363 * collapse the init timer back in case of a exponential
1366 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1369 * the send at the end of the inbound data processing will
1370 * cause the cookie to be sent
1373 case SCTP_STATE_SHUTDOWN_SENT:
1374 /* incorrect state... discard */
1376 case SCTP_STATE_COOKIE_ECHOED:
1377 /* incorrect state... discard */
1379 case SCTP_STATE_OPEN:
1380 /* incorrect state... discard */
1382 case SCTP_STATE_EMPTY:
1383 case SCTP_STATE_INUSE:
1385 /* incorrect state... discard */
1389 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1393 static struct sctp_tcb *
1394 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1395 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1396 struct sctp_inpcb *inp, struct sctp_nets **netp,
1397 struct sockaddr *init_src, int *notification,
1398 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1399 uint32_t vrf_id, uint16_t port);
1403 * handle a state cookie for an existing association m: input packet mbuf
1404 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1405 * "split" mbuf and the cookie signature does not exist offset: offset into
1406 * mbuf to the cookie-echo chunk
1408 static struct sctp_tcb *
1409 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1410 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1411 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1412 struct sockaddr *init_src, int *notification,
1413 uint32_t vrf_id, int auth_skipped, uint32_t auth_offset, uint32_t auth_len, uint16_t port)
1415 struct sctp_association *asoc;
1416 struct sctp_init_chunk *init_cp, init_buf;
1417 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1418 struct sctp_nets *net;
1419 struct mbuf *op_err;
1420 struct sctp_paramhdr *ph;
1421 int init_offset, initack_offset, i;
1427 /* I know that the TCB is non-NULL from the caller */
1429 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1430 if (asoc->cookie_how[how_indx] == 0)
1433 if (how_indx < sizeof(asoc->cookie_how)) {
1434 asoc->cookie_how[how_indx] = 1;
1436 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1437 /* SHUTDOWN came in after sending INIT-ACK */
1438 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1439 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1440 0, M_DONTWAIT, 1, MT_DATA);
1441 if (op_err == NULL) {
1446 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1447 ph = mtod(op_err, struct sctp_paramhdr *);
1448 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
1449 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1450 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag,
1452 if (how_indx < sizeof(asoc->cookie_how))
1453 asoc->cookie_how[how_indx] = 2;
1457 * find and validate the INIT chunk in the cookie (peer's info) the
1458 * INIT should start after the cookie-echo header struct (chunk
1459 * header, state cookie header struct)
1461 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1463 init_cp = (struct sctp_init_chunk *)
1464 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1465 (uint8_t *) & init_buf);
1466 if (init_cp == NULL) {
1467 /* could not pull a INIT chunk in cookie */
1470 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1474 * find and validate the INIT-ACK chunk in the cookie (my info) the
1475 * INIT-ACK follows the INIT chunk
1477 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1478 initack_cp = (struct sctp_init_ack_chunk *)
1479 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1480 (uint8_t *) & initack_buf);
1481 if (initack_cp == NULL) {
1482 /* could not pull INIT-ACK chunk in cookie */
1485 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1488 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1489 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1491 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1492 * to get into the OPEN state
1494 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1496 * Opps, this means that we somehow generated two vtag's
1497 * the same. I.e. we did:
1499 * <---INIT(tag=a)------
1500 * ----INIT-ACK(tag=t)-->
1501 * ----INIT(tag=t)------> *1
1502 * <---INIT-ACK(tag=a)---
1503 * <----CE(tag=t)------------- *2
1505 * At point *1 we should be generating a different
1506 * tag t'. Which means we would throw away the CE and send
1507 * ours instead. Basically this is case C (throw away side).
1509 if (how_indx < sizeof(asoc->cookie_how))
1510 asoc->cookie_how[how_indx] = 17;
1514 switch SCTP_GET_STATE
1516 case SCTP_STATE_COOKIE_WAIT:
1517 case SCTP_STATE_COOKIE_ECHOED:
1519 * INIT was sent but got a COOKIE_ECHO with the
1520 * correct tags... just accept it...but we must
1521 * process the init so that we can make sure we have
1522 * the right seq no's.
1524 /* First we must process the INIT !! */
1525 retval = sctp_process_init(init_cp, stcb);
1527 if (how_indx < sizeof(asoc->cookie_how))
1528 asoc->cookie_how[how_indx] = 3;
1531 /* we have already processed the INIT so no problem */
1532 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1533 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1534 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1535 /* update current state */
1536 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1537 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1539 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1541 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1542 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1543 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1544 stcb->sctp_ep, stcb, asoc->primary_destination);
1546 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1547 sctp_stop_all_cookie_timers(stcb);
1548 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1549 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1550 (inp->sctp_socket->so_qlimit == 0)
1552 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1557 * Here is where collision would go if we
1558 * did a connect() and instead got a
1559 * init/init-ack/cookie done before the
1560 * init-ack came back..
1562 stcb->sctp_ep->sctp_flags |=
1563 SCTP_PCB_FLAGS_CONNECTED;
1564 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1565 so = SCTP_INP_SO(stcb->sctp_ep);
1566 atomic_add_int(&stcb->asoc.refcnt, 1);
1567 SCTP_TCB_UNLOCK(stcb);
1568 SCTP_SOCKET_LOCK(so, 1);
1569 SCTP_TCB_LOCK(stcb);
1570 atomic_add_int(&stcb->asoc.refcnt, -1);
1571 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1572 SCTP_SOCKET_UNLOCK(so, 1);
1576 soisconnected(stcb->sctp_socket);
1577 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1578 SCTP_SOCKET_UNLOCK(so, 1);
1581 /* notify upper layer */
1582 *notification = SCTP_NOTIFY_ASSOC_UP;
1584 * since we did not send a HB make sure we don't
1587 net->hb_responded = 1;
1588 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1589 &cookie->time_entered,
1590 sctp_align_unsafe_makecopy,
1591 SCTP_RTT_FROM_NON_DATA);
1593 if (stcb->asoc.sctp_autoclose_ticks &&
1594 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1595 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1601 * we're in the OPEN state (or beyond), so peer must
1602 * have simply lost the COOKIE-ACK
1606 sctp_stop_all_cookie_timers(stcb);
1608 * We ignore the return code here.. not sure if we should
1609 * somehow abort.. but we do have an existing asoc. This
1610 * really should not fail.
1612 if (sctp_load_addresses_from_init(stcb, m,
1613 init_offset + sizeof(struct sctp_init_chunk),
1614 initack_offset, sh, init_src)) {
1615 if (how_indx < sizeof(asoc->cookie_how))
1616 asoc->cookie_how[how_indx] = 4;
1619 /* respond with a COOKIE-ACK */
1620 sctp_toss_old_cookies(stcb, asoc);
1621 sctp_send_cookie_ack(stcb);
1622 if (how_indx < sizeof(asoc->cookie_how))
1623 asoc->cookie_how[how_indx] = 5;
1626 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1627 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1628 cookie->tie_tag_my_vtag == 0 &&
1629 cookie->tie_tag_peer_vtag == 0) {
1631 * case C in Section 5.2.4 Table 2: XMOO silently discard
1633 if (how_indx < sizeof(asoc->cookie_how))
1634 asoc->cookie_how[how_indx] = 6;
1638 * If nat support, and the below and stcb is established, send back
1639 * a ABORT(colliding state) if we are established.
1641 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1642 (asoc->peer_supports_nat) &&
1643 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1644 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1645 (asoc->peer_vtag == 0)))) {
1647 * Special case - Peer's support nat. We may have two init's
1648 * that we gave out the same tag on since one was not
1649 * established.. i.e. we get INIT from host-1 behind the nat
1650 * and we respond tag-a, we get a INIT from host-2 behind
1651 * the nat and we get tag-a again. Then we bring up host-1
1652 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1653 * Now we have colliding state. We must send an abort here
1654 * with colliding state indication.
1656 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1657 0, M_DONTWAIT, 1, MT_DATA);
1658 if (op_err == NULL) {
1662 /* pre-reserve some space */
1664 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
1666 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
1668 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
1669 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1671 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1672 ph = mtod(op_err, struct sctp_paramhdr *);
1673 ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE);
1674 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1675 sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port);
1678 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1679 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1680 (asoc->peer_vtag == 0))) {
1682 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1683 * should be ok, re-accept peer info
1685 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1687 * Extension of case C. If we hit this, then the
1688 * random number generator returned the same vtag
1689 * when we first sent our INIT-ACK and when we later
1690 * sent our INIT. The side with the seq numbers that
1691 * are different will be the one that normnally
1692 * would have hit case C. This in effect "extends"
1693 * our vtags in this collision case to be 64 bits.
1694 * The same collision could occur aka you get both
1695 * vtag and seq number the same twice in a row.. but
1696 * is much less likely. If it did happen then we
1697 * would proceed through and bring up the assoc.. we
1698 * may end up with the wrong stream setup however..
1699 * which would be bad.. but there is no way to
1700 * tell.. until we send on a stream that does not
1703 if (how_indx < sizeof(asoc->cookie_how))
1704 asoc->cookie_how[how_indx] = 7;
1708 if (how_indx < sizeof(asoc->cookie_how))
1709 asoc->cookie_how[how_indx] = 8;
1710 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1711 sctp_stop_all_cookie_timers(stcb);
1713 * since we did not send a HB make sure we don't double
1716 net->hb_responded = 1;
1717 if (stcb->asoc.sctp_autoclose_ticks &&
1718 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1719 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1722 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1723 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1725 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1727 * Ok the peer probably discarded our data (if we
1728 * echoed a cookie+data). So anything on the
1729 * sent_queue should be marked for retransmit, we
1730 * may not get something to kick us so it COULD
1731 * still take a timeout to move these.. but it can't
1732 * hurt to mark them.
1734 struct sctp_tmit_chunk *chk;
1736 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1737 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1738 chk->sent = SCTP_DATAGRAM_RESEND;
1739 sctp_flight_size_decrease(chk);
1740 sctp_total_flight_decrease(stcb, chk);
1741 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1747 /* process the INIT info (peer's info) */
1748 retval = sctp_process_init(init_cp, stcb);
1750 if (how_indx < sizeof(asoc->cookie_how))
1751 asoc->cookie_how[how_indx] = 9;
1754 if (sctp_load_addresses_from_init(stcb, m,
1755 init_offset + sizeof(struct sctp_init_chunk),
1756 initack_offset, sh, init_src)) {
1757 if (how_indx < sizeof(asoc->cookie_how))
1758 asoc->cookie_how[how_indx] = 10;
1761 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1762 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1763 *notification = SCTP_NOTIFY_ASSOC_UP;
1765 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1766 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1767 (inp->sctp_socket->so_qlimit == 0)) {
1768 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1772 stcb->sctp_ep->sctp_flags |=
1773 SCTP_PCB_FLAGS_CONNECTED;
1774 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1775 so = SCTP_INP_SO(stcb->sctp_ep);
1776 atomic_add_int(&stcb->asoc.refcnt, 1);
1777 SCTP_TCB_UNLOCK(stcb);
1778 SCTP_SOCKET_LOCK(so, 1);
1779 SCTP_TCB_LOCK(stcb);
1780 atomic_add_int(&stcb->asoc.refcnt, -1);
1781 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1782 SCTP_SOCKET_UNLOCK(so, 1);
1786 soisconnected(stcb->sctp_socket);
1787 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1788 SCTP_SOCKET_UNLOCK(so, 1);
1791 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1792 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1794 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1795 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1796 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1797 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1799 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1801 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1802 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1803 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1804 stcb->sctp_ep, stcb, asoc->primary_destination);
1806 sctp_stop_all_cookie_timers(stcb);
1807 sctp_toss_old_cookies(stcb, asoc);
1808 sctp_send_cookie_ack(stcb);
1811 * only if we have retrans set do we do this. What
1812 * this call does is get only the COOKIE-ACK out and
1813 * then when we return the normal call to
1814 * sctp_chunk_output will get the retrans out behind
1817 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1819 if (how_indx < sizeof(asoc->cookie_how))
1820 asoc->cookie_how[how_indx] = 11;
1824 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1825 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1826 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1827 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1828 cookie->tie_tag_peer_vtag != 0) {
1829 struct sctpasochead *head;
1831 if (asoc->peer_supports_nat) {
1833 * This is a gross gross hack. just call the
1834 * cookie_new code since we are allowing a duplicate
1835 * association. I hope this works...
1837 return (sctp_process_cookie_new(m, iphlen, offset, sh, cookie, cookie_len,
1838 inp, netp, init_src, notification,
1839 auth_skipped, auth_offset, auth_len,
1843 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1846 if (how_indx < sizeof(asoc->cookie_how))
1847 asoc->cookie_how[how_indx] = 12;
1848 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1849 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1851 /* notify upper layer */
1852 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1853 atomic_add_int(&stcb->asoc.refcnt, 1);
1854 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1855 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1856 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1857 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1859 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1860 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1861 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1862 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1864 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1865 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1866 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1867 stcb->sctp_ep, stcb, asoc->primary_destination);
1869 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1870 /* move to OPEN state, if not in SHUTDOWN_SENT */
1871 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1873 asoc->pre_open_streams =
1874 ntohs(initack_cp->init.num_outbound_streams);
1875 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1876 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1877 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1879 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1881 asoc->str_reset_seq_in = asoc->init_seq_number;
1883 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1884 if (asoc->mapping_array) {
1885 memset(asoc->mapping_array, 0,
1886 asoc->mapping_array_size);
1888 if (asoc->nr_mapping_array) {
1889 memset(asoc->nr_mapping_array, 0,
1890 asoc->mapping_array_size);
1892 SCTP_TCB_UNLOCK(stcb);
1893 SCTP_INP_INFO_WLOCK();
1894 SCTP_INP_WLOCK(stcb->sctp_ep);
1895 SCTP_TCB_LOCK(stcb);
1896 atomic_add_int(&stcb->asoc.refcnt, -1);
1897 /* send up all the data */
1898 SCTP_TCB_SEND_LOCK(stcb);
1900 sctp_report_all_outbound(stcb, 1, SCTP_SO_NOT_LOCKED);
1901 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1902 stcb->asoc.strmout[i].stream_no = i;
1903 stcb->asoc.strmout[i].next_sequence_sent = 0;
1904 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1906 /* process the INIT-ACK info (my info) */
1907 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1908 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1910 /* pull from vtag hash */
1911 LIST_REMOVE(stcb, sctp_asocs);
1912 /* re-insert to new vtag position */
1913 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1914 SCTP_BASE_INFO(hashasocmark))];
1916 * put it in the bucket in the vtag hash of assoc's for the
1919 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1921 /* process the INIT info (peer's info) */
1922 SCTP_TCB_SEND_UNLOCK(stcb);
1923 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1924 SCTP_INP_INFO_WUNLOCK();
1926 retval = sctp_process_init(init_cp, stcb);
1928 if (how_indx < sizeof(asoc->cookie_how))
1929 asoc->cookie_how[how_indx] = 13;
1934 * since we did not send a HB make sure we don't double
1937 net->hb_responded = 1;
1939 if (sctp_load_addresses_from_init(stcb, m,
1940 init_offset + sizeof(struct sctp_init_chunk),
1941 initack_offset, sh, init_src)) {
1942 if (how_indx < sizeof(asoc->cookie_how))
1943 asoc->cookie_how[how_indx] = 14;
1947 /* respond with a COOKIE-ACK */
1948 sctp_stop_all_cookie_timers(stcb);
1949 sctp_toss_old_cookies(stcb, asoc);
1950 sctp_send_cookie_ack(stcb);
1951 if (how_indx < sizeof(asoc->cookie_how))
1952 asoc->cookie_how[how_indx] = 15;
1956 if (how_indx < sizeof(asoc->cookie_how))
1957 asoc->cookie_how[how_indx] = 16;
1958 /* all other cases... */
1964 * handle a state cookie for a new association m: input packet mbuf chain--
1965 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
1966 * and the cookie signature does not exist offset: offset into mbuf to the
1967 * cookie-echo chunk length: length of the cookie chunk to: where the init
1968 * was from returns a new TCB
1971 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1972 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1973 struct sctp_inpcb *inp, struct sctp_nets **netp,
1974 struct sockaddr *init_src, int *notification,
1975 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1976 uint32_t vrf_id, uint16_t port)
1978 struct sctp_tcb *stcb;
1979 struct sctp_init_chunk *init_cp, init_buf;
1980 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1981 struct sockaddr_storage sa_store;
1982 struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
1983 struct sctp_association *asoc;
1984 int init_offset, initack_offset, initack_limit;
1987 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
1990 struct sockaddr_in *sin;
1994 struct sockaddr_in6 *sin6;
1997 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2000 so = SCTP_INP_SO(inp);
2004 * find and validate the INIT chunk in the cookie (peer's info) the
2005 * INIT should start after the cookie-echo header struct (chunk
2006 * header, state cookie header struct)
2008 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2009 init_cp = (struct sctp_init_chunk *)
2010 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2011 (uint8_t *) & init_buf);
2012 if (init_cp == NULL) {
2013 /* could not pull a INIT chunk in cookie */
2014 SCTPDBG(SCTP_DEBUG_INPUT1,
2015 "process_cookie_new: could not pull INIT chunk hdr\n");
2018 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2019 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2022 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2024 * find and validate the INIT-ACK chunk in the cookie (my info) the
2025 * INIT-ACK follows the INIT chunk
2027 initack_cp = (struct sctp_init_ack_chunk *)
2028 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2029 (uint8_t *) & initack_buf);
2030 if (initack_cp == NULL) {
2031 /* could not pull INIT-ACK chunk in cookie */
2032 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2035 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2039 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2040 * "initack_limit" value. This is because the chk_length field
2041 * includes the length of the cookie, but the cookie is omitted when
2042 * the INIT and INIT_ACK are tacked onto the cookie...
2044 initack_limit = offset + cookie_len;
2047 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2052 * Here we do a trick, we set in NULL for the proc/thread argument.
2053 * We do this since in effect we only use the p argument when the
2054 * socket is unbound and we must do an implicit bind. Since we are
2055 * getting a cookie, we cannot be unbound.
2057 stcb = sctp_aloc_assoc(inp, init_src, &error,
2058 ntohl(initack_cp->init.initiate_tag), vrf_id,
2059 (struct thread *)NULL
2062 struct mbuf *op_err;
2064 /* memory problem? */
2065 SCTPDBG(SCTP_DEBUG_INPUT1,
2066 "process_cookie_new: no room for another TCB!\n");
2067 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2069 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2070 sh, op_err, vrf_id, port);
2073 /* get the correct sctp_nets */
2075 *netp = sctp_findnet(stcb, init_src);
2078 /* get scope variables out of cookie */
2079 asoc->ipv4_local_scope = cookie->ipv4_scope;
2080 asoc->site_scope = cookie->site_scope;
2081 asoc->local_scope = cookie->local_scope;
2082 asoc->loopback_scope = cookie->loopback_scope;
2084 if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2085 (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2086 struct mbuf *op_err;
2089 * Houston we have a problem. The EP changed while the
2090 * cookie was in flight. Only recourse is to abort the
2093 atomic_add_int(&stcb->asoc.refcnt, 1);
2094 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2095 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2096 sh, op_err, vrf_id, port);
2097 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2098 SCTP_TCB_UNLOCK(stcb);
2099 SCTP_SOCKET_LOCK(so, 1);
2100 SCTP_TCB_LOCK(stcb);
2102 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2103 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2104 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2105 SCTP_SOCKET_UNLOCK(so, 1);
2107 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2110 /* process the INIT-ACK info (my info) */
2111 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2112 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2113 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2114 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2115 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2116 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2117 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2118 asoc->str_reset_seq_in = asoc->init_seq_number;
2120 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2122 /* process the INIT info (peer's info) */
2124 retval = sctp_process_init(init_cp, stcb);
2128 atomic_add_int(&stcb->asoc.refcnt, 1);
2129 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2130 SCTP_TCB_UNLOCK(stcb);
2131 SCTP_SOCKET_LOCK(so, 1);
2132 SCTP_TCB_LOCK(stcb);
2134 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2135 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2136 SCTP_SOCKET_UNLOCK(so, 1);
2138 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2141 /* load all addresses */
2142 if (sctp_load_addresses_from_init(stcb, m,
2143 init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh,
2145 atomic_add_int(&stcb->asoc.refcnt, 1);
2146 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2147 SCTP_TCB_UNLOCK(stcb);
2148 SCTP_SOCKET_LOCK(so, 1);
2149 SCTP_TCB_LOCK(stcb);
2151 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
2152 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2153 SCTP_SOCKET_UNLOCK(so, 1);
2155 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2159 * verify any preceding AUTH chunk that was skipped
2161 /* pull the local authentication parameters from the cookie/init-ack */
2162 sctp_auth_get_cookie_params(stcb, m,
2163 initack_offset + sizeof(struct sctp_init_ack_chunk),
2164 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2166 struct sctp_auth_chunk *auth;
2168 auth = (struct sctp_auth_chunk *)
2169 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2170 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2171 /* auth HMAC failed, dump the assoc and packet */
2172 SCTPDBG(SCTP_DEBUG_AUTH1,
2173 "COOKIE-ECHO: AUTH failed\n");
2174 atomic_add_int(&stcb->asoc.refcnt, 1);
2175 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2176 SCTP_TCB_UNLOCK(stcb);
2177 SCTP_SOCKET_LOCK(so, 1);
2178 SCTP_TCB_LOCK(stcb);
2180 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2181 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2182 SCTP_SOCKET_UNLOCK(so, 1);
2184 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2187 /* remaining chunks checked... good to go */
2188 stcb->asoc.authenticated = 1;
2191 /* update current state */
2192 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2193 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2194 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2195 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2196 stcb->sctp_ep, stcb, asoc->primary_destination);
2198 sctp_stop_all_cookie_timers(stcb);
2199 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2200 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2203 * if we're doing ASCONFs, check to see if we have any new local
2204 * addresses that need to get added to the peer (eg. addresses
2205 * changed while cookie echo in flight). This needs to be done
2206 * after we go to the OPEN state to do the correct asconf
2207 * processing. else, make sure we have the correct addresses in our
2211 /* warning, we re-use sin, sin6, sa_store here! */
2212 /* pull in local_address (our "from" address) */
2213 switch (cookie->laddr_type) {
2215 case SCTP_IPV4_ADDRESS:
2216 /* source addr is IPv4 */
2217 sin = (struct sockaddr_in *)initack_src;
2218 memset(sin, 0, sizeof(*sin));
2219 sin->sin_family = AF_INET;
2220 sin->sin_len = sizeof(struct sockaddr_in);
2221 sin->sin_addr.s_addr = cookie->laddress[0];
2225 case SCTP_IPV6_ADDRESS:
2226 /* source addr is IPv6 */
2227 sin6 = (struct sockaddr_in6 *)initack_src;
2228 memset(sin6, 0, sizeof(*sin6));
2229 sin6->sin6_family = AF_INET6;
2230 sin6->sin6_len = sizeof(struct sockaddr_in6);
2231 sin6->sin6_scope_id = cookie->scope_id;
2232 memcpy(&sin6->sin6_addr, cookie->laddress,
2233 sizeof(sin6->sin6_addr));
2237 atomic_add_int(&stcb->asoc.refcnt, 1);
2238 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2239 SCTP_TCB_UNLOCK(stcb);
2240 SCTP_SOCKET_LOCK(so, 1);
2241 SCTP_TCB_LOCK(stcb);
2243 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2244 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2245 SCTP_SOCKET_UNLOCK(so, 1);
2247 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2251 /* set up to notify upper layer */
2252 *notification = SCTP_NOTIFY_ASSOC_UP;
2253 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2254 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2255 (inp->sctp_socket->so_qlimit == 0)) {
2257 * This is an endpoint that called connect() how it got a
2258 * cookie that is NEW is a bit of a mystery. It must be that
2259 * the INIT was sent, but before it got there.. a complete
2260 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2261 * should have went to the other code.. not here.. oh well..
2262 * a bit of protection is worth having..
2264 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2265 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2266 atomic_add_int(&stcb->asoc.refcnt, 1);
2267 SCTP_TCB_UNLOCK(stcb);
2268 SCTP_SOCKET_LOCK(so, 1);
2269 SCTP_TCB_LOCK(stcb);
2270 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2271 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2272 SCTP_SOCKET_UNLOCK(so, 1);
2276 soisconnected(stcb->sctp_socket);
2277 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2278 SCTP_SOCKET_UNLOCK(so, 1);
2280 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2281 (inp->sctp_socket->so_qlimit)) {
2283 * We don't want to do anything with this one. Since it is
2284 * the listening guy. The timer will get started for
2285 * accepted connections in the caller.
2289 /* since we did not send a HB make sure we don't double things */
2290 if ((netp) && (*netp))
2291 (*netp)->hb_responded = 1;
2293 if (stcb->asoc.sctp_autoclose_ticks &&
2294 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2295 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2297 /* calculate the RTT */
2298 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2299 if ((netp) && (*netp)) {
2300 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2301 &cookie->time_entered, sctp_align_unsafe_makecopy,
2302 SCTP_RTT_FROM_NON_DATA);
2304 /* respond with a COOKIE-ACK */
2305 sctp_send_cookie_ack(stcb);
2308 * check the address lists for any ASCONFs that need to be sent
2309 * AFTER the cookie-ack is sent
2311 sctp_check_address_list(stcb, m,
2312 initack_offset + sizeof(struct sctp_init_ack_chunk),
2313 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2314 initack_src, cookie->local_scope, cookie->site_scope,
2315 cookie->ipv4_scope, cookie->loopback_scope);
2322 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2323 * we NEED to make sure we are not already using the vtag. If so we
2324 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2325 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2326 SCTP_BASE_INFO(hashasocmark))];
2327 LIST_FOREACH(stcb, head, sctp_asocs) {
2328 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2329 -- SEND ABORT - TRY AGAIN --
2335 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2336 * existing (non-NULL) TCB
2338 static struct mbuf *
2339 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2340 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2341 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2342 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2343 struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint16_t port)
2345 struct sctp_state_cookie *cookie;
2346 struct sctp_tcb *l_stcb = *stcb;
2347 struct sctp_inpcb *l_inp;
2348 struct sockaddr *to;
2349 struct sctp_pcb *ep;
2351 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2353 uint8_t cookie_ok = 0;
2354 unsigned int size_of_pkt, sig_offset, cookie_offset;
2355 unsigned int cookie_len;
2357 struct timeval time_expires;
2358 struct sockaddr_storage dest_store;
2359 struct sockaddr *localep_sa = (struct sockaddr *)&dest_store;
2361 int notification = 0;
2362 struct sctp_nets *netl;
2363 int had_a_existing_tcb = 0;
2364 int send_int_conf = 0;
2367 struct sockaddr_in sin;
2371 struct sockaddr_in6 sin6;
2375 SCTPDBG(SCTP_DEBUG_INPUT2,
2376 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2378 if (inp_p == NULL) {
2381 /* First get the destination address setup too. */
2382 iph = mtod(m, struct ip *);
2383 switch (iph->ip_v) {
2388 struct sockaddr_in *lsin;
2390 lsin = (struct sockaddr_in *)(localep_sa);
2391 memset(lsin, 0, sizeof(*lsin));
2392 lsin->sin_family = AF_INET;
2393 lsin->sin_len = sizeof(*lsin);
2394 lsin->sin_port = sh->dest_port;
2395 lsin->sin_addr.s_addr = iph->ip_dst.s_addr;
2396 size_of_pkt = SCTP_GET_IPV4_LENGTH(iph);
2401 case IPV6_VERSION >> 4:
2404 struct ip6_hdr *ip6;
2405 struct sockaddr_in6 *lsin6;
2407 lsin6 = (struct sockaddr_in6 *)(localep_sa);
2408 memset(lsin6, 0, sizeof(*lsin6));
2409 lsin6->sin6_family = AF_INET6;
2410 lsin6->sin6_len = sizeof(struct sockaddr_in6);
2411 ip6 = mtod(m, struct ip6_hdr *);
2412 lsin6->sin6_port = sh->dest_port;
2413 lsin6->sin6_addr = ip6->ip6_dst;
2414 size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen;
2422 cookie = &cp->cookie;
2423 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2424 cookie_len = ntohs(cp->ch.chunk_length);
2426 if ((cookie->peerport != sh->src_port) &&
2427 (cookie->myport != sh->dest_port) &&
2428 (cookie->my_vtag != sh->v_tag)) {
2430 * invalid ports or bad tag. Note that we always leave the
2431 * v_tag in the header in network order and when we stored
2432 * it in the my_vtag slot we also left it in network order.
2433 * This maintains the match even though it may be in the
2434 * opposite byte order of the machine :->
2438 if (cookie_len > size_of_pkt ||
2439 cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2440 sizeof(struct sctp_init_chunk) +
2441 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2442 /* cookie too long! or too small */
2446 * split off the signature into its own mbuf (since it should not be
2447 * calculated in the sctp_hmac_m() call).
2449 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2450 if (sig_offset > size_of_pkt) {
2451 /* packet not correct size! */
2452 /* XXX this may already be accounted for earlier... */
2455 m_sig = m_split(m, sig_offset, M_DONTWAIT);
2456 if (m_sig == NULL) {
2457 /* out of memory or ?? */
2460 #ifdef SCTP_MBUF_LOGGING
2461 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2466 if (SCTP_BUF_IS_EXTENDED(mat)) {
2467 sctp_log_mb(mat, SCTP_MBUF_SPLIT);
2469 mat = SCTP_BUF_NEXT(mat);
2475 * compute the signature/digest for the cookie
2477 ep = &(*inp_p)->sctp_ep;
2480 SCTP_TCB_UNLOCK(l_stcb);
2482 SCTP_INP_RLOCK(l_inp);
2484 SCTP_TCB_LOCK(l_stcb);
2486 /* which cookie is it? */
2487 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2488 (ep->current_secret_number != ep->last_secret_number)) {
2489 /* it's the old cookie */
2490 (void)sctp_hmac_m(SCTP_HMAC,
2491 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2492 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2494 /* it's the current cookie */
2495 (void)sctp_hmac_m(SCTP_HMAC,
2496 (uint8_t *) ep->secret_key[(int)ep->current_secret_number],
2497 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2499 /* get the signature */
2500 SCTP_INP_RUNLOCK(l_inp);
2501 sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig);
2503 /* couldn't find signature */
2504 sctp_m_freem(m_sig);
2507 /* compare the received digest with the computed digest */
2508 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2509 /* try the old cookie? */
2510 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2511 (ep->current_secret_number != ep->last_secret_number)) {
2512 /* compute digest with old */
2513 (void)sctp_hmac_m(SCTP_HMAC,
2514 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2515 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2517 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2525 * Now before we continue we must reconstruct our mbuf so that
2526 * normal processing of any other chunks will work.
2532 while (SCTP_BUF_NEXT(m_at) != NULL) {
2533 m_at = SCTP_BUF_NEXT(m_at);
2535 SCTP_BUF_NEXT(m_at) = m_sig;
2538 if (cookie_ok == 0) {
2539 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2540 SCTPDBG(SCTP_DEBUG_INPUT2,
2541 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2542 (uint32_t) offset, cookie_offset, sig_offset);
2546 * check the cookie timestamps to be sure it's not stale
2548 (void)SCTP_GETTIME_TIMEVAL(&now);
2549 /* Expire time is in Ticks, so we convert to seconds */
2550 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2551 time_expires.tv_usec = cookie->time_entered.tv_usec;
2553 * TODO sctp_constants.h needs alternative time macros when _KERNEL
2556 if (timevalcmp(&now, &time_expires, >)) {
2557 /* cookie is stale! */
2558 struct mbuf *op_err;
2559 struct sctp_stale_cookie_msg *scm;
2562 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg),
2563 0, M_DONTWAIT, 1, MT_DATA);
2564 if (op_err == NULL) {
2569 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg);
2570 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
2571 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
2572 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
2573 (sizeof(uint32_t))));
2574 /* seconds to usec */
2575 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2578 tim = now.tv_usec - cookie->time_entered.tv_usec;
2579 scm->time_usec = htonl(tim);
2580 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag,
2585 * Now we must see with the lookup address if we have an existing
2586 * asoc. This will only happen if we were in the COOKIE-WAIT state
2587 * and a INIT collided with us and somewhere the peer sent the
2588 * cookie on another address besides the single address our assoc
2589 * had for him. In this case we will have one of the tie-tags set at
2590 * least AND the address field in the cookie can be used to look it
2594 switch (cookie->addr_type) {
2596 case SCTP_IPV6_ADDRESS:
2597 memset(&sin6, 0, sizeof(sin6));
2598 sin6.sin6_family = AF_INET6;
2599 sin6.sin6_len = sizeof(sin6);
2600 sin6.sin6_port = sh->src_port;
2601 sin6.sin6_scope_id = cookie->scope_id;
2602 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2603 sizeof(sin6.sin6_addr.s6_addr));
2604 to = (struct sockaddr *)&sin6;
2608 case SCTP_IPV4_ADDRESS:
2609 memset(&sin, 0, sizeof(sin));
2610 sin.sin_family = AF_INET;
2611 sin.sin_len = sizeof(sin);
2612 sin.sin_port = sh->src_port;
2613 sin.sin_addr.s_addr = cookie->address[0];
2614 to = (struct sockaddr *)&sin;
2618 /* This should not happen */
2621 if ((*stcb == NULL) && to) {
2622 /* Yep, lets check */
2623 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL);
2624 if (*stcb == NULL) {
2626 * We should have only got back the same inp. If we
2627 * got back a different ep we have a problem. The
2628 * original findep got back l_inp and now
2630 if (l_inp != *inp_p) {
2631 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2634 if (*locked_tcb == NULL) {
2636 * In this case we found the assoc only
2637 * after we locked the create lock. This
2638 * means we are in a colliding case and we
2639 * must make sure that we unlock the tcb if
2640 * its one of the cases where we throw away
2641 * the incoming packets.
2643 *locked_tcb = *stcb;
2646 * We must also increment the inp ref count
2647 * since the ref_count flags was set when we
2648 * did not find the TCB, now we found it
2649 * which reduces the refcount.. we must
2650 * raise it back out to balance it all :-)
2652 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2653 if ((*stcb)->sctp_ep != l_inp) {
2654 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2655 (*stcb)->sctp_ep, l_inp);
2663 cookie_len -= SCTP_SIGNATURE_SIZE;
2664 if (*stcb == NULL) {
2665 /* this is the "normal" case... get a new TCB */
2666 *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie,
2667 cookie_len, *inp_p, netp, to, ¬ification,
2668 auth_skipped, auth_offset, auth_len, vrf_id, port);
2670 /* this is abnormal... cookie-echo on existing TCB */
2671 had_a_existing_tcb = 1;
2672 *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh,
2673 cookie, cookie_len, *inp_p, *stcb, netp, to,
2674 ¬ification, vrf_id, auth_skipped, auth_offset, auth_len, port);
2677 if (*stcb == NULL) {
2678 /* still no TCB... must be bad cookie-echo */
2681 if ((*netp != NULL) && (m->m_flags & M_FLOWID)) {
2682 (*netp)->flowid = m->m_pkthdr.flowid;
2684 (*netp)->flowidset = 1;
2688 * Ok, we built an association so confirm the address we sent the
2691 netl = sctp_findnet(*stcb, to);
2693 * This code should in theory NOT run but
2696 /* TSNH! Huh, why do I need to add this address here? */
2697 if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2700 netl = sctp_findnet(*stcb, to);
2703 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2704 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2705 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2710 sctp_start_net_timers(*stcb);
2711 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2712 if (!had_a_existing_tcb ||
2713 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2715 * If we have a NEW cookie or the connect never
2716 * reached the connected state during collision we
2717 * must do the TCP accept thing.
2719 struct socket *so, *oso;
2720 struct sctp_inpcb *inp;
2722 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2724 * For a restart we will keep the same
2725 * socket, no need to do anything. I THINK!!
2727 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2728 if (send_int_conf) {
2729 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2730 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2734 oso = (*inp_p)->sctp_socket;
2735 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2736 SCTP_TCB_UNLOCK((*stcb));
2737 CURVNET_SET(oso->so_vnet);
2738 so = sonewconn(oso, 0
2741 SCTP_TCB_LOCK((*stcb));
2742 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2745 struct mbuf *op_err;
2747 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2748 struct socket *pcb_so;
2751 /* Too many sockets */
2752 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2753 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2754 sctp_abort_association(*inp_p, NULL, m, iphlen,
2755 sh, op_err, vrf_id, port);
2756 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2757 pcb_so = SCTP_INP_SO(*inp_p);
2758 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2759 SCTP_TCB_UNLOCK((*stcb));
2760 SCTP_SOCKET_LOCK(pcb_so, 1);
2761 SCTP_TCB_LOCK((*stcb));
2762 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2764 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2765 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2766 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2770 inp = (struct sctp_inpcb *)so->so_pcb;
2771 SCTP_INP_INCR_REF(inp);
2773 * We add the unbound flag here so that if we get an
2774 * soabort() before we get the move_pcb done, we
2775 * will properly cleanup.
2777 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2778 SCTP_PCB_FLAGS_CONNECTED |
2779 SCTP_PCB_FLAGS_IN_TCPPOOL |
2780 SCTP_PCB_FLAGS_UNBOUND |
2781 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2782 SCTP_PCB_FLAGS_DONT_WAKE);
2783 inp->sctp_features = (*inp_p)->sctp_features;
2784 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2785 inp->sctp_socket = so;
2786 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2787 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2788 inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable;
2789 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2790 inp->sctp_context = (*inp_p)->sctp_context;
2791 inp->inp_starting_point_for_iterator = NULL;
2793 * copy in the authentication parameters from the
2796 if (inp->sctp_ep.local_hmacs)
2797 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2798 inp->sctp_ep.local_hmacs =
2799 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2800 if (inp->sctp_ep.local_auth_chunks)
2801 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2802 inp->sctp_ep.local_auth_chunks =
2803 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2806 * Now we must move it from one hash table to
2807 * another and get the tcb in the right place.
2811 * This is where the one-2-one socket is put into
2812 * the accept state waiting for the accept!
2815 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2817 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2819 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2820 SCTP_TCB_UNLOCK((*stcb));
2822 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2824 SCTP_TCB_LOCK((*stcb));
2825 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2829 * now we must check to see if we were aborted while
2830 * the move was going on and the lock/unlock
2833 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2835 * yep it was, we leave the assoc attached
2836 * to the socket since the sctp_inpcb_free()
2837 * call will send an abort for us.
2839 SCTP_INP_DECR_REF(inp);
2842 SCTP_INP_DECR_REF(inp);
2843 /* Switch over to the new guy */
2845 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2846 if (send_int_conf) {
2847 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2848 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2851 * Pull it from the incomplete queue and wake the
2854 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2855 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2856 SCTP_TCB_UNLOCK((*stcb));
2857 SCTP_SOCKET_LOCK(so, 1);
2860 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2861 SCTP_TCB_LOCK((*stcb));
2862 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2863 SCTP_SOCKET_UNLOCK(so, 1);
2868 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
2870 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2872 if (send_int_conf) {
2873 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2874 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2881 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2882 struct sctp_tcb *stcb, struct sctp_nets *net)
2884 /* cp must not be used, others call this without a c-ack :-) */
2885 struct sctp_association *asoc;
2887 SCTPDBG(SCTP_DEBUG_INPUT2,
2888 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2894 sctp_stop_all_cookie_timers(stcb);
2895 /* process according to association state */
2896 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2897 /* state change only needed when I am in right state */
2898 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2899 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2900 sctp_start_net_timers(stcb);
2901 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2902 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2903 stcb->sctp_ep, stcb, asoc->primary_destination);
2907 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2908 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2909 if (asoc->overall_error_count == 0) {
2910 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2911 &asoc->time_entered, sctp_align_safe_nocopy,
2912 SCTP_RTT_FROM_NON_DATA);
2914 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2915 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2916 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2917 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2918 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2922 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2923 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2924 so = SCTP_INP_SO(stcb->sctp_ep);
2925 atomic_add_int(&stcb->asoc.refcnt, 1);
2926 SCTP_TCB_UNLOCK(stcb);
2927 SCTP_SOCKET_LOCK(so, 1);
2928 SCTP_TCB_LOCK(stcb);
2929 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2931 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2932 soisconnected(stcb->sctp_socket);
2934 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2935 SCTP_SOCKET_UNLOCK(so, 1);
2939 * since we did not send a HB make sure we don't double
2942 net->hb_responded = 1;
2944 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2946 * We don't need to do the asconf thing, nor hb or
2947 * autoclose if the socket is closed.
2951 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2955 if (stcb->asoc.sctp_autoclose_ticks &&
2956 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2957 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2958 stcb->sctp_ep, stcb, NULL);
2961 * send ASCONF if parameters are pending and ASCONFs are
2962 * allowed (eg. addresses changed when init/cookie echo were
2965 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2966 (stcb->asoc.peer_supports_asconf) &&
2967 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2968 #ifdef SCTP_TIMER_BASED_ASCONF
2969 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2970 stcb->sctp_ep, stcb,
2971 stcb->asoc.primary_destination);
2973 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2974 SCTP_ADDR_NOT_LOCKED);
2979 /* Toss the cookie if I can */
2980 sctp_toss_old_cookies(stcb, asoc);
2981 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
2982 /* Restart the timer if we have pending data */
2983 struct sctp_tmit_chunk *chk;
2985 chk = TAILQ_FIRST(&asoc->sent_queue);
2986 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
2991 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2992 struct sctp_tcb *stcb)
2994 struct sctp_nets *net;
2995 struct sctp_tmit_chunk *lchk;
2996 struct sctp_ecne_chunk bkup;
2997 uint8_t override_bit;
2998 uint32_t tsn, window_data_tsn;
3000 unsigned int pkt_cnt;
3002 len = ntohs(cp->ch.chunk_length);
3003 if ((len != sizeof(struct sctp_ecne_chunk)) &&
3004 (len != sizeof(struct old_sctp_ecne_chunk))) {
3007 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3008 /* Its the old format */
3009 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3010 bkup.num_pkts_since_cwr = htonl(1);
3013 SCTP_STAT_INCR(sctps_recvecne);
3014 tsn = ntohl(cp->tsn);
3015 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3016 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3018 window_data_tsn = stcb->asoc.sending_seq - 1;
3020 window_data_tsn = lchk->rec.data.TSN_seq;
3023 /* Find where it was sent to if possible. */
3025 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3026 if (lchk->rec.data.TSN_seq == tsn) {
3028 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3031 if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) {
3037 * What to do. A previous send of a CWR was possibly lost.
3038 * See how old it is, we may have it marked on the actual
3041 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3042 if (tsn == net->last_cwr_tsn) {
3043 /* Found him, send it off */
3049 * If we reach here, we need to send a special CWR
3050 * that says hey, we did this a long time ago and
3051 * you lost the response.
3053 net = TAILQ_FIRST(&stcb->asoc.nets);
3058 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3065 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3066 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3068 * JRS - Use the congestion control given in the pluggable
3071 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3073 * We reduce once every RTT. So we will only lower cwnd at
3074 * the next sending seq i.e. the window_data_tsn
3076 net->cwr_window_tsn = window_data_tsn;
3077 net->ecn_ce_pkt_cnt += pkt_cnt;
3078 net->lost_cnt = pkt_cnt;
3079 net->last_cwr_tsn = tsn;
3081 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3082 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3083 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3085 * Another loss in the same window update how many
3086 * marks/packets lost we have had.
3090 if (pkt_cnt > net->lost_cnt) {
3091 /* Should be the case */
3092 cnt = (pkt_cnt - net->lost_cnt);
3093 net->ecn_ce_pkt_cnt += cnt;
3095 net->lost_cnt = pkt_cnt;
3096 net->last_cwr_tsn = tsn;
3098 * Most CC functions will ignore this call, since we
3099 * are in-window yet of the initial CE the peer saw.
3101 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3105 * We always send a CWR this way if our previous one was lost our
3106 * peer will get an update, or if it is not time again to reduce we
3107 * still get the cwr to the peer. Note we set the override when we
3108 * could not find the TSN on the chunk or the destination network.
3110 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3114 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3117 * Here we get a CWR from the peer. We must look in the outqueue and
3118 * make sure that we have a covered ECNE in teh control chunk part.
3121 struct sctp_tmit_chunk *chk;
3122 struct sctp_ecne_chunk *ecne;
3126 cwr_tsn = ntohl(cp->tsn);
3128 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3129 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
3130 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3133 if ((override == 0) && (chk->whoTo != net)) {
3134 /* Must be from the right src unless override is set */
3137 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3138 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3139 /* this covers this ECNE, we can remove it */
3140 stcb->asoc.ecn_echo_cnt_onq--;
3141 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3144 sctp_m_freem(chk->data);
3147 stcb->asoc.ctrl_queue_cnt--;
3148 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3149 if (override == 0) {
3157 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3158 struct sctp_tcb *stcb, struct sctp_nets *net)
3160 struct sctp_association *asoc;
3162 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3167 SCTPDBG(SCTP_DEBUG_INPUT2,
3168 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3173 /* process according to association state */
3174 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3175 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3176 SCTPDBG(SCTP_DEBUG_INPUT2,
3177 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3178 SCTP_TCB_UNLOCK(stcb);
3181 /* notify upper layer protocol */
3182 if (stcb->sctp_socket) {
3183 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3184 /* are the queues empty? they should be */
3185 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3186 !TAILQ_EMPTY(&asoc->sent_queue) ||
3187 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
3188 sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED);
3191 /* stop the timer */
3192 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
3193 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3195 SCTPDBG(SCTP_DEBUG_INPUT2,
3196 "sctp_handle_shutdown_complete: calls free-asoc\n");
3197 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3198 so = SCTP_INP_SO(stcb->sctp_ep);
3199 atomic_add_int(&stcb->asoc.refcnt, 1);
3200 SCTP_TCB_UNLOCK(stcb);
3201 SCTP_SOCKET_LOCK(so, 1);
3202 SCTP_TCB_LOCK(stcb);
3203 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3205 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
3206 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3207 SCTP_SOCKET_UNLOCK(so, 1);
3213 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3214 struct sctp_nets *net, uint8_t flg)
3216 switch (desc->chunk_type) {
3218 /* find the tsn to resend (possibly */
3221 struct sctp_tmit_chunk *tp1;
3223 tsn = ntohl(desc->tsn_ifany);
3224 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3225 if (tp1->rec.data.TSN_seq == tsn) {
3229 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) {
3237 * Do it the other way , aka without paying
3238 * attention to queue seq order.
3240 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3241 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3242 if (tp1->rec.data.TSN_seq == tsn) {
3249 SCTP_STAT_INCR(sctps_pdrptsnnf);
3251 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3254 if (((flg & SCTP_BADCRC) == 0) &&
3255 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3258 if ((stcb->asoc.peers_rwnd == 0) &&
3259 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3260 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3263 if (stcb->asoc.peers_rwnd == 0 &&
3264 (flg & SCTP_FROM_MIDDLE_BOX)) {
3265 SCTP_STAT_INCR(sctps_pdrpdizrw);
3268 ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+
3269 sizeof(struct sctp_data_chunk));
3273 for (iii = 0; iii < sizeof(desc->data_bytes);
3275 if (ddp[iii] != desc->data_bytes[iii]) {
3276 SCTP_STAT_INCR(sctps_pdrpbadd);
3284 * this guy had a RTO calculation
3285 * pending on it, cancel it
3287 if (tp1->whoTo->rto_needed == 0) {
3288 tp1->whoTo->rto_needed = 1;
3292 SCTP_STAT_INCR(sctps_pdrpmark);
3293 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3294 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3296 * mark it as if we were doing a FR, since
3297 * we will be getting gap ack reports behind
3298 * the info from the router.
3300 tp1->rec.data.doing_fast_retransmit = 1;
3302 * mark the tsn with what sequences can
3305 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3306 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3308 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
3311 /* restart the timer */
3312 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3313 stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3314 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3317 /* fix counts and things */
3318 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3319 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3320 tp1->whoTo->flight_size,
3323 tp1->rec.data.TSN_seq);
3325 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3326 sctp_flight_size_decrease(tp1);
3327 sctp_total_flight_decrease(stcb, tp1);
3329 tp1->sent = SCTP_DATAGRAM_RESEND;
3335 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3336 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3339 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3341 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3344 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3345 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3346 audit, stcb->asoc.sent_queue_retran_cnt);
3347 #ifndef SCTP_AUDITING_ENABLED
3348 stcb->asoc.sent_queue_retran_cnt = audit;
3356 struct sctp_tmit_chunk *asconf;
3358 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3360 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3365 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3366 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3367 asconf->sent = SCTP_DATAGRAM_RESEND;
3368 asconf->snd_count--;
3372 case SCTP_INITIATION:
3373 /* resend the INIT */
3374 stcb->asoc.dropped_special_cnt++;
3375 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3377 * If we can get it in, in a few attempts we do
3378 * this, otherwise we let the timer fire.
3380 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3381 stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3382 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3385 case SCTP_SELECTIVE_ACK:
3386 case SCTP_NR_SELECTIVE_ACK:
3387 /* resend the sack */
3388 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3390 case SCTP_HEARTBEAT_REQUEST:
3391 /* resend a demand HB */
3392 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3394 * Only retransmit if we KNOW we wont destroy the
3397 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3401 sctp_send_shutdown(stcb, net);
3403 case SCTP_SHUTDOWN_ACK:
3404 sctp_send_shutdown_ack(stcb, net);
3406 case SCTP_COOKIE_ECHO:
3408 struct sctp_tmit_chunk *cookie;
3411 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3413 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3418 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3419 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3420 cookie->sent = SCTP_DATAGRAM_RESEND;
3421 sctp_stop_all_cookie_timers(stcb);
3425 case SCTP_COOKIE_ACK:
3426 sctp_send_cookie_ack(stcb);
3428 case SCTP_ASCONF_ACK:
3429 /* resend last asconf ack */
3430 sctp_send_asconf_ack(stcb);
3432 case SCTP_FORWARD_CUM_TSN:
3433 send_forward_tsn(stcb, &stcb->asoc);
3435 /* can't do anything with these */
3436 case SCTP_PACKET_DROPPED:
3437 case SCTP_INITIATION_ACK: /* this should not happen */
3438 case SCTP_HEARTBEAT_ACK:
3439 case SCTP_ABORT_ASSOCIATION:
3440 case SCTP_OPERATION_ERROR:
3441 case SCTP_SHUTDOWN_COMPLETE:
3451 sctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3457 * We set things to 0xffff since this is the last delivered sequence
3458 * and we will be sending in 0 after the reset.
3461 if (number_entries) {
3462 for (i = 0; i < number_entries; i++) {
3463 temp = ntohs(list[i]);
3464 if (temp >= stcb->asoc.streamincnt) {
3467 stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff;
3471 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3472 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3475 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3479 sctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3483 if (number_entries == 0) {
3484 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3485 stcb->asoc.strmout[i].next_sequence_sent = 0;
3487 } else if (number_entries) {
3488 for (i = 0; i < number_entries; i++) {
3491 temp = ntohs(list[i]);
3492 if (temp >= stcb->asoc.streamoutcnt) {
3493 /* no such stream */
3496 stcb->asoc.strmout[temp].next_sequence_sent = 0;
3499 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3503 struct sctp_stream_reset_out_request *
3504 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3506 struct sctp_association *asoc;
3507 struct sctp_stream_reset_out_req *req;
3508 struct sctp_stream_reset_out_request *r;
3509 struct sctp_tmit_chunk *chk;
3513 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3514 asoc->stream_reset_outstanding = 0;
3517 if (stcb->asoc.str_reset == NULL) {
3518 asoc->stream_reset_outstanding = 0;
3521 chk = stcb->asoc.str_reset;
3522 if (chk->data == NULL) {
3526 /* he wants a copy of the chk pointer */
3529 clen = chk->send_size;
3530 req = mtod(chk->data, struct sctp_stream_reset_out_req *);
3532 if (ntohl(r->request_seq) == seq) {
3536 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3537 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3538 /* move to the next one, there can only be a max of two */
3539 r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len);
3540 if (ntohl(r->request_seq) == seq) {
3544 /* that seq is not here */
3549 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3551 struct sctp_association *asoc;
3552 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3554 if (stcb->asoc.str_reset == NULL) {
3559 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3560 TAILQ_REMOVE(&asoc->control_send_queue,
3564 sctp_m_freem(chk->data);
3567 asoc->ctrl_queue_cnt--;
3568 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3569 /* sa_ignore NO_NULL_CHK */
3570 stcb->asoc.str_reset = NULL;
3575 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3576 uint32_t seq, uint32_t action,
3577 struct sctp_stream_reset_response *respin)
3581 struct sctp_association *asoc = &stcb->asoc;
3582 struct sctp_tmit_chunk *chk;
3583 struct sctp_stream_reset_out_request *srparam;
3586 if (asoc->stream_reset_outstanding == 0) {
3590 if (seq == stcb->asoc.str_reset_seq_out) {
3591 srparam = sctp_find_stream_reset(stcb, seq, &chk);
3593 stcb->asoc.str_reset_seq_out++;
3594 type = ntohs(srparam->ph.param_type);
3595 lparm_len = ntohs(srparam->ph.param_length);
3596 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3597 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3598 asoc->stream_reset_out_is_outstanding = 0;
3599 if (asoc->stream_reset_outstanding)
3600 asoc->stream_reset_outstanding--;
3601 if (action == SCTP_STREAM_RESET_PERFORMED) {
3603 sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams);
3605 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3607 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3608 /* Answered my request */
3609 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3610 if (asoc->stream_reset_outstanding)
3611 asoc->stream_reset_outstanding--;
3612 if (action != SCTP_STREAM_RESET_PERFORMED) {
3613 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3615 } else if (type == SCTP_STR_RESET_ADD_STREAMS) {
3616 /* Ok we now may have more streams */
3617 if (asoc->stream_reset_outstanding)
3618 asoc->stream_reset_outstanding--;
3619 if (action == SCTP_STREAM_RESET_PERFORMED) {
3620 /* Put the new streams into effect */
3621 stcb->asoc.streamoutcnt = stcb->asoc.strm_realoutsize;
3622 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_OK, stcb,
3623 (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED);
3625 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_FAIL, stcb,
3626 (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED);
3628 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3630 * a) Adopt the new in tsn.
3632 * c) Adopt the new out-tsn
3634 struct sctp_stream_reset_response_tsn *resp;
3635 struct sctp_forward_tsn_chunk fwdtsn;
3638 if (respin == NULL) {
3642 if (action == SCTP_STREAM_RESET_PERFORMED) {
3643 resp = (struct sctp_stream_reset_response_tsn *)respin;
3644 asoc->stream_reset_outstanding--;
3645 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3646 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3647 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3648 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3652 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3653 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3654 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3656 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3657 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3658 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3660 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3661 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3663 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3664 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3666 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3667 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3671 /* get rid of the request and get the request flags */
3672 if (asoc->stream_reset_outstanding == 0) {
3673 sctp_clean_up_stream_reset(stcb);
3681 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3682 struct sctp_tmit_chunk *chk,
3683 struct sctp_stream_reset_in_request *req, int trunc)
3691 * peer wants me to send a str-reset to him for my outgoing seq's if
3694 struct sctp_association *asoc = &stcb->asoc;
3696 seq = ntohl(req->request_seq);
3697 if (asoc->str_reset_seq_in == seq) {
3699 /* Can't do it, since they exceeded our buffer size */
3700 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3701 asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3702 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3703 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3704 len = ntohs(req->ph.param_length);
3705 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3706 for (i = 0; i < number_entries; i++) {
3707 temp = ntohs(req->list_of_streams[i]);
3708 req->list_of_streams[i] = temp;
3710 /* move the reset action back one */
3711 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3712 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3713 sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams,
3714 asoc->str_reset_seq_out,
3715 seq, (asoc->sending_seq - 1));
3716 asoc->stream_reset_out_is_outstanding = 1;
3717 asoc->str_reset = chk;
3718 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
3719 stcb->asoc.stream_reset_outstanding++;
3721 /* Can't do it, since we have sent one out */
3722 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3723 asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER;
3724 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3726 asoc->str_reset_seq_in++;
3727 } else if (asoc->str_reset_seq_in - 1 == seq) {
3728 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3729 } else if (asoc->str_reset_seq_in - 2 == seq) {
3730 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3732 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3737 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3738 struct sctp_tmit_chunk *chk,
3739 struct sctp_stream_reset_tsn_request *req)
3741 /* reset all in and out and update the tsn */
3743 * A) reset my str-seq's on in and out. B) Select a receive next,
3744 * and set cum-ack to it. Also process this selected number as a
3745 * fwd-tsn as well. C) set in the response my next sending seq.
3747 struct sctp_forward_tsn_chunk fwdtsn;
3748 struct sctp_association *asoc = &stcb->asoc;
3752 seq = ntohl(req->request_seq);
3753 if (asoc->str_reset_seq_in == seq) {
3754 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3755 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3756 fwdtsn.ch.chunk_flags = 0;
3757 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3758 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3762 stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3763 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3764 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3766 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3767 stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1;
3768 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3769 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3770 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3771 atomic_add_int(&stcb->asoc.sending_seq, 1);
3772 /* save off historical data for retrans */
3773 stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0];
3774 stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq;
3775 stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0];
3776 stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn;
3778 sctp_add_stream_reset_result_tsn(chk,
3779 ntohl(req->request_seq),
3780 SCTP_STREAM_RESET_PERFORMED,
3781 stcb->asoc.sending_seq,
3782 stcb->asoc.mapping_array_base_tsn);
3783 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3784 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3785 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3786 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3788 asoc->str_reset_seq_in++;
3789 } else if (asoc->str_reset_seq_in - 1 == seq) {
3790 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3791 stcb->asoc.last_sending_seq[0],
3792 stcb->asoc.last_base_tsnsent[0]
3794 } else if (asoc->str_reset_seq_in - 2 == seq) {
3795 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3796 stcb->asoc.last_sending_seq[1],
3797 stcb->asoc.last_base_tsnsent[1]
3800 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3806 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3807 struct sctp_tmit_chunk *chk,
3808 struct sctp_stream_reset_out_request *req, int trunc)
3811 int number_entries, len;
3812 struct sctp_association *asoc = &stcb->asoc;
3814 seq = ntohl(req->request_seq);
3816 /* now if its not a duplicate we process it */
3817 if (asoc->str_reset_seq_in == seq) {
3818 len = ntohs(req->ph.param_length);
3819 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3821 * the sender is resetting, handle the list issue.. we must
3822 * a) verify if we can do the reset, if so no problem b) If
3823 * we can't do the reset we must copy the request. c) queue
3824 * it, and setup the data in processor to trigger it off
3825 * when needed and dequeue all the queued data.
3827 tsn = ntohl(req->send_reset_at_tsn);
3829 /* move the reset action back one */
3830 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3832 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED);
3833 asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3834 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3835 /* we can do it now */
3836 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3837 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3838 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3841 * we must queue it up and thus wait for the TSN's
3842 * to arrive that are at or before tsn
3844 struct sctp_stream_reset_list *liste;
3847 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3848 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3849 siz, SCTP_M_STRESET);
3850 if (liste == NULL) {
3851 /* gak out of memory */
3852 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED);
3853 asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3857 liste->number_entries = number_entries;
3858 memcpy(&liste->req, req,
3859 (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t))));
3860 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3861 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3862 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3864 asoc->str_reset_seq_in++;
3865 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3867 * one seq back, just echo back last action since my
3868 * response was lost.
3870 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3871 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3873 * two seq back, just echo back last action since my
3874 * response was lost.
3876 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3878 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3883 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3884 struct sctp_stream_reset_add_strm *str_add)
3887 * Peer is requesting to add more streams. If its within our
3888 * max-streams we will allow it.
3890 uint16_t num_stream, i;
3892 struct sctp_association *asoc = &stcb->asoc;
3893 struct sctp_queued_to_read *ctl, *nctl;
3895 /* Get the number. */
3896 seq = ntohl(str_add->request_seq);
3897 num_stream = ntohs(str_add->number_of_streams);
3898 /* Now what would be the new total? */
3899 if (asoc->str_reset_seq_in == seq) {
3900 num_stream += stcb->asoc.streamincnt;
3901 if (num_stream > stcb->asoc.max_inbound_streams) {
3902 /* We must reject it they ask for to many */
3904 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED);
3905 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3906 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3908 /* Ok, we can do that :-) */
3909 struct sctp_stream_in *oldstrm;
3911 /* save off the old */
3912 oldstrm = stcb->asoc.strmin;
3913 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
3914 (num_stream * sizeof(struct sctp_stream_in)),
3916 if (stcb->asoc.strmin == NULL) {
3917 stcb->asoc.strmin = oldstrm;
3920 /* copy off the old data */
3921 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3922 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3923 stcb->asoc.strmin[i].stream_no = i;
3924 stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered;
3925 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
3926 /* now anything on those queues? */
3927 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) {
3928 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next);
3929 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next);
3932 /* Init the new streams */
3933 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
3934 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3935 stcb->asoc.strmin[i].stream_no = i;
3936 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3937 stcb->asoc.strmin[i].delivery_started = 0;
3939 SCTP_FREE(oldstrm, SCTP_M_STRMI);
3940 /* update the size */
3941 stcb->asoc.streamincnt = num_stream;
3943 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3944 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3945 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3946 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_INSTREAM_ADD_OK, stcb,
3947 (uint32_t) stcb->asoc.streamincnt, NULL, SCTP_SO_NOT_LOCKED);
3949 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3951 * one seq back, just echo back last action since my
3952 * response was lost.
3954 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3955 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3957 * two seq back, just echo back last action since my
3958 * response was lost.
3960 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3962 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3968 __attribute__((noinline))
3971 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3972 struct sctp_stream_reset_out_req *sr_req)
3974 int chk_length, param_len, ptype;
3975 struct sctp_paramhdr pstore;
3976 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
3981 struct sctp_tmit_chunk *chk;
3982 struct sctp_chunkhdr *ch;
3983 struct sctp_paramhdr *ph;
3987 /* now it may be a reset or a reset-response */
3988 chk_length = ntohs(sr_req->ch.chunk_length);
3990 /* setup for adding the response */
3991 sctp_alloc_a_chunk(stcb, chk);
3995 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
3996 chk->rec.chunk_id.can_take_data = 0;
3997 chk->asoc = &stcb->asoc;
3998 chk->no_fr_allowed = 0;
3999 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4000 chk->book_size_scale = 0;
4001 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4002 if (chk->data == NULL) {
4005 sctp_m_freem(chk->data);
4008 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4011 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4013 /* setup chunk parameters */
4014 chk->sent = SCTP_DATAGRAM_UNSENT;
4018 ch = mtod(chk->data, struct sctp_chunkhdr *);
4019 ch->chunk_type = SCTP_STREAM_RESET;
4020 ch->chunk_flags = 0;
4021 ch->chunk_length = htons(chk->send_size);
4022 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4023 offset += sizeof(struct sctp_chunkhdr);
4024 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) {
4025 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore);
4028 param_len = ntohs(ph->param_length);
4029 if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) {
4033 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)),
4034 (uint8_t *) & cstore);
4035 ptype = ntohs(ph->param_type);
4037 if (param_len > (int)sizeof(cstore)) {
4043 if (num_param > SCTP_MAX_RESET_PARAMS) {
4044 /* hit the max of parameters already sorry.. */
4047 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4048 struct sctp_stream_reset_out_request *req_out;
4050 req_out = (struct sctp_stream_reset_out_request *)ph;
4052 if (stcb->asoc.stream_reset_outstanding) {
4053 seq = ntohl(req_out->response_seq);
4054 if (seq == stcb->asoc.str_reset_seq_out) {
4056 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL);
4059 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4060 } else if (ptype == SCTP_STR_RESET_ADD_STREAMS) {
4061 struct sctp_stream_reset_add_strm *str_add;
4063 str_add = (struct sctp_stream_reset_add_strm *)ph;
4065 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4066 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4067 struct sctp_stream_reset_in_request *req_in;
4071 req_in = (struct sctp_stream_reset_in_request *)ph;
4073 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4074 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4075 struct sctp_stream_reset_tsn_request *req_tsn;
4078 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4080 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4082 goto strres_nochunk;
4086 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4087 struct sctp_stream_reset_response *resp;
4090 resp = (struct sctp_stream_reset_response *)ph;
4091 seq = ntohl(resp->response_seq);
4092 result = ntohl(resp->result);
4093 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4095 goto strres_nochunk;
4100 offset += SCTP_SIZE32(param_len);
4101 chk_length -= SCTP_SIZE32(param_len);
4104 /* we have no response free the stuff */
4105 goto strres_nochunk;
4107 /* ok we have a chunk to link in */
4108 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4111 stcb->asoc.ctrl_queue_cnt++;
4116 * Handle a router or endpoints report of a packet loss, there are two ways
4117 * to handle this, either we get the whole packet and must disect it
4118 * ourselves (possibly with truncation and or corruption) or it is a summary
4119 * from a middle box that did the disectting for us.
4122 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4123 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4125 uint32_t bottle_bw, on_queue;
4129 struct sctp_chunk_desc desc;
4130 struct sctp_chunkhdr *ch;
4132 chlen = ntohs(cp->ch.chunk_length);
4133 chlen -= sizeof(struct sctp_pktdrop_chunk);
4134 /* XXX possible chlen underflow */
4137 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4138 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4140 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4141 chlen -= sizeof(struct sctphdr);
4142 /* XXX possible chlen underflow */
4143 memset(&desc, 0, sizeof(desc));
4145 trunc_len = (uint16_t) ntohs(cp->trunc_len);
4146 if (trunc_len > limit) {
4149 /* now the chunks themselves */
4150 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4151 desc.chunk_type = ch->chunk_type;
4152 /* get amount we need to move */
4153 at = ntohs(ch->chunk_length);
4154 if (at < sizeof(struct sctp_chunkhdr)) {
4155 /* corrupt chunk, maybe at the end? */
4156 SCTP_STAT_INCR(sctps_pdrpcrupt);
4159 if (trunc_len == 0) {
4160 /* we are supposed to have all of it */
4162 /* corrupt skip it */
4163 SCTP_STAT_INCR(sctps_pdrpcrupt);
4167 /* is there enough of it left ? */
4168 if (desc.chunk_type == SCTP_DATA) {
4169 if (chlen < (sizeof(struct sctp_data_chunk) +
4170 sizeof(desc.data_bytes))) {
4174 if (chlen < sizeof(struct sctp_chunkhdr)) {
4179 if (desc.chunk_type == SCTP_DATA) {
4180 /* can we get out the tsn? */
4181 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4182 SCTP_STAT_INCR(sctps_pdrpmbda);
4184 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4186 struct sctp_data_chunk *dcp;
4190 dcp = (struct sctp_data_chunk *)ch;
4191 ddp = (uint8_t *) (dcp + 1);
4192 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4193 desc.data_bytes[iii] = ddp[iii];
4195 desc.tsn_ifany = dcp->dp.tsn;
4197 /* nope we are done. */
4198 SCTP_STAT_INCR(sctps_pdrpnedat);
4202 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4203 SCTP_STAT_INCR(sctps_pdrpmbct);
4206 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4207 SCTP_STAT_INCR(sctps_pdrppdbrk);
4210 if (SCTP_SIZE32(at) > chlen) {
4213 chlen -= SCTP_SIZE32(at);
4214 if (chlen < sizeof(struct sctp_chunkhdr)) {
4215 /* done, none left */
4218 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4220 /* Now update any rwnd --- possibly */
4221 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4222 /* From a peer, we get a rwnd report */
4225 SCTP_STAT_INCR(sctps_pdrpfehos);
4227 bottle_bw = ntohl(cp->bottle_bw);
4228 on_queue = ntohl(cp->current_onq);
4229 if (bottle_bw && on_queue) {
4230 /* a rwnd report is in here */
4231 if (bottle_bw > on_queue)
4232 a_rwnd = bottle_bw - on_queue;
4237 stcb->asoc.peers_rwnd = 0;
4239 if (a_rwnd > stcb->asoc.total_flight) {
4240 stcb->asoc.peers_rwnd =
4241 a_rwnd - stcb->asoc.total_flight;
4243 stcb->asoc.peers_rwnd = 0;
4245 if (stcb->asoc.peers_rwnd <
4246 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4247 /* SWS sender side engages */
4248 stcb->asoc.peers_rwnd = 0;
4253 SCTP_STAT_INCR(sctps_pdrpfmbox);
4256 /* now middle boxes in sat networks get a cwnd bump */
4257 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4258 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4259 (stcb->asoc.sat_network)) {
4261 * This is debateable but for sat networks it makes sense
4262 * Note if a T3 timer has went off, we will prohibit any
4263 * changes to cwnd until we exit the t3 loss recovery.
4265 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4266 net, cp, &bottle_bw, &on_queue);
4271 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4272 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4273 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4274 * length of the complete packet outputs: - length: modified to remaining
4275 * length after control processing - netp: modified to new sctp_nets after
4276 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4277 * bad packet,...) otherwise return the tcb for this packet
4280 __attribute__((noinline))
4282 static struct sctp_tcb *
4283 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4284 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4285 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4286 uint32_t vrf_id, uint16_t port)
4288 struct sctp_association *asoc;
4290 int num_chunks = 0; /* number of control chunks processed */
4291 uint32_t chk_length;
4293 int abort_no_unlock = 0;
4297 * How big should this be, and should it be alloc'd? Lets try the
4298 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4299 * until we get into jumbo grams and such..
4301 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4302 struct sctp_tcb *locked_tcb = stcb;
4304 uint32_t auth_offset = 0, auth_len = 0;
4305 int auth_skipped = 0;
4308 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4313 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4314 iphlen, *offset, length, stcb);
4316 /* validate chunk header length... */
4317 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4318 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4319 ntohs(ch->chunk_length));
4321 SCTP_TCB_UNLOCK(locked_tcb);
4326 * validate the verification tag
4328 vtag_in = ntohl(sh->v_tag);
4331 SCTP_TCB_LOCK_ASSERT(locked_tcb);
4333 if (ch->chunk_type == SCTP_INITIATION) {
4334 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4335 ntohs(ch->chunk_length), vtag_in);
4337 /* protocol error- silently discard... */
4338 SCTP_STAT_INCR(sctps_badvtag);
4340 SCTP_TCB_UNLOCK(locked_tcb);
4344 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4346 * If there is no stcb, skip the AUTH chunk and process
4347 * later after a stcb is found (to validate the lookup was
4350 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4352 !SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4353 /* save this chunk for later processing */
4355 auth_offset = *offset;
4356 auth_len = ntohs(ch->chunk_length);
4358 /* (temporarily) move past this chunk */
4359 *offset += SCTP_SIZE32(auth_len);
4360 if (*offset >= length) {
4361 /* no more data left in the mbuf chain */
4364 SCTP_TCB_UNLOCK(locked_tcb);
4368 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4369 sizeof(struct sctp_chunkhdr), chunk_buf);
4375 SCTP_TCB_UNLOCK(locked_tcb);
4379 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4380 goto process_control_chunks;
4383 * first check if it's an ASCONF with an unknown src addr we
4384 * need to look inside to find the association
4386 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4387 struct sctp_chunkhdr *asconf_ch = ch;
4388 uint32_t asconf_offset = 0, asconf_len = 0;
4390 /* inp's refcount may be reduced */
4391 SCTP_INP_INCR_REF(inp);
4393 asconf_offset = *offset;
4395 asconf_len = ntohs(asconf_ch->chunk_length);
4396 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4398 stcb = sctp_findassociation_ep_asconf(m,
4399 *offset, sh, &inp, netp, vrf_id);
4402 asconf_offset += SCTP_SIZE32(asconf_len);
4403 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4404 sizeof(struct sctp_chunkhdr), chunk_buf);
4405 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4408 * reduce inp's refcount if not reduced in
4409 * sctp_findassociation_ep_asconf().
4411 SCTP_INP_DECR_REF(inp);
4416 /* now go back and verify any auth chunk to be sure */
4417 if (auth_skipped && (stcb != NULL)) {
4418 struct sctp_auth_chunk *auth;
4420 auth = (struct sctp_auth_chunk *)
4421 sctp_m_getptr(m, auth_offset,
4422 auth_len, chunk_buf);
4425 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4427 /* auth HMAC failed so dump it */
4430 SCTP_TCB_UNLOCK(locked_tcb);
4434 /* remaining chunks are HMAC checked */
4435 stcb->asoc.authenticated = 1;
4440 /* no association, so it's out of the blue... */
4441 sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL,
4445 SCTP_TCB_UNLOCK(locked_tcb);
4450 /* ABORT and SHUTDOWN can use either v_tag... */
4451 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4452 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4453 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4454 if ((vtag_in == asoc->my_vtag) ||
4455 ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
4456 (vtag_in == asoc->peer_vtag))) {
4459 /* drop this packet... */
4460 SCTP_STAT_INCR(sctps_badvtag);
4462 SCTP_TCB_UNLOCK(locked_tcb);
4466 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4467 if (vtag_in != asoc->my_vtag) {
4469 * this could be a stale SHUTDOWN-ACK or the
4470 * peer never got the SHUTDOWN-COMPLETE and
4471 * is still hung; we have started a new asoc
4472 * but it won't complete until the shutdown
4476 SCTP_TCB_UNLOCK(locked_tcb);
4478 sctp_handle_ootb(m, iphlen, *offset, sh, inp,
4479 NULL, vrf_id, port);
4483 /* for all other chunks, vtag must match */
4484 if (vtag_in != asoc->my_vtag) {
4485 /* invalid vtag... */
4486 SCTPDBG(SCTP_DEBUG_INPUT3,
4487 "invalid vtag: %xh, expect %xh\n",
4488 vtag_in, asoc->my_vtag);
4489 SCTP_STAT_INCR(sctps_badvtag);
4491 SCTP_TCB_UNLOCK(locked_tcb);
4497 } /* end if !SCTP_COOKIE_ECHO */
4499 * process all control chunks...
4501 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4503 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4504 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4505 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4506 /* implied cookie-ack.. we must have lost the ack */
4507 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4508 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4509 stcb->asoc.overall_error_count,
4511 SCTP_FROM_SCTP_INPUT,
4514 stcb->asoc.overall_error_count = 0;
4515 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4518 process_control_chunks:
4519 while (IS_SCTP_CONTROL(ch)) {
4520 /* validate chunk length */
4521 chk_length = ntohs(ch->chunk_length);
4522 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4523 ch->chunk_type, chk_length);
4524 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4525 if (chk_length < sizeof(*ch) ||
4526 (*offset + (int)chk_length) > length) {
4529 SCTP_TCB_UNLOCK(locked_tcb);
4533 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4535 * INIT-ACK only gets the init ack "header" portion only
4536 * because we don't have to process the peer's COOKIE. All
4537 * others get a complete chunk.
4539 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4540 (ch->chunk_type == SCTP_INITIATION)) {
4541 /* get an init-ack chunk */
4542 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4543 sizeof(struct sctp_init_ack_chunk), chunk_buf);
4547 SCTP_TCB_UNLOCK(locked_tcb);
4552 /* For cookies and all other chunks. */
4553 if (chk_length > sizeof(chunk_buf)) {
4555 * use just the size of the chunk buffer so
4556 * the front part of our chunks fit in
4557 * contiguous space up to the chunk buffer
4558 * size (508 bytes). For chunks that need to
4559 * get more than that they must use the
4560 * sctp_m_getptr() function or other means
4561 * (e.g. know how to parse mbuf chains).
4562 * Cookies do this already.
4564 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4565 (sizeof(chunk_buf) - 4),
4570 SCTP_TCB_UNLOCK(locked_tcb);
4575 /* We can fit it all */
4576 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4577 chk_length, chunk_buf);
4579 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4582 SCTP_TCB_UNLOCK(locked_tcb);
4589 /* Save off the last place we got a control from */
4591 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4593 * allow last_control to be NULL if
4594 * ASCONF... ASCONF processing will find the
4597 if ((netp != NULL) && (*netp != NULL))
4598 stcb->asoc.last_control_chunk_from = *netp;
4601 #ifdef SCTP_AUDITING_ENABLED
4602 sctp_audit_log(0xB0, ch->chunk_type);
4605 /* check to see if this chunk required auth, but isn't */
4606 if ((stcb != NULL) &&
4607 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
4608 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4609 !stcb->asoc.authenticated) {
4610 /* "silently" ignore */
4611 SCTP_STAT_INCR(sctps_recvauthmissing);
4614 switch (ch->chunk_type) {
4615 case SCTP_INITIATION:
4616 /* must be first and only chunk */
4617 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4618 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4619 /* We are not interested anymore? */
4620 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4622 * collision case where we are
4623 * sending to them too
4628 SCTP_TCB_UNLOCK(locked_tcb);
4634 if ((chk_length > SCTP_LARGEST_INIT_ACCEPTED) ||
4636 (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) {
4639 SCTP_TCB_UNLOCK(locked_tcb);
4643 if ((stcb != NULL) &&
4644 (SCTP_GET_STATE(&stcb->asoc) ==
4645 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
4646 sctp_send_shutdown_ack(stcb, NULL);
4648 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4650 SCTP_TCB_UNLOCK(locked_tcb);
4655 sctp_handle_init(m, iphlen, *offset, sh,
4656 (struct sctp_init_chunk *)ch, inp,
4657 stcb, &abort_no_unlock, vrf_id, port);
4659 if (abort_no_unlock)
4664 SCTP_TCB_UNLOCK(locked_tcb);
4668 case SCTP_PAD_CHUNK:
4670 case SCTP_INITIATION_ACK:
4671 /* must be first and only chunk */
4672 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4673 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4674 /* We are not interested anymore */
4675 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4678 if (locked_tcb != stcb) {
4680 SCTP_TCB_UNLOCK(locked_tcb);
4684 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4685 so = SCTP_INP_SO(inp);
4686 atomic_add_int(&stcb->asoc.refcnt, 1);
4687 SCTP_TCB_UNLOCK(stcb);
4688 SCTP_SOCKET_LOCK(so, 1);
4689 SCTP_TCB_LOCK(stcb);
4690 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4692 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4693 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4694 SCTP_SOCKET_UNLOCK(so, 1);
4700 if ((num_chunks > 1) ||
4701 (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) {
4704 SCTP_TCB_UNLOCK(locked_tcb);
4708 if ((netp) && (*netp)) {
4709 ret = sctp_handle_init_ack(m, iphlen, *offset, sh,
4710 (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id);
4715 * Special case, I must call the output routine to
4716 * get the cookie echoed
4718 if (abort_no_unlock)
4721 if ((stcb) && ret == 0)
4722 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4725 SCTP_TCB_UNLOCK(locked_tcb);
4729 case SCTP_SELECTIVE_ACK:
4731 struct sctp_sack_chunk *sack;
4733 uint32_t a_rwnd, cum_ack;
4734 uint16_t num_seg, num_dup;
4736 int offset_seg, offset_dup;
4738 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4739 SCTP_STAT_INCR(sctps_recvsacks);
4741 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4744 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4745 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4748 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4750 * If we have sent a shutdown-ack, we will pay no
4751 * attention to a sack sent in to us since
4752 * we don't care anymore.
4756 sack = (struct sctp_sack_chunk *)ch;
4757 flags = ch->chunk_flags;
4758 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4759 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4760 num_dup = ntohs(sack->sack.num_dup_tsns);
4761 a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
4762 if (sizeof(struct sctp_sack_chunk) +
4763 num_seg * sizeof(struct sctp_gap_ack_block) +
4764 num_dup * sizeof(uint32_t) != chk_length) {
4765 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4768 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4769 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4770 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4771 cum_ack, num_seg, a_rwnd);
4772 stcb->asoc.seen_a_sack_this_pkt = 1;
4773 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4775 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4776 (stcb->asoc.saw_sack_with_frags == 0) &&
4777 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4778 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4781 * We have a SIMPLE sack having no
4782 * prior segments and data on sent
4783 * queue to be acked.. Use the
4784 * faster path sack processing. We
4785 * also allow window update sacks
4786 * with no missing segments to go
4789 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
4792 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4793 num_seg, 0, num_dup, &abort_now, flags,
4794 cum_ack, a_rwnd, ecne_seen);
4797 /* ABORT signal from sack processing */
4801 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4802 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4803 (stcb->asoc.stream_queue_cnt == 0)) {
4804 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4809 * EY - nr_sack: If the received chunk is an
4812 case SCTP_NR_SELECTIVE_ACK:
4814 struct sctp_nr_sack_chunk *nr_sack;
4816 uint32_t a_rwnd, cum_ack;
4817 uint16_t num_seg, num_nr_seg, num_dup;
4819 int offset_seg, offset_dup;
4821 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
4822 SCTP_STAT_INCR(sctps_recvsacks);
4824 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
4827 if ((stcb->asoc.sctp_nr_sack_on_off == 0) ||
4828 (stcb->asoc.peer_supports_nr_sack == 0)) {
4831 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4832 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
4835 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4837 * If we have sent a shutdown-ack, we will pay no
4838 * attention to a sack sent in to us since
4839 * we don't care anymore.
4843 nr_sack = (struct sctp_nr_sack_chunk *)ch;
4844 flags = ch->chunk_flags;
4845 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4846 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4847 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4848 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4849 a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd);
4850 if (sizeof(struct sctp_nr_sack_chunk) +
4851 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4852 num_dup * sizeof(uint32_t) != chk_length) {
4853 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4856 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4857 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4858 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4859 cum_ack, num_seg, a_rwnd);
4860 stcb->asoc.seen_a_sack_this_pkt = 1;
4861 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4862 (num_seg == 0) && (num_nr_seg == 0) &&
4863 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4864 (stcb->asoc.saw_sack_with_frags == 0) &&
4865 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4866 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
4868 * We have a SIMPLE sack having no
4869 * prior segments and data on sent
4870 * queue to be acked. Use the faster
4871 * path sack processing. We also
4872 * allow window update sacks with no
4873 * missing segments to go this way
4876 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
4877 &abort_now, ecne_seen);
4880 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4881 num_seg, num_nr_seg, num_dup, &abort_now, flags,
4882 cum_ack, a_rwnd, ecne_seen);
4885 /* ABORT signal from sack processing */
4889 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4890 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4891 (stcb->asoc.stream_queue_cnt == 0)) {
4892 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4897 case SCTP_HEARTBEAT_REQUEST:
4898 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
4899 if ((stcb) && netp && *netp) {
4900 SCTP_STAT_INCR(sctps_recvheartbeat);
4901 sctp_send_heartbeat_ack(stcb, m, *offset,
4904 /* He's alive so give him credit */
4905 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4906 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4907 stcb->asoc.overall_error_count,
4909 SCTP_FROM_SCTP_INPUT,
4912 stcb->asoc.overall_error_count = 0;
4915 case SCTP_HEARTBEAT_ACK:
4916 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
4917 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
4921 SCTP_TCB_UNLOCK(locked_tcb);
4925 /* He's alive so give him credit */
4926 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4927 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4928 stcb->asoc.overall_error_count,
4930 SCTP_FROM_SCTP_INPUT,
4933 stcb->asoc.overall_error_count = 0;
4934 SCTP_STAT_INCR(sctps_recvheartbeatack);
4936 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
4939 case SCTP_ABORT_ASSOCIATION:
4940 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
4942 if ((stcb) && netp && *netp)
4943 sctp_handle_abort((struct sctp_abort_chunk *)ch,
4949 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
4951 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
4954 SCTP_TCB_UNLOCK(locked_tcb);
4958 if (netp && *netp) {
4961 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
4962 stcb, *netp, &abort_flag);
4969 case SCTP_SHUTDOWN_ACK:
4970 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb);
4971 if ((stcb) && (netp) && (*netp))
4972 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
4977 case SCTP_OPERATION_ERROR:
4978 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
4979 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
4985 case SCTP_COOKIE_ECHO:
4986 SCTPDBG(SCTP_DEBUG_INPUT3,
4987 "SCTP_COOKIE-ECHO, stcb %p\n", stcb);
4988 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4991 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4992 /* We are not interested anymore */
4995 SCTP_TCB_UNLOCK(stcb);
5002 * First are we accepting? We do this again here
5003 * since it is possible that a previous endpoint WAS
5004 * listening responded to a INIT-ACK and then
5005 * closed. We opened and bound.. and are now no
5009 if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
5010 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5011 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5013 struct sctp_paramhdr *phdr;
5015 oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
5016 0, M_DONTWAIT, 1, MT_DATA);
5018 SCTP_BUF_LEN(oper) =
5019 sizeof(struct sctp_paramhdr);
5021 struct sctp_paramhdr *);
5023 htons(SCTP_CAUSE_OUT_OF_RESC);
5024 phdr->param_length =
5025 htons(sizeof(struct sctp_paramhdr));
5027 sctp_abort_association(inp, stcb, m,
5028 iphlen, sh, oper, vrf_id, port);
5033 struct mbuf *ret_buf;
5034 struct sctp_inpcb *linp;
5043 SCTP_ASOC_CREATE_LOCK(linp);
5044 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5045 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5046 SCTP_ASOC_CREATE_UNLOCK(linp);
5052 sctp_handle_cookie_echo(m, iphlen,
5054 (struct sctp_cookie_echo_chunk *)ch,
5066 SCTP_ASOC_CREATE_UNLOCK(linp);
5068 if (ret_buf == NULL) {
5070 SCTP_TCB_UNLOCK(locked_tcb);
5072 SCTPDBG(SCTP_DEBUG_INPUT3,
5073 "GAK, null buffer\n");
5077 /* if AUTH skipped, see if it verified... */
5082 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5084 * Restart the timer if we have
5087 struct sctp_tmit_chunk *chk;
5089 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5090 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5094 case SCTP_COOKIE_ACK:
5095 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb);
5096 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5098 SCTP_TCB_UNLOCK(locked_tcb);
5102 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5103 /* We are not interested anymore */
5104 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5107 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5108 so = SCTP_INP_SO(inp);
5109 atomic_add_int(&stcb->asoc.refcnt, 1);
5110 SCTP_TCB_UNLOCK(stcb);
5111 SCTP_SOCKET_LOCK(so, 1);
5112 SCTP_TCB_LOCK(stcb);
5113 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5115 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
5116 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5117 SCTP_SOCKET_UNLOCK(so, 1);
5123 /* He's alive so give him credit */
5124 if ((stcb) && netp && *netp) {
5125 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5126 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5127 stcb->asoc.overall_error_count,
5129 SCTP_FROM_SCTP_INPUT,
5132 stcb->asoc.overall_error_count = 0;
5133 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5137 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5138 /* He's alive so give him credit */
5139 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5142 SCTP_TCB_UNLOCK(locked_tcb);
5148 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5149 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5150 stcb->asoc.overall_error_count,
5152 SCTP_FROM_SCTP_INPUT,
5155 stcb->asoc.overall_error_count = 0;
5156 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5162 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5163 /* He's alive so give him credit */
5164 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5167 SCTP_TCB_UNLOCK(locked_tcb);
5173 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5174 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5175 stcb->asoc.overall_error_count,
5177 SCTP_FROM_SCTP_INPUT,
5180 stcb->asoc.overall_error_count = 0;
5181 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5184 case SCTP_SHUTDOWN_COMPLETE:
5185 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb);
5186 /* must be first and only chunk */
5187 if ((num_chunks > 1) ||
5188 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5191 SCTP_TCB_UNLOCK(locked_tcb);
5195 if ((stcb) && netp && *netp) {
5196 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5203 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5204 /* He's alive so give him credit */
5206 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5207 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5208 stcb->asoc.overall_error_count,
5210 SCTP_FROM_SCTP_INPUT,
5213 stcb->asoc.overall_error_count = 0;
5214 sctp_handle_asconf(m, *offset,
5215 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5219 case SCTP_ASCONF_ACK:
5220 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5221 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5224 SCTP_TCB_UNLOCK(locked_tcb);
5229 if ((stcb) && netp && *netp) {
5230 /* He's alive so give him credit */
5231 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5232 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5233 stcb->asoc.overall_error_count,
5235 SCTP_FROM_SCTP_INPUT,
5238 stcb->asoc.overall_error_count = 0;
5239 sctp_handle_asconf_ack(m, *offset,
5240 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5241 if (abort_no_unlock)
5245 case SCTP_FORWARD_CUM_TSN:
5246 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5247 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5250 SCTP_TCB_UNLOCK(locked_tcb);
5255 /* He's alive so give him credit */
5259 stcb->asoc.overall_error_count = 0;
5260 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5261 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5262 stcb->asoc.overall_error_count,
5264 SCTP_FROM_SCTP_INPUT,
5268 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5269 /* We are not interested anymore */
5270 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5271 so = SCTP_INP_SO(inp);
5272 atomic_add_int(&stcb->asoc.refcnt, 1);
5273 SCTP_TCB_UNLOCK(stcb);
5274 SCTP_SOCKET_LOCK(so, 1);
5275 SCTP_TCB_LOCK(stcb);
5276 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5278 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
5279 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5280 SCTP_SOCKET_UNLOCK(so, 1);
5285 sctp_handle_forward_tsn(stcb,
5286 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5291 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5292 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5293 stcb->asoc.overall_error_count,
5295 SCTP_FROM_SCTP_INPUT,
5298 stcb->asoc.overall_error_count = 0;
5303 case SCTP_STREAM_RESET:
5304 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5305 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5308 SCTP_TCB_UNLOCK(locked_tcb);
5313 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5314 /* We are not interested anymore */
5315 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5316 so = SCTP_INP_SO(inp);
5317 atomic_add_int(&stcb->asoc.refcnt, 1);
5318 SCTP_TCB_UNLOCK(stcb);
5319 SCTP_SOCKET_LOCK(so, 1);
5320 SCTP_TCB_LOCK(stcb);
5321 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5323 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5324 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5325 SCTP_SOCKET_UNLOCK(so, 1);
5330 if (stcb->asoc.peer_supports_strreset == 0) {
5332 * hmm, peer should have announced this, but
5333 * we will turn it on since he is sending us
5336 stcb->asoc.peer_supports_strreset = 1;
5338 if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) {
5339 /* stop processing */
5344 case SCTP_PACKET_DROPPED:
5345 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5346 /* re-get it all please */
5347 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5350 SCTP_TCB_UNLOCK(locked_tcb);
5355 if (ch && (stcb) && netp && (*netp)) {
5356 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5358 min(chk_length, (sizeof(chunk_buf) - 4)));
5363 case SCTP_AUTHENTICATION:
5364 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5365 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
5369 /* save the first AUTH for later processing */
5370 if (auth_skipped == 0) {
5371 auth_offset = *offset;
5372 auth_len = chk_length;
5375 /* skip this chunk (temporarily) */
5378 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5379 (chk_length > (sizeof(struct sctp_auth_chunk) +
5380 SCTP_AUTH_DIGEST_LEN_MAX))) {
5383 SCTP_TCB_UNLOCK(locked_tcb);
5388 if (got_auth == 1) {
5389 /* skip this chunk... it's already auth'd */
5393 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5395 /* auth HMAC failed so dump the packet */
5399 /* remaining chunks are HMAC checked */
5400 stcb->asoc.authenticated = 1;
5406 /* it's an unknown chunk! */
5407 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5409 struct sctp_paramhdr *phd;
5411 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
5412 0, M_DONTWAIT, 1, MT_DATA);
5414 phd = mtod(mm, struct sctp_paramhdr *);
5416 * We cheat and use param type since
5417 * we did not bother to define a
5418 * error cause struct. They are the
5419 * same basic format with different
5422 phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5423 phd->param_length = htons(chk_length + sizeof(*phd));
5424 SCTP_BUF_LEN(mm) = sizeof(*phd);
5425 SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length),
5427 if (SCTP_BUF_NEXT(mm)) {
5428 #ifdef SCTP_MBUF_LOGGING
5429 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5432 mat = SCTP_BUF_NEXT(mm);
5434 if (SCTP_BUF_IS_EXTENDED(mat)) {
5435 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5437 mat = SCTP_BUF_NEXT(mat);
5441 sctp_queue_op_err(stcb, mm);
5447 if ((ch->chunk_type & 0x80) == 0) {
5448 /* discard this packet */
5451 } /* else skip this bad chunk and continue... */
5453 } /* switch (ch->chunk_type) */
5457 /* get the next chunk */
5458 *offset += SCTP_SIZE32(chk_length);
5459 if (*offset >= length) {
5460 /* no more data left in the mbuf chain */
5463 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5464 sizeof(struct sctp_chunkhdr), chunk_buf);
5467 SCTP_TCB_UNLOCK(locked_tcb);
5474 if (asconf_cnt > 0 && stcb != NULL) {
5475 sctp_send_asconf_ack(stcb);
5483 __attribute__((noinline))
5486 sctp_validate_no_locks(struct sctp_inpcb *inp)
5488 struct sctp_tcb *lstcb;
5490 LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) {
5491 if (mtx_owned(&lstcb->tcb_mtx)) {
5492 panic("Own lock on stcb at return from input");
5495 if (mtx_owned(&inp->inp_create_mtx)) {
5496 panic("Own create lock on inp");
5498 if (mtx_owned(&inp->inp_mtx)) {
5499 panic("Own inp lock on inp");
5506 * common input chunk processing (v4 and v6)
5509 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset,
5510 int length, struct sctphdr *sh, struct sctp_chunkhdr *ch,
5511 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
5512 uint8_t ecn_bits, uint32_t vrf_id, uint16_t port)
5515 * Control chunk processing
5518 int fwd_tsn_seen = 0, data_processed = 0;
5519 struct mbuf *m = *mm;
5521 int cnt_ctrl_ready = 0;
5523 SCTP_STAT_INCR(sctps_recvdatagrams);
5524 #ifdef SCTP_AUDITING_ENABLED
5525 sctp_audit_log(0xE0, 1);
5526 sctp_auditing(0, inp, stcb, net);
5529 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5530 m, iphlen, offset, length, stcb);
5532 /* always clear this before beginning a packet */
5533 stcb->asoc.authenticated = 0;
5534 stcb->asoc.seen_a_sack_this_pkt = 0;
5535 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5536 stcb, stcb->asoc.state);
5538 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5539 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5541 * If we hit here, we had a ref count
5542 * up when the assoc was aborted and the
5543 * timer is clearing out the assoc, we should
5544 * NOT respond to any packet.. its OOTB.
5546 SCTP_TCB_UNLOCK(stcb);
5547 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5552 if (IS_SCTP_CONTROL(ch)) {
5553 /* process the control portion of the SCTP packet */
5554 /* sa_ignore NO_NULL_CHK */
5555 stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch,
5556 inp, stcb, &net, &fwd_tsn_seen, vrf_id, port);
5559 * This covers us if the cookie-echo was there and
5560 * it changes our INP.
5562 inp = stcb->sctp_ep;
5563 if ((net) && (port)) {
5564 if (net->port == 0) {
5565 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5572 * no control chunks, so pre-process DATA chunks (these
5573 * checks are taken care of by control processing)
5577 * if DATA only packet, and auth is required, then punt...
5578 * can't have authenticated without any AUTH (control)
5581 if ((stcb != NULL) &&
5582 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5583 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5584 /* "silently" ignore */
5585 SCTP_STAT_INCR(sctps_recvauthmissing);
5586 SCTP_TCB_UNLOCK(stcb);
5590 /* out of the blue DATA chunk */
5591 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5595 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5596 /* v_tag mismatch! */
5597 SCTP_STAT_INCR(sctps_badvtag);
5598 SCTP_TCB_UNLOCK(stcb);
5605 * no valid TCB for this packet, or we found it's a bad
5606 * packet while processing control, or we're done with this
5607 * packet (done or skip rest of data), so we drop it...
5612 * DATA chunk processing
5614 /* plow through the data chunks while length > offset */
5617 * Rest should be DATA only. Check authentication state if AUTH for
5620 if ((length > offset) &&
5622 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5623 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5624 !stcb->asoc.authenticated) {
5625 /* "silently" ignore */
5626 SCTP_STAT_INCR(sctps_recvauthmissing);
5627 SCTPDBG(SCTP_DEBUG_AUTH1,
5628 "Data chunk requires AUTH, skipped\n");
5631 if (length > offset) {
5635 * First check to make sure our state is correct. We would
5636 * not get here unless we really did have a tag, so we don't
5637 * abort if this happens, just dump the chunk silently.
5639 switch (SCTP_GET_STATE(&stcb->asoc)) {
5640 case SCTP_STATE_COOKIE_ECHOED:
5642 * we consider data with valid tags in this state
5643 * shows us the cookie-ack was lost. Imply it was
5646 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5647 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5648 stcb->asoc.overall_error_count,
5650 SCTP_FROM_SCTP_INPUT,
5653 stcb->asoc.overall_error_count = 0;
5654 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5656 case SCTP_STATE_COOKIE_WAIT:
5658 * We consider OOTB any data sent during asoc setup.
5660 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5662 SCTP_TCB_UNLOCK(stcb);
5664 /* sa_ignore NOTREACHED */
5666 case SCTP_STATE_EMPTY: /* should not happen */
5667 case SCTP_STATE_INUSE: /* should not happen */
5668 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5669 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5671 SCTP_TCB_UNLOCK(stcb);
5673 /* sa_ignore NOTREACHED */
5675 case SCTP_STATE_OPEN:
5676 case SCTP_STATE_SHUTDOWN_SENT:
5679 /* plow through the data chunks while length > offset */
5680 retval = sctp_process_data(mm, iphlen, &offset, length, sh,
5681 inp, stcb, net, &high_tsn);
5684 * The association aborted, NO UNLOCK needed since
5685 * the association is destroyed.
5691 * Anything important needs to have been m_copy'ed in
5695 /* take care of ecn */
5696 if ((data_processed == 1) &&
5697 (stcb->asoc.ecn_allowed == 1) &&
5698 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5699 /* Yep, we need to add a ECNE */
5700 sctp_send_ecn_echo(stcb, net, high_tsn);
5702 if ((data_processed == 0) && (fwd_tsn_seen)) {
5704 uint32_t highest_tsn;
5706 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5707 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5709 highest_tsn = stcb->asoc.highest_tsn_inside_map;
5711 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
5712 stcb->asoc.send_sack = 1;
5713 sctp_sack_check(stcb, was_a_gap);
5714 } else if (fwd_tsn_seen) {
5715 stcb->asoc.send_sack = 1;
5717 /* trigger send of any chunks in queue... */
5719 #ifdef SCTP_AUDITING_ENABLED
5720 sctp_audit_log(0xE0, 2);
5721 sctp_auditing(1, inp, stcb, net);
5723 SCTPDBG(SCTP_DEBUG_INPUT1,
5724 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5725 stcb->asoc.peers_rwnd,
5726 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5727 stcb->asoc.total_flight);
5728 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5729 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5730 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5732 if (cnt_ctrl_ready ||
5734 (stcb->asoc.peers_rwnd > 0 ||
5735 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5736 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5737 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5738 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5740 #ifdef SCTP_AUDITING_ENABLED
5741 sctp_audit_log(0xE0, 3);
5742 sctp_auditing(2, inp, stcb, net);
5744 SCTP_TCB_UNLOCK(stcb);
5747 sctp_validate_no_locks(inp);
5754 sctp_print_mbuf_chain(struct mbuf *m)
5756 for (; m; m = SCTP_BUF_NEXT(m)) {
5757 printf("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m));
5758 if (SCTP_BUF_IS_EXTENDED(m))
5759 printf("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m));
5767 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5769 #ifdef SCTP_MBUF_LOGGING
5775 uint32_t vrf_id = 0;
5779 struct sctp_inpcb *inp = NULL;
5780 struct sctp_nets *net;
5781 struct sctp_tcb *stcb = NULL;
5782 struct sctp_chunkhdr *ch;
5783 int refcount_up = 0;
5784 int length, mlen, offset;
5786 #if !defined(SCTP_WITH_NO_CSUM)
5787 uint32_t check, calc_check;
5791 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
5792 SCTP_RELEASE_PKT(i_pak);
5795 mlen = SCTP_HEADER_LEN(i_pak);
5797 m = SCTP_HEADER_TO_CHAIN(i_pak);
5800 SCTP_STAT_INCR(sctps_recvpackets);
5801 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
5804 #ifdef SCTP_MBUF_LOGGING
5805 /* Log in any input mbufs */
5806 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5807 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
5808 if (SCTP_BUF_IS_EXTENDED(mat)) {
5809 sctp_log_mb(mat, SCTP_MBUF_INPUT);
5814 #ifdef SCTP_PACKET_LOGGING
5815 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
5816 sctp_packet_log(m, mlen);
5819 * Must take out the iphlen, since mlen expects this (only effect lb
5825 * Get IP, SCTP, and first chunk header together in first mbuf.
5827 ip = mtod(m, struct ip *);
5828 offset = iphlen + sizeof(*sh) + sizeof(*ch);
5829 if (SCTP_BUF_LEN(m) < offset) {
5830 if ((m = m_pullup(m, offset)) == 0) {
5831 SCTP_STAT_INCR(sctps_hdrops);
5834 ip = mtod(m, struct ip *);
5836 /* validate mbuf chain length with IP payload length */
5837 if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) {
5838 SCTP_STAT_INCR(sctps_hdrops);
5841 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
5842 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh));
5843 SCTPDBG(SCTP_DEBUG_INPUT1,
5844 "sctp_input() length:%d iphlen:%d\n", mlen, iphlen);
5846 /* SCTP does not allow broadcasts or multicasts */
5847 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
5850 if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) {
5852 * We only look at broadcast if its a front state, All
5853 * others we will not have a tcb for anyway.
5857 /* validate SCTP checksum */
5858 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
5859 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
5861 if_name(m->m_pkthdr.rcvif),
5862 m->m_pkthdr.csum_flags);
5863 #if defined(SCTP_WITH_NO_CSUM)
5864 SCTP_STAT_INCR(sctps_recvnocrc);
5866 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5867 SCTP_STAT_INCR(sctps_recvhwcrc);
5868 goto sctp_skip_csum_4;
5870 check = sh->checksum; /* save incoming checksum */
5871 sh->checksum = 0; /* prepare for calc */
5872 calc_check = sctp_calculate_cksum(m, iphlen);
5873 sh->checksum = check;
5874 SCTP_STAT_INCR(sctps_recvswcrc);
5875 if (calc_check != check) {
5876 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5877 calc_check, check, m, mlen, iphlen);
5879 stcb = sctp_findassociation_addr(m,
5880 offset - sizeof(*ch),
5883 if ((net) && (port)) {
5884 if (net->port == 0) {
5885 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5889 if ((net != NULL) && (m->m_flags & M_FLOWID)) {
5890 net->flowid = m->m_pkthdr.flowid;
5895 if ((inp) && (stcb)) {
5896 sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
5897 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5898 } else if ((inp != NULL) && (stcb == NULL)) {
5901 SCTP_STAT_INCR(sctps_badsum);
5902 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5907 /* destination port of 0 is illegal, based on RFC2960. */
5908 if (sh->dest_port == 0) {
5909 SCTP_STAT_INCR(sctps_hdrops);
5913 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
5914 * IP/SCTP/first chunk header...
5916 stcb = sctp_findassociation_addr(m, offset - sizeof(*ch),
5917 sh, ch, &inp, &net, vrf_id);
5918 if ((net) && (port)) {
5919 if (net->port == 0) {
5920 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5924 if ((net != NULL) && (m->m_flags & M_FLOWID)) {
5925 net->flowid = m->m_pkthdr.flowid;
5930 /* inp's ref-count increased && stcb locked */
5932 struct sctp_init_chunk *init_chk, chunk_buf;
5934 SCTP_STAT_INCR(sctps_noport);
5937 * we use the bandwidth limiting to protect against sending
5938 * too many ABORTS all at once. In this case these count the
5939 * same as an ICMP message.
5941 if (badport_bandlim(0) < 0)
5943 #endif /* ICMP_BANDLIM */
5944 SCTPDBG(SCTP_DEBUG_INPUT1,
5945 "Sending a ABORT from packet entry!\n");
5946 if (ch->chunk_type == SCTP_INITIATION) {
5948 * we do a trick here to get the INIT tag, dig in
5949 * and get the tag from the INIT and put it in the
5952 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
5953 iphlen + sizeof(*sh), sizeof(*init_chk),
5954 (uint8_t *) & chunk_buf);
5955 if (init_chk != NULL)
5956 sh->v_tag = init_chk->init.initiate_tag;
5958 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5959 sctp_send_shutdown_complete2(m, sh, vrf_id, port);
5962 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5965 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
5966 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
5968 } else if (stcb == NULL) {
5973 * I very much doubt any of the IPSEC stuff will work but I have no
5974 * idea, so I will leave it in place.
5976 if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) {
5977 MODULE_GLOBAL(ipsec4stat).in_polvio++;
5978 SCTP_STAT_INCR(sctps_hdrops);
5984 * common chunk processing
5986 length = ip->ip_len + iphlen;
5987 offset -= sizeof(struct sctp_chunkhdr);
5989 ecn_bits = ip->ip_tos;
5991 /* sa_ignore NO_NULL_CHK */
5992 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
5993 inp, stcb, net, ecn_bits, vrf_id, port);
5994 /* inp's ref-count reduced && stcb unlocked */
5998 if ((inp) && (refcount_up)) {
5999 /* reduce ref-count */
6000 SCTP_INP_DECR_REF(inp);
6005 SCTP_TCB_UNLOCK(stcb);
6007 if ((inp) && (refcount_up)) {
6008 /* reduce ref-count */
6009 SCTP_INP_DECR_REF(inp);
6017 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6018 extern int *sctp_cpuarry;
6023 sctp_input(struct mbuf *m, int off)
6025 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6030 uint32_t flowid, tag;
6033 if (m->m_flags & M_FLOWID) {
6034 flowid = m->m_pkthdr.flowid;
6037 * No flow id built by lower layers fix it so we
6040 ip = mtod(m, struct ip *);
6041 offset = off + sizeof(*sh);
6042 if (SCTP_BUF_LEN(m) < offset) {
6043 if ((m = m_pullup(m, offset)) == 0) {
6044 SCTP_STAT_INCR(sctps_hdrops);
6047 ip = mtod(m, struct ip *);
6049 sh = (struct sctphdr *)((caddr_t)ip + off);
6050 tag = htonl(sh->v_tag);
6051 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6052 m->m_pkthdr.flowid = flowid;
6053 m->m_flags |= M_FLOWID;
6055 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6056 sctp_queue_to_mcore(m, off, cpu_to_use);
6060 sctp_input_with_port(m, off, 0);