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 atomic_add_int(&stcb->asoc.refcnt, 1);
2153 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2154 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2155 src, dst, sh, op_err,
2158 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2159 SCTP_TCB_UNLOCK(stcb);
2160 SCTP_SOCKET_LOCK(so, 1);
2161 SCTP_TCB_LOCK(stcb);
2163 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2164 SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2165 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2166 SCTP_SOCKET_UNLOCK(so, 1);
2168 atomic_subtract_int(&stcb->asoc.refcnt, 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 atomic_add_int(&stcb->asoc.refcnt, 1);
2190 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2191 SCTP_TCB_UNLOCK(stcb);
2192 SCTP_SOCKET_LOCK(so, 1);
2193 SCTP_TCB_LOCK(stcb);
2195 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2196 SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2197 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2198 SCTP_SOCKET_UNLOCK(so, 1);
2200 atomic_subtract_int(&stcb->asoc.refcnt, 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 atomic_add_int(&stcb->asoc.refcnt, 1);
2208 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2209 SCTP_TCB_UNLOCK(stcb);
2210 SCTP_SOCKET_LOCK(so, 1);
2211 SCTP_TCB_LOCK(stcb);
2213 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2214 SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2215 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2216 SCTP_SOCKET_UNLOCK(so, 1);
2218 atomic_subtract_int(&stcb->asoc.refcnt, 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 atomic_add_int(&stcb->asoc.refcnt, 1);
2238 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2239 SCTP_TCB_UNLOCK(stcb);
2240 SCTP_SOCKET_LOCK(so, 1);
2241 SCTP_TCB_LOCK(stcb);
2243 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2244 SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2245 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2246 SCTP_SOCKET_UNLOCK(so, 1);
2248 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2251 /* remaining chunks checked... good to go */
2252 stcb->asoc.authenticated = 1;
2255 /* update current state */
2256 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2257 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2258 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2259 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2260 stcb->sctp_ep, stcb, asoc->primary_destination);
2262 sctp_stop_all_cookie_timers(stcb);
2263 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2264 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2267 * if we're doing ASCONFs, check to see if we have any new local
2268 * addresses that need to get added to the peer (eg. addresses
2269 * changed while cookie echo in flight). This needs to be done
2270 * after we go to the OPEN state to do the correct asconf
2271 * processing. else, make sure we have the correct addresses in our
2275 /* warning, we re-use sin, sin6, sa_store here! */
2276 /* pull in local_address (our "from" address) */
2277 switch (cookie->laddr_type) {
2279 case SCTP_IPV4_ADDRESS:
2280 /* source addr is IPv4 */
2281 memset(&store.sin, 0, sizeof(struct sockaddr_in));
2282 store.sin.sin_family = AF_INET;
2283 store.sin.sin_len = sizeof(struct sockaddr_in);
2284 store.sin.sin_addr.s_addr = cookie->laddress[0];
2288 case SCTP_IPV6_ADDRESS:
2289 /* source addr is IPv6 */
2290 memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
2291 store.sin6.sin6_family = AF_INET6;
2292 store.sin6.sin6_len = sizeof(struct sockaddr_in6);
2293 store.sin6.sin6_scope_id = cookie->scope_id;
2294 memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2298 atomic_add_int(&stcb->asoc.refcnt, 1);
2299 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2300 SCTP_TCB_UNLOCK(stcb);
2301 SCTP_SOCKET_LOCK(so, 1);
2302 SCTP_TCB_LOCK(stcb);
2304 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2305 SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2306 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2307 SCTP_SOCKET_UNLOCK(so, 1);
2309 atomic_subtract_int(&stcb->asoc.refcnt, 1);
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->peerport != sh->src_port) ||
2445 (cookie->myport != sh->dest_port) ||
2446 (cookie->my_vtag != sh->v_tag)) {
2448 * invalid ports or bad tag. Note that we always leave the
2449 * v_tag in the header in network order and when we stored
2450 * it in the my_vtag slot we also left it in network order.
2451 * This maintains the match even though it may be in the
2452 * opposite byte order of the machine :->
2456 if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2457 sizeof(struct sctp_init_chunk) +
2458 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2459 /* cookie too small */
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;
2557 * TODO sctp_constants.h needs alternative time macros when _KERNEL
2560 if (timevalcmp(&now, &time_expires, >)) {
2561 /* cookie is stale! */
2562 struct mbuf *op_err;
2563 struct sctp_error_stale_cookie *cause;
2566 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2567 0, M_NOWAIT, 1, MT_DATA);
2568 if (op_err == NULL) {
2573 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
2574 cause = mtod(op_err, struct sctp_error_stale_cookie *);
2575 cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
2576 cause->cause.length = htons((sizeof(struct sctp_paramhdr) +
2577 (sizeof(uint32_t))));
2578 /* seconds to usec */
2579 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2582 tim = now.tv_usec - cookie->time_entered.tv_usec;
2583 cause->stale_time = htonl(tim);
2584 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2585 mflowtype, mflowid, l_inp->fibnum,
2590 * Now we must see with the lookup address if we have an existing
2591 * asoc. This will only happen if we were in the COOKIE-WAIT state
2592 * and a INIT collided with us and somewhere the peer sent the
2593 * cookie on another address besides the single address our assoc
2594 * had for him. In this case we will have one of the tie-tags set at
2595 * least AND the address field in the cookie can be used to look it
2599 switch (cookie->addr_type) {
2601 case SCTP_IPV6_ADDRESS:
2602 memset(&sin6, 0, sizeof(sin6));
2603 sin6.sin6_family = AF_INET6;
2604 sin6.sin6_len = sizeof(sin6);
2605 sin6.sin6_port = sh->src_port;
2606 sin6.sin6_scope_id = cookie->scope_id;
2607 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2608 sizeof(sin6.sin6_addr.s6_addr));
2609 to = (struct sockaddr *)&sin6;
2613 case SCTP_IPV4_ADDRESS:
2614 memset(&sin, 0, sizeof(sin));
2615 sin.sin_family = AF_INET;
2616 sin.sin_len = sizeof(sin);
2617 sin.sin_port = sh->src_port;
2618 sin.sin_addr.s_addr = cookie->address[0];
2619 to = (struct sockaddr *)&sin;
2623 /* This should not happen */
2626 if (*stcb == NULL) {
2627 /* Yep, lets check */
2628 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2629 if (*stcb == NULL) {
2631 * We should have only got back the same inp. If we
2632 * got back a different ep we have a problem. The
2633 * original findep got back l_inp and now
2635 if (l_inp != *inp_p) {
2636 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2639 if (*locked_tcb == NULL) {
2641 * In this case we found the assoc only
2642 * after we locked the create lock. This
2643 * means we are in a colliding case and we
2644 * must make sure that we unlock the tcb if
2645 * its one of the cases where we throw away
2646 * the incoming packets.
2648 *locked_tcb = *stcb;
2651 * We must also increment the inp ref count
2652 * since the ref_count flags was set when we
2653 * did not find the TCB, now we found it
2654 * which reduces the refcount.. we must
2655 * raise it back out to balance it all :-)
2657 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2658 if ((*stcb)->sctp_ep != l_inp) {
2659 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2660 (void *)(*stcb)->sctp_ep, (void *)l_inp);
2665 cookie_len -= SCTP_SIGNATURE_SIZE;
2666 if (*stcb == NULL) {
2667 /* this is the "normal" case... get a new TCB */
2668 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2669 cookie, cookie_len, *inp_p,
2670 netp, to, ¬ification,
2671 auth_skipped, auth_offset, auth_len,
2675 /* this is abnormal... cookie-echo on existing TCB */
2676 had_a_existing_tcb = 1;
2677 *stcb = sctp_process_cookie_existing(m, iphlen, offset,
2679 cookie, cookie_len, *inp_p, *stcb, netp, to,
2680 ¬ification, auth_skipped, auth_offset, auth_len,
2685 if (*stcb == NULL) {
2686 /* still no TCB... must be bad cookie-echo */
2689 if (*netp != NULL) {
2690 (*netp)->flowtype = mflowtype;
2691 (*netp)->flowid = mflowid;
2694 * Ok, we built an association so confirm the address we sent the
2697 netl = sctp_findnet(*stcb, to);
2699 * This code should in theory NOT run but
2702 /* TSNH! Huh, why do I need to add this address here? */
2703 if (sctp_add_remote_addr(*stcb, to, NULL, port,
2704 SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2707 netl = sctp_findnet(*stcb, to);
2710 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2711 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2712 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2717 sctp_start_net_timers(*stcb);
2718 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2719 if (!had_a_existing_tcb ||
2720 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2722 * If we have a NEW cookie or the connect never
2723 * reached the connected state during collision we
2724 * must do the TCP accept thing.
2726 struct socket *so, *oso;
2727 struct sctp_inpcb *inp;
2729 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2731 * For a restart we will keep the same
2732 * socket, no need to do anything. I THINK!!
2734 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2735 if (send_int_conf) {
2736 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2737 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2741 oso = (*inp_p)->sctp_socket;
2742 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2743 SCTP_TCB_UNLOCK((*stcb));
2744 CURVNET_SET(oso->so_vnet);
2745 so = sonewconn(oso, 0
2748 SCTP_TCB_LOCK((*stcb));
2749 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2752 struct mbuf *op_err;
2753 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2754 struct socket *pcb_so;
2756 /* Too many sockets */
2757 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2758 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2759 sctp_abort_association(*inp_p, NULL, m, iphlen,
2760 src, dst, sh, op_err,
2763 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2764 pcb_so = SCTP_INP_SO(*inp_p);
2765 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2766 SCTP_TCB_UNLOCK((*stcb));
2767 SCTP_SOCKET_LOCK(pcb_so, 1);
2768 SCTP_TCB_LOCK((*stcb));
2769 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2771 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2772 SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2773 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2774 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2778 inp = (struct sctp_inpcb *)so->so_pcb;
2779 SCTP_INP_INCR_REF(inp);
2781 * We add the unbound flag here so that if we get an
2782 * soabort() before we get the move_pcb done, we
2783 * will properly cleanup.
2785 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2786 SCTP_PCB_FLAGS_CONNECTED |
2787 SCTP_PCB_FLAGS_IN_TCPPOOL |
2788 SCTP_PCB_FLAGS_UNBOUND |
2789 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2790 SCTP_PCB_FLAGS_DONT_WAKE);
2791 inp->sctp_features = (*inp_p)->sctp_features;
2792 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2793 inp->sctp_socket = so;
2794 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2795 inp->max_cwnd = (*inp_p)->max_cwnd;
2796 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2797 inp->ecn_supported = (*inp_p)->ecn_supported;
2798 inp->prsctp_supported = (*inp_p)->prsctp_supported;
2799 inp->auth_supported = (*inp_p)->auth_supported;
2800 inp->asconf_supported = (*inp_p)->asconf_supported;
2801 inp->reconfig_supported = (*inp_p)->reconfig_supported;
2802 inp->nrsack_supported = (*inp_p)->nrsack_supported;
2803 inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2804 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2805 inp->sctp_context = (*inp_p)->sctp_context;
2806 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2807 inp->fibnum = (*inp_p)->fibnum;
2808 inp->inp_starting_point_for_iterator = NULL;
2810 * copy in the authentication parameters from the
2813 if (inp->sctp_ep.local_hmacs)
2814 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2815 inp->sctp_ep.local_hmacs =
2816 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2817 if (inp->sctp_ep.local_auth_chunks)
2818 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2819 inp->sctp_ep.local_auth_chunks =
2820 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2823 * Now we must move it from one hash table to
2824 * another and get the tcb in the right place.
2828 * This is where the one-2-one socket is put into
2829 * the accept state waiting for the accept!
2832 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2834 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2836 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2837 SCTP_TCB_UNLOCK((*stcb));
2839 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2841 SCTP_TCB_LOCK((*stcb));
2842 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2846 * now we must check to see if we were aborted while
2847 * the move was going on and the lock/unlock
2850 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2852 * yep it was, we leave the assoc attached
2853 * to the socket since the sctp_inpcb_free()
2854 * call will send an abort for us.
2856 SCTP_INP_DECR_REF(inp);
2859 SCTP_INP_DECR_REF(inp);
2860 /* Switch over to the new guy */
2862 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2863 if (send_int_conf) {
2864 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2865 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2868 * Pull it from the incomplete queue and wake the
2871 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2872 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2873 SCTP_TCB_UNLOCK((*stcb));
2874 SCTP_SOCKET_LOCK(so, 1);
2877 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2878 SCTP_TCB_LOCK((*stcb));
2879 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2880 SCTP_SOCKET_UNLOCK(so, 1);
2886 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2888 if (send_int_conf) {
2889 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2890 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2896 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2897 struct sctp_tcb *stcb, struct sctp_nets *net)
2899 /* cp must not be used, others call this without a c-ack :-) */
2900 struct sctp_association *asoc;
2902 SCTPDBG(SCTP_DEBUG_INPUT2,
2903 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2904 if ((stcb == NULL) || (net == NULL)) {
2909 sctp_stop_all_cookie_timers(stcb);
2910 /* process according to association state */
2911 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2912 /* state change only needed when I am in right state */
2913 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2914 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2915 sctp_start_net_timers(stcb);
2916 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2917 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2918 stcb->sctp_ep, stcb, asoc->primary_destination);
2922 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2923 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2924 if (asoc->overall_error_count == 0) {
2925 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2926 &asoc->time_entered, sctp_align_safe_nocopy,
2927 SCTP_RTT_FROM_NON_DATA);
2929 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2930 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2931 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2932 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2933 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2937 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2938 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2939 so = SCTP_INP_SO(stcb->sctp_ep);
2940 atomic_add_int(&stcb->asoc.refcnt, 1);
2941 SCTP_TCB_UNLOCK(stcb);
2942 SCTP_SOCKET_LOCK(so, 1);
2943 SCTP_TCB_LOCK(stcb);
2944 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2946 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2947 soisconnected(stcb->sctp_socket);
2949 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2950 SCTP_SOCKET_UNLOCK(so, 1);
2954 * since we did not send a HB make sure we don't double
2957 net->hb_responded = 1;
2959 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2961 * We don't need to do the asconf thing, nor hb or
2962 * autoclose if the socket is closed.
2966 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2970 if (stcb->asoc.sctp_autoclose_ticks &&
2971 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2972 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2973 stcb->sctp_ep, stcb, NULL);
2976 * send ASCONF if parameters are pending and ASCONFs are
2977 * allowed (eg. addresses changed when init/cookie echo were
2980 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2981 (stcb->asoc.asconf_supported == 1) &&
2982 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2983 #ifdef SCTP_TIMER_BASED_ASCONF
2984 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2985 stcb->sctp_ep, stcb,
2986 stcb->asoc.primary_destination);
2988 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2989 SCTP_ADDR_NOT_LOCKED);
2994 /* Toss the cookie if I can */
2995 sctp_toss_old_cookies(stcb, asoc);
2996 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
2997 /* Restart the timer if we have pending data */
2998 struct sctp_tmit_chunk *chk;
3000 chk = TAILQ_FIRST(&asoc->sent_queue);
3001 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
3006 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
3007 struct sctp_tcb *stcb)
3009 struct sctp_nets *net;
3010 struct sctp_tmit_chunk *lchk;
3011 struct sctp_ecne_chunk bkup;
3012 uint8_t override_bit;
3013 uint32_t tsn, window_data_tsn;
3015 unsigned int pkt_cnt;
3017 len = ntohs(cp->ch.chunk_length);
3018 if ((len != sizeof(struct sctp_ecne_chunk)) &&
3019 (len != sizeof(struct old_sctp_ecne_chunk))) {
3022 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3023 /* Its the old format */
3024 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3025 bkup.num_pkts_since_cwr = htonl(1);
3028 SCTP_STAT_INCR(sctps_recvecne);
3029 tsn = ntohl(cp->tsn);
3030 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3031 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3033 window_data_tsn = stcb->asoc.sending_seq - 1;
3035 window_data_tsn = lchk->rec.data.tsn;
3038 /* Find where it was sent to if possible. */
3040 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3041 if (lchk->rec.data.tsn == tsn) {
3043 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3046 if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) {
3052 * What to do. A previous send of a CWR was possibly lost.
3053 * See how old it is, we may have it marked on the actual
3056 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3057 if (tsn == net->last_cwr_tsn) {
3058 /* Found him, send it off */
3064 * If we reach here, we need to send a special CWR
3065 * that says hey, we did this a long time ago and
3066 * you lost the response.
3068 net = TAILQ_FIRST(&stcb->asoc.nets);
3073 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3080 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3081 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3083 * JRS - Use the congestion control given in the pluggable
3086 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3088 * We reduce once every RTT. So we will only lower cwnd at
3089 * the next sending seq i.e. the window_data_tsn
3091 net->cwr_window_tsn = window_data_tsn;
3092 net->ecn_ce_pkt_cnt += pkt_cnt;
3093 net->lost_cnt = pkt_cnt;
3094 net->last_cwr_tsn = tsn;
3096 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3097 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3098 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3100 * Another loss in the same window update how many
3101 * marks/packets lost we have had.
3105 if (pkt_cnt > net->lost_cnt) {
3106 /* Should be the case */
3107 cnt = (pkt_cnt - net->lost_cnt);
3108 net->ecn_ce_pkt_cnt += cnt;
3110 net->lost_cnt = pkt_cnt;
3111 net->last_cwr_tsn = tsn;
3113 * Most CC functions will ignore this call, since we
3114 * are in-window yet of the initial CE the peer saw.
3116 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3120 * We always send a CWR this way if our previous one was lost our
3121 * peer will get an update, or if it is not time again to reduce we
3122 * still get the cwr to the peer. Note we set the override when we
3123 * could not find the TSN on the chunk or the destination network.
3125 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3129 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3132 * Here we get a CWR from the peer. We must look in the outqueue and
3133 * make sure that we have a covered ECNE in the control chunk part.
3136 struct sctp_tmit_chunk *chk;
3137 struct sctp_ecne_chunk *ecne;
3141 cwr_tsn = ntohl(cp->tsn);
3142 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3143 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
3144 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3147 if ((override == 0) && (chk->whoTo != net)) {
3148 /* Must be from the right src unless override is set */
3151 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3152 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3153 /* this covers this ECNE, we can remove it */
3154 stcb->asoc.ecn_echo_cnt_onq--;
3155 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3157 sctp_m_freem(chk->data);
3159 stcb->asoc.ctrl_queue_cnt--;
3160 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3161 if (override == 0) {
3169 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3170 struct sctp_tcb *stcb, struct sctp_nets *net)
3172 struct sctp_association *asoc;
3173 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3177 SCTPDBG(SCTP_DEBUG_INPUT2,
3178 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3183 /* process according to association state */
3184 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3185 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3186 SCTPDBG(SCTP_DEBUG_INPUT2,
3187 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3188 SCTP_TCB_UNLOCK(stcb);
3191 /* notify upper layer protocol */
3192 if (stcb->sctp_socket) {
3193 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3196 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3197 !TAILQ_EMPTY(&asoc->sent_queue) ||
3198 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
3199 panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3202 /* stop the timer */
3203 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3204 SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3205 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3207 SCTPDBG(SCTP_DEBUG_INPUT2,
3208 "sctp_handle_shutdown_complete: calls free-asoc\n");
3209 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3210 so = SCTP_INP_SO(stcb->sctp_ep);
3211 atomic_add_int(&stcb->asoc.refcnt, 1);
3212 SCTP_TCB_UNLOCK(stcb);
3213 SCTP_SOCKET_LOCK(so, 1);
3214 SCTP_TCB_LOCK(stcb);
3215 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3217 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3218 SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3219 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3220 SCTP_SOCKET_UNLOCK(so, 1);
3226 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3227 struct sctp_nets *net, uint8_t flg)
3229 switch (desc->chunk_type) {
3231 /* find the tsn to resend (possibly */
3234 struct sctp_tmit_chunk *tp1;
3236 tsn = ntohl(desc->tsn_ifany);
3237 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3238 if (tp1->rec.data.tsn == tsn) {
3242 if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) {
3250 * Do it the other way , aka without paying
3251 * attention to queue seq order.
3253 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3254 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3255 if (tp1->rec.data.tsn == tsn) {
3262 SCTP_STAT_INCR(sctps_pdrptsnnf);
3264 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3267 if (((flg & SCTP_BADCRC) == 0) &&
3268 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3271 if ((stcb->asoc.peers_rwnd == 0) &&
3272 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3273 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3276 if (stcb->asoc.peers_rwnd == 0 &&
3277 (flg & SCTP_FROM_MIDDLE_BOX)) {
3278 SCTP_STAT_INCR(sctps_pdrpdizrw);
3281 ddp = (uint8_t *)(mtod(tp1->data, caddr_t)+
3282 sizeof(struct sctp_data_chunk));
3286 for (iii = 0; iii < sizeof(desc->data_bytes);
3288 if (ddp[iii] != desc->data_bytes[iii]) {
3289 SCTP_STAT_INCR(sctps_pdrpbadd);
3297 * this guy had a RTO calculation
3298 * pending on it, cancel it
3300 if (tp1->whoTo->rto_needed == 0) {
3301 tp1->whoTo->rto_needed = 1;
3305 SCTP_STAT_INCR(sctps_pdrpmark);
3306 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3307 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3309 * mark it as if we were doing a FR, since
3310 * we will be getting gap ack reports behind
3311 * the info from the router.
3313 tp1->rec.data.doing_fast_retransmit = 1;
3315 * mark the tsn with what sequences can
3318 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3319 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3321 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
3324 /* restart the timer */
3325 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3327 SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3328 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3331 /* fix counts and things */
3332 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3333 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3334 tp1->whoTo->flight_size,
3336 (uint32_t)(uintptr_t)stcb,
3339 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3340 sctp_flight_size_decrease(tp1);
3341 sctp_total_flight_decrease(stcb, tp1);
3343 tp1->sent = SCTP_DATAGRAM_RESEND;
3349 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3350 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3353 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3355 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3358 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3359 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3360 audit, stcb->asoc.sent_queue_retran_cnt);
3361 #ifndef SCTP_AUDITING_ENABLED
3362 stcb->asoc.sent_queue_retran_cnt = audit;
3370 struct sctp_tmit_chunk *asconf;
3372 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3374 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3379 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3380 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3381 asconf->sent = SCTP_DATAGRAM_RESEND;
3382 asconf->snd_count--;
3386 case SCTP_INITIATION:
3387 /* resend the INIT */
3388 stcb->asoc.dropped_special_cnt++;
3389 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3391 * If we can get it in, in a few attempts we do
3392 * this, otherwise we let the timer fire.
3394 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3396 SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3397 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3400 case SCTP_SELECTIVE_ACK:
3401 case SCTP_NR_SELECTIVE_ACK:
3402 /* resend the sack */
3403 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3405 case SCTP_HEARTBEAT_REQUEST:
3406 /* resend a demand HB */
3407 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3409 * Only retransmit if we KNOW we wont destroy the
3412 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3416 sctp_send_shutdown(stcb, net);
3418 case SCTP_SHUTDOWN_ACK:
3419 sctp_send_shutdown_ack(stcb, net);
3421 case SCTP_COOKIE_ECHO:
3423 struct sctp_tmit_chunk *cookie;
3426 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3428 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3433 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3434 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3435 cookie->sent = SCTP_DATAGRAM_RESEND;
3436 sctp_stop_all_cookie_timers(stcb);
3440 case SCTP_COOKIE_ACK:
3441 sctp_send_cookie_ack(stcb);
3443 case SCTP_ASCONF_ACK:
3444 /* resend last asconf ack */
3445 sctp_send_asconf_ack(stcb);
3447 case SCTP_IFORWARD_CUM_TSN:
3448 case SCTP_FORWARD_CUM_TSN:
3449 send_forward_tsn(stcb, &stcb->asoc);
3451 /* can't do anything with these */
3452 case SCTP_PACKET_DROPPED:
3453 case SCTP_INITIATION_ACK: /* this should not happen */
3454 case SCTP_HEARTBEAT_ACK:
3455 case SCTP_ABORT_ASSOCIATION:
3456 case SCTP_OPERATION_ERROR:
3457 case SCTP_SHUTDOWN_COMPLETE:
3467 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3473 * We set things to 0xffffffff since this is the last delivered
3474 * sequence and we will be sending in 0 after the reset.
3477 if (number_entries) {
3478 for (i = 0; i < number_entries; i++) {
3479 temp = ntohs(list[i]);
3480 if (temp >= stcb->asoc.streamincnt) {
3483 stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff;
3487 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3488 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
3491 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3495 sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3500 if (number_entries > 0) {
3501 for (i = 0; i < number_entries; i++) {
3502 temp = ntohs(list[i]);
3503 if (temp >= stcb->asoc.streamoutcnt) {
3504 /* no such stream */
3507 stcb->asoc.strmout[temp].next_mid_ordered = 0;
3508 stcb->asoc.strmout[temp].next_mid_unordered = 0;
3511 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3512 stcb->asoc.strmout[i].next_mid_ordered = 0;
3513 stcb->asoc.strmout[i].next_mid_unordered = 0;
3516 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3520 sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3525 if (number_entries > 0) {
3526 for (i = 0; i < number_entries; i++) {
3527 temp = ntohs(list[i]);
3528 if (temp >= stcb->asoc.streamoutcnt) {
3529 /* no such stream */
3532 stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
3535 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3536 stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
3542 struct sctp_stream_reset_request *
3543 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3545 struct sctp_association *asoc;
3546 struct sctp_chunkhdr *ch;
3547 struct sctp_stream_reset_request *r;
3548 struct sctp_tmit_chunk *chk;
3552 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3553 asoc->stream_reset_outstanding = 0;
3556 if (stcb->asoc.str_reset == NULL) {
3557 asoc->stream_reset_outstanding = 0;
3560 chk = stcb->asoc.str_reset;
3561 if (chk->data == NULL) {
3565 /* he wants a copy of the chk pointer */
3568 clen = chk->send_size;
3569 ch = mtod(chk->data, struct sctp_chunkhdr *);
3570 r = (struct sctp_stream_reset_request *)(ch + 1);
3571 if (ntohl(r->request_seq) == seq) {
3575 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3576 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3577 /* move to the next one, there can only be a max of two */
3578 r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3579 if (ntohl(r->request_seq) == seq) {
3583 /* that seq is not here */
3588 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3590 struct sctp_association *asoc;
3591 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3593 if (stcb->asoc.str_reset == NULL) {
3598 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
3599 chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3600 TAILQ_REMOVE(&asoc->control_send_queue,
3604 sctp_m_freem(chk->data);
3607 asoc->ctrl_queue_cnt--;
3608 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3609 /* sa_ignore NO_NULL_CHK */
3610 stcb->asoc.str_reset = NULL;
3615 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3616 uint32_t seq, uint32_t action,
3617 struct sctp_stream_reset_response *respin)
3621 struct sctp_association *asoc = &stcb->asoc;
3622 struct sctp_tmit_chunk *chk;
3623 struct sctp_stream_reset_request *req_param;
3624 struct sctp_stream_reset_out_request *req_out_param;
3625 struct sctp_stream_reset_in_request *req_in_param;
3626 uint32_t number_entries;
3628 if (asoc->stream_reset_outstanding == 0) {
3632 if (seq == stcb->asoc.str_reset_seq_out) {
3633 req_param = sctp_find_stream_reset(stcb, seq, &chk);
3634 if (req_param != NULL) {
3635 stcb->asoc.str_reset_seq_out++;
3636 type = ntohs(req_param->ph.param_type);
3637 lparm_len = ntohs(req_param->ph.param_length);
3638 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3641 req_out_param = (struct sctp_stream_reset_out_request *)req_param;
3642 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3643 asoc->stream_reset_out_is_outstanding = 0;
3644 if (asoc->stream_reset_outstanding)
3645 asoc->stream_reset_outstanding--;
3646 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3648 sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3649 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3650 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3651 } else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
3653 * Set it up so we don't stop
3656 asoc->stream_reset_outstanding++;
3657 stcb->asoc.str_reset_seq_out--;
3658 asoc->stream_reset_out_is_outstanding = 1;
3661 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3663 if (no_clear == 0) {
3664 sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
3666 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3667 req_in_param = (struct sctp_stream_reset_in_request *)req_param;
3668 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3669 if (asoc->stream_reset_outstanding)
3670 asoc->stream_reset_outstanding--;
3671 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3672 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3673 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3674 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3675 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3676 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3678 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3679 /* Ok we now may have more streams */
3682 num_stream = stcb->asoc.strm_pending_add_size;
3683 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3685 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3687 stcb->asoc.strm_pending_add_size = 0;
3688 if (asoc->stream_reset_outstanding)
3689 asoc->stream_reset_outstanding--;
3690 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3691 /* Put the new streams into effect */
3694 for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
3695 asoc->strmout[i].state = SCTP_STREAM_OPEN;
3697 asoc->streamoutcnt += num_stream;
3698 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3699 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3700 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3701 SCTP_STREAM_CHANGE_DENIED);
3703 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3704 SCTP_STREAM_CHANGE_FAILED);
3706 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3707 if (asoc->stream_reset_outstanding)
3708 asoc->stream_reset_outstanding--;
3709 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3710 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3711 SCTP_STREAM_CHANGE_DENIED);
3712 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3713 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3714 SCTP_STREAM_CHANGE_FAILED);
3716 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3718 * a) Adopt the new in tsn.
3720 * c) Adopt the new out-tsn
3722 struct sctp_stream_reset_response_tsn *resp;
3723 struct sctp_forward_tsn_chunk fwdtsn;
3726 if (respin == NULL) {
3730 if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
3733 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3734 resp = (struct sctp_stream_reset_response_tsn *)respin;
3735 asoc->stream_reset_outstanding--;
3736 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3737 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3738 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3739 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3743 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3744 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3745 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3747 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3748 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3749 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3751 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3752 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3754 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3755 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3757 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3758 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3759 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3760 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3761 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3762 SCTP_ASSOC_RESET_DENIED);
3764 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3765 SCTP_ASSOC_RESET_FAILED);
3768 /* get rid of the request and get the request flags */
3769 if (asoc->stream_reset_outstanding == 0) {
3770 sctp_clean_up_stream_reset(stcb);
3774 if (asoc->stream_reset_outstanding == 0) {
3775 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3781 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3782 struct sctp_tmit_chunk *chk,
3783 struct sctp_stream_reset_in_request *req, int trunc)
3791 * peer wants me to send a str-reset to him for my outgoing seq's if
3794 struct sctp_association *asoc = &stcb->asoc;
3796 seq = ntohl(req->request_seq);
3797 if (asoc->str_reset_seq_in == seq) {
3798 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3799 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3800 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3802 /* Can't do it, since they exceeded our buffer size */
3803 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3804 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3805 len = ntohs(req->ph.param_length);
3806 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3807 if (number_entries) {
3808 for (i = 0; i < number_entries; i++) {
3809 temp = ntohs(req->list_of_streams[i]);
3810 if (temp >= stcb->asoc.streamoutcnt) {
3811 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3814 req->list_of_streams[i] = temp;
3816 for (i = 0; i < number_entries; i++) {
3817 if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
3818 stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
3823 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3824 if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
3825 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
3828 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3830 /* Can't do it, since we have sent one out */
3831 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3834 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3835 asoc->str_reset_seq_in++;
3836 } else if (asoc->str_reset_seq_in - 1 == seq) {
3837 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3838 } else if (asoc->str_reset_seq_in - 2 == seq) {
3839 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3841 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3843 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3847 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3848 struct sctp_tmit_chunk *chk,
3849 struct sctp_stream_reset_tsn_request *req)
3851 /* reset all in and out and update the tsn */
3853 * A) reset my str-seq's on in and out. B) Select a receive next,
3854 * and set cum-ack to it. Also process this selected number as a
3855 * fwd-tsn as well. C) set in the response my next sending seq.
3857 struct sctp_forward_tsn_chunk fwdtsn;
3858 struct sctp_association *asoc = &stcb->asoc;
3862 seq = ntohl(req->request_seq);
3863 if (asoc->str_reset_seq_in == seq) {
3864 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3865 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3866 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3868 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3869 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3870 fwdtsn.ch.chunk_flags = 0;
3871 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3872 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3876 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3877 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3878 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3880 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3881 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3882 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3883 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3884 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3885 atomic_add_int(&asoc->sending_seq, 1);
3886 /* save off historical data for retrans */
3887 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3888 asoc->last_sending_seq[0] = asoc->sending_seq;
3889 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3890 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3891 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3892 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3893 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3894 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3896 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3897 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3898 asoc->str_reset_seq_in++;
3899 } else if (asoc->str_reset_seq_in - 1 == seq) {
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 } else if (asoc->str_reset_seq_in - 2 == seq) {
3903 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3904 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3906 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3912 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3913 struct sctp_tmit_chunk *chk,
3914 struct sctp_stream_reset_out_request *req, int trunc)
3917 int number_entries, len;
3918 struct sctp_association *asoc = &stcb->asoc;
3920 seq = ntohl(req->request_seq);
3922 /* now if its not a duplicate we process it */
3923 if (asoc->str_reset_seq_in == seq) {
3924 len = ntohs(req->ph.param_length);
3925 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3927 * the sender is resetting, handle the list issue.. we must
3928 * a) verify if we can do the reset, if so no problem b) If
3929 * we can't do the reset we must copy the request. c) queue
3930 * it, and setup the data in processor to trigger it off
3931 * when needed and dequeue all the queued data.
3933 tsn = ntohl(req->send_reset_at_tsn);
3935 /* move the reset action back one */
3936 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3937 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3938 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3940 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3941 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3942 /* we can do it now */
3943 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3944 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3947 * we must queue it up and thus wait for the TSN's
3948 * to arrive that are at or before tsn
3950 struct sctp_stream_reset_list *liste;
3953 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3954 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3955 siz, SCTP_M_STRESET);
3956 if (liste == NULL) {
3957 /* gak out of memory */
3958 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3959 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3964 liste->number_entries = number_entries;
3965 memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
3966 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3967 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
3969 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3970 asoc->str_reset_seq_in++;
3971 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3973 * one seq back, just echo back last action since my
3974 * response was lost.
3976 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3977 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3979 * two seq back, just echo back last action since my
3980 * response was lost.
3982 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3984 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3989 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3990 struct sctp_stream_reset_add_strm *str_add)
3993 * Peer is requesting to add more streams. If its within our
3994 * max-streams we will allow it.
3996 uint32_t num_stream, i;
3998 struct sctp_association *asoc = &stcb->asoc;
3999 struct sctp_queued_to_read *ctl, *nctl;
4001 /* Get the number. */
4002 seq = ntohl(str_add->request_seq);
4003 num_stream = ntohs(str_add->number_of_streams);
4004 /* Now what would be the new total? */
4005 if (asoc->str_reset_seq_in == seq) {
4006 num_stream += stcb->asoc.streamincnt;
4007 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4008 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4009 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4010 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
4011 (num_stream > 0xffff)) {
4012 /* We must reject it they ask for to many */
4014 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4016 /* Ok, we can do that :-) */
4017 struct sctp_stream_in *oldstrm;
4019 /* save off the old */
4020 oldstrm = stcb->asoc.strmin;
4021 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
4022 (num_stream * sizeof(struct sctp_stream_in)),
4024 if (stcb->asoc.strmin == NULL) {
4025 stcb->asoc.strmin = oldstrm;
4028 /* copy off the old data */
4029 for (i = 0; i < stcb->asoc.streamincnt; i++) {
4030 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4031 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4032 stcb->asoc.strmin[i].sid = i;
4033 stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered;
4034 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
4035 stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
4036 /* now anything on those queues? */
4037 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
4038 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
4039 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
4041 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
4042 TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
4043 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
4046 /* Init the new streams */
4047 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
4048 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4049 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4050 stcb->asoc.strmin[i].sid = i;
4051 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
4052 stcb->asoc.strmin[i].pd_api_started = 0;
4053 stcb->asoc.strmin[i].delivery_started = 0;
4055 SCTP_FREE(oldstrm, SCTP_M_STRMI);
4056 /* update the size */
4057 stcb->asoc.streamincnt = num_stream;
4058 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4059 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
4061 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4062 asoc->str_reset_seq_in++;
4063 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4065 * one seq back, just echo back last action since my
4066 * response was lost.
4068 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4069 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4071 * two seq back, just echo back last action since my
4072 * response was lost.
4074 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4076 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4082 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4083 struct sctp_stream_reset_add_strm *str_add)
4086 * Peer is requesting to add more streams. If its within our
4087 * max-streams we will allow it.
4089 uint16_t num_stream;
4091 struct sctp_association *asoc = &stcb->asoc;
4093 /* Get the number. */
4094 seq = ntohl(str_add->request_seq);
4095 num_stream = ntohs(str_add->number_of_streams);
4096 /* Now what would be the new total? */
4097 if (asoc->str_reset_seq_in == seq) {
4098 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4099 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4100 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4101 } else if (stcb->asoc.stream_reset_outstanding) {
4102 /* We must reject it we have something pending */
4103 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4105 /* Ok, we can do that :-) */
4108 mychk = stcb->asoc.streamoutcnt;
4109 mychk += num_stream;
4110 if (mychk < 0x10000) {
4111 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4112 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
4113 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4116 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4119 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4120 asoc->str_reset_seq_in++;
4121 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4123 * one seq back, just echo back last action since my
4124 * response was lost.
4126 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4127 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4129 * two seq back, just echo back last action since my
4130 * response was lost.
4132 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4134 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4139 __attribute__((noinline))
4142 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4143 struct sctp_chunkhdr *ch_req)
4145 uint16_t remaining_length, param_len, ptype;
4146 struct sctp_paramhdr pstore;
4147 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4151 struct sctp_tmit_chunk *chk;
4152 struct sctp_chunkhdr *ch;
4153 struct sctp_paramhdr *ph;
4157 /* now it may be a reset or a reset-response */
4158 remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
4160 /* setup for adding the response */
4161 sctp_alloc_a_chunk(stcb, chk);
4165 chk->copy_by_ref = 0;
4166 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4167 chk->rec.chunk_id.can_take_data = 0;
4169 chk->asoc = &stcb->asoc;
4170 chk->no_fr_allowed = 0;
4171 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4172 chk->book_size_scale = 0;
4173 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4174 if (chk->data == NULL) {
4177 sctp_m_freem(chk->data);
4180 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4183 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4185 /* setup chunk parameters */
4186 chk->sent = SCTP_DATAGRAM_UNSENT;
4190 ch = mtod(chk->data, struct sctp_chunkhdr *);
4191 ch->chunk_type = SCTP_STREAM_RESET;
4192 ch->chunk_flags = 0;
4193 ch->chunk_length = htons(chk->send_size);
4194 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4195 offset += sizeof(struct sctp_chunkhdr);
4196 while (remaining_length >= sizeof(struct sctp_paramhdr)) {
4197 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
4202 param_len = ntohs(ph->param_length);
4203 if ((param_len > remaining_length) ||
4204 (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
4205 /* bad parameter length */
4208 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
4209 (uint8_t *)&cstore);
4214 ptype = ntohs(ph->param_type);
4216 if (param_len > sizeof(cstore)) {
4221 if (num_param > SCTP_MAX_RESET_PARAMS) {
4222 /* hit the max of parameters already sorry.. */
4225 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4226 struct sctp_stream_reset_out_request *req_out;
4228 if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
4231 req_out = (struct sctp_stream_reset_out_request *)ph;
4233 if (stcb->asoc.stream_reset_outstanding) {
4234 seq = ntohl(req_out->response_seq);
4235 if (seq == stcb->asoc.str_reset_seq_out) {
4237 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4240 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4241 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4242 struct sctp_stream_reset_add_strm *str_add;
4244 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4247 str_add = (struct sctp_stream_reset_add_strm *)ph;
4249 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4250 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4251 struct sctp_stream_reset_add_strm *str_add;
4253 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4256 str_add = (struct sctp_stream_reset_add_strm *)ph;
4258 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4259 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4260 struct sctp_stream_reset_in_request *req_in;
4263 req_in = (struct sctp_stream_reset_in_request *)ph;
4264 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4265 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4266 struct sctp_stream_reset_tsn_request *req_tsn;
4269 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4270 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4272 goto strres_nochunk;
4276 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4277 struct sctp_stream_reset_response *resp;
4280 if (param_len < sizeof(struct sctp_stream_reset_response)) {
4283 resp = (struct sctp_stream_reset_response *)ph;
4284 seq = ntohl(resp->response_seq);
4285 result = ntohl(resp->result);
4286 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4288 goto strres_nochunk;
4293 offset += SCTP_SIZE32(param_len);
4294 if (remaining_length >= SCTP_SIZE32(param_len)) {
4295 remaining_length -= SCTP_SIZE32(param_len);
4297 remaining_length = 0;
4301 /* we have no response free the stuff */
4302 goto strres_nochunk;
4304 /* ok we have a chunk to link in */
4305 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4308 stcb->asoc.ctrl_queue_cnt++;
4313 * Handle a router or endpoints report of a packet loss, there are two ways
4314 * to handle this, either we get the whole packet and must disect it
4315 * ourselves (possibly with truncation and or corruption) or it is a summary
4316 * from a middle box that did the disectting for us.
4319 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4320 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4322 uint32_t bottle_bw, on_queue;
4326 struct sctp_chunk_desc desc;
4327 struct sctp_chunkhdr *ch;
4329 chlen = ntohs(cp->ch.chunk_length);
4330 chlen -= sizeof(struct sctp_pktdrop_chunk);
4331 /* XXX possible chlen underflow */
4334 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4335 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4337 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4338 chlen -= sizeof(struct sctphdr);
4339 /* XXX possible chlen underflow */
4340 memset(&desc, 0, sizeof(desc));
4342 trunc_len = (uint16_t)ntohs(cp->trunc_len);
4343 if (trunc_len > limit) {
4346 /* now the chunks themselves */
4347 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4348 desc.chunk_type = ch->chunk_type;
4349 /* get amount we need to move */
4350 at = ntohs(ch->chunk_length);
4351 if (at < sizeof(struct sctp_chunkhdr)) {
4352 /* corrupt chunk, maybe at the end? */
4353 SCTP_STAT_INCR(sctps_pdrpcrupt);
4356 if (trunc_len == 0) {
4357 /* we are supposed to have all of it */
4359 /* corrupt skip it */
4360 SCTP_STAT_INCR(sctps_pdrpcrupt);
4364 /* is there enough of it left ? */
4365 if (desc.chunk_type == SCTP_DATA) {
4366 if (chlen < (sizeof(struct sctp_data_chunk) +
4367 sizeof(desc.data_bytes))) {
4371 if (chlen < sizeof(struct sctp_chunkhdr)) {
4376 if (desc.chunk_type == SCTP_DATA) {
4377 /* can we get out the tsn? */
4378 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4379 SCTP_STAT_INCR(sctps_pdrpmbda);
4381 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4383 struct sctp_data_chunk *dcp;
4387 dcp = (struct sctp_data_chunk *)ch;
4388 ddp = (uint8_t *)(dcp + 1);
4389 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4390 desc.data_bytes[iii] = ddp[iii];
4392 desc.tsn_ifany = dcp->dp.tsn;
4394 /* nope we are done. */
4395 SCTP_STAT_INCR(sctps_pdrpnedat);
4399 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4400 SCTP_STAT_INCR(sctps_pdrpmbct);
4403 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4404 SCTP_STAT_INCR(sctps_pdrppdbrk);
4407 if (SCTP_SIZE32(at) > chlen) {
4410 chlen -= SCTP_SIZE32(at);
4411 if (chlen < sizeof(struct sctp_chunkhdr)) {
4412 /* done, none left */
4415 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4417 /* Now update any rwnd --- possibly */
4418 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4419 /* From a peer, we get a rwnd report */
4422 SCTP_STAT_INCR(sctps_pdrpfehos);
4424 bottle_bw = ntohl(cp->bottle_bw);
4425 on_queue = ntohl(cp->current_onq);
4426 if (bottle_bw && on_queue) {
4427 /* a rwnd report is in here */
4428 if (bottle_bw > on_queue)
4429 a_rwnd = bottle_bw - on_queue;
4434 stcb->asoc.peers_rwnd = 0;
4436 if (a_rwnd > stcb->asoc.total_flight) {
4437 stcb->asoc.peers_rwnd =
4438 a_rwnd - stcb->asoc.total_flight;
4440 stcb->asoc.peers_rwnd = 0;
4442 if (stcb->asoc.peers_rwnd <
4443 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4444 /* SWS sender side engages */
4445 stcb->asoc.peers_rwnd = 0;
4450 SCTP_STAT_INCR(sctps_pdrpfmbox);
4453 /* now middle boxes in sat networks get a cwnd bump */
4454 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4455 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4456 (stcb->asoc.sat_network)) {
4458 * This is debatable but for sat networks it makes sense
4459 * Note if a T3 timer has went off, we will prohibit any
4460 * changes to cwnd until we exit the t3 loss recovery.
4462 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4463 net, cp, &bottle_bw, &on_queue);
4468 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4469 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4470 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4471 * length of the complete packet outputs: - length: modified to remaining
4472 * length after control processing - netp: modified to new sctp_nets after
4473 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4474 * bad packet,...) otherwise return the tcb for this packet
4477 __attribute__((noinline))
4479 static struct sctp_tcb *
4480 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4481 struct sockaddr *src, struct sockaddr *dst,
4482 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4483 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4484 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4485 uint32_t vrf_id, uint16_t port)
4487 struct sctp_association *asoc;
4488 struct mbuf *op_err;
4489 char msg[SCTP_DIAG_INFO_LEN];
4491 int num_chunks = 0; /* number of control chunks processed */
4492 uint32_t chk_length;
4494 int abort_no_unlock = 0;
4498 * How big should this be, and should it be alloc'd? Lets try the
4499 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4500 * until we get into jumbo grams and such..
4502 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4503 struct sctp_tcb *locked_tcb = stcb;
4505 uint32_t auth_offset = 0, auth_len = 0;
4506 int auth_skipped = 0;
4508 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4512 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4513 iphlen, *offset, length, (void *)stcb);
4515 /* validate chunk header length... */
4516 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4517 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4518 ntohs(ch->chunk_length));
4520 SCTP_TCB_UNLOCK(locked_tcb);
4525 * validate the verification tag
4527 vtag_in = ntohl(sh->v_tag);
4530 SCTP_TCB_LOCK_ASSERT(locked_tcb);
4532 if (ch->chunk_type == SCTP_INITIATION) {
4533 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4534 ntohs(ch->chunk_length), vtag_in);
4536 /* protocol error- silently discard... */
4537 SCTP_STAT_INCR(sctps_badvtag);
4539 SCTP_TCB_UNLOCK(locked_tcb);
4543 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4545 * If there is no stcb, skip the AUTH chunk and process
4546 * later after a stcb is found (to validate the lookup was
4549 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4551 (inp->auth_supported == 1)) {
4552 /* save this chunk for later processing */
4554 auth_offset = *offset;
4555 auth_len = ntohs(ch->chunk_length);
4557 /* (temporarily) move past this chunk */
4558 *offset += SCTP_SIZE32(auth_len);
4559 if (*offset >= length) {
4560 /* no more data left in the mbuf chain */
4563 SCTP_TCB_UNLOCK(locked_tcb);
4567 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4568 sizeof(struct sctp_chunkhdr), chunk_buf);
4574 SCTP_TCB_UNLOCK(locked_tcb);
4578 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4579 goto process_control_chunks;
4582 * first check if it's an ASCONF with an unknown src addr we
4583 * need to look inside to find the association
4585 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4586 struct sctp_chunkhdr *asconf_ch = ch;
4587 uint32_t asconf_offset = 0, asconf_len = 0;
4589 /* inp's refcount may be reduced */
4590 SCTP_INP_INCR_REF(inp);
4592 asconf_offset = *offset;
4594 asconf_len = ntohs(asconf_ch->chunk_length);
4595 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4597 stcb = sctp_findassociation_ep_asconf(m,
4600 sh, &inp, netp, vrf_id);
4603 asconf_offset += SCTP_SIZE32(asconf_len);
4604 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4605 sizeof(struct sctp_chunkhdr), chunk_buf);
4606 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4609 * reduce inp's refcount if not reduced in
4610 * sctp_findassociation_ep_asconf().
4612 SCTP_INP_DECR_REF(inp);
4617 /* now go back and verify any auth chunk to be sure */
4618 if (auth_skipped && (stcb != NULL)) {
4619 struct sctp_auth_chunk *auth;
4621 auth = (struct sctp_auth_chunk *)
4622 sctp_m_getptr(m, auth_offset,
4623 auth_len, chunk_buf);
4626 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4628 /* auth HMAC failed so dump it */
4631 SCTP_TCB_UNLOCK(locked_tcb);
4635 /* remaining chunks are HMAC checked */
4636 stcb->asoc.authenticated = 1;
4641 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4642 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4644 /* no association, so it's out of the blue... */
4645 sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4646 mflowtype, mflowid, inp->fibnum,
4650 SCTP_TCB_UNLOCK(locked_tcb);
4655 /* ABORT and SHUTDOWN can use either v_tag... */
4656 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4657 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4658 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4659 /* Take the T-bit always into account. */
4660 if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
4661 (vtag_in == asoc->my_vtag)) ||
4662 (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4663 (vtag_in == asoc->peer_vtag))) {
4666 /* drop this packet... */
4667 SCTP_STAT_INCR(sctps_badvtag);
4669 SCTP_TCB_UNLOCK(locked_tcb);
4673 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4674 if (vtag_in != asoc->my_vtag) {
4676 * this could be a stale SHUTDOWN-ACK or the
4677 * peer never got the SHUTDOWN-COMPLETE and
4678 * is still hung; we have started a new asoc
4679 * but it won't complete until the shutdown
4683 SCTP_TCB_UNLOCK(locked_tcb);
4685 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4686 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4688 sctp_handle_ootb(m, iphlen, *offset, src, dst,
4690 mflowtype, mflowid, fibnum,
4695 /* for all other chunks, vtag must match */
4696 if (vtag_in != asoc->my_vtag) {
4697 /* invalid vtag... */
4698 SCTPDBG(SCTP_DEBUG_INPUT3,
4699 "invalid vtag: %xh, expect %xh\n",
4700 vtag_in, asoc->my_vtag);
4701 SCTP_STAT_INCR(sctps_badvtag);
4703 SCTP_TCB_UNLOCK(locked_tcb);
4709 } /* end if !SCTP_COOKIE_ECHO */
4711 * process all control chunks...
4713 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4714 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4715 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4716 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4717 /* implied cookie-ack.. we must have lost the ack */
4718 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4719 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4720 stcb->asoc.overall_error_count,
4722 SCTP_FROM_SCTP_INPUT,
4725 stcb->asoc.overall_error_count = 0;
4726 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4729 process_control_chunks:
4730 while (IS_SCTP_CONTROL(ch)) {
4731 /* validate chunk length */
4732 chk_length = ntohs(ch->chunk_length);
4733 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4734 ch->chunk_type, chk_length);
4735 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4736 if (chk_length < sizeof(*ch) ||
4737 (*offset + (int)chk_length) > length) {
4740 SCTP_TCB_UNLOCK(locked_tcb);
4744 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4746 * INIT-ACK only gets the init ack "header" portion only
4747 * because we don't have to process the peer's COOKIE. All
4748 * others get a complete chunk.
4750 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4751 (ch->chunk_type == SCTP_INITIATION)) {
4752 /* get an init-ack chunk */
4753 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4754 sizeof(struct sctp_init_ack_chunk), chunk_buf);
4758 SCTP_TCB_UNLOCK(locked_tcb);
4763 /* For cookies and all other chunks. */
4764 if (chk_length > sizeof(chunk_buf)) {
4766 * use just the size of the chunk buffer so
4767 * the front part of our chunks fit in
4768 * contiguous space up to the chunk buffer
4769 * size (508 bytes). For chunks that need to
4770 * get more than that they must use the
4771 * sctp_m_getptr() function or other means
4772 * (e.g. know how to parse mbuf chains).
4773 * Cookies do this already.
4775 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4776 (sizeof(chunk_buf) - 4),
4781 SCTP_TCB_UNLOCK(locked_tcb);
4786 /* We can fit it all */
4787 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4788 chk_length, chunk_buf);
4790 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4793 SCTP_TCB_UNLOCK(locked_tcb);
4800 /* Save off the last place we got a control from */
4802 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4804 * allow last_control to be NULL if
4805 * ASCONF... ASCONF processing will find the
4808 if ((netp != NULL) && (*netp != NULL))
4809 stcb->asoc.last_control_chunk_from = *netp;
4812 #ifdef SCTP_AUDITING_ENABLED
4813 sctp_audit_log(0xB0, ch->chunk_type);
4816 /* check to see if this chunk required auth, but isn't */
4817 if ((stcb != NULL) &&
4818 (stcb->asoc.auth_supported == 1) &&
4819 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4820 !stcb->asoc.authenticated) {
4821 /* "silently" ignore */
4822 SCTP_STAT_INCR(sctps_recvauthmissing);
4825 switch (ch->chunk_type) {
4826 case SCTP_INITIATION:
4827 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4828 /* The INIT chunk must be the only chunk. */
4829 if ((num_chunks > 1) ||
4830 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4831 /* RFC 4960 requires that no ABORT is sent */
4834 SCTP_TCB_UNLOCK(locked_tcb);
4838 /* Honor our resource limit. */
4839 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4840 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4841 sctp_abort_association(inp, stcb, m, iphlen,
4842 src, dst, sh, op_err,
4848 sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4849 (struct sctp_init_chunk *)ch, inp,
4850 stcb, *netp, &abort_no_unlock,
4854 if ((!abort_no_unlock) && (locked_tcb)) {
4855 SCTP_TCB_UNLOCK(locked_tcb);
4859 case SCTP_PAD_CHUNK:
4861 case SCTP_INITIATION_ACK:
4862 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4863 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4864 /* We are not interested anymore */
4865 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4868 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
4870 SCTP_TCB_UNLOCK(locked_tcb);
4874 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4875 so = SCTP_INP_SO(inp);
4876 atomic_add_int(&stcb->asoc.refcnt, 1);
4877 SCTP_TCB_UNLOCK(stcb);
4878 SCTP_SOCKET_LOCK(so, 1);
4879 SCTP_TCB_LOCK(stcb);
4880 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4882 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4883 SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4884 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4885 SCTP_SOCKET_UNLOCK(so, 1);
4891 /* The INIT-ACK chunk must be the only chunk. */
4892 if ((num_chunks > 1) ||
4893 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4896 SCTP_TCB_UNLOCK(locked_tcb);
4900 if ((netp) && (*netp)) {
4901 ret = sctp_handle_init_ack(m, iphlen, *offset,
4903 (struct sctp_init_ack_chunk *)ch,
4912 if (abort_no_unlock) {
4916 * Special case, I must call the output routine to
4917 * get the cookie echoed
4919 if ((stcb != NULL) && (ret == 0)) {
4920 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4923 SCTP_TCB_UNLOCK(locked_tcb);
4927 case SCTP_SELECTIVE_ACK:
4929 struct sctp_sack_chunk *sack;
4931 uint32_t a_rwnd, cum_ack;
4932 uint16_t num_seg, num_dup;
4934 int offset_seg, offset_dup;
4936 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4937 SCTP_STAT_INCR(sctps_recvsacks);
4939 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4942 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4943 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4946 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4948 * If we have sent a shutdown-ack, we will pay no
4949 * attention to a sack sent in to us since
4950 * we don't care anymore.
4954 sack = (struct sctp_sack_chunk *)ch;
4955 flags = ch->chunk_flags;
4956 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4957 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4958 num_dup = ntohs(sack->sack.num_dup_tsns);
4959 a_rwnd = (uint32_t)ntohl(sack->sack.a_rwnd);
4960 if (sizeof(struct sctp_sack_chunk) +
4961 num_seg * sizeof(struct sctp_gap_ack_block) +
4962 num_dup * sizeof(uint32_t) != chk_length) {
4963 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4966 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4967 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4968 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4969 cum_ack, num_seg, a_rwnd);
4970 stcb->asoc.seen_a_sack_this_pkt = 1;
4971 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4973 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4974 (stcb->asoc.saw_sack_with_frags == 0) &&
4975 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4976 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4979 * We have a SIMPLE sack having no
4980 * prior segments and data on sent
4981 * queue to be acked.. Use the
4982 * faster path sack processing. We
4983 * also allow window update sacks
4984 * with no missing segments to go
4987 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
4990 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4991 num_seg, 0, num_dup, &abort_now, flags,
4992 cum_ack, a_rwnd, ecne_seen);
4995 /* ABORT signal from sack processing */
4999 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5000 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5001 (stcb->asoc.stream_queue_cnt == 0)) {
5002 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5007 * EY - nr_sack: If the received chunk is an
5010 case SCTP_NR_SELECTIVE_ACK:
5012 struct sctp_nr_sack_chunk *nr_sack;
5014 uint32_t a_rwnd, cum_ack;
5015 uint16_t num_seg, num_nr_seg, num_dup;
5017 int offset_seg, offset_dup;
5019 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
5020 SCTP_STAT_INCR(sctps_recvsacks);
5022 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
5025 if (stcb->asoc.nrsack_supported == 0) {
5028 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
5029 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
5032 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
5034 * If we have sent a shutdown-ack, we will pay no
5035 * attention to a sack sent in to us since
5036 * we don't care anymore.
5040 nr_sack = (struct sctp_nr_sack_chunk *)ch;
5041 flags = ch->chunk_flags;
5042 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
5043 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
5044 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
5045 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
5046 a_rwnd = (uint32_t)ntohl(nr_sack->nr_sack.a_rwnd);
5047 if (sizeof(struct sctp_nr_sack_chunk) +
5048 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
5049 num_dup * sizeof(uint32_t) != chk_length) {
5050 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
5053 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
5054 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
5055 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
5056 cum_ack, num_seg, a_rwnd);
5057 stcb->asoc.seen_a_sack_this_pkt = 1;
5058 if ((stcb->asoc.pr_sctp_cnt == 0) &&
5059 (num_seg == 0) && (num_nr_seg == 0) &&
5060 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
5061 (stcb->asoc.saw_sack_with_frags == 0) &&
5062 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
5063 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
5065 * We have a SIMPLE sack having no
5066 * prior segments and data on sent
5067 * queue to be acked. Use the faster
5068 * path sack processing. We also
5069 * allow window update sacks with no
5070 * missing segments to go this way
5073 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
5074 &abort_now, ecne_seen);
5077 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
5078 num_seg, num_nr_seg, num_dup, &abort_now, flags,
5079 cum_ack, a_rwnd, ecne_seen);
5082 /* ABORT signal from sack processing */
5086 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5087 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5088 (stcb->asoc.stream_queue_cnt == 0)) {
5089 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5094 case SCTP_HEARTBEAT_REQUEST:
5095 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
5096 if ((stcb) && netp && *netp) {
5097 SCTP_STAT_INCR(sctps_recvheartbeat);
5098 sctp_send_heartbeat_ack(stcb, m, *offset,
5101 /* He's alive so give him credit */
5102 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5103 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5104 stcb->asoc.overall_error_count,
5106 SCTP_FROM_SCTP_INPUT,
5109 stcb->asoc.overall_error_count = 0;
5112 case SCTP_HEARTBEAT_ACK:
5113 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
5114 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5118 SCTP_TCB_UNLOCK(locked_tcb);
5122 /* He's alive so give him credit */
5123 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5124 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5125 stcb->asoc.overall_error_count,
5127 SCTP_FROM_SCTP_INPUT,
5130 stcb->asoc.overall_error_count = 0;
5131 SCTP_STAT_INCR(sctps_recvheartbeatack);
5133 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5136 case SCTP_ABORT_ASSOCIATION:
5137 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5139 if ((stcb) && netp && *netp)
5140 sctp_handle_abort((struct sctp_abort_chunk *)ch,
5146 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5148 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5151 SCTP_TCB_UNLOCK(locked_tcb);
5155 if (netp && *netp) {
5158 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5159 stcb, *netp, &abort_flag);
5166 case SCTP_SHUTDOWN_ACK:
5167 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", (void *)stcb);
5168 if ((stcb) && (netp) && (*netp))
5169 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5174 case SCTP_OPERATION_ERROR:
5175 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
5176 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
5181 case SCTP_COOKIE_ECHO:
5182 SCTPDBG(SCTP_DEBUG_INPUT3,
5183 "SCTP_COOKIE-ECHO, stcb %p\n", (void *)stcb);
5184 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5187 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5188 /* We are not interested anymore */
5191 SCTP_TCB_UNLOCK(stcb);
5198 * First are we accepting? We do this again here
5199 * since it is possible that a previous endpoint WAS
5200 * listening responded to a INIT-ACK and then
5201 * closed. We opened and bound.. and are now no
5204 * XXXGL: notes on checking listen queue length.
5205 * 1) SCTP_IS_LISTENING() doesn't necessarily mean
5206 * SOLISTENING(), because a listening "UDP type"
5207 * socket isn't listening in terms of the socket
5208 * layer. It is a normal data flow socket, that
5209 * can fork off new connections. Thus, we should
5210 * look into sol_qlen only in case we are !UDP.
5211 * 2) Checking sol_qlen in general requires locking
5212 * the socket, and this code lacks that.
5214 if ((stcb == NULL) &&
5215 (!SCTP_IS_LISTENING(inp) ||
5216 (!(inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
5217 inp->sctp_socket->sol_qlen >= inp->sctp_socket->sol_qlimit))) {
5218 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5219 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5220 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
5221 sctp_abort_association(inp, stcb, m, iphlen,
5222 src, dst, sh, op_err,
5229 struct mbuf *ret_buf;
5230 struct sctp_inpcb *linp;
5239 SCTP_ASOC_CREATE_LOCK(linp);
5240 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5241 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5242 SCTP_ASOC_CREATE_UNLOCK(linp);
5248 sctp_handle_cookie_echo(m, iphlen,
5252 (struct sctp_cookie_echo_chunk *)ch,
5266 SCTP_ASOC_CREATE_UNLOCK(linp);
5268 if (ret_buf == NULL) {
5270 SCTP_TCB_UNLOCK(locked_tcb);
5272 SCTPDBG(SCTP_DEBUG_INPUT3,
5273 "GAK, null buffer\n");
5277 /* if AUTH skipped, see if it verified... */
5282 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5284 * Restart the timer if we have
5287 struct sctp_tmit_chunk *chk;
5289 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5290 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5294 case SCTP_COOKIE_ACK:
5295 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", (void *)stcb);
5296 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5298 SCTP_TCB_UNLOCK(locked_tcb);
5302 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5303 /* We are not interested anymore */
5304 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5307 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5308 so = SCTP_INP_SO(inp);
5309 atomic_add_int(&stcb->asoc.refcnt, 1);
5310 SCTP_TCB_UNLOCK(stcb);
5311 SCTP_SOCKET_LOCK(so, 1);
5312 SCTP_TCB_LOCK(stcb);
5313 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5315 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5316 SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5317 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5318 SCTP_SOCKET_UNLOCK(so, 1);
5324 /* He's alive so give him credit */
5325 if ((stcb) && netp && *netp) {
5326 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5327 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5328 stcb->asoc.overall_error_count,
5330 SCTP_FROM_SCTP_INPUT,
5333 stcb->asoc.overall_error_count = 0;
5334 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5338 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5339 /* He's alive so give him credit */
5340 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5343 SCTP_TCB_UNLOCK(locked_tcb);
5349 if (stcb->asoc.ecn_supported == 0) {
5352 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5353 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5354 stcb->asoc.overall_error_count,
5356 SCTP_FROM_SCTP_INPUT,
5359 stcb->asoc.overall_error_count = 0;
5360 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5366 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5367 /* He's alive so give him credit */
5368 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5371 SCTP_TCB_UNLOCK(locked_tcb);
5377 if (stcb->asoc.ecn_supported == 0) {
5380 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5381 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5382 stcb->asoc.overall_error_count,
5384 SCTP_FROM_SCTP_INPUT,
5387 stcb->asoc.overall_error_count = 0;
5388 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5391 case SCTP_SHUTDOWN_COMPLETE:
5392 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", (void *)stcb);
5393 /* must be first and only chunk */
5394 if ((num_chunks > 1) ||
5395 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5398 SCTP_TCB_UNLOCK(locked_tcb);
5402 if ((stcb) && netp && *netp) {
5403 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5410 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5411 /* He's alive so give him credit */
5413 if (stcb->asoc.asconf_supported == 0) {
5416 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5417 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5418 stcb->asoc.overall_error_count,
5420 SCTP_FROM_SCTP_INPUT,
5423 stcb->asoc.overall_error_count = 0;
5424 sctp_handle_asconf(m, *offset, src,
5425 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5429 case SCTP_ASCONF_ACK:
5430 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5431 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5434 SCTP_TCB_UNLOCK(locked_tcb);
5439 if ((stcb) && netp && *netp) {
5440 if (stcb->asoc.asconf_supported == 0) {
5443 /* He's alive so give him credit */
5444 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5445 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5446 stcb->asoc.overall_error_count,
5448 SCTP_FROM_SCTP_INPUT,
5451 stcb->asoc.overall_error_count = 0;
5452 sctp_handle_asconf_ack(m, *offset,
5453 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5454 if (abort_no_unlock)
5458 case SCTP_FORWARD_CUM_TSN:
5459 case SCTP_IFORWARD_CUM_TSN:
5460 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5461 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5464 SCTP_TCB_UNLOCK(locked_tcb);
5469 /* He's alive so give him credit */
5473 if (stcb->asoc.prsctp_supported == 0) {
5476 stcb->asoc.overall_error_count = 0;
5477 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5478 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5479 stcb->asoc.overall_error_count,
5481 SCTP_FROM_SCTP_INPUT,
5485 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5486 /* We are not interested anymore */
5487 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5488 so = SCTP_INP_SO(inp);
5489 atomic_add_int(&stcb->asoc.refcnt, 1);
5490 SCTP_TCB_UNLOCK(stcb);
5491 SCTP_SOCKET_LOCK(so, 1);
5492 SCTP_TCB_LOCK(stcb);
5493 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5495 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5496 SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5497 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5498 SCTP_SOCKET_UNLOCK(so, 1);
5504 * For sending a SACK this looks like DATA
5507 stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from;
5508 sctp_handle_forward_tsn(stcb,
5509 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5514 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5515 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5516 stcb->asoc.overall_error_count,
5518 SCTP_FROM_SCTP_INPUT,
5521 stcb->asoc.overall_error_count = 0;
5526 case SCTP_STREAM_RESET:
5527 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5528 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5531 SCTP_TCB_UNLOCK(locked_tcb);
5536 if (stcb->asoc.reconfig_supported == 0) {
5539 if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5540 /* stop processing */
5545 case SCTP_PACKET_DROPPED:
5546 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5547 /* re-get it all please */
5548 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5551 SCTP_TCB_UNLOCK(locked_tcb);
5556 if (ch && (stcb) && netp && (*netp)) {
5557 if (stcb->asoc.pktdrop_supported == 0) {
5560 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5562 min(chk_length, (sizeof(chunk_buf) - 4)));
5566 case SCTP_AUTHENTICATION:
5567 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5569 /* save the first AUTH for later processing */
5570 if (auth_skipped == 0) {
5571 auth_offset = *offset;
5572 auth_len = chk_length;
5575 /* skip this chunk (temporarily) */
5578 if (stcb->asoc.auth_supported == 0) {
5581 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5582 (chk_length > (sizeof(struct sctp_auth_chunk) +
5583 SCTP_AUTH_DIGEST_LEN_MAX))) {
5586 SCTP_TCB_UNLOCK(locked_tcb);
5591 if (got_auth == 1) {
5592 /* skip this chunk... it's already auth'd */
5596 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5598 /* auth HMAC failed so dump the packet */
5602 /* remaining chunks are HMAC checked */
5603 stcb->asoc.authenticated = 1;
5609 /* it's an unknown chunk! */
5610 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5611 struct sctp_gen_error_cause *cause;
5614 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5615 0, M_NOWAIT, 1, MT_DATA);
5616 if (op_err != NULL) {
5617 len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset));
5618 cause = mtod(op_err, struct sctp_gen_error_cause *);
5619 cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5620 cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause)));
5621 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
5622 SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
5623 if (SCTP_BUF_NEXT(op_err) != NULL) {
5624 #ifdef SCTP_MBUF_LOGGING
5625 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5626 sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5629 sctp_queue_op_err(stcb, op_err);
5631 sctp_m_freem(op_err);
5635 if ((ch->chunk_type & 0x80) == 0) {
5636 /* discard this packet */
5639 } /* else skip this bad chunk and continue... */
5641 } /* switch (ch->chunk_type) */
5645 /* get the next chunk */
5646 *offset += SCTP_SIZE32(chk_length);
5647 if (*offset >= length) {
5648 /* no more data left in the mbuf chain */
5651 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5652 sizeof(struct sctp_chunkhdr), chunk_buf);
5655 SCTP_TCB_UNLOCK(locked_tcb);
5662 if (asconf_cnt > 0 && stcb != NULL) {
5663 sctp_send_asconf_ack(stcb);
5670 * common input chunk processing (v4 and v6)
5673 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5674 struct sockaddr *src, struct sockaddr *dst,
5675 struct sctphdr *sh, struct sctp_chunkhdr *ch,
5676 #if !defined(SCTP_WITH_NO_CSUM)
5677 uint8_t compute_crc,
5680 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5681 uint32_t vrf_id, uint16_t port)
5684 int fwd_tsn_seen = 0, data_processed = 0;
5685 struct mbuf *m = *mm, *op_err;
5686 char msg[SCTP_DIAG_INFO_LEN];
5688 int cnt_ctrl_ready = 0;
5689 struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5690 struct sctp_tcb *stcb = NULL;
5691 struct sctp_nets *net = NULL;
5693 SCTP_STAT_INCR(sctps_recvdatagrams);
5694 #ifdef SCTP_AUDITING_ENABLED
5695 sctp_audit_log(0xE0, 1);
5696 sctp_auditing(0, inp, stcb, net);
5698 #if !defined(SCTP_WITH_NO_CSUM)
5699 if (compute_crc != 0) {
5700 uint32_t check, calc_check;
5702 check = sh->checksum;
5704 calc_check = sctp_calculate_cksum(m, iphlen);
5705 sh->checksum = check;
5706 if (calc_check != check) {
5707 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5708 calc_check, check, (void *)m, length, iphlen);
5709 stcb = sctp_findassociation_addr(m, offset, src, dst,
5710 sh, ch, &inp, &net, vrf_id);
5711 #if defined(INET) || defined(INET6)
5712 if ((ch->chunk_type != SCTP_INITIATION) &&
5713 (net != NULL) && (net->port != port)) {
5714 if (net->port == 0) {
5715 /* UDP encapsulation turned on. */
5716 net->mtu -= sizeof(struct udphdr);
5717 if (stcb->asoc.smallest_mtu > net->mtu) {
5718 sctp_pathmtu_adjustment(stcb, net->mtu);
5720 } else if (port == 0) {
5721 /* UDP encapsulation turned off. */
5722 net->mtu += sizeof(struct udphdr);
5723 /* XXX Update smallest_mtu */
5729 net->flowtype = mflowtype;
5730 net->flowid = mflowid;
5732 if ((inp != NULL) && (stcb != NULL)) {
5733 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5734 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5735 } else if ((inp != NULL) && (stcb == NULL)) {
5738 SCTP_STAT_INCR(sctps_badsum);
5739 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5744 /* Destination port of 0 is illegal, based on RFC4960. */
5745 if (sh->dest_port == 0) {
5746 SCTP_STAT_INCR(sctps_hdrops);
5749 stcb = sctp_findassociation_addr(m, offset, src, dst,
5750 sh, ch, &inp, &net, vrf_id);
5751 #if defined(INET) || defined(INET6)
5752 if ((ch->chunk_type != SCTP_INITIATION) &&
5753 (net != NULL) && (net->port != port)) {
5754 if (net->port == 0) {
5755 /* UDP encapsulation turned on. */
5756 net->mtu -= sizeof(struct udphdr);
5757 if (stcb->asoc.smallest_mtu > net->mtu) {
5758 sctp_pathmtu_adjustment(stcb, net->mtu);
5760 } else if (port == 0) {
5761 /* UDP encapsulation turned off. */
5762 net->mtu += sizeof(struct udphdr);
5763 /* XXX Update smallest_mtu */
5769 net->flowtype = mflowtype;
5770 net->flowid = mflowid;
5773 SCTP_STAT_INCR(sctps_noport);
5774 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5777 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5778 sctp_send_shutdown_complete2(src, dst, sh,
5779 mflowtype, mflowid, fibnum,
5783 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5786 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5787 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5788 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5789 (ch->chunk_type != SCTP_INIT))) {
5790 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5792 sctp_send_abort(m, iphlen, src, dst,
5794 mflowtype, mflowid, fibnum,
5799 } else if (stcb == NULL) {
5802 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5803 (void *)m, iphlen, offset, length, (void *)stcb);
5805 /* always clear this before beginning a packet */
5806 stcb->asoc.authenticated = 0;
5807 stcb->asoc.seen_a_sack_this_pkt = 0;
5808 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5809 (void *)stcb, stcb->asoc.state);
5811 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5812 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5814 * If we hit here, we had a ref count
5815 * up when the assoc was aborted and the
5816 * timer is clearing out the assoc, we should
5817 * NOT respond to any packet.. its OOTB.
5819 SCTP_TCB_UNLOCK(stcb);
5821 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5822 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5824 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5825 mflowtype, mflowid, inp->fibnum,
5830 if (IS_SCTP_CONTROL(ch)) {
5831 /* process the control portion of the SCTP packet */
5832 /* sa_ignore NO_NULL_CHK */
5833 stcb = sctp_process_control(m, iphlen, &offset, length,
5835 inp, stcb, &net, &fwd_tsn_seen,
5836 mflowtype, mflowid, fibnum,
5840 * This covers us if the cookie-echo was there and
5841 * it changes our INP.
5843 inp = stcb->sctp_ep;
5844 #if defined(INET) || defined(INET6)
5845 if ((ch->chunk_type != SCTP_INITIATION) &&
5846 (net != NULL) && (net->port != port)) {
5847 if (net->port == 0) {
5848 /* UDP encapsulation turned on. */
5849 net->mtu -= sizeof(struct udphdr);
5850 if (stcb->asoc.smallest_mtu > net->mtu) {
5851 sctp_pathmtu_adjustment(stcb, net->mtu);
5853 } else if (port == 0) {
5854 /* UDP encapsulation turned off. */
5855 net->mtu += sizeof(struct udphdr);
5856 /* XXX Update smallest_mtu */
5864 * no control chunks, so pre-process DATA chunks (these
5865 * checks are taken care of by control processing)
5869 * if DATA only packet, and auth is required, then punt...
5870 * can't have authenticated without any AUTH (control)
5873 if ((stcb != NULL) &&
5874 (stcb->asoc.auth_supported == 1) &&
5875 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5876 /* "silently" ignore */
5877 SCTP_STAT_INCR(sctps_recvauthmissing);
5881 /* out of the blue DATA chunk */
5882 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5883 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5885 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5886 mflowtype, mflowid, fibnum,
5890 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5891 /* v_tag mismatch! */
5892 SCTP_STAT_INCR(sctps_badvtag);
5899 * no valid TCB for this packet, or we found it's a bad
5900 * packet while processing control, or we're done with this
5901 * packet (done or skip rest of data), so we drop it...
5906 * DATA chunk processing
5908 /* plow through the data chunks while length > offset */
5911 * Rest should be DATA only. Check authentication state if AUTH for
5914 if ((length > offset) &&
5916 (stcb->asoc.auth_supported == 1) &&
5917 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5918 !stcb->asoc.authenticated) {
5919 /* "silently" ignore */
5920 SCTP_STAT_INCR(sctps_recvauthmissing);
5921 SCTPDBG(SCTP_DEBUG_AUTH1,
5922 "Data chunk requires AUTH, skipped\n");
5925 if (length > offset) {
5929 * First check to make sure our state is correct. We would
5930 * not get here unless we really did have a tag, so we don't
5931 * abort if this happens, just dump the chunk silently.
5933 switch (SCTP_GET_STATE(&stcb->asoc)) {
5934 case SCTP_STATE_COOKIE_ECHOED:
5936 * we consider data with valid tags in this state
5937 * shows us the cookie-ack was lost. Imply it was
5940 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5941 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5942 stcb->asoc.overall_error_count,
5944 SCTP_FROM_SCTP_INPUT,
5947 stcb->asoc.overall_error_count = 0;
5948 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5950 case SCTP_STATE_COOKIE_WAIT:
5952 * We consider OOTB any data sent during asoc setup.
5954 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5955 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5957 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5958 mflowtype, mflowid, inp->fibnum,
5961 /* sa_ignore NOTREACHED */
5963 case SCTP_STATE_EMPTY: /* should not happen */
5964 case SCTP_STATE_INUSE: /* should not happen */
5965 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5966 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5969 /* sa_ignore NOTREACHED */
5971 case SCTP_STATE_OPEN:
5972 case SCTP_STATE_SHUTDOWN_SENT:
5975 /* plow through the data chunks while length > offset */
5976 retval = sctp_process_data(mm, iphlen, &offset, length,
5977 inp, stcb, net, &high_tsn);
5980 * The association aborted, NO UNLOCK needed since
5981 * the association is destroyed.
5988 * Anything important needs to have been m_copy'ed in
5992 /* take care of ecn */
5993 if ((data_processed == 1) &&
5994 (stcb->asoc.ecn_supported == 1) &&
5995 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5996 /* Yep, we need to add a ECNE */
5997 sctp_send_ecn_echo(stcb, net, high_tsn);
5999 if ((data_processed == 0) && (fwd_tsn_seen)) {
6001 uint32_t highest_tsn;
6003 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
6004 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
6006 highest_tsn = stcb->asoc.highest_tsn_inside_map;
6008 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
6009 stcb->asoc.send_sack = 1;
6010 sctp_sack_check(stcb, was_a_gap);
6011 } else if (fwd_tsn_seen) {
6012 stcb->asoc.send_sack = 1;
6014 /* trigger send of any chunks in queue... */
6016 #ifdef SCTP_AUDITING_ENABLED
6017 sctp_audit_log(0xE0, 2);
6018 sctp_auditing(1, inp, stcb, net);
6020 SCTPDBG(SCTP_DEBUG_INPUT1,
6021 "Check for chunk output prw:%d tqe:%d tf=%d\n",
6022 stcb->asoc.peers_rwnd,
6023 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
6024 stcb->asoc.total_flight);
6025 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
6026 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
6027 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
6029 if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
6031 stcb->asoc.trigger_reset ||
6033 (stcb->asoc.peers_rwnd > 0 ||
6034 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
6035 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
6036 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
6037 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
6039 #ifdef SCTP_AUDITING_ENABLED
6040 sctp_audit_log(0xE0, 3);
6041 sctp_auditing(2, inp, stcb, net);
6045 SCTP_TCB_UNLOCK(stcb);
6047 if (inp_decr != NULL) {
6048 /* reduce ref-count */
6049 SCTP_INP_WLOCK(inp_decr);
6050 SCTP_INP_DECR_REF(inp_decr);
6051 SCTP_INP_WUNLOCK(inp_decr);
6058 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
6062 uint32_t vrf_id = 0;
6064 struct sockaddr_in src, dst;
6067 struct sctp_chunkhdr *ch;
6069 #if !defined(SCTP_WITH_NO_CSUM)
6070 uint8_t compute_crc;
6077 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
6078 SCTP_RELEASE_PKT(i_pak);
6081 m = SCTP_HEADER_TO_CHAIN(i_pak);
6082 #ifdef SCTP_MBUF_LOGGING
6083 /* Log in any input mbufs */
6084 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6085 sctp_log_mbc(m, SCTP_MBUF_INPUT);
6088 #ifdef SCTP_PACKET_LOGGING
6089 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
6093 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6094 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
6096 if_name(m->m_pkthdr.rcvif),
6097 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
6098 mflowid = m->m_pkthdr.flowid;
6099 mflowtype = M_HASHTYPE_GET(m);
6100 fibnum = M_GETFIB(m);
6101 SCTP_STAT_INCR(sctps_recvpackets);
6102 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
6103 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
6104 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6105 if (SCTP_BUF_LEN(m) < offset) {
6106 if ((m = m_pullup(m, offset)) == NULL) {
6107 SCTP_STAT_INCR(sctps_hdrops);
6111 ip = mtod(m, struct ip *);
6112 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
6113 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
6114 offset -= sizeof(struct sctp_chunkhdr);
6115 memset(&src, 0, sizeof(struct sockaddr_in));
6116 src.sin_family = AF_INET;
6117 src.sin_len = sizeof(struct sockaddr_in);
6118 src.sin_port = sh->src_port;
6119 src.sin_addr = ip->ip_src;
6120 memset(&dst, 0, sizeof(struct sockaddr_in));
6121 dst.sin_family = AF_INET;
6122 dst.sin_len = sizeof(struct sockaddr_in);
6123 dst.sin_port = sh->dest_port;
6124 dst.sin_addr = ip->ip_dst;
6125 length = ntohs(ip->ip_len);
6126 /* Validate mbuf chain length with IP payload length. */
6127 if (SCTP_HEADER_LEN(m) != length) {
6128 SCTPDBG(SCTP_DEBUG_INPUT1,
6129 "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
6130 SCTP_STAT_INCR(sctps_hdrops);
6133 /* SCTP does not allow broadcasts or multicasts */
6134 if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
6137 if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
6140 ecn_bits = ip->ip_tos;
6141 #if defined(SCTP_WITH_NO_CSUM)
6142 SCTP_STAT_INCR(sctps_recvnocrc);
6144 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
6145 SCTP_STAT_INCR(sctps_recvhwcrc);
6148 SCTP_STAT_INCR(sctps_recvswcrc);
6152 sctp_common_input_processing(&m, iphlen, offset, length,
6153 (struct sockaddr *)&src,
6154 (struct sockaddr *)&dst,
6156 #if !defined(SCTP_WITH_NO_CSUM)
6160 mflowtype, mflowid, fibnum,
6169 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6170 extern int *sctp_cpuarry;
6174 sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
6181 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6187 uint32_t flowid, tag;
6189 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
6190 flowid = m->m_pkthdr.flowid;
6193 * No flow id built by lower layers fix it so we
6196 offset = off + sizeof(struct sctphdr);
6197 if (SCTP_BUF_LEN(m) < offset) {
6198 if ((m = m_pullup(m, offset)) == NULL) {
6199 SCTP_STAT_INCR(sctps_hdrops);
6200 return (IPPROTO_DONE);
6203 ip = mtod(m, struct ip *);
6204 sh = (struct sctphdr *)((caddr_t)ip + off);
6205 tag = htonl(sh->v_tag);
6206 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6207 m->m_pkthdr.flowid = flowid;
6208 M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH);
6210 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6211 sctp_queue_to_mcore(m, off, cpu_to_use);
6212 return (IPPROTO_DONE);
6215 sctp_input_with_port(m, off, 0);
6216 return (IPPROTO_DONE);