2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.h>
37 #include <netinet/sctp_var.h>
38 #include <netinet/sctp_sysctl.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctputil.h>
42 #include <netinet/sctp_output.h>
43 #include <netinet/sctp_input.h>
44 #include <netinet/sctp_auth.h>
45 #include <netinet/sctp_indata.h>
46 #include <netinet/sctp_asconf.h>
47 #include <netinet/sctp_bsd_addr.h>
48 #include <netinet/sctp_timer.h>
49 #include <netinet/sctp_crc32.h>
50 #include <netinet/udp.h>
56 sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
58 struct sctp_nets *net;
61 * This now not only stops all cookie timers it also stops any INIT
62 * timers as well. This will make sure that the timers are stopped
63 * in all collision cases.
65 SCTP_TCB_LOCK_ASSERT(stcb);
66 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
67 if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
68 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
71 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
72 } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
73 sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
76 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
83 sctp_handle_init(struct mbuf *m, int iphlen, int offset,
84 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
85 struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
86 struct sctp_tcb *stcb, int *abort_no_unlock,
87 uint8_t use_mflowid, uint32_t mflowid,
88 uint32_t vrf_id, uint16_t port)
90 struct sctp_init *init;
93 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
99 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
100 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
101 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
102 use_mflowid, mflowid,
105 *abort_no_unlock = 1;
108 /* validate parameters */
110 if (init->initiate_tag == 0) {
111 /* protocol error... send abort */
112 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
113 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
114 use_mflowid, mflowid,
117 *abort_no_unlock = 1;
120 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
121 /* invalid parameter... send abort */
122 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
123 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
124 use_mflowid, mflowid,
127 *abort_no_unlock = 1;
130 if (init->num_inbound_streams == 0) {
131 /* protocol error... send abort */
132 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
133 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
134 use_mflowid, mflowid,
137 *abort_no_unlock = 1;
140 if (init->num_outbound_streams == 0) {
141 /* protocol error... send abort */
142 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
143 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
144 use_mflowid, mflowid,
147 *abort_no_unlock = 1;
150 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
151 offset + ntohs(cp->ch.chunk_length))) {
152 /* auth parameter(s) error... send abort */
153 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, NULL,
154 use_mflowid, mflowid,
157 *abort_no_unlock = 1;
161 * We are only accepting if we have a socket with positive
164 if ((stcb == NULL) &&
165 ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
166 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
167 (inp->sctp_socket == NULL) ||
168 (inp->sctp_socket->so_qlimit == 0))) {
170 * FIX ME ?? What about TCP model and we have a
171 * match/restart case? Actually no fix is needed. the lookup
172 * will always find the existing assoc so stcb would not be
173 * NULL. It may be questionable to do this since we COULD
174 * just send back the INIT-ACK and hope that the app did
175 * accept()'s by the time the COOKIE was sent. But there is
176 * a price to pay for COOKIE generation and I don't want to
177 * pay it on the chance that the app will actually do some
178 * accepts(). The App just looses and should NOT be in this
181 if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
182 sctp_send_abort(m, iphlen, src, dst, sh, 0, NULL,
183 use_mflowid, mflowid,
188 if ((stcb != NULL) &&
189 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
190 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
191 sctp_send_shutdown_ack(stcb, NULL);
192 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
194 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
195 sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, src, dst,
197 use_mflowid, mflowid,
199 ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED));
203 SCTP_INP_RUNLOCK(inp);
208 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
212 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
213 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
220 struct sctp_stream_queue_pending *sp;
221 struct sctp_association *asoc;
224 * This function returns the number of streams that have true unsent
225 * data on them. Note that as it looks through it will clean up any
226 * places that have old data that has been sent but left at top of
230 SCTP_TCB_SEND_LOCK(stcb);
231 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
232 /* Check to see if some data queued */
233 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
234 /* sa_ignore FREED_MEMORY */
235 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
239 if ((sp->msg_is_complete) &&
241 (sp->sender_all_done)) {
243 * We are doing differed cleanup. Last time
244 * through when we took all the data the
245 * sender_all_done was not set.
247 if (sp->put_last_out == 0) {
248 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
249 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
255 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
256 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
258 sctp_free_remote_addr(sp->net);
262 sctp_m_freem(sp->data);
265 sctp_free_a_strmoq(stcb, sp, so_locked);
272 SCTP_TCB_SEND_UNLOCK(stcb);
273 return (unsent_data);
277 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
279 struct sctp_init *init;
280 struct sctp_association *asoc;
281 struct sctp_nets *lnet;
286 /* save off parameters */
287 asoc->peer_vtag = ntohl(init->initiate_tag);
288 asoc->peers_rwnd = ntohl(init->a_rwnd);
290 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
292 if (!TAILQ_EMPTY(&asoc->nets)) {
293 /* update any ssthresh's that may have a default */
294 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
295 lnet->ssthresh = asoc->peers_rwnd;
296 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
297 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
301 SCTP_TCB_SEND_LOCK(stcb);
302 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
304 struct sctp_stream_out *outs;
305 struct sctp_stream_queue_pending *sp, *nsp;
306 struct sctp_tmit_chunk *chk, *nchk;
308 /* abandon the upper streams */
309 newcnt = ntohs(init->num_inbound_streams);
310 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
311 if (chk->rec.data.stream_number >= newcnt) {
312 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
313 asoc->send_queue_cnt--;
314 if (chk->data != NULL) {
315 sctp_free_bufspace(stcb, asoc, chk, 1);
316 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
317 0, chk, SCTP_SO_NOT_LOCKED);
319 sctp_m_freem(chk->data);
323 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
324 /* sa_ignore FREED_MEMORY */
328 for (i = newcnt; i < asoc->pre_open_streams; i++) {
329 outs = &asoc->strmout[i];
330 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
331 TAILQ_REMOVE(&outs->outqueue, sp, next);
332 asoc->stream_queue_cnt--;
333 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
334 stcb, 0, sp, SCTP_SO_NOT_LOCKED);
336 sctp_m_freem(sp->data);
340 sctp_free_remote_addr(sp->net);
344 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
345 /* sa_ignore FREED_MEMORY */
349 /* cut back the count */
350 asoc->pre_open_streams = newcnt;
352 SCTP_TCB_SEND_UNLOCK(stcb);
353 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
355 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
356 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
357 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
358 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
360 /* This is the next one we expect */
361 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
363 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
364 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
366 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
367 /* open the requested streams */
369 if (asoc->strmin != NULL) {
370 /* Free the old ones */
371 struct sctp_queued_to_read *ctl, *nctl;
373 for (i = 0; i < asoc->streamincnt; i++) {
374 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) {
375 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
376 sctp_free_remote_addr(ctl->whoFrom);
378 sctp_m_freem(ctl->data);
380 sctp_free_a_readq(stcb, ctl);
383 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
385 asoc->streamincnt = ntohs(init->num_outbound_streams);
386 if (asoc->streamincnt > MAX_SCTP_STREAMS) {
387 asoc->streamincnt = MAX_SCTP_STREAMS;
389 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
390 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
391 if (asoc->strmin == NULL) {
392 /* we didn't get memory for the streams! */
393 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
396 for (i = 0; i < asoc->streamincnt; i++) {
397 asoc->strmin[i].stream_no = i;
398 asoc->strmin[i].last_sequence_delivered = 0xffff;
400 * U-stream ranges will be set when the cookie is unpacked.
401 * Or for the INIT sender they are un set (if pr-sctp not
402 * supported) when the INIT-ACK arrives.
404 TAILQ_INIT(&asoc->strmin[i].inqueue);
405 asoc->strmin[i].delivery_started = 0;
408 * load_address_from_init will put the addresses into the
409 * association when the COOKIE is processed or the INIT-ACK is
410 * processed. Both types of COOKIE's existing and new call this
411 * routine. It will remove addresses that are no longer in the
412 * association (for the restarting case where addresses are
413 * removed). Up front when the INIT arrives we will discard it if it
414 * is a restart and new addresses have been added.
416 /* sa_ignore MEMLEAK */
421 * INIT-ACK message processing/consumption returns value < 0 on error
424 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
425 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
426 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
427 struct sctp_nets *net, int *abort_no_unlock,
428 uint8_t use_mflowid, uint32_t mflowid,
431 struct sctp_association *asoc;
433 int retval, abort_flag;
434 uint32_t initack_limit;
435 int nat_friendly = 0;
437 /* First verify that we have no illegal param's */
441 op_err = sctp_arethere_unrecognized_parameters(m,
442 (offset + sizeof(struct sctp_init_chunk)),
443 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
445 /* Send an abort and notify peer */
446 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
447 *abort_no_unlock = 1;
451 asoc->peer_supports_nat = (uint8_t) nat_friendly;
452 /* process the peer's parameters in the INIT-ACK */
453 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
457 initack_limit = offset + ntohs(cp->ch.chunk_length);
458 /* load all addresses */
459 if ((retval = sctp_load_addresses_from_init(stcb, m,
460 (offset + sizeof(struct sctp_init_chunk)), initack_limit,
462 /* Huh, we should abort */
463 SCTPDBG(SCTP_DEBUG_INPUT1,
464 "Load addresses from INIT causes an abort %d\n",
466 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
468 use_mflowid, mflowid,
470 *abort_no_unlock = 1;
473 /* if the peer doesn't support asconf, flush the asconf queue */
474 if (asoc->peer_supports_asconf == 0) {
475 struct sctp_asconf_addr *param, *nparam;
477 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
478 TAILQ_REMOVE(&asoc->asconf_queue, param, next);
479 SCTP_FREE(param, SCTP_M_ASC_ADDR);
482 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
483 stcb->asoc.local_hmacs);
485 sctp_queue_op_err(stcb, op_err);
486 /* queuing will steal away the mbuf chain to the out queue */
489 /* extract the cookie and queue it to "echo" it back... */
490 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
491 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
492 stcb->asoc.overall_error_count,
494 SCTP_FROM_SCTP_INPUT,
497 stcb->asoc.overall_error_count = 0;
498 net->error_count = 0;
501 * Cancel the INIT timer, We do this first before queueing the
502 * cookie. We always cancel at the primary to assue that we are
503 * canceling the timer started by the INIT which always goes to the
506 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
507 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
509 /* calculate the RTO */
510 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy,
511 SCTP_RTT_FROM_NON_DATA);
513 retval = sctp_send_cookie_echo(m, offset, stcb, net);
516 * No cookie, we probably should send a op error. But in any
517 * case if there is no cookie in the INIT-ACK, we can
518 * abandon the peer, its broke.
521 /* We abort with an error of missing mandatory param */
523 sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM);
526 * Expand beyond to include the mandatory
529 struct sctp_inv_mandatory_param *mp;
531 SCTP_BUF_LEN(op_err) =
532 sizeof(struct sctp_inv_mandatory_param);
534 struct sctp_inv_mandatory_param *);
535 /* Subtract the reserved param */
537 htons(sizeof(struct sctp_inv_mandatory_param) - 2);
538 mp->num_param = htonl(1);
539 mp->param = htons(SCTP_STATE_COOKIE);
542 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
543 src, dst, sh, op_err,
544 use_mflowid, mflowid,
546 *abort_no_unlock = 1;
554 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
555 struct sctp_tcb *stcb, struct sctp_nets *net)
557 struct sockaddr_storage store;
558 struct sctp_nets *r_net, *f_net;
561 uint16_t old_error_counter;
564 struct sockaddr_in *sin;
568 struct sockaddr_in6 *sin6;
572 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
576 memset(&store, 0, sizeof(store));
577 switch (cp->heartbeat.hb_info.addr_family) {
580 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
581 sin = (struct sockaddr_in *)&store;
582 sin->sin_family = cp->heartbeat.hb_info.addr_family;
583 sin->sin_len = cp->heartbeat.hb_info.addr_len;
584 sin->sin_port = stcb->rport;
585 memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address,
586 sizeof(sin->sin_addr));
594 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
595 sin6 = (struct sockaddr_in6 *)&store;
596 sin6->sin6_family = cp->heartbeat.hb_info.addr_family;
597 sin6->sin6_len = cp->heartbeat.hb_info.addr_len;
598 sin6->sin6_port = stcb->rport;
599 memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address,
600 sizeof(sin6->sin6_addr));
609 r_net = sctp_findnet(stcb, (struct sockaddr *)&store);
611 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
614 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
615 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
616 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
618 * If the its a HB and it's random value is correct when can
619 * confirm the destination.
621 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
622 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
623 stcb->asoc.primary_destination = r_net;
624 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
625 f_net = TAILQ_FIRST(&stcb->asoc.nets);
626 if (f_net != r_net) {
628 * first one on the list is NOT the primary
629 * sctp_cmpaddr() is much more efficent if
630 * the primary is the first on the list,
633 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
634 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
638 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
639 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
640 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
641 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
643 old_error_counter = r_net->error_count;
644 r_net->error_count = 0;
645 r_net->hb_responded = 1;
646 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
647 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
648 /* Now lets do a RTO with this */
649 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy,
650 SCTP_RTT_FROM_NON_DATA);
651 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
652 r_net->dest_state |= SCTP_ADDR_REACHABLE;
653 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
654 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
656 if (r_net->dest_state & SCTP_ADDR_PF) {
657 r_net->dest_state &= ~SCTP_ADDR_PF;
658 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
660 if (old_error_counter > 0) {
661 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
662 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
664 if (r_net == stcb->asoc.primary_destination) {
665 if (stcb->asoc.alternate) {
666 /* release the alternate, primary is good */
667 sctp_free_remote_addr(stcb->asoc.alternate);
668 stcb->asoc.alternate = NULL;
671 /* Mobility adaptation */
673 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
674 SCTP_MOBILITY_BASE) ||
675 sctp_is_mobility_feature_on(stcb->sctp_ep,
676 SCTP_MOBILITY_FASTHANDOFF)) &&
677 sctp_is_mobility_feature_on(stcb->sctp_ep,
678 SCTP_MOBILITY_PRIM_DELETED)) {
680 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7);
681 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
682 SCTP_MOBILITY_FASTHANDOFF)) {
683 sctp_assoc_immediate_retrans(stcb,
684 stcb->asoc.primary_destination);
686 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
687 SCTP_MOBILITY_BASE)) {
688 sctp_move_chunks_from_net(stcb,
689 stcb->asoc.deleted_primary);
691 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
692 stcb->asoc.deleted_primary);
698 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
701 * return 0 means we want you to proceed with the abort non-zero
702 * means no abort processing
704 struct sctpasochead *head;
706 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
707 /* generate a new vtag and send init */
708 LIST_REMOVE(stcb, sctp_asocs);
709 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
710 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
712 * put it in the bucket in the vtag hash of assoc's for the
715 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
716 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
719 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
721 * treat like a case where the cookie expired i.e.: - dump
722 * current cookie. - generate a new vtag. - resend init.
724 /* generate a new vtag and send init */
725 LIST_REMOVE(stcb, sctp_asocs);
726 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
727 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
728 sctp_stop_all_cookie_timers(stcb);
729 sctp_toss_old_cookies(stcb, &stcb->asoc);
730 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
731 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
733 * put it in the bucket in the vtag hash of assoc's for the
736 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
737 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
744 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
745 struct sctp_nets *net)
748 * return 0 means we want you to proceed with the abort non-zero
749 * means no abort processing
751 if (stcb->asoc.peer_supports_auth == 0) {
752 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
755 sctp_asconf_send_nat_state_update(stcb, net);
761 sctp_handle_abort(struct sctp_abort_chunk *abort,
762 struct sctp_tcb *stcb, struct sctp_nets *net)
764 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
771 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
775 len = ntohs(abort->ch.chunk_length);
776 if (len > sizeof(struct sctp_chunkhdr)) {
778 * Need to check the cause codes for our two magic nat
779 * aborts which don't kill the assoc necessarily.
781 struct sctp_missing_nat_state *natc;
783 natc = (struct sctp_missing_nat_state *)(abort + 1);
784 error = ntohs(natc->cause);
785 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
786 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
787 abort->ch.chunk_flags);
788 if (sctp_handle_nat_colliding_state(stcb)) {
791 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
792 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
793 abort->ch.chunk_flags);
794 if (sctp_handle_nat_missing_state(stcb, net)) {
801 /* stop any receive timers */
802 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
803 /* notify user of the abort and clean up... */
804 sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
806 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
807 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
808 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
809 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
811 #ifdef SCTP_ASOCLOG_OF_TSNS
812 sctp_print_out_track_log(stcb);
814 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
815 so = SCTP_INP_SO(stcb->sctp_ep);
816 atomic_add_int(&stcb->asoc.refcnt, 1);
817 SCTP_TCB_UNLOCK(stcb);
818 SCTP_SOCKET_LOCK(so, 1);
820 atomic_subtract_int(&stcb->asoc.refcnt, 1);
822 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
823 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
824 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
825 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
826 SCTP_SOCKET_UNLOCK(so, 1);
828 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
832 sctp_start_net_timers(struct sctp_tcb *stcb)
834 uint32_t cnt_hb_sent;
835 struct sctp_nets *net;
838 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
840 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
841 * If the dest in unconfirmed send a hb as well if under
842 * max_hb_burst have been sent.
844 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
845 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
846 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
847 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
848 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
853 sctp_chunk_output(stcb->sctp_ep, stcb,
854 SCTP_OUTPUT_FROM_COOKIE_ACK,
861 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
862 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
864 struct sctp_association *asoc;
865 int some_on_streamwheel;
867 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
872 SCTPDBG(SCTP_DEBUG_INPUT2,
873 "sctp_handle_shutdown: handling SHUTDOWN\n");
877 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
878 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
881 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
882 /* Shutdown NOT the expected size */
885 sctp_update_acked(stcb, cp, abort_flag);
890 if (asoc->control_pdapi) {
892 * With a normal shutdown we assume the end of last record.
894 SCTP_INP_READ_LOCK(stcb->sctp_ep);
895 asoc->control_pdapi->end_added = 1;
896 asoc->control_pdapi->pdapi_aborted = 1;
897 asoc->control_pdapi = NULL;
898 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
899 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
900 so = SCTP_INP_SO(stcb->sctp_ep);
901 atomic_add_int(&stcb->asoc.refcnt, 1);
902 SCTP_TCB_UNLOCK(stcb);
903 SCTP_SOCKET_LOCK(so, 1);
905 atomic_subtract_int(&stcb->asoc.refcnt, 1);
906 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
907 /* assoc was freed while we were unlocked */
908 SCTP_SOCKET_UNLOCK(so, 1);
912 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
913 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
914 SCTP_SOCKET_UNLOCK(so, 1);
917 /* goto SHUTDOWN_RECEIVED state to block new requests */
918 if (stcb->sctp_socket) {
919 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
920 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
921 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
922 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
923 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
925 * notify upper layer that peer has initiated a
928 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
931 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
934 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
936 * stop the shutdown timer, since we WILL move to
939 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
941 /* Now is there unsent data on a stream somewhere? */
942 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
944 if (!TAILQ_EMPTY(&asoc->send_queue) ||
945 !TAILQ_EMPTY(&asoc->sent_queue) ||
946 some_on_streamwheel) {
947 /* By returning we will push more data out */
950 /* no outstanding data to send, so move on... */
951 /* send SHUTDOWN-ACK */
952 sctp_send_shutdown_ack(stcb, net);
953 /* move to SHUTDOWN-ACK-SENT state */
954 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
955 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
956 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
958 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
959 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
960 sctp_stop_timers_for_shutdown(stcb);
961 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
967 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
968 struct sctp_tcb *stcb,
969 struct sctp_nets *net)
971 struct sctp_association *asoc;
973 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
976 so = SCTP_INP_SO(stcb->sctp_ep);
978 SCTPDBG(SCTP_DEBUG_INPUT2,
979 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
984 /* process according to association state */
985 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
986 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
987 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
988 sctp_send_shutdown_complete(stcb, net, 1);
989 SCTP_TCB_UNLOCK(stcb);
992 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
993 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
994 /* unexpected SHUTDOWN-ACK... so ignore... */
995 SCTP_TCB_UNLOCK(stcb);
998 if (asoc->control_pdapi) {
1000 * With a normal shutdown we assume the end of last record.
1002 SCTP_INP_READ_LOCK(stcb->sctp_ep);
1003 asoc->control_pdapi->end_added = 1;
1004 asoc->control_pdapi->pdapi_aborted = 1;
1005 asoc->control_pdapi = NULL;
1006 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1007 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1008 atomic_add_int(&stcb->asoc.refcnt, 1);
1009 SCTP_TCB_UNLOCK(stcb);
1010 SCTP_SOCKET_LOCK(so, 1);
1011 SCTP_TCB_LOCK(stcb);
1012 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1013 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1014 /* assoc was freed while we were unlocked */
1015 SCTP_SOCKET_UNLOCK(so, 1);
1019 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1020 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1021 SCTP_SOCKET_UNLOCK(so, 1);
1024 /* are the queues empty? */
1025 if (!TAILQ_EMPTY(&asoc->send_queue) ||
1026 !TAILQ_EMPTY(&asoc->sent_queue) ||
1027 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
1028 sctp_report_all_outbound(stcb, 0, 0, SCTP_SO_NOT_LOCKED);
1030 /* stop the timer */
1031 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
1032 /* send SHUTDOWN-COMPLETE */
1033 sctp_send_shutdown_complete(stcb, net, 0);
1034 /* notify upper layer protocol */
1035 if (stcb->sctp_socket) {
1036 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1037 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1038 stcb->sctp_socket->so_snd.sb_cc = 0;
1040 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1042 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1043 /* free the TCB but first save off the ep */
1044 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1045 atomic_add_int(&stcb->asoc.refcnt, 1);
1046 SCTP_TCB_UNLOCK(stcb);
1047 SCTP_SOCKET_LOCK(so, 1);
1048 SCTP_TCB_LOCK(stcb);
1049 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1051 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1052 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1053 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1054 SCTP_SOCKET_UNLOCK(so, 1);
1059 * Skip past the param header and then we will find the chunk that caused the
1060 * problem. There are two possiblities ASCONF or FWD-TSN other than that and
1061 * our peer must be broken.
1064 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1065 struct sctp_nets *net)
1067 struct sctp_chunkhdr *chk;
1069 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1070 switch (chk->chunk_type) {
1071 case SCTP_ASCONF_ACK:
1073 sctp_asconf_cleanup(stcb, net);
1075 case SCTP_FORWARD_CUM_TSN:
1076 stcb->asoc.peer_supports_prsctp = 0;
1079 SCTPDBG(SCTP_DEBUG_INPUT2,
1080 "Peer does not support chunk type %d(%x)??\n",
1081 chk->chunk_type, (uint32_t) chk->chunk_type);
1087 * Skip past the param header and then we will find the param that caused the
1088 * problem. There are a number of param's in a ASCONF OR the prsctp param
1089 * these will turn of specific features.
1092 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1094 struct sctp_paramhdr *pbad;
1097 switch (ntohs(pbad->param_type)) {
1099 case SCTP_PRSCTP_SUPPORTED:
1100 stcb->asoc.peer_supports_prsctp = 0;
1102 case SCTP_SUPPORTED_CHUNK_EXT:
1104 /* draft-ietf-tsvwg-addip-sctp */
1105 case SCTP_HAS_NAT_SUPPORT:
1106 stcb->asoc.peer_supports_nat = 0;
1108 case SCTP_ADD_IP_ADDRESS:
1109 case SCTP_DEL_IP_ADDRESS:
1110 case SCTP_SET_PRIM_ADDR:
1111 stcb->asoc.peer_supports_asconf = 0;
1113 case SCTP_SUCCESS_REPORT:
1114 case SCTP_ERROR_CAUSE_IND:
1115 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1116 SCTPDBG(SCTP_DEBUG_INPUT2,
1117 "Turning off ASCONF to this strange peer\n");
1118 stcb->asoc.peer_supports_asconf = 0;
1121 SCTPDBG(SCTP_DEBUG_INPUT2,
1122 "Peer does not support param type %d(%x)??\n",
1123 pbad->param_type, (uint32_t) pbad->param_type);
1129 sctp_handle_error(struct sctp_chunkhdr *ch,
1130 struct sctp_tcb *stcb, struct sctp_nets *net)
1133 struct sctp_paramhdr *phdr;
1134 uint16_t error, error_type;
1136 struct sctp_association *asoc;
1139 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1144 /* parse through all of the errors and process */
1146 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1147 sizeof(struct sctp_chunkhdr));
1148 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1150 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1151 /* Process an Error Cause */
1152 error_type = ntohs(phdr->param_type);
1153 error_len = ntohs(phdr->param_length);
1154 if ((error_len > chklen) || (error_len == 0)) {
1155 /* invalid param length for this param */
1156 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1161 /* report the first error cause */
1164 switch (error_type) {
1165 case SCTP_CAUSE_INVALID_STREAM:
1166 case SCTP_CAUSE_MISSING_PARAM:
1167 case SCTP_CAUSE_INVALID_PARAM:
1168 case SCTP_CAUSE_NO_USER_DATA:
1169 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1172 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1173 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1175 if (sctp_handle_nat_colliding_state(stcb)) {
1179 case SCTP_CAUSE_NAT_MISSING_STATE:
1180 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1182 if (sctp_handle_nat_missing_state(stcb, net)) {
1186 case SCTP_CAUSE_STALE_COOKIE:
1188 * We only act if we have echoed a cookie and are
1191 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1194 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1195 /* Save the time doubled */
1196 asoc->cookie_preserve_req = ntohl(*p) << 1;
1197 asoc->stale_cookie_count++;
1198 if (asoc->stale_cookie_count >
1199 asoc->max_init_times) {
1200 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1201 /* now free the asoc */
1202 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1203 so = SCTP_INP_SO(stcb->sctp_ep);
1204 atomic_add_int(&stcb->asoc.refcnt, 1);
1205 SCTP_TCB_UNLOCK(stcb);
1206 SCTP_SOCKET_LOCK(so, 1);
1207 SCTP_TCB_LOCK(stcb);
1208 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1210 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1211 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1212 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1213 SCTP_SOCKET_UNLOCK(so, 1);
1217 /* blast back to INIT state */
1218 sctp_toss_old_cookies(stcb, &stcb->asoc);
1219 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1220 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1221 sctp_stop_all_cookie_timers(stcb);
1222 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1225 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1227 * Nothing we can do here, we don't do hostname
1228 * addresses so if the peer does not like my IPv6
1229 * (or IPv4 for that matter) it does not matter. If
1230 * they don't support that type of address, they can
1231 * NOT possibly get that packet type... i.e. with no
1232 * IPv6 you can't recieve a IPv6 packet. so we can
1233 * safely ignore this one. If we ever added support
1234 * for HOSTNAME Addresses, then we would need to do
1238 case SCTP_CAUSE_UNRECOG_CHUNK:
1239 sctp_process_unrecog_chunk(stcb, phdr, net);
1241 case SCTP_CAUSE_UNRECOG_PARAM:
1242 sctp_process_unrecog_param(stcb, phdr);
1244 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1246 * We ignore this since the timer will drive out a
1247 * new cookie anyway and there timer will drive us
1248 * to send a SHUTDOWN_COMPLETE. We can't send one
1249 * here since we don't have their tag.
1252 case SCTP_CAUSE_DELETING_LAST_ADDR:
1253 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1254 case SCTP_CAUSE_DELETING_SRC_ADDR:
1256 * We should NOT get these here, but in a
1259 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1262 case SCTP_CAUSE_OUT_OF_RESC:
1264 * And what, pray tell do we do with the fact that
1265 * the peer is out of resources? Not really sure we
1266 * could do anything but abort. I suspect this
1267 * should have came WITH an abort instead of in a
1272 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1276 adjust = SCTP_SIZE32(error_len);
1278 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1280 sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, error, ch, SCTP_SO_NOT_LOCKED);
1285 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1286 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1287 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1288 struct sctp_nets *net, int *abort_no_unlock,
1289 uint8_t use_mflowid, uint32_t mflowid,
1292 struct sctp_init_ack *init_ack;
1293 struct mbuf *op_err;
1295 SCTPDBG(SCTP_DEBUG_INPUT2,
1296 "sctp_handle_init_ack: handling INIT-ACK\n");
1299 SCTPDBG(SCTP_DEBUG_INPUT2,
1300 "sctp_handle_init_ack: TCB is null\n");
1303 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1304 /* Invalid length */
1305 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1306 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1307 src, dst, sh, op_err,
1308 use_mflowid, mflowid,
1310 *abort_no_unlock = 1;
1313 init_ack = &cp->init;
1314 /* validate parameters */
1315 if (init_ack->initiate_tag == 0) {
1316 /* protocol error... send an abort */
1317 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1318 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1319 src, dst, sh, op_err,
1320 use_mflowid, mflowid,
1322 *abort_no_unlock = 1;
1325 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1326 /* protocol error... send an abort */
1327 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1328 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1329 src, dst, sh, op_err,
1330 use_mflowid, mflowid,
1332 *abort_no_unlock = 1;
1335 if (init_ack->num_inbound_streams == 0) {
1336 /* protocol error... send an abort */
1337 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1338 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1339 src, dst, sh, op_err,
1340 use_mflowid, mflowid,
1342 *abort_no_unlock = 1;
1345 if (init_ack->num_outbound_streams == 0) {
1346 /* protocol error... send an abort */
1347 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1348 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1349 src, dst, sh, op_err,
1350 use_mflowid, mflowid,
1352 *abort_no_unlock = 1;
1355 /* process according to association state... */
1356 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1357 case SCTP_STATE_COOKIE_WAIT:
1358 /* this is the expected state for this chunk */
1359 /* process the INIT-ACK parameters */
1360 if (stcb->asoc.primary_destination->dest_state &
1361 SCTP_ADDR_UNCONFIRMED) {
1363 * The primary is where we sent the INIT, we can
1364 * always consider it confirmed when the INIT-ACK is
1365 * returned. Do this before we load addresses
1368 stcb->asoc.primary_destination->dest_state &=
1369 ~SCTP_ADDR_UNCONFIRMED;
1370 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1371 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1373 if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1374 net, abort_no_unlock,
1375 use_mflowid, mflowid,
1377 /* error in parsing parameters */
1380 /* update our state */
1381 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1382 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1384 /* reset the RTO calc */
1385 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1386 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1387 stcb->asoc.overall_error_count,
1389 SCTP_FROM_SCTP_INPUT,
1392 stcb->asoc.overall_error_count = 0;
1393 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1395 * collapse the init timer back in case of a exponential
1398 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1401 * the send at the end of the inbound data processing will
1402 * cause the cookie to be sent
1405 case SCTP_STATE_SHUTDOWN_SENT:
1406 /* incorrect state... discard */
1408 case SCTP_STATE_COOKIE_ECHOED:
1409 /* incorrect state... discard */
1411 case SCTP_STATE_OPEN:
1412 /* incorrect state... discard */
1414 case SCTP_STATE_EMPTY:
1415 case SCTP_STATE_INUSE:
1417 /* incorrect state... discard */
1421 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1425 static struct sctp_tcb *
1426 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1427 struct sockaddr *src, struct sockaddr *dst,
1428 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1429 struct sctp_inpcb *inp, struct sctp_nets **netp,
1430 struct sockaddr *init_src, int *notification,
1431 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1432 uint8_t use_mflowid, uint32_t mflowid,
1433 uint32_t vrf_id, uint16_t port);
1437 * handle a state cookie for an existing association m: input packet mbuf
1438 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1439 * "split" mbuf and the cookie signature does not exist offset: offset into
1440 * mbuf to the cookie-echo chunk
1442 static struct sctp_tcb *
1443 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1444 struct sockaddr *src, struct sockaddr *dst,
1445 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1446 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1447 struct sockaddr *init_src, int *notification,
1448 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1449 uint8_t use_mflowid, uint32_t mflowid,
1450 uint32_t vrf_id, uint16_t port)
1452 struct sctp_association *asoc;
1453 struct sctp_init_chunk *init_cp, init_buf;
1454 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1455 struct sctp_nets *net;
1456 struct mbuf *op_err;
1457 struct sctp_paramhdr *ph;
1458 int init_offset, initack_offset, i;
1464 /* I know that the TCB is non-NULL from the caller */
1466 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1467 if (asoc->cookie_how[how_indx] == 0)
1470 if (how_indx < sizeof(asoc->cookie_how)) {
1471 asoc->cookie_how[how_indx] = 1;
1473 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1474 /* SHUTDOWN came in after sending INIT-ACK */
1475 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1476 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1477 0, M_DONTWAIT, 1, MT_DATA);
1478 if (op_err == NULL) {
1483 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1484 ph = mtod(op_err, struct sctp_paramhdr *);
1485 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
1486 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1487 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1488 use_mflowid, mflowid,
1490 if (how_indx < sizeof(asoc->cookie_how))
1491 asoc->cookie_how[how_indx] = 2;
1495 * find and validate the INIT chunk in the cookie (peer's info) the
1496 * INIT should start after the cookie-echo header struct (chunk
1497 * header, state cookie header struct)
1499 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1501 init_cp = (struct sctp_init_chunk *)
1502 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1503 (uint8_t *) & init_buf);
1504 if (init_cp == NULL) {
1505 /* could not pull a INIT chunk in cookie */
1508 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1512 * find and validate the INIT-ACK chunk in the cookie (my info) the
1513 * INIT-ACK follows the INIT chunk
1515 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1516 initack_cp = (struct sctp_init_ack_chunk *)
1517 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1518 (uint8_t *) & initack_buf);
1519 if (initack_cp == NULL) {
1520 /* could not pull INIT-ACK chunk in cookie */
1523 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1526 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1527 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1529 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1530 * to get into the OPEN state
1532 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1534 * Opps, this means that we somehow generated two vtag's
1535 * the same. I.e. we did:
1537 * <---INIT(tag=a)------
1538 * ----INIT-ACK(tag=t)-->
1539 * ----INIT(tag=t)------> *1
1540 * <---INIT-ACK(tag=a)---
1541 * <----CE(tag=t)------------- *2
1543 * At point *1 we should be generating a different
1544 * tag t'. Which means we would throw away the CE and send
1545 * ours instead. Basically this is case C (throw away side).
1547 if (how_indx < sizeof(asoc->cookie_how))
1548 asoc->cookie_how[how_indx] = 17;
1552 switch SCTP_GET_STATE
1554 case SCTP_STATE_COOKIE_WAIT:
1555 case SCTP_STATE_COOKIE_ECHOED:
1557 * INIT was sent but got a COOKIE_ECHO with the
1558 * correct tags... just accept it...but we must
1559 * process the init so that we can make sure we have
1560 * the right seq no's.
1562 /* First we must process the INIT !! */
1563 retval = sctp_process_init(init_cp, stcb);
1565 if (how_indx < sizeof(asoc->cookie_how))
1566 asoc->cookie_how[how_indx] = 3;
1569 /* we have already processed the INIT so no problem */
1570 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1571 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1572 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1573 /* update current state */
1574 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1575 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1577 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1579 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1580 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1581 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1582 stcb->sctp_ep, stcb, asoc->primary_destination);
1584 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1585 sctp_stop_all_cookie_timers(stcb);
1586 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1587 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1588 (inp->sctp_socket->so_qlimit == 0)
1590 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1595 * Here is where collision would go if we
1596 * did a connect() and instead got a
1597 * init/init-ack/cookie done before the
1598 * init-ack came back..
1600 stcb->sctp_ep->sctp_flags |=
1601 SCTP_PCB_FLAGS_CONNECTED;
1602 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1603 so = SCTP_INP_SO(stcb->sctp_ep);
1604 atomic_add_int(&stcb->asoc.refcnt, 1);
1605 SCTP_TCB_UNLOCK(stcb);
1606 SCTP_SOCKET_LOCK(so, 1);
1607 SCTP_TCB_LOCK(stcb);
1608 atomic_add_int(&stcb->asoc.refcnt, -1);
1609 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1610 SCTP_SOCKET_UNLOCK(so, 1);
1614 soisconnected(stcb->sctp_socket);
1615 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1616 SCTP_SOCKET_UNLOCK(so, 1);
1619 /* notify upper layer */
1620 *notification = SCTP_NOTIFY_ASSOC_UP;
1622 * since we did not send a HB make sure we don't
1625 net->hb_responded = 1;
1626 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1627 &cookie->time_entered,
1628 sctp_align_unsafe_makecopy,
1629 SCTP_RTT_FROM_NON_DATA);
1631 if (stcb->asoc.sctp_autoclose_ticks &&
1632 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1633 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1639 * we're in the OPEN state (or beyond), so peer must
1640 * have simply lost the COOKIE-ACK
1644 sctp_stop_all_cookie_timers(stcb);
1646 * We ignore the return code here.. not sure if we should
1647 * somehow abort.. but we do have an existing asoc. This
1648 * really should not fail.
1650 if (sctp_load_addresses_from_init(stcb, m,
1651 init_offset + sizeof(struct sctp_init_chunk),
1652 initack_offset, src, dst, init_src)) {
1653 if (how_indx < sizeof(asoc->cookie_how))
1654 asoc->cookie_how[how_indx] = 4;
1657 /* respond with a COOKIE-ACK */
1658 sctp_toss_old_cookies(stcb, asoc);
1659 sctp_send_cookie_ack(stcb);
1660 if (how_indx < sizeof(asoc->cookie_how))
1661 asoc->cookie_how[how_indx] = 5;
1664 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1665 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1666 cookie->tie_tag_my_vtag == 0 &&
1667 cookie->tie_tag_peer_vtag == 0) {
1669 * case C in Section 5.2.4 Table 2: XMOO silently discard
1671 if (how_indx < sizeof(asoc->cookie_how))
1672 asoc->cookie_how[how_indx] = 6;
1676 * If nat support, and the below and stcb is established, send back
1677 * a ABORT(colliding state) if we are established.
1679 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1680 (asoc->peer_supports_nat) &&
1681 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1682 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1683 (asoc->peer_vtag == 0)))) {
1685 * Special case - Peer's support nat. We may have two init's
1686 * that we gave out the same tag on since one was not
1687 * established.. i.e. we get INIT from host-1 behind the nat
1688 * and we respond tag-a, we get a INIT from host-2 behind
1689 * the nat and we get tag-a again. Then we bring up host-1
1690 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1691 * Now we have colliding state. We must send an abort here
1692 * with colliding state indication.
1694 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1695 0, M_DONTWAIT, 1, MT_DATA);
1696 if (op_err == NULL) {
1700 /* pre-reserve some space */
1702 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
1704 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
1706 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
1707 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1709 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1710 ph = mtod(op_err, struct sctp_paramhdr *);
1711 ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE);
1712 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1713 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1714 use_mflowid, mflowid,
1718 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1719 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1720 (asoc->peer_vtag == 0))) {
1722 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1723 * should be ok, re-accept peer info
1725 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1727 * Extension of case C. If we hit this, then the
1728 * random number generator returned the same vtag
1729 * when we first sent our INIT-ACK and when we later
1730 * sent our INIT. The side with the seq numbers that
1731 * are different will be the one that normnally
1732 * would have hit case C. This in effect "extends"
1733 * our vtags in this collision case to be 64 bits.
1734 * The same collision could occur aka you get both
1735 * vtag and seq number the same twice in a row.. but
1736 * is much less likely. If it did happen then we
1737 * would proceed through and bring up the assoc.. we
1738 * may end up with the wrong stream setup however..
1739 * which would be bad.. but there is no way to
1740 * tell.. until we send on a stream that does not
1743 if (how_indx < sizeof(asoc->cookie_how))
1744 asoc->cookie_how[how_indx] = 7;
1748 if (how_indx < sizeof(asoc->cookie_how))
1749 asoc->cookie_how[how_indx] = 8;
1750 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1751 sctp_stop_all_cookie_timers(stcb);
1753 * since we did not send a HB make sure we don't double
1756 net->hb_responded = 1;
1757 if (stcb->asoc.sctp_autoclose_ticks &&
1758 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1759 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1762 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1763 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1765 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1767 * Ok the peer probably discarded our data (if we
1768 * echoed a cookie+data). So anything on the
1769 * sent_queue should be marked for retransmit, we
1770 * may not get something to kick us so it COULD
1771 * still take a timeout to move these.. but it can't
1772 * hurt to mark them.
1774 struct sctp_tmit_chunk *chk;
1776 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1777 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1778 chk->sent = SCTP_DATAGRAM_RESEND;
1779 sctp_flight_size_decrease(chk);
1780 sctp_total_flight_decrease(stcb, chk);
1781 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1787 /* process the INIT info (peer's info) */
1788 retval = sctp_process_init(init_cp, stcb);
1790 if (how_indx < sizeof(asoc->cookie_how))
1791 asoc->cookie_how[how_indx] = 9;
1794 if (sctp_load_addresses_from_init(stcb, m,
1795 init_offset + sizeof(struct sctp_init_chunk),
1796 initack_offset, src, dst, init_src)) {
1797 if (how_indx < sizeof(asoc->cookie_how))
1798 asoc->cookie_how[how_indx] = 10;
1801 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1802 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1803 *notification = SCTP_NOTIFY_ASSOC_UP;
1805 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1806 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1807 (inp->sctp_socket->so_qlimit == 0)) {
1808 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1812 stcb->sctp_ep->sctp_flags |=
1813 SCTP_PCB_FLAGS_CONNECTED;
1814 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1815 so = SCTP_INP_SO(stcb->sctp_ep);
1816 atomic_add_int(&stcb->asoc.refcnt, 1);
1817 SCTP_TCB_UNLOCK(stcb);
1818 SCTP_SOCKET_LOCK(so, 1);
1819 SCTP_TCB_LOCK(stcb);
1820 atomic_add_int(&stcb->asoc.refcnt, -1);
1821 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1822 SCTP_SOCKET_UNLOCK(so, 1);
1826 soisconnected(stcb->sctp_socket);
1827 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1828 SCTP_SOCKET_UNLOCK(so, 1);
1831 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1832 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1834 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1835 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1836 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1837 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1839 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1841 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1842 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1843 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1844 stcb->sctp_ep, stcb, asoc->primary_destination);
1846 sctp_stop_all_cookie_timers(stcb);
1847 sctp_toss_old_cookies(stcb, asoc);
1848 sctp_send_cookie_ack(stcb);
1851 * only if we have retrans set do we do this. What
1852 * this call does is get only the COOKIE-ACK out and
1853 * then when we return the normal call to
1854 * sctp_chunk_output will get the retrans out behind
1857 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1859 if (how_indx < sizeof(asoc->cookie_how))
1860 asoc->cookie_how[how_indx] = 11;
1864 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1865 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1866 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1867 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1868 cookie->tie_tag_peer_vtag != 0) {
1869 struct sctpasochead *head;
1871 if (asoc->peer_supports_nat) {
1873 * This is a gross gross hack. just call the
1874 * cookie_new code since we are allowing a duplicate
1875 * association. I hope this works...
1877 return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
1878 sh, cookie, cookie_len,
1879 inp, netp, init_src, notification,
1880 auth_skipped, auth_offset, auth_len,
1881 use_mflowid, mflowid,
1885 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1888 if (how_indx < sizeof(asoc->cookie_how))
1889 asoc->cookie_how[how_indx] = 12;
1890 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1891 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1893 /* notify upper layer */
1894 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1895 atomic_add_int(&stcb->asoc.refcnt, 1);
1896 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1897 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1898 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1899 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1901 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1902 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1903 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1904 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1906 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1907 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1908 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1909 stcb->sctp_ep, stcb, asoc->primary_destination);
1911 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1912 /* move to OPEN state, if not in SHUTDOWN_SENT */
1913 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1915 asoc->pre_open_streams =
1916 ntohs(initack_cp->init.num_outbound_streams);
1917 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1918 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1919 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1921 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1923 asoc->str_reset_seq_in = asoc->init_seq_number;
1925 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1926 if (asoc->mapping_array) {
1927 memset(asoc->mapping_array, 0,
1928 asoc->mapping_array_size);
1930 if (asoc->nr_mapping_array) {
1931 memset(asoc->nr_mapping_array, 0,
1932 asoc->mapping_array_size);
1934 SCTP_TCB_UNLOCK(stcb);
1935 SCTP_INP_INFO_WLOCK();
1936 SCTP_INP_WLOCK(stcb->sctp_ep);
1937 SCTP_TCB_LOCK(stcb);
1938 atomic_add_int(&stcb->asoc.refcnt, -1);
1939 /* send up all the data */
1940 SCTP_TCB_SEND_LOCK(stcb);
1942 sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_NOT_LOCKED);
1943 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1944 stcb->asoc.strmout[i].stream_no = i;
1945 stcb->asoc.strmout[i].next_sequence_sent = 0;
1946 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1948 /* process the INIT-ACK info (my info) */
1949 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1950 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1952 /* pull from vtag hash */
1953 LIST_REMOVE(stcb, sctp_asocs);
1954 /* re-insert to new vtag position */
1955 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1956 SCTP_BASE_INFO(hashasocmark))];
1958 * put it in the bucket in the vtag hash of assoc's for the
1961 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1963 /* process the INIT info (peer's info) */
1964 SCTP_TCB_SEND_UNLOCK(stcb);
1965 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1966 SCTP_INP_INFO_WUNLOCK();
1968 retval = sctp_process_init(init_cp, stcb);
1970 if (how_indx < sizeof(asoc->cookie_how))
1971 asoc->cookie_how[how_indx] = 13;
1976 * since we did not send a HB make sure we don't double
1979 net->hb_responded = 1;
1981 if (sctp_load_addresses_from_init(stcb, m,
1982 init_offset + sizeof(struct sctp_init_chunk),
1983 initack_offset, src, dst, init_src)) {
1984 if (how_indx < sizeof(asoc->cookie_how))
1985 asoc->cookie_how[how_indx] = 14;
1989 /* respond with a COOKIE-ACK */
1990 sctp_stop_all_cookie_timers(stcb);
1991 sctp_toss_old_cookies(stcb, asoc);
1992 sctp_send_cookie_ack(stcb);
1993 if (how_indx < sizeof(asoc->cookie_how))
1994 asoc->cookie_how[how_indx] = 15;
1998 if (how_indx < sizeof(asoc->cookie_how))
1999 asoc->cookie_how[how_indx] = 16;
2000 /* all other cases... */
2006 * handle a state cookie for a new association m: input packet mbuf chain--
2007 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
2008 * and the cookie signature does not exist offset: offset into mbuf to the
2009 * cookie-echo chunk length: length of the cookie chunk to: where the init
2010 * was from returns a new TCB
2012 static struct sctp_tcb *
2013 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
2014 struct sockaddr *src, struct sockaddr *dst,
2015 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
2016 struct sctp_inpcb *inp, struct sctp_nets **netp,
2017 struct sockaddr *init_src, int *notification,
2018 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2019 uint8_t use_mflowid, uint32_t mflowid,
2020 uint32_t vrf_id, uint16_t port)
2022 struct sctp_tcb *stcb;
2023 struct sctp_init_chunk *init_cp, init_buf;
2024 struct sctp_init_ack_chunk *initack_cp, initack_buf;
2025 struct sockaddr_storage sa_store;
2026 struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
2027 struct sctp_association *asoc;
2028 int init_offset, initack_offset, initack_limit;
2031 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
2034 struct sockaddr_in *sin;
2038 struct sockaddr_in6 *sin6;
2041 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2044 so = SCTP_INP_SO(inp);
2048 * find and validate the INIT chunk in the cookie (peer's info) the
2049 * INIT should start after the cookie-echo header struct (chunk
2050 * header, state cookie header struct)
2052 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2053 init_cp = (struct sctp_init_chunk *)
2054 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2055 (uint8_t *) & init_buf);
2056 if (init_cp == NULL) {
2057 /* could not pull a INIT chunk in cookie */
2058 SCTPDBG(SCTP_DEBUG_INPUT1,
2059 "process_cookie_new: could not pull INIT chunk hdr\n");
2062 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2063 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2066 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2068 * find and validate the INIT-ACK chunk in the cookie (my info) the
2069 * INIT-ACK follows the INIT chunk
2071 initack_cp = (struct sctp_init_ack_chunk *)
2072 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2073 (uint8_t *) & initack_buf);
2074 if (initack_cp == NULL) {
2075 /* could not pull INIT-ACK chunk in cookie */
2076 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2079 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2083 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2084 * "initack_limit" value. This is because the chk_length field
2085 * includes the length of the cookie, but the cookie is omitted when
2086 * the INIT and INIT_ACK are tacked onto the cookie...
2088 initack_limit = offset + cookie_len;
2091 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2096 * Here we do a trick, we set in NULL for the proc/thread argument.
2097 * We do this since in effect we only use the p argument when the
2098 * socket is unbound and we must do an implicit bind. Since we are
2099 * getting a cookie, we cannot be unbound.
2101 stcb = sctp_aloc_assoc(inp, init_src, &error,
2102 ntohl(initack_cp->init.initiate_tag), vrf_id,
2103 (struct thread *)NULL
2106 struct mbuf *op_err;
2108 /* memory problem? */
2109 SCTPDBG(SCTP_DEBUG_INPUT1,
2110 "process_cookie_new: no room for another TCB!\n");
2111 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2113 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2114 src, dst, sh, op_err,
2115 use_mflowid, mflowid,
2119 /* get the correct sctp_nets */
2121 *netp = sctp_findnet(stcb, init_src);
2124 /* get scope variables out of cookie */
2125 asoc->ipv4_local_scope = cookie->ipv4_scope;
2126 asoc->site_scope = cookie->site_scope;
2127 asoc->local_scope = cookie->local_scope;
2128 asoc->loopback_scope = cookie->loopback_scope;
2130 if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2131 (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2132 struct mbuf *op_err;
2135 * Houston we have a problem. The EP changed while the
2136 * cookie was in flight. Only recourse is to abort the
2139 atomic_add_int(&stcb->asoc.refcnt, 1);
2140 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2141 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2142 src, dst, sh, op_err,
2143 use_mflowid, mflowid,
2145 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2146 SCTP_TCB_UNLOCK(stcb);
2147 SCTP_SOCKET_LOCK(so, 1);
2148 SCTP_TCB_LOCK(stcb);
2150 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2151 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2152 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2153 SCTP_SOCKET_UNLOCK(so, 1);
2155 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2158 /* process the INIT-ACK info (my info) */
2159 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2160 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2161 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2162 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2163 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2164 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2165 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2166 asoc->str_reset_seq_in = asoc->init_seq_number;
2168 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2170 /* process the INIT info (peer's info) */
2172 retval = sctp_process_init(init_cp, stcb);
2176 atomic_add_int(&stcb->asoc.refcnt, 1);
2177 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2178 SCTP_TCB_UNLOCK(stcb);
2179 SCTP_SOCKET_LOCK(so, 1);
2180 SCTP_TCB_LOCK(stcb);
2182 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2183 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2184 SCTP_SOCKET_UNLOCK(so, 1);
2186 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2189 /* load all addresses */
2190 if (sctp_load_addresses_from_init(stcb, m,
2191 init_offset + sizeof(struct sctp_init_chunk), initack_offset,
2192 src, dst, init_src)) {
2193 atomic_add_int(&stcb->asoc.refcnt, 1);
2194 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2195 SCTP_TCB_UNLOCK(stcb);
2196 SCTP_SOCKET_LOCK(so, 1);
2197 SCTP_TCB_LOCK(stcb);
2199 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
2200 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2201 SCTP_SOCKET_UNLOCK(so, 1);
2203 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2207 * verify any preceding AUTH chunk that was skipped
2209 /* pull the local authentication parameters from the cookie/init-ack */
2210 sctp_auth_get_cookie_params(stcb, m,
2211 initack_offset + sizeof(struct sctp_init_ack_chunk),
2212 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2214 struct sctp_auth_chunk *auth;
2216 auth = (struct sctp_auth_chunk *)
2217 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2218 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2219 /* auth HMAC failed, dump the assoc and packet */
2220 SCTPDBG(SCTP_DEBUG_AUTH1,
2221 "COOKIE-ECHO: AUTH failed\n");
2222 atomic_add_int(&stcb->asoc.refcnt, 1);
2223 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2224 SCTP_TCB_UNLOCK(stcb);
2225 SCTP_SOCKET_LOCK(so, 1);
2226 SCTP_TCB_LOCK(stcb);
2228 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2229 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2230 SCTP_SOCKET_UNLOCK(so, 1);
2232 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2235 /* remaining chunks checked... good to go */
2236 stcb->asoc.authenticated = 1;
2239 /* update current state */
2240 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2241 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2242 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2243 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2244 stcb->sctp_ep, stcb, asoc->primary_destination);
2246 sctp_stop_all_cookie_timers(stcb);
2247 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2248 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2251 * if we're doing ASCONFs, check to see if we have any new local
2252 * addresses that need to get added to the peer (eg. addresses
2253 * changed while cookie echo in flight). This needs to be done
2254 * after we go to the OPEN state to do the correct asconf
2255 * processing. else, make sure we have the correct addresses in our
2259 /* warning, we re-use sin, sin6, sa_store here! */
2260 /* pull in local_address (our "from" address) */
2261 switch (cookie->laddr_type) {
2263 case SCTP_IPV4_ADDRESS:
2264 /* source addr is IPv4 */
2265 sin = (struct sockaddr_in *)initack_src;
2266 memset(sin, 0, sizeof(*sin));
2267 sin->sin_family = AF_INET;
2268 sin->sin_len = sizeof(struct sockaddr_in);
2269 sin->sin_addr.s_addr = cookie->laddress[0];
2273 case SCTP_IPV6_ADDRESS:
2274 /* source addr is IPv6 */
2275 sin6 = (struct sockaddr_in6 *)initack_src;
2276 memset(sin6, 0, sizeof(*sin6));
2277 sin6->sin6_family = AF_INET6;
2278 sin6->sin6_len = sizeof(struct sockaddr_in6);
2279 sin6->sin6_scope_id = cookie->scope_id;
2280 memcpy(&sin6->sin6_addr, cookie->laddress,
2281 sizeof(sin6->sin6_addr));
2285 atomic_add_int(&stcb->asoc.refcnt, 1);
2286 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2287 SCTP_TCB_UNLOCK(stcb);
2288 SCTP_SOCKET_LOCK(so, 1);
2289 SCTP_TCB_LOCK(stcb);
2291 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2292 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2293 SCTP_SOCKET_UNLOCK(so, 1);
2295 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2299 /* set up to notify upper layer */
2300 *notification = SCTP_NOTIFY_ASSOC_UP;
2301 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2302 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2303 (inp->sctp_socket->so_qlimit == 0)) {
2305 * This is an endpoint that called connect() how it got a
2306 * cookie that is NEW is a bit of a mystery. It must be that
2307 * the INIT was sent, but before it got there.. a complete
2308 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2309 * should have went to the other code.. not here.. oh well..
2310 * a bit of protection is worth having..
2312 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2313 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2314 atomic_add_int(&stcb->asoc.refcnt, 1);
2315 SCTP_TCB_UNLOCK(stcb);
2316 SCTP_SOCKET_LOCK(so, 1);
2317 SCTP_TCB_LOCK(stcb);
2318 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2319 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2320 SCTP_SOCKET_UNLOCK(so, 1);
2324 soisconnected(stcb->sctp_socket);
2325 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2326 SCTP_SOCKET_UNLOCK(so, 1);
2328 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2329 (inp->sctp_socket->so_qlimit)) {
2331 * We don't want to do anything with this one. Since it is
2332 * the listening guy. The timer will get started for
2333 * accepted connections in the caller.
2337 /* since we did not send a HB make sure we don't double things */
2338 if ((netp) && (*netp))
2339 (*netp)->hb_responded = 1;
2341 if (stcb->asoc.sctp_autoclose_ticks &&
2342 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2343 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2345 /* calculate the RTT */
2346 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2347 if ((netp) && (*netp)) {
2348 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2349 &cookie->time_entered, sctp_align_unsafe_makecopy,
2350 SCTP_RTT_FROM_NON_DATA);
2352 /* respond with a COOKIE-ACK */
2353 sctp_send_cookie_ack(stcb);
2356 * check the address lists for any ASCONFs that need to be sent
2357 * AFTER the cookie-ack is sent
2359 sctp_check_address_list(stcb, m,
2360 initack_offset + sizeof(struct sctp_init_ack_chunk),
2361 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2362 initack_src, cookie->local_scope, cookie->site_scope,
2363 cookie->ipv4_scope, cookie->loopback_scope);
2370 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2371 * we NEED to make sure we are not already using the vtag. If so we
2372 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2373 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2374 SCTP_BASE_INFO(hashasocmark))];
2375 LIST_FOREACH(stcb, head, sctp_asocs) {
2376 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2377 -- SEND ABORT - TRY AGAIN --
2383 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2384 * existing (non-NULL) TCB
2386 static struct mbuf *
2387 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2388 struct sockaddr *src, struct sockaddr *dst,
2389 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2390 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2391 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2392 struct sctp_tcb **locked_tcb,
2393 uint8_t use_mflowid, uint32_t mflowid,
2394 uint32_t vrf_id, uint16_t port)
2396 struct sctp_state_cookie *cookie;
2397 struct sctp_tcb *l_stcb = *stcb;
2398 struct sctp_inpcb *l_inp;
2399 struct sockaddr *to;
2400 struct sctp_pcb *ep;
2402 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2404 uint8_t cookie_ok = 0;
2405 unsigned int sig_offset, cookie_offset;
2406 unsigned int cookie_len;
2408 struct timeval time_expires;
2409 int notification = 0;
2410 struct sctp_nets *netl;
2411 int had_a_existing_tcb = 0;
2412 int send_int_conf = 0;
2415 struct sockaddr_in sin;
2419 struct sockaddr_in6 sin6;
2423 SCTPDBG(SCTP_DEBUG_INPUT2,
2424 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2426 if (inp_p == NULL) {
2429 cookie = &cp->cookie;
2430 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2431 cookie_len = ntohs(cp->ch.chunk_length);
2433 if ((cookie->peerport != sh->src_port) &&
2434 (cookie->myport != sh->dest_port) &&
2435 (cookie->my_vtag != sh->v_tag)) {
2437 * invalid ports or bad tag. Note that we always leave the
2438 * v_tag in the header in network order and when we stored
2439 * it in the my_vtag slot we also left it in network order.
2440 * This maintains the match even though it may be in the
2441 * opposite byte order of the machine :->
2445 if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2446 sizeof(struct sctp_init_chunk) +
2447 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2448 /* cookie too small */
2452 * split off the signature into its own mbuf (since it should not be
2453 * calculated in the sctp_hmac_m() call).
2455 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2456 m_sig = m_split(m, sig_offset, M_DONTWAIT);
2457 if (m_sig == NULL) {
2458 /* out of memory or ?? */
2461 #ifdef SCTP_MBUF_LOGGING
2462 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2465 for (mat = m_sig; mat; mat = SCTP_BUF_NEXT(mat)) {
2466 if (SCTP_BUF_IS_EXTENDED(mat)) {
2467 sctp_log_mb(mat, SCTP_MBUF_SPLIT);
2474 * compute the signature/digest for the cookie
2476 ep = &(*inp_p)->sctp_ep;
2479 SCTP_TCB_UNLOCK(l_stcb);
2481 SCTP_INP_RLOCK(l_inp);
2483 SCTP_TCB_LOCK(l_stcb);
2485 /* which cookie is it? */
2486 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2487 (ep->current_secret_number != ep->last_secret_number)) {
2488 /* it's the old cookie */
2489 (void)sctp_hmac_m(SCTP_HMAC,
2490 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2491 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2493 /* it's the current cookie */
2494 (void)sctp_hmac_m(SCTP_HMAC,
2495 (uint8_t *) ep->secret_key[(int)ep->current_secret_number],
2496 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2498 /* get the signature */
2499 SCTP_INP_RUNLOCK(l_inp);
2500 sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig);
2502 /* couldn't find signature */
2503 sctp_m_freem(m_sig);
2506 /* compare the received digest with the computed digest */
2507 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2508 /* try the old cookie? */
2509 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2510 (ep->current_secret_number != ep->last_secret_number)) {
2511 /* compute digest with old */
2512 (void)sctp_hmac_m(SCTP_HMAC,
2513 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2514 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2516 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2524 * Now before we continue we must reconstruct our mbuf so that
2525 * normal processing of any other chunks will work.
2531 while (SCTP_BUF_NEXT(m_at) != NULL) {
2532 m_at = SCTP_BUF_NEXT(m_at);
2534 SCTP_BUF_NEXT(m_at) = m_sig;
2537 if (cookie_ok == 0) {
2538 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2539 SCTPDBG(SCTP_DEBUG_INPUT2,
2540 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2541 (uint32_t) offset, cookie_offset, sig_offset);
2545 * check the cookie timestamps to be sure it's not stale
2547 (void)SCTP_GETTIME_TIMEVAL(&now);
2548 /* Expire time is in Ticks, so we convert to seconds */
2549 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2550 time_expires.tv_usec = cookie->time_entered.tv_usec;
2552 * TODO sctp_constants.h needs alternative time macros when _KERNEL
2555 if (timevalcmp(&now, &time_expires, >)) {
2556 /* cookie is stale! */
2557 struct mbuf *op_err;
2558 struct sctp_stale_cookie_msg *scm;
2561 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg),
2562 0, M_DONTWAIT, 1, MT_DATA);
2563 if (op_err == NULL) {
2568 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg);
2569 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
2570 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
2571 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
2572 (sizeof(uint32_t))));
2573 /* seconds to usec */
2574 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2577 tim = now.tv_usec - cookie->time_entered.tv_usec;
2578 scm->time_usec = htonl(tim);
2579 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2580 use_mflowid, mflowid,
2585 * Now we must see with the lookup address if we have an existing
2586 * asoc. This will only happen if we were in the COOKIE-WAIT state
2587 * and a INIT collided with us and somewhere the peer sent the
2588 * cookie on another address besides the single address our assoc
2589 * had for him. In this case we will have one of the tie-tags set at
2590 * least AND the address field in the cookie can be used to look it
2594 switch (cookie->addr_type) {
2596 case SCTP_IPV6_ADDRESS:
2597 memset(&sin6, 0, sizeof(sin6));
2598 sin6.sin6_family = AF_INET6;
2599 sin6.sin6_len = sizeof(sin6);
2600 sin6.sin6_port = sh->src_port;
2601 sin6.sin6_scope_id = cookie->scope_id;
2602 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2603 sizeof(sin6.sin6_addr.s6_addr));
2604 to = (struct sockaddr *)&sin6;
2608 case SCTP_IPV4_ADDRESS:
2609 memset(&sin, 0, sizeof(sin));
2610 sin.sin_family = AF_INET;
2611 sin.sin_len = sizeof(sin);
2612 sin.sin_port = sh->src_port;
2613 sin.sin_addr.s_addr = cookie->address[0];
2614 to = (struct sockaddr *)&sin;
2618 /* This should not happen */
2621 if ((*stcb == NULL) && to) {
2622 /* Yep, lets check */
2623 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2624 if (*stcb == NULL) {
2626 * We should have only got back the same inp. If we
2627 * got back a different ep we have a problem. The
2628 * original findep got back l_inp and now
2630 if (l_inp != *inp_p) {
2631 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2634 if (*locked_tcb == NULL) {
2636 * In this case we found the assoc only
2637 * after we locked the create lock. This
2638 * means we are in a colliding case and we
2639 * must make sure that we unlock the tcb if
2640 * its one of the cases where we throw away
2641 * the incoming packets.
2643 *locked_tcb = *stcb;
2646 * We must also increment the inp ref count
2647 * since the ref_count flags was set when we
2648 * did not find the TCB, now we found it
2649 * which reduces the refcount.. we must
2650 * raise it back out to balance it all :-)
2652 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2653 if ((*stcb)->sctp_ep != l_inp) {
2654 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2655 (*stcb)->sctp_ep, l_inp);
2663 cookie_len -= SCTP_SIGNATURE_SIZE;
2664 if (*stcb == NULL) {
2665 /* this is the "normal" case... get a new TCB */
2666 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2667 cookie, cookie_len, *inp_p,
2668 netp, to, ¬ification,
2669 auth_skipped, auth_offset, auth_len,
2670 use_mflowid, mflowid,
2673 /* this is abnormal... cookie-echo on existing TCB */
2674 had_a_existing_tcb = 1;
2675 *stcb = sctp_process_cookie_existing(m, iphlen, offset,
2677 cookie, cookie_len, *inp_p, *stcb, netp, to,
2678 ¬ification, auth_skipped, auth_offset, auth_len,
2679 use_mflowid, mflowid,
2683 if (*stcb == NULL) {
2684 /* still no TCB... must be bad cookie-echo */
2687 if ((*netp != NULL) && (use_mflowid != 0)) {
2688 (*netp)->flowid = mflowid;
2690 (*netp)->flowidset = 1;
2694 * Ok, we built an association so confirm the address we sent the
2697 netl = sctp_findnet(*stcb, to);
2699 * This code should in theory NOT run but
2702 /* TSNH! Huh, why do I need to add this address here? */
2703 if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2706 netl = sctp_findnet(*stcb, to);
2709 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2710 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2711 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2716 sctp_start_net_timers(*stcb);
2717 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2718 if (!had_a_existing_tcb ||
2719 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2721 * If we have a NEW cookie or the connect never
2722 * reached the connected state during collision we
2723 * must do the TCP accept thing.
2725 struct socket *so, *oso;
2726 struct sctp_inpcb *inp;
2728 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2730 * For a restart we will keep the same
2731 * socket, no need to do anything. I THINK!!
2733 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2734 if (send_int_conf) {
2735 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2736 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2740 oso = (*inp_p)->sctp_socket;
2741 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2742 SCTP_TCB_UNLOCK((*stcb));
2743 CURVNET_SET(oso->so_vnet);
2744 so = sonewconn(oso, 0
2747 SCTP_TCB_LOCK((*stcb));
2748 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2751 struct mbuf *op_err;
2753 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2754 struct socket *pcb_so;
2757 /* Too many sockets */
2758 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2759 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2760 sctp_abort_association(*inp_p, NULL, m, iphlen,
2761 src, dst, sh, op_err,
2762 use_mflowid, mflowid,
2764 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2765 pcb_so = SCTP_INP_SO(*inp_p);
2766 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2767 SCTP_TCB_UNLOCK((*stcb));
2768 SCTP_SOCKET_LOCK(pcb_so, 1);
2769 SCTP_TCB_LOCK((*stcb));
2770 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2772 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2773 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2774 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2778 inp = (struct sctp_inpcb *)so->so_pcb;
2779 SCTP_INP_INCR_REF(inp);
2781 * We add the unbound flag here so that if we get an
2782 * soabort() before we get the move_pcb done, we
2783 * will properly cleanup.
2785 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2786 SCTP_PCB_FLAGS_CONNECTED |
2787 SCTP_PCB_FLAGS_IN_TCPPOOL |
2788 SCTP_PCB_FLAGS_UNBOUND |
2789 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2790 SCTP_PCB_FLAGS_DONT_WAKE);
2791 inp->sctp_features = (*inp_p)->sctp_features;
2792 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2793 inp->sctp_socket = so;
2794 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2795 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2796 inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable;
2797 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2798 inp->sctp_context = (*inp_p)->sctp_context;
2799 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2800 inp->inp_starting_point_for_iterator = NULL;
2802 * copy in the authentication parameters from the
2805 if (inp->sctp_ep.local_hmacs)
2806 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2807 inp->sctp_ep.local_hmacs =
2808 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2809 if (inp->sctp_ep.local_auth_chunks)
2810 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2811 inp->sctp_ep.local_auth_chunks =
2812 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2815 * Now we must move it from one hash table to
2816 * another and get the tcb in the right place.
2820 * This is where the one-2-one socket is put into
2821 * the accept state waiting for the accept!
2824 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2826 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2828 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2829 SCTP_TCB_UNLOCK((*stcb));
2831 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2833 SCTP_TCB_LOCK((*stcb));
2834 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2838 * now we must check to see if we were aborted while
2839 * the move was going on and the lock/unlock
2842 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2844 * yep it was, we leave the assoc attached
2845 * to the socket since the sctp_inpcb_free()
2846 * call will send an abort for us.
2848 SCTP_INP_DECR_REF(inp);
2851 SCTP_INP_DECR_REF(inp);
2852 /* Switch over to the new guy */
2854 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2855 if (send_int_conf) {
2856 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2857 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2860 * Pull it from the incomplete queue and wake the
2863 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2864 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2865 SCTP_TCB_UNLOCK((*stcb));
2866 SCTP_SOCKET_LOCK(so, 1);
2869 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2870 SCTP_TCB_LOCK((*stcb));
2871 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2872 SCTP_SOCKET_UNLOCK(so, 1);
2878 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2880 if (send_int_conf) {
2881 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2882 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2888 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2889 struct sctp_tcb *stcb, struct sctp_nets *net)
2891 /* cp must not be used, others call this without a c-ack :-) */
2892 struct sctp_association *asoc;
2894 SCTPDBG(SCTP_DEBUG_INPUT2,
2895 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2901 sctp_stop_all_cookie_timers(stcb);
2902 /* process according to association state */
2903 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2904 /* state change only needed when I am in right state */
2905 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2906 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2907 sctp_start_net_timers(stcb);
2908 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2909 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2910 stcb->sctp_ep, stcb, asoc->primary_destination);
2914 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2915 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2916 if (asoc->overall_error_count == 0) {
2917 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2918 &asoc->time_entered, sctp_align_safe_nocopy,
2919 SCTP_RTT_FROM_NON_DATA);
2921 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2922 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2923 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2924 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2925 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2929 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2930 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2931 so = SCTP_INP_SO(stcb->sctp_ep);
2932 atomic_add_int(&stcb->asoc.refcnt, 1);
2933 SCTP_TCB_UNLOCK(stcb);
2934 SCTP_SOCKET_LOCK(so, 1);
2935 SCTP_TCB_LOCK(stcb);
2936 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2938 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2939 soisconnected(stcb->sctp_socket);
2941 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2942 SCTP_SOCKET_UNLOCK(so, 1);
2946 * since we did not send a HB make sure we don't double
2949 net->hb_responded = 1;
2951 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2953 * We don't need to do the asconf thing, nor hb or
2954 * autoclose if the socket is closed.
2958 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2962 if (stcb->asoc.sctp_autoclose_ticks &&
2963 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2964 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2965 stcb->sctp_ep, stcb, NULL);
2968 * send ASCONF if parameters are pending and ASCONFs are
2969 * allowed (eg. addresses changed when init/cookie echo were
2972 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2973 (stcb->asoc.peer_supports_asconf) &&
2974 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2975 #ifdef SCTP_TIMER_BASED_ASCONF
2976 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2977 stcb->sctp_ep, stcb,
2978 stcb->asoc.primary_destination);
2980 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2981 SCTP_ADDR_NOT_LOCKED);
2986 /* Toss the cookie if I can */
2987 sctp_toss_old_cookies(stcb, asoc);
2988 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
2989 /* Restart the timer if we have pending data */
2990 struct sctp_tmit_chunk *chk;
2992 chk = TAILQ_FIRST(&asoc->sent_queue);
2993 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
2998 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2999 struct sctp_tcb *stcb)
3001 struct sctp_nets *net;
3002 struct sctp_tmit_chunk *lchk;
3003 struct sctp_ecne_chunk bkup;
3004 uint8_t override_bit;
3005 uint32_t tsn, window_data_tsn;
3007 unsigned int pkt_cnt;
3009 len = ntohs(cp->ch.chunk_length);
3010 if ((len != sizeof(struct sctp_ecne_chunk)) &&
3011 (len != sizeof(struct old_sctp_ecne_chunk))) {
3014 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3015 /* Its the old format */
3016 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3017 bkup.num_pkts_since_cwr = htonl(1);
3020 SCTP_STAT_INCR(sctps_recvecne);
3021 tsn = ntohl(cp->tsn);
3022 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3023 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3025 window_data_tsn = stcb->asoc.sending_seq - 1;
3027 window_data_tsn = lchk->rec.data.TSN_seq;
3030 /* Find where it was sent to if possible. */
3032 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3033 if (lchk->rec.data.TSN_seq == tsn) {
3035 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3038 if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) {
3044 * What to do. A previous send of a CWR was possibly lost.
3045 * See how old it is, we may have it marked on the actual
3048 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3049 if (tsn == net->last_cwr_tsn) {
3050 /* Found him, send it off */
3056 * If we reach here, we need to send a special CWR
3057 * that says hey, we did this a long time ago and
3058 * you lost the response.
3060 net = TAILQ_FIRST(&stcb->asoc.nets);
3065 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3072 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3073 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3075 * JRS - Use the congestion control given in the pluggable
3078 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3080 * We reduce once every RTT. So we will only lower cwnd at
3081 * the next sending seq i.e. the window_data_tsn
3083 net->cwr_window_tsn = window_data_tsn;
3084 net->ecn_ce_pkt_cnt += pkt_cnt;
3085 net->lost_cnt = pkt_cnt;
3086 net->last_cwr_tsn = tsn;
3088 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3089 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3090 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3092 * Another loss in the same window update how many
3093 * marks/packets lost we have had.
3097 if (pkt_cnt > net->lost_cnt) {
3098 /* Should be the case */
3099 cnt = (pkt_cnt - net->lost_cnt);
3100 net->ecn_ce_pkt_cnt += cnt;
3102 net->lost_cnt = pkt_cnt;
3103 net->last_cwr_tsn = tsn;
3105 * Most CC functions will ignore this call, since we
3106 * are in-window yet of the initial CE the peer saw.
3108 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3112 * We always send a CWR this way if our previous one was lost our
3113 * peer will get an update, or if it is not time again to reduce we
3114 * still get the cwr to the peer. Note we set the override when we
3115 * could not find the TSN on the chunk or the destination network.
3117 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3121 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3124 * Here we get a CWR from the peer. We must look in the outqueue and
3125 * make sure that we have a covered ECNE in the control chunk part.
3128 struct sctp_tmit_chunk *chk;
3129 struct sctp_ecne_chunk *ecne;
3133 cwr_tsn = ntohl(cp->tsn);
3135 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3136 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
3137 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3140 if ((override == 0) && (chk->whoTo != net)) {
3141 /* Must be from the right src unless override is set */
3144 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3145 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3146 /* this covers this ECNE, we can remove it */
3147 stcb->asoc.ecn_echo_cnt_onq--;
3148 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3151 sctp_m_freem(chk->data);
3154 stcb->asoc.ctrl_queue_cnt--;
3155 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3156 if (override == 0) {
3164 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3165 struct sctp_tcb *stcb, struct sctp_nets *net)
3167 struct sctp_association *asoc;
3169 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3174 SCTPDBG(SCTP_DEBUG_INPUT2,
3175 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3180 /* process according to association state */
3181 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3182 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3183 SCTPDBG(SCTP_DEBUG_INPUT2,
3184 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3185 SCTP_TCB_UNLOCK(stcb);
3188 /* notify upper layer protocol */
3189 if (stcb->sctp_socket) {
3190 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3191 /* are the queues empty? they should be */
3192 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3193 !TAILQ_EMPTY(&asoc->sent_queue) ||
3194 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
3195 sctp_report_all_outbound(stcb, 0, 0, SCTP_SO_NOT_LOCKED);
3198 /* stop the timer */
3199 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
3200 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3202 SCTPDBG(SCTP_DEBUG_INPUT2,
3203 "sctp_handle_shutdown_complete: calls free-asoc\n");
3204 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3205 so = SCTP_INP_SO(stcb->sctp_ep);
3206 atomic_add_int(&stcb->asoc.refcnt, 1);
3207 SCTP_TCB_UNLOCK(stcb);
3208 SCTP_SOCKET_LOCK(so, 1);
3209 SCTP_TCB_LOCK(stcb);
3210 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3212 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
3213 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3214 SCTP_SOCKET_UNLOCK(so, 1);
3220 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3221 struct sctp_nets *net, uint8_t flg)
3223 switch (desc->chunk_type) {
3225 /* find the tsn to resend (possibly */
3228 struct sctp_tmit_chunk *tp1;
3230 tsn = ntohl(desc->tsn_ifany);
3231 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3232 if (tp1->rec.data.TSN_seq == tsn) {
3236 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) {
3244 * Do it the other way , aka without paying
3245 * attention to queue seq order.
3247 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3248 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3249 if (tp1->rec.data.TSN_seq == tsn) {
3256 SCTP_STAT_INCR(sctps_pdrptsnnf);
3258 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3261 if (((flg & SCTP_BADCRC) == 0) &&
3262 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3265 if ((stcb->asoc.peers_rwnd == 0) &&
3266 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3267 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3270 if (stcb->asoc.peers_rwnd == 0 &&
3271 (flg & SCTP_FROM_MIDDLE_BOX)) {
3272 SCTP_STAT_INCR(sctps_pdrpdizrw);
3275 ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+
3276 sizeof(struct sctp_data_chunk));
3280 for (iii = 0; iii < sizeof(desc->data_bytes);
3282 if (ddp[iii] != desc->data_bytes[iii]) {
3283 SCTP_STAT_INCR(sctps_pdrpbadd);
3291 * this guy had a RTO calculation
3292 * pending on it, cancel it
3294 if (tp1->whoTo->rto_needed == 0) {
3295 tp1->whoTo->rto_needed = 1;
3299 SCTP_STAT_INCR(sctps_pdrpmark);
3300 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3301 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3303 * mark it as if we were doing a FR, since
3304 * we will be getting gap ack reports behind
3305 * the info from the router.
3307 tp1->rec.data.doing_fast_retransmit = 1;
3309 * mark the tsn with what sequences can
3312 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3313 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3315 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
3318 /* restart the timer */
3319 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3320 stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3321 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3324 /* fix counts and things */
3325 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3326 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3327 tp1->whoTo->flight_size,
3330 tp1->rec.data.TSN_seq);
3332 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3333 sctp_flight_size_decrease(tp1);
3334 sctp_total_flight_decrease(stcb, tp1);
3336 tp1->sent = SCTP_DATAGRAM_RESEND;
3342 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3343 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3346 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3348 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3351 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3352 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3353 audit, stcb->asoc.sent_queue_retran_cnt);
3354 #ifndef SCTP_AUDITING_ENABLED
3355 stcb->asoc.sent_queue_retran_cnt = audit;
3363 struct sctp_tmit_chunk *asconf;
3365 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3367 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3372 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3373 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3374 asconf->sent = SCTP_DATAGRAM_RESEND;
3375 asconf->snd_count--;
3379 case SCTP_INITIATION:
3380 /* resend the INIT */
3381 stcb->asoc.dropped_special_cnt++;
3382 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3384 * If we can get it in, in a few attempts we do
3385 * this, otherwise we let the timer fire.
3387 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3388 stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3389 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3392 case SCTP_SELECTIVE_ACK:
3393 case SCTP_NR_SELECTIVE_ACK:
3394 /* resend the sack */
3395 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3397 case SCTP_HEARTBEAT_REQUEST:
3398 /* resend a demand HB */
3399 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3401 * Only retransmit if we KNOW we wont destroy the
3404 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3408 sctp_send_shutdown(stcb, net);
3410 case SCTP_SHUTDOWN_ACK:
3411 sctp_send_shutdown_ack(stcb, net);
3413 case SCTP_COOKIE_ECHO:
3415 struct sctp_tmit_chunk *cookie;
3418 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3420 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3425 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3426 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3427 cookie->sent = SCTP_DATAGRAM_RESEND;
3428 sctp_stop_all_cookie_timers(stcb);
3432 case SCTP_COOKIE_ACK:
3433 sctp_send_cookie_ack(stcb);
3435 case SCTP_ASCONF_ACK:
3436 /* resend last asconf ack */
3437 sctp_send_asconf_ack(stcb);
3439 case SCTP_FORWARD_CUM_TSN:
3440 send_forward_tsn(stcb, &stcb->asoc);
3442 /* can't do anything with these */
3443 case SCTP_PACKET_DROPPED:
3444 case SCTP_INITIATION_ACK: /* this should not happen */
3445 case SCTP_HEARTBEAT_ACK:
3446 case SCTP_ABORT_ASSOCIATION:
3447 case SCTP_OPERATION_ERROR:
3448 case SCTP_SHUTDOWN_COMPLETE:
3458 sctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3464 * We set things to 0xffff since this is the last delivered sequence
3465 * and we will be sending in 0 after the reset.
3468 if (number_entries) {
3469 for (i = 0; i < number_entries; i++) {
3470 temp = ntohs(list[i]);
3471 if (temp >= stcb->asoc.streamincnt) {
3474 stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff;
3478 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3479 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3482 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3486 sctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3490 if (number_entries == 0) {
3491 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3492 stcb->asoc.strmout[i].next_sequence_sent = 0;
3494 } else if (number_entries) {
3495 for (i = 0; i < number_entries; i++) {
3498 temp = ntohs(list[i]);
3499 if (temp >= stcb->asoc.streamoutcnt) {
3500 /* no such stream */
3503 stcb->asoc.strmout[temp].next_sequence_sent = 0;
3506 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3510 struct sctp_stream_reset_out_request *
3511 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3513 struct sctp_association *asoc;
3514 struct sctp_stream_reset_out_req *req;
3515 struct sctp_stream_reset_out_request *r;
3516 struct sctp_tmit_chunk *chk;
3520 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3521 asoc->stream_reset_outstanding = 0;
3524 if (stcb->asoc.str_reset == NULL) {
3525 asoc->stream_reset_outstanding = 0;
3528 chk = stcb->asoc.str_reset;
3529 if (chk->data == NULL) {
3533 /* he wants a copy of the chk pointer */
3536 clen = chk->send_size;
3537 req = mtod(chk->data, struct sctp_stream_reset_out_req *);
3539 if (ntohl(r->request_seq) == seq) {
3543 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3544 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3545 /* move to the next one, there can only be a max of two */
3546 r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len);
3547 if (ntohl(r->request_seq) == seq) {
3551 /* that seq is not here */
3556 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3558 struct sctp_association *asoc;
3559 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3561 if (stcb->asoc.str_reset == NULL) {
3566 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3567 TAILQ_REMOVE(&asoc->control_send_queue,
3571 sctp_m_freem(chk->data);
3574 asoc->ctrl_queue_cnt--;
3575 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3576 /* sa_ignore NO_NULL_CHK */
3577 stcb->asoc.str_reset = NULL;
3582 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3583 uint32_t seq, uint32_t action,
3584 struct sctp_stream_reset_response *respin)
3588 struct sctp_association *asoc = &stcb->asoc;
3589 struct sctp_tmit_chunk *chk;
3590 struct sctp_stream_reset_out_request *srparam;
3593 if (asoc->stream_reset_outstanding == 0) {
3597 if (seq == stcb->asoc.str_reset_seq_out) {
3598 srparam = sctp_find_stream_reset(stcb, seq, &chk);
3600 stcb->asoc.str_reset_seq_out++;
3601 type = ntohs(srparam->ph.param_type);
3602 lparm_len = ntohs(srparam->ph.param_length);
3603 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3604 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3605 asoc->stream_reset_out_is_outstanding = 0;
3606 if (asoc->stream_reset_outstanding)
3607 asoc->stream_reset_outstanding--;
3608 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3610 sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams);
3611 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3612 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3614 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3616 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3617 /* Answered my request */
3618 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3619 if (asoc->stream_reset_outstanding)
3620 asoc->stream_reset_outstanding--;
3621 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3622 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3623 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3624 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3625 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3626 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3628 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3629 /* Ok we now may have more streams */
3632 num_stream = stcb->asoc.strm_pending_add_size;
3633 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3635 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3637 stcb->asoc.strm_pending_add_size = 0;
3638 if (asoc->stream_reset_outstanding)
3639 asoc->stream_reset_outstanding--;
3640 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3641 /* Put the new streams into effect */
3642 stcb->asoc.streamoutcnt += num_stream;
3643 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3644 } else 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);
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_ADD_IN_STREAMS) {
3652 if (asoc->stream_reset_outstanding)
3653 asoc->stream_reset_outstanding--;
3654 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3655 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3656 SCTP_STREAM_CHANGE_DENIED);
3657 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3658 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3659 SCTP_STREAM_CHANGE_FAILED);
3661 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3663 * a) Adopt the new in tsn.
3665 * c) Adopt the new out-tsn
3667 struct sctp_stream_reset_response_tsn *resp;
3668 struct sctp_forward_tsn_chunk fwdtsn;
3671 if (respin == NULL) {
3675 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3676 resp = (struct sctp_stream_reset_response_tsn *)respin;
3677 asoc->stream_reset_outstanding--;
3678 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3679 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3680 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3681 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3685 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3686 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3687 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3689 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3690 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3691 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3693 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3694 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3696 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3697 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3699 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3700 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3701 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3702 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3703 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3704 SCTP_ASSOC_RESET_DENIED);
3706 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3707 SCTP_ASSOC_RESET_FAILED);
3710 /* get rid of the request and get the request flags */
3711 if (asoc->stream_reset_outstanding == 0) {
3712 sctp_clean_up_stream_reset(stcb);
3720 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3721 struct sctp_tmit_chunk *chk,
3722 struct sctp_stream_reset_in_request *req, int trunc)
3730 * peer wants me to send a str-reset to him for my outgoing seq's if
3733 struct sctp_association *asoc = &stcb->asoc;
3735 seq = ntohl(req->request_seq);
3736 if (asoc->str_reset_seq_in == seq) {
3737 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3738 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3739 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3741 /* Can't do it, since they exceeded our buffer size */
3742 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3743 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3744 len = ntohs(req->ph.param_length);
3745 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3746 for (i = 0; i < number_entries; i++) {
3747 temp = ntohs(req->list_of_streams[i]);
3748 req->list_of_streams[i] = temp;
3750 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3751 sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams,
3752 asoc->str_reset_seq_out,
3753 seq, (asoc->sending_seq - 1));
3754 asoc->stream_reset_out_is_outstanding = 1;
3755 asoc->str_reset = chk;
3756 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
3757 stcb->asoc.stream_reset_outstanding++;
3759 /* Can't do it, since we have sent one out */
3760 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3762 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3763 asoc->str_reset_seq_in++;
3764 } else if (asoc->str_reset_seq_in - 1 == seq) {
3765 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3766 } else if (asoc->str_reset_seq_in - 2 == seq) {
3767 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3769 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3774 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3775 struct sctp_tmit_chunk *chk,
3776 struct sctp_stream_reset_tsn_request *req)
3778 /* reset all in and out and update the tsn */
3780 * A) reset my str-seq's on in and out. B) Select a receive next,
3781 * and set cum-ack to it. Also process this selected number as a
3782 * fwd-tsn as well. C) set in the response my next sending seq.
3784 struct sctp_forward_tsn_chunk fwdtsn;
3785 struct sctp_association *asoc = &stcb->asoc;
3789 seq = ntohl(req->request_seq);
3790 if (asoc->str_reset_seq_in == seq) {
3791 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3792 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3793 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3795 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3796 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3797 fwdtsn.ch.chunk_flags = 0;
3798 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3799 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3803 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3804 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3805 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3807 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3808 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3809 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3810 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3811 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3812 atomic_add_int(&asoc->sending_seq, 1);
3813 /* save off historical data for retrans */
3814 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3815 asoc->last_sending_seq[0] = asoc->sending_seq;
3816 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3817 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3818 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3819 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3820 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3821 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3823 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3824 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3825 asoc->str_reset_seq_in++;
3826 } else if (asoc->str_reset_seq_in - 1 == seq) {
3827 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3828 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3829 } else if (asoc->str_reset_seq_in - 2 == seq) {
3830 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3831 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3833 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3839 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3840 struct sctp_tmit_chunk *chk,
3841 struct sctp_stream_reset_out_request *req, int trunc)
3844 int number_entries, len;
3845 struct sctp_association *asoc = &stcb->asoc;
3847 seq = ntohl(req->request_seq);
3849 /* now if its not a duplicate we process it */
3850 if (asoc->str_reset_seq_in == seq) {
3851 len = ntohs(req->ph.param_length);
3852 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3854 * the sender is resetting, handle the list issue.. we must
3855 * a) verify if we can do the reset, if so no problem b) If
3856 * we can't do the reset we must copy the request. c) queue
3857 * it, and setup the data in processor to trigger it off
3858 * when needed and dequeue all the queued data.
3860 tsn = ntohl(req->send_reset_at_tsn);
3862 /* move the reset action back one */
3863 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3864 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3865 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3867 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3868 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3869 /* we can do it now */
3870 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3871 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3874 * we must queue it up and thus wait for the TSN's
3875 * to arrive that are at or before tsn
3877 struct sctp_stream_reset_list *liste;
3880 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3881 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3882 siz, SCTP_M_STRESET);
3883 if (liste == NULL) {
3884 /* gak out of memory */
3885 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3886 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3890 liste->number_entries = number_entries;
3891 memcpy(&liste->req, req,
3892 (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t))));
3893 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3894 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3896 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3897 asoc->str_reset_seq_in++;
3898 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3900 * one seq back, just echo back last action since my
3901 * response was lost.
3903 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3904 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3906 * two seq back, just echo back last action since my
3907 * response was lost.
3909 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3911 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3916 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3917 struct sctp_stream_reset_add_strm *str_add)
3920 * Peer is requesting to add more streams. If its within our
3921 * max-streams we will allow it.
3923 uint32_t num_stream, i;
3925 struct sctp_association *asoc = &stcb->asoc;
3926 struct sctp_queued_to_read *ctl, *nctl;
3928 /* Get the number. */
3929 seq = ntohl(str_add->request_seq);
3930 num_stream = ntohs(str_add->number_of_streams);
3931 /* Now what would be the new total? */
3932 if (asoc->str_reset_seq_in == seq) {
3933 num_stream += stcb->asoc.streamincnt;
3934 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3935 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3936 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3937 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
3938 (num_stream > 0xffff)) {
3939 /* We must reject it they ask for to many */
3941 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3943 /* Ok, we can do that :-) */
3944 struct sctp_stream_in *oldstrm;
3946 /* save off the old */
3947 oldstrm = stcb->asoc.strmin;
3948 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
3949 (num_stream * sizeof(struct sctp_stream_in)),
3951 if (stcb->asoc.strmin == NULL) {
3952 stcb->asoc.strmin = oldstrm;
3955 /* copy off the old data */
3956 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3957 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3958 stcb->asoc.strmin[i].stream_no = i;
3959 stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered;
3960 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
3961 /* now anything on those queues? */
3962 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) {
3963 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next);
3964 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next);
3967 /* Init the new streams */
3968 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
3969 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3970 stcb->asoc.strmin[i].stream_no = i;
3971 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3972 stcb->asoc.strmin[i].delivery_started = 0;
3974 SCTP_FREE(oldstrm, SCTP_M_STRMI);
3975 /* update the size */
3976 stcb->asoc.streamincnt = num_stream;
3977 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3978 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3980 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3981 asoc->str_reset_seq_in++;
3982 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3984 * one seq back, just echo back last action since my
3985 * response was lost.
3987 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3988 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3990 * two seq back, just echo back last action since my
3991 * response was lost.
3993 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3995 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4001 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4002 struct sctp_stream_reset_add_strm *str_add)
4005 * Peer is requesting to add more streams. If its within our
4006 * max-streams we will allow it.
4008 uint16_t num_stream;
4010 struct sctp_association *asoc = &stcb->asoc;
4012 /* Get the number. */
4013 seq = ntohl(str_add->request_seq);
4014 num_stream = ntohs(str_add->number_of_streams);
4015 /* Now what would be the new total? */
4016 if (asoc->str_reset_seq_in == seq) {
4017 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4018 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4019 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4020 } else if (stcb->asoc.stream_reset_outstanding) {
4021 /* We must reject it we have something pending */
4022 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4024 /* Ok, we can do that :-) */
4027 mychk = stcb->asoc.streamoutcnt;
4028 mychk += num_stream;
4029 if (mychk < 0x10000) {
4030 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4031 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 0, 1, num_stream, 0, 1)) {
4032 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4035 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4038 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4039 asoc->str_reset_seq_in++;
4040 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4042 * one seq back, just echo back last action since my
4043 * response was lost.
4045 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4046 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4048 * two seq back, just echo back last action since my
4049 * response was lost.
4051 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4053 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4058 __attribute__((noinline))
4061 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4062 struct sctp_stream_reset_out_req *sr_req)
4064 int chk_length, param_len, ptype;
4065 struct sctp_paramhdr pstore;
4066 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4070 struct sctp_tmit_chunk *chk;
4071 struct sctp_chunkhdr *ch;
4072 struct sctp_paramhdr *ph;
4076 /* now it may be a reset or a reset-response */
4077 chk_length = ntohs(sr_req->ch.chunk_length);
4079 /* setup for adding the response */
4080 sctp_alloc_a_chunk(stcb, chk);
4084 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4085 chk->rec.chunk_id.can_take_data = 0;
4086 chk->asoc = &stcb->asoc;
4087 chk->no_fr_allowed = 0;
4088 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4089 chk->book_size_scale = 0;
4090 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4091 if (chk->data == NULL) {
4094 sctp_m_freem(chk->data);
4097 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4100 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4102 /* setup chunk parameters */
4103 chk->sent = SCTP_DATAGRAM_UNSENT;
4107 ch = mtod(chk->data, struct sctp_chunkhdr *);
4108 ch->chunk_type = SCTP_STREAM_RESET;
4109 ch->chunk_flags = 0;
4110 ch->chunk_length = htons(chk->send_size);
4111 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4112 offset += sizeof(struct sctp_chunkhdr);
4113 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) {
4114 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore);
4117 param_len = ntohs(ph->param_length);
4118 if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) {
4122 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)),
4123 (uint8_t *) & cstore);
4124 ptype = ntohs(ph->param_type);
4126 if (param_len > (int)sizeof(cstore)) {
4131 if (num_param > SCTP_MAX_RESET_PARAMS) {
4132 /* hit the max of parameters already sorry.. */
4135 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4136 struct sctp_stream_reset_out_request *req_out;
4138 req_out = (struct sctp_stream_reset_out_request *)ph;
4140 if (stcb->asoc.stream_reset_outstanding) {
4141 seq = ntohl(req_out->response_seq);
4142 if (seq == stcb->asoc.str_reset_seq_out) {
4144 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4147 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4148 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4149 struct sctp_stream_reset_add_strm *str_add;
4151 str_add = (struct sctp_stream_reset_add_strm *)ph;
4153 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4154 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4155 struct sctp_stream_reset_add_strm *str_add;
4157 str_add = (struct sctp_stream_reset_add_strm *)ph;
4159 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4160 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4161 struct sctp_stream_reset_in_request *req_in;
4164 req_in = (struct sctp_stream_reset_in_request *)ph;
4165 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4166 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4167 struct sctp_stream_reset_tsn_request *req_tsn;
4170 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4171 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4173 goto strres_nochunk;
4177 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4178 struct sctp_stream_reset_response *resp;
4181 resp = (struct sctp_stream_reset_response *)ph;
4182 seq = ntohl(resp->response_seq);
4183 result = ntohl(resp->result);
4184 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4186 goto strres_nochunk;
4191 offset += SCTP_SIZE32(param_len);
4192 chk_length -= SCTP_SIZE32(param_len);
4195 /* we have no response free the stuff */
4196 goto strres_nochunk;
4198 /* ok we have a chunk to link in */
4199 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4202 stcb->asoc.ctrl_queue_cnt++;
4207 * Handle a router or endpoints report of a packet loss, there are two ways
4208 * to handle this, either we get the whole packet and must disect it
4209 * ourselves (possibly with truncation and or corruption) or it is a summary
4210 * from a middle box that did the disectting for us.
4213 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4214 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4216 uint32_t bottle_bw, on_queue;
4220 struct sctp_chunk_desc desc;
4221 struct sctp_chunkhdr *ch;
4223 chlen = ntohs(cp->ch.chunk_length);
4224 chlen -= sizeof(struct sctp_pktdrop_chunk);
4225 /* XXX possible chlen underflow */
4228 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4229 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4231 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4232 chlen -= sizeof(struct sctphdr);
4233 /* XXX possible chlen underflow */
4234 memset(&desc, 0, sizeof(desc));
4236 trunc_len = (uint16_t) ntohs(cp->trunc_len);
4237 if (trunc_len > limit) {
4240 /* now the chunks themselves */
4241 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4242 desc.chunk_type = ch->chunk_type;
4243 /* get amount we need to move */
4244 at = ntohs(ch->chunk_length);
4245 if (at < sizeof(struct sctp_chunkhdr)) {
4246 /* corrupt chunk, maybe at the end? */
4247 SCTP_STAT_INCR(sctps_pdrpcrupt);
4250 if (trunc_len == 0) {
4251 /* we are supposed to have all of it */
4253 /* corrupt skip it */
4254 SCTP_STAT_INCR(sctps_pdrpcrupt);
4258 /* is there enough of it left ? */
4259 if (desc.chunk_type == SCTP_DATA) {
4260 if (chlen < (sizeof(struct sctp_data_chunk) +
4261 sizeof(desc.data_bytes))) {
4265 if (chlen < sizeof(struct sctp_chunkhdr)) {
4270 if (desc.chunk_type == SCTP_DATA) {
4271 /* can we get out the tsn? */
4272 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4273 SCTP_STAT_INCR(sctps_pdrpmbda);
4275 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4277 struct sctp_data_chunk *dcp;
4281 dcp = (struct sctp_data_chunk *)ch;
4282 ddp = (uint8_t *) (dcp + 1);
4283 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4284 desc.data_bytes[iii] = ddp[iii];
4286 desc.tsn_ifany = dcp->dp.tsn;
4288 /* nope we are done. */
4289 SCTP_STAT_INCR(sctps_pdrpnedat);
4293 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4294 SCTP_STAT_INCR(sctps_pdrpmbct);
4297 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4298 SCTP_STAT_INCR(sctps_pdrppdbrk);
4301 if (SCTP_SIZE32(at) > chlen) {
4304 chlen -= SCTP_SIZE32(at);
4305 if (chlen < sizeof(struct sctp_chunkhdr)) {
4306 /* done, none left */
4309 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4311 /* Now update any rwnd --- possibly */
4312 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4313 /* From a peer, we get a rwnd report */
4316 SCTP_STAT_INCR(sctps_pdrpfehos);
4318 bottle_bw = ntohl(cp->bottle_bw);
4319 on_queue = ntohl(cp->current_onq);
4320 if (bottle_bw && on_queue) {
4321 /* a rwnd report is in here */
4322 if (bottle_bw > on_queue)
4323 a_rwnd = bottle_bw - on_queue;
4328 stcb->asoc.peers_rwnd = 0;
4330 if (a_rwnd > stcb->asoc.total_flight) {
4331 stcb->asoc.peers_rwnd =
4332 a_rwnd - stcb->asoc.total_flight;
4334 stcb->asoc.peers_rwnd = 0;
4336 if (stcb->asoc.peers_rwnd <
4337 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4338 /* SWS sender side engages */
4339 stcb->asoc.peers_rwnd = 0;
4344 SCTP_STAT_INCR(sctps_pdrpfmbox);
4347 /* now middle boxes in sat networks get a cwnd bump */
4348 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4349 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4350 (stcb->asoc.sat_network)) {
4352 * This is debateable but for sat networks it makes sense
4353 * Note if a T3 timer has went off, we will prohibit any
4354 * changes to cwnd until we exit the t3 loss recovery.
4356 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4357 net, cp, &bottle_bw, &on_queue);
4362 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4363 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4364 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4365 * length of the complete packet outputs: - length: modified to remaining
4366 * length after control processing - netp: modified to new sctp_nets after
4367 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4368 * bad packet,...) otherwise return the tcb for this packet
4371 __attribute__((noinline))
4373 static struct sctp_tcb *
4374 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4375 struct sockaddr *src, struct sockaddr *dst,
4376 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4377 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4378 uint8_t use_mflowid, uint32_t mflowid,
4379 uint32_t vrf_id, uint16_t port)
4381 struct sctp_association *asoc;
4383 int num_chunks = 0; /* number of control chunks processed */
4384 uint32_t chk_length;
4386 int abort_no_unlock = 0;
4390 * How big should this be, and should it be alloc'd? Lets try the
4391 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4392 * until we get into jumbo grams and such..
4394 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4395 struct sctp_tcb *locked_tcb = stcb;
4397 uint32_t auth_offset = 0, auth_len = 0;
4398 int auth_skipped = 0;
4401 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4406 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4407 iphlen, *offset, length, stcb);
4409 /* validate chunk header length... */
4410 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4411 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4412 ntohs(ch->chunk_length));
4414 SCTP_TCB_UNLOCK(locked_tcb);
4419 * validate the verification tag
4421 vtag_in = ntohl(sh->v_tag);
4424 SCTP_TCB_LOCK_ASSERT(locked_tcb);
4426 if (ch->chunk_type == SCTP_INITIATION) {
4427 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4428 ntohs(ch->chunk_length), vtag_in);
4430 /* protocol error- silently discard... */
4431 SCTP_STAT_INCR(sctps_badvtag);
4433 SCTP_TCB_UNLOCK(locked_tcb);
4437 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4439 * If there is no stcb, skip the AUTH chunk and process
4440 * later after a stcb is found (to validate the lookup was
4443 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4445 !SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4446 /* save this chunk for later processing */
4448 auth_offset = *offset;
4449 auth_len = ntohs(ch->chunk_length);
4451 /* (temporarily) move past this chunk */
4452 *offset += SCTP_SIZE32(auth_len);
4453 if (*offset >= length) {
4454 /* no more data left in the mbuf chain */
4457 SCTP_TCB_UNLOCK(locked_tcb);
4461 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4462 sizeof(struct sctp_chunkhdr), chunk_buf);
4468 SCTP_TCB_UNLOCK(locked_tcb);
4472 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4473 goto process_control_chunks;
4476 * first check if it's an ASCONF with an unknown src addr we
4477 * need to look inside to find the association
4479 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4480 struct sctp_chunkhdr *asconf_ch = ch;
4481 uint32_t asconf_offset = 0, asconf_len = 0;
4483 /* inp's refcount may be reduced */
4484 SCTP_INP_INCR_REF(inp);
4486 asconf_offset = *offset;
4488 asconf_len = ntohs(asconf_ch->chunk_length);
4489 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4491 stcb = sctp_findassociation_ep_asconf(m,
4494 sh, &inp, netp, vrf_id);
4497 asconf_offset += SCTP_SIZE32(asconf_len);
4498 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4499 sizeof(struct sctp_chunkhdr), chunk_buf);
4500 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4503 * reduce inp's refcount if not reduced in
4504 * sctp_findassociation_ep_asconf().
4506 SCTP_INP_DECR_REF(inp);
4511 /* now go back and verify any auth chunk to be sure */
4512 if (auth_skipped && (stcb != NULL)) {
4513 struct sctp_auth_chunk *auth;
4515 auth = (struct sctp_auth_chunk *)
4516 sctp_m_getptr(m, auth_offset,
4517 auth_len, chunk_buf);
4520 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4522 /* auth HMAC failed so dump it */
4525 SCTP_TCB_UNLOCK(locked_tcb);
4529 /* remaining chunks are HMAC checked */
4530 stcb->asoc.authenticated = 1;
4535 /* no association, so it's out of the blue... */
4536 sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp,
4537 use_mflowid, mflowid,
4541 SCTP_TCB_UNLOCK(locked_tcb);
4546 /* ABORT and SHUTDOWN can use either v_tag... */
4547 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4548 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4549 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4550 if ((vtag_in == asoc->my_vtag) ||
4551 ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
4552 (vtag_in == asoc->peer_vtag))) {
4555 /* drop this packet... */
4556 SCTP_STAT_INCR(sctps_badvtag);
4558 SCTP_TCB_UNLOCK(locked_tcb);
4562 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4563 if (vtag_in != asoc->my_vtag) {
4565 * this could be a stale SHUTDOWN-ACK or the
4566 * peer never got the SHUTDOWN-COMPLETE and
4567 * is still hung; we have started a new asoc
4568 * but it won't complete until the shutdown
4572 SCTP_TCB_UNLOCK(locked_tcb);
4574 sctp_handle_ootb(m, iphlen, *offset, src, dst,
4576 use_mflowid, mflowid,
4581 /* for all other chunks, vtag must match */
4582 if (vtag_in != asoc->my_vtag) {
4583 /* invalid vtag... */
4584 SCTPDBG(SCTP_DEBUG_INPUT3,
4585 "invalid vtag: %xh, expect %xh\n",
4586 vtag_in, asoc->my_vtag);
4587 SCTP_STAT_INCR(sctps_badvtag);
4589 SCTP_TCB_UNLOCK(locked_tcb);
4595 } /* end if !SCTP_COOKIE_ECHO */
4597 * process all control chunks...
4599 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4600 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4601 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4602 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4603 /* implied cookie-ack.. we must have lost the ack */
4604 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4605 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4606 stcb->asoc.overall_error_count,
4608 SCTP_FROM_SCTP_INPUT,
4611 stcb->asoc.overall_error_count = 0;
4612 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4615 process_control_chunks:
4616 while (IS_SCTP_CONTROL(ch)) {
4617 /* validate chunk length */
4618 chk_length = ntohs(ch->chunk_length);
4619 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4620 ch->chunk_type, chk_length);
4621 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4622 if (chk_length < sizeof(*ch) ||
4623 (*offset + (int)chk_length) > length) {
4626 SCTP_TCB_UNLOCK(locked_tcb);
4630 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4632 * INIT-ACK only gets the init ack "header" portion only
4633 * because we don't have to process the peer's COOKIE. All
4634 * others get a complete chunk.
4636 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4637 (ch->chunk_type == SCTP_INITIATION)) {
4638 /* get an init-ack chunk */
4639 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4640 sizeof(struct sctp_init_ack_chunk), chunk_buf);
4644 SCTP_TCB_UNLOCK(locked_tcb);
4649 /* For cookies and all other chunks. */
4650 if (chk_length > sizeof(chunk_buf)) {
4652 * use just the size of the chunk buffer so
4653 * the front part of our chunks fit in
4654 * contiguous space up to the chunk buffer
4655 * size (508 bytes). For chunks that need to
4656 * get more than that they must use the
4657 * sctp_m_getptr() function or other means
4658 * (e.g. know how to parse mbuf chains).
4659 * Cookies do this already.
4661 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4662 (sizeof(chunk_buf) - 4),
4667 SCTP_TCB_UNLOCK(locked_tcb);
4672 /* We can fit it all */
4673 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4674 chk_length, chunk_buf);
4676 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4679 SCTP_TCB_UNLOCK(locked_tcb);
4686 /* Save off the last place we got a control from */
4688 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4690 * allow last_control to be NULL if
4691 * ASCONF... ASCONF processing will find the
4694 if ((netp != NULL) && (*netp != NULL))
4695 stcb->asoc.last_control_chunk_from = *netp;
4698 #ifdef SCTP_AUDITING_ENABLED
4699 sctp_audit_log(0xB0, ch->chunk_type);
4702 /* check to see if this chunk required auth, but isn't */
4703 if ((stcb != NULL) &&
4704 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
4705 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4706 !stcb->asoc.authenticated) {
4707 /* "silently" ignore */
4708 SCTP_STAT_INCR(sctps_recvauthmissing);
4711 switch (ch->chunk_type) {
4712 case SCTP_INITIATION:
4713 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4714 /* The INIT chunk must be the only chunk. */
4715 if ((num_chunks > 1) ||
4716 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4717 sctp_abort_association(inp, stcb, m, iphlen,
4719 use_mflowid, mflowid,
4724 /* Honor our resource limit. */
4725 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4726 struct mbuf *op_err;
4728 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
4729 sctp_abort_association(inp, stcb, m, iphlen,
4730 src, dst, sh, op_err,
4731 use_mflowid, mflowid,
4736 sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4737 (struct sctp_init_chunk *)ch, inp,
4738 stcb, &abort_no_unlock,
4739 use_mflowid, mflowid,
4742 if ((!abort_no_unlock) && (locked_tcb)) {
4743 SCTP_TCB_UNLOCK(locked_tcb);
4747 case SCTP_PAD_CHUNK:
4749 case SCTP_INITIATION_ACK:
4750 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4751 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4752 /* We are not interested anymore */
4753 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4756 if (locked_tcb != stcb) {
4758 SCTP_TCB_UNLOCK(locked_tcb);
4762 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4763 so = SCTP_INP_SO(inp);
4764 atomic_add_int(&stcb->asoc.refcnt, 1);
4765 SCTP_TCB_UNLOCK(stcb);
4766 SCTP_SOCKET_LOCK(so, 1);
4767 SCTP_TCB_LOCK(stcb);
4768 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4770 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4771 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4772 SCTP_SOCKET_UNLOCK(so, 1);
4778 /* The INIT-ACK chunk must be the only chunk. */
4779 if ((num_chunks > 1) ||
4780 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4783 SCTP_TCB_UNLOCK(locked_tcb);
4787 if ((netp) && (*netp)) {
4788 ret = sctp_handle_init_ack(m, iphlen, *offset,
4790 (struct sctp_init_ack_chunk *)ch,
4793 use_mflowid, mflowid,
4799 if (abort_no_unlock) {
4803 * Special case, I must call the output routine to
4804 * get the cookie echoed
4806 if ((stcb != NULL) && (ret == 0)) {
4807 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4810 SCTP_TCB_UNLOCK(locked_tcb);
4814 case SCTP_SELECTIVE_ACK:
4816 struct sctp_sack_chunk *sack;
4818 uint32_t a_rwnd, cum_ack;
4819 uint16_t num_seg, num_dup;
4821 int offset_seg, offset_dup;
4823 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4824 SCTP_STAT_INCR(sctps_recvsacks);
4826 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4829 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4830 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4833 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4835 * If we have sent a shutdown-ack, we will pay no
4836 * attention to a sack sent in to us since
4837 * we don't care anymore.
4841 sack = (struct sctp_sack_chunk *)ch;
4842 flags = ch->chunk_flags;
4843 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4844 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4845 num_dup = ntohs(sack->sack.num_dup_tsns);
4846 a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
4847 if (sizeof(struct sctp_sack_chunk) +
4848 num_seg * sizeof(struct sctp_gap_ack_block) +
4849 num_dup * sizeof(uint32_t) != chk_length) {
4850 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4853 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4854 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4855 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4856 cum_ack, num_seg, a_rwnd);
4857 stcb->asoc.seen_a_sack_this_pkt = 1;
4858 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4860 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4861 (stcb->asoc.saw_sack_with_frags == 0) &&
4862 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4863 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4866 * We have a SIMPLE sack having no
4867 * prior segments and data on sent
4868 * queue to be acked.. Use the
4869 * faster path sack processing. We
4870 * also allow window update sacks
4871 * with no missing segments to go
4874 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
4877 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4878 num_seg, 0, num_dup, &abort_now, flags,
4879 cum_ack, a_rwnd, ecne_seen);
4882 /* ABORT signal from sack processing */
4886 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4887 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4888 (stcb->asoc.stream_queue_cnt == 0)) {
4889 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4894 * EY - nr_sack: If the received chunk is an
4897 case SCTP_NR_SELECTIVE_ACK:
4899 struct sctp_nr_sack_chunk *nr_sack;
4901 uint32_t a_rwnd, cum_ack;
4902 uint16_t num_seg, num_nr_seg, num_dup;
4904 int offset_seg, offset_dup;
4906 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
4907 SCTP_STAT_INCR(sctps_recvsacks);
4909 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
4912 if ((stcb->asoc.sctp_nr_sack_on_off == 0) ||
4913 (stcb->asoc.peer_supports_nr_sack == 0)) {
4916 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4917 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
4920 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4922 * If we have sent a shutdown-ack, we will pay no
4923 * attention to a sack sent in to us since
4924 * we don't care anymore.
4928 nr_sack = (struct sctp_nr_sack_chunk *)ch;
4929 flags = ch->chunk_flags;
4930 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4931 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4932 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4933 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4934 a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd);
4935 if (sizeof(struct sctp_nr_sack_chunk) +
4936 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4937 num_dup * sizeof(uint32_t) != chk_length) {
4938 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4941 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4942 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4943 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4944 cum_ack, num_seg, a_rwnd);
4945 stcb->asoc.seen_a_sack_this_pkt = 1;
4946 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4947 (num_seg == 0) && (num_nr_seg == 0) &&
4948 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4949 (stcb->asoc.saw_sack_with_frags == 0) &&
4950 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4951 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
4953 * We have a SIMPLE sack having no
4954 * prior segments and data on sent
4955 * queue to be acked. Use the faster
4956 * path sack processing. We also
4957 * allow window update sacks with no
4958 * missing segments to go this way
4961 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
4962 &abort_now, ecne_seen);
4965 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4966 num_seg, num_nr_seg, num_dup, &abort_now, flags,
4967 cum_ack, a_rwnd, ecne_seen);
4970 /* ABORT signal from sack processing */
4974 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4975 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4976 (stcb->asoc.stream_queue_cnt == 0)) {
4977 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4982 case SCTP_HEARTBEAT_REQUEST:
4983 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
4984 if ((stcb) && netp && *netp) {
4985 SCTP_STAT_INCR(sctps_recvheartbeat);
4986 sctp_send_heartbeat_ack(stcb, m, *offset,
4989 /* He's alive so give him credit */
4990 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4991 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4992 stcb->asoc.overall_error_count,
4994 SCTP_FROM_SCTP_INPUT,
4997 stcb->asoc.overall_error_count = 0;
5000 case SCTP_HEARTBEAT_ACK:
5001 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
5002 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5006 SCTP_TCB_UNLOCK(locked_tcb);
5010 /* He's alive so give him credit */
5011 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5012 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5013 stcb->asoc.overall_error_count,
5015 SCTP_FROM_SCTP_INPUT,
5018 stcb->asoc.overall_error_count = 0;
5019 SCTP_STAT_INCR(sctps_recvheartbeatack);
5021 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5024 case SCTP_ABORT_ASSOCIATION:
5025 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5027 if ((stcb) && netp && *netp)
5028 sctp_handle_abort((struct sctp_abort_chunk *)ch,
5034 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5036 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5039 SCTP_TCB_UNLOCK(locked_tcb);
5043 if (netp && *netp) {
5046 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5047 stcb, *netp, &abort_flag);
5054 case SCTP_SHUTDOWN_ACK:
5055 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb);
5056 if ((stcb) && (netp) && (*netp))
5057 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5062 case SCTP_OPERATION_ERROR:
5063 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
5064 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
5069 case SCTP_COOKIE_ECHO:
5070 SCTPDBG(SCTP_DEBUG_INPUT3,
5071 "SCTP_COOKIE-ECHO, stcb %p\n", stcb);
5072 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5075 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5076 /* We are not interested anymore */
5079 SCTP_TCB_UNLOCK(stcb);
5086 * First are we accepting? We do this again here
5087 * since it is possible that a previous endpoint WAS
5088 * listening responded to a INIT-ACK and then
5089 * closed. We opened and bound.. and are now no
5093 if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
5094 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5095 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5096 struct mbuf *op_err;
5098 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
5099 sctp_abort_association(inp, stcb, m, iphlen,
5100 src, dst, sh, op_err,
5101 use_mflowid, mflowid,
5107 struct mbuf *ret_buf;
5108 struct sctp_inpcb *linp;
5117 SCTP_ASOC_CREATE_LOCK(linp);
5118 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5119 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5120 SCTP_ASOC_CREATE_UNLOCK(linp);
5126 sctp_handle_cookie_echo(m, iphlen,
5130 (struct sctp_cookie_echo_chunk *)ch,
5144 SCTP_ASOC_CREATE_UNLOCK(linp);
5146 if (ret_buf == NULL) {
5148 SCTP_TCB_UNLOCK(locked_tcb);
5150 SCTPDBG(SCTP_DEBUG_INPUT3,
5151 "GAK, null buffer\n");
5155 /* if AUTH skipped, see if it verified... */
5160 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5162 * Restart the timer if we have
5165 struct sctp_tmit_chunk *chk;
5167 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5168 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5172 case SCTP_COOKIE_ACK:
5173 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb);
5174 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5176 SCTP_TCB_UNLOCK(locked_tcb);
5180 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5181 /* We are not interested anymore */
5182 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5185 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5186 so = SCTP_INP_SO(inp);
5187 atomic_add_int(&stcb->asoc.refcnt, 1);
5188 SCTP_TCB_UNLOCK(stcb);
5189 SCTP_SOCKET_LOCK(so, 1);
5190 SCTP_TCB_LOCK(stcb);
5191 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5193 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
5194 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5195 SCTP_SOCKET_UNLOCK(so, 1);
5201 /* He's alive so give him credit */
5202 if ((stcb) && netp && *netp) {
5203 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5204 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5205 stcb->asoc.overall_error_count,
5207 SCTP_FROM_SCTP_INPUT,
5210 stcb->asoc.overall_error_count = 0;
5211 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5215 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5216 /* He's alive so give him credit */
5217 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5220 SCTP_TCB_UNLOCK(locked_tcb);
5226 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5227 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5228 stcb->asoc.overall_error_count,
5230 SCTP_FROM_SCTP_INPUT,
5233 stcb->asoc.overall_error_count = 0;
5234 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5240 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5241 /* He's alive so give him credit */
5242 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5245 SCTP_TCB_UNLOCK(locked_tcb);
5251 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5252 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5253 stcb->asoc.overall_error_count,
5255 SCTP_FROM_SCTP_INPUT,
5258 stcb->asoc.overall_error_count = 0;
5259 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5262 case SCTP_SHUTDOWN_COMPLETE:
5263 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb);
5264 /* must be first and only chunk */
5265 if ((num_chunks > 1) ||
5266 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5269 SCTP_TCB_UNLOCK(locked_tcb);
5273 if ((stcb) && netp && *netp) {
5274 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5281 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5282 /* He's alive so give him credit */
5284 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5285 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5286 stcb->asoc.overall_error_count,
5288 SCTP_FROM_SCTP_INPUT,
5291 stcb->asoc.overall_error_count = 0;
5292 sctp_handle_asconf(m, *offset, src,
5293 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5297 case SCTP_ASCONF_ACK:
5298 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5299 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5302 SCTP_TCB_UNLOCK(locked_tcb);
5307 if ((stcb) && netp && *netp) {
5308 /* He's alive so give him credit */
5309 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5310 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5311 stcb->asoc.overall_error_count,
5313 SCTP_FROM_SCTP_INPUT,
5316 stcb->asoc.overall_error_count = 0;
5317 sctp_handle_asconf_ack(m, *offset,
5318 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5319 if (abort_no_unlock)
5323 case SCTP_FORWARD_CUM_TSN:
5324 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5325 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5328 SCTP_TCB_UNLOCK(locked_tcb);
5333 /* He's alive so give him credit */
5337 stcb->asoc.overall_error_count = 0;
5338 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5339 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5340 stcb->asoc.overall_error_count,
5342 SCTP_FROM_SCTP_INPUT,
5346 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5347 /* We are not interested anymore */
5348 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5349 so = SCTP_INP_SO(inp);
5350 atomic_add_int(&stcb->asoc.refcnt, 1);
5351 SCTP_TCB_UNLOCK(stcb);
5352 SCTP_SOCKET_LOCK(so, 1);
5353 SCTP_TCB_LOCK(stcb);
5354 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5356 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
5357 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5358 SCTP_SOCKET_UNLOCK(so, 1);
5363 sctp_handle_forward_tsn(stcb,
5364 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5369 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5370 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5371 stcb->asoc.overall_error_count,
5373 SCTP_FROM_SCTP_INPUT,
5376 stcb->asoc.overall_error_count = 0;
5381 case SCTP_STREAM_RESET:
5382 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5383 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5386 SCTP_TCB_UNLOCK(locked_tcb);
5391 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5392 /* We are not interested anymore */
5393 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5394 so = SCTP_INP_SO(inp);
5395 atomic_add_int(&stcb->asoc.refcnt, 1);
5396 SCTP_TCB_UNLOCK(stcb);
5397 SCTP_SOCKET_LOCK(so, 1);
5398 SCTP_TCB_LOCK(stcb);
5399 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5401 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5402 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5403 SCTP_SOCKET_UNLOCK(so, 1);
5408 if (stcb->asoc.peer_supports_strreset == 0) {
5410 * hmm, peer should have announced this, but
5411 * we will turn it on since he is sending us
5414 stcb->asoc.peer_supports_strreset = 1;
5416 if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) {
5417 /* stop processing */
5422 case SCTP_PACKET_DROPPED:
5423 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5424 /* re-get it all please */
5425 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5428 SCTP_TCB_UNLOCK(locked_tcb);
5433 if (ch && (stcb) && netp && (*netp)) {
5434 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5436 min(chk_length, (sizeof(chunk_buf) - 4)));
5441 case SCTP_AUTHENTICATION:
5442 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5443 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
5447 /* save the first AUTH for later processing */
5448 if (auth_skipped == 0) {
5449 auth_offset = *offset;
5450 auth_len = chk_length;
5453 /* skip this chunk (temporarily) */
5456 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5457 (chk_length > (sizeof(struct sctp_auth_chunk) +
5458 SCTP_AUTH_DIGEST_LEN_MAX))) {
5461 SCTP_TCB_UNLOCK(locked_tcb);
5466 if (got_auth == 1) {
5467 /* skip this chunk... it's already auth'd */
5471 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5473 /* auth HMAC failed so dump the packet */
5477 /* remaining chunks are HMAC checked */
5478 stcb->asoc.authenticated = 1;
5484 /* it's an unknown chunk! */
5485 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5487 struct sctp_paramhdr *phd;
5489 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
5490 0, M_DONTWAIT, 1, MT_DATA);
5492 phd = mtod(mm, struct sctp_paramhdr *);
5494 * We cheat and use param type since
5495 * we did not bother to define a
5496 * error cause struct. They are the
5497 * same basic format with different
5500 phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5501 phd->param_length = htons(chk_length + sizeof(*phd));
5502 SCTP_BUF_LEN(mm) = sizeof(*phd);
5503 SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, chk_length, M_DONTWAIT);
5504 if (SCTP_BUF_NEXT(mm)) {
5505 if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(mm), SCTP_SIZE32(chk_length) - chk_length, NULL)) {
5508 #ifdef SCTP_MBUF_LOGGING
5509 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5512 for (mat = SCTP_BUF_NEXT(mm); mat; mat = SCTP_BUF_NEXT(mat)) {
5513 if (SCTP_BUF_IS_EXTENDED(mat)) {
5514 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5519 sctp_queue_op_err(stcb, mm);
5526 if ((ch->chunk_type & 0x80) == 0) {
5527 /* discard this packet */
5530 } /* else skip this bad chunk and continue... */
5532 } /* switch (ch->chunk_type) */
5536 /* get the next chunk */
5537 *offset += SCTP_SIZE32(chk_length);
5538 if (*offset >= length) {
5539 /* no more data left in the mbuf chain */
5542 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5543 sizeof(struct sctp_chunkhdr), chunk_buf);
5546 SCTP_TCB_UNLOCK(locked_tcb);
5553 if (asconf_cnt > 0 && stcb != NULL) {
5554 sctp_send_asconf_ack(stcb);
5562 __attribute__((noinline))
5565 sctp_validate_no_locks(struct sctp_inpcb *inp)
5567 struct sctp_tcb *lstcb;
5569 LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) {
5570 if (mtx_owned(&lstcb->tcb_mtx)) {
5571 panic("Own lock on stcb at return from input");
5574 if (mtx_owned(&inp->inp_create_mtx)) {
5575 panic("Own create lock on inp");
5577 if (mtx_owned(&inp->inp_mtx)) {
5578 panic("Own inp lock on inp");
5585 * common input chunk processing (v4 and v6)
5588 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5589 struct sockaddr *src, struct sockaddr *dst,
5590 struct sctphdr *sh, struct sctp_chunkhdr *ch,
5591 #if !defined(SCTP_WITH_NO_CSUM)
5592 uint8_t compute_crc,
5595 uint8_t use_mflowid, uint32_t mflowid,
5596 uint32_t vrf_id, uint16_t port)
5599 int fwd_tsn_seen = 0, data_processed = 0;
5600 struct mbuf *m = *mm;
5602 int cnt_ctrl_ready = 0;
5603 struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5604 struct sctp_tcb *stcb = NULL;
5605 struct sctp_nets *net = NULL;
5607 SCTP_STAT_INCR(sctps_recvdatagrams);
5608 #ifdef SCTP_AUDITING_ENABLED
5609 sctp_audit_log(0xE0, 1);
5610 sctp_auditing(0, inp, stcb, net);
5612 #if !defined(SCTP_WITH_NO_CSUM)
5613 if (compute_crc != 0) {
5614 uint32_t check, calc_check;
5616 check = sh->checksum;
5618 calc_check = sctp_calculate_cksum(m, iphlen);
5619 sh->checksum = check;
5620 if (calc_check != check) {
5621 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5622 calc_check, check, m, length, iphlen);
5623 stcb = sctp_findassociation_addr(m, offset, src, dst,
5624 sh, ch, &inp, &net, vrf_id);
5625 if ((net != NULL) && (port != 0)) {
5626 if (net->port == 0) {
5627 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5631 if ((net != NULL) && (use_mflowid != 0)) {
5632 net->flowid = mflowid;
5637 if ((inp != NULL) && (stcb != NULL)) {
5638 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5639 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5640 } else if ((inp != NULL) && (stcb == NULL)) {
5643 SCTP_STAT_INCR(sctps_badsum);
5644 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5649 /* Destination port of 0 is illegal, based on RFC4960. */
5650 if (sh->dest_port == 0) {
5651 SCTP_STAT_INCR(sctps_hdrops);
5654 stcb = sctp_findassociation_addr(m, offset, src, dst,
5655 sh, ch, &inp, &net, vrf_id);
5656 if ((net != NULL) && (port != 0)) {
5657 if (net->port == 0) {
5658 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5662 if ((net != NULL) && (use_mflowid != 0)) {
5663 net->flowid = mflowid;
5669 SCTP_STAT_INCR(sctps_noport);
5670 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5673 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5674 sctp_send_shutdown_complete2(src, dst, sh,
5675 use_mflowid, mflowid,
5679 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5682 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5683 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5684 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5685 (ch->chunk_type != SCTP_INIT))) {
5686 sctp_send_abort(m, iphlen, src, dst,
5688 use_mflowid, mflowid,
5693 } else if (stcb == NULL) {
5698 * I very much doubt any of the IPSEC stuff will work but I have no
5699 * idea, so I will leave it in place.
5702 switch (dst->sa_family) {
5705 if (ipsec4_in_reject(m, &inp->ip_inp.inp)) {
5706 MODULE_GLOBAL(ipsec4stat).in_polvio++;
5707 SCTP_STAT_INCR(sctps_hdrops);
5714 if (ipsec6_in_reject(m, &inp->ip_inp.inp)) {
5715 MODULE_GLOBAL(ipsec6stat).in_polvio++;
5716 SCTP_STAT_INCR(sctps_hdrops);
5726 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5727 m, iphlen, offset, length, stcb);
5729 /* always clear this before beginning a packet */
5730 stcb->asoc.authenticated = 0;
5731 stcb->asoc.seen_a_sack_this_pkt = 0;
5732 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5733 stcb, stcb->asoc.state);
5735 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5736 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5738 * If we hit here, we had a ref count
5739 * up when the assoc was aborted and the
5740 * timer is clearing out the assoc, we should
5741 * NOT respond to any packet.. its OOTB.
5743 SCTP_TCB_UNLOCK(stcb);
5745 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp,
5746 use_mflowid, mflowid,
5751 if (IS_SCTP_CONTROL(ch)) {
5752 /* process the control portion of the SCTP packet */
5753 /* sa_ignore NO_NULL_CHK */
5754 stcb = sctp_process_control(m, iphlen, &offset, length,
5756 inp, stcb, &net, &fwd_tsn_seen,
5757 use_mflowid, mflowid,
5761 * This covers us if the cookie-echo was there and
5762 * it changes our INP.
5764 inp = stcb->sctp_ep;
5765 if ((net) && (port)) {
5766 if (net->port == 0) {
5767 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5774 * no control chunks, so pre-process DATA chunks (these
5775 * checks are taken care of by control processing)
5779 * if DATA only packet, and auth is required, then punt...
5780 * can't have authenticated without any AUTH (control)
5783 if ((stcb != NULL) &&
5784 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5785 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5786 /* "silently" ignore */
5787 SCTP_STAT_INCR(sctps_recvauthmissing);
5791 /* out of the blue DATA chunk */
5792 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp,
5793 use_mflowid, mflowid,
5797 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5798 /* v_tag mismatch! */
5799 SCTP_STAT_INCR(sctps_badvtag);
5806 * no valid TCB for this packet, or we found it's a bad
5807 * packet while processing control, or we're done with this
5808 * packet (done or skip rest of data), so we drop it...
5813 * DATA chunk processing
5815 /* plow through the data chunks while length > offset */
5818 * Rest should be DATA only. Check authentication state if AUTH for
5821 if ((length > offset) &&
5823 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5824 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5825 !stcb->asoc.authenticated) {
5826 /* "silently" ignore */
5827 SCTP_STAT_INCR(sctps_recvauthmissing);
5828 SCTPDBG(SCTP_DEBUG_AUTH1,
5829 "Data chunk requires AUTH, skipped\n");
5832 if (length > offset) {
5836 * First check to make sure our state is correct. We would
5837 * not get here unless we really did have a tag, so we don't
5838 * abort if this happens, just dump the chunk silently.
5840 switch (SCTP_GET_STATE(&stcb->asoc)) {
5841 case SCTP_STATE_COOKIE_ECHOED:
5843 * we consider data with valid tags in this state
5844 * shows us the cookie-ack was lost. Imply it was
5847 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5848 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5849 stcb->asoc.overall_error_count,
5851 SCTP_FROM_SCTP_INPUT,
5854 stcb->asoc.overall_error_count = 0;
5855 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5857 case SCTP_STATE_COOKIE_WAIT:
5859 * We consider OOTB any data sent during asoc setup.
5861 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp,
5862 use_mflowid, mflowid,
5865 /* sa_ignore NOTREACHED */
5867 case SCTP_STATE_EMPTY: /* should not happen */
5868 case SCTP_STATE_INUSE: /* should not happen */
5869 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5870 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5873 /* sa_ignore NOTREACHED */
5875 case SCTP_STATE_OPEN:
5876 case SCTP_STATE_SHUTDOWN_SENT:
5879 /* plow through the data chunks while length > offset */
5880 retval = sctp_process_data(mm, iphlen, &offset, length,
5882 inp, stcb, net, &high_tsn,
5883 use_mflowid, mflowid,
5887 * The association aborted, NO UNLOCK needed since
5888 * the association is destroyed.
5895 * Anything important needs to have been m_copy'ed in
5899 /* take care of ecn */
5900 if ((data_processed == 1) &&
5901 (stcb->asoc.ecn_allowed == 1) &&
5902 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5903 /* Yep, we need to add a ECNE */
5904 sctp_send_ecn_echo(stcb, net, high_tsn);
5906 if ((data_processed == 0) && (fwd_tsn_seen)) {
5908 uint32_t highest_tsn;
5910 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5911 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5913 highest_tsn = stcb->asoc.highest_tsn_inside_map;
5915 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
5916 stcb->asoc.send_sack = 1;
5917 sctp_sack_check(stcb, was_a_gap);
5918 } else if (fwd_tsn_seen) {
5919 stcb->asoc.send_sack = 1;
5921 /* trigger send of any chunks in queue... */
5923 #ifdef SCTP_AUDITING_ENABLED
5924 sctp_audit_log(0xE0, 2);
5925 sctp_auditing(1, inp, stcb, net);
5927 SCTPDBG(SCTP_DEBUG_INPUT1,
5928 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5929 stcb->asoc.peers_rwnd,
5930 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5931 stcb->asoc.total_flight);
5932 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5933 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5934 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5936 if (cnt_ctrl_ready ||
5938 (stcb->asoc.peers_rwnd > 0 ||
5939 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5940 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5941 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5942 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5944 #ifdef SCTP_AUDITING_ENABLED
5945 sctp_audit_log(0xE0, 3);
5946 sctp_auditing(2, inp, stcb, net);
5950 SCTP_TCB_UNLOCK(stcb);
5952 if (inp_decr != NULL) {
5953 /* reduce ref-count */
5954 SCTP_INP_WLOCK(inp_decr);
5955 SCTP_INP_DECR_REF(inp_decr);
5956 SCTP_INP_WUNLOCK(inp_decr);
5960 sctp_validate_no_locks(inp);
5968 sctp_print_mbuf_chain(struct mbuf *m)
5970 for (; m; m = SCTP_BUF_NEXT(m)) {
5971 SCTP_PRINTF("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m));
5972 if (SCTP_BUF_IS_EXTENDED(m))
5973 SCTP_PRINTF("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m));
5981 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5985 uint32_t vrf_id = 0;
5987 struct sockaddr_in src, dst;
5990 struct sctp_chunkhdr *ch;
5993 #if !defined(SCTP_WITH_NO_CSUM)
5994 uint8_t compute_crc;
5998 uint8_t use_mflowid;
6001 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
6002 SCTP_RELEASE_PKT(i_pak);
6005 m = SCTP_HEADER_TO_CHAIN(i_pak);
6006 #ifdef SCTP_MBUF_LOGGING
6007 /* Log in any input mbufs */
6008 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6011 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
6012 if (SCTP_BUF_IS_EXTENDED(mat)) {
6013 sctp_log_mb(mat, SCTP_MBUF_INPUT);
6018 #ifdef SCTP_PACKET_LOGGING
6019 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
6023 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6024 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
6026 if_name(m->m_pkthdr.rcvif),
6027 m->m_pkthdr.csum_flags);
6028 if (m->m_flags & M_FLOWID) {
6029 mflowid = m->m_pkthdr.flowid;
6035 SCTP_STAT_INCR(sctps_recvpackets);
6036 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
6037 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
6038 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6039 if (SCTP_BUF_LEN(m) < offset) {
6040 if ((m = m_pullup(m, offset)) == NULL) {
6041 SCTP_STAT_INCR(sctps_hdrops);
6045 ip = mtod(m, struct ip *);
6046 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
6047 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
6048 offset -= sizeof(struct sctp_chunkhdr);
6049 memset(&src, 0, sizeof(struct sockaddr_in));
6050 src.sin_family = AF_INET;
6051 src.sin_len = sizeof(struct sockaddr_in);
6052 src.sin_port = sh->src_port;
6053 src.sin_addr = ip->ip_src;
6054 memset(&dst, 0, sizeof(struct sockaddr_in));
6055 dst.sin_family = AF_INET;
6056 dst.sin_len = sizeof(struct sockaddr_in);
6057 dst.sin_port = sh->dest_port;
6058 dst.sin_addr = ip->ip_dst;
6059 length = ip->ip_len + iphlen;
6060 /* Validate mbuf chain length with IP payload length. */
6061 if (SCTP_HEADER_LEN(m) != length) {
6062 SCTPDBG(SCTP_DEBUG_INPUT1,
6063 "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
6064 SCTP_STAT_INCR(sctps_hdrops);
6067 /* SCTP does not allow broadcasts or multicasts */
6068 if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
6071 if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
6074 ecn_bits = ip->ip_tos;
6075 #if defined(SCTP_WITH_NO_CSUM)
6076 SCTP_STAT_INCR(sctps_recvnocrc);
6078 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
6079 SCTP_STAT_INCR(sctps_recvhwcrc);
6082 SCTP_STAT_INCR(sctps_recvswcrc);
6086 sctp_common_input_processing(&m, iphlen, offset, length,
6087 (struct sockaddr *)&src,
6088 (struct sockaddr *)&dst,
6090 #if !defined(SCTP_WITH_NO_CSUM)
6094 use_mflowid, mflowid,
6103 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6104 extern int *sctp_cpuarry;
6109 sctp_input(struct mbuf *m, int off)
6111 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6116 uint32_t flowid, tag;
6119 if (m->m_flags & M_FLOWID) {
6120 flowid = m->m_pkthdr.flowid;
6123 * No flow id built by lower layers fix it so we
6126 offset = off + sizeof(struct sctphdr);
6127 if (SCTP_BUF_LEN(m) < offset) {
6128 if ((m = m_pullup(m, offset)) == NULL) {
6129 SCTP_STAT_INCR(sctps_hdrops);
6133 ip = mtod(m, struct ip *);
6134 sh = (struct sctphdr *)((caddr_t)ip + off);
6135 tag = htonl(sh->v_tag);
6136 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6137 m->m_pkthdr.flowid = flowid;
6138 m->m_flags |= M_FLOWID;
6140 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6141 sctp_queue_to_mcore(m, off, cpu_to_use);
6145 sctp_input_with_port(m, off, 0);