2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.h>
37 #include <netinet/sctp_var.h>
38 #include <netinet/sctp_sysctl.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctputil.h>
42 #include <netinet/sctp_output.h>
43 #include <netinet/sctp_input.h>
44 #include <netinet/sctp_auth.h>
45 #include <netinet/sctp_indata.h>
46 #include <netinet/sctp_asconf.h>
47 #include <netinet/sctp_bsd_addr.h>
48 #include <netinet/sctp_timer.h>
49 #include <netinet/sctp_crc32.h>
50 #include <netinet/udp.h>
56 sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
58 struct sctp_nets *net;
61 * This now not only stops all cookie timers it also stops any INIT
62 * timers as well. This will make sure that the timers are stopped
63 * in all collision cases.
65 SCTP_TCB_LOCK_ASSERT(stcb);
66 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
67 if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
68 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
71 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
72 } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
73 sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
76 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
83 sctp_handle_init(struct mbuf *m, int iphlen, int offset,
84 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
85 struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
86 struct sctp_tcb *stcb, int *abort_no_unlock,
87 uint8_t use_mflowid, uint32_t mflowid,
88 uint32_t vrf_id, uint16_t port)
90 struct sctp_init *init;
93 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
99 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
100 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
101 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
102 use_mflowid, mflowid,
105 *abort_no_unlock = 1;
108 /* validate parameters */
110 if (init->initiate_tag == 0) {
111 /* protocol error... send abort */
112 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
113 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
114 use_mflowid, mflowid,
117 *abort_no_unlock = 1;
120 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
121 /* invalid parameter... send abort */
122 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
123 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
124 use_mflowid, mflowid,
127 *abort_no_unlock = 1;
130 if (init->num_inbound_streams == 0) {
131 /* protocol error... send abort */
132 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
133 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
134 use_mflowid, mflowid,
137 *abort_no_unlock = 1;
140 if (init->num_outbound_streams == 0) {
141 /* protocol error... send abort */
142 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
143 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
144 use_mflowid, mflowid,
147 *abort_no_unlock = 1;
150 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
151 offset + ntohs(cp->ch.chunk_length))) {
152 /* auth parameter(s) error... send abort */
153 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, NULL,
154 use_mflowid, mflowid,
157 *abort_no_unlock = 1;
161 * We are only accepting if we have a socket with positive
164 if ((stcb == NULL) &&
165 ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
166 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
167 (inp->sctp_socket == NULL) ||
168 (inp->sctp_socket->so_qlimit == 0))) {
170 * FIX ME ?? What about TCP model and we have a
171 * match/restart case? Actually no fix is needed. the lookup
172 * will always find the existing assoc so stcb would not be
173 * NULL. It may be questionable to do this since we COULD
174 * just send back the INIT-ACK and hope that the app did
175 * accept()'s by the time the COOKIE was sent. But there is
176 * a price to pay for COOKIE generation and I don't want to
177 * pay it on the chance that the app will actually do some
178 * accepts(). The App just looses and should NOT be in this
181 if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
182 sctp_send_abort(m, iphlen, src, dst, sh, 0, NULL,
183 use_mflowid, mflowid,
188 if ((stcb != NULL) &&
189 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
190 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
191 sctp_send_shutdown_ack(stcb, NULL);
192 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
194 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
195 sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, src, dst,
197 use_mflowid, mflowid,
199 ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED));
203 SCTP_INP_RUNLOCK(inp);
208 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
212 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
213 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
220 struct sctp_stream_queue_pending *sp;
221 struct sctp_association *asoc;
224 * This function returns the number of streams that have true unsent
225 * data on them. Note that as it looks through it will clean up any
226 * places that have old data that has been sent but left at top of
230 SCTP_TCB_SEND_LOCK(stcb);
231 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
232 /* Check to see if some data queued */
233 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
234 /* sa_ignore FREED_MEMORY */
235 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
239 if ((sp->msg_is_complete) &&
241 (sp->sender_all_done)) {
243 * We are doing differed cleanup. Last time
244 * through when we took all the data the
245 * sender_all_done was not set.
247 if (sp->put_last_out == 0) {
248 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
249 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
255 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
256 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
258 sctp_free_remote_addr(sp->net);
262 sctp_m_freem(sp->data);
265 sctp_free_a_strmoq(stcb, sp, so_locked);
272 SCTP_TCB_SEND_UNLOCK(stcb);
273 return (unsent_data);
277 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
279 struct sctp_init *init;
280 struct sctp_association *asoc;
281 struct sctp_nets *lnet;
286 /* save off parameters */
287 asoc->peer_vtag = ntohl(init->initiate_tag);
288 asoc->peers_rwnd = ntohl(init->a_rwnd);
290 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
292 if (!TAILQ_EMPTY(&asoc->nets)) {
293 /* update any ssthresh's that may have a default */
294 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
295 lnet->ssthresh = asoc->peers_rwnd;
296 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
297 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
301 SCTP_TCB_SEND_LOCK(stcb);
302 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
304 struct sctp_stream_out *outs;
305 struct sctp_stream_queue_pending *sp, *nsp;
306 struct sctp_tmit_chunk *chk, *nchk;
308 /* abandon the upper streams */
309 newcnt = ntohs(init->num_inbound_streams);
310 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
311 if (chk->rec.data.stream_number >= newcnt) {
312 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
313 asoc->send_queue_cnt--;
314 if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
315 asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
318 panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number);
321 if (chk->data != NULL) {
322 sctp_free_bufspace(stcb, asoc, chk, 1);
323 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
324 0, chk, SCTP_SO_NOT_LOCKED);
326 sctp_m_freem(chk->data);
330 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
331 /* sa_ignore FREED_MEMORY */
335 for (i = newcnt; i < asoc->pre_open_streams; i++) {
336 outs = &asoc->strmout[i];
337 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
338 TAILQ_REMOVE(&outs->outqueue, sp, next);
339 asoc->stream_queue_cnt--;
340 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
341 stcb, 0, sp, SCTP_SO_NOT_LOCKED);
343 sctp_m_freem(sp->data);
347 sctp_free_remote_addr(sp->net);
351 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
352 /* sa_ignore FREED_MEMORY */
356 /* cut back the count */
357 asoc->pre_open_streams = newcnt;
359 SCTP_TCB_SEND_UNLOCK(stcb);
360 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
362 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
363 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
364 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
365 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
367 /* This is the next one we expect */
368 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
370 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
371 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
373 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
374 /* open the requested streams */
376 if (asoc->strmin != NULL) {
377 /* Free the old ones */
378 struct sctp_queued_to_read *ctl, *nctl;
380 for (i = 0; i < asoc->streamincnt; i++) {
381 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) {
382 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
383 sctp_free_remote_addr(ctl->whoFrom);
385 sctp_m_freem(ctl->data);
387 sctp_free_a_readq(stcb, ctl);
390 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
392 asoc->streamincnt = ntohs(init->num_outbound_streams);
393 if (asoc->streamincnt > MAX_SCTP_STREAMS) {
394 asoc->streamincnt = MAX_SCTP_STREAMS;
396 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
397 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
398 if (asoc->strmin == NULL) {
399 /* we didn't get memory for the streams! */
400 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
403 for (i = 0; i < asoc->streamincnt; i++) {
404 asoc->strmin[i].stream_no = i;
405 asoc->strmin[i].last_sequence_delivered = 0xffff;
407 * U-stream ranges will be set when the cookie is unpacked.
408 * Or for the INIT sender they are un set (if pr-sctp not
409 * supported) when the INIT-ACK arrives.
411 TAILQ_INIT(&asoc->strmin[i].inqueue);
412 asoc->strmin[i].delivery_started = 0;
415 * load_address_from_init will put the addresses into the
416 * association when the COOKIE is processed or the INIT-ACK is
417 * processed. Both types of COOKIE's existing and new call this
418 * routine. It will remove addresses that are no longer in the
419 * association (for the restarting case where addresses are
420 * removed). Up front when the INIT arrives we will discard it if it
421 * is a restart and new addresses have been added.
423 /* sa_ignore MEMLEAK */
428 * INIT-ACK message processing/consumption returns value < 0 on error
431 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
432 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
433 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
434 struct sctp_nets *net, int *abort_no_unlock,
435 uint8_t use_mflowid, uint32_t mflowid,
438 struct sctp_association *asoc;
440 int retval, abort_flag;
441 uint32_t initack_limit;
442 int nat_friendly = 0;
444 /* First verify that we have no illegal param's */
448 op_err = sctp_arethere_unrecognized_parameters(m,
449 (offset + sizeof(struct sctp_init_chunk)),
450 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
452 /* Send an abort and notify peer */
453 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
454 *abort_no_unlock = 1;
458 asoc->peer_supports_nat = (uint8_t) nat_friendly;
459 /* process the peer's parameters in the INIT-ACK */
460 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
464 initack_limit = offset + ntohs(cp->ch.chunk_length);
465 /* load all addresses */
466 if ((retval = sctp_load_addresses_from_init(stcb, m,
467 (offset + sizeof(struct sctp_init_chunk)), initack_limit,
469 /* Huh, we should abort */
470 SCTPDBG(SCTP_DEBUG_INPUT1,
471 "Load addresses from INIT causes an abort %d\n",
473 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
475 use_mflowid, mflowid,
477 *abort_no_unlock = 1;
480 /* if the peer doesn't support asconf, flush the asconf queue */
481 if (asoc->peer_supports_asconf == 0) {
482 struct sctp_asconf_addr *param, *nparam;
484 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
485 TAILQ_REMOVE(&asoc->asconf_queue, param, next);
486 SCTP_FREE(param, SCTP_M_ASC_ADDR);
489 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
490 stcb->asoc.local_hmacs);
492 sctp_queue_op_err(stcb, op_err);
493 /* queuing will steal away the mbuf chain to the out queue */
496 /* extract the cookie and queue it to "echo" it back... */
497 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
498 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
499 stcb->asoc.overall_error_count,
501 SCTP_FROM_SCTP_INPUT,
504 stcb->asoc.overall_error_count = 0;
505 net->error_count = 0;
508 * Cancel the INIT timer, We do this first before queueing the
509 * cookie. We always cancel at the primary to assue that we are
510 * canceling the timer started by the INIT which always goes to the
513 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
514 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
516 /* calculate the RTO */
517 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy,
518 SCTP_RTT_FROM_NON_DATA);
520 retval = sctp_send_cookie_echo(m, offset, stcb, net);
523 * No cookie, we probably should send a op error. But in any
524 * case if there is no cookie in the INIT-ACK, we can
525 * abandon the peer, its broke.
528 /* We abort with an error of missing mandatory param */
530 sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM);
533 * Expand beyond to include the mandatory
536 struct sctp_inv_mandatory_param *mp;
538 SCTP_BUF_LEN(op_err) =
539 sizeof(struct sctp_inv_mandatory_param);
541 struct sctp_inv_mandatory_param *);
542 /* Subtract the reserved param */
544 htons(sizeof(struct sctp_inv_mandatory_param) - 2);
545 mp->num_param = htonl(1);
546 mp->param = htons(SCTP_STATE_COOKIE);
549 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
550 src, dst, sh, op_err,
551 use_mflowid, mflowid,
553 *abort_no_unlock = 1;
561 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
562 struct sctp_tcb *stcb, struct sctp_nets *net)
564 struct sockaddr_storage store;
565 struct sctp_nets *r_net, *f_net;
568 uint16_t old_error_counter;
571 struct sockaddr_in *sin;
575 struct sockaddr_in6 *sin6;
579 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
583 memset(&store, 0, sizeof(store));
584 switch (cp->heartbeat.hb_info.addr_family) {
587 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
588 sin = (struct sockaddr_in *)&store;
589 sin->sin_family = cp->heartbeat.hb_info.addr_family;
590 sin->sin_len = cp->heartbeat.hb_info.addr_len;
591 sin->sin_port = stcb->rport;
592 memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address,
593 sizeof(sin->sin_addr));
601 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
602 sin6 = (struct sockaddr_in6 *)&store;
603 sin6->sin6_family = cp->heartbeat.hb_info.addr_family;
604 sin6->sin6_len = cp->heartbeat.hb_info.addr_len;
605 sin6->sin6_port = stcb->rport;
606 memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address,
607 sizeof(sin6->sin6_addr));
616 r_net = sctp_findnet(stcb, (struct sockaddr *)&store);
618 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
621 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
622 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
623 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
625 * If the its a HB and it's random value is correct when can
626 * confirm the destination.
628 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
629 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
630 stcb->asoc.primary_destination = r_net;
631 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
632 f_net = TAILQ_FIRST(&stcb->asoc.nets);
633 if (f_net != r_net) {
635 * first one on the list is NOT the primary
636 * sctp_cmpaddr() is much more efficent if
637 * the primary is the first on the list,
640 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
641 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
645 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
646 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
647 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
648 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
650 old_error_counter = r_net->error_count;
651 r_net->error_count = 0;
652 r_net->hb_responded = 1;
653 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
654 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
655 /* Now lets do a RTO with this */
656 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy,
657 SCTP_RTT_FROM_NON_DATA);
658 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
659 r_net->dest_state |= SCTP_ADDR_REACHABLE;
660 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
661 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
663 if (r_net->dest_state & SCTP_ADDR_PF) {
664 r_net->dest_state &= ~SCTP_ADDR_PF;
665 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
667 if (old_error_counter > 0) {
668 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
669 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
671 if (r_net == stcb->asoc.primary_destination) {
672 if (stcb->asoc.alternate) {
673 /* release the alternate, primary is good */
674 sctp_free_remote_addr(stcb->asoc.alternate);
675 stcb->asoc.alternate = NULL;
678 /* Mobility adaptation */
680 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
681 SCTP_MOBILITY_BASE) ||
682 sctp_is_mobility_feature_on(stcb->sctp_ep,
683 SCTP_MOBILITY_FASTHANDOFF)) &&
684 sctp_is_mobility_feature_on(stcb->sctp_ep,
685 SCTP_MOBILITY_PRIM_DELETED)) {
687 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7);
688 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
689 SCTP_MOBILITY_FASTHANDOFF)) {
690 sctp_assoc_immediate_retrans(stcb,
691 stcb->asoc.primary_destination);
693 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
694 SCTP_MOBILITY_BASE)) {
695 sctp_move_chunks_from_net(stcb,
696 stcb->asoc.deleted_primary);
698 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
699 stcb->asoc.deleted_primary);
705 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
708 * return 0 means we want you to proceed with the abort non-zero
709 * means no abort processing
711 struct sctpasochead *head;
713 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
714 /* generate a new vtag and send init */
715 LIST_REMOVE(stcb, sctp_asocs);
716 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
717 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
719 * put it in the bucket in the vtag hash of assoc's for the
722 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
723 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
726 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
728 * treat like a case where the cookie expired i.e.: - dump
729 * current cookie. - generate a new vtag. - resend init.
731 /* generate a new vtag and send init */
732 LIST_REMOVE(stcb, sctp_asocs);
733 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
734 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
735 sctp_stop_all_cookie_timers(stcb);
736 sctp_toss_old_cookies(stcb, &stcb->asoc);
737 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
738 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
740 * put it in the bucket in the vtag hash of assoc's for the
743 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
744 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
751 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
752 struct sctp_nets *net)
755 * return 0 means we want you to proceed with the abort non-zero
756 * means no abort processing
758 if (stcb->asoc.peer_supports_auth == 0) {
759 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
762 sctp_asconf_send_nat_state_update(stcb, net);
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)
778 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
782 len = ntohs(abort->ch.chunk_length);
783 if (len > sizeof(struct sctp_chunkhdr)) {
785 * Need to check the cause codes for our two magic nat
786 * aborts which don't kill the assoc necessarily.
788 struct sctp_missing_nat_state *natc;
790 natc = (struct sctp_missing_nat_state *)(abort + 1);
791 error = ntohs(natc->cause);
792 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
793 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
794 abort->ch.chunk_flags);
795 if (sctp_handle_nat_colliding_state(stcb)) {
798 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
799 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
800 abort->ch.chunk_flags);
801 if (sctp_handle_nat_missing_state(stcb, net)) {
808 /* stop any receive timers */
809 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
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_6);
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");
839 sctp_start_net_timers(struct sctp_tcb *stcb)
841 uint32_t cnt_hb_sent;
842 struct sctp_nets *net;
845 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
847 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
848 * If the dest in unconfirmed send a hb as well if under
849 * max_hb_burst have been sent.
851 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
852 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
853 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
854 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
855 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
860 sctp_chunk_output(stcb->sctp_ep, stcb,
861 SCTP_OUTPUT_FROM_COOKIE_ACK,
868 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
869 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
871 struct sctp_association *asoc;
872 int some_on_streamwheel;
874 #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 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 asoc->control_pdapi->end_added = 1;
903 asoc->control_pdapi->pdapi_aborted = 1;
904 asoc->control_pdapi = NULL;
905 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
906 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
907 so = SCTP_INP_SO(stcb->sctp_ep);
908 atomic_add_int(&stcb->asoc.refcnt, 1);
909 SCTP_TCB_UNLOCK(stcb);
910 SCTP_SOCKET_LOCK(so, 1);
912 atomic_subtract_int(&stcb->asoc.refcnt, 1);
913 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
914 /* assoc was freed while we were unlocked */
915 SCTP_SOCKET_UNLOCK(so, 1);
919 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
920 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
921 SCTP_SOCKET_UNLOCK(so, 1);
924 /* goto SHUTDOWN_RECEIVED state to block new requests */
925 if (stcb->sctp_socket) {
926 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
927 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
928 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
929 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
930 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
932 * notify upper layer that peer has initiated a
935 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
938 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
941 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
943 * stop the shutdown timer, since we WILL move to
946 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
948 /* Now is there unsent data on a stream somewhere? */
949 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
951 if (!TAILQ_EMPTY(&asoc->send_queue) ||
952 !TAILQ_EMPTY(&asoc->sent_queue) ||
953 some_on_streamwheel) {
954 /* By returning we will push more data out */
957 /* no outstanding data to send, so move on... */
958 /* send SHUTDOWN-ACK */
959 /* move to SHUTDOWN-ACK-SENT state */
960 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
961 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
962 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
964 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
965 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
966 sctp_stop_timers_for_shutdown(stcb);
967 sctp_send_shutdown_ack(stcb, net);
968 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
974 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
975 struct sctp_tcb *stcb,
976 struct sctp_nets *net)
978 struct sctp_association *asoc;
980 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
983 so = SCTP_INP_SO(stcb->sctp_ep);
985 SCTPDBG(SCTP_DEBUG_INPUT2,
986 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
991 /* process according to association state */
992 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
993 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
994 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
995 sctp_send_shutdown_complete(stcb, net, 1);
996 SCTP_TCB_UNLOCK(stcb);
999 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
1000 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1001 /* unexpected SHUTDOWN-ACK... so ignore... */
1002 SCTP_TCB_UNLOCK(stcb);
1005 if (asoc->control_pdapi) {
1007 * With a normal shutdown we assume the end of last record.
1009 SCTP_INP_READ_LOCK(stcb->sctp_ep);
1010 asoc->control_pdapi->end_added = 1;
1011 asoc->control_pdapi->pdapi_aborted = 1;
1012 asoc->control_pdapi = NULL;
1013 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1014 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1015 atomic_add_int(&stcb->asoc.refcnt, 1);
1016 SCTP_TCB_UNLOCK(stcb);
1017 SCTP_SOCKET_LOCK(so, 1);
1018 SCTP_TCB_LOCK(stcb);
1019 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1020 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1021 /* assoc was freed while we were unlocked */
1022 SCTP_SOCKET_UNLOCK(so, 1);
1026 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1027 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1028 SCTP_SOCKET_UNLOCK(so, 1);
1031 /* are the queues empty? */
1032 if (!TAILQ_EMPTY(&asoc->send_queue) ||
1033 !TAILQ_EMPTY(&asoc->sent_queue) ||
1034 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
1035 sctp_report_all_outbound(stcb, 0, 0, SCTP_SO_NOT_LOCKED);
1037 /* stop the timer */
1038 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
1039 /* send SHUTDOWN-COMPLETE */
1040 sctp_send_shutdown_complete(stcb, net, 0);
1041 /* notify upper layer protocol */
1042 if (stcb->sctp_socket) {
1043 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1044 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1045 stcb->sctp_socket->so_snd.sb_cc = 0;
1047 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1049 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1050 /* free the TCB but first save off the ep */
1051 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1052 atomic_add_int(&stcb->asoc.refcnt, 1);
1053 SCTP_TCB_UNLOCK(stcb);
1054 SCTP_SOCKET_LOCK(so, 1);
1055 SCTP_TCB_LOCK(stcb);
1056 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1058 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1059 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1060 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1061 SCTP_SOCKET_UNLOCK(so, 1);
1066 * Skip past the param header and then we will find the chunk that caused the
1067 * problem. There are two possiblities ASCONF or FWD-TSN other than that and
1068 * our peer must be broken.
1071 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1072 struct sctp_nets *net)
1074 struct sctp_chunkhdr *chk;
1076 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1077 switch (chk->chunk_type) {
1078 case SCTP_ASCONF_ACK:
1080 sctp_asconf_cleanup(stcb, net);
1082 case SCTP_FORWARD_CUM_TSN:
1083 stcb->asoc.peer_supports_prsctp = 0;
1086 SCTPDBG(SCTP_DEBUG_INPUT2,
1087 "Peer does not support chunk type %d(%x)??\n",
1088 chk->chunk_type, (uint32_t) chk->chunk_type);
1094 * Skip past the param header and then we will find the param that caused the
1095 * problem. There are a number of param's in a ASCONF OR the prsctp param
1096 * these will turn of specific features.
1099 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1101 struct sctp_paramhdr *pbad;
1104 switch (ntohs(pbad->param_type)) {
1106 case SCTP_PRSCTP_SUPPORTED:
1107 stcb->asoc.peer_supports_prsctp = 0;
1109 case SCTP_SUPPORTED_CHUNK_EXT:
1111 /* draft-ietf-tsvwg-addip-sctp */
1112 case SCTP_HAS_NAT_SUPPORT:
1113 stcb->asoc.peer_supports_nat = 0;
1115 case SCTP_ADD_IP_ADDRESS:
1116 case SCTP_DEL_IP_ADDRESS:
1117 case SCTP_SET_PRIM_ADDR:
1118 stcb->asoc.peer_supports_asconf = 0;
1120 case SCTP_SUCCESS_REPORT:
1121 case SCTP_ERROR_CAUSE_IND:
1122 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1123 SCTPDBG(SCTP_DEBUG_INPUT2,
1124 "Turning off ASCONF to this strange peer\n");
1125 stcb->asoc.peer_supports_asconf = 0;
1128 SCTPDBG(SCTP_DEBUG_INPUT2,
1129 "Peer does not support param type %d(%x)??\n",
1130 pbad->param_type, (uint32_t) pbad->param_type);
1136 sctp_handle_error(struct sctp_chunkhdr *ch,
1137 struct sctp_tcb *stcb, struct sctp_nets *net)
1140 struct sctp_paramhdr *phdr;
1141 uint16_t error, error_type;
1143 struct sctp_association *asoc;
1146 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1151 /* parse through all of the errors and process */
1153 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1154 sizeof(struct sctp_chunkhdr));
1155 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1157 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1158 /* Process an Error Cause */
1159 error_type = ntohs(phdr->param_type);
1160 error_len = ntohs(phdr->param_length);
1161 if ((error_len > chklen) || (error_len == 0)) {
1162 /* invalid param length for this param */
1163 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1168 /* report the first error cause */
1171 switch (error_type) {
1172 case SCTP_CAUSE_INVALID_STREAM:
1173 case SCTP_CAUSE_MISSING_PARAM:
1174 case SCTP_CAUSE_INVALID_PARAM:
1175 case SCTP_CAUSE_NO_USER_DATA:
1176 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1179 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1180 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1182 if (sctp_handle_nat_colliding_state(stcb)) {
1186 case SCTP_CAUSE_NAT_MISSING_STATE:
1187 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1189 if (sctp_handle_nat_missing_state(stcb, net)) {
1193 case SCTP_CAUSE_STALE_COOKIE:
1195 * We only act if we have echoed a cookie and are
1198 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1201 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1202 /* Save the time doubled */
1203 asoc->cookie_preserve_req = ntohl(*p) << 1;
1204 asoc->stale_cookie_count++;
1205 if (asoc->stale_cookie_count >
1206 asoc->max_init_times) {
1207 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1208 /* now free the asoc */
1209 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1210 so = SCTP_INP_SO(stcb->sctp_ep);
1211 atomic_add_int(&stcb->asoc.refcnt, 1);
1212 SCTP_TCB_UNLOCK(stcb);
1213 SCTP_SOCKET_LOCK(so, 1);
1214 SCTP_TCB_LOCK(stcb);
1215 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1217 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1218 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1219 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1220 SCTP_SOCKET_UNLOCK(so, 1);
1224 /* blast back to INIT state */
1225 sctp_toss_old_cookies(stcb, &stcb->asoc);
1226 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1227 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1228 sctp_stop_all_cookie_timers(stcb);
1229 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1232 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1234 * Nothing we can do here, we don't do hostname
1235 * addresses so if the peer does not like my IPv6
1236 * (or IPv4 for that matter) it does not matter. If
1237 * they don't support that type of address, they can
1238 * NOT possibly get that packet type... i.e. with no
1239 * IPv6 you can't recieve a IPv6 packet. so we can
1240 * safely ignore this one. If we ever added support
1241 * for HOSTNAME Addresses, then we would need to do
1245 case SCTP_CAUSE_UNRECOG_CHUNK:
1246 sctp_process_unrecog_chunk(stcb, phdr, net);
1248 case SCTP_CAUSE_UNRECOG_PARAM:
1249 sctp_process_unrecog_param(stcb, phdr);
1251 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1253 * We ignore this since the timer will drive out a
1254 * new cookie anyway and there timer will drive us
1255 * to send a SHUTDOWN_COMPLETE. We can't send one
1256 * here since we don't have their tag.
1259 case SCTP_CAUSE_DELETING_LAST_ADDR:
1260 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1261 case SCTP_CAUSE_DELETING_SRC_ADDR:
1263 * We should NOT get these here, but in a
1266 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1269 case SCTP_CAUSE_OUT_OF_RESC:
1271 * And what, pray tell do we do with the fact that
1272 * the peer is out of resources? Not really sure we
1273 * could do anything but abort. I suspect this
1274 * should have came WITH an abort instead of in a
1279 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1283 adjust = SCTP_SIZE32(error_len);
1285 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1287 sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, error, ch, SCTP_SO_NOT_LOCKED);
1292 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1293 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1294 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1295 struct sctp_nets *net, int *abort_no_unlock,
1296 uint8_t use_mflowid, uint32_t mflowid,
1299 struct sctp_init_ack *init_ack;
1300 struct mbuf *op_err;
1302 SCTPDBG(SCTP_DEBUG_INPUT2,
1303 "sctp_handle_init_ack: handling INIT-ACK\n");
1306 SCTPDBG(SCTP_DEBUG_INPUT2,
1307 "sctp_handle_init_ack: TCB is null\n");
1310 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1311 /* Invalid length */
1312 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1313 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1314 src, dst, sh, op_err,
1315 use_mflowid, mflowid,
1317 *abort_no_unlock = 1;
1320 init_ack = &cp->init;
1321 /* validate parameters */
1322 if (init_ack->initiate_tag == 0) {
1323 /* protocol error... send an abort */
1324 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1325 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1326 src, dst, sh, op_err,
1327 use_mflowid, mflowid,
1329 *abort_no_unlock = 1;
1332 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1333 /* protocol error... send an abort */
1334 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1335 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1336 src, dst, sh, op_err,
1337 use_mflowid, mflowid,
1339 *abort_no_unlock = 1;
1342 if (init_ack->num_inbound_streams == 0) {
1343 /* protocol error... send an abort */
1344 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1345 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1346 src, dst, sh, op_err,
1347 use_mflowid, mflowid,
1349 *abort_no_unlock = 1;
1352 if (init_ack->num_outbound_streams == 0) {
1353 /* protocol error... send an abort */
1354 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1355 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1356 src, dst, sh, op_err,
1357 use_mflowid, mflowid,
1359 *abort_no_unlock = 1;
1362 /* process according to association state... */
1363 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1364 case SCTP_STATE_COOKIE_WAIT:
1365 /* this is the expected state for this chunk */
1366 /* process the INIT-ACK parameters */
1367 if (stcb->asoc.primary_destination->dest_state &
1368 SCTP_ADDR_UNCONFIRMED) {
1370 * The primary is where we sent the INIT, we can
1371 * always consider it confirmed when the INIT-ACK is
1372 * returned. Do this before we load addresses
1375 stcb->asoc.primary_destination->dest_state &=
1376 ~SCTP_ADDR_UNCONFIRMED;
1377 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1378 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1380 if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1381 net, abort_no_unlock,
1382 use_mflowid, mflowid,
1384 /* error in parsing parameters */
1387 /* update our state */
1388 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1389 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1391 /* reset the RTO calc */
1392 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1393 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1394 stcb->asoc.overall_error_count,
1396 SCTP_FROM_SCTP_INPUT,
1399 stcb->asoc.overall_error_count = 0;
1400 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1402 * collapse the init timer back in case of a exponential
1405 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1408 * the send at the end of the inbound data processing will
1409 * cause the cookie to be sent
1412 case SCTP_STATE_SHUTDOWN_SENT:
1413 /* incorrect state... discard */
1415 case SCTP_STATE_COOKIE_ECHOED:
1416 /* incorrect state... discard */
1418 case SCTP_STATE_OPEN:
1419 /* incorrect state... discard */
1421 case SCTP_STATE_EMPTY:
1422 case SCTP_STATE_INUSE:
1424 /* incorrect state... discard */
1428 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1432 static struct sctp_tcb *
1433 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1434 struct sockaddr *src, struct sockaddr *dst,
1435 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1436 struct sctp_inpcb *inp, struct sctp_nets **netp,
1437 struct sockaddr *init_src, int *notification,
1438 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1439 uint8_t use_mflowid, uint32_t mflowid,
1440 uint32_t vrf_id, uint16_t port);
1444 * handle a state cookie for an existing association m: input packet mbuf
1445 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1446 * "split" mbuf and the cookie signature does not exist offset: offset into
1447 * mbuf to the cookie-echo chunk
1449 static struct sctp_tcb *
1450 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1451 struct sockaddr *src, struct sockaddr *dst,
1452 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1453 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1454 struct sockaddr *init_src, int *notification,
1455 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1456 uint8_t use_mflowid, uint32_t mflowid,
1457 uint32_t vrf_id, uint16_t port)
1459 struct sctp_association *asoc;
1460 struct sctp_init_chunk *init_cp, init_buf;
1461 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1462 struct sctp_nets *net;
1463 struct mbuf *op_err;
1464 struct sctp_paramhdr *ph;
1465 int init_offset, initack_offset, i;
1471 /* I know that the TCB is non-NULL from the caller */
1473 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1474 if (asoc->cookie_how[how_indx] == 0)
1477 if (how_indx < sizeof(asoc->cookie_how)) {
1478 asoc->cookie_how[how_indx] = 1;
1480 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1481 /* SHUTDOWN came in after sending INIT-ACK */
1482 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1483 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1484 0, M_DONTWAIT, 1, MT_DATA);
1485 if (op_err == NULL) {
1490 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1491 ph = mtod(op_err, struct sctp_paramhdr *);
1492 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
1493 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1494 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1495 use_mflowid, mflowid,
1497 if (how_indx < sizeof(asoc->cookie_how))
1498 asoc->cookie_how[how_indx] = 2;
1502 * find and validate the INIT chunk in the cookie (peer's info) the
1503 * INIT should start after the cookie-echo header struct (chunk
1504 * header, state cookie header struct)
1506 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1508 init_cp = (struct sctp_init_chunk *)
1509 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1510 (uint8_t *) & init_buf);
1511 if (init_cp == NULL) {
1512 /* could not pull a INIT chunk in cookie */
1515 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1519 * find and validate the INIT-ACK chunk in the cookie (my info) the
1520 * INIT-ACK follows the INIT chunk
1522 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1523 initack_cp = (struct sctp_init_ack_chunk *)
1524 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1525 (uint8_t *) & initack_buf);
1526 if (initack_cp == NULL) {
1527 /* could not pull INIT-ACK chunk in cookie */
1530 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1533 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1534 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1536 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1537 * to get into the OPEN state
1539 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1541 * Opps, this means that we somehow generated two vtag's
1542 * the same. I.e. we did:
1544 * <---INIT(tag=a)------
1545 * ----INIT-ACK(tag=t)-->
1546 * ----INIT(tag=t)------> *1
1547 * <---INIT-ACK(tag=a)---
1548 * <----CE(tag=t)------------- *2
1550 * At point *1 we should be generating a different
1551 * tag t'. Which means we would throw away the CE and send
1552 * ours instead. Basically this is case C (throw away side).
1554 if (how_indx < sizeof(asoc->cookie_how))
1555 asoc->cookie_how[how_indx] = 17;
1559 switch SCTP_GET_STATE
1561 case SCTP_STATE_COOKIE_WAIT:
1562 case SCTP_STATE_COOKIE_ECHOED:
1564 * INIT was sent but got a COOKIE_ECHO with the
1565 * correct tags... just accept it...but we must
1566 * process the init so that we can make sure we have
1567 * the right seq no's.
1569 /* First we must process the INIT !! */
1570 retval = sctp_process_init(init_cp, stcb);
1572 if (how_indx < sizeof(asoc->cookie_how))
1573 asoc->cookie_how[how_indx] = 3;
1576 /* we have already processed the INIT so no problem */
1577 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1578 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1579 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1580 /* update current state */
1581 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1582 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1584 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1586 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1587 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1588 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1589 stcb->sctp_ep, stcb, asoc->primary_destination);
1591 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1592 sctp_stop_all_cookie_timers(stcb);
1593 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1594 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1595 (inp->sctp_socket->so_qlimit == 0)
1597 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1602 * Here is where collision would go if we
1603 * did a connect() and instead got a
1604 * init/init-ack/cookie done before the
1605 * init-ack came back..
1607 stcb->sctp_ep->sctp_flags |=
1608 SCTP_PCB_FLAGS_CONNECTED;
1609 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1610 so = SCTP_INP_SO(stcb->sctp_ep);
1611 atomic_add_int(&stcb->asoc.refcnt, 1);
1612 SCTP_TCB_UNLOCK(stcb);
1613 SCTP_SOCKET_LOCK(so, 1);
1614 SCTP_TCB_LOCK(stcb);
1615 atomic_add_int(&stcb->asoc.refcnt, -1);
1616 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1617 SCTP_SOCKET_UNLOCK(so, 1);
1621 soisconnected(stcb->sctp_socket);
1622 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1623 SCTP_SOCKET_UNLOCK(so, 1);
1626 /* notify upper layer */
1627 *notification = SCTP_NOTIFY_ASSOC_UP;
1629 * since we did not send a HB make sure we don't
1632 net->hb_responded = 1;
1633 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1634 &cookie->time_entered,
1635 sctp_align_unsafe_makecopy,
1636 SCTP_RTT_FROM_NON_DATA);
1638 if (stcb->asoc.sctp_autoclose_ticks &&
1639 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1640 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1646 * we're in the OPEN state (or beyond), so peer must
1647 * have simply lost the COOKIE-ACK
1651 sctp_stop_all_cookie_timers(stcb);
1653 * We ignore the return code here.. not sure if we should
1654 * somehow abort.. but we do have an existing asoc. This
1655 * really should not fail.
1657 if (sctp_load_addresses_from_init(stcb, m,
1658 init_offset + sizeof(struct sctp_init_chunk),
1659 initack_offset, src, dst, init_src)) {
1660 if (how_indx < sizeof(asoc->cookie_how))
1661 asoc->cookie_how[how_indx] = 4;
1664 /* respond with a COOKIE-ACK */
1665 sctp_toss_old_cookies(stcb, asoc);
1666 sctp_send_cookie_ack(stcb);
1667 if (how_indx < sizeof(asoc->cookie_how))
1668 asoc->cookie_how[how_indx] = 5;
1671 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1672 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1673 cookie->tie_tag_my_vtag == 0 &&
1674 cookie->tie_tag_peer_vtag == 0) {
1676 * case C in Section 5.2.4 Table 2: XMOO silently discard
1678 if (how_indx < sizeof(asoc->cookie_how))
1679 asoc->cookie_how[how_indx] = 6;
1683 * If nat support, and the below and stcb is established, send back
1684 * a ABORT(colliding state) if we are established.
1686 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1687 (asoc->peer_supports_nat) &&
1688 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1689 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1690 (asoc->peer_vtag == 0)))) {
1692 * Special case - Peer's support nat. We may have two init's
1693 * that we gave out the same tag on since one was not
1694 * established.. i.e. we get INIT from host-1 behind the nat
1695 * and we respond tag-a, we get a INIT from host-2 behind
1696 * the nat and we get tag-a again. Then we bring up host-1
1697 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1698 * Now we have colliding state. We must send an abort here
1699 * with colliding state indication.
1701 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1702 0, M_DONTWAIT, 1, MT_DATA);
1703 if (op_err == NULL) {
1707 /* pre-reserve some space */
1709 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
1711 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
1713 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
1714 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1716 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1717 ph = mtod(op_err, struct sctp_paramhdr *);
1718 ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE);
1719 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1720 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1721 use_mflowid, mflowid,
1725 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1726 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1727 (asoc->peer_vtag == 0))) {
1729 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1730 * should be ok, re-accept peer info
1732 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1734 * Extension of case C. If we hit this, then the
1735 * random number generator returned the same vtag
1736 * when we first sent our INIT-ACK and when we later
1737 * sent our INIT. The side with the seq numbers that
1738 * are different will be the one that normnally
1739 * would have hit case C. This in effect "extends"
1740 * our vtags in this collision case to be 64 bits.
1741 * The same collision could occur aka you get both
1742 * vtag and seq number the same twice in a row.. but
1743 * is much less likely. If it did happen then we
1744 * would proceed through and bring up the assoc.. we
1745 * may end up with the wrong stream setup however..
1746 * which would be bad.. but there is no way to
1747 * tell.. until we send on a stream that does not
1750 if (how_indx < sizeof(asoc->cookie_how))
1751 asoc->cookie_how[how_indx] = 7;
1755 if (how_indx < sizeof(asoc->cookie_how))
1756 asoc->cookie_how[how_indx] = 8;
1757 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1758 sctp_stop_all_cookie_timers(stcb);
1760 * since we did not send a HB make sure we don't double
1763 net->hb_responded = 1;
1764 if (stcb->asoc.sctp_autoclose_ticks &&
1765 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1766 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1769 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1770 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1772 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1774 * Ok the peer probably discarded our data (if we
1775 * echoed a cookie+data). So anything on the
1776 * sent_queue should be marked for retransmit, we
1777 * may not get something to kick us so it COULD
1778 * still take a timeout to move these.. but it can't
1779 * hurt to mark them.
1781 struct sctp_tmit_chunk *chk;
1783 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1784 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1785 chk->sent = SCTP_DATAGRAM_RESEND;
1786 sctp_flight_size_decrease(chk);
1787 sctp_total_flight_decrease(stcb, chk);
1788 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1794 /* process the INIT info (peer's info) */
1795 retval = sctp_process_init(init_cp, stcb);
1797 if (how_indx < sizeof(asoc->cookie_how))
1798 asoc->cookie_how[how_indx] = 9;
1801 if (sctp_load_addresses_from_init(stcb, m,
1802 init_offset + sizeof(struct sctp_init_chunk),
1803 initack_offset, src, dst, init_src)) {
1804 if (how_indx < sizeof(asoc->cookie_how))
1805 asoc->cookie_how[how_indx] = 10;
1808 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1809 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1810 *notification = SCTP_NOTIFY_ASSOC_UP;
1812 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1813 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1814 (inp->sctp_socket->so_qlimit == 0)) {
1815 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1819 stcb->sctp_ep->sctp_flags |=
1820 SCTP_PCB_FLAGS_CONNECTED;
1821 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1822 so = SCTP_INP_SO(stcb->sctp_ep);
1823 atomic_add_int(&stcb->asoc.refcnt, 1);
1824 SCTP_TCB_UNLOCK(stcb);
1825 SCTP_SOCKET_LOCK(so, 1);
1826 SCTP_TCB_LOCK(stcb);
1827 atomic_add_int(&stcb->asoc.refcnt, -1);
1828 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1829 SCTP_SOCKET_UNLOCK(so, 1);
1833 soisconnected(stcb->sctp_socket);
1834 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1835 SCTP_SOCKET_UNLOCK(so, 1);
1838 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1839 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1841 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1842 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1843 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1844 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1846 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1848 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1849 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1850 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1851 stcb->sctp_ep, stcb, asoc->primary_destination);
1853 sctp_stop_all_cookie_timers(stcb);
1854 sctp_toss_old_cookies(stcb, asoc);
1855 sctp_send_cookie_ack(stcb);
1858 * only if we have retrans set do we do this. What
1859 * this call does is get only the COOKIE-ACK out and
1860 * then when we return the normal call to
1861 * sctp_chunk_output will get the retrans out behind
1864 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1866 if (how_indx < sizeof(asoc->cookie_how))
1867 asoc->cookie_how[how_indx] = 11;
1871 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1872 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1873 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1874 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1875 cookie->tie_tag_peer_vtag != 0) {
1876 struct sctpasochead *head;
1878 if (asoc->peer_supports_nat) {
1880 * This is a gross gross hack. just call the
1881 * cookie_new code since we are allowing a duplicate
1882 * association. I hope this works...
1884 return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
1885 sh, cookie, cookie_len,
1886 inp, netp, init_src, notification,
1887 auth_skipped, auth_offset, auth_len,
1888 use_mflowid, mflowid,
1892 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1895 if (how_indx < sizeof(asoc->cookie_how))
1896 asoc->cookie_how[how_indx] = 12;
1897 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1898 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1900 /* notify upper layer */
1901 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1902 atomic_add_int(&stcb->asoc.refcnt, 1);
1903 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1904 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1905 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1906 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1908 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1909 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1910 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1911 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1913 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1914 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1915 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1916 stcb->sctp_ep, stcb, asoc->primary_destination);
1918 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1919 /* move to OPEN state, if not in SHUTDOWN_SENT */
1920 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1922 asoc->pre_open_streams =
1923 ntohs(initack_cp->init.num_outbound_streams);
1924 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1925 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1926 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1928 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1930 asoc->str_reset_seq_in = asoc->init_seq_number;
1932 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1933 if (asoc->mapping_array) {
1934 memset(asoc->mapping_array, 0,
1935 asoc->mapping_array_size);
1937 if (asoc->nr_mapping_array) {
1938 memset(asoc->nr_mapping_array, 0,
1939 asoc->mapping_array_size);
1941 SCTP_TCB_UNLOCK(stcb);
1942 SCTP_INP_INFO_WLOCK();
1943 SCTP_INP_WLOCK(stcb->sctp_ep);
1944 SCTP_TCB_LOCK(stcb);
1945 atomic_add_int(&stcb->asoc.refcnt, -1);
1946 /* send up all the data */
1947 SCTP_TCB_SEND_LOCK(stcb);
1949 sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_NOT_LOCKED);
1950 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1951 stcb->asoc.strmout[i].chunks_on_queues = 0;
1952 stcb->asoc.strmout[i].stream_no = i;
1953 stcb->asoc.strmout[i].next_sequence_send = 0;
1954 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1956 /* process the INIT-ACK info (my info) */
1957 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1958 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1960 /* pull from vtag hash */
1961 LIST_REMOVE(stcb, sctp_asocs);
1962 /* re-insert to new vtag position */
1963 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1964 SCTP_BASE_INFO(hashasocmark))];
1966 * put it in the bucket in the vtag hash of assoc's for the
1969 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1971 /* process the INIT info (peer's info) */
1972 SCTP_TCB_SEND_UNLOCK(stcb);
1973 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1974 SCTP_INP_INFO_WUNLOCK();
1976 retval = sctp_process_init(init_cp, stcb);
1978 if (how_indx < sizeof(asoc->cookie_how))
1979 asoc->cookie_how[how_indx] = 13;
1984 * since we did not send a HB make sure we don't double
1987 net->hb_responded = 1;
1989 if (sctp_load_addresses_from_init(stcb, m,
1990 init_offset + sizeof(struct sctp_init_chunk),
1991 initack_offset, src, dst, init_src)) {
1992 if (how_indx < sizeof(asoc->cookie_how))
1993 asoc->cookie_how[how_indx] = 14;
1997 /* respond with a COOKIE-ACK */
1998 sctp_stop_all_cookie_timers(stcb);
1999 sctp_toss_old_cookies(stcb, asoc);
2000 sctp_send_cookie_ack(stcb);
2001 if (how_indx < sizeof(asoc->cookie_how))
2002 asoc->cookie_how[how_indx] = 15;
2006 if (how_indx < sizeof(asoc->cookie_how))
2007 asoc->cookie_how[how_indx] = 16;
2008 /* all other cases... */
2014 * handle a state cookie for a new association m: input packet mbuf chain--
2015 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
2016 * and the cookie signature does not exist offset: offset into mbuf to the
2017 * cookie-echo chunk length: length of the cookie chunk to: where the init
2018 * was from returns a new TCB
2020 static struct sctp_tcb *
2021 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
2022 struct sockaddr *src, struct sockaddr *dst,
2023 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
2024 struct sctp_inpcb *inp, struct sctp_nets **netp,
2025 struct sockaddr *init_src, int *notification,
2026 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2027 uint8_t use_mflowid, uint32_t mflowid,
2028 uint32_t vrf_id, uint16_t port)
2030 struct sctp_tcb *stcb;
2031 struct sctp_init_chunk *init_cp, init_buf;
2032 struct sctp_init_ack_chunk *initack_cp, initack_buf;
2033 struct sockaddr_storage sa_store;
2034 struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
2035 struct sctp_association *asoc;
2036 int init_offset, initack_offset, initack_limit;
2039 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
2042 struct sockaddr_in *sin;
2046 struct sockaddr_in6 *sin6;
2049 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2052 so = SCTP_INP_SO(inp);
2056 * find and validate the INIT chunk in the cookie (peer's info) the
2057 * INIT should start after the cookie-echo header struct (chunk
2058 * header, state cookie header struct)
2060 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2061 init_cp = (struct sctp_init_chunk *)
2062 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2063 (uint8_t *) & init_buf);
2064 if (init_cp == NULL) {
2065 /* could not pull a INIT chunk in cookie */
2066 SCTPDBG(SCTP_DEBUG_INPUT1,
2067 "process_cookie_new: could not pull INIT chunk hdr\n");
2070 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2071 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2074 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2076 * find and validate the INIT-ACK chunk in the cookie (my info) the
2077 * INIT-ACK follows the INIT chunk
2079 initack_cp = (struct sctp_init_ack_chunk *)
2080 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2081 (uint8_t *) & initack_buf);
2082 if (initack_cp == NULL) {
2083 /* could not pull INIT-ACK chunk in cookie */
2084 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2087 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2091 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2092 * "initack_limit" value. This is because the chk_length field
2093 * includes the length of the cookie, but the cookie is omitted when
2094 * the INIT and INIT_ACK are tacked onto the cookie...
2096 initack_limit = offset + cookie_len;
2099 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2104 * Here we do a trick, we set in NULL for the proc/thread argument.
2105 * We do this since in effect we only use the p argument when the
2106 * socket is unbound and we must do an implicit bind. Since we are
2107 * getting a cookie, we cannot be unbound.
2109 stcb = sctp_aloc_assoc(inp, init_src, &error,
2110 ntohl(initack_cp->init.initiate_tag), vrf_id,
2111 (struct thread *)NULL
2114 struct mbuf *op_err;
2116 /* memory problem? */
2117 SCTPDBG(SCTP_DEBUG_INPUT1,
2118 "process_cookie_new: no room for another TCB!\n");
2119 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2121 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2122 src, dst, sh, op_err,
2123 use_mflowid, mflowid,
2127 /* get the correct sctp_nets */
2129 *netp = sctp_findnet(stcb, init_src);
2132 /* get scope variables out of cookie */
2133 asoc->ipv4_local_scope = cookie->ipv4_scope;
2134 asoc->site_scope = cookie->site_scope;
2135 asoc->local_scope = cookie->local_scope;
2136 asoc->loopback_scope = cookie->loopback_scope;
2138 if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2139 (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2140 struct mbuf *op_err;
2143 * Houston we have a problem. The EP changed while the
2144 * cookie was in flight. Only recourse is to abort the
2147 atomic_add_int(&stcb->asoc.refcnt, 1);
2148 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2149 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2150 src, dst, sh, op_err,
2151 use_mflowid, mflowid,
2153 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2154 SCTP_TCB_UNLOCK(stcb);
2155 SCTP_SOCKET_LOCK(so, 1);
2156 SCTP_TCB_LOCK(stcb);
2158 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2159 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2160 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2161 SCTP_SOCKET_UNLOCK(so, 1);
2163 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2166 /* process the INIT-ACK info (my info) */
2167 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2168 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2169 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2170 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2171 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2172 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2173 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2174 asoc->str_reset_seq_in = asoc->init_seq_number;
2176 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2178 /* process the INIT info (peer's info) */
2180 retval = sctp_process_init(init_cp, stcb);
2184 atomic_add_int(&stcb->asoc.refcnt, 1);
2185 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2186 SCTP_TCB_UNLOCK(stcb);
2187 SCTP_SOCKET_LOCK(so, 1);
2188 SCTP_TCB_LOCK(stcb);
2190 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2191 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2192 SCTP_SOCKET_UNLOCK(so, 1);
2194 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2197 /* load all addresses */
2198 if (sctp_load_addresses_from_init(stcb, m,
2199 init_offset + sizeof(struct sctp_init_chunk), initack_offset,
2200 src, dst, init_src)) {
2201 atomic_add_int(&stcb->asoc.refcnt, 1);
2202 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2203 SCTP_TCB_UNLOCK(stcb);
2204 SCTP_SOCKET_LOCK(so, 1);
2205 SCTP_TCB_LOCK(stcb);
2207 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
2208 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2209 SCTP_SOCKET_UNLOCK(so, 1);
2211 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2215 * verify any preceding AUTH chunk that was skipped
2217 /* pull the local authentication parameters from the cookie/init-ack */
2218 sctp_auth_get_cookie_params(stcb, m,
2219 initack_offset + sizeof(struct sctp_init_ack_chunk),
2220 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2222 struct sctp_auth_chunk *auth;
2224 auth = (struct sctp_auth_chunk *)
2225 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2226 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2227 /* auth HMAC failed, dump the assoc and packet */
2228 SCTPDBG(SCTP_DEBUG_AUTH1,
2229 "COOKIE-ECHO: AUTH failed\n");
2230 atomic_add_int(&stcb->asoc.refcnt, 1);
2231 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2232 SCTP_TCB_UNLOCK(stcb);
2233 SCTP_SOCKET_LOCK(so, 1);
2234 SCTP_TCB_LOCK(stcb);
2236 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2237 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2238 SCTP_SOCKET_UNLOCK(so, 1);
2240 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2243 /* remaining chunks checked... good to go */
2244 stcb->asoc.authenticated = 1;
2247 /* update current state */
2248 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2249 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2250 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2251 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2252 stcb->sctp_ep, stcb, asoc->primary_destination);
2254 sctp_stop_all_cookie_timers(stcb);
2255 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2256 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2259 * if we're doing ASCONFs, check to see if we have any new local
2260 * addresses that need to get added to the peer (eg. addresses
2261 * changed while cookie echo in flight). This needs to be done
2262 * after we go to the OPEN state to do the correct asconf
2263 * processing. else, make sure we have the correct addresses in our
2267 /* warning, we re-use sin, sin6, sa_store here! */
2268 /* pull in local_address (our "from" address) */
2269 switch (cookie->laddr_type) {
2271 case SCTP_IPV4_ADDRESS:
2272 /* source addr is IPv4 */
2273 sin = (struct sockaddr_in *)initack_src;
2274 memset(sin, 0, sizeof(*sin));
2275 sin->sin_family = AF_INET;
2276 sin->sin_len = sizeof(struct sockaddr_in);
2277 sin->sin_addr.s_addr = cookie->laddress[0];
2281 case SCTP_IPV6_ADDRESS:
2282 /* source addr is IPv6 */
2283 sin6 = (struct sockaddr_in6 *)initack_src;
2284 memset(sin6, 0, sizeof(*sin6));
2285 sin6->sin6_family = AF_INET6;
2286 sin6->sin6_len = sizeof(struct sockaddr_in6);
2287 sin6->sin6_scope_id = cookie->scope_id;
2288 memcpy(&sin6->sin6_addr, cookie->laddress,
2289 sizeof(sin6->sin6_addr));
2293 atomic_add_int(&stcb->asoc.refcnt, 1);
2294 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2295 SCTP_TCB_UNLOCK(stcb);
2296 SCTP_SOCKET_LOCK(so, 1);
2297 SCTP_TCB_LOCK(stcb);
2299 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2300 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2301 SCTP_SOCKET_UNLOCK(so, 1);
2303 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2307 /* set up to notify upper layer */
2308 *notification = SCTP_NOTIFY_ASSOC_UP;
2309 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2310 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2311 (inp->sctp_socket->so_qlimit == 0)) {
2313 * This is an endpoint that called connect() how it got a
2314 * cookie that is NEW is a bit of a mystery. It must be that
2315 * the INIT was sent, but before it got there.. a complete
2316 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2317 * should have went to the other code.. not here.. oh well..
2318 * a bit of protection is worth having..
2320 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2321 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2322 atomic_add_int(&stcb->asoc.refcnt, 1);
2323 SCTP_TCB_UNLOCK(stcb);
2324 SCTP_SOCKET_LOCK(so, 1);
2325 SCTP_TCB_LOCK(stcb);
2326 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2327 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2328 SCTP_SOCKET_UNLOCK(so, 1);
2332 soisconnected(stcb->sctp_socket);
2333 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2334 SCTP_SOCKET_UNLOCK(so, 1);
2336 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2337 (inp->sctp_socket->so_qlimit)) {
2339 * We don't want to do anything with this one. Since it is
2340 * the listening guy. The timer will get started for
2341 * accepted connections in the caller.
2345 /* since we did not send a HB make sure we don't double things */
2346 if ((netp) && (*netp))
2347 (*netp)->hb_responded = 1;
2349 if (stcb->asoc.sctp_autoclose_ticks &&
2350 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2351 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2353 /* calculate the RTT */
2354 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2355 if ((netp) && (*netp)) {
2356 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2357 &cookie->time_entered, sctp_align_unsafe_makecopy,
2358 SCTP_RTT_FROM_NON_DATA);
2360 /* respond with a COOKIE-ACK */
2361 sctp_send_cookie_ack(stcb);
2364 * check the address lists for any ASCONFs that need to be sent
2365 * AFTER the cookie-ack is sent
2367 sctp_check_address_list(stcb, m,
2368 initack_offset + sizeof(struct sctp_init_ack_chunk),
2369 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2370 initack_src, cookie->local_scope, cookie->site_scope,
2371 cookie->ipv4_scope, cookie->loopback_scope);
2378 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2379 * we NEED to make sure we are not already using the vtag. If so we
2380 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2381 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2382 SCTP_BASE_INFO(hashasocmark))];
2383 LIST_FOREACH(stcb, head, sctp_asocs) {
2384 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2385 -- SEND ABORT - TRY AGAIN --
2391 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2392 * existing (non-NULL) TCB
2394 static struct mbuf *
2395 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2396 struct sockaddr *src, struct sockaddr *dst,
2397 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2398 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2399 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2400 struct sctp_tcb **locked_tcb,
2401 uint8_t use_mflowid, uint32_t mflowid,
2402 uint32_t vrf_id, uint16_t port)
2404 struct sctp_state_cookie *cookie;
2405 struct sctp_tcb *l_stcb = *stcb;
2406 struct sctp_inpcb *l_inp;
2407 struct sockaddr *to;
2408 struct sctp_pcb *ep;
2410 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2412 uint8_t cookie_ok = 0;
2413 unsigned int sig_offset, cookie_offset;
2414 unsigned int cookie_len;
2416 struct timeval time_expires;
2417 int notification = 0;
2418 struct sctp_nets *netl;
2419 int had_a_existing_tcb = 0;
2420 int send_int_conf = 0;
2423 struct sockaddr_in sin;
2427 struct sockaddr_in6 sin6;
2431 SCTPDBG(SCTP_DEBUG_INPUT2,
2432 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2434 if (inp_p == NULL) {
2437 cookie = &cp->cookie;
2438 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2439 cookie_len = ntohs(cp->ch.chunk_length);
2441 if ((cookie->peerport != sh->src_port) &&
2442 (cookie->myport != sh->dest_port) &&
2443 (cookie->my_vtag != sh->v_tag)) {
2445 * invalid ports or bad tag. Note that we always leave the
2446 * v_tag in the header in network order and when we stored
2447 * it in the my_vtag slot we also left it in network order.
2448 * This maintains the match even though it may be in the
2449 * opposite byte order of the machine :->
2453 if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2454 sizeof(struct sctp_init_chunk) +
2455 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2456 /* cookie too small */
2460 * split off the signature into its own mbuf (since it should not be
2461 * calculated in the sctp_hmac_m() call).
2463 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2464 m_sig = m_split(m, sig_offset, M_DONTWAIT);
2465 if (m_sig == NULL) {
2466 /* out of memory or ?? */
2469 #ifdef SCTP_MBUF_LOGGING
2470 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2473 for (mat = m_sig; mat; mat = SCTP_BUF_NEXT(mat)) {
2474 if (SCTP_BUF_IS_EXTENDED(mat)) {
2475 sctp_log_mb(mat, SCTP_MBUF_SPLIT);
2482 * compute the signature/digest for the cookie
2484 ep = &(*inp_p)->sctp_ep;
2487 SCTP_TCB_UNLOCK(l_stcb);
2489 SCTP_INP_RLOCK(l_inp);
2491 SCTP_TCB_LOCK(l_stcb);
2493 /* which cookie is it? */
2494 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2495 (ep->current_secret_number != ep->last_secret_number)) {
2496 /* it's the old cookie */
2497 (void)sctp_hmac_m(SCTP_HMAC,
2498 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2499 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2501 /* it's the current cookie */
2502 (void)sctp_hmac_m(SCTP_HMAC,
2503 (uint8_t *) ep->secret_key[(int)ep->current_secret_number],
2504 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2506 /* get the signature */
2507 SCTP_INP_RUNLOCK(l_inp);
2508 sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig);
2510 /* couldn't find signature */
2511 sctp_m_freem(m_sig);
2514 /* compare the received digest with the computed digest */
2515 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2516 /* try the old cookie? */
2517 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2518 (ep->current_secret_number != ep->last_secret_number)) {
2519 /* compute digest with old */
2520 (void)sctp_hmac_m(SCTP_HMAC,
2521 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2522 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2524 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2532 * Now before we continue we must reconstruct our mbuf so that
2533 * normal processing of any other chunks will work.
2539 while (SCTP_BUF_NEXT(m_at) != NULL) {
2540 m_at = SCTP_BUF_NEXT(m_at);
2542 SCTP_BUF_NEXT(m_at) = m_sig;
2545 if (cookie_ok == 0) {
2546 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2547 SCTPDBG(SCTP_DEBUG_INPUT2,
2548 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2549 (uint32_t) offset, cookie_offset, sig_offset);
2553 * check the cookie timestamps to be sure it's not stale
2555 (void)SCTP_GETTIME_TIMEVAL(&now);
2556 /* Expire time is in Ticks, so we convert to seconds */
2557 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2558 time_expires.tv_usec = cookie->time_entered.tv_usec;
2560 * TODO sctp_constants.h needs alternative time macros when _KERNEL
2563 if (timevalcmp(&now, &time_expires, >)) {
2564 /* cookie is stale! */
2565 struct mbuf *op_err;
2566 struct sctp_stale_cookie_msg *scm;
2569 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg),
2570 0, M_DONTWAIT, 1, MT_DATA);
2571 if (op_err == NULL) {
2576 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg);
2577 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
2578 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
2579 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
2580 (sizeof(uint32_t))));
2581 /* seconds to usec */
2582 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2585 tim = now.tv_usec - cookie->time_entered.tv_usec;
2586 scm->time_usec = htonl(tim);
2587 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2588 use_mflowid, mflowid,
2593 * Now we must see with the lookup address if we have an existing
2594 * asoc. This will only happen if we were in the COOKIE-WAIT state
2595 * and a INIT collided with us and somewhere the peer sent the
2596 * cookie on another address besides the single address our assoc
2597 * had for him. In this case we will have one of the tie-tags set at
2598 * least AND the address field in the cookie can be used to look it
2602 switch (cookie->addr_type) {
2604 case SCTP_IPV6_ADDRESS:
2605 memset(&sin6, 0, sizeof(sin6));
2606 sin6.sin6_family = AF_INET6;
2607 sin6.sin6_len = sizeof(sin6);
2608 sin6.sin6_port = sh->src_port;
2609 sin6.sin6_scope_id = cookie->scope_id;
2610 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2611 sizeof(sin6.sin6_addr.s6_addr));
2612 to = (struct sockaddr *)&sin6;
2616 case SCTP_IPV4_ADDRESS:
2617 memset(&sin, 0, sizeof(sin));
2618 sin.sin_family = AF_INET;
2619 sin.sin_len = sizeof(sin);
2620 sin.sin_port = sh->src_port;
2621 sin.sin_addr.s_addr = cookie->address[0];
2622 to = (struct sockaddr *)&sin;
2626 /* This should not happen */
2629 if ((*stcb == NULL) && to) {
2630 /* Yep, lets check */
2631 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2632 if (*stcb == NULL) {
2634 * We should have only got back the same inp. If we
2635 * got back a different ep we have a problem. The
2636 * original findep got back l_inp and now
2638 if (l_inp != *inp_p) {
2639 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2642 if (*locked_tcb == NULL) {
2644 * In this case we found the assoc only
2645 * after we locked the create lock. This
2646 * means we are in a colliding case and we
2647 * must make sure that we unlock the tcb if
2648 * its one of the cases where we throw away
2649 * the incoming packets.
2651 *locked_tcb = *stcb;
2654 * We must also increment the inp ref count
2655 * since the ref_count flags was set when we
2656 * did not find the TCB, now we found it
2657 * which reduces the refcount.. we must
2658 * raise it back out to balance it all :-)
2660 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2661 if ((*stcb)->sctp_ep != l_inp) {
2662 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2663 (void *)(*stcb)->sctp_ep, (void *)l_inp);
2671 cookie_len -= SCTP_SIGNATURE_SIZE;
2672 if (*stcb == NULL) {
2673 /* this is the "normal" case... get a new TCB */
2674 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2675 cookie, cookie_len, *inp_p,
2676 netp, to, ¬ification,
2677 auth_skipped, auth_offset, auth_len,
2678 use_mflowid, mflowid,
2681 /* this is abnormal... cookie-echo on existing TCB */
2682 had_a_existing_tcb = 1;
2683 *stcb = sctp_process_cookie_existing(m, iphlen, offset,
2685 cookie, cookie_len, *inp_p, *stcb, netp, to,
2686 ¬ification, auth_skipped, auth_offset, auth_len,
2687 use_mflowid, mflowid,
2691 if (*stcb == NULL) {
2692 /* still no TCB... must be bad cookie-echo */
2695 if ((*netp != NULL) && (use_mflowid != 0)) {
2696 (*netp)->flowid = mflowid;
2698 (*netp)->flowidset = 1;
2702 * Ok, we built an association so confirm the address we sent the
2705 netl = sctp_findnet(*stcb, to);
2707 * This code should in theory NOT run but
2710 /* TSNH! Huh, why do I need to add this address here? */
2711 if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2714 netl = sctp_findnet(*stcb, to);
2717 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2718 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2719 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2724 sctp_start_net_timers(*stcb);
2725 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2726 if (!had_a_existing_tcb ||
2727 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2729 * If we have a NEW cookie or the connect never
2730 * reached the connected state during collision we
2731 * must do the TCP accept thing.
2733 struct socket *so, *oso;
2734 struct sctp_inpcb *inp;
2736 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2738 * For a restart we will keep the same
2739 * socket, no need to do anything. I THINK!!
2741 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2742 if (send_int_conf) {
2743 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2744 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2748 oso = (*inp_p)->sctp_socket;
2749 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2750 SCTP_TCB_UNLOCK((*stcb));
2751 CURVNET_SET(oso->so_vnet);
2752 so = sonewconn(oso, 0
2755 SCTP_TCB_LOCK((*stcb));
2756 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2759 struct mbuf *op_err;
2761 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2762 struct socket *pcb_so;
2765 /* Too many sockets */
2766 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2767 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2768 sctp_abort_association(*inp_p, NULL, m, iphlen,
2769 src, dst, sh, op_err,
2770 use_mflowid, mflowid,
2772 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2773 pcb_so = SCTP_INP_SO(*inp_p);
2774 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2775 SCTP_TCB_UNLOCK((*stcb));
2776 SCTP_SOCKET_LOCK(pcb_so, 1);
2777 SCTP_TCB_LOCK((*stcb));
2778 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2780 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2781 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2782 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2786 inp = (struct sctp_inpcb *)so->so_pcb;
2787 SCTP_INP_INCR_REF(inp);
2789 * We add the unbound flag here so that if we get an
2790 * soabort() before we get the move_pcb done, we
2791 * will properly cleanup.
2793 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2794 SCTP_PCB_FLAGS_CONNECTED |
2795 SCTP_PCB_FLAGS_IN_TCPPOOL |
2796 SCTP_PCB_FLAGS_UNBOUND |
2797 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2798 SCTP_PCB_FLAGS_DONT_WAKE);
2799 inp->sctp_features = (*inp_p)->sctp_features;
2800 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2801 inp->sctp_socket = so;
2802 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2803 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2804 inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable;
2805 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2806 inp->sctp_context = (*inp_p)->sctp_context;
2807 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2808 inp->inp_starting_point_for_iterator = NULL;
2810 * copy in the authentication parameters from the
2813 if (inp->sctp_ep.local_hmacs)
2814 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2815 inp->sctp_ep.local_hmacs =
2816 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2817 if (inp->sctp_ep.local_auth_chunks)
2818 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2819 inp->sctp_ep.local_auth_chunks =
2820 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2823 * Now we must move it from one hash table to
2824 * another and get the tcb in the right place.
2828 * This is where the one-2-one socket is put into
2829 * the accept state waiting for the accept!
2832 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2834 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2836 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2837 SCTP_TCB_UNLOCK((*stcb));
2839 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2841 SCTP_TCB_LOCK((*stcb));
2842 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2846 * now we must check to see if we were aborted while
2847 * the move was going on and the lock/unlock
2850 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2852 * yep it was, we leave the assoc attached
2853 * to the socket since the sctp_inpcb_free()
2854 * call will send an abort for us.
2856 SCTP_INP_DECR_REF(inp);
2859 SCTP_INP_DECR_REF(inp);
2860 /* Switch over to the new guy */
2862 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2863 if (send_int_conf) {
2864 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2865 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2868 * Pull it from the incomplete queue and wake the
2871 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2872 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2873 SCTP_TCB_UNLOCK((*stcb));
2874 SCTP_SOCKET_LOCK(so, 1);
2877 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2878 SCTP_TCB_LOCK((*stcb));
2879 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2880 SCTP_SOCKET_UNLOCK(so, 1);
2886 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2888 if (send_int_conf) {
2889 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2890 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2896 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2897 struct sctp_tcb *stcb, struct sctp_nets *net)
2899 /* cp must not be used, others call this without a c-ack :-) */
2900 struct sctp_association *asoc;
2902 SCTPDBG(SCTP_DEBUG_INPUT2,
2903 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2909 sctp_stop_all_cookie_timers(stcb);
2910 /* process according to association state */
2911 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2912 /* state change only needed when I am in right state */
2913 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2914 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2915 sctp_start_net_timers(stcb);
2916 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2917 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2918 stcb->sctp_ep, stcb, asoc->primary_destination);
2922 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2923 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2924 if (asoc->overall_error_count == 0) {
2925 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2926 &asoc->time_entered, sctp_align_safe_nocopy,
2927 SCTP_RTT_FROM_NON_DATA);
2929 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2930 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2931 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2932 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2933 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2937 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2938 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2939 so = SCTP_INP_SO(stcb->sctp_ep);
2940 atomic_add_int(&stcb->asoc.refcnt, 1);
2941 SCTP_TCB_UNLOCK(stcb);
2942 SCTP_SOCKET_LOCK(so, 1);
2943 SCTP_TCB_LOCK(stcb);
2944 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2946 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2947 soisconnected(stcb->sctp_socket);
2949 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2950 SCTP_SOCKET_UNLOCK(so, 1);
2954 * since we did not send a HB make sure we don't double
2957 net->hb_responded = 1;
2959 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2961 * We don't need to do the asconf thing, nor hb or
2962 * autoclose if the socket is closed.
2966 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2970 if (stcb->asoc.sctp_autoclose_ticks &&
2971 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2972 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2973 stcb->sctp_ep, stcb, NULL);
2976 * send ASCONF if parameters are pending and ASCONFs are
2977 * allowed (eg. addresses changed when init/cookie echo were
2980 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2981 (stcb->asoc.peer_supports_asconf) &&
2982 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2983 #ifdef SCTP_TIMER_BASED_ASCONF
2984 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2985 stcb->sctp_ep, stcb,
2986 stcb->asoc.primary_destination);
2988 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2989 SCTP_ADDR_NOT_LOCKED);
2994 /* Toss the cookie if I can */
2995 sctp_toss_old_cookies(stcb, asoc);
2996 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
2997 /* Restart the timer if we have pending data */
2998 struct sctp_tmit_chunk *chk;
3000 chk = TAILQ_FIRST(&asoc->sent_queue);
3001 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
3006 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
3007 struct sctp_tcb *stcb)
3009 struct sctp_nets *net;
3010 struct sctp_tmit_chunk *lchk;
3011 struct sctp_ecne_chunk bkup;
3012 uint8_t override_bit;
3013 uint32_t tsn, window_data_tsn;
3015 unsigned int pkt_cnt;
3017 len = ntohs(cp->ch.chunk_length);
3018 if ((len != sizeof(struct sctp_ecne_chunk)) &&
3019 (len != sizeof(struct old_sctp_ecne_chunk))) {
3022 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3023 /* Its the old format */
3024 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3025 bkup.num_pkts_since_cwr = htonl(1);
3028 SCTP_STAT_INCR(sctps_recvecne);
3029 tsn = ntohl(cp->tsn);
3030 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3031 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3033 window_data_tsn = stcb->asoc.sending_seq - 1;
3035 window_data_tsn = lchk->rec.data.TSN_seq;
3038 /* Find where it was sent to if possible. */
3040 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3041 if (lchk->rec.data.TSN_seq == tsn) {
3043 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3046 if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) {
3052 * What to do. A previous send of a CWR was possibly lost.
3053 * See how old it is, we may have it marked on the actual
3056 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3057 if (tsn == net->last_cwr_tsn) {
3058 /* Found him, send it off */
3064 * If we reach here, we need to send a special CWR
3065 * that says hey, we did this a long time ago and
3066 * you lost the response.
3068 net = TAILQ_FIRST(&stcb->asoc.nets);
3073 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3080 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3081 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3083 * JRS - Use the congestion control given in the pluggable
3086 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3088 * We reduce once every RTT. So we will only lower cwnd at
3089 * the next sending seq i.e. the window_data_tsn
3091 net->cwr_window_tsn = window_data_tsn;
3092 net->ecn_ce_pkt_cnt += pkt_cnt;
3093 net->lost_cnt = pkt_cnt;
3094 net->last_cwr_tsn = tsn;
3096 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3097 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3098 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3100 * Another loss in the same window update how many
3101 * marks/packets lost we have had.
3105 if (pkt_cnt > net->lost_cnt) {
3106 /* Should be the case */
3107 cnt = (pkt_cnt - net->lost_cnt);
3108 net->ecn_ce_pkt_cnt += cnt;
3110 net->lost_cnt = pkt_cnt;
3111 net->last_cwr_tsn = tsn;
3113 * Most CC functions will ignore this call, since we
3114 * are in-window yet of the initial CE the peer saw.
3116 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3120 * We always send a CWR this way if our previous one was lost our
3121 * peer will get an update, or if it is not time again to reduce we
3122 * still get the cwr to the peer. Note we set the override when we
3123 * could not find the TSN on the chunk or the destination network.
3125 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3129 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3132 * Here we get a CWR from the peer. We must look in the outqueue and
3133 * make sure that we have a covered ECNE in the control chunk part.
3136 struct sctp_tmit_chunk *chk;
3137 struct sctp_ecne_chunk *ecne;
3141 cwr_tsn = ntohl(cp->tsn);
3143 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3144 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
3145 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3148 if ((override == 0) && (chk->whoTo != net)) {
3149 /* Must be from the right src unless override is set */
3152 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3153 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3154 /* this covers this ECNE, we can remove it */
3155 stcb->asoc.ecn_echo_cnt_onq--;
3156 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3159 sctp_m_freem(chk->data);
3162 stcb->asoc.ctrl_queue_cnt--;
3163 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3164 if (override == 0) {
3172 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3173 struct sctp_tcb *stcb, struct sctp_nets *net)
3175 struct sctp_association *asoc;
3177 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3182 SCTPDBG(SCTP_DEBUG_INPUT2,
3183 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3188 /* process according to association state */
3189 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3190 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3191 SCTPDBG(SCTP_DEBUG_INPUT2,
3192 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3193 SCTP_TCB_UNLOCK(stcb);
3196 /* notify upper layer protocol */
3197 if (stcb->sctp_socket) {
3198 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3199 /* are the queues empty? they should be */
3200 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3201 !TAILQ_EMPTY(&asoc->sent_queue) ||
3202 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
3203 sctp_report_all_outbound(stcb, 0, 0, SCTP_SO_NOT_LOCKED);
3206 /* stop the timer */
3207 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
3208 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3210 SCTPDBG(SCTP_DEBUG_INPUT2,
3211 "sctp_handle_shutdown_complete: calls free-asoc\n");
3212 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3213 so = SCTP_INP_SO(stcb->sctp_ep);
3214 atomic_add_int(&stcb->asoc.refcnt, 1);
3215 SCTP_TCB_UNLOCK(stcb);
3216 SCTP_SOCKET_LOCK(so, 1);
3217 SCTP_TCB_LOCK(stcb);
3218 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3220 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
3221 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3222 SCTP_SOCKET_UNLOCK(so, 1);
3228 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3229 struct sctp_nets *net, uint8_t flg)
3231 switch (desc->chunk_type) {
3233 /* find the tsn to resend (possibly */
3236 struct sctp_tmit_chunk *tp1;
3238 tsn = ntohl(desc->tsn_ifany);
3239 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3240 if (tp1->rec.data.TSN_seq == tsn) {
3244 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) {
3252 * Do it the other way , aka without paying
3253 * attention to queue seq order.
3255 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3256 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3257 if (tp1->rec.data.TSN_seq == tsn) {
3264 SCTP_STAT_INCR(sctps_pdrptsnnf);
3266 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3269 if (((flg & SCTP_BADCRC) == 0) &&
3270 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3273 if ((stcb->asoc.peers_rwnd == 0) &&
3274 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3275 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3278 if (stcb->asoc.peers_rwnd == 0 &&
3279 (flg & SCTP_FROM_MIDDLE_BOX)) {
3280 SCTP_STAT_INCR(sctps_pdrpdizrw);
3283 ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+
3284 sizeof(struct sctp_data_chunk));
3288 for (iii = 0; iii < sizeof(desc->data_bytes);
3290 if (ddp[iii] != desc->data_bytes[iii]) {
3291 SCTP_STAT_INCR(sctps_pdrpbadd);
3299 * this guy had a RTO calculation
3300 * pending on it, cancel it
3302 if (tp1->whoTo->rto_needed == 0) {
3303 tp1->whoTo->rto_needed = 1;
3307 SCTP_STAT_INCR(sctps_pdrpmark);
3308 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3309 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3311 * mark it as if we were doing a FR, since
3312 * we will be getting gap ack reports behind
3313 * the info from the router.
3315 tp1->rec.data.doing_fast_retransmit = 1;
3317 * mark the tsn with what sequences can
3320 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3321 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3323 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
3326 /* restart the timer */
3327 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3328 stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3329 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3332 /* fix counts and things */
3333 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3334 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3335 tp1->whoTo->flight_size,
3338 tp1->rec.data.TSN_seq);
3340 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3341 sctp_flight_size_decrease(tp1);
3342 sctp_total_flight_decrease(stcb, tp1);
3344 tp1->sent = SCTP_DATAGRAM_RESEND;
3350 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3351 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3354 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3356 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3359 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3360 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3361 audit, stcb->asoc.sent_queue_retran_cnt);
3362 #ifndef SCTP_AUDITING_ENABLED
3363 stcb->asoc.sent_queue_retran_cnt = audit;
3371 struct sctp_tmit_chunk *asconf;
3373 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3375 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3380 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3381 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3382 asconf->sent = SCTP_DATAGRAM_RESEND;
3383 asconf->snd_count--;
3387 case SCTP_INITIATION:
3388 /* resend the INIT */
3389 stcb->asoc.dropped_special_cnt++;
3390 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3392 * If we can get it in, in a few attempts we do
3393 * this, otherwise we let the timer fire.
3395 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3396 stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3397 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3400 case SCTP_SELECTIVE_ACK:
3401 case SCTP_NR_SELECTIVE_ACK:
3402 /* resend the sack */
3403 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3405 case SCTP_HEARTBEAT_REQUEST:
3406 /* resend a demand HB */
3407 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3409 * Only retransmit if we KNOW we wont destroy the
3412 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3416 sctp_send_shutdown(stcb, net);
3418 case SCTP_SHUTDOWN_ACK:
3419 sctp_send_shutdown_ack(stcb, net);
3421 case SCTP_COOKIE_ECHO:
3423 struct sctp_tmit_chunk *cookie;
3426 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3428 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3433 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3434 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3435 cookie->sent = SCTP_DATAGRAM_RESEND;
3436 sctp_stop_all_cookie_timers(stcb);
3440 case SCTP_COOKIE_ACK:
3441 sctp_send_cookie_ack(stcb);
3443 case SCTP_ASCONF_ACK:
3444 /* resend last asconf ack */
3445 sctp_send_asconf_ack(stcb);
3447 case SCTP_FORWARD_CUM_TSN:
3448 send_forward_tsn(stcb, &stcb->asoc);
3450 /* can't do anything with these */
3451 case SCTP_PACKET_DROPPED:
3452 case SCTP_INITIATION_ACK: /* this should not happen */
3453 case SCTP_HEARTBEAT_ACK:
3454 case SCTP_ABORT_ASSOCIATION:
3455 case SCTP_OPERATION_ERROR:
3456 case SCTP_SHUTDOWN_COMPLETE:
3466 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t * list)
3472 * We set things to 0xffff since this is the last delivered sequence
3473 * and we will be sending in 0 after the reset.
3476 if (number_entries) {
3477 for (i = 0; i < number_entries; i++) {
3478 temp = ntohs(list[i]);
3479 if (temp >= stcb->asoc.streamincnt) {
3482 stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff;
3486 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3487 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3490 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3494 sctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3498 if (number_entries == 0) {
3499 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3500 stcb->asoc.strmout[i].next_sequence_send = 0;
3502 } else if (number_entries) {
3503 for (i = 0; i < number_entries; i++) {
3506 temp = ntohs(list[i]);
3507 if (temp >= stcb->asoc.streamoutcnt) {
3508 /* no such stream */
3511 stcb->asoc.strmout[temp].next_sequence_send = 0;
3514 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3518 struct sctp_stream_reset_out_request *
3519 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3521 struct sctp_association *asoc;
3522 struct sctp_chunkhdr *ch;
3523 struct sctp_stream_reset_out_request *r;
3524 struct sctp_tmit_chunk *chk;
3528 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3529 asoc->stream_reset_outstanding = 0;
3532 if (stcb->asoc.str_reset == NULL) {
3533 asoc->stream_reset_outstanding = 0;
3536 chk = stcb->asoc.str_reset;
3537 if (chk->data == NULL) {
3541 /* he wants a copy of the chk pointer */
3544 clen = chk->send_size;
3545 ch = mtod(chk->data, struct sctp_chunkhdr *);
3546 r = (struct sctp_stream_reset_out_request *)(ch + 1);
3547 if (ntohl(r->request_seq) == seq) {
3551 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3552 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3553 /* move to the next one, there can only be a max of two */
3554 r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len);
3555 if (ntohl(r->request_seq) == seq) {
3559 /* that seq is not here */
3564 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3566 struct sctp_association *asoc;
3567 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3569 if (stcb->asoc.str_reset == NULL) {
3574 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3575 TAILQ_REMOVE(&asoc->control_send_queue,
3579 sctp_m_freem(chk->data);
3582 asoc->ctrl_queue_cnt--;
3583 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3584 /* sa_ignore NO_NULL_CHK */
3585 stcb->asoc.str_reset = NULL;
3590 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3591 uint32_t seq, uint32_t action,
3592 struct sctp_stream_reset_response *respin)
3596 struct sctp_association *asoc = &stcb->asoc;
3597 struct sctp_tmit_chunk *chk;
3598 struct sctp_stream_reset_out_request *srparam;
3601 if (asoc->stream_reset_outstanding == 0) {
3605 if (seq == stcb->asoc.str_reset_seq_out) {
3606 srparam = sctp_find_stream_reset(stcb, seq, &chk);
3608 stcb->asoc.str_reset_seq_out++;
3609 type = ntohs(srparam->ph.param_type);
3610 lparm_len = ntohs(srparam->ph.param_length);
3611 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3612 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3613 asoc->stream_reset_out_is_outstanding = 0;
3614 if (asoc->stream_reset_outstanding)
3615 asoc->stream_reset_outstanding--;
3616 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3618 sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams);
3619 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3620 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3622 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3624 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3625 /* Answered my request */
3626 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3627 if (asoc->stream_reset_outstanding)
3628 asoc->stream_reset_outstanding--;
3629 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3630 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3631 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3632 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3633 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3634 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3636 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3637 /* Ok we now may have more streams */
3640 num_stream = stcb->asoc.strm_pending_add_size;
3641 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3643 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3645 stcb->asoc.strm_pending_add_size = 0;
3646 if (asoc->stream_reset_outstanding)
3647 asoc->stream_reset_outstanding--;
3648 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3649 /* Put the new streams into effect */
3650 stcb->asoc.streamoutcnt += num_stream;
3651 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3652 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3653 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3654 SCTP_STREAM_CHANGE_DENIED);
3656 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3657 SCTP_STREAM_CHANGE_FAILED);
3659 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3660 if (asoc->stream_reset_outstanding)
3661 asoc->stream_reset_outstanding--;
3662 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3663 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3664 SCTP_STREAM_CHANGE_DENIED);
3665 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3666 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3667 SCTP_STREAM_CHANGE_FAILED);
3669 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3671 * a) Adopt the new in tsn.
3673 * c) Adopt the new out-tsn
3675 struct sctp_stream_reset_response_tsn *resp;
3676 struct sctp_forward_tsn_chunk fwdtsn;
3679 if (respin == NULL) {
3683 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3684 resp = (struct sctp_stream_reset_response_tsn *)respin;
3685 asoc->stream_reset_outstanding--;
3686 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3687 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3688 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3689 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3693 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3694 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3695 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3697 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3698 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3699 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3701 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3702 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3704 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3705 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3707 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3708 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3709 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3710 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3711 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3712 SCTP_ASSOC_RESET_DENIED);
3714 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3715 SCTP_ASSOC_RESET_FAILED);
3718 /* get rid of the request and get the request flags */
3719 if (asoc->stream_reset_outstanding == 0) {
3720 sctp_clean_up_stream_reset(stcb);
3728 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3729 struct sctp_tmit_chunk *chk,
3730 struct sctp_stream_reset_in_request *req, int trunc)
3738 * peer wants me to send a str-reset to him for my outgoing seq's if
3741 struct sctp_association *asoc = &stcb->asoc;
3743 seq = ntohl(req->request_seq);
3744 if (asoc->str_reset_seq_in == seq) {
3745 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3746 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3747 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3749 /* Can't do it, since they exceeded our buffer size */
3750 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3751 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3752 len = ntohs(req->ph.param_length);
3753 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3754 for (i = 0; i < number_entries; i++) {
3755 temp = ntohs(req->list_of_streams[i]);
3756 req->list_of_streams[i] = temp;
3758 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3759 sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams,
3760 asoc->str_reset_seq_out,
3761 seq, (asoc->sending_seq - 1));
3762 asoc->stream_reset_out_is_outstanding = 1;
3763 asoc->str_reset = chk;
3764 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
3765 stcb->asoc.stream_reset_outstanding++;
3767 /* Can't do it, since we have sent one out */
3768 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3770 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3771 asoc->str_reset_seq_in++;
3772 } else if (asoc->str_reset_seq_in - 1 == seq) {
3773 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3774 } else if (asoc->str_reset_seq_in - 2 == seq) {
3775 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3777 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3782 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3783 struct sctp_tmit_chunk *chk,
3784 struct sctp_stream_reset_tsn_request *req)
3786 /* reset all in and out and update the tsn */
3788 * A) reset my str-seq's on in and out. B) Select a receive next,
3789 * and set cum-ack to it. Also process this selected number as a
3790 * fwd-tsn as well. C) set in the response my next sending seq.
3792 struct sctp_forward_tsn_chunk fwdtsn;
3793 struct sctp_association *asoc = &stcb->asoc;
3797 seq = ntohl(req->request_seq);
3798 if (asoc->str_reset_seq_in == seq) {
3799 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3800 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3801 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3803 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3804 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3805 fwdtsn.ch.chunk_flags = 0;
3806 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3807 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3811 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3812 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3813 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3815 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3816 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3817 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3818 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3819 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3820 atomic_add_int(&asoc->sending_seq, 1);
3821 /* save off historical data for retrans */
3822 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3823 asoc->last_sending_seq[0] = asoc->sending_seq;
3824 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3825 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3826 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3827 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3828 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3829 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3831 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3832 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3833 asoc->str_reset_seq_in++;
3834 } else if (asoc->str_reset_seq_in - 1 == seq) {
3835 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3836 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3837 } else if (asoc->str_reset_seq_in - 2 == seq) {
3838 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3839 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3841 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3847 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3848 struct sctp_tmit_chunk *chk,
3849 struct sctp_stream_reset_out_request *req, int trunc)
3852 int number_entries, len;
3853 struct sctp_association *asoc = &stcb->asoc;
3855 seq = ntohl(req->request_seq);
3857 /* now if its not a duplicate we process it */
3858 if (asoc->str_reset_seq_in == seq) {
3859 len = ntohs(req->ph.param_length);
3860 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3862 * the sender is resetting, handle the list issue.. we must
3863 * a) verify if we can do the reset, if so no problem b) If
3864 * we can't do the reset we must copy the request. c) queue
3865 * it, and setup the data in processor to trigger it off
3866 * when needed and dequeue all the queued data.
3868 tsn = ntohl(req->send_reset_at_tsn);
3870 /* move the reset action back one */
3871 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3872 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3873 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3875 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3876 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3877 /* we can do it now */
3878 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3879 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3882 * we must queue it up and thus wait for the TSN's
3883 * to arrive that are at or before tsn
3885 struct sctp_stream_reset_list *liste;
3888 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3889 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3890 siz, SCTP_M_STRESET);
3891 if (liste == NULL) {
3892 /* gak out of memory */
3893 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3894 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3898 liste->number_entries = number_entries;
3899 memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
3900 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3901 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3903 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3904 asoc->str_reset_seq_in++;
3905 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3907 * one seq back, just echo back last action since my
3908 * response was lost.
3910 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3911 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3913 * two seq back, just echo back last action since my
3914 * response was lost.
3916 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3918 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3923 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3924 struct sctp_stream_reset_add_strm *str_add)
3927 * Peer is requesting to add more streams. If its within our
3928 * max-streams we will allow it.
3930 uint32_t num_stream, i;
3932 struct sctp_association *asoc = &stcb->asoc;
3933 struct sctp_queued_to_read *ctl, *nctl;
3935 /* Get the number. */
3936 seq = ntohl(str_add->request_seq);
3937 num_stream = ntohs(str_add->number_of_streams);
3938 /* Now what would be the new total? */
3939 if (asoc->str_reset_seq_in == seq) {
3940 num_stream += stcb->asoc.streamincnt;
3941 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3942 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3943 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3944 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
3945 (num_stream > 0xffff)) {
3946 /* We must reject it they ask for to many */
3948 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3950 /* Ok, we can do that :-) */
3951 struct sctp_stream_in *oldstrm;
3953 /* save off the old */
3954 oldstrm = stcb->asoc.strmin;
3955 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
3956 (num_stream * sizeof(struct sctp_stream_in)),
3958 if (stcb->asoc.strmin == NULL) {
3959 stcb->asoc.strmin = oldstrm;
3962 /* copy off the old data */
3963 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3964 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3965 stcb->asoc.strmin[i].stream_no = i;
3966 stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered;
3967 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
3968 /* now anything on those queues? */
3969 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) {
3970 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next);
3971 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next);
3974 /* Init the new streams */
3975 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
3976 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3977 stcb->asoc.strmin[i].stream_no = i;
3978 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3979 stcb->asoc.strmin[i].delivery_started = 0;
3981 SCTP_FREE(oldstrm, SCTP_M_STRMI);
3982 /* update the size */
3983 stcb->asoc.streamincnt = num_stream;
3984 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3985 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3987 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3988 asoc->str_reset_seq_in++;
3989 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3991 * one seq back, just echo back last action since my
3992 * response was lost.
3994 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3995 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3997 * two seq back, just echo back last action since my
3998 * response was lost.
4000 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4002 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4008 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4009 struct sctp_stream_reset_add_strm *str_add)
4012 * Peer is requesting to add more streams. If its within our
4013 * max-streams we will allow it.
4015 uint16_t num_stream;
4017 struct sctp_association *asoc = &stcb->asoc;
4019 /* Get the number. */
4020 seq = ntohl(str_add->request_seq);
4021 num_stream = ntohs(str_add->number_of_streams);
4022 /* Now what would be the new total? */
4023 if (asoc->str_reset_seq_in == seq) {
4024 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4025 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4026 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4027 } else if (stcb->asoc.stream_reset_outstanding) {
4028 /* We must reject it we have something pending */
4029 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4031 /* Ok, we can do that :-) */
4034 mychk = stcb->asoc.streamoutcnt;
4035 mychk += num_stream;
4036 if (mychk < 0x10000) {
4037 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4038 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 0, 1, num_stream, 0, 1)) {
4039 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4042 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4045 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4046 asoc->str_reset_seq_in++;
4047 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4049 * one seq back, just echo back last action since my
4050 * response was lost.
4052 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4053 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4055 * two seq back, just echo back last action since my
4056 * response was lost.
4058 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4060 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4065 __attribute__((noinline))
4068 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4069 struct sctp_chunkhdr *ch_req)
4071 int chk_length, param_len, ptype;
4072 struct sctp_paramhdr pstore;
4073 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4077 struct sctp_tmit_chunk *chk;
4078 struct sctp_chunkhdr *ch;
4079 struct sctp_paramhdr *ph;
4083 /* now it may be a reset or a reset-response */
4084 chk_length = ntohs(ch_req->chunk_length);
4086 /* setup for adding the response */
4087 sctp_alloc_a_chunk(stcb, chk);
4091 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4092 chk->rec.chunk_id.can_take_data = 0;
4093 chk->asoc = &stcb->asoc;
4094 chk->no_fr_allowed = 0;
4095 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4096 chk->book_size_scale = 0;
4097 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4098 if (chk->data == NULL) {
4101 sctp_m_freem(chk->data);
4104 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4107 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4109 /* setup chunk parameters */
4110 chk->sent = SCTP_DATAGRAM_UNSENT;
4114 ch = mtod(chk->data, struct sctp_chunkhdr *);
4115 ch->chunk_type = SCTP_STREAM_RESET;
4116 ch->chunk_flags = 0;
4117 ch->chunk_length = htons(chk->send_size);
4118 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4119 offset += sizeof(struct sctp_chunkhdr);
4120 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) {
4121 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore);
4124 param_len = ntohs(ph->param_length);
4125 if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) {
4129 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)),
4130 (uint8_t *) & cstore);
4131 ptype = ntohs(ph->param_type);
4133 if (param_len > (int)sizeof(cstore)) {
4138 if (num_param > SCTP_MAX_RESET_PARAMS) {
4139 /* hit the max of parameters already sorry.. */
4142 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4143 struct sctp_stream_reset_out_request *req_out;
4145 req_out = (struct sctp_stream_reset_out_request *)ph;
4147 if (stcb->asoc.stream_reset_outstanding) {
4148 seq = ntohl(req_out->response_seq);
4149 if (seq == stcb->asoc.str_reset_seq_out) {
4151 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4154 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4155 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4156 struct sctp_stream_reset_add_strm *str_add;
4158 str_add = (struct sctp_stream_reset_add_strm *)ph;
4160 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4161 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4162 struct sctp_stream_reset_add_strm *str_add;
4164 str_add = (struct sctp_stream_reset_add_strm *)ph;
4166 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4167 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4168 struct sctp_stream_reset_in_request *req_in;
4171 req_in = (struct sctp_stream_reset_in_request *)ph;
4172 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4173 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4174 struct sctp_stream_reset_tsn_request *req_tsn;
4177 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4178 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4180 goto strres_nochunk;
4184 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4185 struct sctp_stream_reset_response *resp;
4188 resp = (struct sctp_stream_reset_response *)ph;
4189 seq = ntohl(resp->response_seq);
4190 result = ntohl(resp->result);
4191 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4193 goto strres_nochunk;
4198 offset += SCTP_SIZE32(param_len);
4199 chk_length -= SCTP_SIZE32(param_len);
4202 /* we have no response free the stuff */
4203 goto strres_nochunk;
4205 /* ok we have a chunk to link in */
4206 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4209 stcb->asoc.ctrl_queue_cnt++;
4214 * Handle a router or endpoints report of a packet loss, there are two ways
4215 * to handle this, either we get the whole packet and must disect it
4216 * ourselves (possibly with truncation and or corruption) or it is a summary
4217 * from a middle box that did the disectting for us.
4220 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4221 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4223 uint32_t bottle_bw, on_queue;
4227 struct sctp_chunk_desc desc;
4228 struct sctp_chunkhdr *ch;
4230 chlen = ntohs(cp->ch.chunk_length);
4231 chlen -= sizeof(struct sctp_pktdrop_chunk);
4232 /* XXX possible chlen underflow */
4235 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4236 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4238 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4239 chlen -= sizeof(struct sctphdr);
4240 /* XXX possible chlen underflow */
4241 memset(&desc, 0, sizeof(desc));
4243 trunc_len = (uint16_t) ntohs(cp->trunc_len);
4244 if (trunc_len > limit) {
4247 /* now the chunks themselves */
4248 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4249 desc.chunk_type = ch->chunk_type;
4250 /* get amount we need to move */
4251 at = ntohs(ch->chunk_length);
4252 if (at < sizeof(struct sctp_chunkhdr)) {
4253 /* corrupt chunk, maybe at the end? */
4254 SCTP_STAT_INCR(sctps_pdrpcrupt);
4257 if (trunc_len == 0) {
4258 /* we are supposed to have all of it */
4260 /* corrupt skip it */
4261 SCTP_STAT_INCR(sctps_pdrpcrupt);
4265 /* is there enough of it left ? */
4266 if (desc.chunk_type == SCTP_DATA) {
4267 if (chlen < (sizeof(struct sctp_data_chunk) +
4268 sizeof(desc.data_bytes))) {
4272 if (chlen < sizeof(struct sctp_chunkhdr)) {
4277 if (desc.chunk_type == SCTP_DATA) {
4278 /* can we get out the tsn? */
4279 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4280 SCTP_STAT_INCR(sctps_pdrpmbda);
4282 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4284 struct sctp_data_chunk *dcp;
4288 dcp = (struct sctp_data_chunk *)ch;
4289 ddp = (uint8_t *) (dcp + 1);
4290 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4291 desc.data_bytes[iii] = ddp[iii];
4293 desc.tsn_ifany = dcp->dp.tsn;
4295 /* nope we are done. */
4296 SCTP_STAT_INCR(sctps_pdrpnedat);
4300 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4301 SCTP_STAT_INCR(sctps_pdrpmbct);
4304 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4305 SCTP_STAT_INCR(sctps_pdrppdbrk);
4308 if (SCTP_SIZE32(at) > chlen) {
4311 chlen -= SCTP_SIZE32(at);
4312 if (chlen < sizeof(struct sctp_chunkhdr)) {
4313 /* done, none left */
4316 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4318 /* Now update any rwnd --- possibly */
4319 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4320 /* From a peer, we get a rwnd report */
4323 SCTP_STAT_INCR(sctps_pdrpfehos);
4325 bottle_bw = ntohl(cp->bottle_bw);
4326 on_queue = ntohl(cp->current_onq);
4327 if (bottle_bw && on_queue) {
4328 /* a rwnd report is in here */
4329 if (bottle_bw > on_queue)
4330 a_rwnd = bottle_bw - on_queue;
4335 stcb->asoc.peers_rwnd = 0;
4337 if (a_rwnd > stcb->asoc.total_flight) {
4338 stcb->asoc.peers_rwnd =
4339 a_rwnd - stcb->asoc.total_flight;
4341 stcb->asoc.peers_rwnd = 0;
4343 if (stcb->asoc.peers_rwnd <
4344 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4345 /* SWS sender side engages */
4346 stcb->asoc.peers_rwnd = 0;
4351 SCTP_STAT_INCR(sctps_pdrpfmbox);
4354 /* now middle boxes in sat networks get a cwnd bump */
4355 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4356 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4357 (stcb->asoc.sat_network)) {
4359 * This is debateable but for sat networks it makes sense
4360 * Note if a T3 timer has went off, we will prohibit any
4361 * changes to cwnd until we exit the t3 loss recovery.
4363 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4364 net, cp, &bottle_bw, &on_queue);
4369 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4370 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4371 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4372 * length of the complete packet outputs: - length: modified to remaining
4373 * length after control processing - netp: modified to new sctp_nets after
4374 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4375 * bad packet,...) otherwise return the tcb for this packet
4378 __attribute__((noinline))
4380 static struct sctp_tcb *
4381 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4382 struct sockaddr *src, struct sockaddr *dst,
4383 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4384 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4385 uint8_t use_mflowid, uint32_t mflowid,
4386 uint32_t vrf_id, uint16_t port)
4388 struct sctp_association *asoc;
4390 int num_chunks = 0; /* number of control chunks processed */
4391 uint32_t chk_length;
4393 int abort_no_unlock = 0;
4397 * How big should this be, and should it be alloc'd? Lets try the
4398 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4399 * until we get into jumbo grams and such..
4401 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4402 struct sctp_tcb *locked_tcb = stcb;
4404 uint32_t auth_offset = 0, auth_len = 0;
4405 int auth_skipped = 0;
4408 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4413 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4414 iphlen, *offset, length, (void *)stcb);
4416 /* validate chunk header length... */
4417 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4418 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4419 ntohs(ch->chunk_length));
4421 SCTP_TCB_UNLOCK(locked_tcb);
4426 * validate the verification tag
4428 vtag_in = ntohl(sh->v_tag);
4431 SCTP_TCB_LOCK_ASSERT(locked_tcb);
4433 if (ch->chunk_type == SCTP_INITIATION) {
4434 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4435 ntohs(ch->chunk_length), vtag_in);
4437 /* protocol error- silently discard... */
4438 SCTP_STAT_INCR(sctps_badvtag);
4440 SCTP_TCB_UNLOCK(locked_tcb);
4444 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4446 * If there is no stcb, skip the AUTH chunk and process
4447 * later after a stcb is found (to validate the lookup was
4450 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4452 !SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4453 /* save this chunk for later processing */
4455 auth_offset = *offset;
4456 auth_len = ntohs(ch->chunk_length);
4458 /* (temporarily) move past this chunk */
4459 *offset += SCTP_SIZE32(auth_len);
4460 if (*offset >= length) {
4461 /* no more data left in the mbuf chain */
4464 SCTP_TCB_UNLOCK(locked_tcb);
4468 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4469 sizeof(struct sctp_chunkhdr), chunk_buf);
4475 SCTP_TCB_UNLOCK(locked_tcb);
4479 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4480 goto process_control_chunks;
4483 * first check if it's an ASCONF with an unknown src addr we
4484 * need to look inside to find the association
4486 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4487 struct sctp_chunkhdr *asconf_ch = ch;
4488 uint32_t asconf_offset = 0, asconf_len = 0;
4490 /* inp's refcount may be reduced */
4491 SCTP_INP_INCR_REF(inp);
4493 asconf_offset = *offset;
4495 asconf_len = ntohs(asconf_ch->chunk_length);
4496 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4498 stcb = sctp_findassociation_ep_asconf(m,
4501 sh, &inp, netp, vrf_id);
4504 asconf_offset += SCTP_SIZE32(asconf_len);
4505 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4506 sizeof(struct sctp_chunkhdr), chunk_buf);
4507 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4510 * reduce inp's refcount if not reduced in
4511 * sctp_findassociation_ep_asconf().
4513 SCTP_INP_DECR_REF(inp);
4518 /* now go back and verify any auth chunk to be sure */
4519 if (auth_skipped && (stcb != NULL)) {
4520 struct sctp_auth_chunk *auth;
4522 auth = (struct sctp_auth_chunk *)
4523 sctp_m_getptr(m, auth_offset,
4524 auth_len, chunk_buf);
4527 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4529 /* auth HMAC failed so dump it */
4532 SCTP_TCB_UNLOCK(locked_tcb);
4536 /* remaining chunks are HMAC checked */
4537 stcb->asoc.authenticated = 1;
4542 /* no association, so it's out of the blue... */
4543 sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp,
4544 use_mflowid, mflowid,
4548 SCTP_TCB_UNLOCK(locked_tcb);
4553 /* ABORT and SHUTDOWN can use either v_tag... */
4554 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4555 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4556 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4557 if ((vtag_in == asoc->my_vtag) ||
4558 ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
4559 (vtag_in == asoc->peer_vtag))) {
4562 /* drop this packet... */
4563 SCTP_STAT_INCR(sctps_badvtag);
4565 SCTP_TCB_UNLOCK(locked_tcb);
4569 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4570 if (vtag_in != asoc->my_vtag) {
4572 * this could be a stale SHUTDOWN-ACK or the
4573 * peer never got the SHUTDOWN-COMPLETE and
4574 * is still hung; we have started a new asoc
4575 * but it won't complete until the shutdown
4579 SCTP_TCB_UNLOCK(locked_tcb);
4581 sctp_handle_ootb(m, iphlen, *offset, src, dst,
4583 use_mflowid, mflowid,
4588 /* for all other chunks, vtag must match */
4589 if (vtag_in != asoc->my_vtag) {
4590 /* invalid vtag... */
4591 SCTPDBG(SCTP_DEBUG_INPUT3,
4592 "invalid vtag: %xh, expect %xh\n",
4593 vtag_in, asoc->my_vtag);
4594 SCTP_STAT_INCR(sctps_badvtag);
4596 SCTP_TCB_UNLOCK(locked_tcb);
4602 } /* end if !SCTP_COOKIE_ECHO */
4604 * process all control chunks...
4606 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4607 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4608 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4609 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4610 /* implied cookie-ack.. we must have lost the ack */
4611 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4612 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4613 stcb->asoc.overall_error_count,
4615 SCTP_FROM_SCTP_INPUT,
4618 stcb->asoc.overall_error_count = 0;
4619 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4622 process_control_chunks:
4623 while (IS_SCTP_CONTROL(ch)) {
4624 /* validate chunk length */
4625 chk_length = ntohs(ch->chunk_length);
4626 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4627 ch->chunk_type, chk_length);
4628 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4629 if (chk_length < sizeof(*ch) ||
4630 (*offset + (int)chk_length) > length) {
4633 SCTP_TCB_UNLOCK(locked_tcb);
4637 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4639 * INIT-ACK only gets the init ack "header" portion only
4640 * because we don't have to process the peer's COOKIE. All
4641 * others get a complete chunk.
4643 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4644 (ch->chunk_type == SCTP_INITIATION)) {
4645 /* get an init-ack chunk */
4646 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4647 sizeof(struct sctp_init_ack_chunk), chunk_buf);
4651 SCTP_TCB_UNLOCK(locked_tcb);
4656 /* For cookies and all other chunks. */
4657 if (chk_length > sizeof(chunk_buf)) {
4659 * use just the size of the chunk buffer so
4660 * the front part of our chunks fit in
4661 * contiguous space up to the chunk buffer
4662 * size (508 bytes). For chunks that need to
4663 * get more than that they must use the
4664 * sctp_m_getptr() function or other means
4665 * (e.g. know how to parse mbuf chains).
4666 * Cookies do this already.
4668 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4669 (sizeof(chunk_buf) - 4),
4674 SCTP_TCB_UNLOCK(locked_tcb);
4679 /* We can fit it all */
4680 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4681 chk_length, chunk_buf);
4683 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4686 SCTP_TCB_UNLOCK(locked_tcb);
4693 /* Save off the last place we got a control from */
4695 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4697 * allow last_control to be NULL if
4698 * ASCONF... ASCONF processing will find the
4701 if ((netp != NULL) && (*netp != NULL))
4702 stcb->asoc.last_control_chunk_from = *netp;
4705 #ifdef SCTP_AUDITING_ENABLED
4706 sctp_audit_log(0xB0, ch->chunk_type);
4709 /* check to see if this chunk required auth, but isn't */
4710 if ((stcb != NULL) &&
4711 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
4712 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4713 !stcb->asoc.authenticated) {
4714 /* "silently" ignore */
4715 SCTP_STAT_INCR(sctps_recvauthmissing);
4718 switch (ch->chunk_type) {
4719 case SCTP_INITIATION:
4720 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4721 /* The INIT chunk must be the only chunk. */
4722 if ((num_chunks > 1) ||
4723 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4724 sctp_abort_association(inp, stcb, m, iphlen,
4726 use_mflowid, mflowid,
4731 /* Honor our resource limit. */
4732 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4733 struct mbuf *op_err;
4735 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
4736 sctp_abort_association(inp, stcb, m, iphlen,
4737 src, dst, sh, op_err,
4738 use_mflowid, mflowid,
4743 sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4744 (struct sctp_init_chunk *)ch, inp,
4745 stcb, &abort_no_unlock,
4746 use_mflowid, mflowid,
4749 if ((!abort_no_unlock) && (locked_tcb)) {
4750 SCTP_TCB_UNLOCK(locked_tcb);
4754 case SCTP_PAD_CHUNK:
4756 case SCTP_INITIATION_ACK:
4757 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4758 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4759 /* We are not interested anymore */
4760 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4763 if (locked_tcb != stcb) {
4765 SCTP_TCB_UNLOCK(locked_tcb);
4769 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4770 so = SCTP_INP_SO(inp);
4771 atomic_add_int(&stcb->asoc.refcnt, 1);
4772 SCTP_TCB_UNLOCK(stcb);
4773 SCTP_SOCKET_LOCK(so, 1);
4774 SCTP_TCB_LOCK(stcb);
4775 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4777 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4778 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4779 SCTP_SOCKET_UNLOCK(so, 1);
4785 /* The INIT-ACK chunk must be the only chunk. */
4786 if ((num_chunks > 1) ||
4787 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4790 SCTP_TCB_UNLOCK(locked_tcb);
4794 if ((netp) && (*netp)) {
4795 ret = sctp_handle_init_ack(m, iphlen, *offset,
4797 (struct sctp_init_ack_chunk *)ch,
4800 use_mflowid, mflowid,
4806 if (abort_no_unlock) {
4810 * Special case, I must call the output routine to
4811 * get the cookie echoed
4813 if ((stcb != NULL) && (ret == 0)) {
4814 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4817 SCTP_TCB_UNLOCK(locked_tcb);
4821 case SCTP_SELECTIVE_ACK:
4823 struct sctp_sack_chunk *sack;
4825 uint32_t a_rwnd, cum_ack;
4826 uint16_t num_seg, num_dup;
4828 int offset_seg, offset_dup;
4830 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4831 SCTP_STAT_INCR(sctps_recvsacks);
4833 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4836 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4837 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4840 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4842 * If we have sent a shutdown-ack, we will pay no
4843 * attention to a sack sent in to us since
4844 * we don't care anymore.
4848 sack = (struct sctp_sack_chunk *)ch;
4849 flags = ch->chunk_flags;
4850 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4851 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4852 num_dup = ntohs(sack->sack.num_dup_tsns);
4853 a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
4854 if (sizeof(struct sctp_sack_chunk) +
4855 num_seg * sizeof(struct sctp_gap_ack_block) +
4856 num_dup * sizeof(uint32_t) != chk_length) {
4857 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4860 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4861 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4862 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4863 cum_ack, num_seg, a_rwnd);
4864 stcb->asoc.seen_a_sack_this_pkt = 1;
4865 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4867 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4868 (stcb->asoc.saw_sack_with_frags == 0) &&
4869 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4870 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4873 * We have a SIMPLE sack having no
4874 * prior segments and data on sent
4875 * queue to be acked.. Use the
4876 * faster path sack processing. We
4877 * also allow window update sacks
4878 * with no missing segments to go
4881 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
4884 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4885 num_seg, 0, num_dup, &abort_now, flags,
4886 cum_ack, a_rwnd, ecne_seen);
4889 /* ABORT signal from sack processing */
4893 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4894 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4895 (stcb->asoc.stream_queue_cnt == 0)) {
4896 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4901 * EY - nr_sack: If the received chunk is an
4904 case SCTP_NR_SELECTIVE_ACK:
4906 struct sctp_nr_sack_chunk *nr_sack;
4908 uint32_t a_rwnd, cum_ack;
4909 uint16_t num_seg, num_nr_seg, num_dup;
4911 int offset_seg, offset_dup;
4913 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
4914 SCTP_STAT_INCR(sctps_recvsacks);
4916 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
4919 if ((stcb->asoc.sctp_nr_sack_on_off == 0) ||
4920 (stcb->asoc.peer_supports_nr_sack == 0)) {
4923 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4924 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
4927 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4929 * If we have sent a shutdown-ack, we will pay no
4930 * attention to a sack sent in to us since
4931 * we don't care anymore.
4935 nr_sack = (struct sctp_nr_sack_chunk *)ch;
4936 flags = ch->chunk_flags;
4937 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4938 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4939 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4940 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4941 a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd);
4942 if (sizeof(struct sctp_nr_sack_chunk) +
4943 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4944 num_dup * sizeof(uint32_t) != chk_length) {
4945 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4948 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4949 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4950 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4951 cum_ack, num_seg, a_rwnd);
4952 stcb->asoc.seen_a_sack_this_pkt = 1;
4953 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4954 (num_seg == 0) && (num_nr_seg == 0) &&
4955 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4956 (stcb->asoc.saw_sack_with_frags == 0) &&
4957 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4958 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
4960 * We have a SIMPLE sack having no
4961 * prior segments and data on sent
4962 * queue to be acked. Use the faster
4963 * path sack processing. We also
4964 * allow window update sacks with no
4965 * missing segments to go this way
4968 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
4969 &abort_now, ecne_seen);
4972 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4973 num_seg, num_nr_seg, num_dup, &abort_now, flags,
4974 cum_ack, a_rwnd, ecne_seen);
4977 /* ABORT signal from sack processing */
4981 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4982 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4983 (stcb->asoc.stream_queue_cnt == 0)) {
4984 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4989 case SCTP_HEARTBEAT_REQUEST:
4990 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
4991 if ((stcb) && netp && *netp) {
4992 SCTP_STAT_INCR(sctps_recvheartbeat);
4993 sctp_send_heartbeat_ack(stcb, m, *offset,
4996 /* He's alive so give him credit */
4997 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4998 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4999 stcb->asoc.overall_error_count,
5001 SCTP_FROM_SCTP_INPUT,
5004 stcb->asoc.overall_error_count = 0;
5007 case SCTP_HEARTBEAT_ACK:
5008 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
5009 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5013 SCTP_TCB_UNLOCK(locked_tcb);
5017 /* He's alive so give him credit */
5018 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5019 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5020 stcb->asoc.overall_error_count,
5022 SCTP_FROM_SCTP_INPUT,
5025 stcb->asoc.overall_error_count = 0;
5026 SCTP_STAT_INCR(sctps_recvheartbeatack);
5028 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5031 case SCTP_ABORT_ASSOCIATION:
5032 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5034 if ((stcb) && netp && *netp)
5035 sctp_handle_abort((struct sctp_abort_chunk *)ch,
5041 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5043 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5046 SCTP_TCB_UNLOCK(locked_tcb);
5050 if (netp && *netp) {
5053 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5054 stcb, *netp, &abort_flag);
5061 case SCTP_SHUTDOWN_ACK:
5062 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", (void *)stcb);
5063 if ((stcb) && (netp) && (*netp))
5064 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5069 case SCTP_OPERATION_ERROR:
5070 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
5071 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
5076 case SCTP_COOKIE_ECHO:
5077 SCTPDBG(SCTP_DEBUG_INPUT3,
5078 "SCTP_COOKIE-ECHO, stcb %p\n", (void *)stcb);
5079 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5082 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5083 /* We are not interested anymore */
5086 SCTP_TCB_UNLOCK(stcb);
5093 * First are we accepting? We do this again here
5094 * since it is possible that a previous endpoint WAS
5095 * listening responded to a INIT-ACK and then
5096 * closed. We opened and bound.. and are now no
5100 if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
5101 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5102 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5103 struct mbuf *op_err;
5105 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
5106 sctp_abort_association(inp, stcb, m, iphlen,
5107 src, dst, sh, op_err,
5108 use_mflowid, mflowid,
5114 struct mbuf *ret_buf;
5115 struct sctp_inpcb *linp;
5124 SCTP_ASOC_CREATE_LOCK(linp);
5125 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5126 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5127 SCTP_ASOC_CREATE_UNLOCK(linp);
5133 sctp_handle_cookie_echo(m, iphlen,
5137 (struct sctp_cookie_echo_chunk *)ch,
5151 SCTP_ASOC_CREATE_UNLOCK(linp);
5153 if (ret_buf == NULL) {
5155 SCTP_TCB_UNLOCK(locked_tcb);
5157 SCTPDBG(SCTP_DEBUG_INPUT3,
5158 "GAK, null buffer\n");
5162 /* if AUTH skipped, see if it verified... */
5167 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5169 * Restart the timer if we have
5172 struct sctp_tmit_chunk *chk;
5174 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5175 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5179 case SCTP_COOKIE_ACK:
5180 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", (void *)stcb);
5181 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5183 SCTP_TCB_UNLOCK(locked_tcb);
5187 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5188 /* We are not interested anymore */
5189 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5192 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5193 so = SCTP_INP_SO(inp);
5194 atomic_add_int(&stcb->asoc.refcnt, 1);
5195 SCTP_TCB_UNLOCK(stcb);
5196 SCTP_SOCKET_LOCK(so, 1);
5197 SCTP_TCB_LOCK(stcb);
5198 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5200 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
5201 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5202 SCTP_SOCKET_UNLOCK(so, 1);
5208 /* He's alive so give him credit */
5209 if ((stcb) && netp && *netp) {
5210 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5211 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5212 stcb->asoc.overall_error_count,
5214 SCTP_FROM_SCTP_INPUT,
5217 stcb->asoc.overall_error_count = 0;
5218 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5222 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5223 /* He's alive so give him credit */
5224 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5227 SCTP_TCB_UNLOCK(locked_tcb);
5233 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5234 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5235 stcb->asoc.overall_error_count,
5237 SCTP_FROM_SCTP_INPUT,
5240 stcb->asoc.overall_error_count = 0;
5241 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5247 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5248 /* He's alive so give him credit */
5249 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5252 SCTP_TCB_UNLOCK(locked_tcb);
5258 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5259 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5260 stcb->asoc.overall_error_count,
5262 SCTP_FROM_SCTP_INPUT,
5265 stcb->asoc.overall_error_count = 0;
5266 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5269 case SCTP_SHUTDOWN_COMPLETE:
5270 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", (void *)stcb);
5271 /* must be first and only chunk */
5272 if ((num_chunks > 1) ||
5273 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5276 SCTP_TCB_UNLOCK(locked_tcb);
5280 if ((stcb) && netp && *netp) {
5281 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5288 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5289 /* He's alive so give him credit */
5291 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5292 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5293 stcb->asoc.overall_error_count,
5295 SCTP_FROM_SCTP_INPUT,
5298 stcb->asoc.overall_error_count = 0;
5299 sctp_handle_asconf(m, *offset, src,
5300 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5304 case SCTP_ASCONF_ACK:
5305 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5306 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5309 SCTP_TCB_UNLOCK(locked_tcb);
5314 if ((stcb) && netp && *netp) {
5315 /* He's alive so give him credit */
5316 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5317 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5318 stcb->asoc.overall_error_count,
5320 SCTP_FROM_SCTP_INPUT,
5323 stcb->asoc.overall_error_count = 0;
5324 sctp_handle_asconf_ack(m, *offset,
5325 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5326 if (abort_no_unlock)
5330 case SCTP_FORWARD_CUM_TSN:
5331 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5332 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5335 SCTP_TCB_UNLOCK(locked_tcb);
5340 /* He's alive so give him credit */
5344 stcb->asoc.overall_error_count = 0;
5345 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5346 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5347 stcb->asoc.overall_error_count,
5349 SCTP_FROM_SCTP_INPUT,
5353 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5354 /* We are not interested anymore */
5355 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5356 so = SCTP_INP_SO(inp);
5357 atomic_add_int(&stcb->asoc.refcnt, 1);
5358 SCTP_TCB_UNLOCK(stcb);
5359 SCTP_SOCKET_LOCK(so, 1);
5360 SCTP_TCB_LOCK(stcb);
5361 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5363 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
5364 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5365 SCTP_SOCKET_UNLOCK(so, 1);
5370 sctp_handle_forward_tsn(stcb,
5371 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5376 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5377 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5378 stcb->asoc.overall_error_count,
5380 SCTP_FROM_SCTP_INPUT,
5383 stcb->asoc.overall_error_count = 0;
5388 case SCTP_STREAM_RESET:
5389 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5390 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5393 SCTP_TCB_UNLOCK(locked_tcb);
5398 if (stcb->asoc.peer_supports_strreset == 0) {
5400 * hmm, peer should have announced this, but
5401 * we will turn it on since he is sending us
5404 stcb->asoc.peer_supports_strreset = 1;
5406 if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5407 /* stop processing */
5412 case SCTP_PACKET_DROPPED:
5413 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5414 /* re-get it all please */
5415 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5418 SCTP_TCB_UNLOCK(locked_tcb);
5423 if (ch && (stcb) && netp && (*netp)) {
5424 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5426 min(chk_length, (sizeof(chunk_buf) - 4)));
5431 case SCTP_AUTHENTICATION:
5432 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5433 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
5437 /* save the first AUTH for later processing */
5438 if (auth_skipped == 0) {
5439 auth_offset = *offset;
5440 auth_len = chk_length;
5443 /* skip this chunk (temporarily) */
5446 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5447 (chk_length > (sizeof(struct sctp_auth_chunk) +
5448 SCTP_AUTH_DIGEST_LEN_MAX))) {
5451 SCTP_TCB_UNLOCK(locked_tcb);
5456 if (got_auth == 1) {
5457 /* skip this chunk... it's already auth'd */
5461 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5463 /* auth HMAC failed so dump the packet */
5467 /* remaining chunks are HMAC checked */
5468 stcb->asoc.authenticated = 1;
5474 /* it's an unknown chunk! */
5475 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5477 struct sctp_paramhdr *phd;
5479 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
5480 0, M_DONTWAIT, 1, MT_DATA);
5482 phd = mtod(mm, struct sctp_paramhdr *);
5484 * We cheat and use param type since
5485 * we did not bother to define a
5486 * error cause struct. They are the
5487 * same basic format with different
5490 phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5491 phd->param_length = htons(chk_length + sizeof(*phd));
5492 SCTP_BUF_LEN(mm) = sizeof(*phd);
5493 SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, chk_length, M_DONTWAIT);
5494 if (SCTP_BUF_NEXT(mm)) {
5495 if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(mm), SCTP_SIZE32(chk_length) - chk_length, NULL)) {
5498 #ifdef SCTP_MBUF_LOGGING
5499 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5502 for (mat = SCTP_BUF_NEXT(mm); mat; mat = SCTP_BUF_NEXT(mat)) {
5503 if (SCTP_BUF_IS_EXTENDED(mat)) {
5504 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5509 sctp_queue_op_err(stcb, mm);
5516 if ((ch->chunk_type & 0x80) == 0) {
5517 /* discard this packet */
5520 } /* else skip this bad chunk and continue... */
5522 } /* switch (ch->chunk_type) */
5526 /* get the next chunk */
5527 *offset += SCTP_SIZE32(chk_length);
5528 if (*offset >= length) {
5529 /* no more data left in the mbuf chain */
5532 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5533 sizeof(struct sctp_chunkhdr), chunk_buf);
5536 SCTP_TCB_UNLOCK(locked_tcb);
5543 if (asconf_cnt > 0 && stcb != NULL) {
5544 sctp_send_asconf_ack(stcb);
5552 __attribute__((noinline))
5555 sctp_validate_no_locks(struct sctp_inpcb *inp)
5557 struct sctp_tcb *lstcb;
5559 LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) {
5560 if (mtx_owned(&lstcb->tcb_mtx)) {
5561 panic("Own lock on stcb at return from input");
5564 if (mtx_owned(&inp->inp_create_mtx)) {
5565 panic("Own create lock on inp");
5567 if (mtx_owned(&inp->inp_mtx)) {
5568 panic("Own inp lock on inp");
5575 * common input chunk processing (v4 and v6)
5578 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5579 struct sockaddr *src, struct sockaddr *dst,
5580 struct sctphdr *sh, struct sctp_chunkhdr *ch,
5581 #if !defined(SCTP_WITH_NO_CSUM)
5582 uint8_t compute_crc,
5585 uint8_t use_mflowid, uint32_t mflowid,
5586 uint32_t vrf_id, uint16_t port)
5589 int fwd_tsn_seen = 0, data_processed = 0;
5590 struct mbuf *m = *mm;
5592 int cnt_ctrl_ready = 0;
5593 struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5594 struct sctp_tcb *stcb = NULL;
5595 struct sctp_nets *net = NULL;
5597 SCTP_STAT_INCR(sctps_recvdatagrams);
5598 #ifdef SCTP_AUDITING_ENABLED
5599 sctp_audit_log(0xE0, 1);
5600 sctp_auditing(0, inp, stcb, net);
5602 #if !defined(SCTP_WITH_NO_CSUM)
5603 if (compute_crc != 0) {
5604 uint32_t check, calc_check;
5606 check = sh->checksum;
5608 calc_check = sctp_calculate_cksum(m, iphlen);
5609 sh->checksum = check;
5610 if (calc_check != check) {
5611 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5612 calc_check, check, (void *)m, length, iphlen);
5613 stcb = sctp_findassociation_addr(m, offset, src, dst,
5614 sh, ch, &inp, &net, vrf_id);
5615 if ((net != NULL) && (port != 0)) {
5616 if (net->port == 0) {
5617 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5621 if ((net != NULL) && (use_mflowid != 0)) {
5622 net->flowid = mflowid;
5627 if ((inp != NULL) && (stcb != NULL)) {
5628 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5629 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5630 } else if ((inp != NULL) && (stcb == NULL)) {
5633 SCTP_STAT_INCR(sctps_badsum);
5634 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5639 /* Destination port of 0 is illegal, based on RFC4960. */
5640 if (sh->dest_port == 0) {
5641 SCTP_STAT_INCR(sctps_hdrops);
5644 stcb = sctp_findassociation_addr(m, offset, src, dst,
5645 sh, ch, &inp, &net, vrf_id);
5646 if ((net != NULL) && (port != 0)) {
5647 if (net->port == 0) {
5648 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5652 if ((net != NULL) && (use_mflowid != 0)) {
5653 net->flowid = mflowid;
5659 SCTP_STAT_INCR(sctps_noport);
5660 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5663 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5664 sctp_send_shutdown_complete2(src, dst, sh,
5665 use_mflowid, mflowid,
5669 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5672 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5673 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5674 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5675 (ch->chunk_type != SCTP_INIT))) {
5676 sctp_send_abort(m, iphlen, src, dst,
5678 use_mflowid, mflowid,
5683 } else if (stcb == NULL) {
5688 * I very much doubt any of the IPSEC stuff will work but I have no
5689 * idea, so I will leave it in place.
5692 switch (dst->sa_family) {
5695 if (ipsec4_in_reject(m, &inp->ip_inp.inp)) {
5696 MODULE_GLOBAL(ipsec4stat).in_polvio++;
5697 SCTP_STAT_INCR(sctps_hdrops);
5704 if (ipsec6_in_reject(m, &inp->ip_inp.inp)) {
5705 MODULE_GLOBAL(ipsec6stat).in_polvio++;
5706 SCTP_STAT_INCR(sctps_hdrops);
5716 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5717 (void *)m, iphlen, offset, length, (void *)stcb);
5719 /* always clear this before beginning a packet */
5720 stcb->asoc.authenticated = 0;
5721 stcb->asoc.seen_a_sack_this_pkt = 0;
5722 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5723 (void *)stcb, stcb->asoc.state);
5725 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5726 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5728 * If we hit here, we had a ref count
5729 * up when the assoc was aborted and the
5730 * timer is clearing out the assoc, we should
5731 * NOT respond to any packet.. its OOTB.
5733 SCTP_TCB_UNLOCK(stcb);
5735 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp,
5736 use_mflowid, mflowid,
5741 if (IS_SCTP_CONTROL(ch)) {
5742 /* process the control portion of the SCTP packet */
5743 /* sa_ignore NO_NULL_CHK */
5744 stcb = sctp_process_control(m, iphlen, &offset, length,
5746 inp, stcb, &net, &fwd_tsn_seen,
5747 use_mflowid, mflowid,
5751 * This covers us if the cookie-echo was there and
5752 * it changes our INP.
5754 inp = stcb->sctp_ep;
5755 if ((net) && (port)) {
5756 if (net->port == 0) {
5757 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5764 * no control chunks, so pre-process DATA chunks (these
5765 * checks are taken care of by control processing)
5769 * if DATA only packet, and auth is required, then punt...
5770 * can't have authenticated without any AUTH (control)
5773 if ((stcb != NULL) &&
5774 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5775 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5776 /* "silently" ignore */
5777 SCTP_STAT_INCR(sctps_recvauthmissing);
5781 /* out of the blue DATA chunk */
5782 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp,
5783 use_mflowid, mflowid,
5787 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5788 /* v_tag mismatch! */
5789 SCTP_STAT_INCR(sctps_badvtag);
5796 * no valid TCB for this packet, or we found it's a bad
5797 * packet while processing control, or we're done with this
5798 * packet (done or skip rest of data), so we drop it...
5803 * DATA chunk processing
5805 /* plow through the data chunks while length > offset */
5808 * Rest should be DATA only. Check authentication state if AUTH for
5811 if ((length > offset) &&
5813 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5814 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5815 !stcb->asoc.authenticated) {
5816 /* "silently" ignore */
5817 SCTP_STAT_INCR(sctps_recvauthmissing);
5818 SCTPDBG(SCTP_DEBUG_AUTH1,
5819 "Data chunk requires AUTH, skipped\n");
5822 if (length > offset) {
5826 * First check to make sure our state is correct. We would
5827 * not get here unless we really did have a tag, so we don't
5828 * abort if this happens, just dump the chunk silently.
5830 switch (SCTP_GET_STATE(&stcb->asoc)) {
5831 case SCTP_STATE_COOKIE_ECHOED:
5833 * we consider data with valid tags in this state
5834 * shows us the cookie-ack was lost. Imply it was
5837 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5838 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5839 stcb->asoc.overall_error_count,
5841 SCTP_FROM_SCTP_INPUT,
5844 stcb->asoc.overall_error_count = 0;
5845 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5847 case SCTP_STATE_COOKIE_WAIT:
5849 * We consider OOTB any data sent during asoc setup.
5851 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp,
5852 use_mflowid, mflowid,
5855 /* sa_ignore NOTREACHED */
5857 case SCTP_STATE_EMPTY: /* should not happen */
5858 case SCTP_STATE_INUSE: /* should not happen */
5859 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5860 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5863 /* sa_ignore NOTREACHED */
5865 case SCTP_STATE_OPEN:
5866 case SCTP_STATE_SHUTDOWN_SENT:
5869 /* plow through the data chunks while length > offset */
5870 retval = sctp_process_data(mm, iphlen, &offset, length,
5872 inp, stcb, net, &high_tsn,
5873 use_mflowid, mflowid,
5877 * The association aborted, NO UNLOCK needed since
5878 * the association is destroyed.
5885 * Anything important needs to have been m_copy'ed in
5889 /* take care of ecn */
5890 if ((data_processed == 1) &&
5891 (stcb->asoc.ecn_allowed == 1) &&
5892 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5893 /* Yep, we need to add a ECNE */
5894 sctp_send_ecn_echo(stcb, net, high_tsn);
5896 if ((data_processed == 0) && (fwd_tsn_seen)) {
5898 uint32_t highest_tsn;
5900 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5901 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5903 highest_tsn = stcb->asoc.highest_tsn_inside_map;
5905 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
5906 stcb->asoc.send_sack = 1;
5907 sctp_sack_check(stcb, was_a_gap);
5908 } else if (fwd_tsn_seen) {
5909 stcb->asoc.send_sack = 1;
5911 /* trigger send of any chunks in queue... */
5913 #ifdef SCTP_AUDITING_ENABLED
5914 sctp_audit_log(0xE0, 2);
5915 sctp_auditing(1, inp, stcb, net);
5917 SCTPDBG(SCTP_DEBUG_INPUT1,
5918 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5919 stcb->asoc.peers_rwnd,
5920 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5921 stcb->asoc.total_flight);
5922 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5923 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5924 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5926 if (cnt_ctrl_ready ||
5928 (stcb->asoc.peers_rwnd > 0 ||
5929 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5930 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5931 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5932 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5934 #ifdef SCTP_AUDITING_ENABLED
5935 sctp_audit_log(0xE0, 3);
5936 sctp_auditing(2, inp, stcb, net);
5940 SCTP_TCB_UNLOCK(stcb);
5942 if (inp_decr != NULL) {
5943 /* reduce ref-count */
5944 SCTP_INP_WLOCK(inp_decr);
5945 SCTP_INP_DECR_REF(inp_decr);
5946 SCTP_INP_WUNLOCK(inp_decr);
5950 sctp_validate_no_locks(inp);
5958 sctp_print_mbuf_chain(struct mbuf *m)
5960 for (; m; m = SCTP_BUF_NEXT(m)) {
5961 SCTP_PRINTF("%p: m_len = %ld\n", (void *)m, SCTP_BUF_LEN(m));
5962 if (SCTP_BUF_IS_EXTENDED(m))
5963 SCTP_PRINTF("%p: extend_size = %d\n", (void *)m, SCTP_BUF_EXTEND_SIZE(m));
5971 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5975 uint32_t vrf_id = 0;
5977 struct sockaddr_in src, dst;
5980 struct sctp_chunkhdr *ch;
5983 #if !defined(SCTP_WITH_NO_CSUM)
5984 uint8_t compute_crc;
5988 uint8_t use_mflowid;
5991 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
5992 SCTP_RELEASE_PKT(i_pak);
5995 m = SCTP_HEADER_TO_CHAIN(i_pak);
5996 #ifdef SCTP_MBUF_LOGGING
5997 /* Log in any input mbufs */
5998 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6001 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
6002 if (SCTP_BUF_IS_EXTENDED(mat)) {
6003 sctp_log_mb(mat, SCTP_MBUF_INPUT);
6008 #ifdef SCTP_PACKET_LOGGING
6009 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
6013 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6014 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
6016 if_name(m->m_pkthdr.rcvif),
6017 m->m_pkthdr.csum_flags);
6018 if (m->m_flags & M_FLOWID) {
6019 mflowid = m->m_pkthdr.flowid;
6025 SCTP_STAT_INCR(sctps_recvpackets);
6026 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
6027 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
6028 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6029 if (SCTP_BUF_LEN(m) < offset) {
6030 if ((m = m_pullup(m, offset)) == NULL) {
6031 SCTP_STAT_INCR(sctps_hdrops);
6035 ip = mtod(m, struct ip *);
6036 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
6037 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
6038 offset -= sizeof(struct sctp_chunkhdr);
6039 memset(&src, 0, sizeof(struct sockaddr_in));
6040 src.sin_family = AF_INET;
6041 src.sin_len = sizeof(struct sockaddr_in);
6042 src.sin_port = sh->src_port;
6043 src.sin_addr = ip->ip_src;
6044 memset(&dst, 0, sizeof(struct sockaddr_in));
6045 dst.sin_family = AF_INET;
6046 dst.sin_len = sizeof(struct sockaddr_in);
6047 dst.sin_port = sh->dest_port;
6048 dst.sin_addr = ip->ip_dst;
6049 length = ip->ip_len + iphlen;
6050 /* Validate mbuf chain length with IP payload length. */
6051 if (SCTP_HEADER_LEN(m) != length) {
6052 SCTPDBG(SCTP_DEBUG_INPUT1,
6053 "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
6054 SCTP_STAT_INCR(sctps_hdrops);
6057 /* SCTP does not allow broadcasts or multicasts */
6058 if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
6061 if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
6064 ecn_bits = ip->ip_tos;
6065 #if defined(SCTP_WITH_NO_CSUM)
6066 SCTP_STAT_INCR(sctps_recvnocrc);
6068 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
6069 SCTP_STAT_INCR(sctps_recvhwcrc);
6072 SCTP_STAT_INCR(sctps_recvswcrc);
6076 sctp_common_input_processing(&m, iphlen, offset, length,
6077 (struct sockaddr *)&src,
6078 (struct sockaddr *)&dst,
6080 #if !defined(SCTP_WITH_NO_CSUM)
6084 use_mflowid, mflowid,
6093 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6094 extern int *sctp_cpuarry;
6099 sctp_input(struct mbuf *m, int off)
6101 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6106 uint32_t flowid, tag;
6109 if (m->m_flags & M_FLOWID) {
6110 flowid = m->m_pkthdr.flowid;
6113 * No flow id built by lower layers fix it so we
6116 offset = off + sizeof(struct sctphdr);
6117 if (SCTP_BUF_LEN(m) < offset) {
6118 if ((m = m_pullup(m, offset)) == NULL) {
6119 SCTP_STAT_INCR(sctps_hdrops);
6123 ip = mtod(m, struct ip *);
6124 sh = (struct sctphdr *)((caddr_t)ip + off);
6125 tag = htonl(sh->v_tag);
6126 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6127 m->m_pkthdr.flowid = flowid;
6128 m->m_flags |= M_FLOWID;
6130 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6131 sctp_queue_to_mcore(m, off, cpu_to_use);
6135 sctp_input_with_port(m, off, 0);