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_cause(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_cause(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_cause(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_cause(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_cause(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 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
154 "Problem with AUTH parameters");
155 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
156 use_mflowid, mflowid,
159 *abort_no_unlock = 1;
163 * We are only accepting if we have a socket with positive
166 if ((stcb == NULL) &&
167 ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
168 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
169 (inp->sctp_socket == NULL) ||
170 (inp->sctp_socket->so_qlimit == 0))) {
172 * FIX ME ?? What about TCP model and we have a
173 * match/restart case? Actually no fix is needed. the lookup
174 * will always find the existing assoc so stcb would not be
175 * NULL. It may be questionable to do this since we COULD
176 * just send back the INIT-ACK and hope that the app did
177 * accept()'s by the time the COOKIE was sent. But there is
178 * a price to pay for COOKIE generation and I don't want to
179 * pay it on the chance that the app will actually do some
180 * accepts(). The App just looses and should NOT be in this
183 if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
184 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
186 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
187 use_mflowid, mflowid,
192 if ((stcb != NULL) &&
193 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
194 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
195 sctp_send_shutdown_ack(stcb, NULL);
196 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
198 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
199 sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, src, dst,
201 use_mflowid, mflowid,
203 ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED));
207 SCTP_INP_RUNLOCK(inp);
212 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
216 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
217 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
224 struct sctp_stream_queue_pending *sp;
225 struct sctp_association *asoc;
228 * This function returns the number of streams that have true unsent
229 * data on them. Note that as it looks through it will clean up any
230 * places that have old data that has been sent but left at top of
234 SCTP_TCB_SEND_LOCK(stcb);
235 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
236 /* Check to see if some data queued */
237 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
238 /* sa_ignore FREED_MEMORY */
239 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
243 if ((sp->msg_is_complete) &&
245 (sp->sender_all_done)) {
247 * We are doing differed cleanup. Last time
248 * through when we took all the data the
249 * sender_all_done was not set.
251 if (sp->put_last_out == 0) {
252 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
253 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
259 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
260 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
262 sctp_free_remote_addr(sp->net);
266 sctp_m_freem(sp->data);
269 sctp_free_a_strmoq(stcb, sp, so_locked);
276 SCTP_TCB_SEND_UNLOCK(stcb);
277 return (unsent_data);
281 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
283 struct sctp_init *init;
284 struct sctp_association *asoc;
285 struct sctp_nets *lnet;
290 /* save off parameters */
291 asoc->peer_vtag = ntohl(init->initiate_tag);
292 asoc->peers_rwnd = ntohl(init->a_rwnd);
294 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
296 if (!TAILQ_EMPTY(&asoc->nets)) {
297 /* update any ssthresh's that may have a default */
298 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
299 lnet->ssthresh = asoc->peers_rwnd;
300 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
301 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
305 SCTP_TCB_SEND_LOCK(stcb);
306 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
308 struct sctp_stream_out *outs;
309 struct sctp_stream_queue_pending *sp, *nsp;
310 struct sctp_tmit_chunk *chk, *nchk;
312 /* abandon the upper streams */
313 newcnt = ntohs(init->num_inbound_streams);
314 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
315 if (chk->rec.data.stream_number >= newcnt) {
316 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
317 asoc->send_queue_cnt--;
318 if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
319 asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
322 panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number);
325 if (chk->data != NULL) {
326 sctp_free_bufspace(stcb, asoc, chk, 1);
327 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
328 0, chk, SCTP_SO_NOT_LOCKED);
330 sctp_m_freem(chk->data);
334 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
335 /* sa_ignore FREED_MEMORY */
339 for (i = newcnt; i < asoc->pre_open_streams; i++) {
340 outs = &asoc->strmout[i];
341 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
342 TAILQ_REMOVE(&outs->outqueue, sp, next);
343 asoc->stream_queue_cnt--;
344 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
345 stcb, 0, sp, SCTP_SO_NOT_LOCKED);
347 sctp_m_freem(sp->data);
351 sctp_free_remote_addr(sp->net);
355 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
356 /* sa_ignore FREED_MEMORY */
360 /* cut back the count */
361 asoc->pre_open_streams = newcnt;
363 SCTP_TCB_SEND_UNLOCK(stcb);
364 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
366 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
367 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
368 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
369 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
371 /* This is the next one we expect */
372 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
374 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
375 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
377 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
378 /* open the requested streams */
380 if (asoc->strmin != NULL) {
381 /* Free the old ones */
382 struct sctp_queued_to_read *ctl, *nctl;
384 for (i = 0; i < asoc->streamincnt; i++) {
385 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) {
386 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
387 sctp_free_remote_addr(ctl->whoFrom);
389 sctp_m_freem(ctl->data);
391 sctp_free_a_readq(stcb, ctl);
394 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
396 if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
397 asoc->streamincnt = ntohs(init->num_outbound_streams);
399 asoc->streamincnt = asoc->max_inbound_streams;
401 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
402 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
403 if (asoc->strmin == NULL) {
404 /* we didn't get memory for the streams! */
405 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
408 for (i = 0; i < asoc->streamincnt; i++) {
409 asoc->strmin[i].stream_no = i;
410 asoc->strmin[i].last_sequence_delivered = 0xffff;
411 TAILQ_INIT(&asoc->strmin[i].inqueue);
412 asoc->strmin[i].delivery_started = 0;
415 * load_address_from_init will put the addresses into the
416 * association when the COOKIE is processed or the INIT-ACK is
417 * processed. Both types of COOKIE's existing and new call this
418 * routine. It will remove addresses that are no longer in the
419 * association (for the restarting case where addresses are
420 * removed). Up front when the INIT arrives we will discard it if it
421 * is a restart and new addresses have been added.
423 /* sa_ignore MEMLEAK */
428 * INIT-ACK message processing/consumption returns value < 0 on error
431 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
432 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
433 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
434 struct sctp_nets *net, int *abort_no_unlock,
435 uint8_t use_mflowid, uint32_t mflowid,
438 struct sctp_association *asoc;
440 int retval, abort_flag;
441 uint32_t initack_limit;
442 int nat_friendly = 0;
444 /* First verify that we have no illegal param's */
447 op_err = sctp_arethere_unrecognized_parameters(m,
448 (offset + sizeof(struct sctp_init_chunk)),
449 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
451 /* Send an abort and notify peer */
452 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
453 *abort_no_unlock = 1;
457 asoc->peer_supports_nat = (uint8_t) nat_friendly;
458 /* process the peer's parameters in the INIT-ACK */
459 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
463 initack_limit = offset + ntohs(cp->ch.chunk_length);
464 /* load all addresses */
465 if ((retval = sctp_load_addresses_from_init(stcb, m,
466 (offset + sizeof(struct sctp_init_chunk)), initack_limit,
468 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
469 "Problem with address parameters");
470 SCTPDBG(SCTP_DEBUG_INPUT1,
471 "Load addresses from INIT causes an abort %d\n",
473 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
474 src, dst, sh, op_err,
475 use_mflowid, mflowid,
477 *abort_no_unlock = 1;
480 /* if the peer doesn't support asconf, flush the asconf queue */
481 if (asoc->peer_supports_asconf == 0) {
482 struct sctp_asconf_addr *param, *nparam;
484 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
485 TAILQ_REMOVE(&asoc->asconf_queue, param, next);
486 SCTP_FREE(param, SCTP_M_ASC_ADDR);
489 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
490 stcb->asoc.local_hmacs);
492 sctp_queue_op_err(stcb, op_err);
493 /* queuing will steal away the mbuf chain to the out queue */
496 /* extract the cookie and queue it to "echo" it back... */
497 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
498 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
499 stcb->asoc.overall_error_count,
501 SCTP_FROM_SCTP_INPUT,
504 stcb->asoc.overall_error_count = 0;
505 net->error_count = 0;
508 * Cancel the INIT timer, We do this first before queueing the
509 * cookie. We always cancel at the primary to assue that we are
510 * canceling the timer started by the INIT which always goes to the
513 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
514 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
516 /* calculate the RTO */
517 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy,
518 SCTP_RTT_FROM_NON_DATA);
520 retval = sctp_send_cookie_echo(m, offset, stcb, net);
523 * No cookie, we probably should send a op error. But in any
524 * case if there is no cookie in the INIT-ACK, we can
525 * abandon the peer, its broke.
528 /* We abort with an error of missing mandatory param */
529 op_err = sctp_generate_cause(SCTP_CAUSE_MISSING_PARAM, "");
532 * Expand beyond to include the mandatory
535 struct sctp_inv_mandatory_param *mp;
537 SCTP_BUF_LEN(op_err) =
538 sizeof(struct sctp_inv_mandatory_param);
540 struct sctp_inv_mandatory_param *);
541 /* Subtract the reserved param */
543 htons(sizeof(struct sctp_inv_mandatory_param) - 2);
544 mp->num_param = htonl(1);
545 mp->param = htons(SCTP_STATE_COOKIE);
548 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
549 src, dst, sh, op_err,
550 use_mflowid, mflowid,
552 *abort_no_unlock = 1;
560 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
561 struct sctp_tcb *stcb, struct sctp_nets *net)
563 struct sockaddr_storage store;
564 struct sctp_nets *r_net, *f_net;
567 uint16_t old_error_counter;
570 struct sockaddr_in *sin;
574 struct sockaddr_in6 *sin6;
578 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
582 memset(&store, 0, sizeof(store));
583 switch (cp->heartbeat.hb_info.addr_family) {
586 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
587 sin = (struct sockaddr_in *)&store;
588 sin->sin_family = cp->heartbeat.hb_info.addr_family;
589 sin->sin_len = cp->heartbeat.hb_info.addr_len;
590 sin->sin_port = stcb->rport;
591 memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address,
592 sizeof(sin->sin_addr));
600 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
601 sin6 = (struct sockaddr_in6 *)&store;
602 sin6->sin6_family = cp->heartbeat.hb_info.addr_family;
603 sin6->sin6_len = cp->heartbeat.hb_info.addr_len;
604 sin6->sin6_port = stcb->rport;
605 memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address,
606 sizeof(sin6->sin6_addr));
615 r_net = sctp_findnet(stcb, (struct sockaddr *)&store);
617 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
620 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
621 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
622 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
624 * If the its a HB and it's random value is correct when can
625 * confirm the destination.
627 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
628 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
629 stcb->asoc.primary_destination = r_net;
630 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
631 f_net = TAILQ_FIRST(&stcb->asoc.nets);
632 if (f_net != r_net) {
634 * first one on the list is NOT the primary
635 * sctp_cmpaddr() is much more efficent if
636 * the primary is the first on the list,
639 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
640 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
644 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
645 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
646 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
647 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
649 old_error_counter = r_net->error_count;
650 r_net->error_count = 0;
651 r_net->hb_responded = 1;
652 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
653 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
654 /* Now lets do a RTO with this */
655 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy,
656 SCTP_RTT_FROM_NON_DATA);
657 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
658 r_net->dest_state |= SCTP_ADDR_REACHABLE;
659 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
660 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
662 if (r_net->dest_state & SCTP_ADDR_PF) {
663 r_net->dest_state &= ~SCTP_ADDR_PF;
664 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
666 if (old_error_counter > 0) {
667 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
668 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
670 if (r_net == stcb->asoc.primary_destination) {
671 if (stcb->asoc.alternate) {
672 /* release the alternate, primary is good */
673 sctp_free_remote_addr(stcb->asoc.alternate);
674 stcb->asoc.alternate = NULL;
677 /* Mobility adaptation */
679 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
680 SCTP_MOBILITY_BASE) ||
681 sctp_is_mobility_feature_on(stcb->sctp_ep,
682 SCTP_MOBILITY_FASTHANDOFF)) &&
683 sctp_is_mobility_feature_on(stcb->sctp_ep,
684 SCTP_MOBILITY_PRIM_DELETED)) {
686 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7);
687 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
688 SCTP_MOBILITY_FASTHANDOFF)) {
689 sctp_assoc_immediate_retrans(stcb,
690 stcb->asoc.primary_destination);
692 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
693 SCTP_MOBILITY_BASE)) {
694 sctp_move_chunks_from_net(stcb,
695 stcb->asoc.deleted_primary);
697 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
698 stcb->asoc.deleted_primary);
704 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
707 * return 0 means we want you to proceed with the abort non-zero
708 * means no abort processing
710 struct sctpasochead *head;
712 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
713 /* generate a new vtag and send init */
714 LIST_REMOVE(stcb, sctp_asocs);
715 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
716 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
718 * put it in the bucket in the vtag hash of assoc's for the
721 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
722 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
725 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
727 * treat like a case where the cookie expired i.e.: - dump
728 * current cookie. - generate a new vtag. - resend init.
730 /* generate a new vtag and send init */
731 LIST_REMOVE(stcb, sctp_asocs);
732 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
733 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
734 sctp_stop_all_cookie_timers(stcb);
735 sctp_toss_old_cookies(stcb, &stcb->asoc);
736 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
737 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
739 * put it in the bucket in the vtag hash of assoc's for the
742 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
743 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
750 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
751 struct sctp_nets *net)
754 * return 0 means we want you to proceed with the abort non-zero
755 * means no abort processing
757 if (stcb->asoc.peer_supports_auth == 0) {
758 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
761 sctp_asconf_send_nat_state_update(stcb, net);
767 sctp_handle_abort(struct sctp_abort_chunk *abort,
768 struct sctp_tcb *stcb, struct sctp_nets *net)
770 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
777 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
781 len = ntohs(abort->ch.chunk_length);
782 if (len > sizeof(struct sctp_chunkhdr)) {
784 * Need to check the cause codes for our two magic nat
785 * aborts which don't kill the assoc necessarily.
787 struct sctp_missing_nat_state *natc;
789 natc = (struct sctp_missing_nat_state *)(abort + 1);
790 error = ntohs(natc->cause);
791 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
792 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
793 abort->ch.chunk_flags);
794 if (sctp_handle_nat_colliding_state(stcb)) {
797 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
798 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
799 abort->ch.chunk_flags);
800 if (sctp_handle_nat_missing_state(stcb, net)) {
807 /* stop any receive timers */
808 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
809 /* notify user of the abort and clean up... */
810 sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
812 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
813 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
814 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
815 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
817 #ifdef SCTP_ASOCLOG_OF_TSNS
818 sctp_print_out_track_log(stcb);
820 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
821 so = SCTP_INP_SO(stcb->sctp_ep);
822 atomic_add_int(&stcb->asoc.refcnt, 1);
823 SCTP_TCB_UNLOCK(stcb);
824 SCTP_SOCKET_LOCK(so, 1);
826 atomic_subtract_int(&stcb->asoc.refcnt, 1);
828 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
829 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
830 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
831 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
832 SCTP_SOCKET_UNLOCK(so, 1);
834 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
838 sctp_start_net_timers(struct sctp_tcb *stcb)
840 uint32_t cnt_hb_sent;
841 struct sctp_nets *net;
844 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
846 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
847 * If the dest in unconfirmed send a hb as well if under
848 * max_hb_burst have been sent.
850 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
851 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
852 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
853 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
854 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
859 sctp_chunk_output(stcb->sctp_ep, stcb,
860 SCTP_OUTPUT_FROM_COOKIE_ACK,
867 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
868 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
870 struct sctp_association *asoc;
871 int some_on_streamwheel;
873 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
878 SCTPDBG(SCTP_DEBUG_INPUT2,
879 "sctp_handle_shutdown: handling SHUTDOWN\n");
883 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
884 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
887 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
888 /* Shutdown NOT the expected size */
891 sctp_update_acked(stcb, cp, abort_flag);
896 if (asoc->control_pdapi) {
898 * With a normal shutdown we assume the end of last record.
900 SCTP_INP_READ_LOCK(stcb->sctp_ep);
901 asoc->control_pdapi->end_added = 1;
902 asoc->control_pdapi->pdapi_aborted = 1;
903 asoc->control_pdapi = NULL;
904 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
905 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
906 so = SCTP_INP_SO(stcb->sctp_ep);
907 atomic_add_int(&stcb->asoc.refcnt, 1);
908 SCTP_TCB_UNLOCK(stcb);
909 SCTP_SOCKET_LOCK(so, 1);
911 atomic_subtract_int(&stcb->asoc.refcnt, 1);
912 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
913 /* assoc was freed while we were unlocked */
914 SCTP_SOCKET_UNLOCK(so, 1);
918 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
919 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
920 SCTP_SOCKET_UNLOCK(so, 1);
923 /* goto SHUTDOWN_RECEIVED state to block new requests */
924 if (stcb->sctp_socket) {
925 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
926 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
927 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
928 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
929 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
931 * notify upper layer that peer has initiated a
934 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
937 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
940 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
942 * stop the shutdown timer, since we WILL move to
945 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
947 /* Now is there unsent data on a stream somewhere? */
948 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
950 if (!TAILQ_EMPTY(&asoc->send_queue) ||
951 !TAILQ_EMPTY(&asoc->sent_queue) ||
952 some_on_streamwheel) {
953 /* By returning we will push more data out */
956 /* no outstanding data to send, so move on... */
957 /* send SHUTDOWN-ACK */
958 /* move to SHUTDOWN-ACK-SENT state */
959 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
960 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
961 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
963 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
964 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
965 sctp_stop_timers_for_shutdown(stcb);
966 sctp_send_shutdown_ack(stcb, net);
967 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
973 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
974 struct sctp_tcb *stcb,
975 struct sctp_nets *net)
977 struct sctp_association *asoc;
979 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
982 so = SCTP_INP_SO(stcb->sctp_ep);
984 SCTPDBG(SCTP_DEBUG_INPUT2,
985 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
990 /* process according to association state */
991 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
992 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
993 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
994 sctp_send_shutdown_complete(stcb, net, 1);
995 SCTP_TCB_UNLOCK(stcb);
998 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
999 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1000 /* unexpected SHUTDOWN-ACK... so ignore... */
1001 SCTP_TCB_UNLOCK(stcb);
1004 if (asoc->control_pdapi) {
1006 * With a normal shutdown we assume the end of last record.
1008 SCTP_INP_READ_LOCK(stcb->sctp_ep);
1009 asoc->control_pdapi->end_added = 1;
1010 asoc->control_pdapi->pdapi_aborted = 1;
1011 asoc->control_pdapi = NULL;
1012 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1013 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1014 atomic_add_int(&stcb->asoc.refcnt, 1);
1015 SCTP_TCB_UNLOCK(stcb);
1016 SCTP_SOCKET_LOCK(so, 1);
1017 SCTP_TCB_LOCK(stcb);
1018 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1019 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1020 /* assoc was freed while we were unlocked */
1021 SCTP_SOCKET_UNLOCK(so, 1);
1025 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1026 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1027 SCTP_SOCKET_UNLOCK(so, 1);
1031 if (!TAILQ_EMPTY(&asoc->send_queue) ||
1032 !TAILQ_EMPTY(&asoc->sent_queue) ||
1033 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
1034 panic("Queues are not empty when handling SHUTDOWN-ACK");
1037 /* stop the timer */
1038 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
1039 /* send SHUTDOWN-COMPLETE */
1040 sctp_send_shutdown_complete(stcb, net, 0);
1041 /* notify upper layer protocol */
1042 if (stcb->sctp_socket) {
1043 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1044 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1045 stcb->sctp_socket->so_snd.sb_cc = 0;
1047 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1049 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1050 /* free the TCB but first save off the ep */
1051 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1052 atomic_add_int(&stcb->asoc.refcnt, 1);
1053 SCTP_TCB_UNLOCK(stcb);
1054 SCTP_SOCKET_LOCK(so, 1);
1055 SCTP_TCB_LOCK(stcb);
1056 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1058 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1059 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1060 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1061 SCTP_SOCKET_UNLOCK(so, 1);
1066 * Skip past the param header and then we will find the chunk that caused the
1067 * problem. There are two possiblities ASCONF or FWD-TSN other than that and
1068 * our peer must be broken.
1071 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1072 struct sctp_nets *net)
1074 struct sctp_chunkhdr *chk;
1076 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1077 switch (chk->chunk_type) {
1078 case SCTP_ASCONF_ACK:
1080 sctp_asconf_cleanup(stcb, net);
1082 case SCTP_FORWARD_CUM_TSN:
1083 stcb->asoc.peer_supports_prsctp = 0;
1086 SCTPDBG(SCTP_DEBUG_INPUT2,
1087 "Peer does not support chunk type %d(%x)??\n",
1088 chk->chunk_type, (uint32_t) chk->chunk_type);
1094 * Skip past the param header and then we will find the param that caused the
1095 * problem. There are a number of param's in a ASCONF OR the prsctp param
1096 * these will turn of specific features.
1099 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1101 struct sctp_paramhdr *pbad;
1104 switch (ntohs(pbad->param_type)) {
1106 case SCTP_PRSCTP_SUPPORTED:
1107 stcb->asoc.peer_supports_prsctp = 0;
1109 case SCTP_SUPPORTED_CHUNK_EXT:
1111 /* draft-ietf-tsvwg-addip-sctp */
1112 case SCTP_HAS_NAT_SUPPORT:
1113 stcb->asoc.peer_supports_nat = 0;
1115 case SCTP_ADD_IP_ADDRESS:
1116 case SCTP_DEL_IP_ADDRESS:
1117 case SCTP_SET_PRIM_ADDR:
1118 stcb->asoc.peer_supports_asconf = 0;
1120 case SCTP_SUCCESS_REPORT:
1121 case SCTP_ERROR_CAUSE_IND:
1122 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1123 SCTPDBG(SCTP_DEBUG_INPUT2,
1124 "Turning off ASCONF to this strange peer\n");
1125 stcb->asoc.peer_supports_asconf = 0;
1128 SCTPDBG(SCTP_DEBUG_INPUT2,
1129 "Peer does not support param type %d(%x)??\n",
1130 pbad->param_type, (uint32_t) pbad->param_type);
1136 sctp_handle_error(struct sctp_chunkhdr *ch,
1137 struct sctp_tcb *stcb, struct sctp_nets *net)
1140 struct sctp_paramhdr *phdr;
1141 uint16_t error, error_type;
1143 struct sctp_association *asoc;
1146 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1151 /* parse through all of the errors and process */
1153 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1154 sizeof(struct sctp_chunkhdr));
1155 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1157 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1158 /* Process an Error Cause */
1159 error_type = ntohs(phdr->param_type);
1160 error_len = ntohs(phdr->param_length);
1161 if ((error_len > chklen) || (error_len == 0)) {
1162 /* invalid param length for this param */
1163 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1168 /* report the first error cause */
1171 switch (error_type) {
1172 case SCTP_CAUSE_INVALID_STREAM:
1173 case SCTP_CAUSE_MISSING_PARAM:
1174 case SCTP_CAUSE_INVALID_PARAM:
1175 case SCTP_CAUSE_NO_USER_DATA:
1176 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1179 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1180 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1182 if (sctp_handle_nat_colliding_state(stcb)) {
1186 case SCTP_CAUSE_NAT_MISSING_STATE:
1187 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1189 if (sctp_handle_nat_missing_state(stcb, net)) {
1193 case SCTP_CAUSE_STALE_COOKIE:
1195 * We only act if we have echoed a cookie and are
1198 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1201 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1202 /* Save the time doubled */
1203 asoc->cookie_preserve_req = ntohl(*p) << 1;
1204 asoc->stale_cookie_count++;
1205 if (asoc->stale_cookie_count >
1206 asoc->max_init_times) {
1207 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1208 /* now free the asoc */
1209 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1210 so = SCTP_INP_SO(stcb->sctp_ep);
1211 atomic_add_int(&stcb->asoc.refcnt, 1);
1212 SCTP_TCB_UNLOCK(stcb);
1213 SCTP_SOCKET_LOCK(so, 1);
1214 SCTP_TCB_LOCK(stcb);
1215 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1217 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1218 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1219 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1220 SCTP_SOCKET_UNLOCK(so, 1);
1224 /* blast back to INIT state */
1225 sctp_toss_old_cookies(stcb, &stcb->asoc);
1226 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1227 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1228 sctp_stop_all_cookie_timers(stcb);
1229 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1232 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1234 * Nothing we can do here, we don't do hostname
1235 * addresses so if the peer does not like my IPv6
1236 * (or IPv4 for that matter) it does not matter. If
1237 * they don't support that type of address, they can
1238 * NOT possibly get that packet type... i.e. with no
1239 * IPv6 you can't recieve a IPv6 packet. so we can
1240 * safely ignore this one. If we ever added support
1241 * for HOSTNAME Addresses, then we would need to do
1245 case SCTP_CAUSE_UNRECOG_CHUNK:
1246 sctp_process_unrecog_chunk(stcb, phdr, net);
1248 case SCTP_CAUSE_UNRECOG_PARAM:
1249 sctp_process_unrecog_param(stcb, phdr);
1251 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1253 * We ignore this since the timer will drive out a
1254 * new cookie anyway and there timer will drive us
1255 * to send a SHUTDOWN_COMPLETE. We can't send one
1256 * here since we don't have their tag.
1259 case SCTP_CAUSE_DELETING_LAST_ADDR:
1260 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1261 case SCTP_CAUSE_DELETING_SRC_ADDR:
1263 * We should NOT get these here, but in a
1266 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1269 case SCTP_CAUSE_OUT_OF_RESC:
1271 * And what, pray tell do we do with the fact that
1272 * the peer is out of resources? Not really sure we
1273 * could do anything but abort. I suspect this
1274 * should have came WITH an abort instead of in a
1279 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1283 adjust = SCTP_SIZE32(error_len);
1285 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1287 sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, error, ch, SCTP_SO_NOT_LOCKED);
1292 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1293 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1294 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1295 struct sctp_nets *net, int *abort_no_unlock,
1296 uint8_t use_mflowid, uint32_t mflowid,
1299 struct sctp_init_ack *init_ack;
1300 struct mbuf *op_err;
1302 SCTPDBG(SCTP_DEBUG_INPUT2,
1303 "sctp_handle_init_ack: handling INIT-ACK\n");
1306 SCTPDBG(SCTP_DEBUG_INPUT2,
1307 "sctp_handle_init_ack: TCB is null\n");
1310 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1311 /* Invalid length */
1312 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1313 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1314 src, dst, sh, op_err,
1315 use_mflowid, mflowid,
1317 *abort_no_unlock = 1;
1320 init_ack = &cp->init;
1321 /* validate parameters */
1322 if (init_ack->initiate_tag == 0) {
1323 /* protocol error... send an abort */
1324 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1325 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1326 src, dst, sh, op_err,
1327 use_mflowid, mflowid,
1329 *abort_no_unlock = 1;
1332 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1333 /* protocol error... send an abort */
1334 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1335 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1336 src, dst, sh, op_err,
1337 use_mflowid, mflowid,
1339 *abort_no_unlock = 1;
1342 if (init_ack->num_inbound_streams == 0) {
1343 /* protocol error... send an abort */
1344 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1345 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1346 src, dst, sh, op_err,
1347 use_mflowid, mflowid,
1349 *abort_no_unlock = 1;
1352 if (init_ack->num_outbound_streams == 0) {
1353 /* protocol error... send an abort */
1354 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1355 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1356 src, dst, sh, op_err,
1357 use_mflowid, mflowid,
1359 *abort_no_unlock = 1;
1362 /* process according to association state... */
1363 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1364 case SCTP_STATE_COOKIE_WAIT:
1365 /* this is the expected state for this chunk */
1366 /* process the INIT-ACK parameters */
1367 if (stcb->asoc.primary_destination->dest_state &
1368 SCTP_ADDR_UNCONFIRMED) {
1370 * The primary is where we sent the INIT, we can
1371 * always consider it confirmed when the INIT-ACK is
1372 * returned. Do this before we load addresses
1375 stcb->asoc.primary_destination->dest_state &=
1376 ~SCTP_ADDR_UNCONFIRMED;
1377 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1378 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1380 if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1381 net, abort_no_unlock,
1382 use_mflowid, mflowid,
1384 /* error in parsing parameters */
1387 /* update our state */
1388 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1389 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1391 /* reset the RTO calc */
1392 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1393 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1394 stcb->asoc.overall_error_count,
1396 SCTP_FROM_SCTP_INPUT,
1399 stcb->asoc.overall_error_count = 0;
1400 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1402 * collapse the init timer back in case of a exponential
1405 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1408 * the send at the end of the inbound data processing will
1409 * cause the cookie to be sent
1412 case SCTP_STATE_SHUTDOWN_SENT:
1413 /* incorrect state... discard */
1415 case SCTP_STATE_COOKIE_ECHOED:
1416 /* incorrect state... discard */
1418 case SCTP_STATE_OPEN:
1419 /* incorrect state... discard */
1421 case SCTP_STATE_EMPTY:
1422 case SCTP_STATE_INUSE:
1424 /* incorrect state... discard */
1428 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1432 static struct sctp_tcb *
1433 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1434 struct sockaddr *src, struct sockaddr *dst,
1435 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1436 struct sctp_inpcb *inp, struct sctp_nets **netp,
1437 struct sockaddr *init_src, int *notification,
1438 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1439 uint8_t use_mflowid, uint32_t mflowid,
1440 uint32_t vrf_id, uint16_t port);
1444 * handle a state cookie for an existing association m: input packet mbuf
1445 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1446 * "split" mbuf and the cookie signature does not exist offset: offset into
1447 * mbuf to the cookie-echo chunk
1449 static struct sctp_tcb *
1450 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1451 struct sockaddr *src, struct sockaddr *dst,
1452 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1453 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1454 struct sockaddr *init_src, int *notification,
1455 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1456 uint8_t use_mflowid, uint32_t mflowid,
1457 uint32_t vrf_id, uint16_t port)
1459 struct sctp_association *asoc;
1460 struct sctp_init_chunk *init_cp, init_buf;
1461 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1462 struct sctp_nets *net;
1463 struct mbuf *op_err;
1464 int init_offset, initack_offset, i;
1470 /* I know that the TCB is non-NULL from the caller */
1472 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1473 if (asoc->cookie_how[how_indx] == 0)
1476 if (how_indx < sizeof(asoc->cookie_how)) {
1477 asoc->cookie_how[how_indx] = 1;
1479 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1480 /* SHUTDOWN came in after sending INIT-ACK */
1481 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1482 op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
1483 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1484 use_mflowid, mflowid,
1486 if (how_indx < sizeof(asoc->cookie_how))
1487 asoc->cookie_how[how_indx] = 2;
1491 * find and validate the INIT chunk in the cookie (peer's info) the
1492 * INIT should start after the cookie-echo header struct (chunk
1493 * header, state cookie header struct)
1495 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1497 init_cp = (struct sctp_init_chunk *)
1498 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1499 (uint8_t *) & init_buf);
1500 if (init_cp == NULL) {
1501 /* could not pull a INIT chunk in cookie */
1504 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1508 * find and validate the INIT-ACK chunk in the cookie (my info) the
1509 * INIT-ACK follows the INIT chunk
1511 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1512 initack_cp = (struct sctp_init_ack_chunk *)
1513 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1514 (uint8_t *) & initack_buf);
1515 if (initack_cp == NULL) {
1516 /* could not pull INIT-ACK chunk in cookie */
1519 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1522 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1523 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1525 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1526 * to get into the OPEN state
1528 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1530 * Opps, this means that we somehow generated two vtag's
1531 * the same. I.e. we did:
1533 * <---INIT(tag=a)------
1534 * ----INIT-ACK(tag=t)-->
1535 * ----INIT(tag=t)------> *1
1536 * <---INIT-ACK(tag=a)---
1537 * <----CE(tag=t)------------- *2
1539 * At point *1 we should be generating a different
1540 * tag t'. Which means we would throw away the CE and send
1541 * ours instead. Basically this is case C (throw away side).
1543 if (how_indx < sizeof(asoc->cookie_how))
1544 asoc->cookie_how[how_indx] = 17;
1548 switch (SCTP_GET_STATE(asoc)) {
1549 case SCTP_STATE_COOKIE_WAIT:
1550 case SCTP_STATE_COOKIE_ECHOED:
1552 * INIT was sent but got a COOKIE_ECHO with the
1553 * correct tags... just accept it...but we must
1554 * process the init so that we can make sure we have
1555 * the right seq no's.
1557 /* First we must process the INIT !! */
1558 retval = sctp_process_init(init_cp, stcb);
1560 if (how_indx < sizeof(asoc->cookie_how))
1561 asoc->cookie_how[how_indx] = 3;
1564 /* we have already processed the INIT so no problem */
1565 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1566 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1567 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1568 /* update current state */
1569 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1570 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1572 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1574 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1575 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1576 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1577 stcb->sctp_ep, stcb, asoc->primary_destination);
1579 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1580 sctp_stop_all_cookie_timers(stcb);
1581 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1582 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1583 (inp->sctp_socket->so_qlimit == 0)
1585 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1590 * Here is where collision would go if we
1591 * did a connect() and instead got a
1592 * init/init-ack/cookie done before the
1593 * init-ack came back..
1595 stcb->sctp_ep->sctp_flags |=
1596 SCTP_PCB_FLAGS_CONNECTED;
1597 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1598 so = SCTP_INP_SO(stcb->sctp_ep);
1599 atomic_add_int(&stcb->asoc.refcnt, 1);
1600 SCTP_TCB_UNLOCK(stcb);
1601 SCTP_SOCKET_LOCK(so, 1);
1602 SCTP_TCB_LOCK(stcb);
1603 atomic_add_int(&stcb->asoc.refcnt, -1);
1604 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1605 SCTP_SOCKET_UNLOCK(so, 1);
1609 soisconnected(stcb->sctp_socket);
1610 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1611 SCTP_SOCKET_UNLOCK(so, 1);
1614 /* notify upper layer */
1615 *notification = SCTP_NOTIFY_ASSOC_UP;
1617 * since we did not send a HB make sure we don't
1620 net->hb_responded = 1;
1621 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1622 &cookie->time_entered,
1623 sctp_align_unsafe_makecopy,
1624 SCTP_RTT_FROM_NON_DATA);
1626 if (stcb->asoc.sctp_autoclose_ticks &&
1627 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1628 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1634 * we're in the OPEN state (or beyond), so peer must
1635 * have simply lost the COOKIE-ACK
1639 sctp_stop_all_cookie_timers(stcb);
1641 * We ignore the return code here.. not sure if we should
1642 * somehow abort.. but we do have an existing asoc. This
1643 * really should not fail.
1645 if (sctp_load_addresses_from_init(stcb, m,
1646 init_offset + sizeof(struct sctp_init_chunk),
1647 initack_offset, src, dst, init_src)) {
1648 if (how_indx < sizeof(asoc->cookie_how))
1649 asoc->cookie_how[how_indx] = 4;
1652 /* respond with a COOKIE-ACK */
1653 sctp_toss_old_cookies(stcb, asoc);
1654 sctp_send_cookie_ack(stcb);
1655 if (how_indx < sizeof(asoc->cookie_how))
1656 asoc->cookie_how[how_indx] = 5;
1659 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1660 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1661 cookie->tie_tag_my_vtag == 0 &&
1662 cookie->tie_tag_peer_vtag == 0) {
1664 * case C in Section 5.2.4 Table 2: XMOO silently discard
1666 if (how_indx < sizeof(asoc->cookie_how))
1667 asoc->cookie_how[how_indx] = 6;
1671 * If nat support, and the below and stcb is established, send back
1672 * a ABORT(colliding state) if we are established.
1674 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1675 (asoc->peer_supports_nat) &&
1676 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1677 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1678 (asoc->peer_vtag == 0)))) {
1680 * Special case - Peer's support nat. We may have two init's
1681 * that we gave out the same tag on since one was not
1682 * established.. i.e. we get INIT from host-1 behind the nat
1683 * and we respond tag-a, we get a INIT from host-2 behind
1684 * the nat and we get tag-a again. Then we bring up host-1
1685 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1686 * Now we have colliding state. We must send an abort here
1687 * with colliding state indication.
1689 op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
1690 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1691 use_mflowid, mflowid,
1695 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1696 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1697 (asoc->peer_vtag == 0))) {
1699 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1700 * should be ok, re-accept peer info
1702 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1704 * Extension of case C. If we hit this, then the
1705 * random number generator returned the same vtag
1706 * when we first sent our INIT-ACK and when we later
1707 * sent our INIT. The side with the seq numbers that
1708 * are different will be the one that normnally
1709 * would have hit case C. This in effect "extends"
1710 * our vtags in this collision case to be 64 bits.
1711 * The same collision could occur aka you get both
1712 * vtag and seq number the same twice in a row.. but
1713 * is much less likely. If it did happen then we
1714 * would proceed through and bring up the assoc.. we
1715 * may end up with the wrong stream setup however..
1716 * which would be bad.. but there is no way to
1717 * tell.. until we send on a stream that does not
1720 if (how_indx < sizeof(asoc->cookie_how))
1721 asoc->cookie_how[how_indx] = 7;
1725 if (how_indx < sizeof(asoc->cookie_how))
1726 asoc->cookie_how[how_indx] = 8;
1727 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1728 sctp_stop_all_cookie_timers(stcb);
1730 * since we did not send a HB make sure we don't double
1733 net->hb_responded = 1;
1734 if (stcb->asoc.sctp_autoclose_ticks &&
1735 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1736 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1739 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1740 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1742 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1744 * Ok the peer probably discarded our data (if we
1745 * echoed a cookie+data). So anything on the
1746 * sent_queue should be marked for retransmit, we
1747 * may not get something to kick us so it COULD
1748 * still take a timeout to move these.. but it can't
1749 * hurt to mark them.
1751 struct sctp_tmit_chunk *chk;
1753 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1754 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1755 chk->sent = SCTP_DATAGRAM_RESEND;
1756 sctp_flight_size_decrease(chk);
1757 sctp_total_flight_decrease(stcb, chk);
1758 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1764 /* process the INIT info (peer's info) */
1765 retval = sctp_process_init(init_cp, stcb);
1767 if (how_indx < sizeof(asoc->cookie_how))
1768 asoc->cookie_how[how_indx] = 9;
1771 if (sctp_load_addresses_from_init(stcb, m,
1772 init_offset + sizeof(struct sctp_init_chunk),
1773 initack_offset, src, dst, init_src)) {
1774 if (how_indx < sizeof(asoc->cookie_how))
1775 asoc->cookie_how[how_indx] = 10;
1778 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1779 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1780 *notification = SCTP_NOTIFY_ASSOC_UP;
1782 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1783 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1784 (inp->sctp_socket->so_qlimit == 0)) {
1785 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1789 stcb->sctp_ep->sctp_flags |=
1790 SCTP_PCB_FLAGS_CONNECTED;
1791 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1792 so = SCTP_INP_SO(stcb->sctp_ep);
1793 atomic_add_int(&stcb->asoc.refcnt, 1);
1794 SCTP_TCB_UNLOCK(stcb);
1795 SCTP_SOCKET_LOCK(so, 1);
1796 SCTP_TCB_LOCK(stcb);
1797 atomic_add_int(&stcb->asoc.refcnt, -1);
1798 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1799 SCTP_SOCKET_UNLOCK(so, 1);
1803 soisconnected(stcb->sctp_socket);
1804 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1805 SCTP_SOCKET_UNLOCK(so, 1);
1808 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1809 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1811 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1812 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1813 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1814 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1816 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1818 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1819 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1820 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1821 stcb->sctp_ep, stcb, asoc->primary_destination);
1823 sctp_stop_all_cookie_timers(stcb);
1824 sctp_toss_old_cookies(stcb, asoc);
1825 sctp_send_cookie_ack(stcb);
1828 * only if we have retrans set do we do this. What
1829 * this call does is get only the COOKIE-ACK out and
1830 * then when we return the normal call to
1831 * sctp_chunk_output will get the retrans out behind
1834 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1836 if (how_indx < sizeof(asoc->cookie_how))
1837 asoc->cookie_how[how_indx] = 11;
1841 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1842 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1843 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1844 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1845 cookie->tie_tag_peer_vtag != 0) {
1846 struct sctpasochead *head;
1848 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1853 if (asoc->peer_supports_nat) {
1855 * This is a gross gross hack. Just call the
1856 * cookie_new code since we are allowing a duplicate
1857 * association. I hope this works...
1859 return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
1860 sh, cookie, cookie_len,
1861 inp, netp, init_src, notification,
1862 auth_skipped, auth_offset, auth_len,
1863 use_mflowid, mflowid,
1867 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1870 if (how_indx < sizeof(asoc->cookie_how))
1871 asoc->cookie_how[how_indx] = 12;
1872 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1873 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1875 /* notify upper layer */
1876 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1877 atomic_add_int(&stcb->asoc.refcnt, 1);
1878 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1879 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1880 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1881 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1883 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1884 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1885 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1886 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1888 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1889 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1890 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1891 stcb->sctp_ep, stcb, asoc->primary_destination);
1893 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1894 /* move to OPEN state, if not in SHUTDOWN_SENT */
1895 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1897 asoc->pre_open_streams =
1898 ntohs(initack_cp->init.num_outbound_streams);
1899 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1900 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1901 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1903 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1905 asoc->str_reset_seq_in = asoc->init_seq_number;
1907 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1908 if (asoc->mapping_array) {
1909 memset(asoc->mapping_array, 0,
1910 asoc->mapping_array_size);
1912 if (asoc->nr_mapping_array) {
1913 memset(asoc->nr_mapping_array, 0,
1914 asoc->mapping_array_size);
1916 SCTP_TCB_UNLOCK(stcb);
1917 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1918 so = SCTP_INP_SO(stcb->sctp_ep);
1919 SCTP_SOCKET_LOCK(so, 1);
1921 SCTP_INP_INFO_WLOCK();
1922 SCTP_INP_WLOCK(stcb->sctp_ep);
1923 SCTP_TCB_LOCK(stcb);
1924 atomic_add_int(&stcb->asoc.refcnt, -1);
1925 /* send up all the data */
1926 SCTP_TCB_SEND_LOCK(stcb);
1928 sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_LOCKED);
1929 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1930 stcb->asoc.strmout[i].chunks_on_queues = 0;
1931 stcb->asoc.strmout[i].stream_no = i;
1932 stcb->asoc.strmout[i].next_sequence_send = 0;
1933 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1935 /* process the INIT-ACK info (my info) */
1936 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1937 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1939 /* pull from vtag hash */
1940 LIST_REMOVE(stcb, sctp_asocs);
1941 /* re-insert to new vtag position */
1942 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1943 SCTP_BASE_INFO(hashasocmark))];
1945 * put it in the bucket in the vtag hash of assoc's for the
1948 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1950 SCTP_TCB_SEND_UNLOCK(stcb);
1951 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1952 SCTP_INP_INFO_WUNLOCK();
1953 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1954 SCTP_SOCKET_UNLOCK(so, 1);
1956 asoc->total_flight = 0;
1957 asoc->total_flight_count = 0;
1958 /* process the INIT info (peer's info) */
1959 retval = sctp_process_init(init_cp, stcb);
1961 if (how_indx < sizeof(asoc->cookie_how))
1962 asoc->cookie_how[how_indx] = 13;
1967 * since we did not send a HB make sure we don't double
1970 net->hb_responded = 1;
1972 if (sctp_load_addresses_from_init(stcb, m,
1973 init_offset + sizeof(struct sctp_init_chunk),
1974 initack_offset, src, dst, init_src)) {
1975 if (how_indx < sizeof(asoc->cookie_how))
1976 asoc->cookie_how[how_indx] = 14;
1980 /* respond with a COOKIE-ACK */
1981 sctp_stop_all_cookie_timers(stcb);
1982 sctp_toss_old_cookies(stcb, asoc);
1983 sctp_send_cookie_ack(stcb);
1984 if (how_indx < sizeof(asoc->cookie_how))
1985 asoc->cookie_how[how_indx] = 15;
1989 if (how_indx < sizeof(asoc->cookie_how))
1990 asoc->cookie_how[how_indx] = 16;
1991 /* all other cases... */
1997 * handle a state cookie for a new association m: input packet mbuf chain--
1998 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
1999 * and the cookie signature does not exist offset: offset into mbuf to the
2000 * cookie-echo chunk length: length of the cookie chunk to: where the init
2001 * was from returns a new TCB
2003 static struct sctp_tcb *
2004 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
2005 struct sockaddr *src, struct sockaddr *dst,
2006 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
2007 struct sctp_inpcb *inp, struct sctp_nets **netp,
2008 struct sockaddr *init_src, int *notification,
2009 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2010 uint8_t use_mflowid, uint32_t mflowid,
2011 uint32_t vrf_id, uint16_t port)
2013 struct sctp_tcb *stcb;
2014 struct sctp_init_chunk *init_cp, init_buf;
2015 struct sctp_init_ack_chunk *initack_cp, initack_buf;
2016 struct sockaddr_storage sa_store;
2017 struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
2018 struct sctp_association *asoc;
2019 int init_offset, initack_offset, initack_limit;
2022 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
2025 struct sockaddr_in *sin;
2029 struct sockaddr_in6 *sin6;
2032 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2035 so = SCTP_INP_SO(inp);
2039 * find and validate the INIT chunk in the cookie (peer's info) the
2040 * INIT should start after the cookie-echo header struct (chunk
2041 * header, state cookie header struct)
2043 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2044 init_cp = (struct sctp_init_chunk *)
2045 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2046 (uint8_t *) & init_buf);
2047 if (init_cp == NULL) {
2048 /* could not pull a INIT chunk in cookie */
2049 SCTPDBG(SCTP_DEBUG_INPUT1,
2050 "process_cookie_new: could not pull INIT chunk hdr\n");
2053 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2054 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2057 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2059 * find and validate the INIT-ACK chunk in the cookie (my info) the
2060 * INIT-ACK follows the INIT chunk
2062 initack_cp = (struct sctp_init_ack_chunk *)
2063 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2064 (uint8_t *) & initack_buf);
2065 if (initack_cp == NULL) {
2066 /* could not pull INIT-ACK chunk in cookie */
2067 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2070 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2074 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2075 * "initack_limit" value. This is because the chk_length field
2076 * includes the length of the cookie, but the cookie is omitted when
2077 * the INIT and INIT_ACK are tacked onto the cookie...
2079 initack_limit = offset + cookie_len;
2082 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2087 * Here we do a trick, we set in NULL for the proc/thread argument.
2088 * We do this since in effect we only use the p argument when the
2089 * socket is unbound and we must do an implicit bind. Since we are
2090 * getting a cookie, we cannot be unbound.
2092 stcb = sctp_aloc_assoc(inp, init_src, &error,
2093 ntohl(initack_cp->init.initiate_tag), vrf_id,
2094 (struct thread *)NULL
2097 struct mbuf *op_err;
2099 /* memory problem? */
2100 SCTPDBG(SCTP_DEBUG_INPUT1,
2101 "process_cookie_new: no room for another TCB!\n");
2102 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2103 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2104 src, dst, sh, op_err,
2105 use_mflowid, mflowid,
2109 /* get the correct sctp_nets */
2111 *netp = sctp_findnet(stcb, init_src);
2114 /* get scope variables out of cookie */
2115 asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2116 asoc->scope.site_scope = cookie->site_scope;
2117 asoc->scope.local_scope = cookie->local_scope;
2118 asoc->scope.loopback_scope = cookie->loopback_scope;
2120 if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2121 (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2122 struct mbuf *op_err;
2125 * Houston we have a problem. The EP changed while the
2126 * cookie was in flight. Only recourse is to abort the
2129 atomic_add_int(&stcb->asoc.refcnt, 1);
2130 op_err = sctp_generate_cause(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,
2135 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2136 SCTP_TCB_UNLOCK(stcb);
2137 SCTP_SOCKET_LOCK(so, 1);
2138 SCTP_TCB_LOCK(stcb);
2140 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2141 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2142 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2143 SCTP_SOCKET_UNLOCK(so, 1);
2145 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2148 /* process the INIT-ACK info (my info) */
2149 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2150 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2151 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2152 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2153 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2154 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2155 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2156 asoc->str_reset_seq_in = asoc->init_seq_number;
2158 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2160 /* process the INIT info (peer's info) */
2162 retval = sctp_process_init(init_cp, stcb);
2166 atomic_add_int(&stcb->asoc.refcnt, 1);
2167 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2168 SCTP_TCB_UNLOCK(stcb);
2169 SCTP_SOCKET_LOCK(so, 1);
2170 SCTP_TCB_LOCK(stcb);
2172 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2173 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2174 SCTP_SOCKET_UNLOCK(so, 1);
2176 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2179 /* load all addresses */
2180 if (sctp_load_addresses_from_init(stcb, m,
2181 init_offset + sizeof(struct sctp_init_chunk), initack_offset,
2182 src, dst, init_src)) {
2183 atomic_add_int(&stcb->asoc.refcnt, 1);
2184 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2185 SCTP_TCB_UNLOCK(stcb);
2186 SCTP_SOCKET_LOCK(so, 1);
2187 SCTP_TCB_LOCK(stcb);
2189 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
2190 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2191 SCTP_SOCKET_UNLOCK(so, 1);
2193 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2197 * verify any preceding AUTH chunk that was skipped
2199 /* pull the local authentication parameters from the cookie/init-ack */
2200 sctp_auth_get_cookie_params(stcb, m,
2201 initack_offset + sizeof(struct sctp_init_ack_chunk),
2202 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2204 struct sctp_auth_chunk *auth;
2206 auth = (struct sctp_auth_chunk *)
2207 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2208 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2209 /* auth HMAC failed, dump the assoc and packet */
2210 SCTPDBG(SCTP_DEBUG_AUTH1,
2211 "COOKIE-ECHO: AUTH failed\n");
2212 atomic_add_int(&stcb->asoc.refcnt, 1);
2213 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2214 SCTP_TCB_UNLOCK(stcb);
2215 SCTP_SOCKET_LOCK(so, 1);
2216 SCTP_TCB_LOCK(stcb);
2218 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2219 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2220 SCTP_SOCKET_UNLOCK(so, 1);
2222 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2225 /* remaining chunks checked... good to go */
2226 stcb->asoc.authenticated = 1;
2229 /* update current state */
2230 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2231 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2232 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2233 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2234 stcb->sctp_ep, stcb, asoc->primary_destination);
2236 sctp_stop_all_cookie_timers(stcb);
2237 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2238 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2241 * if we're doing ASCONFs, check to see if we have any new local
2242 * addresses that need to get added to the peer (eg. addresses
2243 * changed while cookie echo in flight). This needs to be done
2244 * after we go to the OPEN state to do the correct asconf
2245 * processing. else, make sure we have the correct addresses in our
2249 /* warning, we re-use sin, sin6, sa_store here! */
2250 /* pull in local_address (our "from" address) */
2251 switch (cookie->laddr_type) {
2253 case SCTP_IPV4_ADDRESS:
2254 /* source addr is IPv4 */
2255 sin = (struct sockaddr_in *)initack_src;
2256 memset(sin, 0, sizeof(*sin));
2257 sin->sin_family = AF_INET;
2258 sin->sin_len = sizeof(struct sockaddr_in);
2259 sin->sin_addr.s_addr = cookie->laddress[0];
2263 case SCTP_IPV6_ADDRESS:
2264 /* source addr is IPv6 */
2265 sin6 = (struct sockaddr_in6 *)initack_src;
2266 memset(sin6, 0, sizeof(*sin6));
2267 sin6->sin6_family = AF_INET6;
2268 sin6->sin6_len = sizeof(struct sockaddr_in6);
2269 sin6->sin6_scope_id = cookie->scope_id;
2270 memcpy(&sin6->sin6_addr, cookie->laddress,
2271 sizeof(sin6->sin6_addr));
2275 atomic_add_int(&stcb->asoc.refcnt, 1);
2276 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2277 SCTP_TCB_UNLOCK(stcb);
2278 SCTP_SOCKET_LOCK(so, 1);
2279 SCTP_TCB_LOCK(stcb);
2281 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2282 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2283 SCTP_SOCKET_UNLOCK(so, 1);
2285 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2289 /* set up to notify upper layer */
2290 *notification = SCTP_NOTIFY_ASSOC_UP;
2291 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2292 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2293 (inp->sctp_socket->so_qlimit == 0)) {
2295 * This is an endpoint that called connect() how it got a
2296 * cookie that is NEW is a bit of a mystery. It must be that
2297 * the INIT was sent, but before it got there.. a complete
2298 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2299 * should have went to the other code.. not here.. oh well..
2300 * a bit of protection is worth having..
2302 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2303 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2304 atomic_add_int(&stcb->asoc.refcnt, 1);
2305 SCTP_TCB_UNLOCK(stcb);
2306 SCTP_SOCKET_LOCK(so, 1);
2307 SCTP_TCB_LOCK(stcb);
2308 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2309 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2310 SCTP_SOCKET_UNLOCK(so, 1);
2314 soisconnected(stcb->sctp_socket);
2315 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2316 SCTP_SOCKET_UNLOCK(so, 1);
2318 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2319 (inp->sctp_socket->so_qlimit)) {
2321 * We don't want to do anything with this one. Since it is
2322 * the listening guy. The timer will get started for
2323 * accepted connections in the caller.
2327 /* since we did not send a HB make sure we don't double things */
2328 if ((netp) && (*netp))
2329 (*netp)->hb_responded = 1;
2331 if (stcb->asoc.sctp_autoclose_ticks &&
2332 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2333 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2335 /* calculate the RTT */
2336 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2337 if ((netp) && (*netp)) {
2338 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2339 &cookie->time_entered, sctp_align_unsafe_makecopy,
2340 SCTP_RTT_FROM_NON_DATA);
2342 /* respond with a COOKIE-ACK */
2343 sctp_send_cookie_ack(stcb);
2346 * check the address lists for any ASCONFs that need to be sent
2347 * AFTER the cookie-ack is sent
2349 sctp_check_address_list(stcb, m,
2350 initack_offset + sizeof(struct sctp_init_ack_chunk),
2351 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2352 initack_src, cookie->local_scope, cookie->site_scope,
2353 cookie->ipv4_scope, cookie->loopback_scope);
2360 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2361 * we NEED to make sure we are not already using the vtag. If so we
2362 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2363 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2364 SCTP_BASE_INFO(hashasocmark))];
2365 LIST_FOREACH(stcb, head, sctp_asocs) {
2366 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2367 -- SEND ABORT - TRY AGAIN --
2373 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2374 * existing (non-NULL) TCB
2376 static struct mbuf *
2377 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2378 struct sockaddr *src, struct sockaddr *dst,
2379 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2380 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2381 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2382 struct sctp_tcb **locked_tcb,
2383 uint8_t use_mflowid, uint32_t mflowid,
2384 uint32_t vrf_id, uint16_t port)
2386 struct sctp_state_cookie *cookie;
2387 struct sctp_tcb *l_stcb = *stcb;
2388 struct sctp_inpcb *l_inp;
2389 struct sockaddr *to;
2390 struct sctp_pcb *ep;
2392 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2394 uint8_t cookie_ok = 0;
2395 unsigned int sig_offset, cookie_offset;
2396 unsigned int cookie_len;
2398 struct timeval time_expires;
2399 int notification = 0;
2400 struct sctp_nets *netl;
2401 int had_a_existing_tcb = 0;
2402 int send_int_conf = 0;
2405 struct sockaddr_in sin;
2409 struct sockaddr_in6 sin6;
2413 SCTPDBG(SCTP_DEBUG_INPUT2,
2414 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2416 if (inp_p == NULL) {
2419 cookie = &cp->cookie;
2420 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2421 cookie_len = ntohs(cp->ch.chunk_length);
2423 if ((cookie->peerport != sh->src_port) &&
2424 (cookie->myport != sh->dest_port) &&
2425 (cookie->my_vtag != sh->v_tag)) {
2427 * invalid ports or bad tag. Note that we always leave the
2428 * v_tag in the header in network order and when we stored
2429 * it in the my_vtag slot we also left it in network order.
2430 * This maintains the match even though it may be in the
2431 * opposite byte order of the machine :->
2435 if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2436 sizeof(struct sctp_init_chunk) +
2437 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2438 /* cookie too small */
2442 * split off the signature into its own mbuf (since it should not be
2443 * calculated in the sctp_hmac_m() call).
2445 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2446 m_sig = m_split(m, sig_offset, M_DONTWAIT);
2447 if (m_sig == NULL) {
2448 /* out of memory or ?? */
2451 #ifdef SCTP_MBUF_LOGGING
2452 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2455 for (mat = m_sig; mat; mat = SCTP_BUF_NEXT(mat)) {
2456 if (SCTP_BUF_IS_EXTENDED(mat)) {
2457 sctp_log_mb(mat, SCTP_MBUF_SPLIT);
2464 * compute the signature/digest for the cookie
2466 ep = &(*inp_p)->sctp_ep;
2469 SCTP_TCB_UNLOCK(l_stcb);
2471 SCTP_INP_RLOCK(l_inp);
2473 SCTP_TCB_LOCK(l_stcb);
2475 /* which cookie is it? */
2476 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2477 (ep->current_secret_number != ep->last_secret_number)) {
2478 /* it's the old cookie */
2479 (void)sctp_hmac_m(SCTP_HMAC,
2480 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2481 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2483 /* it's the current cookie */
2484 (void)sctp_hmac_m(SCTP_HMAC,
2485 (uint8_t *) ep->secret_key[(int)ep->current_secret_number],
2486 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2488 /* get the signature */
2489 SCTP_INP_RUNLOCK(l_inp);
2490 sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig);
2492 /* couldn't find signature */
2493 sctp_m_freem(m_sig);
2496 /* compare the received digest with the computed digest */
2497 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2498 /* try the old cookie? */
2499 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2500 (ep->current_secret_number != ep->last_secret_number)) {
2501 /* compute digest with old */
2502 (void)sctp_hmac_m(SCTP_HMAC,
2503 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2504 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2506 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2514 * Now before we continue we must reconstruct our mbuf so that
2515 * normal processing of any other chunks will work.
2521 while (SCTP_BUF_NEXT(m_at) != NULL) {
2522 m_at = SCTP_BUF_NEXT(m_at);
2524 SCTP_BUF_NEXT(m_at) = m_sig;
2527 if (cookie_ok == 0) {
2528 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2529 SCTPDBG(SCTP_DEBUG_INPUT2,
2530 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2531 (uint32_t) offset, cookie_offset, sig_offset);
2535 * check the cookie timestamps to be sure it's not stale
2537 (void)SCTP_GETTIME_TIMEVAL(&now);
2538 /* Expire time is in Ticks, so we convert to seconds */
2539 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2540 time_expires.tv_usec = cookie->time_entered.tv_usec;
2542 * TODO sctp_constants.h needs alternative time macros when _KERNEL
2545 if (timevalcmp(&now, &time_expires, >)) {
2546 /* cookie is stale! */
2547 struct mbuf *op_err;
2548 struct sctp_stale_cookie_msg *scm;
2551 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg),
2552 0, M_DONTWAIT, 1, MT_DATA);
2553 if (op_err == NULL) {
2558 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg);
2559 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
2560 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
2561 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
2562 (sizeof(uint32_t))));
2563 /* seconds to usec */
2564 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2567 tim = now.tv_usec - cookie->time_entered.tv_usec;
2568 scm->time_usec = htonl(tim);
2569 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2570 use_mflowid, mflowid,
2575 * Now we must see with the lookup address if we have an existing
2576 * asoc. This will only happen if we were in the COOKIE-WAIT state
2577 * and a INIT collided with us and somewhere the peer sent the
2578 * cookie on another address besides the single address our assoc
2579 * had for him. In this case we will have one of the tie-tags set at
2580 * least AND the address field in the cookie can be used to look it
2584 switch (cookie->addr_type) {
2586 case SCTP_IPV6_ADDRESS:
2587 memset(&sin6, 0, sizeof(sin6));
2588 sin6.sin6_family = AF_INET6;
2589 sin6.sin6_len = sizeof(sin6);
2590 sin6.sin6_port = sh->src_port;
2591 sin6.sin6_scope_id = cookie->scope_id;
2592 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2593 sizeof(sin6.sin6_addr.s6_addr));
2594 to = (struct sockaddr *)&sin6;
2598 case SCTP_IPV4_ADDRESS:
2599 memset(&sin, 0, sizeof(sin));
2600 sin.sin_family = AF_INET;
2601 sin.sin_len = sizeof(sin);
2602 sin.sin_port = sh->src_port;
2603 sin.sin_addr.s_addr = cookie->address[0];
2604 to = (struct sockaddr *)&sin;
2608 /* This should not happen */
2611 if ((*stcb == NULL) && to) {
2612 /* Yep, lets check */
2613 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2614 if (*stcb == NULL) {
2616 * We should have only got back the same inp. If we
2617 * got back a different ep we have a problem. The
2618 * original findep got back l_inp and now
2620 if (l_inp != *inp_p) {
2621 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2624 if (*locked_tcb == NULL) {
2626 * In this case we found the assoc only
2627 * after we locked the create lock. This
2628 * means we are in a colliding case and we
2629 * must make sure that we unlock the tcb if
2630 * its one of the cases where we throw away
2631 * the incoming packets.
2633 *locked_tcb = *stcb;
2636 * We must also increment the inp ref count
2637 * since the ref_count flags was set when we
2638 * did not find the TCB, now we found it
2639 * which reduces the refcount.. we must
2640 * raise it back out to balance it all :-)
2642 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2643 if ((*stcb)->sctp_ep != l_inp) {
2644 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2645 (void *)(*stcb)->sctp_ep, (void *)l_inp);
2653 cookie_len -= SCTP_SIGNATURE_SIZE;
2654 if (*stcb == NULL) {
2655 /* this is the "normal" case... get a new TCB */
2656 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2657 cookie, cookie_len, *inp_p,
2658 netp, to, ¬ification,
2659 auth_skipped, auth_offset, auth_len,
2660 use_mflowid, mflowid,
2663 /* this is abnormal... cookie-echo on existing TCB */
2664 had_a_existing_tcb = 1;
2665 *stcb = sctp_process_cookie_existing(m, iphlen, offset,
2667 cookie, cookie_len, *inp_p, *stcb, netp, to,
2668 ¬ification, auth_skipped, auth_offset, auth_len,
2669 use_mflowid, mflowid,
2673 if (*stcb == NULL) {
2674 /* still no TCB... must be bad cookie-echo */
2677 if ((*netp != NULL) && (use_mflowid != 0)) {
2678 (*netp)->flowid = mflowid;
2680 (*netp)->flowidset = 1;
2684 * Ok, we built an association so confirm the address we sent the
2687 netl = sctp_findnet(*stcb, to);
2689 * This code should in theory NOT run but
2692 /* TSNH! Huh, why do I need to add this address here? */
2693 if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2696 netl = sctp_findnet(*stcb, to);
2699 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2700 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2701 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2706 sctp_start_net_timers(*stcb);
2707 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2708 if (!had_a_existing_tcb ||
2709 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2711 * If we have a NEW cookie or the connect never
2712 * reached the connected state during collision we
2713 * must do the TCP accept thing.
2715 struct socket *so, *oso;
2716 struct sctp_inpcb *inp;
2718 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2720 * For a restart we will keep the same
2721 * socket, no need to do anything. I THINK!!
2723 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2724 if (send_int_conf) {
2725 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2726 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2730 oso = (*inp_p)->sctp_socket;
2731 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2732 SCTP_TCB_UNLOCK((*stcb));
2733 CURVNET_SET(oso->so_vnet);
2734 so = sonewconn(oso, 0
2737 SCTP_TCB_LOCK((*stcb));
2738 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2741 struct mbuf *op_err;
2743 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2744 struct socket *pcb_so;
2747 /* Too many sockets */
2748 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2749 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2750 sctp_abort_association(*inp_p, NULL, m, iphlen,
2751 src, dst, sh, op_err,
2752 use_mflowid, mflowid,
2754 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2755 pcb_so = SCTP_INP_SO(*inp_p);
2756 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2757 SCTP_TCB_UNLOCK((*stcb));
2758 SCTP_SOCKET_LOCK(pcb_so, 1);
2759 SCTP_TCB_LOCK((*stcb));
2760 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2762 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2763 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2764 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2768 inp = (struct sctp_inpcb *)so->so_pcb;
2769 SCTP_INP_INCR_REF(inp);
2771 * We add the unbound flag here so that if we get an
2772 * soabort() before we get the move_pcb done, we
2773 * will properly cleanup.
2775 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2776 SCTP_PCB_FLAGS_CONNECTED |
2777 SCTP_PCB_FLAGS_IN_TCPPOOL |
2778 SCTP_PCB_FLAGS_UNBOUND |
2779 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2780 SCTP_PCB_FLAGS_DONT_WAKE);
2781 inp->sctp_features = (*inp_p)->sctp_features;
2782 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2783 inp->sctp_socket = so;
2784 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2785 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2786 inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable;
2787 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2788 inp->sctp_context = (*inp_p)->sctp_context;
2789 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2790 inp->inp_starting_point_for_iterator = NULL;
2792 * copy in the authentication parameters from the
2795 if (inp->sctp_ep.local_hmacs)
2796 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2797 inp->sctp_ep.local_hmacs =
2798 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2799 if (inp->sctp_ep.local_auth_chunks)
2800 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2801 inp->sctp_ep.local_auth_chunks =
2802 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2805 * Now we must move it from one hash table to
2806 * another and get the tcb in the right place.
2810 * This is where the one-2-one socket is put into
2811 * the accept state waiting for the accept!
2814 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2816 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2818 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2819 SCTP_TCB_UNLOCK((*stcb));
2821 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2823 SCTP_TCB_LOCK((*stcb));
2824 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2828 * now we must check to see if we were aborted while
2829 * the move was going on and the lock/unlock
2832 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2834 * yep it was, we leave the assoc attached
2835 * to the socket since the sctp_inpcb_free()
2836 * call will send an abort for us.
2838 SCTP_INP_DECR_REF(inp);
2841 SCTP_INP_DECR_REF(inp);
2842 /* Switch over to the new guy */
2844 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2845 if (send_int_conf) {
2846 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2847 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2850 * Pull it from the incomplete queue and wake the
2853 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2854 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2855 SCTP_TCB_UNLOCK((*stcb));
2856 SCTP_SOCKET_LOCK(so, 1);
2859 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2860 SCTP_TCB_LOCK((*stcb));
2861 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2862 SCTP_SOCKET_UNLOCK(so, 1);
2868 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2870 if (send_int_conf) {
2871 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2872 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2878 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2879 struct sctp_tcb *stcb, struct sctp_nets *net)
2881 /* cp must not be used, others call this without a c-ack :-) */
2882 struct sctp_association *asoc;
2884 SCTPDBG(SCTP_DEBUG_INPUT2,
2885 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2891 sctp_stop_all_cookie_timers(stcb);
2892 /* process according to association state */
2893 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2894 /* state change only needed when I am in right state */
2895 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2896 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2897 sctp_start_net_timers(stcb);
2898 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2899 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2900 stcb->sctp_ep, stcb, asoc->primary_destination);
2904 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2905 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2906 if (asoc->overall_error_count == 0) {
2907 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2908 &asoc->time_entered, sctp_align_safe_nocopy,
2909 SCTP_RTT_FROM_NON_DATA);
2911 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2912 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2913 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2914 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2915 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2919 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2920 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2921 so = SCTP_INP_SO(stcb->sctp_ep);
2922 atomic_add_int(&stcb->asoc.refcnt, 1);
2923 SCTP_TCB_UNLOCK(stcb);
2924 SCTP_SOCKET_LOCK(so, 1);
2925 SCTP_TCB_LOCK(stcb);
2926 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2928 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2929 soisconnected(stcb->sctp_socket);
2931 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2932 SCTP_SOCKET_UNLOCK(so, 1);
2936 * since we did not send a HB make sure we don't double
2939 net->hb_responded = 1;
2941 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2943 * We don't need to do the asconf thing, nor hb or
2944 * autoclose if the socket is closed.
2948 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2952 if (stcb->asoc.sctp_autoclose_ticks &&
2953 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2954 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2955 stcb->sctp_ep, stcb, NULL);
2958 * send ASCONF if parameters are pending and ASCONFs are
2959 * allowed (eg. addresses changed when init/cookie echo were
2962 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2963 (stcb->asoc.peer_supports_asconf) &&
2964 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2965 #ifdef SCTP_TIMER_BASED_ASCONF
2966 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2967 stcb->sctp_ep, stcb,
2968 stcb->asoc.primary_destination);
2970 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2971 SCTP_ADDR_NOT_LOCKED);
2976 /* Toss the cookie if I can */
2977 sctp_toss_old_cookies(stcb, asoc);
2978 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
2979 /* Restart the timer if we have pending data */
2980 struct sctp_tmit_chunk *chk;
2982 chk = TAILQ_FIRST(&asoc->sent_queue);
2983 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
2988 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2989 struct sctp_tcb *stcb)
2991 struct sctp_nets *net;
2992 struct sctp_tmit_chunk *lchk;
2993 struct sctp_ecne_chunk bkup;
2994 uint8_t override_bit;
2995 uint32_t tsn, window_data_tsn;
2997 unsigned int pkt_cnt;
2999 len = ntohs(cp->ch.chunk_length);
3000 if ((len != sizeof(struct sctp_ecne_chunk)) &&
3001 (len != sizeof(struct old_sctp_ecne_chunk))) {
3004 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3005 /* Its the old format */
3006 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3007 bkup.num_pkts_since_cwr = htonl(1);
3010 SCTP_STAT_INCR(sctps_recvecne);
3011 tsn = ntohl(cp->tsn);
3012 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3013 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3015 window_data_tsn = stcb->asoc.sending_seq - 1;
3017 window_data_tsn = lchk->rec.data.TSN_seq;
3020 /* Find where it was sent to if possible. */
3022 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3023 if (lchk->rec.data.TSN_seq == tsn) {
3025 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3028 if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) {
3034 * What to do. A previous send of a CWR was possibly lost.
3035 * See how old it is, we may have it marked on the actual
3038 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3039 if (tsn == net->last_cwr_tsn) {
3040 /* Found him, send it off */
3046 * If we reach here, we need to send a special CWR
3047 * that says hey, we did this a long time ago and
3048 * you lost the response.
3050 net = TAILQ_FIRST(&stcb->asoc.nets);
3055 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3062 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3063 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3065 * JRS - Use the congestion control given in the pluggable
3068 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3070 * We reduce once every RTT. So we will only lower cwnd at
3071 * the next sending seq i.e. the window_data_tsn
3073 net->cwr_window_tsn = window_data_tsn;
3074 net->ecn_ce_pkt_cnt += pkt_cnt;
3075 net->lost_cnt = pkt_cnt;
3076 net->last_cwr_tsn = tsn;
3078 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3079 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3080 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3082 * Another loss in the same window update how many
3083 * marks/packets lost we have had.
3087 if (pkt_cnt > net->lost_cnt) {
3088 /* Should be the case */
3089 cnt = (pkt_cnt - net->lost_cnt);
3090 net->ecn_ce_pkt_cnt += cnt;
3092 net->lost_cnt = pkt_cnt;
3093 net->last_cwr_tsn = tsn;
3095 * Most CC functions will ignore this call, since we
3096 * are in-window yet of the initial CE the peer saw.
3098 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3102 * We always send a CWR this way if our previous one was lost our
3103 * peer will get an update, or if it is not time again to reduce we
3104 * still get the cwr to the peer. Note we set the override when we
3105 * could not find the TSN on the chunk or the destination network.
3107 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3111 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3114 * Here we get a CWR from the peer. We must look in the outqueue and
3115 * make sure that we have a covered ECNE in the control chunk part.
3118 struct sctp_tmit_chunk *chk;
3119 struct sctp_ecne_chunk *ecne;
3123 cwr_tsn = ntohl(cp->tsn);
3125 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3126 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
3127 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3130 if ((override == 0) && (chk->whoTo != net)) {
3131 /* Must be from the right src unless override is set */
3134 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3135 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3136 /* this covers this ECNE, we can remove it */
3137 stcb->asoc.ecn_echo_cnt_onq--;
3138 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3141 sctp_m_freem(chk->data);
3144 stcb->asoc.ctrl_queue_cnt--;
3145 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3146 if (override == 0) {
3154 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3155 struct sctp_tcb *stcb, struct sctp_nets *net)
3157 struct sctp_association *asoc;
3159 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3164 SCTPDBG(SCTP_DEBUG_INPUT2,
3165 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3170 /* process according to association state */
3171 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3172 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3173 SCTPDBG(SCTP_DEBUG_INPUT2,
3174 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3175 SCTP_TCB_UNLOCK(stcb);
3178 /* notify upper layer protocol */
3179 if (stcb->sctp_socket) {
3180 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3183 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3184 !TAILQ_EMPTY(&asoc->sent_queue) ||
3185 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
3186 panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3189 /* stop the timer */
3190 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
3191 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3193 SCTPDBG(SCTP_DEBUG_INPUT2,
3194 "sctp_handle_shutdown_complete: calls free-asoc\n");
3195 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3196 so = SCTP_INP_SO(stcb->sctp_ep);
3197 atomic_add_int(&stcb->asoc.refcnt, 1);
3198 SCTP_TCB_UNLOCK(stcb);
3199 SCTP_SOCKET_LOCK(so, 1);
3200 SCTP_TCB_LOCK(stcb);
3201 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3203 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
3204 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3205 SCTP_SOCKET_UNLOCK(so, 1);
3211 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3212 struct sctp_nets *net, uint8_t flg)
3214 switch (desc->chunk_type) {
3216 /* find the tsn to resend (possibly */
3219 struct sctp_tmit_chunk *tp1;
3221 tsn = ntohl(desc->tsn_ifany);
3222 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3223 if (tp1->rec.data.TSN_seq == tsn) {
3227 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) {
3235 * Do it the other way , aka without paying
3236 * attention to queue seq order.
3238 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3239 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3240 if (tp1->rec.data.TSN_seq == tsn) {
3247 SCTP_STAT_INCR(sctps_pdrptsnnf);
3249 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3252 if (((flg & SCTP_BADCRC) == 0) &&
3253 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3256 if ((stcb->asoc.peers_rwnd == 0) &&
3257 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3258 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3261 if (stcb->asoc.peers_rwnd == 0 &&
3262 (flg & SCTP_FROM_MIDDLE_BOX)) {
3263 SCTP_STAT_INCR(sctps_pdrpdizrw);
3266 ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+
3267 sizeof(struct sctp_data_chunk));
3271 for (iii = 0; iii < sizeof(desc->data_bytes);
3273 if (ddp[iii] != desc->data_bytes[iii]) {
3274 SCTP_STAT_INCR(sctps_pdrpbadd);
3282 * this guy had a RTO calculation
3283 * pending on it, cancel it
3285 if (tp1->whoTo->rto_needed == 0) {
3286 tp1->whoTo->rto_needed = 1;
3290 SCTP_STAT_INCR(sctps_pdrpmark);
3291 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3292 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3294 * mark it as if we were doing a FR, since
3295 * we will be getting gap ack reports behind
3296 * the info from the router.
3298 tp1->rec.data.doing_fast_retransmit = 1;
3300 * mark the tsn with what sequences can
3303 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3304 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3306 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
3309 /* restart the timer */
3310 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3311 stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3312 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3315 /* fix counts and things */
3316 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3317 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3318 tp1->whoTo->flight_size,
3321 tp1->rec.data.TSN_seq);
3323 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3324 sctp_flight_size_decrease(tp1);
3325 sctp_total_flight_decrease(stcb, tp1);
3327 tp1->sent = SCTP_DATAGRAM_RESEND;
3333 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3334 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3337 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3339 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3342 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3343 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3344 audit, stcb->asoc.sent_queue_retran_cnt);
3345 #ifndef SCTP_AUDITING_ENABLED
3346 stcb->asoc.sent_queue_retran_cnt = audit;
3354 struct sctp_tmit_chunk *asconf;
3356 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3358 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3363 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3364 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3365 asconf->sent = SCTP_DATAGRAM_RESEND;
3366 asconf->snd_count--;
3370 case SCTP_INITIATION:
3371 /* resend the INIT */
3372 stcb->asoc.dropped_special_cnt++;
3373 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3375 * If we can get it in, in a few attempts we do
3376 * this, otherwise we let the timer fire.
3378 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3379 stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3380 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3383 case SCTP_SELECTIVE_ACK:
3384 case SCTP_NR_SELECTIVE_ACK:
3385 /* resend the sack */
3386 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3388 case SCTP_HEARTBEAT_REQUEST:
3389 /* resend a demand HB */
3390 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3392 * Only retransmit if we KNOW we wont destroy the
3395 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3399 sctp_send_shutdown(stcb, net);
3401 case SCTP_SHUTDOWN_ACK:
3402 sctp_send_shutdown_ack(stcb, net);
3404 case SCTP_COOKIE_ECHO:
3406 struct sctp_tmit_chunk *cookie;
3409 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3411 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3416 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3417 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3418 cookie->sent = SCTP_DATAGRAM_RESEND;
3419 sctp_stop_all_cookie_timers(stcb);
3423 case SCTP_COOKIE_ACK:
3424 sctp_send_cookie_ack(stcb);
3426 case SCTP_ASCONF_ACK:
3427 /* resend last asconf ack */
3428 sctp_send_asconf_ack(stcb);
3430 case SCTP_FORWARD_CUM_TSN:
3431 send_forward_tsn(stcb, &stcb->asoc);
3433 /* can't do anything with these */
3434 case SCTP_PACKET_DROPPED:
3435 case SCTP_INITIATION_ACK: /* this should not happen */
3436 case SCTP_HEARTBEAT_ACK:
3437 case SCTP_ABORT_ASSOCIATION:
3438 case SCTP_OPERATION_ERROR:
3439 case SCTP_SHUTDOWN_COMPLETE:
3449 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t * list)
3455 * We set things to 0xffff since this is the last delivered sequence
3456 * and we will be sending in 0 after the reset.
3459 if (number_entries) {
3460 for (i = 0; i < number_entries; i++) {
3461 temp = ntohs(list[i]);
3462 if (temp >= stcb->asoc.streamincnt) {
3465 stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff;
3469 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3470 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3473 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3477 sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t * list)
3482 if (number_entries > 0) {
3483 for (i = 0; i < number_entries; i++) {
3484 temp = ntohs(list[i]);
3485 if (temp >= stcb->asoc.streamoutcnt) {
3486 /* no such stream */
3489 stcb->asoc.strmout[temp].next_sequence_send = 0;
3492 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3493 stcb->asoc.strmout[i].next_sequence_send = 0;
3496 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3500 struct sctp_stream_reset_out_request *
3501 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3503 struct sctp_association *asoc;
3504 struct sctp_chunkhdr *ch;
3505 struct sctp_stream_reset_out_request *r;
3506 struct sctp_tmit_chunk *chk;
3510 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3511 asoc->stream_reset_outstanding = 0;
3514 if (stcb->asoc.str_reset == NULL) {
3515 asoc->stream_reset_outstanding = 0;
3518 chk = stcb->asoc.str_reset;
3519 if (chk->data == NULL) {
3523 /* he wants a copy of the chk pointer */
3526 clen = chk->send_size;
3527 ch = mtod(chk->data, struct sctp_chunkhdr *);
3528 r = (struct sctp_stream_reset_out_request *)(ch + 1);
3529 if (ntohl(r->request_seq) == seq) {
3533 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3534 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3535 /* move to the next one, there can only be a max of two */
3536 r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len);
3537 if (ntohl(r->request_seq) == seq) {
3541 /* that seq is not here */
3546 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3548 struct sctp_association *asoc;
3549 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3551 if (stcb->asoc.str_reset == NULL) {
3556 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3557 TAILQ_REMOVE(&asoc->control_send_queue,
3561 sctp_m_freem(chk->data);
3564 asoc->ctrl_queue_cnt--;
3565 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3566 /* sa_ignore NO_NULL_CHK */
3567 stcb->asoc.str_reset = NULL;
3572 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3573 uint32_t seq, uint32_t action,
3574 struct sctp_stream_reset_response *respin)
3578 struct sctp_association *asoc = &stcb->asoc;
3579 struct sctp_tmit_chunk *chk;
3580 struct sctp_stream_reset_out_request *srparam;
3581 uint32_t number_entries;
3583 if (asoc->stream_reset_outstanding == 0) {
3587 if (seq == stcb->asoc.str_reset_seq_out) {
3588 srparam = sctp_find_stream_reset(stcb, seq, &chk);
3590 stcb->asoc.str_reset_seq_out++;
3591 type = ntohs(srparam->ph.param_type);
3592 lparm_len = ntohs(srparam->ph.param_length);
3593 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3594 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3595 asoc->stream_reset_out_is_outstanding = 0;
3596 if (asoc->stream_reset_outstanding)
3597 asoc->stream_reset_outstanding--;
3598 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3600 sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams);
3601 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3602 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3604 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3606 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3607 /* Answered my request */
3608 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3609 if (asoc->stream_reset_outstanding)
3610 asoc->stream_reset_outstanding--;
3611 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3612 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3613 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3614 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3615 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3616 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3618 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3619 /* Ok we now may have more streams */
3622 num_stream = stcb->asoc.strm_pending_add_size;
3623 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3625 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3627 stcb->asoc.strm_pending_add_size = 0;
3628 if (asoc->stream_reset_outstanding)
3629 asoc->stream_reset_outstanding--;
3630 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3631 /* Put the new streams into effect */
3632 stcb->asoc.streamoutcnt += num_stream;
3633 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3634 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3635 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3636 SCTP_STREAM_CHANGE_DENIED);
3638 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3639 SCTP_STREAM_CHANGE_FAILED);
3641 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3642 if (asoc->stream_reset_outstanding)
3643 asoc->stream_reset_outstanding--;
3644 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3645 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3646 SCTP_STREAM_CHANGE_DENIED);
3647 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3648 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3649 SCTP_STREAM_CHANGE_FAILED);
3651 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3653 * a) Adopt the new in tsn.
3655 * c) Adopt the new out-tsn
3657 struct sctp_stream_reset_response_tsn *resp;
3658 struct sctp_forward_tsn_chunk fwdtsn;
3661 if (respin == NULL) {
3665 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3666 resp = (struct sctp_stream_reset_response_tsn *)respin;
3667 asoc->stream_reset_outstanding--;
3668 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3669 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3670 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3671 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3675 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3676 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3677 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3679 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3680 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3681 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3683 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3684 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3686 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3687 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3689 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3690 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3691 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3692 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3693 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3694 SCTP_ASSOC_RESET_DENIED);
3696 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3697 SCTP_ASSOC_RESET_FAILED);
3700 /* get rid of the request and get the request flags */
3701 if (asoc->stream_reset_outstanding == 0) {
3702 sctp_clean_up_stream_reset(stcb);
3710 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3711 struct sctp_tmit_chunk *chk,
3712 struct sctp_stream_reset_in_request *req, int trunc)
3720 * peer wants me to send a str-reset to him for my outgoing seq's if
3723 struct sctp_association *asoc = &stcb->asoc;
3725 seq = ntohl(req->request_seq);
3726 if (asoc->str_reset_seq_in == seq) {
3727 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3728 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3729 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3731 /* Can't do it, since they exceeded our buffer size */
3732 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3733 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3734 len = ntohs(req->ph.param_length);
3735 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3736 for (i = 0; i < number_entries; i++) {
3737 temp = ntohs(req->list_of_streams[i]);
3738 req->list_of_streams[i] = temp;
3740 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3741 sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams,
3742 asoc->str_reset_seq_out,
3743 seq, (asoc->sending_seq - 1));
3744 asoc->stream_reset_out_is_outstanding = 1;
3745 asoc->str_reset = chk;
3746 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
3747 stcb->asoc.stream_reset_outstanding++;
3749 /* Can't do it, since we have sent one out */
3750 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3752 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3753 asoc->str_reset_seq_in++;
3754 } else if (asoc->str_reset_seq_in - 1 == seq) {
3755 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3756 } else if (asoc->str_reset_seq_in - 2 == seq) {
3757 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3759 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3764 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3765 struct sctp_tmit_chunk *chk,
3766 struct sctp_stream_reset_tsn_request *req)
3768 /* reset all in and out and update the tsn */
3770 * A) reset my str-seq's on in and out. B) Select a receive next,
3771 * and set cum-ack to it. Also process this selected number as a
3772 * fwd-tsn as well. C) set in the response my next sending seq.
3774 struct sctp_forward_tsn_chunk fwdtsn;
3775 struct sctp_association *asoc = &stcb->asoc;
3779 seq = ntohl(req->request_seq);
3780 if (asoc->str_reset_seq_in == seq) {
3781 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3782 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3783 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3785 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3786 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3787 fwdtsn.ch.chunk_flags = 0;
3788 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3789 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3793 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3794 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3795 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3797 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3798 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3799 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3800 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3801 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3802 atomic_add_int(&asoc->sending_seq, 1);
3803 /* save off historical data for retrans */
3804 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3805 asoc->last_sending_seq[0] = asoc->sending_seq;
3806 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3807 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3808 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3809 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3810 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3811 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3813 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3814 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3815 asoc->str_reset_seq_in++;
3816 } else if (asoc->str_reset_seq_in - 1 == seq) {
3817 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3818 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3819 } else if (asoc->str_reset_seq_in - 2 == seq) {
3820 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3821 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3823 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3829 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3830 struct sctp_tmit_chunk *chk,
3831 struct sctp_stream_reset_out_request *req, int trunc)
3834 int number_entries, len;
3835 struct sctp_association *asoc = &stcb->asoc;
3837 seq = ntohl(req->request_seq);
3839 /* now if its not a duplicate we process it */
3840 if (asoc->str_reset_seq_in == seq) {
3841 len = ntohs(req->ph.param_length);
3842 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3844 * the sender is resetting, handle the list issue.. we must
3845 * a) verify if we can do the reset, if so no problem b) If
3846 * we can't do the reset we must copy the request. c) queue
3847 * it, and setup the data in processor to trigger it off
3848 * when needed and dequeue all the queued data.
3850 tsn = ntohl(req->send_reset_at_tsn);
3852 /* move the reset action back one */
3853 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3854 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3855 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3857 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3858 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3859 /* we can do it now */
3860 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3861 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3864 * we must queue it up and thus wait for the TSN's
3865 * to arrive that are at or before tsn
3867 struct sctp_stream_reset_list *liste;
3870 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3871 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3872 siz, SCTP_M_STRESET);
3873 if (liste == NULL) {
3874 /* gak out of memory */
3875 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3876 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3880 liste->number_entries = number_entries;
3881 memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
3882 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3883 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3885 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3886 asoc->str_reset_seq_in++;
3887 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3889 * one seq back, just echo back last action since my
3890 * response was lost.
3892 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3893 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3895 * two seq back, just echo back last action since my
3896 * response was lost.
3898 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3900 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3905 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3906 struct sctp_stream_reset_add_strm *str_add)
3909 * Peer is requesting to add more streams. If its within our
3910 * max-streams we will allow it.
3912 uint32_t num_stream, i;
3914 struct sctp_association *asoc = &stcb->asoc;
3915 struct sctp_queued_to_read *ctl, *nctl;
3917 /* Get the number. */
3918 seq = ntohl(str_add->request_seq);
3919 num_stream = ntohs(str_add->number_of_streams);
3920 /* Now what would be the new total? */
3921 if (asoc->str_reset_seq_in == seq) {
3922 num_stream += stcb->asoc.streamincnt;
3923 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3924 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3925 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3926 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
3927 (num_stream > 0xffff)) {
3928 /* We must reject it they ask for to many */
3930 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3932 /* Ok, we can do that :-) */
3933 struct sctp_stream_in *oldstrm;
3935 /* save off the old */
3936 oldstrm = stcb->asoc.strmin;
3937 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
3938 (num_stream * sizeof(struct sctp_stream_in)),
3940 if (stcb->asoc.strmin == NULL) {
3941 stcb->asoc.strmin = oldstrm;
3944 /* copy off the old data */
3945 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3946 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3947 stcb->asoc.strmin[i].stream_no = i;
3948 stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered;
3949 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
3950 /* now anything on those queues? */
3951 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) {
3952 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next);
3953 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next);
3956 /* Init the new streams */
3957 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
3958 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3959 stcb->asoc.strmin[i].stream_no = i;
3960 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3961 stcb->asoc.strmin[i].delivery_started = 0;
3963 SCTP_FREE(oldstrm, SCTP_M_STRMI);
3964 /* update the size */
3965 stcb->asoc.streamincnt = num_stream;
3966 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3967 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3969 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3970 asoc->str_reset_seq_in++;
3971 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3973 * one seq back, just echo back last action since my
3974 * response was lost.
3976 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3977 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3979 * two seq back, just echo back last action since my
3980 * response was lost.
3982 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3984 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3990 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3991 struct sctp_stream_reset_add_strm *str_add)
3994 * Peer is requesting to add more streams. If its within our
3995 * max-streams we will allow it.
3997 uint16_t num_stream;
3999 struct sctp_association *asoc = &stcb->asoc;
4001 /* Get the number. */
4002 seq = ntohl(str_add->request_seq);
4003 num_stream = ntohs(str_add->number_of_streams);
4004 /* Now what would be the new total? */
4005 if (asoc->str_reset_seq_in == seq) {
4006 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4007 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4008 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4009 } else if (stcb->asoc.stream_reset_outstanding) {
4010 /* We must reject it we have something pending */
4011 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4013 /* Ok, we can do that :-) */
4016 mychk = stcb->asoc.streamoutcnt;
4017 mychk += num_stream;
4018 if (mychk < 0x10000) {
4019 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4020 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 0, 1, num_stream, 0, 1)) {
4021 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4024 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4027 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4028 asoc->str_reset_seq_in++;
4029 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4031 * one seq back, just echo back last action since my
4032 * response was lost.
4034 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4035 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4037 * two seq back, just echo back last action since my
4038 * response was lost.
4040 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4042 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4047 __attribute__((noinline))
4050 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4051 struct sctp_chunkhdr *ch_req)
4053 int chk_length, param_len, ptype;
4054 struct sctp_paramhdr pstore;
4055 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4059 struct sctp_tmit_chunk *chk;
4060 struct sctp_chunkhdr *ch;
4061 struct sctp_paramhdr *ph;
4065 /* now it may be a reset or a reset-response */
4066 chk_length = ntohs(ch_req->chunk_length);
4068 /* setup for adding the response */
4069 sctp_alloc_a_chunk(stcb, chk);
4073 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4074 chk->rec.chunk_id.can_take_data = 0;
4075 chk->asoc = &stcb->asoc;
4076 chk->no_fr_allowed = 0;
4077 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4078 chk->book_size_scale = 0;
4079 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4080 if (chk->data == NULL) {
4083 sctp_m_freem(chk->data);
4086 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4089 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4091 /* setup chunk parameters */
4092 chk->sent = SCTP_DATAGRAM_UNSENT;
4096 ch = mtod(chk->data, struct sctp_chunkhdr *);
4097 ch->chunk_type = SCTP_STREAM_RESET;
4098 ch->chunk_flags = 0;
4099 ch->chunk_length = htons(chk->send_size);
4100 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4101 offset += sizeof(struct sctp_chunkhdr);
4102 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) {
4103 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore);
4106 param_len = ntohs(ph->param_length);
4107 if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) {
4111 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)),
4112 (uint8_t *) & cstore);
4113 ptype = ntohs(ph->param_type);
4115 if (param_len > (int)sizeof(cstore)) {
4120 if (num_param > SCTP_MAX_RESET_PARAMS) {
4121 /* hit the max of parameters already sorry.. */
4124 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4125 struct sctp_stream_reset_out_request *req_out;
4127 req_out = (struct sctp_stream_reset_out_request *)ph;
4129 if (stcb->asoc.stream_reset_outstanding) {
4130 seq = ntohl(req_out->response_seq);
4131 if (seq == stcb->asoc.str_reset_seq_out) {
4133 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4136 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4137 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4138 struct sctp_stream_reset_add_strm *str_add;
4140 str_add = (struct sctp_stream_reset_add_strm *)ph;
4142 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4143 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4144 struct sctp_stream_reset_add_strm *str_add;
4146 str_add = (struct sctp_stream_reset_add_strm *)ph;
4148 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4149 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4150 struct sctp_stream_reset_in_request *req_in;
4153 req_in = (struct sctp_stream_reset_in_request *)ph;
4154 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4155 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4156 struct sctp_stream_reset_tsn_request *req_tsn;
4159 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4160 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4162 goto strres_nochunk;
4166 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4167 struct sctp_stream_reset_response *resp;
4170 resp = (struct sctp_stream_reset_response *)ph;
4171 seq = ntohl(resp->response_seq);
4172 result = ntohl(resp->result);
4173 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4175 goto strres_nochunk;
4180 offset += SCTP_SIZE32(param_len);
4181 chk_length -= SCTP_SIZE32(param_len);
4184 /* we have no response free the stuff */
4185 goto strres_nochunk;
4187 /* ok we have a chunk to link in */
4188 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4191 stcb->asoc.ctrl_queue_cnt++;
4196 * Handle a router or endpoints report of a packet loss, there are two ways
4197 * to handle this, either we get the whole packet and must disect it
4198 * ourselves (possibly with truncation and or corruption) or it is a summary
4199 * from a middle box that did the disectting for us.
4202 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4203 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4205 uint32_t bottle_bw, on_queue;
4209 struct sctp_chunk_desc desc;
4210 struct sctp_chunkhdr *ch;
4212 chlen = ntohs(cp->ch.chunk_length);
4213 chlen -= sizeof(struct sctp_pktdrop_chunk);
4214 /* XXX possible chlen underflow */
4217 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4218 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4220 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4221 chlen -= sizeof(struct sctphdr);
4222 /* XXX possible chlen underflow */
4223 memset(&desc, 0, sizeof(desc));
4225 trunc_len = (uint16_t) ntohs(cp->trunc_len);
4226 if (trunc_len > limit) {
4229 /* now the chunks themselves */
4230 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4231 desc.chunk_type = ch->chunk_type;
4232 /* get amount we need to move */
4233 at = ntohs(ch->chunk_length);
4234 if (at < sizeof(struct sctp_chunkhdr)) {
4235 /* corrupt chunk, maybe at the end? */
4236 SCTP_STAT_INCR(sctps_pdrpcrupt);
4239 if (trunc_len == 0) {
4240 /* we are supposed to have all of it */
4242 /* corrupt skip it */
4243 SCTP_STAT_INCR(sctps_pdrpcrupt);
4247 /* is there enough of it left ? */
4248 if (desc.chunk_type == SCTP_DATA) {
4249 if (chlen < (sizeof(struct sctp_data_chunk) +
4250 sizeof(desc.data_bytes))) {
4254 if (chlen < sizeof(struct sctp_chunkhdr)) {
4259 if (desc.chunk_type == SCTP_DATA) {
4260 /* can we get out the tsn? */
4261 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4262 SCTP_STAT_INCR(sctps_pdrpmbda);
4264 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4266 struct sctp_data_chunk *dcp;
4270 dcp = (struct sctp_data_chunk *)ch;
4271 ddp = (uint8_t *) (dcp + 1);
4272 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4273 desc.data_bytes[iii] = ddp[iii];
4275 desc.tsn_ifany = dcp->dp.tsn;
4277 /* nope we are done. */
4278 SCTP_STAT_INCR(sctps_pdrpnedat);
4282 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4283 SCTP_STAT_INCR(sctps_pdrpmbct);
4286 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4287 SCTP_STAT_INCR(sctps_pdrppdbrk);
4290 if (SCTP_SIZE32(at) > chlen) {
4293 chlen -= SCTP_SIZE32(at);
4294 if (chlen < sizeof(struct sctp_chunkhdr)) {
4295 /* done, none left */
4298 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4300 /* Now update any rwnd --- possibly */
4301 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4302 /* From a peer, we get a rwnd report */
4305 SCTP_STAT_INCR(sctps_pdrpfehos);
4307 bottle_bw = ntohl(cp->bottle_bw);
4308 on_queue = ntohl(cp->current_onq);
4309 if (bottle_bw && on_queue) {
4310 /* a rwnd report is in here */
4311 if (bottle_bw > on_queue)
4312 a_rwnd = bottle_bw - on_queue;
4317 stcb->asoc.peers_rwnd = 0;
4319 if (a_rwnd > stcb->asoc.total_flight) {
4320 stcb->asoc.peers_rwnd =
4321 a_rwnd - stcb->asoc.total_flight;
4323 stcb->asoc.peers_rwnd = 0;
4325 if (stcb->asoc.peers_rwnd <
4326 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4327 /* SWS sender side engages */
4328 stcb->asoc.peers_rwnd = 0;
4333 SCTP_STAT_INCR(sctps_pdrpfmbox);
4336 /* now middle boxes in sat networks get a cwnd bump */
4337 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4338 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4339 (stcb->asoc.sat_network)) {
4341 * This is debateable but for sat networks it makes sense
4342 * Note if a T3 timer has went off, we will prohibit any
4343 * changes to cwnd until we exit the t3 loss recovery.
4345 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4346 net, cp, &bottle_bw, &on_queue);
4351 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4352 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4353 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4354 * length of the complete packet outputs: - length: modified to remaining
4355 * length after control processing - netp: modified to new sctp_nets after
4356 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4357 * bad packet,...) otherwise return the tcb for this packet
4360 __attribute__((noinline))
4362 static struct sctp_tcb *
4363 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4364 struct sockaddr *src, struct sockaddr *dst,
4365 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4366 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4367 uint8_t use_mflowid, uint32_t mflowid,
4368 uint32_t vrf_id, uint16_t port)
4370 struct sctp_association *asoc;
4371 struct mbuf *op_err;
4372 char msg[SCTP_DIAG_INFO_LEN];
4374 int num_chunks = 0; /* number of control chunks processed */
4375 uint32_t chk_length;
4377 int abort_no_unlock = 0;
4381 * How big should this be, and should it be alloc'd? Lets try the
4382 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4383 * until we get into jumbo grams and such..
4385 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4386 struct sctp_tcb *locked_tcb = stcb;
4388 uint32_t auth_offset = 0, auth_len = 0;
4389 int auth_skipped = 0;
4392 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4397 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4398 iphlen, *offset, length, (void *)stcb);
4400 /* validate chunk header length... */
4401 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4402 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4403 ntohs(ch->chunk_length));
4405 SCTP_TCB_UNLOCK(locked_tcb);
4410 * validate the verification tag
4412 vtag_in = ntohl(sh->v_tag);
4415 SCTP_TCB_LOCK_ASSERT(locked_tcb);
4417 if (ch->chunk_type == SCTP_INITIATION) {
4418 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4419 ntohs(ch->chunk_length), vtag_in);
4421 /* protocol error- silently discard... */
4422 SCTP_STAT_INCR(sctps_badvtag);
4424 SCTP_TCB_UNLOCK(locked_tcb);
4428 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4430 * If there is no stcb, skip the AUTH chunk and process
4431 * later after a stcb is found (to validate the lookup was
4434 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4436 !SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4437 /* save this chunk for later processing */
4439 auth_offset = *offset;
4440 auth_len = ntohs(ch->chunk_length);
4442 /* (temporarily) move past this chunk */
4443 *offset += SCTP_SIZE32(auth_len);
4444 if (*offset >= length) {
4445 /* no more data left in the mbuf chain */
4448 SCTP_TCB_UNLOCK(locked_tcb);
4452 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4453 sizeof(struct sctp_chunkhdr), chunk_buf);
4459 SCTP_TCB_UNLOCK(locked_tcb);
4463 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4464 goto process_control_chunks;
4467 * first check if it's an ASCONF with an unknown src addr we
4468 * need to look inside to find the association
4470 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4471 struct sctp_chunkhdr *asconf_ch = ch;
4472 uint32_t asconf_offset = 0, asconf_len = 0;
4474 /* inp's refcount may be reduced */
4475 SCTP_INP_INCR_REF(inp);
4477 asconf_offset = *offset;
4479 asconf_len = ntohs(asconf_ch->chunk_length);
4480 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4482 stcb = sctp_findassociation_ep_asconf(m,
4485 sh, &inp, netp, vrf_id);
4488 asconf_offset += SCTP_SIZE32(asconf_len);
4489 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4490 sizeof(struct sctp_chunkhdr), chunk_buf);
4491 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4494 * reduce inp's refcount if not reduced in
4495 * sctp_findassociation_ep_asconf().
4497 SCTP_INP_DECR_REF(inp);
4502 /* now go back and verify any auth chunk to be sure */
4503 if (auth_skipped && (stcb != NULL)) {
4504 struct sctp_auth_chunk *auth;
4506 auth = (struct sctp_auth_chunk *)
4507 sctp_m_getptr(m, auth_offset,
4508 auth_len, chunk_buf);
4511 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4513 /* auth HMAC failed so dump it */
4516 SCTP_TCB_UNLOCK(locked_tcb);
4520 /* remaining chunks are HMAC checked */
4521 stcb->asoc.authenticated = 1;
4526 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s\n", __FILE__, __LINE__, __FUNCTION__);
4527 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4529 /* no association, so it's out of the blue... */
4530 sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4531 use_mflowid, mflowid,
4535 SCTP_TCB_UNLOCK(locked_tcb);
4540 /* ABORT and SHUTDOWN can use either v_tag... */
4541 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4542 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4543 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4544 /* Take the T-bit always into account. */
4545 if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
4546 (vtag_in == asoc->my_vtag)) ||
4547 (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4548 (vtag_in == asoc->peer_vtag))) {
4551 /* drop this packet... */
4552 SCTP_STAT_INCR(sctps_badvtag);
4554 SCTP_TCB_UNLOCK(locked_tcb);
4558 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4559 if (vtag_in != asoc->my_vtag) {
4561 * this could be a stale SHUTDOWN-ACK or the
4562 * peer never got the SHUTDOWN-COMPLETE and
4563 * is still hung; we have started a new asoc
4564 * but it won't complete until the shutdown
4568 SCTP_TCB_UNLOCK(locked_tcb);
4570 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s\n", __FILE__, __LINE__, __FUNCTION__);
4571 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4573 sctp_handle_ootb(m, iphlen, *offset, src, dst,
4575 use_mflowid, mflowid,
4580 /* for all other chunks, vtag must match */
4581 if (vtag_in != asoc->my_vtag) {
4582 /* invalid vtag... */
4583 SCTPDBG(SCTP_DEBUG_INPUT3,
4584 "invalid vtag: %xh, expect %xh\n",
4585 vtag_in, asoc->my_vtag);
4586 SCTP_STAT_INCR(sctps_badvtag);
4588 SCTP_TCB_UNLOCK(locked_tcb);
4594 } /* end if !SCTP_COOKIE_ECHO */
4596 * process all control chunks...
4598 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4599 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4600 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4601 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4602 /* implied cookie-ack.. we must have lost the ack */
4603 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4604 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4605 stcb->asoc.overall_error_count,
4607 SCTP_FROM_SCTP_INPUT,
4610 stcb->asoc.overall_error_count = 0;
4611 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4614 process_control_chunks:
4615 while (IS_SCTP_CONTROL(ch)) {
4616 /* validate chunk length */
4617 chk_length = ntohs(ch->chunk_length);
4618 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4619 ch->chunk_type, chk_length);
4620 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4621 if (chk_length < sizeof(*ch) ||
4622 (*offset + (int)chk_length) > length) {
4625 SCTP_TCB_UNLOCK(locked_tcb);
4629 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4631 * INIT-ACK only gets the init ack "header" portion only
4632 * because we don't have to process the peer's COOKIE. All
4633 * others get a complete chunk.
4635 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4636 (ch->chunk_type == SCTP_INITIATION)) {
4637 /* get an init-ack chunk */
4638 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4639 sizeof(struct sctp_init_ack_chunk), chunk_buf);
4643 SCTP_TCB_UNLOCK(locked_tcb);
4648 /* For cookies and all other chunks. */
4649 if (chk_length > sizeof(chunk_buf)) {
4651 * use just the size of the chunk buffer so
4652 * the front part of our chunks fit in
4653 * contiguous space up to the chunk buffer
4654 * size (508 bytes). For chunks that need to
4655 * get more than that they must use the
4656 * sctp_m_getptr() function or other means
4657 * (e.g. know how to parse mbuf chains).
4658 * Cookies do this already.
4660 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4661 (sizeof(chunk_buf) - 4),
4666 SCTP_TCB_UNLOCK(locked_tcb);
4671 /* We can fit it all */
4672 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4673 chk_length, chunk_buf);
4675 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4678 SCTP_TCB_UNLOCK(locked_tcb);
4685 /* Save off the last place we got a control from */
4687 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4689 * allow last_control to be NULL if
4690 * ASCONF... ASCONF processing will find the
4693 if ((netp != NULL) && (*netp != NULL))
4694 stcb->asoc.last_control_chunk_from = *netp;
4697 #ifdef SCTP_AUDITING_ENABLED
4698 sctp_audit_log(0xB0, ch->chunk_type);
4701 /* check to see if this chunk required auth, but isn't */
4702 if ((stcb != NULL) &&
4703 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
4704 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4705 !stcb->asoc.authenticated) {
4706 /* "silently" ignore */
4707 SCTP_STAT_INCR(sctps_recvauthmissing);
4710 switch (ch->chunk_type) {
4711 case SCTP_INITIATION:
4712 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4713 /* The INIT chunk must be the only chunk. */
4714 if ((num_chunks > 1) ||
4715 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4716 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4717 "INIT not the only chunk");
4718 sctp_abort_association(inp, stcb, m, iphlen,
4719 src, dst, sh, op_err,
4720 use_mflowid, mflowid,
4725 /* Honor our resource limit. */
4726 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4727 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4728 sctp_abort_association(inp, stcb, m, iphlen,
4729 src, dst, sh, op_err,
4730 use_mflowid, mflowid,
4735 sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4736 (struct sctp_init_chunk *)ch, inp,
4737 stcb, &abort_no_unlock,
4738 use_mflowid, mflowid,
4741 if ((!abort_no_unlock) && (locked_tcb)) {
4742 SCTP_TCB_UNLOCK(locked_tcb);
4746 case SCTP_PAD_CHUNK:
4748 case SCTP_INITIATION_ACK:
4749 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4750 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4751 /* We are not interested anymore */
4752 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4755 if (locked_tcb != stcb) {
4757 SCTP_TCB_UNLOCK(locked_tcb);
4761 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4762 so = SCTP_INP_SO(inp);
4763 atomic_add_int(&stcb->asoc.refcnt, 1);
4764 SCTP_TCB_UNLOCK(stcb);
4765 SCTP_SOCKET_LOCK(so, 1);
4766 SCTP_TCB_LOCK(stcb);
4767 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4769 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4770 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4771 SCTP_SOCKET_UNLOCK(so, 1);
4777 /* The INIT-ACK chunk must be the only chunk. */
4778 if ((num_chunks > 1) ||
4779 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4782 SCTP_TCB_UNLOCK(locked_tcb);
4786 if ((netp) && (*netp)) {
4787 ret = sctp_handle_init_ack(m, iphlen, *offset,
4789 (struct sctp_init_ack_chunk *)ch,
4792 use_mflowid, mflowid,
4798 if (abort_no_unlock) {
4802 * Special case, I must call the output routine to
4803 * get the cookie echoed
4805 if ((stcb != NULL) && (ret == 0)) {
4806 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4809 SCTP_TCB_UNLOCK(locked_tcb);
4813 case SCTP_SELECTIVE_ACK:
4815 struct sctp_sack_chunk *sack;
4817 uint32_t a_rwnd, cum_ack;
4818 uint16_t num_seg, num_dup;
4820 int offset_seg, offset_dup;
4822 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4823 SCTP_STAT_INCR(sctps_recvsacks);
4825 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4828 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4829 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4832 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4834 * If we have sent a shutdown-ack, we will pay no
4835 * attention to a sack sent in to us since
4836 * we don't care anymore.
4840 sack = (struct sctp_sack_chunk *)ch;
4841 flags = ch->chunk_flags;
4842 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4843 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4844 num_dup = ntohs(sack->sack.num_dup_tsns);
4845 a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
4846 if (sizeof(struct sctp_sack_chunk) +
4847 num_seg * sizeof(struct sctp_gap_ack_block) +
4848 num_dup * sizeof(uint32_t) != chk_length) {
4849 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4852 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4853 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4854 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4855 cum_ack, num_seg, a_rwnd);
4856 stcb->asoc.seen_a_sack_this_pkt = 1;
4857 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4859 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4860 (stcb->asoc.saw_sack_with_frags == 0) &&
4861 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4862 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4865 * We have a SIMPLE sack having no
4866 * prior segments and data on sent
4867 * queue to be acked.. Use the
4868 * faster path sack processing. We
4869 * also allow window update sacks
4870 * with no missing segments to go
4873 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
4876 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4877 num_seg, 0, num_dup, &abort_now, flags,
4878 cum_ack, a_rwnd, ecne_seen);
4881 /* ABORT signal from sack processing */
4885 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4886 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4887 (stcb->asoc.stream_queue_cnt == 0)) {
4888 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4893 * EY - nr_sack: If the received chunk is an
4896 case SCTP_NR_SELECTIVE_ACK:
4898 struct sctp_nr_sack_chunk *nr_sack;
4900 uint32_t a_rwnd, cum_ack;
4901 uint16_t num_seg, num_nr_seg, num_dup;
4903 int offset_seg, offset_dup;
4905 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
4906 SCTP_STAT_INCR(sctps_recvsacks);
4908 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
4911 if ((stcb->asoc.sctp_nr_sack_on_off == 0) ||
4912 (stcb->asoc.peer_supports_nr_sack == 0)) {
4915 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4916 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
4919 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4921 * If we have sent a shutdown-ack, we will pay no
4922 * attention to a sack sent in to us since
4923 * we don't care anymore.
4927 nr_sack = (struct sctp_nr_sack_chunk *)ch;
4928 flags = ch->chunk_flags;
4929 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4930 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4931 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4932 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4933 a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd);
4934 if (sizeof(struct sctp_nr_sack_chunk) +
4935 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4936 num_dup * sizeof(uint32_t) != chk_length) {
4937 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4940 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4941 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4942 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4943 cum_ack, num_seg, a_rwnd);
4944 stcb->asoc.seen_a_sack_this_pkt = 1;
4945 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4946 (num_seg == 0) && (num_nr_seg == 0) &&
4947 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4948 (stcb->asoc.saw_sack_with_frags == 0) &&
4949 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4950 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
4952 * We have a SIMPLE sack having no
4953 * prior segments and data on sent
4954 * queue to be acked. Use the faster
4955 * path sack processing. We also
4956 * allow window update sacks with no
4957 * missing segments to go this way
4960 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
4961 &abort_now, ecne_seen);
4964 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4965 num_seg, num_nr_seg, num_dup, &abort_now, flags,
4966 cum_ack, a_rwnd, ecne_seen);
4969 /* ABORT signal from sack processing */
4973 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4974 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4975 (stcb->asoc.stream_queue_cnt == 0)) {
4976 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4981 case SCTP_HEARTBEAT_REQUEST:
4982 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
4983 if ((stcb) && netp && *netp) {
4984 SCTP_STAT_INCR(sctps_recvheartbeat);
4985 sctp_send_heartbeat_ack(stcb, m, *offset,
4988 /* He's alive so give him credit */
4989 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4990 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4991 stcb->asoc.overall_error_count,
4993 SCTP_FROM_SCTP_INPUT,
4996 stcb->asoc.overall_error_count = 0;
4999 case SCTP_HEARTBEAT_ACK:
5000 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
5001 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5005 SCTP_TCB_UNLOCK(locked_tcb);
5009 /* He's alive so give him credit */
5010 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5011 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5012 stcb->asoc.overall_error_count,
5014 SCTP_FROM_SCTP_INPUT,
5017 stcb->asoc.overall_error_count = 0;
5018 SCTP_STAT_INCR(sctps_recvheartbeatack);
5020 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5023 case SCTP_ABORT_ASSOCIATION:
5024 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5026 if ((stcb) && netp && *netp)
5027 sctp_handle_abort((struct sctp_abort_chunk *)ch,
5033 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5035 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5038 SCTP_TCB_UNLOCK(locked_tcb);
5042 if (netp && *netp) {
5045 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5046 stcb, *netp, &abort_flag);
5053 case SCTP_SHUTDOWN_ACK:
5054 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", (void *)stcb);
5055 if ((stcb) && (netp) && (*netp))
5056 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5061 case SCTP_OPERATION_ERROR:
5062 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
5063 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
5068 case SCTP_COOKIE_ECHO:
5069 SCTPDBG(SCTP_DEBUG_INPUT3,
5070 "SCTP_COOKIE-ECHO, stcb %p\n", (void *)stcb);
5071 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5074 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5075 /* We are not interested anymore */
5078 SCTP_TCB_UNLOCK(stcb);
5085 * First are we accepting? We do this again here
5086 * since it is possible that a previous endpoint WAS
5087 * listening responded to a INIT-ACK and then
5088 * closed. We opened and bound.. and are now no
5092 if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
5093 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5094 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5095 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
5096 sctp_abort_association(inp, stcb, m, iphlen,
5097 src, dst, sh, op_err,
5098 use_mflowid, mflowid,
5104 struct mbuf *ret_buf;
5105 struct sctp_inpcb *linp;
5114 SCTP_ASOC_CREATE_LOCK(linp);
5115 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5116 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5117 SCTP_ASOC_CREATE_UNLOCK(linp);
5123 sctp_handle_cookie_echo(m, iphlen,
5127 (struct sctp_cookie_echo_chunk *)ch,
5141 SCTP_ASOC_CREATE_UNLOCK(linp);
5143 if (ret_buf == NULL) {
5145 SCTP_TCB_UNLOCK(locked_tcb);
5147 SCTPDBG(SCTP_DEBUG_INPUT3,
5148 "GAK, null buffer\n");
5152 /* if AUTH skipped, see if it verified... */
5157 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5159 * Restart the timer if we have
5162 struct sctp_tmit_chunk *chk;
5164 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5165 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5169 case SCTP_COOKIE_ACK:
5170 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", (void *)stcb);
5171 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5173 SCTP_TCB_UNLOCK(locked_tcb);
5177 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5178 /* We are not interested anymore */
5179 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5182 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5183 so = SCTP_INP_SO(inp);
5184 atomic_add_int(&stcb->asoc.refcnt, 1);
5185 SCTP_TCB_UNLOCK(stcb);
5186 SCTP_SOCKET_LOCK(so, 1);
5187 SCTP_TCB_LOCK(stcb);
5188 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5190 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
5191 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5192 SCTP_SOCKET_UNLOCK(so, 1);
5198 /* He's alive so give him credit */
5199 if ((stcb) && netp && *netp) {
5200 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5201 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5202 stcb->asoc.overall_error_count,
5204 SCTP_FROM_SCTP_INPUT,
5207 stcb->asoc.overall_error_count = 0;
5208 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5212 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5213 /* He's alive so give him credit */
5214 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5217 SCTP_TCB_UNLOCK(locked_tcb);
5223 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5224 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5225 stcb->asoc.overall_error_count,
5227 SCTP_FROM_SCTP_INPUT,
5230 stcb->asoc.overall_error_count = 0;
5231 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5237 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5238 /* He's alive so give him credit */
5239 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5242 SCTP_TCB_UNLOCK(locked_tcb);
5248 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5249 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5250 stcb->asoc.overall_error_count,
5252 SCTP_FROM_SCTP_INPUT,
5255 stcb->asoc.overall_error_count = 0;
5256 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5259 case SCTP_SHUTDOWN_COMPLETE:
5260 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", (void *)stcb);
5261 /* must be first and only chunk */
5262 if ((num_chunks > 1) ||
5263 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5266 SCTP_TCB_UNLOCK(locked_tcb);
5270 if ((stcb) && netp && *netp) {
5271 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5278 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5279 /* He's alive so give him credit */
5281 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5282 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5283 stcb->asoc.overall_error_count,
5285 SCTP_FROM_SCTP_INPUT,
5288 stcb->asoc.overall_error_count = 0;
5289 sctp_handle_asconf(m, *offset, src,
5290 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5294 case SCTP_ASCONF_ACK:
5295 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5296 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5299 SCTP_TCB_UNLOCK(locked_tcb);
5304 if ((stcb) && netp && *netp) {
5305 /* He's alive so give him credit */
5306 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5307 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5308 stcb->asoc.overall_error_count,
5310 SCTP_FROM_SCTP_INPUT,
5313 stcb->asoc.overall_error_count = 0;
5314 sctp_handle_asconf_ack(m, *offset,
5315 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5316 if (abort_no_unlock)
5320 case SCTP_FORWARD_CUM_TSN:
5321 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5322 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5325 SCTP_TCB_UNLOCK(locked_tcb);
5330 /* He's alive so give him credit */
5334 stcb->asoc.overall_error_count = 0;
5335 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5336 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5337 stcb->asoc.overall_error_count,
5339 SCTP_FROM_SCTP_INPUT,
5343 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5344 /* We are not interested anymore */
5345 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5346 so = SCTP_INP_SO(inp);
5347 atomic_add_int(&stcb->asoc.refcnt, 1);
5348 SCTP_TCB_UNLOCK(stcb);
5349 SCTP_SOCKET_LOCK(so, 1);
5350 SCTP_TCB_LOCK(stcb);
5351 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5353 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
5354 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5355 SCTP_SOCKET_UNLOCK(so, 1);
5360 sctp_handle_forward_tsn(stcb,
5361 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5366 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5367 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5368 stcb->asoc.overall_error_count,
5370 SCTP_FROM_SCTP_INPUT,
5373 stcb->asoc.overall_error_count = 0;
5378 case SCTP_STREAM_RESET:
5379 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5380 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5383 SCTP_TCB_UNLOCK(locked_tcb);
5388 if (stcb->asoc.peer_supports_strreset == 0) {
5390 * hmm, peer should have announced this, but
5391 * we will turn it on since he is sending us
5394 stcb->asoc.peer_supports_strreset = 1;
5396 if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5397 /* stop processing */
5402 case SCTP_PACKET_DROPPED:
5403 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5404 /* re-get it all please */
5405 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5408 SCTP_TCB_UNLOCK(locked_tcb);
5413 if (ch && (stcb) && netp && (*netp)) {
5414 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5416 min(chk_length, (sizeof(chunk_buf) - 4)));
5421 case SCTP_AUTHENTICATION:
5422 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5423 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
5427 /* save the first AUTH for later processing */
5428 if (auth_skipped == 0) {
5429 auth_offset = *offset;
5430 auth_len = chk_length;
5433 /* skip this chunk (temporarily) */
5436 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5437 (chk_length > (sizeof(struct sctp_auth_chunk) +
5438 SCTP_AUTH_DIGEST_LEN_MAX))) {
5441 SCTP_TCB_UNLOCK(locked_tcb);
5446 if (got_auth == 1) {
5447 /* skip this chunk... it's already auth'd */
5451 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5453 /* auth HMAC failed so dump the packet */
5457 /* remaining chunks are HMAC checked */
5458 stcb->asoc.authenticated = 1;
5464 /* it's an unknown chunk! */
5465 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5467 struct sctp_paramhdr *phd;
5469 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
5470 0, M_DONTWAIT, 1, MT_DATA);
5472 phd = mtod(mm, struct sctp_paramhdr *);
5474 * We cheat and use param type since
5475 * we did not bother to define a
5476 * error cause struct. They are the
5477 * same basic format with different
5480 phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5481 phd->param_length = htons(chk_length + sizeof(*phd));
5482 SCTP_BUF_LEN(mm) = sizeof(*phd);
5483 SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, chk_length, M_DONTWAIT);
5484 if (SCTP_BUF_NEXT(mm)) {
5485 if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(mm), SCTP_SIZE32(chk_length) - chk_length, NULL)) {
5488 #ifdef SCTP_MBUF_LOGGING
5489 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5492 for (mat = SCTP_BUF_NEXT(mm); mat; mat = SCTP_BUF_NEXT(mat)) {
5493 if (SCTP_BUF_IS_EXTENDED(mat)) {
5494 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5499 sctp_queue_op_err(stcb, mm);
5506 if ((ch->chunk_type & 0x80) == 0) {
5507 /* discard this packet */
5510 } /* else skip this bad chunk and continue... */
5512 } /* switch (ch->chunk_type) */
5516 /* get the next chunk */
5517 *offset += SCTP_SIZE32(chk_length);
5518 if (*offset >= length) {
5519 /* no more data left in the mbuf chain */
5522 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5523 sizeof(struct sctp_chunkhdr), chunk_buf);
5526 SCTP_TCB_UNLOCK(locked_tcb);
5533 if (asconf_cnt > 0 && stcb != NULL) {
5534 sctp_send_asconf_ack(stcb);
5542 __attribute__((noinline))
5545 sctp_validate_no_locks(struct sctp_inpcb *inp)
5547 struct sctp_tcb *lstcb;
5549 LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) {
5550 if (mtx_owned(&lstcb->tcb_mtx)) {
5551 panic("Own lock on stcb at return from input");
5554 if (mtx_owned(&inp->inp_create_mtx)) {
5555 panic("Own create lock on inp");
5557 if (mtx_owned(&inp->inp_mtx)) {
5558 panic("Own inp lock on inp");
5565 * common input chunk processing (v4 and v6)
5568 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5569 struct sockaddr *src, struct sockaddr *dst,
5570 struct sctphdr *sh, struct sctp_chunkhdr *ch,
5571 #if !defined(SCTP_WITH_NO_CSUM)
5572 uint8_t compute_crc,
5575 uint8_t use_mflowid, uint32_t mflowid,
5576 uint32_t vrf_id, uint16_t port)
5579 int fwd_tsn_seen = 0, data_processed = 0;
5580 struct mbuf *m = *mm, *op_err;
5581 char msg[SCTP_DIAG_INFO_LEN];
5583 int cnt_ctrl_ready = 0;
5584 struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5585 struct sctp_tcb *stcb = NULL;
5586 struct sctp_nets *net = NULL;
5588 SCTP_STAT_INCR(sctps_recvdatagrams);
5589 #ifdef SCTP_AUDITING_ENABLED
5590 sctp_audit_log(0xE0, 1);
5591 sctp_auditing(0, inp, stcb, net);
5593 #if !defined(SCTP_WITH_NO_CSUM)
5594 if (compute_crc != 0) {
5595 uint32_t check, calc_check;
5597 check = sh->checksum;
5599 calc_check = sctp_calculate_cksum(m, iphlen);
5600 sh->checksum = check;
5601 if (calc_check != check) {
5602 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5603 calc_check, check, (void *)m, length, iphlen);
5604 stcb = sctp_findassociation_addr(m, offset, src, dst,
5605 sh, ch, &inp, &net, vrf_id);
5606 if ((net != NULL) && (port != 0)) {
5607 if (net->port == 0) {
5608 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5612 if ((net != NULL) && (use_mflowid != 0)) {
5613 net->flowid = mflowid;
5618 if ((inp != NULL) && (stcb != NULL)) {
5619 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5620 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5621 } else if ((inp != NULL) && (stcb == NULL)) {
5624 SCTP_STAT_INCR(sctps_badsum);
5625 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5630 /* Destination port of 0 is illegal, based on RFC4960. */
5631 if (sh->dest_port == 0) {
5632 SCTP_STAT_INCR(sctps_hdrops);
5635 stcb = sctp_findassociation_addr(m, offset, src, dst,
5636 sh, ch, &inp, &net, vrf_id);
5637 if ((net != NULL) && (port != 0)) {
5638 if (net->port == 0) {
5639 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5643 if ((net != NULL) && (use_mflowid != 0)) {
5644 net->flowid = mflowid;
5650 SCTP_STAT_INCR(sctps_noport);
5651 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5654 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5655 sctp_send_shutdown_complete2(src, dst, sh,
5656 use_mflowid, mflowid,
5660 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5663 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5664 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5665 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5666 (ch->chunk_type != SCTP_INIT))) {
5667 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5669 sctp_send_abort(m, iphlen, src, dst,
5671 use_mflowid, mflowid,
5676 } else if (stcb == NULL) {
5681 * I very much doubt any of the IPSEC stuff will work but I have no
5682 * idea, so I will leave it in place.
5685 switch (dst->sa_family) {
5688 if (ipsec4_in_reject(m, &inp->ip_inp.inp)) {
5689 IPSECSTAT_INC(in_polvio);
5690 SCTP_STAT_INCR(sctps_hdrops);
5697 if (ipsec6_in_reject(m, &inp->ip_inp.inp)) {
5698 IPSEC6STAT_INC(in_polvio);
5699 SCTP_STAT_INCR(sctps_hdrops);
5709 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5710 (void *)m, iphlen, offset, length, (void *)stcb);
5712 /* always clear this before beginning a packet */
5713 stcb->asoc.authenticated = 0;
5714 stcb->asoc.seen_a_sack_this_pkt = 0;
5715 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5716 (void *)stcb, stcb->asoc.state);
5718 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5719 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5721 * If we hit here, we had a ref count
5722 * up when the assoc was aborted and the
5723 * timer is clearing out the assoc, we should
5724 * NOT respond to any packet.. its OOTB.
5726 SCTP_TCB_UNLOCK(stcb);
5728 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s\n", __FILE__, __LINE__, __FUNCTION__);
5729 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5731 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5732 use_mflowid, mflowid,
5737 if (IS_SCTP_CONTROL(ch)) {
5738 /* process the control portion of the SCTP packet */
5739 /* sa_ignore NO_NULL_CHK */
5740 stcb = sctp_process_control(m, iphlen, &offset, length,
5742 inp, stcb, &net, &fwd_tsn_seen,
5743 use_mflowid, mflowid,
5747 * This covers us if the cookie-echo was there and
5748 * it changes our INP.
5750 inp = stcb->sctp_ep;
5751 if ((net) && (port)) {
5752 if (net->port == 0) {
5753 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5760 * no control chunks, so pre-process DATA chunks (these
5761 * checks are taken care of by control processing)
5765 * if DATA only packet, and auth is required, then punt...
5766 * can't have authenticated without any AUTH (control)
5769 if ((stcb != NULL) &&
5770 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5771 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5772 /* "silently" ignore */
5773 SCTP_STAT_INCR(sctps_recvauthmissing);
5777 /* out of the blue DATA chunk */
5778 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s\n", __FILE__, __LINE__, __FUNCTION__);
5779 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5781 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5782 use_mflowid, mflowid,
5786 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5787 /* v_tag mismatch! */
5788 SCTP_STAT_INCR(sctps_badvtag);
5795 * no valid TCB for this packet, or we found it's a bad
5796 * packet while processing control, or we're done with this
5797 * packet (done or skip rest of data), so we drop it...
5802 * DATA chunk processing
5804 /* plow through the data chunks while length > offset */
5807 * Rest should be DATA only. Check authentication state if AUTH for
5810 if ((length > offset) &&
5812 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5813 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5814 !stcb->asoc.authenticated) {
5815 /* "silently" ignore */
5816 SCTP_STAT_INCR(sctps_recvauthmissing);
5817 SCTPDBG(SCTP_DEBUG_AUTH1,
5818 "Data chunk requires AUTH, skipped\n");
5821 if (length > offset) {
5825 * First check to make sure our state is correct. We would
5826 * not get here unless we really did have a tag, so we don't
5827 * abort if this happens, just dump the chunk silently.
5829 switch (SCTP_GET_STATE(&stcb->asoc)) {
5830 case SCTP_STATE_COOKIE_ECHOED:
5832 * we consider data with valid tags in this state
5833 * shows us the cookie-ack was lost. Imply it was
5836 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5837 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5838 stcb->asoc.overall_error_count,
5840 SCTP_FROM_SCTP_INPUT,
5843 stcb->asoc.overall_error_count = 0;
5844 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5846 case SCTP_STATE_COOKIE_WAIT:
5848 * We consider OOTB any data sent during asoc setup.
5850 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s\n", __FILE__, __LINE__, __FUNCTION__);
5851 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5853 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5854 use_mflowid, mflowid,
5857 /* sa_ignore NOTREACHED */
5859 case SCTP_STATE_EMPTY: /* should not happen */
5860 case SCTP_STATE_INUSE: /* should not happen */
5861 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5862 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5865 /* sa_ignore NOTREACHED */
5867 case SCTP_STATE_OPEN:
5868 case SCTP_STATE_SHUTDOWN_SENT:
5871 /* plow through the data chunks while length > offset */
5872 retval = sctp_process_data(mm, iphlen, &offset, length,
5874 inp, stcb, net, &high_tsn,
5875 use_mflowid, mflowid,
5879 * The association aborted, NO UNLOCK needed since
5880 * the association is destroyed.
5887 * Anything important needs to have been m_copy'ed in
5891 /* take care of ecn */
5892 if ((data_processed == 1) &&
5893 (stcb->asoc.ecn_allowed == 1) &&
5894 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5895 /* Yep, we need to add a ECNE */
5896 sctp_send_ecn_echo(stcb, net, high_tsn);
5898 if ((data_processed == 0) && (fwd_tsn_seen)) {
5900 uint32_t highest_tsn;
5902 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5903 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5905 highest_tsn = stcb->asoc.highest_tsn_inside_map;
5907 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
5908 stcb->asoc.send_sack = 1;
5909 sctp_sack_check(stcb, was_a_gap);
5910 } else if (fwd_tsn_seen) {
5911 stcb->asoc.send_sack = 1;
5913 /* trigger send of any chunks in queue... */
5915 #ifdef SCTP_AUDITING_ENABLED
5916 sctp_audit_log(0xE0, 2);
5917 sctp_auditing(1, inp, stcb, net);
5919 SCTPDBG(SCTP_DEBUG_INPUT1,
5920 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5921 stcb->asoc.peers_rwnd,
5922 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5923 stcb->asoc.total_flight);
5924 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5925 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5926 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5928 if (cnt_ctrl_ready ||
5930 (stcb->asoc.peers_rwnd > 0 ||
5931 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5932 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5933 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5934 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5936 #ifdef SCTP_AUDITING_ENABLED
5937 sctp_audit_log(0xE0, 3);
5938 sctp_auditing(2, inp, stcb, net);
5942 SCTP_TCB_UNLOCK(stcb);
5944 if (inp_decr != NULL) {
5945 /* reduce ref-count */
5946 SCTP_INP_WLOCK(inp_decr);
5947 SCTP_INP_DECR_REF(inp_decr);
5948 SCTP_INP_WUNLOCK(inp_decr);
5952 sctp_validate_no_locks(inp);
5960 sctp_print_mbuf_chain(struct mbuf *m)
5962 for (; m; m = SCTP_BUF_NEXT(m)) {
5963 SCTP_PRINTF("%p: m_len = %ld\n", (void *)m, SCTP_BUF_LEN(m));
5964 if (SCTP_BUF_IS_EXTENDED(m))
5965 SCTP_PRINTF("%p: extend_size = %d\n", (void *)m, SCTP_BUF_EXTEND_SIZE(m));
5973 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5977 uint32_t vrf_id = 0;
5979 struct sockaddr_in src, dst;
5982 struct sctp_chunkhdr *ch;
5985 #if !defined(SCTP_WITH_NO_CSUM)
5986 uint8_t compute_crc;
5990 uint8_t use_mflowid;
5993 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
5994 SCTP_RELEASE_PKT(i_pak);
5997 m = SCTP_HEADER_TO_CHAIN(i_pak);
5998 #ifdef SCTP_MBUF_LOGGING
5999 /* Log in any input mbufs */
6000 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6003 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
6004 if (SCTP_BUF_IS_EXTENDED(mat)) {
6005 sctp_log_mb(mat, SCTP_MBUF_INPUT);
6010 #ifdef SCTP_PACKET_LOGGING
6011 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
6015 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6016 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
6018 if_name(m->m_pkthdr.rcvif),
6019 m->m_pkthdr.csum_flags);
6020 if (m->m_flags & M_FLOWID) {
6021 mflowid = m->m_pkthdr.flowid;
6027 SCTP_STAT_INCR(sctps_recvpackets);
6028 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
6029 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
6030 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6031 if (SCTP_BUF_LEN(m) < offset) {
6032 if ((m = m_pullup(m, offset)) == NULL) {
6033 SCTP_STAT_INCR(sctps_hdrops);
6037 ip = mtod(m, struct ip *);
6038 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
6039 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
6040 offset -= sizeof(struct sctp_chunkhdr);
6041 memset(&src, 0, sizeof(struct sockaddr_in));
6042 src.sin_family = AF_INET;
6043 src.sin_len = sizeof(struct sockaddr_in);
6044 src.sin_port = sh->src_port;
6045 src.sin_addr = ip->ip_src;
6046 memset(&dst, 0, sizeof(struct sockaddr_in));
6047 dst.sin_family = AF_INET;
6048 dst.sin_len = sizeof(struct sockaddr_in);
6049 dst.sin_port = sh->dest_port;
6050 dst.sin_addr = ip->ip_dst;
6051 length = ip->ip_len + iphlen;
6052 /* Validate mbuf chain length with IP payload length. */
6053 if (SCTP_HEADER_LEN(m) != length) {
6054 SCTPDBG(SCTP_DEBUG_INPUT1,
6055 "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
6056 SCTP_STAT_INCR(sctps_hdrops);
6059 /* SCTP does not allow broadcasts or multicasts */
6060 if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
6063 if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
6066 ecn_bits = ip->ip_tos;
6067 #if defined(SCTP_WITH_NO_CSUM)
6068 SCTP_STAT_INCR(sctps_recvnocrc);
6070 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
6071 SCTP_STAT_INCR(sctps_recvhwcrc);
6074 SCTP_STAT_INCR(sctps_recvswcrc);
6078 sctp_common_input_processing(&m, iphlen, offset, length,
6079 (struct sockaddr *)&src,
6080 (struct sockaddr *)&dst,
6082 #if !defined(SCTP_WITH_NO_CSUM)
6086 use_mflowid, mflowid,
6095 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6096 extern int *sctp_cpuarry;
6101 sctp_input(struct mbuf *m, int off)
6103 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6108 uint32_t flowid, tag;
6111 if (m->m_flags & M_FLOWID) {
6112 flowid = m->m_pkthdr.flowid;
6115 * No flow id built by lower layers fix it so we
6118 offset = off + sizeof(struct sctphdr);
6119 if (SCTP_BUF_LEN(m) < offset) {
6120 if ((m = m_pullup(m, offset)) == NULL) {
6121 SCTP_STAT_INCR(sctps_hdrops);
6125 ip = mtod(m, struct ip *);
6126 sh = (struct sctphdr *)((caddr_t)ip + off);
6127 tag = htonl(sh->v_tag);
6128 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6129 m->m_pkthdr.flowid = flowid;
6130 m->m_flags |= M_FLOWID;
6132 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6133 sctp_queue_to_mcore(m, off, cpu_to_use);
6137 sctp_input_with_port(m, off, 0);