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, struct sctphdr *sh,
84 struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
85 struct sctp_tcb *stcb, int *abort_no_unlock,
86 uint8_t use_mflowid, uint32_t mflowid,
87 uint32_t vrf_id, uint16_t port)
89 struct sctp_init *init;
92 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
98 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
99 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
100 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
101 use_mflowid, mflowid,
104 *abort_no_unlock = 1;
107 /* validate parameters */
109 if (init->initiate_tag == 0) {
110 /* protocol error... send abort */
111 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
112 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
113 use_mflowid, mflowid,
116 *abort_no_unlock = 1;
119 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
120 /* invalid parameter... send abort */
121 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
122 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
123 use_mflowid, mflowid,
126 *abort_no_unlock = 1;
129 if (init->num_inbound_streams == 0) {
130 /* protocol error... send abort */
131 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
132 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
133 use_mflowid, mflowid,
136 *abort_no_unlock = 1;
139 if (init->num_outbound_streams == 0) {
140 /* protocol error... send abort */
141 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
142 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
143 use_mflowid, mflowid,
146 *abort_no_unlock = 1;
149 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
150 offset + ntohs(cp->ch.chunk_length))) {
151 /* auth parameter(s) error... send abort */
152 sctp_abort_association(inp, stcb, m, iphlen, sh, NULL,
153 use_mflowid, mflowid,
156 *abort_no_unlock = 1;
160 * We are only accepting if we have a socket with positive
163 if ((stcb == NULL) &&
164 ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
165 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
166 (inp->sctp_socket == NULL) ||
167 (inp->sctp_socket->so_qlimit == 0))) {
169 * FIX ME ?? What about TCP model and we have a
170 * match/restart case? Actually no fix is needed. the lookup
171 * will always find the existing assoc so stcb would not be
172 * NULL. It may be questionable to do this since we COULD
173 * just send back the INIT-ACK and hope that the app did
174 * accept()'s by the time the COOKIE was sent. But there is
175 * a price to pay for COOKIE generation and I don't want to
176 * pay it on the chance that the app will actually do some
177 * accepts(). The App just looses and should NOT be in this
180 if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
181 sctp_send_abort(m, iphlen, sh, 0, NULL,
182 use_mflowid, mflowid,
187 if ((stcb != NULL) &&
188 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
189 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
190 sctp_send_shutdown_ack(stcb, NULL);
191 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
193 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
194 sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp,
195 use_mflowid, mflowid,
197 ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED));
201 SCTP_INP_RUNLOCK(inp);
206 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
210 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
211 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
218 struct sctp_stream_queue_pending *sp;
219 struct sctp_association *asoc;
222 * This function returns the number of streams that have true unsent
223 * data on them. Note that as it looks through it will clean up any
224 * places that have old data that has been sent but left at top of
228 SCTP_TCB_SEND_LOCK(stcb);
229 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
230 /* Check to see if some data queued */
231 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
232 /* sa_ignore FREED_MEMORY */
233 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
237 if ((sp->msg_is_complete) &&
239 (sp->sender_all_done)) {
241 * We are doing differed cleanup. Last time
242 * through when we took all the data the
243 * sender_all_done was not set.
245 if (sp->put_last_out == 0) {
246 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
247 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
253 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
254 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
256 sctp_free_remote_addr(sp->net);
260 sctp_m_freem(sp->data);
263 sctp_free_a_strmoq(stcb, sp, so_locked);
270 SCTP_TCB_SEND_UNLOCK(stcb);
271 return (unsent_data);
275 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
277 struct sctp_init *init;
278 struct sctp_association *asoc;
279 struct sctp_nets *lnet;
284 /* save off parameters */
285 asoc->peer_vtag = ntohl(init->initiate_tag);
286 asoc->peers_rwnd = ntohl(init->a_rwnd);
288 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
290 if (!TAILQ_EMPTY(&asoc->nets)) {
291 /* update any ssthresh's that may have a default */
292 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
293 lnet->ssthresh = asoc->peers_rwnd;
294 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
295 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
299 SCTP_TCB_SEND_LOCK(stcb);
300 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
302 struct sctp_stream_out *outs;
303 struct sctp_stream_queue_pending *sp, *nsp;
304 struct sctp_tmit_chunk *chk, *nchk;
306 /* abandon the upper streams */
307 newcnt = ntohs(init->num_inbound_streams);
308 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
309 if (chk->rec.data.stream_number >= newcnt) {
310 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
311 asoc->send_queue_cnt--;
312 if (chk->data != NULL) {
313 sctp_free_bufspace(stcb, asoc, chk, 1);
314 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
315 0, chk, SCTP_SO_NOT_LOCKED);
317 sctp_m_freem(chk->data);
321 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
322 /* sa_ignore FREED_MEMORY */
326 for (i = newcnt; i < asoc->pre_open_streams; i++) {
327 outs = &asoc->strmout[i];
328 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
329 TAILQ_REMOVE(&outs->outqueue, sp, next);
330 asoc->stream_queue_cnt--;
331 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
332 stcb, 0, sp, SCTP_SO_NOT_LOCKED);
334 sctp_m_freem(sp->data);
338 sctp_free_remote_addr(sp->net);
342 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
343 /* sa_ignore FREED_MEMORY */
347 /* cut back the count */
348 asoc->pre_open_streams = newcnt;
350 SCTP_TCB_SEND_UNLOCK(stcb);
351 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
353 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
354 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
355 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
356 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
358 /* This is the next one we expect */
359 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
361 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
362 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
364 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
365 /* open the requested streams */
367 if (asoc->strmin != NULL) {
368 /* Free the old ones */
369 struct sctp_queued_to_read *ctl, *nctl;
371 for (i = 0; i < asoc->streamincnt; i++) {
372 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) {
373 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
374 sctp_free_remote_addr(ctl->whoFrom);
376 sctp_m_freem(ctl->data);
378 sctp_free_a_readq(stcb, ctl);
381 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
383 asoc->streamincnt = ntohs(init->num_outbound_streams);
384 if (asoc->streamincnt > MAX_SCTP_STREAMS) {
385 asoc->streamincnt = MAX_SCTP_STREAMS;
387 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
388 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
389 if (asoc->strmin == NULL) {
390 /* we didn't get memory for the streams! */
391 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
394 for (i = 0; i < asoc->streamincnt; i++) {
395 asoc->strmin[i].stream_no = i;
396 asoc->strmin[i].last_sequence_delivered = 0xffff;
398 * U-stream ranges will be set when the cookie is unpacked.
399 * Or for the INIT sender they are un set (if pr-sctp not
400 * supported) when the INIT-ACK arrives.
402 TAILQ_INIT(&asoc->strmin[i].inqueue);
403 asoc->strmin[i].delivery_started = 0;
406 * load_address_from_init will put the addresses into the
407 * association when the COOKIE is processed or the INIT-ACK is
408 * processed. Both types of COOKIE's existing and new call this
409 * routine. It will remove addresses that are no longer in the
410 * association (for the restarting case where addresses are
411 * removed). Up front when the INIT arrives we will discard it if it
412 * is a restart and new addresses have been added.
414 /* sa_ignore MEMLEAK */
419 * INIT-ACK message processing/consumption returns value < 0 on error
422 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
423 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
424 struct sctp_nets *net, int *abort_no_unlock,
425 uint8_t use_mflowid, uint32_t mflowid,
428 struct sctp_association *asoc;
430 int retval, abort_flag;
431 uint32_t initack_limit;
432 int nat_friendly = 0;
434 /* First verify that we have no illegal param's */
438 op_err = sctp_arethere_unrecognized_parameters(m,
439 (offset + sizeof(struct sctp_init_chunk)),
440 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
442 /* Send an abort and notify peer */
443 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
444 *abort_no_unlock = 1;
448 asoc->peer_supports_nat = (uint8_t) nat_friendly;
449 /* process the peer's parameters in the INIT-ACK */
450 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
454 initack_limit = offset + ntohs(cp->ch.chunk_length);
455 /* load all addresses */
456 if ((retval = sctp_load_addresses_from_init(stcb, m,
457 (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh,
459 /* Huh, we should abort */
460 SCTPDBG(SCTP_DEBUG_INPUT1,
461 "Load addresses from INIT causes an abort %d\n",
463 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, NULL,
464 use_mflowid, mflowid,
466 *abort_no_unlock = 1;
469 /* if the peer doesn't support asconf, flush the asconf queue */
470 if (asoc->peer_supports_asconf == 0) {
471 struct sctp_asconf_addr *param, *nparam;
473 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
474 TAILQ_REMOVE(&asoc->asconf_queue, param, next);
475 SCTP_FREE(param, SCTP_M_ASC_ADDR);
478 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
479 stcb->asoc.local_hmacs);
481 sctp_queue_op_err(stcb, op_err);
482 /* queuing will steal away the mbuf chain to the out queue */
485 /* extract the cookie and queue it to "echo" it back... */
486 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
487 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
488 stcb->asoc.overall_error_count,
490 SCTP_FROM_SCTP_INPUT,
493 stcb->asoc.overall_error_count = 0;
494 net->error_count = 0;
497 * Cancel the INIT timer, We do this first before queueing the
498 * cookie. We always cancel at the primary to assue that we are
499 * canceling the timer started by the INIT which always goes to the
502 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
503 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
505 /* calculate the RTO */
506 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy,
507 SCTP_RTT_FROM_NON_DATA);
509 retval = sctp_send_cookie_echo(m, offset, stcb, net);
512 * No cookie, we probably should send a op error. But in any
513 * case if there is no cookie in the INIT-ACK, we can
514 * abandon the peer, its broke.
517 /* We abort with an error of missing mandatory param */
519 sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM);
522 * Expand beyond to include the mandatory
525 struct sctp_inv_mandatory_param *mp;
527 SCTP_BUF_LEN(op_err) =
528 sizeof(struct sctp_inv_mandatory_param);
530 struct sctp_inv_mandatory_param *);
531 /* Subtract the reserved param */
533 htons(sizeof(struct sctp_inv_mandatory_param) - 2);
534 mp->num_param = htonl(1);
535 mp->param = htons(SCTP_STATE_COOKIE);
538 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
540 use_mflowid, mflowid,
542 *abort_no_unlock = 1;
550 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
551 struct sctp_tcb *stcb, struct sctp_nets *net)
553 struct sockaddr_storage store;
554 struct sctp_nets *r_net, *f_net;
557 uint16_t old_error_counter;
560 struct sockaddr_in *sin;
564 struct sockaddr_in6 *sin6;
568 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
572 memset(&store, 0, sizeof(store));
573 switch (cp->heartbeat.hb_info.addr_family) {
576 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
577 sin = (struct sockaddr_in *)&store;
578 sin->sin_family = cp->heartbeat.hb_info.addr_family;
579 sin->sin_len = cp->heartbeat.hb_info.addr_len;
580 sin->sin_port = stcb->rport;
581 memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address,
582 sizeof(sin->sin_addr));
590 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
591 sin6 = (struct sockaddr_in6 *)&store;
592 sin6->sin6_family = cp->heartbeat.hb_info.addr_family;
593 sin6->sin6_len = cp->heartbeat.hb_info.addr_len;
594 sin6->sin6_port = stcb->rport;
595 memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address,
596 sizeof(sin6->sin6_addr));
605 r_net = sctp_findnet(stcb, (struct sockaddr *)&store);
607 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
610 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
611 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
612 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
614 * If the its a HB and it's random value is correct when can
615 * confirm the destination.
617 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
618 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
619 stcb->asoc.primary_destination = r_net;
620 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
621 f_net = TAILQ_FIRST(&stcb->asoc.nets);
622 if (f_net != r_net) {
624 * first one on the list is NOT the primary
625 * sctp_cmpaddr() is much more efficent if
626 * the primary is the first on the list,
629 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
630 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
634 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
635 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
636 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
637 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
639 old_error_counter = r_net->error_count;
640 r_net->error_count = 0;
641 r_net->hb_responded = 1;
642 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
643 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
644 /* Now lets do a RTO with this */
645 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy,
646 SCTP_RTT_FROM_NON_DATA);
647 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
648 r_net->dest_state |= SCTP_ADDR_REACHABLE;
649 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
650 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
652 if (r_net->dest_state & SCTP_ADDR_PF) {
653 r_net->dest_state &= ~SCTP_ADDR_PF;
654 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
656 if (old_error_counter > 0) {
657 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
658 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
660 if (r_net == stcb->asoc.primary_destination) {
661 if (stcb->asoc.alternate) {
662 /* release the alternate, primary is good */
663 sctp_free_remote_addr(stcb->asoc.alternate);
664 stcb->asoc.alternate = NULL;
667 /* Mobility adaptation */
669 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
670 SCTP_MOBILITY_BASE) ||
671 sctp_is_mobility_feature_on(stcb->sctp_ep,
672 SCTP_MOBILITY_FASTHANDOFF)) &&
673 sctp_is_mobility_feature_on(stcb->sctp_ep,
674 SCTP_MOBILITY_PRIM_DELETED)) {
676 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7);
677 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
678 SCTP_MOBILITY_FASTHANDOFF)) {
679 sctp_assoc_immediate_retrans(stcb,
680 stcb->asoc.primary_destination);
682 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
683 SCTP_MOBILITY_BASE)) {
684 sctp_move_chunks_from_net(stcb,
685 stcb->asoc.deleted_primary);
687 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
688 stcb->asoc.deleted_primary);
694 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
697 * return 0 means we want you to proceed with the abort non-zero
698 * means no abort processing
700 struct sctpasochead *head;
702 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
703 /* generate a new vtag and send init */
704 LIST_REMOVE(stcb, sctp_asocs);
705 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
706 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
708 * put it in the bucket in the vtag hash of assoc's for the
711 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
712 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
715 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
717 * treat like a case where the cookie expired i.e.: - dump
718 * current cookie. - generate a new vtag. - resend init.
720 /* generate a new vtag and send init */
721 LIST_REMOVE(stcb, sctp_asocs);
722 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
723 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
724 sctp_stop_all_cookie_timers(stcb);
725 sctp_toss_old_cookies(stcb, &stcb->asoc);
726 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
727 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
729 * put it in the bucket in the vtag hash of assoc's for the
732 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
733 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
740 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
741 struct sctp_nets *net)
744 * return 0 means we want you to proceed with the abort non-zero
745 * means no abort processing
747 if (stcb->asoc.peer_supports_auth == 0) {
748 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
751 sctp_asconf_send_nat_state_update(stcb, net);
757 sctp_handle_abort(struct sctp_abort_chunk *abort,
758 struct sctp_tcb *stcb, struct sctp_nets *net)
760 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
767 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
771 len = ntohs(abort->ch.chunk_length);
772 if (len > sizeof(struct sctp_chunkhdr)) {
774 * Need to check the cause codes for our two magic nat
775 * aborts which don't kill the assoc necessarily.
777 struct sctp_missing_nat_state *natc;
779 natc = (struct sctp_missing_nat_state *)(abort + 1);
780 error = ntohs(natc->cause);
781 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
782 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
783 abort->ch.chunk_flags);
784 if (sctp_handle_nat_colliding_state(stcb)) {
787 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
788 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
789 abort->ch.chunk_flags);
790 if (sctp_handle_nat_missing_state(stcb, net)) {
797 /* stop any receive timers */
798 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
799 /* notify user of the abort and clean up... */
800 sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
802 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
803 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
804 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
805 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
807 #ifdef SCTP_ASOCLOG_OF_TSNS
808 sctp_print_out_track_log(stcb);
810 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
811 so = SCTP_INP_SO(stcb->sctp_ep);
812 atomic_add_int(&stcb->asoc.refcnt, 1);
813 SCTP_TCB_UNLOCK(stcb);
814 SCTP_SOCKET_LOCK(so, 1);
816 atomic_subtract_int(&stcb->asoc.refcnt, 1);
818 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
819 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
820 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
821 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
822 SCTP_SOCKET_UNLOCK(so, 1);
824 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
828 sctp_start_net_timers(struct sctp_tcb *stcb)
830 uint32_t cnt_hb_sent;
831 struct sctp_nets *net;
834 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
836 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
837 * If the dest in unconfirmed send a hb as well if under
838 * max_hb_burst have been sent.
840 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
841 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
842 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
843 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
844 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
849 sctp_chunk_output(stcb->sctp_ep, stcb,
850 SCTP_OUTPUT_FROM_COOKIE_ACK,
857 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
858 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
860 struct sctp_association *asoc;
861 int some_on_streamwheel;
863 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
868 SCTPDBG(SCTP_DEBUG_INPUT2,
869 "sctp_handle_shutdown: handling SHUTDOWN\n");
873 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
874 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
877 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
878 /* Shutdown NOT the expected size */
881 sctp_update_acked(stcb, cp, abort_flag);
886 if (asoc->control_pdapi) {
888 * With a normal shutdown we assume the end of last record.
890 SCTP_INP_READ_LOCK(stcb->sctp_ep);
891 asoc->control_pdapi->end_added = 1;
892 asoc->control_pdapi->pdapi_aborted = 1;
893 asoc->control_pdapi = NULL;
894 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
895 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
896 so = SCTP_INP_SO(stcb->sctp_ep);
897 atomic_add_int(&stcb->asoc.refcnt, 1);
898 SCTP_TCB_UNLOCK(stcb);
899 SCTP_SOCKET_LOCK(so, 1);
901 atomic_subtract_int(&stcb->asoc.refcnt, 1);
902 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
903 /* assoc was freed while we were unlocked */
904 SCTP_SOCKET_UNLOCK(so, 1);
908 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
909 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
910 SCTP_SOCKET_UNLOCK(so, 1);
913 /* goto SHUTDOWN_RECEIVED state to block new requests */
914 if (stcb->sctp_socket) {
915 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
916 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
917 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
918 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
919 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
921 * notify upper layer that peer has initiated a
924 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
927 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
930 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
932 * stop the shutdown timer, since we WILL move to
935 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
937 /* Now is there unsent data on a stream somewhere? */
938 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
940 if (!TAILQ_EMPTY(&asoc->send_queue) ||
941 !TAILQ_EMPTY(&asoc->sent_queue) ||
942 some_on_streamwheel) {
943 /* By returning we will push more data out */
946 /* no outstanding data to send, so move on... */
947 /* send SHUTDOWN-ACK */
948 sctp_send_shutdown_ack(stcb, net);
949 /* move to SHUTDOWN-ACK-SENT state */
950 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
951 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
952 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
954 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
955 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
956 sctp_stop_timers_for_shutdown(stcb);
957 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
963 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
964 struct sctp_tcb *stcb,
965 struct sctp_nets *net)
967 struct sctp_association *asoc;
969 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
972 so = SCTP_INP_SO(stcb->sctp_ep);
974 SCTPDBG(SCTP_DEBUG_INPUT2,
975 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
980 /* process according to association state */
981 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
982 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
983 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
984 sctp_send_shutdown_complete(stcb, net, 1);
985 SCTP_TCB_UNLOCK(stcb);
988 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
989 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
990 /* unexpected SHUTDOWN-ACK... so ignore... */
991 SCTP_TCB_UNLOCK(stcb);
994 if (asoc->control_pdapi) {
996 * With a normal shutdown we assume the end of last record.
998 SCTP_INP_READ_LOCK(stcb->sctp_ep);
999 asoc->control_pdapi->end_added = 1;
1000 asoc->control_pdapi->pdapi_aborted = 1;
1001 asoc->control_pdapi = NULL;
1002 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1003 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1004 atomic_add_int(&stcb->asoc.refcnt, 1);
1005 SCTP_TCB_UNLOCK(stcb);
1006 SCTP_SOCKET_LOCK(so, 1);
1007 SCTP_TCB_LOCK(stcb);
1008 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1009 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1010 /* assoc was freed while we were unlocked */
1011 SCTP_SOCKET_UNLOCK(so, 1);
1015 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1016 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1017 SCTP_SOCKET_UNLOCK(so, 1);
1020 /* are the queues empty? */
1021 if (!TAILQ_EMPTY(&asoc->send_queue) ||
1022 !TAILQ_EMPTY(&asoc->sent_queue) ||
1023 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
1024 sctp_report_all_outbound(stcb, 0, 0, SCTP_SO_NOT_LOCKED);
1026 /* stop the timer */
1027 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
1028 /* send SHUTDOWN-COMPLETE */
1029 sctp_send_shutdown_complete(stcb, net, 0);
1030 /* notify upper layer protocol */
1031 if (stcb->sctp_socket) {
1032 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1033 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1034 stcb->sctp_socket->so_snd.sb_cc = 0;
1036 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1038 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1039 /* free the TCB but first save off the ep */
1040 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1041 atomic_add_int(&stcb->asoc.refcnt, 1);
1042 SCTP_TCB_UNLOCK(stcb);
1043 SCTP_SOCKET_LOCK(so, 1);
1044 SCTP_TCB_LOCK(stcb);
1045 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1047 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1048 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1049 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1050 SCTP_SOCKET_UNLOCK(so, 1);
1055 * Skip past the param header and then we will find the chunk that caused the
1056 * problem. There are two possiblities ASCONF or FWD-TSN other than that and
1057 * our peer must be broken.
1060 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1061 struct sctp_nets *net)
1063 struct sctp_chunkhdr *chk;
1065 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1066 switch (chk->chunk_type) {
1067 case SCTP_ASCONF_ACK:
1069 sctp_asconf_cleanup(stcb, net);
1071 case SCTP_FORWARD_CUM_TSN:
1072 stcb->asoc.peer_supports_prsctp = 0;
1075 SCTPDBG(SCTP_DEBUG_INPUT2,
1076 "Peer does not support chunk type %d(%x)??\n",
1077 chk->chunk_type, (uint32_t) chk->chunk_type);
1083 * Skip past the param header and then we will find the param that caused the
1084 * problem. There are a number of param's in a ASCONF OR the prsctp param
1085 * these will turn of specific features.
1088 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1090 struct sctp_paramhdr *pbad;
1093 switch (ntohs(pbad->param_type)) {
1095 case SCTP_PRSCTP_SUPPORTED:
1096 stcb->asoc.peer_supports_prsctp = 0;
1098 case SCTP_SUPPORTED_CHUNK_EXT:
1100 /* draft-ietf-tsvwg-addip-sctp */
1101 case SCTP_HAS_NAT_SUPPORT:
1102 stcb->asoc.peer_supports_nat = 0;
1104 case SCTP_ADD_IP_ADDRESS:
1105 case SCTP_DEL_IP_ADDRESS:
1106 case SCTP_SET_PRIM_ADDR:
1107 stcb->asoc.peer_supports_asconf = 0;
1109 case SCTP_SUCCESS_REPORT:
1110 case SCTP_ERROR_CAUSE_IND:
1111 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1112 SCTPDBG(SCTP_DEBUG_INPUT2,
1113 "Turning off ASCONF to this strange peer\n");
1114 stcb->asoc.peer_supports_asconf = 0;
1117 SCTPDBG(SCTP_DEBUG_INPUT2,
1118 "Peer does not support param type %d(%x)??\n",
1119 pbad->param_type, (uint32_t) pbad->param_type);
1125 sctp_handle_error(struct sctp_chunkhdr *ch,
1126 struct sctp_tcb *stcb, struct sctp_nets *net)
1129 struct sctp_paramhdr *phdr;
1130 uint16_t error, error_type;
1132 struct sctp_association *asoc;
1135 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1140 /* parse through all of the errors and process */
1142 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1143 sizeof(struct sctp_chunkhdr));
1144 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1146 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1147 /* Process an Error Cause */
1148 error_type = ntohs(phdr->param_type);
1149 error_len = ntohs(phdr->param_length);
1150 if ((error_len > chklen) || (error_len == 0)) {
1151 /* invalid param length for this param */
1152 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1157 /* report the first error cause */
1160 switch (error_type) {
1161 case SCTP_CAUSE_INVALID_STREAM:
1162 case SCTP_CAUSE_MISSING_PARAM:
1163 case SCTP_CAUSE_INVALID_PARAM:
1164 case SCTP_CAUSE_NO_USER_DATA:
1165 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1168 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1169 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1171 if (sctp_handle_nat_colliding_state(stcb)) {
1175 case SCTP_CAUSE_NAT_MISSING_STATE:
1176 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1178 if (sctp_handle_nat_missing_state(stcb, net)) {
1182 case SCTP_CAUSE_STALE_COOKIE:
1184 * We only act if we have echoed a cookie and are
1187 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1190 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1191 /* Save the time doubled */
1192 asoc->cookie_preserve_req = ntohl(*p) << 1;
1193 asoc->stale_cookie_count++;
1194 if (asoc->stale_cookie_count >
1195 asoc->max_init_times) {
1196 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1197 /* now free the asoc */
1198 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1199 so = SCTP_INP_SO(stcb->sctp_ep);
1200 atomic_add_int(&stcb->asoc.refcnt, 1);
1201 SCTP_TCB_UNLOCK(stcb);
1202 SCTP_SOCKET_LOCK(so, 1);
1203 SCTP_TCB_LOCK(stcb);
1204 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1206 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1207 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1208 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1209 SCTP_SOCKET_UNLOCK(so, 1);
1213 /* blast back to INIT state */
1214 sctp_toss_old_cookies(stcb, &stcb->asoc);
1215 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1216 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1217 sctp_stop_all_cookie_timers(stcb);
1218 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1221 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1223 * Nothing we can do here, we don't do hostname
1224 * addresses so if the peer does not like my IPv6
1225 * (or IPv4 for that matter) it does not matter. If
1226 * they don't support that type of address, they can
1227 * NOT possibly get that packet type... i.e. with no
1228 * IPv6 you can't recieve a IPv6 packet. so we can
1229 * safely ignore this one. If we ever added support
1230 * for HOSTNAME Addresses, then we would need to do
1234 case SCTP_CAUSE_UNRECOG_CHUNK:
1235 sctp_process_unrecog_chunk(stcb, phdr, net);
1237 case SCTP_CAUSE_UNRECOG_PARAM:
1238 sctp_process_unrecog_param(stcb, phdr);
1240 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1242 * We ignore this since the timer will drive out a
1243 * new cookie anyway and there timer will drive us
1244 * to send a SHUTDOWN_COMPLETE. We can't send one
1245 * here since we don't have their tag.
1248 case SCTP_CAUSE_DELETING_LAST_ADDR:
1249 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1250 case SCTP_CAUSE_DELETING_SRC_ADDR:
1252 * We should NOT get these here, but in a
1255 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1258 case SCTP_CAUSE_OUT_OF_RESC:
1260 * And what, pray tell do we do with the fact that
1261 * the peer is out of resources? Not really sure we
1262 * could do anything but abort. I suspect this
1263 * should have came WITH an abort instead of in a
1268 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1272 adjust = SCTP_SIZE32(error_len);
1274 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1276 sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, error, ch, SCTP_SO_NOT_LOCKED);
1281 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
1282 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1283 struct sctp_nets *net, int *abort_no_unlock,
1284 uint8_t use_mflowid, uint32_t mflowid,
1287 struct sctp_init_ack *init_ack;
1288 struct mbuf *op_err;
1290 SCTPDBG(SCTP_DEBUG_INPUT2,
1291 "sctp_handle_init_ack: handling INIT-ACK\n");
1294 SCTPDBG(SCTP_DEBUG_INPUT2,
1295 "sctp_handle_init_ack: TCB is null\n");
1298 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1299 /* Invalid length */
1300 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1301 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1303 use_mflowid, mflowid,
1305 *abort_no_unlock = 1;
1308 init_ack = &cp->init;
1309 /* validate parameters */
1310 if (init_ack->initiate_tag == 0) {
1311 /* protocol error... send an abort */
1312 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1313 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1315 use_mflowid, mflowid,
1317 *abort_no_unlock = 1;
1320 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1321 /* protocol error... send an abort */
1322 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1323 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1325 use_mflowid, mflowid,
1327 *abort_no_unlock = 1;
1330 if (init_ack->num_inbound_streams == 0) {
1331 /* protocol error... send an abort */
1332 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1333 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1335 use_mflowid, mflowid,
1337 *abort_no_unlock = 1;
1340 if (init_ack->num_outbound_streams == 0) {
1341 /* protocol error... send an abort */
1342 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1343 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1345 use_mflowid, mflowid,
1347 *abort_no_unlock = 1;
1350 /* process according to association state... */
1351 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1352 case SCTP_STATE_COOKIE_WAIT:
1353 /* this is the expected state for this chunk */
1354 /* process the INIT-ACK parameters */
1355 if (stcb->asoc.primary_destination->dest_state &
1356 SCTP_ADDR_UNCONFIRMED) {
1358 * The primary is where we sent the INIT, we can
1359 * always consider it confirmed when the INIT-ACK is
1360 * returned. Do this before we load addresses
1363 stcb->asoc.primary_destination->dest_state &=
1364 ~SCTP_ADDR_UNCONFIRMED;
1365 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1366 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1368 if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb,
1369 net, abort_no_unlock,
1370 use_mflowid, mflowid,
1372 /* error in parsing parameters */
1375 /* update our state */
1376 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1377 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1379 /* reset the RTO calc */
1380 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1381 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1382 stcb->asoc.overall_error_count,
1384 SCTP_FROM_SCTP_INPUT,
1387 stcb->asoc.overall_error_count = 0;
1388 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1390 * collapse the init timer back in case of a exponential
1393 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1396 * the send at the end of the inbound data processing will
1397 * cause the cookie to be sent
1400 case SCTP_STATE_SHUTDOWN_SENT:
1401 /* incorrect state... discard */
1403 case SCTP_STATE_COOKIE_ECHOED:
1404 /* incorrect state... discard */
1406 case SCTP_STATE_OPEN:
1407 /* incorrect state... discard */
1409 case SCTP_STATE_EMPTY:
1410 case SCTP_STATE_INUSE:
1412 /* incorrect state... discard */
1416 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1420 static struct sctp_tcb *
1421 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1422 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1423 struct sctp_inpcb *inp, struct sctp_nets **netp,
1424 struct sockaddr *init_src, int *notification,
1425 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1426 uint8_t use_mflowid, uint32_t mflowid,
1427 uint32_t vrf_id, uint16_t port);
1431 * handle a state cookie for an existing association m: input packet mbuf
1432 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1433 * "split" mbuf and the cookie signature does not exist offset: offset into
1434 * mbuf to the cookie-echo chunk
1436 static struct sctp_tcb *
1437 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1438 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1439 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1440 struct sockaddr *init_src, int *notification,
1441 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1442 uint8_t use_mflowid, uint32_t mflowid,
1443 uint32_t vrf_id, uint16_t port)
1445 struct sctp_association *asoc;
1446 struct sctp_init_chunk *init_cp, init_buf;
1447 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1448 struct sctp_nets *net;
1449 struct mbuf *op_err;
1450 struct sctp_paramhdr *ph;
1451 int init_offset, initack_offset, i;
1457 /* I know that the TCB is non-NULL from the caller */
1459 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1460 if (asoc->cookie_how[how_indx] == 0)
1463 if (how_indx < sizeof(asoc->cookie_how)) {
1464 asoc->cookie_how[how_indx] = 1;
1466 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1467 /* SHUTDOWN came in after sending INIT-ACK */
1468 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1469 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1470 0, M_DONTWAIT, 1, MT_DATA);
1471 if (op_err == NULL) {
1476 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1477 ph = mtod(op_err, struct sctp_paramhdr *);
1478 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
1479 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1480 sctp_send_operr_to(m, sh, cookie->peers_vtag, op_err,
1481 use_mflowid, mflowid,
1483 if (how_indx < sizeof(asoc->cookie_how))
1484 asoc->cookie_how[how_indx] = 2;
1488 * find and validate the INIT chunk in the cookie (peer's info) the
1489 * INIT should start after the cookie-echo header struct (chunk
1490 * header, state cookie header struct)
1492 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1494 init_cp = (struct sctp_init_chunk *)
1495 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1496 (uint8_t *) & init_buf);
1497 if (init_cp == NULL) {
1498 /* could not pull a INIT chunk in cookie */
1501 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1505 * find and validate the INIT-ACK chunk in the cookie (my info) the
1506 * INIT-ACK follows the INIT chunk
1508 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1509 initack_cp = (struct sctp_init_ack_chunk *)
1510 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1511 (uint8_t *) & initack_buf);
1512 if (initack_cp == NULL) {
1513 /* could not pull INIT-ACK chunk in cookie */
1516 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1519 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1520 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1522 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1523 * to get into the OPEN state
1525 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1527 * Opps, this means that we somehow generated two vtag's
1528 * the same. I.e. we did:
1530 * <---INIT(tag=a)------
1531 * ----INIT-ACK(tag=t)-->
1532 * ----INIT(tag=t)------> *1
1533 * <---INIT-ACK(tag=a)---
1534 * <----CE(tag=t)------------- *2
1536 * At point *1 we should be generating a different
1537 * tag t'. Which means we would throw away the CE and send
1538 * ours instead. Basically this is case C (throw away side).
1540 if (how_indx < sizeof(asoc->cookie_how))
1541 asoc->cookie_how[how_indx] = 17;
1545 switch SCTP_GET_STATE
1547 case SCTP_STATE_COOKIE_WAIT:
1548 case SCTP_STATE_COOKIE_ECHOED:
1550 * INIT was sent but got a COOKIE_ECHO with the
1551 * correct tags... just accept it...but we must
1552 * process the init so that we can make sure we have
1553 * the right seq no's.
1555 /* First we must process the INIT !! */
1556 retval = sctp_process_init(init_cp, stcb);
1558 if (how_indx < sizeof(asoc->cookie_how))
1559 asoc->cookie_how[how_indx] = 3;
1562 /* we have already processed the INIT so no problem */
1563 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1564 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1565 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1566 /* update current state */
1567 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1568 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1570 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1572 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1573 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1574 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1575 stcb->sctp_ep, stcb, asoc->primary_destination);
1577 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1578 sctp_stop_all_cookie_timers(stcb);
1579 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1580 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1581 (inp->sctp_socket->so_qlimit == 0)
1583 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1588 * Here is where collision would go if we
1589 * did a connect() and instead got a
1590 * init/init-ack/cookie done before the
1591 * init-ack came back..
1593 stcb->sctp_ep->sctp_flags |=
1594 SCTP_PCB_FLAGS_CONNECTED;
1595 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1596 so = SCTP_INP_SO(stcb->sctp_ep);
1597 atomic_add_int(&stcb->asoc.refcnt, 1);
1598 SCTP_TCB_UNLOCK(stcb);
1599 SCTP_SOCKET_LOCK(so, 1);
1600 SCTP_TCB_LOCK(stcb);
1601 atomic_add_int(&stcb->asoc.refcnt, -1);
1602 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1603 SCTP_SOCKET_UNLOCK(so, 1);
1607 soisconnected(stcb->sctp_socket);
1608 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1609 SCTP_SOCKET_UNLOCK(so, 1);
1612 /* notify upper layer */
1613 *notification = SCTP_NOTIFY_ASSOC_UP;
1615 * since we did not send a HB make sure we don't
1618 net->hb_responded = 1;
1619 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1620 &cookie->time_entered,
1621 sctp_align_unsafe_makecopy,
1622 SCTP_RTT_FROM_NON_DATA);
1624 if (stcb->asoc.sctp_autoclose_ticks &&
1625 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1626 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1632 * we're in the OPEN state (or beyond), so peer must
1633 * have simply lost the COOKIE-ACK
1637 sctp_stop_all_cookie_timers(stcb);
1639 * We ignore the return code here.. not sure if we should
1640 * somehow abort.. but we do have an existing asoc. This
1641 * really should not fail.
1643 if (sctp_load_addresses_from_init(stcb, m,
1644 init_offset + sizeof(struct sctp_init_chunk),
1645 initack_offset, sh, init_src)) {
1646 if (how_indx < sizeof(asoc->cookie_how))
1647 asoc->cookie_how[how_indx] = 4;
1650 /* respond with a COOKIE-ACK */
1651 sctp_toss_old_cookies(stcb, asoc);
1652 sctp_send_cookie_ack(stcb);
1653 if (how_indx < sizeof(asoc->cookie_how))
1654 asoc->cookie_how[how_indx] = 5;
1657 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1658 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1659 cookie->tie_tag_my_vtag == 0 &&
1660 cookie->tie_tag_peer_vtag == 0) {
1662 * case C in Section 5.2.4 Table 2: XMOO silently discard
1664 if (how_indx < sizeof(asoc->cookie_how))
1665 asoc->cookie_how[how_indx] = 6;
1669 * If nat support, and the below and stcb is established, send back
1670 * a ABORT(colliding state) if we are established.
1672 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1673 (asoc->peer_supports_nat) &&
1674 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1675 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1676 (asoc->peer_vtag == 0)))) {
1678 * Special case - Peer's support nat. We may have two init's
1679 * that we gave out the same tag on since one was not
1680 * established.. i.e. we get INIT from host-1 behind the nat
1681 * and we respond tag-a, we get a INIT from host-2 behind
1682 * the nat and we get tag-a again. Then we bring up host-1
1683 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1684 * Now we have colliding state. We must send an abort here
1685 * with colliding state indication.
1687 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1688 0, M_DONTWAIT, 1, MT_DATA);
1689 if (op_err == NULL) {
1693 /* pre-reserve some space */
1695 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
1697 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
1699 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
1700 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1702 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1703 ph = mtod(op_err, struct sctp_paramhdr *);
1704 ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE);
1705 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1706 sctp_send_abort(m, iphlen, sh, 0, op_err,
1707 use_mflowid, mflowid,
1711 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1712 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1713 (asoc->peer_vtag == 0))) {
1715 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1716 * should be ok, re-accept peer info
1718 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1720 * Extension of case C. If we hit this, then the
1721 * random number generator returned the same vtag
1722 * when we first sent our INIT-ACK and when we later
1723 * sent our INIT. The side with the seq numbers that
1724 * are different will be the one that normnally
1725 * would have hit case C. This in effect "extends"
1726 * our vtags in this collision case to be 64 bits.
1727 * The same collision could occur aka you get both
1728 * vtag and seq number the same twice in a row.. but
1729 * is much less likely. If it did happen then we
1730 * would proceed through and bring up the assoc.. we
1731 * may end up with the wrong stream setup however..
1732 * which would be bad.. but there is no way to
1733 * tell.. until we send on a stream that does not
1736 if (how_indx < sizeof(asoc->cookie_how))
1737 asoc->cookie_how[how_indx] = 7;
1741 if (how_indx < sizeof(asoc->cookie_how))
1742 asoc->cookie_how[how_indx] = 8;
1743 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1744 sctp_stop_all_cookie_timers(stcb);
1746 * since we did not send a HB make sure we don't double
1749 net->hb_responded = 1;
1750 if (stcb->asoc.sctp_autoclose_ticks &&
1751 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1752 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1755 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1756 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1758 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1760 * Ok the peer probably discarded our data (if we
1761 * echoed a cookie+data). So anything on the
1762 * sent_queue should be marked for retransmit, we
1763 * may not get something to kick us so it COULD
1764 * still take a timeout to move these.. but it can't
1765 * hurt to mark them.
1767 struct sctp_tmit_chunk *chk;
1769 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1770 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1771 chk->sent = SCTP_DATAGRAM_RESEND;
1772 sctp_flight_size_decrease(chk);
1773 sctp_total_flight_decrease(stcb, chk);
1774 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1780 /* process the INIT info (peer's info) */
1781 retval = sctp_process_init(init_cp, stcb);
1783 if (how_indx < sizeof(asoc->cookie_how))
1784 asoc->cookie_how[how_indx] = 9;
1787 if (sctp_load_addresses_from_init(stcb, m,
1788 init_offset + sizeof(struct sctp_init_chunk),
1789 initack_offset, sh, init_src)) {
1790 if (how_indx < sizeof(asoc->cookie_how))
1791 asoc->cookie_how[how_indx] = 10;
1794 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1795 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1796 *notification = SCTP_NOTIFY_ASSOC_UP;
1798 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1799 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1800 (inp->sctp_socket->so_qlimit == 0)) {
1801 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1805 stcb->sctp_ep->sctp_flags |=
1806 SCTP_PCB_FLAGS_CONNECTED;
1807 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1808 so = SCTP_INP_SO(stcb->sctp_ep);
1809 atomic_add_int(&stcb->asoc.refcnt, 1);
1810 SCTP_TCB_UNLOCK(stcb);
1811 SCTP_SOCKET_LOCK(so, 1);
1812 SCTP_TCB_LOCK(stcb);
1813 atomic_add_int(&stcb->asoc.refcnt, -1);
1814 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1815 SCTP_SOCKET_UNLOCK(so, 1);
1819 soisconnected(stcb->sctp_socket);
1820 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1821 SCTP_SOCKET_UNLOCK(so, 1);
1824 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1825 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1827 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1828 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1829 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1830 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1832 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1834 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1835 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1836 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1837 stcb->sctp_ep, stcb, asoc->primary_destination);
1839 sctp_stop_all_cookie_timers(stcb);
1840 sctp_toss_old_cookies(stcb, asoc);
1841 sctp_send_cookie_ack(stcb);
1844 * only if we have retrans set do we do this. What
1845 * this call does is get only the COOKIE-ACK out and
1846 * then when we return the normal call to
1847 * sctp_chunk_output will get the retrans out behind
1850 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1852 if (how_indx < sizeof(asoc->cookie_how))
1853 asoc->cookie_how[how_indx] = 11;
1857 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1858 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1859 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1860 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1861 cookie->tie_tag_peer_vtag != 0) {
1862 struct sctpasochead *head;
1864 if (asoc->peer_supports_nat) {
1866 * This is a gross gross hack. just call the
1867 * cookie_new code since we are allowing a duplicate
1868 * association. I hope this works...
1870 return (sctp_process_cookie_new(m, iphlen, offset, sh, cookie, cookie_len,
1871 inp, netp, init_src, notification,
1872 auth_skipped, auth_offset, auth_len,
1873 use_mflowid, mflowid,
1877 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1880 if (how_indx < sizeof(asoc->cookie_how))
1881 asoc->cookie_how[how_indx] = 12;
1882 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1883 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1885 /* notify upper layer */
1886 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1887 atomic_add_int(&stcb->asoc.refcnt, 1);
1888 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1889 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1890 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1891 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1893 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1894 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1895 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1896 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1898 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1899 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1900 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1901 stcb->sctp_ep, stcb, asoc->primary_destination);
1903 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1904 /* move to OPEN state, if not in SHUTDOWN_SENT */
1905 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1907 asoc->pre_open_streams =
1908 ntohs(initack_cp->init.num_outbound_streams);
1909 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1910 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1911 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1913 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1915 asoc->str_reset_seq_in = asoc->init_seq_number;
1917 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1918 if (asoc->mapping_array) {
1919 memset(asoc->mapping_array, 0,
1920 asoc->mapping_array_size);
1922 if (asoc->nr_mapping_array) {
1923 memset(asoc->nr_mapping_array, 0,
1924 asoc->mapping_array_size);
1926 SCTP_TCB_UNLOCK(stcb);
1927 SCTP_INP_INFO_WLOCK();
1928 SCTP_INP_WLOCK(stcb->sctp_ep);
1929 SCTP_TCB_LOCK(stcb);
1930 atomic_add_int(&stcb->asoc.refcnt, -1);
1931 /* send up all the data */
1932 SCTP_TCB_SEND_LOCK(stcb);
1934 sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_NOT_LOCKED);
1935 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1936 stcb->asoc.strmout[i].stream_no = i;
1937 stcb->asoc.strmout[i].next_sequence_sent = 0;
1938 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1940 /* process the INIT-ACK info (my info) */
1941 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1942 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1944 /* pull from vtag hash */
1945 LIST_REMOVE(stcb, sctp_asocs);
1946 /* re-insert to new vtag position */
1947 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1948 SCTP_BASE_INFO(hashasocmark))];
1950 * put it in the bucket in the vtag hash of assoc's for the
1953 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1955 /* process the INIT info (peer's info) */
1956 SCTP_TCB_SEND_UNLOCK(stcb);
1957 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1958 SCTP_INP_INFO_WUNLOCK();
1960 retval = sctp_process_init(init_cp, stcb);
1962 if (how_indx < sizeof(asoc->cookie_how))
1963 asoc->cookie_how[how_indx] = 13;
1968 * since we did not send a HB make sure we don't double
1971 net->hb_responded = 1;
1973 if (sctp_load_addresses_from_init(stcb, m,
1974 init_offset + sizeof(struct sctp_init_chunk),
1975 initack_offset, sh, init_src)) {
1976 if (how_indx < sizeof(asoc->cookie_how))
1977 asoc->cookie_how[how_indx] = 14;
1981 /* respond with a COOKIE-ACK */
1982 sctp_stop_all_cookie_timers(stcb);
1983 sctp_toss_old_cookies(stcb, asoc);
1984 sctp_send_cookie_ack(stcb);
1985 if (how_indx < sizeof(asoc->cookie_how))
1986 asoc->cookie_how[how_indx] = 15;
1990 if (how_indx < sizeof(asoc->cookie_how))
1991 asoc->cookie_how[how_indx] = 16;
1992 /* all other cases... */
1998 * handle a state cookie for a new association m: input packet mbuf chain--
1999 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
2000 * and the cookie signature does not exist offset: offset into mbuf to the
2001 * cookie-echo chunk length: length of the cookie chunk to: where the init
2002 * was from returns a new TCB
2004 static struct sctp_tcb *
2005 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
2006 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
2007 struct sctp_inpcb *inp, struct sctp_nets **netp,
2008 struct sockaddr *init_src, int *notification,
2009 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2010 uint8_t use_mflowid, uint32_t mflowid,
2011 uint32_t vrf_id, uint16_t port)
2013 struct sctp_tcb *stcb;
2014 struct sctp_init_chunk *init_cp, init_buf;
2015 struct sctp_init_ack_chunk *initack_cp, initack_buf;
2016 struct sockaddr_storage sa_store;
2017 struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
2018 struct sctp_association *asoc;
2019 int init_offset, initack_offset, initack_limit;
2022 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
2025 struct sockaddr_in *sin;
2029 struct sockaddr_in6 *sin6;
2032 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2035 so = SCTP_INP_SO(inp);
2039 * find and validate the INIT chunk in the cookie (peer's info) the
2040 * INIT should start after the cookie-echo header struct (chunk
2041 * header, state cookie header struct)
2043 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2044 init_cp = (struct sctp_init_chunk *)
2045 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2046 (uint8_t *) & init_buf);
2047 if (init_cp == NULL) {
2048 /* could not pull a INIT chunk in cookie */
2049 SCTPDBG(SCTP_DEBUG_INPUT1,
2050 "process_cookie_new: could not pull INIT chunk hdr\n");
2053 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2054 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2057 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2059 * find and validate the INIT-ACK chunk in the cookie (my info) the
2060 * INIT-ACK follows the INIT chunk
2062 initack_cp = (struct sctp_init_ack_chunk *)
2063 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2064 (uint8_t *) & initack_buf);
2065 if (initack_cp == NULL) {
2066 /* could not pull INIT-ACK chunk in cookie */
2067 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2070 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2074 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2075 * "initack_limit" value. This is because the chk_length field
2076 * includes the length of the cookie, but the cookie is omitted when
2077 * the INIT and INIT_ACK are tacked onto the cookie...
2079 initack_limit = offset + cookie_len;
2082 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2087 * Here we do a trick, we set in NULL for the proc/thread argument.
2088 * We do this since in effect we only use the p argument when the
2089 * socket is unbound and we must do an implicit bind. Since we are
2090 * getting a cookie, we cannot be unbound.
2092 stcb = sctp_aloc_assoc(inp, init_src, &error,
2093 ntohl(initack_cp->init.initiate_tag), vrf_id,
2094 (struct thread *)NULL
2097 struct mbuf *op_err;
2099 /* memory problem? */
2100 SCTPDBG(SCTP_DEBUG_INPUT1,
2101 "process_cookie_new: no room for another TCB!\n");
2102 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2104 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2106 use_mflowid, mflowid,
2110 /* get the correct sctp_nets */
2112 *netp = sctp_findnet(stcb, init_src);
2115 /* get scope variables out of cookie */
2116 asoc->ipv4_local_scope = cookie->ipv4_scope;
2117 asoc->site_scope = cookie->site_scope;
2118 asoc->local_scope = cookie->local_scope;
2119 asoc->loopback_scope = cookie->loopback_scope;
2121 if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2122 (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2123 struct mbuf *op_err;
2126 * Houston we have a problem. The EP changed while the
2127 * cookie was in flight. Only recourse is to abort the
2130 atomic_add_int(&stcb->asoc.refcnt, 1);
2131 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2132 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2134 use_mflowid, mflowid,
2136 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2137 SCTP_TCB_UNLOCK(stcb);
2138 SCTP_SOCKET_LOCK(so, 1);
2139 SCTP_TCB_LOCK(stcb);
2141 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2142 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2143 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2144 SCTP_SOCKET_UNLOCK(so, 1);
2146 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2149 /* process the INIT-ACK info (my info) */
2150 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2151 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2152 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2153 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2154 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2155 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2156 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2157 asoc->str_reset_seq_in = asoc->init_seq_number;
2159 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2161 /* process the INIT info (peer's info) */
2163 retval = sctp_process_init(init_cp, stcb);
2167 atomic_add_int(&stcb->asoc.refcnt, 1);
2168 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2169 SCTP_TCB_UNLOCK(stcb);
2170 SCTP_SOCKET_LOCK(so, 1);
2171 SCTP_TCB_LOCK(stcb);
2173 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 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 /* load all addresses */
2181 if (sctp_load_addresses_from_init(stcb, m,
2182 init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh,
2184 atomic_add_int(&stcb->asoc.refcnt, 1);
2185 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2186 SCTP_TCB_UNLOCK(stcb);
2187 SCTP_SOCKET_LOCK(so, 1);
2188 SCTP_TCB_LOCK(stcb);
2190 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
2191 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2192 SCTP_SOCKET_UNLOCK(so, 1);
2194 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2198 * verify any preceding AUTH chunk that was skipped
2200 /* pull the local authentication parameters from the cookie/init-ack */
2201 sctp_auth_get_cookie_params(stcb, m,
2202 initack_offset + sizeof(struct sctp_init_ack_chunk),
2203 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2205 struct sctp_auth_chunk *auth;
2207 auth = (struct sctp_auth_chunk *)
2208 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2209 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2210 /* auth HMAC failed, dump the assoc and packet */
2211 SCTPDBG(SCTP_DEBUG_AUTH1,
2212 "COOKIE-ECHO: AUTH failed\n");
2213 atomic_add_int(&stcb->asoc.refcnt, 1);
2214 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2215 SCTP_TCB_UNLOCK(stcb);
2216 SCTP_SOCKET_LOCK(so, 1);
2217 SCTP_TCB_LOCK(stcb);
2219 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2220 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2221 SCTP_SOCKET_UNLOCK(so, 1);
2223 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2226 /* remaining chunks checked... good to go */
2227 stcb->asoc.authenticated = 1;
2230 /* update current state */
2231 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2232 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2233 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2234 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2235 stcb->sctp_ep, stcb, asoc->primary_destination);
2237 sctp_stop_all_cookie_timers(stcb);
2238 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2239 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2242 * if we're doing ASCONFs, check to see if we have any new local
2243 * addresses that need to get added to the peer (eg. addresses
2244 * changed while cookie echo in flight). This needs to be done
2245 * after we go to the OPEN state to do the correct asconf
2246 * processing. else, make sure we have the correct addresses in our
2250 /* warning, we re-use sin, sin6, sa_store here! */
2251 /* pull in local_address (our "from" address) */
2252 switch (cookie->laddr_type) {
2254 case SCTP_IPV4_ADDRESS:
2255 /* source addr is IPv4 */
2256 sin = (struct sockaddr_in *)initack_src;
2257 memset(sin, 0, sizeof(*sin));
2258 sin->sin_family = AF_INET;
2259 sin->sin_len = sizeof(struct sockaddr_in);
2260 sin->sin_addr.s_addr = cookie->laddress[0];
2264 case SCTP_IPV6_ADDRESS:
2265 /* source addr is IPv6 */
2266 sin6 = (struct sockaddr_in6 *)initack_src;
2267 memset(sin6, 0, sizeof(*sin6));
2268 sin6->sin6_family = AF_INET6;
2269 sin6->sin6_len = sizeof(struct sockaddr_in6);
2270 sin6->sin6_scope_id = cookie->scope_id;
2271 memcpy(&sin6->sin6_addr, cookie->laddress,
2272 sizeof(sin6->sin6_addr));
2276 atomic_add_int(&stcb->asoc.refcnt, 1);
2277 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2278 SCTP_TCB_UNLOCK(stcb);
2279 SCTP_SOCKET_LOCK(so, 1);
2280 SCTP_TCB_LOCK(stcb);
2282 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2283 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2284 SCTP_SOCKET_UNLOCK(so, 1);
2286 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2290 /* set up to notify upper layer */
2291 *notification = SCTP_NOTIFY_ASSOC_UP;
2292 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2293 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2294 (inp->sctp_socket->so_qlimit == 0)) {
2296 * This is an endpoint that called connect() how it got a
2297 * cookie that is NEW is a bit of a mystery. It must be that
2298 * the INIT was sent, but before it got there.. a complete
2299 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2300 * should have went to the other code.. not here.. oh well..
2301 * a bit of protection is worth having..
2303 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2304 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2305 atomic_add_int(&stcb->asoc.refcnt, 1);
2306 SCTP_TCB_UNLOCK(stcb);
2307 SCTP_SOCKET_LOCK(so, 1);
2308 SCTP_TCB_LOCK(stcb);
2309 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2310 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2311 SCTP_SOCKET_UNLOCK(so, 1);
2315 soisconnected(stcb->sctp_socket);
2316 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2317 SCTP_SOCKET_UNLOCK(so, 1);
2319 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2320 (inp->sctp_socket->so_qlimit)) {
2322 * We don't want to do anything with this one. Since it is
2323 * the listening guy. The timer will get started for
2324 * accepted connections in the caller.
2328 /* since we did not send a HB make sure we don't double things */
2329 if ((netp) && (*netp))
2330 (*netp)->hb_responded = 1;
2332 if (stcb->asoc.sctp_autoclose_ticks &&
2333 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2334 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2336 /* calculate the RTT */
2337 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2338 if ((netp) && (*netp)) {
2339 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2340 &cookie->time_entered, sctp_align_unsafe_makecopy,
2341 SCTP_RTT_FROM_NON_DATA);
2343 /* respond with a COOKIE-ACK */
2344 sctp_send_cookie_ack(stcb);
2347 * check the address lists for any ASCONFs that need to be sent
2348 * AFTER the cookie-ack is sent
2350 sctp_check_address_list(stcb, m,
2351 initack_offset + sizeof(struct sctp_init_ack_chunk),
2352 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2353 initack_src, cookie->local_scope, cookie->site_scope,
2354 cookie->ipv4_scope, cookie->loopback_scope);
2361 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2362 * we NEED to make sure we are not already using the vtag. If so we
2363 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2364 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2365 SCTP_BASE_INFO(hashasocmark))];
2366 LIST_FOREACH(stcb, head, sctp_asocs) {
2367 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2368 -- SEND ABORT - TRY AGAIN --
2374 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2375 * existing (non-NULL) TCB
2377 static struct mbuf *
2378 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2379 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2380 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2381 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2382 struct sctp_tcb **locked_tcb,
2383 uint8_t use_mflowid, uint32_t mflowid,
2384 uint32_t vrf_id, uint16_t port)
2386 struct sctp_state_cookie *cookie;
2387 struct sctp_tcb *l_stcb = *stcb;
2388 struct sctp_inpcb *l_inp;
2389 struct sockaddr *to;
2390 struct sctp_pcb *ep;
2392 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2394 uint8_t cookie_ok = 0;
2395 unsigned int size_of_pkt, sig_offset, cookie_offset;
2396 unsigned int cookie_len;
2398 struct timeval time_expires;
2399 struct sockaddr_storage dest_store;
2400 struct sockaddr *localep_sa = (struct sockaddr *)&dest_store;
2402 int notification = 0;
2403 struct sctp_nets *netl;
2404 int had_a_existing_tcb = 0;
2405 int send_int_conf = 0;
2408 struct sockaddr_in sin;
2412 struct sockaddr_in6 sin6;
2416 SCTPDBG(SCTP_DEBUG_INPUT2,
2417 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2419 if (inp_p == NULL) {
2422 /* First get the destination address setup too. */
2423 iph = mtod(m, struct ip *);
2424 switch (iph->ip_v) {
2429 struct sockaddr_in *lsin;
2431 lsin = (struct sockaddr_in *)(localep_sa);
2432 memset(lsin, 0, sizeof(*lsin));
2433 lsin->sin_family = AF_INET;
2434 lsin->sin_len = sizeof(*lsin);
2435 lsin->sin_port = sh->dest_port;
2436 lsin->sin_addr.s_addr = iph->ip_dst.s_addr;
2437 size_of_pkt = SCTP_GET_IPV4_LENGTH(iph);
2442 case IPV6_VERSION >> 4:
2445 struct ip6_hdr *ip6;
2446 struct sockaddr_in6 *lsin6;
2448 lsin6 = (struct sockaddr_in6 *)(localep_sa);
2449 memset(lsin6, 0, sizeof(*lsin6));
2450 lsin6->sin6_family = AF_INET6;
2451 lsin6->sin6_len = sizeof(struct sockaddr_in6);
2452 ip6 = mtod(m, struct ip6_hdr *);
2453 lsin6->sin6_port = sh->dest_port;
2454 lsin6->sin6_addr = ip6->ip6_dst;
2455 size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen;
2463 cookie = &cp->cookie;
2464 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2465 cookie_len = ntohs(cp->ch.chunk_length);
2467 if ((cookie->peerport != sh->src_port) &&
2468 (cookie->myport != sh->dest_port) &&
2469 (cookie->my_vtag != sh->v_tag)) {
2471 * invalid ports or bad tag. Note that we always leave the
2472 * v_tag in the header in network order and when we stored
2473 * it in the my_vtag slot we also left it in network order.
2474 * This maintains the match even though it may be in the
2475 * opposite byte order of the machine :->
2479 if (cookie_len > size_of_pkt ||
2480 cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2481 sizeof(struct sctp_init_chunk) +
2482 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2483 /* cookie too long! or too small */
2487 * split off the signature into its own mbuf (since it should not be
2488 * calculated in the sctp_hmac_m() call).
2490 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2491 if (sig_offset > size_of_pkt) {
2492 /* packet not correct size! */
2493 /* XXX this may already be accounted for earlier... */
2496 m_sig = m_split(m, sig_offset, M_DONTWAIT);
2497 if (m_sig == NULL) {
2498 /* out of memory or ?? */
2501 #ifdef SCTP_MBUF_LOGGING
2502 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2505 for (mat = m_sig; mat; mat = SCTP_BUF_NEXT(mat)) {
2506 if (SCTP_BUF_IS_EXTENDED(mat)) {
2507 sctp_log_mb(mat, SCTP_MBUF_SPLIT);
2514 * compute the signature/digest for the cookie
2516 ep = &(*inp_p)->sctp_ep;
2519 SCTP_TCB_UNLOCK(l_stcb);
2521 SCTP_INP_RLOCK(l_inp);
2523 SCTP_TCB_LOCK(l_stcb);
2525 /* which cookie is it? */
2526 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2527 (ep->current_secret_number != ep->last_secret_number)) {
2528 /* it's the old cookie */
2529 (void)sctp_hmac_m(SCTP_HMAC,
2530 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2531 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2533 /* it's the current cookie */
2534 (void)sctp_hmac_m(SCTP_HMAC,
2535 (uint8_t *) ep->secret_key[(int)ep->current_secret_number],
2536 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2538 /* get the signature */
2539 SCTP_INP_RUNLOCK(l_inp);
2540 sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig);
2542 /* couldn't find signature */
2543 sctp_m_freem(m_sig);
2546 /* compare the received digest with the computed digest */
2547 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2548 /* try the old cookie? */
2549 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2550 (ep->current_secret_number != ep->last_secret_number)) {
2551 /* compute digest with old */
2552 (void)sctp_hmac_m(SCTP_HMAC,
2553 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2554 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2556 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2564 * Now before we continue we must reconstruct our mbuf so that
2565 * normal processing of any other chunks will work.
2571 while (SCTP_BUF_NEXT(m_at) != NULL) {
2572 m_at = SCTP_BUF_NEXT(m_at);
2574 SCTP_BUF_NEXT(m_at) = m_sig;
2577 if (cookie_ok == 0) {
2578 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2579 SCTPDBG(SCTP_DEBUG_INPUT2,
2580 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2581 (uint32_t) offset, cookie_offset, sig_offset);
2585 * check the cookie timestamps to be sure it's not stale
2587 (void)SCTP_GETTIME_TIMEVAL(&now);
2588 /* Expire time is in Ticks, so we convert to seconds */
2589 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2590 time_expires.tv_usec = cookie->time_entered.tv_usec;
2592 * TODO sctp_constants.h needs alternative time macros when _KERNEL
2595 if (timevalcmp(&now, &time_expires, >)) {
2596 /* cookie is stale! */
2597 struct mbuf *op_err;
2598 struct sctp_stale_cookie_msg *scm;
2601 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg),
2602 0, M_DONTWAIT, 1, MT_DATA);
2603 if (op_err == NULL) {
2608 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg);
2609 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
2610 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
2611 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
2612 (sizeof(uint32_t))));
2613 /* seconds to usec */
2614 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2617 tim = now.tv_usec - cookie->time_entered.tv_usec;
2618 scm->time_usec = htonl(tim);
2619 sctp_send_operr_to(m, sh, cookie->peers_vtag, op_err,
2620 use_mflowid, mflowid,
2625 * Now we must see with the lookup address if we have an existing
2626 * asoc. This will only happen if we were in the COOKIE-WAIT state
2627 * and a INIT collided with us and somewhere the peer sent the
2628 * cookie on another address besides the single address our assoc
2629 * had for him. In this case we will have one of the tie-tags set at
2630 * least AND the address field in the cookie can be used to look it
2634 switch (cookie->addr_type) {
2636 case SCTP_IPV6_ADDRESS:
2637 memset(&sin6, 0, sizeof(sin6));
2638 sin6.sin6_family = AF_INET6;
2639 sin6.sin6_len = sizeof(sin6);
2640 sin6.sin6_port = sh->src_port;
2641 sin6.sin6_scope_id = cookie->scope_id;
2642 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2643 sizeof(sin6.sin6_addr.s6_addr));
2644 to = (struct sockaddr *)&sin6;
2648 case SCTP_IPV4_ADDRESS:
2649 memset(&sin, 0, sizeof(sin));
2650 sin.sin_family = AF_INET;
2651 sin.sin_len = sizeof(sin);
2652 sin.sin_port = sh->src_port;
2653 sin.sin_addr.s_addr = cookie->address[0];
2654 to = (struct sockaddr *)&sin;
2658 /* This should not happen */
2661 if ((*stcb == NULL) && to) {
2662 /* Yep, lets check */
2663 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL);
2664 if (*stcb == NULL) {
2666 * We should have only got back the same inp. If we
2667 * got back a different ep we have a problem. The
2668 * original findep got back l_inp and now
2670 if (l_inp != *inp_p) {
2671 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2674 if (*locked_tcb == NULL) {
2676 * In this case we found the assoc only
2677 * after we locked the create lock. This
2678 * means we are in a colliding case and we
2679 * must make sure that we unlock the tcb if
2680 * its one of the cases where we throw away
2681 * the incoming packets.
2683 *locked_tcb = *stcb;
2686 * We must also increment the inp ref count
2687 * since the ref_count flags was set when we
2688 * did not find the TCB, now we found it
2689 * which reduces the refcount.. we must
2690 * raise it back out to balance it all :-)
2692 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2693 if ((*stcb)->sctp_ep != l_inp) {
2694 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2695 (*stcb)->sctp_ep, l_inp);
2703 cookie_len -= SCTP_SIGNATURE_SIZE;
2704 if (*stcb == NULL) {
2705 /* this is the "normal" case... get a new TCB */
2706 *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie,
2707 cookie_len, *inp_p, netp, to, ¬ification,
2708 auth_skipped, auth_offset, auth_len,
2709 use_mflowid, mflowid,
2712 /* this is abnormal... cookie-echo on existing TCB */
2713 had_a_existing_tcb = 1;
2714 *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh,
2715 cookie, cookie_len, *inp_p, *stcb, netp, to,
2716 ¬ification, auth_skipped, auth_offset, auth_len,
2717 use_mflowid, mflowid,
2721 if (*stcb == NULL) {
2722 /* still no TCB... must be bad cookie-echo */
2725 if ((*netp != NULL) && (use_mflowid != 0)) {
2726 (*netp)->flowid = mflowid;
2728 (*netp)->flowidset = 1;
2732 * Ok, we built an association so confirm the address we sent the
2735 netl = sctp_findnet(*stcb, to);
2737 * This code should in theory NOT run but
2740 /* TSNH! Huh, why do I need to add this address here? */
2741 if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2744 netl = sctp_findnet(*stcb, to);
2747 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2748 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2749 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2754 sctp_start_net_timers(*stcb);
2755 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2756 if (!had_a_existing_tcb ||
2757 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2759 * If we have a NEW cookie or the connect never
2760 * reached the connected state during collision we
2761 * must do the TCP accept thing.
2763 struct socket *so, *oso;
2764 struct sctp_inpcb *inp;
2766 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2768 * For a restart we will keep the same
2769 * socket, no need to do anything. I THINK!!
2771 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2772 if (send_int_conf) {
2773 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2774 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2778 oso = (*inp_p)->sctp_socket;
2779 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2780 SCTP_TCB_UNLOCK((*stcb));
2781 CURVNET_SET(oso->so_vnet);
2782 so = sonewconn(oso, 0
2785 SCTP_TCB_LOCK((*stcb));
2786 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2789 struct mbuf *op_err;
2791 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2792 struct socket *pcb_so;
2795 /* Too many sockets */
2796 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2797 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2798 sctp_abort_association(*inp_p, NULL, m, iphlen,
2800 use_mflowid, mflowid,
2802 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2803 pcb_so = SCTP_INP_SO(*inp_p);
2804 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2805 SCTP_TCB_UNLOCK((*stcb));
2806 SCTP_SOCKET_LOCK(pcb_so, 1);
2807 SCTP_TCB_LOCK((*stcb));
2808 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2810 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2811 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2812 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2816 inp = (struct sctp_inpcb *)so->so_pcb;
2817 SCTP_INP_INCR_REF(inp);
2819 * We add the unbound flag here so that if we get an
2820 * soabort() before we get the move_pcb done, we
2821 * will properly cleanup.
2823 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2824 SCTP_PCB_FLAGS_CONNECTED |
2825 SCTP_PCB_FLAGS_IN_TCPPOOL |
2826 SCTP_PCB_FLAGS_UNBOUND |
2827 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2828 SCTP_PCB_FLAGS_DONT_WAKE);
2829 inp->sctp_features = (*inp_p)->sctp_features;
2830 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2831 inp->sctp_socket = so;
2832 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2833 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2834 inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable;
2835 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2836 inp->sctp_context = (*inp_p)->sctp_context;
2837 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2838 inp->inp_starting_point_for_iterator = NULL;
2840 * copy in the authentication parameters from the
2843 if (inp->sctp_ep.local_hmacs)
2844 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2845 inp->sctp_ep.local_hmacs =
2846 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2847 if (inp->sctp_ep.local_auth_chunks)
2848 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2849 inp->sctp_ep.local_auth_chunks =
2850 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2853 * Now we must move it from one hash table to
2854 * another and get the tcb in the right place.
2858 * This is where the one-2-one socket is put into
2859 * the accept state waiting for the accept!
2862 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2864 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2866 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2867 SCTP_TCB_UNLOCK((*stcb));
2869 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2871 SCTP_TCB_LOCK((*stcb));
2872 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2876 * now we must check to see if we were aborted while
2877 * the move was going on and the lock/unlock
2880 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2882 * yep it was, we leave the assoc attached
2883 * to the socket since the sctp_inpcb_free()
2884 * call will send an abort for us.
2886 SCTP_INP_DECR_REF(inp);
2889 SCTP_INP_DECR_REF(inp);
2890 /* Switch over to the new guy */
2892 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2893 if (send_int_conf) {
2894 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2895 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2898 * Pull it from the incomplete queue and wake the
2901 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2902 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2903 SCTP_TCB_UNLOCK((*stcb));
2904 SCTP_SOCKET_LOCK(so, 1);
2907 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2908 SCTP_TCB_LOCK((*stcb));
2909 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2910 SCTP_SOCKET_UNLOCK(so, 1);
2916 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2918 if (send_int_conf) {
2919 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2920 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2926 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2927 struct sctp_tcb *stcb, struct sctp_nets *net)
2929 /* cp must not be used, others call this without a c-ack :-) */
2930 struct sctp_association *asoc;
2932 SCTPDBG(SCTP_DEBUG_INPUT2,
2933 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2939 sctp_stop_all_cookie_timers(stcb);
2940 /* process according to association state */
2941 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2942 /* state change only needed when I am in right state */
2943 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2944 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2945 sctp_start_net_timers(stcb);
2946 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2947 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2948 stcb->sctp_ep, stcb, asoc->primary_destination);
2952 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2953 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2954 if (asoc->overall_error_count == 0) {
2955 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2956 &asoc->time_entered, sctp_align_safe_nocopy,
2957 SCTP_RTT_FROM_NON_DATA);
2959 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2960 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2961 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2962 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2963 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2967 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2968 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2969 so = SCTP_INP_SO(stcb->sctp_ep);
2970 atomic_add_int(&stcb->asoc.refcnt, 1);
2971 SCTP_TCB_UNLOCK(stcb);
2972 SCTP_SOCKET_LOCK(so, 1);
2973 SCTP_TCB_LOCK(stcb);
2974 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2976 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2977 soisconnected(stcb->sctp_socket);
2979 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2980 SCTP_SOCKET_UNLOCK(so, 1);
2984 * since we did not send a HB make sure we don't double
2987 net->hb_responded = 1;
2989 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2991 * We don't need to do the asconf thing, nor hb or
2992 * autoclose if the socket is closed.
2996 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
3000 if (stcb->asoc.sctp_autoclose_ticks &&
3001 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
3002 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
3003 stcb->sctp_ep, stcb, NULL);
3006 * send ASCONF if parameters are pending and ASCONFs are
3007 * allowed (eg. addresses changed when init/cookie echo were
3010 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
3011 (stcb->asoc.peer_supports_asconf) &&
3012 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
3013 #ifdef SCTP_TIMER_BASED_ASCONF
3014 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
3015 stcb->sctp_ep, stcb,
3016 stcb->asoc.primary_destination);
3018 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
3019 SCTP_ADDR_NOT_LOCKED);
3024 /* Toss the cookie if I can */
3025 sctp_toss_old_cookies(stcb, asoc);
3026 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3027 /* Restart the timer if we have pending data */
3028 struct sctp_tmit_chunk *chk;
3030 chk = TAILQ_FIRST(&asoc->sent_queue);
3031 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
3036 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
3037 struct sctp_tcb *stcb)
3039 struct sctp_nets *net;
3040 struct sctp_tmit_chunk *lchk;
3041 struct sctp_ecne_chunk bkup;
3042 uint8_t override_bit;
3043 uint32_t tsn, window_data_tsn;
3045 unsigned int pkt_cnt;
3047 len = ntohs(cp->ch.chunk_length);
3048 if ((len != sizeof(struct sctp_ecne_chunk)) &&
3049 (len != sizeof(struct old_sctp_ecne_chunk))) {
3052 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3053 /* Its the old format */
3054 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3055 bkup.num_pkts_since_cwr = htonl(1);
3058 SCTP_STAT_INCR(sctps_recvecne);
3059 tsn = ntohl(cp->tsn);
3060 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3061 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3063 window_data_tsn = stcb->asoc.sending_seq - 1;
3065 window_data_tsn = lchk->rec.data.TSN_seq;
3068 /* Find where it was sent to if possible. */
3070 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3071 if (lchk->rec.data.TSN_seq == tsn) {
3073 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3076 if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) {
3082 * What to do. A previous send of a CWR was possibly lost.
3083 * See how old it is, we may have it marked on the actual
3086 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3087 if (tsn == net->last_cwr_tsn) {
3088 /* Found him, send it off */
3094 * If we reach here, we need to send a special CWR
3095 * that says hey, we did this a long time ago and
3096 * you lost the response.
3098 net = TAILQ_FIRST(&stcb->asoc.nets);
3103 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3110 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3111 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3113 * JRS - Use the congestion control given in the pluggable
3116 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3118 * We reduce once every RTT. So we will only lower cwnd at
3119 * the next sending seq i.e. the window_data_tsn
3121 net->cwr_window_tsn = window_data_tsn;
3122 net->ecn_ce_pkt_cnt += pkt_cnt;
3123 net->lost_cnt = pkt_cnt;
3124 net->last_cwr_tsn = tsn;
3126 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3127 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3128 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3130 * Another loss in the same window update how many
3131 * marks/packets lost we have had.
3135 if (pkt_cnt > net->lost_cnt) {
3136 /* Should be the case */
3137 cnt = (pkt_cnt - net->lost_cnt);
3138 net->ecn_ce_pkt_cnt += cnt;
3140 net->lost_cnt = pkt_cnt;
3141 net->last_cwr_tsn = tsn;
3143 * Most CC functions will ignore this call, since we
3144 * are in-window yet of the initial CE the peer saw.
3146 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3150 * We always send a CWR this way if our previous one was lost our
3151 * peer will get an update, or if it is not time again to reduce we
3152 * still get the cwr to the peer. Note we set the override when we
3153 * could not find the TSN on the chunk or the destination network.
3155 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3159 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3162 * Here we get a CWR from the peer. We must look in the outqueue and
3163 * make sure that we have a covered ECNE in teh control chunk part.
3166 struct sctp_tmit_chunk *chk;
3167 struct sctp_ecne_chunk *ecne;
3171 cwr_tsn = ntohl(cp->tsn);
3173 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3174 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
3175 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3178 if ((override == 0) && (chk->whoTo != net)) {
3179 /* Must be from the right src unless override is set */
3182 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3183 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3184 /* this covers this ECNE, we can remove it */
3185 stcb->asoc.ecn_echo_cnt_onq--;
3186 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3189 sctp_m_freem(chk->data);
3192 stcb->asoc.ctrl_queue_cnt--;
3193 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3194 if (override == 0) {
3202 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3203 struct sctp_tcb *stcb, struct sctp_nets *net)
3205 struct sctp_association *asoc;
3207 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3212 SCTPDBG(SCTP_DEBUG_INPUT2,
3213 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3218 /* process according to association state */
3219 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3220 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3221 SCTPDBG(SCTP_DEBUG_INPUT2,
3222 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3223 SCTP_TCB_UNLOCK(stcb);
3226 /* notify upper layer protocol */
3227 if (stcb->sctp_socket) {
3228 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3229 /* are the queues empty? they should be */
3230 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3231 !TAILQ_EMPTY(&asoc->sent_queue) ||
3232 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
3233 sctp_report_all_outbound(stcb, 0, 0, SCTP_SO_NOT_LOCKED);
3236 /* stop the timer */
3237 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
3238 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3240 SCTPDBG(SCTP_DEBUG_INPUT2,
3241 "sctp_handle_shutdown_complete: calls free-asoc\n");
3242 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3243 so = SCTP_INP_SO(stcb->sctp_ep);
3244 atomic_add_int(&stcb->asoc.refcnt, 1);
3245 SCTP_TCB_UNLOCK(stcb);
3246 SCTP_SOCKET_LOCK(so, 1);
3247 SCTP_TCB_LOCK(stcb);
3248 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3250 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
3251 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3252 SCTP_SOCKET_UNLOCK(so, 1);
3258 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3259 struct sctp_nets *net, uint8_t flg)
3261 switch (desc->chunk_type) {
3263 /* find the tsn to resend (possibly */
3266 struct sctp_tmit_chunk *tp1;
3268 tsn = ntohl(desc->tsn_ifany);
3269 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3270 if (tp1->rec.data.TSN_seq == tsn) {
3274 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) {
3282 * Do it the other way , aka without paying
3283 * attention to queue seq order.
3285 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3286 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3287 if (tp1->rec.data.TSN_seq == tsn) {
3294 SCTP_STAT_INCR(sctps_pdrptsnnf);
3296 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3299 if (((flg & SCTP_BADCRC) == 0) &&
3300 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3303 if ((stcb->asoc.peers_rwnd == 0) &&
3304 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3305 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3308 if (stcb->asoc.peers_rwnd == 0 &&
3309 (flg & SCTP_FROM_MIDDLE_BOX)) {
3310 SCTP_STAT_INCR(sctps_pdrpdizrw);
3313 ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+
3314 sizeof(struct sctp_data_chunk));
3318 for (iii = 0; iii < sizeof(desc->data_bytes);
3320 if (ddp[iii] != desc->data_bytes[iii]) {
3321 SCTP_STAT_INCR(sctps_pdrpbadd);
3329 * this guy had a RTO calculation
3330 * pending on it, cancel it
3332 if (tp1->whoTo->rto_needed == 0) {
3333 tp1->whoTo->rto_needed = 1;
3337 SCTP_STAT_INCR(sctps_pdrpmark);
3338 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3339 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3341 * mark it as if we were doing a FR, since
3342 * we will be getting gap ack reports behind
3343 * the info from the router.
3345 tp1->rec.data.doing_fast_retransmit = 1;
3347 * mark the tsn with what sequences can
3350 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3351 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3353 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
3356 /* restart the timer */
3357 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3358 stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3359 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3362 /* fix counts and things */
3363 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3364 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3365 tp1->whoTo->flight_size,
3368 tp1->rec.data.TSN_seq);
3370 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3371 sctp_flight_size_decrease(tp1);
3372 sctp_total_flight_decrease(stcb, tp1);
3374 tp1->sent = SCTP_DATAGRAM_RESEND;
3380 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3381 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3384 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3386 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3389 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3390 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3391 audit, stcb->asoc.sent_queue_retran_cnt);
3392 #ifndef SCTP_AUDITING_ENABLED
3393 stcb->asoc.sent_queue_retran_cnt = audit;
3401 struct sctp_tmit_chunk *asconf;
3403 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3405 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3410 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3411 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3412 asconf->sent = SCTP_DATAGRAM_RESEND;
3413 asconf->snd_count--;
3417 case SCTP_INITIATION:
3418 /* resend the INIT */
3419 stcb->asoc.dropped_special_cnt++;
3420 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3422 * If we can get it in, in a few attempts we do
3423 * this, otherwise we let the timer fire.
3425 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3426 stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3427 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3430 case SCTP_SELECTIVE_ACK:
3431 case SCTP_NR_SELECTIVE_ACK:
3432 /* resend the sack */
3433 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3435 case SCTP_HEARTBEAT_REQUEST:
3436 /* resend a demand HB */
3437 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3439 * Only retransmit if we KNOW we wont destroy the
3442 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3446 sctp_send_shutdown(stcb, net);
3448 case SCTP_SHUTDOWN_ACK:
3449 sctp_send_shutdown_ack(stcb, net);
3451 case SCTP_COOKIE_ECHO:
3453 struct sctp_tmit_chunk *cookie;
3456 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3458 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3463 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3464 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3465 cookie->sent = SCTP_DATAGRAM_RESEND;
3466 sctp_stop_all_cookie_timers(stcb);
3470 case SCTP_COOKIE_ACK:
3471 sctp_send_cookie_ack(stcb);
3473 case SCTP_ASCONF_ACK:
3474 /* resend last asconf ack */
3475 sctp_send_asconf_ack(stcb);
3477 case SCTP_FORWARD_CUM_TSN:
3478 send_forward_tsn(stcb, &stcb->asoc);
3480 /* can't do anything with these */
3481 case SCTP_PACKET_DROPPED:
3482 case SCTP_INITIATION_ACK: /* this should not happen */
3483 case SCTP_HEARTBEAT_ACK:
3484 case SCTP_ABORT_ASSOCIATION:
3485 case SCTP_OPERATION_ERROR:
3486 case SCTP_SHUTDOWN_COMPLETE:
3496 sctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3502 * We set things to 0xffff since this is the last delivered sequence
3503 * and we will be sending in 0 after the reset.
3506 if (number_entries) {
3507 for (i = 0; i < number_entries; i++) {
3508 temp = ntohs(list[i]);
3509 if (temp >= stcb->asoc.streamincnt) {
3512 stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff;
3516 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3517 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3520 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3524 sctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3528 if (number_entries == 0) {
3529 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3530 stcb->asoc.strmout[i].next_sequence_sent = 0;
3532 } else if (number_entries) {
3533 for (i = 0; i < number_entries; i++) {
3536 temp = ntohs(list[i]);
3537 if (temp >= stcb->asoc.streamoutcnt) {
3538 /* no such stream */
3541 stcb->asoc.strmout[temp].next_sequence_sent = 0;
3544 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3548 struct sctp_stream_reset_out_request *
3549 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3551 struct sctp_association *asoc;
3552 struct sctp_stream_reset_out_req *req;
3553 struct sctp_stream_reset_out_request *r;
3554 struct sctp_tmit_chunk *chk;
3558 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3559 asoc->stream_reset_outstanding = 0;
3562 if (stcb->asoc.str_reset == NULL) {
3563 asoc->stream_reset_outstanding = 0;
3566 chk = stcb->asoc.str_reset;
3567 if (chk->data == NULL) {
3571 /* he wants a copy of the chk pointer */
3574 clen = chk->send_size;
3575 req = mtod(chk->data, struct sctp_stream_reset_out_req *);
3577 if (ntohl(r->request_seq) == seq) {
3581 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3582 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3583 /* move to the next one, there can only be a max of two */
3584 r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len);
3585 if (ntohl(r->request_seq) == seq) {
3589 /* that seq is not here */
3594 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3596 struct sctp_association *asoc;
3597 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3599 if (stcb->asoc.str_reset == NULL) {
3604 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3605 TAILQ_REMOVE(&asoc->control_send_queue,
3609 sctp_m_freem(chk->data);
3612 asoc->ctrl_queue_cnt--;
3613 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3614 /* sa_ignore NO_NULL_CHK */
3615 stcb->asoc.str_reset = NULL;
3620 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3621 uint32_t seq, uint32_t action,
3622 struct sctp_stream_reset_response *respin)
3626 struct sctp_association *asoc = &stcb->asoc;
3627 struct sctp_tmit_chunk *chk;
3628 struct sctp_stream_reset_out_request *srparam;
3631 if (asoc->stream_reset_outstanding == 0) {
3635 if (seq == stcb->asoc.str_reset_seq_out) {
3636 srparam = sctp_find_stream_reset(stcb, seq, &chk);
3638 stcb->asoc.str_reset_seq_out++;
3639 type = ntohs(srparam->ph.param_type);
3640 lparm_len = ntohs(srparam->ph.param_length);
3641 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3642 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3643 asoc->stream_reset_out_is_outstanding = 0;
3644 if (asoc->stream_reset_outstanding)
3645 asoc->stream_reset_outstanding--;
3646 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3648 sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams);
3649 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3650 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3652 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3654 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3655 /* Answered my request */
3656 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3657 if (asoc->stream_reset_outstanding)
3658 asoc->stream_reset_outstanding--;
3659 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3660 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3661 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3662 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3663 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3664 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3666 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3667 /* Ok we now may have more streams */
3670 num_stream = stcb->asoc.strm_pending_add_size;
3671 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3673 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3675 stcb->asoc.strm_pending_add_size = 0;
3676 if (asoc->stream_reset_outstanding)
3677 asoc->stream_reset_outstanding--;
3678 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3679 /* Put the new streams into effect */
3680 stcb->asoc.streamoutcnt += num_stream;
3681 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3682 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3683 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3684 SCTP_STREAM_CHANGE_DENIED);
3686 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3687 SCTP_STREAM_CHANGE_FAILED);
3689 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3690 if (asoc->stream_reset_outstanding)
3691 asoc->stream_reset_outstanding--;
3692 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3693 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3694 SCTP_STREAM_CHANGE_DENIED);
3695 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3696 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3697 SCTP_STREAM_CHANGE_FAILED);
3699 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3701 * a) Adopt the new in tsn.
3703 * c) Adopt the new out-tsn
3705 struct sctp_stream_reset_response_tsn *resp;
3706 struct sctp_forward_tsn_chunk fwdtsn;
3709 if (respin == NULL) {
3713 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3714 resp = (struct sctp_stream_reset_response_tsn *)respin;
3715 asoc->stream_reset_outstanding--;
3716 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3717 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3718 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3719 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3723 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3724 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3725 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3727 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3728 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3729 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3731 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3732 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3734 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3735 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3737 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3738 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3739 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3740 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3741 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3742 SCTP_ASSOC_RESET_DENIED);
3744 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3745 SCTP_ASSOC_RESET_FAILED);
3748 /* get rid of the request and get the request flags */
3749 if (asoc->stream_reset_outstanding == 0) {
3750 sctp_clean_up_stream_reset(stcb);
3758 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3759 struct sctp_tmit_chunk *chk,
3760 struct sctp_stream_reset_in_request *req, int trunc)
3768 * peer wants me to send a str-reset to him for my outgoing seq's if
3771 struct sctp_association *asoc = &stcb->asoc;
3773 seq = ntohl(req->request_seq);
3774 if (asoc->str_reset_seq_in == seq) {
3775 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3776 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3777 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3779 /* Can't do it, since they exceeded our buffer size */
3780 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3781 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3782 len = ntohs(req->ph.param_length);
3783 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3784 for (i = 0; i < number_entries; i++) {
3785 temp = ntohs(req->list_of_streams[i]);
3786 req->list_of_streams[i] = temp;
3788 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3789 sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams,
3790 asoc->str_reset_seq_out,
3791 seq, (asoc->sending_seq - 1));
3792 asoc->stream_reset_out_is_outstanding = 1;
3793 asoc->str_reset = chk;
3794 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
3795 stcb->asoc.stream_reset_outstanding++;
3797 /* Can't do it, since we have sent one out */
3798 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3800 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3801 asoc->str_reset_seq_in++;
3802 } else if (asoc->str_reset_seq_in - 1 == seq) {
3803 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3804 } else if (asoc->str_reset_seq_in - 2 == seq) {
3805 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3807 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3812 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3813 struct sctp_tmit_chunk *chk,
3814 struct sctp_stream_reset_tsn_request *req)
3816 /* reset all in and out and update the tsn */
3818 * A) reset my str-seq's on in and out. B) Select a receive next,
3819 * and set cum-ack to it. Also process this selected number as a
3820 * fwd-tsn as well. C) set in the response my next sending seq.
3822 struct sctp_forward_tsn_chunk fwdtsn;
3823 struct sctp_association *asoc = &stcb->asoc;
3827 seq = ntohl(req->request_seq);
3828 if (asoc->str_reset_seq_in == seq) {
3829 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3830 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3831 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3833 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3834 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3835 fwdtsn.ch.chunk_flags = 0;
3836 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3837 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3841 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3842 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3843 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3845 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3846 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3847 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3848 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3849 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3850 atomic_add_int(&asoc->sending_seq, 1);
3851 /* save off historical data for retrans */
3852 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3853 asoc->last_sending_seq[0] = asoc->sending_seq;
3854 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3855 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3856 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3857 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3858 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3859 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3861 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3862 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3863 asoc->str_reset_seq_in++;
3864 } else if (asoc->str_reset_seq_in - 1 == seq) {
3865 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3866 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3867 } else if (asoc->str_reset_seq_in - 2 == seq) {
3868 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3869 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3871 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3877 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3878 struct sctp_tmit_chunk *chk,
3879 struct sctp_stream_reset_out_request *req, int trunc)
3882 int number_entries, len;
3883 struct sctp_association *asoc = &stcb->asoc;
3885 seq = ntohl(req->request_seq);
3887 /* now if its not a duplicate we process it */
3888 if (asoc->str_reset_seq_in == seq) {
3889 len = ntohs(req->ph.param_length);
3890 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3892 * the sender is resetting, handle the list issue.. we must
3893 * a) verify if we can do the reset, if so no problem b) If
3894 * we can't do the reset we must copy the request. c) queue
3895 * it, and setup the data in processor to trigger it off
3896 * when needed and dequeue all the queued data.
3898 tsn = ntohl(req->send_reset_at_tsn);
3900 /* move the reset action back one */
3901 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3902 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3903 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3905 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3906 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3907 /* we can do it now */
3908 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3909 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3912 * we must queue it up and thus wait for the TSN's
3913 * to arrive that are at or before tsn
3915 struct sctp_stream_reset_list *liste;
3918 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3919 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3920 siz, SCTP_M_STRESET);
3921 if (liste == NULL) {
3922 /* gak out of memory */
3923 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3924 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3928 liste->number_entries = number_entries;
3929 memcpy(&liste->req, req,
3930 (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t))));
3931 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3932 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3934 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3935 asoc->str_reset_seq_in++;
3936 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3938 * one seq back, just echo back last action since my
3939 * response was lost.
3941 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3942 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3944 * two seq back, just echo back last action since my
3945 * response was lost.
3947 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3949 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3954 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3955 struct sctp_stream_reset_add_strm *str_add)
3958 * Peer is requesting to add more streams. If its within our
3959 * max-streams we will allow it.
3961 uint32_t num_stream, i;
3963 struct sctp_association *asoc = &stcb->asoc;
3964 struct sctp_queued_to_read *ctl, *nctl;
3966 /* Get the number. */
3967 seq = ntohl(str_add->request_seq);
3968 num_stream = ntohs(str_add->number_of_streams);
3969 /* Now what would be the new total? */
3970 if (asoc->str_reset_seq_in == seq) {
3971 num_stream += stcb->asoc.streamincnt;
3972 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3973 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3974 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3975 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
3976 (num_stream > 0xffff)) {
3977 /* We must reject it they ask for to many */
3979 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3981 /* Ok, we can do that :-) */
3982 struct sctp_stream_in *oldstrm;
3984 /* save off the old */
3985 oldstrm = stcb->asoc.strmin;
3986 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
3987 (num_stream * sizeof(struct sctp_stream_in)),
3989 if (stcb->asoc.strmin == NULL) {
3990 stcb->asoc.strmin = oldstrm;
3993 /* copy off the old data */
3994 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3995 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3996 stcb->asoc.strmin[i].stream_no = i;
3997 stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered;
3998 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
3999 /* now anything on those queues? */
4000 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) {
4001 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next);
4002 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next);
4005 /* Init the new streams */
4006 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
4007 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4008 stcb->asoc.strmin[i].stream_no = i;
4009 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
4010 stcb->asoc.strmin[i].delivery_started = 0;
4012 SCTP_FREE(oldstrm, SCTP_M_STRMI);
4013 /* update the size */
4014 stcb->asoc.streamincnt = num_stream;
4015 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4016 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
4018 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4019 asoc->str_reset_seq_in++;
4020 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4022 * one seq back, just echo back last action since my
4023 * response was lost.
4025 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4026 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4028 * two seq back, just echo back last action since my
4029 * response was lost.
4031 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4033 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4039 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4040 struct sctp_stream_reset_add_strm *str_add)
4043 * Peer is requesting to add more streams. If its within our
4044 * max-streams we will allow it.
4046 uint16_t num_stream;
4048 struct sctp_association *asoc = &stcb->asoc;
4050 /* Get the number. */
4051 seq = ntohl(str_add->request_seq);
4052 num_stream = ntohs(str_add->number_of_streams);
4053 /* Now what would be the new total? */
4054 if (asoc->str_reset_seq_in == seq) {
4055 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4056 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4057 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4058 } else if (stcb->asoc.stream_reset_outstanding) {
4059 /* We must reject it we have something pending */
4060 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4062 /* Ok, we can do that :-) */
4065 mychk = stcb->asoc.streamoutcnt;
4066 mychk += num_stream;
4067 if (mychk < 0x10000) {
4068 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4069 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 0, 1, num_stream, 0, 1)) {
4070 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4073 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4076 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4077 asoc->str_reset_seq_in++;
4078 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4080 * one seq back, just echo back last action since my
4081 * response was lost.
4083 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4084 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4086 * two seq back, just echo back last action since my
4087 * response was lost.
4089 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4091 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4096 __attribute__((noinline))
4099 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4100 struct sctp_stream_reset_out_req *sr_req)
4102 int chk_length, param_len, ptype;
4103 struct sctp_paramhdr pstore;
4104 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4108 struct sctp_tmit_chunk *chk;
4109 struct sctp_chunkhdr *ch;
4110 struct sctp_paramhdr *ph;
4114 /* now it may be a reset or a reset-response */
4115 chk_length = ntohs(sr_req->ch.chunk_length);
4117 /* setup for adding the response */
4118 sctp_alloc_a_chunk(stcb, chk);
4122 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4123 chk->rec.chunk_id.can_take_data = 0;
4124 chk->asoc = &stcb->asoc;
4125 chk->no_fr_allowed = 0;
4126 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4127 chk->book_size_scale = 0;
4128 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4129 if (chk->data == NULL) {
4132 sctp_m_freem(chk->data);
4135 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4138 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4140 /* setup chunk parameters */
4141 chk->sent = SCTP_DATAGRAM_UNSENT;
4145 ch = mtod(chk->data, struct sctp_chunkhdr *);
4146 ch->chunk_type = SCTP_STREAM_RESET;
4147 ch->chunk_flags = 0;
4148 ch->chunk_length = htons(chk->send_size);
4149 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4150 offset += sizeof(struct sctp_chunkhdr);
4151 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) {
4152 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore);
4155 param_len = ntohs(ph->param_length);
4156 if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) {
4160 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)),
4161 (uint8_t *) & cstore);
4162 ptype = ntohs(ph->param_type);
4164 if (param_len > (int)sizeof(cstore)) {
4169 if (num_param > SCTP_MAX_RESET_PARAMS) {
4170 /* hit the max of parameters already sorry.. */
4173 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4174 struct sctp_stream_reset_out_request *req_out;
4176 req_out = (struct sctp_stream_reset_out_request *)ph;
4178 if (stcb->asoc.stream_reset_outstanding) {
4179 seq = ntohl(req_out->response_seq);
4180 if (seq == stcb->asoc.str_reset_seq_out) {
4182 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4185 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4186 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4187 struct sctp_stream_reset_add_strm *str_add;
4189 str_add = (struct sctp_stream_reset_add_strm *)ph;
4191 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4192 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4193 struct sctp_stream_reset_add_strm *str_add;
4195 str_add = (struct sctp_stream_reset_add_strm *)ph;
4197 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4198 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4199 struct sctp_stream_reset_in_request *req_in;
4202 req_in = (struct sctp_stream_reset_in_request *)ph;
4203 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4204 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4205 struct sctp_stream_reset_tsn_request *req_tsn;
4208 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4209 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4211 goto strres_nochunk;
4215 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4216 struct sctp_stream_reset_response *resp;
4219 resp = (struct sctp_stream_reset_response *)ph;
4220 seq = ntohl(resp->response_seq);
4221 result = ntohl(resp->result);
4222 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4224 goto strres_nochunk;
4229 offset += SCTP_SIZE32(param_len);
4230 chk_length -= SCTP_SIZE32(param_len);
4233 /* we have no response free the stuff */
4234 goto strres_nochunk;
4236 /* ok we have a chunk to link in */
4237 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4240 stcb->asoc.ctrl_queue_cnt++;
4245 * Handle a router or endpoints report of a packet loss, there are two ways
4246 * to handle this, either we get the whole packet and must disect it
4247 * ourselves (possibly with truncation and or corruption) or it is a summary
4248 * from a middle box that did the disectting for us.
4251 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4252 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4254 uint32_t bottle_bw, on_queue;
4258 struct sctp_chunk_desc desc;
4259 struct sctp_chunkhdr *ch;
4261 chlen = ntohs(cp->ch.chunk_length);
4262 chlen -= sizeof(struct sctp_pktdrop_chunk);
4263 /* XXX possible chlen underflow */
4266 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4267 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4269 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4270 chlen -= sizeof(struct sctphdr);
4271 /* XXX possible chlen underflow */
4272 memset(&desc, 0, sizeof(desc));
4274 trunc_len = (uint16_t) ntohs(cp->trunc_len);
4275 if (trunc_len > limit) {
4278 /* now the chunks themselves */
4279 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4280 desc.chunk_type = ch->chunk_type;
4281 /* get amount we need to move */
4282 at = ntohs(ch->chunk_length);
4283 if (at < sizeof(struct sctp_chunkhdr)) {
4284 /* corrupt chunk, maybe at the end? */
4285 SCTP_STAT_INCR(sctps_pdrpcrupt);
4288 if (trunc_len == 0) {
4289 /* we are supposed to have all of it */
4291 /* corrupt skip it */
4292 SCTP_STAT_INCR(sctps_pdrpcrupt);
4296 /* is there enough of it left ? */
4297 if (desc.chunk_type == SCTP_DATA) {
4298 if (chlen < (sizeof(struct sctp_data_chunk) +
4299 sizeof(desc.data_bytes))) {
4303 if (chlen < sizeof(struct sctp_chunkhdr)) {
4308 if (desc.chunk_type == SCTP_DATA) {
4309 /* can we get out the tsn? */
4310 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4311 SCTP_STAT_INCR(sctps_pdrpmbda);
4313 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4315 struct sctp_data_chunk *dcp;
4319 dcp = (struct sctp_data_chunk *)ch;
4320 ddp = (uint8_t *) (dcp + 1);
4321 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4322 desc.data_bytes[iii] = ddp[iii];
4324 desc.tsn_ifany = dcp->dp.tsn;
4326 /* nope we are done. */
4327 SCTP_STAT_INCR(sctps_pdrpnedat);
4331 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4332 SCTP_STAT_INCR(sctps_pdrpmbct);
4335 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4336 SCTP_STAT_INCR(sctps_pdrppdbrk);
4339 if (SCTP_SIZE32(at) > chlen) {
4342 chlen -= SCTP_SIZE32(at);
4343 if (chlen < sizeof(struct sctp_chunkhdr)) {
4344 /* done, none left */
4347 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4349 /* Now update any rwnd --- possibly */
4350 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4351 /* From a peer, we get a rwnd report */
4354 SCTP_STAT_INCR(sctps_pdrpfehos);
4356 bottle_bw = ntohl(cp->bottle_bw);
4357 on_queue = ntohl(cp->current_onq);
4358 if (bottle_bw && on_queue) {
4359 /* a rwnd report is in here */
4360 if (bottle_bw > on_queue)
4361 a_rwnd = bottle_bw - on_queue;
4366 stcb->asoc.peers_rwnd = 0;
4368 if (a_rwnd > stcb->asoc.total_flight) {
4369 stcb->asoc.peers_rwnd =
4370 a_rwnd - stcb->asoc.total_flight;
4372 stcb->asoc.peers_rwnd = 0;
4374 if (stcb->asoc.peers_rwnd <
4375 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4376 /* SWS sender side engages */
4377 stcb->asoc.peers_rwnd = 0;
4382 SCTP_STAT_INCR(sctps_pdrpfmbox);
4385 /* now middle boxes in sat networks get a cwnd bump */
4386 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4387 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4388 (stcb->asoc.sat_network)) {
4390 * This is debateable but for sat networks it makes sense
4391 * Note if a T3 timer has went off, we will prohibit any
4392 * changes to cwnd until we exit the t3 loss recovery.
4394 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4395 net, cp, &bottle_bw, &on_queue);
4400 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4401 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4402 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4403 * length of the complete packet outputs: - length: modified to remaining
4404 * length after control processing - netp: modified to new sctp_nets after
4405 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4406 * bad packet,...) otherwise return the tcb for this packet
4409 __attribute__((noinline))
4411 static struct sctp_tcb *
4412 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4413 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4414 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4415 uint8_t use_mflowid, uint32_t mflowid,
4416 uint32_t vrf_id, uint16_t port)
4418 struct sctp_association *asoc;
4420 int num_chunks = 0; /* number of control chunks processed */
4421 uint32_t chk_length;
4423 int abort_no_unlock = 0;
4427 * How big should this be, and should it be alloc'd? Lets try the
4428 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4429 * until we get into jumbo grams and such..
4431 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4432 struct sctp_tcb *locked_tcb = stcb;
4434 uint32_t auth_offset = 0, auth_len = 0;
4435 int auth_skipped = 0;
4438 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4443 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4444 iphlen, *offset, length, stcb);
4446 /* validate chunk header length... */
4447 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4448 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4449 ntohs(ch->chunk_length));
4451 SCTP_TCB_UNLOCK(locked_tcb);
4456 * validate the verification tag
4458 vtag_in = ntohl(sh->v_tag);
4461 SCTP_TCB_LOCK_ASSERT(locked_tcb);
4463 if (ch->chunk_type == SCTP_INITIATION) {
4464 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4465 ntohs(ch->chunk_length), vtag_in);
4467 /* protocol error- silently discard... */
4468 SCTP_STAT_INCR(sctps_badvtag);
4470 SCTP_TCB_UNLOCK(locked_tcb);
4474 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4476 * If there is no stcb, skip the AUTH chunk and process
4477 * later after a stcb is found (to validate the lookup was
4480 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4482 !SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4483 /* save this chunk for later processing */
4485 auth_offset = *offset;
4486 auth_len = ntohs(ch->chunk_length);
4488 /* (temporarily) move past this chunk */
4489 *offset += SCTP_SIZE32(auth_len);
4490 if (*offset >= length) {
4491 /* no more data left in the mbuf chain */
4494 SCTP_TCB_UNLOCK(locked_tcb);
4498 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4499 sizeof(struct sctp_chunkhdr), chunk_buf);
4505 SCTP_TCB_UNLOCK(locked_tcb);
4509 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4510 goto process_control_chunks;
4513 * first check if it's an ASCONF with an unknown src addr we
4514 * need to look inside to find the association
4516 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4517 struct sctp_chunkhdr *asconf_ch = ch;
4518 uint32_t asconf_offset = 0, asconf_len = 0;
4520 /* inp's refcount may be reduced */
4521 SCTP_INP_INCR_REF(inp);
4523 asconf_offset = *offset;
4525 asconf_len = ntohs(asconf_ch->chunk_length);
4526 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4528 stcb = sctp_findassociation_ep_asconf(m,
4529 *offset, sh, &inp, netp, vrf_id);
4532 asconf_offset += SCTP_SIZE32(asconf_len);
4533 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4534 sizeof(struct sctp_chunkhdr), chunk_buf);
4535 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4538 * reduce inp's refcount if not reduced in
4539 * sctp_findassociation_ep_asconf().
4541 SCTP_INP_DECR_REF(inp);
4546 /* now go back and verify any auth chunk to be sure */
4547 if (auth_skipped && (stcb != NULL)) {
4548 struct sctp_auth_chunk *auth;
4550 auth = (struct sctp_auth_chunk *)
4551 sctp_m_getptr(m, auth_offset,
4552 auth_len, chunk_buf);
4555 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4557 /* auth HMAC failed so dump it */
4560 SCTP_TCB_UNLOCK(locked_tcb);
4564 /* remaining chunks are HMAC checked */
4565 stcb->asoc.authenticated = 1;
4570 /* no association, so it's out of the blue... */
4571 sctp_handle_ootb(m, iphlen, *offset, sh, inp,
4572 use_mflowid, mflowid,
4576 SCTP_TCB_UNLOCK(locked_tcb);
4581 /* ABORT and SHUTDOWN can use either v_tag... */
4582 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4583 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4584 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4585 if ((vtag_in == asoc->my_vtag) ||
4586 ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
4587 (vtag_in == asoc->peer_vtag))) {
4590 /* drop this packet... */
4591 SCTP_STAT_INCR(sctps_badvtag);
4593 SCTP_TCB_UNLOCK(locked_tcb);
4597 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4598 if (vtag_in != asoc->my_vtag) {
4600 * this could be a stale SHUTDOWN-ACK or the
4601 * peer never got the SHUTDOWN-COMPLETE and
4602 * is still hung; we have started a new asoc
4603 * but it won't complete until the shutdown
4607 SCTP_TCB_UNLOCK(locked_tcb);
4609 sctp_handle_ootb(m, iphlen, *offset, sh, inp,
4610 use_mflowid, mflowid,
4615 /* for all other chunks, vtag must match */
4616 if (vtag_in != asoc->my_vtag) {
4617 /* invalid vtag... */
4618 SCTPDBG(SCTP_DEBUG_INPUT3,
4619 "invalid vtag: %xh, expect %xh\n",
4620 vtag_in, asoc->my_vtag);
4621 SCTP_STAT_INCR(sctps_badvtag);
4623 SCTP_TCB_UNLOCK(locked_tcb);
4629 } /* end if !SCTP_COOKIE_ECHO */
4631 * process all control chunks...
4633 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4634 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4635 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4636 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4637 /* implied cookie-ack.. we must have lost the ack */
4638 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4639 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4640 stcb->asoc.overall_error_count,
4642 SCTP_FROM_SCTP_INPUT,
4645 stcb->asoc.overall_error_count = 0;
4646 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4649 process_control_chunks:
4650 while (IS_SCTP_CONTROL(ch)) {
4651 /* validate chunk length */
4652 chk_length = ntohs(ch->chunk_length);
4653 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4654 ch->chunk_type, chk_length);
4655 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4656 if (chk_length < sizeof(*ch) ||
4657 (*offset + (int)chk_length) > length) {
4660 SCTP_TCB_UNLOCK(locked_tcb);
4664 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4666 * INIT-ACK only gets the init ack "header" portion only
4667 * because we don't have to process the peer's COOKIE. All
4668 * others get a complete chunk.
4670 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4671 (ch->chunk_type == SCTP_INITIATION)) {
4672 /* get an init-ack chunk */
4673 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4674 sizeof(struct sctp_init_ack_chunk), chunk_buf);
4678 SCTP_TCB_UNLOCK(locked_tcb);
4683 /* For cookies and all other chunks. */
4684 if (chk_length > sizeof(chunk_buf)) {
4686 * use just the size of the chunk buffer so
4687 * the front part of our chunks fit in
4688 * contiguous space up to the chunk buffer
4689 * size (508 bytes). For chunks that need to
4690 * get more than that they must use the
4691 * sctp_m_getptr() function or other means
4692 * (e.g. know how to parse mbuf chains).
4693 * Cookies do this already.
4695 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4696 (sizeof(chunk_buf) - 4),
4701 SCTP_TCB_UNLOCK(locked_tcb);
4706 /* We can fit it all */
4707 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4708 chk_length, chunk_buf);
4710 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4713 SCTP_TCB_UNLOCK(locked_tcb);
4720 /* Save off the last place we got a control from */
4722 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4724 * allow last_control to be NULL if
4725 * ASCONF... ASCONF processing will find the
4728 if ((netp != NULL) && (*netp != NULL))
4729 stcb->asoc.last_control_chunk_from = *netp;
4732 #ifdef SCTP_AUDITING_ENABLED
4733 sctp_audit_log(0xB0, ch->chunk_type);
4736 /* check to see if this chunk required auth, but isn't */
4737 if ((stcb != NULL) &&
4738 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
4739 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4740 !stcb->asoc.authenticated) {
4741 /* "silently" ignore */
4742 SCTP_STAT_INCR(sctps_recvauthmissing);
4745 switch (ch->chunk_type) {
4746 case SCTP_INITIATION:
4747 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4748 /* The INIT chunk must be the only chunk. */
4749 if ((num_chunks > 1) ||
4750 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4751 sctp_abort_association(inp, stcb, m,
4753 use_mflowid, mflowid,
4758 /* Honor our resource limit. */
4759 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4760 struct mbuf *op_err;
4762 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
4763 sctp_abort_association(inp, stcb, m,
4765 use_mflowid, mflowid,
4770 sctp_handle_init(m, iphlen, *offset, sh,
4771 (struct sctp_init_chunk *)ch, inp,
4772 stcb, &abort_no_unlock,
4773 use_mflowid, mflowid,
4776 if ((!abort_no_unlock) && (locked_tcb)) {
4777 SCTP_TCB_UNLOCK(locked_tcb);
4781 case SCTP_PAD_CHUNK:
4783 case SCTP_INITIATION_ACK:
4784 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4785 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4786 /* We are not interested anymore */
4787 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4790 if (locked_tcb != stcb) {
4792 SCTP_TCB_UNLOCK(locked_tcb);
4796 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4797 so = SCTP_INP_SO(inp);
4798 atomic_add_int(&stcb->asoc.refcnt, 1);
4799 SCTP_TCB_UNLOCK(stcb);
4800 SCTP_SOCKET_LOCK(so, 1);
4801 SCTP_TCB_LOCK(stcb);
4802 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4804 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4805 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4806 SCTP_SOCKET_UNLOCK(so, 1);
4812 /* The INIT-ACK chunk must be the only chunk. */
4813 if ((num_chunks > 1) ||
4814 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4817 SCTP_TCB_UNLOCK(locked_tcb);
4821 if ((netp) && (*netp)) {
4822 ret = sctp_handle_init_ack(m, iphlen, *offset, sh,
4823 (struct sctp_init_ack_chunk *)ch,
4826 use_mflowid, mflowid,
4832 if (abort_no_unlock) {
4836 * Special case, I must call the output routine to
4837 * get the cookie echoed
4839 if ((stcb != NULL) && (ret == 0)) {
4840 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4843 SCTP_TCB_UNLOCK(locked_tcb);
4847 case SCTP_SELECTIVE_ACK:
4849 struct sctp_sack_chunk *sack;
4851 uint32_t a_rwnd, cum_ack;
4852 uint16_t num_seg, num_dup;
4854 int offset_seg, offset_dup;
4856 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4857 SCTP_STAT_INCR(sctps_recvsacks);
4859 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4862 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4863 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4866 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4868 * If we have sent a shutdown-ack, we will pay no
4869 * attention to a sack sent in to us since
4870 * we don't care anymore.
4874 sack = (struct sctp_sack_chunk *)ch;
4875 flags = ch->chunk_flags;
4876 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4877 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4878 num_dup = ntohs(sack->sack.num_dup_tsns);
4879 a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
4880 if (sizeof(struct sctp_sack_chunk) +
4881 num_seg * sizeof(struct sctp_gap_ack_block) +
4882 num_dup * sizeof(uint32_t) != chk_length) {
4883 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4886 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4887 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4888 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4889 cum_ack, num_seg, a_rwnd);
4890 stcb->asoc.seen_a_sack_this_pkt = 1;
4891 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4893 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4894 (stcb->asoc.saw_sack_with_frags == 0) &&
4895 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4896 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4899 * We have a SIMPLE sack having no
4900 * prior segments and data on sent
4901 * queue to be acked.. Use the
4902 * faster path sack processing. We
4903 * also allow window update sacks
4904 * with no missing segments to go
4907 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
4910 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4911 num_seg, 0, num_dup, &abort_now, flags,
4912 cum_ack, a_rwnd, ecne_seen);
4915 /* ABORT signal from sack processing */
4919 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4920 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4921 (stcb->asoc.stream_queue_cnt == 0)) {
4922 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4927 * EY - nr_sack: If the received chunk is an
4930 case SCTP_NR_SELECTIVE_ACK:
4932 struct sctp_nr_sack_chunk *nr_sack;
4934 uint32_t a_rwnd, cum_ack;
4935 uint16_t num_seg, num_nr_seg, num_dup;
4937 int offset_seg, offset_dup;
4939 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
4940 SCTP_STAT_INCR(sctps_recvsacks);
4942 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
4945 if ((stcb->asoc.sctp_nr_sack_on_off == 0) ||
4946 (stcb->asoc.peer_supports_nr_sack == 0)) {
4949 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4950 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
4953 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4955 * If we have sent a shutdown-ack, we will pay no
4956 * attention to a sack sent in to us since
4957 * we don't care anymore.
4961 nr_sack = (struct sctp_nr_sack_chunk *)ch;
4962 flags = ch->chunk_flags;
4963 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4964 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4965 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4966 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4967 a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd);
4968 if (sizeof(struct sctp_nr_sack_chunk) +
4969 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4970 num_dup * sizeof(uint32_t) != chk_length) {
4971 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4974 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4975 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4976 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4977 cum_ack, num_seg, a_rwnd);
4978 stcb->asoc.seen_a_sack_this_pkt = 1;
4979 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4980 (num_seg == 0) && (num_nr_seg == 0) &&
4981 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4982 (stcb->asoc.saw_sack_with_frags == 0) &&
4983 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4984 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
4986 * We have a SIMPLE sack having no
4987 * prior segments and data on sent
4988 * queue to be acked. Use the faster
4989 * path sack processing. We also
4990 * allow window update sacks with no
4991 * missing segments to go this way
4994 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
4995 &abort_now, ecne_seen);
4998 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4999 num_seg, num_nr_seg, num_dup, &abort_now, flags,
5000 cum_ack, a_rwnd, ecne_seen);
5003 /* ABORT signal from sack processing */
5007 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5008 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5009 (stcb->asoc.stream_queue_cnt == 0)) {
5010 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5015 case SCTP_HEARTBEAT_REQUEST:
5016 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
5017 if ((stcb) && netp && *netp) {
5018 SCTP_STAT_INCR(sctps_recvheartbeat);
5019 sctp_send_heartbeat_ack(stcb, m, *offset,
5022 /* He's alive so give him credit */
5023 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5024 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5025 stcb->asoc.overall_error_count,
5027 SCTP_FROM_SCTP_INPUT,
5030 stcb->asoc.overall_error_count = 0;
5033 case SCTP_HEARTBEAT_ACK:
5034 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
5035 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5039 SCTP_TCB_UNLOCK(locked_tcb);
5043 /* He's alive so give him credit */
5044 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5045 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5046 stcb->asoc.overall_error_count,
5048 SCTP_FROM_SCTP_INPUT,
5051 stcb->asoc.overall_error_count = 0;
5052 SCTP_STAT_INCR(sctps_recvheartbeatack);
5054 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5057 case SCTP_ABORT_ASSOCIATION:
5058 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5060 if ((stcb) && netp && *netp)
5061 sctp_handle_abort((struct sctp_abort_chunk *)ch,
5067 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5069 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5072 SCTP_TCB_UNLOCK(locked_tcb);
5076 if (netp && *netp) {
5079 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5080 stcb, *netp, &abort_flag);
5087 case SCTP_SHUTDOWN_ACK:
5088 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb);
5089 if ((stcb) && (netp) && (*netp))
5090 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5095 case SCTP_OPERATION_ERROR:
5096 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
5097 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
5102 case SCTP_COOKIE_ECHO:
5103 SCTPDBG(SCTP_DEBUG_INPUT3,
5104 "SCTP_COOKIE-ECHO, stcb %p\n", stcb);
5105 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5108 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5109 /* We are not interested anymore */
5112 SCTP_TCB_UNLOCK(stcb);
5119 * First are we accepting? We do this again here
5120 * since it is possible that a previous endpoint WAS
5121 * listening responded to a INIT-ACK and then
5122 * closed. We opened and bound.. and are now no
5126 if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
5127 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5128 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5129 struct mbuf *op_err;
5131 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
5132 sctp_abort_association(inp, stcb, m,
5134 use_mflowid, mflowid,
5140 struct mbuf *ret_buf;
5141 struct sctp_inpcb *linp;
5150 SCTP_ASOC_CREATE_LOCK(linp);
5151 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5152 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5153 SCTP_ASOC_CREATE_UNLOCK(linp);
5159 sctp_handle_cookie_echo(m, iphlen,
5161 (struct sctp_cookie_echo_chunk *)ch,
5175 SCTP_ASOC_CREATE_UNLOCK(linp);
5177 if (ret_buf == NULL) {
5179 SCTP_TCB_UNLOCK(locked_tcb);
5181 SCTPDBG(SCTP_DEBUG_INPUT3,
5182 "GAK, null buffer\n");
5186 /* if AUTH skipped, see if it verified... */
5191 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5193 * Restart the timer if we have
5196 struct sctp_tmit_chunk *chk;
5198 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5199 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5203 case SCTP_COOKIE_ACK:
5204 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb);
5205 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5207 SCTP_TCB_UNLOCK(locked_tcb);
5211 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5212 /* We are not interested anymore */
5213 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5216 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5217 so = SCTP_INP_SO(inp);
5218 atomic_add_int(&stcb->asoc.refcnt, 1);
5219 SCTP_TCB_UNLOCK(stcb);
5220 SCTP_SOCKET_LOCK(so, 1);
5221 SCTP_TCB_LOCK(stcb);
5222 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5224 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
5225 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5226 SCTP_SOCKET_UNLOCK(so, 1);
5232 /* He's alive so give him credit */
5233 if ((stcb) && netp && *netp) {
5234 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5235 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5236 stcb->asoc.overall_error_count,
5238 SCTP_FROM_SCTP_INPUT,
5241 stcb->asoc.overall_error_count = 0;
5242 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5246 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5247 /* He's alive so give him credit */
5248 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5251 SCTP_TCB_UNLOCK(locked_tcb);
5257 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5258 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5259 stcb->asoc.overall_error_count,
5261 SCTP_FROM_SCTP_INPUT,
5264 stcb->asoc.overall_error_count = 0;
5265 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5271 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5272 /* He's alive so give him credit */
5273 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5276 SCTP_TCB_UNLOCK(locked_tcb);
5282 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5283 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5284 stcb->asoc.overall_error_count,
5286 SCTP_FROM_SCTP_INPUT,
5289 stcb->asoc.overall_error_count = 0;
5290 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5293 case SCTP_SHUTDOWN_COMPLETE:
5294 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb);
5295 /* must be first and only chunk */
5296 if ((num_chunks > 1) ||
5297 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5300 SCTP_TCB_UNLOCK(locked_tcb);
5304 if ((stcb) && netp && *netp) {
5305 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5312 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5313 /* He's alive so give him credit */
5315 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5316 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5317 stcb->asoc.overall_error_count,
5319 SCTP_FROM_SCTP_INPUT,
5322 stcb->asoc.overall_error_count = 0;
5323 sctp_handle_asconf(m, *offset,
5324 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5328 case SCTP_ASCONF_ACK:
5329 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5330 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5333 SCTP_TCB_UNLOCK(locked_tcb);
5338 if ((stcb) && netp && *netp) {
5339 /* He's alive so give him credit */
5340 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5341 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5342 stcb->asoc.overall_error_count,
5344 SCTP_FROM_SCTP_INPUT,
5347 stcb->asoc.overall_error_count = 0;
5348 sctp_handle_asconf_ack(m, *offset,
5349 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5350 if (abort_no_unlock)
5354 case SCTP_FORWARD_CUM_TSN:
5355 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5356 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5359 SCTP_TCB_UNLOCK(locked_tcb);
5364 /* He's alive so give him credit */
5368 stcb->asoc.overall_error_count = 0;
5369 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5370 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5371 stcb->asoc.overall_error_count,
5373 SCTP_FROM_SCTP_INPUT,
5377 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5378 /* We are not interested anymore */
5379 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5380 so = SCTP_INP_SO(inp);
5381 atomic_add_int(&stcb->asoc.refcnt, 1);
5382 SCTP_TCB_UNLOCK(stcb);
5383 SCTP_SOCKET_LOCK(so, 1);
5384 SCTP_TCB_LOCK(stcb);
5385 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5387 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
5388 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5389 SCTP_SOCKET_UNLOCK(so, 1);
5394 sctp_handle_forward_tsn(stcb,
5395 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5400 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5401 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5402 stcb->asoc.overall_error_count,
5404 SCTP_FROM_SCTP_INPUT,
5407 stcb->asoc.overall_error_count = 0;
5412 case SCTP_STREAM_RESET:
5413 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5414 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5417 SCTP_TCB_UNLOCK(locked_tcb);
5422 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5423 /* We are not interested anymore */
5424 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5425 so = SCTP_INP_SO(inp);
5426 atomic_add_int(&stcb->asoc.refcnt, 1);
5427 SCTP_TCB_UNLOCK(stcb);
5428 SCTP_SOCKET_LOCK(so, 1);
5429 SCTP_TCB_LOCK(stcb);
5430 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5432 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5433 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5434 SCTP_SOCKET_UNLOCK(so, 1);
5439 if (stcb->asoc.peer_supports_strreset == 0) {
5441 * hmm, peer should have announced this, but
5442 * we will turn it on since he is sending us
5445 stcb->asoc.peer_supports_strreset = 1;
5447 if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) {
5448 /* stop processing */
5453 case SCTP_PACKET_DROPPED:
5454 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5455 /* re-get it all please */
5456 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5459 SCTP_TCB_UNLOCK(locked_tcb);
5464 if (ch && (stcb) && netp && (*netp)) {
5465 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5467 min(chk_length, (sizeof(chunk_buf) - 4)));
5472 case SCTP_AUTHENTICATION:
5473 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5474 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
5478 /* save the first AUTH for later processing */
5479 if (auth_skipped == 0) {
5480 auth_offset = *offset;
5481 auth_len = chk_length;
5484 /* skip this chunk (temporarily) */
5487 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5488 (chk_length > (sizeof(struct sctp_auth_chunk) +
5489 SCTP_AUTH_DIGEST_LEN_MAX))) {
5492 SCTP_TCB_UNLOCK(locked_tcb);
5497 if (got_auth == 1) {
5498 /* skip this chunk... it's already auth'd */
5502 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5504 /* auth HMAC failed so dump the packet */
5508 /* remaining chunks are HMAC checked */
5509 stcb->asoc.authenticated = 1;
5515 /* it's an unknown chunk! */
5516 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5518 struct sctp_paramhdr *phd;
5520 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
5521 0, M_DONTWAIT, 1, MT_DATA);
5523 phd = mtod(mm, struct sctp_paramhdr *);
5525 * We cheat and use param type since
5526 * we did not bother to define a
5527 * error cause struct. They are the
5528 * same basic format with different
5531 phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5532 phd->param_length = htons(chk_length + sizeof(*phd));
5533 SCTP_BUF_LEN(mm) = sizeof(*phd);
5534 SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, chk_length, M_DONTWAIT);
5535 if (SCTP_BUF_NEXT(mm)) {
5536 if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(mm), SCTP_SIZE32(chk_length) - chk_length, NULL)) {
5539 #ifdef SCTP_MBUF_LOGGING
5540 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5543 for (mat = SCTP_BUF_NEXT(mm); mat; mat = SCTP_BUF_NEXT(mat)) {
5544 if (SCTP_BUF_IS_EXTENDED(mat)) {
5545 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5550 sctp_queue_op_err(stcb, mm);
5557 if ((ch->chunk_type & 0x80) == 0) {
5558 /* discard this packet */
5561 } /* else skip this bad chunk and continue... */
5563 } /* switch (ch->chunk_type) */
5567 /* get the next chunk */
5568 *offset += SCTP_SIZE32(chk_length);
5569 if (*offset >= length) {
5570 /* no more data left in the mbuf chain */
5573 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5574 sizeof(struct sctp_chunkhdr), chunk_buf);
5577 SCTP_TCB_UNLOCK(locked_tcb);
5584 if (asconf_cnt > 0 && stcb != NULL) {
5585 sctp_send_asconf_ack(stcb);
5593 __attribute__((noinline))
5596 sctp_validate_no_locks(struct sctp_inpcb *inp)
5598 struct sctp_tcb *lstcb;
5600 LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) {
5601 if (mtx_owned(&lstcb->tcb_mtx)) {
5602 panic("Own lock on stcb at return from input");
5605 if (mtx_owned(&inp->inp_create_mtx)) {
5606 panic("Own create lock on inp");
5608 if (mtx_owned(&inp->inp_mtx)) {
5609 panic("Own inp lock on inp");
5616 * common input chunk processing (v4 and v6)
5619 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset,
5620 int length, struct sctphdr *sh, struct sctp_chunkhdr *ch,
5621 struct sctp_inpcb *inp, struct sctp_tcb *stcb,
5622 struct sctp_nets *net, uint8_t ecn_bits,
5623 uint8_t use_mflowid, uint32_t mflowid,
5624 uint32_t vrf_id, uint16_t port)
5627 * Control chunk processing
5630 int fwd_tsn_seen = 0, data_processed = 0;
5631 struct mbuf *m = *mm;
5633 int cnt_ctrl_ready = 0;
5635 SCTP_STAT_INCR(sctps_recvdatagrams);
5636 #ifdef SCTP_AUDITING_ENABLED
5637 sctp_audit_log(0xE0, 1);
5638 sctp_auditing(0, inp, stcb, net);
5641 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5642 m, iphlen, offset, length, stcb);
5644 /* always clear this before beginning a packet */
5645 stcb->asoc.authenticated = 0;
5646 stcb->asoc.seen_a_sack_this_pkt = 0;
5647 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5648 stcb, stcb->asoc.state);
5650 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5651 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5653 * If we hit here, we had a ref count
5654 * up when the assoc was aborted and the
5655 * timer is clearing out the assoc, we should
5656 * NOT respond to any packet.. its OOTB.
5658 SCTP_TCB_UNLOCK(stcb);
5659 sctp_handle_ootb(m, iphlen, offset, sh, inp,
5660 use_mflowid, mflowid,
5665 if (IS_SCTP_CONTROL(ch)) {
5666 /* process the control portion of the SCTP packet */
5667 /* sa_ignore NO_NULL_CHK */
5668 stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch,
5669 inp, stcb, &net, &fwd_tsn_seen,
5670 use_mflowid, mflowid,
5674 * This covers us if the cookie-echo was there and
5675 * it changes our INP.
5677 inp = stcb->sctp_ep;
5678 if ((net) && (port)) {
5679 if (net->port == 0) {
5680 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5687 * no control chunks, so pre-process DATA chunks (these
5688 * checks are taken care of by control processing)
5692 * if DATA only packet, and auth is required, then punt...
5693 * can't have authenticated without any AUTH (control)
5696 if ((stcb != NULL) &&
5697 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5698 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5699 /* "silently" ignore */
5700 SCTP_STAT_INCR(sctps_recvauthmissing);
5701 SCTP_TCB_UNLOCK(stcb);
5705 /* out of the blue DATA chunk */
5706 sctp_handle_ootb(m, iphlen, offset, sh, inp,
5707 use_mflowid, mflowid,
5711 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5712 /* v_tag mismatch! */
5713 SCTP_STAT_INCR(sctps_badvtag);
5714 SCTP_TCB_UNLOCK(stcb);
5721 * no valid TCB for this packet, or we found it's a bad
5722 * packet while processing control, or we're done with this
5723 * packet (done or skip rest of data), so we drop it...
5728 * DATA chunk processing
5730 /* plow through the data chunks while length > offset */
5733 * Rest should be DATA only. Check authentication state if AUTH for
5736 if ((length > offset) &&
5738 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5739 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5740 !stcb->asoc.authenticated) {
5741 /* "silently" ignore */
5742 SCTP_STAT_INCR(sctps_recvauthmissing);
5743 SCTPDBG(SCTP_DEBUG_AUTH1,
5744 "Data chunk requires AUTH, skipped\n");
5747 if (length > offset) {
5751 * First check to make sure our state is correct. We would
5752 * not get here unless we really did have a tag, so we don't
5753 * abort if this happens, just dump the chunk silently.
5755 switch (SCTP_GET_STATE(&stcb->asoc)) {
5756 case SCTP_STATE_COOKIE_ECHOED:
5758 * we consider data with valid tags in this state
5759 * shows us the cookie-ack was lost. Imply it was
5762 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5763 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5764 stcb->asoc.overall_error_count,
5766 SCTP_FROM_SCTP_INPUT,
5769 stcb->asoc.overall_error_count = 0;
5770 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5772 case SCTP_STATE_COOKIE_WAIT:
5774 * We consider OOTB any data sent during asoc setup.
5776 sctp_handle_ootb(m, iphlen, offset, sh, inp,
5777 use_mflowid, mflowid,
5779 SCTP_TCB_UNLOCK(stcb);
5781 /* sa_ignore NOTREACHED */
5783 case SCTP_STATE_EMPTY: /* should not happen */
5784 case SCTP_STATE_INUSE: /* should not happen */
5785 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5786 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5788 SCTP_TCB_UNLOCK(stcb);
5790 /* sa_ignore NOTREACHED */
5792 case SCTP_STATE_OPEN:
5793 case SCTP_STATE_SHUTDOWN_SENT:
5796 /* plow through the data chunks while length > offset */
5797 retval = sctp_process_data(mm, iphlen, &offset, length, sh,
5798 inp, stcb, net, &high_tsn,
5799 use_mflowid, mflowid,
5803 * The association aborted, NO UNLOCK needed since
5804 * the association is destroyed.
5810 * Anything important needs to have been m_copy'ed in
5814 /* take care of ecn */
5815 if ((data_processed == 1) &&
5816 (stcb->asoc.ecn_allowed == 1) &&
5817 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5818 /* Yep, we need to add a ECNE */
5819 sctp_send_ecn_echo(stcb, net, high_tsn);
5821 if ((data_processed == 0) && (fwd_tsn_seen)) {
5823 uint32_t highest_tsn;
5825 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5826 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5828 highest_tsn = stcb->asoc.highest_tsn_inside_map;
5830 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
5831 stcb->asoc.send_sack = 1;
5832 sctp_sack_check(stcb, was_a_gap);
5833 } else if (fwd_tsn_seen) {
5834 stcb->asoc.send_sack = 1;
5836 /* trigger send of any chunks in queue... */
5838 #ifdef SCTP_AUDITING_ENABLED
5839 sctp_audit_log(0xE0, 2);
5840 sctp_auditing(1, inp, stcb, net);
5842 SCTPDBG(SCTP_DEBUG_INPUT1,
5843 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5844 stcb->asoc.peers_rwnd,
5845 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5846 stcb->asoc.total_flight);
5847 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5848 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5849 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5851 if (cnt_ctrl_ready ||
5853 (stcb->asoc.peers_rwnd > 0 ||
5854 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5855 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5856 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5857 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5859 #ifdef SCTP_AUDITING_ENABLED
5860 sctp_audit_log(0xE0, 3);
5861 sctp_auditing(2, inp, stcb, net);
5863 SCTP_TCB_UNLOCK(stcb);
5866 sctp_validate_no_locks(inp);
5873 sctp_print_mbuf_chain(struct mbuf *m)
5875 for (; m; m = SCTP_BUF_NEXT(m)) {
5876 SCTP_PRINTF("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m));
5877 if (SCTP_BUF_IS_EXTENDED(m))
5878 SCTP_PRINTF("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m));
5886 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5890 uint32_t vrf_id = 0;
5894 struct sctp_inpcb *inp = NULL;
5895 struct sctp_nets *net;
5896 struct sctp_tcb *stcb = NULL;
5897 struct sctp_chunkhdr *ch;
5898 int refcount_up = 0;
5899 int length, mlen, offset;
5901 uint8_t use_mflowid;
5903 #if !defined(SCTP_WITH_NO_CSUM)
5904 uint32_t check, calc_check;
5908 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
5909 SCTP_RELEASE_PKT(i_pak);
5912 mlen = SCTP_HEADER_LEN(i_pak);
5914 m = SCTP_HEADER_TO_CHAIN(i_pak);
5917 SCTP_STAT_INCR(sctps_recvpackets);
5918 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
5921 #ifdef SCTP_MBUF_LOGGING
5922 /* Log in any input mbufs */
5923 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5926 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
5927 if (SCTP_BUF_IS_EXTENDED(mat)) {
5928 sctp_log_mb(mat, SCTP_MBUF_INPUT);
5933 #ifdef SCTP_PACKET_LOGGING
5934 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
5935 sctp_packet_log(m, mlen);
5937 if (m->m_flags & M_FLOWID) {
5938 mflowid = m->m_pkthdr.flowid;
5945 * Must take out the iphlen, since mlen expects this (only effect lb
5951 * Get IP, SCTP, and first chunk header together in first mbuf.
5953 ip = mtod(m, struct ip *);
5954 offset = iphlen + sizeof(*sh) + sizeof(*ch);
5955 if (SCTP_BUF_LEN(m) < offset) {
5956 if ((m = m_pullup(m, offset)) == 0) {
5957 SCTP_STAT_INCR(sctps_hdrops);
5960 ip = mtod(m, struct ip *);
5962 /* validate mbuf chain length with IP payload length */
5963 if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) {
5964 SCTP_STAT_INCR(sctps_hdrops);
5967 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
5968 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh));
5969 SCTPDBG(SCTP_DEBUG_INPUT1,
5970 "sctp_input() length:%d iphlen:%d\n", mlen, iphlen);
5972 /* SCTP does not allow broadcasts or multicasts */
5973 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
5976 if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) {
5978 * We only look at broadcast if its a front state, All
5979 * others we will not have a tcb for anyway.
5983 /* validate SCTP checksum */
5984 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
5985 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
5987 if_name(m->m_pkthdr.rcvif),
5988 m->m_pkthdr.csum_flags);
5989 #if defined(SCTP_WITH_NO_CSUM)
5990 SCTP_STAT_INCR(sctps_recvnocrc);
5992 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5993 SCTP_STAT_INCR(sctps_recvhwcrc);
5994 goto sctp_skip_csum_4;
5996 check = sh->checksum; /* save incoming checksum */
5997 sh->checksum = 0; /* prepare for calc */
5998 calc_check = sctp_calculate_cksum(m, iphlen);
5999 sh->checksum = check;
6000 SCTP_STAT_INCR(sctps_recvswcrc);
6001 if (calc_check != check) {
6002 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
6003 calc_check, check, m, mlen, iphlen);
6005 stcb = sctp_findassociation_addr(m,
6006 offset - sizeof(*ch),
6009 if ((net) && (port)) {
6010 if (net->port == 0) {
6011 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
6015 if ((net != NULL) && (use_mflowid != 0)) {
6016 net->flowid = mflowid;
6021 if ((inp) && (stcb)) {
6022 sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
6023 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
6024 } else if ((inp != NULL) && (stcb == NULL)) {
6027 SCTP_STAT_INCR(sctps_badsum);
6028 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
6033 /* destination port of 0 is illegal, based on RFC2960. */
6034 if (sh->dest_port == 0) {
6035 SCTP_STAT_INCR(sctps_hdrops);
6039 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
6040 * IP/SCTP/first chunk header...
6042 stcb = sctp_findassociation_addr(m, offset - sizeof(*ch),
6043 sh, ch, &inp, &net, vrf_id);
6044 if ((net) && (port)) {
6045 if (net->port == 0) {
6046 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
6050 if ((net != NULL) && (use_mflowid != 0)) {
6051 net->flowid = mflowid;
6056 /* inp's ref-count increased && stcb locked */
6058 struct sctp_init_chunk *init_chk, chunk_buf;
6060 SCTP_STAT_INCR(sctps_noport);
6061 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0)
6063 SCTPDBG(SCTP_DEBUG_INPUT1,
6064 "Sending a ABORT from packet entry!\n");
6065 if (ch->chunk_type == SCTP_INITIATION) {
6067 * we do a trick here to get the INIT tag, dig in
6068 * and get the tag from the INIT and put it in the
6071 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
6072 iphlen + sizeof(*sh), sizeof(*init_chk),
6073 (uint8_t *) & chunk_buf);
6074 if (init_chk != NULL)
6075 sh->v_tag = init_chk->init.initiate_tag;
6077 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
6078 sctp_send_shutdown_complete2(m, sh,
6079 use_mflowid, mflowid,
6083 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
6086 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
6087 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
6088 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
6089 (ch->chunk_type != SCTP_INIT))) {
6090 sctp_send_abort(m, iphlen, sh, 0, NULL,
6091 use_mflowid, mflowid,
6096 } else if (stcb == NULL) {
6101 * I very much doubt any of the IPSEC stuff will work but I have no
6102 * idea, so I will leave it in place.
6104 if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) {
6105 MODULE_GLOBAL(ipsec4stat).in_polvio++;
6106 SCTP_STAT_INCR(sctps_hdrops);
6112 * common chunk processing
6114 length = ip->ip_len + iphlen;
6115 offset -= sizeof(struct sctp_chunkhdr);
6117 ecn_bits = ip->ip_tos;
6119 /* sa_ignore NO_NULL_CHK */
6120 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
6121 inp, stcb, net, ecn_bits,
6122 use_mflowid, mflowid,
6124 /* inp's ref-count reduced && stcb unlocked */
6128 if ((inp) && (refcount_up)) {
6129 /* reduce ref-count */
6130 SCTP_INP_DECR_REF(inp);
6135 SCTP_TCB_UNLOCK(stcb);
6137 if ((inp) && (refcount_up)) {
6138 /* reduce ref-count */
6139 SCTP_INP_DECR_REF(inp);
6147 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6148 extern int *sctp_cpuarry;
6153 sctp_input(struct mbuf *m, int off)
6155 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6160 uint32_t flowid, tag;
6163 if (m->m_flags & M_FLOWID) {
6164 flowid = m->m_pkthdr.flowid;
6167 * No flow id built by lower layers fix it so we
6170 ip = mtod(m, struct ip *);
6171 offset = off + sizeof(*sh);
6172 if (SCTP_BUF_LEN(m) < offset) {
6173 if ((m = m_pullup(m, offset)) == 0) {
6174 SCTP_STAT_INCR(sctps_hdrops);
6177 ip = mtod(m, struct ip *);
6179 sh = (struct sctphdr *)((caddr_t)ip + off);
6180 tag = htonl(sh->v_tag);
6181 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6182 m->m_pkthdr.flowid = flowid;
6183 m->m_flags |= M_FLOWID;
6185 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6186 sctp_queue_to_mcore(m, off, cpu_to_use);
6190 sctp_input_with_port(m, off, 0);