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,
204 SCTP_INP_RUNLOCK(inp);
209 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
213 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
214 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
221 struct sctp_stream_queue_pending *sp;
222 struct sctp_association *asoc;
225 * This function returns if any stream has true unsent data on it.
226 * Note that as it looks through it will clean up any places that
227 * have old data that has been sent but left at top of stream queue.
231 SCTP_TCB_SEND_LOCK(stcb);
232 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
233 /* Check to see if some data queued */
234 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
235 /* sa_ignore FREED_MEMORY */
236 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
240 if ((sp->msg_is_complete) &&
242 (sp->sender_all_done)) {
244 * We are doing differed cleanup. Last time
245 * through when we took all the data the
246 * sender_all_done was not set.
248 if (sp->put_last_out == 0) {
249 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
250 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
256 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
257 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
258 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, &asoc->strmout[i], sp, 1);
260 sctp_free_remote_addr(sp->net);
264 sctp_m_freem(sp->data);
267 sctp_free_a_strmoq(stcb, sp, so_locked);
268 if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
274 if (unsent_data > 0) {
279 SCTP_TCB_SEND_UNLOCK(stcb);
280 return (unsent_data);
284 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
286 struct sctp_init *init;
287 struct sctp_association *asoc;
288 struct sctp_nets *lnet;
293 /* save off parameters */
294 asoc->peer_vtag = ntohl(init->initiate_tag);
295 asoc->peers_rwnd = ntohl(init->a_rwnd);
297 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
299 if (!TAILQ_EMPTY(&asoc->nets)) {
300 /* update any ssthresh's that may have a default */
301 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
302 lnet->ssthresh = asoc->peers_rwnd;
303 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
304 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
308 SCTP_TCB_SEND_LOCK(stcb);
309 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
311 struct sctp_stream_out *outs;
312 struct sctp_stream_queue_pending *sp, *nsp;
313 struct sctp_tmit_chunk *chk, *nchk;
315 /* abandon the upper streams */
316 newcnt = ntohs(init->num_inbound_streams);
317 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
318 if (chk->rec.data.sid >= newcnt) {
319 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
320 asoc->send_queue_cnt--;
321 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
322 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
325 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
328 if (chk->data != NULL) {
329 sctp_free_bufspace(stcb, asoc, chk, 1);
330 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
331 0, chk, SCTP_SO_NOT_LOCKED);
333 sctp_m_freem(chk->data);
337 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
338 /* sa_ignore FREED_MEMORY */
342 for (i = newcnt; i < asoc->pre_open_streams; i++) {
343 outs = &asoc->strmout[i];
344 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
345 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
346 TAILQ_REMOVE(&outs->outqueue, sp, next);
347 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
348 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
349 stcb, 0, sp, SCTP_SO_NOT_LOCKED);
351 sctp_m_freem(sp->data);
355 sctp_free_remote_addr(sp->net);
359 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
360 /* sa_ignore FREED_MEMORY */
362 outs->state = SCTP_STREAM_CLOSED;
365 /* cut back the count */
366 asoc->pre_open_streams = newcnt;
368 SCTP_TCB_SEND_UNLOCK(stcb);
369 asoc->streamoutcnt = asoc->pre_open_streams;
371 for (i = 0; i < asoc->streamoutcnt; i++) {
372 asoc->strmout[i].state = SCTP_STREAM_OPEN;
375 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
376 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
377 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
378 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
380 /* This is the next one we expect */
381 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
383 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
384 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
386 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
387 /* open the requested streams */
389 if (asoc->strmin != NULL) {
390 /* Free the old ones */
391 for (i = 0; i < asoc->streamincnt; i++) {
392 sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
393 sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
395 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
397 if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
398 asoc->streamincnt = ntohs(init->num_outbound_streams);
400 asoc->streamincnt = asoc->max_inbound_streams;
402 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
403 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
404 if (asoc->strmin == NULL) {
405 /* we didn't get memory for the streams! */
406 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
409 for (i = 0; i < asoc->streamincnt; i++) {
410 asoc->strmin[i].sid = i;
411 asoc->strmin[i].last_mid_delivered = 0xffffffff;
412 TAILQ_INIT(&asoc->strmin[i].inqueue);
413 TAILQ_INIT(&asoc->strmin[i].uno_inqueue);
414 asoc->strmin[i].pd_api_started = 0;
415 asoc->strmin[i].delivery_started = 0;
418 * load_address_from_init will put the addresses into the
419 * association when the COOKIE is processed or the INIT-ACK is
420 * processed. Both types of COOKIE's existing and new call this
421 * routine. It will remove addresses that are no longer in the
422 * association (for the restarting case where addresses are
423 * removed). Up front when the INIT arrives we will discard it if it
424 * is a restart and new addresses have been added.
426 /* sa_ignore MEMLEAK */
431 * INIT-ACK message processing/consumption returns value < 0 on error
434 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
435 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
436 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
437 struct sctp_nets *net, int *abort_no_unlock,
438 uint8_t mflowtype, uint32_t mflowid,
441 struct sctp_association *asoc;
443 int retval, abort_flag;
444 uint32_t initack_limit;
445 int nat_friendly = 0;
447 /* First verify that we have no illegal param's */
450 op_err = sctp_arethere_unrecognized_parameters(m,
451 (offset + sizeof(struct sctp_init_chunk)),
452 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
454 /* Send an abort and notify peer */
455 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
456 *abort_no_unlock = 1;
460 asoc->peer_supports_nat = (uint8_t)nat_friendly;
461 /* process the peer's parameters in the INIT-ACK */
462 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
466 initack_limit = offset + ntohs(cp->ch.chunk_length);
467 /* load all addresses */
468 if ((retval = sctp_load_addresses_from_init(stcb, m,
469 (offset + sizeof(struct sctp_init_chunk)), initack_limit,
470 src, dst, NULL, stcb->asoc.port))) {
471 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
472 "Problem with address parameters");
473 SCTPDBG(SCTP_DEBUG_INPUT1,
474 "Load addresses from INIT causes an abort %d\n",
476 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
477 src, dst, sh, op_err,
480 *abort_no_unlock = 1;
483 /* if the peer doesn't support asconf, flush the asconf queue */
484 if (asoc->asconf_supported == 0) {
485 struct sctp_asconf_addr *param, *nparam;
487 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
488 TAILQ_REMOVE(&asoc->asconf_queue, param, next);
489 SCTP_FREE(param, SCTP_M_ASC_ADDR);
492 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
493 stcb->asoc.local_hmacs);
495 sctp_queue_op_err(stcb, op_err);
496 /* queuing will steal away the mbuf chain to the out queue */
499 /* extract the cookie and queue it to "echo" it back... */
500 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
501 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
502 stcb->asoc.overall_error_count,
504 SCTP_FROM_SCTP_INPUT,
507 stcb->asoc.overall_error_count = 0;
508 net->error_count = 0;
511 * Cancel the INIT timer, We do this first before queueing the
512 * cookie. We always cancel at the primary to assue that we are
513 * canceling the timer started by the INIT which always goes to the
516 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
517 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
519 /* calculate the RTO */
520 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
521 SCTP_RTT_FROM_NON_DATA);
522 retval = sctp_send_cookie_echo(m, offset, stcb, net);
525 * No cookie, we probably should send a op error. But in any
526 * case if there is no cookie in the INIT-ACK, we can
527 * abandon the peer, its broke.
532 len = (uint16_t)(sizeof(struct sctp_error_missing_param) + sizeof(uint16_t));
533 /* We abort with an error of missing mandatory param */
534 op_err = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
535 if (op_err != NULL) {
536 struct sctp_error_missing_param *cause;
538 SCTP_BUF_LEN(op_err) = len;
539 cause = mtod(op_err, struct sctp_error_missing_param *);
540 /* Subtract the reserved param */
541 cause->cause.code = htons(SCTP_CAUSE_MISSING_PARAM);
542 cause->cause.length = htons(len);
543 cause->num_missing_params = htonl(1);
544 cause->type[0] = htons(SCTP_STATE_COOKIE);
546 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
547 src, dst, sh, op_err,
550 *abort_no_unlock = 1;
558 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
559 struct sctp_tcb *stcb, struct sctp_nets *net)
561 union sctp_sockstore store;
562 struct sctp_nets *r_net, *f_net;
565 uint16_t old_error_counter;
567 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
571 memset(&store, 0, sizeof(store));
572 switch (cp->heartbeat.hb_info.addr_family) {
575 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
576 store.sin.sin_family = cp->heartbeat.hb_info.addr_family;
577 store.sin.sin_len = cp->heartbeat.hb_info.addr_len;
578 store.sin.sin_port = stcb->rport;
579 memcpy(&store.sin.sin_addr, cp->heartbeat.hb_info.address,
580 sizeof(store.sin.sin_addr));
588 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
589 store.sin6.sin6_family = cp->heartbeat.hb_info.addr_family;
590 store.sin6.sin6_len = cp->heartbeat.hb_info.addr_len;
591 store.sin6.sin6_port = stcb->rport;
592 memcpy(&store.sin6.sin6_addr, cp->heartbeat.hb_info.address, sizeof(struct in6_addr));
601 r_net = sctp_findnet(stcb, &store.sa);
603 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
606 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
607 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
608 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
610 * If the its a HB and it's random value is correct when can
611 * confirm the destination.
613 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
614 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
615 stcb->asoc.primary_destination = r_net;
616 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
617 f_net = TAILQ_FIRST(&stcb->asoc.nets);
618 if (f_net != r_net) {
620 * first one on the list is NOT the primary
621 * sctp_cmpaddr() is much more efficient if
622 * the primary is the first on the list,
625 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
626 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
630 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
631 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
632 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
633 r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
634 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
636 old_error_counter = r_net->error_count;
637 r_net->error_count = 0;
638 r_net->hb_responded = 1;
639 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
640 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
641 /* Now lets do a RTO with this */
642 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv,
643 SCTP_RTT_FROM_NON_DATA);
644 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
645 r_net->dest_state |= SCTP_ADDR_REACHABLE;
646 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
647 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
649 if (r_net->dest_state & SCTP_ADDR_PF) {
650 r_net->dest_state &= ~SCTP_ADDR_PF;
651 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
653 if (old_error_counter > 0) {
654 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
655 stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
656 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
658 if (r_net == stcb->asoc.primary_destination) {
659 if (stcb->asoc.alternate) {
660 /* release the alternate, primary is good */
661 sctp_free_remote_addr(stcb->asoc.alternate);
662 stcb->asoc.alternate = NULL;
665 /* Mobility adaptation */
667 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
668 SCTP_MOBILITY_BASE) ||
669 sctp_is_mobility_feature_on(stcb->sctp_ep,
670 SCTP_MOBILITY_FASTHANDOFF)) &&
671 sctp_is_mobility_feature_on(stcb->sctp_ep,
672 SCTP_MOBILITY_PRIM_DELETED)) {
674 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
675 stcb->sctp_ep, stcb, NULL,
676 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
677 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
678 SCTP_MOBILITY_FASTHANDOFF)) {
679 sctp_assoc_immediate_retrans(stcb,
680 stcb->asoc.primary_destination);
682 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
683 SCTP_MOBILITY_BASE)) {
684 sctp_move_chunks_from_net(stcb,
685 stcb->asoc.deleted_primary);
687 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
688 stcb->asoc.deleted_primary);
694 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
697 * return 0 means we want you to proceed with the abort non-zero
698 * means no abort processing
700 struct sctpasochead *head;
702 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) ||
703 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
704 atomic_add_int(&stcb->asoc.refcnt, 1);
705 SCTP_TCB_UNLOCK(stcb);
706 SCTP_INP_INFO_WLOCK();
708 atomic_subtract_int(&stcb->asoc.refcnt, 1);
710 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
711 /* generate a new vtag and send init */
712 LIST_REMOVE(stcb, sctp_asocs);
713 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
714 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
716 * put it in the bucket in the vtag hash of assoc's for the
719 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
720 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
721 SCTP_INP_INFO_WUNLOCK();
724 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
726 * treat like a case where the cookie expired i.e.: - dump
727 * current cookie. - generate a new vtag. - resend init.
729 /* generate a new vtag and send init */
730 LIST_REMOVE(stcb, sctp_asocs);
731 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
732 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
733 sctp_stop_all_cookie_timers(stcb);
734 sctp_toss_old_cookies(stcb, &stcb->asoc);
735 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
736 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
738 * put it in the bucket in the vtag hash of assoc's for the
741 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
742 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
743 SCTP_INP_INFO_WUNLOCK();
750 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
751 struct sctp_nets *net)
754 * return 0 means we want you to proceed with the abort non-zero
755 * means no abort processing
757 if (stcb->asoc.auth_supported == 0) {
758 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
761 sctp_asconf_send_nat_state_update(stcb, net);
766 /* Returns 1 if the stcb was aborted, 0 otherwise */
768 sctp_handle_abort(struct sctp_abort_chunk *abort,
769 struct sctp_tcb *stcb, struct sctp_nets *net)
771 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
777 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
781 len = ntohs(abort->ch.chunk_length);
782 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_error_cause)) {
784 * Need to check the cause codes for our two magic nat
785 * aborts which don't kill the assoc necessarily.
787 struct sctp_error_cause *cause;
789 cause = (struct sctp_error_cause *)(abort + 1);
790 error = ntohs(cause->code);
791 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
792 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
793 abort->ch.chunk_flags);
794 if (sctp_handle_nat_colliding_state(stcb)) {
797 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
798 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
799 abort->ch.chunk_flags);
800 if (sctp_handle_nat_missing_state(stcb, net)) {
807 /* stop any receive timers */
808 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net,
809 SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
810 /* notify user of the abort and clean up... */
811 sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
813 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
814 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
815 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
816 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
818 #ifdef SCTP_ASOCLOG_OF_TSNS
819 sctp_print_out_track_log(stcb);
821 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
822 so = SCTP_INP_SO(stcb->sctp_ep);
823 atomic_add_int(&stcb->asoc.refcnt, 1);
824 SCTP_TCB_UNLOCK(stcb);
825 SCTP_SOCKET_LOCK(so, 1);
827 atomic_subtract_int(&stcb->asoc.refcnt, 1);
829 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
830 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
831 SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
832 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
833 SCTP_SOCKET_UNLOCK(so, 1);
835 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
840 sctp_start_net_timers(struct sctp_tcb *stcb)
842 uint32_t cnt_hb_sent;
843 struct sctp_nets *net;
846 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
848 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
849 * If the dest in unconfirmed send a hb as well if under
850 * max_hb_burst have been sent.
852 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
853 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
854 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
855 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
856 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
861 sctp_chunk_output(stcb->sctp_ep, stcb,
862 SCTP_OUTPUT_FROM_COOKIE_ACK,
869 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
870 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
872 struct sctp_association *asoc;
873 int some_on_streamwheel;
875 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
879 SCTPDBG(SCTP_DEBUG_INPUT2,
880 "sctp_handle_shutdown: handling SHUTDOWN\n");
884 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
885 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
888 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
889 /* Shutdown NOT the expected size */
892 old_state = SCTP_GET_STATE(asoc);
893 sctp_update_acked(stcb, cp, abort_flag);
897 if (asoc->control_pdapi) {
899 * With a normal shutdown we assume the end of last record.
901 SCTP_INP_READ_LOCK(stcb->sctp_ep);
902 if (asoc->control_pdapi->on_strm_q) {
903 struct sctp_stream_in *strm;
905 strm = &asoc->strmin[asoc->control_pdapi->sinfo_stream];
906 if (asoc->control_pdapi->on_strm_q == SCTP_ON_UNORDERED) {
908 TAILQ_REMOVE(&strm->uno_inqueue, asoc->control_pdapi, next_instrm);
909 asoc->control_pdapi->on_strm_q = 0;
910 } else if (asoc->control_pdapi->on_strm_q == SCTP_ON_ORDERED) {
912 TAILQ_REMOVE(&strm->inqueue, asoc->control_pdapi, next_instrm);
913 asoc->control_pdapi->on_strm_q = 0;
916 panic("Unknown state on ctrl:%p on_strm_q:%d",
918 asoc->control_pdapi->on_strm_q);
922 asoc->control_pdapi->end_added = 1;
923 asoc->control_pdapi->pdapi_aborted = 1;
924 asoc->control_pdapi = NULL;
925 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
926 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
927 so = SCTP_INP_SO(stcb->sctp_ep);
928 atomic_add_int(&stcb->asoc.refcnt, 1);
929 SCTP_TCB_UNLOCK(stcb);
930 SCTP_SOCKET_LOCK(so, 1);
932 atomic_subtract_int(&stcb->asoc.refcnt, 1);
933 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
934 /* assoc was freed while we were unlocked */
935 SCTP_SOCKET_UNLOCK(so, 1);
939 if (stcb->sctp_socket) {
940 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
942 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
943 SCTP_SOCKET_UNLOCK(so, 1);
946 /* goto SHUTDOWN_RECEIVED state to block new requests */
947 if (stcb->sctp_socket) {
948 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
949 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
950 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
951 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
952 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
954 * notify upper layer that peer has initiated a
957 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
960 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
963 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
965 * stop the shutdown timer, since we WILL move to
968 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
969 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
971 /* Now is there unsent data on a stream somewhere? */
972 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
974 if (!TAILQ_EMPTY(&asoc->send_queue) ||
975 !TAILQ_EMPTY(&asoc->sent_queue) ||
976 some_on_streamwheel) {
977 /* By returning we will push more data out */
980 /* no outstanding data to send, so move on... */
981 /* send SHUTDOWN-ACK */
982 /* move to SHUTDOWN-ACK-SENT state */
983 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
984 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
985 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
987 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
988 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
989 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
990 sctp_stop_timers_for_shutdown(stcb);
991 sctp_send_shutdown_ack(stcb, net);
992 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
993 stcb->sctp_ep, stcb, net);
994 } else if (old_state == SCTP_STATE_SHUTDOWN_ACK_SENT) {
995 sctp_send_shutdown_ack(stcb, net);
1001 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
1002 struct sctp_tcb *stcb,
1003 struct sctp_nets *net)
1005 struct sctp_association *asoc;
1006 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1009 so = SCTP_INP_SO(stcb->sctp_ep);
1011 SCTPDBG(SCTP_DEBUG_INPUT2,
1012 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
1017 /* process according to association state */
1018 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
1019 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
1020 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
1021 sctp_send_shutdown_complete(stcb, net, 1);
1022 SCTP_TCB_UNLOCK(stcb);
1025 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
1026 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1027 /* unexpected SHUTDOWN-ACK... so ignore... */
1028 SCTP_TCB_UNLOCK(stcb);
1031 if (asoc->control_pdapi) {
1033 * With a normal shutdown we assume the end of last record.
1035 SCTP_INP_READ_LOCK(stcb->sctp_ep);
1036 asoc->control_pdapi->end_added = 1;
1037 asoc->control_pdapi->pdapi_aborted = 1;
1038 asoc->control_pdapi = NULL;
1039 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1040 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1041 atomic_add_int(&stcb->asoc.refcnt, 1);
1042 SCTP_TCB_UNLOCK(stcb);
1043 SCTP_SOCKET_LOCK(so, 1);
1044 SCTP_TCB_LOCK(stcb);
1045 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1046 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1047 /* assoc was freed while we were unlocked */
1048 SCTP_SOCKET_UNLOCK(so, 1);
1052 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1053 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1054 SCTP_SOCKET_UNLOCK(so, 1);
1058 if (!TAILQ_EMPTY(&asoc->send_queue) ||
1059 !TAILQ_EMPTY(&asoc->sent_queue) ||
1060 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
1061 panic("Queues are not empty when handling SHUTDOWN-ACK");
1064 /* stop the timer */
1065 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net,
1066 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1067 /* send SHUTDOWN-COMPLETE */
1068 sctp_send_shutdown_complete(stcb, net, 0);
1069 /* notify upper layer protocol */
1070 if (stcb->sctp_socket) {
1071 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1072 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1073 stcb->sctp_socket->so_snd.sb_cc = 0;
1075 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1077 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1078 /* free the TCB but first save off the ep */
1079 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1080 atomic_add_int(&stcb->asoc.refcnt, 1);
1081 SCTP_TCB_UNLOCK(stcb);
1082 SCTP_SOCKET_LOCK(so, 1);
1083 SCTP_TCB_LOCK(stcb);
1084 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1086 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1087 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1088 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1089 SCTP_SOCKET_UNLOCK(so, 1);
1094 * Skip past the param header and then we will find the chunk that caused the
1095 * problem. There are two possibilities ASCONF or FWD-TSN other than that and
1096 * our peer must be broken.
1099 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1100 struct sctp_nets *net)
1102 struct sctp_chunkhdr *chk;
1104 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1105 switch (chk->chunk_type) {
1106 case SCTP_ASCONF_ACK:
1108 sctp_asconf_cleanup(stcb, net);
1110 case SCTP_IFORWARD_CUM_TSN:
1111 case SCTP_FORWARD_CUM_TSN:
1112 stcb->asoc.prsctp_supported = 0;
1115 SCTPDBG(SCTP_DEBUG_INPUT2,
1116 "Peer does not support chunk type %d(%x)??\n",
1117 chk->chunk_type, (uint32_t)chk->chunk_type);
1123 * Skip past the param header and then we will find the param that caused the
1124 * problem. There are a number of param's in a ASCONF OR the prsctp param
1125 * these will turn of specific features.
1126 * XXX: Is this the right thing to do?
1129 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1131 struct sctp_paramhdr *pbad;
1134 switch (ntohs(pbad->param_type)) {
1136 case SCTP_PRSCTP_SUPPORTED:
1137 stcb->asoc.prsctp_supported = 0;
1139 case SCTP_SUPPORTED_CHUNK_EXT:
1141 /* draft-ietf-tsvwg-addip-sctp */
1142 case SCTP_HAS_NAT_SUPPORT:
1143 stcb->asoc.peer_supports_nat = 0;
1145 case SCTP_ADD_IP_ADDRESS:
1146 case SCTP_DEL_IP_ADDRESS:
1147 case SCTP_SET_PRIM_ADDR:
1148 stcb->asoc.asconf_supported = 0;
1150 case SCTP_SUCCESS_REPORT:
1151 case SCTP_ERROR_CAUSE_IND:
1152 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1153 SCTPDBG(SCTP_DEBUG_INPUT2,
1154 "Turning off ASCONF to this strange peer\n");
1155 stcb->asoc.asconf_supported = 0;
1158 SCTPDBG(SCTP_DEBUG_INPUT2,
1159 "Peer does not support param type %d(%x)??\n",
1160 pbad->param_type, (uint32_t)pbad->param_type);
1166 sctp_handle_error(struct sctp_chunkhdr *ch,
1167 struct sctp_tcb *stcb, struct sctp_nets *net)
1170 struct sctp_paramhdr *phdr;
1171 uint16_t error, error_type;
1173 struct sctp_association *asoc;
1175 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1179 /* parse through all of the errors and process */
1181 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1182 sizeof(struct sctp_chunkhdr));
1183 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1185 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1186 /* Process an Error Cause */
1187 error_type = ntohs(phdr->param_type);
1188 error_len = ntohs(phdr->param_length);
1189 if ((error_len > chklen) || (error_len == 0)) {
1190 /* invalid param length for this param */
1191 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1196 /* report the first error cause */
1199 switch (error_type) {
1200 case SCTP_CAUSE_INVALID_STREAM:
1201 case SCTP_CAUSE_MISSING_PARAM:
1202 case SCTP_CAUSE_INVALID_PARAM:
1203 case SCTP_CAUSE_NO_USER_DATA:
1204 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1207 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1208 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1210 if (sctp_handle_nat_colliding_state(stcb)) {
1214 case SCTP_CAUSE_NAT_MISSING_STATE:
1215 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1217 if (sctp_handle_nat_missing_state(stcb, net)) {
1221 case SCTP_CAUSE_STALE_COOKIE:
1223 * We only act if we have echoed a cookie and are
1226 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1229 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1230 /* Save the time doubled */
1231 asoc->cookie_preserve_req = ntohl(*p) << 1;
1232 asoc->stale_cookie_count++;
1233 if (asoc->stale_cookie_count >
1234 asoc->max_init_times) {
1235 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1236 /* now free the asoc */
1237 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1238 so = SCTP_INP_SO(stcb->sctp_ep);
1239 atomic_add_int(&stcb->asoc.refcnt, 1);
1240 SCTP_TCB_UNLOCK(stcb);
1241 SCTP_SOCKET_LOCK(so, 1);
1242 SCTP_TCB_LOCK(stcb);
1243 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1245 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1246 SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1247 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1248 SCTP_SOCKET_UNLOCK(so, 1);
1252 /* blast back to INIT state */
1253 sctp_toss_old_cookies(stcb, &stcb->asoc);
1254 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1255 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1256 sctp_stop_all_cookie_timers(stcb);
1257 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1260 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1262 * Nothing we can do here, we don't do hostname
1263 * addresses so if the peer does not like my IPv6
1264 * (or IPv4 for that matter) it does not matter. If
1265 * they don't support that type of address, they can
1266 * NOT possibly get that packet type... i.e. with no
1267 * IPv6 you can't receive a IPv6 packet. so we can
1268 * safely ignore this one. If we ever added support
1269 * for HOSTNAME Addresses, then we would need to do
1273 case SCTP_CAUSE_UNRECOG_CHUNK:
1274 sctp_process_unrecog_chunk(stcb, phdr, net);
1276 case SCTP_CAUSE_UNRECOG_PARAM:
1277 sctp_process_unrecog_param(stcb, phdr);
1279 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1281 * We ignore this since the timer will drive out a
1282 * new cookie anyway and there timer will drive us
1283 * to send a SHUTDOWN_COMPLETE. We can't send one
1284 * here since we don't have their tag.
1287 case SCTP_CAUSE_DELETING_LAST_ADDR:
1288 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1289 case SCTP_CAUSE_DELETING_SRC_ADDR:
1291 * We should NOT get these here, but in a
1294 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1297 case SCTP_CAUSE_OUT_OF_RESC:
1299 * And what, pray tell do we do with the fact that
1300 * the peer is out of resources? Not really sure we
1301 * could do anything but abort. I suspect this
1302 * should have came WITH an abort instead of in a
1307 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1311 adjust = SCTP_SIZE32(error_len);
1313 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1315 sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, error, ch, SCTP_SO_NOT_LOCKED);
1320 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1321 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1322 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1323 struct sctp_nets *net, int *abort_no_unlock,
1324 uint8_t mflowtype, uint32_t mflowid,
1327 struct sctp_init_ack *init_ack;
1328 struct mbuf *op_err;
1330 SCTPDBG(SCTP_DEBUG_INPUT2,
1331 "sctp_handle_init_ack: handling INIT-ACK\n");
1334 SCTPDBG(SCTP_DEBUG_INPUT2,
1335 "sctp_handle_init_ack: TCB is null\n");
1338 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1339 /* Invalid length */
1340 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1341 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1342 src, dst, sh, op_err,
1345 *abort_no_unlock = 1;
1348 init_ack = &cp->init;
1349 /* validate parameters */
1350 if (init_ack->initiate_tag == 0) {
1351 /* protocol error... send an abort */
1352 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1353 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1354 src, dst, sh, op_err,
1357 *abort_no_unlock = 1;
1360 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1361 /* protocol error... send an abort */
1362 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1363 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1364 src, dst, sh, op_err,
1367 *abort_no_unlock = 1;
1370 if (init_ack->num_inbound_streams == 0) {
1371 /* protocol error... send an abort */
1372 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1373 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1374 src, dst, sh, op_err,
1377 *abort_no_unlock = 1;
1380 if (init_ack->num_outbound_streams == 0) {
1381 /* protocol error... send an abort */
1382 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1383 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1384 src, dst, sh, op_err,
1387 *abort_no_unlock = 1;
1390 /* process according to association state... */
1391 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1392 case SCTP_STATE_COOKIE_WAIT:
1393 /* this is the expected state for this chunk */
1394 /* process the INIT-ACK parameters */
1395 if (stcb->asoc.primary_destination->dest_state &
1396 SCTP_ADDR_UNCONFIRMED) {
1398 * The primary is where we sent the INIT, we can
1399 * always consider it confirmed when the INIT-ACK is
1400 * returned. Do this before we load addresses
1403 stcb->asoc.primary_destination->dest_state &=
1404 ~SCTP_ADDR_UNCONFIRMED;
1405 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1406 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1408 if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1409 net, abort_no_unlock,
1412 /* error in parsing parameters */
1415 /* update our state */
1416 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1417 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1419 /* reset the RTO calc */
1420 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1421 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1422 stcb->asoc.overall_error_count,
1424 SCTP_FROM_SCTP_INPUT,
1427 stcb->asoc.overall_error_count = 0;
1428 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1430 * collapse the init timer back in case of a exponential
1433 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1436 * the send at the end of the inbound data processing will
1437 * cause the cookie to be sent
1440 case SCTP_STATE_SHUTDOWN_SENT:
1441 /* incorrect state... discard */
1443 case SCTP_STATE_COOKIE_ECHOED:
1444 /* incorrect state... discard */
1446 case SCTP_STATE_OPEN:
1447 /* incorrect state... discard */
1449 case SCTP_STATE_EMPTY:
1450 case SCTP_STATE_INUSE:
1452 /* incorrect state... discard */
1456 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1460 static struct sctp_tcb *
1461 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1462 struct sockaddr *src, struct sockaddr *dst,
1463 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1464 struct sctp_inpcb *inp, struct sctp_nets **netp,
1465 struct sockaddr *init_src, int *notification,
1466 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1467 uint8_t mflowtype, uint32_t mflowid,
1468 uint32_t vrf_id, uint16_t port);
1472 * handle a state cookie for an existing association m: input packet mbuf
1473 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1474 * "split" mbuf and the cookie signature does not exist offset: offset into
1475 * mbuf to the cookie-echo chunk
1477 static struct sctp_tcb *
1478 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1479 struct sockaddr *src, struct sockaddr *dst,
1480 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1481 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1482 struct sockaddr *init_src, int *notification,
1483 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1484 uint8_t mflowtype, uint32_t mflowid,
1485 uint32_t vrf_id, uint16_t port)
1487 struct sctp_association *asoc;
1488 struct sctp_init_chunk *init_cp, init_buf;
1489 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1490 struct sctp_nets *net;
1491 struct mbuf *op_err;
1493 int init_offset, initack_offset, i;
1497 #if defined(SCTP_DETAILED_STR_STATS)
1502 /* I know that the TCB is non-NULL from the caller */
1504 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1505 if (asoc->cookie_how[how_indx] == 0)
1508 if (how_indx < sizeof(asoc->cookie_how)) {
1509 asoc->cookie_how[how_indx] = 1;
1511 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1512 /* SHUTDOWN came in after sending INIT-ACK */
1513 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1514 op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
1515 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1516 mflowtype, mflowid, inp->fibnum,
1518 if (how_indx < sizeof(asoc->cookie_how))
1519 asoc->cookie_how[how_indx] = 2;
1523 * find and validate the INIT chunk in the cookie (peer's info) the
1524 * INIT should start after the cookie-echo header struct (chunk
1525 * header, state cookie header struct)
1527 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1529 init_cp = (struct sctp_init_chunk *)
1530 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1531 (uint8_t *)&init_buf);
1532 if (init_cp == NULL) {
1533 /* could not pull a INIT chunk in cookie */
1536 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1540 * find and validate the INIT-ACK chunk in the cookie (my info) the
1541 * INIT-ACK follows the INIT chunk
1543 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1544 initack_cp = (struct sctp_init_ack_chunk *)
1545 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1546 (uint8_t *)&initack_buf);
1547 if (initack_cp == NULL) {
1548 /* could not pull INIT-ACK chunk in cookie */
1551 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1554 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1555 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1557 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1558 * to get into the OPEN state
1560 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1562 * Opps, this means that we somehow generated two vtag's
1563 * the same. I.e. we did:
1565 * <---INIT(tag=a)------
1566 * ----INIT-ACK(tag=t)-->
1567 * ----INIT(tag=t)------> *1
1568 * <---INIT-ACK(tag=a)---
1569 * <----CE(tag=t)------------- *2
1571 * At point *1 we should be generating a different
1572 * tag t'. Which means we would throw away the CE and send
1573 * ours instead. Basically this is case C (throw away side).
1575 if (how_indx < sizeof(asoc->cookie_how))
1576 asoc->cookie_how[how_indx] = 17;
1580 switch (SCTP_GET_STATE(asoc)) {
1581 case SCTP_STATE_COOKIE_WAIT:
1582 case SCTP_STATE_COOKIE_ECHOED:
1584 * INIT was sent but got a COOKIE_ECHO with the
1585 * correct tags... just accept it...but we must
1586 * process the init so that we can make sure we have
1587 * the right seq no's.
1589 /* First we must process the INIT !! */
1590 retval = sctp_process_init(init_cp, stcb);
1592 if (how_indx < sizeof(asoc->cookie_how))
1593 asoc->cookie_how[how_indx] = 3;
1596 /* we have already processed the INIT so no problem */
1597 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp,
1599 SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1600 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp,
1602 SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1603 /* update current state */
1604 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1605 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1607 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1609 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1610 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1611 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1612 stcb->sctp_ep, stcb, asoc->primary_destination);
1614 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1615 sctp_stop_all_cookie_timers(stcb);
1616 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1617 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1618 (!SCTP_IS_LISTENING(inp))) {
1619 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1623 * Here is where collision would go if we
1624 * did a connect() and instead got a
1625 * init/init-ack/cookie done before the
1626 * init-ack came back..
1628 stcb->sctp_ep->sctp_flags |=
1629 SCTP_PCB_FLAGS_CONNECTED;
1630 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1631 so = SCTP_INP_SO(stcb->sctp_ep);
1632 atomic_add_int(&stcb->asoc.refcnt, 1);
1633 SCTP_TCB_UNLOCK(stcb);
1634 SCTP_SOCKET_LOCK(so, 1);
1635 SCTP_TCB_LOCK(stcb);
1636 atomic_add_int(&stcb->asoc.refcnt, -1);
1637 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1638 SCTP_SOCKET_UNLOCK(so, 1);
1642 soisconnected(stcb->sctp_socket);
1643 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1644 SCTP_SOCKET_UNLOCK(so, 1);
1647 /* notify upper layer */
1648 *notification = SCTP_NOTIFY_ASSOC_UP;
1650 * since we did not send a HB make sure we don't
1653 old.tv_sec = cookie->time_entered.tv_sec;
1654 old.tv_usec = cookie->time_entered.tv_usec;
1655 net->hb_responded = 1;
1656 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1658 SCTP_RTT_FROM_NON_DATA);
1660 if (stcb->asoc.sctp_autoclose_ticks &&
1661 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1662 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1668 * we're in the OPEN state (or beyond), so peer must
1669 * have simply lost the COOKIE-ACK
1673 sctp_stop_all_cookie_timers(stcb);
1675 * We ignore the return code here.. not sure if we should
1676 * somehow abort.. but we do have an existing asoc. This
1677 * really should not fail.
1679 if (sctp_load_addresses_from_init(stcb, m,
1680 init_offset + sizeof(struct sctp_init_chunk),
1681 initack_offset, src, dst, init_src, stcb->asoc.port)) {
1682 if (how_indx < sizeof(asoc->cookie_how))
1683 asoc->cookie_how[how_indx] = 4;
1686 /* respond with a COOKIE-ACK */
1687 sctp_toss_old_cookies(stcb, asoc);
1688 sctp_send_cookie_ack(stcb);
1689 if (how_indx < sizeof(asoc->cookie_how))
1690 asoc->cookie_how[how_indx] = 5;
1693 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1694 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1695 cookie->tie_tag_my_vtag == 0 &&
1696 cookie->tie_tag_peer_vtag == 0) {
1698 * case C in Section 5.2.4 Table 2: XMOO silently discard
1700 if (how_indx < sizeof(asoc->cookie_how))
1701 asoc->cookie_how[how_indx] = 6;
1705 * If nat support, and the below and stcb is established, send back
1706 * a ABORT(colliding state) if we are established.
1708 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1709 (asoc->peer_supports_nat) &&
1710 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1711 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1712 (asoc->peer_vtag == 0)))) {
1714 * Special case - Peer's support nat. We may have two init's
1715 * that we gave out the same tag on since one was not
1716 * established.. i.e. we get INIT from host-1 behind the nat
1717 * and we respond tag-a, we get a INIT from host-2 behind
1718 * the nat and we get tag-a again. Then we bring up host-1
1719 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1720 * Now we have colliding state. We must send an abort here
1721 * with colliding state indication.
1723 op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
1724 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1725 mflowtype, mflowid, inp->fibnum,
1729 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1730 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1731 (asoc->peer_vtag == 0))) {
1733 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1734 * should be ok, re-accept peer info
1736 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1738 * Extension of case C. If we hit this, then the
1739 * random number generator returned the same vtag
1740 * when we first sent our INIT-ACK and when we later
1741 * sent our INIT. The side with the seq numbers that
1742 * are different will be the one that normnally
1743 * would have hit case C. This in effect "extends"
1744 * our vtags in this collision case to be 64 bits.
1745 * The same collision could occur aka you get both
1746 * vtag and seq number the same twice in a row.. but
1747 * is much less likely. If it did happen then we
1748 * would proceed through and bring up the assoc.. we
1749 * may end up with the wrong stream setup however..
1750 * which would be bad.. but there is no way to
1751 * tell.. until we send on a stream that does not
1754 if (how_indx < sizeof(asoc->cookie_how))
1755 asoc->cookie_how[how_indx] = 7;
1759 if (how_indx < sizeof(asoc->cookie_how))
1760 asoc->cookie_how[how_indx] = 8;
1761 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1762 SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1763 sctp_stop_all_cookie_timers(stcb);
1765 * since we did not send a HB make sure we don't double
1768 net->hb_responded = 1;
1769 if (stcb->asoc.sctp_autoclose_ticks &&
1770 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1771 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1774 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1775 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1777 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1779 * Ok the peer probably discarded our data (if we
1780 * echoed a cookie+data). So anything on the
1781 * sent_queue should be marked for retransmit, we
1782 * may not get something to kick us so it COULD
1783 * still take a timeout to move these.. but it can't
1784 * hurt to mark them.
1786 struct sctp_tmit_chunk *chk;
1788 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1789 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1790 chk->sent = SCTP_DATAGRAM_RESEND;
1791 sctp_flight_size_decrease(chk);
1792 sctp_total_flight_decrease(stcb, chk);
1793 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1799 /* process the INIT info (peer's info) */
1800 retval = sctp_process_init(init_cp, stcb);
1802 if (how_indx < sizeof(asoc->cookie_how))
1803 asoc->cookie_how[how_indx] = 9;
1806 if (sctp_load_addresses_from_init(stcb, m,
1807 init_offset + sizeof(struct sctp_init_chunk),
1808 initack_offset, src, dst, init_src, stcb->asoc.port)) {
1809 if (how_indx < sizeof(asoc->cookie_how))
1810 asoc->cookie_how[how_indx] = 10;
1813 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1814 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1815 *notification = SCTP_NOTIFY_ASSOC_UP;
1817 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1818 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1819 (!SCTP_IS_LISTENING(inp))) {
1820 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1823 stcb->sctp_ep->sctp_flags |=
1824 SCTP_PCB_FLAGS_CONNECTED;
1825 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1826 so = SCTP_INP_SO(stcb->sctp_ep);
1827 atomic_add_int(&stcb->asoc.refcnt, 1);
1828 SCTP_TCB_UNLOCK(stcb);
1829 SCTP_SOCKET_LOCK(so, 1);
1830 SCTP_TCB_LOCK(stcb);
1831 atomic_add_int(&stcb->asoc.refcnt, -1);
1832 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1833 SCTP_SOCKET_UNLOCK(so, 1);
1837 soisconnected(stcb->sctp_socket);
1838 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1839 SCTP_SOCKET_UNLOCK(so, 1);
1842 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1843 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1845 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1846 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1847 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1848 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1850 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1852 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1853 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1854 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1855 stcb->sctp_ep, stcb, asoc->primary_destination);
1857 sctp_stop_all_cookie_timers(stcb);
1858 sctp_toss_old_cookies(stcb, asoc);
1859 sctp_send_cookie_ack(stcb);
1862 * only if we have retrans set do we do this. What
1863 * this call does is get only the COOKIE-ACK out and
1864 * then when we return the normal call to
1865 * sctp_chunk_output will get the retrans out behind
1868 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1870 if (how_indx < sizeof(asoc->cookie_how))
1871 asoc->cookie_how[how_indx] = 11;
1875 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1876 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1877 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1878 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1879 cookie->tie_tag_peer_vtag != 0) {
1880 struct sctpasochead *head;
1881 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1885 if (asoc->peer_supports_nat) {
1887 * This is a gross gross hack. Just call the
1888 * cookie_new code since we are allowing a duplicate
1889 * association. I hope this works...
1891 return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
1892 sh, cookie, cookie_len,
1893 inp, netp, init_src, notification,
1894 auth_skipped, auth_offset, auth_len,
1899 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1902 if (how_indx < sizeof(asoc->cookie_how))
1903 asoc->cookie_how[how_indx] = 12;
1904 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
1905 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1906 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1907 SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
1909 /* notify upper layer */
1910 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1911 atomic_add_int(&stcb->asoc.refcnt, 1);
1912 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1913 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1914 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1915 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1917 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1918 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1919 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1920 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1922 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1923 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1924 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1925 stcb->sctp_ep, stcb, asoc->primary_destination);
1927 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1928 /* move to OPEN state, if not in SHUTDOWN_SENT */
1929 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1931 asoc->pre_open_streams =
1932 ntohs(initack_cp->init.num_outbound_streams);
1933 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1934 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1935 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1937 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1939 asoc->str_reset_seq_in = asoc->init_seq_number;
1941 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1942 if (asoc->mapping_array) {
1943 memset(asoc->mapping_array, 0,
1944 asoc->mapping_array_size);
1946 if (asoc->nr_mapping_array) {
1947 memset(asoc->nr_mapping_array, 0,
1948 asoc->mapping_array_size);
1950 SCTP_TCB_UNLOCK(stcb);
1951 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1952 so = SCTP_INP_SO(stcb->sctp_ep);
1953 SCTP_SOCKET_LOCK(so, 1);
1955 SCTP_INP_INFO_WLOCK();
1956 SCTP_INP_WLOCK(stcb->sctp_ep);
1957 SCTP_TCB_LOCK(stcb);
1958 atomic_add_int(&stcb->asoc.refcnt, -1);
1959 /* send up all the data */
1960 SCTP_TCB_SEND_LOCK(stcb);
1962 sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_LOCKED);
1963 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1964 stcb->asoc.strmout[i].chunks_on_queues = 0;
1965 #if defined(SCTP_DETAILED_STR_STATS)
1966 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1967 asoc->strmout[i].abandoned_sent[j] = 0;
1968 asoc->strmout[i].abandoned_unsent[j] = 0;
1971 asoc->strmout[i].abandoned_sent[0] = 0;
1972 asoc->strmout[i].abandoned_unsent[0] = 0;
1974 stcb->asoc.strmout[i].sid = i;
1975 stcb->asoc.strmout[i].next_mid_ordered = 0;
1976 stcb->asoc.strmout[i].next_mid_unordered = 0;
1977 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1979 /* process the INIT-ACK info (my info) */
1980 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1981 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1983 /* pull from vtag hash */
1984 LIST_REMOVE(stcb, sctp_asocs);
1985 /* re-insert to new vtag position */
1986 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1987 SCTP_BASE_INFO(hashasocmark))];
1989 * put it in the bucket in the vtag hash of assoc's for the
1992 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1994 SCTP_TCB_SEND_UNLOCK(stcb);
1995 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1996 SCTP_INP_INFO_WUNLOCK();
1997 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1998 SCTP_SOCKET_UNLOCK(so, 1);
2000 asoc->total_flight = 0;
2001 asoc->total_flight_count = 0;
2002 /* process the INIT info (peer's info) */
2003 retval = sctp_process_init(init_cp, stcb);
2005 if (how_indx < sizeof(asoc->cookie_how))
2006 asoc->cookie_how[how_indx] = 13;
2011 * since we did not send a HB make sure we don't double
2014 net->hb_responded = 1;
2016 if (sctp_load_addresses_from_init(stcb, m,
2017 init_offset + sizeof(struct sctp_init_chunk),
2018 initack_offset, src, dst, init_src, stcb->asoc.port)) {
2019 if (how_indx < sizeof(asoc->cookie_how))
2020 asoc->cookie_how[how_indx] = 14;
2024 /* respond with a COOKIE-ACK */
2025 sctp_stop_all_cookie_timers(stcb);
2026 sctp_toss_old_cookies(stcb, asoc);
2027 sctp_send_cookie_ack(stcb);
2028 if (how_indx < sizeof(asoc->cookie_how))
2029 asoc->cookie_how[how_indx] = 15;
2033 if (how_indx < sizeof(asoc->cookie_how))
2034 asoc->cookie_how[how_indx] = 16;
2035 /* all other cases... */
2041 * handle a state cookie for a new association m: input packet mbuf chain--
2042 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
2043 * and the cookie signature does not exist offset: offset into mbuf to the
2044 * cookie-echo chunk length: length of the cookie chunk to: where the init
2045 * was from returns a new TCB
2047 static struct sctp_tcb *
2048 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
2049 struct sockaddr *src, struct sockaddr *dst,
2050 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
2051 struct sctp_inpcb *inp, struct sctp_nets **netp,
2052 struct sockaddr *init_src, int *notification,
2053 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2054 uint8_t mflowtype, uint32_t mflowid,
2055 uint32_t vrf_id, uint16_t port)
2057 struct sctp_tcb *stcb;
2058 struct sctp_init_chunk *init_cp, init_buf;
2059 struct sctp_init_ack_chunk *initack_cp, initack_buf;
2060 union sctp_sockstore store;
2061 struct sctp_association *asoc;
2062 int init_offset, initack_offset, initack_limit;
2065 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
2066 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2069 so = SCTP_INP_SO(inp);
2073 * find and validate the INIT chunk in the cookie (peer's info) the
2074 * INIT should start after the cookie-echo header struct (chunk
2075 * header, state cookie header struct)
2077 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2078 init_cp = (struct sctp_init_chunk *)
2079 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2080 (uint8_t *)&init_buf);
2081 if (init_cp == NULL) {
2082 /* could not pull a INIT chunk in cookie */
2083 SCTPDBG(SCTP_DEBUG_INPUT1,
2084 "process_cookie_new: could not pull INIT chunk hdr\n");
2087 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2088 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2091 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2093 * find and validate the INIT-ACK chunk in the cookie (my info) the
2094 * INIT-ACK follows the INIT chunk
2096 initack_cp = (struct sctp_init_ack_chunk *)
2097 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2098 (uint8_t *)&initack_buf);
2099 if (initack_cp == NULL) {
2100 /* could not pull INIT-ACK chunk in cookie */
2101 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2104 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2108 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2109 * "initack_limit" value. This is because the chk_length field
2110 * includes the length of the cookie, but the cookie is omitted when
2111 * the INIT and INIT_ACK are tacked onto the cookie...
2113 initack_limit = offset + cookie_len;
2116 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2121 * Here we do a trick, we set in NULL for the proc/thread argument.
2122 * We do this since in effect we only use the p argument when the
2123 * socket is unbound and we must do an implicit bind. Since we are
2124 * getting a cookie, we cannot be unbound.
2126 stcb = sctp_aloc_assoc(inp, init_src, &error,
2127 ntohl(initack_cp->init.initiate_tag), vrf_id,
2128 ntohs(initack_cp->init.num_outbound_streams),
2130 (struct thread *)NULL
2133 struct mbuf *op_err;
2135 /* memory problem? */
2136 SCTPDBG(SCTP_DEBUG_INPUT1,
2137 "process_cookie_new: no room for another TCB!\n");
2138 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2139 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2140 src, dst, sh, op_err,
2145 /* get the correct sctp_nets */
2147 *netp = sctp_findnet(stcb, init_src);
2150 /* get scope variables out of cookie */
2151 asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2152 asoc->scope.site_scope = cookie->site_scope;
2153 asoc->scope.local_scope = cookie->local_scope;
2154 asoc->scope.loopback_scope = cookie->loopback_scope;
2156 if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2157 (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2158 struct mbuf *op_err;
2161 * Houston we have a problem. The EP changed while the
2162 * cookie was in flight. Only recourse is to abort the
2165 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2166 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2167 src, dst, sh, op_err,
2170 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2171 atomic_add_int(&stcb->asoc.refcnt, 1);
2172 SCTP_TCB_UNLOCK(stcb);
2173 SCTP_SOCKET_LOCK(so, 1);
2174 SCTP_TCB_LOCK(stcb);
2175 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2177 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2178 SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2179 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2180 SCTP_SOCKET_UNLOCK(so, 1);
2184 /* process the INIT-ACK info (my info) */
2185 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2186 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2187 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2188 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2189 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2190 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2191 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2192 asoc->str_reset_seq_in = asoc->init_seq_number;
2194 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2196 /* process the INIT info (peer's info) */
2198 retval = sctp_process_init(init_cp, stcb);
2202 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2203 atomic_add_int(&stcb->asoc.refcnt, 1);
2204 SCTP_TCB_UNLOCK(stcb);
2205 SCTP_SOCKET_LOCK(so, 1);
2206 SCTP_TCB_LOCK(stcb);
2207 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2209 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2210 SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2211 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2212 SCTP_SOCKET_UNLOCK(so, 1);
2216 /* load all addresses */
2217 if (sctp_load_addresses_from_init(stcb, m,
2218 init_offset + sizeof(struct sctp_init_chunk), initack_offset,
2219 src, dst, init_src, port)) {
2220 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2221 atomic_add_int(&stcb->asoc.refcnt, 1);
2222 SCTP_TCB_UNLOCK(stcb);
2223 SCTP_SOCKET_LOCK(so, 1);
2224 SCTP_TCB_LOCK(stcb);
2225 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2227 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2228 SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2229 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2230 SCTP_SOCKET_UNLOCK(so, 1);
2235 * verify any preceding AUTH chunk that was skipped
2237 /* pull the local authentication parameters from the cookie/init-ack */
2238 sctp_auth_get_cookie_params(stcb, m,
2239 initack_offset + sizeof(struct sctp_init_ack_chunk),
2240 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2242 struct sctp_auth_chunk *auth;
2244 auth = (struct sctp_auth_chunk *)
2245 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2246 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2247 /* auth HMAC failed, dump the assoc and packet */
2248 SCTPDBG(SCTP_DEBUG_AUTH1,
2249 "COOKIE-ECHO: AUTH failed\n");
2250 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2251 atomic_add_int(&stcb->asoc.refcnt, 1);
2252 SCTP_TCB_UNLOCK(stcb);
2253 SCTP_SOCKET_LOCK(so, 1);
2254 SCTP_TCB_LOCK(stcb);
2255 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2257 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2258 SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2259 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2260 SCTP_SOCKET_UNLOCK(so, 1);
2264 /* remaining chunks checked... good to go */
2265 stcb->asoc.authenticated = 1;
2269 * if we're doing ASCONFs, check to see if we have any new local
2270 * addresses that need to get added to the peer (eg. addresses
2271 * changed while cookie echo in flight). This needs to be done
2272 * after we go to the OPEN state to do the correct asconf
2273 * processing. else, make sure we have the correct addresses in our
2277 /* warning, we re-use sin, sin6, sa_store here! */
2278 /* pull in local_address (our "from" address) */
2279 switch (cookie->laddr_type) {
2281 case SCTP_IPV4_ADDRESS:
2282 /* source addr is IPv4 */
2283 memset(&store.sin, 0, sizeof(struct sockaddr_in));
2284 store.sin.sin_family = AF_INET;
2285 store.sin.sin_len = sizeof(struct sockaddr_in);
2286 store.sin.sin_addr.s_addr = cookie->laddress[0];
2290 case SCTP_IPV6_ADDRESS:
2291 /* source addr is IPv6 */
2292 memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
2293 store.sin6.sin6_family = AF_INET6;
2294 store.sin6.sin6_len = sizeof(struct sockaddr_in6);
2295 store.sin6.sin6_scope_id = cookie->scope_id;
2296 memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2300 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2301 atomic_add_int(&stcb->asoc.refcnt, 1);
2302 SCTP_TCB_UNLOCK(stcb);
2303 SCTP_SOCKET_LOCK(so, 1);
2304 SCTP_TCB_LOCK(stcb);
2305 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2307 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2308 SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2309 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2310 SCTP_SOCKET_UNLOCK(so, 1);
2315 /* update current state */
2316 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2317 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2318 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2319 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2320 stcb->sctp_ep, stcb, asoc->primary_destination);
2322 sctp_stop_all_cookie_timers(stcb);
2323 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2324 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2326 /* set up to notify upper layer */
2327 *notification = SCTP_NOTIFY_ASSOC_UP;
2328 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2329 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2330 (!SCTP_IS_LISTENING(inp))) {
2332 * This is an endpoint that called connect() how it got a
2333 * cookie that is NEW is a bit of a mystery. It must be that
2334 * the INIT was sent, but before it got there.. a complete
2335 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2336 * should have went to the other code.. not here.. oh well..
2337 * a bit of protection is worth having..
2339 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2340 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2341 atomic_add_int(&stcb->asoc.refcnt, 1);
2342 SCTP_TCB_UNLOCK(stcb);
2343 SCTP_SOCKET_LOCK(so, 1);
2344 SCTP_TCB_LOCK(stcb);
2345 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2346 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2347 SCTP_SOCKET_UNLOCK(so, 1);
2351 soisconnected(stcb->sctp_socket);
2352 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2353 SCTP_SOCKET_UNLOCK(so, 1);
2355 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2356 (SCTP_IS_LISTENING(inp))) {
2358 * We don't want to do anything with this one. Since it is
2359 * the listening guy. The timer will get started for
2360 * accepted connections in the caller.
2364 /* since we did not send a HB make sure we don't double things */
2365 if ((netp) && (*netp))
2366 (*netp)->hb_responded = 1;
2368 if (stcb->asoc.sctp_autoclose_ticks &&
2369 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2370 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2372 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2373 if ((netp != NULL) && (*netp != NULL)) {
2376 /* calculate the RTT and set the encaps port */
2377 old.tv_sec = cookie->time_entered.tv_sec;
2378 old.tv_usec = cookie->time_entered.tv_usec;
2379 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2380 &old, SCTP_RTT_FROM_NON_DATA);
2382 /* respond with a COOKIE-ACK */
2383 sctp_send_cookie_ack(stcb);
2386 * check the address lists for any ASCONFs that need to be sent
2387 * AFTER the cookie-ack is sent
2389 sctp_check_address_list(stcb, m,
2390 initack_offset + sizeof(struct sctp_init_ack_chunk),
2391 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2392 &store.sa, cookie->local_scope, cookie->site_scope,
2393 cookie->ipv4_scope, cookie->loopback_scope);
2400 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2401 * we NEED to make sure we are not already using the vtag. If so we
2402 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2403 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2404 SCTP_BASE_INFO(hashasocmark))];
2405 LIST_FOREACH(stcb, head, sctp_asocs) {
2406 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2407 -- SEND ABORT - TRY AGAIN --
2413 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2414 * existing (non-NULL) TCB
2416 static struct mbuf *
2417 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2418 struct sockaddr *src, struct sockaddr *dst,
2419 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2420 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2421 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2422 struct sctp_tcb **locked_tcb,
2423 uint8_t mflowtype, uint32_t mflowid,
2424 uint32_t vrf_id, uint16_t port)
2426 struct sctp_state_cookie *cookie;
2427 struct sctp_tcb *l_stcb = *stcb;
2428 struct sctp_inpcb *l_inp;
2429 struct sockaddr *to;
2430 struct sctp_pcb *ep;
2432 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2434 uint8_t cookie_ok = 0;
2435 unsigned int sig_offset, cookie_offset;
2436 unsigned int cookie_len;
2438 struct timeval time_expires;
2439 int notification = 0;
2440 struct sctp_nets *netl;
2441 int had_a_existing_tcb = 0;
2442 int send_int_conf = 0;
2444 struct sockaddr_in sin;
2447 struct sockaddr_in6 sin6;
2450 SCTPDBG(SCTP_DEBUG_INPUT2,
2451 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2453 if (inp_p == NULL) {
2456 cookie = &cp->cookie;
2457 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2458 cookie_len = ntohs(cp->ch.chunk_length);
2460 if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2461 sizeof(struct sctp_init_chunk) +
2462 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2463 /* cookie too small */
2466 if ((cookie->peerport != sh->src_port) ||
2467 (cookie->myport != sh->dest_port) ||
2468 (cookie->my_vtag != sh->v_tag)) {
2470 * invalid ports or bad tag. Note that we always leave the
2471 * v_tag in the header in network order and when we stored
2472 * it in the my_vtag slot we also left it in network order.
2473 * This maintains the match even though it may be in the
2474 * opposite byte order of the machine :->
2479 * split off the signature into its own mbuf (since it should not be
2480 * calculated in the sctp_hmac_m() call).
2482 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2483 m_sig = m_split(m, sig_offset, M_NOWAIT);
2484 if (m_sig == NULL) {
2485 /* out of memory or ?? */
2488 #ifdef SCTP_MBUF_LOGGING
2489 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2490 sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
2495 * compute the signature/digest for the cookie
2497 ep = &(*inp_p)->sctp_ep;
2500 SCTP_TCB_UNLOCK(l_stcb);
2502 SCTP_INP_RLOCK(l_inp);
2504 SCTP_TCB_LOCK(l_stcb);
2506 /* which cookie is it? */
2507 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2508 (ep->current_secret_number != ep->last_secret_number)) {
2509 /* it's the old cookie */
2510 (void)sctp_hmac_m(SCTP_HMAC,
2511 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2512 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2514 /* it's the current cookie */
2515 (void)sctp_hmac_m(SCTP_HMAC,
2516 (uint8_t *)ep->secret_key[(int)ep->current_secret_number],
2517 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2519 /* get the signature */
2520 SCTP_INP_RUNLOCK(l_inp);
2521 sig = (uint8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *)&tmp_sig);
2523 /* couldn't find signature */
2524 sctp_m_freem(m_sig);
2527 /* compare the received digest with the computed digest */
2528 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2529 /* try the old cookie? */
2530 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2531 (ep->current_secret_number != ep->last_secret_number)) {
2532 /* compute digest with old */
2533 (void)sctp_hmac_m(SCTP_HMAC,
2534 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2535 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2537 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2545 * Now before we continue we must reconstruct our mbuf so that
2546 * normal processing of any other chunks will work.
2552 while (SCTP_BUF_NEXT(m_at) != NULL) {
2553 m_at = SCTP_BUF_NEXT(m_at);
2555 SCTP_BUF_NEXT(m_at) = m_sig;
2558 if (cookie_ok == 0) {
2559 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2560 SCTPDBG(SCTP_DEBUG_INPUT2,
2561 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2562 (uint32_t)offset, cookie_offset, sig_offset);
2566 * check the cookie timestamps to be sure it's not stale
2568 (void)SCTP_GETTIME_TIMEVAL(&now);
2569 /* Expire time is in Ticks, so we convert to seconds */
2570 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2571 time_expires.tv_usec = cookie->time_entered.tv_usec;
2572 if (timevalcmp(&now, &time_expires, >)) {
2573 /* cookie is stale! */
2574 struct mbuf *op_err;
2575 struct sctp_error_stale_cookie *cause;
2576 struct timeval diff;
2579 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2580 0, M_NOWAIT, 1, MT_DATA);
2581 if (op_err == NULL) {
2586 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
2587 cause = mtod(op_err, struct sctp_error_stale_cookie *);
2588 cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
2589 cause->cause.length = htons((sizeof(struct sctp_paramhdr) +
2590 (sizeof(uint32_t))));
2592 timevalsub(&diff, &time_expires);
2593 if (diff.tv_sec > UINT32_MAX / 1000000) {
2594 staleness = UINT32_MAX;
2596 staleness = diff.tv_sec * 1000000;
2598 if (UINT32_MAX - staleness >= (uint32_t)diff.tv_usec) {
2599 staleness += diff.tv_usec;
2601 staleness = UINT32_MAX;
2603 cause->stale_time = htonl(staleness);
2604 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2605 mflowtype, mflowid, l_inp->fibnum,
2610 * Now we must see with the lookup address if we have an existing
2611 * asoc. This will only happen if we were in the COOKIE-WAIT state
2612 * and a INIT collided with us and somewhere the peer sent the
2613 * cookie on another address besides the single address our assoc
2614 * had for him. In this case we will have one of the tie-tags set at
2615 * least AND the address field in the cookie can be used to look it
2619 switch (cookie->addr_type) {
2621 case SCTP_IPV6_ADDRESS:
2622 memset(&sin6, 0, sizeof(sin6));
2623 sin6.sin6_family = AF_INET6;
2624 sin6.sin6_len = sizeof(sin6);
2625 sin6.sin6_port = sh->src_port;
2626 sin6.sin6_scope_id = cookie->scope_id;
2627 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2628 sizeof(sin6.sin6_addr.s6_addr));
2629 to = (struct sockaddr *)&sin6;
2633 case SCTP_IPV4_ADDRESS:
2634 memset(&sin, 0, sizeof(sin));
2635 sin.sin_family = AF_INET;
2636 sin.sin_len = sizeof(sin);
2637 sin.sin_port = sh->src_port;
2638 sin.sin_addr.s_addr = cookie->address[0];
2639 to = (struct sockaddr *)&sin;
2643 /* This should not happen */
2646 if (*stcb == NULL) {
2647 /* Yep, lets check */
2648 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2649 if (*stcb == NULL) {
2651 * We should have only got back the same inp. If we
2652 * got back a different ep we have a problem. The
2653 * original findep got back l_inp and now
2655 if (l_inp != *inp_p) {
2656 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2659 if (*locked_tcb == NULL) {
2661 * In this case we found the assoc only
2662 * after we locked the create lock. This
2663 * means we are in a colliding case and we
2664 * must make sure that we unlock the tcb if
2665 * its one of the cases where we throw away
2666 * the incoming packets.
2668 *locked_tcb = *stcb;
2671 * We must also increment the inp ref count
2672 * since the ref_count flags was set when we
2673 * did not find the TCB, now we found it
2674 * which reduces the refcount.. we must
2675 * raise it back out to balance it all :-)
2677 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2678 if ((*stcb)->sctp_ep != l_inp) {
2679 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2680 (void *)(*stcb)->sctp_ep, (void *)l_inp);
2685 cookie_len -= SCTP_SIGNATURE_SIZE;
2686 if (*stcb == NULL) {
2687 /* this is the "normal" case... get a new TCB */
2688 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2689 cookie, cookie_len, *inp_p,
2690 netp, to, ¬ification,
2691 auth_skipped, auth_offset, auth_len,
2695 /* this is abnormal... cookie-echo on existing TCB */
2696 had_a_existing_tcb = 1;
2697 *stcb = sctp_process_cookie_existing(m, iphlen, offset,
2699 cookie, cookie_len, *inp_p, *stcb, netp, to,
2700 ¬ification, auth_skipped, auth_offset, auth_len,
2705 if (*stcb == NULL) {
2706 /* still no TCB... must be bad cookie-echo */
2709 if (*netp != NULL) {
2710 (*netp)->flowtype = mflowtype;
2711 (*netp)->flowid = mflowid;
2714 * Ok, we built an association so confirm the address we sent the
2717 netl = sctp_findnet(*stcb, to);
2719 * This code should in theory NOT run but
2722 /* TSNH! Huh, why do I need to add this address here? */
2723 if (sctp_add_remote_addr(*stcb, to, NULL, port,
2724 SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2727 netl = sctp_findnet(*stcb, to);
2730 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2731 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2732 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2737 sctp_start_net_timers(*stcb);
2738 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2739 if (!had_a_existing_tcb ||
2740 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2742 * If we have a NEW cookie or the connect never
2743 * reached the connected state during collision we
2744 * must do the TCP accept thing.
2746 struct socket *so, *oso;
2747 struct sctp_inpcb *inp;
2749 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2751 * For a restart we will keep the same
2752 * socket, no need to do anything. I THINK!!
2754 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2755 if (send_int_conf) {
2756 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2757 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2761 oso = (*inp_p)->sctp_socket;
2762 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2763 SCTP_TCB_UNLOCK((*stcb));
2764 CURVNET_SET(oso->so_vnet);
2765 so = sonewconn(oso, 0
2768 SCTP_TCB_LOCK((*stcb));
2769 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2772 struct mbuf *op_err;
2773 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2774 struct socket *pcb_so;
2776 /* Too many sockets */
2777 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2778 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2779 sctp_abort_association(*inp_p, NULL, m, iphlen,
2780 src, dst, sh, op_err,
2783 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2784 pcb_so = SCTP_INP_SO(*inp_p);
2785 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2786 SCTP_TCB_UNLOCK((*stcb));
2787 SCTP_SOCKET_LOCK(pcb_so, 1);
2788 SCTP_TCB_LOCK((*stcb));
2789 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2791 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2792 SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2793 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2794 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2798 inp = (struct sctp_inpcb *)so->so_pcb;
2799 SCTP_INP_INCR_REF(inp);
2801 * We add the unbound flag here so that if we get an
2802 * soabort() before we get the move_pcb done, we
2803 * will properly cleanup.
2805 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2806 SCTP_PCB_FLAGS_CONNECTED |
2807 SCTP_PCB_FLAGS_IN_TCPPOOL |
2808 SCTP_PCB_FLAGS_UNBOUND |
2809 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2810 SCTP_PCB_FLAGS_DONT_WAKE);
2811 inp->sctp_features = (*inp_p)->sctp_features;
2812 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2813 inp->sctp_socket = so;
2814 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2815 inp->max_cwnd = (*inp_p)->max_cwnd;
2816 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2817 inp->ecn_supported = (*inp_p)->ecn_supported;
2818 inp->prsctp_supported = (*inp_p)->prsctp_supported;
2819 inp->auth_supported = (*inp_p)->auth_supported;
2820 inp->asconf_supported = (*inp_p)->asconf_supported;
2821 inp->reconfig_supported = (*inp_p)->reconfig_supported;
2822 inp->nrsack_supported = (*inp_p)->nrsack_supported;
2823 inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2824 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2825 inp->sctp_context = (*inp_p)->sctp_context;
2826 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2827 inp->fibnum = (*inp_p)->fibnum;
2828 inp->inp_starting_point_for_iterator = NULL;
2830 * copy in the authentication parameters from the
2833 if (inp->sctp_ep.local_hmacs)
2834 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2835 inp->sctp_ep.local_hmacs =
2836 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2837 if (inp->sctp_ep.local_auth_chunks)
2838 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2839 inp->sctp_ep.local_auth_chunks =
2840 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2843 * Now we must move it from one hash table to
2844 * another and get the tcb in the right place.
2848 * This is where the one-2-one socket is put into
2849 * the accept state waiting for the accept!
2852 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2854 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2856 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2857 SCTP_TCB_UNLOCK((*stcb));
2859 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2861 SCTP_TCB_LOCK((*stcb));
2862 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2866 * now we must check to see if we were aborted while
2867 * the move was going on and the lock/unlock
2870 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2872 * yep it was, we leave the assoc attached
2873 * to the socket since the sctp_inpcb_free()
2874 * call will send an abort for us.
2876 SCTP_INP_DECR_REF(inp);
2879 SCTP_INP_DECR_REF(inp);
2880 /* Switch over to the new guy */
2882 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2883 if (send_int_conf) {
2884 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2885 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2888 * Pull it from the incomplete queue and wake the
2891 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2892 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2893 SCTP_TCB_UNLOCK((*stcb));
2894 SCTP_SOCKET_LOCK(so, 1);
2897 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2898 SCTP_TCB_LOCK((*stcb));
2899 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2900 SCTP_SOCKET_UNLOCK(so, 1);
2906 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2908 if (send_int_conf) {
2909 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2910 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2916 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2917 struct sctp_tcb *stcb, struct sctp_nets *net)
2919 /* cp must not be used, others call this without a c-ack :-) */
2920 struct sctp_association *asoc;
2922 SCTPDBG(SCTP_DEBUG_INPUT2,
2923 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2924 if ((stcb == NULL) || (net == NULL)) {
2929 sctp_stop_all_cookie_timers(stcb);
2930 /* process according to association state */
2931 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2932 /* state change only needed when I am in right state */
2933 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2934 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2935 sctp_start_net_timers(stcb);
2936 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2937 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2938 stcb->sctp_ep, stcb, asoc->primary_destination);
2942 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2943 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2944 if (asoc->overall_error_count == 0) {
2945 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2946 &asoc->time_entered,
2947 SCTP_RTT_FROM_NON_DATA);
2949 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2950 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2951 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2952 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2953 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2957 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2958 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2959 so = SCTP_INP_SO(stcb->sctp_ep);
2960 atomic_add_int(&stcb->asoc.refcnt, 1);
2961 SCTP_TCB_UNLOCK(stcb);
2962 SCTP_SOCKET_LOCK(so, 1);
2963 SCTP_TCB_LOCK(stcb);
2964 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2966 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2967 soisconnected(stcb->sctp_socket);
2969 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2970 SCTP_SOCKET_UNLOCK(so, 1);
2974 * since we did not send a HB make sure we don't double
2977 net->hb_responded = 1;
2979 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2981 * We don't need to do the asconf thing, nor hb or
2982 * autoclose if the socket is closed.
2986 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2990 if (stcb->asoc.sctp_autoclose_ticks &&
2991 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2992 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2993 stcb->sctp_ep, stcb, NULL);
2996 * send ASCONF if parameters are pending and ASCONFs are
2997 * allowed (eg. addresses changed when init/cookie echo were
3000 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
3001 (stcb->asoc.asconf_supported == 1) &&
3002 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
3003 #ifdef SCTP_TIMER_BASED_ASCONF
3004 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
3005 stcb->sctp_ep, stcb,
3006 stcb->asoc.primary_destination);
3008 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
3009 SCTP_ADDR_NOT_LOCKED);
3014 /* Toss the cookie if I can */
3015 sctp_toss_old_cookies(stcb, asoc);
3016 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3017 /* Restart the timer if we have pending data */
3018 struct sctp_tmit_chunk *chk;
3020 chk = TAILQ_FIRST(&asoc->sent_queue);
3021 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
3026 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
3027 struct sctp_tcb *stcb)
3029 struct sctp_nets *net;
3030 struct sctp_tmit_chunk *lchk;
3031 struct sctp_ecne_chunk bkup;
3032 uint8_t override_bit;
3033 uint32_t tsn, window_data_tsn;
3035 unsigned int pkt_cnt;
3037 len = ntohs(cp->ch.chunk_length);
3038 if ((len != sizeof(struct sctp_ecne_chunk)) &&
3039 (len != sizeof(struct old_sctp_ecne_chunk))) {
3042 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3043 /* Its the old format */
3044 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3045 bkup.num_pkts_since_cwr = htonl(1);
3048 SCTP_STAT_INCR(sctps_recvecne);
3049 tsn = ntohl(cp->tsn);
3050 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3051 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3053 window_data_tsn = stcb->asoc.sending_seq - 1;
3055 window_data_tsn = lchk->rec.data.tsn;
3058 /* Find where it was sent to if possible. */
3060 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3061 if (lchk->rec.data.tsn == tsn) {
3063 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3066 if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) {
3072 * What to do. A previous send of a CWR was possibly lost.
3073 * See how old it is, we may have it marked on the actual
3076 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3077 if (tsn == net->last_cwr_tsn) {
3078 /* Found him, send it off */
3084 * If we reach here, we need to send a special CWR
3085 * that says hey, we did this a long time ago and
3086 * you lost the response.
3088 net = TAILQ_FIRST(&stcb->asoc.nets);
3093 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3100 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3101 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3103 * JRS - Use the congestion control given in the pluggable
3106 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3108 * We reduce once every RTT. So we will only lower cwnd at
3109 * the next sending seq i.e. the window_data_tsn
3111 net->cwr_window_tsn = window_data_tsn;
3112 net->ecn_ce_pkt_cnt += pkt_cnt;
3113 net->lost_cnt = pkt_cnt;
3114 net->last_cwr_tsn = tsn;
3116 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3117 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3118 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3120 * Another loss in the same window update how many
3121 * marks/packets lost we have had.
3125 if (pkt_cnt > net->lost_cnt) {
3126 /* Should be the case */
3127 cnt = (pkt_cnt - net->lost_cnt);
3128 net->ecn_ce_pkt_cnt += cnt;
3130 net->lost_cnt = pkt_cnt;
3131 net->last_cwr_tsn = tsn;
3133 * Most CC functions will ignore this call, since we
3134 * are in-window yet of the initial CE the peer saw.
3136 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3140 * We always send a CWR this way if our previous one was lost our
3141 * peer will get an update, or if it is not time again to reduce we
3142 * still get the cwr to the peer. Note we set the override when we
3143 * could not find the TSN on the chunk or the destination network.
3145 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3149 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3152 * Here we get a CWR from the peer. We must look in the outqueue and
3153 * make sure that we have a covered ECNE in the control chunk part.
3156 struct sctp_tmit_chunk *chk, *nchk;
3157 struct sctp_ecne_chunk *ecne;
3161 cwr_tsn = ntohl(cp->tsn);
3162 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3163 TAILQ_FOREACH_SAFE(chk, &stcb->asoc.control_send_queue, sctp_next, nchk) {
3164 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3167 if ((override == 0) && (chk->whoTo != net)) {
3168 /* Must be from the right src unless override is set */
3171 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3172 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3173 /* this covers this ECNE, we can remove it */
3174 stcb->asoc.ecn_echo_cnt_onq--;
3175 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3177 sctp_m_freem(chk->data);
3179 stcb->asoc.ctrl_queue_cnt--;
3180 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3181 if (override == 0) {
3189 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3190 struct sctp_tcb *stcb, struct sctp_nets *net)
3192 struct sctp_association *asoc;
3193 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3197 SCTPDBG(SCTP_DEBUG_INPUT2,
3198 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3203 /* process according to association state */
3204 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3205 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3206 SCTPDBG(SCTP_DEBUG_INPUT2,
3207 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3208 SCTP_TCB_UNLOCK(stcb);
3211 /* notify upper layer protocol */
3212 if (stcb->sctp_socket) {
3213 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3216 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3217 !TAILQ_EMPTY(&asoc->sent_queue) ||
3218 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
3219 panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3222 /* stop the timer */
3223 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3224 SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3225 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3227 SCTPDBG(SCTP_DEBUG_INPUT2,
3228 "sctp_handle_shutdown_complete: calls free-asoc\n");
3229 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3230 so = SCTP_INP_SO(stcb->sctp_ep);
3231 atomic_add_int(&stcb->asoc.refcnt, 1);
3232 SCTP_TCB_UNLOCK(stcb);
3233 SCTP_SOCKET_LOCK(so, 1);
3234 SCTP_TCB_LOCK(stcb);
3235 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3237 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3238 SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3239 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3240 SCTP_SOCKET_UNLOCK(so, 1);
3246 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3247 struct sctp_nets *net, uint8_t flg)
3249 switch (desc->chunk_type) {
3251 /* find the tsn to resend (possibly */
3254 struct sctp_tmit_chunk *tp1;
3256 tsn = ntohl(desc->tsn_ifany);
3257 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3258 if (tp1->rec.data.tsn == tsn) {
3262 if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) {
3270 * Do it the other way , aka without paying
3271 * attention to queue seq order.
3273 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3274 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3275 if (tp1->rec.data.tsn == tsn) {
3282 SCTP_STAT_INCR(sctps_pdrptsnnf);
3284 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3287 if (((flg & SCTP_BADCRC) == 0) &&
3288 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3291 if ((stcb->asoc.peers_rwnd == 0) &&
3292 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3293 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3296 if (stcb->asoc.peers_rwnd == 0 &&
3297 (flg & SCTP_FROM_MIDDLE_BOX)) {
3298 SCTP_STAT_INCR(sctps_pdrpdizrw);
3301 ddp = (uint8_t *)(mtod(tp1->data, caddr_t)+
3302 sizeof(struct sctp_data_chunk));
3306 for (iii = 0; iii < sizeof(desc->data_bytes);
3308 if (ddp[iii] != desc->data_bytes[iii]) {
3309 SCTP_STAT_INCR(sctps_pdrpbadd);
3317 * this guy had a RTO calculation
3318 * pending on it, cancel it
3320 if (tp1->whoTo->rto_needed == 0) {
3321 tp1->whoTo->rto_needed = 1;
3325 SCTP_STAT_INCR(sctps_pdrpmark);
3326 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3327 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3329 * mark it as if we were doing a FR, since
3330 * we will be getting gap ack reports behind
3331 * the info from the router.
3333 tp1->rec.data.doing_fast_retransmit = 1;
3335 * mark the tsn with what sequences can
3338 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3339 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3341 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
3344 /* restart the timer */
3345 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3347 SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3348 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3351 /* fix counts and things */
3352 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3353 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3354 tp1->whoTo->flight_size,
3356 (uint32_t)(uintptr_t)stcb,
3359 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3360 sctp_flight_size_decrease(tp1);
3361 sctp_total_flight_decrease(stcb, tp1);
3363 tp1->sent = SCTP_DATAGRAM_RESEND;
3369 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3370 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3373 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3375 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3378 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3379 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3380 audit, stcb->asoc.sent_queue_retran_cnt);
3381 #ifndef SCTP_AUDITING_ENABLED
3382 stcb->asoc.sent_queue_retran_cnt = audit;
3390 struct sctp_tmit_chunk *asconf;
3392 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3394 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3399 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3400 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3401 asconf->sent = SCTP_DATAGRAM_RESEND;
3402 asconf->snd_count--;
3406 case SCTP_INITIATION:
3407 /* resend the INIT */
3408 stcb->asoc.dropped_special_cnt++;
3409 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3411 * If we can get it in, in a few attempts we do
3412 * this, otherwise we let the timer fire.
3414 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3416 SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3417 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3420 case SCTP_SELECTIVE_ACK:
3421 case SCTP_NR_SELECTIVE_ACK:
3422 /* resend the sack */
3423 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3425 case SCTP_HEARTBEAT_REQUEST:
3426 /* resend a demand HB */
3427 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3429 * Only retransmit if we KNOW we wont destroy the
3432 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3436 sctp_send_shutdown(stcb, net);
3438 case SCTP_SHUTDOWN_ACK:
3439 sctp_send_shutdown_ack(stcb, net);
3441 case SCTP_COOKIE_ECHO:
3443 struct sctp_tmit_chunk *cookie;
3446 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3448 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3453 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3454 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3455 cookie->sent = SCTP_DATAGRAM_RESEND;
3456 sctp_stop_all_cookie_timers(stcb);
3460 case SCTP_COOKIE_ACK:
3461 sctp_send_cookie_ack(stcb);
3463 case SCTP_ASCONF_ACK:
3464 /* resend last asconf ack */
3465 sctp_send_asconf_ack(stcb);
3467 case SCTP_IFORWARD_CUM_TSN:
3468 case SCTP_FORWARD_CUM_TSN:
3469 send_forward_tsn(stcb, &stcb->asoc);
3471 /* can't do anything with these */
3472 case SCTP_PACKET_DROPPED:
3473 case SCTP_INITIATION_ACK: /* this should not happen */
3474 case SCTP_HEARTBEAT_ACK:
3475 case SCTP_ABORT_ASSOCIATION:
3476 case SCTP_OPERATION_ERROR:
3477 case SCTP_SHUTDOWN_COMPLETE:
3487 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3493 * We set things to 0xffffffff since this is the last delivered
3494 * sequence and we will be sending in 0 after the reset.
3497 if (number_entries) {
3498 for (i = 0; i < number_entries; i++) {
3499 temp = ntohs(list[i]);
3500 if (temp >= stcb->asoc.streamincnt) {
3503 stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff;
3507 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3508 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
3511 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3515 sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3520 if (number_entries > 0) {
3521 for (i = 0; i < number_entries; i++) {
3522 temp = ntohs(list[i]);
3523 if (temp >= stcb->asoc.streamoutcnt) {
3524 /* no such stream */
3527 stcb->asoc.strmout[temp].next_mid_ordered = 0;
3528 stcb->asoc.strmout[temp].next_mid_unordered = 0;
3531 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3532 stcb->asoc.strmout[i].next_mid_ordered = 0;
3533 stcb->asoc.strmout[i].next_mid_unordered = 0;
3536 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3540 sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3545 if (number_entries > 0) {
3546 for (i = 0; i < number_entries; i++) {
3547 temp = ntohs(list[i]);
3548 if (temp >= stcb->asoc.streamoutcnt) {
3549 /* no such stream */
3552 stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
3555 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3556 stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
3562 struct sctp_stream_reset_request *
3563 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3565 struct sctp_association *asoc;
3566 struct sctp_chunkhdr *ch;
3567 struct sctp_stream_reset_request *r;
3568 struct sctp_tmit_chunk *chk;
3572 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3573 asoc->stream_reset_outstanding = 0;
3576 if (stcb->asoc.str_reset == NULL) {
3577 asoc->stream_reset_outstanding = 0;
3580 chk = stcb->asoc.str_reset;
3581 if (chk->data == NULL) {
3585 /* he wants a copy of the chk pointer */
3588 clen = chk->send_size;
3589 ch = mtod(chk->data, struct sctp_chunkhdr *);
3590 r = (struct sctp_stream_reset_request *)(ch + 1);
3591 if (ntohl(r->request_seq) == seq) {
3595 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3596 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3597 /* move to the next one, there can only be a max of two */
3598 r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3599 if (ntohl(r->request_seq) == seq) {
3603 /* that seq is not here */
3608 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3610 struct sctp_association *asoc;
3611 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3613 if (stcb->asoc.str_reset == NULL) {
3618 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
3619 chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3620 TAILQ_REMOVE(&asoc->control_send_queue,
3624 sctp_m_freem(chk->data);
3627 asoc->ctrl_queue_cnt--;
3628 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3629 /* sa_ignore NO_NULL_CHK */
3630 stcb->asoc.str_reset = NULL;
3635 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3636 uint32_t seq, uint32_t action,
3637 struct sctp_stream_reset_response *respin)
3641 struct sctp_association *asoc = &stcb->asoc;
3642 struct sctp_tmit_chunk *chk;
3643 struct sctp_stream_reset_request *req_param;
3644 struct sctp_stream_reset_out_request *req_out_param;
3645 struct sctp_stream_reset_in_request *req_in_param;
3646 uint32_t number_entries;
3648 if (asoc->stream_reset_outstanding == 0) {
3652 if (seq == stcb->asoc.str_reset_seq_out) {
3653 req_param = sctp_find_stream_reset(stcb, seq, &chk);
3654 if (req_param != NULL) {
3655 stcb->asoc.str_reset_seq_out++;
3656 type = ntohs(req_param->ph.param_type);
3657 lparam_len = ntohs(req_param->ph.param_length);
3658 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3661 req_out_param = (struct sctp_stream_reset_out_request *)req_param;
3662 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3663 asoc->stream_reset_out_is_outstanding = 0;
3664 if (asoc->stream_reset_outstanding)
3665 asoc->stream_reset_outstanding--;
3666 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3668 sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3669 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3670 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3671 } else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
3673 * Set it up so we don't stop
3676 asoc->stream_reset_outstanding++;
3677 stcb->asoc.str_reset_seq_out--;
3678 asoc->stream_reset_out_is_outstanding = 1;
3681 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3683 if (no_clear == 0) {
3684 sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
3686 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3687 req_in_param = (struct sctp_stream_reset_in_request *)req_param;
3688 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3689 if (asoc->stream_reset_outstanding)
3690 asoc->stream_reset_outstanding--;
3691 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3692 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3693 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3694 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3695 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3696 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3698 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3699 /* Ok we now may have more streams */
3702 num_stream = stcb->asoc.strm_pending_add_size;
3703 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3705 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3707 stcb->asoc.strm_pending_add_size = 0;
3708 if (asoc->stream_reset_outstanding)
3709 asoc->stream_reset_outstanding--;
3710 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3711 /* Put the new streams into effect */
3714 for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
3715 asoc->strmout[i].state = SCTP_STREAM_OPEN;
3717 asoc->streamoutcnt += num_stream;
3718 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3719 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3720 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3721 SCTP_STREAM_CHANGE_DENIED);
3723 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3724 SCTP_STREAM_CHANGE_FAILED);
3726 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3727 if (asoc->stream_reset_outstanding)
3728 asoc->stream_reset_outstanding--;
3729 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3730 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3731 SCTP_STREAM_CHANGE_DENIED);
3732 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3733 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3734 SCTP_STREAM_CHANGE_FAILED);
3736 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3738 * a) Adopt the new in tsn.
3740 * c) Adopt the new out-tsn
3742 struct sctp_stream_reset_response_tsn *resp;
3743 struct sctp_forward_tsn_chunk fwdtsn;
3746 if (respin == NULL) {
3750 if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
3753 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3754 resp = (struct sctp_stream_reset_response_tsn *)respin;
3755 asoc->stream_reset_outstanding--;
3756 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3757 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3758 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3759 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3763 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3764 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3765 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3767 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3768 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3769 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3771 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3772 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3774 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3775 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3777 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3778 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3779 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3780 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3781 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3782 SCTP_ASSOC_RESET_DENIED);
3784 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3785 SCTP_ASSOC_RESET_FAILED);
3788 /* get rid of the request and get the request flags */
3789 if (asoc->stream_reset_outstanding == 0) {
3790 sctp_clean_up_stream_reset(stcb);
3794 if (asoc->stream_reset_outstanding == 0) {
3795 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3801 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3802 struct sctp_tmit_chunk *chk,
3803 struct sctp_stream_reset_in_request *req, int trunc)
3811 * peer wants me to send a str-reset to him for my outgoing seq's if
3814 struct sctp_association *asoc = &stcb->asoc;
3816 seq = ntohl(req->request_seq);
3817 if (asoc->str_reset_seq_in == seq) {
3818 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3819 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3820 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3822 /* Can't do it, since they exceeded our buffer size */
3823 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3824 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3825 len = ntohs(req->ph.param_length);
3826 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3827 if (number_entries) {
3828 for (i = 0; i < number_entries; i++) {
3829 temp = ntohs(req->list_of_streams[i]);
3830 if (temp >= stcb->asoc.streamoutcnt) {
3831 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3834 req->list_of_streams[i] = temp;
3836 for (i = 0; i < number_entries; i++) {
3837 if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
3838 stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
3843 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3844 if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
3845 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
3848 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3850 /* Can't do it, since we have sent one out */
3851 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3854 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3855 asoc->str_reset_seq_in++;
3856 } else if (asoc->str_reset_seq_in - 1 == seq) {
3857 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3858 } else if (asoc->str_reset_seq_in - 2 == seq) {
3859 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3861 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3863 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3867 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3868 struct sctp_tmit_chunk *chk,
3869 struct sctp_stream_reset_tsn_request *req)
3871 /* reset all in and out and update the tsn */
3873 * A) reset my str-seq's on in and out. B) Select a receive next,
3874 * and set cum-ack to it. Also process this selected number as a
3875 * fwd-tsn as well. C) set in the response my next sending seq.
3877 struct sctp_forward_tsn_chunk fwdtsn;
3878 struct sctp_association *asoc = &stcb->asoc;
3882 seq = ntohl(req->request_seq);
3883 if (asoc->str_reset_seq_in == seq) {
3884 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3885 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3886 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3888 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3889 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3890 fwdtsn.ch.chunk_flags = 0;
3891 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3892 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3896 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3897 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3898 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3900 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3901 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3902 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3903 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3904 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3905 atomic_add_int(&asoc->sending_seq, 1);
3906 /* save off historical data for retrans */
3907 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3908 asoc->last_sending_seq[0] = asoc->sending_seq;
3909 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3910 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3911 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3912 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3913 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3914 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3916 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3917 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3918 asoc->str_reset_seq_in++;
3919 } else if (asoc->str_reset_seq_in - 1 == seq) {
3920 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3921 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3922 } else if (asoc->str_reset_seq_in - 2 == seq) {
3923 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3924 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3926 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3932 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3933 struct sctp_tmit_chunk *chk,
3934 struct sctp_stream_reset_out_request *req, int trunc)
3937 int number_entries, len;
3938 struct sctp_association *asoc = &stcb->asoc;
3940 seq = ntohl(req->request_seq);
3942 /* now if its not a duplicate we process it */
3943 if (asoc->str_reset_seq_in == seq) {
3944 len = ntohs(req->ph.param_length);
3945 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3947 * the sender is resetting, handle the list issue.. we must
3948 * a) verify if we can do the reset, if so no problem b) If
3949 * we can't do the reset we must copy the request. c) queue
3950 * it, and setup the data in processor to trigger it off
3951 * when needed and dequeue all the queued data.
3953 tsn = ntohl(req->send_reset_at_tsn);
3955 /* move the reset action back one */
3956 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3957 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3958 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3960 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3961 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3962 /* we can do it now */
3963 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3964 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3967 * we must queue it up and thus wait for the TSN's
3968 * to arrive that are at or before tsn
3970 struct sctp_stream_reset_list *liste;
3973 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3974 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3975 siz, SCTP_M_STRESET);
3976 if (liste == NULL) {
3977 /* gak out of memory */
3978 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3979 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3984 liste->number_entries = number_entries;
3985 memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
3986 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3987 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
3989 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3990 asoc->str_reset_seq_in++;
3991 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3993 * one seq back, just echo back last action since my
3994 * response was lost.
3996 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3997 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3999 * two seq back, just echo back last action since my
4000 * response was lost.
4002 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4004 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4009 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4010 struct sctp_stream_reset_add_strm *str_add)
4013 * Peer is requesting to add more streams. If its within our
4014 * max-streams we will allow it.
4016 uint32_t num_stream, i;
4018 struct sctp_association *asoc = &stcb->asoc;
4019 struct sctp_queued_to_read *ctl, *nctl;
4021 /* Get the number. */
4022 seq = ntohl(str_add->request_seq);
4023 num_stream = ntohs(str_add->number_of_streams);
4024 /* Now what would be the new total? */
4025 if (asoc->str_reset_seq_in == seq) {
4026 num_stream += stcb->asoc.streamincnt;
4027 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4028 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4029 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4030 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
4031 (num_stream > 0xffff)) {
4032 /* We must reject it they ask for to many */
4034 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4036 /* Ok, we can do that :-) */
4037 struct sctp_stream_in *oldstrm;
4039 /* save off the old */
4040 oldstrm = stcb->asoc.strmin;
4041 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
4042 (num_stream * sizeof(struct sctp_stream_in)),
4044 if (stcb->asoc.strmin == NULL) {
4045 stcb->asoc.strmin = oldstrm;
4048 /* copy off the old data */
4049 for (i = 0; i < stcb->asoc.streamincnt; i++) {
4050 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4051 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4052 stcb->asoc.strmin[i].sid = i;
4053 stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered;
4054 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
4055 stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
4056 /* now anything on those queues? */
4057 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
4058 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
4059 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
4061 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
4062 TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
4063 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
4066 /* Init the new streams */
4067 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
4068 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4069 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4070 stcb->asoc.strmin[i].sid = i;
4071 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
4072 stcb->asoc.strmin[i].pd_api_started = 0;
4073 stcb->asoc.strmin[i].delivery_started = 0;
4075 SCTP_FREE(oldstrm, SCTP_M_STRMI);
4076 /* update the size */
4077 stcb->asoc.streamincnt = num_stream;
4078 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4079 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
4081 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4082 asoc->str_reset_seq_in++;
4083 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4085 * one seq back, just echo back last action since my
4086 * response was lost.
4088 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4089 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4091 * two seq back, just echo back last action since my
4092 * response was lost.
4094 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4096 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4102 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4103 struct sctp_stream_reset_add_strm *str_add)
4106 * Peer is requesting to add more streams. If its within our
4107 * max-streams we will allow it.
4109 uint16_t num_stream;
4111 struct sctp_association *asoc = &stcb->asoc;
4113 /* Get the number. */
4114 seq = ntohl(str_add->request_seq);
4115 num_stream = ntohs(str_add->number_of_streams);
4116 /* Now what would be the new total? */
4117 if (asoc->str_reset_seq_in == seq) {
4118 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4119 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4120 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4121 } else if (stcb->asoc.stream_reset_outstanding) {
4122 /* We must reject it we have something pending */
4123 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4125 /* Ok, we can do that :-) */
4128 mychk = stcb->asoc.streamoutcnt;
4129 mychk += num_stream;
4130 if (mychk < 0x10000) {
4131 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4132 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
4133 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4136 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4139 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4140 asoc->str_reset_seq_in++;
4141 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4143 * one seq back, just echo back last action since my
4144 * response was lost.
4146 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4147 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4149 * two seq back, just echo back last action since my
4150 * response was lost.
4152 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4154 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4159 __attribute__((noinline))
4162 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4163 struct sctp_chunkhdr *ch_req)
4165 uint16_t remaining_length, param_len, ptype;
4166 struct sctp_paramhdr pstore;
4167 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4171 struct sctp_tmit_chunk *chk;
4172 struct sctp_chunkhdr *ch;
4173 struct sctp_paramhdr *ph;
4177 /* now it may be a reset or a reset-response */
4178 remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
4180 /* setup for adding the response */
4181 sctp_alloc_a_chunk(stcb, chk);
4185 chk->copy_by_ref = 0;
4186 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4187 chk->rec.chunk_id.can_take_data = 0;
4189 chk->asoc = &stcb->asoc;
4190 chk->no_fr_allowed = 0;
4191 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4192 chk->book_size_scale = 0;
4193 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4194 if (chk->data == NULL) {
4197 sctp_m_freem(chk->data);
4200 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4203 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4205 /* setup chunk parameters */
4206 chk->sent = SCTP_DATAGRAM_UNSENT;
4210 ch = mtod(chk->data, struct sctp_chunkhdr *);
4211 ch->chunk_type = SCTP_STREAM_RESET;
4212 ch->chunk_flags = 0;
4213 ch->chunk_length = htons(chk->send_size);
4214 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4215 offset += sizeof(struct sctp_chunkhdr);
4216 while (remaining_length >= sizeof(struct sctp_paramhdr)) {
4217 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
4222 param_len = ntohs(ph->param_length);
4223 if ((param_len > remaining_length) ||
4224 (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
4225 /* bad parameter length */
4228 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
4229 (uint8_t *)&cstore);
4234 ptype = ntohs(ph->param_type);
4236 if (param_len > sizeof(cstore)) {
4241 if (num_param > SCTP_MAX_RESET_PARAMS) {
4242 /* hit the max of parameters already sorry.. */
4245 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4246 struct sctp_stream_reset_out_request *req_out;
4248 if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
4251 req_out = (struct sctp_stream_reset_out_request *)ph;
4253 if (stcb->asoc.stream_reset_outstanding) {
4254 seq = ntohl(req_out->response_seq);
4255 if (seq == stcb->asoc.str_reset_seq_out) {
4257 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4260 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4261 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4262 struct sctp_stream_reset_add_strm *str_add;
4264 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4267 str_add = (struct sctp_stream_reset_add_strm *)ph;
4269 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4270 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4271 struct sctp_stream_reset_add_strm *str_add;
4273 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4276 str_add = (struct sctp_stream_reset_add_strm *)ph;
4278 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4279 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4280 struct sctp_stream_reset_in_request *req_in;
4283 req_in = (struct sctp_stream_reset_in_request *)ph;
4284 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4285 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4286 struct sctp_stream_reset_tsn_request *req_tsn;
4289 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4290 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4292 goto strres_nochunk;
4296 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4297 struct sctp_stream_reset_response *resp;
4300 if (param_len < sizeof(struct sctp_stream_reset_response)) {
4303 resp = (struct sctp_stream_reset_response *)ph;
4304 seq = ntohl(resp->response_seq);
4305 result = ntohl(resp->result);
4306 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4308 goto strres_nochunk;
4313 offset += SCTP_SIZE32(param_len);
4314 if (remaining_length >= SCTP_SIZE32(param_len)) {
4315 remaining_length -= SCTP_SIZE32(param_len);
4317 remaining_length = 0;
4321 /* we have no response free the stuff */
4322 goto strres_nochunk;
4324 /* ok we have a chunk to link in */
4325 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4328 stcb->asoc.ctrl_queue_cnt++;
4333 * Handle a router or endpoints report of a packet loss, there are two ways
4334 * to handle this, either we get the whole packet and must disect it
4335 * ourselves (possibly with truncation and or corruption) or it is a summary
4336 * from a middle box that did the disectting for us.
4339 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4340 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4342 uint32_t bottle_bw, on_queue;
4346 struct sctp_chunk_desc desc;
4347 struct sctp_chunkhdr *ch;
4349 chlen = ntohs(cp->ch.chunk_length);
4350 chlen -= sizeof(struct sctp_pktdrop_chunk);
4351 /* XXX possible chlen underflow */
4354 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4355 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4357 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4358 chlen -= sizeof(struct sctphdr);
4359 /* XXX possible chlen underflow */
4360 memset(&desc, 0, sizeof(desc));
4362 trunc_len = (uint16_t)ntohs(cp->trunc_len);
4363 if (trunc_len > limit) {
4366 /* now the chunks themselves */
4367 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4368 desc.chunk_type = ch->chunk_type;
4369 /* get amount we need to move */
4370 at = ntohs(ch->chunk_length);
4371 if (at < sizeof(struct sctp_chunkhdr)) {
4372 /* corrupt chunk, maybe at the end? */
4373 SCTP_STAT_INCR(sctps_pdrpcrupt);
4376 if (trunc_len == 0) {
4377 /* we are supposed to have all of it */
4379 /* corrupt skip it */
4380 SCTP_STAT_INCR(sctps_pdrpcrupt);
4384 /* is there enough of it left ? */
4385 if (desc.chunk_type == SCTP_DATA) {
4386 if (chlen < (sizeof(struct sctp_data_chunk) +
4387 sizeof(desc.data_bytes))) {
4391 if (chlen < sizeof(struct sctp_chunkhdr)) {
4396 if (desc.chunk_type == SCTP_DATA) {
4397 /* can we get out the tsn? */
4398 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4399 SCTP_STAT_INCR(sctps_pdrpmbda);
4401 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4403 struct sctp_data_chunk *dcp;
4407 dcp = (struct sctp_data_chunk *)ch;
4408 ddp = (uint8_t *)(dcp + 1);
4409 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4410 desc.data_bytes[iii] = ddp[iii];
4412 desc.tsn_ifany = dcp->dp.tsn;
4414 /* nope we are done. */
4415 SCTP_STAT_INCR(sctps_pdrpnedat);
4419 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4420 SCTP_STAT_INCR(sctps_pdrpmbct);
4423 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4424 SCTP_STAT_INCR(sctps_pdrppdbrk);
4427 if (SCTP_SIZE32(at) > chlen) {
4430 chlen -= SCTP_SIZE32(at);
4431 if (chlen < sizeof(struct sctp_chunkhdr)) {
4432 /* done, none left */
4435 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4437 /* Now update any rwnd --- possibly */
4438 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4439 /* From a peer, we get a rwnd report */
4442 SCTP_STAT_INCR(sctps_pdrpfehos);
4444 bottle_bw = ntohl(cp->bottle_bw);
4445 on_queue = ntohl(cp->current_onq);
4446 if (bottle_bw && on_queue) {
4447 /* a rwnd report is in here */
4448 if (bottle_bw > on_queue)
4449 a_rwnd = bottle_bw - on_queue;
4454 stcb->asoc.peers_rwnd = 0;
4456 if (a_rwnd > stcb->asoc.total_flight) {
4457 stcb->asoc.peers_rwnd =
4458 a_rwnd - stcb->asoc.total_flight;
4460 stcb->asoc.peers_rwnd = 0;
4462 if (stcb->asoc.peers_rwnd <
4463 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4464 /* SWS sender side engages */
4465 stcb->asoc.peers_rwnd = 0;
4470 SCTP_STAT_INCR(sctps_pdrpfmbox);
4473 /* now middle boxes in sat networks get a cwnd bump */
4474 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4475 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4476 (stcb->asoc.sat_network)) {
4478 * This is debatable but for sat networks it makes sense
4479 * Note if a T3 timer has went off, we will prohibit any
4480 * changes to cwnd until we exit the t3 loss recovery.
4482 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4483 net, cp, &bottle_bw, &on_queue);
4488 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4489 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4490 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4491 * length of the complete packet outputs: - length: modified to remaining
4492 * length after control processing - netp: modified to new sctp_nets after
4493 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4494 * bad packet,...) otherwise return the tcb for this packet
4497 __attribute__((noinline))
4499 static struct sctp_tcb *
4500 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4501 struct sockaddr *src, struct sockaddr *dst,
4502 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4503 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4504 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4505 uint32_t vrf_id, uint16_t port)
4507 struct sctp_association *asoc;
4508 struct mbuf *op_err;
4509 char msg[SCTP_DIAG_INFO_LEN];
4511 int num_chunks = 0; /* number of control chunks processed */
4512 uint32_t chk_length;
4514 int abort_no_unlock = 0;
4518 * How big should this be, and should it be alloc'd? Lets try the
4519 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4520 * until we get into jumbo grams and such..
4522 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4524 uint32_t auth_offset = 0, auth_len = 0;
4525 int auth_skipped = 0;
4527 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4531 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4532 iphlen, *offset, length, (void *)stcb);
4535 SCTP_TCB_LOCK_ASSERT(stcb);
4537 /* validate chunk header length... */
4538 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4539 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4540 ntohs(ch->chunk_length));
4545 * validate the verification tag
4547 vtag_in = ntohl(sh->v_tag);
4549 if (ch->chunk_type == SCTP_INITIATION) {
4550 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4551 ntohs(ch->chunk_length), vtag_in);
4553 /* protocol error- silently discard... */
4554 SCTP_STAT_INCR(sctps_badvtag);
4556 SCTP_TCB_UNLOCK(stcb);
4560 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4562 * If there is no stcb, skip the AUTH chunk and process
4563 * later after a stcb is found (to validate the lookup was
4566 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4568 (inp->auth_supported == 1)) {
4569 /* save this chunk for later processing */
4571 auth_offset = *offset;
4572 auth_len = ntohs(ch->chunk_length);
4574 /* (temporarily) move past this chunk */
4575 *offset += SCTP_SIZE32(auth_len);
4576 if (*offset >= length) {
4577 /* no more data left in the mbuf chain */
4581 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4582 sizeof(struct sctp_chunkhdr), chunk_buf);
4589 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4590 goto process_control_chunks;
4593 * first check if it's an ASCONF with an unknown src addr we
4594 * need to look inside to find the association
4596 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4597 struct sctp_chunkhdr *asconf_ch = ch;
4598 uint32_t asconf_offset = 0, asconf_len = 0;
4600 /* inp's refcount may be reduced */
4601 SCTP_INP_INCR_REF(inp);
4603 asconf_offset = *offset;
4605 asconf_len = ntohs(asconf_ch->chunk_length);
4606 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4608 stcb = sctp_findassociation_ep_asconf(m,
4611 sh, &inp, netp, vrf_id);
4614 asconf_offset += SCTP_SIZE32(asconf_len);
4615 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4616 sizeof(struct sctp_chunkhdr), chunk_buf);
4617 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4620 * reduce inp's refcount if not reduced in
4621 * sctp_findassociation_ep_asconf().
4623 SCTP_INP_DECR_REF(inp);
4625 /* now go back and verify any auth chunk to be sure */
4626 if (auth_skipped && (stcb != NULL)) {
4627 struct sctp_auth_chunk *auth;
4629 auth = (struct sctp_auth_chunk *)
4630 sctp_m_getptr(m, auth_offset,
4631 auth_len, chunk_buf);
4634 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4636 /* auth HMAC failed so dump it */
4640 /* remaining chunks are HMAC checked */
4641 stcb->asoc.authenticated = 1;
4646 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4647 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4649 /* no association, so it's out of the blue... */
4650 sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4651 mflowtype, mflowid, inp->fibnum,
4657 /* ABORT and SHUTDOWN can use either v_tag... */
4658 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4659 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4660 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4661 /* Take the T-bit always into account. */
4662 if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
4663 (vtag_in == asoc->my_vtag)) ||
4664 (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4665 (asoc->peer_vtag != htonl(0)) &&
4666 (vtag_in == asoc->peer_vtag))) {
4669 /* drop this packet... */
4670 SCTP_STAT_INCR(sctps_badvtag);
4672 SCTP_TCB_UNLOCK(stcb);
4676 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4677 if (vtag_in != asoc->my_vtag) {
4679 * this could be a stale SHUTDOWN-ACK or the
4680 * peer never got the SHUTDOWN-COMPLETE and
4681 * is still hung; we have started a new asoc
4682 * but it won't complete until the shutdown
4686 SCTP_TCB_UNLOCK(stcb);
4688 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4689 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4691 sctp_handle_ootb(m, iphlen, *offset, src, dst,
4693 mflowtype, mflowid, fibnum,
4698 /* for all other chunks, vtag must match */
4699 if (vtag_in != asoc->my_vtag) {
4700 /* invalid vtag... */
4701 SCTPDBG(SCTP_DEBUG_INPUT3,
4702 "invalid vtag: %xh, expect %xh\n",
4703 vtag_in, asoc->my_vtag);
4704 SCTP_STAT_INCR(sctps_badvtag);
4706 SCTP_TCB_UNLOCK(stcb);
4712 } /* end if !SCTP_COOKIE_ECHO */
4714 * process all control chunks...
4716 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4717 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4718 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4719 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4720 /* implied cookie-ack.. we must have lost the ack */
4721 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4722 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4723 stcb->asoc.overall_error_count,
4725 SCTP_FROM_SCTP_INPUT,
4728 stcb->asoc.overall_error_count = 0;
4729 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4732 process_control_chunks:
4733 while (IS_SCTP_CONTROL(ch)) {
4734 /* validate chunk length */
4735 chk_length = ntohs(ch->chunk_length);
4736 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4737 ch->chunk_type, chk_length);
4738 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4739 if (chk_length < sizeof(*ch) ||
4740 (*offset + (int)chk_length) > length) {
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(stcb);
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(stcb);
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(stcb);
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(stcb);
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) && (stcb != NULL)) {
4855 SCTP_TCB_UNLOCK(stcb);
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 != NULL) && (stcb->asoc.total_output_queue_size)) {
4870 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4871 so = SCTP_INP_SO(inp);
4872 atomic_add_int(&stcb->asoc.refcnt, 1);
4873 SCTP_TCB_UNLOCK(stcb);
4874 SCTP_SOCKET_LOCK(so, 1);
4875 SCTP_TCB_LOCK(stcb);
4876 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4878 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4879 SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4880 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4881 SCTP_SOCKET_UNLOCK(so, 1);
4887 /* The INIT-ACK chunk must be the only chunk. */
4888 if ((num_chunks > 1) ||
4889 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4893 if ((netp) && (*netp)) {
4894 ret = sctp_handle_init_ack(m, iphlen, *offset,
4896 (struct sctp_init_ack_chunk *)ch,
4905 if (abort_no_unlock) {
4909 * Special case, I must call the output routine to
4910 * get the cookie echoed
4912 if ((stcb != NULL) && (ret == 0)) {
4913 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4917 case SCTP_SELECTIVE_ACK:
4919 struct sctp_sack_chunk *sack;
4921 uint32_t a_rwnd, cum_ack;
4922 uint16_t num_seg, num_dup;
4924 int offset_seg, offset_dup;
4926 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4927 SCTP_STAT_INCR(sctps_recvsacks);
4929 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4932 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4933 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4936 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4938 * If we have sent a shutdown-ack, we will pay no
4939 * attention to a sack sent in to us since
4940 * we don't care anymore.
4944 sack = (struct sctp_sack_chunk *)ch;
4945 flags = ch->chunk_flags;
4946 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4947 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4948 num_dup = ntohs(sack->sack.num_dup_tsns);
4949 a_rwnd = (uint32_t)ntohl(sack->sack.a_rwnd);
4950 if (sizeof(struct sctp_sack_chunk) +
4951 num_seg * sizeof(struct sctp_gap_ack_block) +
4952 num_dup * sizeof(uint32_t) != chk_length) {
4953 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4956 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4957 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4958 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4959 cum_ack, num_seg, a_rwnd);
4960 stcb->asoc.seen_a_sack_this_pkt = 1;
4961 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4963 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4964 (stcb->asoc.saw_sack_with_frags == 0) &&
4965 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4966 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4969 * We have a SIMPLE sack having no
4970 * prior segments and data on sent
4971 * queue to be acked.. Use the
4972 * faster path sack processing. We
4973 * also allow window update sacks
4974 * with no missing segments to go
4977 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
4980 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4981 num_seg, 0, num_dup, &abort_now, flags,
4982 cum_ack, a_rwnd, ecne_seen);
4985 /* ABORT signal from sack processing */
4989 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4990 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4991 (stcb->asoc.stream_queue_cnt == 0)) {
4992 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4997 * EY - nr_sack: If the received chunk is an
5000 case SCTP_NR_SELECTIVE_ACK:
5002 struct sctp_nr_sack_chunk *nr_sack;
5004 uint32_t a_rwnd, cum_ack;
5005 uint16_t num_seg, num_nr_seg, num_dup;
5007 int offset_seg, offset_dup;
5009 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
5010 SCTP_STAT_INCR(sctps_recvsacks);
5012 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
5015 if (stcb->asoc.nrsack_supported == 0) {
5018 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
5019 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
5022 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
5024 * If we have sent a shutdown-ack, we will pay no
5025 * attention to a sack sent in to us since
5026 * we don't care anymore.
5030 nr_sack = (struct sctp_nr_sack_chunk *)ch;
5031 flags = ch->chunk_flags;
5032 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
5033 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
5034 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
5035 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
5036 a_rwnd = (uint32_t)ntohl(nr_sack->nr_sack.a_rwnd);
5037 if (sizeof(struct sctp_nr_sack_chunk) +
5038 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
5039 num_dup * sizeof(uint32_t) != chk_length) {
5040 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
5043 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
5044 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
5045 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
5046 cum_ack, num_seg, a_rwnd);
5047 stcb->asoc.seen_a_sack_this_pkt = 1;
5048 if ((stcb->asoc.pr_sctp_cnt == 0) &&
5049 (num_seg == 0) && (num_nr_seg == 0) &&
5050 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
5051 (stcb->asoc.saw_sack_with_frags == 0) &&
5052 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
5053 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
5055 * We have a SIMPLE sack having no
5056 * prior segments and data on sent
5057 * queue to be acked. Use the faster
5058 * path sack processing. We also
5059 * allow window update sacks with no
5060 * missing segments to go this way
5063 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
5064 &abort_now, ecne_seen);
5067 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
5068 num_seg, num_nr_seg, num_dup, &abort_now, flags,
5069 cum_ack, a_rwnd, ecne_seen);
5072 /* ABORT signal from sack processing */
5076 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5077 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5078 (stcb->asoc.stream_queue_cnt == 0)) {
5079 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5084 case SCTP_HEARTBEAT_REQUEST:
5085 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
5086 if ((stcb) && netp && *netp) {
5087 SCTP_STAT_INCR(sctps_recvheartbeat);
5088 sctp_send_heartbeat_ack(stcb, m, *offset,
5091 /* He's alive so give him credit */
5092 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5093 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5094 stcb->asoc.overall_error_count,
5096 SCTP_FROM_SCTP_INPUT,
5099 stcb->asoc.overall_error_count = 0;
5102 case SCTP_HEARTBEAT_ACK:
5103 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
5104 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5109 /* He's alive so give him credit */
5110 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5111 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5112 stcb->asoc.overall_error_count,
5114 SCTP_FROM_SCTP_INPUT,
5117 stcb->asoc.overall_error_count = 0;
5118 SCTP_STAT_INCR(sctps_recvheartbeatack);
5120 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5123 case SCTP_ABORT_ASSOCIATION:
5124 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5127 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5128 if (sctp_handle_abort((struct sctp_abort_chunk *)ch, stcb, *netp)) {
5138 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5140 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5144 if (netp && *netp) {
5147 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5148 stcb, *netp, &abort_flag);
5155 case SCTP_SHUTDOWN_ACK:
5156 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", (void *)stcb);
5157 if ((stcb) && (netp) && (*netp))
5158 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5163 case SCTP_OPERATION_ERROR:
5164 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
5165 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
5170 case SCTP_COOKIE_ECHO:
5171 SCTPDBG(SCTP_DEBUG_INPUT3,
5172 "SCTP_COOKIE-ECHO, stcb %p\n", (void *)stcb);
5173 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5176 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5177 /* We are not interested anymore */
5180 SCTP_TCB_UNLOCK(stcb);
5187 * First are we accepting? We do this again here
5188 * since it is possible that a previous endpoint WAS
5189 * listening responded to a INIT-ACK and then
5190 * closed. We opened and bound.. and are now no
5193 * XXXGL: notes on checking listen queue length.
5194 * 1) SCTP_IS_LISTENING() doesn't necessarily mean
5195 * SOLISTENING(), because a listening "UDP type"
5196 * socket isn't listening in terms of the socket
5197 * layer. It is a normal data flow socket, that
5198 * can fork off new connections. Thus, we should
5199 * look into sol_qlen only in case we are !UDP.
5200 * 2) Checking sol_qlen in general requires locking
5201 * the socket, and this code lacks that.
5203 if ((stcb == NULL) &&
5204 (!SCTP_IS_LISTENING(inp) ||
5205 (!(inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
5206 inp->sctp_socket->sol_qlen >= inp->sctp_socket->sol_qlimit))) {
5207 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5208 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5209 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
5210 sctp_abort_association(inp, stcb, m, iphlen,
5211 src, dst, sh, op_err,
5218 struct mbuf *ret_buf;
5219 struct sctp_inpcb *linp;
5228 SCTP_ASOC_CREATE_LOCK(linp);
5229 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5230 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5231 SCTP_ASOC_CREATE_UNLOCK(linp);
5236 struct sctp_tcb *locked_stcb;
5240 sctp_handle_cookie_echo(m, iphlen,
5244 (struct sctp_cookie_echo_chunk *)ch,
5254 if ((locked_stcb != NULL) && (locked_stcb != stcb)) {
5255 SCTP_TCB_UNLOCK(locked_stcb);
5258 SCTP_TCB_LOCK_ASSERT(stcb);
5264 SCTP_ASOC_CREATE_UNLOCK(linp);
5266 if (ret_buf == NULL) {
5268 SCTP_TCB_UNLOCK(stcb);
5270 SCTPDBG(SCTP_DEBUG_INPUT3,
5271 "GAK, null buffer\n");
5275 /* if AUTH skipped, see if it verified... */
5280 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5282 * Restart the timer if we have
5285 struct sctp_tmit_chunk *chk;
5287 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5288 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5292 case SCTP_COOKIE_ACK:
5293 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", (void *)stcb);
5294 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5297 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5298 /* We are not interested anymore */
5299 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5302 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5303 so = SCTP_INP_SO(inp);
5304 atomic_add_int(&stcb->asoc.refcnt, 1);
5305 SCTP_TCB_UNLOCK(stcb);
5306 SCTP_SOCKET_LOCK(so, 1);
5307 SCTP_TCB_LOCK(stcb);
5308 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5310 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5311 SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5312 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5313 SCTP_SOCKET_UNLOCK(so, 1);
5319 /* He's alive so give him credit */
5320 if ((stcb) && netp && *netp) {
5321 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5322 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5323 stcb->asoc.overall_error_count,
5325 SCTP_FROM_SCTP_INPUT,
5328 stcb->asoc.overall_error_count = 0;
5329 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5333 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5334 /* He's alive so give him credit */
5335 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5341 if (stcb->asoc.ecn_supported == 0) {
5344 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5345 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5346 stcb->asoc.overall_error_count,
5348 SCTP_FROM_SCTP_INPUT,
5351 stcb->asoc.overall_error_count = 0;
5352 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5358 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5359 /* He's alive so give him credit */
5360 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5365 if (stcb->asoc.ecn_supported == 0) {
5368 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5369 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5370 stcb->asoc.overall_error_count,
5372 SCTP_FROM_SCTP_INPUT,
5375 stcb->asoc.overall_error_count = 0;
5376 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5379 case SCTP_SHUTDOWN_COMPLETE:
5380 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", (void *)stcb);
5381 /* must be first and only chunk */
5382 if ((num_chunks > 1) ||
5383 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5387 if ((stcb) && netp && *netp) {
5388 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5395 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5396 /* He's alive so give him credit */
5398 if (stcb->asoc.asconf_supported == 0) {
5401 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5402 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5403 stcb->asoc.overall_error_count,
5405 SCTP_FROM_SCTP_INPUT,
5408 stcb->asoc.overall_error_count = 0;
5409 sctp_handle_asconf(m, *offset, src,
5410 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5414 case SCTP_ASCONF_ACK:
5415 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5416 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5421 if ((stcb) && netp && *netp) {
5422 if (stcb->asoc.asconf_supported == 0) {
5425 /* He's alive so give him credit */
5426 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5427 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5428 stcb->asoc.overall_error_count,
5430 SCTP_FROM_SCTP_INPUT,
5433 stcb->asoc.overall_error_count = 0;
5434 sctp_handle_asconf_ack(m, *offset,
5435 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5436 if (abort_no_unlock)
5440 case SCTP_FORWARD_CUM_TSN:
5441 case SCTP_IFORWARD_CUM_TSN:
5442 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5443 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5448 /* He's alive so give him credit */
5452 if (stcb->asoc.prsctp_supported == 0) {
5455 stcb->asoc.overall_error_count = 0;
5456 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5457 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5458 stcb->asoc.overall_error_count,
5460 SCTP_FROM_SCTP_INPUT,
5464 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5465 /* We are not interested anymore */
5466 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5467 so = SCTP_INP_SO(inp);
5468 atomic_add_int(&stcb->asoc.refcnt, 1);
5469 SCTP_TCB_UNLOCK(stcb);
5470 SCTP_SOCKET_LOCK(so, 1);
5471 SCTP_TCB_LOCK(stcb);
5472 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5474 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5475 SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5476 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5477 SCTP_SOCKET_UNLOCK(so, 1);
5483 * For sending a SACK this looks like DATA
5486 stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from;
5487 sctp_handle_forward_tsn(stcb,
5488 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5493 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5494 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5495 stcb->asoc.overall_error_count,
5497 SCTP_FROM_SCTP_INPUT,
5500 stcb->asoc.overall_error_count = 0;
5505 case SCTP_STREAM_RESET:
5506 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5507 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5512 if (stcb->asoc.reconfig_supported == 0) {
5515 if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5516 /* stop processing */
5521 case SCTP_PACKET_DROPPED:
5522 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5523 /* re-get it all please */
5524 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5529 if (ch && (stcb) && netp && (*netp)) {
5530 if (stcb->asoc.pktdrop_supported == 0) {
5533 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5535 min(chk_length, (sizeof(chunk_buf) - 4)));
5538 case SCTP_AUTHENTICATION:
5539 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5541 /* save the first AUTH for later processing */
5542 if (auth_skipped == 0) {
5543 auth_offset = *offset;
5544 auth_len = chk_length;
5547 /* skip this chunk (temporarily) */
5550 if (stcb->asoc.auth_supported == 0) {
5553 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5554 (chk_length > (sizeof(struct sctp_auth_chunk) +
5555 SCTP_AUTH_DIGEST_LEN_MAX))) {
5560 if (got_auth == 1) {
5561 /* skip this chunk... it's already auth'd */
5565 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5567 /* auth HMAC failed so dump the packet */
5571 /* remaining chunks are HMAC checked */
5572 stcb->asoc.authenticated = 1;
5578 /* it's an unknown chunk! */
5579 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5580 struct sctp_gen_error_cause *cause;
5583 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5584 0, M_NOWAIT, 1, MT_DATA);
5585 if (op_err != NULL) {
5586 len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset));
5587 cause = mtod(op_err, struct sctp_gen_error_cause *);
5588 cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5589 cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause)));
5590 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
5591 SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
5592 if (SCTP_BUF_NEXT(op_err) != NULL) {
5593 #ifdef SCTP_MBUF_LOGGING
5594 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5595 sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5598 sctp_queue_op_err(stcb, op_err);
5600 sctp_m_freem(op_err);
5604 if ((ch->chunk_type & 0x80) == 0) {
5605 /* discard this packet */
5608 } /* else skip this bad chunk and continue... */
5610 } /* switch (ch->chunk_type) */
5614 /* get the next chunk */
5615 *offset += SCTP_SIZE32(chk_length);
5616 if (*offset >= length) {
5617 /* no more data left in the mbuf chain */
5620 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5621 sizeof(struct sctp_chunkhdr), chunk_buf);
5628 if (asconf_cnt > 0 && stcb != NULL) {
5629 sctp_send_asconf_ack(stcb);
5636 * common input chunk processing (v4 and v6)
5639 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5640 struct sockaddr *src, struct sockaddr *dst,
5641 struct sctphdr *sh, struct sctp_chunkhdr *ch,
5642 #if !defined(SCTP_WITH_NO_CSUM)
5643 uint8_t compute_crc,
5646 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5647 uint32_t vrf_id, uint16_t port)
5650 int fwd_tsn_seen = 0, data_processed = 0;
5651 struct mbuf *m = *mm, *op_err;
5652 char msg[SCTP_DIAG_INFO_LEN];
5654 int cnt_ctrl_ready = 0;
5655 struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5656 struct sctp_tcb *stcb = NULL;
5657 struct sctp_nets *net = NULL;
5659 SCTP_STAT_INCR(sctps_recvdatagrams);
5660 #ifdef SCTP_AUDITING_ENABLED
5661 sctp_audit_log(0xE0, 1);
5662 sctp_auditing(0, inp, stcb, net);
5664 #if !defined(SCTP_WITH_NO_CSUM)
5665 if (compute_crc != 0) {
5666 uint32_t check, calc_check;
5668 check = sh->checksum;
5670 calc_check = sctp_calculate_cksum(m, iphlen);
5671 sh->checksum = check;
5672 if (calc_check != check) {
5673 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5674 calc_check, check, (void *)m, length, iphlen);
5675 stcb = sctp_findassociation_addr(m, offset, src, dst,
5676 sh, ch, &inp, &net, vrf_id);
5677 #if defined(INET) || defined(INET6)
5678 if ((ch->chunk_type != SCTP_INITIATION) &&
5679 (net != NULL) && (net->port != port)) {
5680 if (net->port == 0) {
5681 /* UDP encapsulation turned on. */
5682 net->mtu -= sizeof(struct udphdr);
5683 if (stcb->asoc.smallest_mtu > net->mtu) {
5684 sctp_pathmtu_adjustment(stcb, net->mtu);
5686 } else if (port == 0) {
5687 /* UDP encapsulation turned off. */
5688 net->mtu += sizeof(struct udphdr);
5689 /* XXX Update smallest_mtu */
5695 net->flowtype = mflowtype;
5696 net->flowid = mflowid;
5698 if ((inp != NULL) && (stcb != NULL)) {
5699 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5700 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5701 } else if ((inp != NULL) && (stcb == NULL)) {
5704 SCTP_STAT_INCR(sctps_badsum);
5705 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5710 /* Destination port of 0 is illegal, based on RFC4960. */
5711 if (sh->dest_port == 0) {
5712 SCTP_STAT_INCR(sctps_hdrops);
5715 stcb = sctp_findassociation_addr(m, offset, src, dst,
5716 sh, ch, &inp, &net, vrf_id);
5717 #if defined(INET) || defined(INET6)
5718 if ((ch->chunk_type != SCTP_INITIATION) &&
5719 (net != NULL) && (net->port != port)) {
5720 if (net->port == 0) {
5721 /* UDP encapsulation turned on. */
5722 net->mtu -= sizeof(struct udphdr);
5723 if (stcb->asoc.smallest_mtu > net->mtu) {
5724 sctp_pathmtu_adjustment(stcb, net->mtu);
5726 } else if (port == 0) {
5727 /* UDP encapsulation turned off. */
5728 net->mtu += sizeof(struct udphdr);
5729 /* XXX Update smallest_mtu */
5735 net->flowtype = mflowtype;
5736 net->flowid = mflowid;
5739 SCTP_STAT_INCR(sctps_noport);
5740 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5743 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5744 sctp_send_shutdown_complete2(src, dst, sh,
5745 mflowtype, mflowid, fibnum,
5749 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5752 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5753 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5754 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5755 (ch->chunk_type != SCTP_INIT))) {
5756 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5758 sctp_send_abort(m, iphlen, src, dst,
5760 mflowtype, mflowid, fibnum,
5765 } else if (stcb == NULL) {
5768 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5769 (void *)m, iphlen, offset, length, (void *)stcb);
5771 /* always clear this before beginning a packet */
5772 stcb->asoc.authenticated = 0;
5773 stcb->asoc.seen_a_sack_this_pkt = 0;
5774 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5775 (void *)stcb, stcb->asoc.state);
5777 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5778 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5780 * If we hit here, we had a ref count
5781 * up when the assoc was aborted and the
5782 * timer is clearing out the assoc, we should
5783 * NOT respond to any packet.. its OOTB.
5785 SCTP_TCB_UNLOCK(stcb);
5787 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5788 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5790 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5791 mflowtype, mflowid, inp->fibnum,
5796 if (IS_SCTP_CONTROL(ch)) {
5797 /* process the control portion of the SCTP packet */
5798 /* sa_ignore NO_NULL_CHK */
5799 stcb = sctp_process_control(m, iphlen, &offset, length,
5801 inp, stcb, &net, &fwd_tsn_seen,
5802 mflowtype, mflowid, fibnum,
5806 * This covers us if the cookie-echo was there and
5807 * it changes our INP.
5809 inp = stcb->sctp_ep;
5810 #if defined(INET) || defined(INET6)
5811 if ((ch->chunk_type != SCTP_INITIATION) &&
5812 (net != NULL) && (net->port != port)) {
5813 if (net->port == 0) {
5814 /* UDP encapsulation turned on. */
5815 net->mtu -= sizeof(struct udphdr);
5816 if (stcb->asoc.smallest_mtu > net->mtu) {
5817 sctp_pathmtu_adjustment(stcb, net->mtu);
5819 } else if (port == 0) {
5820 /* UDP encapsulation turned off. */
5821 net->mtu += sizeof(struct udphdr);
5822 /* XXX Update smallest_mtu */
5830 * no control chunks, so pre-process DATA chunks (these
5831 * checks are taken care of by control processing)
5835 * if DATA only packet, and auth is required, then punt...
5836 * can't have authenticated without any AUTH (control)
5839 if ((stcb != NULL) &&
5840 (stcb->asoc.auth_supported == 1) &&
5841 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5842 /* "silently" ignore */
5843 SCTP_STAT_INCR(sctps_recvauthmissing);
5847 /* out of the blue DATA chunk */
5848 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5849 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5851 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5852 mflowtype, mflowid, fibnum,
5856 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5857 /* v_tag mismatch! */
5858 SCTP_STAT_INCR(sctps_badvtag);
5865 * no valid TCB for this packet, or we found it's a bad
5866 * packet while processing control, or we're done with this
5867 * packet (done or skip rest of data), so we drop it...
5872 * DATA chunk processing
5874 /* plow through the data chunks while length > offset */
5877 * Rest should be DATA only. Check authentication state if AUTH for
5880 if ((length > offset) &&
5882 (stcb->asoc.auth_supported == 1) &&
5883 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5884 !stcb->asoc.authenticated) {
5885 /* "silently" ignore */
5886 SCTP_STAT_INCR(sctps_recvauthmissing);
5887 SCTPDBG(SCTP_DEBUG_AUTH1,
5888 "Data chunk requires AUTH, skipped\n");
5891 if (length > offset) {
5895 * First check to make sure our state is correct. We would
5896 * not get here unless we really did have a tag, so we don't
5897 * abort if this happens, just dump the chunk silently.
5899 switch (SCTP_GET_STATE(&stcb->asoc)) {
5900 case SCTP_STATE_COOKIE_ECHOED:
5902 * we consider data with valid tags in this state
5903 * shows us the cookie-ack was lost. Imply it was
5906 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5907 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5908 stcb->asoc.overall_error_count,
5910 SCTP_FROM_SCTP_INPUT,
5913 stcb->asoc.overall_error_count = 0;
5914 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5916 case SCTP_STATE_COOKIE_WAIT:
5918 * We consider OOTB any data sent during asoc setup.
5920 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5921 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5923 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5924 mflowtype, mflowid, inp->fibnum,
5927 /* sa_ignore NOTREACHED */
5929 case SCTP_STATE_EMPTY: /* should not happen */
5930 case SCTP_STATE_INUSE: /* should not happen */
5931 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5932 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5935 /* sa_ignore NOTREACHED */
5937 case SCTP_STATE_OPEN:
5938 case SCTP_STATE_SHUTDOWN_SENT:
5941 /* plow through the data chunks while length > offset */
5942 retval = sctp_process_data(mm, iphlen, &offset, length,
5943 inp, stcb, net, &high_tsn);
5946 * The association aborted, NO UNLOCK needed since
5947 * the association is destroyed.
5954 * Anything important needs to have been m_copy'ed in
5958 /* take care of ecn */
5959 if ((data_processed == 1) &&
5960 (stcb->asoc.ecn_supported == 1) &&
5961 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5962 /* Yep, we need to add a ECNE */
5963 sctp_send_ecn_echo(stcb, net, high_tsn);
5965 if ((data_processed == 0) && (fwd_tsn_seen)) {
5967 uint32_t highest_tsn;
5969 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5970 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5972 highest_tsn = stcb->asoc.highest_tsn_inside_map;
5974 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
5975 stcb->asoc.send_sack = 1;
5976 sctp_sack_check(stcb, was_a_gap);
5977 } else if (fwd_tsn_seen) {
5978 stcb->asoc.send_sack = 1;
5980 /* trigger send of any chunks in queue... */
5982 #ifdef SCTP_AUDITING_ENABLED
5983 sctp_audit_log(0xE0, 2);
5984 sctp_auditing(1, inp, stcb, net);
5986 SCTPDBG(SCTP_DEBUG_INPUT1,
5987 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5988 stcb->asoc.peers_rwnd,
5989 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5990 stcb->asoc.total_flight);
5991 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5992 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5993 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5995 if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
5997 stcb->asoc.trigger_reset ||
5999 (stcb->asoc.peers_rwnd > 0 ||
6000 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
6001 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
6002 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
6003 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
6005 #ifdef SCTP_AUDITING_ENABLED
6006 sctp_audit_log(0xE0, 3);
6007 sctp_auditing(2, inp, stcb, net);
6011 SCTP_TCB_UNLOCK(stcb);
6013 if (inp_decr != NULL) {
6014 /* reduce ref-count */
6015 SCTP_INP_WLOCK(inp_decr);
6016 SCTP_INP_DECR_REF(inp_decr);
6017 SCTP_INP_WUNLOCK(inp_decr);
6024 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
6028 uint32_t vrf_id = 0;
6030 struct sockaddr_in src, dst;
6033 struct sctp_chunkhdr *ch;
6035 #if !defined(SCTP_WITH_NO_CSUM)
6036 uint8_t compute_crc;
6043 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
6044 SCTP_RELEASE_PKT(i_pak);
6047 m = SCTP_HEADER_TO_CHAIN(i_pak);
6048 #ifdef SCTP_MBUF_LOGGING
6049 /* Log in any input mbufs */
6050 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6051 sctp_log_mbc(m, SCTP_MBUF_INPUT);
6054 #ifdef SCTP_PACKET_LOGGING
6055 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
6059 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6060 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
6062 if_name(m->m_pkthdr.rcvif),
6063 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
6064 mflowid = m->m_pkthdr.flowid;
6065 mflowtype = M_HASHTYPE_GET(m);
6066 fibnum = M_GETFIB(m);
6067 SCTP_STAT_INCR(sctps_recvpackets);
6068 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
6069 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
6070 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6071 if (SCTP_BUF_LEN(m) < offset) {
6072 if ((m = m_pullup(m, offset)) == NULL) {
6073 SCTP_STAT_INCR(sctps_hdrops);
6077 ip = mtod(m, struct ip *);
6078 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
6079 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
6080 offset -= sizeof(struct sctp_chunkhdr);
6081 memset(&src, 0, sizeof(struct sockaddr_in));
6082 src.sin_family = AF_INET;
6083 src.sin_len = sizeof(struct sockaddr_in);
6084 src.sin_port = sh->src_port;
6085 src.sin_addr = ip->ip_src;
6086 memset(&dst, 0, sizeof(struct sockaddr_in));
6087 dst.sin_family = AF_INET;
6088 dst.sin_len = sizeof(struct sockaddr_in);
6089 dst.sin_port = sh->dest_port;
6090 dst.sin_addr = ip->ip_dst;
6091 length = ntohs(ip->ip_len);
6092 /* Validate mbuf chain length with IP payload length. */
6093 if (SCTP_HEADER_LEN(m) != length) {
6094 SCTPDBG(SCTP_DEBUG_INPUT1,
6095 "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
6096 SCTP_STAT_INCR(sctps_hdrops);
6099 /* SCTP does not allow broadcasts or multicasts */
6100 if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
6103 if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
6106 ecn_bits = ip->ip_tos;
6107 #if defined(SCTP_WITH_NO_CSUM)
6108 SCTP_STAT_INCR(sctps_recvnocrc);
6110 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
6111 SCTP_STAT_INCR(sctps_recvhwcrc);
6114 SCTP_STAT_INCR(sctps_recvswcrc);
6118 sctp_common_input_processing(&m, iphlen, offset, length,
6119 (struct sockaddr *)&src,
6120 (struct sockaddr *)&dst,
6122 #if !defined(SCTP_WITH_NO_CSUM)
6126 mflowtype, mflowid, fibnum,
6135 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6136 extern int *sctp_cpuarry;
6140 sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
6147 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6153 uint32_t flowid, tag;
6155 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
6156 flowid = m->m_pkthdr.flowid;
6159 * No flow id built by lower layers fix it so we
6162 offset = off + sizeof(struct sctphdr);
6163 if (SCTP_BUF_LEN(m) < offset) {
6164 if ((m = m_pullup(m, offset)) == NULL) {
6165 SCTP_STAT_INCR(sctps_hdrops);
6166 return (IPPROTO_DONE);
6169 ip = mtod(m, struct ip *);
6170 sh = (struct sctphdr *)((caddr_t)ip + off);
6171 tag = htonl(sh->v_tag);
6172 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6173 m->m_pkthdr.flowid = flowid;
6174 M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH);
6176 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6177 sctp_queue_to_mcore(m, off, cpu_to_use);
6178 return (IPPROTO_DONE);
6181 sctp_input_with_port(m, off, 0);
6182 return (IPPROTO_DONE);