2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, 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 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.h>
37 #include <netinet/sctp_var.h>
38 #include <netinet/sctp_sysctl.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctputil.h>
42 #include <netinet/sctp_output.h>
43 #include <netinet/sctp_input.h>
44 #include <netinet/sctp_auth.h>
45 #include <netinet/sctp_indata.h>
46 #include <netinet/sctp_asconf.h>
47 #include <netinet/sctp_bsd_addr.h>
48 #include <netinet/sctp_timer.h>
49 #include <netinet/sctp_crc32.h>
50 #include <netinet/udp.h>
56 sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
58 struct sctp_nets *net;
61 * This now not only stops all cookie timers it also stops any INIT
62 * timers as well. This will make sure that the timers are stopped
63 * in all collision cases.
65 SCTP_TCB_LOCK_ASSERT(stcb);
66 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
67 if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
68 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
71 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
72 } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
73 sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
76 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
83 sctp_handle_init(struct mbuf *m, int iphlen, int offset,
84 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
85 struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
86 struct sctp_tcb *stcb, int *abort_no_unlock,
87 uint8_t use_mflowid, uint32_t mflowid,
88 uint32_t vrf_id, uint16_t port)
90 struct sctp_init *init;
93 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
99 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
100 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
101 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
102 use_mflowid, mflowid,
105 *abort_no_unlock = 1;
108 /* validate parameters */
110 if (init->initiate_tag == 0) {
111 /* protocol error... send abort */
112 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
113 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
114 use_mflowid, mflowid,
117 *abort_no_unlock = 1;
120 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
121 /* invalid parameter... send abort */
122 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
123 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
124 use_mflowid, mflowid,
127 *abort_no_unlock = 1;
130 if (init->num_inbound_streams == 0) {
131 /* protocol error... send abort */
132 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
133 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
134 use_mflowid, mflowid,
137 *abort_no_unlock = 1;
140 if (init->num_outbound_streams == 0) {
141 /* protocol error... send abort */
142 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
143 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
144 use_mflowid, mflowid,
147 *abort_no_unlock = 1;
150 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
151 offset + ntohs(cp->ch.chunk_length))) {
152 /* auth parameter(s) error... send abort */
153 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, NULL,
154 use_mflowid, mflowid,
157 *abort_no_unlock = 1;
161 * We are only accepting if we have a socket with positive
164 if ((stcb == NULL) &&
165 ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
166 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
167 (inp->sctp_socket == NULL) ||
168 (inp->sctp_socket->so_qlimit == 0))) {
170 * FIX ME ?? What about TCP model and we have a
171 * match/restart case? Actually no fix is needed. the lookup
172 * will always find the existing assoc so stcb would not be
173 * NULL. It may be questionable to do this since we COULD
174 * just send back the INIT-ACK and hope that the app did
175 * accept()'s by the time the COOKIE was sent. But there is
176 * a price to pay for COOKIE generation and I don't want to
177 * pay it on the chance that the app will actually do some
178 * accepts(). The App just looses and should NOT be in this
181 if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
182 sctp_send_abort(m, iphlen, src, dst, sh, 0, NULL,
183 use_mflowid, mflowid,
188 if ((stcb != NULL) &&
189 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
190 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
191 sctp_send_shutdown_ack(stcb, NULL);
192 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
194 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
195 sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, src, dst,
197 use_mflowid, mflowid,
199 ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED));
203 SCTP_INP_RUNLOCK(inp);
208 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
212 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
213 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
220 struct sctp_stream_queue_pending *sp;
221 struct sctp_association *asoc;
224 * This function returns the number of streams that have true unsent
225 * data on them. Note that as it looks through it will clean up any
226 * places that have old data that has been sent but left at top of
230 SCTP_TCB_SEND_LOCK(stcb);
231 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
232 /* Check to see if some data queued */
233 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
234 /* sa_ignore FREED_MEMORY */
235 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
239 if ((sp->msg_is_complete) &&
241 (sp->sender_all_done)) {
243 * We are doing differed cleanup. Last time
244 * through when we took all the data the
245 * sender_all_done was not set.
247 if (sp->put_last_out == 0) {
248 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
249 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
255 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
256 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
258 sctp_free_remote_addr(sp->net);
262 sctp_m_freem(sp->data);
265 sctp_free_a_strmoq(stcb, sp, so_locked);
272 SCTP_TCB_SEND_UNLOCK(stcb);
273 return (unsent_data);
277 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
279 struct sctp_init *init;
280 struct sctp_association *asoc;
281 struct sctp_nets *lnet;
286 /* save off parameters */
287 asoc->peer_vtag = ntohl(init->initiate_tag);
288 asoc->peers_rwnd = ntohl(init->a_rwnd);
290 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
292 if (!TAILQ_EMPTY(&asoc->nets)) {
293 /* update any ssthresh's that may have a default */
294 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
295 lnet->ssthresh = asoc->peers_rwnd;
296 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
297 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
301 SCTP_TCB_SEND_LOCK(stcb);
302 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
304 struct sctp_stream_out *outs;
305 struct sctp_stream_queue_pending *sp, *nsp;
306 struct sctp_tmit_chunk *chk, *nchk;
308 /* abandon the upper streams */
309 newcnt = ntohs(init->num_inbound_streams);
310 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
311 if (chk->rec.data.stream_number >= newcnt) {
312 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
313 asoc->send_queue_cnt--;
314 if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
315 asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
318 panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number);
321 if (chk->data != NULL) {
322 sctp_free_bufspace(stcb, asoc, chk, 1);
323 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
324 0, chk, SCTP_SO_NOT_LOCKED);
326 sctp_m_freem(chk->data);
330 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
331 /* sa_ignore FREED_MEMORY */
335 for (i = newcnt; i < asoc->pre_open_streams; i++) {
336 outs = &asoc->strmout[i];
337 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
338 TAILQ_REMOVE(&outs->outqueue, sp, next);
339 asoc->stream_queue_cnt--;
340 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
341 stcb, 0, sp, SCTP_SO_NOT_LOCKED);
343 sctp_m_freem(sp->data);
347 sctp_free_remote_addr(sp->net);
351 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
352 /* sa_ignore FREED_MEMORY */
356 /* cut back the count */
357 asoc->pre_open_streams = newcnt;
359 SCTP_TCB_SEND_UNLOCK(stcb);
360 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
362 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
363 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
364 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
365 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
367 /* This is the next one we expect */
368 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
370 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
371 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
373 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
374 /* open the requested streams */
376 if (asoc->strmin != NULL) {
377 /* Free the old ones */
378 struct sctp_queued_to_read *ctl, *nctl;
380 for (i = 0; i < asoc->streamincnt; i++) {
381 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) {
382 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
383 sctp_free_remote_addr(ctl->whoFrom);
385 sctp_m_freem(ctl->data);
387 sctp_free_a_readq(stcb, ctl);
390 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
392 if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
393 asoc->streamincnt = ntohs(init->num_outbound_streams);
395 asoc->streamincnt = asoc->max_inbound_streams;
397 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
398 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
399 if (asoc->strmin == NULL) {
400 /* we didn't get memory for the streams! */
401 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
404 for (i = 0; i < asoc->streamincnt; i++) {
405 asoc->strmin[i].stream_no = i;
406 asoc->strmin[i].last_sequence_delivered = 0xffff;
407 TAILQ_INIT(&asoc->strmin[i].inqueue);
408 asoc->strmin[i].delivery_started = 0;
411 * load_address_from_init will put the addresses into the
412 * association when the COOKIE is processed or the INIT-ACK is
413 * processed. Both types of COOKIE's existing and new call this
414 * routine. It will remove addresses that are no longer in the
415 * association (for the restarting case where addresses are
416 * removed). Up front when the INIT arrives we will discard it if it
417 * is a restart and new addresses have been added.
419 /* sa_ignore MEMLEAK */
424 * INIT-ACK message processing/consumption returns value < 0 on error
427 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
428 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
429 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
430 struct sctp_nets *net, int *abort_no_unlock,
431 uint8_t use_mflowid, uint32_t mflowid,
434 struct sctp_association *asoc;
436 int retval, abort_flag;
437 uint32_t initack_limit;
438 int nat_friendly = 0;
440 /* First verify that we have no illegal param's */
444 op_err = sctp_arethere_unrecognized_parameters(m,
445 (offset + sizeof(struct sctp_init_chunk)),
446 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
448 /* Send an abort and notify peer */
449 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
450 *abort_no_unlock = 1;
454 asoc->peer_supports_nat = (uint8_t) nat_friendly;
455 /* process the peer's parameters in the INIT-ACK */
456 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
460 initack_limit = offset + ntohs(cp->ch.chunk_length);
461 /* load all addresses */
462 if ((retval = sctp_load_addresses_from_init(stcb, m,
463 (offset + sizeof(struct sctp_init_chunk)), initack_limit,
465 /* Huh, we should abort */
466 SCTPDBG(SCTP_DEBUG_INPUT1,
467 "Load addresses from INIT causes an abort %d\n",
469 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
471 use_mflowid, mflowid,
473 *abort_no_unlock = 1;
476 /* if the peer doesn't support asconf, flush the asconf queue */
477 if (asoc->peer_supports_asconf == 0) {
478 struct sctp_asconf_addr *param, *nparam;
480 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
481 TAILQ_REMOVE(&asoc->asconf_queue, param, next);
482 SCTP_FREE(param, SCTP_M_ASC_ADDR);
485 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
486 stcb->asoc.local_hmacs);
488 sctp_queue_op_err(stcb, op_err);
489 /* queuing will steal away the mbuf chain to the out queue */
492 /* extract the cookie and queue it to "echo" it back... */
493 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
494 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
495 stcb->asoc.overall_error_count,
497 SCTP_FROM_SCTP_INPUT,
500 stcb->asoc.overall_error_count = 0;
501 net->error_count = 0;
504 * Cancel the INIT timer, We do this first before queueing the
505 * cookie. We always cancel at the primary to assue that we are
506 * canceling the timer started by the INIT which always goes to the
509 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
510 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
512 /* calculate the RTO */
513 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy,
514 SCTP_RTT_FROM_NON_DATA);
516 retval = sctp_send_cookie_echo(m, offset, stcb, net);
519 * No cookie, we probably should send a op error. But in any
520 * case if there is no cookie in the INIT-ACK, we can
521 * abandon the peer, its broke.
524 /* We abort with an error of missing mandatory param */
526 sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM);
529 * Expand beyond to include the mandatory
532 struct sctp_inv_mandatory_param *mp;
534 SCTP_BUF_LEN(op_err) =
535 sizeof(struct sctp_inv_mandatory_param);
537 struct sctp_inv_mandatory_param *);
538 /* Subtract the reserved param */
540 htons(sizeof(struct sctp_inv_mandatory_param) - 2);
541 mp->num_param = htonl(1);
542 mp->param = htons(SCTP_STATE_COOKIE);
545 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
546 src, dst, sh, op_err,
547 use_mflowid, mflowid,
549 *abort_no_unlock = 1;
557 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
558 struct sctp_tcb *stcb, struct sctp_nets *net)
560 struct sockaddr_storage store;
561 struct sctp_nets *r_net, *f_net;
564 uint16_t old_error_counter;
567 struct sockaddr_in *sin;
571 struct sockaddr_in6 *sin6;
575 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
579 memset(&store, 0, sizeof(store));
580 switch (cp->heartbeat.hb_info.addr_family) {
583 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
584 sin = (struct sockaddr_in *)&store;
585 sin->sin_family = cp->heartbeat.hb_info.addr_family;
586 sin->sin_len = cp->heartbeat.hb_info.addr_len;
587 sin->sin_port = stcb->rport;
588 memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address,
589 sizeof(sin->sin_addr));
597 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
598 sin6 = (struct sockaddr_in6 *)&store;
599 sin6->sin6_family = cp->heartbeat.hb_info.addr_family;
600 sin6->sin6_len = cp->heartbeat.hb_info.addr_len;
601 sin6->sin6_port = stcb->rport;
602 memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address,
603 sizeof(sin6->sin6_addr));
612 r_net = sctp_findnet(stcb, (struct sockaddr *)&store);
614 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
617 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
618 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
619 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
621 * If the its a HB and it's random value is correct when can
622 * confirm the destination.
624 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
625 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
626 stcb->asoc.primary_destination = r_net;
627 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
628 f_net = TAILQ_FIRST(&stcb->asoc.nets);
629 if (f_net != r_net) {
631 * first one on the list is NOT the primary
632 * sctp_cmpaddr() is much more efficent if
633 * the primary is the first on the list,
636 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
637 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
641 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
642 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
643 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
644 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
646 old_error_counter = r_net->error_count;
647 r_net->error_count = 0;
648 r_net->hb_responded = 1;
649 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
650 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
651 /* Now lets do a RTO with this */
652 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy,
653 SCTP_RTT_FROM_NON_DATA);
654 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
655 r_net->dest_state |= SCTP_ADDR_REACHABLE;
656 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
657 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
659 if (r_net->dest_state & SCTP_ADDR_PF) {
660 r_net->dest_state &= ~SCTP_ADDR_PF;
661 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
663 if (old_error_counter > 0) {
664 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
665 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
667 if (r_net == stcb->asoc.primary_destination) {
668 if (stcb->asoc.alternate) {
669 /* release the alternate, primary is good */
670 sctp_free_remote_addr(stcb->asoc.alternate);
671 stcb->asoc.alternate = NULL;
674 /* Mobility adaptation */
676 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
677 SCTP_MOBILITY_BASE) ||
678 sctp_is_mobility_feature_on(stcb->sctp_ep,
679 SCTP_MOBILITY_FASTHANDOFF)) &&
680 sctp_is_mobility_feature_on(stcb->sctp_ep,
681 SCTP_MOBILITY_PRIM_DELETED)) {
683 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7);
684 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
685 SCTP_MOBILITY_FASTHANDOFF)) {
686 sctp_assoc_immediate_retrans(stcb,
687 stcb->asoc.primary_destination);
689 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
690 SCTP_MOBILITY_BASE)) {
691 sctp_move_chunks_from_net(stcb,
692 stcb->asoc.deleted_primary);
694 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
695 stcb->asoc.deleted_primary);
701 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
704 * return 0 means we want you to proceed with the abort non-zero
705 * means no abort processing
707 struct sctpasochead *head;
709 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
710 /* generate a new vtag and send init */
711 LIST_REMOVE(stcb, sctp_asocs);
712 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
713 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
715 * put it in the bucket in the vtag hash of assoc's for the
718 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
719 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
722 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
724 * treat like a case where the cookie expired i.e.: - dump
725 * current cookie. - generate a new vtag. - resend init.
727 /* generate a new vtag and send init */
728 LIST_REMOVE(stcb, sctp_asocs);
729 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
730 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
731 sctp_stop_all_cookie_timers(stcb);
732 sctp_toss_old_cookies(stcb, &stcb->asoc);
733 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
734 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
736 * put it in the bucket in the vtag hash of assoc's for the
739 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
740 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
747 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
748 struct sctp_nets *net)
751 * return 0 means we want you to proceed with the abort non-zero
752 * means no abort processing
754 if (stcb->asoc.peer_supports_auth == 0) {
755 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
758 sctp_asconf_send_nat_state_update(stcb, net);
764 sctp_handle_abort(struct sctp_abort_chunk *abort,
765 struct sctp_tcb *stcb, struct sctp_nets *net)
767 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
774 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
778 len = ntohs(abort->ch.chunk_length);
779 if (len > sizeof(struct sctp_chunkhdr)) {
781 * Need to check the cause codes for our two magic nat
782 * aborts which don't kill the assoc necessarily.
784 struct sctp_missing_nat_state *natc;
786 natc = (struct sctp_missing_nat_state *)(abort + 1);
787 error = ntohs(natc->cause);
788 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
789 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
790 abort->ch.chunk_flags);
791 if (sctp_handle_nat_colliding_state(stcb)) {
794 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
795 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
796 abort->ch.chunk_flags);
797 if (sctp_handle_nat_missing_state(stcb, net)) {
804 /* stop any receive timers */
805 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
806 /* notify user of the abort and clean up... */
807 sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
809 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
810 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
811 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
812 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
814 #ifdef SCTP_ASOCLOG_OF_TSNS
815 sctp_print_out_track_log(stcb);
817 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
818 so = SCTP_INP_SO(stcb->sctp_ep);
819 atomic_add_int(&stcb->asoc.refcnt, 1);
820 SCTP_TCB_UNLOCK(stcb);
821 SCTP_SOCKET_LOCK(so, 1);
823 atomic_subtract_int(&stcb->asoc.refcnt, 1);
825 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
826 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
827 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
828 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
829 SCTP_SOCKET_UNLOCK(so, 1);
831 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
835 sctp_start_net_timers(struct sctp_tcb *stcb)
837 uint32_t cnt_hb_sent;
838 struct sctp_nets *net;
841 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
843 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
844 * If the dest in unconfirmed send a hb as well if under
845 * max_hb_burst have been sent.
847 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
848 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
849 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
850 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
851 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
856 sctp_chunk_output(stcb->sctp_ep, stcb,
857 SCTP_OUTPUT_FROM_COOKIE_ACK,
864 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
865 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
867 struct sctp_association *asoc;
868 int some_on_streamwheel;
870 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
875 SCTPDBG(SCTP_DEBUG_INPUT2,
876 "sctp_handle_shutdown: handling SHUTDOWN\n");
880 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
881 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
884 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
885 /* Shutdown NOT the expected size */
888 sctp_update_acked(stcb, cp, abort_flag);
893 if (asoc->control_pdapi) {
895 * With a normal shutdown we assume the end of last record.
897 SCTP_INP_READ_LOCK(stcb->sctp_ep);
898 asoc->control_pdapi->end_added = 1;
899 asoc->control_pdapi->pdapi_aborted = 1;
900 asoc->control_pdapi = NULL;
901 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
902 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
903 so = SCTP_INP_SO(stcb->sctp_ep);
904 atomic_add_int(&stcb->asoc.refcnt, 1);
905 SCTP_TCB_UNLOCK(stcb);
906 SCTP_SOCKET_LOCK(so, 1);
908 atomic_subtract_int(&stcb->asoc.refcnt, 1);
909 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
910 /* assoc was freed while we were unlocked */
911 SCTP_SOCKET_UNLOCK(so, 1);
915 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
916 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
917 SCTP_SOCKET_UNLOCK(so, 1);
920 /* goto SHUTDOWN_RECEIVED state to block new requests */
921 if (stcb->sctp_socket) {
922 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
923 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
924 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
925 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
926 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
928 * notify upper layer that peer has initiated a
931 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
934 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
937 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
939 * stop the shutdown timer, since we WILL move to
942 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
944 /* Now is there unsent data on a stream somewhere? */
945 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
947 if (!TAILQ_EMPTY(&asoc->send_queue) ||
948 !TAILQ_EMPTY(&asoc->sent_queue) ||
949 some_on_streamwheel) {
950 /* By returning we will push more data out */
953 /* no outstanding data to send, so move on... */
954 /* send SHUTDOWN-ACK */
955 /* move to SHUTDOWN-ACK-SENT state */
956 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
957 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
958 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
960 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
961 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
962 sctp_stop_timers_for_shutdown(stcb);
963 sctp_send_shutdown_ack(stcb, net);
964 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
970 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
971 struct sctp_tcb *stcb,
972 struct sctp_nets *net)
974 struct sctp_association *asoc;
976 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
979 so = SCTP_INP_SO(stcb->sctp_ep);
981 SCTPDBG(SCTP_DEBUG_INPUT2,
982 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
987 /* process according to association state */
988 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
989 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
990 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
991 sctp_send_shutdown_complete(stcb, net, 1);
992 SCTP_TCB_UNLOCK(stcb);
995 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
996 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
997 /* unexpected SHUTDOWN-ACK... so ignore... */
998 SCTP_TCB_UNLOCK(stcb);
1001 if (asoc->control_pdapi) {
1003 * With a normal shutdown we assume the end of last record.
1005 SCTP_INP_READ_LOCK(stcb->sctp_ep);
1006 asoc->control_pdapi->end_added = 1;
1007 asoc->control_pdapi->pdapi_aborted = 1;
1008 asoc->control_pdapi = NULL;
1009 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1010 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1011 atomic_add_int(&stcb->asoc.refcnt, 1);
1012 SCTP_TCB_UNLOCK(stcb);
1013 SCTP_SOCKET_LOCK(so, 1);
1014 SCTP_TCB_LOCK(stcb);
1015 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1016 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1017 /* assoc was freed while we were unlocked */
1018 SCTP_SOCKET_UNLOCK(so, 1);
1022 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1023 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1024 SCTP_SOCKET_UNLOCK(so, 1);
1028 if (!TAILQ_EMPTY(&asoc->send_queue) ||
1029 !TAILQ_EMPTY(&asoc->sent_queue) ||
1030 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
1031 panic("Queues are not empty when handling SHUTDOWN-ACK");
1034 /* stop the timer */
1035 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
1036 /* send SHUTDOWN-COMPLETE */
1037 sctp_send_shutdown_complete(stcb, net, 0);
1038 /* notify upper layer protocol */
1039 if (stcb->sctp_socket) {
1040 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1041 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1042 stcb->sctp_socket->so_snd.sb_cc = 0;
1044 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1046 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1047 /* free the TCB but first save off the ep */
1048 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1049 atomic_add_int(&stcb->asoc.refcnt, 1);
1050 SCTP_TCB_UNLOCK(stcb);
1051 SCTP_SOCKET_LOCK(so, 1);
1052 SCTP_TCB_LOCK(stcb);
1053 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1055 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1056 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1057 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1058 SCTP_SOCKET_UNLOCK(so, 1);
1063 * Skip past the param header and then we will find the chunk that caused the
1064 * problem. There are two possiblities ASCONF or FWD-TSN other than that and
1065 * our peer must be broken.
1068 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1069 struct sctp_nets *net)
1071 struct sctp_chunkhdr *chk;
1073 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1074 switch (chk->chunk_type) {
1075 case SCTP_ASCONF_ACK:
1077 sctp_asconf_cleanup(stcb, net);
1079 case SCTP_FORWARD_CUM_TSN:
1080 stcb->asoc.peer_supports_prsctp = 0;
1083 SCTPDBG(SCTP_DEBUG_INPUT2,
1084 "Peer does not support chunk type %d(%x)??\n",
1085 chk->chunk_type, (uint32_t) chk->chunk_type);
1091 * Skip past the param header and then we will find the param that caused the
1092 * problem. There are a number of param's in a ASCONF OR the prsctp param
1093 * these will turn of specific features.
1096 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1098 struct sctp_paramhdr *pbad;
1101 switch (ntohs(pbad->param_type)) {
1103 case SCTP_PRSCTP_SUPPORTED:
1104 stcb->asoc.peer_supports_prsctp = 0;
1106 case SCTP_SUPPORTED_CHUNK_EXT:
1108 /* draft-ietf-tsvwg-addip-sctp */
1109 case SCTP_HAS_NAT_SUPPORT:
1110 stcb->asoc.peer_supports_nat = 0;
1112 case SCTP_ADD_IP_ADDRESS:
1113 case SCTP_DEL_IP_ADDRESS:
1114 case SCTP_SET_PRIM_ADDR:
1115 stcb->asoc.peer_supports_asconf = 0;
1117 case SCTP_SUCCESS_REPORT:
1118 case SCTP_ERROR_CAUSE_IND:
1119 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1120 SCTPDBG(SCTP_DEBUG_INPUT2,
1121 "Turning off ASCONF to this strange peer\n");
1122 stcb->asoc.peer_supports_asconf = 0;
1125 SCTPDBG(SCTP_DEBUG_INPUT2,
1126 "Peer does not support param type %d(%x)??\n",
1127 pbad->param_type, (uint32_t) pbad->param_type);
1133 sctp_handle_error(struct sctp_chunkhdr *ch,
1134 struct sctp_tcb *stcb, struct sctp_nets *net)
1137 struct sctp_paramhdr *phdr;
1138 uint16_t error, error_type;
1140 struct sctp_association *asoc;
1143 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1148 /* parse through all of the errors and process */
1150 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1151 sizeof(struct sctp_chunkhdr));
1152 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1154 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1155 /* Process an Error Cause */
1156 error_type = ntohs(phdr->param_type);
1157 error_len = ntohs(phdr->param_length);
1158 if ((error_len > chklen) || (error_len == 0)) {
1159 /* invalid param length for this param */
1160 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1165 /* report the first error cause */
1168 switch (error_type) {
1169 case SCTP_CAUSE_INVALID_STREAM:
1170 case SCTP_CAUSE_MISSING_PARAM:
1171 case SCTP_CAUSE_INVALID_PARAM:
1172 case SCTP_CAUSE_NO_USER_DATA:
1173 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1176 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1177 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1179 if (sctp_handle_nat_colliding_state(stcb)) {
1183 case SCTP_CAUSE_NAT_MISSING_STATE:
1184 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1186 if (sctp_handle_nat_missing_state(stcb, net)) {
1190 case SCTP_CAUSE_STALE_COOKIE:
1192 * We only act if we have echoed a cookie and are
1195 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1198 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1199 /* Save the time doubled */
1200 asoc->cookie_preserve_req = ntohl(*p) << 1;
1201 asoc->stale_cookie_count++;
1202 if (asoc->stale_cookie_count >
1203 asoc->max_init_times) {
1204 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1205 /* now free the asoc */
1206 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1207 so = SCTP_INP_SO(stcb->sctp_ep);
1208 atomic_add_int(&stcb->asoc.refcnt, 1);
1209 SCTP_TCB_UNLOCK(stcb);
1210 SCTP_SOCKET_LOCK(so, 1);
1211 SCTP_TCB_LOCK(stcb);
1212 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1214 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1215 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1216 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1217 SCTP_SOCKET_UNLOCK(so, 1);
1221 /* blast back to INIT state */
1222 sctp_toss_old_cookies(stcb, &stcb->asoc);
1223 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1224 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1225 sctp_stop_all_cookie_timers(stcb);
1226 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1229 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1231 * Nothing we can do here, we don't do hostname
1232 * addresses so if the peer does not like my IPv6
1233 * (or IPv4 for that matter) it does not matter. If
1234 * they don't support that type of address, they can
1235 * NOT possibly get that packet type... i.e. with no
1236 * IPv6 you can't recieve a IPv6 packet. so we can
1237 * safely ignore this one. If we ever added support
1238 * for HOSTNAME Addresses, then we would need to do
1242 case SCTP_CAUSE_UNRECOG_CHUNK:
1243 sctp_process_unrecog_chunk(stcb, phdr, net);
1245 case SCTP_CAUSE_UNRECOG_PARAM:
1246 sctp_process_unrecog_param(stcb, phdr);
1248 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1250 * We ignore this since the timer will drive out a
1251 * new cookie anyway and there timer will drive us
1252 * to send a SHUTDOWN_COMPLETE. We can't send one
1253 * here since we don't have their tag.
1256 case SCTP_CAUSE_DELETING_LAST_ADDR:
1257 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1258 case SCTP_CAUSE_DELETING_SRC_ADDR:
1260 * We should NOT get these here, but in a
1263 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1266 case SCTP_CAUSE_OUT_OF_RESC:
1268 * And what, pray tell do we do with the fact that
1269 * the peer is out of resources? Not really sure we
1270 * could do anything but abort. I suspect this
1271 * should have came WITH an abort instead of in a
1276 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1280 adjust = SCTP_SIZE32(error_len);
1282 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1284 sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, error, ch, SCTP_SO_NOT_LOCKED);
1289 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1290 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1291 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1292 struct sctp_nets *net, int *abort_no_unlock,
1293 uint8_t use_mflowid, uint32_t mflowid,
1296 struct sctp_init_ack *init_ack;
1297 struct mbuf *op_err;
1299 SCTPDBG(SCTP_DEBUG_INPUT2,
1300 "sctp_handle_init_ack: handling INIT-ACK\n");
1303 SCTPDBG(SCTP_DEBUG_INPUT2,
1304 "sctp_handle_init_ack: TCB is null\n");
1307 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1308 /* Invalid length */
1309 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1310 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1311 src, dst, sh, op_err,
1312 use_mflowid, mflowid,
1314 *abort_no_unlock = 1;
1317 init_ack = &cp->init;
1318 /* validate parameters */
1319 if (init_ack->initiate_tag == 0) {
1320 /* protocol error... send an abort */
1321 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1322 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1323 src, dst, sh, op_err,
1324 use_mflowid, mflowid,
1326 *abort_no_unlock = 1;
1329 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1330 /* protocol error... send an abort */
1331 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1332 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1333 src, dst, sh, op_err,
1334 use_mflowid, mflowid,
1336 *abort_no_unlock = 1;
1339 if (init_ack->num_inbound_streams == 0) {
1340 /* protocol error... send an abort */
1341 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1342 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1343 src, dst, sh, op_err,
1344 use_mflowid, mflowid,
1346 *abort_no_unlock = 1;
1349 if (init_ack->num_outbound_streams == 0) {
1350 /* protocol error... send an abort */
1351 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1352 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1353 src, dst, sh, op_err,
1354 use_mflowid, mflowid,
1356 *abort_no_unlock = 1;
1359 /* process according to association state... */
1360 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1361 case SCTP_STATE_COOKIE_WAIT:
1362 /* this is the expected state for this chunk */
1363 /* process the INIT-ACK parameters */
1364 if (stcb->asoc.primary_destination->dest_state &
1365 SCTP_ADDR_UNCONFIRMED) {
1367 * The primary is where we sent the INIT, we can
1368 * always consider it confirmed when the INIT-ACK is
1369 * returned. Do this before we load addresses
1372 stcb->asoc.primary_destination->dest_state &=
1373 ~SCTP_ADDR_UNCONFIRMED;
1374 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1375 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1377 if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1378 net, abort_no_unlock,
1379 use_mflowid, mflowid,
1381 /* error in parsing parameters */
1384 /* update our state */
1385 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1386 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1388 /* reset the RTO calc */
1389 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1390 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1391 stcb->asoc.overall_error_count,
1393 SCTP_FROM_SCTP_INPUT,
1396 stcb->asoc.overall_error_count = 0;
1397 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1399 * collapse the init timer back in case of a exponential
1402 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1405 * the send at the end of the inbound data processing will
1406 * cause the cookie to be sent
1409 case SCTP_STATE_SHUTDOWN_SENT:
1410 /* incorrect state... discard */
1412 case SCTP_STATE_COOKIE_ECHOED:
1413 /* incorrect state... discard */
1415 case SCTP_STATE_OPEN:
1416 /* incorrect state... discard */
1418 case SCTP_STATE_EMPTY:
1419 case SCTP_STATE_INUSE:
1421 /* incorrect state... discard */
1425 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1429 static struct sctp_tcb *
1430 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1431 struct sockaddr *src, struct sockaddr *dst,
1432 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1433 struct sctp_inpcb *inp, struct sctp_nets **netp,
1434 struct sockaddr *init_src, int *notification,
1435 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1436 uint8_t use_mflowid, uint32_t mflowid,
1437 uint32_t vrf_id, uint16_t port);
1441 * handle a state cookie for an existing association m: input packet mbuf
1442 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1443 * "split" mbuf and the cookie signature does not exist offset: offset into
1444 * mbuf to the cookie-echo chunk
1446 static struct sctp_tcb *
1447 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1448 struct sockaddr *src, struct sockaddr *dst,
1449 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1450 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1451 struct sockaddr *init_src, int *notification,
1452 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1453 uint8_t use_mflowid, uint32_t mflowid,
1454 uint32_t vrf_id, uint16_t port)
1456 struct sctp_association *asoc;
1457 struct sctp_init_chunk *init_cp, init_buf;
1458 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1459 struct sctp_nets *net;
1460 struct mbuf *op_err;
1461 struct sctp_paramhdr *ph;
1462 int init_offset, initack_offset, i;
1468 /* I know that the TCB is non-NULL from the caller */
1470 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1471 if (asoc->cookie_how[how_indx] == 0)
1474 if (how_indx < sizeof(asoc->cookie_how)) {
1475 asoc->cookie_how[how_indx] = 1;
1477 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1478 /* SHUTDOWN came in after sending INIT-ACK */
1479 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1480 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1481 0, M_DONTWAIT, 1, MT_DATA);
1482 if (op_err == NULL) {
1487 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1488 ph = mtod(op_err, struct sctp_paramhdr *);
1489 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
1490 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1491 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1492 use_mflowid, mflowid,
1494 if (how_indx < sizeof(asoc->cookie_how))
1495 asoc->cookie_how[how_indx] = 2;
1499 * find and validate the INIT chunk in the cookie (peer's info) the
1500 * INIT should start after the cookie-echo header struct (chunk
1501 * header, state cookie header struct)
1503 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1505 init_cp = (struct sctp_init_chunk *)
1506 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1507 (uint8_t *) & init_buf);
1508 if (init_cp == NULL) {
1509 /* could not pull a INIT chunk in cookie */
1512 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1516 * find and validate the INIT-ACK chunk in the cookie (my info) the
1517 * INIT-ACK follows the INIT chunk
1519 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1520 initack_cp = (struct sctp_init_ack_chunk *)
1521 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1522 (uint8_t *) & initack_buf);
1523 if (initack_cp == NULL) {
1524 /* could not pull INIT-ACK chunk in cookie */
1527 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1530 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1531 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1533 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1534 * to get into the OPEN state
1536 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1538 * Opps, this means that we somehow generated two vtag's
1539 * the same. I.e. we did:
1541 * <---INIT(tag=a)------
1542 * ----INIT-ACK(tag=t)-->
1543 * ----INIT(tag=t)------> *1
1544 * <---INIT-ACK(tag=a)---
1545 * <----CE(tag=t)------------- *2
1547 * At point *1 we should be generating a different
1548 * tag t'. Which means we would throw away the CE and send
1549 * ours instead. Basically this is case C (throw away side).
1551 if (how_indx < sizeof(asoc->cookie_how))
1552 asoc->cookie_how[how_indx] = 17;
1556 switch SCTP_GET_STATE
1558 case SCTP_STATE_COOKIE_WAIT:
1559 case SCTP_STATE_COOKIE_ECHOED:
1561 * INIT was sent but got a COOKIE_ECHO with the
1562 * correct tags... just accept it...but we must
1563 * process the init so that we can make sure we have
1564 * the right seq no's.
1566 /* First we must process the INIT !! */
1567 retval = sctp_process_init(init_cp, stcb);
1569 if (how_indx < sizeof(asoc->cookie_how))
1570 asoc->cookie_how[how_indx] = 3;
1573 /* we have already processed the INIT so no problem */
1574 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1575 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1576 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1577 /* update current state */
1578 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1579 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1581 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1583 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1584 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1585 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1586 stcb->sctp_ep, stcb, asoc->primary_destination);
1588 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1589 sctp_stop_all_cookie_timers(stcb);
1590 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1591 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1592 (inp->sctp_socket->so_qlimit == 0)
1594 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1599 * Here is where collision would go if we
1600 * did a connect() and instead got a
1601 * init/init-ack/cookie done before the
1602 * init-ack came back..
1604 stcb->sctp_ep->sctp_flags |=
1605 SCTP_PCB_FLAGS_CONNECTED;
1606 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1607 so = SCTP_INP_SO(stcb->sctp_ep);
1608 atomic_add_int(&stcb->asoc.refcnt, 1);
1609 SCTP_TCB_UNLOCK(stcb);
1610 SCTP_SOCKET_LOCK(so, 1);
1611 SCTP_TCB_LOCK(stcb);
1612 atomic_add_int(&stcb->asoc.refcnt, -1);
1613 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1614 SCTP_SOCKET_UNLOCK(so, 1);
1618 soisconnected(stcb->sctp_socket);
1619 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1620 SCTP_SOCKET_UNLOCK(so, 1);
1623 /* notify upper layer */
1624 *notification = SCTP_NOTIFY_ASSOC_UP;
1626 * since we did not send a HB make sure we don't
1629 net->hb_responded = 1;
1630 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1631 &cookie->time_entered,
1632 sctp_align_unsafe_makecopy,
1633 SCTP_RTT_FROM_NON_DATA);
1635 if (stcb->asoc.sctp_autoclose_ticks &&
1636 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1637 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1643 * we're in the OPEN state (or beyond), so peer must
1644 * have simply lost the COOKIE-ACK
1648 sctp_stop_all_cookie_timers(stcb);
1650 * We ignore the return code here.. not sure if we should
1651 * somehow abort.. but we do have an existing asoc. This
1652 * really should not fail.
1654 if (sctp_load_addresses_from_init(stcb, m,
1655 init_offset + sizeof(struct sctp_init_chunk),
1656 initack_offset, src, dst, init_src)) {
1657 if (how_indx < sizeof(asoc->cookie_how))
1658 asoc->cookie_how[how_indx] = 4;
1661 /* respond with a COOKIE-ACK */
1662 sctp_toss_old_cookies(stcb, asoc);
1663 sctp_send_cookie_ack(stcb);
1664 if (how_indx < sizeof(asoc->cookie_how))
1665 asoc->cookie_how[how_indx] = 5;
1668 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1669 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1670 cookie->tie_tag_my_vtag == 0 &&
1671 cookie->tie_tag_peer_vtag == 0) {
1673 * case C in Section 5.2.4 Table 2: XMOO silently discard
1675 if (how_indx < sizeof(asoc->cookie_how))
1676 asoc->cookie_how[how_indx] = 6;
1680 * If nat support, and the below and stcb is established, send back
1681 * a ABORT(colliding state) if we are established.
1683 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1684 (asoc->peer_supports_nat) &&
1685 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1686 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1687 (asoc->peer_vtag == 0)))) {
1689 * Special case - Peer's support nat. We may have two init's
1690 * that we gave out the same tag on since one was not
1691 * established.. i.e. we get INIT from host-1 behind the nat
1692 * and we respond tag-a, we get a INIT from host-2 behind
1693 * the nat and we get tag-a again. Then we bring up host-1
1694 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1695 * Now we have colliding state. We must send an abort here
1696 * with colliding state indication.
1698 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1699 0, M_DONTWAIT, 1, MT_DATA);
1700 if (op_err == NULL) {
1704 /* pre-reserve some space */
1706 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
1708 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
1710 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
1711 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1713 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1714 ph = mtod(op_err, struct sctp_paramhdr *);
1715 ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE);
1716 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1717 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1718 use_mflowid, mflowid,
1722 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1723 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1724 (asoc->peer_vtag == 0))) {
1726 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1727 * should be ok, re-accept peer info
1729 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1731 * Extension of case C. If we hit this, then the
1732 * random number generator returned the same vtag
1733 * when we first sent our INIT-ACK and when we later
1734 * sent our INIT. The side with the seq numbers that
1735 * are different will be the one that normnally
1736 * would have hit case C. This in effect "extends"
1737 * our vtags in this collision case to be 64 bits.
1738 * The same collision could occur aka you get both
1739 * vtag and seq number the same twice in a row.. but
1740 * is much less likely. If it did happen then we
1741 * would proceed through and bring up the assoc.. we
1742 * may end up with the wrong stream setup however..
1743 * which would be bad.. but there is no way to
1744 * tell.. until we send on a stream that does not
1747 if (how_indx < sizeof(asoc->cookie_how))
1748 asoc->cookie_how[how_indx] = 7;
1752 if (how_indx < sizeof(asoc->cookie_how))
1753 asoc->cookie_how[how_indx] = 8;
1754 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1755 sctp_stop_all_cookie_timers(stcb);
1757 * since we did not send a HB make sure we don't double
1760 net->hb_responded = 1;
1761 if (stcb->asoc.sctp_autoclose_ticks &&
1762 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1763 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1766 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1767 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1769 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1771 * Ok the peer probably discarded our data (if we
1772 * echoed a cookie+data). So anything on the
1773 * sent_queue should be marked for retransmit, we
1774 * may not get something to kick us so it COULD
1775 * still take a timeout to move these.. but it can't
1776 * hurt to mark them.
1778 struct sctp_tmit_chunk *chk;
1780 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1781 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1782 chk->sent = SCTP_DATAGRAM_RESEND;
1783 sctp_flight_size_decrease(chk);
1784 sctp_total_flight_decrease(stcb, chk);
1785 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1791 /* process the INIT info (peer's info) */
1792 retval = sctp_process_init(init_cp, stcb);
1794 if (how_indx < sizeof(asoc->cookie_how))
1795 asoc->cookie_how[how_indx] = 9;
1798 if (sctp_load_addresses_from_init(stcb, m,
1799 init_offset + sizeof(struct sctp_init_chunk),
1800 initack_offset, src, dst, init_src)) {
1801 if (how_indx < sizeof(asoc->cookie_how))
1802 asoc->cookie_how[how_indx] = 10;
1805 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1806 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1807 *notification = SCTP_NOTIFY_ASSOC_UP;
1809 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1810 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1811 (inp->sctp_socket->so_qlimit == 0)) {
1812 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1816 stcb->sctp_ep->sctp_flags |=
1817 SCTP_PCB_FLAGS_CONNECTED;
1818 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1819 so = SCTP_INP_SO(stcb->sctp_ep);
1820 atomic_add_int(&stcb->asoc.refcnt, 1);
1821 SCTP_TCB_UNLOCK(stcb);
1822 SCTP_SOCKET_LOCK(so, 1);
1823 SCTP_TCB_LOCK(stcb);
1824 atomic_add_int(&stcb->asoc.refcnt, -1);
1825 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1826 SCTP_SOCKET_UNLOCK(so, 1);
1830 soisconnected(stcb->sctp_socket);
1831 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1832 SCTP_SOCKET_UNLOCK(so, 1);
1835 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1836 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1838 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1839 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1840 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1841 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1843 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1845 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1846 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1847 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1848 stcb->sctp_ep, stcb, asoc->primary_destination);
1850 sctp_stop_all_cookie_timers(stcb);
1851 sctp_toss_old_cookies(stcb, asoc);
1852 sctp_send_cookie_ack(stcb);
1855 * only if we have retrans set do we do this. What
1856 * this call does is get only the COOKIE-ACK out and
1857 * then when we return the normal call to
1858 * sctp_chunk_output will get the retrans out behind
1861 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1863 if (how_indx < sizeof(asoc->cookie_how))
1864 asoc->cookie_how[how_indx] = 11;
1868 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1869 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1870 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1871 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1872 cookie->tie_tag_peer_vtag != 0) {
1873 struct sctpasochead *head;
1875 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1880 if (asoc->peer_supports_nat) {
1882 * This is a gross gross hack. Just call the
1883 * cookie_new code since we are allowing a duplicate
1884 * association. I hope this works...
1886 return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
1887 sh, cookie, cookie_len,
1888 inp, netp, init_src, notification,
1889 auth_skipped, auth_offset, auth_len,
1890 use_mflowid, mflowid,
1894 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1897 if (how_indx < sizeof(asoc->cookie_how))
1898 asoc->cookie_how[how_indx] = 12;
1899 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1900 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1902 /* notify upper layer */
1903 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1904 atomic_add_int(&stcb->asoc.refcnt, 1);
1905 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1906 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1907 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1908 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1910 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1911 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1912 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1913 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1915 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1916 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1917 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1918 stcb->sctp_ep, stcb, asoc->primary_destination);
1920 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1921 /* move to OPEN state, if not in SHUTDOWN_SENT */
1922 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1924 asoc->pre_open_streams =
1925 ntohs(initack_cp->init.num_outbound_streams);
1926 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1927 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1928 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1930 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1932 asoc->str_reset_seq_in = asoc->init_seq_number;
1934 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1935 if (asoc->mapping_array) {
1936 memset(asoc->mapping_array, 0,
1937 asoc->mapping_array_size);
1939 if (asoc->nr_mapping_array) {
1940 memset(asoc->nr_mapping_array, 0,
1941 asoc->mapping_array_size);
1943 SCTP_TCB_UNLOCK(stcb);
1944 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1945 so = SCTP_INP_SO(stcb->sctp_ep);
1946 SCTP_SOCKET_LOCK(so, 1);
1948 SCTP_INP_INFO_WLOCK();
1949 SCTP_INP_WLOCK(stcb->sctp_ep);
1950 SCTP_TCB_LOCK(stcb);
1951 atomic_add_int(&stcb->asoc.refcnt, -1);
1952 /* send up all the data */
1953 SCTP_TCB_SEND_LOCK(stcb);
1955 sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_LOCKED);
1956 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1957 stcb->asoc.strmout[i].chunks_on_queues = 0;
1958 stcb->asoc.strmout[i].stream_no = i;
1959 stcb->asoc.strmout[i].next_sequence_send = 0;
1960 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1962 /* process the INIT-ACK info (my info) */
1963 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1964 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1966 /* pull from vtag hash */
1967 LIST_REMOVE(stcb, sctp_asocs);
1968 /* re-insert to new vtag position */
1969 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1970 SCTP_BASE_INFO(hashasocmark))];
1972 * put it in the bucket in the vtag hash of assoc's for the
1975 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1977 SCTP_TCB_SEND_UNLOCK(stcb);
1978 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1979 SCTP_INP_INFO_WUNLOCK();
1980 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1981 SCTP_SOCKET_UNLOCK(so, 1);
1983 asoc->total_flight = 0;
1984 asoc->total_flight_count = 0;
1985 /* process the INIT info (peer's info) */
1986 retval = sctp_process_init(init_cp, stcb);
1988 if (how_indx < sizeof(asoc->cookie_how))
1989 asoc->cookie_how[how_indx] = 13;
1994 * since we did not send a HB make sure we don't double
1997 net->hb_responded = 1;
1999 if (sctp_load_addresses_from_init(stcb, m,
2000 init_offset + sizeof(struct sctp_init_chunk),
2001 initack_offset, src, dst, init_src)) {
2002 if (how_indx < sizeof(asoc->cookie_how))
2003 asoc->cookie_how[how_indx] = 14;
2007 /* respond with a COOKIE-ACK */
2008 sctp_stop_all_cookie_timers(stcb);
2009 sctp_toss_old_cookies(stcb, asoc);
2010 sctp_send_cookie_ack(stcb);
2011 if (how_indx < sizeof(asoc->cookie_how))
2012 asoc->cookie_how[how_indx] = 15;
2016 if (how_indx < sizeof(asoc->cookie_how))
2017 asoc->cookie_how[how_indx] = 16;
2018 /* all other cases... */
2024 * handle a state cookie for a new association m: input packet mbuf chain--
2025 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
2026 * and the cookie signature does not exist offset: offset into mbuf to the
2027 * cookie-echo chunk length: length of the cookie chunk to: where the init
2028 * was from returns a new TCB
2030 static struct sctp_tcb *
2031 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
2032 struct sockaddr *src, struct sockaddr *dst,
2033 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
2034 struct sctp_inpcb *inp, struct sctp_nets **netp,
2035 struct sockaddr *init_src, int *notification,
2036 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2037 uint8_t use_mflowid, uint32_t mflowid,
2038 uint32_t vrf_id, uint16_t port)
2040 struct sctp_tcb *stcb;
2041 struct sctp_init_chunk *init_cp, init_buf;
2042 struct sctp_init_ack_chunk *initack_cp, initack_buf;
2043 struct sockaddr_storage sa_store;
2044 struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
2045 struct sctp_association *asoc;
2046 int init_offset, initack_offset, initack_limit;
2049 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
2052 struct sockaddr_in *sin;
2056 struct sockaddr_in6 *sin6;
2059 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2062 so = SCTP_INP_SO(inp);
2066 * find and validate the INIT chunk in the cookie (peer's info) the
2067 * INIT should start after the cookie-echo header struct (chunk
2068 * header, state cookie header struct)
2070 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2071 init_cp = (struct sctp_init_chunk *)
2072 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2073 (uint8_t *) & init_buf);
2074 if (init_cp == NULL) {
2075 /* could not pull a INIT chunk in cookie */
2076 SCTPDBG(SCTP_DEBUG_INPUT1,
2077 "process_cookie_new: could not pull INIT chunk hdr\n");
2080 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2081 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2084 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2086 * find and validate the INIT-ACK chunk in the cookie (my info) the
2087 * INIT-ACK follows the INIT chunk
2089 initack_cp = (struct sctp_init_ack_chunk *)
2090 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2091 (uint8_t *) & initack_buf);
2092 if (initack_cp == NULL) {
2093 /* could not pull INIT-ACK chunk in cookie */
2094 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2097 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2101 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2102 * "initack_limit" value. This is because the chk_length field
2103 * includes the length of the cookie, but the cookie is omitted when
2104 * the INIT and INIT_ACK are tacked onto the cookie...
2106 initack_limit = offset + cookie_len;
2109 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2114 * Here we do a trick, we set in NULL for the proc/thread argument.
2115 * We do this since in effect we only use the p argument when the
2116 * socket is unbound and we must do an implicit bind. Since we are
2117 * getting a cookie, we cannot be unbound.
2119 stcb = sctp_aloc_assoc(inp, init_src, &error,
2120 ntohl(initack_cp->init.initiate_tag), vrf_id,
2121 (struct thread *)NULL
2124 struct mbuf *op_err;
2126 /* memory problem? */
2127 SCTPDBG(SCTP_DEBUG_INPUT1,
2128 "process_cookie_new: no room for another TCB!\n");
2129 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2131 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2132 src, dst, sh, op_err,
2133 use_mflowid, mflowid,
2137 /* get the correct sctp_nets */
2139 *netp = sctp_findnet(stcb, init_src);
2142 /* get scope variables out of cookie */
2143 asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2144 asoc->scope.site_scope = cookie->site_scope;
2145 asoc->scope.local_scope = cookie->local_scope;
2146 asoc->scope.loopback_scope = cookie->loopback_scope;
2148 if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2149 (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2150 struct mbuf *op_err;
2153 * Houston we have a problem. The EP changed while the
2154 * cookie was in flight. Only recourse is to abort the
2157 atomic_add_int(&stcb->asoc.refcnt, 1);
2158 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2159 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2160 src, dst, sh, op_err,
2161 use_mflowid, mflowid,
2163 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2164 SCTP_TCB_UNLOCK(stcb);
2165 SCTP_SOCKET_LOCK(so, 1);
2166 SCTP_TCB_LOCK(stcb);
2168 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2169 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2170 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2171 SCTP_SOCKET_UNLOCK(so, 1);
2173 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2176 /* process the INIT-ACK info (my info) */
2177 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2178 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2179 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2180 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2181 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2182 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2183 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2184 asoc->str_reset_seq_in = asoc->init_seq_number;
2186 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2188 /* process the INIT info (peer's info) */
2190 retval = sctp_process_init(init_cp, stcb);
2194 atomic_add_int(&stcb->asoc.refcnt, 1);
2195 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2196 SCTP_TCB_UNLOCK(stcb);
2197 SCTP_SOCKET_LOCK(so, 1);
2198 SCTP_TCB_LOCK(stcb);
2200 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2201 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2202 SCTP_SOCKET_UNLOCK(so, 1);
2204 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2207 /* load all addresses */
2208 if (sctp_load_addresses_from_init(stcb, m,
2209 init_offset + sizeof(struct sctp_init_chunk), initack_offset,
2210 src, dst, init_src)) {
2211 atomic_add_int(&stcb->asoc.refcnt, 1);
2212 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2213 SCTP_TCB_UNLOCK(stcb);
2214 SCTP_SOCKET_LOCK(so, 1);
2215 SCTP_TCB_LOCK(stcb);
2217 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
2218 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2219 SCTP_SOCKET_UNLOCK(so, 1);
2221 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2225 * verify any preceding AUTH chunk that was skipped
2227 /* pull the local authentication parameters from the cookie/init-ack */
2228 sctp_auth_get_cookie_params(stcb, m,
2229 initack_offset + sizeof(struct sctp_init_ack_chunk),
2230 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2232 struct sctp_auth_chunk *auth;
2234 auth = (struct sctp_auth_chunk *)
2235 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2236 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2237 /* auth HMAC failed, dump the assoc and packet */
2238 SCTPDBG(SCTP_DEBUG_AUTH1,
2239 "COOKIE-ECHO: AUTH failed\n");
2240 atomic_add_int(&stcb->asoc.refcnt, 1);
2241 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2242 SCTP_TCB_UNLOCK(stcb);
2243 SCTP_SOCKET_LOCK(so, 1);
2244 SCTP_TCB_LOCK(stcb);
2246 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2247 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2248 SCTP_SOCKET_UNLOCK(so, 1);
2250 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2253 /* remaining chunks checked... good to go */
2254 stcb->asoc.authenticated = 1;
2257 /* update current state */
2258 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2259 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2260 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2261 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2262 stcb->sctp_ep, stcb, asoc->primary_destination);
2264 sctp_stop_all_cookie_timers(stcb);
2265 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2266 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2269 * if we're doing ASCONFs, check to see if we have any new local
2270 * addresses that need to get added to the peer (eg. addresses
2271 * changed while cookie echo in flight). This needs to be done
2272 * after we go to the OPEN state to do the correct asconf
2273 * processing. else, make sure we have the correct addresses in our
2277 /* warning, we re-use sin, sin6, sa_store here! */
2278 /* pull in local_address (our "from" address) */
2279 switch (cookie->laddr_type) {
2281 case SCTP_IPV4_ADDRESS:
2282 /* source addr is IPv4 */
2283 sin = (struct sockaddr_in *)initack_src;
2284 memset(sin, 0, sizeof(*sin));
2285 sin->sin_family = AF_INET;
2286 sin->sin_len = sizeof(struct sockaddr_in);
2287 sin->sin_addr.s_addr = cookie->laddress[0];
2291 case SCTP_IPV6_ADDRESS:
2292 /* source addr is IPv6 */
2293 sin6 = (struct sockaddr_in6 *)initack_src;
2294 memset(sin6, 0, sizeof(*sin6));
2295 sin6->sin6_family = AF_INET6;
2296 sin6->sin6_len = sizeof(struct sockaddr_in6);
2297 sin6->sin6_scope_id = cookie->scope_id;
2298 memcpy(&sin6->sin6_addr, cookie->laddress,
2299 sizeof(sin6->sin6_addr));
2303 atomic_add_int(&stcb->asoc.refcnt, 1);
2304 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2305 SCTP_TCB_UNLOCK(stcb);
2306 SCTP_SOCKET_LOCK(so, 1);
2307 SCTP_TCB_LOCK(stcb);
2309 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2310 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2311 SCTP_SOCKET_UNLOCK(so, 1);
2313 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2317 /* set up to notify upper layer */
2318 *notification = SCTP_NOTIFY_ASSOC_UP;
2319 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2320 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2321 (inp->sctp_socket->so_qlimit == 0)) {
2323 * This is an endpoint that called connect() how it got a
2324 * cookie that is NEW is a bit of a mystery. It must be that
2325 * the INIT was sent, but before it got there.. a complete
2326 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2327 * should have went to the other code.. not here.. oh well..
2328 * a bit of protection is worth having..
2330 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2331 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2332 atomic_add_int(&stcb->asoc.refcnt, 1);
2333 SCTP_TCB_UNLOCK(stcb);
2334 SCTP_SOCKET_LOCK(so, 1);
2335 SCTP_TCB_LOCK(stcb);
2336 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2337 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2338 SCTP_SOCKET_UNLOCK(so, 1);
2342 soisconnected(stcb->sctp_socket);
2343 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2344 SCTP_SOCKET_UNLOCK(so, 1);
2346 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2347 (inp->sctp_socket->so_qlimit)) {
2349 * We don't want to do anything with this one. Since it is
2350 * the listening guy. The timer will get started for
2351 * accepted connections in the caller.
2355 /* since we did not send a HB make sure we don't double things */
2356 if ((netp) && (*netp))
2357 (*netp)->hb_responded = 1;
2359 if (stcb->asoc.sctp_autoclose_ticks &&
2360 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2361 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2363 /* calculate the RTT */
2364 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2365 if ((netp) && (*netp)) {
2366 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2367 &cookie->time_entered, sctp_align_unsafe_makecopy,
2368 SCTP_RTT_FROM_NON_DATA);
2370 /* respond with a COOKIE-ACK */
2371 sctp_send_cookie_ack(stcb);
2374 * check the address lists for any ASCONFs that need to be sent
2375 * AFTER the cookie-ack is sent
2377 sctp_check_address_list(stcb, m,
2378 initack_offset + sizeof(struct sctp_init_ack_chunk),
2379 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2380 initack_src, cookie->local_scope, cookie->site_scope,
2381 cookie->ipv4_scope, cookie->loopback_scope);
2388 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2389 * we NEED to make sure we are not already using the vtag. If so we
2390 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2391 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2392 SCTP_BASE_INFO(hashasocmark))];
2393 LIST_FOREACH(stcb, head, sctp_asocs) {
2394 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2395 -- SEND ABORT - TRY AGAIN --
2401 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2402 * existing (non-NULL) TCB
2404 static struct mbuf *
2405 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2406 struct sockaddr *src, struct sockaddr *dst,
2407 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2408 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2409 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2410 struct sctp_tcb **locked_tcb,
2411 uint8_t use_mflowid, uint32_t mflowid,
2412 uint32_t vrf_id, uint16_t port)
2414 struct sctp_state_cookie *cookie;
2415 struct sctp_tcb *l_stcb = *stcb;
2416 struct sctp_inpcb *l_inp;
2417 struct sockaddr *to;
2418 struct sctp_pcb *ep;
2420 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2422 uint8_t cookie_ok = 0;
2423 unsigned int sig_offset, cookie_offset;
2424 unsigned int cookie_len;
2426 struct timeval time_expires;
2427 int notification = 0;
2428 struct sctp_nets *netl;
2429 int had_a_existing_tcb = 0;
2430 int send_int_conf = 0;
2433 struct sockaddr_in sin;
2437 struct sockaddr_in6 sin6;
2441 SCTPDBG(SCTP_DEBUG_INPUT2,
2442 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2444 if (inp_p == NULL) {
2447 cookie = &cp->cookie;
2448 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2449 cookie_len = ntohs(cp->ch.chunk_length);
2451 if ((cookie->peerport != sh->src_port) &&
2452 (cookie->myport != sh->dest_port) &&
2453 (cookie->my_vtag != sh->v_tag)) {
2455 * invalid ports or bad tag. Note that we always leave the
2456 * v_tag in the header in network order and when we stored
2457 * it in the my_vtag slot we also left it in network order.
2458 * This maintains the match even though it may be in the
2459 * opposite byte order of the machine :->
2463 if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2464 sizeof(struct sctp_init_chunk) +
2465 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2466 /* cookie too small */
2470 * split off the signature into its own mbuf (since it should not be
2471 * calculated in the sctp_hmac_m() call).
2473 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2474 m_sig = m_split(m, sig_offset, M_DONTWAIT);
2475 if (m_sig == NULL) {
2476 /* out of memory or ?? */
2479 #ifdef SCTP_MBUF_LOGGING
2480 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2483 for (mat = m_sig; mat; mat = SCTP_BUF_NEXT(mat)) {
2484 if (SCTP_BUF_IS_EXTENDED(mat)) {
2485 sctp_log_mb(mat, SCTP_MBUF_SPLIT);
2492 * compute the signature/digest for the cookie
2494 ep = &(*inp_p)->sctp_ep;
2497 SCTP_TCB_UNLOCK(l_stcb);
2499 SCTP_INP_RLOCK(l_inp);
2501 SCTP_TCB_LOCK(l_stcb);
2503 /* which cookie is it? */
2504 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2505 (ep->current_secret_number != ep->last_secret_number)) {
2506 /* it's the old cookie */
2507 (void)sctp_hmac_m(SCTP_HMAC,
2508 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2509 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2511 /* it's the current cookie */
2512 (void)sctp_hmac_m(SCTP_HMAC,
2513 (uint8_t *) ep->secret_key[(int)ep->current_secret_number],
2514 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2516 /* get the signature */
2517 SCTP_INP_RUNLOCK(l_inp);
2518 sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig);
2520 /* couldn't find signature */
2521 sctp_m_freem(m_sig);
2524 /* compare the received digest with the computed digest */
2525 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2526 /* try the old cookie? */
2527 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2528 (ep->current_secret_number != ep->last_secret_number)) {
2529 /* compute digest with old */
2530 (void)sctp_hmac_m(SCTP_HMAC,
2531 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2532 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2534 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2542 * Now before we continue we must reconstruct our mbuf so that
2543 * normal processing of any other chunks will work.
2549 while (SCTP_BUF_NEXT(m_at) != NULL) {
2550 m_at = SCTP_BUF_NEXT(m_at);
2552 SCTP_BUF_NEXT(m_at) = m_sig;
2555 if (cookie_ok == 0) {
2556 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2557 SCTPDBG(SCTP_DEBUG_INPUT2,
2558 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2559 (uint32_t) offset, cookie_offset, sig_offset);
2563 * check the cookie timestamps to be sure it's not stale
2565 (void)SCTP_GETTIME_TIMEVAL(&now);
2566 /* Expire time is in Ticks, so we convert to seconds */
2567 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2568 time_expires.tv_usec = cookie->time_entered.tv_usec;
2570 * TODO sctp_constants.h needs alternative time macros when _KERNEL
2573 if (timevalcmp(&now, &time_expires, >)) {
2574 /* cookie is stale! */
2575 struct mbuf *op_err;
2576 struct sctp_stale_cookie_msg *scm;
2579 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg),
2580 0, M_DONTWAIT, 1, MT_DATA);
2581 if (op_err == NULL) {
2586 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg);
2587 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
2588 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
2589 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
2590 (sizeof(uint32_t))));
2591 /* seconds to usec */
2592 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2595 tim = now.tv_usec - cookie->time_entered.tv_usec;
2596 scm->time_usec = htonl(tim);
2597 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2598 use_mflowid, mflowid,
2603 * Now we must see with the lookup address if we have an existing
2604 * asoc. This will only happen if we were in the COOKIE-WAIT state
2605 * and a INIT collided with us and somewhere the peer sent the
2606 * cookie on another address besides the single address our assoc
2607 * had for him. In this case we will have one of the tie-tags set at
2608 * least AND the address field in the cookie can be used to look it
2612 switch (cookie->addr_type) {
2614 case SCTP_IPV6_ADDRESS:
2615 memset(&sin6, 0, sizeof(sin6));
2616 sin6.sin6_family = AF_INET6;
2617 sin6.sin6_len = sizeof(sin6);
2618 sin6.sin6_port = sh->src_port;
2619 sin6.sin6_scope_id = cookie->scope_id;
2620 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2621 sizeof(sin6.sin6_addr.s6_addr));
2622 to = (struct sockaddr *)&sin6;
2626 case SCTP_IPV4_ADDRESS:
2627 memset(&sin, 0, sizeof(sin));
2628 sin.sin_family = AF_INET;
2629 sin.sin_len = sizeof(sin);
2630 sin.sin_port = sh->src_port;
2631 sin.sin_addr.s_addr = cookie->address[0];
2632 to = (struct sockaddr *)&sin;
2636 /* This should not happen */
2639 if ((*stcb == NULL) && to) {
2640 /* Yep, lets check */
2641 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2642 if (*stcb == NULL) {
2644 * We should have only got back the same inp. If we
2645 * got back a different ep we have a problem. The
2646 * original findep got back l_inp and now
2648 if (l_inp != *inp_p) {
2649 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2652 if (*locked_tcb == NULL) {
2654 * In this case we found the assoc only
2655 * after we locked the create lock. This
2656 * means we are in a colliding case and we
2657 * must make sure that we unlock the tcb if
2658 * its one of the cases where we throw away
2659 * the incoming packets.
2661 *locked_tcb = *stcb;
2664 * We must also increment the inp ref count
2665 * since the ref_count flags was set when we
2666 * did not find the TCB, now we found it
2667 * which reduces the refcount.. we must
2668 * raise it back out to balance it all :-)
2670 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2671 if ((*stcb)->sctp_ep != l_inp) {
2672 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2673 (void *)(*stcb)->sctp_ep, (void *)l_inp);
2681 cookie_len -= SCTP_SIGNATURE_SIZE;
2682 if (*stcb == NULL) {
2683 /* this is the "normal" case... get a new TCB */
2684 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2685 cookie, cookie_len, *inp_p,
2686 netp, to, ¬ification,
2687 auth_skipped, auth_offset, auth_len,
2688 use_mflowid, mflowid,
2691 /* this is abnormal... cookie-echo on existing TCB */
2692 had_a_existing_tcb = 1;
2693 *stcb = sctp_process_cookie_existing(m, iphlen, offset,
2695 cookie, cookie_len, *inp_p, *stcb, netp, to,
2696 ¬ification, auth_skipped, auth_offset, auth_len,
2697 use_mflowid, mflowid,
2701 if (*stcb == NULL) {
2702 /* still no TCB... must be bad cookie-echo */
2705 if ((*netp != NULL) && (use_mflowid != 0)) {
2706 (*netp)->flowid = mflowid;
2708 (*netp)->flowidset = 1;
2712 * Ok, we built an association so confirm the address we sent the
2715 netl = sctp_findnet(*stcb, to);
2717 * This code should in theory NOT run but
2720 /* TSNH! Huh, why do I need to add this address here? */
2721 if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2724 netl = sctp_findnet(*stcb, to);
2727 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2728 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2729 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2734 sctp_start_net_timers(*stcb);
2735 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2736 if (!had_a_existing_tcb ||
2737 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2739 * If we have a NEW cookie or the connect never
2740 * reached the connected state during collision we
2741 * must do the TCP accept thing.
2743 struct socket *so, *oso;
2744 struct sctp_inpcb *inp;
2746 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2748 * For a restart we will keep the same
2749 * socket, no need to do anything. I THINK!!
2751 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2752 if (send_int_conf) {
2753 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2754 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2758 oso = (*inp_p)->sctp_socket;
2759 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2760 SCTP_TCB_UNLOCK((*stcb));
2761 CURVNET_SET(oso->so_vnet);
2762 so = sonewconn(oso, 0
2765 SCTP_TCB_LOCK((*stcb));
2766 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2769 struct mbuf *op_err;
2771 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2772 struct socket *pcb_so;
2775 /* Too many sockets */
2776 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2777 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2778 sctp_abort_association(*inp_p, NULL, m, iphlen,
2779 src, dst, sh, op_err,
2780 use_mflowid, mflowid,
2782 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2783 pcb_so = SCTP_INP_SO(*inp_p);
2784 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2785 SCTP_TCB_UNLOCK((*stcb));
2786 SCTP_SOCKET_LOCK(pcb_so, 1);
2787 SCTP_TCB_LOCK((*stcb));
2788 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2790 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2791 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2792 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2796 inp = (struct sctp_inpcb *)so->so_pcb;
2797 SCTP_INP_INCR_REF(inp);
2799 * We add the unbound flag here so that if we get an
2800 * soabort() before we get the move_pcb done, we
2801 * will properly cleanup.
2803 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2804 SCTP_PCB_FLAGS_CONNECTED |
2805 SCTP_PCB_FLAGS_IN_TCPPOOL |
2806 SCTP_PCB_FLAGS_UNBOUND |
2807 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2808 SCTP_PCB_FLAGS_DONT_WAKE);
2809 inp->sctp_features = (*inp_p)->sctp_features;
2810 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2811 inp->sctp_socket = so;
2812 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2813 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2814 inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable;
2815 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2816 inp->sctp_context = (*inp_p)->sctp_context;
2817 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2818 inp->inp_starting_point_for_iterator = NULL;
2820 * copy in the authentication parameters from the
2823 if (inp->sctp_ep.local_hmacs)
2824 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2825 inp->sctp_ep.local_hmacs =
2826 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2827 if (inp->sctp_ep.local_auth_chunks)
2828 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2829 inp->sctp_ep.local_auth_chunks =
2830 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2833 * Now we must move it from one hash table to
2834 * another and get the tcb in the right place.
2838 * This is where the one-2-one socket is put into
2839 * the accept state waiting for the accept!
2842 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2844 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2846 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2847 SCTP_TCB_UNLOCK((*stcb));
2849 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2851 SCTP_TCB_LOCK((*stcb));
2852 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2856 * now we must check to see if we were aborted while
2857 * the move was going on and the lock/unlock
2860 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2862 * yep it was, we leave the assoc attached
2863 * to the socket since the sctp_inpcb_free()
2864 * call will send an abort for us.
2866 SCTP_INP_DECR_REF(inp);
2869 SCTP_INP_DECR_REF(inp);
2870 /* Switch over to the new guy */
2872 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2873 if (send_int_conf) {
2874 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2875 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2878 * Pull it from the incomplete queue and wake the
2881 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2882 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2883 SCTP_TCB_UNLOCK((*stcb));
2884 SCTP_SOCKET_LOCK(so, 1);
2887 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2888 SCTP_TCB_LOCK((*stcb));
2889 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2890 SCTP_SOCKET_UNLOCK(so, 1);
2896 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2898 if (send_int_conf) {
2899 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2900 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2906 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2907 struct sctp_tcb *stcb, struct sctp_nets *net)
2909 /* cp must not be used, others call this without a c-ack :-) */
2910 struct sctp_association *asoc;
2912 SCTPDBG(SCTP_DEBUG_INPUT2,
2913 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2919 sctp_stop_all_cookie_timers(stcb);
2920 /* process according to association state */
2921 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2922 /* state change only needed when I am in right state */
2923 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2924 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2925 sctp_start_net_timers(stcb);
2926 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2927 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2928 stcb->sctp_ep, stcb, asoc->primary_destination);
2932 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2933 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2934 if (asoc->overall_error_count == 0) {
2935 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2936 &asoc->time_entered, sctp_align_safe_nocopy,
2937 SCTP_RTT_FROM_NON_DATA);
2939 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2940 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2941 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2942 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2943 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2947 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2948 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2949 so = SCTP_INP_SO(stcb->sctp_ep);
2950 atomic_add_int(&stcb->asoc.refcnt, 1);
2951 SCTP_TCB_UNLOCK(stcb);
2952 SCTP_SOCKET_LOCK(so, 1);
2953 SCTP_TCB_LOCK(stcb);
2954 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2956 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2957 soisconnected(stcb->sctp_socket);
2959 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2960 SCTP_SOCKET_UNLOCK(so, 1);
2964 * since we did not send a HB make sure we don't double
2967 net->hb_responded = 1;
2969 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2971 * We don't need to do the asconf thing, nor hb or
2972 * autoclose if the socket is closed.
2976 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2980 if (stcb->asoc.sctp_autoclose_ticks &&
2981 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2982 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2983 stcb->sctp_ep, stcb, NULL);
2986 * send ASCONF if parameters are pending and ASCONFs are
2987 * allowed (eg. addresses changed when init/cookie echo were
2990 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2991 (stcb->asoc.peer_supports_asconf) &&
2992 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2993 #ifdef SCTP_TIMER_BASED_ASCONF
2994 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2995 stcb->sctp_ep, stcb,
2996 stcb->asoc.primary_destination);
2998 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2999 SCTP_ADDR_NOT_LOCKED);
3004 /* Toss the cookie if I can */
3005 sctp_toss_old_cookies(stcb, asoc);
3006 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3007 /* Restart the timer if we have pending data */
3008 struct sctp_tmit_chunk *chk;
3010 chk = TAILQ_FIRST(&asoc->sent_queue);
3011 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
3016 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
3017 struct sctp_tcb *stcb)
3019 struct sctp_nets *net;
3020 struct sctp_tmit_chunk *lchk;
3021 struct sctp_ecne_chunk bkup;
3022 uint8_t override_bit;
3023 uint32_t tsn, window_data_tsn;
3025 unsigned int pkt_cnt;
3027 len = ntohs(cp->ch.chunk_length);
3028 if ((len != sizeof(struct sctp_ecne_chunk)) &&
3029 (len != sizeof(struct old_sctp_ecne_chunk))) {
3032 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3033 /* Its the old format */
3034 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3035 bkup.num_pkts_since_cwr = htonl(1);
3038 SCTP_STAT_INCR(sctps_recvecne);
3039 tsn = ntohl(cp->tsn);
3040 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3041 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3043 window_data_tsn = stcb->asoc.sending_seq - 1;
3045 window_data_tsn = lchk->rec.data.TSN_seq;
3048 /* Find where it was sent to if possible. */
3050 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3051 if (lchk->rec.data.TSN_seq == tsn) {
3053 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3056 if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) {
3062 * What to do. A previous send of a CWR was possibly lost.
3063 * See how old it is, we may have it marked on the actual
3066 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3067 if (tsn == net->last_cwr_tsn) {
3068 /* Found him, send it off */
3074 * If we reach here, we need to send a special CWR
3075 * that says hey, we did this a long time ago and
3076 * you lost the response.
3078 net = TAILQ_FIRST(&stcb->asoc.nets);
3083 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3090 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3091 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3093 * JRS - Use the congestion control given in the pluggable
3096 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3098 * We reduce once every RTT. So we will only lower cwnd at
3099 * the next sending seq i.e. the window_data_tsn
3101 net->cwr_window_tsn = window_data_tsn;
3102 net->ecn_ce_pkt_cnt += pkt_cnt;
3103 net->lost_cnt = pkt_cnt;
3104 net->last_cwr_tsn = tsn;
3106 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3107 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3108 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3110 * Another loss in the same window update how many
3111 * marks/packets lost we have had.
3115 if (pkt_cnt > net->lost_cnt) {
3116 /* Should be the case */
3117 cnt = (pkt_cnt - net->lost_cnt);
3118 net->ecn_ce_pkt_cnt += cnt;
3120 net->lost_cnt = pkt_cnt;
3121 net->last_cwr_tsn = tsn;
3123 * Most CC functions will ignore this call, since we
3124 * are in-window yet of the initial CE the peer saw.
3126 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3130 * We always send a CWR this way if our previous one was lost our
3131 * peer will get an update, or if it is not time again to reduce we
3132 * still get the cwr to the peer. Note we set the override when we
3133 * could not find the TSN on the chunk or the destination network.
3135 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3139 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3142 * Here we get a CWR from the peer. We must look in the outqueue and
3143 * make sure that we have a covered ECNE in the control chunk part.
3146 struct sctp_tmit_chunk *chk;
3147 struct sctp_ecne_chunk *ecne;
3151 cwr_tsn = ntohl(cp->tsn);
3153 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3154 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
3155 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3158 if ((override == 0) && (chk->whoTo != net)) {
3159 /* Must be from the right src unless override is set */
3162 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3163 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3164 /* this covers this ECNE, we can remove it */
3165 stcb->asoc.ecn_echo_cnt_onq--;
3166 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3169 sctp_m_freem(chk->data);
3172 stcb->asoc.ctrl_queue_cnt--;
3173 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3174 if (override == 0) {
3182 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3183 struct sctp_tcb *stcb, struct sctp_nets *net)
3185 struct sctp_association *asoc;
3187 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3192 SCTPDBG(SCTP_DEBUG_INPUT2,
3193 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3198 /* process according to association state */
3199 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3200 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3201 SCTPDBG(SCTP_DEBUG_INPUT2,
3202 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3203 SCTP_TCB_UNLOCK(stcb);
3206 /* notify upper layer protocol */
3207 if (stcb->sctp_socket) {
3208 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3211 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3212 !TAILQ_EMPTY(&asoc->sent_queue) ||
3213 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
3214 panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3217 /* stop the timer */
3218 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
3219 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3221 SCTPDBG(SCTP_DEBUG_INPUT2,
3222 "sctp_handle_shutdown_complete: calls free-asoc\n");
3223 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3224 so = SCTP_INP_SO(stcb->sctp_ep);
3225 atomic_add_int(&stcb->asoc.refcnt, 1);
3226 SCTP_TCB_UNLOCK(stcb);
3227 SCTP_SOCKET_LOCK(so, 1);
3228 SCTP_TCB_LOCK(stcb);
3229 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3231 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
3232 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3233 SCTP_SOCKET_UNLOCK(so, 1);
3239 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3240 struct sctp_nets *net, uint8_t flg)
3242 switch (desc->chunk_type) {
3244 /* find the tsn to resend (possibly */
3247 struct sctp_tmit_chunk *tp1;
3249 tsn = ntohl(desc->tsn_ifany);
3250 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3251 if (tp1->rec.data.TSN_seq == tsn) {
3255 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) {
3263 * Do it the other way , aka without paying
3264 * attention to queue seq order.
3266 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3267 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3268 if (tp1->rec.data.TSN_seq == tsn) {
3275 SCTP_STAT_INCR(sctps_pdrptsnnf);
3277 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3280 if (((flg & SCTP_BADCRC) == 0) &&
3281 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3284 if ((stcb->asoc.peers_rwnd == 0) &&
3285 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3286 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3289 if (stcb->asoc.peers_rwnd == 0 &&
3290 (flg & SCTP_FROM_MIDDLE_BOX)) {
3291 SCTP_STAT_INCR(sctps_pdrpdizrw);
3294 ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+
3295 sizeof(struct sctp_data_chunk));
3299 for (iii = 0; iii < sizeof(desc->data_bytes);
3301 if (ddp[iii] != desc->data_bytes[iii]) {
3302 SCTP_STAT_INCR(sctps_pdrpbadd);
3310 * this guy had a RTO calculation
3311 * pending on it, cancel it
3313 if (tp1->whoTo->rto_needed == 0) {
3314 tp1->whoTo->rto_needed = 1;
3318 SCTP_STAT_INCR(sctps_pdrpmark);
3319 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3320 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3322 * mark it as if we were doing a FR, since
3323 * we will be getting gap ack reports behind
3324 * the info from the router.
3326 tp1->rec.data.doing_fast_retransmit = 1;
3328 * mark the tsn with what sequences can
3331 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3332 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3334 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
3337 /* restart the timer */
3338 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3339 stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3340 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3343 /* fix counts and things */
3344 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3345 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3346 tp1->whoTo->flight_size,
3349 tp1->rec.data.TSN_seq);
3351 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3352 sctp_flight_size_decrease(tp1);
3353 sctp_total_flight_decrease(stcb, tp1);
3355 tp1->sent = SCTP_DATAGRAM_RESEND;
3361 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3362 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3365 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3367 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3370 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3371 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3372 audit, stcb->asoc.sent_queue_retran_cnt);
3373 #ifndef SCTP_AUDITING_ENABLED
3374 stcb->asoc.sent_queue_retran_cnt = audit;
3382 struct sctp_tmit_chunk *asconf;
3384 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3386 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3391 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3392 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3393 asconf->sent = SCTP_DATAGRAM_RESEND;
3394 asconf->snd_count--;
3398 case SCTP_INITIATION:
3399 /* resend the INIT */
3400 stcb->asoc.dropped_special_cnt++;
3401 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3403 * If we can get it in, in a few attempts we do
3404 * this, otherwise we let the timer fire.
3406 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3407 stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3408 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3411 case SCTP_SELECTIVE_ACK:
3412 case SCTP_NR_SELECTIVE_ACK:
3413 /* resend the sack */
3414 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3416 case SCTP_HEARTBEAT_REQUEST:
3417 /* resend a demand HB */
3418 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3420 * Only retransmit if we KNOW we wont destroy the
3423 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3427 sctp_send_shutdown(stcb, net);
3429 case SCTP_SHUTDOWN_ACK:
3430 sctp_send_shutdown_ack(stcb, net);
3432 case SCTP_COOKIE_ECHO:
3434 struct sctp_tmit_chunk *cookie;
3437 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3439 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3444 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3445 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3446 cookie->sent = SCTP_DATAGRAM_RESEND;
3447 sctp_stop_all_cookie_timers(stcb);
3451 case SCTP_COOKIE_ACK:
3452 sctp_send_cookie_ack(stcb);
3454 case SCTP_ASCONF_ACK:
3455 /* resend last asconf ack */
3456 sctp_send_asconf_ack(stcb);
3458 case SCTP_FORWARD_CUM_TSN:
3459 send_forward_tsn(stcb, &stcb->asoc);
3461 /* can't do anything with these */
3462 case SCTP_PACKET_DROPPED:
3463 case SCTP_INITIATION_ACK: /* this should not happen */
3464 case SCTP_HEARTBEAT_ACK:
3465 case SCTP_ABORT_ASSOCIATION:
3466 case SCTP_OPERATION_ERROR:
3467 case SCTP_SHUTDOWN_COMPLETE:
3477 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t * list)
3483 * We set things to 0xffff since this is the last delivered sequence
3484 * and we will be sending in 0 after the reset.
3487 if (number_entries) {
3488 for (i = 0; i < number_entries; i++) {
3489 temp = ntohs(list[i]);
3490 if (temp >= stcb->asoc.streamincnt) {
3493 stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff;
3497 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3498 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3501 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3505 sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t * list)
3510 if (number_entries > 0) {
3511 for (i = 0; i < number_entries; i++) {
3512 temp = ntohs(list[i]);
3513 if (temp >= stcb->asoc.streamoutcnt) {
3514 /* no such stream */
3517 stcb->asoc.strmout[temp].next_sequence_send = 0;
3520 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3521 stcb->asoc.strmout[i].next_sequence_send = 0;
3524 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3528 struct sctp_stream_reset_out_request *
3529 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3531 struct sctp_association *asoc;
3532 struct sctp_chunkhdr *ch;
3533 struct sctp_stream_reset_out_request *r;
3534 struct sctp_tmit_chunk *chk;
3538 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3539 asoc->stream_reset_outstanding = 0;
3542 if (stcb->asoc.str_reset == NULL) {
3543 asoc->stream_reset_outstanding = 0;
3546 chk = stcb->asoc.str_reset;
3547 if (chk->data == NULL) {
3551 /* he wants a copy of the chk pointer */
3554 clen = chk->send_size;
3555 ch = mtod(chk->data, struct sctp_chunkhdr *);
3556 r = (struct sctp_stream_reset_out_request *)(ch + 1);
3557 if (ntohl(r->request_seq) == seq) {
3561 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3562 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3563 /* move to the next one, there can only be a max of two */
3564 r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len);
3565 if (ntohl(r->request_seq) == seq) {
3569 /* that seq is not here */
3574 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3576 struct sctp_association *asoc;
3577 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3579 if (stcb->asoc.str_reset == NULL) {
3584 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3585 TAILQ_REMOVE(&asoc->control_send_queue,
3589 sctp_m_freem(chk->data);
3592 asoc->ctrl_queue_cnt--;
3593 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3594 /* sa_ignore NO_NULL_CHK */
3595 stcb->asoc.str_reset = NULL;
3600 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3601 uint32_t seq, uint32_t action,
3602 struct sctp_stream_reset_response *respin)
3606 struct sctp_association *asoc = &stcb->asoc;
3607 struct sctp_tmit_chunk *chk;
3608 struct sctp_stream_reset_out_request *srparam;
3609 uint32_t number_entries;
3611 if (asoc->stream_reset_outstanding == 0) {
3615 if (seq == stcb->asoc.str_reset_seq_out) {
3616 srparam = sctp_find_stream_reset(stcb, seq, &chk);
3618 stcb->asoc.str_reset_seq_out++;
3619 type = ntohs(srparam->ph.param_type);
3620 lparm_len = ntohs(srparam->ph.param_length);
3621 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3622 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3623 asoc->stream_reset_out_is_outstanding = 0;
3624 if (asoc->stream_reset_outstanding)
3625 asoc->stream_reset_outstanding--;
3626 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3628 sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams);
3629 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3630 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3632 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3634 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3635 /* Answered my request */
3636 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3637 if (asoc->stream_reset_outstanding)
3638 asoc->stream_reset_outstanding--;
3639 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3640 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3641 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3642 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3643 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3644 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3646 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3647 /* Ok we now may have more streams */
3650 num_stream = stcb->asoc.strm_pending_add_size;
3651 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3653 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3655 stcb->asoc.strm_pending_add_size = 0;
3656 if (asoc->stream_reset_outstanding)
3657 asoc->stream_reset_outstanding--;
3658 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3659 /* Put the new streams into effect */
3660 stcb->asoc.streamoutcnt += num_stream;
3661 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3662 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3663 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3664 SCTP_STREAM_CHANGE_DENIED);
3666 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3667 SCTP_STREAM_CHANGE_FAILED);
3669 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3670 if (asoc->stream_reset_outstanding)
3671 asoc->stream_reset_outstanding--;
3672 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3673 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3674 SCTP_STREAM_CHANGE_DENIED);
3675 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3676 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3677 SCTP_STREAM_CHANGE_FAILED);
3679 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3681 * a) Adopt the new in tsn.
3683 * c) Adopt the new out-tsn
3685 struct sctp_stream_reset_response_tsn *resp;
3686 struct sctp_forward_tsn_chunk fwdtsn;
3689 if (respin == NULL) {
3693 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3694 resp = (struct sctp_stream_reset_response_tsn *)respin;
3695 asoc->stream_reset_outstanding--;
3696 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3697 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3698 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3699 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3703 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3704 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3705 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3707 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3708 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3709 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3711 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3712 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3714 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3715 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3717 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3718 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3719 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3720 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3721 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3722 SCTP_ASSOC_RESET_DENIED);
3724 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3725 SCTP_ASSOC_RESET_FAILED);
3728 /* get rid of the request and get the request flags */
3729 if (asoc->stream_reset_outstanding == 0) {
3730 sctp_clean_up_stream_reset(stcb);
3738 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3739 struct sctp_tmit_chunk *chk,
3740 struct sctp_stream_reset_in_request *req, int trunc)
3748 * peer wants me to send a str-reset to him for my outgoing seq's if
3751 struct sctp_association *asoc = &stcb->asoc;
3753 seq = ntohl(req->request_seq);
3754 if (asoc->str_reset_seq_in == seq) {
3755 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3756 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3757 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3759 /* Can't do it, since they exceeded our buffer size */
3760 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3761 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3762 len = ntohs(req->ph.param_length);
3763 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3764 for (i = 0; i < number_entries; i++) {
3765 temp = ntohs(req->list_of_streams[i]);
3766 req->list_of_streams[i] = temp;
3768 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3769 sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams,
3770 asoc->str_reset_seq_out,
3771 seq, (asoc->sending_seq - 1));
3772 asoc->stream_reset_out_is_outstanding = 1;
3773 asoc->str_reset = chk;
3774 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
3775 stcb->asoc.stream_reset_outstanding++;
3777 /* Can't do it, since we have sent one out */
3778 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3780 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3781 asoc->str_reset_seq_in++;
3782 } else if (asoc->str_reset_seq_in - 1 == seq) {
3783 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3784 } else if (asoc->str_reset_seq_in - 2 == seq) {
3785 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3787 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3792 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3793 struct sctp_tmit_chunk *chk,
3794 struct sctp_stream_reset_tsn_request *req)
3796 /* reset all in and out and update the tsn */
3798 * A) reset my str-seq's on in and out. B) Select a receive next,
3799 * and set cum-ack to it. Also process this selected number as a
3800 * fwd-tsn as well. C) set in the response my next sending seq.
3802 struct sctp_forward_tsn_chunk fwdtsn;
3803 struct sctp_association *asoc = &stcb->asoc;
3807 seq = ntohl(req->request_seq);
3808 if (asoc->str_reset_seq_in == seq) {
3809 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3810 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3811 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3813 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3814 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3815 fwdtsn.ch.chunk_flags = 0;
3816 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3817 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3821 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3822 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3823 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3825 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3826 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3827 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3828 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3829 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3830 atomic_add_int(&asoc->sending_seq, 1);
3831 /* save off historical data for retrans */
3832 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3833 asoc->last_sending_seq[0] = asoc->sending_seq;
3834 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3835 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3836 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3837 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3838 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3839 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3841 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3842 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3843 asoc->str_reset_seq_in++;
3844 } else if (asoc->str_reset_seq_in - 1 == seq) {
3845 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3846 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3847 } else if (asoc->str_reset_seq_in - 2 == seq) {
3848 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3849 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3851 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3857 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3858 struct sctp_tmit_chunk *chk,
3859 struct sctp_stream_reset_out_request *req, int trunc)
3862 int number_entries, len;
3863 struct sctp_association *asoc = &stcb->asoc;
3865 seq = ntohl(req->request_seq);
3867 /* now if its not a duplicate we process it */
3868 if (asoc->str_reset_seq_in == seq) {
3869 len = ntohs(req->ph.param_length);
3870 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3872 * the sender is resetting, handle the list issue.. we must
3873 * a) verify if we can do the reset, if so no problem b) If
3874 * we can't do the reset we must copy the request. c) queue
3875 * it, and setup the data in processor to trigger it off
3876 * when needed and dequeue all the queued data.
3878 tsn = ntohl(req->send_reset_at_tsn);
3880 /* move the reset action back one */
3881 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3882 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3883 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3885 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3886 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3887 /* we can do it now */
3888 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3889 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3892 * we must queue it up and thus wait for the TSN's
3893 * to arrive that are at or before tsn
3895 struct sctp_stream_reset_list *liste;
3898 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3899 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3900 siz, SCTP_M_STRESET);
3901 if (liste == NULL) {
3902 /* gak out of memory */
3903 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3904 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3908 liste->number_entries = number_entries;
3909 memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
3910 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3911 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3913 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3914 asoc->str_reset_seq_in++;
3915 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3917 * one seq back, just echo back last action since my
3918 * response was lost.
3920 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3921 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3923 * two seq back, just echo back last action since my
3924 * response was lost.
3926 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3928 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3933 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3934 struct sctp_stream_reset_add_strm *str_add)
3937 * Peer is requesting to add more streams. If its within our
3938 * max-streams we will allow it.
3940 uint32_t num_stream, i;
3942 struct sctp_association *asoc = &stcb->asoc;
3943 struct sctp_queued_to_read *ctl, *nctl;
3945 /* Get the number. */
3946 seq = ntohl(str_add->request_seq);
3947 num_stream = ntohs(str_add->number_of_streams);
3948 /* Now what would be the new total? */
3949 if (asoc->str_reset_seq_in == seq) {
3950 num_stream += stcb->asoc.streamincnt;
3951 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3952 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3953 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3954 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
3955 (num_stream > 0xffff)) {
3956 /* We must reject it they ask for to many */
3958 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3960 /* Ok, we can do that :-) */
3961 struct sctp_stream_in *oldstrm;
3963 /* save off the old */
3964 oldstrm = stcb->asoc.strmin;
3965 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
3966 (num_stream * sizeof(struct sctp_stream_in)),
3968 if (stcb->asoc.strmin == NULL) {
3969 stcb->asoc.strmin = oldstrm;
3972 /* copy off the old data */
3973 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3974 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3975 stcb->asoc.strmin[i].stream_no = i;
3976 stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered;
3977 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
3978 /* now anything on those queues? */
3979 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) {
3980 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next);
3981 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next);
3984 /* Init the new streams */
3985 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
3986 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3987 stcb->asoc.strmin[i].stream_no = i;
3988 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3989 stcb->asoc.strmin[i].delivery_started = 0;
3991 SCTP_FREE(oldstrm, SCTP_M_STRMI);
3992 /* update the size */
3993 stcb->asoc.streamincnt = num_stream;
3994 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3995 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3997 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3998 asoc->str_reset_seq_in++;
3999 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4001 * one seq back, just echo back last action since my
4002 * response was lost.
4004 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4005 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4007 * two seq back, just echo back last action since my
4008 * response was lost.
4010 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4012 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4018 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4019 struct sctp_stream_reset_add_strm *str_add)
4022 * Peer is requesting to add more streams. If its within our
4023 * max-streams we will allow it.
4025 uint16_t num_stream;
4027 struct sctp_association *asoc = &stcb->asoc;
4029 /* Get the number. */
4030 seq = ntohl(str_add->request_seq);
4031 num_stream = ntohs(str_add->number_of_streams);
4032 /* Now what would be the new total? */
4033 if (asoc->str_reset_seq_in == seq) {
4034 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4035 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4036 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4037 } else if (stcb->asoc.stream_reset_outstanding) {
4038 /* We must reject it we have something pending */
4039 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4041 /* Ok, we can do that :-) */
4044 mychk = stcb->asoc.streamoutcnt;
4045 mychk += num_stream;
4046 if (mychk < 0x10000) {
4047 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4048 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 0, 1, num_stream, 0, 1)) {
4049 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4052 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4055 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4056 asoc->str_reset_seq_in++;
4057 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4059 * one seq back, just echo back last action since my
4060 * response was lost.
4062 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4063 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4065 * two seq back, just echo back last action since my
4066 * response was lost.
4068 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4070 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4075 __attribute__((noinline))
4078 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4079 struct sctp_chunkhdr *ch_req)
4081 int chk_length, param_len, ptype;
4082 struct sctp_paramhdr pstore;
4083 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4087 struct sctp_tmit_chunk *chk;
4088 struct sctp_chunkhdr *ch;
4089 struct sctp_paramhdr *ph;
4093 /* now it may be a reset or a reset-response */
4094 chk_length = ntohs(ch_req->chunk_length);
4096 /* setup for adding the response */
4097 sctp_alloc_a_chunk(stcb, chk);
4101 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4102 chk->rec.chunk_id.can_take_data = 0;
4103 chk->asoc = &stcb->asoc;
4104 chk->no_fr_allowed = 0;
4105 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4106 chk->book_size_scale = 0;
4107 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4108 if (chk->data == NULL) {
4111 sctp_m_freem(chk->data);
4114 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4117 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4119 /* setup chunk parameters */
4120 chk->sent = SCTP_DATAGRAM_UNSENT;
4124 ch = mtod(chk->data, struct sctp_chunkhdr *);
4125 ch->chunk_type = SCTP_STREAM_RESET;
4126 ch->chunk_flags = 0;
4127 ch->chunk_length = htons(chk->send_size);
4128 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4129 offset += sizeof(struct sctp_chunkhdr);
4130 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) {
4131 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore);
4134 param_len = ntohs(ph->param_length);
4135 if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) {
4139 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)),
4140 (uint8_t *) & cstore);
4141 ptype = ntohs(ph->param_type);
4143 if (param_len > (int)sizeof(cstore)) {
4148 if (num_param > SCTP_MAX_RESET_PARAMS) {
4149 /* hit the max of parameters already sorry.. */
4152 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4153 struct sctp_stream_reset_out_request *req_out;
4155 req_out = (struct sctp_stream_reset_out_request *)ph;
4157 if (stcb->asoc.stream_reset_outstanding) {
4158 seq = ntohl(req_out->response_seq);
4159 if (seq == stcb->asoc.str_reset_seq_out) {
4161 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4164 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4165 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4166 struct sctp_stream_reset_add_strm *str_add;
4168 str_add = (struct sctp_stream_reset_add_strm *)ph;
4170 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4171 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4172 struct sctp_stream_reset_add_strm *str_add;
4174 str_add = (struct sctp_stream_reset_add_strm *)ph;
4176 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4177 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4178 struct sctp_stream_reset_in_request *req_in;
4181 req_in = (struct sctp_stream_reset_in_request *)ph;
4182 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4183 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4184 struct sctp_stream_reset_tsn_request *req_tsn;
4187 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4188 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4190 goto strres_nochunk;
4194 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4195 struct sctp_stream_reset_response *resp;
4198 resp = (struct sctp_stream_reset_response *)ph;
4199 seq = ntohl(resp->response_seq);
4200 result = ntohl(resp->result);
4201 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4203 goto strres_nochunk;
4208 offset += SCTP_SIZE32(param_len);
4209 chk_length -= SCTP_SIZE32(param_len);
4212 /* we have no response free the stuff */
4213 goto strres_nochunk;
4215 /* ok we have a chunk to link in */
4216 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4219 stcb->asoc.ctrl_queue_cnt++;
4224 * Handle a router or endpoints report of a packet loss, there are two ways
4225 * to handle this, either we get the whole packet and must disect it
4226 * ourselves (possibly with truncation and or corruption) or it is a summary
4227 * from a middle box that did the disectting for us.
4230 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4231 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4233 uint32_t bottle_bw, on_queue;
4237 struct sctp_chunk_desc desc;
4238 struct sctp_chunkhdr *ch;
4240 chlen = ntohs(cp->ch.chunk_length);
4241 chlen -= sizeof(struct sctp_pktdrop_chunk);
4242 /* XXX possible chlen underflow */
4245 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4246 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4248 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4249 chlen -= sizeof(struct sctphdr);
4250 /* XXX possible chlen underflow */
4251 memset(&desc, 0, sizeof(desc));
4253 trunc_len = (uint16_t) ntohs(cp->trunc_len);
4254 if (trunc_len > limit) {
4257 /* now the chunks themselves */
4258 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4259 desc.chunk_type = ch->chunk_type;
4260 /* get amount we need to move */
4261 at = ntohs(ch->chunk_length);
4262 if (at < sizeof(struct sctp_chunkhdr)) {
4263 /* corrupt chunk, maybe at the end? */
4264 SCTP_STAT_INCR(sctps_pdrpcrupt);
4267 if (trunc_len == 0) {
4268 /* we are supposed to have all of it */
4270 /* corrupt skip it */
4271 SCTP_STAT_INCR(sctps_pdrpcrupt);
4275 /* is there enough of it left ? */
4276 if (desc.chunk_type == SCTP_DATA) {
4277 if (chlen < (sizeof(struct sctp_data_chunk) +
4278 sizeof(desc.data_bytes))) {
4282 if (chlen < sizeof(struct sctp_chunkhdr)) {
4287 if (desc.chunk_type == SCTP_DATA) {
4288 /* can we get out the tsn? */
4289 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4290 SCTP_STAT_INCR(sctps_pdrpmbda);
4292 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4294 struct sctp_data_chunk *dcp;
4298 dcp = (struct sctp_data_chunk *)ch;
4299 ddp = (uint8_t *) (dcp + 1);
4300 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4301 desc.data_bytes[iii] = ddp[iii];
4303 desc.tsn_ifany = dcp->dp.tsn;
4305 /* nope we are done. */
4306 SCTP_STAT_INCR(sctps_pdrpnedat);
4310 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4311 SCTP_STAT_INCR(sctps_pdrpmbct);
4314 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4315 SCTP_STAT_INCR(sctps_pdrppdbrk);
4318 if (SCTP_SIZE32(at) > chlen) {
4321 chlen -= SCTP_SIZE32(at);
4322 if (chlen < sizeof(struct sctp_chunkhdr)) {
4323 /* done, none left */
4326 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4328 /* Now update any rwnd --- possibly */
4329 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4330 /* From a peer, we get a rwnd report */
4333 SCTP_STAT_INCR(sctps_pdrpfehos);
4335 bottle_bw = ntohl(cp->bottle_bw);
4336 on_queue = ntohl(cp->current_onq);
4337 if (bottle_bw && on_queue) {
4338 /* a rwnd report is in here */
4339 if (bottle_bw > on_queue)
4340 a_rwnd = bottle_bw - on_queue;
4345 stcb->asoc.peers_rwnd = 0;
4347 if (a_rwnd > stcb->asoc.total_flight) {
4348 stcb->asoc.peers_rwnd =
4349 a_rwnd - stcb->asoc.total_flight;
4351 stcb->asoc.peers_rwnd = 0;
4353 if (stcb->asoc.peers_rwnd <
4354 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4355 /* SWS sender side engages */
4356 stcb->asoc.peers_rwnd = 0;
4361 SCTP_STAT_INCR(sctps_pdrpfmbox);
4364 /* now middle boxes in sat networks get a cwnd bump */
4365 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4366 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4367 (stcb->asoc.sat_network)) {
4369 * This is debateable but for sat networks it makes sense
4370 * Note if a T3 timer has went off, we will prohibit any
4371 * changes to cwnd until we exit the t3 loss recovery.
4373 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4374 net, cp, &bottle_bw, &on_queue);
4379 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4380 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4381 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4382 * length of the complete packet outputs: - length: modified to remaining
4383 * length after control processing - netp: modified to new sctp_nets after
4384 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4385 * bad packet,...) otherwise return the tcb for this packet
4388 __attribute__((noinline))
4390 static struct sctp_tcb *
4391 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4392 struct sockaddr *src, struct sockaddr *dst,
4393 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4394 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4395 uint8_t use_mflowid, uint32_t mflowid,
4396 uint32_t vrf_id, uint16_t port)
4398 struct sctp_association *asoc;
4400 int num_chunks = 0; /* number of control chunks processed */
4401 uint32_t chk_length;
4403 int abort_no_unlock = 0;
4407 * How big should this be, and should it be alloc'd? Lets try the
4408 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4409 * until we get into jumbo grams and such..
4411 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4412 struct sctp_tcb *locked_tcb = stcb;
4414 uint32_t auth_offset = 0, auth_len = 0;
4415 int auth_skipped = 0;
4418 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4423 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4424 iphlen, *offset, length, (void *)stcb);
4426 /* validate chunk header length... */
4427 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4428 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4429 ntohs(ch->chunk_length));
4431 SCTP_TCB_UNLOCK(locked_tcb);
4436 * validate the verification tag
4438 vtag_in = ntohl(sh->v_tag);
4441 SCTP_TCB_LOCK_ASSERT(locked_tcb);
4443 if (ch->chunk_type == SCTP_INITIATION) {
4444 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4445 ntohs(ch->chunk_length), vtag_in);
4447 /* protocol error- silently discard... */
4448 SCTP_STAT_INCR(sctps_badvtag);
4450 SCTP_TCB_UNLOCK(locked_tcb);
4454 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4456 * If there is no stcb, skip the AUTH chunk and process
4457 * later after a stcb is found (to validate the lookup was
4460 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4462 !SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4463 /* save this chunk for later processing */
4465 auth_offset = *offset;
4466 auth_len = ntohs(ch->chunk_length);
4468 /* (temporarily) move past this chunk */
4469 *offset += SCTP_SIZE32(auth_len);
4470 if (*offset >= length) {
4471 /* no more data left in the mbuf chain */
4474 SCTP_TCB_UNLOCK(locked_tcb);
4478 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4479 sizeof(struct sctp_chunkhdr), chunk_buf);
4485 SCTP_TCB_UNLOCK(locked_tcb);
4489 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4490 goto process_control_chunks;
4493 * first check if it's an ASCONF with an unknown src addr we
4494 * need to look inside to find the association
4496 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4497 struct sctp_chunkhdr *asconf_ch = ch;
4498 uint32_t asconf_offset = 0, asconf_len = 0;
4500 /* inp's refcount may be reduced */
4501 SCTP_INP_INCR_REF(inp);
4503 asconf_offset = *offset;
4505 asconf_len = ntohs(asconf_ch->chunk_length);
4506 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4508 stcb = sctp_findassociation_ep_asconf(m,
4511 sh, &inp, netp, vrf_id);
4514 asconf_offset += SCTP_SIZE32(asconf_len);
4515 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4516 sizeof(struct sctp_chunkhdr), chunk_buf);
4517 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4520 * reduce inp's refcount if not reduced in
4521 * sctp_findassociation_ep_asconf().
4523 SCTP_INP_DECR_REF(inp);
4528 /* now go back and verify any auth chunk to be sure */
4529 if (auth_skipped && (stcb != NULL)) {
4530 struct sctp_auth_chunk *auth;
4532 auth = (struct sctp_auth_chunk *)
4533 sctp_m_getptr(m, auth_offset,
4534 auth_len, chunk_buf);
4537 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4539 /* auth HMAC failed so dump it */
4542 SCTP_TCB_UNLOCK(locked_tcb);
4546 /* remaining chunks are HMAC checked */
4547 stcb->asoc.authenticated = 1;
4552 /* no association, so it's out of the blue... */
4553 sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp,
4554 use_mflowid, mflowid,
4558 SCTP_TCB_UNLOCK(locked_tcb);
4563 /* ABORT and SHUTDOWN can use either v_tag... */
4564 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4565 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4566 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4567 /* Take the T-bit always into account. */
4568 if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
4569 (vtag_in == asoc->my_vtag)) ||
4570 (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4571 (vtag_in == asoc->peer_vtag))) {
4574 /* drop this packet... */
4575 SCTP_STAT_INCR(sctps_badvtag);
4577 SCTP_TCB_UNLOCK(locked_tcb);
4581 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4582 if (vtag_in != asoc->my_vtag) {
4584 * this could be a stale SHUTDOWN-ACK or the
4585 * peer never got the SHUTDOWN-COMPLETE and
4586 * is still hung; we have started a new asoc
4587 * but it won't complete until the shutdown
4591 SCTP_TCB_UNLOCK(locked_tcb);
4593 sctp_handle_ootb(m, iphlen, *offset, src, dst,
4595 use_mflowid, mflowid,
4600 /* for all other chunks, vtag must match */
4601 if (vtag_in != asoc->my_vtag) {
4602 /* invalid vtag... */
4603 SCTPDBG(SCTP_DEBUG_INPUT3,
4604 "invalid vtag: %xh, expect %xh\n",
4605 vtag_in, asoc->my_vtag);
4606 SCTP_STAT_INCR(sctps_badvtag);
4608 SCTP_TCB_UNLOCK(locked_tcb);
4614 } /* end if !SCTP_COOKIE_ECHO */
4616 * process all control chunks...
4618 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4619 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4620 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4621 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4622 /* implied cookie-ack.. we must have lost the ack */
4623 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4624 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4625 stcb->asoc.overall_error_count,
4627 SCTP_FROM_SCTP_INPUT,
4630 stcb->asoc.overall_error_count = 0;
4631 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4634 process_control_chunks:
4635 while (IS_SCTP_CONTROL(ch)) {
4636 /* validate chunk length */
4637 chk_length = ntohs(ch->chunk_length);
4638 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4639 ch->chunk_type, chk_length);
4640 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4641 if (chk_length < sizeof(*ch) ||
4642 (*offset + (int)chk_length) > length) {
4645 SCTP_TCB_UNLOCK(locked_tcb);
4649 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4651 * INIT-ACK only gets the init ack "header" portion only
4652 * because we don't have to process the peer's COOKIE. All
4653 * others get a complete chunk.
4655 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4656 (ch->chunk_type == SCTP_INITIATION)) {
4657 /* get an init-ack chunk */
4658 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4659 sizeof(struct sctp_init_ack_chunk), chunk_buf);
4663 SCTP_TCB_UNLOCK(locked_tcb);
4668 /* For cookies and all other chunks. */
4669 if (chk_length > sizeof(chunk_buf)) {
4671 * use just the size of the chunk buffer so
4672 * the front part of our chunks fit in
4673 * contiguous space up to the chunk buffer
4674 * size (508 bytes). For chunks that need to
4675 * get more than that they must use the
4676 * sctp_m_getptr() function or other means
4677 * (e.g. know how to parse mbuf chains).
4678 * Cookies do this already.
4680 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4681 (sizeof(chunk_buf) - 4),
4686 SCTP_TCB_UNLOCK(locked_tcb);
4691 /* We can fit it all */
4692 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4693 chk_length, chunk_buf);
4695 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4698 SCTP_TCB_UNLOCK(locked_tcb);
4705 /* Save off the last place we got a control from */
4707 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4709 * allow last_control to be NULL if
4710 * ASCONF... ASCONF processing will find the
4713 if ((netp != NULL) && (*netp != NULL))
4714 stcb->asoc.last_control_chunk_from = *netp;
4717 #ifdef SCTP_AUDITING_ENABLED
4718 sctp_audit_log(0xB0, ch->chunk_type);
4721 /* check to see if this chunk required auth, but isn't */
4722 if ((stcb != NULL) &&
4723 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
4724 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4725 !stcb->asoc.authenticated) {
4726 /* "silently" ignore */
4727 SCTP_STAT_INCR(sctps_recvauthmissing);
4730 switch (ch->chunk_type) {
4731 case SCTP_INITIATION:
4732 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4733 /* The INIT chunk must be the only chunk. */
4734 if ((num_chunks > 1) ||
4735 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4736 sctp_abort_association(inp, stcb, m, iphlen,
4738 use_mflowid, mflowid,
4743 /* Honor our resource limit. */
4744 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4745 struct mbuf *op_err;
4747 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
4748 sctp_abort_association(inp, stcb, m, iphlen,
4749 src, dst, sh, op_err,
4750 use_mflowid, mflowid,
4755 sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4756 (struct sctp_init_chunk *)ch, inp,
4757 stcb, &abort_no_unlock,
4758 use_mflowid, mflowid,
4761 if ((!abort_no_unlock) && (locked_tcb)) {
4762 SCTP_TCB_UNLOCK(locked_tcb);
4766 case SCTP_PAD_CHUNK:
4768 case SCTP_INITIATION_ACK:
4769 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4770 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4771 /* We are not interested anymore */
4772 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4775 if (locked_tcb != stcb) {
4777 SCTP_TCB_UNLOCK(locked_tcb);
4781 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4782 so = SCTP_INP_SO(inp);
4783 atomic_add_int(&stcb->asoc.refcnt, 1);
4784 SCTP_TCB_UNLOCK(stcb);
4785 SCTP_SOCKET_LOCK(so, 1);
4786 SCTP_TCB_LOCK(stcb);
4787 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4789 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4790 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4791 SCTP_SOCKET_UNLOCK(so, 1);
4797 /* The INIT-ACK chunk must be the only chunk. */
4798 if ((num_chunks > 1) ||
4799 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4802 SCTP_TCB_UNLOCK(locked_tcb);
4806 if ((netp) && (*netp)) {
4807 ret = sctp_handle_init_ack(m, iphlen, *offset,
4809 (struct sctp_init_ack_chunk *)ch,
4812 use_mflowid, mflowid,
4818 if (abort_no_unlock) {
4822 * Special case, I must call the output routine to
4823 * get the cookie echoed
4825 if ((stcb != NULL) && (ret == 0)) {
4826 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4829 SCTP_TCB_UNLOCK(locked_tcb);
4833 case SCTP_SELECTIVE_ACK:
4835 struct sctp_sack_chunk *sack;
4837 uint32_t a_rwnd, cum_ack;
4838 uint16_t num_seg, num_dup;
4840 int offset_seg, offset_dup;
4842 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4843 SCTP_STAT_INCR(sctps_recvsacks);
4845 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4848 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4849 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4852 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4854 * If we have sent a shutdown-ack, we will pay no
4855 * attention to a sack sent in to us since
4856 * we don't care anymore.
4860 sack = (struct sctp_sack_chunk *)ch;
4861 flags = ch->chunk_flags;
4862 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4863 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4864 num_dup = ntohs(sack->sack.num_dup_tsns);
4865 a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
4866 if (sizeof(struct sctp_sack_chunk) +
4867 num_seg * sizeof(struct sctp_gap_ack_block) +
4868 num_dup * sizeof(uint32_t) != chk_length) {
4869 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4872 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4873 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4874 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4875 cum_ack, num_seg, a_rwnd);
4876 stcb->asoc.seen_a_sack_this_pkt = 1;
4877 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4879 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4880 (stcb->asoc.saw_sack_with_frags == 0) &&
4881 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4882 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4885 * We have a SIMPLE sack having no
4886 * prior segments and data on sent
4887 * queue to be acked.. Use the
4888 * faster path sack processing. We
4889 * also allow window update sacks
4890 * with no missing segments to go
4893 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
4896 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4897 num_seg, 0, num_dup, &abort_now, flags,
4898 cum_ack, a_rwnd, ecne_seen);
4901 /* ABORT signal from sack processing */
4905 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4906 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4907 (stcb->asoc.stream_queue_cnt == 0)) {
4908 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4913 * EY - nr_sack: If the received chunk is an
4916 case SCTP_NR_SELECTIVE_ACK:
4918 struct sctp_nr_sack_chunk *nr_sack;
4920 uint32_t a_rwnd, cum_ack;
4921 uint16_t num_seg, num_nr_seg, num_dup;
4923 int offset_seg, offset_dup;
4925 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
4926 SCTP_STAT_INCR(sctps_recvsacks);
4928 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
4931 if ((stcb->asoc.sctp_nr_sack_on_off == 0) ||
4932 (stcb->asoc.peer_supports_nr_sack == 0)) {
4935 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4936 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
4939 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4941 * If we have sent a shutdown-ack, we will pay no
4942 * attention to a sack sent in to us since
4943 * we don't care anymore.
4947 nr_sack = (struct sctp_nr_sack_chunk *)ch;
4948 flags = ch->chunk_flags;
4949 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4950 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4951 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4952 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4953 a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd);
4954 if (sizeof(struct sctp_nr_sack_chunk) +
4955 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4956 num_dup * sizeof(uint32_t) != chk_length) {
4957 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4960 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4961 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4962 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4963 cum_ack, num_seg, a_rwnd);
4964 stcb->asoc.seen_a_sack_this_pkt = 1;
4965 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4966 (num_seg == 0) && (num_nr_seg == 0) &&
4967 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4968 (stcb->asoc.saw_sack_with_frags == 0) &&
4969 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4970 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
4972 * We have a SIMPLE sack having no
4973 * prior segments and data on sent
4974 * queue to be acked. Use the faster
4975 * path sack processing. We also
4976 * allow window update sacks with no
4977 * missing segments to go this way
4980 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
4981 &abort_now, ecne_seen);
4984 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4985 num_seg, num_nr_seg, num_dup, &abort_now, flags,
4986 cum_ack, a_rwnd, ecne_seen);
4989 /* ABORT signal from sack processing */
4993 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4994 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4995 (stcb->asoc.stream_queue_cnt == 0)) {
4996 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5001 case SCTP_HEARTBEAT_REQUEST:
5002 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
5003 if ((stcb) && netp && *netp) {
5004 SCTP_STAT_INCR(sctps_recvheartbeat);
5005 sctp_send_heartbeat_ack(stcb, m, *offset,
5008 /* He's alive so give him credit */
5009 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5010 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5011 stcb->asoc.overall_error_count,
5013 SCTP_FROM_SCTP_INPUT,
5016 stcb->asoc.overall_error_count = 0;
5019 case SCTP_HEARTBEAT_ACK:
5020 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
5021 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5025 SCTP_TCB_UNLOCK(locked_tcb);
5029 /* He's alive so give him credit */
5030 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5031 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5032 stcb->asoc.overall_error_count,
5034 SCTP_FROM_SCTP_INPUT,
5037 stcb->asoc.overall_error_count = 0;
5038 SCTP_STAT_INCR(sctps_recvheartbeatack);
5040 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5043 case SCTP_ABORT_ASSOCIATION:
5044 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5046 if ((stcb) && netp && *netp)
5047 sctp_handle_abort((struct sctp_abort_chunk *)ch,
5053 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5055 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5058 SCTP_TCB_UNLOCK(locked_tcb);
5062 if (netp && *netp) {
5065 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5066 stcb, *netp, &abort_flag);
5073 case SCTP_SHUTDOWN_ACK:
5074 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", (void *)stcb);
5075 if ((stcb) && (netp) && (*netp))
5076 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5081 case SCTP_OPERATION_ERROR:
5082 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
5083 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
5088 case SCTP_COOKIE_ECHO:
5089 SCTPDBG(SCTP_DEBUG_INPUT3,
5090 "SCTP_COOKIE-ECHO, stcb %p\n", (void *)stcb);
5091 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5094 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5095 /* We are not interested anymore */
5098 SCTP_TCB_UNLOCK(stcb);
5105 * First are we accepting? We do this again here
5106 * since it is possible that a previous endpoint WAS
5107 * listening responded to a INIT-ACK and then
5108 * closed. We opened and bound.. and are now no
5112 if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
5113 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5114 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5115 struct mbuf *op_err;
5117 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
5118 sctp_abort_association(inp, stcb, m, iphlen,
5119 src, dst, sh, op_err,
5120 use_mflowid, mflowid,
5126 struct mbuf *ret_buf;
5127 struct sctp_inpcb *linp;
5136 SCTP_ASOC_CREATE_LOCK(linp);
5137 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5138 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5139 SCTP_ASOC_CREATE_UNLOCK(linp);
5145 sctp_handle_cookie_echo(m, iphlen,
5149 (struct sctp_cookie_echo_chunk *)ch,
5163 SCTP_ASOC_CREATE_UNLOCK(linp);
5165 if (ret_buf == NULL) {
5167 SCTP_TCB_UNLOCK(locked_tcb);
5169 SCTPDBG(SCTP_DEBUG_INPUT3,
5170 "GAK, null buffer\n");
5174 /* if AUTH skipped, see if it verified... */
5179 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5181 * Restart the timer if we have
5184 struct sctp_tmit_chunk *chk;
5186 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5187 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5191 case SCTP_COOKIE_ACK:
5192 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", (void *)stcb);
5193 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5195 SCTP_TCB_UNLOCK(locked_tcb);
5199 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5200 /* We are not interested anymore */
5201 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5204 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5205 so = SCTP_INP_SO(inp);
5206 atomic_add_int(&stcb->asoc.refcnt, 1);
5207 SCTP_TCB_UNLOCK(stcb);
5208 SCTP_SOCKET_LOCK(so, 1);
5209 SCTP_TCB_LOCK(stcb);
5210 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5212 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
5213 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5214 SCTP_SOCKET_UNLOCK(so, 1);
5220 /* He's alive so give him credit */
5221 if ((stcb) && netp && *netp) {
5222 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5223 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5224 stcb->asoc.overall_error_count,
5226 SCTP_FROM_SCTP_INPUT,
5229 stcb->asoc.overall_error_count = 0;
5230 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5234 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5235 /* He's alive so give him credit */
5236 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5239 SCTP_TCB_UNLOCK(locked_tcb);
5245 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5246 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5247 stcb->asoc.overall_error_count,
5249 SCTP_FROM_SCTP_INPUT,
5252 stcb->asoc.overall_error_count = 0;
5253 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5259 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5260 /* He's alive so give him credit */
5261 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5264 SCTP_TCB_UNLOCK(locked_tcb);
5270 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5271 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5272 stcb->asoc.overall_error_count,
5274 SCTP_FROM_SCTP_INPUT,
5277 stcb->asoc.overall_error_count = 0;
5278 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5281 case SCTP_SHUTDOWN_COMPLETE:
5282 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", (void *)stcb);
5283 /* must be first and only chunk */
5284 if ((num_chunks > 1) ||
5285 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5288 SCTP_TCB_UNLOCK(locked_tcb);
5292 if ((stcb) && netp && *netp) {
5293 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5300 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5301 /* He's alive so give him credit */
5303 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5304 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5305 stcb->asoc.overall_error_count,
5307 SCTP_FROM_SCTP_INPUT,
5310 stcb->asoc.overall_error_count = 0;
5311 sctp_handle_asconf(m, *offset, src,
5312 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5316 case SCTP_ASCONF_ACK:
5317 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5318 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5321 SCTP_TCB_UNLOCK(locked_tcb);
5326 if ((stcb) && netp && *netp) {
5327 /* He's alive so give him credit */
5328 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5329 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5330 stcb->asoc.overall_error_count,
5332 SCTP_FROM_SCTP_INPUT,
5335 stcb->asoc.overall_error_count = 0;
5336 sctp_handle_asconf_ack(m, *offset,
5337 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5338 if (abort_no_unlock)
5342 case SCTP_FORWARD_CUM_TSN:
5343 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5344 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5347 SCTP_TCB_UNLOCK(locked_tcb);
5352 /* He's alive so give him credit */
5356 stcb->asoc.overall_error_count = 0;
5357 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5358 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5359 stcb->asoc.overall_error_count,
5361 SCTP_FROM_SCTP_INPUT,
5365 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5366 /* We are not interested anymore */
5367 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5368 so = SCTP_INP_SO(inp);
5369 atomic_add_int(&stcb->asoc.refcnt, 1);
5370 SCTP_TCB_UNLOCK(stcb);
5371 SCTP_SOCKET_LOCK(so, 1);
5372 SCTP_TCB_LOCK(stcb);
5373 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5375 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
5376 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5377 SCTP_SOCKET_UNLOCK(so, 1);
5382 sctp_handle_forward_tsn(stcb,
5383 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5388 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5389 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5390 stcb->asoc.overall_error_count,
5392 SCTP_FROM_SCTP_INPUT,
5395 stcb->asoc.overall_error_count = 0;
5400 case SCTP_STREAM_RESET:
5401 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5402 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5405 SCTP_TCB_UNLOCK(locked_tcb);
5410 if (stcb->asoc.peer_supports_strreset == 0) {
5412 * hmm, peer should have announced this, but
5413 * we will turn it on since he is sending us
5416 stcb->asoc.peer_supports_strreset = 1;
5418 if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5419 /* stop processing */
5424 case SCTP_PACKET_DROPPED:
5425 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5426 /* re-get it all please */
5427 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5430 SCTP_TCB_UNLOCK(locked_tcb);
5435 if (ch && (stcb) && netp && (*netp)) {
5436 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5438 min(chk_length, (sizeof(chunk_buf) - 4)));
5443 case SCTP_AUTHENTICATION:
5444 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5445 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
5449 /* save the first AUTH for later processing */
5450 if (auth_skipped == 0) {
5451 auth_offset = *offset;
5452 auth_len = chk_length;
5455 /* skip this chunk (temporarily) */
5458 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5459 (chk_length > (sizeof(struct sctp_auth_chunk) +
5460 SCTP_AUTH_DIGEST_LEN_MAX))) {
5463 SCTP_TCB_UNLOCK(locked_tcb);
5468 if (got_auth == 1) {
5469 /* skip this chunk... it's already auth'd */
5473 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5475 /* auth HMAC failed so dump the packet */
5479 /* remaining chunks are HMAC checked */
5480 stcb->asoc.authenticated = 1;
5486 /* it's an unknown chunk! */
5487 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5489 struct sctp_paramhdr *phd;
5491 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
5492 0, M_DONTWAIT, 1, MT_DATA);
5494 phd = mtod(mm, struct sctp_paramhdr *);
5496 * We cheat and use param type since
5497 * we did not bother to define a
5498 * error cause struct. They are the
5499 * same basic format with different
5502 phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5503 phd->param_length = htons(chk_length + sizeof(*phd));
5504 SCTP_BUF_LEN(mm) = sizeof(*phd);
5505 SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, chk_length, M_DONTWAIT);
5506 if (SCTP_BUF_NEXT(mm)) {
5507 if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(mm), SCTP_SIZE32(chk_length) - chk_length, NULL)) {
5510 #ifdef SCTP_MBUF_LOGGING
5511 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5514 for (mat = SCTP_BUF_NEXT(mm); mat; mat = SCTP_BUF_NEXT(mat)) {
5515 if (SCTP_BUF_IS_EXTENDED(mat)) {
5516 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5521 sctp_queue_op_err(stcb, mm);
5528 if ((ch->chunk_type & 0x80) == 0) {
5529 /* discard this packet */
5532 } /* else skip this bad chunk and continue... */
5534 } /* switch (ch->chunk_type) */
5538 /* get the next chunk */
5539 *offset += SCTP_SIZE32(chk_length);
5540 if (*offset >= length) {
5541 /* no more data left in the mbuf chain */
5544 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5545 sizeof(struct sctp_chunkhdr), chunk_buf);
5548 SCTP_TCB_UNLOCK(locked_tcb);
5555 if (asconf_cnt > 0 && stcb != NULL) {
5556 sctp_send_asconf_ack(stcb);
5564 __attribute__((noinline))
5567 sctp_validate_no_locks(struct sctp_inpcb *inp)
5569 struct sctp_tcb *lstcb;
5571 LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) {
5572 if (mtx_owned(&lstcb->tcb_mtx)) {
5573 panic("Own lock on stcb at return from input");
5576 if (mtx_owned(&inp->inp_create_mtx)) {
5577 panic("Own create lock on inp");
5579 if (mtx_owned(&inp->inp_mtx)) {
5580 panic("Own inp lock on inp");
5587 * common input chunk processing (v4 and v6)
5590 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5591 struct sockaddr *src, struct sockaddr *dst,
5592 struct sctphdr *sh, struct sctp_chunkhdr *ch,
5593 #if !defined(SCTP_WITH_NO_CSUM)
5594 uint8_t compute_crc,
5597 uint8_t use_mflowid, uint32_t mflowid,
5598 uint32_t vrf_id, uint16_t port)
5601 int fwd_tsn_seen = 0, data_processed = 0;
5602 struct mbuf *m = *mm;
5604 int cnt_ctrl_ready = 0;
5605 struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5606 struct sctp_tcb *stcb = NULL;
5607 struct sctp_nets *net = NULL;
5609 SCTP_STAT_INCR(sctps_recvdatagrams);
5610 #ifdef SCTP_AUDITING_ENABLED
5611 sctp_audit_log(0xE0, 1);
5612 sctp_auditing(0, inp, stcb, net);
5614 #if !defined(SCTP_WITH_NO_CSUM)
5615 if (compute_crc != 0) {
5616 uint32_t check, calc_check;
5618 check = sh->checksum;
5620 calc_check = sctp_calculate_cksum(m, iphlen);
5621 sh->checksum = check;
5622 if (calc_check != check) {
5623 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5624 calc_check, check, (void *)m, length, iphlen);
5625 stcb = sctp_findassociation_addr(m, offset, src, dst,
5626 sh, ch, &inp, &net, vrf_id);
5627 if ((net != NULL) && (port != 0)) {
5628 if (net->port == 0) {
5629 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5633 if ((net != NULL) && (use_mflowid != 0)) {
5634 net->flowid = mflowid;
5639 if ((inp != NULL) && (stcb != NULL)) {
5640 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5641 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5642 } else if ((inp != NULL) && (stcb == NULL)) {
5645 SCTP_STAT_INCR(sctps_badsum);
5646 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5651 /* Destination port of 0 is illegal, based on RFC4960. */
5652 if (sh->dest_port == 0) {
5653 SCTP_STAT_INCR(sctps_hdrops);
5656 stcb = sctp_findassociation_addr(m, offset, src, dst,
5657 sh, ch, &inp, &net, vrf_id);
5658 if ((net != NULL) && (port != 0)) {
5659 if (net->port == 0) {
5660 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5664 if ((net != NULL) && (use_mflowid != 0)) {
5665 net->flowid = mflowid;
5671 SCTP_STAT_INCR(sctps_noport);
5672 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5675 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5676 sctp_send_shutdown_complete2(src, dst, sh,
5677 use_mflowid, mflowid,
5681 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5684 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5685 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5686 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5687 (ch->chunk_type != SCTP_INIT))) {
5688 sctp_send_abort(m, iphlen, src, dst,
5690 use_mflowid, mflowid,
5695 } else if (stcb == NULL) {
5700 * I very much doubt any of the IPSEC stuff will work but I have no
5701 * idea, so I will leave it in place.
5704 switch (dst->sa_family) {
5707 if (ipsec4_in_reject(m, &inp->ip_inp.inp)) {
5708 IPSECSTAT_INC(in_polvio);
5709 SCTP_STAT_INCR(sctps_hdrops);
5716 if (ipsec6_in_reject(m, &inp->ip_inp.inp)) {
5717 IPSEC6STAT_INC(in_polvio);
5718 SCTP_STAT_INCR(sctps_hdrops);
5728 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5729 (void *)m, iphlen, offset, length, (void *)stcb);
5731 /* always clear this before beginning a packet */
5732 stcb->asoc.authenticated = 0;
5733 stcb->asoc.seen_a_sack_this_pkt = 0;
5734 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5735 (void *)stcb, stcb->asoc.state);
5737 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5738 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5740 * If we hit here, we had a ref count
5741 * up when the assoc was aborted and the
5742 * timer is clearing out the assoc, we should
5743 * NOT respond to any packet.. its OOTB.
5745 SCTP_TCB_UNLOCK(stcb);
5747 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp,
5748 use_mflowid, mflowid,
5753 if (IS_SCTP_CONTROL(ch)) {
5754 /* process the control portion of the SCTP packet */
5755 /* sa_ignore NO_NULL_CHK */
5756 stcb = sctp_process_control(m, iphlen, &offset, length,
5758 inp, stcb, &net, &fwd_tsn_seen,
5759 use_mflowid, mflowid,
5763 * This covers us if the cookie-echo was there and
5764 * it changes our INP.
5766 inp = stcb->sctp_ep;
5767 if ((net) && (port)) {
5768 if (net->port == 0) {
5769 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5776 * no control chunks, so pre-process DATA chunks (these
5777 * checks are taken care of by control processing)
5781 * if DATA only packet, and auth is required, then punt...
5782 * can't have authenticated without any AUTH (control)
5785 if ((stcb != NULL) &&
5786 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5787 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5788 /* "silently" ignore */
5789 SCTP_STAT_INCR(sctps_recvauthmissing);
5793 /* out of the blue DATA chunk */
5794 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp,
5795 use_mflowid, mflowid,
5799 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5800 /* v_tag mismatch! */
5801 SCTP_STAT_INCR(sctps_badvtag);
5808 * no valid TCB for this packet, or we found it's a bad
5809 * packet while processing control, or we're done with this
5810 * packet (done or skip rest of data), so we drop it...
5815 * DATA chunk processing
5817 /* plow through the data chunks while length > offset */
5820 * Rest should be DATA only. Check authentication state if AUTH for
5823 if ((length > offset) &&
5825 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5826 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5827 !stcb->asoc.authenticated) {
5828 /* "silently" ignore */
5829 SCTP_STAT_INCR(sctps_recvauthmissing);
5830 SCTPDBG(SCTP_DEBUG_AUTH1,
5831 "Data chunk requires AUTH, skipped\n");
5834 if (length > offset) {
5838 * First check to make sure our state is correct. We would
5839 * not get here unless we really did have a tag, so we don't
5840 * abort if this happens, just dump the chunk silently.
5842 switch (SCTP_GET_STATE(&stcb->asoc)) {
5843 case SCTP_STATE_COOKIE_ECHOED:
5845 * we consider data with valid tags in this state
5846 * shows us the cookie-ack was lost. Imply it was
5849 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5850 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5851 stcb->asoc.overall_error_count,
5853 SCTP_FROM_SCTP_INPUT,
5856 stcb->asoc.overall_error_count = 0;
5857 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5859 case SCTP_STATE_COOKIE_WAIT:
5861 * We consider OOTB any data sent during asoc setup.
5863 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp,
5864 use_mflowid, mflowid,
5867 /* sa_ignore NOTREACHED */
5869 case SCTP_STATE_EMPTY: /* should not happen */
5870 case SCTP_STATE_INUSE: /* should not happen */
5871 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5872 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5875 /* sa_ignore NOTREACHED */
5877 case SCTP_STATE_OPEN:
5878 case SCTP_STATE_SHUTDOWN_SENT:
5881 /* plow through the data chunks while length > offset */
5882 retval = sctp_process_data(mm, iphlen, &offset, length,
5884 inp, stcb, net, &high_tsn,
5885 use_mflowid, mflowid,
5889 * The association aborted, NO UNLOCK needed since
5890 * the association is destroyed.
5897 * Anything important needs to have been m_copy'ed in
5901 /* take care of ecn */
5902 if ((data_processed == 1) &&
5903 (stcb->asoc.ecn_allowed == 1) &&
5904 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5905 /* Yep, we need to add a ECNE */
5906 sctp_send_ecn_echo(stcb, net, high_tsn);
5908 if ((data_processed == 0) && (fwd_tsn_seen)) {
5910 uint32_t highest_tsn;
5912 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5913 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5915 highest_tsn = stcb->asoc.highest_tsn_inside_map;
5917 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
5918 stcb->asoc.send_sack = 1;
5919 sctp_sack_check(stcb, was_a_gap);
5920 } else if (fwd_tsn_seen) {
5921 stcb->asoc.send_sack = 1;
5923 /* trigger send of any chunks in queue... */
5925 #ifdef SCTP_AUDITING_ENABLED
5926 sctp_audit_log(0xE0, 2);
5927 sctp_auditing(1, inp, stcb, net);
5929 SCTPDBG(SCTP_DEBUG_INPUT1,
5930 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5931 stcb->asoc.peers_rwnd,
5932 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5933 stcb->asoc.total_flight);
5934 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5935 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5936 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5938 if (cnt_ctrl_ready ||
5940 (stcb->asoc.peers_rwnd > 0 ||
5941 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5942 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5943 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5944 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5946 #ifdef SCTP_AUDITING_ENABLED
5947 sctp_audit_log(0xE0, 3);
5948 sctp_auditing(2, inp, stcb, net);
5952 SCTP_TCB_UNLOCK(stcb);
5954 if (inp_decr != NULL) {
5955 /* reduce ref-count */
5956 SCTP_INP_WLOCK(inp_decr);
5957 SCTP_INP_DECR_REF(inp_decr);
5958 SCTP_INP_WUNLOCK(inp_decr);
5962 sctp_validate_no_locks(inp);
5970 sctp_print_mbuf_chain(struct mbuf *m)
5972 for (; m; m = SCTP_BUF_NEXT(m)) {
5973 SCTP_PRINTF("%p: m_len = %ld\n", (void *)m, SCTP_BUF_LEN(m));
5974 if (SCTP_BUF_IS_EXTENDED(m))
5975 SCTP_PRINTF("%p: extend_size = %d\n", (void *)m, SCTP_BUF_EXTEND_SIZE(m));
5983 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5987 uint32_t vrf_id = 0;
5989 struct sockaddr_in src, dst;
5992 struct sctp_chunkhdr *ch;
5995 #if !defined(SCTP_WITH_NO_CSUM)
5996 uint8_t compute_crc;
6000 uint8_t use_mflowid;
6003 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
6004 SCTP_RELEASE_PKT(i_pak);
6007 m = SCTP_HEADER_TO_CHAIN(i_pak);
6008 #ifdef SCTP_MBUF_LOGGING
6009 /* Log in any input mbufs */
6010 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6013 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
6014 if (SCTP_BUF_IS_EXTENDED(mat)) {
6015 sctp_log_mb(mat, SCTP_MBUF_INPUT);
6020 #ifdef SCTP_PACKET_LOGGING
6021 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
6025 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6026 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
6028 if_name(m->m_pkthdr.rcvif),
6029 m->m_pkthdr.csum_flags);
6030 if (m->m_flags & M_FLOWID) {
6031 mflowid = m->m_pkthdr.flowid;
6037 SCTP_STAT_INCR(sctps_recvpackets);
6038 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
6039 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
6040 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6041 if (SCTP_BUF_LEN(m) < offset) {
6042 if ((m = m_pullup(m, offset)) == NULL) {
6043 SCTP_STAT_INCR(sctps_hdrops);
6047 ip = mtod(m, struct ip *);
6048 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
6049 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
6050 offset -= sizeof(struct sctp_chunkhdr);
6051 memset(&src, 0, sizeof(struct sockaddr_in));
6052 src.sin_family = AF_INET;
6053 src.sin_len = sizeof(struct sockaddr_in);
6054 src.sin_port = sh->src_port;
6055 src.sin_addr = ip->ip_src;
6056 memset(&dst, 0, sizeof(struct sockaddr_in));
6057 dst.sin_family = AF_INET;
6058 dst.sin_len = sizeof(struct sockaddr_in);
6059 dst.sin_port = sh->dest_port;
6060 dst.sin_addr = ip->ip_dst;
6061 length = ip->ip_len + iphlen;
6062 /* Validate mbuf chain length with IP payload length. */
6063 if (SCTP_HEADER_LEN(m) != length) {
6064 SCTPDBG(SCTP_DEBUG_INPUT1,
6065 "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
6066 SCTP_STAT_INCR(sctps_hdrops);
6069 /* SCTP does not allow broadcasts or multicasts */
6070 if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
6073 if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
6076 ecn_bits = ip->ip_tos;
6077 #if defined(SCTP_WITH_NO_CSUM)
6078 SCTP_STAT_INCR(sctps_recvnocrc);
6080 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
6081 SCTP_STAT_INCR(sctps_recvhwcrc);
6084 SCTP_STAT_INCR(sctps_recvswcrc);
6088 sctp_common_input_processing(&m, iphlen, offset, length,
6089 (struct sockaddr *)&src,
6090 (struct sockaddr *)&dst,
6092 #if !defined(SCTP_WITH_NO_CSUM)
6096 use_mflowid, mflowid,
6105 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6106 extern int *sctp_cpuarry;
6111 sctp_input(struct mbuf *m, int off)
6113 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6118 uint32_t flowid, tag;
6121 if (m->m_flags & M_FLOWID) {
6122 flowid = m->m_pkthdr.flowid;
6125 * No flow id built by lower layers fix it so we
6128 offset = off + sizeof(struct sctphdr);
6129 if (SCTP_BUF_LEN(m) < offset) {
6130 if ((m = m_pullup(m, offset)) == NULL) {
6131 SCTP_STAT_INCR(sctps_hdrops);
6135 ip = mtod(m, struct ip *);
6136 sh = (struct sctphdr *)((caddr_t)ip + off);
6137 tag = htonl(sh->v_tag);
6138 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6139 m->m_pkthdr.flowid = flowid;
6140 m->m_flags |= M_FLOWID;
6142 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6143 sctp_queue_to_mcore(m, off, cpu_to_use);
6147 sctp_input_with_port(m, off, 0);