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 #if defined(INET) || defined(INET6)
51 #include <netinet/udp.h>
58 sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
60 struct sctp_nets *net;
63 * This now not only stops all cookie timers it also stops any INIT
64 * timers as well. This will make sure that the timers are stopped
65 * in all collision cases.
67 SCTP_TCB_LOCK_ASSERT(stcb);
68 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
69 if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
70 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
73 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
74 } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
75 sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
78 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
85 sctp_handle_init(struct mbuf *m, int iphlen, int offset,
86 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
87 struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
88 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_no_unlock,
89 uint8_t mflowtype, uint32_t mflowid,
90 uint32_t vrf_id, uint16_t port)
92 struct sctp_init *init;
95 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
100 /* validate length */
101 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
102 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
103 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
107 *abort_no_unlock = 1;
110 /* validate parameters */
112 if (init->initiate_tag == 0) {
113 /* protocol error... send abort */
114 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
115 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
119 *abort_no_unlock = 1;
122 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
123 /* invalid parameter... send abort */
124 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
125 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
129 *abort_no_unlock = 1;
132 if (init->num_inbound_streams == 0) {
133 /* protocol error... send abort */
134 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
135 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
139 *abort_no_unlock = 1;
142 if (init->num_outbound_streams == 0) {
143 /* protocol error... send abort */
144 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
145 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
149 *abort_no_unlock = 1;
152 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
153 offset + ntohs(cp->ch.chunk_length))) {
154 /* auth parameter(s) error... send abort */
155 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
156 "Problem with AUTH parameters");
157 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
161 *abort_no_unlock = 1;
164 /* We are only accepting if we have a listening socket. */
165 if ((stcb == NULL) &&
166 ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
167 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
168 (!SCTP_IS_LISTENING(inp)))) {
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 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
184 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
185 mflowtype, mflowid, inp->fibnum,
190 if ((stcb != NULL) &&
191 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
192 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
193 sctp_send_shutdown_ack(stcb, NULL);
194 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
196 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
197 sctp_send_initiate_ack(inp, stcb, net, m, iphlen, offset,
201 ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED));
205 SCTP_INP_RUNLOCK(inp);
210 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
214 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
215 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
222 struct sctp_stream_queue_pending *sp;
223 struct sctp_association *asoc;
226 * This function returns if any stream has true unsent data on it.
227 * Note that as it looks through it will clean up any places that
228 * have old data that has been sent but left at top of stream queue.
232 SCTP_TCB_SEND_LOCK(stcb);
233 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
234 /* Check to see if some data queued */
235 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
236 /* sa_ignore FREED_MEMORY */
237 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
241 if ((sp->msg_is_complete) &&
243 (sp->sender_all_done)) {
245 * We are doing differed cleanup. Last time
246 * through when we took all the data the
247 * sender_all_done was not set.
249 if (sp->put_last_out == 0) {
250 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
251 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
257 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
258 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
259 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, &asoc->strmout[i], sp, 1);
261 sctp_free_remote_addr(sp->net);
265 sctp_m_freem(sp->data);
268 sctp_free_a_strmoq(stcb, sp, so_locked);
269 if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
275 if (unsent_data > 0) {
280 SCTP_TCB_SEND_UNLOCK(stcb);
281 return (unsent_data);
285 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
287 struct sctp_init *init;
288 struct sctp_association *asoc;
289 struct sctp_nets *lnet;
294 /* save off parameters */
295 asoc->peer_vtag = ntohl(init->initiate_tag);
296 asoc->peers_rwnd = ntohl(init->a_rwnd);
298 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
300 if (!TAILQ_EMPTY(&asoc->nets)) {
301 /* update any ssthresh's that may have a default */
302 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
303 lnet->ssthresh = asoc->peers_rwnd;
304 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
305 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
309 SCTP_TCB_SEND_LOCK(stcb);
310 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
312 struct sctp_stream_out *outs;
313 struct sctp_stream_queue_pending *sp, *nsp;
314 struct sctp_tmit_chunk *chk, *nchk;
316 /* abandon the upper streams */
317 newcnt = ntohs(init->num_inbound_streams);
318 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
319 if (chk->rec.data.sid >= newcnt) {
320 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
321 asoc->send_queue_cnt--;
322 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
323 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
326 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
329 if (chk->data != NULL) {
330 sctp_free_bufspace(stcb, asoc, chk, 1);
331 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
332 0, chk, SCTP_SO_NOT_LOCKED);
334 sctp_m_freem(chk->data);
338 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
339 /* sa_ignore FREED_MEMORY */
343 for (i = newcnt; i < asoc->pre_open_streams; i++) {
344 outs = &asoc->strmout[i];
345 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
346 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
347 TAILQ_REMOVE(&outs->outqueue, sp, next);
348 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
349 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
350 stcb, 0, sp, SCTP_SO_NOT_LOCKED);
352 sctp_m_freem(sp->data);
356 sctp_free_remote_addr(sp->net);
360 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
361 /* sa_ignore FREED_MEMORY */
363 outs->state = SCTP_STREAM_CLOSED;
366 /* cut back the count */
367 asoc->pre_open_streams = newcnt;
369 SCTP_TCB_SEND_UNLOCK(stcb);
370 asoc->streamoutcnt = asoc->pre_open_streams;
372 for (i = 0; i < asoc->streamoutcnt; i++) {
373 asoc->strmout[i].state = SCTP_STREAM_OPEN;
376 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
377 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
378 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
379 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
381 /* This is the next one we expect */
382 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
384 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
385 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
387 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
388 /* open the requested streams */
390 if (asoc->strmin != NULL) {
391 /* Free the old ones */
392 for (i = 0; i < asoc->streamincnt; i++) {
393 sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
394 sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
396 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
398 if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
399 asoc->streamincnt = ntohs(init->num_outbound_streams);
401 asoc->streamincnt = asoc->max_inbound_streams;
403 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
404 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
405 if (asoc->strmin == NULL) {
406 /* we didn't get memory for the streams! */
407 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
410 for (i = 0; i < asoc->streamincnt; i++) {
411 asoc->strmin[i].sid = i;
412 asoc->strmin[i].last_mid_delivered = 0xffffffff;
413 TAILQ_INIT(&asoc->strmin[i].inqueue);
414 TAILQ_INIT(&asoc->strmin[i].uno_inqueue);
415 asoc->strmin[i].pd_api_started = 0;
416 asoc->strmin[i].delivery_started = 0;
419 * load_address_from_init will put the addresses into the
420 * association when the COOKIE is processed or the INIT-ACK is
421 * processed. Both types of COOKIE's existing and new call this
422 * routine. It will remove addresses that are no longer in the
423 * association (for the restarting case where addresses are
424 * removed). Up front when the INIT arrives we will discard it if it
425 * is a restart and new addresses have been added.
427 /* sa_ignore MEMLEAK */
432 * INIT-ACK message processing/consumption returns value < 0 on error
435 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
436 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
437 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
438 struct sctp_nets *net, int *abort_no_unlock,
439 uint8_t mflowtype, uint32_t mflowid,
442 struct sctp_association *asoc;
444 int retval, abort_flag;
445 uint32_t initack_limit;
446 int nat_friendly = 0;
448 /* First verify that we have no illegal param's */
451 op_err = sctp_arethere_unrecognized_parameters(m,
452 (offset + sizeof(struct sctp_init_chunk)),
453 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
455 /* Send an abort and notify peer */
456 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
457 *abort_no_unlock = 1;
461 asoc->peer_supports_nat = (uint8_t)nat_friendly;
462 /* process the peer's parameters in the INIT-ACK */
463 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
467 initack_limit = offset + ntohs(cp->ch.chunk_length);
468 /* load all addresses */
469 if ((retval = sctp_load_addresses_from_init(stcb, m,
470 (offset + sizeof(struct sctp_init_chunk)), initack_limit,
471 src, dst, NULL, stcb->asoc.port))) {
472 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
473 "Problem with address parameters");
474 SCTPDBG(SCTP_DEBUG_INPUT1,
475 "Load addresses from INIT causes an abort %d\n",
477 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
478 src, dst, sh, op_err,
481 *abort_no_unlock = 1;
484 /* if the peer doesn't support asconf, flush the asconf queue */
485 if (asoc->asconf_supported == 0) {
486 struct sctp_asconf_addr *param, *nparam;
488 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
489 TAILQ_REMOVE(&asoc->asconf_queue, param, next);
490 SCTP_FREE(param, SCTP_M_ASC_ADDR);
493 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
494 stcb->asoc.local_hmacs);
496 sctp_queue_op_err(stcb, op_err);
497 /* queuing will steal away the mbuf chain to the out queue */
500 /* extract the cookie and queue it to "echo" it back... */
501 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
502 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
503 stcb->asoc.overall_error_count,
505 SCTP_FROM_SCTP_INPUT,
508 stcb->asoc.overall_error_count = 0;
509 net->error_count = 0;
512 * Cancel the INIT timer, We do this first before queueing the
513 * cookie. We always cancel at the primary to assue that we are
514 * canceling the timer started by the INIT which always goes to the
517 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
518 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
520 /* calculate the RTO */
521 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy,
522 SCTP_RTT_FROM_NON_DATA);
523 retval = sctp_send_cookie_echo(m, offset, stcb, net);
526 * No cookie, we probably should send a op error. But in any
527 * case if there is no cookie in the INIT-ACK, we can
528 * abandon the peer, its broke.
533 len = (uint16_t)(sizeof(struct sctp_error_missing_param) + sizeof(uint16_t));
534 /* We abort with an error of missing mandatory param */
535 op_err = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
536 if (op_err != NULL) {
537 struct sctp_error_missing_param *cause;
539 SCTP_BUF_LEN(op_err) = len;
540 cause = mtod(op_err, struct sctp_error_missing_param *);
541 /* Subtract the reserved param */
542 cause->cause.code = htons(SCTP_CAUSE_MISSING_PARAM);
543 cause->cause.length = htons(len);
544 cause->num_missing_params = htonl(1);
545 cause->type[0] = htons(SCTP_STATE_COOKIE);
547 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
548 src, dst, sh, op_err,
551 *abort_no_unlock = 1;
559 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
560 struct sctp_tcb *stcb, struct sctp_nets *net)
562 union sctp_sockstore store;
563 struct sctp_nets *r_net, *f_net;
566 uint16_t old_error_counter;
568 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
572 memset(&store, 0, sizeof(store));
573 switch (cp->heartbeat.hb_info.addr_family) {
576 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
577 store.sin.sin_family = cp->heartbeat.hb_info.addr_family;
578 store.sin.sin_len = cp->heartbeat.hb_info.addr_len;
579 store.sin.sin_port = stcb->rport;
580 memcpy(&store.sin.sin_addr, cp->heartbeat.hb_info.address,
581 sizeof(store.sin.sin_addr));
589 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
590 store.sin6.sin6_family = cp->heartbeat.hb_info.addr_family;
591 store.sin6.sin6_len = cp->heartbeat.hb_info.addr_len;
592 store.sin6.sin6_port = stcb->rport;
593 memcpy(&store.sin6.sin6_addr, cp->heartbeat.hb_info.address, sizeof(struct in6_addr));
602 r_net = sctp_findnet(stcb, &store.sa);
604 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
607 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
608 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
609 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
611 * If the its a HB and it's random value is correct when can
612 * confirm the destination.
614 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
615 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
616 stcb->asoc.primary_destination = r_net;
617 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
618 f_net = TAILQ_FIRST(&stcb->asoc.nets);
619 if (f_net != r_net) {
621 * first one on the list is NOT the primary
622 * sctp_cmpaddr() is much more efficient if
623 * the primary is the first on the list,
626 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
627 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
631 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
632 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
633 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
634 r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
635 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
637 old_error_counter = r_net->error_count;
638 r_net->error_count = 0;
639 r_net->hb_responded = 1;
640 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
641 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
642 /* Now lets do a RTO with this */
643 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy,
644 SCTP_RTT_FROM_NON_DATA);
645 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
646 r_net->dest_state |= SCTP_ADDR_REACHABLE;
647 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
648 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
650 if (r_net->dest_state & SCTP_ADDR_PF) {
651 r_net->dest_state &= ~SCTP_ADDR_PF;
652 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
654 if (old_error_counter > 0) {
655 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
656 stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
657 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
659 if (r_net == stcb->asoc.primary_destination) {
660 if (stcb->asoc.alternate) {
661 /* release the alternate, primary is good */
662 sctp_free_remote_addr(stcb->asoc.alternate);
663 stcb->asoc.alternate = NULL;
666 /* Mobility adaptation */
668 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
669 SCTP_MOBILITY_BASE) ||
670 sctp_is_mobility_feature_on(stcb->sctp_ep,
671 SCTP_MOBILITY_FASTHANDOFF)) &&
672 sctp_is_mobility_feature_on(stcb->sctp_ep,
673 SCTP_MOBILITY_PRIM_DELETED)) {
675 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
676 stcb->sctp_ep, stcb, NULL,
677 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
678 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
679 SCTP_MOBILITY_FASTHANDOFF)) {
680 sctp_assoc_immediate_retrans(stcb,
681 stcb->asoc.primary_destination);
683 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
684 SCTP_MOBILITY_BASE)) {
685 sctp_move_chunks_from_net(stcb,
686 stcb->asoc.deleted_primary);
688 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
689 stcb->asoc.deleted_primary);
695 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
698 * return 0 means we want you to proceed with the abort non-zero
699 * means no abort processing
701 struct sctpasochead *head;
703 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
704 /* generate a new vtag and send init */
705 LIST_REMOVE(stcb, sctp_asocs);
706 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
707 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
709 * put it in the bucket in the vtag hash of assoc's for the
712 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
713 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
716 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
718 * treat like a case where the cookie expired i.e.: - dump
719 * current cookie. - generate a new vtag. - resend init.
721 /* generate a new vtag and send init */
722 LIST_REMOVE(stcb, sctp_asocs);
723 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
724 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
725 sctp_stop_all_cookie_timers(stcb);
726 sctp_toss_old_cookies(stcb, &stcb->asoc);
727 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
728 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
730 * put it in the bucket in the vtag hash of assoc's for the
733 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
734 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
741 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
742 struct sctp_nets *net)
745 * return 0 means we want you to proceed with the abort non-zero
746 * means no abort processing
748 if (stcb->asoc.auth_supported == 0) {
749 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
752 sctp_asconf_send_nat_state_update(stcb, net);
758 sctp_handle_abort(struct sctp_abort_chunk *abort,
759 struct sctp_tcb *stcb, struct sctp_nets *net)
761 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
767 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
771 len = ntohs(abort->ch.chunk_length);
772 if (len > sizeof(struct sctp_chunkhdr)) {
774 * Need to check the cause codes for our two magic nat
775 * aborts which don't kill the assoc necessarily.
777 struct sctp_gen_error_cause *cause;
779 cause = (struct sctp_gen_error_cause *)(abort + 1);
780 error = ntohs(cause->code);
781 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
782 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
783 abort->ch.chunk_flags);
784 if (sctp_handle_nat_colliding_state(stcb)) {
787 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
788 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
789 abort->ch.chunk_flags);
790 if (sctp_handle_nat_missing_state(stcb, net)) {
797 /* stop any receive timers */
798 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net,
799 SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
800 /* notify user of the abort and clean up... */
801 sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
803 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
804 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
805 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
806 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
808 #ifdef SCTP_ASOCLOG_OF_TSNS
809 sctp_print_out_track_log(stcb);
811 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
812 so = SCTP_INP_SO(stcb->sctp_ep);
813 atomic_add_int(&stcb->asoc.refcnt, 1);
814 SCTP_TCB_UNLOCK(stcb);
815 SCTP_SOCKET_LOCK(so, 1);
817 atomic_subtract_int(&stcb->asoc.refcnt, 1);
819 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
820 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
821 SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
822 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
823 SCTP_SOCKET_UNLOCK(so, 1);
825 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
829 sctp_start_net_timers(struct sctp_tcb *stcb)
831 uint32_t cnt_hb_sent;
832 struct sctp_nets *net;
835 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
837 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
838 * If the dest in unconfirmed send a hb as well if under
839 * max_hb_burst have been sent.
841 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
842 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
843 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
844 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
845 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
850 sctp_chunk_output(stcb->sctp_ep, stcb,
851 SCTP_OUTPUT_FROM_COOKIE_ACK,
858 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
859 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
861 struct sctp_association *asoc;
862 int some_on_streamwheel;
864 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
868 SCTPDBG(SCTP_DEBUG_INPUT2,
869 "sctp_handle_shutdown: handling SHUTDOWN\n");
873 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
874 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
877 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
878 /* Shutdown NOT the expected size */
881 old_state = SCTP_GET_STATE(asoc);
882 sctp_update_acked(stcb, cp, abort_flag);
886 if (asoc->control_pdapi) {
888 * With a normal shutdown we assume the end of last record.
890 SCTP_INP_READ_LOCK(stcb->sctp_ep);
891 if (asoc->control_pdapi->on_strm_q) {
892 struct sctp_stream_in *strm;
894 strm = &asoc->strmin[asoc->control_pdapi->sinfo_stream];
895 if (asoc->control_pdapi->on_strm_q == SCTP_ON_UNORDERED) {
897 TAILQ_REMOVE(&strm->uno_inqueue, asoc->control_pdapi, next_instrm);
898 asoc->control_pdapi->on_strm_q = 0;
899 } else if (asoc->control_pdapi->on_strm_q == SCTP_ON_ORDERED) {
901 TAILQ_REMOVE(&strm->inqueue, asoc->control_pdapi, next_instrm);
902 asoc->control_pdapi->on_strm_q = 0;
905 panic("Unknown state on ctrl:%p on_strm_q:%d",
907 asoc->control_pdapi->on_strm_q);
911 asoc->control_pdapi->end_added = 1;
912 asoc->control_pdapi->pdapi_aborted = 1;
913 asoc->control_pdapi = NULL;
914 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
915 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
916 so = SCTP_INP_SO(stcb->sctp_ep);
917 atomic_add_int(&stcb->asoc.refcnt, 1);
918 SCTP_TCB_UNLOCK(stcb);
919 SCTP_SOCKET_LOCK(so, 1);
921 atomic_subtract_int(&stcb->asoc.refcnt, 1);
922 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
923 /* assoc was freed while we were unlocked */
924 SCTP_SOCKET_UNLOCK(so, 1);
928 if (stcb->sctp_socket) {
929 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
931 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
932 SCTP_SOCKET_UNLOCK(so, 1);
935 /* goto SHUTDOWN_RECEIVED state to block new requests */
936 if (stcb->sctp_socket) {
937 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
938 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
939 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
940 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
941 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
943 * notify upper layer that peer has initiated a
946 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
949 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
952 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
954 * stop the shutdown timer, since we WILL move to
957 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
958 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
960 /* Now is there unsent data on a stream somewhere? */
961 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
963 if (!TAILQ_EMPTY(&asoc->send_queue) ||
964 !TAILQ_EMPTY(&asoc->sent_queue) ||
965 some_on_streamwheel) {
966 /* By returning we will push more data out */
969 /* no outstanding data to send, so move on... */
970 /* send SHUTDOWN-ACK */
971 /* move to SHUTDOWN-ACK-SENT state */
972 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
973 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
974 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
976 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
977 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
978 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
979 sctp_stop_timers_for_shutdown(stcb);
980 sctp_send_shutdown_ack(stcb, net);
981 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
982 stcb->sctp_ep, stcb, net);
983 } else if (old_state == SCTP_STATE_SHUTDOWN_ACK_SENT) {
984 sctp_send_shutdown_ack(stcb, net);
990 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
991 struct sctp_tcb *stcb,
992 struct sctp_nets *net)
994 struct sctp_association *asoc;
995 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
998 so = SCTP_INP_SO(stcb->sctp_ep);
1000 SCTPDBG(SCTP_DEBUG_INPUT2,
1001 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
1006 /* process according to association state */
1007 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
1008 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
1009 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
1010 sctp_send_shutdown_complete(stcb, net, 1);
1011 SCTP_TCB_UNLOCK(stcb);
1014 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
1015 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1016 /* unexpected SHUTDOWN-ACK... so ignore... */
1017 SCTP_TCB_UNLOCK(stcb);
1020 if (asoc->control_pdapi) {
1022 * With a normal shutdown we assume the end of last record.
1024 SCTP_INP_READ_LOCK(stcb->sctp_ep);
1025 asoc->control_pdapi->end_added = 1;
1026 asoc->control_pdapi->pdapi_aborted = 1;
1027 asoc->control_pdapi = NULL;
1028 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1029 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1030 atomic_add_int(&stcb->asoc.refcnt, 1);
1031 SCTP_TCB_UNLOCK(stcb);
1032 SCTP_SOCKET_LOCK(so, 1);
1033 SCTP_TCB_LOCK(stcb);
1034 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1035 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1036 /* assoc was freed while we were unlocked */
1037 SCTP_SOCKET_UNLOCK(so, 1);
1041 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1042 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1043 SCTP_SOCKET_UNLOCK(so, 1);
1047 if (!TAILQ_EMPTY(&asoc->send_queue) ||
1048 !TAILQ_EMPTY(&asoc->sent_queue) ||
1049 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
1050 panic("Queues are not empty when handling SHUTDOWN-ACK");
1053 /* stop the timer */
1054 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net,
1055 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1056 /* send SHUTDOWN-COMPLETE */
1057 sctp_send_shutdown_complete(stcb, net, 0);
1058 /* notify upper layer protocol */
1059 if (stcb->sctp_socket) {
1060 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1061 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1062 stcb->sctp_socket->so_snd.sb_cc = 0;
1064 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1066 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1067 /* free the TCB but first save off the ep */
1068 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1069 atomic_add_int(&stcb->asoc.refcnt, 1);
1070 SCTP_TCB_UNLOCK(stcb);
1071 SCTP_SOCKET_LOCK(so, 1);
1072 SCTP_TCB_LOCK(stcb);
1073 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1075 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1076 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1077 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1078 SCTP_SOCKET_UNLOCK(so, 1);
1083 * Skip past the param header and then we will find the chunk that caused the
1084 * problem. There are two possibilities ASCONF or FWD-TSN other than that and
1085 * our peer must be broken.
1088 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1089 struct sctp_nets *net)
1091 struct sctp_chunkhdr *chk;
1093 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1094 switch (chk->chunk_type) {
1095 case SCTP_ASCONF_ACK:
1097 sctp_asconf_cleanup(stcb, net);
1099 case SCTP_IFORWARD_CUM_TSN:
1100 case SCTP_FORWARD_CUM_TSN:
1101 stcb->asoc.prsctp_supported = 0;
1104 SCTPDBG(SCTP_DEBUG_INPUT2,
1105 "Peer does not support chunk type %d(%x)??\n",
1106 chk->chunk_type, (uint32_t)chk->chunk_type);
1112 * Skip past the param header and then we will find the param that caused the
1113 * problem. There are a number of param's in a ASCONF OR the prsctp param
1114 * these will turn of specific features.
1115 * XXX: Is this the right thing to do?
1118 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1120 struct sctp_paramhdr *pbad;
1123 switch (ntohs(pbad->param_type)) {
1125 case SCTP_PRSCTP_SUPPORTED:
1126 stcb->asoc.prsctp_supported = 0;
1128 case SCTP_SUPPORTED_CHUNK_EXT:
1130 /* draft-ietf-tsvwg-addip-sctp */
1131 case SCTP_HAS_NAT_SUPPORT:
1132 stcb->asoc.peer_supports_nat = 0;
1134 case SCTP_ADD_IP_ADDRESS:
1135 case SCTP_DEL_IP_ADDRESS:
1136 case SCTP_SET_PRIM_ADDR:
1137 stcb->asoc.asconf_supported = 0;
1139 case SCTP_SUCCESS_REPORT:
1140 case SCTP_ERROR_CAUSE_IND:
1141 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1142 SCTPDBG(SCTP_DEBUG_INPUT2,
1143 "Turning off ASCONF to this strange peer\n");
1144 stcb->asoc.asconf_supported = 0;
1147 SCTPDBG(SCTP_DEBUG_INPUT2,
1148 "Peer does not support param type %d(%x)??\n",
1149 pbad->param_type, (uint32_t)pbad->param_type);
1155 sctp_handle_error(struct sctp_chunkhdr *ch,
1156 struct sctp_tcb *stcb, struct sctp_nets *net)
1159 struct sctp_paramhdr *phdr;
1160 uint16_t error, error_type;
1162 struct sctp_association *asoc;
1164 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1168 /* parse through all of the errors and process */
1170 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1171 sizeof(struct sctp_chunkhdr));
1172 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1174 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1175 /* Process an Error Cause */
1176 error_type = ntohs(phdr->param_type);
1177 error_len = ntohs(phdr->param_length);
1178 if ((error_len > chklen) || (error_len == 0)) {
1179 /* invalid param length for this param */
1180 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1185 /* report the first error cause */
1188 switch (error_type) {
1189 case SCTP_CAUSE_INVALID_STREAM:
1190 case SCTP_CAUSE_MISSING_PARAM:
1191 case SCTP_CAUSE_INVALID_PARAM:
1192 case SCTP_CAUSE_NO_USER_DATA:
1193 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1196 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1197 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1199 if (sctp_handle_nat_colliding_state(stcb)) {
1203 case SCTP_CAUSE_NAT_MISSING_STATE:
1204 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1206 if (sctp_handle_nat_missing_state(stcb, net)) {
1210 case SCTP_CAUSE_STALE_COOKIE:
1212 * We only act if we have echoed a cookie and are
1215 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1218 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1219 /* Save the time doubled */
1220 asoc->cookie_preserve_req = ntohl(*p) << 1;
1221 asoc->stale_cookie_count++;
1222 if (asoc->stale_cookie_count >
1223 asoc->max_init_times) {
1224 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1225 /* now free the asoc */
1226 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1227 so = SCTP_INP_SO(stcb->sctp_ep);
1228 atomic_add_int(&stcb->asoc.refcnt, 1);
1229 SCTP_TCB_UNLOCK(stcb);
1230 SCTP_SOCKET_LOCK(so, 1);
1231 SCTP_TCB_LOCK(stcb);
1232 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1234 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1235 SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1236 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1237 SCTP_SOCKET_UNLOCK(so, 1);
1241 /* blast back to INIT state */
1242 sctp_toss_old_cookies(stcb, &stcb->asoc);
1243 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1244 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1245 sctp_stop_all_cookie_timers(stcb);
1246 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1249 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1251 * Nothing we can do here, we don't do hostname
1252 * addresses so if the peer does not like my IPv6
1253 * (or IPv4 for that matter) it does not matter. If
1254 * they don't support that type of address, they can
1255 * NOT possibly get that packet type... i.e. with no
1256 * IPv6 you can't receive a IPv6 packet. so we can
1257 * safely ignore this one. If we ever added support
1258 * for HOSTNAME Addresses, then we would need to do
1262 case SCTP_CAUSE_UNRECOG_CHUNK:
1263 sctp_process_unrecog_chunk(stcb, phdr, net);
1265 case SCTP_CAUSE_UNRECOG_PARAM:
1266 sctp_process_unrecog_param(stcb, phdr);
1268 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1270 * We ignore this since the timer will drive out a
1271 * new cookie anyway and there timer will drive us
1272 * to send a SHUTDOWN_COMPLETE. We can't send one
1273 * here since we don't have their tag.
1276 case SCTP_CAUSE_DELETING_LAST_ADDR:
1277 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1278 case SCTP_CAUSE_DELETING_SRC_ADDR:
1280 * We should NOT get these here, but in a
1283 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1286 case SCTP_CAUSE_OUT_OF_RESC:
1288 * And what, pray tell do we do with the fact that
1289 * the peer is out of resources? Not really sure we
1290 * could do anything but abort. I suspect this
1291 * should have came WITH an abort instead of in a
1296 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1300 adjust = SCTP_SIZE32(error_len);
1302 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1304 sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, error, ch, SCTP_SO_NOT_LOCKED);
1309 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1310 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1311 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1312 struct sctp_nets *net, int *abort_no_unlock,
1313 uint8_t mflowtype, uint32_t mflowid,
1316 struct sctp_init_ack *init_ack;
1317 struct mbuf *op_err;
1319 SCTPDBG(SCTP_DEBUG_INPUT2,
1320 "sctp_handle_init_ack: handling INIT-ACK\n");
1323 SCTPDBG(SCTP_DEBUG_INPUT2,
1324 "sctp_handle_init_ack: TCB is null\n");
1327 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1328 /* Invalid length */
1329 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1330 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1331 src, dst, sh, op_err,
1334 *abort_no_unlock = 1;
1337 init_ack = &cp->init;
1338 /* validate parameters */
1339 if (init_ack->initiate_tag == 0) {
1340 /* protocol error... send an abort */
1341 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1342 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1343 src, dst, sh, op_err,
1346 *abort_no_unlock = 1;
1349 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1350 /* protocol error... send an abort */
1351 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1352 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1353 src, dst, sh, op_err,
1356 *abort_no_unlock = 1;
1359 if (init_ack->num_inbound_streams == 0) {
1360 /* protocol error... send an abort */
1361 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1362 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1363 src, dst, sh, op_err,
1366 *abort_no_unlock = 1;
1369 if (init_ack->num_outbound_streams == 0) {
1370 /* protocol error... send an abort */
1371 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1372 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1373 src, dst, sh, op_err,
1376 *abort_no_unlock = 1;
1379 /* process according to association state... */
1380 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1381 case SCTP_STATE_COOKIE_WAIT:
1382 /* this is the expected state for this chunk */
1383 /* process the INIT-ACK parameters */
1384 if (stcb->asoc.primary_destination->dest_state &
1385 SCTP_ADDR_UNCONFIRMED) {
1387 * The primary is where we sent the INIT, we can
1388 * always consider it confirmed when the INIT-ACK is
1389 * returned. Do this before we load addresses
1392 stcb->asoc.primary_destination->dest_state &=
1393 ~SCTP_ADDR_UNCONFIRMED;
1394 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1395 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1397 if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1398 net, abort_no_unlock,
1401 /* error in parsing parameters */
1404 /* update our state */
1405 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1406 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1408 /* reset the RTO calc */
1409 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1410 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1411 stcb->asoc.overall_error_count,
1413 SCTP_FROM_SCTP_INPUT,
1416 stcb->asoc.overall_error_count = 0;
1417 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1419 * collapse the init timer back in case of a exponential
1422 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1425 * the send at the end of the inbound data processing will
1426 * cause the cookie to be sent
1429 case SCTP_STATE_SHUTDOWN_SENT:
1430 /* incorrect state... discard */
1432 case SCTP_STATE_COOKIE_ECHOED:
1433 /* incorrect state... discard */
1435 case SCTP_STATE_OPEN:
1436 /* incorrect state... discard */
1438 case SCTP_STATE_EMPTY:
1439 case SCTP_STATE_INUSE:
1441 /* incorrect state... discard */
1445 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1449 static struct sctp_tcb *
1450 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1451 struct sockaddr *src, struct sockaddr *dst,
1452 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1453 struct sctp_inpcb *inp, struct sctp_nets **netp,
1454 struct sockaddr *init_src, int *notification,
1455 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1456 uint8_t mflowtype, uint32_t mflowid,
1457 uint32_t vrf_id, uint16_t port);
1461 * handle a state cookie for an existing association m: input packet mbuf
1462 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1463 * "split" mbuf and the cookie signature does not exist offset: offset into
1464 * mbuf to the cookie-echo chunk
1466 static struct sctp_tcb *
1467 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1468 struct sockaddr *src, struct sockaddr *dst,
1469 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1470 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1471 struct sockaddr *init_src, int *notification,
1472 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1473 uint8_t mflowtype, uint32_t mflowid,
1474 uint32_t vrf_id, uint16_t port)
1476 struct sctp_association *asoc;
1477 struct sctp_init_chunk *init_cp, init_buf;
1478 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1479 struct sctp_nets *net;
1480 struct mbuf *op_err;
1481 int init_offset, initack_offset, i;
1485 #if defined(SCTP_DETAILED_STR_STATS)
1490 /* I know that the TCB is non-NULL from the caller */
1492 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1493 if (asoc->cookie_how[how_indx] == 0)
1496 if (how_indx < sizeof(asoc->cookie_how)) {
1497 asoc->cookie_how[how_indx] = 1;
1499 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1500 /* SHUTDOWN came in after sending INIT-ACK */
1501 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1502 op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
1503 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1504 mflowtype, mflowid, inp->fibnum,
1506 if (how_indx < sizeof(asoc->cookie_how))
1507 asoc->cookie_how[how_indx] = 2;
1511 * find and validate the INIT chunk in the cookie (peer's info) the
1512 * INIT should start after the cookie-echo header struct (chunk
1513 * header, state cookie header struct)
1515 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1517 init_cp = (struct sctp_init_chunk *)
1518 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1519 (uint8_t *)&init_buf);
1520 if (init_cp == NULL) {
1521 /* could not pull a INIT chunk in cookie */
1524 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1528 * find and validate the INIT-ACK chunk in the cookie (my info) the
1529 * INIT-ACK follows the INIT chunk
1531 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1532 initack_cp = (struct sctp_init_ack_chunk *)
1533 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1534 (uint8_t *)&initack_buf);
1535 if (initack_cp == NULL) {
1536 /* could not pull INIT-ACK chunk in cookie */
1539 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1542 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1543 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1545 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1546 * to get into the OPEN state
1548 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1550 * Opps, this means that we somehow generated two vtag's
1551 * the same. I.e. we did:
1553 * <---INIT(tag=a)------
1554 * ----INIT-ACK(tag=t)-->
1555 * ----INIT(tag=t)------> *1
1556 * <---INIT-ACK(tag=a)---
1557 * <----CE(tag=t)------------- *2
1559 * At point *1 we should be generating a different
1560 * tag t'. Which means we would throw away the CE and send
1561 * ours instead. Basically this is case C (throw away side).
1563 if (how_indx < sizeof(asoc->cookie_how))
1564 asoc->cookie_how[how_indx] = 17;
1568 switch (SCTP_GET_STATE(asoc)) {
1569 case SCTP_STATE_COOKIE_WAIT:
1570 case SCTP_STATE_COOKIE_ECHOED:
1572 * INIT was sent but got a COOKIE_ECHO with the
1573 * correct tags... just accept it...but we must
1574 * process the init so that we can make sure we have
1575 * the right seq no's.
1577 /* First we must process the INIT !! */
1578 retval = sctp_process_init(init_cp, stcb);
1580 if (how_indx < sizeof(asoc->cookie_how))
1581 asoc->cookie_how[how_indx] = 3;
1584 /* we have already processed the INIT so no problem */
1585 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp,
1587 SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1588 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp,
1590 SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1591 /* update current state */
1592 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1593 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1595 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1597 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1598 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1599 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1600 stcb->sctp_ep, stcb, asoc->primary_destination);
1602 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1603 sctp_stop_all_cookie_timers(stcb);
1604 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1605 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1606 (!SCTP_IS_LISTENING(inp))) {
1607 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1611 * Here is where collision would go if we
1612 * did a connect() and instead got a
1613 * init/init-ack/cookie done before the
1614 * init-ack came back..
1616 stcb->sctp_ep->sctp_flags |=
1617 SCTP_PCB_FLAGS_CONNECTED;
1618 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1619 so = SCTP_INP_SO(stcb->sctp_ep);
1620 atomic_add_int(&stcb->asoc.refcnt, 1);
1621 SCTP_TCB_UNLOCK(stcb);
1622 SCTP_SOCKET_LOCK(so, 1);
1623 SCTP_TCB_LOCK(stcb);
1624 atomic_add_int(&stcb->asoc.refcnt, -1);
1625 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1626 SCTP_SOCKET_UNLOCK(so, 1);
1630 soisconnected(stcb->sctp_socket);
1631 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1632 SCTP_SOCKET_UNLOCK(so, 1);
1635 /* notify upper layer */
1636 *notification = SCTP_NOTIFY_ASSOC_UP;
1638 * since we did not send a HB make sure we don't
1641 net->hb_responded = 1;
1642 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1643 &cookie->time_entered,
1644 sctp_align_unsafe_makecopy,
1645 SCTP_RTT_FROM_NON_DATA);
1647 if (stcb->asoc.sctp_autoclose_ticks &&
1648 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1649 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1655 * we're in the OPEN state (or beyond), so peer must
1656 * have simply lost the COOKIE-ACK
1660 sctp_stop_all_cookie_timers(stcb);
1662 * We ignore the return code here.. not sure if we should
1663 * somehow abort.. but we do have an existing asoc. This
1664 * really should not fail.
1666 if (sctp_load_addresses_from_init(stcb, m,
1667 init_offset + sizeof(struct sctp_init_chunk),
1668 initack_offset, src, dst, init_src, stcb->asoc.port)) {
1669 if (how_indx < sizeof(asoc->cookie_how))
1670 asoc->cookie_how[how_indx] = 4;
1673 /* respond with a COOKIE-ACK */
1674 sctp_toss_old_cookies(stcb, asoc);
1675 sctp_send_cookie_ack(stcb);
1676 if (how_indx < sizeof(asoc->cookie_how))
1677 asoc->cookie_how[how_indx] = 5;
1680 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1681 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1682 cookie->tie_tag_my_vtag == 0 &&
1683 cookie->tie_tag_peer_vtag == 0) {
1685 * case C in Section 5.2.4 Table 2: XMOO silently discard
1687 if (how_indx < sizeof(asoc->cookie_how))
1688 asoc->cookie_how[how_indx] = 6;
1692 * If nat support, and the below and stcb is established, send back
1693 * a ABORT(colliding state) if we are established.
1695 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1696 (asoc->peer_supports_nat) &&
1697 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1698 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1699 (asoc->peer_vtag == 0)))) {
1701 * Special case - Peer's support nat. We may have two init's
1702 * that we gave out the same tag on since one was not
1703 * established.. i.e. we get INIT from host-1 behind the nat
1704 * and we respond tag-a, we get a INIT from host-2 behind
1705 * the nat and we get tag-a again. Then we bring up host-1
1706 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1707 * Now we have colliding state. We must send an abort here
1708 * with colliding state indication.
1710 op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
1711 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1712 mflowtype, mflowid, inp->fibnum,
1716 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1717 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1718 (asoc->peer_vtag == 0))) {
1720 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1721 * should be ok, re-accept peer info
1723 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1725 * Extension of case C. If we hit this, then the
1726 * random number generator returned the same vtag
1727 * when we first sent our INIT-ACK and when we later
1728 * sent our INIT. The side with the seq numbers that
1729 * are different will be the one that normnally
1730 * would have hit case C. This in effect "extends"
1731 * our vtags in this collision case to be 64 bits.
1732 * The same collision could occur aka you get both
1733 * vtag and seq number the same twice in a row.. but
1734 * is much less likely. If it did happen then we
1735 * would proceed through and bring up the assoc.. we
1736 * may end up with the wrong stream setup however..
1737 * which would be bad.. but there is no way to
1738 * tell.. until we send on a stream that does not
1741 if (how_indx < sizeof(asoc->cookie_how))
1742 asoc->cookie_how[how_indx] = 7;
1746 if (how_indx < sizeof(asoc->cookie_how))
1747 asoc->cookie_how[how_indx] = 8;
1748 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1749 SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1750 sctp_stop_all_cookie_timers(stcb);
1752 * since we did not send a HB make sure we don't double
1755 net->hb_responded = 1;
1756 if (stcb->asoc.sctp_autoclose_ticks &&
1757 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1758 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1761 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1762 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1764 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1766 * Ok the peer probably discarded our data (if we
1767 * echoed a cookie+data). So anything on the
1768 * sent_queue should be marked for retransmit, we
1769 * may not get something to kick us so it COULD
1770 * still take a timeout to move these.. but it can't
1771 * hurt to mark them.
1773 struct sctp_tmit_chunk *chk;
1775 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1776 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1777 chk->sent = SCTP_DATAGRAM_RESEND;
1778 sctp_flight_size_decrease(chk);
1779 sctp_total_flight_decrease(stcb, chk);
1780 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1786 /* process the INIT info (peer's info) */
1787 retval = sctp_process_init(init_cp, stcb);
1789 if (how_indx < sizeof(asoc->cookie_how))
1790 asoc->cookie_how[how_indx] = 9;
1793 if (sctp_load_addresses_from_init(stcb, m,
1794 init_offset + sizeof(struct sctp_init_chunk),
1795 initack_offset, src, dst, init_src, stcb->asoc.port)) {
1796 if (how_indx < sizeof(asoc->cookie_how))
1797 asoc->cookie_how[how_indx] = 10;
1800 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1801 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1802 *notification = SCTP_NOTIFY_ASSOC_UP;
1804 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1805 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1806 (!SCTP_IS_LISTENING(inp))) {
1807 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1810 stcb->sctp_ep->sctp_flags |=
1811 SCTP_PCB_FLAGS_CONNECTED;
1812 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1813 so = SCTP_INP_SO(stcb->sctp_ep);
1814 atomic_add_int(&stcb->asoc.refcnt, 1);
1815 SCTP_TCB_UNLOCK(stcb);
1816 SCTP_SOCKET_LOCK(so, 1);
1817 SCTP_TCB_LOCK(stcb);
1818 atomic_add_int(&stcb->asoc.refcnt, -1);
1819 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1820 SCTP_SOCKET_UNLOCK(so, 1);
1824 soisconnected(stcb->sctp_socket);
1825 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1826 SCTP_SOCKET_UNLOCK(so, 1);
1829 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1830 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1832 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1833 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1834 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1835 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1837 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1839 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1840 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1841 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1842 stcb->sctp_ep, stcb, asoc->primary_destination);
1844 sctp_stop_all_cookie_timers(stcb);
1845 sctp_toss_old_cookies(stcb, asoc);
1846 sctp_send_cookie_ack(stcb);
1849 * only if we have retrans set do we do this. What
1850 * this call does is get only the COOKIE-ACK out and
1851 * then when we return the normal call to
1852 * sctp_chunk_output will get the retrans out behind
1855 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1857 if (how_indx < sizeof(asoc->cookie_how))
1858 asoc->cookie_how[how_indx] = 11;
1862 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1863 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1864 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1865 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1866 cookie->tie_tag_peer_vtag != 0) {
1867 struct sctpasochead *head;
1868 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1872 if (asoc->peer_supports_nat) {
1874 * This is a gross gross hack. Just call the
1875 * cookie_new code since we are allowing a duplicate
1876 * association. I hope this works...
1878 return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
1879 sh, cookie, cookie_len,
1880 inp, netp, init_src, notification,
1881 auth_skipped, auth_offset, auth_len,
1886 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1889 if (how_indx < sizeof(asoc->cookie_how))
1890 asoc->cookie_how[how_indx] = 12;
1891 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
1892 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1893 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1894 SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
1896 /* notify upper layer */
1897 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1898 atomic_add_int(&stcb->asoc.refcnt, 1);
1899 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1900 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1901 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1902 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1904 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1905 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1906 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1907 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1909 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1910 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1911 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1912 stcb->sctp_ep, stcb, asoc->primary_destination);
1914 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1915 /* move to OPEN state, if not in SHUTDOWN_SENT */
1916 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1918 asoc->pre_open_streams =
1919 ntohs(initack_cp->init.num_outbound_streams);
1920 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1921 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1922 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1924 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1926 asoc->str_reset_seq_in = asoc->init_seq_number;
1928 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1929 if (asoc->mapping_array) {
1930 memset(asoc->mapping_array, 0,
1931 asoc->mapping_array_size);
1933 if (asoc->nr_mapping_array) {
1934 memset(asoc->nr_mapping_array, 0,
1935 asoc->mapping_array_size);
1937 SCTP_TCB_UNLOCK(stcb);
1938 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1939 so = SCTP_INP_SO(stcb->sctp_ep);
1940 SCTP_SOCKET_LOCK(so, 1);
1942 SCTP_INP_INFO_WLOCK();
1943 SCTP_INP_WLOCK(stcb->sctp_ep);
1944 SCTP_TCB_LOCK(stcb);
1945 atomic_add_int(&stcb->asoc.refcnt, -1);
1946 /* send up all the data */
1947 SCTP_TCB_SEND_LOCK(stcb);
1949 sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_LOCKED);
1950 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1951 stcb->asoc.strmout[i].chunks_on_queues = 0;
1952 #if defined(SCTP_DETAILED_STR_STATS)
1953 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1954 asoc->strmout[i].abandoned_sent[j] = 0;
1955 asoc->strmout[i].abandoned_unsent[j] = 0;
1958 asoc->strmout[i].abandoned_sent[0] = 0;
1959 asoc->strmout[i].abandoned_unsent[0] = 0;
1961 stcb->asoc.strmout[i].sid = i;
1962 stcb->asoc.strmout[i].next_mid_ordered = 0;
1963 stcb->asoc.strmout[i].next_mid_unordered = 0;
1964 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1966 /* process the INIT-ACK info (my info) */
1967 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1968 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1970 /* pull from vtag hash */
1971 LIST_REMOVE(stcb, sctp_asocs);
1972 /* re-insert to new vtag position */
1973 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1974 SCTP_BASE_INFO(hashasocmark))];
1976 * put it in the bucket in the vtag hash of assoc's for the
1979 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1981 SCTP_TCB_SEND_UNLOCK(stcb);
1982 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1983 SCTP_INP_INFO_WUNLOCK();
1984 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1985 SCTP_SOCKET_UNLOCK(so, 1);
1987 asoc->total_flight = 0;
1988 asoc->total_flight_count = 0;
1989 /* process the INIT info (peer's info) */
1990 retval = sctp_process_init(init_cp, stcb);
1992 if (how_indx < sizeof(asoc->cookie_how))
1993 asoc->cookie_how[how_indx] = 13;
1998 * since we did not send a HB make sure we don't double
2001 net->hb_responded = 1;
2003 if (sctp_load_addresses_from_init(stcb, m,
2004 init_offset + sizeof(struct sctp_init_chunk),
2005 initack_offset, src, dst, init_src, stcb->asoc.port)) {
2006 if (how_indx < sizeof(asoc->cookie_how))
2007 asoc->cookie_how[how_indx] = 14;
2011 /* respond with a COOKIE-ACK */
2012 sctp_stop_all_cookie_timers(stcb);
2013 sctp_toss_old_cookies(stcb, asoc);
2014 sctp_send_cookie_ack(stcb);
2015 if (how_indx < sizeof(asoc->cookie_how))
2016 asoc->cookie_how[how_indx] = 15;
2020 if (how_indx < sizeof(asoc->cookie_how))
2021 asoc->cookie_how[how_indx] = 16;
2022 /* all other cases... */
2028 * handle a state cookie for a new association m: input packet mbuf chain--
2029 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
2030 * and the cookie signature does not exist offset: offset into mbuf to the
2031 * cookie-echo chunk length: length of the cookie chunk to: where the init
2032 * was from returns a new TCB
2034 static struct sctp_tcb *
2035 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
2036 struct sockaddr *src, struct sockaddr *dst,
2037 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
2038 struct sctp_inpcb *inp, struct sctp_nets **netp,
2039 struct sockaddr *init_src, int *notification,
2040 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2041 uint8_t mflowtype, uint32_t mflowid,
2042 uint32_t vrf_id, uint16_t port)
2044 struct sctp_tcb *stcb;
2045 struct sctp_init_chunk *init_cp, init_buf;
2046 struct sctp_init_ack_chunk *initack_cp, initack_buf;
2047 union sctp_sockstore store;
2048 struct sctp_association *asoc;
2049 int init_offset, initack_offset, initack_limit;
2052 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
2053 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2056 so = SCTP_INP_SO(inp);
2060 * find and validate the INIT chunk in the cookie (peer's info) the
2061 * INIT should start after the cookie-echo header struct (chunk
2062 * header, state cookie header struct)
2064 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2065 init_cp = (struct sctp_init_chunk *)
2066 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2067 (uint8_t *)&init_buf);
2068 if (init_cp == NULL) {
2069 /* could not pull a INIT chunk in cookie */
2070 SCTPDBG(SCTP_DEBUG_INPUT1,
2071 "process_cookie_new: could not pull INIT chunk hdr\n");
2074 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2075 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2078 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2080 * find and validate the INIT-ACK chunk in the cookie (my info) the
2081 * INIT-ACK follows the INIT chunk
2083 initack_cp = (struct sctp_init_ack_chunk *)
2084 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2085 (uint8_t *)&initack_buf);
2086 if (initack_cp == NULL) {
2087 /* could not pull INIT-ACK chunk in cookie */
2088 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2091 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2095 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2096 * "initack_limit" value. This is because the chk_length field
2097 * includes the length of the cookie, but the cookie is omitted when
2098 * the INIT and INIT_ACK are tacked onto the cookie...
2100 initack_limit = offset + cookie_len;
2103 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2108 * Here we do a trick, we set in NULL for the proc/thread argument.
2109 * We do this since in effect we only use the p argument when the
2110 * socket is unbound and we must do an implicit bind. Since we are
2111 * getting a cookie, we cannot be unbound.
2113 stcb = sctp_aloc_assoc(inp, init_src, &error,
2114 ntohl(initack_cp->init.initiate_tag), vrf_id,
2115 ntohs(initack_cp->init.num_outbound_streams),
2117 (struct thread *)NULL
2120 struct mbuf *op_err;
2122 /* memory problem? */
2123 SCTPDBG(SCTP_DEBUG_INPUT1,
2124 "process_cookie_new: no room for another TCB!\n");
2125 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2126 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2127 src, dst, sh, op_err,
2132 /* get the correct sctp_nets */
2134 *netp = sctp_findnet(stcb, init_src);
2137 /* get scope variables out of cookie */
2138 asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2139 asoc->scope.site_scope = cookie->site_scope;
2140 asoc->scope.local_scope = cookie->local_scope;
2141 asoc->scope.loopback_scope = cookie->loopback_scope;
2143 if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2144 (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2145 struct mbuf *op_err;
2148 * Houston we have a problem. The EP changed while the
2149 * cookie was in flight. Only recourse is to abort the
2152 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2153 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2154 src, dst, sh, op_err,
2157 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2158 atomic_add_int(&stcb->asoc.refcnt, 1);
2159 SCTP_TCB_UNLOCK(stcb);
2160 SCTP_SOCKET_LOCK(so, 1);
2161 SCTP_TCB_LOCK(stcb);
2162 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2164 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2165 SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2166 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2167 SCTP_SOCKET_UNLOCK(so, 1);
2171 /* process the INIT-ACK info (my info) */
2172 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2173 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2174 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2175 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2176 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2177 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2178 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2179 asoc->str_reset_seq_in = asoc->init_seq_number;
2181 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2183 /* process the INIT info (peer's info) */
2185 retval = sctp_process_init(init_cp, stcb);
2189 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2190 atomic_add_int(&stcb->asoc.refcnt, 1);
2191 SCTP_TCB_UNLOCK(stcb);
2192 SCTP_SOCKET_LOCK(so, 1);
2193 SCTP_TCB_LOCK(stcb);
2194 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2196 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2197 SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2198 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2199 SCTP_SOCKET_UNLOCK(so, 1);
2203 /* load all addresses */
2204 if (sctp_load_addresses_from_init(stcb, m,
2205 init_offset + sizeof(struct sctp_init_chunk), initack_offset,
2206 src, dst, init_src, port)) {
2207 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2208 atomic_add_int(&stcb->asoc.refcnt, 1);
2209 SCTP_TCB_UNLOCK(stcb);
2210 SCTP_SOCKET_LOCK(so, 1);
2211 SCTP_TCB_LOCK(stcb);
2212 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2214 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2215 SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2216 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2217 SCTP_SOCKET_UNLOCK(so, 1);
2222 * verify any preceding AUTH chunk that was skipped
2224 /* pull the local authentication parameters from the cookie/init-ack */
2225 sctp_auth_get_cookie_params(stcb, m,
2226 initack_offset + sizeof(struct sctp_init_ack_chunk),
2227 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2229 struct sctp_auth_chunk *auth;
2231 auth = (struct sctp_auth_chunk *)
2232 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2233 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2234 /* auth HMAC failed, dump the assoc and packet */
2235 SCTPDBG(SCTP_DEBUG_AUTH1,
2236 "COOKIE-ECHO: AUTH failed\n");
2237 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2238 atomic_add_int(&stcb->asoc.refcnt, 1);
2239 SCTP_TCB_UNLOCK(stcb);
2240 SCTP_SOCKET_LOCK(so, 1);
2241 SCTP_TCB_LOCK(stcb);
2242 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2244 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2245 SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2246 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2247 SCTP_SOCKET_UNLOCK(so, 1);
2251 /* remaining chunks checked... good to go */
2252 stcb->asoc.authenticated = 1;
2256 * if we're doing ASCONFs, check to see if we have any new local
2257 * addresses that need to get added to the peer (eg. addresses
2258 * changed while cookie echo in flight). This needs to be done
2259 * after we go to the OPEN state to do the correct asconf
2260 * processing. else, make sure we have the correct addresses in our
2264 /* warning, we re-use sin, sin6, sa_store here! */
2265 /* pull in local_address (our "from" address) */
2266 switch (cookie->laddr_type) {
2268 case SCTP_IPV4_ADDRESS:
2269 /* source addr is IPv4 */
2270 memset(&store.sin, 0, sizeof(struct sockaddr_in));
2271 store.sin.sin_family = AF_INET;
2272 store.sin.sin_len = sizeof(struct sockaddr_in);
2273 store.sin.sin_addr.s_addr = cookie->laddress[0];
2277 case SCTP_IPV6_ADDRESS:
2278 /* source addr is IPv6 */
2279 memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
2280 store.sin6.sin6_family = AF_INET6;
2281 store.sin6.sin6_len = sizeof(struct sockaddr_in6);
2282 store.sin6.sin6_scope_id = cookie->scope_id;
2283 memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2287 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2288 atomic_add_int(&stcb->asoc.refcnt, 1);
2289 SCTP_TCB_UNLOCK(stcb);
2290 SCTP_SOCKET_LOCK(so, 1);
2291 SCTP_TCB_LOCK(stcb);
2292 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2294 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2295 SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2296 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2297 SCTP_SOCKET_UNLOCK(so, 1);
2302 /* update current state */
2303 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2304 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2305 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2306 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2307 stcb->sctp_ep, stcb, asoc->primary_destination);
2309 sctp_stop_all_cookie_timers(stcb);
2310 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2311 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2313 /* set up to notify upper layer */
2314 *notification = SCTP_NOTIFY_ASSOC_UP;
2315 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2316 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2317 (!SCTP_IS_LISTENING(inp))) {
2319 * This is an endpoint that called connect() how it got a
2320 * cookie that is NEW is a bit of a mystery. It must be that
2321 * the INIT was sent, but before it got there.. a complete
2322 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2323 * should have went to the other code.. not here.. oh well..
2324 * a bit of protection is worth having..
2326 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2327 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2328 atomic_add_int(&stcb->asoc.refcnt, 1);
2329 SCTP_TCB_UNLOCK(stcb);
2330 SCTP_SOCKET_LOCK(so, 1);
2331 SCTP_TCB_LOCK(stcb);
2332 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2333 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2334 SCTP_SOCKET_UNLOCK(so, 1);
2338 soisconnected(stcb->sctp_socket);
2339 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2340 SCTP_SOCKET_UNLOCK(so, 1);
2342 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2343 (SCTP_IS_LISTENING(inp))) {
2345 * We don't want to do anything with this one. Since it is
2346 * the listening guy. The timer will get started for
2347 * accepted connections in the caller.
2351 /* since we did not send a HB make sure we don't double things */
2352 if ((netp) && (*netp))
2353 (*netp)->hb_responded = 1;
2355 if (stcb->asoc.sctp_autoclose_ticks &&
2356 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2357 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2359 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2360 if ((netp != NULL) && (*netp != NULL)) {
2361 /* calculate the RTT and set the encaps port */
2362 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2363 &cookie->time_entered, sctp_align_unsafe_makecopy,
2364 SCTP_RTT_FROM_NON_DATA);
2366 /* respond with a COOKIE-ACK */
2367 sctp_send_cookie_ack(stcb);
2370 * check the address lists for any ASCONFs that need to be sent
2371 * AFTER the cookie-ack is sent
2373 sctp_check_address_list(stcb, m,
2374 initack_offset + sizeof(struct sctp_init_ack_chunk),
2375 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2376 &store.sa, cookie->local_scope, cookie->site_scope,
2377 cookie->ipv4_scope, cookie->loopback_scope);
2384 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2385 * we NEED to make sure we are not already using the vtag. If so we
2386 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2387 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2388 SCTP_BASE_INFO(hashasocmark))];
2389 LIST_FOREACH(stcb, head, sctp_asocs) {
2390 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2391 -- SEND ABORT - TRY AGAIN --
2397 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2398 * existing (non-NULL) TCB
2400 static struct mbuf *
2401 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2402 struct sockaddr *src, struct sockaddr *dst,
2403 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2404 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2405 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2406 struct sctp_tcb **locked_tcb,
2407 uint8_t mflowtype, uint32_t mflowid,
2408 uint32_t vrf_id, uint16_t port)
2410 struct sctp_state_cookie *cookie;
2411 struct sctp_tcb *l_stcb = *stcb;
2412 struct sctp_inpcb *l_inp;
2413 struct sockaddr *to;
2414 struct sctp_pcb *ep;
2416 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2418 uint8_t cookie_ok = 0;
2419 unsigned int sig_offset, cookie_offset;
2420 unsigned int cookie_len;
2422 struct timeval time_expires;
2423 int notification = 0;
2424 struct sctp_nets *netl;
2425 int had_a_existing_tcb = 0;
2426 int send_int_conf = 0;
2428 struct sockaddr_in sin;
2431 struct sockaddr_in6 sin6;
2434 SCTPDBG(SCTP_DEBUG_INPUT2,
2435 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2437 if (inp_p == NULL) {
2440 cookie = &cp->cookie;
2441 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2442 cookie_len = ntohs(cp->ch.chunk_length);
2444 if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2445 sizeof(struct sctp_init_chunk) +
2446 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2447 /* cookie too small */
2450 if ((cookie->peerport != sh->src_port) ||
2451 (cookie->myport != sh->dest_port) ||
2452 (cookie->my_vtag != sh->v_tag)) {
2454 * invalid ports or bad tag. Note that we always leave the
2455 * v_tag in the header in network order and when we stored
2456 * it in the my_vtag slot we also left it in network order.
2457 * This maintains the match even though it may be in the
2458 * opposite byte order of the machine :->
2463 * split off the signature into its own mbuf (since it should not be
2464 * calculated in the sctp_hmac_m() call).
2466 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2467 m_sig = m_split(m, sig_offset, M_NOWAIT);
2468 if (m_sig == NULL) {
2469 /* out of memory or ?? */
2472 #ifdef SCTP_MBUF_LOGGING
2473 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2474 sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
2479 * compute the signature/digest for the cookie
2481 ep = &(*inp_p)->sctp_ep;
2484 SCTP_TCB_UNLOCK(l_stcb);
2486 SCTP_INP_RLOCK(l_inp);
2488 SCTP_TCB_LOCK(l_stcb);
2490 /* which cookie is it? */
2491 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2492 (ep->current_secret_number != ep->last_secret_number)) {
2493 /* it's the old cookie */
2494 (void)sctp_hmac_m(SCTP_HMAC,
2495 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2496 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2498 /* it's the current cookie */
2499 (void)sctp_hmac_m(SCTP_HMAC,
2500 (uint8_t *)ep->secret_key[(int)ep->current_secret_number],
2501 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2503 /* get the signature */
2504 SCTP_INP_RUNLOCK(l_inp);
2505 sig = (uint8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *)&tmp_sig);
2507 /* couldn't find signature */
2508 sctp_m_freem(m_sig);
2511 /* compare the received digest with the computed digest */
2512 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2513 /* try the old cookie? */
2514 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2515 (ep->current_secret_number != ep->last_secret_number)) {
2516 /* compute digest with old */
2517 (void)sctp_hmac_m(SCTP_HMAC,
2518 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2519 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2521 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2529 * Now before we continue we must reconstruct our mbuf so that
2530 * normal processing of any other chunks will work.
2536 while (SCTP_BUF_NEXT(m_at) != NULL) {
2537 m_at = SCTP_BUF_NEXT(m_at);
2539 SCTP_BUF_NEXT(m_at) = m_sig;
2542 if (cookie_ok == 0) {
2543 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2544 SCTPDBG(SCTP_DEBUG_INPUT2,
2545 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2546 (uint32_t)offset, cookie_offset, sig_offset);
2550 * check the cookie timestamps to be sure it's not stale
2552 (void)SCTP_GETTIME_TIMEVAL(&now);
2553 /* Expire time is in Ticks, so we convert to seconds */
2554 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2555 time_expires.tv_usec = cookie->time_entered.tv_usec;
2556 if (timevalcmp(&now, &time_expires, >)) {
2557 /* cookie is stale! */
2558 struct mbuf *op_err;
2559 struct sctp_error_stale_cookie *cause;
2560 struct timeval diff;
2563 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2564 0, M_NOWAIT, 1, MT_DATA);
2565 if (op_err == NULL) {
2570 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
2571 cause = mtod(op_err, struct sctp_error_stale_cookie *);
2572 cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
2573 cause->cause.length = htons((sizeof(struct sctp_paramhdr) +
2574 (sizeof(uint32_t))));
2576 timevalsub(&diff, &time_expires);
2577 if (diff.tv_sec > UINT32_MAX / 1000000) {
2578 staleness = UINT32_MAX;
2580 staleness = diff.tv_sec * 1000000;
2582 if (UINT32_MAX - staleness >= diff.tv_usec) {
2583 staleness += diff.tv_usec;
2585 staleness = UINT32_MAX;
2587 cause->stale_time = htonl(staleness);
2588 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2589 mflowtype, mflowid, l_inp->fibnum,
2594 * Now we must see with the lookup address if we have an existing
2595 * asoc. This will only happen if we were in the COOKIE-WAIT state
2596 * and a INIT collided with us and somewhere the peer sent the
2597 * cookie on another address besides the single address our assoc
2598 * had for him. In this case we will have one of the tie-tags set at
2599 * least AND the address field in the cookie can be used to look it
2603 switch (cookie->addr_type) {
2605 case SCTP_IPV6_ADDRESS:
2606 memset(&sin6, 0, sizeof(sin6));
2607 sin6.sin6_family = AF_INET6;
2608 sin6.sin6_len = sizeof(sin6);
2609 sin6.sin6_port = sh->src_port;
2610 sin6.sin6_scope_id = cookie->scope_id;
2611 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2612 sizeof(sin6.sin6_addr.s6_addr));
2613 to = (struct sockaddr *)&sin6;
2617 case SCTP_IPV4_ADDRESS:
2618 memset(&sin, 0, sizeof(sin));
2619 sin.sin_family = AF_INET;
2620 sin.sin_len = sizeof(sin);
2621 sin.sin_port = sh->src_port;
2622 sin.sin_addr.s_addr = cookie->address[0];
2623 to = (struct sockaddr *)&sin;
2627 /* This should not happen */
2630 if (*stcb == NULL) {
2631 /* Yep, lets check */
2632 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2633 if (*stcb == NULL) {
2635 * We should have only got back the same inp. If we
2636 * got back a different ep we have a problem. The
2637 * original findep got back l_inp and now
2639 if (l_inp != *inp_p) {
2640 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2643 if (*locked_tcb == NULL) {
2645 * In this case we found the assoc only
2646 * after we locked the create lock. This
2647 * means we are in a colliding case and we
2648 * must make sure that we unlock the tcb if
2649 * its one of the cases where we throw away
2650 * the incoming packets.
2652 *locked_tcb = *stcb;
2655 * We must also increment the inp ref count
2656 * since the ref_count flags was set when we
2657 * did not find the TCB, now we found it
2658 * which reduces the refcount.. we must
2659 * raise it back out to balance it all :-)
2661 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2662 if ((*stcb)->sctp_ep != l_inp) {
2663 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2664 (void *)(*stcb)->sctp_ep, (void *)l_inp);
2669 cookie_len -= SCTP_SIGNATURE_SIZE;
2670 if (*stcb == NULL) {
2671 /* this is the "normal" case... get a new TCB */
2672 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2673 cookie, cookie_len, *inp_p,
2674 netp, to, ¬ification,
2675 auth_skipped, auth_offset, auth_len,
2679 /* this is abnormal... cookie-echo on existing TCB */
2680 had_a_existing_tcb = 1;
2681 *stcb = sctp_process_cookie_existing(m, iphlen, offset,
2683 cookie, cookie_len, *inp_p, *stcb, netp, to,
2684 ¬ification, auth_skipped, auth_offset, auth_len,
2689 if (*stcb == NULL) {
2690 /* still no TCB... must be bad cookie-echo */
2693 if (*netp != NULL) {
2694 (*netp)->flowtype = mflowtype;
2695 (*netp)->flowid = mflowid;
2698 * Ok, we built an association so confirm the address we sent the
2701 netl = sctp_findnet(*stcb, to);
2703 * This code should in theory NOT run but
2706 /* TSNH! Huh, why do I need to add this address here? */
2707 if (sctp_add_remote_addr(*stcb, to, NULL, port,
2708 SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2711 netl = sctp_findnet(*stcb, to);
2714 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2715 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2716 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2721 sctp_start_net_timers(*stcb);
2722 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2723 if (!had_a_existing_tcb ||
2724 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2726 * If we have a NEW cookie or the connect never
2727 * reached the connected state during collision we
2728 * must do the TCP accept thing.
2730 struct socket *so, *oso;
2731 struct sctp_inpcb *inp;
2733 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2735 * For a restart we will keep the same
2736 * socket, no need to do anything. I THINK!!
2738 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2739 if (send_int_conf) {
2740 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2741 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2745 oso = (*inp_p)->sctp_socket;
2746 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2747 SCTP_TCB_UNLOCK((*stcb));
2748 CURVNET_SET(oso->so_vnet);
2749 so = sonewconn(oso, 0
2752 SCTP_TCB_LOCK((*stcb));
2753 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2756 struct mbuf *op_err;
2757 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2758 struct socket *pcb_so;
2760 /* Too many sockets */
2761 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2762 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2763 sctp_abort_association(*inp_p, NULL, m, iphlen,
2764 src, dst, sh, op_err,
2767 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2768 pcb_so = SCTP_INP_SO(*inp_p);
2769 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2770 SCTP_TCB_UNLOCK((*stcb));
2771 SCTP_SOCKET_LOCK(pcb_so, 1);
2772 SCTP_TCB_LOCK((*stcb));
2773 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2775 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2776 SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2777 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2778 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2782 inp = (struct sctp_inpcb *)so->so_pcb;
2783 SCTP_INP_INCR_REF(inp);
2785 * We add the unbound flag here so that if we get an
2786 * soabort() before we get the move_pcb done, we
2787 * will properly cleanup.
2789 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2790 SCTP_PCB_FLAGS_CONNECTED |
2791 SCTP_PCB_FLAGS_IN_TCPPOOL |
2792 SCTP_PCB_FLAGS_UNBOUND |
2793 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2794 SCTP_PCB_FLAGS_DONT_WAKE);
2795 inp->sctp_features = (*inp_p)->sctp_features;
2796 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2797 inp->sctp_socket = so;
2798 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2799 inp->max_cwnd = (*inp_p)->max_cwnd;
2800 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2801 inp->ecn_supported = (*inp_p)->ecn_supported;
2802 inp->prsctp_supported = (*inp_p)->prsctp_supported;
2803 inp->auth_supported = (*inp_p)->auth_supported;
2804 inp->asconf_supported = (*inp_p)->asconf_supported;
2805 inp->reconfig_supported = (*inp_p)->reconfig_supported;
2806 inp->nrsack_supported = (*inp_p)->nrsack_supported;
2807 inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2808 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2809 inp->sctp_context = (*inp_p)->sctp_context;
2810 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2811 inp->fibnum = (*inp_p)->fibnum;
2812 inp->inp_starting_point_for_iterator = NULL;
2814 * copy in the authentication parameters from the
2817 if (inp->sctp_ep.local_hmacs)
2818 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2819 inp->sctp_ep.local_hmacs =
2820 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2821 if (inp->sctp_ep.local_auth_chunks)
2822 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2823 inp->sctp_ep.local_auth_chunks =
2824 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2827 * Now we must move it from one hash table to
2828 * another and get the tcb in the right place.
2832 * This is where the one-2-one socket is put into
2833 * the accept state waiting for the accept!
2836 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2838 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2840 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2841 SCTP_TCB_UNLOCK((*stcb));
2843 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2845 SCTP_TCB_LOCK((*stcb));
2846 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2850 * now we must check to see if we were aborted while
2851 * the move was going on and the lock/unlock
2854 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2856 * yep it was, we leave the assoc attached
2857 * to the socket since the sctp_inpcb_free()
2858 * call will send an abort for us.
2860 SCTP_INP_DECR_REF(inp);
2863 SCTP_INP_DECR_REF(inp);
2864 /* Switch over to the new guy */
2866 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2867 if (send_int_conf) {
2868 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2869 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2872 * Pull it from the incomplete queue and wake the
2875 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2876 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2877 SCTP_TCB_UNLOCK((*stcb));
2878 SCTP_SOCKET_LOCK(so, 1);
2881 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2882 SCTP_TCB_LOCK((*stcb));
2883 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2884 SCTP_SOCKET_UNLOCK(so, 1);
2890 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2892 if (send_int_conf) {
2893 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2894 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2900 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2901 struct sctp_tcb *stcb, struct sctp_nets *net)
2903 /* cp must not be used, others call this without a c-ack :-) */
2904 struct sctp_association *asoc;
2906 SCTPDBG(SCTP_DEBUG_INPUT2,
2907 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2908 if ((stcb == NULL) || (net == NULL)) {
2913 sctp_stop_all_cookie_timers(stcb);
2914 /* process according to association state */
2915 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2916 /* state change only needed when I am in right state */
2917 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2918 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2919 sctp_start_net_timers(stcb);
2920 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2921 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2922 stcb->sctp_ep, stcb, asoc->primary_destination);
2926 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2927 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2928 if (asoc->overall_error_count == 0) {
2929 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2930 &asoc->time_entered, sctp_align_safe_nocopy,
2931 SCTP_RTT_FROM_NON_DATA);
2933 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2934 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2935 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2936 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2937 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2941 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2942 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2943 so = SCTP_INP_SO(stcb->sctp_ep);
2944 atomic_add_int(&stcb->asoc.refcnt, 1);
2945 SCTP_TCB_UNLOCK(stcb);
2946 SCTP_SOCKET_LOCK(so, 1);
2947 SCTP_TCB_LOCK(stcb);
2948 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2950 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2951 soisconnected(stcb->sctp_socket);
2953 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2954 SCTP_SOCKET_UNLOCK(so, 1);
2958 * since we did not send a HB make sure we don't double
2961 net->hb_responded = 1;
2963 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2965 * We don't need to do the asconf thing, nor hb or
2966 * autoclose if the socket is closed.
2970 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2974 if (stcb->asoc.sctp_autoclose_ticks &&
2975 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2976 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2977 stcb->sctp_ep, stcb, NULL);
2980 * send ASCONF if parameters are pending and ASCONFs are
2981 * allowed (eg. addresses changed when init/cookie echo were
2984 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2985 (stcb->asoc.asconf_supported == 1) &&
2986 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2987 #ifdef SCTP_TIMER_BASED_ASCONF
2988 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2989 stcb->sctp_ep, stcb,
2990 stcb->asoc.primary_destination);
2992 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2993 SCTP_ADDR_NOT_LOCKED);
2998 /* Toss the cookie if I can */
2999 sctp_toss_old_cookies(stcb, asoc);
3000 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3001 /* Restart the timer if we have pending data */
3002 struct sctp_tmit_chunk *chk;
3004 chk = TAILQ_FIRST(&asoc->sent_queue);
3005 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
3010 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
3011 struct sctp_tcb *stcb)
3013 struct sctp_nets *net;
3014 struct sctp_tmit_chunk *lchk;
3015 struct sctp_ecne_chunk bkup;
3016 uint8_t override_bit;
3017 uint32_t tsn, window_data_tsn;
3019 unsigned int pkt_cnt;
3021 len = ntohs(cp->ch.chunk_length);
3022 if ((len != sizeof(struct sctp_ecne_chunk)) &&
3023 (len != sizeof(struct old_sctp_ecne_chunk))) {
3026 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3027 /* Its the old format */
3028 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3029 bkup.num_pkts_since_cwr = htonl(1);
3032 SCTP_STAT_INCR(sctps_recvecne);
3033 tsn = ntohl(cp->tsn);
3034 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3035 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3037 window_data_tsn = stcb->asoc.sending_seq - 1;
3039 window_data_tsn = lchk->rec.data.tsn;
3042 /* Find where it was sent to if possible. */
3044 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3045 if (lchk->rec.data.tsn == tsn) {
3047 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3050 if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) {
3056 * What to do. A previous send of a CWR was possibly lost.
3057 * See how old it is, we may have it marked on the actual
3060 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3061 if (tsn == net->last_cwr_tsn) {
3062 /* Found him, send it off */
3068 * If we reach here, we need to send a special CWR
3069 * that says hey, we did this a long time ago and
3070 * you lost the response.
3072 net = TAILQ_FIRST(&stcb->asoc.nets);
3077 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3084 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3085 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3087 * JRS - Use the congestion control given in the pluggable
3090 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3092 * We reduce once every RTT. So we will only lower cwnd at
3093 * the next sending seq i.e. the window_data_tsn
3095 net->cwr_window_tsn = window_data_tsn;
3096 net->ecn_ce_pkt_cnt += pkt_cnt;
3097 net->lost_cnt = pkt_cnt;
3098 net->last_cwr_tsn = tsn;
3100 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3101 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3102 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3104 * Another loss in the same window update how many
3105 * marks/packets lost we have had.
3109 if (pkt_cnt > net->lost_cnt) {
3110 /* Should be the case */
3111 cnt = (pkt_cnt - net->lost_cnt);
3112 net->ecn_ce_pkt_cnt += cnt;
3114 net->lost_cnt = pkt_cnt;
3115 net->last_cwr_tsn = tsn;
3117 * Most CC functions will ignore this call, since we
3118 * are in-window yet of the initial CE the peer saw.
3120 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3124 * We always send a CWR this way if our previous one was lost our
3125 * peer will get an update, or if it is not time again to reduce we
3126 * still get the cwr to the peer. Note we set the override when we
3127 * could not find the TSN on the chunk or the destination network.
3129 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3133 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3136 * Here we get a CWR from the peer. We must look in the outqueue and
3137 * make sure that we have a covered ECNE in the control chunk part.
3140 struct sctp_tmit_chunk *chk, *nchk;
3141 struct sctp_ecne_chunk *ecne;
3145 cwr_tsn = ntohl(cp->tsn);
3146 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3147 TAILQ_FOREACH_SAFE(chk, &stcb->asoc.control_send_queue, sctp_next, nchk) {
3148 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3151 if ((override == 0) && (chk->whoTo != net)) {
3152 /* Must be from the right src unless override is set */
3155 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3156 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3157 /* this covers this ECNE, we can remove it */
3158 stcb->asoc.ecn_echo_cnt_onq--;
3159 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3161 sctp_m_freem(chk->data);
3163 stcb->asoc.ctrl_queue_cnt--;
3164 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3165 if (override == 0) {
3173 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3174 struct sctp_tcb *stcb, struct sctp_nets *net)
3176 struct sctp_association *asoc;
3177 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3181 SCTPDBG(SCTP_DEBUG_INPUT2,
3182 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3187 /* process according to association state */
3188 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3189 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3190 SCTPDBG(SCTP_DEBUG_INPUT2,
3191 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3192 SCTP_TCB_UNLOCK(stcb);
3195 /* notify upper layer protocol */
3196 if (stcb->sctp_socket) {
3197 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3200 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3201 !TAILQ_EMPTY(&asoc->sent_queue) ||
3202 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
3203 panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3206 /* stop the timer */
3207 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3208 SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3209 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3211 SCTPDBG(SCTP_DEBUG_INPUT2,
3212 "sctp_handle_shutdown_complete: calls free-asoc\n");
3213 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3214 so = SCTP_INP_SO(stcb->sctp_ep);
3215 atomic_add_int(&stcb->asoc.refcnt, 1);
3216 SCTP_TCB_UNLOCK(stcb);
3217 SCTP_SOCKET_LOCK(so, 1);
3218 SCTP_TCB_LOCK(stcb);
3219 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3221 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3222 SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3223 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3224 SCTP_SOCKET_UNLOCK(so, 1);
3230 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3231 struct sctp_nets *net, uint8_t flg)
3233 switch (desc->chunk_type) {
3235 /* find the tsn to resend (possibly */
3238 struct sctp_tmit_chunk *tp1;
3240 tsn = ntohl(desc->tsn_ifany);
3241 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3242 if (tp1->rec.data.tsn == tsn) {
3246 if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) {
3254 * Do it the other way , aka without paying
3255 * attention to queue seq order.
3257 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3258 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3259 if (tp1->rec.data.tsn == tsn) {
3266 SCTP_STAT_INCR(sctps_pdrptsnnf);
3268 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3271 if (((flg & SCTP_BADCRC) == 0) &&
3272 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3275 if ((stcb->asoc.peers_rwnd == 0) &&
3276 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3277 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3280 if (stcb->asoc.peers_rwnd == 0 &&
3281 (flg & SCTP_FROM_MIDDLE_BOX)) {
3282 SCTP_STAT_INCR(sctps_pdrpdizrw);
3285 ddp = (uint8_t *)(mtod(tp1->data, caddr_t)+
3286 sizeof(struct sctp_data_chunk));
3290 for (iii = 0; iii < sizeof(desc->data_bytes);
3292 if (ddp[iii] != desc->data_bytes[iii]) {
3293 SCTP_STAT_INCR(sctps_pdrpbadd);
3301 * this guy had a RTO calculation
3302 * pending on it, cancel it
3304 if (tp1->whoTo->rto_needed == 0) {
3305 tp1->whoTo->rto_needed = 1;
3309 SCTP_STAT_INCR(sctps_pdrpmark);
3310 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3311 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3313 * mark it as if we were doing a FR, since
3314 * we will be getting gap ack reports behind
3315 * the info from the router.
3317 tp1->rec.data.doing_fast_retransmit = 1;
3319 * mark the tsn with what sequences can
3322 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3323 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3325 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
3328 /* restart the timer */
3329 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3331 SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3332 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3335 /* fix counts and things */
3336 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3337 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3338 tp1->whoTo->flight_size,
3340 (uint32_t)(uintptr_t)stcb,
3343 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3344 sctp_flight_size_decrease(tp1);
3345 sctp_total_flight_decrease(stcb, tp1);
3347 tp1->sent = SCTP_DATAGRAM_RESEND;
3353 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3354 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3357 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3359 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3362 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3363 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3364 audit, stcb->asoc.sent_queue_retran_cnt);
3365 #ifndef SCTP_AUDITING_ENABLED
3366 stcb->asoc.sent_queue_retran_cnt = audit;
3374 struct sctp_tmit_chunk *asconf;
3376 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3378 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3383 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3384 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3385 asconf->sent = SCTP_DATAGRAM_RESEND;
3386 asconf->snd_count--;
3390 case SCTP_INITIATION:
3391 /* resend the INIT */
3392 stcb->asoc.dropped_special_cnt++;
3393 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3395 * If we can get it in, in a few attempts we do
3396 * this, otherwise we let the timer fire.
3398 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3400 SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3401 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3404 case SCTP_SELECTIVE_ACK:
3405 case SCTP_NR_SELECTIVE_ACK:
3406 /* resend the sack */
3407 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3409 case SCTP_HEARTBEAT_REQUEST:
3410 /* resend a demand HB */
3411 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3413 * Only retransmit if we KNOW we wont destroy the
3416 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3420 sctp_send_shutdown(stcb, net);
3422 case SCTP_SHUTDOWN_ACK:
3423 sctp_send_shutdown_ack(stcb, net);
3425 case SCTP_COOKIE_ECHO:
3427 struct sctp_tmit_chunk *cookie;
3430 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3432 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3437 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3438 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3439 cookie->sent = SCTP_DATAGRAM_RESEND;
3440 sctp_stop_all_cookie_timers(stcb);
3444 case SCTP_COOKIE_ACK:
3445 sctp_send_cookie_ack(stcb);
3447 case SCTP_ASCONF_ACK:
3448 /* resend last asconf ack */
3449 sctp_send_asconf_ack(stcb);
3451 case SCTP_IFORWARD_CUM_TSN:
3452 case SCTP_FORWARD_CUM_TSN:
3453 send_forward_tsn(stcb, &stcb->asoc);
3455 /* can't do anything with these */
3456 case SCTP_PACKET_DROPPED:
3457 case SCTP_INITIATION_ACK: /* this should not happen */
3458 case SCTP_HEARTBEAT_ACK:
3459 case SCTP_ABORT_ASSOCIATION:
3460 case SCTP_OPERATION_ERROR:
3461 case SCTP_SHUTDOWN_COMPLETE:
3471 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3477 * We set things to 0xffffffff since this is the last delivered
3478 * sequence and we will be sending in 0 after the reset.
3481 if (number_entries) {
3482 for (i = 0; i < number_entries; i++) {
3483 temp = ntohs(list[i]);
3484 if (temp >= stcb->asoc.streamincnt) {
3487 stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff;
3491 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3492 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
3495 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3499 sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3504 if (number_entries > 0) {
3505 for (i = 0; i < number_entries; i++) {
3506 temp = ntohs(list[i]);
3507 if (temp >= stcb->asoc.streamoutcnt) {
3508 /* no such stream */
3511 stcb->asoc.strmout[temp].next_mid_ordered = 0;
3512 stcb->asoc.strmout[temp].next_mid_unordered = 0;
3515 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3516 stcb->asoc.strmout[i].next_mid_ordered = 0;
3517 stcb->asoc.strmout[i].next_mid_unordered = 0;
3520 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3524 sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3529 if (number_entries > 0) {
3530 for (i = 0; i < number_entries; i++) {
3531 temp = ntohs(list[i]);
3532 if (temp >= stcb->asoc.streamoutcnt) {
3533 /* no such stream */
3536 stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
3539 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3540 stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
3546 struct sctp_stream_reset_request *
3547 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3549 struct sctp_association *asoc;
3550 struct sctp_chunkhdr *ch;
3551 struct sctp_stream_reset_request *r;
3552 struct sctp_tmit_chunk *chk;
3556 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3557 asoc->stream_reset_outstanding = 0;
3560 if (stcb->asoc.str_reset == NULL) {
3561 asoc->stream_reset_outstanding = 0;
3564 chk = stcb->asoc.str_reset;
3565 if (chk->data == NULL) {
3569 /* he wants a copy of the chk pointer */
3572 clen = chk->send_size;
3573 ch = mtod(chk->data, struct sctp_chunkhdr *);
3574 r = (struct sctp_stream_reset_request *)(ch + 1);
3575 if (ntohl(r->request_seq) == seq) {
3579 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3580 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3581 /* move to the next one, there can only be a max of two */
3582 r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3583 if (ntohl(r->request_seq) == seq) {
3587 /* that seq is not here */
3592 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3594 struct sctp_association *asoc;
3595 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3597 if (stcb->asoc.str_reset == NULL) {
3602 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
3603 chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3604 TAILQ_REMOVE(&asoc->control_send_queue,
3608 sctp_m_freem(chk->data);
3611 asoc->ctrl_queue_cnt--;
3612 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3613 /* sa_ignore NO_NULL_CHK */
3614 stcb->asoc.str_reset = NULL;
3619 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3620 uint32_t seq, uint32_t action,
3621 struct sctp_stream_reset_response *respin)
3625 struct sctp_association *asoc = &stcb->asoc;
3626 struct sctp_tmit_chunk *chk;
3627 struct sctp_stream_reset_request *req_param;
3628 struct sctp_stream_reset_out_request *req_out_param;
3629 struct sctp_stream_reset_in_request *req_in_param;
3630 uint32_t number_entries;
3632 if (asoc->stream_reset_outstanding == 0) {
3636 if (seq == stcb->asoc.str_reset_seq_out) {
3637 req_param = sctp_find_stream_reset(stcb, seq, &chk);
3638 if (req_param != NULL) {
3639 stcb->asoc.str_reset_seq_out++;
3640 type = ntohs(req_param->ph.param_type);
3641 lparam_len = ntohs(req_param->ph.param_length);
3642 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3645 req_out_param = (struct sctp_stream_reset_out_request *)req_param;
3646 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3647 asoc->stream_reset_out_is_outstanding = 0;
3648 if (asoc->stream_reset_outstanding)
3649 asoc->stream_reset_outstanding--;
3650 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3652 sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3653 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3654 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3655 } else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
3657 * Set it up so we don't stop
3660 asoc->stream_reset_outstanding++;
3661 stcb->asoc.str_reset_seq_out--;
3662 asoc->stream_reset_out_is_outstanding = 1;
3665 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3667 if (no_clear == 0) {
3668 sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
3670 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3671 req_in_param = (struct sctp_stream_reset_in_request *)req_param;
3672 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3673 if (asoc->stream_reset_outstanding)
3674 asoc->stream_reset_outstanding--;
3675 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3676 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3677 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3678 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3679 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3680 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3682 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3683 /* Ok we now may have more streams */
3686 num_stream = stcb->asoc.strm_pending_add_size;
3687 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3689 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3691 stcb->asoc.strm_pending_add_size = 0;
3692 if (asoc->stream_reset_outstanding)
3693 asoc->stream_reset_outstanding--;
3694 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3695 /* Put the new streams into effect */
3698 for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
3699 asoc->strmout[i].state = SCTP_STREAM_OPEN;
3701 asoc->streamoutcnt += num_stream;
3702 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3703 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3704 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3705 SCTP_STREAM_CHANGE_DENIED);
3707 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3708 SCTP_STREAM_CHANGE_FAILED);
3710 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3711 if (asoc->stream_reset_outstanding)
3712 asoc->stream_reset_outstanding--;
3713 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3714 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3715 SCTP_STREAM_CHANGE_DENIED);
3716 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3717 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3718 SCTP_STREAM_CHANGE_FAILED);
3720 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3722 * a) Adopt the new in tsn.
3724 * c) Adopt the new out-tsn
3726 struct sctp_stream_reset_response_tsn *resp;
3727 struct sctp_forward_tsn_chunk fwdtsn;
3730 if (respin == NULL) {
3734 if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
3737 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3738 resp = (struct sctp_stream_reset_response_tsn *)respin;
3739 asoc->stream_reset_outstanding--;
3740 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3741 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3742 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3743 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3747 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3748 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3749 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3751 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3752 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3753 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3755 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3756 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3758 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3759 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3761 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3762 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3763 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3764 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3765 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3766 SCTP_ASSOC_RESET_DENIED);
3768 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3769 SCTP_ASSOC_RESET_FAILED);
3772 /* get rid of the request and get the request flags */
3773 if (asoc->stream_reset_outstanding == 0) {
3774 sctp_clean_up_stream_reset(stcb);
3778 if (asoc->stream_reset_outstanding == 0) {
3779 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3785 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3786 struct sctp_tmit_chunk *chk,
3787 struct sctp_stream_reset_in_request *req, int trunc)
3795 * peer wants me to send a str-reset to him for my outgoing seq's if
3798 struct sctp_association *asoc = &stcb->asoc;
3800 seq = ntohl(req->request_seq);
3801 if (asoc->str_reset_seq_in == seq) {
3802 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3803 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3804 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3806 /* Can't do it, since they exceeded our buffer size */
3807 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3808 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3809 len = ntohs(req->ph.param_length);
3810 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3811 if (number_entries) {
3812 for (i = 0; i < number_entries; i++) {
3813 temp = ntohs(req->list_of_streams[i]);
3814 if (temp >= stcb->asoc.streamoutcnt) {
3815 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3818 req->list_of_streams[i] = temp;
3820 for (i = 0; i < number_entries; i++) {
3821 if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
3822 stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
3827 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3828 if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
3829 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
3832 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3834 /* Can't do it, since we have sent one out */
3835 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3838 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3839 asoc->str_reset_seq_in++;
3840 } else if (asoc->str_reset_seq_in - 1 == seq) {
3841 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3842 } else if (asoc->str_reset_seq_in - 2 == seq) {
3843 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3845 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3847 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3851 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3852 struct sctp_tmit_chunk *chk,
3853 struct sctp_stream_reset_tsn_request *req)
3855 /* reset all in and out and update the tsn */
3857 * A) reset my str-seq's on in and out. B) Select a receive next,
3858 * and set cum-ack to it. Also process this selected number as a
3859 * fwd-tsn as well. C) set in the response my next sending seq.
3861 struct sctp_forward_tsn_chunk fwdtsn;
3862 struct sctp_association *asoc = &stcb->asoc;
3866 seq = ntohl(req->request_seq);
3867 if (asoc->str_reset_seq_in == seq) {
3868 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3869 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3870 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3872 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3873 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3874 fwdtsn.ch.chunk_flags = 0;
3875 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3876 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3880 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3881 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3882 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3884 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3885 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3886 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3887 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3888 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3889 atomic_add_int(&asoc->sending_seq, 1);
3890 /* save off historical data for retrans */
3891 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3892 asoc->last_sending_seq[0] = asoc->sending_seq;
3893 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3894 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3895 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3896 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3897 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3898 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3900 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3901 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3902 asoc->str_reset_seq_in++;
3903 } else if (asoc->str_reset_seq_in - 1 == seq) {
3904 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3905 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3906 } else if (asoc->str_reset_seq_in - 2 == seq) {
3907 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3908 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3910 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3916 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3917 struct sctp_tmit_chunk *chk,
3918 struct sctp_stream_reset_out_request *req, int trunc)
3921 int number_entries, len;
3922 struct sctp_association *asoc = &stcb->asoc;
3924 seq = ntohl(req->request_seq);
3926 /* now if its not a duplicate we process it */
3927 if (asoc->str_reset_seq_in == seq) {
3928 len = ntohs(req->ph.param_length);
3929 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3931 * the sender is resetting, handle the list issue.. we must
3932 * a) verify if we can do the reset, if so no problem b) If
3933 * we can't do the reset we must copy the request. c) queue
3934 * it, and setup the data in processor to trigger it off
3935 * when needed and dequeue all the queued data.
3937 tsn = ntohl(req->send_reset_at_tsn);
3939 /* move the reset action back one */
3940 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3941 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3942 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3944 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3945 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3946 /* we can do it now */
3947 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3948 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3951 * we must queue it up and thus wait for the TSN's
3952 * to arrive that are at or before tsn
3954 struct sctp_stream_reset_list *liste;
3957 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3958 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3959 siz, SCTP_M_STRESET);
3960 if (liste == NULL) {
3961 /* gak out of memory */
3962 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3963 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3968 liste->number_entries = number_entries;
3969 memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
3970 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3971 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
3973 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3974 asoc->str_reset_seq_in++;
3975 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3977 * one seq back, just echo back last action since my
3978 * response was lost.
3980 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3981 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3983 * two seq back, just echo back last action since my
3984 * response was lost.
3986 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3988 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3993 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3994 struct sctp_stream_reset_add_strm *str_add)
3997 * Peer is requesting to add more streams. If its within our
3998 * max-streams we will allow it.
4000 uint32_t num_stream, i;
4002 struct sctp_association *asoc = &stcb->asoc;
4003 struct sctp_queued_to_read *ctl, *nctl;
4005 /* Get the number. */
4006 seq = ntohl(str_add->request_seq);
4007 num_stream = ntohs(str_add->number_of_streams);
4008 /* Now what would be the new total? */
4009 if (asoc->str_reset_seq_in == seq) {
4010 num_stream += stcb->asoc.streamincnt;
4011 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4012 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4013 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4014 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
4015 (num_stream > 0xffff)) {
4016 /* We must reject it they ask for to many */
4018 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4020 /* Ok, we can do that :-) */
4021 struct sctp_stream_in *oldstrm;
4023 /* save off the old */
4024 oldstrm = stcb->asoc.strmin;
4025 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
4026 (num_stream * sizeof(struct sctp_stream_in)),
4028 if (stcb->asoc.strmin == NULL) {
4029 stcb->asoc.strmin = oldstrm;
4032 /* copy off the old data */
4033 for (i = 0; i < stcb->asoc.streamincnt; i++) {
4034 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4035 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4036 stcb->asoc.strmin[i].sid = i;
4037 stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered;
4038 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
4039 stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
4040 /* now anything on those queues? */
4041 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
4042 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
4043 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
4045 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
4046 TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
4047 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
4050 /* Init the new streams */
4051 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
4052 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4053 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4054 stcb->asoc.strmin[i].sid = i;
4055 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
4056 stcb->asoc.strmin[i].pd_api_started = 0;
4057 stcb->asoc.strmin[i].delivery_started = 0;
4059 SCTP_FREE(oldstrm, SCTP_M_STRMI);
4060 /* update the size */
4061 stcb->asoc.streamincnt = num_stream;
4062 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4063 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
4065 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4066 asoc->str_reset_seq_in++;
4067 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4069 * one seq back, just echo back last action since my
4070 * response was lost.
4072 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4073 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4075 * two seq back, just echo back last action since my
4076 * response was lost.
4078 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4080 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4086 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4087 struct sctp_stream_reset_add_strm *str_add)
4090 * Peer is requesting to add more streams. If its within our
4091 * max-streams we will allow it.
4093 uint16_t num_stream;
4095 struct sctp_association *asoc = &stcb->asoc;
4097 /* Get the number. */
4098 seq = ntohl(str_add->request_seq);
4099 num_stream = ntohs(str_add->number_of_streams);
4100 /* Now what would be the new total? */
4101 if (asoc->str_reset_seq_in == seq) {
4102 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4103 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4104 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4105 } else if (stcb->asoc.stream_reset_outstanding) {
4106 /* We must reject it we have something pending */
4107 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4109 /* Ok, we can do that :-) */
4112 mychk = stcb->asoc.streamoutcnt;
4113 mychk += num_stream;
4114 if (mychk < 0x10000) {
4115 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4116 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
4117 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4120 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4123 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4124 asoc->str_reset_seq_in++;
4125 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4127 * one seq back, just echo back last action since my
4128 * response was lost.
4130 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4131 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4133 * two seq back, just echo back last action since my
4134 * response was lost.
4136 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4138 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4143 __attribute__((noinline))
4146 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4147 struct sctp_chunkhdr *ch_req)
4149 uint16_t remaining_length, param_len, ptype;
4150 struct sctp_paramhdr pstore;
4151 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4155 struct sctp_tmit_chunk *chk;
4156 struct sctp_chunkhdr *ch;
4157 struct sctp_paramhdr *ph;
4161 /* now it may be a reset or a reset-response */
4162 remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
4164 /* setup for adding the response */
4165 sctp_alloc_a_chunk(stcb, chk);
4169 chk->copy_by_ref = 0;
4170 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4171 chk->rec.chunk_id.can_take_data = 0;
4173 chk->asoc = &stcb->asoc;
4174 chk->no_fr_allowed = 0;
4175 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4176 chk->book_size_scale = 0;
4177 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4178 if (chk->data == NULL) {
4181 sctp_m_freem(chk->data);
4184 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4187 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4189 /* setup chunk parameters */
4190 chk->sent = SCTP_DATAGRAM_UNSENT;
4194 ch = mtod(chk->data, struct sctp_chunkhdr *);
4195 ch->chunk_type = SCTP_STREAM_RESET;
4196 ch->chunk_flags = 0;
4197 ch->chunk_length = htons(chk->send_size);
4198 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4199 offset += sizeof(struct sctp_chunkhdr);
4200 while (remaining_length >= sizeof(struct sctp_paramhdr)) {
4201 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
4206 param_len = ntohs(ph->param_length);
4207 if ((param_len > remaining_length) ||
4208 (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
4209 /* bad parameter length */
4212 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
4213 (uint8_t *)&cstore);
4218 ptype = ntohs(ph->param_type);
4220 if (param_len > sizeof(cstore)) {
4225 if (num_param > SCTP_MAX_RESET_PARAMS) {
4226 /* hit the max of parameters already sorry.. */
4229 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4230 struct sctp_stream_reset_out_request *req_out;
4232 if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
4235 req_out = (struct sctp_stream_reset_out_request *)ph;
4237 if (stcb->asoc.stream_reset_outstanding) {
4238 seq = ntohl(req_out->response_seq);
4239 if (seq == stcb->asoc.str_reset_seq_out) {
4241 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4244 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4245 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4246 struct sctp_stream_reset_add_strm *str_add;
4248 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4251 str_add = (struct sctp_stream_reset_add_strm *)ph;
4253 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4254 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4255 struct sctp_stream_reset_add_strm *str_add;
4257 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4260 str_add = (struct sctp_stream_reset_add_strm *)ph;
4262 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4263 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4264 struct sctp_stream_reset_in_request *req_in;
4267 req_in = (struct sctp_stream_reset_in_request *)ph;
4268 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4269 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4270 struct sctp_stream_reset_tsn_request *req_tsn;
4273 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4274 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4276 goto strres_nochunk;
4280 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4281 struct sctp_stream_reset_response *resp;
4284 if (param_len < sizeof(struct sctp_stream_reset_response)) {
4287 resp = (struct sctp_stream_reset_response *)ph;
4288 seq = ntohl(resp->response_seq);
4289 result = ntohl(resp->result);
4290 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4292 goto strres_nochunk;
4297 offset += SCTP_SIZE32(param_len);
4298 if (remaining_length >= SCTP_SIZE32(param_len)) {
4299 remaining_length -= SCTP_SIZE32(param_len);
4301 remaining_length = 0;
4305 /* we have no response free the stuff */
4306 goto strres_nochunk;
4308 /* ok we have a chunk to link in */
4309 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4312 stcb->asoc.ctrl_queue_cnt++;
4317 * Handle a router or endpoints report of a packet loss, there are two ways
4318 * to handle this, either we get the whole packet and must disect it
4319 * ourselves (possibly with truncation and or corruption) or it is a summary
4320 * from a middle box that did the disectting for us.
4323 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4324 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4326 uint32_t bottle_bw, on_queue;
4330 struct sctp_chunk_desc desc;
4331 struct sctp_chunkhdr *ch;
4333 chlen = ntohs(cp->ch.chunk_length);
4334 chlen -= sizeof(struct sctp_pktdrop_chunk);
4335 /* XXX possible chlen underflow */
4338 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4339 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4341 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4342 chlen -= sizeof(struct sctphdr);
4343 /* XXX possible chlen underflow */
4344 memset(&desc, 0, sizeof(desc));
4346 trunc_len = (uint16_t)ntohs(cp->trunc_len);
4347 if (trunc_len > limit) {
4350 /* now the chunks themselves */
4351 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4352 desc.chunk_type = ch->chunk_type;
4353 /* get amount we need to move */
4354 at = ntohs(ch->chunk_length);
4355 if (at < sizeof(struct sctp_chunkhdr)) {
4356 /* corrupt chunk, maybe at the end? */
4357 SCTP_STAT_INCR(sctps_pdrpcrupt);
4360 if (trunc_len == 0) {
4361 /* we are supposed to have all of it */
4363 /* corrupt skip it */
4364 SCTP_STAT_INCR(sctps_pdrpcrupt);
4368 /* is there enough of it left ? */
4369 if (desc.chunk_type == SCTP_DATA) {
4370 if (chlen < (sizeof(struct sctp_data_chunk) +
4371 sizeof(desc.data_bytes))) {
4375 if (chlen < sizeof(struct sctp_chunkhdr)) {
4380 if (desc.chunk_type == SCTP_DATA) {
4381 /* can we get out the tsn? */
4382 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4383 SCTP_STAT_INCR(sctps_pdrpmbda);
4385 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4387 struct sctp_data_chunk *dcp;
4391 dcp = (struct sctp_data_chunk *)ch;
4392 ddp = (uint8_t *)(dcp + 1);
4393 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4394 desc.data_bytes[iii] = ddp[iii];
4396 desc.tsn_ifany = dcp->dp.tsn;
4398 /* nope we are done. */
4399 SCTP_STAT_INCR(sctps_pdrpnedat);
4403 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4404 SCTP_STAT_INCR(sctps_pdrpmbct);
4407 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4408 SCTP_STAT_INCR(sctps_pdrppdbrk);
4411 if (SCTP_SIZE32(at) > chlen) {
4414 chlen -= SCTP_SIZE32(at);
4415 if (chlen < sizeof(struct sctp_chunkhdr)) {
4416 /* done, none left */
4419 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4421 /* Now update any rwnd --- possibly */
4422 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4423 /* From a peer, we get a rwnd report */
4426 SCTP_STAT_INCR(sctps_pdrpfehos);
4428 bottle_bw = ntohl(cp->bottle_bw);
4429 on_queue = ntohl(cp->current_onq);
4430 if (bottle_bw && on_queue) {
4431 /* a rwnd report is in here */
4432 if (bottle_bw > on_queue)
4433 a_rwnd = bottle_bw - on_queue;
4438 stcb->asoc.peers_rwnd = 0;
4440 if (a_rwnd > stcb->asoc.total_flight) {
4441 stcb->asoc.peers_rwnd =
4442 a_rwnd - stcb->asoc.total_flight;
4444 stcb->asoc.peers_rwnd = 0;
4446 if (stcb->asoc.peers_rwnd <
4447 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4448 /* SWS sender side engages */
4449 stcb->asoc.peers_rwnd = 0;
4454 SCTP_STAT_INCR(sctps_pdrpfmbox);
4457 /* now middle boxes in sat networks get a cwnd bump */
4458 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4459 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4460 (stcb->asoc.sat_network)) {
4462 * This is debatable but for sat networks it makes sense
4463 * Note if a T3 timer has went off, we will prohibit any
4464 * changes to cwnd until we exit the t3 loss recovery.
4466 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4467 net, cp, &bottle_bw, &on_queue);
4472 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4473 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4474 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4475 * length of the complete packet outputs: - length: modified to remaining
4476 * length after control processing - netp: modified to new sctp_nets after
4477 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4478 * bad packet,...) otherwise return the tcb for this packet
4481 __attribute__((noinline))
4483 static struct sctp_tcb *
4484 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4485 struct sockaddr *src, struct sockaddr *dst,
4486 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4487 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4488 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4489 uint32_t vrf_id, uint16_t port)
4491 struct sctp_association *asoc;
4492 struct mbuf *op_err;
4493 char msg[SCTP_DIAG_INFO_LEN];
4495 int num_chunks = 0; /* number of control chunks processed */
4496 uint32_t chk_length;
4498 int abort_no_unlock = 0;
4502 * How big should this be, and should it be alloc'd? Lets try the
4503 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4504 * until we get into jumbo grams and such..
4506 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4507 struct sctp_tcb *locked_tcb = stcb;
4509 uint32_t auth_offset = 0, auth_len = 0;
4510 int auth_skipped = 0;
4512 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4516 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4517 iphlen, *offset, length, (void *)stcb);
4519 /* validate chunk header length... */
4520 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4521 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4522 ntohs(ch->chunk_length));
4524 SCTP_TCB_UNLOCK(locked_tcb);
4529 * validate the verification tag
4531 vtag_in = ntohl(sh->v_tag);
4534 SCTP_TCB_LOCK_ASSERT(locked_tcb);
4536 if (ch->chunk_type == SCTP_INITIATION) {
4537 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4538 ntohs(ch->chunk_length), vtag_in);
4540 /* protocol error- silently discard... */
4541 SCTP_STAT_INCR(sctps_badvtag);
4543 SCTP_TCB_UNLOCK(locked_tcb);
4547 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4549 * If there is no stcb, skip the AUTH chunk and process
4550 * later after a stcb is found (to validate the lookup was
4553 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4555 (inp->auth_supported == 1)) {
4556 /* save this chunk for later processing */
4558 auth_offset = *offset;
4559 auth_len = ntohs(ch->chunk_length);
4561 /* (temporarily) move past this chunk */
4562 *offset += SCTP_SIZE32(auth_len);
4563 if (*offset >= length) {
4564 /* no more data left in the mbuf chain */
4567 SCTP_TCB_UNLOCK(locked_tcb);
4571 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4572 sizeof(struct sctp_chunkhdr), chunk_buf);
4578 SCTP_TCB_UNLOCK(locked_tcb);
4582 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4583 goto process_control_chunks;
4586 * first check if it's an ASCONF with an unknown src addr we
4587 * need to look inside to find the association
4589 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4590 struct sctp_chunkhdr *asconf_ch = ch;
4591 uint32_t asconf_offset = 0, asconf_len = 0;
4593 /* inp's refcount may be reduced */
4594 SCTP_INP_INCR_REF(inp);
4596 asconf_offset = *offset;
4598 asconf_len = ntohs(asconf_ch->chunk_length);
4599 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4601 stcb = sctp_findassociation_ep_asconf(m,
4604 sh, &inp, netp, vrf_id);
4607 asconf_offset += SCTP_SIZE32(asconf_len);
4608 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4609 sizeof(struct sctp_chunkhdr), chunk_buf);
4610 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4613 * reduce inp's refcount if not reduced in
4614 * sctp_findassociation_ep_asconf().
4616 SCTP_INP_DECR_REF(inp);
4621 /* now go back and verify any auth chunk to be sure */
4622 if (auth_skipped && (stcb != NULL)) {
4623 struct sctp_auth_chunk *auth;
4625 auth = (struct sctp_auth_chunk *)
4626 sctp_m_getptr(m, auth_offset,
4627 auth_len, chunk_buf);
4630 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4632 /* auth HMAC failed so dump it */
4635 SCTP_TCB_UNLOCK(locked_tcb);
4639 /* remaining chunks are HMAC checked */
4640 stcb->asoc.authenticated = 1;
4645 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4646 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4648 /* no association, so it's out of the blue... */
4649 sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4650 mflowtype, mflowid, inp->fibnum,
4654 SCTP_TCB_UNLOCK(locked_tcb);
4659 /* ABORT and SHUTDOWN can use either v_tag... */
4660 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4661 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4662 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4663 /* Take the T-bit always into account. */
4664 if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
4665 (vtag_in == asoc->my_vtag)) ||
4666 (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4667 (vtag_in == asoc->peer_vtag))) {
4670 /* drop this packet... */
4671 SCTP_STAT_INCR(sctps_badvtag);
4673 SCTP_TCB_UNLOCK(locked_tcb);
4677 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4678 if (vtag_in != asoc->my_vtag) {
4680 * this could be a stale SHUTDOWN-ACK or the
4681 * peer never got the SHUTDOWN-COMPLETE and
4682 * is still hung; we have started a new asoc
4683 * but it won't complete until the shutdown
4687 SCTP_TCB_UNLOCK(locked_tcb);
4689 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4690 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4692 sctp_handle_ootb(m, iphlen, *offset, src, dst,
4694 mflowtype, mflowid, fibnum,
4699 /* for all other chunks, vtag must match */
4700 if (vtag_in != asoc->my_vtag) {
4701 /* invalid vtag... */
4702 SCTPDBG(SCTP_DEBUG_INPUT3,
4703 "invalid vtag: %xh, expect %xh\n",
4704 vtag_in, asoc->my_vtag);
4705 SCTP_STAT_INCR(sctps_badvtag);
4707 SCTP_TCB_UNLOCK(locked_tcb);
4713 } /* end if !SCTP_COOKIE_ECHO */
4715 * process all control chunks...
4717 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4718 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4719 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4720 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4721 /* implied cookie-ack.. we must have lost the ack */
4722 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4723 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4724 stcb->asoc.overall_error_count,
4726 SCTP_FROM_SCTP_INPUT,
4729 stcb->asoc.overall_error_count = 0;
4730 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4733 process_control_chunks:
4734 while (IS_SCTP_CONTROL(ch)) {
4735 /* validate chunk length */
4736 chk_length = ntohs(ch->chunk_length);
4737 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4738 ch->chunk_type, chk_length);
4739 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4740 if (chk_length < sizeof(*ch) ||
4741 (*offset + (int)chk_length) > length) {
4744 SCTP_TCB_UNLOCK(locked_tcb);
4748 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4750 * INIT-ACK only gets the init ack "header" portion only
4751 * because we don't have to process the peer's COOKIE. All
4752 * others get a complete chunk.
4754 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4755 (ch->chunk_type == SCTP_INITIATION)) {
4756 /* get an init-ack chunk */
4757 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4758 sizeof(struct sctp_init_ack_chunk), chunk_buf);
4762 SCTP_TCB_UNLOCK(locked_tcb);
4767 /* For cookies and all other chunks. */
4768 if (chk_length > sizeof(chunk_buf)) {
4770 * use just the size of the chunk buffer so
4771 * the front part of our chunks fit in
4772 * contiguous space up to the chunk buffer
4773 * size (508 bytes). For chunks that need to
4774 * get more than that they must use the
4775 * sctp_m_getptr() function or other means
4776 * (e.g. know how to parse mbuf chains).
4777 * Cookies do this already.
4779 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4780 (sizeof(chunk_buf) - 4),
4785 SCTP_TCB_UNLOCK(locked_tcb);
4790 /* We can fit it all */
4791 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4792 chk_length, chunk_buf);
4794 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4797 SCTP_TCB_UNLOCK(locked_tcb);
4804 /* Save off the last place we got a control from */
4806 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4808 * allow last_control to be NULL if
4809 * ASCONF... ASCONF processing will find the
4812 if ((netp != NULL) && (*netp != NULL))
4813 stcb->asoc.last_control_chunk_from = *netp;
4816 #ifdef SCTP_AUDITING_ENABLED
4817 sctp_audit_log(0xB0, ch->chunk_type);
4820 /* check to see if this chunk required auth, but isn't */
4821 if ((stcb != NULL) &&
4822 (stcb->asoc.auth_supported == 1) &&
4823 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4824 !stcb->asoc.authenticated) {
4825 /* "silently" ignore */
4826 SCTP_STAT_INCR(sctps_recvauthmissing);
4829 switch (ch->chunk_type) {
4830 case SCTP_INITIATION:
4831 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4832 /* The INIT chunk must be the only chunk. */
4833 if ((num_chunks > 1) ||
4834 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4835 /* RFC 4960 requires that no ABORT is sent */
4838 SCTP_TCB_UNLOCK(locked_tcb);
4842 /* Honor our resource limit. */
4843 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4844 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4845 sctp_abort_association(inp, stcb, m, iphlen,
4846 src, dst, sh, op_err,
4852 sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4853 (struct sctp_init_chunk *)ch, inp,
4854 stcb, *netp, &abort_no_unlock,
4858 if ((!abort_no_unlock) && (locked_tcb)) {
4859 SCTP_TCB_UNLOCK(locked_tcb);
4863 case SCTP_PAD_CHUNK:
4865 case SCTP_INITIATION_ACK:
4866 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4867 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4868 /* We are not interested anymore */
4869 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4872 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
4874 SCTP_TCB_UNLOCK(locked_tcb);
4878 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4879 so = SCTP_INP_SO(inp);
4880 atomic_add_int(&stcb->asoc.refcnt, 1);
4881 SCTP_TCB_UNLOCK(stcb);
4882 SCTP_SOCKET_LOCK(so, 1);
4883 SCTP_TCB_LOCK(stcb);
4884 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4886 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4887 SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4888 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4889 SCTP_SOCKET_UNLOCK(so, 1);
4895 /* The INIT-ACK chunk must be the only chunk. */
4896 if ((num_chunks > 1) ||
4897 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4900 SCTP_TCB_UNLOCK(locked_tcb);
4904 if ((netp) && (*netp)) {
4905 ret = sctp_handle_init_ack(m, iphlen, *offset,
4907 (struct sctp_init_ack_chunk *)ch,
4916 if (abort_no_unlock) {
4920 * Special case, I must call the output routine to
4921 * get the cookie echoed
4923 if ((stcb != NULL) && (ret == 0)) {
4924 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4927 SCTP_TCB_UNLOCK(locked_tcb);
4931 case SCTP_SELECTIVE_ACK:
4933 struct sctp_sack_chunk *sack;
4935 uint32_t a_rwnd, cum_ack;
4936 uint16_t num_seg, num_dup;
4938 int offset_seg, offset_dup;
4940 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4941 SCTP_STAT_INCR(sctps_recvsacks);
4943 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4946 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4947 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4950 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4952 * If we have sent a shutdown-ack, we will pay no
4953 * attention to a sack sent in to us since
4954 * we don't care anymore.
4958 sack = (struct sctp_sack_chunk *)ch;
4959 flags = ch->chunk_flags;
4960 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4961 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4962 num_dup = ntohs(sack->sack.num_dup_tsns);
4963 a_rwnd = (uint32_t)ntohl(sack->sack.a_rwnd);
4964 if (sizeof(struct sctp_sack_chunk) +
4965 num_seg * sizeof(struct sctp_gap_ack_block) +
4966 num_dup * sizeof(uint32_t) != chk_length) {
4967 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4970 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4971 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4972 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4973 cum_ack, num_seg, a_rwnd);
4974 stcb->asoc.seen_a_sack_this_pkt = 1;
4975 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4977 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4978 (stcb->asoc.saw_sack_with_frags == 0) &&
4979 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4980 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4983 * We have a SIMPLE sack having no
4984 * prior segments and data on sent
4985 * queue to be acked.. Use the
4986 * faster path sack processing. We
4987 * also allow window update sacks
4988 * with no missing segments to go
4991 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
4994 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4995 num_seg, 0, num_dup, &abort_now, flags,
4996 cum_ack, a_rwnd, ecne_seen);
4999 /* ABORT signal from sack processing */
5003 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5004 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5005 (stcb->asoc.stream_queue_cnt == 0)) {
5006 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5011 * EY - nr_sack: If the received chunk is an
5014 case SCTP_NR_SELECTIVE_ACK:
5016 struct sctp_nr_sack_chunk *nr_sack;
5018 uint32_t a_rwnd, cum_ack;
5019 uint16_t num_seg, num_nr_seg, num_dup;
5021 int offset_seg, offset_dup;
5023 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
5024 SCTP_STAT_INCR(sctps_recvsacks);
5026 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
5029 if (stcb->asoc.nrsack_supported == 0) {
5032 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
5033 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
5036 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
5038 * If we have sent a shutdown-ack, we will pay no
5039 * attention to a sack sent in to us since
5040 * we don't care anymore.
5044 nr_sack = (struct sctp_nr_sack_chunk *)ch;
5045 flags = ch->chunk_flags;
5046 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
5047 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
5048 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
5049 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
5050 a_rwnd = (uint32_t)ntohl(nr_sack->nr_sack.a_rwnd);
5051 if (sizeof(struct sctp_nr_sack_chunk) +
5052 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
5053 num_dup * sizeof(uint32_t) != chk_length) {
5054 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
5057 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
5058 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
5059 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
5060 cum_ack, num_seg, a_rwnd);
5061 stcb->asoc.seen_a_sack_this_pkt = 1;
5062 if ((stcb->asoc.pr_sctp_cnt == 0) &&
5063 (num_seg == 0) && (num_nr_seg == 0) &&
5064 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
5065 (stcb->asoc.saw_sack_with_frags == 0) &&
5066 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
5067 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
5069 * We have a SIMPLE sack having no
5070 * prior segments and data on sent
5071 * queue to be acked. Use the faster
5072 * path sack processing. We also
5073 * allow window update sacks with no
5074 * missing segments to go this way
5077 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
5078 &abort_now, ecne_seen);
5081 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
5082 num_seg, num_nr_seg, num_dup, &abort_now, flags,
5083 cum_ack, a_rwnd, ecne_seen);
5086 /* ABORT signal from sack processing */
5090 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5091 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5092 (stcb->asoc.stream_queue_cnt == 0)) {
5093 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5098 case SCTP_HEARTBEAT_REQUEST:
5099 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
5100 if ((stcb) && netp && *netp) {
5101 SCTP_STAT_INCR(sctps_recvheartbeat);
5102 sctp_send_heartbeat_ack(stcb, m, *offset,
5105 /* He's alive so give him credit */
5106 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5107 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5108 stcb->asoc.overall_error_count,
5110 SCTP_FROM_SCTP_INPUT,
5113 stcb->asoc.overall_error_count = 0;
5116 case SCTP_HEARTBEAT_ACK:
5117 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
5118 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5122 SCTP_TCB_UNLOCK(locked_tcb);
5126 /* He's alive so give him credit */
5127 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5128 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5129 stcb->asoc.overall_error_count,
5131 SCTP_FROM_SCTP_INPUT,
5134 stcb->asoc.overall_error_count = 0;
5135 SCTP_STAT_INCR(sctps_recvheartbeatack);
5137 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5140 case SCTP_ABORT_ASSOCIATION:
5141 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5143 if ((stcb) && netp && *netp)
5144 sctp_handle_abort((struct sctp_abort_chunk *)ch,
5150 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5152 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5155 SCTP_TCB_UNLOCK(locked_tcb);
5159 if (netp && *netp) {
5162 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5163 stcb, *netp, &abort_flag);
5170 case SCTP_SHUTDOWN_ACK:
5171 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", (void *)stcb);
5172 if ((stcb) && (netp) && (*netp))
5173 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5178 case SCTP_OPERATION_ERROR:
5179 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
5180 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
5185 case SCTP_COOKIE_ECHO:
5186 SCTPDBG(SCTP_DEBUG_INPUT3,
5187 "SCTP_COOKIE-ECHO, stcb %p\n", (void *)stcb);
5188 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5191 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5192 /* We are not interested anymore */
5195 SCTP_TCB_UNLOCK(stcb);
5202 * First are we accepting? We do this again here
5203 * since it is possible that a previous endpoint WAS
5204 * listening responded to a INIT-ACK and then
5205 * closed. We opened and bound.. and are now no
5208 * XXXGL: notes on checking listen queue length.
5209 * 1) SCTP_IS_LISTENING() doesn't necessarily mean
5210 * SOLISTENING(), because a listening "UDP type"
5211 * socket isn't listening in terms of the socket
5212 * layer. It is a normal data flow socket, that
5213 * can fork off new connections. Thus, we should
5214 * look into sol_qlen only in case we are !UDP.
5215 * 2) Checking sol_qlen in general requires locking
5216 * the socket, and this code lacks that.
5218 if ((stcb == NULL) &&
5219 (!SCTP_IS_LISTENING(inp) ||
5220 (!(inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
5221 inp->sctp_socket->sol_qlen >= inp->sctp_socket->sol_qlimit))) {
5222 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5223 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5224 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
5225 sctp_abort_association(inp, stcb, m, iphlen,
5226 src, dst, sh, op_err,
5233 struct mbuf *ret_buf;
5234 struct sctp_inpcb *linp;
5243 SCTP_ASOC_CREATE_LOCK(linp);
5244 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5245 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5246 SCTP_ASOC_CREATE_UNLOCK(linp);
5252 sctp_handle_cookie_echo(m, iphlen,
5256 (struct sctp_cookie_echo_chunk *)ch,
5270 SCTP_ASOC_CREATE_UNLOCK(linp);
5272 if (ret_buf == NULL) {
5274 SCTP_TCB_UNLOCK(locked_tcb);
5276 SCTPDBG(SCTP_DEBUG_INPUT3,
5277 "GAK, null buffer\n");
5281 /* if AUTH skipped, see if it verified... */
5286 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5288 * Restart the timer if we have
5291 struct sctp_tmit_chunk *chk;
5293 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5294 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5298 case SCTP_COOKIE_ACK:
5299 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", (void *)stcb);
5300 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5302 SCTP_TCB_UNLOCK(locked_tcb);
5306 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5307 /* We are not interested anymore */
5308 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5311 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5312 so = SCTP_INP_SO(inp);
5313 atomic_add_int(&stcb->asoc.refcnt, 1);
5314 SCTP_TCB_UNLOCK(stcb);
5315 SCTP_SOCKET_LOCK(so, 1);
5316 SCTP_TCB_LOCK(stcb);
5317 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5319 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5320 SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5321 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5322 SCTP_SOCKET_UNLOCK(so, 1);
5328 /* He's alive so give him credit */
5329 if ((stcb) && netp && *netp) {
5330 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5331 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5332 stcb->asoc.overall_error_count,
5334 SCTP_FROM_SCTP_INPUT,
5337 stcb->asoc.overall_error_count = 0;
5338 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5342 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5343 /* He's alive so give him credit */
5344 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5347 SCTP_TCB_UNLOCK(locked_tcb);
5353 if (stcb->asoc.ecn_supported == 0) {
5356 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5357 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5358 stcb->asoc.overall_error_count,
5360 SCTP_FROM_SCTP_INPUT,
5363 stcb->asoc.overall_error_count = 0;
5364 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5370 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5371 /* He's alive so give him credit */
5372 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5375 SCTP_TCB_UNLOCK(locked_tcb);
5381 if (stcb->asoc.ecn_supported == 0) {
5384 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5385 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5386 stcb->asoc.overall_error_count,
5388 SCTP_FROM_SCTP_INPUT,
5391 stcb->asoc.overall_error_count = 0;
5392 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5395 case SCTP_SHUTDOWN_COMPLETE:
5396 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", (void *)stcb);
5397 /* must be first and only chunk */
5398 if ((num_chunks > 1) ||
5399 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5402 SCTP_TCB_UNLOCK(locked_tcb);
5406 if ((stcb) && netp && *netp) {
5407 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5414 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5415 /* He's alive so give him credit */
5417 if (stcb->asoc.asconf_supported == 0) {
5420 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5421 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5422 stcb->asoc.overall_error_count,
5424 SCTP_FROM_SCTP_INPUT,
5427 stcb->asoc.overall_error_count = 0;
5428 sctp_handle_asconf(m, *offset, src,
5429 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5433 case SCTP_ASCONF_ACK:
5434 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5435 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5438 SCTP_TCB_UNLOCK(locked_tcb);
5443 if ((stcb) && netp && *netp) {
5444 if (stcb->asoc.asconf_supported == 0) {
5447 /* He's alive so give him credit */
5448 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5449 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5450 stcb->asoc.overall_error_count,
5452 SCTP_FROM_SCTP_INPUT,
5455 stcb->asoc.overall_error_count = 0;
5456 sctp_handle_asconf_ack(m, *offset,
5457 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5458 if (abort_no_unlock)
5462 case SCTP_FORWARD_CUM_TSN:
5463 case SCTP_IFORWARD_CUM_TSN:
5464 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5465 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5468 SCTP_TCB_UNLOCK(locked_tcb);
5473 /* He's alive so give him credit */
5477 if (stcb->asoc.prsctp_supported == 0) {
5480 stcb->asoc.overall_error_count = 0;
5481 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5482 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5483 stcb->asoc.overall_error_count,
5485 SCTP_FROM_SCTP_INPUT,
5489 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5490 /* We are not interested anymore */
5491 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5492 so = SCTP_INP_SO(inp);
5493 atomic_add_int(&stcb->asoc.refcnt, 1);
5494 SCTP_TCB_UNLOCK(stcb);
5495 SCTP_SOCKET_LOCK(so, 1);
5496 SCTP_TCB_LOCK(stcb);
5497 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5499 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5500 SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5501 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5502 SCTP_SOCKET_UNLOCK(so, 1);
5508 * For sending a SACK this looks like DATA
5511 stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from;
5512 sctp_handle_forward_tsn(stcb,
5513 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5518 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5519 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5520 stcb->asoc.overall_error_count,
5522 SCTP_FROM_SCTP_INPUT,
5525 stcb->asoc.overall_error_count = 0;
5530 case SCTP_STREAM_RESET:
5531 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5532 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5535 SCTP_TCB_UNLOCK(locked_tcb);
5540 if (stcb->asoc.reconfig_supported == 0) {
5543 if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5544 /* stop processing */
5549 case SCTP_PACKET_DROPPED:
5550 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5551 /* re-get it all please */
5552 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5555 SCTP_TCB_UNLOCK(locked_tcb);
5560 if (ch && (stcb) && netp && (*netp)) {
5561 if (stcb->asoc.pktdrop_supported == 0) {
5564 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5566 min(chk_length, (sizeof(chunk_buf) - 4)));
5570 case SCTP_AUTHENTICATION:
5571 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5573 /* save the first AUTH for later processing */
5574 if (auth_skipped == 0) {
5575 auth_offset = *offset;
5576 auth_len = chk_length;
5579 /* skip this chunk (temporarily) */
5582 if (stcb->asoc.auth_supported == 0) {
5585 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5586 (chk_length > (sizeof(struct sctp_auth_chunk) +
5587 SCTP_AUTH_DIGEST_LEN_MAX))) {
5590 SCTP_TCB_UNLOCK(locked_tcb);
5595 if (got_auth == 1) {
5596 /* skip this chunk... it's already auth'd */
5600 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5602 /* auth HMAC failed so dump the packet */
5606 /* remaining chunks are HMAC checked */
5607 stcb->asoc.authenticated = 1;
5613 /* it's an unknown chunk! */
5614 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5615 struct sctp_gen_error_cause *cause;
5618 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5619 0, M_NOWAIT, 1, MT_DATA);
5620 if (op_err != NULL) {
5621 len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset));
5622 cause = mtod(op_err, struct sctp_gen_error_cause *);
5623 cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5624 cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause)));
5625 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
5626 SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
5627 if (SCTP_BUF_NEXT(op_err) != NULL) {
5628 #ifdef SCTP_MBUF_LOGGING
5629 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5630 sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5633 sctp_queue_op_err(stcb, op_err);
5635 sctp_m_freem(op_err);
5639 if ((ch->chunk_type & 0x80) == 0) {
5640 /* discard this packet */
5643 } /* else skip this bad chunk and continue... */
5645 } /* switch (ch->chunk_type) */
5649 /* get the next chunk */
5650 *offset += SCTP_SIZE32(chk_length);
5651 if (*offset >= length) {
5652 /* no more data left in the mbuf chain */
5655 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5656 sizeof(struct sctp_chunkhdr), chunk_buf);
5659 SCTP_TCB_UNLOCK(locked_tcb);
5666 if (asconf_cnt > 0 && stcb != NULL) {
5667 sctp_send_asconf_ack(stcb);
5674 * common input chunk processing (v4 and v6)
5677 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5678 struct sockaddr *src, struct sockaddr *dst,
5679 struct sctphdr *sh, struct sctp_chunkhdr *ch,
5680 #if !defined(SCTP_WITH_NO_CSUM)
5681 uint8_t compute_crc,
5684 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5685 uint32_t vrf_id, uint16_t port)
5688 int fwd_tsn_seen = 0, data_processed = 0;
5689 struct mbuf *m = *mm, *op_err;
5690 char msg[SCTP_DIAG_INFO_LEN];
5692 int cnt_ctrl_ready = 0;
5693 struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5694 struct sctp_tcb *stcb = NULL;
5695 struct sctp_nets *net = NULL;
5697 SCTP_STAT_INCR(sctps_recvdatagrams);
5698 #ifdef SCTP_AUDITING_ENABLED
5699 sctp_audit_log(0xE0, 1);
5700 sctp_auditing(0, inp, stcb, net);
5702 #if !defined(SCTP_WITH_NO_CSUM)
5703 if (compute_crc != 0) {
5704 uint32_t check, calc_check;
5706 check = sh->checksum;
5708 calc_check = sctp_calculate_cksum(m, iphlen);
5709 sh->checksum = check;
5710 if (calc_check != check) {
5711 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5712 calc_check, check, (void *)m, length, iphlen);
5713 stcb = sctp_findassociation_addr(m, offset, src, dst,
5714 sh, ch, &inp, &net, vrf_id);
5715 #if defined(INET) || defined(INET6)
5716 if ((ch->chunk_type != SCTP_INITIATION) &&
5717 (net != NULL) && (net->port != port)) {
5718 if (net->port == 0) {
5719 /* UDP encapsulation turned on. */
5720 net->mtu -= sizeof(struct udphdr);
5721 if (stcb->asoc.smallest_mtu > net->mtu) {
5722 sctp_pathmtu_adjustment(stcb, net->mtu);
5724 } else if (port == 0) {
5725 /* UDP encapsulation turned off. */
5726 net->mtu += sizeof(struct udphdr);
5727 /* XXX Update smallest_mtu */
5733 net->flowtype = mflowtype;
5734 net->flowid = mflowid;
5736 if ((inp != NULL) && (stcb != NULL)) {
5737 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5738 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5739 } else if ((inp != NULL) && (stcb == NULL)) {
5742 SCTP_STAT_INCR(sctps_badsum);
5743 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5748 /* Destination port of 0 is illegal, based on RFC4960. */
5749 if (sh->dest_port == 0) {
5750 SCTP_STAT_INCR(sctps_hdrops);
5753 stcb = sctp_findassociation_addr(m, offset, src, dst,
5754 sh, ch, &inp, &net, vrf_id);
5755 #if defined(INET) || defined(INET6)
5756 if ((ch->chunk_type != SCTP_INITIATION) &&
5757 (net != NULL) && (net->port != port)) {
5758 if (net->port == 0) {
5759 /* UDP encapsulation turned on. */
5760 net->mtu -= sizeof(struct udphdr);
5761 if (stcb->asoc.smallest_mtu > net->mtu) {
5762 sctp_pathmtu_adjustment(stcb, net->mtu);
5764 } else if (port == 0) {
5765 /* UDP encapsulation turned off. */
5766 net->mtu += sizeof(struct udphdr);
5767 /* XXX Update smallest_mtu */
5773 net->flowtype = mflowtype;
5774 net->flowid = mflowid;
5777 SCTP_STAT_INCR(sctps_noport);
5778 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5781 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5782 sctp_send_shutdown_complete2(src, dst, sh,
5783 mflowtype, mflowid, fibnum,
5787 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5790 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5791 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5792 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5793 (ch->chunk_type != SCTP_INIT))) {
5794 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5796 sctp_send_abort(m, iphlen, src, dst,
5798 mflowtype, mflowid, fibnum,
5803 } else if (stcb == NULL) {
5806 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5807 (void *)m, iphlen, offset, length, (void *)stcb);
5809 /* always clear this before beginning a packet */
5810 stcb->asoc.authenticated = 0;
5811 stcb->asoc.seen_a_sack_this_pkt = 0;
5812 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5813 (void *)stcb, stcb->asoc.state);
5815 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5816 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5818 * If we hit here, we had a ref count
5819 * up when the assoc was aborted and the
5820 * timer is clearing out the assoc, we should
5821 * NOT respond to any packet.. its OOTB.
5823 SCTP_TCB_UNLOCK(stcb);
5825 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5826 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5828 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5829 mflowtype, mflowid, inp->fibnum,
5834 if (IS_SCTP_CONTROL(ch)) {
5835 /* process the control portion of the SCTP packet */
5836 /* sa_ignore NO_NULL_CHK */
5837 stcb = sctp_process_control(m, iphlen, &offset, length,
5839 inp, stcb, &net, &fwd_tsn_seen,
5840 mflowtype, mflowid, fibnum,
5844 * This covers us if the cookie-echo was there and
5845 * it changes our INP.
5847 inp = stcb->sctp_ep;
5848 #if defined(INET) || defined(INET6)
5849 if ((ch->chunk_type != SCTP_INITIATION) &&
5850 (net != NULL) && (net->port != port)) {
5851 if (net->port == 0) {
5852 /* UDP encapsulation turned on. */
5853 net->mtu -= sizeof(struct udphdr);
5854 if (stcb->asoc.smallest_mtu > net->mtu) {
5855 sctp_pathmtu_adjustment(stcb, net->mtu);
5857 } else if (port == 0) {
5858 /* UDP encapsulation turned off. */
5859 net->mtu += sizeof(struct udphdr);
5860 /* XXX Update smallest_mtu */
5868 * no control chunks, so pre-process DATA chunks (these
5869 * checks are taken care of by control processing)
5873 * if DATA only packet, and auth is required, then punt...
5874 * can't have authenticated without any AUTH (control)
5877 if ((stcb != NULL) &&
5878 (stcb->asoc.auth_supported == 1) &&
5879 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5880 /* "silently" ignore */
5881 SCTP_STAT_INCR(sctps_recvauthmissing);
5885 /* out of the blue DATA chunk */
5886 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5887 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5889 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5890 mflowtype, mflowid, fibnum,
5894 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5895 /* v_tag mismatch! */
5896 SCTP_STAT_INCR(sctps_badvtag);
5903 * no valid TCB for this packet, or we found it's a bad
5904 * packet while processing control, or we're done with this
5905 * packet (done or skip rest of data), so we drop it...
5910 * DATA chunk processing
5912 /* plow through the data chunks while length > offset */
5915 * Rest should be DATA only. Check authentication state if AUTH for
5918 if ((length > offset) &&
5920 (stcb->asoc.auth_supported == 1) &&
5921 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5922 !stcb->asoc.authenticated) {
5923 /* "silently" ignore */
5924 SCTP_STAT_INCR(sctps_recvauthmissing);
5925 SCTPDBG(SCTP_DEBUG_AUTH1,
5926 "Data chunk requires AUTH, skipped\n");
5929 if (length > offset) {
5933 * First check to make sure our state is correct. We would
5934 * not get here unless we really did have a tag, so we don't
5935 * abort if this happens, just dump the chunk silently.
5937 switch (SCTP_GET_STATE(&stcb->asoc)) {
5938 case SCTP_STATE_COOKIE_ECHOED:
5940 * we consider data with valid tags in this state
5941 * shows us the cookie-ack was lost. Imply it was
5944 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5945 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5946 stcb->asoc.overall_error_count,
5948 SCTP_FROM_SCTP_INPUT,
5951 stcb->asoc.overall_error_count = 0;
5952 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5954 case SCTP_STATE_COOKIE_WAIT:
5956 * We consider OOTB any data sent during asoc setup.
5958 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5959 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5961 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5962 mflowtype, mflowid, inp->fibnum,
5965 /* sa_ignore NOTREACHED */
5967 case SCTP_STATE_EMPTY: /* should not happen */
5968 case SCTP_STATE_INUSE: /* should not happen */
5969 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5970 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5973 /* sa_ignore NOTREACHED */
5975 case SCTP_STATE_OPEN:
5976 case SCTP_STATE_SHUTDOWN_SENT:
5979 /* plow through the data chunks while length > offset */
5980 retval = sctp_process_data(mm, iphlen, &offset, length,
5981 inp, stcb, net, &high_tsn);
5984 * The association aborted, NO UNLOCK needed since
5985 * the association is destroyed.
5992 * Anything important needs to have been m_copy'ed in
5996 /* take care of ecn */
5997 if ((data_processed == 1) &&
5998 (stcb->asoc.ecn_supported == 1) &&
5999 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
6000 /* Yep, we need to add a ECNE */
6001 sctp_send_ecn_echo(stcb, net, high_tsn);
6003 if ((data_processed == 0) && (fwd_tsn_seen)) {
6005 uint32_t highest_tsn;
6007 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
6008 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
6010 highest_tsn = stcb->asoc.highest_tsn_inside_map;
6012 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
6013 stcb->asoc.send_sack = 1;
6014 sctp_sack_check(stcb, was_a_gap);
6015 } else if (fwd_tsn_seen) {
6016 stcb->asoc.send_sack = 1;
6018 /* trigger send of any chunks in queue... */
6020 #ifdef SCTP_AUDITING_ENABLED
6021 sctp_audit_log(0xE0, 2);
6022 sctp_auditing(1, inp, stcb, net);
6024 SCTPDBG(SCTP_DEBUG_INPUT1,
6025 "Check for chunk output prw:%d tqe:%d tf=%d\n",
6026 stcb->asoc.peers_rwnd,
6027 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
6028 stcb->asoc.total_flight);
6029 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
6030 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
6031 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
6033 if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
6035 stcb->asoc.trigger_reset ||
6037 (stcb->asoc.peers_rwnd > 0 ||
6038 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
6039 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
6040 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
6041 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
6043 #ifdef SCTP_AUDITING_ENABLED
6044 sctp_audit_log(0xE0, 3);
6045 sctp_auditing(2, inp, stcb, net);
6049 SCTP_TCB_UNLOCK(stcb);
6051 if (inp_decr != NULL) {
6052 /* reduce ref-count */
6053 SCTP_INP_WLOCK(inp_decr);
6054 SCTP_INP_DECR_REF(inp_decr);
6055 SCTP_INP_WUNLOCK(inp_decr);
6062 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
6066 uint32_t vrf_id = 0;
6068 struct sockaddr_in src, dst;
6071 struct sctp_chunkhdr *ch;
6073 #if !defined(SCTP_WITH_NO_CSUM)
6074 uint8_t compute_crc;
6081 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
6082 SCTP_RELEASE_PKT(i_pak);
6085 m = SCTP_HEADER_TO_CHAIN(i_pak);
6086 #ifdef SCTP_MBUF_LOGGING
6087 /* Log in any input mbufs */
6088 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6089 sctp_log_mbc(m, SCTP_MBUF_INPUT);
6092 #ifdef SCTP_PACKET_LOGGING
6093 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
6097 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6098 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
6100 if_name(m->m_pkthdr.rcvif),
6101 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
6102 mflowid = m->m_pkthdr.flowid;
6103 mflowtype = M_HASHTYPE_GET(m);
6104 fibnum = M_GETFIB(m);
6105 SCTP_STAT_INCR(sctps_recvpackets);
6106 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
6107 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
6108 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6109 if (SCTP_BUF_LEN(m) < offset) {
6110 if ((m = m_pullup(m, offset)) == NULL) {
6111 SCTP_STAT_INCR(sctps_hdrops);
6115 ip = mtod(m, struct ip *);
6116 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
6117 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
6118 offset -= sizeof(struct sctp_chunkhdr);
6119 memset(&src, 0, sizeof(struct sockaddr_in));
6120 src.sin_family = AF_INET;
6121 src.sin_len = sizeof(struct sockaddr_in);
6122 src.sin_port = sh->src_port;
6123 src.sin_addr = ip->ip_src;
6124 memset(&dst, 0, sizeof(struct sockaddr_in));
6125 dst.sin_family = AF_INET;
6126 dst.sin_len = sizeof(struct sockaddr_in);
6127 dst.sin_port = sh->dest_port;
6128 dst.sin_addr = ip->ip_dst;
6129 length = ntohs(ip->ip_len);
6130 /* Validate mbuf chain length with IP payload length. */
6131 if (SCTP_HEADER_LEN(m) != length) {
6132 SCTPDBG(SCTP_DEBUG_INPUT1,
6133 "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
6134 SCTP_STAT_INCR(sctps_hdrops);
6137 /* SCTP does not allow broadcasts or multicasts */
6138 if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
6141 if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
6144 ecn_bits = ip->ip_tos;
6145 #if defined(SCTP_WITH_NO_CSUM)
6146 SCTP_STAT_INCR(sctps_recvnocrc);
6148 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
6149 SCTP_STAT_INCR(sctps_recvhwcrc);
6152 SCTP_STAT_INCR(sctps_recvswcrc);
6156 sctp_common_input_processing(&m, iphlen, offset, length,
6157 (struct sockaddr *)&src,
6158 (struct sockaddr *)&dst,
6160 #if !defined(SCTP_WITH_NO_CSUM)
6164 mflowtype, mflowid, fibnum,
6173 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6174 extern int *sctp_cpuarry;
6178 sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
6185 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6191 uint32_t flowid, tag;
6193 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
6194 flowid = m->m_pkthdr.flowid;
6197 * No flow id built by lower layers fix it so we
6200 offset = off + sizeof(struct sctphdr);
6201 if (SCTP_BUF_LEN(m) < offset) {
6202 if ((m = m_pullup(m, offset)) == NULL) {
6203 SCTP_STAT_INCR(sctps_hdrops);
6204 return (IPPROTO_DONE);
6207 ip = mtod(m, struct ip *);
6208 sh = (struct sctphdr *)((caddr_t)ip + off);
6209 tag = htonl(sh->v_tag);
6210 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6211 m->m_pkthdr.flowid = flowid;
6212 M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH);
6214 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6215 sctp_queue_to_mcore(m, off, cpu_to_use);
6216 return (IPPROTO_DONE);
6219 sctp_input_with_port(m, off, 0);
6220 return (IPPROTO_DONE);