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);
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 panic("Queues are not empty when handling SHUTDOWN-ACK");
1038 /* stop the timer */
1039 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
1040 /* send SHUTDOWN-COMPLETE */
1041 sctp_send_shutdown_complete(stcb, net, 0);
1042 /* notify upper layer protocol */
1043 if (stcb->sctp_socket) {
1044 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1045 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1046 stcb->sctp_socket->so_snd.sb_cc = 0;
1048 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1050 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1051 /* free the TCB but first save off the ep */
1052 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1053 atomic_add_int(&stcb->asoc.refcnt, 1);
1054 SCTP_TCB_UNLOCK(stcb);
1055 SCTP_SOCKET_LOCK(so, 1);
1056 SCTP_TCB_LOCK(stcb);
1057 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1059 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1060 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1061 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1062 SCTP_SOCKET_UNLOCK(so, 1);
1067 * Skip past the param header and then we will find the chunk that caused the
1068 * problem. There are two possiblities ASCONF or FWD-TSN other than that and
1069 * our peer must be broken.
1072 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1073 struct sctp_nets *net)
1075 struct sctp_chunkhdr *chk;
1077 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1078 switch (chk->chunk_type) {
1079 case SCTP_ASCONF_ACK:
1081 sctp_asconf_cleanup(stcb, net);
1083 case SCTP_FORWARD_CUM_TSN:
1084 stcb->asoc.peer_supports_prsctp = 0;
1087 SCTPDBG(SCTP_DEBUG_INPUT2,
1088 "Peer does not support chunk type %d(%x)??\n",
1089 chk->chunk_type, (uint32_t) chk->chunk_type);
1095 * Skip past the param header and then we will find the param that caused the
1096 * problem. There are a number of param's in a ASCONF OR the prsctp param
1097 * these will turn of specific features.
1100 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1102 struct sctp_paramhdr *pbad;
1105 switch (ntohs(pbad->param_type)) {
1107 case SCTP_PRSCTP_SUPPORTED:
1108 stcb->asoc.peer_supports_prsctp = 0;
1110 case SCTP_SUPPORTED_CHUNK_EXT:
1112 /* draft-ietf-tsvwg-addip-sctp */
1113 case SCTP_HAS_NAT_SUPPORT:
1114 stcb->asoc.peer_supports_nat = 0;
1116 case SCTP_ADD_IP_ADDRESS:
1117 case SCTP_DEL_IP_ADDRESS:
1118 case SCTP_SET_PRIM_ADDR:
1119 stcb->asoc.peer_supports_asconf = 0;
1121 case SCTP_SUCCESS_REPORT:
1122 case SCTP_ERROR_CAUSE_IND:
1123 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1124 SCTPDBG(SCTP_DEBUG_INPUT2,
1125 "Turning off ASCONF to this strange peer\n");
1126 stcb->asoc.peer_supports_asconf = 0;
1129 SCTPDBG(SCTP_DEBUG_INPUT2,
1130 "Peer does not support param type %d(%x)??\n",
1131 pbad->param_type, (uint32_t) pbad->param_type);
1137 sctp_handle_error(struct sctp_chunkhdr *ch,
1138 struct sctp_tcb *stcb, struct sctp_nets *net)
1141 struct sctp_paramhdr *phdr;
1142 uint16_t error, error_type;
1144 struct sctp_association *asoc;
1147 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1152 /* parse through all of the errors and process */
1154 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1155 sizeof(struct sctp_chunkhdr));
1156 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1158 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1159 /* Process an Error Cause */
1160 error_type = ntohs(phdr->param_type);
1161 error_len = ntohs(phdr->param_length);
1162 if ((error_len > chklen) || (error_len == 0)) {
1163 /* invalid param length for this param */
1164 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1169 /* report the first error cause */
1172 switch (error_type) {
1173 case SCTP_CAUSE_INVALID_STREAM:
1174 case SCTP_CAUSE_MISSING_PARAM:
1175 case SCTP_CAUSE_INVALID_PARAM:
1176 case SCTP_CAUSE_NO_USER_DATA:
1177 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1180 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1181 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1183 if (sctp_handle_nat_colliding_state(stcb)) {
1187 case SCTP_CAUSE_NAT_MISSING_STATE:
1188 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1190 if (sctp_handle_nat_missing_state(stcb, net)) {
1194 case SCTP_CAUSE_STALE_COOKIE:
1196 * We only act if we have echoed a cookie and are
1199 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1202 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1203 /* Save the time doubled */
1204 asoc->cookie_preserve_req = ntohl(*p) << 1;
1205 asoc->stale_cookie_count++;
1206 if (asoc->stale_cookie_count >
1207 asoc->max_init_times) {
1208 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1209 /* now free the asoc */
1210 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1211 so = SCTP_INP_SO(stcb->sctp_ep);
1212 atomic_add_int(&stcb->asoc.refcnt, 1);
1213 SCTP_TCB_UNLOCK(stcb);
1214 SCTP_SOCKET_LOCK(so, 1);
1215 SCTP_TCB_LOCK(stcb);
1216 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1218 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1219 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1220 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1221 SCTP_SOCKET_UNLOCK(so, 1);
1225 /* blast back to INIT state */
1226 sctp_toss_old_cookies(stcb, &stcb->asoc);
1227 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1228 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1229 sctp_stop_all_cookie_timers(stcb);
1230 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1233 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1235 * Nothing we can do here, we don't do hostname
1236 * addresses so if the peer does not like my IPv6
1237 * (or IPv4 for that matter) it does not matter. If
1238 * they don't support that type of address, they can
1239 * NOT possibly get that packet type... i.e. with no
1240 * IPv6 you can't recieve a IPv6 packet. so we can
1241 * safely ignore this one. If we ever added support
1242 * for HOSTNAME Addresses, then we would need to do
1246 case SCTP_CAUSE_UNRECOG_CHUNK:
1247 sctp_process_unrecog_chunk(stcb, phdr, net);
1249 case SCTP_CAUSE_UNRECOG_PARAM:
1250 sctp_process_unrecog_param(stcb, phdr);
1252 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1254 * We ignore this since the timer will drive out a
1255 * new cookie anyway and there timer will drive us
1256 * to send a SHUTDOWN_COMPLETE. We can't send one
1257 * here since we don't have their tag.
1260 case SCTP_CAUSE_DELETING_LAST_ADDR:
1261 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1262 case SCTP_CAUSE_DELETING_SRC_ADDR:
1264 * We should NOT get these here, but in a
1267 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1270 case SCTP_CAUSE_OUT_OF_RESC:
1272 * And what, pray tell do we do with the fact that
1273 * the peer is out of resources? Not really sure we
1274 * could do anything but abort. I suspect this
1275 * should have came WITH an abort instead of in a
1280 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1284 adjust = SCTP_SIZE32(error_len);
1286 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1288 sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, error, ch, SCTP_SO_NOT_LOCKED);
1293 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1294 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1295 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1296 struct sctp_nets *net, int *abort_no_unlock,
1297 uint8_t use_mflowid, uint32_t mflowid,
1300 struct sctp_init_ack *init_ack;
1301 struct mbuf *op_err;
1303 SCTPDBG(SCTP_DEBUG_INPUT2,
1304 "sctp_handle_init_ack: handling INIT-ACK\n");
1307 SCTPDBG(SCTP_DEBUG_INPUT2,
1308 "sctp_handle_init_ack: TCB is null\n");
1311 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1312 /* Invalid length */
1313 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1314 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1315 src, dst, sh, op_err,
1316 use_mflowid, mflowid,
1318 *abort_no_unlock = 1;
1321 init_ack = &cp->init;
1322 /* validate parameters */
1323 if (init_ack->initiate_tag == 0) {
1324 /* protocol error... send an abort */
1325 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1326 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1327 src, dst, sh, op_err,
1328 use_mflowid, mflowid,
1330 *abort_no_unlock = 1;
1333 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1334 /* protocol error... send an abort */
1335 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1336 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1337 src, dst, sh, op_err,
1338 use_mflowid, mflowid,
1340 *abort_no_unlock = 1;
1343 if (init_ack->num_inbound_streams == 0) {
1344 /* protocol error... send an abort */
1345 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1346 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1347 src, dst, sh, op_err,
1348 use_mflowid, mflowid,
1350 *abort_no_unlock = 1;
1353 if (init_ack->num_outbound_streams == 0) {
1354 /* protocol error... send an abort */
1355 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1356 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1357 src, dst, sh, op_err,
1358 use_mflowid, mflowid,
1360 *abort_no_unlock = 1;
1363 /* process according to association state... */
1364 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1365 case SCTP_STATE_COOKIE_WAIT:
1366 /* this is the expected state for this chunk */
1367 /* process the INIT-ACK parameters */
1368 if (stcb->asoc.primary_destination->dest_state &
1369 SCTP_ADDR_UNCONFIRMED) {
1371 * The primary is where we sent the INIT, we can
1372 * always consider it confirmed when the INIT-ACK is
1373 * returned. Do this before we load addresses
1376 stcb->asoc.primary_destination->dest_state &=
1377 ~SCTP_ADDR_UNCONFIRMED;
1378 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1379 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1381 if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1382 net, abort_no_unlock,
1383 use_mflowid, mflowid,
1385 /* error in parsing parameters */
1388 /* update our state */
1389 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1390 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1392 /* reset the RTO calc */
1393 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1394 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1395 stcb->asoc.overall_error_count,
1397 SCTP_FROM_SCTP_INPUT,
1400 stcb->asoc.overall_error_count = 0;
1401 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1403 * collapse the init timer back in case of a exponential
1406 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1409 * the send at the end of the inbound data processing will
1410 * cause the cookie to be sent
1413 case SCTP_STATE_SHUTDOWN_SENT:
1414 /* incorrect state... discard */
1416 case SCTP_STATE_COOKIE_ECHOED:
1417 /* incorrect state... discard */
1419 case SCTP_STATE_OPEN:
1420 /* incorrect state... discard */
1422 case SCTP_STATE_EMPTY:
1423 case SCTP_STATE_INUSE:
1425 /* incorrect state... discard */
1429 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1433 static struct sctp_tcb *
1434 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1435 struct sockaddr *src, struct sockaddr *dst,
1436 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1437 struct sctp_inpcb *inp, struct sctp_nets **netp,
1438 struct sockaddr *init_src, int *notification,
1439 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1440 uint8_t use_mflowid, uint32_t mflowid,
1441 uint32_t vrf_id, uint16_t port);
1445 * handle a state cookie for an existing association m: input packet mbuf
1446 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1447 * "split" mbuf and the cookie signature does not exist offset: offset into
1448 * mbuf to the cookie-echo chunk
1450 static struct sctp_tcb *
1451 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1452 struct sockaddr *src, struct sockaddr *dst,
1453 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1454 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1455 struct sockaddr *init_src, int *notification,
1456 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1457 uint8_t use_mflowid, uint32_t mflowid,
1458 uint32_t vrf_id, uint16_t port)
1460 struct sctp_association *asoc;
1461 struct sctp_init_chunk *init_cp, init_buf;
1462 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1463 struct sctp_nets *net;
1464 struct mbuf *op_err;
1465 struct sctp_paramhdr *ph;
1466 int init_offset, initack_offset, i;
1472 /* I know that the TCB is non-NULL from the caller */
1474 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1475 if (asoc->cookie_how[how_indx] == 0)
1478 if (how_indx < sizeof(asoc->cookie_how)) {
1479 asoc->cookie_how[how_indx] = 1;
1481 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1482 /* SHUTDOWN came in after sending INIT-ACK */
1483 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1484 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1485 0, M_DONTWAIT, 1, MT_DATA);
1486 if (op_err == NULL) {
1491 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1492 ph = mtod(op_err, struct sctp_paramhdr *);
1493 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
1494 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1495 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1496 use_mflowid, mflowid,
1498 if (how_indx < sizeof(asoc->cookie_how))
1499 asoc->cookie_how[how_indx] = 2;
1503 * find and validate the INIT chunk in the cookie (peer's info) the
1504 * INIT should start after the cookie-echo header struct (chunk
1505 * header, state cookie header struct)
1507 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1509 init_cp = (struct sctp_init_chunk *)
1510 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1511 (uint8_t *) & init_buf);
1512 if (init_cp == NULL) {
1513 /* could not pull a INIT chunk in cookie */
1516 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1520 * find and validate the INIT-ACK chunk in the cookie (my info) the
1521 * INIT-ACK follows the INIT chunk
1523 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1524 initack_cp = (struct sctp_init_ack_chunk *)
1525 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1526 (uint8_t *) & initack_buf);
1527 if (initack_cp == NULL) {
1528 /* could not pull INIT-ACK chunk in cookie */
1531 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1534 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1535 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1537 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1538 * to get into the OPEN state
1540 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1542 * Opps, this means that we somehow generated two vtag's
1543 * the same. I.e. we did:
1545 * <---INIT(tag=a)------
1546 * ----INIT-ACK(tag=t)-->
1547 * ----INIT(tag=t)------> *1
1548 * <---INIT-ACK(tag=a)---
1549 * <----CE(tag=t)------------- *2
1551 * At point *1 we should be generating a different
1552 * tag t'. Which means we would throw away the CE and send
1553 * ours instead. Basically this is case C (throw away side).
1555 if (how_indx < sizeof(asoc->cookie_how))
1556 asoc->cookie_how[how_indx] = 17;
1560 switch SCTP_GET_STATE
1562 case SCTP_STATE_COOKIE_WAIT:
1563 case SCTP_STATE_COOKIE_ECHOED:
1565 * INIT was sent but got a COOKIE_ECHO with the
1566 * correct tags... just accept it...but we must
1567 * process the init so that we can make sure we have
1568 * the right seq no's.
1570 /* First we must process the INIT !! */
1571 retval = sctp_process_init(init_cp, stcb);
1573 if (how_indx < sizeof(asoc->cookie_how))
1574 asoc->cookie_how[how_indx] = 3;
1577 /* we have already processed the INIT so no problem */
1578 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1579 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1580 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1581 /* update current state */
1582 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1583 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1585 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1587 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1588 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1589 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1590 stcb->sctp_ep, stcb, asoc->primary_destination);
1592 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1593 sctp_stop_all_cookie_timers(stcb);
1594 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1595 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1596 (inp->sctp_socket->so_qlimit == 0)
1598 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1603 * Here is where collision would go if we
1604 * did a connect() and instead got a
1605 * init/init-ack/cookie done before the
1606 * init-ack came back..
1608 stcb->sctp_ep->sctp_flags |=
1609 SCTP_PCB_FLAGS_CONNECTED;
1610 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1611 so = SCTP_INP_SO(stcb->sctp_ep);
1612 atomic_add_int(&stcb->asoc.refcnt, 1);
1613 SCTP_TCB_UNLOCK(stcb);
1614 SCTP_SOCKET_LOCK(so, 1);
1615 SCTP_TCB_LOCK(stcb);
1616 atomic_add_int(&stcb->asoc.refcnt, -1);
1617 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1618 SCTP_SOCKET_UNLOCK(so, 1);
1622 soisconnected(stcb->sctp_socket);
1623 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1624 SCTP_SOCKET_UNLOCK(so, 1);
1627 /* notify upper layer */
1628 *notification = SCTP_NOTIFY_ASSOC_UP;
1630 * since we did not send a HB make sure we don't
1633 net->hb_responded = 1;
1634 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1635 &cookie->time_entered,
1636 sctp_align_unsafe_makecopy,
1637 SCTP_RTT_FROM_NON_DATA);
1639 if (stcb->asoc.sctp_autoclose_ticks &&
1640 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1641 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1647 * we're in the OPEN state (or beyond), so peer must
1648 * have simply lost the COOKIE-ACK
1652 sctp_stop_all_cookie_timers(stcb);
1654 * We ignore the return code here.. not sure if we should
1655 * somehow abort.. but we do have an existing asoc. This
1656 * really should not fail.
1658 if (sctp_load_addresses_from_init(stcb, m,
1659 init_offset + sizeof(struct sctp_init_chunk),
1660 initack_offset, src, dst, init_src)) {
1661 if (how_indx < sizeof(asoc->cookie_how))
1662 asoc->cookie_how[how_indx] = 4;
1665 /* respond with a COOKIE-ACK */
1666 sctp_toss_old_cookies(stcb, asoc);
1667 sctp_send_cookie_ack(stcb);
1668 if (how_indx < sizeof(asoc->cookie_how))
1669 asoc->cookie_how[how_indx] = 5;
1672 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1673 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1674 cookie->tie_tag_my_vtag == 0 &&
1675 cookie->tie_tag_peer_vtag == 0) {
1677 * case C in Section 5.2.4 Table 2: XMOO silently discard
1679 if (how_indx < sizeof(asoc->cookie_how))
1680 asoc->cookie_how[how_indx] = 6;
1684 * If nat support, and the below and stcb is established, send back
1685 * a ABORT(colliding state) if we are established.
1687 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1688 (asoc->peer_supports_nat) &&
1689 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1690 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1691 (asoc->peer_vtag == 0)))) {
1693 * Special case - Peer's support nat. We may have two init's
1694 * that we gave out the same tag on since one was not
1695 * established.. i.e. we get INIT from host-1 behind the nat
1696 * and we respond tag-a, we get a INIT from host-2 behind
1697 * the nat and we get tag-a again. Then we bring up host-1
1698 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1699 * Now we have colliding state. We must send an abort here
1700 * with colliding state indication.
1702 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1703 0, M_DONTWAIT, 1, MT_DATA);
1704 if (op_err == NULL) {
1708 /* pre-reserve some space */
1710 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
1712 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
1714 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
1715 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1717 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1718 ph = mtod(op_err, struct sctp_paramhdr *);
1719 ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE);
1720 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1721 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1722 use_mflowid, mflowid,
1726 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1727 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1728 (asoc->peer_vtag == 0))) {
1730 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1731 * should be ok, re-accept peer info
1733 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1735 * Extension of case C. If we hit this, then the
1736 * random number generator returned the same vtag
1737 * when we first sent our INIT-ACK and when we later
1738 * sent our INIT. The side with the seq numbers that
1739 * are different will be the one that normnally
1740 * would have hit case C. This in effect "extends"
1741 * our vtags in this collision case to be 64 bits.
1742 * The same collision could occur aka you get both
1743 * vtag and seq number the same twice in a row.. but
1744 * is much less likely. If it did happen then we
1745 * would proceed through and bring up the assoc.. we
1746 * may end up with the wrong stream setup however..
1747 * which would be bad.. but there is no way to
1748 * tell.. until we send on a stream that does not
1751 if (how_indx < sizeof(asoc->cookie_how))
1752 asoc->cookie_how[how_indx] = 7;
1756 if (how_indx < sizeof(asoc->cookie_how))
1757 asoc->cookie_how[how_indx] = 8;
1758 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1759 sctp_stop_all_cookie_timers(stcb);
1761 * since we did not send a HB make sure we don't double
1764 net->hb_responded = 1;
1765 if (stcb->asoc.sctp_autoclose_ticks &&
1766 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1767 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1770 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1771 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1773 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1775 * Ok the peer probably discarded our data (if we
1776 * echoed a cookie+data). So anything on the
1777 * sent_queue should be marked for retransmit, we
1778 * may not get something to kick us so it COULD
1779 * still take a timeout to move these.. but it can't
1780 * hurt to mark them.
1782 struct sctp_tmit_chunk *chk;
1784 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1785 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1786 chk->sent = SCTP_DATAGRAM_RESEND;
1787 sctp_flight_size_decrease(chk);
1788 sctp_total_flight_decrease(stcb, chk);
1789 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1795 /* process the INIT info (peer's info) */
1796 retval = sctp_process_init(init_cp, stcb);
1798 if (how_indx < sizeof(asoc->cookie_how))
1799 asoc->cookie_how[how_indx] = 9;
1802 if (sctp_load_addresses_from_init(stcb, m,
1803 init_offset + sizeof(struct sctp_init_chunk),
1804 initack_offset, src, dst, init_src)) {
1805 if (how_indx < sizeof(asoc->cookie_how))
1806 asoc->cookie_how[how_indx] = 10;
1809 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1810 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1811 *notification = SCTP_NOTIFY_ASSOC_UP;
1813 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1814 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1815 (inp->sctp_socket->so_qlimit == 0)) {
1816 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1820 stcb->sctp_ep->sctp_flags |=
1821 SCTP_PCB_FLAGS_CONNECTED;
1822 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1823 so = SCTP_INP_SO(stcb->sctp_ep);
1824 atomic_add_int(&stcb->asoc.refcnt, 1);
1825 SCTP_TCB_UNLOCK(stcb);
1826 SCTP_SOCKET_LOCK(so, 1);
1827 SCTP_TCB_LOCK(stcb);
1828 atomic_add_int(&stcb->asoc.refcnt, -1);
1829 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1830 SCTP_SOCKET_UNLOCK(so, 1);
1834 soisconnected(stcb->sctp_socket);
1835 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1836 SCTP_SOCKET_UNLOCK(so, 1);
1839 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1840 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1842 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1843 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1844 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1845 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1847 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1849 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1850 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1851 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1852 stcb->sctp_ep, stcb, asoc->primary_destination);
1854 sctp_stop_all_cookie_timers(stcb);
1855 sctp_toss_old_cookies(stcb, asoc);
1856 sctp_send_cookie_ack(stcb);
1859 * only if we have retrans set do we do this. What
1860 * this call does is get only the COOKIE-ACK out and
1861 * then when we return the normal call to
1862 * sctp_chunk_output will get the retrans out behind
1865 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1867 if (how_indx < sizeof(asoc->cookie_how))
1868 asoc->cookie_how[how_indx] = 11;
1872 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1873 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1874 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1875 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1876 cookie->tie_tag_peer_vtag != 0) {
1877 struct sctpasochead *head;
1879 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1884 if (asoc->peer_supports_nat) {
1886 * This is a gross gross hack. Just call the
1887 * cookie_new code since we are allowing a duplicate
1888 * association. I hope this works...
1890 return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
1891 sh, cookie, cookie_len,
1892 inp, netp, init_src, notification,
1893 auth_skipped, auth_offset, auth_len,
1894 use_mflowid, mflowid,
1898 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1901 if (how_indx < sizeof(asoc->cookie_how))
1902 asoc->cookie_how[how_indx] = 12;
1903 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1904 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1906 /* notify upper layer */
1907 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1908 atomic_add_int(&stcb->asoc.refcnt, 1);
1909 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1910 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1911 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1912 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1914 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1915 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1916 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1917 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1919 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1920 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1921 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1922 stcb->sctp_ep, stcb, asoc->primary_destination);
1924 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1925 /* move to OPEN state, if not in SHUTDOWN_SENT */
1926 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1928 asoc->pre_open_streams =
1929 ntohs(initack_cp->init.num_outbound_streams);
1930 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1931 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1932 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1934 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1936 asoc->str_reset_seq_in = asoc->init_seq_number;
1938 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1939 if (asoc->mapping_array) {
1940 memset(asoc->mapping_array, 0,
1941 asoc->mapping_array_size);
1943 if (asoc->nr_mapping_array) {
1944 memset(asoc->nr_mapping_array, 0,
1945 asoc->mapping_array_size);
1947 SCTP_TCB_UNLOCK(stcb);
1948 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1949 so = SCTP_INP_SO(stcb->sctp_ep);
1950 SCTP_SOCKET_LOCK(so, 1);
1952 SCTP_INP_INFO_WLOCK();
1953 SCTP_INP_WLOCK(stcb->sctp_ep);
1954 SCTP_TCB_LOCK(stcb);
1955 atomic_add_int(&stcb->asoc.refcnt, -1);
1956 /* send up all the data */
1957 SCTP_TCB_SEND_LOCK(stcb);
1959 sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_LOCKED);
1960 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1961 stcb->asoc.strmout[i].chunks_on_queues = 0;
1962 stcb->asoc.strmout[i].stream_no = i;
1963 stcb->asoc.strmout[i].next_sequence_send = 0;
1964 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1966 /* process the INIT-ACK info (my info) */
1967 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1968 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1970 /* pull from vtag hash */
1971 LIST_REMOVE(stcb, sctp_asocs);
1972 /* re-insert to new vtag position */
1973 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1974 SCTP_BASE_INFO(hashasocmark))];
1976 * put it in the bucket in the vtag hash of assoc's for the
1979 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1981 SCTP_TCB_SEND_UNLOCK(stcb);
1982 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1983 SCTP_INP_INFO_WUNLOCK();
1984 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1985 SCTP_SOCKET_UNLOCK(so, 1);
1987 asoc->total_flight = 0;
1988 asoc->total_flight_count = 0;
1989 /* process the INIT info (peer's info) */
1990 retval = sctp_process_init(init_cp, stcb);
1992 if (how_indx < sizeof(asoc->cookie_how))
1993 asoc->cookie_how[how_indx] = 13;
1998 * since we did not send a HB make sure we don't double
2001 net->hb_responded = 1;
2003 if (sctp_load_addresses_from_init(stcb, m,
2004 init_offset + sizeof(struct sctp_init_chunk),
2005 initack_offset, src, dst, init_src)) {
2006 if (how_indx < sizeof(asoc->cookie_how))
2007 asoc->cookie_how[how_indx] = 14;
2011 /* respond with a COOKIE-ACK */
2012 sctp_stop_all_cookie_timers(stcb);
2013 sctp_toss_old_cookies(stcb, asoc);
2014 sctp_send_cookie_ack(stcb);
2015 if (how_indx < sizeof(asoc->cookie_how))
2016 asoc->cookie_how[how_indx] = 15;
2020 if (how_indx < sizeof(asoc->cookie_how))
2021 asoc->cookie_how[how_indx] = 16;
2022 /* all other cases... */
2028 * handle a state cookie for a new association m: input packet mbuf chain--
2029 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
2030 * and the cookie signature does not exist offset: offset into mbuf to the
2031 * cookie-echo chunk length: length of the cookie chunk to: where the init
2032 * was from returns a new TCB
2034 static struct sctp_tcb *
2035 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
2036 struct sockaddr *src, struct sockaddr *dst,
2037 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
2038 struct sctp_inpcb *inp, struct sctp_nets **netp,
2039 struct sockaddr *init_src, int *notification,
2040 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2041 uint8_t use_mflowid, uint32_t mflowid,
2042 uint32_t vrf_id, uint16_t port)
2044 struct sctp_tcb *stcb;
2045 struct sctp_init_chunk *init_cp, init_buf;
2046 struct sctp_init_ack_chunk *initack_cp, initack_buf;
2047 struct sockaddr_storage sa_store;
2048 struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
2049 struct sctp_association *asoc;
2050 int init_offset, initack_offset, initack_limit;
2053 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
2056 struct sockaddr_in *sin;
2060 struct sockaddr_in6 *sin6;
2063 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2066 so = SCTP_INP_SO(inp);
2070 * find and validate the INIT chunk in the cookie (peer's info) the
2071 * INIT should start after the cookie-echo header struct (chunk
2072 * header, state cookie header struct)
2074 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2075 init_cp = (struct sctp_init_chunk *)
2076 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2077 (uint8_t *) & init_buf);
2078 if (init_cp == NULL) {
2079 /* could not pull a INIT chunk in cookie */
2080 SCTPDBG(SCTP_DEBUG_INPUT1,
2081 "process_cookie_new: could not pull INIT chunk hdr\n");
2084 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2085 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2088 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2090 * find and validate the INIT-ACK chunk in the cookie (my info) the
2091 * INIT-ACK follows the INIT chunk
2093 initack_cp = (struct sctp_init_ack_chunk *)
2094 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2095 (uint8_t *) & initack_buf);
2096 if (initack_cp == NULL) {
2097 /* could not pull INIT-ACK chunk in cookie */
2098 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2101 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2105 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2106 * "initack_limit" value. This is because the chk_length field
2107 * includes the length of the cookie, but the cookie is omitted when
2108 * the INIT and INIT_ACK are tacked onto the cookie...
2110 initack_limit = offset + cookie_len;
2113 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2118 * Here we do a trick, we set in NULL for the proc/thread argument.
2119 * We do this since in effect we only use the p argument when the
2120 * socket is unbound and we must do an implicit bind. Since we are
2121 * getting a cookie, we cannot be unbound.
2123 stcb = sctp_aloc_assoc(inp, init_src, &error,
2124 ntohl(initack_cp->init.initiate_tag), vrf_id,
2125 (struct thread *)NULL
2128 struct mbuf *op_err;
2130 /* memory problem? */
2131 SCTPDBG(SCTP_DEBUG_INPUT1,
2132 "process_cookie_new: no room for another TCB!\n");
2133 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2135 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2136 src, dst, sh, op_err,
2137 use_mflowid, mflowid,
2141 /* get the correct sctp_nets */
2143 *netp = sctp_findnet(stcb, init_src);
2146 /* get scope variables out of cookie */
2147 asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2148 asoc->scope.site_scope = cookie->site_scope;
2149 asoc->scope.local_scope = cookie->local_scope;
2150 asoc->scope.loopback_scope = cookie->loopback_scope;
2152 if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2153 (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2154 struct mbuf *op_err;
2157 * Houston we have a problem. The EP changed while the
2158 * cookie was in flight. Only recourse is to abort the
2161 atomic_add_int(&stcb->asoc.refcnt, 1);
2162 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2163 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2164 src, dst, sh, op_err,
2165 use_mflowid, mflowid,
2167 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2168 SCTP_TCB_UNLOCK(stcb);
2169 SCTP_SOCKET_LOCK(so, 1);
2170 SCTP_TCB_LOCK(stcb);
2172 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2173 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2174 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2175 SCTP_SOCKET_UNLOCK(so, 1);
2177 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2180 /* process the INIT-ACK info (my info) */
2181 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2182 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2183 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2184 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2185 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2186 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2187 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2188 asoc->str_reset_seq_in = asoc->init_seq_number;
2190 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2192 /* process the INIT info (peer's info) */
2194 retval = sctp_process_init(init_cp, stcb);
2198 atomic_add_int(&stcb->asoc.refcnt, 1);
2199 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2200 SCTP_TCB_UNLOCK(stcb);
2201 SCTP_SOCKET_LOCK(so, 1);
2202 SCTP_TCB_LOCK(stcb);
2204 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2205 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2206 SCTP_SOCKET_UNLOCK(so, 1);
2208 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2211 /* load all addresses */
2212 if (sctp_load_addresses_from_init(stcb, m,
2213 init_offset + sizeof(struct sctp_init_chunk), initack_offset,
2214 src, dst, init_src)) {
2215 atomic_add_int(&stcb->asoc.refcnt, 1);
2216 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2217 SCTP_TCB_UNLOCK(stcb);
2218 SCTP_SOCKET_LOCK(so, 1);
2219 SCTP_TCB_LOCK(stcb);
2221 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
2222 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2223 SCTP_SOCKET_UNLOCK(so, 1);
2225 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2229 * verify any preceding AUTH chunk that was skipped
2231 /* pull the local authentication parameters from the cookie/init-ack */
2232 sctp_auth_get_cookie_params(stcb, m,
2233 initack_offset + sizeof(struct sctp_init_ack_chunk),
2234 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2236 struct sctp_auth_chunk *auth;
2238 auth = (struct sctp_auth_chunk *)
2239 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2240 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2241 /* auth HMAC failed, dump the assoc and packet */
2242 SCTPDBG(SCTP_DEBUG_AUTH1,
2243 "COOKIE-ECHO: AUTH failed\n");
2244 atomic_add_int(&stcb->asoc.refcnt, 1);
2245 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2246 SCTP_TCB_UNLOCK(stcb);
2247 SCTP_SOCKET_LOCK(so, 1);
2248 SCTP_TCB_LOCK(stcb);
2250 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2251 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2252 SCTP_SOCKET_UNLOCK(so, 1);
2254 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2257 /* remaining chunks checked... good to go */
2258 stcb->asoc.authenticated = 1;
2261 /* update current state */
2262 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2263 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2264 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2265 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2266 stcb->sctp_ep, stcb, asoc->primary_destination);
2268 sctp_stop_all_cookie_timers(stcb);
2269 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2270 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2273 * if we're doing ASCONFs, check to see if we have any new local
2274 * addresses that need to get added to the peer (eg. addresses
2275 * changed while cookie echo in flight). This needs to be done
2276 * after we go to the OPEN state to do the correct asconf
2277 * processing. else, make sure we have the correct addresses in our
2281 /* warning, we re-use sin, sin6, sa_store here! */
2282 /* pull in local_address (our "from" address) */
2283 switch (cookie->laddr_type) {
2285 case SCTP_IPV4_ADDRESS:
2286 /* source addr is IPv4 */
2287 sin = (struct sockaddr_in *)initack_src;
2288 memset(sin, 0, sizeof(*sin));
2289 sin->sin_family = AF_INET;
2290 sin->sin_len = sizeof(struct sockaddr_in);
2291 sin->sin_addr.s_addr = cookie->laddress[0];
2295 case SCTP_IPV6_ADDRESS:
2296 /* source addr is IPv6 */
2297 sin6 = (struct sockaddr_in6 *)initack_src;
2298 memset(sin6, 0, sizeof(*sin6));
2299 sin6->sin6_family = AF_INET6;
2300 sin6->sin6_len = sizeof(struct sockaddr_in6);
2301 sin6->sin6_scope_id = cookie->scope_id;
2302 memcpy(&sin6->sin6_addr, cookie->laddress,
2303 sizeof(sin6->sin6_addr));
2307 atomic_add_int(&stcb->asoc.refcnt, 1);
2308 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2309 SCTP_TCB_UNLOCK(stcb);
2310 SCTP_SOCKET_LOCK(so, 1);
2311 SCTP_TCB_LOCK(stcb);
2313 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2314 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2315 SCTP_SOCKET_UNLOCK(so, 1);
2317 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2321 /* set up to notify upper layer */
2322 *notification = SCTP_NOTIFY_ASSOC_UP;
2323 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2324 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2325 (inp->sctp_socket->so_qlimit == 0)) {
2327 * This is an endpoint that called connect() how it got a
2328 * cookie that is NEW is a bit of a mystery. It must be that
2329 * the INIT was sent, but before it got there.. a complete
2330 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2331 * should have went to the other code.. not here.. oh well..
2332 * a bit of protection is worth having..
2334 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2335 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2336 atomic_add_int(&stcb->asoc.refcnt, 1);
2337 SCTP_TCB_UNLOCK(stcb);
2338 SCTP_SOCKET_LOCK(so, 1);
2339 SCTP_TCB_LOCK(stcb);
2340 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2341 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2342 SCTP_SOCKET_UNLOCK(so, 1);
2346 soisconnected(stcb->sctp_socket);
2347 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2348 SCTP_SOCKET_UNLOCK(so, 1);
2350 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2351 (inp->sctp_socket->so_qlimit)) {
2353 * We don't want to do anything with this one. Since it is
2354 * the listening guy. The timer will get started for
2355 * accepted connections in the caller.
2359 /* since we did not send a HB make sure we don't double things */
2360 if ((netp) && (*netp))
2361 (*netp)->hb_responded = 1;
2363 if (stcb->asoc.sctp_autoclose_ticks &&
2364 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2365 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2367 /* calculate the RTT */
2368 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2369 if ((netp) && (*netp)) {
2370 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2371 &cookie->time_entered, sctp_align_unsafe_makecopy,
2372 SCTP_RTT_FROM_NON_DATA);
2374 /* respond with a COOKIE-ACK */
2375 sctp_send_cookie_ack(stcb);
2378 * check the address lists for any ASCONFs that need to be sent
2379 * AFTER the cookie-ack is sent
2381 sctp_check_address_list(stcb, m,
2382 initack_offset + sizeof(struct sctp_init_ack_chunk),
2383 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2384 initack_src, cookie->local_scope, cookie->site_scope,
2385 cookie->ipv4_scope, cookie->loopback_scope);
2392 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2393 * we NEED to make sure we are not already using the vtag. If so we
2394 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2395 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2396 SCTP_BASE_INFO(hashasocmark))];
2397 LIST_FOREACH(stcb, head, sctp_asocs) {
2398 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2399 -- SEND ABORT - TRY AGAIN --
2405 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2406 * existing (non-NULL) TCB
2408 static struct mbuf *
2409 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2410 struct sockaddr *src, struct sockaddr *dst,
2411 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2412 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2413 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2414 struct sctp_tcb **locked_tcb,
2415 uint8_t use_mflowid, uint32_t mflowid,
2416 uint32_t vrf_id, uint16_t port)
2418 struct sctp_state_cookie *cookie;
2419 struct sctp_tcb *l_stcb = *stcb;
2420 struct sctp_inpcb *l_inp;
2421 struct sockaddr *to;
2422 struct sctp_pcb *ep;
2424 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2426 uint8_t cookie_ok = 0;
2427 unsigned int sig_offset, cookie_offset;
2428 unsigned int cookie_len;
2430 struct timeval time_expires;
2431 int notification = 0;
2432 struct sctp_nets *netl;
2433 int had_a_existing_tcb = 0;
2434 int send_int_conf = 0;
2437 struct sockaddr_in sin;
2441 struct sockaddr_in6 sin6;
2445 SCTPDBG(SCTP_DEBUG_INPUT2,
2446 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2448 if (inp_p == NULL) {
2451 cookie = &cp->cookie;
2452 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2453 cookie_len = ntohs(cp->ch.chunk_length);
2455 if ((cookie->peerport != sh->src_port) &&
2456 (cookie->myport != sh->dest_port) &&
2457 (cookie->my_vtag != sh->v_tag)) {
2459 * invalid ports or bad tag. Note that we always leave the
2460 * v_tag in the header in network order and when we stored
2461 * it in the my_vtag slot we also left it in network order.
2462 * This maintains the match even though it may be in the
2463 * opposite byte order of the machine :->
2467 if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2468 sizeof(struct sctp_init_chunk) +
2469 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2470 /* cookie too small */
2474 * split off the signature into its own mbuf (since it should not be
2475 * calculated in the sctp_hmac_m() call).
2477 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2478 m_sig = m_split(m, sig_offset, M_DONTWAIT);
2479 if (m_sig == NULL) {
2480 /* out of memory or ?? */
2483 #ifdef SCTP_MBUF_LOGGING
2484 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2487 for (mat = m_sig; mat; mat = SCTP_BUF_NEXT(mat)) {
2488 if (SCTP_BUF_IS_EXTENDED(mat)) {
2489 sctp_log_mb(mat, SCTP_MBUF_SPLIT);
2496 * compute the signature/digest for the cookie
2498 ep = &(*inp_p)->sctp_ep;
2501 SCTP_TCB_UNLOCK(l_stcb);
2503 SCTP_INP_RLOCK(l_inp);
2505 SCTP_TCB_LOCK(l_stcb);
2507 /* which cookie is it? */
2508 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2509 (ep->current_secret_number != ep->last_secret_number)) {
2510 /* it's the old cookie */
2511 (void)sctp_hmac_m(SCTP_HMAC,
2512 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2513 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2515 /* it's the current cookie */
2516 (void)sctp_hmac_m(SCTP_HMAC,
2517 (uint8_t *) ep->secret_key[(int)ep->current_secret_number],
2518 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2520 /* get the signature */
2521 SCTP_INP_RUNLOCK(l_inp);
2522 sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig);
2524 /* couldn't find signature */
2525 sctp_m_freem(m_sig);
2528 /* compare the received digest with the computed digest */
2529 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2530 /* try the old cookie? */
2531 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2532 (ep->current_secret_number != ep->last_secret_number)) {
2533 /* compute digest with old */
2534 (void)sctp_hmac_m(SCTP_HMAC,
2535 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2536 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2538 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2546 * Now before we continue we must reconstruct our mbuf so that
2547 * normal processing of any other chunks will work.
2553 while (SCTP_BUF_NEXT(m_at) != NULL) {
2554 m_at = SCTP_BUF_NEXT(m_at);
2556 SCTP_BUF_NEXT(m_at) = m_sig;
2559 if (cookie_ok == 0) {
2560 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2561 SCTPDBG(SCTP_DEBUG_INPUT2,
2562 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2563 (uint32_t) offset, cookie_offset, sig_offset);
2567 * check the cookie timestamps to be sure it's not stale
2569 (void)SCTP_GETTIME_TIMEVAL(&now);
2570 /* Expire time is in Ticks, so we convert to seconds */
2571 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2572 time_expires.tv_usec = cookie->time_entered.tv_usec;
2574 * TODO sctp_constants.h needs alternative time macros when _KERNEL
2577 if (timevalcmp(&now, &time_expires, >)) {
2578 /* cookie is stale! */
2579 struct mbuf *op_err;
2580 struct sctp_stale_cookie_msg *scm;
2583 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg),
2584 0, M_DONTWAIT, 1, MT_DATA);
2585 if (op_err == NULL) {
2590 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg);
2591 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
2592 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
2593 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
2594 (sizeof(uint32_t))));
2595 /* seconds to usec */
2596 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2599 tim = now.tv_usec - cookie->time_entered.tv_usec;
2600 scm->time_usec = htonl(tim);
2601 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2602 use_mflowid, mflowid,
2607 * Now we must see with the lookup address if we have an existing
2608 * asoc. This will only happen if we were in the COOKIE-WAIT state
2609 * and a INIT collided with us and somewhere the peer sent the
2610 * cookie on another address besides the single address our assoc
2611 * had for him. In this case we will have one of the tie-tags set at
2612 * least AND the address field in the cookie can be used to look it
2616 switch (cookie->addr_type) {
2618 case SCTP_IPV6_ADDRESS:
2619 memset(&sin6, 0, sizeof(sin6));
2620 sin6.sin6_family = AF_INET6;
2621 sin6.sin6_len = sizeof(sin6);
2622 sin6.sin6_port = sh->src_port;
2623 sin6.sin6_scope_id = cookie->scope_id;
2624 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2625 sizeof(sin6.sin6_addr.s6_addr));
2626 to = (struct sockaddr *)&sin6;
2630 case SCTP_IPV4_ADDRESS:
2631 memset(&sin, 0, sizeof(sin));
2632 sin.sin_family = AF_INET;
2633 sin.sin_len = sizeof(sin);
2634 sin.sin_port = sh->src_port;
2635 sin.sin_addr.s_addr = cookie->address[0];
2636 to = (struct sockaddr *)&sin;
2640 /* This should not happen */
2643 if ((*stcb == NULL) && to) {
2644 /* Yep, lets check */
2645 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2646 if (*stcb == NULL) {
2648 * We should have only got back the same inp. If we
2649 * got back a different ep we have a problem. The
2650 * original findep got back l_inp and now
2652 if (l_inp != *inp_p) {
2653 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2656 if (*locked_tcb == NULL) {
2658 * In this case we found the assoc only
2659 * after we locked the create lock. This
2660 * means we are in a colliding case and we
2661 * must make sure that we unlock the tcb if
2662 * its one of the cases where we throw away
2663 * the incoming packets.
2665 *locked_tcb = *stcb;
2668 * We must also increment the inp ref count
2669 * since the ref_count flags was set when we
2670 * did not find the TCB, now we found it
2671 * which reduces the refcount.. we must
2672 * raise it back out to balance it all :-)
2674 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2675 if ((*stcb)->sctp_ep != l_inp) {
2676 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2677 (void *)(*stcb)->sctp_ep, (void *)l_inp);
2685 cookie_len -= SCTP_SIGNATURE_SIZE;
2686 if (*stcb == NULL) {
2687 /* this is the "normal" case... get a new TCB */
2688 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2689 cookie, cookie_len, *inp_p,
2690 netp, to, ¬ification,
2691 auth_skipped, auth_offset, auth_len,
2692 use_mflowid, mflowid,
2695 /* this is abnormal... cookie-echo on existing TCB */
2696 had_a_existing_tcb = 1;
2697 *stcb = sctp_process_cookie_existing(m, iphlen, offset,
2699 cookie, cookie_len, *inp_p, *stcb, netp, to,
2700 ¬ification, auth_skipped, auth_offset, auth_len,
2701 use_mflowid, mflowid,
2705 if (*stcb == NULL) {
2706 /* still no TCB... must be bad cookie-echo */
2709 if ((*netp != NULL) && (use_mflowid != 0)) {
2710 (*netp)->flowid = mflowid;
2712 (*netp)->flowidset = 1;
2716 * Ok, we built an association so confirm the address we sent the
2719 netl = sctp_findnet(*stcb, to);
2721 * This code should in theory NOT run but
2724 /* TSNH! Huh, why do I need to add this address here? */
2725 if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2728 netl = sctp_findnet(*stcb, to);
2731 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2732 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2733 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2738 sctp_start_net_timers(*stcb);
2739 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2740 if (!had_a_existing_tcb ||
2741 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2743 * If we have a NEW cookie or the connect never
2744 * reached the connected state during collision we
2745 * must do the TCP accept thing.
2747 struct socket *so, *oso;
2748 struct sctp_inpcb *inp;
2750 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2752 * For a restart we will keep the same
2753 * socket, no need to do anything. I THINK!!
2755 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2756 if (send_int_conf) {
2757 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2758 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2762 oso = (*inp_p)->sctp_socket;
2763 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2764 SCTP_TCB_UNLOCK((*stcb));
2765 CURVNET_SET(oso->so_vnet);
2766 so = sonewconn(oso, 0
2769 SCTP_TCB_LOCK((*stcb));
2770 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2773 struct mbuf *op_err;
2775 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2776 struct socket *pcb_so;
2779 /* Too many sockets */
2780 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2781 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2782 sctp_abort_association(*inp_p, NULL, m, iphlen,
2783 src, dst, sh, op_err,
2784 use_mflowid, mflowid,
2786 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2787 pcb_so = SCTP_INP_SO(*inp_p);
2788 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2789 SCTP_TCB_UNLOCK((*stcb));
2790 SCTP_SOCKET_LOCK(pcb_so, 1);
2791 SCTP_TCB_LOCK((*stcb));
2792 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2794 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2795 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2796 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2800 inp = (struct sctp_inpcb *)so->so_pcb;
2801 SCTP_INP_INCR_REF(inp);
2803 * We add the unbound flag here so that if we get an
2804 * soabort() before we get the move_pcb done, we
2805 * will properly cleanup.
2807 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2808 SCTP_PCB_FLAGS_CONNECTED |
2809 SCTP_PCB_FLAGS_IN_TCPPOOL |
2810 SCTP_PCB_FLAGS_UNBOUND |
2811 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2812 SCTP_PCB_FLAGS_DONT_WAKE);
2813 inp->sctp_features = (*inp_p)->sctp_features;
2814 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2815 inp->sctp_socket = so;
2816 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2817 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2818 inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable;
2819 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2820 inp->sctp_context = (*inp_p)->sctp_context;
2821 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2822 inp->inp_starting_point_for_iterator = NULL;
2824 * copy in the authentication parameters from the
2827 if (inp->sctp_ep.local_hmacs)
2828 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2829 inp->sctp_ep.local_hmacs =
2830 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2831 if (inp->sctp_ep.local_auth_chunks)
2832 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2833 inp->sctp_ep.local_auth_chunks =
2834 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2837 * Now we must move it from one hash table to
2838 * another and get the tcb in the right place.
2842 * This is where the one-2-one socket is put into
2843 * the accept state waiting for the accept!
2846 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2848 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2850 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2851 SCTP_TCB_UNLOCK((*stcb));
2853 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2855 SCTP_TCB_LOCK((*stcb));
2856 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2860 * now we must check to see if we were aborted while
2861 * the move was going on and the lock/unlock
2864 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2866 * yep it was, we leave the assoc attached
2867 * to the socket since the sctp_inpcb_free()
2868 * call will send an abort for us.
2870 SCTP_INP_DECR_REF(inp);
2873 SCTP_INP_DECR_REF(inp);
2874 /* Switch over to the new guy */
2876 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2877 if (send_int_conf) {
2878 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2879 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2882 * Pull it from the incomplete queue and wake the
2885 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2886 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2887 SCTP_TCB_UNLOCK((*stcb));
2888 SCTP_SOCKET_LOCK(so, 1);
2891 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2892 SCTP_TCB_LOCK((*stcb));
2893 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2894 SCTP_SOCKET_UNLOCK(so, 1);
2900 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2902 if (send_int_conf) {
2903 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2904 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2910 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2911 struct sctp_tcb *stcb, struct sctp_nets *net)
2913 /* cp must not be used, others call this without a c-ack :-) */
2914 struct sctp_association *asoc;
2916 SCTPDBG(SCTP_DEBUG_INPUT2,
2917 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2923 sctp_stop_all_cookie_timers(stcb);
2924 /* process according to association state */
2925 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2926 /* state change only needed when I am in right state */
2927 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2928 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2929 sctp_start_net_timers(stcb);
2930 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2931 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2932 stcb->sctp_ep, stcb, asoc->primary_destination);
2936 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2937 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2938 if (asoc->overall_error_count == 0) {
2939 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2940 &asoc->time_entered, sctp_align_safe_nocopy,
2941 SCTP_RTT_FROM_NON_DATA);
2943 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2944 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2945 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2946 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2947 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2951 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2952 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2953 so = SCTP_INP_SO(stcb->sctp_ep);
2954 atomic_add_int(&stcb->asoc.refcnt, 1);
2955 SCTP_TCB_UNLOCK(stcb);
2956 SCTP_SOCKET_LOCK(so, 1);
2957 SCTP_TCB_LOCK(stcb);
2958 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2960 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2961 soisconnected(stcb->sctp_socket);
2963 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2964 SCTP_SOCKET_UNLOCK(so, 1);
2968 * since we did not send a HB make sure we don't double
2971 net->hb_responded = 1;
2973 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2975 * We don't need to do the asconf thing, nor hb or
2976 * autoclose if the socket is closed.
2980 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2984 if (stcb->asoc.sctp_autoclose_ticks &&
2985 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2986 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2987 stcb->sctp_ep, stcb, NULL);
2990 * send ASCONF if parameters are pending and ASCONFs are
2991 * allowed (eg. addresses changed when init/cookie echo were
2994 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2995 (stcb->asoc.peer_supports_asconf) &&
2996 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2997 #ifdef SCTP_TIMER_BASED_ASCONF
2998 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2999 stcb->sctp_ep, stcb,
3000 stcb->asoc.primary_destination);
3002 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
3003 SCTP_ADDR_NOT_LOCKED);
3008 /* Toss the cookie if I can */
3009 sctp_toss_old_cookies(stcb, asoc);
3010 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3011 /* Restart the timer if we have pending data */
3012 struct sctp_tmit_chunk *chk;
3014 chk = TAILQ_FIRST(&asoc->sent_queue);
3015 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
3020 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
3021 struct sctp_tcb *stcb)
3023 struct sctp_nets *net;
3024 struct sctp_tmit_chunk *lchk;
3025 struct sctp_ecne_chunk bkup;
3026 uint8_t override_bit;
3027 uint32_t tsn, window_data_tsn;
3029 unsigned int pkt_cnt;
3031 len = ntohs(cp->ch.chunk_length);
3032 if ((len != sizeof(struct sctp_ecne_chunk)) &&
3033 (len != sizeof(struct old_sctp_ecne_chunk))) {
3036 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3037 /* Its the old format */
3038 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3039 bkup.num_pkts_since_cwr = htonl(1);
3042 SCTP_STAT_INCR(sctps_recvecne);
3043 tsn = ntohl(cp->tsn);
3044 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3045 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3047 window_data_tsn = stcb->asoc.sending_seq - 1;
3049 window_data_tsn = lchk->rec.data.TSN_seq;
3052 /* Find where it was sent to if possible. */
3054 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3055 if (lchk->rec.data.TSN_seq == tsn) {
3057 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3060 if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) {
3066 * What to do. A previous send of a CWR was possibly lost.
3067 * See how old it is, we may have it marked on the actual
3070 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3071 if (tsn == net->last_cwr_tsn) {
3072 /* Found him, send it off */
3078 * If we reach here, we need to send a special CWR
3079 * that says hey, we did this a long time ago and
3080 * you lost the response.
3082 net = TAILQ_FIRST(&stcb->asoc.nets);
3087 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3094 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3095 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3097 * JRS - Use the congestion control given in the pluggable
3100 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3102 * We reduce once every RTT. So we will only lower cwnd at
3103 * the next sending seq i.e. the window_data_tsn
3105 net->cwr_window_tsn = window_data_tsn;
3106 net->ecn_ce_pkt_cnt += pkt_cnt;
3107 net->lost_cnt = pkt_cnt;
3108 net->last_cwr_tsn = tsn;
3110 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3111 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3112 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3114 * Another loss in the same window update how many
3115 * marks/packets lost we have had.
3119 if (pkt_cnt > net->lost_cnt) {
3120 /* Should be the case */
3121 cnt = (pkt_cnt - net->lost_cnt);
3122 net->ecn_ce_pkt_cnt += cnt;
3124 net->lost_cnt = pkt_cnt;
3125 net->last_cwr_tsn = tsn;
3127 * Most CC functions will ignore this call, since we
3128 * are in-window yet of the initial CE the peer saw.
3130 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3134 * We always send a CWR this way if our previous one was lost our
3135 * peer will get an update, or if it is not time again to reduce we
3136 * still get the cwr to the peer. Note we set the override when we
3137 * could not find the TSN on the chunk or the destination network.
3139 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3143 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3146 * Here we get a CWR from the peer. We must look in the outqueue and
3147 * make sure that we have a covered ECNE in the control chunk part.
3150 struct sctp_tmit_chunk *chk;
3151 struct sctp_ecne_chunk *ecne;
3155 cwr_tsn = ntohl(cp->tsn);
3157 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3158 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
3159 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3162 if ((override == 0) && (chk->whoTo != net)) {
3163 /* Must be from the right src unless override is set */
3166 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3167 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3168 /* this covers this ECNE, we can remove it */
3169 stcb->asoc.ecn_echo_cnt_onq--;
3170 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3173 sctp_m_freem(chk->data);
3176 stcb->asoc.ctrl_queue_cnt--;
3177 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3178 if (override == 0) {
3186 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3187 struct sctp_tcb *stcb, struct sctp_nets *net)
3189 struct sctp_association *asoc;
3191 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3196 SCTPDBG(SCTP_DEBUG_INPUT2,
3197 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3202 /* process according to association state */
3203 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3204 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3205 SCTPDBG(SCTP_DEBUG_INPUT2,
3206 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3207 SCTP_TCB_UNLOCK(stcb);
3210 /* notify upper layer protocol */
3211 if (stcb->sctp_socket) {
3212 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3215 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3216 !TAILQ_EMPTY(&asoc->sent_queue) ||
3217 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
3218 panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3221 /* stop the timer */
3222 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
3223 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3225 SCTPDBG(SCTP_DEBUG_INPUT2,
3226 "sctp_handle_shutdown_complete: calls free-asoc\n");
3227 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3228 so = SCTP_INP_SO(stcb->sctp_ep);
3229 atomic_add_int(&stcb->asoc.refcnt, 1);
3230 SCTP_TCB_UNLOCK(stcb);
3231 SCTP_SOCKET_LOCK(so, 1);
3232 SCTP_TCB_LOCK(stcb);
3233 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3235 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
3236 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3237 SCTP_SOCKET_UNLOCK(so, 1);
3243 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3244 struct sctp_nets *net, uint8_t flg)
3246 switch (desc->chunk_type) {
3248 /* find the tsn to resend (possibly */
3251 struct sctp_tmit_chunk *tp1;
3253 tsn = ntohl(desc->tsn_ifany);
3254 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3255 if (tp1->rec.data.TSN_seq == tsn) {
3259 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) {
3267 * Do it the other way , aka without paying
3268 * attention to queue seq order.
3270 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3271 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3272 if (tp1->rec.data.TSN_seq == tsn) {
3279 SCTP_STAT_INCR(sctps_pdrptsnnf);
3281 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3284 if (((flg & SCTP_BADCRC) == 0) &&
3285 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3288 if ((stcb->asoc.peers_rwnd == 0) &&
3289 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3290 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3293 if (stcb->asoc.peers_rwnd == 0 &&
3294 (flg & SCTP_FROM_MIDDLE_BOX)) {
3295 SCTP_STAT_INCR(sctps_pdrpdizrw);
3298 ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+
3299 sizeof(struct sctp_data_chunk));
3303 for (iii = 0; iii < sizeof(desc->data_bytes);
3305 if (ddp[iii] != desc->data_bytes[iii]) {
3306 SCTP_STAT_INCR(sctps_pdrpbadd);
3314 * this guy had a RTO calculation
3315 * pending on it, cancel it
3317 if (tp1->whoTo->rto_needed == 0) {
3318 tp1->whoTo->rto_needed = 1;
3322 SCTP_STAT_INCR(sctps_pdrpmark);
3323 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3324 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3326 * mark it as if we were doing a FR, since
3327 * we will be getting gap ack reports behind
3328 * the info from the router.
3330 tp1->rec.data.doing_fast_retransmit = 1;
3332 * mark the tsn with what sequences can
3335 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3336 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3338 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
3341 /* restart the timer */
3342 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3343 stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3344 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3347 /* fix counts and things */
3348 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3349 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3350 tp1->whoTo->flight_size,
3353 tp1->rec.data.TSN_seq);
3355 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3356 sctp_flight_size_decrease(tp1);
3357 sctp_total_flight_decrease(stcb, tp1);
3359 tp1->sent = SCTP_DATAGRAM_RESEND;
3365 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3366 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3369 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3371 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3374 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3375 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3376 audit, stcb->asoc.sent_queue_retran_cnt);
3377 #ifndef SCTP_AUDITING_ENABLED
3378 stcb->asoc.sent_queue_retran_cnt = audit;
3386 struct sctp_tmit_chunk *asconf;
3388 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3390 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3395 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3396 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3397 asconf->sent = SCTP_DATAGRAM_RESEND;
3398 asconf->snd_count--;
3402 case SCTP_INITIATION:
3403 /* resend the INIT */
3404 stcb->asoc.dropped_special_cnt++;
3405 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3407 * If we can get it in, in a few attempts we do
3408 * this, otherwise we let the timer fire.
3410 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3411 stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3412 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3415 case SCTP_SELECTIVE_ACK:
3416 case SCTP_NR_SELECTIVE_ACK:
3417 /* resend the sack */
3418 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3420 case SCTP_HEARTBEAT_REQUEST:
3421 /* resend a demand HB */
3422 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3424 * Only retransmit if we KNOW we wont destroy the
3427 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3431 sctp_send_shutdown(stcb, net);
3433 case SCTP_SHUTDOWN_ACK:
3434 sctp_send_shutdown_ack(stcb, net);
3436 case SCTP_COOKIE_ECHO:
3438 struct sctp_tmit_chunk *cookie;
3441 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3443 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3448 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3449 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3450 cookie->sent = SCTP_DATAGRAM_RESEND;
3451 sctp_stop_all_cookie_timers(stcb);
3455 case SCTP_COOKIE_ACK:
3456 sctp_send_cookie_ack(stcb);
3458 case SCTP_ASCONF_ACK:
3459 /* resend last asconf ack */
3460 sctp_send_asconf_ack(stcb);
3462 case SCTP_FORWARD_CUM_TSN:
3463 send_forward_tsn(stcb, &stcb->asoc);
3465 /* can't do anything with these */
3466 case SCTP_PACKET_DROPPED:
3467 case SCTP_INITIATION_ACK: /* this should not happen */
3468 case SCTP_HEARTBEAT_ACK:
3469 case SCTP_ABORT_ASSOCIATION:
3470 case SCTP_OPERATION_ERROR:
3471 case SCTP_SHUTDOWN_COMPLETE:
3481 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t * list)
3487 * We set things to 0xffff since this is the last delivered sequence
3488 * and we will be sending in 0 after the reset.
3491 if (number_entries) {
3492 for (i = 0; i < number_entries; i++) {
3493 temp = ntohs(list[i]);
3494 if (temp >= stcb->asoc.streamincnt) {
3497 stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff;
3501 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3502 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3505 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3509 sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t * list)
3514 if (number_entries > 0) {
3515 for (i = 0; i < number_entries; i++) {
3516 temp = ntohs(list[i]);
3517 if (temp >= stcb->asoc.streamoutcnt) {
3518 /* no such stream */
3521 stcb->asoc.strmout[temp].next_sequence_send = 0;
3524 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3525 stcb->asoc.strmout[i].next_sequence_send = 0;
3528 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3532 struct sctp_stream_reset_out_request *
3533 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3535 struct sctp_association *asoc;
3536 struct sctp_chunkhdr *ch;
3537 struct sctp_stream_reset_out_request *r;
3538 struct sctp_tmit_chunk *chk;
3542 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3543 asoc->stream_reset_outstanding = 0;
3546 if (stcb->asoc.str_reset == NULL) {
3547 asoc->stream_reset_outstanding = 0;
3550 chk = stcb->asoc.str_reset;
3551 if (chk->data == NULL) {
3555 /* he wants a copy of the chk pointer */
3558 clen = chk->send_size;
3559 ch = mtod(chk->data, struct sctp_chunkhdr *);
3560 r = (struct sctp_stream_reset_out_request *)(ch + 1);
3561 if (ntohl(r->request_seq) == seq) {
3565 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3566 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3567 /* move to the next one, there can only be a max of two */
3568 r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len);
3569 if (ntohl(r->request_seq) == seq) {
3573 /* that seq is not here */
3578 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3580 struct sctp_association *asoc;
3581 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3583 if (stcb->asoc.str_reset == NULL) {
3588 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3589 TAILQ_REMOVE(&asoc->control_send_queue,
3593 sctp_m_freem(chk->data);
3596 asoc->ctrl_queue_cnt--;
3597 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3598 /* sa_ignore NO_NULL_CHK */
3599 stcb->asoc.str_reset = NULL;
3604 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3605 uint32_t seq, uint32_t action,
3606 struct sctp_stream_reset_response *respin)
3610 struct sctp_association *asoc = &stcb->asoc;
3611 struct sctp_tmit_chunk *chk;
3612 struct sctp_stream_reset_out_request *srparam;
3613 uint32_t number_entries;
3615 if (asoc->stream_reset_outstanding == 0) {
3619 if (seq == stcb->asoc.str_reset_seq_out) {
3620 srparam = sctp_find_stream_reset(stcb, seq, &chk);
3622 stcb->asoc.str_reset_seq_out++;
3623 type = ntohs(srparam->ph.param_type);
3624 lparm_len = ntohs(srparam->ph.param_length);
3625 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3626 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3627 asoc->stream_reset_out_is_outstanding = 0;
3628 if (asoc->stream_reset_outstanding)
3629 asoc->stream_reset_outstanding--;
3630 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3632 sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams);
3633 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3634 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3636 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3638 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3639 /* Answered my request */
3640 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3641 if (asoc->stream_reset_outstanding)
3642 asoc->stream_reset_outstanding--;
3643 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3644 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3645 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3646 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3647 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3648 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3650 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3651 /* Ok we now may have more streams */
3654 num_stream = stcb->asoc.strm_pending_add_size;
3655 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3657 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3659 stcb->asoc.strm_pending_add_size = 0;
3660 if (asoc->stream_reset_outstanding)
3661 asoc->stream_reset_outstanding--;
3662 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3663 /* Put the new streams into effect */
3664 stcb->asoc.streamoutcnt += num_stream;
3665 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3666 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3667 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3668 SCTP_STREAM_CHANGE_DENIED);
3670 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3671 SCTP_STREAM_CHANGE_FAILED);
3673 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3674 if (asoc->stream_reset_outstanding)
3675 asoc->stream_reset_outstanding--;
3676 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3677 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3678 SCTP_STREAM_CHANGE_DENIED);
3679 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3680 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3681 SCTP_STREAM_CHANGE_FAILED);
3683 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3685 * a) Adopt the new in tsn.
3687 * c) Adopt the new out-tsn
3689 struct sctp_stream_reset_response_tsn *resp;
3690 struct sctp_forward_tsn_chunk fwdtsn;
3693 if (respin == NULL) {
3697 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3698 resp = (struct sctp_stream_reset_response_tsn *)respin;
3699 asoc->stream_reset_outstanding--;
3700 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3701 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3702 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3703 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3707 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3708 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3709 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3711 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3712 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3713 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3715 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3716 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3718 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3719 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3721 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3722 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3723 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3724 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3725 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3726 SCTP_ASSOC_RESET_DENIED);
3728 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3729 SCTP_ASSOC_RESET_FAILED);
3732 /* get rid of the request and get the request flags */
3733 if (asoc->stream_reset_outstanding == 0) {
3734 sctp_clean_up_stream_reset(stcb);
3742 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3743 struct sctp_tmit_chunk *chk,
3744 struct sctp_stream_reset_in_request *req, int trunc)
3752 * peer wants me to send a str-reset to him for my outgoing seq's if
3755 struct sctp_association *asoc = &stcb->asoc;
3757 seq = ntohl(req->request_seq);
3758 if (asoc->str_reset_seq_in == seq) {
3759 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3760 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3761 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3763 /* Can't do it, since they exceeded our buffer size */
3764 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3765 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3766 len = ntohs(req->ph.param_length);
3767 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3768 for (i = 0; i < number_entries; i++) {
3769 temp = ntohs(req->list_of_streams[i]);
3770 req->list_of_streams[i] = temp;
3772 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3773 sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams,
3774 asoc->str_reset_seq_out,
3775 seq, (asoc->sending_seq - 1));
3776 asoc->stream_reset_out_is_outstanding = 1;
3777 asoc->str_reset = chk;
3778 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
3779 stcb->asoc.stream_reset_outstanding++;
3781 /* Can't do it, since we have sent one out */
3782 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3784 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3785 asoc->str_reset_seq_in++;
3786 } else if (asoc->str_reset_seq_in - 1 == seq) {
3787 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3788 } else if (asoc->str_reset_seq_in - 2 == seq) {
3789 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3791 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3796 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3797 struct sctp_tmit_chunk *chk,
3798 struct sctp_stream_reset_tsn_request *req)
3800 /* reset all in and out and update the tsn */
3802 * A) reset my str-seq's on in and out. B) Select a receive next,
3803 * and set cum-ack to it. Also process this selected number as a
3804 * fwd-tsn as well. C) set in the response my next sending seq.
3806 struct sctp_forward_tsn_chunk fwdtsn;
3807 struct sctp_association *asoc = &stcb->asoc;
3811 seq = ntohl(req->request_seq);
3812 if (asoc->str_reset_seq_in == seq) {
3813 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3814 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3815 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3817 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3818 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3819 fwdtsn.ch.chunk_flags = 0;
3820 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3821 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3825 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3826 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3827 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3829 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3830 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3831 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3832 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3833 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3834 atomic_add_int(&asoc->sending_seq, 1);
3835 /* save off historical data for retrans */
3836 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3837 asoc->last_sending_seq[0] = asoc->sending_seq;
3838 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3839 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3840 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3841 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3842 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3843 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3845 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3846 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3847 asoc->str_reset_seq_in++;
3848 } else if (asoc->str_reset_seq_in - 1 == seq) {
3849 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3850 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3851 } else if (asoc->str_reset_seq_in - 2 == seq) {
3852 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3853 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3855 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3861 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3862 struct sctp_tmit_chunk *chk,
3863 struct sctp_stream_reset_out_request *req, int trunc)
3866 int number_entries, len;
3867 struct sctp_association *asoc = &stcb->asoc;
3869 seq = ntohl(req->request_seq);
3871 /* now if its not a duplicate we process it */
3872 if (asoc->str_reset_seq_in == seq) {
3873 len = ntohs(req->ph.param_length);
3874 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3876 * the sender is resetting, handle the list issue.. we must
3877 * a) verify if we can do the reset, if so no problem b) If
3878 * we can't do the reset we must copy the request. c) queue
3879 * it, and setup the data in processor to trigger it off
3880 * when needed and dequeue all the queued data.
3882 tsn = ntohl(req->send_reset_at_tsn);
3884 /* move the reset action back one */
3885 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3886 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3887 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3889 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3890 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3891 /* we can do it now */
3892 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3893 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3896 * we must queue it up and thus wait for the TSN's
3897 * to arrive that are at or before tsn
3899 struct sctp_stream_reset_list *liste;
3902 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3903 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3904 siz, SCTP_M_STRESET);
3905 if (liste == NULL) {
3906 /* gak out of memory */
3907 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3908 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3912 liste->number_entries = number_entries;
3913 memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
3914 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3915 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3917 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3918 asoc->str_reset_seq_in++;
3919 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3921 * one seq back, just echo back last action since my
3922 * response was lost.
3924 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3925 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3927 * two seq back, just echo back last action since my
3928 * response was lost.
3930 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3932 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3937 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3938 struct sctp_stream_reset_add_strm *str_add)
3941 * Peer is requesting to add more streams. If its within our
3942 * max-streams we will allow it.
3944 uint32_t num_stream, i;
3946 struct sctp_association *asoc = &stcb->asoc;
3947 struct sctp_queued_to_read *ctl, *nctl;
3949 /* Get the number. */
3950 seq = ntohl(str_add->request_seq);
3951 num_stream = ntohs(str_add->number_of_streams);
3952 /* Now what would be the new total? */
3953 if (asoc->str_reset_seq_in == seq) {
3954 num_stream += stcb->asoc.streamincnt;
3955 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3956 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3957 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3958 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
3959 (num_stream > 0xffff)) {
3960 /* We must reject it they ask for to many */
3962 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3964 /* Ok, we can do that :-) */
3965 struct sctp_stream_in *oldstrm;
3967 /* save off the old */
3968 oldstrm = stcb->asoc.strmin;
3969 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
3970 (num_stream * sizeof(struct sctp_stream_in)),
3972 if (stcb->asoc.strmin == NULL) {
3973 stcb->asoc.strmin = oldstrm;
3976 /* copy off the old data */
3977 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3978 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3979 stcb->asoc.strmin[i].stream_no = i;
3980 stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered;
3981 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
3982 /* now anything on those queues? */
3983 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) {
3984 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next);
3985 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next);
3988 /* Init the new streams */
3989 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
3990 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3991 stcb->asoc.strmin[i].stream_no = i;
3992 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3993 stcb->asoc.strmin[i].delivery_started = 0;
3995 SCTP_FREE(oldstrm, SCTP_M_STRMI);
3996 /* update the size */
3997 stcb->asoc.streamincnt = num_stream;
3998 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3999 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
4001 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4002 asoc->str_reset_seq_in++;
4003 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4005 * one seq back, just echo back last action since my
4006 * response was lost.
4008 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4009 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4011 * two seq back, just echo back last action since my
4012 * response was lost.
4014 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4016 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4022 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4023 struct sctp_stream_reset_add_strm *str_add)
4026 * Peer is requesting to add more streams. If its within our
4027 * max-streams we will allow it.
4029 uint16_t num_stream;
4031 struct sctp_association *asoc = &stcb->asoc;
4033 /* Get the number. */
4034 seq = ntohl(str_add->request_seq);
4035 num_stream = ntohs(str_add->number_of_streams);
4036 /* Now what would be the new total? */
4037 if (asoc->str_reset_seq_in == seq) {
4038 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4039 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4040 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4041 } else if (stcb->asoc.stream_reset_outstanding) {
4042 /* We must reject it we have something pending */
4043 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4045 /* Ok, we can do that :-) */
4048 mychk = stcb->asoc.streamoutcnt;
4049 mychk += num_stream;
4050 if (mychk < 0x10000) {
4051 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4052 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 0, 1, num_stream, 0, 1)) {
4053 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4056 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4059 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4060 asoc->str_reset_seq_in++;
4061 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4063 * one seq back, just echo back last action since my
4064 * response was lost.
4066 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4067 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4069 * two seq back, just echo back last action since my
4070 * response was lost.
4072 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4074 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4079 __attribute__((noinline))
4082 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4083 struct sctp_chunkhdr *ch_req)
4085 int chk_length, param_len, ptype;
4086 struct sctp_paramhdr pstore;
4087 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4091 struct sctp_tmit_chunk *chk;
4092 struct sctp_chunkhdr *ch;
4093 struct sctp_paramhdr *ph;
4097 /* now it may be a reset or a reset-response */
4098 chk_length = ntohs(ch_req->chunk_length);
4100 /* setup for adding the response */
4101 sctp_alloc_a_chunk(stcb, chk);
4105 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4106 chk->rec.chunk_id.can_take_data = 0;
4107 chk->asoc = &stcb->asoc;
4108 chk->no_fr_allowed = 0;
4109 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4110 chk->book_size_scale = 0;
4111 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4112 if (chk->data == NULL) {
4115 sctp_m_freem(chk->data);
4118 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4121 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4123 /* setup chunk parameters */
4124 chk->sent = SCTP_DATAGRAM_UNSENT;
4128 ch = mtod(chk->data, struct sctp_chunkhdr *);
4129 ch->chunk_type = SCTP_STREAM_RESET;
4130 ch->chunk_flags = 0;
4131 ch->chunk_length = htons(chk->send_size);
4132 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4133 offset += sizeof(struct sctp_chunkhdr);
4134 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) {
4135 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore);
4138 param_len = ntohs(ph->param_length);
4139 if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) {
4143 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)),
4144 (uint8_t *) & cstore);
4145 ptype = ntohs(ph->param_type);
4147 if (param_len > (int)sizeof(cstore)) {
4152 if (num_param > SCTP_MAX_RESET_PARAMS) {
4153 /* hit the max of parameters already sorry.. */
4156 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4157 struct sctp_stream_reset_out_request *req_out;
4159 req_out = (struct sctp_stream_reset_out_request *)ph;
4161 if (stcb->asoc.stream_reset_outstanding) {
4162 seq = ntohl(req_out->response_seq);
4163 if (seq == stcb->asoc.str_reset_seq_out) {
4165 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4168 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4169 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4170 struct sctp_stream_reset_add_strm *str_add;
4172 str_add = (struct sctp_stream_reset_add_strm *)ph;
4174 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4175 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4176 struct sctp_stream_reset_add_strm *str_add;
4178 str_add = (struct sctp_stream_reset_add_strm *)ph;
4180 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4181 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4182 struct sctp_stream_reset_in_request *req_in;
4185 req_in = (struct sctp_stream_reset_in_request *)ph;
4186 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4187 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4188 struct sctp_stream_reset_tsn_request *req_tsn;
4191 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4192 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4194 goto strres_nochunk;
4198 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4199 struct sctp_stream_reset_response *resp;
4202 resp = (struct sctp_stream_reset_response *)ph;
4203 seq = ntohl(resp->response_seq);
4204 result = ntohl(resp->result);
4205 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4207 goto strres_nochunk;
4212 offset += SCTP_SIZE32(param_len);
4213 chk_length -= SCTP_SIZE32(param_len);
4216 /* we have no response free the stuff */
4217 goto strres_nochunk;
4219 /* ok we have a chunk to link in */
4220 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4223 stcb->asoc.ctrl_queue_cnt++;
4228 * Handle a router or endpoints report of a packet loss, there are two ways
4229 * to handle this, either we get the whole packet and must disect it
4230 * ourselves (possibly with truncation and or corruption) or it is a summary
4231 * from a middle box that did the disectting for us.
4234 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4235 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4237 uint32_t bottle_bw, on_queue;
4241 struct sctp_chunk_desc desc;
4242 struct sctp_chunkhdr *ch;
4244 chlen = ntohs(cp->ch.chunk_length);
4245 chlen -= sizeof(struct sctp_pktdrop_chunk);
4246 /* XXX possible chlen underflow */
4249 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4250 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4252 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4253 chlen -= sizeof(struct sctphdr);
4254 /* XXX possible chlen underflow */
4255 memset(&desc, 0, sizeof(desc));
4257 trunc_len = (uint16_t) ntohs(cp->trunc_len);
4258 if (trunc_len > limit) {
4261 /* now the chunks themselves */
4262 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4263 desc.chunk_type = ch->chunk_type;
4264 /* get amount we need to move */
4265 at = ntohs(ch->chunk_length);
4266 if (at < sizeof(struct sctp_chunkhdr)) {
4267 /* corrupt chunk, maybe at the end? */
4268 SCTP_STAT_INCR(sctps_pdrpcrupt);
4271 if (trunc_len == 0) {
4272 /* we are supposed to have all of it */
4274 /* corrupt skip it */
4275 SCTP_STAT_INCR(sctps_pdrpcrupt);
4279 /* is there enough of it left ? */
4280 if (desc.chunk_type == SCTP_DATA) {
4281 if (chlen < (sizeof(struct sctp_data_chunk) +
4282 sizeof(desc.data_bytes))) {
4286 if (chlen < sizeof(struct sctp_chunkhdr)) {
4291 if (desc.chunk_type == SCTP_DATA) {
4292 /* can we get out the tsn? */
4293 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4294 SCTP_STAT_INCR(sctps_pdrpmbda);
4296 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4298 struct sctp_data_chunk *dcp;
4302 dcp = (struct sctp_data_chunk *)ch;
4303 ddp = (uint8_t *) (dcp + 1);
4304 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4305 desc.data_bytes[iii] = ddp[iii];
4307 desc.tsn_ifany = dcp->dp.tsn;
4309 /* nope we are done. */
4310 SCTP_STAT_INCR(sctps_pdrpnedat);
4314 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4315 SCTP_STAT_INCR(sctps_pdrpmbct);
4318 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4319 SCTP_STAT_INCR(sctps_pdrppdbrk);
4322 if (SCTP_SIZE32(at) > chlen) {
4325 chlen -= SCTP_SIZE32(at);
4326 if (chlen < sizeof(struct sctp_chunkhdr)) {
4327 /* done, none left */
4330 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4332 /* Now update any rwnd --- possibly */
4333 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4334 /* From a peer, we get a rwnd report */
4337 SCTP_STAT_INCR(sctps_pdrpfehos);
4339 bottle_bw = ntohl(cp->bottle_bw);
4340 on_queue = ntohl(cp->current_onq);
4341 if (bottle_bw && on_queue) {
4342 /* a rwnd report is in here */
4343 if (bottle_bw > on_queue)
4344 a_rwnd = bottle_bw - on_queue;
4349 stcb->asoc.peers_rwnd = 0;
4351 if (a_rwnd > stcb->asoc.total_flight) {
4352 stcb->asoc.peers_rwnd =
4353 a_rwnd - stcb->asoc.total_flight;
4355 stcb->asoc.peers_rwnd = 0;
4357 if (stcb->asoc.peers_rwnd <
4358 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4359 /* SWS sender side engages */
4360 stcb->asoc.peers_rwnd = 0;
4365 SCTP_STAT_INCR(sctps_pdrpfmbox);
4368 /* now middle boxes in sat networks get a cwnd bump */
4369 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4370 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4371 (stcb->asoc.sat_network)) {
4373 * This is debateable but for sat networks it makes sense
4374 * Note if a T3 timer has went off, we will prohibit any
4375 * changes to cwnd until we exit the t3 loss recovery.
4377 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4378 net, cp, &bottle_bw, &on_queue);
4383 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4384 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4385 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4386 * length of the complete packet outputs: - length: modified to remaining
4387 * length after control processing - netp: modified to new sctp_nets after
4388 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4389 * bad packet,...) otherwise return the tcb for this packet
4392 __attribute__((noinline))
4394 static struct sctp_tcb *
4395 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4396 struct sockaddr *src, struct sockaddr *dst,
4397 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4398 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4399 uint8_t use_mflowid, uint32_t mflowid,
4400 uint32_t vrf_id, uint16_t port)
4402 struct sctp_association *asoc;
4404 int num_chunks = 0; /* number of control chunks processed */
4405 uint32_t chk_length;
4407 int abort_no_unlock = 0;
4411 * How big should this be, and should it be alloc'd? Lets try the
4412 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4413 * until we get into jumbo grams and such..
4415 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4416 struct sctp_tcb *locked_tcb = stcb;
4418 uint32_t auth_offset = 0, auth_len = 0;
4419 int auth_skipped = 0;
4422 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4427 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4428 iphlen, *offset, length, (void *)stcb);
4430 /* validate chunk header length... */
4431 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4432 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4433 ntohs(ch->chunk_length));
4435 SCTP_TCB_UNLOCK(locked_tcb);
4440 * validate the verification tag
4442 vtag_in = ntohl(sh->v_tag);
4445 SCTP_TCB_LOCK_ASSERT(locked_tcb);
4447 if (ch->chunk_type == SCTP_INITIATION) {
4448 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4449 ntohs(ch->chunk_length), vtag_in);
4451 /* protocol error- silently discard... */
4452 SCTP_STAT_INCR(sctps_badvtag);
4454 SCTP_TCB_UNLOCK(locked_tcb);
4458 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4460 * If there is no stcb, skip the AUTH chunk and process
4461 * later after a stcb is found (to validate the lookup was
4464 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4466 !SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4467 /* save this chunk for later processing */
4469 auth_offset = *offset;
4470 auth_len = ntohs(ch->chunk_length);
4472 /* (temporarily) move past this chunk */
4473 *offset += SCTP_SIZE32(auth_len);
4474 if (*offset >= length) {
4475 /* no more data left in the mbuf chain */
4478 SCTP_TCB_UNLOCK(locked_tcb);
4482 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4483 sizeof(struct sctp_chunkhdr), chunk_buf);
4489 SCTP_TCB_UNLOCK(locked_tcb);
4493 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4494 goto process_control_chunks;
4497 * first check if it's an ASCONF with an unknown src addr we
4498 * need to look inside to find the association
4500 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4501 struct sctp_chunkhdr *asconf_ch = ch;
4502 uint32_t asconf_offset = 0, asconf_len = 0;
4504 /* inp's refcount may be reduced */
4505 SCTP_INP_INCR_REF(inp);
4507 asconf_offset = *offset;
4509 asconf_len = ntohs(asconf_ch->chunk_length);
4510 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4512 stcb = sctp_findassociation_ep_asconf(m,
4515 sh, &inp, netp, vrf_id);
4518 asconf_offset += SCTP_SIZE32(asconf_len);
4519 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4520 sizeof(struct sctp_chunkhdr), chunk_buf);
4521 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4524 * reduce inp's refcount if not reduced in
4525 * sctp_findassociation_ep_asconf().
4527 SCTP_INP_DECR_REF(inp);
4532 /* now go back and verify any auth chunk to be sure */
4533 if (auth_skipped && (stcb != NULL)) {
4534 struct sctp_auth_chunk *auth;
4536 auth = (struct sctp_auth_chunk *)
4537 sctp_m_getptr(m, auth_offset,
4538 auth_len, chunk_buf);
4541 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4543 /* auth HMAC failed so dump it */
4546 SCTP_TCB_UNLOCK(locked_tcb);
4550 /* remaining chunks are HMAC checked */
4551 stcb->asoc.authenticated = 1;
4556 /* no association, so it's out of the blue... */
4557 sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp,
4558 use_mflowid, mflowid,
4562 SCTP_TCB_UNLOCK(locked_tcb);
4567 /* ABORT and SHUTDOWN can use either v_tag... */
4568 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4569 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4570 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4571 if ((vtag_in == asoc->my_vtag) ||
4572 ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
4573 (vtag_in == asoc->peer_vtag))) {
4576 /* drop this packet... */
4577 SCTP_STAT_INCR(sctps_badvtag);
4579 SCTP_TCB_UNLOCK(locked_tcb);
4583 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4584 if (vtag_in != asoc->my_vtag) {
4586 * this could be a stale SHUTDOWN-ACK or the
4587 * peer never got the SHUTDOWN-COMPLETE and
4588 * is still hung; we have started a new asoc
4589 * but it won't complete until the shutdown
4593 SCTP_TCB_UNLOCK(locked_tcb);
4595 sctp_handle_ootb(m, iphlen, *offset, src, dst,
4597 use_mflowid, mflowid,
4602 /* for all other chunks, vtag must match */
4603 if (vtag_in != asoc->my_vtag) {
4604 /* invalid vtag... */
4605 SCTPDBG(SCTP_DEBUG_INPUT3,
4606 "invalid vtag: %xh, expect %xh\n",
4607 vtag_in, asoc->my_vtag);
4608 SCTP_STAT_INCR(sctps_badvtag);
4610 SCTP_TCB_UNLOCK(locked_tcb);
4616 } /* end if !SCTP_COOKIE_ECHO */
4618 * process all control chunks...
4620 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4621 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4622 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4623 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4624 /* implied cookie-ack.. we must have lost the ack */
4625 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4626 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4627 stcb->asoc.overall_error_count,
4629 SCTP_FROM_SCTP_INPUT,
4632 stcb->asoc.overall_error_count = 0;
4633 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4636 process_control_chunks:
4637 while (IS_SCTP_CONTROL(ch)) {
4638 /* validate chunk length */
4639 chk_length = ntohs(ch->chunk_length);
4640 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4641 ch->chunk_type, chk_length);
4642 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4643 if (chk_length < sizeof(*ch) ||
4644 (*offset + (int)chk_length) > length) {
4647 SCTP_TCB_UNLOCK(locked_tcb);
4651 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4653 * INIT-ACK only gets the init ack "header" portion only
4654 * because we don't have to process the peer's COOKIE. All
4655 * others get a complete chunk.
4657 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4658 (ch->chunk_type == SCTP_INITIATION)) {
4659 /* get an init-ack chunk */
4660 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4661 sizeof(struct sctp_init_ack_chunk), chunk_buf);
4665 SCTP_TCB_UNLOCK(locked_tcb);
4670 /* For cookies and all other chunks. */
4671 if (chk_length > sizeof(chunk_buf)) {
4673 * use just the size of the chunk buffer so
4674 * the front part of our chunks fit in
4675 * contiguous space up to the chunk buffer
4676 * size (508 bytes). For chunks that need to
4677 * get more than that they must use the
4678 * sctp_m_getptr() function or other means
4679 * (e.g. know how to parse mbuf chains).
4680 * Cookies do this already.
4682 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4683 (sizeof(chunk_buf) - 4),
4688 SCTP_TCB_UNLOCK(locked_tcb);
4693 /* We can fit it all */
4694 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4695 chk_length, chunk_buf);
4697 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4700 SCTP_TCB_UNLOCK(locked_tcb);
4707 /* Save off the last place we got a control from */
4709 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4711 * allow last_control to be NULL if
4712 * ASCONF... ASCONF processing will find the
4715 if ((netp != NULL) && (*netp != NULL))
4716 stcb->asoc.last_control_chunk_from = *netp;
4719 #ifdef SCTP_AUDITING_ENABLED
4720 sctp_audit_log(0xB0, ch->chunk_type);
4723 /* check to see if this chunk required auth, but isn't */
4724 if ((stcb != NULL) &&
4725 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
4726 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4727 !stcb->asoc.authenticated) {
4728 /* "silently" ignore */
4729 SCTP_STAT_INCR(sctps_recvauthmissing);
4732 switch (ch->chunk_type) {
4733 case SCTP_INITIATION:
4734 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4735 /* The INIT chunk must be the only chunk. */
4736 if ((num_chunks > 1) ||
4737 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4738 sctp_abort_association(inp, stcb, m, iphlen,
4740 use_mflowid, mflowid,
4745 /* Honor our resource limit. */
4746 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4747 struct mbuf *op_err;
4749 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
4750 sctp_abort_association(inp, stcb, m, iphlen,
4751 src, dst, sh, op_err,
4752 use_mflowid, mflowid,
4757 sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4758 (struct sctp_init_chunk *)ch, inp,
4759 stcb, &abort_no_unlock,
4760 use_mflowid, mflowid,
4763 if ((!abort_no_unlock) && (locked_tcb)) {
4764 SCTP_TCB_UNLOCK(locked_tcb);
4768 case SCTP_PAD_CHUNK:
4770 case SCTP_INITIATION_ACK:
4771 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4772 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4773 /* We are not interested anymore */
4774 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4777 if (locked_tcb != stcb) {
4779 SCTP_TCB_UNLOCK(locked_tcb);
4783 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4784 so = SCTP_INP_SO(inp);
4785 atomic_add_int(&stcb->asoc.refcnt, 1);
4786 SCTP_TCB_UNLOCK(stcb);
4787 SCTP_SOCKET_LOCK(so, 1);
4788 SCTP_TCB_LOCK(stcb);
4789 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4791 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4792 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4793 SCTP_SOCKET_UNLOCK(so, 1);
4799 /* The INIT-ACK chunk must be the only chunk. */
4800 if ((num_chunks > 1) ||
4801 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4804 SCTP_TCB_UNLOCK(locked_tcb);
4808 if ((netp) && (*netp)) {
4809 ret = sctp_handle_init_ack(m, iphlen, *offset,
4811 (struct sctp_init_ack_chunk *)ch,
4814 use_mflowid, mflowid,
4820 if (abort_no_unlock) {
4824 * Special case, I must call the output routine to
4825 * get the cookie echoed
4827 if ((stcb != NULL) && (ret == 0)) {
4828 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4831 SCTP_TCB_UNLOCK(locked_tcb);
4835 case SCTP_SELECTIVE_ACK:
4837 struct sctp_sack_chunk *sack;
4839 uint32_t a_rwnd, cum_ack;
4840 uint16_t num_seg, num_dup;
4842 int offset_seg, offset_dup;
4844 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4845 SCTP_STAT_INCR(sctps_recvsacks);
4847 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4850 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4851 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4854 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4856 * If we have sent a shutdown-ack, we will pay no
4857 * attention to a sack sent in to us since
4858 * we don't care anymore.
4862 sack = (struct sctp_sack_chunk *)ch;
4863 flags = ch->chunk_flags;
4864 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4865 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4866 num_dup = ntohs(sack->sack.num_dup_tsns);
4867 a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
4868 if (sizeof(struct sctp_sack_chunk) +
4869 num_seg * sizeof(struct sctp_gap_ack_block) +
4870 num_dup * sizeof(uint32_t) != chk_length) {
4871 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4874 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4875 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4876 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4877 cum_ack, num_seg, a_rwnd);
4878 stcb->asoc.seen_a_sack_this_pkt = 1;
4879 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4881 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4882 (stcb->asoc.saw_sack_with_frags == 0) &&
4883 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4884 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4887 * We have a SIMPLE sack having no
4888 * prior segments and data on sent
4889 * queue to be acked.. Use the
4890 * faster path sack processing. We
4891 * also allow window update sacks
4892 * with no missing segments to go
4895 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
4898 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4899 num_seg, 0, num_dup, &abort_now, flags,
4900 cum_ack, a_rwnd, ecne_seen);
4903 /* ABORT signal from sack processing */
4907 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4908 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4909 (stcb->asoc.stream_queue_cnt == 0)) {
4910 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4915 * EY - nr_sack: If the received chunk is an
4918 case SCTP_NR_SELECTIVE_ACK:
4920 struct sctp_nr_sack_chunk *nr_sack;
4922 uint32_t a_rwnd, cum_ack;
4923 uint16_t num_seg, num_nr_seg, num_dup;
4925 int offset_seg, offset_dup;
4927 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
4928 SCTP_STAT_INCR(sctps_recvsacks);
4930 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
4933 if ((stcb->asoc.sctp_nr_sack_on_off == 0) ||
4934 (stcb->asoc.peer_supports_nr_sack == 0)) {
4937 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4938 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
4941 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4943 * If we have sent a shutdown-ack, we will pay no
4944 * attention to a sack sent in to us since
4945 * we don't care anymore.
4949 nr_sack = (struct sctp_nr_sack_chunk *)ch;
4950 flags = ch->chunk_flags;
4951 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4952 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4953 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4954 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4955 a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd);
4956 if (sizeof(struct sctp_nr_sack_chunk) +
4957 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4958 num_dup * sizeof(uint32_t) != chk_length) {
4959 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4962 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4963 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4964 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4965 cum_ack, num_seg, a_rwnd);
4966 stcb->asoc.seen_a_sack_this_pkt = 1;
4967 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4968 (num_seg == 0) && (num_nr_seg == 0) &&
4969 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4970 (stcb->asoc.saw_sack_with_frags == 0) &&
4971 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4972 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
4974 * We have a SIMPLE sack having no
4975 * prior segments and data on sent
4976 * queue to be acked. Use the faster
4977 * path sack processing. We also
4978 * allow window update sacks with no
4979 * missing segments to go this way
4982 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
4983 &abort_now, ecne_seen);
4986 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4987 num_seg, num_nr_seg, num_dup, &abort_now, flags,
4988 cum_ack, a_rwnd, ecne_seen);
4991 /* ABORT signal from sack processing */
4995 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4996 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4997 (stcb->asoc.stream_queue_cnt == 0)) {
4998 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5003 case SCTP_HEARTBEAT_REQUEST:
5004 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
5005 if ((stcb) && netp && *netp) {
5006 SCTP_STAT_INCR(sctps_recvheartbeat);
5007 sctp_send_heartbeat_ack(stcb, m, *offset,
5010 /* He's alive so give him credit */
5011 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5012 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5013 stcb->asoc.overall_error_count,
5015 SCTP_FROM_SCTP_INPUT,
5018 stcb->asoc.overall_error_count = 0;
5021 case SCTP_HEARTBEAT_ACK:
5022 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
5023 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5027 SCTP_TCB_UNLOCK(locked_tcb);
5031 /* He's alive so give him credit */
5032 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5033 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5034 stcb->asoc.overall_error_count,
5036 SCTP_FROM_SCTP_INPUT,
5039 stcb->asoc.overall_error_count = 0;
5040 SCTP_STAT_INCR(sctps_recvheartbeatack);
5042 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5045 case SCTP_ABORT_ASSOCIATION:
5046 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5048 if ((stcb) && netp && *netp)
5049 sctp_handle_abort((struct sctp_abort_chunk *)ch,
5055 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5057 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5060 SCTP_TCB_UNLOCK(locked_tcb);
5064 if (netp && *netp) {
5067 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5068 stcb, *netp, &abort_flag);
5075 case SCTP_SHUTDOWN_ACK:
5076 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", (void *)stcb);
5077 if ((stcb) && (netp) && (*netp))
5078 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5083 case SCTP_OPERATION_ERROR:
5084 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
5085 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
5090 case SCTP_COOKIE_ECHO:
5091 SCTPDBG(SCTP_DEBUG_INPUT3,
5092 "SCTP_COOKIE-ECHO, stcb %p\n", (void *)stcb);
5093 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5096 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5097 /* We are not interested anymore */
5100 SCTP_TCB_UNLOCK(stcb);
5107 * First are we accepting? We do this again here
5108 * since it is possible that a previous endpoint WAS
5109 * listening responded to a INIT-ACK and then
5110 * closed. We opened and bound.. and are now no
5114 if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
5115 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5116 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5117 struct mbuf *op_err;
5119 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
5120 sctp_abort_association(inp, stcb, m, iphlen,
5121 src, dst, sh, op_err,
5122 use_mflowid, mflowid,
5128 struct mbuf *ret_buf;
5129 struct sctp_inpcb *linp;
5138 SCTP_ASOC_CREATE_LOCK(linp);
5139 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5140 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5141 SCTP_ASOC_CREATE_UNLOCK(linp);
5147 sctp_handle_cookie_echo(m, iphlen,
5151 (struct sctp_cookie_echo_chunk *)ch,
5165 SCTP_ASOC_CREATE_UNLOCK(linp);
5167 if (ret_buf == NULL) {
5169 SCTP_TCB_UNLOCK(locked_tcb);
5171 SCTPDBG(SCTP_DEBUG_INPUT3,
5172 "GAK, null buffer\n");
5176 /* if AUTH skipped, see if it verified... */
5181 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5183 * Restart the timer if we have
5186 struct sctp_tmit_chunk *chk;
5188 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5189 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5193 case SCTP_COOKIE_ACK:
5194 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", (void *)stcb);
5195 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5197 SCTP_TCB_UNLOCK(locked_tcb);
5201 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5202 /* We are not interested anymore */
5203 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5206 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5207 so = SCTP_INP_SO(inp);
5208 atomic_add_int(&stcb->asoc.refcnt, 1);
5209 SCTP_TCB_UNLOCK(stcb);
5210 SCTP_SOCKET_LOCK(so, 1);
5211 SCTP_TCB_LOCK(stcb);
5212 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5214 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
5215 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5216 SCTP_SOCKET_UNLOCK(so, 1);
5222 /* He's alive so give him credit */
5223 if ((stcb) && netp && *netp) {
5224 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5225 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5226 stcb->asoc.overall_error_count,
5228 SCTP_FROM_SCTP_INPUT,
5231 stcb->asoc.overall_error_count = 0;
5232 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5236 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5237 /* He's alive so give him credit */
5238 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5241 SCTP_TCB_UNLOCK(locked_tcb);
5247 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5248 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5249 stcb->asoc.overall_error_count,
5251 SCTP_FROM_SCTP_INPUT,
5254 stcb->asoc.overall_error_count = 0;
5255 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5261 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5262 /* He's alive so give him credit */
5263 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5266 SCTP_TCB_UNLOCK(locked_tcb);
5272 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5273 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5274 stcb->asoc.overall_error_count,
5276 SCTP_FROM_SCTP_INPUT,
5279 stcb->asoc.overall_error_count = 0;
5280 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5283 case SCTP_SHUTDOWN_COMPLETE:
5284 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", (void *)stcb);
5285 /* must be first and only chunk */
5286 if ((num_chunks > 1) ||
5287 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5290 SCTP_TCB_UNLOCK(locked_tcb);
5294 if ((stcb) && netp && *netp) {
5295 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5302 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5303 /* He's alive so give him credit */
5305 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5306 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5307 stcb->asoc.overall_error_count,
5309 SCTP_FROM_SCTP_INPUT,
5312 stcb->asoc.overall_error_count = 0;
5313 sctp_handle_asconf(m, *offset, src,
5314 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5318 case SCTP_ASCONF_ACK:
5319 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5320 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5323 SCTP_TCB_UNLOCK(locked_tcb);
5328 if ((stcb) && netp && *netp) {
5329 /* He's alive so give him credit */
5330 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5331 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5332 stcb->asoc.overall_error_count,
5334 SCTP_FROM_SCTP_INPUT,
5337 stcb->asoc.overall_error_count = 0;
5338 sctp_handle_asconf_ack(m, *offset,
5339 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5340 if (abort_no_unlock)
5344 case SCTP_FORWARD_CUM_TSN:
5345 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5346 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5349 SCTP_TCB_UNLOCK(locked_tcb);
5354 /* He's alive so give him credit */
5358 stcb->asoc.overall_error_count = 0;
5359 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5360 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5361 stcb->asoc.overall_error_count,
5363 SCTP_FROM_SCTP_INPUT,
5367 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5368 /* We are not interested anymore */
5369 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5370 so = SCTP_INP_SO(inp);
5371 atomic_add_int(&stcb->asoc.refcnt, 1);
5372 SCTP_TCB_UNLOCK(stcb);
5373 SCTP_SOCKET_LOCK(so, 1);
5374 SCTP_TCB_LOCK(stcb);
5375 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5377 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
5378 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5379 SCTP_SOCKET_UNLOCK(so, 1);
5384 sctp_handle_forward_tsn(stcb,
5385 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5390 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5391 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5392 stcb->asoc.overall_error_count,
5394 SCTP_FROM_SCTP_INPUT,
5397 stcb->asoc.overall_error_count = 0;
5402 case SCTP_STREAM_RESET:
5403 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5404 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5407 SCTP_TCB_UNLOCK(locked_tcb);
5412 if (stcb->asoc.peer_supports_strreset == 0) {
5414 * hmm, peer should have announced this, but
5415 * we will turn it on since he is sending us
5418 stcb->asoc.peer_supports_strreset = 1;
5420 if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5421 /* stop processing */
5426 case SCTP_PACKET_DROPPED:
5427 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5428 /* re-get it all please */
5429 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5432 SCTP_TCB_UNLOCK(locked_tcb);
5437 if (ch && (stcb) && netp && (*netp)) {
5438 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5440 min(chk_length, (sizeof(chunk_buf) - 4)));
5445 case SCTP_AUTHENTICATION:
5446 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5447 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
5451 /* save the first AUTH for later processing */
5452 if (auth_skipped == 0) {
5453 auth_offset = *offset;
5454 auth_len = chk_length;
5457 /* skip this chunk (temporarily) */
5460 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5461 (chk_length > (sizeof(struct sctp_auth_chunk) +
5462 SCTP_AUTH_DIGEST_LEN_MAX))) {
5465 SCTP_TCB_UNLOCK(locked_tcb);
5470 if (got_auth == 1) {
5471 /* skip this chunk... it's already auth'd */
5475 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5477 /* auth HMAC failed so dump the packet */
5481 /* remaining chunks are HMAC checked */
5482 stcb->asoc.authenticated = 1;
5488 /* it's an unknown chunk! */
5489 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5491 struct sctp_paramhdr *phd;
5493 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
5494 0, M_DONTWAIT, 1, MT_DATA);
5496 phd = mtod(mm, struct sctp_paramhdr *);
5498 * We cheat and use param type since
5499 * we did not bother to define a
5500 * error cause struct. They are the
5501 * same basic format with different
5504 phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5505 phd->param_length = htons(chk_length + sizeof(*phd));
5506 SCTP_BUF_LEN(mm) = sizeof(*phd);
5507 SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, chk_length, M_DONTWAIT);
5508 if (SCTP_BUF_NEXT(mm)) {
5509 if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(mm), SCTP_SIZE32(chk_length) - chk_length, NULL)) {
5512 #ifdef SCTP_MBUF_LOGGING
5513 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5516 for (mat = SCTP_BUF_NEXT(mm); mat; mat = SCTP_BUF_NEXT(mat)) {
5517 if (SCTP_BUF_IS_EXTENDED(mat)) {
5518 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5523 sctp_queue_op_err(stcb, mm);
5530 if ((ch->chunk_type & 0x80) == 0) {
5531 /* discard this packet */
5534 } /* else skip this bad chunk and continue... */
5536 } /* switch (ch->chunk_type) */
5540 /* get the next chunk */
5541 *offset += SCTP_SIZE32(chk_length);
5542 if (*offset >= length) {
5543 /* no more data left in the mbuf chain */
5546 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5547 sizeof(struct sctp_chunkhdr), chunk_buf);
5550 SCTP_TCB_UNLOCK(locked_tcb);
5557 if (asconf_cnt > 0 && stcb != NULL) {
5558 sctp_send_asconf_ack(stcb);
5566 __attribute__((noinline))
5569 sctp_validate_no_locks(struct sctp_inpcb *inp)
5571 struct sctp_tcb *lstcb;
5573 LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) {
5574 if (mtx_owned(&lstcb->tcb_mtx)) {
5575 panic("Own lock on stcb at return from input");
5578 if (mtx_owned(&inp->inp_create_mtx)) {
5579 panic("Own create lock on inp");
5581 if (mtx_owned(&inp->inp_mtx)) {
5582 panic("Own inp lock on inp");
5589 * common input chunk processing (v4 and v6)
5592 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5593 struct sockaddr *src, struct sockaddr *dst,
5594 struct sctphdr *sh, struct sctp_chunkhdr *ch,
5595 #if !defined(SCTP_WITH_NO_CSUM)
5596 uint8_t compute_crc,
5599 uint8_t use_mflowid, uint32_t mflowid,
5600 uint32_t vrf_id, uint16_t port)
5603 int fwd_tsn_seen = 0, data_processed = 0;
5604 struct mbuf *m = *mm;
5606 int cnt_ctrl_ready = 0;
5607 struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5608 struct sctp_tcb *stcb = NULL;
5609 struct sctp_nets *net = NULL;
5611 SCTP_STAT_INCR(sctps_recvdatagrams);
5612 #ifdef SCTP_AUDITING_ENABLED
5613 sctp_audit_log(0xE0, 1);
5614 sctp_auditing(0, inp, stcb, net);
5616 #if !defined(SCTP_WITH_NO_CSUM)
5617 if (compute_crc != 0) {
5618 uint32_t check, calc_check;
5620 check = sh->checksum;
5622 calc_check = sctp_calculate_cksum(m, iphlen);
5623 sh->checksum = check;
5624 if (calc_check != check) {
5625 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5626 calc_check, check, (void *)m, length, iphlen);
5627 stcb = sctp_findassociation_addr(m, offset, src, dst,
5628 sh, ch, &inp, &net, vrf_id);
5629 if ((net != NULL) && (port != 0)) {
5630 if (net->port == 0) {
5631 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5635 if ((net != NULL) && (use_mflowid != 0)) {
5636 net->flowid = mflowid;
5641 if ((inp != NULL) && (stcb != NULL)) {
5642 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5643 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5644 } else if ((inp != NULL) && (stcb == NULL)) {
5647 SCTP_STAT_INCR(sctps_badsum);
5648 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5653 /* Destination port of 0 is illegal, based on RFC4960. */
5654 if (sh->dest_port == 0) {
5655 SCTP_STAT_INCR(sctps_hdrops);
5658 stcb = sctp_findassociation_addr(m, offset, src, dst,
5659 sh, ch, &inp, &net, vrf_id);
5660 if ((net != NULL) && (port != 0)) {
5661 if (net->port == 0) {
5662 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5666 if ((net != NULL) && (use_mflowid != 0)) {
5667 net->flowid = mflowid;
5673 SCTP_STAT_INCR(sctps_noport);
5674 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5677 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5678 sctp_send_shutdown_complete2(src, dst, sh,
5679 use_mflowid, mflowid,
5683 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5686 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5687 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5688 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5689 (ch->chunk_type != SCTP_INIT))) {
5690 sctp_send_abort(m, iphlen, src, dst,
5692 use_mflowid, mflowid,
5697 } else if (stcb == NULL) {
5702 * I very much doubt any of the IPSEC stuff will work but I have no
5703 * idea, so I will leave it in place.
5706 switch (dst->sa_family) {
5709 if (ipsec4_in_reject(m, &inp->ip_inp.inp)) {
5710 MODULE_GLOBAL(ipsec4stat).in_polvio++;
5711 SCTP_STAT_INCR(sctps_hdrops);
5718 if (ipsec6_in_reject(m, &inp->ip_inp.inp)) {
5719 MODULE_GLOBAL(ipsec6stat).in_polvio++;
5720 SCTP_STAT_INCR(sctps_hdrops);
5730 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5731 (void *)m, iphlen, offset, length, (void *)stcb);
5733 /* always clear this before beginning a packet */
5734 stcb->asoc.authenticated = 0;
5735 stcb->asoc.seen_a_sack_this_pkt = 0;
5736 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5737 (void *)stcb, stcb->asoc.state);
5739 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5740 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5742 * If we hit here, we had a ref count
5743 * up when the assoc was aborted and the
5744 * timer is clearing out the assoc, we should
5745 * NOT respond to any packet.. its OOTB.
5747 SCTP_TCB_UNLOCK(stcb);
5749 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp,
5750 use_mflowid, mflowid,
5755 if (IS_SCTP_CONTROL(ch)) {
5756 /* process the control portion of the SCTP packet */
5757 /* sa_ignore NO_NULL_CHK */
5758 stcb = sctp_process_control(m, iphlen, &offset, length,
5760 inp, stcb, &net, &fwd_tsn_seen,
5761 use_mflowid, mflowid,
5765 * This covers us if the cookie-echo was there and
5766 * it changes our INP.
5768 inp = stcb->sctp_ep;
5769 if ((net) && (port)) {
5770 if (net->port == 0) {
5771 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5778 * no control chunks, so pre-process DATA chunks (these
5779 * checks are taken care of by control processing)
5783 * if DATA only packet, and auth is required, then punt...
5784 * can't have authenticated without any AUTH (control)
5787 if ((stcb != NULL) &&
5788 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5789 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5790 /* "silently" ignore */
5791 SCTP_STAT_INCR(sctps_recvauthmissing);
5795 /* out of the blue DATA chunk */
5796 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp,
5797 use_mflowid, mflowid,
5801 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5802 /* v_tag mismatch! */
5803 SCTP_STAT_INCR(sctps_badvtag);
5810 * no valid TCB for this packet, or we found it's a bad
5811 * packet while processing control, or we're done with this
5812 * packet (done or skip rest of data), so we drop it...
5817 * DATA chunk processing
5819 /* plow through the data chunks while length > offset */
5822 * Rest should be DATA only. Check authentication state if AUTH for
5825 if ((length > offset) &&
5827 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5828 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5829 !stcb->asoc.authenticated) {
5830 /* "silently" ignore */
5831 SCTP_STAT_INCR(sctps_recvauthmissing);
5832 SCTPDBG(SCTP_DEBUG_AUTH1,
5833 "Data chunk requires AUTH, skipped\n");
5836 if (length > offset) {
5840 * First check to make sure our state is correct. We would
5841 * not get here unless we really did have a tag, so we don't
5842 * abort if this happens, just dump the chunk silently.
5844 switch (SCTP_GET_STATE(&stcb->asoc)) {
5845 case SCTP_STATE_COOKIE_ECHOED:
5847 * we consider data with valid tags in this state
5848 * shows us the cookie-ack was lost. Imply it was
5851 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5852 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5853 stcb->asoc.overall_error_count,
5855 SCTP_FROM_SCTP_INPUT,
5858 stcb->asoc.overall_error_count = 0;
5859 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5861 case SCTP_STATE_COOKIE_WAIT:
5863 * We consider OOTB any data sent during asoc setup.
5865 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp,
5866 use_mflowid, mflowid,
5869 /* sa_ignore NOTREACHED */
5871 case SCTP_STATE_EMPTY: /* should not happen */
5872 case SCTP_STATE_INUSE: /* should not happen */
5873 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5874 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5877 /* sa_ignore NOTREACHED */
5879 case SCTP_STATE_OPEN:
5880 case SCTP_STATE_SHUTDOWN_SENT:
5883 /* plow through the data chunks while length > offset */
5884 retval = sctp_process_data(mm, iphlen, &offset, length,
5886 inp, stcb, net, &high_tsn,
5887 use_mflowid, mflowid,
5891 * The association aborted, NO UNLOCK needed since
5892 * the association is destroyed.
5899 * Anything important needs to have been m_copy'ed in
5903 /* take care of ecn */
5904 if ((data_processed == 1) &&
5905 (stcb->asoc.ecn_allowed == 1) &&
5906 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5907 /* Yep, we need to add a ECNE */
5908 sctp_send_ecn_echo(stcb, net, high_tsn);
5910 if ((data_processed == 0) && (fwd_tsn_seen)) {
5912 uint32_t highest_tsn;
5914 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5915 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5917 highest_tsn = stcb->asoc.highest_tsn_inside_map;
5919 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
5920 stcb->asoc.send_sack = 1;
5921 sctp_sack_check(stcb, was_a_gap);
5922 } else if (fwd_tsn_seen) {
5923 stcb->asoc.send_sack = 1;
5925 /* trigger send of any chunks in queue... */
5927 #ifdef SCTP_AUDITING_ENABLED
5928 sctp_audit_log(0xE0, 2);
5929 sctp_auditing(1, inp, stcb, net);
5931 SCTPDBG(SCTP_DEBUG_INPUT1,
5932 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5933 stcb->asoc.peers_rwnd,
5934 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5935 stcb->asoc.total_flight);
5936 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5937 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5938 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5940 if (cnt_ctrl_ready ||
5942 (stcb->asoc.peers_rwnd > 0 ||
5943 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5944 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5945 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5946 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5948 #ifdef SCTP_AUDITING_ENABLED
5949 sctp_audit_log(0xE0, 3);
5950 sctp_auditing(2, inp, stcb, net);
5954 SCTP_TCB_UNLOCK(stcb);
5956 if (inp_decr != NULL) {
5957 /* reduce ref-count */
5958 SCTP_INP_WLOCK(inp_decr);
5959 SCTP_INP_DECR_REF(inp_decr);
5960 SCTP_INP_WUNLOCK(inp_decr);
5964 sctp_validate_no_locks(inp);
5972 sctp_print_mbuf_chain(struct mbuf *m)
5974 for (; m; m = SCTP_BUF_NEXT(m)) {
5975 SCTP_PRINTF("%p: m_len = %ld\n", (void *)m, SCTP_BUF_LEN(m));
5976 if (SCTP_BUF_IS_EXTENDED(m))
5977 SCTP_PRINTF("%p: extend_size = %d\n", (void *)m, SCTP_BUF_EXTEND_SIZE(m));
5985 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5989 uint32_t vrf_id = 0;
5991 struct sockaddr_in src, dst;
5994 struct sctp_chunkhdr *ch;
5997 #if !defined(SCTP_WITH_NO_CSUM)
5998 uint8_t compute_crc;
6002 uint8_t use_mflowid;
6005 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
6006 SCTP_RELEASE_PKT(i_pak);
6009 m = SCTP_HEADER_TO_CHAIN(i_pak);
6010 #ifdef SCTP_MBUF_LOGGING
6011 /* Log in any input mbufs */
6012 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6015 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
6016 if (SCTP_BUF_IS_EXTENDED(mat)) {
6017 sctp_log_mb(mat, SCTP_MBUF_INPUT);
6022 #ifdef SCTP_PACKET_LOGGING
6023 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
6027 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6028 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
6030 if_name(m->m_pkthdr.rcvif),
6031 m->m_pkthdr.csum_flags);
6032 if (m->m_flags & M_FLOWID) {
6033 mflowid = m->m_pkthdr.flowid;
6039 SCTP_STAT_INCR(sctps_recvpackets);
6040 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
6041 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
6042 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6043 if (SCTP_BUF_LEN(m) < offset) {
6044 if ((m = m_pullup(m, offset)) == NULL) {
6045 SCTP_STAT_INCR(sctps_hdrops);
6049 ip = mtod(m, struct ip *);
6050 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
6051 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
6052 offset -= sizeof(struct sctp_chunkhdr);
6053 memset(&src, 0, sizeof(struct sockaddr_in));
6054 src.sin_family = AF_INET;
6055 src.sin_len = sizeof(struct sockaddr_in);
6056 src.sin_port = sh->src_port;
6057 src.sin_addr = ip->ip_src;
6058 memset(&dst, 0, sizeof(struct sockaddr_in));
6059 dst.sin_family = AF_INET;
6060 dst.sin_len = sizeof(struct sockaddr_in);
6061 dst.sin_port = sh->dest_port;
6062 dst.sin_addr = ip->ip_dst;
6063 length = ip->ip_len + iphlen;
6064 /* Validate mbuf chain length with IP payload length. */
6065 if (SCTP_HEADER_LEN(m) != length) {
6066 SCTPDBG(SCTP_DEBUG_INPUT1,
6067 "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
6068 SCTP_STAT_INCR(sctps_hdrops);
6071 /* SCTP does not allow broadcasts or multicasts */
6072 if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
6075 if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
6078 ecn_bits = ip->ip_tos;
6079 #if defined(SCTP_WITH_NO_CSUM)
6080 SCTP_STAT_INCR(sctps_recvnocrc);
6082 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
6083 SCTP_STAT_INCR(sctps_recvhwcrc);
6086 SCTP_STAT_INCR(sctps_recvswcrc);
6090 sctp_common_input_processing(&m, iphlen, offset, length,
6091 (struct sockaddr *)&src,
6092 (struct sockaddr *)&dst,
6094 #if !defined(SCTP_WITH_NO_CSUM)
6098 use_mflowid, mflowid,
6107 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6108 extern int *sctp_cpuarry;
6113 sctp_input(struct mbuf *m, int off)
6115 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6120 uint32_t flowid, tag;
6123 if (m->m_flags & M_FLOWID) {
6124 flowid = m->m_pkthdr.flowid;
6127 * No flow id built by lower layers fix it so we
6130 offset = off + sizeof(struct sctphdr);
6131 if (SCTP_BUF_LEN(m) < offset) {
6132 if ((m = m_pullup(m, offset)) == NULL) {
6133 SCTP_STAT_INCR(sctps_hdrops);
6137 ip = mtod(m, struct ip *);
6138 sh = (struct sctphdr *)((caddr_t)ip + off);
6139 tag = htonl(sh->v_tag);
6140 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6141 m->m_pkthdr.flowid = flowid;
6142 m->m_flags |= M_FLOWID;
6144 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6145 sctp_queue_to_mcore(m, off, cpu_to_use);
6149 sctp_input_with_port(m, off, 0);